4 struct btrfs_trans_handle;
6 #define BTRFS_MAGIC "_BtRfS_M"
8 #define BTRFS_ROOT_TREE_OBJECTID 1
9 #define BTRFS_EXTENT_TREE_OBJECTID 2
10 #define BTRFS_INODE_MAP_OBJECTID 3
11 #define BTRFS_FS_TREE_OBJECTID 4
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.
21 * offset is the starting byte offset for this key in the stream.
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)
27 struct btrfs_disk_key {
31 } __attribute__ ((__packed__));
37 } __attribute__ ((__packed__));
40 * every tree block (leaf or node) starts with this header.
43 u8 fsid[16]; /* FS specific uuid */
44 __le64 blocknr; /* which block this node is supposed to live in */
45 __le64 parentid; /* objectid of the tree root */
50 /* generation flags to be added */
51 } __attribute__ ((__packed__));
53 #define BTRFS_MAX_LEVEL 8
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))
62 * the super block basically lists the main trees of the FS
63 * it currently lacks any block count etc etc
65 struct btrfs_super_block {
66 u8 fsid[16]; /* FS specific uuid */
67 __le64 blocknr; /* this block number */
75 __le64 root_dir_objectid;
76 } __attribute__ ((__packed__));
79 * A leaf is full of items. offset and size tell us where to find
80 * the item in the leaf (relative to the start of the data area)
83 struct btrfs_disk_key key;
86 } __attribute__ ((__packed__));
89 * leaves have an item area and a data area:
90 * [item0, item1....itemN] [free space] [dataN...data1, data0]
92 * The data is separate from the items to get the keys closer together
96 struct btrfs_header header;
97 struct btrfs_item items[];
98 } __attribute__ ((__packed__));
101 * all non-leaf blocks are nodes, they hold only keys and pointers to
104 struct btrfs_key_ptr {
105 struct btrfs_disk_key key;
107 } __attribute__ ((__packed__));
110 struct btrfs_header header;
111 struct btrfs_key_ptr ptrs[];
112 } __attribute__ ((__packed__));
115 * btrfs_paths remember the path taken from the root down to the leaf.
116 * level 0 is always the leaf, and nodes[1...BTRFS_MAX_LEVEL] will point
117 * to any other levels that are present.
119 * The slots array records the index of the item or block pointer
120 * used while walking the tree.
123 struct btrfs_buffer *nodes[BTRFS_MAX_LEVEL];
124 int slots[BTRFS_MAX_LEVEL];
128 * items in the extent btree are used to record the objectid of the
129 * owner of the block and the number of references
131 struct btrfs_extent_item {
134 } __attribute__ ((__packed__));
136 struct btrfs_inode_timespec {
139 } __attribute__ ((__packed__));
142 * there is no padding here on purpose. If you want to extent the inode,
143 * make a new item type
145 struct btrfs_inode_item {
156 struct btrfs_inode_timespec atime;
157 struct btrfs_inode_timespec ctime;
158 struct btrfs_inode_timespec mtime;
159 struct btrfs_inode_timespec otime;
160 } __attribute__ ((__packed__));
162 /* inline data is just a blob of bytes */
163 struct btrfs_inline_data_item {
165 } __attribute__ ((__packed__));
167 struct btrfs_dir_item {
172 } __attribute__ ((__packed__));
174 struct btrfs_root_item {
180 } __attribute__ ((__packed__));
182 struct btrfs_file_extent_item {
184 * disk space consumed by the extent, checksum blocks are included
188 __le64 disk_num_blocks;
190 * the logical offset in file bytes (no csums)
191 * this extent record is for. This allows a file extent to point
192 * into the middle of an existing extent on disk, sharing it
193 * between two snapshots (useful if some bytes in the middle of the
194 * extent have changed
198 * the logical number of file blocks (no csums included)
201 } __attribute__ ((__packed__));
203 struct btrfs_inode_map_item {
204 struct btrfs_disk_key key;
205 } __attribute__ ((__packed__));
207 struct btrfs_fs_info {
208 struct btrfs_root *fs_root;
209 struct btrfs_root *extent_root;
210 struct btrfs_root *tree_root;
211 struct btrfs_root *inode_root;
212 struct btrfs_key current_insert;
213 struct btrfs_key last_insert;
214 struct radix_tree_root cache_radix;
215 struct radix_tree_root pinned_radix;
216 struct list_head trans;
217 struct list_head cache;
218 u64 last_inode_alloc;
219 u64 last_inode_alloc_dirid;
223 struct btrfs_trans_handle *running_transaction;
224 struct btrfs_super_block *disk_super;
228 * in ram representation of the tree. extent_root is used for all allocations
229 * and for the extent tree extent_root root. current_insert is used
230 * only for the extent tree.
233 struct btrfs_buffer *node;
234 struct btrfs_buffer *commit_root;
235 struct btrfs_root_item root_item;
236 struct btrfs_key root_key;
237 struct btrfs_fs_info *fs_info;
243 /* the lower bits in the key flags defines the item type */
244 #define BTRFS_KEY_TYPE_MAX 256
245 #define BTRFS_KEY_TYPE_MASK (BTRFS_KEY_TYPE_MAX - 1)
248 * inode items have the data typically returned from stat and store other
249 * info about object characteristics. There is one for every file and dir in
252 #define BTRFS_INODE_ITEM_KEY 1
255 * dir items are the name -> inode pointers in a directory. There is one
256 * for every name in a directory.
258 #define BTRFS_DIR_ITEM_KEY 2
260 * inline data is file data that fits in the btree.
262 #define BTRFS_INLINE_DATA_KEY 3
264 * extent data is for data that can't fit in the btree. It points to
265 * a (hopefully) huge chunk of disk
267 #define BTRFS_EXTENT_DATA_KEY 4
269 * root items point to tree roots. There are typically in the root
270 * tree used by the super block to find all the other trees
272 #define BTRFS_ROOT_ITEM_KEY 5
274 * extent items are in the extent map tree. These record which blocks
275 * are used, and how many references there are to each block
277 #define BTRFS_EXTENT_ITEM_KEY 6
280 * the inode map records which inode numbers are in use and where
281 * they actually live on disk
283 #define BTRFS_INODE_MAP_ITEM_KEY 7
285 * string items are for debugging. They just store a short string of
288 #define BTRFS_STRING_ITEM_KEY 8
290 static inline u64 btrfs_inode_generation(struct btrfs_inode_item *i)
292 return le64_to_cpu(i->generation);
295 static inline void btrfs_set_inode_generation(struct btrfs_inode_item *i,
298 i->generation = cpu_to_le64(val);
301 static inline u64 btrfs_inode_size(struct btrfs_inode_item *i)
303 return le64_to_cpu(i->size);
306 static inline void btrfs_set_inode_size(struct btrfs_inode_item *i, u64 val)
308 i->size = cpu_to_le64(val);
311 static inline u64 btrfs_inode_nblocks(struct btrfs_inode_item *i)
313 return le64_to_cpu(i->nblocks);
316 static inline void btrfs_set_inode_nblocks(struct btrfs_inode_item *i, u64 val)
318 i->nblocks = cpu_to_le64(val);
321 static inline u32 btrfs_inode_nlink(struct btrfs_inode_item *i)
323 return le32_to_cpu(i->nlink);
326 static inline void btrfs_set_inode_nlink(struct btrfs_inode_item *i, u32 val)
328 i->nlink = cpu_to_le32(val);
331 static inline u32 btrfs_inode_uid(struct btrfs_inode_item *i)
333 return le32_to_cpu(i->uid);
336 static inline void btrfs_set_inode_uid(struct btrfs_inode_item *i, u32 val)
338 i->uid = cpu_to_le32(val);
341 static inline u32 btrfs_inode_gid(struct btrfs_inode_item *i)
343 return le32_to_cpu(i->gid);
346 static inline void btrfs_set_inode_gid(struct btrfs_inode_item *i, u32 val)
348 i->gid = cpu_to_le32(val);
351 static inline u32 btrfs_inode_mode(struct btrfs_inode_item *i)
353 return le32_to_cpu(i->mode);
356 static inline void btrfs_set_inode_mode(struct btrfs_inode_item *i, u32 val)
358 i->mode = cpu_to_le32(val);
361 static inline u32 btrfs_inode_rdev(struct btrfs_inode_item *i)
363 return le32_to_cpu(i->rdev);
366 static inline void btrfs_set_inode_rdev(struct btrfs_inode_item *i, u32 val)
368 i->rdev = cpu_to_le32(val);
371 static inline u16 btrfs_inode_flags(struct btrfs_inode_item *i)
373 return le16_to_cpu(i->flags);
376 static inline void btrfs_set_inode_flags(struct btrfs_inode_item *i, u16 val)
378 i->flags = cpu_to_le16(val);
381 static inline u16 btrfs_inode_compat_flags(struct btrfs_inode_item *i)
383 return le16_to_cpu(i->compat_flags);
386 static inline void btrfs_set_inode_compat_flags(struct btrfs_inode_item *i,
389 i->compat_flags = cpu_to_le16(val);
393 static inline u64 btrfs_extent_owner(struct btrfs_extent_item *ei)
395 return le64_to_cpu(ei->owner);
398 static inline void btrfs_set_extent_owner(struct btrfs_extent_item *ei, u64 val)
400 ei->owner = cpu_to_le64(val);
403 static inline u32 btrfs_extent_refs(struct btrfs_extent_item *ei)
405 return le32_to_cpu(ei->refs);
408 static inline void btrfs_set_extent_refs(struct btrfs_extent_item *ei, u32 val)
410 ei->refs = cpu_to_le32(val);
413 static inline u64 btrfs_node_blockptr(struct btrfs_node *n, int nr)
415 return le64_to_cpu(n->ptrs[nr].blockptr);
418 static inline void btrfs_set_node_blockptr(struct btrfs_node *n, int nr,
421 n->ptrs[nr].blockptr = cpu_to_le64(val);
424 static inline u32 btrfs_item_offset(struct btrfs_item *item)
426 return le32_to_cpu(item->offset);
429 static inline void btrfs_set_item_offset(struct btrfs_item *item, u32 val)
431 item->offset = cpu_to_le32(val);
434 static inline u32 btrfs_item_end(struct btrfs_item *item)
436 return le32_to_cpu(item->offset) + le16_to_cpu(item->size);
439 static inline u16 btrfs_item_size(struct btrfs_item *item)
441 return le16_to_cpu(item->size);
444 static inline void btrfs_set_item_size(struct btrfs_item *item, u16 val)
446 item->size = cpu_to_le16(val);
449 static inline u64 btrfs_dir_objectid(struct btrfs_dir_item *d)
451 return le64_to_cpu(d->objectid);
454 static inline void btrfs_set_dir_objectid(struct btrfs_dir_item *d, u64 val)
456 d->objectid = cpu_to_le64(val);
459 static inline u16 btrfs_dir_flags(struct btrfs_dir_item *d)
461 return le16_to_cpu(d->flags);
464 static inline void btrfs_set_dir_flags(struct btrfs_dir_item *d, u16 val)
466 d->flags = cpu_to_le16(val);
469 static inline u8 btrfs_dir_type(struct btrfs_dir_item *d)
474 static inline void btrfs_set_dir_type(struct btrfs_dir_item *d, u8 val)
479 static inline u16 btrfs_dir_name_len(struct btrfs_dir_item *d)
481 return le16_to_cpu(d->name_len);
484 static inline void btrfs_set_dir_name_len(struct btrfs_dir_item *d, u16 val)
486 d->name_len = cpu_to_le16(val);
489 static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu,
490 struct btrfs_disk_key *disk)
492 cpu->offset = le64_to_cpu(disk->offset);
493 cpu->flags = le32_to_cpu(disk->flags);
494 cpu->objectid = le64_to_cpu(disk->objectid);
497 static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk,
498 struct btrfs_key *cpu)
500 disk->offset = cpu_to_le64(cpu->offset);
501 disk->flags = cpu_to_le32(cpu->flags);
502 disk->objectid = cpu_to_le64(cpu->objectid);
505 static inline u64 btrfs_disk_key_objectid(struct btrfs_disk_key *disk)
507 return le64_to_cpu(disk->objectid);
510 static inline void btrfs_set_disk_key_objectid(struct btrfs_disk_key *disk,
513 disk->objectid = cpu_to_le64(val);
516 static inline u64 btrfs_disk_key_offset(struct btrfs_disk_key *disk)
518 return le64_to_cpu(disk->offset);
521 static inline void btrfs_set_disk_key_offset(struct btrfs_disk_key *disk,
524 disk->offset = cpu_to_le64(val);
527 static inline u32 btrfs_disk_key_flags(struct btrfs_disk_key *disk)
529 return le32_to_cpu(disk->flags);
532 static inline void btrfs_set_disk_key_flags(struct btrfs_disk_key *disk,
535 disk->flags = cpu_to_le32(val);
538 static inline u32 btrfs_key_type(struct btrfs_key *key)
540 return key->flags & BTRFS_KEY_TYPE_MASK;
543 static inline u32 btrfs_disk_key_type(struct btrfs_disk_key *key)
545 return le32_to_cpu(key->flags) & BTRFS_KEY_TYPE_MASK;
548 static inline void btrfs_set_key_type(struct btrfs_key *key, u32 type)
550 BUG_ON(type >= BTRFS_KEY_TYPE_MAX);
551 key->flags = (key->flags & ~((u64)BTRFS_KEY_TYPE_MASK)) | type;
554 static inline void btrfs_set_disk_key_type(struct btrfs_disk_key *key, u32 type)
556 u32 flags = btrfs_disk_key_flags(key);
557 BUG_ON(type >= BTRFS_KEY_TYPE_MAX);
558 flags = (flags & ~((u64)BTRFS_KEY_TYPE_MASK)) | type;
559 btrfs_set_disk_key_flags(key, flags);
562 static inline u64 btrfs_header_blocknr(struct btrfs_header *h)
564 return le64_to_cpu(h->blocknr);
567 static inline void btrfs_set_header_blocknr(struct btrfs_header *h, u64 blocknr)
569 h->blocknr = cpu_to_le64(blocknr);
572 static inline u64 btrfs_header_parentid(struct btrfs_header *h)
574 return le64_to_cpu(h->parentid);
577 static inline void btrfs_set_header_parentid(struct btrfs_header *h,
580 h->parentid = cpu_to_le64(parentid);
583 static inline u16 btrfs_header_nritems(struct btrfs_header *h)
585 return le16_to_cpu(h->nritems);
588 static inline void btrfs_set_header_nritems(struct btrfs_header *h, u16 val)
590 h->nritems = cpu_to_le16(val);
593 static inline u16 btrfs_header_flags(struct btrfs_header *h)
595 return le16_to_cpu(h->flags);
598 static inline void btrfs_set_header_flags(struct btrfs_header *h, u16 val)
600 h->flags = cpu_to_le16(val);
603 static inline int btrfs_header_level(struct btrfs_header *h)
605 return btrfs_header_flags(h) & (BTRFS_MAX_LEVEL - 1);
608 static inline void btrfs_set_header_level(struct btrfs_header *h, int level)
611 BUG_ON(level > BTRFS_MAX_LEVEL);
612 flags = btrfs_header_flags(h) & ~(BTRFS_MAX_LEVEL - 1);
613 btrfs_set_header_flags(h, flags | level);
616 static inline int btrfs_is_leaf(struct btrfs_node *n)
618 return (btrfs_header_level(&n->header) == 0);
621 static inline u64 btrfs_root_blocknr(struct btrfs_root_item *item)
623 return le64_to_cpu(item->blocknr);
626 static inline void btrfs_set_root_blocknr(struct btrfs_root_item *item, u64 val)
628 item->blocknr = cpu_to_le64(val);
631 static inline u32 btrfs_root_refs(struct btrfs_root_item *item)
633 return le32_to_cpu(item->refs);
636 static inline void btrfs_set_root_refs(struct btrfs_root_item *item, u32 val)
638 item->refs = cpu_to_le32(val);
641 static inline u64 btrfs_super_blocknr(struct btrfs_super_block *s)
643 return le64_to_cpu(s->blocknr);
646 static inline void btrfs_set_super_blocknr(struct btrfs_super_block *s, u64 val)
648 s->blocknr = cpu_to_le64(val);
651 static inline u64 btrfs_super_root(struct btrfs_super_block *s)
653 return le64_to_cpu(s->root);
656 static inline void btrfs_set_super_root(struct btrfs_super_block *s, u64 val)
658 s->root = cpu_to_le64(val);
661 static inline u64 btrfs_super_total_blocks(struct btrfs_super_block *s)
663 return le64_to_cpu(s->total_blocks);
666 static inline void btrfs_set_super_total_blocks(struct btrfs_super_block *s,
669 s->total_blocks = cpu_to_le64(val);
672 static inline u64 btrfs_super_blocks_used(struct btrfs_super_block *s)
674 return le64_to_cpu(s->blocks_used);
677 static inline void btrfs_set_super_blocks_used(struct btrfs_super_block *s,
680 s->blocks_used = cpu_to_le64(val);
683 static inline u32 btrfs_super_blocksize(struct btrfs_super_block *s)
685 return le32_to_cpu(s->blocksize);
688 static inline void btrfs_set_super_blocksize(struct btrfs_super_block *s,
691 s->blocksize = cpu_to_le32(val);
694 static inline u64 btrfs_super_root_dir(struct btrfs_super_block *s)
696 return le64_to_cpu(s->root_dir_objectid);
699 static inline void btrfs_set_super_root_dir(struct btrfs_super_block *s, u64
702 s->root_dir_objectid = cpu_to_le64(val);
705 static inline u8 *btrfs_leaf_data(struct btrfs_leaf *l)
707 return (u8 *)l->items;
710 static inline u64 btrfs_file_extent_disk_blocknr(struct btrfs_file_extent_item
713 return le64_to_cpu(e->disk_blocknr);
716 static inline void btrfs_set_file_extent_disk_blocknr(struct
717 btrfs_file_extent_item
720 e->disk_blocknr = cpu_to_le64(val);
723 static inline u64 btrfs_file_extent_disk_num_blocks(struct
724 btrfs_file_extent_item *e)
726 return le64_to_cpu(e->disk_num_blocks);
729 static inline void btrfs_set_file_extent_disk_num_blocks(struct
730 btrfs_file_extent_item
733 e->disk_num_blocks = cpu_to_le64(val);
736 static inline u64 btrfs_file_extent_offset(struct btrfs_file_extent_item *e)
738 return le64_to_cpu(e->offset);
741 static inline void btrfs_set_file_extent_offset(struct btrfs_file_extent_item
744 e->offset = cpu_to_le64(val);
747 static inline u64 btrfs_file_extent_num_blocks(struct btrfs_file_extent_item
750 return le64_to_cpu(e->num_blocks);
753 static inline void btrfs_set_file_extent_num_blocks(struct
754 btrfs_file_extent_item *e,
757 e->num_blocks = cpu_to_le64(val);
760 /* helper function to cast into the data area of the leaf. */
761 #define btrfs_item_ptr(leaf, slot, type) \
762 ((type *)(btrfs_leaf_data(leaf) + \
763 btrfs_item_offset((leaf)->items + (slot))))
765 struct btrfs_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
766 struct btrfs_root *root);
767 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
768 struct btrfs_buffer *buf);
769 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
770 *root, u64 blocknr, u64 num_blocks, int pin);
771 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
772 *root, struct btrfs_key *key, struct btrfs_path *p, int
774 void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p);
775 void btrfs_init_path(struct btrfs_path *p);
776 int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
777 struct btrfs_path *path);
778 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
779 *root, struct btrfs_key *key, void *data, u32 data_size);
780 int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root
781 *root, struct btrfs_path *path, struct btrfs_key
782 *cpu_key, u32 data_size);
783 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path);
784 int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf);
785 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
786 *root, struct btrfs_buffer *snap);
787 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
789 int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
790 struct btrfs_key *key);
791 int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
792 *root, struct btrfs_key *key, struct btrfs_root_item
794 int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
795 *root, struct btrfs_key *key, struct btrfs_root_item
797 int btrfs_find_last_root(struct btrfs_root *root, u64 objectid, struct
798 btrfs_root_item *item, struct btrfs_key *key);
799 int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
800 *root, char *name, int name_len, u64 dir, u64
802 int btrfs_lookup_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
803 *root, struct btrfs_path *path, u64 dir, char *name,
804 int name_len, int mod);
805 int btrfs_match_dir_item_name(struct btrfs_root *root, struct btrfs_path *path,
806 char *name, int name_len);
807 int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
808 struct btrfs_root *fs_root,
809 u64 dirid, u64 *objectid);
810 int btrfs_insert_inode_map(struct btrfs_trans_handle *trans,
811 struct btrfs_root *root,
812 u64 objectid, struct btrfs_key *location);
813 int btrfs_lookup_inode_map(struct btrfs_trans_handle *trans,
814 struct btrfs_root *root, struct btrfs_path *path,
815 u64 objectid, int mod);
816 int btrfs_insert_inode(struct btrfs_trans_handle *trans, struct btrfs_root
817 *root, u64 objectid, struct btrfs_inode_item
819 int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root
820 *root, struct btrfs_path *path, u64 objectid, int mod);