From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.216.47 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796298746; b=voNw5TOeZiftMEC+8E3jUj5Q4NkQFBiWsw3qBIVYrUhRn2BmiAEk5dHPW066LYaBki5n mNs/HaFyIbKPd9x7jo1J+iuOEJ/ASnksTJZT0SmwLDJMQbRyuPvXEyqLxoRku5dbipo36 +XZBziq8KwgVgRiUPhCIXJOFiXMrdtp3L8x/bMWhghq265vUWHq4kzvV9we5Q8W2BltrC jchWEzPLtel/glr88FC4DakrmMUJDAYShOVNqaFdwPIeON+vaSlq2zhQFzUij7GC+3DFN 5GrX8IjEtqerHoiNkhIbokfdldzlIRRFKC1WmYJF0DNdTCRB5e7KAkR2FktHZyb2FAg== 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=lIcLviafl7teLNLwResSxoZZY77orMlLefFWsh/MCoijtd6prx1rh5b3EO4t7ncuyzQW hZwl9OuoPZ0BO1mzDhdUS5u+GJHOZ2K6n/WalwVbWUQf9W8iJskFGJY6BzJn2+3p3D1bs ql7CkHo3oG5uK0E+5sqEb37GQDayIAkeLlSN0DQ1N4mfsui/nj5VF6F5CGEuxNk5dYTds Wmu7OSGB47gHWQbHMzx4aeGhKTYbmWMvGvW8MigBxF/ft5EL8RFDidN9UytHbcsyBE65k ZjjdW+NiNZZwJnRq3iVKaPuerzj+ticSRNgMmd9/3M5I0+1d+QD6kHViM3tNlMfC1mQ== 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.47 Received: from mail-pj1-f47.google.com (mail-pj1-f47.google.com [209.85.216.47]) (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 01B98606A32C for ; Thu, 3 Dec 2026 11:52:25 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 01B98606A32C 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-f47.google.com with SMTP id 98e67ed59e1d1-3b3a42a4bcdso5835870a91.0 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=wTyZQsyzBAKCrf9C3wufIipxvp0eu+q4XpjUnAhN4+v8lod6UUp6DrbVOMHgJ503P4 xu+pbK8eXRgVdpoFNmeY2byDx9K0QYtfWuMuJV8sofznL6Q5lDc1nNmL+fVm/lW9hONK Vq/4X/wxI2pQjNmUF0eIeNht2A0dz4VKY3ZygrgRvYfg1Q8mv8Bzc3N2WSMXAuQI0Wbf i9DvAidZvihdsfqxp2cmg2FQnDCN/Fj61X+af1jUKVX3FAati7mQjC+pfw8REIpuZpkb EcBkTlYTRv+9SWSCsYJKYU2yNn5erUz9nKM091iZcPLltJQDQnnWcThdlmUzg4V06ipR jv0w== X-Forwarded-Encrypted: i=1; AL+hErFm58zlUe6B7v1ErW+ec/kZq6SD47VlI9xJ62WEGxXGlYzSi+idsc1RyE3vqXJDEqv8DgwaZZXhuOQBzBE=@vger.kernel.org X-Gm-Message-State: APuU4Z+eHD+O9vsfb3oC3sLqeQ+04wafbBtvSTRnq7m6YRmTUZvo/qPs SrAHs0PZSoZTGD6rMeBYzz9Pem3DbOohhcyo8z0KCi1O3qEKd+d+IX/7 X-Gm-Gg: AZFdTZdlgBEGa3cmLhkPKU8axKNa1fiKuTsHT0JoCQGPYRucb4KuuWwdfr5KXjuIwQJ u0cVbR67AqwbgzbOyelmwI3M2N/vJwZYYqqHwtKDXoRyIQj7qOi68D1MLEAVZrZgzjJ4uBwVmHb /D5REe2+Q2oLJTa8HELJoR8LDFpRZ8QgFbbJCqkFqiHl9J2/84oFhhtLywzJJdTkoML/M4NnCf4 YkyobNpCwwbmBpq12ant7P5azBHt+P5MKRJGH1XeRM995TWkqO43epfkDuhZ5mLaST6zK92vRrI Npl1ic3cw/4GCZBgbqrawgf6U3PVXvtj3+2aP2bg3hy6oIHsUlXpZ4P9gdKt6RZNZ1CMF4tDxpg xUNXQC/jdIpkVXL4YsZAQdUPDU6lz7QwUwAh6h1sQ4QNWOKLNPWIzVzwO+5pbJdF6Z9Ey+MMKa2 YoVHUDfY862NF/DFs08C37YyxAvjgSXwUPMYVMJDHenv1bgQ5ZWPcT/It0ay86z/I968erBiWgN FReW7muJ5bdKEVFy8Zdz9G41feOnE4x1Pfjkhv1a+5p6bIJxlr/iqwAdiv62cuEW9IiovbPTCBc Authq9j3PSG1TEvKAvrtFyDtd+DTwZRg+nCYEtZhtlGsGok/CcBDXeSQXpT2 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-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. 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