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