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 00/10] migrate ocfs2 DIO from buffer_head to iomap
Date: Mon, 30 Nov 2026 18:01:53 +0800	[thread overview]
Message-ID: <20261130100203.383093-1-joseph.qi@linux.alibaba.com> (raw)

This patch set migrates ocfs2 Direct I/O (DIO) from the legacy buffer_head
path to iomap, so ocfs2 stops being a user of __blockdev_direct_IO() and
LEGACY_DIRECT_IO can be dropped.

Reads are served by ocfs2_iomap_begin() over ocfs2_map_blocks().  Writes
get a mapping of as many clusters as one struct iomap can describe,
allocate their whole contiguous range with a single ocfs2_add_inode_data()
call, and convert the unwritten extents at completion.  The kiocb is the
identity of the I/O across ->iomap_begin() and ->end_io(), so the unwritten
records and the append-DIO orphan entry belong to a specific write instead
of to a buffer_head.

It is based on commit a9c48428cbd8 (next-20261126).

v13 changelog:

The write conversion is split so it can be reviewed in pieces.  v12.1's
three patches become ten: 1-2 are unchanged, 3-5 are preparation split out
of the write patch, 6 reworks the write context while still on buffer_head,
7 is the iomap switch, and 8-10 are three design changes kept separate.

patch 1/10, 2/10: no change

patch 3/10, 4/10, 5/10: split out of v12.1's patch 3/3, no functional
change.  ocfs2_get_clusters_uncached(), the ocfs2_lock_allocators()
refactor, and recording the fsync transaction when CoW makes clusters
writable.

patch 6/10: new.  Rework the write context, the folio preparation and the
unwritten extent tracking for multi-cluster mappings.  Unwritten extents
become a range with an owner instead of one entry per cluster carrying a
physical address; the physical cluster is looked up at conversion time
with ocfs2_get_clusters_uncached().  The buffer_head path is ported onto
this and still maps one cluster per call, so behaviour is unchanged.

The buffer_head path keeps its own concurrency behaviour here.  The
unwritten check still just defers to whichever write recorded the cluster,
without distinguishing an owner, because the wait for foreign records
arrives with the iomap mapping in patch 7.  And the orphan entry
get_block() adds is removed in ocfs2_dio_bh_end_io(), as the old
ocfs2_dio_end_io_write() did.  Both halves have to be in the same patch:
leaving the entry set makes the next extending write recover it by
truncating to i_size, which frees clusters another write has just
allocated, and warning about a foreign record that nothing waits for
fires under concurrent DIO.

patch 7/10: new.  Switch the write path to iomap.
ocfs2_dio_wr_map_blocks() replaces ocfs2_dio_wr_get_block() and hands out
a mapping of as many clusters as one struct iomap can describe, up to
16MB, stopped at the first cluster of a different kind.  Completion
converts the recorded ranges and updates i_size.  The orphan add and
remove and the EOF gap zeroing move to ocfs2_file_write_iter(), where
inode_lock() serializes the writers and IOMAP_DIO_FORCE_WAIT makes an
extending write complete before the function returns.  The kiocb passed in
iomap_iter.private is the identity of the I/O, so the unwritten records
belong to a specific write.  ocfs2 no longer needs LEGACY_DIRECT_IO.

The mapping waits for unwritten records belonging to another dio before it
takes the re-write fast path, and the unwritten check distinguishes its own
records from a foreign one.  Those two arrive together: a completion that
fails part way has already converted clusters and dropped the locks but
still owns the records and is about to write the zeroed head/tail buffers
over them, so the fast path must not hand them out first.

Every path that falls back to generic_perform_write() upgrades the rw lock
to EX, including the -ENOTBLK / zero-length bounce inside the direct
branch.  Under coherency=buffered a PR lock lets another node write the
same range directly while this node does a read-modify-write in the page
cache.  This reinstates ocfs2_dio_upgrade_rw_lock(), which v12.1 removed
over the stale inode state after the lock drop.  The drop is deliberate:
PR is released and EX taken as two requests, because a single upgrade can
deadlock in the cluster lock manager when two nodes upgrade at once, and
no state is validated across the drop - the buffered write re-checks under
the inode cluster lock in ocfs2_write_begin().

