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