btrfs-progs: Fix printf format casting errors
[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 #include <stdio.h>
21 #include <stdlib.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <unistd.h>
26 #include <uuid/uuid.h>
27 #include <linux/fs.h>
28 #include <ctype.h>
29 #include "kerncompat.h"
30 #include "ctree.h"
31 #include "disk-io.h"
32 #include "volumes.h"
33 #include "transaction.h"
34 #include "utils.h"
35
36 static u64 parse_size(char *s)
37 {
38         int len = strlen(s);
39         char c;
40         u64 mult = 1;
41
42         if (!isdigit(s[len - 1])) {
43                 c = tolower(s[len - 1]);
44                 switch (c) {
45                 case 'g':
46                         mult *= 1024;
47                 case 'm':
48                         mult *= 1024;
49                 case 'k':
50                         mult *= 1024;
51                 case 'b':
52                         break;
53                 default:
54                         fprintf(stderr, "Unknown size descriptor %c\n", c);
55                         exit(1);
56                 }
57                 s[len - 1] = '\0';
58         }
59         return atol(s) * mult;
60 }
61
62 static int make_root_dir(int fd, const char *device_name) {
63         struct btrfs_root *root;
64         struct btrfs_trans_handle *trans;
65         struct btrfs_key location;
66         u64 bytes_used;
67         u64 chunk_start = 0;
68         u64 chunk_size = 0;
69         int ret;
70
71         root = open_ctree_fd(fd, device_name, 0);
72
73         if (!root) {
74                 fprintf(stderr, "ctree init failed\n");
75                 return -1;
76         }
77         trans = btrfs_start_transaction(root, 1);
78         bytes_used = btrfs_super_bytes_used(&root->fs_info->super_copy);
79
80         root->fs_info->force_system_allocs = 1;
81         ret = btrfs_make_block_group(trans, root, bytes_used,
82                                      BTRFS_BLOCK_GROUP_SYSTEM,
83                                      BTRFS_CHUNK_TREE_OBJECTID,
84                                      0, BTRFS_MKFS_SYSTEM_GROUP_SIZE);
85         BUG_ON(ret);
86         ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
87                                 &chunk_start, &chunk_size,
88                                 BTRFS_BLOCK_GROUP_METADATA);
89         BUG_ON(ret);
90         ret = btrfs_make_block_group(trans, root, 0,
91                                      BTRFS_BLOCK_GROUP_METADATA,
92                                      BTRFS_CHUNK_TREE_OBJECTID,
93                                      chunk_start, chunk_size);
94         BUG_ON(ret);
95
96         root->fs_info->force_system_allocs = 0;
97         btrfs_commit_transaction(trans, root);
98         trans = btrfs_start_transaction(root, 1);
99         BUG_ON(!trans);
100
101         ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
102                                 &chunk_start, &chunk_size,
103                                 BTRFS_BLOCK_GROUP_DATA);
104         BUG_ON(ret);
105         ret = btrfs_make_block_group(trans, root, 0,
106                                      BTRFS_BLOCK_GROUP_DATA,
107                                      BTRFS_CHUNK_TREE_OBJECTID,
108                                      chunk_start, chunk_size);
109         BUG_ON(ret);
110
111         // ret = btrfs_make_block_group(trans, root, 0, 1);
112         ret = btrfs_make_root_dir(trans, root->fs_info->tree_root,
113                               BTRFS_ROOT_TREE_DIR_OBJECTID);
114         if (ret)
115                 goto err;
116         ret = btrfs_make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID);
117         if (ret)
118                 goto err;
119         memcpy(&location, &root->fs_info->fs_root->root_key, sizeof(location));
120         location.offset = (u64)-1;
121         ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
122                         "default", 7,
123                         btrfs_super_root_dir(&root->fs_info->super_copy),
124                         &location, BTRFS_FT_DIR);
125         if (ret)
126                 goto err;
127
128         ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
129                              "default", 7, location.objectid,
130                              BTRFS_ROOT_TREE_DIR_OBJECTID);
131         if (ret)
132                 goto err;
133
134         btrfs_commit_transaction(trans, root);
135         ret = close_ctree(root);
136 err:
137         return ret;
138 }
139
140 static void print_usage(void)
141 {
142         fprintf(stderr, "usage: mkfs.btrfs [ -l leafsize ] [ -n nodesize] dev [ blocks ]\n");
143         exit(1);
144 }
145
146 int main(int ac, char **av)
147 {
148         char *file;
149         u64 block_count = 0;
150         u64 dev_block_count = 0;
151         int fd;
152         int first_fd;
153         int ret;
154         int i;
155         u32 leafsize = 16 * 1024;
156         u32 sectorsize = 4096;
157         u32 nodesize = 16 * 1024;
158         u32 stripesize = 4096;
159         u64 blocks[6];
160         int zero_end = 1;
161         struct btrfs_root *root;
162         struct btrfs_trans_handle *trans;
163
164         while(1) {
165                 int c;
166                 c = getopt(ac, av, "b:l:n:s:");
167                 if (c < 0)
168                         break;
169                 switch(c) {
170                         case 'l':
171                                 leafsize = parse_size(optarg);
172                                 break;
173                         case 'n':
174                                 nodesize = parse_size(optarg);
175                                 break;
176                         case 's':
177                                 stripesize = parse_size(optarg);
178                                 break;
179                         case 'b':
180                                 block_count = parse_size(optarg);
181                                 zero_end = 0;
182                                 break;
183                         default:
184                                 print_usage();
185                 }
186         }
187         if (leafsize < sectorsize || (leafsize & (sectorsize - 1))) {
188                 fprintf(stderr, "Illegal leafsize %u\n", leafsize);
189                 exit(1);
190         }
191         if (nodesize < sectorsize || (nodesize & (sectorsize - 1))) {
192                 fprintf(stderr, "Illegal nodesize %u\n", nodesize);
193                 exit(1);
194         }
195         ac = ac - optind;
196         if (ac == 0)
197                 print_usage();
198
199         file = av[optind++];
200         ret = check_mounted(file);
201         if (ret < 0) {
202                 fprintf(stderr, "error checking %s mount status\n", file);
203                 exit(1);
204         }
205         if (ret == 1) {
206                 fprintf(stderr, "%s is mounted\n", file);
207                 exit(1);
208         }
209         ac--;
210         fd = open(file, O_RDWR);
211         if (fd < 0) {
212                 fprintf(stderr, "unable to open %s\n", file);
213                 exit(1);
214         }
215         first_fd = fd;
216         ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count);
217         if (block_count == 0)
218                 block_count = dev_block_count;
219
220         for (i = 0; i < 6; i++)
221                 blocks[i] = BTRFS_SUPER_INFO_OFFSET + leafsize * i;
222
223         ret = make_btrfs(fd, file, blocks, block_count, nodesize, leafsize,
224                          sectorsize, stripesize);
225         if (ret) {
226                 fprintf(stderr, "error during mkfs %d\n", ret);
227                 exit(1);
228         }
229         ret = make_root_dir(fd, file);
230         if (ret) {
231                 fprintf(stderr, "failed to setup the root directory\n");
232                 exit(1);
233         }
234         printf("fs created on %s nodesize %u leafsize %u sectorsize %u bytes %llu\n",
235                file, nodesize, leafsize, sectorsize,
236                (unsigned long long)block_count);
237
238         if (ac == 0)
239                 goto done;
240
241         btrfs_register_one_device(file);
242         root = open_ctree(file, 0);
243
244         if (!root) {
245                 fprintf(stderr, "ctree init failed\n");
246                 return -1;
247         }
248         trans = btrfs_start_transaction(root, 1);
249
250         zero_end = 1;
251         while(ac-- > 0) {
252                 file = av[optind++];
253                 ret = check_mounted(file);
254                 if (ret < 0) {
255                         fprintf(stderr, "error checking %s mount status\n",
256                                 file);
257                         exit(1);
258                 }
259                 if (ret == 1) {
260                         fprintf(stderr, "%s is mounted\n", file);
261                         exit(1);
262                 }
263                 fd = open(file, O_RDWR);
264                 if (fd < 0) {
265                         fprintf(stderr, "unable to open %s\n", file);
266                         exit(1);
267                 }
268                 fprintf(stderr, "adding device %s\n", file);
269                 ret = btrfs_prepare_device(fd, file, zero_end,
270                                            &dev_block_count);
271
272                 BUG_ON(ret);
273
274                 ret = btrfs_add_to_fsid(trans, root, fd, dev_block_count,
275                                         sectorsize, sectorsize, sectorsize);
276                 BUG_ON(ret);
277                 close(fd);
278                 btrfs_register_one_device(file);
279         }
280         btrfs_commit_transaction(trans, root);
281         ret = close_ctree(root);
282         BUG_ON(ret);
283 done:
284         return 0;
285 }
286