OpenVZ / Virtuozzo kernel development (devel@openvz.org)
 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 1/2] fs/ext4: rectify behaviour of swapext ioctl on error
Date: Tue, 17 Nov 2026 21:24:01 +0800	[thread overview]
Message-ID: <avxVcSRzTpDkZmFN@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.

It was forgotten, but did not affect existing user space
csd, because it aborted on any error of this kind.
But now we work on better handling of errors, both
recoverable and not-recoverable and found this missing element.

Affects: #VSTOR-148028

Signed-off-by: Alexey Kuznetsov <kuznet@virtuozzo.com>
---
 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 13:25 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17 13:24 Alexey Kuznetsov [this message]
2026-11-17 13:25 ` [Devel] [PATCH hci-8.1 2/2] fs/ext4: ensure ext4 transaction credits while swapext Alexey Kuznetsov
2026-11-17 14:00   ` Konstantin Khorenko
2026-11-17 14:00 ` [Devel] [PATCH hci-8.1 1/2] fs/ext4: rectify behaviour of swapext ioctl on error Konstantin Khorenko

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=avxVcSRzTpDkZmFN@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox