[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20230827132835.1373581-11-hao.xu@linux.dev>
Date: Sun, 27 Aug 2023 21:28:34 +0800
From: Hao Xu <hao.xu@...ux.dev>
To: io-uring@...r.kernel.org, Jens Axboe <axboe@...nel.dk>
Cc: Dominique Martinet <asmadeus@...ewreck.org>,
Pavel Begunkov <asml.silence@...il.com>,
Christian Brauner <brauner@...nel.org>,
Alexander Viro <viro@...iv.linux.org.uk>,
Stefan Roesch <shr@...com>, Clay Harris <bugs@...ycon.org>,
Dave Chinner <david@...morbit.com>,
"Darrick J . Wong" <djwong@...nel.org>,
linux-fsdevel@...r.kernel.org, linux-xfs@...r.kernel.org,
linux-ext4@...r.kernel.org, linux-cachefs@...hat.com,
ecryptfs@...r.kernel.org, linux-nfs@...r.kernel.org,
linux-unionfs@...r.kernel.org, bpf@...r.kernel.org,
netdev@...r.kernel.org, linux-s390@...r.kernel.org,
linux-kernel@...r.kernel.org, linux-block@...r.kernel.org,
linux-btrfs@...r.kernel.org, codalist@...a.cs.cmu.edu,
linux-f2fs-devel@...ts.sourceforge.net, cluster-devel@...hat.com,
linux-mm@...ck.org, linux-nilfs@...r.kernel.org,
devel@...ts.orangefs.org, linux-cifs@...r.kernel.org,
samba-technical@...ts.samba.org, linux-mtd@...ts.infradead.org,
Wanpeng Li <wanpengli@...cent.com>
Subject: [PATCH 10/11] vfs: trylock inode->i_rwsem in iterate_dir() to support nowait
From: Hao Xu <howeyxu@...cent.com>
Trylock inode->i_rwsem in iterate_dir() to support nowait semantics and
error out -EAGAIN when there is contention.
Signed-off-by: Hao Xu <howeyxu@...cent.com>
---
fs/readdir.c | 20 ++++++++++++++------
1 file changed, 14 insertions(+), 6 deletions(-)
diff --git a/fs/readdir.c b/fs/readdir.c
index 6469f076ba6e..664ecd9665a1 100644
--- a/fs/readdir.c
+++ b/fs/readdir.c
@@ -43,6 +43,8 @@ int iterate_dir(struct file *file, struct dir_context *ctx)
struct inode *inode = file_inode(file);
bool shared = false;
int res = -ENOTDIR;
+ bool nowait;
+
if (file->f_op->iterate_shared)
shared = true;
else if (!file->f_op->iterate)
@@ -52,16 +54,22 @@ int iterate_dir(struct file *file, struct dir_context *ctx)
if (res)
goto out;
- if (shared)
- res = down_read_killable(&inode->i_rwsem);
- else
- res = down_write_killable(&inode->i_rwsem);
- if (res)
+ nowait = ctx->flags & DIR_CONTEXT_F_NOWAIT;
+ if (nowait) {
+ res = shared ? down_read_trylock(&inode->i_rwsem) :
+ down_write_trylock(&inode->i_rwsem);
+ if (!res)
+ res = -EAGAIN;
+ } else {
+ res = shared ? down_read_killable(&inode->i_rwsem) :
+ down_write_killable(&inode->i_rwsem);
+ }
+ if (res < 0)
goto out;
res = -ENOENT;
if (!IS_DEADDIR(inode)) {
- res = file_accessed(file, ctx->flags & DIR_CONTEXT_F_NOWAIT);
+ res = file_accessed(file, nowait);
if (res == -EAGAIN)
goto out_unlock;
--
2.25.1
Powered by blists - more mailing lists