Write all super blocks during commit
[btrfs-progs-unstable/devel.git] / mkfs.c
blob8eabc41dcdd432b1291091f04d77c8853b01185e
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
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <unistd.h>
27 #include <getopt.h>
28 #include <uuid/uuid.h>
29 #include <linux/fs.h>
30 #include <ctype.h>
31 #include "kerncompat.h"
32 #include "ctree.h"
33 #include "disk-io.h"
34 #include "volumes.h"
35 #include "transaction.h"
36 #include "utils.h"
38 static u64 parse_size(char *s)
40 int len = strlen(s);
41 char c;
42 u64 mult = 1;
44 if (!isdigit(s[len - 1])) {
45 c = tolower(s[len - 1]);
46 switch (c) {
47 case 'g':
48 mult *= 1024;
49 case 'm':
50 mult *= 1024;
51 case 'k':
52 mult *= 1024;
53 case 'b':
54 break;
55 default:
56 fprintf(stderr, "Unknown size descriptor %c\n", c);
57 exit(1);
59 s[len - 1] = '\0';
61 return atol(s) * mult;
64 static int make_root_dir(int fd, const char *device_name) {
65 struct btrfs_root *root;
66 struct btrfs_trans_handle *trans;
67 struct btrfs_key location;
68 u64 bytes_used;
69 u64 chunk_start = 0;
70 u64 chunk_size = 0;
71 int ret;
73 root = open_ctree_fd(fd, device_name, 0);
75 if (!root) {
76 fprintf(stderr, "ctree init failed\n");
77 return -1;
79 trans = btrfs_start_transaction(root, 1);
80 bytes_used = btrfs_super_bytes_used(&root->fs_info->super_copy);
82 root->fs_info->force_system_allocs = 1;
83 ret = btrfs_make_block_group(trans, root, bytes_used,
84 BTRFS_BLOCK_GROUP_SYSTEM,
85 BTRFS_CHUNK_TREE_OBJECTID,
86 0, BTRFS_MKFS_SYSTEM_GROUP_SIZE);
87 BUG_ON(ret);
88 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
89 &chunk_start, &chunk_size,
90 BTRFS_BLOCK_GROUP_METADATA);
91 BUG_ON(ret);
92 ret = btrfs_make_block_group(trans, root, 0,
93 BTRFS_BLOCK_GROUP_METADATA,
94 BTRFS_CHUNK_TREE_OBJECTID,
95 chunk_start, chunk_size);
96 BUG_ON(ret);
98 root->fs_info->force_system_allocs = 0;
99 btrfs_commit_transaction(trans, root);
100 trans = btrfs_start_transaction(root, 1);
101 BUG_ON(!trans);
103 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
104 &chunk_start, &chunk_size,
105 BTRFS_BLOCK_GROUP_DATA);
106 BUG_ON(ret);
107 ret = btrfs_make_block_group(trans, root, 0,
108 BTRFS_BLOCK_GROUP_DATA,
109 BTRFS_CHUNK_TREE_OBJECTID,
110 chunk_start, chunk_size);
111 BUG_ON(ret);
113 // ret = btrfs_make_block_group(trans, root, 0, 1);
114 ret = btrfs_make_root_dir(trans, root->fs_info->tree_root,
115 BTRFS_ROOT_TREE_DIR_OBJECTID);
116 if (ret)
117 goto err;
118 ret = btrfs_make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID);
119 if (ret)
120 goto err;
121 memcpy(&location, &root->fs_info->fs_root->root_key, sizeof(location));
122 location.offset = (u64)-1;
123 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
124 "default", 7,
125 btrfs_super_root_dir(&root->fs_info->super_copy),
126 &location, BTRFS_FT_DIR);
127 if (ret)
128 goto err;
130 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
131 "default", 7, location.objectid,
132 BTRFS_ROOT_TREE_DIR_OBJECTID);
133 if (ret)
134 goto err;
136 btrfs_commit_transaction(trans, root);
137 ret = close_ctree(root);
138 err:
139 return ret;
142 static int recow_roots(struct btrfs_trans_handle *trans,
143 struct btrfs_root *root)
145 int ret;
146 struct extent_buffer *tmp;
147 struct btrfs_fs_info *info = root->fs_info;
149 ret = __btrfs_cow_block(trans, info->fs_root, info->fs_root->node,
150 NULL, 0, &tmp, 0, 0);
151 BUG_ON(ret);
152 free_extent_buffer(tmp);
154 ret = __btrfs_cow_block(trans, info->tree_root, info->tree_root->node,
155 NULL, 0, &tmp, 0, 0);
156 BUG_ON(ret);
157 free_extent_buffer(tmp);
159 ret = __btrfs_cow_block(trans, info->extent_root,
160 info->extent_root->node, NULL, 0, &tmp, 0, 0);
161 BUG_ON(ret);
162 free_extent_buffer(tmp);
164 ret = __btrfs_cow_block(trans, info->chunk_root, info->chunk_root->node,
165 NULL, 0, &tmp, 0, 0);
166 BUG_ON(ret);
167 free_extent_buffer(tmp);
170 ret = __btrfs_cow_block(trans, info->dev_root, info->dev_root->node,
171 NULL, 0, &tmp, 0, 0);
172 BUG_ON(ret);
173 free_extent_buffer(tmp);
175 return 0;
178 static int create_one_raid_group(struct btrfs_trans_handle *trans,
179 struct btrfs_root *root, u64 type)
181 u64 chunk_start;
182 u64 chunk_size;
183 int ret;
185 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
186 &chunk_start, &chunk_size, type);
187 BUG_ON(ret);
188 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
189 type, BTRFS_CHUNK_TREE_OBJECTID,
190 chunk_start, chunk_size);
191 BUG_ON(ret);
192 return ret;
195 static int create_raid_groups(struct btrfs_trans_handle *trans,
196 struct btrfs_root *root, u64 data_profile,
197 u64 metadata_profile)
199 u64 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy);
200 u64 allowed;
201 int ret;
203 if (num_devices == 1)
204 allowed = BTRFS_BLOCK_GROUP_DUP;
205 else
206 allowed = BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1;
208 if (allowed & metadata_profile) {
209 ret = create_one_raid_group(trans, root,
210 BTRFS_BLOCK_GROUP_SYSTEM |
211 (allowed & metadata_profile));
212 BUG_ON(ret);
214 ret = create_one_raid_group(trans, root,
215 BTRFS_BLOCK_GROUP_METADATA |
216 (allowed & metadata_profile));
217 BUG_ON(ret);
219 ret = recow_roots(trans, root);
220 BUG_ON(ret);
222 if (num_devices > 1 && (allowed & data_profile)) {
223 ret = create_one_raid_group(trans, root,
224 BTRFS_BLOCK_GROUP_DATA |
225 (allowed & data_profile));
226 BUG_ON(ret);
228 return 0;
231 static void print_usage(void)
233 fprintf(stderr, "usage: mkfs.btrfs [options] dev [ dev ... ]\n");
234 fprintf(stderr, "options:\n");
235 fprintf(stderr, "\t -b --byte-count total number of bytes in the FS\n");
236 fprintf(stderr, "\t -l --leafsize size of btree leaves\n");
237 fprintf(stderr, "\t -n --nodesize size of btree leaves\n");
238 fprintf(stderr, "\t -s --sectorsize min block allocation\n");
239 exit(1);
242 static u64 parse_profile(char *s)
244 if (strcmp(s, "raid0") == 0) {
245 return BTRFS_BLOCK_GROUP_RAID0;
246 } else if (strcmp(s, "raid1") == 0) {
247 return BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP;
248 } else if (strcmp(s, "single") == 0) {
249 return 0;
250 } else {
251 fprintf(stderr, "Unknown option %s\n", s);
252 print_usage();
254 return 0;
257 static struct option long_options[] = {
258 { "byte-count", 1, NULL, 'b' },
259 { "leafsize", 1, NULL, 'l' },
260 { "nodesize", 1, NULL, 'n' },
261 { "sectorsize", 1, NULL, 's' },
262 { "metadata", 1, NULL, 'm' },
263 { "data", 1, NULL, 'd' },
264 { 0, 0, 0, 0}
267 int main(int ac, char **av)
269 char *file;
270 u64 block_count = 0;
271 u64 dev_block_count = 0;
272 int fd;
273 int first_fd;
274 int ret;
275 int i;
276 u32 leafsize = getpagesize();
277 u32 sectorsize = 4096;
278 u32 nodesize = leafsize;
279 u32 stripesize = 4096;
280 u64 blocks[6];
281 int zero_end = 1;
282 int option_index = 0;
283 struct btrfs_root *root;
284 struct btrfs_trans_handle *trans;
285 u64 metadata_profile = BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP;
286 u64 data_profile = BTRFS_BLOCK_GROUP_RAID0;
288 while(1) {
289 int c;
290 c = getopt_long(ac, av, "b:l:n:s:m:d:", long_options,
291 &option_index);
292 if (c < 0)
293 break;
294 switch(c) {
295 case 'd':
296 data_profile = parse_profile(optarg);
297 break;
298 case 'm':
299 metadata_profile = parse_profile(optarg);
300 break;
301 case 'l':
302 leafsize = parse_size(optarg);
303 break;
304 case 'n':
305 nodesize = parse_size(optarg);
306 break;
307 case 's':
308 sectorsize = parse_size(optarg);
309 break;
310 case 'b':
311 block_count = parse_size(optarg);
312 zero_end = 0;
313 break;
314 default:
315 print_usage();
318 sectorsize = max(sectorsize, (u32)getpagesize());
319 if (leafsize < sectorsize || (leafsize & (sectorsize - 1))) {
320 fprintf(stderr, "Illegal leafsize %u\n", leafsize);
321 exit(1);
323 if (nodesize < sectorsize || (nodesize & (sectorsize - 1))) {
324 fprintf(stderr, "Illegal nodesize %u\n", nodesize);
325 exit(1);
327 ac = ac - optind;
328 if (ac == 0)
329 print_usage();
331 file = av[optind++];
332 ret = check_mounted(file);
333 if (ret < 0) {
334 fprintf(stderr, "error checking %s mount status\n", file);
335 exit(1);
337 if (ret == 1) {
338 fprintf(stderr, "%s is mounted\n", file);
339 exit(1);
341 ac--;
342 fd = open(file, O_RDWR);
343 if (fd < 0) {
344 fprintf(stderr, "unable to open %s\n", file);
345 exit(1);
347 first_fd = fd;
348 ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count);
349 if (block_count == 0)
350 block_count = dev_block_count;
352 for (i = 0; i < 6; i++)
353 blocks[i] = BTRFS_SUPER_INFO_OFFSET + leafsize * i;
355 ret = make_btrfs(fd, file, blocks, block_count, nodesize, leafsize,
356 sectorsize, stripesize);
357 if (ret) {
358 fprintf(stderr, "error during mkfs %d\n", ret);
359 exit(1);
361 ret = make_root_dir(fd, file);
362 if (ret) {
363 fprintf(stderr, "failed to setup the root directory\n");
364 exit(1);
366 printf("fs created on %s nodesize %u leafsize %u sectorsize %u bytes %llu\n",
367 file, nodesize, leafsize, sectorsize,
368 (unsigned long long)block_count);
370 root = open_ctree(file, 0);
371 trans = btrfs_start_transaction(root, 1);
373 if (ac == 0)
374 goto raid_groups;
376 btrfs_register_one_device(file);
377 if (!root) {
378 fprintf(stderr, "ctree init failed\n");
379 return -1;
382 zero_end = 1;
383 while(ac-- > 0) {
384 file = av[optind++];
385 ret = check_mounted(file);
386 if (ret < 0) {
387 fprintf(stderr, "error checking %s mount status\n",
388 file);
389 exit(1);
391 if (ret == 1) {
392 fprintf(stderr, "%s is mounted\n", file);
393 exit(1);
395 fd = open(file, O_RDWR);
396 if (fd < 0) {
397 fprintf(stderr, "unable to open %s\n", file);
398 exit(1);
400 fprintf(stderr, "adding device %s\n", file);
401 ret = btrfs_prepare_device(fd, file, zero_end,
402 &dev_block_count);
404 BUG_ON(ret);
406 ret = btrfs_add_to_fsid(trans, root, fd, dev_block_count,
407 sectorsize, sectorsize, sectorsize);
408 BUG_ON(ret);
409 btrfs_register_one_device(file);
412 raid_groups:
413 ret = create_raid_groups(trans, root, data_profile,
414 metadata_profile);
415 btrfs_commit_transaction(trans, root);
416 ret = close_ctree(root);
417 BUG_ON(ret);
418 return 0;