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->force_system_allocs = 1;
83 ret = btrfs_make_block_group(trans, root, bytes_used,
84 BTRFS_BLOCK_GROUP_SYSTEM,
85 BTRFS_CHUNK_TREE_OBJECTID,
86 0, BTRFS_MKFS_SYSTEM_GROUP_SIZE);
88 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
89 &chunk_start, &chunk_size,
90 BTRFS_BLOCK_GROUP_METADATA);
92 ret = btrfs_make_block_group(trans, root, 0,
93 BTRFS_BLOCK_GROUP_METADATA,
94 BTRFS_CHUNK_TREE_OBJECTID,
95 chunk_start, chunk_size);
98 root->fs_info->force_system_allocs = 0;
99 btrfs_commit_transaction(trans, root);
100 trans = btrfs_start_transaction(root, 1);
103 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
104 &chunk_start, &chunk_size,
105 BTRFS_BLOCK_GROUP_DATA);
107 ret = btrfs_make_block_group(trans, root, 0,
108 BTRFS_BLOCK_GROUP_DATA,
109 BTRFS_CHUNK_TREE_OBJECTID,
110 chunk_start, chunk_size);
113 // ret = btrfs_make_block_group(trans, root, 0, 1);
114 ret = btrfs_make_root_dir(trans, root->fs_info->tree_root,
115 BTRFS_ROOT_TREE_DIR_OBJECTID);
118 ret = btrfs_make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID);
121 memcpy(&location, &root->fs_info->fs_root->root_key, sizeof(location));
122 location.offset = (u64)-1;
123 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
125 btrfs_super_root_dir(&root->fs_info->super_copy),
126 &location, BTRFS_FT_DIR);
130 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
131 "default", 7, location.objectid,
132 BTRFS_ROOT_TREE_DIR_OBJECTID);
136 btrfs_commit_transaction(trans, root);
137 ret = close_ctree(root);
142 static int recow_roots(struct btrfs_trans_handle *trans,
143 struct btrfs_root *root)
146 struct extent_buffer *tmp;
147 struct btrfs_fs_info *info = root->fs_info;
149 ret = __btrfs_cow_block(trans, info->fs_root, info->fs_root->node,
150 NULL, 0, &tmp, 0, 0);
152 free_extent_buffer(tmp);
154 ret = __btrfs_cow_block(trans, info->tree_root, info->tree_root->node,
155 NULL, 0, &tmp, 0, 0);
157 free_extent_buffer(tmp);
159 ret = __btrfs_cow_block(trans, info->extent_root,
160 info->extent_root->node, NULL, 0, &tmp, 0, 0);
162 free_extent_buffer(tmp);
164 ret = __btrfs_cow_block(trans, info->chunk_root, info->chunk_root->node,
165 NULL, 0, &tmp, 0, 0);
167 free_extent_buffer(tmp);
170 ret = __btrfs_cow_block(trans, info->dev_root, info->dev_root->node,
171 NULL, 0, &tmp, 0, 0);
173 free_extent_buffer(tmp);
178 static int create_one_raid_group(struct btrfs_trans_handle *trans,
179 struct btrfs_root *root, u64 type)
185 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
186 &chunk_start, &chunk_size, type);
188 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
189 type, BTRFS_CHUNK_TREE_OBJECTID,
190 chunk_start, chunk_size);
195 static int create_raid_groups(struct btrfs_trans_handle *trans,
196 struct btrfs_root *root, u64 data_profile,
197 u64 metadata_profile)
199 u64 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy);
203 if (num_devices == 1)
204 allowed = BTRFS_BLOCK_GROUP_DUP;
206 allowed = BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1;
208 if (allowed & metadata_profile) {
209 ret = create_one_raid_group(trans, root,
210 BTRFS_BLOCK_GROUP_SYSTEM |
211 (allowed & metadata_profile));
214 ret = create_one_raid_group(trans, root,
215 BTRFS_BLOCK_GROUP_METADATA |
216 (allowed & metadata_profile));
219 ret = recow_roots(trans, root);
222 if (num_devices > 1 && (allowed & data_profile)) {
223 ret = create_one_raid_group(trans, root,
224 BTRFS_BLOCK_GROUP_DATA |
225 (allowed & data_profile));
231 static void print_usage(void)
233 fprintf(stderr, "usage: mkfs.btrfs [options] dev [ dev ... ]\n");
234 fprintf(stderr, "options:\n");
235 fprintf(stderr, "\t -b --byte-count total number of bytes in the FS\n");
236 fprintf(stderr, "\t -l --leafsize size of btree leaves\n");
237 fprintf(stderr, "\t -n --nodesize size of btree leaves\n");
238 fprintf(stderr, "\t -s --sectorsize min block allocation\n");
242 static u64 parse_profile(char *s)
244 if (strcmp(s, "raid0") == 0) {
245 return BTRFS_BLOCK_GROUP_RAID0;
246 } else if (strcmp(s, "raid1") == 0) {
247 return BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP;
248 } else if (strcmp(s, "single") == 0) {
251 fprintf(stderr, "Unknown option %s\n", s);
257 static struct option long_options[] = {
258 { "byte-count", 1, NULL, 'b' },
259 { "leafsize", 1, NULL, 'l' },
260 { "nodesize", 1, NULL, 'n' },
261 { "sectorsize", 1, NULL, 's' },
262 { "metadata", 1, NULL, 'm' },
263 { "data", 1, NULL, 'd' },
267 int main(int ac, char **av)
271 u64 dev_block_count = 0;
276 u32 leafsize = 16 * 1024;
277 u32 sectorsize = 4096;
278 u32 nodesize = 16 * 1024;
279 u32 stripesize = 4096;
282 int option_index = 0;
283 struct btrfs_root *root;
284 struct btrfs_trans_handle *trans;
285 u64 metadata_profile = BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP;
286 u64 data_profile = BTRFS_BLOCK_GROUP_RAID0;
290 c = getopt_long(ac, av, "b:l:n:s:m:d:", long_options,
296 data_profile = parse_profile(optarg);
299 metadata_profile = parse_profile(optarg);
302 leafsize = parse_size(optarg);
305 nodesize = parse_size(optarg);
308 sectorsize = parse_size(optarg);
311 block_count = parse_size(optarg);
318 sectorsize = max(sectorsize, (u32)getpagesize());
319 if (leafsize < sectorsize || (leafsize & (sectorsize - 1))) {
320 fprintf(stderr, "Illegal leafsize %u\n", leafsize);
323 if (nodesize < sectorsize || (nodesize & (sectorsize - 1))) {
324 fprintf(stderr, "Illegal nodesize %u\n", nodesize);
332 ret = check_mounted(file);
334 fprintf(stderr, "error checking %s mount status\n", file);
338 fprintf(stderr, "%s is mounted\n", file);
342 fd = open(file, O_RDWR);
344 fprintf(stderr, "unable to open %s\n", file);
348 ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count);
349 if (block_count == 0)
350 block_count = dev_block_count;
352 for (i = 0; i < 6; i++)
353 blocks[i] = BTRFS_SUPER_INFO_OFFSET + leafsize * i;
355 ret = make_btrfs(fd, file, blocks, block_count, nodesize, leafsize,
356 sectorsize, stripesize);
358 fprintf(stderr, "error during mkfs %d\n", ret);
361 ret = make_root_dir(fd, file);
363 fprintf(stderr, "failed to setup the root directory\n");
366 printf("fs created on %s nodesize %u leafsize %u sectorsize %u bytes %llu\n",
367 file, nodesize, leafsize, sectorsize,
368 (unsigned long long)block_count);
370 root = open_ctree(file, 0);
371 trans = btrfs_start_transaction(root, 1);
376 btrfs_register_one_device(file);
378 fprintf(stderr, "ctree init failed\n");
385 ret = check_mounted(file);
387 fprintf(stderr, "error checking %s mount status\n",
392 fprintf(stderr, "%s is mounted\n", file);
395 fd = open(file, O_RDWR);
397 fprintf(stderr, "unable to open %s\n", file);
400 fprintf(stderr, "adding device %s\n", file);
401 ret = btrfs_prepare_device(fd, file, zero_end,
406 ret = btrfs_add_to_fsid(trans, root, fd, dev_block_count,
407 sectorsize, sectorsize, sectorsize);
409 btrfs_register_one_device(file);
413 ret = create_raid_groups(trans, root, data_profile,
415 btrfs_commit_transaction(trans, root);
416 ret = close_ctree(root);