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
;
318 u8 uuid
[BTRFS_UUID_SIZE
] = {0};
321 ret
= btrfs_copy_root(trans
, root
, root
->node
, &tmp
, objectid
);
325 memcpy(&root_item
, &root
->root_item
, sizeof(root_item
));
326 btrfs_set_root_bytenr(&root_item
, tmp
->start
);
327 btrfs_set_root_level(&root_item
, btrfs_header_level(tmp
));
328 btrfs_set_root_generation(&root_item
, trans
->transid
);
329 /* clear uuid and o/ctime of source tree */
330 memcpy(root_item
.uuid
, uuid
, BTRFS_UUID_SIZE
);
331 btrfs_set_stack_timespec_sec(&root_item
.otime
, 0);
332 btrfs_set_stack_timespec_sec(&root_item
.ctime
, 0);
333 free_extent_buffer(tmp
);
335 location
.objectid
= objectid
;
336 location
.type
= BTRFS_ROOT_ITEM_KEY
;
338 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
,
339 &location
, &root_item
);
344 static void print_usage(int ret
)
346 printf("Usage: mkfs.btrfs [options] dev [ dev ... ]\n");
347 printf("Options:\n");
348 printf(" allocation profiles:\n");
349 printf("\t-d|--data PROFILE data profile, raid0, raid1, raid5, raid6, raid10, dup or single\n");
350 printf("\t-m|--metadata PROFILE metadata profile, values like for data profile\n");
351 printf("\t-M|--mixed mix metadata and data together\n");
352 printf(" features:\n");
353 printf("\t-n|--nodesize SIZE size of btree nodes\n");
354 printf("\t-s|--sectorsize SIZE data block size (may not be mountable by current kernel)\n");
355 printf("\t-O|--features LIST comma separated list of filesystem features (use '-O list-all' to list features)\n");
356 printf("\t-L|--label LABEL set the filesystem label\n");
357 printf("\t-U|--uuid UUID specify the filesystem UUID (must be unique)\n");
358 printf(" creation:\n");
359 printf("\t-b|--byte-count SIZE set filesystem size to SIZE (on the first device)\n");
360 printf("\t-r|--rootdir DIR copy files from DIR to the image root directory\n");
361 printf("\t--shrink (with --rootdir) shrink the filled filesystem to minimal size\n");
362 printf("\t-K|--nodiscard do not perform whole device TRIM\n");
363 printf("\t-f|--force force overwrite of existing filesystem\n");
364 printf(" general:\n");
365 printf("\t-q|--quiet no messages except errors\n");
366 printf("\t-V|--version print the mkfs.btrfs version and exit\n");
367 printf("\t--help print this help and exit\n");
368 printf(" deprecated:\n");
369 printf("\t-A|--alloc-start START the offset to start the filesystem\n");
370 printf("\t-l|--leafsize SIZE deprecated, alias for nodesize\n");
374 static u64
parse_profile(const char *s
)
376 if (strcasecmp(s
, "raid0") == 0) {
377 return BTRFS_BLOCK_GROUP_RAID0
;
378 } else if (strcasecmp(s
, "raid1") == 0) {
379 return BTRFS_BLOCK_GROUP_RAID1
;
380 } else if (strcasecmp(s
, "raid5") == 0) {
381 return BTRFS_BLOCK_GROUP_RAID5
;
382 } else if (strcasecmp(s
, "raid6") == 0) {
383 return BTRFS_BLOCK_GROUP_RAID6
;
384 } else if (strcasecmp(s
, "raid10") == 0) {
385 return BTRFS_BLOCK_GROUP_RAID10
;
386 } else if (strcasecmp(s
, "dup") == 0) {
387 return BTRFS_BLOCK_GROUP_DUP
;
388 } else if (strcasecmp(s
, "single") == 0) {
391 error("unknown profile %s", s
);
398 static char *parse_label(const char *input
)
400 int len
= strlen(input
);
402 if (len
>= BTRFS_LABEL_SIZE
) {
403 error("label %s is too long (max %d)", input
,
404 BTRFS_LABEL_SIZE
- 1);
407 return strdup(input
);
410 static int zero_output_file(int out_fd
, u64 size
)
418 memset(buf
, 0, SZ_4K
);
420 /* Only zero out the first 1M */
421 loop_num
= SZ_1M
/ SZ_4K
;
422 for (i
= 0; i
< loop_num
; i
++) {
423 written
= pwrite64(out_fd
, buf
, SZ_4K
, location
);
424 if (written
!= SZ_4K
)
429 /* Then enlarge the file to size */
430 written
= pwrite64(out_fd
, buf
, 1, size
- 1);
436 static int is_ssd(const char *file
)
439 char wholedisk
[PATH_MAX
];
440 char sysfs_path
[PATH_MAX
];
446 probe
= blkid_new_probe_from_filename(file
);
450 /* Device number of this disk (possibly a partition) */
451 devno
= blkid_probe_get_devno(probe
);
453 blkid_free_probe(probe
);
457 /* Get whole disk name (not full path) for this devno */
458 ret
= blkid_devno_to_wholedisk(devno
,
459 wholedisk
, sizeof(wholedisk
), NULL
);
461 blkid_free_probe(probe
);
465 snprintf(sysfs_path
, PATH_MAX
, "/sys/block/%s/queue/rotational",
468 blkid_free_probe(probe
);
470 fd
= open(sysfs_path
, O_RDONLY
);
475 if (read(fd
, &rotational
, 1) < 1) {
481 return rotational
== '0';
484 static int _cmp_device_by_id(void *priv
, struct list_head
*a
,
487 return list_entry(a
, struct btrfs_device
, dev_list
)->devid
-
488 list_entry(b
, struct btrfs_device
, dev_list
)->devid
;
491 static void list_all_devices(struct btrfs_root
*root
)
493 struct btrfs_fs_devices
*fs_devices
;
494 struct btrfs_device
*device
;
495 int number_of_devices
= 0;
496 u64 total_block_count
= 0;
498 fs_devices
= root
->fs_info
->fs_devices
;
500 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
)
503 list_sort(NULL
, &fs_devices
->devices
, _cmp_device_by_id
);
505 printf("Number of devices: %d\n", number_of_devices
);
506 /* printf("Total devices size: %10s\n", */
507 /* pretty_size(total_block_count)); */
508 printf("Devices:\n");
509 printf(" ID SIZE PATH\n");
510 list_for_each_entry(device
, &fs_devices
->devices
, dev_list
) {
511 printf(" %3llu %10s %s\n",
513 pretty_size(device
->total_bytes
),
515 total_block_count
+= device
->total_bytes
;
521 static int is_temp_block_group(struct extent_buffer
*node
,
522 struct btrfs_block_group_item
*bgi
,
523 u64 data_profile
, u64 meta_profile
,
526 u64 flag
= btrfs_disk_block_group_flags(node
, bgi
);
527 u64 flag_type
= flag
& BTRFS_BLOCK_GROUP_TYPE_MASK
;
528 u64 flag_profile
= flag
& BTRFS_BLOCK_GROUP_PROFILE_MASK
;
529 u64 used
= btrfs_disk_block_group_used(node
, bgi
);
532 * Chunks meets all the following conditions is a temp chunk
534 * Temp chunk is always empty.
536 * 2) profile mismatch with mkfs profile.
537 * Temp chunk is always in SINGLE
539 * 3) Size differs with mkfs_alloc
540 * Special case for SINGLE/SINGLE btrfs.
541 * In that case, temp data chunk and real data chunk are always empty.
542 * So we need to use mkfs_alloc to be sure which chunk is the newly
545 * Normally, new chunk size is equal to mkfs one (One chunk)
546 * If it has multiple chunks, we just refuse to delete any one.
547 * As they are all single, so no real problem will happen.
548 * So only use condition 1) and 2) to judge them.
553 case BTRFS_BLOCK_GROUP_DATA
:
554 case BTRFS_BLOCK_GROUP_DATA
| BTRFS_BLOCK_GROUP_METADATA
:
555 data_profile
&= BTRFS_BLOCK_GROUP_PROFILE_MASK
;
556 if (flag_profile
!= data_profile
)
559 case BTRFS_BLOCK_GROUP_METADATA
:
560 meta_profile
&= BTRFS_BLOCK_GROUP_PROFILE_MASK
;
561 if (flag_profile
!= meta_profile
)
564 case BTRFS_BLOCK_GROUP_SYSTEM
:
565 sys_profile
&= BTRFS_BLOCK_GROUP_PROFILE_MASK
;
566 if (flag_profile
!= sys_profile
)
573 /* Note: if current is a block group, it will skip it anyway */
574 static int next_block_group(struct btrfs_root
*root
,
575 struct btrfs_path
*path
)
577 struct btrfs_key key
;
581 ret
= btrfs_next_item(root
, path
);
585 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
586 if (key
.type
== BTRFS_BLOCK_GROUP_ITEM_KEY
)
593 /* This function will cleanup */
594 static int cleanup_temp_chunks(struct btrfs_fs_info
*fs_info
,
595 struct mkfs_allocation
*alloc
,
596 u64 data_profile
, u64 meta_profile
,
599 struct btrfs_trans_handle
*trans
= NULL
;
600 struct btrfs_block_group_item
*bgi
;
601 struct btrfs_root
*root
= fs_info
->extent_root
;
602 struct btrfs_key key
;
603 struct btrfs_key found_key
;
604 struct btrfs_path path
;
607 btrfs_init_path(&path
);
608 trans
= btrfs_start_transaction(root
, 1);
609 BUG_ON(IS_ERR(trans
));
612 key
.type
= BTRFS_BLOCK_GROUP_ITEM_KEY
;
617 * as the rest of the loop may modify the tree, we need to
618 * start a new search each time.
620 ret
= btrfs_search_slot(trans
, root
, &key
, &path
, 0, 0);
623 /* Don't pollute ret for >0 case */
627 btrfs_item_key_to_cpu(path
.nodes
[0], &found_key
,
629 if (found_key
.objectid
< key
.objectid
)
631 if (found_key
.type
!= BTRFS_BLOCK_GROUP_ITEM_KEY
) {
632 ret
= next_block_group(root
, &path
);
639 btrfs_item_key_to_cpu(path
.nodes
[0], &found_key
,
643 bgi
= btrfs_item_ptr(path
.nodes
[0], path
.slots
[0],
644 struct btrfs_block_group_item
);
645 if (is_temp_block_group(path
.nodes
[0], bgi
,
646 data_profile
, meta_profile
,
648 u64 flags
= btrfs_disk_block_group_flags(path
.nodes
[0],
651 ret
= btrfs_free_block_group(trans
, fs_info
,
652 found_key
.objectid
, found_key
.offset
);
656 if ((flags
& BTRFS_BLOCK_GROUP_TYPE_MASK
) ==
657 BTRFS_BLOCK_GROUP_DATA
)
658 alloc
->data
-= found_key
.offset
;
659 else if ((flags
& BTRFS_BLOCK_GROUP_TYPE_MASK
) ==
660 BTRFS_BLOCK_GROUP_METADATA
)
661 alloc
->metadata
-= found_key
.offset
;
662 else if ((flags
& BTRFS_BLOCK_GROUP_TYPE_MASK
) ==
663 BTRFS_BLOCK_GROUP_SYSTEM
)
664 alloc
->system
-= found_key
.offset
;
665 else if ((flags
& BTRFS_BLOCK_GROUP_TYPE_MASK
) ==
666 (BTRFS_BLOCK_GROUP_METADATA
|
667 BTRFS_BLOCK_GROUP_DATA
))
668 alloc
->mixed
-= found_key
.offset
;
670 btrfs_release_path(&path
);
671 key
.objectid
= found_key
.objectid
+ found_key
.offset
;
675 btrfs_commit_transaction(trans
, root
);
676 btrfs_release_path(&path
);
681 * Just update chunk allocation info, since --rootdir may allocate new
682 * chunks which is not updated in @allocation structure.
684 static void update_chunk_allocation(struct btrfs_fs_info
*fs_info
,
685 struct mkfs_allocation
*allocation
)
687 struct btrfs_block_group_cache
*bg_cache
;
688 const u64 mixed_flag
= BTRFS_BLOCK_GROUP_DATA
| BTRFS_BLOCK_GROUP_METADATA
;
689 u64 search_start
= 0;
691 allocation
->mixed
= 0;
692 allocation
->data
= 0;
693 allocation
->metadata
= 0;
694 allocation
->system
= 0;
696 bg_cache
= btrfs_lookup_first_block_group(fs_info
,
700 if ((bg_cache
->flags
& mixed_flag
) == mixed_flag
)
701 allocation
->mixed
+= bg_cache
->key
.offset
;
702 else if (bg_cache
->flags
& BTRFS_BLOCK_GROUP_DATA
)
703 allocation
->data
+= bg_cache
->key
.offset
;
704 else if (bg_cache
->flags
& BTRFS_BLOCK_GROUP_METADATA
)
705 allocation
->metadata
+= bg_cache
->key
.offset
;
707 allocation
->system
+= bg_cache
->key
.offset
;
708 search_start
= bg_cache
->key
.objectid
+ bg_cache
->key
.offset
;
712 int main(int argc
, char **argv
)
715 struct btrfs_root
*root
;
716 struct btrfs_fs_info
*fs_info
;
717 struct btrfs_trans_handle
*trans
;
720 u64 dev_block_count
= 0;
722 u64 metadata_profile
= 0;
723 u64 data_profile
= 0;
724 u32 nodesize
= max_t(u32
, sysconf(_SC_PAGESIZE
),
725 BTRFS_MKFS_DEFAULT_NODE_SIZE
);
726 u32 sectorsize
= 4096;
727 u32 stripesize
= 4096;
734 int nodesize_forced
= 0;
735 int data_profile_opt
= 0;
736 int metadata_profile_opt
= 0;
739 int force_overwrite
= 0;
740 char *source_dir
= NULL
;
741 bool source_dir_set
= false;
742 bool shrink_rootdir
= false;
743 u64 source_dir_size
= 0;
748 char fs_uuid
[BTRFS_UUID_UNPARSED_SIZE
] = { 0 };
749 u64 features
= BTRFS_MKFS_DEFAULT_FEATURES
;
750 struct mkfs_allocation allocation
= { 0 };
751 struct btrfs_mkfs_config mkfs_cfg
;
755 enum { GETOPT_VAL_SHRINK
= 257 };
756 static const struct option long_options
[] = {
757 { "alloc-start", required_argument
, NULL
, 'A'},
758 { "byte-count", required_argument
, NULL
, 'b' },
759 { "force", no_argument
, NULL
, 'f' },
760 { "leafsize", required_argument
, NULL
, 'l' },
761 { "label", required_argument
, NULL
, 'L'},
762 { "metadata", required_argument
, NULL
, 'm' },
763 { "mixed", no_argument
, NULL
, 'M' },
764 { "nodesize", required_argument
, NULL
, 'n' },
765 { "sectorsize", required_argument
, NULL
, 's' },
766 { "data", required_argument
, NULL
, 'd' },
767 { "version", no_argument
, NULL
, 'V' },
768 { "rootdir", required_argument
, NULL
, 'r' },
769 { "nodiscard", no_argument
, NULL
, 'K' },
770 { "features", required_argument
, NULL
, 'O' },
771 { "uuid", required_argument
, NULL
, 'U' },
772 { "quiet", 0, NULL
, 'q' },
773 { "shrink", no_argument
, NULL
, GETOPT_VAL_SHRINK
},
774 { "help", no_argument
, NULL
, GETOPT_VAL_HELP
},
778 c
= getopt_long(argc
, argv
, "A:b:fl:n:s:m:d:L:O:r:U:VMKq",
784 alloc_start
= parse_size(optarg
);
790 data_profile
= parse_profile(optarg
);
791 data_profile_opt
= 1;
794 warning("--leafsize is deprecated, use --nodesize");
797 nodesize
= parse_size(optarg
);
801 label
= parse_label(optarg
);
804 metadata_profile
= parse_profile(optarg
);
805 metadata_profile_opt
= 1;
811 char *orig
= strdup(optarg
);
814 tmp
= btrfs_parse_fs_features(tmp
, &features
);
816 error("unrecognized filesystem feature '%s'",
822 if (features
& BTRFS_FEATURE_LIST_ALL
) {
823 btrfs_list_all_fs_features(0);
829 sectorsize
= parse_size(optarg
);
832 block_count
= parse_size(optarg
);
836 printf("mkfs.btrfs, part of %s\n",
841 source_dir_set
= true;
844 strncpy(fs_uuid
, optarg
,
845 BTRFS_UUID_UNPARSED_SIZE
- 1);
853 case GETOPT_VAL_SHRINK
:
854 shrink_rootdir
= true;
856 case GETOPT_VAL_HELP
:
858 print_usage(c
!= GETOPT_VAL_HELP
);
863 printf("%s\n", PACKAGE_STRING
);
864 printf("See %s for more information.\n\n", PACKAGE_URL
);
867 sectorsize
= max(sectorsize
, (u32
)sysconf(_SC_PAGESIZE
));
868 stripesize
= sectorsize
;
869 saved_optind
= optind
;
870 dev_cnt
= argc
- optind
;
874 if (source_dir_set
&& dev_cnt
> 1) {
875 error("the option -r is limited to a single device");
878 if (shrink_rootdir
&& !source_dir_set
) {
879 error("the option --shrink must be used with --rootdir");
886 if (uuid_parse(fs_uuid
, dummy_uuid
) != 0) {
887 error("could not parse UUID: %s", fs_uuid
);
890 if (!test_uuid_unique(fs_uuid
)) {
891 error("non-unique UUID: %s", fs_uuid
);
896 while (dev_cnt
-- > 0) {
897 file
= argv
[optind
++];
898 if (source_dir_set
&& is_path_exist(file
) == 0)
900 else if (is_block_device(file
) == 1)
901 ret
= test_dev_for_mkfs(file
, force_overwrite
);
903 ret
= test_status_for_mkfs(file
, force_overwrite
);
909 optind
= saved_optind
;
910 dev_cnt
= argc
- optind
;
912 file
= argv
[optind
++];
916 * Set default profiles according to number of added devices.
917 * For mixed groups defaults are single/single.
920 if (!metadata_profile_opt
) {
921 if (dev_cnt
== 1 && ssd
&& verbose
)
922 printf("Detected a SSD, turning off metadata "
923 "duplication. Mkfs with -m dup if you want to "
924 "force metadata duplication.\n");
926 metadata_profile
= (dev_cnt
> 1) ?
927 BTRFS_BLOCK_GROUP_RAID1
: (ssd
) ?
928 0: BTRFS_BLOCK_GROUP_DUP
;
930 if (!data_profile_opt
) {
931 data_profile
= (dev_cnt
> 1) ?
932 BTRFS_BLOCK_GROUP_RAID0
: 0; /* raid0 or single */
935 u32 best_nodesize
= max_t(u32
, sysconf(_SC_PAGESIZE
), sectorsize
);
937 if (metadata_profile_opt
|| data_profile_opt
) {
938 if (metadata_profile
!= data_profile
) {
940 "with mixed block groups data and metadata profiles must be the same");
945 if (!nodesize_forced
)
946 nodesize
= best_nodesize
;
950 * FS features that can be set by other means than -O
951 * just set the bit here
954 features
|= BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS
;
956 if ((data_profile
| metadata_profile
) &
957 (BTRFS_BLOCK_GROUP_RAID5
| BTRFS_BLOCK_GROUP_RAID6
)) {
958 features
|= BTRFS_FEATURE_INCOMPAT_RAID56
;
961 if (btrfs_check_nodesize(nodesize
, sectorsize
,
965 if (sectorsize
< sizeof(struct btrfs_super_block
)) {
966 error("sectorsize smaller than superblock: %u < %zu",
967 sectorsize
, sizeof(struct btrfs_super_block
));
971 min_dev_size
= btrfs_min_dev_size(nodesize
, mixed
, metadata_profile
,
974 * Enlarge the destination file or create a new one, using the size
975 * calculated from source dir.
977 * This must be done before minimal device size checks.
979 if (source_dir_set
) {
983 if (is_path_exist(file
) == 0)
986 fd
= open(file
, oflags
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
|
989 error("unable to open %s: %m", file
);
993 ret
= fstat(fd
, &statbuf
);
995 error("unable to stat %s: %m", file
);
1001 * Block_count not specified, use file/device size first.
1002 * Or we will always use source_dir_size calculated for mkfs.
1005 block_count
= btrfs_device_size(fd
, &statbuf
);
1006 source_dir_size
= btrfs_mkfs_size_dir(source_dir
, sectorsize
,
1007 min_dev_size
, metadata_profile
, data_profile
);
1008 if (block_count
< source_dir_size
)
1009 block_count
= source_dir_size
;
1010 ret
= zero_output_file(fd
, block_count
);
1012 error("unable to zero the output file");
1016 /* our "device" is the new image file */
1017 dev_block_count
= block_count
;
1020 /* Check device/block_count after the nodesize is determined */
1021 if (block_count
&& block_count
< min_dev_size
) {
1022 error("size %llu is too small to make a usable filesystem",
1024 error("minimum size for btrfs filesystem is %llu",
1028 for (i
= saved_optind
; i
< saved_optind
+ dev_cnt
; i
++) {
1032 ret
= test_minimum_size(path
, min_dev_size
);
1034 error("failed to check size for %s: %m", path
);
1038 error("'%s' is too small to make a usable filesystem",
1040 error("minimum size for each btrfs device is %llu",
1045 ret
= test_num_disk_vs_raid(metadata_profile
, data_profile
,
1046 dev_cnt
, mixed
, ssd
);
1053 * Open without O_EXCL so that the problem should not occur by the
1054 * following operation in kernel:
1055 * (btrfs_register_one_device() fails if O_EXCL is on)
1057 fd
= open(file
, O_RDWR
);
1059 error("unable to open %s: %m", file
);
1062 ret
= btrfs_prepare_device(fd
, file
, &dev_block_count
, block_count
,
1063 (zero_end
? PREP_DEVICE_ZERO_END
: 0) |
1064 (discard
? PREP_DEVICE_DISCARD
: 0) |
1065 (verbose
? PREP_DEVICE_VERBOSE
: 0));
1068 if (block_count
&& block_count
> dev_block_count
) {
1069 error("%s is smaller than requested size, expected %llu, found %llu",
1070 file
, (unsigned long long)block_count
,
1071 (unsigned long long)dev_block_count
);
1075 /* To create the first block group and chunk 0 in make_btrfs */
1076 if (dev_block_count
< BTRFS_MKFS_SYSTEM_GROUP_SIZE
) {
1077 error("device is too small to make filesystem, must be at least %llu",
1078 (unsigned long long)BTRFS_MKFS_SYSTEM_GROUP_SIZE
);
1082 if (group_profile_max_safe_loss(metadata_profile
) <
1083 group_profile_max_safe_loss(data_profile
)){
1084 warning("metadata has lower redundancy than data!\n");
1087 mkfs_cfg
.label
= label
;
1088 memcpy(mkfs_cfg
.fs_uuid
, fs_uuid
, sizeof(mkfs_cfg
.fs_uuid
));
1089 mkfs_cfg
.num_bytes
= dev_block_count
;
1090 mkfs_cfg
.nodesize
= nodesize
;
1091 mkfs_cfg
.sectorsize
= sectorsize
;
1092 mkfs_cfg
.stripesize
= stripesize
;
1093 mkfs_cfg
.features
= features
;
1095 ret
= make_btrfs(fd
, &mkfs_cfg
);
1097 error("error during mkfs: %s", strerror(-ret
));
1101 fs_info
= open_ctree_fs_info(file
, 0, 0, 0,
1102 OPEN_CTREE_WRITES
| OPEN_CTREE_TEMPORARY_SUPER
);
1104 error("open ctree failed");
1109 root
= fs_info
->fs_root
;
1110 fs_info
->alloc_start
= alloc_start
;
1112 ret
= create_metadata_block_groups(root
, mixed
, &allocation
);
1114 error("failed to create default block groups: %d", ret
);
1118 trans
= btrfs_start_transaction(root
, 1);
1119 if (IS_ERR(trans
)) {
1120 error("failed to start transaction");
1124 ret
= create_data_block_groups(trans
, root
, mixed
, &allocation
);
1126 error("failed to create default data block groups: %d", ret
);
1130 ret
= make_root_dir(trans
, root
);
1132 error("failed to setup the root directory: %d", ret
);
1136 ret
= btrfs_commit_transaction(trans
, root
);
1138 error("unable to commit transaction: %d", ret
);
1142 trans
= btrfs_start_transaction(root
, 1);
1143 if (IS_ERR(trans
)) {
1144 error("failed to start transaction");
1151 while (dev_cnt
-- > 0) {
1152 file
= argv
[optind
++];
1155 * open without O_EXCL so that the problem should not
1156 * occur by the following processing.
1157 * (btrfs_register_one_device() fails if O_EXCL is on)
1159 fd
= open(file
, O_RDWR
);
1161 error("unable to open %s: %m", file
);
1164 ret
= btrfs_device_already_in_root(root
, fd
,
1165 BTRFS_SUPER_INFO_OFFSET
);
1167 error("skipping duplicate device %s in the filesystem",
1172 ret
= btrfs_prepare_device(fd
, file
, &dev_block_count
,
1174 (verbose
? PREP_DEVICE_VERBOSE
: 0) |
1175 (zero_end
? PREP_DEVICE_ZERO_END
: 0) |
1176 (discard
? PREP_DEVICE_DISCARD
: 0));
1181 ret
= btrfs_add_to_fsid(trans
, root
, fd
, file
, dev_block_count
,
1182 sectorsize
, sectorsize
, sectorsize
);
1184 error("unable to add %s to filesystem: %d", file
, ret
);
1188 struct btrfs_device
*device
;
1190 device
= container_of(fs_info
->fs_devices
->devices
.next
,
1191 struct btrfs_device
, dev_list
);
1192 printf("adding device %s id %llu\n", file
,
1193 (unsigned long long)device
->devid
);
1198 ret
= create_raid_groups(trans
, root
, data_profile
,
1199 metadata_profile
, mixed
, &allocation
);
1201 error("unable to create raid groups: %d", ret
);
1205 ret
= create_tree(trans
, root
, BTRFS_DATA_RELOC_TREE_OBJECTID
);
1207 error("unable to create data reloc tree: %d", ret
);
1211 ret
= create_tree(trans
, root
, BTRFS_UUID_TREE_OBJECTID
);
1214 "unable to create uuid tree, will be created after mount: %d", ret
);
1216 ret
= btrfs_commit_transaction(trans
, root
);
1218 error("unable to commit transaction: %d", ret
);
1222 ret
= cleanup_temp_chunks(fs_info
, &allocation
, data_profile
,
1223 metadata_profile
, metadata_profile
);
1225 error("failed to cleanup temporary chunks: %d", ret
);
1229 if (source_dir_set
) {
1230 ret
= btrfs_mkfs_fill_dir(source_dir
, root
, verbose
);
1232 error("error wihle filling filesystem: %d", ret
);
1235 if (shrink_rootdir
) {
1236 ret
= btrfs_mkfs_shrink_fs(fs_info
, &shrink_size
,
1239 error("error while shrinking filesystem: %d",
1247 char features_buf
[64];
1249 update_chunk_allocation(fs_info
, &allocation
);
1250 printf("Label: %s\n", label
);
1251 printf("UUID: %s\n", mkfs_cfg
.fs_uuid
);
1252 printf("Node size: %u\n", nodesize
);
1253 printf("Sector size: %u\n", sectorsize
);
1254 printf("Filesystem size: %s\n",
1255 pretty_size(btrfs_super_total_bytes(fs_info
->super_copy
)));
1256 printf("Block group profiles:\n");
1257 if (allocation
.data
)
1258 printf(" Data: %-8s %16s\n",
1259 btrfs_group_profile_str(data_profile
),
1260 pretty_size(allocation
.data
));
1261 if (allocation
.metadata
)
1262 printf(" Metadata: %-8s %16s\n",
1263 btrfs_group_profile_str(metadata_profile
),
1264 pretty_size(allocation
.metadata
));
1265 if (allocation
.mixed
)
1266 printf(" Data+Metadata: %-8s %16s\n",
1267 btrfs_group_profile_str(data_profile
),
1268 pretty_size(allocation
.mixed
));
1269 printf(" System: %-8s %16s\n",
1270 btrfs_group_profile_str(metadata_profile
),
1271 pretty_size(allocation
.system
));
1272 printf("SSD detected: %s\n", ssd
? "yes" : "no");
1273 btrfs_parse_features_to_string(features_buf
, features
);
1274 printf("Incompat features: %s", features_buf
);
1277 list_all_devices(root
);
1281 * The filesystem is now fully set up, commit the remaining changes and
1282 * fix the signature as the last step before closing the devices.
1284 fs_info
->finalize_on_close
= 1;
1286 close_ret
= close_ctree(root
);
1289 optind
= saved_optind
;
1290 dev_cnt
= argc
- optind
;
1291 while (dev_cnt
-- > 0) {
1292 file
= argv
[optind
++];
1293 if (is_block_device(file
) == 1)
1294 btrfs_register_one_device(file
);
1298 if (!ret
&& close_ret
) {
1300 error("failed to close ctree, the filesystem may be inconsistent: %d",
1304 btrfs_close_all_devices();