Create macros to generation set/get funcs for on disk structures
[platform/upstream/btrfs-progs.git] / ctree.h
1 /*
2  * Copyright (C) 2007 Oracle.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License v2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public
14  * License along with this program; if not, write to the
15  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16  * Boston, MA 021110-1307, USA.
17  */
18
19 #ifndef __BTRFS__
20 #define __BTRFS__
21
22 #include "list.h"
23 #include "kerncompat.h"
24 #include "radix-tree.h"
25 #include "extent-cache.h"
26 struct btrfs_root;
27 struct btrfs_trans_handle;
28 #define BTRFS_MAGIC "_B2RfS_M"
29
30 #define BTRFS_ROOT_TREE_OBJECTID 1ULL
31 #define BTRFS_EXTENT_TREE_OBJECTID 2ULL
32 #define BTRFS_FS_TREE_OBJECTID 3ULL
33 #define BTRFS_ROOT_TREE_DIR_OBJECTID 4ULL
34 #define BTRFS_FIRST_FREE_OBJECTID 5ULL
35
36 /*
37  * we can actually store much bigger names, but lets not confuse the rest
38  * of linux
39  */
40 #define BTRFS_NAME_LEN 255
41
42 /* 32 bytes in various csum fields */
43 #define BTRFS_CSUM_SIZE 32
44 /* four bytes for CRC32 */
45 #define BTRFS_CRC32_SIZE 4
46
47 #define BTRFS_FT_UNKNOWN        0
48 #define BTRFS_FT_REG_FILE       1
49 #define BTRFS_FT_DIR            2
50 #define BTRFS_FT_CHRDEV         3
51 #define BTRFS_FT_BLKDEV         4
52 #define BTRFS_FT_FIFO           5
53 #define BTRFS_FT_SOCK           6
54 #define BTRFS_FT_SYMLINK        7
55 #define BTRFS_FT_XATTR          8
56 #define BTRFS_FT_MAX            9
57
58 /*
59  * the key defines the order in the tree, and so it also defines (optimal)
60  * block layout.  objectid corresonds to the inode number.  The flags
61  * tells us things about the object, and is a kind of stream selector.
62  * so for a given inode, keys with flags of 1 might refer to the inode
63  * data, flags of 2 may point to file data in the btree and flags == 3
64  * may point to extents.
65  *
66  * offset is the starting byte offset for this key in the stream.
67  *
68  * btrfs_disk_key is in disk byte order.  struct btrfs_key is always
69  * in cpu native order.  Otherwise they are identical and their sizes
70  * should be the same (ie both packed)
71  */
72 struct btrfs_disk_key {
73         __le64 objectid;
74         u8 type;
75         __le64 offset;
76 } __attribute__ ((__packed__));
77
78 struct btrfs_key {
79         u64 objectid;
80         u8 type;
81         u64 offset;
82 } __attribute__ ((__packed__));
83
84 /*
85  * every tree block (leaf or node) starts with this header.
86  */
87 struct btrfs_header {
88         u8 csum[BTRFS_CSUM_SIZE];
89         u8 fsid[16]; /* FS specific uuid */
90         __le64 bytenr; /* which block this node is supposed to live in */
91         __le64 generation;
92         __le64 owner;
93         __le32 nritems;
94         __le16 flags;
95         u8 level;
96 } __attribute__ ((__packed__));
97
98 #define BTRFS_MAX_LEVEL 8
99 #define BTRFS_NODEPTRS_PER_BLOCK(r) (((r)->nodesize - \
100                                 sizeof(struct btrfs_header)) / \
101                                sizeof(struct btrfs_key_ptr))
102 #define __BTRFS_LEAF_DATA_SIZE(bs) ((bs) - sizeof(struct btrfs_header))
103 #define BTRFS_LEAF_DATA_SIZE(r) (__BTRFS_LEAF_DATA_SIZE(r->leafsize))
104 #define BTRFS_MAX_INLINE_DATA_SIZE(r) (BTRFS_LEAF_DATA_SIZE(r) - \
105                                         sizeof(struct btrfs_item) - \
106                                         sizeof(struct btrfs_file_extent_item))
107 struct btrfs_buffer;
108 /*
109  * the super block basically lists the main trees of the FS
110  * it currently lacks any block count etc etc
111  */
112 struct btrfs_super_block {
113         u8 csum[BTRFS_CSUM_SIZE];
114         /* the first 3 fields must match struct btrfs_header */
115         u8 fsid[16];    /* FS specific uuid */
116         __le64 bytenr; /* this block number */
117         __le64 magic;
118         __le64 generation;
119         __le64 root;
120         __le64 total_bytes;
121         __le64 bytes_used;
122         __le64 root_dir_objectid;
123         __le32 sectorsize;
124         __le32 nodesize;
125         __le32 leafsize;
126         __le32 stripesize;
127         u8 root_level;
128 } __attribute__ ((__packed__));
129
130 /*
131  * A leaf is full of items. offset and size tell us where to find
132  * the item in the leaf (relative to the start of the data area)
133  */
134 struct btrfs_item {
135         struct btrfs_disk_key key;
136         __le32 offset;
137         __le32 size;
138 } __attribute__ ((__packed__));
139
140 /*
141  * leaves have an item area and a data area:
142  * [item0, item1....itemN] [free space] [dataN...data1, data0]
143  *
144  * The data is separate from the items to get the keys closer together
145  * during searches.
146  */
147 struct btrfs_leaf {
148         struct btrfs_header header;
149         struct btrfs_item items[];
150 } __attribute__ ((__packed__));
151
152 /*
153  * all non-leaf blocks are nodes, they hold only keys and pointers to
154  * other blocks
155  */
156 struct btrfs_key_ptr {
157         struct btrfs_disk_key key;
158         __le64 blockptr;
159         __le64 generation;
160 } __attribute__ ((__packed__));
161
162 struct btrfs_node {
163         struct btrfs_header header;
164         struct btrfs_key_ptr ptrs[];
165 } __attribute__ ((__packed__));
166
167 /*
168  * btrfs_paths remember the path taken from the root down to the leaf.
169  * level 0 is always the leaf, and nodes[1...BTRFS_MAX_LEVEL] will point
170  * to any other levels that are present.
171  *
172  * The slots array records the index of the item or block pointer
173  * used while walking the tree.
174  */
175 struct btrfs_path {
176         struct btrfs_buffer *nodes[BTRFS_MAX_LEVEL];
177         int slots[BTRFS_MAX_LEVEL];
178 };
179
180 /*
181  * items in the extent btree are used to record the objectid of the
182  * owner of the block and the number of references
183  */
184 struct btrfs_extent_item {
185         __le32 refs;
186 } __attribute__ ((__packed__));
187
188 struct btrfs_extent_ref {
189         __le64 root;
190         __le64 generation;
191         __le64 objectid;
192         __le64 offset;
193 } __attribute__ ((__packed__));
194
195 struct btrfs_inode_ref {
196         __le16 name_len;
197         /* name goes here */
198 } __attribute__ ((__packed__));
199
200 struct btrfs_inode_timespec {
201         __le64 sec;
202         __le32 nsec;
203 } __attribute__ ((__packed__));
204
205 /*
206  * there is no padding here on purpose.  If you want to extent the inode,
207  * make a new item type
208  */
209 struct btrfs_inode_item {
210         __le64 generation;
211         __le64 size;
212         __le64 nblocks;
213         __le64 block_group;
214         __le32 nlink;
215         __le32 uid;
216         __le32 gid;
217         __le32 mode;
218         __le32 rdev;
219         __le16 flags;
220         __le16 compat_flags;
221         struct btrfs_inode_timespec atime;
222         struct btrfs_inode_timespec ctime;
223         struct btrfs_inode_timespec mtime;
224         struct btrfs_inode_timespec otime;
225 } __attribute__ ((__packed__));
226
227 /* inline data is just a blob of bytes */
228 struct btrfs_inline_data_item {
229         u8 data;
230 } __attribute__ ((__packed__));
231
232 struct btrfs_dir_item {
233         struct btrfs_disk_key location;
234         __le16 data_len;
235         __le16 name_len;
236         u8 type;
237 } __attribute__ ((__packed__));
238
239 struct btrfs_root_item {
240         struct btrfs_inode_item inode;
241         __le64 root_dirid;
242         __le64 bytenr;
243         __le64 byte_limit;
244         __le64 bytes_used;
245         __le32 flags;
246         __le32 refs;
247         struct btrfs_disk_key drop_progress;
248         u8 drop_level;
249         u8 level;
250 } __attribute__ ((__packed__));
251
252 #define BTRFS_FILE_EXTENT_REG 0
253 #define BTRFS_FILE_EXTENT_INLINE 1
254
255 struct btrfs_file_extent_item {
256         __le64 generation;
257         u8 type;
258         /*
259          * disk space consumed by the extent, checksum blocks are included
260          * in these numbers
261          */
262         __le64 disk_bytenr;
263         __le64 disk_num_bytes;
264         /*
265          * the logical offset in file blocks (no csums)
266          * this extent record is for.  This allows a file extent to point
267          * into the middle of an existing extent on disk, sharing it
268          * between two snapshots (useful if some bytes in the middle of the
269          * extent have changed
270          */
271         __le64 offset;
272         /*
273          * the logical number of file blocks (no csums included)
274          */
275         __le64 num_bytes;
276 } __attribute__ ((__packed__));
277
278 struct btrfs_csum_item {
279         u8 csum[BTRFS_CSUM_SIZE];
280 } __attribute__ ((__packed__));
281
282 /* tag for the radix tree of block groups in ram */
283 #define BTRFS_BLOCK_GROUP_DIRTY 0
284 #define BTRFS_BLOCK_GROUP_SIZE (256 * 1024 * 1024)
285
286
287 #define BTRFS_BLOCK_GROUP_DATA 1
288 struct btrfs_block_group_item {
289         __le64 used;
290         u8 flags;
291 } __attribute__ ((__packed__));
292
293 struct btrfs_block_group_cache {
294         struct cache_extent cache;
295         struct btrfs_key key;
296         struct btrfs_block_group_item item;
297         int dirty;
298 };
299
300 struct btrfs_fs_info {
301         struct btrfs_root *fs_root;
302         struct btrfs_root *extent_root;
303         struct btrfs_root *tree_root;
304         struct btrfs_key last_insert;
305         struct cache_tree extent_cache;
306         struct cache_tree block_group_cache;
307         struct cache_tree pending_tree;
308         struct cache_tree pinned_tree;
309         struct cache_tree del_pending;
310         struct list_head trans;
311         struct list_head cache;
312         u64 last_inode_alloc;
313         u64 last_inode_alloc_dirid;
314         u64 generation;
315         int cache_size;
316         int fp;
317         struct btrfs_trans_handle *running_transaction;
318         struct btrfs_super_block *disk_super;
319 };
320
321 /*
322  * in ram representation of the tree.  extent_root is used for all allocations
323  * and for the extent tree extent_root root.
324  */
325 struct btrfs_root {
326         struct btrfs_buffer *node;
327         struct btrfs_buffer *commit_root;
328         struct btrfs_root_item root_item;
329         struct btrfs_key root_key;
330         struct btrfs_fs_info *fs_info;
331
332         /* data allocations are done in sectorsize units */
333         u32 sectorsize;
334
335         /* node allocations are done in nodesize units */
336         u32 nodesize;
337
338         /* leaf allocations are done in leafsize units */
339         u32 leafsize;
340
341         /* leaf allocations are done in leafsize units */
342         u32 stripesize;
343
344         int ref_cows;
345         u32 type;
346 };
347
348 /* the lower bits in the key flags defines the item type */
349 #define BTRFS_KEY_TYPE_MAX      256
350 #define BTRFS_KEY_TYPE_SHIFT    24
351 #define BTRFS_KEY_TYPE_MASK     (((u32)BTRFS_KEY_TYPE_MAX - 1) << \
352                                   BTRFS_KEY_TYPE_SHIFT)
353
354 /*
355  * inode items have the data typically returned from stat and store other
356  * info about object characteristics.  There is one for every file and dir in
357  * the FS
358  */
359 #define BTRFS_INODE_ITEM_KEY            1
360 #define BTRFS_INODE_REF_KEY             2
361 #define BTRFS_XATTR_ITEM_KEY            8
362
363 /* reserve 3-15 close to the inode for later flexibility */
364
365 /*
366  * dir items are the name -> inode pointers in a directory.  There is one
367  * for every name in a directory.
368  */
369 #define BTRFS_DIR_ITEM_KEY      16
370 #define BTRFS_DIR_INDEX_KEY     17
371 /*
372  * extent data is for file data
373  */
374 #define BTRFS_EXTENT_DATA_KEY   18
375 /*
376  * csum items have the checksums for data in the extents
377  */
378 #define BTRFS_CSUM_ITEM_KEY     19
379
380 /* reserve 20-31 for other file stuff */
381
382 /*
383  * root items point to tree roots.  There are typically in the root
384  * tree used by the super block to find all the other trees
385  */
386 #define BTRFS_ROOT_ITEM_KEY     32
387 /*
388  * extent items are in the extent map tree.  These record which blocks
389  * are used, and how many references there are to each block
390  */
391 #define BTRFS_EXTENT_ITEM_KEY   33
392 #define BTRFS_EXTENT_REF_KEY    34
393
394 /*
395  * block groups give us hints into the extent allocation trees.  Which
396  * blocks are free etc etc
397  */
398 #define BTRFS_BLOCK_GROUP_ITEM_KEY 50
399
400 /*
401  * string items are for debugging.  They just store a short string of
402  * data in the FS
403  */
404 #define BTRFS_STRING_ITEM_KEY   253
405
406 #define BTRFS_SETGET_STACK_FUNCS(name, type, member, bits)              \
407 static inline u##bits btrfs_##name(type *s)                             \
408 {                                                                       \
409         return le##bits##_to_cpu(s->member);                            \
410 }                                                                       \
411 static inline void btrfs_set_##name(type *s, u##bits val)               \
412 {                                                                       \
413         s->member = cpu_to_le##bits(val);                               \
414 }
415
416 BTRFS_SETGET_STACK_FUNCS(block_group_used, struct btrfs_block_group_item,
417                          used, 64);
418
419 BTRFS_SETGET_STACK_FUNCS(inode_generation, struct btrfs_inode_item,
420                          generation, 64);
421
422 BTRFS_SETGET_STACK_FUNCS(inode_size, struct btrfs_inode_item, size, 64);
423 BTRFS_SETGET_STACK_FUNCS(inode_nblocks, struct btrfs_inode_item, nblocks, 64);
424 BTRFS_SETGET_STACK_FUNCS(inode_block_group, struct btrfs_inode_item,
425                          block_group, 64);
426 BTRFS_SETGET_STACK_FUNCS(inode_nlink, struct btrfs_inode_item, nlink, 32);
427 BTRFS_SETGET_STACK_FUNCS(inode_uid, struct btrfs_inode_item, uid, 32);
428 BTRFS_SETGET_STACK_FUNCS(inode_gid, struct btrfs_inode_item, gid, 32);
429 BTRFS_SETGET_STACK_FUNCS(inode_mode, struct btrfs_inode_item, mode, 32);
430 BTRFS_SETGET_STACK_FUNCS(inode_rdev, struct btrfs_inode_item, rdev, 32);
431 BTRFS_SETGET_STACK_FUNCS(inode_flags, struct btrfs_inode_item, flags, 16);
432 BTRFS_SETGET_STACK_FUNCS(inode_compat_flags, struct btrfs_inode_item,
433                          compat_flags, 16);
434
435 BTRFS_SETGET_STACK_FUNCS(timpsec_sec, struct btrfs_inode_timespec, sec, 64);
436 BTRFS_SETGET_STACK_FUNCS(timpsec_nsec, struct btrfs_inode_timespec, nsec, 32);
437 BTRFS_SETGET_STACK_FUNCS(extent_refs, struct btrfs_extent_item, refs, 32);
438
439 BTRFS_SETGET_STACK_FUNCS(inode_ref_name_len, struct btrfs_inode_ref,
440                          name_len, 16);
441
442
443 BTRFS_SETGET_STACK_FUNCS(ref_root, struct btrfs_extent_ref, root, 64);
444 BTRFS_SETGET_STACK_FUNCS(ref_generation, struct btrfs_extent_ref,
445                          generation, 64);
446 BTRFS_SETGET_STACK_FUNCS(ref_objectid, struct btrfs_extent_ref, objectid, 64);
447 BTRFS_SETGET_STACK_FUNCS(ref_offset, struct btrfs_extent_ref, offset, 64);
448
449 static inline u64 btrfs_node_blockptr(struct btrfs_node *n, int nr)
450 {
451         return le64_to_cpu(n->ptrs[nr].blockptr);
452 }
453
454 static inline void btrfs_set_node_blockptr(struct btrfs_node *n, int nr,
455                                            u64 val)
456 {
457         n->ptrs[nr].blockptr = cpu_to_le64(val);
458 }
459
460 static inline u64 btrfs_node_ptr_generation(struct btrfs_node *n, int nr)
461 {
462         return le64_to_cpu(n->ptrs[nr].generation);
463 }
464
465 static inline void btrfs_set_node_ptr_generation(struct btrfs_node *n, int nr,
466                                                  u64 val)
467 {
468         n->ptrs[nr].generation = cpu_to_le64(val);
469 }
470
471 BTRFS_SETGET_STACK_FUNCS(item_offset, struct btrfs_item, offset, 32);
472
473 static inline u32 btrfs_item_end(struct btrfs_item *item)
474 {
475         return le32_to_cpu(item->offset) + le32_to_cpu(item->size);
476 }
477
478 BTRFS_SETGET_STACK_FUNCS(item_size, struct btrfs_item, size, 32);
479 BTRFS_SETGET_STACK_FUNCS(dir_type, struct btrfs_dir_item, type, 8);
480 BTRFS_SETGET_STACK_FUNCS(dir_name_len, struct btrfs_dir_item, name_len, 16);
481 BTRFS_SETGET_STACK_FUNCS(dir_data_len, struct btrfs_dir_item, data_len, 16);
482
483 static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu,
484                                          struct btrfs_disk_key *disk)
485 {
486         cpu->offset = le64_to_cpu(disk->offset);
487         cpu->type = le32_to_cpu(disk->type);
488         cpu->objectid = le64_to_cpu(disk->objectid);
489 }
490
491 static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk,
492                                          struct btrfs_key *cpu)
493 {
494         disk->offset = cpu_to_le64(cpu->offset);
495         disk->type = cpu_to_le32(cpu->type);
496         disk->objectid = cpu_to_le64(cpu->objectid);
497 }
498
499 BTRFS_SETGET_STACK_FUNCS(disk_key_objectid, struct btrfs_disk_key,
500                          objectid, 64);
501 BTRFS_SETGET_STACK_FUNCS(disk_key_offset, struct btrfs_disk_key, offset, 64);
502 BTRFS_SETGET_STACK_FUNCS(disk_key_type, struct btrfs_disk_key, type, 8);
503
504 static inline u8 btrfs_key_type(struct btrfs_key *key)
505 {
506         return key->type;
507 }
508
509 static inline void btrfs_set_key_type(struct btrfs_key *key, u8 val)
510 {
511         key->type = val;
512 }
513
514 BTRFS_SETGET_STACK_FUNCS(header_bytenr, struct btrfs_header, bytenr, 64);
515 BTRFS_SETGET_STACK_FUNCS(header_generation, struct btrfs_header,
516                          generation, 64);
517 BTRFS_SETGET_STACK_FUNCS(header_owner, struct btrfs_header, owner, 64);
518 BTRFS_SETGET_STACK_FUNCS(header_nritems, struct btrfs_header, nritems, 32);
519 BTRFS_SETGET_STACK_FUNCS(header_flags, struct btrfs_header, flags, 16);
520
521 static inline int btrfs_header_level(struct btrfs_header *h)
522 {
523         return h->level;
524 }
525
526 static inline void btrfs_set_header_level(struct btrfs_header *h, int level)
527 {
528         BUG_ON(level > BTRFS_MAX_LEVEL);
529         h->level = level;
530 }
531
532 static inline int btrfs_is_leaf(struct btrfs_node *n)
533 {
534         return (btrfs_header_level(&n->header) == 0);
535 }
536
537 BTRFS_SETGET_STACK_FUNCS(root_bytenr, struct btrfs_root_item, bytenr, 64);
538 BTRFS_SETGET_STACK_FUNCS(root_byte_limit, struct btrfs_root_item,
539                          byte_limit, 64);
540 BTRFS_SETGET_STACK_FUNCS(root_level, struct btrfs_root_item, level, 8);
541 BTRFS_SETGET_STACK_FUNCS(root_dirid, struct btrfs_root_item, root_dirid, 64);
542 BTRFS_SETGET_STACK_FUNCS(root_refs, struct btrfs_root_item, refs, 32);
543 BTRFS_SETGET_STACK_FUNCS(root_flags, struct btrfs_root_item, flags, 32);
544 BTRFS_SETGET_STACK_FUNCS(root_bytes_used, struct btrfs_root_item,
545                          bytes_used, 64);
546 BTRFS_SETGET_STACK_FUNCS(super_bytenr, struct btrfs_super_block, bytenr, 64);
547 BTRFS_SETGET_STACK_FUNCS(super_generation, struct btrfs_super_block,
548                          generation, 64);
549 BTRFS_SETGET_STACK_FUNCS(super_root_level, struct btrfs_super_block,
550                          root_level, 8);
551 BTRFS_SETGET_STACK_FUNCS(super_root, struct btrfs_super_block, root, 64);
552 BTRFS_SETGET_STACK_FUNCS(super_total_bytes, struct btrfs_super_block,
553                          total_bytes, 64);
554 BTRFS_SETGET_STACK_FUNCS(super_bytes_used, struct btrfs_super_block,
555                          bytes_used, 64);
556 BTRFS_SETGET_STACK_FUNCS(super_sectorsize, struct btrfs_super_block,
557                          sectorsize, 32);
558 BTRFS_SETGET_STACK_FUNCS(super_nodesize, struct btrfs_super_block,
559                          nodesize, 32);
560 BTRFS_SETGET_STACK_FUNCS(super_leafsize, struct btrfs_super_block,
561                          leafsize, 32);
562 BTRFS_SETGET_STACK_FUNCS(super_stripesize, struct btrfs_super_block,
563                          stripesize, 32);
564 BTRFS_SETGET_STACK_FUNCS(super_root_dir, struct btrfs_super_block,
565                          root_dir_objectid, 64);
566
567 static inline u8 *btrfs_leaf_data(struct btrfs_leaf *l)
568 {
569         return (u8 *)l->items;
570 }
571
572 BTRFS_SETGET_STACK_FUNCS(file_extent_type, struct btrfs_file_extent_item,
573                          type, 8);
574
575 static inline char *btrfs_file_extent_inline_start(struct
576                                                    btrfs_file_extent_item *e)
577 {
578         return (char *)(&e->disk_bytenr);
579 }
580
581 static inline u32 btrfs_file_extent_calc_inline_size(u32 datasize)
582 {
583         return (unsigned long)(&((struct
584                   btrfs_file_extent_item *)NULL)->disk_bytenr) + datasize;
585 }
586
587 static inline u32 btrfs_file_extent_inline_len(struct btrfs_item *e)
588 {
589         struct btrfs_file_extent_item *fe = NULL;
590         return btrfs_item_size(e) - (unsigned long)(&fe->disk_bytenr);
591 }
592
593 BTRFS_SETGET_STACK_FUNCS(file_extent_disk_bytenr, struct btrfs_file_extent_item,
594                          disk_bytenr, 64);
595 BTRFS_SETGET_STACK_FUNCS(file_extent_generation, struct btrfs_file_extent_item,
596                          generation, 64);
597 BTRFS_SETGET_STACK_FUNCS(file_extent_disk_num_bytes,
598                          struct btrfs_file_extent_item, disk_num_bytes, 64);
599 BTRFS_SETGET_STACK_FUNCS(file_extent_offset, struct btrfs_file_extent_item,
600                          offset, 64);
601 BTRFS_SETGET_STACK_FUNCS(file_extent_num_bytes, struct btrfs_file_extent_item,
602                          num_bytes, 64);
603
604 /* helper function to cast into the data area of the leaf. */
605 #define btrfs_item_ptr(leaf, slot, type) \
606         ((type *)(btrfs_leaf_data(leaf) + \
607         btrfs_item_offset((leaf)->items + (slot))))
608 #define btrfs_item_ptr_offset(leaf, slot) \
609         ((unsigned long)(btrfs_leaf_data(leaf) + \
610         btrfs_item_offset_nr(leaf, slot)))
611
612 static inline u32 btrfs_level_size(struct btrfs_root *root, int level)
613 {
614         if (level == 0)
615                 return root->leafsize;
616         return root->nodesize;
617 }
618 int btrfs_comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2);
619 struct btrfs_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
620                                             struct btrfs_root *root,
621                                             u32 blocksize);
622 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
623                   struct btrfs_buffer *buf);
624 int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
625                        struct btrfs_root *root, u64 owner_objectid);
626 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
627                       *root, u64 bytenr, u64 num_bytes,
628                       u64 root_objectid, u64 root_generation,
629                       u64 owner, u64 owner_offset, int pin);
630 int btrfs_cow_block(struct btrfs_trans_handle *trans,
631                     struct btrfs_root *root, struct btrfs_buffer *buf,
632                     struct btrfs_buffer *parent, int parent_slot,
633                     struct btrfs_buffer **cow_ret);
634 int btrfs_extend_item(struct btrfs_trans_handle *trans, struct btrfs_root
635                       *root, struct btrfs_path *path, u32 data_size);
636 int btrfs_truncate_item(struct btrfs_trans_handle *trans,
637                         struct btrfs_root *root,
638                         struct btrfs_path *path,
639                         u32 new_size, int from_end);
640 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
641                       *root, struct btrfs_key *key, struct btrfs_path *p, int
642                       ins_len, int cow);
643 void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p);
644 void btrfs_init_path(struct btrfs_path *p);
645 int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
646                    struct btrfs_path *path);
647 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
648                       *root, struct btrfs_key *key, void *data, u32 data_size);
649 int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root
650                             *root, struct btrfs_path *path, struct btrfs_key
651                             *cpu_key, u32 data_size);
652 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path);
653 int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf);
654 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
655                         *root, struct btrfs_buffer *snap);
656 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
657                                btrfs_root *root);
658 int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
659                    struct btrfs_key *key);
660 int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
661                       *root, struct btrfs_key *key, struct btrfs_root_item
662                       *item);
663 int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
664                       *root, struct btrfs_key *key, struct btrfs_root_item
665                       *item);
666 int btrfs_find_last_root(struct btrfs_root *root, u64 objectid, struct
667                          btrfs_root_item *item, struct btrfs_key *key);
668 int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
669                           *root, char *name, int name_len, u64 dir,
670                           struct btrfs_key *location, u8 type);
671 struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
672                           struct btrfs_root *root, struct btrfs_path *path,
673                           u64 dir, char *name, int name_len, int mod);
674 struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_root *root,
675                               struct btrfs_path *path,
676                               const char *name, int name_len);
677 int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans,
678                               struct btrfs_root *root,
679                               struct btrfs_path *path,
680                               struct btrfs_dir_item *di);
681 int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
682                              struct btrfs_root *fs_root,
683                              u64 dirid, u64 *objectid);
684 int btrfs_insert_inode(struct btrfs_trans_handle *trans, struct btrfs_root
685                        *root, u64 objectid, struct btrfs_inode_item
686                        *inode_item);
687 int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root
688                        *root, struct btrfs_path *path, u64 objectid, int mod);
689 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
690                                     struct btrfs_root *root);
691 int btrfs_free_block_groups(struct btrfs_fs_info *info);
692 int btrfs_read_block_groups(struct btrfs_root *root);
693 int btrfs_insert_block_group(struct btrfs_trans_handle *trans,
694                              struct btrfs_root *root,
695                              struct btrfs_key *key,
696                              struct btrfs_block_group_item *bi);
697 /* file-item.c */
698 int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
699                              struct btrfs_root *root,
700                              u64 objectid, u64 pos, u64 offset,
701                              u64 disk_num_bytes, u64 num_bytes);
702 int btrfs_insert_inline_file_extent(struct btrfs_trans_handle *trans,
703                                     struct btrfs_root *root, u64 objectid,
704                                     u64 offset, char *buffer, size_t size);
705 int btrfs_lookup_csum(struct btrfs_trans_handle *trans, struct btrfs_root
706                       *root, struct btrfs_path *path, u64 objectid,
707                       u64 offset, int cow, struct btrfs_csum_item **item_ret);
708 int btrfs_csum_file_block(struct btrfs_trans_handle *trans, struct btrfs_root
709                           *root, struct btrfs_inode_item *inode,
710                           u64 objectid, u64 offset, char *data, size_t len);
711 int btrfs_insert_inode_ref(struct btrfs_trans_handle *trans,
712                            struct btrfs_root *root,
713                            const char *name, int name_len,
714                            u64 inode_objectid, u64 ref_objectid);
715 #endif