4 * IO engine using the xNVMe C API.
6 * See: http://xnvme.io/
8 * SPDX-License-Identifier: Apache-2.0
13 #include <libxnvme_libconf.h>
14 #include <libxnvme_nvm.h>
15 #include <libxnvme_znd.h>
16 #include <libxnvme_spec_fs.h>
18 #include "zbd_types.h"
21 static pthread_mutex_t g_serialize = PTHREAD_MUTEX_INITIALIZER;
23 struct xnvme_fioe_fwrap {
24 /* fio file representation */
25 struct fio_file *fio_file;
27 /* xNVMe device handle */
28 struct xnvme_dev *dev;
29 /* xNVMe device geometry */
30 const struct xnvme_geo *geo;
32 struct xnvme_queue *queue;
39 XNVME_STATIC_ASSERT(sizeof(struct xnvme_fioe_fwrap) == 64, "Incorrect size")
41 struct xnvme_fioe_data {
42 /* I/O completion queue */
45 /* # of iocq entries; incremented via getevents()/cb_pool() */
49 * # of errors; incremented when observed on completion via
50 * getevents()/cb_pool()
54 /* Controller which device/file to select */
58 /* Number of devices/files for which open() has been called */
60 /* Number of devices/files allocated in files[] */
67 struct xnvme_fioe_fwrap files[];
69 XNVME_STATIC_ASSERT(sizeof(struct xnvme_fioe_data) == 64, "Incorrect size")
71 struct xnvme_fioe_options {
74 unsigned int sqpoll_thread;
75 unsigned int xnvme_dev_nsid;
76 unsigned int xnvme_iovec;
82 char *xnvme_dev_subnqn;
85 static struct fio_option options[] = {
88 .lname = "High Priority",
89 .type = FIO_OPT_STR_SET,
90 .off1 = offsetof(struct xnvme_fioe_options, hipri),
91 .help = "Use polled IO completions",
92 .category = FIO_OPT_C_ENGINE,
93 .group = FIO_OPT_G_XNVME,
96 .name = "sqthread_poll",
97 .lname = "Kernel SQ thread polling",
98 .type = FIO_OPT_STR_SET,
99 .off1 = offsetof(struct xnvme_fioe_options, sqpoll_thread),
100 .help = "Offload submission/completion to kernel thread",
101 .category = FIO_OPT_C_ENGINE,
102 .group = FIO_OPT_G_XNVME,
106 .lname = "xNVMe Backend",
107 .type = FIO_OPT_STR_STORE,
108 .off1 = offsetof(struct xnvme_fioe_options, xnvme_be),
109 .help = "Select xNVMe backend [spdk,linux,fbsd]",
110 .category = FIO_OPT_C_ENGINE,
111 .group = FIO_OPT_G_XNVME,
115 .lname = "xNVMe Memory Backend",
116 .type = FIO_OPT_STR_STORE,
117 .off1 = offsetof(struct xnvme_fioe_options, xnvme_mem),
118 .help = "Select xNVMe memory backend",
119 .category = FIO_OPT_C_ENGINE,
120 .group = FIO_OPT_G_XNVME,
123 .name = "xnvme_async",
124 .lname = "xNVMe Asynchronous command-interface",
125 .type = FIO_OPT_STR_STORE,
126 .off1 = offsetof(struct xnvme_fioe_options, xnvme_async),
127 .help = "Select xNVMe async. interface: "
128 "[emu,thrpool,io_uring,io_uring_cmd,libaio,posix,vfio,nil]",
129 .category = FIO_OPT_C_ENGINE,
130 .group = FIO_OPT_G_XNVME,
133 .name = "xnvme_sync",
134 .lname = "xNVMe Synchronous. command-interface",
135 .type = FIO_OPT_STR_STORE,
136 .off1 = offsetof(struct xnvme_fioe_options, xnvme_sync),
137 .help = "Select xNVMe sync. interface: [nvme,psync,block]",
138 .category = FIO_OPT_C_ENGINE,
139 .group = FIO_OPT_G_XNVME,
142 .name = "xnvme_admin",
143 .lname = "xNVMe Admin command-interface",
144 .type = FIO_OPT_STR_STORE,
145 .off1 = offsetof(struct xnvme_fioe_options, xnvme_admin),
146 .help = "Select xNVMe admin. cmd-interface: [nvme,block]",
147 .category = FIO_OPT_C_ENGINE,
148 .group = FIO_OPT_G_XNVME,
151 .name = "xnvme_dev_nsid",
152 .lname = "xNVMe Namespace-Identifier, for user-space NVMe driver",
154 .off1 = offsetof(struct xnvme_fioe_options, xnvme_dev_nsid),
155 .help = "xNVMe Namespace-Identifier, for user-space NVMe driver",
156 .category = FIO_OPT_C_ENGINE,
157 .group = FIO_OPT_G_XNVME,
160 .name = "xnvme_dev_subnqn",
161 .lname = "Subsystem nqn for Fabrics",
162 .type = FIO_OPT_STR_STORE,
163 .off1 = offsetof(struct xnvme_fioe_options, xnvme_dev_subnqn),
164 .help = "Subsystem NQN for Fabrics",
165 .category = FIO_OPT_C_ENGINE,
166 .group = FIO_OPT_G_XNVME,
169 .name = "xnvme_iovec",
170 .lname = "Vectored IOs",
171 .type = FIO_OPT_STR_SET,
172 .off1 = offsetof(struct xnvme_fioe_options, xnvme_iovec),
173 .help = "Send vectored IOs",
174 .category = FIO_OPT_C_ENGINE,
175 .group = FIO_OPT_G_XNVME,
183 static void cb_pool(struct xnvme_cmd_ctx *ctx, void *cb_arg)
185 struct io_u *io_u = cb_arg;
186 struct xnvme_fioe_data *xd = io_u->mmap_data;
188 if (xnvme_cmd_ctx_cpl_status(ctx)) {
189 xnvme_cmd_ctx_pr(ctx, XNVME_PR_DEF);
194 xd->iocq[xd->completed++] = io_u;
195 xnvme_queue_put_cmd_ctx(ctx->async.queue, ctx);
198 static struct xnvme_opts xnvme_opts_from_fioe(struct thread_data *td)
200 struct xnvme_fioe_options *o = td->eo;
201 struct xnvme_opts opts = xnvme_opts_default();
203 opts.nsid = o->xnvme_dev_nsid;
204 opts.subnqn = o->xnvme_dev_subnqn;
205 opts.be = o->xnvme_be;
206 opts.mem = o->xnvme_mem;
207 opts.async = o->xnvme_async;
208 opts.sync = o->xnvme_sync;
209 opts.admin = o->xnvme_admin;
211 opts.poll_io = o->hipri;
212 opts.poll_sq = o->sqpoll_thread;
214 opts.direct = td->o.odirect;
219 static void _dev_close(struct thread_data *td, struct xnvme_fioe_fwrap *fwrap)
222 xnvme_queue_term(fwrap->queue);
224 xnvme_dev_close(fwrap->dev);
226 memset(fwrap, 0, sizeof(*fwrap));
229 static void xnvme_fioe_cleanup(struct thread_data *td)
231 struct xnvme_fioe_data *xd = NULL;
234 if (!td->io_ops_data)
237 xd = td->io_ops_data;
239 err = pthread_mutex_lock(&g_serialize);
241 log_err("ioeng->cleanup(): pthread_mutex_lock(), err(%d)\n", err);
242 /* NOTE: not returning here */
244 for (uint64_t i = 0; i < xd->nallocated; ++i)
245 _dev_close(td, &xd->files[i]);
248 err = pthread_mutex_unlock(&g_serialize);
250 log_err("ioeng->cleanup(): pthread_mutex_unlock(), err(%d)\n", err);
256 td->io_ops_data = NULL;
260 * Helper function setting up device handles as addressed by the naming
261 * convention of the given `fio_file` filename.
263 * Checks thread-options for explicit control of asynchronous implementation via
264 * the ``--xnvme_async={thrpool,emu,posix,io_uring,libaio,nil}``.
266 static int _dev_open(struct thread_data *td, struct fio_file *f)
268 struct xnvme_opts opts = xnvme_opts_from_fioe(td);
269 struct xnvme_fioe_data *xd = td->io_ops_data;
270 struct xnvme_fioe_fwrap *fwrap;
274 if (f->fileno > (int)xd->nallocated) {
275 log_err("ioeng->_dev_open(%s): invalid assumption\n", f->file_name);
279 fwrap = &xd->files[f->fileno];
281 err = pthread_mutex_lock(&g_serialize);
283 log_err("ioeng->_dev_open(%s): pthread_mutex_lock(), err(%d)\n", f->file_name,
288 fwrap->dev = xnvme_dev_open(f->file_name, &opts);
290 log_err("ioeng->_dev_open(%s): xnvme_dev_open(), err(%d)\n", f->file_name, errno);
293 fwrap->geo = xnvme_dev_get_geo(fwrap->dev);
295 if (xnvme_queue_init(fwrap->dev, td->o.iodepth, flags, &(fwrap->queue))) {
296 log_err("ioeng->_dev_open(%s): xnvme_queue_init(), err(?)\n", f->file_name);
299 xnvme_queue_set_cb(fwrap->queue, cb_pool, NULL);
301 fwrap->ssw = xnvme_dev_get_ssw(fwrap->dev);
302 fwrap->lba_nbytes = fwrap->geo->lba_nbytes;
305 fwrap->fio_file->filetype = FIO_TYPE_BLOCK;
306 fwrap->fio_file->real_file_size = fwrap->geo->tbytes;
307 fio_file_set_size_known(fwrap->fio_file);
309 err = pthread_mutex_unlock(&g_serialize);
311 log_err("ioeng->_dev_open(%s): pthread_mutex_unlock(), err(%d)\n", f->file_name,
317 xnvme_queue_term(fwrap->queue);
318 xnvme_dev_close(fwrap->dev);
320 err = pthread_mutex_unlock(&g_serialize);
322 log_err("ioeng->_dev_open(%s): pthread_mutex_unlock(), err(%d)\n", f->file_name,
328 static int xnvme_fioe_init(struct thread_data *td)
330 struct xnvme_fioe_data *xd = NULL;
334 if (!td->o.use_thread) {
335 log_err("ioeng->init(): --thread=1 is required\n");
339 /* Allocate xd and iocq */
340 xd = calloc(1, sizeof(*xd) + sizeof(*xd->files) * td->o.nr_files);
342 log_err("ioeng->init(): !calloc(), err(%d)\n", errno);
346 xd->iocq = calloc(td->o.iodepth, sizeof(struct io_u *));
349 log_err("ioeng->init(): !calloc(xd->iocq), err(%d)\n", errno);
353 xd->iovec = calloc(td->o.iodepth, sizeof(*xd->iovec));
357 log_err("ioeng->init(): !calloc(xd->iovec), err(%d)\n", errno);
362 td->io_ops_data = xd;
364 for_each_file(td, f, i)
366 if (_dev_open(td, f)) {
368 * Note: We are not freeing xd, iocq and iovec. This
369 * will be done as part of cleanup routine.
371 log_err("ioeng->init(): failed; _dev_open(%s)\n", f->file_name);
378 if (xd->nallocated != td->o.nr_files) {
379 log_err("ioeng->init(): failed; nallocated != td->o.nr_files\n");
386 /* NOTE: using the first device for buffer-allocators) */
387 static int xnvme_fioe_iomem_alloc(struct thread_data *td, size_t total_mem)
389 struct xnvme_fioe_data *xd = td->io_ops_data;
390 struct xnvme_fioe_fwrap *fwrap = &xd->files[0];
393 log_err("ioeng->iomem_alloc(): failed; no dev-handle\n");
397 td->orig_buffer = xnvme_buf_alloc(fwrap->dev, total_mem);
399 return td->orig_buffer == NULL;
402 /* NOTE: using the first device for buffer-allocators) */
403 static void xnvme_fioe_iomem_free(struct thread_data *td)
405 struct xnvme_fioe_data *xd = NULL;
406 struct xnvme_fioe_fwrap *fwrap = NULL;
408 if (!td->io_ops_data)
411 xd = td->io_ops_data;
412 fwrap = &xd->files[0];
415 log_err("ioeng->iomem_free(): failed no dev-handle\n");
419 xnvme_buf_free(fwrap->dev, td->orig_buffer);
422 static int xnvme_fioe_io_u_init(struct thread_data *td, struct io_u *io_u)
424 io_u->mmap_data = td->io_ops_data;
429 static void xnvme_fioe_io_u_free(struct thread_data *td, struct io_u *io_u)
431 io_u->mmap_data = NULL;
434 static struct io_u *xnvme_fioe_event(struct thread_data *td, int event)
436 struct xnvme_fioe_data *xd = td->io_ops_data;
439 assert((unsigned)event < xd->completed);
441 return xd->iocq[event];
444 static int xnvme_fioe_getevents(struct thread_data *td, unsigned int min, unsigned int max,
445 const struct timespec *t)
447 struct xnvme_fioe_data *xd = td->io_ops_data;
448 struct xnvme_fioe_fwrap *fwrap = NULL;
449 int nfiles = xd->nallocated;
452 if (xd->prev != -1 && ++xd->prev < nfiles) {
453 fwrap = &xd->files[xd->prev];
459 if (fwrap == NULL || xd->cur == nfiles) {
460 fwrap = &xd->files[0];
464 while (fwrap != NULL && xd->cur < nfiles && err >= 0) {
465 err = xnvme_queue_poke(fwrap->queue, max - xd->completed);
474 log_err("ioeng->getevents(): unhandled IO error\n");
479 if (xd->completed >= min) {
481 return xd->completed;
484 fwrap = &xd->files[xd->cur];
499 return xd->completed;
502 static enum fio_q_status xnvme_fioe_queue(struct thread_data *td, struct io_u *io_u)
504 struct xnvme_fioe_data *xd = td->io_ops_data;
505 struct xnvme_fioe_fwrap *fwrap;
506 struct xnvme_cmd_ctx *ctx;
511 bool vectored_io = ((struct xnvme_fioe_options *)td->eo)->xnvme_iovec;
513 fio_ro_check(td, io_u);
515 fwrap = &xd->files[io_u->file->fileno];
516 nsid = xnvme_dev_get_nsid(fwrap->dev);
518 slba = io_u->offset >> fwrap->ssw;
519 nlb = (io_u->xfer_buflen >> fwrap->ssw) - 1;
521 ctx = xnvme_queue_get_cmd_ctx(fwrap->queue);
522 ctx->async.cb_arg = io_u;
524 ctx->cmd.common.nsid = nsid;
525 ctx->cmd.nvm.slba = slba;
526 ctx->cmd.nvm.nlb = nlb;
528 switch (io_u->ddir) {
530 ctx->cmd.common.opcode = XNVME_SPEC_NVM_OPC_READ;
534 ctx->cmd.common.opcode = XNVME_SPEC_NVM_OPC_WRITE;
538 log_err("ioeng->queue(): ENOSYS: %u\n", io_u->ddir);
539 xnvme_queue_put_cmd_ctx(ctx->async.queue, ctx);
541 io_u->error = ENOSYS;
543 return FIO_Q_COMPLETED;
547 xd->iovec[io_u->index].iov_base = io_u->xfer_buf;
548 xd->iovec[io_u->index].iov_len = io_u->xfer_buflen;
550 err = xnvme_cmd_passv(ctx, &xd->iovec[io_u->index], 1, io_u->xfer_buflen, NULL, 0,
553 err = xnvme_cmd_pass(ctx, io_u->xfer_buf, io_u->xfer_buflen, NULL, 0);
561 xnvme_queue_put_cmd_ctx(ctx->async.queue, ctx);
565 log_err("ioeng->queue(): err: '%d'\n", err);
567 xnvme_queue_put_cmd_ctx(ctx->async.queue, ctx);
569 io_u->error = abs(err);
571 return FIO_Q_COMPLETED;
575 static int xnvme_fioe_close(struct thread_data *td, struct fio_file *f)
577 struct xnvme_fioe_data *xd = td->io_ops_data;
579 dprint(FD_FILE, "xnvme close %s -- nopen: %ld\n", f->file_name, xd->nopen);
586 static int xnvme_fioe_open(struct thread_data *td, struct fio_file *f)
588 struct xnvme_fioe_data *xd = td->io_ops_data;
590 dprint(FD_FILE, "xnvme open %s -- nopen: %ld\n", f->file_name, xd->nopen);
592 if (f->fileno > (int)xd->nallocated) {
593 log_err("ioeng->open(): f->fileno > xd->nallocated; invalid assumption\n");
596 if (xd->files[f->fileno].fio_file != f) {
597 log_err("ioeng->open(): fio_file != f; invalid assumption\n");
606 static int xnvme_fioe_invalidate(struct thread_data *td, struct fio_file *f)
608 /* Consider only doing this with be:spdk */
612 static int xnvme_fioe_get_max_open_zones(struct thread_data *td, struct fio_file *f,
613 unsigned int *max_open_zones)
615 struct xnvme_opts opts = xnvme_opts_from_fioe(td);
616 struct xnvme_dev *dev;
617 const struct xnvme_spec_znd_idfy_ns *zns;
618 int err = 0, err_lock;
620 if (f->filetype != FIO_TYPE_FILE && f->filetype != FIO_TYPE_BLOCK &&
621 f->filetype != FIO_TYPE_CHAR) {
622 log_info("ioeng->get_max_open_zoned(): ignoring filetype: %d\n", f->filetype);
625 err_lock = pthread_mutex_lock(&g_serialize);
627 log_err("ioeng->get_max_open_zones(): pthread_mutex_lock(), err(%d)\n", err_lock);
631 dev = xnvme_dev_open(f->file_name, &opts);
633 log_err("ioeng->get_max_open_zones(): xnvme_dev_open(), err(%d)\n", err_lock);
637 if (xnvme_dev_get_geo(dev)->type != XNVME_GEO_ZONED) {
643 zns = (void *)xnvme_dev_get_ns_css(dev);
645 log_err("ioeng->get_max_open_zones(): xnvme_dev_get_ns_css(), err(%d)\n", errno);
651 * intentional overflow as the value is zero-based and NVMe
652 * defines 0xFFFFFFFF as unlimited thus overflowing to 0 which
653 * is how fio indicates unlimited and otherwise just converting
656 *max_open_zones = zns->mor + 1;
659 xnvme_dev_close(dev);
660 err_lock = pthread_mutex_unlock(&g_serialize);
662 log_err("ioeng->get_max_open_zones(): pthread_mutex_unlock(), err(%d)\n",
669 * Currently, this function is called before of I/O engine initialization, so,
670 * we cannot consult the file-wrapping done when 'fioe' initializes.
671 * Instead we just open based on the given filename.
673 * TODO: unify the different setup methods, consider keeping the handle around,
674 * and consider how to support the --be option in this usecase
676 static int xnvme_fioe_get_zoned_model(struct thread_data *td, struct fio_file *f,
677 enum zbd_zoned_model *model)
679 struct xnvme_opts opts = xnvme_opts_from_fioe(td);
680 struct xnvme_dev *dev;
681 int err = 0, err_lock;
683 if (f->filetype != FIO_TYPE_FILE && f->filetype != FIO_TYPE_BLOCK &&
684 f->filetype != FIO_TYPE_CHAR) {
685 log_info("ioeng->get_zoned_model(): ignoring filetype: %d\n", f->filetype);
689 err = pthread_mutex_lock(&g_serialize);
691 log_err("ioeng->get_zoned_model(): pthread_mutex_lock(), err(%d)\n", err);
695 dev = xnvme_dev_open(f->file_name, &opts);
697 log_err("ioeng->get_zoned_model(): xnvme_dev_open(%s) failed, errno: %d\n",
698 f->file_name, errno);
703 switch (xnvme_dev_get_geo(dev)->type) {
704 case XNVME_GEO_UNKNOWN:
705 dprint(FD_ZBD, "%s: got 'unknown', assigning ZBD_NONE\n", f->file_name);
709 case XNVME_GEO_CONVENTIONAL:
710 dprint(FD_ZBD, "%s: got 'conventional', assigning ZBD_NONE\n", f->file_name);
714 case XNVME_GEO_ZONED:
715 dprint(FD_ZBD, "%s: got 'zoned', assigning ZBD_HOST_MANAGED\n", f->file_name);
716 *model = ZBD_HOST_MANAGED;
720 dprint(FD_ZBD, "%s: hit-default, assigning ZBD_NONE\n", f->file_name);
728 xnvme_dev_close(dev);
730 err_lock = pthread_mutex_unlock(&g_serialize);
732 log_err("ioeng->get_zoned_model(): pthread_mutex_unlock(), err(%d)\n", err_lock);
738 * Fills the given ``zbdz`` with at most ``nr_zones`` zone-descriptors.
740 * The implementation converts the NVMe Zoned Command Set log-pages for Zone
741 * descriptors into the Linux Kernel Zoned Block Report format.
743 * NOTE: This function is called before I/O engine initialization, that is,
744 * before ``_dev_open`` has been called and file-wrapping is setup. Thus is has
745 * to do the ``_dev_open`` itself, and shut it down again once it is done
746 * retrieving the log-pages and converting them to the report format.
748 * TODO: unify the different setup methods, consider keeping the handle around,
749 * and consider how to support the --async option in this usecase
751 static int xnvme_fioe_report_zones(struct thread_data *td, struct fio_file *f, uint64_t offset,
752 struct zbd_zone *zbdz, unsigned int nr_zones)
754 struct xnvme_opts opts = xnvme_opts_from_fioe(td);
755 const struct xnvme_spec_znd_idfy_lbafe *lbafe = NULL;
756 struct xnvme_dev *dev = NULL;
757 const struct xnvme_geo *geo = NULL;
758 struct xnvme_znd_report *rprt = NULL;
761 unsigned int limit = 0;
762 int err = 0, err_lock;
764 dprint(FD_ZBD, "%s: report_zones() offset: %zu, nr_zones: %u\n", f->file_name, offset,
767 err = pthread_mutex_lock(&g_serialize);
769 log_err("ioeng->report_zones(%s): pthread_mutex_lock(), err(%d)\n", f->file_name,
774 dev = xnvme_dev_open(f->file_name, &opts);
776 log_err("ioeng->report_zones(%s): xnvme_dev_open(), err(%d)\n", f->file_name,
781 geo = xnvme_dev_get_geo(dev);
782 ssw = xnvme_dev_get_ssw(dev);
783 lbafe = xnvme_znd_dev_get_lbafe(dev);
785 limit = nr_zones > geo->nzone ? geo->nzone : nr_zones;
787 dprint(FD_ZBD, "%s: limit: %u\n", f->file_name, limit);
789 slba = ((offset >> ssw) / geo->nsect) * geo->nsect;
791 rprt = xnvme_znd_report_from_dev(dev, slba, limit, 0);
793 log_err("ioeng->report_zones(%s): xnvme_znd_report_from_dev(), err(%d)\n",
794 f->file_name, errno);
798 if (rprt->nentries != limit) {
799 log_err("ioeng->report_zones(%s): nentries != nr_zones\n", f->file_name);
803 if (offset > geo->tbytes) {
804 log_err("ioeng->report_zones(%s): out-of-bounds\n", f->file_name);
808 /* Transform the zone-report */
809 for (uint32_t idx = 0; idx < rprt->nentries; ++idx) {
810 struct xnvme_spec_znd_descr *descr = XNVME_ZND_REPORT_DESCR(rprt, idx);
812 zbdz[idx].start = descr->zslba << ssw;
813 zbdz[idx].len = lbafe->zsze << ssw;
814 zbdz[idx].capacity = descr->zcap << ssw;
815 zbdz[idx].wp = descr->wp << ssw;
818 case XNVME_SPEC_ZND_TYPE_SEQWR:
819 zbdz[idx].type = ZBD_ZONE_TYPE_SWR;
823 log_err("ioeng->report_zones(%s): invalid type for zone at offset(%zu)\n",
824 f->file_name, zbdz[idx].start);
830 case XNVME_SPEC_ZND_STATE_EMPTY:
831 zbdz[idx].cond = ZBD_ZONE_COND_EMPTY;
833 case XNVME_SPEC_ZND_STATE_IOPEN:
834 zbdz[idx].cond = ZBD_ZONE_COND_IMP_OPEN;
836 case XNVME_SPEC_ZND_STATE_EOPEN:
837 zbdz[idx].cond = ZBD_ZONE_COND_EXP_OPEN;
839 case XNVME_SPEC_ZND_STATE_CLOSED:
840 zbdz[idx].cond = ZBD_ZONE_COND_CLOSED;
842 case XNVME_SPEC_ZND_STATE_FULL:
843 zbdz[idx].cond = ZBD_ZONE_COND_FULL;
846 case XNVME_SPEC_ZND_STATE_RONLY:
847 case XNVME_SPEC_ZND_STATE_OFFLINE:
849 zbdz[idx].cond = ZBD_ZONE_COND_OFFLINE;
855 xnvme_buf_virt_free(rprt);
857 xnvme_dev_close(dev);
859 err_lock = pthread_mutex_unlock(&g_serialize);
861 log_err("ioeng->report_zones(): pthread_mutex_unlock(), err: %d\n", err_lock);
863 dprint(FD_ZBD, "err: %d, nr_zones: %d\n", err, (int)nr_zones);
865 return err ? err : (int)limit;
869 * NOTE: This function may get called before I/O engine initialization, that is,
870 * before ``_dev_open`` has been called and file-wrapping is setup. In such
871 * case it has to do ``_dev_open`` itself, and shut it down again once it is
872 * done resetting write pointer of zones.
874 static int xnvme_fioe_reset_wp(struct thread_data *td, struct fio_file *f, uint64_t offset,
877 struct xnvme_opts opts = xnvme_opts_from_fioe(td);
878 struct xnvme_fioe_data *xd = NULL;
879 struct xnvme_fioe_fwrap *fwrap = NULL;
880 struct xnvme_dev *dev = NULL;
881 const struct xnvme_geo *geo = NULL;
882 uint64_t first, last;
885 int err = 0, err_lock;
887 if (td->io_ops_data) {
888 xd = td->io_ops_data;
889 fwrap = &xd->files[f->fileno];
898 err = pthread_mutex_lock(&g_serialize);
900 log_err("ioeng->reset_wp(): pthread_mutex_lock(), err(%d)\n", err);
904 dev = xnvme_dev_open(f->file_name, &opts);
906 log_err("ioeng->reset_wp(): xnvme_dev_open(%s) failed, errno(%d)\n",
907 f->file_name, errno);
910 geo = xnvme_dev_get_geo(dev);
911 ssw = xnvme_dev_get_ssw(dev);
914 nsid = xnvme_dev_get_nsid(dev);
916 first = ((offset >> ssw) / geo->nsect) * geo->nsect;
917 last = (((offset + length) >> ssw) / geo->nsect) * geo->nsect;
918 dprint(FD_ZBD, "first: 0x%lx, last: 0x%lx\n", first, last);
920 for (uint64_t zslba = first; zslba < last; zslba += geo->nsect) {
921 struct xnvme_cmd_ctx ctx = xnvme_cmd_ctx_from_dev(dev);
923 if (zslba >= (geo->nsect * geo->nzone)) {
924 log_err("ioeng->reset_wp(): out-of-bounds\n");
929 err = xnvme_znd_mgmt_send(&ctx, nsid, zslba, false,
930 XNVME_SPEC_ZND_CMD_MGMT_SEND_RESET, 0x0, NULL);
931 if (err || xnvme_cmd_ctx_cpl_status(&ctx)) {
932 err = err ? err : -EIO;
933 log_err("ioeng->reset_wp(): err(%d), sc(%d)", err, ctx.cpl.status.sc);
939 if (!td->io_ops_data) {
940 xnvme_dev_close(dev);
942 err_lock = pthread_mutex_unlock(&g_serialize);
944 log_err("ioeng->reset_wp(): pthread_mutex_unlock(), err(%d)\n", err_lock);
950 static int xnvme_fioe_get_file_size(struct thread_data *td, struct fio_file *f)
952 struct xnvme_opts opts = xnvme_opts_from_fioe(td);
953 struct xnvme_dev *dev;
956 if (fio_file_size_known(f))
959 ret = pthread_mutex_lock(&g_serialize);
961 log_err("ioeng->reset_wp(): pthread_mutex_lock(), err(%d)\n", ret);
965 dev = xnvme_dev_open(f->file_name, &opts);
967 log_err("%s: failed retrieving device handle, errno: %d\n", f->file_name, errno);
972 f->real_file_size = xnvme_dev_get_geo(dev)->tbytes;
973 fio_file_set_size_known(f);
974 f->filetype = FIO_TYPE_BLOCK;
977 xnvme_dev_close(dev);
978 err = pthread_mutex_unlock(&g_serialize);
980 log_err("ioeng->reset_wp(): pthread_mutex_unlock(), err(%d)\n", err);
985 FIO_STATIC struct ioengine_ops ioengine = {
987 .version = FIO_IOOPS_VERSION,
989 .option_struct_size = sizeof(struct xnvme_fioe_options),
990 .flags = FIO_DISKLESSIO | FIO_NODISKUTIL | FIO_NOEXTEND | FIO_MEMALIGN | FIO_RAWIO,
992 .cleanup = xnvme_fioe_cleanup,
993 .init = xnvme_fioe_init,
995 .iomem_free = xnvme_fioe_iomem_free,
996 .iomem_alloc = xnvme_fioe_iomem_alloc,
998 .io_u_free = xnvme_fioe_io_u_free,
999 .io_u_init = xnvme_fioe_io_u_init,
1001 .event = xnvme_fioe_event,
1002 .getevents = xnvme_fioe_getevents,
1003 .queue = xnvme_fioe_queue,
1005 .close_file = xnvme_fioe_close,
1006 .open_file = xnvme_fioe_open,
1007 .get_file_size = xnvme_fioe_get_file_size,
1009 .invalidate = xnvme_fioe_invalidate,
1010 .get_max_open_zones = xnvme_fioe_get_max_open_zones,
1011 .get_zoned_model = xnvme_fioe_get_zoned_model,
1012 .report_zones = xnvme_fioe_report_zones,
1013 .reset_wp = xnvme_fioe_reset_wp,
1016 static void fio_init fio_xnvme_register(void)
1018 register_ioengine(&ioengine);
1021 static void fio_exit fio_xnvme_unregister(void)
1023 unregister_ioengine(&ioengine);