4d28bdf0cca720e5156142b43fc9dabf43292148
[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->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);
97
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);
102
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);
112
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;
129
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;
135
136         btrfs_commit_transaction(trans, root);
137         ret = close_ctree(root);
138 err:
139         return ret;
140 }
141
142 static int create_one_raid_group(struct btrfs_trans_handle *trans,
143                               struct btrfs_root *root, u64 type)
144 {
145         u64 chunk_start;
146         u64 chunk_size;
147         int ret;
148
149         ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
150                                 &chunk_start, &chunk_size, type);
151         BUG_ON(ret);
152         ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
153                                      type, BTRFS_CHUNK_TREE_OBJECTID,
154                                      chunk_start, chunk_size);
155         BUG_ON(ret);
156         return ret;
157 }
158
159 static int create_raid_groups(struct btrfs_trans_handle *trans,
160                               struct btrfs_root *root, u64 data_profile,
161                               u64 metadata_profile)
162 {
163         u64 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy);
164         u64 allowed;
165         int ret;
166
167         if (num_devices == 1)
168                 allowed = BTRFS_BLOCK_GROUP_DUP;
169         else
170                 allowed = BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1;
171
172         if (allowed & metadata_profile) {
173                 ret = create_one_raid_group(trans, root,
174                                             BTRFS_BLOCK_GROUP_METADATA |
175                                             (allowed & metadata_profile));
176                 BUG_ON(ret);
177         }
178         if (num_devices > 1 && (allowed & data_profile)) {
179                 ret = create_one_raid_group(trans, root,
180                                             BTRFS_BLOCK_GROUP_DATA |
181                                             (allowed & data_profile));
182                 BUG_ON(ret);
183         }
184         return 0;
185 }
186
187 static void print_usage(void)
188 {
189         fprintf(stderr, "usage: mkfs.btrfs [options] dev [ dev ... ]\n");
190         fprintf(stderr, "options:\n");
191         fprintf(stderr, "\t -b --byte-count total number of bytes in the FS\n");
192         fprintf(stderr, "\t -l --leafsize size of btree leaves\n");
193         fprintf(stderr, "\t -n --nodesize size of btree leaves\n");
194         fprintf(stderr, "\t -s --sectorsize min block allocation\n");
195         exit(1);
196 }
197
198 static u64 parse_profile(char *s)
199 {
200         if (strcmp(s, "raid0") == 0) {
201                 return BTRFS_BLOCK_GROUP_RAID0;
202         } else if (strcmp(s, "raid1") == 0) {
203                 return BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP;
204         } else if (strcmp(s, "single") == 0) {
205                 return 0;
206         } else {
207                 fprintf(stderr, "Unknown option %s\n", s);
208                 print_usage();
209         }
210         return 0;
211 }
212
213 static struct option long_options[] = {
214         { "byte-count", 1, NULL, 'b' },
215         { "leafsize", 1, NULL, 'l' },
216         { "nodesize", 1, NULL, 'n' },
217         { "sectorsize", 1, NULL, 's' },
218         { "metadata", 1, NULL, 'm' },
219         { "data", 1, NULL, 'd' },
220         { 0, 0, 0, 0}
221 };
222
223 int main(int ac, char **av)
224 {
225         char *file;
226         u64 block_count = 0;
227         u64 dev_block_count = 0;
228         int fd;
229         int first_fd;
230         int ret;
231         int i;
232         u32 leafsize = 16 * 1024;
233         u32 sectorsize = 4096;
234         u32 nodesize = 16 * 1024;
235         u32 stripesize = 4096;
236         u64 blocks[6];
237         int zero_end = 1;
238         int option_index = 0;
239         struct btrfs_root *root;
240         struct btrfs_trans_handle *trans;
241         u64 metadata_profile = BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP;
242         u64 data_profile = BTRFS_BLOCK_GROUP_RAID0;
243
244         while(1) {
245                 int c;
246                 c = getopt_long(ac, av, "b:l:n:s:m:d:", long_options,
247                                 &option_index);
248                 if (c < 0)
249                         break;
250                 switch(c) {
251                         case 'd':
252                                 data_profile = parse_profile(optarg);
253                                 break;
254                         case 'm':
255                                 metadata_profile = parse_profile(optarg);
256                                 break;
257                         case 'l':
258                                 leafsize = parse_size(optarg);
259                                 break;
260                         case 'n':
261                                 nodesize = parse_size(optarg);
262                                 break;
263                         case 's':
264                                 sectorsize = parse_size(optarg);
265                                 break;
266                         case 'b':
267                                 block_count = parse_size(optarg);
268                                 zero_end = 0;
269                                 break;
270                         default:
271                                 print_usage();
272                 }
273         }
274         sectorsize = max(sectorsize, (u32)getpagesize());
275         if (leafsize < sectorsize || (leafsize & (sectorsize - 1))) {
276                 fprintf(stderr, "Illegal leafsize %u\n", leafsize);
277                 exit(1);
278         }
279         if (nodesize < sectorsize || (nodesize & (sectorsize - 1))) {
280                 fprintf(stderr, "Illegal nodesize %u\n", nodesize);
281                 exit(1);
282         }
283         ac = ac - optind;
284         if (ac == 0)
285                 print_usage();
286
287         file = av[optind++];
288         ret = check_mounted(file);
289         if (ret < 0) {
290                 fprintf(stderr, "error checking %s mount status\n", file);
291                 exit(1);
292         }
293         if (ret == 1) {
294                 fprintf(stderr, "%s is mounted\n", file);
295                 exit(1);
296         }
297         ac--;
298         fd = open(file, O_RDWR);
299         if (fd < 0) {
300                 fprintf(stderr, "unable to open %s\n", file);
301                 exit(1);
302         }
303         first_fd = fd;
304         ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count);
305         if (block_count == 0)
306                 block_count = dev_block_count;
307
308         for (i = 0; i < 6; i++)
309                 blocks[i] = BTRFS_SUPER_INFO_OFFSET + leafsize * i;
310
311         ret = make_btrfs(fd, file, blocks, block_count, nodesize, leafsize,
312                          sectorsize, stripesize);
313         if (ret) {
314                 fprintf(stderr, "error during mkfs %d\n", ret);
315                 exit(1);
316         }
317         ret = make_root_dir(fd, file);
318         if (ret) {
319                 fprintf(stderr, "failed to setup the root directory\n");
320                 exit(1);
321         }
322         printf("fs created on %s nodesize %u leafsize %u sectorsize %u bytes %llu\n",
323                file, nodesize, leafsize, sectorsize,
324                (unsigned long long)block_count);
325
326         root = open_ctree(file, 0);
327         trans = btrfs_start_transaction(root, 1);
328
329         if (ac == 0)
330                 goto raid_groups;
331
332         btrfs_register_one_device(file);
333         if (!root) {
334                 fprintf(stderr, "ctree init failed\n");
335                 return -1;
336         }
337
338         zero_end = 1;
339         while(ac-- > 0) {
340                 file = av[optind++];
341                 ret = check_mounted(file);
342                 if (ret < 0) {
343                         fprintf(stderr, "error checking %s mount status\n",
344                                 file);
345                         exit(1);
346                 }
347                 if (ret == 1) {
348                         fprintf(stderr, "%s is mounted\n", file);
349                         exit(1);
350                 }
351                 fd = open(file, O_RDWR);
352                 if (fd < 0) {
353                         fprintf(stderr, "unable to open %s\n", file);
354                         exit(1);
355                 }
356                 fprintf(stderr, "adding device %s\n", file);
357                 ret = btrfs_prepare_device(fd, file, zero_end,
358                                            &dev_block_count);
359
360                 BUG_ON(ret);
361
362                 ret = btrfs_add_to_fsid(trans, root, fd, dev_block_count,
363                                         sectorsize, sectorsize, sectorsize);
364                 BUG_ON(ret);
365                 close(fd);
366                 btrfs_register_one_device(file);
367         }
368
369 raid_groups:
370         ret = create_raid_groups(trans, root, data_profile,
371                                  metadata_profile);
372         btrfs_commit_transaction(trans, root);
373         ret = close_ctree(root);
374         BUG_ON(ret);
375         return 0;
376 }
377