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 #define _XOPEN_SOURCE 500
23 #include <sys/ioctl.h>
24 #include <sys/mount.h>
30 #include <sys/types.h>
35 #include <uuid/uuid.h>
38 #include "kerncompat.h"
42 #include "transaction.h"
46 static u64 parse_size(char *s)
52 if (!isdigit(s[len - 1])) {
53 c = tolower(s[len - 1]);
64 fprintf(stderr, "Unknown size descriptor %c\n", c);
69 return atol(s) * mult;
72 static int make_root_dir(struct btrfs_root *root, int mixed)
74 struct btrfs_trans_handle *trans;
75 struct btrfs_key location;
81 trans = btrfs_start_transaction(root, 1);
82 bytes_used = btrfs_super_bytes_used(&root->fs_info->super_copy);
84 root->fs_info->system_allocs = 1;
85 ret = btrfs_make_block_group(trans, root, bytes_used,
86 BTRFS_BLOCK_GROUP_SYSTEM,
87 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
88 0, BTRFS_MKFS_SYSTEM_GROUP_SIZE);
92 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
93 &chunk_start, &chunk_size,
94 BTRFS_BLOCK_GROUP_METADATA |
95 BTRFS_BLOCK_GROUP_DATA);
97 ret = btrfs_make_block_group(trans, root, 0,
98 BTRFS_BLOCK_GROUP_METADATA |
99 BTRFS_BLOCK_GROUP_DATA,
100 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
101 chunk_start, chunk_size);
103 printf("Created a data/metadata chunk of size %llu\n", chunk_size);
105 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
106 &chunk_start, &chunk_size,
107 BTRFS_BLOCK_GROUP_METADATA);
109 ret = btrfs_make_block_group(trans, root, 0,
110 BTRFS_BLOCK_GROUP_METADATA,
111 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
112 chunk_start, chunk_size);
116 root->fs_info->system_allocs = 0;
117 btrfs_commit_transaction(trans, root);
118 trans = btrfs_start_transaction(root, 1);
122 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
123 &chunk_start, &chunk_size,
124 BTRFS_BLOCK_GROUP_DATA);
126 ret = btrfs_make_block_group(trans, root, 0,
127 BTRFS_BLOCK_GROUP_DATA,
128 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
129 chunk_start, chunk_size);
133 ret = btrfs_make_root_dir(trans, root->fs_info->tree_root,
134 BTRFS_ROOT_TREE_DIR_OBJECTID);
137 ret = btrfs_make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID);
140 memcpy(&location, &root->fs_info->fs_root->root_key, sizeof(location));
141 location.offset = (u64)-1;
142 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
144 btrfs_super_root_dir(&root->fs_info->super_copy),
145 &location, BTRFS_FT_DIR, 0);
149 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
150 "default", 7, location.objectid,
151 BTRFS_ROOT_TREE_DIR_OBJECTID, 0);
155 btrfs_commit_transaction(trans, root);
160 static int recow_roots(struct btrfs_trans_handle *trans,
161 struct btrfs_root *root)
164 struct extent_buffer *tmp;
165 struct btrfs_fs_info *info = root->fs_info;
167 ret = __btrfs_cow_block(trans, info->fs_root, info->fs_root->node,
168 NULL, 0, &tmp, 0, 0);
170 free_extent_buffer(tmp);
172 ret = __btrfs_cow_block(trans, info->tree_root, info->tree_root->node,
173 NULL, 0, &tmp, 0, 0);
175 free_extent_buffer(tmp);
177 ret = __btrfs_cow_block(trans, info->extent_root,
178 info->extent_root->node, NULL, 0, &tmp, 0, 0);
180 free_extent_buffer(tmp);
182 ret = __btrfs_cow_block(trans, info->chunk_root, info->chunk_root->node,
183 NULL, 0, &tmp, 0, 0);
185 free_extent_buffer(tmp);
188 ret = __btrfs_cow_block(trans, info->dev_root, info->dev_root->node,
189 NULL, 0, &tmp, 0, 0);
191 free_extent_buffer(tmp);
193 ret = __btrfs_cow_block(trans, info->csum_root, info->csum_root->node,
194 NULL, 0, &tmp, 0, 0);
196 free_extent_buffer(tmp);
201 static int create_one_raid_group(struct btrfs_trans_handle *trans,
202 struct btrfs_root *root, u64 type)
208 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
209 &chunk_start, &chunk_size, type);
211 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
212 type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
213 chunk_start, chunk_size);
218 static int create_raid_groups(struct btrfs_trans_handle *trans,
219 struct btrfs_root *root, u64 data_profile,
220 u64 metadata_profile, int mixed)
222 u64 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy);
226 if (num_devices == 1)
227 allowed = BTRFS_BLOCK_GROUP_DUP;
228 else if (num_devices >= 4) {
229 allowed = BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
230 BTRFS_BLOCK_GROUP_RAID10;
232 allowed = BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1;
234 if (allowed & metadata_profile) {
235 u64 meta_flags = BTRFS_BLOCK_GROUP_METADATA;
237 ret = create_one_raid_group(trans, root,
238 BTRFS_BLOCK_GROUP_SYSTEM |
239 (allowed & metadata_profile));
243 meta_flags |= BTRFS_BLOCK_GROUP_DATA;
245 ret = create_one_raid_group(trans, root, meta_flags |
246 (allowed & metadata_profile));
249 ret = recow_roots(trans, root);
252 if (!mixed && num_devices > 1 && (allowed & data_profile)) {
253 ret = create_one_raid_group(trans, root,
254 BTRFS_BLOCK_GROUP_DATA |
255 (allowed & data_profile));
261 static int create_data_reloc_tree(struct btrfs_trans_handle *trans,
262 struct btrfs_root *root)
264 struct btrfs_key location;
265 struct btrfs_root_item root_item;
266 struct extent_buffer *tmp;
267 u64 objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
270 ret = btrfs_copy_root(trans, root, root->node, &tmp, objectid);
273 memcpy(&root_item, &root->root_item, sizeof(root_item));
274 btrfs_set_root_bytenr(&root_item, tmp->start);
275 btrfs_set_root_level(&root_item, btrfs_header_level(tmp));
276 btrfs_set_root_generation(&root_item, trans->transid);
277 free_extent_buffer(tmp);
279 location.objectid = objectid;
280 location.type = BTRFS_ROOT_ITEM_KEY;
282 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
283 &location, &root_item);
288 static void print_usage(void)
290 fprintf(stderr, "usage: mkfs.btrfs [options] dev [ dev ... ]\n");
291 fprintf(stderr, "options:\n");
292 fprintf(stderr, "\t -A --alloc-start the offset to start the FS\n");
293 fprintf(stderr, "\t -b --byte-count total number of bytes in the FS\n");
294 fprintf(stderr, "\t -d --data data profile, raid0, raid1, raid10 or single\n");
295 fprintf(stderr, "\t -l --leafsize size of btree leaves\n");
296 fprintf(stderr, "\t -L --label set a label\n");
297 fprintf(stderr, "\t -m --metadata metadata profile, values like data profile\n");
298 fprintf(stderr, "\t -M --mixed mix metadata and data together\n");
299 fprintf(stderr, "\t -n --nodesize size of btree nodes\n");
300 fprintf(stderr, "\t -s --sectorsize min block allocation\n");
301 fprintf(stderr, "%s\n", BTRFS_BUILD_VERSION);
305 static void print_version(void)
307 fprintf(stderr, "mkfs.btrfs, part of %s\n", BTRFS_BUILD_VERSION);
311 static u64 parse_profile(char *s)
313 if (strcmp(s, "raid0") == 0) {
314 return BTRFS_BLOCK_GROUP_RAID0;
315 } else if (strcmp(s, "raid1") == 0) {
316 return BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP;
317 } else if (strcmp(s, "raid10") == 0) {
318 return BTRFS_BLOCK_GROUP_RAID10 | BTRFS_BLOCK_GROUP_DUP;
319 } else if (strcmp(s, "single") == 0) {
322 fprintf(stderr, "Unknown option %s\n", s);
328 static char *parse_label(char *input)
331 int len = strlen(input);
333 if (len > BTRFS_LABEL_SIZE) {
334 fprintf(stderr, "Label %s is too long (max %d)\n", input,
338 for (i = 0; i < len; i++) {
339 if (input[i] == '/' || input[i] == '\\') {
340 fprintf(stderr, "invalid label %s\n", input);
344 return strdup(input);
347 static struct option long_options[] = {
348 { "alloc-start", 1, NULL, 'A'},
349 { "byte-count", 1, NULL, 'b' },
350 { "leafsize", 1, NULL, 'l' },
351 { "label", 1, NULL, 'L'},
352 { "metadata", 1, NULL, 'm' },
353 { "mixed", 0, NULL, 'M' },
354 { "nodesize", 1, NULL, 'n' },
355 { "sectorsize", 1, NULL, 's' },
356 { "data", 1, NULL, 'd' },
357 { "version", 0, NULL, 'V' },
361 int main(int ac, char **av)
364 struct btrfs_root *root;
365 struct btrfs_trans_handle *trans;
369 u64 dev_block_count = 0;
372 u64 metadata_profile = BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP;
373 u64 data_profile = BTRFS_BLOCK_GROUP_RAID0;
374 u32 leafsize = getpagesize();
375 u32 sectorsize = 4096;
376 u32 nodesize = leafsize;
377 u32 stripesize = 4096;
379 int option_index = 0;
385 int data_profile_opt = 0;
386 int metadata_profile_opt = 0;
390 c = getopt_long(ac, av, "A:b:l:n:s:m:d:L:VM", long_options,
396 alloc_start = parse_size(optarg);
399 data_profile = parse_profile(optarg);
400 data_profile_opt = 1;
403 leafsize = parse_size(optarg);
406 label = parse_label(optarg);
409 metadata_profile = parse_profile(optarg);
410 metadata_profile_opt = 1;
416 nodesize = parse_size(optarg);
419 sectorsize = parse_size(optarg);
422 block_count = parse_size(optarg);
423 if (block_count <= 1024*1024*1024) {
424 printf("SMALL VOLUME: forcing mixed "
425 "metadata/data groups\n");
437 sectorsize = max(sectorsize, (u32)getpagesize());
438 if (leafsize < sectorsize || (leafsize & (sectorsize - 1))) {
439 fprintf(stderr, "Illegal leafsize %u\n", leafsize);
442 if (nodesize < sectorsize || (nodesize & (sectorsize - 1))) {
443 fprintf(stderr, "Illegal nodesize %u\n", nodesize);
450 printf("\nWARNING! - %s IS EXPERIMENTAL\n", BTRFS_BUILD_VERSION);
451 printf("WARNING! - see http://btrfs.wiki.kernel.org before using\n\n");
454 ret = check_mounted(file);
456 fprintf(stderr, "error checking %s mount status\n", file);
460 fprintf(stderr, "%s is mounted\n", file);
464 fd = open(file, O_RDWR);
466 fprintf(stderr, "unable to open %s\n", file);
471 ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count,
473 if (block_count == 0)
474 block_count = dev_block_count;
476 if (!metadata_profile_opt)
477 metadata_profile = 0;
478 if (!data_profile_opt)
481 if (metadata_profile != data_profile) {
482 fprintf(stderr, "With mixed block groups data and metadata "
483 "profiles must be the same\n");
488 blocks[0] = BTRFS_SUPER_INFO_OFFSET;
489 for (i = 1; i < 7; i++) {
490 blocks[i] = BTRFS_SUPER_INFO_OFFSET + 1024 * 1024 +
494 ret = make_btrfs(fd, file, label, blocks, block_count,
496 sectorsize, stripesize);
498 fprintf(stderr, "error during mkfs %d\n", ret);
501 root = open_ctree(file, 0, O_RDWR);
502 root->fs_info->alloc_start = alloc_start;
504 ret = make_root_dir(root, mixed);
506 fprintf(stderr, "failed to setup the root directory\n");
510 trans = btrfs_start_transaction(root, 1);
515 btrfs_register_one_device(file);
517 fprintf(stderr, "ctree init failed\n");
523 int old_mixed = mixed;
526 ret = check_mounted(file);
528 fprintf(stderr, "error checking %s mount status\n",
533 fprintf(stderr, "%s is mounted\n", file);
536 fd = open(file, O_RDWR);
538 fprintf(stderr, "unable to open %s\n", file);
541 ret = btrfs_device_already_in_root(root, fd,
542 BTRFS_SUPER_INFO_OFFSET);
544 fprintf(stderr, "skipping duplicate device %s in FS\n",
549 ret = btrfs_prepare_device(fd, file, zero_end,
550 &dev_block_count, &mixed);
554 ret = btrfs_add_to_fsid(trans, root, fd, file, dev_block_count,
555 sectorsize, sectorsize, sectorsize);
557 btrfs_register_one_device(file);
561 ret = create_raid_groups(trans, root, data_profile,
562 metadata_profile, mixed);
565 ret = create_data_reloc_tree(trans, root);
569 struct btrfs_super_block *super = &root->fs_info->super_copy;
570 u64 flags = btrfs_super_incompat_flags(super);
572 flags |= BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS;
573 btrfs_set_super_incompat_flags(super, flags);
576 printf("fs created label %s on %s\n\tnodesize %u leafsize %u "
577 "sectorsize %u size %s\n",
578 label, first_file, nodesize, leafsize, sectorsize,
579 pretty_sizes(btrfs_super_total_bytes(&root->fs_info->super_copy)));
581 printf("%s\n", BTRFS_BUILD_VERSION);
582 btrfs_commit_transaction(trans, root);
583 ret = close_ctree(root);