From: Avi Kivity FMODE_NOWAIT was only set on read-only files, and only by nfs_file_open() - nfs4_file_open() never set it at all, so on NFSv4 mounts even read-only files did not get it. io_uring gates REQ_F_SUPPORT_NOWAIT on this flag, so every read and write on an O_DIRECT NFS file was punted to an io-wq worker before the request ever reached NFS. Now that nfs_file_direct_write() honours IOCB_NOWAIT, set the flag regardless of the access mode from all three open paths: nfs_file_open(), nfs4_file_open(), and nfs_atomic_open(). The last one matters because on NFSv4 a file opened with O_CREAT goes through ->atomic_open() and never reaches nfs4_file_open(); without it every newly created file would still lack the flag. The buffered write path returns -EAGAIN instead of warning; move that check below the O_DIRECT dispatch so direct writes are not caught by it. pNFS is excluded. pnfs_update_layout() is reached from ->pg_init() in the submission loop and blocks - on NFS_LAYOUT_DRAIN, on a pending layoutreturn, and on the LAYOUTGET RPC itself - so claiming NOWAIT there would only move the stall into the io_uring submitter. Making that path non-blocking needs a nowait flag plumbed through pnfs_update_layout() and the layout drivers, which is left for later. Assisted-by: Claude:claude-opus-5 Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Avi Kivity --- fs/nfs/dir.c | 4 ++++ fs/nfs/file.c | 24 ++++++++++++++++-------- fs/nfs/nfs4file.c | 3 +++ fs/nfs/pnfs.h | 5 +++++ 4 files changed, 28 insertions(+), 8 deletions(-) diff --git a/fs/nfs/dir.c b/fs/nfs/dir.c index 49437037f948d..a3089cbdc1cf2 100644 --- a/fs/nfs/dir.c +++ b/fs/nfs/dir.c @@ -42,10 +42,11 @@ #include "delegation.h" #include "iostat.h" #include "internal.h" #include "fscache.h" +#include "pnfs.h" #include "nfstrace.h" /* #define NFS_DEBUG_VERBOSE 1 */ @@ -2209,10 +2210,13 @@ int nfs_atomic_open(struct inode *dir, struct dentry *dentry, break; } goto out; } file->f_mode |= FMODE_CAN_ODIRECT; + /* See the FMODE_NOWAIT comment in nfs_file_open() */ + if (!pnfs_enabled_sb(NFS_SERVER(dir))) + file->f_mode |= FMODE_NOWAIT; if (test_bit(NFS_CONTEXT_O_DIRECT, &ctx->flags)) { file->f_flags |= O_DIRECT; open_flags |= O_DIRECT; } diff --git a/fs/nfs/file.c b/fs/nfs/file.c index 38f830a6467c9..8e119b55577d9 100644 --- a/fs/nfs/file.c +++ b/fs/nfs/file.c @@ -71,12 +71,22 @@ nfs_file_open(struct inode *inode, struct file *filp) return res; res = nfs_open(inode, filp); if (res == 0) { filp->f_mode |= FMODE_CAN_ODIRECT; - /* flag NOWAIT on read-only files only */ - if (!(filp->f_mode & FMODE_WRITE)) + /* + * Both nfs_file_read() and nfs_file_write() honour IOCB_NOWAIT: + * the O_DIRECT paths make a genuine non-blocking attempt, and + * the buffered paths report -EAGAIN. Without FMODE_NOWAIT + * io_uring punts every request on a writable file to an io-wq + * worker before it ever reaches us. + * + * pNFS is excluded: issuing an I/O for which no layout segment + * is cached requires a synchronous LAYOUTGET, and there is no + * way to ask for that without blocking yet. + */ + if (!pnfs_enabled_sb(NFS_SERVER(inode))) filp->f_mode |= FMODE_NOWAIT; } return res; } @@ -743,23 +753,21 @@ ssize_t nfs_file_write(struct kiocb *iocb, struct iov_iter *from) errseq_t since; int error; 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; if (iocb->ki_flags & IOCB_DIRECT) return nfs_file_direct_write(iocb, from, false); + /* NOWAIT is only supported on direct writes */ + if (iocb->ki_flags & IOCB_NOWAIT) + return -EAGAIN; + dprintk("NFS: write(%pD2, %zu@%Ld)\n", file, iov_iter_count(from), (long long) iocb->ki_pos); if (IS_SWAPFILE(inode)) goto out_swapfile; diff --git a/fs/nfs/nfs4file.c b/fs/nfs/nfs4file.c index 9a434f5dda8d3..1cbb4160377c3 100644 --- a/fs/nfs/nfs4file.c +++ b/fs/nfs/nfs4file.c @@ -89,10 +89,13 @@ nfs4_file_open(struct inode *inode, struct file *filp) nfs_file_set_open_context(filp, ctx); nfs_fscache_open_file(inode, filp); err = 0; filp->f_mode |= FMODE_CAN_ODIRECT; + /* See the FMODE_NOWAIT comment in nfs_file_open() */ + if (!pnfs_enabled_sb(NFS_SERVER(inode))) + filp->f_mode |= FMODE_NOWAIT; if (test_bit(NFS_CONTEXT_O_DIRECT, &ctx->flags)) filp->f_flags |= O_DIRECT; out_put_ctx: put_nfs_open_context(ctx); diff --git a/fs/nfs/pnfs.h b/fs/nfs/pnfs.h index ce98fe5264acd..13b4ee7c6febb 100644 --- a/fs/nfs/pnfs.h +++ b/fs/nfs/pnfs.h @@ -908,10 +908,15 @@ static inline void set_pnfs_layoutdriver(struct nfs_server *s, static inline void unset_pnfs_layoutdriver(struct nfs_server *s) { } +static inline int pnfs_enabled_sb(struct nfs_server *nfss) +{ + return 0; +} + static inline int pnfs_commit_list(struct inode *inode, struct list_head *mds_pages, int how, struct nfs_commit_info *cinfo) { return PNFS_NOT_ATTEMPTED; -- 2.56.1