From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.216.53 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796289292; b=AR27M2g8U3CI+lZ2n78lcfb4Mh6YvY3Z2p8Ozb7XRCF+0G2YXy0Pe23mt9ZdvAYmeQd9 CDQykSjtGvnbyAeMCOvjaqc+tILmhIBKkHTMrwFlZPX7GIqxevRGcKUPrOCxIRz0KCvMk oBKqqksP8jkjFwM3h9fZvB1HDxH3giwsKL+/O83imm9BI/ormybOUbyF3b1DUROLLU2hw 78yXUwD5Jxfu+DU1Se2cLOtV/OKDGbbLMxJ6BEf1aVmH8cW0aMertSCYvGV4BRIx7aDZr 1KjEEKwyz8Ml7U4BcSkgkxzy1qkgKcOREcvB7JbvkFrkxMFLR58cX/ijtZH4N0V+0xA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796289292; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=wSeRxvLnl2JWw+DAwhjgcmyUAHhRwcVGLCyAo4aGcDM=; b=idNMjEACLZEGKIsVdlqKsuj9z4ymTyU4qxf+3P8lEODhBYAKrllkdDNx7hSdqbyVNeW6 3Lmd27UApn3bAnExkrvU05/LbzD8ZQvbdKWUM+1QFTobJpaoFzpovxrUa5fW4/vxs1rEL mlkolZvOCcuYve68AESBSZ2zgR3C2i/CPisETD0qQeoP5GkiPAKHTRMBnokWDnzUuae+i k/coxESj9WfSj1LUTYzxoYKD/5DVCGY0P5c7U86z+BAN8rzB7iUcVUdlQIij/tplPeKAW cqm3Y2jBNDpOjdNC/ZuMe+DyLMGG1v6QYJiB+WCXU/5BXvaZ4E2o0SHL5JdLuhe7BwA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=lU7dRi9b; arc=none smtp.remote-ip=209.85.216.53 Received: from mail-pj1-f53.google.com (mail-pj1-f53.google.com [209.85.216.53]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D2E7B606A322 for ; Thu, 3 Dec 2026 09:14:51 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org D2E7B606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=lU7dRi9b Received: by mail-pj1-f53.google.com with SMTP id 98e67ed59e1d1-3b567ae3d54so2684104a91.0 for ; Thu, 03 Dec 2026 01:14:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796289291; x=1796894091; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=wSeRxvLnl2JWw+DAwhjgcmyUAHhRwcVGLCyAo4aGcDM=; b=lU7dRi9bloj+s6jul2I5S1ouIQ1m2AtMTkNczdDI/I9PguDzAPZFBz2KP162XM6+2J SP+nZ87ZKIWgJm5J+8LKikCFlFRyuSWYpd7C0TaJOQbWrcZTrqCd+IUYBO3NCinDblyE q86523hXc0R44ahTEU4rssksWiqZJlcZ4wm1/NSDC8Zu5AeT8iNLLahxuJ0pii6qDfXU 4pLwaxHkLP9xTeo1CWBCp7WAg+wuwAqB4ySgvsS9h7pl8wPejync8fqytj8xPnZ6Vfh/ RVPQJiS1xBsXO55yBbDGdoKek768YS/UHsEQ9bg4BA1WB3iDpdNwQnYb3/TmrGm8x9C3 iwlw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796289291; x=1796894091; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=wSeRxvLnl2JWw+DAwhjgcmyUAHhRwcVGLCyAo4aGcDM=; b=i2/Ki+7ulIfSF4Wiq6Sy3LHsul/5F4eXej6cKAk95swceFzbDZOzmv1IGPaPc+pim7 f8QAAltUI1OYPO8G4IygjYM41718AEwi1UcVAYgIjVm0d4XKcNrdcyVJbGDJ9zPN7Y3n F0CalH6zzAKigfFtElcaf8dlQ3JEgrCyH8cw42SteLWI4VdwnzzScn8qpkEQSz4dSn8v 7VWg0fU5H9g4FIpe4T93jWzX3U0nKL1DAaQiuhYdfNYOo6mL1ktArpoX+N5O3hZuXIRR TBYg9VttjQR8QKgKjAzX7dgUV9u2RUVmPGf3t+Mk0EYBYSUQs9OYlaoEA1MpgW9oqDHJ P1Ow== X-Forwarded-Encrypted: i=1; AL+hErFZ5x5+57VVKK7tYkIBXuujzlJFwrnSTUWvBidoZ23mxpf/1TPtWqK23z/TAbp6GE2ZiTK6LfbKciaS6ic=@vger.kernel.org X-Gm-Message-State: APuU4Z/JeTgSN2fH8NfvTYgTZEDstp3uxcQIKiLIiRrU+sAPqBV/RPA6 QBKhw3WiFv6k4BI1Zc7PdV3/Wz0ibYeKj8VJNprJ1KhnuHXw0Lqyjdft X-Gm-Gg: AZFdTZeB7aKfCYg0GvzNdHGj13f5a5qwadUbNwaF80bEWzYIzqlmjE4ZwptHEXKgeew bhZnbxCshLcWfRwjnJY8kAKUP0k9X7uf82yq/e3/5Tr5Xm+T9yF095tid5a8gpSa4K/Smpo171d ybeQwxl1u5LZxUFP+Oe7ZOXcGwwEYK0Tj5yFEpcVKrTZYMUBx7f1SwoYA/Em9BDb8LwG/0a6VZA 6VRz81PuuFfrLxaYp16oabTTU0zge3kkVtbaGhLvmfNWguG6HyG/aA+8O1PPZ6q8MYVEVeWqsqD QYuX4+LeX1XEqsA4CQBbM+K5OVjhxaWUmDQRPUEUxYjS9AqXzUZLQMS0jawAXvJiltUPvdWPNnl RWnqTaMmvNAZocewuFJYBcoEKgUNRFsGE1Yh4naD9NHxuGrg8Ipd3d5RC3RWH/K3kShM6Wx9d2b L0ywxgYa9UzlCEfVA6O9m4jzN0wCgJGiXoBb0g0tlOSaHugs/3z7bWREK6Njfm8Va+TzajKrcMM D+60cDbdGLsTg1BSFWwfdGGIk+MrYkk/4po/zsMaIiFFwoYF3fL68PVVhLI2GyiSr/0vrBBHl+K BWLh7C6/lE09FZiuIxMLFIMjCQ== X-Received: by 2002:a17:90a:e70c:b0:3b4:22d:e078 with SMTP id 98e67ed59e1d1-3b41c7c238fmr41441966a91.1.1796289290952; Thu, 03 Dec 2026 01:14:50 -0800 (PST) Received: from localhost.localdomain ([2404:c140:2100:6::1b:9648]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b44b7a8c64sm31200942a91.3.2026.12.03.01.14.44 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 03 Dec 2026 01:14:50 -0800 (PST) From: Ding Feng To: "Theodore Ts'o" , Andreas Dilger Cc: Ding Feng , Jan Kara , Baokun Li , Ojaswin Mujoo , "Ritesh Harjani (IBM)" , Zhang Yi , Hugh Dickins , Dave Chinner , 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 Message-ID: <20261203091435.73786-1-dingf3ng@gmail.com> X-Mailer: git-send-email 2.55.0 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 Fixes: a4bb6b64e39a ("ext4: enable "punch hole" functionality") Signed-off-by: Ding Feng --- 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 #include #include #include #include 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