Linux EXT4 FS development
 help / color / mirror / Atom feed
* [PATCH 0/3] ext4: fix data loss in inline data conversion of regular files
@ 2026-11-26 20:12 Petr Vaněk
  2026-11-26 20:12 ` [PATCH 1/3] ext4: drop KUnit checks tied to the inline conversion's metadata buffer Petr Vaněk
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Petr Vaněk @ 2026-11-26 20:12 UTC (permalink / raw)
  To: Theodore Ts'o, Andreas Dilger, Baokun Li, Jan Kara,
	Ojaswin Mujoo, Ritesh Harjani (IBM), Zhang Yi
  Cc: Deepanshu Kartikey, Petr Vaněk, linux-ext4

Since commit ed9356a30e59 ("ext4: convert inline data to extents when
truncate exceeds inline size"), growing an inline file beyond its
inline capacity converts its data to a block.  The conversion copies
the data into the block device's buffer and journals
it as metadata, while subsequent file writes reach the same block through
the page cache.  A journal checkpoint can then overwrite those writes
with the stale copy, causing data loss.  Patch 2 describes the failure
and includes a reproducer.

Patch 2 fixes this by converting regular files through the page cache,
as buffered writes already do.  It changes only fs/ext4/inline.c, keeping
the fix separate from the KUnit changes for stable backports.

Patches 1 and 3 update the KUnit test for ext4_convert_inline_data().
Patch 1 removes checks of the metadata buffer that the fix no longer
uses, allowing the test to pass both before and after the fix.  Patch 3
adds checks for conversion through the page cache.

Testing:

  - The reproducer in patch 2 loses data on 7.4-rc4 and passes with the
    fix, in QEMU on x86_64 with KASAN and lockdep enabled.
  - A QEMU test program covers truncate, fallocate, mmap and
    full-filesystem cases on inline_data filesystems with several mount
    options.  All cases pass with the series, and e2fsck reports no
    errors.  The truncate cases pass on the parent of ed9356a30e59 and
    fail on that commit.
  - The ext4 KUnit suite passes after each patch on top of the ext4 dev
    branch.

I have not run xfstests or tested other architectures.

For stable, patch 2 applies to 6.18 and later.  Kernels with
fs/ext4/inline-test.c need patch 1.  Older stable kernels carrying
ed9356a30e59 (6.12, 6.6, 6.1, 5.15, 5.10) need adapted backports.

AI assistance: Claude Opus via OpenCode analysed the bug and wrote the
fix, KUnit test changes, changelogs and QEMU tests.  I reviewed the code
and tests, ran the tests myself and checked the results by hand.

Petr Vaněk (3):
  ext4: drop KUnit checks tied to the inline conversion's metadata
    buffer
  ext4: convert regular file inline data through the page cache
  ext4: test inline data conversion through the page cache

 fs/ext4/inline-test.c |  92 ++++++++++++++++++++++++--------
 fs/ext4/inline.c      | 118 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 187 insertions(+), 23 deletions(-)

-- 
2.56.1


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 1/3] ext4: drop KUnit checks tied to the inline conversion's metadata buffer
  2026-11-26 20:12 [PATCH 0/3] ext4: fix data loss in inline data conversion of regular files Petr Vaněk
@ 2026-11-26 20:12 ` Petr Vaněk
  2026-11-26 20:12 ` [PATCH 2/3] ext4: convert regular file inline data through the page cache Petr Vaněk
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 6+ messages in thread
From: Petr Vaněk @ 2026-11-26 20:12 UTC (permalink / raw)
  To: Theodore Ts'o, Andreas Dilger, Baokun Li, Jan Kara,
	Ojaswin Mujoo, Ritesh Harjani (IBM), Zhang Yi
  Cc: Deepanshu Kartikey, Petr Vaněk, linux-ext4

test_convert_inline_data_and_rollback() checks that converting a
regular file's inline data writes it into the block device's buffer for
the new block, and that failures to get or dirty that buffer restore the
inline data.  Those are details of how ext4_convert_inline_data_nolock()
converts the data, not of what ext4_convert_inline_data() promises, and
the next patch moves regular files to the page cache, where there is no
such buffer.

Drop the content checks on that buffer and the cases for an unmapped
block, ext4_inline_getblk() failing and ext4_handle_dirty_metadata()
failing.  Keep the checks that hold for either implementation: the
conversion clears the inline data and sets the extents flag, and an
ext4_map_blocks() failure restores the inline data.

Signed-off-by: Petr Vaněk <petr.vanek@e2b.dev>
---
 fs/ext4/inline-test.c | 38 ++------------------------------------
 1 file changed, 2 insertions(+), 36 deletions(-)

diff --git a/fs/ext4/inline-test.c b/fs/ext4/inline-test.c
index b3d7c51ad216..3b697d4c425c 100644
--- a/fs/ext4/inline-test.c
+++ b/fs/ext4/inline-test.c
@@ -1149,7 +1149,6 @@ static void test_convert_inline_data_and_rollback(struct kunit *test)
 	ret = ext4_convert_inline_data(inode);
 	KUNIT_EXPECT_EQ(test, ret, 0);
 	KUNIT_EXPECT_FALSE(test, ext4_has_inline_data(inode));
-	KUNIT_EXPECT_MEMEQ(test, ctx->data_block_buf, buf, sizeof(buf));
 
 	/*
 	 * 3. Error rollback when ext4_map_blocks fails (-ENOSPC):
@@ -1171,46 +1170,13 @@ static void test_convert_inline_data_and_rollback(struct kunit *test)
 	ctx->map_blocks_ret = 1;
 
 	/*
-	 * 4. Error rollback when ext4_map_blocks returns unmapped block -> -EIO
+	 * 4. Successful conversion via ext4_convert_inline_data():
+	 * destroys inline data, initializes extent tree.
 	 */
-	ctx->map_flags = 0;
-	ret = ext4_convert_inline_data(inode);
-	KUNIT_EXPECT_EQ(test, ret, -EIO);
-	KUNIT_EXPECT_TRUE(test, ext4_has_inline_data(inode));
-	ctx->map_flags = EXT4_MAP_MAPPED;
-
-	/*
-	 * 5. Error rollback when ext4_inline_getblk returns NULL -> -ENOMEM
-	 */
-	ctx->getblk_fail = true;
-	ret = ext4_convert_inline_data(inode);
-	KUNIT_EXPECT_EQ(test, ret, -ENOMEM);
-	KUNIT_EXPECT_TRUE(test, ext4_has_inline_data(inode));
-	ctx->getblk_fail = false;
-
-	/*
-	 * 6. Error rollback when ext4_handle_dirty_metadata fails on data_bh
-	 */
-	ctx->dirty_metadata_err = -EIO;
-	ctx->dirty_metadata_fail_on_call = 1;
-	ctx->dirty_metadata_calls = 0;
-	ret = ext4_convert_inline_data(inode);
-	KUNIT_EXPECT_EQ(test, ret, -EIO);
-	KUNIT_EXPECT_TRUE(test, ext4_has_inline_data(inode));
-	ctx->dirty_metadata_err = 0;
-	ctx->dirty_metadata_fail_on_call = 0;
-
-	/*
-	 * 7. Successful conversion via ext4_convert_inline_data():
-	 * copies 80 bytes into data_bh, destroys inline data, initializes
-	 * extent tree.
-	 */
-	memset(ctx->data_block_buf, 0, sizeof(ctx->data_block_buf));
 	ret = ext4_convert_inline_data(inode);
 	KUNIT_EXPECT_EQ(test, ret, 0);
 	KUNIT_EXPECT_FALSE(test, ext4_has_inline_data(inode));
 	KUNIT_EXPECT_TRUE(test, ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS));
-	KUNIT_EXPECT_MEMEQ(test, ctx->data_block_buf, buf, sizeof(buf));
 }
 
 static void test_write_triggers_inline_to_extent_conversion(struct kunit *test)
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 2/3] ext4: convert regular file inline data through the page cache
  2026-11-26 20:12 [PATCH 0/3] ext4: fix data loss in inline data conversion of regular files Petr Vaněk
  2026-11-26 20:12 ` [PATCH 1/3] ext4: drop KUnit checks tied to the inline conversion's metadata buffer Petr Vaněk