A write that errors after completing a prefix converts that prefix and
returns the byte count, as the buffer_head path did, instead of returning
only an errno; the zeroed folios are only dropped when nothing is
converted.  ip_dio_orphan_pid is reset in ocfs2_alloc_inode(), so a reused
slab object plus a reused pid cannot make an unrelated write delete a
crash-left orphan entry without the truncate recovery.  IOCB_NOWAIT skips
ocfs2_dio_clear_orphan(), which takes a blocking cluster lock, and leaves
the marker for a later write.  ocfs2_free_write_ctxt() runs the cached
deallocs after unlocking the folios, so the error paths do not lose
extents queued for deletion.

patch 8/10: new.  Sub-page O_DIRECT overwrites of already-written blocks
stay direct.  v12.1 demoted every non-page-aligned write when
i_blocksize(inode) < PAGE_SIZE, which on 16K and 64K page kernels is every
ocfs2 volume, so all sub-page overwrites took an exclusive rw lock and
failed IOCB_NOWAIT.  Probe the partial blocks at both ends with a no-create
lookup and demote only for a hole, an unwritten extent, or an extending
write.  The probe runs at a second ocfs2_should_use_dio() call site, after
generic_write_checks() has resolved O_APPEND and clamped the length and
ocfs2_prepare_inode_for_write() has refreshed the inode.

patch 9/10: new.  Write the zeroed head/tail buffers out at mapping time.
They can be wholly beyond i_size until completion, and
__block_write_full_folio() clears their dirty bit without submitting them,
so the zeroes never reach the disk and the converted cluster exposes stale
blocks.  Flushing at completion is too late: the buffers may already have
been cleaned or the folio reclaimed.  Submit every buffer first and wait
in a second pass, so a cluster spanning many folios does not cost one round
trip per folio.

patch 10/10: new.  Buffered writes wait for in-flight direct writes before
deferring zeroing and unwritten conversion to them.  A dio that fails or
ends short leaves the cluster unwritten while the buffered data sits in a
dirty folio, and it reads back as zeroes once that folio is evicted.

Two windows are left open deliberately:

 - ocfs2_page_mkwrite() still defers zeroing and unwritten conversion to an
   in-flight dio without waiting for it.  It holds no i_rwsem, 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, so a plain wait there
   would match the dio's own records and deadlock against itself.  Closing
   it needs a way to exclude the current task's own in-flight dio first.
 - a failed extending dio still leaves its orphan entry and the clusters it
   allocated beyond i_size for recovery.  The in-memory marker is kept so
   the next extending write recovers them, but an unlink before that still
   strands the inode until an unclean recovery.

--------------
# test report

Tested on next-20261126, single node, o2cb cluster stack.

## ocfs2 testsuite

single_run-WIP.sh -d /dev/vdb (40G), mkfs -b 4k -C 16k -N 4
--fs-features=usrquota,grpquota

80 PASSED, 2 FAILED, total runtime 7706 seconds.  Identical to the
pre-series baseline (80 PASSED, 2 FAILED, 7707 seconds).  The two failures
are buildkernel data=writeback and data=ordered, both failing in 0 seconds
on a missing buildkernel log file, unrelated to the filesystem.

## xfstests

./check -E ocfs2.exclude <90 generic DIO tests>
mkfs -b 4k -C 16k -N 4 --fs-features=usrquota,grpquota

52 pass, 10 fail, 28 not run.  Per-test verdicts identical to the
pre-conversion baseline.  Failures: generic/091 generic/094 generic/219
generic/263 generic/326 generic/328 generic/521 generic/587 generic/617
generic/733 - all pre-existing.

Also run on a 1M cluster volume, so clustersize > PAGE_SIZE and the
head/tail folio paths actually execute: sub-cluster O_DIRECT zeroing reads
back from disk correctly, and four concurrent O_DIRECT writers over a
shared region complete without corruption.

