mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] xfs: optimize getfsmap by reusing and advancing the refcount cursor
@ 2026-11-23  7:50 Shida Zhang
  2026-11-25  6:58 ` Christoph Hellwig
  2026-11-26  1:30 ` [PATCH v2 0/2] " zhangshida2026
  0 siblings, 2 replies; 7+ messages in thread
From: Shida Zhang @ 2026-11-23  7:50 UTC (permalink / raw)
  To: Carlos Maiolino; +Cc: linux-xfs, linux-kernel, djwong

GETFSMAP sweeps the whole device: online fsck, dedup scanners and
imaging tools walk thousands of records per call.

Every record pays two costs.  xfs_getfsmap_is_shared() allocates and
frees a refcount btree cursor, and xfs_refcount_find_shared() searches
the refcount btree from the root via xfs_btree_lookup().  A fresh cursor
re-reads the root block each time; the search re-descends root to leaf
even when the answer is the next slot.

Keep one cursor per group and use its position: look up once, cache the
record, then step forward with xfs_btree_increment() instead of
re-searching from the root.  This gives the same answer as
xfs_refcount_find_shared(), with O(1) memory.  The cursor is dropped when
the group changes, before the AGF buffer is released.

One GETFSMAP call asks is_shared() once per rmap record: thousands of
questions per call, all about blocks, never files.

Small example: foo at blocks 10-19, bar a reflink copy of foo, baz alone
at 20-29.  Shared blocks have one rmap record per owner, so one call
walks three records:

  rmap record     answer   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

The real workload: one call over 3000 reflinked files walks ~6000
shareable records.  Old code: ~6000 cursor allocs and ~6000 root-to-leaf
descents per call.  New code: one cursor per group, 4 descents (one per
AG), and a cache hit or single forward step per question.

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     331 us   (9.2x)
  xfs_btree_lookup    8.2%        ~0%      (% of cycles)

The cursor alloc/free and root-block get/put are gone too:
_raw_spin_unlock_irqrestore 16.6% -> 0.8%, kmem_cache_alloc 5.6% -> 1.3%.
The full fsmap output is byte-identical before and after (6217 records).

Signed-off-by: Shida Zhang <zhangshida@kylinos.cn>
---
 fs/xfs/xfs_fsmap.c | 120 ++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 103 insertions(+), 17 deletions(-)

Testing: xfstests with rmapbt=1,reflink=1 passes the fsmap/rmap/clone subset
(xfs/271-274, 277, 114, 233, 234, 236, 517, 560, 600, generic/127, 129). The
full fsmap record set is byte-identical before and after.

Not tested: the rtrefcountbt advance path (no RT device on the test box).
It uses the same logic via the rtrefcountbt cursor, and the rtrmap walk is
also sorted.

diff --git a/fs/xfs/xfs_fsmap.c b/fs/xfs/xfs_fsmap.c
index 041bb2105ec..0a6a87c3a02 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,10 @@ 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_positioned;	/* refcount cursor positioned */
+	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 */
@@ -202,7 +208,26 @@ xfs_getfsmap_dev_compare(
 	return d1->dev - d2->dev;
 }
 
-/* Decide if this mapping is shared. */
+/* Drop the cached refcount btree cursor when leaving a group. */
+static void
+xfs_getfsmap_put_refcount_cur(
+	struct xfs_getfsmap_info	*info,
+	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;
+	}
+}
+
+/*
+ * Decide if this mapping is shared.
+ *
+ * The refcount cursor is cached across calls and only advances forward, so
+ * this must be called with non-decreasing agbno (frec->rec_key).  The rmapbt
+ * walk visits records in increasing startblock order, which guarantees it.
+ */
 STATIC int
 xfs_getfsmap_is_shared(
 	struct xfs_trans		*tp,
@@ -212,31 +237,88 @@ 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;
+	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->refcount_cur) {
+		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;
+		info->rc_positioned = false;
+		info->rc_eof = false;
+	}
+	cur = info->refcount_cur;
+	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);
+	/*
+	 * The rmapbt walk visits records in increasing startblock order, so the
+	 * refcount cursor only ever advances forward.  Position it once with a
+	 * full lookup, then advance incrementally rather than re-descending the
+	 * btree from the root for every record.
+	 */
+	if (!info->rc_positioned) {
+		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;
+			}
+		}
+		info->rc_positioned = true;
+		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;
+		}
+	}
 
