xref: /linux/tools/testing/selftests/ublk/kublk.h (revision 55ab7e14222e5f0b0fd9f7711ca391d2924b35e3)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef KUBLK_INTERNAL_H
3 #define KUBLK_INTERNAL_H
4 
5 #include <unistd.h>
6 #include <stdlib.h>
7 #include <assert.h>
8 #include <stdio.h>
9 #include <stdarg.h>
10 #include <string.h>
11 #include <pthread.h>
12 #include <getopt.h>
13 #include <limits.h>
14 #include <poll.h>
15 #include <fcntl.h>
16 #include <sys/syscall.h>
17 #include <sys/mman.h>
18 #include <sys/ioctl.h>
19 #include <sys/inotify.h>
20 #include <sys/wait.h>
21 #include <sys/eventfd.h>
22 #include <sys/ipc.h>
23 #include <sys/shm.h>
24 #include <linux/io_uring.h>
25 #include <liburing.h>
26 #include <semaphore.h>
27 
28 /* allow ublk_dep.h to override ublk_cmd.h */
29 #include "ublk_dep.h"
30 #include <linux/ublk_cmd.h>
31 
32 #include "utils.h"
33 
34 #define MAX_BACK_FILES   4
35 
36 /****************** part 1: libublk ********************/
37 
38 #define CTRL_DEV		"/dev/ublk-control"
39 #define UBLKC_DEV		"/dev/ublkc"
40 #define UBLKB_DEV		"/dev/ublkb"
41 #define UBLK_CTRL_RING_DEPTH            32
42 #define ERROR_EVTFD_DEVID 	-2
43 
44 #define UBLK_IO_MAX_BYTES               (1 << 20)
45 #define UBLK_MAX_QUEUES_SHIFT		5
46 #define UBLK_MAX_QUEUES                 (1 << UBLK_MAX_QUEUES_SHIFT)
47 #define UBLK_MAX_THREADS_SHIFT		5
48 #define UBLK_MAX_THREADS		(1 << UBLK_MAX_THREADS_SHIFT)
49 #define UBLK_QUEUE_DEPTH                1024
50 
51 struct ublk_dev;
52 struct ublk_queue;
53 struct ublk_thread;
54 
55 struct stripe_ctx {
56 	/* stripe */
57 	unsigned int    chunk_size;
58 };
59 
60 struct fault_inject_ctx {
61 	/* fault_inject */
62 	unsigned long   delay_us;
63 	bool die_during_fetch;
64 };
65 
66 struct params_ctx {
67 	__u32 types;
68 
69 	__u32 logical_bs_shift;
70 	__u32 physical_bs_shift;
71 	__u32 io_min_shift;
72 	__u32 io_opt_shift;
73 	__u32 max_sectors;
74 	__u32 chunk_sectors;
75 	__u64 dev_sectors;
76 
77 	__u32 max_open_zones;
78 	__u32 max_active_zones;
79 	__u32 max_zone_append_sectors;
80 };
81 
82 struct dev_ctx {
83 	char tgt_type[16];
84 	unsigned long flags;
85 	unsigned nr_hw_queues;
86 	unsigned short nthreads;
87 	unsigned queue_depth;
88 	int dev_id;
89 	int nr_files;
90 	char *files[MAX_BACK_FILES];
91 	unsigned int	logging:1;
92 	unsigned int	all:1;
93 	unsigned int	fg:1;
94 	unsigned int	recovery:1;
95 	unsigned int	auto_zc_fallback:1;
96 	unsigned int	per_io_tasks:1;
97 	unsigned int	no_ublk_fixed_fd:1;
98 	unsigned int	safe_stop:1;
99 	unsigned int	no_auto_part_scan:1;
100 	unsigned int	rdonly_shmem_buf:1;
101 	unsigned int	rotate_auto_buf:1;
102 	__u32 integrity_flags;
103 	__u8 metadata_size;
104 	__u8 pi_offset;
105 	__u8 csum_type;
106 	__u8 tag_size;
107 	__u16 io_desc_size;
108 
109 	int _evtfd;
110 	int _shmid;
111 
112 	/* built from shmem, only for ublk_dump_dev() */
113 	struct ublk_dev *shadow_dev;
114 
115 	/* for 'update_size' command */
116 	unsigned long long size;
117 
118 	struct params_ctx params;
119 
120 	char *htlb_path;
121 
122 	union {
123 		struct stripe_ctx 	stripe;
124 		struct fault_inject_ctx fault_inject;
125 	};
126 };
127 
128 struct ublk_ctrl_cmd_data {
129 	__u32 cmd_op;
130 #define CTRL_CMD_HAS_DATA	1
131 #define CTRL_CMD_HAS_BUF	2
132 	__u32 flags;
133 
134 	__u64 data[2];
135 	__u64 addr;
136 	__u32 len;
137 };
138 
139 struct ublk_io {
140 	char *buf_addr;
141 	void *integrity_buf;
142 
143 #define UBLKS_IO_NEED_FETCH_RQ		(1UL << 0)
144 #define UBLKS_IO_NEED_COMMIT_RQ_COMP	(1UL << 1)
145 #define UBLKS_IO_FREE			(1UL << 2)
146 #define UBLKS_IO_NEED_GET_DATA           (1UL << 3)
147 #define UBLKS_IO_NEED_REG_BUF            (1UL << 4)
148 	unsigned short flags;
149 	unsigned short refs;		/* used by target code only */
150 
151 	int tag;
152 
153 	int result;
154 
155 	unsigned short buf_index;
156 	unsigned short tgt_ios;
157 	unsigned char auto_buf_phase;
158 	void *private_data;
159 };
160 
161 struct ublk_tgt_ops {
162 	const char *name;
163 	int (*init_tgt)(const struct dev_ctx *ctx, struct ublk_dev *);
164 	void (*deinit_tgt)(struct ublk_dev *);
165 
166 	void (*pre_fetch_io)(struct ublk_thread *t, struct ublk_queue *q,
167 			     int tag, bool batch);
168 	int (*queue_io)(struct ublk_thread *, struct ublk_queue *, int tag);
169 	void (*tgt_io_done)(struct ublk_thread *, struct ublk_queue *,
170 			    const struct io_uring_cqe *);
171 
172 	/*
173 	 * Target specific command line handling
174 	 *
175 	 * each option requires argument for target command line
176 	 */
177 	void (*parse_cmd_line)(struct dev_ctx *ctx, int argc, char *argv[]);
178 	void (*usage)(const struct ublk_tgt_ops *ops);
179 
180 	/* return buffer index for UBLK_F_AUTO_BUF_REG */
181 	unsigned short (*buf_index)(const struct ublk_thread *t,
182 			const struct ublk_queue *, int tag);
183 };
184 
185 struct ublk_tgt {
186 	unsigned long dev_size;
187 	unsigned int  sq_depth;
188 	unsigned int  cq_depth;
189 	const struct ublk_tgt_ops *ops;
190 	struct ublk_params params;
191 
192 	int nr_backing_files;
193 	unsigned long backing_file_size[MAX_BACK_FILES];
194 	char backing_file[MAX_BACK_FILES][PATH_MAX];
195 };
196 
197 struct ublk_queue {
198 	int q_id;
199 	int q_depth;
200 	struct ublk_dev *dev;
201 	const struct ublk_tgt_ops *tgt_ops;
202 	struct ublksrv_io_desc *io_cmd_buf;
203 
204 /* borrow three bit of ublk uapi flags, which may never be used */
205 #define UBLKS_Q_AUTO_BUF_REG_FALLBACK	(1ULL << 63)
206 #define UBLKS_Q_NO_UBLK_FIXED_FD	(1ULL << 62)
207 #define UBLKS_Q_PREPARED	(1ULL << 61)
208 #define UBLKS_Q_ROTATE_AUTO_BUF	(1ULL << 60)
209 	__u64 flags;
210 	int ublk_fd;	/* cached ublk char device fd */
211 	__u8 metadata_size;
212 	__u16 io_desc_size;
213 	struct ublk_io ios[UBLK_QUEUE_DEPTH];
214 
215 	/* used for prep io commands */
216 	pthread_spinlock_t lock;
217 };
218 
219 /* align with `ublk_elem_header` */
220 struct ublk_batch_elem {
221 	__u16 tag;
222 	__u16 buf_index;
223 	__s32 result;
224 	__u64 buf_addr;
225 };
226 
227 struct batch_commit_buf {
228 	unsigned short q_id;
229 	unsigned short buf_idx;
230 	void *elem;
231 	unsigned short done;
232 	unsigned short count;
233 };
234 
235 struct batch_fetch_buf {
236 	struct io_uring_buf_ring *br;
237 	void *fetch_buf;
238 	unsigned int fetch_buf_size;
239 	unsigned int fetch_buf_off;
240 };
241 
242 struct ublk_thread {
243 	/* Thread-local copy of queue-to-thread mapping for this thread */
244 	unsigned char q_map[UBLK_MAX_QUEUES];
245 
246 	struct ublk_dev *dev;
247 	unsigned short idx;
248 	unsigned short nr_queues;
249 
250 #define UBLKS_T_STOPPING	(1U << 0)
251 #define UBLKS_T_IDLE	(1U << 1)
252 #define UBLKS_T_BATCH_IO	(1U << 31) 	/* readonly */
253 	unsigned state;
254 	unsigned int cmd_inflight;
255 	unsigned int io_inflight;
256 
257 	unsigned short nr_bufs;
258 	unsigned short auto_buf_stride;
259 
260        /* followings are for BATCH_IO */
261 	unsigned short commit_buf_start;
262 	unsigned char  commit_buf_elem_size;
263        /*
264         * We just support single device, so pre-calculate commit/prep flags
265         */
266 	unsigned short cmd_flags;
267 	unsigned int   nr_commit_buf;
268 	unsigned int   commit_buf_size;
269 	void *commit_buf;
270 #define UBLKS_T_COMMIT_BUF_INV_IDX  ((unsigned short)-1)
271 	struct allocator commit_buf_alloc;
272 	struct batch_commit_buf *commit;
273 	/* FETCH_IO_CMDS buffer */
274 	unsigned short nr_fetch_bufs;
275 	struct batch_fetch_buf *fetch;
276 
277 	struct io_uring ring;
278 };
279 
280 struct ublk_dev {
281 	struct ublk_tgt tgt;
282 	struct ublksrv_ctrl_dev_info  dev_info;
283 	struct ublk_queue q[UBLK_MAX_QUEUES];
284 	unsigned nthreads;
285 	unsigned per_io_tasks;
286 
287 	int fds[MAX_BACK_FILES + 1];	/* fds[0] points to /dev/ublkcN */
288 	int nr_fds;
289 	int ctrl_fd;
290 	struct io_uring ring;
291 
292 	void *private_data;
293 };
294 
295 extern int ublk_queue_io_cmd(struct ublk_thread *t, struct ublk_io *io);
296 
__ublk_use_batch_io(__u64 flags)297 static inline int __ublk_use_batch_io(__u64 flags)
298 {
299 	return flags & UBLK_F_BATCH_IO;
300 }
301 
ublk_queue_batch_io(const struct ublk_queue * q)302 static inline int ublk_queue_batch_io(const struct ublk_queue *q)
303 {
304 	return __ublk_use_batch_io(q->flags);
305 }
306 
ublk_dev_batch_io(const struct ublk_dev * dev)307 static inline int ublk_dev_batch_io(const struct ublk_dev *dev)
308 {
309 	return __ublk_use_batch_io(dev->dev_info.flags);
310 }
311 
312 /* only work for handle single device in this pthread context */
ublk_thread_batch_io(const struct ublk_thread * t)313 static inline int ublk_thread_batch_io(const struct ublk_thread *t)
314 {
315 	return t->state & UBLKS_T_BATCH_IO;
316 }
317 
ublk_set_integrity_params(const struct dev_ctx * ctx,struct ublk_params * params)318 static inline void ublk_set_integrity_params(const struct dev_ctx *ctx,
319 					     struct ublk_params *params)
320 {
321 	if (!ctx->metadata_size)
322 		return;
323 
324 	params->types |= UBLK_PARAM_TYPE_INTEGRITY;
325 	params->integrity = (struct ublk_param_integrity) {
326 		.flags = ctx->integrity_flags,
327 		.interval_exp = params->basic.logical_bs_shift,
328 		.metadata_size = ctx->metadata_size,
329 		.pi_offset = ctx->pi_offset,
330 		.csum_type = ctx->csum_type,
331 		.tag_size = ctx->tag_size,
332 	};
333 }
334 
ublk_integrity_len(const struct ublk_queue * q,size_t len)335 static inline size_t ublk_integrity_len(const struct ublk_queue *q, size_t len)
336 {
337 	/* All targets currently use interval_exp = logical_bs_shift = 9 */
338 	return (len >> 9) * q->metadata_size;
339 }
340 
341 static inline size_t
ublk_integrity_data_len(const struct ublk_queue * q,size_t integrity_len)342 ublk_integrity_data_len(const struct ublk_queue *q, size_t integrity_len)
343 {
344 	return (integrity_len / q->metadata_size) << 9;
345 }
346 
ublk_io_auto_zc_fallback(const struct ublksrv_io_desc * iod)347 static inline int ublk_io_auto_zc_fallback(const struct ublksrv_io_desc *iod)
348 {
349 	return !!(iod->op_flags & UBLK_IO_F_NEED_REG_BUF);
350 }
351 
ublk_user_copy_offset(unsigned q_id,unsigned tag)352 static inline __u64 ublk_user_copy_offset(unsigned q_id, unsigned tag)
353 {
354 	return UBLKSRV_IO_BUF_OFFSET +
355 	       ((__u64)q_id << UBLK_QID_OFF | (__u64)tag << UBLK_TAG_OFF);
356 }
357 
is_target_io(__u64 user_data)358 static inline int is_target_io(__u64 user_data)
359 {
360 	return (user_data & (1ULL << 63)) != 0;
361 }
362 
build_user_data(unsigned tag,unsigned op,unsigned tgt_data,unsigned q_id,unsigned is_target_io)363 static inline __u64 build_user_data(unsigned tag, unsigned op,
364 		unsigned tgt_data, unsigned q_id, unsigned is_target_io)
365 {
366 	/* we only have 7 bits to encode q_id */
367 	_Static_assert(UBLK_MAX_QUEUES_SHIFT <= 7, "UBLK_MAX_QUEUES_SHIFT must be <= 7");
368 	ublk_assert(!(tag >> 16) && !(op >> 8) && !(tgt_data >> 16) && !(q_id >> 7));
369 
370 	return tag | ((__u64)op << 16) | ((__u64)tgt_data << 24) |
371 		(__u64)q_id << 56 | (__u64)is_target_io << 63;
372 }
373 
user_data_to_tag(__u64 user_data)374 static inline unsigned int user_data_to_tag(__u64 user_data)
375 {
376 	return user_data & 0xffff;
377 }
378 
user_data_to_op(__u64 user_data)379 static inline unsigned int user_data_to_op(__u64 user_data)
380 {
381 	return (user_data >> 16) & 0xff;
382 }
383 
user_data_to_tgt_data(__u64 user_data)384 static inline unsigned int user_data_to_tgt_data(__u64 user_data)
385 {
386 	return (user_data >> 24) & 0xffff;
387 }
388 
user_data_to_q_id(__u64 user_data)389 static inline unsigned int user_data_to_q_id(__u64 user_data)
390 {
391 	return (user_data >> 56) & 0x7f;
392 }
393 
ublk_cmd_op_nr(unsigned int op)394 static inline unsigned short ublk_cmd_op_nr(unsigned int op)
395 {
396 	return _IOC_NR(op);
397 }
398 
ublk_io_to_queue(const struct ublk_io * io)399 static inline struct ublk_queue *ublk_io_to_queue(const struct ublk_io *io)
400 {
401 	return container_of(io, struct ublk_queue, ios[io->tag]);
402 }
403 
ublk_io_alloc_sqes(struct ublk_thread * t,struct io_uring_sqe * sqes[],int nr_sqes)404 static inline int ublk_io_alloc_sqes(struct ublk_thread *t,
405 		struct io_uring_sqe *sqes[], int nr_sqes)
406 {
407 	struct io_uring *ring = &t->ring;
408 	unsigned left = io_uring_sq_space_left(ring);
409 	int i;
410 
411 	if (left < nr_sqes)
412 		io_uring_submit(ring);
413 
414 	for (i = 0; i < nr_sqes; i++) {
415 		sqes[i] = io_uring_get_sqe(ring);
416 		if (!sqes[i])
417 			return i;
418 	}
419 
420 	return nr_sqes;
421 }
422 
ublk_get_registered_fd(struct ublk_queue * q,int fd_index)423 static inline int ublk_get_registered_fd(struct ublk_queue *q, int fd_index)
424 {
425 	if (q->flags & UBLKS_Q_NO_UBLK_FIXED_FD) {
426 		if (fd_index == 0)
427 			/* Return the raw ublk FD for index 0 */
428 			return q->ublk_fd;
429 		/* Adjust index for backing files (index 1 becomes 0, etc.) */
430 		return fd_index - 1;
431 	}
432 	return fd_index;
433 }
434 
__io_uring_prep_buf_reg_unreg(struct io_uring_sqe * sqe,struct ublk_queue * q,int tag,int q_id,__u64 index)435 static inline void __io_uring_prep_buf_reg_unreg(struct io_uring_sqe *sqe,
436 		struct ublk_queue *q, int tag, int q_id, __u64 index)
437 {
438 	struct ublksrv_io_cmd *cmd = (struct ublksrv_io_cmd *)sqe->cmd;
439 	int dev_fd = ublk_get_registered_fd(q, 0);
440 
441 	io_uring_prep_read(sqe, dev_fd, 0, 0, 0);
442 	sqe->opcode		= IORING_OP_URING_CMD;
443 	if (q->flags & UBLKS_Q_NO_UBLK_FIXED_FD)
444 		sqe->flags	&= ~IOSQE_FIXED_FILE;
445 	else
446 		sqe->flags	|= IOSQE_FIXED_FILE;
447 
448 	cmd->tag		= tag;
449 	cmd->addr		= index;
450 	cmd->q_id		= q_id;
451 }
452 
io_uring_prep_buf_register(struct io_uring_sqe * sqe,struct ublk_queue * q,int tag,int q_id,__u64 index)453 static inline void io_uring_prep_buf_register(struct io_uring_sqe *sqe,
454 		struct ublk_queue *q, int tag, int q_id, __u64 index)
455 {
456 	__io_uring_prep_buf_reg_unreg(sqe, q, tag, q_id, index);
457 	sqe->cmd_op		= UBLK_U_IO_REGISTER_IO_BUF;
458 }
459 
io_uring_prep_buf_unregister(struct io_uring_sqe * sqe,struct ublk_queue * q,int tag,int q_id,__u64 index)460 static inline void io_uring_prep_buf_unregister(struct io_uring_sqe *sqe,
461 		struct ublk_queue *q, int tag, int q_id, __u64 index)
462 {
463 	__io_uring_prep_buf_reg_unreg(sqe, q, tag, q_id, index);
464 	sqe->cmd_op		= UBLK_U_IO_UNREGISTER_IO_BUF;
465 }
466 
ublk_get_sqe_cmd(const struct io_uring_sqe * sqe)467 static inline void *ublk_get_sqe_cmd(const struct io_uring_sqe *sqe)
468 {
469 	return (void *)&sqe->cmd;
470 }
471 
ublk_set_io_res(struct ublk_queue * q,int tag,int res)472 static inline void ublk_set_io_res(struct ublk_queue *q, int tag, int res)
473 {
474 	q->ios[tag].result = res;
475 }
476 
ublk_get_io_res(const struct ublk_queue * q,unsigned tag)477 static inline int ublk_get_io_res(const struct ublk_queue *q, unsigned tag)
478 {
479 	return q->ios[tag].result;
480 }
481 
ublk_mark_io_done(struct ublk_io * io,int res)482 static inline void ublk_mark_io_done(struct ublk_io *io, int res)
483 {
484 	io->flags |= (UBLKS_IO_NEED_COMMIT_RQ_COMP | UBLKS_IO_FREE);
485 	io->result = res;
486 }
487 
ublk_get_iod(const struct ublk_queue * q,__u16 tag)488 static inline const struct ublksrv_io_desc *ublk_get_iod(const struct ublk_queue *q, __u16 tag)
489 {
490 	return (void *)q->io_cmd_buf + tag * (size_t)q->io_desc_size;
491 }
492 
ublk_set_sqe_cmd_op(struct io_uring_sqe * sqe,__u32 cmd_op)493 static inline void ublk_set_sqe_cmd_op(struct io_uring_sqe *sqe, __u32 cmd_op)
494 {
495 	__u32 *addr = (__u32 *)&sqe->off;
496 
497 	addr[0] = cmd_op;
498 	addr[1] = 0;
499 }
500 
501 static inline unsigned short ublk_batch_io_buf_idx(
502 		const struct ublk_thread *t, const struct ublk_queue *q,
503 		unsigned tag);
504 
ublk_io_buf_idx(const struct ublk_thread * t,const struct ublk_queue * q,unsigned tag)505 static inline unsigned short ublk_io_buf_idx(const struct ublk_thread *t,
506 					     const struct ublk_queue *q,
507 					     unsigned tag)
508 {
509 	if (ublk_queue_batch_io(q))
510 		return ublk_batch_io_buf_idx(t, q, tag);
511 	return q->ios[tag].buf_index;
512 }
513 
ublk_get_io(struct ublk_queue * q,unsigned tag)514 static inline struct ublk_io *ublk_get_io(struct ublk_queue *q, unsigned tag)
515 {
516 	return &q->ios[tag];
517 }
518 
ublk_completed_tgt_io(struct ublk_thread * t,struct ublk_queue * q,unsigned tag)519 static inline int ublk_completed_tgt_io(struct ublk_thread *t,
520 					struct ublk_queue *q, unsigned tag)
521 {
522 	struct ublk_io *io = ublk_get_io(q, tag);
523 
524 	t->io_inflight--;
525 
526 	return --io->tgt_ios == 0;
527 }
528 
ublk_queue_use_zc(const struct ublk_queue * q)529 static inline bool ublk_queue_use_zc(const struct ublk_queue *q)
530 {
531 	return !!(q->flags & UBLK_F_SUPPORT_ZERO_COPY);
532 }
533 
ublk_queue_use_auto_zc(const struct ublk_queue * q)534 static inline bool ublk_queue_use_auto_zc(const struct ublk_queue *q)
535 {
536 	return !!(q->flags & UBLK_F_AUTO_BUF_REG);
537 }
538 
ublk_queue_auto_zc_fallback(const struct ublk_queue * q)539 static inline bool ublk_queue_auto_zc_fallback(const struct ublk_queue *q)
540 {
541 	return !!(q->flags & UBLKS_Q_AUTO_BUF_REG_FALLBACK);
542 }
543 
ublk_queue_use_user_copy(const struct ublk_queue * q)544 static inline bool ublk_queue_use_user_copy(const struct ublk_queue *q)
545 {
546 	return !!(q->flags & UBLK_F_USER_COPY);
547 }
548 
ublk_queue_no_buf(const struct ublk_queue * q)549 static inline int ublk_queue_no_buf(const struct ublk_queue *q)
550 {
551 	return ublk_queue_use_zc(q) || ublk_queue_use_auto_zc(q);
552 }
553 
ublk_batch_commit_prepared(struct batch_commit_buf * cb)554 static inline int ublk_batch_commit_prepared(struct batch_commit_buf *cb)
555 {
556 	return cb->buf_idx != UBLKS_T_COMMIT_BUF_INV_IDX;
557 }
558 
ublk_queue_idx_in_thread(const struct ublk_thread * t,const struct ublk_queue * q)559 static inline unsigned ublk_queue_idx_in_thread(const struct ublk_thread *t,
560 						const struct ublk_queue *q)
561 {
562 	unsigned char idx;
563 
564 	idx = t->q_map[q->q_id];
565 	ublk_assert(idx != 0);
566 	return idx - 1;
567 }
568 
569 /*
570  * Each IO's buffer index has to be calculated by this helper for
571  * UBLKS_T_BATCH_IO
572  */
ublk_batch_io_buf_idx(const struct ublk_thread * t,const struct ublk_queue * q,unsigned tag)573 static inline unsigned short ublk_batch_io_buf_idx(
574 		const struct ublk_thread *t, const struct ublk_queue *q,
575 		unsigned tag)
576 {
577 	unsigned short base = ublk_queue_idx_in_thread(t, q) * q->q_depth + tag;
578 
579 	if (q->flags & UBLKS_Q_ROTATE_AUTO_BUF)
580 		return base + q->ios[tag].auto_buf_phase * t->auto_buf_stride;
581 	return base;
582 }
583 
ublk_batch_io_buf_idx_next(const struct ublk_thread * t,struct ublk_queue * q,unsigned tag)584 static inline unsigned short ublk_batch_io_buf_idx_next(
585 		const struct ublk_thread *t, struct ublk_queue *q,
586 		unsigned tag)
587 {
588 	if (q->flags & UBLKS_Q_ROTATE_AUTO_BUF)
589 		q->ios[tag].auto_buf_phase ^= 1;
590 	return ublk_batch_io_buf_idx(t, q, tag);
591 }
592 
593 /* Queue UBLK_U_IO_PREP_IO_CMDS for a specific queue with batch elements */
594 int ublk_batch_queue_prep_io_cmds(struct ublk_thread *t, struct ublk_queue *q);
595 /* Start fetching I/O commands using multishot UBLK_U_IO_FETCH_IO_CMDS */
596 void ublk_batch_start_fetch(struct ublk_thread *t);
597 /* Handle completion of batch I/O commands (prep/commit) */
598 void ublk_batch_compl_cmd(struct ublk_thread *t,
599 			  const struct io_uring_cqe *cqe);
600 /* Initialize batch I/O state and calculate buffer parameters */
601 void ublk_batch_prepare(struct ublk_thread *t);
602 /* Allocate and register commit buffers for batch operations */
603 int ublk_batch_alloc_buf(struct ublk_thread *t);
604 /* Free commit buffers and cleanup batch allocator */
605 void ublk_batch_free_buf(struct ublk_thread *t);
606 
607 /* Prepare a new commit buffer for batching completed I/O operations */
608 void ublk_batch_prep_commit(struct ublk_thread *t);
609 /* Submit UBLK_U_IO_COMMIT_IO_CMDS with batched completed I/O operations */
610 void ublk_batch_commit_io_cmds(struct ublk_thread *t);
611 /* Add a completed I/O operation to the current batch commit buffer */
612 void ublk_batch_complete_io(struct ublk_thread *t, struct ublk_queue *q,
613 			    unsigned tag, int res);
614 void ublk_batch_setup_map(unsigned char (*q_thread_map)[UBLK_MAX_QUEUES],
615 			   int nthreads, int queues);
616 
ublk_complete_io(struct ublk_thread * t,struct ublk_queue * q,unsigned tag,int res)617 static inline int ublk_complete_io(struct ublk_thread *t, struct ublk_queue *q,
618 				   unsigned tag, int res)
619 {
620 	if (ublk_queue_batch_io(q)) {
621 		ublk_batch_complete_io(t, q, tag, res);
622 		return 0;
623 	} else {
624 		struct ublk_io *io = &q->ios[tag];
625 
626 		ublk_mark_io_done(io, res);
627 		return ublk_queue_io_cmd(t, io);
628 	}
629 }
630 
ublk_queued_tgt_io(struct ublk_thread * t,struct ublk_queue * q,unsigned tag,int queued)631 static inline void ublk_queued_tgt_io(struct ublk_thread *t, struct ublk_queue *q,
632 				      unsigned tag, int queued)
633 {
634 	if (queued < 0)
635 		ublk_complete_io(t, q, tag, queued);
636 	else {
637 		struct ublk_io *io = ublk_get_io(q, tag);
638 
639 		t->io_inflight += queued;
640 		io->tgt_ios = queued;
641 		io->result = 0;
642 	}
643 }
644 
645 /* shared memory zero-copy support */
646 #define UBLK_BUF_MAX		256
647 
648 struct ublk_shmem_entry {
649 	int fd;
650 	void *mmap_base;
651 	size_t size;
652 };
653 
654 extern struct ublk_shmem_entry shmem_table[UBLK_BUF_MAX];
655 extern int shmem_count;
656 
657 extern const struct ublk_tgt_ops null_tgt_ops;
658 extern const struct ublk_tgt_ops loop_tgt_ops;
659 extern const struct ublk_tgt_ops stripe_tgt_ops;
660 extern const struct ublk_tgt_ops fault_inject_tgt_ops;
661 
662 void backing_file_tgt_deinit(struct ublk_dev *dev);
663 int backing_file_tgt_init(struct ublk_dev *dev, unsigned int nr_direct);
664 
665 #endif
666