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 	/* int's last bit, sign checks are usually faster than a bit test */
30 	IO_URING_F_NONBLOCK		= INT_MIN,
31 
32 	/* ctx state flags, for URING_CMD */
33 	IO_URING_F_SQE128		= (1 << 8),
34 	IO_URING_F_CQE32		= (1 << 9),
35 	IO_URING_F_IOPOLL		= (1 << 10),
36 
37 	/* set when uring wants to cancel a previously issued command */
38 	IO_URING_F_CANCEL		= (1 << 11),
39 	IO_URING_F_COMPAT		= (1 << 12),
40 	IO_URING_F_TASK_DEAD		= (1 << 13),
41 };
42 
43 struct io_zcrx_ifq;
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 		unsigned		cq_extra;
347 
348 		void			*cq_wait_arg;
349 		size_t			cq_wait_size;
350 	} ____cacheline_aligned_in_smp;
351 
352 	/*
353 	 * task_work and async notification delivery cacheline. Expected to
354 	 * regularly bounce b/w CPUs.
355 	 */
356 	struct {
357 		struct llist_head	work_llist;
358 		struct llist_head	retry_llist;
359 		unsigned long		check_cq;
360 		atomic_t		cq_wait_nr;
361 		atomic_t		cq_timeouts;
362 		struct wait_queue_head	cq_wait;
363 	} ____cacheline_aligned_in_smp;
364 
365 	/* timeouts */
366 	struct {
367 		raw_spinlock_t		timeout_lock;
368 		struct list_head	timeout_list;
369 		struct list_head	ltimeout_list;
370 		unsigned		cq_last_tm_flush;
371 	} ____cacheline_aligned_in_smp;
372 
373 	spinlock_t		completion_lock;
374 
375 	struct list_head	cq_overflow_list;
376 
377 	struct hlist_head	waitid_list;
378 
379 #ifdef CONFIG_FUTEX
380 	struct hlist_head	futex_list;
381 	struct io_alloc_cache	futex_cache;
382 #endif
383 
384 	const struct cred	*sq_creds;	/* cred used for __io_sq_thread() */
385 	struct io_sq_data	*sq_data;	/* if using sq thread polling */
386 
387 	struct wait_queue_head	sqo_sq_wait;
388 	struct list_head	sqd_list;
389 
390 	unsigned int		file_alloc_start;
391 	unsigned int		file_alloc_end;
392 
393 	/* Keep this last, we don't need it for the fast path */
394 	struct wait_queue_head		poll_wq;
395 	struct io_restriction		restrictions;
396 
397 	struct io_zcrx_ifq		*ifq;
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 
422 	struct io_alloc_cache		msg_cache;
423 	spinlock_t			msg_lock;
424 
425 #ifdef CONFIG_NET_RX_BUSY_POLL
426 	struct list_head	napi_list;	/* track busy poll napi_id */
427 	spinlock_t		napi_lock;	/* napi_list lock */
428 
429 	/* napi busy poll default timeout */
430 	ktime_t			napi_busy_poll_dt;
431 	bool			napi_prefer_busy_poll;
432 	u8			napi_track_mode;
433 
434 	DECLARE_HASHTABLE(napi_ht, 4);
435 #endif
436 
437 	/* protected by ->completion_lock */
438 	unsigned			evfd_last_cq_tail;
439 
440 	/*
441 	 * Protection for resize vs mmap races - both the mmap and resize
442 	 * side will need to grab this lock, to prevent either side from
443 	 * being run concurrently with the other.
444 	 */
445 	struct mutex			mmap_lock;
446 
447 	struct io_mapped_region		sq_region;
448 	struct io_mapped_region		ring_region;
449 	/* used for optimised request parameter and wait argument passing  */
450 	struct io_mapped_region		param_region;
451 	/* just one zcrx per ring for now, will move to io_zcrx_ifq eventually */
452 	struct io_mapped_region		zcrx_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 
508 	/* not a real bit, just to check we're not overflowing the space */
509 	__REQ_F_LAST_BIT,
510 };
511 
512 typedef u64 __bitwise io_req_flags_t;
513 #define IO_REQ_FLAG(bitno)	((__force io_req_flags_t) BIT_ULL((bitno)))
514 
515 enum {
516 	/* ctx owns file */
517 	REQ_F_FIXED_FILE	= IO_REQ_FLAG(REQ_F_FIXED_FILE_BIT),
518 	/* drain existing IO first */
519 	REQ_F_IO_DRAIN		= IO_REQ_FLAG(REQ_F_IO_DRAIN_BIT),
520 	/* linked sqes */
521 	REQ_F_LINK		= IO_REQ_FLAG(REQ_F_LINK_BIT),
522 	/* doesn't sever on completion < 0 */
523 	REQ_F_HARDLINK		= IO_REQ_FLAG(REQ_F_HARDLINK_BIT),
524 	/* IOSQE_ASYNC */
525 	REQ_F_FORCE_ASYNC	= IO_REQ_FLAG(REQ_F_FORCE_ASYNC_BIT),
526 	/* IOSQE_BUFFER_SELECT */
527 	REQ_F_BUFFER_SELECT	= IO_REQ_FLAG(REQ_F_BUFFER_SELECT_BIT),
528 	/* IOSQE_CQE_SKIP_SUCCESS */
529 	REQ_F_CQE_SKIP		= IO_REQ_FLAG(REQ_F_CQE_SKIP_BIT),
530 
531 	/* fail rest of links */
532 	REQ_F_FAIL		= IO_REQ_FLAG(REQ_F_FAIL_BIT),
533 	/* on inflight list, should be cancelled and waited on exit reliably */
534 	REQ_F_INFLIGHT		= IO_REQ_FLAG(REQ_F_INFLIGHT_BIT),
535 	/* read/write uses file position */
536 	REQ_F_CUR_POS		= IO_REQ_FLAG(REQ_F_CUR_POS_BIT),
537 	/* must not punt to workers */
538 	REQ_F_NOWAIT		= IO_REQ_FLAG(REQ_F_NOWAIT_BIT),
539 	/* has or had linked timeout */
540 	REQ_F_LINK_TIMEOUT	= IO_REQ_FLAG(REQ_F_LINK_TIMEOUT_BIT),
541 	/* needs cleanup */
542 	REQ_F_NEED_CLEANUP	= IO_REQ_FLAG(REQ_F_NEED_CLEANUP_BIT),
543 	/* already went through poll handler */
544 	REQ_F_POLLED		= IO_REQ_FLAG(REQ_F_POLLED_BIT),
545 	/* every req only blocks once in hybrid poll */
546 	REQ_F_IOPOLL_STATE        = IO_REQ_FLAG(REQ_F_HYBRID_IOPOLL_STATE_BIT),
547 	/* buffer already selected */
548 	REQ_F_BUFFER_SELECTED	= IO_REQ_FLAG(REQ_F_BUFFER_SELECTED_BIT),
549 	/* buffer selected from ring, needs commit */
550 	REQ_F_BUFFER_RING	= IO_REQ_FLAG(REQ_F_BUFFER_RING_BIT),
551 	/* caller should reissue async */
552 	REQ_F_REISSUE		= IO_REQ_FLAG(REQ_F_REISSUE_BIT),
553 	/* supports async reads/writes */
554 	REQ_F_SUPPORT_NOWAIT	= IO_REQ_FLAG(REQ_F_SUPPORT_NOWAIT_BIT),
555 	/* regular file */
556 	REQ_F_ISREG		= IO_REQ_FLAG(REQ_F_ISREG_BIT),
557 	/* has creds assigned */
558 	REQ_F_CREDS		= IO_REQ_FLAG(REQ_F_CREDS_BIT),
559 	/* skip refcounting if not set */
560 	REQ_F_REFCOUNT		= IO_REQ_FLAG(REQ_F_REFCOUNT_BIT),
561 	/* there is a linked timeout that has to be armed */
562 	REQ_F_ARM_LTIMEOUT	= IO_REQ_FLAG(REQ_F_ARM_LTIMEOUT_BIT),
563 	/* ->async_data allocated */
564 	REQ_F_ASYNC_DATA	= IO_REQ_FLAG(REQ_F_ASYNC_DATA_BIT),
565 	/* don't post CQEs while failing linked requests */
566 	REQ_F_SKIP_LINK_CQES	= IO_REQ_FLAG(REQ_F_SKIP_LINK_CQES_BIT),
567 	/* single poll may be active */
568 	REQ_F_SINGLE_POLL	= IO_REQ_FLAG(REQ_F_SINGLE_POLL_BIT),
569 	/* double poll may active */
570 	REQ_F_DOUBLE_POLL	= IO_REQ_FLAG(REQ_F_DOUBLE_POLL_BIT),
571 	/* request posts multiple completions, should be set at prep time */
572 	REQ_F_MULTISHOT		= IO_REQ_FLAG(REQ_F_MULTISHOT_BIT),
573 	/* fast poll multishot mode */
574 	REQ_F_APOLL_MULTISHOT	= IO_REQ_FLAG(REQ_F_APOLL_MULTISHOT_BIT),
575 	/* recvmsg special flag, clear EPOLLIN */
576 	REQ_F_CLEAR_POLLIN	= IO_REQ_FLAG(REQ_F_CLEAR_POLLIN_BIT),
577 	/* don't use lazy poll wake for this request */
578 	REQ_F_POLL_NO_LAZY	= IO_REQ_FLAG(REQ_F_POLL_NO_LAZY_BIT),
579 	/* file is pollable */
580 	REQ_F_CAN_POLL		= IO_REQ_FLAG(REQ_F_CAN_POLL_BIT),
581 	/* buffer list was empty after selection of buffer */
582 	REQ_F_BL_EMPTY		= IO_REQ_FLAG(REQ_F_BL_EMPTY_BIT),
583 	/* don't recycle provided buffers for this request */
584 	REQ_F_BL_NO_RECYCLE	= IO_REQ_FLAG(REQ_F_BL_NO_RECYCLE_BIT),
585 	/* buffer ring head needs incrementing on put */
586 	REQ_F_BUFFERS_COMMIT	= IO_REQ_FLAG(REQ_F_BUFFERS_COMMIT_BIT),
587 	/* buf node is valid */
588 	REQ_F_BUF_NODE		= IO_REQ_FLAG(REQ_F_BUF_NODE_BIT),
589 	/* request has read/write metadata assigned */
590 	REQ_F_HAS_METADATA	= IO_REQ_FLAG(REQ_F_HAS_METADATA_BIT),
591 	/*
592 	 * For vectored fixed buffers, resolve iovec to registered buffers.
593 	 * For SEND_ZC, whether to import buffers (i.e. the first issue).
594 	 */
595 	REQ_F_IMPORT_BUFFER	= IO_REQ_FLAG(REQ_F_IMPORT_BUFFER_BIT),
596 };
597 
598 typedef void (*io_req_tw_func_t)(struct io_kiocb *req, io_tw_token_t tw);
599 
600 struct io_task_work {
601 	struct llist_node		node;
602 	io_req_tw_func_t		func;
603 };
604 
605 struct io_cqe {
606 	__u64	user_data;
607 	__s32	res;
608 	/* fd initially, then cflags for completion */
609 	union {
610 		__u32	flags;
611 		int	fd;
612 	};
613 };
614 
615 /*
616  * Each request type overlays its private data structure on top of this one.
617  * They must not exceed this one in size.
618  */
619 struct io_cmd_data {
620 	struct file		*file;
621 	/* each command gets 56 bytes of data */
622 	__u8			data[56];
623 };
624 
io_kiocb_cmd_sz_check(size_t cmd_sz)625 static inline void io_kiocb_cmd_sz_check(size_t cmd_sz)
626 {
627 	BUILD_BUG_ON(cmd_sz > sizeof(struct io_cmd_data));
628 }
629 #define io_kiocb_to_cmd(req, cmd_type) ( \
630 	io_kiocb_cmd_sz_check(sizeof(cmd_type)) , \
631 	((cmd_type *)&(req)->cmd) \
632 )
633 
cmd_to_io_kiocb(void * ptr)634 static inline struct io_kiocb *cmd_to_io_kiocb(void *ptr)
635 {
636 	return ptr;
637 }
638 
639 struct io_kiocb {
640 	union {
641 		/*
642 		 * NOTE! Each of the io_kiocb union members has the file pointer
643 		 * as the first entry in their struct definition. So you can
644 		 * access the file pointer through any of the sub-structs,
645 		 * or directly as just 'file' in this struct.
646 		 */
647 		struct file		*file;
648 		struct io_cmd_data	cmd;
649 	};
650 
651 	u8				opcode;
652 	/* polled IO has completed */
653 	u8				iopoll_completed;
654 	/*
655 	 * Can be either a fixed buffer index, or used with provided buffers.
656 	 * For the latter, before issue it points to the buffer group ID,
657 	 * and after selection it points to the buffer ID itself.
658 	 */
659 	u16				buf_index;
660 
661 	unsigned			nr_tw;
662 
663 	/* REQ_F_* flags */
664 	io_req_flags_t			flags;
665 
666 	struct io_cqe			cqe;
667 
668 	struct io_ring_ctx		*ctx;
669 	struct io_uring_task		*tctx;
670 
671 	union {
672 		/* stores selected buf, valid IFF REQ_F_BUFFER_SELECTED is set */
673 		struct io_buffer	*kbuf;
674 
675 		/*
676 		 * stores buffer ID for ring provided buffers, valid IFF
677 		 * REQ_F_BUFFER_RING is set.
678 		 */
679 		struct io_buffer_list	*buf_list;
680 
681 		struct io_rsrc_node	*buf_node;
682 	};
683 
684 	union {
685 		/* used by request caches, completion batching and iopoll */
686 		struct io_wq_work_node	comp_list;
687 		/* cache ->apoll->events */
688 		__poll_t apoll_events;
689 	};
690 
691 	struct io_rsrc_node		*file_node;
692 
693 	atomic_t			refs;
694 	bool				cancel_seq_set;
695 	struct io_task_work		io_task_work;
696 	union {
697 		/*
698 		 * for polled requests, i.e. IORING_OP_POLL_ADD and async armed
699 		 * poll
700 		 */
701 		struct hlist_node	hash_node;
702 		/* For IOPOLL setup queues, with hybrid polling */
703 		u64                     iopoll_start;
704 	};
705 	/* internal polling, see IORING_FEAT_FAST_POLL */
706 	struct async_poll		*apoll;
707 	/* opcode allocated if it needs to store data for async defer */
708 	void				*async_data;
709 	/* linked requests, IFF REQ_F_HARDLINK or REQ_F_LINK are set */
710 	atomic_t			poll_refs;
711 	struct io_kiocb			*link;
712 	/* custom credentials, valid IFF REQ_F_CREDS is set */
713 	const struct cred		*creds;
714 	struct io_wq_work		work;
715 
716 	struct {
717 		u64			extra1;
718 		u64			extra2;
719 	} big_cqe;
720 };
721 
722 struct io_overflow_cqe {
723 	struct list_head list;
724 	struct io_uring_cqe cqe;
725 };
726 
io_ctx_cqe32(struct io_ring_ctx * ctx)727 static inline bool io_ctx_cqe32(struct io_ring_ctx *ctx)
728 {
729 	return ctx->flags & IORING_SETUP_CQE32;
730 }
731 
732 #endif
733