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