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