* [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
0 siblings, 1 reply; 2+ 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] 2+ 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
0 siblings, 0 replies; 2+ 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] 2+ messages in thread
end of thread, other threads:[~2026-11-25 6:58 UTC | newest]
Thread overview: 2+ 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
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®