]> www.infradead.org Git - users/hch/xfsprogs.git/commitdiff
xfs: add realtime refcount btree operations
authorDarrick J. Wong <djwong@kernel.org>
Tue, 15 Oct 2024 19:44:42 +0000 (12:44 -0700)
committerDarrick J. Wong <djwong@kernel.org>
Fri, 1 Nov 2024 20:44:59 +0000 (13:44 -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 83e3467758064f79bce311e69b27524d179387bc..009ab272f286b745a7a8e4d5c4b9dd75e526d976 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_group.h"
@@ -44,6 +45,106 @@ xfs_rtrefcountbt_dup_cursor(
        return xfs_rtrefcountbt_init_cursor(cur->bc_tp, to_rtg(cur->bc_group));
 }
 
+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)
@@ -110,6 +211,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,
@@ -123,7 +258,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_metafile_block,
+       .free_block             = xfs_btree_free_metafile_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. */