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 15/23] ext4: cap migrated extents at EXT_INIT_MAX_LEN
Date: Wed, 11 Nov 2026 23:47:04 -0500 [thread overview]
Message-ID: <20261112044712.1577296-16-tytso@mit.edu> (raw)
In-Reply-To: <20261112044712.1577296-1-tytso@mit.edu>
When converting an indirect-mapped inode to extents, ext4_ext_migrate()
coalesces physically and logically contiguous blocks in
update_extent_range() without any upper bound, and finish_range() then
stores the raw run length into the 16-bit ee_len field.
An initialized extent can be at most EXT_INIT_MAX_LEN (32768) blocks
long; larger ee_len values encode unwritten extents. So if an indirect
file contains a contiguous run of more than 32768 blocks (128 MiB with
4 KiB blocks), the migrated extent is silently turned into an unwritten
extent of length (len - 32768): the file data reads back as zeroes, the
tail of the run becomes a hole, and 32768 blocks are leaked once the
file is truncated or unlinked. A run of exactly 65536 blocks wraps
ee_len to zero, producing a corrupt extent.
Stop extending the current range once it reaches EXT_INIT_MAX_LEN, so
that long runs are split into several initialized extents.
Fixes: c14c6fd5c56a ("ext4: Add EXT4_IOC_MIGRATE ioctl")
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
---
fs/ext4/migrate-test.c | 91 ++++++++++++++++++++++++++++++++++++++++++
fs/ext4/migrate.c | 6 ++-
2 files changed, 95 insertions(+), 2 deletions(-)
diff --git a/fs/ext4/migrate-test.c b/fs/ext4/migrate-test.c
index e83d208a5117..7a7d6607b67f 100644
--- a/fs/ext4/migrate-test.c
+++ b/fs/ext4/migrate-test.c
@@ -1041,6 +1041,96 @@ static void test_ind_migrate_rejections(struct kunit *test)
iput(inode);
}
+/*
+ * Test 8: ext4_ext_migrate() splits a physically and logically contiguous
+ * run longer than EXT_INIT_MAX_LEN into multiple initialized extents rather
+ * than encoding the raw length into ee_len, which would turn the extent
+ * into an unwritten one (or wrap ee_len) and lose the file data.
+ */
+static void test_ext_migrate_caps_long_run_at_init_max_len(struct kunit *test)
+{
+ struct migrate_test_ctx *ctx = test->priv;
+ const unsigned int per_blk = MIGRATE_TEST_BLK_SIZE / sizeof(__le32);
+ const unsigned int nr_ind = 128;
+ const ext4_fsblk_t data_pblk = 2000;
+ const ext4_fsblk_t ind_pblk = 600, dind_pblk = 700;
+ const ext4_lblk_t len = EXT4_NDIR_BLOCKS + per_blk + nr_ind * per_blk;
+ const int max_len = EXT_INIT_MAX_LEN;
+ struct ext4_extent_header *eh;
+ struct ext4_extent *ex;
+ struct buffer_head *bh;
+ struct inode *inode;
+ ext4_fsblk_t pblk = data_pblk;
+ unsigned int i, j;
+
+ /* 12 + 256 + 128 * 256 = 33036 blocks > EXT_INIT_MAX_LEN */
+ KUNIT_ASSERT_GT(test, len, (ext4_lblk_t)max_len);
+ ext4_blocks_count_set(ctx->es, data_pblk + len + 1024);
+
+ inode = migrate_make_test_inode(test, 22, S_IFREG | 0644);
+ i_size_write(inode, (loff_t)len * MIGRATE_TEST_BLK_SIZE);
+
+ for (i = 0; i < EXT4_NDIR_BLOCKS; i++)
+ EXT4_I(inode)->i_data[i] = cpu_to_le32(pblk++);
+
+ EXT4_I(inode)->i_data[EXT4_IND_BLOCK] = cpu_to_le32(ind_pblk);
+ bh = migrate_get_or_create_bh(ctx, ind_pblk);
+ KUNIT_ASSERT_NOT_NULL(test, bh);
+ for (j = 0; j < per_blk; j++)
+ ((__le32 *)bh->b_data)[j] = cpu_to_le32(pblk++);
+ brelse(bh);
+
+ EXT4_I(inode)->i_data[EXT4_DIND_BLOCK] = cpu_to_le32(dind_pblk);
+ bh = migrate_get_or_create_bh(ctx, dind_pblk);
+ KUNIT_ASSERT_NOT_NULL(test, bh);
+ for (i = 0; i < nr_ind; i++)
+ ((__le32 *)bh->b_data)[i] = cpu_to_le32(dind_pblk + 1 + i);
+ brelse(bh);
+
+ for (i = 0; i < nr_ind; i++) {
+ bh = migrate_get_or_create_bh(ctx, dind_pblk + 1 + i);
+ KUNIT_ASSERT_NOT_NULL(test, bh);
+ for (j = 0; j < per_blk; j++)
+ ((__le32 *)bh->b_data)[j] = cpu_to_le32(pblk++);
+ brelse(bh);
+ }
+ inode->i_blocks = (blkcnt_t)(len + 2 + nr_ind) *
+ (MIGRATE_TEST_BLK_SIZE >> 9);
+
+ KUNIT_EXPECT_EQ(test, ext4_ext_migrate(inode), 0);
+ KUNIT_EXPECT_TRUE(test,
+ ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS));
+ eh = ext_inode_hdr(inode);
+ if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS) ||
+ !eh->eh_entries)
+ goto out;
+
+ KUNIT_EXPECT_EQ(test, le16_to_cpu(eh->eh_depth), 0U);
+ ex = EXT_FIRST_EXTENT(eh);
+
+ /* Extent 0: lblk [0..32767] -> data_pblk, initialized */
+ KUNIT_EXPECT_EQ(test, le32_to_cpu(ex[0].ee_block), 0U);
+ KUNIT_EXPECT_FALSE(test, ext4_ext_is_unwritten(&ex[0]));
+ KUNIT_EXPECT_EQ(test, ext4_ext_get_actual_len(&ex[0]), max_len);
+ KUNIT_EXPECT_EQ(test, ext4_ext_pblock(&ex[0]), data_pblk);
+
+ /* Extent 1: remainder of the run, initialized */
+ KUNIT_EXPECT_EQ(test, le16_to_cpu(eh->eh_entries), 2U);
+ if (le16_to_cpu(eh->eh_entries) < 2)
+ goto out;
+ KUNIT_EXPECT_EQ(test, le32_to_cpu(ex[1].ee_block), (u32)max_len);
+ KUNIT_EXPECT_FALSE(test, ext4_ext_is_unwritten(&ex[1]));
+ KUNIT_EXPECT_EQ(test, ext4_ext_get_actual_len(&ex[1]),
+ (int)len - max_len);
+ KUNIT_EXPECT_EQ(test, ext4_ext_pblock(&ex[1]), data_pblk + max_len);
+
+ /* Indirect metadata blocks were released */
+ KUNIT_EXPECT_TRUE(test, migrate_was_freed(ctx, ind_pblk));
+ KUNIT_EXPECT_TRUE(test, migrate_was_freed(ctx, dind_pblk + 1));
+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),
@@ -1049,6 +1139,7 @@ static struct kunit_case ext4_migrate_test_cases[] = {
KUNIT_CASE(test_ext_swap_inode_data_detects_concurrent_allocation),
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),
{}
};
diff --git a/fs/ext4/migrate.c b/fs/ext4/migrate.c
index 8d76042f0cae..e4c1603e1402 100644
--- a/fs/ext4/migrate.c
+++ b/fs/ext4/migrate.c
@@ -67,11 +67,13 @@ static int update_extent_range(handle_t *handle, struct inode *inode,
{
int retval;
/*
- * See if we can add on to the existing range (if it exists)
+ * See if we can add on to the existing range (if it exists).
+ * An initialized extent cannot be longer than EXT_INIT_MAX_LEN.
*/
if (lb->first_pblock &&
(lb->last_pblock+1 == pblock) &&
- (lb->last_block+1 == lb->curr_block)) {
+ (lb->last_block+1 == lb->curr_block) &&
+ (lb->last_block - lb->first_block + 1 < EXT_INIT_MAX_LEN)) {
lb->last_pblock = pblock;
lb->last_block = lb->curr_block;
lb->curr_block++;
--
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 ` Theodore Ts'o [this message]
2026-11-12 4:47 ` [PATCH 16/23] ext4: don't treat a handle restart as an error in finish_range() Theodore Ts'o
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-16-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