* [PATCH v2 1/2] ext4: fix the fast commit range of a punched hole
2026-12-03 11:51 [PATCH v2 0/2] ext4: punch hole beyond i_size Ding Feng
@ 2026-12-03 11:52 ` Ding Feng
2026-12-03 11:52 ` [PATCH v2 2/2] ext4: punch hole beyond i_size Ding Feng
2026-12-03 12:22 ` [PATCH v2 0/2] " sashiko-bot
2 siblings, 0 replies; 5+ messages in thread
From: Ding Feng @ 2026-12-03 11:52 UTC (permalink / raw)
To: tytso, adilger.kernel
Cc: jack, libaokun, ojaswin, ritesh.list, yi.zhang, hughd, david,
linux-ext4, linux-kernel
ext4_fc_track_range() takes an inclusive end block: __track_range()
records end - start + 1 blocks. ext4_punch_hole() passes end_lblk, the
first block after the hole, so fast commit tracks one block more than
the punch removed, and it is called even when the hole covers no whole
block.
The extra block also wraps the length: with huge_file, s_maxbytes is
0xffffffff blocks, so a hole from block 0 to s_maxbytes records a
length of 0, which ext4_fc_snapshot_inode_data() reads as no change to
the file's blocks. Today the hole stops at the block that holds i_size,
so only a file that large gets there; the next patch lets the hole run
to s_maxbytes on any file.
Track [start_lblk, end_lblk - 1], and only when blocks were removed.
Keep queueing the inode for every punch, which the call at the end did
for a hole within one block, by tracking the inode before the removal.
Fixes: aa75f4d3daae ("ext4: main fast-commit commit path")
Signed-off-by: Ding Feng <dingf3ng@gmail.com>
---
fs/ext4/inode.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index 07a62d856..26cf00ff8 100644
--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -5426,10 +5426,10 @@ int ext4_punch_hole(struct file *file, loff_t offset, loff_t length)
start_lblk = EXT4_B_TO_LBLK(inode, offset);
end_lblk = end >> inode->i_blkbits;
+ ext4_fc_track_inode(handle, inode);
if (end_lblk > start_lblk) {
ext4_lblk_t hole_len = end_lblk - start_lblk;
- ext4_fc_track_inode(handle, inode);
ext4_check_map_extents_env(inode);
down_write(&EXT4_I(inode)->i_data_sem);
ext4_discard_preallocations(inode);
@@ -5450,8 +5450,8 @@ int ext4_punch_hole(struct file *file, loff_t offset, loff_t length)
ext4_es_insert_extent(inode, start_lblk, hole_len, ~0,
EXTENT_STATUS_HOLE, 0);
up_write(&EXT4_I(inode)->i_data_sem);
+ ext4_fc_track_range(handle, inode, start_lblk, end_lblk - 1);
}
- ext4_fc_track_range(handle, inode, start_lblk, end_lblk);
ret = ext4_mark_inode_dirty(handle, inode);
if (unlikely(ret))
--
2.39.5
^ permalink raw reply related [flat|nested] 5+ messages in thread* [PATCH v2 2/2] ext4: punch hole beyond i_size
2026-12-03 11:51 [PATCH v2 0/2] ext4: punch hole beyond i_size Ding Feng
2026-12-03 11:52 ` [PATCH v2 1/2] ext4: fix the fast commit range of a punched hole Ding Feng
@ 2026-12-03 11:52 ` Ding Feng
2026-12-03 12:22 ` sashiko-bot
2026-12-03 12:22 ` [PATCH v2 0/2] " sashiko-bot
2 siblings, 1 reply; 5+ messages in thread
From: Ding Feng @ 2026-12-03 11:52 UTC (permalink / raw)
To: tytso, adilger.kernel
Cc: jack, libaokun, ojaswin, ritesh.list, yi.zhang, hughd, david,
linux-ext4, linux-kernel
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. 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@google.com>
Fixes: 2be4751b21ae ("ext4: fix 2nd xfstests 127 punch hole failure")
Signed-off-by: Ding Feng <dingf3ng@gmail.com>
---
fs/ext4/inode.c | 25 +++++++++++++------------
1 file changed, 13 insertions(+), 12 deletions(-)
diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index 26cf00ff8..7f491a723 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,16 @@ 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.
- */
- if (end >= inode->i_size)
- 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 +5392,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
^ permalink raw reply related [flat|nested] 5+ messages in thread* Re: [PATCH v2 2/2] ext4: punch hole beyond i_size
2026-12-03 11:52 ` [PATCH v2 2/2] ext4: punch hole beyond i_size Ding Feng
@ 2026-12-03 12:22 ` sashiko-bot
0 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-12-03 12:22 UTC (permalink / raw)
To: Ding Feng; +Cc: linux-ext4, tytso
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] ext4: ext4_punch_hole() drops EOF block round-up when end
reaches i_size
--
commit fa94f38a0bf415aa375c8cc034c22fb0d79dfa23
Author: Ding Feng <dingf3ng@gmail.com>
ext4: punch hole beyond i_size
This commit updates ext4_punch_hole() to remove extents allocated beyond
i_size instead of returning early or stopping at the block containing
i_size, while restricting partial block zeroing to holes starting below
i_size.
> diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
> index 26cf00ff8630a..7f491a7237357 100644
> --- a/fs/ext4/inode.c
> +++ b/fs/ext4/inode.c
[ ... ]
> @@ -5371,19 +5372,16 @@ 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.
> - */
> - if (end >= inode->i_size)
> - 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 +5392,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;
> + }
[Severity: Medium]
Does removing the upward round-up of end regress EOF block reclamation and
reintroduce tail-block zeroing when end falls in
[i_size, round_up(i_size, sb->s_blocksize))?
The deleted check both clamped end downward and rounded it upward to the
end of the EOF block, which commit d37a7ddd3a38 ("ext4: make
ext4_punch_hole() support large block size") kept to free the EOF block
and avoid pointless tail-block zeroing.
With this change, punching [0, 5000) on a 5000-byte file with 4096-byte
blocks leaves both end and zero_end at 5000. ext4_zero_partial_blocks()
then reads, zeros, and dirties the EOF block, and end_lblk excludes that
block so it stays allocated on disk instead of being freed.
If the same file is punched over [0, 6000), zero_end stays at 6000 and
ext4_zero_partial_blocks() also writes zeroes over [5000, 6000) past
i_size while leaving the EOF block allocated.
Preserving the upward round-up when end >= inode->i_size with
end = max_t(loff_t, end, round_up(inode->i_size, sb->s_blocksize)) before
clamping against max_end would still punch blocks past i_size without
regressing EOF block reclamation or tail-block zeroing.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203115201.48810-1-dingf3ng@gmail.com?part=2
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2 0/2] ext4: punch hole beyond i_size
2026-12-03 11:51 [PATCH v2 0/2] ext4: punch hole beyond i_size Ding Feng
2026-12-03 11:52 ` [PATCH v2 1/2] ext4: fix the fast commit range of a punched hole Ding Feng
2026-12-03 11:52 ` [PATCH v2 2/2] ext4: punch hole beyond i_size Ding Feng
@ 2026-12-03 12:22 ` sashiko-bot
2 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-12-03 12:22 UTC (permalink / raw)
To: Ding Feng; +Cc: linux-ext4, tytso
Sashiko has finished reviewing this 2-patch series:
- Patch 1: no issues found.
- Patch 2 ("ext4: punch hole beyond i_size"): 1 potential issue (1
Medium)
A detailed review has been sent as a separate reply to patch 2.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203115201.48810-1-dingf3ng@gmail.com
^ permalink raw reply [flat|nested] 5+ messages in thread