@ 2026-11-26 20:12 ` Petr Vaněk
  2026-11-26 20:43   ` sashiko-bot
  2026-11-26 20:12 ` [PATCH 3/3] ext4: test inline data conversion " Petr Vaněk
  2026-11-26 20:43 ` [PATCH 0/3] ext4: fix data loss in inline data conversion of regular files sashiko-bot
  3 siblings, 1 reply; 6+ messages in thread
From: Petr Vaněk @ 2026-11-26 20:12 UTC (permalink / raw)
  To: Theodore Ts'o, Andreas Dilger, Baokun Li, Jan Kara,
	Ojaswin Mujoo, Ritesh Harjani (IBM), Zhang Yi
  Cc: Deepanshu Kartikey, Petr Vaněk, stable, linux-ext4

ext4_convert_inline_data() moves a regular file's inline data to a new
block by copying it into the block device's buffer for that block and
journalling the buffer as metadata.  The file's own writes reach the
same block through the page cache, and nothing invalidates the
journalled copy.  When the journal checkpoints it, the stale buffer is
written over whatever the file has put in the block since.  The page
cache can also miss the data altogether: a folio read in after the
conversion reads the block from disk before the checkpoint has written
it.

Truncate, fallocate, page_mkwrite and fs-verity all convert inline
files this way, and since commit ed9356a30e59 ("ext4: convert inline
data to extents when truncate exceeds inline size") so does every
truncate that grows an inline file.  An ordinary sequence now loses
data:

  mkfs.ext4 -O inline_data /dev/vda
  mount /dev/vda /mnt
  xfs_io -f -c "pwrite -S 0x61 0 42" /mnt/f
  sync; echo 3 > /proc/sys/vm/drop_caches
  xfs_io -c "truncate 0" -c "truncate 77" -c "pwrite -S 0x62 0 77" /mnt/f
  sync; echo 3 > /proc/sys/vm/drop_caches
  od -c /mnt/f    # 77 zero bytes instead of 77 'b'

Growing the file without shrinking it first leaves the old inline bytes
on disk but loses whatever was written after the truncate, and
fallocate or an mmap write on a small inline file loses the new data
the same way.

Convert regular files the way a buffered write does in
ext4_convert_inline_data_to_extent(): copy the inline data into
folio 0, drop the inline data, map block 0 with ext4_get_block() and
dirty the folio.  The data then reaches the disk by writeback, and only
metadata goes through the journal.  The new helper differs from the
buffered-write one in three ways:

 - It always maps a written extent, never an unwritten one, so that in
   data=ordered mode the commit that drops the inline copy waits for
   the data.
 - An empty file needs no block; only the inline data is dropped.
 - If mapping the block fails, it restores the inline data instead of
   truncating the file: truncation takes invalidate_lock, which
   ext4_page_mkwrite() already holds shared.

Directories keep the old path, since their blocks are metadata.

Fixes: ed9356a30e59 ("ext4: convert inline data to extents when truncate exceeds inline size")
Cc: stable@vger.kernel.org
Signed-off-by: Petr Vaněk <petr.vanek@e2b.dev>
---
 fs/ext4/inline.c | 118 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 118 insertions(+)

diff --git a/fs/ext4/inline.c b/fs/ext4/inline.c
index acdc65ce4796..537857c3b4b2 100644
--- a/fs/ext4/inline.c
+++ b/fs/ext4/inline.c
@@ -1999,6 +1999,121 @@ int ext4_inline_data_truncate(struct inode *inode, int *has_inline)
 	return err;
 }
 
