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