Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1 | #ifndef __CTREE__ |
| 2 | #define __CTREE__ |
| 3 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame^] | 4 | #define CTREE_BLOCKSIZE 1024 |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 5 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame^] | 6 | /* |
| 7 | * the key defines the order in the tree, and so it also defines (optimal) |
| 8 | * block layout. objectid corresonds to the inode number. The flags |
| 9 | * tells us things about the object, and is a kind of stream selector. |
| 10 | * so for a given inode, keys with flags of 1 might refer to the inode |
| 11 | * data, flags of 2 may point to file data in the btree and flags == 3 |
| 12 | * may point to extents. |
| 13 | * |
| 14 | * offset is the starting byte offset for this key in the stream. |
| 15 | */ |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 16 | struct key { |
| 17 | u64 objectid; |
| 18 | u32 flags; |
| 19 | u64 offset; |
| 20 | } __attribute__ ((__packed__)); |
| 21 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame^] | 22 | /* |
| 23 | * every tree block (leaf or node) starts with this header. |
| 24 | */ |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 25 | struct header { |
| 26 | u64 fsid[2]; /* FS specific uuid */ |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame^] | 27 | u64 blocknr; /* which block this node is supposed to live in */ |
| 28 | u64 parentid; /* objectid of the tree root */ |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 29 | u32 csum; |
| 30 | u32 ham; |
| 31 | u16 nritems; |
| 32 | u16 flags; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame^] | 33 | /* generation flags to be added */ |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 34 | } __attribute__ ((__packed__)); |
| 35 | |
| 36 | #define NODEPTRS_PER_BLOCK ((CTREE_BLOCKSIZE - sizeof(struct header)) / \ |
| 37 | (sizeof(struct key) + sizeof(u64))) |
| 38 | |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 39 | #define MAX_LEVEL 8 |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 40 | #define node_level(f) ((f) & (MAX_LEVEL-1)) |
| 41 | #define is_leaf(f) (node_level(f) == 0) |
| 42 | |
| 43 | struct tree_buffer; |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 44 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame^] | 45 | /* |
| 46 | * in ram representation of the tree. extent_root is used for all allocations |
| 47 | * and for the extent tree extent_root root. current_insert is used |
| 48 | * only for the extent tree. |
| 49 | */ |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 50 | struct ctree_root { |
| 51 | struct tree_buffer *node; |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 52 | struct ctree_root *extent_root; |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 53 | struct key current_insert; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 54 | int fp; |
| 55 | struct radix_tree_root cache_radix; |
| 56 | }; |
| 57 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame^] | 58 | /* |
| 59 | * describes a tree on disk |
| 60 | */ |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 61 | struct ctree_root_info { |
| 62 | u64 fsid[2]; /* FS specific uuid */ |
| 63 | u64 blocknr; /* blocknr of this block */ |
| 64 | u64 objectid; /* inode number of this root */ |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame^] | 65 | u64 tree_root; /* the tree root block */ |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 66 | u32 csum; |
| 67 | u32 ham; |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 68 | u64 snapuuid[2]; /* root specific uuid */ |
| 69 | } __attribute__ ((__packed__)); |
| 70 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame^] | 71 | /* |
| 72 | * the super block basically lists the main trees of the FS |
| 73 | * it currently lacks any block count etc etc |
| 74 | */ |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 75 | struct ctree_super_block { |
| 76 | struct ctree_root_info root_info; |
| 77 | struct ctree_root_info extent_info; |
| 78 | } __attribute__ ((__packed__)); |
| 79 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame^] | 80 | /* |
| 81 | * A leaf is full of items. The exact type of item is defined by |
| 82 | * the key flags parameter. offset and size tell us where to find |
| 83 | * the item in the leaf (relative to the start of the data area) |
| 84 | */ |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 85 | struct item { |
| 86 | struct key key; |
| 87 | u16 offset; |
| 88 | u16 size; |
| 89 | } __attribute__ ((__packed__)); |
| 90 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame^] | 91 | /* |
| 92 | * leaves have an item area and a data area: |
| 93 | * [item0, item1....itemN] [free space] [dataN...data1, data0] |
| 94 | * |
| 95 | * The data is separate from the items to get the keys closer together |
| 96 | * during searches. |
| 97 | */ |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 98 | #define LEAF_DATA_SIZE (CTREE_BLOCKSIZE - sizeof(struct header)) |
| 99 | struct leaf { |
| 100 | struct header header; |
| 101 | union { |
| 102 | struct item items[LEAF_DATA_SIZE/sizeof(struct item)]; |
| 103 | u8 data[CTREE_BLOCKSIZE-sizeof(struct header)]; |
| 104 | }; |
| 105 | } __attribute__ ((__packed__)); |
| 106 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame^] | 107 | /* |
| 108 | * all non-leaf blocks are nodes, they hold only keys and pointers to |
| 109 | * other blocks |
| 110 | */ |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 111 | struct node { |
| 112 | struct header header; |
| 113 | struct key keys[NODEPTRS_PER_BLOCK]; |
| 114 | u64 blockptrs[NODEPTRS_PER_BLOCK]; |
| 115 | } __attribute__ ((__packed__)); |
| 116 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame^] | 117 | /* |
| 118 | * items in the extent btree are used to record the objectid of the |
| 119 | * owner of the block and the number of references |
| 120 | */ |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 121 | struct extent_item { |
| 122 | u32 refs; |
| 123 | u64 owner; |
| 124 | } __attribute__ ((__packed__)); |
| 125 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame^] | 126 | /* |
| 127 | * ctree_paths remember the path taken from the root down to the leaf. |
| 128 | * level 0 is always the leaf, and nodes[1...MAX_LEVEL] will point |
| 129 | * to any other levels that are present. |
| 130 | * |
| 131 | * The slots array records the index of the item or block pointer |
| 132 | * used while walking the tree. |
| 133 | */ |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 134 | struct ctree_path { |
| 135 | struct tree_buffer *nodes[MAX_LEVEL]; |
| 136 | int slots[MAX_LEVEL]; |
| 137 | }; |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 138 | |
| 139 | struct tree_buffer *alloc_free_block(struct ctree_root *root); |
| 140 | int free_extent(struct ctree_root *root, u64 blocknr, u64 num_blocks); |
| 141 | int search_slot(struct ctree_root *root, struct key *key, struct ctree_path *p, int ins_len); |
| 142 | void release_path(struct ctree_root *root, struct ctree_path *p); |
| 143 | void init_path(struct ctree_path *p); |
| 144 | int del_item(struct ctree_root *root, struct ctree_path *path); |
| 145 | int insert_item(struct ctree_root *root, struct key *key, void *data, int data_size); |
| 146 | int next_leaf(struct ctree_root *root, struct ctree_path *path); |
| 147 | int leaf_free_space(struct leaf *leaf); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 148 | #endif |