Create a slightly more generic extent-caching structure
[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 btrfs_key key;
280         struct btrfs_block_group_item item;
281 };
282
283 struct btrfs_fs_info {
284         struct btrfs_root *fs_root;
285         struct btrfs_root *extent_root;
286         struct btrfs_root *tree_root;
287         struct btrfs_key last_insert;
288         struct cache_tree extent_cache;
289         struct radix_tree_root block_group_radix;
290         struct cache_tree pending_tree;
291         struct cache_tree pinned_tree;
292         struct cache_tree del_pending;
293         struct list_head trans;
294         struct list_head cache;
295         u64 last_inode_alloc;
296         u64 last_inode_alloc_dirid;
297         u64 generation;
298         int cache_size;
299         int fp;
300         struct btrfs_trans_handle *running_transaction;
301         struct btrfs_super_block *disk_super;
302 };
303
304 /*
305  * in ram representation of the tree.  extent_root is used for all allocations
306  * and for the extent tree extent_root root.
307  */
308 struct btrfs_root {
309         struct btrfs_buffer *node;
310         struct btrfs_buffer *commit_root;
311         struct btrfs_root_item root_item;
312         struct btrfs_key root_key;
313         struct btrfs_fs_info *fs_info;
314
315         /* data allocations are done in sectorsize units */
316         u32 sectorsize;
317
318         /* node allocations are done in nodesize units */
319         u32 nodesize;
320
321         /* leaf allocations are done in leafsize units */
322         u32 leafsize;
323
324         int ref_cows;
325         u32 type;
326 };
327
328 /* the lower bits in the key flags defines the item type */
329 #define BTRFS_KEY_TYPE_MAX      256
330 #define BTRFS_KEY_TYPE_SHIFT    24
331 #define BTRFS_KEY_TYPE_MASK     (((u32)BTRFS_KEY_TYPE_MAX - 1) << \
332                                   BTRFS_KEY_TYPE_SHIFT)
333
334 /*
335  * inode items have the data typically returned from stat and store other
336  * info about object characteristics.  There is one for every file and dir in
337  * the FS
338  */
339 #define BTRFS_INODE_ITEM_KEY            1
340
341 /* reserve 2-15 close to the inode for later flexibility */
342
343 /*
344  * dir items are the name -> inode pointers in a directory.  There is one
345  * for every name in a directory.
346  */
347 #define BTRFS_DIR_ITEM_KEY      16
348 #define BTRFS_DIR_INDEX_KEY     17
349 /*
350  * extent data is for file data
351  */
352 #define BTRFS_EXTENT_DATA_KEY   18
353 /*
354  * csum items have the checksums for data in the extents
355  */
356 #define BTRFS_CSUM_ITEM_KEY     19
357
358 /* reserve 20-31 for other file stuff */
359
360 /*
361  * root items point to tree roots.  There are typically in the root
362  * tree used by the super block to find all the other trees
363  */
364 #define BTRFS_ROOT_ITEM_KEY     32
365 /*
366  * extent items are in the extent map tree.  These record which blocks
367  * are used, and how many references there are to each block
368  */
369 #define BTRFS_EXTENT_ITEM_KEY   33
370
371 /*
372  * block groups give us hints into the extent allocation trees.  Which
373  * blocks are free etc etc
374  */
375 #define BTRFS_BLOCK_GROUP_ITEM_KEY 34
376
377 /*
378  * string items are for debugging.  They just store a short string of
379  * data in the FS
380  */
381 #define BTRFS_STRING_ITEM_KEY   253
382
383
384 static inline u64 btrfs_block_group_used(struct btrfs_block_group_item *bi)
385 {
386         return le64_to_cpu(bi->used);
387 }
388
389 static inline void btrfs_set_block_group_used(struct
390                                                    btrfs_block_group_item *bi,
391                                                    u64 val)
392 {
393         bi->used = cpu_to_le64(val);
394 }
395
396 static inline u64 btrfs_inode_generation(struct btrfs_inode_item *i)
397 {
398         return le64_to_cpu(i->generation);
399 }
400
401 static inline void btrfs_set_inode_generation(struct btrfs_inode_item *i,
402                                               u64 val)
403 {
404         i->generation = cpu_to_le64(val);
405 }
406
407 static inline u64 btrfs_inode_size(struct btrfs_inode_item *i)
408 {
409         return le64_to_cpu(i->size);
410 }
411
412 static inline void btrfs_set_inode_size(struct btrfs_inode_item *i, u64 val)
413 {
414         i->size = cpu_to_le64(val);
415 }
416
417 static inline u64 btrfs_inode_nblocks(struct btrfs_inode_item *i)
418 {
419         return le64_to_cpu(i->nblocks);
420 }
421
422 static inline void btrfs_set_inode_nblocks(struct btrfs_inode_item *i, u64 val)
423 {
424         i->nblocks = cpu_to_le64(val);
425 }
426
427 static inline u64 btrfs_inode_block_group(struct btrfs_inode_item *i)
428 {
429         return le64_to_cpu(i->block_group);
430 }
431
432 static inline void btrfs_set_inode_block_group(struct btrfs_inode_item *i,
433                                                 u64 val)
434 {
435         i->block_group = cpu_to_le64(val);
436 }
437
438 static inline u32 btrfs_inode_nlink(struct btrfs_inode_item *i)
439 {
440         return le32_to_cpu(i->nlink);
441 }
442
443 static inline void btrfs_set_inode_nlink(struct btrfs_inode_item *i, u32 val)
444 {
445         i->nlink = cpu_to_le32(val);
446 }
447
448 static inline u32 btrfs_inode_uid(struct btrfs_inode_item *i)
449 {
450         return le32_to_cpu(i->uid);
451 }
452
453 static inline void btrfs_set_inode_uid(struct btrfs_inode_item *i, u32 val)
454 {
455         i->uid = cpu_to_le32(val);
456 }
457
458 static inline u32 btrfs_inode_gid(struct btrfs_inode_item *i)
459 {
460         return le32_to_cpu(i->gid);
461 }
462
463 static inline void btrfs_set_inode_gid(struct btrfs_inode_item *i, u32 val)
464 {
465         i->gid = cpu_to_le32(val);
466 }
467
468 static inline u32 btrfs_inode_mode(struct btrfs_inode_item *i)
469 {
470         return le32_to_cpu(i->mode);
471 }
472
473 static inline void btrfs_set_inode_mode(struct btrfs_inode_item *i, u32 val)
474 {
475         i->mode = cpu_to_le32(val);
476 }
477
478 static inline u32 btrfs_inode_rdev(struct btrfs_inode_item *i)
479 {
480         return le32_to_cpu(i->rdev);
481 }
482
483 static inline void btrfs_set_inode_rdev(struct btrfs_inode_item *i, u32 val)
484 {
485         i->rdev = cpu_to_le32(val);
486 }
487
488 static inline u16 btrfs_inode_flags(struct btrfs_inode_item *i)
489 {
490         return le16_to_cpu(i->flags);
491 }
492
493 static inline void btrfs_set_inode_flags(struct btrfs_inode_item *i, u16 val)
494 {
495         i->flags = cpu_to_le16(val);
496 }
497
498 static inline u16 btrfs_inode_compat_flags(struct btrfs_inode_item *i)
499 {
500         return le16_to_cpu(i->compat_flags);
501 }
502
503 static inline void btrfs_set_inode_compat_flags(struct btrfs_inode_item *i,
504                                                 u16 val)
505 {
506         i->compat_flags = cpu_to_le16(val);
507 }
508
509 static inline u64 btrfs_timespec_sec(struct btrfs_inode_timespec *ts)
510 {
511         return le64_to_cpu(ts->sec);
512 }
513
514 static inline void btrfs_set_timespec_sec(struct btrfs_inode_timespec *ts,
515                                           u64 val)
516 {
517         ts->sec = cpu_to_le64(val);
518 }
519
520 static inline u32 btrfs_timespec_nsec(struct btrfs_inode_timespec *ts)
521 {
522         return le32_to_cpu(ts->nsec);
523 }
524
525 static inline void btrfs_set_timespec_nsec(struct btrfs_inode_timespec *ts,
526                                           u32 val)
527 {
528         ts->nsec = cpu_to_le32(val);
529 }
530
531 static inline u32 btrfs_extent_refs(struct btrfs_extent_item *ei)
532 {
533         return le32_to_cpu(ei->refs);
534 }
535
536 static inline void btrfs_set_extent_refs(struct btrfs_extent_item *ei, u32 val)
537 {
538         ei->refs = cpu_to_le32(val);
539 }
540
541 static inline u64 btrfs_extent_owner(struct btrfs_extent_item *ei)
542 {
543         return le64_to_cpu(ei->owner);
544 }
545
546 static inline void btrfs_set_extent_owner(struct btrfs_extent_item *ei, u64 val)
547 {
548         ei->owner = cpu_to_le64(val);
549 }
550
551 static inline u64 btrfs_node_blockptr(struct btrfs_node *n, int nr)
552 {
553         return le64_to_cpu(n->ptrs[nr].blockptr);
554 }
555
556
557 static inline void btrfs_set_node_blockptr(struct btrfs_node *n, int nr,
558                                            u64 val)
559 {
560         n->ptrs[nr].blockptr = cpu_to_le64(val);
561 }
562
563 static inline u32 btrfs_item_offset(struct btrfs_item *item)
564 {
565         return le32_to_cpu(item->offset);
566 }
567
568 static inline void btrfs_set_item_offset(struct btrfs_item *item, u32 val)
569 {
570         item->offset = cpu_to_le32(val);
571 }
572
573 static inline u32 btrfs_item_end(struct btrfs_item *item)
574 {
575         return le32_to_cpu(item->offset) + le32_to_cpu(item->size);
576 }
577
578 static inline u32 btrfs_item_size(struct btrfs_item *item)
579 {
580         return le32_to_cpu(item->size);
581 }
582
583 static inline void btrfs_set_item_size(struct btrfs_item *item, u32 val)
584 {
585         item->size = cpu_to_le32(val);
586 }
587
588 static inline u16 btrfs_dir_flags(struct btrfs_dir_item *d)
589 {
590         return le16_to_cpu(d->flags);
591 }
592
593 static inline void btrfs_set_dir_flags(struct btrfs_dir_item *d, u16 val)
594 {
595         d->flags = cpu_to_le16(val);
596 }
597
598 static inline u8 btrfs_dir_type(struct btrfs_dir_item *d)
599 {
600         return d->type;
601 }
602
603 static inline void btrfs_set_dir_type(struct btrfs_dir_item *d, u8 val)
604 {
605         d->type = val;
606 }
607
608 static inline u16 btrfs_dir_name_len(struct btrfs_dir_item *d)
609 {
610         return le16_to_cpu(d->name_len);
611 }
612
613 static inline void btrfs_set_dir_name_len(struct btrfs_dir_item *d, u16 val)
614 {
615         d->name_len = cpu_to_le16(val);
616 }
617
618 static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu,
619                                          struct btrfs_disk_key *disk)
620 {
621         cpu->offset = le64_to_cpu(disk->offset);
622         cpu->type = le32_to_cpu(disk->type);
623         cpu->objectid = le64_to_cpu(disk->objectid);
624 }
625
626 static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk,
627                                          struct btrfs_key *cpu)
628 {
629         disk->offset = cpu_to_le64(cpu->offset);
630         disk->type = cpu_to_le32(cpu->type);
631         disk->objectid = cpu_to_le64(cpu->objectid);
632 }
633
634 static inline u64 btrfs_disk_key_objectid(struct btrfs_disk_key *disk)
635 {
636         return le64_to_cpu(disk->objectid);
637 }
638
639 static inline void btrfs_set_disk_key_objectid(struct btrfs_disk_key *disk,
640                                                u64 val)
641 {
642         disk->objectid = cpu_to_le64(val);
643 }
644
645 static inline u64 btrfs_disk_key_offset(struct btrfs_disk_key *disk)
646 {
647         return le64_to_cpu(disk->offset);
648 }
649
650 static inline void btrfs_set_disk_key_offset(struct btrfs_disk_key *disk,
651                                              u64 val)
652 {
653         disk->offset = cpu_to_le64(val);
654 }
655
656 static inline u8 btrfs_disk_key_type(struct btrfs_disk_key *key)
657 {
658         return key->type;
659 }
660
661 static inline void btrfs_set_disk_key_type(struct btrfs_disk_key *key, u8 val)
662 {
663         key->type = val;
664 }
665
666 static inline u32 btrfs_key_type(struct btrfs_key *key)
667 {
668         return key->type;
669 }
670
671 static inline void btrfs_set_key_type(struct btrfs_key *key, u32 val)
672 {
673         key->type = val;
674 }
675
676 static inline u64 btrfs_header_bytenr(struct btrfs_header *h)
677 {
678         return le64_to_cpu(h->bytenr);
679 }
680
681 static inline void btrfs_set_header_bytenr(struct btrfs_header *h, u64 bytenr)
682 {
683         h->bytenr = cpu_to_le64(bytenr);
684 }
685
686 static inline u64 btrfs_header_generation(struct btrfs_header *h)
687 {
688         return le64_to_cpu(h->generation);
689 }
690
691 static inline void btrfs_set_header_generation(struct btrfs_header *h,
692                                                u64 val)
693 {
694         h->generation = cpu_to_le64(val);
695 }
696
697 static inline u64 btrfs_header_owner(struct btrfs_header *h)
698 {
699         return le64_to_cpu(h->owner);
700 }
701
702 static inline void btrfs_set_header_owner(struct btrfs_header *h,
703                                                u64 val)
704 {
705         h->owner = cpu_to_le64(val);
706 }
707
708 static inline u32 btrfs_header_nritems(struct btrfs_header *h)
709 {
710         return le32_to_cpu(h->nritems);
711 }
712
713 static inline void btrfs_set_header_nritems(struct btrfs_header *h, u32 val)
714 {
715         h->nritems = cpu_to_le32(val);
716 }
717
718 static inline u16 btrfs_header_flags(struct btrfs_header *h)
719 {
720         return le16_to_cpu(h->flags);
721 }
722
723 static inline void btrfs_set_header_flags(struct btrfs_header *h, u16 val)
724 {
725         h->flags = cpu_to_le16(val);
726 }
727
728 static inline int btrfs_header_level(struct btrfs_header *h)
729 {
730         return h->level;
731 }
732
733 static inline void btrfs_set_header_level(struct btrfs_header *h, int level)
734 {
735         BUG_ON(level > BTRFS_MAX_LEVEL);
736         h->level = level;
737 }
738
739 static inline int btrfs_is_leaf(struct btrfs_node *n)
740 {
741         return (btrfs_header_level(&n->header) == 0);
742 }
743
744 static inline u64 btrfs_root_bytenr(struct btrfs_root_item *item)
745 {
746         return le64_to_cpu(item->bytenr);
747 }
748
749 static inline void btrfs_set_root_bytenr(struct btrfs_root_item *item, u64 val)
750 {
751         item->bytenr = cpu_to_le64(val);
752 }
753
754 static inline u8 btrfs_root_level(struct btrfs_root_item *item)
755 {
756         return item->level;
757 }
758
759 static inline void btrfs_set_root_level(struct btrfs_root_item *item, u8 val)
760 {
761         item->level = val;
762 }
763
764 static inline u64 btrfs_root_dirid(struct btrfs_root_item *item)
765 {
766         return le64_to_cpu(item->root_dirid);
767 }
768
769 static inline void btrfs_set_root_dirid(struct btrfs_root_item *item, u64 val)
770 {
771         item->root_dirid = cpu_to_le64(val);
772 }
773
774 static inline u32 btrfs_root_refs(struct btrfs_root_item *item)
775 {
776         return le32_to_cpu(item->refs);
777 }
778
779 static inline void btrfs_set_root_refs(struct btrfs_root_item *item, u32 val)
780 {
781         item->refs = cpu_to_le32(val);
782 }
783
784 static inline u32 btrfs_root_flags(struct btrfs_root_item *item)
785 {
786         return le32_to_cpu(item->flags);
787 }
788
789 static inline void btrfs_set_root_flags(struct btrfs_root_item *item, u32 val)
790 {
791         item->flags = cpu_to_le32(val);
792 }
793
794 static inline void btrfs_set_root_bytes_used(struct btrfs_root_item *item,
795                                               u64 val)
796 {
797         item->bytes_used = cpu_to_le64(val);
798 }
799
800 static inline u64 btrfs_root_bytes_used(struct btrfs_root_item *item)
801 {
802         return le64_to_cpu(item->bytes_used);
803 }
804
805 static inline u64 btrfs_super_bytenr(struct btrfs_super_block *s)
806 {
807         return le64_to_cpu(s->bytenr);
808 }
809
810 static inline void btrfs_set_super_bytenr(struct btrfs_super_block *s, u64 val)
811 {
812         s->bytenr = cpu_to_le64(val);
813 }
814
815 static inline u64 btrfs_super_generation(struct btrfs_super_block *s)
816 {
817         return le64_to_cpu(s->generation);
818 }
819
820 static inline void btrfs_set_super_generation(struct btrfs_super_block *s,
821                                               u64 val)
822 {
823         s->generation = cpu_to_le64(val);
824 }
825
826 static inline u8 btrfs_super_root_level(struct btrfs_super_block *s)
827 {
828         return s->root_level;
829 }
830
831 static inline void btrfs_set_super_root_level(struct btrfs_super_block *s,
832                                               u8 val)
833 {
834         s->root_level = val;
835 }
836
837 static inline u64 btrfs_super_root(struct btrfs_super_block *s)
838 {
839         return le64_to_cpu(s->root);
840 }
841
842 static inline void btrfs_set_super_root(struct btrfs_super_block *s, u64 val)
843 {
844         s->root = cpu_to_le64(val);
845 }
846
847 static inline u64 btrfs_super_total_bytes(struct btrfs_super_block *s)
848 {
849         return le64_to_cpu(s->total_bytes);
850 }
851
852 static inline void btrfs_set_super_total_bytes(struct btrfs_super_block *s,
853                                                 u64 val)
854 {
855         s->total_bytes = cpu_to_le64(val);
856 }
857
858 static inline u64 btrfs_super_bytes_used(struct btrfs_super_block *s)
859 {
860         return le64_to_cpu(s->bytes_used);
861 }
862
863 static inline void btrfs_set_super_bytes_used(struct btrfs_super_block *s,
864                                                 u64 val)
865 {
866         s->bytes_used = cpu_to_le64(val);
867 }
868
869 static inline u32 btrfs_super_sectorsize(struct btrfs_super_block *s)
870 {
871         return le32_to_cpu(s->sectorsize);
872 }
873
874 static inline void btrfs_set_super_sectorsize(struct btrfs_super_block *s,
875                                                 u32 val)
876 {
877         s->sectorsize = cpu_to_le32(val);
878 }
879
880 static inline u32 btrfs_super_nodesize(struct btrfs_super_block *s)
881 {
882         return le32_to_cpu(s->nodesize);
883 }
884
885 static inline void btrfs_set_super_nodesize(struct btrfs_super_block *s,
886                                                 u32 val)
887 {
888         s->nodesize = cpu_to_le32(val);
889 }
890
891 static inline u32 btrfs_super_leafsize(struct btrfs_super_block *s)
892 {
893         return le32_to_cpu(s->leafsize);
894 }
895
896 static inline void btrfs_set_super_leafsize(struct btrfs_super_block *s,
897                                                 u32 val)
898 {
899         s->leafsize = cpu_to_le32(val);
900 }
901
902 static inline u64 btrfs_super_root_dir(struct btrfs_super_block *s)
903 {
904         return le64_to_cpu(s->root_dir_objectid);
905 }
906
907 static inline void btrfs_set_super_root_dir(struct btrfs_super_block *s, u64
908                                             val)
909 {
910         s->root_dir_objectid = cpu_to_le64(val);
911 }
912
913 static inline u8 *btrfs_leaf_data(struct btrfs_leaf *l)
914 {
915         return (u8 *)l->items;
916 }
917
918 static inline int btrfs_file_extent_type(struct btrfs_file_extent_item *e)
919 {
920         return e->type;
921 }
922 static inline void btrfs_set_file_extent_type(struct btrfs_file_extent_item *e,
923                                               u8 val)
924 {
925         e->type = val;
926 }
927
928 static inline char *btrfs_file_extent_inline_start(struct
929                                                    btrfs_file_extent_item *e)
930 {
931         return (char *)(&e->disk_bytenr);
932 }
933
934 static inline u32 btrfs_file_extent_calc_inline_size(u32 datasize)
935 {
936         return (unsigned long)(&((struct
937                   btrfs_file_extent_item *)NULL)->disk_bytenr) + datasize;
938 }
939
940 static inline u32 btrfs_file_extent_inline_len(struct btrfs_item *e)
941 {
942         struct btrfs_file_extent_item *fe = NULL;
943         return btrfs_item_size(e) - (unsigned long)(&fe->disk_bytenr);
944 }
945
946 static inline u64 btrfs_file_extent_disk_bytenr(struct btrfs_file_extent_item
947                                                  *e)
948 {
949         return le64_to_cpu(e->disk_bytenr);
950 }
951
952 static inline void btrfs_set_file_extent_disk_bytenr(struct
953                                                       btrfs_file_extent_item
954                                                       *e, u64 val)
955 {
956         e->disk_bytenr = cpu_to_le64(val);
957 }
958
959 static inline u64 btrfs_file_extent_generation(struct btrfs_file_extent_item *e)
960 {
961         return le64_to_cpu(e->generation);
962 }
963
964 static inline void btrfs_set_file_extent_generation(struct
965                                                     btrfs_file_extent_item *e,
966                                                     u64 val)
967 {
968         e->generation = cpu_to_le64(val);
969 }
970
971 static inline u64 btrfs_file_extent_disk_num_bytes(struct
972                                                     btrfs_file_extent_item *e)
973 {
974         return le64_to_cpu(e->disk_num_bytes);
975 }
976
977 static inline void btrfs_set_file_extent_disk_num_bytes(struct
978                                                          btrfs_file_extent_item
979                                                          *e, u64 val)
980 {
981         e->disk_num_bytes = cpu_to_le64(val);
982 }
983
984 static inline u64 btrfs_file_extent_offset(struct btrfs_file_extent_item *e)
985 {
986         return le64_to_cpu(e->offset);
987 }
988
989 static inline void btrfs_set_file_extent_offset(struct btrfs_file_extent_item
990                                                 *e, u64 val)
991 {
992         e->offset = cpu_to_le64(val);
993 }
994
995 static inline u64 btrfs_file_extent_num_bytes(struct btrfs_file_extent_item
996                                                *e)
997 {
998         return le64_to_cpu(e->num_bytes);
999 }
1000
1001 static inline void btrfs_set_file_extent_num_bytes(struct
1002                                                     btrfs_file_extent_item *e,
1003                                                     u64 val)
1004 {
1005         e->num_bytes = 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 static inline u32 btrfs_level_size(struct btrfs_root *root, int level)
1014 {
1015         if (level == 0)
1016                 return root->leafsize;
1017         return root->nodesize;
1018 }
1019 int btrfs_comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2);
1020 int btrfs_extend_item(struct btrfs_trans_handle *trans, struct btrfs_root
1021                       *root, struct btrfs_path *path, u32 data_size);
1022 struct btrfs_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1023                                             struct btrfs_root *root,
1024                                             u32 blocksize);
1025 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1026                   struct btrfs_buffer *buf);
1027 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
1028                       *root, u64 bytenr, u64 num_bytes, int pin);
1029 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1030                       *root, struct btrfs_key *key, struct btrfs_path *p, int
1031                       ins_len, int cow);
1032 void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p);
1033 void btrfs_init_path(struct btrfs_path *p);
1034 int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1035                    struct btrfs_path *path);
1036 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
1037                       *root, struct btrfs_key *key, void *data, u32 data_size);
1038 int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root
1039                             *root, struct btrfs_path *path, struct btrfs_key
1040                             *cpu_key, u32 data_size);
1041 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path);
1042 int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf);
1043 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
1044                         *root, struct btrfs_buffer *snap);
1045 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
1046                                btrfs_root *root);
1047 int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1048                    struct btrfs_key *key);
1049 int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
1050                       *root, struct btrfs_key *key, struct btrfs_root_item
1051                       *item);
1052 int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
1053                       *root, struct btrfs_key *key, struct btrfs_root_item
1054                       *item);
1055 int btrfs_find_last_root(struct btrfs_root *root, u64 objectid, struct
1056                          btrfs_root_item *item, struct btrfs_key *key);
1057 int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
1058                           *root, char *name, int name_len, u64 dir,
1059                           struct btrfs_key *location, u8 type);
1060 int btrfs_lookup_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
1061                           *root, struct btrfs_path *path, u64 dir, char *name,
1062                           int name_len, int mod);
1063 int btrfs_match_dir_item_name(struct btrfs_root *root, struct btrfs_path *path,
1064                               char *name, int name_len);
1065 int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
1066                              struct btrfs_root *fs_root,
1067                              u64 dirid, u64 *objectid);
1068 int btrfs_insert_inode(struct btrfs_trans_handle *trans, struct btrfs_root
1069                        *root, u64 objectid, struct btrfs_inode_item
1070                        *inode_item);
1071 int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root
1072                        *root, struct btrfs_path *path, u64 objectid, int mod);
1073 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1074                                     struct btrfs_root *root);
1075 int btrfs_free_block_groups(struct btrfs_fs_info *info);
1076 int btrfs_read_block_groups(struct btrfs_root *root);
1077 int btrfs_insert_block_group(struct btrfs_trans_handle *trans,
1078                              struct btrfs_root *root,
1079                              struct btrfs_key *key,
1080                              struct btrfs_block_group_item *bi);
1081 #endif