+/*
+ * Move a regular file's inline data to block 0 through the page cache, as a
+ * buffered write does, so the data reaches the disk by writeback.  It must not
+ * be journalled as a metadata block: checkpointing that copy later would
+ * overwrite whatever the file has written to the block since.
+ */
+static int ext4_convert_inline_data_reg(struct inode *inode)
+{
+	struct address_space *mapping = inode->i_mapping;
+	int ret, ret2, needed_blocks, no_expand, retries = 0;
+	unsigned int inline_size, len;
+	struct buffer_head *bh;
+	struct ext4_iloc iloc;
+	struct folio *folio;
+	handle_t *handle;
+	void *buf;
+
+	needed_blocks = ext4_chunk_trans_extent(inode, 1);
+
+	ret = ext4_get_inode_loc(inode, &iloc);
+	if (ret)
+		return ret;
+
+retry:
+	buf = NULL;
+	handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
+	if (IS_ERR(handle)) {
+		ret = PTR_ERR(handle);
+		goto out_brelse;
+	}
+
+	folio = __filemap_get_folio(mapping, 0, FGP_WRITEBEGIN | FGP_NOFS,
+				    mapping_gfp_mask(mapping));
+	if (IS_ERR(folio)) {
+		ret = PTR_ERR(folio);
+		goto out_stop;
+	}
+
+	ext4_write_lock_xattr(inode, &no_expand);
+	ret = 0;
+	if (!ext4_has_inline_data(inode))
+		goto out_unlock;
+
+	inline_size = ext4_get_inline_size(inode);
+	len = min_t(loff_t, inline_size, i_size_read(inode));
+	if (len > i_blocksize(inode)) {
+		EXT4_ERROR_INODE(inode, "inline data size %u exceeds block size",
+				 len);
+		ret = -EFSCORRUPTED;
+		goto out_unlock;
+	}
+
+	ext4_fc_track_inode(handle, inode);
+	if (!len) {
+		ret = ext4_destroy_inline_data_nolock(handle, inode);
+		goto out_unlock;
+	}
+
+	buf = kmalloc(inline_size, GFP_NOFS);
+	if (!buf) {
+		ret = -ENOMEM;
+		goto out_unlock;
+	}
+	ret = ext4_read_inline_data(inode, buf, inline_size, &iloc);
+	if (ret < 0)
+		goto out_unlock;
+
+	if (!folio_test_uptodate(folio)) {
+		ret = ext4_read_inline_folio(inode, folio);
+		if (ret < 0)
+			goto out_unlock;
+	}
+
+	ret = ext4_destroy_inline_data_nolock(handle, inode);
+	if (ret)
+		goto out_unlock;
+
+	/*
+	 * A written extent, not an unwritten one: in data=ordered mode the
+	 * commit that drops the inline copy then waits for the data.
+	 */
+	ret = ext4_block_write_begin(handle, folio, 0, len, ext4_get_block);
+	if (ret) {
+		ext4_restore_inline_data(handle, inode, &iloc, buf, inline_size);
+		goto out_unlock;
+	}
+
+	bh = folio_buffers(folio);
+	clear_buffer_new(bh);
+	if (ext4_should_journal_data(inode)) {
+		ret = do_journal_get_write_access(handle, inode, bh);
+		if (!ret) {
+			folio_mark_dirty(folio);
+			ret = ext4_handle_dirty_metadata(handle, NULL, bh);
+		}
+	} else {
+		block_commit_write(folio, 0, len);
+	}
+
+out_unlock:
+	ext4_write_unlock_xattr(inode, &no_expand);
+	folio_unlock(folio);
+	folio_put(folio);
+out_stop:
+	ret2 = ext4_journal_stop(handle);
+	if (!ret)
+		ret = ret2;
+	kfree(buf);
+	if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
+		goto retry;
+out_brelse:
+	brelse(iloc.bh);
+	return ret;
+}
+
 int ext4_convert_inline_data(struct inode *inode)
 {
 	int error, needed_blocks, no_expand;
@@ -2022,6 +2137,9 @@ int ext4_convert_inline_data(struct inode *inode)
 			return 0;
 	}
 
