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