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(int fd
, const char *device_name
) {
73 struct btrfs_root
*root
;
74 struct btrfs_trans_handle
*trans
;
75 struct btrfs_key location
;
81 root
= open_ctree_fd(fd
, device_name
, 0, O_RDWR
);
84 fprintf(stderr
, "ctree init failed\n");
87 trans
= btrfs_start_transaction(root
, 1);
88 bytes_used
= btrfs_super_bytes_used(&root
->fs_info
->super_copy
);
90 root
->fs_info
->system_allocs
= 1;
91 ret
= btrfs_make_block_group(trans
, root
, bytes_used
,
92 BTRFS_BLOCK_GROUP_SYSTEM
,
93 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
94 0, BTRFS_MKFS_SYSTEM_GROUP_SIZE
);
97 ret
= btrfs_alloc_chunk(trans
, root
->fs_info
->extent_root
,
98 &chunk_start
, &chunk_size
,
99 BTRFS_BLOCK_GROUP_METADATA
);
101 ret
= btrfs_make_block_group(trans
, root
, 0,
102 BTRFS_BLOCK_GROUP_METADATA
,
103 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
104 chunk_start
, chunk_size
);
107 root
->fs_info
->system_allocs
= 0;
108 btrfs_commit_transaction(trans
, root
);
109 trans
= btrfs_start_transaction(root
, 1);
112 ret
= btrfs_alloc_chunk(trans
, root
->fs_info
->extent_root
,
113 &chunk_start
, &chunk_size
,
114 BTRFS_BLOCK_GROUP_DATA
);
116 ret
= btrfs_make_block_group(trans
, root
, 0,
117 BTRFS_BLOCK_GROUP_DATA
,
118 BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
119 chunk_start
, chunk_size
);
122 // ret = btrfs_make_block_group(trans, root, 0, 1);
123 ret
= btrfs_make_root_dir(trans
, root
->fs_info
->tree_root
,
124 BTRFS_ROOT_TREE_DIR_OBJECTID
);
127 ret
= btrfs_make_root_dir(trans
, root
, BTRFS_FIRST_FREE_OBJECTID
);
130 memcpy(&location
, &root
->fs_info
->fs_root
->root_key
, sizeof(location
));
131 location
.offset
= (u64
)-1;
132 ret
= btrfs_insert_dir_item(trans
, root
->fs_info
->tree_root
,
134 btrfs_super_root_dir(&root
->fs_info
->super_copy
),
135 &location
, BTRFS_FT_DIR
);
139 ret
= btrfs_insert_inode_ref(trans
, root
->fs_info
->tree_root
,
140 "default", 7, location
.objectid
,
141 BTRFS_ROOT_TREE_DIR_OBJECTID
, 0);
145 btrfs_commit_transaction(trans
, root
);
146 ret
= close_ctree(root
);
151 static int recow_roots(struct btrfs_trans_handle
*trans
,
152 struct btrfs_root
*root
)
155 struct extent_buffer
*tmp
;
156 struct btrfs_fs_info
*info
= root
->fs_info
;
158 ret
= __btrfs_cow_block(trans
, info
->fs_root
, info
->fs_root
->node
,
159 NULL
, 0, &tmp
, 0, 0);
161 free_extent_buffer(tmp
);
163 ret
= __btrfs_cow_block(trans
, info
->tree_root
, info
->tree_root
->node
,
164 NULL
, 0, &tmp
, 0, 0);
166 free_extent_buffer(tmp
);
168 ret
= __btrfs_cow_block(trans
, info
->extent_root
,
169 info
->extent_root
->node
, NULL
, 0, &tmp
, 0, 0);
171 free_extent_buffer(tmp
);
173 ret
= __btrfs_cow_block(trans
, info
->chunk_root
, info
->chunk_root
->node
,
174 NULL
, 0, &tmp
, 0, 0);
176 free_extent_buffer(tmp
);
179 ret
= __btrfs_cow_block(trans
, info
->dev_root
, info
->dev_root
->node
,
180 NULL
, 0, &tmp
, 0, 0);
182 free_extent_buffer(tmp
);
187 static int create_one_raid_group(struct btrfs_trans_handle
*trans
,
188 struct btrfs_root
*root
, u64 type
)
194 ret
= btrfs_alloc_chunk(trans
, root
->fs_info
->extent_root
,
195 &chunk_start
, &chunk_size
, type
);
197 ret
= btrfs_make_block_group(trans
, root
->fs_info
->extent_root
, 0,
198 type
, BTRFS_FIRST_CHUNK_TREE_OBJECTID
,
199 chunk_start
, chunk_size
);
204 static int create_raid_groups(struct btrfs_trans_handle
*trans
,
205 struct btrfs_root
*root
, u64 data_profile
,
206 u64 metadata_profile
)
208 u64 num_devices
= btrfs_super_num_devices(&root
->fs_info
->super_copy
);
212 if (num_devices
== 1)
213 allowed
= BTRFS_BLOCK_GROUP_DUP
;
214 else if (num_devices
>= 4) {
215 allowed
= BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
|
216 BTRFS_BLOCK_GROUP_RAID10
;
218 allowed
= BTRFS_BLOCK_GROUP_RAID0
| BTRFS_BLOCK_GROUP_RAID1
;
220 if (allowed
& metadata_profile
) {
221 ret
= create_one_raid_group(trans
, root
,
222 BTRFS_BLOCK_GROUP_SYSTEM
|
223 (allowed
& metadata_profile
));
226 ret
= create_one_raid_group(trans
, root
,
227 BTRFS_BLOCK_GROUP_METADATA
|
228 (allowed
& metadata_profile
));
231 ret
= recow_roots(trans
, root
);
234 if (num_devices
> 1 && (allowed
& data_profile
)) {
235 ret
= create_one_raid_group(trans
, root
,
236 BTRFS_BLOCK_GROUP_DATA
|
237 (allowed
& data_profile
));
243 static void print_usage(void)
245 fprintf(stderr
, "usage: mkfs.btrfs [options] dev [ dev ... ]\n");
246 fprintf(stderr
, "options:\n");
247 fprintf(stderr
, "\t -b --byte-count total number of bytes in the FS\n");
248 fprintf(stderr
, "\t -l --leafsize size of btree leaves\n");
249 fprintf(stderr
, "\t -n --nodesize size of btree leaves\n");
250 fprintf(stderr
, "\t -s --sectorsize min block allocation\n");
251 fprintf(stderr
, "%s\n", BTRFS_BUILD_VERSION
);
255 static u64
parse_profile(char *s
)
257 if (strcmp(s
, "raid0") == 0) {
258 return BTRFS_BLOCK_GROUP_RAID0
;
259 } else if (strcmp(s
, "raid1") == 0) {
260 return BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_DUP
;
261 } else if (strcmp(s
, "raid10") == 0) {
262 return BTRFS_BLOCK_GROUP_RAID10
| BTRFS_BLOCK_GROUP_DUP
;
263 } else if (strcmp(s
, "single") == 0) {
266 fprintf(stderr
, "Unknown option %s\n", s
);
272 static char *parse_label(char *input
)
275 int len
= strlen(input
);
277 if (len
> BTRFS_LABEL_SIZE
) {
278 fprintf(stderr
, "Label %s is too long (max %d)\n", input
,
282 for (i
= 0; i
< len
; i
++) {
283 if (input
[i
] == '/' || input
[i
] == '\\') {
284 fprintf(stderr
, "invalid label %s\n", input
);
288 return strdup(input
);
291 static struct option long_options
[] = {
292 { "alloc-start", 1, NULL
, 'A'},
293 { "byte-count", 1, NULL
, 'b' },
294 { "leafsize", 1, NULL
, 'l' },
295 { "label", 1, NULL
, 'L'},
296 { "metadata", 1, NULL
, 'm' },
297 { "nodesize", 1, NULL
, 'n' },
298 { "sectorsize", 1, NULL
, 's' },
299 { "data", 1, NULL
, 'd' },
303 int main(int ac
, char **av
)
306 struct btrfs_root
*root
;
307 struct btrfs_trans_handle
*trans
;
311 u64 dev_block_count
= 0;
314 u64 metadata_profile
= BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_DUP
;
315 u64 data_profile
= BTRFS_BLOCK_GROUP_RAID0
;
316 u32 leafsize
= getpagesize();
317 u32 sectorsize
= 4096;
318 u32 nodesize
= leafsize
;
319 u32 stripesize
= 4096;
321 int option_index
= 0;
329 c
= getopt_long(ac
, av
, "A:b:l:n:s:m:d:L:", long_options
,
335 alloc_start
= parse_size(optarg
);
338 data_profile
= parse_profile(optarg
);
341 leafsize
= parse_size(optarg
);
344 label
= parse_label(optarg
);
347 metadata_profile
= parse_profile(optarg
);
350 nodesize
= parse_size(optarg
);
353 sectorsize
= parse_size(optarg
);
356 block_count
= parse_size(optarg
);
363 sectorsize
= max(sectorsize
, (u32
)getpagesize());
364 if (leafsize
< sectorsize
|| (leafsize
& (sectorsize
- 1))) {
365 fprintf(stderr
, "Illegal leafsize %u\n", leafsize
);
368 if (nodesize
< sectorsize
|| (nodesize
& (sectorsize
- 1))) {
369 fprintf(stderr
, "Illegal nodesize %u\n", nodesize
);
377 ret
= check_mounted(file
);
379 fprintf(stderr
, "error checking %s mount status\n", file
);
383 fprintf(stderr
, "%s is mounted\n", file
);
387 fd
= open(file
, O_RDWR
);
389 fprintf(stderr
, "unable to open %s\n", file
);
394 ret
= btrfs_prepare_device(fd
, file
, zero_end
, &dev_block_count
);
395 if (block_count
== 0)
396 block_count
= dev_block_count
;
398 for (i
= 0; i
< 6; i
++)
399 blocks
[i
] = BTRFS_SUPER_INFO_OFFSET
+ leafsize
* i
;
401 ret
= make_btrfs(fd
, file
, label
, blocks
, block_count
,
403 sectorsize
, stripesize
);
405 fprintf(stderr
, "error during mkfs %d\n", ret
);
408 ret
= make_root_dir(fd
, file
);
410 fprintf(stderr
, "failed to setup the root directory\n");
413 root
= open_ctree(file
, 0, O_RDWR
);
414 root
->fs_info
->alloc_start
= alloc_start
;
415 trans
= btrfs_start_transaction(root
, 1);
420 btrfs_register_one_device(file
);
422 fprintf(stderr
, "ctree init failed\n");
429 ret
= check_mounted(file
);
431 fprintf(stderr
, "error checking %s mount status\n",
436 fprintf(stderr
, "%s is mounted\n", file
);
439 fd
= open(file
, O_RDWR
);
441 fprintf(stderr
, "unable to open %s\n", file
);
444 ret
= btrfs_device_already_in_root(root
, fd
,
445 BTRFS_SUPER_INFO_OFFSET
);
447 fprintf(stderr
, "skipping duplicate device %s in FS\n",
452 ret
= btrfs_prepare_device(fd
, file
, zero_end
,
457 ret
= btrfs_add_to_fsid(trans
, root
, fd
, file
, dev_block_count
,
458 sectorsize
, sectorsize
, sectorsize
);
460 btrfs_register_one_device(file
);
464 ret
= create_raid_groups(trans
, root
, data_profile
,
467 printf("fs created label %s on %s\n\tnodesize %u leafsize %u "
468 "sectorsize %u size %s\n",
469 label
, first_file
, nodesize
, leafsize
, sectorsize
,
470 pretty_sizes(btrfs_super_total_bytes(&root
->fs_info
->super_copy
)));
472 printf("%s\n", BTRFS_BUILD_VERSION
);
473 btrfs_commit_transaction(trans
, root
);
474 ret
= close_ctree(root
);