groundwork for subvolume and snapshot roots
[platform/upstream/btrfs-progs.git] / ctree.h
1 #ifndef __BTRFS__
2 #define __BTRFS__
3
4 #include "list.h"
5 #include "kerncompat.h"
6
7 struct btrfs_trans_handle;
8
9 #define BTRFS_MAGIC "_BtRfS_M"
10
11 #define BTRFS_ROOT_TREE_OBJECTID 1ULL
12 #define BTRFS_EXTENT_TREE_OBJECTID 2ULL
13 #define BTRFS_INODE_MAP_OBJECTID 3ULL
14 #define BTRFS_FS_TREE_OBJECTID 4ULL
15 #define BTRFS_FIRST_FREE_OBJECTID 5ULL
16
17 /*
18  * we can actually store much bigger names, but lets not confuse the rest
19  * of linux
20  */
21 #define BTRFS_NAME_LEN 255
22
23 /* 32 bytes in various csum fields */
24 #define BTRFS_CSUM_SIZE 32
25
26 /*
27  * the key defines the order in the tree, and so it also defines (optimal)
28  * block layout.  objectid corresonds to the inode number.  The flags
29  * tells us things about the object, and is a kind of stream selector.
30  * so for a given inode, keys with flags of 1 might refer to the inode
31  * data, flags of 2 may point to file data in the btree and flags == 3
32  * may point to extents.
33  *
34  * offset is the starting byte offset for this key in the stream.
35  *
36  * btrfs_disk_key is in disk byte order.  struct btrfs_key is always
37  * in cpu native order.  Otherwise they are identical and their sizes
38  * should be the same (ie both packed)
39  */
40 struct btrfs_disk_key {
41         __le64 objectid;
42         __le64 offset;
43         __le32 flags;
44 } __attribute__ ((__packed__));
45
46 struct btrfs_key {
47         u64 objectid;
48         u64 offset;
49         u32 flags;
50 } __attribute__ ((__packed__));
51
52 /*
53  * every tree block (leaf or node) starts with this header.
54  */
55 struct btrfs_header {
56         u8 csum[BTRFS_CSUM_SIZE];
57         u8 fsid[16]; /* FS specific uuid */
58         __le64 blocknr; /* which block this node is supposed to live in */
59         __le64 generation;
60         __le64 parentid; /* objectid of the tree root */
61         __le32 ham;
62         __le16 nritems;
63         __le16 flags;
64         u8 level;
65 } __attribute__ ((__packed__));
66
67 #define BTRFS_MAX_LEVEL 8
68 #define BTRFS_NODEPTRS_PER_BLOCK(r) (((r)->blocksize - \
69                                 sizeof(struct btrfs_header)) / \
70                                (sizeof(struct btrfs_disk_key) + sizeof(u64)))
71 #define __BTRFS_LEAF_DATA_SIZE(bs) ((bs) - sizeof(struct btrfs_header))
72 #define BTRFS_LEAF_DATA_SIZE(r) (__BTRFS_LEAF_DATA_SIZE(r->blocksize))
73
74 struct btrfs_buffer;
75 /*
76  * the super block basically lists the main trees of the FS
77  * it currently lacks any block count etc etc
78  */
79 struct btrfs_super_block {
80         u8 csum[BTRFS_CSUM_SIZE];
81         /* the first 3 fields must match struct btrfs_header */
82         u8 fsid[16];    /* FS specific uuid */
83         __le64 blocknr; /* this block number */
84         __le64 magic;
85         __le32 blocksize;
86         __le64 generation;
87         __le64 root;
88         __le64 total_blocks;
89         __le64 blocks_used;
90         __le64 root_dir_objectid;
91 } __attribute__ ((__packed__));
92
93 /*
94  * A leaf is full of items. offset and size tell us where to find
95  * the item in the leaf (relative to the start of the data area)
96  */
97 struct btrfs_item {
98         struct btrfs_disk_key key;
99         __le32 offset;
100         __le16 size;
101 } __attribute__ ((__packed__));
102
103 /*
104  * leaves have an item area and a data area:
105  * [item0, item1....itemN] [free space] [dataN...data1, data0]
106  *
107  * The data is separate from the items to get the keys closer together
108  * during searches.
109  */
110 struct btrfs_leaf {
111         struct btrfs_header header;
112         struct btrfs_item items[];
113 } __attribute__ ((__packed__));
114
115 /*
116  * all non-leaf blocks are nodes, they hold only keys and pointers to
117  * other blocks
118  */
119 struct btrfs_key_ptr {
120         struct btrfs_disk_key key;
121         __le64 blockptr;
122 } __attribute__ ((__packed__));
123
124 struct btrfs_node {
125         struct btrfs_header header;
126         struct btrfs_key_ptr ptrs[];
127 } __attribute__ ((__packed__));
128
129 /*
130  * btrfs_paths remember the path taken from the root down to the leaf.
131  * level 0 is always the leaf, and nodes[1...BTRFS_MAX_LEVEL] will point
132  * to any other levels that are present.
133  *
134  * The slots array records the index of the item or block pointer
135  * used while walking the tree.
136  */
137 struct btrfs_path {
138         struct btrfs_buffer *nodes[BTRFS_MAX_LEVEL];
139         int slots[BTRFS_MAX_LEVEL];
140 };
141
142 /*
143  * items in the extent btree are used to record the objectid of the
144  * owner of the block and the number of references
145  */
146 struct btrfs_extent_item {
147         __le32 refs;
148         __le64 owner;
149 } __attribute__ ((__packed__));
150
151 struct btrfs_inode_timespec {
152         __le64 sec;
153         __le32 nsec;
154 } __attribute__ ((__packed__));
155
156 /*
157  * there is no padding here on purpose.  If you want to extent the inode,
158  * make a new item type
159  */
160 struct btrfs_inode_item {
161         __le64 generation;
162         __le64 size;
163         __le64 nblocks;
164         __le32 nlink;
165         __le32 uid;
166         __le32 gid;
167         __le32 mode;
168         __le32 rdev;
169         __le16 flags;
170         __le16 compat_flags;
171         struct btrfs_inode_timespec atime;
172         struct btrfs_inode_timespec ctime;
173         struct btrfs_inode_timespec mtime;
174         struct btrfs_inode_timespec otime;
175 } __attribute__ ((__packed__));
176
177 /* inline data is just a blob of bytes */
178 struct btrfs_inline_data_item {
179         u8 data;
180 } __attribute__ ((__packed__));
181
182 struct btrfs_dir_item {
183         struct btrfs_disk_key location;
184         __le16 flags;
185         __le16 name_len;
186         u8 type;
187 } __attribute__ ((__packed__));
188
189 struct btrfs_root_item {
190         struct btrfs_inode_item inode;
191         __le64 root_dirid;
192         __le64 blocknr;
193         __le32 flags;
194         __le64 block_limit;
195         __le64 blocks_used;
196         __le32 refs;
197 } __attribute__ ((__packed__));
198
199 struct btrfs_file_extent_item {
200         __le64 generation;
201         /*
202          * disk space consumed by the extent, checksum blocks are included
203          * in these numbers
204          */
205         __le64 disk_blocknr;
206         __le64 disk_num_blocks;
207         /*
208          * the logical offset in file blocks (no csums)
209          * this extent record is for.  This allows a file extent to point
210          * into the middle of an existing extent on disk, sharing it
211          * between two snapshots (useful if some bytes in the middle of the
212          * extent have changed
213          */
214         __le64 offset;
215         /*
216          * the logical number of file blocks (no csums included)
217          */
218         __le64 num_blocks;
219 } __attribute__ ((__packed__));
220
221 struct btrfs_csum_item {
222         u8 csum[BTRFS_CSUM_SIZE];
223 } __attribute__ ((__packed__));
224
225 struct btrfs_inode_map_item {
226         struct btrfs_disk_key key;
227 } __attribute__ ((__packed__));
228
229
230 struct btrfs_fs_info {
231         struct btrfs_root *fs_root;
232         struct btrfs_root *extent_root;
233         struct btrfs_root *tree_root;
234         struct btrfs_root *inode_root;
235         struct btrfs_key current_insert;
236         struct btrfs_key last_insert;
237         struct radix_tree_root cache_radix;
238         struct radix_tree_root pinned_radix;
239         struct list_head trans;
240         struct list_head cache;
241         u64 last_inode_alloc;
242         u64 last_inode_alloc_dirid;
243         u64 generation;
244         int cache_size;
245         int fp;
246         struct btrfs_trans_handle *running_transaction;
247         struct btrfs_super_block *disk_super;
248 };
249
250 /*
251  * in ram representation of the tree.  extent_root is used for all allocations
252  * and for the extent tree extent_root root.  current_insert is used
253  * only for the extent tree.
254  */
255 struct btrfs_root {
256         struct btrfs_buffer *node;
257         struct btrfs_buffer *commit_root;
258         struct btrfs_root_item root_item;
259         struct btrfs_key root_key;
260         struct btrfs_fs_info *fs_info;
261         u32 blocksize;
262         int ref_cows;
263         u32 type;
264 };
265
266 /* the lower bits in the key flags defines the item type */
267 #define BTRFS_KEY_TYPE_MAX      256
268 #define BTRFS_KEY_TYPE_MASK     (BTRFS_KEY_TYPE_MAX - 1)
269
270 #define BTRFS_KEY_OVERFLOW_MAX 128
271 #define BTRFS_KEY_OVERFLOW_SHIFT 8
272 #define BTRFS_KEY_OVERFLOW_MASK (0x7FULL << BTRFS_KEY_OVERFLOW_SHIFT)
273
274 /*
275  * inode items have the data typically returned from stat and store other
276  * info about object characteristics.  There is one for every file and dir in
277  * the FS
278  */
279 #define BTRFS_INODE_ITEM_KEY    1
280
281 /*
282  * dir items are the name -> inode pointers in a directory.  There is one
283  * for every name in a directory.
284  */
285 #define BTRFS_DIR_ITEM_KEY      2
286 #define BTRFS_DIR_INDEX_KEY     3
287 /*
288  * inline data is file data that fits in the btree.
289  */
290 #define BTRFS_INLINE_DATA_KEY   4
291 /*
292  * extent data is for data that can't fit in the btree.  It points to
293  * a (hopefully) huge chunk of disk
294  */
295 #define BTRFS_EXTENT_DATA_KEY   5
296 /*
297  * csum items have the checksums for data in the extents
298  */
299 #define BTRFS_CSUM_ITEM_KEY     6
300
301 /*
302  * root items point to tree roots.  There are typically in the root
303  * tree used by the super block to find all the other trees
304  */
305 #define BTRFS_ROOT_ITEM_KEY     7
306 /*
307  * extent items are in the extent map tree.  These record which blocks
308  * are used, and how many references there are to each block
309  */
310 #define BTRFS_EXTENT_ITEM_KEY   8
311
312 /*
313  * the inode map records which inode numbers are in use and where
314  * they actually live on disk
315  */
316 #define BTRFS_INODE_MAP_ITEM_KEY 9
317 /*
318  * string items are for debugging.  They just store a short string of
319  * data in the FS
320  */
321 #define BTRFS_STRING_ITEM_KEY   10
322
323 static inline u64 btrfs_inode_generation(struct btrfs_inode_item *i)
324 {
325         return le64_to_cpu(i->generation);
326 }
327
328 static inline void btrfs_set_inode_generation(struct btrfs_inode_item *i,
329                                               u64 val)
330 {
331         i->generation = cpu_to_le64(val);
332 }
333
334 static inline u64 btrfs_inode_size(struct btrfs_inode_item *i)
335 {
336         return le64_to_cpu(i->size);
337 }
338
339 static inline void btrfs_set_inode_size(struct btrfs_inode_item *i, u64 val)
340 {
341         i->size = cpu_to_le64(val);
342 }
343
344 static inline u64 btrfs_inode_nblocks(struct btrfs_inode_item *i)
345 {
346         return le64_to_cpu(i->nblocks);
347 }
348
349 static inline void btrfs_set_inode_nblocks(struct btrfs_inode_item *i, u64 val)
350 {
351         i->nblocks = cpu_to_le64(val);
352 }
353
354 static inline u32 btrfs_inode_nlink(struct btrfs_inode_item *i)
355 {
356         return le32_to_cpu(i->nlink);
357 }
358
359 static inline void btrfs_set_inode_nlink(struct btrfs_inode_item *i, u32 val)
360 {
361         i->nlink = cpu_to_le32(val);
362 }
363
364 static inline u32 btrfs_inode_uid(struct btrfs_inode_item *i)
365 {
366         return le32_to_cpu(i->uid);
367 }
368
369 static inline void btrfs_set_inode_uid(struct btrfs_inode_item *i, u32 val)
370 {
371         i->uid = cpu_to_le32(val);
372 }
373
374 static inline u32 btrfs_inode_gid(struct btrfs_inode_item *i)
375 {
376         return le32_to_cpu(i->gid);
377 }
378
379 static inline void btrfs_set_inode_gid(struct btrfs_inode_item *i, u32 val)
380 {
381         i->gid = cpu_to_le32(val);
382 }
383
384 static inline u32 btrfs_inode_mode(struct btrfs_inode_item *i)
385 {
386         return le32_to_cpu(i->mode);
387 }
388
389 static inline void btrfs_set_inode_mode(struct btrfs_inode_item *i, u32 val)
390 {
391         i->mode = cpu_to_le32(val);
392 }
393
394 static inline u32 btrfs_inode_rdev(struct btrfs_inode_item *i)
395 {
396         return le32_to_cpu(i->rdev);
397 }
398
399 static inline void btrfs_set_inode_rdev(struct btrfs_inode_item *i, u32 val)
400 {
401         i->rdev = cpu_to_le32(val);
402 }
403
404 static inline u16 btrfs_inode_flags(struct btrfs_inode_item *i)
405 {
406         return le16_to_cpu(i->flags);
407 }
408
409 static inline void btrfs_set_inode_flags(struct btrfs_inode_item *i, u16 val)
410 {
411         i->flags = cpu_to_le16(val);
412 }
413
414 static inline u16 btrfs_inode_compat_flags(struct btrfs_inode_item *i)
415 {
416         return le16_to_cpu(i->compat_flags);
417 }
418
419 static inline void btrfs_set_inode_compat_flags(struct btrfs_inode_item *i,
420                                                 u16 val)
421 {
422         i->compat_flags = cpu_to_le16(val);
423 }
424
425 static inline u64 btrfs_timespec_sec(struct btrfs_inode_timespec *ts)
426 {
427         return le64_to_cpu(ts->sec);
428 }
429
430 static inline void btrfs_set_timespec_sec(struct btrfs_inode_timespec *ts,
431                                           u64 val)
432 {
433         ts->sec = cpu_to_le64(val);
434 }
435
436 static inline u32 btrfs_timespec_nsec(struct btrfs_inode_timespec *ts)
437 {
438         return le32_to_cpu(ts->nsec);
439 }
440
441 static inline void btrfs_set_timespec_nsec(struct btrfs_inode_timespec *ts,
442                                           u32 val)
443 {
444         ts->nsec = cpu_to_le32(val);
445 }
446
447 static inline u64 btrfs_extent_owner(struct btrfs_extent_item *ei)
448 {
449         return le64_to_cpu(ei->owner);
450 }
451
452 static inline void btrfs_set_extent_owner(struct btrfs_extent_item *ei, u64 val)
453 {
454         ei->owner = cpu_to_le64(val);
455 }
456
457 static inline u32 btrfs_extent_refs(struct btrfs_extent_item *ei)
458 {
459         return le32_to_cpu(ei->refs);
460 }
461
462 static inline void btrfs_set_extent_refs(struct btrfs_extent_item *ei, u32 val)
463 {
464         ei->refs = cpu_to_le32(val);
465 }
466
467 static inline u64 btrfs_node_blockptr(struct btrfs_node *n, int nr)
468 {
469         return le64_to_cpu(n->ptrs[nr].blockptr);
470 }
471
472 static inline void btrfs_set_node_blockptr(struct btrfs_node *n, int nr,
473                                            u64 val)
474 {
475         n->ptrs[nr].blockptr = cpu_to_le64(val);
476 }
477
478 static inline u32 btrfs_item_offset(struct btrfs_item *item)
479 {
480         return le32_to_cpu(item->offset);
481 }
482
483 static inline void btrfs_set_item_offset(struct btrfs_item *item, u32 val)
484 {
485         item->offset = cpu_to_le32(val);
486 }
487
488 static inline u32 btrfs_item_end(struct btrfs_item *item)
489 {
490         return le32_to_cpu(item->offset) + le16_to_cpu(item->size);
491 }
492
493 static inline u16 btrfs_item_size(struct btrfs_item *item)
494 {
495         return le16_to_cpu(item->size);
496 }
497
498 static inline void btrfs_set_item_size(struct btrfs_item *item, u16 val)
499 {
500         item->size = cpu_to_le16(val);
501 }
502
503 static inline u16 btrfs_dir_flags(struct btrfs_dir_item *d)
504 {
505         return le16_to_cpu(d->flags);
506 }
507
508 static inline void btrfs_set_dir_flags(struct btrfs_dir_item *d, u16 val)
509 {
510         d->flags = cpu_to_le16(val);
511 }
512
513 static inline u8 btrfs_dir_type(struct btrfs_dir_item *d)
514 {
515         return d->type;
516 }
517
518 static inline void btrfs_set_dir_type(struct btrfs_dir_item *d, u8 val)
519 {
520         d->type = val;
521 }
522
523 static inline u16 btrfs_dir_name_len(struct btrfs_dir_item *d)
524 {
525         return le16_to_cpu(d->name_len);
526 }
527
528 static inline void btrfs_set_dir_name_len(struct btrfs_dir_item *d, u16 val)
529 {
530         d->name_len = cpu_to_le16(val);
531 }
532
533 static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu,
534                                          struct btrfs_disk_key *disk)
535 {
536         cpu->offset = le64_to_cpu(disk->offset);
537         cpu->flags = le32_to_cpu(disk->flags);
538         cpu->objectid = le64_to_cpu(disk->objectid);
539 }
540
541 static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk,
542                                          struct btrfs_key *cpu)
543 {
544         disk->offset = cpu_to_le64(cpu->offset);
545         disk->flags = cpu_to_le32(cpu->flags);
546         disk->objectid = cpu_to_le64(cpu->objectid);
547 }
548
549 static inline u64 btrfs_disk_key_objectid(struct btrfs_disk_key *disk)
550 {
551         return le64_to_cpu(disk->objectid);
552 }
553
554 static inline void btrfs_set_disk_key_objectid(struct btrfs_disk_key *disk,
555                                                u64 val)
556 {
557         disk->objectid = cpu_to_le64(val);
558 }
559
560 static inline u64 btrfs_disk_key_offset(struct btrfs_disk_key *disk)
561 {
562         return le64_to_cpu(disk->offset);
563 }
564
565 static inline void btrfs_set_disk_key_offset(struct btrfs_disk_key *disk,
566                                              u64 val)
567 {
568         disk->offset = cpu_to_le64(val);
569 }
570
571 static inline u32 btrfs_disk_key_flags(struct btrfs_disk_key *disk)
572 {
573         return le32_to_cpu(disk->flags);
574 }
575
576 static inline void btrfs_set_disk_key_flags(struct btrfs_disk_key *disk,
577                                             u32 val)
578 {
579         disk->flags = cpu_to_le32(val);
580 }
581
582 static inline u32 btrfs_key_overflow(struct btrfs_key *key)
583 {
584         u32 over = key->flags & BTRFS_KEY_OVERFLOW_MASK;
585         return over >> BTRFS_KEY_OVERFLOW_SHIFT;
586 }
587
588 static inline void btrfs_set_key_overflow(struct btrfs_key *key, u32 over)
589 {
590         BUG_ON(over >= BTRFS_KEY_OVERFLOW_MAX);
591         over = over << BTRFS_KEY_OVERFLOW_SHIFT;
592         key->flags = (key->flags & ~((u64)BTRFS_KEY_OVERFLOW_MASK)) | over;
593 }
594
595 static inline u32 btrfs_key_type(struct btrfs_key *key)
596 {
597         return key->flags & BTRFS_KEY_TYPE_MASK;
598 }
599
600 static inline u32 btrfs_disk_key_type(struct btrfs_disk_key *key)
601 {
602         return le32_to_cpu(key->flags) & BTRFS_KEY_TYPE_MASK;
603 }
604
605 static inline void btrfs_set_key_type(struct btrfs_key *key, u32 type)
606 {
607         BUG_ON(type >= BTRFS_KEY_TYPE_MAX);
608         key->flags = (key->flags & ~((u64)BTRFS_KEY_TYPE_MASK)) | type;
609 }
610
611 static inline void btrfs_set_disk_key_type(struct btrfs_disk_key *key, u32 type)
612 {
613         u32 flags = btrfs_disk_key_flags(key);
614         BUG_ON(type >= BTRFS_KEY_TYPE_MAX);
615         flags = (flags & ~((u64)BTRFS_KEY_TYPE_MASK)) | type;
616         btrfs_set_disk_key_flags(key, flags);
617 }
618
619 static inline u32 btrfs_disk_key_overflow(struct btrfs_disk_key *key)
620 {
621         u32 over = le32_to_cpu(key->flags) & BTRFS_KEY_OVERFLOW_MASK;
622         return over >> BTRFS_KEY_OVERFLOW_SHIFT;
623 }
624
625 static inline void btrfs_set_disK_key_overflow(struct btrfs_disk_key *key,
626                                                u32 over)
627 {
628         u32 flags = btrfs_disk_key_flags(key);
629         BUG_ON(over >= BTRFS_KEY_OVERFLOW_MAX);
630         over = over << BTRFS_KEY_OVERFLOW_SHIFT;
631         flags = (flags & ~((u64)BTRFS_KEY_OVERFLOW_MASK)) | over;
632         btrfs_set_disk_key_flags(key, flags);
633 }
634
635 static inline u64 btrfs_header_blocknr(struct btrfs_header *h)
636 {
637         return le64_to_cpu(h->blocknr);
638 }
639
640 static inline void btrfs_set_header_blocknr(struct btrfs_header *h, u64 blocknr)
641 {
642         h->blocknr = cpu_to_le64(blocknr);
643 }
644
645 static inline u64 btrfs_header_generation(struct btrfs_header *h)
646 {
647         return le64_to_cpu(h->generation);
648 }
649
650 static inline void btrfs_set_header_generation(struct btrfs_header *h,
651                                                u64 val)
652 {
653         h->generation = cpu_to_le64(val);
654 }
655
656 static inline u64 btrfs_header_parentid(struct btrfs_header *h)
657 {
658         return le64_to_cpu(h->parentid);
659 }
660
661 static inline void btrfs_set_header_parentid(struct btrfs_header *h,
662                                              u64 parentid)
663 {
664         h->parentid = cpu_to_le64(parentid);
665 }
666
667 static inline u16 btrfs_header_nritems(struct btrfs_header *h)
668 {
669         return le16_to_cpu(h->nritems);
670 }
671
672 static inline void btrfs_set_header_nritems(struct btrfs_header *h, u16 val)
673 {
674         h->nritems = cpu_to_le16(val);
675 }
676
677 static inline u16 btrfs_header_flags(struct btrfs_header *h)
678 {
679         return le16_to_cpu(h->flags);
680 }
681
682 static inline void btrfs_set_header_flags(struct btrfs_header *h, u16 val)
683 {
684         h->flags = cpu_to_le16(val);
685 }
686
687 static inline int btrfs_header_level(struct btrfs_header *h)
688 {
689         return h->level;
690 }
691
692 static inline void btrfs_set_header_level(struct btrfs_header *h, int level)
693 {
694         BUG_ON(level > BTRFS_MAX_LEVEL);
695         h->level = level;
696 }
697
698 static inline int btrfs_is_leaf(struct btrfs_node *n)
699 {
700         return (btrfs_header_level(&n->header) == 0);
701 }
702
703 static inline u64 btrfs_root_blocknr(struct btrfs_root_item *item)
704 {
705         return le64_to_cpu(item->blocknr);
706 }
707
708 static inline void btrfs_set_root_blocknr(struct btrfs_root_item *item, u64 val)
709 {
710         item->blocknr = cpu_to_le64(val);
711 }
712
713 static inline u64 btrfs_root_dirid(struct btrfs_root_item *item)
714 {
715         return le64_to_cpu(item->root_dirid);
716 }
717
718 static inline void btrfs_set_root_dirid(struct btrfs_root_item *item, u64 val)
719 {
720         item->root_dirid = cpu_to_le64(val);
721 }
722
723 static inline u32 btrfs_root_refs(struct btrfs_root_item *item)
724 {
725         return le32_to_cpu(item->refs);
726 }
727
728 static inline void btrfs_set_root_refs(struct btrfs_root_item *item, u32 val)
729 {
730         item->refs = cpu_to_le32(val);
731 }
732
733 static inline u64 btrfs_super_blocknr(struct btrfs_super_block *s)
734 {
735         return le64_to_cpu(s->blocknr);
736 }
737
738 static inline void btrfs_set_super_blocknr(struct btrfs_super_block *s, u64 val)
739 {
740         s->blocknr = cpu_to_le64(val);
741 }
742
743 static inline u64 btrfs_super_generation(struct btrfs_super_block *s)
744 {
745         return le64_to_cpu(s->generation);
746 }
747
748 static inline void btrfs_set_super_generation(struct btrfs_super_block *s,
749                                               u64 val)
750 {
751         s->generation = cpu_to_le64(val);
752 }
753
754 static inline u64 btrfs_super_root(struct btrfs_super_block *s)
755 {
756         return le64_to_cpu(s->root);
757 }
758
759 static inline void btrfs_set_super_root(struct btrfs_super_block *s, u64 val)
760 {
761         s->root = cpu_to_le64(val);
762 }
763
764 static inline u64 btrfs_super_total_blocks(struct btrfs_super_block *s)
765 {
766         return le64_to_cpu(s->total_blocks);
767 }
768
769 static inline void btrfs_set_super_total_blocks(struct btrfs_super_block *s,
770                                                 u64 val)
771 {
772         s->total_blocks = cpu_to_le64(val);
773 }
774
775 static inline u64 btrfs_super_blocks_used(struct btrfs_super_block *s)
776 {
777         return le64_to_cpu(s->blocks_used);
778 }
779
780 static inline void btrfs_set_super_blocks_used(struct btrfs_super_block *s,
781                                                 u64 val)
782 {
783         s->blocks_used = cpu_to_le64(val);
784 }
785
786 static inline u32 btrfs_super_blocksize(struct btrfs_super_block *s)
787 {
788         return le32_to_cpu(s->blocksize);
789 }
790
791 static inline void btrfs_set_super_blocksize(struct btrfs_super_block *s,
792                                                 u32 val)
793 {
794         s->blocksize = cpu_to_le32(val);
795 }
796
797 static inline u64 btrfs_super_root_dir(struct btrfs_super_block *s)
798 {
799         return le64_to_cpu(s->root_dir_objectid);
800 }
801
802 static inline void btrfs_set_super_root_dir(struct btrfs_super_block *s, u64
803                                             val)
804 {
805         s->root_dir_objectid = cpu_to_le64(val);
806 }
807
808 static inline u8 *btrfs_leaf_data(struct btrfs_leaf *l)
809 {
810         return (u8 *)l->items;
811 }
812
813 static inline u64 btrfs_file_extent_disk_blocknr(struct btrfs_file_extent_item
814                                                  *e)
815 {
816         return le64_to_cpu(e->disk_blocknr);
817 }
818
819 static inline void btrfs_set_file_extent_disk_blocknr(struct
820                                                       btrfs_file_extent_item
821                                                       *e, u64 val)
822 {
823         e->disk_blocknr = cpu_to_le64(val);
824 }
825
826 static inline u64 btrfs_file_extent_generation(struct btrfs_file_extent_item *e)
827 {
828         return le64_to_cpu(e->generation);
829 }
830
831 static inline void btrfs_set_file_extent_generation(struct
832                                                     btrfs_file_extent_item *e,
833                                                     u64 val)
834 {
835         e->generation = cpu_to_le64(val);
836 }
837
838 static inline u64 btrfs_file_extent_disk_num_blocks(struct
839                                                     btrfs_file_extent_item *e)
840 {
841         return le64_to_cpu(e->disk_num_blocks);
842 }
843
844 static inline void btrfs_set_file_extent_disk_num_blocks(struct
845                                                          btrfs_file_extent_item
846                                                          *e, u64 val)
847 {
848         e->disk_num_blocks = cpu_to_le64(val);
849 }
850
851 static inline u64 btrfs_file_extent_offset(struct btrfs_file_extent_item *e)
852 {
853         return le64_to_cpu(e->offset);
854 }
855
856 static inline void btrfs_set_file_extent_offset(struct btrfs_file_extent_item
857                                                 *e, u64 val)
858 {
859         e->offset = cpu_to_le64(val);
860 }
861
862 static inline u64 btrfs_file_extent_num_blocks(struct btrfs_file_extent_item
863                                                *e)
864 {
865         return le64_to_cpu(e->num_blocks);
866 }
867
868 static inline void btrfs_set_file_extent_num_blocks(struct
869                                                     btrfs_file_extent_item *e,
870                                                     u64 val)
871 {
872         e->num_blocks = cpu_to_le64(val);
873 }
874
875 /* helper function to cast into the data area of the leaf. */
876 #define btrfs_item_ptr(leaf, slot, type) \
877         ((type *)(btrfs_leaf_data(leaf) + \
878         btrfs_item_offset((leaf)->items + (slot))))
879
880 struct btrfs_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
881                                             struct btrfs_root *root);
882 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
883                   struct btrfs_buffer *buf);
884 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
885                       *root, u64 blocknr, u64 num_blocks, int pin);
886 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
887                       *root, struct btrfs_key *key, struct btrfs_path *p, int
888                       ins_len, int cow);
889 void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p);
890 void btrfs_init_path(struct btrfs_path *p);
891 int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
892                    struct btrfs_path *path);
893 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
894                       *root, struct btrfs_key *key, void *data, u32 data_size);
895 int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root
896                             *root, struct btrfs_path *path, struct btrfs_key
897                             *cpu_key, u32 data_size);
898 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path);
899 int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf);
900 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
901                         *root, struct btrfs_buffer *snap);
902 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
903                                btrfs_root *root);
904 int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
905                    struct btrfs_key *key);
906 int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
907                       *root, struct btrfs_key *key, struct btrfs_root_item
908                       *item);
909 int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
910                       *root, struct btrfs_key *key, struct btrfs_root_item
911                       *item);
912 int btrfs_find_last_root(struct btrfs_root *root, u64 objectid, struct
913                          btrfs_root_item *item, struct btrfs_key *key);
914 int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
915                           *root, char *name, int name_len, u64 dir,
916                           struct btrfs_key *location, u8 type);
917 int btrfs_lookup_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
918                           *root, struct btrfs_path *path, u64 dir, char *name,
919                           int name_len, int mod);
920 int btrfs_match_dir_item_name(struct btrfs_root *root, struct btrfs_path *path,
921                               char *name, int name_len);
922 int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
923                              struct btrfs_root *fs_root,
924                              u64 dirid, u64 *objectid);
925 int btrfs_insert_inode_map(struct btrfs_trans_handle *trans,
926                            struct btrfs_root *root,
927                            u64 objectid, struct btrfs_key *location);
928 int btrfs_lookup_inode_map(struct btrfs_trans_handle *trans,
929                            struct btrfs_root *root, struct btrfs_path *path,
930                            u64 objectid, int mod);
931 int btrfs_insert_inode(struct btrfs_trans_handle *trans, struct btrfs_root
932                        *root, u64 objectid, struct btrfs_inode_item
933                        *inode_item);
934 int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root
935                        *root, struct btrfs_path *path, u64 objectid, int mod);
936 #endif