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