| 1 | #ifndef __BTRFS__ |
| 2 | #define __BTRFS__ |
| 3 | |
| 4 | #include <linux/fs.h> |
| 5 | #include "bit-radix.h" |
| 6 | |
| 7 | struct btrfs_trans_handle; |
| 8 | struct btrfs_transaction; |
| 9 | |
| 10 | #define BTRFS_MAGIC "_BtRfS_M" |
| 11 | |
| 12 | #define BTRFS_ROOT_TREE_OBJECTID 1ULL |
| 13 | #define BTRFS_EXTENT_TREE_OBJECTID 2ULL |
| 14 | #define BTRFS_INODE_MAP_OBJECTID 3ULL |
| 15 | #define BTRFS_FS_TREE_OBJECTID 4ULL |
| 16 | #define BTRFS_FIRST_FREE_OBJECTID 5ULL |
| 17 | |
| 18 | /* |
| 19 | * we can actually store much bigger names, but lets not confuse the rest |
| 20 | * of linux |
| 21 | */ |
| 22 | #define BTRFS_NAME_LEN 255 |
| 23 | |
| 24 | /* |
| 25 | * the key defines the order in the tree, and so it also defines (optimal) |
| 26 | * block layout. objectid corresonds to the inode number. The flags |
| 27 | * tells us things about the object, and is a kind of stream selector. |
| 28 | * so for a given inode, keys with flags of 1 might refer to the inode |
| 29 | * data, flags of 2 may point to file data in the btree and flags == 3 |
| 30 | * may point to extents. |
| 31 | * |
| 32 | * offset is the starting byte offset for this key in the stream. |
| 33 | * |
| 34 | * btrfs_disk_key is in disk byte order. struct btrfs_key is always |
| 35 | * in cpu native order. Otherwise they are identical and their sizes |
| 36 | * should be the same (ie both packed) |
| 37 | */ |
| 38 | struct btrfs_disk_key { |
| 39 | __le64 objectid; |
| 40 | __le32 flags; |
| 41 | __le64 offset; |
| 42 | } __attribute__ ((__packed__)); |
| 43 | |
| 44 | struct btrfs_key { |
| 45 | u64 objectid; |
| 46 | u32 flags; |
| 47 | u64 offset; |
| 48 | } __attribute__ ((__packed__)); |
| 49 | |
| 50 | /* |
| 51 | * every tree block (leaf or node) starts with this header. |
| 52 | */ |
| 53 | struct btrfs_header { |
| 54 | u8 fsid[16]; /* FS specific uuid */ |
| 55 | __le64 blocknr; /* which block this node is supposed to live in */ |
| 56 | __le64 generation; |
| 57 | __le64 parentid; /* objectid of the tree root */ |
| 58 | __le32 csum; |
| 59 | __le32 ham; |
| 60 | __le16 nritems; |
| 61 | __le16 flags; |
| 62 | /* generation flags to be added */ |
| 63 | } __attribute__ ((__packed__)); |
| 64 | |
| 65 | #define BTRFS_MAX_LEVEL 8 |
| 66 | #define BTRFS_NODEPTRS_PER_BLOCK(r) (((r)->blocksize - \ |
| 67 | sizeof(struct btrfs_header)) / \ |
| 68 | (sizeof(struct btrfs_disk_key) + sizeof(u64))) |
| 69 | #define __BTRFS_LEAF_DATA_SIZE(bs) ((bs) - sizeof(struct btrfs_header)) |
| 70 | #define BTRFS_LEAF_DATA_SIZE(r) (__BTRFS_LEAF_DATA_SIZE(r->blocksize)) |
| 71 | |
| 72 | struct buffer_head; |
| 73 | /* |
| 74 | * the super block basically lists the main trees of the FS |
| 75 | * it currently lacks any block count etc etc |
| 76 | */ |
| 77 | struct btrfs_super_block { |
| 78 | u8 fsid[16]; /* FS specific uuid */ |
| 79 | __le64 blocknr; /* this block number */ |
| 80 | __le32 csum; |
| 81 | __le64 magic; |
| 82 | __le32 blocksize; |
| 83 | __le64 generation; |
| 84 | __le64 root; |
| 85 | __le64 total_blocks; |
| 86 | __le64 blocks_used; |
| 87 | __le64 root_dir_objectid; |
| 88 | } __attribute__ ((__packed__)); |
| 89 | |
| 90 | /* |
| 91 | * A leaf is full of items. offset and size tell us where to find |
| 92 | * the item in the leaf (relative to the start of the data area) |
| 93 | */ |
| 94 | struct btrfs_item { |
| 95 | struct btrfs_disk_key key; |
| 96 | __le32 offset; |
| 97 | __le16 size; |
| 98 | } __attribute__ ((__packed__)); |
| 99 | |
| 100 | /* |
| 101 | * leaves have an item area and a data area: |
| 102 | * [item0, item1....itemN] [free space] [dataN...data1, data0] |
| 103 | * |
| 104 | * The data is separate from the items to get the keys closer together |
| 105 | * during searches. |
| 106 | */ |
| 107 | struct btrfs_leaf { |
| 108 | struct btrfs_header header; |
| 109 | struct btrfs_item items[]; |
| 110 | } __attribute__ ((__packed__)); |
| 111 | |
| 112 | /* |
| 113 | * all non-leaf blocks are nodes, they hold only keys and pointers to |
| 114 | * other blocks |
| 115 | */ |
| 116 | struct btrfs_key_ptr { |
| 117 | struct btrfs_disk_key key; |
| 118 | __le64 blockptr; |
| 119 | } __attribute__ ((__packed__)); |
| 120 | |
| 121 | struct btrfs_node { |
| 122 | struct btrfs_header header; |
| 123 | struct btrfs_key_ptr ptrs[]; |
| 124 | } __attribute__ ((__packed__)); |
| 125 | |
| 126 | /* |
| 127 | * btrfs_paths remember the path taken from the root down to the leaf. |
| 128 | * level 0 is always the leaf, and nodes[1...BTRFS_MAX_LEVEL] will point |
| 129 | * to any other levels that are present. |
| 130 | * |
| 131 | * The slots array records the index of the item or block pointer |
| 132 | * used while walking the tree. |
| 133 | */ |
| 134 | struct btrfs_path { |
| 135 | struct buffer_head *nodes[BTRFS_MAX_LEVEL]; |
| 136 | int slots[BTRFS_MAX_LEVEL]; |
| 137 | }; |
| 138 | |
| 139 | /* |
| 140 | * items in the extent btree are used to record the objectid of the |
| 141 | * owner of the block and the number of references |
| 142 | */ |
| 143 | struct btrfs_extent_item { |
| 144 | __le32 refs; |
| 145 | __le64 owner; |
| 146 | } __attribute__ ((__packed__)); |
| 147 | |
| 148 | struct btrfs_inode_timespec { |
| 149 | __le32 sec; |
| 150 | __le32 nsec; |
| 151 | } __attribute__ ((__packed__)); |
| 152 | |
| 153 | /* |
| 154 | * there is no padding here on purpose. If you want to extent the inode, |
| 155 | * make a new item type |
| 156 | */ |
| 157 | struct btrfs_inode_item { |
| 158 | __le64 generation; |
| 159 | __le64 size; |
| 160 | __le64 nblocks; |
| 161 | __le32 nlink; |
| 162 | __le32 uid; |
| 163 | __le32 gid; |
| 164 | __le32 mode; |
| 165 | __le32 rdev; |
| 166 | __le16 flags; |
| 167 | __le16 compat_flags; |
| 168 | struct btrfs_inode_timespec atime; |
| 169 | struct btrfs_inode_timespec ctime; |
| 170 | struct btrfs_inode_timespec mtime; |
| 171 | struct btrfs_inode_timespec otime; |
| 172 | } __attribute__ ((__packed__)); |
| 173 | |
| 174 | /* inline data is just a blob of bytes */ |
| 175 | struct btrfs_inline_data_item { |
| 176 | u8 data; |
| 177 | } __attribute__ ((__packed__)); |
| 178 | |
| 179 | struct btrfs_dir_item { |
| 180 | __le64 objectid; |
| 181 | __le16 flags; |
| 182 | __le16 name_len; |
| 183 | u8 type; |
| 184 | } __attribute__ ((__packed__)); |
| 185 | |
| 186 | struct btrfs_root_item { |
| 187 | __le64 blocknr; |
| 188 | __le32 flags; |
| 189 | __le64 block_limit; |
| 190 | __le64 blocks_used; |
| 191 | __le32 refs; |
| 192 | } __attribute__ ((__packed__)); |
| 193 | |
| 194 | struct btrfs_file_extent_item { |
| 195 | /* |
| 196 | * disk space consumed by the extent, checksum blocks are included |
| 197 | * in these numbers |
| 198 | */ |
| 199 | __le64 disk_blocknr; |
| 200 | __le64 disk_num_blocks; |
| 201 | /* |
| 202 | * the logical offset in file blocks (no csums) |
| 203 | * this extent record is for. This allows a file extent to point |
| 204 | * into the middle of an existing extent on disk, sharing it |
| 205 | * between two snapshots (useful if some bytes in the middle of the |
| 206 | * extent have changed |
| 207 | */ |
| 208 | __le64 offset; |
| 209 | /* |
| 210 | * the logical number of file blocks (no csums included) |
| 211 | */ |
| 212 | __le64 num_blocks; |
| 213 | } __attribute__ ((__packed__)); |
| 214 | |
| 215 | struct btrfs_inode_map_item { |
| 216 | struct btrfs_disk_key key; |
| 217 | } __attribute__ ((__packed__)); |
| 218 | |
| 219 | struct btrfs_fs_info { |
| 220 | struct btrfs_root *fs_root; |
| 221 | struct btrfs_root *extent_root; |
| 222 | struct btrfs_root *tree_root; |
| 223 | struct btrfs_root *inode_root; |
| 224 | struct btrfs_key current_insert; |
| 225 | struct btrfs_key last_insert; |
| 226 | struct radix_tree_root pending_del_radix; |
| 227 | struct radix_tree_root pinned_radix; |
| 228 | u64 last_inode_alloc; |
| 229 | u64 last_inode_alloc_dirid; |
| 230 | u64 generation; |
| 231 | struct btrfs_transaction *running_transaction; |
| 232 | struct btrfs_super_block *disk_super; |
| 233 | struct buffer_head *sb_buffer; |
| 234 | struct super_block *sb; |
| 235 | struct mutex trans_mutex; |
| 236 | struct mutex fs_mutex; |
| 237 | }; |
| 238 | |
| 239 | /* |
| 240 | * in ram representation of the tree. extent_root is used for all allocations |
| 241 | * and for the extent tree extent_root root. current_insert is used |
| 242 | * only for the extent tree. |
| 243 | */ |
| 244 | struct btrfs_root { |
| 245 | struct buffer_head *node; |
| 246 | struct buffer_head *commit_root; |
| 247 | struct btrfs_root_item root_item; |
| 248 | struct btrfs_key root_key; |
| 249 | struct btrfs_fs_info *fs_info; |
| 250 | u32 blocksize; |
| 251 | int ref_cows; |
| 252 | u32 type; |
| 253 | }; |
| 254 | |
| 255 | /* the lower bits in the key flags defines the item type */ |
| 256 | #define BTRFS_KEY_TYPE_MAX 256 |
| 257 | #define BTRFS_KEY_TYPE_MASK (BTRFS_KEY_TYPE_MAX - 1) |
| 258 | |
| 259 | /* |
| 260 | * inode items have the data typically returned from stat and store other |
| 261 | * info about object characteristics. There is one for every file and dir in |
| 262 | * the FS |
| 263 | */ |
| 264 | #define BTRFS_INODE_ITEM_KEY 1 |
| 265 | |
| 266 | /* |
| 267 | * dir items are the name -> inode pointers in a directory. There is one |
| 268 | * for every name in a directory. |
| 269 | */ |
| 270 | #define BTRFS_DIR_ITEM_KEY 2 |
| 271 | /* |
| 272 | * inline data is file data that fits in the btree. |
| 273 | */ |
| 274 | #define BTRFS_INLINE_DATA_KEY 3 |
| 275 | /* |
| 276 | * extent data is for data that can't fit in the btree. It points to |
| 277 | * a (hopefully) huge chunk of disk |
| 278 | */ |
| 279 | #define BTRFS_EXTENT_DATA_KEY 4 |
| 280 | /* |
| 281 | * root items point to tree roots. There are typically in the root |
| 282 | * tree used by the super block to find all the other trees |
| 283 | */ |
| 284 | #define BTRFS_ROOT_ITEM_KEY 5 |
| 285 | /* |
| 286 | * extent items are in the extent map tree. These record which blocks |
| 287 | * are used, and how many references there are to each block |
| 288 | */ |
| 289 | #define BTRFS_EXTENT_ITEM_KEY 6 |
| 290 | |
| 291 | /* |
| 292 | * the inode map records which inode numbers are in use and where |
| 293 | * they actually live on disk |
| 294 | */ |
| 295 | #define BTRFS_INODE_MAP_ITEM_KEY 7 |
| 296 | /* |
| 297 | * string items are for debugging. They just store a short string of |
| 298 | * data in the FS |
| 299 | */ |
| 300 | #define BTRFS_STRING_ITEM_KEY 8 |
| 301 | |
| 302 | static inline u64 btrfs_inode_generation(struct btrfs_inode_item *i) |
| 303 | { |
| 304 | return le64_to_cpu(i->generation); |
| 305 | } |
| 306 | |
| 307 | static inline void btrfs_set_inode_generation(struct btrfs_inode_item *i, |
| 308 | u64 val) |
| 309 | { |
| 310 | i->generation = cpu_to_le64(val); |
| 311 | } |
| 312 | |
| 313 | static inline u64 btrfs_inode_size(struct btrfs_inode_item *i) |
| 314 | { |
| 315 | return le64_to_cpu(i->size); |
| 316 | } |
| 317 | |
| 318 | static inline void btrfs_set_inode_size(struct btrfs_inode_item *i, u64 val) |
| 319 | { |
| 320 | i->size = cpu_to_le64(val); |
| 321 | } |
| 322 | |
| 323 | static inline u64 btrfs_inode_nblocks(struct btrfs_inode_item *i) |
| 324 | { |
| 325 | return le64_to_cpu(i->nblocks); |
| 326 | } |
| 327 | |
| 328 | static inline void btrfs_set_inode_nblocks(struct btrfs_inode_item *i, u64 val) |
| 329 | { |
| 330 | i->nblocks = cpu_to_le64(val); |
| 331 | } |
| 332 | |
| 333 | static inline u32 btrfs_inode_nlink(struct btrfs_inode_item *i) |
| 334 | { |
| 335 | return le32_to_cpu(i->nlink); |
| 336 | } |
| 337 | |
| 338 | static inline void btrfs_set_inode_nlink(struct btrfs_inode_item *i, u32 val) |
| 339 | { |
| 340 | i->nlink = cpu_to_le32(val); |
| 341 | } |
| 342 | |
| 343 | static inline u32 btrfs_inode_uid(struct btrfs_inode_item *i) |
| 344 | { |
| 345 | return le32_to_cpu(i->uid); |
| 346 | } |
| 347 | |
| 348 | static inline void btrfs_set_inode_uid(struct btrfs_inode_item *i, u32 val) |
| 349 | { |
| 350 | i->uid = cpu_to_le32(val); |
| 351 | } |
| 352 | |
| 353 | static inline u32 btrfs_inode_gid(struct btrfs_inode_item *i) |
| 354 | { |
| 355 | return le32_to_cpu(i->gid); |
| 356 | } |
| 357 | |
| 358 | static inline void btrfs_set_inode_gid(struct btrfs_inode_item *i, u32 val) |
| 359 | { |
| 360 | i->gid = cpu_to_le32(val); |
| 361 | } |
| 362 | |
| 363 | static inline u32 btrfs_inode_mode(struct btrfs_inode_item *i) |
| 364 | { |
| 365 | return le32_to_cpu(i->mode); |
| 366 | } |
| 367 | |
| 368 | static inline void btrfs_set_inode_mode(struct btrfs_inode_item *i, u32 val) |
| 369 | { |
| 370 | i->mode = cpu_to_le32(val); |
| 371 | } |
| 372 | |
| 373 | static inline u32 btrfs_inode_rdev(struct btrfs_inode_item *i) |
| 374 | { |
| 375 | return le32_to_cpu(i->rdev); |
| 376 | } |
| 377 | |
| 378 | static inline void btrfs_set_inode_rdev(struct btrfs_inode_item *i, u32 val) |
| 379 | { |
| 380 | i->rdev = cpu_to_le32(val); |
| 381 | } |
| 382 | |
| 383 | static inline u16 btrfs_inode_flags(struct btrfs_inode_item *i) |
| 384 | { |
| 385 | return le16_to_cpu(i->flags); |
| 386 | } |
| 387 | |
| 388 | static inline void btrfs_set_inode_flags(struct btrfs_inode_item *i, u16 val) |
| 389 | { |
| 390 | i->flags = cpu_to_le16(val); |
| 391 | } |
| 392 | |
| 393 | static inline u16 btrfs_inode_compat_flags(struct btrfs_inode_item *i) |
| 394 | { |
| 395 | return le16_to_cpu(i->compat_flags); |
| 396 | } |
| 397 | |
| 398 | static inline void btrfs_set_inode_compat_flags(struct btrfs_inode_item *i, |
| 399 | u16 val) |
| 400 | { |
| 401 | i->compat_flags = cpu_to_le16(val); |
| 402 | } |
| 403 | |
| 404 | static inline u32 btrfs_timespec_sec(struct btrfs_inode_timespec *ts) |
| 405 | { |
| 406 | return le32_to_cpu(ts->sec); |
| 407 | } |
| 408 | |
| 409 | static inline void btrfs_set_timespec_sec(struct btrfs_inode_timespec *ts, |
| 410 | u32 val) |
| 411 | { |
| 412 | ts->sec = cpu_to_le32(val); |
| 413 | } |
| 414 | |
| 415 | static inline u32 btrfs_timespec_nsec(struct btrfs_inode_timespec *ts) |
| 416 | { |
| 417 | return le32_to_cpu(ts->nsec); |
| 418 | } |
| 419 | |
| 420 | static inline void btrfs_set_timespec_nsec(struct btrfs_inode_timespec *ts, |
| 421 | u32 val) |
| 422 | { |
| 423 | ts->nsec = cpu_to_le32(val); |
| 424 | } |
| 425 | |
| 426 | |
| 427 | |
| 428 | static inline u64 btrfs_extent_owner(struct btrfs_extent_item *ei) |
| 429 | { |
| 430 | return le64_to_cpu(ei->owner); |
| 431 | } |
| 432 | |
| 433 | static inline void btrfs_set_extent_owner(struct btrfs_extent_item *ei, u64 val) |
| 434 | { |
| 435 | ei->owner = cpu_to_le64(val); |
| 436 | } |
| 437 | |
| 438 | static inline u32 btrfs_extent_refs(struct btrfs_extent_item *ei) |
| 439 | { |
| 440 | return le32_to_cpu(ei->refs); |
| 441 | } |
| 442 | |
| 443 | static inline void btrfs_set_extent_refs(struct btrfs_extent_item *ei, u32 val) |
| 444 | { |
| 445 | ei->refs = cpu_to_le32(val); |
| 446 | } |
| 447 | |
| 448 | static inline u64 btrfs_node_blockptr(struct btrfs_node *n, int nr) |
| 449 | { |
| 450 | return le64_to_cpu(n->ptrs[nr].blockptr); |
| 451 | } |
| 452 | |
| 453 | static inline void btrfs_set_node_blockptr(struct btrfs_node *n, int nr, |
| 454 | u64 val) |
| 455 | { |
| 456 | n->ptrs[nr].blockptr = cpu_to_le64(val); |
| 457 | } |
| 458 | |
| 459 | static inline u32 btrfs_item_offset(struct btrfs_item *item) |
| 460 | { |
| 461 | return le32_to_cpu(item->offset); |
| 462 | } |
| 463 | |
| 464 | static inline void btrfs_set_item_offset(struct btrfs_item *item, u32 val) |
| 465 | { |
| 466 | item->offset = cpu_to_le32(val); |
| 467 | } |
| 468 | |
| 469 | static inline u32 btrfs_item_end(struct btrfs_item *item) |
| 470 | { |
| 471 | return le32_to_cpu(item->offset) + le16_to_cpu(item->size); |
| 472 | } |
| 473 | |
| 474 | static inline u16 btrfs_item_size(struct btrfs_item *item) |
| 475 | { |
| 476 | return le16_to_cpu(item->size); |
| 477 | } |
| 478 | |
| 479 | static inline void btrfs_set_item_size(struct btrfs_item *item, u16 val) |
| 480 | { |
| 481 | item->size = cpu_to_le16(val); |
| 482 | } |
| 483 | |
| 484 | static inline u64 btrfs_dir_objectid(struct btrfs_dir_item *d) |
| 485 | { |
| 486 | return le64_to_cpu(d->objectid); |
| 487 | } |
| 488 | |
| 489 | static inline void btrfs_set_dir_objectid(struct btrfs_dir_item *d, u64 val) |
| 490 | { |
| 491 | d->objectid = cpu_to_le64(val); |
| 492 | } |
| 493 | |
| 494 | static inline u16 btrfs_dir_flags(struct btrfs_dir_item *d) |
| 495 | { |
| 496 | return le16_to_cpu(d->flags); |
| 497 | } |
| 498 | |
| 499 | static inline void btrfs_set_dir_flags(struct btrfs_dir_item *d, u16 val) |
| 500 | { |
| 501 | d->flags = cpu_to_le16(val); |
| 502 | } |
| 503 | |
| 504 | static inline u8 btrfs_dir_type(struct btrfs_dir_item *d) |
| 505 | { |
| 506 | return d->type; |
| 507 | } |
| 508 | |
| 509 | static inline void btrfs_set_dir_type(struct btrfs_dir_item *d, u8 val) |
| 510 | { |
| 511 | d->type = val; |
| 512 | } |
| 513 | |
| 514 | static inline u16 btrfs_dir_name_len(struct btrfs_dir_item *d) |
| 515 | { |
| 516 | return le16_to_cpu(d->name_len); |
| 517 | } |
| 518 | |
| 519 | static inline void btrfs_set_dir_name_len(struct btrfs_dir_item *d, u16 val) |
| 520 | { |
| 521 | d->name_len = cpu_to_le16(val); |
| 522 | } |
| 523 | |
| 524 | static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu, |
| 525 | struct btrfs_disk_key *disk) |
| 526 | { |
| 527 | cpu->offset = le64_to_cpu(disk->offset); |
| 528 | cpu->flags = le32_to_cpu(disk->flags); |
| 529 | cpu->objectid = le64_to_cpu(disk->objectid); |
| 530 | } |
| 531 | |
| 532 | static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk, |
| 533 | struct btrfs_key *cpu) |
| 534 | { |
| 535 | disk->offset = cpu_to_le64(cpu->offset); |
| 536 | disk->flags = cpu_to_le32(cpu->flags); |
| 537 | disk->objectid = cpu_to_le64(cpu->objectid); |
| 538 | } |
| 539 | |
| 540 | static inline u64 btrfs_disk_key_objectid(struct btrfs_disk_key *disk) |
| 541 | { |
| 542 | return le64_to_cpu(disk->objectid); |
| 543 | } |
| 544 | |
| 545 | static inline void btrfs_set_disk_key_objectid(struct btrfs_disk_key *disk, |
| 546 | u64 val) |
| 547 | { |
| 548 | disk->objectid = cpu_to_le64(val); |
| 549 | } |
| 550 | |
| 551 | static inline u64 btrfs_disk_key_offset(struct btrfs_disk_key *disk) |
| 552 | { |
| 553 | return le64_to_cpu(disk->offset); |
| 554 | } |
| 555 | |
| 556 | static inline void btrfs_set_disk_key_offset(struct btrfs_disk_key *disk, |
| 557 | u64 val) |
| 558 | { |
| 559 | disk->offset = cpu_to_le64(val); |
| 560 | } |
| 561 | |
| 562 | static inline u32 btrfs_disk_key_flags(struct btrfs_disk_key *disk) |
| 563 | { |
| 564 | return le32_to_cpu(disk->flags); |
| 565 | } |
| 566 | |
| 567 | static inline void btrfs_set_disk_key_flags(struct btrfs_disk_key *disk, |
| 568 | u32 val) |
| 569 | { |
| 570 | disk->flags = cpu_to_le32(val); |
| 571 | } |
| 572 | |
| 573 | static inline u32 btrfs_key_type(struct btrfs_key *key) |
| 574 | { |
| 575 | return key->flags & BTRFS_KEY_TYPE_MASK; |
| 576 | } |
| 577 | |
| 578 | static inline u32 btrfs_disk_key_type(struct btrfs_disk_key *key) |
| 579 | { |
| 580 | return le32_to_cpu(key->flags) & BTRFS_KEY_TYPE_MASK; |
| 581 | } |
| 582 | |
| 583 | static inline void btrfs_set_key_type(struct btrfs_key *key, u32 type) |
| 584 | { |
| 585 | BUG_ON(type >= BTRFS_KEY_TYPE_MAX); |
| 586 | key->flags = (key->flags & ~((u64)BTRFS_KEY_TYPE_MASK)) | type; |
| 587 | } |
| 588 | |
| 589 | static inline void btrfs_set_disk_key_type(struct btrfs_disk_key *key, u32 type) |
| 590 | { |
| 591 | u32 flags = btrfs_disk_key_flags(key); |
| 592 | BUG_ON(type >= BTRFS_KEY_TYPE_MAX); |
| 593 | flags = (flags & ~((u64)BTRFS_KEY_TYPE_MASK)) | type; |
| 594 | btrfs_set_disk_key_flags(key, flags); |
| 595 | } |
| 596 | |
| 597 | static inline u64 btrfs_header_blocknr(struct btrfs_header *h) |
| 598 | { |
| 599 | return le64_to_cpu(h->blocknr); |
| 600 | } |
| 601 | |
| 602 | static inline void btrfs_set_header_blocknr(struct btrfs_header *h, u64 blocknr) |
| 603 | { |
| 604 | h->blocknr = cpu_to_le64(blocknr); |
| 605 | } |
| 606 | |
| 607 | static inline u64 btrfs_header_generation(struct btrfs_header *h) |
| 608 | { |
| 609 | return le64_to_cpu(h->generation); |
| 610 | } |
| 611 | |
| 612 | static inline void btrfs_set_header_generation(struct btrfs_header *h, |
| 613 | u64 val) |
| 614 | { |
| 615 | h->generation = cpu_to_le64(val); |
| 616 | } |
| 617 | |
| 618 | static inline u64 btrfs_header_parentid(struct btrfs_header *h) |
| 619 | { |
| 620 | return le64_to_cpu(h->parentid); |
| 621 | } |
| 622 | |
| 623 | static inline void btrfs_set_header_parentid(struct btrfs_header *h, |
| 624 | u64 parentid) |
| 625 | { |
| 626 | h->parentid = cpu_to_le64(parentid); |
| 627 | } |
| 628 | |
| 629 | static inline u16 btrfs_header_nritems(struct btrfs_header *h) |
| 630 | { |
| 631 | return le16_to_cpu(h->nritems); |
| 632 | } |
| 633 | |
| 634 | static inline void btrfs_set_header_nritems(struct btrfs_header *h, u16 val) |
| 635 | { |
| 636 | h->nritems = cpu_to_le16(val); |
| 637 | } |
| 638 | |
| 639 | static inline u16 btrfs_header_flags(struct btrfs_header *h) |
| 640 | { |
| 641 | return le16_to_cpu(h->flags); |
| 642 | } |
| 643 | |
| 644 | static inline void btrfs_set_header_flags(struct btrfs_header *h, u16 val) |
| 645 | { |
| 646 | h->flags = cpu_to_le16(val); |
| 647 | } |
| 648 | |
| 649 | static inline int btrfs_header_level(struct btrfs_header *h) |
| 650 | { |
| 651 | return btrfs_header_flags(h) & (BTRFS_MAX_LEVEL - 1); |
| 652 | } |
| 653 | |
| 654 | static inline void btrfs_set_header_level(struct btrfs_header *h, int level) |
| 655 | { |
| 656 | u16 flags; |
| 657 | BUG_ON(level > BTRFS_MAX_LEVEL); |
| 658 | flags = btrfs_header_flags(h) & ~(BTRFS_MAX_LEVEL - 1); |
| 659 | btrfs_set_header_flags(h, flags | level); |
| 660 | } |
| 661 | |
| 662 | static inline int btrfs_is_leaf(struct btrfs_node *n) |
| 663 | { |
| 664 | return (btrfs_header_level(&n->header) == 0); |
| 665 | } |
| 666 | |
| 667 | static inline u64 btrfs_root_blocknr(struct btrfs_root_item *item) |
| 668 | { |
| 669 | return le64_to_cpu(item->blocknr); |
| 670 | } |
| 671 | |
| 672 | static inline void btrfs_set_root_blocknr(struct btrfs_root_item *item, u64 val) |
| 673 | { |
| 674 | item->blocknr = cpu_to_le64(val); |
| 675 | } |
| 676 | |
| 677 | static inline u32 btrfs_root_refs(struct btrfs_root_item *item) |
| 678 | { |
| 679 | return le32_to_cpu(item->refs); |
| 680 | } |
| 681 | |
| 682 | static inline void btrfs_set_root_refs(struct btrfs_root_item *item, u32 val) |
| 683 | { |
| 684 | item->refs = cpu_to_le32(val); |
| 685 | } |
| 686 | |
| 687 | static inline u64 btrfs_super_blocknr(struct btrfs_super_block *s) |
| 688 | { |
| 689 | return le64_to_cpu(s->blocknr); |
| 690 | } |
| 691 | |
| 692 | static inline void btrfs_set_super_blocknr(struct btrfs_super_block *s, u64 val) |
| 693 | { |
| 694 | s->blocknr = cpu_to_le64(val); |
| 695 | } |
| 696 | |
| 697 | static inline u64 btrfs_super_root(struct btrfs_super_block *s) |
| 698 | { |
| 699 | return le64_to_cpu(s->root); |
| 700 | } |
| 701 | |
| 702 | static inline void btrfs_set_super_root(struct btrfs_super_block *s, u64 val) |
| 703 | { |
| 704 | s->root = cpu_to_le64(val); |
| 705 | } |
| 706 | |
| 707 | static inline u64 btrfs_super_total_blocks(struct btrfs_super_block *s) |
| 708 | { |
| 709 | return le64_to_cpu(s->total_blocks); |
| 710 | } |
| 711 | |
| 712 | static inline void btrfs_set_super_total_blocks(struct btrfs_super_block *s, |
| 713 | u64 val) |
| 714 | { |
| 715 | s->total_blocks = cpu_to_le64(val); |
| 716 | } |
| 717 | |
| 718 | static inline u64 btrfs_super_blocks_used(struct btrfs_super_block *s) |
| 719 | { |
| 720 | return le64_to_cpu(s->blocks_used); |
| 721 | } |
| 722 | |
| 723 | static inline void btrfs_set_super_blocks_used(struct btrfs_super_block *s, |
| 724 | u64 val) |
| 725 | { |
| 726 | s->blocks_used = cpu_to_le64(val); |
| 727 | } |
| 728 | |
| 729 | static inline u32 btrfs_super_blocksize(struct btrfs_super_block *s) |
| 730 | { |
| 731 | return le32_to_cpu(s->blocksize); |
| 732 | } |
| 733 | |
| 734 | static inline void btrfs_set_super_blocksize(struct btrfs_super_block *s, |
| 735 | u32 val) |
| 736 | { |
| 737 | s->blocksize = cpu_to_le32(val); |
| 738 | } |
| 739 | |
| 740 | static inline u64 btrfs_super_root_dir(struct btrfs_super_block *s) |
| 741 | { |
| 742 | return le64_to_cpu(s->root_dir_objectid); |
| 743 | } |
| 744 | |
| 745 | static inline void btrfs_set_super_root_dir(struct btrfs_super_block *s, u64 |
| 746 | val) |
| 747 | { |
| 748 | s->root_dir_objectid = cpu_to_le64(val); |
| 749 | } |
| 750 | |
| 751 | static inline u8 *btrfs_leaf_data(struct btrfs_leaf *l) |
| 752 | { |
| 753 | return (u8 *)l->items; |
| 754 | } |
| 755 | |
| 756 | static inline u64 btrfs_file_extent_disk_blocknr(struct btrfs_file_extent_item |
| 757 | *e) |
| 758 | { |
| 759 | return le64_to_cpu(e->disk_blocknr); |
| 760 | } |
| 761 | |
| 762 | static inline void btrfs_set_file_extent_disk_blocknr(struct |
| 763 | btrfs_file_extent_item |
| 764 | *e, u64 val) |
| 765 | { |
| 766 | e->disk_blocknr = cpu_to_le64(val); |
| 767 | } |
| 768 | |
| 769 | static inline u64 btrfs_file_extent_disk_num_blocks(struct |
| 770 | btrfs_file_extent_item *e) |
| 771 | { |
| 772 | return le64_to_cpu(e->disk_num_blocks); |
| 773 | } |
| 774 | |
| 775 | static inline void btrfs_set_file_extent_disk_num_blocks(struct |
| 776 | btrfs_file_extent_item |
| 777 | *e, u64 val) |
| 778 | { |
| 779 | e->disk_num_blocks = cpu_to_le64(val); |
| 780 | } |
| 781 | |
| 782 | static inline u64 btrfs_file_extent_offset(struct btrfs_file_extent_item *e) |
| 783 | { |
| 784 | return le64_to_cpu(e->offset); |
| 785 | } |
| 786 | |
| 787 | static inline void btrfs_set_file_extent_offset(struct btrfs_file_extent_item |
| 788 | *e, u64 val) |
| 789 | { |
| 790 | e->offset = cpu_to_le64(val); |
| 791 | } |
| 792 | |
| 793 | static inline u64 btrfs_file_extent_num_blocks(struct btrfs_file_extent_item |
| 794 | *e) |
| 795 | { |
| 796 | return le64_to_cpu(e->num_blocks); |
| 797 | } |
| 798 | |
| 799 | static inline void btrfs_set_file_extent_num_blocks(struct |
| 800 | btrfs_file_extent_item *e, |
| 801 | u64 val) |
| 802 | { |
| 803 | e->num_blocks = cpu_to_le64(val); |
| 804 | } |
| 805 | |
| 806 | static inline struct btrfs_root *btrfs_sb(struct super_block *sb) |
| 807 | { |
| 808 | return sb->s_fs_info; |
| 809 | } |
| 810 | |
| 811 | /* helper function to cast into the data area of the leaf. */ |
| 812 | #define btrfs_item_ptr(leaf, slot, type) \ |
| 813 | ((type *)(btrfs_leaf_data(leaf) + \ |
| 814 | btrfs_item_offset((leaf)->items + (slot)))) |
| 815 | |
| 816 | /* extent-item.c */ |
| 817 | struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans, |
| 818 | struct btrfs_root *root); |
| 819 | int btrfs_alloc_extent(struct btrfs_trans_handle *trans, struct btrfs_root |
| 820 | *root, u64 num_blocks, u64 search_start, u64 |
| 821 | search_end, u64 owner, struct btrfs_key *ins); |
| 822 | int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 823 | struct buffer_head *buf); |
| 824 | int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root |
| 825 | *root, u64 blocknr, u64 num_blocks, int pin); |
| 826 | int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct |
| 827 | btrfs_root *root); |
| 828 | /* ctree.c */ |
| 829 | int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root |
| 830 | *root, struct btrfs_key *key, struct btrfs_path *p, int |
| 831 | ins_len, int cow); |
| 832 | void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p); |
| 833 | void btrfs_init_path(struct btrfs_path *p); |
| 834 | int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 835 | struct btrfs_path *path); |
| 836 | int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root |
| 837 | *root, struct btrfs_key *key, void *data, u32 data_size); |
| 838 | int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root |
| 839 | *root, struct btrfs_path *path, struct btrfs_key |
| 840 | *cpu_key, u32 data_size); |
| 841 | int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path); |
| 842 | int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf); |
| 843 | int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root |
| 844 | *root, struct buffer_head *snap); |
| 845 | /* root-item.c */ |
| 846 | int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 847 | struct btrfs_key *key); |
| 848 | int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root |
| 849 | *root, struct btrfs_key *key, struct btrfs_root_item |
| 850 | *item); |
| 851 | int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root |
| 852 | *root, struct btrfs_key *key, struct btrfs_root_item |
| 853 | *item); |
| 854 | int btrfs_find_last_root(struct btrfs_root *root, u64 objectid, struct |
| 855 | btrfs_root_item *item, struct btrfs_key *key); |
| 856 | /* dir-item.c */ |
| 857 | int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root |
| 858 | *root, const char *name, int name_len, u64 dir, u64 |
| 859 | objectid, u8 type); |
| 860 | int btrfs_lookup_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root |
| 861 | *root, struct btrfs_path *path, u64 dir, |
| 862 | const char *name, int name_len, int mod); |
| 863 | int btrfs_match_dir_item_name(struct btrfs_root *root, struct btrfs_path *path, |
| 864 | const char *name, int name_len); |
| 865 | /* inode-map.c */ |
| 866 | int btrfs_find_free_objectid(struct btrfs_trans_handle *trans, |
| 867 | struct btrfs_root *fs_root, |
| 868 | u64 dirid, u64 *objectid); |
| 869 | int btrfs_insert_inode_map(struct btrfs_trans_handle *trans, |
| 870 | struct btrfs_root *root, |
| 871 | u64 objectid, struct btrfs_key *location); |
| 872 | int btrfs_lookup_inode_map(struct btrfs_trans_handle *trans, |
| 873 | struct btrfs_root *root, struct btrfs_path *path, |
| 874 | u64 objectid, int mod); |
| 875 | /* inode-item.c */ |
| 876 | int btrfs_insert_inode(struct btrfs_trans_handle *trans, struct btrfs_root |
| 877 | *root, u64 objectid, struct btrfs_inode_item |
| 878 | *inode_item); |
| 879 | int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root |
| 880 | *root, struct btrfs_path *path, u64 objectid, int mod); |
| 881 | |
| 882 | /* file-item.c */ |
| 883 | int btrfs_alloc_file_extent(struct btrfs_trans_handle *trans, |
| 884 | struct btrfs_root *root, |
| 885 | u64 objectid, u64 offset, |
| 886 | u64 num_blocks, u64 hint_block, |
| 887 | u64 *result); |
| 888 | int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans, |
| 889 | struct btrfs_root *root, |
| 890 | struct btrfs_path *path, u64 objectid, |
| 891 | u64 blocknr, u64 num_blocks, int mod); |
| 892 | #endif |