From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.182 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796307359; b=3Fnxf7X8IyGGwXC0B5789flDiP91dW3s12ARS2Qq+tiiNSpqmHCWCKA4LBINFKKQ/sVY QSo/mgAxPALtD913aS5T9JLhH3ep1b6aI2MdgvavQS3u/RITPczzJoHZ9xJeW/XCHw9yw YETtzYK7fopAUdnjz28fUTDKubYsm++TyWbP/EP9vSMdOETZgmlA1sOGQuBS5tXFkuS5o z/fuLr646gGPH8Ri3AFEcEJqgJmioFjWWf5XxBOc8nxMMxWhZ5GSc6Nm0eaMKfJp+MJwU QpNwmf+tIJniGcTys24jYpzB65iSjcP2wEvH1rE8bhM2+j8quDt9zVctjZWCY877olw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796307359; 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=OLO5Z8dQ4N5RbC8beXplFGI55SN0rxu1/cYoAu6so5E=; b=5gMNehl37qql1NFZKApo7y/IiQfFYc51bvK6fVk7K2+iynWThkQApmteFT0HiRtE6Cn7 K5V43v6WvLW3ln35ATGKEu7hZSeiDYcdqURC6vlyk3qQxGB09mAdPxuORQ+mxacBkdnP6 F9PZuEFGewVRNYN+rnkyPkIBovWxFtNxN9TrZj+NSSI14SSbG3PqwnyhCnVF94eNqVnbl 3CLLpXEuNzLjv7XBIZFdjOmhxgWrfGc1xcq+ymbd7cdFW+WQoMSk8abJwYhsSnlGRMq1+ KH2/zl5CpAmnRDu8LErk2+WQNdkdR8u4Dxg2bh8X3byNb17pMKP2jIII06Awr6vR9iQ== 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=RHvFJEV+; arc=none smtp.remote-ip=209.85.210.182 Received: from mail-pf1-f182.google.com (mail-pf1-f182.google.com [209.85.210.182]) (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 E69A4606A32C for ; Thu, 3 Dec 2026 14:15:58 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E69A4606A32C 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=RHvFJEV+ Received: by mail-pf1-f182.google.com with SMTP id d2e1a72fcca58-8c6c06b3321so11356795b3a.3 for ; Thu, 03 Dec 2026 06:15:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796307358; x=1796912158; 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=OLO5Z8dQ4N5RbC8beXplFGI55SN0rxu1/cYoAu6so5E=; b=RHvFJEV+FgUQnXoc17UdCxbN9gX8biKveB5th/uOzHu5x7z99eANrXi3vfqPAnUF6/ AtbUKSSUUBbt6yaKx7Ro/XWinh0kTi/jw+xsrKDtcgUQuo/MJRe6ubjLdtOqSqqHaCDb J2jNQ0KLpiYUxIpuQgDgY2/L40nOYm50mRLyxkl3s7abyI2Jc7FUrX9xvLzeosFtmXsV 6v9Zmzk6YBQzNC+Jfotj3hn5ICKdAWqNvZcNB+MimdwD4zMjgOrDNT+fTLSLEIkvDQFE LpyZ7cgT22nbyEFocDLpCnHaro1bxbscP3ODRnld+n3Pn3w516MMpEUr/a+LGhTwZRkp mbGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796307358; x=1796912158; 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=OLO5Z8dQ4N5RbC8beXplFGI55SN0rxu1/cYoAu6so5E=; b=Y6URTfvL0+8pMZ4CWXkCHVYCPzih3uc3VxKEwtwkTRqzediFKgMQ8+8jGj439jNY3L 463qPI7fdlz4JWeteaAJbxC7Jtn9dKycVDF/35hwqGw0L95QJ50n7M97Mvk+h16LpbPa E+yANYAXztigFaX6Pm5kp9/S90VIA6zFEEFZ38ANKc4ZF3nu0940Toviegc2iIP1aYea aaBFGPK7lOVsOSDGdYz7P0xCNCD2hvWNY9+Jpo1BtOvGFXP4nJWBqg68uTibGIC1oO4r ickGyM8T39cxQu5TyeonjZ9w935iQTag/ub6LY3Cd0C9mmoe8Vam3yUNP6gwEjGEcp2o vQGA== X-Forwarded-Encrypted: i=1; AL+hErGE71oVjPWyTwWZEZ586Y9q/zvscp2PIRzi0gtpIIq4J4PFefltAfYhswCF3AAz3EcRZSRD034UZUNK@vger.kernel.org X-Gm-Message-State: APuU4Z9xMdnY7R7pPkKd49Mp5A9XyFc7DeTdWQMfTaedIogtcRnKOSmQ FA3fFiJfSl6UWgUbKS0GXtEr6qa2tjgHtG4iF4iruXAkulbcqJHM64cQ X-Gm-Gg: AZFdTZctbdHlqXxod9bz0w1JQpUMht9gtnX+hPSLQYosiKNZZeDjeariJGjwjWbfMJn lm/gzXN+tDIee5ryz51UnRDMRG8J6/7LQMJ87ZpWFsk+N9UCugJDwC2qbqNgRwH49t3/2wIA1Gs xhKtXjKQrXAdGBaX+odKdK4YHHYJx22hS0mkyttwdgjjQRPhY8ZhpnBALjDm1crqu4tFMEr0njb Vc3VMtwFQfKv++lC3Jbe8Y7gFb4zqa6kFrz6mYfQobYJ9MiEt5PhNy++XHbzLtbq+hApaNeGMlz RmTLo/RevOc8b5XNsDdELDQjQiBU5/SVW8RcUufZZD4U7iMQinCC5FcWXiSU10zJWUM8J6j4ZSn 5+hNSTeu9PoOs4SjULrTxUtHMKQXbs9H7sGm6jStUm6IcJ9KBCjW/1DMaJQsmzc5eSYNJO+VEAm Hr6xzSqsvZN/LZGa/QLFDR67oZeDU8NFtcE+m10+uNBxM4M0YCUSRzZkKYEMN87lxZS7H7bQPn2 zUJIhUl9O4njNZMX3WjrFF224+WVgVt1HjK/AfN2VoMbcRKglnXhGsOiK9Xrg+6Uj5Y66mwZ0v4 4A351u2srv1Cdxpjxf9MvCjfrkhOj7q7ae0647E= X-Received: by 2002:a05:6a21:6456:b0:414:98ed:1100 with SMTP id adf61e73a8af0-41498ed170amr949157637.57.1796307358117; Thu, 03 Dec 2026 06:15:58 -0800 (PST) Received: from localhost.localdomain ([64.118.140.61]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-cde6d03adafsm24544845a12.23.2026.12.03.06.15.53 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 03 Dec 2026 06:15:57 -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 v4 2/2] ext4: punch hole beyond i_size Date: Thu, 3 Dec 2026 22:15:36 +0800 Message-ID: <20261203141536.52700-3-dingf3ng@gmail.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261203141536.52700-1-dingf3ng@gmail.com> References: <20261203141536.52700-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. A hole that reaches i_size still extends to the end of the block that holds i_size, as before, so it frees that block unless it starts inside it. A hole that starts at or past i_size neither zeroes partial blocks nor moves i_disksize; one that starts below i_size zeroes partial blocks only up to the end of the block that holds i_size. [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-test.c | 2 +- fs/ext4/inode.c | 33 +++++++++++++++++++++------------ 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/fs/ext4/inode-test.c b/fs/ext4/inode-test.c index 529cab24d..da2b9e7b0 100644 --- a/fs/ext4/inode-test.c +++ b/fs/ext4/inode-test.c @@ -3225,7 +3225,7 @@ static void inode_test_punch_hole_and_truncate(struct kunit *test) EXTENT_STATUS_WRITTEN, 0); up_write(&ctx->ei.i_data_sem); - /* 1. Punch past i_size is a no-op */ + /* 1. Punch past i_size frees nothing: no block is allocated there */ inode_lock(inode); err = ext4_punch_hole(dummy_file, 5 * INODE_TEST_BLK_SIZE, INODE_TEST_BLK_SIZE); diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c index 866c1d92d..6c7f58083 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,32 +5372,40 @@ 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. + * A hole that reaches i_size also covers the rest of the block that + * holds i_size, so it frees that block unless it starts inside it. */ if (end >= inode->i_size) - end = round_up(inode->i_size, sb->s_blocksize); + end = max_t(loff_t, 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) - return ret; + if (offset < inode->i_size) { + ret = ext4_update_disksize_before_punch(inode, offset, length); + if (ret) + return ret; + } /* Now release the pages and zero block aligned part of pages*/ ret = ext4_truncate_page_cache_block_range(inode, offset, end); 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