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