bab98c6d6fa75baf4dc7ac39669d01b7fcc95ec6
[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         memset(info, 0, sizeof(info));
22         info[0].blocknr = 16;
23         info[0].objectid = 1;
24         info[0].tree_root = 17;
25         info[0].alloc_extent.blocknr = 0;
26         info[0].alloc_extent.num_blocks = 20;
27         /* 0-17 are used (inclusive) */
28         info[0].alloc_extent.num_used = 18;
29
30         info[1].blocknr = 16;
31         info[1].objectid = 2;
32         info[1].tree_root = 64;
33         info[1].alloc_extent.blocknr = 64;
34         info[1].alloc_extent.num_blocks = 8;
35         info[1].alloc_extent.num_used = 1;
36         ret = pwrite(fd, info, sizeof(info),
37                      CTREE_SUPER_INFO_OFFSET(CTREE_BLOCKSIZE));
38         if (ret != sizeof(info))
39                 return -1;
40         memset(&empty_leaf, 0, sizeof(empty_leaf));
41         empty_leaf.header.parentid = 1;
42         empty_leaf.header.blocknr = 17;
43         ret = pwrite(fd, &empty_leaf, sizeof(empty_leaf), 17 * CTREE_BLOCKSIZE);
44         if (ret != sizeof(empty_leaf))
45                 return -1;
46
47         empty_leaf.header.parentid = 2;
48         empty_leaf.header.blocknr = 64;
49         empty_leaf.header.nritems = 2;
50         item.key.objectid = 0;
51         item.key.offset = 64;
52         item.key.flags = 0;
53         item.offset = LEAF_DATA_SIZE - sizeof(struct extent_item);
54         item.size = sizeof(struct extent_item);
55         extent_item.refs = 1;
56         extent_item.owner = 1;
57         memcpy(empty_leaf.items, &item, sizeof(item));
58         memcpy(empty_leaf.data + item.offset, &extent_item, item.size);
59         item.key.objectid = 64;
60         item.key.offset = 64;
61         item.offset = LEAF_DATA_SIZE - sizeof(struct extent_item) * 2;
62         extent_item.owner = 2;
63         memcpy(empty_leaf.items + 1, &item, sizeof(item));
64         memcpy(empty_leaf.data + item.offset, &extent_item, item.size);
65         ret = pwrite(fd, &empty_leaf, sizeof(empty_leaf), 64 * CTREE_BLOCKSIZE);
66         if (ret != sizeof(empty_leaf))
67                 return -1;
68         return 0;
69 }