lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20230726102603.155522-8-hao.xu@linux.dev>
Date:   Wed, 26 Jul 2023 18:26:03 +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, Wanpeng Li <wanpengli@...cent.com>
Subject: [PATCH 7/7] add lseek for io_uring

From: Hao Xu <howeyxu@...cent.com>

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 <howeyxu@...cent.com>
---
 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

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