2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
23 #include "kerncompat.h"
24 #include "radix-tree.h"
25 #include "extent-cache.h"
26 #include "extent_map.h"
29 struct btrfs_trans_handle;
30 #define BTRFS_MAGIC "_B2RfS_M"
32 #define BTRFS_MAX_LEVEL 8
33 #define BTRFS_ROOT_TREE_OBJECTID 1ULL
34 #define BTRFS_EXTENT_TREE_OBJECTID 2ULL
35 #define BTRFS_FS_TREE_OBJECTID 3ULL
36 #define BTRFS_ROOT_TREE_DIR_OBJECTID 4ULL
37 #define BTRFS_FIRST_FREE_OBJECTID 256ULL
40 * we can actually store much bigger names, but lets not confuse the rest
43 #define BTRFS_NAME_LEN 255
45 /* 32 bytes in various csum fields */
46 #define BTRFS_CSUM_SIZE 32
47 /* four bytes for CRC32 */
48 #define BTRFS_CRC32_SIZE 4
49 #define BTRFS_EMPTY_DIR_SIZE 0
51 #define BTRFS_FT_UNKNOWN 0
52 #define BTRFS_FT_REG_FILE 1
53 #define BTRFS_FT_DIR 2
54 #define BTRFS_FT_CHRDEV 3
55 #define BTRFS_FT_BLKDEV 4
56 #define BTRFS_FT_FIFO 5
57 #define BTRFS_FT_SOCK 6
58 #define BTRFS_FT_SYMLINK 7
59 #define BTRFS_FT_XATTR 8
60 #define BTRFS_FT_MAX 9
63 * the key defines the order in the tree, and so it also defines (optimal)
64 * block layout. objectid corresonds to the inode number. The flags
65 * tells us things about the object, and is a kind of stream selector.
66 * so for a given inode, keys with flags of 1 might refer to the inode
67 * data, flags of 2 may point to file data in the btree and flags == 3
68 * may point to extents.
70 * offset is the starting byte offset for this key in the stream.
72 * btrfs_disk_key is in disk byte order. struct btrfs_key is always
73 * in cpu native order. Otherwise they are identical and their sizes
74 * should be the same (ie both packed)
76 struct btrfs_disk_key {
80 } __attribute__ ((__packed__));
86 } __attribute__ ((__packed__));
88 #define BTRFS_FSID_SIZE 16
90 * every tree block (leaf or node) starts with this header.
93 u8 csum[BTRFS_CSUM_SIZE];
94 u8 fsid[BTRFS_FSID_SIZE]; /* FS specific uuid */
95 __le64 bytenr; /* which block this node is supposed to live in */
101 } __attribute__ ((__packed__));
103 #define BTRFS_NODEPTRS_PER_BLOCK(r) (((r)->nodesize - \
104 sizeof(struct btrfs_header)) / \
105 sizeof(struct btrfs_key_ptr))
106 #define __BTRFS_LEAF_DATA_SIZE(bs) ((bs) - sizeof(struct btrfs_header))
107 #define BTRFS_LEAF_DATA_SIZE(r) (__BTRFS_LEAF_DATA_SIZE(r->leafsize))
108 #define BTRFS_MAX_INLINE_DATA_SIZE(r) (BTRFS_LEAF_DATA_SIZE(r) - \
109 sizeof(struct btrfs_item) - \
110 sizeof(struct btrfs_file_extent_item))
112 * the super block basically lists the main trees of the FS
113 * it currently lacks any block count etc etc
115 struct btrfs_super_block {
116 u8 csum[BTRFS_CSUM_SIZE];
117 /* the first 3 fields must match struct btrfs_header */
118 u8 fsid[BTRFS_FSID_SIZE]; /* FS specific uuid */
119 __le64 bytenr; /* this block number */
125 __le64 root_dir_objectid;
131 } __attribute__ ((__packed__));
134 * A leaf is full of items. offset and size tell us where to find
135 * the item in the leaf (relative to the start of the data area)
138 struct btrfs_disk_key key;
141 } __attribute__ ((__packed__));
144 * leaves have an item area and a data area:
145 * [item0, item1....itemN] [free space] [dataN...data1, data0]
147 * The data is separate from the items to get the keys closer together
151 struct btrfs_header header;
152 struct btrfs_item items[];
153 } __attribute__ ((__packed__));
156 * all non-leaf blocks are nodes, they hold only keys and pointers to
159 struct btrfs_key_ptr {
160 struct btrfs_disk_key key;
163 } __attribute__ ((__packed__));
166 struct btrfs_header header;
167 struct btrfs_key_ptr ptrs[];
168 } __attribute__ ((__packed__));
171 * btrfs_paths remember the path taken from the root down to the leaf.
172 * level 0 is always the leaf, and nodes[1...BTRFS_MAX_LEVEL] will point
173 * to any other levels that are present.
175 * The slots array records the index of the item or block pointer
176 * used while walking the tree.
179 struct extent_buffer *nodes[BTRFS_MAX_LEVEL];
180 int slots[BTRFS_MAX_LEVEL];
186 * items in the extent btree are used to record the objectid of the
187 * owner of the block and the number of references
189 struct btrfs_extent_item {
191 } __attribute__ ((__packed__));
193 struct btrfs_extent_ref {
198 } __attribute__ ((__packed__));
200 struct btrfs_inode_ref {
203 } __attribute__ ((__packed__));
205 struct btrfs_inode_timespec {
208 } __attribute__ ((__packed__));
211 * there is no padding here on purpose. If you want to extent the inode,
212 * make a new item type
214 struct btrfs_inode_item {
226 struct btrfs_inode_timespec atime;
227 struct btrfs_inode_timespec ctime;
228 struct btrfs_inode_timespec mtime;
229 struct btrfs_inode_timespec otime;
230 } __attribute__ ((__packed__));
232 struct btrfs_dir_item {
233 struct btrfs_disk_key location;
237 } __attribute__ ((__packed__));
239 struct btrfs_root_item {
240 struct btrfs_inode_item inode;
247 struct btrfs_disk_key drop_progress;
250 } __attribute__ ((__packed__));
252 #define BTRFS_FILE_EXTENT_REG 0
253 #define BTRFS_FILE_EXTENT_INLINE 1
255 struct btrfs_file_extent_item {
259 * disk space consumed by the extent, checksum blocks are included
263 __le64 disk_num_bytes;
265 * the logical offset in file blocks (no csums)
266 * this extent record is for. This allows a file extent to point
267 * into the middle of an existing extent on disk, sharing it
268 * between two snapshots (useful if some bytes in the middle of the
269 * extent have changed
273 * the logical number of file blocks (no csums included)
276 } __attribute__ ((__packed__));
278 struct btrfs_csum_item {
280 } __attribute__ ((__packed__));
282 /* tag for the radix tree of block groups in ram */
283 #define BTRFS_BLOCK_GROUP_SIZE (256 * 1024 * 1024)
285 #define BTRFS_BLOCK_GROUP_DATA 1
286 #define BTRFS_BLOCK_GROUP_MIXED 2
288 struct btrfs_block_group_item {
291 } __attribute__ ((__packed__));
293 struct btrfs_block_group_cache {
294 struct cache_extent cache;
295 struct btrfs_key key;
296 struct btrfs_block_group_item item;
301 struct btrfs_extent_ops {
302 int (*alloc_extent)(struct btrfs_root *root, u64 num_bytes,
303 u64 hint_byte, struct btrfs_key *ins);
304 int (*free_extent)(struct btrfs_root *root, u64 bytenr,
308 struct btrfs_fs_info {
309 u8 fsid[BTRFS_FSID_SIZE];
310 struct btrfs_root *fs_root;
311 struct btrfs_root *extent_root;
312 struct btrfs_root *tree_root;
314 struct extent_map_tree extent_cache;
315 struct extent_map_tree free_space_cache;
316 struct extent_map_tree block_group_cache;
317 struct extent_map_tree pending_tree;
318 struct extent_map_tree pinned_extents;
319 struct extent_map_tree del_pending;
320 struct extent_map_tree pending_del;
321 struct extent_map_tree extent_ins;
324 u64 last_trans_committed;
325 struct btrfs_trans_handle *running_transaction;
326 struct btrfs_super_block super_copy;
327 struct extent_buffer *sb_buffer;
328 struct mutex fs_mutex;
332 struct btrfs_extent_ops *extent_ops;
336 * in ram representation of the tree. extent_root is used for all allocations
337 * and for the extent tree extent_root root.
340 struct extent_buffer *node;
341 struct extent_buffer *commit_root;
342 struct btrfs_root_item root_item;
343 struct btrfs_key root_key;
344 struct btrfs_fs_info *fs_info;
348 /* data allocations are done in sectorsize units */
351 /* node allocations are done in nodesize units */
354 /* leaf allocations are done in leafsize units */
357 /* leaf allocations are done in leafsize units */
364 u64 last_inode_alloc;
368 * inode items have the data typically returned from stat and store other
369 * info about object characteristics. There is one for every file and dir in
372 #define BTRFS_INODE_ITEM_KEY 1
373 #define BTRFS_INODE_REF_KEY 2
374 #define BTRFS_XATTR_ITEM_KEY 8
376 /* reserve 3-15 close to the inode for later flexibility */
379 * dir items are the name -> inode pointers in a directory. There is one
380 * for every name in a directory.
382 #define BTRFS_DIR_ITEM_KEY 16
383 #define BTRFS_DIR_INDEX_KEY 17
385 * extent data is for file data
387 #define BTRFS_EXTENT_DATA_KEY 18
389 * csum items have the checksums for data in the extents
391 #define BTRFS_CSUM_ITEM_KEY 19
393 /* reserve 20-31 for other file stuff */
396 * root items point to tree roots. There are typically in the root
397 * tree used by the super block to find all the other trees
399 #define BTRFS_ROOT_ITEM_KEY 32
401 * extent items are in the extent map tree. These record which blocks
402 * are used, and how many references there are to each block
404 #define BTRFS_EXTENT_ITEM_KEY 33
405 #define BTRFS_EXTENT_REF_KEY 34
408 * block groups give us hints into the extent allocation trees. Which
409 * blocks are free etc etc
411 #define BTRFS_BLOCK_GROUP_ITEM_KEY 50
414 * string items are for debugging. They just store a short string of
417 #define BTRFS_STRING_ITEM_KEY 253
420 #define read_eb_member(eb, ptr, type, member, result) ( \
421 read_extent_buffer(eb, (char *)(result), \
422 ((unsigned long)(ptr)) + \
423 offsetof(type, member), \
424 sizeof(((type *)0)->member)))
426 #define write_eb_member(eb, ptr, type, member, result) ( \
427 write_extent_buffer(eb, (char *)(result), \
428 ((unsigned long)(ptr)) + \
429 offsetof(type, member), \
430 sizeof(((type *)0)->member)))
432 #define BTRFS_SETGET_HEADER_FUNCS(name, type, member, bits) \
433 static inline u##bits btrfs_##name(struct extent_buffer *eb) \
435 struct btrfs_header *h = (struct btrfs_header *)eb->data; \
436 return le##bits##_to_cpu(h->member); \
438 static inline void btrfs_set_##name(struct extent_buffer *eb, \
441 struct btrfs_header *h = (struct btrfs_header *)eb->data; \
442 h->member = cpu_to_le##bits(val); \
445 #define BTRFS_SETGET_FUNCS(name, type, member, bits) \
446 static inline u##bits btrfs_##name(struct extent_buffer *eb, \
449 unsigned long offset = (unsigned long)s + \
450 offsetof(type, member); \
451 __le##bits *tmp = (__le##bits *)(eb->data + offset); \
452 return le##bits##_to_cpu(*tmp); \
454 static inline void btrfs_set_##name(struct extent_buffer *eb, \
455 type *s, u##bits val) \
457 unsigned long offset = (unsigned long)s + \
458 offsetof(type, member); \
459 __le##bits *tmp = (__le##bits *)(eb->data + offset); \
460 *tmp = cpu_to_le##bits(val); \
463 #define BTRFS_SETGET_STACK_FUNCS(name, type, member, bits) \
464 static inline u##bits btrfs_##name(type *s) \
466 return le##bits##_to_cpu(s->member); \
468 static inline void btrfs_set_##name(type *s, u##bits val) \
470 s->member = cpu_to_le##bits(val); \
473 /* struct btrfs_block_group_item */
474 BTRFS_SETGET_STACK_FUNCS(block_group_used, struct btrfs_block_group_item,
476 BTRFS_SETGET_FUNCS(disk_block_group_used, struct btrfs_block_group_item,
479 /* struct btrfs_inode_ref */
480 BTRFS_SETGET_FUNCS(inode_ref_name_len, struct btrfs_inode_ref, name_len, 16);
482 /* struct btrfs_inode_item */
483 BTRFS_SETGET_FUNCS(inode_generation, struct btrfs_inode_item, generation, 64);
484 BTRFS_SETGET_FUNCS(inode_size, struct btrfs_inode_item, size, 64);
485 BTRFS_SETGET_FUNCS(inode_nblocks, struct btrfs_inode_item, nblocks, 64);
486 BTRFS_SETGET_FUNCS(inode_block_group, struct btrfs_inode_item, block_group, 64);
487 BTRFS_SETGET_FUNCS(inode_nlink, struct btrfs_inode_item, nlink, 32);
488 BTRFS_SETGET_FUNCS(inode_uid, struct btrfs_inode_item, uid, 32);
489 BTRFS_SETGET_FUNCS(inode_gid, struct btrfs_inode_item, gid, 32);
490 BTRFS_SETGET_FUNCS(inode_mode, struct btrfs_inode_item, mode, 32);
491 BTRFS_SETGET_FUNCS(inode_rdev, struct btrfs_inode_item, rdev, 32);
492 BTRFS_SETGET_FUNCS(inode_flags, struct btrfs_inode_item, flags, 16);
493 BTRFS_SETGET_FUNCS(inode_compat_flags, struct btrfs_inode_item,
496 BTRFS_SETGET_STACK_FUNCS(stack_inode_generation,
497 struct btrfs_inode_item, generation, 64);
498 BTRFS_SETGET_STACK_FUNCS(stack_inode_size,
499 struct btrfs_inode_item, size, 64);
500 BTRFS_SETGET_STACK_FUNCS(stack_inode_nblocks,
501 struct btrfs_inode_item, nblocks, 64);
502 BTRFS_SETGET_STACK_FUNCS(stack_inode_block_group,
503 struct btrfs_inode_item, block_group, 64);
504 BTRFS_SETGET_STACK_FUNCS(stack_inode_nlink,
505 struct btrfs_inode_item, nlink, 32);
506 BTRFS_SETGET_STACK_FUNCS(stack_inode_uid,
507 struct btrfs_inode_item, uid, 32);
508 BTRFS_SETGET_STACK_FUNCS(stack_inode_gid,
509 struct btrfs_inode_item, gid, 32);
510 BTRFS_SETGET_STACK_FUNCS(stack_inode_mode,
511 struct btrfs_inode_item, mode, 32);
512 BTRFS_SETGET_STACK_FUNCS(stack_inode_rdev,
513 struct btrfs_inode_item, rdev, 32);
514 BTRFS_SETGET_STACK_FUNCS(stack_inode_flags,
515 struct btrfs_inode_item, flags, 16);
516 BTRFS_SETGET_STACK_FUNCS(stack_inode_compat_flags,
517 struct btrfs_inode_item, compat_flags, 16);
519 static inline struct btrfs_inode_timespec *
520 btrfs_inode_atime(struct btrfs_inode_item *inode_item)
522 unsigned long ptr = (unsigned long)inode_item;
523 ptr += offsetof(struct btrfs_inode_item, atime);
524 return (struct btrfs_inode_timespec *)ptr;
527 static inline struct btrfs_inode_timespec *
528 btrfs_inode_mtime(struct btrfs_inode_item *inode_item)
530 unsigned long ptr = (unsigned long)inode_item;
531 ptr += offsetof(struct btrfs_inode_item, mtime);
532 return (struct btrfs_inode_timespec *)ptr;
535 static inline struct btrfs_inode_timespec *
536 btrfs_inode_ctime(struct btrfs_inode_item *inode_item)
538 unsigned long ptr = (unsigned long)inode_item;
539 ptr += offsetof(struct btrfs_inode_item, ctime);
540 return (struct btrfs_inode_timespec *)ptr;
543 static inline struct btrfs_inode_timespec *
544 btrfs_inode_otime(struct btrfs_inode_item *inode_item)
546 unsigned long ptr = (unsigned long)inode_item;
547 ptr += offsetof(struct btrfs_inode_item, otime);
548 return (struct btrfs_inode_timespec *)ptr;
551 BTRFS_SETGET_FUNCS(timespec_sec, struct btrfs_inode_timespec, sec, 64);
552 BTRFS_SETGET_FUNCS(timespec_nsec, struct btrfs_inode_timespec, nsec, 32);
553 BTRFS_SETGET_STACK_FUNCS(stack_timespec_sec, struct btrfs_inode_timespec,
555 BTRFS_SETGET_STACK_FUNCS(stack_timespec_nsec, struct btrfs_inode_timespec,
558 /* struct btrfs_extent_item */
559 BTRFS_SETGET_FUNCS(extent_refs, struct btrfs_extent_item, refs, 32);
561 /* struct btrfs_extent_ref */
562 BTRFS_SETGET_FUNCS(ref_root, struct btrfs_extent_ref, root, 64);
563 BTRFS_SETGET_FUNCS(ref_generation, struct btrfs_extent_ref, generation, 64);
564 BTRFS_SETGET_FUNCS(ref_objectid, struct btrfs_extent_ref, objectid, 64);
565 BTRFS_SETGET_FUNCS(ref_offset, struct btrfs_extent_ref, offset, 64);
567 BTRFS_SETGET_STACK_FUNCS(stack_ref_root, struct btrfs_extent_ref, root, 64);
568 BTRFS_SETGET_STACK_FUNCS(stack_ref_generation, struct btrfs_extent_ref,
570 BTRFS_SETGET_STACK_FUNCS(stack_ref_objectid, struct btrfs_extent_ref,
572 BTRFS_SETGET_STACK_FUNCS(stack_ref_offset, struct btrfs_extent_ref, offset, 64);
574 BTRFS_SETGET_STACK_FUNCS(stack_extent_refs, struct btrfs_extent_item,
577 /* struct btrfs_node */
578 BTRFS_SETGET_FUNCS(key_blockptr, struct btrfs_key_ptr, blockptr, 64);
579 BTRFS_SETGET_FUNCS(key_generation, struct btrfs_key_ptr, generation, 64);
581 static inline u64 btrfs_node_blockptr(struct extent_buffer *eb, int nr)
584 ptr = offsetof(struct btrfs_node, ptrs) +
585 sizeof(struct btrfs_key_ptr) * nr;
586 return btrfs_key_blockptr(eb, (struct btrfs_key_ptr *)ptr);
589 static inline void btrfs_set_node_blockptr(struct extent_buffer *eb,
593 ptr = offsetof(struct btrfs_node, ptrs) +
594 sizeof(struct btrfs_key_ptr) * nr;
595 btrfs_set_key_blockptr(eb, (struct btrfs_key_ptr *)ptr, val);
598 static inline u64 btrfs_node_ptr_generation(struct extent_buffer *eb, int nr)
601 ptr = offsetof(struct btrfs_node, ptrs) +
602 sizeof(struct btrfs_key_ptr) * nr;
603 return btrfs_key_generation(eb, (struct btrfs_key_ptr *)ptr);
606 static inline void btrfs_set_node_ptr_generation(struct extent_buffer *eb,
610 ptr = offsetof(struct btrfs_node, ptrs) +
611 sizeof(struct btrfs_key_ptr) * nr;
612 btrfs_set_key_generation(eb, (struct btrfs_key_ptr *)ptr, val);
615 static inline unsigned long btrfs_node_key_ptr_offset(int nr)
617 return offsetof(struct btrfs_node, ptrs) +
618 sizeof(struct btrfs_key_ptr) * nr;
621 static inline void btrfs_node_key(struct extent_buffer *eb,
622 struct btrfs_disk_key *disk_key, int nr)
625 ptr = btrfs_node_key_ptr_offset(nr);
626 read_eb_member(eb, (struct btrfs_key_ptr *)ptr,
627 struct btrfs_key_ptr, key, disk_key);
630 static inline void btrfs_set_node_key(struct extent_buffer *eb,
631 struct btrfs_disk_key *disk_key, int nr)
634 ptr = btrfs_node_key_ptr_offset(nr);
635 write_eb_member(eb, (struct btrfs_key_ptr *)ptr,
636 struct btrfs_key_ptr, key, disk_key);
639 /* struct btrfs_item */
640 BTRFS_SETGET_FUNCS(item_offset, struct btrfs_item, offset, 32);
641 BTRFS_SETGET_FUNCS(item_size, struct btrfs_item, size, 32);
643 static inline unsigned long btrfs_item_nr_offset(int nr)
645 return offsetof(struct btrfs_leaf, items) +
646 sizeof(struct btrfs_item) * nr;
649 static inline struct btrfs_item *btrfs_item_nr(struct extent_buffer *eb,
652 return (struct btrfs_item *)btrfs_item_nr_offset(nr);
655 static inline u32 btrfs_item_end(struct extent_buffer *eb,
656 struct btrfs_item *item)
658 return btrfs_item_offset(eb, item) + btrfs_item_size(eb, item);
661 static inline u32 btrfs_item_end_nr(struct extent_buffer *eb, int nr)
663 return btrfs_item_end(eb, btrfs_item_nr(eb, nr));
666 static inline u32 btrfs_item_offset_nr(struct extent_buffer *eb, int nr)
668 return btrfs_item_offset(eb, btrfs_item_nr(eb, nr));
671 static inline u32 btrfs_item_size_nr(struct extent_buffer *eb, int nr)
673 return btrfs_item_size(eb, btrfs_item_nr(eb, nr));
676 static inline void btrfs_item_key(struct extent_buffer *eb,
677 struct btrfs_disk_key *disk_key, int nr)
679 struct btrfs_item *item = btrfs_item_nr(eb, nr);
680 read_eb_member(eb, item, struct btrfs_item, key, disk_key);
683 static inline void btrfs_set_item_key(struct extent_buffer *eb,
684 struct btrfs_disk_key *disk_key, int nr)
686 struct btrfs_item *item = btrfs_item_nr(eb, nr);
687 write_eb_member(eb, item, struct btrfs_item, key, disk_key);
690 /* struct btrfs_dir_item */
691 BTRFS_SETGET_FUNCS(dir_data_len, struct btrfs_dir_item, data_len, 16);
692 BTRFS_SETGET_FUNCS(dir_type, struct btrfs_dir_item, type, 8);
693 BTRFS_SETGET_FUNCS(dir_name_len, struct btrfs_dir_item, name_len, 16);
695 static inline void btrfs_dir_item_key(struct extent_buffer *eb,
696 struct btrfs_dir_item *item,
697 struct btrfs_disk_key *key)
699 read_eb_member(eb, item, struct btrfs_dir_item, location, key);
702 static inline void btrfs_set_dir_item_key(struct extent_buffer *eb,
703 struct btrfs_dir_item *item,
704 struct btrfs_disk_key *key)
706 write_eb_member(eb, item, struct btrfs_dir_item, location, key);
709 /* struct btrfs_disk_key */
710 BTRFS_SETGET_STACK_FUNCS(disk_key_objectid, struct btrfs_disk_key,
712 BTRFS_SETGET_STACK_FUNCS(disk_key_offset, struct btrfs_disk_key, offset, 64);
713 BTRFS_SETGET_STACK_FUNCS(disk_key_type, struct btrfs_disk_key, type, 8);
715 static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu,
716 struct btrfs_disk_key *disk)
718 cpu->offset = le64_to_cpu(disk->offset);
719 cpu->type = disk->type;
720 cpu->objectid = le64_to_cpu(disk->objectid);
723 static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk,
724 struct btrfs_key *cpu)
726 disk->offset = cpu_to_le64(cpu->offset);
727 disk->type = cpu->type;
728 disk->objectid = cpu_to_le64(cpu->objectid);
731 static inline void btrfs_node_key_to_cpu(struct extent_buffer *eb,
732 struct btrfs_key *key, int nr)
734 struct btrfs_disk_key disk_key;
735 btrfs_node_key(eb, &disk_key, nr);
736 btrfs_disk_key_to_cpu(key, &disk_key);
739 static inline void btrfs_item_key_to_cpu(struct extent_buffer *eb,
740 struct btrfs_key *key, int nr)
742 struct btrfs_disk_key disk_key;
743 btrfs_item_key(eb, &disk_key, nr);
744 btrfs_disk_key_to_cpu(key, &disk_key);
747 static inline void btrfs_dir_item_key_to_cpu(struct extent_buffer *eb,
748 struct btrfs_dir_item *item,
749 struct btrfs_key *key)
751 struct btrfs_disk_key disk_key;
752 btrfs_dir_item_key(eb, item, &disk_key);
753 btrfs_disk_key_to_cpu(key, &disk_key);
757 static inline u8 btrfs_key_type(struct btrfs_key *key)
762 static inline void btrfs_set_key_type(struct btrfs_key *key, u8 val)
767 /* struct btrfs_header */
768 BTRFS_SETGET_HEADER_FUNCS(header_bytenr, struct btrfs_header, bytenr, 64);
769 BTRFS_SETGET_HEADER_FUNCS(header_generation, struct btrfs_header,
771 BTRFS_SETGET_HEADER_FUNCS(header_owner, struct btrfs_header, owner, 64);
772 BTRFS_SETGET_HEADER_FUNCS(header_nritems, struct btrfs_header, nritems, 32);
773 BTRFS_SETGET_HEADER_FUNCS(header_flags, struct btrfs_header, flags, 16);
774 BTRFS_SETGET_HEADER_FUNCS(header_level, struct btrfs_header, level, 8);
776 static inline u8 *btrfs_header_fsid(struct extent_buffer *eb)
778 unsigned long ptr = offsetof(struct btrfs_header, fsid);
782 static inline u8 *btrfs_super_fsid(struct extent_buffer *eb)
784 unsigned long ptr = offsetof(struct btrfs_super_block, fsid);
788 static inline u8 *btrfs_header_csum(struct extent_buffer *eb)
790 unsigned long ptr = offsetof(struct btrfs_header, csum);
794 static inline struct btrfs_node *btrfs_buffer_node(struct extent_buffer *eb)
799 static inline struct btrfs_leaf *btrfs_buffer_leaf(struct extent_buffer *eb)
804 static inline struct btrfs_header *btrfs_buffer_header(struct extent_buffer *eb)
809 static inline int btrfs_is_leaf(struct extent_buffer *eb)
811 return (btrfs_header_level(eb) == 0);
814 /* struct btrfs_root_item */
815 BTRFS_SETGET_FUNCS(disk_root_refs, struct btrfs_root_item, refs, 32);
816 BTRFS_SETGET_FUNCS(disk_root_bytenr, struct btrfs_root_item, bytenr, 64);
817 BTRFS_SETGET_FUNCS(disk_root_level, struct btrfs_root_item, level, 8);
819 BTRFS_SETGET_STACK_FUNCS(root_bytenr, struct btrfs_root_item, bytenr, 64);
820 BTRFS_SETGET_STACK_FUNCS(root_level, struct btrfs_root_item, level, 8);
821 BTRFS_SETGET_STACK_FUNCS(root_dirid, struct btrfs_root_item, root_dirid, 64);
822 BTRFS_SETGET_STACK_FUNCS(root_refs, struct btrfs_root_item, refs, 32);
823 BTRFS_SETGET_STACK_FUNCS(root_flags, struct btrfs_root_item, flags, 32);
824 BTRFS_SETGET_STACK_FUNCS(root_used, struct btrfs_root_item, bytes_used, 64);
825 BTRFS_SETGET_STACK_FUNCS(root_limit, struct btrfs_root_item, byte_limit, 64);
827 /* struct btrfs_super_block */
828 BTRFS_SETGET_STACK_FUNCS(super_bytenr, struct btrfs_super_block, bytenr, 64);
829 BTRFS_SETGET_STACK_FUNCS(super_generation, struct btrfs_super_block,
831 BTRFS_SETGET_STACK_FUNCS(super_root, struct btrfs_super_block, root, 64);
832 BTRFS_SETGET_STACK_FUNCS(super_root_level, struct btrfs_super_block,
834 BTRFS_SETGET_STACK_FUNCS(super_total_bytes, struct btrfs_super_block,
836 BTRFS_SETGET_STACK_FUNCS(super_bytes_used, struct btrfs_super_block,
838 BTRFS_SETGET_STACK_FUNCS(super_sectorsize, struct btrfs_super_block,
840 BTRFS_SETGET_STACK_FUNCS(super_nodesize, struct btrfs_super_block,
842 BTRFS_SETGET_STACK_FUNCS(super_leafsize, struct btrfs_super_block,
844 BTRFS_SETGET_STACK_FUNCS(super_stripesize, struct btrfs_super_block,
846 BTRFS_SETGET_STACK_FUNCS(super_root_dir, struct btrfs_super_block,
847 root_dir_objectid, 64);
849 static inline unsigned long btrfs_leaf_data(struct extent_buffer *l)
851 return offsetof(struct btrfs_leaf, items);
854 /* struct btrfs_file_extent_item */
855 BTRFS_SETGET_FUNCS(file_extent_type, struct btrfs_file_extent_item, type, 8);
857 static inline unsigned long btrfs_file_extent_inline_start(struct
858 btrfs_file_extent_item *e)
860 unsigned long offset = (unsigned long)e;
861 offset += offsetof(struct btrfs_file_extent_item, disk_bytenr);
865 static inline u32 btrfs_file_extent_calc_inline_size(u32 datasize)
867 return offsetof(struct btrfs_file_extent_item, disk_bytenr) + datasize;
870 static inline u32 btrfs_file_extent_inline_len(struct extent_buffer *eb,
871 struct btrfs_item *e)
873 unsigned long offset;
874 offset = offsetof(struct btrfs_file_extent_item, disk_bytenr);
875 return btrfs_item_size(eb, e) - offset;
878 BTRFS_SETGET_FUNCS(file_extent_disk_bytenr, struct btrfs_file_extent_item,
880 BTRFS_SETGET_FUNCS(file_extent_generation, struct btrfs_file_extent_item,
882 BTRFS_SETGET_FUNCS(file_extent_disk_num_bytes, struct btrfs_file_extent_item,
884 BTRFS_SETGET_FUNCS(file_extent_offset, struct btrfs_file_extent_item,
886 BTRFS_SETGET_FUNCS(file_extent_num_bytes, struct btrfs_file_extent_item,
889 static inline u32 btrfs_level_size(struct btrfs_root *root, int level) {
891 return root->leafsize;
892 return root->nodesize;
895 /* helper function to cast into the data area of the leaf. */
896 #define btrfs_item_ptr(leaf, slot, type) \
897 ((type *)(btrfs_leaf_data(leaf) + \
898 btrfs_item_offset_nr(leaf, slot)))
900 #define btrfs_item_ptr_offset(leaf, slot) \
901 ((unsigned long)(btrfs_leaf_data(leaf) + \
902 btrfs_item_offset_nr(leaf, slot)))
905 u32 btrfs_count_snapshots_in_path(struct btrfs_root *root,
906 struct btrfs_path *count_path,
908 int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
909 struct btrfs_root *root);
910 int btrfs_copy_pinned(struct btrfs_root *root, struct extent_map_tree *copy);
911 struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
914 struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
915 struct btrfs_block_group_cache
916 *hint, u64 search_start,
917 int data, int owner);
918 int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
919 struct btrfs_root *root, u64 owner_objectid);
920 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
921 struct btrfs_root *root, u32 size,
923 u64 hint, u64 empty_size);
924 struct extent_buffer *__btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
925 struct btrfs_root *root,
933 int btrfs_grow_extent_tree(struct btrfs_trans_handle *trans,
934 struct btrfs_root *root, u64 new_size);
935 int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 new_size);
936 int btrfs_insert_extent_backref(struct btrfs_trans_handle *trans,
937 struct btrfs_root *root,
938 struct btrfs_path *path, u64 bytenr,
939 u64 root_objectid, u64 ref_generation,
940 u64 owner, u64 owner_offset);
941 int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
942 struct btrfs_root *root,
943 u64 num_bytes, u64 root_objectid, u64 ref_generation,
944 u64 owner, u64 owner_offset,
945 u64 empty_size, u64 hint_byte,
946 u64 search_end, struct btrfs_key *ins, int data);
947 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
948 struct extent_buffer *buf);
949 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
950 *root, u64 bytenr, u64 num_bytes,
951 u64 root_objectid, u64 ref_generation,
952 u64 owner_objectid, u64 owner_offset, int pin);
953 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
954 struct btrfs_root *root,
955 struct extent_map_tree *unpin);
956 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
957 struct btrfs_root *root,
958 u64 bytenr, u64 num_bytes,
959 u64 root_objectid, u64 ref_generation,
960 u64 owner, u64 owner_offset);
961 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
962 struct btrfs_root *root);
963 int btrfs_free_block_groups(struct btrfs_fs_info *info);
964 int btrfs_read_block_groups(struct btrfs_root *root);
965 int btrfs_make_block_groups(struct btrfs_trans_handle *trans,
966 struct btrfs_root *root);
967 u64 btrfs_hash_extent_ref(u64 root_objectid, u64 ref_generation,
968 u64 owner, u64 owner_offset);
969 int btrfs_update_block_group(struct btrfs_trans_handle *trans,
970 struct btrfs_root *root, u64 bytenr, u64 num,
971 int alloc, int mark_free, int data);
973 int btrfs_comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2);
974 int btrfs_cow_block(struct btrfs_trans_handle *trans,
975 struct btrfs_root *root, struct extent_buffer *buf,
976 struct extent_buffer *parent, int parent_slot,
977 struct extent_buffer **cow_ret);
978 int btrfs_copy_root(struct btrfs_trans_handle *trans,
979 struct btrfs_root *root,
980 struct extent_buffer *buf,
981 struct extent_buffer **cow_ret, u64 new_root_objectid);
982 int btrfs_extend_item(struct btrfs_trans_handle *trans, struct btrfs_root
983 *root, struct btrfs_path *path, u32 data_size);
984 int btrfs_truncate_item(struct btrfs_trans_handle *trans,
985 struct btrfs_root *root,
986 struct btrfs_path *path,
987 u32 new_size, int from_end);
988 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
989 *root, struct btrfs_key *key, struct btrfs_path *p, int
991 int btrfs_realloc_node(struct btrfs_trans_handle *trans,
992 struct btrfs_root *root, struct extent_buffer *parent,
993 int start_slot, int cache_only, u64 *last_ret,
994 struct btrfs_key *progress);
995 void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p);
996 struct btrfs_path *btrfs_alloc_path(void);
997 void btrfs_free_path(struct btrfs_path *p);
998 void btrfs_init_path(struct btrfs_path *p);
999 int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1000 struct btrfs_path *path);
1001 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
1002 *root, struct btrfs_key *key, void *data, u32 data_size);
1003 int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root
1004 *root, struct btrfs_path *path, struct btrfs_key
1005 *cpu_key, u32 data_size);
1006 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path);
1007 int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path);
1008 int btrfs_leaf_free_space(struct btrfs_root *root, struct extent_buffer *leaf);
1009 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
1014 int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1015 struct btrfs_key *key);
1016 int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
1017 *root, struct btrfs_key *key, struct btrfs_root_item
1019 int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
1020 *root, struct btrfs_key *key, struct btrfs_root_item
1022 int btrfs_find_last_root(struct btrfs_root *root, u64 objectid, struct
1023 btrfs_root_item *item, struct btrfs_key *key);
1024 int btrfs_find_dead_roots(struct btrfs_root *root, u64 objectid,
1025 struct btrfs_root *latest_root);
1027 int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
1028 *root, const char *name, int name_len, u64 dir,
1029 struct btrfs_key *location, u8 type);
1030 struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
1031 struct btrfs_root *root,
1032 struct btrfs_path *path, u64 dir,
1033 const char *name, int name_len,
1035 struct btrfs_dir_item *
1036 btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans,
1037 struct btrfs_root *root,
1038 struct btrfs_path *path, u64 dir,
1039 u64 objectid, const char *name, int name_len,
1041 struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_root *root,
1042 struct btrfs_path *path,
1043 const char *name, int name_len);
1044 int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans,
1045 struct btrfs_root *root,
1046 struct btrfs_path *path,
1047 struct btrfs_dir_item *di);
1048 int btrfs_insert_xattr_item(struct btrfs_trans_handle *trans,
1049 struct btrfs_root *root, const char *name,
1050 u16 name_len, const void *data, u16 data_len,
1052 struct btrfs_dir_item *btrfs_lookup_xattr(struct btrfs_trans_handle *trans,
1053 struct btrfs_root *root,
1054 struct btrfs_path *path, u64 dir,
1055 const char *name, u16 name_len,
1058 int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
1059 struct btrfs_root *fs_root,
1060 u64 dirid, u64 *objectid);
1061 int btrfs_find_highest_inode(struct btrfs_root *fs_root, u64 *objectid);
1064 int btrfs_insert_inode_ref(struct btrfs_trans_handle *trans,
1065 struct btrfs_root *root,
1066 const char *name, int name_len,
1067 u64 inode_objectid, u64 ref_objectid);
1068 int btrfs_del_inode_ref(struct btrfs_trans_handle *trans,
1069 struct btrfs_root *root,
1070 const char *name, int name_len,
1071 u64 inode_objectid, u64 ref_objectid);
1072 int btrfs_insert_empty_inode(struct btrfs_trans_handle *trans,
1073 struct btrfs_root *root,
1074 struct btrfs_path *path, u64 objectid);
1075 int btrfs_insert_inode(struct btrfs_trans_handle *trans, struct btrfs_root
1076 *root, u64 objectid, struct btrfs_inode_item
1078 int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root
1079 *root, struct btrfs_path *path,
1080 struct btrfs_key *location, int mod);
1083 int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
1084 struct btrfs_root *root,
1085 u64 objectid, u64 pos, u64 offset,
1088 int btrfs_insert_inline_extent(struct btrfs_trans_handle *trans,
1089 struct btrfs_root *root, u64 objectid,
1090 u64 offset, char *buffer, size_t size);
1091 int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
1092 struct btrfs_root *root,
1093 struct btrfs_path *path, u64 objectid,
1094 u64 bytenr, int mod);
1095 int btrfs_csum_file_block(struct btrfs_trans_handle *trans,
1096 struct btrfs_root *root,
1097 struct btrfs_inode_item *inode,
1098 u64 objectid, u64 offset,
1099 char *data, size_t len);
1100 struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans,
1101 struct btrfs_root *root,
1102 struct btrfs_path *path,
1103 u64 objectid, u64 offset,
1105 int btrfs_csum_truncate(struct btrfs_trans_handle *trans,
1106 struct btrfs_root *root, struct btrfs_path *path,