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