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