]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/btrfs/ctree.h
Btrfs: add leaf data casting helper
[mirror_ubuntu-artful-kernel.git] / fs / btrfs / ctree.h
CommitLineData
234b63a0
CM
1#ifndef __BTRFS__
2#define __BTRFS__
eb60ceac 3
ed2ff2cb 4#include "list.h"
e2fa7227 5#include "kerncompat.h"
ed2ff2cb 6
3768f368 7#define BTRFS_MAGIC "_BtRfS_M"
234b63a0 8#define BTRFS_BLOCKSIZE 1024
eb60ceac 9
3768f368
CM
10#define BTRFS_ROOT_TREE_OBJECTID 1
11#define BTRFS_EXTENT_TREE_OBJECTID 2
12#define BTRFS_FS_TREE_OBJECTID 3
13
fec577fb
CM
14/*
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.
21 *
22 * offset is the starting byte offset for this key in the stream.
e2fa7227
CM
23 *
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)
fec577fb 27 */
e2fa7227
CM
28struct btrfs_disk_key {
29 __le64 objectid;
30 __le32 flags;
31 __le64 offset;
32} __attribute__ ((__packed__));
33
34struct btrfs_key {
eb60ceac
CM
35 u64 objectid;
36 u32 flags;
37 u64 offset;
38} __attribute__ ((__packed__));
39
fec577fb
CM
40/*
41 * every tree block (leaf or node) starts with this header.
42 */
bb492bb0 43struct btrfs_header {
3768f368 44 u8 fsid[16]; /* FS specific uuid */
bb492bb0
CM
45 __le64 blocknr; /* which block this node is supposed to live in */
46 __le64 parentid; /* objectid of the tree root */
47 __le32 csum;
48 __le32 ham;
49 __le16 nritems;
50 __le16 flags;
fec577fb 51 /* generation flags to be added */
eb60ceac
CM
52} __attribute__ ((__packed__));
53
234b63a0
CM
54#define BTRFS_MAX_LEVEL 8
55#define NODEPTRS_PER_BLOCK ((BTRFS_BLOCKSIZE - sizeof(struct btrfs_header)) / \
e2fa7227 56 (sizeof(struct btrfs_disk_key) + sizeof(u64)))
eb60ceac 57
234b63a0 58struct btrfs_buffer;
d97e63b6 59
3768f368
CM
60struct btrfs_root_item {
61 __le64 blocknr;
62 __le32 flags;
63 __le64 block_limit;
64 __le64 blocks_used;
65 __le32 refs;
66};
67
fec577fb
CM
68/*
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.
72 */
234b63a0
CM
73struct btrfs_root {
74 struct btrfs_buffer *node;
75 struct btrfs_buffer *commit_root;
76 struct btrfs_root *extent_root;
3768f368 77 struct btrfs_root *tree_root;
e2fa7227
CM
78 struct btrfs_key current_insert;
79 struct btrfs_key last_insert;
eb60ceac
CM
80 int fp;
81 struct radix_tree_root cache_radix;
a28ec197 82 struct radix_tree_root pinned_radix;
ed2ff2cb
CM
83 struct list_head trans;
84 struct list_head cache;
85 int cache_size;
3768f368
CM
86 int ref_cows;
87 struct btrfs_root_item root_item;
88 struct btrfs_key root_key;
eb60ceac
CM
89};
90
fec577fb
CM
91/*
92 * the super block basically lists the main trees of the FS
93 * it currently lacks any block count etc etc
94 */
234b63a0 95struct btrfs_super_block {
3768f368
CM
96 u8 fsid[16]; /* FS specific uuid */
97 __le64 blocknr; /* this block number */
98 __le32 csum;
99 __le64 magic;
100 __le16 blocksize;
101 __le64 generation;
102 __le64 root;
103 __le64 total_blocks;
104 __le64 blocks_used;
cfaa7295
CM
105} __attribute__ ((__packed__));
106
fec577fb
CM
107/*
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)
111 */
0783fcfc 112struct btrfs_item {
e2fa7227 113 struct btrfs_disk_key key;
0783fcfc
CM
114 __le16 offset;
115 __le16 size;
eb60ceac
CM
116} __attribute__ ((__packed__));
117
fec577fb
CM
118/*
119 * leaves have an item area and a data area:
120 * [item0, item1....itemN] [free space] [dataN...data1, data0]
121 *
122 * The data is separate from the items to get the keys closer together
123 * during searches.
124 */
234b63a0
CM
125#define LEAF_DATA_SIZE (BTRFS_BLOCKSIZE - sizeof(struct btrfs_header))
126struct btrfs_leaf {
bb492bb0 127 struct btrfs_header header;
eb60ceac 128 union {
0783fcfc
CM
129 struct btrfs_item items[LEAF_DATA_SIZE/
130 sizeof(struct btrfs_item)];
234b63a0 131 u8 data[BTRFS_BLOCKSIZE - sizeof(struct btrfs_header)];
eb60ceac
CM
132 };
133} __attribute__ ((__packed__));
134
fec577fb
CM
135/*
136 * all non-leaf blocks are nodes, they hold only keys and pointers to
137 * other blocks
138 */
234b63a0 139struct btrfs_node {
bb492bb0 140 struct btrfs_header header;
e2fa7227 141 struct btrfs_disk_key keys[NODEPTRS_PER_BLOCK];
1d4f8a0c 142 __le64 blockptrs[NODEPTRS_PER_BLOCK];
eb60ceac
CM
143} __attribute__ ((__packed__));
144
fec577fb
CM
145/*
146 * items in the extent btree are used to record the objectid of the
147 * owner of the block and the number of references
148 */
234b63a0 149struct btrfs_extent_item {
cf27e1ee
CM
150 __le32 refs;
151 __le64 owner;
d97e63b6
CM
152} __attribute__ ((__packed__));
153
fec577fb 154/*
234b63a0
CM
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
fec577fb
CM
157 * to any other levels that are present.
158 *
159 * The slots array records the index of the item or block pointer
160 * used while walking the tree.
161 */
234b63a0
CM
162struct btrfs_path {
163 struct btrfs_buffer *nodes[BTRFS_MAX_LEVEL];
164 int slots[BTRFS_MAX_LEVEL];
eb60ceac 165};
5de08d7d 166
234b63a0 167static inline u64 btrfs_extent_owner(struct btrfs_extent_item *ei)
cf27e1ee
CM
168{
169 return le64_to_cpu(ei->owner);
170}
171
234b63a0 172static inline void btrfs_set_extent_owner(struct btrfs_extent_item *ei, u64 val)
cf27e1ee
CM
173{
174 ei->owner = cpu_to_le64(val);
175}
176
234b63a0 177static inline u32 btrfs_extent_refs(struct btrfs_extent_item *ei)
cf27e1ee
CM
178{
179 return le32_to_cpu(ei->refs);
180}
181
234b63a0 182static inline void btrfs_set_extent_refs(struct btrfs_extent_item *ei, u32 val)
cf27e1ee
CM
183{
184 ei->refs = cpu_to_le32(val);
185}
186
234b63a0 187static inline u64 btrfs_node_blockptr(struct btrfs_node *n, int nr)
1d4f8a0c
CM
188{
189 return le64_to_cpu(n->blockptrs[nr]);
190}
191
234b63a0
CM
192static inline void btrfs_set_node_blockptr(struct btrfs_node *n, int nr,
193 u64 val)
1d4f8a0c
CM
194{
195 n->blockptrs[nr] = cpu_to_le64(val);
196}
197
0783fcfc
CM
198static inline u16 btrfs_item_offset(struct btrfs_item *item)
199{
200 return le16_to_cpu(item->offset);
201}
202
203static inline void btrfs_set_item_offset(struct btrfs_item *item, u16 val)
204{
205 item->offset = cpu_to_le16(val);
206}
207
208static inline u16 btrfs_item_end(struct btrfs_item *item)
209{
210 return le16_to_cpu(item->offset) + le16_to_cpu(item->size);
211}
212
213static inline u16 btrfs_item_size(struct btrfs_item *item)
214{
215 return le16_to_cpu(item->size);
216}
217
218static inline void btrfs_set_item_size(struct btrfs_item *item, u16 val)
219{
220 item->size = cpu_to_le16(val);
221}
222
e2fa7227
CM
223static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu,
224 struct btrfs_disk_key *disk)
225{
226 cpu->offset = le64_to_cpu(disk->offset);
227 cpu->flags = le32_to_cpu(disk->flags);
228 cpu->objectid = le64_to_cpu(disk->objectid);
229}
230
231static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk,
232 struct btrfs_key *cpu)
233{
234 disk->offset = cpu_to_le64(cpu->offset);
235 disk->flags = cpu_to_le32(cpu->flags);
236 disk->objectid = cpu_to_le64(cpu->objectid);
237}
238
239static inline u64 btrfs_key_objectid(struct btrfs_disk_key *disk)
240{
241 return le64_to_cpu(disk->objectid);
242}
243
244static inline void btrfs_set_key_objectid(struct btrfs_disk_key *disk,
245 u64 val)
246{
247 disk->objectid = cpu_to_le64(val);
248}
249
250static inline u64 btrfs_key_offset(struct btrfs_disk_key *disk)
251{
252 return le64_to_cpu(disk->offset);
253}
254
255static inline void btrfs_set_key_offset(struct btrfs_disk_key *disk,
256 u64 val)
257{
258 disk->offset = cpu_to_le64(val);
259}
260
261static inline u32 btrfs_key_flags(struct btrfs_disk_key *disk)
262{
263 return le32_to_cpu(disk->flags);
264}
265
266static inline void btrfs_set_key_flags(struct btrfs_disk_key *disk,
267 u32 val)
268{
269 disk->flags = cpu_to_le32(val);
270}
271
bb492bb0 272static inline u64 btrfs_header_blocknr(struct btrfs_header *h)
7518a238 273{
bb492bb0 274 return le64_to_cpu(h->blocknr);
7518a238
CM
275}
276
bb492bb0 277static inline void btrfs_set_header_blocknr(struct btrfs_header *h, u64 blocknr)
7518a238 278{
bb492bb0 279 h->blocknr = cpu_to_le64(blocknr);
7518a238
CM
280}
281
bb492bb0 282static inline u64 btrfs_header_parentid(struct btrfs_header *h)
7518a238 283{
bb492bb0 284 return le64_to_cpu(h->parentid);
7518a238
CM
285}
286
bb492bb0
CM
287static inline void btrfs_set_header_parentid(struct btrfs_header *h,
288 u64 parentid)
7518a238 289{
bb492bb0 290 h->parentid = cpu_to_le64(parentid);
7518a238
CM
291}
292
bb492bb0 293static inline u16 btrfs_header_nritems(struct btrfs_header *h)
7518a238 294{
bb492bb0 295 return le16_to_cpu(h->nritems);
7518a238
CM
296}
297
bb492bb0 298static inline void btrfs_set_header_nritems(struct btrfs_header *h, u16 val)
7518a238 299{
bb492bb0 300 h->nritems = cpu_to_le16(val);
7518a238
CM
301}
302
bb492bb0 303static inline u16 btrfs_header_flags(struct btrfs_header *h)
7518a238 304{
bb492bb0 305 return le16_to_cpu(h->flags);
7518a238
CM
306}
307
bb492bb0 308static inline void btrfs_set_header_flags(struct btrfs_header *h, u16 val)
7518a238 309{
bb492bb0 310 h->flags = cpu_to_le16(val);
7518a238
CM
311}
312
bb492bb0 313static inline int btrfs_header_level(struct btrfs_header *h)
7518a238 314{
234b63a0 315 return btrfs_header_flags(h) & (BTRFS_MAX_LEVEL - 1);
7518a238
CM
316}
317
bb492bb0 318static inline void btrfs_set_header_level(struct btrfs_header *h, int level)
7518a238 319{
bb492bb0 320 u16 flags;
234b63a0
CM
321 BUG_ON(level > BTRFS_MAX_LEVEL);
322 flags = btrfs_header_flags(h) & ~(BTRFS_MAX_LEVEL - 1);
7518a238
CM
323 btrfs_set_header_flags(h, flags | level);
324}
325
234b63a0 326static inline int btrfs_is_leaf(struct btrfs_node *n)
7518a238
CM
327{
328 return (btrfs_header_level(&n->header) == 0);
329}
330
3768f368
CM
331static inline u64 btrfs_root_blocknr(struct btrfs_root_item *item)
332{
333 return le64_to_cpu(item->blocknr);
334}
335
336static inline void btrfs_set_root_blocknr(struct btrfs_root_item *item, u64 val)
337{
338 item->blocknr = cpu_to_le64(val);
339}
340
341static inline u32 btrfs_root_refs(struct btrfs_root_item *item)
342{
343 return le32_to_cpu(item->refs);
344}
345
346static inline void btrfs_set_root_refs(struct btrfs_root_item *item, u32 val)
347{
348 item->refs = cpu_to_le32(val);
349}
350
351static inline u64 btrfs_super_blocknr(struct btrfs_super_block *s)
352{
353 return le64_to_cpu(s->blocknr);
354}
355
356static inline void btrfs_set_super_blocknr(struct btrfs_super_block *s, u64 val)
357{
358 s->blocknr = cpu_to_le64(val);
359}
360
361static inline u64 btrfs_super_root(struct btrfs_super_block *s)
362{
363 return le64_to_cpu(s->root);
364}
365
366static inline void btrfs_set_super_root(struct btrfs_super_block *s, u64 val)
367{
368 s->root = cpu_to_le64(val);
369}
370
371static inline u64 btrfs_super_total_blocks(struct btrfs_super_block *s)
372{
373 return le64_to_cpu(s->total_blocks);
374}
375
376static inline void btrfs_set_super_total_blocks(struct btrfs_super_block *s,
377 u64 val)
378{
379 s->total_blocks = cpu_to_le64(val);
380}
381
382static inline u64 btrfs_super_blocks_used(struct btrfs_super_block *s)
383{
384 return le64_to_cpu(s->blocks_used);
385}
386
387static inline void btrfs_set_super_blocks_used(struct btrfs_super_block *s,
388 u64 val)
389{
390 s->blocks_used = cpu_to_le64(val);
391}
392
393static inline u16 btrfs_super_blocksize(struct btrfs_super_block *s)
394{
395 return le16_to_cpu(s->blocksize);
396}
397
398static inline void btrfs_set_super_blocksize(struct btrfs_super_block *s,
399 u16 val)
400{
401 s->blocksize = cpu_to_le16(val);
402}
403
4beb1b8b
CM
404/* helper function to cast into the data area of the leaf. */
405#define btrfs_item_ptr(leaf, slot, type) \
406 ((type *)((leaf)->data + btrfs_item_offset((leaf)->items + (slot))))
407
234b63a0
CM
408struct btrfs_buffer *btrfs_alloc_free_block(struct btrfs_root *root);
409int btrfs_inc_ref(struct btrfs_root *root, struct btrfs_buffer *buf);
410int btrfs_free_extent(struct btrfs_root *root, u64 blocknr, u64 num_blocks);
411int btrfs_search_slot(struct btrfs_root *root, struct btrfs_key *key,
412 struct btrfs_path *p, int ins_len, int cow);
413void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p);
414void btrfs_init_path(struct btrfs_path *p);
415int btrfs_del_item(struct btrfs_root *root, struct btrfs_path *path);
416int btrfs_insert_item(struct btrfs_root *root, struct btrfs_key *key,
e2fa7227 417 void *data, int data_size);
234b63a0
CM
418int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path);
419int btrfs_leaf_free_space(struct btrfs_leaf *leaf);
420int btrfs_drop_snapshot(struct btrfs_root *root, struct btrfs_buffer *snap);
421int btrfs_finish_extent_commit(struct btrfs_root *root);
3768f368
CM
422int btrfs_del_root(struct btrfs_root *root, struct btrfs_key *key);
423int btrfs_insert_root(struct btrfs_root *root, struct btrfs_key *key,
424 struct btrfs_root_item *item);
425int btrfs_update_root(struct btrfs_root *root, struct btrfs_key *key,
426 struct btrfs_root_item *item);
427int btrfs_find_last_root(struct btrfs_root *root, u64 objectid,
428 struct btrfs_root_item *item, struct btrfs_key *key);
eb60ceac 429#endif