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