[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20230827132835.1373581-5-hao.xu@linux.dev>
Date: Sun, 27 Aug 2023 21:28:28 +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 04/11] vfs: add a vfs helper for io_uring file pos lock
From: Hao Xu <howeyxu@...cent.com>
Add a vfs helper file_pos_lock_nowait() for io_uring usage. The function
have conditional nowait logic, i.e. if nowait is needed, return -EAGAIN
when trylock fails.
Signed-off-by: Hao Xu <howeyxu@...cent.com>
---
fs/file.c | 13 +++++++++++++
include/linux/file.h | 2 ++
2 files changed, 15 insertions(+)
diff --git a/fs/file.c b/fs/file.c
index 35c62b54c9d6..8e5c38f5db52 100644
--- a/fs/file.c
+++ b/fs/file.c
@@ -1053,6 +1053,19 @@ void __f_unlock_pos(struct file *f)
mutex_unlock(&f->f_pos_lock);
}
+int file_pos_lock_nowait(struct file *file, bool nowait)
+{
+ if (!(file->f_mode & FMODE_ATOMIC_POS))
+ return 0;
+
+ if (!nowait)
+ mutex_lock(&file->f_pos_lock);
+ else if (!mutex_trylock(&file->f_pos_lock))
+ return -EAGAIN;
+
+ return 1;
+}
+
/*
* We only lock f_pos if we have threads or if the file might be
* shared with another process. In both cases we'll have an elevated
diff --git a/include/linux/file.h b/include/linux/file.h
index 6e9099d29343..bcc6ba0aec50 100644
--- a/include/linux/file.h
+++ b/include/linux/file.h
@@ -81,6 +81,8 @@ static inline void fdput_pos(struct fd f)
fdput(f);
}
+extern int file_pos_lock_nowait(struct file *file, bool nowait);
+
DEFINE_CLASS(fd, struct fd, fdput(_T), fdget(fd), int fd)
extern int f_dupfd(unsigned int from, struct file *file, unsigned flags);
--
2.25.1
Powered by blists - more mailing lists