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