From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.215.179 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796289292; b=auRmM+r/9GMKPfHYKN3slVr+XStoWys8d0dlCk/5GZL/hfagAewXI2dLbqJUig8LG+kV o10RY9M3B+2oizcbwOMUHG9vuUKncQRr8EzUlEsgZ7UMxaVX8PEBnlaLY2xwxu50sWBTN Cp6a7MCufplfTjZP6/07LN31ZuS7I1GkgPVSxI7NPpNAwzLg0/Lqbt9NAtWxyFMGQRm/z 1YF9ahBB1igv59eMlwXt/SKh56AeXzAGJWcPJkqfQHVDYFU/YvuQmCjh4LY2aQpaCOOMp vFZsc3FFLONccnZsV0TXBc68aIZWVcftB0GSVNjmh/08LcugFAcEkuzI/nlHYSD9MYA== 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=II5IyFpB0zDqFnihcZo3wLfKqpnOTXERWntCMwv5w/ejP12TPK+uOKPpXkl60jVEvxy+ SrzkGZVn0b9sga6faQ+xpnt2vSxyMs33H49O5bRbgIgJ+zkuu0VtwBW0pOskkZw1WthTw v+JqpwnWkj5C7quKPqOBUs91/yMx5BMkemwdPAgdD4ULKaR8LTMKDdSHCDGvceLXC7ZUX WBON9les2Elc3YyTsyG27nlxJEigbXTeXKn6zCbf4Bkf9SXTCGHQsBNMttz5l55U/9TNY Fl0Fo9sJstCKkCHu90YFD8ra7ovJNP+tedZUk3lvJE1ypqdKgyP/kT/BivbrJM3JrDg== 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.215.179 Received: from mail-pg1-f179.google.com (mail-pg1-f179.google.com [209.85.215.179]) (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 DE000606A32C for ; Thu, 3 Dec 2026 09:14:51 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org DE000606A32C 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-pg1-f179.google.com with SMTP id 41be03b00d2f7-cde5a7d7849so6206090a12.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=NmmuEFQs53E13/D98PTrl69MpoYLNN0hJwmQThedXvIqTpKofXMcB/+tkJpj7s1fwX OKkENodP264SAx7o+c7wk4inGu3ACV2phTDC6SpGR0ZWRGzgewP+CgAXXMoD4lsEH4Ue poo75ahown7to8noPnvv2WMbgNJhwDOEF/kPzaOkBgOfkYasxYeYKGKbaryF58BrIzrN lS3/SWGVYu2748ZpcllKH7LTr8Z1y/FNox0g1fg0aIqq/EyMmWoFLULyPZMj5EudXFFp K+49ezrVW0UZ0ZNeW/dfJkScYVqpbgIvKGuVm3gPoFtAE/BSPynMK6yeQr2KYH7UOkLb KXWA== X-Forwarded-Encrypted: i=1; AL+hErFjrM2Z71A0Alntskdg0FedvJKF9iQps0nDx+TFRIEPgPiV3ixrmFti1IuVyOXBicLyOW+v/aET0WZs@vger.kernel.org X-Gm-Message-State: APuU4Z9Qd4BqxpdMVru+GY4CoPuTPG2n17460IIPqC8BuEv7zvWq0wFZ xoTt0xhdDjEaypA1Kj/KQDqkcRwX6tf+gWvwNrzPiXZkhJBPcssW9lon X-Gm-Gg: AZFdTZfpRlsLDk1kWdJNjDd/eEX358RkmMikrCJYlosEHzjkfK6by5c+3U5p8YkPIIt QTulPWiyTQL1kycj3paaRv/G5oJkgH6nQt3SFkQiepUVJGddJqvPzkFF+avs7yZxBvFxRTi5dBW 9Li+wMcmxAKFJMG1ofUK+14Z0cmCYoRSeblzUpQ0qvy1xr2dL6tEQ1U7DmWW6+5tx70SOEkvmlm khZOc5272itC2/BNxCt8tEdg30TvqWh0y7TTiM5Gpb7nOJZHidQnwlOjC5apcAsX05G9tZUEbID YL001ALtESpDZaVvK7a0S3H9m0PmcVL3v7Ah9lZngPQ3N4AsDV/1UOyLeca1zLj1IttqF0xi+Wt 7W969+eO/pxAOZ0a9TN2MYnTuRLQpNZmehaKj7US3jRD1DCvDfs07mHxU7Fm28ts5fqt1tckOse DFnMEHDZ60mBLO+mMeVpNBa9KLPo/0PvKUt6mv+C4fl8EeFlLidfA5YemNdBDfd6GOoTVasqf9b IiGPBQ+ELQMJ1Kwdrk+P7OZaduFtKNniLpSy4kJAYtsqM/ZnVVi5dlNDs1/E4shOR1ateNMjQ+/ mFLHYi/z8pDeqANmCoBDIXUQKA== 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-ext4@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