@@ -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,
@@ -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;
+}
+
@@ -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);
@@ -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)