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