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