public inbox for [email protected]
 help / color / mirror / Atom feed
From: Hao Xu <[email protected]>
To: [email protected], Jens Axboe <[email protected]>
Cc: Dominique Martinet <[email protected]>,
	Pavel Begunkov <[email protected]>,
	Christian Brauner <[email protected]>,
	Alexander Viro <[email protected]>,
	Stefan Roesch <[email protected]>, Clay Harris <[email protected]>,
	Dave Chinner <[email protected]>,
	"Darrick J . Wong" <[email protected]>,
	[email protected], [email protected],
	[email protected], Wanpeng Li <[email protected]>
Subject: [PATCH 7/7] add lseek for io_uring
Date: Wed, 26 Jul 2023 18:26:03 +0800	[thread overview]
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>

From: Hao Xu <[email protected]>

This is related with previous io_uring getdents patchset, we need a way
to rewind the cursor of file when it comes to the end of a file by
getdents. Introduce lseek to io_uring for this, besides, it's also a
common syscall users call. So it's good for coding consistency when
users use io_uring as their main loop.

Signed-off-by: Hao Xu <[email protected]>
---
 include/uapi/linux/io_uring.h |  1 +
 io_uring/fs.c                 | 63 +++++++++++++++++++++++++++++++++++
 io_uring/fs.h                 |  3 ++
 io_uring/opdef.c              |  8 +++++
 4 files changed, 75 insertions(+)

diff --git a/include/uapi/linux/io_uring.h b/include/uapi/linux/io_uring.h
index c3efe241e310..d445876d4afc 100644
--- a/include/uapi/linux/io_uring.h
+++ b/include/uapi/linux/io_uring.h
@@ -236,6 +236,7 @@ enum io_uring_op {
 	IORING_OP_SEND_ZC,
 	IORING_OP_SENDMSG_ZC,
 	IORING_OP_GETDENTS,
+	IORING_OP_LSEEK,
 
 	/* this goes last, obviously */
 	IORING_OP_LAST,
diff --git a/io_uring/fs.c b/io_uring/fs.c
index 793eceb562a7..3992a19195ff 100644
--- a/io_uring/fs.c
+++ b/io_uring/fs.c
@@ -53,6 +53,12 @@ struct io_getdents {
 	unsigned int			count;
 };
 
+struct io_lseek {
+	struct file			*file;
+	off_t				offset;
+	unsigned int			whence;
+};
+
 int io_renameat_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
 {
 	struct io_rename *ren = io_kiocb_to_cmd(req, struct io_rename);
@@ -348,3 +354,60 @@ int io_getdents(struct io_kiocb *req, unsigned int issue_flags)
 	return 0;
 }
 
+int io_lseek_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
+{
+	struct io_lseek *lsk = io_kiocb_to_cmd(req, struct io_lseek);
+
+	if (unlikely(req->flags & REQ_F_FIXED_FILE))
+		return -EBADF;
+
+	lsk->offset = READ_ONCE(sqe->addr);
+	lsk->whence = READ_ONCE(sqe->len);
+
+	return 0;
+}
+
+int io_lseek(struct io_kiocb *req, unsigned int issue_flags)
+{
+	struct io_lseek *lsk = io_kiocb_to_cmd(req, struct io_lseek);
+	struct file *file = req->file;
+	bool force_nonblock = issue_flags & IO_URING_F_NONBLOCK;
+	bool should_lock = file->f_mode & FMODE_ATOMIC_POS;
+	unsigned int whence = lsk->whence;
+	loff_t res;
+	off_t ret;
+
+	if (whence > SEEK_MAX)
+		return -EINVAL;
+
+	if (force_nonblock) {
+		if (!(file->f_flags & O_NONBLOCK) &&
+		    !(file->f_mode & FMODE_NOWAIT))
+			return -EAGAIN;
+	}
+
+	if (should_lock) {
+		if (!force_nonblock)
+			mutex_lock(&file->f_pos_lock);
+		else if (!mutex_trylock(&file->f_pos_lock))
+			return -EAGAIN;
+	}
+
+	res = vfs_lseek_nowait(file, lsk->offset, whence, force_nonblock);
+	if (res == -EAGAIN && force_nonblock) {
+		if (should_lock)
+			mutex_unlock(&file->f_pos_lock);
+		return -EAGAIN;
+	}
+
+	ret = res;
+	if (res != (loff_t)ret)
+		ret = -EOVERFLOW;
+
+	if (should_lock)
+		mutex_unlock(&file->f_pos_lock);
+
+	io_req_set_res(req, ret, 0);
+	return 0;
+}
+
diff --git a/io_uring/fs.h b/io_uring/fs.h
index f83a6f3a678d..32a8441c5142 100644
--- a/io_uring/fs.h
+++ b/io_uring/fs.h
@@ -21,3 +21,6 @@ void io_link_cleanup(struct io_kiocb *req);
 
 int io_getdents_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
 int io_getdents(struct io_kiocb *req, unsigned int issue_flags);
+
+int io_lseek_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
+int io_lseek(struct io_kiocb *req, unsigned int issue_flags);
diff --git a/io_uring/opdef.c b/io_uring/opdef.c
index 1bae6b2a8d0b..eb1f7ee4f079 100644
--- a/io_uring/opdef.c
+++ b/io_uring/opdef.c
@@ -433,6 +433,11 @@ const struct io_issue_def io_issue_defs[] = {
 		.prep			= io_getdents_prep,
 		.issue			= io_getdents,
 	},
+	[IORING_OP_LSEEK] = {
+		.needs_file		= 1,
+		.prep			= io_lseek_prep,
+		.issue			= io_lseek,
+	},
 };
 
 
@@ -656,6 +661,9 @@ const struct io_cold_def io_cold_defs[] = {
 	[IORING_OP_GETDENTS] = {
 		.name			= "GETDENTS",
 	},
+	[IORING_OP_LSEEK] = {
+		.name			= "LSEEK",
+	},
 };
 
 const char *io_uring_get_opcode(u8 opcode)
-- 
2.25.1


  parent reply	other threads:[~2023-07-26 10:27 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-26 10:25 [RFC 0/7] io_uring lseek Hao Xu
2023-07-26 10:25 ` [PATCH 1/7] iomap: merge iomap_seek_hole() and iomap_seek_data() Hao Xu
2023-07-26 21:50   ` Dave Chinner
2023-07-27 12:10     ` Hao Xu
2023-07-26 10:25 ` [PATCH 2/7] xfs: add nowait support for xfs_seek_iomap_begin() Hao Xu
2023-07-26 21:55   ` Dave Chinner
2023-07-26 22:14     ` Darrick J. Wong
2023-07-27 12:17       ` Hao Xu
2023-07-26 10:25 ` [PATCH 3/7] add nowait parameter for iomap_seek() Hao Xu
2023-07-26 22:01   ` Dave Chinner
2023-07-26 10:26 ` [PATCH 4/7] add llseek_nowait() for struct file_operations Hao Xu
2023-07-27 13:25   ` Matthew Wilcox
2023-07-26 10:26 ` [PATCH 5/7] add llseek_nowait support for xfs Hao Xu
2023-07-26 22:14   ` Dave Chinner
2023-07-27 12:26     ` Hao Xu
2023-07-26 10:26 ` [PATCH 6/7] add vfs_lseek_nowait() Hao Xu
2023-07-26 10:26 ` Hao Xu [this message]
2023-07-26 13:22 ` [RFC 0/7] io_uring lseek Christian Brauner
2023-07-27 12:30 ` Hao Xu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox