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