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=1796409647; b=Zn1pXahiBfF+AJXww6lcZAV8AyhfUGuWwnj4uTUdwqMtNfrqrVon/m7KgOhoVMZ0dbHj bIyZlaTIuW9cstar81zD0b1nOVVVRbepzqoNhHV3j2OrjrVN+VyEbBYifIML80qfZz7wg fLb9U4hxL2QBFz2ioL6n2yzFqz0MSfUbPgVOtZFEQsrDtvNXrylEIE1us650HP+1MxNLh dJhpDsnD7Y9vfotVHw5THkrkZ90hqMrw/ifVPSYumBKPQV8ZLiU8Td8Jt+kGNj088r66Z n9ARzM23aAkWGFXMgs5Kcpv1QELp5t+BglJqE6qDvifwsRUAr2h2wTePxD449er2sjQ== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796409647; 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=DkSTkjg2QHxdHnYHldI12Xn4FdE79RYGTPs5lEafLpI=; b=BWiYdrtlrsWm/wGSq8woWnqDay/30Op/0SOfgqO93ap//AXR+eik8XIo6axgl32zBUA7 yD/nO7l7wJQwrHZzSl8zjRz+83D/UXZc6Pc8vz4CHReFHUIdZnggF+T9HNty6STWLHS+q zucqjOTbVVno+AYvISvAuVAa8rSUV3TOkAaZwnUhfPiySfbyhQ6G9zHN9QRT9uBDPqCbs Fd8chwZSwNfT4qFsxd34AN1HZfPZGHNyiI7L2UdPLex5bHfCdrMx1im0Hl6CMqPuSZALU 1tWBdngSlbT3d52HKEh3NZhuej53IAdPb9VFy943vxYXdPlx8rbMXU1lZhbBg0grftQ== 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=aOgMTOpm; 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 A8A1C606A32F; Fri, 4 Dec 2026 18:40:47 +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=aOgMTOpm 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=1796409647; b=wdq449MGGVmsmIos/m1U5BTrkV9HAz+NUhDVRqI/v+t7rpW5nGN2lHLxhT79mqBA+Rxx ejz6NMFM8leLNQF/OBW6kGoKIa5kP2ixeSRExGbGno3JFsT3OU8C46ck+8uolYJYDgVRS 6xb9s2pHJjuV86ppOMGGnuM+5XjSxX1zJuqV0NMqSPuX/MxxEm3DeNMExr9MdKF35anH2 89rG6/wIyHOBRQ6Jp4P521UhZFyFyK6qkWpKONfXUWDILv9Gt8HHthE7CmARlE2ufKHse izQju41RdhHm5jNEWmLmSeN0JRD9+4+bKPZrO6V0Eb+c6PzJN/qMBzsQZXUL55TYoMw== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1796409647; 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=DkSTkjg2QHxdHnYHldI12Xn4FdE79RYGTPs5lEafLpI=; b=pb2ZO1YLsXKc29PmJHY1JXf6VBi0wwCu4aRVHp6i/YiaeoF3wV1xzgBB3N1uZhESeeVX oJkj2Jbj3PwvJqIg874RlH1ZkubI6bs8E9WcB0wD5KztDlJEYDCDlrR5pdQyb+qT9/na/ wDvw4L6vtMRiodFTmLTQpeMa193XArZPuBwBCvr4S6rpomX2mZWbRljML8Nc47gGcBT20 eNZndzU9Lyvxmn+pksEi6HHJzIErrregxz9B46Sb7ef9po6rhfduiwTshpznBC9u1z81m N7umrVN7gUHnboXIiF0oIVJCjqObpirQAvrs/VUBnj6FiYD0uS7zE1CAULmYCRq8qHA== 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 CF73C1F00A10; 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=1796409647; bh=DkSTkjg2QHxdHnYHldI12Xn4FdE79RYGTPs5lEafLpI=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=aOgMTOpmiSHYXxhOvOQZdll+Z33K7sH/w2zE31wKE+6v/gmKINxIXordZ48sHHF1+ 4txnpC9yTT0dsz56eN4Djhq1TOzn2f9HAnED09bv2+sGQ+DWNj6z3iSXeb6Yyhm+UW RdoeY4vyYZkoLqjyXYFPVvjzFJxyLKbW2pO2traB/YrQbIh8stVmzDGEt3m+SUm2Q+ wBJC8H41nM/VhRJpLTBSj1fim0Br2eOGWpUdexMCJNDwzd4VLfIBM09Q1+H0aHlZc/ b5cvmNwhlfbY4fB8kCHDYsZYEbTOM/dy0tBLmIRAAr5rX2pYxuwooUs6n7tR+LyF8M RHUR8514Lj/8Q== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 689FF4DC51; Fri, 4 Dec 2026 18:40:36 +0000 (UTC) From: Ernest Borowski via B4 Relay Date: Fri, 04 Dec 2026 19:40:41 +0100 Subject: [PATCH v5 10/13] ntfs: zero the rest of a new cluster after a short buffered write 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-10-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=8998; i=ernest.borowski.git@proton.me; s=default; h=from:subject:message-id; bh=m8M97RrL7pf+3MZe0lKnNvvTvw52pQwUIxN8iQfj2pE=; b=owGbwMvMwCVWzJ65uCpJ5D/jabUkhmxhToXqxYX5Td2R6yY6HGNds+XI1j0Jc62mW5TMc0/pm NMR+qKvo5SFQYyLQVZMkaXj/40Qn5Bvn5bFafXBzGFlAhnCwMUpABPp6mD4n/ZhdQHvv/DEqsjl f9mda2L99lnJq9hXJ3/gqojPi47yZmSYvS9UJaG7R/zbLceXp48ssj7v7nNuE3t4ZW34gYOWXFs ZAA== 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 ntfs_write_simple_iomap_begin_non_resident() turns the holes a buffered write covers into LCN_DELALLOC runs, and zeroes in the page cache only the parts of the first and last cluster that the write will not cover. When the copy from user space stops inside a cluster (the source buffer runs into an unmapped page, or a fatal signal), only the bytes copied are dirty. Writeback allocates the whole cluster and writes the dirty blocks, and the rest of the cluster keeps what the reused clusters held before. Below initialized_size, for example a write into a hole of a sparse file, reads return that old data. Reproducer: fill a file, fsync, truncate it to 0 and back up, write the last byte, then pwrite() 2.5 clusters at a cluster boundary from a buffer whose readable part ends 5096 bytes in: the write returns 5096, and after fsync and drop_caches the rest of the second cluster (4 KiB clusters) or of the first (64 KiB clusters) reads back the old file contents. Mark the mappings that ->iomap_begin() made out of a hole, and on a short copy make writeback write the rest of the cluster the copy stopped in, before the unused delalloc clusters after it are punched out. This runs under the invalidate lock. While the cluster is still delalloc, only the write and its zeroing of the cluster's edges have put data there (a write fault would have allocated it), so zero the rest of the cluster: iomap_zero_range() dirties exactly the blocks it zeroes. Once a write fault or writeback has allocated the cluster during the copy, an mmap write's data may be on the disk only, and zeroing would overwrite it. Then mark the blocks of the range in folios in the page cache dirty as they are, with iomap_dirty_folio_range(), and leave the rest (the last two patches of this series make both zero a partly written cluster on the disk before mapping it, and until then writeback that allocates the cluster while the copy is stalled leaves the rest of it unzeroed). Either way the other blocks of those folios stay clean, so the punch still releases the clusters the write did not reach. When the writer has a fatal signal pending, iomap_zero_range() refuses to run in its context (-EINTR), so the zeroing goes through ntfs_zero_range_unkillable(), which the short write fix added to file.c for the same case and which zeroes from a worker then. It is no longer static, and it now checks with lockdep that its caller holds the invalidate lock, as both callers do. If the zeroing fails, the write still returns the bytes it copied, so the error is also recorded in the mapping with mapping_set_error(), and a later fsync() reports it. Fixes: b041ca562526 ("ntfs: update iomap and address space operations") Cc: stable@vger.kernel.org Suggested-by: Hyunchul Lee Signed-off-by: Ernest Borowski --- fs/ntfs/file.c | 5 ++-- fs/ntfs/iomap.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ fs/ntfs/iomap.h | 1 + 3 files changed, 94 insertions(+), 2 deletions(-) diff --git a/fs/ntfs/file.c b/fs/ntfs/file.c index a66b615ddd..036f4deac9 100644 --- a/fs/ntfs/file.c +++ b/fs/ntfs/file.c @@ -901,12 +901,13 @@ static void ntfs_zero_workfn(struct work_struct *work) * still be zeroed: zero from a worker then, which has no signal to see. The * caller holds the invalidate lock, which keeps page faults out meanwhile. */ -static int ntfs_zero_range_unkillable(struct inode *inode, loff_t pos, - loff_t len) +int ntfs_zero_range_unkillable(struct inode *inode, loff_t pos, loff_t len) { struct ntfs_zero_work zw = { .inode = inode, .pos = pos, .len = len }; int err; + lockdep_assert_held_write(&inode->i_mapping->invalidate_lock); + if (!fatal_signal_pending(current)) { err = iomap_zero_range(inode, pos, len, NULL, ntfs_seek_iomap_next, diff --git a/fs/ntfs/iomap.c b/fs/ntfs/iomap.c index 112319e19e..820a89c640 100644 --- a/fs/ntfs/iomap.c +++ b/fs/ntfs/iomap.c @@ -12,6 +12,9 @@ #include "ntfs.h" #include "iomap.h" +/* iomap->flags: ->iomap_begin() turned a hole into this delalloc mapping */ +#define NTFS_IOMAP_F_WAS_HOLE IOMAP_F_PRIVATE + static void ntfs_iomap_put_folio_non_resident(struct inode *inode, loff_t pos, unsigned int len, struct folio *folio) { @@ -486,6 +489,9 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_ if (lcn <= LCN_HOLE) { loff_t end = offset + length; + /* The whole mapping was a hole before this write. */ + iomap->flags |= NTFS_IOMAP_F_WAS_HOLE; + if (vcn_ofs || ((vol->cluster_size > iomap->length) && end < ni->initialized_size)) { loff_t z_start, z_end; @@ -805,11 +811,95 @@ static void __ntfs_write_delalloc_release(struct inode *inode, loff_t pos, ntfs_write_delalloc_punch); } +/* True if the cluster at byte @pos is still a delalloc run. */ +static bool ntfs_pos_is_delalloc(struct inode *inode, loff_t pos) +{ + struct ntfs_inode *ni = NTFS_I(inode); + s64 vcn = ntfs_bytes_to_cluster(ni->vol, pos); + struct runlist_element *rl; + s64 lcn; + + down_read(&ni->runlist.lock); + rl = __ntfs_attr_find_vcn_nolock(&ni->runlist, vcn); + lcn = IS_ERR(rl) ? LCN_EINVAL : ntfs_rl_vcn_to_lcn(rl, vcn); + up_read(&ni->runlist.lock); + return lcn == LCN_DELALLOC; +} + +/* + * Make writeback write all of [start, end), the rest of a cluster that was a + * hole and that a short buffered write filled only in part. While the + * cluster is still delalloc, nothing but the write and its zeroing of the + * cluster's edges has put data there (a write fault would have allocated + * it), so zero the range: iomap_zero_range() dirties just the blocks it + * zeroes. Once a write fault or writeback has allocated the cluster, what + * an mmap write put there since may be on the disk only, and zeroing would + * overwrite it: then the blocks of the range in folios in the page cache are + * marked dirty as they are. Either way the other clean blocks of a folio + * stay clean: they may be holes, or delalloc clusters the write did not + * reach, which the caller punches out. The caller holds the invalidate lock. + */ +static int ntfs_dirty_cluster_tail(struct inode *inode, loff_t start, + loff_t end) +{ + struct folio *folio; + loff_t next; + + lockdep_assert_held_write(&inode->i_mapping->invalidate_lock); + /* [start, end) is part of a single cluster */ + if (ntfs_pos_is_delalloc(inode, start)) + return ntfs_zero_range_unkillable(inode, start, end - start); + + for (; start < end; start = next) { + folio = filemap_lock_folio(inode->i_mapping, + start >> PAGE_SHIFT); + if (IS_ERR(folio)) { + next = min_t(loff_t, round_up(start + 1, PAGE_SIZE), + end); + continue; + } + next = min_t(loff_t, folio_pos(folio) + folio_size(folio), + end); + if (folio_test_uptodate(folio)) + iomap_dirty_folio_range(folio, + offset_in_folio(folio, start), + next - start); + folio_unlock(folio); + folio_put(folio); + } + return 0; +} + 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; + filemap_invalidate_lock(inode->i_mapping); + /* + * A copy that stopped inside a cluster which was a hole leaves the + * rest of that cluster neither written nor zeroed, but writeback + * allocates the whole cluster and writes only the dirty blocks, so + * the old contents of the cluster would show through. + */ + if (written && iomap->flags & NTFS_IOMAP_F_WAS_HOLE) { + loff_t start = pos + written; + loff_t end = min_t(loff_t, round_up(start, vol->cluster_size), + i_size_read(inode)); + int err = 0; + + if (end > start) + err = ntfs_dirty_cluster_tail(inode, start, end); + if (err) { + ntfs_error(vol->sb, + "Failed to zero [%lld, %lld) of inode 0x%llx after a short write: %d", + start, end, NTFS_I(inode)->mft_no, err); + NVolSetErrors(vol); + /* the write returns the bytes copied; let fsync() see it */ + mapping_set_error(inode->i_mapping, err); + } + } __ntfs_write_delalloc_release(inode, pos, length, written, flags, iomap); filemap_invalidate_unlock(inode->i_mapping); diff --git a/fs/ntfs/iomap.h b/fs/ntfs/iomap.h index fec8b907c4..25f50744fd 100644 --- a/fs/ntfs/iomap.h +++ b/fs/ntfs/iomap.h @@ -20,4 +20,5 @@ DECLARE_IOMAP_ITER_NEXT(ntfs_dio_iomap_next); extern const struct iomap_writeback_ops ntfs_writeback_ops; extern const struct iomap_write_ops ntfs_iomap_folio_ops; extern int ntfs_dio_zero_range(struct inode *inode, loff_t offset, loff_t length); +int ntfs_zero_range_unkillable(struct inode *inode, loff_t pos, loff_t len); #endif /* _LINUX_NTFS_IOMAP_H */ -- 2.56.1