From nobody Tue Nov 24 11:44:05 2026 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1795371583; b=sNYcFJTX2CZxMjk7ZsPXRaNGg4+ZExqa9tQEv+vhUPA3CXl3iYfZl6REmf/7nzTH8xaZ ilc/cumOHngOvV0PLZwD2stEmdbV4hxydY+G72JvpLQJDW1eU6FVc7KTTJCjwX2p3sNSZ 7Mc5mnHDxf2MuNWsTWt1o+/HW1isnKjS5f9lMDioVGPstBsJj+1t0RAYUbNbD0IfLeChU CTqZnHLYcq8CGdXfrz4rt+y084Up3chIjLplZy4vA2pet9mTZOQFw+JURog/QfCrEiM1u upfG/i1cGuKmtlf2aAuNgy6eseFk9qy9vcBaBfqsKp3On1Tay2vG19cKFPjTpVzniUg== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795371583; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=7oQB3/O/c4dFGNnygzjgJHN8nXWsiGK/1pYzsrZRsWo=; b=poRirNnD9rY68bSCfRHk+5yqUWW3Ar9RYfIlpHtWeqC96EHjKCv7ifhyQJv2o8+GmdaM CXAVVD+uFs+3XD9d3mq9fbqLFhRmYvlhm0KNdjfNcC6oOsHmmhYC0uB1+mwjgCqnBg+i2 Ll0PbzfRu6DRDwFAbVcE6EJ/cXzRoq91IwqG6oR0D6+gCRhvlmPSX6phUgwisbOaUPCGL 0+ibmA+MpzNjRKHOeU1BLWzfg4p8aUALIWzv+wAeifq6AdVK9FGeImjH/bN3pHCrvWE4c 98YDaVvbs52dpWdn8EljzyJ86tCM0AanPHx8BL9Mf1k5d8p86uibOIFKAFQvMpMKOww== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=LnPr5l4M; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9DEE8606A320; Sun, 22 Nov 2026 18:19:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=LnPr5l4M Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1795371583; b=vRdG3KoFSu6XRebj9AiPAhViDKw2UdTyV5RNVhKz20OLMomjWUAhh3I/ZxPWufKAhzVV q2+w1iOe8lgsRN64fDnbdhi9MFDN5PAemJ/OfPLP+hMm1p2KZ5/0sq0n0oL0lpDtUjG2w mH/OcFFoRrJdRpfku9ZgI2OPO1N/aFy3CzxFBoz8eJpT/Blbjrrzn5RWTXazy/RkOzHB+ F3iQrK1ZTBTg+LvwrLrEKvuMOokbRUalRVkloC+tp5bpQLCKy/2M9RH6sQGzT2PX0J87l qDWMO0JbDpAJIb41sJYcxSzqSKZL/3RJ8mjsX2f3h3vakBN/KUYfZrZ0dDaw1j81Z5g== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1795371583; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=7oQB3/O/c4dFGNnygzjgJHN8nXWsiGK/1pYzsrZRsWo=; b=FkMG2caQ/CSTpD424280DSWbMwJFHocujW8Go4bfaPZ99IHCh+3LbBlTzsBaPPGLSAv1 0BLBw7Q1EFcXNBpjtq14dVtLD1BWmKFnqR48Eo/0cmNOiZL9rHn5bUiDqEH04jJ2/lwLk JaRWgbD+B6+s8SAD1m/4tS8jowGMflJ2YLLRoHCCUiGbZ1i6O9Ia0UJ+gQz/SPCKWl/+X ECarO0P0G0GDf4Ih6vFVAFu6X+lYHM/1/Ppyv6TXrRWIclpfQbWApj9Zf2nXvC1EJ/wQB u7xePuTJIu0gomb/oYbrS59lha1qcK5pZgZLdl2hMbTLQHI8wl0E/X2E1aWqsyuRBSg== ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.kernel.org (Postfix) with ESMTPS id 18ADD1F000CA; Sun, 22 Nov 2026 18:19:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795371582; bh=7oQB3/O/c4dFGNnygzjgJHN8nXWsiGK/1pYzsrZRsWo=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=LnPr5l4MVl/WBTctWsGBV2HvZcZIukVEPQKWUD57xBAvydn1/8vPaqe3pJPdQgmyJ 1rXGf49el+7SAqsN7thQvPY45jiNYiQInmacX50IX51TZs1O7zOeVaoUIjuefXLfPb aOEzrvo7p/hqaWhPmCNSUA3a8V3GQ6WekU+UxHiGhMTGfMw+MqMpa0Xd4g/eZfTGNP wusgcpvw7jITS43w32TwECbTP2Rr5TOi3teeRUej40s3SOFvrBLsn6KtlS2NLMBpAd 6rUTBteKEazK9uo/uIcooxXkQLEQUgIBsTIXxVk1xj8LLWzj4CBsn/RQSjZDbrVsZo d0P2Vku+Ifcig== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 9045F4C175; Sun, 22 Nov 2026 18:19:38 +0000 (UTC) From: Ernest Borowski via B4 Relay Date: Sun, 22 Nov 2026 19:19:37 +0100 Subject: [PATCH 1/7] ntfs: release delalloc reservations cut off by truncate 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-Type: text/plain; charset="utf-8" Message-Id: <20261122-b4-ntfs-delalloc-fixes-v1-1-0726f0dce403@proton.me> References: <20261122-b4-ntfs-delalloc-fixes-v1-0-0726f0dce403@proton.me> In-Reply-To: <20261122-b4-ntfs-delalloc-fixes-v1-0-0726f0dce403@proton.me> To: Namjae Jeon , Hyunchul Lee , Baolin Liu Cc: ntfs@lists.linux.dev, linux-kernel@vger.kernel.org, Zhan Xusheng , stable@vger.kernel.org, Ernest Borowski X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=3407; i=ernest.borowski.git@proton.me; s=default; h=from:subject:message-id; bh=3nT+zUonqfx2kvoDVZIlPQfmCvPmSP51Ad2wiF9F3AM=; b=owGbwMvMwCVWzJ65uCpJ5D/jabUkhmxmIwu/cw/s2LYc4p8keqf/7MdHmhn5plfylyX//5Ttr rKrTiaxo5SFQYyLQVZMkaXj/40Qn5Bvn5bFafXBzGFlAhnCwMUpABMp/MPwP6xkV/Pcrjv++XNq zzRkf7jv7h8TMD/r87KrAsGO694pczMyTFXbq1OxddnKVW27eRfcufNyVf/s79pJZx/cUTPgqbi azw4A X-Developer-Key: i=ernest.borowski.git@proton.me; a=openpgp; fpr=88FFD8544C54F6F2A65E2A8E730769A37A6214FF X-Endpoint-Received: by B4 Relay for ernest.borowski.git@proton.me/default with auth_id=1589 X-Original-From: Ernest Borowski Reply-To: ernest.borowski.git@proton.me Content-Transfer-Encoding: quoted-printable From: Ernest Borowski A buffered write into a hole turns the hole into an LCN_DELALLOC run and reserves its clusters in vol->dirty_clusters (ntfs_write_simple_iomap_begin_non_resident()). Writeback hands the reservation back when ntfs_cluster_alloc() allocates the run. A truncate that cuts off delalloc runs before writeback drops their dirty folios, frees the clusters of the attribute with ntfs_cluster_free(), which skips everything that is not a real cluster, and removes the runs with ntfs_rl_truncate_nolock(). Nothing gives the reservation back, so the free space reported by statfs() and used by ntfs_available_clusters_count() shrinks by that many clusters until the volume is unmounted. $Bitmap is not affected. Workloads that rewrite files (open(O_TRUNC) + write) or truncate freshly written data lose free space quickly: a test that writes, truncates and punches holes in six 400 KiB files on a 128 MiB volume sees statfs() free space drop from 79 MB to zero in 80 seconds, after which writes fail with ENOSPC or come back short. A 1 MiB buffered write followed by ftruncate(fd, 0) and sync leaves 256 (4 KiB) clusters reserved. Release the reservation of the delalloc runs that the shrink cuts off, before the runlist is truncated. Fixes: 495e90fa3348 ("ntfs: update attrib operations") Cc: stable@vger.kernel.org Signed-off-by: Ernest Borowski --- fs/ntfs/attrib.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/fs/ntfs/attrib.c b/fs/ntfs/attrib.c index 6ee6b6bcac..4028c310fc 100644 --- a/fs/ntfs/attrib.c +++ b/fs/ntfs/attrib.c @@ -4383,6 +4383,37 @@ static int ntfs_attr_make_resident(struct ntfs_inode= *ni, struct ntfs_attr_searc return 0; } =20 +/* + * ntfs_release_delalloc_from - give back the reservation of delalloc runs + * @ni: non-resident ntfs attribute whose runlist is about to be truncated + * @start_vcn: first vcn that is cut off + * + * Delayed allocation reserves vol->dirty_clusters for every LCN_DELALLOC + * cluster when the buffered write is done and only gives it back when + * writeback allocates the clusters. Runs cut off by a truncate never rea= ch + * writeback (their dirty folios are dropped from the page cache), so their + * reservation has to be released here or it is lost until unmount. + * + * The runlist must be mapped completely and locked for writing. + */ +static void ntfs_release_delalloc_from(struct ntfs_inode *ni, s64 start_vc= n) +{ + struct runlist_element *rl =3D ni->runlist.rl; + s64 nr =3D 0; + + lockdep_assert_held_write(&ni->runlist.lock); + + for (; rl && rl->length; rl++) { + if (rl->lcn !=3D LCN_DELALLOC || rl->vcn + rl->length <=3D start_vcn) + continue; + nr +=3D rl->vcn + rl->length - max(rl->vcn, start_vcn); + } + if (!nr) + return; + ntfs_release_dirty_clusters(ni->vol, nr); + ni->i_dealloc_clusters -=3D min_t(s64, nr, ni->i_dealloc_clusters); +} + /* * ntfs_non_resident_attr_shrink - shrink a non-resident, open ntfs attrib= ute * @ni: non-resident ntfs attribute to shrink @@ -4479,6 +4510,8 @@ static int ntfs_non_resident_attr_shrink(struct ntfs_= inode *ni, } ntfs_attr_put_search_ctx(ctx); =20 + ntfs_release_delalloc_from(ni, first_free_vcn); + /* Truncate the runlist itself. */ if (ntfs_rl_truncate_nolock(vol, &ni->runlist, first_free_vcn)) { /* --=20 2.56.1 From nobody Tue Nov 24 11:44:05 2026 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1795371590; b=rj6x3fTqZc+jUOAuACcWLkV9xVTcmL7a6YPCTzv7fpj8szQLycE02m9pDZnzb3tkna+P KcGlFANxhKnYMVvjBKPutp1Ope28/LyYg+A7JB81gNP8uLnk6MaGE9gBAKAar1SZaFJql q651/FSf/vwcThXv5NIitSdCpZlOccEm59a+o0Tu9qRi732SONCGXdjiM8lF1ePmAtacL X4n8Py4kcJBl0KTKZiq7/1uCsIqlSqrk5IMOD+iZTPTtcVi7OEhW8hpSeJFa8IEjjxKvb oj8PtPQGI6pvX3VJUqu4P0Uwfxmlw7QnGwIpf8wVVpgij7C5JY+CPn/2hzn8lxpUZeg== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795371590; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=+RidziUgx+kZM9EX+B/idvPdGe7SZT8gd5HysQwlsX0=; b=Tl/3aaTY2ZVqBtxYxHyPkYb4YYzBQmQC6VufT0948iqkbAUPP4n8QVZZFzlkAwlSMJEX FKRaOBnBibbf33teqpEs+A86AvJRoyGhRnM2LjRTljRnuSKMdsBDcjiMwyuwFApDnG0hd dPAcOr4n0Cq++zgyKMa/Uz8omiLNZpOtoiG2JoKKpoMehut87cF1wfFvBR/3Me8Pqzv2F NDqu7dDYhuAfFhPFZ0Ac8oXimtHroqAXnnvZpFk0jVkYxpHbT+uHYLMrh3s2wv4y2rKUk qYJamyECWg1PCyclCVo0k3eVw4XHaLXBRRxqDqU+6sS1+537qzB0q+yAIhcIrEgaRzA== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=BX1D3YJd; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A046E606A32A; Sun, 22 Nov 2026 18:19:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=BX1D3YJd Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1795371590; b=L3yAq7lGfsCjiRwTHSBNAOi+FduQfX5nmasEj+MQMY3PLdiBChSueqwTLycMb3+0SQGW jiZUVeaQnWHmwDnm4ljWb6ggO6xx8u7U7l3Q/rhQaTOO9pUxgvSpgVBLB6EXQJVR3W76l 8OKSxtHdL2FZpQHY8CQIJHPK7q6n4ILvnZNjRFaJbtR5wUIAtWIa+UP5leGJSDF1DCPJC aGiGX+jmZBY6YaD72d1DA4/nZ+W5BKwdn/xIK+7yl3tJcADQrKz2IfUxnSt61HPRFCL6D yQMP7FOnBytnqN+7412lx057OyyL7Co2KcsPvIJCABf040bNHvWGN02S5Ooy19x57rA== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1795371590; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=+RidziUgx+kZM9EX+B/idvPdGe7SZT8gd5HysQwlsX0=; b=FN2Z4otKNNH4LFhHwvg7fU+pM4U1GEVkFBnV6QxxuBIEfrPegEpFtAnVyl5eV1yHu7Zk U3phW5YI6Sj//D+OSA1Rsg3qHv9H8ZmOWufr7sLu1a1H/O6kG3dVSM+Z6kfv7tOZjkr1h XALGaiD18ZgiMe0BOR1TknEaSh+bGbA8BBJj4AxsNwibkqHNCVzViJrF/mjEkj3p1XcUt i0WWzBWsIK5JMOIDS+47QzO/Fczc8NIgzVNCU/bQbsmD/onhjYQNKDHI5As8SDggRBPNr YUOta00J83IPzngz/agf2pEk/XOSfGohKjjFcEXa/6rtHSqJ3eSBnFjjODlvRIVJNfA== ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.kernel.org (Postfix) with ESMTPS id 288F41F000CF; Sun, 22 Nov 2026 18:19:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795371590; bh=+RidziUgx+kZM9EX+B/idvPdGe7SZT8gd5HysQwlsX0=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=BX1D3YJddSEKJFDUn21cUQCN3anLtAntRJRcR8gGT8K8clc1vmYFZudattZpUwJ1L qmxLIC17TWD6Qjd7gTlMNAOImWTnnYDKi2kzq4qF3t7knF3tmBtdLcMsixu8TfVPXP lbfEsdM5eqQlglTxJ910hqwwheGhxDdwdIBYWe3r1zZwRYapkZZlmA3oj8qpVw8K62 KO4fcW+p+wW2nljj9fS92zI7YG0Pj9U5aRWBm/Jt5cwkyjyebkz1mKqsSqZQhIyYH0 wJYKj0QJMmEKP384k2xBYcqH989veSqyqAm2J7SoceOCplsBZYjkHOj+RILJElBKLR vDEDBDDX+Vd0g== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id AA3424C17C; Sun, 22 Nov 2026 18:19:38 +0000 (UTC) From: Ernest Borowski via B4 Relay Date: Sun, 22 Nov 2026 19:19:38 +0100 Subject: [PATCH 2/7] ntfs: do not consume delalloc reservations when filling holes 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-Type: text/plain; charset="utf-8" Message-Id: <20261122-b4-ntfs-delalloc-fixes-v1-2-0726f0dce403@proton.me> References: <20261122-b4-ntfs-delalloc-fixes-v1-0-0726f0dce403@proton.me> In-Reply-To: <20261122-b4-ntfs-delalloc-fixes-v1-0-0726f0dce403@proton.me> To: Namjae Jeon , Hyunchul Lee , Baolin Liu Cc: ntfs@lists.linux.dev, linux-kernel@vger.kernel.org, Zhan Xusheng , stable@vger.kernel.org, Ernest Borowski X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=2158; i=ernest.borowski.git@proton.me; s=default; h=from:subject:message-id; bh=+jVS4i0N2xYWTOmK5KLBxioqsjuDV8wLrjNVc80eqAM=; b=owGbwMvMwCVWzJ65uCpJ5D/jabUkhmxmIwt55rhdurzxrz5tubPox/O0O+5M5zdeyJpQfz9M7 FNd+7zrHaUsDGJcDLJiiiwd/2+E+IR8+7QsTqsPZg4rE8gQBi5OAZhI03dGhtOTlRlXSpYckT2g sfrTxb6/C2zKv/sfWHZSQHrnk/PzPpcxMqw7sIilTmnpljWeVzZJJn/uL/qVk7VOg+NATPDkpCX zd/ABAA== X-Developer-Key: i=ernest.borowski.git@proton.me; a=openpgp; fpr=88FFD8544C54F6F2A65E2A8E730769A37A6214FF X-Endpoint-Received: by B4 Relay for ernest.borowski.git@proton.me/default with auth_id=1589 X-Original-From: Ernest Borowski Reply-To: ernest.borowski.git@proton.me Content-Transfer-Encoding: quoted-printable From: Ernest Borowski ntfs_attr_map_cluster() always calls ntfs_cluster_alloc() with is_dealloc set, which makes the allocator skip the dirty_clusters check and subtract the allocated clusters from vol->dirty_clusters. That is right for writeback converting an LCN_DELALLOC run, whose clusters were reserved by the buffered write, but ntfs_attr_map_cluster() also fills plain holes for fallocate(), direct I/O and page_mkwrite(), which never reserved anything. Those allocations release reservations that belong to dirty data of other files, and can use clusters that are promised to that data, so writeback of the delayed data can later run out of space although every earlier check succeeded. Reproducer: write 1 MiB to file A without syncing (statfs() free space drops by 256 clusters), then fallocate() 1 MiB on file B: free space does not drop at all. Only treat the allocation as consuming a reservation when the run being filled is a delalloc run. Filling a hole now fails with ENOSPC when the remaining free clusters are all reserved for delayed data, so a write fault into a hole of an mmap()ed file gets SIGBUS in that case, as on other filesystems with delayed allocation. Fixes: 495e90fa3348 ("ntfs: update attrib operations") Cc: stable@vger.kernel.org Signed-off-by: Ernest Borowski --- fs/ntfs/attrib.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/fs/ntfs/attrib.c b/fs/ntfs/attrib.c index 4028c310fc..cdac846f37 100644 --- a/fs/ntfs/attrib.c +++ b/fs/ntfs/attrib.c @@ -5520,8 +5520,14 @@ int ntfs_attr_map_cluster(struct ntfs_inode *ni, s64= vcn_start, s64 *lcn_start, } } =20 + /* + * Only a delayed-allocation run holds a reservation in + * vol->dirty_clusters for the allocator to consume. Holes filled by + * fallocate(), direct I/O or page_mkwrite() have none and must leave + * the reservations of other writers alone. + */ rlc =3D ntfs_cluster_alloc(vol, vcn, clu_count, lcn_seek_from, DATA_ZONE, - false, true, true); + false, true, lcn =3D=3D LCN_DELALLOC); if (IS_ERR(rlc)) { err =3D PTR_ERR(rlc); goto out; --=20 2.56.1 From nobody Tue Nov 24 11:44:05 2026 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1795371590; b=Ot6565WehjjS/7/k0t3vLNGCmfIAzPvE2gZZxvv57tNYBbhXhWTMVngRvpNwyHnMFff7 HnUZj674ddjdNkX/wlkyvEgrAOxXQ50nrfWKv8ZunE47J80SrrbfIzIMhuDqex00E3dfN Tmj62SNVZ/P9pp1xBRjET2kQ8Bc6/YJY2ZedDodjcGBv1lbTWPAiCqhkZjDEX7WfbOQjP AyWSOK1CKEuP7Lms9cXd3VE4s6Tk26gtAsncvz1ujcToSNUpPkL9e7i2CeDuIR02Fz3u6 Fe7Enc2NfjAYqMZM1iTm5zBa0FH+jgda79OvJOT6X3Tff4GvOp74eokOCiP2izmQhzg== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795371590; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=7XzCl99XSt0jQeYSzp9DDMkSNqpxrpMBcV0n58ptXLg=; b=saH6Oyi4ffw9jaeYqBFaJbzpJ5QzWtEJn/mtZ7hsyNiKosmTUnB8y1vk8o5VxgpqmFHR LHz9kwDtaNfFvxIeyUFtm+aJjnmpT+FWHpEnKE0fJcsKke/q5Mt0wQmzOJ9/tqtomOFy2 e1N9loPpaFyuodJa1/p6kw3xY2vXDYALWxv85hs3BhYI0PtHflF4lBaj9tr9xYbfCv6ep SulNa3HGLSOmBL/baevuQVw2VDwev5XtmIBgVUDA9sNY/5xB3ywLDmJx3ITw4gfSE60X/ q66BpSiExattafbxeZKMYhyyucCAG8/MnXiI7U3EB1EWbcmAq1YfD/bLc3t7kYw1C8Q== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=bH1t+tj7; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CC111606A681; Sun, 22 Nov 2026 18:19:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=bH1t+tj7 Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1795371590; b=utjwlqkYHKzIUVqloITEPjk03w3aHbIhSRnUivoPjvu3Z6QwswHBMxCI9FwEmEiXa1Gl koChkucSPHtP7qXCrfY0qHIOJaDzoOcjBTj6xFMkaWb43LGdvE6G6usZt6hHBizSd7FMS x/Bq4guHuahxULVGxsamGk0qCm/nkayMcvSnYbNGciFilSAetpGElJZtii51EOGoSaNuV +TjIo5x+/BsL0w3WKJ04ng1KzZizMiFgP/ZTuhFBtdGhCwhlguMqQByanJlCSTx6d3qYV 7O1MxV+SAijmFD8duzg95GCSLbtOCo736xzazqNRur3wDpbQM2GshKoNggDH6RzgkSw== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1795371590; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=7XzCl99XSt0jQeYSzp9DDMkSNqpxrpMBcV0n58ptXLg=; b=O4fpinc99coP29vFNvyQljQwbsJEQ8z58r3aLfi73WsD5LQStjMbHXCyUrEuMRVKv+Tv eqJ4MLZkU+mlbBIL8hflhKAkeaQQ5VNa8I+UQ8h76qcXpwcfeFcWyH8vWMhhVXC3lSmI/ VJfB+rHUAFVPJD4e0yRVqXn2o8kQvWAL9KiTtirKss1gAJmnnvs5WWQkRpwX0BqBh0M3e RlUP9dLBqwUN8OwoAFsCitHENEq1UOW4HuVpzOZqy4xdOom9kYZ2dwWsFtyz6jF0Kfs9Q TfZ34INuhsJ0DmxhwZEHssJ/AFMiWw0jIwBFR5OZjl/C5uA5qikVEwh2ei9tTp9uL3A== ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.kernel.org (Postfix) with ESMTPS id 44BF81F000D0; Sun, 22 Nov 2026 18:19:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795371590; bh=7XzCl99XSt0jQeYSzp9DDMkSNqpxrpMBcV0n58ptXLg=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=bH1t+tj7cTnygUVy1Om0x5gAmcOK+W001/1WpoTWOqUTPcbQ7vAEjEnI+cMhBUC/N FcPdVQBsm4DLypQuCfA83HM1nF1sVF4lZOVi52sgIyw6ep6x2M3MHCxx0YfHhFgmsd GQRciYGoYzKgSBJCfWbZAowMOEGLFDN0nW+n2UrEQ+3cm939zTvw9dnJJlTrhYisK4 UxvlFhgB/b1EUOwWdcsq2s4OF9Gqyu/9QVEtmZ7UG0PxwhbN99s86Gv+CGwIzbkxqo 7m5qK0qq6PCwoYimOwlm4LWN1Qhzbg1uPIWhAdNC/H5zzAyaUdVKYxtSLgAWnTfU/u 21DSE25oM5VNw== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id C5F514C180; Sun, 22 Nov 2026 18:19:38 +0000 (UTC) From: Ernest Borowski via B4 Relay Date: Sun, 22 Nov 2026 19:19:39 +0100 Subject: [PATCH 3/7] ntfs: release the delalloc reservations of an evicted inode 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-Type: text/plain; charset="utf-8" Message-Id: <20261122-b4-ntfs-delalloc-fixes-v1-3-0726f0dce403@proton.me> References: <20261122-b4-ntfs-delalloc-fixes-v1-0-0726f0dce403@proton.me> In-Reply-To: <20261122-b4-ntfs-delalloc-fixes-v1-0-0726f0dce403@proton.me> To: Namjae Jeon , Hyunchul Lee , Baolin Liu Cc: ntfs@lists.linux.dev, linux-kernel@vger.kernel.org, Zhan Xusheng , stable@vger.kernel.org, Ernest Borowski X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=4574; i=ernest.borowski.git@proton.me; s=default; h=from:subject:message-id; bh=jX0WnDWyu6iBmr0vTGV8OHp2dfN/AQIQZ7hTHKPYgG4=; b=owGbwMvMwCVWzJ65uCpJ5D/jabUkhmxmIwvP1qsrjRryHpuXyn80iY6b9tiGl/144ZQuT76A/ x4HGad2lLIwiHExyIopsnT8vxHiE/Lt07I4rT6YOaxMIEMYuDgFYCIzFjP8D88N0rYVkoxob/sj UpdRJckVwDdv8gQJHx91pzOHthj+YPjvruGUzLP05SvGlU+5ZRhkfl4TFktssv8hIdXFcbhczo4 fAA== X-Developer-Key: i=ernest.borowski.git@proton.me; a=openpgp; fpr=88FFD8544C54F6F2A65E2A8E730769A37A6214FF X-Endpoint-Received: by B4 Relay for ernest.borowski.git@proton.me/default with auth_id=1589 X-Original-From: Ernest Borowski Reply-To: ernest.borowski.git@proton.me Content-Transfer-Encoding: quoted-printable From: Ernest Borowski When an inode is evicted with LCN_DELALLOC runs left in its runlist, truncate_inode_pages_final() drops their dirty folios and the clusters reserved for them in vol->dirty_clusters are never given back. Only the deletion of a base inode released anything, using the per-inode i_dealloc_clusters counter. This leaks free space until unmount when a named stream with delayed data is removed (ntfs_stream_finish_remove() truncates the page cache and ntfs_attr_rm() frees only real clusters), when a file is unlinked while one of its named streams has delayed data (ntfs_cleanup_deleted_inode() drops the stream inode, whose reservation is not covered by the base inode's counter), and when an inode that keeps a delalloc run no dirty folio covers (see the next patches) is evicted. Reproducer: mount with streams_interface=3Dwindows, write 1 MiB to f:s, rm f:s (or rm f), sync: statfs() reports 256 (4 KiB) clusters less free space than after a remount. Release the reservations of whatever delalloc runs are left in the runlist when any ntfs inode is evicted, and drop the counter-based release from the deletion path, which would release them twice. Fixes: b041ca562526 ("ntfs: update iomap and address space operations") Cc: stable@vger.kernel.org Signed-off-by: Ernest Borowski --- fs/ntfs/attrib.c | 12 +++++++----- fs/ntfs/attrib.h | 1 + fs/ntfs/inode.c | 12 +++++++++++- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/fs/ntfs/attrib.c b/fs/ntfs/attrib.c index cdac846f37..39fa983cd8 100644 --- a/fs/ntfs/attrib.c +++ b/fs/ntfs/attrib.c @@ -4390,13 +4390,15 @@ static int ntfs_attr_make_resident(struct ntfs_inod= e *ni, struct ntfs_attr_searc * * Delayed allocation reserves vol->dirty_clusters for every LCN_DELALLOC * cluster when the buffered write is done and only gives it back when - * writeback allocates the clusters. Runs cut off by a truncate never rea= ch - * writeback (their dirty folios are dropped from the page cache), so their - * reservation has to be released here or it is lost until unmount. + * writeback allocates the clusters. Runs cut off by a truncate, or left = in + * the runlist of an inode that is evicted, never reach writeback (their d= irty + * folios are dropped from the page cache), so their reservation has to be + * released here or it is lost until unmount. * - * The runlist must be mapped completely and locked for writing. + * The runlist must be locked for writing. It need not be mapped complete= ly: + * delalloc runs only exist in fragments of the runlist that are mapped. */ -static void ntfs_release_delalloc_from(struct ntfs_inode *ni, s64 start_vc= n) +void ntfs_release_delalloc_from(struct ntfs_inode *ni, s64 start_vcn) { struct runlist_element *rl =3D ni->runlist.rl; s64 nr =3D 0; diff --git a/fs/ntfs/attrib.h b/fs/ntfs/attrib.h index 9d6bf03202..faa2dfba3a 100644 --- a/fs/ntfs/attrib.h +++ b/fs/ntfs/attrib.h @@ -109,6 +109,7 @@ int ntfs_attr_fallocate(struct ntfs_inode *ni, loff_t s= tart, loff_t byte_len, bo int ntfs_non_resident_attr_insert_range(struct ntfs_inode *ni, s64 start_v= cn, s64 len); int ntfs_non_resident_attr_collapse_range(struct ntfs_inode *ni, s64 start= _vcn, s64 len); int ntfs_non_resident_attr_punch_hole(struct ntfs_inode *ni, s64 start_vcn= , s64 len); +void ntfs_release_delalloc_from(struct ntfs_inode *ni, s64 start_vcn); int __ntfs_attr_truncate_vfs(struct ntfs_inode *ni, const s64 newsize, const s64 i_size); int ntfs_attr_expand(struct ntfs_inode *ni, const s64 newsize, const s64 p= realloc_size); diff --git a/fs/ntfs/inode.c b/fs/ntfs/inode.c index 4fbdcd4596..911e68fa91 100644 --- a/fs/ntfs/inode.c +++ b/fs/ntfs/inode.c @@ -430,7 +430,6 @@ static int ntfs_non_resident_dealloc_clusters(struct nt= fs_inode *ni) } } =20 - ntfs_release_dirty_clusters(ni->vol, ni->i_dealloc_clusters); ntfs_attr_put_search_ctx(actx); return err; } @@ -2459,6 +2458,17 @@ void ntfs_evict_big_inode(struct inode *vi) =20 truncate_inode_pages_final(&vi->i_data); =20 + /* + * The dirty folios dropped above never reach writeback. Give back + * the reservations of the delalloc runs they had, for named streams + * and attribute inodes as well as for base inodes. + */ + if (NInoNonResident(ni) && ni->runlist.rl) { + down_write(&ni->runlist.lock); + ntfs_release_delalloc_from(ni, 0); + up_write(&ni->runlist.lock); + } + if (!vi->i_nlink) { if (!NInoAttr(ni)) { /* Never called with extent inodes */ --=20 2.56.1 From nobody Tue Nov 24 11:44:05 2026 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1795371589; b=k7SpFAwE/f/b23V8ntWQ/yeRq/tU2OnU/3rqjSZPByhd/dnK/vD8KCWLkhjsaQdu6t63 s220g68nQxtNF/BIf2+oABt8loJkwXUZK04ZB3TKA0sVhwt0icfAmOCa723+KuJ5R26RQ crRj7sKiN3ayAgAvuoxHvCJELtC+x8M5PwHdUuDxhDvxKFKuEQu9Tsm0SvaQhVjonRTa+ TWauzO0m6aRmeMP1I6AJo1dzy66U1iSMxzx39xSJAb4QUyVreH5pzpAL/OV0nxJNHZV+h gWBX0T4Cqg2HNFAL+Lhb+HPBwJk9er1YQm86+9IPAdjoSncjZ6DaSqS6nK2LtfrjpiA== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795371589; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=RI4kRWxl4XYAMairOnQShzPIXYese8AvwwdfRR+uYTw=; b=EUuidJxIaqBHIBcyTBu1UJdFLix2bHhVhYerRCGaMlvRLhq44BWRv1+T554v6VAwkiE+ DWoOG3JnVa3Gwwz4eNs+/oLewfcUEAD/JiGhLXnweKFcRWCdvyYgKMcbBM71STEYnBXcF uS8gBKUtEzP9uqAB+NYQerLgTkO7hWNXcWuQki+PTv7lRoJdel3O1lSaM4xdLvn3AgaVK imOt9ZFLPspnRraRdhnALZpmQDIvktjMo06jj33+R4G3MQ6drLN8foDPhOduTuDaliiRW yuphcud2wPiTwIyJuk7mnTQuZO2R1xVsf3fWJ4FC++h2jZGaqxYdX5iWoFuX/qaZxDg== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b="Zvcv/9e4"; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 72CC3606A330; Sun, 22 Nov 2026 18:19:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Zvcv/9e4 Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1795371589; b=ZI51zN6HeGqgAuTrHcpTDy9bH6wcWfGpBcgJClVdTzdRmUOQXOhSb5vFdbg9ohln6eYz i4FkMpxQb+ZIQW8JF6wy3at4MQE9smLYmjmNTlLjuuXcG76IzlasKcRU2WBF/nEjz0PgO Mh3FmjViWjGtpbKKI1y5XcbTvhTenb/hyLmZdyq3QkvPu/6Pd1SvrNoZ57JQCAyi5HPAH VWm3XWph7uKDjk0aaZjpK2rDQpAXAUHl2SCb9bSwEt+aT9299pgKcU01D1er5kvRfNYvB 89XDfV5R4KGQEvc7M5CfJGGcQe3TRc7nXuOdj+IWf3SDP/vM4X/T8CPfOSPrtOCj3Vw== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1795371589; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=RI4kRWxl4XYAMairOnQShzPIXYese8AvwwdfRR+uYTw=; b=xUVbEUH0BfCMrhMmPjlOWDG5hc2eQR+vEWuE7vO/fm+bJxXUxHicBvqUbBLUmdeCC/Qe ZJA66KBq+llNFoZRGIrfUrX/eEVZpblGL1gsnTWTZf1qkTk5IaHAOndB4mr/Ns5PBudMA lKxth8Sump/iCp49K+TVtScQNH9d7sP3gklwWj1UQewEzCkWkHI9CWlMg7z65tejsD/hA M3PK61QBxMIR9tmv2wsf3Hnk5bdv95PRqHxaPZeIfFe2sqGzD6CxGGv0Zi7QD5j41hnwt fOMAEb/qdIu7v4gIxs4MU53zOJaII4zea4kUw2XAkfXUlqP4hdlhQGbt5xNnz5Rc7hA== ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.kernel.org (Postfix) with ESMTPS id 5D7FE1F000D1; Sun, 22 Nov 2026 18:19:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795371589; bh=RI4kRWxl4XYAMairOnQShzPIXYese8AvwwdfRR+uYTw=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=Zvcv/9e4o9cEt5RidnZkS0BNvjR/Djm88RDm/0y974XNc6p74OmvR+3khITH18kQX xWtEsWiS7khQqY0TD+t1WQNd4GCLvGUT3k9PJ6F/F1mq4cCgs4y3P7NIv3jz3YJI3f Hcdtu0QhfhwKvtfL9soo5lSJOApw98FE38CNUVsX3S0YI8brZpMRJNXF1TwTlKpWjO 4GGi92WLX5plZOiPjRFiYjsSUk5vHc1+j0uWeuzbRN1U3ChRIrTQLBYYTAcqqq8WP1 QqEw2eJymUgw7SP1InMfMReXi7IQ/lteIFGDqByWADanlzPOgYoBMnRlwKLptH+5m7 NGnj8UHIt3sOg== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id DFE4B4C184; Sun, 22 Nov 2026 18:19:38 +0000 (UTC) From: Ernest Borowski via B4 Relay Date: Sun, 22 Nov 2026 19:19:40 +0100 Subject: [PATCH 4/7] ntfs: do not shift the cluster number of a delalloc run on split 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-Type: text/plain; charset="utf-8" Message-Id: <20261122-b4-ntfs-delalloc-fixes-v1-4-0726f0dce403@proton.me> References: <20261122-b4-ntfs-delalloc-fixes-v1-0-0726f0dce403@proton.me> In-Reply-To: <20261122-b4-ntfs-delalloc-fixes-v1-0-0726f0dce403@proton.me> To: Namjae Jeon , Hyunchul Lee , Baolin Liu Cc: ntfs@lists.linux.dev, linux-kernel@vger.kernel.org, Zhan Xusheng , stable@vger.kernel.org, Ernest Borowski X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=3638; i=ernest.borowski.git@proton.me; s=default; h=from:subject:message-id; bh=QYrY3PPRwc1pxE1XgYFpRxvEIryQYj/9Bk2ICaRmWlw=; b=owGbwMvMwCVWzJ65uCpJ5D/jabUkhmxmI8uYJ2p3p3zxX3m5Z+l87XsPRQRffrlbHGnPoXIvT 2tDVuXkjlIWBjEuBlkxRZaO/zdCfEK+fVoWp9UHM4eVCWQIAxenAEykbg/DX5EcxajDEzf/Xf5G ZlJS+6FnbK2lXxZLbVq8+efZ7578meqMDAdieUtfsxmmlhy5cX3t60fV3f6TGM/Hae05E7HX4V0 XFzcA X-Developer-Key: i=ernest.borowski.git@proton.me; a=openpgp; fpr=88FFD8544C54F6F2A65E2A8E730769A37A6214FF X-Endpoint-Received: by B4 Relay for ernest.borowski.git@proton.me/default with auth_id=1589 X-Original-From: Ernest Borowski Reply-To: ernest.borowski.git@proton.me Content-Transfer-Encoding: quoted-printable From: Ernest Borowski ntfs_rl_punch_hole() and ntfs_rl_collapse_range() advance the lcn of a run they split with "if (lcn > LCN_HOLE) lcn +=3D n". LCN_DELALLOC (-1) is greater than LCN_HOLE (-2), so splitting a delalloc run turns its tail into a run of real clusters starting at n - 1. ntfs_rl_insert_range() advances it unconditionally; its callers insert holes, so only a hole split for a non-hole source and a split delalloc run are affected. ntfs_rle_lcn_contiguous() has the same problem and can merge a delalloc run with a following real run. The punched-out part then goes to ntfs_cluster_free_from_rl(), which frees clusters that belong to other files or to metadata, and the part left in the runlist maps the file onto them. Delalloc runs normally do not survive the writeback that fallocate() does first, but a buffered write that fails after ->iomap_begin() leaves one without dirty folios (fixed by the next patch); FALLOC_FL_PUNCH_HOLE or COLLAPSE_RANGE through it then corrupts the volume (ntfsck: "Found 1 inconsistent bitmap block(s)"). Only shift the lcn of runs that have real clusters. Fixes: 11ccc9107dc4 ("ntfs: update runlist handling and cluster allocator") Cc: stable@vger.kernel.org Signed-off-by: Ernest Borowski --- fs/ntfs/runlist.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/fs/ntfs/runlist.c b/fs/ntfs/runlist.c index 76ba6ab72b..1e87db35ce 100644 --- a/fs/ntfs/runlist.c +++ b/fs/ntfs/runlist.c @@ -1770,7 +1770,7 @@ s64 ntfs_rl_get_compressed_size(struct ntfs_volume *v= ol, struct runlist_element static inline bool ntfs_rle_lcn_contiguous(struct runlist_element *left_rl= e, struct runlist_element *right_rle) { - if (left_rle->lcn > LCN_HOLE && + if (left_rle->lcn >=3D 0 && left_rle->lcn + left_rle->length =3D=3D right_rle->lcn) return true; else if (left_rle->lcn =3D=3D LCN_HOLE && right_rle->lcn =3D=3D LCN_HOLE) @@ -1832,7 +1832,8 @@ struct runlist_element *ntfs_rl_insert_range(struct r= unlist_element *dst_rl, int =20 i_rl[0].vcn =3D start_vcn; i_rl[0].length -=3D dst_rl_split.length; - i_rl[0].lcn +=3D dst_rl_split.length; + if (i_rl[0].lcn >=3D 0) + i_rl[0].lcn +=3D dst_rl_split.length; } else { struct runlist_element *dst_rle, *src_rle; merge_src_rle: @@ -1988,7 +1989,7 @@ struct runlist_element *ntfs_rl_punch_hole(struct run= list_element *dst_rl, int d =20 (*punch_rl)[0].vcn =3D start_vcn; (*punch_rl)[0].length -=3D first_cnt; - if ((*punch_rl)[0].lcn > LCN_HOLE) + if ((*punch_rl)[0].lcn >=3D 0) (*punch_rl)[0].lcn +=3D first_cnt; } =20 @@ -2008,7 +2009,7 @@ struct runlist_element *ntfs_rl_punch_hole(struct run= list_element *dst_rl, int d =20 new_rl[new_1st_cnt + 1].vcn =3D end_vcn + 1; new_rl[new_1st_cnt + 1].length -=3D first_cnt; - if (new_rl[new_1st_cnt + 1].lcn > LCN_HOLE) + if (new_rl[new_1st_cnt + 1].lcn >=3D 0) new_rl[new_1st_cnt + 1].lcn +=3D first_cnt; =20 if (one_split_3) @@ -2136,7 +2137,7 @@ struct runlist_element *ntfs_rl_collapse_range(struct= runlist_element *dst_rl, i =20 (*punch_rl)[0].vcn =3D start_vcn; (*punch_rl)[0].length -=3D first_cnt; - if ((*punch_rl)[0].lcn > LCN_HOLE) + if ((*punch_rl)[0].lcn >=3D 0) (*punch_rl)[0].lcn +=3D first_cnt; } =20 @@ -2150,7 +2151,7 @@ struct runlist_element *ntfs_rl_collapse_range(struct= runlist_element *dst_rl, i =20 new_rl[new_1st_cnt].vcn =3D end_vcn + 1; new_rl[new_1st_cnt].length -=3D first_cnt; - if (new_rl[new_1st_cnt].lcn > LCN_HOLE) + if (new_rl[new_1st_cnt].lcn >=3D 0) new_rl[new_1st_cnt].lcn +=3D first_cnt; =20 if (one_split_3) --=20 2.56.1 From nobody Tue Nov 24 11:44:05 2026 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1795371594; b=txR0vW/eqDlGlO4QX8yDUrd4KLaPRSqrjpdJ/H+7JHmAfjTQRJBSiRRCpQh5+FhdmiHX SxnsC1TJE0rQx7KxCJRN/Qqnv7LFxfU0HHNLfPCYKm3xfmX/u213AV5RieT38hLAvdGBz bG+tTDpjVMS4if+46ylcyfXFyNsDAbuN4OuUkk/kTxGfLQasLBvxpOmKR8V713lArDvzj 7a1IJUcfKZxtBXOXsyktOi/U59e3H7bgE/1sPJjDIwoELu6KpAPQtrByemJsf9BrtPuFV pLUfB4eFVhI3XaP5QbsCXyvE2kKYHvSRDn7Tzg93W9zkMEWAK4hDmLIdmi5JVB6+nQQ== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795371594; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=R0f3OJAgm4ujtP5f3KKgLJeANRzsR2n5JuwmMJaa2AQ=; b=40UyphIKWp4UXu0xQp555x18JnTW4c03bpRK7+UXd3dhwylms0ygD14Jtk2u4oCvV6BW I+CbrRt2PS0p6+ggcKV1/puej/8eLnVfUtUPTGBr0m3F3rviuH6GIuE0uVJX9WlVVKhhV UgRKjVW6X1Od8BwmBVYTqJRhOYKeVVazRvnVqDxGppNVLe+j7Cbcpt6NiXnLagBxg7etx ipA0/46t28DtKatPTN++Yh3wBrc8NLE4+2T/ccm19/7qqaDFOOKYbXIeuVg1Brp6YpchN 4t6huBEvDYDo7JWIvkGest+W67qiz++yTZwQb8ML/VEPdAQkqZ3I/94ps9OMC9hdwkA== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=AZFyoKX5; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 30903606A698; Sun, 22 Nov 2026 18:19:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=AZFyoKX5 Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1795371594; b=aX5S4ZlQ8satUouxw2xxfp2Hv6RPG+AH4+4XoONixL8bgbnnZuDJ7fhWo4E6joPEOxmZ lvf0i4ztRKmdq7ntYcMOiugrMLc8xmRp5vtuzh5gmg+KAp/4fC0jay4iuCeHBwIf8IIM3 RwgnW6pvtqqxUpdSqdAyybDMoLsDw4WYkHRUP2bHkam7+Vj9ODeOLDYv/0a4SzoVCddwB WoC7YqG6HBtwzOMAmWYWRzbB1qmfmCP/evxagRuAgg2RHD3+hf3MWWNi1xMVemxiKPbmy XTqA0chf5S38uHcG5K7xOJMURn12byqdO1gdc8l2cs1jFtI2xz8KBhJMocR4t7FPQJQ== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1795371594; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=R0f3OJAgm4ujtP5f3KKgLJeANRzsR2n5JuwmMJaa2AQ=; b=GssBGwkG0eKV2olunf4PImFakYHnW2MetgiysNI9v+W65gF+pHn/E/vLW0Srcjz46Mg7 Sx4NFVQabKBgz52o7G2Za/Hkennjm9NXFivMP3T1jtBoC4QUXWvutsEyj26y6lk4Lf/tb 8nYAmYBIo+GV7oR8DPPGCmP2kA3jWkYwgWTRz1Ky3CJAJDaigDUpHVAUAWFT5W2KGZUdI caTGxYJ9DTQgk8PtTTn1de5lwiXIH8nb+h8y3ha9C1/lc+e3UFc8JxmSL9y1BbtYCNQOT xs5ybp5RElmMwOCJksLy1/r1oz45BoBZKp5aBWhqrqlpG36BI2Qj3kxjWLkMRsY0p8A== ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.kernel.org (Postfix) with ESMTPS id 120641F000EB; Sun, 22 Nov 2026 18:19:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795371594; bh=R0f3OJAgm4ujtP5f3KKgLJeANRzsR2n5JuwmMJaa2AQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=AZFyoKX5hgGawpoP4c43DMrT/1VSjkua2wsRdaWz42i1mfjuKN6RKDB59itDYiyPv kjFgOFXZeRuWs1Yq2rADIc0GGxmG0PE+5XXhjvlTj/XMOsZ982odplET7SWjg/N6IK GsCvEICKScK/woKpvl1+BuUM5qbK+H2nE9bBacZxb6iQmtSZv2IkfJR9fvURQDGiSN rm+zXiUU9tiDxcPMGfXelUPiLt4XuIpf1Xya+CJsW2Ya0oCyz6BPFj/72doayhImr0 O1WUDvHpyVEdOALfc0AXj6xT0Eyp7JozMeZDqppB0XmpGKYOb4dZ5UtZJvCkahfdKb uVI0pOdJBQ18A== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 0423A4C188; Sun, 22 Nov 2026 18:19:39 +0000 (UTC) From: Ernest Borowski via B4 Relay Date: Sun, 22 Nov 2026 19:19:41 +0100 Subject: [PATCH 5/7] ntfs: punch out the delalloc reservation of a failed buffered write 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-Type: text/plain; charset="utf-8" Message-Id: <20261122-b4-ntfs-delalloc-fixes-v1-5-0726f0dce403@proton.me> References: <20261122-b4-ntfs-delalloc-fixes-v1-0-0726f0dce403@proton.me> In-Reply-To: <20261122-b4-ntfs-delalloc-fixes-v1-0-0726f0dce403@proton.me> To: Namjae Jeon , Hyunchul Lee , Baolin Liu Cc: ntfs@lists.linux.dev, linux-kernel@vger.kernel.org, Zhan Xusheng , stable@vger.kernel.org, Ernest Borowski X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=9090; i=ernest.borowski.git@proton.me; s=default; h=from:subject:message-id; bh=OfhotPEQBsrIVWZ7Allecp+BirX5T5buedwwuU3fnB8=; b=owGbwMvMwCVWzJ65uCpJ5D/jabUkhmxmI8s5glw5t/Yt3PbUV7VN+NXpzdkH+25mm2+e13DH4 YxT1oL8jlIWBjEuBlkxRZaO/zdCfEK+fVoWp9UHM4eVCWQIAxenAEwktYmRYdl5nnkdS9bYnJ/T kL5nlsn0i5+ny1pf/N7V7GE547HxkThGhgm8UZ690xsurwjw7RYy2bm2ctqkayaG9rk9a7qctU7 95gMA X-Developer-Key: i=ernest.borowski.git@proton.me; a=openpgp; fpr=88FFD8544C54F6F2A65E2A8E730769A37A6214FF X-Endpoint-Received: by B4 Relay for ernest.borowski.git@proton.me/default with auth_id=1589 X-Original-From: Ernest Borowski Reply-To: ernest.borowski.git@proton.me Content-Transfer-Encoding: quoted-printable From: Ernest Borowski ntfs_write_simple_iomap_begin_non_resident() turns the holes a buffered write covers into LCN_DELALLOC runs and reserves their clusters before any data is copied. If the copy then fails or comes up short (EFAULT from the user buffer, a fatal signal), the clusters that no dirty folio covers stay delalloc. Writeback never allocates them, so the reservation is kept until the inode is evicted, and the stale runs survive the writeback that fallocate() relies on before it edits the runlist. Reproducer: a pwrite() of 512 KiB from an unmapped address into a hole of a 1 MiB sparse file fails with EFAULT and leaves 128 (4 KiB) clusters reserved after sync. A FALLOC_FL_PUNCH_HOLE or COLLAPSE_RANGE through the range then hits the run splitting bug fixed by the previous patch. Do what XFS does: in ->iomap_end() of a delalloc mapping that was not written completely, let iomap_write_delalloc_release() find the ranges without dirty data and turn the clusters entirely inside them back into holes, releasing their reservation. Clusters that still hold dirty data stay delalloc, including the partial first and last clusters of an unaligned write that ->iomap_begin() zeroed in the page cache; those are written back as zeros, as before. Delalloc runs are holes on disk, so the mapping pairs do not change. The error path of ->iomap_begin() itself, taken when zeroing the edges of the new run fails, only subtracted the reservation and left the run and i_dealloc_clusters in place, so a later truncate or eviction would release it a second time. Use the same release there. Fixes: b041ca562526 ("ntfs: update iomap and address space operations") Cc: stable@vger.kernel.org Signed-off-by: Ernest Borowski --- fs/ntfs/attrib.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++ fs/ntfs/attrib.h | 1 + fs/ntfs/iomap.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++= +++- 3 files changed, 134 insertions(+), 1 deletion(-) diff --git a/fs/ntfs/attrib.c b/fs/ntfs/attrib.c index 39fa983cd8..7c19e4534a 100644 --- a/fs/ntfs/attrib.c +++ b/fs/ntfs/attrib.c @@ -4416,6 +4416,69 @@ void ntfs_release_delalloc_from(struct ntfs_inode *n= i, s64 start_vcn) ni->i_dealloc_clusters -=3D min_t(s64, nr, ni->i_dealloc_clusters); } =20 +/* + * ntfs_attr_punch_delalloc - turn delalloc runs back into holes + * @ni: non-resident ntfs attribute + * @start_vcn: first vcn of the range + * @len: number of clusters in the range + * + * Turn the LCN_DELALLOC parts of [@start_vcn, @start_vcn + @len) into hol= es + * and give back their reservation. Used when a buffered write that reser= ved + * them failed before it dirtied any folio over them, so that writeback wi= ll + * never allocate them. Runs that are not delalloc (anymore) are left alo= ne. + * Delalloc runs are stored as holes on disk, so the mapping pairs need no + * update. + * + * The caller holds the mrec_lock of the base inode and the runlist lock f= or + * writing. + */ +void ntfs_attr_punch_delalloc(struct ntfs_inode *ni, s64 start_vcn, s64 le= n) +{ + struct runlist_element *rl, *punch_rl; + s64 vcn =3D start_vcn, end_vcn =3D start_vcn + len, run_end; + size_t new_rl_count; + int rl_count, err; + + lockdep_assert_held_write(&ni->runlist.lock); + + err =3D ntfs_attr_map_whole_runlist(ni); + if (err) { + ntfs_error(ni->vol->sb, + "Failed to map the runlist to drop delalloc runs: %d", err); + return; + } + + while (vcn < end_vcn) { + rl =3D ntfs_rl_find_vcn_nolock(ni->runlist.rl, vcn); + if (!rl || !rl->length) + break; + run_end =3D min(rl->vcn + rl->length, end_vcn); + if (rl->lcn !=3D LCN_DELALLOC) { + vcn =3D run_end; + continue; + } + + for (rl =3D ni->runlist.rl, rl_count =3D 0; rl->length; rl++) + rl_count++; + rl =3D ntfs_rl_punch_hole(ni->runlist.rl, rl_count + 1, vcn, + run_end - vcn, &punch_rl, &new_rl_count); + if (IS_ERR(rl)) { + ntfs_error(ni->vol->sb, + "Failed to drop delalloc run at vcn 0x%llx: %ld", + vcn, PTR_ERR(rl)); + break; + } + kvfree(punch_rl); + ni->runlist.rl =3D rl; + ni->runlist.count =3D new_rl_count; + + ntfs_release_dirty_clusters(ni->vol, run_end - vcn); + ni->i_dealloc_clusters -=3D min_t(s64, run_end - vcn, + ni->i_dealloc_clusters); + vcn =3D run_end; + } +} + /* * ntfs_non_resident_attr_shrink - shrink a non-resident, open ntfs attrib= ute * @ni: non-resident ntfs attribute to shrink diff --git a/fs/ntfs/attrib.h b/fs/ntfs/attrib.h index faa2dfba3a..ca2a8b53a4 100644 --- a/fs/ntfs/attrib.h +++ b/fs/ntfs/attrib.h @@ -110,6 +110,7 @@ int ntfs_non_resident_attr_insert_range(struct ntfs_ino= de *ni, s64 start_vcn, s6 int ntfs_non_resident_attr_collapse_range(struct ntfs_inode *ni, s64 start= _vcn, s64 len); int ntfs_non_resident_attr_punch_hole(struct ntfs_inode *ni, s64 start_vcn= , s64 len); void ntfs_release_delalloc_from(struct ntfs_inode *ni, s64 start_vcn); +void ntfs_attr_punch_delalloc(struct ntfs_inode *ni, s64 start_vcn, s64 le= n); int __ntfs_attr_truncate_vfs(struct ntfs_inode *ni, const s64 newsize, const s64 i_size); int ntfs_attr_expand(struct ntfs_inode *ni, const s64 newsize, const s64 p= realloc_size); diff --git a/fs/ntfs/iomap.c b/fs/ntfs/iomap.c index ce916707f6..1909824c30 100644 --- a/fs/ntfs/iomap.c +++ b/fs/ntfs/iomap.c @@ -372,6 +372,10 @@ static int ntfs_zero_range(struct inode *inode, loff_t= offset, loff_t length) NULL); } =20 +static void ntfs_write_delalloc_release(struct inode *inode, loff_t pos, + loff_t length, ssize_t written, + unsigned int flags, struct iomap *iomap); + static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode,= loff_t offset, loff_t length, struct iomap *iomap) { @@ -543,7 +547,13 @@ static int ntfs_write_simple_iomap_begin_non_resident(= struct inode *inode, loff_ if (err =3D=3D -EPERM) err =3D 0; if (err) { - ntfs_release_dirty_clusters(vol, max_clu_count); + /* + * Nothing will be copied: drop the delalloc + * clusters that no zeroed folio covers. + */ + ntfs_write_delalloc_release(inode, offset, + iomap->length, 0, + IOMAP_WRITE, iomap); return err; } } @@ -762,6 +772,61 @@ static int ntfs_write_iomap_end_resident(struct inode = *inode, loff_t pos, return written; } =20 +static void ntfs_write_delalloc_punch(struct inode *inode, loff_t offset, + loff_t length, struct iomap *iomap) +{ + struct ntfs_inode *ni =3D NTFS_I(inode); + struct ntfs_inode *mrec_ni =3D ntfs_inode_is_named_stream(ni) ? + ntfs_base_inode(ni) : ni; + struct ntfs_volume *vol =3D ni->vol; + /* Clusters that still hold some dirty data stay delalloc. */ + loff_t start =3D round_up(offset, vol->cluster_size); + loff_t end =3D round_down(offset + length, vol->cluster_size); + + if (start >=3D end) + return; + + mutex_lock(&mrec_ni->mrec_lock); + down_write(&ni->runlist.lock); + ntfs_attr_punch_delalloc(ni, ntfs_bytes_to_cluster(vol, start), + ntfs_bytes_to_cluster(vol, end - start)); + up_write(&ni->runlist.lock); + mutex_unlock(&mrec_ni->mrec_lock); +} + +/* + * A buffered write that failed or copied less than it mapped leaves delal= loc + * clusters that no dirty folio covers: writeback never allocates them, so + * their reservation would stay until the inode is evicted, and the stale + * delalloc runs would outlive the flush done by fallocate() and friends. + * Punch them out again, keeping every cluster that has dirty data in the + * page cache. + */ +static void ntfs_write_delalloc_release(struct inode *inode, loff_t pos, + loff_t length, ssize_t written, + unsigned int flags, struct iomap *iomap) +{ + struct ntfs_volume *vol =3D NTFS_I(inode)->vol; + loff_t start_byte, end_byte; + + /* + * Delalloc is reserved per cluster, which can be larger than the + * block size iomap_last_written_block() would round to. + */ + if (written) + start_byte =3D round_up(pos + written, vol->cluster_size); + else + start_byte =3D round_down(pos, vol->cluster_size); + end_byte =3D round_up(pos + length, vol->cluster_size); + if (start_byte >=3D end_byte) + return; + + filemap_invalidate_lock(inode->i_mapping); + iomap_write_delalloc_release(inode, start_byte, end_byte, flags, iomap, + ntfs_write_delalloc_punch); + filemap_invalidate_unlock(inode->i_mapping); +} + static int ntfs_write_iomap_end(struct inode *inode, loff_t pos, loff_t le= ngth, ssize_t written, unsigned int flags, struct iomap *iomap) @@ -769,6 +834,10 @@ static int ntfs_write_iomap_end(struct inode *inode, l= off_t pos, loff_t length, if (iomap->type =3D=3D IOMAP_INLINE) return ntfs_write_iomap_end_resident(inode, pos, length, written, flags, iomap); + if (iomap->type =3D=3D IOMAP_DELALLOC && iomap->flags & IOMAP_F_NEW && + !(flags & IOMAP_FAULT) && written < length) + ntfs_write_delalloc_release(inode, pos, length, written, flags, + iomap); return written; } =20 --=20 2.56.1 From nobody Tue Nov 24 11:44:05 2026 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1795371600; b=YzYOBWqxtJVt5HGUxrfedftgbVdO2DGEjU2Ezkyl9bv/r/WAcs64cG/3aLLZjI8lrIFF Ene9vkvxFOV1+Q5AtvYjVr5VAE9dZqx2AFO86VjQUlmYqcAOY9QT2JqIH1wprWa0SJ6Qd P2D1moTE8p7zMgL2y8QrvcuSyY5BifQFxOIrcmGL2r04+ocv/mgIb+aG6v+ezsXmgSSqE mKIoaerJmnlJBSGtystMlMN3iyN0BBGwQxwFQ+ivh6W0mli7dfJhpt/PudAqvBR5ol37E IFKltLQE66+PF0ZnRZo/uIPvAFfPnebTF7nOjahNekP+QQSMU8XReR6iN8HqQHkvdRA== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795371600; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=a2Ffas3bLeeCGSUnr0XQ4vHOK5iuvqsq87RRMDoP0wk=; b=GJqPgOZ4hwNZd0Prxuaci9K19yaAyOIbLn/PS+9BdkKZVbPqlGTA2o/f5eQJBopaSr7T EMGw+35B5p8uJp1UhAx42HGem9mezGoheVNJ287FgqyGEZDXuaeN1dvzgf0mP9jQnNINZ YzO1GMDHFlSlGB3lHxhoN3BbXYTbGWP7OD11hrzLiQ4hWRS/yOs09ouk3UMrfLzi2zscD W90d816p+OcC5/W0J1jtmTETBVsCUQKmz3y471A0S5IFNVR0loSUK5ZsSvsXr5NQN2oO+ fD+UGnEzLsQTDTNoKTuEpXp5zUwizT08IzrGW7dLvxc31H/bXVi9S9XhFxYHzPPetng== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=ihYhQORB; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4DFAA606A682; Sun, 22 Nov 2026 18:20:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=ihYhQORB Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1795371599; b=xbjSoGQVW+NEEdHx94G+tEQTkZDw6oj7v8cjkmbwwmea0g5sh9IIs50DW4muJUkq6Uy3 qPh0dpGfpuLXPtdrilkE/4VQGfCXHrAXtgFlRGEBh0DWSQOnGR8VTWWnHNTVZiWBMmkwj 56LDRhyroi6YfTDA1IgsHRt5vKAzZBuOILK3O9DrAMeSnrHtPchxMazsEtrIDv72qBNXV 58D0/r5HRh8CSOk4QfmjoxffJ7CMUwypyfkDIXsHlBN3MW/+kvdfZcTSLdPZnUGAB8/Pq CM5fT0By930lOD1z3JqjDzPO3QoxAOOMwKhn8sdcTPT6HiSGL6/NEeogZV3KPmj9BYA== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1795371599; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=a2Ffas3bLeeCGSUnr0XQ4vHOK5iuvqsq87RRMDoP0wk=; b=C8rRdiL86s236FLpuCqL90CXmEdo8QH/dzn0un//My042dRITuvcQquhOl1efYGH3Gsm hYa0Lue507czOr6bLAMz3pKwJ3Vyv1ibyH+y4hUwsmdPNVuRbfQu01QwwimMdHiNgjD9l WRqoFyxvQGQo3VCTLwoQyWtPMUpt9zxt4HSEryQSkp7W/344wyJndarq3ACQes3S1Q4hW Fh8R71F5IXAjCbbUMIFu91DXp1Gt4UtaG1GqrQnFw/zJ8HOFHbAVargKETtjf7dTrPSTP h59DKAUhxLQ+SKqRXWrCpBgn8QqUFt6ALyW7urxAgyF58Zq2sLS7I3pdgNb+VDn/lvQ== ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.kernel.org (Postfix) with ESMTPS id 1492E1F000CA; Sun, 22 Nov 2026 18:19:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795371599; bh=a2Ffas3bLeeCGSUnr0XQ4vHOK5iuvqsq87RRMDoP0wk=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=ihYhQORBXf+PV4dfI6vq0t0EuJx7BPX5hCTQU0iKpXhJeePrU1K/5oscAUL/vJ9Dv x7TRkZj0V1PAmEUAeXpECx48dPFCz810v+0bafmhP/XyWRAd38j7FEQhsiZIZ3y7hr kP8HGMrR5mmG7TAR2mdeNB9thlLK68BhI22JzMpgFtp6Yy1Qvb/2RiL1oVAejmk6Ax VfNKiWDveEfBa74NdWocypHxXU3fAfgD3dkGvLB6O2BTHV5I4JQT96Zv7A56ks03IF 1Gi5o9dr6Kdbv4f/1QT00S+Ie5VLeO38X0XS31Ldk3ccNaBhHTInmjpVQqzEqEi/2h EkPdnmIwAdeIQ== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 1E2484C18C; Sun, 22 Nov 2026 18:19:39 +0000 (UTC) From: Ernest Borowski via B4 Relay Date: Sun, 22 Nov 2026 19:19:42 +0100 Subject: [PATCH 6/7] ntfs: zero new delalloc clusters even when writeback allocates them 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-Type: text/plain; charset="utf-8" Message-Id: <20261122-b4-ntfs-delalloc-fixes-v1-6-0726f0dce403@proton.me> References: <20261122-b4-ntfs-delalloc-fixes-v1-0-0726f0dce403@proton.me> In-Reply-To: <20261122-b4-ntfs-delalloc-fixes-v1-0-0726f0dce403@proton.me> To: Namjae Jeon , Hyunchul Lee , Baolin Liu Cc: ntfs@lists.linux.dev, linux-kernel@vger.kernel.org, Zhan Xusheng , stable@vger.kernel.org, Ernest Borowski X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=9573; i=ernest.borowski.git@proton.me; s=default; h=from:subject:message-id; bh=XEJQKTLIixRNBY3im1HoKIxgT03eHpIBNJyLu/JSGrU=; b=owGbwMvMwCVWzJ65uCpJ5D/jabUkhmxmI0u7b9dz9l2YezflCouk23Pl3exfz7NOe5q9ZIWoz OQPWnmqHaUsDGJcDLJiiiwd/2+E+IR8+7QsTqsPZg4rE8gQBi5OAZhI5EVGhstRM9wrDx8X01xc VvAzwzHVdj7jxt2xvpciPWKfzjD/Wc7wvy5mXtGNE7fnbpHcMulnguex0xaCb+vc9Rb/XdkUERz 3jREA X-Developer-Key: i=ernest.borowski.git@proton.me; a=openpgp; fpr=88FFD8544C54F6F2A65E2A8E730769A37A6214FF X-Endpoint-Received: by B4 Relay for ernest.borowski.git@proton.me/default with auth_id=1589 X-Original-From: Ernest Borowski Reply-To: ernest.borowski.git@proton.me Content-Transfer-Encoding: quoted-printable From: Ernest Borowski When a buffered write starts or ends inside a cluster that is a hole, ntfs_write_simple_iomap_begin_non_resident() turns the hole into an LCN_DELALLOC run and then zeroes the part of the first and last cluster that the write does not cover, in the page cache, so that writeback writes whole clusters. initialized_size already covers that part, and the clusters writeback allocates are not zeroed on disk. The zeroing runs after mrec_lock and the runlist lock are dropped, and its ->iomap_valid() check, ntfs_iomap_valid(), calls the mapping stale as soon as the run is no longer delalloc. Writeback that runs meanwhile (the background flusher, or FIEMAP_FLAG_SYNC and sync_file_range(), which do not take the inode lock) picks up the folios that were zeroed first, and ntfs_attr_map_cluster() allocates the whole delalloc run for them. iomap_zero_range() then stops with -EPERM from ntfs_zero_read_iomap_end(), which the caller ignores. The rest of the cluster is neither in the page cache nor on disk, so reads return what the reallocated clusters held before: data of deleted or truncated files. Reproducer: fill a file, fsync, truncate it to 0 and back up, then write small pieces at unaligned offsets into the holes while other threads call FIEMAP with FIEMAP_FLAG_SYNC. With 4 KiB and 64 KiB clusters the old contents show up within seconds, also without the FIEMAP threads. The validity check was there to keep the zeroing from overwriting data that an mmap write put into the new cluster in that window. Hold the invalidate lock across ->iomap_begin() of a buffered write instead, which keeps page faults out from the moment the hole becomes delalloc until it is zeroed, and zero with the plain seek mapping, which zeroes delalloc and mapped ranges alike, so that writeback allocating the run no longer matters. The error path of ->iomap_begin() now runs with the invalidate lock held. Fixes: b041ca562526 ("ntfs: update iomap and address space operations") Cc: stable@vger.kernel.org Signed-off-by: Ernest Borowski --- fs/ntfs/iomap.c | 127 ++++++++++++++++++++++++----------------------------= ---- 1 file changed, 55 insertions(+), 72 deletions(-) diff --git a/fs/ntfs/iomap.c b/fs/ntfs/iomap.c index 1909824c30..fd94d4b34f 100644 --- a/fs/ntfs/iomap.c +++ b/fs/ntfs/iomap.c @@ -297,39 +297,6 @@ static int ntfs_read_iomap_end(struct inode *inode, lo= ff_t pos, loff_t length, DEFINE_IOMAP_ITER_NEXT_END(ntfs_read_iomap_next, ntfs_read_iomap_begin, ntfs_read_iomap_end); =20 -/* - * Check that the cached iomap still matches the NTFS runlist before - * iomap_zero_range() is called. if the runlist changes while iomap is - * iterating a cached iomap, iomap_zero_range() may overwrite folios - * that have been already written with valid data. - */ -static bool ntfs_iomap_valid(struct inode *inode, const struct iomap *ioma= p) -{ - struct ntfs_inode *ni =3D NTFS_I(inode); - struct runlist_element *rl; - s64 vcn, lcn; - - if (!NInoNonResident(ni)) - return false; - - vcn =3D iomap->offset >> ni->vol->cluster_size_bits; - - down_read(&ni->runlist.lock); - rl =3D __ntfs_attr_find_vcn_nolock(&ni->runlist, vcn); - if (IS_ERR(rl)) { - up_read(&ni->runlist.lock); - return false; - } - lcn =3D ntfs_rl_vcn_to_lcn(rl, vcn); - up_read(&ni->runlist.lock); - return lcn =3D=3D LCN_DELALLOC; -} - -static const struct iomap_write_ops ntfs_zero_iomap_folio_ops =3D { - .put_folio =3D ntfs_iomap_put_folio, - .iomap_valid =3D ntfs_iomap_valid, -}; - static int ntfs_seek_iomap_begin(struct inode *inode, loff_t offset, loff_= t length, unsigned int flags, struct iomap *iomap, struct iomap *srcmap) { @@ -337,17 +304,6 @@ static int ntfs_seek_iomap_begin(struct inode *inode, = loff_t offset, loff_t leng srcmap, false, false); } =20 -static int ntfs_zero_read_iomap_end(struct inode *inode, loff_t pos, loff_= t length, - ssize_t written, unsigned int flags, struct iomap *iomap) -{ - if ((flags & IOMAP_ZERO) && (iomap->flags & IOMAP_F_STALE)) - return -EPERM; - return written; -} - -static DEFINE_IOMAP_ITER_NEXT_END(ntfs_zero_read_iomap_next, - ntfs_seek_iomap_begin, ntfs_zero_read_iomap_end); - DEFINE_IOMAP_ITER_NEXT(ntfs_seek_iomap_next, ntfs_seek_iomap_begin); =20 int ntfs_dio_zero_range(struct inode *inode, loff_t offset, loff_t length) @@ -362,19 +318,28 @@ int ntfs_dio_zero_range(struct inode *inode, loff_t o= ffset, loff_t length) BLKDEV_ZERO_NOUNMAP); } =20 +/* + * Zero the part of a new delalloc cluster that the buffered write does not + * cover, in the page cache, so that writeback writes the whole cluster. + * Writeback can allocate the delalloc run while this runs (the folios + * zeroed first are dirty), so the zeroing must not depend on the mapping + * staying delalloc: iomap_zero_range() zeroes delalloc and mapped ranges + * alike. The caller holds the invalidate lock, which keeps page faults + * from putting data into the cluster meanwhile. + */ static int ntfs_zero_range(struct inode *inode, loff_t offset, loff_t leng= th) { - return iomap_zero_range(inode, - offset, length, - NULL, - ntfs_zero_read_iomap_next, - &ntfs_zero_iomap_folio_ops, + lockdep_assert_held_write(&inode->i_mapping->invalidate_lock); + + return iomap_zero_range(inode, offset, length, NULL, + ntfs_seek_iomap_next, &ntfs_iomap_folio_ops, NULL); } =20 -static void ntfs_write_delalloc_release(struct inode *inode, loff_t pos, - loff_t length, ssize_t written, - unsigned int flags, struct iomap *iomap); +static void __ntfs_write_delalloc_release(struct inode *inode, loff_t pos, + loff_t length, ssize_t written, + unsigned int flags, + struct iomap *iomap); =20 static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode,= loff_t offset, loff_t length, struct iomap *iomap) @@ -528,8 +493,7 @@ static int ntfs_write_simple_iomap_begin_non_resident(s= truct inode *inode, loff_ z_start, z_end - z_start); } - if ((!err || err =3D=3D -EPERM) && - max_clu_count > 1 && + if (!err && max_clu_count > 1 && (iomap->length & vol->cluster_size_mask && end < ni->initialized_size)) { loff_t z_start, z_end; @@ -544,16 +508,15 @@ static int ntfs_write_simple_iomap_begin_non_resident= (struct inode *inode, loff_ z_end - z_start); } =20 - if (err =3D=3D -EPERM) - err =3D 0; if (err) { /* * Nothing will be copied: drop the delalloc * clusters that no zeroed folio covers. */ - ntfs_write_delalloc_release(inode, offset, - iomap->length, 0, - IOMAP_WRITE, iomap); + __ntfs_write_delalloc_release(inode, offset, + iomap->length, 0, + IOMAP_WRITE, + iomap); return err; } } @@ -721,16 +684,27 @@ static int ntfs_write_iomap_begin_non_resident(struct= inode *inode, loff_t offse struct ntfs_inode *mrec_ni =3D ntfs_inode_is_named_stream(ni) ? ntfs_base_inode(ni) : ni; =20 - mutex_lock(&mrec_ni->mrec_lock); + if (ntfs_iomap_flags & NTFS_IOMAP_FLAGS_BEGIN) { + int err; =20 - if (ntfs_iomap_flags & NTFS_IOMAP_FLAGS_BEGIN) - return ntfs_write_simple_iomap_begin_non_resident(inode, offset, - length, iomap); - else - return ntfs_write_da_iomap_begin_non_resident(inode, - offset, length, - flags, iomap, - ntfs_iomap_flags); + /* + * A hole becomes delalloc here and the rest of its partial + * clusters is zeroed after the locks below are dropped: keep + * page faults, which could write into those clusters, out + * until then. + */ + filemap_invalidate_lock(inode->i_mapping); + mutex_lock(&mrec_ni->mrec_lock); + err =3D ntfs_write_simple_iomap_begin_non_resident(inode, offset, + length, iomap); + filemap_invalidate_unlock(inode->i_mapping); + return err; + } + + mutex_lock(&mrec_ni->mrec_lock); + return ntfs_write_da_iomap_begin_non_resident(inode, offset, length, + flags, iomap, + ntfs_iomap_flags); } =20 static int __ntfs_write_iomap_begin(struct inode *inode, loff_t offset, @@ -802,9 +776,10 @@ static void ntfs_write_delalloc_punch(struct inode *in= ode, loff_t offset, * Punch them out again, keeping every cluster that has dirty data in the * page cache. */ -static void ntfs_write_delalloc_release(struct inode *inode, loff_t pos, - loff_t length, ssize_t written, - unsigned int flags, struct iomap *iomap) +static void __ntfs_write_delalloc_release(struct inode *inode, loff_t pos, + loff_t length, ssize_t written, + unsigned int flags, + struct iomap *iomap) { struct ntfs_volume *vol =3D NTFS_I(inode)->vol; loff_t start_byte, end_byte; @@ -821,9 +796,17 @@ static void ntfs_write_delalloc_release(struct inode *= inode, loff_t pos, if (start_byte >=3D end_byte) return; =20 - filemap_invalidate_lock(inode->i_mapping); iomap_write_delalloc_release(inode, start_byte, end_byte, flags, iomap, ntfs_write_delalloc_punch); +} + +static void ntfs_write_delalloc_release(struct inode *inode, loff_t pos, + loff_t length, ssize_t written, + unsigned int flags, struct iomap *iomap) +{ + filemap_invalidate_lock(inode->i_mapping); + __ntfs_write_delalloc_release(inode, pos, length, written, flags, + iomap); filemap_invalidate_unlock(inode->i_mapping); } =20 --=20 2.56.1 From nobody Tue Nov 24 11:44:05 2026 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1795371594; b=zcM83mfCjUPDcnsPzNCvai0PztWBxLI+UM/NHEFzA2b4LZrVn8PmWV5D+643Nxr6/mFS ZuihDxGlelqujl6RFtDuikEuFCM7Sz1ectWYflj5TM86l3c3sX7fsD6fhrxIUoK9NWp24 bTRs2ASU+xcMNtjTytR/3IkLjD7wgsHx9RI8bK6A88SW6rRQ4dmApITxXgFCSK0RAc/8a PYj/ZzDRR3h8DW/K2UMmgFkKk2ITddSIkvDErGG9yDIK2OMPL6sUySysa9SixFmekb+u+ ehuNUZHyyqGYyz8mNDe0FRwt8z1Bjg7RzfBh+gagj87DzMiK8XRhKOMgQ5W6XrByrjQ== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795371594; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=LwOLZT0yZls4qSknTvgbh/VunVz5eSP1RQX7iDD7b6Y=; b=O4FCYdrkFv828tMHdzjzIsqnVgQSyHCCDw7Cr5Eegz+FFpqlwDBy0uK1SbraCFgsx5YH jbMCt6Ib6OLzoJdrfzbVujlWS4ZJ6kVi6GtIoxAAmFAtMmN9S1o9LJP85lh+7+W6dFxyc jcMiqdsOl4MHlddV7mtymG18CSQ+dxv5SpdPy78PJsaJmMHaak2aWIwGARB63aqjoP1dT KIURwgfB7zajbfciEj/hiZ6kPI4WmfRKZNyIHEMZ6slJ4ByqpBSybUAi7VlzhfAqyWAmT wrckT7yoI9Fq1SBM2sy1vVEDvlef7x4aTqWwXFz7cdSgIkuXqxLnP7p5kBImOo8+4Nw== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b="Tk1TUsi/"; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3EE07606A692; Sun, 22 Nov 2026 18:19:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Tk1TUsi/ Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1795371594; b=DgQMdMquBujVIPYMGDUTFATkfANh/GTjCaDXzbIcjHFmAiI6dQmfmzzU0MP7cELMHgZd mXta+7jDpEFRVAtMS/3c7xxqfWYhx+XpewFRCikaNHlVsPr07ejMgy+n9Abz00M4QsFYJ zXyWQ/cGRn54J+0aitgF6drq//dTH8mwMi2e6tbZ8YzQiEJjLMmpMjq9VzqKDmrZ28Id9 bYW3Og+ncyTmih9OE/AZrGL25XuuBPCNxABl+zOLAaemTmllHiC8TJujNYzxzzsP9Pueg fzr56MbHUKNFZkoFp7oFN5ZwifXL8YSZSz4eEaCEtwHNr22YZwM7z73Px6/rT4dsU5Q== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1795371594; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=LwOLZT0yZls4qSknTvgbh/VunVz5eSP1RQX7iDD7b6Y=; b=praZriNIMhq4D35PYqxYB4wKkEFiAJKQHjcLYWNYeh8VDbo73bM2zw5hVHqbIT17ANnU mFxfUB+cmUzmNo9XFfxRVw7eo3MOjjLHcxfKEN1H5wCJCXLadhbJ+AKuTTPQ15NucWtgc dhi3YLEOO4dx/7KlykQqZ/TpY93UFgV51ugLscUKIKFht9ppxIAkbT2OEEyU00nnGDofb jczw40cvVX3TkgqwkgOBv+Gz2TW605o77YIfzWwFx1P7gEJZ1IG34pq9kn6nbaCqbLdba 5OtqV0bQbYmjEllHSRWIKQnwVR1WpWeUP2u7riCHOZqiIRaOKZsfnIblaKM4t3K8IJA== ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.kernel.org (Postfix) with ESMTPS id BC7CC1F000C4; Sun, 22 Nov 2026 18:19:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795371594; bh=LwOLZT0yZls4qSknTvgbh/VunVz5eSP1RQX7iDD7b6Y=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=Tk1TUsi/paeu0LiNj0vnAYAbRRrSG69tERHDTiFCqyX5NzV3tzytYgy+muxuVndFb ytOegGHYsXYXYO2RkbAm9a4Ni+lIc/prrsCvofGE8igAUTWIYsFVq8hTePBLE8k4rZ IPjKAPflu9DcKmRMCDxi572vgIpinDYLHfs4zxDN7v/x5+6pqT0lxZlVB7xFZ+mmCe Ur47YY1rlCB2ar6HKAPHEGGWko6apPAS07RT1ACmP6lY9aQG8D4wqi1IOea5PTova5 5SxCPn5MMgQ8AHgzMxdM3ArdQ7vA73rMDgjTj7Xou63UkZqX+v4MdoecN7YK4cHBtu ZTxvu7mtoTsaQ== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 383584C190; Sun, 22 Nov 2026 18:19:39 +0000 (UTC) From: Ernest Borowski via B4 Relay Date: Sun, 22 Nov 2026 19:19:43 +0100 Subject: [PATCH 7/7] ntfs: zero the rest of a new cluster after a short buffered write 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-Type: text/plain; charset="utf-8" Message-Id: <20261122-b4-ntfs-delalloc-fixes-v1-7-0726f0dce403@proton.me> References: <20261122-b4-ntfs-delalloc-fixes-v1-0-0726f0dce403@proton.me> In-Reply-To: <20261122-b4-ntfs-delalloc-fixes-v1-0-0726f0dce403@proton.me> To: Namjae Jeon , Hyunchul Lee , Baolin Liu Cc: ntfs@lists.linux.dev, linux-kernel@vger.kernel.org, Zhan Xusheng , stable@vger.kernel.org, Ernest Borowski X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=8086; i=ernest.borowski.git@proton.me; s=default; h=from:subject:message-id; bh=SIuoyumwdVQAIkLosER9C52F/l7mvPhg9EX1v73MzkE=; b=owGbwMvMwCVWzJ65uCpJ5D/jabUkhmxmI8ukzmsXhaes3bJOVeiF+7mXydbz0nPUvvGlJyR35 Vww2X+mo5SFQYyLQVZMkaXj/40Qn5Bvn5bFafXBzGFlAhnCwMUpABMRf8bI8LxtYXSPw7X5L+OD bTTXfwneLizyuegHw7Gc8zcaJ18u5Gb4p5/4PO00UxXDcT0lWyHRHHMxn+B5a0vzQ5VKL/4rFXr AAQA= X-Developer-Key: i=ernest.borowski.git@proton.me; a=openpgp; fpr=88FFD8544C54F6F2A65E2A8E730769A37A6214FF X-Endpoint-Received: by B4 Relay for ernest.borowski.git@proton.me/default with auth_id=1589 X-Original-From: Ernest Borowski Reply-To: ernest.borowski.git@proton.me Content-Transfer-Encoding: quoted-printable From: Ernest Borowski ntfs_write_simple_iomap_begin_non_resident() turns the holes a buffered write covers into LCN_DELALLOC runs, and zeroes in the page cache only the parts of the first and last cluster that the write will not cover. When the copy from user space stops inside a cluster (the source buffer runs into an unmapped page, or a fatal signal), only the bytes copied are dirty. Writeback allocates the whole cluster and writes the dirty blocks, and the rest of the cluster keeps what the reused clusters held before. Below initialized_size, for example a write into a hole of a sparse file, reads return that old data. Reproducer: fill a file, fsync, truncate it to 0 and back up, write the last byte, then pwrite() 2.5 clusters at a cluster boundary from a buffer whose readable part ends 5096 bytes in: the write returns 5096, and after fsync and drop_caches the rest of the second cluster (4 KiB clusters) or of the first (64 KiB clusters) reads back the old file contents. Mark the mappings that ->iomap_begin() made out of a hole, and on a short copy make writeback write the rest of the cluster the copy stopped in, before the unused delalloc clusters after it are punched out: under the invalidate lock, folios already in the page cache are dirtied as they are (they hold the zeroes __iomap_write_begin() put there, or data an mmap write added meanwhile, which must not be lost), and the rest of the range is zeroed while it is still delalloc (once a write fault or writeback has allocated it, an mmap write's data may have left the page cache; a follow-up series makes both zero a partly written cluster on the disk before mapping it). When the writer has a fatal signal pending, iomap_zero_range() refuses to run in its context (-EINTR), so the zeroing is done from a worker then. Fixes: b041ca562526 ("ntfs: update iomap and address space operations") Cc: stable@vger.kernel.org Signed-off-by: Ernest Borowski --- fs/ntfs/iomap.c | 138 ++++++++++++++++++++++++++++++++++++++++++++++++++++= ++++ 1 file changed, 138 insertions(+) diff --git a/fs/ntfs/iomap.c b/fs/ntfs/iomap.c index fd94d4b34f..9547136cd5 100644 --- a/fs/ntfs/iomap.c +++ b/fs/ntfs/iomap.c @@ -5,6 +5,7 @@ * Copyright (c) 2025 LG Electronics Co., Ltd. */ =20 +#include #include =20 #include "attrib.h" @@ -12,6 +13,9 @@ #include "ntfs.h" #include "iomap.h" =20 +/* iomap->flags: ->iomap_begin() turned a hole into this delalloc mapping = */ +#define NTFS_IOMAP_F_WAS_HOLE IOMAP_F_PRIVATE + static void ntfs_iomap_put_folio_non_resident(struct inode *inode, loff_t = pos, unsigned int len, struct folio *folio) { @@ -481,6 +485,9 @@ static int ntfs_write_simple_iomap_begin_non_resident(s= truct inode *inode, loff_ if (lcn <=3D LCN_HOLE) { loff_t end =3D offset + length; =20 + /* The whole mapping was a hole before this write. */ + iomap->flags |=3D NTFS_IOMAP_F_WAS_HOLE; + if (vcn_ofs || ((vol->cluster_size > iomap->length) && end < ni->initialized_size)) { loff_t z_start, z_end; @@ -800,11 +807,142 @@ static void __ntfs_write_delalloc_release(struct ino= de *inode, loff_t pos, ntfs_write_delalloc_punch); } =20 +struct ntfs_zero_work { + struct work_struct work; + struct inode *inode; + loff_t pos; + loff_t len; + int err; +}; + +static void ntfs_zero_range_workfn(struct work_struct *work) +{ + struct ntfs_zero_work *zw =3D container_of(work, struct ntfs_zero_work, + work); + + zw->err =3D iomap_zero_range(zw->inode, zw->pos, zw->len, NULL, + ntfs_seek_iomap_next, &ntfs_iomap_folio_ops, + NULL); +} + +/* + * Like ntfs_zero_range(), but also for a writer that has a fatal signal + * pending: iomap_zero_range() then gives up with -EINTR, and a write cut + * short by SIGKILL is one whose cluster tail must still be zeroed. Zero + * from a worker in that case, which has no signal to see; the caller's + * invalidate lock still keeps page faults out of the range. + */ +static int ntfs_zero_range_unkillable(struct inode *inode, loff_t pos, + loff_t len) +{ + struct ntfs_zero_work zw =3D { .inode =3D inode, .pos =3D pos, .len =3D l= en }; + + int err; + + if (!fatal_signal_pending(current)) { + err =3D ntfs_zero_range(inode, pos, len); + /* the signal may also come while it zeroes */ + if (err !=3D -EINTR || !fatal_signal_pending(current)) + return err; + } + + lockdep_assert_held_write(&inode->i_mapping->invalidate_lock); + INIT_WORK_ONSTACK(&zw.work, ntfs_zero_range_workfn); + queue_work(system_unbound_wq, &zw.work); + flush_work(&zw.work); + destroy_work_on_stack(&zw.work); + return zw.err; +} + +/* True if the cluster at byte @pos is still a delalloc run. */ +static bool ntfs_pos_is_delalloc(struct inode *inode, loff_t pos) +{ + struct ntfs_inode *ni =3D NTFS_I(inode); + s64 vcn =3D ntfs_bytes_to_cluster(ni->vol, pos); + struct runlist_element *rl; + s64 lcn; + + down_read(&ni->runlist.lock); + rl =3D __ntfs_attr_find_vcn_nolock(&ni->runlist, vcn); + lcn =3D IS_ERR(rl) ? LCN_EINVAL : ntfs_rl_vcn_to_lcn(rl, vcn); + up_read(&ni->runlist.lock); + return lcn =3D=3D LCN_DELALLOC; +} + +/* + * Make writeback write all of [start, end), the rest of a cluster that wa= s a + * hole and that a short buffered write filled only in part. Folios in the + * page cache keep their contents (the zeroes __iomap_write_begin() put + * there, or data an mmap write added meanwhile) and are dirtied as a whol= e; + * the rest of the range is zeroed while the cluster is still delalloc. O= nce + * a write fault or writeback has allocated it, the cluster was zeroed on = the + * disk before it was mapped, and what an mmap write put there since may o= nly + * have left the page cache: it must not be zeroed again. The caller holds + * the invalidate lock. + */ +static int ntfs_dirty_cluster_tail(struct inode *inode, loff_t start, + loff_t end) +{ + struct folio *folio; + loff_t next; + int err =3D 0; + + lockdep_assert_held_write(&inode->i_mapping->invalidate_lock); + while (!err && start < end) { + folio =3D filemap_lock_folio(inode->i_mapping, + start >> PAGE_SHIFT); + if (IS_ERR(folio)) { + next =3D min_t(loff_t, round_up(start + 1, PAGE_SIZE), + end); + if (ntfs_pos_is_delalloc(inode, start)) + err =3D ntfs_zero_range_unkillable(inode, start, + next - start); + } else { + bool uptodate =3D folio_test_uptodate(folio); + + next =3D min_t(loff_t, folio_pos(folio) + + folio_size(folio), end); + if (uptodate) + folio_mark_dirty(folio); + folio_unlock(folio); + folio_put(folio); + if (!uptodate && ntfs_pos_is_delalloc(inode, start)) + err =3D ntfs_zero_range_unkillable(inode, start, + next - start); + } + start =3D next; + } + return err; +} + static void ntfs_write_delalloc_release(struct inode *inode, loff_t pos, loff_t length, ssize_t written, unsigned int flags, struct iomap *iomap) { + struct ntfs_volume *vol =3D NTFS_I(inode)->vol; + filemap_invalidate_lock(inode->i_mapping); + /* + * A copy that stopped inside a cluster which was a hole leaves the + * rest of that cluster neither written nor zeroed, but writeback + * allocates the whole cluster and writes only the dirty blocks, so + * the old contents of the cluster would show through. + */ + if (written && iomap->flags & NTFS_IOMAP_F_WAS_HOLE) { + loff_t start =3D pos + written; + loff_t end =3D min_t(loff_t, round_up(start, vol->cluster_size), + i_size_read(inode)); + int err =3D 0; + + if (end > start) + err =3D ntfs_dirty_cluster_tail(inode, start, end); + if (err) { + ntfs_error(vol->sb, + "Failed to zero [%lld, %lld) of inode 0x%llx after a short write: %= d", + start, end, NTFS_I(inode)->mft_no, err); + NVolSetErrors(vol); + } + } __ntfs_write_delalloc_release(inode, pos, length, written, flags, iomap); filemap_invalidate_unlock(inode->i_mapping); --=20 2.56.1