Linux kernel -stable discussions
 help / color / mirror / Atom feed
* [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
  0 siblings, 0 replies; only message 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] only message in thread

only message in thread, other threads:[~2026-11-26 20:13 UTC | newest]

Thread overview: (only message) (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

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