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