+	if (S_ISREG(inode->i_mode))
+		return ext4_convert_inline_data_reg(inode);
+
 	needed_blocks = ext4_chunk_trans_extent(inode, 1);
 
 	iloc.bh = NULL;
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 3/3] ext4: test inline data conversion through the page cache
  2026-11-26 20:12 [PATCH 0/3] ext4: fix data loss in inline data conversion of regular files Petr Vaněk
  2026-11-26 20:12 ` [PATCH 1/3] ext4: drop KUnit checks tied to the inline conversion's metadata buffer Petr Vaněk
  2026-11-26 20:12 ` [PATCH 2/3] ext4: convert regular file inline data through the page cache Petr Vaněk
@ 2026-11-26 20:12 ` Petr Vaněk
  2026-11-26 20:43 ` [PATCH 0/3] ext4: fix data loss in inline data conversion of regular files sashiko-bot
  3 siblings, 0 replies; 6+ messages in thread
From: Petr Vaněk @ 2026-11-26 20:12 UTC (permalink / raw)
  To: Theodore Ts'o, Andreas Dilger, Baokun Li, Jan Kara,
	Ojaswin Mujoo, Ritesh Harjani (IBM), Zhang Yi
  Cc: Deepanshu Kartikey, Petr Vaněk, linux-ext4

ext4_convert_inline_data() now moves a regular file's inline data into
folio 0 and leaves it to writeback.  Check that: after the conversion,
folio 0 must be uptodate and dirty, hold the old inline data, and have
its buffer mapped to the new block, without any metadata buffer being
dirtied.  Drop the page cache before converting, so that folio 0 has to
be read from the inline data.

Add cases for an ext4_map_blocks() failure with -EIO restoring the
inline data, for data=journal mode, where the folio's buffer is
journalled through ext4_handle_dirty_metadata(), and for an empty
inline file, which must be converted without mapping a block.

Signed-off-by: Petr Vaněk <petr.vanek@e2b.dev>
---
 fs/ext4/inline-test.c | 84 +++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 82 insertions(+), 2 deletions(-)

