aboutsummaryrefslogtreecommitdiffstats
path: root/fs
diff options
context:
space:
mode:
authorChris Mason <chris.mason@oracle.com>2007-02-20 16:41:09 -0500
committerDavid Woodhouse <dwmw2@hera.kernel.org>2007-02-20 16:41:09 -0500
commit06ed4b316e8e24b6899ece7186c6a7a0129326ba (patch)
tree2396d50c3599a56c43e17b96368257c70c958904 /fs
parentd97e63b69ef21c02b67e20e41d9968b0e503572e (diff)
Btrfs: add mkfs.c
Signed-off-by: Chris Mason <chris.mason@oracle.com>
Diffstat (limited to 'fs')
-rw-r--r--fs/btrfs/mkfs.c69
1 files changed, 69 insertions, 0 deletions
diff --git a/fs/btrfs/mkfs.c b/fs/btrfs/mkfs.c
new file mode 100644
index 000000000000..bab98c6d6fa7
--- /dev/null
+++ b/fs/btrfs/mkfs.c
@@ -0,0 +1,69 @@
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
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
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}