Linux EXT4 FS development
 help / color / mirror / Atom feed
From: Ding Feng <dingf3ng@gmail.com>
To: "Theodore Ts'o" <tytso@mit.edu>,
	Andreas Dilger <adilger.kernel@dilger.ca>
Cc: Ding Feng <dingf3ng@gmail.com>, Jan Kara <jack@suse.cz>,
	Baokun Li <libaokun@linux.alibaba.com>,
	Ojaswin Mujoo <ojaswin@linux.ibm.com>,
	"Ritesh Harjani (IBM)" <ritesh.list@gmail.com>,
	Zhang Yi <yi.zhang@huawei.com>, Hugh Dickins <hughd@google.com>,
	Dave Chinner <david@fromorbit.com>,
	linux-ext4@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] ext4: punch hole beyond i_size
Date: Thu,  3 Dec 2026 17:14:35 +0800	[thread overview]
Message-ID: <20261203091435.73786-1-dingf3ng@gmail.com> (raw)

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. Partial-block zeroing still
stops at 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: a4bb6b64e39a ("ext4: enable "punch hole" functionality")
Signed-off-by: Ding Feng <dingf3ng@gmail.com>
---
Tested on v7.4-rc5 (x86_64, QEMU) with fstests on ext4, 4k blocks: the
punch and prealloc groups show no new failures (generic/042 fails with and
without this patch). The new test "generic: punch a hole past EOF over
preallocated blocks", sent to fstests, fails before this patch and
passes after it, and passes on XFS. The data below EOF and the size are
unchanged, and a punch inside the file still zeroes exactly its range.

Reproducer, run as ./punch_past_eof /mnt/ext4/file:

	/* A 4096-byte file with 1 MiB allocated by FALLOC_FL_KEEP_SIZE: punch the blocks past EOF. */
	#define _GNU_SOURCE
	#include <fcntl.h>
	#include <stdio.h>
	#include <string.h>
	#include <sys/stat.h>
	#include <unistd.h>
	
	int main(int argc, char **argv)
	{
		char buf[4096];
		struct stat st;
		int fd;
	
		fd = open(argc > 1 ? argv[1] : "file", O_CREAT | O_RDWR | O_TRUNC, 0644);
		memset(buf, 'a', sizeof(buf));
		pwrite(fd, buf, sizeof(buf), 0);
		fallocate(fd, FALLOC_FL_KEEP_SIZE, 0, 1 << 20);
		fsync(fd);
		fstat(fd, &st);
		printf("%s: after KEEP_SIZE allocation st_blocks %lld\n", argv[1], (long long)st.st_blocks);
		if (fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 4096, (1 << 20) - 4096))
			perror("PUNCH_HOLE");
		fsync(fd);
		fstat(fd, &st);
		printf("%s: after punching [4096, 1M) st_blocks %lld, size %lld\n", argv[1],
		       (long long)st.st_blocks, (long long)st.st_size);
		return 0;
	}

 fs/ext4/inode.c | 25 +++++++++++++------------
 1 file changed, 13 insertions(+), 12 deletions(-)

--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -5356,6 +5356,7 @@ int ext4_punch_hole(struct file *file, l
 	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, l
 	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, l
 	if (ret)
 		return ret;
 
-	ret = ext4_zero_partial_blocks(inode, offset, length, &partial_zeroed);
-	if (ret)
-		return ret;
+	if (offset < zero_end) {
+		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

             reply	other threads:[~2026-12-03  9:14 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03  9:14 Ding Feng [this message]
2026-12-03  9:44 ` [PATCH] ext4: punch hole beyond i_size sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261203091435.73786-1-dingf3ng@gmail.com \
    --to=dingf3ng@gmail.com \
    --cc=adilger.kernel@dilger.ca \
    --cc=david@fromorbit.com \
    --cc=hughd@google.com \
    --cc=jack@suse.cz \
    --cc=libaokun@linux.alibaba.com \
    --cc=linux-ext4@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ojaswin@linux.ibm.com \
    --cc=ritesh.list@gmail.com \
    --cc=tytso@mit.edu \
    --cc=yi.zhang@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox