]> www.infradead.org Git - users/hch/xfsprogs.git/commitdiff
xfs: add realtime refcount btree operations
authorDarrick J. Wong <djwong@kernel.org>
Wed, 3 Jul 2024 21:22:28 +0000 (14:22 -0700)
committerDarrick J. Wong <djwong@kernel.org>
Tue, 16 Jul 2024 22:49:22 +0000 (15:49 -0700)
Implement the generic btree operations needed to manipulate rtrefcount
btree blocks. This is different from the regular refcountbt in that we
allocate space from the filesystem at large, and are neither constrained
to the free space nor any particular AG.

Signed-off-by: Darrick J. Wong <djwong@kernel.org>
libxfs/xfs_rtrefcount_btree.c

index 79727cff7d9dc622cc0da66647a1dee553585d5f..8d5591ded4c570ef896a9d98b3cf8411af6f0375 100644 (file)
@@ -19,6 +19,7 @@
 #include "xfs_btree.h"
 #include "xfs_btree_staging.h"
 #include "xfs_rtrefcount_btree.h"
+#include "xfs_refcount.h"
 #include "xfs_trace.h"
 #include "xfs_cksum.h"
 #include "xfs_rtgroup.h"
@@ -48,6 +49,106 @@ xfs_rtrefcountbt_dup_cursor(
        return new;
 }
 
+STATIC int
+xfs_rtrefcountbt_get_minrecs(
+       struct xfs_btree_cur    *cur,
+       int                     level)
+{
+       if (level == cur->bc_nlevels - 1) {
+               struct xfs_ifork        *ifp = xfs_btree_ifork_ptr(cur);
+
+               return xfs_rtrefcountbt_maxrecs(cur->bc_mp, ifp->if_broot_bytes,
+                               level == 0) / 2;
+       }
+
+       return cur->bc_mp->m_rtrefc_mnr[level != 0];
+}
+
+STATIC int
+xfs_rtrefcountbt_get_maxrecs(
+       struct xfs_btree_cur    *cur,
+       int                     level)
+{
+       if (level == cur->bc_nlevels - 1) {
+               struct xfs_ifork        *ifp = xfs_btree_ifork_ptr(cur);
+
+               return xfs_rtrefcountbt_maxrecs(cur->bc_mp, ifp->if_broot_bytes,
+                               level == 0);
+       }
+
+       return cur->bc_mp->m_rtrefc_mxr[level != 0];
+}
+
+STATIC void
+xfs_rtrefcountbt_init_key_from_rec(
+       union xfs_btree_key             *key,
+       const union xfs_btree_rec       *rec)
+{
+       key->refc.rc_startblock = rec->refc.rc_startblock;
+}
+
+STATIC void
+xfs_rtrefcountbt_init_high_key_from_rec(
+       union xfs_btree_key             *key,
+       const union xfs_btree_rec       *rec)
+{
+       __u32                           x;
+
+       x = be32_to_cpu(rec->refc.rc_startblock);
+       x += be32_to_cpu(rec->refc.rc_blockcount) - 1;
+       key->refc.rc_startblock = cpu_to_be32(x);
+}
+
+STATIC void
+xfs_rtrefcountbt_init_rec_from_cur(
+       struct xfs_btree_cur    *cur,
+       union xfs_btree_rec     *rec)
+{
+       const struct xfs_refcount_irec *irec = &cur->bc_rec.rc;
+       uint32_t                start;
+
+       start = xfs_refcount_encode_startblock(irec->rc_startblock,
+                       irec->rc_domain);
+       rec->refc.rc_startblock = cpu_to_be32(start);
+       rec->refc.rc_blockcount = cpu_to_be32(cur->bc_rec.rc.rc_blockcount);
+       rec->refc.rc_refcount = cpu_to_be32(cur->bc_rec.rc.rc_refcount);
+}
+
+STATIC void
+xfs_rtrefcountbt_init_ptr_from_cur(
+       struct xfs_btree_cur    *cur,
+       union xfs_btree_ptr     *ptr)
+{
+       ptr->l = 0;
+}
+
+STATIC int64_t
+xfs_rtrefcountbt_key_diff(
+       struct xfs_btree_cur            *cur,
+       const union xfs_btree_key       *key)
+{
+       const struct xfs_refcount_key   *kp = &key->refc;
+       const struct xfs_refcount_irec  *irec = &cur->bc_rec.rc;
+       uint32_t                        start;
+
+       start = xfs_refcount_encode_startblock(irec->rc_startblock,
+                       irec->rc_domain);
+       return (int64_t)be32_to_cpu(kp->rc_startblock) - start;
+}
+
+STATIC int64_t
+xfs_rtrefcountbt_diff_two_keys(
+       struct xfs_btree_cur            *cur,
+       const union xfs_btree_key       *k1,
+       const union xfs_btree_key       *k2,
+       const union xfs_btree_key       *mask)
+{
+       ASSERT(!mask || mask->refc.rc_startblock);
+
+       return (int64_t)be32_to_cpu(k1->refc.rc_startblock) -
+                       be32_to_cpu(k2->refc.rc_startblock);
+}
+
 static xfs_failaddr_t
 xfs_rtrefcountbt_verify(
        struct xfs_buf          *bp)
@@ -114,6 +215,40 @@ const struct xfs_buf_ops xfs_rtrefcountbt_buf_ops = {
        .verify_struct          = xfs_rtrefcountbt_verify,
 };
 
+STATIC int
+xfs_rtrefcountbt_keys_inorder(
+       struct xfs_btree_cur            *cur,
+       const union xfs_btree_key       *k1,
+       const union xfs_btree_key       *k2)
+{
+       return be32_to_cpu(k1->refc.rc_startblock) <
+              be32_to_cpu(k2->refc.rc_startblock);
+}
+
+STATIC int
+xfs_rtrefcountbt_recs_inorder(
+       struct xfs_btree_cur            *cur,
+       const union xfs_btree_rec       *r1,
+       const union xfs_btree_rec       *r2)
+{
+       return  be32_to_cpu(r1->refc.rc_startblock) +
+               be32_to_cpu(r1->refc.rc_blockcount) <=
+               be32_to_cpu(r2->refc.rc_startblock);
+}
+
+STATIC enum xbtree_key_contig
+xfs_rtrefcountbt_keys_contiguous(
+       struct xfs_btree_cur            *cur,
+       const union xfs_btree_key       *key1,
+       const union xfs_btree_key       *key2,
+       const union xfs_btree_key       *mask)
+{
+       ASSERT(!mask || mask->refc.rc_startblock);
+
+       return xbtree_key_contig(be32_to_cpu(key1->refc.rc_startblock),
+                                be32_to_cpu(key2->refc.rc_startblock));
+}
+
 const struct xfs_btree_ops xfs_rtrefcountbt_ops = {
        .name                   = "rtrefcount",
        .type                   = XFS_BTREE_TYPE_INODE,
@@ -127,7 +262,20 @@ const struct xfs_btree_ops xfs_rtrefcountbt_ops = {
        .statoff                = XFS_STATS_CALC_INDEX(xs_rtrefcbt_2),
 
        .dup_cursor             = xfs_rtrefcountbt_dup_cursor,
+       .alloc_block            = xfs_btree_alloc_imeta_block,
+       .free_block             = xfs_btree_free_imeta_block,
+       .get_minrecs            = xfs_rtrefcountbt_get_minrecs,
+       .get_maxrecs            = xfs_rtrefcountbt_get_maxrecs,
+       .init_key_from_rec      = xfs_rtrefcountbt_init_key_from_rec,
+       .init_high_key_from_rec = xfs_rtrefcountbt_init_high_key_from_rec,
+       .init_rec_from_cur      = xfs_rtrefcountbt_init_rec_from_cur,
+       .init_ptr_from_cur      = xfs_rtrefcountbt_init_ptr_from_cur,
+       .key_diff               = xfs_rtrefcountbt_key_diff,
        .buf_ops                = &xfs_rtrefcountbt_buf_ops,
+       .diff_two_keys          = xfs_rtrefcountbt_diff_two_keys,
+       .keys_inorder           = xfs_rtrefcountbt_keys_inorder,
+       .recs_inorder           = xfs_rtrefcountbt_recs_inorder,
+       .keys_contiguous        = xfs_rtrefcountbt_keys_contiguous,
 };
 
 /* Allocate a new rt refcount btree cursor. */