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