xfs: compute absolute maximum nlevels for each btree type
authorDarrick J. Wong <djwong@kernel.org>
Thu, 23 Sep 2021 17:32:06 +0000 (10:32 -0700)
committerDarrick J. Wong <djwong@kernel.org>
Tue, 19 Oct 2021 18:45:16 +0000 (11:45 -0700)
Add code for all five btree types so that we can compute the absolute
maximum possible btree height for each btree type.  This is a setup for
the next patch, which makes every btree type have its own cursor cache.

The functions are exported so that we can have xfs_db report the
absolute maximum btree heights for each btree type, rather than making
everyone run their own ad-hoc computations.

Signed-off-by: Darrick J. Wong <djwong@kernel.org>
Reviewed-by: Dave Chinner <dchinner@redhat.com>
14 files changed:
fs/xfs/libxfs/xfs_alloc.c
fs/xfs/libxfs/xfs_alloc_btree.c
fs/xfs/libxfs/xfs_alloc_btree.h
fs/xfs/libxfs/xfs_bmap.c
fs/xfs/libxfs/xfs_bmap_btree.c
fs/xfs/libxfs/xfs_bmap_btree.h
fs/xfs/libxfs/xfs_fs.h
fs/xfs/libxfs/xfs_ialloc.c
fs/xfs/libxfs/xfs_ialloc_btree.c
fs/xfs/libxfs/xfs_ialloc_btree.h
fs/xfs/libxfs/xfs_refcount_btree.c
fs/xfs/libxfs/xfs_refcount_btree.h
fs/xfs/libxfs/xfs_rmap_btree.c
fs/xfs/libxfs/xfs_rmap_btree.h

