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