From: Andrew Morton <akpm@linux-foundation.org>
To: mm-commits@vger.kernel.org,mark@fasheh.com,jlbec@evilplan.org,
heming.zhao@suse.com,hch@lst.de,joseph.qi@linux.alibaba.com,
akpm@linux-foundation.org
Subject: + ocfs2-write-the-dio-zeroed-head-tail-buffers-out-before-completion.patch added to mm-nonmm-unstable branch
Date: Sat, 05 Dec 2026 09:20:25 -0800 [thread overview]
Message-ID: <20261205172025.772BF1F0092B@smtp.kernel.org> (raw)
In-Reply-To: <20261205143959.704982-10-joseph.qi@linux.alibaba.com>
The patch titled
Subject: ocfs2: write the dio zeroed head/tail buffers out before completion
has been added to the -mm mm-nonmm-unstable branch. Its filename is
ocfs2-write-the-dio-zeroed-head-tail-buffers-out-before-completion.patch
This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/ocfs2-write-the-dio-zeroed-head-tail-buffers-out-before-completion.patch
This patch will later appear in the mm-nonmm-unstable branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a
reply-to-all to that, adding suitable additional cc's
*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***
The -mm tree is included into linux-next via various
branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days
------------------------------------------------------
From: Joseph Qi <joseph.qi@linux.alibaba.com>
Subject: ocfs2: write the dio zeroed head/tail buffers out before completion
Date: Sat, 05 Dec 2026 22:39:58 +0800
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. bh_submit()/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().
Link: https://patch.msgid.link/20261205143959.704982-10-joseph.qi@linux.alibaba.com
Signed-off-by: Joseph Qi <joseph.qi@linux.alibaba.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Cc: Heming Zhao <heming.zhao@suse.com>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Mark Fasheh <mark@fasheh.com>
Cc: Joel Becker <jlbec@evilplan.org>
fs/ocfs2/aops.c | 110 ++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 110 insertions(+)
--- a/fs/ocfs2/aops.c~ocfs2-write-the-dio-zeroed-head-tail-buffers-out-before-completion
+++ a/fs/ocfs2/aops.c
@@ -2694,6 +2694,103 @@ static void ocfs2_write_end_inline(struc
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;
+
+ /*
+ * Tag the folio for writeback before submitting its buffers,
+ * as __block_write_full_folio() does. clear_dirty_for_io()
+ * drops PG_dirty and write-protects any mmap so a concurrent
+ * write re-dirties it; start_writeback() then clears the
+ * PAGECACHE_TAG_DIRTY that block_commit_write() left and sets
+ * PAGECACHE_TAG_WRITEBACK. Without the pairing the dirty tag
+ * survives, so a RWF_NOWAIT direct read keeps seeing the folio
+ * as needing writeback and writeback never clears it. Wait out
+ * any earlier writeback first: start_writeback() BUGs on a
+ * folio already under writeback.
+ */
+ folio_wait_writeback(folio);
+ folio_clear_dirty_for_io(folio);
+ folio_start_writeback(folio);
+
+ bh = head;
+ do {
+ lock_buffer(bh);
+ /*
+ * Clear a stale error bit from an earlier writeback; no
+ * write of this operation has completed yet, so only the
+ * wait pass below may latch an error into ret.
+ */
+ 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);
+
+ /* Every buffer is on disk; end the writeback started above. */
+ folio_end_writeback(folio);
+ }
+
+ return ret;
+}
+
int ocfs2_write_end_nolock(struct address_space *mapping, loff_t pos,
unsigned len, unsigned copied, void *fsdata)
{
@@ -2798,6 +2895,19 @@ int ocfs2_write_end_nolock(struct addres
}
}
+ if (wc->w_type == OCFS2_WRITE_DIRECT) {
+ ret = ocfs2_dio_write_zero_folios(wc);
+ if (ret) {
+ copied = ret;
+ /*
+ * On failure out_write_size still runs: the allocation
+ * is committed below regardless, so refresh i_blocks and
+ * record the fsync transaction; a first-mapping failure
+ * has no end_io to do it later.
+ */
+ }
+ }
+
out_write_size:
/* Direct io do not update i_size here. */
if (wc->w_type != OCFS2_WRITE_DIRECT) {
_
Patches currently in -mm which might be from joseph.qi@linux.alibaba.com are
ocfs2-exempt-orphaned-dinodes-from-the-zero-nlink-check.patch
ocfs2-abstract-the-zero-nlink-or-mode-dinode-check.patch
ocfs2-abstract-the-dinode-i_blkno-mismatch-check.patch
ocfs2-abstract-the-dinode-ocfs2_valid_fl-check.patch
ocfs2-abstract-the-dinode-fs_generation-mismatch-check.patch
ocfs2-abstract-the-dinode-suballoc-slot-checks.patch
ocfs2-abstract-the-dinode-suballoc-bit-check.patch
ocfs2-abstract-the-dinode-orphaned-slot-check.patch
ocfs2-abstract-the-dinode-dio-orphaned-slot-check.patch
ocfs2-abstract-the-inline-data-dinode-guard-and-cluster-check.patch
ocfs2-abstract-the-inline-data-id_count-bound-check.patch
ocfs2-abstract-the-inline-data-i_size-bound-check.patch
ocfs2-abstract-the-fast-symlink-dinode-guard-and-size-check.patch
ocfs2-abstract-the-fast-symlink-nul-termination-check.patch
ocfs2-abstract-the-chain-list-dinode-guard-and-count-check.patch
ocfs2-abstract-the-chain-list-next-free-record-check.patch
ocfs2-abstract-the-chain-list-bits-per-cluster-check.patch
ocfs2-abstract-the-dinode-refcount-location-check.patch
ocfs2-abstract-the-zero-extent-list-count-check.patch
ocfs2-abstract-the-oversized-extent-list-count-check.patch
ocfs2-abstract-the-extent-list-next-free-record-check.patch
ocfs2-report-unrepairable-corruption-from-online-filecheck-fix.patch
ocfs2-check-dinode-nlink-and-mode-in-online-filecheck.patch
ocfs2-check-dinode-suballoc-slot-in-online-filecheck.patch
ocfs2-check-dinode-suballoc-bit-in-online-filecheck.patch
ocfs2-check-dinode-orphaned-slots-in-online-filecheck.patch
ocfs2-check-inline-data-dinodes-in-online-filecheck.patch
ocfs2-check-fast-symlink-dinodes-in-online-filecheck.patch
ocfs2-check-chain-list-dinodes-in-online-filecheck.patch
ocfs2-check-dinode-refcount-location-in-online-filecheck.patch
ocfs2-check-extent-list-dinodes-in-online-filecheck.patch
ocfs2-dont-bug_on-when-a-group-extend-leaves-too-few-free-bits.patch
ocfs2-validate-chain-allocator-clusters-per-group.patch
ocfs2-check-chain-allocator-clusters-per-group-in-online-filecheck.patch
ocfs2-keep-sub-page-overwrites-of-written-blocks-on-direct-i-o.patch
ocfs2-write-the-dio-zeroed-head-tail-buffers-out-before-completion.patch
ocfs2-make-buffered-writes-wait-for-in-flight-direct-writes.patch
next prev parent reply other threads:[~2026-12-05 17:20 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-05 14:39 [PATCH v18 00/10] migrate ocfs2 DIO from buffer_head to iomap Joseph Qi
2026-12-05 14:39 ` [PATCH v18 01/10] ocfs2: Add new ocfs2_map_blocks() to introduce iomap feature Joseph Qi
2026-12-05 17:19 ` + ocfs2-add-new-ocfs2_map_blocks-to-introduce-iomap-feature.patch added to mm-nonmm-unstable branch Andrew Morton
2026-12-05 14:39 ` [PATCH v18 02/10] ocfs2: switch DIO read path from buffer_head to iomap Joseph Qi
2026-12-05 17:19 ` + ocfs2-switch-dio-read-path-from-buffer_head-to-iomap.patch added to mm-nonmm-unstable branch Andrew Morton
2026-12-05 14:39 ` [PATCH v18 03/10] ocfs2: add ocfs2_get_clusters_uncached() Joseph Qi
2026-12-05 17:20 ` + ocfs2-add-ocfs2_get_clusters_uncached.patch added to mm-nonmm-unstable branch Andrew Morton
2026-12-05 14:39 ` [PATCH v18 04/10] ocfs2: refactor ocfs2_lock_allocators() and add a dio variant Joseph Qi
2026-12-05 17:20 ` + ocfs2-refactor-ocfs2_lock_allocators-and-add-a-dio-variant.patch added to mm-nonmm-unstable branch Andrew Morton
2026-12-05 14:39 ` [PATCH v18 05/10] ocfs2: record the fsync transaction when CoW makes clusters writable Joseph Qi
2026-12-05 17:20 ` + ocfs2-record-the-fsync-transaction-when-cow-makes-clusters-writable.patch added to mm-nonmm-unstable branch Andrew Morton
2026-12-05 14:39 ` [PATCH v18 06/10] ocfs2: rework the direct write context for multi-cluster mappings Joseph Qi
2026-12-05 17:20 ` + ocfs2-rework-the-direct-write-context-for-multi-cluster-mappings.patch added to mm-nonmm-unstable branch Andrew Morton
2026-12-05 14:39 ` [PATCH v18 07/10] ocfs2: switch dio write path from buffer_head to iomap Joseph Qi
2026-12-05 17:20 ` + ocfs2-switch-dio-write-path-from-buffer_head-to-iomap.patch added to mm-nonmm-unstable branch Andrew Morton
2026-12-05 14:39 ` [PATCH v18 08/10] ocfs2: keep sub-page overwrites of written blocks on direct I/O Joseph Qi
2026-12-05 17:20 ` + ocfs2-keep-sub-page-overwrites-of-written-blocks-on-direct-i-o.patch added to mm-nonmm-unstable branch Andrew Morton
2026-12-05 14:39 ` [PATCH v18 09/10] ocfs2: write the dio zeroed head/tail buffers out before completion Joseph Qi
2026-12-05 17:20 ` Andrew Morton [this message]
2026-12-05 14:39 ` [PATCH v18 10/10] ocfs2: make buffered writes wait for in-flight direct writes Joseph Qi
2026-12-05 17:20 ` + ocfs2-make-buffered-writes-wait-for-in-flight-direct-writes.patch added to mm-nonmm-unstable branch Andrew Morton
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=20261205172025.772BF1F0092B@smtp.kernel.org \
--to=akpm@linux-foundation.org \
--cc=hch@lst.de \
--cc=heming.zhao@suse.com \
--cc=jlbec@evilplan.org \
--cc=joseph.qi@linux.alibaba.com \
--cc=mark@fasheh.com \
--cc=mm-commits@vger.kernel.org \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.