[PATCH v2 09/13] ext4: don't zeroout if conv pending ranges are present
From: Ojaswin Mujoo
Date: Fri Nov 27 2026 - 10:12:13 EST
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@xxxxxxxxxxxxx>
---
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