Re: [PATCH v2 02/11] iomap: add a helper to mark a range of a folio dirty

From: Ernest Borowski

Date: Wed Nov 25 2026 - 12:38:19 EST


On Wed, Nov 25, 2026 at 06:50:16AM +0100, Christoph Hellwig wrote:
> If we end up with this, iomap_dirty_folio should be rewritten
> to call this instead of duplicating the logic.

Done for v3. Both now share a static helper, and iomap_dirty_folio()
keeps passing the mapping it is given rather than folio->mapping, as
->dirty_folio can be called without the folio lock.

> But I'm a little sceptical the use of this can be correct. I'll try
> to look into it, but it might take some time to understand the ntfs
> code.

To save you some of that time: the user is ntfs_dirty_cluster_tail() in
patch 10.

ntfs reserves delalloc space in clusters, which can be larger than a
block and larger or smaller than a folio, and it has no unwritten
extents. If the copy of a buffered write into a hole stops early (an
unmapped source page, a fatal signal), the rest of the cluster it
stopped in is never written. Writeback then allocates the whole cluster
and writes only the copied blocks, so a read of the rest after
drop_caches returns stale disk data.

Before the unused delalloc clusters are punched out, ntfs walks the rest
of that cluster under the invalidate lock:

- in an uptodate folio in the page cache, it locks the folio and uses
this helper to dirty only the blocks in that range. Those blocks
already hold the right contents: zeroes, or data a write through mmap
added since;
- otherwise (no folio, or not uptodate), if the cluster is still
delalloc, it zeroes the range with iomap_zero_range().

It cannot zero an uptodate folio without overwriting such mmap data,
and it cannot use folio_mark_dirty(), because that dirties the whole
folio. When clusters are smaller than the folio (1k and 2k clusters, or
a large folio), the other blocks can be holes or delalloc clusters the
write never reached: the punch would then keep those clusters reserved,
and writeback would allocate clusters that hold only zeroes.

The alternative needs no iomap change: when writeback allocates a
cluster, zero on the disk the part the write does not cover, before the
cluster goes into the runlist. A later series of mine already does this
for clusters allocated by a write fault. Doing it in writeback costs
extra zeroing I/O for every partly written cluster, but it also closes
the race where writeback allocates the cluster before
ntfs_dirty_cluster_tail() runs, which patch 10 leaves to that later
series. If you think that is the better way, I can rework patch 10 like
that and drop this patch.