f4105b422522ae5ba15cef0bd8d8f020d1d0a6e9
[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)
35                 zone_idx = offset >> f->zbd_info->zone_size_log2;
36         else
37                 zone_idx = (offset >> 9) / 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 >> 9) > 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 << 9)) {
125                                 new_offset = (z+1)->start << 9;
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 %lu to %lu\n",
132                                          f->file_name, f->file_offset,
133                                          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 << 9;
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 %lu to %lu\n",
147                                          f->file_name, f->io_size,
148                                          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 << 9) % 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 << 9);
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  */
190 static int read_zone_info(int fd, uint64_t start_sector,
191                           void *buf, unsigned int bufsz)
192 {
193         struct blk_zone_report *hdr = buf;
194
195         if (bufsz < sizeof(*hdr))
196                 return -EINVAL;
197
198         memset(hdr, 0, sizeof(*hdr));
199
200         hdr->nr_zones = (bufsz - sizeof(*hdr)) / sizeof(struct blk_zone);
201         hdr->sector = start_sector;
202         return ioctl(fd, BLKREPORTZONE, hdr) >= 0 ? 0 : -errno;
203 }
204
205 /*
206  * Read up to 255 characters from the first line of a file. Strip the trailing
207  * newline.
208  */
209 static char *read_file(const char *path)
210 {
211         char line[256], *p = line;
212         FILE *f;
213
214         f = fopen(path, "rb");
215         if (!f)
216                 return NULL;
217         if (!fgets(line, sizeof(line), f))
218                 line[0] = '\0';
219         strsep(&p, "\n");
220         fclose(f);
221
222         return strdup(line);
223 }
224
225 static enum blk_zoned_model get_zbd_model(const char *file_name)
226 {
227         enum blk_zoned_model model = ZBD_DM_NONE;
228         char *zoned_attr_path = NULL;
229         char *model_str = NULL;
230         struct stat statbuf;
231
232         if (stat(file_name, &statbuf) < 0)
233                 goto out;
234         if (asprintf(&zoned_attr_path, "/sys/dev/block/%d:%d/queue/zoned",
235                      major(statbuf.st_rdev), minor(statbuf.st_rdev)) < 0)
236                 goto out;
237         model_str = read_file(zoned_attr_path);
238         if (!model_str)
239                 goto out;
240         dprint(FD_ZBD, "%s: zbd model string: %s\n", file_name, model_str);
241         if (strcmp(model_str, "host-aware") == 0)
242                 model = ZBD_DM_HOST_AWARE;
243         else if (strcmp(model_str, "host-managed") == 0)
244                 model = ZBD_DM_HOST_MANAGED;
245
246 out:
247         free(model_str);
248         free(zoned_attr_path);
249         return model;
250 }
251
252 static int ilog2(uint64_t i)
253 {
254         int log = -1;
255
256         while (i) {
257                 i >>= 1;
258                 log++;
259         }
260         return log;
261 }
262
263 /*
264  * Initialize f->zbd_info for devices that are not zoned block devices. This
265  * allows to execute a ZBD workload against a non-ZBD device.
266  */
267 static int init_zone_info(struct thread_data *td, struct fio_file *f)
268 {
269         uint32_t nr_zones;
270         struct fio_zone_info *p;
271         uint64_t zone_size;
272         struct zoned_block_device_info *zbd_info = NULL;
273         pthread_mutexattr_t attr;
274         int i;
275
276         zone_size = td->o.zone_size >> 9;
277         assert(zone_size);
278         nr_zones = ((f->real_file_size >> 9) + zone_size - 1) / zone_size;
279         zbd_info = scalloc(1, sizeof(*zbd_info) +
280                            (nr_zones + 1) * sizeof(zbd_info->zone_info[0]));
281         if (!zbd_info)
282                 return -ENOMEM;
283
284         pthread_mutexattr_init(&attr);
285         pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
286         pthread_mutexattr_setpshared(&attr, true);
287         pthread_mutex_init(&zbd_info->mutex, &attr);
288         zbd_info->refcount = 1;
289         p = &zbd_info->zone_info[0];
290         for (i = 0; i < nr_zones; i++, p++) {
291                 pthread_mutex_init(&p->mutex, &attr);
292                 p->start = i * zone_size;
293                 p->wp = p->start + zone_size;
294                 p->type = BLK_ZONE_TYPE_SEQWRITE_REQ;
295                 p->cond = BLK_ZONE_COND_EMPTY;
296         }
297         /* a sentinel */
298         p->start = nr_zones * zone_size;
299
300         f->zbd_info = zbd_info;
301         f->zbd_info->zone_size = zone_size;
302         f->zbd_info->zone_size_log2 = is_power_of_2(zone_size) ?
303                 ilog2(zone_size) + 9 : -1;
304         f->zbd_info->nr_zones = nr_zones;
305         pthread_mutexattr_destroy(&attr);
306         return 0;
307 }
308
309 /*
310  * Parse the BLKREPORTZONE output and store it in f->zbd_info. Must be called
311  * only for devices that support this ioctl, namely zoned block devices.
312  */
313 static int parse_zone_info(struct thread_data *td, struct fio_file *f)
314 {
315         const unsigned int bufsz = sizeof(struct blk_zone_report) +
316                 4096 * sizeof(struct blk_zone);
317         uint32_t nr_zones;
318         struct blk_zone_report *hdr;
319         const struct blk_zone *z;
320         struct fio_zone_info *p;
321         uint64_t zone_size, start_sector;
322         struct zoned_block_device_info *zbd_info = NULL;
323         pthread_mutexattr_t attr;
324         void *buf;
325         int fd, i, j, ret = 0;
326
327         pthread_mutexattr_init(&attr);
328         pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
329         pthread_mutexattr_setpshared(&attr, true);
330
331         buf = malloc(bufsz);
332         if (!buf)
333                 goto out;
334
335         fd = open(f->file_name, O_RDONLY | O_LARGEFILE);
336         if (fd < 0) {
337                 ret = -errno;
338                 goto free;
339         }
340
341         ret = read_zone_info(fd, 0, buf, bufsz);
342         if (ret < 0) {
343                 log_info("fio: BLKREPORTZONE(%lu) failed for %s (%d).\n",
344                          0UL, f->file_name, -ret);
345                 goto close;
346         }
347         hdr = buf;
348         if (hdr->nr_zones < 1) {
349                 log_info("fio: %s has invalid zone information.\n",
350                          f->file_name);
351                 goto close;
352         }
353         z = (void *)(hdr + 1);
354         zone_size = z->len;
355         nr_zones = ((f->real_file_size >> 9) + zone_size - 1) / zone_size;
356
357         if (td->o.zone_size == 0) {
358                 td->o.zone_size = zone_size << 9;
359         } else if (td->o.zone_size != zone_size << 9) {
360                 log_info("fio: %s job parameter zonesize %lld does not match disk zone size %ld.\n",
361                          f->file_name, td->o.zone_size, zone_size << 9);
362                 ret = -EINVAL;
363                 goto close;
364         }
365
366         dprint(FD_ZBD, "Device %s has %d zones of size %lu KB\n", f->file_name,
367                nr_zones, zone_size / 2);
368
369         zbd_info = scalloc(1, sizeof(*zbd_info) +
370                            (nr_zones + 1) * sizeof(zbd_info->zone_info[0]));
371         ret = -ENOMEM;
372         if (!zbd_info)
373                 goto close;
374         pthread_mutex_init(&zbd_info->mutex, &attr);
375         zbd_info->refcount = 1;
376         p = &zbd_info->zone_info[0];
377         for (start_sector = 0, j = 0; j < nr_zones;) {
378                 z = (void *)(hdr + 1);
379                 for (i = 0; i < hdr->nr_zones; i++, j++, z++, p++) {
380                         pthread_mutex_init(&p->mutex, &attr);
381                         p->start = z->start;
382                         switch (z->cond) {
383                         case BLK_ZONE_COND_NOT_WP:
384                                 p->wp = z->start;
385                                 break;
386                         case BLK_ZONE_COND_FULL:
387                                 p->wp = z->start + zone_size;
388                                 break;
389                         default:
390                                 assert(z->start <= z->wp);
391                                 assert(z->wp <= z->start + zone_size);
392                                 p->wp = z->wp;
393                                 break;
394                         }
395                         p->type = z->type;
396                         p->cond = z->cond;
397                         if (j > 0 && p->start != p[-1].start + zone_size) {
398                                 log_info("%s: invalid zone data\n",
399                                          f->file_name);
400                                 ret = -EINVAL;
401                                 goto close;
402                         }
403                 }
404                 z--;
405                 start_sector = z->start + z->len;
406                 if (j >= nr_zones)
407                         break;
408                 ret = read_zone_info(fd, start_sector, buf, bufsz);
409                 if (ret < 0) {
410                         log_info("fio: BLKREPORTZONE(%lu) failed for %s (%d).\n",
411                                  start_sector, f->file_name, -ret);
412                         goto close;
413                 }
414         }
415         /* a sentinel */
416         zbd_info->zone_info[nr_zones].start = start_sector;
417
418         f->zbd_info = zbd_info;
419         f->zbd_info->zone_size = zone_size;
420         f->zbd_info->zone_size_log2 = is_power_of_2(zone_size) ?
421                 ilog2(zone_size) + 9 : -1;
422         f->zbd_info->nr_zones = nr_zones;
423         zbd_info = NULL;
424         ret = 0;
425
426 close:
427         sfree(zbd_info);
428         close(fd);
429 free:
430         free(buf);
431 out:
432         pthread_mutexattr_destroy(&attr);
433         return ret;
434 }
435
436 /*
437  * Allocate zone information and store it into f->zbd_info if zonemode=zbd.
438  *
439  * Returns 0 upon success and a negative error code upon failure.
440  */
441 int zbd_create_zone_info(struct thread_data *td, struct fio_file *f)
442 {
443         enum blk_zoned_model zbd_model;
444         int ret = 0;
445
446         assert(td->o.zone_mode == ZONE_MODE_ZBD);
447
448         zbd_model = get_zbd_model(f->file_name);
449         switch (zbd_model) {
450         case ZBD_DM_HOST_AWARE:
451         case ZBD_DM_HOST_MANAGED:
452                 ret = parse_zone_info(td, f);
453                 break;
454         case ZBD_DM_NONE:
455                 ret = init_zone_info(td, f);
456                 break;
457         }
458         if (ret == 0)
459                 f->zbd_info->model = zbd_model;
460         return ret;
461 }
462
463 void zbd_free_zone_info(struct fio_file *f)
464 {
465         uint32_t refcount;
466
467         if (!f->zbd_info)
468                 return;
469
470         pthread_mutex_lock(&f->zbd_info->mutex);
471         refcount = --f->zbd_info->refcount;
472         pthread_mutex_unlock(&f->zbd_info->mutex);
473
474         assert((int32_t)refcount >= 0);
475         if (refcount == 0)
476                 sfree(f->zbd_info);
477         f->zbd_info = NULL;
478 }
479
480 /*
481  * Initialize f->zbd_info.
482  *
483  * Returns 0 upon success and a negative error code upon failure.
484  *
485  * Note: this function can only work correctly if it is called before the first
486  * fio fork() call.
487  */
488 static int zbd_init_zone_info(struct thread_data *td, struct fio_file *file)
489 {
490         struct thread_data *td2;
491         struct fio_file *f2;
492         int i, j, ret;
493
494         for_each_td(td2, i) {
495                 for_each_file(td2, f2, j) {
496                         if (td2 == td && f2 == file)
497                                 continue;
498                         if (!f2->zbd_info ||
499                             strcmp(f2->file_name, file->file_name) != 0)
500                                 continue;
501                         file->zbd_info = f2->zbd_info;
502                         file->zbd_info->refcount++;
503                         return 0;
504                 }
505         }
506
507         ret = zbd_create_zone_info(td, file);
508         if (ret < 0)
509                 td_verror(td, -ret, "BLKREPORTZONE failed");
510         return ret;
511 }
512
513 int zbd_init(struct thread_data *td)
514 {
515         struct fio_file *f;
516         int i;
517
518         for_each_file(td, f, i) {
519                 if (f->filetype != FIO_TYPE_BLOCK)
520                         continue;
521                 if (td->o.zone_size && td->o.zone_size < 512) {
522                         log_err("%s: zone size must be at least 512 bytes for --zonemode=zbd\n\n",
523                                 f->file_name);
524                         return 1;
525                 }
526                 if (td->o.zone_size == 0 &&
527                     get_zbd_model(f->file_name) == ZBD_DM_NONE) {
528                         log_err("%s: Specifying the zone size is mandatory for regular block devices with --zonemode=zbd\n\n",
529                                 f->file_name);
530                         return 1;
531                 }
532                 zbd_init_zone_info(td, f);
533         }
534
535         if (!zbd_using_direct_io()) {
536                 log_err("Using direct I/O is mandatory for writing to ZBD drives\n\n");
537                 return 1;
538         }
539
540         if (!zbd_verify_sizes())
541                 return 1;
542
543         if (!zbd_verify_bs())
544                 return 1;
545
546         return 0;
547 }
548
549 /**
550  * zbd_reset_range - reset zones for a range of sectors
551  * @td: FIO thread data.
552  * @f: Fio file for which to reset zones
553  * @sector: Starting sector in units of 512 bytes
554  * @nr_sectors: Number of sectors in units of 512 bytes
555  *
556  * Returns 0 upon success and a negative error code upon failure.
557  */
558 static int zbd_reset_range(struct thread_data *td, const struct fio_file *f,
559                            uint64_t sector, uint64_t nr_sectors)
560 {
561         struct blk_zone_range zr = {
562                 .sector         = sector,
563                 .nr_sectors     = nr_sectors,
564         };
565         uint32_t zone_idx_b, zone_idx_e;
566         struct fio_zone_info *zb, *ze, *z;
567         int ret = 0;
568
569         assert(f->fd != -1);
570         assert(is_valid_offset(f, ((sector + nr_sectors) << 9) - 1));
571         switch (f->zbd_info->model) {
572         case ZBD_DM_HOST_AWARE:
573         case ZBD_DM_HOST_MANAGED:
574                 ret = ioctl(f->fd, BLKRESETZONE, &zr);
575                 if (ret < 0) {
576                         td_verror(td, errno, "resetting wp failed");
577                         log_err("%s: resetting wp for %llu sectors at sector %llu failed (%d).\n",
578                                 f->file_name, zr.nr_sectors, zr.sector, errno);
579                         return ret;
580                 }
581                 break;
582         case ZBD_DM_NONE:
583                 break;
584         }
585
586         zone_idx_b = zbd_zone_idx(f, sector << 9);
587         zb = &f->zbd_info->zone_info[zone_idx_b];
588         zone_idx_e = zbd_zone_idx(f, (sector + nr_sectors) << 9);
589         ze = &f->zbd_info->zone_info[zone_idx_e];
590         for (z = zb; z < ze; z++) {
591                 pthread_mutex_lock(&z->mutex);
592                 z->wp = z->start;
593                 z->verify_block = 0;
594                 pthread_mutex_unlock(&z->mutex);
595         }
596
597         td->ts.nr_zone_resets += ze - zb;
598
599         return ret;
600 }
601
602 /**
603  * zbd_reset_zone - reset the write pointer of a single zone
604  * @td: FIO thread data.
605  * @f: FIO file associated with the disk for which to reset a write pointer.
606  * @z: Zone to reset.
607  *
608  * Returns 0 upon success and a negative error code upon failure.
609  */
610 static int zbd_reset_zone(struct thread_data *td, const struct fio_file *f,
611                           struct fio_zone_info *z)
612 {
613         int ret;
614
615         dprint(FD_ZBD, "%s: resetting wp of zone %lu.\n", f->file_name,
616                z - f->zbd_info->zone_info);
617         ret = zbd_reset_range(td, f, z->start, (z+1)->start - z->start);
618         return ret;
619 }
620
621 /*
622  * Reset a range of zones. Returns 0 upon success and 1 upon failure.
623  * @td: fio thread data.
624  * @f: fio file for which to reset zones
625  * @zb: first zone to reset.
626  * @ze: first zone not to reset.
627  * @all_zones: whether to reset all zones or only those zones for which the
628  *      write pointer is not a multiple of td->o.min_bs[DDIR_WRITE].
629  */
630 static int zbd_reset_zones(struct thread_data *td, struct fio_file *f,
631                            struct fio_zone_info *const zb,
632                            struct fio_zone_info *const ze, bool all_zones)
633 {
634         struct fio_zone_info *z, *start_z = ze;
635         const uint32_t min_bs = td->o.min_bs[DDIR_WRITE] >> 9;
636         bool reset_wp;
637         int res = 0;
638
639         dprint(FD_ZBD, "%s: examining zones %lu .. %lu\n", f->file_name,
640                zb - f->zbd_info->zone_info, ze - f->zbd_info->zone_info);
641         assert(f->fd != -1);
642         for (z = zb; z < ze; z++) {
643                 pthread_mutex_lock(&z->mutex);
644                 switch (z->type) {
645                 case BLK_ZONE_TYPE_SEQWRITE_REQ:
646                         reset_wp = all_zones ? z->wp != z->start :
647                                         (td->o.td_ddir & TD_DDIR_WRITE) &&
648                                         z->wp % min_bs != 0;
649                         if (start_z == ze && reset_wp) {
650                                 start_z = z;
651                         } else if (start_z < ze && !reset_wp) {
652                                 dprint(FD_ZBD,
653                                        "%s: resetting zones %lu .. %lu\n",
654                                        f->file_name,
655                                        start_z - f->zbd_info->zone_info,
656                                        z - f->zbd_info->zone_info);
657                                 if (zbd_reset_range(td, f, start_z->start,
658                                                 z->start - start_z->start) < 0)
659                                         res = 1;
660                                 start_z = ze;
661                         }
662                         break;
663                 default:
664                         if (start_z == ze)
665                                 break;
666                         dprint(FD_ZBD, "%s: resetting zones %lu .. %lu\n",
667                                f->file_name, start_z - f->zbd_info->zone_info,
668                                z - f->zbd_info->zone_info);
669                         if (zbd_reset_range(td, f, start_z->start,
670                                             z->start - start_z->start) < 0)
671                                 res = 1;
672                         start_z = ze;
673                         break;
674                 }
675         }
676         if (start_z < ze) {
677                 dprint(FD_ZBD, "%s: resetting zones %lu .. %lu\n", f->file_name,
678                        start_z - f->zbd_info->zone_info,
679                        z - f->zbd_info->zone_info);
680                 if (zbd_reset_range(td, f, start_z->start,
681                                     z->start - start_z->start) < 0)
682                         res = 1;
683         }
684         for (z = zb; z < ze; z++)
685                 pthread_mutex_unlock(&z->mutex);
686
687         return res;
688 }
689
690 void zbd_file_reset(struct thread_data *td, struct fio_file *f)
691 {
692         struct fio_zone_info *zb, *ze;
693         uint32_t zone_idx_e;
694
695         if (!f->zbd_info)
696                 return;
697
698         zb = &f->zbd_info->zone_info[zbd_zone_idx(f, f->file_offset)];
699         zone_idx_e = zbd_zone_idx(f, f->file_offset + f->io_size);
700         ze = &f->zbd_info->zone_info[zone_idx_e];
701         /*
702          * If data verification is enabled reset the affected zones before
703          * writing any data to avoid that a zone reset has to be issued while
704          * writing data, which causes data loss.
705          */
706         zbd_reset_zones(td, f, zb, ze, td->o.verify != VERIFY_NONE &&
707                         (td->o.td_ddir & TD_DDIR_WRITE) &&
708                         td->runstate != TD_VERIFYING);
709 }
710
711 /* The caller must hold z->mutex. */
712 static void zbd_replay_write_order(struct thread_data *td, struct io_u *io_u,
713                                    struct fio_zone_info *z)
714 {
715         const struct fio_file *f = io_u->file;
716         const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
717
718         if (z->verify_block * min_bs >= f->zbd_info->zone_size)
719                 log_err("%s: %d * %d >= %ld\n", f->file_name, z->verify_block,
720                         min_bs, f->zbd_info->zone_size);
721         io_u->offset = (z->start << 9) + z->verify_block++ * min_bs;
722 }
723
724 /*
725  * Find another zone for which @io_u fits below the write pointer. Start
726  * searching in zones @zb + 1 .. @zl and continue searching in zones
727  * @zf .. @zb - 1.
728  *
729  * Either returns NULL or returns a zone pointer and holds the mutex for that
730  * zone.
731  */
732 static struct fio_zone_info *
733 zbd_find_zone(struct thread_data *td, struct io_u *io_u,
734               struct fio_zone_info *zb, struct fio_zone_info *zl)
735 {
736         const uint32_t min_bs = td->o.min_bs[io_u->ddir];
737         const struct fio_file *f = io_u->file;
738         struct fio_zone_info *z1, *z2;
739         const struct fio_zone_info *const zf =
740                 &f->zbd_info->zone_info[zbd_zone_idx(f, f->file_offset)];
741
742         /*
743          * Skip to the next non-empty zone in case of sequential I/O and to
744          * the nearest non-empty zone in case of random I/O.
745          */
746         for (z1 = zb + 1, z2 = zb - 1; z1 < zl || z2 >= zf; z1++, z2--) {
747                 if (z1 < zl && z1->cond != BLK_ZONE_COND_OFFLINE) {
748                         pthread_mutex_lock(&z1->mutex);
749                         if (z1->start + (min_bs >> 9) <= z1->wp)
750                                 return z1;
751                         pthread_mutex_unlock(&z1->mutex);
752                 } else if (!td_random(td)) {
753                         break;
754                 }
755                 if (td_random(td) && z2 >= zf &&
756                     z2->cond != BLK_ZONE_COND_OFFLINE) {
757                         pthread_mutex_lock(&z2->mutex);
758                         if (z2->start + (min_bs >> 9) <= z2->wp)
759                                 return z2;
760                         pthread_mutex_unlock(&z2->mutex);
761                 }
762         }
763         dprint(FD_ZBD, "%s: adjusting random read offset failed\n",
764                f->file_name);
765         return NULL;
766 }
767
768
769 /**
770  * zbd_post_submit - update the write pointer and unlock the zone lock
771  * @io_u: I/O unit
772  * @success: Whether or not the I/O unit has been executed successfully
773  *
774  * For write and trim operations, update the write pointer of all affected
775  * zones.
776  */
777 static void zbd_post_submit(const struct io_u *io_u, bool success)
778 {
779         struct zoned_block_device_info *zbd_info;
780         struct fio_zone_info *z;
781         uint32_t zone_idx;
782         uint64_t end, zone_end;
783
784         zbd_info = io_u->file->zbd_info;
785         if (!zbd_info)
786                 return;
787
788         zone_idx = zbd_zone_idx(io_u->file, io_u->offset);
789         end = (io_u->offset + io_u->buflen) >> 9;
790         z = &zbd_info->zone_info[zone_idx];
791         assert(zone_idx < zbd_info->nr_zones);
792         if (z->type != BLK_ZONE_TYPE_SEQWRITE_REQ)
793                 return;
794         if (!success)
795                 goto unlock;
796         switch (io_u->ddir) {
797         case DDIR_WRITE:
798                 zone_end = min(end, (z + 1)->start);
799                 z->wp = zone_end;
800                 break;
801         case DDIR_TRIM:
802                 assert(z->wp == z->start);
803                 break;
804         default:
805                 break;
806         }
807 unlock:
808         pthread_mutex_unlock(&z->mutex);
809 }
810
811 bool zbd_unaligned_write(int error_code)
812 {
813         switch (error_code) {
814         case EIO:
815         case EREMOTEIO:
816                 return true;
817         }
818         return false;
819 }
820
821 /**
822  * zbd_adjust_block - adjust the offset and length as necessary for ZBD drives
823  * @td: FIO thread data.
824  * @io_u: FIO I/O unit.
825  *
826  * Locking strategy: returns with z->mutex locked if and only if z refers
827  * to a sequential zone and if io_u_accept is returned. z is the zone that
828  * corresponds to io_u->offset at the end of this function.
829  */
830 enum io_u_action zbd_adjust_block(struct thread_data *td, struct io_u *io_u)
831 {
832         const struct fio_file *f = io_u->file;
833         uint32_t zone_idx_b;
834         struct fio_zone_info *zb, *zl;
835         uint32_t orig_len = io_u->buflen;
836         uint32_t min_bs = td->o.min_bs[io_u->ddir];
837         uint64_t new_len;
838         int64_t range;
839
840         if (!f->zbd_info)
841                 return io_u_accept;
842
843         assert(is_valid_offset(f, io_u->offset));
844         assert(io_u->buflen);
845         zone_idx_b = zbd_zone_idx(f, io_u->offset);
846         zb = &f->zbd_info->zone_info[zone_idx_b];
847
848         /* Accept the I/O offset for conventional zones. */
849         if (zb->type == BLK_ZONE_TYPE_CONVENTIONAL)
850                 return io_u_accept;
851
852         /*
853          * Accept the I/O offset for reads if reading beyond the write pointer
854          * is enabled.
855          */
856         if (zb->cond != BLK_ZONE_COND_OFFLINE &&
857             io_u->ddir == DDIR_READ && td->o.read_beyond_wp)
858                 return io_u_accept;
859
860         pthread_mutex_lock(&zb->mutex);
861         switch (io_u->ddir) {
862         case DDIR_READ:
863                 if (td->runstate == TD_VERIFYING) {
864                         zbd_replay_write_order(td, io_u, zb);
865                         goto accept;
866                 }
867                 /*
868                  * Avoid reads past the write pointer because such reads do not
869                  * hit the medium.
870                  */
871                 range = zb->cond != BLK_ZONE_COND_OFFLINE ?
872                         ((zb->wp - zb->start) << 9) - io_u->buflen : 0;
873                 if (td_random(td) && range >= 0) {
874                         io_u->offset = (zb->start << 9) +
875                                 ((io_u->offset - (zb->start << 9)) %
876                                  (range + 1)) / min_bs * min_bs;
877                         assert(zb->start << 9 <= io_u->offset);
878                         assert(io_u->offset + io_u->buflen <= zb->wp << 9);
879                         goto accept;
880                 }
881                 if (zb->cond == BLK_ZONE_COND_OFFLINE ||
882                     (io_u->offset + io_u->buflen) >> 9 > zb->wp) {
883                         pthread_mutex_unlock(&zb->mutex);
884                         zl = &f->zbd_info->zone_info[zbd_zone_idx(f,
885                                                 f->file_offset + f->io_size)];
886                         zb = zbd_find_zone(td, io_u, zb, zl);
887                         if (!zb) {
888                                 dprint(FD_ZBD,
889                                        "%s: zbd_find_zone(%lld, %llu) failed\n",
890                                        f->file_name, io_u->offset,
891                                        io_u->buflen);
892                                 goto eof;
893                         }
894                         io_u->offset = zb->start << 9;
895                 }
896                 if ((io_u->offset + io_u->buflen) >> 9 > zb->wp) {
897                         dprint(FD_ZBD, "%s: %lld + %lld > %" PRIu64 "\n",
898                                f->file_name, io_u->offset, io_u->buflen,
899                                zb->wp);
900                         goto eof;
901                 }
902                 goto accept;
903         case DDIR_WRITE:
904                 if (io_u->buflen > (f->zbd_info->zone_size << 9))
905                         goto eof;
906                 /* Reset the zone pointer if necessary */
907                 if (zb->reset_zone || zbd_zone_full(f, zb, min_bs)) {
908                         assert(td->o.verify == VERIFY_NONE);
909                         /*
910                          * Since previous write requests may have been submitted
911                          * asynchronously and since we will submit the zone
912                          * reset synchronously, wait until previously submitted
913                          * write requests have completed before issuing a
914                          * zone reset.
915                          */
916                         io_u_quiesce(td);
917                         zb->reset_zone = 0;
918                         if (zbd_reset_zone(td, f, zb) < 0)
919                                 goto eof;
920                 }
921                 /* Make writes occur at the write pointer */
922                 assert(!zbd_zone_full(f, zb, min_bs));
923                 io_u->offset = zb->wp << 9;
924                 if (!is_valid_offset(f, io_u->offset)) {
925                         dprint(FD_ZBD, "Dropped request with offset %llu\n",
926                                io_u->offset);
927                         goto eof;
928                 }
929                 /*
930                  * Make sure that the buflen is a multiple of the minimal
931                  * block size. Give up if shrinking would make the request too
932                  * small.
933                  */
934                 new_len = min((unsigned long long)io_u->buflen,
935                               ((zb + 1)->start << 9) - io_u->offset);
936                 new_len = new_len / min_bs * min_bs;
937                 if (new_len == io_u->buflen)
938                         goto accept;
939                 if (new_len >= min_bs) {
940                         io_u->buflen = new_len;
941                         dprint(FD_IO, "Changed length from %u into %llu\n",
942                                orig_len, io_u->buflen);
943                         goto accept;
944                 }
945                 log_err("Zone remainder %lld smaller than minimum block size %d\n",
946                         (((zb + 1)->start << 9) - io_u->offset),
947                         min_bs);
948                 goto eof;
949         case DDIR_TRIM:
950                 /* fall-through */
951         case DDIR_SYNC:
952         case DDIR_DATASYNC:
953         case DDIR_SYNC_FILE_RANGE:
954         case DDIR_WAIT:
955         case DDIR_LAST:
956         case DDIR_INVAL:
957                 goto accept;
958         }
959
960         assert(false);
961
962 accept:
963         assert(zb);
964         assert(zb->cond != BLK_ZONE_COND_OFFLINE);
965         assert(!io_u->post_submit);
966         io_u->post_submit = zbd_post_submit;
967         return io_u_accept;
968
969 eof:
970         if (zb)
971                 pthread_mutex_unlock(&zb->mutex);
972         return io_u_eof;
973 }
974
975 /* Return a string with ZBD statistics */
976 char *zbd_write_status(const struct thread_stat *ts)
977 {
978         char *res;
979
980         if (asprintf(&res, "; %ld zone resets", ts->nr_zone_resets) < 0)
981                 return NULL;
982         return res;
983 }