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=1796409653; b=pflR7NbVOgyESfSwlrkd8L/c6oi8DI7KEYsmx5pDKPdso9/TjUpOTwnvA25jsXKDZjJc lRgrpRdlbfAfV9vzjJrpvamtHzUlYjoTwk+QKAt/9lV4BZW6XtNdA4Fo/tcdQWksQKrTN PzAYZlB4GXgtm5bqUGeZrQi+j5dleNRR7hN3IZfG0DrNHEENUfKHZ4EryZdG7noGcpu0S 0vUkZb1MoDCjrZHAK3tyB4yrIFU7OIW1003OAuocafcV4o4buyKhBEss8HMUdsld8P0KT L7yMA3oD7nCQXMdSHXAMOGDypcn06G8HNUJPoAaz02WOSLhkkJVoUBnMcshyEEjaw1g== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796409653; 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=G9VR6e8RM9LtlFfFpBisAiSZL3KqiaTp/8+qjG3/TJE=; b=5A6abyWnAJaUwIPhDXsgtIcIPWjTWC03PftU1reLlcPxREDqLHaMOzm/UDRnMVpFpk6U YbowZHyFrE+M9lKT7swor2lOK9KOHA8TAiCY+Fb+Vmwe3CA4d8dhE55UZ+sAHH/vMX3k0 hU2515fSBmHtxWe2mX+3/bXLvIkIZDiDRyUwle5IFQ/V0uBnqP0XQ3K7uCUn0ZZvnGI+V hXIh+aAh0xuyrSs2yFZbpzDoSUDBznd81V7Ica27MimA5V+Vz90o13pttbIEm2jpnOUD5 EyMS3UJmSrapONp9e6/cQF1TNxFU4pkvrAKn9zZXK0K5nfoTq8K99NsAhOIo3XPYBUg== 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=ZmRw9KGP; 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 4E8F260A3839; Fri, 4 Dec 2026 18:40:53 +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=ZmRw9KGP 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=1796409653; b=g8UxJCcZamMUVS5yfjryicw36RO/o2oDGXXU4XwKItZujjmSqVdYeHLdGUoIbzMbnm4Z Ic9DW+wWkhttytlbNi/ngTV2Ge5RJ8ltuTDBTfoKEyyR/3rAqYOqX/uAxUIQP/BN54SwA UscTrk7oyUsjQ6+lhOhNAElPi6jyXcbl9qZjFlByfcoX7IwAWnAr+rRaKSTyRieFzFadK kjpfnwgKbFY0w+PcPmhiiHvurUlzC8zlg7FoZv7KlgJGiJk5I1beTcnUxbpDEBRK5vWK6 PgPY96wGY/DKkIPPxrOgMJPcqb8AFNX2QWXKOEiT+HzuR53dYWiaD08EQNWxMzeYKXA== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1796409653; 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=G9VR6e8RM9LtlFfFpBisAiSZL3KqiaTp/8+qjG3/TJE=; b=FGwYkRhKog+Xpyqamn17edvmS++KmhI512ZaEWPKzd1pSHx3b6RCRqymqcsudBeRssfG 62CHsJRIBPt2pI5mhYe6FbvHk6AftPdXg46mp0Athn5Y63pFZY5mBXJFVZaqJcgMQG/wj xaUMWkcsu8W7MBCLXrsAURHQNESWp9U1YWDxBziXl9lbJvq3CAnj8xvYQhMmzDnfiKayH F7K40FjGPmxwE+7n/2LKgZF0FYypHUCa7ETe0UJOBd3QySLgIM0VI4xYfE26jZfHVPJs8 O8MdK0GcxGSMssP7LrAiB/k4SE8pp2Wv9pA3x4r6ShDxWjrudUlFauzx5vZd8RcJGpQ== 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 1DAA91F00A1F; 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=1796409653; bh=G9VR6e8RM9LtlFfFpBisAiSZL3KqiaTp/8+qjG3/TJE=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=ZmRw9KGPvw7c/R7btw119vGczlfzcjtXbDQNfF2dJZkZU5xOudQJNb2DqrVnNOTbZ F6t39PRaN4z8srP+CvyjsZbnIR0pC/mgGCk6fp7Qg2NNGwWwkj+eaKPu/dehYAgl4+ nBWq4R4meIWEhQq0D0F1XIYYLL1HgT6bcVTrQv49nE9fwZ/jLrr5vwIUDWCCLNAJf9 KN1R9te9M7YBGAy0I5PPDn5GshxX7qojrFueYWumIWhGGg25OJW4nDdzDZGlwJ7rur muO5nDET3jx8Jarju5CbX7vc2Q/sZEhRU6nOKCOqOzxYVyEDKXwi3e56DtkWSmGj0p eO919BamOLVAw== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 90C564DC54; Fri, 4 Dec 2026 18:40:36 +0000 (UTC) From: Ernest Borowski via B4 Relay Date: Fri, 04 Dec 2026 19:40:42 +0100 Subject: [PATCH v5 11/13] ntfs: revalidate a buffered write's mapping after its clusters were allocated 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-11-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=5841; i=ernest.borowski.git@proton.me; s=default; h=from:subject:message-id; bh=C7aP5Xmk6G3MUbQ2uBOJbufrsR0/xqHisI7RE4e8cDE=; b=owGbwMvMwCVWzJ65uCpJ5D/jabUkhmxhTsULKaevWHhWzQj8eXbrzsLY5Ws3Sc67X/HafUZLz aZDAn9ZO0pZGMS4GGTFFFk6/t8I8Qn59mlZnFYfzBxWJpAhDFycAjCRL4EM/+wOaKsqBa5WDJ10 //qH8m+vslPOxgnwfe7c1y72JHmjBBsjw9So5m2T9fW9WwT3H2HwabucszNK9NcVRkfWgivGq0o sWAE= 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 buffered write turns a hole into an LCN_DELALLOC run in ->iomap_begin() and copies into the page cache afterwards, with only the inode lock held. A write fault or writeback does not take that lock and can allocate the clusters in the meantime. If the folio the write has not reached yet is then written back and reclaimed, __iomap_write_begin() still sees IOMAP_DELALLOC and zeroes the parts of the block that the write does not cover instead of reading them, and data that a write through a shared mapping put there is lost. Reproducer: block the copy of a write into a hole on a userfaultfd page before its second page, store a byte through a shared mapping next to where the write ends, sync_file_range() the file and drop the page with POSIX_FADV_DONTNEED, then let the copy go on. The byte reads back as zero. Give the delalloc mapping of a buffered write a validity cookie. ntfs_attr_map_cluster() bumps a per-inode count whenever it allocates clusters, and ->iomap_valid() calls the mapping stale when the count moved and its clusters are no longer all delalloc. ->iomap_end() then releases what was not copied, as for a short write, and the rest of the write is mapped again. 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/attrib.c | 2 ++ fs/ntfs/inode.c | 1 + fs/ntfs/inode.h | 3 +++ fs/ntfs/iomap.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 54 insertions(+) diff --git a/fs/ntfs/attrib.c b/fs/ntfs/attrib.c index 3de589ca0b..a980198046 100644 --- a/fs/ntfs/attrib.c +++ b/fs/ntfs/attrib.c @@ -5680,6 +5680,8 @@ int ntfs_attr_map_cluster(struct ntfs_inode *ni, s64 vcn_start, s64 *lcn_start, } ni->runlist.rl = rl; ni->runlist.count = new_rl_count; + /* Delalloc mappings of buffered writes in flight go stale. */ + WRITE_ONCE(ni->alloc_seq, ni->alloc_seq + 1); if (!update_mp) { u64 free = atomic64_read(&vol->free_clusters) * 100; diff --git a/fs/ntfs/inode.c b/fs/ntfs/inode.c index 651427c31f..ba00c09e1b 100644 --- a/fs/ntfs/inode.c +++ b/fs/ntfs/inode.c @@ -557,6 +557,7 @@ void __ntfs_init_inode(struct super_block *sb, struct ntfs_inode *ni) ni->target = NULL; ni->linuxflags = 0; ni->i_dealloc_clusters = 0; + ni->alloc_seq = 0; } /* diff --git a/fs/ntfs/inode.h b/fs/ntfs/inode.h index 8c1929bebe..9ccaf55682 100644 --- a/fs/ntfs/inode.h +++ b/fs/ntfs/inode.h @@ -94,6 +94,8 @@ enum ntfs_inode_mutex_lock_class { * record. For fake inodes, the real (base) inode to which the attribute * belongs. * @i_dealloc_clusters: delayed allocated clusters. + * @alloc_seq: bumped under the runlist lock whenever clusters are allocated + * for a hole or delalloc run; a buffered write's ->iomap_valid() uses it. * @target: symlink buffer. * @linuxflags: Linux-specific file attributes loaded from the $LINUXFLAGS EA. */ @@ -144,6 +146,7 @@ struct ntfs_inode { struct ntfs_inode *base_ntfs_ino; } ext; unsigned int i_dealloc_clusters; + unsigned int alloc_seq; __le32 reparse_tag; __le32 reparse_flags; char *target; diff --git a/fs/ntfs/iomap.c b/fs/ntfs/iomap.c index 820a89c640..82ef13e7bb 100644 --- a/fs/ntfs/iomap.c +++ b/fs/ntfs/iomap.c @@ -79,8 +79,55 @@ static void ntfs_iomap_put_folio(struct inode *inode, loff_t pos, folio_put(folio); } +/* Never 0: a mapping with no cookie is not checked. */ +static u64 ntfs_alloc_cookie(struct ntfs_inode *ni) +{ + return (u64)READ_ONCE(ni->alloc_seq) + 1; +} + +/* + * A buffered write maps a hole or delalloc run as delalloc and copies into + * the page cache afterwards, without the locks a write fault or writeback + * takes to allocate the clusters. If one of them allocates them meanwhile + * and the folio is written back and reclaimed, __iomap_write_begin() would + * zero the parts of a block that the write does not cover instead of + * reading what a write through a mapping put there. Every allocation + * bumps ni->alloc_seq; when it moved since the mapping was made, the + * mapping is still good only if its clusters are all still delalloc. + * A stale mapping ends like a short write (->iomap_end() releases what was + * not copied) and the rest of the write is mapped again. + */ +static bool ntfs_iomap_valid(struct inode *inode, const struct iomap *iomap) +{ + struct ntfs_inode *ni = NTFS_I(inode); + struct ntfs_volume *vol = ni->vol; + struct runlist_element *rl; + s64 vcn, end; + bool valid = true; + + if (!iomap->validity_cookie || + iomap->validity_cookie == ntfs_alloc_cookie(ni)) + return true; + + vcn = ntfs_bytes_to_cluster(vol, iomap->offset); + end = ntfs_bytes_to_cluster(vol, round_up(iomap->offset + iomap->length, + vol->cluster_size)); + down_read(&ni->runlist.lock); + rl = __ntfs_attr_find_vcn_nolock(&ni->runlist, vcn); + if (IS_ERR(rl)) + valid = false; + for (; valid && vcn < end; rl++) { + if (!rl->length || rl->lcn != LCN_DELALLOC) + valid = false; + vcn = rl->vcn + rl->length; + } + up_read(&ni->runlist.lock); + return valid; +} + const struct iomap_write_ops ntfs_iomap_folio_ops = { .put_folio = ntfs_iomap_put_folio, + .iomap_valid = ntfs_iomap_valid, }; static int ntfs_read_iomap_begin_resident(struct inode *inode, loff_t offset, loff_t length, @@ -473,6 +520,7 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_ ni->runlist.count = new_rl_count; ni->i_dealloc_clusters += max_clu_count; } + iomap->validity_cookie = ntfs_alloc_cookie(ni); up_write(&ni->runlist.lock); mutex_unlock(&mrec_ni->mrec_lock); -- 2.56.1