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