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=1796146988; b=niVhjWCJ2cb6Lm+A/kRb0fiegwWV+AqwRHw8YSzRjLcdhZOVplGuDFBOjY3CSMcZ2fTq Yrc27SZvMlR6E7V2c+Csdyj9jZeC7nYMWcLK54y96eONYhHNVjACuvNYdHqJnz+Vo/Aiw zuadO7ZMYTFOtTGlsBnlH7aJm3oj16qZ9zS4+YBPzJ+Nh+TkDLef5G6ALnaHd7Qzbqd0n Ezsv6wKs/IWEwRQhgutdBwSaTxLY04RI9+OyKaGoMG5Blvb3ncDQE/DPPtwYOZbL1Zu3O RGY+sRQ5GKntXJVVxmLfT8SCcKdGz2PgpNg46mS0iv1zW6DqMHMesO0dC9WgoH93stQ== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796146988; 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=mUrqAs3Mw0Xk1MnKJKxxznXjkAX3tAFMbaPtPTNKIqo=; b=ylxzr4wD/1TjmHfufOyld3DEl0gLMH/qwi7zCZaJRcLlWn5nj5omPji6XcNT28FCK1mB KckCw4bpQAfSbQWvie333wgf1T6h57CpHSImqsF6UViWc7ceS9E67Dmmt9ZjaJ5yLTGt4 8Td8EC6enidYAouS+JU7FPeNnil/z7i3GHpZfKlqxcWglw4Kfe0Kt0HJmL03hFZ7nvALk li5vQ/21ReTcNSsDfDCMTH1wJQargh1VvviztEw0v6Zt5OGvUQ/uZg8IJxUiqmIV7RFs3 Wh7xswnHuxaHqwGQVmoMMf/MhjqZFzfjtdue8p0wjuuYyg6yha4yg+eOSTudYRIJ1Zg== 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=h09tsxMb; 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 B82CA606A68B; Tue, 1 Dec 2026 17:43:08 +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=h09tsxMb 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=1796146987; b=Q8w7fwgRj4svDTOhxqYwm9S8v8u7XpepM7cPjw5/TbSwq1I0KSBtbJZYJcibq6687HOL uoPsqRIiHHyqFqpnjLFFOtgMkQJFNTRDVNl5LXO+uM1hPebB6VCuzI2ddBGxzJ8O9fg94 8VbGUgCz2UD+7kDKx3rBMyYfZH4ok7FVHDSMaG82S0ShSOhbe/PsIPkB5KMY7I5+llo5F nqmPPpGHmVKSwliKiFh2TmSzlkNRFHQ0EBC8QnUC7I8xCnwd1z9ckk9p/kP+BUfpDUuTE +f+loynFHSYTGPBI1W1OyDb/aHz6LFrh1kUEW4fSwSJtjRdVh4f2kMm3d9COyiJfGgQ== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1796146987; 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=mUrqAs3Mw0Xk1MnKJKxxznXjkAX3tAFMbaPtPTNKIqo=; b=iRiBijDMPPAzGu4NWkbyQC9c2tbwlkmQwz629NFRs4uLn1wPcUV3UFzZBGjIsIHP8dje j7N52gCC8fJktD/vbTayxcLz2c2SonsE9+UVRL78v5/vDW7PuZBmmS+Z4G/xYKarNTzy3 jbzxXDzHduoeuhzW7GqegJN1ZxwC9aU0kdICGkDcXdlspKH42KiRQgNn5UeJiYMkYjgAp /vLBIYPpILZobAeXAlZijThTv2CoUvcehtryYSTJPs5SH/aQI/Z4jP6ShC7u1iRSNLCYx g5+5qbN1Xf7X3yrrVXKJQ/8YXMRD79gHf7kdKhqW+AoHl8/51tZ71AJr0910ezkfv0Q== 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 5A3F41F00A1A; 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=1796146987; bh=mUrqAs3Mw0Xk1MnKJKxxznXjkAX3tAFMbaPtPTNKIqo=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=h09tsxMbaEpcEFo9fhXyu0EonMTH2GKtpSYw3kbp9T8CkysnAFIz9C/gLLV53slPe lFbGucuX9rVp8fXstMPYOCIsnz3VjapOGich7U2Rl5JldUkr81YNG5BAPTVODpIC6u F8BiobbqNO5AVe9drLVwzRcB3saVQ0y3udlm2n0cJzd05bqSLNwD1Un8CbhwLnPvZ5 gSd4sJ+jiHwQRrgVZulOjABqWsMbU1iRXYQww2mLPOBG7HxRRW9CfCdhL0HosQfRlz 1b3BCxsfIooyPhxr1eWYpb7glsWh1Lwhb8cV9OyfebVR2lkBq49xIibgnW8X0aiJzU TEe7IHPHWwmXQ== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id DF7A94EF37; Tue, 1 Dec 2026 17:42:39 +0000 (UTC) From: Ernest Borowski via B4 Relay Date: Tue, 01 Dec 2026 18:42:46 +0100 Subject: [PATCH v4 11/11] 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: <20261201-b4-ntfs-delalloc-fixes-v4-11-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=5793; i=ernest.borowski.git@proton.me; s=default; h=from:subject:message-id; bh=7YfBywwxzWoTOmvLHhfQBnYWAESnlSrM2KSuzsoMVqA=; b=owGbwMvMwCVWzJ65uCpJ5D/jabUkhmx+dt5W7zSLF1lPK7hfSexofZPPyesdH/05+s1f03jNO 89uOu7sKGVhEONikBVTZOn4fyPEJ+Tbp2VxWn0wc1iZQIYwcHEKwESsmRgZPvh90HG8PpfR3z/x /zybB9NCZ8m0+G+VUZ7N8zpiU3nrCkaGzbKzpPiSTijISyx7lLUj1Zlphcepoz5R4XbPF1r/Orq eDwA= 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 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 2ee08fa738..a5727968fa 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, @@ -468,6 +515,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