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