Chris Mason | 06ed4b3 | 2007-02-20 16:41:09 -0500 | [diff] [blame] | 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 | |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 21 | /* setup the super block area */ |
Chris Mason | 06ed4b3 | 2007-02-20 16:41:09 -0500 | [diff] [blame] | 22 | memset(info, 0, sizeof(info)); |
| 23 | info[0].blocknr = 16; |
| 24 | info[0].objectid = 1; |
| 25 | info[0].tree_root = 17; |
Chris Mason | 06ed4b3 | 2007-02-20 16:41:09 -0500 | [diff] [blame] | 26 | |
| 27 | info[1].blocknr = 16; |
| 28 | info[1].objectid = 2; |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame^] | 29 | info[1].tree_root = 18; |
Chris Mason | 06ed4b3 | 2007-02-20 16:41:09 -0500 | [diff] [blame] | 30 | ret = pwrite(fd, info, sizeof(info), |
| 31 | CTREE_SUPER_INFO_OFFSET(CTREE_BLOCKSIZE)); |
| 32 | if (ret != sizeof(info)) |
| 33 | return -1; |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 34 | |
| 35 | /* create leaves for the tree root and extent root */ |
Chris Mason | 06ed4b3 | 2007-02-20 16:41:09 -0500 | [diff] [blame] | 36 | 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 Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame^] | 44 | empty_leaf.header.blocknr = 18; |
| 45 | empty_leaf.header.nritems = 3; |
| 46 | |
| 47 | /* item1, reserve blocks 0-16 */ |
Chris Mason | 06ed4b3 | 2007-02-20 16:41:09 -0500 | [diff] [blame] | 48 | item.key.objectid = 0; |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame^] | 49 | item.key.offset = 17; |
Chris Mason | 06ed4b3 | 2007-02-20 16:41:09 -0500 | [diff] [blame] | 50 | 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 Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame^] | 54 | extent_item.owner = 0; |
Chris Mason | 06ed4b3 | 2007-02-20 16:41:09 -0500 | [diff] [blame] | 55 | memcpy(empty_leaf.items, &item, sizeof(item)); |
| 56 | memcpy(empty_leaf.data + item.offset, &extent_item, item.size); |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame^] | 57 | |
| 58 | /* item2, give block 17 to the root */ |
| 59 | item.key.objectid = 17; |
| 60 | item.key.offset = 1; |
Chris Mason | 06ed4b3 | 2007-02-20 16:41:09 -0500 | [diff] [blame] | 61 | item.offset = LEAF_DATA_SIZE - sizeof(struct extent_item) * 2; |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame^] | 62 | extent_item.owner = 1; |
Chris Mason | 06ed4b3 | 2007-02-20 16:41:09 -0500 | [diff] [blame] | 63 | memcpy(empty_leaf.items + 1, &item, sizeof(item)); |
| 64 | memcpy(empty_leaf.data + item.offset, &extent_item, item.size); |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame^] | 65 | |
| 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 Mason | 06ed4b3 | 2007-02-20 16:41:09 -0500 | [diff] [blame] | 74 | if (ret != sizeof(empty_leaf)) |
| 75 | return -1; |
| 76 | return 0; |
| 77 | } |