Update TODO
[btrfs-progs-unstable.git] / mkfs.c
blob1cac5ab114dde64f7819b13ab990afcd47184e20
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, u64 num_blocks, u32 blocksize)
15 struct btrfs_super_block super;
16 struct btrfs_leaf *empty_leaf;
17 struct btrfs_root_item root_item;
18 struct btrfs_item item;
19 struct btrfs_extent_item extent_item;
20 char *block;
21 int ret;
22 u32 itemoff;
23 u32 start_block = BTRFS_SUPER_INFO_OFFSET / blocksize;
25 btrfs_set_super_blocknr(&super, start_block);
26 btrfs_set_super_root(&super, start_block + 1);
27 strcpy((char *)(&super.magic), BTRFS_MAGIC);
28 btrfs_set_super_blocksize(&super, blocksize);
29 btrfs_set_super_total_blocks(&super, num_blocks);
30 btrfs_set_super_blocks_used(&super, 0);
32 block = malloc(blocksize);
33 memset(block, 0, blocksize);
34 BUG_ON(sizeof(super) > blocksize);
35 memcpy(block, &super, sizeof(super));
36 ret = pwrite(fd, block, blocksize, BTRFS_SUPER_INFO_OFFSET);
37 BUG_ON(ret != blocksize);
39 /* create the tree of root objects */
40 empty_leaf = malloc(blocksize);
41 memset(empty_leaf, 0, blocksize);
42 btrfs_set_header_parentid(&empty_leaf->header,
43 BTRFS_ROOT_TREE_OBJECTID);
44 btrfs_set_header_blocknr(&empty_leaf->header, start_block + 1);
45 btrfs_set_header_nritems(&empty_leaf->header, 3);
47 /* create the items for the root tree */
48 btrfs_set_root_blocknr(&root_item, start_block + 2);
49 btrfs_set_root_refs(&root_item, 1);
50 itemoff = __BTRFS_LEAF_DATA_SIZE(blocksize) - sizeof(root_item);
51 btrfs_set_item_offset(&item, itemoff);
52 btrfs_set_item_size(&item, sizeof(root_item));
53 btrfs_set_disk_key_objectid(&item.key, BTRFS_EXTENT_TREE_OBJECTID);
54 btrfs_set_disk_key_offset(&item.key, 0);
55 btrfs_set_disk_key_flags(&item.key, 0);
56 btrfs_set_disk_key_type(&item.key, BTRFS_ROOT_ITEM_KEY);
57 memcpy(empty_leaf->items, &item, sizeof(item));
58 memcpy(btrfs_leaf_data(empty_leaf) + itemoff,
59 &root_item, sizeof(root_item));
61 btrfs_set_root_blocknr(&root_item, start_block + 3);
62 itemoff = itemoff - sizeof(root_item);
63 btrfs_set_item_offset(&item, itemoff);
64 btrfs_set_disk_key_objectid(&item.key, BTRFS_INODE_MAP_OBJECTID);
65 memcpy(empty_leaf->items + 1, &item, sizeof(item));
66 memcpy(btrfs_leaf_data(empty_leaf) + itemoff,
67 &root_item, sizeof(root_item));
69 btrfs_set_root_blocknr(&root_item, start_block + 4);
70 itemoff = itemoff - sizeof(root_item);
71 btrfs_set_item_offset(&item, itemoff);
72 btrfs_set_disk_key_objectid(&item.key, BTRFS_FS_TREE_OBJECTID);
73 memcpy(empty_leaf->items + 2, &item, sizeof(item));
74 memcpy(btrfs_leaf_data(empty_leaf) + itemoff,
75 &root_item, sizeof(root_item));
76 ret = pwrite(fd, empty_leaf, blocksize, (start_block + 1) * blocksize);
78 /* create the items for the extent tree */
79 btrfs_set_header_parentid(&empty_leaf->header,
80 BTRFS_EXTENT_TREE_OBJECTID);
81 btrfs_set_header_blocknr(&empty_leaf->header, start_block + 2);
82 btrfs_set_header_nritems(&empty_leaf->header, 5);
84 /* item1, reserve blocks 0-16 */
85 btrfs_set_disk_key_objectid(&item.key, 0);
86 btrfs_set_disk_key_offset(&item.key, start_block + 1);
87 btrfs_set_disk_key_flags(&item.key, 0);
88 btrfs_set_disk_key_type(&item.key, BTRFS_EXTENT_ITEM_KEY);
89 itemoff = __BTRFS_LEAF_DATA_SIZE(blocksize) -
90 sizeof(struct btrfs_extent_item);
91 btrfs_set_item_offset(&item, itemoff);
92 btrfs_set_item_size(&item, sizeof(struct btrfs_extent_item));
93 btrfs_set_extent_refs(&extent_item, 1);
94 btrfs_set_extent_owner(&extent_item, 0);
95 memcpy(empty_leaf->items, &item, sizeof(item));
96 memcpy(btrfs_leaf_data(empty_leaf) + btrfs_item_offset(&item),
97 &extent_item, btrfs_item_size(&item));
99 /* item2, give block 17 to the root */
100 btrfs_set_disk_key_objectid(&item.key, start_block + 1);
101 btrfs_set_disk_key_offset(&item.key, 1);
102 itemoff = itemoff - sizeof(struct btrfs_extent_item);
103 btrfs_set_item_offset(&item, itemoff);
104 btrfs_set_extent_owner(&extent_item, BTRFS_ROOT_TREE_OBJECTID);
105 memcpy(empty_leaf->items + 1, &item, sizeof(item));
106 memcpy(btrfs_leaf_data(empty_leaf) + btrfs_item_offset(&item),
107 &extent_item, btrfs_item_size(&item));
109 /* item3, give block 18 to the extent root */
110 btrfs_set_disk_key_objectid(&item.key, start_block + 2);
111 btrfs_set_disk_key_offset(&item.key, 1);
112 itemoff = itemoff - sizeof(struct btrfs_extent_item);
113 btrfs_set_item_offset(&item, itemoff);
114 btrfs_set_extent_owner(&extent_item, BTRFS_EXTENT_TREE_OBJECTID);
115 memcpy(empty_leaf->items + 2, &item, sizeof(item));
116 memcpy(btrfs_leaf_data(empty_leaf) + btrfs_item_offset(&item),
117 &extent_item, btrfs_item_size(&item));
119 /* item4, give block 19 to the inode map */
120 btrfs_set_disk_key_objectid(&item.key, start_block + 3);
121 btrfs_set_disk_key_offset(&item.key, 1);
122 itemoff = itemoff - sizeof(struct btrfs_extent_item);
123 btrfs_set_item_offset(&item, itemoff);
124 btrfs_set_extent_owner(&extent_item, BTRFS_INODE_MAP_OBJECTID);
125 memcpy(empty_leaf->items + 3, &item, sizeof(item));
126 memcpy(btrfs_leaf_data(empty_leaf) + btrfs_item_offset(&item),
127 &extent_item, btrfs_item_size(&item));
128 ret = pwrite(fd, empty_leaf, blocksize, (start_block + 2) * blocksize);
129 if (ret != blocksize)
130 return -1;
132 /* item5, give block 20 to the FS root */
133 btrfs_set_disk_key_objectid(&item.key, start_block + 4);
134 btrfs_set_disk_key_offset(&item.key, 1);
135 itemoff = itemoff - sizeof(struct btrfs_extent_item);
136 btrfs_set_item_offset(&item, itemoff);
137 btrfs_set_extent_owner(&extent_item, BTRFS_FS_TREE_OBJECTID);
138 memcpy(empty_leaf->items + 4, &item, sizeof(item));
139 memcpy(btrfs_leaf_data(empty_leaf) + btrfs_item_offset(&item),
140 &extent_item, btrfs_item_size(&item));
141 ret = pwrite(fd, empty_leaf, blocksize, (start_block + 2) * blocksize);
142 if (ret != blocksize)
143 return -1;
145 /* create the inode map */
146 btrfs_set_header_parentid(&empty_leaf->header,
147 BTRFS_INODE_MAP_OBJECTID);
148 btrfs_set_header_blocknr(&empty_leaf->header, start_block + 3);
149 btrfs_set_header_nritems(&empty_leaf->header, 0);
150 ret = pwrite(fd, empty_leaf, blocksize, (start_block + 3) * blocksize);
151 if (ret != blocksize)
152 return -1;
154 /* finally create the FS root */
155 btrfs_set_header_parentid(&empty_leaf->header, BTRFS_FS_TREE_OBJECTID);
156 btrfs_set_header_blocknr(&empty_leaf->header, start_block + 4);
157 btrfs_set_header_nritems(&empty_leaf->header, 0);
158 ret = pwrite(fd, empty_leaf, blocksize, (start_block + 4) * blocksize);
159 if (ret != blocksize)
160 return -1;
161 return 0;