Linux EXT4 FS development
 help / color / mirror / Atom feed
* [PATCH v2 00/13] ext4: fix endio zeroout corruption by tracking to-be-converted unwritten IOs
@ 2026-11-27 15:10 Ojaswin Mujoo
  2026-11-27 15:10 ` [PATCH v2 01/13] ext4: disable zeroout fallback in ext4_split_extent() Ojaswin Mujoo
                   ` (12 more replies)
  0 siblings, 13 replies; 18+ messages in thread
From: Ojaswin Mujoo @ 2026-11-27 15:10 UTC (permalink / raw)
  To: Theodore Ts'o, Andreas Dilger, Baokun Li, Jan Kara,
	Ritesh Harjani (IBM), Zhang Yi
  Cc: linux-ext4, linux-kernel

** Changes since v1 **

v1: https://lore.kernel.org/all/20261102-ext4-endio-zero-bug-v1-0-266d2bb9e6dc@linux.ibm.com

* The design to fix has completely changed from v1. Instead of tracking 
  inflight IOs, we track to-be-converted ranges in the extent status
  cache. This gives us much more accurate accounting

* Most of the details regarding the design are in subsequent patches
  arranged as follows:

  Patch 1: disable zeroout, this can be backported by distros as a quick fix
  Patch 2-4: introduce convert pending es type and helpers to handle it
  Patch 5: introduce get block flags that can be used to add such extents
  Patch 6-8: onboard IO paths to use conv pending tracking
  Patch 9: don't zeroout if conv pending blocks are present in the extent
  Patch 10: add some useful tracepoints
  Patch 11-12: misc fixes
  Patch 13: re enable zeroout fallback

* Testing: this passes my targeted testing for endio corruption (faliing before
  this patch), -g quick tests for 1k, 4k and 64k bs (4k pagesize) and all ext4 
  kunit tests (incl the newly added ones). I plan to run more auto group and
  stress testing parallely.

** Original Cover (Fix design is outdated) **

There is a data corruption in the zeroout fallback
ext4_split_extent_zeroout() when we encounter an error like ENOSPC
while trying to perform endio extent splitting.

The issue was discovered by Fedora coreos community while using 1kb
blocks size on a 64kb pagesize arch like powerpc or aarch64 and the
discussion can be found at [1].

** The Issue **

The issue can be encountered as follows:

1. Make an FS with no reserved to root reserved blocks and fill it
   completely.
2. Delete a file so that ext4_sb_info thinks there is free space but
   mballoc doesnt have any (until txn is committed).
3. Now when endio kicks in it eventually reaches:

ext4_handle_unwritten_extents
  ext4_split_convert_extents
    ext4_split_extent
      ext4_split_extent_at
        ext4_ext_insert_extent
          ext4_new_meta_blocks  // extent tree split/grow needs a block
            ext4_mb_new_blocks
              ext4_claim_free_clusters  // thinks we have free space but mballoc
                                           has none until txn commit frees it
              ext4_mb_regular_allocator
                // return -ENOSPC
    // ext4_split_extent fallsback to zeroout
    ext4_split_extent_zeroout
      // We try to zeroout surrounding unwritten ranges. This code has a bug
         where we zero an unwritten extent which could have valid data on disk 
         whose endio has not yet converted it

The main issue is that zeroout assumes any unwritten extent is valid to
zeroout but it could actually be a soon-to-be-converted extent holding
valid data.

** The fix **

The way this series fixes it is by per-inode tracking the no. of
unwritten IOs that are submitted but not yet converted
(ei->i_conv_pending_ios). If we end up in the zeroout fallback and see
there are such IOs existing in the inode, then we don't zeroout and just
return back the original error. More design notes can be found in
subsequent patches.

For direct IO, ext4_iomap_begin() is called once per mapping and has no
way to account something only once per dio. So we first fix it in a
simple way by relying on inode->i_dio_count which is already
incremented/decremented once per dio. The last patch optimizes this by
moving dio accounting to i_conv_pending_ios as well, by using a custom
ext4_iomap_next() (needed because we want to increment once per
dio). Since iomap_next() infra was added in 7.2+ this can't be
backported so kept this approach separate.

There are other approaches considered like completely disabling zeroout
(not ideal as it's a useful fallback) or tracking the conv_pending ranges
in extent cache (this is an intrusive change) however the current
approach was chosen as a middle ground to balance complexity vs
accuracy of accounting.

** Backport Notes **

This issue seems to have existed ever since we had the zeroout logic
however this was never encountered until we moved extent splitting from
writeback time to endio time in commit:

ea96cb5c4ae3 ("ext4: don't split extent before submitting I/O") (merged
in 7.0)

which changed the dioread_nolock writeback/dio path to submit I/O
without pre-splitting extents, deferring the split to endio. This means
multiple concurrent writes can share a single unwritten extent, and a
failed split on one of them could zeroout data belonging to another,
which exposes this issue in the zeroout logic. 

Note that the issue in zeroout was always present but the above patch
just uncovered it. However, for the purpose of backports, I've used this
patch. Seeing how we never saw this issue before this, hopefully just
backporting it till 7.0 should be fine.

** Testing **

I've tested -g quick for various configurations to catch any obvious
issues or counter leaks, saw no regressions. I'll continue to test for
auto group. I've also added some new tests to hit this regression which
I'll post soon.

[1] https://github.com/coreos/fedora-coreos-tracker/issues/2234

Thoughts and suggestions welcome.

Regards,
ojaswin

Signed-off-by: Ojaswin Mujoo <ojaswin@linux.ibm.com>
---
Ojaswin Mujoo (13):
      ext4: disable zeroout fallback in ext4_split_extent()
      ext4: introduce CONVERT_PENDING extent status type
      ext4: preserve convert pending extents when adding unwrit extents to es cache
      ext4: add es helper to convert unwrit to convert pending without changing i_es_seq
      ext4: add support to track convert pending ranges in ext4_map_blocks()
      ext4: add tracking for unwritten to-be-converted ranges in dio write path
      ext4: add tracking for unwritten to-be-converted ranges in non iomap buffered path
      ext4: add tracking for unwritten to-be-converted ranges in iomap buffered path
      ext4: don't zeroout if conv pending ranges are present
      ext4: add tracepoints for extent split and convert
      ext4: update extent status cache when zeroing extents
      ext4: don't drop the extents in case extent split fails
      ext4: re-enable zeroout fallback

 fs/ext4/ext4.h                |  11 ++-
 fs/ext4/extents.c             |  86 +++++++++++++++++--
 fs/ext4/extents_status-test.c |   8 +-
 fs/ext4/extents_status.c      | 132 +++++++++++++++++++++++-----
 fs/ext4/extents_status.h      |  13 ++-
 fs/ext4/inode.c               | 129 ++++++++++++++++++++++++++--
 fs/ext4/page-io.c             |  43 ++++++++++
 include/trace/events/ext4.h   | 194 +++++++++++++++++++++++++++++++++++++++++-
 8 files changed, 577 insertions(+), 39 deletions(-)
---
base-commit: bae28a1b11f422d0214654800d9425e042295c71
change-id: 20261023-ext4-endio-zero-bug-ec627ae61f39

Best regards,
-- 
Ojaswin Mujoo <ojaswin@linux.ibm.com>


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

* [PATCH v2 01/13] ext4: disable zeroout fallback in ext4_split_extent()
  2026-11-27 15:10 [PATCH v2 00/13] ext4: fix endio zeroout corruption by tracking to-be-converted unwritten IOs Ojaswin Mujoo
@ 2026-11-27 15:10 ` Ojaswin Mujoo
  2026-11-27 15:10 ` [PATCH v2 02/13] ext4: introduce CONVERT_PENDING extent status type Ojaswin Mujoo
                   ` (11 subsequent siblings)
  12 siblings, 0 replies; 18+ messages in thread
From: Ojaswin Mujoo @ 2026-11-27 15:10 UTC (permalink / raw)
  To: Theodore Ts'o, Andreas Dilger, Baokun Li, Jan Kara,
	Ritesh Harjani (IBM), Zhang Yi
  Cc: linux-ext4, linux-kernel

The zeroout fallback can race with in-flight endio conversion of unwritten
extents and zero out valid data. Just disable it for now with a plan to fix
the race and re-enable it in future patches.

Fixes: ea96cb5c4ae3 ("ext4: don't split extent before submitting I/O")
Signed-off-by: Ojaswin Mujoo <ojaswin@linux.ibm.com>
---
 fs/ext4/extents.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c
index 10d36012ade4..48c36322bbc8 100644
--- a/fs/ext4/extents.c
+++ b/fs/ext4/extents.c
@@ -3512,6 +3512,9 @@ static struct ext4_ext_path *ext4_split_extent(handle_t *handle,
 	if (orig_err != -ENOSPC && orig_err != -EDQUOT && orig_err != -ENOMEM)
 		goto out_orig_err;
 
+	/* zeroout fallback disabled: always return the original err */
+	goto out_orig_err;
+
 	/* we can't zeroout? just return the original err */
 	if (!(split_flag & EXT4_EXT_MAY_ZEROOUT))
 		goto out_orig_err;

-- 
2.55.0


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

* [PATCH v2 02/13] ext4: introduce CONVERT_PENDING extent status type
  2026-11-27 15:10 [PATCH v2 00/13] ext4: fix endio zeroout corruption by tracking to-be-converted unwritten IOs Ojaswin Mujoo
  2026-11-27 15:10 ` [PATCH v2 01/13] ext4: disable zeroout fallback in ext4_split_extent() Ojaswin Mujoo
@ 2026-11-27 15:10 ` Ojaswin Mujoo
  2026-11-27 15:58   ` sashiko-bot
  2026-11-27 15:10 ` [PATCH v2 03/13] ext4: preserve convert pending extents when adding unwrit extents to es cache Ojaswin Mujoo
                   ` (10 subsequent siblings)
  12 siblings, 1 reply; 18+ messages in thread
From: Ojaswin Mujoo @ 2026-11-27 15:10 UTC (permalink / raw)
  To: Theodore Ts'o, Andreas Dilger, Baokun Li, Jan Kara,
	Ritesh Harjani (IBM), Zhang Yi
  Cc: linux-ext4, linux-kernel

Add support to track ranges that are in an unwritten state but actaully
cover valid data. These are the unwritten ranges which have an in progress
disk IO, but are not yet converted to written via endio handling.

Upcoming patches will use this extent type to avoid zerooing out such
ranges during endio extent splitting, because they cover valid data.

The lifecycle of an extent undergoing write is now as follows:

1. Say we start with an extent (0,10,unwrit) which is also cached in es.

    Ondisk:     [0,10,unwrit]
    es cache:   [0,10,unwrit]

2. We perform a write to (0, 5):

    Ondisk:     [0,10,unwrit]
    es cache:   [0,5,conv_pending][5,10,unwrit]

3. IO is finished and endio handler converts (0,5) to written:

    ondisk:     [0,5,writ][5,10,unwrit]
    es cache:   [0,5,writ][5,10,unwrit]

An extent of CONVERT_PENDING type will behave similar to an UNWRITTEN
extent for most of the purposes like read. Just that we will take special
care in es cache to make sure the conv_pending range is not split or merged
so that we don't lose the status. Similarly, it also can't be evicted from
memory because we don't want to lost the status.

Signed-off-by: Ojaswin Mujoo <ojaswin@linux.ibm.com>
---
 fs/ext4/extents_status-test.c |  8 ++++----
 fs/ext4/extents_status.c      | 18 ++++++++++++++++++
 fs/ext4/extents_status.h      | 11 ++++++++++-
 include/trace/events/ext4.h   |  4 +++-
 4 files changed, 35 insertions(+), 6 deletions(-)

diff --git a/fs/ext4/extents_status-test.c b/fs/ext4/extents_status-test.c
index 825d12513e4a..014475bae07c 100644
--- a/fs/ext4/extents_status-test.c
+++ b/fs/ext4/extents_status-test.c
@@ -452,13 +452,13 @@ static void test_es_bitfield_written_and_unwritten_encoding(struct kunit *test)
 static void test_es_bitfield_delayed_and_hole_encoding(struct kunit *test)
 {
 	struct extent_status es = {};
-	/* 59-bit physical block field (64 - 5 status bits) all ones */
-	const ext4_fsblk_t max_59bit_pblk = 0x07ffffffffffffffULL;
+	/* 58-bit physical block field (64 - 6 status bits) all ones */
+	const ext4_fsblk_t max_pblk = ~ES_MASK;
 
 	ext4_es_store_pblock_status(&es, ~0ULL, EXTENT_STATUS_DELAYED);
 	KUNIT_EXPECT_TRUE(test, ext4_es_is_delayed(&es));
 	KUNIT_EXPECT_FALSE(test, ext4_es_is_mapped(&es));
-	KUNIT_EXPECT_EQ(test, ext4_es_pblock(&es), max_59bit_pblk);
+	KUNIT_EXPECT_EQ(test, ext4_es_pblock(&es), max_pblk);
 	KUNIT_EXPECT_EQ(test, ext4_es_show_pblock(&es), 0ULL);
 
 	ext4_es_store_pblock_status(&es, 0ULL, EXTENT_STATUS_HOLE);
@@ -472,7 +472,7 @@ test_es_bitfield_referenced_and_pblock_update_preserve_state(struct kunit *test)
 {
 	struct extent_status es = {};
 	const ext4_fsblk_t initial_pblk = 0x123456789abcULL;
-	const ext4_fsblk_t updated_pblk = 0x07ffffffffffffffULL;
+	const ext4_fsblk_t updated_pblk = ~ES_MASK;
 
 	ext4_es_store_pblock_status(&es, initial_pblk, EXTENT_STATUS_WRITTEN);
 
diff --git a/fs/ext4/extents_status.c b/fs/ext4/extents_status.c
index b7fb8502f9d4..28528f19be1d 100644
--- a/fs/ext4/extents_status.c
+++ b/fs/ext4/extents_status.c
@@ -529,6 +529,9 @@ static inline bool ext4_es_must_keep(struct extent_status *es)
 	if (ext4_es_is_delayed(es))
 		return true;
 
+	if (ext4_es_is_convert_pending(es))
+		return true;
+
 	return false;
 }
 
@@ -594,6 +597,12 @@ static int ext4_es_can_be_merged(struct extent_status *es1,
 	if (ext4_es_type(es1) != ext4_es_type(es2))
 		return 0;
 
+	/*
+	 * Don't merge block range that's about to get converted to written.
+	 */
+	if (ext4_es_is_convert_pending(es1))
+		return 0;
+
 	if (((__u64) es1->es_len) + es2->es_len > EXT_MAX_BLOCKS) {
 		pr_warn("ES assertion failed when merging extents. "
 			"The sum of lengths of es1 (%d) and es2 (%d) "
@@ -1076,6 +1085,15 @@ void ext4_es_cache_extent(struct inode *inode, ext4_lblk_t lblk,
 	    ext4_es_type(&chkes) == EXTENT_STATUS_DELAYED)
 		return;
 
+	/*
+	 * An ondisk unwritten extent is allowed to conflict with
+	 * convert_pending since the latter signifies an ondisk unwritten extent
+	 * about to be converted to written due to endio.
+	 */
+	if (status == EXTENT_STATUS_UNWRITTEN &&
+	    ext4_es_type(&chkes) == EXTENT_STATUS_CONVERT_PENDING)
+		return;
+
 	ext4_warning_inode(inode,
 			   "ES cache extent failed: add [%d,%d,%llu,0x%x] conflict with existing [%d,%d,%llu,0x%x]\n",
 			   lblk, len, pblk, status, chkes.es_lblk, chkes.es_len,
diff --git a/fs/ext4/extents_status.h b/fs/ext4/extents_status.h
index 01cddcaaa68e..2247f811f6c3 100644
--- a/fs/ext4/extents_status.h
+++ b/fs/ext4/extents_status.h
@@ -36,6 +36,7 @@ enum {
 	ES_DELAYED_B,
 	ES_HOLE_B,
 	ES_REFERENCED_B,
+	ES_CONVERT_PENDING_B,
 	ES_FLAGS
 };
 
@@ -51,10 +52,12 @@ enum {
 #define EXTENT_STATUS_DELAYED	(1 << ES_DELAYED_B)
 #define EXTENT_STATUS_HOLE	(1 << ES_HOLE_B)
 #define EXTENT_STATUS_REFERENCED	(1 << ES_REFERENCED_B)
+#define EXTENT_STATUS_CONVERT_PENDING	(1 << ES_CONVERT_PENDING_B)
 
 #define ES_TYPE_MASK	((ext4_fsblk_t)(EXTENT_STATUS_WRITTEN | \
 			  EXTENT_STATUS_UNWRITTEN | \
 			  EXTENT_STATUS_DELAYED | \
+			  EXTENT_STATUS_CONVERT_PENDING | \
 			  EXTENT_STATUS_HOLE))
 
 #define ES_TYPE_VALID(type)	((type) && !((type) & ((type) - 1)))
@@ -173,7 +176,13 @@ static inline int ext4_es_is_written(struct extent_status *es)
 
 static inline int ext4_es_is_unwritten(struct extent_status *es)
 {
-	return (ext4_es_type(es) & EXTENT_STATUS_UNWRITTEN) != 0;
+	return (ext4_es_type(es) &
+		(EXTENT_STATUS_UNWRITTEN | EXTENT_STATUS_CONVERT_PENDING)) != 0;
+}
+
+static inline int ext4_es_is_convert_pending(struct extent_status *es)
+{
+	return (ext4_es_type(es) & EXTENT_STATUS_CONVERT_PENDING) != 0;
 }
 
 static inline int ext4_es_is_delayed(struct extent_status *es)
diff --git a/include/trace/events/ext4.h b/include/trace/events/ext4.h
index 92048da9dae4..4313694cd4e9 100644
--- a/include/trace/events/ext4.h
+++ b/include/trace/events/ext4.h
@@ -77,13 +77,15 @@ TRACE_DEFINE_ENUM(ES_UNWRITTEN_B);
 TRACE_DEFINE_ENUM(ES_DELAYED_B);
 TRACE_DEFINE_ENUM(ES_HOLE_B);
 TRACE_DEFINE_ENUM(ES_REFERENCED_B);
+TRACE_DEFINE_ENUM(ES_CONVERT_PENDING_B);
 
 #define show_extent_status(status) __print_flags(status, "",	\
 	{ EXTENT_STATUS_WRITTEN,	"W" },			\
 	{ EXTENT_STATUS_UNWRITTEN,	"U" },			\
 	{ EXTENT_STATUS_DELAYED,	"D" },			\
 	{ EXTENT_STATUS_HOLE,		"H" },			\
-	{ EXTENT_STATUS_REFERENCED,	"R" })
+	{ EXTENT_STATUS_REFERENCED,	"R" },			\
+	{ EXTENT_STATUS_CONVERT_PENDING,	"C" })
 
 #define show_falloc_mode(mode) __print_flags(mode, "|",		\
 	{ FALLOC_FL_KEEP_SIZE,		"KEEP_SIZE"},		\

-- 
2.55.0


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

* [PATCH v2 03/13] ext4: preserve convert pending extents when adding unwrit extents to es cache
  2026-11-27 15:10 [PATCH v2 00/13] ext4: fix endio zeroout corruption by tracking to-be-converted unwritten IOs Ojaswin Mujoo
  2026-11-27 15:10 ` [PATCH v2 01/13] ext4: disable zeroout fallback in ext4_split_extent() Ojaswin Mujoo
  2026-11-27 15:10 ` [PATCH v2 02/13] ext4: introduce CONVERT_PENDING extent status type Ojaswin Mujoo
@ 2026-11-27 15:10 ` Ojaswin Mujoo
  2026-11-27 15:10 ` [PATCH v2 04/13] ext4: add es helper to convert unwrit to convert pending without changing i_es_seq Ojaswin Mujoo
                   ` (9 subsequent siblings)
  12 siblings, 0 replies; 18+ messages in thread
From: Ojaswin Mujoo @ 2026-11-27 15:10 UTC (permalink / raw)
  To: Theodore Ts'o, Andreas Dilger, Baokun Li, Jan Kara,
	Ritesh Harjani (IBM), Zhang Yi
  Cc: linux-ext4, linux-kernel

ext4_es_insert_extent() interfaces overwrites the old range with the new
extent type. This can cause a CONVERT_PENDING extent type to be lost when
it is overwritten with unwritten extent.

This is undesirable because paths like falloc will call
ext4_es_insert_extent() which removes all extents in the range and then
adds the new unwritten es extent. To ensure this doesn't end up removing
pending CONV_PENDING extents, we add logic in __es_remove_extent() to check
if we are crossing any CONV_PENDING extent, in which case we don't cache
the new unwrit extent.

Signed-off-by: Ojaswin Mujoo <ojaswin@linux.ibm.com>
---
 fs/ext4/extents_status.c | 67 ++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 57 insertions(+), 10 deletions(-)

diff --git a/fs/ext4/extents_status.c b/fs/ext4/extents_status.c
index 28528f19be1d..75dd065106e4 100644
--- a/fs/ext4/extents_status.c
+++ b/fs/ext4/extents_status.c
@@ -181,7 +181,8 @@ static int __es_insert_extent(struct inode *inode, struct extent_status *newes,
 static int __es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
 			      ext4_lblk_t end, unsigned int status,
 			      int *reserved, struct extent_status *res,
-			      struct extent_status *prealloc);
+			      struct extent_status *prealloc,
+			      bool *preserve_conv_pending);
 static int es_reclaim_extents(struct ext4_inode_info *ei, int *nr_to_scan);
 static int __es_shrink(struct ext4_sb_info *sbi, int nr_to_scan,
 		       int *nr_scanned);
@@ -924,6 +925,7 @@ void ext4_es_insert_extent(struct inode *inode, ext4_lblk_t lblk,
 	struct extent_status *es2 = NULL;
 	struct pending_reservation *pr = NULL;
 	bool revise_pending = false;
+	bool preserve_conv_pending;
 
 	if (EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
 		return;
@@ -946,7 +948,8 @@ void ext4_es_insert_extent(struct inode *inode, ext4_lblk_t lblk,
 	revise_pending = sbi->s_cluster_ratio > 1 &&
 			 test_opt(inode->i_sb, DELALLOC) &&
 			 (status & (EXTENT_STATUS_WRITTEN |
-				    EXTENT_STATUS_UNWRITTEN));
+				    EXTENT_STATUS_UNWRITTEN |
+				    EXTENT_STATUS_CONVERT_PENDING));
 retry:
 	if (err1 && !es1)
 		es1 = __es_alloc_extent(true);
@@ -956,7 +959,10 @@ void ext4_es_insert_extent(struct inode *inode, ext4_lblk_t lblk,
 		pr = __alloc_pending(true);
 	write_lock(&EXT4_I(inode)->i_es_lock);
 
-	err1 = __es_remove_extent(inode, lblk, end, 0, &resv_used, NULL, es1);
+	preserve_conv_pending  = status & EXTENT_STATUS_UNWRITTEN;
+
+	err1 = __es_remove_extent(inode, lblk, end, 0, &resv_used, NULL, es1,
+				  &preserve_conv_pending);
 	if (err1 != 0)
 		goto error;
 	/* Free preallocated extent if it didn't get used. */
@@ -966,6 +972,15 @@ void ext4_es_insert_extent(struct inode *inode, ext4_lblk_t lblk,
 		es1 = NULL;
 	}
 
+	/*
+	 * For now, to keep things simple we don't insert an unwritten extent if
+	 * there are conver pending extents already in the range. Hopefully they
+	 * will be cached once we finish the endio conversoin, however this can
+	 * be implemented later if performance issues are noticed.
+	 */
+	if (preserve_conv_pending)
+		goto skip_insert;
+
 	err2 = __es_insert_extent(inode, &newes, es2);
 	if (err2 == -ENOMEM && !ext4_es_must_keep(&newes))
 		err2 = 0;
@@ -978,6 +993,7 @@ void ext4_es_insert_extent(struct inode *inode, ext4_lblk_t lblk,
 		es2 = NULL;
 	}
 
+skip_insert:
 	if (revise_pending) {
 		err3 = __revise_pending(inode, lblk, len, &pr);
 		if (err3 < 0)
@@ -1063,7 +1079,7 @@ void ext4_es_cache_extent(struct inode *inode, ext4_lblk_t lblk,
 		}
 		/* Check and remove all extents in range. */
 		err = __es_remove_extent(inode, lblk, end, status, NULL,
-					 &chkes, NULL);
+					 &chkes, NULL, NULL);
 		if (err) {
 			if (err == -EINVAL)
 				conflict = true;
@@ -1502,7 +1518,8 @@ static unsigned int get_rsvd(struct inode *inode, ext4_lblk_t end,
 static int __es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
 			      ext4_lblk_t end, unsigned int status,
 			      int *reserved, struct extent_status *res,
-			      struct extent_status *prealloc)
+			      struct extent_status *prealloc,
+			      bool *preserve_conv_pending)
 {
 	struct ext4_es_tree *tree = &EXT4_I(inode)->i_es_tree;
 	struct rb_node *node;
@@ -1513,6 +1530,16 @@ static int __es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
 	int err;
 	bool count_reserved = true;
 	struct rsvd_count rc;
+	bool has_conv_pending = false;
+	bool preserve_es = preserve_conv_pending && *preserve_conv_pending;
+
+
+	/*
+	 * Reset it, we will set it again based on if the range had a convert
+	 * pending entry
+	 */
+	if (preserve_es)
+		*preserve_conv_pending = false;
 
 	if (reserved == NULL || !test_opt(inode->i_sb, DELALLOC))
 		count_reserved = false;
@@ -1540,6 +1567,12 @@ static int __es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
 
 	len1 = lblk > es->es_lblk ? lblk - es->es_lblk : 0;
 	len2 = ext4_es_end(es) > end ? ext4_es_end(es) - end : 0;
+
+	if (ext4_es_is_convert_pending(es) && preserve_es) {
+		has_conv_pending = true;
+		goto next;
+	}
+
 	if (len1 > 0)
 		es->es_len = len1;
 	if (len2 > 0) {
@@ -1583,6 +1616,7 @@ static int __es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
 		if (count_reserved)
 			count_rsvd(inode, lblk, orig_es.es_len - len1,
 				   &orig_es, &rc);
+next:
 		node = rb_next(&es->rb_node);
 		if (node)
 			es = rb_entry(node, struct extent_status, rb_node);
@@ -1597,8 +1631,14 @@ static int __es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
 		if (count_reserved)
 			count_rsvd(inode, es->es_lblk, es->es_len, es, &rc);
 		node = rb_next(&es->rb_node);
-		rb_erase(&es->rb_node, &tree->root);
-		ext4_es_free_extent(inode, es);
+
+		if (ext4_es_is_convert_pending(es) && preserve_es) {
+			has_conv_pending = true;
+		} else {
+			rb_erase(&es->rb_node, &tree->root);
+			ext4_es_free_extent(inode, es);
+		}
+
 		if (!node) {
 			es = NULL;
 			break;
@@ -1613,19 +1653,26 @@ static int __es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
 		if (err)
 			return err;
 
+		if (ext4_es_is_convert_pending(es) && preserve_es) {
+			has_conv_pending = true;
+			goto out;
+		}
+
 		len1 = ext4_es_end(es) - end;
 		if (count_reserved)
 			count_rsvd(inode, es->es_lblk, orig_len - len1,
 				   es, &rc);
 		es->es_lblk = end + 1;
 		es->es_len = len1;
-		if (ext4_es_is_written(es) || ext4_es_is_unwritten(es)) {
+		if (ext4_es_is_mapped(es)) {
 			block = es->es_pblk + orig_len - len1;
 			ext4_es_store_pblock(es, block);
 		}
 	}
 
 out:
+	if (preserve_conv_pending && has_conv_pending)
+		*preserve_conv_pending = true;
 	if (count_reserved)
 		*reserved = get_rsvd(inode, end, es, &rc);
 	return 0;
@@ -1670,7 +1717,7 @@ void ext4_es_remove_extent(struct inode *inode, ext4_lblk_t lblk,
 	 * is reclaimed.
 	 */
 	write_lock(&EXT4_I(inode)->i_es_lock);
-	err = __es_remove_extent(inode, lblk, end, 0, &reserved, NULL, es);
+	err = __es_remove_extent(inode, lblk, end, 0, &reserved, NULL, es, NULL);
 	if (err)
 		goto error;
 	/* Free preallocated extent if it didn't get used. */
@@ -2288,7 +2335,7 @@ void ext4_es_insert_delayed_extent(struct inode *inode, ext4_lblk_t lblk,
 	}
 	write_lock(&EXT4_I(inode)->i_es_lock);
 
-	err1 = __es_remove_extent(inode, lblk, end, 0, NULL, NULL, es1);
+	err1 = __es_remove_extent(inode, lblk, end, 0, NULL, NULL, es1, NULL);
 	if (err1 != 0)
 		goto error;
 	/* Free preallocated extent if it didn't get used. */

-- 
2.55.0


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

* [PATCH v2 04/13] ext4: add es helper to convert unwrit to convert pending without changing i_es_seq
  2026-11-27 15:10 [PATCH v2 00/13] ext4: fix endio zeroout corruption by tracking to-be-converted unwritten IOs Ojaswin Mujoo
                   ` (2 preceding siblings ...)
  2026-11-27 15:10 ` [PATCH v2 03/13] ext4: preserve convert pending extents when adding unwrit extents to es cache Ojaswin Mujoo
@ 2026-11-27 15:10 ` Ojaswin Mujoo
  2026-11-27 16:46   ` sashiko-bot
  2026-11-27 15:10 ` [PATCH v2 05/13] ext4: add support to track convert pending ranges in ext4_map_blocks() Ojaswin Mujoo
                   ` (8 subsequent siblings)
  12 siblings, 1 reply; 18+ messages in thread
From: Ojaswin Mujoo @ 2026-11-27 15:10 UTC (permalink / raw)
  To: Theodore Ts'o, Andreas Dilger, Baokun Li, Jan Kara,
	Ritesh Harjani (IBM), Zhang Yi
  Cc: linux-ext4, linux-kernel

Upcoming codepaths would often want to convert an existing unwritten extent
to convert pending in extent status cache. This is essentially not an
operation that needs to bump the i_es_seq because both of these types mean
the same thing for most of the code paths that care about the sequence
counter. Hence, introduce a new helper to change the extent status without
having to bump the extent status cache.

Signed-off-by: Ojaswin Mujoo <ojaswin@linux.ibm.com>
---
 fs/ext4/extents_status.c | 45 ++++++++++++++++++++++++++++++++++++---------
 fs/ext4/extents_status.h |  2 ++
 fs/ext4/inode.c          | 23 +++++++++++++++++++++++
 3 files changed, 61 insertions(+), 9 deletions(-)

diff --git a/fs/ext4/extents_status.c b/fs/ext4/extents_status.c
index 75dd065106e4..244cc0f62196 100644
--- a/fs/ext4/extents_status.c
+++ b/fs/ext4/extents_status.c
@@ -907,14 +907,10 @@ static int __es_insert_extent(struct inode *inode, struct extent_status *newes,
 	return 0;
 }
 
-/*
- * ext4_es_insert_extent() adds information to an inode's extent
- * status tree. This interface is used for modifying extents. To cache
- * on-disk extents, use ext4_es_cache_extent() instead.
- */
-void ext4_es_insert_extent(struct inode *inode, ext4_lblk_t lblk,
-			   ext4_lblk_t len, ext4_fsblk_t pblk,
-			   unsigned int status, bool delalloc_reserve_used)
+static void __ext4_es_insert_extent(struct inode *inode, ext4_lblk_t lblk,
+				    ext4_lblk_t len, ext4_fsblk_t pblk,
+				    unsigned int status,
+				    bool delalloc_reserve_used, bool inc_seq)
 {
 	struct extent_status newes;
 	ext4_lblk_t end = lblk + len - 1;
@@ -1004,7 +1000,8 @@ void ext4_es_insert_extent(struct inode *inode, ext4_lblk_t lblk,
 		}
 		pending = err3;
 	}
-	ext4_es_inc_seq(inode);
+	if (inc_seq)
+		ext4_es_inc_seq(inode);
 error:
 	write_unlock(&EXT4_I(inode)->i_es_lock);
 	/*
@@ -1036,6 +1033,35 @@ void ext4_es_insert_extent(struct inode *inode, ext4_lblk_t lblk,
 	return;
 }
 
+/*
+ * ext4_es_insert_extent() adds information to an inode's extent
+ * status tree. This interface is used for modifying extents. To cache
+ * on-disk extents, use ext4_es_cache_extent() instead.
+ */
+void ext4_es_insert_extent(struct inode *inode, ext4_lblk_t lblk,
+			   ext4_lblk_t len, ext4_fsblk_t pblk,
+			   unsigned int status, bool delalloc_reserve_used)
+{
+	__ext4_es_insert_extent(inode, lblk, len, pblk, status,
+				delalloc_reserve_used, true);
+}
+
+/*
+ * ext4_es_mark_convert_pending() marks an already unwritten range as
+ * CONVERT_PENDING. For most of the codepaths CONVERT_PENDING is treated as
+ * unwritten everywhere (except the endio zeroout path, which is already
+ * serialized with i_data_sem) hence we don't bump the i_es_seq here.
+ *
+ * The caller must hold i_data_sem in shared (at least) and must have made sure
+ * that the range is unwritten.
+ */
+void ext4_es_mark_convert_pending(struct inode *inode, ext4_lblk_t lblk,
+				  ext4_lblk_t len, ext4_fsblk_t pblk)
+{
+	__ext4_es_insert_extent(inode, lblk, len, pblk,
+				EXTENT_STATUS_CONVERT_PENDING, false, false);
+}
+
 /*
  * ext4_es_cache_extent() inserts information into the extent status tree
  * only if there is no existing information about the specified range or
@@ -2488,6 +2514,7 @@ static int __revise_pending(struct inode *inode, ext4_lblk_t lblk,
 #if IS_ENABLED(CONFIG_EXT4_KUNIT_TESTS)
 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_es_init_tree);
 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_es_insert_extent);
+EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_es_mark_convert_pending);
 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_es_cache_extent);
 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_es_remove_extent);
 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_es_find_extent_range);
diff --git a/fs/ext4/extents_status.h b/fs/ext4/extents_status.h
index 2247f811f6c3..b1bc34af786f 100644
--- a/fs/ext4/extents_status.h
+++ b/fs/ext4/extents_status.h
@@ -140,6 +140,8 @@ extern void ext4_es_insert_extent(struct inode *inode, ext4_lblk_t lblk,
 				  ext4_lblk_t len, ext4_fsblk_t pblk,
 				  unsigned int status,
 				  bool delalloc_reserve_used);
+extern void ext4_es_mark_convert_pending(struct inode *inode, ext4_lblk_t lblk,
+					 ext4_lblk_t len, ext4_fsblk_t pblk);
 extern void ext4_es_cache_extent(struct inode *inode, ext4_lblk_t lblk,
 				 ext4_lblk_t len, ext4_fsblk_t pblk,
 				 unsigned int status);
diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index 9368a68db289..f7fc5fb7e819 100644
--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -757,6 +757,29 @@ int ext4_map_create_blocks(handle_t *handle, struct inode *inode,
 	return retval;
 }
 
+/*
+ * Mark the unwritten range in @map as convert pending in the extent status
+ * tree. This is used by the zeroout code to ensure we don't write zeroes on
+ * such unwritten ranges and hence must be serialized against endio conversion /
+ * extent zeroout, which hold i_data_sem excl.
+ *
+ * Returns false if the extent status tree changed since @map was looked up, in
+ * which case the caller must redo the lookup.
+ */
+static bool ext4_map_mark_convert_pending(struct inode *inode,
+					  struct ext4_map_blocks *map)
+{
+	bool ok;
+
+	down_read(&EXT4_I(inode)->i_data_sem);
+	ok = map->m_seq == READ_ONCE(EXT4_I(inode)->i_es_seq);
+	if (ok)
+		ext4_es_mark_convert_pending(inode, map->m_lblk, map->m_len,
+					     map->m_pblk);
+	up_read(&EXT4_I(inode)->i_data_sem);
+	return ok;
+}
+
 /*
  * The ext4_map_blocks() function tries to look up the requested blocks,
  * and returns if the blocks are already mapped.

-- 
2.55.0


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

* [PATCH v2 05/13] ext4: add support to track convert pending ranges in ext4_map_blocks()
  2026-11-27 15:10 [PATCH v2 00/13] ext4: fix endio zeroout corruption by tracking to-be-converted unwritten IOs Ojaswin Mujoo
                   ` (3 preceding siblings ...)
  2026-11-27 15:10 ` [PATCH v2 04/13] ext4: add es helper to convert unwrit to convert pending without changing i_es_seq Ojaswin Mujoo
@ 2026-11-27 15:10 ` Ojaswin Mujoo
  2026-11-27 17:19   ` sashiko-bot
  2026-11-27 15:10 ` [PATCH v2 06/13] ext4: add tracking for unwritten to-be-converted ranges in dio write path Ojaswin Mujoo
                   ` (7 subsequent siblings)
  12 siblings, 1 reply; 18+ messages in thread
From: Ojaswin Mujoo @ 2026-11-27 15:10 UTC (permalink / raw)
  To: Theodore Ts'o, Andreas Dilger, Baokun Li, Jan Kara,
	Ritesh Harjani (IBM), Zhang Yi
  Cc: linux-ext4, linux-kernel

Introduce a new get block flag to indicate that the caller is about to
issue IO over unwritten ranges and hence wants them to be marked as
convert pending. This allows upcoming changes to query such ranges before
trying to zero them out in endio.

We need to be careful to always add the CONVERT_PENDING extent status entry
under i_data_sem because the upcoming changes will use that sem to
serialize with the endio zeroout path.

Signed-off-by: Ojaswin Mujoo <ojaswin@linux.ibm.com>
---
 fs/ext4/ext4.h              |  9 ++++++++-
 fs/ext4/inode.c             | 23 +++++++++++++++++++++--
 include/trace/events/ext4.h |  1 +
 3 files changed, 30 insertions(+), 3 deletions(-)

diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
index 646c017dda1c..8cce99d68978 100644
--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -748,6 +748,12 @@ struct ext4_new_flex_group_data {
  */
 #define EXT4_GET_BLOCKS_QUERY_LAST_IN_LEAF	0x1000
 
+/*
+ * This range will get converted from unwritten to written soon due to endio
+ * conversion.
+ */
+#define EXT4_GET_BLOCKS_CONVERT_PENDING	0x2000
+
 /*
  * The bit position of these flags must not overlap with any of the
  * EXT4_GET_BLOCKS_*.  They are used by ext4_find_extent(),
@@ -766,7 +772,8 @@ struct ext4_new_flex_group_data {
  */
 #define EXT4_EX_QUERY_FILTER	(EXT4_EX_NOCACHE | EXT4_EX_FORCE_CACHE |\
 				 EXT4_EX_NOFAIL |\
-				 EXT4_GET_BLOCKS_QUERY_LAST_IN_LEAF)
+				 EXT4_GET_BLOCKS_QUERY_LAST_IN_LEAF |\
+				 EXT4_GET_BLOCKS_CONVERT_PENDING)
 
 /*
  * Flags used by ext4_free_blocks
diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index f7fc5fb7e819..3e4c0e1396ab 100644
--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -750,6 +750,9 @@ int ext4_map_create_blocks(handle_t *handle, struct inode *inode,
 
 	status = map->m_flags & EXT4_MAP_UNWRITTEN ?
 			EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
+	if (status == EXTENT_STATUS_UNWRITTEN &&
+	    flags & EXT4_GET_BLOCKS_CONVERT_PENDING)
+		status = EXTENT_STATUS_CONVERT_PENDING;
 	ext4_es_insert_extent(inode, map->m_lblk, map->m_len, map->m_pblk,
 			      status, flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE);
 	map->m_seq = READ_ONCE(EXT4_I(inode)->i_es_seq);
@@ -911,8 +914,17 @@ int ext4_map_blocks(handle_t *handle, struct inode *inode,
 	}
 
 	/* If it is only a block(s) look up */
-	if ((flags & EXT4_GET_BLOCKS_CREATE) == 0)
+	if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
+		if (map->m_flags & EXT4_MAP_UNWRITTEN &&
+		    flags & EXT4_GET_BLOCKS_CONVERT_PENDING &&
+		    !ext4_map_mark_convert_pending(inode, map)) {
+			map->m_flags = 0;
+			map->m_len = orig_mlen;
+			goto create_retry;
+		}
+
 		return retval;
+	}
 
 	/*
 	 * Returns if the blocks have already allocated
@@ -934,8 +946,15 @@ int ext4_map_blocks(handle_t *handle, struct inode *inode,
 		    !(flags & EXT4_GET_BLOCKS_CONVERT)) {
 			/* Contains EXT4_GET_BLOCKS_CREATE - mark mapped. */
 			ret = check_block_validity(inode, map);
-			if (!ret)
+			if (!ret) {
+				if (flags & EXT4_GET_BLOCKS_CONVERT_PENDING &&
+				    !ext4_map_mark_convert_pending(inode, map)) {
+					map->m_flags = 0;
+					map->m_len = orig_mlen;
+					goto create_retry;
+				}
 				map->m_flags |= EXT4_MAP_MAPPED;
+			}
 			goto out;
 		}
 	} else if (retval == 0) {
diff --git a/include/trace/events/ext4.h b/include/trace/events/ext4.h
index 4313694cd4e9..fb18fdcb3003 100644
--- a/include/trace/events/ext4.h
+++ b/include/trace/events/ext4.h
@@ -46,6 +46,7 @@ struct partial_cluster;
 	{ EXT4_GET_BLOCKS_CONVERT_UNWRITTEN,	"CONVERT_UNWRITTEN" },  \
 	{ EXT4_GET_BLOCKS_ZERO,			"ZERO" },		\
 	{ EXT4_GET_BLOCKS_IO_SUBMIT,		"IO_SUBMIT" },		\
+	{ EXT4_GET_BLOCKS_CONVERT_PENDING,	"CONV_PENDING" },		\
 	{ EXT4_EX_NOCACHE,			"EX_NOCACHE" })
 
 /*

-- 
2.55.0


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

* [PATCH v2 06/13] ext4: add tracking for unwritten to-be-converted ranges in dio write path
  2026-11-27 15:10 [PATCH v2 00/13] ext4: fix endio zeroout corruption by tracking to-be-converted unwritten IOs Ojaswin Mujoo
                   ` (4 preceding siblings ...)
  2026-11-27 15:10 ` [PATCH v2 05/13] ext4: add support to track convert pending ranges in ext4_map_blocks() Ojaswin Mujoo
@ 2026-11-27 15:10 ` Ojaswin Mujoo
  2026-11-27 17:48   ` sashiko-bot
  2026-11-27 15:10 ` [PATCH v2 07/13] ext4: add tracking for unwritten to-be-converted ranges in non iomap buffered path Ojaswin Mujoo
                   ` (6 subsequent siblings)
  12 siblings, 1 reply; 18+ messages in thread
From: Ojaswin Mujoo @ 2026-11-27 15:10 UTC (permalink / raw)
  To: Theodore Ts'o, Andreas Dilger, Baokun Li, Jan Kara,
	Ritesh Harjani (IBM), Zhang Yi
  Cc: linux-ext4, linux-kernel

Make sure dio write paths pass the EXT4_GET_BLOCKS_CONVERT_PENDING flag to
track unwritten ranges that are about to be converted. This enables
tracking such ranges in extent status. This avoid data corruption by
allowing endio extent zeroout code to avoid zeroing over such ranges.

Error note:

Ideally, on an IO error we should clear the CONVERT_PENDING bits since they
will not be converted, however, this is not straigh forward in dio. This is
because nothing stops parallel overlapping dios to mark the same range as
CONVERT_PENDING. If one of those fails, clearing CONVERT_PENDING would be
wrong because the other dio is still depending on it.

For simplicitly, we just don't clear it at all on dio errors because:

1. Clearing unconditionally exposes the range for zeroout corruption that
CONVERT_PENDING was designed to solve. Not clearning it is much more
forgiving because it just disables zeroout fallback on such ranges.

2. One sideeffect is that we would end up leaving these stale unreclaimable
CONVERT_PENDING es entries but this would be rare and hopefully future IOs
would consume the stale CONVERT_PENDING ranges if this happens.

3. Solving it would require much more intrusive changes to refcount track
each CONVERT_PENDING extent status.

Signed-off-by: Ojaswin Mujoo <ojaswin@linux.ibm.com>
---
 fs/ext4/ext4.h  |  2 ++
 fs/ext4/inode.c | 39 +++++++++++++++++++++++++++++++++++++--
 2 files changed, 39 insertions(+), 2 deletions(-)

diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
index 8cce99d68978..72de06efb4b2 100644
--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -4221,6 +4221,8 @@ extern int ext4_map_blocks(handle_t *handle, struct inode *inode,
 			   struct ext4_map_blocks *map, int flags);
 extern int ext4_map_query_blocks(handle_t *handle, struct inode *inode,
 				  struct ext4_map_blocks *map, int flags);
+extern void ext4_clear_convert_pending(struct inode *inode, ext4_lblk_t lblk,
+				       ext4_lblk_t len);
 extern int ext4_map_create_blocks(handle_t *handle, struct inode *inode,
 				  struct ext4_map_blocks *map, int flags);
 extern int ext4_ext_calc_credits_for_single_extent(struct inode *inode,
diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index 3e4c0e1396ab..4aafd0aaea69 100644
--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -783,6 +783,37 @@ static bool ext4_map_mark_convert_pending(struct inode *inode,
 	return ok;
 }
 
+/*
+ * Drop the convert pending extents in the given range from the extent status
+ * tree. Convert pending is set when we map an unwritten extent which is
+ * expected to be converted to written for endio soon. If the IO fails, nobody
+ * will do that, so just drop the cache.
+ */
+void ext4_clear_convert_pending(struct inode *inode, ext4_lblk_t lblk,
+				ext4_lblk_t len)
+{
+	ext4_lblk_t end = lblk + len - 1;
+	struct extent_status es;
+
+	if (!len)
+		return;
+
+	while (lblk <= end) {
+		ext4_lblk_t start, stop;
+
+		ext4_es_find_extent_range(inode, ext4_es_is_convert_pending,
+					  lblk, end, &es);
+		if (!es.es_len || es.es_lblk > end)
+			break;
+		start = max(lblk, es.es_lblk);
+		stop = min(end, es.es_lblk + es.es_len - 1);
+		ext4_es_remove_extent(inode, start, stop - start + 1);
+		if (stop == end)
+			break;
+		lblk = stop + 1;
+	}
+}
+
 /*
  * The ext4_map_blocks() function tries to look up the requested blocks,
  * and returns if the blocks are already mapped.
@@ -4012,7 +4043,8 @@ static int ext4_iomap_alloc(struct inode *inode, struct ext4_map_blocks *map,
 	else if (EXT4_LBLK_TO_B(inode, map->m_lblk) >= i_size_read(inode))
 		m_flags = EXT4_GET_BLOCKS_CREATE;
 	else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
-		m_flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT;
+		m_flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT |
+			  EXT4_GET_BLOCKS_CONVERT_PENDING;
 
 	if (flags & IOMAP_ATOMIC)
 		ret = ext4_map_blocks_atomic_write(handle, inode, map, m_flags,
@@ -4082,7 +4114,10 @@ int ext4_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
 		 * especially in multi-threaded overwrite requests.
 		 */
 		if (offset + length <= i_size_read(inode)) {
-			ret = ext4_map_blocks(NULL, inode, &map, 0);
+			ret = ext4_map_blocks(NULL, inode, &map,
+					      (flags & IOMAP_DAX) ?
+					      0 :
+					      EXT4_GET_BLOCKS_CONVERT_PENDING);
 			/*
 			 * For DAX we convert extents to initialized ones before
 			 * copying the data, otherwise we do it after I/O so

-- 
2.55.0


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

* [PATCH v2 07/13] ext4: add tracking for unwritten to-be-converted ranges in non iomap buffered path
  2026-11-27 15:10 [PATCH v2 00/13] ext4: fix endio zeroout corruption by tracking to-be-converted unwritten IOs Ojaswin Mujoo
                   ` (5 preceding siblings ...)
  2026-11-27 15:10 ` [PATCH v2 06/13] ext4: add tracking for unwritten to-be-converted ranges in dio write path Ojaswin Mujoo
@ 2026-11-27 15:10 ` Ojaswin Mujoo
  2026-11-27 15:10 ` [PATCH v2 08/13] ext4: add tracking for unwritten to-be-converted ranges in " Ojaswin Mujoo
                   ` (5 subsequent siblings)
  12 siblings, 0 replies; 18+ messages in thread
From: Ojaswin Mujoo @ 2026-11-27 15:10 UTC (permalink / raw)
  To: Theodore Ts'o, Andreas Dilger, Baokun Li, Jan Kara,
	Ritesh Harjani (IBM), Zhang Yi
  Cc: linux-ext4, linux-kernel

Make sure non iomap buffered write paths pass the
EXT4_GET_BLOCKS_CONVERT_PENDING flag to track unwritten ranges that are
about to be converted. This enables tracking such ranges in extent status.
This avoid data corruption by allowing endio extent zeroout code to avoid
zeroing over such ranges.

Note that CONVERT_PENDING and folio writeback state will always exist
together (serialised by folio_lock()). This ensures paths like
ext4_move_extents() won't silently drop CONVERT_PENDING state (as it drops
es cache).

Signed-off-by: Ojaswin Mujoo <ojaswin@linux.ibm.com>
---
 fs/ext4/inode.c   |  3 ++-
 fs/ext4/page-io.c | 20 ++++++++++++++++++++
 2 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index 4aafd0aaea69..2031b1dd7069 100644
--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -2633,7 +2633,8 @@ static int mpage_map_one_extent(handle_t *handle, struct mpage_da_data *mpd)
 
 	dioread_nolock = ext4_should_dioread_nolock(inode);
 	if (dioread_nolock)
-		get_blocks_flags |= EXT4_GET_BLOCKS_UNWRIT_EXT;
+		get_blocks_flags |= EXT4_GET_BLOCKS_UNWRIT_EXT |
+				    EXT4_GET_BLOCKS_CONVERT_PENDING;
 
 	err = ext4_map_blocks(handle, inode, map, get_blocks_flags);
 	if (err < 0)
diff --git a/fs/ext4/page-io.c b/fs/ext4/page-io.c
index 059827b961ec..6753b35311b4 100644
--- a/fs/ext4/page-io.c
+++ b/fs/ext4/page-io.c
@@ -158,6 +158,24 @@ static void ext4_release_io_end(ext4_io_end_t *io_end)
 	kmem_cache_free(io_end_cachep, io_end);
 }
 
+/*
+ * The unwritten ranges of a failed io_end were marked convert pending in
+ * mpage_map_one_extent() before the IO was submitted but will not be
+ * converted, so drop the cached state.
+ */
+static void ext4_io_end_clear_convert_pending(ext4_io_end_t *io_end)
+{
+	struct inode *inode = io_end->inode;
+	u8 blkbits = inode->i_blkbits;
+	struct ext4_io_end_vec *io_end_vec;
+
+	list_for_each_entry(io_end_vec, &io_end->list_vec, list)
+		ext4_clear_convert_pending(inode,
+				io_end_vec->offset >> blkbits,
+				EXT4_MAX_BLOCKS(io_end_vec->size,
+						io_end_vec->offset, blkbits));
+}
+
 /*
  * On successful IO, check a range of space and convert unwritten extents to
  * written. On IO failure, check if journal abort is needed. Note that
@@ -195,6 +213,8 @@ static int ext4_end_io_end(ext4_io_end_t *io_end)
 		if (ret < 0)
 			mapping_set_error(inode->i_mapping, ret);
 	}
+	if (ret < 0 && io_end->flag & EXT4_IO_END_UNWRITTEN)
+		ext4_io_end_clear_convert_pending(io_end);
 	if (ret < 0 && !ext4_emergency_state(sb) &&
 	    io_end->flag & EXT4_IO_END_UNWRITTEN) {
 		ext4_msg(sb, KERN_EMERG,

-- 
2.55.0


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

* [PATCH v2 08/13] ext4: add tracking for unwritten to-be-converted ranges in iomap buffered path
  2026-11-27 15:10 [PATCH v2 00/13] ext4: fix endio zeroout corruption by tracking to-be-converted unwritten IOs Ojaswin Mujoo
                   ` (6 preceding siblings ...)
  2026-11-27 15:10 ` [PATCH v2 07/13] ext4: add tracking for unwritten to-be-converted ranges in non iomap buffered path Ojaswin Mujoo
@ 2026-11-27 15:10 ` Ojaswin Mujoo
  2026-11-27 15:10 ` [PATCH v2 09/13] ext4: don't zeroout if conv pending ranges are present Ojaswin Mujoo
                   ` (4 subsequent siblings)
  12 siblings, 0 replies; 18+ messages in thread
From: Ojaswin Mujoo @ 2026-11-27 15:10 UTC (permalink / raw)
  To: Theodore Ts'o, Andreas Dilger, Baokun Li, Jan Kara,
	Ritesh Harjani (IBM), Zhang Yi
  Cc: linux-ext4, linux-kernel

Use the new CONV_PENDING tracking to track unwritten to-be-converted
extents in buffered iomap path in ext4. This ensures we don't zero out
unwritten ranges written by buffered iomap writeback path.

Signed-off-by: Ojaswin Mujoo <ojaswin@linux.ibm.com>
---
 fs/ext4/inode.c   | 41 +++++++++++++++++++++++++++++++++++++++++
 fs/ext4/page-io.c | 23 +++++++++++++++++++++++
 2 files changed, 64 insertions(+)

diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index 2031b1dd7069..947c20d081c6 100644
--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -4575,6 +4575,38 @@ static ssize_t ext4_iomap_writeback_range(struct iomap_writepage_ctx *wpc,
 	return ret;
 }
 
+/*
+ * Mark the unwritten blocks covered by @ioend as convert pending in the extent
+ * status tree.
+ */
+static int ext4_iomap_mark_convert_pending(struct inode *inode,
+					   struct iomap_ioend *ioend)
+{
+	u8 blkbits = inode->i_blkbits;
+	ext4_lblk_t lblk = ioend->io_offset >> blkbits;
+	unsigned int len = EXT4_MAX_BLOCKS(ioend->io_size, ioend->io_offset,
+					   blkbits);
+	struct ext4_map_blocks map;
+	int ret;
+
+	while (len) {
+		map.m_lblk = lblk;
+		map.m_len = len;
+		ret = ext4_map_blocks(NULL, inode, &map,
+				      EXT4_GET_BLOCKS_CONVERT_PENDING |
+				      EXT4_GET_BLOCKS_IO_SUBMIT |
+				      EXT4_EX_NOCACHE);
+		if (ret < 0)
+			return ret;
+		/* Blocks under writeback cannot be a hole. */
+		if (WARN_ON_ONCE(!ret))
+			return -EFSCORRUPTED;
+		lblk += ret;
+		len -= ret;
+	}
+	return 0;
+}
+
 static int ext4_iomap_writeback_submit(struct iomap_writepage_ctx *wpc,
 				       int error)
 {
@@ -4584,6 +4616,15 @@ static int ext4_iomap_writeback_submit(struct iomap_writepage_ctx *wpc,
 	unsigned int blocksize = i_blocksize(inode);
 	loff_t pstart, plen;
 
+	/*
+	 * The unwritten blocks are about to be written. Mark them convert
+	 * pending before the I/O is submitted so that endio doesn't zeroout the
+	 * range.
+	 */
+	if (!error && (ioend->io_flags & IOMAP_IOEND_UNWRITTEN) &&
+	    ioend->io_size)
+		error = ext4_iomap_mark_convert_pending(inode, ioend);
+
 	/*
 	 * After I/O completion, a worker needs to be scheduled when:
 	 * 1) Unwritten extents require conversion.
diff --git a/fs/ext4/page-io.c b/fs/ext4/page-io.c
index 6753b35311b4..591dd539a44e 100644
--- a/fs/ext4/page-io.c
+++ b/fs/ext4/page-io.c
@@ -666,6 +666,27 @@ static int ext4_iomap_wb_update_disksize(handle_t *handle, struct inode *inode,
 	return ret;
 }
 
+/*
+ * The unwritten range of a failed ioend was marked convert pending before the
+ * I/O was submitted but will not be converted. Drop the cached state so that
+ * it is re-read from the extent tree as plain unwritten. Since this is a pure
+ * extent status cache change, i_data_sem shared should be enough
+ */
+static void ext4_iomap_clear_convert_pending(struct iomap_ioend *ioend)
+{
+	struct inode *inode = ioend->io_inode;
+	u8 blkbits = inode->i_blkbits;
+
+	if (!ioend->io_size)
+		return;
+
+	down_read(&EXT4_I(inode)->i_data_sem);
+	ext4_clear_convert_pending(inode, ioend->io_offset >> blkbits,
+				   EXT4_MAX_BLOCKS(ioend->io_size,
+						   ioend->io_offset, blkbits));
+	up_read(&EXT4_I(inode)->i_data_sem);
+}
+
 static void ext4_iomap_finish_ioend(struct iomap_ioend *ioend)
 {
 	struct inode *inode = ioend->io_inode;
@@ -731,6 +752,8 @@ static void ext4_iomap_finish_ioend(struct iomap_ioend *ioend)
 			 inode->i_ino, ret);
 	}
 out:
+	if (ret && (ioend->io_flags & IOMAP_IOEND_UNWRITTEN))
+		ext4_iomap_clear_convert_pending(ioend);
 	iomap_finish_ioends(ioend, ret);
 }
 

-- 
2.55.0


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

* [PATCH v2 09/13] ext4: don't zeroout if conv pending ranges are present
  2026-11-27 15:10 [PATCH v2 00/13] ext4: fix endio zeroout corruption by tracking to-be-converted unwritten IOs Ojaswin Mujoo
                   ` (7 preceding siblings ...)
  2026-11-27 15:10 ` [PATCH v2 08/13] ext4: add tracking for unwritten to-be-converted ranges in " Ojaswin Mujoo
@ 2026-11-27 15:10 ` Ojaswin Mujoo
  2026-11-27 15:10 ` [PATCH v2 10/13] ext4: add tracepoints for extent split and convert Ojaswin Mujoo
                   ` (3 subsequent siblings)
  12 siblings, 0 replies; 18+ messages in thread
From: Ojaswin Mujoo @ 2026-11-27 15:10 UTC (permalink / raw)
  To: Theodore Ts'o, Andreas Dilger, Baokun Li, Jan Kara,
	Ritesh Harjani (IBM), Zhang Yi
  Cc: linux-ext4, linux-kernel

Currently, if ext4 is unable to perform a split due an error in endio, we
have a zeroout fallback which zeroes the neighboring ranges if they are
unwritten and converts the whole extent to written without splitting.
This is helpful to offset any transient endio errors, preventing data loss.

However, there exists a race which was causing us to zeroout valid data.
This was as follows:

1. Make an FS with no reserved to root reserved blocks and fill it
   completely.
2. Delete a file so that ext4_sb_info thinks there is free space but
   mballoc doesn't have any (until txn is committed).
3. Now when endio kicks in it eventually reaches:

ext4_handle_unwritten_extents
  ext4_split_convert_extents
    ext4_split_extent
      ext4_split_extent_at
        ext4_ext_insert_extent
          ext4_new_meta_blocks  // extent tree split/grow needs a block
            ext4_mb_new_blocks
              ext4_claim_free_clusters  // thinks we have free space but
	                                   mballoc has none until txn
					   commit frees it
              ext4_mb_regular_allocator
                // return -ENOSPC
    // ext4_split_extent fallsback to zeroout
    ext4_split_extent_zeroout
      // We try to zeroout surrounding unwritten ranges. This code has a
      bug where we zero an unwritten extent which could have valid data on
      disk whose endio has not yet converted it

The main issue is that zeroout assumes any unwritten extent is valid to
zeroout but it could actually be a soon-to-be-converted extent holding
valid data. Patches leading up to this patch have added a new extent status
cache entry that can indicate such soon to be converted extents.

This patch modfies the zeroout path to query these extents before taking
the decision to zeroout or not, hence allowing us to prevent the data
corruption.

Signed-off-by: Ojaswin Mujoo <ojaswin@linux.ibm.com>
---
 fs/ext4/extents.c | 28 ++++++++++++++++++++++++----
 1 file changed, 24 insertions(+), 4 deletions(-)

diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c
index 48c36322bbc8..b816e9edb041 100644
--- a/fs/ext4/extents.c
+++ b/fs/ext4/extents.c
@@ -3342,6 +3342,7 @@ static int ext4_split_extent_zeroout(handle_t *handle, struct inode *inode,
 	uint64_t lblk, pblk, len;
 	int is_unwrit;
 	int err = 0;
+	loff_t map_end, ex_end;
 
 	depth = ext_depth(inode);
 	ex = path[depth].p_ext;
@@ -3349,16 +3350,35 @@ static int ext4_split_extent_zeroout(handle_t *handle, struct inode *inode,
 	ee_len = ext4_ext_get_actual_len(ex);
 	is_unwrit = ext4_ext_is_unwritten(ex);
 
+	map_end = (loff_t) map->m_lblk + map->m_len;
+	ex_end = (loff_t) ee_block + ee_len;
+
+	/*
+	 * zeroout is only possible if we don't have range in the extent that is
+	 * pending conversion (ie IO is complete and endio conversion is
+	 * remaining) This is so that we don't accidentally zeroout a valid data
+	 * range we have not yet converted.
+	 */
+	/* Check left side */
+	lblk = ee_block;
+	len = map->m_lblk > ee_block ? map->m_lblk - ee_block : 0;
+	if (len > 0 && ext4_es_scan_range(inode, &ext4_es_is_convert_pending,
+					  lblk, lblk + len - 1))
+		return -EINVAL;
+
+	/* Check right side */
+	lblk = map_end;
+	len = ex_end > map_end ? ex_end - map_end : 0;
+	if (len > 0 && ext4_es_scan_range(inode, &ext4_es_is_convert_pending,
+					  lblk, lblk + len - 1))
+		return -EINVAL;
+
 	if (flags & EXT4_GET_BLOCKS_CONVERT) {
 		/*
 		 * EXT4_GET_BLOCKS_CONVERT: Caller wants the range specified by
 		 * map to be initialized. Zeroout everything except the map
 		 * range.
 		 */
-
-		loff_t map_end = (loff_t) map->m_lblk + map->m_len;
-		loff_t ex_end = (loff_t) ee_block + ee_len;
-
 		if (!is_unwrit)
 			/* Shouldn't happen. Just exit */
 			return -EINVAL;

-- 
2.55.0


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

* [PATCH v2 10/13] ext4: add tracepoints for extent split and convert
  2026-11-27 15:10 [PATCH v2 00/13] ext4: fix endio zeroout corruption by tracking to-be-converted unwritten IOs Ojaswin Mujoo
                   ` (8 preceding siblings ...)
  2026-11-27 15:10 ` [PATCH v2 09/13] ext4: don't zeroout if conv pending ranges are present Ojaswin Mujoo
@ 2026-11-27 15:10 ` Ojaswin Mujoo
  2026-11-27 15:10 ` [PATCH v2 11/13] ext4: update extent status cache when zeroing extents Ojaswin Mujoo
                   ` (2 subsequent siblings)
  12 siblings, 0 replies; 18+ messages in thread
From: Ojaswin Mujoo @ 2026-11-27 15:10 UTC (permalink / raw)
  To: Theodore Ts'o, Andreas Dilger, Baokun Li, Jan Kara,
	Ritesh Harjani (IBM), Zhang Yi
  Cc: linux-ext4, linux-kernel

Add some useful tracepoint to track how the extent split, conversion and
zeroout fallback behave. Also add a tracepoint to track conlicts in extent
status cache.

Signed-off-by: Ojaswin Mujoo <ojaswin@linux.ibm.com>
---
 fs/ext4/extents.c           |  29 +++++++
 fs/ext4/extents_status.c    |   2 +
 include/trace/events/ext4.h | 189 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 220 insertions(+)

diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c
index b816e9edb041..0f5d9f3c42a9 100644
--- a/fs/ext4/extents.c
+++ b/fs/ext4/extents.c
@@ -3390,6 +3390,10 @@ static int ext4_split_extent_zeroout(handle_t *handle, struct inode *inode,
 			pblk = ext4_ext_pblock(ex);
 			err = ext4_issue_zeroout(handle, inode, lblk, pblk,
 						 len);
+			trace_ext4_extent_zeroout(inode, ee_block, ee_len,
+						  ext4_ext_pblock(ex),
+						  is_unwrit, lblk, pblk, len,
+						  err);
 			if (err)
 				/* ZEROOUT failed, just return original error */
 				return err;
@@ -3402,6 +3406,10 @@ static int ext4_split_extent_zeroout(handle_t *handle, struct inode *inode,
 			pblk = ext4_ext_pblock(ex) + (map_end - ee_block);
 			err = ext4_issue_zeroout(handle, inode, lblk, pblk,
 						 len);
+			trace_ext4_extent_zeroout(inode, ee_block, ee_len,
+						  ext4_ext_pblock(ex),
+						  is_unwrit, lblk, pblk, len,
+						  err);
 			if (err)
 				/* ZEROOUT failed, just return original error */
 				return err;
@@ -3421,6 +3429,9 @@ static int ext4_split_extent_zeroout(handle_t *handle, struct inode *inode,
 		len = map->m_len;
 		pblk = ext4_ext_pblock(ex) + (map->m_lblk - ee_block);
 		err = ext4_issue_zeroout(handle, inode, lblk, pblk, len);
+		trace_ext4_extent_zeroout(inode, ee_block, ee_len,
+					  ext4_ext_pblock(ex), is_unwrit,
+					  lblk, pblk, len, err);
 		if (err)
 			/* ZEROOUT failed, just return original error */
 			return err;
@@ -3519,6 +3530,9 @@ static struct ext4_ext_path *ext4_split_extent(handle_t *handle,
 			goto try_zeroout;
 	}
 
+	trace_ext4_extent_split(inode, map, ee_block, ee_len, flags,
+				unwritten);
+
 	goto success;
 
 try_zeroout:
@@ -3882,6 +3896,9 @@ static struct ext4_ext_path *ext4_split_convert_extents(handle_t *handle,
 	struct ext4_extent *ex;
 	struct ext4_extent converted_ex;
 	unsigned int ee_len;
+	ext4_lblk_t old_ee_block;
+	unsigned int old_ee_len;
+	bool old_unwrit;
 	int split_flag = 0, depth, err = 0;
 	bool did_zeroout = false;
 
@@ -3897,6 +3914,9 @@ static struct ext4_ext_path *ext4_split_convert_extents(handle_t *handle,
 	ee_block = le32_to_cpu(ex->ee_block);
 	ee_len = ext4_ext_get_actual_len(ex);
 
+	trace_ext4_split_convert_enter(inode, map, ee_block, ee_len, flags,
+				       ext4_ext_is_unwritten(ex));
+
 	/* No split needed */
 	if (ee_block == map->m_lblk && ee_len == map->m_len)
 		goto convert;
@@ -3933,6 +3953,10 @@ static struct ext4_ext_path *ext4_split_convert_extents(handle_t *handle,
 		if (err)
 			goto err;
 
+		old_ee_block = le32_to_cpu(ex->ee_block);
+		old_ee_len = ext4_ext_get_actual_len(ex);
+		old_unwrit = ext4_ext_is_unwritten(ex);
+
 		if (flags & EXT4_GET_BLOCKS_CONVERT)
 			ext4_ext_mark_initialized(ex);
 		else if (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN)
@@ -3952,6 +3976,11 @@ static struct ext4_ext_path *ext4_split_convert_extents(handle_t *handle,
 		if (err)
 			goto err;
 		ex = &converted_ex;
+
+		trace_ext4_extent_convert(inode, map, old_ee_block, old_ee_len,
+				old_unwrit, le32_to_cpu(ex->ee_block),
+				ext4_ext_get_actual_len(ex),
+				ext4_ext_is_unwritten(ex), flags);
 	}
 
 	/* Lets update the extent status tree after conversion */
diff --git a/fs/ext4/extents_status.c b/fs/ext4/extents_status.c
index 244cc0f62196..6c5340dce6ed 100644
--- a/fs/ext4/extents_status.c
+++ b/fs/ext4/extents_status.c
@@ -1136,6 +1136,8 @@ void ext4_es_cache_extent(struct inode *inode, ext4_lblk_t lblk,
 	    ext4_es_type(&chkes) == EXTENT_STATUS_CONVERT_PENDING)
 		return;
 
+	trace_ext4_es_cache_extent_conflict(inode, lblk, len, pblk, status,
+					    &chkes);
 	ext4_warning_inode(inode,
 			   "ES cache extent failed: add [%d,%d,%llu,0x%x] conflict with existing [%d,%d,%llu,0x%x]\n",
 			   lblk, len, pblk, status, chkes.es_lblk, chkes.es_len,
diff --git a/include/trace/events/ext4.h b/include/trace/events/ext4.h
index fb18fdcb3003..988f86729ea2 100644
--- a/include/trace/events/ext4.h
+++ b/include/trace/events/ext4.h
@@ -2273,6 +2273,49 @@ DEFINE_EVENT(ext4__es_extent, ext4_es_cache_extent,
 	TP_ARGS(inode, es)
 );
 
+TRACE_EVENT(ext4_es_cache_extent_conflict,
+	TP_PROTO(struct inode *inode, ext4_lblk_t lblk, ext4_lblk_t len,
+		 ext4_fsblk_t pblk, unsigned int status,
+		 struct extent_status *conflict_es),
+
+	TP_ARGS(inode, lblk, len, pblk, status, conflict_es),
+
+	TP_STRUCT__entry(
+		__field(	dev_t,		dev		)
+		__field(	u64,		ino		)
+		__field(	ext4_lblk_t,	lblk		)
+		__field(	ext4_lblk_t,	len		)
+		__field(	ext4_fsblk_t,	pblk		)
+		__field(	unsigned int,	status		)
+		__field(	ext4_lblk_t,	conflict_lblk	)
+		__field(	ext4_lblk_t,	conflict_len	)
+		__field(	ext4_fsblk_t,	conflict_pblk	)
+		__field(	unsigned int,	conflict_status	)
+	),
+
+	TP_fast_assign(
+		__entry->dev		= inode->i_sb->s_dev;
+		__entry->ino		= inode->i_ino;
+		__entry->lblk		= lblk;
+		__entry->len		= len;
+		__entry->pblk		= pblk;
+		__entry->status		= status;
+		__entry->conflict_lblk	= conflict_es->es_lblk;
+		__entry->conflict_len	= conflict_es->es_len;
+		__entry->conflict_pblk	= ext4_es_pblock(conflict_es);
+		__entry->conflict_status = ext4_es_status(conflict_es);
+	),
+
+	TP_printk("dev %d,%d ino %llu new extent [%u/%u) pblk %llu status 0x%x "
+		  "conflicts with cached extent [%u/%u) pblk %llu status 0x%x",
+		  MAJOR(__entry->dev), MINOR(__entry->dev),
+		  __entry->ino,
+		  __entry->lblk, __entry->len,
+		  __entry->pblk, __entry->status,
+		  __entry->conflict_lblk, __entry->conflict_len,
+		  __entry->conflict_pblk, __entry->conflict_status)
+);
+
 TRACE_EVENT(ext4_es_remove_extent,
 	TP_PROTO(struct inode *inode, ext4_lblk_t lblk, ext4_lblk_t len),
 
@@ -3369,6 +3412,152 @@ TRACE_EVENT(ext4_block_zero_eof,
 		  __entry->i_disksize, __entry->did_zero, __entry->zero_written)
 );
 
+DECLARE_EVENT_CLASS(ext4_split_convert,
+	TP_PROTO(struct inode *inode, struct ext4_map_blocks *map,
+		 ext4_lblk_t ee_block, unsigned int ee_len,
+		 int flags, bool ee_unwrit),
+
+	TP_ARGS(inode, map, ee_block, ee_len, flags, ee_unwrit),
+
+	TP_STRUCT__entry(
+		__field(	dev_t,		dev		)
+		__field(	u64,		ino		)
+		__field(	ext4_lblk_t,	m_lblk		)
+		__field(	unsigned int,	m_len		)
+		__field(	ext4_lblk_t,	ee_block	)
+		__field(	unsigned int,	ee_len		)
+		__field(	int,		flags		)
+		__field(	bool,		ee_unwrit	)
+	),
+
+	TP_fast_assign(
+		__entry->dev		= inode->i_sb->s_dev;
+		__entry->ino		= inode->i_ino;
+		__entry->m_lblk		= map->m_lblk;
+		__entry->m_len		= map->m_len;
+		__entry->ee_block	= ee_block;
+		__entry->ee_len		= ee_len;
+		__entry->flags		= flags;
+		__entry->ee_unwrit	= ee_unwrit;
+	),
+
+	TP_printk("dev %d,%d ino %llu m_lblk %u m_len %u ee_block %u ee_len %u ee_unwrit %d flags %s",
+		  MAJOR(__entry->dev), MINOR(__entry->dev),
+		  __entry->ino,
+		  __entry->m_lblk, __entry->m_len,
+		  __entry->ee_block, __entry->ee_len,
+		  __entry->ee_unwrit,
+		  show_map_flags(__entry->flags))
+);
+
+DEFINE_EVENT(ext4_split_convert, ext4_split_convert_enter,
+	TP_PROTO(struct inode *inode, struct ext4_map_blocks *map,
+		 ext4_lblk_t ee_block, unsigned int ee_len,
+		 int flags, bool ee_unwrit),
+	TP_ARGS(inode, map, ee_block, ee_len, flags, ee_unwrit)
+);
+
+DEFINE_EVENT(ext4_split_convert, ext4_extent_split,
+	TP_PROTO(struct inode *inode, struct ext4_map_blocks *map,
+		 ext4_lblk_t ee_block, unsigned int ee_len,
+		 int flags, bool ee_unwrit),
+	TP_ARGS(inode, map, ee_block, ee_len, flags, ee_unwrit)
+);
+
+TRACE_EVENT(ext4_extent_zeroout,
+	TP_PROTO(struct inode *inode, ext4_lblk_t ee_block,
+		 unsigned int ee_len, ext4_fsblk_t ee_pblk, bool ee_unwrit,
+		 ext4_lblk_t zero_lblk, ext4_fsblk_t zero_pblk,
+		 unsigned int zero_len, int err),
+
+	TP_ARGS(inode, ee_block, ee_len, ee_pblk, ee_unwrit, zero_lblk,
+		zero_pblk, zero_len, err),
+
+	TP_STRUCT__entry(
+		__field(	dev_t,		dev		)
+		__field(	u64,		ino		)
+		__field(	ext4_lblk_t,	ee_block	)
+		__field(	unsigned int,	ee_len		)
+		__field(	ext4_fsblk_t,	ee_pblk		)
+		__field(	bool,		ee_unwrit	)
+		__field(	ext4_lblk_t,	zero_lblk	)
+		__field(	ext4_fsblk_t,	zero_pblk	)
+		__field(	unsigned int,	zero_len	)
+		__field(	int,		err		)
+	),
+
+	TP_fast_assign(
+		__entry->dev		= inode->i_sb->s_dev;
+		__entry->ino		= inode->i_ino;
+		__entry->ee_block	= ee_block;
+		__entry->ee_len		= ee_len;
+		__entry->ee_pblk	= ee_pblk;
+		__entry->ee_unwrit	= ee_unwrit;
+		__entry->zero_lblk	= zero_lblk;
+		__entry->zero_pblk	= zero_pblk;
+		__entry->zero_len	= zero_len;
+		__entry->err		= err;
+	),
+
+	TP_printk("dev %d,%d ino %llu ee_block %u ee_len %u ee_pblk %llu ee_unwrit %d zero_lblk %u zero_len %u zero_pblk %llu err %d",
+		  MAJOR(__entry->dev), MINOR(__entry->dev),
+		  __entry->ino,
+		  __entry->ee_block, __entry->ee_len,
+		  (unsigned long long) __entry->ee_pblk,
+		  __entry->ee_unwrit,
+		  __entry->zero_lblk, __entry->zero_len,
+		  (unsigned long long) __entry->zero_pblk,
+		  __entry->err)
+);
+
+TRACE_EVENT(ext4_extent_convert,
+	TP_PROTO(struct inode *inode, struct ext4_map_blocks *map,
+		 ext4_lblk_t old_ee_block, unsigned int old_ee_len,
+		 bool old_unwrit, ext4_lblk_t new_ee_block,
+		 unsigned int new_ee_len, bool new_unwrit, int flags),
+
+	TP_ARGS(inode, map, old_ee_block, old_ee_len, old_unwrit,
+		new_ee_block, new_ee_len, new_unwrit, flags),
+
+	TP_STRUCT__entry(
+		__field(	dev_t,		dev		)
+		__field(	u64,		ino		)
+		__field(	ext4_lblk_t,	m_lblk		)
+		__field(	unsigned int,	m_len		)
+		__field(	ext4_lblk_t,	old_ee_block	)
+		__field(	unsigned int,	old_ee_len	)
+		__field(	bool,		old_unwrit	)
+		__field(	ext4_lblk_t,	new_ee_block	)
+		__field(	unsigned int,	new_ee_len	)
+		__field(	bool,		new_unwrit	)
+		__field(	int,		flags		)
+	),
+
+	TP_fast_assign(
+		__entry->dev		= inode->i_sb->s_dev;
+		__entry->ino		= inode->i_ino;
+		__entry->m_lblk		= map->m_lblk;
+		__entry->m_len		= map->m_len;
+		__entry->old_ee_block	= old_ee_block;
+		__entry->old_ee_len	= old_ee_len;
+		__entry->old_unwrit	= old_unwrit;
+		__entry->new_ee_block	= new_ee_block;
+		__entry->new_ee_len	= new_ee_len;
+		__entry->new_unwrit	= new_unwrit;
+		__entry->flags		= flags;
+	),
+
+	TP_printk("dev %d,%d ino %llu m_lblk %u m_len %u old [%u/%u) unwrit %d -> new [%u/%u) unwrit %d flags %s",
+		  MAJOR(__entry->dev), MINOR(__entry->dev),
+		  __entry->ino,
+		  __entry->m_lblk, __entry->m_len,
+		  __entry->old_ee_block, __entry->old_ee_len,
+		  __entry->old_unwrit,
+		  __entry->new_ee_block, __entry->new_ee_len,
+		  __entry->new_unwrit,
+		  show_map_flags(__entry->flags))
+);
+
 #endif /* _TRACE_EXT4_H */
 
 /* This part must be outside protection */

-- 
2.55.0


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

* [PATCH v2 11/13] ext4: update extent status cache when zeroing extents
  2026-11-27 15:10 [PATCH v2 00/13] ext4: fix endio zeroout corruption by tracking to-be-converted unwritten IOs Ojaswin Mujoo
                   ` (9 preceding siblings ...)
  2026-11-27 15:10 ` [PATCH v2 10/13] ext4: add tracepoints for extent split and convert Ojaswin Mujoo
@ 2026-11-27 15:10 ` Ojaswin Mujoo
  2026-11-27 15:10 ` [PATCH v2 12/13] ext4: don't drop the extents in case extent split fails Ojaswin Mujoo
  2026-11-27 15:11 ` [PATCH v2 13/13] ext4: re-enable zeroout fallback Ojaswin Mujoo
  12 siblings, 0 replies; 18+ messages in thread
From: Ojaswin Mujoo @ 2026-11-27 15:10 UTC (permalink / raw)
  To: Theodore Ts'o, Andreas Dilger, Baokun Li, Jan Kara,
	Ritesh Harjani (IBM), Zhang Yi
  Cc: linux-ext4, linux-kernel

In case of zeroout fallback, make sure to mark the zeroed ranges as written
in the extent status tree. Be careful of only doing so once we are sure the
zeroout has succeeded and we have converted the extent to written.

Signed-off-by: Ojaswin Mujoo <ojaswin@linux.ibm.com>
---
 fs/ext4/extents.c | 26 +++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)

diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c
index 0f5d9f3c42a9..e8ebc518e58b 100644
--- a/fs/ext4/extents.c
+++ b/fs/ext4/extents.c
@@ -3341,8 +3341,15 @@ static int ext4_split_extent_zeroout(handle_t *handle, struct inode *inode,
 	ext4_lblk_t ee_block;
 	uint64_t lblk, pblk, len;
 	int is_unwrit;
-	int err = 0;
+	int err = 0, i, nr_zeroed = 0;
 	loff_t map_end, ex_end;
+	/*
+	 * zeroed[] is used to record ranges successfully zeroed so only these
+	 * can be added to the cache
+	 */
+	struct {
+		uint64_t lblk, len, pblk;
+	} zeroed[2];
 
 	depth = ext_depth(inode);
 	ex = path[depth].p_ext;
@@ -3397,6 +3404,10 @@ static int ext4_split_extent_zeroout(handle_t *handle, struct inode *inode,
 			if (err)
 				/* ZEROOUT failed, just return original error */
 				return err;
+
+			zeroed[nr_zeroed].lblk = lblk;
+			zeroed[nr_zeroed].len = len;
+			zeroed[nr_zeroed++].pblk = pblk;
 		}
 
 		/* zeroout right */
@@ -3413,6 +3424,10 @@ static int ext4_split_extent_zeroout(handle_t *handle, struct inode *inode,
 			if (err)
 				/* ZEROOUT failed, just return original error */
 				return err;
+
+			zeroed[nr_zeroed].lblk = lblk;
+			zeroed[nr_zeroed].len = len;
+			zeroed[nr_zeroed++].pblk = pblk;
 		}
 	} else if (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN) {
 		/*
@@ -3435,6 +3450,10 @@ static int ext4_split_extent_zeroout(handle_t *handle, struct inode *inode,
 		if (err)
 			/* ZEROOUT failed, just return original error */
 			return err;
+
+		zeroed[nr_zeroed].lblk = lblk;
+		zeroed[nr_zeroed].len = len;
+		zeroed[nr_zeroed++].pblk = pblk;
 	} else {
 		/*
 		 * We no longer perform unwritten to unwritten splits in IO paths.
@@ -3450,6 +3469,11 @@ static int ext4_split_extent_zeroout(handle_t *handle, struct inode *inode,
 
 	ext4_ext_mark_initialized(ex);
 
+	for (i = 0; i < nr_zeroed; i++)
+		ext4_es_insert_extent(inode, zeroed[i].lblk, zeroed[i].len,
+				      zeroed[i].pblk, EXTENT_STATUS_WRITTEN,
+				      false);
+
 	err = ext4_ext_dirty(handle, inode, path + depth);
 	if (err)
 		return err;

-- 
2.55.0


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

* [PATCH v2 12/13] ext4: don't drop the extents in case extent split fails
  2026-11-27 15:10 [PATCH v2 00/13] ext4: fix endio zeroout corruption by tracking to-be-converted unwritten IOs Ojaswin Mujoo
                   ` (10 preceding siblings ...)
  2026-11-27 15:10 ` [PATCH v2 11/13] ext4: update extent status cache when zeroing extents Ojaswin Mujoo
@ 2026-11-27 15:10 ` Ojaswin Mujoo
  2026-11-27 15:11 ` [PATCH v2 13/13] ext4: re-enable zeroout fallback Ojaswin Mujoo
  12 siblings, 0 replies; 18+ messages in thread
From: Ojaswin Mujoo @ 2026-11-27 15:10 UTC (permalink / raw)
  To: Theodore Ts'o, Andreas Dilger, Baokun Li, Jan Kara,
	Ritesh Harjani (IBM), Zhang Yi
  Cc: linux-ext4, linux-kernel

This was originally added in [1] to avoid leaving stale extents in
cache when the zeroout operation failed. Now, with the recent refactoring
and fixes, this issue no longer exists as the callers handle the split
error correctly and make sure to update the es cache once zeroout and
convertsion is successfully complete.

In fact, we NEED the extent status cache here to exist because we don't
want to drop the CONVERT_PENDING extent entries, as they are used in making
zeroout decision.

Hence, don't drop the extent status cache if we run into an error while
splitting

[1] 6d882ea3b093 ("ext4: drop extent cache after doing PARTIAL_VALID1 zeroout")

Signed-off-by: Ojaswin Mujoo <ojaswin@linux.ibm.com>
---
 fs/ext4/extents.c | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c
index e8ebc518e58b..5130416aa8b4 100644
--- a/fs/ext4/extents.c
+++ b/fs/ext4/extents.c
@@ -3325,9 +3325,6 @@ static struct ext4_ext_path *ext4_split_extent_at(handle_t *handle,
 		path = err ? ERR_PTR(err) : ERR_PTR(insert_err);
 	}
 out_path:
-	if (IS_ERR(path))
-		/* Remove all remaining potentially stale extents. */
-		ext4_es_remove_extent(inode, ee_block, ee_len);
 	ext4_ext_show_leaf(inode, path);
 	return path;
 }

-- 
2.55.0


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

* [PATCH v2 13/13] ext4: re-enable zeroout fallback
  2026-11-27 15:10 [PATCH v2 00/13] ext4: fix endio zeroout corruption by tracking to-be-converted unwritten IOs Ojaswin Mujoo
                   ` (11 preceding siblings ...)
  2026-11-27 15:10 ` [PATCH v2 12/13] ext4: don't drop the extents in case extent split fails Ojaswin Mujoo
@ 2026-11-27 15:11 ` Ojaswin Mujoo
  12 siblings, 0 replies; 18+ messages in thread
From: Ojaswin Mujoo @ 2026-11-27 15:11 UTC (permalink / raw)
  To: Theodore Ts'o, Andreas Dilger, Baokun Li, Jan Kara,
	Ritesh Harjani (IBM), Zhang Yi
  Cc: linux-ext4, linux-kernel

Now that we have fixed the data corruption and cache consistency bugs
around zeroout fallback, lets re-enable the feature.

Signed-off-by: Ojaswin Mujoo <ojaswin@linux.ibm.com>
---
 fs/ext4/extents.c | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c
index 5130416aa8b4..df3085f2fc44 100644
--- a/fs/ext4/extents.c
+++ b/fs/ext4/extents.c
@@ -3567,9 +3567,6 @@ static struct ext4_ext_path *ext4_split_extent(handle_t *handle,
 	if (orig_err != -ENOSPC && orig_err != -EDQUOT && orig_err != -ENOMEM)
 		goto out_orig_err;
 
-	/* zeroout fallback disabled: always return the original err */
-	goto out_orig_err;
-
 	/* we can't zeroout? just return the original err */
 	if (!(split_flag & EXT4_EXT_MAY_ZEROOUT))
 		goto out_orig_err;

-- 
2.55.0


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

* Re: [PATCH v2 02/13] ext4: introduce CONVERT_PENDING extent status type
  2026-11-27 15:10 ` [PATCH v2 02/13] ext4: introduce CONVERT_PENDING extent status type Ojaswin Mujoo
@ 2026-11-27 15:58   ` sashiko-bot
  0 siblings, 0 replies; 18+ messages in thread
From: sashiko-bot @ 2026-11-27 15:58 UTC (permalink / raw)
  To: Ojaswin Mujoo; +Cc: linux-ext4, tytso

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] ext4: double reservation update on ENOMEM retry in
  ext4_es_insert_extent()
--

commit 574b0ce6804297f971eb1e0a8136bb02463b4f9a
Author: Ojaswin Mujoo <ojaswin@linux.ibm.com>

ext4: introduce CONVERT_PENDING extent status type

This commit adds the EXTENT_STATUS_CONVERT_PENDING extent status type to
track unwritten extents undergoing disk I/O before endio converts them to
written.

> diff --git a/fs/ext4/extents_status.c b/fs/ext4/extents_status.c
> index b7fb8502f9d46..28528f19be1df 100644
> --- a/fs/ext4/extents_status.c
> +++ b/fs/ext4/extents_status.c
> @@ -529,6 +529,9 @@ static inline bool ext4_es_must_keep(struct extent_status *es)
>  	if (ext4_es_is_delayed(es))
>  		return true;
>  
> +	if (ext4_es_is_convert_pending(es))
> +		return true;
> +
>  	return false;
>  }

[Severity: Medium]
Can making EXTENT_STATUS_CONVERT_PENDING a must-keep extent cause
ext4_es_insert_extent() to call ext4_da_update_reserve_space() twice with
the same resv_used count on an -ENOMEM retry?

Previously, ext4_es_must_keep() only returned true for delayed extents,
which are not inserted through ext4_es_insert_extent(), so err2 == -ENOMEM
was always cleared to 0.  Now, when ext4_es_insert_extent() replaces a
delayed extent with a convert-pending extent, __es_remove_extent() removes
the delayed extent and sets resv_used > 0.  If __es_insert_extent() then
fails its initial GFP_ATOMIC allocation with -ENOMEM, execution jumps to
error: and updates the reservation before retrying:

fs/ext4/extents_status.c:ext4_es_insert_extent() {
	...
	err1 = __es_remove_extent(inode, lblk, end, 0, &resv_used, NULL, es1);
	if (err1 != 0)
		goto error;
	...
	err2 = __es_insert_extent(inode, &newes, es2);
	if (err2 == -ENOMEM && !ext4_es_must_keep(&newes))
		err2 = 0;
	if (err2 != 0)
		goto error;
	...
error:
	write_unlock(&EXT4_I(inode)->i_es_lock);
	...
	resv_used += pending;
	if (resv_used)
		ext4_da_update_reserve_space(inode, resv_used,
					     delalloc_reserve_used);

	if (err1 || err2 || err3 < 0)
		goto retry;
}

On the retry pass, no extents remain in [lblk, end], so
__es_remove_extent() returns 0 early without writing *reserved, leaving
resv_used at its value from the first pass.  After __es_insert_extent()
succeeds with the preallocated es2, ext4_da_update_reserve_space() runs a
second time with the stale resv_used, underflowing i_reserved_data_blocks
(triggering WARN_ON(1)) and double-claiming or double-releasing quota.

Resetting resv_used = 0 (and pending = 0) before goto retry would avoid
applying the reservation update twice.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261127-ext4-endio-zero-bug-v2-0-4106968e4d15@linux.ibm.com?part=2

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

* Re: [PATCH v2 04/13] ext4: add es helper to convert unwrit to convert pending without changing i_es_seq
  2026-11-27 15:10 ` [PATCH v2 04/13] ext4: add es helper to convert unwrit to convert pending without changing i_es_seq Ojaswin Mujoo
@ 2026-11-27 16:46   ` sashiko-bot
  0 siblings, 0 replies; 18+ messages in thread
From: sashiko-bot @ 2026-11-27 16:46 UTC (permalink / raw)
  To: Ojaswin Mujoo; +Cc: linux-ext4, tytso

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] ext4: unused static ext4_map_mark_convert_pending() breaks
  build at this commit
--

commit 80a3192297d6fe076d14d5912fca2f3312352549
Author: Ojaswin Mujoo <ojaswin@linux.ibm.com>

ext4: add es helper to convert unwrit to convert pending without changing i_es_seq

This commit adds helpers to mark an unwritten extent as
EXTENT_STATUS_CONVERT_PENDING in the extent status tree without
incrementing i_es_seq.

> diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
> index 9368a68db2894..f7fc5fb7e8197 100644
> --- a/fs/ext4/inode.c
> +++ b/fs/ext4/inode.c
> @@ -757,6 +757,29 @@ int ext4_map_create_blocks(handle_t *handle, struct inode *inode,
>  	return retval;
>  }
>  
> +/*
> + * Mark the unwritten range in @map as convert pending in the extent status
> + * tree. This is used by the zeroout code to ensure we don't write zeroes on
> + * such unwritten ranges and hence must be serialized against endio conversion /
> + * extent zeroout, which hold i_data_sem excl.
> + *
> + * Returns false if the extent status tree changed since @map was looked up, in
> + * which case the caller must redo the lookup.
> + */
> +static bool ext4_map_mark_convert_pending(struct inode *inode,
> +					  struct ext4_map_blocks *map)
> +{
> +	bool ok;
> +
> +	down_read(&EXT4_I(inode)->i_data_sem);
> +	ok = map->m_seq == READ_ONCE(EXT4_I(inode)->i_es_seq);
> +	if (ok)
> +		ext4_es_mark_convert_pending(inode, map->m_lblk, map->m_len,
> +					     map->m_pblk);
> +	up_read(&EXT4_I(inode)->i_data_sem);
> +	return ok;
> +}

[Severity: Medium]
Does defining ext4_map_mark_convert_pending() here without callers or
__maybe_unused break the build at this commit?  Its callers in
ext4_map_blocks() are not added until commit "ext4: add support to track
convert pending ranges in ext4_map_blocks()", so compiling at this point
in the series triggers a -Wunused-function warning (and a build failure
with CONFIG_WERROR=y), breaking bisection.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261127-ext4-endio-zero-bug-v2-0-4106968e4d15@linux.ibm.com?part=4

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

* Re: [PATCH v2 05/13] ext4: add support to track convert pending ranges in ext4_map_blocks()
  2026-11-27 15:10 ` [PATCH v2 05/13] ext4: add support to track convert pending ranges in ext4_map_blocks() Ojaswin Mujoo
@ 2026-11-27 17:19   ` sashiko-bot
  0 siblings, 0 replies; 18+ messages in thread
From: sashiko-bot @ 2026-11-27 17:19 UTC (permalink / raw)
  To: Ojaswin Mujoo; +Cc: linux-ext4, tytso

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] ext4: double delalloc reservation update on retry in
  __ext4_es_insert_extent()
- [Medium] ext4: ext4_map_blocks() clamping on CONVERT_PENDING splits
  ioend and atomic IO
--

commit f21d7f26169cb4b1b89944929ad98cc0819cca34
Author: Ojaswin Mujoo <ojaswin@linux.ibm.com>

ext4: add support to track convert pending ranges in ext4_map_blocks()

This commit adds the EXT4_GET_BLOCKS_CONVERT_PENDING flag so that
ext4_map_blocks() and ext4_map_create_blocks() can mark unwritten ranges
in the extent status tree as EXTENT_STATUS_CONVERT_PENDING before I/O
submission.

> diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
> index f7fc5fb7e8197..3e4c0e1396ab1 100644
> --- a/fs/ext4/inode.c
> +++ b/fs/ext4/inode.c
> @@ -750,6 +750,9 @@ int ext4_map_create_blocks(handle_t *handle, struct inode *inode,
>  
>  	status = map->m_flags & EXT4_MAP_UNWRITTEN ?
>  			EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
> +	if (status == EXTENT_STATUS_UNWRITTEN &&
> +	    flags & EXT4_GET_BLOCKS_CONVERT_PENDING)
> +		status = EXTENT_STATUS_CONVERT_PENDING;
>  	ext4_es_insert_extent(inode, map->m_lblk, map->m_len, map->m_pblk,
>  			      status, flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE);
>  	map->m_seq = READ_ONCE(EXT4_I(inode)->i_es_seq);

[Severity: Medium]
Can passing EXTENT_STATUS_CONVERT_PENDING to ext4_es_insert_extent() cause
__ext4_es_insert_extent() to deduct delayed allocation reservations twice
when __es_insert_extent() retries after -ENOMEM?

Previously, ext4_es_insert_extent() only inserted WRITTEN or UNWRITTEN
extents, for which ext4_es_must_keep() is false, so an -ENOMEM from
__es_insert_extent() was cleared to 0.  Because ext4_es_must_keep() is
true for EXTENT_STATUS_CONVERT_PENDING, an initial GFP_ATOMIC failure in
__es_insert_extent() after __es_remove_extent() has already removed the
delayed extent (setting resv_used > 0) jumps to error:

fs/ext4/extents_status.c:__ext4_es_insert_extent() {
    ...
	err2 = __es_insert_extent(inode, &newes, es2);
	if (err2 == -ENOMEM && !ext4_es_must_keep(&newes))
		err2 = 0;
	if (err2 != 0)
		goto error;
    ...
error:
	write_unlock(&EXT4_I(inode)->i_es_lock);
	resv_used += pending;
	if (resv_used)
		ext4_da_update_reserve_space(inode, resv_used,
					     delalloc_reserve_used);

	if (err1 || err2 || err3 < 0)
		goto retry;
}

Because resv_used and pending are initialized outside the retry loop and
__es_remove_extent() returns early on the retry pass without modifying
resv_used when no extents remain in the range, the second pass calls
ext4_da_update_reserve_space() again with the same resv_used count.  This
underflows i_reserved_data_blocks, triggers a WARN_ON(1) in
ext4_da_update_reserve_space(), and corrupts delalloc block and quota
accounting.

> @@ -911,8 +914,17 @@ int ext4_map_blocks(handle_t *handle, struct inode *inode,
>  	}
>  
>  	/* If it is only a block(s) look up */
> -	if ((flags & EXT4_GET_BLOCKS_CREATE) == 0)
> +	if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
> +		if (map->m_flags & EXT4_MAP_UNWRITTEN &&
> +		    flags & EXT4_GET_BLOCKS_CONVERT_PENDING &&
> +		    !ext4_map_mark_convert_pending(inode, map)) {
> +			map->m_flags = 0;
> +			map->m_len = orig_mlen;
> +			goto create_retry;
> +		}
> +
>  		return retval;
> +	}

[Severity: Medium]
Does marking sub-ranges of an unwritten extent as
EXTENT_STATUS_CONVERT_PENDING fragment the extent status tree and clamp
later ext4_map_blocks() lookups across a contiguous on-disk unwritten
extent?

Because ext4_es_can_be_merged() refuses to merge any extent where
ext4_es_is_convert_pending() is true, each I/O submission over part of an
unwritten extent leaves a separate CONVERT_PENDING entry in i_es_tree, and
ext4_map_blocks() clamps map->m_len to a single es entry on cache hit.

When iomap merges completed ioends across a single on-disk unwritten
extent, ext4_convert_unwritten_extents() now has map.m_len clamped to each
unmerged CONVERT_PENDING entry, forcing ext4_split_convert_extents() to
repeatedly split and re-merge the on-disk extent on every sub-range.  In
addition, if an atomic write (ext4_map_blocks_atomic_write()) queries a
contiguous on-disk unwritten extent that contains a CONVERT_PENDING
sub-range in i_es_tree, ext4_map_blocks() returns ret < m_len,
misclassifying the extent as a mixed mapping and demoting the atomic write
to ext4_map_blocks_atomic_write_slow() (synchronous zeroout and
ext4_force_commit()).

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261127-ext4-endio-zero-bug-v2-0-4106968e4d15@linux.ibm.com?part=5

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

* Re: [PATCH v2 06/13] ext4: add tracking for unwritten to-be-converted ranges in dio write path
  2026-11-27 15:10 ` [PATCH v2 06/13] ext4: add tracking for unwritten to-be-converted ranges in dio write path Ojaswin Mujoo
@ 2026-11-27 17:48   ` sashiko-bot
  0 siblings, 0 replies; 18+ messages in thread
From: sashiko-bot @ 2026-11-27 17:48 UTC (permalink / raw)
  To: Ojaswin Mujoo; +Cc: linux-ext4, tytso

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] ext4: fragmented CONVERT_PENDING es entries break atomic DIO
  writes
--

commit ad9b3b49092f6b64f0a47182a02163ecc97b4e9e
Author: Ojaswin Mujoo <ojaswin@linux.ibm.com>

ext4: add tracking for unwritten to-be-converted ranges in dio write path

This commit passes EXT4_GET_BLOCKS_CONVERT_PENDING in the direct I/O write
mapping paths to track unwritten ranges in the extent status tree while
I/O is in flight, and adds ext4_clear_convert_pending().

> diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
> index 3e4c0e1396ab1..4aafd0aaea699 100644
> --- a/fs/ext4/inode.c
> +++ b/fs/ext4/inode.c
[ ... ]
> @@ -4012,7 +4043,8 @@ static int ext4_iomap_alloc(struct inode *inode, struct ext4_map_blocks *map,
>  	else if (EXT4_LBLK_TO_B(inode, map->m_lblk) >= i_size_read(inode))
>  		m_flags = EXT4_GET_BLOCKS_CREATE;
>  	else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
> -		m_flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT;
> +		m_flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT |
> +			  EXT4_GET_BLOCKS_CONVERT_PENDING;
>  
>  	if (flags & IOMAP_ATOMIC)
>  		ret = ext4_map_blocks_atomic_write(handle, inode, map, m_flags,
> @@ -4082,7 +4114,10 @@ int ext4_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
>  		 * especially in multi-threaded overwrite requests.
>  		 */
>  		if (offset + length <= i_size_read(inode)) {
> -			ret = ext4_map_blocks(NULL, inode, &map, 0);
> +			ret = ext4_map_blocks(NULL, inode, &map,
> +					      (flags & IOMAP_DAX) ?
> +					      0 :
> +					      EXT4_GET_BLOCKS_CONVERT_PENDING);
>  			/*
>  			 * For DAX we convert extents to initialized ones before
>  			 * copying the data, otherwise we do it after I/O so

[Severity: High]
Can marking sub-ranges as EXTENT_STATUS_CONVERT_PENDING split a contiguous
on-disk unwritten extent in i_es_tree and break multi-block IOMAP_ATOMIC
writes?

ext4_es_can_be_merged() rejects merging whenever either entry is
convert-pending or the status types differ.  When a sub-range of a
contiguous unwritten extent is marked EXTENT_STATUS_CONVERT_PENDING (from
a concurrent sub-range DIO write, an earlier failed DIO write, or an
IOMAP_NOWAIT lookup that marks a partial unwritten prefix here before
failing with -EAGAIN in ext4_iomap_alloc()), i_es_tree splits the cached
extent into separate entries.

A subsequent multi-block IOMAP_ATOMIC write across that range hits only
the first entry in ext4_es_lookup_extent(), clamping map.m_len to a value
smaller than orig_mlen.  In ext4_iomap_begin(), ret == orig_mlen fails and
skips the fast path (causing RWF_ATOMIC | RWF_NOWAIT on a fully allocated
unwritten extent to fail with -EAGAIN in ext4_iomap_alloc()).

For blocking IOMAP_ATOMIC writes, ext4_map_blocks_atomic_write() also sees
ret < m_len, treats the contiguous on-disk unwritten extent as a mixed
mapping, forces a transaction commit, and falls back to
ext4_map_blocks_atomic_write_slow().  That slow path maps with
EXT4_GET_BLOCKS_CREATE_ZERO, which converts the unwritten blocks to
initialized and synchronously writes zeroes to disk via
ext4_issue_zeroout() before the atomic write BIO is submitted, overwriting
any concurrent in-flight DIO write on the CONVERT_PENDING range.

In addition, if concurrent sub-range DIO lookups split an in-flight atomic
write's unwritten range into more than two CONVERT_PENDING entries in
i_es_tree, the loop in ext4_convert_unwritten_extents_atomic() clamps each
ext4_map_blocks() call to a single ES fragment and performs more than the
two extent conversions for which it reserved journal credits.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261127-ext4-endio-zero-bug-v2-0-4106968e4d15@linux.ibm.com?part=6

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

end of thread, other threads:[~2026-11-27 17:48 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-27 15:10 [PATCH v2 00/13] ext4: fix endio zeroout corruption by tracking to-be-converted unwritten IOs Ojaswin Mujoo
2026-11-27 15:10 ` [PATCH v2 01/13] ext4: disable zeroout fallback in ext4_split_extent() Ojaswin Mujoo
2026-11-27 15:10 ` [PATCH v2 02/13] ext4: introduce CONVERT_PENDING extent status type Ojaswin Mujoo
2026-11-27 15:58   ` sashiko-bot
2026-11-27 15:10 ` [PATCH v2 03/13] ext4: preserve convert pending extents when adding unwrit extents to es cache Ojaswin Mujoo
2026-11-27 15:10 ` [PATCH v2 04/13] ext4: add es helper to convert unwrit to convert pending without changing i_es_seq Ojaswin Mujoo
2026-11-27 16:46   ` sashiko-bot
2026-11-27 15:10 ` [PATCH v2 05/13] ext4: add support to track convert pending ranges in ext4_map_blocks() Ojaswin Mujoo
2026-11-27 17:19   ` sashiko-bot
2026-11-27 15:10 ` [PATCH v2 06/13] ext4: add tracking for unwritten to-be-converted ranges in dio write path Ojaswin Mujoo
2026-11-27 17:48   ` sashiko-bot
2026-11-27 15:10 ` [PATCH v2 07/13] ext4: add tracking for unwritten to-be-converted ranges in non iomap buffered path Ojaswin Mujoo
2026-11-27 15:10 ` [PATCH v2 08/13] ext4: add tracking for unwritten to-be-converted ranges in " Ojaswin Mujoo
2026-11-27 15:10 ` [PATCH v2 09/13] ext4: don't zeroout if conv pending ranges are present Ojaswin Mujoo
2026-11-27 15:10 ` [PATCH v2 10/13] ext4: add tracepoints for extent split and convert Ojaswin Mujoo
2026-11-27 15:10 ` [PATCH v2 11/13] ext4: update extent status cache when zeroing extents Ojaswin Mujoo
2026-11-27 15:10 ` [PATCH v2 12/13] ext4: don't drop the extents in case extent split fails Ojaswin Mujoo
2026-11-27 15:11 ` [PATCH v2 13/13] ext4: re-enable zeroout fallback Ojaswin Mujoo

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