blob: 5290423343f89c9e0837c20658c0af1931443f9c [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));
Chris Mason7518a232007-03-12 12:01:18 -040037 btrfs_set_header_parentid(&empty_leaf.header, 1);
38 btrfs_set_header_blocknr(&empty_leaf.header, 17);
Chris Mason06ed4b32007-02-20 16:41:09 -050039 ret = pwrite(fd, &empty_leaf, sizeof(empty_leaf), 17 * CTREE_BLOCKSIZE);
40 if (ret != sizeof(empty_leaf))
41 return -1;
42
Chris Mason7518a232007-03-12 12:01:18 -040043 btrfs_set_header_parentid(&empty_leaf.header, 2);
44 btrfs_set_header_blocknr(&empty_leaf.header, 18);
45 btrfs_set_header_nritems(&empty_leaf.header, 3);
Chris Mason9a8dd152007-02-23 08:38:36 -050046
47 /* item1, reserve blocks 0-16 */
Chris Masone2fa7222007-03-12 16:22:34 -040048 item.key.objectid = cpu_to_le64(0);
49 item.key.offset = cpu_to_le64(17);
50 item.key.flags = cpu_to_le32(0);
Chris Mason06ed4b32007-02-20 16:41:09 -050051 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 */
Chris Masone2fa7222007-03-12 16:22:34 -040059 item.key.objectid = cpu_to_le64(17);
60 item.key.offset = cpu_to_le64(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 */
Chris Masone2fa7222007-03-12 16:22:34 -040067 item.key.objectid = cpu_to_le64(18);
68 item.key.offset = cpu_to_le64(1);
Chris Mason9a8dd152007-02-23 08:38:36 -050069 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}