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 09/10] ocfs2: write the dio zeroed head/tail buffers out before completion
Date: Mon, 30 Nov 2026 18:02:02 +0800	[thread overview]
Message-ID: <20261130100203.383093-10-joseph.qi@linux.alibaba.com> (raw)
In-Reply-To: <20261130100203.383093-1-joseph.qi@linux.alibaba.com>

Preparing a direct write of a new or unwritten cluster zeroes the head and
tail folios outside the user range and leaves them dirty in the page cache,
outside the range iomap invalidates.  They can be wholly beyond i_size
until ocfs2_dio_end_io_write() updates it at completion.

Writeback will not submit such buffers.  ocfs2_writepages() uses
mpage_writepages(), which for a folio at or past i_size falls through to
block_write_full_folio(), and __block_write_full_folio() does:

	if (block > last_block) {
		clear_buffer_dirty(bh);
		set_buffer_uptodate(bh);
	}

So if writeback runs before the conversion, the zeroes are discarded
without ever reaching the disk, the folio is left clean, and the cluster
exposes stale blocks once ocfs2_dio_end_io_write() marks it written.  A
short write leaves the zeroed tail folios of the unconverted trailing
clusters dirty in the same way.

ocfs2_dio_cleanup_pages(..., true) cannot fix this: it only runs when the
conversion fails, and even then the buffers may already have been cleaned
or the folio reclaimed, so ocfs2_write_range_buffers() finds nothing to
write.  The zeroes have to reach the disk while the clusters are still
ours and the folios are still locked.

Write them in ocfs2_write_end_nolock() for a direct write, right after
block_commit_write() has marked them dirty.  Submit every buffer first and
wait in a second pass, so a cluster spanning hundreds of folios does not
cost one round trip per folio.  submit_bh()/wait_on_buffer() bypass the
i_size check, which is the point.

The folio lock keeps the buffers alive and excludes any other io on them,
so no extra buffer reference is taken and a write error seen in the wait
pass can only come from the submit pass.  ip_alloc_sem, the inode cluster
lock and an open journal handle are held; the data io needs no journal
progress, so there is no cycle with a commit that is blocked on our handle
and waiting for the folio lock.  IOMAP_NOWAIT never reaches here,
ocfs2_dio_wr_map_blocks() returns -EAGAIN before the allocation path.

This makes the zeroing io synchronous in the mapping path instead of
deferring it to background writeback.  That io was always required; iomap
issues its own block-size padding the same way through iomap_dio_zero().

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

diff --git a/fs/ocfs2/aops.c b/fs/ocfs2/aops.c
index 6e5d5a4f976f7..831be79a9a377 100644
--- a/fs/ocfs2/aops.c
+++ b/fs/ocfs2/aops.c
@@ -2635,6 +2635,81 @@ static void ocfs2_write_end_inline(struct inode *inode, loff_t pos,
 	     le16_to_cpu(di->i_dyn_features));
 }
 
+/*
+ * The head/tail folios a direct write zeroed are outside the range iomap
+ * invalidates, and they can be wholly beyond i_size until ->end_io() updates
+ * it.  Writeback will not submit such buffers: __block_write_full_folio()
+ * clears their dirty bit without writing them, so the zeroes would never
+ * reach the disk and the cluster would expose stale blocks once
+ * ocfs2_dio_end_io_write() converts it.  Flushing at completion is too late,
+ * by then the buffers may already have been cleaned or the folio reclaimed.
+ * Write them here, while the folios are locked and the clusters are ours.
+ *
+ * The folio lock keeps the buffers alive and excludes any other io on them,
+ * so no extra buffer reference is needed and a write error seen in the second
+ * pass can only come from the first.
+ */
+static int ocfs2_dio_write_zero_folios(struct ocfs2_write_ctxt *wc)
+{
+	int i, ret = 0;
+
+	/* Submit them all before waiting, a cluster can span many folios. */
+	for (i = 0; i < wc->w_num_folios; i++) {
+		struct folio *folio = wc->w_folios[i];
+		struct buffer_head *bh, *head;
+
+		if (!folio)
+			continue;
+		head = folio_buffers(folio);
+		if (!head)
+			continue;
+
+		bh = head;
+		do {
+			lock_buffer(bh);
+			if (buffer_write_io_error(bh) && !ret)
+				ret = -EIO;
+			clear_buffer_write_io_error(bh);
+			if (!buffer_dirty(bh)) {
+				unlock_buffer(bh);
+				goto next;
+			}
+			if (!buffer_mapped(bh) || !buffer_uptodate(bh) ||
+			    buffer_delay(bh) || buffer_unwritten(bh)) {
+				unlock_buffer(bh);
+				if (!ret)
+					ret = -EIO;
+				goto next;
+			}
+			clear_buffer_dirty(bh);
+			bh_submit(bh, REQ_OP_WRITE | REQ_SYNC, bh_end_write);
+next:
+			bh = bh->b_this_page;
+		} while (bh != head);
+	}
+
+	for (i = 0; i < wc->w_num_folios; i++) {
+		struct folio *folio = wc->w_folios[i];
+		struct buffer_head *bh, *head;
+
+		if (!folio)
+			continue;
+		head = folio_buffers(folio);
+		if (!head)
+			continue;
+
+		bh = head;
+		do {
+			wait_on_buffer(bh);
+			if (buffer_write_io_error(bh) && !ret)
+				ret = -EIO;
+			bh = bh->b_this_page;
+		} while (bh != head);
+	}
+
+	return ret;
+}
+
 int ocfs2_write_end_nolock(struct address_space *mapping, loff_t pos,
 		unsigned len, unsigned copied, void *fsdata)
 {
@@ -2723,6 +2798,14 @@ int ocfs2_write_end_nolock(struct address_space *mapping, loff_t pos,
 		}
 	}
 
+	if (wc->w_type == OCFS2_WRITE_DIRECT) {
+		ret = ocfs2_dio_write_zero_folios(wc);
+		if (ret) {
+			copied = ret;
+			goto out;
+		}
+	}
+
 out_write_size:
 	/* Direct io do not update i_size here. */
 	if (wc->w_type != OCFS2_WRITE_DIRECT) {
-- 
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 ` Joseph Qi [this message]
2026-11-30 10:02 ` [RFC PATCH v13 10/10] ocfs2: make buffered writes wait for in-flight direct writes Joseph Qi

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-10-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®