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