[RFC PATCH v13 10/10] ocfs2: make buffered writes wait for in-flight direct writes

From: Joseph Qi

Date: Mon Nov 30 2026 - 05:03:23 EST


ocfs2_unwritten_check() lets a buffered write skip zeroing and unwritten
conversion for a cluster that an in-flight direct write has put on
ip_unwritten_list, on the assumption that the dio will convert it. The
dio only converts the range it actually completed, and converts nothing
at all when it fails, so a dio which ends short or errors leaves the
cluster unwritten while the buffered data sits in a dirty folio. Once
that folio is written back and evicted the data reads back as zeroes.

Wait for foreign entries on ip_unwritten_wq before taking any lock in
ocfs2_write_begin(), the same way ocfs2_dio_wr_map_blocks() already does.
The wait must be outside ip_alloc_sem and the inode cluster lock because
ocfs2_dio_end_io_write() takes both to convert. Every caller holds
i_rwsem and entries are only added under it, so the set can only shrink
and no retry loop is needed. The folio is not locked yet at this point,
so there is no ordering conflict with ocfs2_grab_folios_direct().

page_mkwrite is deliberately left alone. It holds no i_rwsem, so entries
can appear between the wait and taking ip_alloc_sem, and it is reachable
through get_user_pages() from bio_iov_iter_get_pages() when the dio
source buffer is an mmap of the same file. Waiting there with no owner
would match the dio's own entries, which are only removed at end_io, and
deadlock against itself. The mmap window therefore remains open and
needs a way to exclude the current task's own in-flight dio first.

Signed-off-by: Joseph Qi <joseph.qi@xxxxxxxxxxxxxxxxx>
---
fs/ocfs2/aops.c | 30 ++++++++++++++++++++++++++++++
1 file changed, 30 insertions(+)

diff --git a/fs/ocfs2/aops.c b/fs/ocfs2/aops.c
index 831be79a9a377..5fbaf83b8daaa 100644
--- a/fs/ocfs2/aops.c
+++ b/fs/ocfs2/aops.c
@@ -2569,6 +2569,32 @@ int ocfs2_write_begin_nolock(struct address_space *mapping,
return ret;
}

+/*
+ * Wait for direct writes still in flight on [pos, pos + len) to finish.
+ * A buffered write must not defer its zeroing to a dio which may never
+ * convert the cluster: if that dio ends short or fails, the cluster stays
+ * unwritten on disk and the buffered data reads back as zeroes.
+ *
+ * Must be called without ip_alloc_sem or the inode cluster lock held,
+ * because ocfs2_dio_end_io_write() takes both to convert. i_rwsem is held
+ * by every caller and all entries are added under it, so the set can only
+ * shrink while we wait and no retry is needed.
+ */
+static int ocfs2_wait_foreign_unwritten(struct inode *inode, loff_t pos,
+ unsigned int len)
+{
+ struct ocfs2_inode_info *oi = OCFS2_I(inode);
+ struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
+ u32 cstart = pos >> osb->s_clustersize_bits;
+ u32 cend = ocfs2_clusters_for_bytes(inode->i_sb, pos + len);
+
+ if (!ocfs2_dio_foreign_unwritten(oi, cstart, cend, NULL))
+ return 0;
+
+ return wait_event_killable(oi->ip_unwritten_wq,
+ !ocfs2_dio_foreign_unwritten(oi, cstart, cend, NULL));
+}
+
static int ocfs2_write_begin(const struct kiocb *iocb,
struct address_space *mapping,
loff_t pos, unsigned len,
@@ -2578,6 +2604,10 @@ static int ocfs2_write_begin(const struct kiocb *iocb,
struct buffer_head *di_bh = NULL;
struct inode *inode = mapping->host;

+ ret = ocfs2_wait_foreign_unwritten(inode, pos, len);
+ if (ret)
+ return ret;
+
ret = ocfs2_inode_lock(inode, &di_bh, 1);
if (ret) {
mlog_errno(ret);
--
2.39.3