diff --git a/fs/ext4/inline-test.c b/fs/ext4/inline-test.c
index 3b697d4c425c..399c8cfcf659 100644
--- a/fs/ext4/inline-test.c
+++ b/fs/ext4/inline-test.c
@@ -1122,6 +1122,35 @@ static void test_inline_data_truncate(struct kunit *test)
  * =========================================================================
  */
 
+/*
+ * Check that a regular file's inline data was moved into folio 0, dirty and
+ * mapped to the new block, so that writeback writes it out.
+ */
+static void expect_converted_folio(struct kunit *test, struct inode *inode,
+				   const void *buf, size_t len)
+{
+	struct buffer_head *bh;
+	struct folio *folio;
+	void *kaddr;
+
+	folio = filemap_lock_folio(inode->i_mapping, 0);
+	KUNIT_ASSERT_FALSE(test, IS_ERR(folio));
+	KUNIT_EXPECT_TRUE(test, folio_test_uptodate(folio));
+	KUNIT_EXPECT_TRUE(test, folio_test_dirty(folio));
+	bh = folio_buffers(folio);
+	KUNIT_EXPECT_NOT_NULL(test, bh);
+	if (bh) {
+		KUNIT_EXPECT_TRUE(test, buffer_mapped(bh));
+		KUNIT_EXPECT_TRUE(test, buffer_dirty(bh));
+		KUNIT_EXPECT_EQ(test, bh->b_blocknr, (sector_t)TEST_PBLK);
+	}
+	kaddr = kmap_local_folio(folio, 0);
+	KUNIT_EXPECT_MEMEQ(test, kaddr, buf, len);
+	kunmap_local(kaddr);
+	folio_unlock(folio);
+	folio_put(folio);
+}
+
 static void test_convert_inline_data_and_rollback(struct kunit *test)
 {
 	struct inline_test_ctx *ctx = test->priv;
@@ -1142,13 +1171,20 @@ static void test_convert_inline_data_and_rollback(struct kunit *test)
 	/*
 	 * 2. Inline inode without EXT4_STATE_MAY_INLINE_DATA -> calls
 	 * filemap_flush() and finishes converting inline data if still inline.
+	 * Drop the page cache first so that folio 0 must be read from the
+	 * inline data.
 	 */
 	ret = create_inline_file(inode, buf, sizeof(buf));
 	KUNIT_ASSERT_EQ(test, ret, 0);
+	truncate_inode_pages(&inode->i_data, 0);
 	ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
+	ctx->dirty_metadata_calls = 0;
 	ret = ext4_convert_inline_data(inode);
 	KUNIT_EXPECT_EQ(test, ret, 0);
 	KUNIT_EXPECT_FALSE(test, ext4_has_inline_data(inode));
+	expect_converted_folio(test, inode, buf, sizeof(buf));
+	/* The data goes through the page cache, not a metadata buffer. */
+	KUNIT_EXPECT_EQ(test, ctx->dirty_metadata_calls, 0);
 
 	/*
 	 * 3. Error rollback when ext4_map_blocks fails (-ENOSPC):
@@ -1169,14 +1205,58 @@ static void test_convert_inline_data_and_rollback(struct kunit *test)
 			   sizeof(buf) - EXT4_MIN_INLINE_DATA_SIZE);
 	ctx->map_blocks_ret = 1;
 
+	/* 4. Error rollback when ext4_map_blocks fails with -EIO */
+	ctx->map_blocks_ret = -EIO;
+	ret = ext4_convert_inline_data(inode);
+	KUNIT_EXPECT_EQ(test, ret, -EIO);
+	KUNIT_EXPECT_TRUE(test, ext4_has_inline_data(inode));
+	KUNIT_EXPECT_MEMEQ(test, raw_inline_i_block(ctx), buf,
+			   EXT4_MIN_INLINE_DATA_SIZE);
+	ctx->map_blocks_ret = 1;
+
+	/*
+	 * 5. Successful conversion via ext4_convert_inline_data():
+	 * copies 80 bytes into folio 0, destroys inline data, initializes
+	 * extent tree.
+	 */
+	ret = ext4_convert_inline_data(inode);
+	KUNIT_EXPECT_EQ(test, ret, 0);
+	KUNIT_EXPECT_FALSE(test, ext4_has_inline_data(inode));
+	KUNIT_EXPECT_TRUE(test, ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS));
+	expect_converted_folio(test, inode, buf, sizeof(buf));
+
 	/*
-	 * 4. Successful conversion via ext4_convert_inline_data():
-	 * destroys inline data, initializes extent tree.
+	 * 6. data=journal: the folio's buffer is journalled as data and
+	 * dirtied through ext4_handle_dirty_metadata().
 	 */
