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/types.h>
28 #include <uuid/uuid.h>
31 #include "kerncompat.h"
35 #include "transaction.h"
38 static u64
parse_size(char *s
)
44 if (!isdigit(s
[len
- 1])) {
45 c
= tolower(s
[len
- 1]);
56 fprintf(stderr
, "Unknown size descriptor %c\n", c
);
61 return atol(s
) * mult
;
64 static int make_root_dir(int fd
, const char *device_name
) {
65 struct btrfs_root
*root
;
66 struct btrfs_trans_handle
*trans
;
67 struct btrfs_key location
;
73 root
= open_ctree_fd(fd
, device_name
, 0);
76 fprintf(stderr
, "ctree init failed\n");
79 trans
= btrfs_start_transaction(root
, 1);
80 bytes_used
= btrfs_super_bytes_used(&root
->fs_info
->super_copy
);
82 root
->fs_info
->system_allocs
= 1;
83 ret
= btrfs_make_block_group(trans
, root
, bytes_used
,
84 BTRFS_BLOCK_GROUP_SYSTEM
,
85 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
86 0, BTRFS_MKFS_SYSTEM_GROUP_SIZE
);
89 ret
= btrfs_alloc_chunk(trans
, root
->fs_info
->extent_root
,
90 &chunk_start
, &chunk_size
,
91 BTRFS_BLOCK_GROUP_METADATA
);
93 ret
= btrfs_make_block_group(trans
, root
, 0,
94 BTRFS_BLOCK_GROUP_METADATA
,
95 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
96 chunk_start
, chunk_size
);
99 root
->fs_info
->system_allocs
= 0;
100 btrfs_commit_transaction(trans
, root
);
101 trans
= btrfs_start_transaction(root
, 1);
104 ret
= btrfs_alloc_chunk(trans
, root
->fs_info
->extent_root
,
105 &chunk_start
, &chunk_size
,
106 BTRFS_BLOCK_GROUP_DATA
);
108 ret
= btrfs_make_block_group(trans
, root
, 0,
109 BTRFS_BLOCK_GROUP_DATA
,
110 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
111 chunk_start
, chunk_size
);
114 // ret = btrfs_make_block_group(trans, root, 0, 1);
115 ret
= btrfs_make_root_dir(trans
, root
->fs_info
->tree_root
,
116 BTRFS_ROOT_TREE_DIR_OBJECTID
);
119 ret
= btrfs_make_root_dir(trans
, root
, BTRFS_FIRST_FREE_OBJECTID
);
122 memcpy(&location
, &root
->fs_info
->fs_root
->root_key
, sizeof(location
));
123 location
.offset
= (u64
)-1;
124 ret
= btrfs_insert_dir_item(trans
, root
->fs_info
->tree_root
,
126 btrfs_super_root_dir(&root
->fs_info
->super_copy
),
127 &location
, BTRFS_FT_DIR
);
131 ret
= btrfs_insert_inode_ref(trans
, root
->fs_info
->tree_root
,
132 "default", 7, location
.objectid
,
133 BTRFS_ROOT_TREE_DIR_OBJECTID
);
137 btrfs_commit_transaction(trans
, root
);
138 ret
= close_ctree(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 void print_usage(void)
237 fprintf(stderr
, "usage: mkfs.btrfs [options] dev [ dev ... ]\n");
238 fprintf(stderr
, "options:\n");
239 fprintf(stderr
, "\t -b --byte-count total number of bytes in the FS\n");
240 fprintf(stderr
, "\t -l --leafsize size of btree leaves\n");
241 fprintf(stderr
, "\t -n --nodesize size of btree leaves\n");
242 fprintf(stderr
, "\t -s --sectorsize min block allocation\n");
246 static u64
parse_profile(char *s
)
248 if (strcmp(s
, "raid0") == 0) {
249 return BTRFS_BLOCK_GROUP_RAID0
;
250 } else if (strcmp(s
, "raid1") == 0) {
251 return BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_DUP
;
252 } else if (strcmp(s
, "raid10") == 0) {
253 return BTRFS_BLOCK_GROUP_RAID10
| BTRFS_BLOCK_GROUP_DUP
;
254 } else if (strcmp(s
, "single") == 0) {
257 fprintf(stderr
, "Unknown option %s\n", s
);
263 static char *parse_label(char *input
)
266 int len
= strlen(input
);
268 if (len
> BTRFS_LABEL_SIZE
) {
269 fprintf(stderr
, "Label %s is too long (max %d)\n", input
,
273 for (i
= 0; i
< len
; i
++) {
274 if (input
[i
] == '/' || input
[i
] == '\\') {
275 fprintf(stderr
, "invalid label %s\n", input
);
279 return strdup(input
);
282 static struct option long_options
[] = {
283 { "byte-count", 1, NULL
, 'b' },
284 { "leafsize", 1, NULL
, 'l' },
285 { "label", 1, NULL
, 'L'},
286 { "metadata", 1, NULL
, 'm' },
287 { "nodesize", 1, NULL
, 'n' },
288 { "sectorsize", 1, NULL
, 's' },
289 { "data", 1, NULL
, 'd' },
293 int main(int ac
, char **av
)
296 struct btrfs_root
*root
;
297 struct btrfs_trans_handle
*trans
;
301 u64 dev_block_count
= 0;
303 u64 metadata_profile
= BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_DUP
;
304 u64 data_profile
= BTRFS_BLOCK_GROUP_RAID0
;
305 u32 leafsize
= getpagesize();
306 u32 sectorsize
= 4096;
307 u32 nodesize
= leafsize
;
308 u32 stripesize
= 4096;
310 int option_index
= 0;
318 c
= getopt_long(ac
, av
, "b:l:n:s:m:d:L:", long_options
,
324 data_profile
= parse_profile(optarg
);
327 leafsize
= parse_size(optarg
);
330 label
= parse_label(optarg
);
333 metadata_profile
= parse_profile(optarg
);
336 nodesize
= parse_size(optarg
);
339 sectorsize
= parse_size(optarg
);
342 block_count
= parse_size(optarg
);
349 sectorsize
= max(sectorsize
, (u32
)getpagesize());
350 if (leafsize
< sectorsize
|| (leafsize
& (sectorsize
- 1))) {
351 fprintf(stderr
, "Illegal leafsize %u\n", leafsize
);
354 if (nodesize
< sectorsize
|| (nodesize
& (sectorsize
- 1))) {
355 fprintf(stderr
, "Illegal nodesize %u\n", nodesize
);
363 ret
= check_mounted(file
);
365 fprintf(stderr
, "error checking %s mount status\n", file
);
369 fprintf(stderr
, "%s is mounted\n", file
);
373 fd
= open(file
, O_RDWR
);
375 fprintf(stderr
, "unable to open %s\n", file
);
380 ret
= btrfs_prepare_device(fd
, file
, zero_end
, &dev_block_count
);
381 if (block_count
== 0)
382 block_count
= dev_block_count
;
384 for (i
= 0; i
< 6; i
++)
385 blocks
[i
] = BTRFS_SUPER_INFO_OFFSET
+ leafsize
* i
;
387 ret
= make_btrfs(fd
, file
, label
, blocks
, block_count
,
389 sectorsize
, stripesize
);
391 fprintf(stderr
, "error during mkfs %d\n", ret
);
394 ret
= make_root_dir(fd
, file
);
396 fprintf(stderr
, "failed to setup the root directory\n");
399 root
= open_ctree(file
, 0);
400 trans
= btrfs_start_transaction(root
, 1);
405 btrfs_register_one_device(file
);
407 fprintf(stderr
, "ctree init failed\n");
414 ret
= check_mounted(file
);
416 fprintf(stderr
, "error checking %s mount status\n",
421 fprintf(stderr
, "%s is mounted\n", file
);
424 fd
= open(file
, O_RDWR
);
426 fprintf(stderr
, "unable to open %s\n", file
);
429 ret
= btrfs_device_already_in_root(root
, fd
,
430 BTRFS_SUPER_INFO_OFFSET
);
432 fprintf(stderr
, "skipping duplicate device %s in FS\n",
437 ret
= btrfs_prepare_device(fd
, file
, zero_end
,
442 ret
= btrfs_add_to_fsid(trans
, root
, fd
, file
, dev_block_count
,
443 sectorsize
, sectorsize
, sectorsize
);
445 btrfs_register_one_device(file
);
449 ret
= create_raid_groups(trans
, root
, data_profile
,
452 printf("fs created label %s on %s\n\tnodesize %u leafsize %u "
453 "sectorsize %u size %s\n",
454 label
, first_file
, nodesize
, leafsize
, sectorsize
,
455 pretty_sizes(btrfs_super_total_bytes(&root
->fs_info
->super_copy
)));
457 btrfs_commit_transaction(trans
, root
);
458 ret
= close_ctree(root
);