[PATCH v3 2/2] ext4: punch hole beyond i_size

From: Ding Feng

Date: Thu Dec 03 2026 - 08:06:54 EST


ext4_punch_hole() returns early when the hole starts at or past i_size,
and otherwise ends it at the block that holds i_size. Blocks that
fallocate(FALLOC_FL_KEEP_SIZE) allocated past EOF can therefore never be
punched out; only a truncate frees them. XFS and tmpfs remove them.
fallocate(2) says that "within the specified range, partial filesystem
blocks are zeroed, and whole filesystem blocks are removed
from the file".

A 4096-byte file with 1 MiB allocated by FALLOC_FL_KEEP_SIZE, then
PUNCH_HOLE|KEEP_SIZE over [4096, 1M), on v7.4-rc5 (x86_64, QEMU):

ext4 st_blocks 2048 -> 2048
xfs st_blocks 2048 -> 8
tmpfs st_blocks 2048 -> 8

Hugh Dickins reported this in 2012 [1]. Allison Henderson's fix removed
the early return [2], and Lukas Czerner carried it in a later series,
but it was held back: the LSF 2012 discussion concluded to wait for range
locks rather than take i_mutex for a punch beyond i_size [3]. Today
ext4_punch_hole() always runs with the inode lock held.

Remove extents up to the end of the range. A hole that reaches i_size
still covers the whole block that holds i_size, as before, so that block
is freed rather than zeroed. Zero partial blocks only for a hole that
starts below i_size, and not past the block that holds i_size, so
nothing past EOF is written.

[1] https://lists.openwall.net/linux-ext4/2012/01/12/3
[2] https://lists.openwall.net/linux-ext4/2012/01/14/7
[3] https://lists.openwall.net/linux-ext4/2012/05/16/3

Reported-by: Hugh Dickins <hughd@xxxxxxxxxx>
Fixes: 2be4751b21ae ("ext4: fix 2nd xfstests 127 punch hole failure")
Signed-off-by: Ding Feng <dingf3ng@xxxxxxxxx>
---
fs/ext4/inode.c | 25 ++++++++++++++++---------
1 file changed, 16 insertions(+), 9 deletions(-)

diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index 26cf00ff8..f7d0a2e99 100644
--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -5356,6 +5356,7 @@ int ext4_punch_hole(struct file *file, loff_t offset, loff_t length)
ext4_lblk_t start_lblk, end_lblk;
loff_t max_end = sb->s_maxbytes;
loff_t end = offset + length;
+ loff_t zero_end;
handle_t *handle;
unsigned int credits;
unsigned int partial_zeroed = 0;
@@ -5371,19 +5372,22 @@ int ext4_punch_hole(struct file *file, loff_t offset, loff_t length)
if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
max_end = EXT4_SB(sb)->s_bitmap_maxbytes - sb->s_blocksize;

- /* No need to punch hole beyond i_size */
- if (offset >= inode->i_size || offset >= max_end)
+ if (offset >= max_end)
return 0;
-
/*
- * If the hole extends beyond i_size, set the hole to end after
- * the block that contains i_size to save pointless tail block zeroing.
+ * A hole that reaches i_size covers the rest of the block that holds
+ * i_size, so that block is freed rather than zeroed.
*/
if (end >= inode->i_size)
- end = round_up(inode->i_size, sb->s_blocksize);
+ end = max_t(loff_t, end, round_up(inode->i_size, sb->s_blocksize));
if (end > max_end)
end = max_end;
length = end - offset;
+ /*
+ * Blocks past i_size (from FALLOC_FL_KEEP_SIZE) are removed too, but
+ * nothing after the block that contains i_size needs zeroing.
+ */
+ zero_end = min_t(loff_t, end, round_up(inode->i_size, sb->s_blocksize));

ret = ext4_update_disksize_before_punch(inode, offset, length);
if (ret)
@@ -5394,9 +5398,12 @@ int ext4_punch_hole(struct file *file, loff_t offset, loff_t length)
if (ret)
return ret;

- ret = ext4_zero_partial_blocks(inode, offset, length, &partial_zeroed);
- if (ret)
- return ret;
+ if (offset < inode->i_size) {
+ ret = ext4_zero_partial_blocks(inode, offset, zero_end - offset,
+ &partial_zeroed);
+ if (ret)
+ return ret;
+ }
/*
* On the iomap path, partial invalidate of a folio without an ifs
* leaves sub-block dirty bits uncleared. Drain writeback before
--
2.39.5