1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/kernel.h>
3 #include <linux/errno.h>
4 #include <linux/file.h>
5 #include <linux/io_uring.h>
7 #include <uapi/linux/io_uring.h>
10 #include "uring_cmd.h"
12 static void io_uring_cmd_work(struct io_kiocb *req, bool *locked)
14 struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd);
16 ioucmd->task_work_cb(ioucmd);
19 void io_uring_cmd_complete_in_task(struct io_uring_cmd *ioucmd,
20 void (*task_work_cb)(struct io_uring_cmd *))
22 struct io_kiocb *req = cmd_to_io_kiocb(ioucmd);
24 ioucmd->task_work_cb = task_work_cb;
25 req->io_task_work.func = io_uring_cmd_work;
26 io_req_task_work_add(req);
28 EXPORT_SYMBOL_GPL(io_uring_cmd_complete_in_task);
30 static inline void io_req_set_cqe32_extra(struct io_kiocb *req,
31 u64 extra1, u64 extra2)
35 req->flags |= REQ_F_CQE32_INIT;
39 * Called by consumers of io_uring_cmd, if they originally returned
40 * -EIOCBQUEUED upon receiving the command.
42 void io_uring_cmd_done(struct io_uring_cmd *ioucmd, ssize_t ret, ssize_t res2)
44 struct io_kiocb *req = cmd_to_io_kiocb(ioucmd);
49 io_req_set_res(req, ret, 0);
50 if (req->ctx->flags & IORING_SETUP_CQE32)
51 io_req_set_cqe32_extra(req, res2, 0);
52 __io_req_complete(req, 0);
54 EXPORT_SYMBOL_GPL(io_uring_cmd_done);
56 int io_uring_cmd_prep_async(struct io_kiocb *req)
58 struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd);
61 BUILD_BUG_ON(uring_cmd_pdu_size(0) != 16);
62 BUILD_BUG_ON(uring_cmd_pdu_size(1) != 80);
64 cmd_size = uring_cmd_pdu_size(req->ctx->flags & IORING_SETUP_SQE128);
66 memcpy(req->async_data, ioucmd->cmd, cmd_size);
70 int io_uring_cmd_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
72 struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd);
74 if (sqe->rw_flags || sqe->__pad1)
76 ioucmd->cmd = sqe->cmd;
77 ioucmd->cmd_op = READ_ONCE(sqe->cmd_op);
81 int io_uring_cmd(struct io_kiocb *req, unsigned int issue_flags)
83 struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd);
84 struct io_ring_ctx *ctx = req->ctx;
85 struct file *file = req->file;
88 if (!req->file->f_op->uring_cmd)
91 if (ctx->flags & IORING_SETUP_SQE128)
92 issue_flags |= IO_URING_F_SQE128;
93 if (ctx->flags & IORING_SETUP_CQE32)
94 issue_flags |= IO_URING_F_CQE32;
95 if (ctx->flags & IORING_SETUP_IOPOLL)
96 issue_flags |= IO_URING_F_IOPOLL;
98 if (req_has_async_data(req))
99 ioucmd->cmd = req->async_data;
101 ret = file->f_op->uring_cmd(ioucmd, issue_flags);
102 if (ret == -EAGAIN) {
103 if (!req_has_async_data(req)) {
104 if (io_alloc_async_data(req))
106 io_uring_cmd_prep_async(req);
111 if (ret != -EIOCBQUEUED) {
114 io_req_set_res(req, ret, 0);
118 return IOU_ISSUE_SKIP_COMPLETE;