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