4 * IO engine using the xNVMe C API.
6 * See: http://xnvme.io/
8 * SPDX-License-Identifier: Apache-2.0
14 #include "zbd_types.h"
18 static pthread_mutex_t g_serialize = PTHREAD_MUTEX_INITIALIZER;
20 struct xnvme_fioe_fwrap {
21 /* fio file representation */
22 struct fio_file *fio_file;
24 /* xNVMe device handle */
25 struct xnvme_dev *dev;
26 /* xNVMe device geometry */
27 const struct xnvme_geo *geo;
29 struct xnvme_queue *queue;
38 XNVME_STATIC_ASSERT(sizeof(struct xnvme_fioe_fwrap) == 64, "Incorrect size")
40 struct xnvme_fioe_data {
41 /* I/O completion queue */
44 /* # of iocq entries; incremented via getevents()/cb_pool() */
48 * # of errors; incremented when observed on completion via
49 * getevents()/cb_pool()
53 /* Controller which device/file to select */
57 /* Number of devices/files for which open() has been called */
59 /* Number of devices/files allocated in files[] */
63 struct iovec *md_iovec;
65 struct xnvme_fioe_fwrap files[];
67 XNVME_STATIC_ASSERT(sizeof(struct xnvme_fioe_data) == 64, "Incorrect size")
69 struct xnvme_fioe_request {
70 /* Separate metadata buffer pointer */
74 struct xnvme_fioe_options {
77 unsigned int sqpoll_thread;
78 unsigned int xnvme_dev_nsid;
79 unsigned int xnvme_iovec;
80 unsigned int md_per_io_size;
86 char *xnvme_dev_subnqn;
89 static struct fio_option options[] = {
92 .lname = "High Priority",
93 .type = FIO_OPT_STR_SET,
94 .off1 = offsetof(struct xnvme_fioe_options, hipri),
95 .help = "Use polled IO completions",
96 .category = FIO_OPT_C_ENGINE,
97 .group = FIO_OPT_G_XNVME,
100 .name = "sqthread_poll",
101 .lname = "Kernel SQ thread polling",
102 .type = FIO_OPT_STR_SET,
103 .off1 = offsetof(struct xnvme_fioe_options, sqpoll_thread),
104 .help = "Offload submission/completion to kernel thread",
105 .category = FIO_OPT_C_ENGINE,
106 .group = FIO_OPT_G_XNVME,
110 .lname = "xNVMe Backend",
111 .type = FIO_OPT_STR_STORE,
112 .off1 = offsetof(struct xnvme_fioe_options, xnvme_be),
113 .help = "Select xNVMe backend [spdk,linux,fbsd]",
114 .category = FIO_OPT_C_ENGINE,
115 .group = FIO_OPT_G_XNVME,
119 .lname = "xNVMe Memory Backend",
120 .type = FIO_OPT_STR_STORE,
121 .off1 = offsetof(struct xnvme_fioe_options, xnvme_mem),
122 .help = "Select xNVMe memory backend",
123 .category = FIO_OPT_C_ENGINE,
124 .group = FIO_OPT_G_XNVME,
127 .name = "xnvme_async",
128 .lname = "xNVMe Asynchronous command-interface",
129 .type = FIO_OPT_STR_STORE,
130 .off1 = offsetof(struct xnvme_fioe_options, xnvme_async),
131 .help = "Select xNVMe async. interface: "
132 "[emu,thrpool,io_uring,io_uring_cmd,libaio,posix,vfio,nil]",
133 .category = FIO_OPT_C_ENGINE,
134 .group = FIO_OPT_G_XNVME,
137 .name = "xnvme_sync",
138 .lname = "xNVMe Synchronous. command-interface",
139 .type = FIO_OPT_STR_STORE,
140 .off1 = offsetof(struct xnvme_fioe_options, xnvme_sync),
141 .help = "Select xNVMe sync. interface: [nvme,psync,block]",
142 .category = FIO_OPT_C_ENGINE,
143 .group = FIO_OPT_G_XNVME,
146 .name = "xnvme_admin",
147 .lname = "xNVMe Admin command-interface",
148 .type = FIO_OPT_STR_STORE,
149 .off1 = offsetof(struct xnvme_fioe_options, xnvme_admin),
150 .help = "Select xNVMe admin. cmd-interface: [nvme,block]",
151 .category = FIO_OPT_C_ENGINE,
152 .group = FIO_OPT_G_XNVME,
155 .name = "xnvme_dev_nsid",
156 .lname = "xNVMe Namespace-Identifier, for user-space NVMe driver",
158 .off1 = offsetof(struct xnvme_fioe_options, xnvme_dev_nsid),
159 .help = "xNVMe Namespace-Identifier, for user-space NVMe driver",
160 .category = FIO_OPT_C_ENGINE,
161 .group = FIO_OPT_G_XNVME,
164 .name = "xnvme_dev_subnqn",
165 .lname = "Subsystem nqn for Fabrics",
166 .type = FIO_OPT_STR_STORE,
167 .off1 = offsetof(struct xnvme_fioe_options, xnvme_dev_subnqn),
168 .help = "Subsystem NQN for Fabrics",
169 .category = FIO_OPT_C_ENGINE,
170 .group = FIO_OPT_G_XNVME,
173 .name = "xnvme_iovec",
174 .lname = "Vectored IOs",
175 .type = FIO_OPT_STR_SET,
176 .off1 = offsetof(struct xnvme_fioe_options, xnvme_iovec),
177 .help = "Send vectored IOs",
178 .category = FIO_OPT_C_ENGINE,
179 .group = FIO_OPT_G_XNVME,
182 .name = "md_per_io_size",
183 .lname = "Separate Metadata Buffer Size per I/O",
185 .off1 = offsetof(struct xnvme_fioe_options, md_per_io_size),
187 .help = "Size of separate metadata buffer per I/O (Default: 0)",
188 .category = FIO_OPT_C_ENGINE,
189 .group = FIO_OPT_G_XNVME,
197 static void cb_pool(struct xnvme_cmd_ctx *ctx, void *cb_arg)
199 struct io_u *io_u = cb_arg;
200 struct xnvme_fioe_data *xd = io_u->mmap_data;
202 if (xnvme_cmd_ctx_cpl_status(ctx)) {
203 xnvme_cmd_ctx_pr(ctx, XNVME_PR_DEF);
208 xd->iocq[xd->completed++] = io_u;
209 xnvme_queue_put_cmd_ctx(ctx->async.queue, ctx);
212 static struct xnvme_opts xnvme_opts_from_fioe(struct thread_data *td)
214 struct xnvme_fioe_options *o = td->eo;
215 struct xnvme_opts opts = xnvme_opts_default();
217 opts.nsid = o->xnvme_dev_nsid;
218 opts.subnqn = o->xnvme_dev_subnqn;
219 opts.be = o->xnvme_be;
220 opts.mem = o->xnvme_mem;
221 opts.async = o->xnvme_async;
222 opts.sync = o->xnvme_sync;
223 opts.admin = o->xnvme_admin;
225 opts.poll_io = o->hipri;
226 opts.poll_sq = o->sqpoll_thread;
228 opts.direct = td->o.odirect;
233 static void _dev_close(struct thread_data *td, struct xnvme_fioe_fwrap *fwrap)
236 xnvme_queue_term(fwrap->queue);
238 xnvme_dev_close(fwrap->dev);
240 memset(fwrap, 0, sizeof(*fwrap));
243 static void xnvme_fioe_cleanup(struct thread_data *td)
245 struct xnvme_fioe_data *xd = NULL;
248 if (!td->io_ops_data)
251 xd = td->io_ops_data;
253 err = pthread_mutex_lock(&g_serialize);
255 log_err("ioeng->cleanup(): pthread_mutex_lock(), err(%d)\n", err);
256 /* NOTE: not returning here */
258 for (uint64_t i = 0; i < xd->nallocated; ++i)
259 _dev_close(td, &xd->files[i]);
262 err = pthread_mutex_unlock(&g_serialize);
264 log_err("ioeng->cleanup(): pthread_mutex_unlock(), err(%d)\n", err);
271 td->io_ops_data = NULL;
275 * Helper function setting up device handles as addressed by the naming
276 * convention of the given `fio_file` filename.
278 * Checks thread-options for explicit control of asynchronous implementation via
279 * the ``--xnvme_async={thrpool,emu,posix,io_uring,libaio,nil}``.
281 static int _dev_open(struct thread_data *td, struct fio_file *f)
283 struct xnvme_opts opts = xnvme_opts_from_fioe(td);
284 struct xnvme_fioe_data *xd = td->io_ops_data;
285 struct xnvme_fioe_fwrap *fwrap;
289 if (f->fileno > (int)xd->nallocated) {
290 log_err("ioeng->_dev_open(%s): invalid assumption\n", f->file_name);
294 fwrap = &xd->files[f->fileno];
296 err = pthread_mutex_lock(&g_serialize);
298 log_err("ioeng->_dev_open(%s): pthread_mutex_lock(), err(%d)\n", f->file_name,
303 fwrap->dev = xnvme_dev_open(f->file_name, &opts);
305 log_err("ioeng->_dev_open(%s): xnvme_dev_open(), err(%d)\n", f->file_name, errno);
308 fwrap->geo = xnvme_dev_get_geo(fwrap->dev);
310 if (xnvme_queue_init(fwrap->dev, td->o.iodepth, flags, &(fwrap->queue))) {
311 log_err("ioeng->_dev_open(%s): xnvme_queue_init(), err(?)\n", f->file_name);
314 xnvme_queue_set_cb(fwrap->queue, cb_pool, NULL);
316 fwrap->ssw = xnvme_dev_get_ssw(fwrap->dev);
317 fwrap->lba_nbytes = fwrap->geo->lba_nbytes;
318 fwrap->md_nbytes = fwrap->geo->nbytes_oob;
320 if (fwrap->geo->lba_extended)
326 fwrap->fio_file->filetype = FIO_TYPE_BLOCK;
327 fwrap->fio_file->real_file_size = fwrap->geo->tbytes;
328 fio_file_set_size_known(fwrap->fio_file);
330 err = pthread_mutex_unlock(&g_serialize);
332 log_err("ioeng->_dev_open(%s): pthread_mutex_unlock(), err(%d)\n", f->file_name,
338 xnvme_queue_term(fwrap->queue);
339 xnvme_dev_close(fwrap->dev);
341 err = pthread_mutex_unlock(&g_serialize);
343 log_err("ioeng->_dev_open(%s): pthread_mutex_unlock(), err(%d)\n", f->file_name,
349 static int xnvme_fioe_init(struct thread_data *td)
351 struct xnvme_fioe_data *xd = NULL;
352 struct xnvme_fioe_options *o = td->eo;
356 if (!td->o.use_thread) {
357 log_err("ioeng->init(): --thread=1 is required\n");
361 /* Allocate xd and iocq */
362 xd = calloc(1, sizeof(*xd) + sizeof(*xd->files) * td->o.nr_files);
364 log_err("ioeng->init(): !calloc(), err(%d)\n", errno);
368 xd->iocq = calloc(td->o.iodepth, sizeof(struct io_u *));
371 log_err("ioeng->init(): !calloc(xd->iocq), err(%d)\n", errno);
375 if (o->xnvme_iovec) {
376 xd->iovec = calloc(td->o.iodepth, sizeof(*xd->iovec));
380 log_err("ioeng->init(): !calloc(xd->iovec), err(%d)\n", errno);
385 if (o->xnvme_iovec && o->md_per_io_size) {
386 xd->md_iovec = calloc(td->o.iodepth, sizeof(*xd->md_iovec));
391 log_err("ioeng->init(): !calloc(xd->md_iovec), err(%d)\n", errno);
397 td->io_ops_data = xd;
399 for_each_file(td, f, i)
401 if (_dev_open(td, f)) {
403 * Note: We are not freeing xd, iocq, iovec and md_iovec.
404 * This will be done as part of cleanup routine.
406 log_err("ioeng->init(): failed; _dev_open(%s)\n", f->file_name);
413 if (xd->nallocated != td->o.nr_files) {
414 log_err("ioeng->init(): failed; nallocated != td->o.nr_files\n");
421 /* NOTE: using the first device for buffer-allocators) */
422 static int xnvme_fioe_iomem_alloc(struct thread_data *td, size_t total_mem)
424 struct xnvme_fioe_data *xd = td->io_ops_data;
425 struct xnvme_fioe_fwrap *fwrap = &xd->files[0];
428 log_err("ioeng->iomem_alloc(): failed; no dev-handle\n");
432 td->orig_buffer = xnvme_buf_alloc(fwrap->dev, total_mem);
434 return td->orig_buffer == NULL;
437 /* NOTE: using the first device for buffer-allocators) */
438 static void xnvme_fioe_iomem_free(struct thread_data *td)
440 struct xnvme_fioe_data *xd = NULL;
441 struct xnvme_fioe_fwrap *fwrap = NULL;
443 if (!td->io_ops_data)
446 xd = td->io_ops_data;
447 fwrap = &xd->files[0];
450 log_err("ioeng->iomem_free(): failed no dev-handle\n");
454 xnvme_buf_free(fwrap->dev, td->orig_buffer);
457 static int xnvme_fioe_io_u_init(struct thread_data *td, struct io_u *io_u)
459 struct xnvme_fioe_request *fio_req;
460 struct xnvme_fioe_options *o = td->eo;
461 struct xnvme_fioe_data *xd = td->io_ops_data;
462 struct xnvme_fioe_fwrap *fwrap = &xd->files[0];
465 log_err("ioeng->io_u_init(): failed; no dev-handle\n");
469 io_u->mmap_data = td->io_ops_data;
470 io_u->engine_data = NULL;
472 fio_req = calloc(1, sizeof(*fio_req));
474 log_err("ioeng->io_u_init(): !calloc(fio_req), err(%d)\n", errno);
478 if (o->md_per_io_size) {
479 fio_req->md_buf = xnvme_buf_alloc(fwrap->dev, o->md_per_io_size);
480 if (!fio_req->md_buf) {
486 io_u->engine_data = fio_req;
491 static void xnvme_fioe_io_u_free(struct thread_data *td, struct io_u *io_u)
493 struct xnvme_fioe_data *xd = NULL;
494 struct xnvme_fioe_fwrap *fwrap = NULL;
495 struct xnvme_fioe_request *fio_req = NULL;
497 if (!td->io_ops_data)
500 xd = td->io_ops_data;
501 fwrap = &xd->files[0];
504 log_err("ioeng->io_u_free(): failed no dev-handle\n");
508 fio_req = io_u->engine_data;
510 xnvme_buf_free(fwrap->dev, fio_req->md_buf);
514 io_u->mmap_data = NULL;
517 static struct io_u *xnvme_fioe_event(struct thread_data *td, int event)
519 struct xnvme_fioe_data *xd = td->io_ops_data;
522 assert((unsigned)event < xd->completed);
524 return xd->iocq[event];
527 static int xnvme_fioe_getevents(struct thread_data *td, unsigned int min, unsigned int max,
528 const struct timespec *t)
530 struct xnvme_fioe_data *xd = td->io_ops_data;
531 struct xnvme_fioe_fwrap *fwrap = NULL;
532 int nfiles = xd->nallocated;
535 if (xd->prev != -1 && ++xd->prev < nfiles) {
536 fwrap = &xd->files[xd->prev];
542 if (fwrap == NULL || xd->cur == nfiles) {
543 fwrap = &xd->files[0];
547 while (fwrap != NULL && xd->cur < nfiles && err >= 0) {
548 err = xnvme_queue_poke(fwrap->queue, max - xd->completed);
557 log_err("ioeng->getevents(): unhandled IO error\n");
562 if (xd->completed >= min) {
564 return xd->completed;
567 fwrap = &xd->files[xd->cur];
582 return xd->completed;
585 static enum fio_q_status xnvme_fioe_queue(struct thread_data *td, struct io_u *io_u)
587 struct xnvme_fioe_data *xd = td->io_ops_data;
588 struct xnvme_fioe_fwrap *fwrap;
589 struct xnvme_cmd_ctx *ctx;
590 struct xnvme_fioe_request *fio_req = io_u->engine_data;
595 bool vectored_io = ((struct xnvme_fioe_options *)td->eo)->xnvme_iovec;
596 uint32_t dir = io_u->dtype;
598 fio_ro_check(td, io_u);
600 fwrap = &xd->files[io_u->file->fileno];
601 nsid = xnvme_dev_get_nsid(fwrap->dev);
603 if (fwrap->lba_pow2) {
604 slba = io_u->offset >> fwrap->ssw;
605 nlb = (io_u->xfer_buflen >> fwrap->ssw) - 1;
607 slba = io_u->offset / fwrap->lba_nbytes;
608 nlb = (io_u->xfer_buflen / fwrap->lba_nbytes) - 1;
611 ctx = xnvme_queue_get_cmd_ctx(fwrap->queue);
612 ctx->async.cb_arg = io_u;
614 ctx->cmd.common.nsid = nsid;
615 ctx->cmd.nvm.slba = slba;
616 ctx->cmd.nvm.nlb = nlb;
618 ctx->cmd.nvm.dtype = io_u->dtype;
619 ctx->cmd.nvm.cdw13.dspec = io_u->dspec;
622 switch (io_u->ddir) {
624 ctx->cmd.common.opcode = XNVME_SPEC_NVM_OPC_READ;
628 ctx->cmd.common.opcode = XNVME_SPEC_NVM_OPC_WRITE;
632 log_err("ioeng->queue(): ENOSYS: %u\n", io_u->ddir);
633 xnvme_queue_put_cmd_ctx(ctx->async.queue, ctx);
635 io_u->error = ENOSYS;
637 return FIO_Q_COMPLETED;
641 xd->iovec[io_u->index].iov_base = io_u->xfer_buf;
642 xd->iovec[io_u->index].iov_len = io_u->xfer_buflen;
643 if (fwrap->md_nbytes && fwrap->lba_pow2) {
644 xd->md_iovec[io_u->index].iov_base = fio_req->md_buf;
645 xd->md_iovec[io_u->index].iov_len = fwrap->md_nbytes * (nlb + 1);
646 err = xnvme_cmd_passv(ctx, &xd->iovec[io_u->index], 1, io_u->xfer_buflen,
647 &xd->md_iovec[io_u->index], 1,
648 fwrap->md_nbytes * (nlb + 1));
650 err = xnvme_cmd_passv(ctx, &xd->iovec[io_u->index], 1, io_u->xfer_buflen,
654 if (fwrap->md_nbytes && fwrap->lba_pow2)
655 err = xnvme_cmd_pass(ctx, io_u->xfer_buf, io_u->xfer_buflen,
656 fio_req->md_buf, fwrap->md_nbytes * (nlb + 1));
658 err = xnvme_cmd_pass(ctx, io_u->xfer_buf, io_u->xfer_buflen, NULL, 0);
666 xnvme_queue_put_cmd_ctx(ctx->async.queue, ctx);
670 log_err("ioeng->queue(): err: '%d'\n", err);
672 xnvme_queue_put_cmd_ctx(ctx->async.queue, ctx);
674 io_u->error = abs(err);
676 return FIO_Q_COMPLETED;
680 static int xnvme_fioe_close(struct thread_data *td, struct fio_file *f)
682 struct xnvme_fioe_data *xd = td->io_ops_data;
684 dprint(FD_FILE, "xnvme close %s -- nopen: %ld\n", f->file_name, xd->nopen);
691 static int xnvme_fioe_open(struct thread_data *td, struct fio_file *f)
693 struct xnvme_fioe_data *xd = td->io_ops_data;
695 dprint(FD_FILE, "xnvme open %s -- nopen: %ld\n", f->file_name, xd->nopen);
697 if (f->fileno > (int)xd->nallocated) {
698 log_err("ioeng->open(): f->fileno > xd->nallocated; invalid assumption\n");
701 if (xd->files[f->fileno].fio_file != f) {
702 log_err("ioeng->open(): fio_file != f; invalid assumption\n");
711 static int xnvme_fioe_invalidate(struct thread_data *td, struct fio_file *f)
713 /* Consider only doing this with be:spdk */
717 static int xnvme_fioe_get_max_open_zones(struct thread_data *td, struct fio_file *f,
718 unsigned int *max_open_zones)
720 struct xnvme_opts opts = xnvme_opts_from_fioe(td);
721 struct xnvme_dev *dev;
722 const struct xnvme_spec_znd_idfy_ns *zns;
723 int err = 0, err_lock;
725 if (f->filetype != FIO_TYPE_FILE && f->filetype != FIO_TYPE_BLOCK &&
726 f->filetype != FIO_TYPE_CHAR) {
727 log_info("ioeng->get_max_open_zoned(): ignoring filetype: %d\n", f->filetype);
730 err_lock = pthread_mutex_lock(&g_serialize);
732 log_err("ioeng->get_max_open_zones(): pthread_mutex_lock(), err(%d)\n", err_lock);
736 dev = xnvme_dev_open(f->file_name, &opts);
738 log_err("ioeng->get_max_open_zones(): xnvme_dev_open(), err(%d)\n", err_lock);
742 if (xnvme_dev_get_geo(dev)->type != XNVME_GEO_ZONED) {
748 zns = (void *)xnvme_dev_get_ns_css(dev);
750 log_err("ioeng->get_max_open_zones(): xnvme_dev_get_ns_css(), err(%d)\n", errno);
756 * intentional overflow as the value is zero-based and NVMe
757 * defines 0xFFFFFFFF as unlimited thus overflowing to 0 which
758 * is how fio indicates unlimited and otherwise just converting
761 *max_open_zones = zns->mor + 1;
764 xnvme_dev_close(dev);
765 err_lock = pthread_mutex_unlock(&g_serialize);
767 log_err("ioeng->get_max_open_zones(): pthread_mutex_unlock(), err(%d)\n",
774 * Currently, this function is called before of I/O engine initialization, so,
775 * we cannot consult the file-wrapping done when 'fioe' initializes.
776 * Instead we just open based on the given filename.
778 * TODO: unify the different setup methods, consider keeping the handle around,
779 * and consider how to support the --be option in this usecase
781 static int xnvme_fioe_get_zoned_model(struct thread_data *td, struct fio_file *f,
782 enum zbd_zoned_model *model)
784 struct xnvme_opts opts = xnvme_opts_from_fioe(td);
785 struct xnvme_dev *dev;
786 int err = 0, err_lock;
788 if (f->filetype != FIO_TYPE_FILE && f->filetype != FIO_TYPE_BLOCK &&
789 f->filetype != FIO_TYPE_CHAR) {
790 log_info("ioeng->get_zoned_model(): ignoring filetype: %d\n", f->filetype);
794 err = pthread_mutex_lock(&g_serialize);
796 log_err("ioeng->get_zoned_model(): pthread_mutex_lock(), err(%d)\n", err);
800 dev = xnvme_dev_open(f->file_name, &opts);
802 log_err("ioeng->get_zoned_model(): xnvme_dev_open(%s) failed, errno: %d\n",
803 f->file_name, errno);
808 switch (xnvme_dev_get_geo(dev)->type) {
809 case XNVME_GEO_UNKNOWN:
810 dprint(FD_ZBD, "%s: got 'unknown', assigning ZBD_NONE\n", f->file_name);
814 case XNVME_GEO_CONVENTIONAL:
815 dprint(FD_ZBD, "%s: got 'conventional', assigning ZBD_NONE\n", f->file_name);
819 case XNVME_GEO_ZONED:
820 dprint(FD_ZBD, "%s: got 'zoned', assigning ZBD_HOST_MANAGED\n", f->file_name);
821 *model = ZBD_HOST_MANAGED;
825 dprint(FD_ZBD, "%s: hit-default, assigning ZBD_NONE\n", f->file_name);
833 xnvme_dev_close(dev);
835 err_lock = pthread_mutex_unlock(&g_serialize);
837 log_err("ioeng->get_zoned_model(): pthread_mutex_unlock(), err(%d)\n", err_lock);
843 * Fills the given ``zbdz`` with at most ``nr_zones`` zone-descriptors.
845 * The implementation converts the NVMe Zoned Command Set log-pages for Zone
846 * descriptors into the Linux Kernel Zoned Block Report format.
848 * NOTE: This function is called before I/O engine initialization, that is,
849 * before ``_dev_open`` has been called and file-wrapping is setup. Thus is has
850 * to do the ``_dev_open`` itself, and shut it down again once it is done
851 * retrieving the log-pages and converting them to the report format.
853 * TODO: unify the different setup methods, consider keeping the handle around,
854 * and consider how to support the --async option in this usecase
856 static int xnvme_fioe_report_zones(struct thread_data *td, struct fio_file *f, uint64_t offset,
857 struct zbd_zone *zbdz, unsigned int nr_zones)
859 struct xnvme_opts opts = xnvme_opts_from_fioe(td);
860 const struct xnvme_spec_znd_idfy_lbafe *lbafe = NULL;
861 struct xnvme_dev *dev = NULL;
862 const struct xnvme_geo *geo = NULL;
863 struct xnvme_znd_report *rprt = NULL;
866 unsigned int limit = 0;
867 int err = 0, err_lock;
869 dprint(FD_ZBD, "%s: report_zones() offset: %zu, nr_zones: %u\n", f->file_name, offset,
872 err = pthread_mutex_lock(&g_serialize);
874 log_err("ioeng->report_zones(%s): pthread_mutex_lock(), err(%d)\n", f->file_name,
879 dev = xnvme_dev_open(f->file_name, &opts);
881 log_err("ioeng->report_zones(%s): xnvme_dev_open(), err(%d)\n", f->file_name,
886 geo = xnvme_dev_get_geo(dev);
887 ssw = xnvme_dev_get_ssw(dev);
888 lbafe = xnvme_znd_dev_get_lbafe(dev);
890 limit = nr_zones > geo->nzone ? geo->nzone : nr_zones;
892 dprint(FD_ZBD, "%s: limit: %u\n", f->file_name, limit);
894 slba = ((offset >> ssw) / geo->nsect) * geo->nsect;
896 rprt = xnvme_znd_report_from_dev(dev, slba, limit, 0);
898 log_err("ioeng->report_zones(%s): xnvme_znd_report_from_dev(), err(%d)\n",
899 f->file_name, errno);
903 if (rprt->nentries != limit) {
904 log_err("ioeng->report_zones(%s): nentries != nr_zones\n", f->file_name);
908 if (offset > geo->tbytes) {
909 log_err("ioeng->report_zones(%s): out-of-bounds\n", f->file_name);
913 /* Transform the zone-report */
914 for (uint32_t idx = 0; idx < rprt->nentries; ++idx) {
915 struct xnvme_spec_znd_descr *descr = XNVME_ZND_REPORT_DESCR(rprt, idx);
917 zbdz[idx].start = descr->zslba << ssw;
918 zbdz[idx].len = lbafe->zsze << ssw;
919 zbdz[idx].capacity = descr->zcap << ssw;
920 zbdz[idx].wp = descr->wp << ssw;
923 case XNVME_SPEC_ZND_TYPE_SEQWR:
924 zbdz[idx].type = ZBD_ZONE_TYPE_SWR;
928 log_err("ioeng->report_zones(%s): invalid type for zone at offset(%zu)\n",
929 f->file_name, zbdz[idx].start);
935 case XNVME_SPEC_ZND_STATE_EMPTY:
936 zbdz[idx].cond = ZBD_ZONE_COND_EMPTY;
938 case XNVME_SPEC_ZND_STATE_IOPEN:
939 zbdz[idx].cond = ZBD_ZONE_COND_IMP_OPEN;
941 case XNVME_SPEC_ZND_STATE_EOPEN:
942 zbdz[idx].cond = ZBD_ZONE_COND_EXP_OPEN;
944 case XNVME_SPEC_ZND_STATE_CLOSED:
945 zbdz[idx].cond = ZBD_ZONE_COND_CLOSED;
947 case XNVME_SPEC_ZND_STATE_FULL:
948 zbdz[idx].cond = ZBD_ZONE_COND_FULL;
951 case XNVME_SPEC_ZND_STATE_RONLY:
952 case XNVME_SPEC_ZND_STATE_OFFLINE:
954 zbdz[idx].cond = ZBD_ZONE_COND_OFFLINE;
960 xnvme_buf_virt_free(rprt);
962 xnvme_dev_close(dev);
964 err_lock = pthread_mutex_unlock(&g_serialize);
966 log_err("ioeng->report_zones(): pthread_mutex_unlock(), err: %d\n", err_lock);
968 dprint(FD_ZBD, "err: %d, nr_zones: %d\n", err, (int)nr_zones);
970 return err ? err : (int)limit;
974 * NOTE: This function may get called before I/O engine initialization, that is,
975 * before ``_dev_open`` has been called and file-wrapping is setup. In such
976 * case it has to do ``_dev_open`` itself, and shut it down again once it is
977 * done resetting write pointer of zones.
979 static int xnvme_fioe_reset_wp(struct thread_data *td, struct fio_file *f, uint64_t offset,
982 struct xnvme_opts opts = xnvme_opts_from_fioe(td);
983 struct xnvme_fioe_data *xd = NULL;
984 struct xnvme_fioe_fwrap *fwrap = NULL;
985 struct xnvme_dev *dev = NULL;
986 const struct xnvme_geo *geo = NULL;
987 uint64_t first, last;
990 int err = 0, err_lock;
992 if (td->io_ops_data) {
993 xd = td->io_ops_data;
994 fwrap = &xd->files[f->fileno];
1003 err = pthread_mutex_lock(&g_serialize);
1005 log_err("ioeng->reset_wp(): pthread_mutex_lock(), err(%d)\n", err);
1009 dev = xnvme_dev_open(f->file_name, &opts);
1011 log_err("ioeng->reset_wp(): xnvme_dev_open(%s) failed, errno(%d)\n",
1012 f->file_name, errno);
1015 geo = xnvme_dev_get_geo(dev);
1016 ssw = xnvme_dev_get_ssw(dev);
1019 nsid = xnvme_dev_get_nsid(dev);
1021 first = ((offset >> ssw) / geo->nsect) * geo->nsect;
1022 last = (((offset + length) >> ssw) / geo->nsect) * geo->nsect;
1023 dprint(FD_ZBD, "first: 0x%lx, last: 0x%lx\n", first, last);
1025 for (uint64_t zslba = first; zslba < last; zslba += geo->nsect) {
1026 struct xnvme_cmd_ctx ctx = xnvme_cmd_ctx_from_dev(dev);
1028 if (zslba >= (geo->nsect * geo->nzone)) {
1029 log_err("ioeng->reset_wp(): out-of-bounds\n");
1034 err = xnvme_znd_mgmt_send(&ctx, nsid, zslba, false,
1035 XNVME_SPEC_ZND_CMD_MGMT_SEND_RESET, 0x0, NULL);
1036 if (err || xnvme_cmd_ctx_cpl_status(&ctx)) {
1037 err = err ? err : -EIO;
1038 log_err("ioeng->reset_wp(): err(%d), sc(%d)", err, ctx.cpl.status.sc);
1044 if (!td->io_ops_data) {
1045 xnvme_dev_close(dev);
1047 err_lock = pthread_mutex_unlock(&g_serialize);
1049 log_err("ioeng->reset_wp(): pthread_mutex_unlock(), err(%d)\n", err_lock);
1055 static int xnvme_fioe_fetch_ruhs(struct thread_data *td, struct fio_file *f,
1056 struct fio_ruhs_info *fruhs_info)
1058 struct xnvme_opts opts = xnvme_opts_from_fioe(td);
1059 struct xnvme_dev *dev;
1060 struct xnvme_spec_ruhs *ruhs;
1061 struct xnvme_cmd_ctx ctx;
1062 uint32_t ruhs_nbytes;
1064 int err = 0, err_lock;
1066 if (f->filetype != FIO_TYPE_CHAR && f->filetype != FIO_TYPE_FILE) {
1067 log_err("ioeng->fdp_ruhs(): ignoring filetype: %d\n", f->filetype);
1071 err = pthread_mutex_lock(&g_serialize);
1073 log_err("ioeng->fdp_ruhs(): pthread_mutex_lock(), err(%d)\n", err);
1077 dev = xnvme_dev_open(f->file_name, &opts);
1079 log_err("ioeng->fdp_ruhs(): xnvme_dev_open(%s) failed, errno: %d\n",
1080 f->file_name, errno);
1085 ruhs_nbytes = sizeof(*ruhs) + (FDP_MAX_RUHS * sizeof(struct xnvme_spec_ruhs_desc));
1086 ruhs = xnvme_buf_alloc(dev, ruhs_nbytes);
1091 memset(ruhs, 0, ruhs_nbytes);
1093 ctx = xnvme_cmd_ctx_from_dev(dev);
1094 nsid = xnvme_dev_get_nsid(dev);
1096 err = xnvme_nvm_mgmt_recv(&ctx, nsid, XNVME_SPEC_IO_MGMT_RECV_RUHS, 0, ruhs, ruhs_nbytes);
1098 if (err || xnvme_cmd_ctx_cpl_status(&ctx)) {
1099 err = err ? err : -EIO;
1100 log_err("ioeng->fdp_ruhs(): err(%d), sc(%d)", err, ctx.cpl.status.sc);
1104 fruhs_info->nr_ruhs = ruhs->nruhsd;
1105 for (uint32_t idx = 0; idx < fruhs_info->nr_ruhs; ++idx) {
1106 fruhs_info->plis[idx] = le16_to_cpu(ruhs->desc[idx].pi);
1110 xnvme_buf_free(dev, ruhs);
1112 xnvme_dev_close(dev);
1114 err_lock = pthread_mutex_unlock(&g_serialize);
1116 log_err("ioeng->fdp_ruhs(): pthread_mutex_unlock(), err(%d)\n", err_lock);
1121 static int xnvme_fioe_get_file_size(struct thread_data *td, struct fio_file *f)
1123 struct xnvme_opts opts = xnvme_opts_from_fioe(td);
1124 struct xnvme_dev *dev;
1127 if (fio_file_size_known(f))
1130 ret = pthread_mutex_lock(&g_serialize);
1132 log_err("ioeng->reset_wp(): pthread_mutex_lock(), err(%d)\n", ret);
1136 dev = xnvme_dev_open(f->file_name, &opts);
1138 log_err("%s: failed retrieving device handle, errno: %d\n", f->file_name, errno);
1143 f->real_file_size = xnvme_dev_get_geo(dev)->tbytes;
1144 fio_file_set_size_known(f);
1146 if (td->o.zone_mode == ZONE_MODE_ZBD)
1147 f->filetype = FIO_TYPE_BLOCK;
1150 xnvme_dev_close(dev);
1151 err = pthread_mutex_unlock(&g_serialize);
1153 log_err("ioeng->reset_wp(): pthread_mutex_unlock(), err(%d)\n", err);
1158 FIO_STATIC struct ioengine_ops ioengine = {
1160 .version = FIO_IOOPS_VERSION,
1162 .option_struct_size = sizeof(struct xnvme_fioe_options),
1163 .flags = FIO_DISKLESSIO | FIO_NODISKUTIL | FIO_NOEXTEND | FIO_MEMALIGN | FIO_RAWIO,
1165 .cleanup = xnvme_fioe_cleanup,
1166 .init = xnvme_fioe_init,
1168 .iomem_free = xnvme_fioe_iomem_free,
1169 .iomem_alloc = xnvme_fioe_iomem_alloc,
1171 .io_u_free = xnvme_fioe_io_u_free,
1172 .io_u_init = xnvme_fioe_io_u_init,
1174 .event = xnvme_fioe_event,
1175 .getevents = xnvme_fioe_getevents,
1176 .queue = xnvme_fioe_queue,
1178 .close_file = xnvme_fioe_close,
1179 .open_file = xnvme_fioe_open,
1180 .get_file_size = xnvme_fioe_get_file_size,
1182 .invalidate = xnvme_fioe_invalidate,
1183 .get_max_open_zones = xnvme_fioe_get_max_open_zones,
1184 .get_zoned_model = xnvme_fioe_get_zoned_model,
1185 .report_zones = xnvme_fioe_report_zones,
1186 .reset_wp = xnvme_fioe_reset_wp,
1188 .fdp_fetch_ruhs = xnvme_fioe_fetch_ruhs,
1191 static void fio_init fio_xnvme_register(void)
1193 register_ioengine(&ioengine);
1196 static void fio_exit fio_xnvme_unregister(void)
1198 unregister_ioengine(&ioengine);