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 int path_cat_out(char *out, const char *p1, const char *p2);
52 static u64 index_cnt = 2;
53 static int verbose = 1;
55 struct directory_name_entry {
59 struct list_head list;
62 struct mkfs_allocation {
69 static int create_metadata_block_groups(struct btrfs_root *root, int mixed,
70 struct mkfs_allocation *allocation)
72 struct btrfs_fs_info *fs_info = root->fs_info;
73 struct btrfs_trans_handle *trans;
79 trans = btrfs_start_transaction(root, 1);
80 BUG_ON(IS_ERR(trans));
81 bytes_used = btrfs_super_bytes_used(fs_info->super_copy);
83 root->fs_info->system_allocs = 1;
84 ret = btrfs_make_block_group(trans, fs_info, bytes_used,
85 BTRFS_BLOCK_GROUP_SYSTEM,
86 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
87 0, BTRFS_MKFS_SYSTEM_GROUP_SIZE);
88 allocation->system += BTRFS_MKFS_SYSTEM_GROUP_SIZE;
93 ret = btrfs_alloc_chunk(trans, fs_info,
94 &chunk_start, &chunk_size,
95 BTRFS_BLOCK_GROUP_METADATA |
96 BTRFS_BLOCK_GROUP_DATA);
98 error("no space to allocate data/metadata chunk");
103 ret = btrfs_make_block_group(trans, fs_info, 0,
104 BTRFS_BLOCK_GROUP_METADATA |
105 BTRFS_BLOCK_GROUP_DATA,
106 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
107 chunk_start, chunk_size);
110 allocation->mixed += chunk_size;
112 ret = btrfs_alloc_chunk(trans, fs_info,
113 &chunk_start, &chunk_size,
114 BTRFS_BLOCK_GROUP_METADATA);
115 if (ret == -ENOSPC) {
116 error("no space to allocate metadata chunk");
121 ret = btrfs_make_block_group(trans, fs_info, 0,
122 BTRFS_BLOCK_GROUP_METADATA,
123 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
124 chunk_start, chunk_size);
125 allocation->metadata += chunk_size;
130 root->fs_info->system_allocs = 0;
131 ret = btrfs_commit_transaction(trans, root);
137 static int create_data_block_groups(struct btrfs_trans_handle *trans,
138 struct btrfs_root *root, int mixed,
139 struct mkfs_allocation *allocation)
141 struct btrfs_fs_info *fs_info = root->fs_info;
147 ret = btrfs_alloc_chunk(trans, fs_info,
148 &chunk_start, &chunk_size,
149 BTRFS_BLOCK_GROUP_DATA);
150 if (ret == -ENOSPC) {
151 error("no space to allocate data chunk");
156 ret = btrfs_make_block_group(trans, fs_info, 0,
157 BTRFS_BLOCK_GROUP_DATA,
158 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
159 chunk_start, chunk_size);
160 allocation->data += chunk_size;
169 static int make_root_dir(struct btrfs_trans_handle *trans,
170 struct btrfs_root *root)
172 struct btrfs_key location;
175 ret = btrfs_make_root_dir(trans, root->fs_info->tree_root,
176 BTRFS_ROOT_TREE_DIR_OBJECTID);
179 ret = btrfs_make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID);
182 memcpy(&location, &root->fs_info->fs_root->root_key, sizeof(location));
183 location.offset = (u64)-1;
184 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
186 btrfs_super_root_dir(root->fs_info->super_copy),
187 &location, BTRFS_FT_DIR, 0);
191 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
192 "default", 7, location.objectid,
193 BTRFS_ROOT_TREE_DIR_OBJECTID, 0);
201 static int __recow_root(struct btrfs_trans_handle *trans,
202 struct btrfs_root *root)
204 struct extent_buffer *tmp;
207 if (trans->transid != btrfs_root_generation(&root->root_item)) {
208 extent_buffer_get(root->node);
209 ret = __btrfs_cow_block(trans, root, root->node,
210 NULL, 0, &tmp, 0, 0);
213 free_extent_buffer(tmp);
219 static int recow_roots(struct btrfs_trans_handle *trans,
220 struct btrfs_root *root)
222 struct btrfs_fs_info *info = root->fs_info;
225 ret = __recow_root(trans, info->fs_root);
228 ret = __recow_root(trans, info->tree_root);
231 ret = __recow_root(trans, info->extent_root);
234 ret = __recow_root(trans, info->chunk_root);
237 ret = __recow_root(trans, info->dev_root);
240 ret = __recow_root(trans, info->csum_root);
247 static int create_one_raid_group(struct btrfs_trans_handle *trans,
248 struct btrfs_root *root, u64 type,
249 struct mkfs_allocation *allocation)
252 struct btrfs_fs_info *fs_info = root->fs_info;
257 ret = btrfs_alloc_chunk(trans, fs_info,
258 &chunk_start, &chunk_size, type);
259 if (ret == -ENOSPC) {
260 error("not enough free space to allocate chunk");
266 ret = btrfs_make_block_group(trans, fs_info, 0,
267 type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
268 chunk_start, chunk_size);
270 type &= BTRFS_BLOCK_GROUP_TYPE_MASK;
271 if (type == BTRFS_BLOCK_GROUP_DATA) {
272 allocation->data += chunk_size;
273 } else if (type == BTRFS_BLOCK_GROUP_METADATA) {
274 allocation->metadata += chunk_size;
275 } else if (type == BTRFS_BLOCK_GROUP_SYSTEM) {
276 allocation->system += chunk_size;
278 (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA)) {
279 allocation->mixed += chunk_size;
281 error("unrecognized profile type: 0x%llx",
282 (unsigned long long)type);
289 static int create_raid_groups(struct btrfs_trans_handle *trans,
290 struct btrfs_root *root, u64 data_profile,
291 u64 metadata_profile, int mixed,
292 struct mkfs_allocation *allocation)
296 if (metadata_profile) {
297 u64 meta_flags = BTRFS_BLOCK_GROUP_METADATA;
299 ret = create_one_raid_group(trans, root,
300 BTRFS_BLOCK_GROUP_SYSTEM |
301 metadata_profile, allocation);
306 meta_flags |= BTRFS_BLOCK_GROUP_DATA;
308 ret = create_one_raid_group(trans, root, meta_flags |
309 metadata_profile, allocation);
314 if (!mixed && data_profile) {
315 ret = create_one_raid_group(trans, root,
316 BTRFS_BLOCK_GROUP_DATA |
317 data_profile, allocation);
321 ret = recow_roots(trans, root);
326 static int create_tree(struct btrfs_trans_handle *trans,
327 struct btrfs_root *root, u64 objectid)
329 struct btrfs_key location;
330 struct btrfs_root_item root_item;
331 struct extent_buffer *tmp;
334 ret = btrfs_copy_root(trans, root, root->node, &tmp, objectid);
338 memcpy(&root_item, &root->root_item, sizeof(root_item));
339 btrfs_set_root_bytenr(&root_item, tmp->start);
340 btrfs_set_root_level(&root_item, btrfs_header_level(tmp));
341 btrfs_set_root_generation(&root_item, trans->transid);
342 free_extent_buffer(tmp);
344 location.objectid = objectid;
345 location.type = BTRFS_ROOT_ITEM_KEY;
347 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
348 &location, &root_item);
353 static void print_usage(int ret)
355 printf("Usage: mkfs.btrfs [options] dev [ dev ... ]\n");
356 printf("Options:\n");
357 printf(" allocation profiles:\n");
358 printf("\t-d|--data PROFILE data profile, raid0, raid1, raid5, raid6, raid10, dup or single\n");
359 printf("\t-m|--metadata PROFILE metadata profile, values like for data profile\n");
360 printf("\t-M|--mixed mix metadata and data together\n");
361 printf(" features:\n");
362 printf("\t-n|--nodesize SIZE size of btree nodes\n");
363 printf("\t-s|--sectorsize SIZE data block size (may not be mountable by current kernel)\n");
364 printf("\t-O|--features LIST comma separated list of filesystem features (use '-O list-all' to list features)\n");
365 printf("\t-L|--label LABEL set the filesystem label\n");
366 printf("\t-U|--uuid UUID specify the filesystem UUID (must be unique)\n");
367 printf(" creation:\n");
368 printf("\t-b|--byte-count SIZE set filesystem size to SIZE (on the first device)\n");
369 printf("\t-r|--rootdir DIR copy files from DIR to the image root directory\n");
370 printf("\t-K|--nodiscard do not perform whole device TRIM\n");
371 printf("\t-f|--force force overwrite of existing filesystem\n");
372 printf(" general:\n");
373 printf("\t-q|--quiet no messages except errors\n");
374 printf("\t-V|--version print the mkfs.btrfs version and exit\n");
375 printf("\t--help print this help and exit\n");
376 printf(" deprecated:\n");
377 printf("\t-A|--alloc-start START the offset to start the filesystem\n");
378 printf("\t-l|--leafsize SIZE deprecated, alias for nodesize\n");
382 static u64 parse_profile(const char *s)
384 if (strcasecmp(s, "raid0") == 0) {
385 return BTRFS_BLOCK_GROUP_RAID0;
386 } else if (strcasecmp(s, "raid1") == 0) {
387 return BTRFS_BLOCK_GROUP_RAID1;
388 } else if (strcasecmp(s, "raid5") == 0) {
389 return BTRFS_BLOCK_GROUP_RAID5;
390 } else if (strcasecmp(s, "raid6") == 0) {
391 return BTRFS_BLOCK_GROUP_RAID6;
392 } else if (strcasecmp(s, "raid10") == 0) {
393 return BTRFS_BLOCK_GROUP_RAID10;
394 } else if (strcasecmp(s, "dup") == 0) {
395 return BTRFS_BLOCK_GROUP_DUP;
396 } else if (strcasecmp(s, "single") == 0) {
399 error("unknown profile %s", s);
406 static char *parse_label(const char *input)
408 int len = strlen(input);
410 if (len >= BTRFS_LABEL_SIZE) {
411 error("label %s is too long (max %d)", input,
412 BTRFS_LABEL_SIZE - 1);
415 return strdup(input);
418 static int add_directory_items(struct btrfs_trans_handle *trans,
419 struct btrfs_root *root, u64 objectid,
420 ino_t parent_inum, const char *name,
421 struct stat *st, int *dir_index_cnt)
425 struct btrfs_key location;
428 name_len = strlen(name);
430 location.objectid = objectid;
432 location.type = BTRFS_INODE_ITEM_KEY;
434 if (S_ISDIR(st->st_mode))
435 filetype = BTRFS_FT_DIR;
436 if (S_ISREG(st->st_mode))
437 filetype = BTRFS_FT_REG_FILE;
438 if (S_ISLNK(st->st_mode))
439 filetype = BTRFS_FT_SYMLINK;
440 if (S_ISSOCK(st->st_mode))
441 filetype = BTRFS_FT_SOCK;
442 if (S_ISCHR(st->st_mode))
443 filetype = BTRFS_FT_CHRDEV;
444 if (S_ISBLK(st->st_mode))
445 filetype = BTRFS_FT_BLKDEV;
446 if (S_ISFIFO(st->st_mode))
447 filetype = BTRFS_FT_FIFO;
449 ret = btrfs_insert_dir_item(trans, root, name, name_len,
450 parent_inum, &location,
451 filetype, index_cnt);
454 ret = btrfs_insert_inode_ref(trans, root, name, name_len,
455 objectid, parent_inum, index_cnt);
456 *dir_index_cnt = index_cnt;
462 static int fill_inode_item(struct btrfs_trans_handle *trans,
463 struct btrfs_root *root,
464 struct btrfs_inode_item *dst, struct stat *src)
467 u64 sectorsize = root->fs_info->sectorsize;
470 * btrfs_inode_item has some reserved fields
471 * and represents on-disk inode entry, so
472 * zero everything to prevent information leak
474 memset(dst, 0, sizeof (*dst));
476 btrfs_set_stack_inode_generation(dst, trans->transid);
477 btrfs_set_stack_inode_size(dst, src->st_size);
478 btrfs_set_stack_inode_nbytes(dst, 0);
479 btrfs_set_stack_inode_block_group(dst, 0);
480 btrfs_set_stack_inode_nlink(dst, src->st_nlink);
481 btrfs_set_stack_inode_uid(dst, src->st_uid);
482 btrfs_set_stack_inode_gid(dst, src->st_gid);
483 btrfs_set_stack_inode_mode(dst, src->st_mode);
484 btrfs_set_stack_inode_rdev(dst, 0);
485 btrfs_set_stack_inode_flags(dst, 0);
486 btrfs_set_stack_timespec_sec(&dst->atime, src->st_atime);
487 btrfs_set_stack_timespec_nsec(&dst->atime, 0);
488 btrfs_set_stack_timespec_sec(&dst->ctime, src->st_ctime);
489 btrfs_set_stack_timespec_nsec(&dst->ctime, 0);
490 btrfs_set_stack_timespec_sec(&dst->mtime, src->st_mtime);
491 btrfs_set_stack_timespec_nsec(&dst->mtime, 0);
492 btrfs_set_stack_timespec_sec(&dst->otime, 0);
493 btrfs_set_stack_timespec_nsec(&dst->otime, 0);
495 if (S_ISDIR(src->st_mode)) {
496 btrfs_set_stack_inode_size(dst, 0);
497 btrfs_set_stack_inode_nlink(dst, 1);
499 if (S_ISREG(src->st_mode)) {
500 btrfs_set_stack_inode_size(dst, (u64)src->st_size);
501 if (src->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root))
502 btrfs_set_stack_inode_nbytes(dst, src->st_size);
504 blocks = src->st_size / sectorsize;
505 if (src->st_size % sectorsize)
507 blocks *= sectorsize;
508 btrfs_set_stack_inode_nbytes(dst, blocks);
511 if (S_ISLNK(src->st_mode))
512 btrfs_set_stack_inode_nbytes(dst, src->st_size + 1);
517 static int directory_select(const struct direct *entry)
519 if (entry->d_name[0] == '.' &&
520 (entry->d_name[1] == 0 ||
521 (entry->d_name[1] == '.' && entry->d_name[2] == 0)))
526 static void free_namelist(struct direct **files, int count)
533 for (i = 0; i < count; ++i)
538 static u64 calculate_dir_inode_size(const char *dirname)
541 struct direct **files, *cur_file;
542 u64 dir_inode_size = 0;
544 count = scandir(dirname, &files, directory_select, NULL);
546 for (i = 0; i < count; i++) {
548 dir_inode_size += strlen(cur_file->d_name);
551 free_namelist(files, count);
554 return dir_inode_size;
557 static int add_inode_items(struct btrfs_trans_handle *trans,
558 struct btrfs_root *root,
559 struct stat *st, const char *name,
561 struct btrfs_inode_item *inode_ret)
564 struct btrfs_inode_item btrfs_inode;
568 fill_inode_item(trans, root, &btrfs_inode, st);
569 objectid = self_objectid;
571 if (S_ISDIR(st->st_mode)) {
572 inode_size = calculate_dir_inode_size(name);
573 btrfs_set_stack_inode_size(&btrfs_inode, inode_size);
576 ret = btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
578 *inode_ret = btrfs_inode;
582 static int add_xattr_item(struct btrfs_trans_handle *trans,
583 struct btrfs_root *root, u64 objectid,
584 const char *file_name)
588 char xattr_list[XATTR_LIST_MAX];
590 char cur_value[XATTR_SIZE_MAX];
591 char delimiter = '\0';
592 char *next_location = xattr_list;
594 ret = llistxattr(file_name, xattr_list, XATTR_LIST_MAX);
598 error("getting a list of xattr failed for %s: %s", file_name,
605 cur_name = strtok(xattr_list, &delimiter);
606 while (cur_name != NULL) {
607 cur_name_len = strlen(cur_name);
608 next_location += cur_name_len + 1;
610 ret = getxattr(file_name, cur_name, cur_value, XATTR_SIZE_MAX);
614 error("gettig a xattr value failed for %s attr %s: %s",
615 file_name, cur_name, strerror(errno));
619 ret = btrfs_insert_xattr_item(trans, root, cur_name,
620 cur_name_len, cur_value,
623 error("inserting a xattr item failed for %s: %s",
624 file_name, strerror(-ret));
627 cur_name = strtok(next_location, &delimiter);
633 static int add_symbolic_link(struct btrfs_trans_handle *trans,
634 struct btrfs_root *root,
635 u64 objectid, const char *path_name)
640 ret = readlink(path_name, buf, sizeof(buf));
642 error("readlink failed for %s: %s", path_name, strerror(errno));
645 if (ret >= sizeof(buf)) {
646 error("symlink too long for %s", path_name);
651 buf[ret] = '\0'; /* readlink does not do it for us */
652 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
658 static int add_file_items(struct btrfs_trans_handle *trans,
659 struct btrfs_root *root,
660 struct btrfs_inode_item *btrfs_inode, u64 objectid,
661 struct stat *st, const char *path_name)
666 struct btrfs_key key;
668 u32 sectorsize = root->fs_info->sectorsize;
673 struct extent_buffer *eb = NULL;
676 if (st->st_size == 0)
679 fd = open(path_name, O_RDONLY);
681 error("cannot open %s: %s", path_name, strerror(errno));
685 blocks = st->st_size / sectorsize;
686 if (st->st_size % sectorsize)
689 if (st->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root)) {
690 char *buffer = malloc(st->st_size);
697 ret_read = pread64(fd, buffer, st->st_size, bytes_read);
698 if (ret_read == -1) {
699 error("cannot read %s at offset %llu length %llu: %s",
700 path_name, (unsigned long long)bytes_read,
701 (unsigned long long)st->st_size,
707 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
708 buffer, st->st_size);
713 /* round up our st_size to the FS blocksize */
714 total_bytes = (u64)blocks * sectorsize;
717 * do our IO in extent buffers so it can work
718 * against any raid type
720 eb = calloc(1, sizeof(*eb) + sectorsize);
729 * keep our extent size at 1MB max, this makes it easier to work inside
730 * the tiny block groups created during mkfs
732 cur_bytes = min(total_bytes, (u64)SZ_1M);
733 ret = btrfs_reserve_extent(trans, root, cur_bytes, 0, 0, (u64)-1,
738 first_block = key.objectid;
741 while (bytes_read < cur_bytes) {
743 memset(eb->data, 0, sectorsize);
745 ret_read = pread64(fd, eb->data, sectorsize, file_pos + bytes_read);
746 if (ret_read == -1) {
747 error("cannot read %s at offset %llu length %llu: %s",
749 (unsigned long long)file_pos + bytes_read,
750 (unsigned long long)sectorsize,
755 eb->start = first_block + bytes_read;
756 eb->len = sectorsize;
759 * we're doing the csum before we record the extent, but
762 ret = btrfs_csum_file_block(trans, root->fs_info->csum_root,
763 first_block + bytes_read + sectorsize,
764 first_block + bytes_read,
765 eb->data, sectorsize);
769 ret = write_and_map_eb(root->fs_info, eb);
771 error("failed to write %s", path_name);
775 bytes_read += sectorsize;
779 ret = btrfs_record_file_extent(trans, root, objectid, btrfs_inode,
780 file_pos, first_block, cur_bytes);
786 file_pos += cur_bytes;
787 total_bytes -= cur_bytes;
798 static char *make_path(const char *dir, const char *name)
802 path = malloc(strlen(dir) + strlen(name) + 2);
806 if (dir[strlen(dir) - 1] != '/')
812 static int traverse_directory(struct btrfs_trans_handle *trans,
813 struct btrfs_root *root, const char *dir_name,
814 struct directory_name_entry *dir_head)
818 struct btrfs_inode_item cur_inode;
819 struct btrfs_inode_item *inode_item;
820 int count, i, dir_index_cnt;
821 struct direct **files;
823 struct directory_name_entry *dir_entry, *parent_dir_entry;
824 struct direct *cur_file;
825 ino_t parent_inum, cur_inum;
826 ino_t highest_inum = 0;
827 const char *parent_dir_name;
828 char real_path[PATH_MAX];
829 struct btrfs_path path;
830 struct extent_buffer *leaf;
831 struct btrfs_key root_dir_key;
832 u64 root_dir_inode_size = 0;
834 /* Add list for source directory */
835 dir_entry = malloc(sizeof(struct directory_name_entry));
838 dir_entry->dir_name = dir_name;
839 dir_entry->path = realpath(dir_name, real_path);
840 if (!dir_entry->path) {
841 error("realpath failed for %s: %s", dir_name, strerror(errno));
846 parent_inum = highest_inum + BTRFS_FIRST_FREE_OBJECTID;
847 dir_entry->inum = parent_inum;
848 list_add_tail(&dir_entry->list, &dir_head->list);
850 btrfs_init_path(&path);
852 root_dir_key.objectid = btrfs_root_dirid(&root->root_item);
853 root_dir_key.offset = 0;
854 root_dir_key.type = BTRFS_INODE_ITEM_KEY;
855 ret = btrfs_lookup_inode(trans, root, &path, &root_dir_key, 1);
857 error("failed to lookup root dir: %d", ret);
861 leaf = path.nodes[0];
862 inode_item = btrfs_item_ptr(leaf, path.slots[0],
863 struct btrfs_inode_item);
865 root_dir_inode_size = calculate_dir_inode_size(dir_name);
866 btrfs_set_inode_size(leaf, inode_item, root_dir_inode_size);
867 btrfs_mark_buffer_dirty(leaf);
869 btrfs_release_path(&path);
872 parent_dir_entry = list_entry(dir_head->list.next,
873 struct directory_name_entry,
875 list_del(&parent_dir_entry->list);
877 parent_inum = parent_dir_entry->inum;
878 parent_dir_name = parent_dir_entry->dir_name;
879 if (chdir(parent_dir_entry->path)) {
880 error("chdir failed for %s: %s",
881 parent_dir_name, strerror(errno));
886 count = scandir(parent_dir_entry->path, &files,
887 directory_select, NULL);
890 error("scandir failed for %s: %s",
891 parent_dir_name, strerror (errno));
896 for (i = 0; i < count; i++) {
899 if (lstat(cur_file->d_name, &st) == -1) {
900 error("lstat failed for %s: %s",
901 cur_file->d_name, strerror(errno));
906 cur_inum = st.st_ino;
907 ret = add_directory_items(trans, root,
908 cur_inum, parent_inum,
910 &st, &dir_index_cnt);
912 error("unable to add directory items for %s: %d",
913 cur_file->d_name, ret);
917 ret = add_inode_items(trans, root, &st,
918 cur_file->d_name, cur_inum,
920 if (ret == -EEXIST) {
921 if (st.st_nlink <= 1) {
923 "item %s already exists but has wrong st_nlink %lu <= 1",
925 (unsigned long)st.st_nlink);
931 error("unable to add inode items for %s: %d",
932 cur_file->d_name, ret);
936 ret = add_xattr_item(trans, root,
937 cur_inum, cur_file->d_name);
939 error("unable to add xattr items for %s: %d",
940 cur_file->d_name, ret);
945 if (S_ISDIR(st.st_mode)) {
948 dir_entry = malloc(sizeof(struct directory_name_entry));
953 dir_entry->dir_name = cur_file->d_name;
954 if (path_cat_out(tmp, parent_dir_entry->path,
956 error("invalid path: %s/%s",
957 parent_dir_entry->path,
962 dir_entry->path = strdup(tmp);
963 if (!dir_entry->path) {
964 error("not enough memory to store path");
968 dir_entry->inum = cur_inum;
969 list_add_tail(&dir_entry->list, &dir_head->list);
970 } else if (S_ISREG(st.st_mode)) {
971 ret = add_file_items(trans, root, &cur_inode,
975 error("unable to add file items for %s: %d",
976 cur_file->d_name, ret);
979 } else if (S_ISLNK(st.st_mode)) {
980 ret = add_symbolic_link(trans, root,
981 cur_inum, cur_file->d_name);
983 error("unable to add symlink for %s: %d",
984 cur_file->d_name, ret);
990 free_namelist(files, count);
991 free(parent_dir_entry);
995 } while (!list_empty(&dir_head->list));
1000 free_namelist(files, count);
1002 free(parent_dir_entry);
1009 static int create_chunks(struct btrfs_trans_handle *trans,
1010 struct btrfs_root *root, u64 num_of_meta_chunks,
1012 struct mkfs_allocation *allocation)
1014 struct btrfs_fs_info *fs_info = root->fs_info;
1017 u64 meta_type = BTRFS_BLOCK_GROUP_METADATA;
1018 u64 data_type = BTRFS_BLOCK_GROUP_DATA;
1019 u64 minimum_data_chunk_size = SZ_8M;
1023 for (i = 0; i < num_of_meta_chunks; i++) {
1024 ret = btrfs_alloc_chunk(trans, fs_info,
1025 &chunk_start, &chunk_size, meta_type);
1028 ret = btrfs_make_block_group(trans, fs_info, 0,
1029 meta_type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
1030 chunk_start, chunk_size);
1031 allocation->metadata += chunk_size;
1034 set_extent_dirty(&root->fs_info->free_space_cache,
1035 chunk_start, chunk_start + chunk_size - 1);
1038 if (size_of_data < minimum_data_chunk_size)
1039 size_of_data = minimum_data_chunk_size;
1041 ret = btrfs_alloc_data_chunk(trans, fs_info,
1042 &chunk_start, size_of_data, data_type, 0);
1045 ret = btrfs_make_block_group(trans, fs_info, 0,
1046 data_type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
1047 chunk_start, size_of_data);
1048 allocation->data += size_of_data;
1051 set_extent_dirty(&root->fs_info->free_space_cache,
1052 chunk_start, chunk_start + size_of_data - 1);
1056 static int make_image(const char *source_dir, struct btrfs_root *root)
1059 struct btrfs_trans_handle *trans;
1060 struct stat root_st;
1061 struct directory_name_entry dir_head;
1062 struct directory_name_entry *dir_entry = NULL;
1064 ret = lstat(source_dir, &root_st);
1066 error("unable to lstat %s: %s", source_dir, strerror(errno));
1071 INIT_LIST_HEAD(&dir_head.list);
1073 trans = btrfs_start_transaction(root, 1);
1074 BUG_ON(IS_ERR(trans));
1075 ret = traverse_directory(trans, root, source_dir, &dir_head);
1077 error("unable to traverse directory %s: %d", source_dir, ret);
1080 ret = btrfs_commit_transaction(trans, root);
1082 error("transaction commit failed: %d", ret);
1087 printf("Making image is completed.\n");
1090 while (!list_empty(&dir_head.list)) {
1091 dir_entry = list_entry(dir_head.list.next,
1092 struct directory_name_entry, list);
1093 list_del(&dir_entry->list);
1101 * This ignores symlinks with unreadable targets and subdirs that can't
1102 * be read. It's a best-effort to give a rough estimate of the size of
1103 * a subdir. It doesn't guarantee that prepopulating btrfs from this
1104 * tree won't still run out of space.
1106 static u64 global_total_size;
1107 static u64 fs_block_size;
1108 static int ftw_add_entry_size(const char *fpath, const struct stat *st,
1111 if (type == FTW_F || type == FTW_D)
1112 global_total_size += round_up(st->st_size, fs_block_size);
1117 static u64 size_sourcedir(const char *dir_name, u64 sectorsize,
1118 u64 *num_of_meta_chunks_ret, u64 *size_of_data_ret)
1123 u64 default_chunk_size = SZ_8M;
1124 u64 allocated_meta_size = SZ_8M;
1125 u64 allocated_total_size = 20 * SZ_1M; /* 20MB */
1126 u64 num_of_meta_chunks = 0;
1127 u64 num_of_data_chunks = 0;
1128 u64 num_of_allocated_meta_chunks =
1129 allocated_meta_size / default_chunk_size;
1131 global_total_size = 0;
1132 fs_block_size = sectorsize;
1133 ret = ftw(dir_name, ftw_add_entry_size, 10);
1134 dir_size = global_total_size;
1136 error("ftw subdir walk of %s failed: %s", dir_name,
1141 num_of_data_chunks = (dir_size + default_chunk_size - 1) /
1144 num_of_meta_chunks = (dir_size / 2) / default_chunk_size;
1145 if (((dir_size / 2) % default_chunk_size) != 0)
1146 num_of_meta_chunks++;
1147 if (num_of_meta_chunks <= num_of_allocated_meta_chunks)
1148 num_of_meta_chunks = 0;
1150 num_of_meta_chunks -= num_of_allocated_meta_chunks;
1152 total_size = allocated_total_size +
1153 (num_of_data_chunks * default_chunk_size) +
1154 (num_of_meta_chunks * default_chunk_size);
1156 *num_of_meta_chunks_ret = num_of_meta_chunks;
1157 *size_of_data_ret = num_of_data_chunks * default_chunk_size;
1161 static int zero_output_file(int out_fd, u64 size)
1169 memset(buf, 0, 4096);
1170 loop_num = size / 4096;
1171 for (i = 0; i < loop_num; i++) {
1172 written = pwrite64(out_fd, buf, 4096, location);
1173 if (written != 4096)
1180 static int is_ssd(const char *file)
1183 char wholedisk[PATH_MAX];
1184 char sysfs_path[PATH_MAX];
1190 probe = blkid_new_probe_from_filename(file);
1194 /* Device number of this disk (possibly a partition) */
1195 devno = blkid_probe_get_devno(probe);
1197 blkid_free_probe(probe);
1201 /* Get whole disk name (not full path) for this devno */
1202 ret = blkid_devno_to_wholedisk(devno,
1203 wholedisk, sizeof(wholedisk), NULL);
1205 blkid_free_probe(probe);
1209 snprintf(sysfs_path, PATH_MAX, "/sys/block/%s/queue/rotational",
1212 blkid_free_probe(probe);
1214 fd = open(sysfs_path, O_RDONLY);
1219 if (read(fd, &rotational, 1) < 1) {
1225 return rotational == '0';
1228 static int _cmp_device_by_id(void *priv, struct list_head *a,
1229 struct list_head *b)
1231 return list_entry(a, struct btrfs_device, dev_list)->devid -
1232 list_entry(b, struct btrfs_device, dev_list)->devid;
1235 static void list_all_devices(struct btrfs_root *root)
1237 struct btrfs_fs_devices *fs_devices;
1238 struct btrfs_device *device;
1239 int number_of_devices = 0;
1240 u64 total_block_count = 0;
1242 fs_devices = root->fs_info->fs_devices;
1244 list_for_each_entry(device, &fs_devices->devices, dev_list)
1245 number_of_devices++;
1247 list_sort(NULL, &fs_devices->devices, _cmp_device_by_id);
1249 printf("Number of devices: %d\n", number_of_devices);
1250 /* printf("Total devices size: %10s\n", */
1251 /* pretty_size(total_block_count)); */
1252 printf("Devices:\n");
1253 printf(" ID SIZE PATH\n");
1254 list_for_each_entry(device, &fs_devices->devices, dev_list) {
1255 printf(" %3llu %10s %s\n",
1257 pretty_size(device->total_bytes),
1259 total_block_count += device->total_bytes;
1265 static int is_temp_block_group(struct extent_buffer *node,
1266 struct btrfs_block_group_item *bgi,
1267 u64 data_profile, u64 meta_profile,
1270 u64 flag = btrfs_disk_block_group_flags(node, bgi);
1271 u64 flag_type = flag & BTRFS_BLOCK_GROUP_TYPE_MASK;
1272 u64 flag_profile = flag & BTRFS_BLOCK_GROUP_PROFILE_MASK;
1273 u64 used = btrfs_disk_block_group_used(node, bgi);
1276 * Chunks meets all the following conditions is a temp chunk
1278 * Temp chunk is always empty.
1280 * 2) profile mismatch with mkfs profile.
1281 * Temp chunk is always in SINGLE
1283 * 3) Size differs with mkfs_alloc
1284 * Special case for SINGLE/SINGLE btrfs.
1285 * In that case, temp data chunk and real data chunk are always empty.
1286 * So we need to use mkfs_alloc to be sure which chunk is the newly
1289 * Normally, new chunk size is equal to mkfs one (One chunk)
1290 * If it has multiple chunks, we just refuse to delete any one.
1291 * As they are all single, so no real problem will happen.
1292 * So only use condition 1) and 2) to judge them.
1296 switch (flag_type) {
1297 case BTRFS_BLOCK_GROUP_DATA:
1298 case BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA:
1299 data_profile &= BTRFS_BLOCK_GROUP_PROFILE_MASK;
1300 if (flag_profile != data_profile)
1303 case BTRFS_BLOCK_GROUP_METADATA:
1304 meta_profile &= BTRFS_BLOCK_GROUP_PROFILE_MASK;
1305 if (flag_profile != meta_profile)
1308 case BTRFS_BLOCK_GROUP_SYSTEM:
1309 sys_profile &= BTRFS_BLOCK_GROUP_PROFILE_MASK;
1310 if (flag_profile != sys_profile)
1317 /* Note: if current is a block group, it will skip it anyway */
1318 static int next_block_group(struct btrfs_root *root,
1319 struct btrfs_path *path)
1321 struct btrfs_key key;
1325 ret = btrfs_next_item(root, path);
1329 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1330 if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY)
1337 /* This function will cleanup */
1338 static int cleanup_temp_chunks(struct btrfs_fs_info *fs_info,
1339 struct mkfs_allocation *alloc,
1340 u64 data_profile, u64 meta_profile,
1343 struct btrfs_trans_handle *trans = NULL;
1344 struct btrfs_block_group_item *bgi;
1345 struct btrfs_root *root = fs_info->extent_root;
1346 struct btrfs_key key;
1347 struct btrfs_key found_key;
1348 struct btrfs_path path;
1351 btrfs_init_path(&path);
1352 trans = btrfs_start_transaction(root, 1);
1353 BUG_ON(IS_ERR(trans));
1356 key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
1361 * as the rest of the loop may modify the tree, we need to
1362 * start a new search each time.
1364 ret = btrfs_search_slot(trans, root, &key, &path, 0, 0);
1368 btrfs_item_key_to_cpu(path.nodes[0], &found_key,
1370 if (found_key.objectid < key.objectid)
1372 if (found_key.type != BTRFS_BLOCK_GROUP_ITEM_KEY) {
1373 ret = next_block_group(root, &path);
1380 btrfs_item_key_to_cpu(path.nodes[0], &found_key,
1384 bgi = btrfs_item_ptr(path.nodes[0], path.slots[0],
1385 struct btrfs_block_group_item);
1386 if (is_temp_block_group(path.nodes[0], bgi,
1387 data_profile, meta_profile,
1389 u64 flags = btrfs_disk_block_group_flags(path.nodes[0],
1392 ret = btrfs_free_block_group(trans, fs_info,
1393 found_key.objectid, found_key.offset);
1397 if ((flags & BTRFS_BLOCK_GROUP_TYPE_MASK) ==
1398 BTRFS_BLOCK_GROUP_DATA)
1399 alloc->data -= found_key.offset;
1400 else if ((flags & BTRFS_BLOCK_GROUP_TYPE_MASK) ==
1401 BTRFS_BLOCK_GROUP_METADATA)
1402 alloc->metadata -= found_key.offset;
1403 else if ((flags & BTRFS_BLOCK_GROUP_TYPE_MASK) ==
1404 BTRFS_BLOCK_GROUP_SYSTEM)
1405 alloc->system -= found_key.offset;
1406 else if ((flags & BTRFS_BLOCK_GROUP_TYPE_MASK) ==
1407 (BTRFS_BLOCK_GROUP_METADATA |
1408 BTRFS_BLOCK_GROUP_DATA))
1409 alloc->mixed -= found_key.offset;
1411 btrfs_release_path(&path);
1412 key.objectid = found_key.objectid + found_key.offset;
1416 btrfs_commit_transaction(trans, root);
1417 btrfs_release_path(&path);
1421 int main(int argc, char **argv)
1424 struct btrfs_root *root;
1425 struct btrfs_fs_info *fs_info;
1426 struct btrfs_trans_handle *trans;
1428 u64 block_count = 0;
1429 u64 dev_block_count = 0;
1430 u64 alloc_start = 0;
1431 u64 metadata_profile = 0;
1432 u64 data_profile = 0;
1433 u32 nodesize = max_t(u32, sysconf(_SC_PAGESIZE),
1434 BTRFS_MKFS_DEFAULT_NODE_SIZE);
1435 u32 sectorsize = 4096;
1436 u32 stripesize = 4096;
1442 int nodesize_forced = 0;
1443 int data_profile_opt = 0;
1444 int metadata_profile_opt = 0;
1447 int force_overwrite = 0;
1448 char *source_dir = NULL;
1449 int source_dir_set = 0;
1450 u64 num_of_meta_chunks = 0;
1451 u64 size_of_data = 0;
1452 u64 source_dir_size = 0;
1455 char fs_uuid[BTRFS_UUID_UNPARSED_SIZE] = { 0 };
1456 u64 features = BTRFS_MKFS_DEFAULT_FEATURES;
1457 struct mkfs_allocation allocation = { 0 };
1458 struct btrfs_mkfs_config mkfs_cfg;
1462 static const struct option long_options[] = {
1463 { "alloc-start", required_argument, NULL, 'A'},
1464 { "byte-count", required_argument, NULL, 'b' },
1465 { "force", no_argument, NULL, 'f' },
1466 { "leafsize", required_argument, NULL, 'l' },
1467 { "label", required_argument, NULL, 'L'},
1468 { "metadata", required_argument, NULL, 'm' },
1469 { "mixed", no_argument, NULL, 'M' },
1470 { "nodesize", required_argument, NULL, 'n' },
1471 { "sectorsize", required_argument, NULL, 's' },
1472 { "data", required_argument, NULL, 'd' },
1473 { "version", no_argument, NULL, 'V' },
1474 { "rootdir", required_argument, NULL, 'r' },
1475 { "nodiscard", no_argument, NULL, 'K' },
1476 { "features", required_argument, NULL, 'O' },
1477 { "uuid", required_argument, NULL, 'U' },
1478 { "quiet", 0, NULL, 'q' },
1479 { "help", no_argument, NULL, GETOPT_VAL_HELP },
1483 c = getopt_long(argc, argv, "A:b:fl:n:s:m:d:L:O:r:U:VMKq",
1484 long_options, NULL);
1489 alloc_start = parse_size(optarg);
1492 force_overwrite = 1;
1495 data_profile = parse_profile(optarg);
1496 data_profile_opt = 1;
1499 warning("--leafsize is deprecated, use --nodesize");
1502 nodesize = parse_size(optarg);
1503 nodesize_forced = 1;
1506 label = parse_label(optarg);
1509 metadata_profile = parse_profile(optarg);
1510 metadata_profile_opt = 1;
1516 char *orig = strdup(optarg);
1519 tmp = btrfs_parse_fs_features(tmp, &features);
1521 error("unrecognized filesystem feature '%s'",
1527 if (features & BTRFS_FEATURE_LIST_ALL) {
1528 btrfs_list_all_fs_features(0);
1534 sectorsize = parse_size(optarg);
1537 block_count = parse_size(optarg);
1541 printf("mkfs.btrfs, part of %s\n",
1545 source_dir = optarg;
1549 strncpy(fs_uuid, optarg,
1550 BTRFS_UUID_UNPARSED_SIZE - 1);
1558 case GETOPT_VAL_HELP:
1560 print_usage(c != GETOPT_VAL_HELP);
1565 printf("%s\n", PACKAGE_STRING);
1566 printf("See %s for more information.\n\n", PACKAGE_URL);
1569 sectorsize = max(sectorsize, (u32)sysconf(_SC_PAGESIZE));
1570 stripesize = sectorsize;
1571 saved_optind = optind;
1572 dev_cnt = argc - optind;
1576 if (source_dir_set && dev_cnt > 1) {
1577 error("the option -r is limited to a single device");
1584 if (uuid_parse(fs_uuid, dummy_uuid) != 0) {
1585 error("could not parse UUID: %s", fs_uuid);
1588 if (!test_uuid_unique(fs_uuid)) {
1589 error("non-unique UUID: %s", fs_uuid);
1594 while (dev_cnt-- > 0) {
1595 file = argv[optind++];
1596 if (is_block_device(file) == 1)
1597 if (test_dev_for_mkfs(file, force_overwrite))
1601 optind = saved_optind;
1602 dev_cnt = argc - optind;
1604 file = argv[optind++];
1608 * Set default profiles according to number of added devices.
1609 * For mixed groups defaults are single/single.
1612 if (!metadata_profile_opt) {
1613 if (dev_cnt == 1 && ssd && verbose)
1614 printf("Detected a SSD, turning off metadata "
1615 "duplication. Mkfs with -m dup if you want to "
1616 "force metadata duplication.\n");
1618 metadata_profile = (dev_cnt > 1) ?
1619 BTRFS_BLOCK_GROUP_RAID1 : (ssd) ?
1620 0: BTRFS_BLOCK_GROUP_DUP;
1622 if (!data_profile_opt) {
1623 data_profile = (dev_cnt > 1) ?
1624 BTRFS_BLOCK_GROUP_RAID0 : 0; /* raid0 or single */
1627 u32 best_nodesize = max_t(u32, sysconf(_SC_PAGESIZE), sectorsize);
1629 if (metadata_profile_opt || data_profile_opt) {
1630 if (metadata_profile != data_profile) {
1632 "with mixed block groups data and metadata profiles must be the same");
1637 if (!nodesize_forced)
1638 nodesize = best_nodesize;
1642 * FS features that can be set by other means than -O
1643 * just set the bit here
1646 features |= BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS;
1648 if ((data_profile | metadata_profile) &
1649 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
1650 features |= BTRFS_FEATURE_INCOMPAT_RAID56;
1653 if (btrfs_check_nodesize(nodesize, sectorsize,
1657 if (sectorsize < sizeof(struct btrfs_super_block)) {
1658 error("sectorsize smaller than superblock: %u < %zu",
1659 sectorsize, sizeof(struct btrfs_super_block));
1663 /* Check device/block_count after the nodesize is determined */
1664 if (block_count && block_count < btrfs_min_dev_size(nodesize)) {
1665 error("size %llu is too small to make a usable filesystem",
1667 error("minimum size for btrfs filesystem is %llu",
1668 btrfs_min_dev_size(nodesize));
1671 for (i = saved_optind; i < saved_optind + dev_cnt; i++) {
1675 ret = test_minimum_size(path, nodesize);
1677 error("failed to check size for %s: %s",
1678 path, strerror(-ret));
1682 error("'%s' is too small to make a usable filesystem",
1684 error("minimum size for each btrfs device is %llu",
1685 btrfs_min_dev_size(nodesize));
1689 ret = test_num_disk_vs_raid(metadata_profile, data_profile,
1690 dev_cnt, mixed, ssd);
1696 if (!source_dir_set) {
1698 * open without O_EXCL so that the problem should not
1699 * occur by the following processing.
1700 * (btrfs_register_one_device() fails if O_EXCL is on)
1702 fd = open(file, O_RDWR);
1704 error("unable to open %s: %s", file, strerror(errno));
1707 ret = btrfs_prepare_device(fd, file, &dev_block_count,
1709 (zero_end ? PREP_DEVICE_ZERO_END : 0) |
1710 (discard ? PREP_DEVICE_DISCARD : 0) |
1711 (verbose ? PREP_DEVICE_VERBOSE : 0));
1715 if (block_count && block_count > dev_block_count) {
1716 error("%s is smaller than requested size, expected %llu, found %llu",
1718 (unsigned long long)block_count,
1719 (unsigned long long)dev_block_count);
1723 fd = open(file, O_CREAT | O_RDWR,
1724 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
1726 error("unable to open %s: %s", file, strerror(errno));
1730 source_dir_size = size_sourcedir(source_dir, sectorsize,
1731 &num_of_meta_chunks, &size_of_data);
1732 if(block_count < source_dir_size)
1733 block_count = source_dir_size;
1734 ret = zero_output_file(fd, block_count);
1736 error("unable to zero the output file");
1739 /* our "device" is the new image file */
1740 dev_block_count = block_count;
1743 /* To create the first block group and chunk 0 in make_btrfs */
1744 if (dev_block_count < BTRFS_MKFS_SYSTEM_GROUP_SIZE) {
1745 error("device is too small to make filesystem, must be at least %llu",
1746 (unsigned long long)BTRFS_MKFS_SYSTEM_GROUP_SIZE);
1750 if (group_profile_max_safe_loss(metadata_profile) <
1751 group_profile_max_safe_loss(data_profile)){
1752 warning("metadata has lower redundancy than data!\n");
1755 mkfs_cfg.label = label;
1756 memcpy(mkfs_cfg.fs_uuid, fs_uuid, sizeof(mkfs_cfg.fs_uuid));
1757 mkfs_cfg.num_bytes = dev_block_count;
1758 mkfs_cfg.nodesize = nodesize;
1759 mkfs_cfg.sectorsize = sectorsize;
1760 mkfs_cfg.stripesize = stripesize;
1761 mkfs_cfg.features = features;
1763 ret = make_btrfs(fd, &mkfs_cfg);
1765 error("error during mkfs: %s", strerror(-ret));
1769 fs_info = open_ctree_fs_info(file, 0, 0, 0,
1770 OPEN_CTREE_WRITES | OPEN_CTREE_FS_PARTIAL);
1772 error("open ctree failed");
1777 root = fs_info->fs_root;
1778 fs_info->alloc_start = alloc_start;
1780 ret = create_metadata_block_groups(root, mixed, &allocation);
1782 error("failed to create default block groups: %d", ret);
1786 trans = btrfs_start_transaction(root, 1);
1787 if (IS_ERR(trans)) {
1788 error("failed to start transaction");
1792 ret = create_data_block_groups(trans, root, mixed, &allocation);
1794 error("failed to create default data block groups: %d", ret);
1798 ret = make_root_dir(trans, root);
1800 error("failed to setup the root directory: %d", ret);
1804 ret = btrfs_commit_transaction(trans, root);
1806 error("unable to commit transaction: %d", ret);
1810 trans = btrfs_start_transaction(root, 1);
1811 if (IS_ERR(trans)) {
1812 error("failed to start transaction");
1819 while (dev_cnt-- > 0) {
1820 file = argv[optind++];
1823 * open without O_EXCL so that the problem should not
1824 * occur by the following processing.
1825 * (btrfs_register_one_device() fails if O_EXCL is on)
1827 fd = open(file, O_RDWR);
1829 error("unable to open %s: %s", file, strerror(errno));
1832 ret = btrfs_device_already_in_root(root, fd,
1833 BTRFS_SUPER_INFO_OFFSET);
1835 error("skipping duplicate device %s in the filesystem",
1840 ret = btrfs_prepare_device(fd, file, &dev_block_count,
1842 (verbose ? PREP_DEVICE_VERBOSE : 0) |
1843 (zero_end ? PREP_DEVICE_ZERO_END : 0) |
1844 (discard ? PREP_DEVICE_DISCARD : 0));
1849 ret = btrfs_add_to_fsid(trans, root, fd, file, dev_block_count,
1850 sectorsize, sectorsize, sectorsize);
1852 error("unable to add %s to filesystem: %d", file, ret);
1856 struct btrfs_device *device;
1858 device = container_of(fs_info->fs_devices->devices.next,
1859 struct btrfs_device, dev_list);
1860 printf("adding device %s id %llu\n", file,
1861 (unsigned long long)device->devid);
1866 if (!source_dir_set) {
1867 ret = create_raid_groups(trans, root, data_profile,
1868 metadata_profile, mixed, &allocation);
1870 error("unable to create raid groups: %d", ret);
1875 ret = create_tree(trans, root, BTRFS_DATA_RELOC_TREE_OBJECTID);
1877 error("unable to create data reloc tree: %d", ret);
1881 ret = btrfs_commit_transaction(trans, root);
1883 error("unable to commit transaction: %d", ret);
1887 if (source_dir_set) {
1888 trans = btrfs_start_transaction(root, 1);
1889 BUG_ON(IS_ERR(trans));
1890 ret = create_chunks(trans, root,
1891 num_of_meta_chunks, size_of_data,
1894 error("unable to create chunks: %d", ret);
1897 ret = btrfs_commit_transaction(trans, root);
1899 error("transaction commit failed: %d", ret);
1903 ret = make_image(source_dir, root);
1905 error("error wihle filling filesystem: %d", ret);
1909 ret = cleanup_temp_chunks(fs_info, &allocation, data_profile,
1910 metadata_profile, metadata_profile);
1912 error("failed to cleanup temporary chunks: %d", ret);
1917 char features_buf[64];
1919 printf("Label: %s\n", label);
1920 printf("UUID: %s\n", mkfs_cfg.fs_uuid);
1921 printf("Node size: %u\n", nodesize);
1922 printf("Sector size: %u\n", sectorsize);
1923 printf("Filesystem size: %s\n",
1924 pretty_size(btrfs_super_total_bytes(fs_info->super_copy)));
1925 printf("Block group profiles:\n");
1926 if (allocation.data)
1927 printf(" Data: %-8s %16s\n",
1928 btrfs_group_profile_str(data_profile),
1929 pretty_size(allocation.data));
1930 if (allocation.metadata)
1931 printf(" Metadata: %-8s %16s\n",
1932 btrfs_group_profile_str(metadata_profile),
1933 pretty_size(allocation.metadata));
1934 if (allocation.mixed)
1935 printf(" Data+Metadata: %-8s %16s\n",
1936 btrfs_group_profile_str(data_profile),
1937 pretty_size(allocation.mixed));
1938 printf(" System: %-8s %16s\n",
1939 btrfs_group_profile_str(metadata_profile),
1940 pretty_size(allocation.system));
1941 printf("SSD detected: %s\n", ssd ? "yes" : "no");
1942 btrfs_parse_features_to_string(features_buf, features);
1943 printf("Incompat features: %s", features_buf);
1946 list_all_devices(root);
1950 * The filesystem is now fully set up, commit the remaining changes and
1951 * fix the signature as the last step before closing the devices.
1953 fs_info->finalize_on_close = 1;
1955 ret = close_ctree(root);
1958 optind = saved_optind;
1959 dev_cnt = argc - optind;
1960 while (dev_cnt-- > 0) {
1961 file = argv[optind++];
1962 if (is_block_device(file) == 1)
1963 btrfs_register_one_device(file);
1967 btrfs_close_all_devices();