[Devel] [PATCH hci-8.1 v2 4/5] fs/ext4: the swapext ioctl could fail with -ENOSPC and a jbd2 WARN

Alexey Kuznetsov kuznet at virtuozzo.com
Tue Nov 17 20:00:26 MSK 2026


Cap amount of credits allocated for transaction handle
to avoid failure on large fragmented ranges.

Note, this fix is possible due to previous patch
"fs/ext4: ensure ext4 transaction credits while swapext",
which allows to split swapext over multiple transactions.

Note, this is not necessary for expose_one_extent(),
credits required always fit to the limit.

Fixes: 1a3d440e7fbc ("fs/ext4: swapext ioctl")
Affects: #VSTOR-155600
https://virtuozzo.atlassian.net/browse/VSTOR-155600

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 | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/fs/ext4/move_extent.c b/fs/ext4/move_extent.c
index cd07efaa1f30..b25f2865df89 100644
--- a/fs/ext4/move_extent.c
+++ b/fs/ext4/move_extent.c
@@ -728,6 +728,22 @@ swap_one_extent(struct file *o_filp, struct inode *donor_inode,
 	 * fragmented area, up to len pages.
 	 */
 	jblocks = ext4_chunk_trans_blocks(donor_inode, len) + nexts;
+	/*
+	 * nexts grows with fragmentation of the orig range and can exceed
+	 * the maximal handle size, then the handle cannot be started at all.
+	 * Cap it: ext4_swap_extents_2() checks credits before every pass but
+	 * the first one and returns -EAGAIN to continue in a new transaction.
+	 */
+	if (EXT4_SB(sb)->s_journal) {
+		journal_t *journal = EXT4_SB(sb)->s_journal;
+		int limit = (journal->j_max_transaction_buffers -
+			     journal->j_transaction_overhead_buffers) / 2;
+
+		jblocks = min(jblocks, limit);
+	}
+	/* The first pass is not checked, it must always be covered */
+	jblocks = max(jblocks, ext4_chunk_trans_blocks(donor_inode, 1) +
+			       ext4_chunk_trans_blocks(orig_inode, 1));
 	handle = ext4_journal_start(orig_inode, EXT4_HT_MOVE_EXTENTS, jblocks);
 	if (IS_ERR(handle)) {
 		*err = PTR_ERR(handle);
-- 
2.47.1



More information about the Devel mailing list