Fix man page headers to include the correct program name.
[btrfs-progs-unstable/devel.git] / mkfs.c
blob4f959f741864686ae9860c45b8c8c02b69a0dad3
1 /*
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
20 #define _GNU_SOURCE
22 #ifndef __CHECKER__
23 #include <sys/ioctl.h>
24 #include <sys/mount.h>
25 #include "ioctl.h"
26 #endif
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <fcntl.h>
33 #include <unistd.h>
34 #include <getopt.h>
35 #include <uuid/uuid.h>
36 #include <linux/fs.h>
37 #include <ctype.h>
38 #include "kerncompat.h"
39 #include "ctree.h"
40 #include "disk-io.h"
41 #include "volumes.h"
42 #include "transaction.h"
43 #include "utils.h"
44 #include "version.h"
46 static u64 parse_size(char *s)
48 int len = strlen(s);
49 char c;
50 u64 mult = 1;
52 if (!isdigit(s[len - 1])) {
53 c = tolower(s[len - 1]);
54 switch (c) {
55 case 'g':
56 mult *= 1024;
57 case 'm':
58 mult *= 1024;
59 case 'k':
60 mult *= 1024;
61 case 'b':
62 break;
63 default:
64 fprintf(stderr, "Unknown size descriptor %c\n", c);
65 exit(1);
67 s[len - 1] = '\0';
69 return atol(s) * mult;
72 static int make_root_dir(struct btrfs_root *root)
74 struct btrfs_trans_handle *trans;
75 struct btrfs_key location;
76 u64 bytes_used;
77 u64 chunk_start = 0;
78 u64 chunk_size = 0;
79 int ret;
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);
89 BUG_ON(ret);
91 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
92 &chunk_start, &chunk_size,
93 BTRFS_BLOCK_GROUP_METADATA);
94 BUG_ON(ret);
95 ret = btrfs_make_block_group(trans, root, 0,
96 BTRFS_BLOCK_GROUP_METADATA,
97 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
98 chunk_start, chunk_size);
99 BUG_ON(ret);
101 root->fs_info->system_allocs = 0;
102 btrfs_commit_transaction(trans, root);
103 trans = btrfs_start_transaction(root, 1);
104 BUG_ON(!trans);
106 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
107 &chunk_start, &chunk_size,
108 BTRFS_BLOCK_GROUP_DATA);
109 BUG_ON(ret);
110 ret = btrfs_make_block_group(trans, root, 0,
111 BTRFS_BLOCK_GROUP_DATA,
112 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
113 chunk_start, chunk_size);
114 BUG_ON(ret);
116 ret = btrfs_make_root_dir(trans, root->fs_info->tree_root,
117 BTRFS_ROOT_TREE_DIR_OBJECTID);
118 if (ret)
119 goto err;
120 ret = btrfs_make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID);
121 if (ret)
122 goto err;
123 memcpy(&location, &root->fs_info->fs_root->root_key, sizeof(location));
124 location.offset = (u64)-1;
125 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
126 "default", 7,
127 btrfs_super_root_dir(&root->fs_info->super_copy),
128 &location, BTRFS_FT_DIR, 0);
129 if (ret)
130 goto err;
132 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
133 "default", 7, location.objectid,
134 BTRFS_ROOT_TREE_DIR_OBJECTID, 0);
135 if (ret)
136 goto err;
138 btrfs_commit_transaction(trans, root);
139 err:
140 return ret;
143 static int recow_roots(struct btrfs_trans_handle *trans,
144 struct btrfs_root *root)
146 int ret;
147 struct extent_buffer *tmp;
148 struct btrfs_fs_info *info = root->fs_info;
150 ret = __btrfs_cow_block(trans, info->fs_root, info->fs_root->node,
151 NULL, 0, &tmp, 0, 0);
152 BUG_ON(ret);
153 free_extent_buffer(tmp);
155 ret = __btrfs_cow_block(trans, info->tree_root, info->tree_root->node,
156 NULL, 0, &tmp, 0, 0);
157 BUG_ON(ret);
158 free_extent_buffer(tmp);
160 ret = __btrfs_cow_block(trans, info->extent_root,
161 info->extent_root->node, NULL, 0, &tmp, 0, 0);
162 BUG_ON(ret);
163 free_extent_buffer(tmp);
165 ret = __btrfs_cow_block(trans, info->chunk_root, info->chunk_root->node,
166 NULL, 0, &tmp, 0, 0);
167 BUG_ON(ret);
168 free_extent_buffer(tmp);
171 ret = __btrfs_cow_block(trans, info->dev_root, info->dev_root->node,
172 NULL, 0, &tmp, 0, 0);
173 BUG_ON(ret);
174 free_extent_buffer(tmp);
176 return 0;
179 static int create_one_raid_group(struct btrfs_trans_handle *trans,
180 struct btrfs_root *root, u64 type)
182 u64 chunk_start;
183 u64 chunk_size;
184 int ret;
186 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
187 &chunk_start, &chunk_size, type);
188 BUG_ON(ret);
189 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
190 type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
191 chunk_start, chunk_size);
192 BUG_ON(ret);
193 return ret;
196 static int create_raid_groups(struct btrfs_trans_handle *trans,
197 struct btrfs_root *root, u64 data_profile,
198 u64 metadata_profile)
200 u64 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy);
201 u64 allowed;
202 int ret;
204 if (num_devices == 1)
205 allowed = BTRFS_BLOCK_GROUP_DUP;
206 else if (num_devices >= 4) {
207 allowed = BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
208 BTRFS_BLOCK_GROUP_RAID10;
209 } else
210 allowed = BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1;
212 if (allowed & metadata_profile) {
213 ret = create_one_raid_group(trans, root,
214 BTRFS_BLOCK_GROUP_SYSTEM |
215 (allowed & metadata_profile));
216 BUG_ON(ret);
218 ret = create_one_raid_group(trans, root,
219 BTRFS_BLOCK_GROUP_METADATA |
220 (allowed & metadata_profile));
221 BUG_ON(ret);
223 ret = recow_roots(trans, root);
224 BUG_ON(ret);
226 if (num_devices > 1 && (allowed & data_profile)) {
227 ret = create_one_raid_group(trans, root,
228 BTRFS_BLOCK_GROUP_DATA |
229 (allowed & data_profile));
230 BUG_ON(ret);
232 return 0;
235 static int create_data_reloc_tree(struct btrfs_trans_handle *trans,
236 struct btrfs_root *root)
238 struct btrfs_key location;
239 struct btrfs_root_item root_item;
240 struct extent_buffer *tmp;
241 u64 objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
242 int ret;
244 ret = btrfs_copy_root(trans, root, root->node, &tmp, objectid);
245 BUG_ON(ret);
247 memcpy(&root_item, &root->root_item, sizeof(root_item));
248 btrfs_set_root_bytenr(&root_item, tmp->start);
249 btrfs_set_root_level(&root_item, btrfs_header_level(tmp));
250 btrfs_set_root_generation(&root_item, trans->transid);
251 free_extent_buffer(tmp);
253 location.objectid = objectid;
254 location.type = BTRFS_ROOT_ITEM_KEY;
255 location.offset = trans->transid;
256 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
257 &location, &root_item);
258 BUG_ON(ret);
259 return 0;
262 static void print_usage(void)
264 fprintf(stderr, "usage: mkfs.btrfs [options] dev [ dev ... ]\n");
265 fprintf(stderr, "options:\n");
266 fprintf(stderr, "\t -A --alloc-start the offset to start the FS\n");
267 fprintf(stderr, "\t -b --byte-count total number of bytes in the FS\n");
268 fprintf(stderr, "\t -d --data data profile, raid0, raid1, raid10 or single\n");
269 fprintf(stderr, "\t -l --leafsize size of btree leaves\n");
270 fprintf(stderr, "\t -L --label set a label\n");
271 fprintf(stderr, "\t -m --metadata metadata profile, values like data profile\n");
272 fprintf(stderr, "\t -n --nodesize size of btree nodes\n");
273 fprintf(stderr, "\t -s --sectorsize min block allocation\n");
274 fprintf(stderr, "%s\n", BTRFS_BUILD_VERSION);
275 exit(1);
278 static void print_version(void)
280 fprintf(stderr, "mkfs.btrfs, part of %s\n", BTRFS_BUILD_VERSION);
281 exit(0);
284 static u64 parse_profile(char *s)
286 if (strcmp(s, "raid0") == 0) {
287 return BTRFS_BLOCK_GROUP_RAID0;
288 } else if (strcmp(s, "raid1") == 0) {
289 return BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP;
290 } else if (strcmp(s, "raid10") == 0) {
291 return BTRFS_BLOCK_GROUP_RAID10 | BTRFS_BLOCK_GROUP_DUP;
292 } else if (strcmp(s, "single") == 0) {
293 return 0;
294 } else {
295 fprintf(stderr, "Unknown option %s\n", s);
296 print_usage();
298 return 0;
301 static char *parse_label(char *input)
303 int i;
304 int len = strlen(input);
306 if (len > BTRFS_LABEL_SIZE) {
307 fprintf(stderr, "Label %s is too long (max %d)\n", input,
308 BTRFS_LABEL_SIZE);
309 exit(1);
311 for (i = 0; i < len; i++) {
312 if (input[i] == '/' || input[i] == '\\') {
313 fprintf(stderr, "invalid label %s\n", input);
314 exit(1);
317 return strdup(input);
320 static struct option long_options[] = {
321 { "alloc-start", 1, NULL, 'A'},
322 { "byte-count", 1, NULL, 'b' },
323 { "leafsize", 1, NULL, 'l' },
324 { "label", 1, NULL, 'L'},
325 { "metadata", 1, NULL, 'm' },
326 { "nodesize", 1, NULL, 'n' },
327 { "sectorsize", 1, NULL, 's' },
328 { "data", 1, NULL, 'd' },
329 { "version", 0, NULL, 'V' },
330 { 0, 0, 0, 0}
333 int main(int ac, char **av)
335 char *file;
336 struct btrfs_root *root;
337 struct btrfs_trans_handle *trans;
338 char *label = NULL;
339 char *first_file;
340 u64 block_count = 0;
341 u64 dev_block_count = 0;
342 u64 blocks[7];
343 u64 alloc_start = 0;
344 u64 metadata_profile = BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP;
345 u64 data_profile = BTRFS_BLOCK_GROUP_RAID0;
346 u32 leafsize = getpagesize();
347 u32 sectorsize = 4096;
348 u32 nodesize = leafsize;
349 u32 stripesize = 4096;
350 int zero_end = 1;
351 int option_index = 0;
352 int fd;
353 int first_fd;
354 int ret;
355 int i;
357 while(1) {
358 int c;
359 c = getopt_long(ac, av, "A:b:l:n:s:m:d:L:V", long_options,
360 &option_index);
361 if (c < 0)
362 break;
363 switch(c) {
364 case 'A':
365 alloc_start = parse_size(optarg);
366 break;
367 case 'd':
368 data_profile = parse_profile(optarg);
369 break;
370 case 'l':
371 leafsize = parse_size(optarg);
372 break;
373 case 'L':
374 label = parse_label(optarg);
375 break;
376 case 'm':
377 metadata_profile = parse_profile(optarg);
378 break;
379 case 'n':
380 nodesize = parse_size(optarg);
381 break;
382 case 's':
383 sectorsize = parse_size(optarg);
384 break;
385 case 'b':
386 block_count = parse_size(optarg);
387 if (block_count < 256*1024*1024) {
388 fprintf(stderr, "File system size "
389 "%llu bytes is too small, "
390 "256M is required at least\n",
391 (unsigned long long)block_count);
392 exit(1);
394 zero_end = 0;
395 break;
396 case 'V':
397 print_version();
398 break;
399 default:
400 print_usage();
403 sectorsize = max(sectorsize, (u32)getpagesize());
404 if (leafsize < sectorsize || (leafsize & (sectorsize - 1))) {
405 fprintf(stderr, "Illegal leafsize %u\n", leafsize);
406 exit(1);
408 if (nodesize < sectorsize || (nodesize & (sectorsize - 1))) {
409 fprintf(stderr, "Illegal nodesize %u\n", nodesize);
410 exit(1);
412 ac = ac - optind;
413 if (ac == 0)
414 print_usage();
416 printf("\nWARNING! - %s IS EXPERIMENTAL\n", BTRFS_BUILD_VERSION);
417 printf("WARNING! - see http://btrfs.wiki.kernel.org before using\n\n");
419 file = av[optind++];
420 ret = check_mounted(file);
421 if (ret < 0) {
422 fprintf(stderr, "error checking %s mount status\n", file);
423 exit(1);
425 if (ret == 1) {
426 fprintf(stderr, "%s is mounted\n", file);
427 exit(1);
429 ac--;
430 fd = open(file, O_RDWR);
431 if (fd < 0) {
432 fprintf(stderr, "unable to open %s\n", file);
433 exit(1);
435 first_fd = fd;
436 first_file = file;
437 ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count);
438 if (block_count == 0)
439 block_count = dev_block_count;
441 blocks[0] = BTRFS_SUPER_INFO_OFFSET;
442 for (i = 1; i < 7; i++) {
443 blocks[i] = BTRFS_SUPER_INFO_OFFSET + 1024 * 1024 +
444 leafsize * i;
447 ret = make_btrfs(fd, file, label, blocks, block_count,
448 nodesize, leafsize,
449 sectorsize, stripesize);
450 if (ret) {
451 fprintf(stderr, "error during mkfs %d\n", ret);
452 exit(1);
454 root = open_ctree(file, 0, O_RDWR);
455 root->fs_info->alloc_start = alloc_start;
457 ret = make_root_dir(root);
458 if (ret) {
459 fprintf(stderr, "failed to setup the root directory\n");
460 exit(1);
463 trans = btrfs_start_transaction(root, 1);
465 if (ac == 0)
466 goto raid_groups;
468 btrfs_register_one_device(file);
469 if (!root) {
470 fprintf(stderr, "ctree init failed\n");
471 return -1;
474 zero_end = 1;
475 while(ac-- > 0) {
476 file = av[optind++];
477 ret = check_mounted(file);
478 if (ret < 0) {
479 fprintf(stderr, "error checking %s mount status\n",
480 file);
481 exit(1);
483 if (ret == 1) {
484 fprintf(stderr, "%s is mounted\n", file);
485 exit(1);
487 fd = open(file, O_RDWR);
488 if (fd < 0) {
489 fprintf(stderr, "unable to open %s\n", file);
490 exit(1);
492 ret = btrfs_device_already_in_root(root, fd,
493 BTRFS_SUPER_INFO_OFFSET);
494 if (ret) {
495 fprintf(stderr, "skipping duplicate device %s in FS\n",
496 file);
497 close(fd);
498 continue;
500 ret = btrfs_prepare_device(fd, file, zero_end,
501 &dev_block_count);
503 BUG_ON(ret);
505 ret = btrfs_add_to_fsid(trans, root, fd, file, dev_block_count,
506 sectorsize, sectorsize, sectorsize);
507 BUG_ON(ret);
508 btrfs_register_one_device(file);
511 raid_groups:
512 ret = create_raid_groups(trans, root, data_profile,
513 metadata_profile);
514 BUG_ON(ret);
516 ret = create_data_reloc_tree(trans, root);
517 BUG_ON(ret);
519 printf("fs created label %s on %s\n\tnodesize %u leafsize %u "
520 "sectorsize %u size %s\n",
521 label, first_file, nodesize, leafsize, sectorsize,
522 pretty_sizes(btrfs_super_total_bytes(&root->fs_info->super_copy)));
524 printf("%s\n", BTRFS_BUILD_VERSION);
525 btrfs_commit_transaction(trans, root);
526 ret = close_ctree(root);
527 BUG_ON(ret);
529 free(label);
530 return 0;