Re: [PATCH] xfs: optimize getfsmap by reusing and advancing the refcount cursor
From: Christoph Hellwig
Date: Wed Nov 25 2026 - 01:58:12 EST
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.