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