Linux EXT4 FS development
 help / color / mirror / Atom feed
* [PATCH] generic: punch a hole past EOF over preallocated blocks
@ 2026-12-03  9:14 Ding Feng
  2026-12-03  9:30 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Ding Feng @ 2026-12-03  9:14 UTC (permalink / raw)
  To: Zorro Lang; +Cc: Ding Feng, fstests, linux-ext4

Blocks that fallocate(FALLOC_FL_KEEP_SIZE) allocates past EOF should be
freed by a later FALLOC_FL_PUNCH_HOLE over them; fallocate(2) says whole
blocks in the range are removed. XFS and tmpfs do so. ext4 returns
early for a hole that starts at or past i_size and leaves them allocated.

Write 4k, preallocate 1M with KEEP_SIZE, punch [4k, 1M), and check that at
most the block holding EOF stays allocated, and that the size and the data
below EOF are unchanged.

On v7.4-rc5 this fails on ext4 (1048576 bytes stay allocated) and passes on
XFS. It passes on ext4 with "ext4: punch hole beyond i_size", sent to
linux-ext4.

Signed-off-by: Ding Feng <dingf3ng@gmail.com>
---
 tests/generic/810     | 37 ++++++++++++++++++++++++++++++
 tests/generic/810.out |  2 ++
 2 files changed, 39 insertions(+)
 create mode 100755 tests/generic/810
 create mode 100644 tests/generic/810.out

diff --git a/tests/generic/810 b/tests/generic/810
new file mode 100755
--- /dev/null
+++ b/tests/generic/810
@@ -0,0 +1,37 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (c) 2026 Ding Feng.  All Rights Reserved.
+#
+# FS QA Test No. 810
+#
+# Punch a hole past EOF over blocks preallocated with FALLOC_FL_KEEP_SIZE
+# and check that the blocks are freed while the size and the data below
+# EOF stay unchanged.
+#
+. ./common/preamble
+_begin_fstest auto quick punch prealloc
+
+_require_test
+_require_xfs_io_command "falloc" "-k"
+_require_xfs_io_command "fpunch"
+
+file=$TEST_DIR/$seq.file
+rm -f $file
+
+$XFS_IO_PROG -f -c "pwrite -S 0x61 0 4k" -c "falloc -k 0 1m" -c fsync \
+	$file >> $seqres.full
+before=$(md5sum < $file)
+
+$XFS_IO_PROG -c "fpunch 4k 1020k" -c fsync $file >> $seqres.full
+
+bs=$(_get_file_block_size $TEST_DIR)
+limit=$(( (4096 + bs - 1) / bs * bs ))
+allocated=$(( $(stat -c %b $file) * 512 ))
+[ $allocated -le $limit ] || \
+	echo "$allocated bytes allocated after punching past EOF, expected at most $limit"
+[ $(stat -c %s $file) -eq 4096 ] || echo "size changed to $(stat -c %s $file)"
+[ "$(md5sum < $file)" = "$before" ] || echo "data below EOF changed"
+
+echo "Silence is golden"
+status=0
+exit
diff --git a/tests/generic/810.out b/tests/generic/810.out
new file mode 100644
--- /dev/null
+++ b/tests/generic/810.out
@@ -0,0 +1,2 @@
+QA output created by 810
+Silence is golden

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] generic: punch a hole past EOF over preallocated blocks
  2026-12-03  9:14 [PATCH] generic: punch a hole past EOF over preallocated blocks Ding Feng
@ 2026-12-03  9:30 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-03  9:30 UTC (permalink / raw)
  To: Ding Feng; +Cc: linux-ext4, tytso

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] fstests: stat -c %b limit in generic/810 ignores inode
  metadata blocks
- [Medium] fstests: hardcoded 4k/1m offsets in generic/810 ignore file
  block size
- [Low] fstests: generic/810 leaves TEST_DIR test file behind without
  _cleanup()
--

commit ddbdf0d7928967d0167f771003c32e951506eb24
Author: Ding Feng <dingf3ng@gmail.com>

generic: punch a hole past EOF over preallocated blocks

This commit adds fstest generic/810 to verify that punching a hole past
EOF frees blocks preallocated with FALLOC_FL_KEEP_SIZE without changing
the file size or data below EOF.

> diff --git a/tests/generic/810 b/tests/generic/810
> new file mode 100755
> index 0000000000000..60f92b38a270f
> --- /dev/null
> +++ b/tests/generic/810

[ ... ]

> +file=$TEST_DIR/$seq.file
> +rm -f $file

[Severity: Low]
Should this test override _cleanup() to remove $file on exit?  TEST_DIR
persists across tests, and the default _cleanup() only removes $tmp.*,
leaving $TEST_DIR/$seq.file behind after every run or interruption.

> +
> +$XFS_IO_PROG -f -c "pwrite -S 0x61 0 4k" -c "falloc -k 0 1m" -c fsync \
> +	$file >> $seqres.full
> +before=$(md5sum < $file)
> +
> +$XFS_IO_PROG -c "fpunch 4k 1020k" -c fsync $file >> $seqres.full
> +
> +bs=$(_get_file_block_size $TEST_DIR)

[Severity: Medium]
Can the hardcoded 4k, 1m, and 1020k offsets cause false failures or
vacuous passes depending on the file block size?

When bs does not divide 1m (such as an XFS realtime volume with a 12k
rextsize), xfs_bmap_extsize_align() rounds the falloc end past 1m while
xfs_free_file_space() rounds the fpunch end down, leaving the trailing
extent allocated and exceeding limit on a working filesystem.

When bs is 1m or larger (such as ext4 bigalloc or XFS rextsize >= 1m),
both pwrite and falloc fit in the first allocation unit.  Because the
test never checks allocated blocks before fpunch, it passes vacuously
even on a buggy kernel.

Querying bs first, scaling the write, preallocation, and punch ranges to
multiples of bs, and verifying that blocks past EOF were allocated before
punching would avoid both issues.

> +limit=$(( (4096 + bs - 1) / bs * bs ))
> +allocated=$(( $(stat -c %b $file) * 512 ))
> +[ $allocated -le $limit ] || \
> +	echo "$allocated bytes allocated after punching past EOF, expected at most $limit"

[Severity: Medium]
Can this stat -c %b check fail spuriously because st_blocks includes
per-inode metadata blocks?

On ext4 with 128-byte inodes under SELinux or default ACLs,
ext4_xattr_block_set() charges an external xattr block to i_blocks (and
XFS similarly includes out-of-line attribute fork blocks in
xfs_vn_getattr()).

On a fragmented TEST_DIR, ext4_ext_grow_indepth() can also allocate an
extent-tree block that ext4_ext_remove_space() retains while the first
extent remains, leaving allocated above limit even when all post-EOF data
blocks are freed.

Checking data extents past EOF directly (for example with xfs_io fiemap)
or accounting for metadata blocks would avoid these false failures.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203091452.74081-1-dingf3ng@gmail.com?part=1

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-03  9:30 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03  9:14 [PATCH] generic: punch a hole past EOF over preallocated blocks Ding Feng
2026-12-03  9:30 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox