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