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

From: Ernest Borowski via B4 Relay

Date: Fri Dec 04 2026 - 13:41:03 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 range of contiguous clean blocks to ->punch() in one call.
The clean blocks at the start of a dirty folio join the range that ends
there, and the clean blocks at the end of the part of the folio being
scanned are left to the next punch, which also covers what follows
them: the next folio, the gap to the next cached data, or the range past
the end of the scan. Otherwise a clean 1 KiB cluster that straddles the
end of the scan, which stops at the block that holds EOF, still goes to
->punch() in two parts and stays reserved. The blocks left over are
punched after their folio is unlocked, as the range past the end of the
scan always was. 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 | 82 +++++++++++++++++++++++++-------------------------
1 file changed, 41 insertions(+), 41 deletions(-)

diff --git a/fs/iomap/buffered-io.c b/fs/iomap/buffered-io.c
index 25915c0f5e..2b9bcff7ce 100644
--- a/fs/iomap/buffered-io.c
+++ b/fs/iomap/buffered-io.c
@@ -1350,58 +1350,58 @@ int iomap_fsverity_write(struct file *file, loff_t pos, size_t length,
}
EXPORT_SYMBOL_GPL(iomap_fsverity_write);

-static void iomap_write_delalloc_ifs_punch(struct inode *inode,
- struct folio *folio, loff_t start_byte, loff_t end_byte,
- struct iomap *iomap, iomap_punch_t punch)
-{
- unsigned int first_blk, last_blk;
- loff_t last_byte;
- u8 blkbits = inode->i_blkbits;
- struct iomap_folio_state *ifs;
-
- /*
- * When we have per-block dirty tracking, there can be
- * 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.
- */
- ifs = folio->private;
- if (!ifs)
- return;
-
- last_byte = min_t(loff_t, end_byte - 1, folio_next_pos(folio) - 1);
- first_blk = offset_in_folio(folio, start_byte) >> blkbits;
- last_blk = offset_in_folio(folio, last_byte) >> blkbits;
- while ((first_blk = ifs_next_clean_block(folio, first_blk, last_blk))
- <= last_blk) {
- punch(inode, folio_pos(folio) + (first_blk << blkbits),
- 1 << blkbits, iomap);
- first_blk++;
- }
-}
-
static void iomap_write_delalloc_punch(struct inode *inode, struct folio *folio,
loff_t *punch_start_byte, loff_t start_byte, loff_t end_byte,
struct iomap *iomap, iomap_punch_t punch)
{
+ loff_t range_end = min_t(loff_t, end_byte, folio_next_pos(folio));
+ unsigned int first_blk, last_blk, blk;
+ u8 blkbits = inode->i_blkbits;
+
if (!folio_test_dirty(folio))
return;

- /* if dirty, punch up to offset */
- if (start_byte > *punch_start_byte) {
- punch(inode, *punch_start_byte, start_byte - *punch_start_byte,
- iomap);
+ /* Without per-block state the whole folio is dirty. */
+ if (!folio->private) {
+ /* if dirty, punch up to offset */
+ if (start_byte > *punch_start_byte)
+ punch(inode, *punch_start_byte,
+ start_byte - *punch_start_byte, iomap);
+ /*
+ * Make sure the next punch start is correctly bound to
+ * the end of this data range, not the end of the folio.
+ */
+ *punch_start_byte = range_end;
+ return;
}

- /* Punch non-dirty blocks within folio */
- iomap_write_delalloc_ifs_punch(inode, folio, start_byte, end_byte,
- iomap, punch);
-
/*
- * Make sure the next punch start is correctly bound to
- * the end of this data range, not the end of the folio.
+ * When we have per-block dirty tracking, there can be 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.
+ *
+ * A file system that reserves space in units larger than a block can
+ * only release a unit that one ->punch() call covers whole, so pass
+ * every clean range in one call: the clean blocks before the first
+ * dirty block join the range that ends at start_byte, and clean blocks
+ * at the end of the range are not punched here but left to the next
+ * punch, which also covers what follows them. The next punch start
+ * stays bound to the end of this data range, not the end of the folio.
*/
- *punch_start_byte = min_t(loff_t, end_byte, folio_next_pos(folio));
+ first_blk = offset_in_folio(folio, start_byte) >> blkbits;
+ last_blk = offset_in_folio(folio, range_end - 1) >> blkbits;
+ while ((blk = ifs_next_dirty_block(folio, first_blk, last_blk)) <=
+ last_blk) {
+ loff_t pos = folio_pos(folio) + ((loff_t)blk << blkbits);
+
+ if (pos > *punch_start_byte)
+ punch(inode, *punch_start_byte,
+ pos - *punch_start_byte, iomap);
+ first_blk = ifs_next_clean_block(folio, blk + 1, last_blk);
+ *punch_start_byte = min_t(loff_t, range_end, folio_pos(folio) +
+ ((loff_t)first_blk << blkbits));
+ }
}

/*

--
2.56.1