zbd: simplify zbd_close_zone()
[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
410a071c
DLM
25static bool is_valid_offset(const struct fio_file *f, uint64_t offset)
26{
27 return (uint64_t)(offset - f->file_offset) < f->io_size;
28}
29
30static inline unsigned int zbd_zone_nr(const struct fio_file *f,
31 struct fio_zone_info *zone)
32{
33 return zone - f->zbd_info->zone_info;
34}
35
36/**
37 * zbd_zone_idx - convert an offset into a zone number
38 * @f: file pointer.
39 * @offset: offset in bytes. If this offset is in the first zone_size bytes
40 * past the disk size then the index of the sentinel is returned.
41 */
42static uint32_t zbd_zone_idx(const struct fio_file *f, uint64_t offset)
43{
44 uint32_t zone_idx;
45
46 if (f->zbd_info->zone_size_log2 > 0)
47 zone_idx = offset >> f->zbd_info->zone_size_log2;
48 else
49 zone_idx = offset / f->zbd_info->zone_size;
50
51 return min(zone_idx, f->zbd_info->nr_zones);
52}
53
54/**
55 * zbd_zone_end - Return zone end location
56 * @z: zone info pointer.
57 */
58static inline uint64_t zbd_zone_end(const struct fio_zone_info *z)
59{
60 return (z+1)->start;
61}
62
63/**
64 * zbd_zone_capacity_end - Return zone capacity limit end location
65 * @z: zone info pointer.
66 */
67static inline uint64_t zbd_zone_capacity_end(const struct fio_zone_info *z)
68{
69 return z->start + z->capacity;
70}
71
72/**
73 * zbd_zone_full - verify whether a minimum number of bytes remain in a zone
74 * @f: file pointer.
75 * @z: zone info pointer.
76 * @required: minimum number of bytes that must remain in a zone.
77 *
78 * The caller must hold z->mutex.
79 */
80static bool zbd_zone_full(const struct fio_file *f, struct fio_zone_info *z,
81 uint64_t required)
82{
83 assert((required & 511) == 0);
84
85 return z->has_wp &&
86 z->wp + required > zbd_zone_capacity_end(z);
87}
88
89static void zone_lock(struct thread_data *td, const struct fio_file *f,
90 struct fio_zone_info *z)
91{
92 struct zoned_block_device_info *zbd = f->zbd_info;
93 uint32_t nz = z - zbd->zone_info;
94
95 /* A thread should never lock zones outside its working area. */
96 assert(f->min_zone <= nz && nz < f->max_zone);
97
98 assert(z->has_wp);
99
100 /*
101 * Lock the io_u target zone. The zone will be unlocked if io_u offset
102 * is changed or when io_u completes and zbd_put_io() executed.
103 * To avoid multiple jobs doing asynchronous I/Os from deadlocking each
104 * other waiting for zone locks when building an io_u batch, first
105 * only trylock the zone. If the zone is already locked by another job,
106 * process the currently queued I/Os so that I/O progress is made and
107 * zones unlocked.
108 */
109 if (pthread_mutex_trylock(&z->mutex) != 0) {
110 if (!td_ioengine_flagged(td, FIO_SYNCIO))
111 io_u_quiesce(td);
112 pthread_mutex_lock(&z->mutex);
113 }
114}
115
116static inline void zone_unlock(struct fio_zone_info *z)
117{
118 int ret;
119
120 assert(z->has_wp);
121 ret = pthread_mutex_unlock(&z->mutex);
122 assert(!ret);
123}
124
125static inline struct fio_zone_info *get_zone(const struct fio_file *f,
126 unsigned int zone_nr)
127{
128 return &f->zbd_info->zone_info[zone_nr];
129}
130
b7694961
DLM
131/**
132 * zbd_get_zoned_model - Get a device zoned model
133 * @td: FIO thread data
134 * @f: FIO file for which to get model information
135 */
38334c13
DLM
136static int zbd_get_zoned_model(struct thread_data *td, struct fio_file *f,
137 enum zbd_zoned_model *model)
b7694961
DLM
138{
139 int ret;
140
50cc48d5
NC
141 if (f->filetype == FIO_TYPE_PIPE) {
142 log_err("zonemode=zbd does not support pipes\n");
143 return -EINVAL;
144 }
145
9db0cde8
NC
146 /* If regular file, always emulate zones inside the file. */
147 if (f->filetype == FIO_TYPE_FILE) {
148 *model = ZBD_NONE;
149 return 0;
150 }
151
6c5b11d3
DLM
152 if (td->io_ops && td->io_ops->get_zoned_model)
153 ret = td->io_ops->get_zoned_model(td, f, model);
154 else
155 ret = blkzoned_get_zoned_model(td, f, model);
b7694961
DLM
156 if (ret < 0) {
157 td_verror(td, errno, "get zoned model failed");
158 log_err("%s: get zoned model failed (%d).\n",
159 f->file_name, errno);
160 }
161
162 return ret;
163}
164
165/**
166 * zbd_report_zones - Get zone information
167 * @td: FIO thread data.
168 * @f: FIO file for which to get zone information
169 * @offset: offset from which to report zones
170 * @zones: Array of struct zbd_zone
171 * @nr_zones: Size of @zones array
172 *
173 * Get zone information into @zones starting from the zone at offset @offset
174 * for the device specified by @f.
175 *
176 * Returns the number of zones reported upon success and a negative error code
177 * upon failure. If the zone report is empty, always assume an error (device
178 * problem) and return -EIO.
179 */
38334c13
DLM
180static int zbd_report_zones(struct thread_data *td, struct fio_file *f,
181 uint64_t offset, struct zbd_zone *zones,
182 unsigned int nr_zones)
b7694961
DLM
183{
184 int ret;
185
6c5b11d3
DLM
186 if (td->io_ops && td->io_ops->report_zones)
187 ret = td->io_ops->report_zones(td, f, offset, zones, nr_zones);
188 else
189 ret = blkzoned_report_zones(td, f, offset, zones, nr_zones);
b7694961
DLM
190 if (ret < 0) {
191 td_verror(td, errno, "report zones failed");
ee5e3436
SK
192 log_err("%s: report zones from sector %"PRIu64" failed (%d).\n",
193 f->file_name, offset >> 9, errno);
b7694961
DLM
194 } else if (ret == 0) {
195 td_verror(td, errno, "Empty zone report");
ee5e3436
SK
196 log_err("%s: report zones from sector %"PRIu64" is empty.\n",
197 f->file_name, offset >> 9);
b7694961
DLM
198 ret = -EIO;
199 }
200
201 return ret;
202}
203
204/**
205 * zbd_reset_wp - reset the write pointer of a range of zones
206 * @td: FIO thread data.
207 * @f: FIO file for which to reset zones
208 * @offset: Starting offset of the first zone to reset
209 * @length: Length of the range of zones to reset
210 *
211 * Reset the write pointer of all zones in the range @offset...@offset+@length.
212 * Returns 0 upon success and a negative error code upon failure.
213 */
38334c13
DLM
214static int zbd_reset_wp(struct thread_data *td, struct fio_file *f,
215 uint64_t offset, uint64_t length)
b7694961
DLM
216{
217 int ret;
218
6c5b11d3
DLM
219 if (td->io_ops && td->io_ops->reset_wp)
220 ret = td->io_ops->reset_wp(td, f, offset, length);
221 else
222 ret = blkzoned_reset_wp(td, f, offset, length);
b7694961
DLM
223 if (ret < 0) {
224 td_verror(td, errno, "resetting wp failed");
ee5e3436
SK
225 log_err("%s: resetting wp for %"PRIu64" sectors at sector %"PRIu64" failed (%d).\n",
226 f->file_name, length >> 9, offset >> 9, errno);
b7694961
DLM
227 }
228
229 return ret;
230}
231
410a071c
DLM
232/**
233 * zbd_reset_zone - reset the write pointer of a single zone
234 * @td: FIO thread data.
235 * @f: FIO file associated with the disk for which to reset a write pointer.
236 * @z: Zone to reset.
237 *
238 * Returns 0 upon success and a negative error code upon failure.
239 *
240 * The caller must hold z->mutex.
241 */
242static int zbd_reset_zone(struct thread_data *td, struct fio_file *f,
243 struct fio_zone_info *z)
244{
245 uint64_t offset = z->start;
246 uint64_t length = (z+1)->start - offset;
247 uint64_t data_in_zone = z->wp - z->start;
248 int ret = 0;
249
250 if (!data_in_zone)
251 return 0;
252
253 assert(is_valid_offset(f, offset + length - 1));
254
139d8dc6
DLM
255 dprint(FD_ZBD, "%s: resetting wp of zone %u.\n",
256 f->file_name, zbd_zone_nr(f, z));
257
410a071c
DLM
258 switch (f->zbd_info->model) {
259 case ZBD_HOST_AWARE:
260 case ZBD_HOST_MANAGED:
261 ret = zbd_reset_wp(td, f, offset, length);
262 if (ret < 0)
263 return ret;
264 break;
265 default:
266 break;
267 }
268
269 pthread_mutex_lock(&f->zbd_info->mutex);
270 f->zbd_info->sectors_with_data -= data_in_zone;
271 f->zbd_info->wp_sectors_with_data -= data_in_zone;
272 pthread_mutex_unlock(&f->zbd_info->mutex);
139d8dc6 273
410a071c
DLM
274 z->wp = z->start;
275 z->verify_block = 0;
276
277 td->ts.nr_zone_resets++;
278
279 return ret;
280}
281
282/**
283 * zbd_close_zone - Remove a zone from the open zones array.
284 * @td: FIO thread data.
285 * @f: FIO file associated with the disk for which to reset a write pointer.
286 * @zone_idx: Index of the zone to remove.
287 *
288 * The caller must hold f->zbd_info->mutex.
289 */
290static void zbd_close_zone(struct thread_data *td, const struct fio_file *f,
a23411bb 291 struct fio_zone_info *z)
410a071c 292{
a23411bb 293 uint32_t ozi;
410a071c 294
a23411bb
DLM
295 if (!z->open)
296 return;
297
298 for (ozi = 0; ozi < f->zbd_info->num_open_zones; ozi++) {
299 if (get_zone(f, f->zbd_info->open_zones[ozi]) == z)
410a071c
DLM
300 break;
301 }
a23411bb 302 if (ozi == f->zbd_info->num_open_zones)
410a071c
DLM
303 return;
304
a23411bb
DLM
305 dprint(FD_ZBD, "%s: closing zone %u\n",
306 f->file_name, zbd_zone_nr(f, z));
139d8dc6 307
a23411bb
DLM
308 memmove(f->zbd_info->open_zones + ozi,
309 f->zbd_info->open_zones + ozi + 1,
310 (ZBD_MAX_OPEN_ZONES - (ozi + 1)) *
410a071c 311 sizeof(f->zbd_info->open_zones[0]));
139d8dc6 312
410a071c
DLM
313 f->zbd_info->num_open_zones--;
314 td->num_open_zones--;
a23411bb 315 z->open = 0;
410a071c
DLM
316}
317
318/**
319 * zbd_reset_zones - Reset a range of zones.
320 * @td: fio thread data.
321 * @f: fio file for which to reset zones
322 * @zb: first zone to reset.
323 * @ze: first zone not to reset.
324 *
325 * Returns 0 upon success and 1 upon failure.
326 */
327static int zbd_reset_zones(struct thread_data *td, struct fio_file *f,
328 struct fio_zone_info *const zb,
329 struct fio_zone_info *const ze)
330{
331 struct fio_zone_info *z;
332 const uint64_t min_bs = td->o.min_bs[DDIR_WRITE];
333 int res = 0;
334
335 assert(min_bs);
336
139d8dc6
DLM
337 dprint(FD_ZBD, "%s: examining zones %u .. %u\n",
338 f->file_name, zbd_zone_nr(f, zb), zbd_zone_nr(f, ze));
339
410a071c 340 for (z = zb; z < ze; z++) {
410a071c
DLM
341 if (!z->has_wp)
342 continue;
139d8dc6 343
410a071c
DLM
344 zone_lock(td, f, z);
345 pthread_mutex_lock(&f->zbd_info->mutex);
a23411bb 346 zbd_close_zone(td, f, z);
410a071c 347 pthread_mutex_unlock(&f->zbd_info->mutex);
139d8dc6 348
410a071c
DLM
349 if (z->wp != z->start) {
350 dprint(FD_ZBD, "%s: resetting zone %u\n",
351 f->file_name, zbd_zone_nr(f, z));
352 if (zbd_reset_zone(td, f, z) < 0)
353 res = 1;
354 }
139d8dc6 355
410a071c
DLM
356 zone_unlock(z);
357 }
358
359 return res;
360}
361
d2f442bc
NC
362/**
363 * zbd_get_max_open_zones - Get the maximum number of open zones
364 * @td: FIO thread data
365 * @f: FIO file for which to get max open zones
366 * @max_open_zones: Upon success, result will be stored here.
367 *
368 * A @max_open_zones value set to zero means no limit.
369 *
370 * Returns 0 upon success and a negative error code upon failure.
371 */
38334c13
DLM
372static int zbd_get_max_open_zones(struct thread_data *td, struct fio_file *f,
373 unsigned int *max_open_zones)
d2f442bc
NC
374{
375 int ret;
376
377 if (td->io_ops && td->io_ops->get_max_open_zones)
378 ret = td->io_ops->get_max_open_zones(td, f, max_open_zones);
379 else
380 ret = blkzoned_get_max_open_zones(td, f, max_open_zones);
381 if (ret < 0) {
382 td_verror(td, errno, "get max open zones failed");
383 log_err("%s: get max open zones failed (%d).\n",
384 f->file_name, errno);
385 }
386
387 return ret;
388}
389
bfbdd35b 390/**
410a071c
DLM
391 * zbd_open_zone - Add a zone to the array of open zones.
392 * @td: fio thread data.
393 * @f: fio file that has the open zones to add.
394 * @zone_idx: Index of the zone to add.
bfbdd35b 395 *
410a071c
DLM
396 * Open a ZBD zone if it is not already open. Returns true if either the zone
397 * was already open or if the zone was successfully added to the array of open
398 * zones without exceeding the maximum number of open zones. Returns false if
399 * the zone was not already open and opening the zone would cause the zone limit
400 * to be exceeded.
bfbdd35b 401 */
410a071c
DLM
402static bool zbd_open_zone(struct thread_data *td, const struct fio_file *f,
403 uint32_t zone_idx)
1f57803b 404{
410a071c
DLM
405 const uint64_t min_bs = td->o.min_bs[DDIR_WRITE];
406 struct zoned_block_device_info *zbdi = f->zbd_info;
407 struct fio_zone_info *z = get_zone(f, zone_idx);
408 bool res = true;
fae3b9a0 409
410a071c
DLM
410 if (z->cond == ZBD_ZONE_COND_OFFLINE)
411 return false;
43bcbd5b 412
1f57803b 413 /*
410a071c
DLM
414 * Skip full zones with data verification enabled because resetting a
415 * zone causes data loss and hence causes verification to fail.
1f57803b 416 */
410a071c
DLM
417 if (td->o.verify != VERIFY_NONE && zbd_zone_full(f, z, min_bs))
418 return false;
4d4c71e6 419
410a071c
DLM
420 /*
421 * zbdi->max_open_zones == 0 means that there is no limit on the maximum
422 * number of open zones. In this case, do no track open zones in
423 * zbdi->open_zones array.
424 */
425 if (!zbdi->max_open_zones)
426 return true;
4d4c71e6 427
410a071c 428 pthread_mutex_lock(&zbdi->mutex);
b5a0f7ce
DLM
429
430 if (z->open) {
410a071c 431 /*
b5a0f7ce
DLM
432 * If the zone is going to be completely filled by writes
433 * already in-flight, handle it as a full zone instead of an
434 * open zone.
410a071c
DLM
435 */
436 if (z->wp >= zbd_zone_capacity_end(z))
437 res = false;
438 goto out;
439 }
139d8dc6 440
410a071c
DLM
441 res = false;
442 /* Zero means no limit */
443 if (td->o.job_max_open_zones > 0 &&
444 td->num_open_zones >= td->o.job_max_open_zones)
445 goto out;
446 if (zbdi->num_open_zones >= zbdi->max_open_zones)
447 goto out;
139d8dc6
DLM
448
449 dprint(FD_ZBD, "%s: opening zone %d\n",
450 f->file_name, zone_idx);
451
410a071c
DLM
452 zbdi->open_zones[zbdi->num_open_zones++] = zone_idx;
453 td->num_open_zones++;
454 z->open = 1;
455 res = true;
bfbdd35b 456
410a071c
DLM
457out:
458 pthread_mutex_unlock(&zbdi->mutex);
459 return res;
923f7c1e
DF
460}
461
bfbdd35b
BVA
462/* Verify whether direct I/O is used for all host-managed zoned drives. */
463static bool zbd_using_direct_io(void)
464{
465 struct thread_data *td;
466 struct fio_file *f;
467 int i, j;
468
469 for_each_td(td, i) {
470 if (td->o.odirect || !(td->o.td_ddir & TD_DDIR_WRITE))
471 continue;
472 for_each_file(td, f, j) {
473 if (f->zbd_info &&
b7694961 474 f->zbd_info->model == ZBD_HOST_MANAGED)
bfbdd35b
BVA
475 return false;
476 }
477 }
478
479 return true;
480}
481
482/* Whether or not the I/O range for f includes one or more sequential zones */
483static bool zbd_is_seq_job(struct fio_file *f)
484{
485 uint32_t zone_idx, zone_idx_b, zone_idx_e;
486
487 assert(f->zbd_info);
139d8dc6 488
bfbdd35b
BVA
489 if (f->io_size == 0)
490 return false;
139d8dc6 491
bfbdd35b
BVA
492 zone_idx_b = zbd_zone_idx(f, f->file_offset);
493 zone_idx_e = zbd_zone_idx(f, f->file_offset + f->io_size - 1);
494 for (zone_idx = zone_idx_b; zone_idx <= zone_idx_e; zone_idx++)
be7a6bae 495 if (get_zone(f, zone_idx)->has_wp)
bfbdd35b
BVA
496 return true;
497
498 return false;
499}
500
0bf93a1a
DLM
501/*
502 * Verify whether the file offset and size parameters are aligned with zone
503 * boundaries. If the file offset is not aligned, align it down to the start of
504 * the zone containing the start offset and align up the file io_size parameter.
505 */
506static bool zbd_zone_align_file_sizes(struct thread_data *td,
507 struct fio_file *f)
508{
509 const struct fio_zone_info *z;
510 uint64_t new_offset, new_end;
511 uint32_t zone_idx;
512
513 if (!f->zbd_info)
514 return true;
515 if (f->file_offset >= f->real_file_size)
516 return true;
517 if (!zbd_is_seq_job(f))
518 return true;
519
520 if (!td->o.zone_size) {
521 td->o.zone_size = f->zbd_info->zone_size;
522 if (!td->o.zone_size) {
523 log_err("%s: invalid 0 zone size\n",
524 f->file_name);
525 return false;
526 }
527 } else if (td->o.zone_size != f->zbd_info->zone_size) {
528 log_err("%s: zonesize %llu does not match the device zone size %"PRIu64".\n",
529 f->file_name, td->o.zone_size,
530 f->zbd_info->zone_size);
531 return false;
532 }
533
534 if (td->o.zone_skip % td->o.zone_size) {
535 log_err("%s: zoneskip %llu is not a multiple of the device zone size %llu.\n",
536 f->file_name, td->o.zone_skip,
537 td->o.zone_size);
538 return false;
539 }
540
541 zone_idx = zbd_zone_idx(f, f->file_offset);
542 z = get_zone(f, zone_idx);
543 if ((f->file_offset != z->start) &&
544 (td->o.td_ddir != TD_DDIR_READ)) {
545 new_offset = zbd_zone_end(z);
546 if (new_offset >= f->file_offset + f->io_size) {
547 log_info("%s: io_size must be at least one zone\n",
548 f->file_name);
549 return false;
550 }
551 log_info("%s: rounded up offset from %"PRIu64" to %"PRIu64"\n",
552 f->file_name, f->file_offset,
553 new_offset);
554 f->io_size -= (new_offset - f->file_offset);
555 f->file_offset = new_offset;
556 }
557
558 zone_idx = zbd_zone_idx(f, f->file_offset + f->io_size);
559 z = get_zone(f, zone_idx);
560 new_end = z->start;
561 if ((td->o.td_ddir != TD_DDIR_READ) &&
562 (f->file_offset + f->io_size != new_end)) {
563 if (new_end <= f->file_offset) {
564 log_info("%s: io_size must be at least one zone\n",
565 f->file_name);
566 return false;
567 }
568 log_info("%s: rounded down io_size from %"PRIu64" to %"PRIu64"\n",
569 f->file_name, f->io_size,
570 new_end - f->file_offset);
571 f->io_size = new_end - f->file_offset;
572 }
573
574 return true;
575}
576
bfbdd35b
BVA
577/*
578 * Verify whether offset and size parameters are aligned with zone boundaries.
579 */
580static bool zbd_verify_sizes(void)
581{
bfbdd35b
BVA
582 struct thread_data *td;
583 struct fio_file *f;
bfbdd35b
BVA
584 int i, j;
585
586 for_each_td(td, i) {
587 for_each_file(td, f, j) {
0bf93a1a 588 if (!zbd_zone_align_file_sizes(td, f))
4d37720a 589 return false;
bfbdd35b
BVA
590 }
591 }
592
593 return true;
594}
595
596static bool zbd_verify_bs(void)
597{
598 struct thread_data *td;
599 struct fio_file *f;
bfbdd35b
BVA
600 int i, j, k;
601
602 for_each_td(td, i) {
e3be810b
SK
603 if (td_trim(td) &&
604 (td->o.min_bs[DDIR_TRIM] != td->o.max_bs[DDIR_TRIM] ||
605 td->o.bssplit_nr[DDIR_TRIM])) {
606 log_info("bsrange and bssplit are not allowed for trim with zonemode=zbd\n");
607 return false;
608 }
bfbdd35b 609 for_each_file(td, f, j) {
1ddd225e
AD
610 uint64_t zone_size;
611
bfbdd35b
BVA
612 if (!f->zbd_info)
613 continue;
139d8dc6 614
bfbdd35b 615 zone_size = f->zbd_info->zone_size;
e3be810b 616 if (td_trim(td) && td->o.bs[DDIR_TRIM] != zone_size) {
ee5e3436 617 log_info("%s: trim block size %llu is not the zone size %"PRIu64"\n",
e3be810b 618 f->file_name, td->o.bs[DDIR_TRIM],
ee5e3436 619 zone_size);
e3be810b
SK
620 return false;
621 }
59f94d26 622 for (k = 0; k < FIO_ARRAY_SIZE(td->o.bs); k++) {
bfbdd35b 623 if (td->o.verify != VERIFY_NONE &&
ee3696bd 624 zone_size % td->o.bs[k] != 0) {
ee5e3436 625 log_info("%s: block size %llu is not a divisor of the zone size %"PRIu64"\n",
bfbdd35b 626 f->file_name, td->o.bs[k],
ee5e3436 627 zone_size);
bfbdd35b
BVA
628 return false;
629 }
630 }
631 }
632 }
633 return true;
634}
635
bfbdd35b
BVA
636static int ilog2(uint64_t i)
637{
638 int log = -1;
639
640 while (i) {
641 i >>= 1;
642 log++;
643 }
644 return log;
645}
646
647/*
648 * Initialize f->zbd_info for devices that are not zoned block devices. This
649 * allows to execute a ZBD workload against a non-ZBD device.
650 */
651static int init_zone_info(struct thread_data *td, struct fio_file *f)
652{
653 uint32_t nr_zones;
654 struct fio_zone_info *p;
a4b7f12b 655 uint64_t zone_size = td->o.zone_size;
b8dd9750 656 uint64_t zone_capacity = td->o.zone_capacity;
bfbdd35b 657 struct zoned_block_device_info *zbd_info = NULL;
bfbdd35b
BVA
658 int i;
659
a4b7f12b 660 if (zone_size == 0) {
9db0cde8 661 log_err("%s: Specifying the zone size is mandatory for regular file/block device with --zonemode=zbd\n\n",
a4b7f12b
DLM
662 f->file_name);
663 return 1;
664 }
665
666 if (zone_size < 512) {
667 log_err("%s: zone size must be at least 512 bytes for --zonemode=zbd\n\n",
668 f->file_name);
669 return 1;
670 }
671
b8dd9750
HH
672 if (zone_capacity == 0)
673 zone_capacity = zone_size;
674
675 if (zone_capacity > zone_size) {
676 log_err("%s: job parameter zonecapacity %llu is larger than zone size %llu\n",
ee5e3436 677 f->file_name, td->o.zone_capacity, td->o.zone_size);
b8dd9750
HH
678 return 1;
679 }
680
9db0cde8
NC
681 if (f->real_file_size < zone_size) {
682 log_err("%s: file/device size %"PRIu64" is smaller than zone size %"PRIu64"\n",
683 f->file_name, f->real_file_size, zone_size);
684 return -EINVAL;
685 }
686
ee3696bd 687 nr_zones = (f->real_file_size + zone_size - 1) / zone_size;
bfbdd35b
BVA
688 zbd_info = scalloc(1, sizeof(*zbd_info) +
689 (nr_zones + 1) * sizeof(zbd_info->zone_info[0]));
690 if (!zbd_info)
691 return -ENOMEM;
692
44ec32cb 693 mutex_init_pshared(&zbd_info->mutex);
bfbdd35b
BVA
694 zbd_info->refcount = 1;
695 p = &zbd_info->zone_info[0];
696 for (i = 0; i < nr_zones; i++, p++) {
44ec32cb
SK
697 mutex_init_pshared_with_type(&p->mutex,
698 PTHREAD_MUTEX_RECURSIVE);
bfbdd35b 699 p->start = i * zone_size;
b14651a2 700 p->wp = p->start;
b7694961
DLM
701 p->type = ZBD_ZONE_TYPE_SWR;
702 p->cond = ZBD_ZONE_COND_EMPTY;
b8dd9750 703 p->capacity = zone_capacity;
be7a6bae 704 p->has_wp = 1;
bfbdd35b
BVA
705 }
706 /* a sentinel */
707 p->start = nr_zones * zone_size;
708
709 f->zbd_info = zbd_info;
710 f->zbd_info->zone_size = zone_size;
711 f->zbd_info->zone_size_log2 = is_power_of_2(zone_size) ?
ebc403fe 712 ilog2(zone_size) : 0;
bfbdd35b 713 f->zbd_info->nr_zones = nr_zones;
bfbdd35b
BVA
714 return 0;
715}
716
717/*
b7694961
DLM
718 * Maximum number of zones to report in one operation.
719 */
720#define ZBD_REPORT_MAX_ZONES 8192U
721
722/*
723 * Parse the device zone report and store it in f->zbd_info. Must be called
724 * only for devices that are zoned, namely those with a model != ZBD_NONE.
bfbdd35b
BVA
725 */
726static int parse_zone_info(struct thread_data *td, struct fio_file *f)
727{
b7694961
DLM
728 int nr_zones, nrz;
729 struct zbd_zone *zones, *z;
bfbdd35b 730 struct fio_zone_info *p;
b7694961 731 uint64_t zone_size, offset;
bfbdd35b 732 struct zoned_block_device_info *zbd_info = NULL;
d060babc 733 int i, j, ret = -ENOMEM;
bfbdd35b 734
b7694961
DLM
735 zones = calloc(ZBD_REPORT_MAX_ZONES, sizeof(struct zbd_zone));
736 if (!zones)
bfbdd35b
BVA
737 goto out;
738
b7694961
DLM
739 nrz = zbd_report_zones(td, f, 0, zones, ZBD_REPORT_MAX_ZONES);
740 if (nrz < 0) {
741 ret = nrz;
742 log_info("fio: report zones (offset 0) failed for %s (%d).\n",
743 f->file_name, -ret);
744 goto out;
bfbdd35b
BVA
745 }
746
b7694961 747 zone_size = zones[0].len;
ee3696bd 748 nr_zones = (f->real_file_size + zone_size - 1) / zone_size;
bfbdd35b
BVA
749
750 if (td->o.zone_size == 0) {
ee3696bd
DLM
751 td->o.zone_size = zone_size;
752 } else if (td->o.zone_size != zone_size) {
ee5e3436
SK
753 log_err("fio: %s job parameter zonesize %llu does not match disk zone size %"PRIu64".\n",
754 f->file_name, td->o.zone_size, zone_size);
bfbdd35b 755 ret = -EINVAL;
b7694961 756 goto out;
bfbdd35b
BVA
757 }
758
139d8dc6
DLM
759 dprint(FD_ZBD, "Device %s has %d zones of size %"PRIu64" KB\n",
760 f->file_name, nr_zones, zone_size / 1024);
bfbdd35b
BVA
761
762 zbd_info = scalloc(1, sizeof(*zbd_info) +
763 (nr_zones + 1) * sizeof(zbd_info->zone_info[0]));
bfbdd35b 764 if (!zbd_info)
b7694961 765 goto out;
44ec32cb 766 mutex_init_pshared(&zbd_info->mutex);
bfbdd35b
BVA
767 zbd_info->refcount = 1;
768 p = &zbd_info->zone_info[0];
b7694961
DLM
769 for (offset = 0, j = 0; j < nr_zones;) {
770 z = &zones[0];
771 for (i = 0; i < nrz; i++, j++, z++, p++) {
44ec32cb
SK
772 mutex_init_pshared_with_type(&p->mutex,
773 PTHREAD_MUTEX_RECURSIVE);
b7694961 774 p->start = z->start;
236d23a8 775 p->capacity = z->capacity;
139d8dc6 776
bfbdd35b 777 switch (z->cond) {
b7694961
DLM
778 case ZBD_ZONE_COND_NOT_WP:
779 case ZBD_ZONE_COND_FULL:
236d23a8 780 p->wp = p->start + p->capacity;
bfbdd35b
BVA
781 break;
782 default:
783 assert(z->start <= z->wp);
b7694961
DLM
784 assert(z->wp <= z->start + zone_size);
785 p->wp = z->wp;
bfbdd35b
BVA
786 break;
787 }
be7a6bae
DF
788
789 switch (z->type) {
790 case ZBD_ZONE_TYPE_SWR:
791 p->has_wp = 1;
792 break;
793 default:
794 p->has_wp = 0;
795 }
bfbdd35b
BVA
796 p->type = z->type;
797 p->cond = z->cond;
be7a6bae 798
bfbdd35b
BVA
799 if (j > 0 && p->start != p[-1].start + zone_size) {
800 log_info("%s: invalid zone data\n",
801 f->file_name);
802 ret = -EINVAL;
b7694961 803 goto out;
bfbdd35b
BVA
804 }
805 }
806 z--;
b7694961 807 offset = z->start + z->len;
bfbdd35b
BVA
808 if (j >= nr_zones)
809 break;
139d8dc6 810
6c3f1cc1
DF
811 nrz = zbd_report_zones(td, f, offset, zones,
812 min((uint32_t)(nr_zones - j),
813 ZBD_REPORT_MAX_ZONES));
b7694961
DLM
814 if (nrz < 0) {
815 ret = nrz;
ee5e3436
SK
816 log_info("fio: report zones (offset %"PRIu64") failed for %s (%d).\n",
817 offset, f->file_name, -ret);
b7694961 818 goto out;
bfbdd35b
BVA
819 }
820 }
b7694961 821
bfbdd35b 822 /* a sentinel */
b7694961 823 zbd_info->zone_info[nr_zones].start = offset;
bfbdd35b
BVA
824
825 f->zbd_info = zbd_info;
826 f->zbd_info->zone_size = zone_size;
827 f->zbd_info->zone_size_log2 = is_power_of_2(zone_size) ?
ebc403fe 828 ilog2(zone_size) : 0;
bfbdd35b
BVA
829 f->zbd_info->nr_zones = nr_zones;
830 zbd_info = NULL;
831 ret = 0;
832
bfbdd35b 833out:
b7694961
DLM
834 sfree(zbd_info);
835 free(zones);
bfbdd35b
BVA
836 return ret;
837}
838
d2f442bc
NC
839static int zbd_set_max_open_zones(struct thread_data *td, struct fio_file *f)
840{
841 struct zoned_block_device_info *zbd = f->zbd_info;
842 unsigned int max_open_zones;
843 int ret;
844
575686bb 845 if (zbd->model != ZBD_HOST_MANAGED || td->o.ignore_zone_limits) {
d2f442bc
NC
846 /* Only host-managed devices have a max open limit */
847 zbd->max_open_zones = td->o.max_open_zones;
848 goto out;
849 }
850
851 /* If host-managed, get the max open limit */
852 ret = zbd_get_max_open_zones(td, f, &max_open_zones);
853 if (ret)
854 return ret;
855
856 if (!max_open_zones) {
857 /* No device limit */
858 zbd->max_open_zones = td->o.max_open_zones;
859 } else if (!td->o.max_open_zones) {
860 /* No user limit. Set limit to device limit */
861 zbd->max_open_zones = max_open_zones;
862 } else if (td->o.max_open_zones <= max_open_zones) {
863 /* Both user limit and dev limit. User limit not too large */
864 zbd->max_open_zones = td->o.max_open_zones;
865 } else {
866 /* Both user limit and dev limit. User limit too large */
867 td_verror(td, EINVAL,
868 "Specified --max_open_zones is too large");
869 log_err("Specified --max_open_zones (%d) is larger than max (%u)\n",
870 td->o.max_open_zones, max_open_zones);
871 return -EINVAL;
872 }
873
874out:
875 /* Ensure that the limit is not larger than FIO's internal limit */
b346af90
NC
876 if (zbd->max_open_zones > ZBD_MAX_OPEN_ZONES) {
877 td_verror(td, EINVAL, "'max_open_zones' value is too large");
139d8dc6
DLM
878 log_err("'max_open_zones' value is larger than %u\n",
879 ZBD_MAX_OPEN_ZONES);
b346af90
NC
880 return -EINVAL;
881 }
882
d2f442bc
NC
883 dprint(FD_ZBD, "%s: using max open zones limit: %"PRIu32"\n",
884 f->file_name, zbd->max_open_zones);
885
886 return 0;
887}
888
bfbdd35b
BVA
889/*
890 * Allocate zone information and store it into f->zbd_info if zonemode=zbd.
891 *
892 * Returns 0 upon success and a negative error code upon failure.
893 */
379e5f09 894static int zbd_create_zone_info(struct thread_data *td, struct fio_file *f)
bfbdd35b 895{
b7694961
DLM
896 enum zbd_zoned_model zbd_model;
897 int ret;
bfbdd35b
BVA
898
899 assert(td->o.zone_mode == ZONE_MODE_ZBD);
900
b7694961
DLM
901 ret = zbd_get_zoned_model(td, f, &zbd_model);
902 if (ret)
903 return ret;
904
bfbdd35b 905 switch (zbd_model) {
b7694961
DLM
906 case ZBD_HOST_AWARE:
907 case ZBD_HOST_MANAGED:
bfbdd35b 908 ret = parse_zone_info(td, f);
d2f442bc
NC
909 if (ret)
910 return ret;
bfbdd35b 911 break;
b7694961 912 case ZBD_NONE:
bfbdd35b 913 ret = init_zone_info(td, f);
d2f442bc
NC
914 if (ret)
915 return ret;
bfbdd35b 916 break;
b7694961
DLM
917 default:
918 td_verror(td, EINVAL, "Unsupported zoned model");
919 log_err("Unsupported zoned model\n");
920 return -EINVAL;
bfbdd35b 921 }
b7694961 922
2c7dd23e 923 assert(f->zbd_info);
d2f442bc
NC
924 f->zbd_info->model = zbd_model;
925
926 ret = zbd_set_max_open_zones(td, f);
927 if (ret) {
928 zbd_free_zone_info(f);
929 return ret;
219c662d 930 }
d2f442bc
NC
931
932 return 0;
bfbdd35b
BVA
933}
934
935void zbd_free_zone_info(struct fio_file *f)
936{
937 uint32_t refcount;
938
3c1dc34c 939 assert(f->zbd_info);
bfbdd35b
BVA
940
941 pthread_mutex_lock(&f->zbd_info->mutex);
942 refcount = --f->zbd_info->refcount;
943 pthread_mutex_unlock(&f->zbd_info->mutex);
944
945 assert((int32_t)refcount >= 0);
946 if (refcount == 0)
947 sfree(f->zbd_info);
948 f->zbd_info = NULL;
949}
950
951/*
952 * Initialize f->zbd_info.
953 *
954 * Returns 0 upon success and a negative error code upon failure.
955 *
956 * Note: this function can only work correctly if it is called before the first
957 * fio fork() call.
958 */
959static int zbd_init_zone_info(struct thread_data *td, struct fio_file *file)
960{
961 struct thread_data *td2;
962 struct fio_file *f2;
963 int i, j, ret;
964
965 for_each_td(td2, i) {
966 for_each_file(td2, f2, j) {
967 if (td2 == td && f2 == file)
968 continue;
969 if (!f2->zbd_info ||
970 strcmp(f2->file_name, file->file_name) != 0)
971 continue;
972 file->zbd_info = f2->zbd_info;
973 file->zbd_info->refcount++;
974 return 0;
975 }
976 }
977
978 ret = zbd_create_zone_info(td, file);
979 if (ret < 0)
c5837eec 980 td_verror(td, -ret, "zbd_create_zone_info() failed");
139d8dc6 981
bfbdd35b
BVA
982 return ret;
983}
984
8f39afa7 985int zbd_init_files(struct thread_data *td)
bfbdd35b
BVA
986{
987 struct fio_file *f;
988 int i;
989
990 for_each_file(td, f, i) {
a4b7f12b 991 if (zbd_init_zone_info(td, f))
bfbdd35b 992 return 1;
bfbdd35b 993 }
139d8dc6 994
8f39afa7
AD
995 return 0;
996}
997
998void zbd_recalc_options_with_zone_granularity(struct thread_data *td)
999{
1000 struct fio_file *f;
1001 int i;
1002
1003 for_each_file(td, f, i) {
1004 struct zoned_block_device_info *zbd = f->zbd_info;
139d8dc6 1005 uint64_t zone_size;
8f39afa7 1006
139d8dc6
DLM
1007 /* zonemode=strided doesn't get per-file zone size. */
1008 zone_size = zbd ? zbd->zone_size : td->o.zone_size;
8f39afa7
AD
1009 if (zone_size == 0)
1010 continue;
1011
139d8dc6 1012 if (td->o.size_nz > 0)
8f39afa7 1013 td->o.size = td->o.size_nz * zone_size;
139d8dc6 1014 if (td->o.io_size_nz > 0)
8f39afa7 1015 td->o.io_size = td->o.io_size_nz * zone_size;
139d8dc6 1016 if (td->o.start_offset_nz > 0)
8f39afa7 1017 td->o.start_offset = td->o.start_offset_nz * zone_size;
139d8dc6
DLM
1018 if (td->o.offset_increment_nz > 0)
1019 td->o.offset_increment =
1020 td->o.offset_increment_nz * zone_size;
1021 if (td->o.zone_skip_nz > 0)
8f39afa7 1022 td->o.zone_skip = td->o.zone_skip_nz * zone_size;
8f39afa7
AD
1023 }
1024}
1025
1026int zbd_setup_files(struct thread_data *td)
1027{
1028 struct fio_file *f;
1029 int i;
bfbdd35b
BVA
1030
1031 if (!zbd_using_direct_io()) {
1032 log_err("Using direct I/O is mandatory for writing to ZBD drives\n\n");
1033 return 1;
1034 }
1035
1036 if (!zbd_verify_sizes())
1037 return 1;
1038
1039 if (!zbd_verify_bs())
1040 return 1;
1041
219c662d
AD
1042 for_each_file(td, f, i) {
1043 struct zoned_block_device_info *zbd = f->zbd_info;
954217b9
SK
1044 struct fio_zone_info *z;
1045 int zi;
219c662d 1046
5ddf46d0 1047 assert(zbd);
219c662d 1048
f952800a
SK
1049 f->min_zone = zbd_zone_idx(f, f->file_offset);
1050 f->max_zone = zbd_zone_idx(f, f->file_offset + f->io_size);
1051
1052 /*
1053 * When all zones in the I/O range are conventional, io_size
1054 * can be smaller than zone size, making min_zone the same
1055 * as max_zone. This is why the assert below needs to be made
1056 * conditional.
1057 */
1058 if (zbd_is_seq_job(f))
1059 assert(f->min_zone < f->max_zone);
1060
219c662d
AD
1061 if (td->o.max_open_zones > 0 &&
1062 zbd->max_open_zones != td->o.max_open_zones) {
1063 log_err("Different 'max_open_zones' values\n");
1064 return 1;
1065 }
b346af90
NC
1066
1067 /*
1068 * The per job max open zones limit cannot be used without a
1069 * global max open zones limit. (As the tracking of open zones
1070 * is disabled when there is no global max open zones limit.)
1071 */
1072 if (td->o.job_max_open_zones && !zbd->max_open_zones) {
1073 log_err("'job_max_open_zones' cannot be used without a global open zones limit\n");
219c662d
AD
1074 return 1;
1075 }
954217b9 1076
ea51055c
NC
1077 /*
1078 * zbd->max_open_zones is the global limit shared for all jobs
1079 * that target the same zoned block device. Force sync the per
1080 * thread global limit with the actual global limit. (The real
1081 * per thread/job limit is stored in td->o.job_max_open_zones).
1082 */
1083 td->o.max_open_zones = zbd->max_open_zones;
1084
954217b9
SK
1085 for (zi = f->min_zone; zi < f->max_zone; zi++) {
1086 z = &zbd->zone_info[zi];
1087 if (z->cond != ZBD_ZONE_COND_IMP_OPEN &&
1088 z->cond != ZBD_ZONE_COND_EXP_OPEN)
1089 continue;
1090 if (zbd_open_zone(td, f, zi))
1091 continue;
1092 /*
1093 * If the number of open zones exceeds specified limits,
1094 * reset all extra open zones.
1095 */
1096 if (zbd_reset_zone(td, f, z) < 0) {
1097 log_err("Failed to reest zone %d\n", zi);
1098 return 1;
1099 }
1100 }
219c662d
AD
1101 }
1102
bfbdd35b
BVA
1103 return 0;
1104}
1105
a7c2b6fc
BVA
1106/*
1107 * Reset zbd_info.write_cnt, the counter that counts down towards the next
1108 * zone reset.
1109 */
1bb1bcad
AD
1110static void _zbd_reset_write_cnt(const struct thread_data *td,
1111 const struct fio_file *f)
a7c2b6fc
BVA
1112{
1113 assert(0 <= td->o.zrf.u.f && td->o.zrf.u.f <= 1);
1114
a7c2b6fc
BVA
1115 f->zbd_info->write_cnt = td->o.zrf.u.f ?
1116 min(1.0 / td->o.zrf.u.f, 0.0 + UINT_MAX) : UINT_MAX;
1bb1bcad
AD
1117}
1118
1119static void zbd_reset_write_cnt(const struct thread_data *td,
1120 const struct fio_file *f)
1121{
1122 pthread_mutex_lock(&f->zbd_info->mutex);
1123 _zbd_reset_write_cnt(td, f);
a7c2b6fc
BVA
1124 pthread_mutex_unlock(&f->zbd_info->mutex);
1125}
1126
1127static bool zbd_dec_and_reset_write_cnt(const struct thread_data *td,
1128 const struct fio_file *f)
1129{
1130 uint32_t write_cnt = 0;
1131
1132 pthread_mutex_lock(&f->zbd_info->mutex);
1133 assert(f->zbd_info->write_cnt);
1134 if (f->zbd_info->write_cnt)
1135 write_cnt = --f->zbd_info->write_cnt;
1136 if (write_cnt == 0)
1bb1bcad 1137 _zbd_reset_write_cnt(td, f);
a7c2b6fc
BVA
1138 pthread_mutex_unlock(&f->zbd_info->mutex);
1139
1140 return write_cnt == 0;
1141}
1142
91d25131
BVA
1143enum swd_action {
1144 CHECK_SWD,
1145 SET_SWD,
1146};
1147
1148/* Calculate the number of sectors with data (swd) and perform action 'a' */
11afb212
DF
1149static uint64_t zbd_process_swd(struct thread_data *td,
1150 const struct fio_file *f, enum swd_action a)
615555bb 1151{
615555bb
BVA
1152 struct fio_zone_info *zb, *ze, *z;
1153 uint64_t swd = 0;
403ea795 1154 uint64_t wp_swd = 0;
615555bb 1155
923f7c1e
DF
1156 zb = get_zone(f, f->min_zone);
1157 ze = get_zone(f, f->max_zone);
615555bb 1158 for (z = zb; z < ze; z++) {
403ea795 1159 if (z->has_wp) {
11afb212 1160 zone_lock(td, f, z);
403ea795
SK
1161 wp_swd += z->wp - z->start;
1162 }
615555bb
BVA
1163 swd += z->wp - z->start;
1164 }
139d8dc6 1165
615555bb 1166 pthread_mutex_lock(&f->zbd_info->mutex);
91d25131
BVA
1167 switch (a) {
1168 case CHECK_SWD:
1169 assert(f->zbd_info->sectors_with_data == swd);
403ea795 1170 assert(f->zbd_info->wp_sectors_with_data == wp_swd);
91d25131
BVA
1171 break;
1172 case SET_SWD:
1173 f->zbd_info->sectors_with_data = swd;
403ea795 1174 f->zbd_info->wp_sectors_with_data = wp_swd;
91d25131
BVA
1175 break;
1176 }
615555bb 1177 pthread_mutex_unlock(&f->zbd_info->mutex);
139d8dc6 1178
615555bb 1179 for (z = zb; z < ze; z++)
403ea795
SK
1180 if (z->has_wp)
1181 zone_unlock(z);
91d25131
BVA
1182
1183 return swd;
1184}
1185
1186/*
1187 * The swd check is useful for debugging but takes too much time to leave
1188 * it enabled all the time. Hence it is disabled by default.
1189 */
1190static const bool enable_check_swd = false;
1191
403ea795 1192/* Check whether the values of zbd_info.*sectors_with_data are correct. */
11afb212 1193static void zbd_check_swd(struct thread_data *td, const struct fio_file *f)
91d25131
BVA
1194{
1195 if (!enable_check_swd)
1196 return;
1197
11afb212 1198 zbd_process_swd(td, f, CHECK_SWD);
615555bb
BVA
1199}
1200
bfbdd35b
BVA
1201void zbd_file_reset(struct thread_data *td, struct fio_file *f)
1202{
91d25131 1203 struct fio_zone_info *zb, *ze;
11afb212 1204 uint64_t swd;
bfbdd35b 1205
767d1372 1206 if (!f->zbd_info || !td_write(td))
bfbdd35b
BVA
1207 return;
1208
923f7c1e
DF
1209 zb = get_zone(f, f->min_zone);
1210 ze = get_zone(f, f->max_zone);
11afb212 1211 swd = zbd_process_swd(td, f, SET_SWD);
139d8dc6
DLM
1212
1213 dprint(FD_ZBD, "%s(%s): swd = %" PRIu64 "\n",
1214 __func__, f->file_name, swd);
1215
bfbdd35b
BVA
1216 /*
1217 * If data verification is enabled reset the affected zones before
1218 * writing any data to avoid that a zone reset has to be issued while
1219 * writing data, which causes data loss.
1220 */
57f88219
SK
1221 if (td->o.verify != VERIFY_NONE && td->runstate != TD_VERIFYING)
1222 zbd_reset_zones(td, f, zb, ze);
a7c2b6fc 1223 zbd_reset_write_cnt(td, f);
bfbdd35b
BVA
1224}
1225
cb765e41 1226/* Return random zone index for one of the open zones. */
6463db6c
AD
1227static uint32_t pick_random_zone_idx(const struct fio_file *f,
1228 const struct io_u *io_u)
1229{
139d8dc6
DLM
1230 return (io_u->offset - f->file_offset) *
1231 f->zbd_info->num_open_zones / f->io_size;
6463db6c
AD
1232}
1233
0f77c977
SK
1234static bool any_io_in_flight(void)
1235{
1236 struct thread_data *td;
1237 int i;
1238
1239 for_each_td(td, i) {
1240 if (td->io_u_in_flight)
1241 return true;
1242 }
1243
1244 return false;
1245}
1246
59b07544
BVA
1247/*
1248 * Modify the offset of an I/O unit that does not refer to an open zone such
1249 * that it refers to an open zone. Close an open zone and open a new zone if
21c0c884
SK
1250 * necessary. The open zone is searched across sequential zones.
1251 * This algorithm can only work correctly if all write pointers are
59b07544
BVA
1252 * a multiple of the fio block size. The caller must neither hold z->mutex
1253 * nor f->zbd_info->mutex. Returns with z->mutex held upon success.
1254 */
379e5f09
BVA
1255static struct fio_zone_info *zbd_convert_to_open_zone(struct thread_data *td,
1256 struct io_u *io_u)
59b07544 1257{
07fc3f57 1258 const uint64_t min_bs = td->o.min_bs[io_u->ddir];
fae3b9a0 1259 struct fio_file *f = io_u->file;
af94a8c3 1260 struct zoned_block_device_info *zbdi = f->zbd_info;
59b07544
BVA
1261 struct fio_zone_info *z;
1262 unsigned int open_zone_idx = -1;
1263 uint32_t zone_idx, new_zone_idx;
1264 int i;
b2da58c4 1265 bool wait_zone_close;
0f77c977
SK
1266 bool in_flight;
1267 bool should_retry = true;
59b07544
BVA
1268
1269 assert(is_valid_offset(f, io_u->offset));
1270
ea51055c 1271 if (zbdi->max_open_zones || td->o.job_max_open_zones) {
59b07544 1272 /*
af94a8c3 1273 * This statement accesses zbdi->open_zones[] on purpose
59b07544
BVA
1274 * without locking.
1275 */
af94a8c3 1276 zone_idx = zbdi->open_zones[pick_random_zone_idx(f, io_u)];
59b07544
BVA
1277 } else {
1278 zone_idx = zbd_zone_idx(f, io_u->offset);
1279 }
fae3b9a0
AD
1280 if (zone_idx < f->min_zone)
1281 zone_idx = f->min_zone;
1282 else if (zone_idx >= f->max_zone)
1283 zone_idx = f->max_zone - 1;
139d8dc6
DLM
1284
1285 dprint(FD_ZBD,
1286 "%s(%s): starting from zone %d (offset %lld, buflen %lld)\n",
59b07544
BVA
1287 __func__, f->file_name, zone_idx, io_u->offset, io_u->buflen);
1288
1289 /*
af94a8c3 1290 * Since z->mutex is the outer lock and zbdi->mutex the inner
59b07544 1291 * lock it can happen that the state of the zone with index zone_idx
af94a8c3 1292 * has changed after 'z' has been assigned and before zbdi->mutex
59b07544
BVA
1293 * has been obtained. Hence the loop.
1294 */
1295 for (;;) {
6463db6c
AD
1296 uint32_t tmp_idx;
1297
923f7c1e 1298 z = get_zone(f, zone_idx);
14351148
DF
1299 if (z->has_wp)
1300 zone_lock(td, f, z);
139d8dc6 1301
af94a8c3 1302 pthread_mutex_lock(&zbdi->mutex);
139d8dc6 1303
14351148
DF
1304 if (z->has_wp) {
1305 if (z->cond != ZBD_ZONE_COND_OFFLINE &&
139d8dc6
DLM
1306 zbdi->max_open_zones == 0 &&
1307 td->o.job_max_open_zones == 0)
14351148 1308 goto examine_zone;
af94a8c3 1309 if (zbdi->num_open_zones == 0) {
14351148
DF
1310 dprint(FD_ZBD, "%s(%s): no zones are open\n",
1311 __func__, f->file_name);
1312 goto open_other_zone;
1313 }
59b07544 1314 }
6463db6c
AD
1315
1316 /*
139d8dc6
DLM
1317 * List of opened zones is per-device, shared across all
1318 * threads. Start with quasi-random candidate zone. Ignore
1319 * zones which don't belong to thread's offset/size area.
6463db6c
AD
1320 */
1321 open_zone_idx = pick_random_zone_idx(f, io_u);
1a8d510a 1322 assert(!open_zone_idx ||
af94a8c3 1323 open_zone_idx < zbdi->num_open_zones);
6463db6c 1324 tmp_idx = open_zone_idx;
139d8dc6 1325
af94a8c3 1326 for (i = 0; i < zbdi->num_open_zones; i++) {
6463db6c
AD
1327 uint32_t tmpz;
1328
af94a8c3 1329 if (tmp_idx >= zbdi->num_open_zones)
6463db6c 1330 tmp_idx = 0;
af94a8c3 1331 tmpz = zbdi->open_zones[tmp_idx];
fae3b9a0 1332 if (f->min_zone <= tmpz && tmpz < f->max_zone) {
6463db6c
AD
1333 open_zone_idx = tmp_idx;
1334 goto found_candidate_zone;
1335 }
1336
1337 tmp_idx++;
1338 }
1339
1340 dprint(FD_ZBD, "%s(%s): no candidate zone\n",
1341 __func__, f->file_name);
139d8dc6 1342
af94a8c3 1343 pthread_mutex_unlock(&zbdi->mutex);
139d8dc6 1344
14351148
DF
1345 if (z->has_wp)
1346 zone_unlock(z);
139d8dc6 1347
6463db6c
AD
1348 return NULL;
1349
1350found_candidate_zone:
af94a8c3 1351 new_zone_idx = zbdi->open_zones[open_zone_idx];
59b07544
BVA
1352 if (new_zone_idx == zone_idx)
1353 break;
1354 zone_idx = new_zone_idx;
139d8dc6 1355
af94a8c3 1356 pthread_mutex_unlock(&zbdi->mutex);
139d8dc6 1357
14351148
DF
1358 if (z->has_wp)
1359 zone_unlock(z);
59b07544
BVA
1360 }
1361
af94a8c3 1362 /* Both z->mutex and zbdi->mutex are held. */
59b07544
BVA
1363
1364examine_zone:
236d23a8 1365 if (z->wp + min_bs <= zbd_zone_capacity_end(z)) {
af94a8c3 1366 pthread_mutex_unlock(&zbdi->mutex);
59b07544
BVA
1367 goto out;
1368 }
b2da58c4
SK
1369
1370open_other_zone:
1371 /* Check if number of open zones reaches one of limits. */
1372 wait_zone_close =
af94a8c3 1373 zbdi->num_open_zones == f->max_zone - f->min_zone ||
ea51055c
NC
1374 (zbdi->max_open_zones &&
1375 zbdi->num_open_zones == zbdi->max_open_zones) ||
b2da58c4
SK
1376 (td->o.job_max_open_zones &&
1377 td->num_open_zones == td->o.job_max_open_zones);
1378
af94a8c3 1379 pthread_mutex_unlock(&zbdi->mutex);
59b07544
BVA
1380
1381 /* Only z->mutex is held. */
1382
b2da58c4
SK
1383 /*
1384 * When number of open zones reaches to one of limits, wait for
1385 * zone close before opening a new zone.
1386 */
1387 if (wait_zone_close) {
139d8dc6
DLM
1388 dprint(FD_ZBD,
1389 "%s(%s): quiesce to allow open zones to close\n",
b2da58c4
SK
1390 __func__, f->file_name);
1391 io_u_quiesce(td);
1392 }
1393
0f77c977 1394retry:
59b07544 1395 /* Zone 'z' is full, so try to open a new zone. */
af94a8c3 1396 for (i = f->io_size / zbdi->zone_size; i > 0; i--) {
59b07544 1397 zone_idx++;
21c0c884
SK
1398 if (z->has_wp)
1399 zone_unlock(z);
59b07544 1400 z++;
ee3696bd 1401 if (!is_valid_offset(f, z->start)) {
59b07544 1402 /* Wrap-around. */
fae3b9a0 1403 zone_idx = f->min_zone;
923f7c1e 1404 z = get_zone(f, zone_idx);
59b07544 1405 }
ee3696bd 1406 assert(is_valid_offset(f, z->start));
21c0c884
SK
1407 if (!z->has_wp)
1408 continue;
fae3b9a0 1409 zone_lock(td, f, z);
59b07544
BVA
1410 if (z->open)
1411 continue;
954217b9 1412 if (zbd_open_zone(td, f, zone_idx))
59b07544
BVA
1413 goto out;
1414 }
1415
1416 /* Only z->mutex is held. */
1417
1418 /* Check whether the write fits in any of the already opened zones. */
af94a8c3
NC
1419 pthread_mutex_lock(&zbdi->mutex);
1420 for (i = 0; i < zbdi->num_open_zones; i++) {
1421 zone_idx = zbdi->open_zones[i];
fae3b9a0
AD
1422 if (zone_idx < f->min_zone || zone_idx >= f->max_zone)
1423 continue;
af94a8c3 1424 pthread_mutex_unlock(&zbdi->mutex);
4d4c71e6 1425 zone_unlock(z);
59b07544 1426
923f7c1e 1427 z = get_zone(f, zone_idx);
59b07544 1428
fae3b9a0 1429 zone_lock(td, f, z);
236d23a8 1430 if (z->wp + min_bs <= zbd_zone_capacity_end(z))
59b07544 1431 goto out;
af94a8c3 1432 pthread_mutex_lock(&zbdi->mutex);
59b07544 1433 }
0f77c977
SK
1434
1435 /*
1436 * When any I/O is in-flight or when all I/Os in-flight get completed,
1437 * the I/Os might have closed zones then retry the steps to open a zone.
1438 * Before retry, call io_u_quiesce() to complete in-flight writes.
1439 */
1440 in_flight = any_io_in_flight();
1441 if (in_flight || should_retry) {
139d8dc6
DLM
1442 dprint(FD_ZBD,
1443 "%s(%s): wait zone close and retry open zones\n",
0f77c977
SK
1444 __func__, f->file_name);
1445 pthread_mutex_unlock(&zbdi->mutex);
1446 zone_unlock(z);
1447 io_u_quiesce(td);
1448 zone_lock(td, f, z);
1449 should_retry = in_flight;
1450 goto retry;
1451 }
1452
af94a8c3 1453 pthread_mutex_unlock(&zbdi->mutex);
139d8dc6 1454
4d4c71e6 1455 zone_unlock(z);
139d8dc6
DLM
1456
1457 dprint(FD_ZBD, "%s(%s): did not open another zone\n",
1458 __func__, f->file_name);
1459
59b07544
BVA
1460 return NULL;
1461
1462out:
139d8dc6
DLM
1463 dprint(FD_ZBD, "%s(%s): returning zone %d\n",
1464 __func__, f->file_name, zone_idx);
1465
ee3696bd 1466 io_u->offset = z->start;
21c0c884 1467 assert(z->has_wp);
8a866de7 1468 assert(z->cond != ZBD_ZONE_COND_OFFLINE);
139d8dc6 1469
59b07544
BVA
1470 return z;
1471}
1472
bfbdd35b 1473/* The caller must hold z->mutex. */
59b07544
BVA
1474static struct fio_zone_info *zbd_replay_write_order(struct thread_data *td,
1475 struct io_u *io_u,
1476 struct fio_zone_info *z)
bfbdd35b
BVA
1477{
1478 const struct fio_file *f = io_u->file;
07fc3f57 1479 const uint64_t min_bs = td->o.min_bs[DDIR_WRITE];
bfbdd35b 1480
122c3539 1481 if (!zbd_open_zone(td, f, zbd_zone_nr(f, z))) {
4d4c71e6 1482 zone_unlock(z);
59b07544
BVA
1483 z = zbd_convert_to_open_zone(td, io_u);
1484 assert(z);
1485 }
1486
0cac10b4 1487 if (z->verify_block * min_bs >= z->capacity) {
139d8dc6
DLM
1488 log_err("%s: %d * %"PRIu64" >= %"PRIu64"\n",
1489 f->file_name, z->verify_block, min_bs, z->capacity);
0cac10b4
DF
1490 /*
1491 * If the assertion below fails during a test run, adding
1492 * "--experimental_verify=1" to the command line may help.
1493 */
1494 assert(false);
1495 }
139d8dc6 1496
0cac10b4
DF
1497 io_u->offset = z->start + z->verify_block * min_bs;
1498 if (io_u->offset + io_u->buflen >= zbd_zone_capacity_end(z)) {
139d8dc6
DLM
1499 log_err("%s: %llu + %llu >= %"PRIu64"\n",
1500 f->file_name, io_u->offset, io_u->buflen,
1501 zbd_zone_capacity_end(z));
0cac10b4
DF
1502 assert(false);
1503 }
1504 z->verify_block += io_u->buflen / min_bs;
1505
59b07544 1506 return z;
bfbdd35b
BVA
1507}
1508
1509/*
5c86fdf6
SK
1510 * Find another zone which has @min_bytes of readable data. Search in zones
1511 * @zb + 1 .. @zl. For random workload, also search in zones @zb - 1 .. @zf.
bfbdd35b 1512 *
21c0c884
SK
1513 * Either returns NULL or returns a zone pointer. When the zone has write
1514 * pointer, hold the mutex for the zone.
bfbdd35b
BVA
1515 */
1516static struct fio_zone_info *
07fc3f57 1517zbd_find_zone(struct thread_data *td, struct io_u *io_u, uint64_t min_bytes,
bfbdd35b
BVA
1518 struct fio_zone_info *zb, struct fio_zone_info *zl)
1519{
fae3b9a0 1520 struct fio_file *f = io_u->file;
bfbdd35b 1521 struct fio_zone_info *z1, *z2;
923f7c1e 1522 const struct fio_zone_info *const zf = get_zone(f, f->min_zone);
bfbdd35b
BVA
1523
1524 /*
1525 * Skip to the next non-empty zone in case of sequential I/O and to
1526 * the nearest non-empty zone in case of random I/O.
1527 */
1528 for (z1 = zb + 1, z2 = zb - 1; z1 < zl || z2 >= zf; z1++, z2--) {
b7694961 1529 if (z1 < zl && z1->cond != ZBD_ZONE_COND_OFFLINE) {
21c0c884
SK
1530 if (z1->has_wp)
1531 zone_lock(td, f, z1);
5c86fdf6 1532 if (z1->start + min_bytes <= z1->wp)
bfbdd35b 1533 return z1;
21c0c884
SK
1534 if (z1->has_wp)
1535 zone_unlock(z1);
bfbdd35b
BVA
1536 } else if (!td_random(td)) {
1537 break;
1538 }
139d8dc6 1539
bfbdd35b 1540 if (td_random(td) && z2 >= zf &&
b7694961 1541 z2->cond != ZBD_ZONE_COND_OFFLINE) {
21c0c884
SK
1542 if (z2->has_wp)
1543 zone_lock(td, f, z2);
5c86fdf6 1544 if (z2->start + min_bytes <= z2->wp)
bfbdd35b 1545 return z2;
21c0c884
SK
1546 if (z2->has_wp)
1547 zone_unlock(z2);
bfbdd35b
BVA
1548 }
1549 }
139d8dc6
DLM
1550
1551 dprint(FD_ZBD,
1552 "%s: no zone has %"PRIu64" bytes of readable data\n",
5c86fdf6 1553 f->file_name, min_bytes);
139d8dc6 1554
bfbdd35b
BVA
1555 return NULL;
1556}
1557
b2da58c4
SK
1558/**
1559 * zbd_end_zone_io - update zone status at command completion
1560 * @io_u: I/O unit
1561 * @z: zone info pointer
1562 *
1563 * If the write command made the zone full, close it.
1564 *
1565 * The caller must hold z->mutex.
1566 */
1567static void zbd_end_zone_io(struct thread_data *td, const struct io_u *io_u,
1568 struct fio_zone_info *z)
1569{
1570 const struct fio_file *f = io_u->file;
1571
1572 if (io_u->ddir == DDIR_WRITE &&
1573 io_u->offset + io_u->buflen >= zbd_zone_capacity_end(z)) {
1574 pthread_mutex_lock(&f->zbd_info->mutex);
a23411bb 1575 zbd_close_zone(td, f, z);
b2da58c4
SK
1576 pthread_mutex_unlock(&f->zbd_info->mutex);
1577 }
1578}
1579
bfbdd35b 1580/**
d9ed3e63 1581 * zbd_queue_io - update the write pointer of a sequential zone
bfbdd35b 1582 * @io_u: I/O unit
d9ed3e63
DLM
1583 * @success: Whether or not the I/O unit has been queued successfully
1584 * @q: queueing status (busy, completed or queued).
bfbdd35b 1585 *
d9ed3e63
DLM
1586 * For write and trim operations, update the write pointer of the I/O unit
1587 * target zone.
bfbdd35b 1588 */
b2da58c4
SK
1589static void zbd_queue_io(struct thread_data *td, struct io_u *io_u, int q,
1590 bool success)
bfbdd35b 1591{
d9ed3e63
DLM
1592 const struct fio_file *f = io_u->file;
1593 struct zoned_block_device_info *zbd_info = f->zbd_info;
bfbdd35b
BVA
1594 struct fio_zone_info *z;
1595 uint32_t zone_idx;
d9ed3e63 1596 uint64_t zone_end;
bfbdd35b 1597
5ddf46d0 1598 assert(zbd_info);
bfbdd35b 1599
d9ed3e63 1600 zone_idx = zbd_zone_idx(f, io_u->offset);
bfbdd35b 1601 assert(zone_idx < zbd_info->nr_zones);
923f7c1e 1602 z = get_zone(f, zone_idx);
d9ed3e63 1603
43bcbd5b 1604 assert(z->has_wp);
d9ed3e63 1605
bfbdd35b
BVA
1606 if (!success)
1607 goto unlock;
d9ed3e63
DLM
1608
1609 dprint(FD_ZBD,
1610 "%s: queued I/O (%lld, %llu) for zone %u\n",
1611 f->file_name, io_u->offset, io_u->buflen, zone_idx);
1612
bfbdd35b
BVA
1613 switch (io_u->ddir) {
1614 case DDIR_WRITE:
d9ed3e63 1615 zone_end = min((uint64_t)(io_u->offset + io_u->buflen),
236d23a8 1616 zbd_zone_capacity_end(z));
139d8dc6 1617
a7c2b6fc
BVA
1618 /*
1619 * z->wp > zone_end means that one or more I/O errors
1620 * have occurred.
1621 */
139d8dc6 1622 pthread_mutex_lock(&zbd_info->mutex);
403ea795 1623 if (z->wp <= zone_end) {
a7c2b6fc 1624 zbd_info->sectors_with_data += zone_end - z->wp;
403ea795
SK
1625 zbd_info->wp_sectors_with_data += zone_end - z->wp;
1626 }
a7c2b6fc 1627 pthread_mutex_unlock(&zbd_info->mutex);
bfbdd35b
BVA
1628 z->wp = zone_end;
1629 break;
bfbdd35b
BVA
1630 default:
1631 break;
1632 }
d9ed3e63 1633
b2da58c4
SK
1634 if (q == FIO_Q_COMPLETED && !io_u->error)
1635 zbd_end_zone_io(td, io_u, z);
1636
bfbdd35b 1637unlock:
d9ed3e63
DLM
1638 if (!success || q != FIO_Q_QUEUED) {
1639 /* BUSY or COMPLETED: unlock the zone */
4d4c71e6 1640 zone_unlock(z);
d9ed3e63
DLM
1641 io_u->zbd_put_io = NULL;
1642 }
1643}
1644
1645/**
1646 * zbd_put_io - Unlock an I/O unit target zone lock
1647 * @io_u: I/O unit
1648 */
b2da58c4 1649static void zbd_put_io(struct thread_data *td, const struct io_u *io_u)
d9ed3e63
DLM
1650{
1651 const struct fio_file *f = io_u->file;
1652 struct zoned_block_device_info *zbd_info = f->zbd_info;
1653 struct fio_zone_info *z;
1654 uint32_t zone_idx;
1655
5ddf46d0 1656 assert(zbd_info);
615555bb 1657
d9ed3e63
DLM
1658 zone_idx = zbd_zone_idx(f, io_u->offset);
1659 assert(zone_idx < zbd_info->nr_zones);
923f7c1e 1660 z = get_zone(f, zone_idx);
d9ed3e63 1661
43bcbd5b 1662 assert(z->has_wp);
d9ed3e63
DLM
1663
1664 dprint(FD_ZBD,
1665 "%s: terminate I/O (%lld, %llu) for zone %u\n",
1666 f->file_name, io_u->offset, io_u->buflen, zone_idx);
1667
b2da58c4
SK
1668 zbd_end_zone_io(td, io_u, z);
1669
4d4c71e6 1670 zone_unlock(z);
11afb212 1671 zbd_check_swd(td, f);
bfbdd35b
BVA
1672}
1673
9d87c646
DLM
1674/*
1675 * Windows and MacOS do not define this.
1676 */
1677#ifndef EREMOTEIO
1678#define EREMOTEIO 121 /* POSIX value */
1679#endif
1680
bfbdd35b
BVA
1681bool zbd_unaligned_write(int error_code)
1682{
1683 switch (error_code) {
1684 case EIO:
1685 case EREMOTEIO:
1686 return true;
1687 }
1688 return false;
1689}
1690
4d37720a
DLM
1691/**
1692 * setup_zbd_zone_mode - handle zoneskip as necessary for ZBD drives
1693 * @td: FIO thread data.
1694 * @io_u: FIO I/O unit.
1695 *
1696 * For sequential workloads, change the file offset to skip zoneskip bytes when
1697 * no more IO can be performed in the current zone.
1698 * - For read workloads, zoneskip is applied when the io has reached the end of
1699 * the zone or the zone write position (when td->o.read_beyond_wp is false).
1700 * - For write workloads, zoneskip is applied when the zone is full.
1701 * This applies only to read and write operations.
1702 */
1703void setup_zbd_zone_mode(struct thread_data *td, struct io_u *io_u)
1704{
1705 struct fio_file *f = io_u->file;
1706 enum fio_ddir ddir = io_u->ddir;
1707 struct fio_zone_info *z;
1708 uint32_t zone_idx;
1709
1710 assert(td->o.zone_mode == ZONE_MODE_ZBD);
1711 assert(td->o.zone_size);
5ddf46d0 1712 assert(f->zbd_info);
4d37720a 1713
236d23a8 1714 zone_idx = zbd_zone_idx(f, f->last_pos[ddir]);
923f7c1e 1715 z = get_zone(f, zone_idx);
236d23a8
SK
1716
1717 /*
1718 * When the zone capacity is smaller than the zone size and the I/O is
1719 * sequential write, skip to zone end if the latest position is at the
1720 * zone capacity limit.
1721 */
139d8dc6
DLM
1722 if (z->capacity < f->zbd_info->zone_size &&
1723 !td_random(td) && ddir == DDIR_WRITE &&
236d23a8
SK
1724 f->last_pos[ddir] >= zbd_zone_capacity_end(z)) {
1725 dprint(FD_ZBD,
1726 "%s: Jump from zone capacity limit to zone end:"
ee5e3436
SK
1727 " (%"PRIu64" -> %"PRIu64") for zone %u (%"PRIu64")\n",
1728 f->file_name, f->last_pos[ddir],
1729 zbd_zone_end(z), zone_idx, z->capacity);
236d23a8
SK
1730 td->io_skip_bytes += zbd_zone_end(z) - f->last_pos[ddir];
1731 f->last_pos[ddir] = zbd_zone_end(z);
1732 }
1733
4d37720a
DLM
1734 /*
1735 * zone_skip is valid only for sequential workloads.
1736 */
1737 if (td_random(td) || !td->o.zone_skip)
1738 return;
1739
1740 /*
1741 * It is time to switch to a new zone if:
1742 * - zone_bytes == zone_size bytes have already been accessed
1743 * - The last position reached the end of the current zone.
1744 * - For reads with td->o.read_beyond_wp == false, the last position
1745 * reached the zone write pointer.
1746 */
4d37720a 1747 if (td->zone_bytes >= td->o.zone_size ||
236d23a8 1748 f->last_pos[ddir] >= zbd_zone_end(z) ||
4d37720a
DLM
1749 (ddir == DDIR_READ &&
1750 (!td->o.read_beyond_wp) && f->last_pos[ddir] >= z->wp)) {
1751 /*
1752 * Skip zones.
1753 */
1754 td->zone_bytes = 0;
1755 f->file_offset += td->o.zone_size + td->o.zone_skip;
1756
1757 /*
1758 * Wrap from the beginning, if we exceed the file size
1759 */
1760 if (f->file_offset >= f->real_file_size)
1761 f->file_offset = get_start_offset(td, f);
1762
1763 f->last_pos[ddir] = f->file_offset;
1764 td->io_skip_bytes += td->o.zone_skip;
1765 }
1766}
1767
c65057f9 1768/**
c7d5e152 1769 * zbd_adjust_ddir - Adjust an I/O direction for zonemode=zbd.
c65057f9
SK
1770 *
1771 * @td: FIO thread data.
1772 * @io_u: FIO I/O unit.
1773 * @ddir: I/O direction before adjustment.
1774 *
1775 * Return adjusted I/O direction.
1776 */
1777enum fio_ddir zbd_adjust_ddir(struct thread_data *td, struct io_u *io_u,
1778 enum fio_ddir ddir)
1779{
1780 /*
1781 * In case read direction is chosen for the first random I/O, fio with
1782 * zonemode=zbd stops because no data can be read from zoned block
1783 * devices with all empty zones. Overwrite the first I/O direction as
1784 * write to make sure data to read exists.
1785 */
5ddf46d0 1786 assert(io_u->file->zbd_info);
731461cc 1787 if (ddir != DDIR_READ || !td_rw(td))
c65057f9
SK
1788 return ddir;
1789
1790 if (io_u->file->zbd_info->sectors_with_data ||
1791 td->o.read_beyond_wp)
1792 return DDIR_READ;
1793
1794 return DDIR_WRITE;
1795}
1796
bfbdd35b
BVA
1797/**
1798 * zbd_adjust_block - adjust the offset and length as necessary for ZBD drives
1799 * @td: FIO thread data.
1800 * @io_u: FIO I/O unit.
1801 *
1802 * Locking strategy: returns with z->mutex locked if and only if z refers
1803 * to a sequential zone and if io_u_accept is returned. z is the zone that
1804 * corresponds to io_u->offset at the end of this function.
1805 */
1806enum io_u_action zbd_adjust_block(struct thread_data *td, struct io_u *io_u)
1807{
b7694961 1808 struct fio_file *f = io_u->file;
af94a8c3 1809 struct zoned_block_device_info *zbdi = f->zbd_info;
bfbdd35b 1810 uint32_t zone_idx_b;
de65f7b7 1811 struct fio_zone_info *zb, *zl, *orig_zb;
bfbdd35b 1812 uint32_t orig_len = io_u->buflen;
07fc3f57 1813 uint64_t min_bs = td->o.min_bs[io_u->ddir];
bfbdd35b
BVA
1814 uint64_t new_len;
1815 int64_t range;
1816
af94a8c3 1817 assert(zbdi);
adc6adcb 1818 assert(min_bs);
bfbdd35b
BVA
1819 assert(is_valid_offset(f, io_u->offset));
1820 assert(io_u->buflen);
139d8dc6 1821
bfbdd35b 1822 zone_idx_b = zbd_zone_idx(f, io_u->offset);
923f7c1e 1823 zb = get_zone(f, zone_idx_b);
de65f7b7 1824 orig_zb = zb;
bfbdd35b 1825
2efcf74b
SK
1826 if (!zb->has_wp) {
1827 /* Accept non-write I/Os for conventional zones. */
1828 if (io_u->ddir != DDIR_WRITE)
1829 return io_u_accept;
139d8dc6 1830
2efcf74b
SK
1831 /*
1832 * Make sure that writes to conventional zones
1833 * don't cross over to any sequential zones.
1834 */
1835 if (!(zb + 1)->has_wp ||
1836 io_u->offset + io_u->buflen <= (zb + 1)->start)
1837 return io_u_accept;
1838
1839 if (io_u->offset + min_bs > (zb + 1)->start) {
1840 dprint(FD_IO,
07fc3f57 1841 "%s: off=%llu + min_bs=%"PRIu64" > next zone %"PRIu64"\n",
1e3d6e03 1842 f->file_name, io_u->offset,
ee5e3436 1843 min_bs, (zb + 1)->start);
139d8dc6
DLM
1844 io_u->offset =
1845 zb->start + (zb + 1)->start - io_u->offset;
1846 new_len = min(io_u->buflen,
1847 (zb + 1)->start - io_u->offset);
2efcf74b
SK
1848 } else {
1849 new_len = (zb + 1)->start - io_u->offset;
1850 }
139d8dc6 1851
2efcf74b 1852 io_u->buflen = new_len / min_bs * min_bs;
139d8dc6 1853
bfbdd35b 1854 return io_u_accept;
2efcf74b 1855 }
bfbdd35b
BVA
1856
1857 /*
1858 * Accept the I/O offset for reads if reading beyond the write pointer
1859 * is enabled.
1860 */
b7694961 1861 if (zb->cond != ZBD_ZONE_COND_OFFLINE &&
bfbdd35b
BVA
1862 io_u->ddir == DDIR_READ && td->o.read_beyond_wp)
1863 return io_u_accept;
1864
11afb212 1865 zbd_check_swd(td, f);
615555bb 1866
fae3b9a0 1867 zone_lock(td, f, zb);
6f0c6085 1868
bfbdd35b
BVA
1869 switch (io_u->ddir) {
1870 case DDIR_READ:
0ed7d55e
AR
1871 if (td->runstate == TD_VERIFYING && td_write(td)) {
1872 zb = zbd_replay_write_order(td, io_u, zb);
bfbdd35b
BVA
1873 goto accept;
1874 }
139d8dc6 1875
bfbdd35b 1876 /*
de65f7b7
DLM
1877 * Check that there is enough written data in the zone to do an
1878 * I/O of at least min_bs B. If there isn't, find a new zone for
1879 * the I/O.
bfbdd35b 1880 */
b7694961 1881 range = zb->cond != ZBD_ZONE_COND_OFFLINE ?
ee3696bd 1882 zb->wp - zb->start : 0;
de65f7b7 1883 if (range < min_bs ||
ee3696bd 1884 ((!td_random(td)) && (io_u->offset + min_bs > zb->wp))) {
4d4c71e6 1885 zone_unlock(zb);
923f7c1e 1886 zl = get_zone(f, f->max_zone);
5c86fdf6 1887 zb = zbd_find_zone(td, io_u, min_bs, zb, zl);
bfbdd35b
BVA
1888 if (!zb) {
1889 dprint(FD_ZBD,
1890 "%s: zbd_find_zone(%lld, %llu) failed\n",
1891 f->file_name, io_u->offset,
1892 io_u->buflen);
1893 goto eof;
1894 }
de65f7b7
DLM
1895 /*
1896 * zbd_find_zone() returned a zone with a range of at
1897 * least min_bs.
1898 */
ee3696bd 1899 range = zb->wp - zb->start;
de65f7b7
DLM
1900 assert(range >= min_bs);
1901
1902 if (!td_random(td))
ee3696bd 1903 io_u->offset = zb->start;
bfbdd35b 1904 }
139d8dc6 1905
de65f7b7
DLM
1906 /*
1907 * Make sure the I/O is within the zone valid data range while
1908 * maximizing the I/O size and preserving randomness.
1909 */
1910 if (range <= io_u->buflen)
ee3696bd 1911 io_u->offset = zb->start;
de65f7b7 1912 else if (td_random(td))
ee3696bd
DLM
1913 io_u->offset = zb->start +
1914 ((io_u->offset - orig_zb->start) %
de65f7b7 1915 (range - io_u->buflen)) / min_bs * min_bs;
139d8dc6 1916
43bcbd5b
SK
1917 /*
1918 * When zbd_find_zone() returns a conventional zone,
1919 * we can simply accept the new i/o offset here.
1920 */
1921 if (!zb->has_wp)
1922 return io_u_accept;
139d8dc6 1923
de65f7b7
DLM
1924 /*
1925 * Make sure the I/O does not cross over the zone wp position.
1926 */
1927 new_len = min((unsigned long long)io_u->buflen,
ee3696bd 1928 (unsigned long long)(zb->wp - io_u->offset));
de65f7b7
DLM
1929 new_len = new_len / min_bs * min_bs;
1930 if (new_len < io_u->buflen) {
1931 io_u->buflen = new_len;
1932 dprint(FD_IO, "Changed length from %u into %llu\n",
1933 orig_len, io_u->buflen);
bfbdd35b 1934 }
139d8dc6 1935
ee3696bd
DLM
1936 assert(zb->start <= io_u->offset);
1937 assert(io_u->offset + io_u->buflen <= zb->wp);
139d8dc6 1938
bfbdd35b 1939 goto accept;
139d8dc6 1940
bfbdd35b 1941 case DDIR_WRITE:
af94a8c3 1942 if (io_u->buflen > zbdi->zone_size) {
1c74aadc
DF
1943 td_verror(td, EINVAL, "I/O buflen exceeds zone size");
1944 dprint(FD_IO,
ee5e3436
SK
1945 "%s: I/O buflen %llu exceeds zone size %"PRIu64"\n",
1946 f->file_name, io_u->buflen, zbdi->zone_size);
bfbdd35b 1947 goto eof;
1c74aadc 1948 }
139d8dc6 1949
954217b9 1950 if (!zbd_open_zone(td, f, zone_idx_b)) {
4d4c71e6 1951 zone_unlock(zb);
59b07544 1952 zb = zbd_convert_to_open_zone(td, io_u);
1c74aadc
DF
1953 if (!zb) {
1954 dprint(FD_IO, "%s: can't convert to open zone",
1955 f->file_name);
59b07544 1956 goto eof;
1c74aadc 1957 }
59b07544 1958 }
139d8dc6 1959
a7c2b6fc
BVA
1960 /* Check whether the zone reset threshold has been exceeded */
1961 if (td->o.zrf.u.f) {
139d8dc6
DLM
1962 if (zbdi->wp_sectors_with_data >= f->io_size * td->o.zrt.u.f &&
1963 zbd_dec_and_reset_write_cnt(td, f))
a7c2b6fc 1964 zb->reset_zone = 1;
a7c2b6fc 1965 }
139d8dc6 1966
bfbdd35b
BVA
1967 /* Reset the zone pointer if necessary */
1968 if (zb->reset_zone || zbd_zone_full(f, zb, min_bs)) {
1969 assert(td->o.verify == VERIFY_NONE);
1970 /*
1971 * Since previous write requests may have been submitted
1972 * asynchronously and since we will submit the zone
1973 * reset synchronously, wait until previously submitted
1974 * write requests have completed before issuing a
1975 * zone reset.
1976 */
1977 io_u_quiesce(td);
1978 zb->reset_zone = 0;
1979 if (zbd_reset_zone(td, f, zb) < 0)
1980 goto eof;
236d23a8
SK
1981
1982 if (zb->capacity < min_bs) {
1c74aadc 1983 td_verror(td, EINVAL, "ZCAP is less min_bs");
07fc3f57 1984 log_err("zone capacity %"PRIu64" smaller than minimum block size %"PRIu64"\n",
ee5e3436 1985 zb->capacity, min_bs);
236d23a8
SK
1986 goto eof;
1987 }
bfbdd35b 1988 }
139d8dc6 1989
bfbdd35b
BVA
1990 /* Make writes occur at the write pointer */
1991 assert(!zbd_zone_full(f, zb, min_bs));
ee3696bd 1992 io_u->offset = zb->wp;
bfbdd35b 1993 if (!is_valid_offset(f, io_u->offset)) {
1c74aadc
DF
1994 td_verror(td, EINVAL, "invalid WP value");
1995 dprint(FD_ZBD, "%s: dropped request with offset %llu\n",
1996 f->file_name, io_u->offset);
bfbdd35b
BVA
1997 goto eof;
1998 }
139d8dc6 1999
bfbdd35b
BVA
2000 /*
2001 * Make sure that the buflen is a multiple of the minimal
2002 * block size. Give up if shrinking would make the request too
2003 * small.
2004 */
2005 new_len = min((unsigned long long)io_u->buflen,
236d23a8 2006 zbd_zone_capacity_end(zb) - io_u->offset);
bfbdd35b
BVA
2007 new_len = new_len / min_bs * min_bs;
2008 if (new_len == io_u->buflen)
2009 goto accept;
2010 if (new_len >= min_bs) {
2011 io_u->buflen = new_len;
2012 dprint(FD_IO, "Changed length from %u into %llu\n",
2013 orig_len, io_u->buflen);
2014 goto accept;
2015 }
139d8dc6 2016
1c74aadc 2017 td_verror(td, EIO, "zone remainder too small");
07fc3f57 2018 log_err("zone remainder %lld smaller than min block size %"PRIu64"\n",
1c74aadc 2019 (zbd_zone_capacity_end(zb) - io_u->offset), min_bs);
139d8dc6 2020
bfbdd35b 2021 goto eof;
139d8dc6 2022
bfbdd35b 2023 case DDIR_TRIM:
e3be810b
SK
2024 /* Check random trim targets a non-empty zone */
2025 if (!td_random(td) || zb->wp > zb->start)
2026 goto accept;
2027
2028 /* Find out a non-empty zone to trim */
2029 zone_unlock(zb);
2030 zl = get_zone(f, f->max_zone);
2031 zb = zbd_find_zone(td, io_u, 1, zb, zl);
2032 if (zb) {
2033 io_u->offset = zb->start;
2034 dprint(FD_ZBD, "%s: found new zone(%lld) for trim\n",
2035 f->file_name, io_u->offset);
2036 goto accept;
2037 }
139d8dc6 2038
e3be810b 2039 goto eof;
139d8dc6 2040
bfbdd35b 2041 case DDIR_SYNC:
e3be810b 2042 /* fall-through */
bfbdd35b
BVA
2043 case DDIR_DATASYNC:
2044 case DDIR_SYNC_FILE_RANGE:
2045 case DDIR_WAIT:
2046 case DDIR_LAST:
2047 case DDIR_INVAL:
2048 goto accept;
2049 }
2050
2051 assert(false);
2052
2053accept:
43bcbd5b 2054 assert(zb->has_wp);
b7694961 2055 assert(zb->cond != ZBD_ZONE_COND_OFFLINE);
d9ed3e63
DLM
2056 assert(!io_u->zbd_queue_io);
2057 assert(!io_u->zbd_put_io);
139d8dc6 2058
d9ed3e63
DLM
2059 io_u->zbd_queue_io = zbd_queue_io;
2060 io_u->zbd_put_io = zbd_put_io;
139d8dc6 2061
2ef3c1b0
DF
2062 /*
2063 * Since we return with the zone lock still held,
2064 * add an annotation to let Coverity know that it
2065 * is intentional.
2066 */
2067 /* coverity[missing_unlock] */
139d8dc6 2068
bfbdd35b
BVA
2069 return io_u_accept;
2070
2071eof:
43bcbd5b 2072 if (zb && zb->has_wp)
4d4c71e6 2073 zone_unlock(zb);
139d8dc6 2074
bfbdd35b
BVA
2075 return io_u_eof;
2076}
fd5d733f
BVA
2077
2078/* Return a string with ZBD statistics */
2079char *zbd_write_status(const struct thread_stat *ts)
2080{
2081 char *res;
2082
ee5e3436 2083 if (asprintf(&res, "; %"PRIu64" zone resets", ts->nr_zone_resets) < 0)
fd5d733f
BVA
2084 return NULL;
2085 return res;
2086}
e3be810b
SK
2087
2088/**
2089 * zbd_do_io_u_trim - If reset zone is applicable, do reset zone instead of trim
2090 *
2091 * @td: FIO thread data.
2092 * @io_u: FIO I/O unit.
2093 *
2094 * It is assumed that z->mutex is already locked.
2095 * Return io_u_completed when reset zone succeeds. Return 0 when the target zone
2096 * does not have write pointer. On error, return negative errno.
2097 */
2098int zbd_do_io_u_trim(const struct thread_data *td, struct io_u *io_u)
2099{
2100 struct fio_file *f = io_u->file;
2101 struct fio_zone_info *z;
2102 uint32_t zone_idx;
2103 int ret;
2104
2105 zone_idx = zbd_zone_idx(f, io_u->offset);
2106 z = get_zone(f, zone_idx);
2107
2108 if (!z->has_wp)
2109 return 0;
2110
2111 if (io_u->offset != z->start) {
139d8dc6
DLM
2112 log_err("Trim offset not at zone start (%lld)\n",
2113 io_u->offset);
e3be810b
SK
2114 return -EINVAL;
2115 }
2116
2117 ret = zbd_reset_zone((struct thread_data *)td, f, z);
2118 if (ret < 0)
2119 return ret;
2120
2121 return io_u_completed;
2122}