zbd: Fix zone report handling
[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 <dirent.h>
11 #include <fcntl.h>
12 #include <sys/ioctl.h>
13 #include <sys/stat.h>
14 #include <unistd.h>
15 #include <linux/blkzoned.h>
16 #include "file.h"
17 #include "fio.h"
18 #include "lib/pow2.h"
19 #include "log.h"
20 #include "smalloc.h"
21 #include "verify.h"
22 #include "zbd.h"
23
24 /**
25  * zbd_zone_idx - convert an offset into a zone number
26  * @f: file pointer.
27  * @offset: offset in bytes. If this offset is in the first zone_size bytes
28  *          past the disk size then the index of the sentinel is returned.
29  */
30 static uint32_t zbd_zone_idx(const struct fio_file *f, uint64_t offset)
31 {
32         uint32_t zone_idx;
33
34         if (f->zbd_info->zone_size_log2 > 0)
35                 zone_idx = offset >> f->zbd_info->zone_size_log2;
36         else
37                 zone_idx = offset / f->zbd_info->zone_size;
38
39         return min(zone_idx, f->zbd_info->nr_zones);
40 }
41
42 /**
43  * zbd_zone_full - verify whether a minimum number of bytes remain in a zone
44  * @f: file pointer.
45  * @z: zone info pointer.
46  * @required: minimum number of bytes that must remain in a zone.
47  *
48  * The caller must hold z->mutex.
49  */
50 static bool zbd_zone_full(const struct fio_file *f, struct fio_zone_info *z,
51                           uint64_t required)
52 {
53         assert((required & 511) == 0);
54
55         return z->type == BLK_ZONE_TYPE_SEQWRITE_REQ &&
56                 z->wp + required > z->start + f->zbd_info->zone_size;
57 }
58
59 static bool is_valid_offset(const struct fio_file *f, uint64_t offset)
60 {
61         return (uint64_t)(offset - f->file_offset) < f->io_size;
62 }
63
64 /* Verify whether direct I/O is used for all host-managed zoned drives. */
65 static bool zbd_using_direct_io(void)
66 {
67         struct thread_data *td;
68         struct fio_file *f;
69         int i, j;
70
71         for_each_td(td, i) {
72                 if (td->o.odirect || !(td->o.td_ddir & TD_DDIR_WRITE))
73                         continue;
74                 for_each_file(td, f, j) {
75                         if (f->zbd_info &&
76                             f->zbd_info->model == ZBD_DM_HOST_MANAGED)
77                                 return false;
78                 }
79         }
80
81         return true;
82 }
83
84 /* Whether or not the I/O range for f includes one or more sequential zones */
85 static bool zbd_is_seq_job(struct fio_file *f)
86 {
87         uint32_t zone_idx, zone_idx_b, zone_idx_e;
88
89         assert(f->zbd_info);
90         if (f->io_size == 0)
91                 return false;
92         zone_idx_b = zbd_zone_idx(f, f->file_offset);
93         zone_idx_e = zbd_zone_idx(f, f->file_offset + f->io_size - 1);
94         for (zone_idx = zone_idx_b; zone_idx <= zone_idx_e; zone_idx++)
95                 if (f->zbd_info->zone_info[zone_idx].type ==
96                     BLK_ZONE_TYPE_SEQWRITE_REQ)
97                         return true;
98
99         return false;
100 }
101
102 /*
103  * Verify whether offset and size parameters are aligned with zone boundaries.
104  */
105 static bool zbd_verify_sizes(void)
106 {
107         const struct fio_zone_info *z;
108         struct thread_data *td;
109         struct fio_file *f;
110         uint64_t new_offset, new_end;
111         uint32_t zone_idx;
112         int i, j;
113
114         for_each_td(td, i) {
115                 for_each_file(td, f, j) {
116                         if (!f->zbd_info)
117                                 continue;
118                         if (f->file_offset >= f->real_file_size)
119                                 continue;
120                         if (!zbd_is_seq_job(f))
121                                 continue;
122                         zone_idx = zbd_zone_idx(f, f->file_offset);
123                         z = &f->zbd_info->zone_info[zone_idx];
124                         if (f->file_offset != z->start) {
125                                 new_offset = (z+1)->start;
126                                 if (new_offset >= f->file_offset + f->io_size) {
127                                         log_info("%s: io_size must be at least one zone\n",
128                                                  f->file_name);
129                                         return false;
130                                 }
131                                 log_info("%s: rounded up offset from %llu to %llu\n",
132                                          f->file_name, (unsigned long long) f->file_offset,
133                                          (unsigned long long) new_offset);
134                                 f->io_size -= (new_offset - f->file_offset);
135                                 f->file_offset = new_offset;
136                         }
137                         zone_idx = zbd_zone_idx(f, f->file_offset + f->io_size);
138                         z = &f->zbd_info->zone_info[zone_idx];
139                         new_end = z->start;
140                         if (f->file_offset + f->io_size != new_end) {
141                                 if (new_end <= f->file_offset) {
142                                         log_info("%s: io_size must be at least one zone\n",
143                                                  f->file_name);
144                                         return false;
145                                 }
146                                 log_info("%s: rounded down io_size from %llu to %llu\n",
147                                          f->file_name, (unsigned long long) f->io_size,
148                                          (unsigned long long) new_end - f->file_offset);
149                                 f->io_size = new_end - f->file_offset;
150                         }
151                 }
152         }
153
154         return true;
155 }
156
157 static bool zbd_verify_bs(void)
158 {
159         struct thread_data *td;
160         struct fio_file *f;
161         uint32_t zone_size;
162         int i, j, k;
163
164         for_each_td(td, i) {
165                 for_each_file(td, f, j) {
166                         if (!f->zbd_info)
167                                 continue;
168                         zone_size = f->zbd_info->zone_size;
169                         for (k = 0; k < ARRAY_SIZE(td->o.bs); k++) {
170                                 if (td->o.verify != VERIFY_NONE &&
171                                     zone_size % td->o.bs[k] != 0) {
172                                         log_info("%s: block size %llu is not a divisor of the zone size %d\n",
173                                                  f->file_name, td->o.bs[k],
174                                                  zone_size);
175                                         return false;
176                                 }
177                         }
178                 }
179         }
180         return true;
181 }
182
183 /*
184  * Read zone information into @buf starting from sector @start_sector.
185  * @fd is a file descriptor that refers to a block device and @bufsz is the
186  * size of @buf.
187  *
188  * Returns 0 upon success and a negative error code upon failure.
189  * If the zone report is empty, always assume an error (device problem) and
190  * return -EIO.
191  */
192 static int read_zone_info(int fd, uint64_t start_sector,
193                           void *buf, unsigned int bufsz)
194 {
195         struct blk_zone_report *hdr = buf;
196         int ret;
197
198         if (bufsz < sizeof(*hdr))
199                 return -EINVAL;
200
201         memset(hdr, 0, sizeof(*hdr));
202
203         hdr->nr_zones = (bufsz - sizeof(*hdr)) / sizeof(struct blk_zone);
204         hdr->sector = start_sector;
205         ret = ioctl(fd, BLKREPORTZONE, hdr);
206         if (ret)
207                 return -errno;
208         if (!hdr->nr_zones)
209                 return -EIO;
210         return 0;
211 }
212
213 /*
214  * Read up to 255 characters from the first line of a file. Strip the trailing
215  * newline.
216  */
217 static char *read_file(const char *path)
218 {
219         char line[256], *p = line;
220         FILE *f;
221
222         f = fopen(path, "rb");
223         if (!f)
224                 return NULL;
225         if (!fgets(line, sizeof(line), f))
226                 line[0] = '\0';
227         strsep(&p, "\n");
228         fclose(f);
229
230         return strdup(line);
231 }
232
233 static enum blk_zoned_model get_zbd_model(const char *file_name)
234 {
235         enum blk_zoned_model model = ZBD_DM_NONE;
236         char *zoned_attr_path = NULL;
237         char *model_str = NULL;
238         struct stat statbuf;
239         char *sys_devno_path = NULL;
240         char *part_attr_path = NULL;
241         char *part_str = NULL;
242         char sys_path[PATH_MAX];
243         ssize_t sz;
244         char *delim = NULL;
245
246         if (stat(file_name, &statbuf) < 0)
247                 goto out;
248
249         if (asprintf(&sys_devno_path, "/sys/dev/block/%d:%d",
250                      major(statbuf.st_rdev), minor(statbuf.st_rdev)) < 0)
251                 goto out;
252
253         sz = readlink(sys_devno_path, sys_path, sizeof(sys_path) - 1);
254         if (sz < 0)
255                 goto out;
256         sys_path[sz] = '\0';
257
258         /*
259          * If the device is a partition device, cut the device name in the
260          * canonical sysfs path to obtain the sysfs path of the holder device.
261          *   e.g.:  /sys/devices/.../sda/sda1 -> /sys/devices/.../sda
262          */
263         if (asprintf(&part_attr_path, "/sys/dev/block/%s/partition",
264                      sys_path) < 0)
265                 goto out;
266         part_str = read_file(part_attr_path);
267         if (part_str && *part_str == '1') {
268                 delim = strrchr(sys_path, '/');
269                 if (!delim)
270                         goto out;
271                 *delim = '\0';
272         }
273
274         if (asprintf(&zoned_attr_path,
275                      "/sys/dev/block/%s/queue/zoned", sys_path) < 0)
276                 goto out;
277
278         model_str = read_file(zoned_attr_path);
279         if (!model_str)
280                 goto out;
281         dprint(FD_ZBD, "%s: zbd model string: %s\n", file_name, model_str);
282         if (strcmp(model_str, "host-aware") == 0)
283                 model = ZBD_DM_HOST_AWARE;
284         else if (strcmp(model_str, "host-managed") == 0)
285                 model = ZBD_DM_HOST_MANAGED;
286
287 out:
288         free(model_str);
289         free(zoned_attr_path);
290         free(part_str);
291         free(part_attr_path);
292         free(sys_devno_path);
293         return model;
294 }
295
296 static int ilog2(uint64_t i)
297 {
298         int log = -1;
299
300         while (i) {
301                 i >>= 1;
302                 log++;
303         }
304         return log;
305 }
306
307 /*
308  * Initialize f->zbd_info for devices that are not zoned block devices. This
309  * allows to execute a ZBD workload against a non-ZBD device.
310  */
311 static int init_zone_info(struct thread_data *td, struct fio_file *f)
312 {
313         uint32_t nr_zones;
314         struct fio_zone_info *p;
315         uint64_t zone_size;
316         struct zoned_block_device_info *zbd_info = NULL;
317         pthread_mutexattr_t attr;
318         int i;
319
320         zone_size = td->o.zone_size;
321         assert(zone_size);
322         nr_zones = (f->real_file_size + zone_size - 1) / zone_size;
323         zbd_info = scalloc(1, sizeof(*zbd_info) +
324                            (nr_zones + 1) * sizeof(zbd_info->zone_info[0]));
325         if (!zbd_info)
326                 return -ENOMEM;
327
328         pthread_mutexattr_init(&attr);
329         pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
330         pthread_mutexattr_setpshared(&attr, true);
331         pthread_mutex_init(&zbd_info->mutex, &attr);
332         zbd_info->refcount = 1;
333         p = &zbd_info->zone_info[0];
334         for (i = 0; i < nr_zones; i++, p++) {
335                 pthread_mutex_init(&p->mutex, &attr);
336                 p->start = i * zone_size;
337                 p->wp = p->start + zone_size;
338                 p->type = BLK_ZONE_TYPE_SEQWRITE_REQ;
339                 p->cond = BLK_ZONE_COND_EMPTY;
340         }
341         /* a sentinel */
342         p->start = nr_zones * zone_size;
343
344         f->zbd_info = zbd_info;
345         f->zbd_info->zone_size = zone_size;
346         f->zbd_info->zone_size_log2 = is_power_of_2(zone_size) ?
347                 ilog2(zone_size) : -1;
348         f->zbd_info->nr_zones = nr_zones;
349         pthread_mutexattr_destroy(&attr);
350         return 0;
351 }
352
353 /*
354  * Parse the BLKREPORTZONE output and store it in f->zbd_info. Must be called
355  * only for devices that support this ioctl, namely zoned block devices.
356  */
357 static int parse_zone_info(struct thread_data *td, struct fio_file *f)
358 {
359         const unsigned int bufsz = sizeof(struct blk_zone_report) +
360                 4096 * sizeof(struct blk_zone);
361         uint32_t nr_zones;
362         struct blk_zone_report *hdr;
363         const struct blk_zone *z;
364         struct fio_zone_info *p;
365         uint64_t zone_size, start_sector;
366         struct zoned_block_device_info *zbd_info = NULL;
367         pthread_mutexattr_t attr;
368         void *buf;
369         int fd, i, j, ret = 0;
370
371         pthread_mutexattr_init(&attr);
372         pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
373         pthread_mutexattr_setpshared(&attr, true);
374
375         buf = malloc(bufsz);
376         if (!buf)
377                 goto out;
378
379         fd = open(f->file_name, O_RDONLY | O_LARGEFILE);
380         if (fd < 0) {
381                 ret = -errno;
382                 goto free;
383         }
384
385         ret = read_zone_info(fd, 0, buf, bufsz);
386         if (ret < 0) {
387                 log_info("fio: BLKREPORTZONE(%lu) failed for %s (%d).\n",
388                          0UL, f->file_name, -ret);
389                 goto close;
390         }
391         hdr = buf;
392         if (hdr->nr_zones < 1) {
393                 log_info("fio: %s has invalid zone information.\n",
394                          f->file_name);
395                 goto close;
396         }
397         z = (void *)(hdr + 1);
398         zone_size = z->len << 9;
399         nr_zones = (f->real_file_size + zone_size - 1) / zone_size;
400
401         if (td->o.zone_size == 0) {
402                 td->o.zone_size = zone_size;
403         } else if (td->o.zone_size != zone_size) {
404                 log_info("fio: %s job parameter zonesize %llu does not match disk zone size %llu.\n",
405                          f->file_name, (unsigned long long) td->o.zone_size,
406                         (unsigned long long) zone_size);
407                 ret = -EINVAL;
408                 goto close;
409         }
410
411         dprint(FD_ZBD, "Device %s has %d zones of size %llu KB\n", f->file_name,
412                nr_zones, (unsigned long long) zone_size / 1024);
413
414         zbd_info = scalloc(1, sizeof(*zbd_info) +
415                            (nr_zones + 1) * sizeof(zbd_info->zone_info[0]));
416         ret = -ENOMEM;
417         if (!zbd_info)
418                 goto close;
419         pthread_mutex_init(&zbd_info->mutex, &attr);
420         zbd_info->refcount = 1;
421         p = &zbd_info->zone_info[0];
422         for (start_sector = 0, j = 0; j < nr_zones;) {
423                 z = (void *)(hdr + 1);
424                 for (i = 0; i < hdr->nr_zones; i++, j++, z++, p++) {
425                         pthread_mutex_init(&p->mutex, &attr);
426                         p->start = z->start << 9;
427                         switch (z->cond) {
428                         case BLK_ZONE_COND_NOT_WP:
429                                 p->wp = p->start;
430                                 break;
431                         case BLK_ZONE_COND_FULL:
432                                 p->wp = p->start + zone_size;
433                                 break;
434                         default:
435                                 assert(z->start <= z->wp);
436                                 assert(z->wp <= z->start + (zone_size >> 9));
437                                 p->wp = z->wp << 9;
438                                 break;
439                         }
440                         p->type = z->type;
441                         p->cond = z->cond;
442                         if (j > 0 && p->start != p[-1].start + zone_size) {
443                                 log_info("%s: invalid zone data\n",
444                                          f->file_name);
445                                 ret = -EINVAL;
446                                 goto close;
447                         }
448                 }
449                 z--;
450                 start_sector = z->start + z->len;
451                 if (j >= nr_zones)
452                         break;
453                 ret = read_zone_info(fd, start_sector, buf, bufsz);
454                 if (ret < 0) {
455                         log_info("fio: BLKREPORTZONE(%llu) failed for %s (%d).\n",
456                                  (unsigned long long) start_sector, f->file_name, -ret);
457                         goto close;
458                 }
459         }
460         /* a sentinel */
461         zbd_info->zone_info[nr_zones].start = start_sector << 9;
462
463         f->zbd_info = zbd_info;
464         f->zbd_info->zone_size = zone_size;
465         f->zbd_info->zone_size_log2 = is_power_of_2(zone_size) ?
466                 ilog2(zone_size) : -1;
467         f->zbd_info->nr_zones = nr_zones;
468         zbd_info = NULL;
469         ret = 0;
470
471 close:
472         sfree(zbd_info);
473         close(fd);
474 free:
475         free(buf);
476 out:
477         pthread_mutexattr_destroy(&attr);
478         return ret;
479 }
480
481 /*
482  * Allocate zone information and store it into f->zbd_info if zonemode=zbd.
483  *
484  * Returns 0 upon success and a negative error code upon failure.
485  */
486 int zbd_create_zone_info(struct thread_data *td, struct fio_file *f)
487 {
488         enum blk_zoned_model zbd_model;
489         int ret = 0;
490
491         assert(td->o.zone_mode == ZONE_MODE_ZBD);
492
493         zbd_model = get_zbd_model(f->file_name);
494         switch (zbd_model) {
495         case ZBD_DM_HOST_AWARE:
496         case ZBD_DM_HOST_MANAGED:
497                 ret = parse_zone_info(td, f);
498                 break;
499         case ZBD_DM_NONE:
500                 ret = init_zone_info(td, f);
501                 break;
502         }
503         if (ret == 0)
504                 f->zbd_info->model = zbd_model;
505         return ret;
506 }
507
508 void zbd_free_zone_info(struct fio_file *f)
509 {
510         uint32_t refcount;
511
512         if (!f->zbd_info)
513                 return;
514
515         pthread_mutex_lock(&f->zbd_info->mutex);
516         refcount = --f->zbd_info->refcount;
517         pthread_mutex_unlock(&f->zbd_info->mutex);
518
519         assert((int32_t)refcount >= 0);
520         if (refcount == 0)
521                 sfree(f->zbd_info);
522         f->zbd_info = NULL;
523 }
524
525 /*
526  * Initialize f->zbd_info.
527  *
528  * Returns 0 upon success and a negative error code upon failure.
529  *
530  * Note: this function can only work correctly if it is called before the first
531  * fio fork() call.
532  */
533 static int zbd_init_zone_info(struct thread_data *td, struct fio_file *file)
534 {
535         struct thread_data *td2;
536         struct fio_file *f2;
537         int i, j, ret;
538
539         for_each_td(td2, i) {
540                 for_each_file(td2, f2, j) {
541                         if (td2 == td && f2 == file)
542                                 continue;
543                         if (!f2->zbd_info ||
544                             strcmp(f2->file_name, file->file_name) != 0)
545                                 continue;
546                         file->zbd_info = f2->zbd_info;
547                         file->zbd_info->refcount++;
548                         return 0;
549                 }
550         }
551
552         ret = zbd_create_zone_info(td, file);
553         if (ret < 0)
554                 td_verror(td, -ret, "BLKREPORTZONE failed");
555         return ret;
556 }
557
558 int zbd_init(struct thread_data *td)
559 {
560         struct fio_file *f;
561         int i;
562
563         for_each_file(td, f, i) {
564                 if (f->filetype != FIO_TYPE_BLOCK)
565                         continue;
566                 if (td->o.zone_size && td->o.zone_size < 512) {
567                         log_err("%s: zone size must be at least 512 bytes for --zonemode=zbd\n\n",
568                                 f->file_name);
569                         return 1;
570                 }
571                 if (td->o.zone_size == 0 &&
572                     get_zbd_model(f->file_name) == ZBD_DM_NONE) {
573                         log_err("%s: Specifying the zone size is mandatory for regular block devices with --zonemode=zbd\n\n",
574                                 f->file_name);
575                         return 1;
576                 }
577                 zbd_init_zone_info(td, f);
578         }
579
580         if (!zbd_using_direct_io()) {
581                 log_err("Using direct I/O is mandatory for writing to ZBD drives\n\n");
582                 return 1;
583         }
584
585         if (!zbd_verify_sizes())
586                 return 1;
587
588         if (!zbd_verify_bs())
589                 return 1;
590
591         return 0;
592 }
593
594 /**
595  * zbd_reset_range - reset zones for a range of sectors
596  * @td: FIO thread data.
597  * @f: Fio file for which to reset zones
598  * @sector: Starting sector in units of 512 bytes
599  * @nr_sectors: Number of sectors in units of 512 bytes
600  *
601  * Returns 0 upon success and a negative error code upon failure.
602  */
603 static int zbd_reset_range(struct thread_data *td, const struct fio_file *f,
604                            uint64_t offset, uint64_t length)
605 {
606         struct blk_zone_range zr = {
607                 .sector         = offset >> 9,
608                 .nr_sectors     = length >> 9,
609         };
610         uint32_t zone_idx_b, zone_idx_e;
611         struct fio_zone_info *zb, *ze, *z;
612         int ret = 0;
613
614         assert(f->fd != -1);
615         assert(is_valid_offset(f, offset + length - 1));
616         switch (f->zbd_info->model) {
617         case ZBD_DM_HOST_AWARE:
618         case ZBD_DM_HOST_MANAGED:
619                 ret = ioctl(f->fd, BLKRESETZONE, &zr);
620                 if (ret < 0) {
621                         td_verror(td, errno, "resetting wp failed");
622                         log_err("%s: resetting wp for %llu sectors at sector %llu failed (%d).\n",
623                                 f->file_name, zr.nr_sectors, zr.sector, errno);
624                         return ret;
625                 }
626                 break;
627         case ZBD_DM_NONE:
628                 break;
629         }
630
631         zone_idx_b = zbd_zone_idx(f, offset);
632         zb = &f->zbd_info->zone_info[zone_idx_b];
633         zone_idx_e = zbd_zone_idx(f, offset + length);
634         ze = &f->zbd_info->zone_info[zone_idx_e];
635         for (z = zb; z < ze; z++) {
636                 pthread_mutex_lock(&z->mutex);
637                 pthread_mutex_lock(&f->zbd_info->mutex);
638                 f->zbd_info->sectors_with_data -= z->wp - z->start;
639                 pthread_mutex_unlock(&f->zbd_info->mutex);
640                 z->wp = z->start;
641                 z->verify_block = 0;
642                 pthread_mutex_unlock(&z->mutex);
643         }
644
645         td->ts.nr_zone_resets += ze - zb;
646
647         return ret;
648 }
649
650 static unsigned int zbd_zone_nr(struct zoned_block_device_info *zbd_info,
651                                 struct fio_zone_info *zone)
652 {
653         return zone - zbd_info->zone_info;
654 }
655
656 /**
657  * zbd_reset_zone - reset the write pointer of a single zone
658  * @td: FIO thread data.
659  * @f: FIO file associated with the disk for which to reset a write pointer.
660  * @z: Zone to reset.
661  *
662  * Returns 0 upon success and a negative error code upon failure.
663  */
664 static int zbd_reset_zone(struct thread_data *td, const struct fio_file *f,
665                           struct fio_zone_info *z)
666 {
667         dprint(FD_ZBD, "%s: resetting wp of zone %u.\n", f->file_name,
668                 zbd_zone_nr(f->zbd_info, z));
669
670         return zbd_reset_range(td, f, z->start, (z+1)->start - z->start);
671 }
672
673 /*
674  * Reset a range of zones. Returns 0 upon success and 1 upon failure.
675  * @td: fio thread data.
676  * @f: fio file for which to reset zones
677  * @zb: first zone to reset.
678  * @ze: first zone not to reset.
679  * @all_zones: whether to reset all zones or only those zones for which the
680  *      write pointer is not a multiple of td->o.min_bs[DDIR_WRITE].
681  */
682 static int zbd_reset_zones(struct thread_data *td, struct fio_file *f,
683                            struct fio_zone_info *const zb,
684                            struct fio_zone_info *const ze, bool all_zones)
685 {
686         struct fio_zone_info *z, *start_z = ze;
687         const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
688         bool reset_wp;
689         int res = 0;
690
691         dprint(FD_ZBD, "%s: examining zones %u .. %u\n", f->file_name,
692                 zbd_zone_nr(f->zbd_info, zb), zbd_zone_nr(f->zbd_info, ze));
693         assert(f->fd != -1);
694         for (z = zb; z < ze; z++) {
695                 pthread_mutex_lock(&z->mutex);
696                 switch (z->type) {
697                 case BLK_ZONE_TYPE_SEQWRITE_REQ:
698                         reset_wp = all_zones ? z->wp != z->start :
699                                         (td->o.td_ddir & TD_DDIR_WRITE) &&
700                                         z->wp % min_bs != 0;
701                         if (start_z == ze && reset_wp) {
702                                 start_z = z;
703                         } else if (start_z < ze && !reset_wp) {
704                                 dprint(FD_ZBD,
705                                        "%s: resetting zones %u .. %u\n",
706                                        f->file_name,
707                                         zbd_zone_nr(f->zbd_info, start_z),
708                                         zbd_zone_nr(f->zbd_info, z));
709                                 if (zbd_reset_range(td, f, start_z->start,
710                                                 z->start - start_z->start) < 0)
711                                         res = 1;
712                                 start_z = ze;
713                         }
714                         break;
715                 default:
716                         if (start_z == ze)
717                                 break;
718                         dprint(FD_ZBD, "%s: resetting zones %u .. %u\n",
719                                f->file_name, zbd_zone_nr(f->zbd_info, start_z),
720                                zbd_zone_nr(f->zbd_info, z));
721                         if (zbd_reset_range(td, f, start_z->start,
722                                             z->start - start_z->start) < 0)
723                                 res = 1;
724                         start_z = ze;
725                         break;
726                 }
727         }
728         if (start_z < ze) {
729                 dprint(FD_ZBD, "%s: resetting zones %u .. %u\n", f->file_name,
730                         zbd_zone_nr(f->zbd_info, start_z),
731                         zbd_zone_nr(f->zbd_info, z));
732                 if (zbd_reset_range(td, f, start_z->start,
733                                     z->start - start_z->start) < 0)
734                         res = 1;
735         }
736         for (z = zb; z < ze; z++)
737                 pthread_mutex_unlock(&z->mutex);
738
739         return res;
740 }
741
742 /*
743  * Reset zbd_info.write_cnt, the counter that counts down towards the next
744  * zone reset.
745  */
746 static void zbd_reset_write_cnt(const struct thread_data *td,
747                                 const struct fio_file *f)
748 {
749         assert(0 <= td->o.zrf.u.f && td->o.zrf.u.f <= 1);
750
751         pthread_mutex_lock(&f->zbd_info->mutex);
752         f->zbd_info->write_cnt = td->o.zrf.u.f ?
753                 min(1.0 / td->o.zrf.u.f, 0.0 + UINT_MAX) : UINT_MAX;
754         pthread_mutex_unlock(&f->zbd_info->mutex);
755 }
756
757 static bool zbd_dec_and_reset_write_cnt(const struct thread_data *td,
758                                         const struct fio_file *f)
759 {
760         uint32_t write_cnt = 0;
761
762         pthread_mutex_lock(&f->zbd_info->mutex);
763         assert(f->zbd_info->write_cnt);
764         if (f->zbd_info->write_cnt)
765                 write_cnt = --f->zbd_info->write_cnt;
766         if (write_cnt == 0)
767                 zbd_reset_write_cnt(td, f);
768         pthread_mutex_unlock(&f->zbd_info->mutex);
769
770         return write_cnt == 0;
771 }
772
773 enum swd_action {
774         CHECK_SWD,
775         SET_SWD,
776 };
777
778 /* Calculate the number of sectors with data (swd) and perform action 'a' */
779 static uint64_t zbd_process_swd(const struct fio_file *f, enum swd_action a)
780 {
781         struct fio_zone_info *zb, *ze, *z;
782         uint64_t swd = 0;
783
784         zb = &f->zbd_info->zone_info[zbd_zone_idx(f, f->file_offset)];
785         ze = &f->zbd_info->zone_info[zbd_zone_idx(f, f->file_offset +
786                                                   f->io_size)];
787         for (z = zb; z < ze; z++) {
788                 pthread_mutex_lock(&z->mutex);
789                 swd += z->wp - z->start;
790         }
791         pthread_mutex_lock(&f->zbd_info->mutex);
792         switch (a) {
793         case CHECK_SWD:
794                 assert(f->zbd_info->sectors_with_data == swd);
795                 break;
796         case SET_SWD:
797                 f->zbd_info->sectors_with_data = swd;
798                 break;
799         }
800         pthread_mutex_unlock(&f->zbd_info->mutex);
801         for (z = zb; z < ze; z++)
802                 pthread_mutex_unlock(&z->mutex);
803
804         return swd;
805 }
806
807 /*
808  * The swd check is useful for debugging but takes too much time to leave
809  * it enabled all the time. Hence it is disabled by default.
810  */
811 static const bool enable_check_swd = false;
812
813 /* Check whether the value of zbd_info.sectors_with_data is correct. */
814 static void zbd_check_swd(const struct fio_file *f)
815 {
816         if (!enable_check_swd)
817                 return;
818
819         zbd_process_swd(f, CHECK_SWD);
820 }
821
822 static void zbd_init_swd(struct fio_file *f)
823 {
824         uint64_t swd;
825
826         swd = zbd_process_swd(f, SET_SWD);
827         dprint(FD_ZBD, "%s(%s): swd = %" PRIu64 "\n", __func__, f->file_name,
828                swd);
829 }
830
831 void zbd_file_reset(struct thread_data *td, struct fio_file *f)
832 {
833         struct fio_zone_info *zb, *ze;
834         uint32_t zone_idx_e;
835
836         if (!f->zbd_info)
837                 return;
838
839         zb = &f->zbd_info->zone_info[zbd_zone_idx(f, f->file_offset)];
840         zone_idx_e = zbd_zone_idx(f, f->file_offset + f->io_size);
841         ze = &f->zbd_info->zone_info[zone_idx_e];
842         zbd_init_swd(f);
843         /*
844          * If data verification is enabled reset the affected zones before
845          * writing any data to avoid that a zone reset has to be issued while
846          * writing data, which causes data loss.
847          */
848         zbd_reset_zones(td, f, zb, ze, td->o.verify != VERIFY_NONE &&
849                         (td->o.td_ddir & TD_DDIR_WRITE) &&
850                         td->runstate != TD_VERIFYING);
851         zbd_reset_write_cnt(td, f);
852 }
853
854 /* The caller must hold f->zbd_info->mutex. */
855 static bool is_zone_open(const struct thread_data *td, const struct fio_file *f,
856                          unsigned int zone_idx)
857 {
858         struct zoned_block_device_info *zbdi = f->zbd_info;
859         int i;
860
861         assert(td->o.max_open_zones <= ARRAY_SIZE(zbdi->open_zones));
862         assert(zbdi->num_open_zones <= td->o.max_open_zones);
863
864         for (i = 0; i < zbdi->num_open_zones; i++)
865                 if (zbdi->open_zones[i] == zone_idx)
866                         return true;
867
868         return false;
869 }
870
871 /*
872  * Open a ZBD zone if it was not yet open. Returns true if either the zone was
873  * already open or if opening a new zone is allowed. Returns false if the zone
874  * was not yet open and opening a new zone would cause the zone limit to be
875  * exceeded.
876  */
877 static bool zbd_open_zone(struct thread_data *td, const struct io_u *io_u,
878                           uint32_t zone_idx)
879 {
880         const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
881         const struct fio_file *f = io_u->file;
882         struct fio_zone_info *z = &f->zbd_info->zone_info[zone_idx];
883         bool res = true;
884
885         if (z->cond == BLK_ZONE_COND_OFFLINE)
886                 return false;
887
888         /*
889          * Skip full zones with data verification enabled because resetting a
890          * zone causes data loss and hence causes verification to fail.
891          */
892         if (td->o.verify != VERIFY_NONE && zbd_zone_full(f, z, min_bs))
893                 return false;
894
895         /* Zero means no limit */
896         if (!td->o.max_open_zones)
897                 return true;
898
899         pthread_mutex_lock(&f->zbd_info->mutex);
900         if (is_zone_open(td, f, zone_idx))
901                 goto out;
902         res = false;
903         if (f->zbd_info->num_open_zones >= td->o.max_open_zones)
904                 goto out;
905         dprint(FD_ZBD, "%s: opening zone %d\n", f->file_name, zone_idx);
906         f->zbd_info->open_zones[f->zbd_info->num_open_zones++] = zone_idx;
907         z->open = 1;
908         res = true;
909
910 out:
911         pthread_mutex_unlock(&f->zbd_info->mutex);
912         return res;
913 }
914
915 /* The caller must hold f->zbd_info->mutex */
916 static void zbd_close_zone(struct thread_data *td, const struct fio_file *f,
917                            unsigned int open_zone_idx)
918 {
919         uint32_t zone_idx;
920
921         assert(open_zone_idx < f->zbd_info->num_open_zones);
922         zone_idx = f->zbd_info->open_zones[open_zone_idx];
923         memmove(f->zbd_info->open_zones + open_zone_idx,
924                 f->zbd_info->open_zones + open_zone_idx + 1,
925                 (FIO_MAX_OPEN_ZBD_ZONES - (open_zone_idx + 1)) *
926                 sizeof(f->zbd_info->open_zones[0]));
927         f->zbd_info->num_open_zones--;
928         f->zbd_info->zone_info[zone_idx].open = 0;
929 }
930
931 /*
932  * Modify the offset of an I/O unit that does not refer to an open zone such
933  * that it refers to an open zone. Close an open zone and open a new zone if
934  * necessary. This algorithm can only work correctly if all write pointers are
935  * a multiple of the fio block size. The caller must neither hold z->mutex
936  * nor f->zbd_info->mutex. Returns with z->mutex held upon success.
937  */
938 struct fio_zone_info *zbd_convert_to_open_zone(struct thread_data *td,
939                                                struct io_u *io_u)
940 {
941         const uint32_t min_bs = td->o.min_bs[io_u->ddir];
942         const struct fio_file *f = io_u->file;
943         struct fio_zone_info *z;
944         unsigned int open_zone_idx = -1;
945         uint32_t zone_idx, new_zone_idx;
946         int i;
947
948         assert(is_valid_offset(f, io_u->offset));
949
950         if (td->o.max_open_zones) {
951                 /*
952                  * This statement accesses f->zbd_info->open_zones[] on purpose
953                  * without locking.
954                  */
955                 zone_idx = f->zbd_info->open_zones[(io_u->offset -
956                                                     f->file_offset) *
957                                 f->zbd_info->num_open_zones / f->io_size];
958         } else {
959                 zone_idx = zbd_zone_idx(f, io_u->offset);
960         }
961         dprint(FD_ZBD, "%s(%s): starting from zone %d (offset %lld, buflen %lld)\n",
962                __func__, f->file_name, zone_idx, io_u->offset, io_u->buflen);
963
964         /*
965          * Since z->mutex is the outer lock and f->zbd_info->mutex the inner
966          * lock it can happen that the state of the zone with index zone_idx
967          * has changed after 'z' has been assigned and before f->zbd_info->mutex
968          * has been obtained. Hence the loop.
969          */
970         for (;;) {
971                 z = &f->zbd_info->zone_info[zone_idx];
972
973                 pthread_mutex_lock(&z->mutex);
974                 pthread_mutex_lock(&f->zbd_info->mutex);
975                 if (td->o.max_open_zones == 0)
976                         goto examine_zone;
977                 if (f->zbd_info->num_open_zones == 0) {
978                         pthread_mutex_unlock(&f->zbd_info->mutex);
979                         pthread_mutex_unlock(&z->mutex);
980                         dprint(FD_ZBD, "%s(%s): no zones are open\n",
981                                __func__, f->file_name);
982                         return NULL;
983                 }
984                 open_zone_idx = (io_u->offset - f->file_offset) *
985                         f->zbd_info->num_open_zones / f->io_size;
986                 assert(open_zone_idx < f->zbd_info->num_open_zones);
987                 new_zone_idx = f->zbd_info->open_zones[open_zone_idx];
988                 if (new_zone_idx == zone_idx)
989                         break;
990                 zone_idx = new_zone_idx;
991                 pthread_mutex_unlock(&f->zbd_info->mutex);
992                 pthread_mutex_unlock(&z->mutex);
993         }
994
995         /* Both z->mutex and f->zbd_info->mutex are held. */
996
997 examine_zone:
998         if (z->wp + min_bs <= (z+1)->start) {
999                 pthread_mutex_unlock(&f->zbd_info->mutex);
1000                 goto out;
1001         }
1002         dprint(FD_ZBD, "%s(%s): closing zone %d\n", __func__, f->file_name,
1003                zone_idx);
1004         if (td->o.max_open_zones)
1005                 zbd_close_zone(td, f, open_zone_idx);
1006         pthread_mutex_unlock(&f->zbd_info->mutex);
1007
1008         /* Only z->mutex is held. */
1009
1010         /* Zone 'z' is full, so try to open a new zone. */
1011         for (i = f->io_size / f->zbd_info->zone_size; i > 0; i--) {
1012                 zone_idx++;
1013                 pthread_mutex_unlock(&z->mutex);
1014                 z++;
1015                 if (!is_valid_offset(f, z->start)) {
1016                         /* Wrap-around. */
1017                         zone_idx = zbd_zone_idx(f, f->file_offset);
1018                         z = &f->zbd_info->zone_info[zone_idx];
1019                 }
1020                 assert(is_valid_offset(f, z->start));
1021                 pthread_mutex_lock(&z->mutex);
1022                 if (z->open)
1023                         continue;
1024                 if (zbd_open_zone(td, io_u, zone_idx))
1025                         goto out;
1026         }
1027
1028         /* Only z->mutex is held. */
1029
1030         /* Check whether the write fits in any of the already opened zones. */
1031         pthread_mutex_lock(&f->zbd_info->mutex);
1032         for (i = 0; i < f->zbd_info->num_open_zones; i++) {
1033                 zone_idx = f->zbd_info->open_zones[i];
1034                 pthread_mutex_unlock(&f->zbd_info->mutex);
1035                 pthread_mutex_unlock(&z->mutex);
1036
1037                 z = &f->zbd_info->zone_info[zone_idx];
1038
1039                 pthread_mutex_lock(&z->mutex);
1040                 if (z->wp + min_bs <= (z+1)->start)
1041                         goto out;
1042                 pthread_mutex_lock(&f->zbd_info->mutex);
1043         }
1044         pthread_mutex_unlock(&f->zbd_info->mutex);
1045         pthread_mutex_unlock(&z->mutex);
1046         dprint(FD_ZBD, "%s(%s): did not open another zone\n", __func__,
1047                f->file_name);
1048         return NULL;
1049
1050 out:
1051         dprint(FD_ZBD, "%s(%s): returning zone %d\n", __func__, f->file_name,
1052                zone_idx);
1053         io_u->offset = z->start;
1054         return z;
1055 }
1056
1057 /* The caller must hold z->mutex. */
1058 static struct fio_zone_info *zbd_replay_write_order(struct thread_data *td,
1059                                                     struct io_u *io_u,
1060                                                     struct fio_zone_info *z)
1061 {
1062         const struct fio_file *f = io_u->file;
1063         const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
1064
1065         if (!zbd_open_zone(td, io_u, z - f->zbd_info->zone_info)) {
1066                 pthread_mutex_unlock(&z->mutex);
1067                 z = zbd_convert_to_open_zone(td, io_u);
1068                 assert(z);
1069         }
1070
1071         if (z->verify_block * min_bs >= f->zbd_info->zone_size)
1072                 log_err("%s: %d * %d >= %llu\n", f->file_name, z->verify_block,
1073                         min_bs, (unsigned long long) f->zbd_info->zone_size);
1074         io_u->offset = z->start + z->verify_block++ * min_bs;
1075         return z;
1076 }
1077
1078 /*
1079  * Find another zone for which @io_u fits below the write pointer. Start
1080  * searching in zones @zb + 1 .. @zl and continue searching in zones
1081  * @zf .. @zb - 1.
1082  *
1083  * Either returns NULL or returns a zone pointer and holds the mutex for that
1084  * zone.
1085  */
1086 static struct fio_zone_info *
1087 zbd_find_zone(struct thread_data *td, struct io_u *io_u,
1088               struct fio_zone_info *zb, struct fio_zone_info *zl)
1089 {
1090         const uint32_t min_bs = td->o.min_bs[io_u->ddir];
1091         const struct fio_file *f = io_u->file;
1092         struct fio_zone_info *z1, *z2;
1093         const struct fio_zone_info *const zf =
1094                 &f->zbd_info->zone_info[zbd_zone_idx(f, f->file_offset)];
1095
1096         /*
1097          * Skip to the next non-empty zone in case of sequential I/O and to
1098          * the nearest non-empty zone in case of random I/O.
1099          */
1100         for (z1 = zb + 1, z2 = zb - 1; z1 < zl || z2 >= zf; z1++, z2--) {
1101                 if (z1 < zl && z1->cond != BLK_ZONE_COND_OFFLINE) {
1102                         pthread_mutex_lock(&z1->mutex);
1103                         if (z1->start + min_bs <= z1->wp)
1104                                 return z1;
1105                         pthread_mutex_unlock(&z1->mutex);
1106                 } else if (!td_random(td)) {
1107                         break;
1108                 }
1109                 if (td_random(td) && z2 >= zf &&
1110                     z2->cond != BLK_ZONE_COND_OFFLINE) {
1111                         pthread_mutex_lock(&z2->mutex);
1112                         if (z2->start + min_bs <= z2->wp)
1113                                 return z2;
1114                         pthread_mutex_unlock(&z2->mutex);
1115                 }
1116         }
1117         dprint(FD_ZBD, "%s: adjusting random read offset failed\n",
1118                f->file_name);
1119         return NULL;
1120 }
1121
1122 /**
1123  * zbd_queue_io - update the write pointer of a sequential zone
1124  * @io_u: I/O unit
1125  * @success: Whether or not the I/O unit has been queued successfully
1126  * @q: queueing status (busy, completed or queued).
1127  *
1128  * For write and trim operations, update the write pointer of the I/O unit
1129  * target zone.
1130  */
1131 static void zbd_queue_io(struct io_u *io_u, int q, bool success)
1132 {
1133         const struct fio_file *f = io_u->file;
1134         struct zoned_block_device_info *zbd_info = f->zbd_info;
1135         struct fio_zone_info *z;
1136         uint32_t zone_idx;
1137         uint64_t zone_end;
1138
1139         if (!zbd_info)
1140                 return;
1141
1142         zone_idx = zbd_zone_idx(f, io_u->offset);
1143         assert(zone_idx < zbd_info->nr_zones);
1144         z = &zbd_info->zone_info[zone_idx];
1145
1146         if (z->type != BLK_ZONE_TYPE_SEQWRITE_REQ)
1147                 return;
1148
1149         if (!success)
1150                 goto unlock;
1151
1152         dprint(FD_ZBD,
1153                "%s: queued I/O (%lld, %llu) for zone %u\n",
1154                f->file_name, io_u->offset, io_u->buflen, zone_idx);
1155
1156         switch (io_u->ddir) {
1157         case DDIR_WRITE:
1158                 zone_end = min((uint64_t)(io_u->offset + io_u->buflen),
1159                                (z + 1)->start);
1160                 pthread_mutex_lock(&zbd_info->mutex);
1161                 /*
1162                  * z->wp > zone_end means that one or more I/O errors
1163                  * have occurred.
1164                  */
1165                 if (z->wp <= zone_end)
1166                         zbd_info->sectors_with_data += zone_end - z->wp;
1167                 pthread_mutex_unlock(&zbd_info->mutex);
1168                 z->wp = zone_end;
1169                 break;
1170         case DDIR_TRIM:
1171                 assert(z->wp == z->start);
1172                 break;
1173         default:
1174                 break;
1175         }
1176
1177 unlock:
1178         if (!success || q != FIO_Q_QUEUED) {
1179                 /* BUSY or COMPLETED: unlock the zone */
1180                 pthread_mutex_unlock(&z->mutex);
1181                 io_u->zbd_put_io = NULL;
1182         }
1183 }
1184
1185 /**
1186  * zbd_put_io - Unlock an I/O unit target zone lock
1187  * @io_u: I/O unit
1188  */
1189 static void zbd_put_io(const struct io_u *io_u)
1190 {
1191         const struct fio_file *f = io_u->file;
1192         struct zoned_block_device_info *zbd_info = f->zbd_info;
1193         struct fio_zone_info *z;
1194         uint32_t zone_idx;
1195
1196         if (!zbd_info)
1197                 return;
1198
1199         zone_idx = zbd_zone_idx(f, io_u->offset);
1200         assert(zone_idx < zbd_info->nr_zones);
1201         z = &zbd_info->zone_info[zone_idx];
1202
1203         if (z->type != BLK_ZONE_TYPE_SEQWRITE_REQ)
1204                 return;
1205
1206         dprint(FD_ZBD,
1207                "%s: terminate I/O (%lld, %llu) for zone %u\n",
1208                f->file_name, io_u->offset, io_u->buflen, zone_idx);
1209
1210         assert(pthread_mutex_unlock(&z->mutex) == 0);
1211         zbd_check_swd(f);
1212 }
1213
1214 bool zbd_unaligned_write(int error_code)
1215 {
1216         switch (error_code) {
1217         case EIO:
1218         case EREMOTEIO:
1219                 return true;
1220         }
1221         return false;
1222 }
1223
1224 /**
1225  * zbd_adjust_block - adjust the offset and length as necessary for ZBD drives
1226  * @td: FIO thread data.
1227  * @io_u: FIO I/O unit.
1228  *
1229  * Locking strategy: returns with z->mutex locked if and only if z refers
1230  * to a sequential zone and if io_u_accept is returned. z is the zone that
1231  * corresponds to io_u->offset at the end of this function.
1232  */
1233 enum io_u_action zbd_adjust_block(struct thread_data *td, struct io_u *io_u)
1234 {
1235         const struct fio_file *f = io_u->file;
1236         uint32_t zone_idx_b;
1237         struct fio_zone_info *zb, *zl, *orig_zb;
1238         uint32_t orig_len = io_u->buflen;
1239         uint32_t min_bs = td->o.min_bs[io_u->ddir];
1240         uint64_t new_len;
1241         int64_t range;
1242
1243         if (!f->zbd_info)
1244                 return io_u_accept;
1245
1246         assert(is_valid_offset(f, io_u->offset));
1247         assert(io_u->buflen);
1248         zone_idx_b = zbd_zone_idx(f, io_u->offset);
1249         zb = &f->zbd_info->zone_info[zone_idx_b];
1250         orig_zb = zb;
1251
1252         /* Accept the I/O offset for conventional zones. */
1253         if (zb->type == BLK_ZONE_TYPE_CONVENTIONAL)
1254                 return io_u_accept;
1255
1256         /*
1257          * Accept the I/O offset for reads if reading beyond the write pointer
1258          * is enabled.
1259          */
1260         if (zb->cond != BLK_ZONE_COND_OFFLINE &&
1261             io_u->ddir == DDIR_READ && td->o.read_beyond_wp)
1262                 return io_u_accept;
1263
1264         zbd_check_swd(f);
1265
1266         /*
1267          * Lock the io_u target zone. The zone will be unlocked if io_u offset
1268          * is changed or when io_u completes and zbd_put_io() executed.
1269          * To avoid multiple jobs doing asynchronous I/Os from deadlocking each
1270          * other waiting for zone locks when building an io_u batch, first
1271          * only trylock the zone. If the zone is already locked by another job,
1272          * process the currently queued I/Os so that I/O progress is made and
1273          * zones unlocked.
1274          */
1275         if (pthread_mutex_trylock(&zb->mutex) != 0) {
1276                 if (!td_ioengine_flagged(td, FIO_SYNCIO))
1277                         io_u_quiesce(td);
1278                 pthread_mutex_lock(&zb->mutex);
1279         }
1280
1281         switch (io_u->ddir) {
1282         case DDIR_READ:
1283                 if (td->runstate == TD_VERIFYING) {
1284                         zb = zbd_replay_write_order(td, io_u, zb);
1285                         goto accept;
1286                 }
1287                 /*
1288                  * Check that there is enough written data in the zone to do an
1289                  * I/O of at least min_bs B. If there isn't, find a new zone for
1290                  * the I/O.
1291                  */
1292                 range = zb->cond != BLK_ZONE_COND_OFFLINE ?
1293                         zb->wp - zb->start : 0;
1294                 if (range < min_bs ||
1295                     ((!td_random(td)) && (io_u->offset + min_bs > zb->wp))) {
1296                         pthread_mutex_unlock(&zb->mutex);
1297                         zl = &f->zbd_info->zone_info[zbd_zone_idx(f,
1298                                                 f->file_offset + f->io_size)];
1299                         zb = zbd_find_zone(td, io_u, zb, zl);
1300                         if (!zb) {
1301                                 dprint(FD_ZBD,
1302                                        "%s: zbd_find_zone(%lld, %llu) failed\n",
1303                                        f->file_name, io_u->offset,
1304                                        io_u->buflen);
1305                                 goto eof;
1306                         }
1307                         /*
1308                          * zbd_find_zone() returned a zone with a range of at
1309                          * least min_bs.
1310                          */
1311                         range = zb->wp - zb->start;
1312                         assert(range >= min_bs);
1313
1314                         if (!td_random(td))
1315                                 io_u->offset = zb->start;
1316                 }
1317                 /*
1318                  * Make sure the I/O is within the zone valid data range while
1319                  * maximizing the I/O size and preserving randomness.
1320                  */
1321                 if (range <= io_u->buflen)
1322                         io_u->offset = zb->start;
1323                 else if (td_random(td))
1324                         io_u->offset = zb->start +
1325                                 ((io_u->offset - orig_zb->start) %
1326                                  (range - io_u->buflen)) / min_bs * min_bs;
1327                 /*
1328                  * Make sure the I/O does not cross over the zone wp position.
1329                  */
1330                 new_len = min((unsigned long long)io_u->buflen,
1331                               (unsigned long long)(zb->wp - io_u->offset));
1332                 new_len = new_len / min_bs * min_bs;
1333                 if (new_len < io_u->buflen) {
1334                         io_u->buflen = new_len;
1335                         dprint(FD_IO, "Changed length from %u into %llu\n",
1336                                orig_len, io_u->buflen);
1337                 }
1338                 assert(zb->start <= io_u->offset);
1339                 assert(io_u->offset + io_u->buflen <= zb->wp);
1340                 goto accept;
1341         case DDIR_WRITE:
1342                 if (io_u->buflen > f->zbd_info->zone_size)
1343                         goto eof;
1344                 if (!zbd_open_zone(td, io_u, zone_idx_b)) {
1345                         pthread_mutex_unlock(&zb->mutex);
1346                         zb = zbd_convert_to_open_zone(td, io_u);
1347                         if (!zb)
1348                                 goto eof;
1349                         zone_idx_b = zb - f->zbd_info->zone_info;
1350                 }
1351                 /* Check whether the zone reset threshold has been exceeded */
1352                 if (td->o.zrf.u.f) {
1353                         if (f->zbd_info->sectors_with_data >=
1354                             f->io_size * td->o.zrt.u.f &&
1355                             zbd_dec_and_reset_write_cnt(td, f)) {
1356                                 zb->reset_zone = 1;
1357                         }
1358                 }
1359                 /* Reset the zone pointer if necessary */
1360                 if (zb->reset_zone || zbd_zone_full(f, zb, min_bs)) {
1361                         assert(td->o.verify == VERIFY_NONE);
1362                         /*
1363                          * Since previous write requests may have been submitted
1364                          * asynchronously and since we will submit the zone
1365                          * reset synchronously, wait until previously submitted
1366                          * write requests have completed before issuing a
1367                          * zone reset.
1368                          */
1369                         io_u_quiesce(td);
1370                         zb->reset_zone = 0;
1371                         if (zbd_reset_zone(td, f, zb) < 0)
1372                                 goto eof;
1373                 }
1374                 /* Make writes occur at the write pointer */
1375                 assert(!zbd_zone_full(f, zb, min_bs));
1376                 io_u->offset = zb->wp;
1377                 if (!is_valid_offset(f, io_u->offset)) {
1378                         dprint(FD_ZBD, "Dropped request with offset %llu\n",
1379                                io_u->offset);
1380                         goto eof;
1381                 }
1382                 /*
1383                  * Make sure that the buflen is a multiple of the minimal
1384                  * block size. Give up if shrinking would make the request too
1385                  * small.
1386                  */
1387                 new_len = min((unsigned long long)io_u->buflen,
1388                               (zb + 1)->start - io_u->offset);
1389                 new_len = new_len / min_bs * min_bs;
1390                 if (new_len == io_u->buflen)
1391                         goto accept;
1392                 if (new_len >= min_bs) {
1393                         io_u->buflen = new_len;
1394                         dprint(FD_IO, "Changed length from %u into %llu\n",
1395                                orig_len, io_u->buflen);
1396                         goto accept;
1397                 }
1398                 log_err("Zone remainder %lld smaller than minimum block size %d\n",
1399                         ((zb + 1)->start - io_u->offset),
1400                         min_bs);
1401                 goto eof;
1402         case DDIR_TRIM:
1403                 /* fall-through */
1404         case DDIR_SYNC:
1405         case DDIR_DATASYNC:
1406         case DDIR_SYNC_FILE_RANGE:
1407         case DDIR_WAIT:
1408         case DDIR_LAST:
1409         case DDIR_INVAL:
1410                 goto accept;
1411         }
1412
1413         assert(false);
1414
1415 accept:
1416         assert(zb);
1417         assert(zb->cond != BLK_ZONE_COND_OFFLINE);
1418         assert(!io_u->zbd_queue_io);
1419         assert(!io_u->zbd_put_io);
1420         io_u->zbd_queue_io = zbd_queue_io;
1421         io_u->zbd_put_io = zbd_put_io;
1422         return io_u_accept;
1423
1424 eof:
1425         if (zb)
1426                 pthread_mutex_unlock(&zb->mutex);
1427         return io_u_eof;
1428 }
1429
1430 /* Return a string with ZBD statistics */
1431 char *zbd_write_status(const struct thread_stat *ts)
1432 {
1433         char *res;
1434
1435         if (asprintf(&res, "; %llu zone resets", (unsigned long long) ts->nr_zone_resets) < 0)
1436                 return NULL;
1437         return res;
1438 }