btrfs-progs: Add btrfs_get_extent() and btrfs_punch_hole().
[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_CTREE_H__
20 #define __BTRFS_CTREE_H__
21
22 #if BTRFS_FLAT_INCLUDES
23 #include "list.h"
24 #include "kerncompat.h"
25 #include "radix-tree.h"
26 #include "extent-cache.h"
27 #include "extent_io.h"
28 #include "ioctl.h"
29 #else
30 #include <btrfs/list.h>
31 #include <btrfs/kerncompat.h>
32 #include <btrfs/radix-tree.h>
33 #include <btrfs/extent-cache.h>
34 #include <btrfs/extent_io.h>
35 #include <btrfs/ioctl.h>
36 #endif /* BTRFS_FLAT_INCLUDES */
37
38 struct btrfs_root;
39 struct btrfs_trans_handle;
40 struct btrfs_free_space_ctl;
41 #define BTRFS_MAGIC 0x4D5F53665248425FULL /* ascii _BHRfS_M, no null */
42
43 #define BTRFS_MAX_MIRRORS 3
44
45 #define BTRFS_MAX_LEVEL 8
46
47 #define BTRFS_COMPAT_EXTENT_TREE_V0
48
49 /* holds pointers to all of the tree roots */
50 #define BTRFS_ROOT_TREE_OBJECTID 1ULL
51
52 /* stores information about which extents are in use, and reference counts */
53 #define BTRFS_EXTENT_TREE_OBJECTID 2ULL
54
55 /*
56  * chunk tree stores translations from logical -> physical block numbering
57  * the super block points to the chunk tree
58  */
59 #define BTRFS_CHUNK_TREE_OBJECTID 3ULL
60
61 /*
62  * stores information about which areas of a given device are in use.
63  * one per device.  The tree of tree roots points to the device tree
64  */
65 #define BTRFS_DEV_TREE_OBJECTID 4ULL
66
67 /* one per subvolume, storing files and directories */
68 #define BTRFS_FS_TREE_OBJECTID 5ULL
69
70 /* directory objectid inside the root tree */
71 #define BTRFS_ROOT_TREE_DIR_OBJECTID 6ULL
72 /* holds checksums of all the data extents */
73 #define BTRFS_CSUM_TREE_OBJECTID 7ULL
74 #define BTRFS_QUOTA_TREE_OBJECTID 8ULL
75
76 /* for storing items that use the BTRFS_UUID_KEY* */
77 #define BTRFS_UUID_TREE_OBJECTID 9ULL
78
79 /* for storing balance parameters in the root tree */
80 #define BTRFS_BALANCE_OBJECTID -4ULL
81
82 /* oprhan objectid for tracking unlinked/truncated files */
83 #define BTRFS_ORPHAN_OBJECTID -5ULL
84
85 /* does write ahead logging to speed up fsyncs */
86 #define BTRFS_TREE_LOG_OBJECTID -6ULL
87 #define BTRFS_TREE_LOG_FIXUP_OBJECTID -7ULL
88
89 /* space balancing */
90 #define BTRFS_TREE_RELOC_OBJECTID -8ULL
91 #define BTRFS_DATA_RELOC_TREE_OBJECTID -9ULL
92
93 /*
94  * extent checksums all have this objectid
95  * this allows them to share the logging tree
96  * for fsyncs
97  */
98 #define BTRFS_EXTENT_CSUM_OBJECTID -10ULL
99
100 /* For storing free space cache */
101 #define BTRFS_FREE_SPACE_OBJECTID -11ULL
102
103 /*
104  * The inode number assigned to the special inode for sotring
105  * free ino cache
106  */
107 #define BTRFS_FREE_INO_OBJECTID -12ULL
108
109 /* dummy objectid represents multiple objectids */
110 #define BTRFS_MULTIPLE_OBJECTIDS -255ULL
111
112 /*
113  * All files have objectids in this range.
114  */
115 #define BTRFS_FIRST_FREE_OBJECTID 256ULL
116 #define BTRFS_LAST_FREE_OBJECTID -256ULL
117 #define BTRFS_FIRST_CHUNK_TREE_OBJECTID 256ULL
118
119
120
121 /*
122  * the device items go into the chunk tree.  The key is in the form
123  * [ 1 BTRFS_DEV_ITEM_KEY device_id ]
124  */
125 #define BTRFS_DEV_ITEMS_OBJECTID 1ULL
126
127 /*
128  * the max metadata block size.  This limit is somewhat artificial,
129  * but the memmove costs go through the roof for larger blocks.
130  */
131 #define BTRFS_MAX_METADATA_BLOCKSIZE 65536
132
133 /*
134  * we can actually store much bigger names, but lets not confuse the rest
135  * of linux
136  */
137 #define BTRFS_NAME_LEN 255
138
139 /*
140  * Theoretical limit is larger, but we keep this down to a sane
141  * value. That should limit greatly the possibility of collisions on
142  * inode ref items.
143  */
144 #define BTRFS_LINK_MAX  65535U
145
146 /* 32 bytes in various csum fields */
147 #define BTRFS_CSUM_SIZE 32
148
149 /* csum types */
150 #define BTRFS_CSUM_TYPE_CRC32   0
151
152 static int btrfs_csum_sizes[] = { 4, 0 };
153
154 /* four bytes for CRC32 */
155 #define BTRFS_CRC32_SIZE 4
156 #define BTRFS_EMPTY_DIR_SIZE 0
157
158 #define BTRFS_FT_UNKNOWN        0
159 #define BTRFS_FT_REG_FILE       1
160 #define BTRFS_FT_DIR            2
161 #define BTRFS_FT_CHRDEV         3
162 #define BTRFS_FT_BLKDEV         4
163 #define BTRFS_FT_FIFO           5
164 #define BTRFS_FT_SOCK           6
165 #define BTRFS_FT_SYMLINK        7
166 #define BTRFS_FT_XATTR          8
167 #define BTRFS_FT_MAX            9
168
169 #define BTRFS_ROOT_SUBVOL_RDONLY        (1ULL << 0)
170
171 /*
172  * the key defines the order in the tree, and so it also defines (optimal)
173  * block layout.  objectid corresonds to the inode number.  The flags
174  * tells us things about the object, and is a kind of stream selector.
175  * so for a given inode, keys with flags of 1 might refer to the inode
176  * data, flags of 2 may point to file data in the btree and flags == 3
177  * may point to extents.
178  *
179  * offset is the starting byte offset for this key in the stream.
180  *
181  * btrfs_disk_key is in disk byte order.  struct btrfs_key is always
182  * in cpu native order.  Otherwise they are identical and their sizes
183  * should be the same (ie both packed)
184  */
185 struct btrfs_disk_key {
186         __le64 objectid;
187         u8 type;
188         __le64 offset;
189 } __attribute__ ((__packed__));
190
191 struct btrfs_key {
192         u64 objectid;
193         u8 type;
194         u64 offset;
195 } __attribute__ ((__packed__));
196
197 struct btrfs_mapping_tree {
198         struct cache_tree cache_tree;
199 };
200
201 #define BTRFS_UUID_SIZE 16
202 struct btrfs_dev_item {
203         /* the internal btrfs device id */
204         __le64 devid;
205
206         /* size of the device */
207         __le64 total_bytes;
208
209         /* bytes used */
210         __le64 bytes_used;
211
212         /* optimal io alignment for this device */
213         __le32 io_align;
214
215         /* optimal io width for this device */
216         __le32 io_width;
217
218         /* minimal io size for this device */
219         __le32 sector_size;
220
221         /* type and info about this device */
222         __le64 type;
223
224         /* expected generation for this device */
225         __le64 generation;
226
227         /*
228          * starting byte of this partition on the device,
229          * to allowr for stripe alignment in the future
230          */
231         __le64 start_offset;
232
233         /* grouping information for allocation decisions */
234         __le32 dev_group;
235
236         /* seek speed 0-100 where 100 is fastest */
237         u8 seek_speed;
238
239         /* bandwidth 0-100 where 100 is fastest */
240         u8 bandwidth;
241
242         /* btrfs generated uuid for this device */
243         u8 uuid[BTRFS_UUID_SIZE];
244
245         /* uuid of FS who owns this device */
246         u8 fsid[BTRFS_UUID_SIZE];
247 } __attribute__ ((__packed__));
248
249 struct btrfs_stripe {
250         __le64 devid;
251         __le64 offset;
252         u8 dev_uuid[BTRFS_UUID_SIZE];
253 } __attribute__ ((__packed__));
254
255 struct btrfs_chunk {
256         /* size of this chunk in bytes */
257         __le64 length;
258
259         /* objectid of the root referencing this chunk */
260         __le64 owner;
261
262         __le64 stripe_len;
263         __le64 type;
264
265         /* optimal io alignment for this chunk */
266         __le32 io_align;
267
268         /* optimal io width for this chunk */
269         __le32 io_width;
270
271         /* minimal io size for this chunk */
272         __le32 sector_size;
273
274         /* 2^16 stripes is quite a lot, a second limit is the size of a single
275          * item in the btree
276          */
277         __le16 num_stripes;
278
279         /* sub stripes only matter for raid10 */
280         __le16 sub_stripes;
281         struct btrfs_stripe stripe;
282         /* additional stripes go here */
283 } __attribute__ ((__packed__));
284
285 #define BTRFS_FREE_SPACE_EXTENT 1
286 #define BTRFS_FREE_SPACE_BITMAP 2
287
288 struct btrfs_free_space_entry {
289         __le64 offset;
290         __le64 bytes;
291         u8 type;
292 } __attribute__ ((__packed__));
293
294 struct btrfs_free_space_header {
295         struct btrfs_disk_key location;
296         __le64 generation;
297         __le64 num_entries;
298         __le64 num_bitmaps;
299 } __attribute__ ((__packed__));
300
301 static inline unsigned long btrfs_chunk_item_size(int num_stripes)
302 {
303         BUG_ON(num_stripes == 0);
304         return sizeof(struct btrfs_chunk) +
305                 sizeof(struct btrfs_stripe) * (num_stripes - 1);
306 }
307
308 #define BTRFS_HEADER_FLAG_WRITTEN               (1ULL << 0)
309 #define BTRFS_HEADER_FLAG_RELOC                 (1ULL << 1)
310 #define BTRFS_SUPER_FLAG_SEEDING                (1ULL << 32)
311 #define BTRFS_SUPER_FLAG_METADUMP               (1ULL << 33)
312
313 #define BTRFS_BACKREF_REV_MAX           256
314 #define BTRFS_BACKREF_REV_SHIFT         56
315 #define BTRFS_BACKREF_REV_MASK          (((u64)BTRFS_BACKREF_REV_MAX - 1) << \
316                                          BTRFS_BACKREF_REV_SHIFT)
317
318 #define BTRFS_OLD_BACKREF_REV           0
319 #define BTRFS_MIXED_BACKREF_REV         1
320
321 /*
322  * every tree block (leaf or node) starts with this header.
323  */
324 struct btrfs_header {
325         /* these first four must match the super block */
326         u8 csum[BTRFS_CSUM_SIZE];
327         u8 fsid[BTRFS_FSID_SIZE]; /* FS specific uuid */
328         __le64 bytenr; /* which block this node is supposed to live in */
329         __le64 flags;
330
331         /* allowed to be different from the super from here on down */
332         u8 chunk_tree_uuid[BTRFS_UUID_SIZE];
333         __le64 generation;
334         __le64 owner;
335         __le32 nritems;
336         u8 level;
337 } __attribute__ ((__packed__));
338
339 #define BTRFS_NODEPTRS_PER_BLOCK(r) (((r)->nodesize - \
340                                 sizeof(struct btrfs_header)) / \
341                                 sizeof(struct btrfs_key_ptr))
342 #define __BTRFS_LEAF_DATA_SIZE(bs) ((bs) - sizeof(struct btrfs_header))
343 #define BTRFS_LEAF_DATA_SIZE(r) (__BTRFS_LEAF_DATA_SIZE(r->leafsize))
344 #define BTRFS_MAX_INLINE_DATA_SIZE(r) (BTRFS_LEAF_DATA_SIZE(r) - \
345                                         sizeof(struct btrfs_item) - \
346                                         sizeof(struct btrfs_file_extent_item))
347 #define BTRFS_MAX_XATTR_SIZE(r) (BTRFS_LEAF_DATA_SIZE(r) - \
348                                  sizeof(struct btrfs_item) -\
349                                  sizeof(struct btrfs_dir_item))
350
351
352 /*
353  * this is a very generous portion of the super block, giving us
354  * room to translate 14 chunks with 3 stripes each.
355  */
356 #define BTRFS_SYSTEM_CHUNK_ARRAY_SIZE 2048
357 #define BTRFS_LABEL_SIZE 256
358
359 /*
360  * just in case we somehow lose the roots and are not able to mount,
361  * we store an array of the roots from previous transactions
362  * in the super.
363  */
364 #define BTRFS_NUM_BACKUP_ROOTS 4
365 struct btrfs_root_backup {
366         __le64 tree_root;
367         __le64 tree_root_gen;
368
369         __le64 chunk_root;
370         __le64 chunk_root_gen;
371
372         __le64 extent_root;
373         __le64 extent_root_gen;
374
375         __le64 fs_root;
376         __le64 fs_root_gen;
377
378         __le64 dev_root;
379         __le64 dev_root_gen;
380
381         __le64 csum_root;
382         __le64 csum_root_gen;
383
384         __le64 total_bytes;
385         __le64 bytes_used;
386         __le64 num_devices;
387         /* future */
388         __le64 unsed_64[4];
389
390         u8 tree_root_level;
391         u8 chunk_root_level;
392         u8 extent_root_level;
393         u8 fs_root_level;
394         u8 dev_root_level;
395         u8 csum_root_level;
396         /* future and to align */
397         u8 unused_8[10];
398 } __attribute__ ((__packed__));
399
400 /*
401  * the super block basically lists the main trees of the FS
402  * it currently lacks any block count etc etc
403  */
404 struct btrfs_super_block {
405         u8 csum[BTRFS_CSUM_SIZE];
406         /* the first 3 fields must match struct btrfs_header */
407         u8 fsid[BTRFS_FSID_SIZE];    /* FS specific uuid */
408         __le64 bytenr; /* this block number */
409         __le64 flags;
410
411         /* allowed to be different from the btrfs_header from here own down */
412         __le64 magic;
413         __le64 generation;
414         __le64 root;
415         __le64 chunk_root;
416         __le64 log_root;
417
418         /* this will help find the new super based on the log root */
419         __le64 log_root_transid;
420         __le64 total_bytes;
421         __le64 bytes_used;
422         __le64 root_dir_objectid;
423         __le64 num_devices;
424         __le32 sectorsize;
425         __le32 nodesize;
426         __le32 leafsize;
427         __le32 stripesize;
428         __le32 sys_chunk_array_size;
429         __le64 chunk_root_generation;
430         __le64 compat_flags;
431         __le64 compat_ro_flags;
432         __le64 incompat_flags;
433         __le16 csum_type;
434         u8 root_level;
435         u8 chunk_root_level;
436         u8 log_root_level;
437         struct btrfs_dev_item dev_item;
438
439         char label[BTRFS_LABEL_SIZE];
440
441         __le64 cache_generation;
442         __le64 uuid_tree_generation;
443
444         /* future expansion */
445         __le64 reserved[30];
446         u8 sys_chunk_array[BTRFS_SYSTEM_CHUNK_ARRAY_SIZE];
447         struct btrfs_root_backup super_roots[BTRFS_NUM_BACKUP_ROOTS];
448 } __attribute__ ((__packed__));
449
450 /*
451  * Compat flags that we support.  If any incompat flags are set other than the
452  * ones specified below then we will fail to mount
453  */
454 #define BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF    (1ULL << 0)
455 #define BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL   (1ULL << 1)
456 #define BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS     (1ULL << 2)
457 #define BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO     (1ULL << 3)
458
459 /*
460  * some patches floated around with a second compression method
461  * lets save that incompat here for when they do get in
462  * Note we don't actually support it, we're just reserving the
463  * number
464  */
465 #define BTRFS_FEATURE_INCOMPAT_COMPRESS_LZOv2   (1ULL << 4)
466
467 /*
468  * older kernels tried to do bigger metadata blocks, but the
469  * code was pretty buggy.  Lets not let them try anymore.
470  */
471 #define BTRFS_FEATURE_INCOMPAT_BIG_METADATA     (1ULL << 5)
472 #define BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF    (1ULL << 6)
473 #define BTRFS_FEATURE_INCOMPAT_RAID56           (1ULL << 7)
474 #define BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA  (1ULL << 8)
475 #define BTRFS_FEATURE_INCOMPAT_NO_HOLES         (1ULL << 9)
476
477
478 #define BTRFS_FEATURE_COMPAT_SUPP               0ULL
479 #define BTRFS_FEATURE_COMPAT_RO_SUPP            0ULL
480 #define BTRFS_FEATURE_INCOMPAT_SUPP                     \
481         (BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF |         \
482          BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL |        \
483          BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO |          \
484          BTRFS_FEATURE_INCOMPAT_BIG_METADATA |          \
485          BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF |         \
486          BTRFS_FEATURE_INCOMPAT_RAID56 |                \
487          BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS |          \
488          BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA |       \
489          BTRFS_FEATURE_INCOMPAT_NO_HOLES)
490
491 /*
492  * A leaf is full of items. offset and size tell us where to find
493  * the item in the leaf (relative to the start of the data area)
494  */
495 struct btrfs_item {
496         struct btrfs_disk_key key;
497         __le32 offset;
498         __le32 size;
499 } __attribute__ ((__packed__));
500
501 /*
502  * leaves have an item area and a data area:
503  * [item0, item1....itemN] [free space] [dataN...data1, data0]
504  *
505  * The data is separate from the items to get the keys closer together
506  * during searches.
507  */
508 struct btrfs_leaf {
509         struct btrfs_header header;
510         struct btrfs_item items[];
511 } __attribute__ ((__packed__));
512
513 /*
514  * all non-leaf blocks are nodes, they hold only keys and pointers to
515  * other blocks
516  */
517 struct btrfs_key_ptr {
518         struct btrfs_disk_key key;
519         __le64 blockptr;
520         __le64 generation;
521 } __attribute__ ((__packed__));
522
523 struct btrfs_node {
524         struct btrfs_header header;
525         struct btrfs_key_ptr ptrs[];
526 } __attribute__ ((__packed__));
527
528 /*
529  * btrfs_paths remember the path taken from the root down to the leaf.
530  * level 0 is always the leaf, and nodes[1...BTRFS_MAX_LEVEL] will point
531  * to any other levels that are present.
532  *
533  * The slots array records the index of the item or block pointer
534  * used while walking the tree.
535  */
536
537 struct btrfs_path {
538         struct extent_buffer *nodes[BTRFS_MAX_LEVEL];
539         int slots[BTRFS_MAX_LEVEL];
540         /* if there is real range locking, this locks field will change */
541         int locks[BTRFS_MAX_LEVEL];
542         int reada;
543         /* keep some upper locks as we walk down */
544         int lowest_level;
545
546         /*
547          * set by btrfs_split_item, tells search_slot to keep all locks
548          * and to force calls to keep space in the nodes
549          */
550         unsigned int search_for_split:1;
551         unsigned int skip_check_block:1;
552 };
553
554 /*
555  * items in the extent btree are used to record the objectid of the
556  * owner of the block and the number of references
557  */
558
559 struct btrfs_extent_item {
560         __le64 refs;
561         __le64 generation;
562         __le64 flags;
563 } __attribute__ ((__packed__));
564
565 struct btrfs_extent_item_v0 {
566         __le32 refs;
567 } __attribute__ ((__packed__));
568
569 #define BTRFS_MAX_EXTENT_ITEM_SIZE(r) ((BTRFS_LEAF_DATA_SIZE(r) >> 4) - \
570                                         sizeof(struct btrfs_item))
571
572 #define BTRFS_EXTENT_FLAG_DATA          (1ULL << 0)
573 #define BTRFS_EXTENT_FLAG_TREE_BLOCK    (1ULL << 1)
574
575 /* following flags only apply to tree blocks */
576
577 /* use full backrefs for extent pointers in the block*/
578 #define BTRFS_BLOCK_FLAG_FULL_BACKREF   (1ULL << 8)
579
580 struct btrfs_tree_block_info {
581         struct btrfs_disk_key key;
582         u8 level;
583 } __attribute__ ((__packed__));
584
585 struct btrfs_extent_data_ref {
586         __le64 root;
587         __le64 objectid;
588         __le64 offset;
589         __le32 count;
590 } __attribute__ ((__packed__));
591
592 struct btrfs_shared_data_ref {
593         __le32 count;
594 } __attribute__ ((__packed__));
595
596 struct btrfs_extent_inline_ref {
597         u8 type;
598         __le64 offset;
599 } __attribute__ ((__packed__));
600
601 struct btrfs_extent_ref_v0 {
602         __le64 root;
603         __le64 generation;
604         __le64 objectid;
605         __le32 count;
606 } __attribute__ ((__packed__));
607
608 /* dev extents record free space on individual devices.  The owner
609  * field points back to the chunk allocation mapping tree that allocated
610  * the extent.  The chunk tree uuid field is a way to double check the owner
611  */
612 struct btrfs_dev_extent {
613         __le64 chunk_tree;
614         __le64 chunk_objectid;
615         __le64 chunk_offset;
616         __le64 length;
617         u8 chunk_tree_uuid[BTRFS_UUID_SIZE];
618 } __attribute__ ((__packed__));
619
620 struct btrfs_inode_ref {
621         __le64 index;
622         __le16 name_len;
623         /* name goes here */
624 } __attribute__ ((__packed__));
625
626 struct btrfs_inode_extref {
627         __le64 parent_objectid;
628         __le64 index;
629         __le16 name_len;
630         __u8   name[0]; /* name goes here */
631 } __attribute__ ((__packed__));
632
633 struct btrfs_timespec {
634         __le64 sec;
635         __le32 nsec;
636 } __attribute__ ((__packed__));
637
638 typedef enum {
639         BTRFS_COMPRESS_NONE  = 0,
640         BTRFS_COMPRESS_ZLIB  = 1,
641         BTRFS_COMPRESS_LZO   = 2,
642         BTRFS_COMPRESS_TYPES = 2,
643         BTRFS_COMPRESS_LAST  = 3,
644 } btrfs_compression_type;
645
646 /* we don't understand any encryption methods right now */
647 typedef enum {
648         BTRFS_ENCRYPTION_NONE = 0,
649         BTRFS_ENCRYPTION_LAST = 1,
650 } btrfs_encryption_type;
651
652 enum btrfs_tree_block_status {
653         BTRFS_TREE_BLOCK_CLEAN,
654         BTRFS_TREE_BLOCK_INVALID_NRITEMS,
655         BTRFS_TREE_BLOCK_INVALID_PARENT_KEY,
656         BTRFS_TREE_BLOCK_BAD_KEY_ORDER,
657         BTRFS_TREE_BLOCK_INVALID_LEVEL,
658         BTRFS_TREE_BLOCK_INVALID_FREE_SPACE,
659         BTRFS_TREE_BLOCK_INVALID_OFFSETS,
660 };
661
662 struct btrfs_inode_item {
663         /* nfs style generation number */
664         __le64 generation;
665         /* transid that last touched this inode */
666         __le64 transid;
667         __le64 size;
668         __le64 nbytes;
669         __le64 block_group;
670         __le32 nlink;
671         __le32 uid;
672         __le32 gid;
673         __le32 mode;
674         __le64 rdev;
675         __le64 flags;
676
677         /* modification sequence number for NFS */
678         __le64 sequence;
679
680         /*
681          * a little future expansion, for more than this we can
682          * just grow the inode item and version it
683          */
684         __le64 reserved[4];
685         struct btrfs_timespec atime;
686         struct btrfs_timespec ctime;
687         struct btrfs_timespec mtime;
688         struct btrfs_timespec otime;
689 } __attribute__ ((__packed__));
690
691 struct btrfs_dir_log_item {
692         __le64 end;
693 } __attribute__ ((__packed__));
694
695 struct btrfs_dir_item {
696         struct btrfs_disk_key location;
697         __le64 transid;
698         __le16 data_len;
699         __le16 name_len;
700         u8 type;
701 } __attribute__ ((__packed__));
702
703 struct btrfs_root_item_v0 {
704         struct btrfs_inode_item inode;
705         __le64 generation;
706         __le64 root_dirid;
707         __le64 bytenr;
708         __le64 byte_limit;
709         __le64 bytes_used;
710         __le64 last_snapshot;
711         __le64 flags;
712         __le32 refs;
713         struct btrfs_disk_key drop_progress;
714         u8 drop_level;
715         u8 level;
716 } __attribute__ ((__packed__));
717
718 struct btrfs_root_item {
719         struct btrfs_inode_item inode;
720         __le64 generation;
721         __le64 root_dirid;
722         __le64 bytenr;
723         __le64 byte_limit;
724         __le64 bytes_used;
725         __le64 last_snapshot;
726         __le64 flags;
727         __le32 refs;
728         struct btrfs_disk_key drop_progress;
729         u8 drop_level;
730         u8 level;
731
732         /*
733          * The following fields appear after subvol_uuids+subvol_times
734          * were introduced.
735          */
736
737         /*
738          * This generation number is used to test if the new fields are valid
739          * and up to date while reading the root item. Everytime the root item
740          * is written out, the "generation" field is copied into this field. If
741          * anyone ever mounted the fs with an older kernel, we will have
742          * mismatching generation values here and thus must invalidate the
743          * new fields. See btrfs_update_root and btrfs_find_last_root for
744          * details.
745          * the offset of generation_v2 is also used as the start for the memset
746          * when invalidating the fields.
747          */
748         __le64 generation_v2;
749         u8 uuid[BTRFS_UUID_SIZE];
750         u8 parent_uuid[BTRFS_UUID_SIZE];
751         u8 received_uuid[BTRFS_UUID_SIZE];
752         __le64 ctransid; /* updated when an inode changes */
753         __le64 otransid; /* trans when created */
754         __le64 stransid; /* trans when sent. non-zero for received subvol */
755         __le64 rtransid; /* trans when received. non-zero for received subvol */
756         struct btrfs_timespec ctime;
757         struct btrfs_timespec otime;
758         struct btrfs_timespec stime;
759         struct btrfs_timespec rtime;
760         __le64 reserved[8]; /* for future */
761 } __attribute__ ((__packed__));
762
763 /*
764  * this is used for both forward and backward root refs
765  */
766 struct btrfs_root_ref {
767         __le64 dirid;
768         __le64 sequence;
769         __le16 name_len;
770 } __attribute__ ((__packed__));
771
772 #define BTRFS_FILE_EXTENT_INLINE 0
773 #define BTRFS_FILE_EXTENT_REG 1
774 #define BTRFS_FILE_EXTENT_PREALLOC 2
775
776 struct btrfs_file_extent_item {
777         /*
778          * transaction id that created this extent
779          */
780         __le64 generation;
781         /*
782          * max number of bytes to hold this extent in ram
783          * when we split a compressed extent we can't know how big
784          * each of the resulting pieces will be.  So, this is
785          * an upper limit on the size of the extent in ram instead of
786          * an exact limit.
787          */
788         __le64 ram_bytes;
789
790         /*
791          * 32 bits for the various ways we might encode the data,
792          * including compression and encryption.  If any of these
793          * are set to something a given disk format doesn't understand
794          * it is treated like an incompat flag for reading and writing,
795          * but not for stat.
796          */
797         u8 compression;
798         u8 encryption;
799         __le16 other_encoding; /* spare for later use */
800
801         /* are we inline data or a real extent? */
802         u8 type;
803
804         /*
805          * disk space consumed by the extent, checksum blocks are included
806          * in these numbers
807          */
808         __le64 disk_bytenr;
809         __le64 disk_num_bytes;
810         /*
811          * the logical offset in file blocks (no csums)
812          * this extent record is for.  This allows a file extent to point
813          * into the middle of an existing extent on disk, sharing it
814          * between two snapshots (useful if some bytes in the middle of the
815          * extent have changed
816          */
817         __le64 offset;
818         /*
819          * the logical number of file blocks (no csums included)
820          */
821         __le64 num_bytes;
822
823 } __attribute__ ((__packed__));
824
825 struct btrfs_csum_item {
826         u8 csum;
827 } __attribute__ ((__packed__));
828
829 /*
830  * We don't want to overwrite 1M at the beginning of device, even though
831  * there is our 1st superblock at 64k. Some possible reasons:
832  *  - the first 64k blank is useful for some boot loader/manager
833  *  - the first 1M could be scratched by buggy partitioner or somesuch
834  */
835 #define BTRFS_BLOCK_RESERVED_1M_FOR_SUPER       ((u64)1024 * 1024)
836
837 /* tag for the radix tree of block groups in ram */
838 #define BTRFS_BLOCK_GROUP_DATA          (1ULL << 0)
839 #define BTRFS_BLOCK_GROUP_SYSTEM        (1ULL << 1)
840 #define BTRFS_BLOCK_GROUP_METADATA      (1ULL << 2)
841 #define BTRFS_BLOCK_GROUP_RAID0         (1ULL << 3)
842 #define BTRFS_BLOCK_GROUP_RAID1         (1ULL << 4)
843 #define BTRFS_BLOCK_GROUP_DUP           (1ULL << 5)
844 #define BTRFS_BLOCK_GROUP_RAID10        (1ULL << 6)
845 #define BTRFS_BLOCK_GROUP_RAID5         (1ULL << 7)
846 #define BTRFS_BLOCK_GROUP_RAID6         (1ULL << 8)
847 #define BTRFS_BLOCK_GROUP_RESERVED      BTRFS_AVAIL_ALLOC_BIT_SINGLE
848 #define BTRFS_NR_RAID_TYPES             7
849
850 #define BTRFS_BLOCK_GROUP_TYPE_MASK     (BTRFS_BLOCK_GROUP_DATA |    \
851                                          BTRFS_BLOCK_GROUP_SYSTEM |  \
852                                          BTRFS_BLOCK_GROUP_METADATA)
853
854 #define BTRFS_BLOCK_GROUP_PROFILE_MASK  (BTRFS_BLOCK_GROUP_RAID0 |   \
855                                          BTRFS_BLOCK_GROUP_RAID1 |   \
856                                          BTRFS_BLOCK_GROUP_RAID5 |   \
857                                          BTRFS_BLOCK_GROUP_RAID6 |   \
858                                          BTRFS_BLOCK_GROUP_DUP |     \
859                                          BTRFS_BLOCK_GROUP_RAID10)
860
861 /* used in struct btrfs_balance_args fields */
862 #define BTRFS_AVAIL_ALLOC_BIT_SINGLE    (1ULL << 48)
863
864 /*
865  * GLOBAL_RSV does not exist as a on-disk block group type and is used
866  * internally for exporting info about global block reserve from space infos
867  */
868 #define BTRFS_SPACE_INFO_GLOBAL_RSV    (1ULL << 49)
869
870 #define BTRFS_QGROUP_STATUS_OFF                 0
871 #define BTRFS_QGROUP_STATUS_ON                  1
872 #define BTRFS_QGROUP_STATUS_SCANNING            2
873
874 #define BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT   (1 << 0)
875
876 struct btrfs_qgroup_status_item {
877         __le64 version;
878         __le64 generation;
879         __le64 flags;
880         __le64 scan;            /* progress during scanning */
881 } __attribute__ ((__packed__));
882
883 struct btrfs_block_group_item {
884         __le64 used;
885         __le64 chunk_objectid;
886         __le64 flags;
887 } __attribute__ ((__packed__));
888
889 struct btrfs_qgroup_info_item {
890         __le64 generation;
891         __le64 referenced;
892         __le64 referenced_compressed;
893         __le64 exclusive;
894         __le64 exclusive_compressed;
895 } __attribute__ ((__packed__));
896
897 /* flags definition for qgroup limits */
898 #define BTRFS_QGROUP_LIMIT_MAX_RFER     (1ULL << 0)
899 #define BTRFS_QGROUP_LIMIT_MAX_EXCL     (1ULL << 1)
900 #define BTRFS_QGROUP_LIMIT_RSV_RFER     (1ULL << 2)
901 #define BTRFS_QGROUP_LIMIT_RSV_EXCL     (1ULL << 3)
902 #define BTRFS_QGROUP_LIMIT_RFER_CMPR    (1ULL << 4)
903 #define BTRFS_QGROUP_LIMIT_EXCL_CMPR    (1ULL << 5)
904
905 struct btrfs_qgroup_limit_item {
906         __le64 flags;
907         __le64 max_referenced;
908         __le64 max_exclusive;
909         __le64 rsv_referenced;
910         __le64 rsv_exclusive;
911 } __attribute__ ((__packed__));
912
913 struct btrfs_space_info {
914         u64 flags;
915         u64 total_bytes;
916         u64 bytes_used;
917         u64 bytes_pinned;
918         int full;
919         struct list_head list;
920 };
921
922 struct btrfs_block_group_cache {
923         struct cache_extent cache;
924         struct btrfs_key key;
925         struct btrfs_block_group_item item;
926         struct btrfs_space_info *space_info;
927         struct btrfs_free_space_ctl *free_space_ctl;
928         u64 pinned;
929         u64 flags;
930         int cached;
931         int ro;
932 };
933
934 struct btrfs_extent_ops {
935        int (*alloc_extent)(struct btrfs_root *root, u64 num_bytes,
936                            u64 hint_byte, struct btrfs_key *ins);
937        int (*free_extent)(struct btrfs_root *root, u64 bytenr,
938                           u64 num_bytes);
939 };
940
941 struct btrfs_device;
942 struct btrfs_fs_devices;
943 struct btrfs_fs_info {
944         u8 fsid[BTRFS_FSID_SIZE];
945         u8 chunk_tree_uuid[BTRFS_UUID_SIZE];
946         struct btrfs_root *fs_root;
947         struct btrfs_root *extent_root;
948         struct btrfs_root *tree_root;
949         struct btrfs_root *chunk_root;
950         struct btrfs_root *dev_root;
951         struct btrfs_root *csum_root;
952         struct btrfs_root *quota_root;
953
954         struct rb_root fs_root_tree;
955
956         /* the log root tree is a directory of all the other log roots */
957         struct btrfs_root *log_root_tree;
958
959         struct extent_io_tree extent_cache;
960         struct extent_io_tree free_space_cache;
961         struct extent_io_tree block_group_cache;
962         struct extent_io_tree pinned_extents;
963         struct extent_io_tree pending_del;
964         struct extent_io_tree extent_ins;
965
966         /* logical->physical extent mapping */
967         struct btrfs_mapping_tree mapping_tree;
968
969         u64 generation;
970         u64 last_trans_committed;
971
972         u64 avail_data_alloc_bits;
973         u64 avail_metadata_alloc_bits;
974         u64 avail_system_alloc_bits;
975         u64 data_alloc_profile;
976         u64 metadata_alloc_profile;
977         u64 system_alloc_profile;
978         u64 alloc_start;
979
980         struct btrfs_trans_handle *running_transaction;
981         struct btrfs_super_block *super_copy;
982         struct mutex fs_mutex;
983
984         u64 super_bytenr;
985         u64 total_pinned;
986
987         struct btrfs_extent_ops *extent_ops;
988         struct list_head dirty_cowonly_roots;
989         struct list_head recow_ebs;
990
991         struct btrfs_fs_devices *fs_devices;
992         struct list_head space_info;
993         int system_allocs;
994
995         unsigned int readonly:1;
996         unsigned int on_restoring:1;
997         unsigned int is_chunk_recover:1;
998         unsigned int quota_enabled:1;
999
1000         int (*free_extent_hook)(struct btrfs_trans_handle *trans,
1001                                 struct btrfs_root *root,
1002                                 u64 bytenr, u64 num_bytes, u64 parent,
1003                                 u64 root_objectid, u64 owner, u64 offset,
1004                                 int refs_to_drop);
1005         struct cache_tree *fsck_extent_cache;
1006         struct cache_tree *corrupt_blocks;
1007 };
1008
1009 /*
1010  * in ram representation of the tree.  extent_root is used for all allocations
1011  * and for the extent tree extent_root root.
1012  */
1013 struct btrfs_root {
1014         struct extent_buffer *node;
1015         struct extent_buffer *commit_root;
1016         struct btrfs_root_item root_item;
1017         struct btrfs_key root_key;
1018         struct btrfs_fs_info *fs_info;
1019         u64 objectid;
1020         u64 last_trans;
1021
1022         /* data allocations are done in sectorsize units */
1023         u32 sectorsize;
1024
1025         /* node allocations are done in nodesize units */
1026         u32 nodesize;
1027
1028         /* leaf allocations are done in leafsize units */
1029         u32 leafsize;
1030
1031         /* leaf allocations are done in leafsize units */
1032         u32 stripesize;
1033
1034         int ref_cows;
1035         int track_dirty;
1036
1037
1038         u32 type;
1039         u64 highest_inode;
1040         u64 last_inode_alloc;
1041
1042         /*
1043          * Record orphan data extent ref
1044          *
1045          * TODO: Don't restore things in btrfs_root.
1046          * Directly record it into inode_record, which needs a lot of
1047          * infrastructure change to allow cooperation between extent
1048          * and fs tree scan.
1049          */
1050         struct list_head orphan_data_extents;
1051
1052         /* the dirty list is only used by non-reference counted roots */
1053         struct list_head dirty_list;
1054         struct rb_node rb_node;
1055 };
1056
1057 /*
1058  * inode items have the data typically returned from stat and store other
1059  * info about object characteristics.  There is one for every file and dir in
1060  * the FS
1061  */
1062 #define BTRFS_INODE_ITEM_KEY            1
1063 #define BTRFS_INODE_REF_KEY             12
1064 #define BTRFS_INODE_EXTREF_KEY          13
1065 #define BTRFS_XATTR_ITEM_KEY            24
1066 #define BTRFS_ORPHAN_ITEM_KEY           48
1067
1068 #define BTRFS_DIR_LOG_ITEM_KEY  60
1069 #define BTRFS_DIR_LOG_INDEX_KEY 72
1070 /*
1071  * dir items are the name -> inode pointers in a directory.  There is one
1072  * for every name in a directory.
1073  */
1074 #define BTRFS_DIR_ITEM_KEY      84
1075 #define BTRFS_DIR_INDEX_KEY     96
1076
1077 /*
1078  * extent data is for file data
1079  */
1080 #define BTRFS_EXTENT_DATA_KEY   108
1081
1082 /*
1083  * csum items have the checksums for data in the extents
1084  */
1085 #define BTRFS_CSUM_ITEM_KEY     120
1086 /*
1087  * extent csums are stored in a separate tree and hold csums for
1088  * an entire extent on disk.
1089  */
1090 #define BTRFS_EXTENT_CSUM_KEY   128
1091
1092 /*
1093  * root items point to tree roots.  There are typically in the root
1094  * tree used by the super block to find all the other trees
1095  */
1096 #define BTRFS_ROOT_ITEM_KEY     132
1097
1098 /*
1099  * root backrefs tie subvols and snapshots to the directory entries that
1100  * reference them
1101  */
1102 #define BTRFS_ROOT_BACKREF_KEY  144
1103
1104 /*
1105  * root refs make a fast index for listing all of the snapshots and
1106  * subvolumes referenced by a given root.  They point directly to the
1107  * directory item in the root that references the subvol
1108  */
1109 #define BTRFS_ROOT_REF_KEY      156
1110
1111 /*
1112  * extent items are in the extent map tree.  These record which blocks
1113  * are used, and how many references there are to each block
1114  */
1115 #define BTRFS_EXTENT_ITEM_KEY   168
1116
1117 /*
1118  * The same as the BTRFS_EXTENT_ITEM_KEY, except it's metadata we already know
1119  * the length, so we save the level in key->offset instead of the length.
1120  */
1121 #define BTRFS_METADATA_ITEM_KEY 169
1122
1123 #define BTRFS_TREE_BLOCK_REF_KEY        176
1124
1125 #define BTRFS_EXTENT_DATA_REF_KEY       178
1126
1127 /* old style extent backrefs */
1128 #define BTRFS_EXTENT_REF_V0_KEY         180
1129
1130 #define BTRFS_SHARED_BLOCK_REF_KEY      182
1131
1132 #define BTRFS_SHARED_DATA_REF_KEY       184
1133
1134
1135 /*
1136  * block groups give us hints into the extent allocation trees.  Which
1137  * blocks are free etc etc
1138  */
1139 #define BTRFS_BLOCK_GROUP_ITEM_KEY 192
1140
1141 #define BTRFS_DEV_EXTENT_KEY    204
1142 #define BTRFS_DEV_ITEM_KEY      216
1143 #define BTRFS_CHUNK_ITEM_KEY    228
1144
1145 #define BTRFS_BALANCE_ITEM_KEY  248
1146
1147 /*
1148  * quota groups
1149  */
1150 #define BTRFS_QGROUP_STATUS_KEY         240
1151 #define BTRFS_QGROUP_INFO_KEY           242
1152 #define BTRFS_QGROUP_LIMIT_KEY          244
1153 #define BTRFS_QGROUP_RELATION_KEY       246
1154
1155 /*
1156  * Persistently stores the io stats in the device tree.
1157  * One key for all stats, (0, BTRFS_DEV_STATS_KEY, devid).
1158  */
1159 #define BTRFS_DEV_STATS_KEY     249
1160
1161 /*
1162  * Persistently stores the device replace state in the device tree.
1163  * The key is built like this: (0, BTRFS_DEV_REPLACE_KEY, 0).
1164  */
1165 #define BTRFS_DEV_REPLACE_KEY   250
1166
1167 /*
1168  * Stores items that allow to quickly map UUIDs to something else.
1169  * These items are part of the filesystem UUID tree.
1170  * The key is built like this:
1171  * (UUID_upper_64_bits, BTRFS_UUID_KEY*, UUID_lower_64_bits).
1172  */
1173 #if BTRFS_UUID_SIZE != 16
1174 #error "UUID items require BTRFS_UUID_SIZE == 16!"
1175 #endif
1176 #define BTRFS_UUID_KEY_SUBVOL   251     /* for UUIDs assigned to subvols */
1177 #define BTRFS_UUID_KEY_RECEIVED_SUBVOL  252     /* for UUIDs assigned to
1178                                                  * received subvols */
1179
1180 /*
1181  * string items are for debugging.  They just store a short string of
1182  * data in the FS
1183  */
1184 #define BTRFS_STRING_ITEM_KEY   253
1185 /*
1186  * Inode flags
1187  */
1188 #define BTRFS_INODE_NODATASUM           (1 << 0)
1189 #define BTRFS_INODE_NODATACOW           (1 << 1)
1190 #define BTRFS_INODE_READONLY            (1 << 2)
1191
1192 #define read_eb_member(eb, ptr, type, member, result) (                 \
1193         read_extent_buffer(eb, (char *)(result),                        \
1194                            ((unsigned long)(ptr)) +                     \
1195                             offsetof(type, member),                     \
1196                            sizeof(((type *)0)->member)))
1197
1198 #define write_eb_member(eb, ptr, type, member, result) (                \
1199         write_extent_buffer(eb, (char *)(result),                       \
1200                            ((unsigned long)(ptr)) +                     \
1201                             offsetof(type, member),                     \
1202                            sizeof(((type *)0)->member)))
1203
1204 #define BTRFS_SETGET_HEADER_FUNCS(name, type, member, bits)             \
1205 static inline u##bits btrfs_##name(const struct extent_buffer *eb)      \
1206 {                                                                       \
1207         const struct btrfs_header *h = (struct btrfs_header *)eb->data; \
1208         return le##bits##_to_cpu(h->member);                            \
1209 }                                                                       \
1210 static inline void btrfs_set_##name(struct extent_buffer *eb,           \
1211                                     u##bits val)                        \
1212 {                                                                       \
1213         struct btrfs_header *h = (struct btrfs_header *)eb->data;       \
1214         h->member = cpu_to_le##bits(val);                               \
1215 }
1216
1217 #define BTRFS_SETGET_FUNCS(name, type, member, bits)                    \
1218 static inline u##bits btrfs_##name(const struct extent_buffer *eb,      \
1219                                    const type *s)                       \
1220 {                                                                       \
1221         unsigned long offset = (unsigned long)s;                        \
1222         const type *p = (type *) (eb->data + offset);                   \
1223         return get_unaligned_le##bits(&p->member);                      \
1224 }                                                                       \
1225 static inline void btrfs_set_##name(struct extent_buffer *eb,           \
1226                                     type *s, u##bits val)               \
1227 {                                                                       \
1228         unsigned long offset = (unsigned long)s;                        \
1229         type *p = (type *) (eb->data + offset);                         \
1230         put_unaligned_le##bits(val, &p->member);                        \
1231 }
1232
1233 #define BTRFS_SETGET_STACK_FUNCS(name, type, member, bits)              \
1234 static inline u##bits btrfs_##name(const type *s)                       \
1235 {                                                                       \
1236         return le##bits##_to_cpu(s->member);                            \
1237 }                                                                       \
1238 static inline void btrfs_set_##name(type *s, u##bits val)               \
1239 {                                                                       \
1240         s->member = cpu_to_le##bits(val);                               \
1241 }
1242
1243 BTRFS_SETGET_FUNCS(device_type, struct btrfs_dev_item, type, 64);
1244 BTRFS_SETGET_FUNCS(device_total_bytes, struct btrfs_dev_item, total_bytes, 64);
1245 BTRFS_SETGET_FUNCS(device_bytes_used, struct btrfs_dev_item, bytes_used, 64);
1246 BTRFS_SETGET_FUNCS(device_io_align, struct btrfs_dev_item, io_align, 32);
1247 BTRFS_SETGET_FUNCS(device_io_width, struct btrfs_dev_item, io_width, 32);
1248 BTRFS_SETGET_FUNCS(device_start_offset, struct btrfs_dev_item,
1249                    start_offset, 64);
1250 BTRFS_SETGET_FUNCS(device_sector_size, struct btrfs_dev_item, sector_size, 32);
1251 BTRFS_SETGET_FUNCS(device_id, struct btrfs_dev_item, devid, 64);
1252 BTRFS_SETGET_FUNCS(device_group, struct btrfs_dev_item, dev_group, 32);
1253 BTRFS_SETGET_FUNCS(device_seek_speed, struct btrfs_dev_item, seek_speed, 8);
1254 BTRFS_SETGET_FUNCS(device_bandwidth, struct btrfs_dev_item, bandwidth, 8);
1255 BTRFS_SETGET_FUNCS(device_generation, struct btrfs_dev_item, generation, 64);
1256
1257 BTRFS_SETGET_STACK_FUNCS(stack_device_type, struct btrfs_dev_item, type, 64);
1258 BTRFS_SETGET_STACK_FUNCS(stack_device_total_bytes, struct btrfs_dev_item,
1259                          total_bytes, 64);
1260 BTRFS_SETGET_STACK_FUNCS(stack_device_bytes_used, struct btrfs_dev_item,
1261                          bytes_used, 64);
1262 BTRFS_SETGET_STACK_FUNCS(stack_device_io_align, struct btrfs_dev_item,
1263                          io_align, 32);
1264 BTRFS_SETGET_STACK_FUNCS(stack_device_io_width, struct btrfs_dev_item,
1265                          io_width, 32);
1266 BTRFS_SETGET_STACK_FUNCS(stack_device_sector_size, struct btrfs_dev_item,
1267                          sector_size, 32);
1268 BTRFS_SETGET_STACK_FUNCS(stack_device_id, struct btrfs_dev_item, devid, 64);
1269 BTRFS_SETGET_STACK_FUNCS(stack_device_group, struct btrfs_dev_item,
1270                          dev_group, 32);
1271 BTRFS_SETGET_STACK_FUNCS(stack_device_seek_speed, struct btrfs_dev_item,
1272                          seek_speed, 8);
1273 BTRFS_SETGET_STACK_FUNCS(stack_device_bandwidth, struct btrfs_dev_item,
1274                          bandwidth, 8);
1275 BTRFS_SETGET_STACK_FUNCS(stack_device_generation, struct btrfs_dev_item,
1276                          generation, 64);
1277
1278 static inline char *btrfs_device_uuid(struct btrfs_dev_item *d)
1279 {
1280         return (char *)d + offsetof(struct btrfs_dev_item, uuid);
1281 }
1282
1283 static inline char *btrfs_device_fsid(struct btrfs_dev_item *d)
1284 {
1285         return (char *)d + offsetof(struct btrfs_dev_item, fsid);
1286 }
1287
1288 BTRFS_SETGET_FUNCS(chunk_length, struct btrfs_chunk, length, 64);
1289 BTRFS_SETGET_FUNCS(chunk_owner, struct btrfs_chunk, owner, 64);
1290 BTRFS_SETGET_FUNCS(chunk_stripe_len, struct btrfs_chunk, stripe_len, 64);
1291 BTRFS_SETGET_FUNCS(chunk_io_align, struct btrfs_chunk, io_align, 32);
1292 BTRFS_SETGET_FUNCS(chunk_io_width, struct btrfs_chunk, io_width, 32);
1293 BTRFS_SETGET_FUNCS(chunk_sector_size, struct btrfs_chunk, sector_size, 32);
1294 BTRFS_SETGET_FUNCS(chunk_type, struct btrfs_chunk, type, 64);
1295 BTRFS_SETGET_FUNCS(chunk_num_stripes, struct btrfs_chunk, num_stripes, 16);
1296 BTRFS_SETGET_FUNCS(chunk_sub_stripes, struct btrfs_chunk, sub_stripes, 16);
1297 BTRFS_SETGET_FUNCS(stripe_devid, struct btrfs_stripe, devid, 64);
1298 BTRFS_SETGET_FUNCS(stripe_offset, struct btrfs_stripe, offset, 64);
1299
1300 static inline char *btrfs_stripe_dev_uuid(struct btrfs_stripe *s)
1301 {
1302         return (char *)s + offsetof(struct btrfs_stripe, dev_uuid);
1303 }
1304
1305 BTRFS_SETGET_STACK_FUNCS(stack_chunk_length, struct btrfs_chunk, length, 64);
1306 BTRFS_SETGET_STACK_FUNCS(stack_chunk_owner, struct btrfs_chunk, owner, 64);
1307 BTRFS_SETGET_STACK_FUNCS(stack_chunk_stripe_len, struct btrfs_chunk,
1308                          stripe_len, 64);
1309 BTRFS_SETGET_STACK_FUNCS(stack_chunk_io_align, struct btrfs_chunk,
1310                          io_align, 32);
1311 BTRFS_SETGET_STACK_FUNCS(stack_chunk_io_width, struct btrfs_chunk,
1312                          io_width, 32);
1313 BTRFS_SETGET_STACK_FUNCS(stack_chunk_sector_size, struct btrfs_chunk,
1314                          sector_size, 32);
1315 BTRFS_SETGET_STACK_FUNCS(stack_chunk_type, struct btrfs_chunk, type, 64);
1316 BTRFS_SETGET_STACK_FUNCS(stack_chunk_num_stripes, struct btrfs_chunk,
1317                          num_stripes, 16);
1318 BTRFS_SETGET_STACK_FUNCS(stack_chunk_sub_stripes, struct btrfs_chunk,
1319                          sub_stripes, 16);
1320 BTRFS_SETGET_STACK_FUNCS(stack_stripe_devid, struct btrfs_stripe, devid, 64);
1321 BTRFS_SETGET_STACK_FUNCS(stack_stripe_offset, struct btrfs_stripe, offset, 64);
1322
1323 static inline struct btrfs_stripe *btrfs_stripe_nr(struct btrfs_chunk *c,
1324                                                    int nr)
1325 {
1326         unsigned long offset = (unsigned long)c;
1327         offset += offsetof(struct btrfs_chunk, stripe);
1328         offset += nr * sizeof(struct btrfs_stripe);
1329         return (struct btrfs_stripe *)offset;
1330 }
1331
1332 static inline char *btrfs_stripe_dev_uuid_nr(struct btrfs_chunk *c, int nr)
1333 {
1334         return btrfs_stripe_dev_uuid(btrfs_stripe_nr(c, nr));
1335 }
1336
1337 static inline u64 btrfs_stripe_offset_nr(struct extent_buffer *eb,
1338                                          struct btrfs_chunk *c, int nr)
1339 {
1340         return btrfs_stripe_offset(eb, btrfs_stripe_nr(c, nr));
1341 }
1342
1343 static inline void btrfs_set_stripe_offset_nr(struct extent_buffer *eb,
1344                                              struct btrfs_chunk *c, int nr,
1345                                              u64 val)
1346 {
1347         btrfs_set_stripe_offset(eb, btrfs_stripe_nr(c, nr), val);
1348 }
1349
1350 static inline u64 btrfs_stripe_devid_nr(struct extent_buffer *eb,
1351                                          struct btrfs_chunk *c, int nr)
1352 {
1353         return btrfs_stripe_devid(eb, btrfs_stripe_nr(c, nr));
1354 }
1355
1356 static inline void btrfs_set_stripe_devid_nr(struct extent_buffer *eb,
1357                                              struct btrfs_chunk *c, int nr,
1358                                              u64 val)
1359 {
1360         btrfs_set_stripe_devid(eb, btrfs_stripe_nr(c, nr), val);
1361 }
1362
1363 /* struct btrfs_block_group_item */
1364 BTRFS_SETGET_STACK_FUNCS(block_group_used, struct btrfs_block_group_item,
1365                          used, 64);
1366 BTRFS_SETGET_FUNCS(disk_block_group_used, struct btrfs_block_group_item,
1367                          used, 64);
1368 BTRFS_SETGET_STACK_FUNCS(block_group_chunk_objectid,
1369                         struct btrfs_block_group_item, chunk_objectid, 64);
1370
1371 BTRFS_SETGET_FUNCS(disk_block_group_chunk_objectid,
1372                    struct btrfs_block_group_item, chunk_objectid, 64);
1373 BTRFS_SETGET_FUNCS(disk_block_group_flags,
1374                    struct btrfs_block_group_item, flags, 64);
1375 BTRFS_SETGET_STACK_FUNCS(block_group_flags,
1376                         struct btrfs_block_group_item, flags, 64);
1377
1378 /* struct btrfs_inode_ref */
1379 BTRFS_SETGET_FUNCS(inode_ref_name_len, struct btrfs_inode_ref, name_len, 16);
1380 BTRFS_SETGET_STACK_FUNCS(stack_inode_ref_name_len, struct btrfs_inode_ref, name_len, 16);
1381 BTRFS_SETGET_FUNCS(inode_ref_index, struct btrfs_inode_ref, index, 64);
1382
1383 /* struct btrfs_inode_extref */
1384 BTRFS_SETGET_FUNCS(inode_extref_parent, struct btrfs_inode_extref,
1385                    parent_objectid, 64);
1386 BTRFS_SETGET_FUNCS(inode_extref_name_len, struct btrfs_inode_extref,
1387                    name_len, 16);
1388 BTRFS_SETGET_FUNCS(inode_extref_index, struct btrfs_inode_extref, index, 64);
1389
1390 /* struct btrfs_inode_item */
1391 BTRFS_SETGET_FUNCS(inode_generation, struct btrfs_inode_item, generation, 64);
1392 BTRFS_SETGET_FUNCS(inode_sequence, struct btrfs_inode_item, sequence, 64);
1393 BTRFS_SETGET_FUNCS(inode_transid, struct btrfs_inode_item, transid, 64);
1394 BTRFS_SETGET_FUNCS(inode_size, struct btrfs_inode_item, size, 64);
1395 BTRFS_SETGET_FUNCS(inode_nbytes, struct btrfs_inode_item, nbytes, 64);
1396 BTRFS_SETGET_FUNCS(inode_block_group, struct btrfs_inode_item, block_group, 64);
1397 BTRFS_SETGET_FUNCS(inode_nlink, struct btrfs_inode_item, nlink, 32);
1398 BTRFS_SETGET_FUNCS(inode_uid, struct btrfs_inode_item, uid, 32);
1399 BTRFS_SETGET_FUNCS(inode_gid, struct btrfs_inode_item, gid, 32);
1400 BTRFS_SETGET_FUNCS(inode_mode, struct btrfs_inode_item, mode, 32);
1401 BTRFS_SETGET_FUNCS(inode_rdev, struct btrfs_inode_item, rdev, 64);
1402 BTRFS_SETGET_FUNCS(inode_flags, struct btrfs_inode_item, flags, 64);
1403
1404 BTRFS_SETGET_STACK_FUNCS(stack_inode_generation,
1405                          struct btrfs_inode_item, generation, 64);
1406 BTRFS_SETGET_STACK_FUNCS(stack_inode_sequence,
1407                          struct btrfs_inode_item, generation, 64);
1408 BTRFS_SETGET_STACK_FUNCS(stack_inode_size,
1409                          struct btrfs_inode_item, size, 64);
1410 BTRFS_SETGET_STACK_FUNCS(stack_inode_nbytes,
1411                          struct btrfs_inode_item, nbytes, 64);
1412 BTRFS_SETGET_STACK_FUNCS(stack_inode_block_group,
1413                          struct btrfs_inode_item, block_group, 64);
1414 BTRFS_SETGET_STACK_FUNCS(stack_inode_nlink,
1415                          struct btrfs_inode_item, nlink, 32);
1416 BTRFS_SETGET_STACK_FUNCS(stack_inode_uid,
1417                          struct btrfs_inode_item, uid, 32);
1418 BTRFS_SETGET_STACK_FUNCS(stack_inode_gid,
1419                          struct btrfs_inode_item, gid, 32);
1420 BTRFS_SETGET_STACK_FUNCS(stack_inode_mode,
1421                          struct btrfs_inode_item, mode, 32);
1422 BTRFS_SETGET_STACK_FUNCS(stack_inode_rdev,
1423                          struct btrfs_inode_item, rdev, 64);
1424 BTRFS_SETGET_STACK_FUNCS(stack_inode_flags,
1425                          struct btrfs_inode_item, flags, 64);
1426
1427 static inline struct btrfs_timespec *
1428 btrfs_inode_atime(struct btrfs_inode_item *inode_item)
1429 {
1430         unsigned long ptr = (unsigned long)inode_item;
1431         ptr += offsetof(struct btrfs_inode_item, atime);
1432         return (struct btrfs_timespec *)ptr;
1433 }
1434
1435 static inline struct btrfs_timespec *
1436 btrfs_inode_mtime(struct btrfs_inode_item *inode_item)
1437 {
1438         unsigned long ptr = (unsigned long)inode_item;
1439         ptr += offsetof(struct btrfs_inode_item, mtime);
1440         return (struct btrfs_timespec *)ptr;
1441 }
1442
1443 static inline struct btrfs_timespec *
1444 btrfs_inode_ctime(struct btrfs_inode_item *inode_item)
1445 {
1446         unsigned long ptr = (unsigned long)inode_item;
1447         ptr += offsetof(struct btrfs_inode_item, ctime);
1448         return (struct btrfs_timespec *)ptr;
1449 }
1450
1451 static inline struct btrfs_timespec *
1452 btrfs_inode_otime(struct btrfs_inode_item *inode_item)
1453 {
1454         unsigned long ptr = (unsigned long)inode_item;
1455         ptr += offsetof(struct btrfs_inode_item, otime);
1456         return (struct btrfs_timespec *)ptr;
1457 }
1458
1459 BTRFS_SETGET_FUNCS(timespec_sec, struct btrfs_timespec, sec, 64);
1460 BTRFS_SETGET_FUNCS(timespec_nsec, struct btrfs_timespec, nsec, 32);
1461 BTRFS_SETGET_STACK_FUNCS(stack_timespec_sec, struct btrfs_timespec,
1462                          sec, 64);
1463 BTRFS_SETGET_STACK_FUNCS(stack_timespec_nsec, struct btrfs_timespec,
1464                          nsec, 32);
1465
1466 /* struct btrfs_dev_extent */
1467 BTRFS_SETGET_FUNCS(dev_extent_chunk_tree, struct btrfs_dev_extent,
1468                    chunk_tree, 64);
1469 BTRFS_SETGET_FUNCS(dev_extent_chunk_objectid, struct btrfs_dev_extent,
1470                    chunk_objectid, 64);
1471 BTRFS_SETGET_FUNCS(dev_extent_chunk_offset, struct btrfs_dev_extent,
1472                    chunk_offset, 64);
1473 BTRFS_SETGET_FUNCS(dev_extent_length, struct btrfs_dev_extent, length, 64);
1474
1475 static inline u8 *btrfs_dev_extent_chunk_tree_uuid(struct btrfs_dev_extent *dev)
1476 {
1477         unsigned long ptr = offsetof(struct btrfs_dev_extent, chunk_tree_uuid);
1478         return (u8 *)((unsigned long)dev + ptr);
1479 }
1480
1481
1482 /* struct btrfs_extent_item */
1483 BTRFS_SETGET_FUNCS(extent_refs, struct btrfs_extent_item, refs, 64);
1484 BTRFS_SETGET_STACK_FUNCS(stack_extent_refs, struct btrfs_extent_item, refs, 64);
1485 BTRFS_SETGET_FUNCS(extent_generation, struct btrfs_extent_item,
1486                    generation, 64);
1487 BTRFS_SETGET_FUNCS(extent_flags, struct btrfs_extent_item, flags, 64);
1488 BTRFS_SETGET_STACK_FUNCS(stack_extent_flags, struct btrfs_extent_item, flags, 64);
1489
1490 BTRFS_SETGET_FUNCS(extent_refs_v0, struct btrfs_extent_item_v0, refs, 32);
1491
1492 BTRFS_SETGET_FUNCS(tree_block_level, struct btrfs_tree_block_info, level, 8);
1493
1494 static inline void btrfs_tree_block_key(struct extent_buffer *eb,
1495                                         struct btrfs_tree_block_info *item,
1496                                         struct btrfs_disk_key *key)
1497 {
1498         read_eb_member(eb, item, struct btrfs_tree_block_info, key, key);
1499 }
1500
1501 static inline void btrfs_set_tree_block_key(struct extent_buffer *eb,
1502                                             struct btrfs_tree_block_info *item,
1503                                             struct btrfs_disk_key *key)
1504 {
1505         write_eb_member(eb, item, struct btrfs_tree_block_info, key, key);
1506 }
1507
1508 BTRFS_SETGET_FUNCS(extent_data_ref_root, struct btrfs_extent_data_ref,
1509                    root, 64);
1510 BTRFS_SETGET_FUNCS(extent_data_ref_objectid, struct btrfs_extent_data_ref,
1511                    objectid, 64);
1512 BTRFS_SETGET_FUNCS(extent_data_ref_offset, struct btrfs_extent_data_ref,
1513                    offset, 64);
1514 BTRFS_SETGET_FUNCS(extent_data_ref_count, struct btrfs_extent_data_ref,
1515                    count, 32);
1516
1517 BTRFS_SETGET_FUNCS(shared_data_ref_count, struct btrfs_shared_data_ref,
1518                    count, 32);
1519
1520 BTRFS_SETGET_FUNCS(extent_inline_ref_type, struct btrfs_extent_inline_ref,
1521                    type, 8);
1522 BTRFS_SETGET_FUNCS(extent_inline_ref_offset, struct btrfs_extent_inline_ref,
1523                    offset, 64);
1524 BTRFS_SETGET_STACK_FUNCS(stack_extent_inline_ref_type,
1525                          struct btrfs_extent_inline_ref, type, 8);
1526 BTRFS_SETGET_STACK_FUNCS(stack_extent_inline_ref_offset,
1527                          struct btrfs_extent_inline_ref, offset, 64);
1528
1529 static inline u32 btrfs_extent_inline_ref_size(int type)
1530 {
1531         if (type == BTRFS_TREE_BLOCK_REF_KEY ||
1532             type == BTRFS_SHARED_BLOCK_REF_KEY)
1533                 return sizeof(struct btrfs_extent_inline_ref);
1534         if (type == BTRFS_SHARED_DATA_REF_KEY)
1535                 return sizeof(struct btrfs_shared_data_ref) +
1536                        sizeof(struct btrfs_extent_inline_ref);
1537         if (type == BTRFS_EXTENT_DATA_REF_KEY)
1538                 return sizeof(struct btrfs_extent_data_ref) +
1539                        offsetof(struct btrfs_extent_inline_ref, offset);
1540         BUG();
1541         return 0;
1542 }
1543
1544 BTRFS_SETGET_FUNCS(ref_root_v0, struct btrfs_extent_ref_v0, root, 64);
1545 BTRFS_SETGET_FUNCS(ref_generation_v0, struct btrfs_extent_ref_v0,
1546                    generation, 64);
1547 BTRFS_SETGET_FUNCS(ref_objectid_v0, struct btrfs_extent_ref_v0, objectid, 64);
1548 BTRFS_SETGET_FUNCS(ref_count_v0, struct btrfs_extent_ref_v0, count, 32);
1549
1550 /* struct btrfs_node */
1551 BTRFS_SETGET_FUNCS(key_blockptr, struct btrfs_key_ptr, blockptr, 64);
1552 BTRFS_SETGET_FUNCS(key_generation, struct btrfs_key_ptr, generation, 64);
1553
1554 static inline u64 btrfs_node_blockptr(struct extent_buffer *eb, int nr)
1555 {
1556         unsigned long ptr;
1557         ptr = offsetof(struct btrfs_node, ptrs) +
1558                 sizeof(struct btrfs_key_ptr) * nr;
1559         return btrfs_key_blockptr(eb, (struct btrfs_key_ptr *)ptr);
1560 }
1561
1562 static inline void btrfs_set_node_blockptr(struct extent_buffer *eb,
1563                                            int nr, u64 val)
1564 {
1565         unsigned long ptr;
1566         ptr = offsetof(struct btrfs_node, ptrs) +
1567                 sizeof(struct btrfs_key_ptr) * nr;
1568         btrfs_set_key_blockptr(eb, (struct btrfs_key_ptr *)ptr, val);
1569 }
1570
1571 static inline u64 btrfs_node_ptr_generation(struct extent_buffer *eb, int nr)
1572 {
1573         unsigned long ptr;
1574         ptr = offsetof(struct btrfs_node, ptrs) +
1575                 sizeof(struct btrfs_key_ptr) * nr;
1576         return btrfs_key_generation(eb, (struct btrfs_key_ptr *)ptr);
1577 }
1578
1579 static inline void btrfs_set_node_ptr_generation(struct extent_buffer *eb,
1580                                                  int nr, u64 val)
1581 {
1582         unsigned long ptr;
1583         ptr = offsetof(struct btrfs_node, ptrs) +
1584                 sizeof(struct btrfs_key_ptr) * nr;
1585         btrfs_set_key_generation(eb, (struct btrfs_key_ptr *)ptr, val);
1586 }
1587
1588 static inline unsigned long btrfs_node_key_ptr_offset(int nr)
1589 {
1590         return offsetof(struct btrfs_node, ptrs) +
1591                 sizeof(struct btrfs_key_ptr) * nr;
1592 }
1593
1594 static inline void btrfs_node_key(struct extent_buffer *eb,
1595                                   struct btrfs_disk_key *disk_key, int nr)
1596 {
1597         unsigned long ptr;
1598         ptr = btrfs_node_key_ptr_offset(nr);
1599         read_eb_member(eb, (struct btrfs_key_ptr *)ptr,
1600                        struct btrfs_key_ptr, key, disk_key);
1601 }
1602
1603 static inline void btrfs_set_node_key(struct extent_buffer *eb,
1604                                       struct btrfs_disk_key *disk_key, int nr)
1605 {
1606         unsigned long ptr;
1607         ptr = btrfs_node_key_ptr_offset(nr);
1608         write_eb_member(eb, (struct btrfs_key_ptr *)ptr,
1609                        struct btrfs_key_ptr, key, disk_key);
1610 }
1611
1612 /* struct btrfs_item */
1613 BTRFS_SETGET_FUNCS(item_offset, struct btrfs_item, offset, 32);
1614 BTRFS_SETGET_FUNCS(item_size, struct btrfs_item, size, 32);
1615
1616 static inline unsigned long btrfs_item_nr_offset(int nr)
1617 {
1618         return offsetof(struct btrfs_leaf, items) +
1619                 sizeof(struct btrfs_item) * nr;
1620 }
1621
1622 static inline struct btrfs_item *btrfs_item_nr(int nr)
1623 {
1624         return (struct btrfs_item *)btrfs_item_nr_offset(nr);
1625 }
1626
1627 static inline u32 btrfs_item_end(struct extent_buffer *eb,
1628                                  struct btrfs_item *item)
1629 {
1630         return btrfs_item_offset(eb, item) + btrfs_item_size(eb, item);
1631 }
1632
1633 static inline u32 btrfs_item_end_nr(struct extent_buffer *eb, int nr)
1634 {
1635         return btrfs_item_end(eb, btrfs_item_nr(nr));
1636 }
1637
1638 static inline u32 btrfs_item_offset_nr(struct extent_buffer *eb, int nr)
1639 {
1640         return btrfs_item_offset(eb, btrfs_item_nr(nr));
1641 }
1642
1643 static inline u32 btrfs_item_size_nr(struct extent_buffer *eb, int nr)
1644 {
1645         return btrfs_item_size(eb, btrfs_item_nr(nr));
1646 }
1647
1648 static inline void btrfs_item_key(struct extent_buffer *eb,
1649                            struct btrfs_disk_key *disk_key, int nr)
1650 {
1651         struct btrfs_item *item = btrfs_item_nr(nr);
1652         read_eb_member(eb, item, struct btrfs_item, key, disk_key);
1653 }
1654
1655 static inline void btrfs_set_item_key(struct extent_buffer *eb,
1656                                struct btrfs_disk_key *disk_key, int nr)
1657 {
1658         struct btrfs_item *item = btrfs_item_nr(nr);
1659         write_eb_member(eb, item, struct btrfs_item, key, disk_key);
1660 }
1661
1662 BTRFS_SETGET_FUNCS(dir_log_end, struct btrfs_dir_log_item, end, 64);
1663
1664 /*
1665  * struct btrfs_root_ref
1666  */
1667 BTRFS_SETGET_FUNCS(root_ref_dirid, struct btrfs_root_ref, dirid, 64);
1668 BTRFS_SETGET_FUNCS(root_ref_sequence, struct btrfs_root_ref, sequence, 64);
1669 BTRFS_SETGET_FUNCS(root_ref_name_len, struct btrfs_root_ref, name_len, 16);
1670
1671 BTRFS_SETGET_STACK_FUNCS(stack_root_ref_dirid, struct btrfs_root_ref, dirid, 64);
1672 BTRFS_SETGET_STACK_FUNCS(stack_root_ref_sequence, struct btrfs_root_ref, sequence, 64);
1673 BTRFS_SETGET_STACK_FUNCS(stack_root_ref_name_len, struct btrfs_root_ref, name_len, 16);
1674
1675 /* struct btrfs_dir_item */
1676 BTRFS_SETGET_FUNCS(dir_data_len, struct btrfs_dir_item, data_len, 16);
1677 BTRFS_SETGET_FUNCS(dir_type, struct btrfs_dir_item, type, 8);
1678 BTRFS_SETGET_FUNCS(dir_name_len, struct btrfs_dir_item, name_len, 16);
1679 BTRFS_SETGET_FUNCS(dir_transid, struct btrfs_dir_item, transid, 64);
1680
1681 BTRFS_SETGET_STACK_FUNCS(stack_dir_name_len, struct btrfs_dir_item, name_len, 16);
1682
1683 static inline void btrfs_dir_item_key(struct extent_buffer *eb,
1684                                       struct btrfs_dir_item *item,
1685                                       struct btrfs_disk_key *key)
1686 {
1687         read_eb_member(eb, item, struct btrfs_dir_item, location, key);
1688 }
1689
1690 static inline void btrfs_set_dir_item_key(struct extent_buffer *eb,
1691                                           struct btrfs_dir_item *item,
1692                                           struct btrfs_disk_key *key)
1693 {
1694         write_eb_member(eb, item, struct btrfs_dir_item, location, key);
1695 }
1696
1697 /* struct btrfs_free_space_header */
1698 BTRFS_SETGET_FUNCS(free_space_entries, struct btrfs_free_space_header,
1699                    num_entries, 64);
1700 BTRFS_SETGET_FUNCS(free_space_bitmaps, struct btrfs_free_space_header,
1701                    num_bitmaps, 64);
1702 BTRFS_SETGET_FUNCS(free_space_generation, struct btrfs_free_space_header,
1703                    generation, 64);
1704
1705 static inline void btrfs_free_space_key(struct extent_buffer *eb,
1706                                         struct btrfs_free_space_header *h,
1707                                         struct btrfs_disk_key *key)
1708 {
1709         read_eb_member(eb, h, struct btrfs_free_space_header, location, key);
1710 }
1711
1712 static inline void btrfs_set_free_space_key(struct extent_buffer *eb,
1713                                             struct btrfs_free_space_header *h,
1714                                             struct btrfs_disk_key *key)
1715 {
1716         write_eb_member(eb, h, struct btrfs_free_space_header, location, key);
1717 }
1718
1719 /* struct btrfs_disk_key */
1720 BTRFS_SETGET_STACK_FUNCS(disk_key_objectid, struct btrfs_disk_key,
1721                          objectid, 64);
1722 BTRFS_SETGET_STACK_FUNCS(disk_key_offset, struct btrfs_disk_key, offset, 64);
1723 BTRFS_SETGET_STACK_FUNCS(disk_key_type, struct btrfs_disk_key, type, 8);
1724
1725 static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu,
1726                                          struct btrfs_disk_key *disk)
1727 {
1728         cpu->offset = le64_to_cpu(disk->offset);
1729         cpu->type = disk->type;
1730         cpu->objectid = le64_to_cpu(disk->objectid);
1731 }
1732
1733 static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk,
1734                                          struct btrfs_key *cpu)
1735 {
1736         disk->offset = cpu_to_le64(cpu->offset);
1737         disk->type = cpu->type;
1738         disk->objectid = cpu_to_le64(cpu->objectid);
1739 }
1740
1741 static inline void btrfs_node_key_to_cpu(struct extent_buffer *eb,
1742                                   struct btrfs_key *key, int nr)
1743 {
1744         struct btrfs_disk_key disk_key;
1745         btrfs_node_key(eb, &disk_key, nr);
1746         btrfs_disk_key_to_cpu(key, &disk_key);
1747 }
1748
1749 static inline void btrfs_item_key_to_cpu(struct extent_buffer *eb,
1750                                   struct btrfs_key *key, int nr)
1751 {
1752         struct btrfs_disk_key disk_key;
1753         btrfs_item_key(eb, &disk_key, nr);
1754         btrfs_disk_key_to_cpu(key, &disk_key);
1755 }
1756
1757 static inline void btrfs_dir_item_key_to_cpu(struct extent_buffer *eb,
1758                                       struct btrfs_dir_item *item,
1759                                       struct btrfs_key *key)
1760 {
1761         struct btrfs_disk_key disk_key;
1762         btrfs_dir_item_key(eb, item, &disk_key);
1763         btrfs_disk_key_to_cpu(key, &disk_key);
1764 }
1765
1766
1767 static inline u8 btrfs_key_type(struct btrfs_key *key)
1768 {
1769         return key->type;
1770 }
1771
1772 static inline void btrfs_set_key_type(struct btrfs_key *key, u8 val)
1773 {
1774         key->type = val;
1775 }
1776
1777 /* struct btrfs_header */
1778 BTRFS_SETGET_HEADER_FUNCS(header_bytenr, struct btrfs_header, bytenr, 64);
1779 BTRFS_SETGET_HEADER_FUNCS(header_generation, struct btrfs_header,
1780                           generation, 64);
1781 BTRFS_SETGET_HEADER_FUNCS(header_owner, struct btrfs_header, owner, 64);
1782 BTRFS_SETGET_HEADER_FUNCS(header_nritems, struct btrfs_header, nritems, 32);
1783 BTRFS_SETGET_HEADER_FUNCS(header_flags, struct btrfs_header, flags, 64);
1784 BTRFS_SETGET_HEADER_FUNCS(header_level, struct btrfs_header, level, 8);
1785 BTRFS_SETGET_STACK_FUNCS(stack_header_bytenr, struct btrfs_header, bytenr, 64);
1786 BTRFS_SETGET_STACK_FUNCS(stack_header_nritems, struct btrfs_header, nritems,
1787                          32);
1788 BTRFS_SETGET_STACK_FUNCS(stack_header_owner, struct btrfs_header, owner, 64);
1789 BTRFS_SETGET_STACK_FUNCS(stack_header_generation, struct btrfs_header,
1790                          generation, 64);
1791
1792 static inline int btrfs_header_flag(struct extent_buffer *eb, u64 flag)
1793 {
1794         return (btrfs_header_flags(eb) & flag) == flag;
1795 }
1796
1797 static inline int btrfs_set_header_flag(struct extent_buffer *eb, u64 flag)
1798 {
1799         u64 flags = btrfs_header_flags(eb);
1800         btrfs_set_header_flags(eb, flags | flag);
1801         return (flags & flag) == flag;
1802 }
1803
1804 static inline int btrfs_clear_header_flag(struct extent_buffer *eb, u64 flag)
1805 {
1806         u64 flags = btrfs_header_flags(eb);
1807         btrfs_set_header_flags(eb, flags & ~flag);
1808         return (flags & flag) == flag;
1809 }
1810
1811 static inline int btrfs_header_backref_rev(struct extent_buffer *eb)
1812 {
1813         u64 flags = btrfs_header_flags(eb);
1814         return flags >> BTRFS_BACKREF_REV_SHIFT;
1815 }
1816
1817 static inline void btrfs_set_header_backref_rev(struct extent_buffer *eb,
1818                                                 int rev)
1819 {
1820         u64 flags = btrfs_header_flags(eb);
1821         flags &= ~BTRFS_BACKREF_REV_MASK;
1822         flags |= (u64)rev << BTRFS_BACKREF_REV_SHIFT;
1823         btrfs_set_header_flags(eb, flags);
1824 }
1825
1826 static inline unsigned long btrfs_header_fsid(void)
1827 {
1828         return offsetof(struct btrfs_header, fsid);
1829 }
1830
1831 static inline unsigned long btrfs_header_chunk_tree_uuid(struct extent_buffer *eb)
1832 {
1833         return offsetof(struct btrfs_header, chunk_tree_uuid);
1834 }
1835
1836 static inline u8 *btrfs_super_fsid(struct extent_buffer *eb)
1837 {
1838         unsigned long ptr = offsetof(struct btrfs_super_block, fsid);
1839         return (u8 *)ptr;
1840 }
1841
1842 static inline u8 *btrfs_header_csum(struct extent_buffer *eb)
1843 {
1844         unsigned long ptr = offsetof(struct btrfs_header, csum);
1845         return (u8 *)ptr;
1846 }
1847
1848 static inline struct btrfs_node *btrfs_buffer_node(struct extent_buffer *eb)
1849 {
1850         return NULL;
1851 }
1852
1853 static inline struct btrfs_leaf *btrfs_buffer_leaf(struct extent_buffer *eb)
1854 {
1855         return NULL;
1856 }
1857
1858 static inline struct btrfs_header *btrfs_buffer_header(struct extent_buffer *eb)
1859 {
1860         return NULL;
1861 }
1862
1863 static inline int btrfs_is_leaf(struct extent_buffer *eb)
1864 {
1865         return (btrfs_header_level(eb) == 0);
1866 }
1867
1868 /* struct btrfs_root_item */
1869 BTRFS_SETGET_FUNCS(disk_root_generation, struct btrfs_root_item,
1870                    generation, 64);
1871 BTRFS_SETGET_FUNCS(disk_root_refs, struct btrfs_root_item, refs, 32);
1872 BTRFS_SETGET_FUNCS(disk_root_bytenr, struct btrfs_root_item, bytenr, 64);
1873 BTRFS_SETGET_FUNCS(disk_root_level, struct btrfs_root_item, level, 8);
1874
1875 BTRFS_SETGET_STACK_FUNCS(root_generation, struct btrfs_root_item,
1876                          generation, 64);
1877 BTRFS_SETGET_STACK_FUNCS(root_bytenr, struct btrfs_root_item, bytenr, 64);
1878 BTRFS_SETGET_STACK_FUNCS(root_level, struct btrfs_root_item, level, 8);
1879 BTRFS_SETGET_STACK_FUNCS(root_dirid, struct btrfs_root_item, root_dirid, 64);
1880 BTRFS_SETGET_STACK_FUNCS(root_refs, struct btrfs_root_item, refs, 32);
1881 BTRFS_SETGET_STACK_FUNCS(root_flags, struct btrfs_root_item, flags, 64);
1882 BTRFS_SETGET_STACK_FUNCS(root_used, struct btrfs_root_item, bytes_used, 64);
1883 BTRFS_SETGET_STACK_FUNCS(root_limit, struct btrfs_root_item, byte_limit, 64);
1884 BTRFS_SETGET_STACK_FUNCS(root_last_snapshot, struct btrfs_root_item,
1885                          last_snapshot, 64);
1886 BTRFS_SETGET_STACK_FUNCS(root_generation_v2, struct btrfs_root_item,
1887                          generation_v2, 64);
1888 BTRFS_SETGET_STACK_FUNCS(root_ctransid, struct btrfs_root_item,
1889                          ctransid, 64);
1890 BTRFS_SETGET_STACK_FUNCS(root_otransid, struct btrfs_root_item,
1891                          otransid, 64);
1892 BTRFS_SETGET_STACK_FUNCS(root_stransid, struct btrfs_root_item,
1893                          stransid, 64);
1894 BTRFS_SETGET_STACK_FUNCS(root_rtransid, struct btrfs_root_item,
1895                          rtransid, 64);
1896
1897 /* struct btrfs_root_backup */
1898 BTRFS_SETGET_STACK_FUNCS(backup_tree_root, struct btrfs_root_backup,
1899                    tree_root, 64);
1900 BTRFS_SETGET_STACK_FUNCS(backup_tree_root_gen, struct btrfs_root_backup,
1901                    tree_root_gen, 64);
1902 BTRFS_SETGET_STACK_FUNCS(backup_tree_root_level, struct btrfs_root_backup,
1903                    tree_root_level, 8);
1904
1905 BTRFS_SETGET_STACK_FUNCS(backup_chunk_root, struct btrfs_root_backup,
1906                    chunk_root, 64);
1907 BTRFS_SETGET_STACK_FUNCS(backup_chunk_root_gen, struct btrfs_root_backup,
1908                    chunk_root_gen, 64);
1909 BTRFS_SETGET_STACK_FUNCS(backup_chunk_root_level, struct btrfs_root_backup,
1910                    chunk_root_level, 8);
1911
1912 BTRFS_SETGET_STACK_FUNCS(backup_extent_root, struct btrfs_root_backup,
1913                    extent_root, 64);
1914 BTRFS_SETGET_STACK_FUNCS(backup_extent_root_gen, struct btrfs_root_backup,
1915                    extent_root_gen, 64);
1916 BTRFS_SETGET_STACK_FUNCS(backup_extent_root_level, struct btrfs_root_backup,
1917                    extent_root_level, 8);
1918
1919 BTRFS_SETGET_STACK_FUNCS(backup_fs_root, struct btrfs_root_backup,
1920                    fs_root, 64);
1921 BTRFS_SETGET_STACK_FUNCS(backup_fs_root_gen, struct btrfs_root_backup,
1922                    fs_root_gen, 64);
1923 BTRFS_SETGET_STACK_FUNCS(backup_fs_root_level, struct btrfs_root_backup,
1924                    fs_root_level, 8);
1925
1926 BTRFS_SETGET_STACK_FUNCS(backup_dev_root, struct btrfs_root_backup,
1927                    dev_root, 64);
1928 BTRFS_SETGET_STACK_FUNCS(backup_dev_root_gen, struct btrfs_root_backup,
1929                    dev_root_gen, 64);
1930 BTRFS_SETGET_STACK_FUNCS(backup_dev_root_level, struct btrfs_root_backup,
1931                    dev_root_level, 8);
1932
1933 BTRFS_SETGET_STACK_FUNCS(backup_csum_root, struct btrfs_root_backup,
1934                    csum_root, 64);
1935 BTRFS_SETGET_STACK_FUNCS(backup_csum_root_gen, struct btrfs_root_backup,
1936                    csum_root_gen, 64);
1937 BTRFS_SETGET_STACK_FUNCS(backup_csum_root_level, struct btrfs_root_backup,
1938                    csum_root_level, 8);
1939 BTRFS_SETGET_STACK_FUNCS(backup_total_bytes, struct btrfs_root_backup,
1940                    total_bytes, 64);
1941 BTRFS_SETGET_STACK_FUNCS(backup_bytes_used, struct btrfs_root_backup,
1942                    bytes_used, 64);
1943 BTRFS_SETGET_STACK_FUNCS(backup_num_devices, struct btrfs_root_backup,
1944                    num_devices, 64);
1945
1946 /* struct btrfs_super_block */
1947
1948 BTRFS_SETGET_STACK_FUNCS(super_bytenr, struct btrfs_super_block, bytenr, 64);
1949 BTRFS_SETGET_STACK_FUNCS(super_flags, struct btrfs_super_block, flags, 64);
1950 BTRFS_SETGET_STACK_FUNCS(super_generation, struct btrfs_super_block,
1951                          generation, 64);
1952 BTRFS_SETGET_STACK_FUNCS(super_root, struct btrfs_super_block, root, 64);
1953 BTRFS_SETGET_STACK_FUNCS(super_sys_array_size,
1954                          struct btrfs_super_block, sys_chunk_array_size, 32);
1955 BTRFS_SETGET_STACK_FUNCS(super_chunk_root_generation,
1956                          struct btrfs_super_block, chunk_root_generation, 64);
1957 BTRFS_SETGET_STACK_FUNCS(super_root_level, struct btrfs_super_block,
1958                          root_level, 8);
1959 BTRFS_SETGET_STACK_FUNCS(super_chunk_root, struct btrfs_super_block,
1960                          chunk_root, 64);
1961 BTRFS_SETGET_STACK_FUNCS(super_chunk_root_level, struct btrfs_super_block,
1962                          chunk_root_level, 8);
1963 BTRFS_SETGET_STACK_FUNCS(super_log_root, struct btrfs_super_block,
1964                          log_root, 64);
1965 BTRFS_SETGET_STACK_FUNCS(super_log_root_transid, struct btrfs_super_block,
1966                          log_root_transid, 64);
1967 BTRFS_SETGET_STACK_FUNCS(super_log_root_level, struct btrfs_super_block,
1968                          log_root_level, 8);
1969 BTRFS_SETGET_STACK_FUNCS(super_total_bytes, struct btrfs_super_block,
1970                          total_bytes, 64);
1971 BTRFS_SETGET_STACK_FUNCS(super_bytes_used, struct btrfs_super_block,
1972                          bytes_used, 64);
1973 BTRFS_SETGET_STACK_FUNCS(super_sectorsize, struct btrfs_super_block,
1974                          sectorsize, 32);
1975 BTRFS_SETGET_STACK_FUNCS(super_nodesize, struct btrfs_super_block,
1976                          nodesize, 32);
1977 BTRFS_SETGET_STACK_FUNCS(super_leafsize, struct btrfs_super_block,
1978                          leafsize, 32);
1979 BTRFS_SETGET_STACK_FUNCS(super_stripesize, struct btrfs_super_block,
1980                          stripesize, 32);
1981 BTRFS_SETGET_STACK_FUNCS(super_root_dir, struct btrfs_super_block,
1982                          root_dir_objectid, 64);
1983 BTRFS_SETGET_STACK_FUNCS(super_num_devices, struct btrfs_super_block,
1984                          num_devices, 64);
1985 BTRFS_SETGET_STACK_FUNCS(super_compat_flags, struct btrfs_super_block,
1986                          compat_flags, 64);
1987 BTRFS_SETGET_STACK_FUNCS(super_compat_ro_flags, struct btrfs_super_block,
1988                          compat_ro_flags, 64);
1989 BTRFS_SETGET_STACK_FUNCS(super_incompat_flags, struct btrfs_super_block,
1990                          incompat_flags, 64);
1991 BTRFS_SETGET_STACK_FUNCS(super_csum_type, struct btrfs_super_block,
1992                          csum_type, 16);
1993 BTRFS_SETGET_STACK_FUNCS(super_cache_generation, struct btrfs_super_block,
1994                          cache_generation, 64);
1995 BTRFS_SETGET_STACK_FUNCS(super_uuid_tree_generation, struct btrfs_super_block,
1996                          uuid_tree_generation, 64);
1997 BTRFS_SETGET_STACK_FUNCS(super_magic, struct btrfs_super_block, magic, 64);
1998
1999 static inline int btrfs_super_csum_size(struct btrfs_super_block *s)
2000 {
2001         int t = btrfs_super_csum_type(s);
2002         BUG_ON(t >= ARRAY_SIZE(btrfs_csum_sizes));
2003         return btrfs_csum_sizes[t];
2004 }
2005
2006 static inline unsigned long btrfs_leaf_data(struct extent_buffer *l)
2007 {
2008         return offsetof(struct btrfs_leaf, items);
2009 }
2010
2011 /* struct btrfs_file_extent_item */
2012 BTRFS_SETGET_FUNCS(file_extent_type, struct btrfs_file_extent_item, type, 8);
2013 BTRFS_SETGET_STACK_FUNCS(stack_file_extent_type, struct btrfs_file_extent_item, type, 8);
2014
2015 static inline unsigned long btrfs_file_extent_inline_start(struct
2016                                                    btrfs_file_extent_item *e)
2017 {
2018         unsigned long offset = (unsigned long)e;
2019         offset += offsetof(struct btrfs_file_extent_item, disk_bytenr);
2020         return offset;
2021 }
2022
2023 static inline u32 btrfs_file_extent_calc_inline_size(u32 datasize)
2024 {
2025         return offsetof(struct btrfs_file_extent_item, disk_bytenr) + datasize;
2026 }
2027
2028 BTRFS_SETGET_FUNCS(file_extent_disk_bytenr, struct btrfs_file_extent_item,
2029                    disk_bytenr, 64);
2030 BTRFS_SETGET_STACK_FUNCS(stack_file_extent_disk_bytenr, struct btrfs_file_extent_item,
2031                    disk_bytenr, 64);
2032 BTRFS_SETGET_FUNCS(file_extent_generation, struct btrfs_file_extent_item,
2033                    generation, 64);
2034 BTRFS_SETGET_STACK_FUNCS(stack_file_extent_generation, struct btrfs_file_extent_item,
2035                    generation, 64);
2036 BTRFS_SETGET_FUNCS(file_extent_disk_num_bytes, struct btrfs_file_extent_item,
2037                    disk_num_bytes, 64);
2038 BTRFS_SETGET_FUNCS(file_extent_offset, struct btrfs_file_extent_item,
2039                   offset, 64);
2040 BTRFS_SETGET_STACK_FUNCS(stack_file_extent_offset, struct btrfs_file_extent_item,
2041                   offset, 64);
2042 BTRFS_SETGET_FUNCS(file_extent_num_bytes, struct btrfs_file_extent_item,
2043                    num_bytes, 64);
2044 BTRFS_SETGET_STACK_FUNCS(stack_file_extent_num_bytes, struct btrfs_file_extent_item,
2045                    num_bytes, 64);
2046 BTRFS_SETGET_FUNCS(file_extent_ram_bytes, struct btrfs_file_extent_item,
2047                    ram_bytes, 64);
2048 BTRFS_SETGET_STACK_FUNCS(stack_file_extent_ram_bytes, struct btrfs_file_extent_item,
2049                    ram_bytes, 64);
2050 BTRFS_SETGET_FUNCS(file_extent_compression, struct btrfs_file_extent_item,
2051                    compression, 8);
2052 BTRFS_SETGET_STACK_FUNCS(stack_file_extent_compression, struct btrfs_file_extent_item,
2053                    compression, 8);
2054 BTRFS_SETGET_FUNCS(file_extent_encryption, struct btrfs_file_extent_item,
2055                    encryption, 8);
2056 BTRFS_SETGET_FUNCS(file_extent_other_encoding, struct btrfs_file_extent_item,
2057                    other_encoding, 16);
2058
2059 /* btrfs_qgroup_status_item */
2060 BTRFS_SETGET_FUNCS(qgroup_status_version, struct btrfs_qgroup_status_item,
2061                    version, 64);
2062 BTRFS_SETGET_FUNCS(qgroup_status_generation, struct btrfs_qgroup_status_item,
2063                    generation, 64);
2064 BTRFS_SETGET_FUNCS(qgroup_status_flags, struct btrfs_qgroup_status_item,
2065                    flags, 64);
2066 BTRFS_SETGET_FUNCS(qgroup_status_scan, struct btrfs_qgroup_status_item,
2067                    scan, 64);
2068
2069 /* btrfs_qgroup_info_item */
2070 BTRFS_SETGET_FUNCS(qgroup_info_generation, struct btrfs_qgroup_info_item,
2071                    generation, 64);
2072 BTRFS_SETGET_FUNCS(qgroup_info_referenced, struct btrfs_qgroup_info_item,
2073                    referenced, 64);
2074 BTRFS_SETGET_FUNCS(qgroup_info_referenced_compressed,
2075                    struct btrfs_qgroup_info_item, referenced_compressed, 64);
2076 BTRFS_SETGET_FUNCS(qgroup_info_exclusive, struct btrfs_qgroup_info_item,
2077                    exclusive, 64);
2078 BTRFS_SETGET_FUNCS(qgroup_info_exclusive_compressed,
2079                    struct btrfs_qgroup_info_item, exclusive_compressed, 64);
2080
2081 BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_generation,
2082                          struct btrfs_qgroup_info_item, generation, 64);
2083 BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_referenced,
2084                          struct btrfs_qgroup_info_item, referenced, 64);
2085 BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_referenced_compressed,
2086                    struct btrfs_qgroup_info_item, referenced_compressed, 64);
2087 BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_exclusive,
2088                          struct btrfs_qgroup_info_item, exclusive, 64);
2089 BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_exclusive_compressed,
2090                    struct btrfs_qgroup_info_item, exclusive_compressed, 64);
2091
2092 /* btrfs_qgroup_limit_item */
2093 BTRFS_SETGET_FUNCS(qgroup_limit_flags, struct btrfs_qgroup_limit_item,
2094                    flags, 64);
2095 BTRFS_SETGET_FUNCS(qgroup_limit_max_referenced, struct btrfs_qgroup_limit_item,
2096                    max_referenced, 64);
2097 BTRFS_SETGET_FUNCS(qgroup_limit_max_exclusive, struct btrfs_qgroup_limit_item,
2098                    max_exclusive, 64);
2099 BTRFS_SETGET_FUNCS(qgroup_limit_rsv_referenced, struct btrfs_qgroup_limit_item,
2100                    rsv_referenced, 64);
2101 BTRFS_SETGET_FUNCS(qgroup_limit_rsv_exclusive, struct btrfs_qgroup_limit_item,
2102                    rsv_exclusive, 64);
2103
2104 BTRFS_SETGET_STACK_FUNCS(stack_qgroup_limit_flags,
2105                          struct btrfs_qgroup_limit_item, flags, 64);
2106 BTRFS_SETGET_STACK_FUNCS(stack_qgroup_limit_max_referenced,
2107                          struct btrfs_qgroup_limit_item, max_referenced, 64);
2108 BTRFS_SETGET_STACK_FUNCS(stack_qgroup_limit_max_exclusive,
2109                          struct btrfs_qgroup_limit_item, max_exclusive, 64);
2110 BTRFS_SETGET_STACK_FUNCS(stack_qgroup_limit_rsv_referenced,
2111                          struct btrfs_qgroup_limit_item, rsv_referenced, 64);
2112 BTRFS_SETGET_STACK_FUNCS(stack_qgroup_limit_rsv_exclusive,
2113                          struct btrfs_qgroup_limit_item, rsv_exclusive, 64);
2114
2115 /*
2116  * this returns the number of bytes used by the item on disk, minus the
2117  * size of any extent headers.  If a file is compressed on disk, this is
2118  * the compressed size
2119  */
2120 static inline u32 btrfs_file_extent_inline_item_len(struct extent_buffer *eb,
2121                                                     struct btrfs_item *e)
2122 {
2123        unsigned long offset;
2124        offset = offsetof(struct btrfs_file_extent_item, disk_bytenr);
2125        return btrfs_item_size(eb, e) - offset;
2126 }
2127
2128 /* this returns the number of file bytes represented by the inline item.
2129  * If an item is compressed, this is the uncompressed size
2130  */
2131 static inline u32 btrfs_file_extent_inline_len(struct extent_buffer *eb,
2132                                                int slot,
2133                                                struct btrfs_file_extent_item *fi)
2134 {
2135         /*
2136          * return the space used on disk if this item isn't
2137          * compressed or encoded
2138          */
2139         if (btrfs_file_extent_compression(eb, fi) == 0 &&
2140             btrfs_file_extent_encryption(eb, fi) == 0 &&
2141             btrfs_file_extent_other_encoding(eb, fi) == 0) {
2142                 return btrfs_file_extent_inline_item_len(eb,
2143                                                          btrfs_item_nr(slot));
2144         }
2145
2146         /* otherwise use the ram bytes field */
2147         return btrfs_file_extent_ram_bytes(eb, fi);
2148 }
2149
2150 static inline u32 btrfs_level_size(struct btrfs_root *root, int level) {
2151         if (level == 0)
2152                 return root->leafsize;
2153         return root->nodesize;
2154 }
2155
2156 static inline int btrfs_fs_incompat(struct btrfs_fs_info *fs_info, u64 flag)
2157 {
2158         struct btrfs_super_block *disk_super;
2159         disk_super = fs_info->super_copy;
2160         return !!(btrfs_super_incompat_flags(disk_super) & flag);
2161 }
2162
2163 /* helper function to cast into the data area of the leaf. */
2164 #define btrfs_item_ptr(leaf, slot, type) \
2165         ((type *)(btrfs_leaf_data(leaf) + \
2166         btrfs_item_offset_nr(leaf, slot)))
2167
2168 #define btrfs_item_ptr_offset(leaf, slot) \
2169         ((unsigned long)(btrfs_leaf_data(leaf) + \
2170         btrfs_item_offset_nr(leaf, slot)))
2171
2172 /* extent-tree.c */
2173 int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2174                          struct btrfs_root *root,
2175                          u64 num_bytes, u64 empty_size,
2176                          u64 hint_byte, u64 search_end,
2177                          struct btrfs_key *ins, int data);
2178 int btrfs_fix_block_accounting(struct btrfs_trans_handle *trans,
2179                                  struct btrfs_root *root);
2180 void btrfs_pin_extent(struct btrfs_fs_info *fs_info, u64 bytenr, u64 num_bytes);
2181 void btrfs_unpin_extent(struct btrfs_fs_info *fs_info,
2182                         u64 bytenr, u64 num_bytes);
2183 int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
2184                          struct btrfs_root *root);
2185 struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
2186                                                          btrfs_fs_info *info,
2187                                                          u64 bytenr);
2188 struct btrfs_block_group_cache *btrfs_lookup_first_block_group(struct
2189                                                        btrfs_fs_info *info,
2190                                                        u64 bytenr);
2191 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
2192                                         struct btrfs_root *root,
2193                                         u32 blocksize, u64 root_objectid,
2194                                         struct btrfs_disk_key *key, int level,
2195                                         u64 hint, u64 empty_size);
2196 int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
2197                        struct btrfs_root *root,
2198                        u64 num_bytes, u64 parent,
2199                        u64 root_objectid, u64 ref_generation,
2200                        u64 owner, u64 empty_size, u64 hint_byte,
2201                        u64 search_end, struct btrfs_key *ins, int data);
2202 int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
2203                              struct btrfs_root *root, u64 bytenr,
2204                              u64 offset, int metadata, u64 *refs, u64 *flags);
2205 int btrfs_set_block_flags(struct btrfs_trans_handle *trans,
2206                           struct btrfs_root *root,
2207                           u64 bytenr, int level, u64 flags);
2208 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2209                   struct extent_buffer *buf, int record_parent);
2210 int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2211                   struct extent_buffer *buf, int record_parent);
2212 int btrfs_free_extent(struct btrfs_trans_handle *trans,
2213                       struct btrfs_root *root,
2214                       u64 bytenr, u64 num_bytes, u64 parent,
2215                       u64 root_objectid, u64 owner, u64 offset);
2216 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
2217                                struct btrfs_root *root,
2218                                struct extent_io_tree *unpin);
2219 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
2220                                 struct btrfs_root *root,
2221                                 u64 bytenr, u64 num_bytes, u64 parent,
2222                                 u64 root_objectid, u64 ref_generation,
2223                                 u64 owner_objectid);
2224 int btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
2225                             struct btrfs_root *root, u64 bytenr,
2226                             u64 orig_parent, u64 parent,
2227                             u64 root_objectid, u64 ref_generation,
2228                             u64 owner_objectid);
2229 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
2230                                     struct btrfs_root *root);
2231 int btrfs_free_block_groups(struct btrfs_fs_info *info);
2232 int btrfs_read_block_groups(struct btrfs_root *root);
2233 struct btrfs_block_group_cache *
2234 btrfs_add_block_group(struct btrfs_fs_info *fs_info, u64 bytes_used, u64 type,
2235                       u64 chunk_objectid, u64 chunk_offset, u64 size);
2236 int btrfs_make_block_group(struct btrfs_trans_handle *trans,
2237                            struct btrfs_root *root, u64 bytes_used,
2238                            u64 type, u64 chunk_objectid, u64 chunk_offset,
2239                            u64 size);
2240 int btrfs_make_block_groups(struct btrfs_trans_handle *trans,
2241                             struct btrfs_root *root);
2242 int btrfs_update_block_group(struct btrfs_trans_handle *trans,
2243                              struct btrfs_root *root, u64 bytenr, u64 num,
2244                              int alloc, int mark_free);
2245 int btrfs_record_file_extent(struct btrfs_trans_handle *trans,
2246                               struct btrfs_root *root, u64 objectid,
2247                               struct btrfs_inode_item *inode,
2248                               u64 file_pos, u64 disk_bytenr,
2249                               u64 num_bytes);
2250 /* ctree.c */
2251 int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2);
2252 int btrfs_del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2253                    struct btrfs_path *path, int level, int slot);
2254 enum btrfs_tree_block_status
2255 btrfs_check_node(struct btrfs_root *root, struct btrfs_disk_key *parent_key,
2256                  struct extent_buffer *buf);
2257 enum btrfs_tree_block_status
2258 btrfs_check_leaf(struct btrfs_root *root, struct btrfs_disk_key *parent_key,
2259                  struct extent_buffer *buf);
2260 void reada_for_search(struct btrfs_root *root, struct btrfs_path *path,
2261                              int level, int slot, u64 objectid);
2262 struct extent_buffer *read_node_slot(struct btrfs_root *root,
2263                                    struct extent_buffer *parent, int slot);
2264 int btrfs_previous_item(struct btrfs_root *root,
2265                         struct btrfs_path *path, u64 min_objectid,
2266                         int type);
2267 int btrfs_previous_extent_item(struct btrfs_root *root,
2268                         struct btrfs_path *path, u64 min_objectid);
2269 int btrfs_cow_block(struct btrfs_trans_handle *trans,
2270                     struct btrfs_root *root, struct extent_buffer *buf,
2271                     struct extent_buffer *parent, int parent_slot,
2272                     struct extent_buffer **cow_ret);
2273 int __btrfs_cow_block(struct btrfs_trans_handle *trans,
2274                              struct btrfs_root *root,
2275                              struct extent_buffer *buf,
2276                              struct extent_buffer *parent, int parent_slot,
2277                              struct extent_buffer **cow_ret,
2278                              u64 search_start, u64 empty_size);
2279 int btrfs_copy_root(struct btrfs_trans_handle *trans,
2280                       struct btrfs_root *root,
2281                       struct extent_buffer *buf,
2282                       struct extent_buffer **cow_ret, u64 new_root_objectid);
2283 int btrfs_extend_item(struct btrfs_trans_handle *trans, struct btrfs_root
2284                       *root, struct btrfs_path *path, u32 data_size);
2285 int btrfs_truncate_item(struct btrfs_trans_handle *trans,
2286                         struct btrfs_root *root,
2287                         struct btrfs_path *path,
2288                         u32 new_size, int from_end);
2289 int btrfs_split_item(struct btrfs_trans_handle *trans,
2290                      struct btrfs_root *root,
2291                      struct btrfs_path *path,
2292                      struct btrfs_key *new_key,
2293                      unsigned long split_offset);
2294 int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
2295                       *root, struct btrfs_key *key, struct btrfs_path *p, int
2296                       ins_len, int cow);
2297 int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *found_path,
2298                 u64 iobjectid, u64 ioff, u8 key_type,
2299                 struct btrfs_key *found_key);
2300 void btrfs_release_path(struct btrfs_path *p);
2301 void add_root_to_dirty_list(struct btrfs_root *root);
2302 struct btrfs_path *btrfs_alloc_path(void);
2303 void btrfs_free_path(struct btrfs_path *p);
2304 void btrfs_init_path(struct btrfs_path *p);
2305 int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2306                    struct btrfs_path *path, int slot, int nr);
2307
2308 static inline int btrfs_del_item(struct btrfs_trans_handle *trans,
2309                                  struct btrfs_root *root,
2310                                  struct btrfs_path *path)
2311 {
2312         return btrfs_del_items(trans, root, path, path->slots[0], 1);
2313 }
2314
2315 int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
2316                       *root, struct btrfs_key *key, void *data, u32 data_size);
2317 int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
2318                              struct btrfs_root *root,
2319                              struct btrfs_path *path,
2320                              struct btrfs_key *cpu_key, u32 *data_size, int nr);
2321
2322 static inline int btrfs_insert_empty_item(struct btrfs_trans_handle *trans,
2323                                           struct btrfs_root *root,
2324                                           struct btrfs_path *path,
2325                                           struct btrfs_key *key,
2326                                           u32 data_size)
2327 {
2328         return btrfs_insert_empty_items(trans, root, path, key, &data_size, 1);
2329 }
2330
2331 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path);
2332 static inline int btrfs_next_item(struct btrfs_root *root,
2333                                   struct btrfs_path *p)
2334 {
2335         ++p->slots[0];
2336         if (p->slots[0] >= btrfs_header_nritems(p->nodes[0]))
2337                 return btrfs_next_leaf(root, p);
2338         return 0;
2339 }
2340
2341 int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path);
2342 int btrfs_leaf_free_space(struct btrfs_root *root, struct extent_buffer *leaf);
2343 void btrfs_fixup_low_keys(struct btrfs_root *root, struct btrfs_path *path,
2344                           struct btrfs_disk_key *key, int level);
2345 int btrfs_set_item_key_safe(struct btrfs_root *root, struct btrfs_path *path,
2346                             struct btrfs_key *new_key);
2347 void btrfs_set_item_key_unsafe(struct btrfs_root *root,
2348                                struct btrfs_path *path,
2349                                struct btrfs_key *new_key);
2350
2351 /* root-item.c */
2352 int btrfs_add_root_ref(struct btrfs_trans_handle *trans,
2353                        struct btrfs_root *tree_root,
2354                        u64 root_id, u8 type, u64 ref_id,
2355                        u64 dirid, u64 sequence,
2356                        const char *name, int name_len);
2357 int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
2358                       *root, struct btrfs_key *key, struct btrfs_root_item
2359                       *item);
2360 int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
2361                       *root, struct btrfs_key *key, struct btrfs_root_item
2362                       *item);
2363 int btrfs_find_last_root(struct btrfs_root *root, u64 objectid, struct
2364                          btrfs_root_item *item, struct btrfs_key *key);
2365 /* dir-item.c */
2366 int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
2367                           *root, const char *name, int name_len, u64 dir,
2368                           struct btrfs_key *location, u8 type, u64 index);
2369 struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
2370                                              struct btrfs_root *root,
2371                                              struct btrfs_path *path, u64 dir,
2372                                              const char *name, int name_len,
2373                                              int mod);
2374 struct btrfs_dir_item *btrfs_lookup_dir_index(struct btrfs_trans_handle *trans,
2375                                               struct btrfs_root *root,
2376                                               struct btrfs_path *path, u64 dir,
2377                                               const char *name, int name_len,
2378                                               u64 index, int mod);
2379 int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans,
2380                               struct btrfs_root *root,
2381                               struct btrfs_path *path,
2382                               struct btrfs_dir_item *di);
2383 int btrfs_insert_xattr_item(struct btrfs_trans_handle *trans,
2384                             struct btrfs_root *root, const char *name,
2385                             u16 name_len, const void *data, u16 data_len,
2386                             u64 dir);
2387 /* inode-map.c */
2388 int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
2389                              struct btrfs_root *fs_root,
2390                              u64 dirid, u64 *objectid);
2391
2392 /* inode-item.c */
2393 int btrfs_insert_inode_ref(struct btrfs_trans_handle *trans,
2394                            struct btrfs_root *root,
2395                            const char *name, int name_len,
2396                            u64 inode_objectid, u64 ref_objectid, u64 index);
2397 int btrfs_insert_inode(struct btrfs_trans_handle *trans, struct btrfs_root
2398                        *root, u64 objectid, struct btrfs_inode_item
2399                        *inode_item);
2400 int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root
2401                        *root, struct btrfs_path *path,
2402                        struct btrfs_key *location, int mod);
2403 struct btrfs_inode_extref *btrfs_lookup_inode_extref(struct btrfs_trans_handle
2404                 *trans, struct btrfs_path *path, struct btrfs_root *root,
2405                 u64 ino, u64 parent_ino, u64 index, const char *name,
2406                 int namelen, int ins_len);
2407 int btrfs_del_inode_extref(struct btrfs_trans_handle *trans,
2408                            struct btrfs_root *root,
2409                            const char *name, int name_len,
2410                            u64 inode_objectid, u64 ref_objectid,
2411                            u64 *index);
2412 int btrfs_insert_inode_extref(struct btrfs_trans_handle *trans,
2413                               struct btrfs_root *root,
2414                               const char *name, int name_len,
2415                               u64 inode_objectid, u64 ref_objectid, u64 index);
2416 struct btrfs_inode_ref *btrfs_lookup_inode_ref(struct btrfs_trans_handle *trans,
2417                 struct btrfs_root *root, struct btrfs_path *path,
2418                 const char *name, int namelen, u64 ino, u64 parent_ino,
2419                 u64 index, int ins_len);
2420 int btrfs_del_inode_ref(struct btrfs_trans_handle *trans,
2421                         struct btrfs_root *root, const char *name, int name_len,
2422                         u64 ino, u64 parent_ino, u64 *index);
2423
2424 /* file-item.c */
2425 int btrfs_del_csums(struct btrfs_trans_handle *trans,
2426                     struct btrfs_root *root, u64 bytenr, u64 len);
2427 int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
2428                              struct btrfs_root *root,
2429                              u64 objectid, u64 pos, u64 offset,
2430                              u64 disk_num_bytes,
2431                              u64 num_bytes);
2432 int btrfs_insert_inline_extent(struct btrfs_trans_handle *trans,
2433                                 struct btrfs_root *root, u64 objectid,
2434                                 u64 offset, char *buffer, size_t size);
2435 int btrfs_csum_file_block(struct btrfs_trans_handle *trans,
2436                           struct btrfs_root *root, u64 alloc_end,
2437                           u64 bytenr, char *data, size_t len);
2438 int btrfs_csum_truncate(struct btrfs_trans_handle *trans,
2439                         struct btrfs_root *root, struct btrfs_path *path,
2440                         u64 isize);
2441
2442 /* uuid-tree.c */
2443 int btrfs_lookup_uuid_subvol_item(int fd, const u8 *uuid, u64 *subvol_id);
2444 int btrfs_lookup_uuid_received_subvol_item(int fd, const u8 *uuid,
2445                                            u64 *subvol_id);
2446
2447 static inline int is_fstree(u64 rootid)
2448 {
2449         if (rootid == BTRFS_FS_TREE_OBJECTID ||
2450             (signed long long)rootid >= (signed long long)BTRFS_FIRST_FREE_OBJECTID)
2451                 return 1;
2452         return 0;
2453 }
2454
2455 /* inode.c */
2456 int check_dir_conflict(struct btrfs_root *root, char *name, int namelen,
2457                 u64 dir, u64 index);
2458 int btrfs_new_inode(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2459                 u64 ino, u32 mode);
2460 int btrfs_add_link(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2461                    u64 ino, u64 parent_ino, char *name, int namelen,
2462                    u8 type, u64 *index, int add_backref);
2463 int btrfs_unlink(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2464                  u64 ino, u64 parent_ino, u64 index, const char *name,
2465                  int namelen, int add_orphan);
2466 int btrfs_add_orphan_item(struct btrfs_trans_handle *trans,
2467                           struct btrfs_root *root, struct btrfs_path *path,
2468                           u64 ino);
2469 int btrfs_mkdir(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2470                 char *name, int namelen, u64 parent_ino, u64 *ino, int mode);
2471
2472 /* file.c */
2473 int btrfs_get_extent(struct btrfs_trans_handle *trans,
2474                      struct btrfs_root *root,
2475                      struct btrfs_path *path,
2476                      u64 ino, u64 offset, u64 len, int ins_len);
2477 int btrfs_punch_hole(struct btrfs_trans_handle *trans,
2478                      struct btrfs_root *root,
2479                      u64 ino, u64 offset, u64 len);
2480 #endif