Add rollback support for the converter
[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 #ifndef __CHECKER__
21 #include <sys/ioctl.h>
22 #include <sys/mount.h>
23 #endif
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <uuid/uuid.h>
31 #include <linux/fs.h>
32 #include <ctype.h>
33 #include "kerncompat.h"
34 #include "ctree.h"
35 #include "disk-io.h"
36 #include "transaction.h"
37 #include "utils.h"
38
39 #ifdef __CHECKER__
40 #define BLKGETSIZE64 0
41 static inline int ioctl(int fd, int define, u64 *size) { return 0; }
42 #endif
43
44 static u64 parse_size(char *s)
45 {
46         int len = strlen(s);
47         char c;
48         u64 mult = 1;
49
50         if (!isdigit(s[len - 1])) {
51                 c = tolower(s[len - 1]);
52                 switch (c) {
53                 case 'g':
54                         mult *= 1024;
55                 case 'm':
56                         mult *= 1024;
57                 case 'k':
58                         mult *= 1024;
59                 case 'b':
60                         break;
61                 default:
62                         fprintf(stderr, "Unknown size descriptor %c\n", c);
63                         exit(1);
64                 }
65                 s[len - 1] = '\0';
66         }
67         return atol(s) * mult;
68 }
69
70 static int make_root_dir(int fd) {
71         struct btrfs_root *root;
72         struct btrfs_trans_handle *trans;
73         struct btrfs_key location;
74         int ret;
75
76         root = open_ctree_fd(fd, 0);
77
78         if (!root) {
79                 fprintf(stderr, "ctree init failed\n");
80                 return -1;
81         }
82         trans = btrfs_start_transaction(root, 1);
83         ret = btrfs_make_block_groups(trans, root);
84         ret = btrfs_make_root_dir(trans, root->fs_info->tree_root,
85                               BTRFS_ROOT_TREE_DIR_OBJECTID);
86         if (ret)
87                 goto err;
88         ret = btrfs_make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID);
89         if (ret)
90                 goto err;
91         memcpy(&location, &root->fs_info->fs_root->root_key, sizeof(location));
92         location.offset = (u64)-1;
93         ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
94                         "default", 7,
95                         btrfs_super_root_dir(&root->fs_info->super_copy),
96                         &location, BTRFS_FT_DIR);
97         if (ret)
98                 goto err;
99
100         ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
101                              "default", 7, location.objectid,
102                              BTRFS_ROOT_TREE_DIR_OBJECTID);
103         if (ret)
104                 goto err;
105
106         btrfs_commit_transaction(trans, root);
107         ret = close_ctree(root);
108 err:
109         return ret;
110 }
111
112 u64 device_size(int fd, struct stat *st)
113 {
114         u64 size;
115         if (S_ISREG(st->st_mode)) {
116                 return st->st_size;
117         }
118         if (!S_ISBLK(st->st_mode)) {
119                 return 0;
120         }
121         if (ioctl(fd, BLKGETSIZE64, &size) >= 0) {
122                 return size;
123         }
124         return 0;
125 }
126
127 static void print_usage(void)
128 {
129         fprintf(stderr, "usage: mkfs.btrfs [ -l leafsize ] [ -n nodesize] dev [ blocks ]\n");
130         exit(1);
131 }
132
133 int main(int ac, char **av)
134 {
135         char *file;
136         u64 block_count = 0;
137         int fd;
138         struct stat st;
139         int ret;
140         int i;
141         u32 leafsize = 16 * 1024;
142         u32 sectorsize = 4096;
143         u32 nodesize = 16 * 1024;
144         u32 stripesize = 4096;
145         u64 blocks[4];
146         char *buf = malloc(sectorsize);
147
148         while(1) {
149                 int c;
150                 c = getopt(ac, av, "l:n:s:");
151                 if (c < 0)
152                         break;
153                 switch(c) {
154                         case 'l':
155                                 leafsize = parse_size(optarg);
156                                 break;
157                         case 'n':
158                                 nodesize = parse_size(optarg);
159                                 break;
160                         case 's':
161                                 stripesize = parse_size(optarg);
162                                 break;
163                         default:
164                                 print_usage();
165                 }
166         }
167         if (leafsize < sectorsize || (leafsize & (sectorsize - 1))) {
168                 fprintf(stderr, "Illegal leafsize %u\n", leafsize);
169                 exit(1);
170         }
171         if (nodesize < sectorsize || (nodesize & (sectorsize - 1))) {
172                 fprintf(stderr, "Illegal nodesize %u\n", nodesize);
173                 exit(1);
174         }
175         ac = ac - optind;
176         if (ac >= 1) {
177                 file = av[optind];
178                 if (ac == 2) {
179                         block_count = parse_size(av[optind + 1]);
180                         if (!block_count) {
181                                 fprintf(stderr, "error finding block count\n");
182                                 exit(1);
183                         }
184                 }
185         } else {
186                 print_usage();
187         }
188         fd = open(file, O_RDWR);
189         if (fd < 0) {
190                 fprintf(stderr, "unable to open %s\n", file);
191                 exit(1);
192         }
193         ret = fstat(fd, &st);
194         if (ret < 0) {
195                 fprintf(stderr, "unable to stat %s\n", file);
196                 exit(1);
197         }
198         if (block_count == 0) {
199                 block_count = device_size(fd, &st);
200                 if (block_count == 0) {
201                         fprintf(stderr, "unable to find %s size\n", file);
202                         exit(1);
203                 }
204         }
205         block_count /= sectorsize;
206         block_count *= sectorsize;
207
208         if (block_count < 256 * 1024 * 1024) {
209                 fprintf(stderr, "device %s is too small\n", file);
210                 exit(1);
211         }
212         memset(buf, 0, sectorsize);
213         for(i = 0; i < 64; i++) {
214                 ret = write(fd, buf, sectorsize);
215                 if (ret != sectorsize) {
216                         fprintf(stderr, "unable to zero fill device\n");
217                         exit(1);
218                 }
219         }
220         for (i = 0; i < 4; i++)
221                 blocks[i] = BTRFS_SUPER_INFO_OFFSET + leafsize * i;
222         ret = make_btrfs(fd, blocks, block_count, nodesize, leafsize,
223                          sectorsize, stripesize);
224         if (ret) {
225                 fprintf(stderr, "error during mkfs %d\n", ret);
226                 exit(1);
227         }
228         ret = make_root_dir(fd);
229         if (ret) {
230                 fprintf(stderr, "failed to setup the root directory\n");
231                 exit(1);
232         }
233         printf("fs created on %s nodesize %u leafsize %u sectorsize %u bytes %llu\n",
234                file, nodesize, leafsize, sectorsize,
235                (unsigned long long)block_count);
236         return 0;
237 }
238