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_BLOCK_RESERVED_1M_FOR_SUPER,
76 BTRFS_MKFS_SYSTEM_GROUP_SIZE);
77 allocation->system += BTRFS_MKFS_SYSTEM_GROUP_SIZE;
82 ret = btrfs_alloc_chunk(trans, fs_info,
83 &chunk_start, &chunk_size,
84 BTRFS_BLOCK_GROUP_METADATA |
85 BTRFS_BLOCK_GROUP_DATA);
87 error("no space to allocate data/metadata chunk");
92 ret = btrfs_make_block_group(trans, fs_info, 0,
93 BTRFS_BLOCK_GROUP_METADATA |
94 BTRFS_BLOCK_GROUP_DATA,
95 chunk_start, chunk_size);
98 allocation->mixed += chunk_size;
100 ret = btrfs_alloc_chunk(trans, fs_info,
101 &chunk_start, &chunk_size,
102 BTRFS_BLOCK_GROUP_METADATA);
103 if (ret == -ENOSPC) {
104 error("no space to allocate metadata chunk");
109 ret = btrfs_make_block_group(trans, fs_info, 0,
110 BTRFS_BLOCK_GROUP_METADATA,
111 chunk_start, chunk_size);
112 allocation->metadata += chunk_size;
117 root->fs_info->system_allocs = 0;
118 ret = btrfs_commit_transaction(trans, root);
124 static int create_data_block_groups(struct btrfs_trans_handle *trans,
125 struct btrfs_root *root, int mixed,
126 struct mkfs_allocation *allocation)
128 struct btrfs_fs_info *fs_info = root->fs_info;
134 ret = btrfs_alloc_chunk(trans, fs_info,
135 &chunk_start, &chunk_size,
136 BTRFS_BLOCK_GROUP_DATA);
137 if (ret == -ENOSPC) {
138 error("no space to allocate data chunk");
143 ret = btrfs_make_block_group(trans, fs_info, 0,
144 BTRFS_BLOCK_GROUP_DATA,
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, chunk_start, chunk_size);
255 type &= BTRFS_BLOCK_GROUP_TYPE_MASK;
256 if (type == BTRFS_BLOCK_GROUP_DATA) {
257 allocation->data += chunk_size;
258 } else if (type == BTRFS_BLOCK_GROUP_METADATA) {
259 allocation->metadata += chunk_size;
260 } else if (type == BTRFS_BLOCK_GROUP_SYSTEM) {
261 allocation->system += chunk_size;
263 (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA)) {
264 allocation->mixed += chunk_size;
266 error("unrecognized profile type: 0x%llx",
267 (unsigned long long)type);
274 static int create_raid_groups(struct btrfs_trans_handle *trans,
275 struct btrfs_root *root, u64 data_profile,
276 u64 metadata_profile, int mixed,
277 struct mkfs_allocation *allocation)
281 if (metadata_profile) {
282 u64 meta_flags = BTRFS_BLOCK_GROUP_METADATA;
284 ret = create_one_raid_group(trans, root,
285 BTRFS_BLOCK_GROUP_SYSTEM |
286 metadata_profile, allocation);
291 meta_flags |= BTRFS_BLOCK_GROUP_DATA;
293 ret = create_one_raid_group(trans, root, meta_flags |
294 metadata_profile, allocation);
299 if (!mixed && data_profile) {
300 ret = create_one_raid_group(trans, root,
301 BTRFS_BLOCK_GROUP_DATA |
302 data_profile, allocation);
306 ret = recow_roots(trans, root);
311 static int create_tree(struct btrfs_trans_handle *trans,
312 struct btrfs_root *root, u64 objectid)
314 struct btrfs_key location;
315 struct btrfs_root_item root_item;
316 struct extent_buffer *tmp;
319 ret = btrfs_copy_root(trans, root, root->node, &tmp, objectid);
323 memcpy(&root_item, &root->root_item, sizeof(root_item));
324 btrfs_set_root_bytenr(&root_item, tmp->start);
325 btrfs_set_root_level(&root_item, btrfs_header_level(tmp));
326 btrfs_set_root_generation(&root_item, trans->transid);
327 free_extent_buffer(tmp);
329 location.objectid = objectid;
330 location.type = BTRFS_ROOT_ITEM_KEY;
332 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
333 &location, &root_item);
338 static void print_usage(int ret)
340 printf("Usage: mkfs.btrfs [options] dev [ dev ... ]\n");
341 printf("Options:\n");
342 printf(" allocation profiles:\n");
343 printf("\t-d|--data PROFILE data profile, raid0, raid1, raid5, raid6, raid10, dup or single\n");
344 printf("\t-m|--metadata PROFILE metadata profile, values like for data profile\n");
345 printf("\t-M|--mixed mix metadata and data together\n");
346 printf(" features:\n");
347 printf("\t-n|--nodesize SIZE size of btree nodes\n");
348 printf("\t-s|--sectorsize SIZE data block size (may not be mountable by current kernel)\n");
349 printf("\t-O|--features LIST comma separated list of filesystem features (use '-O list-all' to list features)\n");
350 printf("\t-L|--label LABEL set the filesystem label\n");
351 printf("\t-U|--uuid UUID specify the filesystem UUID (must be unique)\n");
352 printf(" creation:\n");
353 printf("\t-b|--byte-count SIZE set filesystem size to SIZE (on the first device)\n");
354 printf("\t-r|--rootdir DIR copy files from DIR to the image root directory\n");
355 printf("\t--shrink (with --rootdir) shrink the filled filesystem to minimal size\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 zero_output_file(int out_fd, u64 size)
412 memset(buf, 0, SZ_4K);
414 /* Only zero out the first 1M */
415 loop_num = SZ_1M / SZ_4K;
416 for (i = 0; i < loop_num; i++) {
417 written = pwrite64(out_fd, buf, SZ_4K, location);
418 if (written != SZ_4K)
423 /* Then enlarge the file to size */
424 written = pwrite64(out_fd, buf, 1, size - 1);
430 static int is_ssd(const char *file)
433 char wholedisk[PATH_MAX];
434 char sysfs_path[PATH_MAX];
440 probe = blkid_new_probe_from_filename(file);
444 /* Device number of this disk (possibly a partition) */
445 devno = blkid_probe_get_devno(probe);
447 blkid_free_probe(probe);
451 /* Get whole disk name (not full path) for this devno */
452 ret = blkid_devno_to_wholedisk(devno,
453 wholedisk, sizeof(wholedisk), NULL);
455 blkid_free_probe(probe);
459 snprintf(sysfs_path, PATH_MAX, "/sys/block/%s/queue/rotational",
462 blkid_free_probe(probe);
464 fd = open(sysfs_path, O_RDONLY);
469 if (read(fd, &rotational, 1) < 1) {
475 return rotational == '0';
478 static int _cmp_device_by_id(void *priv, struct list_head *a,
481 return list_entry(a, struct btrfs_device, dev_list)->devid -
482 list_entry(b, struct btrfs_device, dev_list)->devid;
485 static void list_all_devices(struct btrfs_root *root)
487 struct btrfs_fs_devices *fs_devices;
488 struct btrfs_device *device;
489 int number_of_devices = 0;
490 u64 total_block_count = 0;
492 fs_devices = root->fs_info->fs_devices;
494 list_for_each_entry(device, &fs_devices->devices, dev_list)
497 list_sort(NULL, &fs_devices->devices, _cmp_device_by_id);
499 printf("Number of devices: %d\n", number_of_devices);
500 /* printf("Total devices size: %10s\n", */
501 /* pretty_size(total_block_count)); */
502 printf("Devices:\n");
503 printf(" ID SIZE PATH\n");
504 list_for_each_entry(device, &fs_devices->devices, dev_list) {
505 printf(" %3llu %10s %s\n",
507 pretty_size(device->total_bytes),
509 total_block_count += device->total_bytes;
515 static int is_temp_block_group(struct extent_buffer *node,
516 struct btrfs_block_group_item *bgi,
517 u64 data_profile, u64 meta_profile,
520 u64 flag = btrfs_disk_block_group_flags(node, bgi);
521 u64 flag_type = flag & BTRFS_BLOCK_GROUP_TYPE_MASK;
522 u64 flag_profile = flag & BTRFS_BLOCK_GROUP_PROFILE_MASK;
523 u64 used = btrfs_disk_block_group_used(node, bgi);
526 * Chunks meets all the following conditions is a temp chunk
528 * Temp chunk is always empty.
530 * 2) profile mismatch with mkfs profile.
531 * Temp chunk is always in SINGLE
533 * 3) Size differs with mkfs_alloc
534 * Special case for SINGLE/SINGLE btrfs.
535 * In that case, temp data chunk and real data chunk are always empty.
536 * So we need to use mkfs_alloc to be sure which chunk is the newly
539 * Normally, new chunk size is equal to mkfs one (One chunk)
540 * If it has multiple chunks, we just refuse to delete any one.
541 * As they are all single, so no real problem will happen.
542 * So only use condition 1) and 2) to judge them.
547 case BTRFS_BLOCK_GROUP_DATA:
548 case BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA:
549 data_profile &= BTRFS_BLOCK_GROUP_PROFILE_MASK;
550 if (flag_profile != data_profile)
553 case BTRFS_BLOCK_GROUP_METADATA:
554 meta_profile &= BTRFS_BLOCK_GROUP_PROFILE_MASK;
555 if (flag_profile != meta_profile)
558 case BTRFS_BLOCK_GROUP_SYSTEM:
559 sys_profile &= BTRFS_BLOCK_GROUP_PROFILE_MASK;
560 if (flag_profile != sys_profile)
567 /* Note: if current is a block group, it will skip it anyway */
568 static int next_block_group(struct btrfs_root *root,
569 struct btrfs_path *path)
571 struct btrfs_key key;
575 ret = btrfs_next_item(root, path);
579 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
580 if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY)
587 /* This function will cleanup */
588 static int cleanup_temp_chunks(struct btrfs_fs_info *fs_info,
589 struct mkfs_allocation *alloc,
590 u64 data_profile, u64 meta_profile,
593 struct btrfs_trans_handle *trans = NULL;
594 struct btrfs_block_group_item *bgi;
595 struct btrfs_root *root = fs_info->extent_root;
596 struct btrfs_key key;
597 struct btrfs_key found_key;
598 struct btrfs_path path;
601 btrfs_init_path(&path);
602 trans = btrfs_start_transaction(root, 1);
603 BUG_ON(IS_ERR(trans));
606 key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
611 * as the rest of the loop may modify the tree, we need to
612 * start a new search each time.
614 ret = btrfs_search_slot(trans, root, &key, &path, 0, 0);
617 /* Don't pollute ret for >0 case */
621 btrfs_item_key_to_cpu(path.nodes[0], &found_key,
623 if (found_key.objectid < key.objectid)
625 if (found_key.type != BTRFS_BLOCK_GROUP_ITEM_KEY) {
626 ret = next_block_group(root, &path);
633 btrfs_item_key_to_cpu(path.nodes[0], &found_key,
637 bgi = btrfs_item_ptr(path.nodes[0], path.slots[0],
638 struct btrfs_block_group_item);
639 if (is_temp_block_group(path.nodes[0], bgi,
640 data_profile, meta_profile,
642 u64 flags = btrfs_disk_block_group_flags(path.nodes[0],
645 ret = btrfs_free_block_group(trans, fs_info,
646 found_key.objectid, found_key.offset);
650 if ((flags & BTRFS_BLOCK_GROUP_TYPE_MASK) ==
651 BTRFS_BLOCK_GROUP_DATA)
652 alloc->data -= found_key.offset;
653 else if ((flags & BTRFS_BLOCK_GROUP_TYPE_MASK) ==
654 BTRFS_BLOCK_GROUP_METADATA)
655 alloc->metadata -= found_key.offset;
656 else if ((flags & BTRFS_BLOCK_GROUP_TYPE_MASK) ==
657 BTRFS_BLOCK_GROUP_SYSTEM)
658 alloc->system -= found_key.offset;
659 else if ((flags & BTRFS_BLOCK_GROUP_TYPE_MASK) ==
660 (BTRFS_BLOCK_GROUP_METADATA |
661 BTRFS_BLOCK_GROUP_DATA))
662 alloc->mixed -= found_key.offset;
664 btrfs_release_path(&path);
665 key.objectid = found_key.objectid + found_key.offset;
669 btrfs_commit_transaction(trans, root);
670 btrfs_release_path(&path);
675 * Just update chunk allocation info, since --rootdir may allocate new
676 * chunks which is not updated in @allocation structure.
678 static void update_chunk_allocation(struct btrfs_fs_info *fs_info,
679 struct mkfs_allocation *allocation)
681 struct btrfs_block_group_cache *bg_cache;
682 const u64 mixed_flag = BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA;
683 u64 search_start = 0;
685 allocation->mixed = 0;
686 allocation->data = 0;
687 allocation->metadata = 0;
688 allocation->system = 0;
690 bg_cache = btrfs_lookup_first_block_group(fs_info,
694 if ((bg_cache->flags & mixed_flag) == mixed_flag)
695 allocation->mixed += bg_cache->key.offset;
696 else if (bg_cache->flags & BTRFS_BLOCK_GROUP_DATA)
697 allocation->data += bg_cache->key.offset;
698 else if (bg_cache->flags & BTRFS_BLOCK_GROUP_METADATA)
699 allocation->metadata += bg_cache->key.offset;
701 allocation->system += bg_cache->key.offset;
702 search_start = bg_cache->key.objectid + bg_cache->key.offset;
706 int main(int argc, char **argv)
709 struct btrfs_root *root;
710 struct btrfs_fs_info *fs_info;
711 struct btrfs_trans_handle *trans;
714 u64 dev_block_count = 0;
716 u64 metadata_profile = 0;
717 u64 data_profile = 0;
718 u32 nodesize = max_t(u32, sysconf(_SC_PAGESIZE),
719 BTRFS_MKFS_DEFAULT_NODE_SIZE);
720 u32 sectorsize = 4096;
721 u32 stripesize = 4096;
728 int nodesize_forced = 0;
729 int data_profile_opt = 0;
730 int metadata_profile_opt = 0;
733 int force_overwrite = 0;
734 char *source_dir = NULL;
735 bool source_dir_set = false;
736 bool shrink_rootdir = false;
737 u64 source_dir_size = 0;
742 char fs_uuid[BTRFS_UUID_UNPARSED_SIZE] = { 0 };
743 u64 features = BTRFS_MKFS_DEFAULT_FEATURES;
744 struct mkfs_allocation allocation = { 0 };
745 struct btrfs_mkfs_config mkfs_cfg;
749 enum { GETOPT_VAL_SHRINK = 257 };
750 static const struct option long_options[] = {
751 { "alloc-start", required_argument, NULL, 'A'},
752 { "byte-count", required_argument, NULL, 'b' },
753 { "force", no_argument, NULL, 'f' },
754 { "leafsize", required_argument, NULL, 'l' },
755 { "label", required_argument, NULL, 'L'},
756 { "metadata", required_argument, NULL, 'm' },
757 { "mixed", no_argument, NULL, 'M' },
758 { "nodesize", required_argument, NULL, 'n' },
759 { "sectorsize", required_argument, NULL, 's' },
760 { "data", required_argument, NULL, 'd' },
761 { "version", no_argument, NULL, 'V' },
762 { "rootdir", required_argument, NULL, 'r' },
763 { "nodiscard", no_argument, NULL, 'K' },
764 { "features", required_argument, NULL, 'O' },
765 { "uuid", required_argument, NULL, 'U' },
766 { "quiet", 0, NULL, 'q' },
767 { "shrink", no_argument, NULL, GETOPT_VAL_SHRINK },
768 { "help", no_argument, NULL, GETOPT_VAL_HELP },
772 c = getopt_long(argc, argv, "A:b:fl:n:s:m:d:L:O:r:U:VMKq",
778 alloc_start = parse_size(optarg);
784 data_profile = parse_profile(optarg);
785 data_profile_opt = 1;
788 warning("--leafsize is deprecated, use --nodesize");
791 nodesize = parse_size(optarg);
795 label = parse_label(optarg);
798 metadata_profile = parse_profile(optarg);
799 metadata_profile_opt = 1;
805 char *orig = strdup(optarg);
808 tmp = btrfs_parse_fs_features(tmp, &features);
810 error("unrecognized filesystem feature '%s'",
816 if (features & BTRFS_FEATURE_LIST_ALL) {
817 btrfs_list_all_fs_features(0);
823 sectorsize = parse_size(optarg);
826 block_count = parse_size(optarg);
830 printf("mkfs.btrfs, part of %s\n",
835 source_dir_set = true;
838 strncpy(fs_uuid, optarg,
839 BTRFS_UUID_UNPARSED_SIZE - 1);
847 case GETOPT_VAL_SHRINK:
848 shrink_rootdir = true;
850 case GETOPT_VAL_HELP:
852 print_usage(c != GETOPT_VAL_HELP);
857 printf("%s\n", PACKAGE_STRING);
858 printf("See %s for more information.\n\n", PACKAGE_URL);
861 sectorsize = max(sectorsize, (u32)sysconf(_SC_PAGESIZE));
862 stripesize = sectorsize;
863 saved_optind = optind;
864 dev_cnt = argc - optind;
868 if (source_dir_set && dev_cnt > 1) {
869 error("the option -r is limited to a single device");
872 if (shrink_rootdir && !source_dir_set) {
873 error("the option --shrink must be used with --rootdir");
880 if (uuid_parse(fs_uuid, dummy_uuid) != 0) {
881 error("could not parse UUID: %s", fs_uuid);
884 if (!test_uuid_unique(fs_uuid)) {
885 error("non-unique UUID: %s", fs_uuid);
890 while (dev_cnt-- > 0) {
891 file = argv[optind++];
892 if (source_dir_set && is_path_exist(file) == 0)
894 else if (is_block_device(file) == 1)
895 ret = test_dev_for_mkfs(file, force_overwrite);
897 ret = test_status_for_mkfs(file, force_overwrite);
903 optind = saved_optind;
904 dev_cnt = argc - optind;
906 file = argv[optind++];
910 * Set default profiles according to number of added devices.
911 * For mixed groups defaults are single/single.
914 if (!metadata_profile_opt) {
915 if (dev_cnt == 1 && ssd && verbose)
916 printf("Detected a SSD, turning off metadata "
917 "duplication. Mkfs with -m dup if you want to "
918 "force metadata duplication.\n");
920 metadata_profile = (dev_cnt > 1) ?
921 BTRFS_BLOCK_GROUP_RAID1 : (ssd) ?
922 0: BTRFS_BLOCK_GROUP_DUP;
924 if (!data_profile_opt) {
925 data_profile = (dev_cnt > 1) ?
926 BTRFS_BLOCK_GROUP_RAID0 : 0; /* raid0 or single */
929 u32 best_nodesize = max_t(u32, sysconf(_SC_PAGESIZE), sectorsize);
931 if (metadata_profile_opt || data_profile_opt) {
932 if (metadata_profile != data_profile) {
934 "with mixed block groups data and metadata profiles must be the same");
939 if (!nodesize_forced)
940 nodesize = best_nodesize;
944 * FS features that can be set by other means than -O
945 * just set the bit here
948 features |= BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS;
950 if ((data_profile | metadata_profile) &
951 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
952 features |= BTRFS_FEATURE_INCOMPAT_RAID56;
955 if (btrfs_check_nodesize(nodesize, sectorsize,
959 if (sectorsize < sizeof(struct btrfs_super_block)) {
960 error("sectorsize smaller than superblock: %u < %zu",
961 sectorsize, sizeof(struct btrfs_super_block));
965 min_dev_size = btrfs_min_dev_size(nodesize, mixed, metadata_profile,
968 * Enlarge the destination file or create a new one, using the size
969 * calculated from source dir.
971 * This must be done before minimal device size checks.
973 if (source_dir_set) {
977 if (is_path_exist(file) == 0)
980 fd = open(file, oflags, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP |
983 error("unable to open %s: %m", file);
987 ret = fstat(fd, &statbuf);
989 error("unable to stat %s: %m", file);
995 * Block_count not specified, use file/device size first.
996 * Or we will always use source_dir_size calculated for mkfs.
999 block_count = btrfs_device_size(fd, &statbuf);
1000 source_dir_size = btrfs_mkfs_size_dir(source_dir, sectorsize,
1001 min_dev_size, metadata_profile, data_profile);
1002 if (block_count < source_dir_size)
1003 block_count = source_dir_size;
1004 ret = zero_output_file(fd, block_count);
1006 error("unable to zero the output file");
1010 /* our "device" is the new image file */
1011 dev_block_count = block_count;
1014 /* Check device/block_count after the nodesize is determined */
1015 if (block_count && block_count < min_dev_size) {
1016 error("size %llu is too small to make a usable filesystem",
1018 error("minimum size for btrfs filesystem is %llu",
1022 for (i = saved_optind; i < saved_optind + dev_cnt; i++) {
1026 ret = test_minimum_size(path, min_dev_size);
1028 error("failed to check size for %s: %m", path);
1032 error("'%s' is too small to make a usable filesystem",
1034 error("minimum size for each btrfs device is %llu",
1039 ret = test_num_disk_vs_raid(metadata_profile, data_profile,
1040 dev_cnt, mixed, ssd);
1047 * Open without O_EXCL so that the problem should not occur by the
1048 * following operation in kernel:
1049 * (btrfs_register_one_device() fails if O_EXCL is on)
1051 fd = open(file, O_RDWR);
1053 error("unable to open %s: %m", file);
1056 ret = btrfs_prepare_device(fd, file, &dev_block_count, block_count,
1057 (zero_end ? PREP_DEVICE_ZERO_END : 0) |
1058 (discard ? PREP_DEVICE_DISCARD : 0) |
1059 (verbose ? PREP_DEVICE_VERBOSE : 0));
1062 if (block_count && block_count > dev_block_count) {
1063 error("%s is smaller than requested size, expected %llu, found %llu",
1064 file, (unsigned long long)block_count,
1065 (unsigned long long)dev_block_count);
1069 /* To create the first block group and chunk 0 in make_btrfs */
1070 if (dev_block_count < BTRFS_MKFS_SYSTEM_GROUP_SIZE) {
1071 error("device is too small to make filesystem, must be at least %llu",
1072 (unsigned long long)BTRFS_MKFS_SYSTEM_GROUP_SIZE);
1076 if (group_profile_max_safe_loss(metadata_profile) <
1077 group_profile_max_safe_loss(data_profile)){
1078 warning("metadata has lower redundancy than data!\n");
1081 mkfs_cfg.label = label;
1082 memcpy(mkfs_cfg.fs_uuid, fs_uuid, sizeof(mkfs_cfg.fs_uuid));
1083 mkfs_cfg.num_bytes = dev_block_count;
1084 mkfs_cfg.nodesize = nodesize;
1085 mkfs_cfg.sectorsize = sectorsize;
1086 mkfs_cfg.stripesize = stripesize;
1087 mkfs_cfg.features = features;
1089 ret = make_btrfs(fd, &mkfs_cfg);
1091 error("error during mkfs: %s", strerror(-ret));
1095 fs_info = open_ctree_fs_info(file, 0, 0, 0,
1096 OPEN_CTREE_WRITES | OPEN_CTREE_FS_PARTIAL);
1098 error("open ctree failed");
1103 root = fs_info->fs_root;
1104 fs_info->alloc_start = alloc_start;
1106 ret = create_metadata_block_groups(root, mixed, &allocation);
1108 error("failed to create default block groups: %d", ret);
1112 trans = btrfs_start_transaction(root, 1);
1113 if (IS_ERR(trans)) {
1114 error("failed to start transaction");
1118 ret = create_data_block_groups(trans, root, mixed, &allocation);
1120 error("failed to create default data block groups: %d", ret);
1124 ret = make_root_dir(trans, root);
1126 error("failed to setup the root directory: %d", ret);
1130 ret = btrfs_commit_transaction(trans, root);
1132 error("unable to commit transaction: %d", ret);
1136 trans = btrfs_start_transaction(root, 1);
1137 if (IS_ERR(trans)) {
1138 error("failed to start transaction");
1145 while (dev_cnt-- > 0) {
1146 file = argv[optind++];
1149 * open without O_EXCL so that the problem should not
1150 * occur by the following processing.
1151 * (btrfs_register_one_device() fails if O_EXCL is on)
1153 fd = open(file, O_RDWR);
1155 error("unable to open %s: %m", file);
1158 ret = btrfs_device_already_in_root(root, fd,
1159 BTRFS_SUPER_INFO_OFFSET);
1161 error("skipping duplicate device %s in the filesystem",
1166 ret = btrfs_prepare_device(fd, file, &dev_block_count,
1168 (verbose ? PREP_DEVICE_VERBOSE : 0) |
1169 (zero_end ? PREP_DEVICE_ZERO_END : 0) |
1170 (discard ? PREP_DEVICE_DISCARD : 0));
1175 ret = btrfs_add_to_fsid(trans, root, fd, file, dev_block_count,
1176 sectorsize, sectorsize, sectorsize);
1178 error("unable to add %s to filesystem: %d", file, ret);
1182 struct btrfs_device *device;
1184 device = container_of(fs_info->fs_devices->devices.next,
1185 struct btrfs_device, dev_list);
1186 printf("adding device %s id %llu\n", file,
1187 (unsigned long long)device->devid);
1192 ret = create_raid_groups(trans, root, data_profile,
1193 metadata_profile, mixed, &allocation);
1195 error("unable to create raid groups: %d", ret);
1199 ret = create_tree(trans, root, BTRFS_DATA_RELOC_TREE_OBJECTID);
1201 error("unable to create data reloc tree: %d", ret);
1205 ret = btrfs_commit_transaction(trans, root);
1207 error("unable to commit transaction: %d", ret);
1211 ret = cleanup_temp_chunks(fs_info, &allocation, data_profile,
1212 metadata_profile, metadata_profile);
1214 error("failed to cleanup temporary chunks: %d", ret);
1218 if (source_dir_set) {
1219 ret = btrfs_mkfs_fill_dir(source_dir, root, verbose);
1221 error("error wihle filling filesystem: %d", ret);
1224 if (shrink_rootdir) {
1225 ret = btrfs_mkfs_shrink_fs(fs_info, &shrink_size,
1228 error("error while shrinking filesystem: %d",
1236 char features_buf[64];
1238 update_chunk_allocation(fs_info, &allocation);
1239 printf("Label: %s\n", label);
1240 printf("UUID: %s\n", mkfs_cfg.fs_uuid);
1241 printf("Node size: %u\n", nodesize);
1242 printf("Sector size: %u\n", sectorsize);
1243 printf("Filesystem size: %s\n",
1244 pretty_size(btrfs_super_total_bytes(fs_info->super_copy)));
1245 printf("Block group profiles:\n");
1246 if (allocation.data)
1247 printf(" Data: %-8s %16s\n",
1248 btrfs_group_profile_str(data_profile),
1249 pretty_size(allocation.data));
1250 if (allocation.metadata)
1251 printf(" Metadata: %-8s %16s\n",
1252 btrfs_group_profile_str(metadata_profile),
1253 pretty_size(allocation.metadata));
1254 if (allocation.mixed)
1255 printf(" Data+Metadata: %-8s %16s\n",
1256 btrfs_group_profile_str(data_profile),
1257 pretty_size(allocation.mixed));
1258 printf(" System: %-8s %16s\n",
1259 btrfs_group_profile_str(metadata_profile),
1260 pretty_size(allocation.system));
1261 printf("SSD detected: %s\n", ssd ? "yes" : "no");
1262 btrfs_parse_features_to_string(features_buf, features);
1263 printf("Incompat features: %s", features_buf);
1266 list_all_devices(root);
1270 * The filesystem is now fully set up, commit the remaining changes and
1271 * fix the signature as the last step before closing the devices.
1273 fs_info->finalize_on_close = 1;
1275 close_ret = close_ctree(root);
1278 optind = saved_optind;
1279 dev_cnt = argc - optind;
1280 while (dev_cnt-- > 0) {
1281 file = argv[optind++];
1282 if (is_block_device(file) == 1)
1283 btrfs_register_one_device(file);
1287 btrfs_close_all_devices();