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