From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.216.52 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796298746; b=wBxvNPdvw8YMWiqwKkRo4yQE7GmQZXp/JYu8CZb6w5T1kCFcxaLa1Jo59dceCuDrFW7n e+7I3COlvlWoDODZ3bf8yQ+na/O3Zp+p9uW+3O8N9UKJQpCxAyPv/GllZq1vHDeJaYH7b YjJbvuAh3iaiuV0Wnz3uqjJS5HgwfarGVAd2kz2oisqfVB3QrnFgdTazwW4VJEYqhdjyG sGJa5vBF1gWqdpZnoqtFI2GWZIResW8i/Wuzco8FZFbILZalJsF/gWK11Hp0hd0ROKn6R cH6Bs5/eQHgn7beYUmoHc5EazyqZ663AieYkt9CRhYGIXENm6cflnPkdyITLmftkXxQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796298746; 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:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=bf+b4r/xTYPEe8P3HZ4XOtYAF5gDq/K9lMAnQoOiML4=; b=GZNP3CoaZVUYpEP64k8TZ8qYcYv9OKPoz1Q/0Q9GQRub0HEA/QRPR8uIjhk8LcmYzN8S BgqkmOynuZveoOhHr4pCWNC0cVa6sYUiOXKTxHvOE2yE2UEL9a2Avska2TaMGxIvJ2EVW lWF7rDcEeR6kKTsNsh5IhL+R5mmsasznUVol0KDG1moKeYzN1dHS6FFdgOafRYmE8Gigp Ub+fy26nAKh6q315KPxGrgSEKaFMRLtfQWYhnSd/rkebAP8tJN0n5nAwLJqzrjKHzM/AD WuelMK1PZctIg07W/lrjZRAA2mehy1xWk/U/YAb9Ak1MnaDe65eqPjgkF6+BB/g8+KA== 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=tDZoqYFl; arc=none smtp.remote-ip=209.85.216.52 Received: from mail-pj1-f52.google.com (mail-pj1-f52.google.com [209.85.216.52]) (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 F3B6A606A322 for ; Thu, 3 Dec 2026 11:52:25 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org F3B6A606A322 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=tDZoqYFl Received: by mail-pj1-f52.google.com with SMTP id 98e67ed59e1d1-3b4a93d0814so5796802a91.1 for ; Thu, 03 Dec 2026 03:52:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796298745; x=1796903545; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=bf+b4r/xTYPEe8P3HZ4XOtYAF5gDq/K9lMAnQoOiML4=; b=tDZoqYFl9LgKf3ndKsNPLSuIjR+3vZH0I3T9Ud7OFvG3doQh+cazaIC05IYmUbR/Fm YOCh5xA4nq3enDruyWgerpeYM5XiYgNyRQM/E8SnLNdfnPXypxG3UTS4oBUGXMYXjvXM Zz0ZIiSBagVaUq758jUzql3uaEnMqupEXn0EdLkPJFJSmNGeEe6mqhzENGd0pPNwca+x Ec2g/9XI2VEjXlcDoFamFFA6aXP19VSP3oYw3+9GLfNu/3BmKTTvMvGRBNsqVO10U16G jOe0pwlWqpug7v7Okvp7vY7mLgGNitZ6K1yvPvkDgMTMCAb4mQEGarHl7M9qnC7ysbyz mWqg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796298745; x=1796903545; h=content-transfer-encoding:mime-version:references:in-reply-to :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=bf+b4r/xTYPEe8P3HZ4XOtYAF5gDq/K9lMAnQoOiML4=; b=FrulT3fgjwe7uoNUtHgiyFXyV838Q99PzTTyntV7Tw5MBinp6RUtxtpT9fDngP8qi3 QNyxea82HvJW8FdLx8fJeCcJ2SqBTvkjFXBbVEGUPFQfkDvf5HURGuLoLBRmCOUO9LqG kfACgEfVoM1BsfNj/IOxzt5IO/mwugc7aqnkR4iT3tvk2W/D6FCI7eXhRy76iEMA2YWI wNlWhNZWE3rchtUzy+OWygLCnuS9nODTBizjFusydPupeyXg5a0zfw94wpZRzYf9iNm2 DPwPI10lKw5gqcyyp6OwU1NPXg6oWKpVwItsw0DMhgjZgLncOwEXnvWJ5WarBXcvtVp+ xIhQ== X-Forwarded-Encrypted: i=1; AL+hErE3MZuZ5CIqj0N8TyHgU5/oflhh2zKGQisQYxHhB3NZ6xmdr3gIkBOgpumMWei0CXAjruEcaw1gBbX5@vger.kernel.org X-Gm-Message-State: APuU4Z9dezLKPiM1Jtv+5bMwADDbWnzb4ViCdqJUQcOuZ3Abpqezt2Jw u2XOV40DzOpWwwSj7T5/a86JBjBigf7mCq2x6c6erGyQngOd9pqBzQ1O X-Gm-Gg: AZFdTZcNWOe+E6QbJR1fpU2Xopge317jGUHjsky2Y9uaoxFEGHT8FijEOHjRGG0lmnf PJsnWfHcu8oLzRX4BQYExVCoESZxqvktHxa8MdjfbeU1omBQqvSVyqjTdCxoqraL2sKt9MT28oQ pOJAlK55TVB2abNUBL4BntY74FoO0kToi8FJ8lmjeSScIsbXTPR/Gyw9uJBwNyls+PHk/g+GqdO cioJe3Eppt35gy0EEhVYaGy2ErRz4q7HLj/v3GI/RnE086JzoUxp6zLJmqHeEGuzHgTuTIsp6as Iu/qvXKmTRUgIYdZB1eK+aeA9lN9gKq7uY2qpgTtjMY2+u0vDTYexvWeHmyl4LMAN9+oTtZIg6+ 65mMg4OrEIHboHixWlfFXShuqCQLHrC8qvIYBnQZhC7ALhX52Sz2QsHKJWwpC5RZz7LsRFGkfGS ie3ZjmCnAfnJDrhYkZcIN78fnvvFEOX1Tv0ZlvTsbaS2/ZyZlcT93UAv43vcMgtHC/rOzC4bQYP FG7oSYj1crr7QxqMakaHGFmkEruYZx2cn98C25+ze4JgZq46dQWBcO3cQX3CK/YVCpt7CvAheKa qsn+QhXEAgOM4PzTbstTnMx7WryWO0We5hUQm2UfpJuU3RpFzGMv7Ytbh9bQ X-Received: by 2002:a17:90b:1e4f:b0:3b5:35eb:5e88 with SMTP id 98e67ed59e1d1-3b535eb5fadmr21322922a91.27.1796298745050; Thu, 03 Dec 2026 03:52:25 -0800 (PST) Received: from localhost.localdomain ([64.118.140.61]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b5c7d486efsm4026470a91.9.2026.12.03.03.52.20 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 03 Dec 2026 03:52:24 -0800 (PST) From: Ding Feng To: tytso@mit.edu, adilger.kernel@dilger.ca Cc: jack@suse.cz, libaokun@linux.alibaba.com, ojaswin@linux.ibm.com, ritesh.list@gmail.com, yi.zhang@huawei.com, hughd@google.com, david@fromorbit.com, linux-ext4@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 2/2] ext4: punch hole beyond i_size Date: Thu, 3 Dec 2026 19:52:01 +0800 Message-ID: <20261203115201.48810-3-dingf3ng@gmail.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261203115201.48810-1-dingf3ng@gmail.com> References: <20261203115201.48810-1-dingf3ng@gmail.com> 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. Zero partial blocks only for a hole that starts below i_size, and not past 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: 2be4751b21ae ("ext4: fix 2nd xfstests 127 punch hole failure") Signed-off-by: Ding Feng --- fs/ext4/inode.c | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c index 26cf00ff8..7f491a723 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,19 +5372,16 @@ 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. - */ - 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, loff_t offset, loff_t length) 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