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 */
32 #include <uuid/uuid.h>
34 #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;
71 * First temporary system chunk must match the chunk layout
72 * created in make_btrfs().
74 ret
= btrfs_make_block_group(trans
, fs_info
, bytes_used
,
75 BTRFS_BLOCK_GROUP_SYSTEM
,
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 chunk_start
, chunk_size
);
99 allocation
->mixed
+= chunk_size
;
101 ret
= btrfs_alloc_chunk(trans
, fs_info
,
102 &chunk_start
, &chunk_size
,
103 BTRFS_BLOCK_GROUP_METADATA
);
104 if (ret
== -ENOSPC
) {
105 error("no space to allocate metadata chunk");
110 ret
= btrfs_make_block_group(trans
, fs_info
, 0,
111 BTRFS_BLOCK_GROUP_METADATA
,
112 chunk_start
, chunk_size
);
113 allocation
->metadata
+= chunk_size
;
118 root
->fs_info
->system_allocs
= 0;
119 ret
= btrfs_commit_transaction(trans
, root
);
125 static int create_data_block_groups(struct btrfs_trans_handle
*trans
,
126 struct btrfs_root
*root
, int mixed
,
127 struct mkfs_allocation
*allocation
)
129 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
135 ret
= btrfs_alloc_chunk(trans
, fs_info
,
136 &chunk_start
, &chunk_size
,
137 BTRFS_BLOCK_GROUP_DATA
);
138 if (ret
== -ENOSPC
) {
139 error("no space to allocate data chunk");
144 ret
= btrfs_make_block_group(trans
, fs_info
, 0,
145 BTRFS_BLOCK_GROUP_DATA
,
146 chunk_start
, chunk_size
);
147 allocation
->data
+= chunk_size
;
156 static int make_root_dir(struct btrfs_trans_handle
*trans
,
157 struct btrfs_root
*root
)
159 struct btrfs_key location
;
162 ret
= btrfs_make_root_dir(trans
, root
->fs_info
->tree_root
,
163 BTRFS_ROOT_TREE_DIR_OBJECTID
);
166 ret
= btrfs_make_root_dir(trans
, root
, BTRFS_FIRST_FREE_OBJECTID
);
169 memcpy(&location
, &root
->fs_info
->fs_root
->root_key
, sizeof(location
));
170 location
.offset
= (u64
)-1;
171 ret
= btrfs_insert_dir_item(trans
, root
->fs_info
->tree_root
,
173 btrfs_super_root_dir(root
->fs_info
->super_copy
),
174 &location
, BTRFS_FT_DIR
, 0);
178 ret
= btrfs_insert_inode_ref(trans
, root
->fs_info
->tree_root
,
179 "default", 7, location
.objectid
,
180 BTRFS_ROOT_TREE_DIR_OBJECTID
, 0);
188 static int __recow_root(struct btrfs_trans_handle
*trans
,
189 struct btrfs_root
*root
)
191 struct extent_buffer
*tmp
;
194 if (trans
->transid
!= btrfs_root_generation(&root
->root_item
)) {
195 extent_buffer_get(root
->node
);
196 ret
= __btrfs_cow_block(trans
, root
, root
->node
,
197 NULL
, 0, &tmp
, 0, 0);
200 free_extent_buffer(tmp
);
206 static int recow_roots(struct btrfs_trans_handle
*trans
,
207 struct btrfs_root
*root
)
209 struct btrfs_fs_info
*info
= root
->fs_info
;
212 ret
= __recow_root(trans
, info
->fs_root
);
215 ret
= __recow_root(trans
, info
->tree_root
);
218 ret
= __recow_root(trans
, info
->extent_root
);
221 ret
= __recow_root(trans
, info
->chunk_root
);
224 ret
= __recow_root(trans
, info
->dev_root
);
227 ret
= __recow_root(trans
, info
->csum_root
);
234 static int create_one_raid_group(struct btrfs_trans_handle
*trans
,
235 struct btrfs_root
*root
, u64 type
,
236 struct mkfs_allocation
*allocation
)
239 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
244 ret
= btrfs_alloc_chunk(trans
, fs_info
,
245 &chunk_start
, &chunk_size
, type
);
246 if (ret
== -ENOSPC
) {
247 error("not enough free space to allocate chunk");
253 ret
= btrfs_make_block_group(trans
, fs_info
, 0,
254 type
, 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--shrink (with --rootdir) shrink the filled filesystem to minimal size\n");
357 printf("\t-K|--nodiscard do not perform whole device TRIM\n");
358 printf("\t-f|--force force overwrite of existing filesystem\n");
359 printf(" general:\n");
360 printf("\t-q|--quiet no messages except errors\n");
361 printf("\t-V|--version print the mkfs.btrfs version and exit\n");
362 printf("\t--help print this help and exit\n");
363 printf(" deprecated:\n");
364 printf("\t-A|--alloc-start START the offset to start the filesystem\n");
365 printf("\t-l|--leafsize SIZE deprecated, alias for nodesize\n");
369 static u64
parse_profile(const char *s
)
371 if (strcasecmp(s
, "raid0") == 0) {
372 return BTRFS_BLOCK_GROUP_RAID0
;
373 } else if (strcasecmp(s
, "raid1") == 0) {
374 return BTRFS_BLOCK_GROUP_RAID1
;
375 } else if (strcasecmp(s
, "raid5") == 0) {
376 return BTRFS_BLOCK_GROUP_RAID5
;
377 } else if (strcasecmp(s
, "raid6") == 0) {
378 return BTRFS_BLOCK_GROUP_RAID6
;
379 } else if (strcasecmp(s
, "raid10") == 0) {
380 return BTRFS_BLOCK_GROUP_RAID10
;
381 } else if (strcasecmp(s
, "dup") == 0) {
382 return BTRFS_BLOCK_GROUP_DUP
;
383 } else if (strcasecmp(s
, "single") == 0) {
386 error("unknown profile %s", s
);
393 static char *parse_label(const char *input
)
395 int len
= strlen(input
);
397 if (len
>= BTRFS_LABEL_SIZE
) {
398 error("label %s is too long (max %d)", input
,
399 BTRFS_LABEL_SIZE
- 1);
402 return strdup(input
);
405 static int zero_output_file(int out_fd
, u64 size
)
413 memset(buf
, 0, SZ_4K
);
415 /* Only zero out the first 1M */
416 loop_num
= SZ_1M
/ SZ_4K
;
417 for (i
= 0; i
< loop_num
; i
++) {
418 written
= pwrite64(out_fd
, buf
, SZ_4K
, location
);
419 if (written
!= SZ_4K
)
424 /* Then enlarge the file to size */
425 written
= pwrite64(out_fd
, buf
, 1, size
- 1);
431 static int is_ssd(const char *file
)
434 char wholedisk
[PATH_MAX
];
435 char sysfs_path
[PATH_MAX
];
441 probe
= blkid_new_probe_from_filename(file
);
445 /* Device number of this disk (possibly a partition) */
446 devno
= blkid_probe_get_devno(probe
);
448 blkid_free_probe(probe
);
452 /* Get whole disk name (not full path) for this devno */
453 ret
= blkid_devno_to_wholedisk(devno
,
454 wholedisk
, sizeof(wholedisk
), NULL
);
456 blkid_free_probe(probe
);
460 snprintf(sysfs_path
, PATH_MAX
, "/sys/block/%s/queue/rotational",
463 blkid_free_probe(probe
);
465 fd
= open(sysfs_path
, O_RDONLY
);
470 if (read(fd
, &rotational
, 1) < 1) {
476 return rotational
== '0';
479 static int _cmp_device_by_id(void *priv
, struct list_head
*a
,
482 return list_entry(a
, struct btrfs_device
, dev_list
)->devid
-
483 list_entry(b
, struct btrfs_device
, dev_list
)->devid
;
486 static void list_all_devices(struct btrfs_root
*root
)
488 struct btrfs_fs_devices
*fs_devices
;
489 struct btrfs_device
*device
;
490 int number_of_devices
= 0;
491 u64 total_block_count
= 0;
493 fs_devices
= root
->fs_info
->fs_devices
;
495 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
)
498 list_sort(NULL
, &fs_devices
->devices
, _cmp_device_by_id
);
500 printf("Number of devices: %d\n", number_of_devices
);
501 /* printf("Total devices size: %10s\n", */
502 /* pretty_size(total_block_count)); */
503 printf("Devices:\n");
504 printf(" ID SIZE PATH\n");
505 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
506 printf(" %3llu %10s %s\n",
508 pretty_size(device
->total_bytes
),
510 total_block_count
+= device
->total_bytes
;
516 static int is_temp_block_group(struct extent_buffer
*node
,
517 struct btrfs_block_group_item
*bgi
,
518 u64 data_profile
, u64 meta_profile
,
521 u64 flag
= btrfs_disk_block_group_flags(node
, bgi
);
522 u64 flag_type
= flag
& BTRFS_BLOCK_GROUP_TYPE_MASK
;
523 u64 flag_profile
= flag
& BTRFS_BLOCK_GROUP_PROFILE_MASK
;
524 u64 used
= btrfs_disk_block_group_used(node
, bgi
);
527 * Chunks meets all the following conditions is a temp chunk
529 * Temp chunk is always empty.
531 * 2) profile mismatch with mkfs profile.
532 * Temp chunk is always in SINGLE
534 * 3) Size differs with mkfs_alloc
535 * Special case for SINGLE/SINGLE btrfs.
536 * In that case, temp data chunk and real data chunk are always empty.
537 * So we need to use mkfs_alloc to be sure which chunk is the newly
540 * Normally, new chunk size is equal to mkfs one (One chunk)
541 * If it has multiple chunks, we just refuse to delete any one.
542 * As they are all single, so no real problem will happen.
543 * So only use condition 1) and 2) to judge them.
548 case BTRFS_BLOCK_GROUP_DATA
:
549 case BTRFS_BLOCK_GROUP_DATA
| BTRFS_BLOCK_GROUP_METADATA
:
550 data_profile
&= BTRFS_BLOCK_GROUP_PROFILE_MASK
;
551 if (flag_profile
!= data_profile
)
554 case BTRFS_BLOCK_GROUP_METADATA
:
555 meta_profile
&= BTRFS_BLOCK_GROUP_PROFILE_MASK
;
556 if (flag_profile
!= meta_profile
)
559 case BTRFS_BLOCK_GROUP_SYSTEM
:
560 sys_profile
&= BTRFS_BLOCK_GROUP_PROFILE_MASK
;
561 if (flag_profile
!= sys_profile
)
568 /* Note: if current is a block group, it will skip it anyway */
569 static int next_block_group(struct btrfs_root
*root
,
570 struct btrfs_path
*path
)
572 struct btrfs_key key
;
576 ret
= btrfs_next_item(root
, path
);
580 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
581 if (key
.type
== BTRFS_BLOCK_GROUP_ITEM_KEY
)
588 /* This function will cleanup */
589 static int cleanup_temp_chunks(struct btrfs_fs_info
*fs_info
,
590 struct mkfs_allocation
*alloc
,
591 u64 data_profile
, u64 meta_profile
,
594 struct btrfs_trans_handle
*trans
= NULL
;
595 struct btrfs_block_group_item
*bgi
;
596 struct btrfs_root
*root
= fs_info
->extent_root
;
597 struct btrfs_key key
;
598 struct btrfs_key found_key
;
599 struct btrfs_path path
;
602 btrfs_init_path(&path
);
603 trans
= btrfs_start_transaction(root
, 1);
604 BUG_ON(IS_ERR(trans
));
607 key
.type
= BTRFS_BLOCK_GROUP_ITEM_KEY
;
612 * as the rest of the loop may modify the tree, we need to
613 * start a new search each time.
615 ret
= btrfs_search_slot(trans
, root
, &key
, &path
, 0, 0);
618 /* Don't pollute ret for >0 case */
622 btrfs_item_key_to_cpu(path
.nodes
[0], &found_key
,
624 if (found_key
.objectid
< key
.objectid
)
626 if (found_key
.type
!= BTRFS_BLOCK_GROUP_ITEM_KEY
) {
627 ret
= next_block_group(root
, &path
);
634 btrfs_item_key_to_cpu(path
.nodes
[0], &found_key
,
638 bgi
= btrfs_item_ptr(path
.nodes
[0], path
.slots
[0],
639 struct btrfs_block_group_item
);
640 if (is_temp_block_group(path
.nodes
[0], bgi
,
641 data_profile
, meta_profile
,
643 u64 flags
= btrfs_disk_block_group_flags(path
.nodes
[0],
646 ret
= btrfs_free_block_group(trans
, fs_info
,
647 found_key
.objectid
, found_key
.offset
);
651 if ((flags
& BTRFS_BLOCK_GROUP_TYPE_MASK
) ==
652 BTRFS_BLOCK_GROUP_DATA
)
653 alloc
->data
-= found_key
.offset
;
654 else if ((flags
& BTRFS_BLOCK_GROUP_TYPE_MASK
) ==
655 BTRFS_BLOCK_GROUP_METADATA
)
656 alloc
->metadata
-= found_key
.offset
;
657 else if ((flags
& BTRFS_BLOCK_GROUP_TYPE_MASK
) ==
658 BTRFS_BLOCK_GROUP_SYSTEM
)
659 alloc
->system
-= found_key
.offset
;
660 else if ((flags
& BTRFS_BLOCK_GROUP_TYPE_MASK
) ==
661 (BTRFS_BLOCK_GROUP_METADATA
|
662 BTRFS_BLOCK_GROUP_DATA
))
663 alloc
->mixed
-= found_key
.offset
;
665 btrfs_release_path(&path
);
666 key
.objectid
= found_key
.objectid
+ found_key
.offset
;
670 btrfs_commit_transaction(trans
, root
);
671 btrfs_release_path(&path
);
676 * Just update chunk allocation info, since --rootdir may allocate new
677 * chunks which is not updated in @allocation structure.
679 static void update_chunk_allocation(struct btrfs_fs_info
*fs_info
,
680 struct mkfs_allocation
*allocation
)
682 struct btrfs_block_group_cache
*bg_cache
;
683 const u64 mixed_flag
= BTRFS_BLOCK_GROUP_DATA
| BTRFS_BLOCK_GROUP_METADATA
;
684 u64 search_start
= 0;
686 allocation
->mixed
= 0;
687 allocation
->data
= 0;
688 allocation
->metadata
= 0;
689 allocation
->system
= 0;
691 bg_cache
= btrfs_lookup_first_block_group(fs_info
,
695 if ((bg_cache
->flags
& mixed_flag
) == mixed_flag
)
696 allocation
->mixed
+= bg_cache
->key
.offset
;
697 else if (bg_cache
->flags
& BTRFS_BLOCK_GROUP_DATA
)
698 allocation
->data
+= bg_cache
->key
.offset
;
699 else if (bg_cache
->flags
& BTRFS_BLOCK_GROUP_METADATA
)
700 allocation
->metadata
+= bg_cache
->key
.offset
;
702 allocation
->system
+= bg_cache
->key
.offset
;
703 search_start
= bg_cache
->key
.objectid
+ bg_cache
->key
.offset
;
707 int main(int argc
, char **argv
)
710 struct btrfs_root
*root
;
711 struct btrfs_fs_info
*fs_info
;
712 struct btrfs_trans_handle
*trans
;
715 u64 dev_block_count
= 0;
717 u64 metadata_profile
= 0;
718 u64 data_profile
= 0;
719 u32 nodesize
= max_t(u32
, sysconf(_SC_PAGESIZE
),
720 BTRFS_MKFS_DEFAULT_NODE_SIZE
);
721 u32 sectorsize
= 4096;
722 u32 stripesize
= 4096;
729 int nodesize_forced
= 0;
730 int data_profile_opt
= 0;
731 int metadata_profile_opt
= 0;
734 int force_overwrite
= 0;
735 char *source_dir
= NULL
;
736 bool source_dir_set
= false;
737 bool shrink_rootdir
= false;
738 u64 source_dir_size
= 0;
743 char fs_uuid
[BTRFS_UUID_UNPARSED_SIZE
] = { 0 };
744 u64 features
= BTRFS_MKFS_DEFAULT_FEATURES
;
745 struct mkfs_allocation allocation
= { 0 };
746 struct btrfs_mkfs_config mkfs_cfg
;
750 enum { GETOPT_VAL_SHRINK
= 257 };
751 static const struct option long_options
[] = {
752 { "alloc-start", required_argument
, NULL
, 'A'},
753 { "byte-count", required_argument
, NULL
, 'b' },
754 { "force", no_argument
, NULL
, 'f' },
755 { "leafsize", required_argument
, NULL
, 'l' },
756 { "label", required_argument
, NULL
, 'L'},
757 { "metadata", required_argument
, NULL
, 'm' },
758 { "mixed", no_argument
, NULL
, 'M' },
759 { "nodesize", required_argument
, NULL
, 'n' },
760 { "sectorsize", required_argument
, NULL
, 's' },
761 { "data", required_argument
, NULL
, 'd' },
762 { "version", no_argument
, NULL
, 'V' },
763 { "rootdir", required_argument
, NULL
, 'r' },
764 { "nodiscard", no_argument
, NULL
, 'K' },
765 { "features", required_argument
, NULL
, 'O' },
766 { "uuid", required_argument
, NULL
, 'U' },
767 { "quiet", 0, NULL
, 'q' },
768 { "shrink", no_argument
, NULL
, GETOPT_VAL_SHRINK
},
769 { "help", no_argument
, NULL
, GETOPT_VAL_HELP
},
773 c
= getopt_long(argc
, argv
, "A:b:fl:n:s:m:d:L:O:r:U:VMKq",
779 alloc_start
= parse_size(optarg
);
785 data_profile
= parse_profile(optarg
);
786 data_profile_opt
= 1;
789 warning("--leafsize is deprecated, use --nodesize");
792 nodesize
= parse_size(optarg
);
796 label
= parse_label(optarg
);
799 metadata_profile
= parse_profile(optarg
);
800 metadata_profile_opt
= 1;
806 char *orig
= strdup(optarg
);
809 tmp
= btrfs_parse_fs_features(tmp
, &features
);
811 error("unrecognized filesystem feature '%s'",
817 if (features
& BTRFS_FEATURE_LIST_ALL
) {
818 btrfs_list_all_fs_features(0);
824 sectorsize
= parse_size(optarg
);
827 block_count
= parse_size(optarg
);
831 printf("mkfs.btrfs, part of %s\n",
836 source_dir_set
= true;
839 strncpy(fs_uuid
, optarg
,
840 BTRFS_UUID_UNPARSED_SIZE
- 1);
848 case GETOPT_VAL_SHRINK
:
849 shrink_rootdir
= true;
851 case GETOPT_VAL_HELP
:
853 print_usage(c
!= GETOPT_VAL_HELP
);
858 printf("%s\n", PACKAGE_STRING
);
859 printf("See %s for more information.\n\n", PACKAGE_URL
);
862 sectorsize
= max(sectorsize
, (u32
)sysconf(_SC_PAGESIZE
));
863 stripesize
= sectorsize
;
864 saved_optind
= optind
;
865 dev_cnt
= argc
- optind
;
869 if (source_dir_set
&& dev_cnt
> 1) {
870 error("the option -r is limited to a single device");
873 if (shrink_rootdir
&& !source_dir_set
) {
874 error("the option --shrink must be used with --rootdir");
881 if (uuid_parse(fs_uuid
, dummy_uuid
) != 0) {
882 error("could not parse UUID: %s", fs_uuid
);
885 if (!test_uuid_unique(fs_uuid
)) {
886 error("non-unique UUID: %s", fs_uuid
);
891 while (dev_cnt
-- > 0) {
892 file
= argv
[optind
++];
893 if (source_dir_set
&& is_path_exist(file
) == 0)
895 else if (is_block_device(file
) == 1)
896 ret
= test_dev_for_mkfs(file
, force_overwrite
);
898 ret
= test_status_for_mkfs(file
, force_overwrite
);
904 optind
= saved_optind
;
905 dev_cnt
= argc
- optind
;
907 file
= argv
[optind
++];
911 * Set default profiles according to number of added devices.
912 * For mixed groups defaults are single/single.
915 if (!metadata_profile_opt
) {
916 if (dev_cnt
== 1 && ssd
&& verbose
)
917 printf("Detected a SSD, turning off metadata "
918 "duplication. Mkfs with -m dup if you want to "
919 "force metadata duplication.\n");
921 metadata_profile
= (dev_cnt
> 1) ?
922 BTRFS_BLOCK_GROUP_RAID1
: (ssd
) ?
923 0: BTRFS_BLOCK_GROUP_DUP
;
925 if (!data_profile_opt
) {
926 data_profile
= (dev_cnt
> 1) ?
927 BTRFS_BLOCK_GROUP_RAID0
: 0; /* raid0 or single */
930 u32 best_nodesize
= max_t(u32
, sysconf(_SC_PAGESIZE
), sectorsize
);
932 if (metadata_profile_opt
|| data_profile_opt
) {
933 if (metadata_profile
!= data_profile
) {
935 "with mixed block groups data and metadata profiles must be the same");
940 if (!nodesize_forced
)
941 nodesize
= best_nodesize
;
945 * FS features that can be set by other means than -O
946 * just set the bit here
949 features
|= BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS
;
951 if ((data_profile
| metadata_profile
) &
952 (BTRFS_BLOCK_GROUP_RAID5
| BTRFS_BLOCK_GROUP_RAID6
)) {
953 features
|= BTRFS_FEATURE_INCOMPAT_RAID56
;
956 if (btrfs_check_nodesize(nodesize
, sectorsize
,
960 if (sectorsize
< sizeof(struct btrfs_super_block
)) {
961 error("sectorsize smaller than superblock: %u < %zu",
962 sectorsize
, sizeof(struct btrfs_super_block
));
966 min_dev_size
= btrfs_min_dev_size(nodesize
, mixed
, metadata_profile
,
969 * Enlarge the destination file or create a new one, using the size
970 * calculated from source dir.
972 * This must be done before minimal device size checks.
974 if (source_dir_set
) {
978 if (is_path_exist(file
) == 0)
981 fd
= open(file
, oflags
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
|
984 error("unable to open %s: %m", file
);
988 ret
= fstat(fd
, &statbuf
);
990 error("unable to stat %s: %m", file
);
996 * Block_count not specified, use file/device size first.
997 * Or we will always use source_dir_size calculated for mkfs.
1000 block_count
= btrfs_device_size(fd
, &statbuf
);
1001 source_dir_size
= btrfs_mkfs_size_dir(source_dir
, sectorsize
,
1002 min_dev_size
, metadata_profile
, data_profile
);
1003 if (block_count
< source_dir_size
)
1004 block_count
= source_dir_size
;
1005 ret
= zero_output_file(fd
, block_count
);
1007 error("unable to zero the output file");
1011 /* our "device" is the new image file */
1012 dev_block_count
= block_count
;
1015 /* Check device/block_count after the nodesize is determined */
1016 if (block_count
&& block_count
< min_dev_size
) {
1017 error("size %llu is too small to make a usable filesystem",
1019 error("minimum size for btrfs filesystem is %llu",
1023 for (i
= saved_optind
; i
< saved_optind
+ dev_cnt
; i
++) {
1027 ret
= test_minimum_size(path
, min_dev_size
);
1029 error("failed to check size for %s: %m", path
);
1033 error("'%s' is too small to make a usable filesystem",
1035 error("minimum size for each btrfs device is %llu",
1040 ret
= test_num_disk_vs_raid(metadata_profile
, data_profile
,
1041 dev_cnt
, mixed
, ssd
);
1048 * Open without O_EXCL so that the problem should not occur by the
1049 * following operation in kernel:
1050 * (btrfs_register_one_device() fails if O_EXCL is on)
1052 fd
= open(file
, O_RDWR
);
1054 error("unable to open %s: %m", file
);
1057 ret
= btrfs_prepare_device(fd
, file
, &dev_block_count
, block_count
,
1058 (zero_end
? PREP_DEVICE_ZERO_END
: 0) |
1059 (discard
? PREP_DEVICE_DISCARD
: 0) |
1060 (verbose
? PREP_DEVICE_VERBOSE
: 0));
1063 if (block_count
&& block_count
> dev_block_count
) {
1064 error("%s is smaller than requested size, expected %llu, found %llu",
1065 file
, (unsigned long long)block_count
,
1066 (unsigned long long)dev_block_count
);
1070 /* To create the first block group and chunk 0 in make_btrfs */
1071 if (dev_block_count
< BTRFS_MKFS_SYSTEM_GROUP_SIZE
) {
1072 error("device is too small to make filesystem, must be at least %llu",
1073 (unsigned long long)BTRFS_MKFS_SYSTEM_GROUP_SIZE
);
1077 if (group_profile_max_safe_loss(metadata_profile
) <
1078 group_profile_max_safe_loss(data_profile
)){
1079 warning("metadata has lower redundancy than data!\n");
1082 mkfs_cfg
.label
= label
;
1083 memcpy(mkfs_cfg
.fs_uuid
, fs_uuid
, sizeof(mkfs_cfg
.fs_uuid
));
1084 mkfs_cfg
.num_bytes
= dev_block_count
;
1085 mkfs_cfg
.nodesize
= nodesize
;
1086 mkfs_cfg
.sectorsize
= sectorsize
;
1087 mkfs_cfg
.stripesize
= stripesize
;
1088 mkfs_cfg
.features
= features
;
1090 ret
= make_btrfs(fd
, &mkfs_cfg
);
1092 error("error during mkfs: %s", strerror(-ret
));
1096 fs_info
= open_ctree_fs_info(file
, 0, 0, 0,
1097 OPEN_CTREE_WRITES
| OPEN_CTREE_FS_PARTIAL
);
1099 error("open ctree failed");
1104 root
= fs_info
->fs_root
;
1105 fs_info
->alloc_start
= alloc_start
;
1107 ret
= create_metadata_block_groups(root
, mixed
, &allocation
);
1109 error("failed to create default block groups: %d", ret
);
1113 trans
= btrfs_start_transaction(root
, 1);
1114 if (IS_ERR(trans
)) {
1115 error("failed to start transaction");
1119 ret
= create_data_block_groups(trans
, root
, mixed
, &allocation
);
1121 error("failed to create default data block groups: %d", ret
);
1125 ret
= make_root_dir(trans
, root
);
1127 error("failed to setup the root directory: %d", ret
);
1131 ret
= btrfs_commit_transaction(trans
, root
);
1133 error("unable to commit transaction: %d", ret
);
1137 trans
= btrfs_start_transaction(root
, 1);
1138 if (IS_ERR(trans
)) {
1139 error("failed to start transaction");
1146 while (dev_cnt
-- > 0) {
1147 file
= argv
[optind
++];
1150 * open without O_EXCL so that the problem should not
1151 * occur by the following processing.
1152 * (btrfs_register_one_device() fails if O_EXCL is on)
1154 fd
= open(file
, O_RDWR
);
1156 error("unable to open %s: %m", file
);
1159 ret
= btrfs_device_already_in_root(root
, fd
,
1160 BTRFS_SUPER_INFO_OFFSET
);
1162 error("skipping duplicate device %s in the filesystem",
1167 ret
= btrfs_prepare_device(fd
, file
, &dev_block_count
,
1169 (verbose
? PREP_DEVICE_VERBOSE
: 0) |
1170 (zero_end
? PREP_DEVICE_ZERO_END
: 0) |
1171 (discard
? PREP_DEVICE_DISCARD
: 0));
1176 ret
= btrfs_add_to_fsid(trans
, root
, fd
, file
, dev_block_count
,
1177 sectorsize
, sectorsize
, sectorsize
);
1179 error("unable to add %s to filesystem: %d", file
, ret
);
1183 struct btrfs_device
*device
;
1185 device
= container_of(fs_info
->fs_devices
->devices
.next
,
1186 struct btrfs_device
, dev_list
);
1187 printf("adding device %s id %llu\n", file
,
1188 (unsigned long long)device
->devid
);
1193 ret
= create_raid_groups(trans
, root
, data_profile
,
1194 metadata_profile
, mixed
, &allocation
);
1196 error("unable to create raid groups: %d", ret
);
1200 ret
= create_tree(trans
, root
, BTRFS_DATA_RELOC_TREE_OBJECTID
);
1202 error("unable to create data reloc tree: %d", ret
);
1206 ret
= btrfs_commit_transaction(trans
, root
);
1208 error("unable to commit transaction: %d", ret
);
1212 ret
= cleanup_temp_chunks(fs_info
, &allocation
, data_profile
,
1213 metadata_profile
, metadata_profile
);
1215 error("failed to cleanup temporary chunks: %d", ret
);
1219 if (source_dir_set
) {
1220 ret
= btrfs_mkfs_fill_dir(source_dir
, root
, verbose
);
1222 error("error wihle filling filesystem: %d", ret
);
1225 if (shrink_rootdir
) {
1226 ret
= btrfs_mkfs_shrink_fs(fs_info
, &shrink_size
,
1229 error("error while shrinking filesystem: %d",
1237 char features_buf
[64];
1239 update_chunk_allocation(fs_info
, &allocation
);
1240 printf("Label: %s\n", label
);
1241 printf("UUID: %s\n", mkfs_cfg
.fs_uuid
);
1242 printf("Node size: %u\n", nodesize
);
1243 printf("Sector size: %u\n", sectorsize
);
1244 printf("Filesystem size: %s\n",
1245 pretty_size(btrfs_super_total_bytes(fs_info
->super_copy
)));
1246 printf("Block group profiles:\n");
1247 if (allocation
.data
)
1248 printf(" Data: %-8s %16s\n",
1249 btrfs_group_profile_str(data_profile
),
1250 pretty_size(allocation
.data
));
1251 if (allocation
.metadata
)
1252 printf(" Metadata: %-8s %16s\n",
1253 btrfs_group_profile_str(metadata_profile
),
1254 pretty_size(allocation
.metadata
));
1255 if (allocation
.mixed
)
1256 printf(" Data+Metadata: %-8s %16s\n",
1257 btrfs_group_profile_str(data_profile
),
1258 pretty_size(allocation
.mixed
));
1259 printf(" System: %-8s %16s\n",
1260 btrfs_group_profile_str(metadata_profile
),
1261 pretty_size(allocation
.system
));
1262 printf("SSD detected: %s\n", ssd
? "yes" : "no");
1263 btrfs_parse_features_to_string(features_buf
, features
);
1264 printf("Incompat features: %s", features_buf
);
1267 list_all_devices(root
);
1271 * The filesystem is now fully set up, commit the remaining changes and
1272 * fix the signature as the last step before closing the devices.
1274 fs_info
->finalize_on_close
= 1;
1276 close_ret
= close_ctree(root
);
1279 optind
= saved_optind
;
1280 dev_cnt
= argc
- optind
;
1281 while (dev_cnt
-- > 0) {
1282 file
= argv
[optind
++];
1283 if (is_block_device(file
) == 1)
1284 btrfs_register_one_device(file
);
1288 btrfs_close_all_devices();