Btrfs: directory testing code and dir item fixes
[linux-2.6-block.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"
eb60ceac 8
3768f368
CM
9#define BTRFS_ROOT_TREE_OBJECTID 1
10#define BTRFS_EXTENT_TREE_OBJECTID 2
11#define BTRFS_FS_TREE_OBJECTID 3
12
fec577fb
CM
13/*
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.
20 *
21 * offset is the starting byte offset for this key in the stream.
e2fa7227
CM
22 *
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)
fec577fb 26 */
e2fa7227
CM
27struct btrfs_disk_key {
28 __le64 objectid;
e2fa7227 29 __le64 offset;
a1516c89 30 __le32 flags;
e2fa7227
CM
31} __attribute__ ((__packed__));
32
33struct btrfs_key {
eb60ceac 34 u64 objectid;
eb60ceac 35 u64 offset;
a1516c89 36 u32 flags;
eb60ceac
CM
37} __attribute__ ((__packed__));
38
fec577fb
CM
39/*
40 * every tree block (leaf or node) starts with this header.
41 */
bb492bb0 42struct btrfs_header {
3768f368 43 u8 fsid[16]; /* FS specific uuid */
bb492bb0
CM
44 __le64 blocknr; /* which block this node is supposed to live in */
45 __le64 parentid; /* objectid of the tree root */
46 __le32 csum;
47 __le32 ham;
48 __le16 nritems;
49 __le16 flags;
fec577fb 50 /* generation flags to be added */
eb60ceac
CM
51} __attribute__ ((__packed__));
52
234b63a0 53#define BTRFS_MAX_LEVEL 8
123abc88
CM
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))
eb60ceac 59
234b63a0 60struct btrfs_buffer;
fec577fb
CM
61/*
62 * the super block basically lists the main trees of the FS
63 * it currently lacks any block count etc etc
64 */
234b63a0 65struct btrfs_super_block {
3768f368
CM
66 u8 fsid[16]; /* FS specific uuid */
67 __le64 blocknr; /* this block number */
68 __le32 csum;
69 __le64 magic;
123abc88 70 __le32 blocksize;
3768f368
CM
71 __le64 generation;
72 __le64 root;
73 __le64 total_blocks;
74 __le64 blocks_used;
cfaa7295
CM
75} __attribute__ ((__packed__));
76
fec577fb 77/*
62e2749e 78 * A leaf is full of items. offset and size tell us where to find
fec577fb
CM
79 * the item in the leaf (relative to the start of the data area)
80 */
0783fcfc 81struct btrfs_item {
e2fa7227 82 struct btrfs_disk_key key;
123abc88 83 __le32 offset;
0783fcfc 84 __le16 size;
eb60ceac
CM
85} __attribute__ ((__packed__));
86
fec577fb
CM
87/*
88 * leaves have an item area and a data area:
89 * [item0, item1....itemN] [free space] [dataN...data1, data0]
90 *
91 * The data is separate from the items to get the keys closer together
92 * during searches.
93 */
234b63a0 94struct btrfs_leaf {
bb492bb0 95 struct btrfs_header header;
123abc88 96 struct btrfs_item items[];
eb60ceac
CM
97} __attribute__ ((__packed__));
98
fec577fb
CM
99/*
100 * all non-leaf blocks are nodes, they hold only keys and pointers to
101 * other blocks
102 */
123abc88
CM
103struct btrfs_key_ptr {
104 struct btrfs_disk_key key;
105 __le64 blockptr;
106} __attribute__ ((__packed__));
107
234b63a0 108struct btrfs_node {
bb492bb0 109 struct btrfs_header header;
123abc88 110 struct btrfs_key_ptr ptrs[];
eb60ceac
CM
111} __attribute__ ((__packed__));
112
fec577fb 113/*
234b63a0
CM
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
fec577fb
CM
116 * to any other levels that are present.
117 *
118 * The slots array records the index of the item or block pointer
119 * used while walking the tree.
120 */
234b63a0
CM
121struct btrfs_path {
122 struct btrfs_buffer *nodes[BTRFS_MAX_LEVEL];
123 int slots[BTRFS_MAX_LEVEL];
eb60ceac 124};
5de08d7d 125
62e2749e
CM
126/*
127 * items in the extent btree are used to record the objectid of the
128 * owner of the block and the number of references
129 */
130struct btrfs_extent_item {
131 __le32 refs;
132 __le64 owner;
133} __attribute__ ((__packed__));
134
135struct btrfs_dir_item {
136 __le64 objectid;
137 __le16 flags;
138 u8 type;
139} __attribute__ ((__packed__));
140
141struct btrfs_root_item {
142 __le64 blocknr;
143 __le32 flags;
144 __le64 block_limit;
145 __le64 blocks_used;
146 __le32 refs;
147};
148
149/*
150 * in ram representation of the tree. extent_root is used for all allocations
151 * and for the extent tree extent_root root. current_insert is used
152 * only for the extent tree.
153 */
154struct btrfs_root {
155 struct btrfs_buffer *node;
156 struct btrfs_buffer *commit_root;
157 struct btrfs_root *extent_root;
158 struct btrfs_root *tree_root;
159 struct btrfs_key current_insert;
160 struct btrfs_key last_insert;
161 int fp;
162 struct radix_tree_root cache_radix;
163 struct radix_tree_root pinned_radix;
164 struct list_head trans;
165 struct list_head cache;
166 int cache_size;
167 int ref_cows;
168 struct btrfs_root_item root_item;
169 struct btrfs_key root_key;
170 u32 blocksize;
171};
172
173
174/* the lower bits in the key flags defines the item type */
175#define BTRFS_KEY_TYPE_MAX 256
176#define BTRFS_KEY_TYPE_MASK (BTRFS_KEY_TYPE_MAX - 1)
177#define BTRFS_INODE_ITEM_KEY 1
178#define BTRFS_DIR_ITEM_KEY 2
179#define BTRFS_ROOT_ITEM_KEY 3
180#define BTRFS_EXTENT_ITEM_KEY 4
181#define BTRFS_STRING_ITEM_KEY 5
182
234b63a0 183static inline u64 btrfs_extent_owner(struct btrfs_extent_item *ei)
cf27e1ee
CM
184{
185 return le64_to_cpu(ei->owner);
186}
187
234b63a0 188static inline void btrfs_set_extent_owner(struct btrfs_extent_item *ei, u64 val)
cf27e1ee
CM
189{
190 ei->owner = cpu_to_le64(val);
191}
192
234b63a0 193static inline u32 btrfs_extent_refs(struct btrfs_extent_item *ei)
cf27e1ee
CM
194{
195 return le32_to_cpu(ei->refs);
196}
197
234b63a0 198static inline void btrfs_set_extent_refs(struct btrfs_extent_item *ei, u32 val)
cf27e1ee
CM
199{
200 ei->refs = cpu_to_le32(val);
201}
202
234b63a0 203static inline u64 btrfs_node_blockptr(struct btrfs_node *n, int nr)
1d4f8a0c 204{
123abc88 205 return le64_to_cpu(n->ptrs[nr].blockptr);
1d4f8a0c
CM
206}
207
234b63a0
CM
208static inline void btrfs_set_node_blockptr(struct btrfs_node *n, int nr,
209 u64 val)
1d4f8a0c 210{
123abc88 211 n->ptrs[nr].blockptr = cpu_to_le64(val);
1d4f8a0c
CM
212}
213
123abc88 214static inline u32 btrfs_item_offset(struct btrfs_item *item)
0783fcfc 215{
123abc88 216 return le32_to_cpu(item->offset);
0783fcfc
CM
217}
218
123abc88 219static inline void btrfs_set_item_offset(struct btrfs_item *item, u32 val)
0783fcfc 220{
123abc88 221 item->offset = cpu_to_le32(val);
0783fcfc
CM
222}
223
123abc88 224static inline u32 btrfs_item_end(struct btrfs_item *item)
0783fcfc 225{
123abc88 226 return le32_to_cpu(item->offset) + le16_to_cpu(item->size);
0783fcfc
CM
227}
228
229static inline u16 btrfs_item_size(struct btrfs_item *item)
230{
231 return le16_to_cpu(item->size);
232}
233
234static inline void btrfs_set_item_size(struct btrfs_item *item, u16 val)
235{
236 item->size = cpu_to_le16(val);
237}
238
1d4f6404
CM
239static inline u64 btrfs_dir_objectid(struct btrfs_dir_item *d)
240{
241 return le64_to_cpu(d->objectid);
242}
243
244static inline void btrfs_set_dir_objectid(struct btrfs_dir_item *d, u64 val)
245{
246 d->objectid = cpu_to_le64(val);
247}
248
249static inline u16 btrfs_dir_flags(struct btrfs_dir_item *d)
250{
251 return le16_to_cpu(d->flags);
252}
253
254static inline void btrfs_set_dir_flags(struct btrfs_dir_item *d, u16 val)
255{
256 d->flags = cpu_to_le16(val);
257}
258
259static inline u8 btrfs_dir_type(struct btrfs_dir_item *d)
260{
261 return d->type;
262}
263
264static inline void btrfs_set_dir_type(struct btrfs_dir_item *d, u8 val)
265{
266 d->type = val;
267}
268
269static inline u32 btrfs_dir_name_len(struct btrfs_item *i)
270{
271 return btrfs_item_size(i) - sizeof(struct btrfs_dir_item);
272}
273
e2fa7227
CM
274static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu,
275 struct btrfs_disk_key *disk)
276{
277 cpu->offset = le64_to_cpu(disk->offset);
278 cpu->flags = le32_to_cpu(disk->flags);
279 cpu->objectid = le64_to_cpu(disk->objectid);
280}
281
282static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk,
283 struct btrfs_key *cpu)
284{
285 disk->offset = cpu_to_le64(cpu->offset);
286 disk->flags = cpu_to_le32(cpu->flags);
287 disk->objectid = cpu_to_le64(cpu->objectid);
288}
289
62e2749e 290static inline u64 btrfs_disk_key_objectid(struct btrfs_disk_key *disk)
e2fa7227
CM
291{
292 return le64_to_cpu(disk->objectid);
293}
294
62e2749e
CM
295static inline void btrfs_set_disk_key_objectid(struct btrfs_disk_key *disk,
296 u64 val)
e2fa7227
CM
297{
298 disk->objectid = cpu_to_le64(val);
299}
300
62e2749e 301static inline u64 btrfs_disk_key_offset(struct btrfs_disk_key *disk)
e2fa7227
CM
302{
303 return le64_to_cpu(disk->offset);
304}
305
62e2749e
CM
306static inline void btrfs_set_disk_key_offset(struct btrfs_disk_key *disk,
307 u64 val)
e2fa7227
CM
308{
309 disk->offset = cpu_to_le64(val);
310}
311
62e2749e 312static inline u32 btrfs_disk_key_flags(struct btrfs_disk_key *disk)
e2fa7227
CM
313{
314 return le32_to_cpu(disk->flags);
315}
316
62e2749e
CM
317static inline void btrfs_set_disk_key_flags(struct btrfs_disk_key *disk,
318 u32 val)
e2fa7227
CM
319{
320 disk->flags = cpu_to_le32(val);
321}
322
62e2749e
CM
323static inline u32 btrfs_key_type(struct btrfs_key *key)
324{
325 return key->flags & BTRFS_KEY_TYPE_MASK;
326}
327
328static inline u32 btrfs_disk_key_type(struct btrfs_disk_key *key)
329{
330 return le32_to_cpu(key->flags) & BTRFS_KEY_TYPE_MASK;
331}
332
333static inline void btrfs_set_key_type(struct btrfs_key *key, u32 type)
334{
335 BUG_ON(type >= BTRFS_KEY_TYPE_MAX);
336 key->flags = (key->flags & ~((u64)BTRFS_KEY_TYPE_MASK)) | type;
337}
338
339static inline void btrfs_set_disk_key_type(struct btrfs_disk_key *key, u32 type)
340{
341 u32 flags = btrfs_disk_key_flags(key);
342 BUG_ON(type >= BTRFS_KEY_TYPE_MAX);
343 flags = (flags & ~((u64)BTRFS_KEY_TYPE_MASK)) | type;
344 btrfs_set_disk_key_flags(key, flags);
345}
346
347
348
bb492bb0 349static inline u64 btrfs_header_blocknr(struct btrfs_header *h)
7518a238 350{
bb492bb0 351 return le64_to_cpu(h->blocknr);
7518a238
CM
352}
353
bb492bb0 354static inline void btrfs_set_header_blocknr(struct btrfs_header *h, u64 blocknr)
7518a238 355{
bb492bb0 356 h->blocknr = cpu_to_le64(blocknr);
7518a238
CM
357}
358
bb492bb0 359static inline u64 btrfs_header_parentid(struct btrfs_header *h)
7518a238 360{
bb492bb0 361 return le64_to_cpu(h->parentid);
7518a238
CM
362}
363
bb492bb0
CM
364static inline void btrfs_set_header_parentid(struct btrfs_header *h,
365 u64 parentid)
7518a238 366{
bb492bb0 367 h->parentid = cpu_to_le64(parentid);
7518a238
CM
368}
369
bb492bb0 370static inline u16 btrfs_header_nritems(struct btrfs_header *h)
7518a238 371{
bb492bb0 372 return le16_to_cpu(h->nritems);
7518a238
CM
373}
374
bb492bb0 375static inline void btrfs_set_header_nritems(struct btrfs_header *h, u16 val)
7518a238 376{
bb492bb0 377 h->nritems = cpu_to_le16(val);
7518a238
CM
378}
379
bb492bb0 380static inline u16 btrfs_header_flags(struct btrfs_header *h)
7518a238 381{
bb492bb0 382 return le16_to_cpu(h->flags);
7518a238
CM
383}
384
bb492bb0 385static inline void btrfs_set_header_flags(struct btrfs_header *h, u16 val)
7518a238 386{
bb492bb0 387 h->flags = cpu_to_le16(val);
7518a238
CM
388}
389
bb492bb0 390static inline int btrfs_header_level(struct btrfs_header *h)
7518a238 391{
234b63a0 392 return btrfs_header_flags(h) & (BTRFS_MAX_LEVEL - 1);
7518a238
CM
393}
394
bb492bb0 395static inline void btrfs_set_header_level(struct btrfs_header *h, int level)
7518a238 396{
bb492bb0 397 u16 flags;
234b63a0
CM
398 BUG_ON(level > BTRFS_MAX_LEVEL);
399 flags = btrfs_header_flags(h) & ~(BTRFS_MAX_LEVEL - 1);
7518a238
CM
400 btrfs_set_header_flags(h, flags | level);
401}
402
234b63a0 403static inline int btrfs_is_leaf(struct btrfs_node *n)
7518a238
CM
404{
405 return (btrfs_header_level(&n->header) == 0);
406}
407
3768f368
CM
408static inline u64 btrfs_root_blocknr(struct btrfs_root_item *item)
409{
410 return le64_to_cpu(item->blocknr);
411}
412
413static inline void btrfs_set_root_blocknr(struct btrfs_root_item *item, u64 val)
414{
415 item->blocknr = cpu_to_le64(val);
416}
417
418static inline u32 btrfs_root_refs(struct btrfs_root_item *item)
419{
420 return le32_to_cpu(item->refs);
421}
422
423static inline void btrfs_set_root_refs(struct btrfs_root_item *item, u32 val)
424{
425 item->refs = cpu_to_le32(val);
426}
427
428static inline u64 btrfs_super_blocknr(struct btrfs_super_block *s)
429{
430 return le64_to_cpu(s->blocknr);
431}
432
433static inline void btrfs_set_super_blocknr(struct btrfs_super_block *s, u64 val)
434{
435 s->blocknr = cpu_to_le64(val);
436}
437
438static inline u64 btrfs_super_root(struct btrfs_super_block *s)
439{
440 return le64_to_cpu(s->root);
441}
442
443static inline void btrfs_set_super_root(struct btrfs_super_block *s, u64 val)
444{
445 s->root = cpu_to_le64(val);
446}
447
448static inline u64 btrfs_super_total_blocks(struct btrfs_super_block *s)
449{
450 return le64_to_cpu(s->total_blocks);
451}
452
453static inline void btrfs_set_super_total_blocks(struct btrfs_super_block *s,
454 u64 val)
455{
456 s->total_blocks = cpu_to_le64(val);
457}
458
459static inline u64 btrfs_super_blocks_used(struct btrfs_super_block *s)
460{
461 return le64_to_cpu(s->blocks_used);
462}
463
464static inline void btrfs_set_super_blocks_used(struct btrfs_super_block *s,
465 u64 val)
466{
467 s->blocks_used = cpu_to_le64(val);
468}
469
123abc88 470static inline u32 btrfs_super_blocksize(struct btrfs_super_block *s)
3768f368 471{
123abc88 472 return le32_to_cpu(s->blocksize);
3768f368
CM
473}
474
475static inline void btrfs_set_super_blocksize(struct btrfs_super_block *s,
123abc88
CM
476 u32 val)
477{
478 s->blocksize = cpu_to_le32(val);
479}
480
481static inline u8 *btrfs_leaf_data(struct btrfs_leaf *l)
3768f368 482{
123abc88 483 return (u8 *)l->items;
3768f368 484}
4beb1b8b
CM
485/* helper function to cast into the data area of the leaf. */
486#define btrfs_item_ptr(leaf, slot, type) \
123abc88
CM
487 ((type *)(btrfs_leaf_data(leaf) + \
488 btrfs_item_offset((leaf)->items + (slot))))
4beb1b8b 489
234b63a0
CM
490struct btrfs_buffer *btrfs_alloc_free_block(struct btrfs_root *root);
491int btrfs_inc_ref(struct btrfs_root *root, struct btrfs_buffer *buf);
492int btrfs_free_extent(struct btrfs_root *root, u64 blocknr, u64 num_blocks);
493int btrfs_search_slot(struct btrfs_root *root, struct btrfs_key *key,
494 struct btrfs_path *p, int ins_len, int cow);
495void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p);
496void btrfs_init_path(struct btrfs_path *p);
497int btrfs_del_item(struct btrfs_root *root, struct btrfs_path *path);
498int btrfs_insert_item(struct btrfs_root *root, struct btrfs_key *key,
62e2749e
CM
499 void *data, u32 data_size);
500int btrfs_insert_empty_item(struct btrfs_root *root, struct btrfs_path *path,
501 struct btrfs_key *cpu_key, u32 data_size);
234b63a0 502int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path);
123abc88 503int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf);
234b63a0
CM
504int btrfs_drop_snapshot(struct btrfs_root *root, struct btrfs_buffer *snap);
505int btrfs_finish_extent_commit(struct btrfs_root *root);
3768f368
CM
506int btrfs_del_root(struct btrfs_root *root, struct btrfs_key *key);
507int btrfs_insert_root(struct btrfs_root *root, struct btrfs_key *key,
508 struct btrfs_root_item *item);
509int btrfs_update_root(struct btrfs_root *root, struct btrfs_key *key,
510 struct btrfs_root_item *item);
511int btrfs_find_last_root(struct btrfs_root *root, u64 objectid,
512 struct btrfs_root_item *item, struct btrfs_key *key);
1d4f6404
CM
513int btrfs_insert_dir_item(struct btrfs_root *root, char *name, int name_len,
514 u64 dir, u64 objectid, u8 type);
515int btrfs_lookup_dir_item(struct btrfs_root *root, struct btrfs_path *path,
516 u64 dir, char *name, int name_len, int mod);
517int btrfs_match_dir_item_name(struct btrfs_root *root, struct btrfs_path *path,
518 char *name, int name_len);
eb60ceac 519#endif