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 2/2] fs/ext4: ensure ext4 transaction credits while swapext
Date: Tue, 17 Nov 2026 21:25:08 +0800 [thread overview]
Message-ID: <avxVtFVOz3wI2xF/@mops2> (raw)
ext4 (like any other journaling fs I guess) 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. Even though existing estimate works, there
is no warranty under some circumstances calculated credits
are not enough to complete the transaction.
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,
and otherwise restarts the transaction and continues
from the last completed pass.
The patch is suggested by Claude. It looks reasonable,
the technique is borrowed from ext4_block_write_begin().
Affects: #VSTOR-148028
Signed-off-by: Alexey Kuznetsov <kuznet@virtuozzo.com>
---
fs/ext4/extents.c | 33 +++++++++++++++++++++++++++++++++
fs/ext4/move_extent.c | 6 ++++++
2 files changed, 39 insertions(+)
diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c
index eca3f0312ac5..6ffee24cf386 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,26 @@ 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 lblk1 + 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 +6014,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 +6024,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
next prev reply other threads:[~2026-11-17 13:26 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-17 13:24 [Devel] [PATCH hci-8.1 1/2] fs/ext4: rectify behaviour of swapext ioctl on error Alexey Kuznetsov
2026-11-17 13:25 ` Alexey Kuznetsov [this message]
2026-11-17 14:00 ` [Devel] [PATCH hci-8.1 2/2] fs/ext4: ensure ext4 transaction credits while swapext 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=avxVtFVOz3wI2xF/@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.