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 v2 2/5] fs/ext4: ensure ext4 transaction credits while swapext
Date: Wed, 18 Nov 2026 00:59:08 +0800	[thread overview]
Message-ID: <avyH3AcU14eRnzBZ@mops2> (raw)

ext4 (like any other journaling fs) has a problem
with estimating journal reservations required for particular
fs operation. swapext is pretty complicated operation, its
exact volume depends on set of extents currently present
in the inodes.

Credits for the whole request are estimated in advance from the
number of extents in the orig range, but a pass may also split
extents and grow the extent tree of both inodes, so a fragmented
range in an inode with a deep tree can need more than reserved.
When the handle runs out of credits in the middle of a pass,
jbd2_journal_dirty_metadata() fails with -ENOSPC, the handle is
aborted and ext4 reports "journal_dirty_metadata failed" error.
With errors=remount-ro (this option is required for css)
the journal is aborted and the filesystem becomes read-only,
with errors=continue the half-done pass can be committed partially,
which is metadata corruption.

This patch suggests a safe way to adjust credits in case
initial estimate was wrong without fatal impact on journal:
it checks credits before each pass, extends the handle if needed
(the logic is borrowed from ext4_block_write_begin() which
has similar problem), and otherwise restarts the transaction
and continues from the last completed pass.

Note, swapext does not require to swap the whole requested
area. It is allowed to swap any partial contiguous area,
provided moved_len reports swapped blocks and blocks
beyond moved_len are not affected.

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/extents.c     | 34 ++++++++++++++++++++++++++++++++++
 fs/ext4/move_extent.c |  6 ++++++
 2 files changed, 40 insertions(+)

diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c
index eca3f0312ac5..58d3acf6646e 100644
--- a/fs/ext4/extents.c
+++ b/fs/ext4/extents.c
@@ -5821,6 +5821,7 @@ ext4_swap_extents_2(handle_t *handle, struct inode *inode1,
 	struct ext4_ext_path *path1 = NULL;
 	struct ext4_ext_path *path2 = NULL;
 	int replaced_count = 0;
+	int passes = 0;
 
 	BUG_ON(!rwsem_is_locked(&EXT4_I(inode1)->i_data_sem));
 	BUG_ON(!rwsem_is_locked(&EXT4_I(inode2)->i_data_sem));
@@ -5834,6 +5835,27 @@ ext4_swap_extents_2(handle_t *handle, struct inode *inode1,
 		int e1_len, e2_len, len;
 		int split = 0;
 
+		/*
+		 * A pass either splits at most one extent in each inode or
+		 * swaps one pair of extents. Running out of credits in the
+		 * middle of a pass aborts the journal, so before each pass
+		 * (but the first one, which the caller has reserved for) make
+		 * sure the handle can afford the worst case. If it cannot,
+		 * return to the caller with -EAGAIN, it will restart the
+		 * transaction and continue from the current lblk1/lblk2
+		 * (the start passed in plus the returned replaced_count).
+		 */
+		if (passes++) {
+			int credits = ext4_chunk_trans_blocks(inode1, 1) +
+				      ext4_chunk_trans_blocks(inode2, 1);
+			int ret = __ext4_journal_ensure_credits(handle, credits,
+								credits, 0);
+			if (ret) {
+				*erp = ret < 0 ? ret : -EAGAIN;
+				break;
+			}
+		}
+
 		path1 = ext4_find_extent(inode1, lblk1, NULL, EXT4_EX_NOCACHE);
 		if (IS_ERR(path1)) {
 			*erp = PTR_ERR(path1);
@@ -5993,6 +6015,7 @@ ext4_expose_extents_2(handle_t *handle, struct inode *inode,
 {
 	struct ext4_ext_path *path = NULL;
 	int replaced_count = 0;
+	int passes = 0;
 
 	ext4_es_remove_extent(inode, lblk, count);
 
@@ -6002,6 +6025,17 @@ ext4_expose_extents_2(handle_t *handle, struct inode *inode,
 		int e_len, len;
 		int split = 0;
 
+		/* See comment in ext4_swap_extents_2() */
+		if (passes++) {
+			int credits = ext4_chunk_trans_blocks(inode, 1);
+			int ret = __ext4_journal_ensure_credits(handle, credits,
+								credits, 0);
+			if (ret) {
+				*erp = ret < 0 ? ret : -EAGAIN;
+				break;
+			}
+		}
+
 		path = ext4_find_extent(inode, lblk, NULL, EXT4_EX_NOCACHE);
 		if (IS_ERR(path)) {
 			*erp = PTR_ERR(path);
diff --git a/fs/ext4/move_extent.c b/fs/ext4/move_extent.c
index 0e8d1366da7a..dfbdb571105f 100644
--- a/fs/ext4/move_extent.c
+++ b/fs/ext4/move_extent.c
@@ -743,6 +743,9 @@ swap_one_extent(struct file *o_filp, struct inode *donor_inode,
 	se->s_dirty += handle->h_requested_credits - handle->h_total_credits;
 
 	ext4_journal_stop(handle);
+	/* Handle is out of credits, continue in a new transaction */
+	if (*err == -EAGAIN)
+		goto again;
 	if (*err == -ENOSPC &&
 	    ext4_should_retry_alloc(sb, &retries))
 		goto again;
@@ -782,6 +785,9 @@ expose_one_extent(struct inode *donor_inode,
 
 	se->s_dirty += handle->h_requested_credits - handle->h_total_credits;
 	ext4_journal_stop(handle);
+	/* Handle is out of credits, continue in a new transaction */
+	if (*err == -EAGAIN)
+		goto again;
 	if (*err == -ENOSPC &&
 	    ext4_should_retry_alloc(sb, &retries))
 		goto again;
-- 
2.47.1

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

                 reply	other threads:[~2026-11-17 17:00 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=avyH3AcU14eRnzBZ@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