xref: /linux/io_uring/nop.c (revision 4f9786035f9e519db41375818e1d0b5f20da2f10)
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 
25 int io_nop_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
26 {
27 	struct io_nop *nop = io_kiocb_to_cmd(req, struct io_nop);
28 
29 	nop->flags = READ_ONCE(sqe->nop_flags);
30 	if (nop->flags & ~NOP_FLAGS)
31 		return -EINVAL;
32 
33 	if (nop->flags & IORING_NOP_INJECT_RESULT)
34 		nop->result = READ_ONCE(sqe->len);
35 	else
36 		nop->result = 0;
37 	if (nop->flags & IORING_NOP_FILE)
38 		nop->fd = READ_ONCE(sqe->fd);
39 	else
40 		nop->fd = -1;
41 	if (nop->flags & IORING_NOP_FIXED_BUFFER)
42 		req->buf_index = READ_ONCE(sqe->buf_index);
43 	return 0;
44 }
45 
46 int io_nop(struct io_kiocb *req, unsigned int issue_flags)
47 {
48 	struct io_nop *nop = io_kiocb_to_cmd(req, struct io_nop);
49 	int ret = nop->result;
50 
51 	if (nop->flags & IORING_NOP_FILE) {
52 		if (nop->flags & IORING_NOP_FIXED_FILE) {
53 			req->file = io_file_get_fixed(req, nop->fd, issue_flags);
54 			req->flags |= REQ_F_FIXED_FILE;
55 		} else {
56 			req->file = io_file_get_normal(req, nop->fd);
57 		}
58 		if (!req->file) {
59 			ret = -EBADF;
60 			goto done;
61 		}
62 	}
63 	if (nop->flags & IORING_NOP_FIXED_BUFFER) {
64 		if (!io_find_buf_node(req, issue_flags))
65 			ret = -EFAULT;
66 	}
67 done:
68 	if (ret < 0)
69 		req_set_fail(req);
70 	io_req_set_res(req, nop->result, 0);
71 	return IOU_OK;
72 }
73