Rev the disk format
[btrfs-progs-unstable.git] / mkfs.c
blob8d7071e72705a77f5f84f3b5c0473939694e58a9
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(int fd, const char *device_name) {
73 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 root = open_ctree_fd(fd, device_name, 0, O_RDWR);
83 if (!root) {
84 fprintf(stderr, "ctree init failed\n");
85 return -1;
87 trans = btrfs_start_transaction(root, 1);
88 bytes_used = btrfs_super_bytes_used(&root->fs_info->super_copy);
90 root->fs_info->system_allocs = 1;
91 ret = btrfs_make_block_group(trans, root, bytes_used,
92 BTRFS_BLOCK_GROUP_SYSTEM,
93 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
94 0, BTRFS_MKFS_SYSTEM_GROUP_SIZE);
95 BUG_ON(ret);
97 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
98 &chunk_start, &chunk_size,
99 BTRFS_BLOCK_GROUP_METADATA);
100 BUG_ON(ret);
101 ret = btrfs_make_block_group(trans, root, 0,
102 BTRFS_BLOCK_GROUP_METADATA,
103 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
104 chunk_start, chunk_size);
105 BUG_ON(ret);
107 root->fs_info->system_allocs = 0;
108 btrfs_commit_transaction(trans, root);
109 trans = btrfs_start_transaction(root, 1);
110 BUG_ON(!trans);
112 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
113 &chunk_start, &chunk_size,
114 BTRFS_BLOCK_GROUP_DATA);
115 BUG_ON(ret);
116 ret = btrfs_make_block_group(trans, root, 0,
117 BTRFS_BLOCK_GROUP_DATA,
118 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
119 chunk_start, chunk_size);
120 BUG_ON(ret);
122 // ret = btrfs_make_block_group(trans, root, 0, 1);
123 ret = btrfs_make_root_dir(trans, root->fs_info->tree_root,
124 BTRFS_ROOT_TREE_DIR_OBJECTID);
125 if (ret)
126 goto err;
127 ret = btrfs_make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID);
128 if (ret)
129 goto err;
130 memcpy(&location, &root->fs_info->fs_root->root_key, sizeof(location));
131 location.offset = (u64)-1;
132 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
133 "default", 7,
134 btrfs_super_root_dir(&root->fs_info->super_copy),
135 &location, BTRFS_FT_DIR);
136 if (ret)
137 goto err;
139 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
140 "default", 7, location.objectid,
141 BTRFS_ROOT_TREE_DIR_OBJECTID, 0);
142 if (ret)
143 goto err;
145 btrfs_commit_transaction(trans, root);
146 ret = close_ctree(root);
147 err:
148 return ret;
151 static int recow_roots(struct btrfs_trans_handle *trans,
152 struct btrfs_root *root)
154 int ret;
155 struct extent_buffer *tmp;
156 struct btrfs_fs_info *info = root->fs_info;
158 ret = __btrfs_cow_block(trans, info->fs_root, info->fs_root->node,
159 NULL, 0, &tmp, 0, 0);
160 BUG_ON(ret);
161 free_extent_buffer(tmp);
163 ret = __btrfs_cow_block(trans, info->tree_root, info->tree_root->node,
164 NULL, 0, &tmp, 0, 0);
165 BUG_ON(ret);
166 free_extent_buffer(tmp);
168 ret = __btrfs_cow_block(trans, info->extent_root,
169 info->extent_root->node, NULL, 0, &tmp, 0, 0);
170 BUG_ON(ret);
171 free_extent_buffer(tmp);
173 ret = __btrfs_cow_block(trans, info->chunk_root, info->chunk_root->node,
174 NULL, 0, &tmp, 0, 0);
175 BUG_ON(ret);
176 free_extent_buffer(tmp);
179 ret = __btrfs_cow_block(trans, info->dev_root, info->dev_root->node,
180 NULL, 0, &tmp, 0, 0);
181 BUG_ON(ret);
182 free_extent_buffer(tmp);
184 return 0;
187 static int create_one_raid_group(struct btrfs_trans_handle *trans,
188 struct btrfs_root *root, u64 type)
190 u64 chunk_start;
191 u64 chunk_size;
192 int ret;
194 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
195 &chunk_start, &chunk_size, type);
196 BUG_ON(ret);
197 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
198 type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
199 chunk_start, chunk_size);
200 BUG_ON(ret);
201 return ret;
204 static int create_raid_groups(struct btrfs_trans_handle *trans,
205 struct btrfs_root *root, u64 data_profile,
206 u64 metadata_profile)
208 u64 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy);
209 u64 allowed;
210 int ret;
212 if (num_devices == 1)
213 allowed = BTRFS_BLOCK_GROUP_DUP;
214 else if (num_devices >= 4) {
215 allowed = BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
216 BTRFS_BLOCK_GROUP_RAID10;
217 } else
218 allowed = BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1;
220 if (allowed & metadata_profile) {
221 ret = create_one_raid_group(trans, root,
222 BTRFS_BLOCK_GROUP_SYSTEM |
223 (allowed & metadata_profile));
224 BUG_ON(ret);
226 ret = create_one_raid_group(trans, root,
227 BTRFS_BLOCK_GROUP_METADATA |
228 (allowed & metadata_profile));
229 BUG_ON(ret);
231 ret = recow_roots(trans, root);
232 BUG_ON(ret);
234 if (num_devices > 1 && (allowed & data_profile)) {
235 ret = create_one_raid_group(trans, root,
236 BTRFS_BLOCK_GROUP_DATA |
237 (allowed & data_profile));
238 BUG_ON(ret);
240 return 0;
243 static void print_usage(void)
245 fprintf(stderr, "usage: mkfs.btrfs [options] dev [ dev ... ]\n");
246 fprintf(stderr, "options:\n");
247 fprintf(stderr, "\t -b --byte-count total number of bytes in the FS\n");
248 fprintf(stderr, "\t -l --leafsize size of btree leaves\n");
249 fprintf(stderr, "\t -n --nodesize size of btree leaves\n");
250 fprintf(stderr, "\t -s --sectorsize min block allocation\n");
251 fprintf(stderr, "%s\n", BTRFS_BUILD_VERSION);
252 exit(1);
255 static u64 parse_profile(char *s)
257 if (strcmp(s, "raid0") == 0) {
258 return BTRFS_BLOCK_GROUP_RAID0;
259 } else if (strcmp(s, "raid1") == 0) {
260 return BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP;
261 } else if (strcmp(s, "raid10") == 0) {
262 return BTRFS_BLOCK_GROUP_RAID10 | BTRFS_BLOCK_GROUP_DUP;
263 } else if (strcmp(s, "single") == 0) {
264 return 0;
265 } else {
266 fprintf(stderr, "Unknown option %s\n", s);
267 print_usage();
269 return 0;
272 static char *parse_label(char *input)
274 int i;
275 int len = strlen(input);
277 if (len > BTRFS_LABEL_SIZE) {
278 fprintf(stderr, "Label %s is too long (max %d)\n", input,
279 BTRFS_LABEL_SIZE);
280 exit(1);
282 for (i = 0; i < len; i++) {
283 if (input[i] == '/' || input[i] == '\\') {
284 fprintf(stderr, "invalid label %s\n", input);
285 exit(1);
288 return strdup(input);
291 static struct option long_options[] = {
292 { "alloc-start", 1, NULL, 'A'},
293 { "byte-count", 1, NULL, 'b' },
294 { "leafsize", 1, NULL, 'l' },
295 { "label", 1, NULL, 'L'},
296 { "metadata", 1, NULL, 'm' },
297 { "nodesize", 1, NULL, 'n' },
298 { "sectorsize", 1, NULL, 's' },
299 { "data", 1, NULL, 'd' },
300 { 0, 0, 0, 0}
303 int main(int ac, char **av)
305 char *file;
306 struct btrfs_root *root;
307 struct btrfs_trans_handle *trans;
308 char *label = NULL;
309 char *first_file;
310 u64 block_count = 0;
311 u64 dev_block_count = 0;
312 u64 blocks[6];
313 u64 alloc_start = 0;
314 u64 metadata_profile = BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP;
315 u64 data_profile = BTRFS_BLOCK_GROUP_RAID0;
316 u32 leafsize = getpagesize();
317 u32 sectorsize = 4096;
318 u32 nodesize = leafsize;
319 u32 stripesize = 4096;
320 int zero_end = 1;
321 int option_index = 0;
322 int fd;
323 int first_fd;
324 int ret;
325 int i;
327 while(1) {
328 int c;
329 c = getopt_long(ac, av, "A:b:l:n:s:m:d:L:", long_options,
330 &option_index);
331 if (c < 0)
332 break;
333 switch(c) {
334 case 'A':
335 alloc_start = parse_size(optarg);
336 break;
337 case 'd':
338 data_profile = parse_profile(optarg);
339 break;
340 case 'l':
341 leafsize = parse_size(optarg);
342 break;
343 case 'L':
344 label = parse_label(optarg);
345 break;
346 case 'm':
347 metadata_profile = parse_profile(optarg);
348 break;
349 case 'n':
350 nodesize = parse_size(optarg);
351 break;
352 case 's':
353 sectorsize = parse_size(optarg);
354 break;
355 case 'b':
356 block_count = parse_size(optarg);
357 zero_end = 0;
358 break;
359 default:
360 print_usage();
363 sectorsize = max(sectorsize, (u32)getpagesize());
364 if (leafsize < sectorsize || (leafsize & (sectorsize - 1))) {
365 fprintf(stderr, "Illegal leafsize %u\n", leafsize);
366 exit(1);
368 if (nodesize < sectorsize || (nodesize & (sectorsize - 1))) {
369 fprintf(stderr, "Illegal nodesize %u\n", nodesize);
370 exit(1);
372 ac = ac - optind;
373 if (ac == 0)
374 print_usage();
376 file = av[optind++];
377 ret = check_mounted(file);
378 if (ret < 0) {
379 fprintf(stderr, "error checking %s mount status\n", file);
380 exit(1);
382 if (ret == 1) {
383 fprintf(stderr, "%s is mounted\n", file);
384 exit(1);
386 ac--;
387 fd = open(file, O_RDWR);
388 if (fd < 0) {
389 fprintf(stderr, "unable to open %s\n", file);
390 exit(1);
392 first_fd = fd;
393 first_file = file;
394 ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count);
395 if (block_count == 0)
396 block_count = dev_block_count;
398 for (i = 0; i < 6; i++)
399 blocks[i] = BTRFS_SUPER_INFO_OFFSET + leafsize * i;
401 ret = make_btrfs(fd, file, label, blocks, block_count,
402 nodesize, leafsize,
403 sectorsize, stripesize);
404 if (ret) {
405 fprintf(stderr, "error during mkfs %d\n", ret);
406 exit(1);
408 ret = make_root_dir(fd, file);
409 if (ret) {
410 fprintf(stderr, "failed to setup the root directory\n");
411 exit(1);
413 root = open_ctree(file, 0, O_RDWR);
414 root->fs_info->alloc_start = alloc_start;
415 trans = btrfs_start_transaction(root, 1);
417 if (ac == 0)
418 goto raid_groups;
420 btrfs_register_one_device(file);
421 if (!root) {
422 fprintf(stderr, "ctree init failed\n");
423 return -1;
426 zero_end = 1;
427 while(ac-- > 0) {
428 file = av[optind++];
429 ret = check_mounted(file);
430 if (ret < 0) {
431 fprintf(stderr, "error checking %s mount status\n",
432 file);
433 exit(1);
435 if (ret == 1) {
436 fprintf(stderr, "%s is mounted\n", file);
437 exit(1);
439 fd = open(file, O_RDWR);
440 if (fd < 0) {
441 fprintf(stderr, "unable to open %s\n", file);
442 exit(1);
444 ret = btrfs_device_already_in_root(root, fd,
445 BTRFS_SUPER_INFO_OFFSET);
446 if (ret) {
447 fprintf(stderr, "skipping duplicate device %s in FS\n",
448 file);
449 close(fd);
450 continue;
452 ret = btrfs_prepare_device(fd, file, zero_end,
453 &dev_block_count);
455 BUG_ON(ret);
457 ret = btrfs_add_to_fsid(trans, root, fd, file, dev_block_count,
458 sectorsize, sectorsize, sectorsize);
459 BUG_ON(ret);
460 btrfs_register_one_device(file);
463 raid_groups:
464 ret = create_raid_groups(trans, root, data_profile,
465 metadata_profile);
467 printf("fs created label %s on %s\n\tnodesize %u leafsize %u "
468 "sectorsize %u size %s\n",
469 label, first_file, nodesize, leafsize, sectorsize,
470 pretty_sizes(btrfs_super_total_bytes(&root->fs_info->super_copy)));
472 printf("%s\n", BTRFS_BUILD_VERSION);
473 btrfs_commit_transaction(trans, root);
474 ret = close_ctree(root);
475 BUG_ON(ret);
477 free(label);
478 return 0;