* [PATCH 2/3] ext4: convert regular file inline data through the page cache
[not found] <20261126201258.1553196-1-petr.vanek@e2b.dev>
@ 2026-11-26 20:12 ` Petr Vaněk
2026-11-27 15:07 ` Jan Kara
0 siblings, 1 reply; 3+ 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] 3+ 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-27 15:07 ` Jan Kara
2026-12-05 10:21 ` Petr Vaněk
0 siblings, 1 reply; 3+ messages in thread
From: Jan Kara @ 2026-11-27 15:07 UTC (permalink / raw)
To: Petr Vaněk
Cc: Theodore Ts'o, Andreas Dilger, Baokun Li, Jan Kara,
Ojaswin Mujoo, Ritesh Harjani (IBM), Zhang Yi, Deepanshu Kartikey,
stable, linux-ext4
On Thu 26-11-26 21:12:54, Petr Vaněk wrote:
> 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.
Nasty, thanks for analyzing this. How did you find the problem? I.e., is it
some syscall fuzzing, AI analysis, or real users hitting this?
> 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.
I have two comments regarding the solution:
1) We converted data using journalled data write to avoid loss of already
written data in case of crash before newly allocated block gets written
out. I personally never liked this much and agree this is asking for big
trouble (as your report shows) so I'm all for getting rid of this case. But
still we should explain the implications in the changelog - if we actually
don't use delayed allocation (which is what you did) and use data=ordered
mode, the data loss after a crash should not happen so I think we are fine.
2) Why did you implement new conversion function and didn't just use
ext4_convert_inline_data_to_extent()? I don't really want *another* inline
data conversion method so if there's some reason why
ext4_convert_inline_data_to_extent() cannot be used, let's discuss how to
make possible to use it...
Honza
>
> 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
>
--
Jan Kara <jack@suse.com>
SUSE Labs, CR
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH 2/3] ext4: convert regular file inline data through the page cache
2026-11-27 15:07 ` Jan Kara
@ 2026-12-05 10:21 ` Petr Vaněk
0 siblings, 0 replies; 3+ messages in thread
From: Petr Vaněk @ 2026-12-05 10:21 UTC (permalink / raw)
To: Jan Kara
Cc: Petr Vaněk, Theodore Ts'o, Andreas Dilger, Baokun Li,
Ojaswin Mujoo, Ritesh Harjani (IBM), Zhang Yi, Deepanshu Kartikey,
stable, linux-ext4
On Fri, Nov 27, 2026 at 04:07:03PM +0100, Jan Kara wrote:
> Nasty, thanks for analyzing this. How did you find the problem? I.e., is it
> some syscall fuzzing, AI analysis, or real users hitting this?
Real users were hitting this.
> I have two comments regarding the solution:
>
> 1) We converted data using journalled data write to avoid loss of already
> written data in case of crash before newly allocated block gets written
> out. I personally never liked this much and agree this is asking for big
> trouble (as your report shows) so I'm all for getting rid of this case. But
> still we should explain the implications in the changelog - if we actually
> don't use delayed allocation (which is what you did) and use data=ordered
> mode, the data loss after a crash should not happen so I think we are fine.
Agreed, I'll explain it in the next version.
> 2) Why did you implement new conversion function and didn't just use
> ext4_convert_inline_data_to_extent()? I don't really want *another* inline
> data conversion method so if there's some reason why
> ext4_convert_inline_data_to_extent() cannot be used, let's discuss how to
> make possible to use it...
The problem was its error path. If mapping block 0 fails, it truncates
the file with ext4_truncate_failed_write(), which takes invalidate_lock
while page_mkwrite already holds it. Changing that would have touched
the buffered-write path, so I wrote a separate function that restores
the inline data instead, as ext4_convert_inline_data_nolock() does.
Sashiko has since found [1] that truncating also loses data in the write
path: the inline data has already been dropped, leaving the bytes only
in the clean folio 0. So the helper needs that change anyway: keep a
kmalloc'ed copy of the inline data before dropping it, and restore it
with ext4_restore_inline_data() if mapping fails. The restore path has
a stale checksum problem, which my earlier patch fixes [2].
With that change, regular files can use the helper too, with no need
for a new conversion function.
[1] https://lore.kernel.org/linux-ext4/sashiko-outbox-240817@kernel.org/
[2] https://lore.kernel.org/20261125143841.928615-1-petr.vanek@e2b.dev
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-12-05 10:21 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <20261126201258.1553196-1-petr.vanek@e2b.dev>
2026-11-26 20:12 ` [PATCH 2/3] ext4: convert regular file inline data through the page cache Petr Vaněk
2026-11-27 15:07 ` Jan Kara
2026-12-05 10:21 ` Petr Vaněk
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox