5 #include "kerncompat.h"
7 #define BTRFS_MAGIC "_BtRfS_M"
9 #define BTRFS_ROOT_TREE_OBJECTID 1
10 #define BTRFS_EXTENT_TREE_OBJECTID 2
11 #define BTRFS_FS_TREE_OBJECTID 3
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 } __attribute__ ((__packed__));
78 * A leaf is full of items. offset and size tell us where to find
79 * the item in the leaf (relative to the start of the data area)
82 struct btrfs_disk_key key;
85 } __attribute__ ((__packed__));
88 * leaves have an item area and a data area:
89 * [item0, item1....itemN] [free space] [dataN...data1, data0]
91 * The data is separate from the items to get the keys closer together
95 struct btrfs_header header;
96 struct btrfs_item items[];
97 } __attribute__ ((__packed__));
100 * all non-leaf blocks are nodes, they hold only keys and pointers to
103 struct btrfs_key_ptr {
104 struct btrfs_disk_key key;
106 } __attribute__ ((__packed__));
109 struct btrfs_header header;
110 struct btrfs_key_ptr ptrs[];
111 } __attribute__ ((__packed__));
114 * btrfs_paths remember the path taken from the root down to the leaf.
115 * level 0 is always the leaf, and nodes[1...BTRFS_MAX_LEVEL] will point
116 * to any other levels that are present.
118 * The slots array records the index of the item or block pointer
119 * used while walking the tree.
122 struct btrfs_buffer *nodes[BTRFS_MAX_LEVEL];
123 int slots[BTRFS_MAX_LEVEL];
127 * items in the extent btree are used to record the objectid of the
128 * owner of the block and the number of references
130 struct btrfs_extent_item {
133 } __attribute__ ((__packed__));
135 struct btrfs_inode_timespec {
138 } __attribute__ ((__packed__));
141 * there is no padding here on purpose. If you want to extent the inode,
142 * make a new item type
144 struct btrfs_inode_item {
155 struct btrfs_inode_timespec atime;
156 struct btrfs_inode_timespec ctime;
157 struct btrfs_inode_timespec mtime;
158 struct btrfs_inode_timespec otime;
159 } __attribute__ ((__packed__));
161 /* inline data is just a blob of bytes */
162 struct btrfs_inline_data_item {
164 } __attribute__ ((__packed__));
166 struct btrfs_dir_item {
170 } __attribute__ ((__packed__));
172 struct btrfs_root_item {
181 * in ram representation of the tree. extent_root is used for all allocations
182 * and for the extent tree extent_root root. current_insert is used
183 * only for the extent tree.
186 struct btrfs_buffer *node;
187 struct btrfs_buffer *commit_root;
188 struct btrfs_root *extent_root;
189 struct btrfs_root *tree_root;
190 struct btrfs_key current_insert;
191 struct btrfs_key last_insert;
193 struct radix_tree_root cache_radix;
194 struct radix_tree_root pinned_radix;
195 struct list_head trans;
196 struct list_head cache;
199 struct btrfs_root_item root_item;
200 struct btrfs_key root_key;
204 /* the lower bits in the key flags defines the item type */
205 #define BTRFS_KEY_TYPE_MAX 256
206 #define BTRFS_KEY_TYPE_MASK (BTRFS_KEY_TYPE_MAX - 1)
209 * inode items have the data typically returned from stat and store other
210 * info about object characteristics. There is one for every file and dir in
213 #define BTRFS_INODE_ITEM_KEY 1
216 * dir items are the name -> inode pointers in a directory. There is one
217 * for every name in a directory.
219 #define BTRFS_DIR_ITEM_KEY 2
221 * inline data is file data that fits in the btree.
223 #define BTRFS_INLINE_DATA_KEY 3
225 * extent data is for data that can't fit in the btree. It points to
226 * a (hopefully) huge chunk of disk
228 #define BTRFS_EXTENT_DATA_KEY 4
230 * root items point to tree roots. There are typically in the root
231 * tree used by the super block to find all the other trees
233 #define BTRFS_ROOT_ITEM_KEY 5
235 * extent items are in the extent map tree. These record which blocks
236 * are used, and how many references there are to each block
238 #define BTRFS_EXTENT_ITEM_KEY 6
240 * string items are for debugging. They just store a short string of
243 #define BTRFS_STRING_ITEM_KEY 7
245 static inline u64 btrfs_inode_generation(struct btrfs_inode_item *i)
247 return le64_to_cpu(i->generation);
250 static inline void btrfs_set_inode_generation(struct btrfs_inode_item *i,
253 i->generation = cpu_to_le64(val);
256 static inline u64 btrfs_inode_size(struct btrfs_inode_item *i)
258 return le64_to_cpu(i->size);
261 static inline void btrfs_set_inode_size(struct btrfs_inode_item *i, u64 val)
263 i->size = cpu_to_le64(val);
266 static inline u64 btrfs_inode_nblocks(struct btrfs_inode_item *i)
268 return le64_to_cpu(i->nblocks);
271 static inline void btrfs_set_inode_nblocks(struct btrfs_inode_item *i, u64 val)
273 i->nblocks = cpu_to_le64(val);
276 static inline u32 btrfs_inode_nlink(struct btrfs_inode_item *i)
278 return le32_to_cpu(i->nlink);
281 static inline void btrfs_set_inode_nlink(struct btrfs_inode_item *i, u32 val)
283 i->nlink = cpu_to_le32(val);
286 static inline u32 btrfs_inode_uid(struct btrfs_inode_item *i)
288 return le32_to_cpu(i->uid);
291 static inline void btrfs_set_inode_uid(struct btrfs_inode_item *i, u32 val)
293 i->uid = cpu_to_le32(val);
296 static inline u32 btrfs_inode_gid(struct btrfs_inode_item *i)
298 return le32_to_cpu(i->gid);
301 static inline void btrfs_set_inode_gid(struct btrfs_inode_item *i, u32 val)
303 i->gid = cpu_to_le32(val);
306 static inline u32 btrfs_inode_mode(struct btrfs_inode_item *i)
308 return le32_to_cpu(i->mode);
311 static inline void btrfs_set_inode_mode(struct btrfs_inode_item *i, u32 val)
313 i->mode = cpu_to_le32(val);
316 static inline u32 btrfs_inode_rdev(struct btrfs_inode_item *i)
318 return le32_to_cpu(i->rdev);
321 static inline void btrfs_set_inode_rdev(struct btrfs_inode_item *i, u32 val)
323 i->rdev = cpu_to_le32(val);
326 static inline u16 btrfs_inode_flags(struct btrfs_inode_item *i)
328 return le16_to_cpu(i->flags);
331 static inline void btrfs_set_inode_flags(struct btrfs_inode_item *i, u16 val)
333 i->flags = cpu_to_le16(val);
336 static inline u16 btrfs_inode_compat_flags(struct btrfs_inode_item *i)
338 return le16_to_cpu(i->compat_flags);
341 static inline void btrfs_set_inode_compat_flags(struct btrfs_inode_item *i,
344 i->compat_flags = cpu_to_le16(val);
348 static inline u64 btrfs_extent_owner(struct btrfs_extent_item *ei)
350 return le64_to_cpu(ei->owner);
353 static inline void btrfs_set_extent_owner(struct btrfs_extent_item *ei, u64 val)
355 ei->owner = cpu_to_le64(val);
358 static inline u32 btrfs_extent_refs(struct btrfs_extent_item *ei)
360 return le32_to_cpu(ei->refs);
363 static inline void btrfs_set_extent_refs(struct btrfs_extent_item *ei, u32 val)
365 ei->refs = cpu_to_le32(val);
368 static inline u64 btrfs_node_blockptr(struct btrfs_node *n, int nr)
370 return le64_to_cpu(n->ptrs[nr].blockptr);
373 static inline void btrfs_set_node_blockptr(struct btrfs_node *n, int nr,
376 n->ptrs[nr].blockptr = cpu_to_le64(val);
379 static inline u32 btrfs_item_offset(struct btrfs_item *item)
381 return le32_to_cpu(item->offset);
384 static inline void btrfs_set_item_offset(struct btrfs_item *item, u32 val)
386 item->offset = cpu_to_le32(val);
389 static inline u32 btrfs_item_end(struct btrfs_item *item)
391 return le32_to_cpu(item->offset) + le16_to_cpu(item->size);
394 static inline u16 btrfs_item_size(struct btrfs_item *item)
396 return le16_to_cpu(item->size);
399 static inline void btrfs_set_item_size(struct btrfs_item *item, u16 val)
401 item->size = cpu_to_le16(val);
404 static inline u64 btrfs_dir_objectid(struct btrfs_dir_item *d)
406 return le64_to_cpu(d->objectid);
409 static inline void btrfs_set_dir_objectid(struct btrfs_dir_item *d, u64 val)
411 d->objectid = cpu_to_le64(val);
414 static inline u16 btrfs_dir_flags(struct btrfs_dir_item *d)
416 return le16_to_cpu(d->flags);
419 static inline void btrfs_set_dir_flags(struct btrfs_dir_item *d, u16 val)
421 d->flags = cpu_to_le16(val);
424 static inline u8 btrfs_dir_type(struct btrfs_dir_item *d)
429 static inline void btrfs_set_dir_type(struct btrfs_dir_item *d, u8 val)
434 static inline u32 btrfs_dir_name_len(struct btrfs_item *i)
436 return btrfs_item_size(i) - sizeof(struct btrfs_dir_item);
439 static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu,
440 struct btrfs_disk_key *disk)
442 cpu->offset = le64_to_cpu(disk->offset);
443 cpu->flags = le32_to_cpu(disk->flags);
444 cpu->objectid = le64_to_cpu(disk->objectid);
447 static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk,
448 struct btrfs_key *cpu)
450 disk->offset = cpu_to_le64(cpu->offset);
451 disk->flags = cpu_to_le32(cpu->flags);
452 disk->objectid = cpu_to_le64(cpu->objectid);
455 static inline u64 btrfs_disk_key_objectid(struct btrfs_disk_key *disk)
457 return le64_to_cpu(disk->objectid);
460 static inline void btrfs_set_disk_key_objectid(struct btrfs_disk_key *disk,
463 disk->objectid = cpu_to_le64(val);
466 static inline u64 btrfs_disk_key_offset(struct btrfs_disk_key *disk)
468 return le64_to_cpu(disk->offset);
471 static inline void btrfs_set_disk_key_offset(struct btrfs_disk_key *disk,
474 disk->offset = cpu_to_le64(val);
477 static inline u32 btrfs_disk_key_flags(struct btrfs_disk_key *disk)
479 return le32_to_cpu(disk->flags);
482 static inline void btrfs_set_disk_key_flags(struct btrfs_disk_key *disk,
485 disk->flags = cpu_to_le32(val);
488 static inline u32 btrfs_key_type(struct btrfs_key *key)
490 return key->flags & BTRFS_KEY_TYPE_MASK;
493 static inline u32 btrfs_disk_key_type(struct btrfs_disk_key *key)
495 return le32_to_cpu(key->flags) & BTRFS_KEY_TYPE_MASK;
498 static inline void btrfs_set_key_type(struct btrfs_key *key, u32 type)
500 BUG_ON(type >= BTRFS_KEY_TYPE_MAX);
501 key->flags = (key->flags & ~((u64)BTRFS_KEY_TYPE_MASK)) | type;
504 static inline void btrfs_set_disk_key_type(struct btrfs_disk_key *key, u32 type)
506 u32 flags = btrfs_disk_key_flags(key);
507 BUG_ON(type >= BTRFS_KEY_TYPE_MAX);
508 flags = (flags & ~((u64)BTRFS_KEY_TYPE_MASK)) | type;
509 btrfs_set_disk_key_flags(key, flags);
512 static inline u64 btrfs_header_blocknr(struct btrfs_header *h)
514 return le64_to_cpu(h->blocknr);
517 static inline void btrfs_set_header_blocknr(struct btrfs_header *h, u64 blocknr)
519 h->blocknr = cpu_to_le64(blocknr);
522 static inline u64 btrfs_header_parentid(struct btrfs_header *h)
524 return le64_to_cpu(h->parentid);
527 static inline void btrfs_set_header_parentid(struct btrfs_header *h,
530 h->parentid = cpu_to_le64(parentid);
533 static inline u16 btrfs_header_nritems(struct btrfs_header *h)
535 return le16_to_cpu(h->nritems);
538 static inline void btrfs_set_header_nritems(struct btrfs_header *h, u16 val)
540 h->nritems = cpu_to_le16(val);
543 static inline u16 btrfs_header_flags(struct btrfs_header *h)
545 return le16_to_cpu(h->flags);
548 static inline void btrfs_set_header_flags(struct btrfs_header *h, u16 val)
550 h->flags = cpu_to_le16(val);
553 static inline int btrfs_header_level(struct btrfs_header *h)
555 return btrfs_header_flags(h) & (BTRFS_MAX_LEVEL - 1);
558 static inline void btrfs_set_header_level(struct btrfs_header *h, int level)
561 BUG_ON(level > BTRFS_MAX_LEVEL);
562 flags = btrfs_header_flags(h) & ~(BTRFS_MAX_LEVEL - 1);
563 btrfs_set_header_flags(h, flags | level);
566 static inline int btrfs_is_leaf(struct btrfs_node *n)
568 return (btrfs_header_level(&n->header) == 0);
571 static inline u64 btrfs_root_blocknr(struct btrfs_root_item *item)
573 return le64_to_cpu(item->blocknr);
576 static inline void btrfs_set_root_blocknr(struct btrfs_root_item *item, u64 val)
578 item->blocknr = cpu_to_le64(val);
581 static inline u32 btrfs_root_refs(struct btrfs_root_item *item)
583 return le32_to_cpu(item->refs);
586 static inline void btrfs_set_root_refs(struct btrfs_root_item *item, u32 val)
588 item->refs = cpu_to_le32(val);
591 static inline u64 btrfs_super_blocknr(struct btrfs_super_block *s)
593 return le64_to_cpu(s->blocknr);
596 static inline void btrfs_set_super_blocknr(struct btrfs_super_block *s, u64 val)
598 s->blocknr = cpu_to_le64(val);
601 static inline u64 btrfs_super_root(struct btrfs_super_block *s)
603 return le64_to_cpu(s->root);
606 static inline void btrfs_set_super_root(struct btrfs_super_block *s, u64 val)
608 s->root = cpu_to_le64(val);
611 static inline u64 btrfs_super_total_blocks(struct btrfs_super_block *s)
613 return le64_to_cpu(s->total_blocks);
616 static inline void btrfs_set_super_total_blocks(struct btrfs_super_block *s,
619 s->total_blocks = cpu_to_le64(val);
622 static inline u64 btrfs_super_blocks_used(struct btrfs_super_block *s)
624 return le64_to_cpu(s->blocks_used);
627 static inline void btrfs_set_super_blocks_used(struct btrfs_super_block *s,
630 s->blocks_used = cpu_to_le64(val);
633 static inline u32 btrfs_super_blocksize(struct btrfs_super_block *s)
635 return le32_to_cpu(s->blocksize);
638 static inline void btrfs_set_super_blocksize(struct btrfs_super_block *s,
641 s->blocksize = cpu_to_le32(val);
644 static inline u8 *btrfs_leaf_data(struct btrfs_leaf *l)
646 return (u8 *)l->items;
648 /* helper function to cast into the data area of the leaf. */
649 #define btrfs_item_ptr(leaf, slot, type) \
650 ((type *)(btrfs_leaf_data(leaf) + \
651 btrfs_item_offset((leaf)->items + (slot))))
653 struct btrfs_buffer *btrfs_alloc_free_block(struct btrfs_root *root);
654 int btrfs_inc_ref(struct btrfs_root *root, struct btrfs_buffer *buf);
655 int btrfs_free_extent(struct btrfs_root *root, u64 blocknr, u64 num_blocks);
656 int btrfs_search_slot(struct btrfs_root *root, struct btrfs_key *key,
657 struct btrfs_path *p, int ins_len, int cow);
658 void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p);
659 void btrfs_init_path(struct btrfs_path *p);
660 int btrfs_del_item(struct btrfs_root *root, struct btrfs_path *path);
661 int btrfs_insert_item(struct btrfs_root *root, struct btrfs_key *key,
662 void *data, u32 data_size);
663 int btrfs_insert_empty_item(struct btrfs_root *root, struct btrfs_path *path,
664 struct btrfs_key *cpu_key, u32 data_size);
665 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path);
666 int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf);
667 int btrfs_drop_snapshot(struct btrfs_root *root, struct btrfs_buffer *snap);
668 int btrfs_finish_extent_commit(struct btrfs_root *root);
669 int btrfs_del_root(struct btrfs_root *root, struct btrfs_key *key);
670 int btrfs_insert_root(struct btrfs_root *root, struct btrfs_key *key,
671 struct btrfs_root_item *item);
672 int btrfs_update_root(struct btrfs_root *root, struct btrfs_key *key,
673 struct btrfs_root_item *item);
674 int btrfs_find_last_root(struct btrfs_root *root, u64 objectid,
675 struct btrfs_root_item *item, struct btrfs_key *key);
676 int btrfs_insert_dir_item(struct btrfs_root *root, char *name, int name_len,
677 u64 dir, u64 objectid, u8 type);
678 int btrfs_lookup_dir_item(struct btrfs_root *root, struct btrfs_path *path,
679 u64 dir, char *name, int name_len, int mod);
680 int btrfs_match_dir_item_name(struct btrfs_root *root, struct btrfs_path *path,
681 char *name, int name_len);