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>
37 #include "transaction.h"
39 #include "list_sort.h"
41 #include "mkfs/common.h"
42 #include "mkfs/rootdir.h"
43 #include "fsfeatures.h"
45 static int verbose = 1;
47 struct mkfs_allocation {
54 static int create_metadata_block_groups(struct btrfs_root *root, int mixed,
55 struct mkfs_allocation *allocation)
57 struct btrfs_fs_info *fs_info = root->fs_info;
58 struct btrfs_trans_handle *trans;
64 trans = btrfs_start_transaction(root, 1);
65 BUG_ON(IS_ERR(trans));
66 bytes_used = btrfs_super_bytes_used(fs_info->super_copy);
68 root->fs_info->system_allocs = 1;
70 * First temporary system chunk must match the chunk layout
71 * created in make_btrfs().
73 ret = btrfs_make_block_group(trans, fs_info, bytes_used,
74 BTRFS_BLOCK_GROUP_SYSTEM,
75 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
76 BTRFS_BLOCK_RESERVED_1M_FOR_SUPER,
77 BTRFS_MKFS_SYSTEM_GROUP_SIZE);
78 allocation->system += BTRFS_MKFS_SYSTEM_GROUP_SIZE;
83 ret = btrfs_alloc_chunk(trans, fs_info,
84 &chunk_start, &chunk_size,
85 BTRFS_BLOCK_GROUP_METADATA |
86 BTRFS_BLOCK_GROUP_DATA);
88 error("no space to allocate data/metadata chunk");
93 ret = btrfs_make_block_group(trans, fs_info, 0,
94 BTRFS_BLOCK_GROUP_METADATA |
95 BTRFS_BLOCK_GROUP_DATA,
96 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
97 chunk_start, chunk_size);
100 allocation->mixed += chunk_size;
102 ret = btrfs_alloc_chunk(trans, fs_info,
103 &chunk_start, &chunk_size,
104 BTRFS_BLOCK_GROUP_METADATA);
105 if (ret == -ENOSPC) {
106 error("no space to allocate metadata chunk");
111 ret = btrfs_make_block_group(trans, fs_info, 0,
112 BTRFS_BLOCK_GROUP_METADATA,
113 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
114 chunk_start, chunk_size);
115 allocation->metadata += chunk_size;
120 root->fs_info->system_allocs = 0;
121 ret = btrfs_commit_transaction(trans, root);
127 static int create_data_block_groups(struct btrfs_trans_handle *trans,
128 struct btrfs_root *root, int mixed,
129 struct mkfs_allocation *allocation)
131 struct btrfs_fs_info *fs_info = root->fs_info;
137 ret = btrfs_alloc_chunk(trans, fs_info,
138 &chunk_start, &chunk_size,
139 BTRFS_BLOCK_GROUP_DATA);
140 if (ret == -ENOSPC) {
141 error("no space to allocate data chunk");
146 ret = btrfs_make_block_group(trans, fs_info, 0,
147 BTRFS_BLOCK_GROUP_DATA,
148 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
149 chunk_start, chunk_size);
150 allocation->data += chunk_size;
159 static int make_root_dir(struct btrfs_trans_handle *trans,
160 struct btrfs_root *root)
162 struct btrfs_key location;
165 ret = btrfs_make_root_dir(trans, root->fs_info->tree_root,
166 BTRFS_ROOT_TREE_DIR_OBJECTID);
169 ret = btrfs_make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID);
172 memcpy(&location, &root->fs_info->fs_root->root_key, sizeof(location));
173 location.offset = (u64)-1;
174 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
176 btrfs_super_root_dir(root->fs_info->super_copy),
177 &location, BTRFS_FT_DIR, 0);
181 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
182 "default", 7, location.objectid,
183 BTRFS_ROOT_TREE_DIR_OBJECTID, 0);
191 static int __recow_root(struct btrfs_trans_handle *trans,
192 struct btrfs_root *root)
194 struct extent_buffer *tmp;
197 if (trans->transid != btrfs_root_generation(&root->root_item)) {
198 extent_buffer_get(root->node);
199 ret = __btrfs_cow_block(trans, root, root->node,
200 NULL, 0, &tmp, 0, 0);
203 free_extent_buffer(tmp);
209 static int recow_roots(struct btrfs_trans_handle *trans,
210 struct btrfs_root *root)
212 struct btrfs_fs_info *info = root->fs_info;
215 ret = __recow_root(trans, info->fs_root);
218 ret = __recow_root(trans, info->tree_root);
221 ret = __recow_root(trans, info->extent_root);
224 ret = __recow_root(trans, info->chunk_root);
227 ret = __recow_root(trans, info->dev_root);
230 ret = __recow_root(trans, info->csum_root);
237 static int create_one_raid_group(struct btrfs_trans_handle *trans,
238 struct btrfs_root *root, u64 type,
239 struct mkfs_allocation *allocation)
242 struct btrfs_fs_info *fs_info = root->fs_info;
247 ret = btrfs_alloc_chunk(trans, fs_info,
248 &chunk_start, &chunk_size, type);
249 if (ret == -ENOSPC) {
250 error("not enough free space to allocate chunk");
256 ret = btrfs_make_block_group(trans, fs_info, 0,
257 type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
258 chunk_start, chunk_size);
260 type &= BTRFS_BLOCK_GROUP_TYPE_MASK;
261 if (type == BTRFS_BLOCK_GROUP_DATA) {
262 allocation->data += chunk_size;
263 } else if (type == BTRFS_BLOCK_GROUP_METADATA) {
264 allocation->metadata += chunk_size;
265 } else if (type == BTRFS_BLOCK_GROUP_SYSTEM) {
266 allocation->system += chunk_size;
268 (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA)) {
269 allocation->mixed += chunk_size;
271 error("unrecognized profile type: 0x%llx",
272 (unsigned long long)type);
279 static int create_raid_groups(struct btrfs_trans_handle *trans,
280 struct btrfs_root *root, u64 data_profile,
281 u64 metadata_profile, int mixed,
282 struct mkfs_allocation *allocation)
286 if (metadata_profile) {
287 u64 meta_flags = BTRFS_BLOCK_GROUP_METADATA;
289 ret = create_one_raid_group(trans, root,
290 BTRFS_BLOCK_GROUP_SYSTEM |
291 metadata_profile, allocation);
296 meta_flags |= BTRFS_BLOCK_GROUP_DATA;
298 ret = create_one_raid_group(trans, root, meta_flags |
299 metadata_profile, allocation);
304 if (!mixed && data_profile) {
305 ret = create_one_raid_group(trans, root,
306 BTRFS_BLOCK_GROUP_DATA |
307 data_profile, allocation);
311 ret = recow_roots(trans, root);
316 static int create_tree(struct btrfs_trans_handle *trans,
317 struct btrfs_root *root, u64 objectid)
319 struct btrfs_key location;
320 struct btrfs_root_item root_item;
321 struct extent_buffer *tmp;
324 ret = btrfs_copy_root(trans, root, root->node, &tmp, objectid);
328 memcpy(&root_item, &root->root_item, sizeof(root_item));
329 btrfs_set_root_bytenr(&root_item, tmp->start);
330 btrfs_set_root_level(&root_item, btrfs_header_level(tmp));
331 btrfs_set_root_generation(&root_item, trans->transid);
332 free_extent_buffer(tmp);
334 location.objectid = objectid;
335 location.type = BTRFS_ROOT_ITEM_KEY;
337 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
338 &location, &root_item);
343 static void print_usage(int ret)
345 printf("Usage: mkfs.btrfs [options] dev [ dev ... ]\n");
346 printf("Options:\n");
347 printf(" allocation profiles:\n");
348 printf("\t-d|--data PROFILE data profile, raid0, raid1, raid5, raid6, raid10, dup or single\n");
349 printf("\t-m|--metadata PROFILE metadata profile, values like for data profile\n");
350 printf("\t-M|--mixed mix metadata and data together\n");
351 printf(" features:\n");
352 printf("\t-n|--nodesize SIZE size of btree nodes\n");
353 printf("\t-s|--sectorsize SIZE data block size (may not be mountable by current kernel)\n");
354 printf("\t-O|--features LIST comma separated list of filesystem features (use '-O list-all' to list features)\n");
355 printf("\t-L|--label LABEL set the filesystem label\n");
356 printf("\t-U|--uuid UUID specify the filesystem UUID (must be unique)\n");
357 printf(" creation:\n");
358 printf("\t-b|--byte-count SIZE set filesystem size to SIZE (on the first device)\n");
359 printf("\t-r|--rootdir DIR copy files from DIR to the image root directory\n");
360 printf("\t--shrink (with --rootdir) shrink the filled filesystem to minimal size\n");
361 printf("\t-K|--nodiscard do not perform whole device TRIM\n");
362 printf("\t-f|--force force overwrite of existing filesystem\n");
363 printf(" general:\n");
364 printf("\t-q|--quiet no messages except errors\n");
365 printf("\t-V|--version print the mkfs.btrfs version and exit\n");
366 printf("\t--help print this help and exit\n");
367 printf(" deprecated:\n");
368 printf("\t-A|--alloc-start START the offset to start the filesystem\n");
369 printf("\t-l|--leafsize SIZE deprecated, alias for nodesize\n");
373 static u64 parse_profile(const char *s)
375 if (strcasecmp(s, "raid0") == 0) {
376 return BTRFS_BLOCK_GROUP_RAID0;
377 } else if (strcasecmp(s, "raid1") == 0) {
378 return BTRFS_BLOCK_GROUP_RAID1;
379 } else if (strcasecmp(s, "raid5") == 0) {
380 return BTRFS_BLOCK_GROUP_RAID5;
381 } else if (strcasecmp(s, "raid6") == 0) {
382 return BTRFS_BLOCK_GROUP_RAID6;
383 } else if (strcasecmp(s, "raid10") == 0) {
384 return BTRFS_BLOCK_GROUP_RAID10;
385 } else if (strcasecmp(s, "dup") == 0) {
386 return BTRFS_BLOCK_GROUP_DUP;
387 } else if (strcasecmp(s, "single") == 0) {
390 error("unknown profile %s", s);
397 static char *parse_label(const char *input)
399 int len = strlen(input);
401 if (len >= BTRFS_LABEL_SIZE) {
402 error("label %s is too long (max %d)", input,
403 BTRFS_LABEL_SIZE - 1);
406 return strdup(input);
409 static int zero_output_file(int out_fd, u64 size)
417 memset(buf, 0, SZ_4K);
419 /* Only zero out the first 1M */
420 loop_num = SZ_1M / SZ_4K;
421 for (i = 0; i < loop_num; i++) {
422 written = pwrite64(out_fd, buf, SZ_4K, location);
423 if (written != SZ_4K)
428 /* Then enlarge the file to size */
429 written = pwrite64(out_fd, buf, 1, size - 1);
435 static int is_ssd(const char *file)
438 char wholedisk[PATH_MAX];
439 char sysfs_path[PATH_MAX];
445 probe = blkid_new_probe_from_filename(file);
449 /* Device number of this disk (possibly a partition) */
450 devno = blkid_probe_get_devno(probe);
452 blkid_free_probe(probe);
456 /* Get whole disk name (not full path) for this devno */
457 ret = blkid_devno_to_wholedisk(devno,
458 wholedisk, sizeof(wholedisk), NULL);
460 blkid_free_probe(probe);
464 snprintf(sysfs_path, PATH_MAX, "/sys/block/%s/queue/rotational",
467 blkid_free_probe(probe);
469 fd = open(sysfs_path, O_RDONLY);
474 if (read(fd, &rotational, 1) < 1) {
480 return rotational == '0';
483 static int _cmp_device_by_id(void *priv, struct list_head *a,
486 return list_entry(a, struct btrfs_device, dev_list)->devid -
487 list_entry(b, struct btrfs_device, dev_list)->devid;
490 static void list_all_devices(struct btrfs_root *root)
492 struct btrfs_fs_devices *fs_devices;
493 struct btrfs_device *device;
494 int number_of_devices = 0;
495 u64 total_block_count = 0;
497 fs_devices = root->fs_info->fs_devices;
499 list_for_each_entry(device, &fs_devices->devices, dev_list)
502 list_sort(NULL, &fs_devices->devices, _cmp_device_by_id);
504 printf("Number of devices: %d\n", number_of_devices);
505 /* printf("Total devices size: %10s\n", */
506 /* pretty_size(total_block_count)); */
507 printf("Devices:\n");
508 printf(" ID SIZE PATH\n");
509 list_for_each_entry(device, &fs_devices->devices, dev_list) {
510 printf(" %3llu %10s %s\n",
512 pretty_size(device->total_bytes),
514 total_block_count += device->total_bytes;
520 static int is_temp_block_group(struct extent_buffer *node,
521 struct btrfs_block_group_item *bgi,
522 u64 data_profile, u64 meta_profile,
525 u64 flag = btrfs_disk_block_group_flags(node, bgi);
526 u64 flag_type = flag & BTRFS_BLOCK_GROUP_TYPE_MASK;
527 u64 flag_profile = flag & BTRFS_BLOCK_GROUP_PROFILE_MASK;
528 u64 used = btrfs_disk_block_group_used(node, bgi);
531 * Chunks meets all the following conditions is a temp chunk
533 * Temp chunk is always empty.
535 * 2) profile mismatch with mkfs profile.
536 * Temp chunk is always in SINGLE
538 * 3) Size differs with mkfs_alloc
539 * Special case for SINGLE/SINGLE btrfs.
540 * In that case, temp data chunk and real data chunk are always empty.
541 * So we need to use mkfs_alloc to be sure which chunk is the newly
544 * Normally, new chunk size is equal to mkfs one (One chunk)
545 * If it has multiple chunks, we just refuse to delete any one.
546 * As they are all single, so no real problem will happen.
547 * So only use condition 1) and 2) to judge them.
552 case BTRFS_BLOCK_GROUP_DATA:
553 case BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA:
554 data_profile &= BTRFS_BLOCK_GROUP_PROFILE_MASK;
555 if (flag_profile != data_profile)
558 case BTRFS_BLOCK_GROUP_METADATA:
559 meta_profile &= BTRFS_BLOCK_GROUP_PROFILE_MASK;
560 if (flag_profile != meta_profile)
563 case BTRFS_BLOCK_GROUP_SYSTEM:
564 sys_profile &= BTRFS_BLOCK_GROUP_PROFILE_MASK;
565 if (flag_profile != sys_profile)
572 /* Note: if current is a block group, it will skip it anyway */
573 static int next_block_group(struct btrfs_root *root,
574 struct btrfs_path *path)
576 struct btrfs_key key;
580 ret = btrfs_next_item(root, path);
584 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
585 if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY)
592 /* This function will cleanup */
593 static int cleanup_temp_chunks(struct btrfs_fs_info *fs_info,
594 struct mkfs_allocation *alloc,
595 u64 data_profile, u64 meta_profile,
598 struct btrfs_trans_handle *trans = NULL;
599 struct btrfs_block_group_item *bgi;
600 struct btrfs_root *root = fs_info->extent_root;
601 struct btrfs_key key;
602 struct btrfs_key found_key;
603 struct btrfs_path path;
606 btrfs_init_path(&path);
607 trans = btrfs_start_transaction(root, 1);
608 BUG_ON(IS_ERR(trans));
611 key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
616 * as the rest of the loop may modify the tree, we need to
617 * start a new search each time.
619 ret = btrfs_search_slot(trans, root, &key, &path, 0, 0);
622 /* Don't pollute ret for >0 case */
626 btrfs_item_key_to_cpu(path.nodes[0], &found_key,
628 if (found_key.objectid < key.objectid)
630 if (found_key.type != BTRFS_BLOCK_GROUP_ITEM_KEY) {
631 ret = next_block_group(root, &path);
638 btrfs_item_key_to_cpu(path.nodes[0], &found_key,
642 bgi = btrfs_item_ptr(path.nodes[0], path.slots[0],
643 struct btrfs_block_group_item);
644 if (is_temp_block_group(path.nodes[0], bgi,
645 data_profile, meta_profile,
647 u64 flags = btrfs_disk_block_group_flags(path.nodes[0],
650 ret = btrfs_free_block_group(trans, fs_info,
651 found_key.objectid, found_key.offset);
655 if ((flags & BTRFS_BLOCK_GROUP_TYPE_MASK) ==
656 BTRFS_BLOCK_GROUP_DATA)
657 alloc->data -= found_key.offset;
658 else if ((flags & BTRFS_BLOCK_GROUP_TYPE_MASK) ==
659 BTRFS_BLOCK_GROUP_METADATA)
660 alloc->metadata -= found_key.offset;
661 else if ((flags & BTRFS_BLOCK_GROUP_TYPE_MASK) ==
662 BTRFS_BLOCK_GROUP_SYSTEM)
663 alloc->system -= found_key.offset;
664 else if ((flags & BTRFS_BLOCK_GROUP_TYPE_MASK) ==
665 (BTRFS_BLOCK_GROUP_METADATA |
666 BTRFS_BLOCK_GROUP_DATA))
667 alloc->mixed -= found_key.offset;
669 btrfs_release_path(&path);
670 key.objectid = found_key.objectid + found_key.offset;
674 btrfs_commit_transaction(trans, root);
675 btrfs_release_path(&path);
680 * Just update chunk allocation info, since --rootdir may allocate new
681 * chunks which is not updated in @allocation structure.
683 static void update_chunk_allocation(struct btrfs_fs_info *fs_info,
684 struct mkfs_allocation *allocation)
686 struct btrfs_block_group_cache *bg_cache;
687 const u64 mixed_flag = BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA;
688 u64 search_start = 0;
690 allocation->mixed = 0;
691 allocation->data = 0;
692 allocation->metadata = 0;
693 allocation->system = 0;
695 bg_cache = btrfs_lookup_first_block_group(fs_info,
699 if ((bg_cache->flags & mixed_flag) == mixed_flag)
700 allocation->mixed += bg_cache->key.offset;
701 else if (bg_cache->flags & BTRFS_BLOCK_GROUP_DATA)
702 allocation->data += bg_cache->key.offset;
703 else if (bg_cache->flags & BTRFS_BLOCK_GROUP_METADATA)
704 allocation->metadata += bg_cache->key.offset;
706 allocation->system += bg_cache->key.offset;
707 search_start = bg_cache->key.objectid + bg_cache->key.offset;
711 int main(int argc, char **argv)
714 struct btrfs_root *root;
715 struct btrfs_fs_info *fs_info;
716 struct btrfs_trans_handle *trans;
719 u64 dev_block_count = 0;
721 u64 metadata_profile = 0;
722 u64 data_profile = 0;
723 u32 nodesize = max_t(u32, sysconf(_SC_PAGESIZE),
724 BTRFS_MKFS_DEFAULT_NODE_SIZE);
725 u32 sectorsize = 4096;
726 u32 stripesize = 4096;
733 int nodesize_forced = 0;
734 int data_profile_opt = 0;
735 int metadata_profile_opt = 0;
738 int force_overwrite = 0;
739 char *source_dir = NULL;
740 bool source_dir_set = false;
741 bool shrink_rootdir = false;
742 u64 source_dir_size = 0;
747 char fs_uuid[BTRFS_UUID_UNPARSED_SIZE] = { 0 };
748 u64 features = BTRFS_MKFS_DEFAULT_FEATURES;
749 struct mkfs_allocation allocation = { 0 };
750 struct btrfs_mkfs_config mkfs_cfg;
754 enum { GETOPT_VAL_SHRINK = 257 };
755 static const struct option long_options[] = {
756 { "alloc-start", required_argument, NULL, 'A'},
757 { "byte-count", required_argument, NULL, 'b' },
758 { "force", no_argument, NULL, 'f' },
759 { "leafsize", required_argument, NULL, 'l' },
760 { "label", required_argument, NULL, 'L'},
761 { "metadata", required_argument, NULL, 'm' },
762 { "mixed", no_argument, NULL, 'M' },
763 { "nodesize", required_argument, NULL, 'n' },
764 { "sectorsize", required_argument, NULL, 's' },
765 { "data", required_argument, NULL, 'd' },
766 { "version", no_argument, NULL, 'V' },
767 { "rootdir", required_argument, NULL, 'r' },
768 { "nodiscard", no_argument, NULL, 'K' },
769 { "features", required_argument, NULL, 'O' },
770 { "uuid", required_argument, NULL, 'U' },
771 { "quiet", 0, NULL, 'q' },
772 { "shrink", no_argument, NULL, GETOPT_VAL_SHRINK },
773 { "help", no_argument, NULL, GETOPT_VAL_HELP },
777 c = getopt_long(argc, argv, "A:b:fl:n:s:m:d:L:O:r:U:VMKq",
783 alloc_start = parse_size(optarg);
789 data_profile = parse_profile(optarg);
790 data_profile_opt = 1;
793 warning("--leafsize is deprecated, use --nodesize");
796 nodesize = parse_size(optarg);
800 label = parse_label(optarg);
803 metadata_profile = parse_profile(optarg);
804 metadata_profile_opt = 1;
810 char *orig = strdup(optarg);
813 tmp = btrfs_parse_fs_features(tmp, &features);
815 error("unrecognized filesystem feature '%s'",
821 if (features & BTRFS_FEATURE_LIST_ALL) {
822 btrfs_list_all_fs_features(0);
828 sectorsize = parse_size(optarg);
831 block_count = parse_size(optarg);
835 printf("mkfs.btrfs, part of %s\n",
840 source_dir_set = true;
843 strncpy(fs_uuid, optarg,
844 BTRFS_UUID_UNPARSED_SIZE - 1);
852 case GETOPT_VAL_SHRINK:
853 shrink_rootdir = true;
855 case GETOPT_VAL_HELP:
857 print_usage(c != GETOPT_VAL_HELP);
862 printf("%s\n", PACKAGE_STRING);
863 printf("See %s for more information.\n\n", PACKAGE_URL);
866 sectorsize = max(sectorsize, (u32)sysconf(_SC_PAGESIZE));
867 stripesize = sectorsize;
868 saved_optind = optind;
869 dev_cnt = argc - optind;
873 if (source_dir_set && dev_cnt > 1) {
874 error("the option -r is limited to a single device");
877 if (shrink_rootdir && !source_dir_set) {
878 error("the option --shrink must be used with --rootdir");
885 if (uuid_parse(fs_uuid, dummy_uuid) != 0) {
886 error("could not parse UUID: %s", fs_uuid);
889 if (!test_uuid_unique(fs_uuid)) {
890 error("non-unique UUID: %s", fs_uuid);
895 while (dev_cnt-- > 0) {
896 file = argv[optind++];
897 if (source_dir_set && is_path_exist(file) == 0)
899 else if (is_block_device(file) == 1)
900 ret = test_dev_for_mkfs(file, force_overwrite);
902 ret = test_status_for_mkfs(file, force_overwrite);
908 optind = saved_optind;
909 dev_cnt = argc - optind;
911 file = argv[optind++];
915 * Set default profiles according to number of added devices.
916 * For mixed groups defaults are single/single.
919 if (!metadata_profile_opt) {
920 if (dev_cnt == 1 && ssd && verbose)
921 printf("Detected a SSD, turning off metadata "
922 "duplication. Mkfs with -m dup if you want to "
923 "force metadata duplication.\n");
925 metadata_profile = (dev_cnt > 1) ?
926 BTRFS_BLOCK_GROUP_RAID1 : (ssd) ?
927 0: BTRFS_BLOCK_GROUP_DUP;
929 if (!data_profile_opt) {
930 data_profile = (dev_cnt > 1) ?
931 BTRFS_BLOCK_GROUP_RAID0 : 0; /* raid0 or single */
934 u32 best_nodesize = max_t(u32, sysconf(_SC_PAGESIZE), sectorsize);
936 if (metadata_profile_opt || data_profile_opt) {
937 if (metadata_profile != data_profile) {
939 "with mixed block groups data and metadata profiles must be the same");
944 if (!nodesize_forced)
945 nodesize = best_nodesize;
949 * FS features that can be set by other means than -O
950 * just set the bit here
953 features |= BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS;
955 if ((data_profile | metadata_profile) &
956 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
957 features |= BTRFS_FEATURE_INCOMPAT_RAID56;
960 if (btrfs_check_nodesize(nodesize, sectorsize,
964 if (sectorsize < sizeof(struct btrfs_super_block)) {
965 error("sectorsize smaller than superblock: %u < %zu",
966 sectorsize, sizeof(struct btrfs_super_block));
970 min_dev_size = btrfs_min_dev_size(nodesize, mixed, metadata_profile,
973 * Enlarge the destination file or create a new one, using the size
974 * calculated from source dir.
976 * This must be done before minimal device size checks.
978 if (source_dir_set) {
982 if (is_path_exist(file) == 0)
985 fd = open(file, oflags, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP |
988 error("unable to open %s: %s", file, strerror(errno));
991 ret = fstat(fd, &statbuf);
993 error("unable to stat %s: %s", file, strerror(errno));
999 * Block_count not specified, use file/device size first.
1000 * Or we will always use source_dir_size calculated for mkfs.
1003 block_count = btrfs_device_size(fd, &statbuf);
1004 source_dir_size = btrfs_mkfs_size_dir(source_dir, sectorsize,
1005 min_dev_size, metadata_profile, data_profile);
1006 if (block_count < source_dir_size)
1007 block_count = source_dir_size;
1008 ret = zero_output_file(fd, block_count);
1010 error("unable to zero the output file");
1014 /* our "device" is the new image file */
1015 dev_block_count = block_count;
1018 /* Check device/block_count after the nodesize is determined */
1019 if (block_count && block_count < min_dev_size) {
1020 error("size %llu is too small to make a usable filesystem",
1022 error("minimum size for btrfs filesystem is %llu",
1026 for (i = saved_optind; i < saved_optind + dev_cnt; i++) {
1030 ret = test_minimum_size(path, min_dev_size);
1032 error("failed to check size for %s: %s",
1033 path, strerror(-ret));
1037 error("'%s' is too small to make a usable filesystem",
1039 error("minimum size for each btrfs device is %llu",
1044 ret = test_num_disk_vs_raid(metadata_profile, data_profile,
1045 dev_cnt, mixed, ssd);
1052 * Open without O_EXCL so that the problem should not occur by the
1053 * following operation in kernel:
1054 * (btrfs_register_one_device() fails if O_EXCL is on)
1056 fd = open(file, O_RDWR);
1058 error("unable to open %s: %s", file, strerror(errno));
1061 ret = btrfs_prepare_device(fd, file, &dev_block_count, block_count,
1062 (zero_end ? PREP_DEVICE_ZERO_END : 0) |
1063 (discard ? PREP_DEVICE_DISCARD : 0) |
1064 (verbose ? PREP_DEVICE_VERBOSE : 0));
1067 if (block_count && block_count > dev_block_count) {
1068 error("%s is smaller than requested size, expected %llu, found %llu",
1069 file, (unsigned long long)block_count,
1070 (unsigned long long)dev_block_count);
1074 /* To create the first block group and chunk 0 in make_btrfs */
1075 if (dev_block_count < BTRFS_MKFS_SYSTEM_GROUP_SIZE) {
1076 error("device is too small to make filesystem, must be at least %llu",
1077 (unsigned long long)BTRFS_MKFS_SYSTEM_GROUP_SIZE);
1081 if (group_profile_max_safe_loss(metadata_profile) <
1082 group_profile_max_safe_loss(data_profile)){
1083 warning("metadata has lower redundancy than data!\n");
1086 mkfs_cfg.label = label;
1087 memcpy(mkfs_cfg.fs_uuid, fs_uuid, sizeof(mkfs_cfg.fs_uuid));
1088 mkfs_cfg.num_bytes = dev_block_count;
1089 mkfs_cfg.nodesize = nodesize;
1090 mkfs_cfg.sectorsize = sectorsize;
1091 mkfs_cfg.stripesize = stripesize;
1092 mkfs_cfg.features = features;
1094 ret = make_btrfs(fd, &mkfs_cfg);
1096 error("error during mkfs: %s", strerror(-ret));
1100 fs_info = open_ctree_fs_info(file, 0, 0, 0,
1101 OPEN_CTREE_WRITES | OPEN_CTREE_FS_PARTIAL);
1103 error("open ctree failed");
1108 root = fs_info->fs_root;
1109 fs_info->alloc_start = alloc_start;
1111 ret = create_metadata_block_groups(root, mixed, &allocation);
1113 error("failed to create default block groups: %d", ret);
1117 trans = btrfs_start_transaction(root, 1);
1118 if (IS_ERR(trans)) {
1119 error("failed to start transaction");
1123 ret = create_data_block_groups(trans, root, mixed, &allocation);
1125 error("failed to create default data block groups: %d", ret);
1129 ret = make_root_dir(trans, root);
1131 error("failed to setup the root directory: %d", ret);
1135 ret = btrfs_commit_transaction(trans, root);
1137 error("unable to commit transaction: %d", ret);
1141 trans = btrfs_start_transaction(root, 1);
1142 if (IS_ERR(trans)) {
1143 error("failed to start transaction");
1150 while (dev_cnt-- > 0) {
1151 file = argv[optind++];
1154 * open without O_EXCL so that the problem should not
1155 * occur by the following processing.
1156 * (btrfs_register_one_device() fails if O_EXCL is on)
1158 fd = open(file, O_RDWR);
1160 error("unable to open %s: %s", file, strerror(errno));
1163 ret = btrfs_device_already_in_root(root, fd,
1164 BTRFS_SUPER_INFO_OFFSET);
1166 error("skipping duplicate device %s in the filesystem",
1171 ret = btrfs_prepare_device(fd, file, &dev_block_count,
1173 (verbose ? PREP_DEVICE_VERBOSE : 0) |
1174 (zero_end ? PREP_DEVICE_ZERO_END : 0) |
1175 (discard ? PREP_DEVICE_DISCARD : 0));
1180 ret = btrfs_add_to_fsid(trans, root, fd, file, dev_block_count,
1181 sectorsize, sectorsize, sectorsize);
1183 error("unable to add %s to filesystem: %d", file, ret);
1187 struct btrfs_device *device;
1189 device = container_of(fs_info->fs_devices->devices.next,
1190 struct btrfs_device, dev_list);
1191 printf("adding device %s id %llu\n", file,
1192 (unsigned long long)device->devid);
1197 ret = create_raid_groups(trans, root, data_profile,
1198 metadata_profile, mixed, &allocation);
1200 error("unable to create raid groups: %d", ret);
1204 ret = create_tree(trans, root, BTRFS_DATA_RELOC_TREE_OBJECTID);
1206 error("unable to create data reloc tree: %d", ret);
1210 ret = btrfs_commit_transaction(trans, root);
1212 error("unable to commit transaction: %d", ret);
1216 ret = cleanup_temp_chunks(fs_info, &allocation, data_profile,
1217 metadata_profile, metadata_profile);
1219 error("failed to cleanup temporary chunks: %d", ret);
1223 if (source_dir_set) {
1224 ret = btrfs_mkfs_fill_dir(source_dir, root, verbose);
1226 error("error wihle filling filesystem: %d", ret);
1229 if (shrink_rootdir) {
1230 ret = btrfs_mkfs_shrink_fs(fs_info, &shrink_size,
1233 error("error while shrinking filesystem: %d",
1241 char features_buf[64];
1243 update_chunk_allocation(fs_info, &allocation);
1244 printf("Label: %s\n", label);
1245 printf("UUID: %s\n", mkfs_cfg.fs_uuid);
1246 printf("Node size: %u\n", nodesize);
1247 printf("Sector size: %u\n", sectorsize);
1248 printf("Filesystem size: %s\n",
1249 pretty_size(btrfs_super_total_bytes(fs_info->super_copy)));
1250 printf("Block group profiles:\n");
1251 if (allocation.data)
1252 printf(" Data: %-8s %16s\n",
1253 btrfs_group_profile_str(data_profile),
1254 pretty_size(allocation.data));
1255 if (allocation.metadata)
1256 printf(" Metadata: %-8s %16s\n",
1257 btrfs_group_profile_str(metadata_profile),
1258 pretty_size(allocation.metadata));
1259 if (allocation.mixed)
1260 printf(" Data+Metadata: %-8s %16s\n",
1261 btrfs_group_profile_str(data_profile),
1262 pretty_size(allocation.mixed));
1263 printf(" System: %-8s %16s\n",
1264 btrfs_group_profile_str(metadata_profile),
1265 pretty_size(allocation.system));
1266 printf("SSD detected: %s\n", ssd ? "yes" : "no");
1267 btrfs_parse_features_to_string(features_buf, features);
1268 printf("Incompat features: %s", features_buf);
1271 list_all_devices(root);
1275 * The filesystem is now fully set up, commit the remaining changes and
1276 * fix the signature as the last step before closing the devices.
1278 fs_info->finalize_on_close = 1;
1280 close_ret = close_ctree(root);
1283 optind = saved_optind;
1284 dev_cnt = argc - optind;
1285 while (dev_cnt-- > 0) {
1286 file = argv[optind++];
1287 if (is_block_device(file) == 1)
1288 btrfs_register_one_device(file);
1292 btrfs_close_all_devices();