zbd: remove zbd_zoned_model ZBD_IGNORE
[fio.git] / engines / libzbc.c
CommitLineData
56a19325
DF
1/*
2 * Copyright (C) 2019 Western Digital Corporation or its affiliates.
3 *
4 * This file is released under the GPL.
5 *
6 * libzbc engine
7 * IO engine using libzbc library to talk to SMR disks.
8 */
9#include <stdlib.h>
10#include <unistd.h>
11#include <errno.h>
12#include <libzbc/zbc.h>
13
14#include "fio.h"
15#include "err.h"
16#include "zbd_types.h"
17
18struct libzbc_data {
19 struct zbc_device *zdev;
20 enum zbc_dev_model model;
21 uint64_t nr_sectors;
e8267436 22 uint32_t max_open_seq_req;
56a19325
DF
23};
24
25static int libzbc_get_dev_info(struct libzbc_data *ld, struct fio_file *f)
26{
27 struct zbc_device_info *zinfo;
28
29 zinfo = calloc(1, sizeof(*zinfo));
30 if (!zinfo)
31 return -ENOMEM;
32
33 zbc_get_device_info(ld->zdev, zinfo);
34 ld->model = zinfo->zbd_model;
35 ld->nr_sectors = zinfo->zbd_sectors;
e8267436 36 ld->max_open_seq_req = zinfo->zbd_max_nr_open_seq_req;
56a19325
DF
37
38 dprint(FD_ZBD, "%s: vendor_id:%s, type: %s, model: %s\n",
39 f->file_name, zinfo->zbd_vendor_id,
40 zbc_device_type_str(zinfo->zbd_type),
41 zbc_device_model_str(zinfo->zbd_model));
42
43 free(zinfo);
44
45 return 0;
46}
47
48static int libzbc_open_dev(struct thread_data *td, struct fio_file *f,
49 struct libzbc_data **p_ld)
50{
51 struct libzbc_data *ld = td->io_ops_data;
976fb54e 52 int ret, flags = OS_O_DIRECT;
56a19325
DF
53
54 if (ld) {
55 /* Already open */
56 assert(ld->zdev);
57 goto out;
58 }
59
60 if (f->filetype != FIO_TYPE_BLOCK && f->filetype != FIO_TYPE_CHAR) {
61 td_verror(td, EINVAL, "wrong file type");
62 log_err("ioengine libzbc only works on block or character devices\n");
63 return -EINVAL;
64 }
65
976fb54e 66 if (td_write(td)) {
56a19325
DF
67 if (!read_only)
68 flags |= O_RDWR;
69 } else if (td_read(td)) {
70 if (f->filetype == FIO_TYPE_CHAR && !read_only)
71 flags |= O_RDWR;
72 else
73 flags |= O_RDONLY;
74 } else if (td_trim(td)) {
75 td_verror(td, EINVAL, "libzbc does not support trim");
976fb54e
DF
76 log_err("%s: libzbc does not support trim\n", f->file_name);
77 return -EINVAL;
56a19325
DF
78 }
79
976fb54e 80 if (td->o.oatomic) {
56a19325 81 td_verror(td, EINVAL, "libzbc does not support O_ATOMIC");
976fb54e
DF
82 log_err("%s: libzbc does not support O_ATOMIC\n", f->file_name);
83 return -EINVAL;
84 }
56a19325
DF
85
86 ld = calloc(1, sizeof(*ld));
87 if (!ld)
88 return -ENOMEM;
89
90 ret = zbc_open(f->file_name,
ed2eb875
DF
91 flags | ZBC_O_DRV_BLOCK | ZBC_O_DRV_SCSI | ZBC_O_DRV_ATA,
92 &ld->zdev);
56a19325
DF
93 if (ret) {
94 log_err("%s: zbc_open() failed, err=%d\n",
95 f->file_name, ret);
891660e4 96 goto err;
56a19325
DF
97 }
98
99 ret = libzbc_get_dev_info(ld, f);
891660e4
DF
100 if (ret)
101 goto err_close;
56a19325
DF
102
103 td->io_ops_data = ld;
104out:
105 if (p_ld)
106 *p_ld = ld;
107
108 return 0;
891660e4
DF
109
110err_close:
111 zbc_close(ld->zdev);
112err:
113 free(ld);
114 return ret;
56a19325
DF
115}
116
117static int libzbc_close_dev(struct thread_data *td)
118{
119 struct libzbc_data *ld = td->io_ops_data;
120 int ret = 0;
121
122 td->io_ops_data = NULL;
123 if (ld) {
124 if (ld->zdev)
125 ret = zbc_close(ld->zdev);
126 free(ld);
127 }
128
129 return ret;
130}
131static int libzbc_open_file(struct thread_data *td, struct fio_file *f)
132{
133 return libzbc_open_dev(td, f, NULL);
134}
135
136static int libzbc_close_file(struct thread_data *td, struct fio_file *f)
137{
138 int ret;
139
140 ret = libzbc_close_dev(td);
141 if (ret)
142 log_err("%s: close device failed err %d\n",
143 f->file_name, ret);
144
145 return ret;
146}
147
148static void libzbc_cleanup(struct thread_data *td)
149{
150 libzbc_close_dev(td);
151}
152
153static int libzbc_invalidate(struct thread_data *td, struct fio_file *f)
154{
155 /* Passthrough IO do not cache data. Nothing to do */
156 return 0;
157}
158
159static int libzbc_get_file_size(struct thread_data *td, struct fio_file *f)
160{
161 struct libzbc_data *ld;
162 int ret;
163
164 if (fio_file_size_known(f))
165 return 0;
166
167 ret = libzbc_open_dev(td, f, &ld);
168 if (ret)
169 return ret;
170
171 f->real_file_size = ld->nr_sectors << 9;
172 fio_file_set_size_known(f);
173
174 return 0;
175}
176
177static int libzbc_get_zoned_model(struct thread_data *td, struct fio_file *f,
178 enum zbd_zoned_model *model)
179{
180 struct libzbc_data *ld;
181 int ret;
182
2c7dd23e
NC
183 if (f->filetype != FIO_TYPE_BLOCK && f->filetype != FIO_TYPE_CHAR)
184 return -EINVAL;
56a19325
DF
185
186 ret = libzbc_open_dev(td, f, &ld);
187 if (ret)
188 return ret;
189
190 switch (ld->model) {
191 case ZBC_DM_HOST_AWARE:
192 *model = ZBD_HOST_AWARE;
193 break;
194 case ZBC_DM_HOST_MANAGED:
195 *model = ZBD_HOST_MANAGED;
196 break;
197 default:
198 *model = ZBD_NONE;
199 break;
200 }
201
202 return 0;
203}
204
205static 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)
208{
209 struct libzbc_data *ld;
210 uint64_t sector = offset >> 9;
211 struct zbc_zone *zones;
212 unsigned int i;
213 int ret;
214
215 ret = libzbc_open_dev(td, f, &ld);
216 if (ret)
217 return ret;
218
219 if (sector >= ld->nr_sectors)
220 return 0;
221
222 zones = calloc(nr_zones, sizeof(struct zbc_zone));
223 if (!zones) {
224 ret = -ENOMEM;
225 goto out;
226 }
227
228 ret = zbc_report_zones(ld->zdev, sector, ZBC_RO_ALL, zones, &nr_zones);
229 if (ret < 0) {
230 log_err("%s: zbc_report_zones failed, err=%d\n",
231 f->file_name, ret);
232 goto out;
233 }
234
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;
236d23a8
SK
239 /*
240 * ZBC/ZAC do not define zone capacity, so use the zone size as
241 * the zone capacity.
242 */
243 zbdz->capacity = zbdz->len;
56a19325
DF
244
245 switch (zones[i].zbz_type) {
246 case ZBC_ZT_CONVENTIONAL:
247 zbdz->type = ZBD_ZONE_TYPE_CNV;
248 break;
249 case ZBC_ZT_SEQUENTIAL_REQ:
250 zbdz->type = ZBD_ZONE_TYPE_SWR;
251 break;
252 case ZBC_ZT_SEQUENTIAL_PREF:
253 zbdz->type = ZBD_ZONE_TYPE_SWP;
254 break;
255 default:
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);
259 ret = -EIO;
260 goto out;
261 }
262
263 switch (zones[i].zbz_condition) {
264 case ZBC_ZC_NOT_WP:
265 zbdz->cond = ZBD_ZONE_COND_NOT_WP;
266 break;
267 case ZBC_ZC_EMPTY:
268 zbdz->cond = ZBD_ZONE_COND_EMPTY;
269 break;
270 case ZBC_ZC_IMP_OPEN:
271 zbdz->cond = ZBD_ZONE_COND_IMP_OPEN;
272 break;
273 case ZBC_ZC_EXP_OPEN:
274 zbdz->cond = ZBD_ZONE_COND_EXP_OPEN;
275 break;
276 case ZBC_ZC_CLOSED:
277 zbdz->cond = ZBD_ZONE_COND_CLOSED;
278 break;
279 case ZBC_ZC_FULL:
280 zbdz->cond = ZBD_ZONE_COND_FULL;
281 break;
282 case ZBC_ZC_RDONLY:
283 case ZBC_ZC_OFFLINE:
284 default:
285 /* Treat all these conditions as offline (don't use!) */
286 zbdz->cond = ZBD_ZONE_COND_OFFLINE;
59b67452 287 zbdz->wp = zbdz->start;
56a19325
DF
288 }
289 }
290
291 ret = nr_zones;
292out:
293 free(zones);
294 return ret;
295}
296
297static int libzbc_reset_wp(struct thread_data *td, struct fio_file *f,
298 uint64_t offset, uint64_t length)
299{
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;
305 int i, ret;
306
307 assert(ld);
308 assert(ld->zdev);
309
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);
314 if (ret)
315 goto err;
316
317 return 0;
318 }
319
320 for (i = 0; i < nr_zones; i++, sector += td->o.zone_size >> 9) {
321 ret = zbc_reset_zone(ld->zdev, sector, 0);
322 if (ret)
323 goto err;
324 }
325
326 return 0;
327
328err:
329 zbc_errno(ld->zdev, &err);
330 td_verror(td, errno, "zbc_reset_zone failed");
331 if (err.sk)
332 log_err("%s: reset wp failed %s:%s\n",
333 f->file_name,
334 zbc_sk_str(err.sk), zbc_asc_ascq_str(err.asc_ascq));
335 return -ret;
336}
337
e8267436
NC
338static int libzbc_get_max_open_zones(struct thread_data *td, struct fio_file *f,
339 unsigned int *max_open_zones)
340{
341 struct libzbc_data *ld;
342 int ret;
343
344 ret = libzbc_open_dev(td, f, &ld);
345 if (ret)
346 return ret;
347
348 if (ld->max_open_seq_req == ZBC_NO_LIMIT)
349 *max_open_zones = 0;
350 else
351 *max_open_zones = ld->max_open_seq_req;
352
353 return 0;
354}
355
56a19325
DF
356ssize_t libzbc_rw(struct thread_data *td, struct io_u *io_u)
357{
358 struct libzbc_data *ld = td->io_ops_data;
359 struct fio_file *f = io_u->file;
360 uint64_t sector = io_u->offset >> 9;
361 size_t count = io_u->xfer_buflen >> 9;
362 struct zbc_errno err;
363 ssize_t ret;
364
365 if (io_u->ddir == DDIR_WRITE)
366 ret = zbc_pwrite(ld->zdev, io_u->xfer_buf, count, sector);
367 else
368 ret = zbc_pread(ld->zdev, io_u->xfer_buf, count, sector);
369 if (ret == count)
370 return ret;
371
372 if (ret > 0) {
373 log_err("Short %s, len=%zu, ret=%zd\n",
374 io_u->ddir == DDIR_READ ? "read" : "write",
375 count << 9, ret << 9);
376 return -EIO;
377 }
378
379 /* I/O error */
380 zbc_errno(ld->zdev, &err);
381 td_verror(td, errno, "libzbc i/o failed");
382 if (err.sk) {
383 log_err("%s: op %u offset %llu+%llu failed (%s:%s), err %zd\n",
384 f->file_name, io_u->ddir,
385 io_u->offset, io_u->xfer_buflen,
386 zbc_sk_str(err.sk),
387 zbc_asc_ascq_str(err.asc_ascq), ret);
388 } else {
389 log_err("%s: op %u offset %llu+%llu failed, err %zd\n",
390 f->file_name, io_u->ddir,
391 io_u->offset, io_u->xfer_buflen, ret);
392 }
393
394 return -EIO;
395}
396
397static enum fio_q_status libzbc_queue(struct thread_data *td, struct io_u *io_u)
398{
399 struct libzbc_data *ld = td->io_ops_data;
400 struct fio_file *f = io_u->file;
401 ssize_t ret = 0;
402
403 fio_ro_check(td, io_u);
404
405 dprint(FD_ZBD, "%p:%s: libzbc queue %llu\n",
406 td, f->file_name, io_u->offset);
407
408 if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) {
409 ret = libzbc_rw(td, io_u);
410 } else if (ddir_sync(io_u->ddir)) {
411 ret = zbc_flush(ld->zdev);
412 if (ret)
413 log_err("zbc_flush error %zd\n", ret);
414 } else if (io_u->ddir != DDIR_TRIM) {
415 log_err("Unsupported operation %u\n", io_u->ddir);
416 ret = -EINVAL;
417 }
418 if (ret < 0)
419 io_u->error = -ret;
420
421 return FIO_Q_COMPLETED;
422}
423
5a8a6a03 424FIO_STATIC struct ioengine_ops ioengine = {
56a19325
DF
425 .name = "libzbc",
426 .version = FIO_IOOPS_VERSION,
427 .open_file = libzbc_open_file,
428 .close_file = libzbc_close_file,
429 .cleanup = libzbc_cleanup,
430 .invalidate = libzbc_invalidate,
431 .get_file_size = libzbc_get_file_size,
432 .get_zoned_model = libzbc_get_zoned_model,
433 .report_zones = libzbc_report_zones,
434 .reset_wp = libzbc_reset_wp,
e8267436 435 .get_max_open_zones = libzbc_get_max_open_zones,
56a19325
DF
436 .queue = libzbc_queue,
437 .flags = FIO_SYNCIO | FIO_NOEXTEND | FIO_RAWIO,
438};
439
440static void fio_init fio_libzbc_register(void)
441{
442 register_ioengine(&ioengine);
443}
444
445static void fio_exit fio_libzbc_unregister(void)
446{
447 unregister_ioengine(&ioengine);
448}