Re: [PATCH 6/7] ntfs: zero new delalloc clusters even when writeback allocates them
From: Ernest Borowski
Date: Tue Nov 24 2026 - 07:41:37 EST
On Mon, Nov 23, 2026 at 02:50:56PM +0900, Hyunchul Lee wrote:
> Could the buffered-write iomap become stale after this unlock but before
> the data is copied?
>
> For example, suppose a buffered write obtains an IOMAP_DEALLOC mapping
> for a hole. After the unlock, a mmap write can allocate the cluster
> and write another byte in the same block. If that foilo is written back
> and reclaimed before the buffered write resumes, the buffered write
> still uses its old IOMAP_DEALLOC iomap. __iomap_write_begin() can zero
> the rest of the block instead of reading the mmap data from disk.
Yes, it can. This is pre-existing: on ntfs-next, buffered writes take
no lock against page faults, and the ntfs_iomap_valid() removed by this
patch only guarded the zeroing in ->iomap_begin(), not the copy.
I wrote a reproducer for your sequence. The copy blocks on a
userfaultfd page before the write's second folio. While it is blocked,
the test stores a byte through a shared mapping into the block where the
write ends, runs sync_file_range() (which does not wait for the inode
lock), drops the page with POSIX_FADV_DONTNEED, and resumes the copy.
The byte reads back as zero on ntfs-next and with v1, and is kept with
the v2 fixes. This holds in all 16 cases (512 B, 4 KiB, 16 KiB and
64 KiB clusters; files initialized to their end or not; Ubuntu 26.04
and Arch Linux):
ntfs-next: https://github.com/xerrni/linux-ntfs/actions/runs/45292350658
ntfs-next + v1: https://github.com/xerrni/linux-ntfs/actions/runs/45292355094
ntfs-next + v1 + v2 fixes: https://github.com/xerrni/linux-ntfs/actions/runs/45292359194
Reproducer: tests/stale_write_mapping.sh (with stale_write_mapping.c) in
https://github.com/xerrni/linux-ntfs.
v2 adds a ->iomap_valid() check for the delalloc mapping of a buffered
write. ntfs_attr_map_cluster() bumps a per-inode sequence count when it
allocates clusters, and the mapping records that count. If the count
has changed, the mapping is stale unless all of its clusters are still
delalloc. A stale mapping is handled like a short write: ->iomap_end()
releases the clean delalloc clusters the write did not reach and, if
the cluster where it stopped is still delalloc, zeroes the rest of that
cluster where it is not cached and dirties it where it is, as for any
short copy. The remaining range is then mapped again. It is now
allocated, so __iomap_write_begin() reads the block.
Thanks,
Ernest