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 #include "mkfs/common.h"
48 #include "fsfeatures.h"
50 static u64 index_cnt = 2;
51 static int verbose = 1;
53 struct directory_name_entry {
57 struct list_head list;
60 struct mkfs_allocation {
67 static int create_metadata_block_groups(struct btrfs_root *root, int mixed,
68 struct mkfs_allocation *allocation)
70 struct btrfs_trans_handle *trans;
76 trans = btrfs_start_transaction(root, 1);
77 bytes_used = btrfs_super_bytes_used(root->fs_info->super_copy);
79 root->fs_info->system_allocs = 1;
80 ret = btrfs_make_block_group(trans, root, bytes_used,
81 BTRFS_BLOCK_GROUP_SYSTEM,
82 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
83 0, BTRFS_MKFS_SYSTEM_GROUP_SIZE);
84 allocation->system += BTRFS_MKFS_SYSTEM_GROUP_SIZE;
89 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
90 &chunk_start, &chunk_size,
91 BTRFS_BLOCK_GROUP_METADATA |
92 BTRFS_BLOCK_GROUP_DATA);
94 error("no space to allocate data/metadata chunk");
99 ret = btrfs_make_block_group(trans, root, 0,
100 BTRFS_BLOCK_GROUP_METADATA |
101 BTRFS_BLOCK_GROUP_DATA,
102 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
103 chunk_start, chunk_size);
106 allocation->mixed += chunk_size;
108 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
109 &chunk_start, &chunk_size,
110 BTRFS_BLOCK_GROUP_METADATA);
111 if (ret == -ENOSPC) {
112 error("no space to allocate metadata chunk");
117 ret = btrfs_make_block_group(trans, root, 0,
118 BTRFS_BLOCK_GROUP_METADATA,
119 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
120 chunk_start, chunk_size);
121 allocation->metadata += chunk_size;
126 root->fs_info->system_allocs = 0;
127 ret = btrfs_commit_transaction(trans, root);
133 static int create_data_block_groups(struct btrfs_trans_handle *trans,
134 struct btrfs_root *root, int mixed,
135 struct mkfs_allocation *allocation)
142 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
143 &chunk_start, &chunk_size,
144 BTRFS_BLOCK_GROUP_DATA);
145 if (ret == -ENOSPC) {
146 error("no space to allocate data chunk");
151 ret = btrfs_make_block_group(trans, root, 0,
152 BTRFS_BLOCK_GROUP_DATA,
153 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
154 chunk_start, chunk_size);
155 allocation->data += chunk_size;
164 static int make_root_dir(struct btrfs_trans_handle *trans, struct btrfs_root *root,
165 struct mkfs_allocation *allocation)
167 struct btrfs_key location;
170 ret = btrfs_make_root_dir(trans, root->fs_info->tree_root,
171 BTRFS_ROOT_TREE_DIR_OBJECTID);
174 ret = btrfs_make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID);
177 memcpy(&location, &root->fs_info->fs_root->root_key, sizeof(location));
178 location.offset = (u64)-1;
179 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
181 btrfs_super_root_dir(root->fs_info->super_copy),
182 &location, BTRFS_FT_DIR, 0);
186 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
187 "default", 7, location.objectid,
188 BTRFS_ROOT_TREE_DIR_OBJECTID, 0);
196 static int __recow_root(struct btrfs_trans_handle *trans,
197 struct btrfs_root *root)
199 struct extent_buffer *tmp;
202 if (trans->transid != btrfs_root_generation(&root->root_item)) {
203 extent_buffer_get(root->node);
204 ret = __btrfs_cow_block(trans, root, root->node,
205 NULL, 0, &tmp, 0, 0);
208 free_extent_buffer(tmp);
214 static int recow_roots(struct btrfs_trans_handle *trans,
215 struct btrfs_root *root)
217 struct btrfs_fs_info *info = root->fs_info;
220 ret = __recow_root(trans, info->fs_root);
223 ret = __recow_root(trans, info->tree_root);
226 ret = __recow_root(trans, info->extent_root);
229 ret = __recow_root(trans, info->chunk_root);
232 ret = __recow_root(trans, info->dev_root);
235 ret = __recow_root(trans, info->csum_root);
242 static int create_one_raid_group(struct btrfs_trans_handle *trans,
243 struct btrfs_root *root, u64 type,
244 struct mkfs_allocation *allocation)
251 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
252 &chunk_start, &chunk_size, type);
253 if (ret == -ENOSPC) {
254 error("not enough free space to allocate chunk");
260 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
261 type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
262 chunk_start, chunk_size);
264 type &= BTRFS_BLOCK_GROUP_TYPE_MASK;
265 if (type == BTRFS_BLOCK_GROUP_DATA) {
266 allocation->data += chunk_size;
267 } else if (type == BTRFS_BLOCK_GROUP_METADATA) {
268 allocation->metadata += chunk_size;
269 } else if (type == BTRFS_BLOCK_GROUP_SYSTEM) {
270 allocation->system += chunk_size;
272 (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA)) {
273 allocation->mixed += chunk_size;
275 error("unrecognized profile type: 0x%llx",
276 (unsigned long long)type);
283 static int create_raid_groups(struct btrfs_trans_handle *trans,
284 struct btrfs_root *root, u64 data_profile,
285 u64 metadata_profile, int mixed,
286 struct mkfs_allocation *allocation)
290 if (metadata_profile) {
291 u64 meta_flags = BTRFS_BLOCK_GROUP_METADATA;
293 ret = create_one_raid_group(trans, root,
294 BTRFS_BLOCK_GROUP_SYSTEM |
295 metadata_profile, allocation);
300 meta_flags |= BTRFS_BLOCK_GROUP_DATA;
302 ret = create_one_raid_group(trans, root, meta_flags |
303 metadata_profile, allocation);
308 if (!mixed && data_profile) {
309 ret = create_one_raid_group(trans, root,
310 BTRFS_BLOCK_GROUP_DATA |
311 data_profile, allocation);
315 ret = recow_roots(trans, root);
320 static int create_data_reloc_tree(struct btrfs_trans_handle *trans,
321 struct btrfs_root *root)
323 struct btrfs_key location;
324 struct btrfs_root_item root_item;
325 struct extent_buffer *tmp;
326 u64 objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
329 ret = btrfs_copy_root(trans, root, root->node, &tmp, objectid);
333 memcpy(&root_item, &root->root_item, sizeof(root_item));
334 btrfs_set_root_bytenr(&root_item, tmp->start);
335 btrfs_set_root_level(&root_item, btrfs_header_level(tmp));
336 btrfs_set_root_generation(&root_item, trans->transid);
337 free_extent_buffer(tmp);
339 location.objectid = objectid;
340 location.type = BTRFS_ROOT_ITEM_KEY;
342 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
343 &location, &root_item);
348 static void print_usage(int ret)
350 printf("Usage: mkfs.btrfs [options] dev [ dev ... ]\n");
351 printf("Options:\n");
352 printf(" allocation profiles:\n");
353 printf("\t-d|--data PROFILE data profile, raid0, raid1, raid5, raid6, raid10, dup or single\n");
354 printf("\t-m|--metadata PROFILE metadata profile, values like for data profile\n");
355 printf("\t-M|--mixed mix metadata and data together\n");
356 printf(" features:\n");
357 printf("\t-n|--nodesize SIZE size of btree nodes\n");
358 printf("\t-s|--sectorsize SIZE data block size (may not be mountable by current kernel)\n");
359 printf("\t-O|--features LIST comma separated list of filesystem features (use '-O list-all' to list features)\n");
360 printf("\t-L|--label LABEL set the filesystem label\n");
361 printf("\t-U|--uuid UUID specify the filesystem UUID (must be unique)\n");
362 printf(" creation:\n");
363 printf("\t-b|--byte-count SIZE set filesystem size to SIZE (on the first device)\n");
364 printf("\t-r|--rootdir DIR copy files from DIR to the image root directory\n");
365 printf("\t-K|--nodiscard do not perform whole device TRIM\n");
366 printf("\t-f|--force force overwrite of existing filesystem\n");
367 printf(" general:\n");
368 printf("\t-q|--quiet no messages except errors\n");
369 printf("\t-V|--version print the mkfs.btrfs version and exit\n");
370 printf("\t--help print this help and exit\n");
371 printf(" deprecated:\n");
372 printf("\t-A|--alloc-start START the offset to start the filesystem\n");
373 printf("\t-l|--leafsize SIZE deprecated, alias for nodesize\n");
377 static u64 parse_profile(const char *s)
379 if (strcasecmp(s, "raid0") == 0) {
380 return BTRFS_BLOCK_GROUP_RAID0;
381 } else if (strcasecmp(s, "raid1") == 0) {
382 return BTRFS_BLOCK_GROUP_RAID1;
383 } else if (strcasecmp(s, "raid5") == 0) {
384 return BTRFS_BLOCK_GROUP_RAID5;
385 } else if (strcasecmp(s, "raid6") == 0) {
386 return BTRFS_BLOCK_GROUP_RAID6;
387 } else if (strcasecmp(s, "raid10") == 0) {
388 return BTRFS_BLOCK_GROUP_RAID10;
389 } else if (strcasecmp(s, "dup") == 0) {
390 return BTRFS_BLOCK_GROUP_DUP;
391 } else if (strcasecmp(s, "single") == 0) {
394 error("unknown profile %s", s);
401 static char *parse_label(const char *input)
403 int len = strlen(input);
405 if (len >= BTRFS_LABEL_SIZE) {
406 error("label %s is too long (max %d)", input,
407 BTRFS_LABEL_SIZE - 1);
410 return strdup(input);
413 static int add_directory_items(struct btrfs_trans_handle *trans,
414 struct btrfs_root *root, u64 objectid,
415 ino_t parent_inum, const char *name,
416 struct stat *st, int *dir_index_cnt)
420 struct btrfs_key location;
423 name_len = strlen(name);
425 location.objectid = objectid;
427 location.type = BTRFS_INODE_ITEM_KEY;
429 if (S_ISDIR(st->st_mode))
430 filetype = BTRFS_FT_DIR;
431 if (S_ISREG(st->st_mode))
432 filetype = BTRFS_FT_REG_FILE;
433 if (S_ISLNK(st->st_mode))
434 filetype = BTRFS_FT_SYMLINK;
436 ret = btrfs_insert_dir_item(trans, root, name, name_len,
437 parent_inum, &location,
438 filetype, index_cnt);
441 ret = btrfs_insert_inode_ref(trans, root, name, name_len,
442 objectid, parent_inum, index_cnt);
443 *dir_index_cnt = index_cnt;
449 static int fill_inode_item(struct btrfs_trans_handle *trans,
450 struct btrfs_root *root,
451 struct btrfs_inode_item *dst, struct stat *src)
454 u64 sectorsize = root->sectorsize;
457 * btrfs_inode_item has some reserved fields
458 * and represents on-disk inode entry, so
459 * zero everything to prevent information leak
461 memset(dst, 0, sizeof (*dst));
463 btrfs_set_stack_inode_generation(dst, trans->transid);
464 btrfs_set_stack_inode_size(dst, src->st_size);
465 btrfs_set_stack_inode_nbytes(dst, 0);
466 btrfs_set_stack_inode_block_group(dst, 0);
467 btrfs_set_stack_inode_nlink(dst, src->st_nlink);
468 btrfs_set_stack_inode_uid(dst, src->st_uid);
469 btrfs_set_stack_inode_gid(dst, src->st_gid);
470 btrfs_set_stack_inode_mode(dst, src->st_mode);
471 btrfs_set_stack_inode_rdev(dst, 0);
472 btrfs_set_stack_inode_flags(dst, 0);
473 btrfs_set_stack_timespec_sec(&dst->atime, src->st_atime);
474 btrfs_set_stack_timespec_nsec(&dst->atime, 0);
475 btrfs_set_stack_timespec_sec(&dst->ctime, src->st_ctime);
476 btrfs_set_stack_timespec_nsec(&dst->ctime, 0);
477 btrfs_set_stack_timespec_sec(&dst->mtime, src->st_mtime);
478 btrfs_set_stack_timespec_nsec(&dst->mtime, 0);
479 btrfs_set_stack_timespec_sec(&dst->otime, 0);
480 btrfs_set_stack_timespec_nsec(&dst->otime, 0);
482 if (S_ISDIR(src->st_mode)) {
483 btrfs_set_stack_inode_size(dst, 0);
484 btrfs_set_stack_inode_nlink(dst, 1);
486 if (S_ISREG(src->st_mode)) {
487 btrfs_set_stack_inode_size(dst, (u64)src->st_size);
488 if (src->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root))
489 btrfs_set_stack_inode_nbytes(dst, src->st_size);
491 blocks = src->st_size / sectorsize;
492 if (src->st_size % sectorsize)
494 blocks *= sectorsize;
495 btrfs_set_stack_inode_nbytes(dst, blocks);
498 if (S_ISLNK(src->st_mode))
499 btrfs_set_stack_inode_nbytes(dst, src->st_size + 1);
504 static int directory_select(const struct direct *entry)
506 if (entry->d_name[0] == '.' &&
507 (entry->d_name[1] == 0 ||
508 (entry->d_name[1] == '.' && entry->d_name[2] == 0)))
513 static void free_namelist(struct direct **files, int count)
520 for (i = 0; i < count; ++i)
525 static u64 calculate_dir_inode_size(const char *dirname)
528 struct direct **files, *cur_file;
529 u64 dir_inode_size = 0;
531 count = scandir(dirname, &files, directory_select, NULL);
533 for (i = 0; i < count; i++) {
535 dir_inode_size += strlen(cur_file->d_name);
538 free_namelist(files, count);
541 return dir_inode_size;
544 static int add_inode_items(struct btrfs_trans_handle *trans,
545 struct btrfs_root *root,
546 struct stat *st, const char *name,
547 u64 self_objectid, ino_t parent_inum,
548 int dir_index_cnt, struct btrfs_inode_item *inode_ret)
551 struct btrfs_inode_item btrfs_inode;
555 fill_inode_item(trans, root, &btrfs_inode, st);
556 objectid = self_objectid;
558 if (S_ISDIR(st->st_mode)) {
559 inode_size = calculate_dir_inode_size(name);
560 btrfs_set_stack_inode_size(&btrfs_inode, inode_size);
563 ret = btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
565 *inode_ret = btrfs_inode;
569 static int add_xattr_item(struct btrfs_trans_handle *trans,
570 struct btrfs_root *root, u64 objectid,
571 const char *file_name)
575 char xattr_list[XATTR_LIST_MAX];
577 char cur_value[XATTR_SIZE_MAX];
578 char delimiter = '\0';
579 char *next_location = xattr_list;
581 ret = llistxattr(file_name, xattr_list, XATTR_LIST_MAX);
585 error("getting a list of xattr failed for %s: %s", file_name,
592 cur_name = strtok(xattr_list, &delimiter);
593 while (cur_name != NULL) {
594 cur_name_len = strlen(cur_name);
595 next_location += cur_name_len + 1;
597 ret = getxattr(file_name, cur_name, cur_value, XATTR_SIZE_MAX);
601 error("gettig a xattr value failed for %s attr %s: %s",
602 file_name, cur_name, strerror(errno));
606 ret = btrfs_insert_xattr_item(trans, root, cur_name,
607 cur_name_len, cur_value,
610 error("inserting a xattr item failed for %s: %s",
611 file_name, strerror(-ret));
614 cur_name = strtok(next_location, &delimiter);
620 static int add_symbolic_link(struct btrfs_trans_handle *trans,
621 struct btrfs_root *root,
622 u64 objectid, const char *path_name)
627 ret = readlink(path_name, buf, sizeof(buf));
629 error("readlink failed for %s: %s", path_name, strerror(errno));
632 if (ret >= sizeof(buf)) {
633 error("symlink too long for %s", path_name);
638 buf[ret] = '\0'; /* readlink does not do it for us */
639 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
645 static int add_file_items(struct btrfs_trans_handle *trans,
646 struct btrfs_root *root,
647 struct btrfs_inode_item *btrfs_inode, u64 objectid,
648 ino_t parent_inum, struct stat *st,
649 const char *path_name, int out_fd)
654 struct btrfs_key key;
656 u32 sectorsize = root->sectorsize;
661 struct extent_buffer *eb = NULL;
664 if (st->st_size == 0)
667 fd = open(path_name, O_RDONLY);
669 error("cannot open %s: %s", path_name, strerror(errno));
673 blocks = st->st_size / sectorsize;
674 if (st->st_size % sectorsize)
677 if (st->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root)) {
678 char *buffer = malloc(st->st_size);
685 ret_read = pread64(fd, buffer, st->st_size, bytes_read);
686 if (ret_read == -1) {
687 error("cannot read %s at offset %llu length %llu: %s",
688 path_name, (unsigned long long)bytes_read,
689 (unsigned long long)st->st_size,
695 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
696 buffer, st->st_size);
701 /* round up our st_size to the FS blocksize */
702 total_bytes = (u64)blocks * sectorsize;
705 * do our IO in extent buffers so it can work
706 * against any raid type
708 eb = calloc(1, sizeof(*eb) + sectorsize);
717 * keep our extent size at 1MB max, this makes it easier to work inside
718 * the tiny block groups created during mkfs
720 cur_bytes = min(total_bytes, 1024ULL * 1024);
721 ret = btrfs_reserve_extent(trans, root, cur_bytes, 0, 0, (u64)-1,
726 first_block = key.objectid;
729 while (bytes_read < cur_bytes) {
731 memset(eb->data, 0, sectorsize);
733 ret_read = pread64(fd, eb->data, sectorsize, file_pos + bytes_read);
734 if (ret_read == -1) {
735 error("cannot read %s at offset %llu length %llu: %s",
737 (unsigned long long)file_pos + bytes_read,
738 (unsigned long long)sectorsize,
743 eb->start = first_block + bytes_read;
744 eb->len = sectorsize;
747 * we're doing the csum before we record the extent, but
750 ret = btrfs_csum_file_block(trans, root->fs_info->csum_root,
751 first_block + bytes_read + sectorsize,
752 first_block + bytes_read,
753 eb->data, sectorsize);
757 ret = write_and_map_eb(trans, root, eb);
759 error("failed to write %s", path_name);
763 bytes_read += sectorsize;
767 ret = btrfs_record_file_extent(trans, root, objectid, btrfs_inode,
768 file_pos, first_block, cur_bytes);
774 file_pos += cur_bytes;
775 total_bytes -= cur_bytes;
786 static char *make_path(const char *dir, const char *name)
790 path = malloc(strlen(dir) + strlen(name) + 2);
794 if (dir[strlen(dir) - 1] != '/')
800 static int traverse_directory(struct btrfs_trans_handle *trans,
801 struct btrfs_root *root, const char *dir_name,
802 struct directory_name_entry *dir_head, int out_fd)
806 struct btrfs_inode_item cur_inode;
807 struct btrfs_inode_item *inode_item;
808 int count, i, dir_index_cnt;
809 struct direct **files;
811 struct directory_name_entry *dir_entry, *parent_dir_entry;
812 struct direct *cur_file;
813 ino_t parent_inum, cur_inum;
814 ino_t highest_inum = 0;
815 const char *parent_dir_name;
816 char real_path[PATH_MAX];
817 struct btrfs_path path;
818 struct extent_buffer *leaf;
819 struct btrfs_key root_dir_key;
820 u64 root_dir_inode_size = 0;
822 /* Add list for source directory */
823 dir_entry = malloc(sizeof(struct directory_name_entry));
826 dir_entry->dir_name = dir_name;
827 dir_entry->path = realpath(dir_name, real_path);
828 if (!dir_entry->path) {
829 error("realpath failed for %s: %s", dir_name, strerror(errno));
834 parent_inum = highest_inum + BTRFS_FIRST_FREE_OBJECTID;
835 dir_entry->inum = parent_inum;
836 list_add_tail(&dir_entry->list, &dir_head->list);
838 btrfs_init_path(&path);
840 root_dir_key.objectid = btrfs_root_dirid(&root->root_item);
841 root_dir_key.offset = 0;
842 root_dir_key.type = BTRFS_INODE_ITEM_KEY;
843 ret = btrfs_lookup_inode(trans, root, &path, &root_dir_key, 1);
845 error("failed to lookup root dir: %d", ret);
849 leaf = path.nodes[0];
850 inode_item = btrfs_item_ptr(leaf, path.slots[0],
851 struct btrfs_inode_item);
853 root_dir_inode_size = calculate_dir_inode_size(dir_name);
854 btrfs_set_inode_size(leaf, inode_item, root_dir_inode_size);
855 btrfs_mark_buffer_dirty(leaf);
857 btrfs_release_path(&path);
860 parent_dir_entry = list_entry(dir_head->list.next,
861 struct directory_name_entry,
863 list_del(&parent_dir_entry->list);
865 parent_inum = parent_dir_entry->inum;
866 parent_dir_name = parent_dir_entry->dir_name;
867 if (chdir(parent_dir_entry->path)) {
868 error("chdir failed for %s: %s",
869 parent_dir_name, strerror(errno));
874 count = scandir(parent_dir_entry->path, &files,
875 directory_select, NULL);
878 error("scandir failed for %s: %s",
879 parent_dir_name, strerror (errno));
884 for (i = 0; i < count; i++) {
887 if (lstat(cur_file->d_name, &st) == -1) {
888 error("lstat failed for %s: %s",
889 cur_file->d_name, strerror(errno));
894 cur_inum = st.st_ino;
895 ret = add_directory_items(trans, root,
896 cur_inum, parent_inum,
898 &st, &dir_index_cnt);
900 error("unable to add directory items for %s: %d",
901 cur_file->d_name, ret);
905 ret = add_inode_items(trans, root, &st,
906 cur_file->d_name, cur_inum,
907 parent_inum, dir_index_cnt,
909 if (ret == -EEXIST) {
910 if (st.st_nlink <= 1) {
912 "item %s already exists but has wrong st_nlink %lu <= 1",
914 (unsigned long)st.st_nlink);
920 error("unable to add inode items for %s: %d",
921 cur_file->d_name, ret);
925 ret = add_xattr_item(trans, root,
926 cur_inum, cur_file->d_name);
928 error("unable to add xattr items for %s: %d",
929 cur_file->d_name, ret);
934 if (S_ISDIR(st.st_mode)) {
935 dir_entry = malloc(sizeof(struct directory_name_entry));
940 dir_entry->dir_name = cur_file->d_name;
941 dir_entry->path = make_path(parent_dir_entry->path,
943 dir_entry->inum = cur_inum;
944 list_add_tail(&dir_entry->list, &dir_head->list);
945 } else if (S_ISREG(st.st_mode)) {
946 ret = add_file_items(trans, root, &cur_inode,
947 cur_inum, parent_inum, &st,
948 cur_file->d_name, out_fd);
950 error("unable to add file items for %s: %d",
951 cur_file->d_name, ret);
954 } else if (S_ISLNK(st.st_mode)) {
955 ret = add_symbolic_link(trans, root,
956 cur_inum, cur_file->d_name);
958 error("unable to add symlink for %s: %d",
959 cur_file->d_name, ret);
965 free_namelist(files, count);
966 free(parent_dir_entry);
970 } while (!list_empty(&dir_head->list));
975 free_namelist(files, count);
977 free(parent_dir_entry);
984 static int create_chunks(struct btrfs_trans_handle *trans,
985 struct btrfs_root *root, u64 num_of_meta_chunks,
987 struct mkfs_allocation *allocation)
991 u64 meta_type = BTRFS_BLOCK_GROUP_METADATA;
992 u64 data_type = BTRFS_BLOCK_GROUP_DATA;
993 u64 minimum_data_chunk_size = 8 * 1024 * 1024;
997 for (i = 0; i < num_of_meta_chunks; i++) {
998 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
999 &chunk_start, &chunk_size, meta_type);
1002 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
1003 meta_type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
1004 chunk_start, chunk_size);
1005 allocation->metadata += chunk_size;
1008 set_extent_dirty(&root->fs_info->free_space_cache,
1009 chunk_start, chunk_start + chunk_size - 1, 0);
1012 if (size_of_data < minimum_data_chunk_size)
1013 size_of_data = minimum_data_chunk_size;
1015 ret = btrfs_alloc_data_chunk(trans, root->fs_info->extent_root,
1016 &chunk_start, size_of_data, data_type, 0);
1019 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
1020 data_type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
1021 chunk_start, size_of_data);
1022 allocation->data += size_of_data;
1025 set_extent_dirty(&root->fs_info->free_space_cache,
1026 chunk_start, chunk_start + size_of_data - 1, 0);
1030 static int make_image(const char *source_dir, struct btrfs_root *root,
1034 struct btrfs_trans_handle *trans;
1035 struct stat root_st;
1036 struct directory_name_entry dir_head;
1037 struct directory_name_entry *dir_entry = NULL;
1039 ret = lstat(source_dir, &root_st);
1041 error("unable to lstat %s: %s", source_dir, strerror(errno));
1046 INIT_LIST_HEAD(&dir_head.list);
1048 trans = btrfs_start_transaction(root, 1);
1049 ret = traverse_directory(trans, root, source_dir, &dir_head, out_fd);
1051 error("unable to traverse directory %s: %d", source_dir, ret);
1054 ret = btrfs_commit_transaction(trans, root);
1056 error("transaction commit failed: %d", ret);
1061 printf("Making image is completed.\n");
1064 while (!list_empty(&dir_head.list)) {
1065 dir_entry = list_entry(dir_head.list.next,
1066 struct directory_name_entry, list);
1067 list_del(&dir_entry->list);
1075 * This ignores symlinks with unreadable targets and subdirs that can't
1076 * be read. It's a best-effort to give a rough estimate of the size of
1077 * a subdir. It doesn't guarantee that prepopulating btrfs from this
1078 * tree won't still run out of space.
1080 static u64 global_total_size;
1081 static u64 fs_block_size;
1082 static int ftw_add_entry_size(const char *fpath, const struct stat *st,
1085 if (type == FTW_F || type == FTW_D)
1086 global_total_size += round_up(st->st_size, fs_block_size);
1091 static u64 size_sourcedir(const char *dir_name, u64 sectorsize,
1092 u64 *num_of_meta_chunks_ret, u64 *size_of_data_ret)
1097 u64 default_chunk_size = 8 * 1024 * 1024; /* 8MB */
1098 u64 allocated_meta_size = 8 * 1024 * 1024; /* 8MB */
1099 u64 allocated_total_size = 20 * 1024 * 1024; /* 20MB */
1100 u64 num_of_meta_chunks = 0;
1101 u64 num_of_data_chunks = 0;
1102 u64 num_of_allocated_meta_chunks =
1103 allocated_meta_size / default_chunk_size;
1105 global_total_size = 0;
1106 fs_block_size = sectorsize;
1107 ret = ftw(dir_name, ftw_add_entry_size, 10);
1108 dir_size = global_total_size;
1110 error("ftw subdir walk of %s failed: %s", dir_name,
1115 num_of_data_chunks = (dir_size + default_chunk_size - 1) /
1118 num_of_meta_chunks = (dir_size / 2) / default_chunk_size;
1119 if (((dir_size / 2) % default_chunk_size) != 0)
1120 num_of_meta_chunks++;
1121 if (num_of_meta_chunks <= num_of_allocated_meta_chunks)
1122 num_of_meta_chunks = 0;
1124 num_of_meta_chunks -= num_of_allocated_meta_chunks;
1126 total_size = allocated_total_size +
1127 (num_of_data_chunks * default_chunk_size) +
1128 (num_of_meta_chunks * default_chunk_size);
1130 *num_of_meta_chunks_ret = num_of_meta_chunks;
1131 *size_of_data_ret = num_of_data_chunks * default_chunk_size;
1135 static int zero_output_file(int out_fd, u64 size)
1143 memset(buf, 0, 4096);
1144 loop_num = size / 4096;
1145 for (i = 0; i < loop_num; i++) {
1146 written = pwrite64(out_fd, buf, 4096, location);
1147 if (written != 4096)
1154 static int is_ssd(const char *file)
1157 char wholedisk[PATH_MAX];
1158 char sysfs_path[PATH_MAX];
1164 probe = blkid_new_probe_from_filename(file);
1168 /* Device number of this disk (possibly a partition) */
1169 devno = blkid_probe_get_devno(probe);
1171 blkid_free_probe(probe);
1175 /* Get whole disk name (not full path) for this devno */
1176 ret = blkid_devno_to_wholedisk(devno,
1177 wholedisk, sizeof(wholedisk), NULL);
1179 blkid_free_probe(probe);
1183 snprintf(sysfs_path, PATH_MAX, "/sys/block/%s/queue/rotational",
1186 blkid_free_probe(probe);
1188 fd = open(sysfs_path, O_RDONLY);
1193 if (read(fd, &rotational, 1) < 1) {
1199 return rotational == '0';
1202 static int _cmp_device_by_id(void *priv, struct list_head *a,
1203 struct list_head *b)
1205 return list_entry(a, struct btrfs_device, dev_list)->devid -
1206 list_entry(b, struct btrfs_device, dev_list)->devid;
1209 static void list_all_devices(struct btrfs_root *root)
1211 struct btrfs_fs_devices *fs_devices;
1212 struct btrfs_device *device;
1213 int number_of_devices = 0;
1214 u64 total_block_count = 0;
1216 fs_devices = root->fs_info->fs_devices;
1218 list_for_each_entry(device, &fs_devices->devices, dev_list)
1219 number_of_devices++;
1221 list_sort(NULL, &fs_devices->devices, _cmp_device_by_id);
1223 printf("Number of devices: %d\n", number_of_devices);
1224 /* printf("Total devices size: %10s\n", */
1225 /* pretty_size(total_block_count)); */
1226 printf("Devices:\n");
1227 printf(" ID SIZE PATH\n");
1228 list_for_each_entry(device, &fs_devices->devices, dev_list) {
1229 printf(" %3llu %10s %s\n",
1231 pretty_size(device->total_bytes),
1233 total_block_count += device->total_bytes;
1239 static int is_temp_block_group(struct extent_buffer *node,
1240 struct btrfs_block_group_item *bgi,
1241 u64 data_profile, u64 meta_profile,
1244 u64 flag = btrfs_disk_block_group_flags(node, bgi);
1245 u64 flag_type = flag & BTRFS_BLOCK_GROUP_TYPE_MASK;
1246 u64 flag_profile = flag & BTRFS_BLOCK_GROUP_PROFILE_MASK;
1247 u64 used = btrfs_disk_block_group_used(node, bgi);
1250 * Chunks meets all the following conditions is a temp chunk
1252 * Temp chunk is always empty.
1254 * 2) profile mismatch with mkfs profile.
1255 * Temp chunk is always in SINGLE
1257 * 3) Size differs with mkfs_alloc
1258 * Special case for SINGLE/SINGLE btrfs.
1259 * In that case, temp data chunk and real data chunk are always empty.
1260 * So we need to use mkfs_alloc to be sure which chunk is the newly
1263 * Normally, new chunk size is equal to mkfs one (One chunk)
1264 * If it has multiple chunks, we just refuse to delete any one.
1265 * As they are all single, so no real problem will happen.
1266 * So only use condition 1) and 2) to judge them.
1270 switch (flag_type) {
1271 case BTRFS_BLOCK_GROUP_DATA:
1272 case BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA:
1273 data_profile &= BTRFS_BLOCK_GROUP_PROFILE_MASK;
1274 if (flag_profile != data_profile)
1277 case BTRFS_BLOCK_GROUP_METADATA:
1278 meta_profile &= BTRFS_BLOCK_GROUP_PROFILE_MASK;
1279 if (flag_profile != meta_profile)
1282 case BTRFS_BLOCK_GROUP_SYSTEM:
1283 sys_profile &= BTRFS_BLOCK_GROUP_PROFILE_MASK;
1284 if (flag_profile != sys_profile)
1291 /* Note: if current is a block group, it will skip it anyway */
1292 static int next_block_group(struct btrfs_root *root,
1293 struct btrfs_path *path)
1295 struct btrfs_key key;
1299 ret = btrfs_next_item(root, path);
1303 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1304 if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY)
1311 /* This function will cleanup */
1312 static int cleanup_temp_chunks(struct btrfs_fs_info *fs_info,
1313 struct mkfs_allocation *alloc,
1314 u64 data_profile, u64 meta_profile,
1317 struct btrfs_trans_handle *trans = NULL;
1318 struct btrfs_block_group_item *bgi;
1319 struct btrfs_root *root = fs_info->extent_root;
1320 struct btrfs_key key;
1321 struct btrfs_key found_key;
1322 struct btrfs_path path;
1325 btrfs_init_path(&path);
1326 trans = btrfs_start_transaction(root, 1);
1329 key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
1334 * as the rest of the loop may modify the tree, we need to
1335 * start a new search each time.
1337 ret = btrfs_search_slot(trans, root, &key, &path, 0, 0);
1341 btrfs_item_key_to_cpu(path.nodes[0], &found_key,
1343 if (found_key.objectid < key.objectid)
1345 if (found_key.type != BTRFS_BLOCK_GROUP_ITEM_KEY) {
1346 ret = next_block_group(root, &path);
1353 btrfs_item_key_to_cpu(path.nodes[0], &found_key,
1357 bgi = btrfs_item_ptr(path.nodes[0], path.slots[0],
1358 struct btrfs_block_group_item);
1359 if (is_temp_block_group(path.nodes[0], bgi,
1360 data_profile, meta_profile,
1362 u64 flags = btrfs_disk_block_group_flags(path.nodes[0],
1365 ret = btrfs_free_block_group(trans, fs_info,
1366 found_key.objectid, found_key.offset);
1370 if ((flags & BTRFS_BLOCK_GROUP_TYPE_MASK) ==
1371 BTRFS_BLOCK_GROUP_DATA)
1372 alloc->data -= found_key.offset;
1373 else if ((flags & BTRFS_BLOCK_GROUP_TYPE_MASK) ==
1374 BTRFS_BLOCK_GROUP_METADATA)
1375 alloc->metadata -= found_key.offset;
1376 else if ((flags & BTRFS_BLOCK_GROUP_TYPE_MASK) ==
1377 BTRFS_BLOCK_GROUP_SYSTEM)
1378 alloc->system -= found_key.offset;
1379 else if ((flags & BTRFS_BLOCK_GROUP_TYPE_MASK) ==
1380 (BTRFS_BLOCK_GROUP_METADATA |
1381 BTRFS_BLOCK_GROUP_DATA))
1382 alloc->mixed -= found_key.offset;
1384 btrfs_release_path(&path);
1385 key.objectid = found_key.objectid + found_key.offset;
1389 btrfs_commit_transaction(trans, root);
1390 btrfs_release_path(&path);
1394 int main(int argc, char **argv)
1397 struct btrfs_root *root;
1398 struct btrfs_fs_info *fs_info;
1399 struct btrfs_trans_handle *trans;
1401 u64 block_count = 0;
1402 u64 dev_block_count = 0;
1403 u64 alloc_start = 0;
1404 u64 metadata_profile = 0;
1405 u64 data_profile = 0;
1406 u32 nodesize = max_t(u32, sysconf(_SC_PAGESIZE),
1407 BTRFS_MKFS_DEFAULT_NODE_SIZE);
1408 u32 sectorsize = 4096;
1409 u32 stripesize = 4096;
1415 int nodesize_forced = 0;
1416 int data_profile_opt = 0;
1417 int metadata_profile_opt = 0;
1420 int force_overwrite = 0;
1421 char *source_dir = NULL;
1422 int source_dir_set = 0;
1423 u64 num_of_meta_chunks = 0;
1424 u64 size_of_data = 0;
1425 u64 source_dir_size = 0;
1428 char fs_uuid[BTRFS_UUID_UNPARSED_SIZE] = { 0 };
1429 u64 features = BTRFS_MKFS_DEFAULT_FEATURES;
1430 struct mkfs_allocation allocation = { 0 };
1431 struct btrfs_mkfs_config mkfs_cfg;
1435 static const struct option long_options[] = {
1436 { "alloc-start", required_argument, NULL, 'A'},
1437 { "byte-count", required_argument, NULL, 'b' },
1438 { "force", no_argument, NULL, 'f' },
1439 { "leafsize", required_argument, NULL, 'l' },
1440 { "label", required_argument, NULL, 'L'},
1441 { "metadata", required_argument, NULL, 'm' },
1442 { "mixed", no_argument, NULL, 'M' },
1443 { "nodesize", required_argument, NULL, 'n' },
1444 { "sectorsize", required_argument, NULL, 's' },
1445 { "data", required_argument, NULL, 'd' },
1446 { "version", no_argument, NULL, 'V' },
1447 { "rootdir", required_argument, NULL, 'r' },
1448 { "nodiscard", no_argument, NULL, 'K' },
1449 { "features", required_argument, NULL, 'O' },
1450 { "uuid", required_argument, NULL, 'U' },
1451 { "quiet", 0, NULL, 'q' },
1452 { "help", no_argument, NULL, GETOPT_VAL_HELP },
1456 c = getopt_long(argc, argv, "A:b:fl:n:s:m:d:L:O:r:U:VMKq",
1457 long_options, NULL);
1462 alloc_start = parse_size(optarg);
1465 force_overwrite = 1;
1468 data_profile = parse_profile(optarg);
1469 data_profile_opt = 1;
1472 warning("--leafsize is deprecated, use --nodesize");
1475 nodesize = parse_size(optarg);
1476 nodesize_forced = 1;
1479 label = parse_label(optarg);
1482 metadata_profile = parse_profile(optarg);
1483 metadata_profile_opt = 1;
1489 char *orig = strdup(optarg);
1492 tmp = btrfs_parse_fs_features(tmp, &features);
1494 error("unrecognized filesystem feature '%s'",
1500 if (features & BTRFS_FEATURE_LIST_ALL) {
1501 btrfs_list_all_fs_features(0);
1507 sectorsize = parse_size(optarg);
1510 block_count = parse_size(optarg);
1514 printf("mkfs.btrfs, part of %s\n",
1519 source_dir = optarg;
1523 strncpy(fs_uuid, optarg,
1524 BTRFS_UUID_UNPARSED_SIZE - 1);
1532 case GETOPT_VAL_HELP:
1534 print_usage(c != GETOPT_VAL_HELP);
1539 printf("%s\n", PACKAGE_STRING);
1540 printf("See %s for more information.\n\n", PACKAGE_URL);
1543 sectorsize = max(sectorsize, (u32)sysconf(_SC_PAGESIZE));
1544 stripesize = sectorsize;
1545 saved_optind = optind;
1546 dev_cnt = argc - optind;
1550 if (source_dir_set && dev_cnt > 1) {
1551 error("the option -r is limited to a single device");
1558 if (uuid_parse(fs_uuid, dummy_uuid) != 0) {
1559 error("could not parse UUID: %s", fs_uuid);
1562 if (!test_uuid_unique(fs_uuid)) {
1563 error("non-unique UUID: %s", fs_uuid);
1568 while (dev_cnt-- > 0) {
1569 file = argv[optind++];
1570 if (is_block_device(file) == 1)
1571 if (test_dev_for_mkfs(file, force_overwrite))
1575 optind = saved_optind;
1576 dev_cnt = argc - optind;
1578 file = argv[optind++];
1582 * Set default profiles according to number of added devices.
1583 * For mixed groups defaults are single/single.
1586 if (!metadata_profile_opt) {
1587 if (dev_cnt == 1 && ssd && verbose)
1588 printf("Detected a SSD, turning off metadata "
1589 "duplication. Mkfs with -m dup if you want to "
1590 "force metadata duplication.\n");
1592 metadata_profile = (dev_cnt > 1) ?
1593 BTRFS_BLOCK_GROUP_RAID1 : (ssd) ?
1594 0: BTRFS_BLOCK_GROUP_DUP;
1596 if (!data_profile_opt) {
1597 data_profile = (dev_cnt > 1) ?
1598 BTRFS_BLOCK_GROUP_RAID0 : 0; /* raid0 or single */
1601 u32 best_nodesize = max_t(u32, sysconf(_SC_PAGESIZE), sectorsize);
1603 if (metadata_profile_opt || data_profile_opt) {
1604 if (metadata_profile != data_profile) {
1606 "with mixed block groups data and metadata profiles must be the same");
1611 if (!nodesize_forced)
1612 nodesize = best_nodesize;
1616 * FS features that can be set by other means than -O
1617 * just set the bit here
1620 features |= BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS;
1622 if ((data_profile | metadata_profile) &
1623 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
1624 features |= BTRFS_FEATURE_INCOMPAT_RAID56;
1627 if (btrfs_check_nodesize(nodesize, sectorsize,
1631 if (sectorsize < sizeof(struct btrfs_super_block)) {
1632 error("sectorsize smaller than superblock: %u < %zu",
1633 sectorsize, sizeof(struct btrfs_super_block));
1637 /* Check device/block_count after the nodesize is determined */
1638 if (block_count && block_count < btrfs_min_dev_size(nodesize)) {
1639 error("size %llu is too small to make a usable filesystem",
1641 error("minimum size for btrfs filesystem is %llu",
1642 btrfs_min_dev_size(nodesize));
1645 for (i = saved_optind; i < saved_optind + dev_cnt; i++) {
1649 ret = test_minimum_size(path, nodesize);
1651 error("failed to check size for %s: %s",
1652 path, strerror(-ret));
1656 error("'%s' is too small to make a usable filesystem",
1658 error("minimum size for each btrfs device is %llu",
1659 btrfs_min_dev_size(nodesize));
1663 ret = test_num_disk_vs_raid(metadata_profile, data_profile,
1664 dev_cnt, mixed, ssd);
1670 if (!source_dir_set) {
1672 * open without O_EXCL so that the problem should not
1673 * occur by the following processing.
1674 * (btrfs_register_one_device() fails if O_EXCL is on)
1676 fd = open(file, O_RDWR);
1678 error("unable to open %s: %s", file, strerror(errno));
1681 ret = btrfs_prepare_device(fd, file, &dev_block_count,
1683 (zero_end ? PREP_DEVICE_ZERO_END : 0) |
1684 (discard ? PREP_DEVICE_DISCARD : 0) |
1685 (verbose ? PREP_DEVICE_VERBOSE : 0));
1690 if (block_count && block_count > dev_block_count) {
1691 error("%s is smaller than requested size, expected %llu, found %llu",
1693 (unsigned long long)block_count,
1694 (unsigned long long)dev_block_count);
1698 fd = open(file, O_CREAT | O_RDWR,
1699 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
1701 error("unable to open %s: %s", file, strerror(errno));
1705 source_dir_size = size_sourcedir(source_dir, sectorsize,
1706 &num_of_meta_chunks, &size_of_data);
1707 if(block_count < source_dir_size)
1708 block_count = source_dir_size;
1709 ret = zero_output_file(fd, block_count);
1711 error("unable to zero the output file");
1714 /* our "device" is the new image file */
1715 dev_block_count = block_count;
1718 /* To create the first block group and chunk 0 in make_btrfs */
1719 if (dev_block_count < BTRFS_MKFS_SYSTEM_GROUP_SIZE) {
1720 error("device is too small to make filesystem, must be at least %llu",
1721 (unsigned long long)BTRFS_MKFS_SYSTEM_GROUP_SIZE);
1725 if (group_profile_max_safe_loss(metadata_profile) <
1726 group_profile_max_safe_loss(data_profile)){
1727 warning("metadata has lower redundancy than data!\n");
1730 mkfs_cfg.label = label;
1731 memcpy(mkfs_cfg.fs_uuid, fs_uuid, sizeof(mkfs_cfg.fs_uuid));
1732 mkfs_cfg.num_bytes = dev_block_count;
1733 mkfs_cfg.nodesize = nodesize;
1734 mkfs_cfg.sectorsize = sectorsize;
1735 mkfs_cfg.stripesize = stripesize;
1736 mkfs_cfg.features = features;
1738 ret = make_btrfs(fd, &mkfs_cfg);
1740 error("error during mkfs: %s", strerror(-ret));
1744 fs_info = open_ctree_fs_info(file, 0, 0, 0,
1745 OPEN_CTREE_WRITES | OPEN_CTREE_FS_PARTIAL);
1747 error("open ctree failed");
1751 root = fs_info->fs_root;
1752 fs_info->alloc_start = alloc_start;
1754 ret = create_metadata_block_groups(root, mixed, &allocation);
1756 error("failed to create default block groups: %d", ret);
1760 trans = btrfs_start_transaction(root, 1);
1762 error("failed to start transaction");
1766 ret = create_data_block_groups(trans, root, mixed, &allocation);
1768 error("failed to create default data block groups: %d", ret);
1772 ret = make_root_dir(trans, root, &allocation);
1774 error("failed to setup the root directory: %d", ret);
1778 ret = btrfs_commit_transaction(trans, root);
1780 error("unable to commit transaction: %d", ret);
1784 trans = btrfs_start_transaction(root, 1);
1786 error("failed to start transaction");
1793 while (dev_cnt-- > 0) {
1794 file = argv[optind++];
1797 * open without O_EXCL so that the problem should not
1798 * occur by the following processing.
1799 * (btrfs_register_one_device() fails if O_EXCL is on)
1801 fd = open(file, O_RDWR);
1803 error("unable to open %s: %s", file, strerror(errno));
1806 ret = btrfs_device_already_in_root(root, fd,
1807 BTRFS_SUPER_INFO_OFFSET);
1809 error("skipping duplicate device %s in the filesystem",
1814 ret = btrfs_prepare_device(fd, file, &dev_block_count,
1816 (verbose ? PREP_DEVICE_VERBOSE : 0) |
1817 (zero_end ? PREP_DEVICE_ZERO_END : 0) |
1818 (discard ? PREP_DEVICE_DISCARD : 0));
1824 ret = btrfs_add_to_fsid(trans, root, fd, file, dev_block_count,
1825 sectorsize, sectorsize, sectorsize);
1827 error("unable to add %s to filesystem: %d", file, ret);
1831 struct btrfs_device *device;
1833 device = container_of(fs_info->fs_devices->devices.next,
1834 struct btrfs_device, dev_list);
1835 printf("adding device %s id %llu\n", file,
1836 (unsigned long long)device->devid);
1841 if (!source_dir_set) {
1842 ret = create_raid_groups(trans, root, data_profile,
1843 metadata_profile, mixed, &allocation);
1845 error("unable to create raid groups: %d", ret);
1850 ret = create_data_reloc_tree(trans, root);
1852 error("unable to create data reloc tree: %d", ret);
1856 ret = btrfs_commit_transaction(trans, root);
1858 error("unable to commit transaction: %d", ret);
1862 if (source_dir_set) {
1863 trans = btrfs_start_transaction(root, 1);
1864 ret = create_chunks(trans, root,
1865 num_of_meta_chunks, size_of_data,
1868 error("unable to create chunks: %d", ret);
1871 ret = btrfs_commit_transaction(trans, root);
1873 error("transaction commit failed: %d", ret);
1877 ret = make_image(source_dir, root, fd);
1879 error("error wihle filling filesystem: %d", ret);
1883 ret = cleanup_temp_chunks(fs_info, &allocation, data_profile,
1884 metadata_profile, metadata_profile);
1886 error("failed to cleanup temporary chunks: %d", ret);
1891 char features_buf[64];
1893 printf("Label: %s\n", label);
1894 printf("UUID: %s\n", mkfs_cfg.fs_uuid);
1895 printf("Node size: %u\n", nodesize);
1896 printf("Sector size: %u\n", sectorsize);
1897 printf("Filesystem size: %s\n",
1898 pretty_size(btrfs_super_total_bytes(fs_info->super_copy)));
1899 printf("Block group profiles:\n");
1900 if (allocation.data)
1901 printf(" Data: %-8s %16s\n",
1902 btrfs_group_profile_str(data_profile),
1903 pretty_size(allocation.data));
1904 if (allocation.metadata)
1905 printf(" Metadata: %-8s %16s\n",
1906 btrfs_group_profile_str(metadata_profile),
1907 pretty_size(allocation.metadata));
1908 if (allocation.mixed)
1909 printf(" Data+Metadata: %-8s %16s\n",
1910 btrfs_group_profile_str(data_profile),
1911 pretty_size(allocation.mixed));
1912 printf(" System: %-8s %16s\n",
1913 btrfs_group_profile_str(metadata_profile),
1914 pretty_size(allocation.system));
1915 printf("SSD detected: %s\n", ssd ? "yes" : "no");
1916 btrfs_parse_features_to_string(features_buf, features);
1917 printf("Incompat features: %s", features_buf);
1920 list_all_devices(root);
1924 * The filesystem is now fully set up, commit the remaining changes and
1925 * fix the signature as the last step before closing the devices.
1927 fs_info->finalize_on_close = 1;
1929 ret = close_ctree(root);
1932 optind = saved_optind;
1933 dev_cnt = argc - optind;
1934 while (dev_cnt-- > 0) {
1935 file = argv[optind++];
1936 if (is_block_device(file) == 1)
1937 btrfs_register_one_device(file);
1941 btrfs_close_all_devices();