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