From mboxrd@z Thu Jan 1 00:00:00 1970 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=1796409649; b=wVCRspBXhTeF5CK7wSO/RzB0CZ1L5UFnI8oYWnVwvZkRoZHE+LMUIOvf7AIfUbrSA0pV dwIvDgBXZiYtGjxkpuGWU9Kbz7omTiUWoPgKHIHc0xuvciRKQsXimstZRvHM0PFLALWeb 7EiJtRW0ykIwKOyIhwS8jALBtGYdwZx8vIuVz6cmFx4Im2oVUbVvtFG2cYcRpISgEtkrw TDga9wnD+e6uZGPJCnC2OEL06QdUbt8Vn/5K7XaL66Q+hKsGPvlSZKbu8U3ybe8zMonkX aHMsYIKkwFCzeubfs6MzKQennGKyvcj8E+DFJeZDV+Kv7NDM8ichcAzqhgJx6eWuPCQ== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796409649; 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=TtikVugELJD6s5pwCy3lkrRFlK2HOXS49s42V99AWgA=; b=Vwfd2XLaiu9RzF1zyQPUs13OSe/1nX4wa1w0uVhzFT23Kil2BHtmtgmM4QeYNsCDXSCh ncXrhry/Z8TeubbERMAxFEXbALHDQApH3dnyEL03mQW4HSQWzYmZX0P4THSjODWIHIO3p GRzGRe760V8a9WLFqzCJcDg2TdOLEABt519gNipGcb0hmdV569brJU+pVsAqGthcBrRs8 IjuW7alvwFC/0SXAZUzb64rW5NzL0nFP2SR3RLtJcj/bLsxy5tiudOP6wBFF0kSNH71lA QeIJvU76sA8L+3upWuEBd9EpBafHnm7xpyn1xtYcDuH0UgrSW4gkDEmrYF5fQ8CVjiw== 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=eTRAxm6X; 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 7A6B9606A68C; Fri, 4 Dec 2026 18:40: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=eTRAxm6X 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=1796409649; b=B8Q3Jmylf67ZkphGlQiDmOCQ61YguHctMIY0S+NwHLTqoaKBBCqAfdldQyxOx9aLEXxr GQUAmWMZFFJqBT2lqOYf8nLT28yOcXX+to7JXaE+riF5Cvlddm1UBSJ85iEDjkcyntDZa 5TA/WUm5fBn0cQnqoCSz/4HNAkCC2LnPZ0C8Rp8O0OvxQ+FTk32Ecevi7tRtVo2deDizQ HiD4oTw7yzaMLj4gZStmbaVLC1Db4iuGz71ZZveZLthYIUuZUfMc7YLZK6aBm90n6l+qm lSUZhCZQe59pVVFlAWGMfQRGdonBOnDbiXEFo2788dlRb4ckEjXpjZ02MqDU80V2gPA== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1796409649; 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=TtikVugELJD6s5pwCy3lkrRFlK2HOXS49s42V99AWgA=; b=sltgDn94sQGwXFTGquAHvtcoth5mjP6ORx4Ym+qtWqbnaReOnfAQ5BPrEyNXmjPdzkS+ bW/GVmj5pk+4nZ8j9uL8coJWFBXf7CJC/KyTTRjMylnhUCrgmPBz/rmjG1YZ2r6svmyTw U3PihBIPEqkz4pjc6gA2v1iwdBdd7quL89HYeSqROYg8I5EGnW7gFFmdiYptn6WriJmmO SKoufmCHA/vrwj0nxG44y/KVAdCtd69m1MdtKuEfIbnoja5PRtjMw1Dtno/7s+GqeIGIh nNZNDxzkbo/OgDgZleViNAq2VS1rxPhnDJhUpmVuWnOHPvIskqPQtE8aFXK535IPNXA== 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 C9A741F00A11; Fri, 4 Dec 2026 18:40:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796409649; bh=TtikVugELJD6s5pwCy3lkrRFlK2HOXS49s42V99AWgA=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=eTRAxm6X2pl/2i5YsvurU6IKaakJR7ILRwr4bbI8R+e9Q7f4GoNtZv19j3Ywo5XYf hyTrWQDW0TSPZ+XMjNxl4c3nMAzyFrEBM0Y1LtZaYEp58KWelQ1VR9JIIpFZ2LMsZU SQZcYJdK46VasYOmqBhNoBZafLAJrrstOxoxRNCwSASJH58IrYXQ1I0zpwLbC6CGyV Kr5+kZl+UA1qXTYlDRcqNKYMhID7DwgJkaBkHUOLakBx7NSCPJ+c5Cq6lNM9LxEtnh Qqj1DIvQQiuuHl6+6J1Dwmc6V25Log0NZ+to4UBAla28WeXz5c0mdAl/bPZtA2M3mW TLqUSQ2hXEDHg== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 407574DC4E; Fri, 4 Dec 2026 18:40:36 +0000 (UTC) From: Ernest Borowski via B4 Relay Date: Fri, 04 Dec 2026 19:40:40 +0100 Subject: [PATCH v5 09/13] 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: <20261204-b4-ntfs-delalloc-fixes-v5-9-1cd0267df2cd@proton.me> References: <20261204-b4-ntfs-delalloc-fixes-v5-0-1cd0267df2cd@proton.me> In-Reply-To: <20261204-b4-ntfs-delalloc-fixes-v5-0-1cd0267df2cd@proton.me> To: Namjae Jeon , Hyunchul Lee , Baolin Liu Cc: Christian Brauner , Christoph Hellwig , "Darrick J. Wong" , ntfs@lists.linux.dev, linux-fsdevel@vger.kernel.org, linux-xfs@vger.kernel.org, linux-kernel@vger.kernel.org, Zhan Xusheng , stable@vger.kernel.org, Baolin Liu , Ernest Borowski X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=9621; i=ernest.borowski.git@proton.me; s=default; h=from:subject:message-id; bh=kZK5qkY3S2WPiuYbdg5gSYLH64HwYwCUvm9jSfxH2lk=; b=owGbwMvMwCVWzJ65uCpJ5D/jabUkhmxhToW2a6XJVQJHHN25pO8eOM+ml/mIhe1/mN70aX+D/ A7PyMzvKGVhEONikBVTZOn4fyPEJ+Tbp2VxWn0wc1iZQIYwcHEKwETWFjAyTNnnXpxnEr3w+1Oz OROSElc5ynQqXGGQUp986InUjme1Vgz/Q/7aPqtqtp3290fp3mvPA/sqexezr5t4LLpsoYH2+c/ f2AE= 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: 7bit 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 Reviewed-by: Baolin Liu 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 dbf1ee9e4a..112319e19e 100644 --- a/fs/ntfs/iomap.c +++ b/fs/ntfs/iomap.c @@ -302,39 +302,6 @@ static int ntfs_read_iomap_end(struct inode *inode, loff_t pos, loff_t length, DEFINE_IOMAP_ITER_NEXT_END(ntfs_read_iomap_next, ntfs_read_iomap_begin, ntfs_read_iomap_end); -/* - * 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 *iomap) -{ - struct ntfs_inode *ni = NTFS_I(inode); - struct runlist_element *rl; - s64 vcn, lcn; - - if (!NInoNonResident(ni)) - return false; - - vcn = iomap->offset >> ni->vol->cluster_size_bits; - - down_read(&ni->runlist.lock); - rl = __ntfs_attr_find_vcn_nolock(&ni->runlist, vcn); - if (IS_ERR(rl)) { - up_read(&ni->runlist.lock); - return false; - } - lcn = ntfs_rl_vcn_to_lcn(rl, vcn); - up_read(&ni->runlist.lock); - return lcn == LCN_DELALLOC; -} - -static const struct iomap_write_ops ntfs_zero_iomap_folio_ops = { - .put_folio = ntfs_iomap_put_folio, - .iomap_valid = 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) { @@ -342,17 +309,6 @@ static int ntfs_seek_iomap_begin(struct inode *inode, loff_t offset, loff_t leng srcmap, false, false); } -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); int ntfs_dio_zero_range(struct inode *inode, loff_t offset, loff_t length) @@ -367,19 +323,28 @@ int ntfs_dio_zero_range(struct inode *inode, loff_t offset, loff_t length) BLKDEV_ZERO_NOUNMAP); } +/* + * 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 length) { - 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); } -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); static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_t offset, loff_t length, struct iomap *iomap) @@ -533,8 +498,7 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_ z_start, z_end - z_start); } - if ((!err || err == -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; @@ -549,16 +513,15 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_ z_end - z_start); } - if (err == -EPERM) - err = 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; } } @@ -726,16 +689,27 @@ static int ntfs_write_iomap_begin_non_resident(struct inode *inode, loff_t offse struct ntfs_inode *mrec_ni = ntfs_inode_is_named_stream(ni) ? ntfs_base_inode(ni) : ni; - mutex_lock(&mrec_ni->mrec_lock); + if (ntfs_iomap_flags & NTFS_IOMAP_FLAGS_BEGIN) { + int err; - 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 = 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); } static int __ntfs_write_iomap_begin(struct inode *inode, loff_t offset, @@ -807,9 +781,10 @@ static void ntfs_write_delalloc_punch(struct inode *inode, 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 = NTFS_I(inode)->vol; loff_t start_byte, end_byte; @@ -826,9 +801,17 @@ static void ntfs_write_delalloc_release(struct inode *inode, loff_t pos, if (start_byte >= end_byte) return; - 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); } -- 2.56.1