Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1 | #ifndef __BTRFS__ |
| 2 | #define __BTRFS__ |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 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 | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 7 | #define BTRFS_MAGIC "_BtRfS_M" |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 8 | #define BTRFS_BLOCKSIZE 1024 |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 9 | |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 10 | #define BTRFS_ROOT_TREE_OBJECTID 1 |
| 11 | #define BTRFS_EXTENT_TREE_OBJECTID 2 |
| 12 | #define BTRFS_FS_TREE_OBJECTID 3 |
| 13 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 14 | /* |
| 15 | * the key defines the order in the tree, and so it also defines (optimal) |
| 16 | * block layout. objectid corresonds to the inode number. The flags |
| 17 | * tells us things about the object, and is a kind of stream selector. |
| 18 | * so for a given inode, keys with flags of 1 might refer to the inode |
| 19 | * data, flags of 2 may point to file data in the btree and flags == 3 |
| 20 | * may point to extents. |
| 21 | * |
| 22 | * offset is the starting byte offset for this key in the stream. |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 23 | * |
| 24 | * btrfs_disk_key is in disk byte order. struct btrfs_key is always |
| 25 | * in cpu native order. Otherwise they are identical and their sizes |
| 26 | * should be the same (ie both packed) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 27 | */ |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 28 | struct btrfs_disk_key { |
| 29 | __le64 objectid; |
| 30 | __le32 flags; |
| 31 | __le64 offset; |
| 32 | } __attribute__ ((__packed__)); |
| 33 | |
| 34 | struct btrfs_key { |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 35 | u64 objectid; |
| 36 | u32 flags; |
| 37 | u64 offset; |
| 38 | } __attribute__ ((__packed__)); |
| 39 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 40 | /* |
| 41 | * every tree block (leaf or node) starts with this header. |
| 42 | */ |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 43 | struct btrfs_header { |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 44 | u8 fsid[16]; /* FS specific uuid */ |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 45 | __le64 blocknr; /* which block this node is supposed to live in */ |
| 46 | __le64 parentid; /* objectid of the tree root */ |
| 47 | __le32 csum; |
| 48 | __le32 ham; |
| 49 | __le16 nritems; |
| 50 | __le16 flags; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 51 | /* generation flags to be added */ |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 52 | } __attribute__ ((__packed__)); |
| 53 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 54 | #define BTRFS_MAX_LEVEL 8 |
| 55 | #define NODEPTRS_PER_BLOCK ((BTRFS_BLOCKSIZE - sizeof(struct btrfs_header)) / \ |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 56 | (sizeof(struct btrfs_disk_key) + sizeof(u64))) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 57 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 58 | struct btrfs_buffer; |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 59 | |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 60 | struct btrfs_root_item { |
| 61 | __le64 blocknr; |
| 62 | __le32 flags; |
| 63 | __le64 block_limit; |
| 64 | __le64 blocks_used; |
| 65 | __le32 refs; |
| 66 | }; |
| 67 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 68 | /* |
| 69 | * in ram representation of the tree. extent_root is used for all allocations |
| 70 | * and for the extent tree extent_root root. current_insert is used |
| 71 | * only for the extent tree. |
| 72 | */ |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 73 | struct btrfs_root { |
| 74 | struct btrfs_buffer *node; |
| 75 | struct btrfs_buffer *commit_root; |
| 76 | struct btrfs_root *extent_root; |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 77 | struct btrfs_root *tree_root; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 78 | struct btrfs_key current_insert; |
| 79 | struct btrfs_key last_insert; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 80 | int fp; |
| 81 | struct radix_tree_root cache_radix; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 82 | struct radix_tree_root pinned_radix; |
Chris Mason | ed2ff2c | 2007-03-01 18:59:40 -0500 | [diff] [blame] | 83 | struct list_head trans; |
| 84 | struct list_head cache; |
| 85 | int cache_size; |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 86 | int ref_cows; |
| 87 | struct btrfs_root_item root_item; |
| 88 | struct btrfs_key root_key; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 89 | }; |
| 90 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 91 | /* |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 92 | * the super block basically lists the main trees of the FS |
| 93 | * it currently lacks any block count etc etc |
| 94 | */ |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 95 | struct btrfs_super_block { |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 96 | u8 fsid[16]; /* FS specific uuid */ |
| 97 | __le64 blocknr; /* this block number */ |
| 98 | __le32 csum; |
| 99 | __le64 magic; |
| 100 | __le16 blocksize; |
| 101 | __le64 generation; |
| 102 | __le64 root; |
| 103 | __le64 total_blocks; |
| 104 | __le64 blocks_used; |
Chris Mason | cfaa729 | 2007-02-21 17:04:57 -0500 | [diff] [blame] | 105 | } __attribute__ ((__packed__)); |
| 106 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 107 | /* |
| 108 | * A leaf is full of items. The exact type of item is defined by |
| 109 | * the key flags parameter. offset and size tell us where to find |
| 110 | * the item in the leaf (relative to the start of the data area) |
| 111 | */ |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 112 | struct btrfs_item { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 113 | struct btrfs_disk_key key; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 114 | __le16 offset; |
| 115 | __le16 size; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 116 | } __attribute__ ((__packed__)); |
| 117 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 118 | /* |
| 119 | * leaves have an item area and a data area: |
| 120 | * [item0, item1....itemN] [free space] [dataN...data1, data0] |
| 121 | * |
| 122 | * The data is separate from the items to get the keys closer together |
| 123 | * during searches. |
| 124 | */ |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 125 | #define LEAF_DATA_SIZE (BTRFS_BLOCKSIZE - sizeof(struct btrfs_header)) |
| 126 | struct btrfs_leaf { |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 127 | struct btrfs_header header; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 128 | union { |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 129 | struct btrfs_item items[LEAF_DATA_SIZE/ |
| 130 | sizeof(struct btrfs_item)]; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 131 | u8 data[BTRFS_BLOCKSIZE - sizeof(struct btrfs_header)]; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 132 | }; |
| 133 | } __attribute__ ((__packed__)); |
| 134 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 135 | /* |
| 136 | * all non-leaf blocks are nodes, they hold only keys and pointers to |
| 137 | * other blocks |
| 138 | */ |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 139 | struct btrfs_node { |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 140 | struct btrfs_header header; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 141 | struct btrfs_disk_key keys[NODEPTRS_PER_BLOCK]; |
Chris Mason | 1d4f8a0 | 2007-03-13 09:28:32 -0400 | [diff] [blame] | 142 | __le64 blockptrs[NODEPTRS_PER_BLOCK]; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 143 | } __attribute__ ((__packed__)); |
| 144 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 145 | /* |
| 146 | * items in the extent btree are used to record the objectid of the |
| 147 | * owner of the block and the number of references |
| 148 | */ |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 149 | struct btrfs_extent_item { |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 150 | __le32 refs; |
| 151 | __le64 owner; |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 152 | } __attribute__ ((__packed__)); |
| 153 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 154 | /* |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 155 | * btrfs_paths remember the path taken from the root down to the leaf. |
| 156 | * level 0 is always the leaf, and nodes[1...BTRFS_MAX_LEVEL] will point |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 157 | * to any other levels that are present. |
| 158 | * |
| 159 | * The slots array records the index of the item or block pointer |
| 160 | * used while walking the tree. |
| 161 | */ |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 162 | struct btrfs_path { |
| 163 | struct btrfs_buffer *nodes[BTRFS_MAX_LEVEL]; |
| 164 | int slots[BTRFS_MAX_LEVEL]; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 165 | }; |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 166 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 167 | static inline u64 btrfs_extent_owner(struct btrfs_extent_item *ei) |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 168 | { |
| 169 | return le64_to_cpu(ei->owner); |
| 170 | } |
| 171 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 172 | static inline void btrfs_set_extent_owner(struct btrfs_extent_item *ei, u64 val) |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 173 | { |
| 174 | ei->owner = cpu_to_le64(val); |
| 175 | } |
| 176 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 177 | static inline u32 btrfs_extent_refs(struct btrfs_extent_item *ei) |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 178 | { |
| 179 | return le32_to_cpu(ei->refs); |
| 180 | } |
| 181 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 182 | static inline void btrfs_set_extent_refs(struct btrfs_extent_item *ei, u32 val) |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 183 | { |
| 184 | ei->refs = cpu_to_le32(val); |
| 185 | } |
| 186 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 187 | static inline u64 btrfs_node_blockptr(struct btrfs_node *n, int nr) |
Chris Mason | 1d4f8a0 | 2007-03-13 09:28:32 -0400 | [diff] [blame] | 188 | { |
| 189 | return le64_to_cpu(n->blockptrs[nr]); |
| 190 | } |
| 191 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 192 | static inline void btrfs_set_node_blockptr(struct btrfs_node *n, int nr, |
| 193 | u64 val) |
Chris Mason | 1d4f8a0 | 2007-03-13 09:28:32 -0400 | [diff] [blame] | 194 | { |
| 195 | n->blockptrs[nr] = cpu_to_le64(val); |
| 196 | } |
| 197 | |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 198 | static inline u16 btrfs_item_offset(struct btrfs_item *item) |
| 199 | { |
| 200 | return le16_to_cpu(item->offset); |
| 201 | } |
| 202 | |
| 203 | static inline void btrfs_set_item_offset(struct btrfs_item *item, u16 val) |
| 204 | { |
| 205 | item->offset = cpu_to_le16(val); |
| 206 | } |
| 207 | |
| 208 | static inline u16 btrfs_item_end(struct btrfs_item *item) |
| 209 | { |
| 210 | return le16_to_cpu(item->offset) + le16_to_cpu(item->size); |
| 211 | } |
| 212 | |
| 213 | static inline u16 btrfs_item_size(struct btrfs_item *item) |
| 214 | { |
| 215 | return le16_to_cpu(item->size); |
| 216 | } |
| 217 | |
| 218 | static inline void btrfs_set_item_size(struct btrfs_item *item, u16 val) |
| 219 | { |
| 220 | item->size = cpu_to_le16(val); |
| 221 | } |
| 222 | |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 223 | static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu, |
| 224 | struct btrfs_disk_key *disk) |
| 225 | { |
| 226 | cpu->offset = le64_to_cpu(disk->offset); |
| 227 | cpu->flags = le32_to_cpu(disk->flags); |
| 228 | cpu->objectid = le64_to_cpu(disk->objectid); |
| 229 | } |
| 230 | |
| 231 | static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk, |
| 232 | struct btrfs_key *cpu) |
| 233 | { |
| 234 | disk->offset = cpu_to_le64(cpu->offset); |
| 235 | disk->flags = cpu_to_le32(cpu->flags); |
| 236 | disk->objectid = cpu_to_le64(cpu->objectid); |
| 237 | } |
| 238 | |
| 239 | static inline u64 btrfs_key_objectid(struct btrfs_disk_key *disk) |
| 240 | { |
| 241 | return le64_to_cpu(disk->objectid); |
| 242 | } |
| 243 | |
| 244 | static inline void btrfs_set_key_objectid(struct btrfs_disk_key *disk, |
| 245 | u64 val) |
| 246 | { |
| 247 | disk->objectid = cpu_to_le64(val); |
| 248 | } |
| 249 | |
| 250 | static inline u64 btrfs_key_offset(struct btrfs_disk_key *disk) |
| 251 | { |
| 252 | return le64_to_cpu(disk->offset); |
| 253 | } |
| 254 | |
| 255 | static inline void btrfs_set_key_offset(struct btrfs_disk_key *disk, |
| 256 | u64 val) |
| 257 | { |
| 258 | disk->offset = cpu_to_le64(val); |
| 259 | } |
| 260 | |
| 261 | static inline u32 btrfs_key_flags(struct btrfs_disk_key *disk) |
| 262 | { |
| 263 | return le32_to_cpu(disk->flags); |
| 264 | } |
| 265 | |
| 266 | static inline void btrfs_set_key_flags(struct btrfs_disk_key *disk, |
| 267 | u32 val) |
| 268 | { |
| 269 | disk->flags = cpu_to_le32(val); |
| 270 | } |
| 271 | |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 272 | static inline u64 btrfs_header_blocknr(struct btrfs_header *h) |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 273 | { |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 274 | return le64_to_cpu(h->blocknr); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 275 | } |
| 276 | |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 277 | 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] | 278 | { |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 279 | h->blocknr = cpu_to_le64(blocknr); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 280 | } |
| 281 | |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 282 | static inline u64 btrfs_header_parentid(struct btrfs_header *h) |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 283 | { |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 284 | return le64_to_cpu(h->parentid); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 285 | } |
| 286 | |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 287 | static inline void btrfs_set_header_parentid(struct btrfs_header *h, |
| 288 | u64 parentid) |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 289 | { |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 290 | h->parentid = cpu_to_le64(parentid); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 291 | } |
| 292 | |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 293 | static inline u16 btrfs_header_nritems(struct btrfs_header *h) |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 294 | { |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 295 | return le16_to_cpu(h->nritems); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 296 | } |
| 297 | |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 298 | 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] | 299 | { |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 300 | h->nritems = cpu_to_le16(val); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 301 | } |
| 302 | |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 303 | static inline u16 btrfs_header_flags(struct btrfs_header *h) |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 304 | { |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 305 | return le16_to_cpu(h->flags); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 306 | } |
| 307 | |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 308 | 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] | 309 | { |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 310 | h->flags = cpu_to_le16(val); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 311 | } |
| 312 | |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 313 | static inline int btrfs_header_level(struct btrfs_header *h) |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 314 | { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 315 | return btrfs_header_flags(h) & (BTRFS_MAX_LEVEL - 1); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 316 | } |
| 317 | |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 318 | 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] | 319 | { |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 320 | u16 flags; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 321 | BUG_ON(level > BTRFS_MAX_LEVEL); |
| 322 | flags = btrfs_header_flags(h) & ~(BTRFS_MAX_LEVEL - 1); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 323 | btrfs_set_header_flags(h, flags | level); |
| 324 | } |
| 325 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 326 | static inline int btrfs_is_leaf(struct btrfs_node *n) |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 327 | { |
| 328 | return (btrfs_header_level(&n->header) == 0); |
| 329 | } |
| 330 | |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 331 | static inline u64 btrfs_root_blocknr(struct btrfs_root_item *item) |
| 332 | { |
| 333 | return le64_to_cpu(item->blocknr); |
| 334 | } |
| 335 | |
| 336 | static inline void btrfs_set_root_blocknr(struct btrfs_root_item *item, u64 val) |
| 337 | { |
| 338 | item->blocknr = cpu_to_le64(val); |
| 339 | } |
| 340 | |
| 341 | static inline u32 btrfs_root_refs(struct btrfs_root_item *item) |
| 342 | { |
| 343 | return le32_to_cpu(item->refs); |
| 344 | } |
| 345 | |
| 346 | static inline void btrfs_set_root_refs(struct btrfs_root_item *item, u32 val) |
| 347 | { |
| 348 | item->refs = cpu_to_le32(val); |
| 349 | } |
| 350 | |
| 351 | static inline u64 btrfs_super_blocknr(struct btrfs_super_block *s) |
| 352 | { |
| 353 | return le64_to_cpu(s->blocknr); |
| 354 | } |
| 355 | |
| 356 | static inline void btrfs_set_super_blocknr(struct btrfs_super_block *s, u64 val) |
| 357 | { |
| 358 | s->blocknr = cpu_to_le64(val); |
| 359 | } |
| 360 | |
| 361 | static inline u64 btrfs_super_root(struct btrfs_super_block *s) |
| 362 | { |
| 363 | return le64_to_cpu(s->root); |
| 364 | } |
| 365 | |
| 366 | static inline void btrfs_set_super_root(struct btrfs_super_block *s, u64 val) |
| 367 | { |
| 368 | s->root = cpu_to_le64(val); |
| 369 | } |
| 370 | |
| 371 | static inline u64 btrfs_super_total_blocks(struct btrfs_super_block *s) |
| 372 | { |
| 373 | return le64_to_cpu(s->total_blocks); |
| 374 | } |
| 375 | |
| 376 | static inline void btrfs_set_super_total_blocks(struct btrfs_super_block *s, |
| 377 | u64 val) |
| 378 | { |
| 379 | s->total_blocks = cpu_to_le64(val); |
| 380 | } |
| 381 | |
| 382 | static inline u64 btrfs_super_blocks_used(struct btrfs_super_block *s) |
| 383 | { |
| 384 | return le64_to_cpu(s->blocks_used); |
| 385 | } |
| 386 | |
| 387 | static inline void btrfs_set_super_blocks_used(struct btrfs_super_block *s, |
| 388 | u64 val) |
| 389 | { |
| 390 | s->blocks_used = cpu_to_le64(val); |
| 391 | } |
| 392 | |
| 393 | static inline u16 btrfs_super_blocksize(struct btrfs_super_block *s) |
| 394 | { |
| 395 | return le16_to_cpu(s->blocksize); |
| 396 | } |
| 397 | |
| 398 | static inline void btrfs_set_super_blocksize(struct btrfs_super_block *s, |
| 399 | u16 val) |
| 400 | { |
| 401 | s->blocksize = cpu_to_le16(val); |
| 402 | } |
| 403 | |
Chris Mason | 4beb1b8 | 2007-03-14 10:31:29 -0400 | [diff] [blame^] | 404 | /* helper function to cast into the data area of the leaf. */ |
| 405 | #define btrfs_item_ptr(leaf, slot, type) \ |
| 406 | ((type *)((leaf)->data + btrfs_item_offset((leaf)->items + (slot)))) |
| 407 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 408 | struct btrfs_buffer *btrfs_alloc_free_block(struct btrfs_root *root); |
| 409 | int btrfs_inc_ref(struct btrfs_root *root, struct btrfs_buffer *buf); |
| 410 | int btrfs_free_extent(struct btrfs_root *root, u64 blocknr, u64 num_blocks); |
| 411 | int btrfs_search_slot(struct btrfs_root *root, struct btrfs_key *key, |
| 412 | struct btrfs_path *p, int ins_len, int cow); |
| 413 | void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p); |
| 414 | void btrfs_init_path(struct btrfs_path *p); |
| 415 | int btrfs_del_item(struct btrfs_root *root, struct btrfs_path *path); |
| 416 | int btrfs_insert_item(struct btrfs_root *root, struct btrfs_key *key, |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 417 | void *data, int data_size); |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 418 | int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path); |
| 419 | int btrfs_leaf_free_space(struct btrfs_leaf *leaf); |
| 420 | int btrfs_drop_snapshot(struct btrfs_root *root, struct btrfs_buffer *snap); |
| 421 | int btrfs_finish_extent_commit(struct btrfs_root *root); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 422 | int btrfs_del_root(struct btrfs_root *root, struct btrfs_key *key); |
| 423 | int btrfs_insert_root(struct btrfs_root *root, struct btrfs_key *key, |
| 424 | struct btrfs_root_item *item); |
| 425 | int btrfs_update_root(struct btrfs_root *root, struct btrfs_key *key, |
| 426 | struct btrfs_root_item *item); |
| 427 | int btrfs_find_last_root(struct btrfs_root *root, u64 objectid, |
| 428 | struct btrfs_root_item *item, struct btrfs_key *key); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 429 | #endif |