From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=220.197.31.3 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796478171; b=vjJFFMDYVBvCpTtXG/HO8NCq1Ty4LYzWHex/GT74oNYb0IXPwekZQDhPYiYhhlqvyYhk E2txg/Bqw5tnAHz0RMzGfktWFqNVIj1gvr/vaRrlp69bPQCi8hfbh1MtsA6L3WHXIpACf /U9mVYk3D/yNoo1pkkGnitIXNZjSJrE35cvpgsbqINziVeVuSbmeUTGU1Mt+h8eIaFmC0 uDg2RbHQRtPaAbBkP4EPGlchRHLvpldtTlflLjfKfS1xO5/oDu4OLJU/+0vx3IOxxvP01 mOHmUmELUOXpnJElv+kXfIqo6h94FI3H2cqhUKS2VrrnC2jdi1F6wCwVWmpizx2OhEA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796478171; h=DMARC-Filter:DKIM-Signature: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-CM-SenderInfo; bh=walKFoJeH61LulpqDKblXoQ6Ft7Rs/42LK/55vuMgOE=; b=jf13K3jlGhL0UtuESXOtlTxweu2Vm1j744Qxvyr7x5MjkVtdH2bEhOA/J6eKWQsat7+h Ai6DJjn0EsPWeuA7EFpyVJnU088kXaokgcCOVx/z8r2XMqA/jtkqLhBmEBz2AS/ybF5ge VwZFpLS3vlga3f7jUp7Enxkc7wrD7zvuQGorDM1tm/zn8HQDRlvIQ261lfb8I1Xw7lOey ugPlHPXnYhH6z22ufmRYCq8Ut2Da6n9TvLCu37iUa7fMlfRiTol8MyJCA1Vv5Eb/wjcUx UiKKq+XEoeuNgjemsZSz2HPHzRba063syjjfG1NdcaXzX0tJR3rRNHe8jKxZh1U+48Q== 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=gsmh5fUz; arc=none smtp.remote-ip=220.197.31.3 Received: from m16.mail.163.com (m16.mail.163.com [220.197.31.3]) (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 BCDE4606A322; Sat, 5 Dec 2026 13:42:49 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org BCDE4606A322 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=gsmh5fUz 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=walKFoJeH61LulpqDKblXoQ6Ft7Rs/42LK/55vuMgOE=; b=gsmh5fUzjLDktn3SBjf8drYpac/x5YxF0b+uEsz8bSc9kcA5AnkqKM20WfyC+0 a8NlbDuL7XOoqCjrt8oY1sJuwlbtqYEkqU4HfxLT86H+c/WUHpd8QWMFFQzBT1mK ajzFXg2OtsbJ8qU6D75uB5HKKT6sFKTmoVGnUiLUUkESg= Message-ID: Date: Sat, 5 Dec 2026 21:42:06 +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 v5 13/13] ntfs: allocate delalloc clusters at writeback only for dirty folios 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: <20261204-b4-ntfs-delalloc-fixes-v5-0-1cd0267df2cd@proton.me> <20261204-b4-ntfs-delalloc-fixes-v5-13-1cd0267df2cd@proton.me> Content-Language: en-US From: liubaolin In-Reply-To: <20261204-b4-ntfs-delalloc-fixes-v5-13-1cd0267df2cd@proton.me> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-CM-TRANSID:_____wD3n9WvFBRrHE9IMQ--.16483S2 X-Coremail-Antispam: 1Uf129KBjvAXoW3ur45KF45Zr1kCw4fKw1UGFg_yoW8Ar15uo Wftw4Yqr4vvryYyrW09ayDt347Wa909348JrnxAr48ZFsIqa48C3yaqwsrXFW7G3WruF1U G34xXan8CFWftFyrn29KB7ZKAUJUUUU8529EdanIXcx71UUUUU7v73VFW2AGmfu7bjvjm3 AaLaJ3UbIYCTnIWIevJa73UjIFyTuYvjxUb5rcDUUUU X-CM-SenderInfo: xolxutxrol0iasrtmqqrwthudrp/xtbC6hAR22sUFLCQAQAA36 在 2026/12/5 02:40, Ernest Borowski via B4 Relay 写道: > From: Ernest Borowski > > A buffered write into a hole maps its whole range as one delalloc run in > ->iomap_begin() and then copies the data folio by folio. Writeback that > runs meanwhile (the flusher, sync_file_range(), fadvise(DONTNEED), > FIEMAP_FLAG_SYNC) asks ntfs_write_da_iomap_begin_non_resident() for a > mapping from the first dirty folio to the end of the allocation, so > ntfs_attr_map_cluster() allocates the whole run, also the clusters whose > data has not been copied yet. Readers that do not take the inode lock > (page faults, splice()) then map those clusters, find no folio in the > page cache, and read what the reused clusters held before, data of > deleted files. The stale folios stay in the page cache. > > Reproducer: fill the free space with a marker pattern and delete it, > create a sparse file, and fill its holes with buffered writes of 1 to 8 > clusters while other processes call fadvise(DONTNEED) on the range being > written and read it through new mappings and splice(). With 4 KiB > clusters the marker shows up within seconds, with 64 KiB clusters too. > > Ask for a mapping only up to the end of the dirty folios that follow the > one being written back. Clusters larger than a page are allocated > whole, so with those also stop before a cluster that is only partly > dirty, and when the folio's own cluster has folios below > initialized_size that are not dirty, zero it on disk before it is > mapped, as for a write fault. In the usual case every folio of a > delalloc cluster is dirty, since the buffered write zeroes the parts of > its edge clusters it does not cover, so that zeroing only happens while > a copy into the cluster is still going on. The scan for dirty folios > runs for every new mapping, so it stops at the end of the run being > mapped and after 16 MiB: scanning to the end of the allocation made > fsync of a fully dirty 1 GiB file of 4000 extents take 98 to 172 s > instead of 0.36 to 0.63 s; with the bound it takes 0.29 to 0.57 s (QEMU, > RAM-backed loop device, 3 runs each). > > With clusters smaller than a page, a dirty folio can hold clean blocks > that the write in progress has mapped as delalloc but not copied into > yet: an earlier write dirtied part of the folio, and this write maps the > rest. The first blocks of the dirty folios that follow can be such > blocks too. The inode lock allows only one buffered write at a time, so > the write records its uncopied range in ntfs_inode; size_lock protects > the two fields. ->iomap_begin() sets the range while it holds the > runlist lock, before writeback can see the new delalloc clusters. > ->put_folio() moves the start of the range past each copied folio, and > ->iomap_end() clears the range after the clusters the write did not > reach are punched out. Writeback reads the range while it holds the > runlist lock, and stops the mapping where the range starts; the mapping > still covers the dirty blocks that iomap passes in. Mapping only the > dirty blocks of each folio would also close the race, but then fsync of > a new 256 MiB file with 512-byte clusters took 1.31 to 1.56 s, compared > to 0.20 to 0.30 s with 4 KiB clusters in the same run. With the > recorded range it takes 0.31 to 0.50 s, compared to 0.29 to 0.36 s with > 4 KiB clusters (QEMU, RAM-backed loop device, 5 runs each). > > Fixes: b041ca562526 ("ntfs: update iomap and address space operations") > Cc: stable@vger.kernel.org > Signed-off-by: Ernest Borowski > --- > fs/ntfs/inode.c | 2 + > fs/ntfs/inode.h | 6 ++ > fs/ntfs/iomap.c | 176 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- > 3 files changed, 173 insertions(+), 11 deletions(-) > > diff --git a/fs/ntfs/inode.c b/fs/ntfs/inode.c > index ba00c09e1b..121f6fa718 100644 > --- a/fs/ntfs/inode.c > +++ b/fs/ntfs/inode.c > @@ -558,6 +558,8 @@ void __ntfs_init_inode(struct super_block *sb, struct ntfs_inode *ni) > ni->linuxflags = 0; > ni->i_dealloc_clusters = 0; > ni->alloc_seq = 0; > + ni->uncopied_start = 0; > + ni->uncopied_end = 0; > } > > /* > diff --git a/fs/ntfs/inode.h b/fs/ntfs/inode.h > index 9ccaf55682..79e7301de5 100644 > --- a/fs/ntfs/inode.h > +++ b/fs/ntfs/inode.h > @@ -96,6 +96,10 @@ enum ntfs_inode_mutex_lock_class { > * @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. > + * @uncopied_start: start of the part of the delalloc mapping of the buffered > + * write in progress that the write has not copied into the page cache yet; > + * writeback does not allocate it. Guarded by @size_lock. > + * @uncopied_end: end of that part; none when not above @uncopied_start. > * @target: symlink buffer. > * @linuxflags: Linux-specific file attributes loaded from the $LINUXFLAGS EA. > */ > @@ -147,6 +151,8 @@ struct ntfs_inode { > } ext; > unsigned int i_dealloc_clusters; > unsigned int alloc_seq; > + loff_t uncopied_start; > + loff_t uncopied_end; > __le32 reparse_tag; > __le32 reparse_flags; > char *target; > diff --git a/fs/ntfs/iomap.c b/fs/ntfs/iomap.c > index 270bd41fa5..4fb82927b4 100644 > --- a/fs/ntfs/iomap.c > +++ b/fs/ntfs/iomap.c > @@ -5,6 +5,8 @@ > * Copyright (c) 2025 LG Electronics Co., Ltd. > */ > > +#include > +#include > #include > > #include "attrib.h" > @@ -15,12 +17,29 @@ > /* iomap->flags: ->iomap_begin() turned a hole into this delalloc mapping */ > #define NTFS_IOMAP_F_WAS_HOLE IOMAP_F_PRIVATE > > +/* > + * A buffered write (the inode lock allows one at a time) maps its range as > + * delalloc before it copies the data folio by folio. Record the part of the > + * mapping it has not copied yet: writeback must not allocate it, see > + * ntfs_writeback_run_end(). > + */ > +static void ntfs_set_uncopied(struct ntfs_inode *ni, loff_t start, loff_t end) > +{ > + unsigned long flags; > + > + write_lock_irqsave(&ni->size_lock, flags); > + ni->uncopied_start = start; > + ni->uncopied_end = end; > + write_unlock_irqrestore(&ni->size_lock, flags); > +} > + > static void ntfs_iomap_put_folio_non_resident(struct inode *inode, loff_t pos, > unsigned int len, struct folio *folio) > { > struct ntfs_inode *ni = NTFS_I(inode); > unsigned long sector_size = 1UL << inode->i_blkbits; > loff_t start_down, end_up, init; > + unsigned long flags; > > start_down = round_down(pos, sector_size); > end_up = (pos + len - 1) | (sector_size - 1); > @@ -61,6 +80,14 @@ static void ntfs_iomap_put_folio_non_resident(struct inode *inode, loff_t pos, > offset2, > folio_size(folio)); > } > + /* [pos, pos + len) is copied, or zeroed, and dirty */ > + if (len) { > + write_lock_irqsave(&ni->size_lock, flags); > + if (ni->uncopied_start < ni->uncopied_end && > + pos <= ni->uncopied_start && ni->uncopied_start < pos + len) > + ni->uncopied_start = pos + len; > + write_unlock_irqrestore(&ni->size_lock, flags); > + } > folio_unlock(folio); > folio_put(folio); > } > @@ -521,17 +548,19 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_ > 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); > - > - if (lcn < LCN_DELALLOC) > - ntfs_hold_dirty_clusters(vol, max_clu_count); > > rl_length = ntfs_cluster_to_bytes(vol, max_clu_count); > if (length > rl_length - vcn_ofs) > iomap->length = rl_length - vcn_ofs; > else > iomap->length = length; > + /* before writeback can see the new delalloc clusters */ > + ntfs_set_uncopied(ni, offset, offset + iomap->length); > + up_write(&ni->runlist.lock); > + mutex_unlock(&mrec_ni->mrec_lock); > + > + if (lcn < LCN_DELALLOC) > + ntfs_hold_dirty_clusters(vol, max_clu_count); > > iomap->flags = IOMAP_F_NEW; > if (lcn <= LCN_HOLE) { > @@ -576,6 +605,7 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_ > iomap->length, 0, > IOMAP_WRITE, > iomap); > + ntfs_set_uncopied(ni, 0, 0); > return err; > } > } > @@ -600,6 +630,8 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_ > #define NTFS_IOMAP_FLAGS_DIO BIT(2) > #define NTFS_IOMAP_FLAGS_MKWRITE BIT(3) > #define NTFS_IOMAP_FLAGS_WRITEBACK BIT(4) > +/* Zero the clusters ->iomap_begin() allocates before mapping them. */ > +#define NTFS_IOMAP_FLAGS_ZERO_NEW BIT(5) > > static int ntfs_write_da_iomap_begin_non_resident(struct inode *inode, > loff_t offset, loff_t length, unsigned int flags, > @@ -626,8 +658,9 @@ static int ntfs_write_da_iomap_begin_non_resident(struct inode *inode, > * of a cluster that the folio does not cover has to be zeroed on disk > * before other readers can map the cluster. > */ > - zero_new = ntfs_iomap_flags & NTFS_IOMAP_FLAGS_MKWRITE && > - (offset | length) & vol->cluster_size_mask; > + zero_new = (ntfs_iomap_flags & NTFS_IOMAP_FLAGS_MKWRITE && > + (offset | length) & vol->cluster_size_mask) || > + ntfs_iomap_flags & NTFS_IOMAP_FLAGS_ZERO_NEW; > down_write(&ni->runlist.lock); > err = ntfs_attr_map_cluster(ni, vcn, &start_lcn, &lcn_count, > max_clu_count, &balloc, update_mp, > @@ -970,6 +1003,9 @@ static int ntfs_write_iomap_end(struct inode *inode, loff_t pos, loff_t length, > !(flags & IOMAP_FAULT) && written < length) > ntfs_write_delalloc_release(inode, pos, length, written, flags, > iomap); > + /* only a buffered write maps delalloc; what it did not copy is gone */ > + if (iomap->type == IOMAP_DELALLOC) > + ntfs_set_uncopied(NTFS_I(inode), 0, 0); > return written; > } > > @@ -998,17 +1034,135 @@ static int ntfs_dio_iomap_begin(struct inode *inode, loff_t offset, > DEFINE_IOMAP_ITER_NEXT_END(ntfs_dio_iomap_next, ntfs_dio_iomap_begin, > ntfs_write_iomap_end); > > +/* > + * Longest mapping writeback asks for at a time. With the end of the run > + * (ntfs_writeback_run_end()) it bounds the scan of ntfs_dirty_range_end(), > + * which runs for every new mapping: unbounded, a fully dirty file of many > + * extents is scanned once per extent. It is the largest cluster size > + * (4096 sectors of 4 KiB), so a mapping can always cover a whole cluster. > + */ > +#define NTFS_WRITEBACK_MAP_MAX SZ_16M > + > +/* > + * Return where the mapping for the dirty blocks [@offset, @offset + @len) of > + * @folio can end, at most @end: at the end of the run holding @offset, so the > + * scan for dirty folios need not go further either. With clusters smaller > + * than a folio, the clean blocks of a dirty folio, and the leading ones of > + * the dirty folios that follow, can be delalloc clusters that the buffered > + * write in progress has not copied into yet: stop where they start, but > + * always cover the blocks iomap passed in. Larger clusters are zeroed or > + * copied whole before writeback can see them dirty. > + */ > +static loff_t ntfs_writeback_run_end(struct inode *inode, struct folio *folio, > + loff_t offset, unsigned int len, > + loff_t end) > +{ > + struct ntfs_inode *ni = NTFS_I(inode); > + struct ntfs_volume *vol = ni->vol; > + struct runlist_element *rl; > + unsigned long flags; > + > + down_read(&ni->runlist.lock); > + rl = __ntfs_attr_find_vcn_nolock(&ni->runlist, > + ntfs_bytes_to_cluster(vol, offset)); > + if (!IS_ERR(rl) && rl->length) { > + end = min_t(loff_t, end, > + ntfs_cluster_to_bytes(vol, rl->vcn + rl->length)); > + /* > + * Read under the runlist lock: the write records the range > + * before it makes the clusters delalloc, and forgets it only > + * after it punched out what it did not copy. > + */ > + if (rl->lcn == LCN_DELALLOC && > + vol->cluster_size < folio_size(folio)) { > + read_lock_irqsave(&ni->size_lock, flags); > + if (ni->uncopied_start < ni->uncopied_end && > + offset < ni->uncopied_end) > + end = min(end, max(round_down(ni->uncopied_start, > + vol->cluster_size), > + offset + len)); > + read_unlock_irqrestore(&ni->size_lock, flags); > + } > + } > + up_read(&ni->runlist.lock); > + return end; > +} > + > +/* > + * Return where the run of dirty folios that follows @folio ends, at most > + * @end. A buffered write maps its whole range as delalloc before it copies > + * the data folio by folio, so writeback that runs meanwhile must not allocate > + * the clusters past the dirty folios: readers that do not take the inode > + * lock (page faults, splice) would map the new clusters and read what they > + * held before. > + */ > +static loff_t ntfs_dirty_range_end(struct address_space *mapping, > + struct folio *folio, loff_t end) > +{ > + pgoff_t index = folio_next_index(folio); > + pgoff_t last = DIV_ROUND_UP(end, PAGE_SIZE); > + > + while (index < last && > + xa_get_mark(&mapping->i_pages, index, PAGECACHE_TAG_DIRTY)) > + index++; > + return min_t(loff_t, (loff_t)index << PAGE_SHIFT, end); > +} > + > +/* > + * With clusters larger than a page, a cluster can be only partly dirty while > + * a buffered write is still copying into it. Return where the mapping for > + * the folio at @offset should end so that writeback does not allocate a > + * partly dirty cluster other than the folio's own, and set @zero when the > + * folio's own cluster has folios below initialized_size that are not dirty: > + * it then has to be zeroed on disk before it is mapped. > + */ > +static loff_t ntfs_writeback_cluster_end(struct inode *inode, > + struct folio *folio, loff_t offset, > + loff_t end, bool *zero) > +{ > + struct ntfs_inode *ni = NTFS_I(inode); > + u32 cs = ni->vol->cluster_size; > + loff_t cstart = round_down(offset, cs); > + loff_t cend = cstart + cs; > + loff_t init = round_up(ni->initialized_size, PAGE_SIZE); > + pgoff_t index; > + Hi Ernest, Thanks for the series. I have a question about 13: > + *zero = false; > + if (cs <= PAGE_SIZE) > + return end; So for clusters up to a page, writeback never zeroes the cluster before publishing its mapping. Is there a case where a dirty folio still has clean blocks below initialized_size here? The case I'm thinking of is a short copy. The rest of the cluster is then left to patch 10, which runs in ->iomap_end(), after the folio has been unlocked again. Could writeback get to the folio before that and allocate the whole cluster, writing only the dirty blocks? Concretely, with 4 KiB clusters and 512-byte blocks: a 4096-byte write at a cluster boundary into a hole, with the whole range below initialized_size, and the copy stops after 512 bytes (the source buffer is readable for the first 512 bytes only, the rest is inaccessible). The folio had been read before, so it is uptodate and the short copy is accepted, and one block is dirty. If writeback then maps the whole cluster and writes those 512 bytes, and the folio is dropped after that writeback has completed but before ->iomap_end() (DONTNEED, reclaim),wouldn't ntfs_dirty_cluster_tail() take the branch for a mapped cluster,find no folio and just return? The sizes don't change in that case, so the rollback wouldn't help either, and the tail of the cluster would keep its old contents. Could you help check whether this can actually happen? If it can, maybe the zeroing could be decided for clusters up to a page as well, from the dirty blocks of the cluster instead of whole folios. The zeroing you added to ntfs_attr_map_cluster() in patch 12 seems like a good place for that. Just a thought. Thanks, Baolin. > + if (end < min(cend, init)) > + *zero = true; > + for (index = cstart >> PAGE_SHIFT; !*zero && index < folio->index && > + ((loff_t)index << PAGE_SHIFT) < init; index++) > + if (!xa_get_mark(&inode->i_mapping->i_pages, index, > + PAGECACHE_TAG_DIRTY)) > + *zero = true; > + if (*zero || end < cend) > + return min(end, cend); > + return round_down(end, cs); > +} > + > static ssize_t ntfs_writeback_range(struct iomap_writepage_ctx *wpc, > struct folio *folio, u64 offset, unsigned int len, u64 end_pos) > { > if (offset < wpc->iomap.offset || > offset >= wpc->iomap.offset + wpc->iomap.length) { > + struct inode *inode = wpc->inode; > + loff_t end = min_t(loff_t, NTFS_I(inode)->allocated_size, > + offset + NTFS_WRITEBACK_MAP_MAX); > + int ntfs_iomap_flags = NTFS_IOMAP_FLAGS_WRITEBACK; > + bool zero; > int error; > > - error = __ntfs_write_iomap_begin(wpc->inode, offset, > - NTFS_I(wpc->inode)->allocated_size - offset, > - IOMAP_WRITE, &wpc->iomap, > - NTFS_IOMAP_FLAGS_WRITEBACK); > + end = ntfs_writeback_run_end(inode, folio, offset, len, end); > + end = ntfs_dirty_range_end(inode->i_mapping, folio, end); > + end = ntfs_writeback_cluster_end(inode, folio, offset, end, > + &zero); > + if (zero) > + ntfs_iomap_flags |= NTFS_IOMAP_FLAGS_ZERO_NEW; > + error = __ntfs_write_iomap_begin(inode, offset, end - offset, > + IOMAP_WRITE, &wpc->iomap, > + ntfs_iomap_flags); > if (error) > return error; > } >