All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] ext4: keep ordered data and i_disksize consistent on size changes
@ 2026-11-18  7:50 Yichong Chen
  2026-11-18  8:43 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Yichong Chen @ 2026-11-18  7:50 UTC (permalink / raw)
  To: tytso, adilger.kernel, libaokun, jack, ojaswin, ritesh.list,
	yi.zhang, linux-ext4, linux-kernel
  Cc: guanwentao, niecheng1, Morduan Zang

From: Morduan Zang <zhangdandan@uniontech.com>

ext4 propagates i_size into i_disksize on three size-changing paths:
ext4_truncate_up(), the shrink branch of ext4_truncate_down(), and
ext4_truncate() reached from a size-preserving ftruncate().  In ordered
data mode these paths can commit the new size while data below it is still
delalloc-only.  Delalloc data has no JBD2 ordered-data registration before
writeback; the registration done while allocating its blocks cannot
retroactively protect an earlier size update.

A crash between the metadata commit and writeback can therefore bring the
file back with a non-zero size, zero blocks and no extents - the classic
NULL file that generic/044..046 check for.

Measured on 7.4.0-rc3 (aebb7274, x86_64, ext4, default mount options) on
a copy of the xfstests scratch image, 20 files per run, pwrite then
truncate with no delay in between, sleep 10s, then simulated crash:

  write  truncate   result before the fix
  64k    64k        size=65536 blocks=0 no extent    (generic/044)
  64k    32k        size=32768 blocks=0 no extent    (generic/045)
  32k    64k        size=65536 blocks=0 no extent    (generic/046)

Fix both directions:

 - A truncate that does not change i_size only has to discard preallocated
   blocks past i_size.  It must not advance i_disksize, so route it to
   __ext4_truncate(inode, false).  Every other caller (evict, orphan
   cleanup, verity, real shrink) keeps advancing it, where the orphan list
   makes the update safe.

 - When the size really changes, write out the still-delalloc range before
   i_size/i_disksize are updated, via ext4_ordered_flush_to().  This runs
   outside any journal handle so writeback can start its own transaction,
   and before i_size is lowered so writeback still sees the old, larger
   size.

Start the writeback at i_disksize, but also search the extent status tree
for the first delayed extent in the surviving range and use the lower of
the two.  DIO and fallocate can advance i_disksize over older buffered
delalloc data, so i_disksize alone is not sufficient to prove that all
blocks below it exist.  For example, a buffered write followed by an
extending fallocate and a shrink can otherwise still leave a NULL file.

The inode and invalidate locks held by ext4_setattr() prevent new delalloc
extents from appearing.  Concurrent writeback can only remove delayed
extents or advance i_disksize, making the selected range more conservative.
Take the i_disksize snapshot under i_data_sem to serialize with writeback
and avoid a torn 64-bit read on 32-bit systems.  Drop the lock before
waiting.

This avoids turning every size change into a whole-file sync point when
there is no earlier delalloc data.  Inline data needs no separate flush
because it is committed with the inode itself.

The new writeback is limited to data=ordered: data=journal already keeps
data in the journal, and data=writeback never made this guarantee.  The
size-preserving change applies to all modes and simply avoids advancing
i_disksize past delalloc data.

Signed-off-by: Morduan Zang <zhangdandan@uniontech.com>
---
 fs/ext4/ext4.h     |  6 ++--
 fs/ext4/extents.c  | 18 +++++++----
 fs/ext4/indirect.c |  8 +++--
 fs/ext4/inode.c    | 75 +++++++++++++++++++++++++++++++++++++++++-----
 4 files changed, 89 insertions(+), 18 deletions(-)

diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
index bf0325ecbec7..b948a5953760 100644
--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -3263,7 +3263,8 @@ static inline bool is_special_ino(struct super_block *sb, unsigned long ino)
 extern int ext4_ind_map_blocks(handle_t *handle, struct inode *inode,
 				struct ext4_map_blocks *map, int flags);
 extern int ext4_ind_trans_blocks(struct inode *inode, int nrblocks);
-extern void ext4_ind_truncate(handle_t *, struct inode *inode);
+extern void ext4_ind_truncate(handle_t *handle, struct inode *inode,
+			      bool update_disksize);
 extern int ext4_ind_remove_space(handle_t *handle, struct inode *inode,
 				 ext4_lblk_t start, ext4_lblk_t end);
 
@@ -3965,7 +3966,8 @@ extern int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
 ext4_lblk_t ext4_determine_insert_hole(struct inode *inode, ext4_lblk_t lblk,
 				       ext4_lblk_t hole_start,
 				       ext4_lblk_t hole_len);
-extern int ext4_ext_truncate(handle_t *, struct inode *);
+extern int ext4_ext_truncate(handle_t *handle, struct inode *inode,
+			     bool update_disksize);
 extern int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start,
 				 ext4_lblk_t end);
 extern void ext4_ext_init(struct super_block *);
diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c
index cd35e88bc372..fb5a16e5b176 100644
--- a/fs/ext4/extents.c
+++ b/fs/ext4/extents.c
@@ -4548,7 +4548,8 @@ int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
 	return err ? err : allocated;
 }
 
