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