1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/kernel.h>
3 #include <linux/errno.h>
4 #include <linux/fs.h>
5 #include <linux/file.h>
6 #include <linux/io_uring.h>
7
8 #include <uapi/linux/io_uring.h>
9
10 #include "io_uring.h"
11 #include "rsrc.h"
12 #include "nop.h"
13
14 struct io_nop {
15 struct file *file;
16 int result;
17 int fd;
18 unsigned int flags;
19 __u64 extra1;
20 __u64 extra2;
21 };
22
23 #define NOP_FLAGS (IORING_NOP_INJECT_RESULT | IORING_NOP_FIXED_FILE | \
24 IORING_NOP_FIXED_BUFFER | IORING_NOP_FILE | \
25 IORING_NOP_TW | IORING_NOP_CQE32)
26
io_nop_prep(struct io_kiocb * req,const struct io_uring_sqe * sqe)27 int io_nop_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
28 {
29 struct io_nop *nop = io_kiocb_to_cmd(req, struct io_nop);
30
31 nop->flags = READ_ONCE(sqe->nop_flags);
32 if (nop->flags & ~NOP_FLAGS)
33 return -EINVAL;
34
35 if (nop->flags & IORING_NOP_INJECT_RESULT)
36 nop->result = READ_ONCE(sqe->len);
37 else
38 nop->result = 0;
39 if (nop->flags & IORING_NOP_FILE)
40 nop->fd = READ_ONCE(sqe->fd);
41 else
42 nop->fd = -1;
43 if (nop->flags & IORING_NOP_FIXED_FILE)
44 req->flags |= REQ_F_FIXED_FILE;
45 if (nop->flags & IORING_NOP_FIXED_BUFFER)
46 req->buf_index = READ_ONCE(sqe->buf_index);
47 if (nop->flags & IORING_NOP_CQE32) {
48 struct io_ring_ctx *ctx = req->ctx;
49
50 if (!(ctx->flags & (IORING_SETUP_CQE32|IORING_SETUP_CQE_MIXED)))
51 return -EINVAL;
52 nop->extra1 = READ_ONCE(sqe->off);
53 nop->extra2 = READ_ONCE(sqe->addr);
54 }
55 return 0;
56 }
57
io_nop(struct io_kiocb * req,unsigned int issue_flags)58 int io_nop(struct io_kiocb *req, unsigned int issue_flags)
59 {
60 struct io_nop *nop = io_kiocb_to_cmd(req, struct io_nop);
61 int ret = nop->result;
62
63 if (nop->flags & IORING_NOP_FILE) {
64 if (req->flags & REQ_F_FIXED_FILE)
65 req->file = io_file_get_fixed(req, nop->fd, issue_flags);
66 else
67 req->file = io_file_get_normal(req, nop->fd);
68 if (!req->file) {
69 ret = -EBADF;
70 goto done;
71 }
72 }
73 if (nop->flags & IORING_NOP_FIXED_BUFFER) {
74 if (!io_find_buf_node(req, issue_flags))
75 ret = -EFAULT;
76 }
77 done:
78 if (ret < 0)
79 req_set_fail(req);
80 if (nop->flags & IORING_NOP_CQE32)
81 io_req_set_res32(req, ret, 0, nop->extra1, nop->extra2);
82 else
83 io_req_set_res(req, ret, 0);
84 if (nop->flags & IORING_NOP_TW) {
85 req->io_task_work.func = io_req_task_complete;
86 io_req_task_work_add(req);
87 return IOU_ISSUE_SKIP_COMPLETE;
88 }
89 return IOU_COMPLETE;
90 }
91