* [PATCH v3 0/2] xfs: optimize getfsmap by reusing and advancing the refcount cursor
@ 2026-12-01 7:49 zhangshida2026
2026-12-01 7:49 ` [PATCH v3 1/2] xfs: factor out a xfs_getfsmap_del_cursors helper zhangshida2026
2026-12-01 7:49 ` [PATCH v3 2/2] xfs: optimize getfsmap by reusing and advancing the refcount cursor zhangshida2026
0 siblings, 2 replies; 4+ messages in thread
From: zhangshida2026 @ 2026-12-01 7:49 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 3016 us 330 us (9.1x)
xfs_btree_lookup 10.9% ~0% (% of cycles)
The full fsmap output is byte-identical before and after (6421 records).
---
v3:
- the advance loop now stops at COW records to match
xfs_refcount_find_shared() exactly (v2 could skip them)
- removed Suggested-by
- renamed rc_eof -> rc_done
- dropped the summary-only init-helper comment and the single-use
aglen and cur locals; del_cursors uses a local error flag
- re-measured with the COW fix
v2: https://lore.kernel.org/linux-xfs/20261126013009.533557-1-zhangshida2026@163.com/
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):
- data fsmap byte-identical base vs series (6421 records)
- with COW records present (reflink + COW writes), fsmap is
byte-identical base vs series (245 records)
- realtime reflink (rtrefcountbt) path exercised: fsmap over a reflinked
realtime volume is byte-identical, 1000 shared records
- full xfstests -g auto: 1166 tests run; the 29 failures are
pre-existing/environmental (none use fsmap) and match the
unpatched base kernel exactly
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 | 144 +++++++++++++++++++++++++++++++++++++--------
1 file changed, 119 insertions(+), 25 deletions(-)
--
2.34.1
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v3 1/2] xfs: factor out a xfs_getfsmap_del_cursors helper
2026-12-01 7:49 [PATCH v3 0/2] xfs: optimize getfsmap by reusing and advancing the refcount cursor zhangshida2026
@ 2026-12-01 7:49 ` zhangshida2026
2026-12-01 7:49 ` [PATCH v3 2/2] xfs: optimize getfsmap by reusing and advancing the refcount cursor zhangshida2026
1 sibling, 0 replies; 4+ messages in thread
From: zhangshida2026 @ 2026-12-01 7:49 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] 4+ messages in thread
* [PATCH v3 2/2] xfs: optimize getfsmap by reusing and advancing the refcount cursor
2026-12-01 7:49 [PATCH v3 0/2] xfs: optimize getfsmap by reusing and advancing the refcount cursor zhangshida2026
2026-12-01 7:49 ` [PATCH v3 1/2] xfs: factor out a xfs_getfsmap_del_cursors helper zhangshida2026
@ 2026-12-01 7:49 ` zhangshida2026
2026-12-01 8:31 ` Christoph Hellwig
1 sibling, 1 reply; 4+ messages in thread
From: zhangshida2026 @ 2026-12-01 7:49 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. Advance past records that end before the current block;
the first record ending after it decides: shared iff it is a SHARED
record that overlaps the query range, not shared if it is a COW record.
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 3016 us 330 us (9.1x)
xfs_btree_lookup 10.9% ~0% (% of cycles)
The cursor alloc/free and root-block get/put are gone too:
_raw_spin_unlock_irqrestore 13.2% -> 0.8%, kmem_cache_alloc 5.8% -> 0.4%.
The full fsmap output is byte-identical before and after (6421 records).
Signed-off-by: Shida Zhang <zhangshida@kylinos.cn>
---
fs/xfs/xfs_fsmap.c | 138 ++++++++++++++++++++++++++++++++++++---------
1 file changed, 112 insertions(+), 26 deletions(-)
diff --git a/fs/xfs/xfs_fsmap.c b/fs/xfs/xfs_fsmap.c
index fe9093c0987..bcd1b95aa66 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_done; /* no more shared records to find */
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,75 @@ 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)
{
+ int derror;
+
+ derror = error < 0 ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR;
+ if (info->refcount_cur) {
+ xfs_btree_del_cursor(info->refcount_cur, derror);
+ info->refcount_cur = NULL;
+ info->rc_done = false;
+ }
if (bt_cur)
- xfs_btree_del_cursor(bt_cur, error < 0 ?
- XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
+ xfs_btree_del_cursor(bt_cur, derror);
}
-/* Decide if this mapping is shared. */
+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_done = 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,
@@ -222,32 +287,53 @@ xfs_getfsmap_is_shared(
bool *stat)
{
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;
+ 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_done)
+ 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_done)
+ return 0;
+ }
- xfs_btree_del_cursor(cur, error);
- if (error)
- return error;
+ /* Advance to the first record that ends after agbno. */
+ while (rc->rc_startblock + rc->rc_blockcount <= agbno) {
+ error = xfs_btree_increment(info->refcount_cur, 0, &have);
+ if (error)
+ goto out_error;
+ if (!have) {
+ info->rc_done = true;
+ return 0;
+ }
+ error = xfs_refcount_get_rec(info->refcount_cur, &info->rc_cur, &i);
+ if (error)
+ goto out_error;
+ if (XFS_IS_CORRUPT(mp, i != 1)) {
+ xfs_btree_mark_sick(info->refcount_cur);
+ error = -EFSCORRUPTED;
+ goto out_error;
+ }
+ }
- *stat = flen > 0;
+ /* Shared iff this SHARED record overlaps the query range. */
+ if (rc->rc_domain == XFS_REFC_DOMAIN_SHARED &&
+ rc->rc_startblock < agbno + XFS_BB_TO_FSBT(mp, frec->len_daddr))
+ *stat = true;
return 0;
+
+out_error:
+ xfs_getfsmap_del_cursors(info, NULL, error);
+ return error;
}
static inline void
@@ -556,7 +642,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 +682,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 +1084,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 +1122,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] 4+ messages in thread
* Re: [PATCH v3 2/2] xfs: optimize getfsmap by reusing and advancing the refcount cursor
2026-12-01 7:49 ` [PATCH v3 2/2] xfs: optimize getfsmap by reusing and advancing the refcount cursor zhangshida2026
@ 2026-12-01 8:31 ` Christoph Hellwig
0 siblings, 0 replies; 4+ messages in thread
From: Christoph Hellwig @ 2026-12-01 8:31 UTC (permalink / raw)
To: zhangshida2026
Cc: Carlos Maiolino, Darrick J . Wong, Christoph Hellwig, linux-xfs,
linux-kernel, Shida Zhang
On Tue, Dec 01, 2026 at 03:49:47PM +0800, zhangshida2026@163.com wrote:
>
> + bool rc_done; /* no more shared records to find */
overly long line. Just stip the "to find".
> 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)
As pointed out last time, please just put the final alignment in place
in the previous patch instead of the churn here.
> + /* Advance to the first record that ends after agbno. */
> + while (rc->rc_startblock + rc->rc_blockcount <= agbno) {
> + error = xfs_btree_increment(info->refcount_cur, 0, &have);
> + if (error)
> + goto out_error;
> + if (!have) {
> + info->rc_done = true;
> + return 0;
> + }
> + error = xfs_refcount_get_rec(info->refcount_cur, &info->rc_cur, &i);
Overly long line.
> + if (error)
> + goto out_error;
> + if (XFS_IS_CORRUPT(mp, i != 1)) {
> + xfs_btree_mark_sick(info->refcount_cur);
> + error = -EFSCORRUPTED;
> + goto out_error;
> + }
> + }
>
> - *stat = flen > 0;
> + /* Shared iff this SHARED record overlaps the query range. */
> + if (rc->rc_domain == XFS_REFC_DOMAIN_SHARED &&
> + rc->rc_startblock < agbno + XFS_BB_TO_FSBT(mp, frec->len_daddr))
As point out by Sashiko last round, the iteration above should just
break out once the record isn't in the shared domain, saving potentially
pointless iteration‥
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-12-01 8:31 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-01 7:49 [PATCH v3 0/2] xfs: optimize getfsmap by reusing and advancing the refcount cursor zhangshida2026
2026-12-01 7:49 ` [PATCH v3 1/2] xfs: factor out a xfs_getfsmap_del_cursors helper zhangshida2026
2026-12-01 7:49 ` [PATCH v3 2/2] xfs: optimize getfsmap by reusing and advancing the refcount cursor zhangshida2026
2026-12-01 8:31 ` 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®