Linux EXT4 FS development
 help / color / mirror / Atom feed
From: "Petr Vaněk" <petr.vanek@e2b.dev>
To: "Theodore Ts'o" <tytso@mit.edu>,
	Andreas Dilger <adilger.kernel@dilger.ca>,
	Baokun Li <libaokun@linux.alibaba.com>, Jan Kara <jack@suse.cz>,
	Ojaswin Mujoo <ojaswin@linux.ibm.com>,
	"Ritesh Harjani (IBM)" <ritesh.list@gmail.com>,
	Zhang Yi <yi.zhang@huawei.com>
Cc: "Deepanshu Kartikey" <kartikey406@gmail.com>,
	"Petr Vaněk" <petr.vanek@e2b.dev>,
	linux-ext4@vger.kernel.org
Subject: [PATCH 3/3] ext4: test inline data conversion through the page cache
Date: Thu, 26 Nov 2026 21:12:55 +0100	[thread overview]
Message-ID: <20261126201258.1553196-4-petr.vanek@e2b.dev> (raw)
In-Reply-To: <20261126201258.1553196-1-petr.vanek@e2b.dev>

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


  parent reply	other threads:[~2026-11-26 20:13 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 ` Petr Vaněk [this message]
2026-11-26 20:43 ` [PATCH 0/3] ext4: fix data loss in inline data conversion of regular files sashiko-bot

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=20261126201258.1553196-4-petr.vanek@e2b.dev \
    --to=petr.vanek@e2b.dev \
    --cc=adilger.kernel@dilger.ca \
    --cc=jack@suse.cz \
    --cc=kartikey406@gmail.com \
    --cc=libaokun@linux.alibaba.com \
    --cc=linux-ext4@vger.kernel.org \
    --cc=ojaswin@linux.ibm.com \
    --cc=ritesh.list@gmail.com \
    --cc=tytso@mit.edu \
    --cc=yi.zhang@huawei.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox