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);
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, 0, ret);
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);
61 cmd_size = uring_cmd_pdu_size(req->ctx->flags & IORING_SETUP_SQE128);
63 memcpy(req->async_data, ioucmd->cmd, cmd_size);
67 int io_uring_cmd_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
69 struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req);
71 if (sqe->rw_flags || sqe->__pad1)
73 ioucmd->cmd = sqe->cmd;
74 ioucmd->cmd_op = READ_ONCE(sqe->cmd_op);
78 int io_uring_cmd(struct io_kiocb *req, unsigned int issue_flags)
80 struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req);
81 struct io_ring_ctx *ctx = req->ctx;
82 struct file *file = req->file;
85 if (!req->file->f_op->uring_cmd)
88 if (ctx->flags & IORING_SETUP_SQE128)
89 issue_flags |= IO_URING_F_SQE128;
90 if (ctx->flags & IORING_SETUP_CQE32)
91 issue_flags |= IO_URING_F_CQE32;
92 if (ctx->flags & IORING_SETUP_IOPOLL)
93 issue_flags |= IO_URING_F_IOPOLL;
95 if (req_has_async_data(req))
96 ioucmd->cmd = req->async_data;
98 ret = file->f_op->uring_cmd(ioucmd, issue_flags);
100 if (!req_has_async_data(req)) {
101 if (io_alloc_async_data(req))
103 io_uring_cmd_prep_async(req);
108 if (ret != -EIOCBQUEUED) {
109 io_uring_cmd_done(ioucmd, ret, 0);
113 return IOU_ISSUE_SKIP_COMPLETE;