Implement raid0 when multiple devices are present
[btrfs-progs-unstable/devel.git] / mkfs.c
blob49f730836007a0aff4d6b2b9ca661196b724e018
1 /*
2 * Copyright (C) 2007 Oracle. All rights reserved.
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.
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.
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.
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"
36 static u64 parse_size(char *s)
38 int len = strlen(s);
39 char c;
40 u64 mult = 1;
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);
57 s[len - 1] = '\0';
59 return atol(s) * mult;
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;
71 root = open_ctree_fd(fd, device_name, 0);
73 if (!root) {
74 fprintf(stderr, "ctree init failed\n");
75 return -1;
77 trans = btrfs_start_transaction(root, 1);
78 bytes_used = btrfs_super_bytes_used(&root->fs_info->super_copy);
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);
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);
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);
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;
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;
134 btrfs_commit_transaction(trans, root);
135 ret = close_ctree(root);
136 err:
137 return ret;
140 static void print_usage(void)
142 fprintf(stderr, "usage: mkfs.btrfs [ -l leafsize ] [ -n nodesize] dev [ blocks ]\n");
143 exit(1);
146 int main(int ac, char **av)
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;
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();
187 if (leafsize < sectorsize || (leafsize & (sectorsize - 1))) {
188 fprintf(stderr, "Illegal leafsize %u\n", leafsize);
189 exit(1);
191 if (nodesize < sectorsize || (nodesize & (sectorsize - 1))) {
192 fprintf(stderr, "Illegal nodesize %u\n", nodesize);
193 exit(1);
195 ac = ac - optind;
196 if (ac == 0)
197 print_usage();
199 file = av[optind++];
200 ac--;
201 fd = open(file, O_RDWR);
202 if (fd < 0) {
203 fprintf(stderr, "unable to open %s\n", file);
204 exit(1);
206 first_fd = fd;
207 ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count);
208 if (block_count == 0)
209 block_count = dev_block_count;
211 for (i = 0; i < 6; i++)
212 blocks[i] = BTRFS_SUPER_INFO_OFFSET + leafsize * i;
214 ret = make_btrfs(fd, file, blocks, block_count, nodesize, leafsize,
215 sectorsize, stripesize);
216 if (ret) {
217 fprintf(stderr, "error during mkfs %d\n", ret);
218 exit(1);
220 ret = make_root_dir(fd, file);
221 if (ret) {
222 fprintf(stderr, "failed to setup the root directory\n");
223 exit(1);
225 printf("fs created on %s nodesize %u leafsize %u sectorsize %u bytes %llu\n",
226 file, nodesize, leafsize, sectorsize,
227 (unsigned long long)block_count);
229 if (ac == 0)
230 goto done;
232 btrfs_register_one_device(file);
233 root = open_ctree(file, 0);
235 if (!root) {
236 fprintf(stderr, "ctree init failed\n");
237 return -1;
239 trans = btrfs_start_transaction(root, 1);
241 zero_end = 1;
242 while(ac-- > 0) {
243 file = av[optind++];
244 fd = open(file, O_RDWR);
245 if (fd < 0) {
246 fprintf(stderr, "unable to open %s\n", file);
247 exit(1);
249 fprintf(stderr, "adding device %s\n", file);
250 ret = btrfs_prepare_device(fd, file, zero_end,
251 &dev_block_count);
253 BUG_ON(ret);
255 ret = btrfs_add_to_fsid(trans, root, fd, dev_block_count,
256 sectorsize, sectorsize, sectorsize);
257 BUG_ON(ret);
258 close(fd);
259 btrfs_register_one_device(file);
261 btrfs_commit_transaction(trans, root);
262 ret = close_ctree(root);
263 BUG_ON(ret);
264 done:
265 return 0;