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