+	reset_inode_state(ctx, S_IFREG | 0644);
+	ret = create_inline_file(inode, buf, sizeof(buf));
+	KUNIT_ASSERT_EQ(test, ret, 0);
+	truncate_inode_pages(&inode->i_data, 0);
+	ctx->journal_mode = EXT4_INODE_JOURNAL_DATA_MODE;
+	ctx->dirty_metadata_calls = 0;
+	ret = ext4_convert_inline_data(inode);
+	KUNIT_EXPECT_EQ(test, ret, 0);
+	KUNIT_EXPECT_FALSE(test, ext4_has_inline_data(inode));
+	KUNIT_EXPECT_GT(test, ctx->dirty_metadata_calls, 0);
+	expect_converted_folio(test, inode, buf, sizeof(buf));
+	ctx->journal_mode = EXT4_INODE_WRITEBACK_DATA_MODE;
+
+	/*
+	 * 7. Empty inline file: the inline data is dropped and no block is
+	 * mapped, so a failing ext4_map_blocks() must not be reached.
+	 */
+	reset_inode_state(ctx, S_IFREG | 0644);
+	ret = create_inline_file(inode, buf, sizeof(buf));
+	KUNIT_ASSERT_EQ(test, ret, 0);
+	i_size_write(inode, 0);
+	ctx->ei.i_disksize = 0;
+	ctx->map_blocks_ret = -EIO;
 	ret = ext4_convert_inline_data(inode);
 	KUNIT_EXPECT_EQ(test, ret, 0);
 	KUNIT_EXPECT_FALSE(test, ext4_has_inline_data(inode));
 	KUNIT_EXPECT_TRUE(test, ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS));
+	ctx->map_blocks_ret = 1;
 }
 
 static void test_write_triggers_inline_to_extent_conversion(struct kunit *test)
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH 2/3] ext4: convert regular file inline data through the page cache
  2026-11-26 20:12 ` [PATCH 2/3] ext4: convert regular file inline data through the page cache Petr Vaněk
@ 2026-11-26 20:43   ` sashiko-bot
  0 siblings, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-11-26 20:43 UTC (permalink / raw)
  To: Petr Vaněk; +Cc: linux-ext4, tytso

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] ext4: NULL jinode dereference in ext4_convert_inline_data_reg()
- [High] ext4: stale inode checksum after rollback in
  ext4_convert_inline_data_reg()
- [Medium] ext4: unhandled -EAGAIN and credit shortfall in
  ext4_convert_inline_data_reg()
- [Medium] ext4: race with delalloc conversion in
  ext4_convert_inline_data_reg()
--

commit cdcc63e61166aa17d9c625ad732f9377e4d25d2b
Author: Petr Vaněk <petr.vanek@e2b.dev>

ext4: convert regular file inline data through the page cache

This commit converts a regular file's inline data to an extent block through
folio 0 in the page cache rather than journalling the data block as metadata.

