]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
nfs: add nowait version of nfs_start_io_direct
authorDylan Yudaken <dyudaken@gmail.com>
Sun, 7 Jun 2026 07:31:54 +0000 (08:31 +0100)
committerAnna Schumaker <anna.schumaker@hammerspace.com>
Mon, 8 Jun 2026 16:07:16 +0000 (12:07 -0400)
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>
Signed-off-by: Anna Schumaker <anna.schumaker@hammerspace.com>
fs/nfs/internal.h
fs/nfs/io.c

index 18d46b0e71ddc83a7b63f03eacf4337ed2735b61..0c9aca624353a8163bfc14b2e6d01e9d75b99066 100644 (file)
@@ -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)
index 8337f0ae852d42dfbcc8c0b20a71a5b54f0c7d26..2faf2003faf671a2a9fe082e3a926fd771aa81a0 100644 (file)
@@ -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