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;
36 XNVME_STATIC_ASSERT(sizeof(struct xnvme_fioe_fwrap) == 64, "Incorrect size")
38 struct xnvme_fioe_data {
39 /* I/O completion queue */
42 /* # of iocq entries; incremented via getevents()/cb_pool() */
46 * # of errors; incremented when observed on completion via
47 * getevents()/cb_pool()
51 /* Controller which device/file to select */
55 /* Number of devices/files for which open() has been called */
57 /* Number of devices/files allocated in files[] */
64 struct xnvme_fioe_fwrap files[];
66 XNVME_STATIC_ASSERT(sizeof(struct xnvme_fioe_data) == 64, "Incorrect size")
68 struct xnvme_fioe_options {
71 unsigned int sqpoll_thread;
72 unsigned int xnvme_dev_nsid;
73 unsigned int xnvme_iovec;
79 char *xnvme_dev_subnqn;
82 static struct fio_option options[] = {
85 .lname = "High Priority",
86 .type = FIO_OPT_STR_SET,
87 .off1 = offsetof(struct xnvme_fioe_options, hipri),
88 .help = "Use polled IO completions",
89 .category = FIO_OPT_C_ENGINE,
90 .group = FIO_OPT_G_XNVME,
93 .name = "sqthread_poll",
94 .lname = "Kernel SQ thread polling",
95 .type = FIO_OPT_STR_SET,
96 .off1 = offsetof(struct xnvme_fioe_options, sqpoll_thread),
97 .help = "Offload submission/completion to kernel thread",
98 .category = FIO_OPT_C_ENGINE,
99 .group = FIO_OPT_G_XNVME,
103 .lname = "xNVMe Backend",
104 .type = FIO_OPT_STR_STORE,
105 .off1 = offsetof(struct xnvme_fioe_options, xnvme_be),
106 .help = "Select xNVMe backend [spdk,linux,fbsd]",
107 .category = FIO_OPT_C_ENGINE,
108 .group = FIO_OPT_G_XNVME,
112 .lname = "xNVMe Memory Backend",
113 .type = FIO_OPT_STR_STORE,
114 .off1 = offsetof(struct xnvme_fioe_options, xnvme_mem),
115 .help = "Select xNVMe memory backend",
116 .category = FIO_OPT_C_ENGINE,
117 .group = FIO_OPT_G_XNVME,
120 .name = "xnvme_async",
121 .lname = "xNVMe Asynchronous command-interface",
122 .type = FIO_OPT_STR_STORE,
123 .off1 = offsetof(struct xnvme_fioe_options, xnvme_async),
124 .help = "Select xNVMe async. interface: "
125 "[emu,thrpool,io_uring,io_uring_cmd,libaio,posix,vfio,nil]",
126 .category = FIO_OPT_C_ENGINE,
127 .group = FIO_OPT_G_XNVME,
130 .name = "xnvme_sync",
131 .lname = "xNVMe Synchronous. command-interface",
132 .type = FIO_OPT_STR_STORE,
133 .off1 = offsetof(struct xnvme_fioe_options, xnvme_sync),
134 .help = "Select xNVMe sync. interface: [nvme,psync,block]",
135 .category = FIO_OPT_C_ENGINE,
136 .group = FIO_OPT_G_XNVME,
139 .name = "xnvme_admin",
140 .lname = "xNVMe Admin command-interface",
141 .type = FIO_OPT_STR_STORE,
142 .off1 = offsetof(struct xnvme_fioe_options, xnvme_admin),
143 .help = "Select xNVMe admin. cmd-interface: [nvme,block]",
144 .category = FIO_OPT_C_ENGINE,
145 .group = FIO_OPT_G_XNVME,
148 .name = "xnvme_dev_nsid",
149 .lname = "xNVMe Namespace-Identifier, for user-space NVMe driver",
151 .off1 = offsetof(struct xnvme_fioe_options, xnvme_dev_nsid),
152 .help = "xNVMe Namespace-Identifier, for user-space NVMe driver",
153 .category = FIO_OPT_C_ENGINE,
154 .group = FIO_OPT_G_XNVME,
157 .name = "xnvme_dev_subnqn",
158 .lname = "Subsystem nqn for Fabrics",
159 .type = FIO_OPT_STR_STORE,
160 .off1 = offsetof(struct xnvme_fioe_options, xnvme_dev_subnqn),
161 .help = "Subsystem NQN for Fabrics",
162 .category = FIO_OPT_C_ENGINE,
163 .group = FIO_OPT_G_XNVME,
166 .name = "xnvme_iovec",
167 .lname = "Vectored IOs",
168 .type = FIO_OPT_STR_SET,
169 .off1 = offsetof(struct xnvme_fioe_options, xnvme_iovec),
170 .help = "Send vectored IOs",
171 .category = FIO_OPT_C_ENGINE,
172 .group = FIO_OPT_G_XNVME,
180 static void cb_pool(struct xnvme_cmd_ctx *ctx, void *cb_arg)
182 struct io_u *io_u = cb_arg;
183 struct xnvme_fioe_data *xd = io_u->mmap_data;
185 if (xnvme_cmd_ctx_cpl_status(ctx)) {
186 xnvme_cmd_ctx_pr(ctx, XNVME_PR_DEF);
191 xd->iocq[xd->completed++] = io_u;
192 xnvme_queue_put_cmd_ctx(ctx->async.queue, ctx);
195 static struct xnvme_opts xnvme_opts_from_fioe(struct thread_data *td)
197 struct xnvme_fioe_options *o = td->eo;
198 struct xnvme_opts opts = xnvme_opts_default();
200 opts.nsid = o->xnvme_dev_nsid;
201 opts.subnqn = o->xnvme_dev_subnqn;
202 opts.be = o->xnvme_be;
203 opts.mem = o->xnvme_mem;
204 opts.async = o->xnvme_async;
205 opts.sync = o->xnvme_sync;
206 opts.admin = o->xnvme_admin;
208 opts.poll_io = o->hipri;
209 opts.poll_sq = o->sqpoll_thread;
211 opts.direct = td->o.odirect;
216 static void _dev_close(struct thread_data *td, struct xnvme_fioe_fwrap *fwrap)
219 xnvme_queue_term(fwrap->queue);
221 xnvme_dev_close(fwrap->dev);
223 memset(fwrap, 0, sizeof(*fwrap));
226 static void xnvme_fioe_cleanup(struct thread_data *td)
228 struct xnvme_fioe_data *xd = NULL;
231 if (!td->io_ops_data)
234 xd = td->io_ops_data;
236 err = pthread_mutex_lock(&g_serialize);
238 log_err("ioeng->cleanup(): pthread_mutex_lock(), err(%d)\n", err);
239 /* NOTE: not returning here */
241 for (uint64_t i = 0; i < xd->nallocated; ++i)
242 _dev_close(td, &xd->files[i]);
245 err = pthread_mutex_unlock(&g_serialize);
247 log_err("ioeng->cleanup(): pthread_mutex_unlock(), err(%d)\n", err);
253 td->io_ops_data = NULL;
257 * Helper function setting up device handles as addressed by the naming
258 * convention of the given `fio_file` filename.
260 * Checks thread-options for explicit control of asynchronous implementation via
261 * the ``--xnvme_async={thrpool,emu,posix,io_uring,libaio,nil}``.
263 static int _dev_open(struct thread_data *td, struct fio_file *f)
265 struct xnvme_opts opts = xnvme_opts_from_fioe(td);
266 struct xnvme_fioe_data *xd = td->io_ops_data;
267 struct xnvme_fioe_fwrap *fwrap;
271 if (f->fileno > (int)xd->nallocated) {
272 log_err("ioeng->_dev_open(%s): invalid assumption\n", f->file_name);
276 fwrap = &xd->files[f->fileno];
278 err = pthread_mutex_lock(&g_serialize);
280 log_err("ioeng->_dev_open(%s): pthread_mutex_lock(), err(%d)\n", f->file_name,
285 fwrap->dev = xnvme_dev_open(f->file_name, &opts);
287 log_err("ioeng->_dev_open(%s): xnvme_dev_open(), err(%d)\n", f->file_name, errno);
290 fwrap->geo = xnvme_dev_get_geo(fwrap->dev);
292 if (xnvme_queue_init(fwrap->dev, td->o.iodepth, flags, &(fwrap->queue))) {
293 log_err("ioeng->_dev_open(%s): xnvme_queue_init(), err(?)\n", f->file_name);
296 xnvme_queue_set_cb(fwrap->queue, cb_pool, NULL);
298 fwrap->ssw = xnvme_dev_get_ssw(fwrap->dev);
299 fwrap->lba_nbytes = fwrap->geo->lba_nbytes;
302 fwrap->fio_file->filetype = FIO_TYPE_BLOCK;
303 fwrap->fio_file->real_file_size = fwrap->geo->tbytes;
304 fio_file_set_size_known(fwrap->fio_file);
306 err = pthread_mutex_unlock(&g_serialize);
308 log_err("ioeng->_dev_open(%s): pthread_mutex_unlock(), err(%d)\n", f->file_name,
314 xnvme_queue_term(fwrap->queue);
315 xnvme_dev_close(fwrap->dev);
317 err = pthread_mutex_unlock(&g_serialize);
319 log_err("ioeng->_dev_open(%s): pthread_mutex_unlock(), err(%d)\n", f->file_name,
325 static int xnvme_fioe_init(struct thread_data *td)
327 struct xnvme_fioe_data *xd = NULL;
331 if (!td->o.use_thread) {
332 log_err("ioeng->init(): --thread=1 is required\n");
336 /* Allocate xd and iocq */
337 xd = calloc(1, sizeof(*xd) + sizeof(*xd->files) * td->o.nr_files);
339 log_err("ioeng->init(): !calloc(), err(%d)\n", errno);
343 xd->iocq = calloc(td->o.iodepth, sizeof(struct io_u *));
346 log_err("ioeng->init(): !calloc(xd->iocq), err(%d)\n", errno);
350 xd->iovec = calloc(td->o.iodepth, sizeof(*xd->iovec));
354 log_err("ioeng->init(): !calloc(xd->iovec), err(%d)\n", errno);
359 td->io_ops_data = xd;
361 for_each_file(td, f, i)
363 if (_dev_open(td, f)) {
365 * Note: We are not freeing xd, iocq and iovec. This
366 * will be done as part of cleanup routine.
368 log_err("ioeng->init(): failed; _dev_open(%s)\n", f->file_name);
375 if (xd->nallocated != td->o.nr_files) {
376 log_err("ioeng->init(): failed; nallocated != td->o.nr_files\n");
383 /* NOTE: using the first device for buffer-allocators) */
384 static int xnvme_fioe_iomem_alloc(struct thread_data *td, size_t total_mem)
386 struct xnvme_fioe_data *xd = td->io_ops_data;
387 struct xnvme_fioe_fwrap *fwrap = &xd->files[0];
390 log_err("ioeng->iomem_alloc(): failed; no dev-handle\n");
394 td->orig_buffer = xnvme_buf_alloc(fwrap->dev, total_mem);
396 return td->orig_buffer == NULL;
399 /* NOTE: using the first device for buffer-allocators) */
400 static void xnvme_fioe_iomem_free(struct thread_data *td)
402 struct xnvme_fioe_data *xd = NULL;
403 struct xnvme_fioe_fwrap *fwrap = NULL;
405 if (!td->io_ops_data)
408 xd = td->io_ops_data;
409 fwrap = &xd->files[0];
412 log_err("ioeng->iomem_free(): failed no dev-handle\n");
416 xnvme_buf_free(fwrap->dev, td->orig_buffer);
419 static int xnvme_fioe_io_u_init(struct thread_data *td, struct io_u *io_u)
421 io_u->mmap_data = td->io_ops_data;
426 static void xnvme_fioe_io_u_free(struct thread_data *td, struct io_u *io_u)
428 io_u->mmap_data = NULL;
431 static struct io_u *xnvme_fioe_event(struct thread_data *td, int event)
433 struct xnvme_fioe_data *xd = td->io_ops_data;
436 assert((unsigned)event < xd->completed);
438 return xd->iocq[event];
441 static int xnvme_fioe_getevents(struct thread_data *td, unsigned int min, unsigned int max,
442 const struct timespec *t)
444 struct xnvme_fioe_data *xd = td->io_ops_data;
445 struct xnvme_fioe_fwrap *fwrap = NULL;
446 int nfiles = xd->nallocated;
449 if (xd->prev != -1 && ++xd->prev < nfiles) {
450 fwrap = &xd->files[xd->prev];
456 if (fwrap == NULL || xd->cur == nfiles) {
457 fwrap = &xd->files[0];
461 while (fwrap != NULL && xd->cur < nfiles && err >= 0) {
462 err = xnvme_queue_poke(fwrap->queue, max - xd->completed);
471 log_err("ioeng->getevents(): unhandled IO error\n");
476 if (xd->completed >= min) {
478 return xd->completed;
481 fwrap = &xd->files[xd->cur];
496 return xd->completed;
499 static enum fio_q_status xnvme_fioe_queue(struct thread_data *td, struct io_u *io_u)
501 struct xnvme_fioe_data *xd = td->io_ops_data;
502 struct xnvme_fioe_fwrap *fwrap;
503 struct xnvme_cmd_ctx *ctx;
508 bool vectored_io = ((struct xnvme_fioe_options *)td->eo)->xnvme_iovec;
509 uint32_t dir = io_u->dtype;
511 fio_ro_check(td, io_u);
513 fwrap = &xd->files[io_u->file->fileno];
514 nsid = xnvme_dev_get_nsid(fwrap->dev);
516 slba = io_u->offset >> fwrap->ssw;
517 nlb = (io_u->xfer_buflen >> fwrap->ssw) - 1;
519 ctx = xnvme_queue_get_cmd_ctx(fwrap->queue);
520 ctx->async.cb_arg = io_u;
522 ctx->cmd.common.nsid = nsid;
523 ctx->cmd.nvm.slba = slba;
524 ctx->cmd.nvm.nlb = nlb;
526 ctx->cmd.nvm.dtype = io_u->dtype;
527 ctx->cmd.nvm.cdw13.dspec = io_u->dspec;
530 switch (io_u->ddir) {
532 ctx->cmd.common.opcode = XNVME_SPEC_NVM_OPC_READ;
536 ctx->cmd.common.opcode = XNVME_SPEC_NVM_OPC_WRITE;
540 log_err("ioeng->queue(): ENOSYS: %u\n", io_u->ddir);
541 xnvme_queue_put_cmd_ctx(ctx->async.queue, ctx);
543 io_u->error = ENOSYS;
545 return FIO_Q_COMPLETED;
549 xd->iovec[io_u->index].iov_base = io_u->xfer_buf;
550 xd->iovec[io_u->index].iov_len = io_u->xfer_buflen;
552 err = xnvme_cmd_passv(ctx, &xd->iovec[io_u->index], 1, io_u->xfer_buflen, NULL, 0,
555 err = xnvme_cmd_pass(ctx, io_u->xfer_buf, io_u->xfer_buflen, NULL, 0);
563 xnvme_queue_put_cmd_ctx(ctx->async.queue, ctx);
567 log_err("ioeng->queue(): err: '%d'\n", err);
569 xnvme_queue_put_cmd_ctx(ctx->async.queue, ctx);
571 io_u->error = abs(err);
573 return FIO_Q_COMPLETED;
577 static int xnvme_fioe_close(struct thread_data *td, struct fio_file *f)
579 struct xnvme_fioe_data *xd = td->io_ops_data;
581 dprint(FD_FILE, "xnvme close %s -- nopen: %ld\n", f->file_name, xd->nopen);
588 static int xnvme_fioe_open(struct thread_data *td, struct fio_file *f)
590 struct xnvme_fioe_data *xd = td->io_ops_data;
592 dprint(FD_FILE, "xnvme open %s -- nopen: %ld\n", f->file_name, xd->nopen);
594 if (f->fileno > (int)xd->nallocated) {
595 log_err("ioeng->open(): f->fileno > xd->nallocated; invalid assumption\n");
598 if (xd->files[f->fileno].fio_file != f) {
599 log_err("ioeng->open(): fio_file != f; invalid assumption\n");
608 static int xnvme_fioe_invalidate(struct thread_data *td, struct fio_file *f)
610 /* Consider only doing this with be:spdk */
614 static int xnvme_fioe_get_max_open_zones(struct thread_data *td, struct fio_file *f,
615 unsigned int *max_open_zones)
617 struct xnvme_opts opts = xnvme_opts_from_fioe(td);
618 struct xnvme_dev *dev;
619 const struct xnvme_spec_znd_idfy_ns *zns;
620 int err = 0, err_lock;
622 if (f->filetype != FIO_TYPE_FILE && f->filetype != FIO_TYPE_BLOCK &&
623 f->filetype != FIO_TYPE_CHAR) {
624 log_info("ioeng->get_max_open_zoned(): ignoring filetype: %d\n", f->filetype);
627 err_lock = pthread_mutex_lock(&g_serialize);
629 log_err("ioeng->get_max_open_zones(): pthread_mutex_lock(), err(%d)\n", err_lock);
633 dev = xnvme_dev_open(f->file_name, &opts);
635 log_err("ioeng->get_max_open_zones(): xnvme_dev_open(), err(%d)\n", err_lock);
639 if (xnvme_dev_get_geo(dev)->type != XNVME_GEO_ZONED) {
645 zns = (void *)xnvme_dev_get_ns_css(dev);
647 log_err("ioeng->get_max_open_zones(): xnvme_dev_get_ns_css(), err(%d)\n", errno);
653 * intentional overflow as the value is zero-based and NVMe
654 * defines 0xFFFFFFFF as unlimited thus overflowing to 0 which
655 * is how fio indicates unlimited and otherwise just converting
658 *max_open_zones = zns->mor + 1;
661 xnvme_dev_close(dev);
662 err_lock = pthread_mutex_unlock(&g_serialize);
664 log_err("ioeng->get_max_open_zones(): pthread_mutex_unlock(), err(%d)\n",
671 * Currently, this function is called before of I/O engine initialization, so,
672 * we cannot consult the file-wrapping done when 'fioe' initializes.
673 * Instead we just open based on the given filename.
675 * TODO: unify the different setup methods, consider keeping the handle around,
676 * and consider how to support the --be option in this usecase
678 static int xnvme_fioe_get_zoned_model(struct thread_data *td, struct fio_file *f,
679 enum zbd_zoned_model *model)
681 struct xnvme_opts opts = xnvme_opts_from_fioe(td);
682 struct xnvme_dev *dev;
683 int err = 0, err_lock;
685 if (f->filetype != FIO_TYPE_FILE && f->filetype != FIO_TYPE_BLOCK &&
686 f->filetype != FIO_TYPE_CHAR) {
687 log_info("ioeng->get_zoned_model(): ignoring filetype: %d\n", f->filetype);
691 err = pthread_mutex_lock(&g_serialize);
693 log_err("ioeng->get_zoned_model(): pthread_mutex_lock(), err(%d)\n", err);
697 dev = xnvme_dev_open(f->file_name, &opts);
699 log_err("ioeng->get_zoned_model(): xnvme_dev_open(%s) failed, errno: %d\n",
700 f->file_name, errno);
705 switch (xnvme_dev_get_geo(dev)->type) {
706 case XNVME_GEO_UNKNOWN:
707 dprint(FD_ZBD, "%s: got 'unknown', assigning ZBD_NONE\n", f->file_name);
711 case XNVME_GEO_CONVENTIONAL:
712 dprint(FD_ZBD, "%s: got 'conventional', assigning ZBD_NONE\n", f->file_name);
716 case XNVME_GEO_ZONED:
717 dprint(FD_ZBD, "%s: got 'zoned', assigning ZBD_HOST_MANAGED\n", f->file_name);
718 *model = ZBD_HOST_MANAGED;
722 dprint(FD_ZBD, "%s: hit-default, assigning ZBD_NONE\n", f->file_name);
730 xnvme_dev_close(dev);
732 err_lock = pthread_mutex_unlock(&g_serialize);
734 log_err("ioeng->get_zoned_model(): pthread_mutex_unlock(), err(%d)\n", err_lock);
740 * Fills the given ``zbdz`` with at most ``nr_zones`` zone-descriptors.
742 * The implementation converts the NVMe Zoned Command Set log-pages for Zone
743 * descriptors into the Linux Kernel Zoned Block Report format.
745 * NOTE: This function is called before I/O engine initialization, that is,
746 * before ``_dev_open`` has been called and file-wrapping is setup. Thus is has
747 * to do the ``_dev_open`` itself, and shut it down again once it is done
748 * retrieving the log-pages and converting them to the report format.
750 * TODO: unify the different setup methods, consider keeping the handle around,
751 * and consider how to support the --async option in this usecase
753 static int xnvme_fioe_report_zones(struct thread_data *td, struct fio_file *f, uint64_t offset,
754 struct zbd_zone *zbdz, unsigned int nr_zones)
756 struct xnvme_opts opts = xnvme_opts_from_fioe(td);
757 const struct xnvme_spec_znd_idfy_lbafe *lbafe = NULL;
758 struct xnvme_dev *dev = NULL;
759 const struct xnvme_geo *geo = NULL;
760 struct xnvme_znd_report *rprt = NULL;
763 unsigned int limit = 0;
764 int err = 0, err_lock;
766 dprint(FD_ZBD, "%s: report_zones() offset: %zu, nr_zones: %u\n", f->file_name, offset,
769 err = pthread_mutex_lock(&g_serialize);
771 log_err("ioeng->report_zones(%s): pthread_mutex_lock(), err(%d)\n", f->file_name,
776 dev = xnvme_dev_open(f->file_name, &opts);
778 log_err("ioeng->report_zones(%s): xnvme_dev_open(), err(%d)\n", f->file_name,
783 geo = xnvme_dev_get_geo(dev);
784 ssw = xnvme_dev_get_ssw(dev);
785 lbafe = xnvme_znd_dev_get_lbafe(dev);
787 limit = nr_zones > geo->nzone ? geo->nzone : nr_zones;
789 dprint(FD_ZBD, "%s: limit: %u\n", f->file_name, limit);
791 slba = ((offset >> ssw) / geo->nsect) * geo->nsect;
793 rprt = xnvme_znd_report_from_dev(dev, slba, limit, 0);
795 log_err("ioeng->report_zones(%s): xnvme_znd_report_from_dev(), err(%d)\n",
796 f->file_name, errno);
800 if (rprt->nentries != limit) {
801 log_err("ioeng->report_zones(%s): nentries != nr_zones\n", f->file_name);
805 if (offset > geo->tbytes) {
806 log_err("ioeng->report_zones(%s): out-of-bounds\n", f->file_name);
810 /* Transform the zone-report */
811 for (uint32_t idx = 0; idx < rprt->nentries; ++idx) {
812 struct xnvme_spec_znd_descr *descr = XNVME_ZND_REPORT_DESCR(rprt, idx);
814 zbdz[idx].start = descr->zslba << ssw;
815 zbdz[idx].len = lbafe->zsze << ssw;
816 zbdz[idx].capacity = descr->zcap << ssw;
817 zbdz[idx].wp = descr->wp << ssw;
820 case XNVME_SPEC_ZND_TYPE_SEQWR:
821 zbdz[idx].type = ZBD_ZONE_TYPE_SWR;
825 log_err("ioeng->report_zones(%s): invalid type for zone at offset(%zu)\n",
826 f->file_name, zbdz[idx].start);
832 case XNVME_SPEC_ZND_STATE_EMPTY:
833 zbdz[idx].cond = ZBD_ZONE_COND_EMPTY;
835 case XNVME_SPEC_ZND_STATE_IOPEN:
836 zbdz[idx].cond = ZBD_ZONE_COND_IMP_OPEN;
838 case XNVME_SPEC_ZND_STATE_EOPEN:
839 zbdz[idx].cond = ZBD_ZONE_COND_EXP_OPEN;
841 case XNVME_SPEC_ZND_STATE_CLOSED:
842 zbdz[idx].cond = ZBD_ZONE_COND_CLOSED;
844 case XNVME_SPEC_ZND_STATE_FULL:
845 zbdz[idx].cond = ZBD_ZONE_COND_FULL;
848 case XNVME_SPEC_ZND_STATE_RONLY:
849 case XNVME_SPEC_ZND_STATE_OFFLINE:
851 zbdz[idx].cond = ZBD_ZONE_COND_OFFLINE;
857 xnvme_buf_virt_free(rprt);
859 xnvme_dev_close(dev);
861 err_lock = pthread_mutex_unlock(&g_serialize);
863 log_err("ioeng->report_zones(): pthread_mutex_unlock(), err: %d\n", err_lock);
865 dprint(FD_ZBD, "err: %d, nr_zones: %d\n", err, (int)nr_zones);
867 return err ? err : (int)limit;
871 * NOTE: This function may get called before I/O engine initialization, that is,
872 * before ``_dev_open`` has been called and file-wrapping is setup. In such
873 * case it has to do ``_dev_open`` itself, and shut it down again once it is
874 * done resetting write pointer of zones.
876 static int xnvme_fioe_reset_wp(struct thread_data *td, struct fio_file *f, uint64_t offset,
879 struct xnvme_opts opts = xnvme_opts_from_fioe(td);
880 struct xnvme_fioe_data *xd = NULL;
881 struct xnvme_fioe_fwrap *fwrap = NULL;
882 struct xnvme_dev *dev = NULL;
883 const struct xnvme_geo *geo = NULL;
884 uint64_t first, last;
887 int err = 0, err_lock;
889 if (td->io_ops_data) {
890 xd = td->io_ops_data;
891 fwrap = &xd->files[f->fileno];
900 err = pthread_mutex_lock(&g_serialize);
902 log_err("ioeng->reset_wp(): pthread_mutex_lock(), err(%d)\n", err);
906 dev = xnvme_dev_open(f->file_name, &opts);
908 log_err("ioeng->reset_wp(): xnvme_dev_open(%s) failed, errno(%d)\n",
909 f->file_name, errno);
912 geo = xnvme_dev_get_geo(dev);
913 ssw = xnvme_dev_get_ssw(dev);
916 nsid = xnvme_dev_get_nsid(dev);
918 first = ((offset >> ssw) / geo->nsect) * geo->nsect;
919 last = (((offset + length) >> ssw) / geo->nsect) * geo->nsect;
920 dprint(FD_ZBD, "first: 0x%lx, last: 0x%lx\n", first, last);
922 for (uint64_t zslba = first; zslba < last; zslba += geo->nsect) {
923 struct xnvme_cmd_ctx ctx = xnvme_cmd_ctx_from_dev(dev);
925 if (zslba >= (geo->nsect * geo->nzone)) {
926 log_err("ioeng->reset_wp(): out-of-bounds\n");
931 err = xnvme_znd_mgmt_send(&ctx, nsid, zslba, false,
932 XNVME_SPEC_ZND_CMD_MGMT_SEND_RESET, 0x0, NULL);
933 if (err || xnvme_cmd_ctx_cpl_status(&ctx)) {
934 err = err ? err : -EIO;
935 log_err("ioeng->reset_wp(): err(%d), sc(%d)", err, ctx.cpl.status.sc);
941 if (!td->io_ops_data) {
942 xnvme_dev_close(dev);
944 err_lock = pthread_mutex_unlock(&g_serialize);
946 log_err("ioeng->reset_wp(): pthread_mutex_unlock(), err(%d)\n", err_lock);
952 static int xnvme_fioe_fetch_ruhs(struct thread_data *td, struct fio_file *f,
953 struct fio_ruhs_info *fruhs_info)
955 struct xnvme_opts opts = xnvme_opts_from_fioe(td);
956 struct xnvme_dev *dev;
957 struct xnvme_spec_ruhs *ruhs;
958 struct xnvme_cmd_ctx ctx;
959 uint32_t ruhs_nbytes;
961 int err = 0, err_lock;
963 if (f->filetype != FIO_TYPE_CHAR && f->filetype != FIO_TYPE_FILE) {
964 log_err("ioeng->fdp_ruhs(): ignoring filetype: %d\n", f->filetype);
968 err = pthread_mutex_lock(&g_serialize);
970 log_err("ioeng->fdp_ruhs(): pthread_mutex_lock(), err(%d)\n", err);
974 dev = xnvme_dev_open(f->file_name, &opts);
976 log_err("ioeng->fdp_ruhs(): xnvme_dev_open(%s) failed, errno: %d\n",
977 f->file_name, errno);
982 ruhs_nbytes = sizeof(*ruhs) + (FDP_MAX_RUHS * sizeof(struct xnvme_spec_ruhs_desc));
983 ruhs = xnvme_buf_alloc(dev, ruhs_nbytes);
988 memset(ruhs, 0, ruhs_nbytes);
990 ctx = xnvme_cmd_ctx_from_dev(dev);
991 nsid = xnvme_dev_get_nsid(dev);
993 err = xnvme_nvm_mgmt_recv(&ctx, nsid, XNVME_SPEC_IO_MGMT_RECV_RUHS, 0, ruhs, ruhs_nbytes);
995 if (err || xnvme_cmd_ctx_cpl_status(&ctx)) {
996 err = err ? err : -EIO;
997 log_err("ioeng->fdp_ruhs(): err(%d), sc(%d)", err, ctx.cpl.status.sc);
1001 fruhs_info->nr_ruhs = ruhs->nruhsd;
1002 for (uint32_t idx = 0; idx < fruhs_info->nr_ruhs; ++idx) {
1003 fruhs_info->plis[idx] = le16_to_cpu(ruhs->desc[idx].pi);
1007 xnvme_buf_free(dev, ruhs);
1009 xnvme_dev_close(dev);
1011 err_lock = pthread_mutex_unlock(&g_serialize);
1013 log_err("ioeng->fdp_ruhs(): pthread_mutex_unlock(), err(%d)\n", err_lock);
1018 static int xnvme_fioe_get_file_size(struct thread_data *td, struct fio_file *f)
1020 struct xnvme_opts opts = xnvme_opts_from_fioe(td);
1021 struct xnvme_dev *dev;
1024 if (fio_file_size_known(f))
1027 ret = pthread_mutex_lock(&g_serialize);
1029 log_err("ioeng->reset_wp(): pthread_mutex_lock(), err(%d)\n", ret);
1033 dev = xnvme_dev_open(f->file_name, &opts);
1035 log_err("%s: failed retrieving device handle, errno: %d\n", f->file_name, errno);
1040 f->real_file_size = xnvme_dev_get_geo(dev)->tbytes;
1041 fio_file_set_size_known(f);
1043 if (td->o.zone_mode == ZONE_MODE_ZBD)
1044 f->filetype = FIO_TYPE_BLOCK;
1047 xnvme_dev_close(dev);
1048 err = pthread_mutex_unlock(&g_serialize);
1050 log_err("ioeng->reset_wp(): pthread_mutex_unlock(), err(%d)\n", err);
1055 FIO_STATIC struct ioengine_ops ioengine = {
1057 .version = FIO_IOOPS_VERSION,
1059 .option_struct_size = sizeof(struct xnvme_fioe_options),
1060 .flags = FIO_DISKLESSIO | FIO_NODISKUTIL | FIO_NOEXTEND | FIO_MEMALIGN | FIO_RAWIO,
1062 .cleanup = xnvme_fioe_cleanup,
1063 .init = xnvme_fioe_init,
1065 .iomem_free = xnvme_fioe_iomem_free,
1066 .iomem_alloc = xnvme_fioe_iomem_alloc,
1068 .io_u_free = xnvme_fioe_io_u_free,
1069 .io_u_init = xnvme_fioe_io_u_init,
1071 .event = xnvme_fioe_event,
1072 .getevents = xnvme_fioe_getevents,
1073 .queue = xnvme_fioe_queue,
1075 .close_file = xnvme_fioe_close,
1076 .open_file = xnvme_fioe_open,
1077 .get_file_size = xnvme_fioe_get_file_size,
1079 .invalidate = xnvme_fioe_invalidate,
1080 .get_max_open_zones = xnvme_fioe_get_max_open_zones,
1081 .get_zoned_model = xnvme_fioe_get_zoned_model,
1082 .report_zones = xnvme_fioe_report_zones,
1083 .reset_wp = xnvme_fioe_reset_wp,
1085 .fdp_fetch_ruhs = xnvme_fioe_fetch_ruhs,
1088 static void fio_init fio_xnvme_register(void)
1090 register_ioengine(&ioengine);
1093 static void fio_exit fio_xnvme_unregister(void)
1095 unregister_ioengine(&ioengine);