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