> diff --git a/fs/ext4/inline.c b/fs/ext4/inline.c
> index acdc65ce47960..537857c3b4b29 100644
> --- a/fs/ext4/inline.c
> +++ b/fs/ext4/inline.c
> @@ -1999,6 +1999,121 @@ int ext4_inline_data_truncate(struct inode *inode, int *has_inline)
>  	return err;
>  }
>  
> +/*
> + * Move a regular file's inline data to block 0 through the page cache, as a
> + * buffered write does, so the data reaches the disk by writeback.  It must not
> + * be journalled as a metadata block: checkpointing that copy later would
> + * overwrite whatever the file has written to the block since.
> + */
> +static int ext4_convert_inline_data_reg(struct inode *inode)
> +{
> +	struct address_space *mapping = inode->i_mapping;
> +	int ret, ret2, needed_blocks, no_expand, retries = 0;
> +	unsigned int inline_size, len;
> +	struct buffer_head *bh;
> +	struct ext4_iloc iloc;
> +	struct folio *folio;
> +	handle_t *handle;
> +	void *buf;
> +
> +	needed_blocks = ext4_chunk_trans_extent(inode, 1);
> +
> +	ret = ext4_get_inode_loc(inode, &iloc);
> +	if (ret)
> +		return ret;

[Severity: High]
Can this trigger a NULL pointer dereference on EXT4_I(inode)->jinode when
mapping block 0 in data=ordered mode?

EXT4_I(inode)->jinode is attached lazily in ext4_file_open() or explicitly by
callers that do not require an open writable file descriptor.  When path-based
truncate(2) grows a non-empty inline file whose in-core inode has not been
opened for writing, ext4_setattr() calls ext4_convert_inline_data() before
ext4_truncate_up() attaches jinode.  ext4_block_write_begin() then calls
ext4_get_block() -> ext4_map_blocks() -> ext4_jbd2_inode_add_write(), which
passes a NULL jinode to jbd2_journal_file_inode().

Calling ext4_inode_attach_jinode(inode) in ext4_convert_inline_data_reg()
before starting the conversion avoids this.

> +
> +retry:
> +	buf = NULL;
> +	handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
> +	if (IS_ERR(handle)) {
> +		ret = PTR_ERR(handle);
> +		goto out_brelse;
> +	}
> +
> +	folio = __filemap_get_folio(mapping, 0, FGP_WRITEBEGIN | FGP_NOFS,
> +				    mapping_gfp_mask(mapping));
> +	if (IS_ERR(folio)) {
> +		ret = PTR_ERR(folio);
> +		goto out_stop;
> +	}
> +
> +	ext4_write_lock_xattr(inode, &no_expand);
> +	ret = 0;
> +	if (!ext4_has_inline_data(inode))
> +		goto out_unlock;

[Severity: Medium]
Can a concurrent delayed-allocation write convert the file between the
EXT4_STATE_MAY_INLINE_DATA check in ext4_convert_inline_data() and acquiring
the folio lock and xattr_sem here?

CPU0 (ext4_page_mkwrite)          CPU1 (buffered write)
ext4_convert_inline_data()
  test MAY_INLINE_DATA (set)
                                  ext4_da_write_inline_data_begin()
                                    ...
                                    ext4_da_convert_inline_data_to_extent()
                                      folio_lock(folio 0)
                                      down_read(xattr_sem)
                                      ext4_block_write_begin(...,
                                        ext4_da_get_block_prep)
                                        -> sets BH_Mapped | BH_Delay
                                      clear MAY_INLINE_DATA
                                      up_read(xattr_sem)
                                      folio_unlock(folio 0)
  ext4_convert_inline_data_reg()
    folio_lock(folio 0)
    ext4_write_lock_xattr()
    ext4_has_inline_data() (still true)
    ext4_destroy_inline_data_nolock()
    ext4_block_write_begin(...,
      ext4_get_block)
      !buffer_mapped(bh) is false <-- skips ext4_get_block()

Because ext4_da_get_block_prep() sets BH_Mapped along with BH_Delay while
leaving EXT4_INODE_INLINE_DATA set until writeback, ext4_block_write_begin()
sees buffer_mapped(bh) == true and skips ext4_get_block().  The inline data is
destroyed on disk without allocating a block or filing the inode into the
transaction's ordered-data list, and if an error occurs earlier and rolls back
via ext4_restore_inline_data(), re-setting EXT4_STATE_MAY_INLINE_DATA on the
dirty inode hits BUG_ON(ext4_test_inode_state(inode,
EXT4_STATE_MAY_INLINE_DATA)) in ext4_do_writepages().

> +
> +	inline_size = ext4_get_inline_size(inode);
> +	len = min_t(loff_t, inline_size, i_size_read(inode));
> +	if (len > i_blocksize(inode)) {
> +		EXT4_ERROR_INODE(inode, "inline data size %u exceeds block size",
> +				 len);
> +		ret = -EFSCORRUPTED;
> +		goto out_unlock;
> +	}
> +
> +	ext4_fc_track_inode(handle, inode);
> +	if (!len) {
> +		ret = ext4_destroy_inline_data_nolock(handle, inode);
> +		goto out_unlock;
> +	}
> +
> +	buf = kmalloc(inline_size, GFP_NOFS);
> +	if (!buf) {
> +		ret = -ENOMEM;
> +		goto out_unlock;
> +	}
> +	ret = ext4_read_inline_data(inode, buf, inline_size, &iloc);
> +	if (ret < 0)
> +		goto out_unlock;
> +
> +	if (!folio_test_uptodate(folio)) {
> +		ret = ext4_read_inline_folio(inode, folio);
> +		if (ret < 0)
> +			goto out_unlock;
> +	}
> +
> +	ret = ext4_destroy_inline_data_nolock(handle, inode);
> +	if (ret)
> +		goto out_unlock;
> +
> +	/*
> +	 * A written extent, not an unwritten one: in data=ordered mode the
> +	 * commit that drops the inline copy then waits for the data.
> +	 */
> +	ret = ext4_block_write_begin(handle, folio, 0, len, ext4_get_block);
> +	if (ret) {
> +		ext4_restore_inline_data(handle, inode, &iloc, buf, inline_size);
> +		goto out_unlock;
> +	}

[Severity: High]
Does rolling back via ext4_restore_inline_data() and jumping to out_unlock
leave the on-disk raw inode with an invalid metadata checksum?

Inside ext4_restore_inline_data(), ext4_create_inline_data() zeroes
raw_inode->i_block and the in-inode system.data xattr and calls
ext4_mark_iloc_dirty(), which computes the raw inode checksum over the zeroed
bytes.  ext4_write_inline_data() then copies buf back into raw_inode->i_block
and the xattr value in iloc.bh without recomputing the checksum, and neither
ext4_restore_inline_data() nor the out_unlock path calls
ext4_mark_iloc_dirty() or ext4_mark_inode_dirty().  When block allocation
fails (for example with -ENOSPC or -EDQUOT) on a file with non-zero inline
data, the committed inode fails ext4_inode_csum_verify() with -EFSBADCRC once
re-read from disk.

> +
> +	bh = folio_buffers(folio);
> +	clear_buffer_new(bh);
> +	if (ext4_should_journal_data(inode)) {
> +		ret = do_journal_get_write_access(handle, inode, bh);
> +		if (!ret) {
> +			folio_mark_dirty(folio);
> +			ret = ext4_handle_dirty_metadata(handle, NULL, bh);
> +		}
> +	} else {
> +		block_commit_write(folio, 0, len);
> +	}
> +
> +out_unlock:
> +	ext4_write_unlock_xattr(inode, &no_expand);
> +	folio_unlock(folio);
> +	folio_put(folio);
> +out_stop:
> +	ret2 = ext4_journal_stop(handle);
> +	if (!ret)
> +		ret = ret2;
> +	kfree(buf);
> +	if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
> +		goto retry;

[Severity: Medium]
Can ext4_block_write_begin() return -EAGAIN here because needed_blocks is
computed before EXT4_INODE_EXTENTS is set?

When ext4_chunk_trans_extent(inode, 1) runs at the top of
ext4_convert_inline_data_reg(), EXT4_INODE_INLINE_DATA is still set and
EXT4_INODE_EXTENTS is clear, so ext4_index_trans_blocks() only reserves 5
index blocks (and ext4_destroy_inline_data_nolock() then consumes a credit on
iloc.bh and sets EXT4_INODE_EXTENTS).  When ext4_block_write_begin() calls
ext4_journal_ensure_extent_credits(), ext4_chunk_trans_blocks(inode, 1) now
requires 11 index blocks for an extent inode, forcing an in-place
ext4_journal_extend() on every non-empty conversion.

If the running transaction cannot be extended in place (for example when it is
locked for commit), ext4_journal_ensure_extent_credits() returns -EAGAIN, and
because only -ENOSPC is retried here, -EAGAIN is returned to callers such as
ext4_page_mkwrite(), where vmf_fs_error(-EAGAIN) turns it into
VM_FAULT_SIGBUS.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261126201258.1553196-1-petr.vanek@e2b.dev?part=2

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 0/3] ext4: fix data loss in inline data conversion of regular files
  2026-11-26 20:12 [PATCH 0/3] ext4: fix data loss in inline data conversion of regular files Petr Vaněk
                   ` (2 preceding siblings ...)
  2026-11-26 20:12 ` [PATCH 3/3] ext4: test inline data conversion " Petr Vaněk
@ 2026-11-26 20:43 ` sashiko-bot
  3 siblings, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-11-26 20:43 UTC (permalink / raw)
  To: Petr Vaněk; +Cc: linux-ext4, tytso

Sashiko has finished reviewing this 3-patch series:

- Patches 1 and 3: no issues found.
- Patch 2 ("ext4: convert regular file inline data through the p..."): 4
  potential issues (2 High, 2 Medium)

A detailed review has been sent as a separate reply to patch 2.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261126201258.1553196-1-petr.vanek@e2b.dev

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-11-26 20:43 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26 20:12 [PATCH 0/3] ext4: fix data loss in inline data conversion of regular files Petr Vaněk
2026-11-26 20:12 ` [PATCH 1/3] ext4: drop KUnit checks tied to the inline conversion's metadata buffer Petr Vaněk
2026-11-26 20:12 ` [PATCH 2/3] ext4: convert regular file inline data through the page cache Petr Vaněk
2026-11-26 20:43   ` sashiko-bot
2026-11-26 20:12 ` [PATCH 3/3] ext4: test inline data conversion " Petr Vaněk
2026-11-26 20:43 ` [PATCH 0/3] ext4: fix data loss in inline data conversion of regular files sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox