[PATCH v2 08/13] ext4: add tracking for unwritten to-be-converted ranges in iomap buffered path
From: Ojaswin Mujoo
Date: Fri Nov 27 2026 - 10:12:05 EST
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@xxxxxxxxxxxxx>
---
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