1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 #ifndef _LINUX_IO_URING_CMD_H
3 #define _LINUX_IO_URING_CMD_H
4
5 #include <uapi/linux/io_uring.h>
6 #include <linux/io_uring_types.h>
7
8 /* only top 8 bits of sqe->uring_cmd_flags for kernel internal use */
9 #define IORING_URING_CMD_CANCELABLE (1U << 30)
10
11 struct io_uring_cmd {
12 struct file *file;
13 const struct io_uring_sqe *sqe;
14 /* callback to defer completions to task context */
15 void (*task_work_cb)(struct io_uring_cmd *cmd, unsigned);
16 u32 cmd_op;
17 u32 flags;
18 u8 pdu[32]; /* available inline for free use */
19 };
20
io_uring_sqe_cmd(const struct io_uring_sqe * sqe)21 static inline const void *io_uring_sqe_cmd(const struct io_uring_sqe *sqe)
22 {
23 return sqe->cmd;
24 }
25
io_uring_cmd_private_sz_check(size_t cmd_sz)26 static inline void io_uring_cmd_private_sz_check(size_t cmd_sz)
27 {
28 BUILD_BUG_ON(cmd_sz > sizeof_field(struct io_uring_cmd, pdu));
29 }
30 #define io_uring_cmd_to_pdu(cmd, pdu_type) ( \
31 io_uring_cmd_private_sz_check(sizeof(pdu_type)), \
32 ((pdu_type *)&(cmd)->pdu) \
33 )
34
35 #if defined(CONFIG_IO_URING)
36 int io_uring_cmd_import_fixed(u64 ubuf, unsigned long len, int rw,
37 struct iov_iter *iter, void *ioucmd);
38
39 /*
40 * Completes the request, i.e. posts an io_uring CQE and deallocates @ioucmd
41 * and the corresponding io_uring request.
42 *
43 * Note: the caller should never hard code @issue_flags and is only allowed
44 * to pass the mask provided by the core io_uring code.
45 */
46 void io_uring_cmd_done(struct io_uring_cmd *cmd, ssize_t ret, ssize_t res2,
47 unsigned issue_flags);
48
49 void __io_uring_cmd_do_in_task(struct io_uring_cmd *ioucmd,
50 void (*task_work_cb)(struct io_uring_cmd *, unsigned),
51 unsigned flags);
52
53 /*
54 * Note: the caller should never hard code @issue_flags and only use the
55 * mask provided by the core io_uring code.
56 */
57 void io_uring_cmd_mark_cancelable(struct io_uring_cmd *cmd,
58 unsigned int issue_flags);
59
60 /* Execute the request from a blocking context */
61 void io_uring_cmd_issue_blocking(struct io_uring_cmd *ioucmd);
62
63 #else
io_uring_cmd_import_fixed(u64 ubuf,unsigned long len,int rw,struct iov_iter * iter,void * ioucmd)64 static inline int io_uring_cmd_import_fixed(u64 ubuf, unsigned long len, int rw,
65 struct iov_iter *iter, void *ioucmd)
66 {
67 return -EOPNOTSUPP;
68 }
io_uring_cmd_done(struct io_uring_cmd * cmd,ssize_t ret,ssize_t ret2,unsigned issue_flags)69 static inline void io_uring_cmd_done(struct io_uring_cmd *cmd, ssize_t ret,
70 ssize_t ret2, unsigned issue_flags)
71 {
72 }
__io_uring_cmd_do_in_task(struct io_uring_cmd * ioucmd,void (* task_work_cb)(struct io_uring_cmd *,unsigned),unsigned flags)73 static inline void __io_uring_cmd_do_in_task(struct io_uring_cmd *ioucmd,
74 void (*task_work_cb)(struct io_uring_cmd *, unsigned),
75 unsigned flags)
76 {
77 }
io_uring_cmd_mark_cancelable(struct io_uring_cmd * cmd,unsigned int issue_flags)78 static inline void io_uring_cmd_mark_cancelable(struct io_uring_cmd *cmd,
79 unsigned int issue_flags)
80 {
81 }
io_uring_cmd_issue_blocking(struct io_uring_cmd * ioucmd)82 static inline void io_uring_cmd_issue_blocking(struct io_uring_cmd *ioucmd)
83 {
84 }
85 #endif
86
87 /*
88 * Polled completions must ensure they are coming from a poll queue, and
89 * hence are completed inside the usual poll handling loops.
90 */
io_uring_cmd_iopoll_done(struct io_uring_cmd * ioucmd,ssize_t ret,ssize_t res2)91 static inline void io_uring_cmd_iopoll_done(struct io_uring_cmd *ioucmd,
92 ssize_t ret, ssize_t res2)
93 {
94 lockdep_assert(in_task());
95 io_uring_cmd_done(ioucmd, ret, res2, 0);
96 }
97
98 /* users must follow the IOU_F_TWQ_LAZY_WAKE semantics */
io_uring_cmd_do_in_task_lazy(struct io_uring_cmd * ioucmd,void (* task_work_cb)(struct io_uring_cmd *,unsigned))99 static inline void io_uring_cmd_do_in_task_lazy(struct io_uring_cmd *ioucmd,
100 void (*task_work_cb)(struct io_uring_cmd *, unsigned))
101 {
102 __io_uring_cmd_do_in_task(ioucmd, task_work_cb, IOU_F_TWQ_LAZY_WAKE);
103 }
104
io_uring_cmd_complete_in_task(struct io_uring_cmd * ioucmd,void (* task_work_cb)(struct io_uring_cmd *,unsigned))105 static inline void io_uring_cmd_complete_in_task(struct io_uring_cmd *ioucmd,
106 void (*task_work_cb)(struct io_uring_cmd *, unsigned))
107 {
108 __io_uring_cmd_do_in_task(ioucmd, task_work_cb, 0);
109 }
110
io_uring_cmd_get_task(struct io_uring_cmd * cmd)111 static inline struct task_struct *io_uring_cmd_get_task(struct io_uring_cmd *cmd)
112 {
113 return cmd_to_io_kiocb(cmd)->task;
114 }
115
116 #endif /* _LINUX_IO_URING_CMD_H */
117