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 create_one_raid_group(struct btrfs_trans_handle *trans,
143 struct btrfs_root *root, u64 type)
149 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
150 &chunk_start, &chunk_size, type);
152 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
153 type, BTRFS_CHUNK_TREE_OBJECTID,
154 chunk_start, chunk_size);
159 static int create_raid_groups(struct btrfs_trans_handle *trans,
160 struct btrfs_root *root, u64 data_profile,
161 u64 metadata_profile)
163 u64 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy);
167 if (num_devices == 1)
168 allowed = BTRFS_BLOCK_GROUP_DUP;
170 allowed = BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1;
172 if (allowed & metadata_profile) {
173 ret = create_one_raid_group(trans, root,
174 BTRFS_BLOCK_GROUP_METADATA |
175 (allowed & metadata_profile));
178 if (num_devices > 1 && (allowed & data_profile)) {
179 ret = create_one_raid_group(trans, root,
180 BTRFS_BLOCK_GROUP_DATA |
181 (allowed & data_profile));
187 static void print_usage(void)
189 fprintf(stderr, "usage: mkfs.btrfs [options] dev [ dev ... ]\n");
190 fprintf(stderr, "options:\n");
191 fprintf(stderr, "\t -b --byte-count total number of bytes in the FS\n");
192 fprintf(stderr, "\t -l --leafsize size of btree leaves\n");
193 fprintf(stderr, "\t -n --nodesize size of btree leaves\n");
194 fprintf(stderr, "\t -s --sectorsize min block allocation\n");
198 static u64 parse_profile(char *s)
200 if (strcmp(s, "raid0") == 0) {
201 return BTRFS_BLOCK_GROUP_RAID0;
202 } else if (strcmp(s, "raid1") == 0) {
203 return BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP;
204 } else if (strcmp(s, "single") == 0) {
207 fprintf(stderr, "Unknown option %s\n", s);
213 static struct option long_options[] = {
214 { "byte-count", 1, NULL, 'b' },
215 { "leafsize", 1, NULL, 'l' },
216 { "nodesize", 1, NULL, 'n' },
217 { "sectorsize", 1, NULL, 's' },
218 { "metadata", 1, NULL, 'm' },
219 { "data", 1, NULL, 'd' },
223 int main(int ac, char **av)
227 u64 dev_block_count = 0;
232 u32 leafsize = 16 * 1024;
233 u32 sectorsize = 4096;
234 u32 nodesize = 16 * 1024;
235 u32 stripesize = 4096;
238 int option_index = 0;
239 struct btrfs_root *root;
240 struct btrfs_trans_handle *trans;
241 u64 metadata_profile = BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP;
242 u64 data_profile = BTRFS_BLOCK_GROUP_RAID0;
246 c = getopt_long(ac, av, "b:l:n:s:m:d:", long_options,
252 data_profile = parse_profile(optarg);
255 metadata_profile = parse_profile(optarg);
258 leafsize = parse_size(optarg);
261 nodesize = parse_size(optarg);
264 sectorsize = parse_size(optarg);
267 block_count = parse_size(optarg);
274 sectorsize = max(sectorsize, (u32)getpagesize());
275 if (leafsize < sectorsize || (leafsize & (sectorsize - 1))) {
276 fprintf(stderr, "Illegal leafsize %u\n", leafsize);
279 if (nodesize < sectorsize || (nodesize & (sectorsize - 1))) {
280 fprintf(stderr, "Illegal nodesize %u\n", nodesize);
288 ret = check_mounted(file);
290 fprintf(stderr, "error checking %s mount status\n", file);
294 fprintf(stderr, "%s is mounted\n", file);
298 fd = open(file, O_RDWR);
300 fprintf(stderr, "unable to open %s\n", file);
304 ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count);
305 if (block_count == 0)
306 block_count = dev_block_count;
308 for (i = 0; i < 6; i++)
309 blocks[i] = BTRFS_SUPER_INFO_OFFSET + leafsize * i;
311 ret = make_btrfs(fd, file, blocks, block_count, nodesize, leafsize,
312 sectorsize, stripesize);
314 fprintf(stderr, "error during mkfs %d\n", ret);
317 ret = make_root_dir(fd, file);
319 fprintf(stderr, "failed to setup the root directory\n");
322 printf("fs created on %s nodesize %u leafsize %u sectorsize %u bytes %llu\n",
323 file, nodesize, leafsize, sectorsize,
324 (unsigned long long)block_count);
326 root = open_ctree(file, 0);
327 trans = btrfs_start_transaction(root, 1);
332 btrfs_register_one_device(file);
334 fprintf(stderr, "ctree init failed\n");
341 ret = check_mounted(file);
343 fprintf(stderr, "error checking %s mount status\n",
348 fprintf(stderr, "%s is mounted\n", file);
351 fd = open(file, O_RDWR);
353 fprintf(stderr, "unable to open %s\n", file);
356 fprintf(stderr, "adding device %s\n", file);
357 ret = btrfs_prepare_device(fd, file, zero_end,
362 ret = btrfs_add_to_fsid(trans, root, fd, dev_block_count,
363 sectorsize, sectorsize, sectorsize);
366 btrfs_register_one_device(file);
370 ret = create_raid_groups(trans, root, data_profile,
372 btrfs_commit_transaction(trans, root);
373 ret = close_ctree(root);