index 2efb4f4..1a5684a 100644 (file)
@@ -2198,6 +2198,7 @@ xfs_alloc_compute_maxlevels(
 {
        mp->m_alloc_maxlevels = xfs_btree_compute_maxlevels(mp->m_alloc_mnr,
                        (mp->m_sb.sb_agblocks + 1) / 2);
+       ASSERT(mp->m_alloc_maxlevels <= xfs_allocbt_maxlevels_ondisk());
 }
 
 /*
index c3e2622..d0a7aa4 100644 (file)
@@ -566,6 +566,17 @@ xfs_allocbt_commit_staged_btree(
        }
 }
 
+/* Calculate number of records in an alloc btree block. */
+static inline unsigned int
+xfs_allocbt_block_maxrecs(
+       unsigned int            blocklen,
+       bool                    leaf)
+{
+       if (leaf)
+               return blocklen / sizeof(xfs_alloc_rec_t);
+       return blocklen / (sizeof(xfs_alloc_key_t) + sizeof(xfs_alloc_ptr_t));
+}
+
 /*
  * Calculate number of records in an alloc btree block.
  */
@@ -576,10 +587,26 @@ xfs_allocbt_maxrecs(
        int                     leaf)
 {
        blocklen -= XFS_ALLOC_BLOCK_LEN(mp);
+       return xfs_allocbt_block_maxrecs(blocklen, leaf);
+}
 
-       if (leaf)
-               return blocklen / sizeof(xfs_alloc_rec_t);
-       return blocklen / (sizeof(xfs_alloc_key_t) + sizeof(xfs_alloc_ptr_t));
+/* Free space btrees are at their largest when every other block is free. */
+#define XFS_MAX_FREESP_RECORDS ((XFS_MAX_AG_BLOCKS + 1) / 2)
+
+/* Compute the max possible height for free space btrees. */
+unsigned int
+xfs_allocbt_maxlevels_ondisk(void)
+{
+       unsigned int            minrecs[2];
+       unsigned int            blocklen;
+
+       blocklen = min(XFS_MIN_BLOCKSIZE - XFS_BTREE_SBLOCK_LEN,
+                      XFS_MIN_CRC_BLOCKSIZE - XFS_BTREE_SBLOCK_CRC_LEN);
+
+       minrecs[0] = xfs_allocbt_block_maxrecs(blocklen, true) / 2;
+       minrecs[1] = xfs_allocbt_block_maxrecs(blocklen, false) / 2;
+
+       return xfs_btree_compute_maxlevels(minrecs, XFS_MAX_FREESP_RECORDS);
 }
 
 /* Calculate the freespace btree size for some records. */
index 2f6b816..c715bee 100644 (file)
@@ -60,4 +60,6 @@ extern xfs_extlen_t xfs_allocbt_calc_size(struct xfs_mount *mp,
 void xfs_allocbt_commit_staged_btree(struct xfs_btree_cur *cur,
                struct xfs_trans *tp, struct xfs_buf *agbp);
 
+unsigned int xfs_allocbt_maxlevels_ondisk(void);
+
 #endif /* __XFS_ALLOC_BTREE_H__ */
index 2ae5bf9..321617e 100644 (file)
@@ -93,6 +93,7 @@ xfs_bmap_compute_maxlevels(
                        maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
        }
        mp->m_bm_maxlevels[whichfork] = level;
+       ASSERT(mp->m_bm_maxlevels[whichfork] <= xfs_bmbt_maxlevels_ondisk());
 }
 
 unsigned int
index b90122d..59d1466 100644 (file)
@@ -571,6 +571,17 @@ xfs_bmbt_init_cursor(
        return cur;
 }
 
+/* Calculate number of records in a block mapping btree block. */
+static inline unsigned int
+xfs_bmbt_block_maxrecs(
+       unsigned int            blocklen,
+       bool                    leaf)
+{
+       if (leaf)
+               return blocklen / sizeof(xfs_bmbt_rec_t);
+       return blocklen / (sizeof(xfs_bmbt_key_t) + sizeof(xfs_bmbt_ptr_t));
+}
+
 /*
  * Calculate number of records in a bmap btree block.
  */
@@ -581,10 +592,24 @@ xfs_bmbt_maxrecs(
        int                     leaf)
 {
        blocklen -= XFS_BMBT_BLOCK_LEN(mp);
+       return xfs_bmbt_block_maxrecs(blocklen, leaf);
+}
 
-       if (leaf)
-               return blocklen / sizeof(xfs_bmbt_rec_t);
-       return blocklen / (sizeof(xfs_bmbt_key_t) + sizeof(xfs_bmbt_ptr_t));
+/* Compute the max possible height for block mapping btrees. */
+unsigned int
+xfs_bmbt_maxlevels_ondisk(void)
+{
+       unsigned int            minrecs[2];
+       unsigned int            blocklen;
+
+       blocklen = min(XFS_MIN_BLOCKSIZE - XFS_BTREE_SBLOCK_LEN,
+                      XFS_MIN_CRC_BLOCKSIZE - XFS_BTREE_SBLOCK_CRC_LEN);
+
+       minrecs[0] = xfs_bmbt_block_maxrecs(blocklen, true) / 2;
+       minrecs[1] = xfs_bmbt_block_maxrecs(blocklen, false) / 2;
+
+       /* One extra level for the inode root. */
+       return xfs_btree_compute_maxlevels(minrecs, MAXEXTNUM) + 1;
 }
 
 /*
index 729e3bc..2a1c9e6 100644 (file)
@@ -110,4 +110,6 @@ extern struct xfs_btree_cur *xfs_bmbt_init_cursor(struct xfs_mount *,
 extern unsigned long long xfs_bmbt_calc_size(struct xfs_mount *mp,
                unsigned long long len);
 
+unsigned int xfs_bmbt_maxlevels_ondisk(void);
+
 #endif /* __XFS_BMAP_BTREE_H__ */
index bde2b4c..c43877c 100644 (file)
@@ -268,6 +268,8 @@ typedef struct xfs_fsop_resblks {
  */
 #define XFS_MIN_AG_BYTES       (1ULL << 24)    /* 16 MB */
 #define XFS_MAX_AG_BYTES       (1ULL << 40)    /* 1 TB */
+#define XFS_MAX_AG_BLOCKS      (XFS_MAX_AG_BYTES / XFS_MIN_BLOCKSIZE)
+#define XFS_MAX_CRC_AG_BLOCKS  (XFS_MAX_AG_BYTES / XFS_MIN_CRC_BLOCKSIZE)
 
 /* keep the maximum size under 2^31 by a small amount */
 #define XFS_MAX_LOG_BYTES \
index 994ad78..f78a600 100644 (file)
@@ -2793,6 +2793,7 @@ xfs_ialloc_setup_geometry(
        inodes = (1LL << XFS_INO_AGINO_BITS(mp)) >> XFS_INODES_PER_CHUNK_LOG;
        igeo->inobt_maxlevels = xfs_btree_compute_maxlevels(igeo->inobt_mnr,
                        inodes);
+       ASSERT(igeo->inobt_maxlevels <= xfs_iallocbt_maxlevels_ondisk());
 
        /*
         * Set the maximum inode count for this filesystem, being careful not
index 3a5a246..74681e8 100644 (file)
@@ -526,6 +526,17 @@ xfs_inobt_commit_staged_btree(
        }
 }
 
+/* Calculate number of records in an inode btree block. */
+static inline unsigned int
+xfs_inobt_block_maxrecs(
+       unsigned int            blocklen,
+       bool                    leaf)
+{
+       if (leaf)
+               return blocklen / sizeof(xfs_inobt_rec_t);
+       return blocklen / (sizeof(xfs_inobt_key_t) + sizeof(xfs_inobt_ptr_t));
+}
+
 /*
  * Calculate number of records in an inobt btree block.
  */
@@ -536,10 +547,54 @@ xfs_inobt_maxrecs(
        int                     leaf)
 {
        blocklen -= XFS_INOBT_BLOCK_LEN(mp);
+       return xfs_inobt_block_maxrecs(blocklen, leaf);
+}
 
-       if (leaf)
-               return blocklen / sizeof(xfs_inobt_rec_t);
-       return blocklen / (sizeof(xfs_inobt_key_t) + sizeof(xfs_inobt_ptr_t));
+/*
+ * Maximum number of inode btree records per AG.  Pretend that we can fill an
+ * entire AG completely full of inodes except for the AG headers.
+ */
+#define XFS_MAX_INODE_RECORDS \
+       ((XFS_MAX_AG_BYTES - (4 * BBSIZE)) / XFS_DINODE_MIN_SIZE) / \
+                       XFS_INODES_PER_CHUNK
+
+/* Compute the max possible height for the inode btree. */
+static inline unsigned int
+xfs_inobt_maxlevels_ondisk(void)
+{
+       unsigned int            minrecs[2];
+       unsigned int            blocklen;
+
+       blocklen = min(XFS_MIN_BLOCKSIZE - XFS_BTREE_SBLOCK_LEN,
+                      XFS_MIN_CRC_BLOCKSIZE - XFS_BTREE_SBLOCK_CRC_LEN);
+
+       minrecs[0] = xfs_inobt_block_maxrecs(blocklen, true) / 2;
+       minrecs[1] = xfs_inobt_block_maxrecs(blocklen, false) / 2;
+
+       return xfs_btree_compute_maxlevels(minrecs, XFS_MAX_INODE_RECORDS);
+}
+
+/* Compute the max possible height for the free inode btree. */
+static inline unsigned int
+xfs_finobt_maxlevels_ondisk(void)
+{
+       unsigned int            minrecs[2];
+       unsigned int            blocklen;
+
+       blocklen = XFS_MIN_CRC_BLOCKSIZE - XFS_BTREE_SBLOCK_CRC_LEN;
+
+       minrecs[0] = xfs_inobt_block_maxrecs(blocklen, true) / 2;
+       minrecs[1] = xfs_inobt_block_maxrecs(blocklen, false) / 2;
+
+       return xfs_btree_compute_maxlevels(minrecs, XFS_MAX_INODE_RECORDS);
+}
+
+/* Compute the max possible height for either inode btree. */
+unsigned int
+xfs_iallocbt_maxlevels_ondisk(void)
+{
+       return max(xfs_inobt_maxlevels_ondisk(),
+                  xfs_finobt_maxlevels_ondisk());
 }
 
 /*
index 8a322d4..6d3e4a3 100644 (file)
@@ -75,4 +75,6 @@ int xfs_inobt_cur(struct xfs_mount *mp, struct xfs_trans *tp,
 void xfs_inobt_commit_staged_btree(struct xfs_btree_cur *cur,
                struct xfs_trans *tp, struct xfs_buf *agbp);
 
+unsigned int xfs_iallocbt_maxlevels_ondisk(void);
+
 #endif /* __XFS_IALLOC_BTREE_H__ */
index 995b0d8..3bf802f 100644 (file)
@@ -393,6 +393,18 @@ xfs_refcountbt_commit_staged_btree(
        xfs_btree_commit_afakeroot(cur, tp, agbp, &xfs_refcountbt_ops);
 }
 
+/* Calculate number of records in a refcount btree block. */
+static inline unsigned int
+xfs_refcountbt_block_maxrecs(
+       unsigned int            blocklen,
+       bool                    leaf)
+{
+       if (leaf)
+               return blocklen / sizeof(struct xfs_refcount_rec);
+       return blocklen / (sizeof(struct xfs_refcount_key) +
+                          sizeof(xfs_refcount_ptr_t));
+}
+
 /*
  * Calculate the number of records in a refcount btree block.
  */
@@ -402,11 +414,22 @@ xfs_refcountbt_maxrecs(
        bool                    leaf)
 {
        blocklen -= XFS_REFCOUNT_BLOCK_LEN;
+       return xfs_refcountbt_block_maxrecs(blocklen, leaf);
+}
 
-       if (leaf)
-               return blocklen / sizeof(struct xfs_refcount_rec);
-       return blocklen / (sizeof(struct xfs_refcount_key) +
-                          sizeof(xfs_refcount_ptr_t));
+/* Compute the max possible height of the maximally sized refcount btree. */
+unsigned int
+xfs_refcountbt_maxlevels_ondisk(void)
+{
+       unsigned int            minrecs[2];
+       unsigned int            blocklen;
+
+       blocklen = XFS_MIN_CRC_BLOCKSIZE - XFS_BTREE_SBLOCK_CRC_LEN;
+
+       minrecs[0] = xfs_refcountbt_block_maxrecs(blocklen, true) / 2;
+       minrecs[1] = xfs_refcountbt_block_maxrecs(blocklen, false) / 2;
+
+       return xfs_btree_compute_maxlevels(minrecs, XFS_MAX_CRC_AG_BLOCKS);
 }
 
 /* Compute the maximum height of a refcount btree. */
@@ -414,8 +437,14 @@ void
 xfs_refcountbt_compute_maxlevels(
        struct xfs_mount                *mp)
 {
+       if (!xfs_has_reflink(mp)) {
+               mp->m_refc_maxlevels = 0;
+               return;
+       }
+
        mp->m_refc_maxlevels = xfs_btree_compute_maxlevels(
                        mp->m_refc_mnr, mp->m_sb.sb_agblocks);
+       ASSERT(mp->m_refc_maxlevels <= xfs_refcountbt_maxlevels_ondisk());
 }
 
 /* Calculate the refcount btree size for some records. */
index bd9ed9e..d7f7c89 100644 (file)
@@ -65,4 +65,6 @@ extern int xfs_refcountbt_calc_reserves(struct xfs_mount *mp,
 void xfs_refcountbt_commit_staged_btree(struct xfs_btree_cur *cur,
                struct xfs_trans *tp, struct xfs_buf *agbp);
 
+unsigned int xfs_refcountbt_maxlevels_ondisk(void);
+
 #endif /* __XFS_REFCOUNT_BTREE_H__ */
index 7388201..0c96e26 100644 (file)
@@ -519,6 +519,18 @@ xfs_rmapbt_commit_staged_btree(
        xfs_btree_commit_afakeroot(cur, tp, agbp, &xfs_rmapbt_ops);
 }
 
+/* Calculate number of records in a reverse mapping btree block. */
+static inline unsigned int
+xfs_rmapbt_block_maxrecs(
+       unsigned int            blocklen,
+       bool                    leaf)
+{
+       if (leaf)
+               return blocklen / sizeof(struct xfs_rmap_rec);
+       return blocklen /
+               (2 * sizeof(struct xfs_rmap_key) + sizeof(xfs_rmap_ptr_t));
+}
+
 /*
  * Calculate number of records in an rmap btree block.
  */
@@ -528,11 +540,33 @@ xfs_rmapbt_maxrecs(
        int                     leaf)
 {
        blocklen -= XFS_RMAP_BLOCK_LEN;
+       return xfs_rmapbt_block_maxrecs(blocklen, leaf);
+}
 
-       if (leaf)
-               return blocklen / sizeof(struct xfs_rmap_rec);
-       return blocklen /
-               (2 * sizeof(struct xfs_rmap_key) + sizeof(xfs_rmap_ptr_t));
+/* Compute the max possible height for reverse mapping btrees. */
+unsigned int
+xfs_rmapbt_maxlevels_ondisk(void)
+{
+       unsigned int            minrecs[2];
+       unsigned int            blocklen;
+
+       blocklen = XFS_MIN_CRC_BLOCKSIZE - XFS_BTREE_SBLOCK_CRC_LEN;
+
+       minrecs[0] = xfs_rmapbt_block_maxrecs(blocklen, true) / 2;
+       minrecs[1] = xfs_rmapbt_block_maxrecs(blocklen, false) / 2;
+
+       /*
+        * Compute the asymptotic maxlevels for an rmapbt on any reflink fs.
+        *
+        * On a reflink filesystem, each AG block can have up to 2^32 (per the
+        * refcount record format) owners, which means that theoretically we
+        * could face up to 2^64 rmap records.  However, we're likely to run
+        * out of blocks in the AG long before that happens, which means that
+        * we must compute the max height based on what the btree will look
+        * like if it consumes almost all the blocks in the AG due to maximal
+        * sharing factor.
+        */
+       return xfs_btree_space_to_height(minrecs, XFS_MAX_CRC_AG_BLOCKS);
 }
 
 /* Compute the maximum height of an rmap btree. */
@@ -569,6 +603,7 @@ xfs_rmapbt_compute_maxlevels(
                mp->m_rmap_maxlevels = xfs_btree_compute_maxlevels(
                                mp->m_rmap_mnr, mp->m_sb.sb_agblocks);
        }
+       ASSERT(mp->m_rmap_maxlevels <= xfs_rmapbt_maxlevels_ondisk());
 }
 
 /* Calculate the refcount btree size for some records. */
index f2eee65..e9778b6 100644 (file)
@@ -59,4 +59,6 @@ extern xfs_extlen_t xfs_rmapbt_max_size(struct xfs_mount *mp,
 extern int xfs_rmapbt_calc_reserves(struct xfs_mount *mp, struct xfs_trans *tp,
                struct xfs_perag *pag, xfs_extlen_t *ask, xfs_extlen_t *used);
 
+unsigned int xfs_rmapbt_maxlevels_ondisk(void);
+
 #endif /* __XFS_RMAP_BTREE_H__ */