mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Joseph Qi <joseph.qi@linux.alibaba.com>
To: Heming Zhao <heming.zhao@suse.com>, hch@lst.de
Cc: Andrew Morton <akpm@linux-foundation.org>,
	Mark Fasheh <mark@fasheh.com>, Joel Becker <jlbec@evilplan.org>,
	ocfs2-devel@lists.linux.dev, linux-kernel@vger.kernel.org,
	Joseph Qi <joseph.qi@linux.alibaba.com>
Subject: [RFC PATCH v13 10/10] ocfs2: make buffered writes wait for in-flight direct writes
Date: Mon, 30 Nov 2026 18:02:03 +0800	[thread overview]
Message-ID: <20261130100203.383093-11-joseph.qi@linux.alibaba.com> (raw)
In-Reply-To: <20261130100203.383093-1-joseph.qi@linux.alibaba.com>

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@linux.alibaba.com>
---
 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


      parent reply	other threads:[~2026-11-30 10:02 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30 10:01 [RFC PATCH v13 00/10] migrate ocfs2 DIO from buffer_head to iomap Joseph Qi
2026-11-30 10:01 ` [RFC PATCH v13 01/10] ocfs2: Add new ocfs2_map_blocks() to introduce iomap feature Joseph Qi
2026-11-30 10:01 ` [RFC PATCH v13 02/10] ocfs2: switch DIO read path from buffer_head to iomap Joseph Qi
2026-11-30 10:01 ` [RFC PATCH v13 03/10] ocfs2: add ocfs2_get_clusters_uncached() Joseph Qi
2026-11-30 10:01 ` [RFC PATCH v13 04/10] ocfs2: refactor ocfs2_lock_allocators() and add a dio variant Joseph Qi
2026-11-30 10:01 ` [RFC PATCH v13 05/10] ocfs2: record the fsync transaction when CoW makes clusters writable Joseph Qi
2026-11-30 10:01 ` [RFC PATCH v13 06/10] ocfs2: rework the direct write context for multi-cluster mappings Joseph Qi
     [not found]   ` <sashiko-outbox-240858@kernel.org>
2026-11-30 12:00     ` Joseph Qi
2026-11-30 10:02 ` [RFC PATCH v13 07/10] ocfs2: switch dio write path from buffer_head to iomap Joseph Qi
2026-11-30 10:02 ` [RFC PATCH v13 08/10] ocfs2: keep sub-page overwrites of written blocks on direct I/O Joseph Qi
2026-11-30 10:02 ` [RFC PATCH v13 09/10] ocfs2: write the dio zeroed head/tail buffers out before completion Joseph Qi
2026-11-30 10:02 ` Joseph Qi [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261130100203.383093-11-joseph.qi@linux.alibaba.com \
    --to=joseph.qi@linux.alibaba.com \
    --cc=akpm@linux-foundation.org \
    --cc=hch@lst.de \
    --cc=heming.zhao@suse.com \
    --cc=jlbec@evilplan.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mark@fasheh.com \
    --cc=ocfs2-devel@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®