From: "Theodore Ts'o" <tytso@mit.edu>
To: Ext4 Developers List <linux-ext4@vger.kernel.org>
Cc: "Theodore Ts'o" <tytso@mit.edu>
Subject: [PATCH 16/23] ext4: don't treat a handle restart as an error in finish_range()
Date: Wed, 11 Nov 2026 23:47:05 -0500 [thread overview]
Message-ID: <20261112044712.1577296-17-tytso@mit.edu> (raw)
In-Reply-To: <20261112044712.1577296-1-tytso@mit.edu>
ext4_datasem_ensure_credits() returns 1 when it had to restart the
transaction to get the requested credits. finish_range() only bails
out on a negative value, but it then leaves the 1 in retval, and since
ext4_ext_insert_extent() was converted to return the path, a successful
insertion no longer overwrites retval. finish_range() therefore
returns 1, which all its callers treat as a failure.
As a result, whenever the handle has to be restarted while building
the extent tree of the temporary inode (which is likely for fragmented
files that need many extent insertions), the migration is aborted and
the extent blocks built so far are thrown away. Because the error
code is later clobbered in ext4_ext_migrate(), userspace is even told
that the migration succeeded while the inode stays indirect-mapped.
A restart is not an error: reset retval to 0 after a successful
ext4_datasem_ensure_credits(). The path does not need to be looked up
again since tmp_inode is private to the migration and cannot change
while i_data_sem is briefly dropped.
Add a KUnit static stub hook to ext4_datasem_ensure_credits() so that
a transaction restart can be emulated without a journal.
Fixes: f7d1331f16a8 ("ext4: get rid of ppath in ext4_ext_insert_extent()")
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
---
fs/ext4/extents.c | 4 +++
fs/ext4/migrate-test.c | 66 ++++++++++++++++++++++++++++++++++++++++++
fs/ext4/migrate.c | 5 ++++
3 files changed, 75 insertions(+)
diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c
index cbada2846190..7872ddc21544 100644
--- a/fs/ext4/extents.c
+++ b/fs/ext4/extents.c
@@ -142,12 +142,16 @@ int ext4_datasem_ensure_credits(handle_t *handle, struct inode *inode,
int ret;
int dropped = 0;
+ KUNIT_STATIC_STUB_REDIRECT(ext4_datasem_ensure_credits, handle, inode,
+ check_cred, restart_cred, revoke_cred);
+
ret = ext4_journal_ensure_credits_fn(handle, check_cred, restart_cred,
revoke_cred, ext4_ext_trunc_restart_fn(inode, &dropped));
if (dropped)
down_write(&EXT4_I(inode)->i_data_sem);
return ret;
}
+EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_datasem_ensure_credits);
/*
* could return:
diff --git a/fs/ext4/migrate-test.c b/fs/ext4/migrate-test.c
index 7a7d6607b67f..1cc7c6fe3b80 100644
--- a/fs/ext4/migrate-test.c
+++ b/fs/ext4/migrate-test.c
@@ -67,6 +67,7 @@ struct migrate_test_ctx {
int new_inode_err;
ext4_fsblk_t freed_blks[MIGRATE_MAX_FREED_REC];
unsigned int freed_cnt;
+ unsigned int datasem_restarts;
bool rwsem_inited;
bool es_shrinker_inited;
bool counters_inited;
@@ -365,6 +366,22 @@ static bool migrate_was_freed(struct migrate_test_ctx *ctx, ext4_fsblk_t blk)
return false;
}
+/*
+ * Emulate ext4_datasem_ensure_credits() having to restart the transaction:
+ * i_data_sem is dropped and re-acquired and 1 is returned.
+ */
+static int migrate_datasem_restart_stub(handle_t *handle, struct inode *inode,
+ int check_cred, int restart_cred,
+ int revoke_cred)
+{
+ struct migrate_test_ctx *ctx = active_migrate_test->priv;
+
+ up_write(&EXT4_I(inode)->i_data_sem);
+ down_write(&EXT4_I(inode)->i_data_sem);
+ ctx->datasem_restarts++;
+ return 1;
+}
+
static struct inode *migrate_make_test_inode(struct kunit *test, unsigned long ino,
umode_t mode)
{
@@ -1131,6 +1148,54 @@ static void test_ext_migrate_caps_long_run_at_init_max_len(struct kunit *test)
iput(inode);
}
+/*
+ * Test 9: A transaction restart while inserting an extent into tmp_inode
+ * (ext4_datasem_ensure_credits() returning 1) is not an error; migration
+ * still completes and builds every extent.
+ */
+static void test_ext_migrate_survives_handle_restart(struct kunit *test)
+{
+ struct migrate_test_ctx *ctx = test->priv;
+ struct ext4_extent_header *eh;
+ struct ext4_extent *ex;
+ struct inode *inode;
+
+ kunit_activate_static_stub(test, ext4_datasem_ensure_credits,
+ migrate_datasem_restart_stub);
+
+ inode = migrate_make_test_inode(test, 23, S_IFREG | 0644);
+ i_size_write(inode, 12 * MIGRATE_TEST_BLK_SIZE);
+ inode->i_blocks = 4 * (MIGRATE_TEST_BLK_SIZE >> 9);
+
+ /* Three discontiguous runs: [0..1] -> 100, [4] -> 200, [8] -> 300 */
+ EXT4_I(inode)->i_data[0] = cpu_to_le32(100);
+ EXT4_I(inode)->i_data[1] = cpu_to_le32(101);
+ EXT4_I(inode)->i_data[4] = cpu_to_le32(200);
+ EXT4_I(inode)->i_data[8] = cpu_to_le32(300);
+
+ KUNIT_EXPECT_EQ(test, ext4_ext_migrate(inode), 0);
+ KUNIT_EXPECT_EQ(test, ctx->datasem_restarts, 3U);
+ KUNIT_EXPECT_TRUE(test,
+ ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS));
+ if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
+ goto out;
+
+ eh = ext_inode_hdr(inode);
+ KUNIT_EXPECT_EQ(test, le16_to_cpu(eh->eh_entries), 3U);
+ if (le16_to_cpu(eh->eh_entries) != 3)
+ goto out;
+ ex = EXT_FIRST_EXTENT(eh);
+ KUNIT_EXPECT_EQ(test, le32_to_cpu(ex[0].ee_block), 0U);
+ KUNIT_EXPECT_EQ(test, ext4_ext_get_actual_len(&ex[0]), 2);
+ KUNIT_EXPECT_EQ(test, ext4_ext_pblock(&ex[0]), 100ULL);
+ KUNIT_EXPECT_EQ(test, le32_to_cpu(ex[1].ee_block), 4U);
+ KUNIT_EXPECT_EQ(test, ext4_ext_pblock(&ex[1]), 200ULL);
+ KUNIT_EXPECT_EQ(test, le32_to_cpu(ex[2].ee_block), 8U);
+ KUNIT_EXPECT_EQ(test, ext4_ext_pblock(&ex[2]), 300ULL);
+out:
+ iput(inode);
+}
+
static struct kunit_case ext4_migrate_test_cases[] = {
KUNIT_CASE(test_ext_migrate_precondition_checks_and_fast_symlink),
KUNIT_CASE(test_ext_migrate_direct_and_ind_coalescing_and_holes),
@@ -1140,6 +1205,7 @@ static struct kunit_case ext4_migrate_test_cases[] = {
KUNIT_CASE(test_ind_migrate_empty_and_single_direct_extent),
KUNIT_CASE(test_ind_migrate_rejections),
KUNIT_CASE(test_ext_migrate_caps_long_run_at_init_max_len),
+ KUNIT_CASE(test_ext_migrate_survives_handle_restart),
{}
};
diff --git a/fs/ext4/migrate.c b/fs/ext4/migrate.c
index e4c1603e1402..dd132a6b6060 100644
--- a/fs/ext4/migrate.c
+++ b/fs/ext4/migrate.c
@@ -52,6 +52,11 @@ static int finish_range(handle_t *handle, struct inode *inode,
retval = ext4_datasem_ensure_credits(handle, inode, needed, needed, 0);
if (retval < 0)
goto err_out;
+ /*
+ * A positive return only means the handle was restarted. The
+ * path is still valid because nobody else can modify tmp_inode.
+ */
+ retval = 0;
path = ext4_ext_insert_extent(handle, inode, path, &newext, 0);
if (IS_ERR(path))
retval = PTR_ERR(path);
--
2.56.0.542.g3ef89b7020-goog
next prev parent reply other threads:[~2026-11-12 4:47 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-12 4:46 [PATCH 00/23] ext4: lots of misc bug fixes Theodore Ts'o
2026-11-12 4:46 ` [PATCH 01/23] ext4: only update s_mount_opts when EXT4_TUNE_FL_MOUNT_OPTS is set Theodore Ts'o
2026-11-12 4:46 ` [PATCH 02/23] ext4: return -EFSBADCRC when skipping a corrupt backup superblock Theodore Ts'o
2026-11-12 4:46 ` [PATCH 03/23] ext4: don't allow enabling casefold via EXT4_IOC_SET_TUNE_SB_PARAM Theodore Ts'o
2026-11-12 4:46 ` [PATCH 04/23] ext4: persist the htree hash seed when enabling dir_index online Theodore Ts'o
2026-11-12 4:46 ` [PATCH 05/23] ext4: enforce feature prerequisites when tuning a mounted fs Theodore Ts'o
2026-11-12 4:46 ` [PATCH 06/23] ext4: handle s_lastcheck_hi in the get/set tune superblock ioctls Theodore Ts'o
2026-11-12 4:46 ` [PATCH 07/23] ext4: pass missing ioctls through ext4_compat_ioctl Theodore Ts'o
2026-11-12 4:46 ` [PATCH 08/23] ext4: fix inverted err_report_sec check in ext4_update_super() Theodore Ts'o
2026-11-12 4:46 ` [PATCH 09/23] ext4: fix err_report_sec_store() timer and value handling Theodore Ts'o
2026-11-12 4:46 ` [PATCH 10/23] ext4: fix inode table accounting in count_overhead() Theodore Ts'o
2026-11-12 4:47 ` [PATCH 11/23] ext4: handle flex groups straddling s_first_meta_bg in online resize Theodore Ts'o
2026-11-12 4:47 ` [PATCH 12/23] ext4: reject EXT4_IOC_GROUP_ADD inputs larger than a block group Theodore Ts'o
2026-11-12 4:47 ` [PATCH 13/23] ext4: propagate ext4_alloc_group_tables() errors from ext4_resize_fs() Theodore Ts'o
2026-11-12 4:47 ` [PATCH 14/23] ext4: reject online shrink before probing the device size Theodore Ts'o
2026-11-12 4:47 ` [PATCH 15/23] ext4: cap migrated extents at EXT_INIT_MAX_LEN Theodore Ts'o
2026-11-12 4:47 ` Theodore Ts'o [this message]
2026-11-12 4:47 ` [PATCH 17/23] ext4: don't clobber the migration result in ext4_ext_migrate() Theodore Ts'o
2026-11-12 4:47 ` [PATCH 18/23] ext4: fix EOF clamping of the range in mext_check_adjust_range() Theodore Ts'o
2026-11-12 4:47 ` [PATCH 19/23] ext4: bound the MMP update and check intervals Theodore Ts'o
2026-11-12 4:47 ` [PATCH 20/23] ext4: fix MMP claim of a cleanly unmounted filesystem Theodore Ts'o
2026-11-12 4:47 ` [PATCH 21/23] ext4: fix device comparison overflow in ext4_getfsmap_dev_compare() Theodore Ts'o
2026-11-12 4:47 ` [PATCH 22/23] ext4: don't allocate over indirect branches that failed to load Theodore Ts'o
2026-11-12 4:47 ` [PATCH 23/23] ext4: don't stop EXT4_IOC_GET_ES_CACHE at uncached ranges Theodore Ts'o
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=20261112044712.1577296-17-tytso@mit.edu \
--to=tytso@mit.edu \
--cc=linux-ext4@vger.kernel.org \
/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