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