-	xfs_btree_del_cursor(cur, error);
-	if (error)
-		return error;
+	/* Advance to the first SHARED record that ends after agbno. */
+	while (info->rc_cur.rc_domain != XFS_REFC_DOMAIN_SHARED ||
+	       info->rc_cur.rc_startblock + info->rc_cur.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 (info->rc_cur.rc_startblock < agbno + aglen)
+		*stat = true;
 	return 0;
+
+out_error:
+	xfs_getfsmap_put_refcount_cur(info, error);
+	return error;
 }
 
 static inline void
@@ -547,6 +629,7 @@ __xfs_getfsmap_datadev(
 		if (bt_cur) {
 			xfs_btree_del_cursor(bt_cur, XFS_BTREE_NOERROR);
 			bt_cur = NULL;
+			xfs_getfsmap_put_refcount_cur(info, 0);
 			xfs_trans_brelse(tp, info->agf_bp);
 			info->agf_bp = NULL;
 		}
@@ -588,6 +671,7 @@ __xfs_getfsmap_datadev(
 	if (bt_cur)
 		xfs_btree_del_cursor(bt_cur, error < 0 ? XFS_BTREE_ERROR :
 							 XFS_BTREE_NOERROR);
+	xfs_getfsmap_put_refcount_cur(info, error);
 	if (info->agf_bp) {
 		xfs_trans_brelse(tp, info->agf_bp);
 		info->agf_bp = NULL;
@@ -986,6 +1070,7 @@ xfs_getfsmap_rtdev_rmapbt(
 		}
 
 		if (bt_cur) {
+			xfs_getfsmap_put_refcount_cur(info, 0);
 			xfs_rtgroup_unlock(to_rtg(bt_cur->bc_group),
 					XFS_RTGLOCK_RMAP |
 					XFS_RTGLOCK_REFCOUNT);
@@ -1025,6 +1110,7 @@ xfs_getfsmap_rtdev_rmapbt(
 	}
 
 	if (bt_cur) {
+		xfs_getfsmap_put_refcount_cur(info, error);
 		xfs_rtgroup_unlock(to_rtg(bt_cur->bc_group),
 				XFS_RTGLOCK_RMAP | XFS_RTGLOCK_REFCOUNT);
 		xfs_btree_del_cursor(bt_cur, error < 0 ? XFS_BTREE_ERROR :
-- 
2.34.1


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] xfs: optimize getfsmap by reusing and advancing the refcount cursor
  2026-11-23  7:50 [PATCH] xfs: optimize getfsmap by reusing and advancing the refcount cursor Shida Zhang
@ 2026-11-25  6:58 ` Christoph Hellwig
  2026-11-26  1:30 ` [PATCH v2 0/2] " zhangshida2026
  1 sibling, 0 replies; 7+ messages in thread
From: Christoph Hellwig @ 2026-11-25  6:58 UTC (permalink / raw)
  To: Shida Zhang; +Cc: Carlos Maiolino, linux-xfs, linux-kernel, djwong

On Mon, Nov 23, 2026 at 03:50:48PM +0800, Shida Zhang wrote:
> GETFSMAP sweeps the whole device: online fsck, dedup scanners and
> imaging tools walk thousands of records per call.
> 
> Every record pays two costs.  xfs_getfsmap_is_shared() allocates and
> frees a refcount btree cursor, and xfs_refcount_find_shared() searches
> the refcount btree from the root via xfs_btree_lookup().  A fresh cursor
> re-reads the root block each time; the search re-descends root to leaf
> even when the answer is the next slot.

This reads pretty odd.  Maybe try to reword it a bit using your own
writing instead of an LLM?

> Testing: xfstests with rmapbt=1,reflink=1 passes the fsmap/rmap/clone subset
> (xfs/271-274, 277, 114, 233, 234, 236, 517, 560, 600, generic/127, 129). The
> full fsmap record set is byte-identical before and after.

A full -g auto run would also be useful as we have quite a few tests
that use getfsmap under the hood.

> Not tested: the rtrefcountbt advance path (no RT device on the test box).
> It uses the same logic via the rtrefcountbt cursor, and the rtrmap walk is
> also sorted.

With the zoned allocator, you can use an internal RT device.
Just add "-r zoned" to the test and scratch mkfs options.

> @@ -161,6 +163,10 @@ 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 */

Overly long line here, and weird indentation for rc_cur.
Please go over the patch and fix all the long lines, not going to
report more of those.

> +/* Drop the cached refcount btree cursor when leaving a group. */
> +static void
> +xfs_getfsmap_put_refcount_cur(
> +	struct xfs_getfsmap_info	*info,
> +	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;
> +	}
> +}

All the callers also delete the rmap btree.  So maybe do a prep patch
to add a xfs_getfsmap_del_cursors helper which initially just does
this work for the rmap one, and where you can then add the refcount
cursor deletion as well.

> +	if (!info->refcount_cur) {
> +		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;
> +		info->rc_positioned = false;
> +		info->rc_eof = false;
> +	}
> +	cur = info->refcount_cur;
> +	if (info->rc_eof)
> +		return 0;

I don't see why we'd want to allocate a new cursor at EOF.  So this
check should probably move to before the cursor allocation.

> +	if (!info->rc_positioned) {

And this only happens if the cursor was just allocatd.  So the two
branches can be merged, and rc_positioned can be removed.  Bonous
points of splitting the cursor allocation and initial positioning
into a helper.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v2 0/2] xfs: optimize getfsmap by reusing and advancing the refcount cursor
  2026-11-23  7:50 [PATCH] xfs: optimize getfsmap by reusing and advancing the refcount cursor Shida Zhang
  2026-11-25  6:58 ` Christoph Hellwig
@ 2026-11-26  1:30 ` zhangshida2026
  2026-11-26  1:30   ` [PATCH v2 1/2] xfs: factor out a xfs_getfsmap_del_cursors helper zhangshida2026
  2026-11-26  1:30   ` [PATCH v2 2/2] xfs: optimize getfsmap by reusing and advancing the refcount cursor zhangshida2026
  1 sibling, 2 replies; 7+ messages in thread
From: zhangshida2026 @ 2026-11-26  1:30 UTC (permalink / raw)
  To: Carlos Maiolino
  Cc: Darrick J . Wong, Christoph Hellwig, linux-xfs, linux-kernel,
	zhangshida2026, Shida Zhang

From: Shida Zhang <zhangshida@kylinos.cn>

GETFSMAP walks the rmap btree in increasing startblock order and asks
the refcount btree whether each record's blocks are shared.  Every
record pays a cursor alloc/free and a root-to-leaf btree search, even
though the next query is almost always the next refcount record.

This series keeps one refcount cursor per group, positions it once, and
advances it with xfs_btree_increment() as the rmap walk moves forward.
Patch 1 factors out the cursor cleanup so patch 2 can add the cached
refcount cursor without duplicating it.

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 series
  walk time           3048 us     324 us   (9.4x)
  xfs_btree_lookup    10.7%       ~0%      (% of cycles)

The full fsmap output is byte-identical before and after (6421 records).

---

v2:
 - split into a prep patch (xfs_getfsmap_del_cursors helper) and the
   optimization, per hch
 - merged the cursor alloc and initial-position branches, removed
   rc_positioned; alloc+position is now xfs_getfsmap_refcount_init()
 - EOF check moved before cursor allocation; rc_eof reset on cursor
   free so it can't leak across groups
 - fixed all lines over 80 columns and the rc_cur indentation
 - reworded the commit messages

v1: https://lore.kernel.org/linux-xfs/20261123075048.3658960-1-zhangshida@kylinos.cn/

Testing (7.3.4, Kunpeng 920 + NVMe, mkfs -m rmapbt=1,reflink=1):
 - full xfstests -g auto: 1166 tests run; the 30 failures are environmental
   (none use fsmap) and reproduce on the unpatched base kernel
 - data fsmap byte-identical base vs series (6421 records)
 - realtime reflink (rtrefcountbt) path exercised: fsmap over a reflinked
   realtime volume is byte-identical, 1000 shared records

Shida Zhang (2):
  xfs: factor out a xfs_getfsmap_del_cursors helper
  xfs: optimize getfsmap by reusing and advancing the refcount cursor

 fs/xfs/xfs_fsmap.c | 149 +++++++++++++++++++++++++++++++++++++--------
 1 file changed, 125 insertions(+), 24 deletions(-)

-- 
2.34.1


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v2 1/2] xfs: factor out a xfs_getfsmap_del_cursors helper
  2026-11-26  1:30 ` [PATCH v2 0/2] " zhangshida2026
@ 2026-11-26  1:30   ` zhangshida2026
  2026-11-30 12:39     ` Christoph Hellwig
  2026-11-26  1:30   ` [PATCH v2 2/2] xfs: optimize getfsmap by reusing and advancing the refcount cursor zhangshida2026
  1 sibling, 1 reply; 7+ messages in thread
From: zhangshida2026 @ 2026-11-26  1:30 UTC (permalink / raw)
  To: Carlos Maiolino
  Cc: Darrick J . Wong, Christoph Hellwig, linux-xfs, linux-kernel,
	zhangshida2026, Shida Zhang

From: Shida Zhang <zhangshida@kylinos.cn>

The data and rt device getfsmap walkers open-code the same rmap btree
cursor deletion at group boundaries and at the end of the walk.  Fold
it into a helper in preparation for caching a refcount cursor across
the walk.

No functional change.

Suggested-by: Christoph Hellwig <hch@infradead.org>
Signed-off-by: Shida Zhang <zhangshida@kylinos.cn>
---
 fs/xfs/xfs_fsmap.c | 22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)

diff --git a/fs/xfs/xfs_fsmap.c b/fs/xfs/xfs_fsmap.c
index 041bb2105ec..fe9093c0987 100644
--- a/fs/xfs/xfs_fsmap.c
+++ b/fs/xfs/xfs_fsmap.c
@@ -202,6 +202,17 @@ xfs_getfsmap_dev_compare(
 	return d1->dev - d2->dev;
 }
 
+/* Delete the btree cursors used by a getfsmap group walk. */
+static void
+xfs_getfsmap_del_cursors(
+	struct xfs_btree_cur	*bt_cur,
+	int			error)
+{
+	if (bt_cur)
+		xfs_btree_del_cursor(bt_cur, error < 0 ?
+				XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
+}
+
 /* Decide if this mapping is shared. */
 STATIC int
 xfs_getfsmap_is_shared(
@@ -545,7 +556,7 @@ __xfs_getfsmap_datadev(
 		}
 
 		if (bt_cur) {
-			xfs_btree_del_cursor(bt_cur, XFS_BTREE_NOERROR);
+			xfs_getfsmap_del_cursors(bt_cur, 0);
 			bt_cur = NULL;
 			xfs_trans_brelse(tp, info->agf_bp);
 			info->agf_bp = NULL;
@@ -585,9 +596,7 @@ __xfs_getfsmap_datadev(
 		info->group = NULL;
 	}
 
-	if (bt_cur)
-		xfs_btree_del_cursor(bt_cur, error < 0 ? XFS_BTREE_ERROR :
-							 XFS_BTREE_NOERROR);
+	xfs_getfsmap_del_cursors(bt_cur, error);
 	if (info->agf_bp) {
 		xfs_trans_brelse(tp, info->agf_bp);
 		info->agf_bp = NULL;
@@ -989,7 +998,7 @@ xfs_getfsmap_rtdev_rmapbt(
 			xfs_rtgroup_unlock(to_rtg(bt_cur->bc_group),
 					XFS_RTGLOCK_RMAP |
 					XFS_RTGLOCK_REFCOUNT);
-			xfs_btree_del_cursor(bt_cur, XFS_BTREE_NOERROR);
+			xfs_getfsmap_del_cursors(bt_cur, 0);
 			bt_cur = NULL;
 		}
 
@@ -1027,8 +1036,7 @@ xfs_getfsmap_rtdev_rmapbt(
 	if (bt_cur) {
 		xfs_rtgroup_unlock(to_rtg(bt_cur->bc_group),
 				XFS_RTGLOCK_RMAP | XFS_RTGLOCK_REFCOUNT);
-		xfs_btree_del_cursor(bt_cur, error < 0 ? XFS_BTREE_ERROR :
-							 XFS_BTREE_NOERROR);
+		xfs_getfsmap_del_cursors(bt_cur, error);
 	}
 
 	/* loop termination case */
-- 
2.34.1


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v2 2/2] xfs: optimize getfsmap by reusing and advancing the refcount cursor
  2026-11-26  1:30 ` [PATCH v2 0/2] " zhangshida2026
  2026-11-26  1:30   ` [PATCH v2 1/2] xfs: factor out a xfs_getfsmap_del_cursors helper zhangshida2026
@ 2026-11-26  1:30   ` zhangshida2026
  2026-11-30 12:51     ` Christoph Hellwig
  1 sibling, 1 reply; 7+ messages in thread
From: zhangshida2026 @ 2026-11-26  1:30 UTC (permalink / raw)
  To: Carlos Maiolino
  Cc: Darrick J . Wong, Christoph Hellwig, linux-xfs, linux-kernel,
	zhangshida2026, Shida Zhang

From: Shida Zhang <zhangshida@kylinos.cn>

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@infradead.org>
Signed-off-by: Shida Zhang <zhangshida@kylinos.cn>
---
 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


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 1/2] xfs: factor out a xfs_getfsmap_del_cursors helper
  2026-11-26  1:30   ` [PATCH v2 1/2] xfs: factor out a xfs_getfsmap_del_cursors helper zhangshida2026
@ 2026-11-30 12:39     ` Christoph Hellwig
  0 siblings, 0 replies; 7+ messages in thread
From: Christoph Hellwig @ 2026-11-30 12:39 UTC (permalink / raw)
  To: zhangshida2026
  Cc: Carlos Maiolino, Darrick J . Wong, linux-xfs, linux-kernel, Shida Zhang

Looks good:

Reviewed-by: Christoph Hellwig <hch@lst.de>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 2/2] xfs: optimize getfsmap by reusing and advancing the refcount cursor
  2026-11-26  1:30   ` [PATCH v2 2/2] xfs: optimize getfsmap by reusing and advancing the refcount cursor zhangshida2026
@ 2026-11-30 12:51     ` Christoph Hellwig
  0 siblings, 0 replies; 7+ messages in thread
From: Christoph Hellwig @ 2026-11-30 12:51 UTC (permalink / raw)
  To: zhangshida2026
  Cc: Carlos Maiolino, Darrick J . Wong, linux-xfs, linux-kernel, Shida Zhang

On Thu, Nov 26, 2026 at 09:30:09AM +0800, zhangshida2026@163.com wrote:
> Suggested-by: Christoph Hellwig <hch@infradead.org>

Not really, this was all your own idea.  I just did a round of feedback.

> +	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 */

I should have pointed that out before, but the name for the flag is a
bit odd.  eof stands for end of file, so it doesn't really apply here.
Maybe call this rc_done

>  	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)

Next time just adjust to the final alignment in the firstr patch.

>  {
> +	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);

And maybe keep a local variable for XFS_BTREE_ERROR vs
XFS_BTREE_NOERROR.

(and we should probably turn XFS_BTREE_NOERROR/XFS_BTREE_ERROR into an
enum pass that to xfs_btree_del_cursor, but that's a separate project).

>  }
>  
> -/* 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.

This is just a summar of what the code does and not really adds any
additional information.  Please don't bother with such comments.
Comments are useful to explain why something is done, or if something
done is really non-obvious.

> @@ -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;

	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);
>
	xfs_extlen_t			aglen =
		XFS_BB_TO_FSBT(mp, frec->len_daddr);

.. or just do away with the variable as it just has a single user.

> +	}
> +	cur = info->refcount_cur;

I'd probably do away with this variable.  Given that the function deals
with two different cursors, having the local variable isn't super
helpful.

>  
> +	/* 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) {

Sashiko complains that this iterators over XFS_REFC_DOMAIN_COW
unintentionally and I think it is right.


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-11-30 12:51 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-23  7:50 [PATCH] xfs: optimize getfsmap by reusing and advancing the refcount cursor Shida Zhang
2026-11-25  6:58 ` Christoph Hellwig
2026-11-26  1:30 ` [PATCH v2 0/2] " zhangshida2026
2026-11-26  1:30   ` [PATCH v2 1/2] xfs: factor out a xfs_getfsmap_del_cursors helper zhangshida2026
2026-11-30 12:39     ` Christoph Hellwig
2026-11-26  1:30   ` [PATCH v2 2/2] xfs: optimize getfsmap by reusing and advancing the refcount cursor zhangshida2026
2026-11-30 12:51     ` Christoph Hellwig

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®