4 #include <linux/radix-tree.h>
7 struct btrfs_trans_handle;
9 #define BTRFS_MAGIC "_BtRfS_M"
11 #define BTRFS_ROOT_TREE_OBJECTID 1
12 #define BTRFS_EXTENT_TREE_OBJECTID 2
13 #define BTRFS_INODE_MAP_OBJECTID 3
14 #define BTRFS_FS_TREE_OBJECTID 4
17 * we can actually store much bigger names, but lets not confuse the rest
20 #define BTRFS_NAME_LEN 255
23 * the key defines the order in the tree, and so it also defines (optimal)
24 * block layout. objectid corresonds to the inode number. The flags
25 * tells us things about the object, and is a kind of stream selector.
26 * so for a given inode, keys with flags of 1 might refer to the inode
27 * data, flags of 2 may point to file data in the btree and flags == 3
28 * may point to extents.
30 * offset is the starting byte offset for this key in the stream.
32 * btrfs_disk_key is in disk byte order. struct btrfs_key is always
33 * in cpu native order. Otherwise they are identical and their sizes
34 * should be the same (ie both packed)
36 struct btrfs_disk_key {
40 } __attribute__ ((__packed__));
46 } __attribute__ ((__packed__));
49 * every tree block (leaf or node) starts with this header.
52 u8 fsid[16]; /* FS specific uuid */
53 __le64 blocknr; /* which block this node is supposed to live in */
54 __le64 parentid; /* objectid of the tree root */
59 /* generation flags to be added */
60 } __attribute__ ((__packed__));
62 #define BTRFS_MAX_LEVEL 8
63 #define BTRFS_NODEPTRS_PER_BLOCK(r) (((r)->blocksize - \
64 sizeof(struct btrfs_header)) / \
65 (sizeof(struct btrfs_disk_key) + sizeof(u64)))
66 #define __BTRFS_LEAF_DATA_SIZE(bs) ((bs) - sizeof(struct btrfs_header))
67 #define BTRFS_LEAF_DATA_SIZE(r) (__BTRFS_LEAF_DATA_SIZE(r->blocksize))
71 * the super block basically lists the main trees of the FS
72 * it currently lacks any block count etc etc
74 struct btrfs_super_block {
75 u8 fsid[16]; /* FS specific uuid */
76 __le64 blocknr; /* this block number */
84 __le64 root_dir_objectid;
85 } __attribute__ ((__packed__));
88 * A leaf is full of items. offset and size tell us where to find
89 * the item in the leaf (relative to the start of the data area)
92 struct btrfs_disk_key key;
95 } __attribute__ ((__packed__));
98 * leaves have an item area and a data area:
99 * [item0, item1....itemN] [free space] [dataN...data1, data0]
101 * The data is separate from the items to get the keys closer together
105 struct btrfs_header header;
106 struct btrfs_item items[];
107 } __attribute__ ((__packed__));
110 * all non-leaf blocks are nodes, they hold only keys and pointers to
113 struct btrfs_key_ptr {
114 struct btrfs_disk_key key;
116 } __attribute__ ((__packed__));
119 struct btrfs_header header;
120 struct btrfs_key_ptr ptrs[];
121 } __attribute__ ((__packed__));
124 * btrfs_paths remember the path taken from the root down to the leaf.
125 * level 0 is always the leaf, and nodes[1...BTRFS_MAX_LEVEL] will point
126 * to any other levels that are present.
128 * The slots array records the index of the item or block pointer
129 * used while walking the tree.
132 struct buffer_head *nodes[BTRFS_MAX_LEVEL];
133 int slots[BTRFS_MAX_LEVEL];
137 * items in the extent btree are used to record the objectid of the
138 * owner of the block and the number of references
140 struct btrfs_extent_item {
143 } __attribute__ ((__packed__));
145 struct btrfs_inode_timespec {
148 } __attribute__ ((__packed__));
151 * there is no padding here on purpose. If you want to extent the inode,
152 * make a new item type
154 struct btrfs_inode_item {
165 struct btrfs_inode_timespec atime;
166 struct btrfs_inode_timespec ctime;
167 struct btrfs_inode_timespec mtime;
168 struct btrfs_inode_timespec otime;
169 } __attribute__ ((__packed__));
171 /* inline data is just a blob of bytes */
172 struct btrfs_inline_data_item {
174 } __attribute__ ((__packed__));
176 struct btrfs_dir_item {
181 } __attribute__ ((__packed__));
183 struct btrfs_root_item {
189 } __attribute__ ((__packed__));
191 struct btrfs_file_extent_item {
193 * disk space consumed by the extent, checksum blocks are included
197 __le64 disk_num_blocks;
199 * the logical offset in file bytes (no csums)
200 * this extent record is for. This allows a file extent to point
201 * into the middle of an existing extent on disk, sharing it
202 * between two snapshots (useful if some bytes in the middle of the
203 * extent have changed
207 * the logical number of file blocks (no csums included)
210 } __attribute__ ((__packed__));
212 struct btrfs_inode_map_item {
213 struct btrfs_disk_key key;
214 } __attribute__ ((__packed__));
216 struct btrfs_fs_info {
217 struct btrfs_root *fs_root;
218 struct btrfs_root *extent_root;
219 struct btrfs_root *tree_root;
220 struct btrfs_root *inode_root;
221 struct btrfs_key current_insert;
222 struct btrfs_key last_insert;
223 struct radix_tree_root pinned_radix;
224 u64 last_inode_alloc;
225 u64 last_inode_alloc_dirid;
227 struct btrfs_trans_handle *running_transaction;
228 struct btrfs_super_block *disk_super;
229 struct buffer_head *sb_buffer;
230 struct super_block *sb;
234 * in ram representation of the tree. extent_root is used for all allocations
235 * and for the extent tree extent_root root. current_insert is used
236 * only for the extent tree.
239 struct buffer_head *node;
240 struct buffer_head *commit_root;
241 struct btrfs_root_item root_item;
242 struct btrfs_key root_key;
243 struct btrfs_fs_info *fs_info;
249 /* the lower bits in the key flags defines the item type */
250 #define BTRFS_KEY_TYPE_MAX 256
251 #define BTRFS_KEY_TYPE_MASK (BTRFS_KEY_TYPE_MAX - 1)
254 * inode items have the data typically returned from stat and store other
255 * info about object characteristics. There is one for every file and dir in
258 #define BTRFS_INODE_ITEM_KEY 1
261 * dir items are the name -> inode pointers in a directory. There is one
262 * for every name in a directory.
264 #define BTRFS_DIR_ITEM_KEY 2
266 * inline data is file data that fits in the btree.
268 #define BTRFS_INLINE_DATA_KEY 3
270 * extent data is for data that can't fit in the btree. It points to
271 * a (hopefully) huge chunk of disk
273 #define BTRFS_EXTENT_DATA_KEY 4
275 * root items point to tree roots. There are typically in the root
276 * tree used by the super block to find all the other trees
278 #define BTRFS_ROOT_ITEM_KEY 5
280 * extent items are in the extent map tree. These record which blocks
281 * are used, and how many references there are to each block
283 #define BTRFS_EXTENT_ITEM_KEY 6
286 * the inode map records which inode numbers are in use and where
287 * they actually live on disk
289 #define BTRFS_INODE_MAP_ITEM_KEY 7
291 * string items are for debugging. They just store a short string of
294 #define BTRFS_STRING_ITEM_KEY 8
296 static inline u64 btrfs_inode_generation(struct btrfs_inode_item *i)
298 return le64_to_cpu(i->generation);
301 static inline void btrfs_set_inode_generation(struct btrfs_inode_item *i,
304 i->generation = cpu_to_le64(val);
307 static inline u64 btrfs_inode_size(struct btrfs_inode_item *i)
309 return le64_to_cpu(i->size);
312 static inline void btrfs_set_inode_size(struct btrfs_inode_item *i, u64 val)
314 i->size = cpu_to_le64(val);
317 static inline u64 btrfs_inode_nblocks(struct btrfs_inode_item *i)
319 return le64_to_cpu(i->nblocks);
322 static inline void btrfs_set_inode_nblocks(struct btrfs_inode_item *i, u64 val)
324 i->nblocks = cpu_to_le64(val);
327 static inline u32 btrfs_inode_nlink(struct btrfs_inode_item *i)
329 return le32_to_cpu(i->nlink);
332 static inline void btrfs_set_inode_nlink(struct btrfs_inode_item *i, u32 val)
334 i->nlink = cpu_to_le32(val);
337 static inline u32 btrfs_inode_uid(struct btrfs_inode_item *i)
339 return le32_to_cpu(i->uid);
342 static inline void btrfs_set_inode_uid(struct btrfs_inode_item *i, u32 val)
344 i->uid = cpu_to_le32(val);
347 static inline u32 btrfs_inode_gid(struct btrfs_inode_item *i)
349 return le32_to_cpu(i->gid);
352 static inline void btrfs_set_inode_gid(struct btrfs_inode_item *i, u32 val)
354 i->gid = cpu_to_le32(val);
357 static inline u32 btrfs_inode_mode(struct btrfs_inode_item *i)
359 return le32_to_cpu(i->mode);
362 static inline void btrfs_set_inode_mode(struct btrfs_inode_item *i, u32 val)
364 i->mode = cpu_to_le32(val);
367 static inline u32 btrfs_inode_rdev(struct btrfs_inode_item *i)
369 return le32_to_cpu(i->rdev);
372 static inline void btrfs_set_inode_rdev(struct btrfs_inode_item *i, u32 val)
374 i->rdev = cpu_to_le32(val);
377 static inline u16 btrfs_inode_flags(struct btrfs_inode_item *i)
379 return le16_to_cpu(i->flags);
382 static inline void btrfs_set_inode_flags(struct btrfs_inode_item *i, u16 val)
384 i->flags = cpu_to_le16(val);
387 static inline u16 btrfs_inode_compat_flags(struct btrfs_inode_item *i)
389 return le16_to_cpu(i->compat_flags);
392 static inline void btrfs_set_inode_compat_flags(struct btrfs_inode_item *i,
395 i->compat_flags = cpu_to_le16(val);
398 static inline u32 btrfs_timespec_sec(struct btrfs_inode_timespec *ts)
400 return le32_to_cpu(ts->sec);
403 static inline void btrfs_set_timespec_sec(struct btrfs_inode_timespec *ts,
406 ts->sec = cpu_to_le32(val);
409 static inline u32 btrfs_timespec_nsec(struct btrfs_inode_timespec *ts)
411 return le32_to_cpu(ts->nsec);
414 static inline void btrfs_set_timespec_nsec(struct btrfs_inode_timespec *ts,
417 ts->nsec = cpu_to_le32(val);
422 static inline u64 btrfs_extent_owner(struct btrfs_extent_item *ei)
424 return le64_to_cpu(ei->owner);
427 static inline void btrfs_set_extent_owner(struct btrfs_extent_item *ei, u64 val)
429 ei->owner = cpu_to_le64(val);
432 static inline u32 btrfs_extent_refs(struct btrfs_extent_item *ei)
434 return le32_to_cpu(ei->refs);
437 static inline void btrfs_set_extent_refs(struct btrfs_extent_item *ei, u32 val)
439 ei->refs = cpu_to_le32(val);
442 static inline u64 btrfs_node_blockptr(struct btrfs_node *n, int nr)
444 return le64_to_cpu(n->ptrs[nr].blockptr);
447 static inline void btrfs_set_node_blockptr(struct btrfs_node *n, int nr,
450 n->ptrs[nr].blockptr = cpu_to_le64(val);
453 static inline u32 btrfs_item_offset(struct btrfs_item *item)
455 return le32_to_cpu(item->offset);
458 static inline void btrfs_set_item_offset(struct btrfs_item *item, u32 val)
460 item->offset = cpu_to_le32(val);
463 static inline u32 btrfs_item_end(struct btrfs_item *item)
465 return le32_to_cpu(item->offset) + le16_to_cpu(item->size);
468 static inline u16 btrfs_item_size(struct btrfs_item *item)
470 return le16_to_cpu(item->size);
473 static inline void btrfs_set_item_size(struct btrfs_item *item, u16 val)
475 item->size = cpu_to_le16(val);
478 static inline u64 btrfs_dir_objectid(struct btrfs_dir_item *d)
480 return le64_to_cpu(d->objectid);
483 static inline void btrfs_set_dir_objectid(struct btrfs_dir_item *d, u64 val)
485 d->objectid = cpu_to_le64(val);
488 static inline u16 btrfs_dir_flags(struct btrfs_dir_item *d)
490 return le16_to_cpu(d->flags);
493 static inline void btrfs_set_dir_flags(struct btrfs_dir_item *d, u16 val)
495 d->flags = cpu_to_le16(val);
498 static inline u8 btrfs_dir_type(struct btrfs_dir_item *d)
503 static inline void btrfs_set_dir_type(struct btrfs_dir_item *d, u8 val)
508 static inline u16 btrfs_dir_name_len(struct btrfs_dir_item *d)
510 return le16_to_cpu(d->name_len);
513 static inline void btrfs_set_dir_name_len(struct btrfs_dir_item *d, u16 val)
515 d->name_len = cpu_to_le16(val);
518 static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu,
519 struct btrfs_disk_key *disk)
521 cpu->offset = le64_to_cpu(disk->offset);
522 cpu->flags = le32_to_cpu(disk->flags);
523 cpu->objectid = le64_to_cpu(disk->objectid);
526 static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk,
527 struct btrfs_key *cpu)
529 disk->offset = cpu_to_le64(cpu->offset);
530 disk->flags = cpu_to_le32(cpu->flags);
531 disk->objectid = cpu_to_le64(cpu->objectid);
534 static inline u64 btrfs_disk_key_objectid(struct btrfs_disk_key *disk)
536 return le64_to_cpu(disk->objectid);
539 static inline void btrfs_set_disk_key_objectid(struct btrfs_disk_key *disk,
542 disk->objectid = cpu_to_le64(val);
545 static inline u64 btrfs_disk_key_offset(struct btrfs_disk_key *disk)
547 return le64_to_cpu(disk->offset);
550 static inline void btrfs_set_disk_key_offset(struct btrfs_disk_key *disk,
553 disk->offset = cpu_to_le64(val);
556 static inline u32 btrfs_disk_key_flags(struct btrfs_disk_key *disk)
558 return le32_to_cpu(disk->flags);
561 static inline void btrfs_set_disk_key_flags(struct btrfs_disk_key *disk,
564 disk->flags = cpu_to_le32(val);
567 static inline u32 btrfs_key_type(struct btrfs_key *key)
569 return key->flags & BTRFS_KEY_TYPE_MASK;
572 static inline u32 btrfs_disk_key_type(struct btrfs_disk_key *key)
574 return le32_to_cpu(key->flags) & BTRFS_KEY_TYPE_MASK;
577 static inline void btrfs_set_key_type(struct btrfs_key *key, u32 type)
579 BUG_ON(type >= BTRFS_KEY_TYPE_MAX);
580 key->flags = (key->flags & ~((u64)BTRFS_KEY_TYPE_MASK)) | type;
583 static inline void btrfs_set_disk_key_type(struct btrfs_disk_key *key, u32 type)
585 u32 flags = btrfs_disk_key_flags(key);
586 BUG_ON(type >= BTRFS_KEY_TYPE_MAX);
587 flags = (flags & ~((u64)BTRFS_KEY_TYPE_MASK)) | type;
588 btrfs_set_disk_key_flags(key, flags);
591 static inline u64 btrfs_header_blocknr(struct btrfs_header *h)
593 return le64_to_cpu(h->blocknr);
596 static inline void btrfs_set_header_blocknr(struct btrfs_header *h, u64 blocknr)
598 h->blocknr = cpu_to_le64(blocknr);
601 static inline u64 btrfs_header_parentid(struct btrfs_header *h)
603 return le64_to_cpu(h->parentid);
606 static inline void btrfs_set_header_parentid(struct btrfs_header *h,
609 h->parentid = cpu_to_le64(parentid);
612 static inline u16 btrfs_header_nritems(struct btrfs_header *h)
614 return le16_to_cpu(h->nritems);
617 static inline void btrfs_set_header_nritems(struct btrfs_header *h, u16 val)
619 h->nritems = cpu_to_le16(val);
622 static inline u16 btrfs_header_flags(struct btrfs_header *h)
624 return le16_to_cpu(h->flags);
627 static inline void btrfs_set_header_flags(struct btrfs_header *h, u16 val)
629 h->flags = cpu_to_le16(val);
632 static inline int btrfs_header_level(struct btrfs_header *h)
634 return btrfs_header_flags(h) & (BTRFS_MAX_LEVEL - 1);
637 static inline void btrfs_set_header_level(struct btrfs_header *h, int level)
640 BUG_ON(level > BTRFS_MAX_LEVEL);
641 flags = btrfs_header_flags(h) & ~(BTRFS_MAX_LEVEL - 1);
642 btrfs_set_header_flags(h, flags | level);
645 static inline int btrfs_is_leaf(struct btrfs_node *n)
647 return (btrfs_header_level(&n->header) == 0);
650 static inline u64 btrfs_root_blocknr(struct btrfs_root_item *item)
652 return le64_to_cpu(item->blocknr);
655 static inline void btrfs_set_root_blocknr(struct btrfs_root_item *item, u64 val)
657 item->blocknr = cpu_to_le64(val);
660 static inline u32 btrfs_root_refs(struct btrfs_root_item *item)
662 return le32_to_cpu(item->refs);
665 static inline void btrfs_set_root_refs(struct btrfs_root_item *item, u32 val)
667 item->refs = cpu_to_le32(val);
670 static inline u64 btrfs_super_blocknr(struct btrfs_super_block *s)
672 return le64_to_cpu(s->blocknr);
675 static inline void btrfs_set_super_blocknr(struct btrfs_super_block *s, u64 val)
677 s->blocknr = cpu_to_le64(val);
680 static inline u64 btrfs_super_root(struct btrfs_super_block *s)
682 return le64_to_cpu(s->root);
685 static inline void btrfs_set_super_root(struct btrfs_super_block *s, u64 val)
687 s->root = cpu_to_le64(val);
690 static inline u64 btrfs_super_total_blocks(struct btrfs_super_block *s)
692 return le64_to_cpu(s->total_blocks);
695 static inline void btrfs_set_super_total_blocks(struct btrfs_super_block *s,
698 s->total_blocks = cpu_to_le64(val);
701 static inline u64 btrfs_super_blocks_used(struct btrfs_super_block *s)
703 return le64_to_cpu(s->blocks_used);
706 static inline void btrfs_set_super_blocks_used(struct btrfs_super_block *s,
709 s->blocks_used = cpu_to_le64(val);
712 static inline u32 btrfs_super_blocksize(struct btrfs_super_block *s)
714 return le32_to_cpu(s->blocksize);
717 static inline void btrfs_set_super_blocksize(struct btrfs_super_block *s,
720 s->blocksize = cpu_to_le32(val);
723 static inline u64 btrfs_super_root_dir(struct btrfs_super_block *s)
725 return le64_to_cpu(s->root_dir_objectid);
728 static inline void btrfs_set_super_root_dir(struct btrfs_super_block *s, u64
731 s->root_dir_objectid = cpu_to_le64(val);
734 static inline u8 *btrfs_leaf_data(struct btrfs_leaf *l)
736 return (u8 *)l->items;
739 static inline u64 btrfs_file_extent_disk_blocknr(struct btrfs_file_extent_item
742 return le64_to_cpu(e->disk_blocknr);
745 static inline void btrfs_set_file_extent_disk_blocknr(struct
746 btrfs_file_extent_item
749 e->disk_blocknr = cpu_to_le64(val);
752 static inline u64 btrfs_file_extent_disk_num_blocks(struct
753 btrfs_file_extent_item *e)
755 return le64_to_cpu(e->disk_num_blocks);
758 static inline void btrfs_set_file_extent_disk_num_blocks(struct
759 btrfs_file_extent_item
762 e->disk_num_blocks = cpu_to_le64(val);
765 static inline u64 btrfs_file_extent_offset(struct btrfs_file_extent_item *e)
767 return le64_to_cpu(e->offset);
770 static inline void btrfs_set_file_extent_offset(struct btrfs_file_extent_item
773 e->offset = cpu_to_le64(val);
776 static inline u64 btrfs_file_extent_num_blocks(struct btrfs_file_extent_item
779 return le64_to_cpu(e->num_blocks);
782 static inline void btrfs_set_file_extent_num_blocks(struct
783 btrfs_file_extent_item *e,
786 e->num_blocks = cpu_to_le64(val);
789 static inline struct btrfs_root *btrfs_sb(struct super_block *sb)
791 return sb->s_fs_info;
794 /* helper function to cast into the data area of the leaf. */
795 #define btrfs_item_ptr(leaf, slot, type) \
796 ((type *)(btrfs_leaf_data(leaf) + \
797 btrfs_item_offset((leaf)->items + (slot))))
799 struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
800 struct btrfs_root *root);
801 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
802 struct buffer_head *buf);
803 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
804 *root, u64 blocknr, u64 num_blocks, int pin);
805 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
806 *root, struct btrfs_key *key, struct btrfs_path *p, int
808 void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p);
809 void btrfs_init_path(struct btrfs_path *p);
810 int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
811 struct btrfs_path *path);
812 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
813 *root, struct btrfs_key *key, void *data, u32 data_size);
814 int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root
815 *root, struct btrfs_path *path, struct btrfs_key
816 *cpu_key, u32 data_size);
817 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path);
818 int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf);
819 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
820 *root, struct buffer_head *snap);
821 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
823 int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
824 struct btrfs_key *key);
825 int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
826 *root, struct btrfs_key *key, struct btrfs_root_item
828 int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
829 *root, struct btrfs_key *key, struct btrfs_root_item
831 int btrfs_find_last_root(struct btrfs_root *root, u64 objectid, struct
832 btrfs_root_item *item, struct btrfs_key *key);
833 int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
834 *root, char *name, int name_len, u64 dir, u64
836 int btrfs_lookup_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
837 *root, struct btrfs_path *path, u64 dir,
838 const char *name, int name_len, int mod);
839 int btrfs_match_dir_item_name(struct btrfs_root *root, struct btrfs_path *path,
840 char *name, int name_len);
841 int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
842 struct btrfs_root *fs_root,
843 u64 dirid, u64 *objectid);
844 int btrfs_insert_inode_map(struct btrfs_trans_handle *trans,
845 struct btrfs_root *root,
846 u64 objectid, struct btrfs_key *location);
847 int btrfs_lookup_inode_map(struct btrfs_trans_handle *trans,
848 struct btrfs_root *root, struct btrfs_path *path,
849 u64 objectid, int mod);
850 int btrfs_insert_inode(struct btrfs_trans_handle *trans, struct btrfs_root
851 *root, u64 objectid, struct btrfs_inode_item
853 int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root
854 *root, struct btrfs_path *path, u64 objectid, int mod);