All of lore.kernel.org
 help / color / mirror / Atom feed
From: Joseph Qi <joseph.qi@linux.alibaba.com>
To: hch@lst.de, Heming Zhao <heming.zhao@suse.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,
	Joseph Qi <joseph.qi@linux.alibaba.com>
Subject: [RFC PATCH v14 08/10] ocfs2: keep sub-page overwrites of written blocks on direct I/O
Date: Tue,  1 Dec 2026 18:07:22 +0800	[thread overview]
Message-ID: <20261201100724.2909559-9-joseph.qi@linux.alibaba.com> (raw)
In-Reply-To: <20261201100724.2909559-1-joseph.qi@linux.alibaba.com>

ocfs2_should_use_dio() demotes every non-page-aligned write to buffered
I/O when i_blocksize(inode) < PAGE_SIZE.  The zeroing hazard it guards
against only exists when a partial block at either end is a hole or an
unwritten extent, or the write extends the file; a pure in-place
overwrite of already-written blocks needs no zeroing at all.  On 16K or
64K page kernels every ocfs2 file system has blocksize < PAGE_SIZE, so
all sub-page O_DIRECT overwrites were taking an exclusive rw lock,
failing IOCB_NOWAIT and going through page-cache writeback.

Probe the partial blocks at both ends with a no-create lookup and demote
only when one of them is not already mapped and written, or the write
extends i_size.

Do this only at the 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.  At the first
site ki_pos and count are not final, so the range cannot be probed
there; leaving it undemoted is safe because the buffered path upgrades
the rw lock later.

That second call site is added here.  A write demoted after
generic_write_checks() has already been handed a PR rw lock by the first
evaluation, and the buffered write it falls into needs EX, so it goes
through ocfs2_dio_upgrade_rw_lock() like the other fallback paths.

The rw lock is held across the probe, so no other node can punch or
truncate the range underneath it.

Signed-off-by: Joseph Qi <joseph.qi@linux.alibaba.com>
---
 fs/ocfs2/aops.c | 52 +++++++++++++++++++++++++++++++++++++
 fs/ocfs2/aops.h |  2 ++
 fs/ocfs2/file.c | 69 ++++++++++++++++++++++++++++++++++++++-----------
 3 files changed, 108 insertions(+), 15 deletions(-)

diff --git a/fs/ocfs2/aops.c b/fs/ocfs2/aops.c
index 491410430f8fd..68f5888757fbc 100644
--- a/fs/ocfs2/aops.c
+++ b/fs/ocfs2/aops.c
@@ -2828,6 +2828,58 @@ int ocfs2_dio_zero_eof(struct inode *inode, loff_t pos, size_t count)
 	return ret;
 }
 
+/*
+ * A direct write which does not cover whole pages leaves the rest of the
+ * page untouched, and iomap only zeroes the padding up to the file system
+ * block size.  That is safe only when the partial blocks at both ends are
+ * already allocated and written; a hole or an unwritten extent there would
+ * expose stale data once the cluster is marked written.  Let buffered I/O
+ * handle those.
+ *
+ * Called with the rw lock held, so no other node can punch or truncate the
+ * range underneath us.
+ */
+bool ocfs2_dio_subpage_needs_buffered(struct inode *inode, loff_t pos,
+				      size_t count, bool nowait)
+{
+	unsigned int blkbits = inode->i_blkbits;
+	struct ocfs2_map_block map;
+	int ret;
+
+	if (i_blocksize(inode) >= PAGE_SIZE)
+		return false;
+	if (IS_ALIGNED(pos, PAGE_SIZE) && IS_ALIGNED(pos + count, PAGE_SIZE))
+		return false;
+	if (pos + count > i_size_read(inode))
+		return true;
+
+	/*
+	 * A page-aligned end is block-aligned and fully covered, so it leaves
+	 * no partial block to check.  Only probe the ends the write does not
+	 * cover whole, and skip the tail when it shares the head's block.
+	 */
+	if (!IS_ALIGNED(pos, PAGE_SIZE)) {
+		map.lblk = pos >> blkbits;
+		map.len = 1;
+		map.flags = 0;
+		ret = ocfs2_lock_map_blocks(inode, &map, 0, nowait);
+		if (ret || !(map.flags & OCFS2_MAP_MAPPED))
+			return true;
+	}
+
+	if (!IS_ALIGNED(pos + count, PAGE_SIZE) &&
+	    ((pos + count - 1) >> blkbits) != (pos >> blkbits)) {
+		map.lblk = (pos + count - 1) >> blkbits;
+		map.len = 1;
+		map.flags = 0;
+		ret = ocfs2_lock_map_blocks(inode, &map, 0, nowait);
+		if (ret || !(map.flags & OCFS2_MAP_MAPPED))
+			return true;
+	}
+
+	return false;
+}
+
 /*
  * The most a single mapping handed out for a direct write spans.
  * It bounds the memory of the write context, one descriptor per
diff --git a/fs/ocfs2/aops.h b/fs/ocfs2/aops.h
index 7336df15ca3a0..38a977938223a 100644
--- a/fs/ocfs2/aops.h
+++ b/fs/ocfs2/aops.h
@@ -51,6 +51,8 @@ int ocfs2_size_fits_inline_data(struct buffer_head *di_bh, u64 new_size);
 int ocfs2_get_block(struct inode *inode, sector_t iblock,
 		    struct buffer_head *bh_result, int create);
 int ocfs2_dio_zero_eof(struct inode *inode, loff_t pos, size_t count);
+bool ocfs2_dio_subpage_needs_buffered(struct inode *inode, loff_t pos,
+				      size_t count, bool nowait);
 void ocfs2_dio_clear_orphan(struct inode *inode, loff_t end);
 int ocfs2_map_blocks(struct inode *inode, struct ocfs2_map_block *map,
 		    int flags);
diff --git a/fs/ocfs2/file.c b/fs/ocfs2/file.c
index a96fb831b8a98..03d24e71645f1 100644
--- a/fs/ocfs2/file.c
+++ b/fs/ocfs2/file.c
@@ -2425,21 +2425,6 @@ static bool ocfs2_should_use_dio(struct kiocb *iocb, struct iov_iter *iter,
 	    !ocfs2_supports_append_dio(osb))
 		return false;
 
-	/*
-	 * A direct write which does not cover whole pages leaves the rest of
-	 * the page untouched. iomap only zeroes the padding up to the file
-	 * system block size, so on a file system with a block size smaller
-	 * than the page size the untouched blocks would expose stale data
-	 * once the cluster is marked written. Let buffered I/O handle it.
-	 *
-	 * TODO: Avoid unnecessarily demoting in-place sub-page overwrites
-	 * to buffered I/O when i_blocksize(inode) < PAGE_SIZE.
-	 */
-	if (iov_iter_rw(iter) == WRITE && i_blocksize(inode) < PAGE_SIZE &&
-	    (!IS_ALIGNED(iocb->ki_pos, PAGE_SIZE) ||
-	     !IS_ALIGNED(iocb->ki_pos + iter->count, PAGE_SIZE)))
-		return false;
-
 	return true;
 }
 
