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