[Devel] [PATCH hci-8.1 v2 1/5] fs/ext4: rectify behaviour of swapext ioctl on error

Alexey Kuznetsov kuznet at virtuozzo.com
Tue Nov 17 19:58:14 MSK 2026


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 at 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



More information about the Devel mailing list