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
21 #include <sys/ioctl.h>
22 #include <sys/mount.h>
26 #include <sys/types.h>
30 #include <uuid/uuid.h>
32 #include "kerncompat.h"
35 #include "transaction.h"
38 #define BLKGETSIZE64 0
39 static inline int ioctl(int fd, int define, u64 *size) { return 0; }
42 static int __make_root_dir(struct btrfs_trans_handle *trans,
43 struct btrfs_root *root, u64 objectid)
47 struct btrfs_key inode_map;
48 struct btrfs_inode_item inode_item;
53 inode_map.objectid = objectid;
54 btrfs_set_key_type(&inode_map, BTRFS_INODE_ITEM_KEY);
57 memset(&inode_item, 0, sizeof(inode_item));
58 btrfs_set_inode_generation(&inode_item, root->fs_info->generation);
59 btrfs_set_inode_size(&inode_item, 6);
60 btrfs_set_inode_nlink(&inode_item, 1);
61 btrfs_set_inode_nblocks(&inode_item, 1);
62 btrfs_set_inode_mode(&inode_item, S_IFDIR | 0555);
64 if (root->fs_info->tree_root == root)
65 btrfs_set_super_root_dir(root->fs_info->disk_super, objectid);
67 ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
70 ret = btrfs_insert_dir_item(trans, root, buf, 1, objectid,
71 &inode_map, BTRFS_FT_DIR);
74 ret = btrfs_insert_dir_item(trans, root, buf, 2, objectid,
75 &inode_map, BTRFS_FT_DIR);
78 btrfs_set_root_dirid(&root->root_item, objectid);
84 static int make_block_groups(struct btrfs_trans_handle *trans,
85 struct btrfs_root *root)
92 struct btrfs_block_group_cache *cache;
93 struct cache_tree *bg_cache = &root->fs_info->block_group_cache;
95 root = root->fs_info->extent_root;
97 /* first we bootstrap the things into cache */
98 group_size = BTRFS_BLOCK_GROUP_SIZE;
99 cache = malloc(sizeof(*cache));
100 cache->key.objectid = 0;
101 cache->key.offset = group_size;
102 cache->cache.start = 0;
103 cache->cache.size = group_size;
104 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
106 memset(&cache->item, 0, sizeof(cache->item));
107 btrfs_set_block_group_used(&cache->item,
108 btrfs_super_bytes_used(root->fs_info->disk_super));
109 ret = insert_existing_cache_extent(bg_cache, &cache->cache);
112 total_bytes = btrfs_super_total_bytes(root->fs_info->disk_super);
113 cur_start = group_size;
114 while(cur_start < total_bytes) {
115 cache = malloc(sizeof(*cache));
116 cache->key.objectid = cur_start;
117 cache->key.offset = group_size;
118 cache->cache.start = cur_start;
119 cache->cache.size = group_size;
120 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
121 memset(&cache->item, 0, sizeof(cache->item));
123 cache->item.flags |= BTRFS_BLOCK_GROUP_DATA;
125 ret = insert_existing_cache_extent(bg_cache, &cache->cache);
127 cur_start += group_size;
130 /* then insert all the items */
132 while(cur_start < total_bytes) {
133 struct cache_extent *ce;
134 ce = find_first_cache_extent(bg_cache, cur_start);
136 cache = container_of(ce, struct btrfs_block_group_cache,
138 ret = btrfs_insert_block_group(trans, root, &cache->key,
141 cur_start += group_size;
146 static int make_root_dir(int fd) {
147 struct btrfs_root *root;
148 struct btrfs_super_block super;
149 struct btrfs_trans_handle *trans;
151 struct btrfs_key location;
153 root = open_ctree_fd(fd, &super);
156 fprintf(stderr, "ctree init failed\n");
159 trans = btrfs_start_transaction(root, 1);
160 ret = make_block_groups(trans, root);
161 ret = __make_root_dir(trans, root->fs_info->tree_root,
162 BTRFS_ROOT_TREE_DIR_OBJECTID);
165 ret = __make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID);
168 memcpy(&location, &root->fs_info->fs_root->root_key, sizeof(location));
169 location.offset = (u64)-1;
170 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
172 btrfs_super_root_dir(root->fs_info->disk_super),
173 &location, BTRFS_FT_DIR);
176 btrfs_commit_transaction(trans, root, root->fs_info->disk_super);
177 ret = close_ctree(root, &super);
182 int mkfs(int fd, char *pathname, u64 num_bytes, u32 nodesize, u32 leafsize,
183 u32 sectorsize, u32 stripesize)
185 struct btrfs_super_block super;
186 struct btrfs_leaf *empty_leaf;
187 struct btrfs_root_item root_item;
188 struct btrfs_item item;
189 struct btrfs_extent_item extent_item;
190 struct btrfs_inode_item *inode_item;
194 u32 start_block = BTRFS_SUPER_INFO_OFFSET;
195 u32 first_free = BTRFS_SUPER_INFO_OFFSET + sectorsize;
197 btrfs_set_super_generation(&super, 1);
198 btrfs_set_super_bytenr(&super, start_block);
199 btrfs_set_super_root_level(&super, 0);
200 btrfs_set_super_root(&super, first_free);
201 strcpy((char *)(&super.magic), BTRFS_MAGIC);
203 printf("blocksize is %d\n", leafsize);
204 btrfs_set_super_sectorsize(&super, sectorsize);
205 btrfs_set_super_leafsize(&super, leafsize);
206 btrfs_set_super_nodesize(&super, nodesize);
207 btrfs_set_super_stripesize(&super, stripesize);
209 num_bytes = (num_bytes / sectorsize) * sectorsize;
210 btrfs_set_super_total_bytes(&super, num_bytes);
211 btrfs_set_super_bytes_used(&super, start_block + 3 * leafsize +
213 uuid_generate(super.fsid);
215 block = malloc(sectorsize);
216 memset(block, 0, sectorsize);
217 BUG_ON(sizeof(super) > sectorsize);
218 memcpy(block, &super, sizeof(super));
219 ret = pwrite(fd, block, sectorsize, BTRFS_SUPER_INFO_OFFSET);
220 BUG_ON(ret != sectorsize);
222 /* create the tree of root objects */
223 empty_leaf = malloc(leafsize);
224 memset(empty_leaf, 0, leafsize);
225 btrfs_set_header_bytenr(&empty_leaf->header, first_free);
226 btrfs_set_header_nritems(&empty_leaf->header, 2);
227 btrfs_set_header_generation(&empty_leaf->header, 0);
228 btrfs_set_header_owner(&empty_leaf->header, BTRFS_ROOT_TREE_OBJECTID);
229 memcpy(empty_leaf->header.fsid, super.fsid,
230 sizeof(empty_leaf->header.fsid));
232 /* create the items for the root tree */
233 inode_item = &root_item.inode;
234 memset(inode_item, 0, sizeof(*inode_item));
235 btrfs_set_inode_generation(inode_item, 1);
236 btrfs_set_inode_size(inode_item, 3);
237 btrfs_set_inode_nlink(inode_item, 1);
238 btrfs_set_inode_nblocks(inode_item, 1);
239 btrfs_set_inode_mode(inode_item, S_IFDIR | 0755);
241 // memset(&root_item, 0, sizeof(root_item));
242 btrfs_set_root_dirid(&root_item, 0);
243 btrfs_set_root_refs(&root_item, 1);
244 btrfs_set_disk_key_offset(&item.key, 0);
245 btrfs_set_item_size(&item, sizeof(root_item));
246 btrfs_set_disk_key_type(&item.key, BTRFS_ROOT_ITEM_KEY);
248 itemoff = __BTRFS_LEAF_DATA_SIZE(leafsize) - sizeof(root_item);
249 btrfs_set_root_bytenr(&root_item, first_free + leafsize);
251 btrfs_set_item_offset(&item, itemoff);
252 btrfs_set_disk_key_objectid(&item.key, BTRFS_EXTENT_TREE_OBJECTID);
253 memcpy(empty_leaf->items, &item, sizeof(item));
254 memcpy(btrfs_leaf_data(empty_leaf) + itemoff,
255 &root_item, sizeof(root_item));
257 btrfs_set_root_bytenr(&root_item, first_free + leafsize * 2);
258 btrfs_set_root_bytes_used(&root_item, 1);
259 itemoff = itemoff - sizeof(root_item);
260 btrfs_set_item_offset(&item, itemoff);
261 btrfs_set_disk_key_objectid(&item.key, BTRFS_FS_TREE_OBJECTID);
262 memcpy(empty_leaf->items + 1, &item, sizeof(item));
263 memcpy(btrfs_leaf_data(empty_leaf) + itemoff,
264 &root_item, sizeof(root_item));
265 ret = pwrite(fd, empty_leaf, leafsize, first_free);
267 /* create the items for the extent tree */
268 btrfs_set_header_bytenr(&empty_leaf->header, first_free + leafsize);
269 btrfs_set_header_nritems(&empty_leaf->header, 4);
271 /* item1, reserve blocks 0-16 */
272 btrfs_set_disk_key_objectid(&item.key, 0);
273 btrfs_set_disk_key_offset(&item.key, first_free);
274 btrfs_set_disk_key_type(&item.key, 0);
275 btrfs_set_disk_key_type(&item.key, BTRFS_EXTENT_ITEM_KEY);
276 itemoff = __BTRFS_LEAF_DATA_SIZE(leafsize) -
277 sizeof(struct btrfs_extent_item);
278 btrfs_set_item_offset(&item, itemoff);
279 btrfs_set_item_size(&item, sizeof(struct btrfs_extent_item));
280 btrfs_set_extent_refs(&extent_item, 1);
281 btrfs_set_extent_owner(&extent_item, BTRFS_ROOT_TREE_OBJECTID);
282 memcpy(empty_leaf->items, &item, sizeof(item));
283 memcpy(btrfs_leaf_data(empty_leaf) + btrfs_item_offset(&item),
284 &extent_item, btrfs_item_size(&item));
286 /* item2, give block 17 to the root */
287 btrfs_set_disk_key_objectid(&item.key, first_free);
288 btrfs_set_disk_key_offset(&item.key, leafsize);
289 itemoff = itemoff - sizeof(struct btrfs_extent_item);
290 btrfs_set_item_offset(&item, itemoff);
291 memcpy(empty_leaf->items + 1, &item, sizeof(item));
292 memcpy(btrfs_leaf_data(empty_leaf) + btrfs_item_offset(&item),
293 &extent_item, btrfs_item_size(&item));
295 /* item3, give block 18 to the extent root */
296 btrfs_set_disk_key_objectid(&item.key, first_free + leafsize);
297 btrfs_set_disk_key_offset(&item.key, leafsize);
298 itemoff = itemoff - sizeof(struct btrfs_extent_item);
299 btrfs_set_item_offset(&item, itemoff);
300 memcpy(empty_leaf->items + 2, &item, sizeof(item));
301 memcpy(btrfs_leaf_data(empty_leaf) + btrfs_item_offset(&item),
302 &extent_item, btrfs_item_size(&item));
304 /* item4, give block 19 to the FS root */
305 btrfs_set_disk_key_objectid(&item.key, first_free + leafsize * 2);
306 btrfs_set_disk_key_offset(&item.key, leafsize);
307 itemoff = itemoff - sizeof(struct btrfs_extent_item);
308 btrfs_set_item_offset(&item, itemoff);
309 memcpy(empty_leaf->items + 3, &item, sizeof(item));
310 memcpy(btrfs_leaf_data(empty_leaf) + btrfs_item_offset(&item),
311 &extent_item, btrfs_item_size(&item));
312 ret = pwrite(fd, empty_leaf, leafsize, first_free + leafsize);
316 /* finally create the FS root */
317 btrfs_set_header_bytenr(&empty_leaf->header, first_free + leafsize * 2);
318 btrfs_set_header_nritems(&empty_leaf->header, 0);
319 ret = pwrite(fd, empty_leaf, leafsize, first_free + leafsize * 2);
325 u64 device_size(int fd, struct stat *st)
328 if (S_ISREG(st->st_mode)) {
331 if (!S_ISBLK(st->st_mode)) {
334 if (ioctl(fd, BLKGETSIZE64, &size) >= 0) {
340 static void print_usage(void)
342 fprintf(stderr, "usage: mkfs.btrfs [ -l leafsize ] [ -n nodesize] dev [ blocks ]\n");
346 int main(int ac, char **av)
354 u32 leafsize = 8 * 1024;
355 u32 sectorsize = 4096;
356 u32 nodesize = 8 * 1024;
357 u32 stripesize = 4096;
358 char *buf = malloc(sectorsize);
363 c = getopt(ac, av, "l:n:s:");
368 leafsize = atol(optarg);
371 nodesize = atol(optarg);
374 stripesize = atol(optarg);
380 if (leafsize < sectorsize || (leafsize & (sectorsize - 1))) {
381 fprintf(stderr, "Illegal leafsize %u\n", leafsize);
384 if (nodesize < sectorsize || (nodesize & (sectorsize - 1))) {
385 fprintf(stderr, "Illegal nodesize %u\n", nodesize);
392 block_count = atol(av[optind + 1]);
394 fprintf(stderr, "error finding block count\n");
401 fd = open(file, O_RDWR);
403 fprintf(stderr, "unable to open %s\n", file);
406 ret = fstat(fd, &st);
408 fprintf(stderr, "unable to stat %s\n", file);
411 if (block_count == 0) {
412 block_count = device_size(fd, &st);
413 if (block_count == 0) {
414 fprintf(stderr, "unable to find %s size\n", file);
417 block_count /= sectorsize;
419 if (block_count < 256) {
420 fprintf(stderr, "device %s is too small\n", file);
423 block_count = block_count * sectorsize;
424 memset(buf, 0, sectorsize);
425 for(i = 0; i < 64; i++) {
426 ret = write(fd, buf, sectorsize);
427 if (ret != sectorsize) {
428 fprintf(stderr, "unable to zero fill device\n");
432 realpath_name = realpath(file, NULL);
433 ret = mkfs(fd, realpath_name, block_count, nodesize, leafsize,
434 sectorsize, stripesize);
436 fprintf(stderr, "error during mkfs %d\n", ret);
439 ret = make_root_dir(fd);
441 fprintf(stderr, "failed to setup the root directory\n");
444 printf("fs created on %s nodesize %u leafsize %u sectorsize %u bytes %llu\n",
445 file, nodesize, leafsize, sectorsize,
446 (unsigned long long)block_count);