No WARNING, BUG or hung task in any run.  One lockdep report is seen during
generic/113, a sb_writers -> i_rwsem -> ocfs2 rw lock chain against
aio_write() in io_submit_one().  It does not fail the test and is not yet
attributed; it is reported here rather than left out.

The intermediate commit 6/10 was verified separately, since it is a live
buffer_head path on the reworked write context: the ocfs2 directio_test
basic, append and fill-hole modes each with four concurrent processes, the
1M cluster test above, and a 24 test DIO xfstests subset.  All match the
final revision on the same tests; the only failure is the pre-existing
generic/263.

All of the above was run on this revision.

-------------
patchset history

v1 patch set:
https://lore.kernel.org/ocfs2-devel/20260724052559.15409-1-heming.zhao@suse.com/
v2 patch set:
https://lore.kernel.org/ocfs2-devel/20260727061802.18485-1-heming.zhao@suse.com/
v3 patch set:
https://lore.kernel.org/ocfs2-devel/20261018143319.22329-1-heming.zhao@suse.com/
v4 patch set:
https://lore.kernel.org/ocfs2-devel/20261103135605.22943-1-heming.zhao@suse.com/
v5 patch set:
https://lore.kernel.org/ocfs2-devel/20261105071930.16399-1-heming.zhao@suse.com/
v6 patch set:
https://lore.kernel.org/ocfs2-devel/20261115154013.9379-1-heming.zhao@suse.com/
v7 patch set:
https://lore.kernel.org/ocfs2-devel/20261118002050.9019-1-heming.zhao@suse.com/
v8 patch set:
https://lore.kernel.org/ocfs2-devel/20261119064029.11910-1-heming.zhao@suse.com/
v9 patch set:
https://lore.kernel.org/ocfs2-devel/20261120060014.12087-1-heming.zhao@suse.com/
v10 patch set:
https://lore.kernel.org/ocfs2-devel/20261124045501.10281-1-heming.zhao@suse.com/
v11 patch set:
https://lore.kernel.org/ocfs2-devel/20261124152623.7838-1-heming.zhao@suse.com/
v12 patch set:
https://lore.kernel.org/ocfs2-devel/20261125154626.22335-1-heming.zhao@suse.com/
v12.1 patch set:
https://lore.kernel.org/ocfs2-devel/20261127033313.9169-1-heming.zhao@suse.com/

-------------------
Heming Zhao (7):
  ocfs2: Add new ocfs2_map_blocks() to introduce iomap feature
  ocfs2: switch DIO read path from buffer_head to iomap
  ocfs2: add ocfs2_get_clusters_uncached()
  ocfs2: refactor ocfs2_lock_allocators() and add a dio variant
  ocfs2: record the fsync transaction when CoW makes clusters writable
  ocfs2: rework the direct write context for multi-cluster mappings
  ocfs2: switch dio write path from buffer_head to iomap

Joseph Qi (3):
  ocfs2: keep sub-page overwrites of written blocks on direct I/O
  ocfs2: write the dio zeroed head/tail buffers out before completion
  ocfs2: make buffered writes wait for in-flight direct writes

 fs/ocfs2/Kconfig        |    2 +-
 fs/ocfs2/aops.c         | 2183 ++++++++++++++++++++++++++++++++-------
 fs/ocfs2/aops.h         |   45 +-
 fs/ocfs2/extent_map.c   |   34 +-
 fs/ocfs2/extent_map.h   |    3 +
 fs/ocfs2/file.c         |  348 ++++++-
 fs/ocfs2/inode.h        |    8 +
 fs/ocfs2/mmap.c         |    3 +-
 fs/ocfs2/ocfs2.h        |   25 +-
 fs/ocfs2/refcounttree.c |    2 +
 fs/ocfs2/suballoc.c     |   50 +-
 fs/ocfs2/suballoc.h     |    5 +
 fs/ocfs2/super.c        |    3 +
 13 files changed, 2260 insertions(+), 451 deletions(-)

-- 
2.39.3


             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 Joseph Qi [this message]
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 ` [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-1-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®