-int ext4_ext_truncate(handle_t *handle, struct inode *inode)
+int ext4_ext_truncate(handle_t *handle, struct inode *inode,
+		      bool update_disksize)
 {
 	struct super_block *sb = inode->i_sb;
 	ext4_lblk_t last_block;
@@ -4560,11 +4561,16 @@ int ext4_ext_truncate(handle_t *handle, struct inode *inode)
 	 * because page truncation is enough.
 	 */
 
-	/* we have to know where to truncate from in crash case */
-	__ext4_set_i_disksize(inode, inode->i_size);
-	err = ext4_mark_inode_dirty(handle, inode);
-	if (err)
-		return err;
+	/*
+	 * A size-preserving truncate must not expose delalloc-only data by
+	 * advancing the on-disk size before writeback.
+	 */
+	if (update_disksize) {
+		__ext4_set_i_disksize(inode, inode->i_size);
+		err = ext4_mark_inode_dirty(handle, inode);
+		if (err)
+			return err;
+	}
 
 	last_block = (inode->i_size + sb->s_blocksize - 1)
 			>> EXT4_BLOCK_SIZE_BITS(sb);
diff --git a/fs/ext4/indirect.c b/fs/ext4/indirect.c
index a495080df710..d4de88c802b8 100644
--- a/fs/ext4/indirect.c
+++ b/fs/ext4/indirect.c
@@ -1126,7 +1126,8 @@ static void ext4_free_branches(handle_t *handle, struct inode *inode,
 	}
 }
 
-void ext4_ind_truncate(handle_t *handle, struct inode *inode)
+void ext4_ind_truncate(handle_t *handle, struct inode *inode,
+		       bool update_disksize)
 {
 	struct ext4_inode_info *ei = EXT4_I(inode);
 	__le32 *i_data = ei->i_data;
@@ -1158,8 +1159,11 @@ void ext4_ind_truncate(handle_t *handle, struct inode *inode)
 	 * the new, shorter inode size (held for now in i_size) into the
 	 * on-disk inode. We do this via i_disksize, which is the value which
 	 * ext4 *really* writes onto the disk inode.
+	 *
+	 * A size-preserving truncate must not advance it past delalloc data.
 	 */
-	ei->i_disksize = inode->i_size;
+	if (update_disksize)
+		ei->i_disksize = inode->i_size;
 
 	if (last_block == max_block) {
 		/*
diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index e3129353d4ed..48de9716305f 100644
--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -5410,7 +5410,7 @@ int ext4_inode_attach_jinode(struct inode *inode)
  * that's fine - as long as they are linked from the inode, the post-crash
  * ext4_truncate() run will find them and release them.
  */
-int ext4_truncate(struct inode *inode)
+static int __ext4_truncate(struct inode *inode, bool update_disksize)
 {
 	struct ext4_inode_info *ei = EXT4_I(inode);
 	unsigned int credits;
@@ -5483,9 +5483,9 @@ int ext4_truncate(struct inode *inode)
 	ext4_discard_preallocations(inode);
 
 	if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
-		err = ext4_ext_truncate(handle, inode);
+		err = ext4_ext_truncate(handle, inode, update_disksize);
 	else
-		ext4_ind_truncate(handle, inode);
+		ext4_ind_truncate(handle, inode, update_disksize);
 
 	up_write(&ei->i_data_sem);
 	if (err)
@@ -5516,6 +5516,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))
@@ -6881,6 +6886,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;
+
+	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);
+}
+
 static int ext4_truncate_up(struct inode *inode, loff_t oldsize, loff_t newsize)
 {
 	ext4_lblk_t old_lblk, new_lblk;
@@ -6900,6 +6947,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);
@@ -6931,10 +6982,20 @@ static int ext4_truncate_down(struct inode *inode, loff_t oldsize,
 	int ret;
 
 	/* Do not change i_size. */
-	if (newsize == oldsize)
-		goto truncate;
+	if (newsize == oldsize) {
+		/*
+		 * Only blocks past i_size need to be discarded.  Do not advance
+		 * i_disksize past data that may still be delalloc-only.
+		 */
+		truncate_pagecache(inode, newsize);
+		return __ext4_truncate(inode, false);
+	}
 
 	/* Shrink. */
+	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);
@@ -6971,11 +7032,9 @@ static int ext4_truncate_down(struct inode *inode, loff_t oldsize,
 
 	if (ext4_should_journal_data(inode))
 		ext4_wait_for_tail_page_commit(inode);
-truncate:
 	/*
 	 * Truncate pagecache after we've waited for commit in data=journal
-	 * mode to make pages freeable.  Call ext4_truncate() even if
-	 * i_size didn't change to truncate possible preallocated blocks.
+	 * mode to make pages freeable.
 	 */
 	truncate_pagecache(inode, newsize);
 	return ext4_truncate(inode);
-- 
2.51.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] ext4: keep ordered data and i_disksize consistent on size changes
  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
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-18  8:43 UTC (permalink / raw)
  To: Yichong Chen, linux-ext4; +Cc: tytso

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-18  8:43 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.