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