extent fixes
[platform/upstream/btrfs-progs.git] / mkfs.c
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"
12
13 int mkfs(int fd)
14 {
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;
20
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;
26         info[0].alloc_extent.blocknr = 0;
27         info[0].alloc_extent.num_blocks = 64;
28         /* 0-17 are used (inclusive) */
29         info[0].alloc_extent.num_used = 18;
30
31         info[1].blocknr = 16;
32         info[1].objectid = 2;
33         info[1].tree_root = 64;
34         info[1].alloc_extent.blocknr = 64;
35         info[1].alloc_extent.num_blocks = 64;
36         info[1].alloc_extent.num_used = 1;
37         ret = pwrite(fd, info, sizeof(info),
38                      CTREE_SUPER_INFO_OFFSET(CTREE_BLOCKSIZE));
39         if (ret != sizeof(info))
40                 return -1;
41
42         /* create leaves for the tree root and extent root */
43         memset(&empty_leaf, 0, sizeof(empty_leaf));
44         empty_leaf.header.parentid = 1;
45         empty_leaf.header.blocknr = 17;
46         ret = pwrite(fd, &empty_leaf, sizeof(empty_leaf), 17 * CTREE_BLOCKSIZE);
47         if (ret != sizeof(empty_leaf))
48                 return -1;
49
50         empty_leaf.header.parentid = 2;
51         empty_leaf.header.blocknr = 64;
52         empty_leaf.header.nritems = 2;
53         item.key.objectid = 0;
54         item.key.offset = 64;
55         item.key.flags = 0;
56         item.offset = LEAF_DATA_SIZE - sizeof(struct extent_item);
57         item.size = sizeof(struct extent_item);
58         extent_item.refs = 1;
59         extent_item.owner = 1;
60         memcpy(empty_leaf.items, &item, sizeof(item));
61         memcpy(empty_leaf.data + item.offset, &extent_item, item.size);
62         item.key.objectid = 64;
63         item.key.offset = 64;
64         item.offset = LEAF_DATA_SIZE - sizeof(struct extent_item) * 2;
65         extent_item.owner = 2;
66         memcpy(empty_leaf.items + 1, &item, sizeof(item));
67         memcpy(empty_leaf.data + item.offset, &extent_item, item.size);
68         ret = pwrite(fd, &empty_leaf, sizeof(empty_leaf), 64 * CTREE_BLOCKSIZE);
69         if (ret != sizeof(empty_leaf))
70                 return -1;
71         return 0;
72 }