Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
0004  * Copyright (c) 2013 Red Hat, Inc.
0005  * All Rights Reserved.
0006  */
0007 #ifndef __XFS_DA_FORMAT_H__
0008 #define __XFS_DA_FORMAT_H__
0009 
0010 /*
0011  * This structure is common to both leaf nodes and non-leaf nodes in the Btree.
0012  *
0013  * It is used to manage a doubly linked list of all blocks at the same
0014  * level in the Btree, and to identify which type of block this is.
0015  */
0016 #define XFS_DA_NODE_MAGIC   0xfebe  /* magic number: non-leaf blocks */
0017 #define XFS_ATTR_LEAF_MAGIC 0xfbee  /* magic number: attribute leaf blks */
0018 #define XFS_DIR2_LEAF1_MAGIC    0xd2f1  /* magic number: v2 dirlf single blks */
0019 #define XFS_DIR2_LEAFN_MAGIC    0xd2ff  /* magic number: v2 dirlf multi blks */
0020 
0021 typedef struct xfs_da_blkinfo {
0022     __be32      forw;           /* previous block in list */
0023     __be32      back;           /* following block in list */
0024     __be16      magic;          /* validity check on block */
0025     __be16      pad;            /* unused */
0026 } xfs_da_blkinfo_t;
0027 
0028 /*
0029  * CRC enabled directory structure types
0030  *
0031  * The headers change size for the additional verification information, but
0032  * otherwise the tree layouts and contents are unchanged. Hence the da btree
0033  * code can use the struct xfs_da_blkinfo for manipulating the tree links and
0034  * magic numbers without modification for both v2 and v3 nodes.
0035  */
0036 #define XFS_DA3_NODE_MAGIC  0x3ebe  /* magic number: non-leaf blocks */
0037 #define XFS_ATTR3_LEAF_MAGIC    0x3bee  /* magic number: attribute leaf blks */
0038 #define XFS_DIR3_LEAF1_MAGIC    0x3df1  /* magic number: v3 dirlf single blks */
0039 #define XFS_DIR3_LEAFN_MAGIC    0x3dff  /* magic number: v3 dirlf multi blks */
0040 
0041 struct xfs_da3_blkinfo {
0042     /*
0043      * the node link manipulation code relies on the fact that the first
0044      * element of this structure is the struct xfs_da_blkinfo so it can
0045      * ignore the differences in the rest of the structures.
0046      */
0047     struct xfs_da_blkinfo   hdr;
0048     __be32          crc;    /* CRC of block */
0049     __be64          blkno;  /* first block of the buffer */
0050     __be64          lsn;    /* sequence number of last write */
0051     uuid_t          uuid;   /* filesystem we belong to */
0052     __be64          owner;  /* inode that owns the block */
0053 };
0054 
0055 /*
0056  * This is the structure of the root and intermediate nodes in the Btree.
0057  * The leaf nodes are defined above.
0058  *
0059  * Entries are not packed.
0060  *
0061  * Since we have duplicate keys, use a binary search but always follow
0062  * all match in the block, not just the first match found.
0063  */
0064 #define XFS_DA_NODE_MAXDEPTH    5   /* max depth of Btree */
0065 
0066 typedef struct xfs_da_node_hdr {
0067     struct xfs_da_blkinfo   info;   /* block type, links, etc. */
0068     __be16          __count; /* count of active entries */
0069     __be16          __level; /* level above leaves (leaf == 0) */
0070 } xfs_da_node_hdr_t;
0071 
0072 struct xfs_da3_node_hdr {
0073     struct xfs_da3_blkinfo  info;   /* block type, links, etc. */
0074     __be16          __count; /* count of active entries */
0075     __be16          __level; /* level above leaves (leaf == 0) */
0076     __be32          __pad32;
0077 };
0078 
0079 #define XFS_DA3_NODE_CRC_OFF    (offsetof(struct xfs_da3_node_hdr, info.crc))
0080 
0081 typedef struct xfs_da_node_entry {
0082     __be32  hashval;    /* hash value for this descendant */
0083     __be32  before;     /* Btree block before this key */
0084 } xfs_da_node_entry_t;
0085 
0086 typedef struct xfs_da_intnode {
0087     struct xfs_da_node_hdr  hdr;
0088     struct xfs_da_node_entry __btree[];
0089 } xfs_da_intnode_t;
0090 
0091 struct xfs_da3_intnode {
0092     struct xfs_da3_node_hdr hdr;
0093     struct xfs_da_node_entry __btree[];
0094 };
0095 
0096 /*
0097  * Directory version 2.
0098  *
0099  * There are 4 possible formats:
0100  *  - shortform - embedded into the inode
0101  *  - single block - data with embedded leaf at the end
0102  *  - multiple data blocks, single leaf+freeindex block
0103  *  - data blocks, node and leaf blocks (btree), freeindex blocks
0104  *
0105  * Note: many node blocks structures and constants are shared with the attr
0106  * code and defined in xfs_da_btree.h.
0107  */
0108 
0109 #define XFS_DIR2_BLOCK_MAGIC    0x58443242  /* XD2B: single block dirs */
0110 #define XFS_DIR2_DATA_MAGIC 0x58443244  /* XD2D: multiblock dirs */
0111 #define XFS_DIR2_FREE_MAGIC 0x58443246  /* XD2F: free index blocks */
0112 
0113 /*
0114  * Directory Version 3 With CRCs.
0115  *
0116  * The tree formats are the same as for version 2 directories.  The difference
0117  * is in the block header and dirent formats. In many cases the v3 structures
0118  * use v2 definitions as they are no different and this makes code sharing much
0119  * easier.
0120  *
0121  * Also, the xfs_dir3_*() functions handle both v2 and v3 formats - if the
0122  * format is v2 then they switch to the existing v2 code, or the format is v3
0123  * they implement the v3 functionality. This means the existing dir2 is a mix of
0124  * xfs_dir2/xfs_dir3 calls and functions. The xfs_dir3 functions are called
0125  * where there is a difference in the formats, otherwise the code is unchanged.
0126  *
0127  * Where it is possible, the code decides what to do based on the magic numbers
0128  * in the blocks rather than feature bits in the superblock. This means the code
0129  * is as independent of the external XFS code as possible as doesn't require
0130  * passing struct xfs_mount pointers into places where it isn't really
0131  * necessary.
0132  *
0133  * Version 3 includes:
0134  *
0135  *  - a larger block header for CRC and identification purposes and so the
0136  *  offsets of all the structures inside the blocks are different.
0137  *
0138  *  - new magic numbers to be able to detect the v2/v3 types on the fly.
0139  */
0140 
0141 #define XFS_DIR3_BLOCK_MAGIC    0x58444233  /* XDB3: single block dirs */
0142 #define XFS_DIR3_DATA_MAGIC 0x58444433  /* XDD3: multiblock dirs */
0143 #define XFS_DIR3_FREE_MAGIC 0x58444633  /* XDF3: free index blocks */
0144 
0145 /*
0146  * Dirents in version 3 directories have a file type field. Additions to this
0147  * list are an on-disk format change, requiring feature bits. Valid values
0148  * are as follows:
0149  */
0150 #define XFS_DIR3_FT_UNKNOWN     0
0151 #define XFS_DIR3_FT_REG_FILE        1
0152 #define XFS_DIR3_FT_DIR         2
0153 #define XFS_DIR3_FT_CHRDEV      3
0154 #define XFS_DIR3_FT_BLKDEV      4
0155 #define XFS_DIR3_FT_FIFO        5
0156 #define XFS_DIR3_FT_SOCK        6
0157 #define XFS_DIR3_FT_SYMLINK     7
0158 #define XFS_DIR3_FT_WHT         8
0159 
0160 #define XFS_DIR3_FT_MAX         9
0161 
0162 /*
0163  * Byte offset in data block and shortform entry.
0164  */
0165 typedef uint16_t    xfs_dir2_data_off_t;
0166 #define NULLDATAOFF 0xffffU
0167 typedef uint        xfs_dir2_data_aoff_t;   /* argument form */
0168 
0169 /*
0170  * Offset in data space of a data entry.
0171  */
0172 typedef uint32_t    xfs_dir2_dataptr_t;
0173 #define XFS_DIR2_MAX_DATAPTR    ((xfs_dir2_dataptr_t)0xffffffff)
0174 #define XFS_DIR2_NULL_DATAPTR   ((xfs_dir2_dataptr_t)0)
0175 
0176 /*
0177  * Byte offset in a directory.
0178  */
0179 typedef xfs_off_t   xfs_dir2_off_t;
0180 
0181 /*
0182  * Directory block number (logical dirblk in file)
0183  */
0184 typedef uint32_t    xfs_dir2_db_t;
0185 
0186 #define XFS_INO32_SIZE  4
0187 #define XFS_INO64_SIZE  8
0188 #define XFS_INO64_DIFF  (XFS_INO64_SIZE - XFS_INO32_SIZE)
0189 
0190 #define XFS_DIR2_MAX_SHORT_INUM ((xfs_ino_t)0xffffffffULL)
0191 
0192 /*
0193  * Directory layout when stored internal to an inode.
0194  *
0195  * Small directories are packed as tightly as possible so as to fit into the
0196  * literal area of the inode.  These "shortform" directories consist of a
0197  * single xfs_dir2_sf_hdr header followed by zero or more xfs_dir2_sf_entry
0198  * structures.  Due the different inode number storage size and the variable
0199  * length name field in the xfs_dir2_sf_entry all these structure are
0200  * variable length, and the accessors in this file should be used to iterate
0201  * over them.
0202  */
0203 typedef struct xfs_dir2_sf_hdr {
0204     uint8_t         count;      /* count of entries */
0205     uint8_t         i8count;    /* count of 8-byte inode #s */
0206     uint8_t         parent[8];  /* parent dir inode number */
0207 } __packed xfs_dir2_sf_hdr_t;
0208 
0209 typedef struct xfs_dir2_sf_entry {
0210     __u8            namelen;    /* actual name length */
0211     __u8            offset[2];  /* saved offset */
0212     __u8            name[];     /* name, variable size */
0213     /*
0214      * A single byte containing the file type field follows the inode
0215      * number for version 3 directory entries.
0216      *
0217      * A 64-bit or 32-bit inode number follows here, at a variable offset
0218      * after the name.
0219      */
0220 } __packed xfs_dir2_sf_entry_t;
0221 
0222 static inline int xfs_dir2_sf_hdr_size(int i8count)
0223 {
0224     return sizeof(struct xfs_dir2_sf_hdr) -
0225         (i8count == 0) * XFS_INO64_DIFF;
0226 }
0227 
0228 static inline xfs_dir2_data_aoff_t
0229 xfs_dir2_sf_get_offset(xfs_dir2_sf_entry_t *sfep)
0230 {
0231     return get_unaligned_be16(sfep->offset);
0232 }
0233 
0234 static inline void
0235 xfs_dir2_sf_put_offset(xfs_dir2_sf_entry_t *sfep, xfs_dir2_data_aoff_t off)
0236 {
0237     put_unaligned_be16(off, sfep->offset);
0238 }
0239 
0240 static inline struct xfs_dir2_sf_entry *
0241 xfs_dir2_sf_firstentry(struct xfs_dir2_sf_hdr *hdr)
0242 {
0243     return (struct xfs_dir2_sf_entry *)
0244         ((char *)hdr + xfs_dir2_sf_hdr_size(hdr->i8count));
0245 }
0246 
0247 /*
0248  * Data block structures.
0249  *
0250  * A pure data block looks like the following drawing on disk:
0251  *
0252  *    +-------------------------------------------------+
0253  *    | xfs_dir2_data_hdr_t                             |
0254  *    +-------------------------------------------------+
0255  *    | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
0256  *    | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
0257  *    | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
0258  *    | ...                                             |
0259  *    +-------------------------------------------------+
0260  *    | unused space                                    |
0261  *    +-------------------------------------------------+
0262  *
0263  * As all the entries are variable size structures the accessors below should
0264  * be used to iterate over them.
0265  *
0266  * In addition to the pure data blocks for the data and node formats,
0267  * most structures are also used for the combined data/freespace "block"
0268  * format below.
0269  */
0270 
0271 #define XFS_DIR2_DATA_ALIGN_LOG 3       /* i.e., 8 bytes */
0272 #define XFS_DIR2_DATA_ALIGN (1 << XFS_DIR2_DATA_ALIGN_LOG)
0273 #define XFS_DIR2_DATA_FREE_TAG  0xffff
0274 #define XFS_DIR2_DATA_FD_COUNT  3
0275 
0276 /*
0277  * Directory address space divided into sections,
0278  * spaces separated by 32GB.
0279  */
0280 #define XFS_DIR2_MAX_SPACES 3
0281 #define XFS_DIR2_SPACE_SIZE (1ULL << (32 + XFS_DIR2_DATA_ALIGN_LOG))
0282 #define XFS_DIR2_DATA_SPACE 0
0283 #define XFS_DIR2_DATA_OFFSET    (XFS_DIR2_DATA_SPACE * XFS_DIR2_SPACE_SIZE)
0284 
0285 /*
0286  * Describe a free area in the data block.
0287  *
0288  * The freespace will be formatted as a xfs_dir2_data_unused_t.
0289  */
0290 typedef struct xfs_dir2_data_free {
0291     __be16          offset;     /* start of freespace */
0292     __be16          length;     /* length of freespace */
0293 } xfs_dir2_data_free_t;
0294 
0295 /*
0296  * Header for the data blocks.
0297  *
0298  * The code knows that XFS_DIR2_DATA_FD_COUNT is 3.
0299  */
0300 typedef struct xfs_dir2_data_hdr {
0301     __be32          magic;      /* XFS_DIR2_DATA_MAGIC or */
0302                         /* XFS_DIR2_BLOCK_MAGIC */
0303     xfs_dir2_data_free_t    bestfree[XFS_DIR2_DATA_FD_COUNT];
0304 } xfs_dir2_data_hdr_t;
0305 
0306 /*
0307  * define a structure for all the verification fields we are adding to the
0308  * directory block structures. This will be used in several structures.
0309  * The magic number must be the first entry to align with all the dir2
0310  * structures so we determine how to decode them just by the magic number.
0311  */
0312 struct xfs_dir3_blk_hdr {
0313     __be32          magic;  /* magic number */
0314     __be32          crc;    /* CRC of block */
0315     __be64          blkno;  /* first block of the buffer */
0316     __be64          lsn;    /* sequence number of last write */
0317     uuid_t          uuid;   /* filesystem we belong to */
0318     __be64          owner;  /* inode that owns the block */
0319 };
0320 
0321 struct xfs_dir3_data_hdr {
0322     struct xfs_dir3_blk_hdr hdr;
0323     xfs_dir2_data_free_t    best_free[XFS_DIR2_DATA_FD_COUNT];
0324     __be32          pad;    /* 64 bit alignment */
0325 };
0326 
0327 #define XFS_DIR3_DATA_CRC_OFF  offsetof(struct xfs_dir3_data_hdr, hdr.crc)
0328 
0329 /*
0330  * Active entry in a data block.
0331  *
0332  * Aligned to 8 bytes.  After the variable length name field there is a
0333  * 2 byte tag field, which can be accessed using xfs_dir3_data_entry_tag_p.
0334  *
0335  * For dir3 structures, there is file type field between the name and the tag.
0336  * This can only be manipulated by helper functions. It is packed hard against
0337  * the end of the name so any padding for rounding is between the file type and
0338  * the tag.
0339  */
0340 typedef struct xfs_dir2_data_entry {
0341     __be64          inumber;    /* inode number */
0342     __u8            namelen;    /* name length */
0343     __u8            name[];     /* name bytes, no null */
0344      /* __u8            filetype; */    /* type of inode we point to */
0345      /* __be16                  tag; */     /* starting offset of us */
0346 } xfs_dir2_data_entry_t;
0347 
0348 /*
0349  * Unused entry in a data block.
0350  *
0351  * Aligned to 8 bytes.  Tag appears as the last 2 bytes and must be accessed
0352  * using xfs_dir2_data_unused_tag_p.
0353  */
0354 typedef struct xfs_dir2_data_unused {
0355     __be16          freetag;    /* XFS_DIR2_DATA_FREE_TAG */
0356     __be16          length;     /* total free length */
0357                         /* variable offset */
0358     __be16          tag;        /* starting offset of us */
0359 } xfs_dir2_data_unused_t;
0360 
0361 /*
0362  * Pointer to a freespace's tag word.
0363  */
0364 static inline __be16 *
0365 xfs_dir2_data_unused_tag_p(struct xfs_dir2_data_unused *dup)
0366 {
0367     return (__be16 *)((char *)dup +
0368             be16_to_cpu(dup->length) - sizeof(__be16));
0369 }
0370 
0371 /*
0372  * Leaf block structures.
0373  *
0374  * A pure leaf block looks like the following drawing on disk:
0375  *
0376  *    +---------------------------+
0377  *    | xfs_dir2_leaf_hdr_t       |
0378  *    +---------------------------+
0379  *    | xfs_dir2_leaf_entry_t     |
0380  *    | xfs_dir2_leaf_entry_t     |
0381  *    | xfs_dir2_leaf_entry_t     |
0382  *    | xfs_dir2_leaf_entry_t     |
0383  *    | ...                       |
0384  *    +---------------------------+
0385  *    | xfs_dir2_data_off_t       |
0386  *    | xfs_dir2_data_off_t       |
0387  *    | xfs_dir2_data_off_t       |
0388  *    | ...                       |
0389  *    +---------------------------+
0390  *    | xfs_dir2_leaf_tail_t      |
0391  *    +---------------------------+
0392  *
0393  * The xfs_dir2_data_off_t members (bests) and tail are at the end of the block
0394  * for single-leaf (magic = XFS_DIR2_LEAF1_MAGIC) blocks only, but not present
0395  * for directories with separate leaf nodes and free space blocks
0396  * (magic = XFS_DIR2_LEAFN_MAGIC).
0397  *
0398  * As all the entries are variable size structures the accessors below should
0399  * be used to iterate over them.
0400  */
0401 
0402 /*
0403  * Offset of the leaf/node space.  First block in this space
0404  * is the btree root.
0405  */
0406 #define XFS_DIR2_LEAF_SPACE 1
0407 #define XFS_DIR2_LEAF_OFFSET    (XFS_DIR2_LEAF_SPACE * XFS_DIR2_SPACE_SIZE)
0408 
0409 /*
0410  * Leaf block header.
0411  */
0412 typedef struct xfs_dir2_leaf_hdr {
0413     xfs_da_blkinfo_t    info;       /* header for da routines */
0414     __be16          count;      /* count of entries */
0415     __be16          stale;      /* count of stale entries */
0416 } xfs_dir2_leaf_hdr_t;
0417 
0418 struct xfs_dir3_leaf_hdr {
0419     struct xfs_da3_blkinfo  info;       /* header for da routines */
0420     __be16          count;      /* count of entries */
0421     __be16          stale;      /* count of stale entries */
0422     __be32          pad;        /* 64 bit alignment */
0423 };
0424 
0425 /*
0426  * Leaf block entry.
0427  */
0428 typedef struct xfs_dir2_leaf_entry {
0429     __be32          hashval;    /* hash value of name */
0430     __be32          address;    /* address of data entry */
0431 } xfs_dir2_leaf_entry_t;
0432 
0433 /*
0434  * Leaf block tail.
0435  */
0436 typedef struct xfs_dir2_leaf_tail {
0437     __be32          bestcount;
0438 } xfs_dir2_leaf_tail_t;
0439 
0440 /*
0441  * Leaf block.
0442  */
0443 typedef struct xfs_dir2_leaf {
0444     xfs_dir2_leaf_hdr_t hdr;            /* leaf header */
0445     xfs_dir2_leaf_entry_t   __ents[];       /* entries */
0446 } xfs_dir2_leaf_t;
0447 
0448 struct xfs_dir3_leaf {
0449     struct xfs_dir3_leaf_hdr    hdr;        /* leaf header */
0450     struct xfs_dir2_leaf_entry  __ents[];   /* entries */
0451 };
0452 
0453 #define XFS_DIR3_LEAF_CRC_OFF  offsetof(struct xfs_dir3_leaf_hdr, info.crc)
0454 
0455 /*
0456  * Get address of the bests array in the single-leaf block.
0457  */
0458 static inline __be16 *
0459 xfs_dir2_leaf_bests_p(struct xfs_dir2_leaf_tail *ltp)
0460 {
0461     return (__be16 *)ltp - be32_to_cpu(ltp->bestcount);
0462 }
0463 
0464 /*
0465  * Free space block definitions for the node format.
0466  */
0467 
0468 /*
0469  * Offset of the freespace index.
0470  */
0471 #define XFS_DIR2_FREE_SPACE 2
0472 #define XFS_DIR2_FREE_OFFSET    (XFS_DIR2_FREE_SPACE * XFS_DIR2_SPACE_SIZE)
0473 
0474 typedef struct xfs_dir2_free_hdr {
0475     __be32          magic;      /* XFS_DIR2_FREE_MAGIC */
0476     __be32          firstdb;    /* db of first entry */
0477     __be32          nvalid;     /* count of valid entries */
0478     __be32          nused;      /* count of used entries */
0479 } xfs_dir2_free_hdr_t;
0480 
0481 typedef struct xfs_dir2_free {
0482     xfs_dir2_free_hdr_t hdr;        /* block header */
0483     __be16          bests[];    /* best free counts */
0484                         /* unused entries are -1 */
0485 } xfs_dir2_free_t;
0486 
0487 struct xfs_dir3_free_hdr {
0488     struct xfs_dir3_blk_hdr hdr;
0489     __be32          firstdb;    /* db of first entry */
0490     __be32          nvalid;     /* count of valid entries */
0491     __be32          nused;      /* count of used entries */
0492     __be32          pad;        /* 64 bit alignment */
0493 };
0494 
0495 struct xfs_dir3_free {
0496     struct xfs_dir3_free_hdr hdr;
0497     __be16          bests[];    /* best free counts */
0498                         /* unused entries are -1 */
0499 };
0500 
0501 #define XFS_DIR3_FREE_CRC_OFF  offsetof(struct xfs_dir3_free, hdr.hdr.crc)
0502 
0503 /*
0504  * Single block format.
0505  *
0506  * The single block format looks like the following drawing on disk:
0507  *
0508  *    +-------------------------------------------------+
0509  *    | xfs_dir2_data_hdr_t                             |
0510  *    +-------------------------------------------------+
0511  *    | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
0512  *    | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t |
0513  *    | xfs_dir2_data_entry_t OR xfs_dir2_data_unused_t :
0514  *    | ...                                             |
0515  *    +-------------------------------------------------+
0516  *    | unused space                                    |
0517  *    +-------------------------------------------------+
0518  *    | ...                                             |
0519  *    | xfs_dir2_leaf_entry_t                           |
0520  *    | xfs_dir2_leaf_entry_t                           |
0521  *    +-------------------------------------------------+
0522  *    | xfs_dir2_block_tail_t                           |
0523  *    +-------------------------------------------------+
0524  *
0525  * As all the entries are variable size structures the accessors below should
0526  * be used to iterate over them.
0527  */
0528 
0529 typedef struct xfs_dir2_block_tail {
0530     __be32      count;          /* count of leaf entries */
0531     __be32      stale;          /* count of stale lf entries */
0532 } xfs_dir2_block_tail_t;
0533 
0534 /*
0535  * Pointer to the leaf entries embedded in a data block (1-block format)
0536  */
0537 static inline struct xfs_dir2_leaf_entry *
0538 xfs_dir2_block_leaf_p(struct xfs_dir2_block_tail *btp)
0539 {
0540     return ((struct xfs_dir2_leaf_entry *)btp) - be32_to_cpu(btp->count);
0541 }
0542 
0543 
0544 /*
0545  * Attribute storage layout
0546  *
0547  * Attribute lists are structured around Btrees where all the data
0548  * elements are in the leaf nodes.  Attribute names are hashed into an int,
0549  * then that int is used as the index into the Btree.  Since the hashval
0550  * of an attribute name may not be unique, we may have duplicate keys.  The
0551  * internal links in the Btree are logical block offsets into the file.
0552  *
0553  * Struct leaf_entry's are packed from the top.  Name/values grow from the
0554  * bottom but are not packed.  The freemap contains run-length-encoded entries
0555  * for the free bytes after the leaf_entry's, but only the N largest such,
0556  * smaller runs are dropped.  When the freemap doesn't show enough space
0557  * for an allocation, we compact the name/value area and try again.  If we
0558  * still don't have enough space, then we have to split the block.  The
0559  * name/value structs (both local and remote versions) must be 32bit aligned.
0560  *
0561  * Since we have duplicate hash keys, for each key that matches, compare
0562  * the actual name string.  The root and intermediate node search always
0563  * takes the first-in-the-block key match found, so we should only have
0564  * to work "forw"ard.  If none matches, continue with the "forw"ard leaf
0565  * nodes until the hash key changes or the attribute name is found.
0566  *
0567  * We store the fact that an attribute is a ROOT/USER/SECURE attribute in
0568  * the leaf_entry.  The namespaces are independent only because we also look
0569  * at the namespace bit when we are looking for a matching attribute name.
0570  *
0571  * We also store an "incomplete" bit in the leaf_entry.  It shows that an
0572  * attribute is in the middle of being created and should not be shown to
0573  * the user if we crash during the time that the bit is set.  We clear the
0574  * bit when we have finished setting up the attribute.  We do this because
0575  * we cannot create some large attributes inside a single transaction, and we
0576  * need some indication that we weren't finished if we crash in the middle.
0577  */
0578 #define XFS_ATTR_LEAF_MAPSIZE   3   /* how many freespace slots */
0579 
0580 /*
0581  * Entries are packed toward the top as tight as possible.
0582  */
0583 struct xfs_attr_shortform {
0584     struct xfs_attr_sf_hdr {    /* constant-structure header block */
0585         __be16  totsize;    /* total bytes in shortform list */
0586         __u8    count;  /* count of active entries */
0587         __u8    padding;
0588     } hdr;
0589     struct xfs_attr_sf_entry {
0590         uint8_t namelen;    /* actual length of name (no NULL) */
0591         uint8_t valuelen;   /* actual length of value (no NULL) */
0592         uint8_t flags;  /* flags bits (see xfs_attr_leaf.h) */
0593         uint8_t nameval[];  /* name & value bytes concatenated */
0594     } list[1];          /* variable sized array */
0595 };
0596 
0597 typedef struct xfs_attr_leaf_map {  /* RLE map of free bytes */
0598     __be16  base;             /* base of free region */
0599     __be16  size;             /* length of free region */
0600 } xfs_attr_leaf_map_t;
0601 
0602 typedef struct xfs_attr_leaf_hdr {  /* constant-structure header block */
0603     xfs_da_blkinfo_t info;      /* block type, links, etc. */
0604     __be16  count;          /* count of active leaf_entry's */
0605     __be16  usedbytes;      /* num bytes of names/values stored */
0606     __be16  firstused;      /* first used byte in name area */
0607     __u8    holes;          /* != 0 if blk needs compaction */
0608     __u8    pad1;
0609     xfs_attr_leaf_map_t freemap[XFS_ATTR_LEAF_MAPSIZE];
0610                     /* N largest free regions */
0611 } xfs_attr_leaf_hdr_t;
0612 
0613 typedef struct xfs_attr_leaf_entry {    /* sorted on key, not name */
0614     __be32  hashval;        /* hash value of name */
0615     __be16  nameidx;        /* index into buffer of name/value */
0616     __u8    flags;          /* LOCAL/ROOT/SECURE/INCOMPLETE flag */
0617     __u8    pad2;           /* unused pad byte */
0618 } xfs_attr_leaf_entry_t;
0619 
0620 typedef struct xfs_attr_leaf_name_local {
0621     __be16  valuelen;       /* number of bytes in value */
0622     __u8    namelen;        /* length of name bytes */
0623     __u8    nameval[1];     /* name/value bytes */
0624 } xfs_attr_leaf_name_local_t;
0625 
0626 typedef struct xfs_attr_leaf_name_remote {
0627     __be32  valueblk;       /* block number of value bytes */
0628     __be32  valuelen;       /* number of bytes in value */
0629     __u8    namelen;        /* length of name bytes */
0630     __u8    name[1];        /* name bytes */
0631 } xfs_attr_leaf_name_remote_t;
0632 
0633 typedef struct xfs_attr_leafblock {
0634     xfs_attr_leaf_hdr_t hdr;    /* constant-structure header block */
0635     xfs_attr_leaf_entry_t   entries[1]; /* sorted on key, not name */
0636     /*
0637      * The rest of the block contains the following structures after the
0638      * leaf entries, growing from the bottom up. The variables are never
0639      * referenced and definining them can actually make gcc optimize away
0640      * accesses to the 'entries' array above index 0 so don't do that.
0641      *
0642      * xfs_attr_leaf_name_local_t namelist;
0643      * xfs_attr_leaf_name_remote_t valuelist;
0644      */
0645 } xfs_attr_leafblock_t;
0646 
0647 /*
0648  * CRC enabled leaf structures. Called "version 3" structures to match the
0649  * version number of the directory and dablk structures for this feature, and
0650  * attr2 is already taken by the variable inode attribute fork size feature.
0651  */
0652 struct xfs_attr3_leaf_hdr {
0653     struct xfs_da3_blkinfo  info;
0654     __be16          count;
0655     __be16          usedbytes;
0656     __be16          firstused;
0657     __u8            holes;
0658     __u8            pad1;
0659     struct xfs_attr_leaf_map freemap[XFS_ATTR_LEAF_MAPSIZE];
0660     __be32          pad2;       /* 64 bit alignment */
0661 };
0662 
0663 #define XFS_ATTR3_LEAF_CRC_OFF  (offsetof(struct xfs_attr3_leaf_hdr, info.crc))
0664 
0665 struct xfs_attr3_leafblock {
0666     struct xfs_attr3_leaf_hdr   hdr;
0667     struct xfs_attr_leaf_entry  entries[1];
0668 
0669     /*
0670      * The rest of the block contains the following structures after the
0671      * leaf entries, growing from the bottom up. The variables are never
0672      * referenced, the locations accessed purely from helper functions.
0673      *
0674      * struct xfs_attr_leaf_name_local
0675      * struct xfs_attr_leaf_name_remote
0676      */
0677 };
0678 
0679 /*
0680  * Special value to represent fs block size in the leaf header firstused field.
0681  * Only used when block size overflows the 2-bytes available on disk.
0682  */
0683 #define XFS_ATTR3_LEAF_NULLOFF  0
0684 
0685 /*
0686  * Flags used in the leaf_entry[i].flags field.
0687  */
0688 #define XFS_ATTR_LOCAL_BIT  0   /* attr is stored locally */
0689 #define XFS_ATTR_ROOT_BIT   1   /* limit access to trusted attrs */
0690 #define XFS_ATTR_SECURE_BIT 2   /* limit access to secure attrs */
0691 #define XFS_ATTR_INCOMPLETE_BIT 7   /* attr in middle of create/delete */
0692 #define XFS_ATTR_LOCAL      (1u << XFS_ATTR_LOCAL_BIT)
0693 #define XFS_ATTR_ROOT       (1u << XFS_ATTR_ROOT_BIT)
0694 #define XFS_ATTR_SECURE     (1u << XFS_ATTR_SECURE_BIT)
0695 #define XFS_ATTR_INCOMPLETE (1u << XFS_ATTR_INCOMPLETE_BIT)
0696 #define XFS_ATTR_NSP_ONDISK_MASK    (XFS_ATTR_ROOT | XFS_ATTR_SECURE)
0697 
0698 /*
0699  * Alignment for namelist and valuelist entries (since they are mixed
0700  * there can be only one alignment value)
0701  */
0702 #define XFS_ATTR_LEAF_NAME_ALIGN    ((uint)sizeof(xfs_dablk_t))
0703 
0704 static inline int
0705 xfs_attr3_leaf_hdr_size(struct xfs_attr_leafblock *leafp)
0706 {
0707     if (leafp->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC))
0708         return sizeof(struct xfs_attr3_leaf_hdr);
0709     return sizeof(struct xfs_attr_leaf_hdr);
0710 }
0711 
0712 static inline struct xfs_attr_leaf_entry *
0713 xfs_attr3_leaf_entryp(xfs_attr_leafblock_t *leafp)
0714 {
0715     if (leafp->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC))
0716         return &((struct xfs_attr3_leafblock *)leafp)->entries[0];
0717     return &leafp->entries[0];
0718 }
0719 
0720 /*
0721  * Cast typed pointers for "local" and "remote" name/value structs.
0722  */
0723 static inline char *
0724 xfs_attr3_leaf_name(xfs_attr_leafblock_t *leafp, int idx)
0725 {
0726     struct xfs_attr_leaf_entry *entries = xfs_attr3_leaf_entryp(leafp);
0727 
0728     return &((char *)leafp)[be16_to_cpu(entries[idx].nameidx)];
0729 }
0730 
0731 static inline xfs_attr_leaf_name_remote_t *
0732 xfs_attr3_leaf_name_remote(xfs_attr_leafblock_t *leafp, int idx)
0733 {
0734     return (xfs_attr_leaf_name_remote_t *)xfs_attr3_leaf_name(leafp, idx);
0735 }
0736 
0737 static inline xfs_attr_leaf_name_local_t *
0738 xfs_attr3_leaf_name_local(xfs_attr_leafblock_t *leafp, int idx)
0739 {
0740     return (xfs_attr_leaf_name_local_t *)xfs_attr3_leaf_name(leafp, idx);
0741 }
0742 
0743 /*
0744  * Calculate total bytes used (including trailing pad for alignment) for
0745  * a "local" name/value structure, a "remote" name/value structure, and
0746  * a pointer which might be either.
0747  */
0748 static inline int xfs_attr_leaf_entsize_remote(int nlen)
0749 {
0750     return round_up(sizeof(struct xfs_attr_leaf_name_remote) - 1 +
0751             nlen, XFS_ATTR_LEAF_NAME_ALIGN);
0752 }
0753 
0754 static inline int xfs_attr_leaf_entsize_local(int nlen, int vlen)
0755 {
0756     return round_up(sizeof(struct xfs_attr_leaf_name_local) - 1 +
0757             nlen + vlen, XFS_ATTR_LEAF_NAME_ALIGN);
0758 }
0759 
0760 static inline int xfs_attr_leaf_entsize_local_max(int bsize)
0761 {
0762     return (((bsize) >> 1) + ((bsize) >> 2));
0763 }
0764 
0765 
0766 
0767 /*
0768  * Remote attribute block format definition
0769  *
0770  * There is one of these headers per filesystem block in a remote attribute.
0771  * This is done to ensure there is a 1:1 mapping between the attribute value
0772  * length and the number of blocks needed to store the attribute. This makes the
0773  * verification of a buffer a little more complex, but greatly simplifies the
0774  * allocation, reading and writing of these attributes as we don't have to guess
0775  * the number of blocks needed to store the attribute data.
0776  */
0777 #define XFS_ATTR3_RMT_MAGIC 0x5841524d  /* XARM */
0778 
0779 struct xfs_attr3_rmt_hdr {
0780     __be32  rm_magic;
0781     __be32  rm_offset;
0782     __be32  rm_bytes;
0783     __be32  rm_crc;
0784     uuid_t  rm_uuid;
0785     __be64  rm_owner;
0786     __be64  rm_blkno;
0787     __be64  rm_lsn;
0788 };
0789 
0790 #define XFS_ATTR3_RMT_CRC_OFF   offsetof(struct xfs_attr3_rmt_hdr, rm_crc)
0791 
0792 #define XFS_ATTR3_RMT_BUF_SPACE(mp, bufsize)    \
0793     ((bufsize) - (xfs_has_crc((mp)) ? \
0794             sizeof(struct xfs_attr3_rmt_hdr) : 0))
0795 
0796 /* Number of bytes in a directory block. */
0797 static inline unsigned int xfs_dir2_dirblock_bytes(struct xfs_sb *sbp)
0798 {
0799     return 1 << (sbp->sb_blocklog + sbp->sb_dirblklog);
0800 }
0801 
0802 xfs_failaddr_t xfs_da3_blkinfo_verify(struct xfs_buf *bp,
0803                       struct xfs_da3_blkinfo *hdr3);
0804 
0805 #endif /* __XFS_DA_FORMAT_H__ */