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/dir.h> included via androidcompat.h */
31 #include <uuid/uuid.h>
33 #include <blkid/blkid.h>
38 #include "transaction.h"
40 #include "list_sort.h"
42 #include "mkfs/common.h"
43 #include "mkfs/rootdir.h"
44 #include "fsfeatures.h"
46 static int verbose = 1;
48 struct mkfs_allocation {
55 static int create_metadata_block_groups(struct btrfs_root *root, int mixed,
56 struct mkfs_allocation *allocation)
58 struct btrfs_fs_info *fs_info = root->fs_info;
59 struct btrfs_trans_handle *trans;
65 trans = btrfs_start_transaction(root, 1);
66 BUG_ON(IS_ERR(trans));
67 bytes_used = btrfs_super_bytes_used(fs_info->super_copy);
69 root->fs_info->system_allocs = 1;
70 ret = btrfs_make_block_group(trans, fs_info, bytes_used,
71 BTRFS_BLOCK_GROUP_SYSTEM,
72 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
73 0, BTRFS_MKFS_SYSTEM_GROUP_SIZE);
74 allocation->system += BTRFS_MKFS_SYSTEM_GROUP_SIZE;
79 ret = btrfs_alloc_chunk(trans, fs_info,
80 &chunk_start, &chunk_size,
81 BTRFS_BLOCK_GROUP_METADATA |
82 BTRFS_BLOCK_GROUP_DATA);
84 error("no space to allocate data/metadata chunk");
89 ret = btrfs_make_block_group(trans, fs_info, 0,
90 BTRFS_BLOCK_GROUP_METADATA |
91 BTRFS_BLOCK_GROUP_DATA,
92 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
93 chunk_start, chunk_size);
96 allocation->mixed += chunk_size;
98 ret = btrfs_alloc_chunk(trans, fs_info,
99 &chunk_start, &chunk_size,
100 BTRFS_BLOCK_GROUP_METADATA);
101 if (ret == -ENOSPC) {
102 error("no space to allocate metadata chunk");
107 ret = btrfs_make_block_group(trans, fs_info, 0,
108 BTRFS_BLOCK_GROUP_METADATA,
109 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
110 chunk_start, chunk_size);
111 allocation->metadata += chunk_size;
116 root->fs_info->system_allocs = 0;
117 ret = btrfs_commit_transaction(trans, root);
123 static int create_data_block_groups(struct btrfs_trans_handle *trans,
124 struct btrfs_root *root, int mixed,
125 struct mkfs_allocation *allocation)
127 struct btrfs_fs_info *fs_info = root->fs_info;
133 ret = btrfs_alloc_chunk(trans, fs_info,
134 &chunk_start, &chunk_size,
135 BTRFS_BLOCK_GROUP_DATA);
136 if (ret == -ENOSPC) {
137 error("no space to allocate data chunk");
142 ret = btrfs_make_block_group(trans, fs_info, 0,
143 BTRFS_BLOCK_GROUP_DATA,
144 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
145 chunk_start, chunk_size);
146 allocation->data += chunk_size;
155 static int make_root_dir(struct btrfs_trans_handle *trans,
156 struct btrfs_root *root)
158 struct btrfs_key location;
161 ret = btrfs_make_root_dir(trans, root->fs_info->tree_root,
162 BTRFS_ROOT_TREE_DIR_OBJECTID);
165 ret = btrfs_make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID);
168 memcpy(&location, &root->fs_info->fs_root->root_key, sizeof(location));
169 location.offset = (u64)-1;
170 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
172 btrfs_super_root_dir(root->fs_info->super_copy),
173 &location, BTRFS_FT_DIR, 0);
177 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
178 "default", 7, location.objectid,
179 BTRFS_ROOT_TREE_DIR_OBJECTID, 0);
187 static int __recow_root(struct btrfs_trans_handle *trans,
188 struct btrfs_root *root)
190 struct extent_buffer *tmp;
193 if (trans->transid != btrfs_root_generation(&root->root_item)) {
194 extent_buffer_get(root->node);
195 ret = __btrfs_cow_block(trans, root, root->node,
196 NULL, 0, &tmp, 0, 0);
199 free_extent_buffer(tmp);
205 static int recow_roots(struct btrfs_trans_handle *trans,
206 struct btrfs_root *root)
208 struct btrfs_fs_info *info = root->fs_info;
211 ret = __recow_root(trans, info->fs_root);
214 ret = __recow_root(trans, info->tree_root);
217 ret = __recow_root(trans, info->extent_root);
220 ret = __recow_root(trans, info->chunk_root);
223 ret = __recow_root(trans, info->dev_root);
226 ret = __recow_root(trans, info->csum_root);
233 static int create_one_raid_group(struct btrfs_trans_handle *trans,
234 struct btrfs_root *root, u64 type,
235 struct mkfs_allocation *allocation)
238 struct btrfs_fs_info *fs_info = root->fs_info;
243 ret = btrfs_alloc_chunk(trans, fs_info,
244 &chunk_start, &chunk_size, type);
245 if (ret == -ENOSPC) {
246 error("not enough free space to allocate chunk");
252 ret = btrfs_make_block_group(trans, fs_info, 0,
253 type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
254 chunk_start, chunk_size);
256 type &= BTRFS_BLOCK_GROUP_TYPE_MASK;
257 if (type == BTRFS_BLOCK_GROUP_DATA) {
258 allocation->data += chunk_size;
259 } else if (type == BTRFS_BLOCK_GROUP_METADATA) {
260 allocation->metadata += chunk_size;
261 } else if (type == BTRFS_BLOCK_GROUP_SYSTEM) {
262 allocation->system += chunk_size;
264 (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA)) {
265 allocation->mixed += chunk_size;
267 error("unrecognized profile type: 0x%llx",
268 (unsigned long long)type);
275 static int create_raid_groups(struct btrfs_trans_handle *trans,
276 struct btrfs_root *root, u64 data_profile,
277 u64 metadata_profile, int mixed,
278 struct mkfs_allocation *allocation)
282 if (metadata_profile) {
283 u64 meta_flags = BTRFS_BLOCK_GROUP_METADATA;
285 ret = create_one_raid_group(trans, root,
286 BTRFS_BLOCK_GROUP_SYSTEM |
287 metadata_profile, allocation);
292 meta_flags |= BTRFS_BLOCK_GROUP_DATA;
294 ret = create_one_raid_group(trans, root, meta_flags |
295 metadata_profile, allocation);
300 if (!mixed && data_profile) {
301 ret = create_one_raid_group(trans, root,
302 BTRFS_BLOCK_GROUP_DATA |
303 data_profile, allocation);
307 ret = recow_roots(trans, root);
312 static int create_tree(struct btrfs_trans_handle *trans,
313 struct btrfs_root *root, u64 objectid)
315 struct btrfs_key location;
316 struct btrfs_root_item root_item;
317 struct extent_buffer *tmp;
320 ret = btrfs_copy_root(trans, root, root->node, &tmp, objectid);
324 memcpy(&root_item, &root->root_item, sizeof(root_item));
325 btrfs_set_root_bytenr(&root_item, tmp->start);
326 btrfs_set_root_level(&root_item, btrfs_header_level(tmp));
327 btrfs_set_root_generation(&root_item, trans->transid);
328 free_extent_buffer(tmp);
330 location.objectid = objectid;
331 location.type = BTRFS_ROOT_ITEM_KEY;
333 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
334 &location, &root_item);
339 static void print_usage(int ret)
341 printf("Usage: mkfs.btrfs [options] dev [ dev ... ]\n");
342 printf("Options:\n");
343 printf(" allocation profiles:\n");
344 printf("\t-d|--data PROFILE data profile, raid0, raid1, raid5, raid6, raid10, dup or single\n");
345 printf("\t-m|--metadata PROFILE metadata profile, values like for data profile\n");
346 printf("\t-M|--mixed mix metadata and data together\n");
347 printf(" features:\n");
348 printf("\t-n|--nodesize SIZE size of btree nodes\n");
349 printf("\t-s|--sectorsize SIZE data block size (may not be mountable by current kernel)\n");
350 printf("\t-O|--features LIST comma separated list of filesystem features (use '-O list-all' to list features)\n");
351 printf("\t-L|--label LABEL set the filesystem label\n");
352 printf("\t-U|--uuid UUID specify the filesystem UUID (must be unique)\n");
353 printf(" creation:\n");
354 printf("\t-b|--byte-count SIZE set filesystem size to SIZE (on the first device)\n");
355 printf("\t-r|--rootdir DIR copy files from DIR to the image root directory\n");
356 printf("\t-K|--nodiscard do not perform whole device TRIM\n");
357 printf("\t-f|--force force overwrite of existing filesystem\n");
358 printf(" general:\n");
359 printf("\t-q|--quiet no messages except errors\n");
360 printf("\t-V|--version print the mkfs.btrfs version and exit\n");
361 printf("\t--help print this help and exit\n");
362 printf(" deprecated:\n");
363 printf("\t-A|--alloc-start START the offset to start the filesystem\n");
364 printf("\t-l|--leafsize SIZE deprecated, alias for nodesize\n");
368 static u64 parse_profile(const char *s)
370 if (strcasecmp(s, "raid0") == 0) {
371 return BTRFS_BLOCK_GROUP_RAID0;
372 } else if (strcasecmp(s, "raid1") == 0) {
373 return BTRFS_BLOCK_GROUP_RAID1;
374 } else if (strcasecmp(s, "raid5") == 0) {
375 return BTRFS_BLOCK_GROUP_RAID5;
376 } else if (strcasecmp(s, "raid6") == 0) {
377 return BTRFS_BLOCK_GROUP_RAID6;
378 } else if (strcasecmp(s, "raid10") == 0) {
379 return BTRFS_BLOCK_GROUP_RAID10;
380 } else if (strcasecmp(s, "dup") == 0) {
381 return BTRFS_BLOCK_GROUP_DUP;
382 } else if (strcasecmp(s, "single") == 0) {
385 error("unknown profile %s", s);
392 static char *parse_label(const char *input)
394 int len = strlen(input);
396 if (len >= BTRFS_LABEL_SIZE) {
397 error("label %s is too long (max %d)", input,
398 BTRFS_LABEL_SIZE - 1);
401 return strdup(input);
404 static int create_chunks(struct btrfs_trans_handle *trans,
405 struct btrfs_root *root, u64 num_of_meta_chunks,
407 struct mkfs_allocation *allocation)
409 struct btrfs_fs_info *fs_info = root->fs_info;
412 u64 meta_type = BTRFS_BLOCK_GROUP_METADATA;
413 u64 data_type = BTRFS_BLOCK_GROUP_DATA;
414 u64 minimum_data_chunk_size = SZ_8M;
418 for (i = 0; i < num_of_meta_chunks; i++) {
419 ret = btrfs_alloc_chunk(trans, fs_info,
420 &chunk_start, &chunk_size, meta_type);
423 ret = btrfs_make_block_group(trans, fs_info, 0,
424 meta_type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
425 chunk_start, chunk_size);
426 allocation->metadata += chunk_size;
429 set_extent_dirty(&root->fs_info->free_space_cache,
430 chunk_start, chunk_start + chunk_size - 1);
433 if (size_of_data < minimum_data_chunk_size)
434 size_of_data = minimum_data_chunk_size;
436 ret = btrfs_alloc_data_chunk(trans, fs_info,
437 &chunk_start, size_of_data, data_type, 0);
440 ret = btrfs_make_block_group(trans, fs_info, 0,
441 data_type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
442 chunk_start, size_of_data);
443 allocation->data += size_of_data;
446 set_extent_dirty(&root->fs_info->free_space_cache,
447 chunk_start, chunk_start + size_of_data - 1);
452 * This ignores symlinks with unreadable targets and subdirs that can't
453 * be read. It's a best-effort to give a rough estimate of the size of
454 * a subdir. It doesn't guarantee that prepopulating btrfs from this
455 * tree won't still run out of space.
457 static u64 global_total_size;
458 static u64 fs_block_size;
459 static int ftw_add_entry_size(const char *fpath, const struct stat *st,
462 if (type == FTW_F || type == FTW_D)
463 global_total_size += round_up(st->st_size, fs_block_size);
468 static u64 size_sourcedir(const char *dir_name, u64 sectorsize,
469 u64 *num_of_meta_chunks_ret, u64 *size_of_data_ret)
474 u64 default_chunk_size = SZ_8M;
475 u64 allocated_meta_size = SZ_8M;
476 u64 allocated_total_size = 20 * SZ_1M; /* 20MB */
477 u64 num_of_meta_chunks = 0;
478 u64 num_of_data_chunks = 0;
479 u64 num_of_allocated_meta_chunks =
480 allocated_meta_size / default_chunk_size;
482 global_total_size = 0;
483 fs_block_size = sectorsize;
484 ret = ftw(dir_name, ftw_add_entry_size, 10);
485 dir_size = global_total_size;
487 error("ftw subdir walk of %s failed: %s", dir_name,
492 num_of_data_chunks = (dir_size + default_chunk_size - 1) /
495 num_of_meta_chunks = (dir_size / 2) / default_chunk_size;
496 if (((dir_size / 2) % default_chunk_size) != 0)
497 num_of_meta_chunks++;
498 if (num_of_meta_chunks <= num_of_allocated_meta_chunks)
499 num_of_meta_chunks = 0;
501 num_of_meta_chunks -= num_of_allocated_meta_chunks;
503 total_size = allocated_total_size +
504 (num_of_data_chunks * default_chunk_size) +
505 (num_of_meta_chunks * default_chunk_size);
507 *num_of_meta_chunks_ret = num_of_meta_chunks;
508 *size_of_data_ret = num_of_data_chunks * default_chunk_size;
512 static int zero_output_file(int out_fd, u64 size)
520 memset(buf, 0, SZ_4K);
522 /* Only zero out the first 1M */
523 loop_num = SZ_1M / SZ_4K;
524 for (i = 0; i < loop_num; i++) {
525 written = pwrite64(out_fd, buf, SZ_4K, location);
526 if (written != SZ_4K)
531 /* Then enlarge the file to size */
532 written = pwrite64(out_fd, buf, 1, size - 1);
538 static int is_ssd(const char *file)
541 char wholedisk[PATH_MAX];
542 char sysfs_path[PATH_MAX];
548 probe = blkid_new_probe_from_filename(file);
552 /* Device number of this disk (possibly a partition) */
553 devno = blkid_probe_get_devno(probe);
555 blkid_free_probe(probe);
559 /* Get whole disk name (not full path) for this devno */
560 ret = blkid_devno_to_wholedisk(devno,
561 wholedisk, sizeof(wholedisk), NULL);
563 blkid_free_probe(probe);
567 snprintf(sysfs_path, PATH_MAX, "/sys/block/%s/queue/rotational",
570 blkid_free_probe(probe);
572 fd = open(sysfs_path, O_RDONLY);
577 if (read(fd, &rotational, 1) < 1) {
583 return rotational == '0';
586 static int _cmp_device_by_id(void *priv, struct list_head *a,
589 return list_entry(a, struct btrfs_device, dev_list)->devid -
590 list_entry(b, struct btrfs_device, dev_list)->devid;
593 static void list_all_devices(struct btrfs_root *root)
595 struct btrfs_fs_devices *fs_devices;
596 struct btrfs_device *device;
597 int number_of_devices = 0;
598 u64 total_block_count = 0;
600 fs_devices = root->fs_info->fs_devices;
602 list_for_each_entry(device, &fs_devices->devices, dev_list)
605 list_sort(NULL, &fs_devices->devices, _cmp_device_by_id);
607 printf("Number of devices: %d\n", number_of_devices);
608 /* printf("Total devices size: %10s\n", */
609 /* pretty_size(total_block_count)); */
610 printf("Devices:\n");
611 printf(" ID SIZE PATH\n");
612 list_for_each_entry(device, &fs_devices->devices, dev_list) {
613 printf(" %3llu %10s %s\n",
615 pretty_size(device->total_bytes),
617 total_block_count += device->total_bytes;
623 static int is_temp_block_group(struct extent_buffer *node,
624 struct btrfs_block_group_item *bgi,
625 u64 data_profile, u64 meta_profile,
628 u64 flag = btrfs_disk_block_group_flags(node, bgi);
629 u64 flag_type = flag & BTRFS_BLOCK_GROUP_TYPE_MASK;
630 u64 flag_profile = flag & BTRFS_BLOCK_GROUP_PROFILE_MASK;
631 u64 used = btrfs_disk_block_group_used(node, bgi);
634 * Chunks meets all the following conditions is a temp chunk
636 * Temp chunk is always empty.
638 * 2) profile mismatch with mkfs profile.
639 * Temp chunk is always in SINGLE
641 * 3) Size differs with mkfs_alloc
642 * Special case for SINGLE/SINGLE btrfs.
643 * In that case, temp data chunk and real data chunk are always empty.
644 * So we need to use mkfs_alloc to be sure which chunk is the newly
647 * Normally, new chunk size is equal to mkfs one (One chunk)
648 * If it has multiple chunks, we just refuse to delete any one.
649 * As they are all single, so no real problem will happen.
650 * So only use condition 1) and 2) to judge them.
655 case BTRFS_BLOCK_GROUP_DATA:
656 case BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA:
657 data_profile &= BTRFS_BLOCK_GROUP_PROFILE_MASK;
658 if (flag_profile != data_profile)
661 case BTRFS_BLOCK_GROUP_METADATA:
662 meta_profile &= BTRFS_BLOCK_GROUP_PROFILE_MASK;
663 if (flag_profile != meta_profile)
666 case BTRFS_BLOCK_GROUP_SYSTEM:
667 sys_profile &= BTRFS_BLOCK_GROUP_PROFILE_MASK;
668 if (flag_profile != sys_profile)
675 /* Note: if current is a block group, it will skip it anyway */
676 static int next_block_group(struct btrfs_root *root,
677 struct btrfs_path *path)
679 struct btrfs_key key;
683 ret = btrfs_next_item(root, path);
687 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
688 if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY)
695 /* This function will cleanup */
696 static int cleanup_temp_chunks(struct btrfs_fs_info *fs_info,
697 struct mkfs_allocation *alloc,
698 u64 data_profile, u64 meta_profile,
701 struct btrfs_trans_handle *trans = NULL;
702 struct btrfs_block_group_item *bgi;
703 struct btrfs_root *root = fs_info->extent_root;
704 struct btrfs_key key;
705 struct btrfs_key found_key;
706 struct btrfs_path path;
709 btrfs_init_path(&path);
710 trans = btrfs_start_transaction(root, 1);
711 BUG_ON(IS_ERR(trans));
714 key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
719 * as the rest of the loop may modify the tree, we need to
720 * start a new search each time.
722 ret = btrfs_search_slot(trans, root, &key, &path, 0, 0);
725 /* Don't pollute ret for >0 case */
729 btrfs_item_key_to_cpu(path.nodes[0], &found_key,
731 if (found_key.objectid < key.objectid)
733 if (found_key.type != BTRFS_BLOCK_GROUP_ITEM_KEY) {
734 ret = next_block_group(root, &path);
741 btrfs_item_key_to_cpu(path.nodes[0], &found_key,
745 bgi = btrfs_item_ptr(path.nodes[0], path.slots[0],
746 struct btrfs_block_group_item);
747 if (is_temp_block_group(path.nodes[0], bgi,
748 data_profile, meta_profile,
750 u64 flags = btrfs_disk_block_group_flags(path.nodes[0],
753 ret = btrfs_free_block_group(trans, fs_info,
754 found_key.objectid, found_key.offset);
758 if ((flags & BTRFS_BLOCK_GROUP_TYPE_MASK) ==
759 BTRFS_BLOCK_GROUP_DATA)
760 alloc->data -= found_key.offset;
761 else if ((flags & BTRFS_BLOCK_GROUP_TYPE_MASK) ==
762 BTRFS_BLOCK_GROUP_METADATA)
763 alloc->metadata -= found_key.offset;
764 else if ((flags & BTRFS_BLOCK_GROUP_TYPE_MASK) ==
765 BTRFS_BLOCK_GROUP_SYSTEM)
766 alloc->system -= found_key.offset;
767 else if ((flags & BTRFS_BLOCK_GROUP_TYPE_MASK) ==
768 (BTRFS_BLOCK_GROUP_METADATA |
769 BTRFS_BLOCK_GROUP_DATA))
770 alloc->mixed -= found_key.offset;
772 btrfs_release_path(&path);
773 key.objectid = found_key.objectid + found_key.offset;
777 btrfs_commit_transaction(trans, root);
778 btrfs_release_path(&path);
782 int main(int argc, char **argv)
785 struct btrfs_root *root;
786 struct btrfs_fs_info *fs_info;
787 struct btrfs_trans_handle *trans;
790 u64 dev_block_count = 0;
792 u64 metadata_profile = 0;
793 u64 data_profile = 0;
794 u32 nodesize = max_t(u32, sysconf(_SC_PAGESIZE),
795 BTRFS_MKFS_DEFAULT_NODE_SIZE);
796 u32 sectorsize = 4096;
797 u32 stripesize = 4096;
804 int nodesize_forced = 0;
805 int data_profile_opt = 0;
806 int metadata_profile_opt = 0;
809 int force_overwrite = 0;
810 char *source_dir = NULL;
811 int source_dir_set = 0;
812 u64 num_of_meta_chunks = 0;
813 u64 size_of_data = 0;
814 u64 source_dir_size = 0;
818 char fs_uuid[BTRFS_UUID_UNPARSED_SIZE] = { 0 };
819 u64 features = BTRFS_MKFS_DEFAULT_FEATURES;
820 struct mkfs_allocation allocation = { 0 };
821 struct btrfs_mkfs_config mkfs_cfg;
825 static const struct option long_options[] = {
826 { "alloc-start", required_argument, NULL, 'A'},
827 { "byte-count", required_argument, NULL, 'b' },
828 { "force", no_argument, NULL, 'f' },
829 { "leafsize", required_argument, NULL, 'l' },
830 { "label", required_argument, NULL, 'L'},
831 { "metadata", required_argument, NULL, 'm' },
832 { "mixed", no_argument, NULL, 'M' },
833 { "nodesize", required_argument, NULL, 'n' },
834 { "sectorsize", required_argument, NULL, 's' },
835 { "data", required_argument, NULL, 'd' },
836 { "version", no_argument, NULL, 'V' },
837 { "rootdir", required_argument, NULL, 'r' },
838 { "nodiscard", no_argument, NULL, 'K' },
839 { "features", required_argument, NULL, 'O' },
840 { "uuid", required_argument, NULL, 'U' },
841 { "quiet", 0, NULL, 'q' },
842 { "help", no_argument, NULL, GETOPT_VAL_HELP },
846 c = getopt_long(argc, argv, "A:b:fl:n:s:m:d:L:O:r:U:VMKq",
852 alloc_start = parse_size(optarg);
858 data_profile = parse_profile(optarg);
859 data_profile_opt = 1;
862 warning("--leafsize is deprecated, use --nodesize");
865 nodesize = parse_size(optarg);
869 label = parse_label(optarg);
872 metadata_profile = parse_profile(optarg);
873 metadata_profile_opt = 1;
879 char *orig = strdup(optarg);
882 tmp = btrfs_parse_fs_features(tmp, &features);
884 error("unrecognized filesystem feature '%s'",
890 if (features & BTRFS_FEATURE_LIST_ALL) {
891 btrfs_list_all_fs_features(0);
897 sectorsize = parse_size(optarg);
900 block_count = parse_size(optarg);
904 printf("mkfs.btrfs, part of %s\n",
912 strncpy(fs_uuid, optarg,
913 BTRFS_UUID_UNPARSED_SIZE - 1);
921 case GETOPT_VAL_HELP:
923 print_usage(c != GETOPT_VAL_HELP);
928 printf("%s\n", PACKAGE_STRING);
929 printf("See %s for more information.\n\n", PACKAGE_URL);
932 sectorsize = max(sectorsize, (u32)sysconf(_SC_PAGESIZE));
933 stripesize = sectorsize;
934 saved_optind = optind;
935 dev_cnt = argc - optind;
939 if (source_dir_set && dev_cnt > 1) {
940 error("the option -r is limited to a single device");
947 if (uuid_parse(fs_uuid, dummy_uuid) != 0) {
948 error("could not parse UUID: %s", fs_uuid);
951 if (!test_uuid_unique(fs_uuid)) {
952 error("non-unique UUID: %s", fs_uuid);
957 while (dev_cnt-- > 0) {
958 file = argv[optind++];
959 if (is_block_device(file) == 1)
960 ret = test_dev_for_mkfs(file, force_overwrite);
962 ret = test_status_for_mkfs(file, force_overwrite);
968 optind = saved_optind;
969 dev_cnt = argc - optind;
971 file = argv[optind++];
975 * Set default profiles according to number of added devices.
976 * For mixed groups defaults are single/single.
979 if (!metadata_profile_opt) {
980 if (dev_cnt == 1 && ssd && verbose)
981 printf("Detected a SSD, turning off metadata "
982 "duplication. Mkfs with -m dup if you want to "
983 "force metadata duplication.\n");
985 metadata_profile = (dev_cnt > 1) ?
986 BTRFS_BLOCK_GROUP_RAID1 : (ssd) ?
987 0: BTRFS_BLOCK_GROUP_DUP;
989 if (!data_profile_opt) {
990 data_profile = (dev_cnt > 1) ?
991 BTRFS_BLOCK_GROUP_RAID0 : 0; /* raid0 or single */
994 u32 best_nodesize = max_t(u32, sysconf(_SC_PAGESIZE), sectorsize);
996 if (metadata_profile_opt || data_profile_opt) {
997 if (metadata_profile != data_profile) {
999 "with mixed block groups data and metadata profiles must be the same");
1004 if (!nodesize_forced)
1005 nodesize = best_nodesize;
1009 * FS features that can be set by other means than -O
1010 * just set the bit here
1013 features |= BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS;
1015 if ((data_profile | metadata_profile) &
1016 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
1017 features |= BTRFS_FEATURE_INCOMPAT_RAID56;
1020 if (btrfs_check_nodesize(nodesize, sectorsize,
1024 if (sectorsize < sizeof(struct btrfs_super_block)) {
1025 error("sectorsize smaller than superblock: %u < %zu",
1026 sectorsize, sizeof(struct btrfs_super_block));
1030 min_dev_size = btrfs_min_dev_size(nodesize, mixed, metadata_profile,
1032 /* Check device/block_count after the nodesize is determined */
1033 if (block_count && block_count < min_dev_size) {
1034 error("size %llu is too small to make a usable filesystem",
1036 error("minimum size for btrfs filesystem is %llu",
1040 for (i = saved_optind; i < saved_optind + dev_cnt; i++) {
1044 ret = test_minimum_size(path, min_dev_size);
1046 error("failed to check size for %s: %s",
1047 path, strerror(-ret));
1051 error("'%s' is too small to make a usable filesystem",
1053 error("minimum size for each btrfs device is %llu",
1058 ret = test_num_disk_vs_raid(metadata_profile, data_profile,
1059 dev_cnt, mixed, ssd);
1065 if (!source_dir_set) {
1067 * open without O_EXCL so that the problem should not
1068 * occur by the following processing.
1069 * (btrfs_register_one_device() fails if O_EXCL is on)
1071 fd = open(file, O_RDWR);
1073 error("unable to open %s: %s", file, strerror(errno));
1076 ret = btrfs_prepare_device(fd, file, &dev_block_count,
1078 (zero_end ? PREP_DEVICE_ZERO_END : 0) |
1079 (discard ? PREP_DEVICE_DISCARD : 0) |
1080 (verbose ? PREP_DEVICE_VERBOSE : 0));
1084 if (block_count && block_count > dev_block_count) {
1085 error("%s is smaller than requested size, expected %llu, found %llu",
1087 (unsigned long long)block_count,
1088 (unsigned long long)dev_block_count);
1092 fd = open(file, O_CREAT | O_RDWR,
1093 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
1095 error("unable to open %s: %s", file, strerror(errno));
1099 source_dir_size = size_sourcedir(source_dir, sectorsize,
1100 &num_of_meta_chunks, &size_of_data);
1101 if(block_count < source_dir_size)
1102 block_count = source_dir_size;
1103 ret = zero_output_file(fd, block_count);
1105 error("unable to zero the output file");
1108 /* our "device" is the new image file */
1109 dev_block_count = block_count;
1112 /* To create the first block group and chunk 0 in make_btrfs */
1113 if (dev_block_count < BTRFS_MKFS_SYSTEM_GROUP_SIZE) {
1114 error("device is too small to make filesystem, must be at least %llu",
1115 (unsigned long long)BTRFS_MKFS_SYSTEM_GROUP_SIZE);
1119 if (group_profile_max_safe_loss(metadata_profile) <
1120 group_profile_max_safe_loss(data_profile)){
1121 warning("metadata has lower redundancy than data!\n");
1124 mkfs_cfg.label = label;
1125 memcpy(mkfs_cfg.fs_uuid, fs_uuid, sizeof(mkfs_cfg.fs_uuid));
1126 mkfs_cfg.num_bytes = dev_block_count;
1127 mkfs_cfg.nodesize = nodesize;
1128 mkfs_cfg.sectorsize = sectorsize;
1129 mkfs_cfg.stripesize = stripesize;
1130 mkfs_cfg.features = features;
1132 ret = make_btrfs(fd, &mkfs_cfg);
1134 error("error during mkfs: %s", strerror(-ret));
1138 fs_info = open_ctree_fs_info(file, 0, 0, 0,
1139 OPEN_CTREE_WRITES | OPEN_CTREE_FS_PARTIAL);
1141 error("open ctree failed");
1146 root = fs_info->fs_root;
1147 fs_info->alloc_start = alloc_start;
1149 ret = create_metadata_block_groups(root, mixed, &allocation);
1151 error("failed to create default block groups: %d", ret);
1155 trans = btrfs_start_transaction(root, 1);
1156 if (IS_ERR(trans)) {
1157 error("failed to start transaction");
1161 ret = create_data_block_groups(trans, root, mixed, &allocation);
1163 error("failed to create default data block groups: %d", ret);
1167 ret = make_root_dir(trans, root);
1169 error("failed to setup the root directory: %d", ret);
1173 ret = btrfs_commit_transaction(trans, root);
1175 error("unable to commit transaction: %d", ret);
1179 trans = btrfs_start_transaction(root, 1);
1180 if (IS_ERR(trans)) {
1181 error("failed to start transaction");
1188 while (dev_cnt-- > 0) {
1189 file = argv[optind++];
1192 * open without O_EXCL so that the problem should not
1193 * occur by the following processing.
1194 * (btrfs_register_one_device() fails if O_EXCL is on)
1196 fd = open(file, O_RDWR);
1198 error("unable to open %s: %s", file, strerror(errno));
1201 ret = btrfs_device_already_in_root(root, fd,
1202 BTRFS_SUPER_INFO_OFFSET);
1204 error("skipping duplicate device %s in the filesystem",
1209 ret = btrfs_prepare_device(fd, file, &dev_block_count,
1211 (verbose ? PREP_DEVICE_VERBOSE : 0) |
1212 (zero_end ? PREP_DEVICE_ZERO_END : 0) |
1213 (discard ? PREP_DEVICE_DISCARD : 0));
1218 ret = btrfs_add_to_fsid(trans, root, fd, file, dev_block_count,
1219 sectorsize, sectorsize, sectorsize);
1221 error("unable to add %s to filesystem: %d", file, ret);
1225 struct btrfs_device *device;
1227 device = container_of(fs_info->fs_devices->devices.next,
1228 struct btrfs_device, dev_list);
1229 printf("adding device %s id %llu\n", file,
1230 (unsigned long long)device->devid);
1235 if (!source_dir_set) {
1236 ret = create_raid_groups(trans, root, data_profile,
1237 metadata_profile, mixed, &allocation);
1239 error("unable to create raid groups: %d", ret);
1244 ret = create_tree(trans, root, BTRFS_DATA_RELOC_TREE_OBJECTID);
1246 error("unable to create data reloc tree: %d", ret);
1250 ret = btrfs_commit_transaction(trans, root);
1252 error("unable to commit transaction: %d", ret);
1256 if (source_dir_set) {
1257 trans = btrfs_start_transaction(root, 1);
1258 BUG_ON(IS_ERR(trans));
1259 ret = create_chunks(trans, root,
1260 num_of_meta_chunks, size_of_data,
1263 error("unable to create chunks: %d", ret);
1266 ret = btrfs_commit_transaction(trans, root);
1268 error("transaction commit failed: %d", ret);
1272 ret = btrfs_mkfs_fill_dir(source_dir, root, verbose);
1274 error("error wihle filling filesystem: %d", ret);
1278 ret = cleanup_temp_chunks(fs_info, &allocation, data_profile,
1279 metadata_profile, metadata_profile);
1281 error("failed to cleanup temporary chunks: %d", ret);
1286 char features_buf[64];
1288 printf("Label: %s\n", label);
1289 printf("UUID: %s\n", mkfs_cfg.fs_uuid);
1290 printf("Node size: %u\n", nodesize);
1291 printf("Sector size: %u\n", sectorsize);
1292 printf("Filesystem size: %s\n",
1293 pretty_size(btrfs_super_total_bytes(fs_info->super_copy)));
1294 printf("Block group profiles:\n");
1295 if (allocation.data)
1296 printf(" Data: %-8s %16s\n",
1297 btrfs_group_profile_str(data_profile),
1298 pretty_size(allocation.data));
1299 if (allocation.metadata)
1300 printf(" Metadata: %-8s %16s\n",
1301 btrfs_group_profile_str(metadata_profile),
1302 pretty_size(allocation.metadata));
1303 if (allocation.mixed)
1304 printf(" Data+Metadata: %-8s %16s\n",
1305 btrfs_group_profile_str(data_profile),
1306 pretty_size(allocation.mixed));
1307 printf(" System: %-8s %16s\n",
1308 btrfs_group_profile_str(metadata_profile),
1309 pretty_size(allocation.system));
1310 printf("SSD detected: %s\n", ssd ? "yes" : "no");
1311 btrfs_parse_features_to_string(features_buf, features);
1312 printf("Incompat features: %s", features_buf);
1315 list_all_devices(root);
1319 * The filesystem is now fully set up, commit the remaining changes and
1320 * fix the signature as the last step before closing the devices.
1322 fs_info->finalize_on_close = 1;
1324 close_ret = close_ctree(root);
1327 optind = saved_optind;
1328 dev_cnt = argc - optind;
1329 while (dev_cnt-- > 0) {
1330 file = argv[optind++];
1331 if (is_block_device(file) == 1)
1332 btrfs_register_one_device(file);
1336 btrfs_close_all_devices();