engines/io_uring: move sqe clear out of hot path
[fio.git] / engines / io_uring.c
1 /*
2  * io_uring engine
3  *
4  * IO engine using the new native Linux aio io_uring interface. See:
5  *
6  * http://git.kernel.dk/cgit/linux-block/log/?h=io_uring
7  *
8  */
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <errno.h>
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"
19 #include "../lib/fls.h"
20 #include "../lib/roundup.h"
21
22 #ifdef ARCH_HAVE_IOURING
23
24 #include "../lib/types.h"
25 #include "../os/linux/io_uring.h"
26
27 struct io_sq_ring {
28         unsigned *head;
29         unsigned *tail;
30         unsigned *ring_mask;
31         unsigned *ring_entries;
32         unsigned *flags;
33         unsigned *array;
34 };
35
36 struct io_cq_ring {
37         unsigned *head;
38         unsigned *tail;
39         unsigned *ring_mask;
40         unsigned *ring_entries;
41         struct io_uring_cqe *cqes;
42 };
43
44 struct ioring_mmap {
45         void *ptr;
46         size_t len;
47 };
48
49 struct ioring_data {
50         int ring_fd;
51
52         struct io_u **io_u_index;
53
54         int *fds;
55
56         struct io_sq_ring sq_ring;
57         struct io_uring_sqe *sqes;
58         struct iovec *iovecs;
59         unsigned sq_ring_mask;
60
61         struct io_cq_ring cq_ring;
62         unsigned cq_ring_mask;
63
64         int queued;
65         int cq_ring_off;
66         unsigned iodepth;
67         bool ioprio_class_set;
68         bool ioprio_set;
69
70         struct ioring_mmap mmap[3];
71 };
72
73 struct ioring_options {
74         void *pad;
75         unsigned int hipri;
76         unsigned int cmdprio_percentage;
77         unsigned int fixedbufs;
78         unsigned int registerfiles;
79         unsigned int sqpoll_thread;
80         unsigned int sqpoll_set;
81         unsigned int sqpoll_cpu;
82         unsigned int nonvectored;
83         unsigned int uncached;
84         unsigned int nowait;
85 };
86
87 static const int ddir_to_op[2][2] = {
88         { IORING_OP_READV, IORING_OP_READ },
89         { IORING_OP_WRITEV, IORING_OP_WRITE }
90 };
91
92 static const int fixed_ddir_to_op[2] = {
93         IORING_OP_READ_FIXED,
94         IORING_OP_WRITE_FIXED
95 };
96
97 static int fio_ioring_sqpoll_cb(void *data, unsigned long long *val)
98 {
99         struct ioring_options *o = data;
100
101         o->sqpoll_cpu = *val;
102         o->sqpoll_set = 1;
103         return 0;
104 }
105
106 static struct fio_option options[] = {
107         {
108                 .name   = "hipri",
109                 .lname  = "High Priority",
110                 .type   = FIO_OPT_STR_SET,
111                 .off1   = offsetof(struct ioring_options, hipri),
112                 .help   = "Use polled IO completions",
113                 .category = FIO_OPT_C_ENGINE,
114                 .group  = FIO_OPT_G_IOURING,
115         },
116 #ifdef FIO_HAVE_IOPRIO_CLASS
117         {
118                 .name   = "cmdprio_percentage",
119                 .lname  = "high priority percentage",
120                 .type   = FIO_OPT_INT,
121                 .off1   = offsetof(struct ioring_options, cmdprio_percentage),
122                 .minval = 1,
123                 .maxval = 100,
124                 .help   = "Send high priority I/O this percentage of the time",
125                 .category = FIO_OPT_C_ENGINE,
126                 .group  = FIO_OPT_G_IOURING,
127         },
128 #else
129         {
130                 .name   = "cmdprio_percentage",
131                 .lname  = "high priority percentage",
132                 .type   = FIO_OPT_UNSUPPORTED,
133                 .help   = "Your platform does not support I/O priority classes",
134         },
135 #endif
136         {
137                 .name   = "fixedbufs",
138                 .lname  = "Fixed (pre-mapped) IO buffers",
139                 .type   = FIO_OPT_STR_SET,
140                 .off1   = offsetof(struct ioring_options, fixedbufs),
141                 .help   = "Pre map IO buffers",
142                 .category = FIO_OPT_C_ENGINE,
143                 .group  = FIO_OPT_G_IOURING,
144         },
145         {
146                 .name   = "registerfiles",
147                 .lname  = "Register file set",
148                 .type   = FIO_OPT_STR_SET,
149                 .off1   = offsetof(struct ioring_options, registerfiles),
150                 .help   = "Pre-open/register files",
151                 .category = FIO_OPT_C_ENGINE,
152                 .group  = FIO_OPT_G_IOURING,
153         },
154         {
155                 .name   = "sqthread_poll",
156                 .lname  = "Kernel SQ thread polling",
157                 .type   = FIO_OPT_INT,
158                 .off1   = offsetof(struct ioring_options, sqpoll_thread),
159                 .help   = "Offload submission/completion to kernel thread",
160                 .category = FIO_OPT_C_ENGINE,
161                 .group  = FIO_OPT_G_IOURING,
162         },
163         {
164                 .name   = "sqthread_poll_cpu",
165                 .lname  = "SQ Thread Poll CPU",
166                 .type   = FIO_OPT_INT,
167                 .cb     = fio_ioring_sqpoll_cb,
168                 .help   = "What CPU to run SQ thread polling on",
169                 .category = FIO_OPT_C_ENGINE,
170                 .group  = FIO_OPT_G_IOURING,
171         },
172         {
173                 .name   = "nonvectored",
174                 .lname  = "Non-vectored",
175                 .type   = FIO_OPT_INT,
176                 .off1   = offsetof(struct ioring_options, nonvectored),
177                 .def    = "-1",
178                 .help   = "Use non-vectored read/write commands",
179                 .category = FIO_OPT_C_ENGINE,
180                 .group  = FIO_OPT_G_IOURING,
181         },
182         {
183                 .name   = "uncached",
184                 .lname  = "Uncached",
185                 .type   = FIO_OPT_INT,
186                 .off1   = offsetof(struct ioring_options, uncached),
187                 .help   = "Use RWF_UNCACHED for buffered read/writes",
188                 .category = FIO_OPT_C_ENGINE,
189                 .group  = FIO_OPT_G_IOURING,
190         },
191         {
192                 .name   = "nowait",
193                 .lname  = "RWF_NOWAIT",
194                 .type   = FIO_OPT_BOOL,
195                 .off1   = offsetof(struct ioring_options, nowait),
196                 .help   = "Use RWF_NOWAIT for reads/writes",
197                 .category = FIO_OPT_C_ENGINE,
198                 .group  = FIO_OPT_G_IOURING,
199         },
200         {
201                 .name   = NULL,
202         },
203 };
204
205 static int io_uring_enter(struct ioring_data *ld, unsigned int to_submit,
206                          unsigned int min_complete, unsigned int flags)
207 {
208         return syscall(__NR_io_uring_enter, ld->ring_fd, to_submit,
209                         min_complete, flags, NULL, 0);
210 }
211
212 static int fio_ioring_prep(struct thread_data *td, struct io_u *io_u)
213 {
214         struct ioring_data *ld = td->io_ops_data;
215         struct ioring_options *o = td->eo;
216         struct fio_file *f = io_u->file;
217         struct io_uring_sqe *sqe;
218
219         sqe = &ld->sqes[io_u->index];
220
221         if (o->registerfiles) {
222                 sqe->fd = f->engine_pos;
223                 sqe->flags = IOSQE_FIXED_FILE;
224         } else {
225                 sqe->fd = f->fd;
226         }
227
228         if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) {
229                 if (o->fixedbufs) {
230                         sqe->opcode = fixed_ddir_to_op[io_u->ddir];
231                         sqe->addr = (unsigned long) io_u->xfer_buf;
232                         sqe->len = io_u->xfer_buflen;
233                         sqe->buf_index = io_u->index;
234                 } else {
235                         struct iovec *iov = &ld->iovecs[io_u->index];
236
237                         /*
238                          * Update based on actual io_u, requeue could have
239                          * adjusted these
240                          */
241                         iov->iov_base = io_u->xfer_buf;
242                         iov->iov_len = io_u->xfer_buflen;
243
244                         sqe->opcode = ddir_to_op[io_u->ddir][!!o->nonvectored];
245                         if (o->nonvectored) {
246                                 sqe->addr = (unsigned long) iov->iov_base;
247                                 sqe->len = iov->iov_len;
248                         } else {
249                                 sqe->addr = (unsigned long) iov;
250                                 sqe->len = 1;
251                         }
252                 }
253                 if (!td->o.odirect && o->uncached)
254                         sqe->rw_flags = RWF_UNCACHED;
255                 if (o->nowait)
256                         sqe->rw_flags |= RWF_NOWAIT;
257                 if (ld->ioprio_class_set)
258                         sqe->ioprio = td->o.ioprio_class << 13;
259                 if (ld->ioprio_set)
260                         sqe->ioprio |= td->o.ioprio;
261                 sqe->off = io_u->offset;
262                 sqe->rw_flags = 0;
263         } else if (ddir_sync(io_u->ddir)) {
264                 sqe->ioprio = 0;
265                 if (io_u->ddir == DDIR_SYNC_FILE_RANGE) {
266                         sqe->off = f->first_write;
267                         sqe->len = f->last_write - f->first_write;
268                         sqe->sync_range_flags = td->o.sync_file_range;
269                         sqe->opcode = IORING_OP_SYNC_FILE_RANGE;
270                 } else {
271                         sqe->off = 0;
272                         sqe->addr = 0;
273                         sqe->len = 0;
274                         if (io_u->ddir == DDIR_DATASYNC)
275                                 sqe->fsync_flags |= IORING_FSYNC_DATASYNC;
276                         sqe->opcode = IORING_OP_FSYNC;
277                 }
278         }
279
280         sqe->user_data = (unsigned long) io_u;
281         return 0;
282 }
283
284 static struct io_u *fio_ioring_event(struct thread_data *td, int event)
285 {
286         struct ioring_data *ld = td->io_ops_data;
287         struct io_uring_cqe *cqe;
288         struct io_u *io_u;
289         unsigned index;
290
291         index = (event + ld->cq_ring_off) & ld->cq_ring_mask;
292
293         cqe = &ld->cq_ring.cqes[index];
294         io_u = (struct io_u *) (uintptr_t) cqe->user_data;
295
296         if (cqe->res != io_u->xfer_buflen) {
297                 if (cqe->res > io_u->xfer_buflen)
298                         io_u->error = -cqe->res;
299                 else
300                         io_u->resid = io_u->xfer_buflen - cqe->res;
301         } else
302                 io_u->error = 0;
303
304         return io_u;
305 }
306
307 static int fio_ioring_cqring_reap(struct thread_data *td, unsigned int events,
308                                    unsigned int max)
309 {
310         struct ioring_data *ld = td->io_ops_data;
311         struct io_cq_ring *ring = &ld->cq_ring;
312         unsigned head, reaped = 0;
313
314         head = *ring->head;
315         do {
316                 if (head == atomic_load_acquire(ring->tail))
317                         break;
318                 reaped++;
319                 head++;
320         } while (reaped + events < max);
321
322         if (reaped)
323                 atomic_store_release(ring->head, head);
324
325         return reaped;
326 }
327
328 static int fio_ioring_getevents(struct thread_data *td, unsigned int min,
329                                 unsigned int max, const struct timespec *t)
330 {
331         struct ioring_data *ld = td->io_ops_data;
332         unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
333         struct ioring_options *o = td->eo;
334         struct io_cq_ring *ring = &ld->cq_ring;
335         unsigned events = 0;
336         int r;
337
338         ld->cq_ring_off = *ring->head;
339         do {
340                 r = fio_ioring_cqring_reap(td, events, max);
341                 if (r) {
342                         events += r;
343                         if (actual_min != 0)
344                                 actual_min -= r;
345                         continue;
346                 }
347
348                 if (!o->sqpoll_thread) {
349                         r = io_uring_enter(ld, 0, actual_min,
350                                                 IORING_ENTER_GETEVENTS);
351                         if (r < 0) {
352                                 if (errno == EAGAIN || errno == EINTR)
353                                         continue;
354                                 td_verror(td, errno, "io_uring_enter");
355                                 break;
356                         }
357                 }
358         } while (events < min);
359
360         return r < 0 ? r : events;
361 }
362
363 static void fio_ioring_prio_prep(struct thread_data *td, struct io_u *io_u)
364 {
365         struct ioring_options *o = td->eo;
366         struct ioring_data *ld = td->io_ops_data;
367         if (rand_between(&td->prio_state, 0, 99) < o->cmdprio_percentage) {
368                 ld->sqes[io_u->index].ioprio = IOPRIO_CLASS_RT << IOPRIO_CLASS_SHIFT;
369                 io_u->flags |= IO_U_F_PRIORITY;
370         }
371         return;
372 }
373
374 static enum fio_q_status fio_ioring_queue(struct thread_data *td,
375                                           struct io_u *io_u)
376 {
377         struct ioring_data *ld = td->io_ops_data;
378         struct io_sq_ring *ring = &ld->sq_ring;
379         struct ioring_options *o = td->eo;
380         unsigned tail, next_tail;
381
382         fio_ro_check(td, io_u);
383
384         if (ld->queued == ld->iodepth)
385                 return FIO_Q_BUSY;
386
387         if (io_u->ddir == DDIR_TRIM) {
388                 if (ld->queued)
389                         return FIO_Q_BUSY;
390
391                 do_io_u_trim(td, io_u);
392                 io_u_mark_submit(td, 1);
393                 io_u_mark_complete(td, 1);
394                 return FIO_Q_COMPLETED;
395         }
396
397         tail = *ring->tail;
398         next_tail = tail + 1;
399         if (next_tail == atomic_load_acquire(ring->head))
400                 return FIO_Q_BUSY;
401
402         if (o->cmdprio_percentage)
403                 fio_ioring_prio_prep(td, io_u);
404         ring->array[tail & ld->sq_ring_mask] = io_u->index;
405         atomic_store_release(ring->tail, next_tail);
406
407         ld->queued++;
408         return FIO_Q_QUEUED;
409 }
410
411 static void fio_ioring_queued(struct thread_data *td, int start, int nr)
412 {
413         struct ioring_data *ld = td->io_ops_data;
414         struct timespec now;
415
416         if (!fio_fill_issue_time(td))
417                 return;
418
419         fio_gettime(&now, NULL);
420
421         while (nr--) {
422                 struct io_sq_ring *ring = &ld->sq_ring;
423                 int index = ring->array[start & ld->sq_ring_mask];
424                 struct io_u *io_u = ld->io_u_index[index];
425
426                 memcpy(&io_u->issue_time, &now, sizeof(now));
427                 io_u_queued(td, io_u);
428
429                 start++;
430         }
431 }
432
433 static int fio_ioring_commit(struct thread_data *td)
434 {
435         struct ioring_data *ld = td->io_ops_data;
436         struct ioring_options *o = td->eo;
437         int ret;
438
439         if (!ld->queued)
440                 return 0;
441
442         /*
443          * Kernel side does submission. just need to check if the ring is
444          * flagged as needing a kick, if so, call io_uring_enter(). This
445          * only happens if we've been idle too long.
446          */
447         if (o->sqpoll_thread) {
448                 struct io_sq_ring *ring = &ld->sq_ring;
449
450                 read_barrier();
451                 if (*ring->flags & IORING_SQ_NEED_WAKEUP)
452                         io_uring_enter(ld, ld->queued, 0,
453                                         IORING_ENTER_SQ_WAKEUP);
454                 ld->queued = 0;
455                 return 0;
456         }
457
458         do {
459                 unsigned start = *ld->sq_ring.head;
460                 long nr = ld->queued;
461
462                 ret = io_uring_enter(ld, nr, 0, IORING_ENTER_GETEVENTS);
463                 if (ret > 0) {
464                         fio_ioring_queued(td, start, ret);
465                         io_u_mark_submit(td, ret);
466
467                         ld->queued -= ret;
468                         ret = 0;
469                 } else if (!ret) {
470                         io_u_mark_submit(td, ret);
471                         continue;
472                 } else {
473                         if (errno == EAGAIN || errno == EINTR) {
474                                 ret = fio_ioring_cqring_reap(td, 0, ld->queued);
475                                 if (ret)
476                                         continue;
477                                 /* Shouldn't happen */
478                                 usleep(1);
479                                 continue;
480                         }
481                         td_verror(td, errno, "io_uring_enter submit");
482                         break;
483                 }
484         } while (ld->queued);
485
486         return ret;
487 }
488
489 static void fio_ioring_unmap(struct ioring_data *ld)
490 {
491         int i;
492
493         for (i = 0; i < ARRAY_SIZE(ld->mmap); i++)
494                 munmap(ld->mmap[i].ptr, ld->mmap[i].len);
495         close(ld->ring_fd);
496 }
497
498 static void fio_ioring_cleanup(struct thread_data *td)
499 {
500         struct ioring_data *ld = td->io_ops_data;
501
502         if (ld) {
503                 if (!(td->flags & TD_F_CHILD))
504                         fio_ioring_unmap(ld);
505
506                 free(ld->io_u_index);
507                 free(ld->iovecs);
508                 free(ld->fds);
509                 free(ld);
510         }
511 }
512
513 static int fio_ioring_mmap(struct ioring_data *ld, struct io_uring_params *p)
514 {
515         struct io_sq_ring *sring = &ld->sq_ring;
516         struct io_cq_ring *cring = &ld->cq_ring;
517         void *ptr;
518
519         ld->mmap[0].len = p->sq_off.array + p->sq_entries * sizeof(__u32);
520         ptr = mmap(0, ld->mmap[0].len, PROT_READ | PROT_WRITE,
521                         MAP_SHARED | MAP_POPULATE, ld->ring_fd,
522                         IORING_OFF_SQ_RING);
523         ld->mmap[0].ptr = ptr;
524         sring->head = ptr + p->sq_off.head;
525         sring->tail = ptr + p->sq_off.tail;
526         sring->ring_mask = ptr + p->sq_off.ring_mask;
527         sring->ring_entries = ptr + p->sq_off.ring_entries;
528         sring->flags = ptr + p->sq_off.flags;
529         sring->array = ptr + p->sq_off.array;
530         ld->sq_ring_mask = *sring->ring_mask;
531
532         ld->mmap[1].len = p->sq_entries * sizeof(struct io_uring_sqe);
533         ld->sqes = mmap(0, ld->mmap[1].len, PROT_READ | PROT_WRITE,
534                                 MAP_SHARED | MAP_POPULATE, ld->ring_fd,
535                                 IORING_OFF_SQES);
536         ld->mmap[1].ptr = ld->sqes;
537
538         ld->mmap[2].len = p->cq_off.cqes +
539                                 p->cq_entries * sizeof(struct io_uring_cqe);
540         ptr = mmap(0, ld->mmap[2].len, PROT_READ | PROT_WRITE,
541                         MAP_SHARED | MAP_POPULATE, ld->ring_fd,
542                         IORING_OFF_CQ_RING);
543         ld->mmap[2].ptr = ptr;
544         cring->head = ptr + p->cq_off.head;
545         cring->tail = ptr + p->cq_off.tail;
546         cring->ring_mask = ptr + p->cq_off.ring_mask;
547         cring->ring_entries = ptr + p->cq_off.ring_entries;
548         cring->cqes = ptr + p->cq_off.cqes;
549         ld->cq_ring_mask = *cring->ring_mask;
550         return 0;
551 }
552
553 static void fio_ioring_probe(struct thread_data *td)
554 {
555         struct ioring_data *ld = td->io_ops_data;
556         struct ioring_options *o = td->eo;
557         struct io_uring_probe *p;
558         int ret;
559
560         /* already set by user, don't touch */
561         if (o->nonvectored != -1)
562                 return;
563
564         /* default to off, as that's always safe */
565         o->nonvectored = 0;
566
567         p = malloc(sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
568         if (!p)
569                 return;
570
571         memset(p, 0, sizeof(*p) + 256 * sizeof(struct io_uring_probe_op));
572         ret = syscall(__NR_io_uring_register, ld->ring_fd,
573                         IORING_REGISTER_PROBE, p, 256);
574         if (ret < 0)
575                 goto out;
576
577         if (IORING_OP_WRITE > p->ops_len)
578                 goto out;
579
580         if ((p->ops[IORING_OP_READ].flags & IO_URING_OP_SUPPORTED) &&
581             (p->ops[IORING_OP_WRITE].flags & IO_URING_OP_SUPPORTED))
582                 o->nonvectored = 1;
583 out:
584         free(p);
585 }
586
587 static int fio_ioring_queue_init(struct thread_data *td)
588 {
589         struct ioring_data *ld = td->io_ops_data;
590         struct ioring_options *o = td->eo;
591         int depth = td->o.iodepth;
592         struct io_uring_params p;
593         int ret;
594
595         memset(&p, 0, sizeof(p));
596
597         if (o->hipri)
598                 p.flags |= IORING_SETUP_IOPOLL;
599         if (o->sqpoll_thread) {
600                 p.flags |= IORING_SETUP_SQPOLL;
601                 if (o->sqpoll_set) {
602                         p.flags |= IORING_SETUP_SQ_AFF;
603                         p.sq_thread_cpu = o->sqpoll_cpu;
604                 }
605         }
606
607         ret = syscall(__NR_io_uring_setup, depth, &p);
608         if (ret < 0)
609                 return ret;
610
611         ld->ring_fd = ret;
612
613         fio_ioring_probe(td);
614
615         if (o->fixedbufs) {
616                 ret = syscall(__NR_io_uring_register, ld->ring_fd,
617                                 IORING_REGISTER_BUFFERS, ld->iovecs, depth);
618                 if (ret < 0)
619                         return ret;
620         }
621
622         return fio_ioring_mmap(ld, &p);
623 }
624
625 static int fio_ioring_register_files(struct thread_data *td)
626 {
627         struct ioring_data *ld = td->io_ops_data;
628         struct fio_file *f;
629         unsigned int i;
630         int ret;
631
632         ld->fds = calloc(td->o.nr_files, sizeof(int));
633
634         for_each_file(td, f, i) {
635                 ret = generic_open_file(td, f);
636                 if (ret)
637                         goto err;
638                 ld->fds[i] = f->fd;
639                 f->engine_pos = i;
640         }
641
642         ret = syscall(__NR_io_uring_register, ld->ring_fd,
643                         IORING_REGISTER_FILES, ld->fds, td->o.nr_files);
644         if (ret) {
645 err:
646                 free(ld->fds);
647                 ld->fds = NULL;
648         }
649
650         /*
651          * Pretend the file is closed again, and really close it if we hit
652          * an error.
653          */
654         for_each_file(td, f, i) {
655                 if (ret) {
656                         int fio_unused ret2;
657                         ret2 = generic_close_file(td, f);
658                 } else
659                         f->fd = -1;
660         }
661
662         return ret;
663 }
664
665 static int fio_ioring_post_init(struct thread_data *td)
666 {
667         struct ioring_data *ld = td->io_ops_data;
668         struct ioring_options *o = td->eo;
669         struct io_u *io_u;
670         int err, i;
671
672         for (i = 0; i < td->o.iodepth; i++) {
673                 struct iovec *iov = &ld->iovecs[i];
674
675                 io_u = ld->io_u_index[i];
676                 iov->iov_base = io_u->buf;
677                 iov->iov_len = td_max_bs(td);
678         }
679
680         err = fio_ioring_queue_init(td);
681         if (err) {
682                 td_verror(td, errno, "io_queue_init");
683                 return 1;
684         }
685
686         for (i = 0; i < td->o.iodepth; i++) {
687                 struct io_uring_sqe *sqe;
688
689                 sqe = &ld->sqes[i];
690                 memset(sqe, 0, sizeof(*sqe));
691         }
692
693         if (o->registerfiles) {
694                 err = fio_ioring_register_files(td);
695                 if (err) {
696                         td_verror(td, errno, "ioring_register_files");
697                         return 1;
698                 }
699         }
700
701         return 0;
702 }
703
704 static int fio_ioring_init(struct thread_data *td)
705 {
706         struct ioring_options *o = td->eo;
707         struct ioring_data *ld;
708         struct thread_options *to = &td->o;
709
710         /* sqthread submission requires registered files */
711         if (o->sqpoll_thread)
712                 o->registerfiles = 1;
713
714         if (o->registerfiles && td->o.nr_files != td->o.open_files) {
715                 log_err("fio: io_uring registered files require nr_files to "
716                         "be identical to open_files\n");
717                 return 1;
718         }
719
720         ld = calloc(1, sizeof(*ld));
721
722         /* ring depth must be a power-of-2 */
723         ld->iodepth = td->o.iodepth;
724         td->o.iodepth = roundup_pow2(td->o.iodepth);
725
726         /* io_u index */
727         ld->io_u_index = calloc(td->o.iodepth, sizeof(struct io_u *));
728         ld->iovecs = calloc(td->o.iodepth, sizeof(struct iovec));
729
730         td->io_ops_data = ld;
731
732         /*
733          * Check for option conflicts
734          */
735         if ((fio_option_is_set(to, ioprio) || fio_option_is_set(to, ioprio_class)) &&
736                         o->cmdprio_percentage != 0) {
737                 log_err("%s: cmdprio_percentage option and mutually exclusive "
738                                 "prio or prioclass option is set, exiting\n", to->name);
739                 td_verror(td, EINVAL, "fio_io_uring_init");
740                 return 1;
741         }
742
743         if (fio_option_is_set(&td->o, ioprio_class))
744                 ld->ioprio_class_set = true;
745         if (fio_option_is_set(&td->o, ioprio))
746                 ld->ioprio_set = true;
747
748         return 0;
749 }
750
751 static int fio_ioring_io_u_init(struct thread_data *td, struct io_u *io_u)
752 {
753         struct ioring_data *ld = td->io_ops_data;
754
755         ld->io_u_index[io_u->index] = io_u;
756         return 0;
757 }
758
759 static int fio_ioring_open_file(struct thread_data *td, struct fio_file *f)
760 {
761         struct ioring_data *ld = td->io_ops_data;
762         struct ioring_options *o = td->eo;
763
764         if (!ld || !o->registerfiles)
765                 return generic_open_file(td, f);
766
767         f->fd = ld->fds[f->engine_pos];
768         return 0;
769 }
770
771 static int fio_ioring_close_file(struct thread_data *td, struct fio_file *f)
772 {
773         struct ioring_data *ld = td->io_ops_data;
774         struct ioring_options *o = td->eo;
775
776         if (!ld || !o->registerfiles)
777                 return generic_close_file(td, f);
778
779         f->fd = -1;
780         return 0;
781 }
782
783 static struct ioengine_ops ioengine = {
784         .name                   = "io_uring",
785         .version                = FIO_IOOPS_VERSION,
786         .flags                  = FIO_ASYNCIO_SYNC_TRIM,
787         .init                   = fio_ioring_init,
788         .post_init              = fio_ioring_post_init,
789         .io_u_init              = fio_ioring_io_u_init,
790         .prep                   = fio_ioring_prep,
791         .queue                  = fio_ioring_queue,
792         .commit                 = fio_ioring_commit,
793         .getevents              = fio_ioring_getevents,
794         .event                  = fio_ioring_event,
795         .cleanup                = fio_ioring_cleanup,
796         .open_file              = fio_ioring_open_file,
797         .close_file             = fio_ioring_close_file,
798         .get_file_size          = generic_get_file_size,
799         .options                = options,
800         .option_struct_size     = sizeof(struct ioring_options),
801 };
802
803 static void fio_init fio_ioring_register(void)
804 {
805         register_ioengine(&ioengine);
806 }
807
808 static void fio_exit fio_ioring_unregister(void)
809 {
810         unregister_ioengine(&ioengine);
811 }
812 #endif