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