* [PATCH 1/2] ext4: don't zero uptodate buffers in ext4_readpage_inline()
2026-12-03 10:06 [PATCH 0/2] ext4: fix data loss when reading during inline data conversion Florian Schmaus
@ 2026-12-03 10:06 ` Florian Schmaus
2026-12-03 10:06 ` [PATCH 2/2] ext4: test that ext4_readpage_inline() keeps uptodate buffers Florian Schmaus
2026-12-03 10:30 ` [PATCH 0/2] ext4: fix data loss when reading during inline data conversion sashiko-bot
2 siblings, 0 replies; 5+ messages in thread
From: Florian Schmaus @ 2026-12-03 10:06 UTC (permalink / raw)
To: Theodore Ts'o, Andreas Dilger, Baokun Li, Jan Kara,
Ojaswin Mujoo, Ritesh Harjani (IBM), Zhang Yi, Tao Ma
Cc: linux-ext4, Florian Schmaus, Ulrich Müller, stable
When a file with inline data outgrows the inode, ext4 with delayed
allocation moves the data out of the inode in two steps, and the second
step only happens at writeback. If the block size is smaller than the
page size, such as 1 KiB blocks on x86, reading the file between the two
steps can replace freshly written data with zeroes, which writeback then
puts on disk.
The first step, ext4_da_convert_inline_data_to_extent(), copies the
inline data into the first folio and clears EXT4_STATE_MAY_INLINE_DATA.
The inline data itself and EXT4_INODE_INLINE_DATA stay in place until
the second step, when writeback calls ext4_destroy_inline_data(). Until
then, ext4_read_folio() hands every folio to ext4_readpage_inline(),
which assumes that only the first folio can hold data and zero-fills any
other folio that is not uptodate.
If the block size is smaller than the folio size, buffered writes that
fill a folio only partially leave the written buffers uptodate, but not
the folio. The last folio of a file written with small appending writes
is a typical example. Reading such a folio through ->read_folio() before
writeback replaces the written data with zeroes and marks the folio
uptodate. The folio is still dirty, so writeback then puts the zeroes on
disk as well.
Whether a read hits this depends on how it accesses the folio. read(2)
copies a range that lies within uptodate buffers directly, see
->is_partially_uptodate(), but calls ->read_folio() if the requested
range reaches into a buffer that is not uptodate, even one past EOF.
Page faults and splice, e.g. sendfile(2), always call ->read_folio() for
a folio that is not uptodate. With 1 KiB blocks and inline_data, a file
created with a few small writes and read back in one go loses its tail.
This prints 904 instead of 0:
# mkfs.ext4 -b 1024 -O inline_data /dev/vdX && mount /dev/vdX /mnt
# python3 -c '
import os
fd = os.open("/mnt/f", os.O_WRONLY | os.O_CREAT | os.O_TRUNC)
for i in range(50):
os.write(fd, b"x" * 100)
os.close(fd)
fd = os.open("/mnt/f", os.O_RDONLY)
print(os.read(fd, 65536).count(0))'
904
Gentoo's portage hit this once Python 3.14 started to read stdin with a
128 KiB buffer (Gentoo bug #987733): bash had written the ebuild
environment with many small writes, Python read it back with a tail of
NUL bytes, causing bash to refuse to source the result.
Fix this by zeroing only the buffers that are not uptodate and marking
them uptodate, leaving the others alone. As long as the inode has inline
data, no data blocks are allocated to the file, so a buffer that is not
uptodate is a hole. Folios without buffers are zeroed as before.
Fixes: 9c3569b50f12 ("ext4: add delalloc support for inline data")
Reported-by: Ulrich Müller <ulm@gentoo.org>
Closes: https://lore.kernel.org/ucxrjwzcv@gentoo.org
Closes: https://bugs.gentoo.org/987733
Cc: stable@vger.kernel.org
Signed-off-by: Florian Schmaus <flo@geekplace.eu>
---
fs/ext4/inline.c | 40 +++++++++++++++++++++++++++++++++++++---
1 file changed, 37 insertions(+), 3 deletions(-)
diff --git a/fs/ext4/inline.c b/fs/ext4/inline.c
index acdc65ce4796..d5d42b288c64 100644
--- a/fs/ext4/inline.c
+++ b/fs/ext4/inline.c
@@ -558,6 +558,39 @@ static int ext4_read_inline_folio(struct inode *inode, struct folio *folio)
return ret;
}
+/*
+ * Zero the parts of a folio beyond the inline data that are not uptodate.
+ *
+ * ext4_da_convert_inline_data_to_extent() leaves the inline data in place
+ * until writeback, so buffered writes may already have put data into this
+ * folio. With a block size smaller than the folio size, a folio that was
+ * only partially written is not uptodate, but the buffers that were written
+ * are, and they hold the only copy of that data, so keep them. All other
+ * blocks of the folio are holes: as long as the inode has inline data, no
+ * blocks are allocated for it, so they read as zeroes.
+ */
+static void ext4_inline_zero_folio(struct folio *folio)
+{
+ struct buffer_head *head = folio_buffers(folio);
+ struct buffer_head *bh;
+ size_t offset = 0;
+
+ if (!head) {
+ folio_zero_segment(folio, 0, folio_size(folio));
+ return;
+ }
+
+ bh = head;
+ do {
+ if (!buffer_uptodate(bh)) {
+ folio_zero_range(folio, offset, bh->b_size);
+ set_buffer_uptodate(bh);
+ }
+ offset += bh->b_size;
+ bh = bh->b_this_page;
+ } while (bh != head);
+}
+
int ext4_readpage_inline(struct inode *inode, struct folio *folio)
{
int ret = 0;
@@ -569,13 +602,14 @@ int ext4_readpage_inline(struct inode *inode, struct folio *folio)
}
/*
- * Current inline data can only exist in the 1st page,
- * So for all the other pages, just set them uptodate.
+ * Current inline data can only exist in the 1st page. The other
+ * pages hold either nothing or data written after a delalloc
+ * conversion that has not been written back yet.
*/
if (!folio->index)
ret = ext4_read_inline_folio(inode, folio);
else if (!folio_test_uptodate(folio)) {
- folio_zero_segment(folio, 0, folio_size(folio));
+ ext4_inline_zero_folio(folio);
folio_mark_uptodate(folio);
}
--
2.56.1
^ permalink raw reply related [flat|nested] 5+ messages in thread* [PATCH 2/2] ext4: test that ext4_readpage_inline() keeps uptodate buffers
2026-12-03 10:06 [PATCH 0/2] ext4: fix data loss when reading during inline data conversion Florian Schmaus
2026-12-03 10:06 ` [PATCH 1/2] ext4: don't zero uptodate buffers in ext4_readpage_inline() Florian Schmaus
@ 2026-12-03 10:06 ` Florian Schmaus
2026-12-03 10:17 ` sashiko-bot
2026-12-03 10:30 ` [PATCH 0/2] ext4: fix data loss when reading during inline data conversion sashiko-bot
2 siblings, 1 reply; 5+ messages in thread
From: Florian Schmaus @ 2026-12-03 10:06 UTC (permalink / raw)
To: Theodore Ts'o, Andreas Dilger, Baokun Li, Jan Kara,
Ojaswin Mujoo, Ritesh Harjani (IBM), Zhang Yi, Tao Ma
Cc: linux-ext4, Florian Schmaus, Ulrich Müller
After a delalloc conversion of inline data, buffered writes can leave a
folio past the first one partially uptodate until writeback. Add a case
to the inline data KUnit tests that checks that ext4_readpage_inline()
keeps the uptodate buffer of such a folio, zeroes the other buffers and
marks all of them uptodate.
Signed-off-by: Florian Schmaus <flo@geekplace.eu>
---
fs/ext4/inline-test.c | 36 ++++++++++++++++++++++++++++++++++++
1 file changed, 36 insertions(+)
diff --git a/fs/ext4/inline-test.c b/fs/ext4/inline-test.c
index b3d7c51ad216..1558454d9d8b 100644
--- a/fs/ext4/inline-test.c
+++ b/fs/ext4/inline-test.c
@@ -895,6 +895,7 @@ static void test_readpage_inline_link_and_iomap(struct kunit *test)
{
struct inline_test_ctx *ctx = test->priv;
struct inode *inode = &ctx->ei.vfs_inode;
+ struct buffer_head *head, *bh;
struct folio *folio;
struct iomap iomap = { 0 };
char content[85];
@@ -960,6 +961,41 @@ static void test_readpage_inline_link_and_iomap(struct kunit *test)
kunmap_local(kaddr);
folio_put(folio);
+ /*
+ * 3b. ext4_readpage_inline on folio->index > 0 with only some buffers
+ * uptodate: keeps their data, zeroes the other buffers and marks all
+ * of them uptodate. Buffered writes after a delalloc conversion
+ * leave such a folio behind until writeback destroys the inline data.
+ */
+ folio = __filemap_get_folio(&inode->i_data, 2,
+ FGP_LOCK | FGP_ACCESSED | FGP_CREAT,
+ GFP_KERNEL);
+ KUNIT_ASSERT_FALSE(test, IS_ERR(folio));
+ kaddr = kmap_local_folio(folio, 0);
+ memset(kaddr, 0xff, PAGE_SIZE);
+ memset(kaddr + TEST_BLOCK_SIZE, 0xab, TEST_BLOCK_SIZE);
+ kunmap_local(kaddr);
+ head = create_empty_buffers(folio, TEST_BLOCK_SIZE, 0);
+ set_buffer_uptodate(head->b_this_page);
+
+ ret = ext4_readpage_inline(inode, folio);
+ KUNIT_EXPECT_EQ(test, ret, 0);
+ KUNIT_EXPECT_TRUE(test, folio_test_uptodate(folio));
+ KUNIT_EXPECT_FALSE(test, folio_test_locked(folio));
+ kaddr = kmap_local_folio(folio, 0);
+ KUNIT_EXPECT_NULL(test, memchr_inv(kaddr, 0, TEST_BLOCK_SIZE));
+ KUNIT_EXPECT_NULL(test, memchr_inv(kaddr + TEST_BLOCK_SIZE, 0xab,
+ TEST_BLOCK_SIZE));
+ KUNIT_EXPECT_NULL(test, memchr_inv(kaddr + 2 * TEST_BLOCK_SIZE, 0,
+ PAGE_SIZE - 2 * TEST_BLOCK_SIZE));
+ kunmap_local(kaddr);
+ bh = head;
+ do {
+ KUNIT_EXPECT_TRUE(test, buffer_uptodate(bh));
+ bh = bh->b_this_page;
+ } while (bh != head);
+ folio_put(folio);
+
/* 4. ext4_readpage_inline when ext4_get_inode_loc fails -> propagates error */
truncate_inode_pages(&inode->i_data, 0);
ctx->get_inode_loc_err = -EIO;
--
2.56.1
^ permalink raw reply related [flat|nested] 5+ messages in thread