Add bcp to test the clone file ioctl
[btrfs-progs-unstable/devel.git] / mkfs.c
blob740cc81dcf143d303f43c89823e848cda1e5f30f
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"
45 static u64 parse_size(char *s)
47 int len = strlen(s);
48 char c;
49 u64 mult = 1;
51 if (!isdigit(s[len - 1])) {
52 c = tolower(s[len - 1]);
53 switch (c) {
54 case 'g':
55 mult *= 1024;
56 case 'm':
57 mult *= 1024;
58 case 'k':
59 mult *= 1024;
60 case 'b':
61 break;
62 default:
63 fprintf(stderr, "Unknown size descriptor %c\n", c);
64 exit(1);
66 s[len - 1] = '\0';
68 return atol(s) * mult;
71 static int make_root_dir(int fd, const char *device_name) {
72 struct btrfs_root *root;
73 struct btrfs_trans_handle *trans;
74 struct btrfs_key location;
75 u64 bytes_used;
76 u64 chunk_start = 0;
77 u64 chunk_size = 0;
78 int ret;
80 root = open_ctree_fd(fd, device_name, 0);
82 if (!root) {
83 fprintf(stderr, "ctree init failed\n");
84 return -1;
86 trans = btrfs_start_transaction(root, 1);
87 bytes_used = btrfs_super_bytes_used(&root->fs_info->super_copy);
89 root->fs_info->system_allocs = 1;
90 ret = btrfs_make_block_group(trans, root, bytes_used,
91 BTRFS_BLOCK_GROUP_SYSTEM,
92 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
93 0, BTRFS_MKFS_SYSTEM_GROUP_SIZE);
94 BUG_ON(ret);
96 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
97 &chunk_start, &chunk_size,
98 BTRFS_BLOCK_GROUP_METADATA);
99 BUG_ON(ret);
100 ret = btrfs_make_block_group(trans, root, 0,
101 BTRFS_BLOCK_GROUP_METADATA,
102 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
103 chunk_start, chunk_size);
104 BUG_ON(ret);
106 root->fs_info->system_allocs = 0;
107 btrfs_commit_transaction(trans, root);
108 trans = btrfs_start_transaction(root, 1);
109 BUG_ON(!trans);
111 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
112 &chunk_start, &chunk_size,
113 BTRFS_BLOCK_GROUP_DATA);
114 BUG_ON(ret);
115 ret = btrfs_make_block_group(trans, root, 0,
116 BTRFS_BLOCK_GROUP_DATA,
117 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
118 chunk_start, chunk_size);
119 BUG_ON(ret);
121 // ret = btrfs_make_block_group(trans, root, 0, 1);
122 ret = btrfs_make_root_dir(trans, root->fs_info->tree_root,
123 BTRFS_ROOT_TREE_DIR_OBJECTID);
124 if (ret)
125 goto err;
126 ret = btrfs_make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID);
127 if (ret)
128 goto err;
129 memcpy(&location, &root->fs_info->fs_root->root_key, sizeof(location));
130 location.offset = (u64)-1;
131 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
132 "default", 7,
133 btrfs_super_root_dir(&root->fs_info->super_copy),
134 &location, BTRFS_FT_DIR);
135 if (ret)
136 goto err;
138 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
139 "default", 7, location.objectid,
140 BTRFS_ROOT_TREE_DIR_OBJECTID);
141 if (ret)
142 goto err;
144 btrfs_commit_transaction(trans, root);
145 ret = close_ctree(root);
146 err:
147 return ret;
150 static int recow_roots(struct btrfs_trans_handle *trans,
151 struct btrfs_root *root)
153 int ret;
154 struct extent_buffer *tmp;
155 struct btrfs_fs_info *info = root->fs_info;
157 ret = __btrfs_cow_block(trans, info->fs_root, info->fs_root->node,
158 NULL, 0, &tmp, 0, 0);
159 BUG_ON(ret);
160 free_extent_buffer(tmp);
162 ret = __btrfs_cow_block(trans, info->tree_root, info->tree_root->node,
163 NULL, 0, &tmp, 0, 0);
164 BUG_ON(ret);
165 free_extent_buffer(tmp);
167 ret = __btrfs_cow_block(trans, info->extent_root,
168 info->extent_root->node, NULL, 0, &tmp, 0, 0);
169 BUG_ON(ret);
170 free_extent_buffer(tmp);
172 ret = __btrfs_cow_block(trans, info->chunk_root, info->chunk_root->node,
173 NULL, 0, &tmp, 0, 0);
174 BUG_ON(ret);
175 free_extent_buffer(tmp);
178 ret = __btrfs_cow_block(trans, info->dev_root, info->dev_root->node,
179 NULL, 0, &tmp, 0, 0);
180 BUG_ON(ret);
181 free_extent_buffer(tmp);
183 return 0;
186 static int create_one_raid_group(struct btrfs_trans_handle *trans,
187 struct btrfs_root *root, u64 type)
189 u64 chunk_start;
190 u64 chunk_size;
191 int ret;
193 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
194 &chunk_start, &chunk_size, type);
195 BUG_ON(ret);
196 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
197 type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
198 chunk_start, chunk_size);
199 BUG_ON(ret);
200 return ret;
203 static int create_raid_groups(struct btrfs_trans_handle *trans,
204 struct btrfs_root *root, u64 data_profile,
205 u64 metadata_profile)
207 u64 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy);
208 u64 allowed;
209 int ret;
211 if (num_devices == 1)
212 allowed = BTRFS_BLOCK_GROUP_DUP;
213 else if (num_devices >= 4) {
214 allowed = BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
215 BTRFS_BLOCK_GROUP_RAID10;
216 } else
217 allowed = BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1;
219 if (allowed & metadata_profile) {
220 ret = create_one_raid_group(trans, root,
221 BTRFS_BLOCK_GROUP_SYSTEM |
222 (allowed & metadata_profile));
223 BUG_ON(ret);
225 ret = create_one_raid_group(trans, root,
226 BTRFS_BLOCK_GROUP_METADATA |
227 (allowed & metadata_profile));
228 BUG_ON(ret);
230 ret = recow_roots(trans, root);
231 BUG_ON(ret);
233 if (num_devices > 1 && (allowed & data_profile)) {
234 ret = create_one_raid_group(trans, root,
235 BTRFS_BLOCK_GROUP_DATA |
236 (allowed & data_profile));
237 BUG_ON(ret);
239 return 0;
242 static void print_usage(void)
244 fprintf(stderr, "usage: mkfs.btrfs [options] dev [ dev ... ]\n");
245 fprintf(stderr, "options:\n");
246 fprintf(stderr, "\t -b --byte-count total number of bytes in the FS\n");
247 fprintf(stderr, "\t -l --leafsize size of btree leaves\n");
248 fprintf(stderr, "\t -n --nodesize size of btree leaves\n");
249 fprintf(stderr, "\t -s --sectorsize min block allocation\n");
250 exit(1);
253 static u64 parse_profile(char *s)
255 if (strcmp(s, "raid0") == 0) {
256 return BTRFS_BLOCK_GROUP_RAID0;
257 } else if (strcmp(s, "raid1") == 0) {
258 return BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP;
259 } else if (strcmp(s, "raid10") == 0) {
260 return BTRFS_BLOCK_GROUP_RAID10 | BTRFS_BLOCK_GROUP_DUP;
261 } else if (strcmp(s, "single") == 0) {
262 return 0;
263 } else {
264 fprintf(stderr, "Unknown option %s\n", s);
265 print_usage();
267 return 0;
270 static char *parse_label(char *input)
272 int i;
273 int len = strlen(input);
275 if (len > BTRFS_LABEL_SIZE) {
276 fprintf(stderr, "Label %s is too long (max %d)\n", input,
277 BTRFS_LABEL_SIZE);
278 exit(1);
280 for (i = 0; i < len; i++) {
281 if (input[i] == '/' || input[i] == '\\') {
282 fprintf(stderr, "invalid label %s\n", input);
283 exit(1);
286 return strdup(input);
289 static struct option long_options[] = {
290 { "alloc-start", 1, NULL, 'A'},
291 { "byte-count", 1, NULL, 'b' },
292 { "leafsize", 1, NULL, 'l' },
293 { "label", 1, NULL, 'L'},
294 { "metadata", 1, NULL, 'm' },
295 { "nodesize", 1, NULL, 'n' },
296 { "sectorsize", 1, NULL, 's' },
297 { "data", 1, NULL, 'd' },
298 { 0, 0, 0, 0}
301 int main(int ac, char **av)
303 char *file;
304 struct btrfs_root *root;
305 struct btrfs_trans_handle *trans;
306 char *label = NULL;
307 char *first_file;
308 u64 block_count = 0;
309 u64 dev_block_count = 0;
310 u64 blocks[6];
311 u64 alloc_start = 0;
312 u64 metadata_profile = BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP;
313 u64 data_profile = BTRFS_BLOCK_GROUP_RAID0;
314 u32 leafsize = getpagesize();
315 u32 sectorsize = 4096;
316 u32 nodesize = leafsize;
317 u32 stripesize = 4096;
318 int zero_end = 1;
319 int option_index = 0;
320 int fd;
321 int first_fd;
322 int ret;
323 int i;
325 while(1) {
326 int c;
327 c = getopt_long(ac, av, "A:b:l:n:s:m:d:L:", long_options,
328 &option_index);
329 if (c < 0)
330 break;
331 switch(c) {
332 case 'A':
333 alloc_start = parse_size(optarg);
334 break;
335 case 'd':
336 data_profile = parse_profile(optarg);
337 break;
338 case 'l':
339 leafsize = parse_size(optarg);
340 break;
341 case 'L':
342 label = parse_label(optarg);
343 break;
344 case 'm':
345 metadata_profile = parse_profile(optarg);
346 break;
347 case 'n':
348 nodesize = parse_size(optarg);
349 break;
350 case 's':
351 sectorsize = parse_size(optarg);
352 break;
353 case 'b':
354 block_count = parse_size(optarg);
355 zero_end = 0;
356 break;
357 default:
358 print_usage();
361 sectorsize = max(sectorsize, (u32)getpagesize());
362 if (leafsize < sectorsize || (leafsize & (sectorsize - 1))) {
363 fprintf(stderr, "Illegal leafsize %u\n", leafsize);
364 exit(1);
366 if (nodesize < sectorsize || (nodesize & (sectorsize - 1))) {
367 fprintf(stderr, "Illegal nodesize %u\n", nodesize);
368 exit(1);
370 ac = ac - optind;
371 if (ac == 0)
372 print_usage();
374 file = av[optind++];
375 ret = check_mounted(file);
376 if (ret < 0) {
377 fprintf(stderr, "error checking %s mount status\n", file);
378 exit(1);
380 if (ret == 1) {
381 fprintf(stderr, "%s is mounted\n", file);
382 exit(1);
384 ac--;
385 fd = open(file, O_RDWR);
386 if (fd < 0) {
387 fprintf(stderr, "unable to open %s\n", file);
388 exit(1);
390 first_fd = fd;
391 first_file = file;
392 ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count);
393 if (block_count == 0)
394 block_count = dev_block_count;
396 for (i = 0; i < 6; i++)
397 blocks[i] = BTRFS_SUPER_INFO_OFFSET + leafsize * i;
399 ret = make_btrfs(fd, file, label, blocks, block_count,
400 nodesize, leafsize,
401 sectorsize, stripesize);
402 if (ret) {
403 fprintf(stderr, "error during mkfs %d\n", ret);
404 exit(1);
406 ret = make_root_dir(fd, file);
407 if (ret) {
408 fprintf(stderr, "failed to setup the root directory\n");
409 exit(1);
411 root = open_ctree(file, 0);
412 root->fs_info->alloc_start = alloc_start;
413 trans = btrfs_start_transaction(root, 1);
415 if (ac == 0)
416 goto raid_groups;
418 btrfs_register_one_device(file);
419 if (!root) {
420 fprintf(stderr, "ctree init failed\n");
421 return -1;
424 zero_end = 1;
425 while(ac-- > 0) {
426 file = av[optind++];
427 ret = check_mounted(file);
428 if (ret < 0) {
429 fprintf(stderr, "error checking %s mount status\n",
430 file);
431 exit(1);
433 if (ret == 1) {
434 fprintf(stderr, "%s is mounted\n", file);
435 exit(1);
437 fd = open(file, O_RDWR);
438 if (fd < 0) {
439 fprintf(stderr, "unable to open %s\n", file);
440 exit(1);
442 ret = btrfs_device_already_in_root(root, fd,
443 BTRFS_SUPER_INFO_OFFSET);
444 if (ret) {
445 fprintf(stderr, "skipping duplicate device %s in FS\n",
446 file);
447 close(fd);
448 continue;
450 ret = btrfs_prepare_device(fd, file, zero_end,
451 &dev_block_count);
453 BUG_ON(ret);
455 ret = btrfs_add_to_fsid(trans, root, fd, file, dev_block_count,
456 sectorsize, sectorsize, sectorsize);
457 BUG_ON(ret);
458 btrfs_register_one_device(file);
461 raid_groups:
462 ret = create_raid_groups(trans, root, data_profile,
463 metadata_profile);
465 printf("fs created label %s on %s\n\tnodesize %u leafsize %u "
466 "sectorsize %u size %s\n",
467 label, first_file, nodesize, leafsize, sectorsize,
468 pretty_sizes(btrfs_super_total_bytes(&root->fs_info->super_copy)));
470 btrfs_commit_transaction(trans, root);
471 ret = close_ctree(root);
472 BUG_ON(ret);
474 free(label);
475 return 0;