Linux EXT4 FS development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Petr Vaněk" <petr.vanek@e2b.dev>
Cc: linux-ext4@vger.kernel.org, tytso@mit.edu
Subject: Re: [PATCH 2/3] ext4: convert regular file inline data through the page cache
Date: Thu, 26 Nov 2026 20:43:25 +0000	[thread overview]
Message-ID: <sashiko-outbox-236164@kernel.org> (raw)
In-Reply-To: <20261126201258.1553196-3-petr.vanek@e2b.dev>

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

  reply	other threads:[~2026-11-26 20:43 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 [this message]
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

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=sashiko-outbox-236164@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-ext4@vger.kernel.org \
    --cc=petr.vanek@e2b.dev \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=tytso@mit.edu \
    /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