2e99b95290d82ac7daad78846bee8791f8cc7a83
[platform/upstream/btrfs-progs.git] / mkfs.c
1 /*
2  * Copyright (C) 2007 Oracle.  All rights reserved.
3  *
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.
7  *
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.
12  *
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.
17  */
18
19 #define _XOPEN_SOURCE 500
20 #define _GNU_SOURCE
21
22 #ifndef __CHECKER__
23 #include <sys/ioctl.h>
24 #include <sys/mount.h>
25 #include "ioctl.h"
26 #endif
27
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"
45
46 static u64 parse_size(char *s)
47 {
48         int len = strlen(s);
49         char c;
50         u64 mult = 1;
51
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);
66                 }
67                 s[len - 1] = '\0';
68         }
69         return atol(s) * mult;
70 }
71
72 static int make_root_dir(struct btrfs_root *root)
73 {
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;
80
81         trans = btrfs_start_transaction(root, 1);
82         bytes_used = btrfs_super_bytes_used(&root->fs_info->super_copy);
83
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);
90
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);
100
101         root->fs_info->system_allocs = 0;
102         btrfs_commit_transaction(trans, root);
103         trans = btrfs_start_transaction(root, 1);
104         BUG_ON(!trans);
105
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);
115
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;
131
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;
137
138         btrfs_commit_transaction(trans, root);
139 err:
140         return ret;
141 }
142
143 static int recow_roots(struct btrfs_trans_handle *trans,
144                        struct btrfs_root *root)
145 {
146         int ret;
147         struct extent_buffer *tmp;
148         struct btrfs_fs_info *info = root->fs_info;
149
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);
154
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);
159
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);
164
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);
169
170
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);
175
176         ret = __btrfs_cow_block(trans, info->csum_root, info->csum_root->node,
177                                 NULL, 0, &tmp, 0, 0);
178         BUG_ON(ret);
179         free_extent_buffer(tmp);
180
181         return 0;
182 }
183
184 static int create_one_raid_group(struct btrfs_trans_handle *trans,
185                               struct btrfs_root *root, u64 type)
186 {
187         u64 chunk_start;
188         u64 chunk_size;
189         int ret;
190
191         ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
192                                 &chunk_start, &chunk_size, type);
193         BUG_ON(ret);
194         ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
195                                      type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
196                                      chunk_start, chunk_size);
197         BUG_ON(ret);
198         return ret;
199 }
200
201 static int create_raid_groups(struct btrfs_trans_handle *trans,
202                               struct btrfs_root *root, u64 data_profile,
203                               u64 metadata_profile)
204 {
205         u64 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy);
206         u64 allowed;
207         int ret;
208
209         if (num_devices == 1)
210                 allowed = BTRFS_BLOCK_GROUP_DUP;
211         else if (num_devices >= 4) {
212                 allowed = BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
213                         BTRFS_BLOCK_GROUP_RAID10;
214         } else
215                 allowed = BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1;
216
217         if (allowed & metadata_profile) {
218                 ret = create_one_raid_group(trans, root,
219                                             BTRFS_BLOCK_GROUP_SYSTEM |
220                                             (allowed & metadata_profile));
221                 BUG_ON(ret);
222
223                 ret = create_one_raid_group(trans, root,
224                                             BTRFS_BLOCK_GROUP_METADATA |
225                                             (allowed & metadata_profile));
226                 BUG_ON(ret);
227
228                 ret = recow_roots(trans, root);
229                 BUG_ON(ret);
230         }
231         if (num_devices > 1 && (allowed & data_profile)) {
232                 ret = create_one_raid_group(trans, root,
233                                             BTRFS_BLOCK_GROUP_DATA |
234                                             (allowed & data_profile));
235                 BUG_ON(ret);
236         }
237         return 0;
238 }
239
240 static int create_data_reloc_tree(struct btrfs_trans_handle *trans,
241                                   struct btrfs_root *root)
242 {
243         struct btrfs_key location;
244         struct btrfs_root_item root_item;
245         struct extent_buffer *tmp;
246         u64 objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
247         int ret;
248
249         ret = btrfs_copy_root(trans, root, root->node, &tmp, objectid);
250         BUG_ON(ret);
251
252         memcpy(&root_item, &root->root_item, sizeof(root_item));
253         btrfs_set_root_bytenr(&root_item, tmp->start);
254         btrfs_set_root_level(&root_item, btrfs_header_level(tmp));
255         btrfs_set_root_generation(&root_item, trans->transid);
256         free_extent_buffer(tmp);
257
258         location.objectid = objectid;
259         location.type = BTRFS_ROOT_ITEM_KEY;
260         location.offset = 0;
261         ret = btrfs_insert_root(trans, root->fs_info->tree_root,
262                                 &location, &root_item);
263         BUG_ON(ret);
264         return 0;
265 }
266
267 static void print_usage(void)
268 {
269         fprintf(stderr, "usage: mkfs.btrfs [options] dev [ dev ... ]\n");
270         fprintf(stderr, "options:\n");
271         fprintf(stderr, "\t -A --alloc-start the offset to start the FS\n");
272         fprintf(stderr, "\t -b --byte-count total number of bytes in the FS\n");
273         fprintf(stderr, "\t -d --data data profile, raid0, raid1, raid10 or single\n");
274         fprintf(stderr, "\t -l --leafsize size of btree leaves\n");
275         fprintf(stderr, "\t -L --label set a label\n");
276         fprintf(stderr, "\t -m --metadata metadata profile, values like data profile\n");
277         fprintf(stderr, "\t -n --nodesize size of btree nodes\n");
278         fprintf(stderr, "\t -s --sectorsize min block allocation\n");
279         fprintf(stderr, "%s\n", BTRFS_BUILD_VERSION);
280         exit(1);
281 }
282
283 static void print_version(void)
284 {
285         fprintf(stderr, "mkfs.btrfs, part of %s\n", BTRFS_BUILD_VERSION);
286         exit(0);
287 }
288
289 static u64 parse_profile(char *s)
290 {
291         if (strcmp(s, "raid0") == 0) {
292                 return BTRFS_BLOCK_GROUP_RAID0;
293         } else if (strcmp(s, "raid1") == 0) {
294                 return BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP;
295         } else if (strcmp(s, "raid10") == 0) {
296                 return BTRFS_BLOCK_GROUP_RAID10 | BTRFS_BLOCK_GROUP_DUP;
297         } else if (strcmp(s, "single") == 0) {
298                 return 0;
299         } else {
300                 fprintf(stderr, "Unknown option %s\n", s);
301                 print_usage();
302         }
303         return 0;
304 }
305
306 static char *parse_label(char *input)
307 {
308         int i;
309         int len = strlen(input);
310
311         if (len > BTRFS_LABEL_SIZE) {
312                 fprintf(stderr, "Label %s is too long (max %d)\n", input,
313                         BTRFS_LABEL_SIZE);
314                 exit(1);
315         }
316         for (i = 0; i < len; i++) {
317                 if (input[i] == '/' || input[i] == '\\') {
318                         fprintf(stderr, "invalid label %s\n", input);
319                         exit(1);
320                 }
321         }
322         return strdup(input);
323 }
324
325 static struct option long_options[] = {
326         { "alloc-start", 1, NULL, 'A'},
327         { "byte-count", 1, NULL, 'b' },
328         { "leafsize", 1, NULL, 'l' },
329         { "label", 1, NULL, 'L'},
330         { "metadata", 1, NULL, 'm' },
331         { "nodesize", 1, NULL, 'n' },
332         { "sectorsize", 1, NULL, 's' },
333         { "data", 1, NULL, 'd' },
334         { "version", 0, NULL, 'V' },
335         { 0, 0, 0, 0}
336 };
337
338 int main(int ac, char **av)
339 {
340         char *file;
341         struct btrfs_root *root;
342         struct btrfs_trans_handle *trans;
343         char *label = NULL;
344         char *first_file;
345         u64 block_count = 0;
346         u64 dev_block_count = 0;
347         u64 blocks[7];
348         u64 alloc_start = 0;
349         u64 metadata_profile = BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP;
350         u64 data_profile = BTRFS_BLOCK_GROUP_RAID0;
351         u32 leafsize = getpagesize();
352         u32 sectorsize = 4096;
353         u32 nodesize = leafsize;
354         u32 stripesize = 4096;
355         int zero_end = 1;
356         int option_index = 0;
357         int fd;
358         int first_fd;
359         int ret;
360         int i;
361
362         while(1) {
363                 int c;
364                 c = getopt_long(ac, av, "A:b:l:n:s:m:d:L:V", long_options,
365                                 &option_index);
366                 if (c < 0)
367                         break;
368                 switch(c) {
369                         case 'A':
370                                 alloc_start = parse_size(optarg);
371                                 break;
372                         case 'd':
373                                 data_profile = parse_profile(optarg);
374                                 break;
375                         case 'l':
376                                 leafsize = parse_size(optarg);
377                                 break;
378                         case 'L':
379                                 label = parse_label(optarg);
380                                 break;
381                         case 'm':
382                                 metadata_profile = parse_profile(optarg);
383                                 break;
384                         case 'n':
385                                 nodesize = parse_size(optarg);
386                                 break;
387                         case 's':
388                                 sectorsize = parse_size(optarg);
389                                 break;
390                         case 'b':
391                                 block_count = parse_size(optarg);
392                                 if (block_count < 256*1024*1024) {
393                                         fprintf(stderr, "File system size "
394                                                 "%llu bytes is too small, "
395                                                 "256M is required at least\n",
396                                                 (unsigned long long)block_count);
397                                         exit(1);
398                                 }
399                                 zero_end = 0;
400                                 break;
401                         case 'V':
402                                 print_version();
403                                 break;
404                         default:
405                                 print_usage();
406                 }
407         }
408         sectorsize = max(sectorsize, (u32)getpagesize());
409         if (leafsize < sectorsize || (leafsize & (sectorsize - 1))) {
410                 fprintf(stderr, "Illegal leafsize %u\n", leafsize);
411                 exit(1);
412         }
413         if (nodesize < sectorsize || (nodesize & (sectorsize - 1))) {
414                 fprintf(stderr, "Illegal nodesize %u\n", nodesize);
415                 exit(1);
416         }
417         ac = ac - optind;
418         if (ac == 0)
419                 print_usage();
420
421         printf("\nWARNING! - %s IS EXPERIMENTAL\n", BTRFS_BUILD_VERSION);
422         printf("WARNING! - see http://btrfs.wiki.kernel.org before using\n\n");
423
424         file = av[optind++];
425         ret = check_mounted(file);
426         if (ret < 0) {
427                 fprintf(stderr, "error checking %s mount status\n", file);
428                 exit(1);
429         }
430         if (ret == 1) {
431                 fprintf(stderr, "%s is mounted\n", file);
432                 exit(1);
433         }
434         ac--;
435         fd = open(file, O_RDWR);
436         if (fd < 0) {
437                 fprintf(stderr, "unable to open %s\n", file);
438                 exit(1);
439         }
440         first_fd = fd;
441         first_file = file;
442         ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count);
443         if (block_count == 0)
444                 block_count = dev_block_count;
445
446         blocks[0] = BTRFS_SUPER_INFO_OFFSET;
447         for (i = 1; i < 7; i++) {
448                 blocks[i] = BTRFS_SUPER_INFO_OFFSET + 1024 * 1024 +
449                         leafsize * i;
450         }
451
452         ret = make_btrfs(fd, file, label, blocks, block_count,
453                          nodesize, leafsize,
454                          sectorsize, stripesize);
455         if (ret) {
456                 fprintf(stderr, "error during mkfs %d\n", ret);
457                 exit(1);
458         }
459         root = open_ctree(file, 0, O_RDWR);
460         root->fs_info->alloc_start = alloc_start;
461
462         ret = make_root_dir(root);
463         if (ret) {
464                 fprintf(stderr, "failed to setup the root directory\n");
465                 exit(1);
466         }
467
468         trans = btrfs_start_transaction(root, 1);
469
470         if (ac == 0)
471                 goto raid_groups;
472
473         btrfs_register_one_device(file);
474         if (!root) {
475                 fprintf(stderr, "ctree init failed\n");
476                 return -1;
477         }
478
479         zero_end = 1;
480         while(ac-- > 0) {
481                 file = av[optind++];
482                 ret = check_mounted(file);
483                 if (ret < 0) {
484                         fprintf(stderr, "error checking %s mount status\n",
485                                 file);
486                         exit(1);
487                 }
488                 if (ret == 1) {
489                         fprintf(stderr, "%s is mounted\n", file);
490                         exit(1);
491                 }
492                 fd = open(file, O_RDWR);
493                 if (fd < 0) {
494                         fprintf(stderr, "unable to open %s\n", file);
495                         exit(1);
496                 }
497                 ret = btrfs_device_already_in_root(root, fd,
498                                                    BTRFS_SUPER_INFO_OFFSET);
499                 if (ret) {
500                         fprintf(stderr, "skipping duplicate device %s in FS\n",
501                                 file);
502                         close(fd);
503                         continue;
504                 }
505                 ret = btrfs_prepare_device(fd, file, zero_end,
506                                            &dev_block_count);
507
508                 BUG_ON(ret);
509
510                 ret = btrfs_add_to_fsid(trans, root, fd, file, dev_block_count,
511                                         sectorsize, sectorsize, sectorsize);
512                 BUG_ON(ret);
513                 btrfs_register_one_device(file);
514         }
515
516 raid_groups:
517         ret = create_raid_groups(trans, root, data_profile,
518                                  metadata_profile);
519         BUG_ON(ret);
520
521         ret = create_data_reloc_tree(trans, root);
522         BUG_ON(ret);
523
524         printf("fs created label %s on %s\n\tnodesize %u leafsize %u "
525             "sectorsize %u size %s\n",
526             label, first_file, nodesize, leafsize, sectorsize,
527             pretty_sizes(btrfs_super_total_bytes(&root->fs_info->super_copy)));
528
529         printf("%s\n", BTRFS_BUILD_VERSION);
530         btrfs_commit_transaction(trans, root);
531         ret = close_ctree(root);
532         BUG_ON(ret);
533
534         free(label);
535         return 0;
536 }
537