blob: fd4e5dea7e1b052a8c85cf0b70a96ce008ec926f [file] [log] [blame]
Chris Mason06ed4b32007-02-20 16:41:09 -05001#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
13int 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
Chris Masoncfaa7292007-02-21 17:04:57 -050021 /* setup the super block area */
Chris Mason06ed4b32007-02-20 16:41:09 -050022 memset(info, 0, sizeof(info));
23 info[0].blocknr = 16;
24 info[0].objectid = 1;
25 info[0].tree_root = 17;
Chris Mason06ed4b32007-02-20 16:41:09 -050026
27 info[1].blocknr = 16;
28 info[1].objectid = 2;
Chris Mason9a8dd152007-02-23 08:38:36 -050029 info[1].tree_root = 18;
Chris Mason06ed4b32007-02-20 16:41:09 -050030 ret = pwrite(fd, info, sizeof(info),
31 CTREE_SUPER_INFO_OFFSET(CTREE_BLOCKSIZE));
32 if (ret != sizeof(info))
33 return -1;
Chris Masoncfaa7292007-02-21 17:04:57 -050034
35 /* create leaves for the tree root and extent root */
Chris Mason06ed4b32007-02-20 16:41:09 -050036 memset(&empty_leaf, 0, sizeof(empty_leaf));
37 empty_leaf.header.parentid = 1;
38 empty_leaf.header.blocknr = 17;
39 ret = pwrite(fd, &empty_leaf, sizeof(empty_leaf), 17 * CTREE_BLOCKSIZE);
40 if (ret != sizeof(empty_leaf))
41 return -1;
42
43 empty_leaf.header.parentid = 2;
Chris Mason9a8dd152007-02-23 08:38:36 -050044 empty_leaf.header.blocknr = 18;
45 empty_leaf.header.nritems = 3;
46
47 /* item1, reserve blocks 0-16 */
Chris Mason06ed4b32007-02-20 16:41:09 -050048 item.key.objectid = 0;
Chris Mason9a8dd152007-02-23 08:38:36 -050049 item.key.offset = 17;
Chris Mason06ed4b32007-02-20 16:41:09 -050050 item.key.flags = 0;
51 item.offset = LEAF_DATA_SIZE - sizeof(struct extent_item);
52 item.size = sizeof(struct extent_item);
53 extent_item.refs = 1;
Chris Mason9a8dd152007-02-23 08:38:36 -050054 extent_item.owner = 0;
Chris Mason06ed4b32007-02-20 16:41:09 -050055 memcpy(empty_leaf.items, &item, sizeof(item));
56 memcpy(empty_leaf.data + item.offset, &extent_item, item.size);
Chris Mason9a8dd152007-02-23 08:38:36 -050057
58 /* item2, give block 17 to the root */
59 item.key.objectid = 17;
60 item.key.offset = 1;
Chris Mason06ed4b32007-02-20 16:41:09 -050061 item.offset = LEAF_DATA_SIZE - sizeof(struct extent_item) * 2;
Chris Mason9a8dd152007-02-23 08:38:36 -050062 extent_item.owner = 1;
Chris Mason06ed4b32007-02-20 16:41:09 -050063 memcpy(empty_leaf.items + 1, &item, sizeof(item));
64 memcpy(empty_leaf.data + item.offset, &extent_item, item.size);
Chris Mason9a8dd152007-02-23 08:38:36 -050065
66 /* item3, give block 18 for the extent root */
67 item.key.objectid = 18;
68 item.key.offset = 1;
69 item.offset = LEAF_DATA_SIZE - sizeof(struct extent_item) * 3;
70 extent_item.owner = 2;
71 memcpy(empty_leaf.items + 2, &item, sizeof(item));
72 memcpy(empty_leaf.data + item.offset, &extent_item, item.size);
73 ret = pwrite(fd, &empty_leaf, sizeof(empty_leaf), 18 * CTREE_BLOCKSIZE);
Chris Mason06ed4b32007-02-20 16:41:09 -050074 if (ret != sizeof(empty_leaf))
75 return -1;
76 return 0;
77}