2 * Copyright (C) 2019 Western Digital Corporation or its affiliates.
4 * This file is released under the GPL.
7 * IO engine using libzbc library to talk to SMR disks.
12 #include <libzbc/zbc.h>
16 #include "zbd_types.h"
20 struct zbc_device *zdev;
21 enum zbc_dev_model model;
23 uint32_t max_open_seq_req;
26 static int libzbc_get_dev_info(struct libzbc_data *ld, struct fio_file *f)
28 struct zbc_device_info *zinfo;
30 zinfo = calloc(1, sizeof(*zinfo));
34 zbc_get_device_info(ld->zdev, zinfo);
35 ld->model = zinfo->zbd_model;
36 ld->nr_sectors = zinfo->zbd_sectors;
37 ld->max_open_seq_req = zinfo->zbd_max_nr_open_seq_req;
39 dprint(FD_ZBD, "%s: vendor_id:%s, type: %s, model: %s\n",
40 f->file_name, zinfo->zbd_vendor_id,
41 zbc_device_type_str(zinfo->zbd_type),
42 zbc_device_model_str(zinfo->zbd_model));
49 static int libzbc_open_dev(struct thread_data *td, struct fio_file *f,
50 struct libzbc_data **p_ld)
52 struct libzbc_data *ld = td->io_ops_data;
53 int ret, flags = OS_O_DIRECT;
61 if (f->filetype != FIO_TYPE_BLOCK && f->filetype != FIO_TYPE_CHAR) {
62 td_verror(td, EINVAL, "wrong file type");
63 log_err("ioengine libzbc only works on block or character devices\n");
67 if (td_write(td) || td_trim(td)) {
70 } else if (td_read(td)) {
75 td_verror(td, EINVAL, "libzbc does not support O_ATOMIC");
76 log_err("%s: libzbc does not support O_ATOMIC\n", f->file_name);
80 ld = calloc(1, sizeof(*ld));
84 ret = zbc_open(f->file_name,
85 flags | ZBC_O_DRV_SCSI | ZBC_O_DRV_ATA,
88 log_err("%s: zbc_open() failed, err=%d\n",
93 ret = libzbc_get_dev_info(ld, f);
111 static int libzbc_close_dev(struct thread_data *td)
113 struct libzbc_data *ld = td->io_ops_data;
116 td->io_ops_data = NULL;
119 ret = zbc_close(ld->zdev);
125 static int libzbc_open_file(struct thread_data *td, struct fio_file *f)
127 return libzbc_open_dev(td, f, NULL);
130 static int libzbc_close_file(struct thread_data *td, struct fio_file *f)
134 ret = libzbc_close_dev(td);
136 log_err("%s: close device failed err %d\n",
142 static void libzbc_cleanup(struct thread_data *td)
144 libzbc_close_dev(td);
147 static int libzbc_invalidate(struct thread_data *td, struct fio_file *f)
149 /* Passthrough IO do not cache data. Nothing to do */
153 static int libzbc_get_file_size(struct thread_data *td, struct fio_file *f)
155 struct libzbc_data *ld;
158 if (fio_file_size_known(f))
161 ret = libzbc_open_dev(td, f, &ld);
165 f->real_file_size = ld->nr_sectors << 9;
166 fio_file_set_size_known(f);
171 static int libzbc_get_zoned_model(struct thread_data *td, struct fio_file *f,
172 enum zbd_zoned_model *model)
174 struct libzbc_data *ld;
177 if (f->filetype != FIO_TYPE_BLOCK && f->filetype != FIO_TYPE_CHAR)
180 ret = libzbc_open_dev(td, f, &ld);
185 case ZBC_DM_HOST_AWARE:
186 *model = ZBD_HOST_AWARE;
188 case ZBC_DM_HOST_MANAGED:
189 *model = ZBD_HOST_MANAGED;
199 static int libzbc_report_zones(struct thread_data *td, struct fio_file *f,
200 uint64_t offset, struct zbd_zone *zbdz,
201 unsigned int nr_zones)
203 struct libzbc_data *ld;
204 uint64_t sector = offset >> 9;
205 struct zbc_zone *zones;
209 ret = libzbc_open_dev(td, f, &ld);
213 if (sector >= ld->nr_sectors)
216 zones = calloc(nr_zones, sizeof(struct zbc_zone));
222 ret = zbc_report_zones(ld->zdev, sector, ZBC_RO_ALL, zones, &nr_zones);
224 log_err("%s: zbc_report_zones failed, err=%d\n",
229 for (i = 0; i < nr_zones; i++, zbdz++) {
230 zbdz->start = zones[i].zbz_start << 9;
231 zbdz->len = zones[i].zbz_length << 9;
232 zbdz->wp = zones[i].zbz_write_pointer << 9;
234 * ZBC/ZAC do not define zone capacity, so use the zone size as
237 zbdz->capacity = zbdz->len;
239 switch (zones[i].zbz_type) {
240 case ZBC_ZT_CONVENTIONAL:
241 zbdz->type = ZBD_ZONE_TYPE_CNV;
243 case ZBC_ZT_SEQUENTIAL_REQ:
244 zbdz->type = ZBD_ZONE_TYPE_SWR;
246 case ZBC_ZT_SEQUENTIAL_PREF:
247 zbdz->type = ZBD_ZONE_TYPE_SWP;
250 td_verror(td, errno, "invalid zone type");
251 log_err("%s: invalid type for zone at sector %llu.\n",
252 f->file_name, (unsigned long long)zbdz->start);
257 switch (zones[i].zbz_condition) {
259 zbdz->cond = ZBD_ZONE_COND_NOT_WP;
262 zbdz->cond = ZBD_ZONE_COND_EMPTY;
264 case ZBC_ZC_IMP_OPEN:
265 zbdz->cond = ZBD_ZONE_COND_IMP_OPEN;
267 case ZBC_ZC_EXP_OPEN:
268 zbdz->cond = ZBD_ZONE_COND_EXP_OPEN;
271 zbdz->cond = ZBD_ZONE_COND_CLOSED;
274 zbdz->cond = ZBD_ZONE_COND_FULL;
279 /* Treat all these conditions as offline (don't use!) */
280 zbdz->cond = ZBD_ZONE_COND_OFFLINE;
281 zbdz->wp = zbdz->start;
291 static int libzbc_reset_wp(struct thread_data *td, struct fio_file *f,
292 uint64_t offset, uint64_t length)
294 struct libzbc_data *ld = td->io_ops_data;
295 uint64_t sector = offset >> 9;
296 uint64_t end_sector = (offset + length) >> 9;
297 unsigned int nr_zones;
298 struct zbc_errno err;
304 nr_zones = (length + td->o.zone_size - 1) / td->o.zone_size;
305 if (!sector && end_sector >= ld->nr_sectors) {
306 /* Reset all zones */
307 ret = zbc_reset_zone(ld->zdev, 0, ZBC_OP_ALL_ZONES);
314 for (i = 0; i < nr_zones; i++, sector += td->o.zone_size >> 9) {
315 ret = zbc_reset_zone(ld->zdev, sector, 0);
323 zbc_errno(ld->zdev, &err);
324 td_verror(td, errno, "zbc_reset_zone failed");
326 log_err("%s: reset wp failed %s:%s\n",
328 zbc_sk_str(err.sk), zbc_asc_ascq_str(err.asc_ascq));
332 static int libzbc_finish_zone(struct thread_data *td, struct fio_file *f,
333 uint64_t offset, uint64_t length)
335 struct libzbc_data *ld = td->io_ops_data;
336 uint64_t sector = offset >> 9;
337 unsigned int nr_zones;
338 struct zbc_errno err;
344 nr_zones = (length + td->o.zone_size - 1) / td->o.zone_size;
345 assert(nr_zones > 0);
347 for (i = 0; i < nr_zones; i++, sector += td->o.zone_size >> 9) {
348 ret = zbc_finish_zone(ld->zdev, sector, 0);
356 zbc_errno(ld->zdev, &err);
357 td_verror(td, errno, "zbc_finish_zone failed");
359 log_err("%s: finish zone failed %s:%s\n",
361 zbc_sk_str(err.sk), zbc_asc_ascq_str(err.asc_ascq));
365 static int libzbc_get_max_open_zones(struct thread_data *td, struct fio_file *f,
366 unsigned int *max_open_zones)
368 struct libzbc_data *ld;
371 ret = libzbc_open_dev(td, f, &ld);
375 if (ld->max_open_seq_req == ZBC_NO_LIMIT)
378 *max_open_zones = ld->max_open_seq_req;
383 ssize_t libzbc_rw(struct thread_data *td, struct io_u *io_u)
385 struct libzbc_data *ld = td->io_ops_data;
386 struct fio_file *f = io_u->file;
387 uint64_t sector = io_u->offset >> 9;
388 size_t count = io_u->xfer_buflen >> 9;
389 struct zbc_errno err;
392 if (io_u->ddir == DDIR_WRITE)
393 ret = zbc_pwrite(ld->zdev, io_u->xfer_buf, count, sector);
395 ret = zbc_pread(ld->zdev, io_u->xfer_buf, count, sector);
400 log_err("Short %s, len=%zu, ret=%zd\n",
401 io_u->ddir == DDIR_READ ? "read" : "write",
402 count << 9, ret << 9);
407 zbc_errno(ld->zdev, &err);
408 td_verror(td, errno, "libzbc i/o failed");
410 log_err("%s: op %u offset %llu+%llu failed (%s:%s), err %zd\n",
411 f->file_name, io_u->ddir,
412 io_u->offset, io_u->xfer_buflen,
414 zbc_asc_ascq_str(err.asc_ascq), ret);
416 log_err("%s: op %u offset %llu+%llu failed, err %zd\n",
417 f->file_name, io_u->ddir,
418 io_u->offset, io_u->xfer_buflen, ret);
424 static enum fio_q_status libzbc_queue(struct thread_data *td, struct io_u *io_u)
426 struct libzbc_data *ld = td->io_ops_data;
427 struct fio_file *f = io_u->file;
430 fio_ro_check(td, io_u);
432 dprint(FD_ZBD, "%p:%s: libzbc queue %llu\n",
433 td, f->file_name, io_u->offset);
435 if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) {
436 ret = libzbc_rw(td, io_u);
437 } else if (ddir_sync(io_u->ddir)) {
438 ret = zbc_flush(ld->zdev);
440 log_err("zbc_flush error %zd\n", ret);
441 } else if (io_u->ddir == DDIR_TRIM) {
442 ret = zbd_do_io_u_trim(td, io_u);
446 log_err("Unsupported operation %u\n", io_u->ddir);
452 return FIO_Q_COMPLETED;
455 FIO_STATIC struct ioengine_ops ioengine = {
457 .version = FIO_IOOPS_VERSION,
458 .open_file = libzbc_open_file,
459 .close_file = libzbc_close_file,
460 .cleanup = libzbc_cleanup,
461 .invalidate = libzbc_invalidate,
462 .get_file_size = libzbc_get_file_size,
463 .get_zoned_model = libzbc_get_zoned_model,
464 .report_zones = libzbc_report_zones,
465 .reset_wp = libzbc_reset_wp,
466 .get_max_open_zones = libzbc_get_max_open_zones,
467 .finish_zone = libzbc_finish_zone,
468 .queue = libzbc_queue,
469 .flags = FIO_SYNCIO | FIO_NOEXTEND | FIO_RAWIO,
472 static void fio_init fio_libzbc_register(void)
474 register_ioengine(&ioengine);
477 static void fio_exit fio_libzbc_unregister(void)
479 unregister_ioengine(&ioengine);