Skip to contentSkip to conversation

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

PATCH In review [email protected] 2 participants 3 messages Feb 2, 2020 – Feb 3, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fuse: Allow parallel DIO reads and check NOWAIT case for DIO writes

From: chenqiwu <[email protected]>

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.

---
 fs/fuse/file.c | 26 +++++++++++++++++++++-----
 1 file changed, 21 insertions(+), 5 deletions(-)
fs/fuse/file.c +21 -5
index ce71538..4fcf492 100644
@@ -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) {
Signature · 1 line
1.9.1
#1 lore raw

fs/fuse/file.c static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to) · line 1530

Quoted 9 lines
fs/fuse/file.c +3 -0
@@ -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);

Shouldn't the file_accessed() in different patch, with a description? It looks totally unrelated to locking?

Thanks,
Bernd

author
#2 lore raw
Quoted 4 lines · Bernd Schubert
Quoted 4 lines
(quoted diff) +0 -0
@@ -1518,6 +1525,9 @@ static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to)
 
Quoted 10 lines

fs/fuse/file.c static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to) · line 1530

Quoted 5 lines
fs/fuse/file.c +3 -0
 		res = __fuse_direct_read(&io, to, &iocb->ki_pos);
 	}
+	inode_unlock_shared(inode);
+
+	file_accessed(iocb->ki_filp);

Shouldn't the file_accessed() in different patch, with a description? It looks totally unrelated to locking?

Thanks for your remind! file_accessed() is used to update atime for every direct read, it's totally unrelated to locking. I will separate it to another patch.