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 	/* NOTE: kiocb has the file as the first member, so don't do it here */
16 	struct file     *file;
17 	int             result;
18 	int		fd;
19 	unsigned int	flags;
20 };
21 
22 #define NOP_FLAGS	(IORING_NOP_INJECT_RESULT | IORING_NOP_FIXED_FILE | \
23 			 IORING_NOP_FIXED_BUFFER | IORING_NOP_FILE | \
24 			 IORING_NOP_TW)
25 
io_nop_prep(struct io_kiocb * req,const struct io_uring_sqe * sqe)26 int io_nop_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
27 {
28 	struct io_nop *nop = io_kiocb_to_cmd(req, struct io_nop);
29 
30 	nop->flags = READ_ONCE(sqe->nop_flags);
31 	if (nop->flags & ~NOP_FLAGS)
32 		return -EINVAL;
33 
34 	if (nop->flags & IORING_NOP_INJECT_RESULT)
35 		nop->result = READ_ONCE(sqe->len);
36 	else
37 		nop->result = 0;
38 	if (nop->flags & IORING_NOP_FILE)
39 		nop->fd = READ_ONCE(sqe->fd);
40 	else
41 		nop->fd = -1;
42 	if (nop->flags & IORING_NOP_FIXED_BUFFER)
43 		req->buf_index = READ_ONCE(sqe->buf_index);
44 	return 0;
45 }
46 
io_nop(struct io_kiocb * req,unsigned int issue_flags)47 int io_nop(struct io_kiocb *req, unsigned int issue_flags)
48 {
49 	struct io_nop *nop = io_kiocb_to_cmd(req, struct io_nop);
50 	int ret = nop->result;
51 
52 	if (nop->flags & IORING_NOP_FILE) {
53 		if (nop->flags & IORING_NOP_FIXED_FILE) {
54 			req->file = io_file_get_fixed(req, nop->fd, issue_flags);
55 			req->flags |= REQ_F_FIXED_FILE;
56 		} else {
57 			req->file = io_file_get_normal(req, nop->fd);
58 		}
59 		if (!req->file) {
60 			ret = -EBADF;
61 			goto done;
62 		}
63 	}
64 	if (nop->flags & IORING_NOP_FIXED_BUFFER) {
65 		if (!io_find_buf_node(req, issue_flags))
66 			ret = -EFAULT;
67 	}
68 done:
69 	if (ret < 0)
70 		req_set_fail(req);
71 	io_req_set_res(req, nop->result, 0);
72 	if (nop->flags & IORING_NOP_TW) {
73 		req->io_task_work.func = io_req_task_complete;
74 		io_req_task_work_add(req);
75 		return IOU_ISSUE_SKIP_COMPLETE;
76 	}
77 	return IOU_COMPLETE;
78 }
79