mkfs.c 2.6 KB
Newer Older
C
Chris Mason 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#define _XOPEN_SOURCE 500
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "kerncompat.h"
#include "radix-tree.h"
#include "ctree.h"
#include "disk-io.h"

int mkfs(int fd)
{
C
Chris Mason 已提交
15 16
	struct btrfs_root_info info[2];
	struct btrfs_leaf empty_leaf;
C
Chris Mason 已提交
17
	struct btrfs_item item;
C
Chris Mason 已提交
18
	struct btrfs_extent_item extent_item;
C
Chris Mason 已提交
19 20
	int ret;

C
Chris Mason 已提交
21
	/* setup the super block area */
C
Chris Mason 已提交
22 23 24 25 26 27 28
	memset(info, 0, sizeof(info));
	info[0].blocknr = 16;
	info[0].objectid = 1;
	info[0].tree_root = 17;

	info[1].blocknr = 16;
	info[1].objectid = 2;
29
	info[1].tree_root = 18;
C
Chris Mason 已提交
30
	ret = pwrite(fd, info, sizeof(info),
C
Chris Mason 已提交
31
		     BTRFS_SUPER_INFO_OFFSET(BTRFS_BLOCKSIZE));
C
Chris Mason 已提交
32 33
	if (ret != sizeof(info))
		return -1;
C
Chris Mason 已提交
34 35

	/* create leaves for the tree root and extent root */
C
Chris Mason 已提交
36
	memset(&empty_leaf, 0, sizeof(empty_leaf));
37 38
	btrfs_set_header_parentid(&empty_leaf.header, 1);
	btrfs_set_header_blocknr(&empty_leaf.header, 17);
C
Chris Mason 已提交
39
	ret = pwrite(fd, &empty_leaf, sizeof(empty_leaf), 17 * BTRFS_BLOCKSIZE);
C
Chris Mason 已提交
40 41 42
	if (ret != sizeof(empty_leaf))
		return -1;

43 44 45
	btrfs_set_header_parentid(&empty_leaf.header, 2);
	btrfs_set_header_blocknr(&empty_leaf.header, 18);
	btrfs_set_header_nritems(&empty_leaf.header, 3);
46 47

	/* item1, reserve blocks 0-16 */
C
Chris Mason 已提交
48 49 50
	btrfs_set_key_objectid(&item.key, 0);
	btrfs_set_key_offset(&item.key, 17);
	btrfs_set_key_flags(&item.key, 0);
C
Chris Mason 已提交
51 52 53
	btrfs_set_item_offset(&item, LEAF_DATA_SIZE -
			      sizeof(struct btrfs_extent_item));
	btrfs_set_item_size(&item, sizeof(struct btrfs_extent_item));
C
Chris Mason 已提交
54 55
	btrfs_set_extent_refs(&extent_item, 1);
	btrfs_set_extent_owner(&extent_item, 0);
C
Chris Mason 已提交
56
	memcpy(empty_leaf.items, &item, sizeof(item));
C
Chris Mason 已提交
57 58
	memcpy(empty_leaf.data + btrfs_item_offset(&item), &extent_item,
		btrfs_item_size(&item));
59 60

	/* item2, give block 17 to the root */
C
Chris Mason 已提交
61 62
	btrfs_set_key_objectid(&item.key, 17);
	btrfs_set_key_offset(&item.key, 1);
C
Chris Mason 已提交
63 64
	btrfs_set_item_offset(&item, LEAF_DATA_SIZE -
			      sizeof(struct btrfs_extent_item) * 2);
C
Chris Mason 已提交
65
	btrfs_set_extent_owner(&extent_item, 1);
C
Chris Mason 已提交
66
	memcpy(empty_leaf.items + 1, &item, sizeof(item));
C
Chris Mason 已提交
67 68
	memcpy(empty_leaf.data + btrfs_item_offset(&item), &extent_item,
		btrfs_item_size(&item));
69 70

	/* item3, give block 18 for the extent root */
C
Chris Mason 已提交
71 72
	btrfs_set_key_objectid(&item.key, 18);
	btrfs_set_key_offset(&item.key, 1);
C
Chris Mason 已提交
73 74
	btrfs_set_item_offset(&item, LEAF_DATA_SIZE -
			      sizeof(struct btrfs_extent_item) * 3);
C
Chris Mason 已提交
75
	btrfs_set_extent_owner(&extent_item, 2);
76
	memcpy(empty_leaf.items + 2, &item, sizeof(item));
C
Chris Mason 已提交
77 78
	memcpy(empty_leaf.data + btrfs_item_offset(&item), &extent_item,
		btrfs_item_size(&item));
C
Chris Mason 已提交
79
	ret = pwrite(fd, &empty_leaf, sizeof(empty_leaf), 18 * BTRFS_BLOCKSIZE);
C
Chris Mason 已提交
80 81 82 83
	if (ret != sizeof(empty_leaf))
		return -1;
	return 0;
}