From: sashiko-bot@kernel.org
To: "Yichong Chen" <chenyichong@uniontech.com>, linux-ext4@vger.kernel.org
Cc: tytso@mit.edu
Subject: Re: [PATCH] ext4: keep ordered data and i_disksize consistent on size changes
Date: Wed, 18 Nov 2026 08:43:35 +0000 [thread overview]
Message-ID: <sashiko-outbox-220480@kernel.org> (raw)
In-Reply-To: <D7EEAE1F0CDD3029+20261118075044.3511841-1-chenyichong@uniontech.com>
Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Medium] ext4: ext4_truncate_up() passes newsize to
ext4_ordered_flush_to() defeating fast path
- [Medium] ext4: size-preserving ext4_truncate() callers advance
i_disksize over delalloc
- [Medium] ext4: ext4_ordered_flush_to() skips delalloc inline
conversion inodes
- [Medium] ext4: ext4_ordered_flush_to() skips in-flight unwritten
writeback extents
--
commit 1960600589f6e33a9117e96bf09155f80a23934b
Author: Morduan Zang <zhangdandan@uniontech.com>
ext4: keep ordered data and i_disksize consistent on size changes
This commit avoids advancing i_disksize during size-preserving ftruncate()
calls and flushes delayed allocation data via ext4_ordered_flush_to()
before updating i_size and i_disksize on size-changing truncates in
data=ordered mode.
> diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
> index 10cd85d4c721b..01d5001e607fd 100644
> --- a/fs/ext4/inode.c
> +++ b/fs/ext4/inode.c
[ ... ]
> @@ -5545,6 +5545,11 @@ int ext4_truncate(struct inode *inode)
> return err;
> }
>
> +int ext4_truncate(struct inode *inode)
> +{
> + return __ext4_truncate(inode, true);
> +}
> +
> static inline u64 ext4_inode_peek_iversion(const struct inode *inode)
> {
> if (unlikely(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL))
[Severity: Medium]
Can size-preserving callers of ext4_truncate() still advance i_disksize
over uncommitted delalloc data?
When a file already has buffered delalloc data below inode->i_size and a
later extending write fails or copies short, ext4_truncate_failed_write()
calls ext4_truncate(inode) to trim blocks past inode->i_size without
changing i_size. Because ext4_truncate() passes update_disksize = true and
__ext4_truncate() removes the orphan entry at out_stop when inode->i_nlink
is non-zero, this commits i_disksize = inode->i_size to the journal while
the earlier delalloc blocks remain unallocated, leaving zero-filled holes
after a crash.
Similarly, ext4_begin_enable_verity() and the error cleanup path in
ext4_end_enable_verity() call ext4_truncate(inode) without changing i_size
before filemap_write_and_wait() runs; on a live inode, orphan recovery in
ext4_process_orphan() only truncates blocks past i_disksize and does not
protect delalloc data below an advanced i_disksize.
> @@ -6907,6 +6912,48 @@ static inline void ext4_set_inode_size(struct inode *inode, loff_t newsize)
> up_write(&EXT4_I(inode)->i_data_sem);
> }
>
> +/*
> + * Write back delalloc data before covering it with a new on-disk size.
> + *
> + * DIO and fallocate can advance i_disksize over older delalloc extents, so
> + * include the first delayed extent in the surviving range when choosing the
> + * start. The inode and invalidate locks held by ext4_setattr() prevent new
> + * delalloc extents; concurrent writeback can only remove them and make the
> + * flush more conservative.
> + *
> + * Read i_disksize under i_data_sem to serialize with writeback and avoid a
> + * torn read on 32-bit systems. Drop the lock before waiting because
> + * writeback needs it to advance i_disksize.
> + *
> + * Inline data is committed with the inode and needs no separate flush.
> + * Must be called without a journal handle and before i_size is lowered.
> + */
> +static int ext4_ordered_flush_to(struct inode *inode, loff_t newsize)
> +{
> + struct extent_status es;
> + ext4_lblk_t end_lblk;
> + loff_t start;
> +
> + if (!ext4_should_order_data(inode) || ext4_has_inline_data(inode) ||
> + newsize <= 0)
> + return 0;
[Severity: Medium]
Does checking ext4_has_inline_data(inode) here skip flushing folio 0 when
an inline inode has a pending delalloc conversion to extents?
When a buffered write exceeds inline capacity,
ext4_da_convert_inline_data_to_extent() copies the inline data into dirty
delalloc folio 0 (with BH_Delay set) and clears EXT4_STATE_MAY_INLINE_DATA,
while leaving EXT4_INODE_INLINE_DATA and i_inline_off set until writeback.
If the file is then truncated to a non-zero size <= i_inline_size,
ext4_setattr() skips ext4_convert_inline_data() and ext4_ordered_flush_to()
returns 0 without writing folio 0. In addition to committing the new
i_disksize over stale on-disk inline data on a crash, shrinking to
0 < newsize < i_inline_size reaches ext4_inline_data_truncate(), which
removes the delayed extent from the extent status tree and releases the
delalloc reservation while folio 0 still has BH_Delay set, triggering a
WARN_ON(1) in ext4_da_update_reserve_space() and reservation underflow
during later writeback.
Checking ext4_has_inline_data(inode) &&
ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA) would ensure
inodes with a pending delalloc conversion are flushed.
> +
> + end_lblk = (newsize - 1) >> inode->i_blkbits;
> + ext4_es_find_extent_range(inode, ext4_es_is_delayed, 0, end_lblk, &es);
> +
> + down_read(&EXT4_I(inode)->i_data_sem);
> + start = EXT4_I(inode)->i_disksize;
> + up_read(&EXT4_I(inode)->i_data_sem);
> +
> + if (es.es_len && es.es_lblk <= end_lblk)
> + start = min(start, EXT4_LBLK_TO_B(inode, es.es_lblk));
> + if (newsize <= start)
> + return 0;
> +
> + return filemap_write_and_wait_range(inode->i_mapping, start,
> + newsize - 1);
> +}
> +
[Severity: Medium]
Can ext4_ordered_flush_to() skip waiting on in-flight writeback if
asynchronous writeback has already mapped the delalloc blocks as unwritten?
With dioread_nolock enabled (the default), mpage_map_and_submit_extent()
maps delayed blocks as unwritten (replacing EXTENT_STATUS_DELAYED with
EXTENT_STATUS_UNWRITTEN) and advances EXT4_I(inode)->i_disksize before the
BIO completes and before ext4_end_io_rsv_work() converts the unwritten
extent to written:
fs/ext4/inode.c:mpage_map_and_submit_extent() {
...
down_write(&EXT4_I(inode)->i_data_sem);
if (disksize > EXT4_I(inode)->i_disksize)
EXT4_I(inode)->i_disksize = disksize;
up_write(&EXT4_I(inode)->i_data_sem);
...
}
Because unwritten extents use a reserved handle on io_end rather than
jbd2_journal_file_inode(), they do not block transaction commit. If
background writeback (or filemap_flush() in ext4_convert_inline_data()) has
already mapped the delalloc range as unwritten and raised i_disksize,
ext4_es_find_extent_range() finds no delayed extent and start is >= the
in-flight range, so ext4_ordered_flush_to() skips
filemap_write_and_wait_range(). If the truncate transaction commits and
the system crashes before unwritten conversion finishes, the file recovers
with the new i_disksize over unwritten extents that read back as zeros.
> static int ext4_truncate_up(struct inode *inode, loff_t oldsize, loff_t newsize)
> {
> ext4_lblk_t old_lblk, new_lblk;
> @@ -6926,6 +6973,10 @@ static int ext4_truncate_up(struct inode *inode, loff_t oldsize, loff_t newsize)
> return ret;
> }
>
> + ret = ext4_ordered_flush_to(inode, newsize);
> + if (ret)
> + return ret;
> +
> handle = ext4_journal_start(inode, EXT4_HT_INODE, 3);
> if (IS_ERR(handle))
> return PTR_ERR(handle);
[Severity: Medium]
Should ext4_truncate_up() pass oldsize instead of newsize to
ext4_ordered_flush_to()?
In ext4_truncate_up(), newsize is always greater than oldsize and
i_disksize is at most oldsize, so start <= i_disksize <= oldsize < newsize
always holds. The fast-path check (newsize <= start) in
ext4_ordered_flush_to() is therefore never taken on an extending truncate,
and when oldsize is not block-aligned, filemap_write_and_wait_range()
synchronously writes and waits on the tail block just dirtied by
ext4_block_zero_eof() even when the file had no delalloc extents.
--
Sashiko AI review · https://sashiko.dev/#/patchset/D7EEAE1F0CDD3029+20261118075044.3511841-1-chenyichong@uniontech.com?part=1
prev parent reply other threads:[~2026-11-18 8:43 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-18 7:50 [PATCH] ext4: keep ordered data and i_disksize consistent on size changes Yichong Chen
2026-11-18 8:43 ` sashiko-bot [this message]
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-220480@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=chenyichong@uniontech.com \
--cc=linux-ext4@vger.kernel.org \
--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