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 | |
Chris Mason | 1e1d270 | 2007-03-15 19:03:33 -0400 | [diff] [blame^] | 135 | struct btrfs_inode_timespec { |
| 136 | __le32 sec; |
| 137 | __le32 nsec; |
| 138 | } __attribute__ ((__packed__)); |
| 139 | |
| 140 | /* |
| 141 | * there is no padding here on purpose. If you want to extent the inode, |
| 142 | * make a new item type |
| 143 | */ |
| 144 | struct btrfs_inode_item { |
| 145 | __le64 generation; |
| 146 | __le64 size; |
| 147 | __le64 nblocks; |
| 148 | __le32 nlink; |
| 149 | __le32 uid; |
| 150 | __le32 gid; |
| 151 | __le32 mode; |
| 152 | __le32 rdev; |
| 153 | __le16 flags; |
| 154 | __le16 compat_flags; |
| 155 | struct btrfs_inode_timespec atime; |
| 156 | struct btrfs_inode_timespec ctime; |
| 157 | struct btrfs_inode_timespec mtime; |
| 158 | struct btrfs_inode_timespec otime; |
| 159 | } __attribute__ ((__packed__)); |
| 160 | |
| 161 | /* inline data is just a blob of bytes */ |
| 162 | struct btrfs_inline_data_item { |
| 163 | u8 data; |
| 164 | } __attribute__ ((__packed__)); |
| 165 | |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 166 | struct btrfs_dir_item { |
| 167 | __le64 objectid; |
| 168 | __le16 flags; |
| 169 | u8 type; |
| 170 | } __attribute__ ((__packed__)); |
| 171 | |
| 172 | struct btrfs_root_item { |
| 173 | __le64 blocknr; |
| 174 | __le32 flags; |
| 175 | __le64 block_limit; |
| 176 | __le64 blocks_used; |
| 177 | __le32 refs; |
| 178 | }; |
| 179 | |
| 180 | /* |
| 181 | * in ram representation of the tree. extent_root is used for all allocations |
| 182 | * and for the extent tree extent_root root. current_insert is used |
| 183 | * only for the extent tree. |
| 184 | */ |
| 185 | struct btrfs_root { |
| 186 | struct btrfs_buffer *node; |
| 187 | struct btrfs_buffer *commit_root; |
| 188 | struct btrfs_root *extent_root; |
| 189 | struct btrfs_root *tree_root; |
| 190 | struct btrfs_key current_insert; |
| 191 | struct btrfs_key last_insert; |
| 192 | int fp; |
| 193 | struct radix_tree_root cache_radix; |
| 194 | struct radix_tree_root pinned_radix; |
| 195 | struct list_head trans; |
| 196 | struct list_head cache; |
| 197 | int cache_size; |
| 198 | int ref_cows; |
| 199 | struct btrfs_root_item root_item; |
| 200 | struct btrfs_key root_key; |
| 201 | u32 blocksize; |
| 202 | }; |
| 203 | |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 204 | /* the lower bits in the key flags defines the item type */ |
| 205 | #define BTRFS_KEY_TYPE_MAX 256 |
| 206 | #define BTRFS_KEY_TYPE_MASK (BTRFS_KEY_TYPE_MAX - 1) |
Chris Mason | 1e1d270 | 2007-03-15 19:03:33 -0400 | [diff] [blame^] | 207 | |
| 208 | /* |
| 209 | * inode items have the data typically returned from stat and store other |
| 210 | * info about object characteristics. There is one for every file and dir in |
| 211 | * the FS |
| 212 | */ |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 213 | #define BTRFS_INODE_ITEM_KEY 1 |
Chris Mason | 1e1d270 | 2007-03-15 19:03:33 -0400 | [diff] [blame^] | 214 | |
| 215 | /* |
| 216 | * dir items are the name -> inode pointers in a directory. There is one |
| 217 | * for every name in a directory. |
| 218 | */ |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 219 | #define BTRFS_DIR_ITEM_KEY 2 |
Chris Mason | 1e1d270 | 2007-03-15 19:03:33 -0400 | [diff] [blame^] | 220 | /* |
| 221 | * inline data is file data that fits in the btree. |
| 222 | */ |
| 223 | #define BTRFS_INLINE_DATA_KEY 3 |
| 224 | /* |
| 225 | * extent data is for data that can't fit in the btree. It points to |
| 226 | * a (hopefully) huge chunk of disk |
| 227 | */ |
| 228 | #define BTRFS_EXTENT_DATA_KEY 4 |
| 229 | /* |
| 230 | * root items point to tree roots. There are typically in the root |
| 231 | * tree used by the super block to find all the other trees |
| 232 | */ |
| 233 | #define BTRFS_ROOT_ITEM_KEY 5 |
| 234 | /* |
| 235 | * extent items are in the extent map tree. These record which blocks |
| 236 | * are used, and how many references there are to each block |
| 237 | */ |
| 238 | #define BTRFS_EXTENT_ITEM_KEY 6 |
| 239 | /* |
| 240 | * string items are for debugging. They just store a short string of |
| 241 | * data in the FS |
| 242 | */ |
| 243 | #define BTRFS_STRING_ITEM_KEY 7 |
| 244 | |
| 245 | static inline u64 btrfs_inode_generation(struct btrfs_inode_item *i) |
| 246 | { |
| 247 | return le64_to_cpu(i->generation); |
| 248 | } |
| 249 | |
| 250 | static inline void btrfs_set_inode_generation(struct btrfs_inode_item *i, |
| 251 | u64 val) |
| 252 | { |
| 253 | i->generation = cpu_to_le64(val); |
| 254 | } |
| 255 | |
| 256 | static inline u64 btrfs_inode_size(struct btrfs_inode_item *i) |
| 257 | { |
| 258 | return le64_to_cpu(i->size); |
| 259 | } |
| 260 | |
| 261 | static inline void btrfs_set_inode_size(struct btrfs_inode_item *i, u64 val) |
| 262 | { |
| 263 | i->size = cpu_to_le64(val); |
| 264 | } |
| 265 | |
| 266 | static inline u64 btrfs_inode_nblocks(struct btrfs_inode_item *i) |
| 267 | { |
| 268 | return le64_to_cpu(i->nblocks); |
| 269 | } |
| 270 | |
| 271 | static inline void btrfs_set_inode_nblocks(struct btrfs_inode_item *i, u64 val) |
| 272 | { |
| 273 | i->nblocks = cpu_to_le64(val); |
| 274 | } |
| 275 | |
| 276 | static inline u32 btrfs_inode_nlink(struct btrfs_inode_item *i) |
| 277 | { |
| 278 | return le32_to_cpu(i->nlink); |
| 279 | } |
| 280 | |
| 281 | static inline void btrfs_set_inode_nlink(struct btrfs_inode_item *i, u32 val) |
| 282 | { |
| 283 | i->nlink = cpu_to_le32(val); |
| 284 | } |
| 285 | |
| 286 | static inline u32 btrfs_inode_uid(struct btrfs_inode_item *i) |
| 287 | { |
| 288 | return le32_to_cpu(i->uid); |
| 289 | } |
| 290 | |
| 291 | static inline void btrfs_set_inode_uid(struct btrfs_inode_item *i, u32 val) |
| 292 | { |
| 293 | i->uid = cpu_to_le32(val); |
| 294 | } |
| 295 | |
| 296 | static inline u32 btrfs_inode_gid(struct btrfs_inode_item *i) |
| 297 | { |
| 298 | return le32_to_cpu(i->gid); |
| 299 | } |
| 300 | |
| 301 | static inline void btrfs_set_inode_gid(struct btrfs_inode_item *i, u32 val) |
| 302 | { |
| 303 | i->gid = cpu_to_le32(val); |
| 304 | } |
| 305 | |
| 306 | static inline u32 btrfs_inode_mode(struct btrfs_inode_item *i) |
| 307 | { |
| 308 | return le32_to_cpu(i->mode); |
| 309 | } |
| 310 | |
| 311 | static inline void btrfs_set_inode_mode(struct btrfs_inode_item *i, u32 val) |
| 312 | { |
| 313 | i->mode = cpu_to_le32(val); |
| 314 | } |
| 315 | |
| 316 | static inline u32 btrfs_inode_rdev(struct btrfs_inode_item *i) |
| 317 | { |
| 318 | return le32_to_cpu(i->rdev); |
| 319 | } |
| 320 | |
| 321 | static inline void btrfs_set_inode_rdev(struct btrfs_inode_item *i, u32 val) |
| 322 | { |
| 323 | i->rdev = cpu_to_le32(val); |
| 324 | } |
| 325 | |
| 326 | static inline u16 btrfs_inode_flags(struct btrfs_inode_item *i) |
| 327 | { |
| 328 | return le16_to_cpu(i->flags); |
| 329 | } |
| 330 | |
| 331 | static inline void btrfs_set_inode_flags(struct btrfs_inode_item *i, u16 val) |
| 332 | { |
| 333 | i->flags = cpu_to_le16(val); |
| 334 | } |
| 335 | |
| 336 | static inline u16 btrfs_inode_compat_flags(struct btrfs_inode_item *i) |
| 337 | { |
| 338 | return le16_to_cpu(i->compat_flags); |
| 339 | } |
| 340 | |
| 341 | static inline void btrfs_set_inode_compat_flags(struct btrfs_inode_item *i, |
| 342 | u16 val) |
| 343 | { |
| 344 | i->compat_flags = cpu_to_le16(val); |
| 345 | } |
| 346 | |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 347 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 348 | static inline u64 btrfs_extent_owner(struct btrfs_extent_item *ei) |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 349 | { |
| 350 | return le64_to_cpu(ei->owner); |
| 351 | } |
| 352 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 353 | 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] | 354 | { |
| 355 | ei->owner = cpu_to_le64(val); |
| 356 | } |
| 357 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 358 | static inline u32 btrfs_extent_refs(struct btrfs_extent_item *ei) |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 359 | { |
| 360 | return le32_to_cpu(ei->refs); |
| 361 | } |
| 362 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 363 | 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] | 364 | { |
| 365 | ei->refs = cpu_to_le32(val); |
| 366 | } |
| 367 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 368 | static inline u64 btrfs_node_blockptr(struct btrfs_node *n, int nr) |
Chris Mason | 1d4f8a0 | 2007-03-13 09:28:32 -0400 | [diff] [blame] | 369 | { |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 370 | return le64_to_cpu(n->ptrs[nr].blockptr); |
Chris Mason | 1d4f8a0 | 2007-03-13 09:28:32 -0400 | [diff] [blame] | 371 | } |
| 372 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 373 | static inline void btrfs_set_node_blockptr(struct btrfs_node *n, int nr, |
| 374 | u64 val) |
Chris Mason | 1d4f8a0 | 2007-03-13 09:28:32 -0400 | [diff] [blame] | 375 | { |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 376 | n->ptrs[nr].blockptr = cpu_to_le64(val); |
Chris Mason | 1d4f8a0 | 2007-03-13 09:28:32 -0400 | [diff] [blame] | 377 | } |
| 378 | |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 379 | static inline u32 btrfs_item_offset(struct btrfs_item *item) |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 380 | { |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 381 | return le32_to_cpu(item->offset); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 382 | } |
| 383 | |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 384 | 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] | 385 | { |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 386 | item->offset = cpu_to_le32(val); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 387 | } |
| 388 | |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 389 | static inline u32 btrfs_item_end(struct btrfs_item *item) |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 390 | { |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 391 | return le32_to_cpu(item->offset) + le16_to_cpu(item->size); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | static inline u16 btrfs_item_size(struct btrfs_item *item) |
| 395 | { |
| 396 | return le16_to_cpu(item->size); |
| 397 | } |
| 398 | |
| 399 | static inline void btrfs_set_item_size(struct btrfs_item *item, u16 val) |
| 400 | { |
| 401 | item->size = cpu_to_le16(val); |
| 402 | } |
| 403 | |
Chris Mason | 1d4f640 | 2007-03-15 15:18:43 -0400 | [diff] [blame] | 404 | static inline u64 btrfs_dir_objectid(struct btrfs_dir_item *d) |
| 405 | { |
| 406 | return le64_to_cpu(d->objectid); |
| 407 | } |
| 408 | |
| 409 | static inline void btrfs_set_dir_objectid(struct btrfs_dir_item *d, u64 val) |
| 410 | { |
| 411 | d->objectid = cpu_to_le64(val); |
| 412 | } |
| 413 | |
| 414 | static inline u16 btrfs_dir_flags(struct btrfs_dir_item *d) |
| 415 | { |
| 416 | return le16_to_cpu(d->flags); |
| 417 | } |
| 418 | |
| 419 | static inline void btrfs_set_dir_flags(struct btrfs_dir_item *d, u16 val) |
| 420 | { |
| 421 | d->flags = cpu_to_le16(val); |
| 422 | } |
| 423 | |
| 424 | static inline u8 btrfs_dir_type(struct btrfs_dir_item *d) |
| 425 | { |
| 426 | return d->type; |
| 427 | } |
| 428 | |
| 429 | static inline void btrfs_set_dir_type(struct btrfs_dir_item *d, u8 val) |
| 430 | { |
| 431 | d->type = val; |
| 432 | } |
| 433 | |
| 434 | static inline u32 btrfs_dir_name_len(struct btrfs_item *i) |
| 435 | { |
| 436 | return btrfs_item_size(i) - sizeof(struct btrfs_dir_item); |
| 437 | } |
| 438 | |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 439 | static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu, |
| 440 | struct btrfs_disk_key *disk) |
| 441 | { |
| 442 | cpu->offset = le64_to_cpu(disk->offset); |
| 443 | cpu->flags = le32_to_cpu(disk->flags); |
| 444 | cpu->objectid = le64_to_cpu(disk->objectid); |
| 445 | } |
| 446 | |
| 447 | static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk, |
| 448 | struct btrfs_key *cpu) |
| 449 | { |
| 450 | disk->offset = cpu_to_le64(cpu->offset); |
| 451 | disk->flags = cpu_to_le32(cpu->flags); |
| 452 | disk->objectid = cpu_to_le64(cpu->objectid); |
| 453 | } |
| 454 | |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 455 | static inline u64 btrfs_disk_key_objectid(struct btrfs_disk_key *disk) |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 456 | { |
| 457 | return le64_to_cpu(disk->objectid); |
| 458 | } |
| 459 | |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 460 | static inline void btrfs_set_disk_key_objectid(struct btrfs_disk_key *disk, |
| 461 | u64 val) |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 462 | { |
| 463 | disk->objectid = cpu_to_le64(val); |
| 464 | } |
| 465 | |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 466 | static inline u64 btrfs_disk_key_offset(struct btrfs_disk_key *disk) |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 467 | { |
| 468 | return le64_to_cpu(disk->offset); |
| 469 | } |
| 470 | |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 471 | static inline void btrfs_set_disk_key_offset(struct btrfs_disk_key *disk, |
| 472 | u64 val) |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 473 | { |
| 474 | disk->offset = cpu_to_le64(val); |
| 475 | } |
| 476 | |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 477 | static inline u32 btrfs_disk_key_flags(struct btrfs_disk_key *disk) |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 478 | { |
| 479 | return le32_to_cpu(disk->flags); |
| 480 | } |
| 481 | |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 482 | static inline void btrfs_set_disk_key_flags(struct btrfs_disk_key *disk, |
| 483 | u32 val) |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 484 | { |
| 485 | disk->flags = cpu_to_le32(val); |
| 486 | } |
| 487 | |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 488 | static inline u32 btrfs_key_type(struct btrfs_key *key) |
| 489 | { |
| 490 | return key->flags & BTRFS_KEY_TYPE_MASK; |
| 491 | } |
| 492 | |
| 493 | static inline u32 btrfs_disk_key_type(struct btrfs_disk_key *key) |
| 494 | { |
| 495 | return le32_to_cpu(key->flags) & BTRFS_KEY_TYPE_MASK; |
| 496 | } |
| 497 | |
| 498 | static inline void btrfs_set_key_type(struct btrfs_key *key, u32 type) |
| 499 | { |
| 500 | BUG_ON(type >= BTRFS_KEY_TYPE_MAX); |
| 501 | key->flags = (key->flags & ~((u64)BTRFS_KEY_TYPE_MASK)) | type; |
| 502 | } |
| 503 | |
| 504 | static inline void btrfs_set_disk_key_type(struct btrfs_disk_key *key, u32 type) |
| 505 | { |
| 506 | u32 flags = btrfs_disk_key_flags(key); |
| 507 | BUG_ON(type >= BTRFS_KEY_TYPE_MAX); |
| 508 | flags = (flags & ~((u64)BTRFS_KEY_TYPE_MASK)) | type; |
| 509 | btrfs_set_disk_key_flags(key, flags); |
| 510 | } |
| 511 | |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 512 | static inline u64 btrfs_header_blocknr(struct btrfs_header *h) |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 513 | { |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 514 | return le64_to_cpu(h->blocknr); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 515 | } |
| 516 | |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 517 | 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] | 518 | { |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 519 | h->blocknr = cpu_to_le64(blocknr); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 520 | } |
| 521 | |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 522 | static inline u64 btrfs_header_parentid(struct btrfs_header *h) |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 523 | { |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 524 | return le64_to_cpu(h->parentid); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 525 | } |
| 526 | |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 527 | static inline void btrfs_set_header_parentid(struct btrfs_header *h, |
| 528 | u64 parentid) |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 529 | { |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 530 | h->parentid = cpu_to_le64(parentid); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 531 | } |
| 532 | |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 533 | static inline u16 btrfs_header_nritems(struct btrfs_header *h) |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 534 | { |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 535 | return le16_to_cpu(h->nritems); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 536 | } |
| 537 | |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 538 | 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] | 539 | { |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 540 | h->nritems = cpu_to_le16(val); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 541 | } |
| 542 | |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 543 | static inline u16 btrfs_header_flags(struct btrfs_header *h) |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 544 | { |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 545 | return le16_to_cpu(h->flags); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 546 | } |
| 547 | |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 548 | 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] | 549 | { |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 550 | h->flags = cpu_to_le16(val); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 551 | } |
| 552 | |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 553 | static inline int btrfs_header_level(struct btrfs_header *h) |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 554 | { |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 555 | return btrfs_header_flags(h) & (BTRFS_MAX_LEVEL - 1); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 556 | } |
| 557 | |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 558 | 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] | 559 | { |
Chris Mason | bb492bb | 2007-03-12 12:29:44 -0400 | [diff] [blame] | 560 | u16 flags; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 561 | BUG_ON(level > BTRFS_MAX_LEVEL); |
| 562 | flags = btrfs_header_flags(h) & ~(BTRFS_MAX_LEVEL - 1); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 563 | btrfs_set_header_flags(h, flags | level); |
| 564 | } |
| 565 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 566 | static inline int btrfs_is_leaf(struct btrfs_node *n) |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 567 | { |
| 568 | return (btrfs_header_level(&n->header) == 0); |
| 569 | } |
| 570 | |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 571 | static inline u64 btrfs_root_blocknr(struct btrfs_root_item *item) |
| 572 | { |
| 573 | return le64_to_cpu(item->blocknr); |
| 574 | } |
| 575 | |
| 576 | static inline void btrfs_set_root_blocknr(struct btrfs_root_item *item, u64 val) |
| 577 | { |
| 578 | item->blocknr = cpu_to_le64(val); |
| 579 | } |
| 580 | |
| 581 | static inline u32 btrfs_root_refs(struct btrfs_root_item *item) |
| 582 | { |
| 583 | return le32_to_cpu(item->refs); |
| 584 | } |
| 585 | |
| 586 | static inline void btrfs_set_root_refs(struct btrfs_root_item *item, u32 val) |
| 587 | { |
| 588 | item->refs = cpu_to_le32(val); |
| 589 | } |
| 590 | |
| 591 | static inline u64 btrfs_super_blocknr(struct btrfs_super_block *s) |
| 592 | { |
| 593 | return le64_to_cpu(s->blocknr); |
| 594 | } |
| 595 | |
| 596 | static inline void btrfs_set_super_blocknr(struct btrfs_super_block *s, u64 val) |
| 597 | { |
| 598 | s->blocknr = cpu_to_le64(val); |
| 599 | } |
| 600 | |
| 601 | static inline u64 btrfs_super_root(struct btrfs_super_block *s) |
| 602 | { |
| 603 | return le64_to_cpu(s->root); |
| 604 | } |
| 605 | |
| 606 | static inline void btrfs_set_super_root(struct btrfs_super_block *s, u64 val) |
| 607 | { |
| 608 | s->root = cpu_to_le64(val); |
| 609 | } |
| 610 | |
| 611 | static inline u64 btrfs_super_total_blocks(struct btrfs_super_block *s) |
| 612 | { |
| 613 | return le64_to_cpu(s->total_blocks); |
| 614 | } |
| 615 | |
| 616 | static inline void btrfs_set_super_total_blocks(struct btrfs_super_block *s, |
| 617 | u64 val) |
| 618 | { |
| 619 | s->total_blocks = cpu_to_le64(val); |
| 620 | } |
| 621 | |
| 622 | static inline u64 btrfs_super_blocks_used(struct btrfs_super_block *s) |
| 623 | { |
| 624 | return le64_to_cpu(s->blocks_used); |
| 625 | } |
| 626 | |
| 627 | static inline void btrfs_set_super_blocks_used(struct btrfs_super_block *s, |
| 628 | u64 val) |
| 629 | { |
| 630 | s->blocks_used = cpu_to_le64(val); |
| 631 | } |
| 632 | |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 633 | static inline u32 btrfs_super_blocksize(struct btrfs_super_block *s) |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 634 | { |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 635 | return le32_to_cpu(s->blocksize); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | static inline void btrfs_set_super_blocksize(struct btrfs_super_block *s, |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 639 | u32 val) |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 640 | { |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 641 | s->blocksize = cpu_to_le32(val); |
| 642 | } |
| 643 | |
| 644 | static inline u8 *btrfs_leaf_data(struct btrfs_leaf *l) |
| 645 | { |
| 646 | return (u8 *)l->items; |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 647 | } |
Chris Mason | 4beb1b8 | 2007-03-14 10:31:29 -0400 | [diff] [blame] | 648 | /* helper function to cast into the data area of the leaf. */ |
| 649 | #define btrfs_item_ptr(leaf, slot, type) \ |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 650 | ((type *)(btrfs_leaf_data(leaf) + \ |
| 651 | btrfs_item_offset((leaf)->items + (slot)))) |
Chris Mason | 4beb1b8 | 2007-03-14 10:31:29 -0400 | [diff] [blame] | 652 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 653 | struct btrfs_buffer *btrfs_alloc_free_block(struct btrfs_root *root); |
| 654 | int btrfs_inc_ref(struct btrfs_root *root, struct btrfs_buffer *buf); |
| 655 | int btrfs_free_extent(struct btrfs_root *root, u64 blocknr, u64 num_blocks); |
| 656 | int btrfs_search_slot(struct btrfs_root *root, struct btrfs_key *key, |
| 657 | struct btrfs_path *p, int ins_len, int cow); |
| 658 | void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p); |
| 659 | void btrfs_init_path(struct btrfs_path *p); |
| 660 | int btrfs_del_item(struct btrfs_root *root, struct btrfs_path *path); |
| 661 | int btrfs_insert_item(struct btrfs_root *root, struct btrfs_key *key, |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 662 | void *data, u32 data_size); |
| 663 | int btrfs_insert_empty_item(struct btrfs_root *root, struct btrfs_path *path, |
| 664 | struct btrfs_key *cpu_key, u32 data_size); |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 665 | int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path); |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 666 | 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] | 667 | int btrfs_drop_snapshot(struct btrfs_root *root, struct btrfs_buffer *snap); |
| 668 | int btrfs_finish_extent_commit(struct btrfs_root *root); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 669 | int btrfs_del_root(struct btrfs_root *root, struct btrfs_key *key); |
| 670 | int btrfs_insert_root(struct btrfs_root *root, struct btrfs_key *key, |
| 671 | struct btrfs_root_item *item); |
| 672 | int btrfs_update_root(struct btrfs_root *root, struct btrfs_key *key, |
| 673 | struct btrfs_root_item *item); |
| 674 | int btrfs_find_last_root(struct btrfs_root *root, u64 objectid, |
| 675 | struct btrfs_root_item *item, struct btrfs_key *key); |
Chris Mason | 1d4f640 | 2007-03-15 15:18:43 -0400 | [diff] [blame] | 676 | int btrfs_insert_dir_item(struct btrfs_root *root, char *name, int name_len, |
| 677 | u64 dir, u64 objectid, u8 type); |
| 678 | int btrfs_lookup_dir_item(struct btrfs_root *root, struct btrfs_path *path, |
| 679 | u64 dir, char *name, int name_len, int mod); |
| 680 | int btrfs_match_dir_item_name(struct btrfs_root *root, struct btrfs_path *path, |
| 681 | char *name, int name_len); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 682 | #endif |