2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include "kerncompat.h"
21 #include <sys/ioctl.h>
22 #include <sys/mount.h>
26 #include <sys/types.h>
32 #include <uuid/uuid.h>
34 #include <sys/xattr.h>
36 #include <linux/limits.h>
37 #include <blkid/blkid.h>
42 #include "transaction.h"
45 static u64 index_cnt = 2;
46 static int verbose = 1;
48 struct directory_name_entry {
52 struct list_head list;
55 struct mkfs_allocation {
62 static int create_metadata_block_groups(struct btrfs_root *root, int mixed,
63 struct mkfs_allocation *allocation)
65 struct btrfs_trans_handle *trans;
71 trans = btrfs_start_transaction(root, 1);
72 bytes_used = btrfs_super_bytes_used(root->fs_info->super_copy);
74 root->fs_info->system_allocs = 1;
75 ret = btrfs_make_block_group(trans, root, bytes_used,
76 BTRFS_BLOCK_GROUP_SYSTEM,
77 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
78 0, BTRFS_MKFS_SYSTEM_GROUP_SIZE);
79 allocation->system += BTRFS_MKFS_SYSTEM_GROUP_SIZE;
83 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
84 &chunk_start, &chunk_size,
85 BTRFS_BLOCK_GROUP_METADATA |
86 BTRFS_BLOCK_GROUP_DATA);
89 "no space to alloc data/metadata chunk\n");
93 ret = btrfs_make_block_group(trans, root, 0,
94 BTRFS_BLOCK_GROUP_METADATA |
95 BTRFS_BLOCK_GROUP_DATA,
96 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
97 chunk_start, chunk_size);
99 allocation->mixed += chunk_size;
101 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
102 &chunk_start, &chunk_size,
103 BTRFS_BLOCK_GROUP_METADATA);
104 if (ret == -ENOSPC) {
105 fprintf(stderr, "no space to alloc metadata chunk\n");
109 ret = btrfs_make_block_group(trans, root, 0,
110 BTRFS_BLOCK_GROUP_METADATA,
111 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
112 chunk_start, chunk_size);
113 allocation->metadata += chunk_size;
117 root->fs_info->system_allocs = 0;
118 btrfs_commit_transaction(trans, root);
124 static int create_data_block_groups(struct btrfs_trans_handle *trans,
125 struct btrfs_root *root, int mixed,
126 struct mkfs_allocation *allocation)
133 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
134 &chunk_start, &chunk_size,
135 BTRFS_BLOCK_GROUP_DATA);
136 if (ret == -ENOSPC) {
137 fprintf(stderr, "no space to alloc data chunk\n");
141 ret = btrfs_make_block_group(trans, root, 0,
142 BTRFS_BLOCK_GROUP_DATA,
143 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
144 chunk_start, chunk_size);
145 allocation->data += chunk_size;
153 static int make_root_dir(struct btrfs_trans_handle *trans, struct btrfs_root *root,
154 int mixed, struct mkfs_allocation *allocation)
156 struct btrfs_key location;
159 ret = btrfs_make_root_dir(trans, root->fs_info->tree_root,
160 BTRFS_ROOT_TREE_DIR_OBJECTID);
163 ret = btrfs_make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID);
166 memcpy(&location, &root->fs_info->fs_root->root_key, sizeof(location));
167 location.offset = (u64)-1;
168 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
170 btrfs_super_root_dir(root->fs_info->super_copy),
171 &location, BTRFS_FT_DIR, 0);
175 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
176 "default", 7, location.objectid,
177 BTRFS_ROOT_TREE_DIR_OBJECTID, 0);
185 static void __recow_root(struct btrfs_trans_handle *trans,
186 struct btrfs_root *root)
189 struct extent_buffer *tmp;
191 if (trans->transid != btrfs_root_generation(&root->root_item)) {
192 extent_buffer_get(root->node);
193 ret = __btrfs_cow_block(trans, root, root->node,
194 NULL, 0, &tmp, 0, 0);
196 free_extent_buffer(tmp);
200 static void recow_roots(struct btrfs_trans_handle *trans,
201 struct btrfs_root *root)
203 struct btrfs_fs_info *info = root->fs_info;
205 __recow_root(trans, info->fs_root);
206 __recow_root(trans, info->tree_root);
207 __recow_root(trans, info->extent_root);
208 __recow_root(trans, info->chunk_root);
209 __recow_root(trans, info->dev_root);
210 __recow_root(trans, info->csum_root);
213 static int create_one_raid_group(struct btrfs_trans_handle *trans,
214 struct btrfs_root *root, u64 type,
215 struct mkfs_allocation *allocation)
222 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
223 &chunk_start, &chunk_size, type);
224 if (ret == -ENOSPC) {
225 fprintf(stderr, "not enough free space\n");
229 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
230 type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
231 chunk_start, chunk_size);
232 if ((type & BTRFS_BLOCK_GROUP_TYPE_MASK) == BTRFS_BLOCK_GROUP_DATA)
233 allocation->data += chunk_size;
234 else if ((type & BTRFS_BLOCK_GROUP_TYPE_MASK) == BTRFS_BLOCK_GROUP_METADATA)
235 allocation->metadata += chunk_size;
236 else if ((type & BTRFS_BLOCK_GROUP_TYPE_MASK) == BTRFS_BLOCK_GROUP_SYSTEM)
237 allocation->system += chunk_size;
238 else if ((type & BTRFS_BLOCK_GROUP_TYPE_MASK) ==
239 (BTRFS_BLOCK_GROUP_METADATA|BTRFS_BLOCK_GROUP_DATA))
240 allocation->mixed += chunk_size;
248 static int create_raid_groups(struct btrfs_trans_handle *trans,
249 struct btrfs_root *root, u64 data_profile,
250 u64 metadata_profile, int mixed,
251 struct mkfs_allocation *allocation)
253 u64 num_devices = btrfs_super_num_devices(root->fs_info->super_copy);
256 if (metadata_profile) {
257 u64 meta_flags = BTRFS_BLOCK_GROUP_METADATA;
259 ret = create_one_raid_group(trans, root,
260 BTRFS_BLOCK_GROUP_SYSTEM |
261 metadata_profile, allocation);
265 meta_flags |= BTRFS_BLOCK_GROUP_DATA;
267 ret = create_one_raid_group(trans, root, meta_flags |
268 metadata_profile, allocation);
272 if (!mixed && num_devices > 1 && data_profile) {
273 ret = create_one_raid_group(trans, root,
274 BTRFS_BLOCK_GROUP_DATA |
275 data_profile, allocation);
278 recow_roots(trans, root);
283 static int create_data_reloc_tree(struct btrfs_trans_handle *trans,
284 struct btrfs_root *root)
286 struct btrfs_key location;
287 struct btrfs_root_item root_item;
288 struct extent_buffer *tmp;
289 u64 objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
292 ret = btrfs_copy_root(trans, root, root->node, &tmp, objectid);
295 memcpy(&root_item, &root->root_item, sizeof(root_item));
296 btrfs_set_root_bytenr(&root_item, tmp->start);
297 btrfs_set_root_level(&root_item, btrfs_header_level(tmp));
298 btrfs_set_root_generation(&root_item, trans->transid);
299 free_extent_buffer(tmp);
301 location.objectid = objectid;
302 location.type = BTRFS_ROOT_ITEM_KEY;
304 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
305 &location, &root_item);
310 static void print_usage(int ret)
312 fprintf(stderr, "usage: mkfs.btrfs [options] dev [ dev ... ]\n");
313 fprintf(stderr, "options:\n");
314 fprintf(stderr, "\t-A|--alloc-start START the offset to start the FS\n");
315 fprintf(stderr, "\t-b|--byte-count SIZE total number of bytes in the FS\n");
316 fprintf(stderr, "\t-d|--data PROFILE data profile, raid0, raid1, raid5, raid6, raid10, dup or single\n");
317 fprintf(stderr, "\t-f|--force force overwrite of existing filesystem\n");
318 fprintf(stderr, "\t-l|--leafsize SIZE deprecated, alias for nodesize\n");
319 fprintf(stderr, "\t-L|--label LABEL set a label\n");
320 fprintf(stderr, "\t-m|--metadata PROFILE metadata profile, values like data profile\n");
321 fprintf(stderr, "\t-M|--mixed mix metadata and data together\n");
322 fprintf(stderr, "\t-n|--nodesize SIZE size of btree nodes\n");
323 fprintf(stderr, "\t-s|--sectorsize SIZE min block allocation (may not mountable by current kernel)\n");
324 fprintf(stderr, "\t-r|--rootdir DIR the source directory\n");
325 fprintf(stderr, "\t-K|--nodiscard do not perform whole device TRIM\n");
326 fprintf(stderr, "\t-O|--features LIST comma separated list of filesystem features, use '-O list-all' to list features\n");
327 fprintf(stderr, "\t-U|--uuid UUID specify the filesystem UUID\n");
328 fprintf(stderr, "\t-q|--quiet no messages except errors\n");
329 fprintf(stderr, "\t-V|--version print the mkfs.btrfs version and exit\n");
330 fprintf(stderr, "%s\n", PACKAGE_STRING);
334 static void print_version(void) __attribute__((noreturn));
335 static void print_version(void)
337 fprintf(stderr, "mkfs.btrfs, part of %s\n", PACKAGE_STRING);
341 static u64 parse_profile(char *s)
343 if (strcmp(s, "raid0") == 0) {
344 return BTRFS_BLOCK_GROUP_RAID0;
345 } else if (strcasecmp(s, "raid1") == 0) {
346 return BTRFS_BLOCK_GROUP_RAID1;
347 } else if (strcasecmp(s, "raid5") == 0) {
348 return BTRFS_BLOCK_GROUP_RAID5;
349 } else if (strcasecmp(s, "raid6") == 0) {
350 return BTRFS_BLOCK_GROUP_RAID6;
351 } else if (strcasecmp(s, "raid10") == 0) {
352 return BTRFS_BLOCK_GROUP_RAID10;
353 } else if (strcasecmp(s, "dup") == 0) {
354 return BTRFS_BLOCK_GROUP_DUP;
355 } else if (strcasecmp(s, "single") == 0) {
358 fprintf(stderr, "Unknown profile %s\n", s);
365 static char *parse_label(char *input)
367 int len = strlen(input);
369 if (len >= BTRFS_LABEL_SIZE) {
370 fprintf(stderr, "Label %s is too long (max %d)\n", input,
371 BTRFS_LABEL_SIZE - 1);
374 return strdup(input);
377 static int add_directory_items(struct btrfs_trans_handle *trans,
378 struct btrfs_root *root, u64 objectid,
379 ino_t parent_inum, const char *name,
380 struct stat *st, int *dir_index_cnt)
384 struct btrfs_key location;
387 name_len = strlen(name);
389 location.objectid = objectid;
391 btrfs_set_key_type(&location, BTRFS_INODE_ITEM_KEY);
393 if (S_ISDIR(st->st_mode))
394 filetype = BTRFS_FT_DIR;
395 if (S_ISREG(st->st_mode))
396 filetype = BTRFS_FT_REG_FILE;
397 if (S_ISLNK(st->st_mode))
398 filetype = BTRFS_FT_SYMLINK;
400 ret = btrfs_insert_dir_item(trans, root, name, name_len,
401 parent_inum, &location,
402 filetype, index_cnt);
405 ret = btrfs_insert_inode_ref(trans, root, name, name_len,
406 objectid, parent_inum, index_cnt);
407 *dir_index_cnt = index_cnt;
413 static int fill_inode_item(struct btrfs_trans_handle *trans,
414 struct btrfs_root *root,
415 struct btrfs_inode_item *dst, struct stat *src)
418 u64 sectorsize = root->sectorsize;
421 * btrfs_inode_item has some reserved fields
422 * and represents on-disk inode entry, so
423 * zero everything to prevent information leak
425 memset(dst, 0, sizeof (*dst));
427 btrfs_set_stack_inode_generation(dst, trans->transid);
428 btrfs_set_stack_inode_size(dst, src->st_size);
429 btrfs_set_stack_inode_nbytes(dst, 0);
430 btrfs_set_stack_inode_block_group(dst, 0);
431 btrfs_set_stack_inode_nlink(dst, src->st_nlink);
432 btrfs_set_stack_inode_uid(dst, src->st_uid);
433 btrfs_set_stack_inode_gid(dst, src->st_gid);
434 btrfs_set_stack_inode_mode(dst, src->st_mode);
435 btrfs_set_stack_inode_rdev(dst, 0);
436 btrfs_set_stack_inode_flags(dst, 0);
437 btrfs_set_stack_timespec_sec(&dst->atime, src->st_atime);
438 btrfs_set_stack_timespec_nsec(&dst->atime, 0);
439 btrfs_set_stack_timespec_sec(&dst->ctime, src->st_ctime);
440 btrfs_set_stack_timespec_nsec(&dst->ctime, 0);
441 btrfs_set_stack_timespec_sec(&dst->mtime, src->st_mtime);
442 btrfs_set_stack_timespec_nsec(&dst->mtime, 0);
443 btrfs_set_stack_timespec_sec(&dst->otime, 0);
444 btrfs_set_stack_timespec_nsec(&dst->otime, 0);
446 if (S_ISDIR(src->st_mode)) {
447 btrfs_set_stack_inode_size(dst, 0);
448 btrfs_set_stack_inode_nlink(dst, 1);
450 if (S_ISREG(src->st_mode)) {
451 btrfs_set_stack_inode_size(dst, (u64)src->st_size);
452 if (src->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root))
453 btrfs_set_stack_inode_nbytes(dst, src->st_size);
455 blocks = src->st_size / sectorsize;
456 if (src->st_size % sectorsize)
458 blocks *= sectorsize;
459 btrfs_set_stack_inode_nbytes(dst, blocks);
462 if (S_ISLNK(src->st_mode))
463 btrfs_set_stack_inode_nbytes(dst, src->st_size + 1);
468 static int directory_select(const struct direct *entry)
470 if ((strncmp(entry->d_name, ".", entry->d_reclen) == 0) ||
471 (strncmp(entry->d_name, "..", entry->d_reclen) == 0))
477 static void free_namelist(struct direct **files, int count)
484 for (i = 0; i < count; ++i)
489 static u64 calculate_dir_inode_size(char *dirname)
492 struct direct **files, *cur_file;
493 u64 dir_inode_size = 0;
495 count = scandir(dirname, &files, directory_select, NULL);
497 for (i = 0; i < count; i++) {
499 dir_inode_size += strlen(cur_file->d_name);
502 free_namelist(files, count);
505 return dir_inode_size;
508 static int add_inode_items(struct btrfs_trans_handle *trans,
509 struct btrfs_root *root,
510 struct stat *st, char *name,
511 u64 self_objectid, ino_t parent_inum,
512 int dir_index_cnt, struct btrfs_inode_item *inode_ret)
515 struct btrfs_key inode_key;
516 struct btrfs_inode_item btrfs_inode;
520 fill_inode_item(trans, root, &btrfs_inode, st);
521 objectid = self_objectid;
523 if (S_ISDIR(st->st_mode)) {
524 inode_size = calculate_dir_inode_size(name);
525 btrfs_set_stack_inode_size(&btrfs_inode, inode_size);
528 inode_key.objectid = objectid;
529 inode_key.offset = 0;
530 btrfs_set_key_type(&inode_key, BTRFS_INODE_ITEM_KEY);
532 ret = btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
534 *inode_ret = btrfs_inode;
538 static int add_xattr_item(struct btrfs_trans_handle *trans,
539 struct btrfs_root *root, u64 objectid,
540 const char *file_name)
544 char xattr_list[XATTR_LIST_MAX];
546 char cur_value[XATTR_SIZE_MAX];
547 char delimiter = '\0';
548 char *next_location = xattr_list;
550 ret = llistxattr(file_name, xattr_list, XATTR_LIST_MAX);
554 fprintf(stderr, "get a list of xattr failed for %s\n",
561 cur_name = strtok(xattr_list, &delimiter);
562 while (cur_name != NULL) {
563 cur_name_len = strlen(cur_name);
564 next_location += cur_name_len + 1;
566 ret = getxattr(file_name, cur_name, cur_value, XATTR_SIZE_MAX);
570 fprintf(stderr, "get a xattr value failed for %s attr %s\n",
571 file_name, cur_name);
575 ret = btrfs_insert_xattr_item(trans, root, cur_name,
576 cur_name_len, cur_value,
579 fprintf(stderr, "insert a xattr item failed for %s\n",
583 cur_name = strtok(next_location, &delimiter);
589 static int add_symbolic_link(struct btrfs_trans_handle *trans,
590 struct btrfs_root *root,
591 u64 objectid, const char *path_name)
594 u64 sectorsize = root->sectorsize;
595 char *buf = malloc(sectorsize);
597 ret = readlink(path_name, buf, sectorsize);
599 fprintf(stderr, "readlink failed for %s\n", path_name);
602 if (ret >= sectorsize) {
603 fprintf(stderr, "symlink too long for %s", path_name);
608 buf[ret] = '\0'; /* readlink does not do it for us */
609 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
616 static int add_file_items(struct btrfs_trans_handle *trans,
617 struct btrfs_root *root,
618 struct btrfs_inode_item *btrfs_inode, u64 objectid,
619 ino_t parent_inum, struct stat *st,
620 const char *path_name, int out_fd)
625 struct btrfs_key key;
627 u32 sectorsize = root->sectorsize;
632 struct extent_buffer *eb = NULL;
635 if (st->st_size == 0)
638 fd = open(path_name, O_RDONLY);
640 fprintf(stderr, "%s open failed\n", path_name);
644 blocks = st->st_size / sectorsize;
645 if (st->st_size % sectorsize)
648 if (st->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root)) {
649 char *buffer = malloc(st->st_size);
650 ret_read = pread64(fd, buffer, st->st_size, bytes_read);
651 if (ret_read == -1) {
652 fprintf(stderr, "%s read failed\n", path_name);
657 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
658 buffer, st->st_size);
663 /* round up our st_size to the FS blocksize */
664 total_bytes = (u64)blocks * sectorsize;
667 * do our IO in extent buffers so it can work
668 * against any raid type
670 eb = malloc(sizeof(*eb) + sectorsize);
675 memset(eb, 0, sizeof(*eb) + sectorsize);
680 * keep our extent size at 1MB max, this makes it easier to work inside
681 * the tiny block groups created during mkfs
683 cur_bytes = min(total_bytes, 1024ULL * 1024);
684 ret = btrfs_reserve_extent(trans, root, cur_bytes, 0, 0, (u64)-1,
689 first_block = key.objectid;
692 while (bytes_read < cur_bytes) {
694 memset(eb->data, 0, sectorsize);
696 ret_read = pread64(fd, eb->data, sectorsize, file_pos + bytes_read);
697 if (ret_read == -1) {
698 fprintf(stderr, "%s read failed\n", path_name);
702 eb->start = first_block + bytes_read;
703 eb->len = sectorsize;
706 * we're doing the csum before we record the extent, but
709 ret = btrfs_csum_file_block(trans, root->fs_info->csum_root,
710 first_block + bytes_read + sectorsize,
711 first_block + bytes_read,
712 eb->data, sectorsize);
716 ret = write_and_map_eb(trans, root, eb);
718 fprintf(stderr, "output file write failed\n");
722 bytes_read += sectorsize;
726 ret = btrfs_record_file_extent(trans, root, objectid, btrfs_inode,
727 file_pos, first_block, cur_bytes);
733 file_pos += cur_bytes;
734 total_bytes -= cur_bytes;
745 static char *make_path(char *dir, char *name)
749 path = malloc(strlen(dir) + strlen(name) + 2);
753 if (dir[strlen(dir) - 1] != '/')
759 static int traverse_directory(struct btrfs_trans_handle *trans,
760 struct btrfs_root *root, char *dir_name,
761 struct directory_name_entry *dir_head, int out_fd)
765 struct btrfs_inode_item cur_inode;
766 struct btrfs_inode_item *inode_item;
767 int count, i, dir_index_cnt;
768 struct direct **files;
770 struct directory_name_entry *dir_entry, *parent_dir_entry;
771 struct direct *cur_file;
772 ino_t parent_inum, cur_inum;
773 ino_t highest_inum = 0;
774 char *parent_dir_name;
775 char real_path[PATH_MAX];
776 struct btrfs_path path;
777 struct extent_buffer *leaf;
778 struct btrfs_key root_dir_key;
779 u64 root_dir_inode_size = 0;
781 /* Add list for source directory */
782 dir_entry = malloc(sizeof(struct directory_name_entry));
783 dir_entry->dir_name = dir_name;
784 dir_entry->path = realpath(dir_name, real_path);
785 if (!dir_entry->path) {
786 fprintf(stderr, "get directory real path error\n");
791 parent_inum = highest_inum + BTRFS_FIRST_FREE_OBJECTID;
792 dir_entry->inum = parent_inum;
793 list_add_tail(&dir_entry->list, &dir_head->list);
795 btrfs_init_path(&path);
797 root_dir_key.objectid = btrfs_root_dirid(&root->root_item);
798 root_dir_key.offset = 0;
799 btrfs_set_key_type(&root_dir_key, BTRFS_INODE_ITEM_KEY);
800 ret = btrfs_lookup_inode(trans, root, &path, &root_dir_key, 1);
802 fprintf(stderr, "root dir lookup error\n");
806 leaf = path.nodes[0];
807 inode_item = btrfs_item_ptr(leaf, path.slots[0],
808 struct btrfs_inode_item);
810 root_dir_inode_size = calculate_dir_inode_size(dir_name);
811 btrfs_set_inode_size(leaf, inode_item, root_dir_inode_size);
812 btrfs_mark_buffer_dirty(leaf);
814 btrfs_release_path(&path);
817 parent_dir_entry = list_entry(dir_head->list.next,
818 struct directory_name_entry,
820 list_del(&parent_dir_entry->list);
822 parent_inum = parent_dir_entry->inum;
823 parent_dir_name = parent_dir_entry->dir_name;
824 if (chdir(parent_dir_entry->path)) {
825 fprintf(stderr, "chdir error for %s\n",
831 count = scandir(parent_dir_entry->path, &files,
832 directory_select, NULL);
835 fprintf(stderr, "scandir for %s failed: %s\n",
836 parent_dir_name, strerror (errno));
841 for (i = 0; i < count; i++) {
844 if (lstat(cur_file->d_name, &st) == -1) {
845 fprintf(stderr, "lstat failed for file %s\n",
851 cur_inum = st.st_ino;
852 ret = add_directory_items(trans, root,
853 cur_inum, parent_inum,
855 &st, &dir_index_cnt);
857 fprintf(stderr, "add_directory_items failed\n");
861 ret = add_inode_items(trans, root, &st,
862 cur_file->d_name, cur_inum,
863 parent_inum, dir_index_cnt,
865 if (ret == -EEXIST) {
866 BUG_ON(st.st_nlink <= 1);
870 fprintf(stderr, "add_inode_items failed\n");
874 ret = add_xattr_item(trans, root,
875 cur_inum, cur_file->d_name);
877 fprintf(stderr, "add_xattr_item failed\n");
882 if (S_ISDIR(st.st_mode)) {
883 dir_entry = malloc(sizeof(struct directory_name_entry));
884 dir_entry->dir_name = cur_file->d_name;
885 dir_entry->path = make_path(parent_dir_entry->path,
887 dir_entry->inum = cur_inum;
888 list_add_tail(&dir_entry->list, &dir_head->list);
889 } else if (S_ISREG(st.st_mode)) {
890 ret = add_file_items(trans, root, &cur_inode,
891 cur_inum, parent_inum, &st,
892 cur_file->d_name, out_fd);
894 fprintf(stderr, "add_file_items failed\n");
897 } else if (S_ISLNK(st.st_mode)) {
898 ret = add_symbolic_link(trans, root,
899 cur_inum, cur_file->d_name);
901 fprintf(stderr, "add_symbolic_link failed\n");
907 free_namelist(files, count);
908 free(parent_dir_entry);
912 } while (!list_empty(&dir_head->list));
917 free_namelist(files, count);
919 free(parent_dir_entry);
926 static int open_target(char *output_name)
929 output_fd = open(output_name, O_CREAT | O_RDWR | O_TRUNC,
930 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
935 static int create_chunks(struct btrfs_trans_handle *trans,
936 struct btrfs_root *root, u64 num_of_meta_chunks,
938 struct mkfs_allocation *allocation)
942 u64 meta_type = BTRFS_BLOCK_GROUP_METADATA;
943 u64 data_type = BTRFS_BLOCK_GROUP_DATA;
944 u64 minimum_data_chunk_size = 8 * 1024 * 1024;
948 for (i = 0; i < num_of_meta_chunks; i++) {
949 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
950 &chunk_start, &chunk_size, meta_type);
952 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
953 meta_type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
954 chunk_start, chunk_size);
955 allocation->metadata += chunk_size;
957 set_extent_dirty(&root->fs_info->free_space_cache,
958 chunk_start, chunk_start + chunk_size - 1, 0);
961 if (size_of_data < minimum_data_chunk_size)
962 size_of_data = minimum_data_chunk_size;
964 ret = btrfs_alloc_data_chunk(trans, root->fs_info->extent_root,
965 &chunk_start, size_of_data, data_type);
967 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
968 data_type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
969 chunk_start, size_of_data);
970 allocation->data += size_of_data;
972 set_extent_dirty(&root->fs_info->free_space_cache,
973 chunk_start, chunk_start + size_of_data - 1, 0);
977 static int make_image(char *source_dir, struct btrfs_root *root, int out_fd)
980 struct btrfs_trans_handle *trans;
984 struct directory_name_entry dir_head;
986 struct directory_name_entry *dir_entry = NULL;
988 ret = lstat(source_dir, &root_st);
990 fprintf(stderr, "unable to lstat the %s\n", source_dir);
994 INIT_LIST_HEAD(&dir_head.list);
996 trans = btrfs_start_transaction(root, 1);
997 ret = traverse_directory(trans, root, source_dir, &dir_head, out_fd);
999 fprintf(stderr, "unable to traverse_directory\n");
1002 btrfs_commit_transaction(trans, root);
1005 printf("Making image is completed.\n");
1008 while (!list_empty(&dir_head.list)) {
1009 dir_entry = list_entry(dir_head.list.next,
1010 struct directory_name_entry, list);
1011 list_del(&dir_entry->list);
1015 fprintf(stderr, "Making image is aborted.\n");
1020 * This ignores symlinks with unreadable targets and subdirs that can't
1021 * be read. It's a best-effort to give a rough estimate of the size of
1022 * a subdir. It doesn't guarantee that prepopulating btrfs from this
1023 * tree won't still run out of space.
1025 * The rounding up to 4096 is questionable. Previous code used du -B 4096.
1027 static u64 global_total_size;
1028 static int ftw_add_entry_size(const char *fpath, const struct stat *st,
1031 if (type == FTW_F || type == FTW_D)
1032 global_total_size += round_up(st->st_size, 4096);
1037 static u64 size_sourcedir(char *dir_name, u64 sectorsize,
1038 u64 *num_of_meta_chunks_ret, u64 *size_of_data_ret)
1043 u64 default_chunk_size = 8 * 1024 * 1024; /* 8MB */
1044 u64 allocated_meta_size = 8 * 1024 * 1024; /* 8MB */
1045 u64 allocated_total_size = 20 * 1024 * 1024; /* 20MB */
1046 u64 num_of_meta_chunks = 0;
1047 u64 num_of_data_chunks = 0;
1048 u64 num_of_allocated_meta_chunks =
1049 allocated_meta_size / default_chunk_size;
1051 global_total_size = 0;
1052 ret = ftw(dir_name, ftw_add_entry_size, 10);
1053 dir_size = global_total_size;
1055 fprintf(stderr, "ftw subdir walk of '%s' failed: %s\n",
1056 dir_name, strerror(errno));
1060 num_of_data_chunks = (dir_size + default_chunk_size - 1) /
1063 num_of_meta_chunks = (dir_size / 2) / default_chunk_size;
1064 if (((dir_size / 2) % default_chunk_size) != 0)
1065 num_of_meta_chunks++;
1066 if (num_of_meta_chunks <= num_of_allocated_meta_chunks)
1067 num_of_meta_chunks = 0;
1069 num_of_meta_chunks -= num_of_allocated_meta_chunks;
1071 total_size = allocated_total_size +
1072 (num_of_data_chunks * default_chunk_size) +
1073 (num_of_meta_chunks * default_chunk_size);
1075 *num_of_meta_chunks_ret = num_of_meta_chunks;
1076 *size_of_data_ret = num_of_data_chunks * default_chunk_size;
1080 static int zero_output_file(int out_fd, u64 size, u32 sectorsize)
1082 int len = sectorsize;
1083 int loop_num = size / sectorsize;
1085 char *buf = malloc(len);
1091 memset(buf, 0, len);
1092 for (i = 0; i < loop_num; i++) {
1093 written = pwrite64(out_fd, buf, len, location);
1096 location += sectorsize;
1102 static int is_ssd(const char *file)
1106 char sysfs_path[PATH_MAX];
1112 probe = blkid_new_probe_from_filename(file);
1116 /* Device number of this disk (possibly a partition) */
1117 devno = blkid_probe_get_devno(probe);
1119 blkid_free_probe(probe);
1123 /* Get whole disk name (not full path) for this devno */
1124 ret = blkid_devno_to_wholedisk(devno,
1125 wholedisk, sizeof(wholedisk), NULL);
1127 blkid_free_probe(probe);
1131 snprintf(sysfs_path, PATH_MAX, "/sys/block/%s/queue/rotational",
1134 blkid_free_probe(probe);
1136 fd = open(sysfs_path, O_RDONLY);
1141 if (read(fd, &rotational, sizeof(char)) < sizeof(char)) {
1147 return !atoi((const char *)&rotational);
1150 static void list_all_devices(struct btrfs_root *root)
1152 struct btrfs_fs_devices *fs_devices;
1153 struct btrfs_device *device;
1154 int number_of_devices = 0;
1155 u64 total_block_count = 0;
1157 fs_devices = root->fs_info->fs_devices;
1159 list_for_each_entry(device, &fs_devices->devices, dev_list)
1160 number_of_devices++;
1162 printf("Number of devices: %d\n", number_of_devices);
1163 /* printf("Total devices size: %10s\n", */
1164 /* pretty_size(total_block_count)); */
1165 printf("Devices:\n");
1166 printf(" ID SIZE PATH\n");
1167 list_for_each_entry_reverse(device, &fs_devices->devices, dev_list) {
1168 char dev_uuid[BTRFS_UUID_UNPARSED_SIZE];
1170 uuid_unparse(device->uuid, dev_uuid);
1171 printf(" %3llu %10s %s\n",
1173 pretty_size(device->total_bytes),
1175 total_block_count += device->total_bytes;
1181 static int is_temp_block_group(struct extent_buffer *node,
1182 struct btrfs_block_group_item *bgi,
1183 u64 data_profile, u64 meta_profile,
1186 u64 flag = btrfs_disk_block_group_flags(node, bgi);
1187 u64 flag_type = flag & BTRFS_BLOCK_GROUP_TYPE_MASK;
1188 u64 flag_profile = flag & BTRFS_BLOCK_GROUP_PROFILE_MASK;
1189 u64 used = btrfs_disk_block_group_used(node, bgi);
1192 * Chunks meets all the following conditions is a temp chunk
1194 * Temp chunk is always empty.
1196 * 2) profile dismatch with mkfs profile.
1197 * Temp chunk is always in SINGLE
1199 * 3) Size differs with mkfs_alloc
1200 * Special case for SINGLE/SINGLE btrfs.
1201 * In that case, temp data chunk and real data chunk are always empty.
1202 * So we need to use mkfs_alloc to be sure which chunk is the newly
1205 * Normally, new chunk size is equal to mkfs one (One chunk)
1206 * If it has multiple chunks, we just refuse to delete any one.
1207 * As they are all single, so no real problem will happen.
1208 * So only use condition 1) and 2) to judge them.
1212 switch (flag_type) {
1213 case BTRFS_BLOCK_GROUP_DATA:
1214 case BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA:
1215 data_profile &= BTRFS_BLOCK_GROUP_PROFILE_MASK;
1216 if (flag_profile != data_profile)
1219 case BTRFS_BLOCK_GROUP_METADATA:
1220 meta_profile &= BTRFS_BLOCK_GROUP_PROFILE_MASK;
1221 if (flag_profile != meta_profile)
1224 case BTRFS_BLOCK_GROUP_SYSTEM:
1225 sys_profile &= BTRFS_BLOCK_GROUP_PROFILE_MASK;
1226 if (flag_profile != sys_profile)
1233 /* Note: if current is a block group, it will skip it anyway */
1234 static int next_block_group(struct btrfs_root *root,
1235 struct btrfs_path *path)
1237 struct btrfs_key key;
1241 ret = btrfs_next_item(root, path);
1245 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1246 if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY)
1253 /* This function will cleanup */
1254 static int cleanup_temp_chunks(struct btrfs_fs_info *fs_info,
1255 struct mkfs_allocation *alloc,
1256 u64 data_profile, u64 meta_profile,
1259 struct btrfs_trans_handle *trans = NULL;
1260 struct btrfs_block_group_item *bgi;
1261 struct btrfs_root *root = fs_info->extent_root;
1262 struct btrfs_key key;
1263 struct btrfs_key found_key;
1264 struct btrfs_path *path;
1267 path = btrfs_alloc_path();
1273 trans = btrfs_start_transaction(root, 1);
1276 key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
1281 * as the rest of the loop may modify the tree, we need to
1282 * start a new search each time.
1284 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
1288 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1290 if (found_key.objectid < key.objectid)
1292 if (found_key.type != BTRFS_BLOCK_GROUP_ITEM_KEY) {
1293 ret = next_block_group(root, path);
1300 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1304 bgi = btrfs_item_ptr(path->nodes[0], path->slots[0],
1305 struct btrfs_block_group_item);
1306 if (is_temp_block_group(path->nodes[0], bgi,
1307 data_profile, meta_profile,
1309 ret = btrfs_free_block_group(trans, fs_info,
1310 found_key.objectid, found_key.offset);
1314 btrfs_release_path(path);
1315 key.objectid = found_key.objectid + found_key.offset;
1319 btrfs_commit_transaction(trans, root);
1320 btrfs_free_path(path);
1324 int main(int ac, char **av)
1327 struct btrfs_root *root;
1328 struct btrfs_trans_handle *trans;
1330 u64 block_count = 0;
1331 u64 dev_block_count = 0;
1333 u64 alloc_start = 0;
1334 u64 metadata_profile = 0;
1335 u64 data_profile = 0;
1336 u32 nodesize = max_t(u32, sysconf(_SC_PAGESIZE),
1337 BTRFS_MKFS_DEFAULT_NODE_SIZE);
1338 u32 sectorsize = 4096;
1339 u32 stripesize = 4096;
1345 int nodesize_forced = 0;
1346 int data_profile_opt = 0;
1347 int metadata_profile_opt = 0;
1350 int force_overwrite = 0;
1351 char *source_dir = NULL;
1352 int source_dir_set = 0;
1353 u64 num_of_meta_chunks = 0;
1354 u64 size_of_data = 0;
1355 u64 source_dir_size = 0;
1358 char fs_uuid[BTRFS_UUID_UNPARSED_SIZE] = { 0 };
1359 u64 features = BTRFS_MKFS_DEFAULT_FEATURES;
1360 struct mkfs_allocation allocation = { 0 };
1361 struct btrfs_mkfs_config mkfs_cfg;
1365 static const struct option long_options[] = {
1366 { "alloc-start", required_argument, NULL, 'A'},
1367 { "byte-count", required_argument, NULL, 'b' },
1368 { "force", no_argument, NULL, 'f' },
1369 { "leafsize", required_argument, NULL, 'l' },
1370 { "label", required_argument, NULL, 'L'},
1371 { "metadata", required_argument, NULL, 'm' },
1372 { "mixed", no_argument, NULL, 'M' },
1373 { "nodesize", required_argument, NULL, 'n' },
1374 { "sectorsize", required_argument, NULL, 's' },
1375 { "data", required_argument, NULL, 'd' },
1376 { "version", no_argument, NULL, 'V' },
1377 { "rootdir", required_argument, NULL, 'r' },
1378 { "nodiscard", no_argument, NULL, 'K' },
1379 { "features", required_argument, NULL, 'O' },
1380 { "uuid", required_argument, NULL, 'U' },
1381 { "quiet", 0, NULL, 'q' },
1382 { "help", no_argument, NULL, GETOPT_VAL_HELP },
1386 c = getopt_long(ac, av, "A:b:fl:n:s:m:d:L:O:r:U:VMKq",
1387 long_options, NULL);
1392 alloc_start = parse_size(optarg);
1395 force_overwrite = 1;
1398 data_profile = parse_profile(optarg);
1399 data_profile_opt = 1;
1403 "WARNING: --leafsize is deprecated, use --nodesize\n");
1405 nodesize = parse_size(optarg);
1406 nodesize_forced = 1;
1409 label = parse_label(optarg);
1412 metadata_profile = parse_profile(optarg);
1413 metadata_profile_opt = 1;
1419 char *orig = strdup(optarg);
1422 tmp = btrfs_parse_fs_features(tmp, &features);
1425 "Unrecognized filesystem feature '%s'\n",
1431 if (features & BTRFS_FEATURE_LIST_ALL) {
1432 btrfs_list_all_fs_features(0);
1438 sectorsize = parse_size(optarg);
1441 block_count = parse_size(optarg);
1442 if (block_count <= BTRFS_MKFS_SMALL_VOLUME_SIZE)
1450 source_dir = optarg;
1454 strncpy(fs_uuid, optarg,
1455 BTRFS_UUID_UNPARSED_SIZE - 1);
1463 case GETOPT_VAL_HELP:
1465 print_usage(c != GETOPT_VAL_HELP);
1468 sectorsize = max(sectorsize, (u32)sysconf(_SC_PAGESIZE));
1469 if (btrfs_check_nodesize(nodesize, sectorsize))
1471 saved_optind = optind;
1472 dev_cnt = ac - optind;
1476 if (source_dir_set && dev_cnt > 1) {
1478 "The -r option is limited to a single device\n");
1485 if (uuid_parse(fs_uuid, dummy_uuid) != 0) {
1486 fprintf(stderr, "could not parse UUID: %s\n", fs_uuid);
1489 if (!test_uuid_unique(fs_uuid)) {
1490 fprintf(stderr, "non-unique UUID: %s\n", fs_uuid);
1495 while (dev_cnt-- > 0) {
1496 file = av[optind++];
1497 if (is_block_device(file))
1498 if (test_dev_for_mkfs(file, force_overwrite))
1502 optind = saved_optind;
1503 dev_cnt = ac - optind;
1505 file = av[optind++];
1508 if (is_vol_small(file) || mixed) {
1510 printf("SMALL VOLUME: forcing mixed metadata/data groups\n");
1515 * Set default profiles according to number of added devices.
1516 * For mixed groups defaults are single/single.
1519 if (!metadata_profile_opt) {
1520 if (dev_cnt == 1 && ssd && verbose)
1521 printf("Detected a SSD, turning off metadata "
1522 "duplication. Mkfs with -m dup if you want to "
1523 "force metadata duplication.\n");
1525 metadata_profile = (dev_cnt > 1) ?
1526 BTRFS_BLOCK_GROUP_RAID1 : (ssd) ?
1527 0: BTRFS_BLOCK_GROUP_DUP;
1529 if (!data_profile_opt) {
1530 data_profile = (dev_cnt > 1) ?
1531 BTRFS_BLOCK_GROUP_RAID0 : 0; /* raid0 or single */
1534 u32 best_nodesize = max_t(u32, sysconf(_SC_PAGESIZE), sectorsize);
1536 if (metadata_profile_opt || data_profile_opt) {
1537 if (metadata_profile != data_profile) {
1539 "ERROR: With mixed block groups data and metadata profiles must be the same\n");
1544 if (!nodesize_forced) {
1545 nodesize = best_nodesize;
1546 if (btrfs_check_nodesize(nodesize, sectorsize))
1549 if (nodesize != sectorsize) {
1550 fprintf(stderr, "Error: mixed metadata/data block groups "
1551 "require metadata blocksizes equal to the sectorsize\n");
1556 /* Check device/block_count after the nodesize is determined */
1557 if (block_count && block_count < btrfs_min_dev_size(nodesize)) {
1559 "Size '%llu' is too small to make a usable filesystem\n",
1562 "Minimum size for btrfs filesystem is %llu\n",
1563 btrfs_min_dev_size(nodesize));
1566 for (i = saved_optind; i < saved_optind + dev_cnt; i++) {
1570 ret = test_minimum_size(path, nodesize);
1572 fprintf(stderr, "Failed to check size for '%s': %s\n",
1573 path, strerror(-ret));
1578 "'%s' is too small to make a usable filesystem\n",
1581 "Minimum size for each btrfs device is %llu.\n",
1582 btrfs_min_dev_size(nodesize));
1586 ret = test_num_disk_vs_raid(metadata_profile, data_profile,
1591 /* if we are here that means all devs are good to btrfsify */
1593 printf("%s\n", PACKAGE_STRING);
1594 printf("See %s for more information.\n\n", PACKAGE_URL);
1599 if (!source_dir_set) {
1601 * open without O_EXCL so that the problem should not
1602 * occur by the following processing.
1603 * (btrfs_register_one_device() fails if O_EXCL is on)
1605 fd = open(file, O_RDWR);
1607 fprintf(stderr, "unable to open %s: %s\n", file,
1611 ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count,
1612 block_count, &mixed, discard);
1617 if (block_count && block_count > dev_block_count) {
1618 fprintf(stderr, "%s is smaller than requested size\n", file);
1622 fd = open_target(file);
1624 fprintf(stderr, "unable to open the %s\n", file);
1628 source_dir_size = size_sourcedir(source_dir, sectorsize,
1629 &num_of_meta_chunks, &size_of_data);
1630 if(block_count < source_dir_size)
1631 block_count = source_dir_size;
1632 ret = zero_output_file(fd, block_count, sectorsize);
1634 fprintf(stderr, "unable to zero the output file\n");
1637 /* our "device" is the new image file */
1638 dev_block_count = block_count;
1641 /* To create the first block group and chunk 0 in make_btrfs */
1642 if (dev_block_count < BTRFS_MKFS_SYSTEM_GROUP_SIZE) {
1643 fprintf(stderr, "device is too small to make filesystem\n");
1647 blocks[0] = BTRFS_SUPER_INFO_OFFSET;
1648 for (i = 1; i < 7; i++) {
1649 blocks[i] = BTRFS_SUPER_INFO_OFFSET + 1024 * 1024 +
1653 if (group_profile_max_safe_loss(metadata_profile) <
1654 group_profile_max_safe_loss(data_profile)){
1656 "WARNING: metatdata has lower redundancy than data!\n\n");
1660 * FS features that can be set by other means than -O
1661 * just set the bit here
1664 features |= BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS;
1666 if ((data_profile | metadata_profile) &
1667 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
1668 features |= BTRFS_FEATURE_INCOMPAT_RAID56;
1671 mkfs_cfg.label = label;
1672 mkfs_cfg.fs_uuid = fs_uuid;
1673 memcpy(mkfs_cfg.blocks, blocks, sizeof(blocks));
1674 mkfs_cfg.num_bytes = dev_block_count;
1675 mkfs_cfg.nodesize = nodesize;
1676 mkfs_cfg.sectorsize = sectorsize;
1677 mkfs_cfg.stripesize = stripesize;
1678 mkfs_cfg.features = features;
1680 ret = make_btrfs(fd, &mkfs_cfg);
1682 fprintf(stderr, "error during mkfs: %s\n", strerror(-ret));
1686 root = open_ctree(file, 0, OPEN_CTREE_WRITES);
1688 fprintf(stderr, "Open ctree failed\n");
1692 root->fs_info->alloc_start = alloc_start;
1694 ret = create_metadata_block_groups(root, mixed, &allocation);
1696 fprintf(stderr, "failed to create default block groups\n");
1700 trans = btrfs_start_transaction(root, 1);
1702 fprintf(stderr, "failed to start transaction\n");
1706 ret = create_data_block_groups(trans, root, mixed, &allocation);
1708 fprintf(stderr, "failed to create default data block groups\n");
1712 ret = make_root_dir(trans, root, mixed, &allocation);
1714 fprintf(stderr, "failed to setup the root directory\n");
1718 btrfs_commit_transaction(trans, root);
1720 trans = btrfs_start_transaction(root, 1);
1722 fprintf(stderr, "failed to start transaction\n");
1726 if (is_block_device(file))
1727 btrfs_register_one_device(file);
1732 while (dev_cnt-- > 0) {
1733 int old_mixed = mixed;
1735 file = av[optind++];
1738 * open without O_EXCL so that the problem should not
1739 * occur by the following processing.
1740 * (btrfs_register_one_device() fails if O_EXCL is on)
1742 fd = open(file, O_RDWR);
1744 fprintf(stderr, "unable to open %s: %s\n", file,
1748 ret = btrfs_device_already_in_root(root, fd,
1749 BTRFS_SUPER_INFO_OFFSET);
1751 fprintf(stderr, "skipping duplicate device %s in FS\n",
1756 ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count,
1757 block_count, &mixed, discard);
1764 ret = btrfs_add_to_fsid(trans, root, fd, file, dev_block_count,
1765 sectorsize, sectorsize, sectorsize);
1768 struct btrfs_device *device;
1770 device = container_of(root->fs_info->fs_devices->devices.next,
1771 struct btrfs_device, dev_list);
1772 printf("adding device %s id %llu\n", file,
1773 (unsigned long long)device->devid);
1776 if (is_block_device(file))
1777 btrfs_register_one_device(file);
1781 if (!source_dir_set) {
1782 ret = create_raid_groups(trans, root, data_profile,
1783 metadata_profile, mixed, &allocation);
1787 ret = create_data_reloc_tree(trans, root);
1790 btrfs_commit_transaction(trans, root);
1792 if (source_dir_set) {
1793 trans = btrfs_start_transaction(root, 1);
1794 ret = create_chunks(trans, root,
1795 num_of_meta_chunks, size_of_data,
1798 btrfs_commit_transaction(trans, root);
1800 ret = make_image(source_dir, root, fd);
1803 ret = cleanup_temp_chunks(root->fs_info, &allocation, data_profile,
1804 metadata_profile, metadata_profile);
1806 fprintf(stderr, "Failed to cleanup temporary chunks\n");
1811 char features_buf[64];
1813 printf("Label: %s\n", label);
1814 printf("UUID: %s\n", fs_uuid);
1815 printf("Node size: %u\n", nodesize);
1816 printf("Sector size: %u\n", sectorsize);
1817 printf("Filesystem size: %s\n",
1818 pretty_size(btrfs_super_total_bytes(root->fs_info->super_copy)));
1819 printf("Block group profiles:\n");
1820 if (allocation.data)
1821 printf(" Data: %-8s %16s\n",
1822 btrfs_group_profile_str(data_profile),
1823 pretty_size(allocation.data));
1824 if (allocation.metadata)
1825 printf(" Metadata: %-8s %16s\n",
1826 btrfs_group_profile_str(metadata_profile),
1827 pretty_size(allocation.metadata));
1828 if (allocation.mixed)
1829 printf(" Data+Metadata: %-8s %16s\n",
1830 btrfs_group_profile_str(data_profile),
1831 pretty_size(allocation.mixed));
1832 printf(" System: %-8s %16s\n",
1833 btrfs_group_profile_str(metadata_profile),
1834 pretty_size(allocation.system));
1835 printf("SSD detected: %s\n", ssd ? "yes" : "no");
1836 btrfs_parse_features_to_string(features_buf, features);
1837 printf("Incompat features: %s", features_buf);
1840 list_all_devices(root);
1844 ret = close_ctree(root);
1846 btrfs_close_all_devices();