Fixup last found extent caching
[btrfs-progs-unstable.git] / mkfs.c
blobfd4e5dea7e1b052a8c85cf0b70a96ce008ec926f
1 #define _XOPEN_SOURCE 500
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include <unistd.h>
8 #include "kerncompat.h"
9 #include "radix-tree.h"
10 #include "ctree.h"
11 #include "disk-io.h"
13 int mkfs(int fd)
15 struct ctree_root_info info[2];
16 struct leaf empty_leaf;
17 struct item item;
18 struct extent_item extent_item;
19 int ret;
21 /* setup the super block area */
22 memset(info, 0, sizeof(info));
23 info[0].blocknr = 16;
24 info[0].objectid = 1;
25 info[0].tree_root = 17;
27 info[1].blocknr = 16;
28 info[1].objectid = 2;
29 info[1].tree_root = 18;
30 ret = pwrite(fd, info, sizeof(info),
31 CTREE_SUPER_INFO_OFFSET(CTREE_BLOCKSIZE));
32 if (ret != sizeof(info))
33 return -1;
35 /* create leaves for the tree root and extent root */
36 memset(&empty_leaf, 0, sizeof(empty_leaf));
37 empty_leaf.header.parentid = 1;
38 empty_leaf.header.blocknr = 17;
39 ret = pwrite(fd, &empty_leaf, sizeof(empty_leaf), 17 * CTREE_BLOCKSIZE);
40 if (ret != sizeof(empty_leaf))
41 return -1;
43 empty_leaf.header.parentid = 2;
44 empty_leaf.header.blocknr = 18;
45 empty_leaf.header.nritems = 3;
47 /* item1, reserve blocks 0-16 */
48 item.key.objectid = 0;
49 item.key.offset = 17;
50 item.key.flags = 0;
51 item.offset = LEAF_DATA_SIZE - sizeof(struct extent_item);
52 item.size = sizeof(struct extent_item);
53 extent_item.refs = 1;
54 extent_item.owner = 0;
55 memcpy(empty_leaf.items, &item, sizeof(item));
56 memcpy(empty_leaf.data + item.offset, &extent_item, item.size);
58 /* item2, give block 17 to the root */
59 item.key.objectid = 17;
60 item.key.offset = 1;
61 item.offset = LEAF_DATA_SIZE - sizeof(struct extent_item) * 2;
62 extent_item.owner = 1;
63 memcpy(empty_leaf.items + 1, &item, sizeof(item));
64 memcpy(empty_leaf.data + item.offset, &extent_item, item.size);
66 /* item3, give block 18 for the extent root */
67 item.key.objectid = 18;
68 item.key.offset = 1;
69 item.offset = LEAF_DATA_SIZE - sizeof(struct extent_item) * 3;
70 extent_item.owner = 2;
71 memcpy(empty_leaf.items + 2, &item, sizeof(item));
72 memcpy(empty_leaf.data + item.offset, &extent_item, item.size);
73 ret = pwrite(fd, &empty_leaf, sizeof(empty_leaf), 18 * CTREE_BLOCKSIZE);
74 if (ret != sizeof(empty_leaf))
75 return -1;
76 return 0;