OpenVZ / Virtuozzo kernel development (devel@openvz.org)
 help / color / mirror / Atom feed
* [Devel] [PATCH hci-8.1 v2 1/5] fs/ext4: rectify behaviour of swapext ioctl on error
@ 2026-11-17 16:58 Alexey Kuznetsov
  2026-11-17 16:59 ` [Devel] [PATCH hci-8.1 v2 2/5] fs/ext4: ensure ext4 transaction credits while swapext Alexey Kuznetsov
                   ` (5 more replies)
  0 siblings, 6 replies; 14+ messages in thread
From: Alexey Kuznetsov @ 2026-11-17 16:58 UTC (permalink / raw)
  To: devel, khorenko, kui.liu, azaitsev

swapext ioctl (like mainstream move_extent) has to have
unorthodox behaviour on error. It can be partial like write()
and then partial success must be returned to calling user.
Unlike write() ioctl really returns an error, but also
ioctl request struct is modified with partial work results.

The fixed version reports correct moved_len to caller even
in case of errors. Now it is guaranteed that area
before moved_len is swapped and area beyond moved_len
is not affected.

Also it fixes bugs: first, when a pass stopped part-way
with ENOSPC or EBUSY and was retried, swapext retried
the already swapped area and failed with ENODATA.
Second, on error swapext left moved_len short or zero, so it
neither discarded preallocations of the inodes nor invalidated
page cache on the swapped area. Not discarding preallocations
could lead to double free of blocks. The same bug was fixed in
mainstream move_extent by 55583e899a53 ("ext4: fix double-free
of blocks due to wrong extents moved_len").

Fixes: 1a3d440e7fbc ("fs/ext4: swapext ioctl")
Affects: #VSTOR-148028, #VSTOR-100610
https://virtuozzo.atlassian.net/browse/VSTOR-100610
https://virtuozzo.atlassian.net/browse/VSTOR-148028

Signed-off-by: Alexey Kuznetsov <kuznet@virtuozzo.com>

Feature: ext4: swapext ioctl: swap range of blocks between two files
---
 fs/ext4/move_extent.c | 26 +++++++++++++-------------
 1 file changed, 13 insertions(+), 13 deletions(-)

diff --git a/fs/ext4/move_extent.c b/fs/ext4/move_extent.c
index f08604816892..0e8d1366da7a 100644
--- a/fs/ext4/move_extent.c
+++ b/fs/ext4/move_extent.c
@@ -714,7 +714,7 @@ swap_one_extent(struct file *o_filp, struct inode *donor_inode,
 	struct inode *orig_inode = file_inode(o_filp);
 	handle_t *handle;
 	int jblocks, retries = 0;
-	int replaced_count = 0;
+	int rc = 0;
 	struct super_block *sb = orig_inode->i_sb;
 
 	/*
@@ -731,13 +731,13 @@ swap_one_extent(struct file *o_filp, struct inode *donor_inode,
 	handle = ext4_journal_start(orig_inode, EXT4_HT_MOVE_EXTENTS, jblocks);
 	if (IS_ERR(handle)) {
 		*err = PTR_ERR(handle);
-		return 0;
+		return rc;
 	}
 
 	ext4_double_down_write_data_sem(orig_inode, donor_inode);
-	replaced_count = ext4_swap_extents_2(handle, orig_inode, donor_inode,
-					   orig_blk, donor_blk, len,
-					   insn, err, se);
+	rc += ext4_swap_extents_2(handle, orig_inode, donor_inode,
+				  orig_blk + rc, donor_blk + rc, len - rc,
+				  insn, err, se);
 	ext4_double_up_write_data_sem(orig_inode, donor_inode);
 
 	se->s_dirty += handle->h_requested_credits - handle->h_total_credits;
@@ -752,7 +752,7 @@ swap_one_extent(struct file *o_filp, struct inode *donor_inode,
 	if (*err == -EBUSY && retries++ < 4 && EXT4_SB(sb)->s_journal &&
 	    jbd2_journal_force_commit_nested(EXT4_SB(sb)->s_journal))
 		goto again;
-	return replaced_count;
+	return rc;
 }
 
 static int
@@ -762,7 +762,7 @@ expose_one_extent(struct inode *donor_inode,
 {
 	handle_t *handle;
 	int jblocks, retries = 0;
-	int replaced_count = 0;
+	int rc = 0;
 	struct super_block *sb = donor_inode->i_sb;
 
 again:
@@ -771,13 +771,13 @@ expose_one_extent(struct inode *donor_inode,
 	handle = ext4_journal_start(donor_inode, EXT4_HT_MOVE_EXTENTS, jblocks);
 	if (IS_ERR(handle)) {
 		*err = PTR_ERR(handle);
-		return 0;
+		return rc;
 	}
 
 	down_write(&EXT4_I(donor_inode)->i_data_sem);
-	replaced_count = ext4_expose_extents_2(handle, donor_inode,
-					       donor_blk, len,
-					       insn, err, se);
+	rc += ext4_expose_extents_2(handle, donor_inode,
+				    donor_blk + rc, len - rc,
+				    insn, err, se);
 	up_write(&EXT4_I(donor_inode)->i_data_sem);
 
 	se->s_dirty += handle->h_requested_credits - handle->h_total_credits;
@@ -791,7 +791,7 @@ expose_one_extent(struct inode *donor_inode,
 	if (*err == -EBUSY && retries++ < 4 && EXT4_SB(sb)->s_journal &&
 	    jbd2_journal_force_commit_nested(EXT4_SB(sb)->s_journal))
 		goto again;
-	return replaced_count;
+	return rc;
 }
 
 static unsigned int count_credits(struct inode *inode, ext4_lblk_t start, ext4_lblk_t len,
@@ -974,12 +974,12 @@ ext4_ioc_swap_extents(struct file *o_filp, struct file *d_filp, __u64 orig_blk,
 			ret = 0;
 			rc = cur_len;
 		}
+		se->moved_len += rc;
 		if (ret < 0)
 			break;
 		o_start += rc;
 		d_start += rc;
 	}
-	se->moved_len = d_start - donor_blk;
 
 out:
 	if (se->moved_len) {
-- 
2.47.1

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2026-11-19 11:01 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-17 16:58 [Devel] [PATCH hci-8.1 v2 1/5] fs/ext4: rectify behaviour of swapext ioctl on error Alexey Kuznetsov
2026-11-17 16:59 ` [Devel] [PATCH hci-8.1 v2 2/5] fs/ext4: ensure ext4 transaction credits while swapext Alexey Kuznetsov
2026-11-18 10:55   ` Konstantin Khorenko
2026-11-17 16:59 ` [Devel] [PATCH hci-8.1 v2 3/5] fs/ext4: assert presence of journal for swapext Alexey Kuznetsov
2026-11-18 10:55   ` Konstantin Khorenko
2026-11-17 17:00 ` [Devel] [PATCH hci-8.1 v2 4/5] fs/ext4: the swapext ioctl could fail with -ENOSPC and a jbd2 WARN Alexey Kuznetsov
2026-11-18 10:55   ` Konstantin Khorenko
2026-11-17 17:01 ` [Devel] [PATCH hci-8.1 v2 5/5] fs/ext4: do not permit swapext on fs with fast commit enabled Alexey Kuznetsov
2026-11-18 10:55   ` Konstantin Khorenko
2026-11-18 10:55 ` [Devel] [PATCH hci-8.1 v2 1/5] fs/ext4: rectify behaviour of swapext ioctl on error Konstantin Khorenko
2026-11-18 10:55 ` Konstantin Khorenko
2026-11-18 15:04   ` Konstantin Khorenko
2026-11-18 19:43     ` Konstantin Khorenko
2026-11-19 10:59       ` Alexey Kuznetsov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox