Re: [RFC PATCH v15 07/10] ocfs2: switch dio write path from buffer_head to iomap

From: Heming Zhao

Date: Thu Dec 03 2026 - 02:49:32 EST


On 12/2/26 22:35, Joseph Qi wrote:
From: Heming Zhao <heming.zhao@xxxxxxxx>

Replace ocfs2_dio_wr_get_block() with ocfs2_dio_wr_map_blocks(), which
hands iomap a mapping of as many clusters as can be described by a single
struct iomap: up to 16MB, stopped at the first cluster that is not of the
same kind as the first one. ocfs2_iomap_begin() serves writes through it,
and ocfs2_dio_end_io() converts the unwritten extents the write recorded
and updates the inode size.

In the buffer_head path, ocfs2_dio_wr_get_block() stores dwc in
bh->b_private, which lets it detect the first call for an I/O chunk and
invoke ocfs2_add_inode_to_orphan() to protect the extent allocation.
iomap provides no similar private field, so ocfs2_add_inode_to_orphan()
moves to ocfs2_file_write_iter().

It is also impossible to pass the dwc to iomap_dio_ops.end_io: the
callback cannot distinguish between I/Os initiated by different threads
concurrently, and without that context it could delete an orphan entry
created by another thread. The orphan removal therefore moves to
ocfs2_file_write_iter() as well, where inode_lock() serializes the writers
and IOMAP_DIO_FORCE_WAIT makes an extending write complete before the
function returns, so i_size is already updated when the entry is removed.

iomap calls ->iomap_begin() once per mapping but ->end_io() once per I/O,
and the latter only gets the kiocb, so the kiocb is the identity of the
I/O: ocfs2_file_write_iter() passes it in iomap_iter.private,
ocfs2_iomap_next() forwards it to ocfs2_dio_wr_map_blocks(), and each
unwritten record stores it in ue_owner. ocfs2_dio_collect_unwritten()
finds the records of one I/O by that owner.

A direct write waits for unwritten records belonging to another dio before
it takes the re-write fast path, not after. A completion which fails part
way through its conversion has already made some clusters written and
dropped ip_alloc_sem and the inode lock, but it still owns the records and
is about to write the zeroed head/tail buffers over those clusters. The
fast path would see them as MAPPED and hand them out, and the new data
would be overwritten by those zeroes. The records are only removed after
that cleanup, so waiting first excludes the whole window. Under
IOMAP_NOWAIT the wait becomes -EAGAIN.

Unlike ocfs2_dio_wr_get_block(), the zeroing of the gap between i_size and
the write is done once per dio by ocfs2_dio_zero_eof() before the io is
issued. Zeroing it per mapping would zero blocks that earlier mappings of
this same dio have already written, because i_size is only updated at
completion; that made xfstests generic/538 fail.

On failure only the first and the last cluster of the touched range are
cleaned up, through ocfs2_dio_cleanup_clusters(): those are the only
clusters ocfs2_grab_folios_direct() fills folio windows for, and the
clusters in between can hold dirty folios of other writes sharing the
cluster. ocfs2_write_range_buffers() writes the buffers directly when some
clusters may already have been converted, because generic writeback
refuses to submit folios beyond i_size.

ocfs2_dio_clear_orphan_locked() only removes the orphan entry. Clusters
beyond i_size are left alone: there is no on-disk way to tell what this
write allocated from unwritten extents preallocated with
fallocate(FALLOC_FL_KEEP_SIZE), so truncating to i_size would free the
preallocated space as well. They stay until a later truncate or recovery.
The in-memory marker is only dropped once the entry is really gone, so a
failed write leaves it for the next extending write to recover, which
ocfs2_add_inode_to_orphan() already does before adding its own.

The pid marker is a strict ownership token: every path that leaves the
entry behind zeroes ip_dio_orphan_pid, so a later write by the same task
cannot match it and remove the entry without the truncate recovery.
ocfs2_dio_clear_orphan() only removes the entry once i_size covers the
range the write meant to write, or nothing is allocated beyond i_size;
otherwise it leaves the entry for recovery and gives up ownership.

Writes that span EOF may trigger an i_size update at completion, and
within i_size there can still be uncommitted metadata such as the CoW done
by ocfs2_prepare_inode_for_write(). Both set IOMAP_F_DIRTY so iomap cannot
take the FUA-only fast path, which would clear IOMAP_DIO_NEED_SYNC and skip
generic_write_sync(), leaving that metadata uncommitted for O_DSYNC.

With the write path on iomap, ocfs2 no longer needs LEGACY_DIRECT_IO.

A buffered write needs the rw lock EX, so every path that falls back to
generic_perform_write() upgrades first: the -ENOTBLK / zero-length bounce
inside the direct branch as well as the demoted direct branch. Under
coherency=buffered a PR lock would let another node write the same range
directly while this node does a read-modify-write in the page cache.
ocfs2_dio_upgrade_rw_lock() drops PR and takes EX rather than asking for
one upgrade, which can deadlock in the cluster lock manager when two nodes
upgrade at once.

ip_dio_orphan_pid is reset in ocfs2_alloc_inode(), not only in the slab
constructor. The slab object is reused for a new inode, and a stale pid
that happens to be reused by another task would let an unrelated write
match the marker and delete a crash-left orphan entry without the truncate
recovery, leaking the clusters beyond i_size.

Clearing the orphan entry takes the inode cluster lock, which blocks, so
IOCB_NOWAIT does not go there. The marker stays for a later write or for
the recovery.

ocfs2_free_write_ctxt() runs the cached deallocs after unlocking the
folios, like ocfs2_write_end_nolock() does. Without it the error paths
that free a write context lose the extents it had cached for deletion.

A write that hits an error does not convert its unwritten extents.
iomap keeps dio->size counting bios that failed at completion, and a
short write can leave the tail of the last cluster unzeroed, so
converting [offset, offset + size) would mark uninitialized disk blocks
written and expose stale data. Leave the extents unwritten, drop the
zeroed folios and return the error, as ext4 and xfs do; the extents read
back as zeroes until a later write converts them.

Co-developed-by: Joseph Qi <joseph.qi@xxxxxxxxxxxxxxxxx>
Signed-off-by: Joseph Qi <joseph.qi@xxxxxxxxxxxxxxxxx>
Signed-off-by: Heming Zhao <heming.zhao@xxxxxxxx>
---
fs/ocfs2/Kconfig | 1 -
fs/ocfs2/aops.c | 1062 +++++++++++++++++++++++++++++++++-------------
fs/ocfs2/aops.h | 33 +-
fs/ocfs2/file.c | 275 ++++++++++--
fs/ocfs2/inode.h | 8 +-
fs/ocfs2/super.c | 2 +
6 files changed, 1032 insertions(+), 349 deletions(-)

diff --git a/fs/ocfs2/Kconfig b/fs/ocfs2/Kconfig
index bf1678a5eb01e..74bc2008ef0bf 100644
--- a/fs/ocfs2/Kconfig
+++ b/fs/ocfs2/Kconfig
@@ -9,7 +9,6 @@ config OCFS2_FS
select QUOTA_TREE
select FS_IOMAP
select FS_POSIX_ACL
- select LEGACY_DIRECT_IO
help
OCFS2 is a general purpose extent based shared disk cluster file
system with many similarities to ext3. It supports 64 bit inode
diff --git a/fs/ocfs2/aops.c b/fs/ocfs2/aops.c
index 4b76be5909689..8d903c55749a9 100644
--- a/fs/ocfs2/aops.c
+++ b/fs/ocfs2/aops.c
@@ -951,6 +951,8 @@ static void ocfs2_free_write_ctxt(struct inode *inode,
... ...
+
+/*
+ * Undo the orphan dir entry an extending direct write has added.
+ *
+ * Clusters beyond i_size are left alone. There is no on-disk way to tell
+ * what this write allocated from unwritten extents preallocated with
+ * fallocate(FALLOC_FL_KEEP_SIZE), so truncating to i_size would free the
+ * preallocated space as well. They stay until a later truncate or recovery.
+ *
+ * TODO: ocfs2_recover_orphans() still truncates to i_size, so dead-slot
+ * recovery can still free FALLOC_FL_KEEP_SIZE extents. That predates the
+ * iomap conversion.
+ */
+static void ocfs2_dio_clear_orphan_locked(struct inode *inode,
+ struct buffer_head *di_bh)
+{
+ struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
+ struct ocfs2_inode_info *oi = OCFS2_I(inode);
+ struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
+ int ret = 0;
+
+ if (di->i_flags & cpu_to_le32(OCFS2_DIO_ORPHANED_FL))
+ ret = ocfs2_del_inode_from_orphan(osb, inode, di_bh, 0, 0);
+ if (ret < 0) {
+ mlog_errno(ret); /* leave the entry to the recovery */
+ WRITE_ONCE(oi->ip_dio_orphan_pid, 0);
+ return;
+ }

Regardless of the "ret" value, the code clears oi->ip_dio_orphan_pid.
I suggest removing the above WRITE_ONCE() and return.

+
+ /*
+ * Only drop the marker once the entry is gone. Keeping it while the
+ * entry survives lets the next extending write recover it, see
+ * ocfs2_add_inode_to_orphan().
+ */

This comment also needs updating if we drop above codes.

Thanks,
Heming

+ WRITE_ONCE(oi->ip_dio_orphan_pid, 0);
+}
+
+/*
+ * Does nothing when this task is not the one which added the entry.
+ * The rw_lock has to be held. @end is the end of the range this write
+ * meant to write, 0 when it added no orphan entry.
+ */
+void ocfs2_dio_clear_orphan(struct inode *inode, loff_t end)
... ...