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"
19 struct zbc_device *zdev;
20 enum zbc_dev_model model;
24 static int libzbc_get_dev_info(struct libzbc_data *ld, struct fio_file *f)
26 struct zbc_device_info *zinfo;
28 zinfo = calloc(1, sizeof(*zinfo));
32 zbc_get_device_info(ld->zdev, zinfo);
33 ld->model = zinfo->zbd_model;
34 ld->nr_sectors = zinfo->zbd_sectors;
36 dprint(FD_ZBD, "%s: vendor_id:%s, type: %s, model: %s\n",
37 f->file_name, zinfo->zbd_vendor_id,
38 zbc_device_type_str(zinfo->zbd_type),
39 zbc_device_model_str(zinfo->zbd_model));
46 static int libzbc_open_dev(struct thread_data *td, struct fio_file *f,
47 struct libzbc_data **p_ld)
49 struct libzbc_data *ld = td->io_ops_data;
50 int ret, flags = OS_O_DIRECT;
58 if (f->filetype != FIO_TYPE_BLOCK && f->filetype != FIO_TYPE_CHAR) {
59 td_verror(td, EINVAL, "wrong file type");
60 log_err("ioengine libzbc only works on block or character devices\n");
67 } else if (td_read(td)) {
68 if (f->filetype == FIO_TYPE_CHAR && !read_only)
72 } else if (td_trim(td)) {
73 td_verror(td, EINVAL, "libzbc does not support trim");
74 log_err("%s: libzbc does not support trim\n", f->file_name);
79 td_verror(td, EINVAL, "libzbc does not support O_ATOMIC");
80 log_err("%s: libzbc does not support O_ATOMIC\n", f->file_name);
84 ld = calloc(1, sizeof(*ld));
88 ret = zbc_open(f->file_name,
89 flags | ZBC_O_DRV_BLOCK | ZBC_O_DRV_SCSI | ZBC_O_DRV_ATA,
92 log_err("%s: zbc_open() failed, err=%d\n",
97 ret = libzbc_get_dev_info(ld, f);
101 td->io_ops_data = ld;
115 static int libzbc_close_dev(struct thread_data *td)
117 struct libzbc_data *ld = td->io_ops_data;
120 td->io_ops_data = NULL;
123 ret = zbc_close(ld->zdev);
129 static int libzbc_open_file(struct thread_data *td, struct fio_file *f)
131 return libzbc_open_dev(td, f, NULL);
134 static int libzbc_close_file(struct thread_data *td, struct fio_file *f)
138 ret = libzbc_close_dev(td);
140 log_err("%s: close device failed err %d\n",
146 static void libzbc_cleanup(struct thread_data *td)
148 libzbc_close_dev(td);
151 static int libzbc_invalidate(struct thread_data *td, struct fio_file *f)
153 /* Passthrough IO do not cache data. Nothing to do */
157 static int libzbc_get_file_size(struct thread_data *td, struct fio_file *f)
159 struct libzbc_data *ld;
162 if (fio_file_size_known(f))
165 ret = libzbc_open_dev(td, f, &ld);
169 f->real_file_size = ld->nr_sectors << 9;
170 fio_file_set_size_known(f);
175 static int libzbc_get_zoned_model(struct thread_data *td, struct fio_file *f,
176 enum zbd_zoned_model *model)
178 struct libzbc_data *ld;
181 if (f->filetype != FIO_TYPE_BLOCK && f->filetype != FIO_TYPE_CHAR) {
186 ret = libzbc_open_dev(td, f, &ld);
191 case ZBC_DM_HOST_AWARE:
192 *model = ZBD_HOST_AWARE;
194 case ZBC_DM_HOST_MANAGED:
195 *model = ZBD_HOST_MANAGED;
205 static int libzbc_report_zones(struct thread_data *td, struct fio_file *f,
206 uint64_t offset, struct zbd_zone *zbdz,
207 unsigned int nr_zones)
209 struct libzbc_data *ld;
210 uint64_t sector = offset >> 9;
211 struct zbc_zone *zones;
215 ret = libzbc_open_dev(td, f, &ld);
219 if (sector >= ld->nr_sectors)
222 zones = calloc(nr_zones, sizeof(struct zbc_zone));
228 ret = zbc_report_zones(ld->zdev, sector, ZBC_RO_ALL, zones, &nr_zones);
230 log_err("%s: zbc_report_zones failed, err=%d\n",
235 for (i = 0; i < nr_zones; i++, zbdz++) {
236 zbdz->start = zones[i].zbz_start << 9;
237 zbdz->len = zones[i].zbz_length << 9;
238 zbdz->wp = zones[i].zbz_write_pointer << 9;
240 * ZBC/ZAC do not define zone capacity, so use the zone size as
243 zbdz->capacity = zbdz->len;
245 switch (zones[i].zbz_type) {
246 case ZBC_ZT_CONVENTIONAL:
247 zbdz->type = ZBD_ZONE_TYPE_CNV;
249 case ZBC_ZT_SEQUENTIAL_REQ:
250 zbdz->type = ZBD_ZONE_TYPE_SWR;
252 case ZBC_ZT_SEQUENTIAL_PREF:
253 zbdz->type = ZBD_ZONE_TYPE_SWP;
256 td_verror(td, errno, "invalid zone type");
257 log_err("%s: invalid type for zone at sector %llu.\n",
258 f->file_name, (unsigned long long)zbdz->start);
263 switch (zones[i].zbz_condition) {
265 zbdz->cond = ZBD_ZONE_COND_NOT_WP;
268 zbdz->cond = ZBD_ZONE_COND_EMPTY;
270 case ZBC_ZC_IMP_OPEN:
271 zbdz->cond = ZBD_ZONE_COND_IMP_OPEN;
273 case ZBC_ZC_EXP_OPEN:
274 zbdz->cond = ZBD_ZONE_COND_EXP_OPEN;
277 zbdz->cond = ZBD_ZONE_COND_CLOSED;
280 zbdz->cond = ZBD_ZONE_COND_FULL;
285 /* Treat all these conditions as offline (don't use!) */
286 zbdz->cond = ZBD_ZONE_COND_OFFLINE;
287 zbdz->wp = zbdz->start;
297 static int libzbc_reset_wp(struct thread_data *td, struct fio_file *f,
298 uint64_t offset, uint64_t length)
300 struct libzbc_data *ld = td->io_ops_data;
301 uint64_t sector = offset >> 9;
302 uint64_t end_sector = (offset + length) >> 9;
303 unsigned int nr_zones;
304 struct zbc_errno err;
310 nr_zones = (length + td->o.zone_size - 1) / td->o.zone_size;
311 if (!sector && end_sector >= ld->nr_sectors) {
312 /* Reset all zones */
313 ret = zbc_reset_zone(ld->zdev, 0, ZBC_OP_ALL_ZONES);
320 for (i = 0; i < nr_zones; i++, sector += td->o.zone_size >> 9) {
321 ret = zbc_reset_zone(ld->zdev, sector, 0);
329 zbc_errno(ld->zdev, &err);
330 td_verror(td, errno, "zbc_reset_zone failed");
332 log_err("%s: reset wp failed %s:%s\n",
334 zbc_sk_str(err.sk), zbc_asc_ascq_str(err.asc_ascq));
338 ssize_t libzbc_rw(struct thread_data *td, struct io_u *io_u)
340 struct libzbc_data *ld = td->io_ops_data;
341 struct fio_file *f = io_u->file;
342 uint64_t sector = io_u->offset >> 9;
343 size_t count = io_u->xfer_buflen >> 9;
344 struct zbc_errno err;
347 if (io_u->ddir == DDIR_WRITE)
348 ret = zbc_pwrite(ld->zdev, io_u->xfer_buf, count, sector);
350 ret = zbc_pread(ld->zdev, io_u->xfer_buf, count, sector);
355 log_err("Short %s, len=%zu, ret=%zd\n",
356 io_u->ddir == DDIR_READ ? "read" : "write",
357 count << 9, ret << 9);
362 zbc_errno(ld->zdev, &err);
363 td_verror(td, errno, "libzbc i/o failed");
365 log_err("%s: op %u offset %llu+%llu failed (%s:%s), err %zd\n",
366 f->file_name, io_u->ddir,
367 io_u->offset, io_u->xfer_buflen,
369 zbc_asc_ascq_str(err.asc_ascq), ret);
371 log_err("%s: op %u offset %llu+%llu failed, err %zd\n",
372 f->file_name, io_u->ddir,
373 io_u->offset, io_u->xfer_buflen, ret);
379 static enum fio_q_status libzbc_queue(struct thread_data *td, struct io_u *io_u)
381 struct libzbc_data *ld = td->io_ops_data;
382 struct fio_file *f = io_u->file;
385 fio_ro_check(td, io_u);
387 dprint(FD_ZBD, "%p:%s: libzbc queue %llu\n",
388 td, f->file_name, io_u->offset);
390 if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) {
391 ret = libzbc_rw(td, io_u);
392 } else if (ddir_sync(io_u->ddir)) {
393 ret = zbc_flush(ld->zdev);
395 log_err("zbc_flush error %zd\n", ret);
396 } else if (io_u->ddir != DDIR_TRIM) {
397 log_err("Unsupported operation %u\n", io_u->ddir);
403 return FIO_Q_COMPLETED;
406 FIO_STATIC struct ioengine_ops ioengine = {
408 .version = FIO_IOOPS_VERSION,
409 .open_file = libzbc_open_file,
410 .close_file = libzbc_close_file,
411 .cleanup = libzbc_cleanup,
412 .invalidate = libzbc_invalidate,
413 .get_file_size = libzbc_get_file_size,
414 .get_zoned_model = libzbc_get_zoned_model,
415 .report_zones = libzbc_report_zones,
416 .reset_wp = libzbc_reset_wp,
417 .queue = libzbc_queue,
418 .flags = FIO_SYNCIO | FIO_NOEXTEND | FIO_RAWIO,
421 static void fio_init fio_libzbc_register(void)
423 register_ioengine(&ioengine);
426 static void fio_exit fio_libzbc_unregister(void)
428 unregister_ioengine(&ioengine);