mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Heming Zhao <heming.zhao@suse.com>
To: Joseph Qi <joseph.qi@linux.alibaba.com>
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,
	hch@lst.de
Subject: Re: [RFC PATCH v15 07/10] ocfs2: switch dio write path from buffer_head to iomap
Date: Thu, 3 Dec 2026 15:49:18 +0800	[thread overview]
Message-ID: <d076a20b-b51f-4888-b809-614ed66f233a@suse.com> (raw)
In-Reply-To: <20261202143507.1754776-8-joseph.qi@linux.alibaba.com>

On 12/2/26 22:35, Joseph Qi wrote:
> From: Heming Zhao <heming.zhao@suse.com>
> 
> Replace ocfs2_dio_wr_get_block() with ocfs2_dio_wr_map_blocks(), which
> hands iomap a mapping of as many clusters as can be described by a single
> struct iomap: up to 16MB, stopped at the first cluster that is not of the
> same kind as the first one.  ocfs2_iomap_begin() serves writes through it,
> and ocfs2_dio_end_io() converts the unwritten extents the write recorded
> and updates the inode size.
> 
> In the buffer_head path, ocfs2_dio_wr_get_block() stores dwc in
> bh->b_private, which lets it detect the first call for an I/O chunk and
> invoke ocfs2_add_inode_to_orphan() to protect the extent allocation.
> iomap provides no similar private field, so ocfs2_add_inode_to_orphan()
> moves to ocfs2_file_write_iter().
> 
> It is also impossible to pass the dwc to iomap_dio_ops.end_io: the
> callback cannot distinguish between I/Os initiated by different threads
> concurrently, and without that context it could delete an orphan entry
> created by another thread.  The orphan removal therefore moves to
> ocfs2_file_write_iter() as well, where inode_lock() serializes the writers
> and IOMAP_DIO_FORCE_WAIT makes an extending write complete before the
> function returns, so i_size is already updated when the entry is removed.
> 
> iomap calls ->iomap_begin() once per mapping but ->end_io() once per I/O,
> and the latter only gets the kiocb, so the kiocb is the identity of the
> I/O: ocfs2_file_write_iter() passes it in iomap_iter.private,
> ocfs2_iomap_next() forwards it to ocfs2_dio_wr_map_blocks(), and each
> unwritten record stores it in ue_owner.  ocfs2_dio_collect_unwritten()
> finds the records of one I/O by that owner.
> 
> A direct write waits for unwritten records belonging to another dio before
> it takes the re-write fast path, not after.  A completion which fails part
> way through its conversion has already made some clusters written and
> dropped ip_alloc_sem and the inode lock, but it still owns the records and
> is about to write the zeroed head/tail buffers over those clusters.  The
> fast path would see them as MAPPED and hand them out, and the new data
> would be overwritten by those zeroes.  The records are only removed after
> that cleanup, so waiting first excludes the whole window.  Under
> IOMAP_NOWAIT the wait becomes -EAGAIN.
> 
> Unlike ocfs2_dio_wr_get_block(), the zeroing of the gap between i_size and
> the write is done once per dio by ocfs2_dio_zero_eof() before the io is
> issued.  Zeroing it per mapping would zero blocks that earlier mappings of
> this same dio have already written, because i_size is only updated at
> completion; that made xfstests generic/538 fail.
> 
> On failure only the first and the last cluster of the touched range are
> cleaned up, through ocfs2_dio_cleanup_clusters(): those are the only
> clusters ocfs2_grab_folios_direct() fills folio windows for, and the
> clusters in between can hold dirty folios of other writes sharing the
> cluster.  ocfs2_write_range_buffers() writes the buffers directly when some
> clusters may already have been converted, because generic writeback
> refuses to submit folios beyond i_size.
> 
> ocfs2_dio_clear_orphan_locked() only removes the orphan entry.  Clusters
> beyond i_size are left alone: there is no on-disk way to tell what this
> write allocated from unwritten extents preallocated with
> fallocate(FALLOC_FL_KEEP_SIZE), so truncating to i_size would free the
> preallocated space as well.  They stay until a later truncate or recovery.
> The in-memory marker is only dropped once the entry is really gone, so a
> failed write leaves it for the next extending write to recover, which
> ocfs2_add_inode_to_orphan() already does before adding its own.
> 
> The pid marker is a strict ownership token: every path that leaves the
> entry behind zeroes ip_dio_orphan_pid, so a later write by the same task
> cannot match it and remove the entry without the truncate recovery.
> ocfs2_dio_clear_orphan() only removes the entry once i_size covers the
> range the write meant to write, or nothing is allocated beyond i_size;
> otherwise it leaves the entry for recovery and gives up ownership.
> 
> Writes that span EOF may trigger an i_size update at completion, and
> within i_size there can still be uncommitted metadata such as the CoW done
> by ocfs2_prepare_inode_for_write().  Both set IOMAP_F_DIRTY so iomap cannot
> take the FUA-only fast path, which would clear IOMAP_DIO_NEED_SYNC and skip
> generic_write_sync(), leaving that metadata uncommitted for O_DSYNC.
> 
> With the write path on iomap, ocfs2 no longer needs LEGACY_DIRECT_IO.
> 
> A buffered write needs the rw lock EX, so every path that falls back to
> generic_perform_write() upgrades first: the -ENOTBLK / zero-length bounce
> inside the direct branch as well as the demoted direct branch.  Under
> coherency=buffered a PR lock would let another node write the same range
> directly while this node does a read-modify-write in the page cache.
> ocfs2_dio_upgrade_rw_lock() drops PR and takes EX rather than asking for
> one upgrade, which can deadlock in the cluster lock manager when two nodes
> upgrade at once.
> 
> ip_dio_orphan_pid is reset in ocfs2_alloc_inode(), not only in the slab
> constructor.  The slab object is reused for a new inode, and a stale pid
> that happens to be reused by another task would let an unrelated write
> match the marker and delete a crash-left orphan entry without the truncate
> recovery, leaking the clusters beyond i_size.
> 
> Clearing the orphan entry takes the inode cluster lock, which blocks, so
> IOCB_NOWAIT does not go there.  The marker stays for a later write or for
> the recovery.
> 
> ocfs2_free_write_ctxt() runs the cached deallocs after unlocking the
> folios, like ocfs2_write_end_nolock() does.  Without it the error paths
> that free a write context lose the extents it had cached for deletion.
> 
> A write that hits an error does not convert its unwritten extents.
> iomap keeps dio->size counting bios that failed at completion, and a
> short write can leave the tail of the last cluster unzeroed, so
> converting [offset, offset + size) would mark uninitialized disk blocks
> written and expose stale data.  Leave the extents unwritten, drop the
> zeroed folios and return the error, as ext4 and xfs do; the extents read
> back as zeroes until a later write converts them.
> 
> Co-developed-by: Joseph Qi <joseph.qi@linux.alibaba.com>
> Signed-off-by: Joseph Qi <joseph.qi@linux.alibaba.com>
> Signed-off-by: Heming Zhao <heming.zhao@suse.com>
> ---
>   fs/ocfs2/Kconfig |    1 -
>   fs/ocfs2/aops.c  | 1062 +++++++++++++++++++++++++++++++++-------------
>   fs/ocfs2/aops.h  |   33 +-
>   fs/ocfs2/file.c  |  275 ++++++++++--
>   fs/ocfs2/inode.h |    8 +-
>   fs/ocfs2/super.c |    2 +
>   6 files changed, 1032 insertions(+), 349 deletions(-)
> 
> diff --git a/fs/ocfs2/Kconfig b/fs/ocfs2/Kconfig
> index bf1678a5eb01e..74bc2008ef0bf 100644
> --- a/fs/ocfs2/Kconfig
> +++ b/fs/ocfs2/Kconfig
> @@ -9,7 +9,6 @@ config OCFS2_FS
>   	select QUOTA_TREE
>   	select FS_IOMAP
>   	select FS_POSIX_ACL
> -	select LEGACY_DIRECT_IO
>   	help
>   	  OCFS2 is a general purpose extent based shared disk cluster file
>   	  system with many similarities to ext3. It supports 64 bit inode
> diff --git a/fs/ocfs2/aops.c b/fs/ocfs2/aops.c
> index 4b76be5909689..8d903c55749a9 100644
> --- a/fs/ocfs2/aops.c
> +++ b/fs/ocfs2/aops.c
> @@ -951,6 +951,8 @@ static void ocfs2_free_write_ctxt(struct inode *inode,
>   ... ...
> +
> +/*
> + * Undo the orphan dir entry an extending direct write has added.
> + *
> + * Clusters beyond i_size are left alone.  There is no on-disk way to tell
> + * what this write allocated from unwritten extents preallocated with
> + * fallocate(FALLOC_FL_KEEP_SIZE), so truncating to i_size would free the
> + * preallocated space as well.  They stay until a later truncate or recovery.
> + *
> + * TODO: ocfs2_recover_orphans() still truncates to i_size, so dead-slot
> + * recovery can still free FALLOC_FL_KEEP_SIZE extents.  That predates the
> + * iomap conversion.
> + */
> +static void ocfs2_dio_clear_orphan_locked(struct inode *inode,
> +				   struct buffer_head *di_bh)
> +{
> +	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
> +	struct ocfs2_inode_info *oi = OCFS2_I(inode);
> +	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
> +	int ret = 0;
> +
> +	if (di->i_flags & cpu_to_le32(OCFS2_DIO_ORPHANED_FL))
> +		ret = ocfs2_del_inode_from_orphan(osb, inode, di_bh, 0, 0);
> +	if (ret < 0) {
> +		mlog_errno(ret); /* leave the entry to the recovery */
> +		WRITE_ONCE(oi->ip_dio_orphan_pid, 0);
> +		return;
> +	}

Regardless of the "ret" value, the code clears oi->ip_dio_orphan_pid.
I suggest removing the above WRITE_ONCE() and return.

> +
> +	/*
> +	 * Only drop the marker once the entry is gone.  Keeping it while the
> +	 * entry survives lets the next extending write recover it, see
> +	 * ocfs2_add_inode_to_orphan().
> +	 */

This comment also needs updating if we drop above codes.

Thanks,
Heming

> +	WRITE_ONCE(oi->ip_dio_orphan_pid, 0);
> +}
> +
> +/*
> + * Does nothing when this task is not the one which added the entry.
> + * The rw_lock has to be held.  @end is the end of the range this write
> + * meant to write, 0 when it added no orphan entry.
> + */
> +void ocfs2_dio_clear_orphan(struct inode *inode, loff_t end)
> ... ...


  parent reply	other threads:[~2026-12-03  7:49 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02 14:34 [RFC PATCH v15 00/10] migrate ocfs2 DIO " Joseph Qi
2026-12-02 14:34 ` [RFC PATCH v15 01/10] ocfs2: Add new ocfs2_map_blocks() to introduce iomap feature Joseph Qi
2026-12-02 14:34 ` [RFC PATCH v15 02/10] ocfs2: switch DIO read path from buffer_head to iomap Joseph Qi
2026-12-02 14:35 ` [RFC PATCH v15 03/10] ocfs2: add ocfs2_get_clusters_uncached() Joseph Qi
2026-12-02 16:16   ` Christoph Hellwig
2026-12-02 14:35 ` [RFC PATCH v15 04/10] ocfs2: refactor ocfs2_lock_allocators() and add a dio variant Joseph Qi
2026-12-02 14:35 ` [RFC PATCH v15 05/10] ocfs2: record the fsync transaction when CoW makes clusters writable Joseph Qi
2026-12-02 14:35 ` [RFC PATCH v15 06/10] ocfs2: rework the direct write context for multi-cluster mappings Joseph Qi
2026-12-02 14:35 ` [RFC PATCH v15 07/10] ocfs2: switch dio write path from buffer_head to iomap Joseph Qi
     [not found]   ` <sashiko-outbox-246055@kernel.org>
2026-12-03  5:43     ` Joseph Qi
2026-12-03  7:49   ` Heming Zhao [this message]
2026-12-03  8:55     ` Joseph Qi
2026-12-02 14:35 ` [RFC PATCH v15 08/10] ocfs2: keep sub-page overwrites of written blocks on direct I/O Joseph Qi
2026-12-02 14:35 ` [RFC PATCH v15 09/10] ocfs2: write the dio zeroed head/tail buffers out before completion Joseph Qi
2026-12-02 14:35 ` [RFC PATCH v15 10/10] ocfs2: make buffered writes wait for in-flight direct writes Joseph Qi
2026-12-02 16:17 ` [RFC PATCH v15 00/10] migrate ocfs2 DIO from buffer_head to iomap Christoph Hellwig

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=d076a20b-b51f-4888-b809-614ed66f233a@suse.com \
    --to=heming.zhao@suse.com \
    --cc=akpm@linux-foundation.org \
    --cc=hch@lst.de \
    --cc=jlbec@evilplan.org \
    --cc=joseph.qi@linux.alibaba.com \
    --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®