[PATCH] fuse: Allow parallel DIO reads and check NOWAIT case for DIO writes

From: qiwuchen55
Date: Sat Feb 01 2020 - 22:35:05 EST


From: chenqiwu <chenqiwu@xxxxxxxxxx>

Earlier there was no shared lock in DIO read path. But this patch
(16c54688592ce: ext4: Allow parallel DIO reads)
simplified some of the locking mechanism while still allowing for
parallel DIO reads by adding shared lock in inode DIO read path.

Add NOWAIT check at the start of cache writes, because an aio request
with IOCB_NOWAIT could block when we call inode_lock(), which is not
allowed.

Change current rwsem code of direct write to do the trylock for the
IOCB_NOWAIT case, otherwise lock for real scheme.

Signed-off-by: chenqiwu <chenqiwu@xxxxxxxxxx>
---
fs/fuse/file.c | 26 +++++++++++++++++++++-----
1 file changed, 21 insertions(+), 5 deletions(-)

diff --git a/fs/fuse/file.c b/fs/fuse/file.c
index ce71538..4fcf492 100644
--- a/fs/fuse/file.c
+++ b/fs/fuse/file.c
@@ -1263,6 +1263,9 @@ static ssize_t fuse_cache_write_iter(struct kiocb *iocb, struct iov_iter *from)
ssize_t err;
loff_t endbyte = 0;

+ if (iocb->ki_flags & IOCB_NOWAIT)
+ return -EOPNOTSUPP;
+
if (get_fuse_conn(inode)->writeback_cache) {
/* Update size (EOF optimization) and mode (SUID clearing) */
err = fuse_update_attributes(mapping->host, file);
@@ -1432,11 +1435,7 @@ ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,

ia->io = io;
if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) {
- if (!write)
- inode_lock(inode);
fuse_sync_writes(inode);
- if (!write)
- inode_unlock(inode);
}

io->should_dirty = !write && iter_is_iovec(iter);
@@ -1510,6 +1509,14 @@ static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to)
{
ssize_t res;
+ struct inode *inode = file_inode(iocb->ki_filp);
+
+ if (iocb->ki_flags & IOCB_NOWAIT) {
+ if (!inode_trylock_shared(inode))
+ return -EAGAIN;
+ } else {
+ inode_lock_shared(inode);
+ }

if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) {
res = fuse_direct_IO(iocb, to);
@@ -1518,6 +1525,9 @@ static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to)

res = __fuse_direct_read(&io, to, &iocb->ki_pos);
}
+ inode_unlock_shared(inode);
+
+ file_accessed(iocb->ki_filp);

return res;
}
@@ -1529,7 +1539,13 @@ static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from)
ssize_t res;

/* Don't allow parallel writes to the same file */
- inode_lock(inode);
+ if (iocb->ki_flags & IOCB_NOWAIT) {
+ if (!inode_trylock(inode))
+ return -EAGAIN;
+ } else {
+ inode_lock(inode);
+ }
+
res = generic_write_checks(iocb, from);
if (res > 0) {
if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) {
--
1.9.1