1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 #ifndef _LINUX_IO_URING_H 3 #define _LINUX_IO_URING_H 4 5 #include <linux/sched.h> 6 #include <linux/xarray.h> 7 #include <uapi/linux/io_uring.h> 8 9 enum io_uring_cmd_flags { 10 IO_URING_F_COMPLETE_DEFER = 1, 11 IO_URING_F_UNLOCKED = 2, 12 /* the request is executed from poll, it should not be freed */ 13 IO_URING_F_MULTISHOT = 4, 14 /* executed by io-wq */ 15 IO_URING_F_IOWQ = 8, 16 /* int's last bit, sign checks are usually faster than a bit test */ 17 IO_URING_F_NONBLOCK = INT_MIN, 18 19 /* ctx state flags, for URING_CMD */ 20 IO_URING_F_SQE128 = (1 << 8), 21 IO_URING_F_CQE32 = (1 << 9), 22 IO_URING_F_IOPOLL = (1 << 10), 23 24 /* set when uring wants to cancel a previously issued command */ 25 IO_URING_F_CANCEL = (1 << 11), 26 IO_URING_F_COMPAT = (1 << 12), 27 }; 28 29 /* only top 8 bits of sqe->uring_cmd_flags for kernel internal use */ 30 #define IORING_URING_CMD_CANCELABLE (1U << 30) 31 32 struct io_uring_cmd { 33 struct file *file; 34 const struct io_uring_sqe *sqe; 35 /* callback to defer completions to task context */ 36 void (*task_work_cb)(struct io_uring_cmd *cmd, unsigned); 37 u32 cmd_op; 38 u32 flags; 39 u8 pdu[32]; /* available inline for free use */ 40 }; 41 42 static inline const void *io_uring_sqe_cmd(const struct io_uring_sqe *sqe) 43 { 44 return sqe->cmd; 45 } 46 47 #if defined(CONFIG_IO_URING) 48 int io_uring_cmd_import_fixed(u64 ubuf, unsigned long len, int rw, 49 struct iov_iter *iter, void *ioucmd); 50 void io_uring_cmd_done(struct io_uring_cmd *cmd, ssize_t ret, ssize_t res2, 51 unsigned issue_flags); 52 struct sock *io_uring_get_socket(struct file *file); 53 void __io_uring_cancel(bool cancel_all); 54 void __io_uring_free(struct task_struct *tsk); 55 void io_uring_unreg_ringfd(void); 56 const char *io_uring_get_opcode(u8 opcode); 57 void __io_uring_cmd_do_in_task(struct io_uring_cmd *ioucmd, 58 void (*task_work_cb)(struct io_uring_cmd *, unsigned), 59 unsigned flags); 60 /* users should follow semantics of IOU_F_TWQ_LAZY_WAKE */ 61 void io_uring_cmd_do_in_task_lazy(struct io_uring_cmd *ioucmd, 62 void (*task_work_cb)(struct io_uring_cmd *, unsigned)); 63 64 static inline void io_uring_cmd_complete_in_task(struct io_uring_cmd *ioucmd, 65 void (*task_work_cb)(struct io_uring_cmd *, unsigned)) 66 { 67 __io_uring_cmd_do_in_task(ioucmd, task_work_cb, 0); 68 } 69 70 static inline void io_uring_files_cancel(void) 71 { 72 if (current->io_uring) { 73 io_uring_unreg_ringfd(); 74 __io_uring_cancel(false); 75 } 76 } 77 static inline void io_uring_task_cancel(void) 78 { 79 if (current->io_uring) 80 __io_uring_cancel(true); 81 } 82 static inline void io_uring_free(struct task_struct *tsk) 83 { 84 if (tsk->io_uring) 85 __io_uring_free(tsk); 86 } 87 int io_uring_cmd_sock(struct io_uring_cmd *cmd, unsigned int issue_flags); 88 void io_uring_cmd_mark_cancelable(struct io_uring_cmd *cmd, 89 unsigned int issue_flags); 90 struct task_struct *io_uring_cmd_get_task(struct io_uring_cmd *cmd); 91 #else 92 static inline int io_uring_cmd_import_fixed(u64 ubuf, unsigned long len, int rw, 93 struct iov_iter *iter, void *ioucmd) 94 { 95 return -EOPNOTSUPP; 96 } 97 static inline void io_uring_cmd_done(struct io_uring_cmd *cmd, ssize_t ret, 98 ssize_t ret2, unsigned issue_flags) 99 { 100 } 101 static inline void io_uring_cmd_complete_in_task(struct io_uring_cmd *ioucmd, 102 void (*task_work_cb)(struct io_uring_cmd *, unsigned)) 103 { 104 } 105 static inline void io_uring_cmd_do_in_task_lazy(struct io_uring_cmd *ioucmd, 106 void (*task_work_cb)(struct io_uring_cmd *, unsigned)) 107 { 108 } 109 static inline struct sock *io_uring_get_socket(struct file *file) 110 { 111 return NULL; 112 } 113 static inline void io_uring_task_cancel(void) 114 { 115 } 116 static inline void io_uring_files_cancel(void) 117 { 118 } 119 static inline void io_uring_free(struct task_struct *tsk) 120 { 121 } 122 static inline const char *io_uring_get_opcode(u8 opcode) 123 { 124 return ""; 125 } 126 static inline int io_uring_cmd_sock(struct io_uring_cmd *cmd, 127 unsigned int issue_flags) 128 { 129 return -EOPNOTSUPP; 130 } 131 static inline void io_uring_cmd_mark_cancelable(struct io_uring_cmd *cmd, 132 unsigned int issue_flags) 133 { 134 } 135 static inline struct task_struct *io_uring_cmd_get_task(struct io_uring_cmd *cmd) 136 { 137 return NULL; 138 } 139 #endif 140 141 #endif 142