From: Avi Kivity nfs_file_direct_write() ignored IOCB_NOWAIT and blocked unconditionally in nfs_start_io_direct(), so it could not be advertised as a non-blocking write path. Use nfs_start_io_direct_nowait() instead, mirroring what the read side already does. If the mapping still holds pages, refuse with -EAGAIN: the invalidation that follows the write cannot be done without blocking, and skipping it would leave a stale page cache behind for buffered readers. A synchronous kiocb with RWF_NOWAIT gets -EAGAIN as well, since there is no way to report the result later. This matches nfs_file_direct_read(). Assisted-by: Claude:claude-opus-5 Signed-off-by: Avi Kivity --- fs/nfs/direct.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/fs/nfs/direct.c b/fs/nfs/direct.c index 736af5cf2f6eb..e265faee5f824 100644 --- a/fs/nfs/direct.c +++ b/fs/nfs/direct.c @@ -1111,18 +1111,36 @@ ssize_t nfs_file_direct_write(struct kiocb *iocb, struct iov_iter *iter, } dreq->l_ctx = l_ctx; if (!is_sync_kiocb(iocb)) { dreq->iocb = iocb; nfs_direct_req_set_task(dreq, iocb); + } else if (iocb->ki_flags & IOCB_NOWAIT) { + /* A synchronous kiocb has no way to report completion later */ + result = -EAGAIN; + nfs_direct_req_release(dreq); + goto out_release; } pnfs_init_ds_commit_info_ops(&dreq->ds_cinfo, inode); if (swap) { requested = nfs_direct_write_schedule_iovec(dreq, iter, pos, FLUSH_STABLE); } else { - result = nfs_start_io_direct(inode); + if (iocb->ki_flags & IOCB_NOWAIT) { + result = nfs_start_io_direct_nowait(inode); + /* + * The post-write invalidation below cannot be done + * without blocking, so refuse the request rather than + * skip it and leave a stale page cache behind. + */ + if (!result && mapping->nrpages) { + nfs_end_io_direct(inode); + result = -EAGAIN; + } + } else { + result = nfs_start_io_direct(inode); + } if (result) { /* release the reference that would usually be * consumed by nfs_direct_write_schedule_iovec() */ nfs_direct_req_release(dreq); -- 2.56.1