zbd: simplify zbd_close_zone()
[fio.git] / zbd.c
1 /*
2  * Copyright (C) 2018 Western Digital Corporation or its affiliates.
3  *
4  * This file is released under the GPL.
5  */
6
7 #include <errno.h>
8 #include <string.h>
9 #include <stdlib.h>
10 #include <fcntl.h>
11 #include <sys/stat.h>
12 #include <unistd.h>
13
14 #include "os/os.h"
15 #include "file.h"
16 #include "fio.h"
17 #include "lib/pow2.h"
18 #include "log.h"
19 #include "oslib/asprintf.h"
20 #include "smalloc.h"
21 #include "verify.h"
22 #include "pshared.h"
23 #include "zbd.h"
24
25 static bool is_valid_offset(const struct fio_file *f, uint64_t offset)
26 {
27         return (uint64_t)(offset - f->file_offset) < f->io_size;
28 }
29
30 static inline unsigned int zbd_zone_nr(const struct fio_file *f,
31                                        struct fio_zone_info *zone)
32 {
33         return zone - f->zbd_info->zone_info;
34 }
35
36 /**
37  * zbd_zone_idx - convert an offset into a zone number
38  * @f: file pointer.
39  * @offset: offset in bytes. If this offset is in the first zone_size bytes
40  *          past the disk size then the index of the sentinel is returned.
41  */
42 static uint32_t zbd_zone_idx(const struct fio_file *f, uint64_t offset)
43 {
44         uint32_t zone_idx;
45
46         if (f->zbd_info->zone_size_log2 > 0)
47                 zone_idx = offset >> f->zbd_info->zone_size_log2;
48         else
49                 zone_idx = offset / f->zbd_info->zone_size;
50
51         return min(zone_idx, f->zbd_info->nr_zones);
52 }
53
54 /**
55  * zbd_zone_end - Return zone end location
56  * @z: zone info pointer.
57  */
58 static inline uint64_t zbd_zone_end(const struct fio_zone_info *z)
59 {
60         return (z+1)->start;
61 }
62
63 /**
64  * zbd_zone_capacity_end - Return zone capacity limit end location
65  * @z: zone info pointer.
66  */
67 static inline uint64_t zbd_zone_capacity_end(const struct fio_zone_info *z)
68 {
69         return z->start + z->capacity;
70 }
71
72 /**
73  * zbd_zone_full - verify whether a minimum number of bytes remain in a zone
74  * @f: file pointer.
75  * @z: zone info pointer.
76  * @required: minimum number of bytes that must remain in a zone.
77  *
78  * The caller must hold z->mutex.
79  */
80 static bool zbd_zone_full(const struct fio_file *f, struct fio_zone_info *z,
81                           uint64_t required)
82 {
83         assert((required & 511) == 0);
84
85         return z->has_wp &&
86                 z->wp + required > zbd_zone_capacity_end(z);
87 }
88
89 static void zone_lock(struct thread_data *td, const struct fio_file *f,
90                       struct fio_zone_info *z)
91 {
92         struct zoned_block_device_info *zbd = f->zbd_info;
93         uint32_t nz = z - zbd->zone_info;
94
95         /* A thread should never lock zones outside its working area. */
96         assert(f->min_zone <= nz && nz < f->max_zone);
97
98         assert(z->has_wp);
99
100         /*
101          * Lock the io_u target zone. The zone will be unlocked if io_u offset
102          * is changed or when io_u completes and zbd_put_io() executed.
103          * To avoid multiple jobs doing asynchronous I/Os from deadlocking each
104          * other waiting for zone locks when building an io_u batch, first
105          * only trylock the zone. If the zone is already locked by another job,
106          * process the currently queued I/Os so that I/O progress is made and
107          * zones unlocked.
108          */
109         if (pthread_mutex_trylock(&z->mutex) != 0) {
110                 if (!td_ioengine_flagged(td, FIO_SYNCIO))
111                         io_u_quiesce(td);
112                 pthread_mutex_lock(&z->mutex);
113         }
114 }
115
116 static inline void zone_unlock(struct fio_zone_info *z)
117 {
118         int ret;
119
120         assert(z->has_wp);
121         ret = pthread_mutex_unlock(&z->mutex);
122         assert(!ret);
123 }
124
125 static inline struct fio_zone_info *get_zone(const struct fio_file *f,
126                                              unsigned int zone_nr)
127 {
128         return &f->zbd_info->zone_info[zone_nr];
129 }
130
131 /**
132  * zbd_get_zoned_model - Get a device zoned model
133  * @td: FIO thread data
134  * @f: FIO file for which to get model information
135  */
136 static int zbd_get_zoned_model(struct thread_data *td, struct fio_file *f,
137                                enum zbd_zoned_model *model)
138 {
139         int ret;
140
141         if (f->filetype == FIO_TYPE_PIPE) {
142                 log_err("zonemode=zbd does not support pipes\n");
143                 return -EINVAL;
144         }
145
146         /* If regular file, always emulate zones inside the file. */
147         if (f->filetype == FIO_TYPE_FILE) {
148                 *model = ZBD_NONE;
149                 return 0;
150         }
151
152         if (td->io_ops && td->io_ops->get_zoned_model)
153                 ret = td->io_ops->get_zoned_model(td, f, model);
154         else
155                 ret = blkzoned_get_zoned_model(td, f, model);
156         if (ret < 0) {
157                 td_verror(td, errno, "get zoned model failed");
158                 log_err("%s: get zoned model failed (%d).\n",
159                         f->file_name, errno);
160         }
161
162         return ret;
163 }
164
165 /**
166  * zbd_report_zones - Get zone information
167  * @td: FIO thread data.
168  * @f: FIO file for which to get zone information
169  * @offset: offset from which to report zones
170  * @zones: Array of struct zbd_zone
171  * @nr_zones: Size of @zones array
172  *
173  * Get zone information into @zones starting from the zone at offset @offset
174  * for the device specified by @f.
175  *
176  * Returns the number of zones reported upon success and a negative error code
177  * upon failure. If the zone report is empty, always assume an error (device
178  * problem) and return -EIO.
179  */
180 static int zbd_report_zones(struct thread_data *td, struct fio_file *f,
181                             uint64_t offset, struct zbd_zone *zones,
182                             unsigned int nr_zones)
183 {
184         int ret;
185
186         if (td->io_ops && td->io_ops->report_zones)
187                 ret = td->io_ops->report_zones(td, f, offset, zones, nr_zones);
188         else
189                 ret = blkzoned_report_zones(td, f, offset, zones, nr_zones);
190         if (ret < 0) {
191                 td_verror(td, errno, "report zones failed");
192                 log_err("%s: report zones from sector %"PRIu64" failed (%d).\n",
193                         f->file_name, offset >> 9, errno);
194         } else if (ret == 0) {
195                 td_verror(td, errno, "Empty zone report");
196                 log_err("%s: report zones from sector %"PRIu64" is empty.\n",
197                         f->file_name, offset >> 9);
198                 ret = -EIO;
199         }
200
201         return ret;
202 }
203
204 /**
205  * zbd_reset_wp - reset the write pointer of a range of zones
206  * @td: FIO thread data.
207  * @f: FIO file for which to reset zones
208  * @offset: Starting offset of the first zone to reset
209  * @length: Length of the range of zones to reset
210  *
211  * Reset the write pointer of all zones in the range @offset...@offset+@length.
212  * Returns 0 upon success and a negative error code upon failure.
213  */
214 static int zbd_reset_wp(struct thread_data *td, struct fio_file *f,
215                         uint64_t offset, uint64_t length)
216 {
217         int ret;
218
219         if (td->io_ops && td->io_ops->reset_wp)
220                 ret = td->io_ops->reset_wp(td, f, offset, length);
221         else
222                 ret = blkzoned_reset_wp(td, f, offset, length);
223         if (ret < 0) {
224                 td_verror(td, errno, "resetting wp failed");
225                 log_err("%s: resetting wp for %"PRIu64" sectors at sector %"PRIu64" failed (%d).\n",
226                         f->file_name, length >> 9, offset >> 9, errno);
227         }
228
229         return ret;
230 }
231
232 /**
233  * zbd_reset_zone - reset the write pointer of a single zone
234  * @td: FIO thread data.
235  * @f: FIO file associated with the disk for which to reset a write pointer.
236  * @z: Zone to reset.
237  *
238  * Returns 0 upon success and a negative error code upon failure.
239  *
240  * The caller must hold z->mutex.
241  */
242 static int zbd_reset_zone(struct thread_data *td, struct fio_file *f,
243                           struct fio_zone_info *z)
244 {
245         uint64_t offset = z->start;
246         uint64_t length = (z+1)->start - offset;
247         uint64_t data_in_zone = z->wp - z->start;
248         int ret = 0;
249
250         if (!data_in_zone)
251                 return 0;
252
253         assert(is_valid_offset(f, offset + length - 1));
254
255         dprint(FD_ZBD, "%s: resetting wp of zone %u.\n",
256                f->file_name, zbd_zone_nr(f, z));
257
258         switch (f->zbd_info->model) {
259         case ZBD_HOST_AWARE:
260         case ZBD_HOST_MANAGED:
261                 ret = zbd_reset_wp(td, f, offset, length);
262                 if (ret < 0)
263                         return ret;
264                 break;
265         default:
266                 break;
267         }
268
269         pthread_mutex_lock(&f->zbd_info->mutex);
270         f->zbd_info->sectors_with_data -= data_in_zone;
271         f->zbd_info->wp_sectors_with_data -= data_in_zone;
272         pthread_mutex_unlock(&f->zbd_info->mutex);
273
274         z->wp = z->start;
275         z->verify_block = 0;
276
277         td->ts.nr_zone_resets++;
278
279         return ret;
280 }
281
282 /**
283  * zbd_close_zone - Remove a zone from the open zones array.
284  * @td: FIO thread data.
285  * @f: FIO file associated with the disk for which to reset a write pointer.
286  * @zone_idx: Index of the zone to remove.
287  *
288  * The caller must hold f->zbd_info->mutex.
289  */
290 static void zbd_close_zone(struct thread_data *td, const struct fio_file *f,
291                            struct fio_zone_info *z)
292 {
293         uint32_t ozi;
294
295         if (!z->open)
296                 return;
297
298         for (ozi = 0; ozi < f->zbd_info->num_open_zones; ozi++) {
299                 if (get_zone(f, f->zbd_info->open_zones[ozi]) == z)
300                         break;
301         }
302         if (ozi == f->zbd_info->num_open_zones)
303                 return;
304
305         dprint(FD_ZBD, "%s: closing zone %u\n",
306                f->file_name, zbd_zone_nr(f, z));
307
308         memmove(f->zbd_info->open_zones + ozi,
309                 f->zbd_info->open_zones + ozi + 1,
310                 (ZBD_MAX_OPEN_ZONES - (ozi + 1)) *
311                 sizeof(f->zbd_info->open_zones[0]));
312
313         f->zbd_info->num_open_zones--;
314         td->num_open_zones--;
315         z->open = 0;
316 }
317
318 /**
319  * zbd_reset_zones - Reset a range of zones.
320  * @td: fio thread data.
321  * @f: fio file for which to reset zones
322  * @zb: first zone to reset.
323  * @ze: first zone not to reset.
324  *
325  * Returns 0 upon success and 1 upon failure.
326  */
327 static int zbd_reset_zones(struct thread_data *td, struct fio_file *f,
328                            struct fio_zone_info *const zb,
329                            struct fio_zone_info *const ze)
330 {
331         struct fio_zone_info *z;
332         const uint64_t min_bs = td->o.min_bs[DDIR_WRITE];
333         int res = 0;
334
335         assert(min_bs);
336
337         dprint(FD_ZBD, "%s: examining zones %u .. %u\n",
338                f->file_name, zbd_zone_nr(f, zb), zbd_zone_nr(f, ze));
339
340         for (z = zb; z < ze; z++) {
341                 if (!z->has_wp)
342                         continue;
343
344                 zone_lock(td, f, z);
345                 pthread_mutex_lock(&f->zbd_info->mutex);
346                 zbd_close_zone(td, f, z);
347                 pthread_mutex_unlock(&f->zbd_info->mutex);
348
349                 if (z->wp != z->start) {
350                         dprint(FD_ZBD, "%s: resetting zone %u\n",
351                                f->file_name, zbd_zone_nr(f, z));
352                         if (zbd_reset_zone(td, f, z) < 0)
353                                 res = 1;
354                 }
355
356                 zone_unlock(z);
357         }
358
359         return res;
360 }
361
362 /**
363  * zbd_get_max_open_zones - Get the maximum number of open zones
364  * @td: FIO thread data
365  * @f: FIO file for which to get max open zones
366  * @max_open_zones: Upon success, result will be stored here.
367  *
368  * A @max_open_zones value set to zero means no limit.
369  *
370  * Returns 0 upon success and a negative error code upon failure.
371  */
372 static int zbd_get_max_open_zones(struct thread_data *td, struct fio_file *f,
373                                   unsigned int *max_open_zones)
374 {
375         int ret;
376
377         if (td->io_ops && td->io_ops->get_max_open_zones)
378                 ret = td->io_ops->get_max_open_zones(td, f, max_open_zones);
379         else
380                 ret = blkzoned_get_max_open_zones(td, f, max_open_zones);
381         if (ret < 0) {
382                 td_verror(td, errno, "get max open zones failed");
383                 log_err("%s: get max open zones failed (%d).\n",
384                         f->file_name, errno);
385         }
386
387         return ret;
388 }
389
390 /**
391  * zbd_open_zone - Add a zone to the array of open zones.
392  * @td: fio thread data.
393  * @f: fio file that has the open zones to add.
394  * @zone_idx: Index of the zone to add.
395  *
396  * Open a ZBD zone if it is not already open. Returns true if either the zone
397  * was already open or if the zone was successfully added to the array of open
398  * zones without exceeding the maximum number of open zones. Returns false if
399  * the zone was not already open and opening the zone would cause the zone limit
400  * to be exceeded.
401  */
402 static bool zbd_open_zone(struct thread_data *td, const struct fio_file *f,
403                           uint32_t zone_idx)
404 {
405         const uint64_t min_bs = td->o.min_bs[DDIR_WRITE];
406         struct zoned_block_device_info *zbdi = f->zbd_info;
407         struct fio_zone_info *z = get_zone(f, zone_idx);
408         bool res = true;
409
410         if (z->cond == ZBD_ZONE_COND_OFFLINE)
411                 return false;
412
413         /*
414          * Skip full zones with data verification enabled because resetting a
415          * zone causes data loss and hence causes verification to fail.
416          */
417         if (td->o.verify != VERIFY_NONE && zbd_zone_full(f, z, min_bs))
418                 return false;
419
420         /*
421          * zbdi->max_open_zones == 0 means that there is no limit on the maximum
422          * number of open zones. In this case, do no track open zones in
423          * zbdi->open_zones array.
424          */
425         if (!zbdi->max_open_zones)
426                 return true;
427
428         pthread_mutex_lock(&zbdi->mutex);
429
430         if (z->open) {
431                 /*
432                  * If the zone is going to be completely filled by writes
433                  * already in-flight, handle it as a full zone instead of an
434                  * open zone.
435                  */
436                 if (z->wp >= zbd_zone_capacity_end(z))
437                         res = false;
438                 goto out;
439         }
440
441         res = false;
442         /* Zero means no limit */
443         if (td->o.job_max_open_zones > 0 &&
444             td->num_open_zones >= td->o.job_max_open_zones)
445                 goto out;
446         if (zbdi->num_open_zones >= zbdi->max_open_zones)
447                 goto out;
448
449         dprint(FD_ZBD, "%s: opening zone %d\n",
450                f->file_name, zone_idx);
451
452         zbdi->open_zones[zbdi->num_open_zones++] = zone_idx;
453         td->num_open_zones++;
454         z->open = 1;
455         res = true;
456
457 out:
458         pthread_mutex_unlock(&zbdi->mutex);
459         return res;
460 }
461
462 /* Verify whether direct I/O is used for all host-managed zoned drives. */
463 static bool zbd_using_direct_io(void)
464 {
465         struct thread_data *td;
466         struct fio_file *f;
467         int i, j;
468
469         for_each_td(td, i) {
470                 if (td->o.odirect || !(td->o.td_ddir & TD_DDIR_WRITE))
471                         continue;
472                 for_each_file(td, f, j) {
473                         if (f->zbd_info &&
474                             f->zbd_info->model == ZBD_HOST_MANAGED)
475                                 return false;
476                 }
477         }
478
479         return true;
480 }
481
482 /* Whether or not the I/O range for f includes one or more sequential zones */
483 static bool zbd_is_seq_job(struct fio_file *f)
484 {
485         uint32_t zone_idx, zone_idx_b, zone_idx_e;
486
487         assert(f->zbd_info);
488
489         if (f->io_size == 0)
490                 return false;
491
492         zone_idx_b = zbd_zone_idx(f, f->file_offset);
493         zone_idx_e = zbd_zone_idx(f, f->file_offset + f->io_size - 1);
494         for (zone_idx = zone_idx_b; zone_idx <= zone_idx_e; zone_idx++)
495                 if (get_zone(f, zone_idx)->has_wp)
496                         return true;
497
498         return false;
499 }
500
501 /*
502  * Verify whether the file offset and size parameters are aligned with zone
503  * boundaries. If the file offset is not aligned, align it down to the start of
504  * the zone containing the start offset and align up the file io_size parameter.
505  */
506 static bool zbd_zone_align_file_sizes(struct thread_data *td,
507                                       struct fio_file *f)
508 {
509         const struct fio_zone_info *z;
510         uint64_t new_offset, new_end;
511         uint32_t zone_idx;
512
513         if (!f->zbd_info)
514                 return true;
515         if (f->file_offset >= f->real_file_size)
516                 return true;
517         if (!zbd_is_seq_job(f))
518                 return true;
519
520         if (!td->o.zone_size) {
521                 td->o.zone_size = f->zbd_info->zone_size;
522                 if (!td->o.zone_size) {
523                         log_err("%s: invalid 0 zone size\n",
524                                 f->file_name);
525                         return false;
526                 }
527         } else if (td->o.zone_size != f->zbd_info->zone_size) {
528                 log_err("%s: zonesize %llu does not match the device zone size %"PRIu64".\n",
529                         f->file_name, td->o.zone_size,
530                         f->zbd_info->zone_size);
531                 return false;
532         }
533
534         if (td->o.zone_skip % td->o.zone_size) {
535                 log_err("%s: zoneskip %llu is not a multiple of the device zone size %llu.\n",
536                         f->file_name, td->o.zone_skip,
537                         td->o.zone_size);
538                 return false;
539         }
540
541         zone_idx = zbd_zone_idx(f, f->file_offset);
542         z = get_zone(f, zone_idx);
543         if ((f->file_offset != z->start) &&
544             (td->o.td_ddir != TD_DDIR_READ)) {
545                 new_offset = zbd_zone_end(z);
546                 if (new_offset >= f->file_offset + f->io_size) {
547                         log_info("%s: io_size must be at least one zone\n",
548                                  f->file_name);
549                         return false;
550                 }
551                 log_info("%s: rounded up offset from %"PRIu64" to %"PRIu64"\n",
552                          f->file_name, f->file_offset,
553                          new_offset);
554                 f->io_size -= (new_offset - f->file_offset);
555                 f->file_offset = new_offset;
556         }
557
558         zone_idx = zbd_zone_idx(f, f->file_offset + f->io_size);
559         z = get_zone(f, zone_idx);
560         new_end = z->start;
561         if ((td->o.td_ddir != TD_DDIR_READ) &&
562             (f->file_offset + f->io_size != new_end)) {
563                 if (new_end <= f->file_offset) {
564                         log_info("%s: io_size must be at least one zone\n",
565                                  f->file_name);
566                         return false;
567                 }
568                 log_info("%s: rounded down io_size from %"PRIu64" to %"PRIu64"\n",
569                          f->file_name, f->io_size,
570                          new_end - f->file_offset);
571                 f->io_size = new_end - f->file_offset;
572         }
573
574         return true;
575 }
576
577 /*
578  * Verify whether offset and size parameters are aligned with zone boundaries.
579  */
580 static bool zbd_verify_sizes(void)
581 {
582         struct thread_data *td;
583         struct fio_file *f;
584         int i, j;
585
586         for_each_td(td, i) {
587                 for_each_file(td, f, j) {
588                         if (!zbd_zone_align_file_sizes(td, f))
589                                 return false;
590                 }
591         }
592
593         return true;
594 }
595
596 static bool zbd_verify_bs(void)
597 {
598         struct thread_data *td;
599         struct fio_file *f;
600         int i, j, k;
601
602         for_each_td(td, i) {
603                 if (td_trim(td) &&
604                     (td->o.min_bs[DDIR_TRIM] != td->o.max_bs[DDIR_TRIM] ||
605                      td->o.bssplit_nr[DDIR_TRIM])) {
606                         log_info("bsrange and bssplit are not allowed for trim with zonemode=zbd\n");
607                         return false;
608                 }
609                 for_each_file(td, f, j) {
610                         uint64_t zone_size;
611
612                         if (!f->zbd_info)
613                                 continue;
614
615                         zone_size = f->zbd_info->zone_size;
616                         if (td_trim(td) && td->o.bs[DDIR_TRIM] != zone_size) {
617                                 log_info("%s: trim block size %llu is not the zone size %"PRIu64"\n",
618                                          f->file_name, td->o.bs[DDIR_TRIM],
619                                          zone_size);
620                                 return false;
621                         }
622                         for (k = 0; k < FIO_ARRAY_SIZE(td->o.bs); k++) {
623                                 if (td->o.verify != VERIFY_NONE &&
624                                     zone_size % td->o.bs[k] != 0) {
625                                         log_info("%s: block size %llu is not a divisor of the zone size %"PRIu64"\n",
626                                                  f->file_name, td->o.bs[k],
627                                                  zone_size);
628                                         return false;
629                                 }
630                         }
631                 }
632         }
633         return true;
634 }
635
636 static int ilog2(uint64_t i)
637 {
638         int log = -1;
639
640         while (i) {
641                 i >>= 1;
642                 log++;
643         }
644         return log;
645 }
646
647 /*
648  * Initialize f->zbd_info for devices that are not zoned block devices. This
649  * allows to execute a ZBD workload against a non-ZBD device.
650  */
651 static int init_zone_info(struct thread_data *td, struct fio_file *f)
652 {
653         uint32_t nr_zones;
654         struct fio_zone_info *p;
655         uint64_t zone_size = td->o.zone_size;
656         uint64_t zone_capacity = td->o.zone_capacity;
657         struct zoned_block_device_info *zbd_info = NULL;
658         int i;
659
660         if (zone_size == 0) {
661                 log_err("%s: Specifying the zone size is mandatory for regular file/block device with --zonemode=zbd\n\n",
662                         f->file_name);
663                 return 1;
664         }
665
666         if (zone_size < 512) {
667                 log_err("%s: zone size must be at least 512 bytes for --zonemode=zbd\n\n",
668                         f->file_name);
669                 return 1;
670         }
671
672         if (zone_capacity == 0)
673                 zone_capacity = zone_size;
674
675         if (zone_capacity > zone_size) {
676                 log_err("%s: job parameter zonecapacity %llu is larger than zone size %llu\n",
677                         f->file_name, td->o.zone_capacity, td->o.zone_size);
678                 return 1;
679         }
680
681         if (f->real_file_size < zone_size) {
682                 log_err("%s: file/device size %"PRIu64" is smaller than zone size %"PRIu64"\n",
683                         f->file_name, f->real_file_size, zone_size);
684                 return -EINVAL;
685         }
686
687         nr_zones = (f->real_file_size + zone_size - 1) / zone_size;
688         zbd_info = scalloc(1, sizeof(*zbd_info) +
689                            (nr_zones + 1) * sizeof(zbd_info->zone_info[0]));
690         if (!zbd_info)
691                 return -ENOMEM;
692
693         mutex_init_pshared(&zbd_info->mutex);
694         zbd_info->refcount = 1;
695         p = &zbd_info->zone_info[0];
696         for (i = 0; i < nr_zones; i++, p++) {
697                 mutex_init_pshared_with_type(&p->mutex,
698                                              PTHREAD_MUTEX_RECURSIVE);
699                 p->start = i * zone_size;
700                 p->wp = p->start;
701                 p->type = ZBD_ZONE_TYPE_SWR;
702                 p->cond = ZBD_ZONE_COND_EMPTY;
703                 p->capacity = zone_capacity;
704                 p->has_wp = 1;
705         }
706         /* a sentinel */
707         p->start = nr_zones * zone_size;
708
709         f->zbd_info = zbd_info;
710         f->zbd_info->zone_size = zone_size;
711         f->zbd_info->zone_size_log2 = is_power_of_2(zone_size) ?
712                 ilog2(zone_size) : 0;
713         f->zbd_info->nr_zones = nr_zones;
714         return 0;
715 }
716
717 /*
718  * Maximum number of zones to report in one operation.
719  */
720 #define ZBD_REPORT_MAX_ZONES    8192U
721
722 /*
723  * Parse the device zone report and store it in f->zbd_info. Must be called
724  * only for devices that are zoned, namely those with a model != ZBD_NONE.
725  */
726 static int parse_zone_info(struct thread_data *td, struct fio_file *f)
727 {
728         int nr_zones, nrz;
729         struct zbd_zone *zones, *z;
730         struct fio_zone_info *p;
731         uint64_t zone_size, offset;
732         struct zoned_block_device_info *zbd_info = NULL;
733         int i, j, ret = -ENOMEM;
734
735         zones = calloc(ZBD_REPORT_MAX_ZONES, sizeof(struct zbd_zone));
736         if (!zones)
737                 goto out;
738
739         nrz = zbd_report_zones(td, f, 0, zones, ZBD_REPORT_MAX_ZONES);
740         if (nrz < 0) {
741                 ret = nrz;
742                 log_info("fio: report zones (offset 0) failed for %s (%d).\n",
743                          f->file_name, -ret);
744                 goto out;
745         }
746
747         zone_size = zones[0].len;
748         nr_zones = (f->real_file_size + zone_size - 1) / zone_size;
749
750         if (td->o.zone_size == 0) {
751                 td->o.zone_size = zone_size;
752         } else if (td->o.zone_size != zone_size) {
753                 log_err("fio: %s job parameter zonesize %llu does not match disk zone size %"PRIu64".\n",
754                         f->file_name, td->o.zone_size, zone_size);
755                 ret = -EINVAL;
756                 goto out;
757         }
758
759         dprint(FD_ZBD, "Device %s has %d zones of size %"PRIu64" KB\n",
760                f->file_name, nr_zones, zone_size / 1024);
761
762         zbd_info = scalloc(1, sizeof(*zbd_info) +
763                            (nr_zones + 1) * sizeof(zbd_info->zone_info[0]));
764         if (!zbd_info)
765                 goto out;
766         mutex_init_pshared(&zbd_info->mutex);
767         zbd_info->refcount = 1;
768         p = &zbd_info->zone_info[0];
769         for (offset = 0, j = 0; j < nr_zones;) {
770                 z = &zones[0];
771                 for (i = 0; i < nrz; i++, j++, z++, p++) {
772                         mutex_init_pshared_with_type(&p->mutex,
773                                                      PTHREAD_MUTEX_RECURSIVE);
774                         p->start = z->start;
775                         p->capacity = z->capacity;
776
777                         switch (z->cond) {
778                         case ZBD_ZONE_COND_NOT_WP:
779                         case ZBD_ZONE_COND_FULL:
780                                 p->wp = p->start + p->capacity;
781                                 break;
782                         default:
783                                 assert(z->start <= z->wp);
784                                 assert(z->wp <= z->start + zone_size);
785                                 p->wp = z->wp;
786                                 break;
787                         }
788
789                         switch (z->type) {
790                         case ZBD_ZONE_TYPE_SWR:
791                                 p->has_wp = 1;
792                                 break;
793                         default:
794                                 p->has_wp = 0;
795                         }
796                         p->type = z->type;
797                         p->cond = z->cond;
798
799                         if (j > 0 && p->start != p[-1].start + zone_size) {
800                                 log_info("%s: invalid zone data\n",
801                                          f->file_name);
802                                 ret = -EINVAL;
803                                 goto out;
804                         }
805                 }
806                 z--;
807                 offset = z->start + z->len;
808                 if (j >= nr_zones)
809                         break;
810
811                 nrz = zbd_report_zones(td, f, offset, zones,
812                                        min((uint32_t)(nr_zones - j),
813                                            ZBD_REPORT_MAX_ZONES));
814                 if (nrz < 0) {
815                         ret = nrz;
816                         log_info("fio: report zones (offset %"PRIu64") failed for %s (%d).\n",
817                                  offset, f->file_name, -ret);
818                         goto out;
819                 }
820         }
821
822         /* a sentinel */
823         zbd_info->zone_info[nr_zones].start = offset;
824
825         f->zbd_info = zbd_info;
826         f->zbd_info->zone_size = zone_size;
827         f->zbd_info->zone_size_log2 = is_power_of_2(zone_size) ?
828                 ilog2(zone_size) : 0;
829         f->zbd_info->nr_zones = nr_zones;
830         zbd_info = NULL;
831         ret = 0;
832
833 out:
834         sfree(zbd_info);
835         free(zones);
836         return ret;
837 }
838
839 static int zbd_set_max_open_zones(struct thread_data *td, struct fio_file *f)
840 {
841         struct zoned_block_device_info *zbd = f->zbd_info;
842         unsigned int max_open_zones;
843         int ret;
844
845         if (zbd->model != ZBD_HOST_MANAGED || td->o.ignore_zone_limits) {
846                 /* Only host-managed devices have a max open limit */
847                 zbd->max_open_zones = td->o.max_open_zones;
848                 goto out;
849         }
850
851         /* If host-managed, get the max open limit */
852         ret = zbd_get_max_open_zones(td, f, &max_open_zones);
853         if (ret)
854                 return ret;
855
856         if (!max_open_zones) {
857                 /* No device limit */
858                 zbd->max_open_zones = td->o.max_open_zones;
859         } else if (!td->o.max_open_zones) {
860                 /* No user limit. Set limit to device limit */
861                 zbd->max_open_zones = max_open_zones;
862         } else if (td->o.max_open_zones <= max_open_zones) {
863                 /* Both user limit and dev limit. User limit not too large */
864                 zbd->max_open_zones = td->o.max_open_zones;
865         } else {
866                 /* Both user limit and dev limit. User limit too large */
867                 td_verror(td, EINVAL,
868                           "Specified --max_open_zones is too large");
869                 log_err("Specified --max_open_zones (%d) is larger than max (%u)\n",
870                         td->o.max_open_zones, max_open_zones);
871                 return -EINVAL;
872         }
873
874 out:
875         /* Ensure that the limit is not larger than FIO's internal limit */
876         if (zbd->max_open_zones > ZBD_MAX_OPEN_ZONES) {
877                 td_verror(td, EINVAL, "'max_open_zones' value is too large");
878                 log_err("'max_open_zones' value is larger than %u\n",
879                         ZBD_MAX_OPEN_ZONES);
880                 return -EINVAL;
881         }
882
883         dprint(FD_ZBD, "%s: using max open zones limit: %"PRIu32"\n",
884                f->file_name, zbd->max_open_zones);
885
886         return 0;
887 }
888
889 /*
890  * Allocate zone information and store it into f->zbd_info if zonemode=zbd.
891  *
892  * Returns 0 upon success and a negative error code upon failure.
893  */
894 static int zbd_create_zone_info(struct thread_data *td, struct fio_file *f)
895 {
896         enum zbd_zoned_model zbd_model;
897         int ret;
898
899         assert(td->o.zone_mode == ZONE_MODE_ZBD);
900
901         ret = zbd_get_zoned_model(td, f, &zbd_model);
902         if (ret)
903                 return ret;
904
905         switch (zbd_model) {
906         case ZBD_HOST_AWARE:
907         case ZBD_HOST_MANAGED:
908                 ret = parse_zone_info(td, f);
909                 if (ret)
910                         return ret;
911                 break;
912         case ZBD_NONE:
913                 ret = init_zone_info(td, f);
914                 if (ret)
915                         return ret;
916                 break;
917         default:
918                 td_verror(td, EINVAL, "Unsupported zoned model");
919                 log_err("Unsupported zoned model\n");
920                 return -EINVAL;
921         }
922
923         assert(f->zbd_info);
924         f->zbd_info->model = zbd_model;
925
926         ret = zbd_set_max_open_zones(td, f);
927         if (ret) {
928                 zbd_free_zone_info(f);
929                 return ret;
930         }
931
932         return 0;
933 }
934
935 void zbd_free_zone_info(struct fio_file *f)
936 {
937         uint32_t refcount;
938
939         assert(f->zbd_info);
940
941         pthread_mutex_lock(&f->zbd_info->mutex);
942         refcount = --f->zbd_info->refcount;
943         pthread_mutex_unlock(&f->zbd_info->mutex);
944
945         assert((int32_t)refcount >= 0);
946         if (refcount == 0)
947                 sfree(f->zbd_info);
948         f->zbd_info = NULL;
949 }
950
951 /*
952  * Initialize f->zbd_info.
953  *
954  * Returns 0 upon success and a negative error code upon failure.
955  *
956  * Note: this function can only work correctly if it is called before the first
957  * fio fork() call.
958  */
959 static int zbd_init_zone_info(struct thread_data *td, struct fio_file *file)
960 {
961         struct thread_data *td2;
962         struct fio_file *f2;
963         int i, j, ret;
964
965         for_each_td(td2, i) {
966                 for_each_file(td2, f2, j) {
967                         if (td2 == td && f2 == file)
968                                 continue;
969                         if (!f2->zbd_info ||
970                             strcmp(f2->file_name, file->file_name) != 0)
971                                 continue;
972                         file->zbd_info = f2->zbd_info;
973                         file->zbd_info->refcount++;
974                         return 0;
975                 }
976         }
977
978         ret = zbd_create_zone_info(td, file);
979         if (ret < 0)
980                 td_verror(td, -ret, "zbd_create_zone_info() failed");
981
982         return ret;
983 }
984
985 int zbd_init_files(struct thread_data *td)
986 {
987         struct fio_file *f;
988         int i;
989
990         for_each_file(td, f, i) {
991                 if (zbd_init_zone_info(td, f))
992                         return 1;
993         }
994
995         return 0;
996 }
997
998 void zbd_recalc_options_with_zone_granularity(struct thread_data *td)
999 {
1000         struct fio_file *f;
1001         int i;
1002
1003         for_each_file(td, f, i) {
1004                 struct zoned_block_device_info *zbd = f->zbd_info;
1005                 uint64_t zone_size;
1006
1007                 /* zonemode=strided doesn't get per-file zone size. */
1008                 zone_size = zbd ? zbd->zone_size : td->o.zone_size;
1009                 if (zone_size == 0)
1010                         continue;
1011
1012                 if (td->o.size_nz > 0)
1013                         td->o.size = td->o.size_nz * zone_size;
1014                 if (td->o.io_size_nz > 0)
1015                         td->o.io_size = td->o.io_size_nz * zone_size;
1016                 if (td->o.start_offset_nz > 0)
1017                         td->o.start_offset = td->o.start_offset_nz * zone_size;
1018                 if (td->o.offset_increment_nz > 0)
1019                         td->o.offset_increment =
1020                                 td->o.offset_increment_nz * zone_size;
1021                 if (td->o.zone_skip_nz > 0)
1022                         td->o.zone_skip = td->o.zone_skip_nz * zone_size;
1023         }
1024 }
1025
1026 int zbd_setup_files(struct thread_data *td)
1027 {
1028         struct fio_file *f;
1029         int i;
1030
1031         if (!zbd_using_direct_io()) {
1032                 log_err("Using direct I/O is mandatory for writing to ZBD drives\n\n");
1033                 return 1;
1034         }
1035
1036         if (!zbd_verify_sizes())
1037                 return 1;
1038
1039         if (!zbd_verify_bs())
1040                 return 1;
1041
1042         for_each_file(td, f, i) {
1043                 struct zoned_block_device_info *zbd = f->zbd_info;
1044                 struct fio_zone_info *z;
1045                 int zi;
1046
1047                 assert(zbd);
1048
1049                 f->min_zone = zbd_zone_idx(f, f->file_offset);
1050                 f->max_zone = zbd_zone_idx(f, f->file_offset + f->io_size);
1051
1052                 /*
1053                  * When all zones in the I/O range are conventional, io_size
1054                  * can be smaller than zone size, making min_zone the same
1055                  * as max_zone. This is why the assert below needs to be made
1056                  * conditional.
1057                  */
1058                 if (zbd_is_seq_job(f))
1059                         assert(f->min_zone < f->max_zone);
1060
1061                 if (td->o.max_open_zones > 0 &&
1062                     zbd->max_open_zones != td->o.max_open_zones) {
1063                         log_err("Different 'max_open_zones' values\n");
1064                         return 1;
1065                 }
1066
1067                 /*
1068                  * The per job max open zones limit cannot be used without a
1069                  * global max open zones limit. (As the tracking of open zones
1070                  * is disabled when there is no global max open zones limit.)
1071                  */
1072                 if (td->o.job_max_open_zones && !zbd->max_open_zones) {
1073                         log_err("'job_max_open_zones' cannot be used without a global open zones limit\n");
1074                         return 1;
1075                 }
1076
1077                 /*
1078                  * zbd->max_open_zones is the global limit shared for all jobs
1079                  * that target the same zoned block device. Force sync the per
1080                  * thread global limit with the actual global limit. (The real
1081                  * per thread/job limit is stored in td->o.job_max_open_zones).
1082                  */
1083                 td->o.max_open_zones = zbd->max_open_zones;
1084
1085                 for (zi = f->min_zone; zi < f->max_zone; zi++) {
1086                         z = &zbd->zone_info[zi];
1087                         if (z->cond != ZBD_ZONE_COND_IMP_OPEN &&
1088                             z->cond != ZBD_ZONE_COND_EXP_OPEN)
1089                                 continue;
1090                         if (zbd_open_zone(td, f, zi))
1091                                 continue;
1092                         /*
1093                          * If the number of open zones exceeds specified limits,
1094                          * reset all extra open zones.
1095                          */
1096                         if (zbd_reset_zone(td, f, z) < 0) {
1097                                 log_err("Failed to reest zone %d\n", zi);
1098                                 return 1;
1099                         }
1100                 }
1101         }
1102
1103         return 0;
1104 }
1105
1106 /*
1107  * Reset zbd_info.write_cnt, the counter that counts down towards the next
1108  * zone reset.
1109  */
1110 static void _zbd_reset_write_cnt(const struct thread_data *td,
1111                                  const struct fio_file *f)
1112 {
1113         assert(0 <= td->o.zrf.u.f && td->o.zrf.u.f <= 1);
1114
1115         f->zbd_info->write_cnt = td->o.zrf.u.f ?
1116                 min(1.0 / td->o.zrf.u.f, 0.0 + UINT_MAX) : UINT_MAX;
1117 }
1118
1119 static void zbd_reset_write_cnt(const struct thread_data *td,
1120                                 const struct fio_file *f)
1121 {
1122         pthread_mutex_lock(&f->zbd_info->mutex);
1123         _zbd_reset_write_cnt(td, f);
1124         pthread_mutex_unlock(&f->zbd_info->mutex);
1125 }
1126
1127 static bool zbd_dec_and_reset_write_cnt(const struct thread_data *td,
1128                                         const struct fio_file *f)
1129 {
1130         uint32_t write_cnt = 0;
1131
1132         pthread_mutex_lock(&f->zbd_info->mutex);
1133         assert(f->zbd_info->write_cnt);
1134         if (f->zbd_info->write_cnt)
1135                 write_cnt = --f->zbd_info->write_cnt;
1136         if (write_cnt == 0)
1137                 _zbd_reset_write_cnt(td, f);
1138         pthread_mutex_unlock(&f->zbd_info->mutex);
1139
1140         return write_cnt == 0;
1141 }
1142
1143 enum swd_action {
1144         CHECK_SWD,
1145         SET_SWD,
1146 };
1147
1148 /* Calculate the number of sectors with data (swd) and perform action 'a' */
1149 static uint64_t zbd_process_swd(struct thread_data *td,
1150                                 const struct fio_file *f, enum swd_action a)
1151 {
1152         struct fio_zone_info *zb, *ze, *z;
1153         uint64_t swd = 0;
1154         uint64_t wp_swd = 0;
1155
1156         zb = get_zone(f, f->min_zone);
1157         ze = get_zone(f, f->max_zone);
1158         for (z = zb; z < ze; z++) {
1159                 if (z->has_wp) {
1160                         zone_lock(td, f, z);
1161                         wp_swd += z->wp - z->start;
1162                 }
1163                 swd += z->wp - z->start;
1164         }
1165
1166         pthread_mutex_lock(&f->zbd_info->mutex);
1167         switch (a) {
1168         case CHECK_SWD:
1169                 assert(f->zbd_info->sectors_with_data == swd);
1170                 assert(f->zbd_info->wp_sectors_with_data == wp_swd);
1171                 break;
1172         case SET_SWD:
1173                 f->zbd_info->sectors_with_data = swd;
1174                 f->zbd_info->wp_sectors_with_data = wp_swd;
1175                 break;
1176         }
1177         pthread_mutex_unlock(&f->zbd_info->mutex);
1178
1179         for (z = zb; z < ze; z++)
1180                 if (z->has_wp)
1181                         zone_unlock(z);
1182
1183         return swd;
1184 }
1185
1186 /*
1187  * The swd check is useful for debugging but takes too much time to leave
1188  * it enabled all the time. Hence it is disabled by default.
1189  */
1190 static const bool enable_check_swd = false;
1191
1192 /* Check whether the values of zbd_info.*sectors_with_data are correct. */
1193 static void zbd_check_swd(struct thread_data *td, const struct fio_file *f)
1194 {
1195         if (!enable_check_swd)
1196                 return;
1197
1198         zbd_process_swd(td, f, CHECK_SWD);
1199 }
1200
1201 void zbd_file_reset(struct thread_data *td, struct fio_file *f)
1202 {
1203         struct fio_zone_info *zb, *ze;
1204         uint64_t swd;
1205
1206         if (!f->zbd_info || !td_write(td))
1207                 return;
1208
1209         zb = get_zone(f, f->min_zone);
1210         ze = get_zone(f, f->max_zone);
1211         swd = zbd_process_swd(td, f, SET_SWD);
1212
1213         dprint(FD_ZBD, "%s(%s): swd = %" PRIu64 "\n",
1214                __func__, f->file_name, swd);
1215
1216         /*
1217          * If data verification is enabled reset the affected zones before
1218          * writing any data to avoid that a zone reset has to be issued while
1219          * writing data, which causes data loss.
1220          */
1221         if (td->o.verify != VERIFY_NONE && td->runstate != TD_VERIFYING)
1222                 zbd_reset_zones(td, f, zb, ze);
1223         zbd_reset_write_cnt(td, f);
1224 }
1225
1226 /* Return random zone index for one of the open zones. */
1227 static uint32_t pick_random_zone_idx(const struct fio_file *f,
1228                                      const struct io_u *io_u)
1229 {
1230         return (io_u->offset - f->file_offset) *
1231                 f->zbd_info->num_open_zones / f->io_size;
1232 }
1233
1234 static bool any_io_in_flight(void)
1235 {
1236         struct thread_data *td;
1237         int i;
1238
1239         for_each_td(td, i) {
1240                 if (td->io_u_in_flight)
1241                         return true;
1242         }
1243
1244         return false;
1245 }
1246
1247 /*
1248  * Modify the offset of an I/O unit that does not refer to an open zone such
1249  * that it refers to an open zone. Close an open zone and open a new zone if
1250  * necessary. The open zone is searched across sequential zones.
1251  * This algorithm can only work correctly if all write pointers are
1252  * a multiple of the fio block size. The caller must neither hold z->mutex
1253  * nor f->zbd_info->mutex. Returns with z->mutex held upon success.
1254  */
1255 static struct fio_zone_info *zbd_convert_to_open_zone(struct thread_data *td,
1256                                                       struct io_u *io_u)
1257 {
1258         const uint64_t min_bs = td->o.min_bs[io_u->ddir];
1259         struct fio_file *f = io_u->file;
1260         struct zoned_block_device_info *zbdi = f->zbd_info;
1261         struct fio_zone_info *z;
1262         unsigned int open_zone_idx = -1;
1263         uint32_t zone_idx, new_zone_idx;
1264         int i;
1265         bool wait_zone_close;
1266         bool in_flight;
1267         bool should_retry = true;
1268
1269         assert(is_valid_offset(f, io_u->offset));
1270
1271         if (zbdi->max_open_zones || td->o.job_max_open_zones) {
1272                 /*
1273                  * This statement accesses zbdi->open_zones[] on purpose
1274                  * without locking.
1275                  */
1276                 zone_idx = zbdi->open_zones[pick_random_zone_idx(f, io_u)];
1277         } else {
1278                 zone_idx = zbd_zone_idx(f, io_u->offset);
1279         }
1280         if (zone_idx < f->min_zone)
1281                 zone_idx = f->min_zone;
1282         else if (zone_idx >= f->max_zone)
1283                 zone_idx = f->max_zone - 1;
1284
1285         dprint(FD_ZBD,
1286                "%s(%s): starting from zone %d (offset %lld, buflen %lld)\n",
1287                __func__, f->file_name, zone_idx, io_u->offset, io_u->buflen);
1288
1289         /*
1290          * Since z->mutex is the outer lock and zbdi->mutex the inner
1291          * lock it can happen that the state of the zone with index zone_idx
1292          * has changed after 'z' has been assigned and before zbdi->mutex
1293          * has been obtained. Hence the loop.
1294          */
1295         for (;;) {
1296                 uint32_t tmp_idx;
1297
1298                 z = get_zone(f, zone_idx);
1299                 if (z->has_wp)
1300                         zone_lock(td, f, z);
1301
1302                 pthread_mutex_lock(&zbdi->mutex);
1303
1304                 if (z->has_wp) {
1305                         if (z->cond != ZBD_ZONE_COND_OFFLINE &&
1306                             zbdi->max_open_zones == 0 &&
1307                             td->o.job_max_open_zones == 0)
1308                                 goto examine_zone;
1309                         if (zbdi->num_open_zones == 0) {
1310                                 dprint(FD_ZBD, "%s(%s): no zones are open\n",
1311                                        __func__, f->file_name);
1312                                 goto open_other_zone;
1313                         }
1314                 }
1315
1316                 /*
1317                  * List of opened zones is per-device, shared across all
1318                  * threads. Start with quasi-random candidate zone. Ignore
1319                  * zones which don't belong to thread's offset/size area.
1320                  */
1321                 open_zone_idx = pick_random_zone_idx(f, io_u);
1322                 assert(!open_zone_idx ||
1323                        open_zone_idx < zbdi->num_open_zones);
1324                 tmp_idx = open_zone_idx;
1325
1326                 for (i = 0; i < zbdi->num_open_zones; i++) {
1327                         uint32_t tmpz;
1328
1329                         if (tmp_idx >= zbdi->num_open_zones)
1330                                 tmp_idx = 0;
1331                         tmpz = zbdi->open_zones[tmp_idx];
1332                         if (f->min_zone <= tmpz && tmpz < f->max_zone) {
1333                                 open_zone_idx = tmp_idx;
1334                                 goto found_candidate_zone;
1335                         }
1336
1337                         tmp_idx++;
1338                 }
1339
1340                 dprint(FD_ZBD, "%s(%s): no candidate zone\n",
1341                         __func__, f->file_name);
1342
1343                 pthread_mutex_unlock(&zbdi->mutex);
1344
1345                 if (z->has_wp)
1346                         zone_unlock(z);
1347
1348                 return NULL;
1349
1350 found_candidate_zone:
1351                 new_zone_idx = zbdi->open_zones[open_zone_idx];
1352                 if (new_zone_idx == zone_idx)
1353                         break;
1354                 zone_idx = new_zone_idx;
1355
1356                 pthread_mutex_unlock(&zbdi->mutex);
1357
1358                 if (z->has_wp)
1359                         zone_unlock(z);
1360         }
1361
1362         /* Both z->mutex and zbdi->mutex are held. */
1363
1364 examine_zone:
1365         if (z->wp + min_bs <= zbd_zone_capacity_end(z)) {
1366                 pthread_mutex_unlock(&zbdi->mutex);
1367                 goto out;
1368         }
1369
1370 open_other_zone:
1371         /* Check if number of open zones reaches one of limits. */
1372         wait_zone_close =
1373                 zbdi->num_open_zones == f->max_zone - f->min_zone ||
1374                 (zbdi->max_open_zones &&
1375                  zbdi->num_open_zones == zbdi->max_open_zones) ||
1376                 (td->o.job_max_open_zones &&
1377                  td->num_open_zones == td->o.job_max_open_zones);
1378
1379         pthread_mutex_unlock(&zbdi->mutex);
1380
1381         /* Only z->mutex is held. */
1382
1383         /*
1384          * When number of open zones reaches to one of limits, wait for
1385          * zone close before opening a new zone.
1386          */
1387         if (wait_zone_close) {
1388                 dprint(FD_ZBD,
1389                        "%s(%s): quiesce to allow open zones to close\n",
1390                        __func__, f->file_name);
1391                 io_u_quiesce(td);
1392         }
1393
1394 retry:
1395         /* Zone 'z' is full, so try to open a new zone. */
1396         for (i = f->io_size / zbdi->zone_size; i > 0; i--) {
1397                 zone_idx++;
1398                 if (z->has_wp)
1399                         zone_unlock(z);
1400                 z++;
1401                 if (!is_valid_offset(f, z->start)) {
1402                         /* Wrap-around. */
1403                         zone_idx = f->min_zone;
1404                         z = get_zone(f, zone_idx);
1405                 }
1406                 assert(is_valid_offset(f, z->start));
1407                 if (!z->has_wp)
1408                         continue;
1409                 zone_lock(td, f, z);
1410                 if (z->open)
1411                         continue;
1412                 if (zbd_open_zone(td, f, zone_idx))
1413                         goto out;
1414         }
1415
1416         /* Only z->mutex is held. */
1417
1418         /* Check whether the write fits in any of the already opened zones. */
1419         pthread_mutex_lock(&zbdi->mutex);
1420         for (i = 0; i < zbdi->num_open_zones; i++) {
1421                 zone_idx = zbdi->open_zones[i];
1422                 if (zone_idx < f->min_zone || zone_idx >= f->max_zone)
1423                         continue;
1424                 pthread_mutex_unlock(&zbdi->mutex);
1425                 zone_unlock(z);
1426
1427                 z = get_zone(f, zone_idx);
1428
1429                 zone_lock(td, f, z);
1430                 if (z->wp + min_bs <= zbd_zone_capacity_end(z))
1431                         goto out;
1432                 pthread_mutex_lock(&zbdi->mutex);
1433         }
1434
1435         /*
1436          * When any I/O is in-flight or when all I/Os in-flight get completed,
1437          * the I/Os might have closed zones then retry the steps to open a zone.
1438          * Before retry, call io_u_quiesce() to complete in-flight writes.
1439          */
1440         in_flight = any_io_in_flight();
1441         if (in_flight || should_retry) {
1442                 dprint(FD_ZBD,
1443                        "%s(%s): wait zone close and retry open zones\n",
1444                        __func__, f->file_name);
1445                 pthread_mutex_unlock(&zbdi->mutex);
1446                 zone_unlock(z);
1447                 io_u_quiesce(td);
1448                 zone_lock(td, f, z);
1449                 should_retry = in_flight;
1450                 goto retry;
1451         }
1452
1453         pthread_mutex_unlock(&zbdi->mutex);
1454
1455         zone_unlock(z);
1456
1457         dprint(FD_ZBD, "%s(%s): did not open another zone\n",
1458                __func__, f->file_name);
1459
1460         return NULL;
1461
1462 out:
1463         dprint(FD_ZBD, "%s(%s): returning zone %d\n",
1464                __func__, f->file_name, zone_idx);
1465
1466         io_u->offset = z->start;
1467         assert(z->has_wp);
1468         assert(z->cond != ZBD_ZONE_COND_OFFLINE);
1469
1470         return z;
1471 }
1472
1473 /* The caller must hold z->mutex. */
1474 static struct fio_zone_info *zbd_replay_write_order(struct thread_data *td,
1475                                                     struct io_u *io_u,
1476                                                     struct fio_zone_info *z)
1477 {
1478         const struct fio_file *f = io_u->file;
1479         const uint64_t min_bs = td->o.min_bs[DDIR_WRITE];
1480
1481         if (!zbd_open_zone(td, f, zbd_zone_nr(f, z))) {
1482                 zone_unlock(z);
1483                 z = zbd_convert_to_open_zone(td, io_u);
1484                 assert(z);
1485         }
1486
1487         if (z->verify_block * min_bs >= z->capacity) {
1488                 log_err("%s: %d * %"PRIu64" >= %"PRIu64"\n",
1489                         f->file_name, z->verify_block, min_bs, z->capacity);
1490                 /*
1491                  * If the assertion below fails during a test run, adding
1492                  * "--experimental_verify=1" to the command line may help.
1493                  */
1494                 assert(false);
1495         }
1496
1497         io_u->offset = z->start + z->verify_block * min_bs;
1498         if (io_u->offset + io_u->buflen >= zbd_zone_capacity_end(z)) {
1499                 log_err("%s: %llu + %llu >= %"PRIu64"\n",
1500                         f->file_name, io_u->offset, io_u->buflen,
1501                         zbd_zone_capacity_end(z));
1502                 assert(false);
1503         }
1504         z->verify_block += io_u->buflen / min_bs;
1505
1506         return z;
1507 }
1508
1509 /*
1510  * Find another zone which has @min_bytes of readable data. Search in zones
1511  * @zb + 1 .. @zl. For random workload, also search in zones @zb - 1 .. @zf.
1512  *
1513  * Either returns NULL or returns a zone pointer. When the zone has write
1514  * pointer, hold the mutex for the zone.
1515  */
1516 static struct fio_zone_info *
1517 zbd_find_zone(struct thread_data *td, struct io_u *io_u, uint64_t min_bytes,
1518               struct fio_zone_info *zb, struct fio_zone_info *zl)
1519 {
1520         struct fio_file *f = io_u->file;
1521         struct fio_zone_info *z1, *z2;
1522         const struct fio_zone_info *const zf = get_zone(f, f->min_zone);
1523
1524         /*
1525          * Skip to the next non-empty zone in case of sequential I/O and to
1526          * the nearest non-empty zone in case of random I/O.
1527          */
1528         for (z1 = zb + 1, z2 = zb - 1; z1 < zl || z2 >= zf; z1++, z2--) {
1529                 if (z1 < zl && z1->cond != ZBD_ZONE_COND_OFFLINE) {
1530                         if (z1->has_wp)
1531                                 zone_lock(td, f, z1);
1532                         if (z1->start + min_bytes <= z1->wp)
1533                                 return z1;
1534                         if (z1->has_wp)
1535                                 zone_unlock(z1);
1536                 } else if (!td_random(td)) {
1537                         break;
1538                 }
1539
1540                 if (td_random(td) && z2 >= zf &&
1541                     z2->cond != ZBD_ZONE_COND_OFFLINE) {
1542                         if (z2->has_wp)
1543                                 zone_lock(td, f, z2);
1544                         if (z2->start + min_bytes <= z2->wp)
1545                                 return z2;
1546                         if (z2->has_wp)
1547                                 zone_unlock(z2);
1548                 }
1549         }
1550
1551         dprint(FD_ZBD,
1552                "%s: no zone has %"PRIu64" bytes of readable data\n",
1553                f->file_name, min_bytes);
1554
1555         return NULL;
1556 }
1557
1558 /**
1559  * zbd_end_zone_io - update zone status at command completion
1560  * @io_u: I/O unit
1561  * @z: zone info pointer
1562  *
1563  * If the write command made the zone full, close it.
1564  *
1565  * The caller must hold z->mutex.
1566  */
1567 static void zbd_end_zone_io(struct thread_data *td, const struct io_u *io_u,
1568                             struct fio_zone_info *z)
1569 {
1570         const struct fio_file *f = io_u->file;
1571
1572         if (io_u->ddir == DDIR_WRITE &&
1573             io_u->offset + io_u->buflen >= zbd_zone_capacity_end(z)) {
1574                 pthread_mutex_lock(&f->zbd_info->mutex);
1575                 zbd_close_zone(td, f, z);
1576                 pthread_mutex_unlock(&f->zbd_info->mutex);
1577         }
1578 }
1579
1580 /**
1581  * zbd_queue_io - update the write pointer of a sequential zone
1582  * @io_u: I/O unit
1583  * @success: Whether or not the I/O unit has been queued successfully
1584  * @q: queueing status (busy, completed or queued).
1585  *
1586  * For write and trim operations, update the write pointer of the I/O unit
1587  * target zone.
1588  */
1589 static void zbd_queue_io(struct thread_data *td, struct io_u *io_u, int q,
1590                          bool success)
1591 {
1592         const struct fio_file *f = io_u->file;
1593         struct zoned_block_device_info *zbd_info = f->zbd_info;
1594         struct fio_zone_info *z;
1595         uint32_t zone_idx;
1596         uint64_t zone_end;
1597
1598         assert(zbd_info);
1599
1600         zone_idx = zbd_zone_idx(f, io_u->offset);
1601         assert(zone_idx < zbd_info->nr_zones);
1602         z = get_zone(f, zone_idx);
1603
1604         assert(z->has_wp);
1605
1606         if (!success)
1607                 goto unlock;
1608
1609         dprint(FD_ZBD,
1610                "%s: queued I/O (%lld, %llu) for zone %u\n",
1611                f->file_name, io_u->offset, io_u->buflen, zone_idx);
1612
1613         switch (io_u->ddir) {
1614         case DDIR_WRITE:
1615                 zone_end = min((uint64_t)(io_u->offset + io_u->buflen),
1616                                zbd_zone_capacity_end(z));
1617
1618                 /*
1619                  * z->wp > zone_end means that one or more I/O errors
1620                  * have occurred.
1621                  */
1622                 pthread_mutex_lock(&zbd_info->mutex);
1623                 if (z->wp <= zone_end) {
1624                         zbd_info->sectors_with_data += zone_end - z->wp;
1625                         zbd_info->wp_sectors_with_data += zone_end - z->wp;
1626                 }
1627                 pthread_mutex_unlock(&zbd_info->mutex);
1628                 z->wp = zone_end;
1629                 break;
1630         default:
1631                 break;
1632         }
1633
1634         if (q == FIO_Q_COMPLETED && !io_u->error)
1635                 zbd_end_zone_io(td, io_u, z);
1636
1637 unlock:
1638         if (!success || q != FIO_Q_QUEUED) {
1639                 /* BUSY or COMPLETED: unlock the zone */
1640                 zone_unlock(z);
1641                 io_u->zbd_put_io = NULL;
1642         }
1643 }
1644
1645 /**
1646  * zbd_put_io - Unlock an I/O unit target zone lock
1647  * @io_u: I/O unit
1648  */
1649 static void zbd_put_io(struct thread_data *td, const struct io_u *io_u)
1650 {
1651         const struct fio_file *f = io_u->file;
1652         struct zoned_block_device_info *zbd_info = f->zbd_info;
1653         struct fio_zone_info *z;
1654         uint32_t zone_idx;
1655
1656         assert(zbd_info);
1657
1658         zone_idx = zbd_zone_idx(f, io_u->offset);
1659         assert(zone_idx < zbd_info->nr_zones);
1660         z = get_zone(f, zone_idx);
1661
1662         assert(z->has_wp);
1663
1664         dprint(FD_ZBD,
1665                "%s: terminate I/O (%lld, %llu) for zone %u\n",
1666                f->file_name, io_u->offset, io_u->buflen, zone_idx);
1667
1668         zbd_end_zone_io(td, io_u, z);
1669
1670         zone_unlock(z);
1671         zbd_check_swd(td, f);
1672 }
1673
1674 /*
1675  * Windows and MacOS do not define this.
1676  */
1677 #ifndef EREMOTEIO
1678 #define EREMOTEIO       121     /* POSIX value */
1679 #endif
1680
1681 bool zbd_unaligned_write(int error_code)
1682 {
1683         switch (error_code) {
1684         case EIO:
1685         case EREMOTEIO:
1686                 return true;
1687         }
1688         return false;
1689 }
1690
1691 /**
1692  * setup_zbd_zone_mode - handle zoneskip as necessary for ZBD drives
1693  * @td: FIO thread data.
1694  * @io_u: FIO I/O unit.
1695  *
1696  * For sequential workloads, change the file offset to skip zoneskip bytes when
1697  * no more IO can be performed in the current zone.
1698  * - For read workloads, zoneskip is applied when the io has reached the end of
1699  *   the zone or the zone write position (when td->o.read_beyond_wp is false).
1700  * - For write workloads, zoneskip is applied when the zone is full.
1701  * This applies only to read and write operations.
1702  */
1703 void setup_zbd_zone_mode(struct thread_data *td, struct io_u *io_u)
1704 {
1705         struct fio_file *f = io_u->file;
1706         enum fio_ddir ddir = io_u->ddir;
1707         struct fio_zone_info *z;
1708         uint32_t zone_idx;
1709
1710         assert(td->o.zone_mode == ZONE_MODE_ZBD);
1711         assert(td->o.zone_size);
1712         assert(f->zbd_info);
1713
1714         zone_idx = zbd_zone_idx(f, f->last_pos[ddir]);
1715         z = get_zone(f, zone_idx);
1716
1717         /*
1718          * When the zone capacity is smaller than the zone size and the I/O is
1719          * sequential write, skip to zone end if the latest position is at the
1720          * zone capacity limit.
1721          */
1722         if (z->capacity < f->zbd_info->zone_size &&
1723             !td_random(td) && ddir == DDIR_WRITE &&
1724             f->last_pos[ddir] >= zbd_zone_capacity_end(z)) {
1725                 dprint(FD_ZBD,
1726                        "%s: Jump from zone capacity limit to zone end:"
1727                        " (%"PRIu64" -> %"PRIu64") for zone %u (%"PRIu64")\n",
1728                        f->file_name, f->last_pos[ddir],
1729                        zbd_zone_end(z), zone_idx, z->capacity);
1730                 td->io_skip_bytes += zbd_zone_end(z) - f->last_pos[ddir];
1731                 f->last_pos[ddir] = zbd_zone_end(z);
1732         }
1733
1734         /*
1735          * zone_skip is valid only for sequential workloads.
1736          */
1737         if (td_random(td) || !td->o.zone_skip)
1738                 return;
1739
1740         /*
1741          * It is time to switch to a new zone if:
1742          * - zone_bytes == zone_size bytes have already been accessed
1743          * - The last position reached the end of the current zone.
1744          * - For reads with td->o.read_beyond_wp == false, the last position
1745          *   reached the zone write pointer.
1746          */
1747         if (td->zone_bytes >= td->o.zone_size ||
1748             f->last_pos[ddir] >= zbd_zone_end(z) ||
1749             (ddir == DDIR_READ &&
1750              (!td->o.read_beyond_wp) && f->last_pos[ddir] >= z->wp)) {
1751                 /*
1752                  * Skip zones.
1753                  */
1754                 td->zone_bytes = 0;
1755                 f->file_offset += td->o.zone_size + td->o.zone_skip;
1756
1757                 /*
1758                  * Wrap from the beginning, if we exceed the file size
1759                  */
1760                 if (f->file_offset >= f->real_file_size)
1761                         f->file_offset = get_start_offset(td, f);
1762
1763                 f->last_pos[ddir] = f->file_offset;
1764                 td->io_skip_bytes += td->o.zone_skip;
1765         }
1766 }
1767
1768 /**
1769  * zbd_adjust_ddir - Adjust an I/O direction for zonemode=zbd.
1770  *
1771  * @td: FIO thread data.
1772  * @io_u: FIO I/O unit.
1773  * @ddir: I/O direction before adjustment.
1774  *
1775  * Return adjusted I/O direction.
1776  */
1777 enum fio_ddir zbd_adjust_ddir(struct thread_data *td, struct io_u *io_u,
1778                               enum fio_ddir ddir)
1779 {
1780         /*
1781          * In case read direction is chosen for the first random I/O, fio with
1782          * zonemode=zbd stops because no data can be read from zoned block
1783          * devices with all empty zones. Overwrite the first I/O direction as
1784          * write to make sure data to read exists.
1785          */
1786         assert(io_u->file->zbd_info);
1787         if (ddir != DDIR_READ || !td_rw(td))
1788                 return ddir;
1789
1790         if (io_u->file->zbd_info->sectors_with_data ||
1791             td->o.read_beyond_wp)
1792                 return DDIR_READ;
1793
1794         return DDIR_WRITE;
1795 }
1796
1797 /**
1798  * zbd_adjust_block - adjust the offset and length as necessary for ZBD drives
1799  * @td: FIO thread data.
1800  * @io_u: FIO I/O unit.
1801  *
1802  * Locking strategy: returns with z->mutex locked if and only if z refers
1803  * to a sequential zone and if io_u_accept is returned. z is the zone that
1804  * corresponds to io_u->offset at the end of this function.
1805  */
1806 enum io_u_action zbd_adjust_block(struct thread_data *td, struct io_u *io_u)
1807 {
1808         struct fio_file *f = io_u->file;
1809         struct zoned_block_device_info *zbdi = f->zbd_info;
1810         uint32_t zone_idx_b;
1811         struct fio_zone_info *zb, *zl, *orig_zb;
1812         uint32_t orig_len = io_u->buflen;
1813         uint64_t min_bs = td->o.min_bs[io_u->ddir];
1814         uint64_t new_len;
1815         int64_t range;
1816
1817         assert(zbdi);
1818         assert(min_bs);
1819         assert(is_valid_offset(f, io_u->offset));
1820         assert(io_u->buflen);
1821
1822         zone_idx_b = zbd_zone_idx(f, io_u->offset);
1823         zb = get_zone(f, zone_idx_b);
1824         orig_zb = zb;
1825
1826         if (!zb->has_wp) {
1827                 /* Accept non-write I/Os for conventional zones. */
1828                 if (io_u->ddir != DDIR_WRITE)
1829                         return io_u_accept;
1830
1831                 /*
1832                  * Make sure that writes to conventional zones
1833                  * don't cross over to any sequential zones.
1834                  */
1835                 if (!(zb + 1)->has_wp ||
1836                     io_u->offset + io_u->buflen <= (zb + 1)->start)
1837                         return io_u_accept;
1838
1839                 if (io_u->offset + min_bs > (zb + 1)->start) {
1840                         dprint(FD_IO,
1841                                "%s: off=%llu + min_bs=%"PRIu64" > next zone %"PRIu64"\n",
1842                                f->file_name, io_u->offset,
1843                                min_bs, (zb + 1)->start);
1844                         io_u->offset =
1845                                 zb->start + (zb + 1)->start - io_u->offset;
1846                         new_len = min(io_u->buflen,
1847                                       (zb + 1)->start - io_u->offset);
1848                 } else {
1849                         new_len = (zb + 1)->start - io_u->offset;
1850                 }
1851
1852                 io_u->buflen = new_len / min_bs * min_bs;
1853
1854                 return io_u_accept;
1855         }
1856
1857         /*
1858          * Accept the I/O offset for reads if reading beyond the write pointer
1859          * is enabled.
1860          */
1861         if (zb->cond != ZBD_ZONE_COND_OFFLINE &&
1862             io_u->ddir == DDIR_READ && td->o.read_beyond_wp)
1863                 return io_u_accept;
1864
1865         zbd_check_swd(td, f);
1866
1867         zone_lock(td, f, zb);
1868
1869         switch (io_u->ddir) {
1870         case DDIR_READ:
1871                 if (td->runstate == TD_VERIFYING && td_write(td)) {
1872                         zb = zbd_replay_write_order(td, io_u, zb);
1873                         goto accept;
1874                 }
1875
1876                 /*
1877                  * Check that there is enough written data in the zone to do an
1878                  * I/O of at least min_bs B. If there isn't, find a new zone for
1879                  * the I/O.
1880                  */
1881                 range = zb->cond != ZBD_ZONE_COND_OFFLINE ?
1882                         zb->wp - zb->start : 0;
1883                 if (range < min_bs ||
1884                     ((!td_random(td)) && (io_u->offset + min_bs > zb->wp))) {
1885                         zone_unlock(zb);
1886                         zl = get_zone(f, f->max_zone);
1887                         zb = zbd_find_zone(td, io_u, min_bs, zb, zl);
1888                         if (!zb) {
1889                                 dprint(FD_ZBD,
1890                                        "%s: zbd_find_zone(%lld, %llu) failed\n",
1891                                        f->file_name, io_u->offset,
1892                                        io_u->buflen);
1893                                 goto eof;
1894                         }
1895                         /*
1896                          * zbd_find_zone() returned a zone with a range of at
1897                          * least min_bs.
1898                          */
1899                         range = zb->wp - zb->start;
1900                         assert(range >= min_bs);
1901
1902                         if (!td_random(td))
1903                                 io_u->offset = zb->start;
1904                 }
1905
1906                 /*
1907                  * Make sure the I/O is within the zone valid data range while
1908                  * maximizing the I/O size and preserving randomness.
1909                  */
1910                 if (range <= io_u->buflen)
1911                         io_u->offset = zb->start;
1912                 else if (td_random(td))
1913                         io_u->offset = zb->start +
1914                                 ((io_u->offset - orig_zb->start) %
1915                                  (range - io_u->buflen)) / min_bs * min_bs;
1916
1917                 /*
1918                  * When zbd_find_zone() returns a conventional zone,
1919                  * we can simply accept the new i/o offset here.
1920                  */
1921                 if (!zb->has_wp)
1922                         return io_u_accept;
1923
1924                 /*
1925                  * Make sure the I/O does not cross over the zone wp position.
1926                  */
1927                 new_len = min((unsigned long long)io_u->buflen,
1928                               (unsigned long long)(zb->wp - io_u->offset));
1929                 new_len = new_len / min_bs * min_bs;
1930                 if (new_len < io_u->buflen) {
1931                         io_u->buflen = new_len;
1932                         dprint(FD_IO, "Changed length from %u into %llu\n",
1933                                orig_len, io_u->buflen);
1934                 }
1935
1936                 assert(zb->start <= io_u->offset);
1937                 assert(io_u->offset + io_u->buflen <= zb->wp);
1938
1939                 goto accept;
1940
1941         case DDIR_WRITE:
1942                 if (io_u->buflen > zbdi->zone_size) {
1943                         td_verror(td, EINVAL, "I/O buflen exceeds zone size");
1944                         dprint(FD_IO,
1945                                "%s: I/O buflen %llu exceeds zone size %"PRIu64"\n",
1946                                f->file_name, io_u->buflen, zbdi->zone_size);
1947                         goto eof;
1948                 }
1949
1950                 if (!zbd_open_zone(td, f, zone_idx_b)) {
1951                         zone_unlock(zb);
1952                         zb = zbd_convert_to_open_zone(td, io_u);
1953                         if (!zb) {
1954                                 dprint(FD_IO, "%s: can't convert to open zone",
1955                                        f->file_name);
1956                                 goto eof;
1957                         }
1958                 }
1959
1960                 /* Check whether the zone reset threshold has been exceeded */
1961                 if (td->o.zrf.u.f) {
1962                         if (zbdi->wp_sectors_with_data >= f->io_size * td->o.zrt.u.f &&
1963                             zbd_dec_and_reset_write_cnt(td, f))
1964                                 zb->reset_zone = 1;
1965                 }
1966
1967                 /* Reset the zone pointer if necessary */
1968                 if (zb->reset_zone || zbd_zone_full(f, zb, min_bs)) {
1969                         assert(td->o.verify == VERIFY_NONE);
1970                         /*
1971                          * Since previous write requests may have been submitted
1972                          * asynchronously and since we will submit the zone
1973                          * reset synchronously, wait until previously submitted
1974                          * write requests have completed before issuing a
1975                          * zone reset.
1976                          */
1977                         io_u_quiesce(td);
1978                         zb->reset_zone = 0;
1979                         if (zbd_reset_zone(td, f, zb) < 0)
1980                                 goto eof;
1981
1982                         if (zb->capacity < min_bs) {
1983                                 td_verror(td, EINVAL, "ZCAP is less min_bs");
1984                                 log_err("zone capacity %"PRIu64" smaller than minimum block size %"PRIu64"\n",
1985                                         zb->capacity, min_bs);
1986                                 goto eof;
1987                         }
1988                 }
1989
1990                 /* Make writes occur at the write pointer */
1991                 assert(!zbd_zone_full(f, zb, min_bs));
1992                 io_u->offset = zb->wp;
1993                 if (!is_valid_offset(f, io_u->offset)) {
1994                         td_verror(td, EINVAL, "invalid WP value");
1995                         dprint(FD_ZBD, "%s: dropped request with offset %llu\n",
1996                                f->file_name, io_u->offset);
1997                         goto eof;
1998                 }
1999
2000                 /*
2001                  * Make sure that the buflen is a multiple of the minimal
2002                  * block size. Give up if shrinking would make the request too
2003                  * small.
2004                  */
2005                 new_len = min((unsigned long long)io_u->buflen,
2006                               zbd_zone_capacity_end(zb) - io_u->offset);
2007                 new_len = new_len / min_bs * min_bs;
2008                 if (new_len == io_u->buflen)
2009                         goto accept;
2010                 if (new_len >= min_bs) {
2011                         io_u->buflen = new_len;
2012                         dprint(FD_IO, "Changed length from %u into %llu\n",
2013                                orig_len, io_u->buflen);
2014                         goto accept;
2015                 }
2016
2017                 td_verror(td, EIO, "zone remainder too small");
2018                 log_err("zone remainder %lld smaller than min block size %"PRIu64"\n",
2019                         (zbd_zone_capacity_end(zb) - io_u->offset), min_bs);
2020
2021                 goto eof;
2022
2023         case DDIR_TRIM:
2024                 /* Check random trim targets a non-empty zone */
2025                 if (!td_random(td) || zb->wp > zb->start)
2026                         goto accept;
2027
2028                 /* Find out a non-empty zone to trim */
2029                 zone_unlock(zb);
2030                 zl = get_zone(f, f->max_zone);
2031                 zb = zbd_find_zone(td, io_u, 1, zb, zl);
2032                 if (zb) {
2033                         io_u->offset = zb->start;
2034                         dprint(FD_ZBD, "%s: found new zone(%lld) for trim\n",
2035                                f->file_name, io_u->offset);
2036                         goto accept;
2037                 }
2038
2039                 goto eof;
2040
2041         case DDIR_SYNC:
2042                 /* fall-through */
2043         case DDIR_DATASYNC:
2044         case DDIR_SYNC_FILE_RANGE:
2045         case DDIR_WAIT:
2046         case DDIR_LAST:
2047         case DDIR_INVAL:
2048                 goto accept;
2049         }
2050
2051         assert(false);
2052
2053 accept:
2054         assert(zb->has_wp);
2055         assert(zb->cond != ZBD_ZONE_COND_OFFLINE);
2056         assert(!io_u->zbd_queue_io);
2057         assert(!io_u->zbd_put_io);
2058
2059         io_u->zbd_queue_io = zbd_queue_io;
2060         io_u->zbd_put_io = zbd_put_io;
2061
2062         /*
2063          * Since we return with the zone lock still held,
2064          * add an annotation to let Coverity know that it
2065          * is intentional.
2066          */
2067         /* coverity[missing_unlock] */
2068
2069         return io_u_accept;
2070
2071 eof:
2072         if (zb && zb->has_wp)
2073                 zone_unlock(zb);
2074
2075         return io_u_eof;
2076 }
2077
2078 /* Return a string with ZBD statistics */
2079 char *zbd_write_status(const struct thread_stat *ts)
2080 {
2081         char *res;
2082
2083         if (asprintf(&res, "; %"PRIu64" zone resets", ts->nr_zone_resets) < 0)
2084                 return NULL;
2085         return res;
2086 }
2087
2088 /**
2089  * zbd_do_io_u_trim - If reset zone is applicable, do reset zone instead of trim
2090  *
2091  * @td: FIO thread data.
2092  * @io_u: FIO I/O unit.
2093  *
2094  * It is assumed that z->mutex is already locked.
2095  * Return io_u_completed when reset zone succeeds. Return 0 when the target zone
2096  * does not have write pointer. On error, return negative errno.
2097  */
2098 int zbd_do_io_u_trim(const struct thread_data *td, struct io_u *io_u)
2099 {
2100         struct fio_file *f = io_u->file;
2101         struct fio_zone_info *z;
2102         uint32_t zone_idx;
2103         int ret;
2104
2105         zone_idx = zbd_zone_idx(f, io_u->offset);
2106         z = get_zone(f, zone_idx);
2107
2108         if (!z->has_wp)
2109                 return 0;
2110
2111         if (io_u->offset != z->start) {
2112                 log_err("Trim offset not at zone start (%lld)\n",
2113                         io_u->offset);
2114                 return -EINVAL;
2115         }
2116
2117         ret = zbd_reset_zone((struct thread_data *)td, f, z);
2118         if (ret < 0)
2119                 return ret;
2120
2121         return io_u_completed;
2122 }