engines/io_uring_cmd: make trims async
[fio.git] / engines / io_uring.c
CommitLineData
52885fa2 1/*
bffad86f 2 * io_uring engine
52885fa2 3 *
bffad86f 4 * IO engine using the new native Linux aio io_uring interface. See:
a90cd050 5 *
bffad86f 6 * http://git.kernel.dk/cgit/linux-block/log/?h=io_uring
52885fa2
JA
7 *
8 */
9#include <stdlib.h>
10#include <unistd.h>
11#include <errno.h>
52885fa2
JA
12#include <sys/time.h>
13#include <sys/resource.h>
14
15#include "../fio.h"
16#include "../lib/pow2.h"
17#include "../optgroup.h"
18#include "../lib/memalign.h"
b87aa01a 19#include "../lib/fls.h"
6d975f2c 20#include "../lib/roundup.h"
52885fa2 21
bffad86f 22#ifdef ARCH_HAVE_IOURING
52885fa2 23
57fa61f0 24#include "../lib/types.h"
f3e769a4 25#include "../os/linux/io_uring.h"
e9f6567a 26#include "cmdprio.h"
16be6037 27#include "zbd.h"
855dc4d4
AG
28#include "nvme.h"
29
30#include <sys/stat.h>
31
32enum uring_cmd_type {
33 FIO_URING_CMD_NVME = 1,
34};
9a2d78b3 35
bffad86f 36struct io_sq_ring {
e2239016
JA
37 unsigned *head;
38 unsigned *tail;
39 unsigned *ring_mask;
40 unsigned *ring_entries;
41 unsigned *flags;
42 unsigned *array;
52885fa2
JA
43};
44
bffad86f 45struct io_cq_ring {
e2239016
JA
46 unsigned *head;
47 unsigned *tail;
48 unsigned *ring_mask;
49 unsigned *ring_entries;
f0403f94 50 struct io_uring_cqe *cqes;
9a2d78b3
JA
51};
52
bffad86f 53struct ioring_mmap {
9a2d78b3
JA
54 void *ptr;
55 size_t len;
52885fa2
JA
56};
57
bffad86f 58struct ioring_data {
9a2d78b3
JA
59 int ring_fd;
60
52885fa2
JA
61 struct io_u **io_u_index;
62
5ffd5626
JA
63 int *fds;
64
bffad86f 65 struct io_sq_ring sq_ring;
f0403f94 66 struct io_uring_sqe *sqes;
9a2d78b3 67 struct iovec *iovecs;
b87aa01a 68 unsigned sq_ring_mask;
52885fa2 69
bffad86f 70 struct io_cq_ring cq_ring;
b87aa01a 71 unsigned cq_ring_mask;
52885fa2
JA
72
73 int queued;
74 int cq_ring_off;
b87aa01a 75 unsigned iodepth;
5a59a81d 76 int prepped;
96563db9 77
bffad86f 78 struct ioring_mmap mmap[3];
d6cbeab4
NC
79
80 struct cmdprio cmdprio;
4885a6eb
VF
81
82 struct nvme_dsm_range *dsm;
52885fa2
JA
83};
84
bffad86f 85struct ioring_options {
a48f0cc7 86 struct thread_data *td;
52885fa2 87 unsigned int hipri;
d6cbeab4 88 struct cmdprio_options cmdprio_options;
52885fa2 89 unsigned int fixedbufs;
5ffd5626 90 unsigned int registerfiles;
3d7d00a3 91 unsigned int sqpoll_thread;
2ea53ca3
JA
92 unsigned int sqpoll_set;
93 unsigned int sqpoll_cpu;
b10b1e70 94 unsigned int nonvectored;
4a87b584 95 unsigned int uncached;
7d42e66e 96 unsigned int nowait;
5a59a81d 97 unsigned int force_async;
855dc4d4 98 enum uring_cmd_type cmd_type;
52885fa2
JA
99};
100
b10b1e70
JA
101static const int ddir_to_op[2][2] = {
102 { IORING_OP_READV, IORING_OP_READ },
103 { IORING_OP_WRITEV, IORING_OP_WRITE }
104};
105
3f1e3af7
KB
106static const int fixed_ddir_to_op[2] = {
107 IORING_OP_READ_FIXED,
108 IORING_OP_WRITE_FIXED
109};
110
2ea53ca3 111static int fio_ioring_sqpoll_cb(void *data, unsigned long long *val)
a90cd050 112{
bffad86f 113 struct ioring_options *o = data;
a90cd050 114
2ea53ca3
JA
115 o->sqpoll_cpu = *val;
116 o->sqpoll_set = 1;
a90cd050
JA
117 return 0;
118}
119
52885fa2
JA
120static struct fio_option options[] = {
121 {
122 .name = "hipri",
123 .lname = "High Priority",
124 .type = FIO_OPT_STR_SET,
bffad86f 125 .off1 = offsetof(struct ioring_options, hipri),
52885fa2
JA
126 .help = "Use polled IO completions",
127 .category = FIO_OPT_C_ENGINE,
27f436d9 128 .group = FIO_OPT_G_IOURING,
52885fa2 129 },
b2a432bf
PC
130#ifdef FIO_HAVE_IOPRIO_CLASS
131 {
132 .name = "cmdprio_percentage",
133 .lname = "high priority percentage",
134 .type = FIO_OPT_INT,
e9f6567a 135 .off1 = offsetof(struct ioring_options,
d6cbeab4 136 cmdprio_options.percentage[DDIR_READ]),
e9f6567a 137 .off2 = offsetof(struct ioring_options,
d6cbeab4 138 cmdprio_options.percentage[DDIR_WRITE]),
e9f6567a 139 .minval = 0,
b2a432bf
PC
140 .maxval = 100,
141 .help = "Send high priority I/O this percentage of the time",
142 .category = FIO_OPT_C_ENGINE,
143 .group = FIO_OPT_G_IOURING,
144 },
12f9d54a
DLM
145 {
146 .name = "cmdprio_class",
147 .lname = "Asynchronous I/O priority class",
148 .type = FIO_OPT_INT,
149 .off1 = offsetof(struct ioring_options,
d6cbeab4 150 cmdprio_options.class[DDIR_READ]),
12f9d54a 151 .off2 = offsetof(struct ioring_options,
d6cbeab4 152 cmdprio_options.class[DDIR_WRITE]),
12f9d54a
DLM
153 .help = "Set asynchronous IO priority class",
154 .minval = IOPRIO_MIN_PRIO_CLASS + 1,
155 .maxval = IOPRIO_MAX_PRIO_CLASS,
156 .interval = 1,
157 .category = FIO_OPT_C_ENGINE,
158 .group = FIO_OPT_G_IOURING,
159 },
160 {
161 .name = "cmdprio",
162 .lname = "Asynchronous I/O priority level",
163 .type = FIO_OPT_INT,
164 .off1 = offsetof(struct ioring_options,
d6cbeab4 165 cmdprio_options.level[DDIR_READ]),
12f9d54a 166 .off2 = offsetof(struct ioring_options,
d6cbeab4 167 cmdprio_options.level[DDIR_WRITE]),
12f9d54a
DLM
168 .help = "Set asynchronous IO priority level",
169 .minval = IOPRIO_MIN_PRIO,
170 .maxval = IOPRIO_MAX_PRIO,
171 .interval = 1,
172 .category = FIO_OPT_C_ENGINE,
173 .group = FIO_OPT_G_IOURING,
174 },
a48f0cc7
DLM
175 {
176 .name = "cmdprio_bssplit",
177 .lname = "Priority percentage block size split",
d6cbeab4
NC
178 .type = FIO_OPT_STR_STORE,
179 .off1 = offsetof(struct ioring_options,
180 cmdprio_options.bssplit_str),
a48f0cc7
DLM
181 .help = "Set priority percentages for different block sizes",
182 .category = FIO_OPT_C_ENGINE,
183 .group = FIO_OPT_G_IOURING,
184 },
b2a432bf
PC
185#else
186 {
187 .name = "cmdprio_percentage",
188 .lname = "high priority percentage",
189 .type = FIO_OPT_UNSUPPORTED,
190 .help = "Your platform does not support I/O priority classes",
191 },
12f9d54a
DLM
192 {
193 .name = "cmdprio_class",
194 .lname = "Asynchronous I/O priority class",
195 .type = FIO_OPT_UNSUPPORTED,
196 .help = "Your platform does not support I/O priority classes",
197 },
198 {
199 .name = "cmdprio",
200 .lname = "Asynchronous I/O priority level",
201 .type = FIO_OPT_UNSUPPORTED,
202 .help = "Your platform does not support I/O priority classes",
203 },
a48f0cc7
DLM
204 {
205 .name = "cmdprio_bssplit",
206 .lname = "Priority percentage block size split",
207 .type = FIO_OPT_UNSUPPORTED,
208 .help = "Your platform does not support I/O priority classes",
209 },
b2a432bf 210#endif
52885fa2
JA
211 {
212 .name = "fixedbufs",
213 .lname = "Fixed (pre-mapped) IO buffers",
214 .type = FIO_OPT_STR_SET,
bffad86f 215 .off1 = offsetof(struct ioring_options, fixedbufs),
52885fa2
JA
216 .help = "Pre map IO buffers",
217 .category = FIO_OPT_C_ENGINE,
27f436d9 218 .group = FIO_OPT_G_IOURING,
52885fa2 219 },
5ffd5626
JA
220 {
221 .name = "registerfiles",
222 .lname = "Register file set",
223 .type = FIO_OPT_STR_SET,
224 .off1 = offsetof(struct ioring_options, registerfiles),
225 .help = "Pre-open/register files",
226 .category = FIO_OPT_C_ENGINE,
27f436d9 227 .group = FIO_OPT_G_IOURING,
5ffd5626 228 },
771c9901
JA
229 {
230 .name = "sqthread_poll",
3d7d00a3 231 .lname = "Kernel SQ thread polling",
d6f936d1 232 .type = FIO_OPT_STR_SET,
3d7d00a3
JA
233 .off1 = offsetof(struct ioring_options, sqpoll_thread),
234 .help = "Offload submission/completion to kernel thread",
235 .category = FIO_OPT_C_ENGINE,
27f436d9 236 .group = FIO_OPT_G_IOURING,
3d7d00a3
JA
237 },
238 {
239 .name = "sqthread_poll_cpu",
240 .lname = "SQ Thread Poll CPU",
2ea53ca3
JA
241 .type = FIO_OPT_INT,
242 .cb = fio_ioring_sqpoll_cb,
3d7d00a3 243 .help = "What CPU to run SQ thread polling on",
a90cd050 244 .category = FIO_OPT_C_ENGINE,
27f436d9 245 .group = FIO_OPT_G_IOURING,
a90cd050 246 },
b10b1e70
JA
247 {
248 .name = "nonvectored",
249 .lname = "Non-vectored",
250 .type = FIO_OPT_INT,
251 .off1 = offsetof(struct ioring_options, nonvectored),
556d8415 252 .def = "-1",
b10b1e70
JA
253 .help = "Use non-vectored read/write commands",
254 .category = FIO_OPT_C_ENGINE,
255 .group = FIO_OPT_G_IOURING,
256 },
4a87b584
JA
257 {
258 .name = "uncached",
259 .lname = "Uncached",
260 .type = FIO_OPT_INT,
261 .off1 = offsetof(struct ioring_options, uncached),
262 .help = "Use RWF_UNCACHED for buffered read/writes",
263 .category = FIO_OPT_C_ENGINE,
264 .group = FIO_OPT_G_IOURING,
265 },
7d42e66e
KK
266 {
267 .name = "nowait",
268 .lname = "RWF_NOWAIT",
269 .type = FIO_OPT_BOOL,
270 .off1 = offsetof(struct ioring_options, nowait),
271 .help = "Use RWF_NOWAIT for reads/writes",
272 .category = FIO_OPT_C_ENGINE,
273 .group = FIO_OPT_G_IOURING,
274 },
5a59a81d
JA
275 {
276 .name = "force_async",
277 .lname = "Force async",
278 .type = FIO_OPT_INT,
279 .off1 = offsetof(struct ioring_options, force_async),
280 .help = "Set IOSQE_ASYNC every N requests",
281 .category = FIO_OPT_C_ENGINE,
282 .group = FIO_OPT_G_IOURING,
283 },
855dc4d4
AG
284 {
285 .name = "cmd_type",
286 .lname = "Uring cmd type",
287 .type = FIO_OPT_STR,
288 .off1 = offsetof(struct ioring_options, cmd_type),
289 .help = "Specify uring-cmd type",
290 .def = "nvme",
291 .posval = {
292 { .ival = "nvme",
293 .oval = FIO_URING_CMD_NVME,
294 .help = "Issue nvme-uring-cmd",
295 },
296 },
297 .category = FIO_OPT_C_ENGINE,
298 .group = FIO_OPT_G_IOURING,
299 },
52885fa2
JA
300 {
301 .name = NULL,
302 },
303};
304
bffad86f 305static int io_uring_enter(struct ioring_data *ld, unsigned int to_submit,
52885fa2
JA
306 unsigned int min_complete, unsigned int flags)
307{
c377f4f8
JA
308#ifdef FIO_ARCH_HAS_SYSCALL
309 return __do_syscall6(__NR_io_uring_enter, ld->ring_fd, to_submit,
310 min_complete, flags, NULL, 0);
311#else
bfed648c 312 return syscall(__NR_io_uring_enter, ld->ring_fd, to_submit,
521164fa 313 min_complete, flags, NULL, 0);
c377f4f8 314#endif
52885fa2
JA
315}
316
bffad86f 317static int fio_ioring_prep(struct thread_data *td, struct io_u *io_u)
52885fa2 318{
bffad86f 319 struct ioring_data *ld = td->io_ops_data;
cfcc8564 320 struct ioring_options *o = td->eo;
52885fa2 321 struct fio_file *f = io_u->file;
f0403f94 322 struct io_uring_sqe *sqe;
52885fa2 323
f0403f94 324 sqe = &ld->sqes[io_u->index];
34d6090e 325
5ffd5626
JA
326 if (o->registerfiles) {
327 sqe->fd = f->engine_pos;
328 sqe->flags = IOSQE_FIXED_FILE;
329 } else {
330 sqe->fd = f->fd;
87b69ef2 331 sqe->flags = 0;
5ffd5626 332 }
52885fa2 333
e3970057 334 if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) {
f0403f94 335 if (o->fixedbufs) {
3f1e3af7 336 sqe->opcode = fixed_ddir_to_op[io_u->ddir];
919850d2 337 sqe->addr = (unsigned long) io_u->xfer_buf;
f0403f94 338 sqe->len = io_u->xfer_buflen;
2ea53ca3 339 sqe->buf_index = io_u->index;
cfcc8564 340 } else {
832faaaf
JA
341 struct iovec *iov = &ld->iovecs[io_u->index];
342
343 /*
344 * Update based on actual io_u, requeue could have
345 * adjusted these
346 */
347 iov->iov_base = io_u->xfer_buf;
348 iov->iov_len = io_u->xfer_buflen;
349
3f1e3af7 350 sqe->opcode = ddir_to_op[io_u->ddir][!!o->nonvectored];
b10b1e70 351 if (o->nonvectored) {
832faaaf
JA
352 sqe->addr = (unsigned long) iov->iov_base;
353 sqe->len = iov->iov_len;
b10b1e70 354 } else {
832faaaf 355 sqe->addr = (unsigned long) iov;
b10b1e70
JA
356 sqe->len = 1;
357 }
cfcc8564 358 }
fd70e361 359 sqe->rw_flags = 0;
4a87b584 360 if (!td->o.odirect && o->uncached)
fd70e361 361 sqe->rw_flags |= RWF_UNCACHED;
7d42e66e
KK
362 if (o->nowait)
363 sqe->rw_flags |= RWF_NOWAIT;
8ff6b289
NC
364
365 /*
366 * Since io_uring can have a submission context (sqthread_poll)
367 * that is different from the process context, we cannot rely on
368 * the IO priority set by ioprio_set() (option prio/prioclass)
369 * to be inherited.
370 * td->ioprio will have the value of the "default prio", so set
371 * this unconditionally. This value might get overridden by
ff00f247 372 * fio_ioring_cmdprio_prep() if the option cmdprio_percentage or
8ff6b289
NC
373 * cmdprio_bssplit is used.
374 */
375 sqe->ioprio = td->ioprio;
f0403f94 376 sqe->off = io_u->offset;
48e698fa 377 } else if (ddir_sync(io_u->ddir)) {
7c70f506 378 sqe->ioprio = 0;
01387bfe
AF
379 if (io_u->ddir == DDIR_SYNC_FILE_RANGE) {
380 sqe->off = f->first_write;
381 sqe->len = f->last_write - f->first_write;
382 sqe->sync_range_flags = td->o.sync_file_range;
383 sqe->opcode = IORING_OP_SYNC_FILE_RANGE;
384 } else {
7c70f506
JA
385 sqe->off = 0;
386 sqe->addr = 0;
387 sqe->len = 0;
01387bfe
AF
388 if (io_u->ddir == DDIR_DATASYNC)
389 sqe->fsync_flags |= IORING_FSYNC_DATASYNC;
390 sqe->opcode = IORING_OP_FSYNC;
391 }
48e698fa 392 }
52885fa2 393
5a59a81d
JA
394 if (o->force_async && ++ld->prepped == o->force_async) {
395 ld->prepped = 0;
396 sqe->flags |= IOSQE_ASYNC;
397 }
398
48e698fa 399 sqe->user_data = (unsigned long) io_u;
52885fa2
JA
400 return 0;
401}
402
855dc4d4
AG
403static int fio_ioring_cmd_prep(struct thread_data *td, struct io_u *io_u)
404{
405 struct ioring_data *ld = td->io_ops_data;
406 struct ioring_options *o = td->eo;
407 struct fio_file *f = io_u->file;
3ce6a3de 408 struct nvme_uring_cmd *cmd;
855dc4d4 409 struct io_uring_sqe *sqe;
855dc4d4 410
3ce6a3de
JA
411 /* only supports nvme_uring_cmd */
412 if (o->cmd_type != FIO_URING_CMD_NVME)
413 return -EINVAL;
855dc4d4 414
4885a6eb 415 if (io_u->ddir == DDIR_TRIM && td->io_ops->flags & FIO_ASYNCIO_SYNC_TRIM)
16be6037
AK
416 return 0;
417
3ce6a3de 418 sqe = &ld->sqes[(io_u->index) << 1];
855dc4d4 419
3ce6a3de
JA
420 if (o->registerfiles) {
421 sqe->fd = f->engine_pos;
422 sqe->flags = IOSQE_FIXED_FILE;
423 } else {
424 sqe->fd = f->fd;
425 }
426 sqe->rw_flags = 0;
427 if (!td->o.odirect && o->uncached)
428 sqe->rw_flags |= RWF_UNCACHED;
429 if (o->nowait)
430 sqe->rw_flags |= RWF_NOWAIT;
855dc4d4 431
3ce6a3de
JA
432 sqe->opcode = IORING_OP_URING_CMD;
433 sqe->user_data = (unsigned long) io_u;
434 if (o->nonvectored)
435 sqe->cmd_op = NVME_URING_CMD_IO;
436 else
437 sqe->cmd_op = NVME_URING_CMD_IO_VEC;
438 if (o->force_async && ++ld->prepped == o->force_async) {
439 ld->prepped = 0;
440 sqe->flags |= IOSQE_ASYNC;
855dc4d4 441 }
0ebd3bf6
AG
442 if (o->fixedbufs) {
443 sqe->uring_cmd_flags = IORING_URING_CMD_FIXED;
444 sqe->buf_index = io_u->index;
445 }
3ce6a3de
JA
446
447 cmd = (struct nvme_uring_cmd *)sqe->cmd;
448 return fio_nvme_uring_cmd_prep(cmd, io_u,
4885a6eb
VF
449 o->nonvectored ? NULL : &ld->iovecs[io_u->index],
450 &ld->dsm[io_u->index]);
855dc4d4
AG
451}
452
bffad86f 453static struct io_u *fio_ioring_event(struct thread_data *td, int event)
52885fa2 454{
bffad86f 455 struct ioring_data *ld = td->io_ops_data;
f0403f94 456 struct io_uring_cqe *cqe;
52885fa2 457 struct io_u *io_u;
b87aa01a 458 unsigned index;
52885fa2 459
b87aa01a 460 index = (event + ld->cq_ring_off) & ld->cq_ring_mask;
52885fa2 461
f0403f94 462 cqe = &ld->cq_ring.cqes[index];
e3466352 463 io_u = (struct io_u *) (uintptr_t) cqe->user_data;
52885fa2 464
f0403f94
JA
465 if (cqe->res != io_u->xfer_buflen) {
466 if (cqe->res > io_u->xfer_buflen)
467 io_u->error = -cqe->res;
52885fa2 468 else
f0403f94 469 io_u->resid = io_u->xfer_buflen - cqe->res;
52885fa2
JA
470 } else
471 io_u->error = 0;
472
473 return io_u;
474}
475
855dc4d4
AG
476static struct io_u *fio_ioring_cmd_event(struct thread_data *td, int event)
477{
478 struct ioring_data *ld = td->io_ops_data;
479 struct ioring_options *o = td->eo;
480 struct io_uring_cqe *cqe;
481 struct io_u *io_u;
482 unsigned index;
483
484 index = (event + ld->cq_ring_off) & ld->cq_ring_mask;
485 if (o->cmd_type == FIO_URING_CMD_NVME)
486 index <<= 1;
487
488 cqe = &ld->cq_ring.cqes[index];
489 io_u = (struct io_u *) (uintptr_t) cqe->user_data;
490
491 if (cqe->res != 0)
492 io_u->error = -cqe->res;
493 else
494 io_u->error = 0;
495
496 return io_u;
497}
498
bffad86f 499static int fio_ioring_cqring_reap(struct thread_data *td, unsigned int events,
52885fa2
JA
500 unsigned int max)
501{
bffad86f
JA
502 struct ioring_data *ld = td->io_ops_data;
503 struct io_cq_ring *ring = &ld->cq_ring;
e2239016 504 unsigned head, reaped = 0;
52885fa2 505
9a2d78b3 506 head = *ring->head;
52885fa2 507 do {
9e26aff9 508 if (head == atomic_load_acquire(ring->tail))
52885fa2
JA
509 break;
510 reaped++;
511 head++;
52885fa2
JA
512 } while (reaped + events < max);
513
76ce63dd
AB
514 if (reaped)
515 atomic_store_release(ring->head, head);
516
52885fa2
JA
517 return reaped;
518}
519
bffad86f
JA
520static int fio_ioring_getevents(struct thread_data *td, unsigned int min,
521 unsigned int max, const struct timespec *t)
52885fa2 522{
bffad86f 523 struct ioring_data *ld = td->io_ops_data;
52885fa2 524 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
bffad86f
JA
525 struct ioring_options *o = td->eo;
526 struct io_cq_ring *ring = &ld->cq_ring;
b87aa01a
JA
527 unsigned events = 0;
528 int r;
52885fa2 529
9a2d78b3 530 ld->cq_ring_off = *ring->head;
52885fa2 531 do {
bffad86f 532 r = fio_ioring_cqring_reap(td, events, max);
52885fa2
JA
533 if (r) {
534 events += r;
ae8646a1 535 max -= r;
f7cbbbf8
ST
536 if (actual_min != 0)
537 actual_min -= r;
52885fa2
JA
538 continue;
539 }
540
3d7d00a3 541 if (!o->sqpoll_thread) {
9a2d78b3
JA
542 r = io_uring_enter(ld, 0, actual_min,
543 IORING_ENTER_GETEVENTS);
771c9901 544 if (r < 0) {
f6abd731 545 if (errno == EAGAIN || errno == EINTR)
771c9901 546 continue;
1816895b 547 r = -errno;
9a2d78b3 548 td_verror(td, errno, "io_uring_enter");
771c9901
JA
549 break;
550 }
52885fa2
JA
551 }
552 } while (events < min);
553
554 return r < 0 ? r : events;
555}
556
127715b6
NC
557static inline void fio_ioring_cmdprio_prep(struct thread_data *td,
558 struct io_u *io_u)
b2a432bf 559{
b2a432bf 560 struct ioring_data *ld = td->io_ops_data;
d6cbeab4 561 struct cmdprio *cmdprio = &ld->cmdprio;
127715b6
NC
562
563 if (fio_cmdprio_set_ioprio(td, cmdprio, io_u))
564 ld->sqes[io_u->index].ioprio = io_u->ioprio;
b2a432bf
PC
565}
566
67282020 567static int fio_ioring_cmd_io_u_trim(struct thread_data *td,
16be6037
AK
568 struct io_u *io_u)
569{
570 struct fio_file *f = io_u->file;
571 int ret;
572
573 if (td->o.zone_mode == ZONE_MODE_ZBD) {
574 ret = zbd_do_io_u_trim(td, io_u);
575 if (ret == io_u_completed)
576 return io_u->xfer_buflen;
577 if (ret)
578 goto err;
579 }
580
581 return fio_nvme_trim(td, f, io_u->offset, io_u->xfer_buflen);
582
583err:
584 io_u->error = ret;
585 return 0;
586}
587
bffad86f
JA
588static enum fio_q_status fio_ioring_queue(struct thread_data *td,
589 struct io_u *io_u)
52885fa2 590{
bffad86f
JA
591 struct ioring_data *ld = td->io_ops_data;
592 struct io_sq_ring *ring = &ld->sq_ring;
52885fa2
JA
593 unsigned tail, next_tail;
594
595 fio_ro_check(td, io_u);
596
b87aa01a 597 if (ld->queued == ld->iodepth)
52885fa2
JA
598 return FIO_Q_BUSY;
599
4885a6eb 600 if (io_u->ddir == DDIR_TRIM && td->io_ops->flags & FIO_ASYNCIO_SYNC_TRIM) {
52885fa2
JA
601 if (ld->queued)
602 return FIO_Q_BUSY;
603
16be6037
AK
604 if (!strcmp(td->io_ops->name, "io_uring_cmd"))
605 fio_ioring_cmd_io_u_trim(td, io_u);
606 else
607 do_io_u_trim(td, io_u);
608
52885fa2
JA
609 io_u_mark_submit(td, 1);
610 io_u_mark_complete(td, 1);
611 return FIO_Q_COMPLETED;
612 }
613
9a2d78b3 614 tail = *ring->tail;
52885fa2 615 next_tail = tail + 1;
9e26aff9 616 if (next_tail == atomic_load_acquire(ring->head))
52885fa2
JA
617 return FIO_Q_BUSY;
618
d6cbeab4 619 if (ld->cmdprio.mode != CMDPRIO_MODE_NONE)
ff00f247
NC
620 fio_ioring_cmdprio_prep(td, io_u);
621
b87aa01a 622 ring->array[tail & ld->sq_ring_mask] = io_u->index;
9e26aff9 623 atomic_store_release(ring->tail, next_tail);
52885fa2
JA
624
625 ld->queued++;
626 return FIO_Q_QUEUED;
627}
628
bffad86f 629static void fio_ioring_queued(struct thread_data *td, int start, int nr)
52885fa2 630{
bffad86f 631 struct ioring_data *ld = td->io_ops_data;
52885fa2
JA
632 struct timespec now;
633
634 if (!fio_fill_issue_time(td))
635 return;
636
637 fio_gettime(&now, NULL);
638
639 while (nr--) {
bffad86f 640 struct io_sq_ring *ring = &ld->sq_ring;
9a2d78b3 641 int index = ring->array[start & ld->sq_ring_mask];
f8289afc 642 struct io_u *io_u = ld->io_u_index[index];
52885fa2
JA
643
644 memcpy(&io_u->issue_time, &now, sizeof(now));
645 io_u_queued(td, io_u);
646
647 start++;
52885fa2 648 }
39f56400
VF
649
650 /*
651 * only used for iolog
652 */
653 if (td->o.read_iolog_file)
654 memcpy(&td->last_issue, &now, sizeof(now));
52885fa2
JA
655}
656
bffad86f 657static int fio_ioring_commit(struct thread_data *td)
52885fa2 658{
bffad86f
JA
659 struct ioring_data *ld = td->io_ops_data;
660 struct ioring_options *o = td->eo;
52885fa2
JA
661 int ret;
662
663 if (!ld->queued)
664 return 0;
665
3d7d00a3
JA
666 /*
667 * Kernel side does submission. just need to check if the ring is
668 * flagged as needing a kick, if so, call io_uring_enter(). This
669 * only happens if we've been idle too long.
670 */
671 if (o->sqpoll_thread) {
bffad86f 672 struct io_sq_ring *ring = &ld->sq_ring;
c011bf12 673 unsigned start = *ld->sq_ring.head;
2dd96cc4 674 unsigned flags;
4cdbc048 675
2dd96cc4
JA
676 flags = atomic_load_acquire(ring->flags);
677 if (flags & IORING_SQ_NEED_WAKEUP)
b532dd6d
JA
678 io_uring_enter(ld, ld->queued, 0,
679 IORING_ENTER_SQ_WAKEUP);
c011bf12
AK
680 fio_ioring_queued(td, start, ld->queued);
681 io_u_mark_submit(td, ld->queued);
682
771c9901
JA
683 ld->queued = 0;
684 return 0;
685 }
686
52885fa2 687 do {
9a2d78b3 688 unsigned start = *ld->sq_ring.head;
52885fa2
JA
689 long nr = ld->queued;
690
9a2d78b3 691 ret = io_uring_enter(ld, nr, 0, IORING_ENTER_GETEVENTS);
52885fa2 692 if (ret > 0) {
bffad86f 693 fio_ioring_queued(td, start, ret);
52885fa2
JA
694 io_u_mark_submit(td, ret);
695
696 ld->queued -= ret;
697 ret = 0;
a90cd050
JA
698 } else if (!ret) {
699 io_u_mark_submit(td, ret);
52885fa2 700 continue;
a90cd050 701 } else {
f6abd731 702 if (errno == EAGAIN || errno == EINTR) {
bffad86f 703 ret = fio_ioring_cqring_reap(td, 0, ld->queued);
a90cd050
JA
704 if (ret)
705 continue;
706 /* Shouldn't happen */
707 usleep(1);
708 continue;
52885fa2 709 }
1816895b 710 ret = -errno;
9a2d78b3 711 td_verror(td, errno, "io_uring_enter submit");
52885fa2 712 break;
a90cd050 713 }
52885fa2
JA
714 } while (ld->queued);
715
716 return ret;
717}
718
bffad86f 719static void fio_ioring_unmap(struct ioring_data *ld)
52885fa2 720{
9a2d78b3 721 int i;
52885fa2 722
59f94d26 723 for (i = 0; i < FIO_ARRAY_SIZE(ld->mmap); i++)
9a2d78b3
JA
724 munmap(ld->mmap[i].ptr, ld->mmap[i].len);
725 close(ld->ring_fd);
b87aa01a
JA
726}
727
bffad86f 728static void fio_ioring_cleanup(struct thread_data *td)
52885fa2 729{
bffad86f 730 struct ioring_data *ld = td->io_ops_data;
52885fa2
JA
731
732 if (ld) {
52885fa2 733 if (!(td->flags & TD_F_CHILD))
bffad86f 734 fio_ioring_unmap(ld);
9a2d78b3 735
d6cbeab4 736 fio_cmdprio_cleanup(&ld->cmdprio);
52885fa2 737 free(ld->io_u_index);
9a2d78b3 738 free(ld->iovecs);
5ffd5626 739 free(ld->fds);
4885a6eb 740 free(ld->dsm);
52885fa2
JA
741 free(ld);
742 }
743}
744
bffad86f 745static int fio_ioring_mmap(struct ioring_data *ld, struct io_uring_params *p)
9a2d78b3 746{
bffad86f
JA
747 struct io_sq_ring *sring = &ld->sq_ring;
748 struct io_cq_ring *cring = &ld->cq_ring;
9a2d78b3
JA
749 void *ptr;
750
e2239016 751 ld->mmap[0].len = p->sq_off.array + p->sq_entries * sizeof(__u32);
9a2d78b3
JA
752 ptr = mmap(0, ld->mmap[0].len, PROT_READ | PROT_WRITE,
753 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
754 IORING_OFF_SQ_RING);
755 ld->mmap[0].ptr = ptr;
756 sring->head = ptr + p->sq_off.head;
757 sring->tail = ptr + p->sq_off.tail;
758 sring->ring_mask = ptr + p->sq_off.ring_mask;
759 sring->ring_entries = ptr + p->sq_off.ring_entries;
760 sring->flags = ptr + p->sq_off.flags;
ac122fea 761 sring->array = ptr + p->sq_off.array;
9a2d78b3
JA
762 ld->sq_ring_mask = *sring->ring_mask;
763
855dc4d4
AG
764 if (p->flags & IORING_SETUP_SQE128)
765 ld->mmap[1].len = 2 * p->sq_entries * sizeof(struct io_uring_sqe);
766 else
767 ld->mmap[1].len = p->sq_entries * sizeof(struct io_uring_sqe);
f0403f94 768 ld->sqes = mmap(0, ld->mmap[1].len, PROT_READ | PROT_WRITE,
9a2d78b3 769 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
f0403f94
JA
770 IORING_OFF_SQES);
771 ld->mmap[1].ptr = ld->sqes;
9a2d78b3 772
855dc4d4
AG
773 if (p->flags & IORING_SETUP_CQE32) {
774 ld->mmap[2].len = p->cq_off.cqes +
775 2 * p->cq_entries * sizeof(struct io_uring_cqe);
776 } else {
777 ld->mmap[2].len = p->cq_off.cqes +
778 p->cq_entries * sizeof(struct io_uring_cqe);
779 }
9a2d78b3
JA
780 ptr = mmap(0, ld->mmap[2].len, PROT_READ | PROT_WRITE,
781 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
782 IORING_OFF_CQ_RING);
783 ld->mmap[2].ptr = ptr;
784 cring->head = ptr + p->cq_off.head;
785 cring->tail = ptr + p->cq_off.tail;
786 cring->ring_mask = ptr + p->cq_off.ring_mask;
787 cring->ring_entries = ptr + p->cq_off.ring_entries;
f0403f94 788 cring->cqes = ptr + p->cq_off.cqes;
9a2d78b3
JA
789 ld->cq_ring_mask = *cring->ring_mask;
790 return 0;
791}
792
556d8415
JA
793static void fio_ioring_probe(struct thread_data *td)
794{
795 struct ioring_data *ld = td->io_ops_data;
796 struct ioring_options *o = td->eo;
797 struct io_uring_probe *p;
798 int ret;
799
800 /* already set by user, don't touch */
801 if (o->nonvectored != -1)
802 return;
803
804 /* default to off, as that's always safe */
805 o->nonvectored = 0;
806
223decdd 807 p = calloc(1, sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
556d8415
JA
808 if (!p)
809 return;
810
556d8415
JA
811 ret = syscall(__NR_io_uring_register, ld->ring_fd,
812 IORING_REGISTER_PROBE, p, 256);
813 if (ret < 0)
814 goto out;
815
816 if (IORING_OP_WRITE > p->ops_len)
817 goto out;
818
819 if ((p->ops[IORING_OP_READ].flags & IO_URING_OP_SUPPORTED) &&
820 (p->ops[IORING_OP_WRITE].flags & IO_URING_OP_SUPPORTED))
821 o->nonvectored = 1;
822out:
823 free(p);
824}
825
bffad86f 826static int fio_ioring_queue_init(struct thread_data *td)
52885fa2 827{
bffad86f
JA
828 struct ioring_data *ld = td->io_ops_data;
829 struct ioring_options *o = td->eo;
52885fa2 830 int depth = td->o.iodepth;
bffad86f 831 struct io_uring_params p;
9a2d78b3
JA
832 int ret;
833
834 memset(&p, 0, sizeof(p));
52885fa2
JA
835
836 if (o->hipri)
bffad86f 837 p.flags |= IORING_SETUP_IOPOLL;
3d7d00a3
JA
838 if (o->sqpoll_thread) {
839 p.flags |= IORING_SETUP_SQPOLL;
840 if (o->sqpoll_set) {
841 p.flags |= IORING_SETUP_SQ_AFF;
842 p.sq_thread_cpu = o->sqpoll_cpu;
843 }
c011bf12
AK
844
845 /*
846 * Submission latency for sqpoll_thread is just the time it
847 * takes to fill in the SQ ring entries, and any syscall if
848 * IORING_SQ_NEED_WAKEUP is set, we don't need to log that time
849 * separately.
850 */
851 td->o.disable_slat = 1;
f635f1fb 852 }
a90cd050 853
1db268db
JA
854 /*
855 * Clamp CQ ring size at our SQ ring size, we don't need more entries
856 * than that.
857 */
858 p.flags |= IORING_SETUP_CQSIZE;
859 p.cq_entries = depth;
860
4d22c103
JA
861 /*
862 * Setup COOP_TASKRUN as we don't need to get IPI interrupted for
863 * completing IO operations.
864 */
865 p.flags |= IORING_SETUP_COOP_TASKRUN;
866
e453f369
JA
867 /*
868 * io_uring is always a single issuer, and we can defer task_work
869 * runs until we reap events.
870 */
871 p.flags |= IORING_SETUP_SINGLE_ISSUER | IORING_SETUP_DEFER_TASKRUN;
872
b5e99df6 873retry:
bfed648c 874 ret = syscall(__NR_io_uring_setup, depth, &p);
b5e99df6 875 if (ret < 0) {
e453f369
JA
876 if (errno == EINVAL && p.flags & IORING_SETUP_DEFER_TASKRUN) {
877 p.flags &= ~IORING_SETUP_DEFER_TASKRUN;
878 p.flags &= ~IORING_SETUP_SINGLE_ISSUER;
879 goto retry;
880 }
4d22c103
JA
881 if (errno == EINVAL && p.flags & IORING_SETUP_COOP_TASKRUN) {
882 p.flags &= ~IORING_SETUP_COOP_TASKRUN;
883 goto retry;
884 }
b5e99df6
JA
885 if (errno == EINVAL && p.flags & IORING_SETUP_CQSIZE) {
886 p.flags &= ~IORING_SETUP_CQSIZE;
887 goto retry;
888 }
9a2d78b3 889 return ret;
b5e99df6 890 }
9a2d78b3
JA
891
892 ld->ring_fd = ret;
2ea53ca3 893
556d8415
JA
894 fio_ioring_probe(td);
895
2ea53ca3 896 if (o->fixedbufs) {
bfed648c 897 ret = syscall(__NR_io_uring_register, ld->ring_fd,
919850d2 898 IORING_REGISTER_BUFFERS, ld->iovecs, depth);
2ea53ca3
JA
899 if (ret < 0)
900 return ret;
901 }
902
bffad86f 903 return fio_ioring_mmap(ld, &p);
52885fa2
JA
904}
905
855dc4d4
AG
906static int fio_ioring_cmd_queue_init(struct thread_data *td)
907{
908 struct ioring_data *ld = td->io_ops_data;
909 struct ioring_options *o = td->eo;
910 int depth = td->o.iodepth;
911 struct io_uring_params p;
912 int ret;
913
914 memset(&p, 0, sizeof(p));
915
916 if (o->hipri)
917 p.flags |= IORING_SETUP_IOPOLL;
918 if (o->sqpoll_thread) {
919 p.flags |= IORING_SETUP_SQPOLL;
920 if (o->sqpoll_set) {
921 p.flags |= IORING_SETUP_SQ_AFF;
922 p.sq_thread_cpu = o->sqpoll_cpu;
923 }
c011bf12
AK
924
925 /*
926 * Submission latency for sqpoll_thread is just the time it
927 * takes to fill in the SQ ring entries, and any syscall if
928 * IORING_SQ_NEED_WAKEUP is set, we don't need to log that time
929 * separately.
930 */
931 td->o.disable_slat = 1;
855dc4d4
AG
932 }
933 if (o->cmd_type == FIO_URING_CMD_NVME) {
934 p.flags |= IORING_SETUP_SQE128;
935 p.flags |= IORING_SETUP_CQE32;
936 }
937
938 /*
939 * Clamp CQ ring size at our SQ ring size, we don't need more entries
940 * than that.
941 */
942 p.flags |= IORING_SETUP_CQSIZE;
943 p.cq_entries = depth;
944
07f78c37
AK
945 /*
946 * Setup COOP_TASKRUN as we don't need to get IPI interrupted for
947 * completing IO operations.
948 */
949 p.flags |= IORING_SETUP_COOP_TASKRUN;
950
951 /*
952 * io_uring is always a single issuer, and we can defer task_work
953 * runs until we reap events.
954 */
955 p.flags |= IORING_SETUP_SINGLE_ISSUER | IORING_SETUP_DEFER_TASKRUN;
956
855dc4d4
AG
957retry:
958 ret = syscall(__NR_io_uring_setup, depth, &p);
959 if (ret < 0) {
07f78c37
AK
960 if (errno == EINVAL && p.flags & IORING_SETUP_DEFER_TASKRUN) {
961 p.flags &= ~IORING_SETUP_DEFER_TASKRUN;
962 p.flags &= ~IORING_SETUP_SINGLE_ISSUER;
963 goto retry;
964 }
965 if (errno == EINVAL && p.flags & IORING_SETUP_COOP_TASKRUN) {
966 p.flags &= ~IORING_SETUP_COOP_TASKRUN;
967 goto retry;
968 }
855dc4d4
AG
969 if (errno == EINVAL && p.flags & IORING_SETUP_CQSIZE) {
970 p.flags &= ~IORING_SETUP_CQSIZE;
971 goto retry;
972 }
973 return ret;
974 }
975
976 ld->ring_fd = ret;
977
978 fio_ioring_probe(td);
979
980 if (o->fixedbufs) {
981 ret = syscall(__NR_io_uring_register, ld->ring_fd,
982 IORING_REGISTER_BUFFERS, ld->iovecs, depth);
983 if (ret < 0)
984 return ret;
985 }
986
987 return fio_ioring_mmap(ld, &p);
988}
989
5ffd5626
JA
990static int fio_ioring_register_files(struct thread_data *td)
991{
992 struct ioring_data *ld = td->io_ops_data;
993 struct fio_file *f;
994 unsigned int i;
995 int ret;
996
997 ld->fds = calloc(td->o.nr_files, sizeof(int));
998
999 for_each_file(td, f, i) {
1000 ret = generic_open_file(td, f);
1001 if (ret)
1002 goto err;
1003 ld->fds[i] = f->fd;
1004 f->engine_pos = i;
1005 }
1006
bfed648c 1007 ret = syscall(__NR_io_uring_register, ld->ring_fd,
5ffd5626
JA
1008 IORING_REGISTER_FILES, ld->fds, td->o.nr_files);
1009 if (ret) {
1010err:
1011 free(ld->fds);
1012 ld->fds = NULL;
1013 }
1014
1015 /*
1016 * Pretend the file is closed again, and really close it if we hit
1017 * an error.
1018 */
1019 for_each_file(td, f, i) {
1020 if (ret) {
1021 int fio_unused ret2;
1022 ret2 = generic_close_file(td, f);
1023 } else
1024 f->fd = -1;
1025 }
1026
1027 return ret;
1028}
1029
bffad86f 1030static int fio_ioring_post_init(struct thread_data *td)
52885fa2 1031{
bffad86f 1032 struct ioring_data *ld = td->io_ops_data;
5ffd5626 1033 struct ioring_options *o = td->eo;
52885fa2 1034 struct io_u *io_u;
650346e1 1035 int err, i;
52885fa2 1036
650346e1
JA
1037 for (i = 0; i < td->o.iodepth; i++) {
1038 struct iovec *iov = &ld->iovecs[i];
9a2d78b3 1039
650346e1
JA
1040 io_u = ld->io_u_index[i];
1041 iov->iov_base = io_u->buf;
1042 iov->iov_len = td_max_bs(td);
52885fa2
JA
1043 }
1044
bffad86f 1045 err = fio_ioring_queue_init(td);
52885fa2 1046 if (err) {
0442b53f 1047 int init_err = errno;
c4f5c92f 1048
0442b53f 1049 if (init_err == ENOSYS)
c4f5c92f 1050 log_err("fio: your kernel doesn't support io_uring\n");
0442b53f 1051 td_verror(td, init_err, "io_queue_init");
52885fa2
JA
1052 return 1;
1053 }
1054
7c70f506
JA
1055 for (i = 0; i < td->o.iodepth; i++) {
1056 struct io_uring_sqe *sqe;
1057
1058 sqe = &ld->sqes[i];
1059 memset(sqe, 0, sizeof(*sqe));
1060 }
1061
5ffd5626
JA
1062 if (o->registerfiles) {
1063 err = fio_ioring_register_files(td);
1064 if (err) {
1065 td_verror(td, errno, "ioring_register_files");
1066 return 1;
1067 }
1068 }
1069
52885fa2
JA
1070 return 0;
1071}
1072
855dc4d4
AG
1073static int fio_ioring_cmd_post_init(struct thread_data *td)
1074{
1075 struct ioring_data *ld = td->io_ops_data;
1076 struct ioring_options *o = td->eo;
1077 struct io_u *io_u;
1078 int err, i;
1079
1080 for (i = 0; i < td->o.iodepth; i++) {
1081 struct iovec *iov = &ld->iovecs[i];
1082
1083 io_u = ld->io_u_index[i];
1084 iov->iov_base = io_u->buf;
1085 iov->iov_len = td_max_bs(td);
1086 }
1087
1088 err = fio_ioring_cmd_queue_init(td);
1089 if (err) {
1090 int init_err = errno;
1091
1092 td_verror(td, init_err, "io_queue_init");
1093 return 1;
1094 }
1095
1096 for (i = 0; i < td->o.iodepth; i++) {
1097 struct io_uring_sqe *sqe;
1098
1099 if (o->cmd_type == FIO_URING_CMD_NVME) {
1100 sqe = &ld->sqes[i << 1];
1101 memset(sqe, 0, 2 * sizeof(*sqe));
1102 } else {
1103 sqe = &ld->sqes[i];
1104 memset(sqe, 0, sizeof(*sqe));
1105 }
1106 }
1107
1108 if (o->registerfiles) {
1109 err = fio_ioring_register_files(td);
1110 if (err) {
1111 td_verror(td, errno, "ioring_register_files");
1112 return 1;
1113 }
1114 }
1115
1116 return 0;
1117}
1118
bffad86f 1119static int fio_ioring_init(struct thread_data *td)
52885fa2 1120{
5ffd5626 1121 struct ioring_options *o = td->eo;
bffad86f 1122 struct ioring_data *ld;
e9f6567a 1123 int ret;
52885fa2 1124
5ffd5626
JA
1125 /* sqthread submission requires registered files */
1126 if (o->sqpoll_thread)
1127 o->registerfiles = 1;
1128
1129 if (o->registerfiles && td->o.nr_files != td->o.open_files) {
1130 log_err("fio: io_uring registered files require nr_files to "
1131 "be identical to open_files\n");
1132 return 1;
1133 }
1134
52885fa2
JA
1135 ld = calloc(1, sizeof(*ld));
1136
b87aa01a
JA
1137 /* ring depth must be a power-of-2 */
1138 ld->iodepth = td->o.iodepth;
1139 td->o.iodepth = roundup_pow2(td->o.iodepth);
1140
52885fa2
JA
1141 /* io_u index */
1142 ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *));
650346e1 1143 ld->iovecs = calloc(td->o.iodepth, sizeof(struct iovec));
52885fa2
JA
1144
1145 td->io_ops_data = ld;
b2a432bf 1146
d6cbeab4 1147 ret = fio_cmdprio_init(td, &ld->cmdprio, &o->cmdprio_options);
e9f6567a
DLM
1148 if (ret) {
1149 td_verror(td, EINVAL, "fio_ioring_init");
b2a432bf
PC
1150 return 1;
1151 }
1af44196 1152
4885a6eb
VF
1153 /*
1154 * For io_uring_cmd, trims are async operations unless we are operating
1155 * in zbd mode where trim means zone reset.
1156 */
1157 if (!strcmp(td->io_ops->name, "io_uring_cmd") && td_trim(td) &&
1158 td->o.zone_mode == ZONE_MODE_ZBD)
1159 td->io_ops->flags |= FIO_ASYNCIO_SYNC_TRIM;
1160 else
1161 ld->dsm = calloc(ld->iodepth, sizeof(*ld->dsm));
1162
52885fa2
JA
1163 return 0;
1164}
1165
bffad86f 1166static int fio_ioring_io_u_init(struct thread_data *td, struct io_u *io_u)
52885fa2 1167{
bffad86f 1168 struct ioring_data *ld = td->io_ops_data;
52885fa2
JA
1169
1170 ld->io_u_index[io_u->index] = io_u;
1171 return 0;
1172}
1173
5ffd5626
JA
1174static int fio_ioring_open_file(struct thread_data *td, struct fio_file *f)
1175{
1176 struct ioring_data *ld = td->io_ops_data;
1177 struct ioring_options *o = td->eo;
1178
17318cf6 1179 if (!ld || !o->registerfiles)
5ffd5626
JA
1180 return generic_open_file(td, f);
1181
1182 f->fd = ld->fds[f->engine_pos];
1183 return 0;
1184}
1185
855dc4d4
AG
1186static int fio_ioring_cmd_open_file(struct thread_data *td, struct fio_file *f)
1187{
1188 struct ioring_data *ld = td->io_ops_data;
1189 struct ioring_options *o = td->eo;
1190
1191 if (o->cmd_type == FIO_URING_CMD_NVME) {
1192 struct nvme_data *data = NULL;
1193 unsigned int nsid, lba_size = 0;
345fa8fd 1194 __u32 ms = 0;
671aa9f5 1195 __u64 nlba = 0;
855dc4d4
AG
1196 int ret;
1197
1198 /* Store the namespace-id and lba size. */
1199 data = FILE_ENG_DATA(f);
1200 if (data == NULL) {
345fa8fd 1201 ret = fio_nvme_get_info(f, &nsid, &lba_size, &ms, &nlba);
855dc4d4
AG
1202 if (ret)
1203 return ret;
1204
1205 data = calloc(1, sizeof(struct nvme_data));
1206 data->nsid = nsid;
345fa8fd
AK
1207 if (ms)
1208 data->lba_ext = lba_size + ms;
1209 else
1210 data->lba_shift = ilog2(lba_size);
855dc4d4
AG
1211
1212 FILE_SET_ENG_DATA(f, data);
1213 }
345fa8fd 1214
be42eadd
AK
1215 assert(data->lba_shift < 32);
1216 lba_size = data->lba_ext ? data->lba_ext : (1U << data->lba_shift);
345fa8fd
AK
1217
1218 for_each_rw_ddir(ddir) {
1219 if (td->o.min_bs[ddir] % lba_size ||
1220 td->o.max_bs[ddir] % lba_size) {
1221 if (data->lba_ext)
1222 log_err("block size must be a multiple of "
1223 "(LBA data size + Metadata size)\n");
1224 else
1225 log_err("block size must be a multiple of LBA data size\n");
1226 return 1;
1227 }
1228 }
855dc4d4
AG
1229 }
1230 if (!ld || !o->registerfiles)
1231 return generic_open_file(td, f);
1232
1233 f->fd = ld->fds[f->engine_pos];
1234 return 0;
1235}
1236
5ffd5626
JA
1237static int fio_ioring_close_file(struct thread_data *td, struct fio_file *f)
1238{
17318cf6 1239 struct ioring_data *ld = td->io_ops_data;
5ffd5626
JA
1240 struct ioring_options *o = td->eo;
1241
17318cf6 1242 if (!ld || !o->registerfiles)
5ffd5626
JA
1243 return generic_close_file(td, f);
1244
1245 f->fd = -1;
1246 return 0;
1247}
1248
855dc4d4
AG
1249static int fio_ioring_cmd_close_file(struct thread_data *td,
1250 struct fio_file *f)
1251{
1252 struct ioring_data *ld = td->io_ops_data;
1253 struct ioring_options *o = td->eo;
1254
1255 if (o->cmd_type == FIO_URING_CMD_NVME) {
1256 struct nvme_data *data = FILE_ENG_DATA(f);
1257
1258 FILE_SET_ENG_DATA(f, NULL);
1259 free(data);
1260 }
1261 if (!ld || !o->registerfiles)
1262 return generic_close_file(td, f);
1263
1264 f->fd = -1;
1265 return 0;
1266}
1267
1268static int fio_ioring_cmd_get_file_size(struct thread_data *td,
1269 struct fio_file *f)
1270{
1271 struct ioring_options *o = td->eo;
1272
1273 if (fio_file_size_known(f))
1274 return 0;
1275
1276 if (o->cmd_type == FIO_URING_CMD_NVME) {
1277 struct nvme_data *data = NULL;
1278 unsigned int nsid, lba_size = 0;
345fa8fd 1279 __u32 ms = 0;
671aa9f5 1280 __u64 nlba = 0;
855dc4d4
AG
1281 int ret;
1282
345fa8fd 1283 ret = fio_nvme_get_info(f, &nsid, &lba_size, &ms, &nlba);
855dc4d4
AG
1284 if (ret)
1285 return ret;
1286
1287 data = calloc(1, sizeof(struct nvme_data));
1288 data->nsid = nsid;
345fa8fd
AK
1289 if (ms)
1290 data->lba_ext = lba_size + ms;
1291 else
1292 data->lba_shift = ilog2(lba_size);
855dc4d4
AG
1293
1294 f->real_file_size = lba_size * nlba;
1295 fio_file_set_size_known(f);
1296
1297 FILE_SET_ENG_DATA(f, data);
1298 return 0;
1299 }
1300 return generic_get_file_size(td, f);
1301}
1302
3d05e0ff
AK
1303static int fio_ioring_cmd_get_zoned_model(struct thread_data *td,
1304 struct fio_file *f,
1305 enum zbd_zoned_model *model)
1306{
1307 return fio_nvme_get_zoned_model(td, f, model);
1308}
1309
1310static int fio_ioring_cmd_report_zones(struct thread_data *td,
1311 struct fio_file *f, uint64_t offset,
1312 struct zbd_zone *zbdz,
1313 unsigned int nr_zones)
1314{
1315 return fio_nvme_report_zones(td, f, offset, zbdz, nr_zones);
1316}
1317
1318static int fio_ioring_cmd_reset_wp(struct thread_data *td, struct fio_file *f,
1319 uint64_t offset, uint64_t length)
1320{
1321 return fio_nvme_reset_wp(td, f, offset, length);
1322}
1323
1324static int fio_ioring_cmd_get_max_open_zones(struct thread_data *td,
1325 struct fio_file *f,
1326 unsigned int *max_open_zones)
1327{
1328 return fio_nvme_get_max_open_zones(td, f, max_open_zones);
1329}
1330
a7e8aae0
KB
1331static int fio_ioring_cmd_fetch_ruhs(struct thread_data *td, struct fio_file *f,
1332 struct fio_ruhs_info *fruhs_info)
1333{
1334 struct nvme_fdp_ruh_status *ruhs;
1335 int bytes, ret, i;
1336
1337 bytes = sizeof(*ruhs) + 128 * sizeof(struct nvme_fdp_ruh_status_desc);
1338 ruhs = scalloc(1, bytes);
1339 if (!ruhs)
1340 return -ENOMEM;
1341
1342 ret = fio_nvme_iomgmt_ruhs(td, f, ruhs, bytes);
1343 if (ret)
1344 goto free;
1345
1346 fruhs_info->nr_ruhs = le16_to_cpu(ruhs->nruhsd);
1347 for (i = 0; i < fruhs_info->nr_ruhs; i++)
1348 fruhs_info->plis[i] = le16_to_cpu(ruhs->ruhss[i].pid);
1349free:
1350 sfree(ruhs);
1351 return ret;
1352}
1353
855dc4d4 1354static struct ioengine_ops ioengine_uring = {
bffad86f 1355 .name = "io_uring",
52885fa2 1356 .version = FIO_IOOPS_VERSION,
4e7e7898
VF
1357 .flags = FIO_ASYNCIO_SYNC_TRIM | FIO_NO_OFFLOAD |
1358 FIO_ASYNCIO_SETS_ISSUE_TIME,
bffad86f
JA
1359 .init = fio_ioring_init,
1360 .post_init = fio_ioring_post_init,
1361 .io_u_init = fio_ioring_io_u_init,
1362 .prep = fio_ioring_prep,
1363 .queue = fio_ioring_queue,
1364 .commit = fio_ioring_commit,
1365 .getevents = fio_ioring_getevents,
1366 .event = fio_ioring_event,
1367 .cleanup = fio_ioring_cleanup,
5ffd5626
JA
1368 .open_file = fio_ioring_open_file,
1369 .close_file = fio_ioring_close_file,
52885fa2
JA
1370 .get_file_size = generic_get_file_size,
1371 .options = options,
bffad86f 1372 .option_struct_size = sizeof(struct ioring_options),
52885fa2
JA
1373};
1374
855dc4d4
AG
1375static struct ioengine_ops ioengine_uring_cmd = {
1376 .name = "io_uring_cmd",
1377 .version = FIO_IOOPS_VERSION,
4885a6eb 1378 .flags = FIO_NO_OFFLOAD | FIO_MEMALIGN | FIO_RAWIO |
4e7e7898 1379 FIO_ASYNCIO_SETS_ISSUE_TIME,
855dc4d4
AG
1380 .init = fio_ioring_init,
1381 .post_init = fio_ioring_cmd_post_init,
1382 .io_u_init = fio_ioring_io_u_init,
1383 .prep = fio_ioring_cmd_prep,
1384 .queue = fio_ioring_queue,
1385 .commit = fio_ioring_commit,
1386 .getevents = fio_ioring_getevents,
1387 .event = fio_ioring_cmd_event,
1388 .cleanup = fio_ioring_cleanup,
1389 .open_file = fio_ioring_cmd_open_file,
1390 .close_file = fio_ioring_cmd_close_file,
1391 .get_file_size = fio_ioring_cmd_get_file_size,
3d05e0ff
AK
1392 .get_zoned_model = fio_ioring_cmd_get_zoned_model,
1393 .report_zones = fio_ioring_cmd_report_zones,
1394 .reset_wp = fio_ioring_cmd_reset_wp,
1395 .get_max_open_zones = fio_ioring_cmd_get_max_open_zones,
855dc4d4
AG
1396 .options = options,
1397 .option_struct_size = sizeof(struct ioring_options),
a7e8aae0 1398 .fdp_fetch_ruhs = fio_ioring_cmd_fetch_ruhs,
855dc4d4
AG
1399};
1400
bffad86f 1401static void fio_init fio_ioring_register(void)
52885fa2 1402{
855dc4d4
AG
1403 register_ioengine(&ioengine_uring);
1404 register_ioengine(&ioengine_uring_cmd);
52885fa2
JA
1405}
1406
bffad86f 1407static void fio_exit fio_ioring_unregister(void)
52885fa2 1408{
855dc4d4
AG
1409 unregister_ioengine(&ioengine_uring);
1410 unregister_ioengine(&ioengine_uring_cmd);
52885fa2 1411}
1f90e9bb 1412#endif