From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=220.197.31.4 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796174548; b=pkBL5+Z73d95TWsCkLdm+0PFZ1F32FIO/EDA11tpUYcaxoQlZLyLbZ2avcyJoW/YRZMM tqX3LziV31BYur8QAHuYuzjD0luWxfl0Y2SX4i5R9M1EQqKSb08jCKBfkDsfA+UDE2sGk Sr7CWhqpCeuQdNWlVMvHET8puQVoG04ZLGgO6LpOMQC9b4doNj1+sY8SNB6+Z7IYA5Quo PWuM/cgyPdtkF0w5iEQsFLR5oFwb7Fbr9Jr2NanicVUv9lG3rmqH/aPR3GpiyDO/yJAFp 3e3QuBFhGpq+TFdcjEmCGvrksvdIxtudRJiM1vGL+Fg9pUKXGk5wGK0H2upLf69gR9g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796174548; h=DMARC-Filter:DKIM-Signature:Received:From:To:Cc:Subject:Date: Message-Id:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-CM-TRANSID:X-Coremail-Antispam: X-CM-SenderInfo; bh=vlDu4DOBCevfZP3haRR7GRF3uFwO78r3AptZyc8hpOA=; b=zjcVU6hc7HYuyO8Tf9TltB/KY2zPDb7LSolOA0Q8gfbH556v/CNaDn1VFm5YGDtKNIPk UUf6DKaSDpxAprfiGYJnUbhbtpgXCAwq6ZM7stZ3AdOvOlouUXU3LUiWwZV/P8cykSemP DgT5I/o1jsETVaVSbTM+uT1LwAvwBWfZlRD2+0/qfQWyT4iYdUNlYmBlGhJ/qxRh/p5n3 xIvFntjEABOyJMYBtManSncl8Ak4B/NysDZDF90vozNCrKZAY7IyeMDDx6Mr9x+5q8KKu dNpQgMVCsMooiLx9OdJf6uzGjOPXphFQkvIRlapZqmiDJjTn955PVhbfwYSe4/Jeo3w== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=163.com; spf=pass smtp.mailfrom=163.com; dkim=pass header.d=163.com header.i=@163.com header.a=rsa-sha256 header.s=s110527 header.b=Ev91hz+o; arc=none smtp.remote-ip=220.197.31.4 Received: from m16.mail.163.com (m16.mail.163.com [220.197.31.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A8DA3606A31A; Wed, 2 Dec 2026 01:22:25 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A8DA3606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=163.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.a=rsa-sha256 header.s=s110527 header.b=Ev91hz+o DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=vl Du4DOBCevfZP3haRR7GRF3uFwO78r3AptZyc8hpOA=; b=Ev91hz+oYksXv0BEog d5SqYcC3ujdabwpXrnTySmBQoefNfXfzYTIFGJAntM39qdAV1drzevNkldK0kto/ FKlDyNNzXjXjIztfk5UJuGSShZ+H7FxKWua9mXio/HL4BoAHoJoj4JWW1ZwLcsQS LOjTg4Z+MOvlipaDqML+oGJ8g= Received: from localhost.localdomain (unknown []) by gzsmtp4 (Coremail) with SMTP id PygvCgD376Owcg9rCSiYJQ--.18561S4; Wed, 02 Dec 2026 09:21:54 +0800 (CST) From: zhangshida2026@163.com To: Carlos Maiolino Cc: "Darrick J . Wong" , Christoph Hellwig , linux-xfs@vger.kernel.org, linux-kernel@vger.kernel.org, zhangshida2026@163.com, Shida Zhang Subject: [PATCH v4 2/2] xfs: optimize getfsmap by reusing and advancing the refcount cursor Date: Wed, 2 Dec 2026 09:21:52 +0800 Message-Id: <20261202012152.3846288-3-zhangshida2026@163.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20261202012152.3846288-1-zhangshida2026@163.com> References: <20261202012152.3846288-1-zhangshida2026@163.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID:PygvCgD376Owcg9rCSiYJQ--.18561S4 X-Coremail-Antispam: 1Uf129KBjvJXoW3Ww15WF1xKFy8ZF17Cr1UAwb_yoW3urWkpF ZrtasxKw4ktF92grn7J34jvF4fX34Fyw4rJr9xGa4Sv347tr13Xwn5KFyF9FWDCrWfArW2 qrZ8Cw1rK34S9aDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0zRs0edUUUUU= X-CM-SenderInfo: x2kd0wxvklvtasqslqqrwthudrp/xtbC6BNnc2sPcrOQVAAA3O From: Shida Zhang 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 SHARED records that end before the current block, stopping at the first COW record; the record we stop at decides: shared iff it is a SHARED record that 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 3040 us 335 us (9.1x) xfs_btree_lookup 11.9% ~0% (% of cycles) The cursor alloc/free and root-block get/put are gone too: _raw_spin_unlock_irqrestore 15.1% -> 1.6%, kmem_cache_alloc 4.7% -> gone. The full fsmap output is byte-identical before and after (6421 records). Signed-off-by: Shida Zhang --- fs/xfs/xfs_fsmap.c | 136 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 112 insertions(+), 24 deletions(-) diff --git a/fs/xfs/xfs_fsmap.c b/fs/xfs/xfs_fsmap.c index 6cef6f95026..e8b98a75087 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 */ 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_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,55 @@ 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; + /* Step to the first SHARED record ending after agbno; stop at COW. */ + while (rc->rc_domain == XFS_REFC_DOMAIN_SHARED && + 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 +644,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 +684,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 +1086,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 +1124,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