[PATCH v2 01/11] iomap: punch out a run of clean blocks in one call

From: Ernest Borowski via B4 Relay

Date: Tue Nov 24 2026 - 17:50:00 EST


From: Ernest Borowski <ernest.borowski.git@xxxxxxxxx>

iomap_write_delalloc_ifs_punch() calls ->punch() once for every clean
block of a dirty folio. A file system that reserves delayed allocation
space in units larger than a block can release a unit only when one call
covers it whole. ntfs reserves whole clusters and uses 512 byte blocks,
so with 1 KiB clusters a buffered write into a hole that fails after
zeroing the first cluster of a 4 KiB folio leaves the other three
clusters of that folio reserved, and writeback later allocates them.

Pass each run of contiguous clean blocks to ->punch() in one call. XFS
and ext4 punch any range they are given, so for them this only means
fewer calls.

"ntfs: punch out the delalloc reservation of a failed buffered write"
later in this series needs this, so it goes to stable with it.

Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Ernest Borowski <ernest.borowski.git@xxxxxxxxx>
---
fs/iomap/buffered-io.c | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/fs/iomap/buffered-io.c b/fs/iomap/buffered-io.c
index 6d843b168d..2eb8070e34 100644
--- a/fs/iomap/buffered-io.c
+++ b/fs/iomap/buffered-io.c
@@ -1364,6 +1364,9 @@ static void iomap_write_delalloc_ifs_punch(struct inode *inode,
* blocks within a folio which are marked uptodate
* but not dirty. In that case it is necessary to punch
* out such blocks to avoid leaking any delalloc blocks.
+ * Pass each run of clean blocks in one call: a file system
+ * that reserves space in units larger than a block can only
+ * release a unit that it sees whole.
*/
ifs = folio->private;
if (!ifs)
@@ -1374,9 +1377,12 @@ static void iomap_write_delalloc_ifs_punch(struct inode *inode,
last_blk = offset_in_folio(folio, last_byte) >> blkbits;
while ((first_blk = ifs_next_clean_block(folio, first_blk, last_blk))
<= last_blk) {
+ unsigned int next_blk = ifs_next_dirty_block(folio,
+ first_blk + 1, last_blk);
+
punch(inode, folio_pos(folio) + (first_blk << blkbits),
- 1 << blkbits, iomap);
- first_blk++;
+ (loff_t)(next_blk - first_blk) << blkbits, iomap);
+ first_blk = next_blk;
}
}


--
2.56.1