Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 1 | #ifndef __CTREE__ |
| 2 | #define __CTREE__ |
| 3 | |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 4 | #include "list.h" |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame^] | 5 | #include "kerncompat.h" |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 6 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 7 | #define CTREE_BLOCKSIZE 1024 |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 8 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 9 | /* |
| 10 | * the key defines the order in the tree, and so it also defines (optimal) |
| 11 | * block layout. objectid corresonds to the inode number. The flags |
| 12 | * tells us things about the object, and is a kind of stream selector. |
| 13 | * so for a given inode, keys with flags of 1 might refer to the inode |
| 14 | * data, flags of 2 may point to file data in the btree and flags == 3 |
| 15 | * may point to extents. |
| 16 | * |
| 17 | * offset is the starting byte offset for this key in the stream. |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame^] | 18 | * |
| 19 | * btrfs_disk_key is in disk byte order. struct btrfs_key is always |
| 20 | * in cpu native order. Otherwise they are identical and their sizes |
| 21 | * should be the same (ie both packed) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 22 | */ |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame^] | 23 | struct btrfs_disk_key { |
| 24 | __le64 objectid; |
| 25 | __le32 flags; |
| 26 | __le64 offset; |
| 27 | } __attribute__ ((__packed__)); |
| 28 | |
| 29 | struct btrfs_key { |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 30 | u64 objectid; |
| 31 | u32 flags; |
| 32 | u64 offset; |
| 33 | } __attribute__ ((__packed__)); |
| 34 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 35 | /* |
| 36 | * every tree block (leaf or node) starts with this header. |
| 37 | */ |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 38 | struct btrfs_header { |
| 39 | __le64 fsid[2]; /* FS specific uuid */ |
| 40 | __le64 blocknr; /* which block this node is supposed to live in */ |
| 41 | __le64 parentid; /* objectid of the tree root */ |
| 42 | __le32 csum; |
| 43 | __le32 ham; |
| 44 | __le16 nritems; |
| 45 | __le16 flags; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 46 | /* generation flags to be added */ |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 47 | } __attribute__ ((__packed__)); |
| 48 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 49 | #define MAX_LEVEL 8 |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 50 | #define NODEPTRS_PER_BLOCK ((CTREE_BLOCKSIZE - sizeof(struct btrfs_header)) / \ |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame^] | 51 | (sizeof(struct btrfs_disk_key) + sizeof(u64))) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 52 | |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 53 | struct tree_buffer; |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 54 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 55 | /* |
| 56 | * in ram representation of the tree. extent_root is used for all allocations |
| 57 | * and for the extent tree extent_root root. current_insert is used |
| 58 | * only for the extent tree. |
| 59 | */ |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 60 | struct ctree_root { |
| 61 | struct tree_buffer *node; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 62 | struct tree_buffer *commit_root; |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 63 | struct ctree_root *extent_root; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame^] | 64 | struct btrfs_key current_insert; |
| 65 | struct btrfs_key last_insert; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 66 | int fp; |
| 67 | struct radix_tree_root cache_radix; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 68 | struct radix_tree_root pinned_radix; |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 69 | struct list_head trans; |
| 70 | struct list_head cache; |
| 71 | int cache_size; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 72 | }; |
| 73 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 74 | /* |
| 75 | * describes a tree on disk |
| 76 | */ |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 77 | struct ctree_root_info { |
| 78 | u64 fsid[2]; /* FS specific uuid */ |
| 79 | u64 blocknr; /* blocknr of this block */ |
| 80 | u64 objectid; /* inode number of this root */ |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 81 | u64 tree_root; /* the tree root block */ |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 82 | u32 csum; |
| 83 | u32 ham; |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 84 | u64 snapuuid[2]; /* root specific uuid */ |
| 85 | } __attribute__ ((__packed__)); |
| 86 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 87 | /* |
| 88 | * the super block basically lists the main trees of the FS |
| 89 | * it currently lacks any block count etc etc |
| 90 | */ |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 91 | struct ctree_super_block { |
| 92 | struct ctree_root_info root_info; |
| 93 | struct ctree_root_info extent_info; |
| 94 | } __attribute__ ((__packed__)); |
| 95 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 96 | /* |
| 97 | * A leaf is full of items. The exact type of item is defined by |
| 98 | * the key flags parameter. offset and size tell us where to find |
| 99 | * the item in the leaf (relative to the start of the data area) |
| 100 | */ |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 101 | struct item { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame^] | 102 | struct btrfs_disk_key key; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 103 | u16 offset; |
| 104 | u16 size; |
| 105 | } __attribute__ ((__packed__)); |
| 106 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 107 | /* |
| 108 | * leaves have an item area and a data area: |
| 109 | * [item0, item1....itemN] [free space] [dataN...data1, data0] |
| 110 | * |
| 111 | * The data is separate from the items to get the keys closer together |
| 112 | * during searches. |
| 113 | */ |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 114 | #define LEAF_DATA_SIZE (CTREE_BLOCKSIZE - sizeof(struct btrfs_header)) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 115 | struct leaf { |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 116 | struct btrfs_header header; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 117 | union { |
| 118 | struct item items[LEAF_DATA_SIZE/sizeof(struct item)]; |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 119 | u8 data[CTREE_BLOCKSIZE-sizeof(struct btrfs_header)]; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 120 | }; |
| 121 | } __attribute__ ((__packed__)); |
| 122 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 123 | /* |
| 124 | * all non-leaf blocks are nodes, they hold only keys and pointers to |
| 125 | * other blocks |
| 126 | */ |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 127 | struct node { |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 128 | struct btrfs_header header; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame^] | 129 | struct btrfs_disk_key keys[NODEPTRS_PER_BLOCK]; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 130 | u64 blockptrs[NODEPTRS_PER_BLOCK]; |
| 131 | } __attribute__ ((__packed__)); |
| 132 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 133 | /* |
| 134 | * items in the extent btree are used to record the objectid of the |
| 135 | * owner of the block and the number of references |
| 136 | */ |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 137 | struct extent_item { |
| 138 | u32 refs; |
| 139 | u64 owner; |
| 140 | } __attribute__ ((__packed__)); |
| 141 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 142 | /* |
| 143 | * ctree_paths remember the path taken from the root down to the leaf. |
| 144 | * level 0 is always the leaf, and nodes[1...MAX_LEVEL] will point |
| 145 | * to any other levels that are present. |
| 146 | * |
| 147 | * The slots array records the index of the item or block pointer |
| 148 | * used while walking the tree. |
| 149 | */ |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 150 | struct ctree_path { |
| 151 | struct tree_buffer *nodes[MAX_LEVEL]; |
| 152 | int slots[MAX_LEVEL]; |
| 153 | }; |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 154 | |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame^] | 155 | static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu, |
| 156 | struct btrfs_disk_key *disk) |
| 157 | { |
| 158 | cpu->offset = le64_to_cpu(disk->offset); |
| 159 | cpu->flags = le32_to_cpu(disk->flags); |
| 160 | cpu->objectid = le64_to_cpu(disk->objectid); |
| 161 | } |
| 162 | |
| 163 | static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk, |
| 164 | struct btrfs_key *cpu) |
| 165 | { |
| 166 | disk->offset = cpu_to_le64(cpu->offset); |
| 167 | disk->flags = cpu_to_le32(cpu->flags); |
| 168 | disk->objectid = cpu_to_le64(cpu->objectid); |
| 169 | } |
| 170 | |
| 171 | static inline u64 btrfs_key_objectid(struct btrfs_disk_key *disk) |
| 172 | { |
| 173 | return le64_to_cpu(disk->objectid); |
| 174 | } |
| 175 | |
| 176 | static inline void btrfs_set_key_objectid(struct btrfs_disk_key *disk, |
| 177 | u64 val) |
| 178 | { |
| 179 | disk->objectid = cpu_to_le64(val); |
| 180 | } |
| 181 | |
| 182 | static inline u64 btrfs_key_offset(struct btrfs_disk_key *disk) |
| 183 | { |
| 184 | return le64_to_cpu(disk->offset); |
| 185 | } |
| 186 | |
| 187 | static inline void btrfs_set_key_offset(struct btrfs_disk_key *disk, |
| 188 | u64 val) |
| 189 | { |
| 190 | disk->offset = cpu_to_le64(val); |
| 191 | } |
| 192 | |
| 193 | static inline u32 btrfs_key_flags(struct btrfs_disk_key *disk) |
| 194 | { |
| 195 | return le32_to_cpu(disk->flags); |
| 196 | } |
| 197 | |
| 198 | static inline void btrfs_set_key_flags(struct btrfs_disk_key *disk, |
| 199 | u32 val) |
| 200 | { |
| 201 | disk->flags = cpu_to_le32(val); |
| 202 | } |
| 203 | |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 204 | static inline u64 btrfs_header_blocknr(struct btrfs_header *h) |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 205 | { |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 206 | return le64_to_cpu(h->blocknr); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 207 | } |
| 208 | |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 209 | static inline void btrfs_set_header_blocknr(struct btrfs_header *h, u64 blocknr) |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 210 | { |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 211 | h->blocknr = cpu_to_le64(blocknr); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 212 | } |
| 213 | |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 214 | static inline u64 btrfs_header_parentid(struct btrfs_header *h) |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 215 | { |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 216 | return le64_to_cpu(h->parentid); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 217 | } |
| 218 | |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 219 | static inline void btrfs_set_header_parentid(struct btrfs_header *h, |
| 220 | u64 parentid) |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 221 | { |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 222 | h->parentid = cpu_to_le64(parentid); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 223 | } |
| 224 | |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 225 | static inline u16 btrfs_header_nritems(struct btrfs_header *h) |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 226 | { |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 227 | return le16_to_cpu(h->nritems); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 228 | } |
| 229 | |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 230 | static inline void btrfs_set_header_nritems(struct btrfs_header *h, u16 val) |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 231 | { |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 232 | h->nritems = cpu_to_le16(val); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 233 | } |
| 234 | |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 235 | static inline u16 btrfs_header_flags(struct btrfs_header *h) |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 236 | { |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 237 | return le16_to_cpu(h->flags); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 238 | } |
| 239 | |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 240 | static inline void btrfs_set_header_flags(struct btrfs_header *h, u16 val) |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 241 | { |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 242 | h->flags = cpu_to_le16(val); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 243 | } |
| 244 | |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 245 | static inline int btrfs_header_level(struct btrfs_header *h) |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 246 | { |
| 247 | return btrfs_header_flags(h) & (MAX_LEVEL - 1); |
| 248 | } |
| 249 | |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 250 | static inline void btrfs_set_header_level(struct btrfs_header *h, int level) |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 251 | { |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 252 | u16 flags; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 253 | BUG_ON(level > MAX_LEVEL); |
| 254 | flags = btrfs_header_flags(h) & ~(MAX_LEVEL - 1); |
| 255 | btrfs_set_header_flags(h, flags | level); |
| 256 | } |
| 257 | |
| 258 | static inline int btrfs_is_leaf(struct node *n) |
| 259 | { |
| 260 | return (btrfs_header_level(&n->header) == 0); |
| 261 | } |
| 262 | |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 263 | struct tree_buffer *alloc_free_block(struct ctree_root *root); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 264 | int btrfs_inc_ref(struct ctree_root *root, struct tree_buffer *buf); |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 265 | int free_extent(struct ctree_root *root, u64 blocknr, u64 num_blocks); |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame^] | 266 | int search_slot(struct ctree_root *root, struct btrfs_key *key, |
| 267 | struct ctree_path *p, int ins_len, int cow); |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 268 | void release_path(struct ctree_root *root, struct ctree_path *p); |
| 269 | void init_path(struct ctree_path *p); |
| 270 | int del_item(struct ctree_root *root, struct ctree_path *path); |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame^] | 271 | int insert_item(struct ctree_root *root, struct btrfs_key *key, |
| 272 | void *data, int data_size); |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 273 | int next_leaf(struct ctree_root *root, struct ctree_path *path); |
| 274 | int leaf_free_space(struct leaf *leaf); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 275 | int btrfs_drop_snapshot(struct ctree_root *root, struct tree_buffer *snap); |
| 276 | int btrfs_finish_extent_commit(struct ctree_root *root); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 277 | #endif |