1 #define _XOPEN_SOURCE 500
12 #include <uuid/uuid.h>
13 #include "kerncompat.h"
14 #include "radix-tree.h"
17 #include "transaction.h"
20 #define BLKGETSIZE64 0
21 static inline int ioctl(int fd, int define, u64 *size) { return 0; }
24 static int __make_root_dir(struct btrfs_trans_handle *trans,
25 struct btrfs_root *root)
30 struct btrfs_key inode_map;
31 struct btrfs_inode_item inode_item;
36 ret = btrfs_find_free_objectid(trans, root, 1, &objectid);
40 inode_map.objectid = objectid;
42 btrfs_set_key_type(&inode_map, BTRFS_INODE_ITEM_KEY);
45 memset(&inode_item, 0, sizeof(inode_item));
46 btrfs_set_inode_generation(&inode_item, root->fs_info->generation);
47 btrfs_set_inode_size(&inode_item, 3);
48 btrfs_set_inode_nlink(&inode_item, 1);
49 btrfs_set_inode_nblocks(&inode_item, 1);
50 btrfs_set_inode_mode(&inode_item, S_IFDIR | 0555);
52 if (root->fs_info->tree_root == root)
53 btrfs_set_super_root_dir(root->fs_info->disk_super, objectid);
55 ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
58 ret = btrfs_insert_dir_item(trans, root, buf, 1, objectid,
62 ret = btrfs_insert_dir_item(trans, root, buf, 2, objectid,
66 btrfs_set_root_dirid(&root->root_item, objectid);
72 static int make_root_dir(int fd) {
73 struct btrfs_root *root;
74 struct btrfs_super_block super;
75 struct btrfs_trans_handle *trans;
77 struct btrfs_key location;
79 root = open_ctree_fd(fd, &super);
82 fprintf(stderr, "ctree init failed\n");
85 trans = btrfs_start_transaction(root, 1);
86 ret = __make_root_dir(trans, root->fs_info->tree_root);
89 ret = __make_root_dir(trans, root);
92 memcpy(&location, &root->fs_info->fs_root->root_key, sizeof(location));
93 location.offset = (u64)-1;
94 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
95 "default", strlen("default"),
96 btrfs_super_root_dir(root->fs_info->disk_super),
100 btrfs_commit_transaction(trans, root, root->fs_info->disk_super);
101 ret = close_ctree(root, &super);
106 int mkfs(int fd, u64 num_blocks, u32 blocksize)
108 struct btrfs_super_block super;
109 struct btrfs_leaf *empty_leaf;
110 struct btrfs_root_item root_item;
111 struct btrfs_item item;
112 struct btrfs_extent_item extent_item;
113 struct btrfs_inode_item *inode_item;
117 u32 start_block = BTRFS_SUPER_INFO_OFFSET / blocksize;
118 btrfs_set_super_generation(&super, 1);
119 btrfs_set_super_blocknr(&super, start_block);
120 btrfs_set_super_root(&super, start_block + 1);
121 strcpy((char *)(&super.magic), BTRFS_MAGIC);
122 btrfs_set_super_blocksize(&super, blocksize);
123 btrfs_set_super_total_blocks(&super, num_blocks);
124 btrfs_set_super_blocks_used(&super, start_block + 4);
125 uuid_generate(super.fsid);
127 block = malloc(blocksize);
128 memset(block, 0, blocksize);
129 BUG_ON(sizeof(super) > blocksize);
130 memcpy(block, &super, sizeof(super));
131 ret = pwrite(fd, block, blocksize, BTRFS_SUPER_INFO_OFFSET);
132 BUG_ON(ret != blocksize);
134 /* create the tree of root objects */
135 empty_leaf = malloc(blocksize);
136 memset(empty_leaf, 0, blocksize);
137 btrfs_set_header_parentid(&empty_leaf->header,
138 BTRFS_ROOT_TREE_OBJECTID);
139 btrfs_set_header_blocknr(&empty_leaf->header, start_block + 1);
140 btrfs_set_header_nritems(&empty_leaf->header, 2);
141 btrfs_set_header_generation(&empty_leaf->header, 0);
142 memcpy(empty_leaf->header.fsid, super.fsid,
143 sizeof(empty_leaf->header.fsid));
145 /* create the items for the root tree */
146 inode_item = &root_item.inode;
147 memset(inode_item, 0, sizeof(*inode_item));
148 btrfs_set_inode_generation(inode_item, 1);
149 btrfs_set_inode_size(inode_item, 3);
150 btrfs_set_inode_nlink(inode_item, 1);
151 btrfs_set_inode_nblocks(inode_item, 1);
152 btrfs_set_inode_mode(inode_item, S_IFDIR | 0755);
154 btrfs_set_root_dirid(&root_item, 0);
155 btrfs_set_root_blocknr(&root_item, start_block + 2);
156 btrfs_set_root_refs(&root_item, 1);
157 itemoff = __BTRFS_LEAF_DATA_SIZE(blocksize) - sizeof(root_item);
158 btrfs_set_item_offset(&item, itemoff);
159 btrfs_set_item_size(&item, sizeof(root_item));
160 btrfs_set_disk_key_objectid(&item.key, BTRFS_EXTENT_TREE_OBJECTID);
161 btrfs_set_disk_key_offset(&item.key, 0);
162 btrfs_set_disk_key_flags(&item.key, 0);
163 btrfs_set_disk_key_type(&item.key, BTRFS_ROOT_ITEM_KEY);
164 memcpy(empty_leaf->items, &item, sizeof(item));
165 memcpy(btrfs_leaf_data(empty_leaf) + itemoff,
166 &root_item, sizeof(root_item));
168 btrfs_set_root_blocknr(&root_item, start_block + 3);
169 itemoff = itemoff - sizeof(root_item);
170 btrfs_set_item_offset(&item, itemoff);
171 btrfs_set_disk_key_objectid(&item.key, BTRFS_FS_TREE_OBJECTID);
172 memcpy(empty_leaf->items + 1, &item, sizeof(item));
173 memcpy(btrfs_leaf_data(empty_leaf) + itemoff,
174 &root_item, sizeof(root_item));
175 ret = pwrite(fd, empty_leaf, blocksize, (start_block + 1) * blocksize);
177 /* create the items for the extent tree */
178 btrfs_set_header_parentid(&empty_leaf->header,
179 BTRFS_EXTENT_TREE_OBJECTID);
180 btrfs_set_header_blocknr(&empty_leaf->header, start_block + 2);
181 btrfs_set_header_nritems(&empty_leaf->header, 4);
183 /* item1, reserve blocks 0-16 */
184 btrfs_set_disk_key_objectid(&item.key, 0);
185 btrfs_set_disk_key_offset(&item.key, start_block + 1);
186 btrfs_set_disk_key_flags(&item.key, 0);
187 btrfs_set_disk_key_type(&item.key, BTRFS_EXTENT_ITEM_KEY);
188 itemoff = __BTRFS_LEAF_DATA_SIZE(blocksize) -
189 sizeof(struct btrfs_extent_item);
190 btrfs_set_item_offset(&item, itemoff);
191 btrfs_set_item_size(&item, sizeof(struct btrfs_extent_item));
192 btrfs_set_extent_refs(&extent_item, 1);
193 btrfs_set_extent_owner(&extent_item, 0);
194 memcpy(empty_leaf->items, &item, sizeof(item));
195 memcpy(btrfs_leaf_data(empty_leaf) + btrfs_item_offset(&item),
196 &extent_item, btrfs_item_size(&item));
198 /* item2, give block 17 to the root */
199 btrfs_set_disk_key_objectid(&item.key, start_block + 1);
200 btrfs_set_disk_key_offset(&item.key, 1);
201 itemoff = itemoff - sizeof(struct btrfs_extent_item);
202 btrfs_set_item_offset(&item, itemoff);
203 btrfs_set_extent_owner(&extent_item, BTRFS_ROOT_TREE_OBJECTID);
204 memcpy(empty_leaf->items + 1, &item, sizeof(item));
205 memcpy(btrfs_leaf_data(empty_leaf) + btrfs_item_offset(&item),
206 &extent_item, btrfs_item_size(&item));
208 /* item3, give block 18 to the extent root */
209 btrfs_set_disk_key_objectid(&item.key, start_block + 2);
210 btrfs_set_disk_key_offset(&item.key, 1);
211 itemoff = itemoff - sizeof(struct btrfs_extent_item);
212 btrfs_set_item_offset(&item, itemoff);
213 btrfs_set_extent_owner(&extent_item, BTRFS_EXTENT_TREE_OBJECTID);
214 memcpy(empty_leaf->items + 2, &item, sizeof(item));
215 memcpy(btrfs_leaf_data(empty_leaf) + btrfs_item_offset(&item),
216 &extent_item, btrfs_item_size(&item));
218 /* item5, give block 19 to the FS root */
219 btrfs_set_disk_key_objectid(&item.key, start_block + 3);
220 btrfs_set_disk_key_offset(&item.key, 1);
221 itemoff = itemoff - sizeof(struct btrfs_extent_item);
222 btrfs_set_item_offset(&item, itemoff);
223 btrfs_set_extent_owner(&extent_item, BTRFS_FS_TREE_OBJECTID);
224 memcpy(empty_leaf->items + 3, &item, sizeof(item));
225 memcpy(btrfs_leaf_data(empty_leaf) + btrfs_item_offset(&item),
226 &extent_item, btrfs_item_size(&item));
227 ret = pwrite(fd, empty_leaf, blocksize, (start_block + 2) * blocksize);
228 if (ret != blocksize)
231 /* finally create the FS root */
232 btrfs_set_header_parentid(&empty_leaf->header, BTRFS_FS_TREE_OBJECTID);
233 btrfs_set_header_blocknr(&empty_leaf->header, start_block + 3);
234 btrfs_set_header_nritems(&empty_leaf->header, 0);
235 ret = pwrite(fd, empty_leaf, blocksize, (start_block + 3) * blocksize);
236 if (ret != blocksize)
241 u64 device_size(int fd, struct stat *st)
244 if (S_ISREG(st->st_mode)) {
247 if (!S_ISBLK(st->st_mode)) {
250 if (ioctl(fd, BLKGETSIZE64, &size) >= 0) {
255 int main(int ac, char **av)
263 char *buf = malloc(4096);
270 block_count = atoi(av[2]);
272 fprintf(stderr, "error finding block count\n");
277 fprintf(stderr, "usage: mkfs.btrfs file [block count]\n");
280 fd = open(file, O_RDWR);
282 fprintf(stderr, "unable to open %s\n", file);
285 ret = fstat(fd, &st);
287 fprintf(stderr, "unable to stat %s\n", file);
290 if (block_count == 0) {
291 block_count = device_size(fd, &st);
292 if (block_count == 0) {
293 fprintf(stderr, "unable to find %s size\n", file);
298 if (block_count < 256) {
299 fprintf(stderr, "device %s is too small\n", file);
302 memset(buf, 0, 4096);
303 for(i = 0; i < 16; i++) {
304 ret = write(fd, buf, 4096);
306 fprintf(stderr, "unable to zero fill device\n");
310 ret = mkfs(fd, block_count, 4096);
312 fprintf(stderr, "error during mkfs %d\n", ret);
315 ret = make_root_dir(fd);
317 fprintf(stderr, "failed to setup the root directory\n");
320 printf("fs created on %s blocksize %d blocks %Lu\n",
321 file, 4096, block_count);