early work to file_write in big extents
[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         __le64 offset;
44         __le32 flags;
45 } __attribute__ ((__packed__));
46
47 struct btrfs_key {
48         u64 objectid;
49         u64 offset;
50         u32 flags;
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         __le64 extent_offset;
227         u8 csum[BTRFS_CSUM_SIZE];
228 } __attribute__ ((__packed__));
229
230 struct btrfs_device_item {
231         __le16 pathlen;
232         __le64 device_id;
233 } __attribute__ ((__packed__));
234
235 struct btrfs_fs_info {
236         struct btrfs_root *fs_root;
237         struct btrfs_root *extent_root;
238         struct btrfs_root *tree_root;
239         struct btrfs_root *dev_root;
240         struct btrfs_key current_insert;
241         struct btrfs_key last_insert;
242         struct radix_tree_root cache_radix;
243         struct radix_tree_root pinned_radix;
244         struct radix_tree_root dev_radix;
245         struct list_head trans;
246         struct list_head cache;
247         u64 last_inode_alloc;
248         u64 last_inode_alloc_dirid;
249         u64 generation;
250         int cache_size;
251         int fp;
252         struct btrfs_trans_handle *running_transaction;
253         struct btrfs_super_block *disk_super;
254 };
255
256 /*
257  * in ram representation of the tree.  extent_root is used for all allocations
258  * and for the extent tree extent_root root.  current_insert is used
259  * only for the extent tree.
260  */
261 struct btrfs_root {
262         struct btrfs_buffer *node;
263         struct btrfs_buffer *commit_root;
264         struct btrfs_root_item root_item;
265         struct btrfs_key root_key;
266         struct btrfs_fs_info *fs_info;
267         u32 blocksize;
268         int ref_cows;
269         u32 type;
270 };
271
272 /* the lower bits in the key flags defines the item type */
273 #define BTRFS_KEY_TYPE_MAX      256
274 #define BTRFS_KEY_TYPE_MASK     (BTRFS_KEY_TYPE_MAX - 1)
275
276 #define BTRFS_KEY_OVERFLOW_MAX 128
277 #define BTRFS_KEY_OVERFLOW_SHIFT 8
278 #define BTRFS_KEY_OVERFLOW_MASK (0x7FULL << BTRFS_KEY_OVERFLOW_SHIFT)
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_key_overflow(struct btrfs_key *key)
579 {
580         u32 over = key->flags & BTRFS_KEY_OVERFLOW_MASK;
581         return over >> BTRFS_KEY_OVERFLOW_SHIFT;
582 }
583
584 static inline void btrfs_set_key_overflow(struct btrfs_key *key, u32 over)
585 {
586         BUG_ON(over >= BTRFS_KEY_OVERFLOW_MAX);
587         over = over << BTRFS_KEY_OVERFLOW_SHIFT;
588         key->flags = (key->flags & ~((u64)BTRFS_KEY_OVERFLOW_MASK)) | over;
589 }
590
591 static inline u32 btrfs_key_type(struct btrfs_key *key)
592 {
593         return key->flags & BTRFS_KEY_TYPE_MASK;
594 }
595
596 static inline u32 btrfs_disk_key_type(struct btrfs_disk_key *key)
597 {
598         return le32_to_cpu(key->flags) & BTRFS_KEY_TYPE_MASK;
599 }
600
601 static inline void btrfs_set_key_type(struct btrfs_key *key, u32 type)
602 {
603         BUG_ON(type >= BTRFS_KEY_TYPE_MAX);
604         key->flags = (key->flags & ~((u64)BTRFS_KEY_TYPE_MASK)) | type;
605 }
606
607 static inline void btrfs_set_disk_key_type(struct btrfs_disk_key *key, u32 type)
608 {
609         u32 flags = btrfs_disk_key_flags(key);
610         BUG_ON(type >= BTRFS_KEY_TYPE_MAX);
611         flags = (flags & ~((u64)BTRFS_KEY_TYPE_MASK)) | type;
612         btrfs_set_disk_key_flags(key, flags);
613 }
614
615 static inline u32 btrfs_disk_key_overflow(struct btrfs_disk_key *key)
616 {
617         u32 over = le32_to_cpu(key->flags) & BTRFS_KEY_OVERFLOW_MASK;
618         return over >> BTRFS_KEY_OVERFLOW_SHIFT;
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         over = over << BTRFS_KEY_OVERFLOW_SHIFT;
627         flags = (flags & ~((u64)BTRFS_KEY_OVERFLOW_MASK)) | over;
628         btrfs_set_disk_key_flags(key, flags);
629 }
630
631 static inline u64 btrfs_header_blocknr(struct btrfs_header *h)
632 {
633         return le64_to_cpu(h->blocknr);
634 }
635
636 static inline void btrfs_set_header_blocknr(struct btrfs_header *h, u64 blocknr)
637 {
638         h->blocknr = cpu_to_le64(blocknr);
639 }
640
641 static inline u64 btrfs_header_generation(struct btrfs_header *h)
642 {
643         return le64_to_cpu(h->generation);
644 }
645
646 static inline void btrfs_set_header_generation(struct btrfs_header *h,
647                                                u64 val)
648 {
649         h->generation = cpu_to_le64(val);
650 }
651
652 static inline u16 btrfs_header_nritems(struct btrfs_header *h)
653 {
654         return le16_to_cpu(h->nritems);
655 }
656
657 static inline void btrfs_set_header_nritems(struct btrfs_header *h, u16 val)
658 {
659         h->nritems = cpu_to_le16(val);
660 }
661
662 static inline u16 btrfs_header_flags(struct btrfs_header *h)
663 {
664         return le16_to_cpu(h->flags);
665 }
666
667 static inline void btrfs_set_header_flags(struct btrfs_header *h, u16 val)
668 {
669         h->flags = cpu_to_le16(val);
670 }
671
672 static inline int btrfs_header_level(struct btrfs_header *h)
673 {
674         return h->level;
675 }
676
677 static inline void btrfs_set_header_level(struct btrfs_header *h, int level)
678 {
679         BUG_ON(level > BTRFS_MAX_LEVEL);
680         h->level = level;
681 }
682
683 static inline int btrfs_is_leaf(struct btrfs_node *n)
684 {
685         return (btrfs_header_level(&n->header) == 0);
686 }
687
688 static inline u64 btrfs_root_blocknr(struct btrfs_root_item *item)
689 {
690         return le64_to_cpu(item->blocknr);
691 }
692
693 static inline void btrfs_set_root_blocknr(struct btrfs_root_item *item, u64 val)
694 {
695         item->blocknr = cpu_to_le64(val);
696 }
697
698 static inline u64 btrfs_root_dirid(struct btrfs_root_item *item)
699 {
700         return le64_to_cpu(item->root_dirid);
701 }
702
703 static inline void btrfs_set_root_dirid(struct btrfs_root_item *item, u64 val)
704 {
705         item->root_dirid = cpu_to_le64(val);
706 }
707
708 static inline u32 btrfs_root_refs(struct btrfs_root_item *item)
709 {
710         return le32_to_cpu(item->refs);
711 }
712
713 static inline void btrfs_set_root_refs(struct btrfs_root_item *item, u32 val)
714 {
715         item->refs = cpu_to_le32(val);
716 }
717
718 static inline u64 btrfs_super_blocknr(struct btrfs_super_block *s)
719 {
720         return le64_to_cpu(s->blocknr);
721 }
722
723 static inline void btrfs_set_super_blocknr(struct btrfs_super_block *s, u64 val)
724 {
725         s->blocknr = cpu_to_le64(val);
726 }
727
728 static inline u64 btrfs_super_generation(struct btrfs_super_block *s)
729 {
730         return le64_to_cpu(s->generation);
731 }
732
733 static inline void btrfs_set_super_generation(struct btrfs_super_block *s,
734                                               u64 val)
735 {
736         s->generation = cpu_to_le64(val);
737 }
738
739 static inline u64 btrfs_super_root(struct btrfs_super_block *s)
740 {
741         return le64_to_cpu(s->root);
742 }
743
744 static inline void btrfs_set_super_root(struct btrfs_super_block *s, u64 val)
745 {
746         s->root = cpu_to_le64(val);
747 }
748
749 static inline u64 btrfs_super_total_blocks(struct btrfs_super_block *s)
750 {
751         return le64_to_cpu(s->total_blocks);
752 }
753
754 static inline void btrfs_set_super_total_blocks(struct btrfs_super_block *s,
755                                                 u64 val)
756 {
757         s->total_blocks = cpu_to_le64(val);
758 }
759
760 static inline u64 btrfs_super_blocks_used(struct btrfs_super_block *s)
761 {
762         return le64_to_cpu(s->blocks_used);
763 }
764
765 static inline void btrfs_set_super_blocks_used(struct btrfs_super_block *s,
766                                                 u64 val)
767 {
768         s->blocks_used = cpu_to_le64(val);
769 }
770
771 static inline u32 btrfs_super_blocksize(struct btrfs_super_block *s)
772 {
773         return le32_to_cpu(s->blocksize);
774 }
775
776 static inline void btrfs_set_super_blocksize(struct btrfs_super_block *s,
777                                                 u32 val)
778 {
779         s->blocksize = cpu_to_le32(val);
780 }
781
782 static inline u64 btrfs_super_root_dir(struct btrfs_super_block *s)
783 {
784         return le64_to_cpu(s->root_dir_objectid);
785 }
786
787 static inline void btrfs_set_super_root_dir(struct btrfs_super_block *s, u64
788                                             val)
789 {
790         s->root_dir_objectid = cpu_to_le64(val);
791 }
792
793 static inline u64 btrfs_super_last_device_id(struct btrfs_super_block *s)
794 {
795         return le64_to_cpu(s->last_device_id);
796 }
797
798 static inline void btrfs_set_super_last_device_id(struct btrfs_super_block *s,
799                                                   u64 val)
800 {
801         s->last_device_id = cpu_to_le64(val);
802 }
803
804 static inline u64 btrfs_super_device_id(struct btrfs_super_block *s)
805 {
806         return le64_to_cpu(s->device_id);
807 }
808
809 static inline void btrfs_set_super_device_id(struct btrfs_super_block *s,
810                                                   u64 val)
811 {
812         s->device_id = cpu_to_le64(val);
813 }
814
815 static inline u64 btrfs_super_device_block_start(struct btrfs_super_block *s)
816 {
817         return le64_to_cpu(s->device_block_start);
818 }
819
820 static inline void btrfs_set_super_device_block_start(struct btrfs_super_block
821                                                       *s, u64 val)
822 {
823         s->device_block_start = cpu_to_le64(val);
824 }
825
826 static inline u64 btrfs_super_device_num_blocks(struct btrfs_super_block *s)
827 {
828         return le64_to_cpu(s->device_num_blocks);
829 }
830
831 static inline void btrfs_set_super_device_num_blocks(struct btrfs_super_block
832                                                      *s, u64 val)
833 {
834         s->device_num_blocks = cpu_to_le64(val);
835 }
836
837 static inline u64 btrfs_super_device_root(struct btrfs_super_block *s)
838 {
839         return le64_to_cpu(s->device_root);
840 }
841
842 static inline void btrfs_set_super_device_root(struct btrfs_super_block
843                                                       *s, u64 val)
844 {
845         s->device_root = cpu_to_le64(val);
846 }
847
848 static inline u8 *btrfs_leaf_data(struct btrfs_leaf *l)
849 {
850         return (u8 *)l->items;
851 }
852
853 static inline u64 btrfs_file_extent_disk_blocknr(struct btrfs_file_extent_item
854                                                  *e)
855 {
856         return le64_to_cpu(e->disk_blocknr);
857 }
858
859 static inline void btrfs_set_file_extent_disk_blocknr(struct
860                                                       btrfs_file_extent_item
861                                                       *e, u64 val)
862 {
863         e->disk_blocknr = cpu_to_le64(val);
864 }
865
866 static inline u64 btrfs_file_extent_generation(struct btrfs_file_extent_item *e)
867 {
868         return le64_to_cpu(e->generation);
869 }
870
871 static inline void btrfs_set_file_extent_generation(struct
872                                                     btrfs_file_extent_item *e,
873                                                     u64 val)
874 {
875         e->generation = cpu_to_le64(val);
876 }
877
878 static inline u64 btrfs_file_extent_disk_num_blocks(struct
879                                                     btrfs_file_extent_item *e)
880 {
881         return le64_to_cpu(e->disk_num_blocks);
882 }
883
884 static inline void btrfs_set_file_extent_disk_num_blocks(struct
885                                                          btrfs_file_extent_item
886                                                          *e, u64 val)
887 {
888         e->disk_num_blocks = cpu_to_le64(val);
889 }
890
891 static inline u64 btrfs_file_extent_offset(struct btrfs_file_extent_item *e)
892 {
893         return le64_to_cpu(e->offset);
894 }
895
896 static inline void btrfs_set_file_extent_offset(struct btrfs_file_extent_item
897                                                 *e, u64 val)
898 {
899         e->offset = cpu_to_le64(val);
900 }
901
902 static inline u64 btrfs_file_extent_num_blocks(struct btrfs_file_extent_item
903                                                *e)
904 {
905         return le64_to_cpu(e->num_blocks);
906 }
907
908 static inline void btrfs_set_file_extent_num_blocks(struct
909                                                     btrfs_file_extent_item *e,
910                                                     u64 val)
911 {
912         e->num_blocks = cpu_to_le64(val);
913 }
914
915 static inline u64 btrfs_csum_extent_offset(struct btrfs_csum_item *c)
916 {
917         return le64_to_cpu(c->extent_offset);
918 }
919
920 static inline void btrfs_set_csum_extent_offset(struct btrfs_csum_item *c,
921                                                 u64 val)
922 {
923         c->extent_offset = cpu_to_le64(val);
924 }
925
926 static inline u16 btrfs_device_pathlen(struct btrfs_device_item *d)
927 {
928         return le16_to_cpu(d->pathlen);
929 }
930
931 static inline void btrfs_set_device_pathlen(struct btrfs_device_item *d,
932                                                 u16 val)
933 {
934         d->pathlen = cpu_to_le16(val);
935 }
936
937 static inline u64 btrfs_device_id(struct btrfs_device_item *d)
938 {
939         return le64_to_cpu(d->device_id);
940 }
941
942 static inline void btrfs_set_device_id(struct btrfs_device_item *d,
943                                                 u64 val)
944 {
945         d->device_id = cpu_to_le64(val);
946 }
947
948 /* helper function to cast into the data area of the leaf. */
949 #define btrfs_item_ptr(leaf, slot, type) \
950         ((type *)(btrfs_leaf_data(leaf) + \
951         btrfs_item_offset((leaf)->items + (slot))))
952
953 struct btrfs_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
954                                             struct btrfs_root *root);
955 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
956                   struct btrfs_buffer *buf);
957 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
958                       *root, u64 blocknr, u64 num_blocks, int pin);
959 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
960                       *root, struct btrfs_key *key, struct btrfs_path *p, int
961                       ins_len, int cow);
962 void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p);
963 void btrfs_init_path(struct btrfs_path *p);
964 int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
965                    struct btrfs_path *path);
966 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
967                       *root, struct btrfs_key *key, void *data, u32 data_size);
968 int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root
969                             *root, struct btrfs_path *path, struct btrfs_key
970                             *cpu_key, u32 data_size);
971 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path);
972 int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf);
973 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
974                         *root, struct btrfs_buffer *snap);
975 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
976                                btrfs_root *root);
977 int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
978                    struct btrfs_key *key);
979 int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
980                       *root, struct btrfs_key *key, struct btrfs_root_item
981                       *item);
982 int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
983                       *root, struct btrfs_key *key, struct btrfs_root_item
984                       *item);
985 int btrfs_find_last_root(struct btrfs_root *root, u64 objectid, struct
986                          btrfs_root_item *item, struct btrfs_key *key);
987 int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
988                           *root, char *name, int name_len, u64 dir,
989                           struct btrfs_key *location, u8 type);
990 int btrfs_lookup_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
991                           *root, struct btrfs_path *path, u64 dir, char *name,
992                           int name_len, int mod);
993 int btrfs_match_dir_item_name(struct btrfs_root *root, struct btrfs_path *path,
994                               char *name, int name_len);
995 int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
996                              struct btrfs_root *fs_root,
997                              u64 dirid, u64 *objectid);
998 int btrfs_insert_inode(struct btrfs_trans_handle *trans, struct btrfs_root
999                        *root, u64 objectid, struct btrfs_inode_item
1000                        *inode_item);
1001 int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root
1002                        *root, struct btrfs_path *path, u64 objectid, int mod);
1003 #endif