[PATCH v2 2/2] xfs: optimize getfsmap by reusing and advancing the refcount cursor
From: zhangshida2026
Date: Wed Nov 25 2026 - 20:31:00 EST
From: Shida Zhang <zhangshida@xxxxxxxxxx>
GETFSMAP walks the rmap btree in increasing startblock order and asks
the refcount btree whether each record's blocks are shared. For every
record, xfs_getfsmap_is_shared() allocates a refcount cursor and calls
xfs_refcount_find_shared(), which searches the btree from the root.
Both costs are paid per record even though the next query is almost
always the next refcount record.
Keep the refcount cursor in xfs_getfsmap_info for the whole group walk.
Position it once with xfs_refcount_lookup_le(), cache the record it
points at, and advance with xfs_btree_increment() as the rmap walk
moves forward. Skip COW records and SHARED records that end before the
current block; the first SHARED record ending after it decides: shared
iff it overlaps the query range. This gives the same answer as
xfs_refcount_find_shared(). The cursor is dropped when the walk leaves
the group, before the AGF buffer is released.
For example, with foo at blocks 10-19, bar a reflink copy of foo, and
baz alone at 20-29, one GETFSMAP call walks three rmap records:
rmap record shared? old code new code
10-19 -> foo yes alloc + lookup lookup once, cache
10-19 -> bar yes alloc + lookup cache hit
20-29 -> baz no alloc + lookup one step, EOF
A walk over 3000 reflinked files asks the question ~6000 times; the old
code does ~6000 cursor allocs and ~6000 root-to-leaf descents, the new
code one cursor and one descent per group.
Measured with a kernel-isolated FS_IOC_GETFSMAP loop over the whole data
device (3000 reflinked 256KB files, mkfs -m rmapbt=1,reflink=1 -d
agcount=4, Kunpeng 920 + NVMe), mean of three interleaved A/B runs:
original this patch
walk time 3048 us 324 us (9.4x)
xfs_btree_lookup 10.7% ~0% (% of cycles)
The cursor alloc/free and root-block get/put are gone too:
_raw_spin_unlock_irqrestore 14.1% -> 1.0%, kmem_cache_alloc 4.5% -> gone.
The full fsmap output is byte-identical before and after (6421 records).
Suggested-by: Christoph Hellwig <hch@xxxxxxxxxxxxx>
Signed-off-by: Shida Zhang <zhangshida@xxxxxxxxxx>
---
fs/xfs/xfs_fsmap.c | 139 +++++++++++++++++++++++++++++++++++++--------
1 file changed, 116 insertions(+), 23 deletions(-)
diff --git a/fs/xfs/xfs_fsmap.c b/fs/xfs/xfs_fsmap.c
index fe9093c0987..8f5d212bce3 100644
--- a/fs/xfs/xfs_fsmap.c
+++ b/fs/xfs/xfs_fsmap.c
@@ -13,6 +13,8 @@
#include "xfs_inode.h"
#include "xfs_trans.h"
#include "xfs_btree.h"
+#include "xfs_error.h"
+#include "xfs_health.h"
#include "xfs_rmap_btree.h"
#include "xfs_trace.h"
#include "xfs_rmap.h"
@@ -161,6 +163,9 @@ struct xfs_getfsmap_info {
struct xfs_fsmap_head *head;
struct fsmap *fsmap_recs; /* mapping records */
struct xfs_buf *agf_bp; /* AGF, for refcount queries */
+ struct xfs_btree_cur *refcount_cur; /* cached refcountbt cursor */
+ struct xfs_refcount_irec rc_cur; /* current refcount record */
+ bool rc_eof; /* refcount cursor past end */
struct xfs_group *group; /* group info, if applicable */
xfs_daddr_t next_daddr; /* next daddr we expect */
/* daddr of low fsmap key when we're using the rtbitmap */
@@ -205,15 +210,79 @@ xfs_getfsmap_dev_compare(
/* Delete the btree cursors used by a getfsmap group walk. */
static void
xfs_getfsmap_del_cursors(
- struct xfs_btree_cur *bt_cur,
- int error)
+ struct xfs_getfsmap_info *info,
+ struct xfs_btree_cur *bt_cur,
+ int error)
{
+ if (info->refcount_cur) {
+ xfs_btree_del_cursor(info->refcount_cur, error < 0 ?
+ XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
+ info->refcount_cur = NULL;
+ info->rc_eof = false;
+ }
if (bt_cur)
xfs_btree_del_cursor(bt_cur, error < 0 ?
XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
}
-/* Decide if this mapping is shared. */
+/*
+ * Allocate the refcount cursor for this group and position it at the last
+ * record at or before agbno, or the first record if there is none. Set
+ * rc_eof if the btree has no records at or after agbno.
+ */
+static int
+xfs_getfsmap_refcount_init(
+ struct xfs_trans *tp,
+ struct xfs_getfsmap_info *info,
+ xfs_agblock_t agbno)
+{
+ struct xfs_mount *mp = tp->t_mountp;
+ struct xfs_btree_cur *cur;
+ int have, i, error;
+
+ if (info->group->xg_type == XG_TYPE_RTG)
+ cur = xfs_rtrefcountbt_init_cursor(tp, to_rtg(info->group));
+ else
+ cur = xfs_refcountbt_init_cursor(mp, tp, info->agf_bp,
+ to_perag(info->group));
+ info->refcount_cur = cur;
+
+ error = xfs_refcount_lookup_le(cur, XFS_REFC_DOMAIN_SHARED, agbno,
+ &have);
+ if (error)
+ goto out_error;
+ if (!have) {
+ error = xfs_btree_increment(cur, 0, &have);
+ if (error)
+ goto out_error;
+ if (!have) {
+ info->rc_eof = true;
+ return 0;
+ }
+ }
+ error = xfs_refcount_get_rec(cur, &info->rc_cur, &i);
+ if (error)
+ goto out_error;
+ if (XFS_IS_CORRUPT(mp, i != 1)) {
+ xfs_btree_mark_sick(cur);
+ error = -EFSCORRUPTED;
+ goto out_error;
+ }
+ return 0;
+
+out_error:
+ xfs_getfsmap_del_cursors(info, NULL, error);
+ return error;
+}
+
+/*
+ * Decide if this mapping is shared.
+ *
+ * The rmapbt walk visits records in increasing startblock order, so the
+ * refcount cursor cached in @info only ever advances forward: position it
+ * once per group, then step with xfs_btree_increment() instead of
+ * re-searching from the root for every record.
+ */
STATIC int
xfs_getfsmap_is_shared(
struct xfs_trans *tp,
@@ -223,31 +292,55 @@ xfs_getfsmap_is_shared(
{
struct xfs_mount *mp = tp->t_mountp;
struct xfs_btree_cur *cur;
- xfs_agblock_t fbno;
- xfs_extlen_t flen = 0;
- int error;
+ struct xfs_refcount_irec *rc = &info->rc_cur;
+ xfs_agblock_t agbno = frec->rec_key;
+ xfs_extlen_t aglen = XFS_BB_TO_FSBT(mp, frec->len_daddr);
+ int have, i, error;
*stat = false;
if (!xfs_has_reflink(mp) || !info->group)
return 0;
- if (info->group->xg_type == XG_TYPE_RTG)
- cur = xfs_rtrefcountbt_init_cursor(tp, to_rtg(info->group));
- else
- cur = xfs_refcountbt_init_cursor(mp, tp, info->agf_bp,
- to_perag(info->group));
+ if (info->rc_eof)
+ return 0;
- /* Are there any shared blocks here? */
- error = xfs_refcount_find_shared(cur, frec->rec_key,
- XFS_BB_TO_FSBT(mp, frec->len_daddr), &fbno, &flen,
- false);
+ if (!info->refcount_cur) {
+ error = xfs_getfsmap_refcount_init(tp, info, agbno);
+ if (error)
+ return error;
+ if (info->rc_eof)
+ return 0;
+ }
+ cur = info->refcount_cur;
- xfs_btree_del_cursor(cur, error);
- if (error)
- return error;
+ /* Advance to the first SHARED record that ends after agbno. */
+ while (rc->rc_domain != XFS_REFC_DOMAIN_SHARED ||
+ rc->rc_startblock + rc->rc_blockcount <= agbno) {
+ error = xfs_btree_increment(cur, 0, &have);
+ if (error)
+ goto out_error;
+ if (!have) {
+ info->rc_eof = true;
+ return 0;
+ }
+ error = xfs_refcount_get_rec(cur, &info->rc_cur, &i);
+ if (error)
+ goto out_error;
+ if (XFS_IS_CORRUPT(mp, i != 1)) {
+ xfs_btree_mark_sick(cur);
+ error = -EFSCORRUPTED;
+ goto out_error;
+ }
+ }
- *stat = flen > 0;
+ /* Shared iff this SHARED record overlaps [agbno, agbno + aglen). */
+ if (rc->rc_startblock < agbno + aglen)
+ *stat = true;
return 0;
+
+out_error:
+ xfs_getfsmap_del_cursors(info, NULL, error);
+ return error;
}
static inline void
@@ -556,7 +649,7 @@ __xfs_getfsmap_datadev(
}
if (bt_cur) {
- xfs_getfsmap_del_cursors(bt_cur, 0);
+ xfs_getfsmap_del_cursors(info, bt_cur, 0);
bt_cur = NULL;
xfs_trans_brelse(tp, info->agf_bp);
info->agf_bp = NULL;
@@ -596,7 +689,7 @@ __xfs_getfsmap_datadev(
info->group = NULL;
}
- xfs_getfsmap_del_cursors(bt_cur, error);
+ xfs_getfsmap_del_cursors(info, bt_cur, error);
if (info->agf_bp) {
xfs_trans_brelse(tp, info->agf_bp);
info->agf_bp = NULL;
@@ -998,7 +1091,7 @@ xfs_getfsmap_rtdev_rmapbt(
xfs_rtgroup_unlock(to_rtg(bt_cur->bc_group),
XFS_RTGLOCK_RMAP |
XFS_RTGLOCK_REFCOUNT);
- xfs_getfsmap_del_cursors(bt_cur, 0);
+ xfs_getfsmap_del_cursors(info, bt_cur, 0);
bt_cur = NULL;
}
@@ -1036,7 +1129,7 @@ xfs_getfsmap_rtdev_rmapbt(
if (bt_cur) {
xfs_rtgroup_unlock(to_rtg(bt_cur->bc_group),
XFS_RTGLOCK_RMAP | XFS_RTGLOCK_REFCOUNT);
- xfs_getfsmap_del_cursors(bt_cur, error);
+ xfs_getfsmap_del_cursors(info, bt_cur, error);
}
/* loop termination case */
--
2.34.1