xref: /linux/io_uring/nop.c (revision 056a5087d87ead77dedbe9cf5bde53b7cd4b4651)
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 
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_BUFFER)
44 		req->buf_index = READ_ONCE(sqe->buf_index);
45 	if (nop->flags & IORING_NOP_CQE32) {
46 		struct io_ring_ctx *ctx = req->ctx;
47 
48 		if (!(ctx->flags & (IORING_SETUP_CQE32|IORING_SETUP_CQE_MIXED)))
49 			return -EINVAL;
50 		nop->extra1 = READ_ONCE(sqe->off);
51 		nop->extra2 = READ_ONCE(sqe->addr);
52 	}
53 	return 0;
54 }
55 
56 int io_nop(struct io_kiocb *req, unsigned int issue_flags)
57 {
58 	struct io_nop *nop = io_kiocb_to_cmd(req, struct io_nop);
59 	int ret = nop->result;
60 
61 	if (nop->flags & IORING_NOP_FILE) {
62 		if (nop->flags & IORING_NOP_FIXED_FILE) {
63 			req->file = io_file_get_fixed(req, nop->fd, issue_flags);
64 			req->flags |= REQ_F_FIXED_FILE;
65 		} else {
66 			req->file = io_file_get_normal(req, nop->fd);
67 		}
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