* [PATCH v4 0/2] ext4: punch hole beyond i_size
@ 2026-12-03 14:15 Ding Feng
2026-12-03 14:15 ` [PATCH v4 1/2] ext4: fix the fast commit range of a punched hole Ding Feng
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Ding Feng @ 2026-12-03 14:15 UTC (permalink / raw)
To: tytso, adilger.kernel
Cc: jack, libaokun, ojaswin, ritesh.list, yi.zhang, hughd, david,
linux-ext4, linux-kernel
ext4 cannot punch out blocks that FALLOC_FL_KEEP_SIZE allocated past
EOF: ext4_punch_hole() returns early for a hole that starts at or past
i_size, and ends any other hole at the block that holds i_size. Patch 2
lifts that limit. Patch 1 fixes the range the punch records for fast
commit, which wraps to 0 once a hole can run to s_maxbytes.
Changes since v3 [1]:
- Patch 2: call ext4_update_disksize_before_punch() only for a hole that
starts below i_size (Sashiko [2]). Since v1, a punch at i_size moved
i_disksize up to i_size ahead of writeback: after a buffered 1 MiB
append, a punch at 1 MiB and a journal-only shutdown, v3 brought the
file back as 1 MiB of zeros; v4, like v7.4-rc5, brings it back empty.
- Patch 1: leave ext4_fc_track_inode() where it was. v2 and v3 moved it
out of "if (end_lblk > start_lblk)" so that a hole within one block
would still queue the inode, but ext4_mark_inode_dirty() queues it on
every path, so the move changed nothing.
- Patch 2: the changelog and a comment said that nothing past EOF is
written and that the EOF block is freed. A hole that starts inside
the EOF block zeroes it to its end and keeps it, as before; both now
say so. The KUnit case "Punch past i_size is a no-op" is reworded to
what it checks.
Changes since v2 [3]: patch 2 keeps rounding a hole that reaches i_size
up to the end of the EOF block, so the EOF block is still freed.
Changes since v1 [4]: patch 1 is new; with v1 on a fast_commit
filesystem, punching [0, s_maxbytes), fsync and a shutdown brought the
punched data back after replay. Patch 2 zeroes nothing for a hole that
starts past i_size and cites Fixes: 2be4751b21ae.
Tested on v7.4-rc5, x86_64 under QEMU:
- fstests -g punch on ext4 with 4k and 1k blocks: generic/042 fails with
and without the series, nothing else fails. On ext4 without extents
the 8 punch tests that run without falloc pass.
- generic/811, "generic: punch a hole past EOF over preallocated
blocks" [5], fails without the series and passes with it, on ext4 4k
and 1k; it passes on XFS.
- ext4 KUnit: 538 pass. W=1 adds no warnings.
- The i_disksize and fast commit crash checks above, a 5000-byte file
punched over [0, 5000) and [0, 6000) at 4k and 1k blocks (the EOF
block is freed), and punches past EOF on indirect-block files
(e2fsck clean).
- Not tested: bigalloc, block sizes above 4k, DAX.
[1] https://lore.kernel.org/all/20261203130608.64892-1-dingf3ng@gmail.com/
[2] https://lore.kernel.org/all/sashiko-outbox-247862@kernel.org/
[3] https://lore.kernel.org/all/20261203115201.48810-1-dingf3ng@gmail.com/
[4] https://lore.kernel.org/all/20261203091435.73786-1-dingf3ng@gmail.com/
[5] https://lore.kernel.org/all/20261203115229.50163-1-dingf3ng@gmail.com/
Ding Feng (2):
ext4: fix the fast commit range of a punched hole
ext4: punch hole beyond i_size
fs/ext4/inode-test.c | 2 +-
fs/ext4/inode.c | 35 ++++++++++++++++++++++-------------
2 files changed, 23 insertions(+), 14 deletions(-)
--
2.39.5
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v4 1/2] ext4: fix the fast commit range of a punched hole
2026-12-03 14:15 [PATCH v4 0/2] ext4: punch hole beyond i_size Ding Feng
@ 2026-12-03 14:15 ` Ding Feng
2026-12-03 14:15 ` [PATCH v4 2/2] ext4: punch hole beyond i_size Ding Feng
2026-12-03 15:07 ` [PATCH v4 0/2] " sashiko-bot
2 siblings, 0 replies; 4+ messages in thread
From: Ding Feng @ 2026-12-03 14:15 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.
Fixes: aa75f4d3daae ("ext4: main fast-commit commit path")
Signed-off-by: Ding Feng <dingf3ng@gmail.com>
---
fs/ext4/inode.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index 07a62d856..866c1d92d 100644
--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -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] 4+ messages in thread
* [PATCH v4 2/2] ext4: punch hole beyond i_size
2026-12-03 14:15 [PATCH v4 0/2] ext4: punch hole beyond i_size Ding Feng
2026-12-03 14:15 ` [PATCH v4 1/2] ext4: fix the fast commit range of a punched hole Ding Feng
@ 2026-12-03 14:15 ` Ding Feng
2026-12-03 15:07 ` [PATCH v4 0/2] " sashiko-bot
2 siblings, 0 replies; 4+ messages in thread
From: Ding Feng @ 2026-12-03 14:15 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. A hole that reaches i_size
still extends to the end of the block that holds i_size, as before, so
it frees that block unless it starts inside it. A hole that starts at or
past i_size neither zeroes partial blocks nor moves i_disksize; one that
starts below i_size zeroes partial blocks only up to the end of the
block that holds i_size.
[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-test.c | 2 +-
fs/ext4/inode.c | 33 +++++++++++++++++++++------------
2 files changed, 22 insertions(+), 13 deletions(-)
diff --git a/fs/ext4/inode-test.c b/fs/ext4/inode-test.c
index 529cab24d..da2b9e7b0 100644
--- a/fs/ext4/inode-test.c
+++ b/fs/ext4/inode-test.c
@@ -3225,7 +3225,7 @@ static void inode_test_punch_hole_and_truncate(struct kunit *test)
EXTENT_STATUS_WRITTEN, 0);
up_write(&ctx->ei.i_data_sem);
- /* 1. Punch past i_size is a no-op */
+ /* 1. Punch past i_size frees nothing: no block is allocated there */
inode_lock(inode);
err = ext4_punch_hole(dummy_file, 5 * INODE_TEST_BLK_SIZE,
INODE_TEST_BLK_SIZE);
diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index 866c1d92d..6c7f58083 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,32 +5372,40 @@ 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 also covers the rest of the block that
+ * holds i_size, so it frees that block unless it starts inside it.
*/
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)
- return ret;
+ if (offset < inode->i_size) {
+ ret = ext4_update_disksize_before_punch(inode, offset, length);
+ if (ret)
+ return ret;
+ }
/* Now release the pages and zero block aligned part of pages*/
ret = ext4_truncate_page_cache_block_range(inode, offset, end);
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] 4+ messages in thread
* Re: [PATCH v4 0/2] ext4: punch hole beyond i_size
2026-12-03 14:15 [PATCH v4 0/2] ext4: punch hole beyond i_size Ding Feng
2026-12-03 14:15 ` [PATCH v4 1/2] ext4: fix the fast commit range of a punched hole Ding Feng
2026-12-03 14:15 ` [PATCH v4 2/2] ext4: punch hole beyond i_size Ding Feng
@ 2026-12-03 15:07 ` sashiko-bot
2 siblings, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-12-03 15:07 UTC (permalink / raw)
To: Ding Feng; +Cc: linux-ext4, tytso
Sashiko has reviewed this 2-patch series and found no issues.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203141536.52700-1-dingf3ng@gmail.com
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-12-03 15:07 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 14:15 [PATCH v4 0/2] ext4: punch hole beyond i_size Ding Feng
2026-12-03 14:15 ` [PATCH v4 1/2] ext4: fix the fast commit range of a punched hole Ding Feng
2026-12-03 14:15 ` [PATCH v4 2/2] ext4: punch hole beyond i_size Ding Feng
2026-12-03 15:07 ` [PATCH v4 0/2] " sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox