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, u64 objectid)
29 struct btrfs_key inode_map;
30 struct btrfs_inode_item inode_item;
35 inode_map.objectid = objectid;
37 btrfs_set_key_type(&inode_map, BTRFS_INODE_ITEM_KEY);
40 memset(&inode_item, 0, sizeof(inode_item));
41 btrfs_set_inode_generation(&inode_item, root->fs_info->generation);
42 btrfs_set_inode_size(&inode_item, 6);
43 btrfs_set_inode_nlink(&inode_item, 1);
44 btrfs_set_inode_nblocks(&inode_item, 1);
45 btrfs_set_inode_mode(&inode_item, S_IFDIR | 0555);
47 if (root->fs_info->tree_root == root)
48 btrfs_set_super_root_dir(root->fs_info->disk_super, objectid);
50 ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
53 ret = btrfs_insert_dir_item(trans, root, buf, 1, objectid,
57 ret = btrfs_insert_dir_item(trans, root, buf, 2, objectid,
61 btrfs_set_root_dirid(&root->root_item, objectid);
67 static int make_root_dir(int fd) {
68 struct btrfs_root *root;
69 struct btrfs_super_block super;
70 struct btrfs_trans_handle *trans;
72 struct btrfs_key location;
74 root = open_ctree_fd(fd, &super);
77 fprintf(stderr, "ctree init failed\n");
80 trans = btrfs_start_transaction(root, 1);
81 ret = __make_root_dir(trans, root->fs_info->tree_root,
82 BTRFS_ROOT_TREE_DIR_OBJECTID);
85 ret = __make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID);
88 memcpy(&location, &root->fs_info->fs_root->root_key, sizeof(location));
89 location.offset = (u64)-1;
90 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
91 "default", strlen("default"),
92 btrfs_super_root_dir(root->fs_info->disk_super),
96 btrfs_commit_transaction(trans, root, root->fs_info->disk_super);
97 ret = close_ctree(root, &super);
102 int mkfs(int fd, char *pathname, u64 num_blocks, u32 blocksize)
104 struct btrfs_super_block super;
105 struct btrfs_leaf *empty_leaf;
106 struct btrfs_root_item root_item;
107 struct btrfs_item item;
108 struct btrfs_extent_item extent_item;
109 struct btrfs_inode_item *inode_item;
110 struct btrfs_device_item dev_item;
114 u32 start_block = BTRFS_SUPER_INFO_OFFSET / blocksize;
117 btrfs_set_super_generation(&super, 1);
118 btrfs_set_super_blocknr(&super, start_block);
119 btrfs_set_super_root(&super, start_block + 1);
120 strcpy((char *)(&super.magic), BTRFS_MAGIC);
121 btrfs_set_super_blocksize(&super, blocksize);
122 btrfs_set_super_total_blocks(&super, num_blocks);
123 btrfs_set_super_blocks_used(&super, start_block + 5);
124 btrfs_set_super_device_block_start(&super, 0);
125 btrfs_set_super_device_num_blocks(&super, num_blocks);
126 btrfs_set_super_device_root(&super, start_block + 2);
127 btrfs_set_super_device_id(&super, 1);
128 btrfs_set_super_last_device_id(&super, 1);
129 uuid_generate(super.fsid);
131 block = malloc(blocksize);
132 memset(block, 0, blocksize);
133 BUG_ON(sizeof(super) > blocksize);
134 memcpy(block, &super, sizeof(super));
135 ret = pwrite(fd, block, blocksize, BTRFS_SUPER_INFO_OFFSET);
136 BUG_ON(ret != blocksize);
138 /* create the tree of root objects */
139 empty_leaf = malloc(blocksize);
140 memset(empty_leaf, 0, blocksize);
141 btrfs_set_header_blocknr(&empty_leaf->header, start_block + 1);
142 btrfs_set_header_nritems(&empty_leaf->header, 2);
143 btrfs_set_header_generation(&empty_leaf->header, 0);
144 btrfs_set_header_owner(&empty_leaf->header, BTRFS_ROOT_TREE_OBJECTID);
145 memcpy(empty_leaf->header.fsid, super.fsid,
146 sizeof(empty_leaf->header.fsid));
148 /* create the items for the root tree */
149 inode_item = &root_item.inode;
150 memset(inode_item, 0, sizeof(*inode_item));
151 btrfs_set_inode_generation(inode_item, 1);
152 btrfs_set_inode_size(inode_item, 3);
153 btrfs_set_inode_nlink(inode_item, 1);
154 btrfs_set_inode_nblocks(inode_item, 1);
155 btrfs_set_inode_mode(inode_item, S_IFDIR | 0755);
157 btrfs_set_root_dirid(&root_item, 0);
158 btrfs_set_root_refs(&root_item, 1);
159 btrfs_set_disk_key_offset(&item.key, 0);
160 btrfs_set_disk_key_flags(&item.key, 0);
161 btrfs_set_item_size(&item, sizeof(root_item));
162 btrfs_set_disk_key_type(&item.key, BTRFS_ROOT_ITEM_KEY);
164 itemoff = __BTRFS_LEAF_DATA_SIZE(blocksize) - sizeof(root_item);
165 btrfs_set_root_blocknr(&root_item, start_block + 3);
166 btrfs_set_item_offset(&item, itemoff);
167 btrfs_set_disk_key_objectid(&item.key, BTRFS_EXTENT_TREE_OBJECTID);
168 memcpy(empty_leaf->items, &item, sizeof(item));
169 memcpy(btrfs_leaf_data(empty_leaf) + itemoff,
170 &root_item, sizeof(root_item));
172 btrfs_set_root_blocknr(&root_item, start_block + 4);
173 itemoff = itemoff - sizeof(root_item);
174 btrfs_set_item_offset(&item, itemoff);
175 btrfs_set_disk_key_objectid(&item.key, BTRFS_FS_TREE_OBJECTID);
176 memcpy(empty_leaf->items + 1, &item, sizeof(item));
177 memcpy(btrfs_leaf_data(empty_leaf) + itemoff,
178 &root_item, sizeof(root_item));
179 ret = pwrite(fd, empty_leaf, blocksize, (start_block + 1) * blocksize);
181 /* create the item for the dev tree */
182 btrfs_set_header_blocknr(&empty_leaf->header, start_block + 2);
183 btrfs_set_header_nritems(&empty_leaf->header, 1);
184 btrfs_set_disk_key_objectid(&item.key, 0);
185 btrfs_set_disk_key_offset(&item.key, num_blocks);
186 btrfs_set_disk_key_flags(&item.key, 0);
187 btrfs_set_disk_key_type(&item.key, BTRFS_DEV_ITEM_KEY);
189 item_size = sizeof(struct btrfs_device_item) + strlen(pathname);
190 itemoff = __BTRFS_LEAF_DATA_SIZE(blocksize) - item_size;
191 btrfs_set_item_offset(&item, itemoff);
192 btrfs_set_item_size(&item, item_size);
193 btrfs_set_device_pathlen(&dev_item, strlen(pathname));
194 btrfs_set_device_id(&dev_item, 1);
195 memcpy(empty_leaf->items, &item, sizeof(item));
196 memcpy(btrfs_leaf_data(empty_leaf) + itemoff, &dev_item,
198 memcpy(btrfs_leaf_data(empty_leaf) + itemoff + sizeof(dev_item),
199 pathname, strlen(pathname));
200 ret = pwrite(fd, empty_leaf, blocksize, (start_block + 2) * blocksize);
201 if (ret != blocksize)
204 /* create the items for the extent tree */
205 btrfs_set_header_blocknr(&empty_leaf->header, start_block + 3);
206 btrfs_set_header_nritems(&empty_leaf->header, 5);
208 /* item1, reserve blocks 0-16 */
209 btrfs_set_disk_key_objectid(&item.key, 0);
210 btrfs_set_disk_key_offset(&item.key, start_block + 1);
211 btrfs_set_disk_key_flags(&item.key, 0);
212 btrfs_set_disk_key_type(&item.key, BTRFS_EXTENT_ITEM_KEY);
213 itemoff = __BTRFS_LEAF_DATA_SIZE(blocksize) -
214 sizeof(struct btrfs_extent_item);
215 btrfs_set_item_offset(&item, itemoff);
216 btrfs_set_item_size(&item, sizeof(struct btrfs_extent_item));
217 btrfs_set_extent_refs(&extent_item, 1);
218 btrfs_set_extent_owner(&extent_item, BTRFS_ROOT_TREE_OBJECTID);
219 btrfs_set_extent_type(&extent_item, BTRFS_EXTENT_TREE);
220 memcpy(empty_leaf->items, &item, sizeof(item));
221 memcpy(btrfs_leaf_data(empty_leaf) + btrfs_item_offset(&item),
222 &extent_item, btrfs_item_size(&item));
224 /* item2, give block 17 to the root */
225 btrfs_set_disk_key_objectid(&item.key, start_block + 1);
226 btrfs_set_disk_key_offset(&item.key, 1);
227 itemoff = itemoff - sizeof(struct btrfs_extent_item);
228 btrfs_set_item_offset(&item, itemoff);
229 memcpy(empty_leaf->items + 1, &item, sizeof(item));
230 memcpy(btrfs_leaf_data(empty_leaf) + btrfs_item_offset(&item),
231 &extent_item, btrfs_item_size(&item));
233 /* item3, give block 18 to the dev root */
234 btrfs_set_disk_key_objectid(&item.key, start_block + 2);
235 btrfs_set_disk_key_offset(&item.key, 1);
236 itemoff = itemoff - sizeof(struct btrfs_extent_item);
237 btrfs_set_item_offset(&item, itemoff);
238 memcpy(empty_leaf->items + 2, &item, sizeof(item));
239 memcpy(btrfs_leaf_data(empty_leaf) + btrfs_item_offset(&item),
240 &extent_item, btrfs_item_size(&item));
242 /* item4, give block 19 to the extent root */
243 btrfs_set_disk_key_objectid(&item.key, start_block + 3);
244 btrfs_set_disk_key_offset(&item.key, 1);
245 itemoff = itemoff - sizeof(struct btrfs_extent_item);
246 btrfs_set_item_offset(&item, itemoff);
247 memcpy(empty_leaf->items + 3, &item, sizeof(item));
248 memcpy(btrfs_leaf_data(empty_leaf) + btrfs_item_offset(&item),
249 &extent_item, btrfs_item_size(&item));
251 /* item5, give block 20 to the FS root */
252 btrfs_set_disk_key_objectid(&item.key, start_block + 4);
253 btrfs_set_disk_key_offset(&item.key, 1);
254 itemoff = itemoff - sizeof(struct btrfs_extent_item);
255 btrfs_set_item_offset(&item, itemoff);
256 memcpy(empty_leaf->items + 4, &item, sizeof(item));
257 memcpy(btrfs_leaf_data(empty_leaf) + btrfs_item_offset(&item),
258 &extent_item, btrfs_item_size(&item));
259 ret = pwrite(fd, empty_leaf, blocksize, (start_block + 3) * blocksize);
260 if (ret != blocksize)
263 /* finally create the FS root */
264 btrfs_set_header_blocknr(&empty_leaf->header, start_block + 4);
265 btrfs_set_header_nritems(&empty_leaf->header, 0);
266 ret = pwrite(fd, empty_leaf, blocksize, (start_block + 4) * blocksize);
267 if (ret != blocksize)
272 u64 device_size(int fd, struct stat *st)
275 if (S_ISREG(st->st_mode)) {
278 if (!S_ISBLK(st->st_mode)) {
281 if (ioctl(fd, BLKGETSIZE64, &size) >= 0) {
286 int main(int ac, char **av)
294 char *buf = malloc(4096);
302 block_count = atoi(av[2]);
304 fprintf(stderr, "error finding block count\n");
309 fprintf(stderr, "usage: mkfs.btrfs file [block count]\n");
312 fd = open(file, O_RDWR);
314 fprintf(stderr, "unable to open %s\n", file);
317 ret = fstat(fd, &st);
319 fprintf(stderr, "unable to stat %s\n", file);
322 if (block_count == 0) {
323 block_count = device_size(fd, &st);
324 if (block_count == 0) {
325 fprintf(stderr, "unable to find %s size\n", file);
330 if (block_count < 256) {
331 fprintf(stderr, "device %s is too small\n", file);
334 memset(buf, 0, 4096);
335 for(i = 0; i < 64; i++) {
336 ret = write(fd, buf, 4096);
338 fprintf(stderr, "unable to zero fill device\n");
342 realpath_name = realpath(file, NULL);
343 ret = mkfs(fd, realpath_name, block_count, 4096);
345 fprintf(stderr, "error during mkfs %d\n", ret);
348 ret = make_root_dir(fd);
350 fprintf(stderr, "failed to setup the root directory\n");
353 printf("fs created on %s blocksize %d blocks %Lu\n",
354 file, 4096, block_count);