* [PATCH v2 1/2] nfs: add nowait version of nfs_start_io_direct
2026-06-07 7:31 [PATCH v2 0/2] nfs: support FMODE_NOWAIT on O_DIRECT reads Dylan Yudaken
@ 2026-06-07 7:31 ` Dylan Yudaken
2026-06-07 7:31 ` [PATCH v2 2/2] nfs: expose FMODE_NOWAIT for read-only files Dylan Yudaken
1 sibling, 0 replies; 3+ messages in thread
From: Dylan Yudaken @ 2026-06-07 7:31 UTC (permalink / raw)
To: trondmy, anna, linux-nfs; +Cc: axboe, io-uring, linux-kernel, Dylan Yudaken
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 | 41 +++++++++++++++++++++++++++++++++++++++++
2 files changed, 42 insertions(+)
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..2faf2003faf6 100644
--- a/fs/nfs/io.c
+++ b/fs/nfs/io.c
@@ -109,6 +109,16 @@ static void nfs_block_buffered(struct nfs_inode *nfsi, struct inode *inode)
}
}
+static int nfs_block_buffered_nowait(struct nfs_inode *nfsi, struct inode *inode)
+{
+ if (!test_bit(NFS_INO_ODIRECT, &nfsi->flags)) {
+ if (inode->i_mapping->nrpages != 0)
+ return 1;
+ set_bit(NFS_INO_ODIRECT, &nfsi->flags);
+ }
+ return 0;
+}
+
/**
* nfs_start_io_direct - declare the file is being used for direct i/o
* @inode: file inode
@@ -149,6 +159,37 @@ nfs_start_io_direct(struct inode *inode)
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_nowait(nfsi, inode)) {
+ 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
^ permalink raw reply [flat|nested] 3+ messages in thread* [PATCH v2 2/2] nfs: expose FMODE_NOWAIT for read-only files
2026-06-07 7:31 [PATCH v2 0/2] nfs: support FMODE_NOWAIT on O_DIRECT reads Dylan Yudaken
2026-06-07 7:31 ` [PATCH v2 1/2] nfs: add nowait version of nfs_start_io_direct Dylan Yudaken
@ 2026-06-07 7:31 ` Dylan Yudaken
1 sibling, 0 replies; 3+ messages in thread
From: Dylan Yudaken @ 2026-06-07 7:31 UTC (permalink / raw)
To: trondmy, anna, linux-nfs; +Cc: axboe, io-uring, linux-kernel, Dylan Yudaken
NFS O_DIRECT reads already (mostly) handle async requests, with the
exception of locking the inode for direct.
Handle async requests properly by using nfs_start_io_direct_nowait,
and then expose FMODE_NOWAIT since it's now supported for direct reads.
Signed-off-by: Dylan Yudaken <dyudaken@gmail.com>
---
fs/nfs/direct.c | 12 ++++++++++--
fs/nfs/file.c | 16 +++++++++++++++-
2 files changed, 25 insertions(+), 3 deletions(-)
diff --git a/fs/nfs/direct.c b/fs/nfs/direct.c
index 48d89716193a..e626c72495e6 100644
--- a/fs/nfs/direct.c
+++ b/fs/nfs/direct.c
@@ -466,14 +466,22 @@ ssize_t nfs_file_direct_read(struct kiocb *iocb, struct iov_iter *iter,
goto out_release;
}
dreq->l_ctx = l_ctx;
- if (!is_sync_kiocb(iocb))
+ if (!is_sync_kiocb(iocb)) {
dreq->iocb = iocb;
+ } else if (iocb->ki_flags & IOCB_NOWAIT) {
+ result = -EAGAIN;
+ nfs_direct_req_release(dreq);
+ goto out_release;
+ }
if (user_backed_iter(iter))
dreq->flags = NFS_ODIRECT_SHOULD_DIRTY;
if (!swap) {
- result = nfs_start_io_direct(inode);
+ if (iocb->ki_flags & IOCB_NOWAIT)
+ result = nfs_start_io_direct_nowait(inode);
+ else
+ result = nfs_start_io_direct(inode);
if (result) {
/* release the reference that would usually be
* consumed by nfs_direct_read_schedule_iovec()
diff --git a/fs/nfs/file.c b/fs/nfs/file.c
index 25048a3c2364..a0d8f1c1cf10 100644
--- a/fs/nfs/file.c
+++ b/fs/nfs/file.c
@@ -72,8 +72,12 @@ nfs_file_open(struct inode *inode, struct file *filp)
return res;
res = nfs_open(inode, filp);
- if (res == 0)
+ if (res == 0) {
filp->f_mode |= FMODE_CAN_ODIRECT;
+ /* flag NOWAIT on read-only files only */
+ if (!(filp->f_mode & FMODE_WRITE))
+ filp->f_mode |= FMODE_NOWAIT;
+ }
return res;
}
@@ -166,6 +170,10 @@ nfs_file_read(struct kiocb *iocb, struct iov_iter *to)
if (iocb->ki_flags & IOCB_DIRECT)
return nfs_file_direct_read(iocb, to, false);
+ /* NOWAIT only supported on direct reads */
+ if (iocb->ki_flags & IOCB_NOWAIT)
+ return -EAGAIN;
+
dprintk("NFS: read(%pD2, %zu@%lu)\n",
iocb->ki_filp,
iov_iter_count(to), (unsigned long) iocb->ki_pos);
@@ -705,6 +713,12 @@ ssize_t nfs_file_write(struct kiocb *iocb, struct iov_iter *from)
trace_nfs_file_write(iocb, from);
+ /*
+ * FMODE_NOWAIT is not set for writable files
+ */
+ if (WARN_ON_ONCE(iocb->ki_flags & IOCB_NOWAIT))
+ return -EAGAIN;
+
result = nfs_key_timeout_notify(file, inode);
if (result)
return result;
--
2.50.1
^ permalink raw reply [flat|nested] 3+ messages in thread