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