05c7707263f5e21448f54ca9ff7493662eee1173
[platform/upstream/btrfs-progs.git] / ctree.h
1 #ifndef __BTRFS__
2 #define __BTRFS__
3
4 #include "list.h"
5 #include "kerncompat.h"
6
7 #define BTRFS_MAGIC "_BtRfS_M"
8 #define BTRFS_BLOCKSIZE 1024
9
10 #define BTRFS_ROOT_TREE_OBJECTID 1
11 #define BTRFS_EXTENT_TREE_OBJECTID 2
12 #define BTRFS_FS_TREE_OBJECTID 3
13
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.
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)
27  */
28 struct btrfs_disk_key {
29         __le64 objectid;
30         __le32 flags;
31         __le64 offset;
32 } __attribute__ ((__packed__));
33
34 struct btrfs_key {
35         u64 objectid;
36         u32 flags;
37         u64 offset;
38 } __attribute__ ((__packed__));
39
40 /*
41  * every tree block (leaf or node) starts with this header.
42  */
43 struct btrfs_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 */
47         __le32 csum;
48         __le32 ham;
49         __le16 nritems;
50         __le16 flags;
51         /* generation flags to be added */
52 } __attribute__ ((__packed__));
53
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)))
57
58 struct btrfs_buffer;
59
60 struct btrfs_root_item {
61         __le64 blocknr;
62         __le32 flags;
63         __le64 block_limit;
64         __le64 blocks_used;
65         __le32 refs;
66 };
67
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  */
73 struct btrfs_root {
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;
80         int fp;
81         struct radix_tree_root cache_radix;
82         struct radix_tree_root pinned_radix;
83         struct list_head trans;
84         struct list_head cache;
85         int cache_size;
86         int ref_cows;
87         struct btrfs_root_item root_item;
88         struct btrfs_key root_key;
89 };
90
91 /*
92  * the super block basically lists the main trees of the FS
93  * it currently lacks any block count etc etc
94  */
95 struct btrfs_super_block {
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;
105 } __attribute__ ((__packed__));
106
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  */
112 struct btrfs_item {
113         struct btrfs_disk_key key;
114         __le16 offset;
115         __le16 size;
116 } __attribute__ ((__packed__));
117
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  */
125 #define LEAF_DATA_SIZE (BTRFS_BLOCKSIZE - sizeof(struct btrfs_header))
126 struct btrfs_leaf {
127         struct btrfs_header header;
128         union {
129                 struct btrfs_item items[LEAF_DATA_SIZE/
130                                         sizeof(struct btrfs_item)];
131                 u8 data[BTRFS_BLOCKSIZE - sizeof(struct btrfs_header)];
132         };
133 } __attribute__ ((__packed__));
134
135 /*
136  * all non-leaf blocks are nodes, they hold only keys and pointers to
137  * other blocks
138  */
139 struct btrfs_node {
140         struct btrfs_header header;
141         struct btrfs_disk_key keys[NODEPTRS_PER_BLOCK];
142         __le64 blockptrs[NODEPTRS_PER_BLOCK];
143 } __attribute__ ((__packed__));
144
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  */
149 struct btrfs_extent_item {
150         __le32 refs;
151         __le64 owner;
152 } __attribute__ ((__packed__));
153
154 /*
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.
158  *
159  * The slots array records the index of the item or block pointer
160  * used while walking the tree.
161  */
162 struct btrfs_path {
163         struct btrfs_buffer *nodes[BTRFS_MAX_LEVEL];
164         int slots[BTRFS_MAX_LEVEL];
165 };
166
167 static inline u64 btrfs_extent_owner(struct btrfs_extent_item *ei)
168 {
169         return le64_to_cpu(ei->owner);
170 }
171
172 static inline void btrfs_set_extent_owner(struct btrfs_extent_item *ei, u64 val)
173 {
174         ei->owner = cpu_to_le64(val);
175 }
176
177 static inline u32 btrfs_extent_refs(struct btrfs_extent_item *ei)
178 {
179         return le32_to_cpu(ei->refs);
180 }
181
182 static inline void btrfs_set_extent_refs(struct btrfs_extent_item *ei, u32 val)
183 {
184         ei->refs = cpu_to_le32(val);
185 }
186
187 static inline u64 btrfs_node_blockptr(struct btrfs_node *n, int nr)
188 {
189         return le64_to_cpu(n->blockptrs[nr]);
190 }
191
192 static inline void btrfs_set_node_blockptr(struct btrfs_node *n, int nr,
193                                            u64 val)
194 {
195         n->blockptrs[nr] = cpu_to_le64(val);
196 }
197
198 static inline u16 btrfs_item_offset(struct btrfs_item *item)
199 {
200         return le16_to_cpu(item->offset);
201 }
202
203 static inline void btrfs_set_item_offset(struct btrfs_item *item, u16 val)
204 {
205         item->offset = cpu_to_le16(val);
206 }
207
208 static inline u16 btrfs_item_end(struct btrfs_item *item)
209 {
210         return le16_to_cpu(item->offset) + le16_to_cpu(item->size);
211 }
212
213 static inline u16 btrfs_item_size(struct btrfs_item *item)
214 {
215         return le16_to_cpu(item->size);
216 }
217
218 static inline void btrfs_set_item_size(struct btrfs_item *item, u16 val)
219 {
220         item->size = cpu_to_le16(val);
221 }
222
223 static 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
231 static 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
239 static inline u64 btrfs_key_objectid(struct btrfs_disk_key *disk)
240 {
241         return le64_to_cpu(disk->objectid);
242 }
243
244 static inline void btrfs_set_key_objectid(struct btrfs_disk_key *disk,
245                                           u64 val)
246 {
247         disk->objectid = cpu_to_le64(val);
248 }
249
250 static inline u64 btrfs_key_offset(struct btrfs_disk_key *disk)
251 {
252         return le64_to_cpu(disk->offset);
253 }
254
255 static inline void btrfs_set_key_offset(struct btrfs_disk_key *disk,
256                                           u64 val)
257 {
258         disk->offset = cpu_to_le64(val);
259 }
260
261 static inline u32 btrfs_key_flags(struct btrfs_disk_key *disk)
262 {
263         return le32_to_cpu(disk->flags);
264 }
265
266 static inline void btrfs_set_key_flags(struct btrfs_disk_key *disk,
267                                           u32 val)
268 {
269         disk->flags = cpu_to_le32(val);
270 }
271
272 static inline u64 btrfs_header_blocknr(struct btrfs_header *h)
273 {
274         return le64_to_cpu(h->blocknr);
275 }
276
277 static inline void btrfs_set_header_blocknr(struct btrfs_header *h, u64 blocknr)
278 {
279         h->blocknr = cpu_to_le64(blocknr);
280 }
281
282 static inline u64 btrfs_header_parentid(struct btrfs_header *h)
283 {
284         return le64_to_cpu(h->parentid);
285 }
286
287 static inline void btrfs_set_header_parentid(struct btrfs_header *h,
288                                              u64 parentid)
289 {
290         h->parentid = cpu_to_le64(parentid);
291 }
292
293 static inline u16 btrfs_header_nritems(struct btrfs_header *h)
294 {
295         return le16_to_cpu(h->nritems);
296 }
297
298 static inline void btrfs_set_header_nritems(struct btrfs_header *h, u16 val)
299 {
300         h->nritems = cpu_to_le16(val);
301 }
302
303 static inline u16 btrfs_header_flags(struct btrfs_header *h)
304 {
305         return le16_to_cpu(h->flags);
306 }
307
308 static inline void btrfs_set_header_flags(struct btrfs_header *h, u16 val)
309 {
310         h->flags = cpu_to_le16(val);
311 }
312
313 static inline int btrfs_header_level(struct btrfs_header *h)
314 {
315         return btrfs_header_flags(h) & (BTRFS_MAX_LEVEL - 1);
316 }
317
318 static inline void btrfs_set_header_level(struct btrfs_header *h, int level)
319 {
320         u16 flags;
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);
324 }
325
326 static inline int btrfs_is_leaf(struct btrfs_node *n)
327 {
328         return (btrfs_header_level(&n->header) == 0);
329 }
330
331 static inline u64 btrfs_root_blocknr(struct btrfs_root_item *item)
332 {
333         return le64_to_cpu(item->blocknr);
334 }
335
336 static inline void btrfs_set_root_blocknr(struct btrfs_root_item *item, u64 val)
337 {
338         item->blocknr = cpu_to_le64(val);
339 }
340
341 static inline u32 btrfs_root_refs(struct btrfs_root_item *item)
342 {
343         return le32_to_cpu(item->refs);
344 }
345
346 static inline void btrfs_set_root_refs(struct btrfs_root_item *item, u32 val)
347 {
348         item->refs = cpu_to_le32(val);
349 }
350
351 static inline u64 btrfs_super_blocknr(struct btrfs_super_block *s)
352 {
353         return le64_to_cpu(s->blocknr);
354 }
355
356 static inline void btrfs_set_super_blocknr(struct btrfs_super_block *s, u64 val)
357 {
358         s->blocknr = cpu_to_le64(val);
359 }
360
361 static inline u64 btrfs_super_root(struct btrfs_super_block *s)
362 {
363         return le64_to_cpu(s->root);
364 }
365
366 static inline void btrfs_set_super_root(struct btrfs_super_block *s, u64 val)
367 {
368         s->root = cpu_to_le64(val);
369 }
370
371 static inline u64 btrfs_super_total_blocks(struct btrfs_super_block *s)
372 {
373         return le64_to_cpu(s->total_blocks);
374 }
375
376 static 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
382 static inline u64 btrfs_super_blocks_used(struct btrfs_super_block *s)
383 {
384         return le64_to_cpu(s->blocks_used);
385 }
386
387 static 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
393 static inline u16 btrfs_super_blocksize(struct btrfs_super_block *s)
394 {
395         return le16_to_cpu(s->blocksize);
396 }
397
398 static inline void btrfs_set_super_blocksize(struct btrfs_super_block *s,
399                                                 u16 val)
400 {
401         s->blocksize = cpu_to_le16(val);
402 }
403
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
408 struct btrfs_buffer *btrfs_alloc_free_block(struct btrfs_root *root);
409 int btrfs_inc_ref(struct btrfs_root *root, struct btrfs_buffer *buf);
410 int btrfs_free_extent(struct btrfs_root *root, u64 blocknr, u64 num_blocks);
411 int btrfs_search_slot(struct btrfs_root *root, struct btrfs_key *key,
412                 struct btrfs_path *p, int ins_len, int cow);
413 void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p);
414 void btrfs_init_path(struct btrfs_path *p);
415 int btrfs_del_item(struct btrfs_root *root, struct btrfs_path *path);
416 int btrfs_insert_item(struct btrfs_root *root, struct btrfs_key *key,
417                 void *data, int data_size);
418 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path);
419 int btrfs_leaf_free_space(struct btrfs_leaf *leaf);
420 int btrfs_drop_snapshot(struct btrfs_root *root, struct btrfs_buffer *snap);
421 int btrfs_finish_extent_commit(struct btrfs_root *root);
422 int btrfs_del_root(struct btrfs_root *root, struct btrfs_key *key);
423 int btrfs_insert_root(struct btrfs_root *root, struct btrfs_key *key,
424                       struct btrfs_root_item *item);
425 int btrfs_update_root(struct btrfs_root *root, struct btrfs_key *key,
426                       struct btrfs_root_item *item);
427 int btrfs_find_last_root(struct btrfs_root *root, u64 objectid,
428                         struct btrfs_root_item *item, struct btrfs_key *key);
429 #endif