Add fallocate support v2
[platform/upstream/btrfs-progs.git] / ctree.h
1 /*
2  * Copyright (C) 2007 Oracle.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License v2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public
14  * License along with this program; if not, write to the
15  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16  * Boston, MA 021110-1307, USA.
17  */
18
19 #ifndef __BTRFS__
20 #define __BTRFS__
21
22 #include "list.h"
23 #include "kerncompat.h"
24 #include "radix-tree.h"
25 #include "extent-cache.h"
26 #include "extent_io.h"
27
28 struct btrfs_root;
29 struct btrfs_trans_handle;
30 #define BTRFS_MAGIC "_BDRfS_M"
31
32 #define BTRFS_MAX_LEVEL 8
33
34 /* holds pointers to all of the tree roots */
35 #define BTRFS_ROOT_TREE_OBJECTID 1ULL
36
37 /* stores information about which extents are in use, and reference counts */
38 #define BTRFS_EXTENT_TREE_OBJECTID 2ULL
39
40 /*
41  * chunk tree stores translations from logical -> physical block numbering
42  * the super block points to the chunk tree
43  */
44 #define BTRFS_CHUNK_TREE_OBJECTID 3ULL
45
46 /*
47  * stores information about which areas of a given device are in use.
48  * one per device.  The tree of tree roots points to the device tree
49  */
50 #define BTRFS_DEV_TREE_OBJECTID 4ULL
51
52 /* one per subvolume, storing files and directories */
53 #define BTRFS_FS_TREE_OBJECTID 5ULL
54
55 /* directory objectid inside the root tree */
56 #define BTRFS_ROOT_TREE_DIR_OBJECTID 6ULL
57
58 /* oprhan objectid for tracking unlinked/truncated files */
59 #define BTRFS_ORPHAN_OBJECTID -5ULL
60
61 /* does write ahead logging to speed up fsyncs */
62 #define BTRFS_TREE_LOG_OBJECTID -6ULL
63 #define BTRFS_TREE_LOG_FIXUP_OBJECTID -7ULL
64
65 /* space balancing */
66 #define BTRFS_TREE_RELOC_OBJECTID -8ULL
67 #define BTRFS_DATA_RELOC_TREE_OBJECTID -9ULL
68
69 /* dummy objectid represents multiple objectids */
70 #define BTRFS_MULTIPLE_OBJECTIDS -255ULL
71
72 /*
73  * All files have objectids in this range.
74  */
75 #define BTRFS_FIRST_FREE_OBJECTID 256ULL
76 #define BTRFS_LAST_FREE_OBJECTID -256ULL
77 #define BTRFS_FIRST_CHUNK_TREE_OBJECTID 256ULL
78
79
80
81 /*
82  * the device items go into the chunk tree.  The key is in the form
83  * [ 1 BTRFS_DEV_ITEM_KEY device_id ]
84  */
85 #define BTRFS_DEV_ITEMS_OBJECTID 1ULL
86
87 /*
88  * we can actually store much bigger names, but lets not confuse the rest
89  * of linux
90  */
91 #define BTRFS_NAME_LEN 255
92
93 /* 32 bytes in various csum fields */
94 #define BTRFS_CSUM_SIZE 32
95 /* four bytes for CRC32 */
96 #define BTRFS_CRC32_SIZE 4
97 #define BTRFS_EMPTY_DIR_SIZE 0
98
99 #define BTRFS_FT_UNKNOWN        0
100 #define BTRFS_FT_REG_FILE       1
101 #define BTRFS_FT_DIR            2
102 #define BTRFS_FT_CHRDEV         3
103 #define BTRFS_FT_BLKDEV         4
104 #define BTRFS_FT_FIFO           5
105 #define BTRFS_FT_SOCK           6
106 #define BTRFS_FT_SYMLINK        7
107 #define BTRFS_FT_XATTR          8
108 #define BTRFS_FT_MAX            9
109
110 /*
111  * the key defines the order in the tree, and so it also defines (optimal)
112  * block layout.  objectid corresonds to the inode number.  The flags
113  * tells us things about the object, and is a kind of stream selector.
114  * so for a given inode, keys with flags of 1 might refer to the inode
115  * data, flags of 2 may point to file data in the btree and flags == 3
116  * may point to extents.
117  *
118  * offset is the starting byte offset for this key in the stream.
119  *
120  * btrfs_disk_key is in disk byte order.  struct btrfs_key is always
121  * in cpu native order.  Otherwise they are identical and their sizes
122  * should be the same (ie both packed)
123  */
124 struct btrfs_disk_key {
125         __le64 objectid;
126         u8 type;
127         __le64 offset;
128 } __attribute__ ((__packed__));
129
130 struct btrfs_key {
131         u64 objectid;
132         u8 type;
133         u64 offset;
134 } __attribute__ ((__packed__));
135
136 struct btrfs_mapping_tree {
137         struct cache_tree cache_tree;
138 };
139
140 #define BTRFS_UUID_SIZE 16
141 struct btrfs_dev_item {
142         /* the internal btrfs device id */
143         __le64 devid;
144
145         /* size of the device */
146         __le64 total_bytes;
147
148         /* bytes used */
149         __le64 bytes_used;
150
151         /* optimal io alignment for this device */
152         __le32 io_align;
153
154         /* optimal io width for this device */
155         __le32 io_width;
156
157         /* minimal io size for this device */
158         __le32 sector_size;
159
160         /* type and info about this device */
161         __le64 type;
162
163         /* grouping information for allocation decisions */
164         __le32 dev_group;
165
166         /* seek speed 0-100 where 100 is fastest */
167         u8 seek_speed;
168
169         /* bandwidth 0-100 where 100 is fastest */
170         u8 bandwidth;
171
172         /* btrfs generated uuid for this device */
173         u8 uuid[BTRFS_UUID_SIZE];
174 } __attribute__ ((__packed__));
175
176 struct btrfs_stripe {
177         __le64 devid;
178         __le64 offset;
179         u8 dev_uuid[BTRFS_UUID_SIZE];
180 } __attribute__ ((__packed__));
181
182 struct btrfs_chunk {
183         /* size of this chunk in bytes */
184         __le64 length;
185
186         /* objectid of the root referencing this chunk */
187         __le64 owner;
188
189         __le64 stripe_len;
190         __le64 type;
191
192         /* optimal io alignment for this chunk */
193         __le32 io_align;
194
195         /* optimal io width for this chunk */
196         __le32 io_width;
197
198         /* minimal io size for this chunk */
199         __le32 sector_size;
200
201         /* 2^16 stripes is quite a lot, a second limit is the size of a single
202          * item in the btree
203          */
204         __le16 num_stripes;
205
206         /* sub stripes only matter for raid10 */
207         __le16 sub_stripes;
208         struct btrfs_stripe stripe;
209         /* additional stripes go here */
210 } __attribute__ ((__packed__));
211
212 static inline unsigned long btrfs_chunk_item_size(int num_stripes)
213 {
214         BUG_ON(num_stripes == 0);
215         return sizeof(struct btrfs_chunk) +
216                 sizeof(struct btrfs_stripe) * (num_stripes - 1);
217 }
218
219 #define BTRFS_FSID_SIZE 16
220 #define BTRFS_HEADER_FLAG_WRITTEN (1 << 0)
221
222 /*
223  * every tree block (leaf or node) starts with this header.
224  */
225 struct btrfs_header {
226         /* these first four must match the super block */
227         u8 csum[BTRFS_CSUM_SIZE];
228         u8 fsid[BTRFS_FSID_SIZE]; /* FS specific uuid */
229         __le64 bytenr; /* which block this node is supposed to live in */
230         __le64 flags;
231
232         /* allowed to be different from the super from here on down */
233         u8 chunk_tree_uuid[BTRFS_UUID_SIZE];
234         __le64 generation;
235         __le64 owner;
236         __le32 nritems;
237         u8 level;
238 } __attribute__ ((__packed__));
239
240 #define BTRFS_NODEPTRS_PER_BLOCK(r) (((r)->nodesize - \
241                                 sizeof(struct btrfs_header)) / \
242                                 sizeof(struct btrfs_key_ptr))
243 #define __BTRFS_LEAF_DATA_SIZE(bs) ((bs) - sizeof(struct btrfs_header))
244 #define BTRFS_LEAF_DATA_SIZE(r) (__BTRFS_LEAF_DATA_SIZE(r->leafsize))
245 #define BTRFS_MAX_INLINE_DATA_SIZE(r) (BTRFS_LEAF_DATA_SIZE(r) - \
246                                         sizeof(struct btrfs_item) - \
247                                         sizeof(struct btrfs_file_extent_item))
248
249 /*
250  * this is a very generous portion of the super block, giving us
251  * room to translate 14 chunks with 3 stripes each.
252  */
253 #define BTRFS_SYSTEM_CHUNK_ARRAY_SIZE 2048
254 #define BTRFS_LABEL_SIZE 256
255
256 /*
257  * the super block basically lists the main trees of the FS
258  * it currently lacks any block count etc etc
259  */
260 struct btrfs_super_block {
261         u8 csum[BTRFS_CSUM_SIZE];
262         /* the first 3 fields must match struct btrfs_header */
263         u8 fsid[BTRFS_FSID_SIZE];    /* FS specific uuid */
264         __le64 bytenr; /* this block number */
265         __le64 flags;
266
267         /* allowed to be different from the btrfs_header from here own down */
268         __le64 magic;
269         __le64 generation;
270         __le64 root;
271         __le64 chunk_root;
272         __le64 log_root;
273         __le64 total_bytes;
274         __le64 bytes_used;
275         __le64 root_dir_objectid;
276         __le64 num_devices;
277         __le32 sectorsize;
278         __le32 nodesize;
279         __le32 leafsize;
280         __le32 stripesize;
281         __le32 sys_chunk_array_size;
282         __le64 chunk_root_generation;
283         u8 root_level;
284         u8 chunk_root_level;
285         u8 log_root_level;
286         struct btrfs_dev_item dev_item;
287         char label[BTRFS_LABEL_SIZE];
288         u8 sys_chunk_array[BTRFS_SYSTEM_CHUNK_ARRAY_SIZE];
289 } __attribute__ ((__packed__));
290
291 /*
292  * A leaf is full of items. offset and size tell us where to find
293  * the item in the leaf (relative to the start of the data area)
294  */
295 struct btrfs_item {
296         struct btrfs_disk_key key;
297         __le32 offset;
298         __le32 size;
299 } __attribute__ ((__packed__));
300
301 /*
302  * leaves have an item area and a data area:
303  * [item0, item1....itemN] [free space] [dataN...data1, data0]
304  *
305  * The data is separate from the items to get the keys closer together
306  * during searches.
307  */
308 struct btrfs_leaf {
309         struct btrfs_header header;
310         struct btrfs_item items[];
311 } __attribute__ ((__packed__));
312
313 /*
314  * all non-leaf blocks are nodes, they hold only keys and pointers to
315  * other blocks
316  */
317 struct btrfs_key_ptr {
318         struct btrfs_disk_key key;
319         __le64 blockptr;
320         __le64 generation;
321 } __attribute__ ((__packed__));
322
323 struct btrfs_node {
324         struct btrfs_header header;
325         struct btrfs_key_ptr ptrs[];
326 } __attribute__ ((__packed__));
327
328 /*
329  * btrfs_paths remember the path taken from the root down to the leaf.
330  * level 0 is always the leaf, and nodes[1...BTRFS_MAX_LEVEL] will point
331  * to any other levels that are present.
332  *
333  * The slots array records the index of the item or block pointer
334  * used while walking the tree.
335  */
336 struct btrfs_path {
337         struct extent_buffer *nodes[BTRFS_MAX_LEVEL];
338         int slots[BTRFS_MAX_LEVEL];
339         int reada;
340         int lowest_level;
341 };
342
343 /*
344  * items in the extent btree are used to record the objectid of the
345  * owner of the block and the number of references
346  */
347 struct btrfs_extent_item {
348         __le32 refs;
349 } __attribute__ ((__packed__));
350
351 struct btrfs_extent_ref {
352         __le64 root;
353         __le64 generation;
354         __le64 objectid;
355         __le32 num_refs;
356 } __attribute__ ((__packed__));
357
358 /* dev extents record free space on individual devices.  The owner
359  * field points back to the chunk allocation mapping tree that allocated
360  * the extent.  The chunk tree uuid field is a way to double check the owner
361  */
362 struct btrfs_dev_extent {
363         __le64 chunk_tree;
364         __le64 chunk_objectid;
365         __le64 chunk_offset;
366         __le64 length;
367         u8 chunk_tree_uuid[BTRFS_UUID_SIZE];
368 } __attribute__ ((__packed__));
369
370 struct btrfs_inode_ref {
371         __le64 index;
372         __le16 name_len;
373         /* name goes here */
374 } __attribute__ ((__packed__));
375
376 struct btrfs_timespec {
377         __le64 sec;
378         __le32 nsec;
379 } __attribute__ ((__packed__));
380
381 typedef enum {
382         BTRFS_COMPRESS_NONE = 0,
383         BTRFS_COMPRESS_ZLIB = 1,
384         BTRFS_COMPRESS_LAST = 2,
385 } btrfs_compression_type;
386
387 /* we don't understand any encryption methods right now */
388 typedef enum {
389         BTRFS_ENCRYPTION_NONE = 0,
390         BTRFS_ENCRYPTION_LAST = 1,
391 } btrfs_encryption_type;
392
393 struct btrfs_inode_item {
394         /* nfs style generation number */
395         __le64 generation;
396         /* transid that last touched this inode */
397         __le64 transid;
398         __le64 size;
399         __le64 nbytes;
400         __le64 block_group;
401         __le32 nlink;
402         __le32 uid;
403         __le32 gid;
404         __le32 mode;
405         __le64 rdev;
406         __le16 flags;
407         __le16 compat_flags;
408
409         struct btrfs_timespec atime;
410         struct btrfs_timespec ctime;
411         struct btrfs_timespec mtime;
412         struct btrfs_timespec otime;
413 } __attribute__ ((__packed__));
414
415 struct btrfs_dir_item {
416         struct btrfs_disk_key location;
417         __le64 transid;
418         __le16 data_len;
419         __le16 name_len;
420         u8 type;
421 } __attribute__ ((__packed__));
422
423 struct btrfs_root_item {
424         struct btrfs_inode_item inode;
425         __le64 generation;
426         __le64 root_dirid;
427         __le64 bytenr;
428         __le64 byte_limit;
429         __le64 bytes_used;
430         __le64 last_snapshot;
431         __le32 flags;
432         __le32 refs;
433         struct btrfs_disk_key drop_progress;
434         u8 drop_level;
435         u8 level;
436 } __attribute__ ((__packed__));
437
438 #define BTRFS_FILE_EXTENT_INLINE 0
439 #define BTRFS_FILE_EXTENT_REG 1
440 #define BTRFS_FILE_EXTENT_PREALLOC 2
441
442 struct btrfs_file_extent_item {
443         /*
444          * transaction id that created this extent
445          */
446         __le64 generation;
447         /*
448          * max number of bytes to hold this extent in ram
449          * when we split a compressed extent we can't know how big
450          * each of the resulting pieces will be.  So, this is
451          * an upper limit on the size of the extent in ram instead of
452          * an exact limit.
453          */
454         __le64 ram_bytes;
455
456         /*
457          * 32 bits for the various ways we might encode the data,
458          * including compression and encryption.  If any of these
459          * are set to something a given disk format doesn't understand
460          * it is treated like an incompat flag for reading and writing,
461          * but not for stat.
462          */
463         u8 compression;
464         u8 encryption;
465         __le16 other_encoding; /* spare for later use */
466
467         /* are we inline data or a real extent? */
468         u8 type;
469
470         /*
471          * disk space consumed by the extent, checksum blocks are included
472          * in these numbers
473          */
474         __le64 disk_bytenr;
475         __le64 disk_num_bytes;
476         /*
477          * the logical offset in file blocks (no csums)
478          * this extent record is for.  This allows a file extent to point
479          * into the middle of an existing extent on disk, sharing it
480          * between two snapshots (useful if some bytes in the middle of the
481          * extent have changed
482          */
483         __le64 offset;
484         /*
485          * the logical number of file blocks (no csums included)
486          */
487         __le64 num_bytes;
488
489 } __attribute__ ((__packed__));
490
491 struct btrfs_csum_item {
492         u8 csum;
493 } __attribute__ ((__packed__));
494
495 /* tag for the radix tree of block groups in ram */
496 #define BTRFS_BLOCK_GROUP_DATA     (1 << 0)
497 #define BTRFS_BLOCK_GROUP_SYSTEM   (1 << 1)
498 #define BTRFS_BLOCK_GROUP_METADATA (1 << 2)
499 #define BTRFS_BLOCK_GROUP_RAID0    (1 << 3)
500 #define BTRFS_BLOCK_GROUP_RAID1    (1 << 4)
501 #define BTRFS_BLOCK_GROUP_DUP      (1 << 5)
502 #define BTRFS_BLOCK_GROUP_RAID10   (1 << 6)
503
504 struct btrfs_block_group_item {
505         __le64 used;
506         __le64 chunk_objectid;
507         __le64 flags;
508 } __attribute__ ((__packed__));
509
510 struct btrfs_space_info {
511         u64 flags;
512         u64 total_bytes;
513         u64 bytes_used;
514         u64 bytes_pinned;
515         int full;
516         struct list_head list;
517 };
518
519 struct btrfs_block_group_cache {
520         struct cache_extent cache;
521         struct btrfs_key key;
522         struct btrfs_block_group_item item;
523         struct btrfs_space_info *space_info;
524         u64 pinned;
525         u64 flags;
526         int cached;
527 };
528
529 struct btrfs_extent_ops {
530        int (*alloc_extent)(struct btrfs_root *root, u64 num_bytes,
531                            u64 hint_byte, struct btrfs_key *ins);
532        int (*free_extent)(struct btrfs_root *root, u64 bytenr,
533                           u64 num_bytes);
534 };
535
536 struct btrfs_device;
537 struct btrfs_fs_devices;
538 struct btrfs_fs_info {
539         u8 fsid[BTRFS_FSID_SIZE];
540         u8 chunk_tree_uuid[BTRFS_UUID_SIZE];
541         struct btrfs_root *fs_root;
542         struct btrfs_root *extent_root;
543         struct btrfs_root *tree_root;
544         struct btrfs_root *chunk_root;
545         struct btrfs_root *dev_root;
546
547         /* the log root tree is a directory of all the other log roots */
548         struct btrfs_root *log_root_tree;
549
550         struct extent_io_tree extent_cache;
551         struct extent_io_tree free_space_cache;
552         struct extent_io_tree block_group_cache;
553         struct extent_io_tree pinned_extents;
554         struct extent_io_tree pending_del;
555         struct extent_io_tree extent_ins;
556
557         /* logical->physical extent mapping */
558         struct btrfs_mapping_tree mapping_tree;
559
560         u64 generation;
561         u64 last_trans_committed;
562
563         u64 avail_data_alloc_bits;
564         u64 avail_metadata_alloc_bits;
565         u64 avail_system_alloc_bits;
566         u64 data_alloc_profile;
567         u64 metadata_alloc_profile;
568         u64 system_alloc_profile;
569         u64 alloc_start;
570
571         struct btrfs_trans_handle *running_transaction;
572         struct btrfs_super_block super_copy;
573         struct extent_buffer *sb_buffer;
574         struct mutex fs_mutex;
575         u64 total_pinned;
576
577         struct btrfs_extent_ops *extent_ops;
578         struct list_head dirty_cowonly_roots;
579
580         struct btrfs_fs_devices *fs_devices;
581         struct list_head space_info;
582         int system_allocs;
583         int readonly;
584 };
585
586 /*
587  * in ram representation of the tree.  extent_root is used for all allocations
588  * and for the extent tree extent_root root.
589  */
590 struct btrfs_root {
591         struct extent_buffer *node;
592         struct extent_buffer *commit_root;
593         struct btrfs_root_item root_item;
594         struct btrfs_key root_key;
595         struct btrfs_fs_info *fs_info;
596         u64 objectid;
597         u64 last_trans;
598
599         /* data allocations are done in sectorsize units */
600         u32 sectorsize;
601
602         /* node allocations are done in nodesize units */
603         u32 nodesize;
604
605         /* leaf allocations are done in leafsize units */
606         u32 leafsize;
607
608         /* leaf allocations are done in leafsize units */
609         u32 stripesize;
610
611         int ref_cows;
612         int track_dirty;
613
614
615         u32 type;
616         u64 highest_inode;
617         u64 last_inode_alloc;
618
619         /* the dirty list is only used by non-reference counted roots */
620         struct list_head dirty_list;
621 };
622
623 /*
624  * inode items have the data typically returned from stat and store other
625  * info about object characteristics.  There is one for every file and dir in
626  * the FS
627  */
628 #define BTRFS_INODE_ITEM_KEY            1
629 #define BTRFS_INODE_REF_KEY             2
630 #define BTRFS_XATTR_ITEM_KEY            8
631 #define BTRFS_ORPHAN_ITEM_KEY           9
632
633 /* reserve 3-15 close to the inode for later flexibility */
634
635 /*
636  * dir items are the name -> inode pointers in a directory.  There is one
637  * for every name in a directory.
638  */
639 #define BTRFS_DIR_ITEM_KEY      16
640 #define BTRFS_DIR_INDEX_KEY     17
641 /*
642  * extent data is for file data
643  */
644 #define BTRFS_EXTENT_DATA_KEY   18
645 /*
646  * csum items have the checksums for data in the extents
647  */
648 #define BTRFS_CSUM_ITEM_KEY     19
649
650 /* reserve 20-31 for other file stuff */
651
652 /*
653  * root items point to tree roots.  There are typically in the root
654  * tree used by the super block to find all the other trees
655  */
656 #define BTRFS_ROOT_ITEM_KEY     32
657 /*
658  * extent items are in the extent map tree.  These record which blocks
659  * are used, and how many references there are to each block
660  */
661 #define BTRFS_EXTENT_ITEM_KEY   33
662 #define BTRFS_EXTENT_REF_KEY    34
663
664 /*
665  * block groups give us hints into the extent allocation trees.  Which
666  * blocks are free etc etc
667  */
668 #define BTRFS_BLOCK_GROUP_ITEM_KEY 50
669
670 #define BTRFS_DEV_EXTENT_KEY    75
671 #define BTRFS_DEV_ITEM_KEY      76
672 #define BTRFS_CHUNK_ITEM_KEY    77
673
674 /*
675  * string items are for debugging.  They just store a short string of
676  * data in the FS
677  */
678 #define BTRFS_STRING_ITEM_KEY   253
679 /*
680  * Inode flags
681  */
682 #define BTRFS_INODE_NODATASUM           (1 << 0)
683 #define BTRFS_INODE_NODATACOW           (1 << 1)
684 #define BTRFS_INODE_READONLY            (1 << 2)
685
686 #define read_eb_member(eb, ptr, type, member, result) (                 \
687         read_extent_buffer(eb, (char *)(result),                        \
688                            ((unsigned long)(ptr)) +                     \
689                             offsetof(type, member),                     \
690                            sizeof(((type *)0)->member)))
691
692 #define write_eb_member(eb, ptr, type, member, result) (                \
693         write_extent_buffer(eb, (char *)(result),                       \
694                            ((unsigned long)(ptr)) +                     \
695                             offsetof(type, member),                     \
696                            sizeof(((type *)0)->member)))
697
698 #define BTRFS_SETGET_HEADER_FUNCS(name, type, member, bits)             \
699 static inline u##bits btrfs_##name(struct extent_buffer *eb)            \
700 {                                                                       \
701         struct btrfs_header *h = (struct btrfs_header *)eb->data;       \
702         return le##bits##_to_cpu(h->member);                            \
703 }                                                                       \
704 static inline void btrfs_set_##name(struct extent_buffer *eb,           \
705                                     u##bits val)                        \
706 {                                                                       \
707         struct btrfs_header *h = (struct btrfs_header *)eb->data;       \
708         h->member = cpu_to_le##bits(val);                               \
709 }
710
711 #define BTRFS_SETGET_FUNCS(name, type, member, bits)                    \
712 static inline u##bits btrfs_##name(struct extent_buffer *eb,            \
713                                    type *s)                             \
714 {                                                                       \
715         unsigned long offset = (unsigned long)s;                        \
716         type *p = (type *) (eb->data + offset);                         \
717         return le##bits##_to_cpu(p->member);                            \
718 }                                                                       \
719 static inline void btrfs_set_##name(struct extent_buffer *eb,           \
720                                     type *s, u##bits val)               \
721 {                                                                       \
722         unsigned long offset = (unsigned long)s;                        \
723         type *p = (type *) (eb->data + offset);                         \
724         p->member = cpu_to_le##bits(val);                               \
725 }
726
727 #define BTRFS_SETGET_STACK_FUNCS(name, type, member, bits)              \
728 static inline u##bits btrfs_##name(type *s)                             \
729 {                                                                       \
730         return le##bits##_to_cpu(s->member);                            \
731 }                                                                       \
732 static inline void btrfs_set_##name(type *s, u##bits val)               \
733 {                                                                       \
734         s->member = cpu_to_le##bits(val);                               \
735 }
736
737 BTRFS_SETGET_FUNCS(device_type, struct btrfs_dev_item, type, 64);
738 BTRFS_SETGET_FUNCS(device_total_bytes, struct btrfs_dev_item, total_bytes, 64);
739 BTRFS_SETGET_FUNCS(device_bytes_used, struct btrfs_dev_item, bytes_used, 64);
740 BTRFS_SETGET_FUNCS(device_io_align, struct btrfs_dev_item, io_align, 32);
741 BTRFS_SETGET_FUNCS(device_io_width, struct btrfs_dev_item, io_width, 32);
742 BTRFS_SETGET_FUNCS(device_sector_size, struct btrfs_dev_item, sector_size, 32);
743 BTRFS_SETGET_FUNCS(device_id, struct btrfs_dev_item, devid, 64);
744 BTRFS_SETGET_FUNCS(device_group, struct btrfs_dev_item, dev_group, 32);
745 BTRFS_SETGET_FUNCS(device_seek_speed, struct btrfs_dev_item, seek_speed, 8);
746 BTRFS_SETGET_FUNCS(device_bandwidth, struct btrfs_dev_item, bandwidth, 8);
747
748 BTRFS_SETGET_STACK_FUNCS(stack_device_type, struct btrfs_dev_item, type, 64);
749 BTRFS_SETGET_STACK_FUNCS(stack_device_total_bytes, struct btrfs_dev_item,
750                          total_bytes, 64);
751 BTRFS_SETGET_STACK_FUNCS(stack_device_bytes_used, struct btrfs_dev_item,
752                          bytes_used, 64);
753 BTRFS_SETGET_STACK_FUNCS(stack_device_io_align, struct btrfs_dev_item,
754                          io_align, 32);
755 BTRFS_SETGET_STACK_FUNCS(stack_device_io_width, struct btrfs_dev_item,
756                          io_width, 32);
757 BTRFS_SETGET_STACK_FUNCS(stack_device_sector_size, struct btrfs_dev_item,
758                          sector_size, 32);
759 BTRFS_SETGET_STACK_FUNCS(stack_device_id, struct btrfs_dev_item, devid, 64);
760 BTRFS_SETGET_STACK_FUNCS(stack_device_group, struct btrfs_dev_item,
761                          dev_group, 32);
762 BTRFS_SETGET_STACK_FUNCS(stack_device_seek_speed, struct btrfs_dev_item,
763                          seek_speed, 8);
764 BTRFS_SETGET_STACK_FUNCS(stack_device_bandwidth, struct btrfs_dev_item,
765                          bandwidth, 8);
766
767 static inline char *btrfs_device_uuid(struct btrfs_dev_item *d)
768 {
769         return (char *)d + offsetof(struct btrfs_dev_item, uuid);
770 }
771
772 BTRFS_SETGET_FUNCS(chunk_length, struct btrfs_chunk, length, 64);
773 BTRFS_SETGET_FUNCS(chunk_owner, struct btrfs_chunk, owner, 64);
774 BTRFS_SETGET_FUNCS(chunk_stripe_len, struct btrfs_chunk, stripe_len, 64);
775 BTRFS_SETGET_FUNCS(chunk_io_align, struct btrfs_chunk, io_align, 32);
776 BTRFS_SETGET_FUNCS(chunk_io_width, struct btrfs_chunk, io_width, 32);
777 BTRFS_SETGET_FUNCS(chunk_sector_size, struct btrfs_chunk, sector_size, 32);
778 BTRFS_SETGET_FUNCS(chunk_type, struct btrfs_chunk, type, 64);
779 BTRFS_SETGET_FUNCS(chunk_num_stripes, struct btrfs_chunk, num_stripes, 16);
780 BTRFS_SETGET_FUNCS(chunk_sub_stripes, struct btrfs_chunk, sub_stripes, 16);
781 BTRFS_SETGET_FUNCS(stripe_devid, struct btrfs_stripe, devid, 64);
782 BTRFS_SETGET_FUNCS(stripe_offset, struct btrfs_stripe, offset, 64);
783
784 static inline char *btrfs_stripe_dev_uuid(struct btrfs_stripe *s)
785 {
786         return (char *)s + offsetof(struct btrfs_stripe, dev_uuid);
787 }
788
789 BTRFS_SETGET_STACK_FUNCS(stack_chunk_length, struct btrfs_chunk, length, 64);
790 BTRFS_SETGET_STACK_FUNCS(stack_chunk_owner, struct btrfs_chunk, owner, 64);
791 BTRFS_SETGET_STACK_FUNCS(stack_chunk_stripe_len, struct btrfs_chunk,
792                          stripe_len, 64);
793 BTRFS_SETGET_STACK_FUNCS(stack_chunk_io_align, struct btrfs_chunk,
794                          io_align, 32);
795 BTRFS_SETGET_STACK_FUNCS(stack_chunk_io_width, struct btrfs_chunk,
796                          io_width, 32);
797 BTRFS_SETGET_STACK_FUNCS(stack_chunk_sector_size, struct btrfs_chunk,
798                          sector_size, 32);
799 BTRFS_SETGET_STACK_FUNCS(stack_chunk_type, struct btrfs_chunk, type, 64);
800 BTRFS_SETGET_STACK_FUNCS(stack_chunk_num_stripes, struct btrfs_chunk,
801                          num_stripes, 16);
802 BTRFS_SETGET_STACK_FUNCS(stack_chunk_sub_stripes, struct btrfs_chunk,
803                          sub_stripes, 16);
804 BTRFS_SETGET_STACK_FUNCS(stack_stripe_devid, struct btrfs_stripe, devid, 64);
805 BTRFS_SETGET_STACK_FUNCS(stack_stripe_offset, struct btrfs_stripe, offset, 64);
806
807 static inline struct btrfs_stripe *btrfs_stripe_nr(struct btrfs_chunk *c,
808                                                    int nr)
809 {
810         unsigned long offset = (unsigned long)c;
811         offset += offsetof(struct btrfs_chunk, stripe);
812         offset += nr * sizeof(struct btrfs_stripe);
813         return (struct btrfs_stripe *)offset;
814 }
815
816 static inline char *btrfs_stripe_dev_uuid_nr(struct btrfs_chunk *c, int nr)
817 {
818         return btrfs_stripe_dev_uuid(btrfs_stripe_nr(c, nr));
819 }
820
821 static inline u64 btrfs_stripe_offset_nr(struct extent_buffer *eb,
822                                          struct btrfs_chunk *c, int nr)
823 {
824         return btrfs_stripe_offset(eb, btrfs_stripe_nr(c, nr));
825 }
826
827 static inline void btrfs_set_stripe_offset_nr(struct extent_buffer *eb,
828                                              struct btrfs_chunk *c, int nr,
829                                              u64 val)
830 {
831         btrfs_set_stripe_offset(eb, btrfs_stripe_nr(c, nr), val);
832 }
833
834 static inline u64 btrfs_stripe_devid_nr(struct extent_buffer *eb,
835                                          struct btrfs_chunk *c, int nr)
836 {
837         return btrfs_stripe_devid(eb, btrfs_stripe_nr(c, nr));
838 }
839
840 static inline void btrfs_set_stripe_devid_nr(struct extent_buffer *eb,
841                                              struct btrfs_chunk *c, int nr,
842                                              u64 val)
843 {
844         btrfs_set_stripe_devid(eb, btrfs_stripe_nr(c, nr), val);
845 }
846
847 /* struct btrfs_block_group_item */
848 BTRFS_SETGET_STACK_FUNCS(block_group_used, struct btrfs_block_group_item,
849                          used, 64);
850 BTRFS_SETGET_FUNCS(disk_block_group_used, struct btrfs_block_group_item,
851                          used, 64);
852 BTRFS_SETGET_STACK_FUNCS(block_group_chunk_objectid,
853                         struct btrfs_block_group_item, chunk_objectid, 64);
854
855 BTRFS_SETGET_FUNCS(disk_block_group_chunk_objectid,
856                    struct btrfs_block_group_item, chunk_objectid, 64);
857 BTRFS_SETGET_FUNCS(disk_block_group_flags,
858                    struct btrfs_block_group_item, flags, 64);
859 BTRFS_SETGET_STACK_FUNCS(block_group_flags,
860                         struct btrfs_block_group_item, flags, 64);
861
862 /* struct btrfs_inode_ref */
863 BTRFS_SETGET_FUNCS(inode_ref_name_len, struct btrfs_inode_ref, name_len, 16);
864 BTRFS_SETGET_FUNCS(inode_ref_index, struct btrfs_inode_ref, index, 64);
865
866 /* struct btrfs_inode_item */
867 BTRFS_SETGET_FUNCS(inode_generation, struct btrfs_inode_item, generation, 64);
868 BTRFS_SETGET_FUNCS(inode_transid, struct btrfs_inode_item, transid, 64);
869 BTRFS_SETGET_FUNCS(inode_size, struct btrfs_inode_item, size, 64);
870 BTRFS_SETGET_FUNCS(inode_nbytes, struct btrfs_inode_item, nbytes, 64);
871 BTRFS_SETGET_FUNCS(inode_block_group, struct btrfs_inode_item, block_group, 64);
872 BTRFS_SETGET_FUNCS(inode_nlink, struct btrfs_inode_item, nlink, 32);
873 BTRFS_SETGET_FUNCS(inode_uid, struct btrfs_inode_item, uid, 32);
874 BTRFS_SETGET_FUNCS(inode_gid, struct btrfs_inode_item, gid, 32);
875 BTRFS_SETGET_FUNCS(inode_mode, struct btrfs_inode_item, mode, 32);
876 BTRFS_SETGET_FUNCS(inode_rdev, struct btrfs_inode_item, rdev, 64);
877 BTRFS_SETGET_FUNCS(inode_flags, struct btrfs_inode_item, flags, 16);
878 BTRFS_SETGET_FUNCS(inode_compat_flags, struct btrfs_inode_item,
879                    compat_flags, 16);
880
881 BTRFS_SETGET_STACK_FUNCS(stack_inode_generation,
882                          struct btrfs_inode_item, generation, 64);
883 BTRFS_SETGET_STACK_FUNCS(stack_inode_size,
884                          struct btrfs_inode_item, size, 64);
885 BTRFS_SETGET_STACK_FUNCS(stack_inode_nbytes,
886                          struct btrfs_inode_item, nbytes, 64);
887 BTRFS_SETGET_STACK_FUNCS(stack_inode_block_group,
888                          struct btrfs_inode_item, block_group, 64);
889 BTRFS_SETGET_STACK_FUNCS(stack_inode_nlink,
890                          struct btrfs_inode_item, nlink, 32);
891 BTRFS_SETGET_STACK_FUNCS(stack_inode_uid,
892                          struct btrfs_inode_item, uid, 32);
893 BTRFS_SETGET_STACK_FUNCS(stack_inode_gid,
894                          struct btrfs_inode_item, gid, 32);
895 BTRFS_SETGET_STACK_FUNCS(stack_inode_mode,
896                          struct btrfs_inode_item, mode, 32);
897 BTRFS_SETGET_STACK_FUNCS(stack_inode_rdev,
898                          struct btrfs_inode_item, rdev, 64);
899 BTRFS_SETGET_STACK_FUNCS(stack_inode_flags,
900                          struct btrfs_inode_item, flags, 16);
901 BTRFS_SETGET_STACK_FUNCS(stack_inode_compat_flags,
902                          struct btrfs_inode_item, compat_flags, 16);
903
904 static inline struct btrfs_timespec *
905 btrfs_inode_atime(struct btrfs_inode_item *inode_item)
906 {
907         unsigned long ptr = (unsigned long)inode_item;
908         ptr += offsetof(struct btrfs_inode_item, atime);
909         return (struct btrfs_timespec *)ptr;
910 }
911
912 static inline struct btrfs_timespec *
913 btrfs_inode_mtime(struct btrfs_inode_item *inode_item)
914 {
915         unsigned long ptr = (unsigned long)inode_item;
916         ptr += offsetof(struct btrfs_inode_item, mtime);
917         return (struct btrfs_timespec *)ptr;
918 }
919
920 static inline struct btrfs_timespec *
921 btrfs_inode_ctime(struct btrfs_inode_item *inode_item)
922 {
923         unsigned long ptr = (unsigned long)inode_item;
924         ptr += offsetof(struct btrfs_inode_item, ctime);
925         return (struct btrfs_timespec *)ptr;
926 }
927
928 static inline struct btrfs_timespec *
929 btrfs_inode_otime(struct btrfs_inode_item *inode_item)
930 {
931         unsigned long ptr = (unsigned long)inode_item;
932         ptr += offsetof(struct btrfs_inode_item, otime);
933         return (struct btrfs_timespec *)ptr;
934 }
935
936 BTRFS_SETGET_FUNCS(timespec_sec, struct btrfs_timespec, sec, 64);
937 BTRFS_SETGET_FUNCS(timespec_nsec, struct btrfs_timespec, nsec, 32);
938 BTRFS_SETGET_STACK_FUNCS(stack_timespec_sec, struct btrfs_timespec,
939                          sec, 64);
940 BTRFS_SETGET_STACK_FUNCS(stack_timespec_nsec, struct btrfs_timespec,
941                          nsec, 32);
942
943 /* struct btrfs_dev_extent */
944 BTRFS_SETGET_FUNCS(dev_extent_chunk_tree, struct btrfs_dev_extent,
945                    chunk_tree, 64);
946 BTRFS_SETGET_FUNCS(dev_extent_chunk_objectid, struct btrfs_dev_extent,
947                    chunk_objectid, 64);
948 BTRFS_SETGET_FUNCS(dev_extent_chunk_offset, struct btrfs_dev_extent,
949                    chunk_offset, 64);
950 BTRFS_SETGET_FUNCS(dev_extent_length, struct btrfs_dev_extent, length, 64);
951
952 static inline u8 *btrfs_dev_extent_chunk_tree_uuid(struct btrfs_dev_extent *dev)
953 {
954         unsigned long ptr = offsetof(struct btrfs_dev_extent, chunk_tree_uuid);
955         return (u8 *)((unsigned long)dev + ptr);
956 }
957
958 /* struct btrfs_extent_ref */
959 BTRFS_SETGET_FUNCS(ref_root, struct btrfs_extent_ref, root, 64);
960 BTRFS_SETGET_FUNCS(ref_generation, struct btrfs_extent_ref, generation, 64);
961 BTRFS_SETGET_FUNCS(ref_objectid, struct btrfs_extent_ref, objectid, 64);
962 BTRFS_SETGET_FUNCS(ref_num_refs, struct btrfs_extent_ref, num_refs, 32);
963
964 BTRFS_SETGET_STACK_FUNCS(stack_ref_root, struct btrfs_extent_ref, root, 64);
965 BTRFS_SETGET_STACK_FUNCS(stack_ref_generation, struct btrfs_extent_ref,
966                          generation, 64);
967 BTRFS_SETGET_STACK_FUNCS(stack_ref_objectid, struct btrfs_extent_ref,
968                          objectid, 64);
969 BTRFS_SETGET_STACK_FUNCS(stack_ref_num_refs, struct btrfs_extent_ref,
970                          num_refs, 32);
971
972 /* struct btrfs_extent_item */
973 BTRFS_SETGET_FUNCS(extent_refs, struct btrfs_extent_item, refs, 32);
974 BTRFS_SETGET_STACK_FUNCS(stack_extent_refs, struct btrfs_extent_item,
975                          refs, 32);
976
977 /* struct btrfs_node */
978 BTRFS_SETGET_FUNCS(key_blockptr, struct btrfs_key_ptr, blockptr, 64);
979 BTRFS_SETGET_FUNCS(key_generation, struct btrfs_key_ptr, generation, 64);
980
981 static inline u64 btrfs_node_blockptr(struct extent_buffer *eb, int nr)
982 {
983         unsigned long ptr;
984         ptr = offsetof(struct btrfs_node, ptrs) +
985                 sizeof(struct btrfs_key_ptr) * nr;
986         return btrfs_key_blockptr(eb, (struct btrfs_key_ptr *)ptr);
987 }
988
989 static inline void btrfs_set_node_blockptr(struct extent_buffer *eb,
990                                            int nr, u64 val)
991 {
992         unsigned long ptr;
993         ptr = offsetof(struct btrfs_node, ptrs) +
994                 sizeof(struct btrfs_key_ptr) * nr;
995         btrfs_set_key_blockptr(eb, (struct btrfs_key_ptr *)ptr, val);
996 }
997
998 static inline u64 btrfs_node_ptr_generation(struct extent_buffer *eb, int nr)
999 {
1000         unsigned long ptr;
1001         ptr = offsetof(struct btrfs_node, ptrs) +
1002                 sizeof(struct btrfs_key_ptr) * nr;
1003         return btrfs_key_generation(eb, (struct btrfs_key_ptr *)ptr);
1004 }
1005
1006 static inline void btrfs_set_node_ptr_generation(struct extent_buffer *eb,
1007                                                  int nr, u64 val)
1008 {
1009         unsigned long ptr;
1010         ptr = offsetof(struct btrfs_node, ptrs) +
1011                 sizeof(struct btrfs_key_ptr) * nr;
1012         btrfs_set_key_generation(eb, (struct btrfs_key_ptr *)ptr, val);
1013 }
1014
1015 static inline unsigned long btrfs_node_key_ptr_offset(int nr)
1016 {
1017         return offsetof(struct btrfs_node, ptrs) +
1018                 sizeof(struct btrfs_key_ptr) * nr;
1019 }
1020
1021 static inline void btrfs_node_key(struct extent_buffer *eb,
1022                                   struct btrfs_disk_key *disk_key, int nr)
1023 {
1024         unsigned long ptr;
1025         ptr = btrfs_node_key_ptr_offset(nr);
1026         read_eb_member(eb, (struct btrfs_key_ptr *)ptr,
1027                        struct btrfs_key_ptr, key, disk_key);
1028 }
1029
1030 static inline void btrfs_set_node_key(struct extent_buffer *eb,
1031                                       struct btrfs_disk_key *disk_key, int nr)
1032 {
1033         unsigned long ptr;
1034         ptr = btrfs_node_key_ptr_offset(nr);
1035         write_eb_member(eb, (struct btrfs_key_ptr *)ptr,
1036                        struct btrfs_key_ptr, key, disk_key);
1037 }
1038
1039 /* struct btrfs_item */
1040 BTRFS_SETGET_FUNCS(item_offset, struct btrfs_item, offset, 32);
1041 BTRFS_SETGET_FUNCS(item_size, struct btrfs_item, size, 32);
1042
1043 static inline unsigned long btrfs_item_nr_offset(int nr)
1044 {
1045         return offsetof(struct btrfs_leaf, items) +
1046                 sizeof(struct btrfs_item) * nr;
1047 }
1048
1049 static inline struct btrfs_item *btrfs_item_nr(struct extent_buffer *eb,
1050                                                int nr)
1051 {
1052         return (struct btrfs_item *)btrfs_item_nr_offset(nr);
1053 }
1054
1055 static inline u32 btrfs_item_end(struct extent_buffer *eb,
1056                                  struct btrfs_item *item)
1057 {
1058         return btrfs_item_offset(eb, item) + btrfs_item_size(eb, item);
1059 }
1060
1061 static inline u32 btrfs_item_end_nr(struct extent_buffer *eb, int nr)
1062 {
1063         return btrfs_item_end(eb, btrfs_item_nr(eb, nr));
1064 }
1065
1066 static inline u32 btrfs_item_offset_nr(struct extent_buffer *eb, int nr)
1067 {
1068         return btrfs_item_offset(eb, btrfs_item_nr(eb, nr));
1069 }
1070
1071 static inline u32 btrfs_item_size_nr(struct extent_buffer *eb, int nr)
1072 {
1073         return btrfs_item_size(eb, btrfs_item_nr(eb, nr));
1074 }
1075
1076 static inline void btrfs_item_key(struct extent_buffer *eb,
1077                            struct btrfs_disk_key *disk_key, int nr)
1078 {
1079         struct btrfs_item *item = btrfs_item_nr(eb, nr);
1080         read_eb_member(eb, item, struct btrfs_item, key, disk_key);
1081 }
1082
1083 static inline void btrfs_set_item_key(struct extent_buffer *eb,
1084                                struct btrfs_disk_key *disk_key, int nr)
1085 {
1086         struct btrfs_item *item = btrfs_item_nr(eb, nr);
1087         write_eb_member(eb, item, struct btrfs_item, key, disk_key);
1088 }
1089
1090 /* struct btrfs_dir_item */
1091 BTRFS_SETGET_FUNCS(dir_data_len, struct btrfs_dir_item, data_len, 16);
1092 BTRFS_SETGET_FUNCS(dir_type, struct btrfs_dir_item, type, 8);
1093 BTRFS_SETGET_FUNCS(dir_name_len, struct btrfs_dir_item, name_len, 16);
1094 BTRFS_SETGET_FUNCS(dir_transid, struct btrfs_dir_item, transid, 64);
1095
1096 static inline void btrfs_dir_item_key(struct extent_buffer *eb,
1097                                       struct btrfs_dir_item *item,
1098                                       struct btrfs_disk_key *key)
1099 {
1100         read_eb_member(eb, item, struct btrfs_dir_item, location, key);
1101 }
1102
1103 static inline void btrfs_set_dir_item_key(struct extent_buffer *eb,
1104                                           struct btrfs_dir_item *item,
1105                                           struct btrfs_disk_key *key)
1106 {
1107         write_eb_member(eb, item, struct btrfs_dir_item, location, key);
1108 }
1109
1110 /* struct btrfs_disk_key */
1111 BTRFS_SETGET_STACK_FUNCS(disk_key_objectid, struct btrfs_disk_key,
1112                          objectid, 64);
1113 BTRFS_SETGET_STACK_FUNCS(disk_key_offset, struct btrfs_disk_key, offset, 64);
1114 BTRFS_SETGET_STACK_FUNCS(disk_key_type, struct btrfs_disk_key, type, 8);
1115
1116 static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu,
1117                                          struct btrfs_disk_key *disk)
1118 {
1119         cpu->offset = le64_to_cpu(disk->offset);
1120         cpu->type = disk->type;
1121         cpu->objectid = le64_to_cpu(disk->objectid);
1122 }
1123
1124 static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk,
1125                                          struct btrfs_key *cpu)
1126 {
1127         disk->offset = cpu_to_le64(cpu->offset);
1128         disk->type = cpu->type;
1129         disk->objectid = cpu_to_le64(cpu->objectid);
1130 }
1131
1132 static inline void btrfs_node_key_to_cpu(struct extent_buffer *eb,
1133                                   struct btrfs_key *key, int nr)
1134 {
1135         struct btrfs_disk_key disk_key;
1136         btrfs_node_key(eb, &disk_key, nr);
1137         btrfs_disk_key_to_cpu(key, &disk_key);
1138 }
1139
1140 static inline void btrfs_item_key_to_cpu(struct extent_buffer *eb,
1141                                   struct btrfs_key *key, int nr)
1142 {
1143         struct btrfs_disk_key disk_key;
1144         btrfs_item_key(eb, &disk_key, nr);
1145         btrfs_disk_key_to_cpu(key, &disk_key);
1146 }
1147
1148 static inline void btrfs_dir_item_key_to_cpu(struct extent_buffer *eb,
1149                                       struct btrfs_dir_item *item,
1150                                       struct btrfs_key *key)
1151 {
1152         struct btrfs_disk_key disk_key;
1153         btrfs_dir_item_key(eb, item, &disk_key);
1154         btrfs_disk_key_to_cpu(key, &disk_key);
1155 }
1156
1157
1158 static inline u8 btrfs_key_type(struct btrfs_key *key)
1159 {
1160         return key->type;
1161 }
1162
1163 static inline void btrfs_set_key_type(struct btrfs_key *key, u8 val)
1164 {
1165         key->type = val;
1166 }
1167
1168 /* struct btrfs_header */
1169 BTRFS_SETGET_HEADER_FUNCS(header_bytenr, struct btrfs_header, bytenr, 64);
1170 BTRFS_SETGET_HEADER_FUNCS(header_generation, struct btrfs_header,
1171                           generation, 64);
1172 BTRFS_SETGET_HEADER_FUNCS(header_owner, struct btrfs_header, owner, 64);
1173 BTRFS_SETGET_HEADER_FUNCS(header_nritems, struct btrfs_header, nritems, 32);
1174 BTRFS_SETGET_HEADER_FUNCS(header_flags, struct btrfs_header, flags, 64);
1175 BTRFS_SETGET_HEADER_FUNCS(header_level, struct btrfs_header, level, 8);
1176
1177 static inline int btrfs_header_flag(struct extent_buffer *eb, u64 flag)
1178 {
1179         return (btrfs_header_flags(eb) & flag) == flag;
1180 }
1181
1182 static inline int btrfs_set_header_flag(struct extent_buffer *eb, u64 flag)
1183 {
1184         u64 flags = btrfs_header_flags(eb);
1185         btrfs_set_header_flags(eb, flags | flag);
1186         return (flags & flag) == flag;
1187 }
1188
1189 static inline int btrfs_clear_header_flag(struct extent_buffer *eb, u64 flag)
1190 {
1191         u64 flags = btrfs_header_flags(eb);
1192         btrfs_set_header_flags(eb, flags & ~flag);
1193         return (flags & flag) == flag;
1194 }
1195
1196 static inline u8 *btrfs_header_fsid(struct extent_buffer *eb)
1197 {
1198         unsigned long ptr = offsetof(struct btrfs_header, fsid);
1199         return (u8 *)ptr;
1200 }
1201
1202 static inline u8 *btrfs_header_chunk_tree_uuid(struct extent_buffer *eb)
1203 {
1204         unsigned long ptr = offsetof(struct btrfs_header, chunk_tree_uuid);
1205         return (u8 *)ptr;
1206 }
1207
1208 static inline u8 *btrfs_super_fsid(struct extent_buffer *eb)
1209 {
1210         unsigned long ptr = offsetof(struct btrfs_super_block, fsid);
1211         return (u8 *)ptr;
1212 }
1213
1214 static inline u8 *btrfs_header_csum(struct extent_buffer *eb)
1215 {
1216         unsigned long ptr = offsetof(struct btrfs_header, csum);
1217         return (u8 *)ptr;
1218 }
1219
1220 static inline struct btrfs_node *btrfs_buffer_node(struct extent_buffer *eb)
1221 {
1222         return NULL;
1223 }
1224
1225 static inline struct btrfs_leaf *btrfs_buffer_leaf(struct extent_buffer *eb)
1226 {
1227         return NULL;
1228 }
1229
1230 static inline struct btrfs_header *btrfs_buffer_header(struct extent_buffer *eb)
1231 {
1232         return NULL;
1233 }
1234
1235 static inline int btrfs_is_leaf(struct extent_buffer *eb)
1236 {
1237         return (btrfs_header_level(eb) == 0);
1238 }
1239
1240 /* struct btrfs_root_item */
1241 BTRFS_SETGET_FUNCS(disk_root_generation, struct btrfs_root_item,
1242                    generation, 64);
1243 BTRFS_SETGET_FUNCS(disk_root_refs, struct btrfs_root_item, refs, 32);
1244 BTRFS_SETGET_FUNCS(disk_root_bytenr, struct btrfs_root_item, bytenr, 64);
1245 BTRFS_SETGET_FUNCS(disk_root_level, struct btrfs_root_item, level, 8);
1246
1247 BTRFS_SETGET_STACK_FUNCS(root_generation, struct btrfs_root_item,
1248                          generation, 64);
1249 BTRFS_SETGET_STACK_FUNCS(root_bytenr, struct btrfs_root_item, bytenr, 64);
1250 BTRFS_SETGET_STACK_FUNCS(root_level, struct btrfs_root_item, level, 8);
1251 BTRFS_SETGET_STACK_FUNCS(root_dirid, struct btrfs_root_item, root_dirid, 64);
1252 BTRFS_SETGET_STACK_FUNCS(root_refs, struct btrfs_root_item, refs, 32);
1253 BTRFS_SETGET_STACK_FUNCS(root_flags, struct btrfs_root_item, flags, 32);
1254 BTRFS_SETGET_STACK_FUNCS(root_used, struct btrfs_root_item, bytes_used, 64);
1255 BTRFS_SETGET_STACK_FUNCS(root_limit, struct btrfs_root_item, byte_limit, 64);
1256 BTRFS_SETGET_STACK_FUNCS(root_last_snapshot, struct btrfs_root_item,
1257                          last_snapshot, 64);
1258
1259
1260 /* struct btrfs_super_block */
1261 BTRFS_SETGET_STACK_FUNCS(super_bytenr, struct btrfs_super_block, bytenr, 64);
1262 BTRFS_SETGET_STACK_FUNCS(super_generation, struct btrfs_super_block,
1263                          generation, 64);
1264 BTRFS_SETGET_STACK_FUNCS(super_root, struct btrfs_super_block, root, 64);
1265 BTRFS_SETGET_STACK_FUNCS(super_sys_array_size,
1266                          struct btrfs_super_block, sys_chunk_array_size, 32);
1267 BTRFS_SETGET_STACK_FUNCS(super_chunk_root_generation,
1268                          struct btrfs_super_block, chunk_root_generation, 64);
1269 BTRFS_SETGET_STACK_FUNCS(super_root_level, struct btrfs_super_block,
1270                          root_level, 8);
1271 BTRFS_SETGET_STACK_FUNCS(super_chunk_root, struct btrfs_super_block,
1272                          chunk_root, 64);
1273 BTRFS_SETGET_STACK_FUNCS(super_chunk_root_level, struct btrfs_super_block,
1274                          chunk_root_level, 8);
1275 BTRFS_SETGET_STACK_FUNCS(super_log_root, struct btrfs_super_block,
1276                          log_root, 64);
1277 BTRFS_SETGET_STACK_FUNCS(super_log_root_level, struct btrfs_super_block,
1278                          log_root_level, 8);
1279 BTRFS_SETGET_STACK_FUNCS(super_total_bytes, struct btrfs_super_block,
1280                          total_bytes, 64);
1281 BTRFS_SETGET_STACK_FUNCS(super_bytes_used, struct btrfs_super_block,
1282                          bytes_used, 64);
1283 BTRFS_SETGET_STACK_FUNCS(super_sectorsize, struct btrfs_super_block,
1284                          sectorsize, 32);
1285 BTRFS_SETGET_STACK_FUNCS(super_nodesize, struct btrfs_super_block,
1286                          nodesize, 32);
1287 BTRFS_SETGET_STACK_FUNCS(super_leafsize, struct btrfs_super_block,
1288                          leafsize, 32);
1289 BTRFS_SETGET_STACK_FUNCS(super_stripesize, struct btrfs_super_block,
1290                          stripesize, 32);
1291 BTRFS_SETGET_STACK_FUNCS(super_root_dir, struct btrfs_super_block,
1292                          root_dir_objectid, 64);
1293 BTRFS_SETGET_STACK_FUNCS(super_num_devices, struct btrfs_super_block,
1294                          num_devices, 64);
1295
1296 static inline unsigned long btrfs_leaf_data(struct extent_buffer *l)
1297 {
1298         return offsetof(struct btrfs_leaf, items);
1299 }
1300
1301 /* struct btrfs_file_extent_item */
1302 BTRFS_SETGET_FUNCS(file_extent_type, struct btrfs_file_extent_item, type, 8);
1303
1304 static inline unsigned long btrfs_file_extent_inline_start(struct
1305                                                    btrfs_file_extent_item *e)
1306 {
1307         unsigned long offset = (unsigned long)e;
1308         offset += offsetof(struct btrfs_file_extent_item, disk_bytenr);
1309         return offset;
1310 }
1311
1312 static inline u32 btrfs_file_extent_calc_inline_size(u32 datasize)
1313 {
1314         return offsetof(struct btrfs_file_extent_item, disk_bytenr) + datasize;
1315 }
1316
1317 static inline u32 btrfs_file_extent_inline_len(struct extent_buffer *eb,
1318                                                struct btrfs_item *e)
1319 {
1320         unsigned long offset;
1321         offset = offsetof(struct btrfs_file_extent_item, disk_bytenr);
1322         return btrfs_item_size(eb, e) - offset;
1323 }
1324
1325 BTRFS_SETGET_FUNCS(file_extent_disk_bytenr, struct btrfs_file_extent_item,
1326                    disk_bytenr, 64);
1327 BTRFS_SETGET_FUNCS(file_extent_generation, struct btrfs_file_extent_item,
1328                    generation, 64);
1329 BTRFS_SETGET_FUNCS(file_extent_disk_num_bytes, struct btrfs_file_extent_item,
1330                    disk_num_bytes, 64);
1331 BTRFS_SETGET_FUNCS(file_extent_offset, struct btrfs_file_extent_item,
1332                   offset, 64);
1333 BTRFS_SETGET_FUNCS(file_extent_num_bytes, struct btrfs_file_extent_item,
1334                    num_bytes, 64);
1335 BTRFS_SETGET_FUNCS(file_extent_ram_bytes, struct btrfs_file_extent_item,
1336                    ram_bytes, 64);
1337 BTRFS_SETGET_FUNCS(file_extent_compression, struct btrfs_file_extent_item,
1338                    compression, 8);
1339 BTRFS_SETGET_FUNCS(file_extent_encryption, struct btrfs_file_extent_item,
1340                    encryption, 8);
1341 BTRFS_SETGET_FUNCS(file_extent_other_encoding, struct btrfs_file_extent_item,
1342                    other_encoding, 16);
1343
1344 static inline u32 btrfs_level_size(struct btrfs_root *root, int level) {
1345         if (level == 0)
1346                 return root->leafsize;
1347         return root->nodesize;
1348 }
1349
1350 /* helper function to cast into the data area of the leaf. */
1351 #define btrfs_item_ptr(leaf, slot, type) \
1352         ((type *)(btrfs_leaf_data(leaf) + \
1353         btrfs_item_offset_nr(leaf, slot)))
1354
1355 #define btrfs_item_ptr_offset(leaf, slot) \
1356         ((unsigned long)(btrfs_leaf_data(leaf) + \
1357         btrfs_item_offset_nr(leaf, slot)))
1358
1359 /* extent-tree.c */
1360 int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
1361                          struct btrfs_root *root);
1362 int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy);
1363 struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
1364                                                          btrfs_fs_info *info,
1365                                                          u64 bytenr);
1366 struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
1367                                                  struct btrfs_block_group_cache
1368                                                  *hint, u64 search_start,
1369                                                  int data, int owner);
1370 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1371                                              struct btrfs_root *root,
1372                                              u32 blocksize, u64 parent,
1373                                              u64 root_objectid,
1374                                              u64 ref_generation,
1375                                              int level,
1376                                              u64 hint,
1377                                              u64 empty_size);
1378 int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1379                        struct btrfs_root *root,
1380                        u64 num_bytes, u64 parent,
1381                        u64 root_objectid, u64 ref_generation,
1382                        u64 owner, u64 empty_size, u64 hint_byte,
1383                        u64 search_end, struct btrfs_key *ins, int data);
1384 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1385                   struct extent_buffer *orig_buf, struct extent_buffer *buf,
1386                   u32 *nr_extents);
1387 int btrfs_update_ref(struct btrfs_trans_handle *trans,
1388                      struct btrfs_root *root, struct extent_buffer *orig_buf,
1389                      struct extent_buffer *buf, int start_slot, int nr);
1390 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
1391                       *root, u64 bytenr, u64 num_bytes, u64 parent,
1392                       u64 root_objectid, u64 ref_generation,
1393                       u64 owner_objectid, int pin);
1394 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
1395                                struct btrfs_root *root,
1396                                struct extent_io_tree *unpin);
1397 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1398                                 struct btrfs_root *root,
1399                                 u64 bytenr, u64 num_bytes, u64 parent,
1400                                 u64 root_objectid, u64 ref_generation,
1401                                 u64 owner_objectid);
1402 int btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
1403                             struct btrfs_root *root, u64 bytenr,
1404                             u64 orig_parent, u64 parent,
1405                             u64 root_objectid, u64 ref_generation,
1406                             u64 owner_objectid);
1407 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1408                                     struct btrfs_root *root);
1409 int btrfs_free_block_groups(struct btrfs_fs_info *info);
1410 int btrfs_read_block_groups(struct btrfs_root *root);
1411 int btrfs_make_block_group(struct btrfs_trans_handle *trans,
1412                            struct btrfs_root *root, u64 bytes_used,
1413                            u64 type, u64 chunk_objectid, u64 chunk_offset,
1414                            u64 size);
1415 int btrfs_make_block_groups(struct btrfs_trans_handle *trans,
1416                             struct btrfs_root *root);
1417 int btrfs_update_block_group(struct btrfs_trans_handle *trans,
1418                              struct btrfs_root *root, u64 bytenr, u64 num,
1419                              int alloc, int mark_free);
1420 /* ctree.c */
1421 int btrfs_previous_item(struct btrfs_root *root,
1422                         struct btrfs_path *path, u64 min_objectid,
1423                         int type);
1424 int btrfs_comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2);
1425 int btrfs_cow_block(struct btrfs_trans_handle *trans,
1426                     struct btrfs_root *root, struct extent_buffer *buf,
1427                     struct extent_buffer *parent, int parent_slot,
1428                     struct extent_buffer **cow_ret);
1429 int __btrfs_cow_block(struct btrfs_trans_handle *trans,
1430                              struct btrfs_root *root,
1431                              struct extent_buffer *buf,
1432                              struct extent_buffer *parent, int parent_slot,
1433                              struct extent_buffer **cow_ret,
1434                              u64 search_start, u64 empty_size);
1435 int btrfs_copy_root(struct btrfs_trans_handle *trans,
1436                       struct btrfs_root *root,
1437                       struct extent_buffer *buf,
1438                       struct extent_buffer **cow_ret, u64 new_root_objectid);
1439 int btrfs_extend_item(struct btrfs_trans_handle *trans, struct btrfs_root
1440                       *root, struct btrfs_path *path, u32 data_size);
1441 int btrfs_truncate_item(struct btrfs_trans_handle *trans,
1442                         struct btrfs_root *root,
1443                         struct btrfs_path *path,
1444                         u32 new_size, int from_end);
1445 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1446                       *root, struct btrfs_key *key, struct btrfs_path *p, int
1447                       ins_len, int cow);
1448 int btrfs_realloc_node(struct btrfs_trans_handle *trans,
1449                        struct btrfs_root *root, struct extent_buffer *parent,
1450                        int start_slot, int cache_only, u64 *last_ret,
1451                        struct btrfs_key *progress);
1452 void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p);
1453 struct btrfs_path *btrfs_alloc_path(void);
1454 void btrfs_free_path(struct btrfs_path *p);
1455 void btrfs_init_path(struct btrfs_path *p);
1456 int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1457                    struct btrfs_path *path, int slot, int nr);
1458
1459 static inline int btrfs_del_item(struct btrfs_trans_handle *trans,
1460                                  struct btrfs_root *root,
1461                                  struct btrfs_path *path)
1462 {
1463         return btrfs_del_items(trans, root, path, path->slots[0], 1);
1464 }
1465
1466 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
1467                       *root, struct btrfs_key *key, void *data, u32 data_size);
1468 int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
1469                              struct btrfs_root *root,
1470                              struct btrfs_path *path,
1471                              struct btrfs_key *cpu_key, u32 *data_size, int nr);
1472
1473 static inline int btrfs_insert_empty_item(struct btrfs_trans_handle *trans,
1474                                           struct btrfs_root *root,
1475                                           struct btrfs_path *path,
1476                                           struct btrfs_key *key,
1477                                           u32 data_size)
1478 {
1479         return btrfs_insert_empty_items(trans, root, path, key, &data_size, 1);
1480 }
1481
1482 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path);
1483 int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path);
1484 int btrfs_leaf_free_space(struct btrfs_root *root, struct extent_buffer *leaf);
1485 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
1486                         *root);
1487 int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
1488                             struct btrfs_root *root, struct btrfs_path *path,
1489                             struct btrfs_key *new_key);
1490
1491 /* root-item.c */
1492 int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1493                    struct btrfs_key *key);
1494 int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
1495                       *root, struct btrfs_key *key, struct btrfs_root_item
1496                       *item);
1497 int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
1498                       *root, struct btrfs_key *key, struct btrfs_root_item
1499                       *item);
1500 int btrfs_find_last_root(struct btrfs_root *root, u64 objectid, struct
1501                          btrfs_root_item *item, struct btrfs_key *key);
1502 int btrfs_find_dead_roots(struct btrfs_root *root, u64 objectid,
1503                           struct btrfs_root *latest_root);
1504 /* dir-item.c */
1505 int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
1506                           *root, const char *name, int name_len, u64 dir,
1507                           struct btrfs_key *location, u8 type);
1508 struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
1509                                              struct btrfs_root *root,
1510                                              struct btrfs_path *path, u64 dir,
1511                                              const char *name, int name_len,
1512                                              int mod);
1513 struct btrfs_dir_item *
1514 btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans,
1515                             struct btrfs_root *root,
1516                             struct btrfs_path *path, u64 dir,
1517                             u64 objectid, const char *name, int name_len,
1518                             int mod);
1519 struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_root *root,
1520                               struct btrfs_path *path,
1521                               const char *name, int name_len);
1522 int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans,
1523                               struct btrfs_root *root,
1524                               struct btrfs_path *path,
1525                               struct btrfs_dir_item *di);
1526 int btrfs_insert_xattr_item(struct btrfs_trans_handle *trans,
1527                             struct btrfs_root *root, const char *name,
1528                             u16 name_len, const void *data, u16 data_len,
1529                             u64 dir);
1530 struct btrfs_dir_item *btrfs_lookup_xattr(struct btrfs_trans_handle *trans,
1531                                           struct btrfs_root *root,
1532                                           struct btrfs_path *path, u64 dir,
1533                                           const char *name, u16 name_len,
1534                                           int mod);
1535 /* inode-map.c */
1536 int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
1537                              struct btrfs_root *fs_root,
1538                              u64 dirid, u64 *objectid);
1539 int btrfs_find_highest_inode(struct btrfs_root *fs_root, u64 *objectid);
1540
1541 /* inode-item.c */
1542 int btrfs_insert_inode_ref(struct btrfs_trans_handle *trans,
1543                            struct btrfs_root *root,
1544                            const char *name, int name_len,
1545                            u64 inode_objectid, u64 ref_objectid, u64 index);
1546 int btrfs_del_inode_ref(struct btrfs_trans_handle *trans,
1547                            struct btrfs_root *root,
1548                            const char *name, int name_len,
1549                            u64 inode_objectid, u64 ref_objectid);
1550 int btrfs_insert_empty_inode(struct btrfs_trans_handle *trans,
1551                              struct btrfs_root *root,
1552                              struct btrfs_path *path, u64 objectid);
1553 int btrfs_insert_inode(struct btrfs_trans_handle *trans, struct btrfs_root
1554                        *root, u64 objectid, struct btrfs_inode_item
1555                        *inode_item);
1556 int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root
1557                        *root, struct btrfs_path *path,
1558                        struct btrfs_key *location, int mod);
1559
1560 /* file-item.c */
1561 int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
1562                              struct btrfs_root *root,
1563                              u64 objectid, u64 pos, u64 offset,
1564                              u64 disk_num_bytes,
1565                              u64 num_bytes);
1566 int btrfs_insert_inline_extent(struct btrfs_trans_handle *trans,
1567                                 struct btrfs_root *root, u64 objectid,
1568                                 u64 offset, char *buffer, size_t size);
1569 int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
1570                              struct btrfs_root *root,
1571                              struct btrfs_path *path, u64 objectid,
1572                              u64 bytenr, int mod);
1573 int btrfs_csum_file_block(struct btrfs_trans_handle *trans,
1574                           struct btrfs_root *root,
1575                           struct btrfs_inode_item *inode,
1576                           u64 objectid, u64 offset,
1577                           char *data, size_t len);
1578 struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans,
1579                                           struct btrfs_root *root,
1580                                           struct btrfs_path *path,
1581                                           u64 objectid, u64 offset,
1582                                           int cow);
1583 int btrfs_csum_truncate(struct btrfs_trans_handle *trans,
1584                         struct btrfs_root *root, struct btrfs_path *path,
1585                         u64 isize);
1586 #endif