From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=220.197.31.5 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796092055; b=gJjzz2p5y5V1Gj4J8jhMduD2S2bjcOsAiYCUOo+ZPCm4Y2CHcl8T4DLX/YCRQQScqMha +wC92nhJEq3/LMdUOTNaiuL10vZa3aEvxRKqtNmr9GSkWny1w3O6KVAxjAIwKPxxeNQUB 0odLWBnTdJLW0YpuxeLOjvGUtQSnNni/3v5857GsdO4vnX/RnOnrp41xBkxOtuUcw3xpe GR7hFWTwYOdHDKAGrjd/b5Qjm2NoS0KUPY2Hj2rYbzhKgUHgsiV4rfbi9NpXC/R7s64kf l74aIxrUe8mNfxhVXWOD+/1LqdbMvYZAvJ0oTZy+K7JaHUw/NcBYQtYN8aGD1f2zxNQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796092055; h=DMARC-Filter:DKIM-Signature:Received:Message-ID:Date:MIME-Version: User-Agent:Subject:To:Cc:References:Content-Language:From:In-Reply-To: Content-Type:Content-Transfer-Encoding:X-CM-TRANSID: X-Coremail-Antispam:X-Originating-IP:X-CM-SenderInfo; bh=CPU5Iz+eOW0yrvX1v1Sx9RS0Ai5smVXwTmMGqHmZm2k=; b=ixypjk6mT9DiOLvAKuAxfxAQahXI7X2Mv+B4IXdafBSw7hMghihIiteVPpt5mI8wzkr7 iTBlSpH4Qpm8Aw5HHUGfuP/Ecs+RyVMQWCNGkIdY6r1rx4aL0I20hZc1Sh1ZOSvrOFfgy bOtGqloC5xW8xd3XgKBo48abS4cFeFngItvhx1PuGJnyyv87M5fDDQPL9eVSUYmEhJHGd w8DlaksbrM4wzCwAi6hYlm0vmYiG1AhOXckMDeMDkSMWE4b1OqlEXEs+zAcZ8B6UUD1pA kuiIjZJyye3mMvWuUOiPZMnNb6MFRePmHF9O1p95qMZBClz33q9qLqWSHdQ+2nteLgg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=163.com; spf=pass smtp.mailfrom=163.com; dkim=pass header.d=163.com header.i=@163.com header.a=rsa-sha256 header.s=s110527 header.b="EZ/48vgP"; arc=none smtp.remote-ip=220.197.31.5 Received: from m16.mail.163.com (m16.mail.163.com [220.197.31.5]) (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.subspace.kernel.org (Postfix) with ESMTPS id ED658606A327; Tue, 1 Dec 2026 02:27:32 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org ED658606A327 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=163.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.a=rsa-sha256 header.s=s110527 header.b=EZ/48vgP DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=Message-ID:Date:MIME-Version:Subject:To:From: Content-Type; bh=CPU5Iz+eOW0yrvX1v1Sx9RS0Ai5smVXwTmMGqHmZm2k=; b=EZ/48vgPZ7xUkb6Et/R4Ie4s01GdpdzmE5cbfQNEZJZBQ7boqIeR1RhhdbVRQw 3OWRdMTPhsOJuzan36wISU6cGmxyti34hzMmgWxN3NcCHxIFSGC9TLdgFsVsvCs4 TbmthOqLWjSFdXROu1ccOmP1ETEtY24SYmQP+A8SY98vs= Received: from [IPV6:2409:8900:1ec4:9e84:6cd2:9e29:d7b8:f50e] (unknown []) by gzsmtp4 (Coremail) with SMTP id PygvCgDXIotqMA5rRL3SJA--.55737S2; Tue, 01 Dec 2026 10:26:52 +0800 (CST) Message-ID: <7bba2d49-ef11-4fe8-b949-1f396c3207b0@163.com> Date: Tue, 1 Dec 2026 10:26:49 +0800 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 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v3 10/11] ntfs: zero the rest of a new cluster after a short buffered write To: ernest.borowski.git@proton.me, Namjae Jeon , Hyunchul Lee 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 References: <20261130-b4-ntfs-delalloc-fixes-v3-0-874d34a6f1fe@proton.me> <20261130-b4-ntfs-delalloc-fixes-v3-10-874d34a6f1fe@proton.me> Content-Language: en-US From: liubaolin In-Reply-To: <20261130-b4-ntfs-delalloc-fixes-v3-10-874d34a6f1fe@proton.me> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-CM-TRANSID:PygvCgDXIotqMA5rRL3SJA--.55737S2 X-Coremail-Antispam: 1Uf129KBjvJXoWxKF48KF1fJr47uF18Aw1rCrg_yoWfAr17pr W5KF98KrZrXFZrWrn3tF12qryF93WSqrWUu343JwnxZ3Z0qr4xKFyqg3ZY9F18JrZ7Ja1x XF4Yk347urWDA3DanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07U5nYwUUUUU= X-CM-SenderInfo: xolxutxrol0iasrtmqqrwthudrp/xtbC6QyFUGsOMGxLVwAA3V 在 2026/11/30 20:58, Ernest Borowski via B4 Relay 写道: > 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 is done from a worker > then. > > Fixes: b041ca562526 ("ntfs: update iomap and address space operations") > Cc: stable@vger.kernel.org > Signed-off-by: Ernest Borowski > --- > fs/ntfs/iomap.c | 141 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 141 insertions(+) > > diff --git a/fs/ntfs/iomap.c b/fs/ntfs/iomap.c > index fd94d4b34f..928ccb685a 100644 > --- a/fs/ntfs/iomap.c > +++ b/fs/ntfs/iomap.c > @@ -5,6 +5,7 @@ > * Copyright (c) 2025 LG Electronics Co., Ltd. > */ > > +#include > #include > > #include "attrib.h" > @@ -12,6 +13,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 +485,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 +807,145 @@ static void __ntfs_write_delalloc_release(struct inode *inode, loff_t pos, > ntfs_write_delalloc_punch); > } > > +struct ntfs_zero_work { > + struct work_struct work; > + struct inode *inode; > + loff_t pos; > + loff_t len; > + int err; > +}; > + > +static void ntfs_zero_range_workfn(struct work_struct *work) > +{ > + struct ntfs_zero_work *zw = container_of(work, struct ntfs_zero_work, > + work); > + > + zw->err = iomap_zero_range(zw->inode, zw->pos, zw->len, NULL, > + ntfs_seek_iomap_next, &ntfs_iomap_folio_ops, > + NULL); > +} > + > +/* > + * Like ntfs_zero_range(), but also for a writer that has a fatal signal > + * pending: iomap_zero_range() then gives up with -EINTR, and a write cut > + * short by SIGKILL is one whose cluster tail must still be zeroed. Zero > + * from a worker in that case, which has no signal to see; the caller's > + * invalidate lock still keeps page faults out of the range. > + */ > +static 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; > + > + if (!fatal_signal_pending(current)) { > + err = ntfs_zero_range(inode, pos, len); > + /* the signal may also come while it zeroes */ > + if (err != -EINTR || !fatal_signal_pending(current)) > + return err; > + } > + > + lockdep_assert_held_write(&inode->i_mapping->invalidate_lock); > + INIT_WORK_ONSTACK(&zw.work, ntfs_zero_range_workfn); > + queue_work(system_unbound_wq, &zw.work); > + flush_work(&zw.work); > + destroy_work_on_stack(&zw.work); > + return zw.err; > +} > + > +/* 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); > + } Hi Ernest, Could we also call mapping_set_error(inode->i_mapping, err) here? The write can still return a positive short count when the tail zeroing fails, and ntfs_file_fsync() doesn't check NVolErrors, so a subsequent fsync() could succeed without reporting this error. Returning the number of bytes already copied is fine, but we should preserve the zeroing error so that fsync() can report it. Thanks, Baolin. > + } > __ntfs_write_delalloc_release(inode, pos, length, written, flags, > iomap); > filemap_invalidate_unlock(inode->i_mapping); >