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