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