All of lore.kernel.org
 help / color / mirror / Atom feed
From: cem@kernel.org
To: cem@kernel.org
Cc: linux-xfs@vger.kernel.org, hch@lst.de, sandeen@sandeen.net,
	dgc@kernel.org, aalbersh@kernel.org, bfoster@redhat.com,
	stable@vger.kernel.org
Subject: [PATCH v3 2/2] xfs: force all shared extent swap to use rmap shared path
Date: Wed, 25 Nov 2026 18:46:00 +0100	[thread overview]
Message-ID: <20261125174607.135959-3-cem@kernel.org> (raw)
In-Reply-To: <20261125174607.135959-1-cem@kernel.org>

From: Carlos Maiolino <cem@kernel.org>

Currently when swapping inodes with shared extents in a rmap enabled FS,
we enforce the rmap updates to use the rmap_shared path only when the
target inode has shared extents, but we should also be using the
rmap_shared path for the case when only the source inode's extent is
shared.

Reported-by: Lauri Gates <lauri.gates@gmail.com>
Fixes: 1f08af52e7c9 ("xfs: implement swapext for rmap filesystems")
Cc: stable@vger.kernel.org
Signed-off-by: Carlos Maiolino <cmaiolino@redhat.com>
---
V3: Remove redundant code I forgot to remove on V2...

V2: Use a transaction roll when setting reflink flag to the inodes if we
need to do update flags. I was initially avoiding doing so but seems
like any attempt to fix it without logging the change always risks to
leave a timebomb behind.

 fs/xfs/xfs_bmap_util.c | 60 +++++++++++++++++++++++++++---------------
 1 file changed, 39 insertions(+), 21 deletions(-)

diff --git a/fs/xfs/xfs_bmap_util.c b/fs/xfs/xfs_bmap_util.c
index 73e7db3203c0..4808bb728a95 100644
--- a/fs/xfs/xfs_bmap_util.c
+++ b/fs/xfs/xfs_bmap_util.c
@@ -1302,17 +1302,6 @@ xfs_swap_extent_rmap(
 	xfs_filblks_t			ilen;
 	xfs_filblks_t			rlen;
 	int				nimaps;
-	uint64_t			tip_flags2;
-
-	/*
-	 * If the source file has shared blocks, we must flag the donor
-	 * file as having shared blocks so that we get the shared-block
-	 * rmap functions when we go to fix up the rmaps.  The flags
-	 * will be switch for reals later.
-	 */
-	tip_flags2 = tip->i_diflags2;
-	if (ip->i_diflags2 & XFS_DIFLAG2_REFLINK)
-		tip->i_diflags2 |= XFS_DIFLAG2_REFLINK;
 
 	offset_fsb = 0;
 	end_fsb = XFS_B_TO_FSB(ip->i_mount, i_size_read(VFS_I(ip)));
@@ -1399,12 +1388,10 @@ xfs_swap_extent_rmap(
 		offset_fsb += ilen;
 	}
 
-	tip->i_diflags2 = tip_flags2;
 	return 0;
 
 out:
 	trace_xfs_swap_extent_rmap_error(ip, error, _RET_IP_);
-	tip->i_diflags2 = tip_flags2;
 	return error;
 }
 
@@ -1547,6 +1534,41 @@ xfs_swap_change_owner(
 	return error;
 }
 
+/*
+ * If either file has shared blocks, both may own shared blocks once the extents
+ * are exchanged, so set reflink flag on both and init the CoW fork.
+ *
+ * rmap code chooses how to process each extent based on the reflink flag on the
+ * inode being remapped.  Both at runtime and when log recovery attempts to
+ * replay deferred bmap intents.
+ */
+static int
+xfs_extents_set_reflink(
+	struct xfs_trans	**tpp,
+	struct xfs_inode	*ip,
+	struct xfs_inode	*tip)
+{
+	int			error;
+
+	if (xfs_is_reflink_inode(ip) == xfs_is_reflink_inode(tip))
+		return 0;
+
+	ip->i_diflags2 |= XFS_DIFLAG2_REFLINK;
+	tip->i_diflags2 |= XFS_DIFLAG2_REFLINK;
+	xfs_ifork_init_cow(ip);
+	xfs_ifork_init_cow(tip);
+	xfs_trans_log_inode(*tpp, ip, XFS_ILOG_CORE);
+	xfs_trans_log_inode(*tpp, tip, XFS_ILOG_CORE);
+
+	error = xfs_trans_roll(tpp);
+	if (error)
+		return error;
+
+	xfs_trans_ijoin(*tpp, ip, 0);
+	xfs_trans_ijoin(*tpp, tip, 0);
+	return 0;
+}
+
 int
 xfs_swap_extents(
 	struct xfs_inode	*ip,	/* target inode */
@@ -1705,6 +1727,10 @@ xfs_swap_extents(
 	src_log_flags = XFS_ILOG_CORE;
 	target_log_flags = XFS_ILOG_CORE;
 
+	error = xfs_extents_set_reflink(&tp, ip, tip);
+	if (error)
+		goto out_trans_cancel;
+
 	if (xfs_has_rmapbt(mp))
 		error = xfs_swap_extent_rmap(&tp, ip, tip);
 	else
@@ -1713,14 +1739,6 @@ xfs_swap_extents(
 	if (error)
 		goto out_trans_cancel;
 
-	/* Either inode may now own shared blocks; flag both, with CoW forks. */
-	if ((ip->i_diflags2 | tip->i_diflags2) & XFS_DIFLAG2_REFLINK) {
-		ip->i_diflags2 |= XFS_DIFLAG2_REFLINK;
-		tip->i_diflags2 |= XFS_DIFLAG2_REFLINK;
-		xfs_ifork_init_cow(ip);
-		xfs_ifork_init_cow(tip);
-	}
-
 	/* Swap the cow forks. */
 	if (xfs_has_reflink(mp)) {
 		ASSERT(!ip->i_cowfp ||
-- 
2.56.1


  parent reply	other threads:[~2026-11-25 17:46 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25 17:45 [PATCH v3 0/2] xfs: force all shared extent swap to use rmap shared path cem
2026-11-25 17:45 ` [PATCH v3 1/2] xfs: move _right_neighbor_result trace before erroring out cem
2026-11-30 13:12   ` Christoph Hellwig
2026-11-25 17:46 ` cem [this message]
2026-11-30 13:13   ` [PATCH v3 2/2] xfs: force all shared extent swap to use rmap shared path Christoph Hellwig
2026-12-01 11:38 ` [PATCH v3 0/2] " Carlos Maiolino

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261125174607.135959-3-cem@kernel.org \
    --to=cem@kernel.org \
    --cc=aalbersh@kernel.org \
    --cc=bfoster@redhat.com \
    --cc=dgc@kernel.org \
    --cc=hch@lst.de \
    --cc=linux-xfs@vger.kernel.org \
    --cc=sandeen@sandeen.net \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.