"Fossies" - the Fresh Open Source Software Archive

Member "btrfs-progs-v5.4.1/ctree.h" (9 Jan 2020, 93730 Bytes) of package /linux/misc/btrfs-progs-v5.4.1.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ctree.h" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: v5.4_vs_v5.4.1.

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