1 #ifndef IO_URING_TYPES_H
2 #define IO_URING_TYPES_H
3
4 #include <linux/blkdev.h>
5 #include <linux/hashtable.h>
6 #include <linux/task_work.h>
7 #include <linux/bitmap.h>
8 #include <linux/llist.h>
9 #include <uapi/linux/io_uring.h>
10
11 enum {
12 /*
13 * A hint to not wake right away but delay until there are enough of
14 * tw's queued to match the number of CQEs the task is waiting for.
15 *
16 * Must not be used with requests generating more than one CQE.
17 * It's also ignored unless IORING_SETUP_DEFER_TASKRUN is set.
18 */
19 IOU_F_TWQ_LAZY_WAKE = 1,
20 };
21
22 enum io_uring_cmd_flags {
23 IO_URING_F_COMPLETE_DEFER = 1,
24 IO_URING_F_UNLOCKED = 2,
25 /* the request is executed from poll, it should not be freed */
26 IO_URING_F_MULTISHOT = 4,
27 /* executed by io-wq */
28 IO_URING_F_IOWQ = 8,
29 /* executed inline from syscall */
30 IO_URING_F_INLINE = 16,
31 /* int's last bit, sign checks are usually faster than a bit test */
32 IO_URING_F_NONBLOCK = INT_MIN,
33
34 /* ctx state flags, for URING_CMD */
35 IO_URING_F_SQE128 = (1 << 8),
36 IO_URING_F_CQE32 = (1 << 9),
37 IO_URING_F_IOPOLL = (1 << 10),
38
39 /* set when uring wants to cancel a previously issued command */
40 IO_URING_F_CANCEL = (1 << 11),
41 IO_URING_F_COMPAT = (1 << 12),
42 IO_URING_F_TASK_DEAD = (1 << 13),
43 };
44
45 struct io_wq_work_node {
46 struct io_wq_work_node *next;
47 };
48
49 struct io_wq_work_list {
50 struct io_wq_work_node *first;
51 struct io_wq_work_node *last;
52 };
53
54 struct io_wq_work {
55 struct io_wq_work_node list;
56 atomic_t flags;
57 /* place it here instead of io_kiocb as it fills padding and saves 4B */
58 int cancel_seq;
59 };
60
61 struct io_rsrc_data {
62 unsigned int nr;
63 struct io_rsrc_node **nodes;
64 };
65
66 struct io_file_table {
67 struct io_rsrc_data data;
68 unsigned long *bitmap;
69 unsigned int alloc_hint;
70 };
71
72 struct io_hash_bucket {
73 struct hlist_head list;
74 } ____cacheline_aligned_in_smp;
75
76 struct io_hash_table {
77 struct io_hash_bucket *hbs;
78 unsigned hash_bits;
79 };
80
81 struct io_mapped_region {
82 struct page **pages;
83 void *ptr;
84 unsigned nr_pages;
85 unsigned flags;
86 };
87
88 /*
89 * Arbitrary limit, can be raised if need be
90 */
91 #define IO_RINGFD_REG_MAX 16
92
93 struct io_uring_task {
94 /* submission side */
95 int cached_refs;
96 const struct io_ring_ctx *last;
97 struct task_struct *task;
98 struct io_wq *io_wq;
99 struct file *registered_rings[IO_RINGFD_REG_MAX];
100
101 struct xarray xa;
102 struct wait_queue_head wait;
103 atomic_t in_cancel;
104 atomic_t inflight_tracked;
105 struct percpu_counter inflight;
106
107 struct { /* task_work */
108 struct llist_head task_list;
109 struct callback_head task_work;
110 } ____cacheline_aligned_in_smp;
111 };
112
113 struct iou_vec {
114 union {
115 struct iovec *iovec;
116 struct bio_vec *bvec;
117 };
118 unsigned nr; /* number of struct iovec it can hold */
119 };
120
121 struct io_uring {
122 u32 head;
123 u32 tail;
124 };
125
126 /*
127 * This data is shared with the application through the mmap at offsets
128 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
129 *
130 * The offsets to the member fields are published through struct
131 * io_sqring_offsets when calling io_uring_setup.
132 */
133 struct io_rings {
134 /*
135 * Head and tail offsets into the ring; the offsets need to be
136 * masked to get valid indices.
137 *
138 * The kernel controls head of the sq ring and the tail of the cq ring,
139 * and the application controls tail of the sq ring and the head of the
140 * cq ring.
141 */
142 struct io_uring sq, cq;
143 /*
144 * Bitmasks to apply to head and tail offsets (constant, equals
145 * ring_entries - 1)
146 */
147 u32 sq_ring_mask, cq_ring_mask;
148 /* Ring sizes (constant, power of 2) */
149 u32 sq_ring_entries, cq_ring_entries;
150 /*
151 * Number of invalid entries dropped by the kernel due to
152 * invalid index stored in array
153 *
154 * Written by the kernel, shouldn't be modified by the
155 * application (i.e. get number of "new events" by comparing to
156 * cached value).
157 *
158 * After a new SQ head value was read by the application this
159 * counter includes all submissions that were dropped reaching
160 * the new SQ head (and possibly more).
161 */
162 u32 sq_dropped;
163 /*
164 * Runtime SQ flags
165 *
166 * Written by the kernel, shouldn't be modified by the
167 * application.
168 *
169 * The application needs a full memory barrier before checking
170 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
171 */
172 atomic_t sq_flags;
173 /*
174 * Runtime CQ flags
175 *
176 * Written by the application, shouldn't be modified by the
177 * kernel.
178 */
179 u32 cq_flags;
180 /*
181 * Number of completion events lost because the queue was full;
182 * this should be avoided by the application by making sure
183 * there are not more requests pending than there is space in
184 * the completion queue.
185 *
186 * Written by the kernel, shouldn't be modified by the
187 * application (i.e. get number of "new events" by comparing to
188 * cached value).
189 *
190 * As completion events come in out of order this counter is not
191 * ordered with any other data.
192 */
193 u32 cq_overflow;
194 /*
195 * Ring buffer of completion events.
196 *
197 * The kernel writes completion events fresh every time they are
198 * produced, so the application is allowed to modify pending
199 * entries.
200 */
201 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
202 };
203
204 struct io_restriction {
205 DECLARE_BITMAP(register_op, IORING_REGISTER_LAST);
206 DECLARE_BITMAP(sqe_op, IORING_OP_LAST);
207 u8 sqe_flags_allowed;
208 u8 sqe_flags_required;
209 bool registered;
210 };
211
212 struct io_submit_link {
213 struct io_kiocb *head;
214 struct io_kiocb *last;
215 };
216
217 struct io_submit_state {
218 /* inline/task_work completion list, under ->uring_lock */
219 struct io_wq_work_node free_list;
220 /* batch completion logic */
221 struct io_wq_work_list compl_reqs;
222 struct io_submit_link link;
223
224 bool plug_started;
225 bool need_plug;
226 bool cq_flush;
227 unsigned short submit_nr;
228 struct blk_plug plug;
229 };
230
231 struct io_alloc_cache {
232 void **entries;
233 unsigned int nr_cached;
234 unsigned int max_cached;
235 unsigned int elem_size;
236 unsigned int init_clear;
237 };
238
239 struct io_ring_ctx {
240 /* const or read-mostly hot data */
241 struct {
242 unsigned int flags;
243 unsigned int drain_next: 1;
244 unsigned int restricted: 1;
245 unsigned int off_timeout_used: 1;
246 unsigned int drain_active: 1;
247 unsigned int has_evfd: 1;
248 /* all CQEs should be posted only by the submitter task */
249 unsigned int task_complete: 1;
250 unsigned int lockless_cq: 1;
251 unsigned int syscall_iopoll: 1;
252 unsigned int poll_activated: 1;
253 unsigned int drain_disabled: 1;
254 unsigned int compat: 1;
255 unsigned int iowq_limits_set : 1;
256
257 struct task_struct *submitter_task;
258 struct io_rings *rings;
259 struct percpu_ref refs;
260
261 clockid_t clockid;
262 enum tk_offsets clock_offset;
263
264 enum task_work_notify_mode notify_method;
265 unsigned sq_thread_idle;
266 } ____cacheline_aligned_in_smp;
267
268 /* submission data */
269 struct {
270 struct mutex uring_lock;
271
272 /*
273 * Ring buffer of indices into array of io_uring_sqe, which is
274 * mmapped by the application using the IORING_OFF_SQES offset.
275 *
276 * This indirection could e.g. be used to assign fixed
277 * io_uring_sqe entries to operations and only submit them to
278 * the queue when needed.
279 *
280 * The kernel modifies neither the indices array nor the entries
281 * array.
282 */
283 u32 *sq_array;
284 struct io_uring_sqe *sq_sqes;
285 unsigned cached_sq_head;
286 unsigned sq_entries;
287
288 /*
289 * Fixed resources fast path, should be accessed only under
290 * uring_lock, and updated through io_uring_register(2)
291 */
292 atomic_t cancel_seq;
293
294 /*
295 * ->iopoll_list is protected by the ctx->uring_lock for
296 * io_uring instances that don't use IORING_SETUP_SQPOLL.
297 * For SQPOLL, only the single threaded io_sq_thread() will
298 * manipulate the list, hence no extra locking is needed there.
299 */
300 bool poll_multi_queue;
301 struct io_wq_work_list iopoll_list;
302
303 struct io_file_table file_table;
304 struct io_rsrc_data buf_table;
305 struct io_alloc_cache node_cache;
306 struct io_alloc_cache imu_cache;
307
308 struct io_submit_state submit_state;
309
310 /*
311 * Modifications are protected by ->uring_lock and ->mmap_lock.
312 * The flags, buf_pages and buf_nr_pages fields should be stable
313 * once published.
314 */
315 struct xarray io_bl_xa;
316
317 struct io_hash_table cancel_table;
318 struct io_alloc_cache apoll_cache;
319 struct io_alloc_cache netmsg_cache;
320 struct io_alloc_cache rw_cache;
321 struct io_alloc_cache cmd_cache;
322
323 /*
324 * Any cancelable uring_cmd is added to this list in
325 * ->uring_cmd() by io_uring_cmd_insert_cancelable()
326 */
327 struct hlist_head cancelable_uring_cmd;
328 /*
329 * For Hybrid IOPOLL, runtime in hybrid polling, without
330 * scheduling time
331 */
332 u64 hybrid_poll_time;
333 } ____cacheline_aligned_in_smp;
334
335 struct {
336 /*
337 * We cache a range of free CQEs we can use, once exhausted it
338 * should go through a slower range setup, see __io_get_cqe()
339 */
340 struct io_uring_cqe *cqe_cached;
341 struct io_uring_cqe *cqe_sentinel;
342
343 unsigned cached_cq_tail;
344 unsigned cq_entries;
345 struct io_ev_fd __rcu *io_ev_fd;
346
347 void *cq_wait_arg;
348 size_t cq_wait_size;
349 } ____cacheline_aligned_in_smp;
350
351 /*
352 * task_work and async notification delivery cacheline. Expected to
353 * regularly bounce b/w CPUs.
354 */
355 struct {
356 struct llist_head work_llist;
357 struct llist_head retry_llist;
358 unsigned long check_cq;
359 atomic_t cq_wait_nr;
360 atomic_t cq_timeouts;
361 struct wait_queue_head cq_wait;
362 } ____cacheline_aligned_in_smp;
363
364 /* timeouts */
365 struct {
366 raw_spinlock_t timeout_lock;
367 struct list_head timeout_list;
368 struct list_head ltimeout_list;
369 unsigned cq_last_tm_flush;
370 } ____cacheline_aligned_in_smp;
371
372 spinlock_t completion_lock;
373
374 struct list_head cq_overflow_list;
375
376 struct hlist_head waitid_list;
377
378 #ifdef CONFIG_FUTEX
379 struct hlist_head futex_list;
380 struct io_alloc_cache futex_cache;
381 #endif
382
383 const struct cred *sq_creds; /* cred used for __io_sq_thread() */
384 struct io_sq_data *sq_data; /* if using sq thread polling */
385
386 struct wait_queue_head sqo_sq_wait;
387 struct list_head sqd_list;
388
389 unsigned int file_alloc_start;
390 unsigned int file_alloc_end;
391
392 /* Keep this last, we don't need it for the fast path */
393 struct wait_queue_head poll_wq;
394 struct io_restriction restrictions;
395
396 /* Stores zcrx object pointers of type struct io_zcrx_ifq */
397 struct xarray zcrx_ctxs;
398
399 u32 pers_next;
400 struct xarray personalities;
401
402 /* hashed buffered write serialization */
403 struct io_wq_hash *hash_map;
404
405 /* Only used for accounting purposes */
406 struct user_struct *user;
407 struct mm_struct *mm_account;
408
409 /* ctx exit and cancelation */
410 struct llist_head fallback_llist;
411 struct delayed_work fallback_work;
412 struct work_struct exit_work;
413 struct list_head tctx_list;
414 struct completion ref_comp;
415
416 /* io-wq management, e.g. thread count */
417 u32 iowq_limits[2];
418
419 struct callback_head poll_wq_task_work;
420 struct list_head defer_list;
421 unsigned nr_drained;
422
423 struct io_alloc_cache msg_cache;
424 spinlock_t msg_lock;
425
426 #ifdef CONFIG_NET_RX_BUSY_POLL
427 struct list_head napi_list; /* track busy poll napi_id */
428 spinlock_t napi_lock; /* napi_list lock */
429
430 /* napi busy poll default timeout */
431 ktime_t napi_busy_poll_dt;
432 bool napi_prefer_busy_poll;
433 u8 napi_track_mode;
434
435 DECLARE_HASHTABLE(napi_ht, 4);
436 #endif
437
438 /* protected by ->completion_lock */
439 unsigned evfd_last_cq_tail;
440 unsigned nr_req_allocated;
441
442 /*
443 * Protection for resize vs mmap races - both the mmap and resize
444 * side will need to grab this lock, to prevent either side from
445 * being run concurrently with the other.
446 */
447 struct mutex mmap_lock;
448
449 struct io_mapped_region sq_region;
450 struct io_mapped_region ring_region;
451 /* used for optimised request parameter and wait argument passing */
452 struct io_mapped_region param_region;
453 };
454
455 /*
456 * Token indicating function is called in task work context:
457 * ctx->uring_lock is held and any completions generated will be flushed.
458 * ONLY core io_uring.c should instantiate this struct.
459 */
460 struct io_tw_state {
461 };
462 /* Alias to use in code that doesn't instantiate struct io_tw_state */
463 typedef struct io_tw_state io_tw_token_t;
464
465 enum {
466 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
467 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
468 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
469 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
470 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
471 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
472 REQ_F_CQE_SKIP_BIT = IOSQE_CQE_SKIP_SUCCESS_BIT,
473
474 /* first byte is taken by user flags, shift it to not overlap */
475 REQ_F_FAIL_BIT = 8,
476 REQ_F_INFLIGHT_BIT,
477 REQ_F_CUR_POS_BIT,
478 REQ_F_NOWAIT_BIT,
479 REQ_F_LINK_TIMEOUT_BIT,
480 REQ_F_NEED_CLEANUP_BIT,
481 REQ_F_POLLED_BIT,
482 REQ_F_HYBRID_IOPOLL_STATE_BIT,
483 REQ_F_BUFFER_SELECTED_BIT,
484 REQ_F_BUFFER_RING_BIT,
485 REQ_F_REISSUE_BIT,
486 REQ_F_CREDS_BIT,
487 REQ_F_REFCOUNT_BIT,
488 REQ_F_ARM_LTIMEOUT_BIT,
489 REQ_F_ASYNC_DATA_BIT,
490 REQ_F_SKIP_LINK_CQES_BIT,
491 REQ_F_SINGLE_POLL_BIT,
492 REQ_F_DOUBLE_POLL_BIT,
493 REQ_F_MULTISHOT_BIT,
494 REQ_F_APOLL_MULTISHOT_BIT,
495 REQ_F_CLEAR_POLLIN_BIT,
496 /* keep async read/write and isreg together and in order */
497 REQ_F_SUPPORT_NOWAIT_BIT,
498 REQ_F_ISREG_BIT,
499 REQ_F_POLL_NO_LAZY_BIT,
500 REQ_F_CAN_POLL_BIT,
501 REQ_F_BL_EMPTY_BIT,
502 REQ_F_BL_NO_RECYCLE_BIT,
503 REQ_F_BUFFERS_COMMIT_BIT,
504 REQ_F_BUF_NODE_BIT,
505 REQ_F_HAS_METADATA_BIT,
506 REQ_F_IMPORT_BUFFER_BIT,
507 REQ_F_SQE_COPIED_BIT,
508
509 /* not a real bit, just to check we're not overflowing the space */
510 __REQ_F_LAST_BIT,
511 };
512
513 typedef u64 __bitwise io_req_flags_t;
514 #define IO_REQ_FLAG(bitno) ((__force io_req_flags_t) BIT_ULL((bitno)))
515
516 enum {
517 /* ctx owns file */
518 REQ_F_FIXED_FILE = IO_REQ_FLAG(REQ_F_FIXED_FILE_BIT),
519 /* drain existing IO first */
520 REQ_F_IO_DRAIN = IO_REQ_FLAG(REQ_F_IO_DRAIN_BIT),
521 /* linked sqes */
522 REQ_F_LINK = IO_REQ_FLAG(REQ_F_LINK_BIT),
523 /* doesn't sever on completion < 0 */
524 REQ_F_HARDLINK = IO_REQ_FLAG(REQ_F_HARDLINK_BIT),
525 /* IOSQE_ASYNC */
526 REQ_F_FORCE_ASYNC = IO_REQ_FLAG(REQ_F_FORCE_ASYNC_BIT),
527 /* IOSQE_BUFFER_SELECT */
528 REQ_F_BUFFER_SELECT = IO_REQ_FLAG(REQ_F_BUFFER_SELECT_BIT),
529 /* IOSQE_CQE_SKIP_SUCCESS */
530 REQ_F_CQE_SKIP = IO_REQ_FLAG(REQ_F_CQE_SKIP_BIT),
531
532 /* fail rest of links */
533 REQ_F_FAIL = IO_REQ_FLAG(REQ_F_FAIL_BIT),
534 /* on inflight list, should be cancelled and waited on exit reliably */
535 REQ_F_INFLIGHT = IO_REQ_FLAG(REQ_F_INFLIGHT_BIT),
536 /* read/write uses file position */
537 REQ_F_CUR_POS = IO_REQ_FLAG(REQ_F_CUR_POS_BIT),
538 /* must not punt to workers */
539 REQ_F_NOWAIT = IO_REQ_FLAG(REQ_F_NOWAIT_BIT),
540 /* has or had linked timeout */
541 REQ_F_LINK_TIMEOUT = IO_REQ_FLAG(REQ_F_LINK_TIMEOUT_BIT),
542 /* needs cleanup */
543 REQ_F_NEED_CLEANUP = IO_REQ_FLAG(REQ_F_NEED_CLEANUP_BIT),
544 /* already went through poll handler */
545 REQ_F_POLLED = IO_REQ_FLAG(REQ_F_POLLED_BIT),
546 /* every req only blocks once in hybrid poll */
547 REQ_F_IOPOLL_STATE = IO_REQ_FLAG(REQ_F_HYBRID_IOPOLL_STATE_BIT),
548 /* buffer already selected */
549 REQ_F_BUFFER_SELECTED = IO_REQ_FLAG(REQ_F_BUFFER_SELECTED_BIT),
550 /* buffer selected from ring, needs commit */
551 REQ_F_BUFFER_RING = IO_REQ_FLAG(REQ_F_BUFFER_RING_BIT),
552 /* caller should reissue async */
553 REQ_F_REISSUE = IO_REQ_FLAG(REQ_F_REISSUE_BIT),
554 /* supports async reads/writes */
555 REQ_F_SUPPORT_NOWAIT = IO_REQ_FLAG(REQ_F_SUPPORT_NOWAIT_BIT),
556 /* regular file */
557 REQ_F_ISREG = IO_REQ_FLAG(REQ_F_ISREG_BIT),
558 /* has creds assigned */
559 REQ_F_CREDS = IO_REQ_FLAG(REQ_F_CREDS_BIT),
560 /* skip refcounting if not set */
561 REQ_F_REFCOUNT = IO_REQ_FLAG(REQ_F_REFCOUNT_BIT),
562 /* there is a linked timeout that has to be armed */
563 REQ_F_ARM_LTIMEOUT = IO_REQ_FLAG(REQ_F_ARM_LTIMEOUT_BIT),
564 /* ->async_data allocated */
565 REQ_F_ASYNC_DATA = IO_REQ_FLAG(REQ_F_ASYNC_DATA_BIT),
566 /* don't post CQEs while failing linked requests */
567 REQ_F_SKIP_LINK_CQES = IO_REQ_FLAG(REQ_F_SKIP_LINK_CQES_BIT),
568 /* single poll may be active */
569 REQ_F_SINGLE_POLL = IO_REQ_FLAG(REQ_F_SINGLE_POLL_BIT),
570 /* double poll may active */
571 REQ_F_DOUBLE_POLL = IO_REQ_FLAG(REQ_F_DOUBLE_POLL_BIT),
572 /* request posts multiple completions, should be set at prep time */
573 REQ_F_MULTISHOT = IO_REQ_FLAG(REQ_F_MULTISHOT_BIT),
574 /* fast poll multishot mode */
575 REQ_F_APOLL_MULTISHOT = IO_REQ_FLAG(REQ_F_APOLL_MULTISHOT_BIT),
576 /* recvmsg special flag, clear EPOLLIN */
577 REQ_F_CLEAR_POLLIN = IO_REQ_FLAG(REQ_F_CLEAR_POLLIN_BIT),
578 /* don't use lazy poll wake for this request */
579 REQ_F_POLL_NO_LAZY = IO_REQ_FLAG(REQ_F_POLL_NO_LAZY_BIT),
580 /* file is pollable */
581 REQ_F_CAN_POLL = IO_REQ_FLAG(REQ_F_CAN_POLL_BIT),
582 /* buffer list was empty after selection of buffer */
583 REQ_F_BL_EMPTY = IO_REQ_FLAG(REQ_F_BL_EMPTY_BIT),
584 /* don't recycle provided buffers for this request */
585 REQ_F_BL_NO_RECYCLE = IO_REQ_FLAG(REQ_F_BL_NO_RECYCLE_BIT),
586 /* buffer ring head needs incrementing on put */
587 REQ_F_BUFFERS_COMMIT = IO_REQ_FLAG(REQ_F_BUFFERS_COMMIT_BIT),
588 /* buf node is valid */
589 REQ_F_BUF_NODE = IO_REQ_FLAG(REQ_F_BUF_NODE_BIT),
590 /* request has read/write metadata assigned */
591 REQ_F_HAS_METADATA = IO_REQ_FLAG(REQ_F_HAS_METADATA_BIT),
592 /*
593 * For vectored fixed buffers, resolve iovec to registered buffers.
594 * For SEND_ZC, whether to import buffers (i.e. the first issue).
595 */
596 REQ_F_IMPORT_BUFFER = IO_REQ_FLAG(REQ_F_IMPORT_BUFFER_BIT),
597 /* ->sqe_copy() has been called, if necessary */
598 REQ_F_SQE_COPIED = IO_REQ_FLAG(REQ_F_SQE_COPIED_BIT),
599 };
600
601 typedef void (*io_req_tw_func_t)(struct io_kiocb *req, io_tw_token_t tw);
602
603 struct io_task_work {
604 struct llist_node node;
605 io_req_tw_func_t func;
606 };
607
608 struct io_cqe {
609 __u64 user_data;
610 __s32 res;
611 /* fd initially, then cflags for completion */
612 union {
613 __u32 flags;
614 int fd;
615 };
616 };
617
618 /*
619 * Each request type overlays its private data structure on top of this one.
620 * They must not exceed this one in size.
621 */
622 struct io_cmd_data {
623 struct file *file;
624 /* each command gets 56 bytes of data */
625 __u8 data[56];
626 };
627
io_kiocb_cmd_sz_check(size_t cmd_sz)628 static inline void io_kiocb_cmd_sz_check(size_t cmd_sz)
629 {
630 BUILD_BUG_ON(cmd_sz > sizeof(struct io_cmd_data));
631 }
632 #define io_kiocb_to_cmd(req, cmd_type) ( \
633 io_kiocb_cmd_sz_check(sizeof(cmd_type)) , \
634 ((cmd_type *)&(req)->cmd) \
635 )
636
cmd_to_io_kiocb(void * ptr)637 static inline struct io_kiocb *cmd_to_io_kiocb(void *ptr)
638 {
639 return ptr;
640 }
641
642 struct io_kiocb {
643 union {
644 /*
645 * NOTE! Each of the io_kiocb union members has the file pointer
646 * as the first entry in their struct definition. So you can
647 * access the file pointer through any of the sub-structs,
648 * or directly as just 'file' in this struct.
649 */
650 struct file *file;
651 struct io_cmd_data cmd;
652 };
653
654 u8 opcode;
655 /* polled IO has completed */
656 u8 iopoll_completed;
657 /*
658 * Can be either a fixed buffer index, or used with provided buffers.
659 * For the latter, it points to the selected buffer ID.
660 */
661 u16 buf_index;
662
663 unsigned nr_tw;
664
665 /* REQ_F_* flags */
666 io_req_flags_t flags;
667
668 struct io_cqe cqe;
669
670 struct io_ring_ctx *ctx;
671 struct io_uring_task *tctx;
672
673 union {
674 /* stores selected buf, valid IFF REQ_F_BUFFER_SELECTED is set */
675 struct io_buffer *kbuf;
676
677 /*
678 * stores buffer ID for ring provided buffers, valid IFF
679 * REQ_F_BUFFER_RING is set.
680 */
681 struct io_buffer_list *buf_list;
682
683 struct io_rsrc_node *buf_node;
684 };
685
686 union {
687 /* used by request caches, completion batching and iopoll */
688 struct io_wq_work_node comp_list;
689 /* cache ->apoll->events */
690 __poll_t apoll_events;
691 };
692
693 struct io_rsrc_node *file_node;
694
695 atomic_t refs;
696 bool cancel_seq_set;
697 struct io_task_work io_task_work;
698 union {
699 /*
700 * for polled requests, i.e. IORING_OP_POLL_ADD and async armed
701 * poll
702 */
703 struct hlist_node hash_node;
704 /* For IOPOLL setup queues, with hybrid polling */
705 u64 iopoll_start;
706 /* for private io_kiocb freeing */
707 struct rcu_head rcu_head;
708 };
709 /* internal polling, see IORING_FEAT_FAST_POLL */
710 struct async_poll *apoll;
711 /* opcode allocated if it needs to store data for async defer */
712 void *async_data;
713 /* linked requests, IFF REQ_F_HARDLINK or REQ_F_LINK are set */
714 atomic_t poll_refs;
715 struct io_kiocb *link;
716 /* custom credentials, valid IFF REQ_F_CREDS is set */
717 const struct cred *creds;
718 struct io_wq_work work;
719
720 struct io_big_cqe {
721 u64 extra1;
722 u64 extra2;
723 } big_cqe;
724 };
725
726 struct io_overflow_cqe {
727 struct list_head list;
728 struct io_uring_cqe cqe;
729 };
730
io_ctx_cqe32(struct io_ring_ctx * ctx)731 static inline bool io_ctx_cqe32(struct io_ring_ctx *ctx)
732 {
733 return ctx->flags & IORING_SETUP_CQE32;
734 }
735
736 #endif
737