5 #include "kerncompat.h"
7 #define BTRFS_MAGIC "_BtRfS_M"
8 #define BTRFS_BLOCKSIZE 1024
10 #define BTRFS_ROOT_TREE_OBJECTID 1
11 #define BTRFS_EXTENT_TREE_OBJECTID 2
12 #define BTRFS_FS_TREE_OBJECTID 3
15 * the key defines the order in the tree, and so it also defines (optimal)
16 * block layout. objectid corresonds to the inode number. The flags
17 * tells us things about the object, and is a kind of stream selector.
18 * so for a given inode, keys with flags of 1 might refer to the inode
19 * data, flags of 2 may point to file data in the btree and flags == 3
20 * may point to extents.
22 * offset is the starting byte offset for this key in the stream.
24 * btrfs_disk_key is in disk byte order. struct btrfs_key is always
25 * in cpu native order. Otherwise they are identical and their sizes
26 * should be the same (ie both packed)
28 struct btrfs_disk_key
{
32 } __attribute__ ((__packed__
));
38 } __attribute__ ((__packed__
));
41 * every tree block (leaf or node) starts with this header.
44 u8 fsid
[16]; /* FS specific uuid */
45 __le64 blocknr
; /* which block this node is supposed to live in */
46 __le64 parentid
; /* objectid of the tree root */
51 /* generation flags to be added */
52 } __attribute__ ((__packed__
));
54 #define BTRFS_MAX_LEVEL 8
55 #define NODEPTRS_PER_BLOCK ((BTRFS_BLOCKSIZE - sizeof(struct btrfs_header)) / \
56 (sizeof(struct btrfs_disk_key) + sizeof(u64)))
60 struct btrfs_root_item
{
69 * in ram representation of the tree. extent_root is used for all allocations
70 * and for the extent tree extent_root root. current_insert is used
71 * only for the extent tree.
74 struct btrfs_buffer
*node
;
75 struct btrfs_buffer
*commit_root
;
76 struct btrfs_root
*extent_root
;
77 struct btrfs_root
*tree_root
;
78 struct btrfs_key current_insert
;
79 struct btrfs_key last_insert
;
81 struct radix_tree_root cache_radix
;
82 struct radix_tree_root pinned_radix
;
83 struct list_head trans
;
84 struct list_head cache
;
87 struct btrfs_root_item root_item
;
88 struct btrfs_key root_key
;
92 * the super block basically lists the main trees of the FS
93 * it currently lacks any block count etc etc
95 struct btrfs_super_block
{
96 u8 fsid
[16]; /* FS specific uuid */
97 __le64 blocknr
; /* this block number */
105 } __attribute__ ((__packed__
));
108 * A leaf is full of items. The exact type of item is defined by
109 * the key flags parameter. offset and size tell us where to find
110 * the item in the leaf (relative to the start of the data area)
113 struct btrfs_disk_key key
;
116 } __attribute__ ((__packed__
));
119 * leaves have an item area and a data area:
120 * [item0, item1....itemN] [free space] [dataN...data1, data0]
122 * The data is separate from the items to get the keys closer together
125 #define LEAF_DATA_SIZE (BTRFS_BLOCKSIZE - sizeof(struct btrfs_header))
127 struct btrfs_header header
;
129 struct btrfs_item items
[LEAF_DATA_SIZE
/
130 sizeof(struct btrfs_item
)];
131 u8 data
[BTRFS_BLOCKSIZE
- sizeof(struct btrfs_header
)];
133 } __attribute__ ((__packed__
));
136 * all non-leaf blocks are nodes, they hold only keys and pointers to
140 struct btrfs_header header
;
141 struct btrfs_disk_key keys
[NODEPTRS_PER_BLOCK
];
142 __le64 blockptrs
[NODEPTRS_PER_BLOCK
];
143 } __attribute__ ((__packed__
));
146 * items in the extent btree are used to record the objectid of the
147 * owner of the block and the number of references
149 struct btrfs_extent_item
{
152 } __attribute__ ((__packed__
));
155 * btrfs_paths remember the path taken from the root down to the leaf.
156 * level 0 is always the leaf, and nodes[1...BTRFS_MAX_LEVEL] will point
157 * to any other levels that are present.
159 * The slots array records the index of the item or block pointer
160 * used while walking the tree.
163 struct btrfs_buffer
*nodes
[BTRFS_MAX_LEVEL
];
164 int slots
[BTRFS_MAX_LEVEL
];
167 static inline u64
btrfs_extent_owner(struct btrfs_extent_item
*ei
)
169 return le64_to_cpu(ei
->owner
);
172 static inline void btrfs_set_extent_owner(struct btrfs_extent_item
*ei
, u64 val
)
174 ei
->owner
= cpu_to_le64(val
);
177 static inline u32
btrfs_extent_refs(struct btrfs_extent_item
*ei
)
179 return le32_to_cpu(ei
->refs
);
182 static inline void btrfs_set_extent_refs(struct btrfs_extent_item
*ei
, u32 val
)
184 ei
->refs
= cpu_to_le32(val
);
187 static inline u64
btrfs_node_blockptr(struct btrfs_node
*n
, int nr
)
189 return le64_to_cpu(n
->blockptrs
[nr
]);
192 static inline void btrfs_set_node_blockptr(struct btrfs_node
*n
, int nr
,
195 n
->blockptrs
[nr
] = cpu_to_le64(val
);
198 static inline u16
btrfs_item_offset(struct btrfs_item
*item
)
200 return le16_to_cpu(item
->offset
);
203 static inline void btrfs_set_item_offset(struct btrfs_item
*item
, u16 val
)
205 item
->offset
= cpu_to_le16(val
);
208 static inline u16
btrfs_item_end(struct btrfs_item
*item
)
210 return le16_to_cpu(item
->offset
) + le16_to_cpu(item
->size
);
213 static inline u16
btrfs_item_size(struct btrfs_item
*item
)
215 return le16_to_cpu(item
->size
);
218 static inline void btrfs_set_item_size(struct btrfs_item
*item
, u16 val
)
220 item
->size
= cpu_to_le16(val
);
223 static inline void btrfs_disk_key_to_cpu(struct btrfs_key
*cpu
,
224 struct btrfs_disk_key
*disk
)
226 cpu
->offset
= le64_to_cpu(disk
->offset
);
227 cpu
->flags
= le32_to_cpu(disk
->flags
);
228 cpu
->objectid
= le64_to_cpu(disk
->objectid
);
231 static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key
*disk
,
232 struct btrfs_key
*cpu
)
234 disk
->offset
= cpu_to_le64(cpu
->offset
);
235 disk
->flags
= cpu_to_le32(cpu
->flags
);
236 disk
->objectid
= cpu_to_le64(cpu
->objectid
);
239 static inline u64
btrfs_key_objectid(struct btrfs_disk_key
*disk
)
241 return le64_to_cpu(disk
->objectid
);
244 static inline void btrfs_set_key_objectid(struct btrfs_disk_key
*disk
,
247 disk
->objectid
= cpu_to_le64(val
);
250 static inline u64
btrfs_key_offset(struct btrfs_disk_key
*disk
)
252 return le64_to_cpu(disk
->offset
);
255 static inline void btrfs_set_key_offset(struct btrfs_disk_key
*disk
,
258 disk
->offset
= cpu_to_le64(val
);
261 static inline u32
btrfs_key_flags(struct btrfs_disk_key
*disk
)
263 return le32_to_cpu(disk
->flags
);
266 static inline void btrfs_set_key_flags(struct btrfs_disk_key
*disk
,
269 disk
->flags
= cpu_to_le32(val
);
272 static inline u64
btrfs_header_blocknr(struct btrfs_header
*h
)
274 return le64_to_cpu(h
->blocknr
);
277 static inline void btrfs_set_header_blocknr(struct btrfs_header
*h
, u64 blocknr
)
279 h
->blocknr
= cpu_to_le64(blocknr
);
282 static inline u64
btrfs_header_parentid(struct btrfs_header
*h
)
284 return le64_to_cpu(h
->parentid
);
287 static inline void btrfs_set_header_parentid(struct btrfs_header
*h
,
290 h
->parentid
= cpu_to_le64(parentid
);
293 static inline u16
btrfs_header_nritems(struct btrfs_header
*h
)
295 return le16_to_cpu(h
->nritems
);
298 static inline void btrfs_set_header_nritems(struct btrfs_header
*h
, u16 val
)
300 h
->nritems
= cpu_to_le16(val
);
303 static inline u16
btrfs_header_flags(struct btrfs_header
*h
)
305 return le16_to_cpu(h
->flags
);
308 static inline void btrfs_set_header_flags(struct btrfs_header
*h
, u16 val
)
310 h
->flags
= cpu_to_le16(val
);
313 static inline int btrfs_header_level(struct btrfs_header
*h
)
315 return btrfs_header_flags(h
) & (BTRFS_MAX_LEVEL
- 1);
318 static inline void btrfs_set_header_level(struct btrfs_header
*h
, int level
)
321 BUG_ON(level
> BTRFS_MAX_LEVEL
);
322 flags
= btrfs_header_flags(h
) & ~(BTRFS_MAX_LEVEL
- 1);
323 btrfs_set_header_flags(h
, flags
| level
);
326 static inline int btrfs_is_leaf(struct btrfs_node
*n
)
328 return (btrfs_header_level(&n
->header
) == 0);
331 static inline u64
btrfs_root_blocknr(struct btrfs_root_item
*item
)
333 return le64_to_cpu(item
->blocknr
);
336 static inline void btrfs_set_root_blocknr(struct btrfs_root_item
*item
, u64 val
)
338 item
->blocknr
= cpu_to_le64(val
);
341 static inline u32
btrfs_root_refs(struct btrfs_root_item
*item
)
343 return le32_to_cpu(item
->refs
);
346 static inline void btrfs_set_root_refs(struct btrfs_root_item
*item
, u32 val
)
348 item
->refs
= cpu_to_le32(val
);
351 static inline u64
btrfs_super_blocknr(struct btrfs_super_block
*s
)
353 return le64_to_cpu(s
->blocknr
);
356 static inline void btrfs_set_super_blocknr(struct btrfs_super_block
*s
, u64 val
)
358 s
->blocknr
= cpu_to_le64(val
);
361 static inline u64
btrfs_super_root(struct btrfs_super_block
*s
)
363 return le64_to_cpu(s
->root
);
366 static inline void btrfs_set_super_root(struct btrfs_super_block
*s
, u64 val
)
368 s
->root
= cpu_to_le64(val
);
371 static inline u64
btrfs_super_total_blocks(struct btrfs_super_block
*s
)
373 return le64_to_cpu(s
->total_blocks
);
376 static inline void btrfs_set_super_total_blocks(struct btrfs_super_block
*s
,
379 s
->total_blocks
= cpu_to_le64(val
);
382 static inline u64
btrfs_super_blocks_used(struct btrfs_super_block
*s
)
384 return le64_to_cpu(s
->blocks_used
);
387 static inline void btrfs_set_super_blocks_used(struct btrfs_super_block
*s
,
390 s
->blocks_used
= cpu_to_le64(val
);
393 static inline u16
btrfs_super_blocksize(struct btrfs_super_block
*s
)
395 return le16_to_cpu(s
->blocksize
);
398 static inline void btrfs_set_super_blocksize(struct btrfs_super_block
*s
,
401 s
->blocksize
= cpu_to_le16(val
);
404 struct btrfs_buffer
*btrfs_alloc_free_block(struct btrfs_root
*root
);
405 int btrfs_inc_ref(struct btrfs_root
*root
, struct btrfs_buffer
*buf
);
406 int btrfs_free_extent(struct btrfs_root
*root
, u64 blocknr
, u64 num_blocks
);
407 int btrfs_search_slot(struct btrfs_root
*root
, struct btrfs_key
*key
,
408 struct btrfs_path
*p
, int ins_len
, int cow
);
409 void btrfs_release_path(struct btrfs_root
*root
, struct btrfs_path
*p
);
410 void btrfs_init_path(struct btrfs_path
*p
);
411 int btrfs_del_item(struct btrfs_root
*root
, struct btrfs_path
*path
);
412 int btrfs_insert_item(struct btrfs_root
*root
, struct btrfs_key
*key
,
413 void *data
, int data_size
);
414 int btrfs_next_leaf(struct btrfs_root
*root
, struct btrfs_path
*path
);
415 int btrfs_leaf_free_space(struct btrfs_leaf
*leaf
);
416 int btrfs_drop_snapshot(struct btrfs_root
*root
, struct btrfs_buffer
*snap
);
417 int btrfs_finish_extent_commit(struct btrfs_root
*root
);
418 int btrfs_del_root(struct btrfs_root
*root
, struct btrfs_key
*key
);
419 int btrfs_insert_root(struct btrfs_root
*root
, struct btrfs_key
*key
,
420 struct btrfs_root_item
*item
);
421 int btrfs_update_root(struct btrfs_root
*root
, struct btrfs_key
*key
,
422 struct btrfs_root_item
*item
);
423 int btrfs_find_last_root(struct btrfs_root
*root
, u64 objectid
,
424 struct btrfs_root_item
*item
, struct btrfs_key
*key
);