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
23 #include <sys/ioctl.h>
24 #include <sys/mount.h>
30 #include <sys/types.h>
35 #include <uuid/uuid.h>
38 #include "kerncompat.h"
42 #include "transaction.h"
46 static u64
parse_size(char *s
)
52 if (!isdigit(s
[len
- 1])) {
53 c
= tolower(s
[len
- 1]);
64 fprintf(stderr
, "Unknown size descriptor %c\n", c
);
69 return atol(s
) * mult
;
72 static int make_root_dir(struct btrfs_root
*root
)
74 struct btrfs_trans_handle
*trans
;
75 struct btrfs_key location
;
81 trans
= btrfs_start_transaction(root
, 1);
82 bytes_used
= btrfs_super_bytes_used(&root
->fs_info
->super_copy
);
84 root
->fs_info
->system_allocs
= 1;
85 ret
= btrfs_make_block_group(trans
, root
, bytes_used
,
86 BTRFS_BLOCK_GROUP_SYSTEM
,
87 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
88 0, BTRFS_MKFS_SYSTEM_GROUP_SIZE
);
91 ret
= btrfs_alloc_chunk(trans
, root
->fs_info
->extent_root
,
92 &chunk_start
, &chunk_size
,
93 BTRFS_BLOCK_GROUP_METADATA
);
95 ret
= btrfs_make_block_group(trans
, root
, 0,
96 BTRFS_BLOCK_GROUP_METADATA
,
97 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
98 chunk_start
, chunk_size
);
101 root
->fs_info
->system_allocs
= 0;
102 btrfs_commit_transaction(trans
, root
);
103 trans
= btrfs_start_transaction(root
, 1);
106 ret
= btrfs_alloc_chunk(trans
, root
->fs_info
->extent_root
,
107 &chunk_start
, &chunk_size
,
108 BTRFS_BLOCK_GROUP_DATA
);
110 ret
= btrfs_make_block_group(trans
, root
, 0,
111 BTRFS_BLOCK_GROUP_DATA
,
112 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
113 chunk_start
, chunk_size
);
116 ret
= btrfs_make_root_dir(trans
, root
->fs_info
->tree_root
,
117 BTRFS_ROOT_TREE_DIR_OBJECTID
);
120 ret
= btrfs_make_root_dir(trans
, root
, BTRFS_FIRST_FREE_OBJECTID
);
123 memcpy(&location
, &root
->fs_info
->fs_root
->root_key
, sizeof(location
));
124 location
.offset
= (u64
)-1;
125 ret
= btrfs_insert_dir_item(trans
, root
->fs_info
->tree_root
,
127 btrfs_super_root_dir(&root
->fs_info
->super_copy
),
128 &location
, BTRFS_FT_DIR
);
132 ret
= btrfs_insert_inode_ref(trans
, root
->fs_info
->tree_root
,
133 "default", 7, location
.objectid
,
134 BTRFS_ROOT_TREE_DIR_OBJECTID
, 0);
138 btrfs_commit_transaction(trans
, root
);
143 static int recow_roots(struct btrfs_trans_handle
*trans
,
144 struct btrfs_root
*root
)
147 struct extent_buffer
*tmp
;
148 struct btrfs_fs_info
*info
= root
->fs_info
;
150 ret
= __btrfs_cow_block(trans
, info
->fs_root
, info
->fs_root
->node
,
151 NULL
, 0, &tmp
, 0, 0);
153 free_extent_buffer(tmp
);
155 ret
= __btrfs_cow_block(trans
, info
->tree_root
, info
->tree_root
->node
,
156 NULL
, 0, &tmp
, 0, 0);
158 free_extent_buffer(tmp
);
160 ret
= __btrfs_cow_block(trans
, info
->extent_root
,
161 info
->extent_root
->node
, NULL
, 0, &tmp
, 0, 0);
163 free_extent_buffer(tmp
);
165 ret
= __btrfs_cow_block(trans
, info
->chunk_root
, info
->chunk_root
->node
,
166 NULL
, 0, &tmp
, 0, 0);
168 free_extent_buffer(tmp
);
171 ret
= __btrfs_cow_block(trans
, info
->dev_root
, info
->dev_root
->node
,
172 NULL
, 0, &tmp
, 0, 0);
174 free_extent_buffer(tmp
);
179 static int create_one_raid_group(struct btrfs_trans_handle
*trans
,
180 struct btrfs_root
*root
, u64 type
)
186 ret
= btrfs_alloc_chunk(trans
, root
->fs_info
->extent_root
,
187 &chunk_start
, &chunk_size
, type
);
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
);
196 static int create_raid_groups(struct btrfs_trans_handle
*trans
,
197 struct btrfs_root
*root
, u64 data_profile
,
198 u64 metadata_profile
)
200 u64 num_devices
= btrfs_super_num_devices(&root
->fs_info
->super_copy
);
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
;
210 allowed
= BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
;
212 if (allowed
& metadata_profile
) {
213 ret
= create_one_raid_group(trans
, root
,
214 BTRFS_BLOCK_GROUP_SYSTEM
|
215 (allowed
& metadata_profile
));
218 ret
= create_one_raid_group(trans
, root
,
219 BTRFS_BLOCK_GROUP_METADATA
|
220 (allowed
& metadata_profile
));
223 ret
= recow_roots(trans
, root
);
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
));
235 static int create_data_reloc_tree(struct btrfs_trans_handle
*trans
,
236 struct btrfs_root
*root
)
238 struct btrfs_key location
;
239 struct btrfs_root_item root_item
;
240 struct extent_buffer
*tmp
;
241 u64 objectid
= BTRFS_DATA_RELOC_TREE_OBJECTID
;
244 ret
= btrfs_copy_root(trans
, root
, root
->node
, &tmp
, objectid
);
247 memcpy(&root_item
, &root
->root_item
, sizeof(root_item
));
248 btrfs_set_root_bytenr(&root_item
, tmp
->start
);
249 btrfs_set_root_level(&root_item
, btrfs_header_level(tmp
));
250 btrfs_set_root_generation(&root_item
, trans
->transid
);
251 free_extent_buffer(tmp
);
253 location
.objectid
= objectid
;
254 location
.type
= BTRFS_ROOT_ITEM_KEY
;
255 location
.offset
= trans
->transid
;
256 ret
= btrfs_insert_root(trans
, root
->fs_info
->tree_root
,
257 &location
, &root_item
);
262 static void print_usage(void)
264 fprintf(stderr
, "usage: mkfs.btrfs [options] dev [ dev ... ]\n");
265 fprintf(stderr
, "options:\n");
266 fprintf(stderr
, "\t -b --byte-count total number of bytes in the FS\n");
267 fprintf(stderr
, "\t -l --leafsize size of btree leaves\n");
268 fprintf(stderr
, "\t -n --nodesize size of btree leaves\n");
269 fprintf(stderr
, "\t -s --sectorsize min block allocation\n");
270 fprintf(stderr
, "%s\n", BTRFS_BUILD_VERSION
);
274 static u64
parse_profile(char *s
)
276 if (strcmp(s
, "raid0") == 0) {
277 return BTRFS_BLOCK_GROUP_RAID0
;
278 } else if (strcmp(s
, "raid1") == 0) {
279 return BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_DUP
;
280 } else if (strcmp(s
, "raid10") == 0) {
281 return BTRFS_BLOCK_GROUP_RAID10
| BTRFS_BLOCK_GROUP_DUP
;
282 } else if (strcmp(s
, "single") == 0) {
285 fprintf(stderr
, "Unknown option %s\n", s
);
291 static char *parse_label(char *input
)
294 int len
= strlen(input
);
296 if (len
> BTRFS_LABEL_SIZE
) {
297 fprintf(stderr
, "Label %s is too long (max %d)\n", input
,
301 for (i
= 0; i
< len
; i
++) {
302 if (input
[i
] == '/' || input
[i
] == '\\') {
303 fprintf(stderr
, "invalid label %s\n", input
);
307 return strdup(input
);
310 static struct option long_options
[] = {
311 { "alloc-start", 1, NULL
, 'A'},
312 { "byte-count", 1, NULL
, 'b' },
313 { "leafsize", 1, NULL
, 'l' },
314 { "label", 1, NULL
, 'L'},
315 { "metadata", 1, NULL
, 'm' },
316 { "nodesize", 1, NULL
, 'n' },
317 { "sectorsize", 1, NULL
, 's' },
318 { "data", 1, NULL
, 'd' },
322 int main(int ac
, char **av
)
325 struct btrfs_root
*root
;
326 struct btrfs_trans_handle
*trans
;
330 u64 dev_block_count
= 0;
333 u64 metadata_profile
= BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_DUP
;
334 u64 data_profile
= BTRFS_BLOCK_GROUP_RAID0
;
335 u32 leafsize
= getpagesize();
336 u32 sectorsize
= 4096;
337 u32 nodesize
= leafsize
;
338 u32 stripesize
= 4096;
340 int option_index
= 0;
348 c
= getopt_long(ac
, av
, "A:b:l:n:s:m:d:L:", long_options
,
354 alloc_start
= parse_size(optarg
);
357 data_profile
= parse_profile(optarg
);
360 leafsize
= parse_size(optarg
);
363 label
= parse_label(optarg
);
366 metadata_profile
= parse_profile(optarg
);
369 nodesize
= parse_size(optarg
);
372 sectorsize
= parse_size(optarg
);
375 block_count
= parse_size(optarg
);
382 sectorsize
= max(sectorsize
, (u32
)getpagesize());
383 if (leafsize
< sectorsize
|| (leafsize
& (sectorsize
- 1))) {
384 fprintf(stderr
, "Illegal leafsize %u\n", leafsize
);
387 if (nodesize
< sectorsize
|| (nodesize
& (sectorsize
- 1))) {
388 fprintf(stderr
, "Illegal nodesize %u\n", nodesize
);
396 ret
= check_mounted(file
);
398 fprintf(stderr
, "error checking %s mount status\n", file
);
402 fprintf(stderr
, "%s is mounted\n", file
);
406 fd
= open(file
, O_RDWR
);
408 fprintf(stderr
, "unable to open %s\n", file
);
413 ret
= btrfs_prepare_device(fd
, file
, zero_end
, &dev_block_count
);
414 if (block_count
== 0)
415 block_count
= dev_block_count
;
417 for (i
= 0; i
< 6; i
++)
418 blocks
[i
] = BTRFS_SUPER_INFO_OFFSET
+ leafsize
* i
;
420 ret
= make_btrfs(fd
, file
, label
, blocks
, block_count
,
422 sectorsize
, stripesize
);
424 fprintf(stderr
, "error during mkfs %d\n", ret
);
427 root
= open_ctree(file
, 0, O_RDWR
);
428 root
->fs_info
->alloc_start
= alloc_start
;
430 ret
= make_root_dir(root
);
432 fprintf(stderr
, "failed to setup the root directory\n");
436 trans
= btrfs_start_transaction(root
, 1);
441 btrfs_register_one_device(file
);
443 fprintf(stderr
, "ctree init failed\n");
450 ret
= check_mounted(file
);
452 fprintf(stderr
, "error checking %s mount status\n",
457 fprintf(stderr
, "%s is mounted\n", file
);
460 fd
= open(file
, O_RDWR
);
462 fprintf(stderr
, "unable to open %s\n", file
);
465 ret
= btrfs_device_already_in_root(root
, fd
,
466 BTRFS_SUPER_INFO_OFFSET
);
468 fprintf(stderr
, "skipping duplicate device %s in FS\n",
473 ret
= btrfs_prepare_device(fd
, file
, zero_end
,
478 ret
= btrfs_add_to_fsid(trans
, root
, fd
, file
, dev_block_count
,
479 sectorsize
, sectorsize
, sectorsize
);
481 btrfs_register_one_device(file
);
485 ret
= create_raid_groups(trans
, root
, data_profile
,
489 ret
= create_data_reloc_tree(trans
, root
);
492 printf("fs created label %s on %s\n\tnodesize %u leafsize %u "
493 "sectorsize %u size %s\n",
494 label
, first_file
, nodesize
, leafsize
, sectorsize
,
495 pretty_sizes(btrfs_super_total_bytes(&root
->fs_info
->super_copy
)));
497 printf("%s\n", BTRFS_BUILD_VERSION
);
498 btrfs_commit_transaction(trans
, root
);
499 ret
= close_ctree(root
);