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"
45 static u64
parse_size(char *s
)
51 if (!isdigit(s
[len
- 1])) {
52 c
= tolower(s
[len
- 1]);
63 fprintf(stderr
, "Unknown size descriptor %c\n", c
);
68 return atol(s
) * mult
;
71 static int make_root_dir(int fd
, const char *device_name
) {
72 struct btrfs_root
*root
;
73 struct btrfs_trans_handle
*trans
;
74 struct btrfs_key location
;
80 root
= open_ctree_fd(fd
, device_name
, 0);
83 fprintf(stderr
, "ctree init failed\n");
86 trans
= btrfs_start_transaction(root
, 1);
87 bytes_used
= btrfs_super_bytes_used(&root
->fs_info
->super_copy
);
89 root
->fs_info
->system_allocs
= 1;
90 ret
= btrfs_make_block_group(trans
, root
, bytes_used
,
91 BTRFS_BLOCK_GROUP_SYSTEM
,
92 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
93 0, BTRFS_MKFS_SYSTEM_GROUP_SIZE
);
96 ret
= btrfs_alloc_chunk(trans
, root
->fs_info
->extent_root
,
97 &chunk_start
, &chunk_size
,
98 BTRFS_BLOCK_GROUP_METADATA
);
100 ret
= btrfs_make_block_group(trans
, root
, 0,
101 BTRFS_BLOCK_GROUP_METADATA
,
102 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
103 chunk_start
, chunk_size
);
106 root
->fs_info
->system_allocs
= 0;
107 btrfs_commit_transaction(trans
, root
);
108 trans
= btrfs_start_transaction(root
, 1);
111 ret
= btrfs_alloc_chunk(trans
, root
->fs_info
->extent_root
,
112 &chunk_start
, &chunk_size
,
113 BTRFS_BLOCK_GROUP_DATA
);
115 ret
= btrfs_make_block_group(trans
, root
, 0,
116 BTRFS_BLOCK_GROUP_DATA
,
117 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
118 chunk_start
, chunk_size
);
121 // ret = btrfs_make_block_group(trans, root, 0, 1);
122 ret
= btrfs_make_root_dir(trans
, root
->fs_info
->tree_root
,
123 BTRFS_ROOT_TREE_DIR_OBJECTID
);
126 ret
= btrfs_make_root_dir(trans
, root
, BTRFS_FIRST_FREE_OBJECTID
);
129 memcpy(&location
, &root
->fs_info
->fs_root
->root_key
, sizeof(location
));
130 location
.offset
= (u64
)-1;
131 ret
= btrfs_insert_dir_item(trans
, root
->fs_info
->tree_root
,
133 btrfs_super_root_dir(&root
->fs_info
->super_copy
),
134 &location
, BTRFS_FT_DIR
);
138 ret
= btrfs_insert_inode_ref(trans
, root
->fs_info
->tree_root
,
139 "default", 7, location
.objectid
,
140 BTRFS_ROOT_TREE_DIR_OBJECTID
);
144 btrfs_commit_transaction(trans
, root
);
145 ret
= close_ctree(root
);
150 static int recow_roots(struct btrfs_trans_handle
*trans
,
151 struct btrfs_root
*root
)
154 struct extent_buffer
*tmp
;
155 struct btrfs_fs_info
*info
= root
->fs_info
;
157 ret
= __btrfs_cow_block(trans
, info
->fs_root
, info
->fs_root
->node
,
158 NULL
, 0, &tmp
, 0, 0);
160 free_extent_buffer(tmp
);
162 ret
= __btrfs_cow_block(trans
, info
->tree_root
, info
->tree_root
->node
,
163 NULL
, 0, &tmp
, 0, 0);
165 free_extent_buffer(tmp
);
167 ret
= __btrfs_cow_block(trans
, info
->extent_root
,
168 info
->extent_root
->node
, NULL
, 0, &tmp
, 0, 0);
170 free_extent_buffer(tmp
);
172 ret
= __btrfs_cow_block(trans
, info
->chunk_root
, info
->chunk_root
->node
,
173 NULL
, 0, &tmp
, 0, 0);
175 free_extent_buffer(tmp
);
178 ret
= __btrfs_cow_block(trans
, info
->dev_root
, info
->dev_root
->node
,
179 NULL
, 0, &tmp
, 0, 0);
181 free_extent_buffer(tmp
);
186 static int create_one_raid_group(struct btrfs_trans_handle
*trans
,
187 struct btrfs_root
*root
, u64 type
)
193 ret
= btrfs_alloc_chunk(trans
, root
->fs_info
->extent_root
,
194 &chunk_start
, &chunk_size
, type
);
196 ret
= btrfs_make_block_group(trans
, root
->fs_info
->extent_root
, 0,
197 type
, BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
198 chunk_start
, chunk_size
);
203 static int create_raid_groups(struct btrfs_trans_handle
*trans
,
204 struct btrfs_root
*root
, u64 data_profile
,
205 u64 metadata_profile
)
207 u64 num_devices
= btrfs_super_num_devices(&root
->fs_info
->super_copy
);
211 if (num_devices
== 1)
212 allowed
= BTRFS_BLOCK_GROUP_DUP
;
213 else if (num_devices
>= 4) {
214 allowed
= BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
|
215 BTRFS_BLOCK_GROUP_RAID10
;
217 allowed
= BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
;
219 if (allowed
& metadata_profile
) {
220 ret
= create_one_raid_group(trans
, root
,
221 BTRFS_BLOCK_GROUP_SYSTEM
|
222 (allowed
& metadata_profile
));
225 ret
= create_one_raid_group(trans
, root
,
226 BTRFS_BLOCK_GROUP_METADATA
|
227 (allowed
& metadata_profile
));
230 ret
= recow_roots(trans
, root
);
233 if (num_devices
> 1 && (allowed
& data_profile
)) {
234 ret
= create_one_raid_group(trans
, root
,
235 BTRFS_BLOCK_GROUP_DATA
|
236 (allowed
& data_profile
));
242 static void print_usage(void)
244 fprintf(stderr
, "usage: mkfs.btrfs [options] dev [ dev ... ]\n");
245 fprintf(stderr
, "options:\n");
246 fprintf(stderr
, "\t -b --byte-count total number of bytes in the FS\n");
247 fprintf(stderr
, "\t -l --leafsize size of btree leaves\n");
248 fprintf(stderr
, "\t -n --nodesize size of btree leaves\n");
249 fprintf(stderr
, "\t -s --sectorsize min block allocation\n");
253 static u64
parse_profile(char *s
)
255 if (strcmp(s
, "raid0") == 0) {
256 return BTRFS_BLOCK_GROUP_RAID0
;
257 } else if (strcmp(s
, "raid1") == 0) {
258 return BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_DUP
;
259 } else if (strcmp(s
, "raid10") == 0) {
260 return BTRFS_BLOCK_GROUP_RAID10
| BTRFS_BLOCK_GROUP_DUP
;
261 } else if (strcmp(s
, "single") == 0) {
264 fprintf(stderr
, "Unknown option %s\n", s
);
270 static char *parse_label(char *input
)
273 int len
= strlen(input
);
275 if (len
> BTRFS_LABEL_SIZE
) {
276 fprintf(stderr
, "Label %s is too long (max %d)\n", input
,
280 for (i
= 0; i
< len
; i
++) {
281 if (input
[i
] == '/' || input
[i
] == '\\') {
282 fprintf(stderr
, "invalid label %s\n", input
);
286 return strdup(input
);
289 static struct option long_options
[] = {
290 { "alloc-start", 1, NULL
, 'A'},
291 { "byte-count", 1, NULL
, 'b' },
292 { "leafsize", 1, NULL
, 'l' },
293 { "label", 1, NULL
, 'L'},
294 { "metadata", 1, NULL
, 'm' },
295 { "nodesize", 1, NULL
, 'n' },
296 { "sectorsize", 1, NULL
, 's' },
297 { "data", 1, NULL
, 'd' },
301 int main(int ac
, char **av
)
304 struct btrfs_root
*root
;
305 struct btrfs_trans_handle
*trans
;
309 u64 dev_block_count
= 0;
312 u64 metadata_profile
= BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_DUP
;
313 u64 data_profile
= BTRFS_BLOCK_GROUP_RAID0
;
314 u32 leafsize
= getpagesize();
315 u32 sectorsize
= 4096;
316 u32 nodesize
= leafsize
;
317 u32 stripesize
= 4096;
319 int option_index
= 0;
327 c
= getopt_long(ac
, av
, "A:b:l:n:s:m:d:L:", long_options
,
333 alloc_start
= parse_size(optarg
);
336 data_profile
= parse_profile(optarg
);
339 leafsize
= parse_size(optarg
);
342 label
= parse_label(optarg
);
345 metadata_profile
= parse_profile(optarg
);
348 nodesize
= parse_size(optarg
);
351 sectorsize
= parse_size(optarg
);
354 block_count
= parse_size(optarg
);
361 sectorsize
= max(sectorsize
, (u32
)getpagesize());
362 if (leafsize
< sectorsize
|| (leafsize
& (sectorsize
- 1))) {
363 fprintf(stderr
, "Illegal leafsize %u\n", leafsize
);
366 if (nodesize
< sectorsize
|| (nodesize
& (sectorsize
- 1))) {
367 fprintf(stderr
, "Illegal nodesize %u\n", nodesize
);
375 ret
= check_mounted(file
);
377 fprintf(stderr
, "error checking %s mount status\n", file
);
381 fprintf(stderr
, "%s is mounted\n", file
);
385 fd
= open(file
, O_RDWR
);
387 fprintf(stderr
, "unable to open %s\n", file
);
392 ret
= btrfs_prepare_device(fd
, file
, zero_end
, &dev_block_count
);
393 if (block_count
== 0)
394 block_count
= dev_block_count
;
396 for (i
= 0; i
< 6; i
++)
397 blocks
[i
] = BTRFS_SUPER_INFO_OFFSET
+ leafsize
* i
;
399 ret
= make_btrfs(fd
, file
, label
, blocks
, block_count
,
401 sectorsize
, stripesize
);
403 fprintf(stderr
, "error during mkfs %d\n", ret
);
406 ret
= make_root_dir(fd
, file
);
408 fprintf(stderr
, "failed to setup the root directory\n");
411 root
= open_ctree(file
, 0);
412 root
->fs_info
->alloc_start
= alloc_start
;
413 trans
= btrfs_start_transaction(root
, 1);
418 btrfs_register_one_device(file
);
420 fprintf(stderr
, "ctree init failed\n");
427 ret
= check_mounted(file
);
429 fprintf(stderr
, "error checking %s mount status\n",
434 fprintf(stderr
, "%s is mounted\n", file
);
437 fd
= open(file
, O_RDWR
);
439 fprintf(stderr
, "unable to open %s\n", file
);
442 ret
= btrfs_device_already_in_root(root
, fd
,
443 BTRFS_SUPER_INFO_OFFSET
);
445 fprintf(stderr
, "skipping duplicate device %s in FS\n",
450 ret
= btrfs_prepare_device(fd
, file
, zero_end
,
455 ret
= btrfs_add_to_fsid(trans
, root
, fd
, file
, dev_block_count
,
456 sectorsize
, sectorsize
, sectorsize
);
458 btrfs_register_one_device(file
);
462 ret
= create_raid_groups(trans
, root
, data_profile
,
465 printf("fs created label %s on %s\n\tnodesize %u leafsize %u "
466 "sectorsize %u size %s\n",
467 label
, first_file
, nodesize
, leafsize
, sectorsize
,
468 pretty_sizes(btrfs_super_total_bytes(&root
->fs_info
->super_copy
)));
470 btrfs_commit_transaction(trans
, root
);
471 ret
= close_ctree(root
);