Add mkfs.btrfs -A offset to control allocation start on devices
[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 #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"
37
38 static u64 parse_size(char *s)
39 {
40         int len = strlen(s);
41         char c;
42         u64 mult = 1;
43
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);
58                 }
59                 s[len - 1] = '\0';
60         }
61         return atol(s) * mult;
62 }
63
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;
72
73         root = open_ctree_fd(fd, device_name, 0);
74
75         if (!root) {
76                 fprintf(stderr, "ctree init failed\n");
77                 return -1;
78         }
79         trans = btrfs_start_transaction(root, 1);
80         bytes_used = btrfs_super_bytes_used(&root->fs_info->super_copy);
81
82         root->fs_info->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);
88
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);
98
99         root->fs_info->system_allocs = 0;
100         btrfs_commit_transaction(trans, root);
101         trans = btrfs_start_transaction(root, 1);
102         BUG_ON(!trans);
103
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);
113
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;
130
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;
136
137         btrfs_commit_transaction(trans, root);
138         ret = close_ctree(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         return 0;
177 }
178
179 static int create_one_raid_group(struct btrfs_trans_handle *trans,
180                               struct btrfs_root *root, u64 type)
181 {
182         u64 chunk_start;
183         u64 chunk_size;
184         int ret;
185
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;
194 }
195
196 static int create_raid_groups(struct btrfs_trans_handle *trans,
197                               struct btrfs_root *root, u64 data_profile,
198                               u64 metadata_profile)
199 {
200         u64 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy);
201         u64 allowed;
202         int ret;
203
204         if (num_devices == 1)
205                 allowed = BTRFS_BLOCK_GROUP_DUP;
206         else if (num_devices >= 4) {
207                 allowed = BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
208                         BTRFS_BLOCK_GROUP_RAID10;
209         } else
210                 allowed = BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1;
211
212         if (allowed & metadata_profile) {
213                 ret = create_one_raid_group(trans, root,
214                                             BTRFS_BLOCK_GROUP_SYSTEM |
215                                             (allowed & metadata_profile));
216                 BUG_ON(ret);
217
218                 ret = create_one_raid_group(trans, root,
219                                             BTRFS_BLOCK_GROUP_METADATA |
220                                             (allowed & metadata_profile));
221                 BUG_ON(ret);
222
223                 ret = recow_roots(trans, root);
224                 BUG_ON(ret);
225         }
226         if (num_devices > 1 && (allowed & data_profile)) {
227                 ret = create_one_raid_group(trans, root,
228                                             BTRFS_BLOCK_GROUP_DATA |
229                                             (allowed & data_profile));
230                 BUG_ON(ret);
231         }
232         return 0;
233 }
234
235 static void print_usage(void)
236 {
237         fprintf(stderr, "usage: mkfs.btrfs [options] dev [ dev ... ]\n");
238         fprintf(stderr, "options:\n");
239         fprintf(stderr, "\t -b --byte-count total number of bytes in the FS\n");
240         fprintf(stderr, "\t -l --leafsize size of btree leaves\n");
241         fprintf(stderr, "\t -n --nodesize size of btree leaves\n");
242         fprintf(stderr, "\t -s --sectorsize min block allocation\n");
243         exit(1);
244 }
245
246 static u64 parse_profile(char *s)
247 {
248         if (strcmp(s, "raid0") == 0) {
249                 return BTRFS_BLOCK_GROUP_RAID0;
250         } else if (strcmp(s, "raid1") == 0) {
251                 return BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP;
252         } else if (strcmp(s, "raid10") == 0) {
253                 return BTRFS_BLOCK_GROUP_RAID10 | BTRFS_BLOCK_GROUP_DUP;
254         } else if (strcmp(s, "single") == 0) {
255                 return 0;
256         } else {
257                 fprintf(stderr, "Unknown option %s\n", s);
258                 print_usage();
259         }
260         return 0;
261 }
262
263 static char *parse_label(char *input)
264 {
265         int i;
266         int len = strlen(input);
267
268         if (len > BTRFS_LABEL_SIZE) {
269                 fprintf(stderr, "Label %s is too long (max %d)\n", input,
270                         BTRFS_LABEL_SIZE);
271                 exit(1);
272         }
273         for (i = 0; i < len; i++) {
274                 if (input[i] == '/' || input[i] == '\\') {
275                         fprintf(stderr, "invalid label %s\n", input);
276                         exit(1);
277                 }
278         }
279         return strdup(input);
280 }
281
282 static struct option long_options[] = {
283         { "alloc-start", 1, NULL, 'A'},
284         { "byte-count", 1, NULL, 'b' },
285         { "leafsize", 1, NULL, 'l' },
286         { "label", 1, NULL, 'L'},
287         { "metadata", 1, NULL, 'm' },
288         { "nodesize", 1, NULL, 'n' },
289         { "sectorsize", 1, NULL, 's' },
290         { "data", 1, NULL, 'd' },
291         { 0, 0, 0, 0}
292 };
293
294 int main(int ac, char **av)
295 {
296         char *file;
297         struct btrfs_root *root;
298         struct btrfs_trans_handle *trans;
299         char *label = NULL;
300         char *first_file;
301         u64 block_count = 0;
302         u64 dev_block_count = 0;
303         u64 blocks[6];
304         u64 alloc_start;
305         u64 metadata_profile = BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP;
306         u64 data_profile = BTRFS_BLOCK_GROUP_RAID0;
307         u32 leafsize = getpagesize();
308         u32 sectorsize = 4096;
309         u32 nodesize = leafsize;
310         u32 stripesize = 4096;
311         int zero_end = 1;
312         int option_index = 0;
313         int fd;
314         int first_fd;
315         int ret;
316         int i;
317
318         while(1) {
319                 int c;
320                 c = getopt_long(ac, av, "A:b:l:n:s:m:d:L:", long_options,
321                                 &option_index);
322                 if (c < 0)
323                         break;
324                 switch(c) {
325                         case 'A':
326                                 alloc_start = parse_size(optarg);
327                                 break;
328                         case 'd':
329                                 data_profile = parse_profile(optarg);
330                                 break;
331                         case 'l':
332                                 leafsize = parse_size(optarg);
333                                 break;
334                         case 'L':
335                                 label = parse_label(optarg);
336                                 break;
337                         case 'm':
338                                 metadata_profile = parse_profile(optarg);
339                                 break;
340                         case 'n':
341                                 nodesize = parse_size(optarg);
342                                 break;
343                         case 's':
344                                 sectorsize = parse_size(optarg);
345                                 break;
346                         case 'b':
347                                 block_count = parse_size(optarg);
348                                 zero_end = 0;
349                                 break;
350                         default:
351                                 print_usage();
352                 }
353         }
354         sectorsize = max(sectorsize, (u32)getpagesize());
355         if (leafsize < sectorsize || (leafsize & (sectorsize - 1))) {
356                 fprintf(stderr, "Illegal leafsize %u\n", leafsize);
357                 exit(1);
358         }
359         if (nodesize < sectorsize || (nodesize & (sectorsize - 1))) {
360                 fprintf(stderr, "Illegal nodesize %u\n", nodesize);
361                 exit(1);
362         }
363         ac = ac - optind;
364         if (ac == 0)
365                 print_usage();
366
367         file = av[optind++];
368         ret = check_mounted(file);
369         if (ret < 0) {
370                 fprintf(stderr, "error checking %s mount status\n", file);
371                 exit(1);
372         }
373         if (ret == 1) {
374                 fprintf(stderr, "%s is mounted\n", file);
375                 exit(1);
376         }
377         ac--;
378         fd = open(file, O_RDWR);
379         if (fd < 0) {
380                 fprintf(stderr, "unable to open %s\n", file);
381                 exit(1);
382         }
383         first_fd = fd;
384         first_file = file;
385         ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count);
386         if (block_count == 0)
387                 block_count = dev_block_count;
388
389         for (i = 0; i < 6; i++)
390                 blocks[i] = BTRFS_SUPER_INFO_OFFSET + leafsize * i;
391
392         ret = make_btrfs(fd, file, label, blocks, block_count,
393                          nodesize, leafsize,
394                          sectorsize, stripesize);
395         if (ret) {
396                 fprintf(stderr, "error during mkfs %d\n", ret);
397                 exit(1);
398         }
399         ret = make_root_dir(fd, file);
400         if (ret) {
401                 fprintf(stderr, "failed to setup the root directory\n");
402                 exit(1);
403         }
404         root = open_ctree(file, 0);
405         root->fs_info->alloc_start = alloc_start;
406         trans = btrfs_start_transaction(root, 1);
407
408         if (ac == 0)
409                 goto raid_groups;
410
411         btrfs_register_one_device(file);
412         if (!root) {
413                 fprintf(stderr, "ctree init failed\n");
414                 return -1;
415         }
416
417         zero_end = 1;
418         while(ac-- > 0) {
419                 file = av[optind++];
420                 ret = check_mounted(file);
421                 if (ret < 0) {
422                         fprintf(stderr, "error checking %s mount status\n",
423                                 file);
424                         exit(1);
425                 }
426                 if (ret == 1) {
427                         fprintf(stderr, "%s is mounted\n", file);
428                         exit(1);
429                 }
430                 fd = open(file, O_RDWR);
431                 if (fd < 0) {
432                         fprintf(stderr, "unable to open %s\n", file);
433                         exit(1);
434                 }
435                 ret = btrfs_device_already_in_root(root, fd,
436                                                    BTRFS_SUPER_INFO_OFFSET);
437                 if (ret) {
438                         fprintf(stderr, "skipping duplicate device %s in FS\n",
439                                 file);
440                         close(fd);
441                         continue;
442                 }
443                 ret = btrfs_prepare_device(fd, file, zero_end,
444                                            &dev_block_count);
445
446                 BUG_ON(ret);
447
448                 ret = btrfs_add_to_fsid(trans, root, fd, file, dev_block_count,
449                                         sectorsize, sectorsize, sectorsize);
450                 BUG_ON(ret);
451                 btrfs_register_one_device(file);
452         }
453
454 raid_groups:
455         ret = create_raid_groups(trans, root, data_profile,
456                                  metadata_profile);
457
458         printf("fs created label %s on %s\n\tnodesize %u leafsize %u "
459             "sectorsize %u size %s\n",
460             label, first_file, nodesize, leafsize, sectorsize,
461             pretty_sizes(btrfs_super_total_bytes(&root->fs_info->super_copy)));
462
463         btrfs_commit_transaction(trans, root);
464         ret = close_ctree(root);
465         BUG_ON(ret);
466
467         free(label);
468         return 0;
469 }
470