Linux NFS development
 help / color / mirror / Atom feed
From: Dylan Yudaken <dyudaken@gmail.com>
To: trondmy@kernel.org, anna@kernel.org, linux-nfs@vger.kernel.org
Cc: axboe@kernel.dk, io-uring@vger.kernel.org,
	linux-kernel@vger.kernel.org, Dylan Yudaken <dyudaken@gmail.com>
Subject: [PATCH 1/2] nfs: add nowait version of nfs_start_io_direct
Date: Sat, 30 May 2026 23:19:46 +0100	[thread overview]
Message-ID: <20260530221947.49518-2-dyudaken@gmail.com> (raw)
In-Reply-To: <20260530221947.49518-1-dyudaken@gmail.com>

nfs_start_io_direct might block on existing operations to the same
inode. In order to support NOWAIT O_DIRECT reads, add a non-blocking
version of this nfs_start_io_direct that just returns -EAGAIN if locks
could not be taken.

Signed-off-by: Dylan Yudaken <dyudaken@gmail.com>
---
 fs/nfs/internal.h |  1 +
 fs/nfs/io.c       | 38 ++++++++++++++++++++++++++++++++++++--
 2 files changed, 37 insertions(+), 2 deletions(-)

diff --git a/fs/nfs/internal.h b/fs/nfs/internal.h
index 18d46b0e71dd..0c9aca624353 100644
--- a/fs/nfs/internal.h
+++ b/fs/nfs/internal.h
@@ -532,6 +532,7 @@ extern void nfs_end_io_read(struct inode *inode);
 extern  __must_check int nfs_start_io_write(struct inode *inode);
 extern void nfs_end_io_write(struct inode *inode);
 extern __must_check int nfs_start_io_direct(struct inode *inode);
+extern __must_check int nfs_start_io_direct_nowait(struct inode *inode);
 extern void nfs_end_io_direct(struct inode *inode);
 
 static inline bool nfs_file_io_is_buffered(struct nfs_inode *nfsi)
diff --git a/fs/nfs/io.c b/fs/nfs/io.c
index 8337f0ae852d..f359a19f7879 100644
--- a/fs/nfs/io.c
+++ b/fs/nfs/io.c
@@ -101,12 +101,15 @@ nfs_end_io_write(struct inode *inode)
 EXPORT_SYMBOL_GPL(nfs_end_io_write);
 
 /* Call with exclusively locked inode->i_rwsem */
-static void nfs_block_buffered(struct nfs_inode *nfsi, struct inode *inode)
+static int nfs_block_buffered(struct nfs_inode *nfsi, struct inode *inode, bool nowait)
 {
 	if (!test_bit(NFS_INO_ODIRECT, &nfsi->flags)) {
+		if (nowait && !mapping_empty(inode->i_mapping))
+			return 1;
 		set_bit(NFS_INO_ODIRECT, &nfsi->flags);
 		nfs_sync_mapping(inode->i_mapping);
 	}
+	return 0;
 }
 
 /**
@@ -143,12 +146,43 @@ nfs_start_io_direct(struct inode *inode)
 	err = down_write_killable(&inode->i_rwsem);
 	if (err)
 		return err;
-	nfs_block_buffered(nfsi, inode);
+	nfs_block_buffered(nfsi, inode, false);
 	downgrade_write(&inode->i_rwsem);
 
 	return 0;
 }
 
+/**
+ * nfs_start_io_direct_nowait - non-blocking variant of nfs_start_io_direct()
+ * @inode: file inode
+ *
+ * Try to declare that a direct I/O operation is about to start without
+ * blocking.
+ * Ensure all buffered I/O is blocked.
+ * If this could not be done without blocking then returns -EAGAIN.
+ */
+int
+nfs_start_io_direct_nowait(struct inode *inode)
+{
+	struct nfs_inode *nfsi = NFS_I(inode);
+
+	if (!down_read_trylock(&inode->i_rwsem))
+		return -EAGAIN;
+	if (test_bit(NFS_INO_ODIRECT, &nfsi->flags))
+		return 0;
+	up_read(&inode->i_rwsem);
+
+	/* Slow path: try to flip NFS_INO_ODIRECT without blocking. */
+	if (!down_write_trylock(&inode->i_rwsem))
+		return -EAGAIN;
+	if (nfs_block_buffered(nfsi, inode, true)) {
+		up_write(&inode->i_rwsem);
+		return -EAGAIN;
+	}
+	downgrade_write(&inode->i_rwsem);
+	return 0;
+}
+
 /**
  * nfs_end_io_direct - declare that the direct i/o operation is done
  * @inode: file inode
-- 
2.50.1


  reply	other threads:[~2026-05-30 22:20 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-30 22:19 [PATCH 0/2] nfs: support FMODE_NOWAIT on O_DIRECT reads Dylan Yudaken
2026-05-30 22:19 ` Dylan Yudaken [this message]
2026-05-30 22:19 ` [PATCH 2/2] nfs: expose FMODE_NOWAIT for O_DIRECT read files Dylan Yudaken

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=20260530221947.49518-2-dyudaken@gmail.com \
    --to=dyudaken@gmail.com \
    --cc=anna@kernel.org \
    --cc=axboe@kernel.dk \
    --cc=io-uring@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-nfs@vger.kernel.org \
    --cc=trondmy@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox