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