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"
20 #include "androidcompat.h"
22 #include <sys/ioctl.h>
23 #include <sys/mount.h>
27 #include <sys/types.h>
29 /* #include <sys/dir.h> included via androidcompat.h */
33 #include <uuid/uuid.h>
35 #include <sys/xattr.h>
37 #include <linux/limits.h>
38 #include <blkid/blkid.h>
43 #include "transaction.h"
45 #include "list_sort.h"
47 static u64 index_cnt = 2;
48 static int verbose = 1;
50 struct directory_name_entry {
54 struct list_head list;
57 struct mkfs_allocation {
64 static int create_metadata_block_groups(struct btrfs_root *root, int mixed,
65 struct mkfs_allocation *allocation)
67 struct btrfs_trans_handle *trans;
73 trans = btrfs_start_transaction(root, 1);
74 bytes_used = btrfs_super_bytes_used(root->fs_info->super_copy);
76 root->fs_info->system_allocs = 1;
77 ret = btrfs_make_block_group(trans, root, bytes_used,
78 BTRFS_BLOCK_GROUP_SYSTEM,
79 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
80 0, BTRFS_MKFS_SYSTEM_GROUP_SIZE);
81 allocation->system += BTRFS_MKFS_SYSTEM_GROUP_SIZE;
86 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
87 &chunk_start, &chunk_size,
88 BTRFS_BLOCK_GROUP_METADATA |
89 BTRFS_BLOCK_GROUP_DATA);
91 error("no space to allocate data/metadata chunk");
96 ret = btrfs_make_block_group(trans, root, 0,
97 BTRFS_BLOCK_GROUP_METADATA |
98 BTRFS_BLOCK_GROUP_DATA,
99 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
100 chunk_start, chunk_size);
103 allocation->mixed += chunk_size;
105 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
106 &chunk_start, &chunk_size,
107 BTRFS_BLOCK_GROUP_METADATA);
108 if (ret == -ENOSPC) {
109 error("no space to allocate metadata chunk");
114 ret = btrfs_make_block_group(trans, root, 0,
115 BTRFS_BLOCK_GROUP_METADATA,
116 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
117 chunk_start, chunk_size);
118 allocation->metadata += chunk_size;
123 root->fs_info->system_allocs = 0;
124 ret = btrfs_commit_transaction(trans, root);
130 static int create_data_block_groups(struct btrfs_trans_handle *trans,
131 struct btrfs_root *root, int mixed,
132 struct mkfs_allocation *allocation)
139 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
140 &chunk_start, &chunk_size,
141 BTRFS_BLOCK_GROUP_DATA);
142 if (ret == -ENOSPC) {
143 error("no space to allocate data chunk");
148 ret = btrfs_make_block_group(trans, root, 0,
149 BTRFS_BLOCK_GROUP_DATA,
150 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
151 chunk_start, chunk_size);
152 allocation->data += chunk_size;
161 static int make_root_dir(struct btrfs_trans_handle *trans, struct btrfs_root *root,
162 struct mkfs_allocation *allocation)
164 struct btrfs_key location;
167 ret = btrfs_make_root_dir(trans, root->fs_info->tree_root,
168 BTRFS_ROOT_TREE_DIR_OBJECTID);
171 ret = btrfs_make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID);
174 memcpy(&location, &root->fs_info->fs_root->root_key, sizeof(location));
175 location.offset = (u64)-1;
176 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
178 btrfs_super_root_dir(root->fs_info->super_copy),
179 &location, BTRFS_FT_DIR, 0);
183 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
184 "default", 7, location.objectid,
185 BTRFS_ROOT_TREE_DIR_OBJECTID, 0);
193 static void __recow_root(struct btrfs_trans_handle *trans,
194 struct btrfs_root *root)
197 struct extent_buffer *tmp;
199 if (trans->transid != btrfs_root_generation(&root->root_item)) {
200 extent_buffer_get(root->node);
201 ret = __btrfs_cow_block(trans, root, root->node,
202 NULL, 0, &tmp, 0, 0);
204 free_extent_buffer(tmp);
208 static void recow_roots(struct btrfs_trans_handle *trans,
209 struct btrfs_root *root)
211 struct btrfs_fs_info *info = root->fs_info;
213 __recow_root(trans, info->fs_root);
214 __recow_root(trans, info->tree_root);
215 __recow_root(trans, info->extent_root);
216 __recow_root(trans, info->chunk_root);
217 __recow_root(trans, info->dev_root);
218 __recow_root(trans, info->csum_root);
221 static int create_one_raid_group(struct btrfs_trans_handle *trans,
222 struct btrfs_root *root, u64 type,
223 struct mkfs_allocation *allocation)
230 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
231 &chunk_start, &chunk_size, type);
232 if (ret == -ENOSPC) {
233 error("not enough free space to allocate chunk");
239 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
240 type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
241 chunk_start, chunk_size);
243 type &= BTRFS_BLOCK_GROUP_TYPE_MASK;
244 if (type == BTRFS_BLOCK_GROUP_DATA) {
245 allocation->data += chunk_size;
246 } else if (type == BTRFS_BLOCK_GROUP_METADATA) {
247 allocation->metadata += chunk_size;
248 } else if (type == BTRFS_BLOCK_GROUP_SYSTEM) {
249 allocation->system += chunk_size;
251 (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA)) {
252 allocation->mixed += chunk_size;
254 error("unrecognized profile type: 0x%llx",
255 (unsigned long long)type);
262 static int create_raid_groups(struct btrfs_trans_handle *trans,
263 struct btrfs_root *root, u64 data_profile,
264 u64 metadata_profile, int mixed,
265 struct mkfs_allocation *allocation)
269 if (metadata_profile) {
270 u64 meta_flags = BTRFS_BLOCK_GROUP_METADATA;
272 ret = create_one_raid_group(trans, root,
273 BTRFS_BLOCK_GROUP_SYSTEM |
274 metadata_profile, allocation);
279 meta_flags |= BTRFS_BLOCK_GROUP_DATA;
281 ret = create_one_raid_group(trans, root, meta_flags |
282 metadata_profile, allocation);
287 if (!mixed && data_profile) {
288 ret = create_one_raid_group(trans, root,
289 BTRFS_BLOCK_GROUP_DATA |
290 data_profile, allocation);
294 recow_roots(trans, root);
299 static int create_data_reloc_tree(struct btrfs_trans_handle *trans,
300 struct btrfs_root *root)
302 struct btrfs_key location;
303 struct btrfs_root_item root_item;
304 struct extent_buffer *tmp;
305 u64 objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
308 ret = btrfs_copy_root(trans, root, root->node, &tmp, objectid);
312 memcpy(&root_item, &root->root_item, sizeof(root_item));
313 btrfs_set_root_bytenr(&root_item, tmp->start);
314 btrfs_set_root_level(&root_item, btrfs_header_level(tmp));
315 btrfs_set_root_generation(&root_item, trans->transid);
316 free_extent_buffer(tmp);
318 location.objectid = objectid;
319 location.type = BTRFS_ROOT_ITEM_KEY;
321 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
322 &location, &root_item);
327 static void print_usage(int ret)
329 fprintf(stderr, "usage: mkfs.btrfs [options] dev [ dev ... ]\n");
330 fprintf(stderr, "options:\n");
331 fprintf(stderr, "\t-A|--alloc-start START the offset to start the FS\n");
332 fprintf(stderr, "\t-b|--byte-count SIZE total number of bytes in the FS\n");
333 fprintf(stderr, "\t-d|--data PROFILE data profile, raid0, raid1, raid5, raid6, raid10, dup or single\n");
334 fprintf(stderr, "\t-f|--force force overwrite of existing filesystem\n");
335 fprintf(stderr, "\t-l|--leafsize SIZE deprecated, alias for nodesize\n");
336 fprintf(stderr, "\t-L|--label LABEL set a label\n");
337 fprintf(stderr, "\t-m|--metadata PROFILE metadata profile, values like data profile\n");
338 fprintf(stderr, "\t-M|--mixed mix metadata and data together\n");
339 fprintf(stderr, "\t-n|--nodesize SIZE size of btree nodes\n");
340 fprintf(stderr, "\t-s|--sectorsize SIZE min block allocation (may not mountable by current kernel)\n");
341 fprintf(stderr, "\t-r|--rootdir DIR the source directory\n");
342 fprintf(stderr, "\t-K|--nodiscard do not perform whole device TRIM\n");
343 fprintf(stderr, "\t-O|--features LIST comma separated list of filesystem features, use '-O list-all' to list features\n");
344 fprintf(stderr, "\t-U|--uuid UUID specify the filesystem UUID\n");
345 fprintf(stderr, "\t-q|--quiet no messages except errors\n");
346 fprintf(stderr, "\t-V|--version print the mkfs.btrfs version and exit\n");
350 static void print_version(void) __attribute__((noreturn));
351 static void print_version(void)
353 fprintf(stderr, "mkfs.btrfs, part of %s\n", PACKAGE_STRING);
357 static u64 parse_profile(char *s)
359 if (strcasecmp(s, "raid0") == 0) {
360 return BTRFS_BLOCK_GROUP_RAID0;
361 } else if (strcasecmp(s, "raid1") == 0) {
362 return BTRFS_BLOCK_GROUP_RAID1;
363 } else if (strcasecmp(s, "raid5") == 0) {
364 return BTRFS_BLOCK_GROUP_RAID5;
365 } else if (strcasecmp(s, "raid6") == 0) {
366 return BTRFS_BLOCK_GROUP_RAID6;
367 } else if (strcasecmp(s, "raid10") == 0) {
368 return BTRFS_BLOCK_GROUP_RAID10;
369 } else if (strcasecmp(s, "dup") == 0) {
370 return BTRFS_BLOCK_GROUP_DUP;
371 } else if (strcasecmp(s, "single") == 0) {
374 error("unknown profile %s", s);
381 static char *parse_label(char *input)
383 int len = strlen(input);
385 if (len >= BTRFS_LABEL_SIZE) {
386 error("label %s is too long (max %d)", input,
387 BTRFS_LABEL_SIZE - 1);
390 return strdup(input);
393 static int add_directory_items(struct btrfs_trans_handle *trans,
394 struct btrfs_root *root, u64 objectid,
395 ino_t parent_inum, const char *name,
396 struct stat *st, int *dir_index_cnt)
400 struct btrfs_key location;
403 name_len = strlen(name);
405 location.objectid = objectid;
407 btrfs_set_key_type(&location, BTRFS_INODE_ITEM_KEY);
409 if (S_ISDIR(st->st_mode))
410 filetype = BTRFS_FT_DIR;
411 if (S_ISREG(st->st_mode))
412 filetype = BTRFS_FT_REG_FILE;
413 if (S_ISLNK(st->st_mode))
414 filetype = BTRFS_FT_SYMLINK;
416 ret = btrfs_insert_dir_item(trans, root, name, name_len,
417 parent_inum, &location,
418 filetype, index_cnt);
421 ret = btrfs_insert_inode_ref(trans, root, name, name_len,
422 objectid, parent_inum, index_cnt);
423 *dir_index_cnt = index_cnt;
429 static int fill_inode_item(struct btrfs_trans_handle *trans,
430 struct btrfs_root *root,
431 struct btrfs_inode_item *dst, struct stat *src)
434 u64 sectorsize = root->sectorsize;
437 * btrfs_inode_item has some reserved fields
438 * and represents on-disk inode entry, so
439 * zero everything to prevent information leak
441 memset(dst, 0, sizeof (*dst));
443 btrfs_set_stack_inode_generation(dst, trans->transid);
444 btrfs_set_stack_inode_size(dst, src->st_size);
445 btrfs_set_stack_inode_nbytes(dst, 0);
446 btrfs_set_stack_inode_block_group(dst, 0);
447 btrfs_set_stack_inode_nlink(dst, src->st_nlink);
448 btrfs_set_stack_inode_uid(dst, src->st_uid);
449 btrfs_set_stack_inode_gid(dst, src->st_gid);
450 btrfs_set_stack_inode_mode(dst, src->st_mode);
451 btrfs_set_stack_inode_rdev(dst, 0);
452 btrfs_set_stack_inode_flags(dst, 0);
453 btrfs_set_stack_timespec_sec(&dst->atime, src->st_atime);
454 btrfs_set_stack_timespec_nsec(&dst->atime, 0);
455 btrfs_set_stack_timespec_sec(&dst->ctime, src->st_ctime);
456 btrfs_set_stack_timespec_nsec(&dst->ctime, 0);
457 btrfs_set_stack_timespec_sec(&dst->mtime, src->st_mtime);
458 btrfs_set_stack_timespec_nsec(&dst->mtime, 0);
459 btrfs_set_stack_timespec_sec(&dst->otime, 0);
460 btrfs_set_stack_timespec_nsec(&dst->otime, 0);
462 if (S_ISDIR(src->st_mode)) {
463 btrfs_set_stack_inode_size(dst, 0);
464 btrfs_set_stack_inode_nlink(dst, 1);
466 if (S_ISREG(src->st_mode)) {
467 btrfs_set_stack_inode_size(dst, (u64)src->st_size);
468 if (src->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root))
469 btrfs_set_stack_inode_nbytes(dst, src->st_size);
471 blocks = src->st_size / sectorsize;
472 if (src->st_size % sectorsize)
474 blocks *= sectorsize;
475 btrfs_set_stack_inode_nbytes(dst, blocks);
478 if (S_ISLNK(src->st_mode))
479 btrfs_set_stack_inode_nbytes(dst, src->st_size + 1);
484 static int directory_select(const struct direct *entry)
486 if ((strncmp(entry->d_name, ".", entry->d_reclen) == 0) ||
487 (strncmp(entry->d_name, "..", entry->d_reclen) == 0))
493 static void free_namelist(struct direct **files, int count)
500 for (i = 0; i < count; ++i)
505 static u64 calculate_dir_inode_size(char *dirname)
508 struct direct **files, *cur_file;
509 u64 dir_inode_size = 0;
511 count = scandir(dirname, &files, directory_select, NULL);
513 for (i = 0; i < count; i++) {
515 dir_inode_size += strlen(cur_file->d_name);
518 free_namelist(files, count);
521 return dir_inode_size;
524 static int add_inode_items(struct btrfs_trans_handle *trans,
525 struct btrfs_root *root,
526 struct stat *st, char *name,
527 u64 self_objectid, ino_t parent_inum,
528 int dir_index_cnt, struct btrfs_inode_item *inode_ret)
531 struct btrfs_key inode_key;
532 struct btrfs_inode_item btrfs_inode;
536 fill_inode_item(trans, root, &btrfs_inode, st);
537 objectid = self_objectid;
539 if (S_ISDIR(st->st_mode)) {
540 inode_size = calculate_dir_inode_size(name);
541 btrfs_set_stack_inode_size(&btrfs_inode, inode_size);
544 inode_key.objectid = objectid;
545 inode_key.offset = 0;
546 btrfs_set_key_type(&inode_key, BTRFS_INODE_ITEM_KEY);
548 ret = btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
550 *inode_ret = btrfs_inode;
554 static int add_xattr_item(struct btrfs_trans_handle *trans,
555 struct btrfs_root *root, u64 objectid,
556 const char *file_name)
560 char xattr_list[XATTR_LIST_MAX];
562 char cur_value[XATTR_SIZE_MAX];
563 char delimiter = '\0';
564 char *next_location = xattr_list;
566 ret = llistxattr(file_name, xattr_list, XATTR_LIST_MAX);
570 error("getting a list of xattr failed for %s: %s", file_name,
577 cur_name = strtok(xattr_list, &delimiter);
578 while (cur_name != NULL) {
579 cur_name_len = strlen(cur_name);
580 next_location += cur_name_len + 1;
582 ret = getxattr(file_name, cur_name, cur_value, XATTR_SIZE_MAX);
586 error("gettig a xattr value failed for %s attr %s: %s",
587 file_name, cur_name, strerror(errno));
591 ret = btrfs_insert_xattr_item(trans, root, cur_name,
592 cur_name_len, cur_value,
595 error("inserting a xattr item failed for %s: %s",
596 file_name, strerror(-ret));
599 cur_name = strtok(next_location, &delimiter);
605 static int add_symbolic_link(struct btrfs_trans_handle *trans,
606 struct btrfs_root *root,
607 u64 objectid, const char *path_name)
612 ret = readlink(path_name, buf, sizeof(buf));
614 error("readlink failed for %s: %s", path_name, strerror(errno));
617 if (ret >= sizeof(buf)) {
618 error("symlink too long for %s", path_name);
623 buf[ret] = '\0'; /* readlink does not do it for us */
624 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
630 static int add_file_items(struct btrfs_trans_handle *trans,
631 struct btrfs_root *root,
632 struct btrfs_inode_item *btrfs_inode, u64 objectid,
633 ino_t parent_inum, struct stat *st,
634 const char *path_name, int out_fd)
639 struct btrfs_key key;
641 u32 sectorsize = root->sectorsize;
646 struct extent_buffer *eb = NULL;
649 if (st->st_size == 0)
652 fd = open(path_name, O_RDONLY);
654 error("cannot open %s: %s", path_name, strerror(errno));
658 blocks = st->st_size / sectorsize;
659 if (st->st_size % sectorsize)
662 if (st->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root)) {
663 char *buffer = malloc(st->st_size);
670 ret_read = pread64(fd, buffer, st->st_size, bytes_read);
671 if (ret_read == -1) {
672 error("cannot read %s at offset %llu length %llu: %s",
673 path_name, (unsigned long long)bytes_read,
674 (unsigned long long)st->st_size,
680 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
681 buffer, st->st_size);
686 /* round up our st_size to the FS blocksize */
687 total_bytes = (u64)blocks * sectorsize;
690 * do our IO in extent buffers so it can work
691 * against any raid type
693 eb = calloc(1, sizeof(*eb) + sectorsize);
702 * keep our extent size at 1MB max, this makes it easier to work inside
703 * the tiny block groups created during mkfs
705 cur_bytes = min(total_bytes, 1024ULL * 1024);
706 ret = btrfs_reserve_extent(trans, root, cur_bytes, 0, 0, (u64)-1,
711 first_block = key.objectid;
714 while (bytes_read < cur_bytes) {
716 memset(eb->data, 0, sectorsize);
718 ret_read = pread64(fd, eb->data, sectorsize, file_pos + bytes_read);
719 if (ret_read == -1) {
720 error("cannot read %s at offset %llu length %llu: %s",
722 (unsigned long long)file_pos + bytes_read,
723 (unsigned long long)sectorsize,
728 eb->start = first_block + bytes_read;
729 eb->len = sectorsize;
732 * we're doing the csum before we record the extent, but
735 ret = btrfs_csum_file_block(trans, root->fs_info->csum_root,
736 first_block + bytes_read + sectorsize,
737 first_block + bytes_read,
738 eb->data, sectorsize);
742 ret = write_and_map_eb(trans, root, eb);
744 error("failed to write %s", path_name);
748 bytes_read += sectorsize;
752 ret = btrfs_record_file_extent(trans, root, objectid, btrfs_inode,
753 file_pos, first_block, cur_bytes);
759 file_pos += cur_bytes;
760 total_bytes -= cur_bytes;
771 static char *make_path(char *dir, char *name)
775 path = malloc(strlen(dir) + strlen(name) + 2);
779 if (dir[strlen(dir) - 1] != '/')
785 static int traverse_directory(struct btrfs_trans_handle *trans,
786 struct btrfs_root *root, char *dir_name,
787 struct directory_name_entry *dir_head, int out_fd)
791 struct btrfs_inode_item cur_inode;
792 struct btrfs_inode_item *inode_item;
793 int count, i, dir_index_cnt;
794 struct direct **files;
796 struct directory_name_entry *dir_entry, *parent_dir_entry;
797 struct direct *cur_file;
798 ino_t parent_inum, cur_inum;
799 ino_t highest_inum = 0;
800 char *parent_dir_name;
801 char real_path[PATH_MAX];
802 struct btrfs_path path;
803 struct extent_buffer *leaf;
804 struct btrfs_key root_dir_key;
805 u64 root_dir_inode_size = 0;
807 /* Add list for source directory */
808 dir_entry = malloc(sizeof(struct directory_name_entry));
811 dir_entry->dir_name = dir_name;
812 dir_entry->path = realpath(dir_name, real_path);
813 if (!dir_entry->path) {
814 error("realpath failed for %s: %s", dir_name, strerror(errno));
819 parent_inum = highest_inum + BTRFS_FIRST_FREE_OBJECTID;
820 dir_entry->inum = parent_inum;
821 list_add_tail(&dir_entry->list, &dir_head->list);
823 btrfs_init_path(&path);
825 root_dir_key.objectid = btrfs_root_dirid(&root->root_item);
826 root_dir_key.offset = 0;
827 btrfs_set_key_type(&root_dir_key, BTRFS_INODE_ITEM_KEY);
828 ret = btrfs_lookup_inode(trans, root, &path, &root_dir_key, 1);
830 error("failed to lookup root dir: %d", ret);
834 leaf = path.nodes[0];
835 inode_item = btrfs_item_ptr(leaf, path.slots[0],
836 struct btrfs_inode_item);
838 root_dir_inode_size = calculate_dir_inode_size(dir_name);
839 btrfs_set_inode_size(leaf, inode_item, root_dir_inode_size);
840 btrfs_mark_buffer_dirty(leaf);
842 btrfs_release_path(&path);
845 parent_dir_entry = list_entry(dir_head->list.next,
846 struct directory_name_entry,
848 list_del(&parent_dir_entry->list);
850 parent_inum = parent_dir_entry->inum;
851 parent_dir_name = parent_dir_entry->dir_name;
852 if (chdir(parent_dir_entry->path)) {
853 error("chdir failed for %s: %s",
854 parent_dir_name, strerror(errno));
859 count = scandir(parent_dir_entry->path, &files,
860 directory_select, NULL);
863 error("scandir failed for %s: %s",
864 parent_dir_name, strerror (errno));
869 for (i = 0; i < count; i++) {
872 if (lstat(cur_file->d_name, &st) == -1) {
873 error("lstat failed for %s: %s",
874 cur_file->d_name, strerror(errno));
879 cur_inum = st.st_ino;
880 ret = add_directory_items(trans, root,
881 cur_inum, parent_inum,
883 &st, &dir_index_cnt);
885 error("unable to add directory items for %s: %d",
886 cur_file->d_name, ret);
890 ret = add_inode_items(trans, root, &st,
891 cur_file->d_name, cur_inum,
892 parent_inum, dir_index_cnt,
894 if (ret == -EEXIST) {
895 BUG_ON(st.st_nlink <= 1);
899 error("unable to add inode items for %s: %d",
900 cur_file->d_name, ret);
904 ret = add_xattr_item(trans, root,
905 cur_inum, cur_file->d_name);
907 error("unable to add xattr items for %s: %d",
908 cur_file->d_name, ret);
913 if (S_ISDIR(st.st_mode)) {
914 dir_entry = malloc(sizeof(struct directory_name_entry));
919 dir_entry->dir_name = cur_file->d_name;
920 dir_entry->path = make_path(parent_dir_entry->path,
922 dir_entry->inum = cur_inum;
923 list_add_tail(&dir_entry->list, &dir_head->list);
924 } else if (S_ISREG(st.st_mode)) {
925 ret = add_file_items(trans, root, &cur_inode,
926 cur_inum, parent_inum, &st,
927 cur_file->d_name, out_fd);
929 error("unable to add file items for %s: %d",
930 cur_file->d_name, ret);
933 } else if (S_ISLNK(st.st_mode)) {
934 ret = add_symbolic_link(trans, root,
935 cur_inum, cur_file->d_name);
937 error("unable to add symlink for %s: %d",
938 cur_file->d_name, ret);
944 free_namelist(files, count);
945 free(parent_dir_entry);
949 } while (!list_empty(&dir_head->list));
954 free_namelist(files, count);
956 free(parent_dir_entry);
963 static int open_target(char *output_name)
966 output_fd = open(output_name, O_CREAT | O_RDWR,
967 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
972 static int create_chunks(struct btrfs_trans_handle *trans,
973 struct btrfs_root *root, u64 num_of_meta_chunks,
975 struct mkfs_allocation *allocation)
979 u64 meta_type = BTRFS_BLOCK_GROUP_METADATA;
980 u64 data_type = BTRFS_BLOCK_GROUP_DATA;
981 u64 minimum_data_chunk_size = 8 * 1024 * 1024;
985 for (i = 0; i < num_of_meta_chunks; i++) {
986 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
987 &chunk_start, &chunk_size, meta_type);
990 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
991 meta_type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
992 chunk_start, chunk_size);
993 allocation->metadata += chunk_size;
996 set_extent_dirty(&root->fs_info->free_space_cache,
997 chunk_start, chunk_start + chunk_size - 1, 0);
1000 if (size_of_data < minimum_data_chunk_size)
1001 size_of_data = minimum_data_chunk_size;
1003 ret = btrfs_alloc_data_chunk(trans, root->fs_info->extent_root,
1004 &chunk_start, size_of_data, data_type, 0);
1007 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
1008 data_type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
1009 chunk_start, size_of_data);
1010 allocation->data += size_of_data;
1013 set_extent_dirty(&root->fs_info->free_space_cache,
1014 chunk_start, chunk_start + size_of_data - 1, 0);
1018 static int make_image(char *source_dir, struct btrfs_root *root, int out_fd)
1021 struct btrfs_trans_handle *trans;
1023 struct stat root_st;
1025 struct directory_name_entry dir_head;
1027 struct directory_name_entry *dir_entry = NULL;
1029 ret = lstat(source_dir, &root_st);
1031 error("unable to lstat %s: %s", source_dir, strerror(errno));
1035 INIT_LIST_HEAD(&dir_head.list);
1037 trans = btrfs_start_transaction(root, 1);
1038 ret = traverse_directory(trans, root, source_dir, &dir_head, out_fd);
1040 error("unable to traverse directory %s: %d", source_dir, ret);
1043 btrfs_commit_transaction(trans, root);
1046 printf("Making image is completed.\n");
1049 while (!list_empty(&dir_head.list)) {
1050 dir_entry = list_entry(dir_head.list.next,
1051 struct directory_name_entry, list);
1052 list_del(&dir_entry->list);
1056 fprintf(stderr, "Making image is aborted.\n");
1061 * This ignores symlinks with unreadable targets and subdirs that can't
1062 * be read. It's a best-effort to give a rough estimate of the size of
1063 * a subdir. It doesn't guarantee that prepopulating btrfs from this
1064 * tree won't still run out of space.
1066 static u64 global_total_size;
1067 static u64 fs_block_size;
1068 static int ftw_add_entry_size(const char *fpath, const struct stat *st,
1071 if (type == FTW_F || type == FTW_D)
1072 global_total_size += round_up(st->st_size, fs_block_size);
1077 static u64 size_sourcedir(char *dir_name, u64 sectorsize,
1078 u64 *num_of_meta_chunks_ret, u64 *size_of_data_ret)
1083 u64 default_chunk_size = 8 * 1024 * 1024; /* 8MB */
1084 u64 allocated_meta_size = 8 * 1024 * 1024; /* 8MB */
1085 u64 allocated_total_size = 20 * 1024 * 1024; /* 20MB */
1086 u64 num_of_meta_chunks = 0;
1087 u64 num_of_data_chunks = 0;
1088 u64 num_of_allocated_meta_chunks =
1089 allocated_meta_size / default_chunk_size;
1091 global_total_size = 0;
1092 fs_block_size = sectorsize;
1093 ret = ftw(dir_name, ftw_add_entry_size, 10);
1094 dir_size = global_total_size;
1096 error("ftw subdir walk of %s failed: %s", dir_name,
1101 num_of_data_chunks = (dir_size + default_chunk_size - 1) /
1104 num_of_meta_chunks = (dir_size / 2) / default_chunk_size;
1105 if (((dir_size / 2) % default_chunk_size) != 0)
1106 num_of_meta_chunks++;
1107 if (num_of_meta_chunks <= num_of_allocated_meta_chunks)
1108 num_of_meta_chunks = 0;
1110 num_of_meta_chunks -= num_of_allocated_meta_chunks;
1112 total_size = allocated_total_size +
1113 (num_of_data_chunks * default_chunk_size) +
1114 (num_of_meta_chunks * default_chunk_size);
1116 *num_of_meta_chunks_ret = num_of_meta_chunks;
1117 *size_of_data_ret = num_of_data_chunks * default_chunk_size;
1121 static int zero_output_file(int out_fd, u64 size)
1129 memset(buf, 0, 4096);
1130 loop_num = size / 4096;
1131 for (i = 0; i < loop_num; i++) {
1132 written = pwrite64(out_fd, buf, 4096, location);
1133 if (written != 4096)
1140 static int is_ssd(const char *file)
1143 char wholedisk[PATH_MAX];
1144 char sysfs_path[PATH_MAX];
1150 probe = blkid_new_probe_from_filename(file);
1154 /* Device number of this disk (possibly a partition) */
1155 devno = blkid_probe_get_devno(probe);
1157 blkid_free_probe(probe);
1161 /* Get whole disk name (not full path) for this devno */
1162 ret = blkid_devno_to_wholedisk(devno,
1163 wholedisk, sizeof(wholedisk), NULL);
1165 blkid_free_probe(probe);
1169 snprintf(sysfs_path, PATH_MAX, "/sys/block/%s/queue/rotational",
1172 blkid_free_probe(probe);
1174 fd = open(sysfs_path, O_RDONLY);
1179 if (read(fd, &rotational, sizeof(char)) < sizeof(char)) {
1185 return !atoi((const char *)&rotational);
1188 static int _cmp_device_by_id(void *priv, struct list_head *a,
1189 struct list_head *b)
1191 return list_entry(a, struct btrfs_device, dev_list)->devid -
1192 list_entry(b, struct btrfs_device, dev_list)->devid;
1195 static void list_all_devices(struct btrfs_root *root)
1197 struct btrfs_fs_devices *fs_devices;
1198 struct btrfs_device *device;
1199 int number_of_devices = 0;
1200 u64 total_block_count = 0;
1202 fs_devices = root->fs_info->fs_devices;
1204 list_for_each_entry(device, &fs_devices->devices, dev_list)
1205 number_of_devices++;
1207 list_sort(NULL, &fs_devices->devices, _cmp_device_by_id);
1209 printf("Number of devices: %d\n", number_of_devices);
1210 /* printf("Total devices size: %10s\n", */
1211 /* pretty_size(total_block_count)); */
1212 printf("Devices:\n");
1213 printf(" ID SIZE PATH\n");
1214 list_for_each_entry(device, &fs_devices->devices, dev_list) {
1215 printf(" %3llu %10s %s\n",
1217 pretty_size(device->total_bytes),
1219 total_block_count += device->total_bytes;
1225 static int is_temp_block_group(struct extent_buffer *node,
1226 struct btrfs_block_group_item *bgi,
1227 u64 data_profile, u64 meta_profile,
1230 u64 flag = btrfs_disk_block_group_flags(node, bgi);
1231 u64 flag_type = flag & BTRFS_BLOCK_GROUP_TYPE_MASK;
1232 u64 flag_profile = flag & BTRFS_BLOCK_GROUP_PROFILE_MASK;
1233 u64 used = btrfs_disk_block_group_used(node, bgi);
1236 * Chunks meets all the following conditions is a temp chunk
1238 * Temp chunk is always empty.
1240 * 2) profile mismatch with mkfs profile.
1241 * Temp chunk is always in SINGLE
1243 * 3) Size differs with mkfs_alloc
1244 * Special case for SINGLE/SINGLE btrfs.
1245 * In that case, temp data chunk and real data chunk are always empty.
1246 * So we need to use mkfs_alloc to be sure which chunk is the newly
1249 * Normally, new chunk size is equal to mkfs one (One chunk)
1250 * If it has multiple chunks, we just refuse to delete any one.
1251 * As they are all single, so no real problem will happen.
1252 * So only use condition 1) and 2) to judge them.
1256 switch (flag_type) {
1257 case BTRFS_BLOCK_GROUP_DATA:
1258 case BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA:
1259 data_profile &= BTRFS_BLOCK_GROUP_PROFILE_MASK;
1260 if (flag_profile != data_profile)
1263 case BTRFS_BLOCK_GROUP_METADATA:
1264 meta_profile &= BTRFS_BLOCK_GROUP_PROFILE_MASK;
1265 if (flag_profile != meta_profile)
1268 case BTRFS_BLOCK_GROUP_SYSTEM:
1269 sys_profile &= BTRFS_BLOCK_GROUP_PROFILE_MASK;
1270 if (flag_profile != sys_profile)
1277 /* Note: if current is a block group, it will skip it anyway */
1278 static int next_block_group(struct btrfs_root *root,
1279 struct btrfs_path *path)
1281 struct btrfs_key key;
1285 ret = btrfs_next_item(root, path);
1289 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1290 if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY)
1297 /* This function will cleanup */
1298 static int cleanup_temp_chunks(struct btrfs_fs_info *fs_info,
1299 struct mkfs_allocation *alloc,
1300 u64 data_profile, u64 meta_profile,
1303 struct btrfs_trans_handle *trans = NULL;
1304 struct btrfs_block_group_item *bgi;
1305 struct btrfs_root *root = fs_info->extent_root;
1306 struct btrfs_key key;
1307 struct btrfs_key found_key;
1308 struct btrfs_path *path;
1311 path = btrfs_alloc_path();
1317 trans = btrfs_start_transaction(root, 1);
1320 key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
1325 * as the rest of the loop may modify the tree, we need to
1326 * start a new search each time.
1328 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
1332 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1334 if (found_key.objectid < key.objectid)
1336 if (found_key.type != BTRFS_BLOCK_GROUP_ITEM_KEY) {
1337 ret = next_block_group(root, path);
1344 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1348 bgi = btrfs_item_ptr(path->nodes[0], path->slots[0],
1349 struct btrfs_block_group_item);
1350 if (is_temp_block_group(path->nodes[0], bgi,
1351 data_profile, meta_profile,
1353 u64 flags = btrfs_disk_block_group_flags(path->nodes[0],
1356 ret = btrfs_free_block_group(trans, fs_info,
1357 found_key.objectid, found_key.offset);
1361 if ((flags & BTRFS_BLOCK_GROUP_TYPE_MASK) ==
1362 BTRFS_BLOCK_GROUP_DATA)
1363 alloc->data -= found_key.offset;
1364 else if ((flags & BTRFS_BLOCK_GROUP_TYPE_MASK) ==
1365 BTRFS_BLOCK_GROUP_METADATA)
1366 alloc->metadata -= found_key.offset;
1367 else if ((flags & BTRFS_BLOCK_GROUP_TYPE_MASK) ==
1368 BTRFS_BLOCK_GROUP_SYSTEM)
1369 alloc->system -= found_key.offset;
1370 else if ((flags & BTRFS_BLOCK_GROUP_TYPE_MASK) ==
1371 (BTRFS_BLOCK_GROUP_METADATA |
1372 BTRFS_BLOCK_GROUP_DATA))
1373 alloc->mixed -= found_key.offset;
1375 btrfs_release_path(path);
1376 key.objectid = found_key.objectid + found_key.offset;
1380 btrfs_commit_transaction(trans, root);
1381 btrfs_free_path(path);
1385 int main(int argc, char **argv)
1388 struct btrfs_root *root;
1389 struct btrfs_trans_handle *trans;
1391 u64 block_count = 0;
1392 u64 dev_block_count = 0;
1394 u64 alloc_start = 0;
1395 u64 metadata_profile = 0;
1396 u64 data_profile = 0;
1397 u32 nodesize = max_t(u32, sysconf(_SC_PAGESIZE),
1398 BTRFS_MKFS_DEFAULT_NODE_SIZE);
1399 u32 sectorsize = 4096;
1400 u32 stripesize = 4096;
1406 int nodesize_forced = 0;
1407 int data_profile_opt = 0;
1408 int metadata_profile_opt = 0;
1411 int force_overwrite = 0;
1412 char *source_dir = NULL;
1413 int source_dir_set = 0;
1414 u64 num_of_meta_chunks = 0;
1415 u64 size_of_data = 0;
1416 u64 source_dir_size = 0;
1419 char fs_uuid[BTRFS_UUID_UNPARSED_SIZE] = { 0 };
1420 u64 features = BTRFS_MKFS_DEFAULT_FEATURES;
1421 struct mkfs_allocation allocation = { 0 };
1422 struct btrfs_mkfs_config mkfs_cfg;
1426 static const struct option long_options[] = {
1427 { "alloc-start", required_argument, NULL, 'A'},
1428 { "byte-count", required_argument, NULL, 'b' },
1429 { "force", no_argument, NULL, 'f' },
1430 { "leafsize", required_argument, NULL, 'l' },
1431 { "label", required_argument, NULL, 'L'},
1432 { "metadata", required_argument, NULL, 'm' },
1433 { "mixed", no_argument, NULL, 'M' },
1434 { "nodesize", required_argument, NULL, 'n' },
1435 { "sectorsize", required_argument, NULL, 's' },
1436 { "data", required_argument, NULL, 'd' },
1437 { "version", no_argument, NULL, 'V' },
1438 { "rootdir", required_argument, NULL, 'r' },
1439 { "nodiscard", no_argument, NULL, 'K' },
1440 { "features", required_argument, NULL, 'O' },
1441 { "uuid", required_argument, NULL, 'U' },
1442 { "quiet", 0, NULL, 'q' },
1443 { "help", no_argument, NULL, GETOPT_VAL_HELP },
1447 c = getopt_long(argc, argv, "A:b:fl:n:s:m:d:L:O:r:U:VMKq",
1448 long_options, NULL);
1453 alloc_start = parse_size(optarg);
1456 force_overwrite = 1;
1459 data_profile = parse_profile(optarg);
1460 data_profile_opt = 1;
1463 warning("--leafsize is deprecated, use --nodesize");
1465 nodesize = parse_size(optarg);
1466 nodesize_forced = 1;
1469 label = parse_label(optarg);
1472 metadata_profile = parse_profile(optarg);
1473 metadata_profile_opt = 1;
1479 char *orig = strdup(optarg);
1482 tmp = btrfs_parse_fs_features(tmp, &features);
1484 error("unrecognized filesystem feature '%s'",
1490 if (features & BTRFS_FEATURE_LIST_ALL) {
1491 btrfs_list_all_fs_features(0);
1497 sectorsize = parse_size(optarg);
1500 block_count = parse_size(optarg);
1507 source_dir = optarg;
1511 strncpy(fs_uuid, optarg,
1512 BTRFS_UUID_UNPARSED_SIZE - 1);
1520 case GETOPT_VAL_HELP:
1522 print_usage(c != GETOPT_VAL_HELP);
1527 printf("%s\n", PACKAGE_STRING);
1528 printf("See %s for more information.\n\n", PACKAGE_URL);
1531 sectorsize = max(sectorsize, (u32)sysconf(_SC_PAGESIZE));
1532 stripesize = sectorsize;
1533 saved_optind = optind;
1534 dev_cnt = argc - optind;
1538 if (source_dir_set && dev_cnt > 1) {
1539 error("the option -r is limited to a single device");
1546 if (uuid_parse(fs_uuid, dummy_uuid) != 0) {
1547 error("could not parse UUID: %s", fs_uuid);
1550 if (!test_uuid_unique(fs_uuid)) {
1551 error("non-unique UUID: %s", fs_uuid);
1556 while (dev_cnt-- > 0) {
1557 file = argv[optind++];
1558 if (is_block_device(file) == 1)
1559 if (test_dev_for_mkfs(file, force_overwrite))
1563 optind = saved_optind;
1564 dev_cnt = argc - optind;
1566 file = argv[optind++];
1570 * Set default profiles according to number of added devices.
1571 * For mixed groups defaults are single/single.
1574 if (!metadata_profile_opt) {
1575 if (dev_cnt == 1 && ssd && verbose)
1576 printf("Detected a SSD, turning off metadata "
1577 "duplication. Mkfs with -m dup if you want to "
1578 "force metadata duplication.\n");
1580 metadata_profile = (dev_cnt > 1) ?
1581 BTRFS_BLOCK_GROUP_RAID1 : (ssd) ?
1582 0: BTRFS_BLOCK_GROUP_DUP;
1584 if (!data_profile_opt) {
1585 data_profile = (dev_cnt > 1) ?
1586 BTRFS_BLOCK_GROUP_RAID0 : 0; /* raid0 or single */
1589 u32 best_nodesize = max_t(u32, sysconf(_SC_PAGESIZE), sectorsize);
1591 if (metadata_profile_opt || data_profile_opt) {
1592 if (metadata_profile != data_profile) {
1594 "with mixed block groups data and metadata profiles must be the same");
1599 if (!nodesize_forced)
1600 nodesize = best_nodesize;
1604 * FS features that can be set by other means than -O
1605 * just set the bit here
1608 features |= BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS;
1610 if ((data_profile | metadata_profile) &
1611 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
1612 features |= BTRFS_FEATURE_INCOMPAT_RAID56;
1615 if (btrfs_check_nodesize(nodesize, sectorsize,
1619 /* Check device/block_count after the nodesize is determined */
1620 if (block_count && block_count < btrfs_min_dev_size(nodesize)) {
1621 error("size %llu is too small to make a usable filesystem",
1623 error("minimum size for btrfs filesystem is %llu",
1624 btrfs_min_dev_size(nodesize));
1627 for (i = saved_optind; i < saved_optind + dev_cnt; i++) {
1631 ret = test_minimum_size(path, nodesize);
1633 error("failed to check size for %s: %s",
1634 path, strerror(-ret));
1638 error("'%s' is too small to make a usable filesystem",
1640 error("minimum size for each btrfs device is %llu",
1641 btrfs_min_dev_size(nodesize));
1645 ret = test_num_disk_vs_raid(metadata_profile, data_profile,
1646 dev_cnt, mixed, ssd);
1652 if (!source_dir_set) {
1654 * open without O_EXCL so that the problem should not
1655 * occur by the following processing.
1656 * (btrfs_register_one_device() fails if O_EXCL is on)
1658 fd = open(file, O_RDWR);
1660 error("unable to open %s: %s", file, strerror(errno));
1663 ret = btrfs_prepare_device(fd, file, &dev_block_count,
1665 (zero_end ? PREP_DEVICE_ZERO_END : 0) |
1666 (discard ? PREP_DEVICE_DISCARD : 0) |
1667 (verbose ? PREP_DEVICE_VERBOSE : 0));
1672 if (block_count && block_count > dev_block_count) {
1673 error("%s is smaller than requested size, expected %llu, found %llu",
1675 (unsigned long long)block_count,
1676 (unsigned long long)dev_block_count);
1680 fd = open_target(file);
1682 error("unable to open %s: %s", file, strerror(errno));
1686 source_dir_size = size_sourcedir(source_dir, sectorsize,
1687 &num_of_meta_chunks, &size_of_data);
1688 if(block_count < source_dir_size)
1689 block_count = source_dir_size;
1690 ret = zero_output_file(fd, block_count);
1692 error("unable to zero the output file");
1695 /* our "device" is the new image file */
1696 dev_block_count = block_count;
1699 /* To create the first block group and chunk 0 in make_btrfs */
1700 if (dev_block_count < BTRFS_MKFS_SYSTEM_GROUP_SIZE) {
1701 error("device is too small to make filesystem, must be at least %llu",
1702 (unsigned long long)BTRFS_MKFS_SYSTEM_GROUP_SIZE);
1706 blocks[0] = BTRFS_SUPER_INFO_OFFSET;
1707 for (i = 1; i < 7; i++) {
1708 blocks[i] = BTRFS_SUPER_INFO_OFFSET + 1024 * 1024 +
1712 if (group_profile_max_safe_loss(metadata_profile) <
1713 group_profile_max_safe_loss(data_profile)){
1714 warning("metadata has lower redundancy than data!\n");
1717 mkfs_cfg.label = label;
1718 mkfs_cfg.fs_uuid = fs_uuid;
1719 memcpy(mkfs_cfg.blocks, blocks, sizeof(blocks));
1720 mkfs_cfg.num_bytes = dev_block_count;
1721 mkfs_cfg.nodesize = nodesize;
1722 mkfs_cfg.sectorsize = sectorsize;
1723 mkfs_cfg.stripesize = stripesize;
1724 mkfs_cfg.features = features;
1726 ret = make_btrfs(fd, &mkfs_cfg, NULL);
1728 error("error during mkfs: %s", strerror(-ret));
1732 root = open_ctree(file, 0, OPEN_CTREE_WRITES | OPEN_CTREE_FS_PARTIAL);
1734 error("open ctree failed");
1738 root->fs_info->alloc_start = alloc_start;
1740 ret = create_metadata_block_groups(root, mixed, &allocation);
1742 error("failed to create default block groups: %d", ret);
1746 trans = btrfs_start_transaction(root, 1);
1748 error("failed to start transaction");
1752 ret = create_data_block_groups(trans, root, mixed, &allocation);
1754 error("failed to create default data block groups: %d", ret);
1758 ret = make_root_dir(trans, root, &allocation);
1760 error("failed to setup the root directory: %d", ret);
1764 btrfs_commit_transaction(trans, root);
1766 trans = btrfs_start_transaction(root, 1);
1768 error("failed to start transaction");
1775 while (dev_cnt-- > 0) {
1776 file = argv[optind++];
1779 * open without O_EXCL so that the problem should not
1780 * occur by the following processing.
1781 * (btrfs_register_one_device() fails if O_EXCL is on)
1783 fd = open(file, O_RDWR);
1785 error("unable to open %s: %s", file, strerror(errno));
1788 ret = btrfs_device_already_in_root(root, fd,
1789 BTRFS_SUPER_INFO_OFFSET);
1791 error("skipping duplicate device %s in the filesystem",
1796 ret = btrfs_prepare_device(fd, file, &dev_block_count,
1798 (verbose ? PREP_DEVICE_VERBOSE : 0) |
1799 (zero_end ? PREP_DEVICE_ZERO_END : 0) |
1800 (discard ? PREP_DEVICE_DISCARD : 0));
1806 ret = btrfs_add_to_fsid(trans, root, fd, file, dev_block_count,
1807 sectorsize, sectorsize, sectorsize);
1809 error("unable to add %s to filesystem: %d", file, ret);
1813 struct btrfs_device *device;
1815 device = container_of(root->fs_info->fs_devices->devices.next,
1816 struct btrfs_device, dev_list);
1817 printf("adding device %s id %llu\n", file,
1818 (unsigned long long)device->devid);
1823 if (!source_dir_set) {
1824 ret = create_raid_groups(trans, root, data_profile,
1825 metadata_profile, mixed, &allocation);
1827 error("unable to create raid groups: %d", ret);
1832 ret = create_data_reloc_tree(trans, root);
1834 error("unable to create data reloc tree: %d", ret);
1838 btrfs_commit_transaction(trans, root);
1840 if (source_dir_set) {
1841 trans = btrfs_start_transaction(root, 1);
1842 ret = create_chunks(trans, root,
1843 num_of_meta_chunks, size_of_data,
1846 error("unable to create chunks: %d", ret);
1849 ret = btrfs_commit_transaction(trans, root);
1851 error("transaction commit failed: %d", ret);
1855 ret = make_image(source_dir, root, fd);
1857 error("error wihle filling filesystem: %d", ret);
1861 ret = cleanup_temp_chunks(root->fs_info, &allocation, data_profile,
1862 metadata_profile, metadata_profile);
1864 error("failed to cleanup temporary chunks: %d", ret);
1869 char features_buf[64];
1871 printf("Label: %s\n", label);
1872 printf("UUID: %s\n", fs_uuid);
1873 printf("Node size: %u\n", nodesize);
1874 printf("Sector size: %u\n", sectorsize);
1875 printf("Filesystem size: %s\n",
1876 pretty_size(btrfs_super_total_bytes(root->fs_info->super_copy)));
1877 printf("Block group profiles:\n");
1878 if (allocation.data)
1879 printf(" Data: %-8s %16s\n",
1880 btrfs_group_profile_str(data_profile),
1881 pretty_size(allocation.data));
1882 if (allocation.metadata)
1883 printf(" Metadata: %-8s %16s\n",
1884 btrfs_group_profile_str(metadata_profile),
1885 pretty_size(allocation.metadata));
1886 if (allocation.mixed)
1887 printf(" Data+Metadata: %-8s %16s\n",
1888 btrfs_group_profile_str(data_profile),
1889 pretty_size(allocation.mixed));
1890 printf(" System: %-8s %16s\n",
1891 btrfs_group_profile_str(metadata_profile),
1892 pretty_size(allocation.system));
1893 printf("SSD detected: %s\n", ssd ? "yes" : "no");
1894 btrfs_parse_features_to_string(features_buf, features);
1895 printf("Incompat features: %s", features_buf);
1898 list_all_devices(root);
1902 * The filesystem is now fully set up, commit the remaining changes and
1903 * fix the signature as the last step before closing the devices.
1905 root->fs_info->finalize_on_close = 1;
1907 ret = close_ctree(root);
1910 optind = saved_optind;
1911 dev_cnt = argc - optind;
1912 while (dev_cnt-- > 0) {
1913 file = argv[optind++];
1914 if (is_block_device(file) == 1)
1915 btrfs_register_one_device(file);
1919 btrfs_close_all_devices();