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