From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.216.51 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796303191; b=g2Kj8VYIM+1/98nlV5rl+grZ1BIqV0/9ohzI4qADkO3L2ks9TXR38Ku6NpHMkC9DhuUR jOHg6O+BlHulTAYZFCOxTEbDAHjPg/9lBt15qDcvUJu0zS364B4EHPaXSJaavoxfREA16 OSdxrU/7T0yXISwPUoYIO5zZMNdGTgYEAbvES180Rxykp+0oJgi917HdVcg4KvRRlgocR doHvuI/OHbY/WlhGlXIfIDrmpa4Xe6gHcivpwvpvLZyaNECpXstYRqyVib0bDnTF9nOdI mzcTH7wg8/u32lsrqGKf3LU5f8DmhMZ8bM0jq+DPSSoxY4aO0PEtr4zDO5PB13nugkQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796303191; 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=jv4iCIweandI1njn3JGI3vasYkgXIWi/dd/j2sc0Ug0=; b=l+kFLbY17Ys4D8o5Avoy1EW30ZRR4mK0bC2OHwIBGYUnoABFoW+JSzr3bU5tWUqU+mSz ssSV3DveuBH/4ySlut+sLcypJ9pKkqiteuKXaw9DJCb4iTnVmDr7qwhXtCSyRz2jOVbUL 8bnkLHRSgYNXgznfqcbE7MfwCXEBrlKW7hkiaW2iuEhppCUN1ppfmj9Pz5gJENjVyfrKJ XpPeKOkbuolJu1LeGz7AMmZa11bWTQ5M2Cyi1s6G30InP7PGSCJwXlOgUH1jof9LAcZSB oh9/VRyg9E6cIC86fZVBNw5t1rc0BBxhAh4ELXog4Zh4oiUDOs+GRnEd3WiaAkRT1Qg== 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=a8Qh839F; arc=none smtp.remote-ip=209.85.216.51 Received: from mail-pj1-f51.google.com (mail-pj1-f51.google.com [209.85.216.51]) (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 29D10606A322 for ; Thu, 3 Dec 2026 13:06:31 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 29D10606A322 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=a8Qh839F Received: by mail-pj1-f51.google.com with SMTP id 98e67ed59e1d1-3b4021b572dso8679729a91.2 for ; Thu, 03 Dec 2026 05:06:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796303190; x=1796907990; 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=jv4iCIweandI1njn3JGI3vasYkgXIWi/dd/j2sc0Ug0=; b=a8Qh839F1IrgVlkPQuyW7LNfv0SR+qNgD4Ts53tSLD6qG3B7KjIfuSgA5Aa6K1OBOr Bse7TqILCg/GYOqPUcg6O4Kjdk90CruLpJYS5Ow4DIwvPoju9NW8zztCQ0898RNEm0GE OyvGOOw+r46Dwe7RqHvYZ0lYWT6N+XtRkXVbM5UWliINluBPSmf22Hqkh+wJ4Aqb6PgL LPrIOpRMoXRJnfJW+59/GvnYMwneFWuYyi7diHy3XGraXpSDdBKttqoRCTi1VZMBZRb7 6s3e1Hn86WWREMrt/khJUJBiiTqfFe86QzYI5A4K68moAi25+Vdo7Q1naaZBtVVhUh1Z ADpQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796303190; x=1796907990; 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=jv4iCIweandI1njn3JGI3vasYkgXIWi/dd/j2sc0Ug0=; b=E2l+MGHM/qs8gHygjeFSvpj8rBJN8UA3uWvWN1YOLU0HuKJ4m5+hb7btNFqrFd4IPm bQFEm945/gRdZZm2N5pNe1PbqPM+JQmCRV/7a5MbmDd0Phb2C4upD/385WObOYE7hCPE 4mwWB8y3Qm+ePxU581pa5LjWr+qMUy9iBvxbv05uXpQcljqA1jJ+bE9yViGZdRJCyb/8 FYfQAVVGImExFlQorUBtVe3JTjD/brarixX1Q29S1XsndDOMSGaYKN/uC/X3wY8+96bD by/quLOlaG/bXwT+yonB1XVAEv5BmkQgXCXoD29ENNTjlK6QGCXS93Abcg8WdRY2WLGH gJRA== X-Forwarded-Encrypted: i=1; AL+hErHwwqNyluCMbQyXo0tRaQtQlhqsn2ZUCOH+9xnMHb2FAyP6pMDMHh98oZVwYHkV0WImlCdy536bf2/lnHA=@vger.kernel.org X-Gm-Message-State: APuU4Z8FJpoCN9pmuGIvxbjmkVjACvK+eCp6PqGmmysokSSQaQS+CM5P 6yto64iJYee8PMGgYqmL9i0mSZLarP25pD/Ha0XMTY+mHbOw1UyJ3Qof X-Gm-Gg: AZFdTZcK/Ry1GULMvDxM0SZLLHgzXbSsf8ru6lWDv0a4APM0ZY3yC321U/w/sIKYFxC VkMvHTVTxHrhvS9YcaArSNIUUHggKN4LeZ1pMW8F1ieB2MO+7/NrMTraJT+9iaXAFn7HIXEXnGm uY2QaYlUccR2LDYQqyAg/pHqCVI91BvokXnxfuAkDe7x4TCGQ+QSo+IgRGxx4sEWACpUuk2KnWY gtxqESvAMFluk/0O8YIN1NfWPhm6ox83gVseqHMOqi4FPf4Bo/0r+jctihnRm3hmyFGjE691Hlv jiMd7Ewwnh0CIVghkY1HITLY7mgfjKKWoXfDRlRH3vG0NOExTCorv1meNE+LmTrV9a0j6p2unCj 9sU5G4WhzLPzlQnfTpDAFtkmxiok7DPJlR6zoiTcZz4WztCNkE5n5TNQk797Za+q+k3V+oqhn9s Ywhz76JGp2k7GbSzrpVh4FUC4UZAV2skDkKkiyXz+tKmC0FVXunbVkafLA+Cd82+XiRnXeS7TWP eguXFJRFqUxJS5S2FkDETtzAFUfaEnkP2dN6Xc2morPw3CSSJ7CdcvhRugDtxOuHqNgy1EzuuwW ML5LJeMr1JxFc63JAreQQtijqA== X-Received: by 2002:a17:90b:2f4e:b0:3b5:4ce7:33de with SMTP id 98e67ed59e1d1-3b54ce73715mr19516911a91.64.1796303190356; Thu, 03 Dec 2026 05:06:30 -0800 (PST) Received: from localhost.localdomain ([2404:c140:2100:6::1b:9648]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-cde6d028a54sm22209945a12.18.2026.12.03.05.06.25 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 03 Dec 2026 05:06:30 -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 v3 2/2] ext4: punch hole beyond i_size Date: Thu, 3 Dec 2026 21:06:08 +0800 Message-ID: <20261203130608.64892-3-dingf3ng@gmail.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261203130608.64892-1-dingf3ng@gmail.com> References: <20261203130608.64892-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 covers the whole block that holds i_size, as before, so that block is freed rather than zeroed. 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, 16 insertions(+), 9 deletions(-) diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c index 26cf00ff8..f7d0a2e99 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,22 @@ 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 covers the rest of the block that holds + * i_size, so that block is freed rather than zeroed. */ 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) @@ -5394,9 +5398,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