From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.215.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796307359; b=sgiXWJEp5rVHOxMO6myhfc5WmrfTDnjbBQlYryPqwxznOPw7UvegTOHeT7dcJnLxSILE A/UzmIWsfKrtiLor0khaBSykjwm4ZsvpRFMlABvpEnJx/MWMppl+qeDGQimVcgtkF1FT2 Huh0b0Zy28zEB4nVZzwZQzupnlfLTT63b7DJAAZQb3+hTufVSQZg3gtdIRBZPCCnJwrPp CwH5TlSCwLJO5nFXjRETEBq2rBJfhAUCnAVkOZJrcoKDc2Qq1kk6qJ16QZldwVt9uSQ9x GgDH9kjAg1m0PAsX1wrdGocUcIqgexbHCa8uNajFpNEysL3W1JOpOPjUhvFcTcwb0vg== 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=gC2u0av0WPP0SGX8Ux8NM+M+wBD5GMc10Hddz4SiZhWzKlRZ0O8IKnBXOqUNhYpMeYNU vRpMWO6o5mG+VJWoYtKPQMGCxuCI/sr0bMUJNYjP5cDVf72Zjf8oxK7LIzTYwwBCm0h1h hU2zd2FlGWIqGx0WAb391JNKaL7NQywiwBHwU/dQyh96EQ2HyNTPhqW3vuPUHoP/g86Xr sScVqrEFgESfBCaADNCcHcmq9ISgd/i3DD+mAC6eNxLkYsW9oEd7BkYFbU58PSK4/kkAp Y9R6jDdjspqs7XUUF6B5XPn8Ep1ZXwOdsUoPV4SgWJ0MUUB1Bp03t5/MnkA76Jm77wA== 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.215.172 Received: from mail-pg1-f172.google.com (mail-pg1-f172.google.com [209.85.215.172]) (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 EFE92606A322 for ; Thu, 3 Dec 2026 14:15:58 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org EFE92606A322 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-pg1-f172.google.com with SMTP id 41be03b00d2f7-cc751eddf25so8856172a12.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=K6kxPTbACOtcyWxAmFg0kpQfSMzqGckpewSNEbduWorhlzKcG1u2mzy6fDvN6BPxsd C7BvdQIS9cgYThVdVFY2sCri9ZoqkpPV2AG2bMYvHx1yb9zlJZUJGoVXzo87RebhA6yw 8PInP+GS+ScljLXZWYXWBJhaM09rkWtTwQ4vzAcqTHluVSUaTwYnoH0X0JleugpL94wT /Rdnroqvh+WnhTqQSX7bpVJBDAaGEdbD4He21KUXBRPDjPPQDAuuDS60bjxvAZckxVG6 LFjtHqTJ4PPsHewB6ZyZNrHNcB1YQrSa/U1EsRFkvfXBKvdvFFW7rAu86NWJcrnuIBFV 2RTQ== X-Forwarded-Encrypted: i=1; AL+hErHdI3KX8upgaLUiE1TwjolSym2q1mclJZpfszvAUVM3nmIR+6xmIeXpVRE8/swYwDQ9kLN3m+2qYLU2iRo=@vger.kernel.org X-Gm-Message-State: APuU4Z8uOpBIcSQByYG9WUWykpeyrg+2VLcUU7Wx1c72Td7Fa4H3r7Qg 7nFMyQ6Jqm2qLCHLUUaicmjPTnSBVJjibJvsHGfUh1/A1678XZ1XIYRU X-Gm-Gg: AZFdTZcEKMrvPlcEe2D6hDiZB16FbUlCPTeMCYlOk+u6hVASA3cVumaNLQb7miFgQ6l ayBHHNfX9Am3fyEhq9/UV6Y4/l4ze0mHTnHUNk9is3IJow4/c1aM09iQl+b4JqjGKoseIicQAjd y+91s6iCH90XW50NIh31raxxXonn8VYk7ZkBqAFSO29SQv8A3o091uk4O9U1bI7nDyVJyW+6UNl ssmPNS7YuJ0Lk1b/Wq7ha5/UKH8SFMr+aqCTyWyLLtWhfAQlfRj4cfhxrEVIh3QXBp7MSqgTiHT FS9fJcp67yLWbb/HmUyTa09izYReykLCtiO2jwff4vVaNVfy3L+4S0va2BKWvME1VQFJgKT2Kjn +H7q732PSnOWytlj8tN0Tj7lNSOijeWvSAPu/S0lrSo9AuXgFJVVKzcr1qVnxYh/Q773rk8/Obb lc2gC8bOPPJeKu0K9dwe3hEBUtrMPxHHjt3dPHFCevTcvBNs8hnPD5r4m+YalG1rcuvL0jD05Tg hdLgFBVAjdgV9vK96oHdI3J66M9iaR+ftqmZnYDepHl5a5gZAP5mHaSKLblAVarKHzn2K0Ko5ir Q+nlpCDzeBGCgmY+kICPc9he5lyJbyzFwlfcEYs= 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-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. 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