7 struct btrfs_trans_handle;
8 struct btrfs_transaction;
10 #define BTRFS_MAGIC "_BtRfS_M"
12 #define BTRFS_ROOT_TREE_OBJECTID 1
13 #define BTRFS_EXTENT_TREE_OBJECTID 2
14 #define BTRFS_INODE_MAP_OBJECTID 3
15 #define BTRFS_FS_TREE_OBJECTID 4
18 * we can actually store much bigger names, but lets not confuse the rest
21 #define BTRFS_NAME_LEN 255
24 * the key defines the order in the tree, and so it also defines (optimal)
25 * block layout. objectid corresonds to the inode number. The flags
26 * tells us things about the object, and is a kind of stream selector.
27 * so for a given inode, keys with flags of 1 might refer to the inode
28 * data, flags of 2 may point to file data in the btree and flags == 3
29 * may point to extents.
31 * offset is the starting byte offset for this key in the stream.
33 * btrfs_disk_key is in disk byte order. struct btrfs_key is always
34 * in cpu native order. Otherwise they are identical and their sizes
35 * should be the same (ie both packed)
37 struct btrfs_disk_key {
41 } __attribute__ ((__packed__));
47 } __attribute__ ((__packed__));
50 * every tree block (leaf or node) starts with this header.
53 u8 fsid[16]; /* FS specific uuid */
54 __le64 blocknr; /* which block this node is supposed to live in */
56 __le64 parentid; /* objectid of the tree root */
61 /* generation flags to be added */
62 } __attribute__ ((__packed__));
64 #define BTRFS_MAX_LEVEL 8
65 #define BTRFS_NODEPTRS_PER_BLOCK(r) (((r)->blocksize - \
66 sizeof(struct btrfs_header)) / \
67 (sizeof(struct btrfs_disk_key) + sizeof(u64)))
68 #define __BTRFS_LEAF_DATA_SIZE(bs) ((bs) - sizeof(struct btrfs_header))
69 #define BTRFS_LEAF_DATA_SIZE(r) (__BTRFS_LEAF_DATA_SIZE(r->blocksize))
73 * the super block basically lists the main trees of the FS
74 * it currently lacks any block count etc etc
76 struct btrfs_super_block {
77 u8 fsid[16]; /* FS specific uuid */
78 __le64 blocknr; /* this block number */
86 __le64 root_dir_objectid;
87 } __attribute__ ((__packed__));
90 * A leaf is full of items. offset and size tell us where to find
91 * the item in the leaf (relative to the start of the data area)
94 struct btrfs_disk_key key;
97 } __attribute__ ((__packed__));
100 * leaves have an item area and a data area:
101 * [item0, item1....itemN] [free space] [dataN...data1, data0]
103 * The data is separate from the items to get the keys closer together
107 struct btrfs_header header;
108 struct btrfs_item items[];
109 } __attribute__ ((__packed__));
112 * all non-leaf blocks are nodes, they hold only keys and pointers to
115 struct btrfs_key_ptr {
116 struct btrfs_disk_key key;
118 } __attribute__ ((__packed__));
121 struct btrfs_header header;
122 struct btrfs_key_ptr ptrs[];
123 } __attribute__ ((__packed__));
126 * btrfs_paths remember the path taken from the root down to the leaf.
127 * level 0 is always the leaf, and nodes[1...BTRFS_MAX_LEVEL] will point
128 * to any other levels that are present.
130 * The slots array records the index of the item or block pointer
131 * used while walking the tree.
134 struct buffer_head *nodes[BTRFS_MAX_LEVEL];
135 int slots[BTRFS_MAX_LEVEL];
139 * items in the extent btree are used to record the objectid of the
140 * owner of the block and the number of references
142 struct btrfs_extent_item {
145 } __attribute__ ((__packed__));
147 struct btrfs_inode_timespec {
150 } __attribute__ ((__packed__));
153 * there is no padding here on purpose. If you want to extent the inode,
154 * make a new item type
156 struct btrfs_inode_item {
167 struct btrfs_inode_timespec atime;
168 struct btrfs_inode_timespec ctime;
169 struct btrfs_inode_timespec mtime;
170 struct btrfs_inode_timespec otime;
171 } __attribute__ ((__packed__));
173 /* inline data is just a blob of bytes */
174 struct btrfs_inline_data_item {
176 } __attribute__ ((__packed__));
178 struct btrfs_dir_item {
183 } __attribute__ ((__packed__));
185 struct btrfs_root_item {
191 } __attribute__ ((__packed__));
193 struct btrfs_file_extent_item {
195 * disk space consumed by the extent, checksum blocks are included
199 __le64 disk_num_blocks;
201 * the logical offset in file bytes (no csums)
202 * this extent record is for. This allows a file extent to point
203 * into the middle of an existing extent on disk, sharing it
204 * between two snapshots (useful if some bytes in the middle of the
205 * extent have changed
209 * the logical number of file blocks (no csums included)
212 } __attribute__ ((__packed__));
214 struct btrfs_inode_map_item {
215 struct btrfs_disk_key key;
216 } __attribute__ ((__packed__));
218 struct btrfs_fs_info {
219 struct btrfs_root *fs_root;
220 struct btrfs_root *extent_root;
221 struct btrfs_root *tree_root;
222 struct btrfs_root *inode_root;
223 struct btrfs_key current_insert;
224 struct btrfs_key last_insert;
225 struct radix_tree_root pending_del_radix;
226 struct radix_tree_root pinned_radix;
227 u64 last_inode_alloc;
228 u64 last_inode_alloc_dirid;
230 struct btrfs_transaction *running_transaction;
231 struct btrfs_super_block *disk_super;
232 struct buffer_head *sb_buffer;
233 struct super_block *sb;
234 struct mutex trans_mutex;
235 struct mutex fs_mutex;
239 * in ram representation of the tree. extent_root is used for all allocations
240 * and for the extent tree extent_root root. current_insert is used
241 * only for the extent tree.
244 struct buffer_head *node;
245 struct buffer_head *commit_root;
246 struct btrfs_root_item root_item;
247 struct btrfs_key root_key;
248 struct btrfs_fs_info *fs_info;
254 /* the lower bits in the key flags defines the item type */
255 #define BTRFS_KEY_TYPE_MAX 256
256 #define BTRFS_KEY_TYPE_MASK (BTRFS_KEY_TYPE_MAX - 1)
259 * inode items have the data typically returned from stat and store other
260 * info about object characteristics. There is one for every file and dir in
263 #define BTRFS_INODE_ITEM_KEY 1
266 * dir items are the name -> inode pointers in a directory. There is one
267 * for every name in a directory.
269 #define BTRFS_DIR_ITEM_KEY 2
271 * inline data is file data that fits in the btree.
273 #define BTRFS_INLINE_DATA_KEY 3
275 * extent data is for data that can't fit in the btree. It points to
276 * a (hopefully) huge chunk of disk
278 #define BTRFS_EXTENT_DATA_KEY 4
280 * root items point to tree roots. There are typically in the root
281 * tree used by the super block to find all the other trees
283 #define BTRFS_ROOT_ITEM_KEY 5
285 * extent items are in the extent map tree. These record which blocks
286 * are used, and how many references there are to each block
288 #define BTRFS_EXTENT_ITEM_KEY 6
291 * the inode map records which inode numbers are in use and where
292 * they actually live on disk
294 #define BTRFS_INODE_MAP_ITEM_KEY 7
296 * string items are for debugging. They just store a short string of
299 #define BTRFS_STRING_ITEM_KEY 8
301 static inline u64 btrfs_inode_generation(struct btrfs_inode_item *i)
303 return le64_to_cpu(i->generation);
306 static inline void btrfs_set_inode_generation(struct btrfs_inode_item *i,
309 i->generation = cpu_to_le64(val);
312 static inline u64 btrfs_inode_size(struct btrfs_inode_item *i)
314 return le64_to_cpu(i->size);
317 static inline void btrfs_set_inode_size(struct btrfs_inode_item *i, u64 val)
319 i->size = cpu_to_le64(val);
322 static inline u64 btrfs_inode_nblocks(struct btrfs_inode_item *i)
324 return le64_to_cpu(i->nblocks);
327 static inline void btrfs_set_inode_nblocks(struct btrfs_inode_item *i, u64 val)
329 i->nblocks = cpu_to_le64(val);
332 static inline u32 btrfs_inode_nlink(struct btrfs_inode_item *i)
334 return le32_to_cpu(i->nlink);
337 static inline void btrfs_set_inode_nlink(struct btrfs_inode_item *i, u32 val)
339 i->nlink = cpu_to_le32(val);
342 static inline u32 btrfs_inode_uid(struct btrfs_inode_item *i)
344 return le32_to_cpu(i->uid);
347 static inline void btrfs_set_inode_uid(struct btrfs_inode_item *i, u32 val)
349 i->uid = cpu_to_le32(val);
352 static inline u32 btrfs_inode_gid(struct btrfs_inode_item *i)
354 return le32_to_cpu(i->gid);
357 static inline void btrfs_set_inode_gid(struct btrfs_inode_item *i, u32 val)
359 i->gid = cpu_to_le32(val);
362 static inline u32 btrfs_inode_mode(struct btrfs_inode_item *i)
364 return le32_to_cpu(i->mode);
367 static inline void btrfs_set_inode_mode(struct btrfs_inode_item *i, u32 val)
369 i->mode = cpu_to_le32(val);
372 static inline u32 btrfs_inode_rdev(struct btrfs_inode_item *i)
374 return le32_to_cpu(i->rdev);
377 static inline void btrfs_set_inode_rdev(struct btrfs_inode_item *i, u32 val)
379 i->rdev = cpu_to_le32(val);
382 static inline u16 btrfs_inode_flags(struct btrfs_inode_item *i)
384 return le16_to_cpu(i->flags);
387 static inline void btrfs_set_inode_flags(struct btrfs_inode_item *i, u16 val)
389 i->flags = cpu_to_le16(val);
392 static inline u16 btrfs_inode_compat_flags(struct btrfs_inode_item *i)
394 return le16_to_cpu(i->compat_flags);
397 static inline void btrfs_set_inode_compat_flags(struct btrfs_inode_item *i,
400 i->compat_flags = cpu_to_le16(val);
403 static inline u32 btrfs_timespec_sec(struct btrfs_inode_timespec *ts)
405 return le32_to_cpu(ts->sec);
408 static inline void btrfs_set_timespec_sec(struct btrfs_inode_timespec *ts,
411 ts->sec = cpu_to_le32(val);
414 static inline u32 btrfs_timespec_nsec(struct btrfs_inode_timespec *ts)
416 return le32_to_cpu(ts->nsec);
419 static inline void btrfs_set_timespec_nsec(struct btrfs_inode_timespec *ts,
422 ts->nsec = cpu_to_le32(val);
427 static inline u64 btrfs_extent_owner(struct btrfs_extent_item *ei)
429 return le64_to_cpu(ei->owner);
432 static inline void btrfs_set_extent_owner(struct btrfs_extent_item *ei, u64 val)
434 ei->owner = cpu_to_le64(val);
437 static inline u32 btrfs_extent_refs(struct btrfs_extent_item *ei)
439 return le32_to_cpu(ei->refs);
442 static inline void btrfs_set_extent_refs(struct btrfs_extent_item *ei, u32 val)
444 ei->refs = cpu_to_le32(val);
447 static inline u64 btrfs_node_blockptr(struct btrfs_node *n, int nr)
449 return le64_to_cpu(n->ptrs[nr].blockptr);
452 static inline void btrfs_set_node_blockptr(struct btrfs_node *n, int nr,
455 n->ptrs[nr].blockptr = cpu_to_le64(val);
458 static inline u32 btrfs_item_offset(struct btrfs_item *item)
460 return le32_to_cpu(item->offset);
463 static inline void btrfs_set_item_offset(struct btrfs_item *item, u32 val)
465 item->offset = cpu_to_le32(val);
468 static inline u32 btrfs_item_end(struct btrfs_item *item)
470 return le32_to_cpu(item->offset) + le16_to_cpu(item->size);
473 static inline u16 btrfs_item_size(struct btrfs_item *item)
475 return le16_to_cpu(item->size);
478 static inline void btrfs_set_item_size(struct btrfs_item *item, u16 val)
480 item->size = cpu_to_le16(val);
483 static inline u64 btrfs_dir_objectid(struct btrfs_dir_item *d)
485 return le64_to_cpu(d->objectid);
488 static inline void btrfs_set_dir_objectid(struct btrfs_dir_item *d, u64 val)
490 d->objectid = cpu_to_le64(val);
493 static inline u16 btrfs_dir_flags(struct btrfs_dir_item *d)
495 return le16_to_cpu(d->flags);
498 static inline void btrfs_set_dir_flags(struct btrfs_dir_item *d, u16 val)
500 d->flags = cpu_to_le16(val);
503 static inline u8 btrfs_dir_type(struct btrfs_dir_item *d)
508 static inline void btrfs_set_dir_type(struct btrfs_dir_item *d, u8 val)
513 static inline u16 btrfs_dir_name_len(struct btrfs_dir_item *d)
515 return le16_to_cpu(d->name_len);
518 static inline void btrfs_set_dir_name_len(struct btrfs_dir_item *d, u16 val)
520 d->name_len = cpu_to_le16(val);
523 static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu,
524 struct btrfs_disk_key *disk)
526 cpu->offset = le64_to_cpu(disk->offset);
527 cpu->flags = le32_to_cpu(disk->flags);
528 cpu->objectid = le64_to_cpu(disk->objectid);
531 static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk,
532 struct btrfs_key *cpu)
534 disk->offset = cpu_to_le64(cpu->offset);
535 disk->flags = cpu_to_le32(cpu->flags);
536 disk->objectid = cpu_to_le64(cpu->objectid);
539 static inline u64 btrfs_disk_key_objectid(struct btrfs_disk_key *disk)
541 return le64_to_cpu(disk->objectid);
544 static inline void btrfs_set_disk_key_objectid(struct btrfs_disk_key *disk,
547 disk->objectid = cpu_to_le64(val);
550 static inline u64 btrfs_disk_key_offset(struct btrfs_disk_key *disk)
552 return le64_to_cpu(disk->offset);
555 static inline void btrfs_set_disk_key_offset(struct btrfs_disk_key *disk,
558 disk->offset = cpu_to_le64(val);
561 static inline u32 btrfs_disk_key_flags(struct btrfs_disk_key *disk)
563 return le32_to_cpu(disk->flags);
566 static inline void btrfs_set_disk_key_flags(struct btrfs_disk_key *disk,
569 disk->flags = cpu_to_le32(val);
572 static inline u32 btrfs_key_type(struct btrfs_key *key)
574 return key->flags & BTRFS_KEY_TYPE_MASK;
577 static inline u32 btrfs_disk_key_type(struct btrfs_disk_key *key)
579 return le32_to_cpu(key->flags) & BTRFS_KEY_TYPE_MASK;
582 static inline void btrfs_set_key_type(struct btrfs_key *key, u32 type)
584 BUG_ON(type >= BTRFS_KEY_TYPE_MAX);
585 key->flags = (key->flags & ~((u64)BTRFS_KEY_TYPE_MASK)) | type;
588 static inline void btrfs_set_disk_key_type(struct btrfs_disk_key *key, u32 type)
590 u32 flags = btrfs_disk_key_flags(key);
591 BUG_ON(type >= BTRFS_KEY_TYPE_MAX);
592 flags = (flags & ~((u64)BTRFS_KEY_TYPE_MASK)) | type;
593 btrfs_set_disk_key_flags(key, flags);
596 static inline u64 btrfs_header_blocknr(struct btrfs_header *h)
598 return le64_to_cpu(h->blocknr);
601 static inline void btrfs_set_header_blocknr(struct btrfs_header *h, u64 blocknr)
603 h->blocknr = cpu_to_le64(blocknr);
606 static inline u64 btrfs_header_generation(struct btrfs_header *h)
608 return le64_to_cpu(h->generation);
611 static inline void btrfs_set_header_generation(struct btrfs_header *h,
614 h->generation = cpu_to_le64(val);
617 static inline u64 btrfs_header_parentid(struct btrfs_header *h)
619 return le64_to_cpu(h->parentid);
622 static inline void btrfs_set_header_parentid(struct btrfs_header *h,
625 h->parentid = cpu_to_le64(parentid);
628 static inline u16 btrfs_header_nritems(struct btrfs_header *h)
630 return le16_to_cpu(h->nritems);
633 static inline void btrfs_set_header_nritems(struct btrfs_header *h, u16 val)
635 h->nritems = cpu_to_le16(val);
638 static inline u16 btrfs_header_flags(struct btrfs_header *h)
640 return le16_to_cpu(h->flags);
643 static inline void btrfs_set_header_flags(struct btrfs_header *h, u16 val)
645 h->flags = cpu_to_le16(val);
648 static inline int btrfs_header_level(struct btrfs_header *h)
650 return btrfs_header_flags(h) & (BTRFS_MAX_LEVEL - 1);
653 static inline void btrfs_set_header_level(struct btrfs_header *h, int level)
656 BUG_ON(level > BTRFS_MAX_LEVEL);
657 flags = btrfs_header_flags(h) & ~(BTRFS_MAX_LEVEL - 1);
658 btrfs_set_header_flags(h, flags | level);
661 static inline int btrfs_is_leaf(struct btrfs_node *n)
663 return (btrfs_header_level(&n->header) == 0);
666 static inline u64 btrfs_root_blocknr(struct btrfs_root_item *item)
668 return le64_to_cpu(item->blocknr);
671 static inline void btrfs_set_root_blocknr(struct btrfs_root_item *item, u64 val)
673 item->blocknr = cpu_to_le64(val);
676 static inline u32 btrfs_root_refs(struct btrfs_root_item *item)
678 return le32_to_cpu(item->refs);
681 static inline void btrfs_set_root_refs(struct btrfs_root_item *item, u32 val)
683 item->refs = cpu_to_le32(val);
686 static inline u64 btrfs_super_blocknr(struct btrfs_super_block *s)
688 return le64_to_cpu(s->blocknr);
691 static inline void btrfs_set_super_blocknr(struct btrfs_super_block *s, u64 val)
693 s->blocknr = cpu_to_le64(val);
696 static inline u64 btrfs_super_root(struct btrfs_super_block *s)
698 return le64_to_cpu(s->root);
701 static inline void btrfs_set_super_root(struct btrfs_super_block *s, u64 val)
703 s->root = cpu_to_le64(val);
706 static inline u64 btrfs_super_total_blocks(struct btrfs_super_block *s)
708 return le64_to_cpu(s->total_blocks);
711 static inline void btrfs_set_super_total_blocks(struct btrfs_super_block *s,
714 s->total_blocks = cpu_to_le64(val);
717 static inline u64 btrfs_super_blocks_used(struct btrfs_super_block *s)
719 return le64_to_cpu(s->blocks_used);
722 static inline void btrfs_set_super_blocks_used(struct btrfs_super_block *s,
725 s->blocks_used = cpu_to_le64(val);
728 static inline u32 btrfs_super_blocksize(struct btrfs_super_block *s)
730 return le32_to_cpu(s->blocksize);
733 static inline void btrfs_set_super_blocksize(struct btrfs_super_block *s,
736 s->blocksize = cpu_to_le32(val);
739 static inline u64 btrfs_super_root_dir(struct btrfs_super_block *s)
741 return le64_to_cpu(s->root_dir_objectid);
744 static inline void btrfs_set_super_root_dir(struct btrfs_super_block *s, u64
747 s->root_dir_objectid = cpu_to_le64(val);
750 static inline u8 *btrfs_leaf_data(struct btrfs_leaf *l)
752 return (u8 *)l->items;
755 static inline u64 btrfs_file_extent_disk_blocknr(struct btrfs_file_extent_item
758 return le64_to_cpu(e->disk_blocknr);
761 static inline void btrfs_set_file_extent_disk_blocknr(struct
762 btrfs_file_extent_item
765 e->disk_blocknr = cpu_to_le64(val);
768 static inline u64 btrfs_file_extent_disk_num_blocks(struct
769 btrfs_file_extent_item *e)
771 return le64_to_cpu(e->disk_num_blocks);
774 static inline void btrfs_set_file_extent_disk_num_blocks(struct
775 btrfs_file_extent_item
778 e->disk_num_blocks = cpu_to_le64(val);
781 static inline u64 btrfs_file_extent_offset(struct btrfs_file_extent_item *e)
783 return le64_to_cpu(e->offset);
786 static inline void btrfs_set_file_extent_offset(struct btrfs_file_extent_item
789 e->offset = cpu_to_le64(val);
792 static inline u64 btrfs_file_extent_num_blocks(struct btrfs_file_extent_item
795 return le64_to_cpu(e->num_blocks);
798 static inline void btrfs_set_file_extent_num_blocks(struct
799 btrfs_file_extent_item *e,
802 e->num_blocks = cpu_to_le64(val);
805 static inline struct btrfs_root *btrfs_sb(struct super_block *sb)
807 return sb->s_fs_info;
810 /* helper function to cast into the data area of the leaf. */
811 #define btrfs_item_ptr(leaf, slot, type) \
812 ((type *)(btrfs_leaf_data(leaf) + \
813 btrfs_item_offset((leaf)->items + (slot))))
815 struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
816 struct btrfs_root *root);
817 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
818 struct buffer_head *buf);
819 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
820 *root, u64 blocknr, u64 num_blocks, int pin);
821 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
822 *root, struct btrfs_key *key, struct btrfs_path *p, int
824 void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p);
825 void btrfs_init_path(struct btrfs_path *p);
826 int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
827 struct btrfs_path *path);
828 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
829 *root, struct btrfs_key *key, void *data, u32 data_size);
830 int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root
831 *root, struct btrfs_path *path, struct btrfs_key
832 *cpu_key, u32 data_size);
833 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path);
834 int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf);
835 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
836 *root, struct buffer_head *snap);
837 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
839 int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
840 struct btrfs_key *key);
841 int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
842 *root, struct btrfs_key *key, struct btrfs_root_item
844 int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
845 *root, struct btrfs_key *key, struct btrfs_root_item
847 int btrfs_find_last_root(struct btrfs_root *root, u64 objectid, struct
848 btrfs_root_item *item, struct btrfs_key *key);
849 int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
850 *root, const char *name, int name_len, u64 dir, u64
852 int btrfs_lookup_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
853 *root, struct btrfs_path *path, u64 dir,
854 const char *name, int name_len, int mod);
855 int btrfs_match_dir_item_name(struct btrfs_root *root, struct btrfs_path *path,
856 const char *name, int name_len);
857 int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
858 struct btrfs_root *fs_root,
859 u64 dirid, u64 *objectid);
860 int btrfs_insert_inode_map(struct btrfs_trans_handle *trans,
861 struct btrfs_root *root,
862 u64 objectid, struct btrfs_key *location);
863 int btrfs_lookup_inode_map(struct btrfs_trans_handle *trans,
864 struct btrfs_root *root, struct btrfs_path *path,
865 u64 objectid, int mod);
866 int btrfs_insert_inode(struct btrfs_trans_handle *trans, struct btrfs_root
867 *root, u64 objectid, struct btrfs_inode_item
869 int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root
870 *root, struct btrfs_path *path, u64 objectid, int mod);