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