@@ -2583,6 +2568,60 @@ static ssize_t ocfs2_file_write_iter(struct kiocb *iocb,
 		goto out;
 	}
 
+	/*
+	 * generic_write_checks() has moved ki_pos to i_size for O_APPEND and
+	 * clamped the length, and ocfs2_prepare_inode_for_write() has
+	 * refreshed i_size and the inode flags under the cluster lock.  The
+	 * decisions taken before them can be stale, take them again with what
+	 * the I/O is actually going to use.
+	 */
+	if (direct_io) {
+		use_dio = ocfs2_should_use_dio(iocb, from, inode);
+
+		/*
+		 * Only now are ki_pos and the length final, so this is where
+		 * a sub-page write can be checked against the blocks it will
+		 * actually touch.  A pure overwrite of written blocks stays
+		 * direct; anything needing zeroing goes buffered.
+		 */
+		if (use_dio && iov_iter_rw(from) == WRITE &&
+		    ocfs2_dio_subpage_needs_buffered(inode, iocb->ki_pos,
+						     iov_iter_count(from),
+						     nowait))
+			use_dio = false;
+
+		/*
+		 * A demoted NOWAIT write cannot fall back to buffered I/O;
+		 * reject it here, before kiocb_modified() dirties the inode.
+		 */
+		if (nowait && !use_dio) {
+			ret = -EAGAIN;
+			goto out;
+		}
+
+		/* An extending write has to hold the rw lock EX */
+		if (use_dio && rw_level == 0 && !nowait &&
+		    iocb->ki_pos + count > i_size_read(inode)) {
+			ret = ocfs2_dio_upgrade_rw_lock(inode, &rw_level);
+			if (ret)
+				goto out;
+			/*
+			 * The upgrade dropped the rw lock, so another node may
+			 * have reflinked, punched or extended the range since
+			 * ocfs2_prepare_inode_for_write() ran.  Refresh the
+			 * inode and extent cache and redo any CoW before the
+			 * direct write maps blocks without the inode lock.
+			 */
+			ret = ocfs2_prepare_inode_for_write(file, iocb->ki_pos,
+							    count, 1);
+			if (ret < 0) {
+				if (ret != -EAGAIN)
+					mlog_errno(ret);
+				goto out;
+			}
+		}
+	}
+
 	ret = kiocb_modified(iocb);
 	if (ret < 0)
 		goto out;
-- 
2.39.3


  parent reply	other threads:[~2026-12-01 10:07 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 10:07 [RFC PATCH v14 00/10] migrate ocfs2 DIO from buffer_head to iomap Joseph Qi
2026-12-01 10:07 ` [RFC PATCH v14 01/10] ocfs2: Add new ocfs2_map_blocks() to introduce iomap feature Joseph Qi
2026-12-01 10:07 ` [RFC PATCH v14 02/10] ocfs2: switch DIO read path from buffer_head to iomap Joseph Qi
2026-12-01 10:07 ` [RFC PATCH v14 03/10] ocfs2: add ocfs2_get_clusters_uncached() Joseph Qi
2026-12-01 10:07 ` [RFC PATCH v14 04/10] ocfs2: refactor ocfs2_lock_allocators() and add a dio variant Joseph Qi
2026-12-01 10:07 ` [RFC PATCH v14 05/10] ocfs2: record the fsync transaction when CoW makes clusters writable Joseph Qi
2026-12-01 10:07 ` [RFC PATCH v14 06/10] ocfs2: rework the direct write context for multi-cluster mappings Joseph Qi
2026-12-01 10:07 ` [RFC PATCH v14 07/10] ocfs2: switch dio write path from buffer_head to iomap Joseph Qi
2026-12-01 10:07 ` Joseph Qi [this message]
2026-12-01 10:07 ` [RFC PATCH v14 09/10] ocfs2: write the dio zeroed head/tail buffers out before completion Joseph Qi
2026-12-01 10:07 ` [RFC PATCH v14 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=20261201100724.2909559-9-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 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.