Add chunk uuids and update multi-device back references
[btrfs-progs-unstable.git] / mkfs.c
blob741edc67c1ab1f24259c418016a24648418abdff
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_FIRST_CHUNK_TREE_OBJECTID,
86 0, BTRFS_MKFS_SYSTEM_GROUP_SIZE);
87 BUG_ON(ret);
89 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
90 &chunk_start, &chunk_size,
91 BTRFS_BLOCK_GROUP_METADATA);
92 BUG_ON(ret);
93 ret = btrfs_make_block_group(trans, root, 0,
94 BTRFS_BLOCK_GROUP_METADATA,
95 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
96 chunk_start, chunk_size);
97 BUG_ON(ret);
99 root->fs_info->force_system_allocs = 0;
100 btrfs_commit_transaction(trans, root);
101 trans = btrfs_start_transaction(root, 1);
102 BUG_ON(!trans);
104 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
105 &chunk_start, &chunk_size,
106 BTRFS_BLOCK_GROUP_DATA);
107 BUG_ON(ret);
108 ret = btrfs_make_block_group(trans, root, 0,
109 BTRFS_BLOCK_GROUP_DATA,
110 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
111 chunk_start, chunk_size);
112 BUG_ON(ret);
114 // ret = btrfs_make_block_group(trans, root, 0, 1);
115 ret = btrfs_make_root_dir(trans, root->fs_info->tree_root,
116 BTRFS_ROOT_TREE_DIR_OBJECTID);
117 if (ret)
118 goto err;
119 ret = btrfs_make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID);
120 if (ret)
121 goto err;
122 memcpy(&location, &root->fs_info->fs_root->root_key, sizeof(location));
123 location.offset = (u64)-1;
124 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
125 "default", 7,
126 btrfs_super_root_dir(&root->fs_info->super_copy),
127 &location, BTRFS_FT_DIR);
128 if (ret)
129 goto err;
131 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
132 "default", 7, location.objectid,
133 BTRFS_ROOT_TREE_DIR_OBJECTID);
134 if (ret)
135 goto err;
137 btrfs_commit_transaction(trans, root);
138 ret = close_ctree(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
207 allowed = BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1;
209 if (allowed & metadata_profile) {
210 ret = create_one_raid_group(trans, root,
211 BTRFS_BLOCK_GROUP_SYSTEM |
212 (allowed & metadata_profile));
213 BUG_ON(ret);
215 ret = create_one_raid_group(trans, root,
216 BTRFS_BLOCK_GROUP_METADATA |
217 (allowed & metadata_profile));
218 BUG_ON(ret);
220 ret = recow_roots(trans, root);
221 BUG_ON(ret);
223 if (num_devices > 1 && (allowed & data_profile)) {
224 ret = create_one_raid_group(trans, root,
225 BTRFS_BLOCK_GROUP_DATA |
226 (allowed & data_profile));
227 BUG_ON(ret);
229 return 0;
232 static void print_usage(void)
234 fprintf(stderr, "usage: mkfs.btrfs [options] dev [ dev ... ]\n");
235 fprintf(stderr, "options:\n");
236 fprintf(stderr, "\t -b --byte-count total number of bytes in the FS\n");
237 fprintf(stderr, "\t -l --leafsize size of btree leaves\n");
238 fprintf(stderr, "\t -n --nodesize size of btree leaves\n");
239 fprintf(stderr, "\t -s --sectorsize min block allocation\n");
240 exit(1);
243 static u64 parse_profile(char *s)
245 if (strcmp(s, "raid0") == 0) {
246 return BTRFS_BLOCK_GROUP_RAID0;
247 } else if (strcmp(s, "raid1") == 0) {
248 return BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP;
249 } else if (strcmp(s, "single") == 0) {
250 return 0;
251 } else {
252 fprintf(stderr, "Unknown option %s\n", s);
253 print_usage();
255 return 0;
258 static struct option long_options[] = {
259 { "byte-count", 1, NULL, 'b' },
260 { "leafsize", 1, NULL, 'l' },
261 { "nodesize", 1, NULL, 'n' },
262 { "sectorsize", 1, NULL, 's' },
263 { "metadata", 1, NULL, 'm' },
264 { "data", 1, NULL, 'd' },
265 { 0, 0, 0, 0}
268 int main(int ac, char **av)
270 char *file;
271 u64 block_count = 0;
272 u64 dev_block_count = 0;
273 int fd;
274 int first_fd;
275 int ret;
276 int i;
277 u32 leafsize = getpagesize();
278 u32 sectorsize = 4096;
279 u32 nodesize = leafsize;
280 u32 stripesize = 4096;
281 u64 blocks[6];
282 int zero_end = 1;
283 int option_index = 0;
284 struct btrfs_root *root;
285 struct btrfs_trans_handle *trans;
286 u64 metadata_profile = BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP;
287 u64 data_profile = BTRFS_BLOCK_GROUP_RAID0;
289 while(1) {
290 int c;
291 c = getopt_long(ac, av, "b:l:n:s:m:d:", long_options,
292 &option_index);
293 if (c < 0)
294 break;
295 switch(c) {
296 case 'd':
297 data_profile = parse_profile(optarg);
298 break;
299 case 'm':
300 metadata_profile = parse_profile(optarg);
301 break;
302 case 'l':
303 leafsize = parse_size(optarg);
304 break;
305 case 'n':
306 nodesize = parse_size(optarg);
307 break;
308 case 's':
309 sectorsize = parse_size(optarg);
310 break;
311 case 'b':
312 block_count = parse_size(optarg);
313 zero_end = 0;
314 break;
315 default:
316 print_usage();
319 sectorsize = max(sectorsize, (u32)getpagesize());
320 if (leafsize < sectorsize || (leafsize & (sectorsize - 1))) {
321 fprintf(stderr, "Illegal leafsize %u\n", leafsize);
322 exit(1);
324 if (nodesize < sectorsize || (nodesize & (sectorsize - 1))) {
325 fprintf(stderr, "Illegal nodesize %u\n", nodesize);
326 exit(1);
328 ac = ac - optind;
329 if (ac == 0)
330 print_usage();
332 file = av[optind++];
333 ret = check_mounted(file);
334 if (ret < 0) {
335 fprintf(stderr, "error checking %s mount status\n", file);
336 exit(1);
338 if (ret == 1) {
339 fprintf(stderr, "%s is mounted\n", file);
340 exit(1);
342 ac--;
343 fd = open(file, O_RDWR);
344 if (fd < 0) {
345 fprintf(stderr, "unable to open %s\n", file);
346 exit(1);
348 first_fd = fd;
349 ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count);
350 if (block_count == 0)
351 block_count = dev_block_count;
353 for (i = 0; i < 6; i++)
354 blocks[i] = BTRFS_SUPER_INFO_OFFSET + leafsize * i;
356 ret = make_btrfs(fd, file, blocks, block_count, nodesize, leafsize,
357 sectorsize, stripesize);
358 if (ret) {
359 fprintf(stderr, "error during mkfs %d\n", ret);
360 exit(1);
362 ret = make_root_dir(fd, file);
363 if (ret) {
364 fprintf(stderr, "failed to setup the root directory\n");
365 exit(1);
367 printf("fs created on %s nodesize %u leafsize %u sectorsize %u bytes %llu\n",
368 file, nodesize, leafsize, sectorsize,
369 (unsigned long long)block_count);
371 root = open_ctree(file, 0);
372 trans = btrfs_start_transaction(root, 1);
374 if (ac == 0)
375 goto raid_groups;
377 btrfs_register_one_device(file);
378 if (!root) {
379 fprintf(stderr, "ctree init failed\n");
380 return -1;
383 zero_end = 1;
384 while(ac-- > 0) {
385 file = av[optind++];
386 ret = check_mounted(file);
387 if (ret < 0) {
388 fprintf(stderr, "error checking %s mount status\n",
389 file);
390 exit(1);
392 if (ret == 1) {
393 fprintf(stderr, "%s is mounted\n", file);
394 exit(1);
396 fd = open(file, O_RDWR);
397 if (fd < 0) {
398 fprintf(stderr, "unable to open %s\n", file);
399 exit(1);
401 fprintf(stderr, "adding device %s\n", file);
402 ret = btrfs_prepare_device(fd, file, zero_end,
403 &dev_block_count);
405 BUG_ON(ret);
407 ret = btrfs_add_to_fsid(trans, root, fd, dev_block_count,
408 sectorsize, sectorsize, sectorsize);
409 BUG_ON(ret);
410 btrfs_register_one_device(file);
413 raid_groups:
414 ret = create_raid_groups(trans, root, data_profile,
415 metadata_profile);
416 btrfs_commit_transaction(trans, root);
417 ret = close_ctree(root);
418 BUG_ON(ret);
419 return 0;