All Virtuozzo development lists (kernel + QEMU)
 help / color / mirror / Atom feed
From: Alexey Kuznetsov <kuznet@virtuozzo.com>
To: devel@openvz.org, khorenko@virtuozzo.com, kui.liu@virtuozzo.com,
	azaitsev@virtuozzo.com
Subject: [Devel] [PATCH hci-8.1 v2 1/5] fs/ext4: rectify behaviour of swapext ioctl on error
Date: Wed, 18 Nov 2026 00:58:14 +0800	[thread overview]
Message-ID: <avyHpuWTiPujndCE@mops2> (raw)

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

                 reply	other threads:[~2026-11-17 16:59 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=avyHpuWTiPujndCE@mops2 \
    --to=kuznet@virtuozzo.com \
    --cc=azaitsev@virtuozzo.com \
    --cc=devel@openvz.org \
    --cc=khorenko@virtuozzo.com \
    --cc=kui.liu@virtuozzo.com \
    /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.