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