engines/rados: Added waiting for completion on cleanup.
[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;
22};
23
24static int libzbc_get_dev_info(struct libzbc_data *ld, struct fio_file *f)
25{
26 struct zbc_device_info *zinfo;
27
28 zinfo = calloc(1, sizeof(*zinfo));
29 if (!zinfo)
30 return -ENOMEM;
31
32 zbc_get_device_info(ld->zdev, zinfo);
33 ld->model = zinfo->zbd_model;
34 ld->nr_sectors = zinfo->zbd_sectors;
35
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));
40
41 free(zinfo);
42
43 return 0;
44}
45
46static int libzbc_open_dev(struct thread_data *td, struct fio_file *f,
47 struct libzbc_data **p_ld)
48{
49 struct libzbc_data *ld = td->io_ops_data;
50 int ret, flags = OS_O_DIRECT;
51
52 if (ld) {
53 /* Already open */
54 assert(ld->zdev);
55 goto out;
56 }
57
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");
61 return -EINVAL;
62 }
63
64 if (td_write(td)) {
65 if (!read_only)
66 flags |= O_RDWR;
67 } else if (td_read(td)) {
68 if (f->filetype == FIO_TYPE_CHAR && !read_only)
69 flags |= O_RDWR;
70 else
71 flags |= O_RDONLY;
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",
75 f->file_name);
76 return -EINVAL;
77 }
78
79 if (td->o.oatomic) {
80 td_verror(td, EINVAL, "libzbc does not support O_ATOMIC");
81 log_err("%s: libzbc does not support O_ATOMIC\n",
82 f->file_name);
83 return -EINVAL;
84 }
85
86 ld = calloc(1, sizeof(*ld));
87 if (!ld)
88 return -ENOMEM;
89
90 ret = zbc_open(f->file_name,
91 flags | ZBC_O_DRV_SCSI | ZBC_O_DRV_ATA, &ld->zdev);
92 if (ret) {
93 log_err("%s: zbc_open() failed, err=%d\n",
94 f->file_name, ret);
95 return ret;
96 }
97
98 ret = libzbc_get_dev_info(ld, f);
99 if (ret) {
100 zbc_close(ld->zdev);
101 free(ld);
102 return ret;
103 }
104
105 td->io_ops_data = ld;
106out:
107 if (p_ld)
108 *p_ld = ld;
109
110 return 0;
111}
112
113static int libzbc_close_dev(struct thread_data *td)
114{
115 struct libzbc_data *ld = td->io_ops_data;
116 int ret = 0;
117
118 td->io_ops_data = NULL;
119 if (ld) {
120 if (ld->zdev)
121 ret = zbc_close(ld->zdev);
122 free(ld);
123 }
124
125 return ret;
126}
127static int libzbc_open_file(struct thread_data *td, struct fio_file *f)
128{
129 return libzbc_open_dev(td, f, NULL);
130}
131
132static int libzbc_close_file(struct thread_data *td, struct fio_file *f)
133{
134 int ret;
135
136 ret = libzbc_close_dev(td);
137 if (ret)
138 log_err("%s: close device failed err %d\n",
139 f->file_name, ret);
140
141 return ret;
142}
143
144static void libzbc_cleanup(struct thread_data *td)
145{
146 libzbc_close_dev(td);
147}
148
149static int libzbc_invalidate(struct thread_data *td, struct fio_file *f)
150{
151 /* Passthrough IO do not cache data. Nothing to do */
152 return 0;
153}
154
155static int libzbc_get_file_size(struct thread_data *td, struct fio_file *f)
156{
157 struct libzbc_data *ld;
158 int ret;
159
160 if (fio_file_size_known(f))
161 return 0;
162
163 ret = libzbc_open_dev(td, f, &ld);
164 if (ret)
165 return ret;
166
167 f->real_file_size = ld->nr_sectors << 9;
168 fio_file_set_size_known(f);
169
170 return 0;
171}
172
173static int libzbc_get_zoned_model(struct thread_data *td, struct fio_file *f,
174 enum zbd_zoned_model *model)
175{
176 struct libzbc_data *ld;
177 int ret;
178
179 if (f->filetype != FIO_TYPE_BLOCK && f->filetype != FIO_TYPE_CHAR) {
180 *model = ZBD_IGNORE;
181 return 0;
182 }
183
184 ret = libzbc_open_dev(td, f, &ld);
185 if (ret)
186 return ret;
187
188 switch (ld->model) {
189 case ZBC_DM_HOST_AWARE:
190 *model = ZBD_HOST_AWARE;
191 break;
192 case ZBC_DM_HOST_MANAGED:
193 *model = ZBD_HOST_MANAGED;
194 break;
195 default:
196 *model = ZBD_NONE;
197 break;
198 }
199
200 return 0;
201}
202
203static int libzbc_report_zones(struct thread_data *td, struct fio_file *f,
204 uint64_t offset, struct zbd_zone *zbdz,
205 unsigned int nr_zones)
206{
207 struct libzbc_data *ld;
208 uint64_t sector = offset >> 9;
209 struct zbc_zone *zones;
210 unsigned int i;
211 int ret;
212
213 ret = libzbc_open_dev(td, f, &ld);
214 if (ret)
215 return ret;
216
217 if (sector >= ld->nr_sectors)
218 return 0;
219
220 zones = calloc(nr_zones, sizeof(struct zbc_zone));
221 if (!zones) {
222 ret = -ENOMEM;
223 goto out;
224 }
225
226 ret = zbc_report_zones(ld->zdev, sector, ZBC_RO_ALL, zones, &nr_zones);
227 if (ret < 0) {
228 log_err("%s: zbc_report_zones failed, err=%d\n",
229 f->file_name, ret);
230 goto out;
231 }
232
233 for (i = 0; i < nr_zones; i++, zbdz++) {
234 zbdz->start = zones[i].zbz_start << 9;
235 zbdz->len = zones[i].zbz_length << 9;
236 zbdz->wp = zones[i].zbz_write_pointer << 9;
237
238 switch (zones[i].zbz_type) {
239 case ZBC_ZT_CONVENTIONAL:
240 zbdz->type = ZBD_ZONE_TYPE_CNV;
241 break;
242 case ZBC_ZT_SEQUENTIAL_REQ:
243 zbdz->type = ZBD_ZONE_TYPE_SWR;
244 break;
245 case ZBC_ZT_SEQUENTIAL_PREF:
246 zbdz->type = ZBD_ZONE_TYPE_SWP;
247 break;
248 default:
249 td_verror(td, errno, "invalid zone type");
250 log_err("%s: invalid type for zone at sector %llu.\n",
251 f->file_name, (unsigned long long)zbdz->start);
252 ret = -EIO;
253 goto out;
254 }
255
256 switch (zones[i].zbz_condition) {
257 case ZBC_ZC_NOT_WP:
258 zbdz->cond = ZBD_ZONE_COND_NOT_WP;
259 break;
260 case ZBC_ZC_EMPTY:
261 zbdz->cond = ZBD_ZONE_COND_EMPTY;
262 break;
263 case ZBC_ZC_IMP_OPEN:
264 zbdz->cond = ZBD_ZONE_COND_IMP_OPEN;
265 break;
266 case ZBC_ZC_EXP_OPEN:
267 zbdz->cond = ZBD_ZONE_COND_EXP_OPEN;
268 break;
269 case ZBC_ZC_CLOSED:
270 zbdz->cond = ZBD_ZONE_COND_CLOSED;
271 break;
272 case ZBC_ZC_FULL:
273 zbdz->cond = ZBD_ZONE_COND_FULL;
274 break;
275 case ZBC_ZC_RDONLY:
276 case ZBC_ZC_OFFLINE:
277 default:
278 /* Treat all these conditions as offline (don't use!) */
279 zbdz->cond = ZBD_ZONE_COND_OFFLINE;
280 break;
281 }
282 }
283
284 ret = nr_zones;
285out:
286 free(zones);
287 return ret;
288}
289
290static int libzbc_reset_wp(struct thread_data *td, struct fio_file *f,
291 uint64_t offset, uint64_t length)
292{
293 struct libzbc_data *ld = td->io_ops_data;
294 uint64_t sector = offset >> 9;
295 uint64_t end_sector = (offset + length) >> 9;
296 unsigned int nr_zones;
297 struct zbc_errno err;
298 int i, ret;
299
300 assert(ld);
301 assert(ld->zdev);
302
303 nr_zones = (length + td->o.zone_size - 1) / td->o.zone_size;
304 if (!sector && end_sector >= ld->nr_sectors) {
305 /* Reset all zones */
306 ret = zbc_reset_zone(ld->zdev, 0, ZBC_OP_ALL_ZONES);
307 if (ret)
308 goto err;
309
310 return 0;
311 }
312
313 for (i = 0; i < nr_zones; i++, sector += td->o.zone_size >> 9) {
314 ret = zbc_reset_zone(ld->zdev, sector, 0);
315 if (ret)
316 goto err;
317 }
318
319 return 0;
320
321err:
322 zbc_errno(ld->zdev, &err);
323 td_verror(td, errno, "zbc_reset_zone failed");
324 if (err.sk)
325 log_err("%s: reset wp failed %s:%s\n",
326 f->file_name,
327 zbc_sk_str(err.sk), zbc_asc_ascq_str(err.asc_ascq));
328 return -ret;
329}
330
331ssize_t libzbc_rw(struct thread_data *td, struct io_u *io_u)
332{
333 struct libzbc_data *ld = td->io_ops_data;
334 struct fio_file *f = io_u->file;
335 uint64_t sector = io_u->offset >> 9;
336 size_t count = io_u->xfer_buflen >> 9;
337 struct zbc_errno err;
338 ssize_t ret;
339
340 if (io_u->ddir == DDIR_WRITE)
341 ret = zbc_pwrite(ld->zdev, io_u->xfer_buf, count, sector);
342 else
343 ret = zbc_pread(ld->zdev, io_u->xfer_buf, count, sector);
344 if (ret == count)
345 return ret;
346
347 if (ret > 0) {
348 log_err("Short %s, len=%zu, ret=%zd\n",
349 io_u->ddir == DDIR_READ ? "read" : "write",
350 count << 9, ret << 9);
351 return -EIO;
352 }
353
354 /* I/O error */
355 zbc_errno(ld->zdev, &err);
356 td_verror(td, errno, "libzbc i/o failed");
357 if (err.sk) {
358 log_err("%s: op %u offset %llu+%llu failed (%s:%s), err %zd\n",
359 f->file_name, io_u->ddir,
360 io_u->offset, io_u->xfer_buflen,
361 zbc_sk_str(err.sk),
362 zbc_asc_ascq_str(err.asc_ascq), ret);
363 } else {
364 log_err("%s: op %u offset %llu+%llu failed, err %zd\n",
365 f->file_name, io_u->ddir,
366 io_u->offset, io_u->xfer_buflen, ret);
367 }
368
369 return -EIO;
370}
371
372static enum fio_q_status libzbc_queue(struct thread_data *td, struct io_u *io_u)
373{
374 struct libzbc_data *ld = td->io_ops_data;
375 struct fio_file *f = io_u->file;
376 ssize_t ret = 0;
377
378 fio_ro_check(td, io_u);
379
380 dprint(FD_ZBD, "%p:%s: libzbc queue %llu\n",
381 td, f->file_name, io_u->offset);
382
383 if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) {
384 ret = libzbc_rw(td, io_u);
385 } else if (ddir_sync(io_u->ddir)) {
386 ret = zbc_flush(ld->zdev);
387 if (ret)
388 log_err("zbc_flush error %zd\n", ret);
389 } else if (io_u->ddir != DDIR_TRIM) {
390 log_err("Unsupported operation %u\n", io_u->ddir);
391 ret = -EINVAL;
392 }
393 if (ret < 0)
394 io_u->error = -ret;
395
396 return FIO_Q_COMPLETED;
397}
398
399static struct ioengine_ops ioengine = {
400 .name = "libzbc",
401 .version = FIO_IOOPS_VERSION,
402 .open_file = libzbc_open_file,
403 .close_file = libzbc_close_file,
404 .cleanup = libzbc_cleanup,
405 .invalidate = libzbc_invalidate,
406 .get_file_size = libzbc_get_file_size,
407 .get_zoned_model = libzbc_get_zoned_model,
408 .report_zones = libzbc_report_zones,
409 .reset_wp = libzbc_reset_wp,
410 .queue = libzbc_queue,
411 .flags = FIO_SYNCIO | FIO_NOEXTEND | FIO_RAWIO,
412};
413
414static void fio_init fio_libzbc_register(void)
415{
416 register_ioengine(&ioengine);
417}
418
419static void fio_exit fio_libzbc_unregister(void)
420{
421 unregister_ioengine(&ioengine);
422}