5 #include "kerncompat.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_FS_TREE_OBJECTID 3
16 * the key defines the order in the tree, and so it also defines (optimal)
17 * block layout. objectid corresonds to the inode number. The flags
18 * tells us things about the object, and is a kind of stream selector.
19 * so for a given inode, keys with flags of 1 might refer to the inode
20 * data, flags of 2 may point to file data in the btree and flags == 3
21 * may point to extents.
23 * offset is the starting byte offset for this key in the stream.
25 * btrfs_disk_key is in disk byte order. struct btrfs_key is always
26 * in cpu native order. Otherwise they are identical and their sizes
27 * should be the same (ie both packed)
29 struct btrfs_disk_key
{
33 } __attribute__ ((__packed__
));
39 } __attribute__ ((__packed__
));
42 * every tree block (leaf or node) starts with this header.
45 u8 fsid
[16]; /* FS specific uuid */
46 __le64 blocknr
; /* which block this node is supposed to live in */
47 __le64 parentid
; /* objectid of the tree root */
52 /* generation flags to be added */
53 } __attribute__ ((__packed__
));
55 #define BTRFS_MAX_LEVEL 8
56 #define BTRFS_NODEPTRS_PER_BLOCK(r) (((r)->blocksize - \
57 sizeof(struct btrfs_header)) / \
58 (sizeof(struct btrfs_disk_key) + sizeof(u64)))
59 #define __BTRFS_LEAF_DATA_SIZE(bs) ((bs) - sizeof(struct btrfs_header))
60 #define BTRFS_LEAF_DATA_SIZE(r) (__BTRFS_LEAF_DATA_SIZE(r->blocksize))
64 * the super block basically lists the main trees of the FS
65 * it currently lacks any block count etc etc
67 struct btrfs_super_block
{
68 u8 fsid
[16]; /* FS specific uuid */
69 __le64 blocknr
; /* this block number */
77 } __attribute__ ((__packed__
));
80 * A leaf is full of items. offset and size tell us where to find
81 * the item in the leaf (relative to the start of the data area)
84 struct btrfs_disk_key key
;
87 } __attribute__ ((__packed__
));
90 * leaves have an item area and a data area:
91 * [item0, item1....itemN] [free space] [dataN...data1, data0]
93 * The data is separate from the items to get the keys closer together
97 struct btrfs_header header
;
98 struct btrfs_item items
[];
99 } __attribute__ ((__packed__
));
102 * all non-leaf blocks are nodes, they hold only keys and pointers to
105 struct btrfs_key_ptr
{
106 struct btrfs_disk_key key
;
108 } __attribute__ ((__packed__
));
111 struct btrfs_header header
;
112 struct btrfs_key_ptr ptrs
[];
113 } __attribute__ ((__packed__
));
116 * btrfs_paths remember the path taken from the root down to the leaf.
117 * level 0 is always the leaf, and nodes[1...BTRFS_MAX_LEVEL] will point
118 * to any other levels that are present.
120 * The slots array records the index of the item or block pointer
121 * used while walking the tree.
124 struct btrfs_buffer
*nodes
[BTRFS_MAX_LEVEL
];
125 int slots
[BTRFS_MAX_LEVEL
];
129 * items in the extent btree are used to record the objectid of the
130 * owner of the block and the number of references
132 struct btrfs_extent_item
{
135 } __attribute__ ((__packed__
));
137 struct btrfs_inode_timespec
{
140 } __attribute__ ((__packed__
));
143 * there is no padding here on purpose. If you want to extent the inode,
144 * make a new item type
146 struct btrfs_inode_item
{
157 struct btrfs_inode_timespec atime
;
158 struct btrfs_inode_timespec ctime
;
159 struct btrfs_inode_timespec mtime
;
160 struct btrfs_inode_timespec otime
;
161 } __attribute__ ((__packed__
));
163 /* inline data is just a blob of bytes */
164 struct btrfs_inline_data_item
{
166 } __attribute__ ((__packed__
));
168 struct btrfs_dir_item
{
173 } __attribute__ ((__packed__
));
175 struct btrfs_root_item
{
184 * in ram representation of the tree. extent_root is used for all allocations
185 * and for the extent tree extent_root root. current_insert is used
186 * only for the extent tree.
189 struct btrfs_buffer
*node
;
190 struct btrfs_buffer
*commit_root
;
191 struct btrfs_root
*extent_root
;
192 struct btrfs_root
*tree_root
;
193 struct btrfs_key current_insert
;
194 struct btrfs_key last_insert
;
196 struct radix_tree_root cache_radix
;
197 struct radix_tree_root pinned_radix
;
198 struct list_head trans
;
199 struct list_head cache
;
202 struct btrfs_root_item root_item
;
203 struct btrfs_key root_key
;
205 struct btrfs_trans_handle
*running_transaction
;
208 /* the lower bits in the key flags defines the item type */
209 #define BTRFS_KEY_TYPE_MAX 256
210 #define BTRFS_KEY_TYPE_MASK (BTRFS_KEY_TYPE_MAX - 1)
213 * inode items have the data typically returned from stat and store other
214 * info about object characteristics. There is one for every file and dir in
217 #define BTRFS_INODE_ITEM_KEY 1
220 * dir items are the name -> inode pointers in a directory. There is one
221 * for every name in a directory.
223 #define BTRFS_DIR_ITEM_KEY 2
225 * inline data is file data that fits in the btree.
227 #define BTRFS_INLINE_DATA_KEY 3
229 * extent data is for data that can't fit in the btree. It points to
230 * a (hopefully) huge chunk of disk
232 #define BTRFS_EXTENT_DATA_KEY 4
234 * root items point to tree roots. There are typically in the root
235 * tree used by the super block to find all the other trees
237 #define BTRFS_ROOT_ITEM_KEY 5
239 * extent items are in the extent map tree. These record which blocks
240 * are used, and how many references there are to each block
242 #define BTRFS_EXTENT_ITEM_KEY 6
244 * string items are for debugging. They just store a short string of
247 #define BTRFS_STRING_ITEM_KEY 7
249 static inline u64
btrfs_inode_generation(struct btrfs_inode_item
*i
)
251 return le64_to_cpu(i
->generation
);
254 static inline void btrfs_set_inode_generation(struct btrfs_inode_item
*i
,
257 i
->generation
= cpu_to_le64(val
);
260 static inline u64
btrfs_inode_size(struct btrfs_inode_item
*i
)
262 return le64_to_cpu(i
->size
);
265 static inline void btrfs_set_inode_size(struct btrfs_inode_item
*i
, u64 val
)
267 i
->size
= cpu_to_le64(val
);
270 static inline u64
btrfs_inode_nblocks(struct btrfs_inode_item
*i
)
272 return le64_to_cpu(i
->nblocks
);
275 static inline void btrfs_set_inode_nblocks(struct btrfs_inode_item
*i
, u64 val
)
277 i
->nblocks
= cpu_to_le64(val
);
280 static inline u32
btrfs_inode_nlink(struct btrfs_inode_item
*i
)
282 return le32_to_cpu(i
->nlink
);
285 static inline void btrfs_set_inode_nlink(struct btrfs_inode_item
*i
, u32 val
)
287 i
->nlink
= cpu_to_le32(val
);
290 static inline u32
btrfs_inode_uid(struct btrfs_inode_item
*i
)
292 return le32_to_cpu(i
->uid
);
295 static inline void btrfs_set_inode_uid(struct btrfs_inode_item
*i
, u32 val
)
297 i
->uid
= cpu_to_le32(val
);
300 static inline u32
btrfs_inode_gid(struct btrfs_inode_item
*i
)
302 return le32_to_cpu(i
->gid
);
305 static inline void btrfs_set_inode_gid(struct btrfs_inode_item
*i
, u32 val
)
307 i
->gid
= cpu_to_le32(val
);
310 static inline u32
btrfs_inode_mode(struct btrfs_inode_item
*i
)
312 return le32_to_cpu(i
->mode
);
315 static inline void btrfs_set_inode_mode(struct btrfs_inode_item
*i
, u32 val
)
317 i
->mode
= cpu_to_le32(val
);
320 static inline u32
btrfs_inode_rdev(struct btrfs_inode_item
*i
)
322 return le32_to_cpu(i
->rdev
);
325 static inline void btrfs_set_inode_rdev(struct btrfs_inode_item
*i
, u32 val
)
327 i
->rdev
= cpu_to_le32(val
);
330 static inline u16
btrfs_inode_flags(struct btrfs_inode_item
*i
)
332 return le16_to_cpu(i
->flags
);
335 static inline void btrfs_set_inode_flags(struct btrfs_inode_item
*i
, u16 val
)
337 i
->flags
= cpu_to_le16(val
);
340 static inline u16
btrfs_inode_compat_flags(struct btrfs_inode_item
*i
)
342 return le16_to_cpu(i
->compat_flags
);
345 static inline void btrfs_set_inode_compat_flags(struct btrfs_inode_item
*i
,
348 i
->compat_flags
= cpu_to_le16(val
);
352 static inline u64
btrfs_extent_owner(struct btrfs_extent_item
*ei
)
354 return le64_to_cpu(ei
->owner
);
357 static inline void btrfs_set_extent_owner(struct btrfs_extent_item
*ei
, u64 val
)
359 ei
->owner
= cpu_to_le64(val
);
362 static inline u32
btrfs_extent_refs(struct btrfs_extent_item
*ei
)
364 return le32_to_cpu(ei
->refs
);
367 static inline void btrfs_set_extent_refs(struct btrfs_extent_item
*ei
, u32 val
)
369 ei
->refs
= cpu_to_le32(val
);
372 static inline u64
btrfs_node_blockptr(struct btrfs_node
*n
, int nr
)
374 return le64_to_cpu(n
->ptrs
[nr
].blockptr
);
377 static inline void btrfs_set_node_blockptr(struct btrfs_node
*n
, int nr
,
380 n
->ptrs
[nr
].blockptr
= cpu_to_le64(val
);
383 static inline u32
btrfs_item_offset(struct btrfs_item
*item
)
385 return le32_to_cpu(item
->offset
);
388 static inline void btrfs_set_item_offset(struct btrfs_item
*item
, u32 val
)
390 item
->offset
= cpu_to_le32(val
);
393 static inline u32
btrfs_item_end(struct btrfs_item
*item
)
395 return le32_to_cpu(item
->offset
) + le16_to_cpu(item
->size
);
398 static inline u16
btrfs_item_size(struct btrfs_item
*item
)
400 return le16_to_cpu(item
->size
);
403 static inline void btrfs_set_item_size(struct btrfs_item
*item
, u16 val
)
405 item
->size
= cpu_to_le16(val
);
408 static inline u64
btrfs_dir_objectid(struct btrfs_dir_item
*d
)
410 return le64_to_cpu(d
->objectid
);
413 static inline void btrfs_set_dir_objectid(struct btrfs_dir_item
*d
, u64 val
)
415 d
->objectid
= cpu_to_le64(val
);
418 static inline u16
btrfs_dir_flags(struct btrfs_dir_item
*d
)
420 return le16_to_cpu(d
->flags
);
423 static inline void btrfs_set_dir_flags(struct btrfs_dir_item
*d
, u16 val
)
425 d
->flags
= cpu_to_le16(val
);
428 static inline u8
btrfs_dir_type(struct btrfs_dir_item
*d
)
433 static inline void btrfs_set_dir_type(struct btrfs_dir_item
*d
, u8 val
)
438 static inline u16
btrfs_dir_name_len(struct btrfs_dir_item
*d
)
440 return le16_to_cpu(d
->name_len
);
443 static inline void btrfs_set_dir_name_len(struct btrfs_dir_item
*d
, u16 val
)
445 d
->name_len
= cpu_to_le16(val
);
448 static inline void btrfs_disk_key_to_cpu(struct btrfs_key
*cpu
,
449 struct btrfs_disk_key
*disk
)
451 cpu
->offset
= le64_to_cpu(disk
->offset
);
452 cpu
->flags
= le32_to_cpu(disk
->flags
);
453 cpu
->objectid
= le64_to_cpu(disk
->objectid
);
456 static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key
*disk
,
457 struct btrfs_key
*cpu
)
459 disk
->offset
= cpu_to_le64(cpu
->offset
);
460 disk
->flags
= cpu_to_le32(cpu
->flags
);
461 disk
->objectid
= cpu_to_le64(cpu
->objectid
);
464 static inline u64
btrfs_disk_key_objectid(struct btrfs_disk_key
*disk
)
466 return le64_to_cpu(disk
->objectid
);
469 static inline void btrfs_set_disk_key_objectid(struct btrfs_disk_key
*disk
,
472 disk
->objectid
= cpu_to_le64(val
);
475 static inline u64
btrfs_disk_key_offset(struct btrfs_disk_key
*disk
)
477 return le64_to_cpu(disk
->offset
);
480 static inline void btrfs_set_disk_key_offset(struct btrfs_disk_key
*disk
,
483 disk
->offset
= cpu_to_le64(val
);
486 static inline u32
btrfs_disk_key_flags(struct btrfs_disk_key
*disk
)
488 return le32_to_cpu(disk
->flags
);
491 static inline void btrfs_set_disk_key_flags(struct btrfs_disk_key
*disk
,
494 disk
->flags
= cpu_to_le32(val
);
497 static inline u32
btrfs_key_type(struct btrfs_key
*key
)
499 return key
->flags
& BTRFS_KEY_TYPE_MASK
;
502 static inline u32
btrfs_disk_key_type(struct btrfs_disk_key
*key
)
504 return le32_to_cpu(key
->flags
) & BTRFS_KEY_TYPE_MASK
;
507 static inline void btrfs_set_key_type(struct btrfs_key
*key
, u32 type
)
509 BUG_ON(type
>= BTRFS_KEY_TYPE_MAX
);
510 key
->flags
= (key
->flags
& ~((u64
)BTRFS_KEY_TYPE_MASK
)) | type
;
513 static inline void btrfs_set_disk_key_type(struct btrfs_disk_key
*key
, u32 type
)
515 u32 flags
= btrfs_disk_key_flags(key
);
516 BUG_ON(type
>= BTRFS_KEY_TYPE_MAX
);
517 flags
= (flags
& ~((u64
)BTRFS_KEY_TYPE_MASK
)) | type
;
518 btrfs_set_disk_key_flags(key
, flags
);
521 static inline u64
btrfs_header_blocknr(struct btrfs_header
*h
)
523 return le64_to_cpu(h
->blocknr
);
526 static inline void btrfs_set_header_blocknr(struct btrfs_header
*h
, u64 blocknr
)
528 h
->blocknr
= cpu_to_le64(blocknr
);
531 static inline u64
btrfs_header_parentid(struct btrfs_header
*h
)
533 return le64_to_cpu(h
->parentid
);
536 static inline void btrfs_set_header_parentid(struct btrfs_header
*h
,
539 h
->parentid
= cpu_to_le64(parentid
);
542 static inline u16
btrfs_header_nritems(struct btrfs_header
*h
)
544 return le16_to_cpu(h
->nritems
);
547 static inline void btrfs_set_header_nritems(struct btrfs_header
*h
, u16 val
)
549 h
->nritems
= cpu_to_le16(val
);
552 static inline u16
btrfs_header_flags(struct btrfs_header
*h
)
554 return le16_to_cpu(h
->flags
);
557 static inline void btrfs_set_header_flags(struct btrfs_header
*h
, u16 val
)
559 h
->flags
= cpu_to_le16(val
);
562 static inline int btrfs_header_level(struct btrfs_header
*h
)
564 return btrfs_header_flags(h
) & (BTRFS_MAX_LEVEL
- 1);
567 static inline void btrfs_set_header_level(struct btrfs_header
*h
, int level
)
570 BUG_ON(level
> BTRFS_MAX_LEVEL
);
571 flags
= btrfs_header_flags(h
) & ~(BTRFS_MAX_LEVEL
- 1);
572 btrfs_set_header_flags(h
, flags
| level
);
575 static inline int btrfs_is_leaf(struct btrfs_node
*n
)
577 return (btrfs_header_level(&n
->header
) == 0);
580 static inline u64
btrfs_root_blocknr(struct btrfs_root_item
*item
)
582 return le64_to_cpu(item
->blocknr
);
585 static inline void btrfs_set_root_blocknr(struct btrfs_root_item
*item
, u64 val
)
587 item
->blocknr
= cpu_to_le64(val
);
590 static inline u32
btrfs_root_refs(struct btrfs_root_item
*item
)
592 return le32_to_cpu(item
->refs
);
595 static inline void btrfs_set_root_refs(struct btrfs_root_item
*item
, u32 val
)
597 item
->refs
= cpu_to_le32(val
);
600 static inline u64
btrfs_super_blocknr(struct btrfs_super_block
*s
)
602 return le64_to_cpu(s
->blocknr
);
605 static inline void btrfs_set_super_blocknr(struct btrfs_super_block
*s
, u64 val
)
607 s
->blocknr
= cpu_to_le64(val
);
610 static inline u64
btrfs_super_root(struct btrfs_super_block
*s
)
612 return le64_to_cpu(s
->root
);
615 static inline void btrfs_set_super_root(struct btrfs_super_block
*s
, u64 val
)
617 s
->root
= cpu_to_le64(val
);
620 static inline u64
btrfs_super_total_blocks(struct btrfs_super_block
*s
)
622 return le64_to_cpu(s
->total_blocks
);
625 static inline void btrfs_set_super_total_blocks(struct btrfs_super_block
*s
,
628 s
->total_blocks
= cpu_to_le64(val
);
631 static inline u64
btrfs_super_blocks_used(struct btrfs_super_block
*s
)
633 return le64_to_cpu(s
->blocks_used
);
636 static inline void btrfs_set_super_blocks_used(struct btrfs_super_block
*s
,
639 s
->blocks_used
= cpu_to_le64(val
);
642 static inline u32
btrfs_super_blocksize(struct btrfs_super_block
*s
)
644 return le32_to_cpu(s
->blocksize
);
647 static inline void btrfs_set_super_blocksize(struct btrfs_super_block
*s
,
650 s
->blocksize
= cpu_to_le32(val
);
653 static inline u8
*btrfs_leaf_data(struct btrfs_leaf
*l
)
655 return (u8
*)l
->items
;
657 /* helper function to cast into the data area of the leaf. */
658 #define btrfs_item_ptr(leaf, slot, type) \
659 ((type *)(btrfs_leaf_data(leaf) + \
660 btrfs_item_offset((leaf)->items + (slot))))
662 struct btrfs_buffer
*btrfs_alloc_free_block(struct btrfs_trans_handle
*trans
,
663 struct btrfs_root
*root
);
664 int btrfs_inc_ref(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
665 struct btrfs_buffer
*buf
);
666 int btrfs_free_extent(struct btrfs_trans_handle
*trans
, struct btrfs_root
667 *root
, u64 blocknr
, u64 num_blocks
, int pin
);
668 int btrfs_search_slot(struct btrfs_trans_handle
*trans
, struct btrfs_root
669 *root
, struct btrfs_key
*key
, struct btrfs_path
*p
, int
671 void btrfs_release_path(struct btrfs_root
*root
, struct btrfs_path
*p
);
672 void btrfs_init_path(struct btrfs_path
*p
);
673 int btrfs_del_item(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
674 struct btrfs_path
*path
);
675 int btrfs_insert_item(struct btrfs_trans_handle
*trans
, struct btrfs_root
676 *root
, struct btrfs_key
*key
, void *data
, u32 data_size
);
677 int btrfs_insert_empty_item(struct btrfs_trans_handle
*trans
, struct btrfs_root
678 *root
, struct btrfs_path
*path
, struct btrfs_key
679 *cpu_key
, u32 data_size
);
680 int btrfs_next_leaf(struct btrfs_root
*root
, struct btrfs_path
*path
);
681 int btrfs_leaf_free_space(struct btrfs_root
*root
, struct btrfs_leaf
*leaf
);
682 int btrfs_drop_snapshot(struct btrfs_trans_handle
*trans
, struct btrfs_root
683 *root
, struct btrfs_buffer
*snap
);
684 int btrfs_finish_extent_commit(struct btrfs_trans_handle
*trans
, struct
686 int btrfs_del_root(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
687 struct btrfs_key
*key
);
688 int btrfs_insert_root(struct btrfs_trans_handle
*trans
, struct btrfs_root
689 *root
, struct btrfs_key
*key
, struct btrfs_root_item
691 int btrfs_update_root(struct btrfs_trans_handle
*trans
, struct btrfs_root
692 *root
, struct btrfs_key
*key
, struct btrfs_root_item
694 int btrfs_find_last_root(struct btrfs_root
*root
, u64 objectid
, struct
695 btrfs_root_item
*item
, struct btrfs_key
*key
);
696 int btrfs_insert_dir_item(struct btrfs_trans_handle
*trans
, struct btrfs_root
697 *root
, char *name
, int name_len
, u64 dir
, u64
699 int btrfs_lookup_dir_item(struct btrfs_trans_handle
*trans
, struct btrfs_root
700 *root
, struct btrfs_path
*path
, u64 dir
, char *name
,
701 int name_len
, int mod
);
702 int btrfs_match_dir_item_name(struct btrfs_root
*root
, struct btrfs_path
*path
,
703 char *name
, int name_len
);