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=1796409651; b=s0liINDQQvI58QvNSYl9XyTwWOpmjGDmUruf5PVe/RvhTSNOsrHfxvQCZqo+tSlZbxHT LCEaEmAUGmJtDjqwHK/QBthKzBrmnEG4emKhtW8W4sBNbEhqSKn6CAsLIy98MMYjyLWnV 85T1+enwHenT5hXLmai03lrEBckImlf3aRzzomkg510pshF53eHv2RiMTQJNxHcsHpi9q tUm0gNN//z+tlw7HR4qB3kRpXeDhD+qY/Jt4QY0o6isUGCF06pJmUlUaEHuALMS0vCEV/ RfgDp1xRbwG1NkHI6A7dCHr3sOKxc/eEmejKe+fnJP8pIw9oM4ytekEmXz7Y//mFWlw== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796409651; 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=jc5rgIcOOliRLBJZan0GvRrERfQJuxVjzqVhVJYEktA=; b=DLf3OUm4JVx4Y0Jm9rHayFPsgPYOTaqEWf12kaQVRgLmr/Ac0Y0klbHnnCvpqzrGVn8C W/xdcbH1F/hVVooSajvBFmUIe7ANHiLRk0mJttSo++kE3uxGUjKtnVAZ2ApPdRa+LiOFm U7AwBEZus1ZyeHxmDNH7OBt8zaNL+QXaHB6316mZ8BS2ZeHryfpGlxu2umC0pAxTBjxjD 62XcfFN91FLjFMt644IitF4iclettVCy0UYMoHUDEuapTyn9+NCwlL1MNl99zaz366qEP oHFcbVy8d2RhnG2gJe41GOCacf2AoHqW7St8yVPfZRWvRvgtQJ7IblGE0ATHhPht7CA== 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=nZxTbZp+; 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 6353760A2635; Fri, 4 Dec 2026 18:40:51 +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=nZxTbZp+ 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=1796409650; b=Au04Qpr6fanVK/0YNgVVe0LWCzqR6exKFSPqMX6s2/bviTx27p4Zkohkxfa7NU5g9LtC rmyg+v+8JlHpch/E9sRuvDAoadOfq/rQnV2jUudFvovCFrOX1s6F3NMFg/R72PlJ9hHu5 fWe/6l92nvGk4nZeViq3zm3m9euh56SEdfHAdPiR7qoI+gmm3Ai25eVK52uFYuaaCla8o q6/SEFbLySFeYkw5jcMstyOpXBQsvZN5m3Wp8SLOj2lzPIJ7SUHFmmu348S4+hAuBP11/ Sk9h3xJSiWkjB6ysdINO9kTB9zvoRALFndEb2h3r8vJqdt+zTXIkok4+jyPygXgTODA== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1796409650; 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=jc5rgIcOOliRLBJZan0GvRrERfQJuxVjzqVhVJYEktA=; b=NGgSp4MBCaf28TDx3rFAQoZMeeodY7YDuOQK3F/BCjkdRi/R71RdrejloyOf7jUiZiAM Jb7AtJduY0COaN8e3vbjKi5DKKPz5bT64gargDkWA4OniHEfaHOt5QZYf0NZ2evKCkSSL 7rHb+8D6AszGDi2BKwBkDcBSdytTUfLsYbWmxOLdC089UOOqFZ3xfQQmVI+KX0u6rtaS+ xA7xquWdTfVNz0jYBJr3aNJCxnJNX3iqRj3u/b3vXYbyf7uUobzpOJRt54ytU/4OtkW9Z LBkIhFE3LSEQr9aqHwOaUq8m/IiHvR1rt8pwfj3uql50+4O+COrxyYzEOjy7UGl9kBg== 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 1F2F31F00A20; Fri, 4 Dec 2026 18:40:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796409650; bh=jc5rgIcOOliRLBJZan0GvRrERfQJuxVjzqVhVJYEktA=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=nZxTbZp+niMZlpG8Jhb+U24ny4h07Kdb8WrI8Nz5g6rh5HE7SFTJnrhfWnsBqh6b8 dgJ2eyi8jGRpU/lJao6xHZ4CAty3e49AJhewqUXkdm33OPP5hggCtZOzGMMCpEY2ob /B0VDTgz5T0xsy/FnXpWIWv8pa+O8300AGu/ZveLakzghGV9VMu9li/N7RE40KaxMb /cR3t+cQ95kerqStq4Rx3ChyDwESE+DNPpDaro9eKq2iaJNvqlghR3qypknNeBnWmt rZF4dgMfPMCJxl7aH7SfL+yPw5xfSUHjz9hY6EJYRxKZFRblGVcLXsG4L/fdb6PtJV N/TE/Wpr3rTqg== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id B97384DC58; Fri, 4 Dec 2026 18:40:36 +0000 (UTC) From: Ernest Borowski via B4 Relay Date: Fri, 04 Dec 2026 19:40:43 +0100 Subject: [PATCH v5 12/13] ntfs: zero a write fault's new cluster before the runlist shows it 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-12-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, Ernest Borowski X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=6745; i=ernest.borowski.git@proton.me; s=default; h=from:subject:message-id; bh=tw2Dt+rSSy8+F9FBamHmvMUSbe/ITd0CGEJLKaVxOzE=; b=owGbwMvMwCVWzJ65uCpJ5D/jabUkhmxhTsVZF6f8f/55tvXcHyfOttwsa1zSE56Z5e9xcss3o bibbVdXdpSyMIhxMciKKbJ0/L8R4hPy7dOyOK0+mDmsTCBDGLg4BWAiDtmMDCtbZsu8S9MWfmGh PG2C/fPLXlKfPljxnsz4tcrL19v9y1KG/0nq05OMDyrceh0ScGG5sI1O1oJ9Z+sTfe++nzpj1aZ VyYwA 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 A write fault into a hole of a file mapped with mmap() allocates clusters in ntfs_write_da_iomap_begin_non_resident(). It calls ntfs_attr_map_cluster(), which puts the new cluster numbers into the runlist and drops the runlist lock and mrec_lock. Only after that does ntfs_write_da_iomap_begin_non_resident() zero the clusters on disk with ntfs_dio_zero_range(). The faulting folio is in the page cache, but when a cluster is larger than the folio, the rest of the cluster is not. Until the zeroing is done, a read of that rest through read(), another mapping or splice() finds the new cluster in the runlist and reads its old contents, such as data of a deleted file. The page cache keeps that stale copy after the zeroing, so the old data stays visible until those folios are dropped. Reproducer: on a volume with 64 KiB clusters, fill the free space with a file of a marker pattern and delete it. Create a sparse file and store single bytes into its holes through a shared mapping. Meanwhile, other processes drop the range being faulted in from the page cache with fadvise(DONTNEED) and read it through new mappings and splice(). The marker shows up within seconds. When the faulting range does not cover whole clusters, zero the new clusters in ntfs_attr_map_cluster() before they are merged into the runlist. Only direct I/O still zeroes after the mapping. Fixes: b041ca562526 ("ntfs: update iomap and address space operations") Cc: stable@vger.kernel.org Signed-off-by: Ernest Borowski --- fs/ntfs/attrib.c | 28 +++++++++++++++++++++++++--- fs/ntfs/attrib.h | 3 ++- fs/ntfs/iomap.c | 14 ++++++++++---- 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/fs/ntfs/attrib.c b/fs/ntfs/attrib.c index a980198046..6da614598d 100644 --- a/fs/ntfs/attrib.c +++ b/fs/ntfs/attrib.c @@ -5545,7 +5545,7 @@ static void ntfs_map_cluster_rehold(struct ntfs_volume *vol, bool was_delalloc, int ntfs_attr_map_cluster(struct ntfs_inode *ni, s64 vcn_start, s64 *lcn_start, s64 *lcn_count, s64 max_clu_count, bool *balloc, bool update_mp, - bool skip_holes) + bool skip_holes, bool zero_new) { struct ntfs_inode *base_ni = ntfs_base_inode(ni); struct ntfs_volume *vol = ni->vol; @@ -5653,6 +5653,26 @@ int ntfs_attr_map_cluster(struct ntfs_inode *ni, s64 vcn_start, s64 *lcn_start, WARN_ON(rlc->vcn != vcn); lcn = rlc->lcn; clu_count = rlc->length; + /* + * Zero the clusters before the runlist shows them: from then on + * readers map them, and they must not see what the clusters held + * before. + */ + if (zero_new) { + err = ntfs_dio_zero_range(VFS_I(ni), + ntfs_cluster_to_bytes(vol, lcn), + ntfs_cluster_to_bytes(vol, clu_count)); + if (err) { + ntfs_map_cluster_rehold(vol, was_delalloc, clu_count); + if (ntfs_cluster_free_from_rl(vol, rlc)) { + ntfs_error(vol->sb, + "Failed to free clusters that could not be zeroed."); + NVolSetErrors(vol); + } + kvfree(rlc); + goto out; + } + } old_rl_count = ni->runlist.count; old_rl = kmemdup(ni->runlist.rl, old_rl_count * sizeof(*old_rl), GFP_NOFS); @@ -6301,7 +6321,8 @@ int ntfs_attr_fallocate(struct ntfs_inode *ni, loff_t start, loff_t byte_len, bo NTFS_INODE_MUTEX_NORMAL); down_write(&ni->runlist.lock); err = ntfs_attr_map_cluster(ni, vcn, &lcn, &alloc_cnt, - try_alloc_cnt, &balloc, false, false); + try_alloc_cnt, &balloc, false, false, + false); up_write(&ni->runlist.lock); mutex_unlock(&base_ni->mrec_lock); if (err) @@ -6334,7 +6355,8 @@ int ntfs_attr_fallocate(struct ntfs_inode *ni, loff_t start, loff_t byte_len, bo mutex_lock_nested(&base_ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL); down_write(&ni->runlist.lock); err = ntfs_attr_map_cluster(ni, vcn, &lcn, &alloc_cnt, - try_alloc_cnt, &balloc, false, false); + try_alloc_cnt, &balloc, false, false, + false); up_write(&ni->runlist.lock); mutex_unlock(&base_ni->mrec_lock); if (err || fatal_signal_pending(current)) diff --git a/fs/ntfs/attrib.h b/fs/ntfs/attrib.h index dd073f9fd7..ca3481cb78 100644 --- a/fs/ntfs/attrib.h +++ b/fs/ntfs/attrib.h @@ -94,7 +94,8 @@ int ntfs_attr_size_bounds_check(const struct ntfs_volume *vol, int ntfs_attr_can_be_resident(const struct ntfs_volume *vol, const __le32 type); int ntfs_attr_map_cluster(struct ntfs_inode *ni, s64 vcn_start, s64 *lcn_start, - s64 *lcn_count, s64 max_clu_count, bool *balloc, bool update_mp, bool skip_holes); + s64 *lcn_count, s64 max_clu_count, bool *balloc, bool update_mp, + bool skip_holes, bool zero_new); int ntfs_attr_record_resize(struct mft_record *m, struct attr_record *a, u32 new_size); int ntfs_resident_attr_value_resize(struct mft_record *m, struct attr_record *a, const u32 new_size); diff --git a/fs/ntfs/iomap.c b/fs/ntfs/iomap.c index 82ef13e7bb..270bd41fa5 100644 --- a/fs/ntfs/iomap.c +++ b/fs/ntfs/iomap.c @@ -611,7 +611,7 @@ static int ntfs_write_da_iomap_begin_non_resident(struct inode *inode, struct ntfs_volume *vol = ni->vol; loff_t vcn_ofs, rl_length; s64 vcn, start_lcn, lcn_count; - bool balloc = false, update_mp; + bool balloc = false, update_mp, zero_new; int err; s64 max_clu_count = ntfs_bytes_to_cluster(vol, round_up(length, vol->cluster_size)); @@ -621,10 +621,17 @@ static int ntfs_write_da_iomap_begin_non_resident(struct inode *inode, update_mp = ntfs_iomap_flags & (NTFS_IOMAP_FLAGS_DIO | NTFS_IOMAP_FLAGS_MKWRITE) || NInoAttr(ni) || ni->mft_no < FILE_first_user; + /* + * A write fault into a hole dirties only the faulting folio: the rest + * of a cluster that the folio does not cover has to be zeroed on disk + * before other readers can map the cluster. + */ + zero_new = ntfs_iomap_flags & NTFS_IOMAP_FLAGS_MKWRITE && + (offset | length) & vol->cluster_size_mask; down_write(&ni->runlist.lock); err = ntfs_attr_map_cluster(ni, vcn, &start_lcn, &lcn_count, max_clu_count, &balloc, update_mp, - ntfs_iomap_flags & NTFS_IOMAP_FLAGS_WRITEBACK); + ntfs_iomap_flags & NTFS_IOMAP_FLAGS_WRITEBACK, zero_new); up_write(&ni->runlist.lock); mutex_unlock(&mrec_ni->mrec_lock); if (err) { @@ -651,8 +658,7 @@ static int ntfs_write_da_iomap_begin_non_resident(struct inode *inode, iomap->addr = ntfs_cluster_to_bytes(vol, start_lcn) + vcn_ofs; if (balloc == true) { - if (flags & IOMAP_DIRECT || - ntfs_iomap_flags & NTFS_IOMAP_FLAGS_MKWRITE) { + if (flags & IOMAP_DIRECT) { loff_t end = offset + length; if (vcn_ofs || ((vol->cluster_size > iomap->length) && -- 2.56.1