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=1796146990; b=a0TktHTCgJ4E5HwIw8qujaAOMbRZMQpNMKrpMyhzmoDPTQKcgP/7Bxw7r3o6+Z38gy5/ kbxQZhcI6eOqLlGcVBrT+CgtNXw6/Crx1blu5pHKybwAELyfZOLajdGjIidU4mVdIHHqA 7RM6/1jykf7NBKM19O5L/RHWVGI7ICATMC6prVmHqtc4ppCMGE3XqX6tjDwEYB28BkakG breoqy6dV9ar36QNFEnXyf6Z6WTJkpwP7toDlIy0+wfge8GAjbty3np1ATrQL1a+cGx9o 1UPCnTSizeRrksmSGIG0QreXpWRpNUan21glIM1OXKzqsQn5tLcrA7D62gzlX+q5xRQ== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796146990; 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=arbILu4ZIIDTm5ywkdYYvLXZ8djYPB5SPlncnstBc172L9LnBky+bfUZmMaYQGhX4gnC QfvM1QbKv2AkNfsKQfg8dBCf7ccP4hrxsRixflvefonLRbNLgz6UqGM43wf4v7Vs6f4lN B9yk+WghBzpsRtaf0c2bz5r5WATYX0xBpD8ayzpID++QWR1AyMGoG5uWuO5v5/RxUkEak H9Jzt8g88w3J9wulYZjWt9oPvkxlscxm/T9rnMFrVbk9xfFDb5wfvwgiKAA3Tuib2kPPY t7Z7s+UadlX6eQHy9IpeDoe6St0qfJvWhYfh3zGWebar4qTyUWWPt4y+24QYO8S8M3Q== 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=S8lNgmbs; 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 E0C30606A6B3; Tue, 1 Dec 2026 17:43:09 +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=S8lNgmbs 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=1796146989; b=oKZEysdSFcjd110t1xlXr9eMMHveNs9jVrl8KRhzk/X+P6sJWFW8nXGgMwU6xc3TmCbC +P/LIUY2W2X6oABv1Uxl75lUingiO7Q/uElRRH6K42gK5atoMcGXwjSe5UruSzmwPCj4P 9OSYQv1avPuSObvaxhKPHcSuLc3Tqq3Yzo1W4llsYjXLqB4Fz9zhTMbbmUYZiMtKYSB2f FaX2MKYbhf7E7Flx5vDcxoomVSUO4KloyADQjd+BoBvAHaT+R+ap49EbZ0QB9yg6048gv cnsA/OMwk3hMNeiSqc9TZ+OexlUTE+d0f0/69FK5QHkGslfh1Yu6yC244tR635FL78w== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1796146989; 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=DXkJ7UyAKz5+UAd/TwIE3iGCRxIlwHmnlTP/R7OYRs29ghNn62sl2FVSKpLcQccamMbn NHrRynxASc3KtCPPB7fG7Xe9iIQ+eTG8gM+/n+fGGh99ZWLnAAwSWNSHu+qWAOV1W71vM 6VXlgOJWsKK7r5qNj/4oXHU7JaBybdxTnKXiCUa/CJGtE0CdwuAQsLSXWwOB+iKqEnDc7 Cjy0jmhPjGFjclFQXYpkXgX20MYxNyovl9FJWRz2jkGOGcVov1r6x3fd8cI/12yBHU4JA idlBwfd+wf115qD29n/Ezk6kCdoEWMUUEVcW/8+OfzpdnD/a6OZXf2IJxOUbSMp5BLA== 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 56A121F00A16; Tue, 1 Dec 2026 17:42:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796146989; bh=a2Ffas3bLeeCGSUnr0XQ4vHOK5iuvqsq87RRMDoP0wk=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=S8lNgmbsWsLZObYT9UWnCxrTCuokzNiucuiXV+ChODT8fBwNwd/Qkg363tsWppH+2 BEu+nMGzw28+f8TG8+MoY4dLQMhCxy+WfKmk3nOdfancGydhWsXL5EAnF/xYkHzAsO RK3b+v08akZtmVd0mS8C3AXaiMKXnY4LKurIB6rm/P03hqMoTRl4fp8LMiTJom92xg g+rCtgp0oVHvAebZYj73gcir8HFtYKwKchhAxy2Se2bsb1z9YFsQ2jHgu4iZAh+bt/ iCCYXCtywwYxdtB5Nmd5tKlG1qAWCsWpIJ+fwk/Y4ido1o4N3sJL8C9NMhXXeInz1a qIl18tuYxDD8g== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 950774EF2F; Tue, 1 Dec 2026 17:42:39 +0000 (UTC) From: Ernest Borowski via B4 Relay Date: Tue, 01 Dec 2026 18:42:44 +0100 Subject: [PATCH v4 09/11] 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: <20261201-b4-ntfs-delalloc-fixes-v4-9-e5dd6f77016a@proton.me> References: <20261201-b4-ntfs-delalloc-fixes-v4-0-e5dd6f77016a@proton.me> In-Reply-To: <20261201-b4-ntfs-delalloc-fixes-v4-0-e5dd6f77016a@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, 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/jabUkhmx+dp63fwwsXk1ab/ywYYKnetnjF8nVjsEJUz+ISPydP e3ftPDajlIWBjEuBlkxRZaO/zdCfEK+fVoWp9UHM4eVCWQIAxenAEwkyYmR4davap/3wrPSpF5L bLOz3hhbLJNy77fDonDryzedT0sKr2D4X9NQq7yN2ZI7jTc0YPu3xo3b678dS/hRuaSqpNtLyiy JHQA= 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 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, 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) { @@ -337,17 +304,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) @@ -362,19 +318,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) @@ -528,8 +493,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; @@ -544,16 +508,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; } } @@ -721,16 +684,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, @@ -802,9 +776,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; @@ -821,9 +796,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