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=1796146992; b=ior9ipe89XXZzu0ENr9XwSOQc+IZP8CDkaV6YhpJTp4YvLwgydN95x7fuHy3lahFg6J3 ZcFG28Ryt4cmfkn4Ie8Gc/8TswdDAGrk21vFQFW1e+WFfAroVS1BAg7BWgrOPMwnAbbd1 sgTgB5HxzgPjVPFdUKvOHwAdwpqLW/hv3wPbaitRF4+FVoT3SvJrPg+/+USRqbMkzPFoY zrhvLUbq1iBxMnmXuZ3aEOXJqS8BRO42SJugpawzdSjYNtn38GvtDhw/mZ7FVaQ4gxmLE fO2VhmI3zTyWJTiPH88OE17LMcvyUBjoDo316PmdeUYuJwz+9VJOCSqi/JdkZEfP2rg== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796146992; 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=h4ykzz7wH+FDcjX7bX23Fd44KzTtDXg8I+lA23v/IQw=; b=Qhhu/xeDYFWQPEAXE9RqwHjHmk0tgBr32btzivptcDtyWXsTve5V2d6IOIexLc66DTd9 Jqm5M5nNwqiWWcLGnz/mIJLfbSoYjjk9hIJyKqhYy6cmdUBJysGQMVt+AAWqU4dm/Czpn NVjRFV3kekS9IBIPCO6UH8MPZO/yIH0xvlD9OSZwxqef25/cJj2X46A8zA+w4mB6KoqvV Ay7VXRKaQp5zhwqCieGkGt2L8tolgladrC9G7U2o6H6TU/GTksYh4S4B9j+tqNybeYE1W em/3HO5a8Rnnen9nm3w8jTtOOGusWQOkPJR3GGsnD6goHmXJ3ImbibmxG6YDQueAaLA== 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=g61CmZR+; 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 556B460A2610; Tue, 1 Dec 2026 17:43:12 +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=g61CmZR+ 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=1796146991; b=naK8l8aqJICxAeWyf+N9SDl0Bu7AilHHp7RHtCHmlUMGNGL+zR0ZSMfFNLs3SFAdvwOZ IRmyItsavBpi0EMQFswZtXua8UPuYvAsGhVu1QZgvoMzNpvEHuNDkblF81s6KxMN6kVEN 1GcXsFzP/Inj0lBA/kiTC1RKyijH170aBvW7psIw5++4UWp3VuP/jx0fAjL7qF7wHhkca +kVLqa2KNqUFgAKTWT7NrP/fNVEgoaP4f2Li11JHnVfBvZe9nkIlykYpLlPXLA6aZlJU1 MrdN6HXLSnxWXIPxaiOq76qBvZEQB974pcvcM11MIODDcjjBEb4J76fKeaBM27FhiXg== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1796146991; 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=h4ykzz7wH+FDcjX7bX23Fd44KzTtDXg8I+lA23v/IQw=; b=fqikgPMYJiR4U0TsTLblPmtD+cdY+hmO15haKW0usY8k+XRNCEoDKXFy6KovL1ms4oE3 GIha21ZWkZ984v61Ka5ReeqXxWa8YtD9q73o2AkEZkvXEWBmnFjA7uwa8FFoOkFT2Qi0B XLwHaqOP22moCFcBNLCLAgpRiTs15XGPszFKGLjnDrHmYa5r+Qxz2XQaBHlX5tn9uT/N+ KKY/q72HYhrZA5rCCom01xSrFNPUCDcTUg7l4e1Ld0W/YgUY/tEQw6VrFQVG6oD03wjpD SzRHUzCZMWU25Rv8CfYoLXRSGDW3wsTdrc8oAfvnnqrCelm5+5Cp4qkWWnOMDhPruJw== 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 563E41F0092F; 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=1796146991; bh=h4ykzz7wH+FDcjX7bX23Fd44KzTtDXg8I+lA23v/IQw=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=g61CmZR+7002LTgykz0ab5qXmH1muaYd/Tp2FsfeA/bXlEb0A0u6N8az4LtGIRw6Q N9/H22YZsJWqGA+hiVhTqxPIHZvQlf87qj8aeJBYw/r6dtJztMKqHZl7sDrQ40cpe4 EUtCpR7nFUCPU+4e/rr3+wUkuHJdQi45uU9EDmB/QlVxf1wKEODZS4wRbsp/mEb7mm eL/7LbOa/YvvYJWQZSYv94WTJsiM9U/Uy0swP0NTR74j3WeseSZIxcKcNag60iykR3 hsEx8X5bdVySTat2Xwmo+ZMtKsHLJ6M10q296Vnk5JJElBGX9zEJbosJLKqiFX34Jf Fgw4HN0ETDz+w== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id B77BD4EF33; Tue, 1 Dec 2026 17:42:39 +0000 (UTC) From: Ernest Borowski via B4 Relay Date: Tue, 01 Dec 2026 18:42:45 +0100 Subject: [PATCH v4 10/11] 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: <20261201-b4-ntfs-delalloc-fixes-v4-10-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=8758; i=ernest.borowski.git@proton.me; s=default; h=from:subject:message-id; bh=yGPS7e8ZmrJEI43ptp8XVHTwPNAYlnt8WmRHj2wl3K4=; b=kA0DAAoWcwdpo3piFP8ByyZiAGsPBwyh0aV1YDPvs9pdnoKy0lMcDsDgiPrNUhCOTtFhRyf3Q oh1BAAWCgAdFiEEiP/YVExU9vKmXiqOcwdpo3piFP8FAmsPBwwACgkQcwdpo3piFP853gD/bcFr kjik4DaT/RCqNDPqNxO4ILqSj8lQX/r3R6tOugoA/3MV3FD1AqJFcQl7ctdz6msLv+zG/R+fLR6 2tEbz4kYN 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: under the invalidate lock, the blocks of that range in folios already in the page cache are marked dirty as they are, with iomap_dirty_folio_range() (they hold the zeroes __iomap_write_begin() put there, or data an mmap write added meanwhile, which must not be lost), and the rest of the range is zeroed while it is still delalloc (once a write fault or writeback has allocated it, an mmap write's data may have left the page cache; a follow-up series makes both zero a partly written cluster on the disk before mapping it). 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 Signed-off-by: Ernest Borowski --- fs/ntfs/file.c | 5 +-- fs/ntfs/iomap.c | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ fs/ntfs/iomap.h | 1 + 3 files changed, 99 insertions(+), 2 deletions(-) diff --git a/fs/ntfs/file.c b/fs/ntfs/file.c index 31ff35176e..3363f0abdd 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 fd94d4b34f..2ee08fa738 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) { @@ -481,6 +484,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; @@ -800,11 +806,100 @@ 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. Folios in the + * page cache keep their contents (the zeroes __iomap_write_begin() put + * there, or data an mmap write added meanwhile), and only their blocks in + * the range are dirtied: other clean blocks of the folio may be holes, or + * delalloc clusters the write did not reach, which the caller punches out. + * The rest of the range is zeroed only while the cluster is still delalloc: + * once a write fault or writeback has allocated it, what an mmap write put + * there since may be on the disk only, and zeroing the page cache would + * overwrite it. 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; + int err = 0; + + lockdep_assert_held_write(&inode->i_mapping->invalidate_lock); + while (!err && start < end) { + 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); + if (ntfs_pos_is_delalloc(inode, start)) + err = ntfs_zero_range_unkillable(inode, start, + next - start); + } else { + bool uptodate = folio_test_uptodate(folio); + size_t off = offset_in_folio(folio, start); + + next = min_t(loff_t, folio_pos(folio) + + folio_size(folio), end); + if (uptodate) + iomap_dirty_folio_range(folio, off, + next - start); + folio_unlock(folio); + folio_put(folio); + if (!uptodate && ntfs_pos_is_delalloc(inode, start)) + err = ntfs_zero_range_unkillable(inode, start, + next - start); + } + start = next; + } + return err; +} + 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