t/zbd: avoid test case 31 failure with small devices
[fio.git] / zbd.c
... / ...
CommitLineData
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>
10#include <fcntl.h>
11#include <sys/stat.h>
12#include <unistd.h>
13
14#include "os/os.h"
15#include "file.h"
16#include "fio.h"
17#include "lib/pow2.h"
18#include "log.h"
19#include "oslib/asprintf.h"
20#include "smalloc.h"
21#include "verify.h"
22#include "pshared.h"
23#include "zbd.h"
24
25static bool is_valid_offset(const struct fio_file *f, uint64_t offset)
26{
27 return (uint64_t)(offset - f->file_offset) < f->io_size;
28}
29
30static inline unsigned int zbd_zone_idx(const struct fio_file *f,
31 struct fio_zone_info *zone)
32{
33 return zone - f->zbd_info->zone_info;
34}
35
36/**
37 * zbd_offset_to_zone_idx - convert an offset into a zone number
38 * @f: file pointer.
39 * @offset: offset in bytes. If this offset is in the first zone_size bytes
40 * past the disk size then the index of the sentinel is returned.
41 */
42static unsigned int zbd_offset_to_zone_idx(const struct fio_file *f,
43 uint64_t offset)
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
126static inline struct fio_zone_info *zbd_get_zone(const struct fio_file *f,
127 unsigned int zone_idx)
128{
129 return &f->zbd_info->zone_info[zone_idx];
130}
131
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
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 */
143static int zbd_get_zoned_model(struct thread_data *td, struct fio_file *f,
144 enum zbd_zoned_model *model)
145{
146 int ret;
147
148 if (f->filetype == FIO_TYPE_PIPE) {
149 log_err("zonemode=zbd does not support pipes\n");
150 return -EINVAL;
151 }
152
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
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);
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 */
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)
190{
191 int ret;
192
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);
197 if (ret < 0) {
198 td_verror(td, errno, "report zones failed");
199 log_err("%s: report zones from sector %"PRIu64" failed (%d).\n",
200 f->file_name, offset >> 9, errno);
201 } else if (ret == 0) {
202 td_verror(td, errno, "Empty zone report");
203 log_err("%s: report zones from sector %"PRIu64" is empty.\n",
204 f->file_name, offset >> 9);
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 */
221static int zbd_reset_wp(struct thread_data *td, struct fio_file *f,
222 uint64_t offset, uint64_t length)
223{
224 int ret;
225
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);
230 if (ret < 0) {
231 td_verror(td, errno, "resetting wp failed");
232 log_err("%s: resetting wp for %"PRIu64" sectors at sector %"PRIu64" failed (%d).\n",
233 f->file_name, length >> 9, offset >> 9, errno);
234 }
235
236 return ret;
237}
238
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
262 dprint(FD_ZBD, "%s: resetting wp of zone %u.\n",
263 f->file_name, zbd_zone_idx(f, z));
264
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);
280
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,
298 struct fio_zone_info *z)
299{
300 uint32_t ozi;
301
302 if (!z->open)
303 return;
304
305 for (ozi = 0; ozi < f->zbd_info->num_open_zones; ozi++) {
306 if (zbd_get_zone(f, f->zbd_info->open_zones[ozi]) == z)
307 break;
308 }
309 if (ozi == f->zbd_info->num_open_zones)
310 return;
311
312 dprint(FD_ZBD, "%s: closing zone %u\n",
313 f->file_name, zbd_zone_idx(f, z));
314
315 memmove(f->zbd_info->open_zones + ozi,
316 f->zbd_info->open_zones + ozi + 1,
317 (ZBD_MAX_OPEN_ZONES - (ozi + 1)) *
318 sizeof(f->zbd_info->open_zones[0]));
319
320 f->zbd_info->num_open_zones--;
321 td->num_open_zones--;
322 z->open = 0;
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
344 dprint(FD_ZBD, "%s: examining zones %u .. %u\n",
345 f->file_name, zbd_zone_idx(f, zb), zbd_zone_idx(f, ze));
346
347 for (z = zb; z < ze; z++) {
348 if (!z->has_wp)
349 continue;
350
351 zone_lock(td, f, z);
352 pthread_mutex_lock(&f->zbd_info->mutex);
353 zbd_close_zone(td, f, z);
354 pthread_mutex_unlock(&f->zbd_info->mutex);
355
356 if (z->wp != z->start) {
357 dprint(FD_ZBD, "%s: resetting zone %u\n",
358 f->file_name, zbd_zone_idx(f, z));
359 if (zbd_reset_zone(td, f, z) < 0)
360 res = 1;
361 }
362
363 zone_unlock(z);
364 }
365
366 return res;
367}
368
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 */
379static int zbd_get_max_open_zones(struct thread_data *td, struct fio_file *f,
380 unsigned int *max_open_zones)
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
397/**
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.
402 *
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.
408 */
409static bool zbd_open_zone(struct thread_data *td, const struct fio_file *f,
410 struct fio_zone_info *z)
411{
412 const uint64_t min_bs = td->o.min_bs[DDIR_WRITE];
413 struct zoned_block_device_info *zbdi = f->zbd_info;
414 uint32_t zone_idx = zbd_zone_idx(f, z);
415 bool res = true;
416
417 if (z->cond == ZBD_ZONE_COND_OFFLINE)
418 return false;
419
420 /*
421 * Skip full zones with data verification enabled because resetting a
422 * zone causes data loss and hence causes verification to fail.
423 */
424 if (td->o.verify != VERIFY_NONE && zbd_zone_full(f, z, min_bs))
425 return false;
426
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;
434
435 pthread_mutex_lock(&zbdi->mutex);
436
437 if (z->open) {
438 /*
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.
442 */
443 if (z->wp >= zbd_zone_capacity_end(z))
444 res = false;
445 goto out;
446 }
447
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;
455
456 dprint(FD_ZBD, "%s: opening zone %u\n",
457 f->file_name, zone_idx);
458
459 zbdi->open_zones[zbdi->num_open_zones++] = zone_idx;
460 td->num_open_zones++;
461 z->open = 1;
462 res = true;
463
464out:
465 pthread_mutex_unlock(&zbdi->mutex);
466 return res;
467}
468
469/* Verify whether direct I/O is used for all host-managed zoned block 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 && f->filetype == FIO_TYPE_BLOCK &&
481 f->zbd_info->model == ZBD_HOST_MANAGED)
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);
495
496 if (f->io_size == 0)
497 return false;
498
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);
502 for (zone_idx = zone_idx_b; zone_idx <= zone_idx_e; zone_idx++)
503 if (zbd_get_zone(f, zone_idx)->has_wp)
504 return true;
505
506 return false;
507}
508
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;
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
548 z = zbd_offset_to_zone(f, f->file_offset);
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
564 z = zbd_offset_to_zone(f, f->file_offset + f->io_size);
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
582/*
583 * Verify whether offset and size parameters are aligned with zone boundaries.
584 */
585static bool zbd_verify_sizes(void)
586{
587 struct thread_data *td;
588 struct fio_file *f;
589 int i, j;
590
591 for_each_td(td, i) {
592 for_each_file(td, f, j) {
593 if (!zbd_zone_align_file_sizes(td, f))
594 return false;
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;
605 int i, j, k;
606
607 for_each_td(td, i) {
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 }
614 for_each_file(td, f, j) {
615 uint64_t zone_size;
616
617 if (!f->zbd_info)
618 continue;
619
620 zone_size = f->zbd_info->zone_size;
621 if (td_trim(td) && td->o.bs[DDIR_TRIM] != zone_size) {
622 log_info("%s: trim block size %llu is not the zone size %"PRIu64"\n",
623 f->file_name, td->o.bs[DDIR_TRIM],
624 zone_size);
625 return false;
626 }
627 for (k = 0; k < FIO_ARRAY_SIZE(td->o.bs); k++) {
628 if (td->o.verify != VERIFY_NONE &&
629 zone_size % td->o.bs[k] != 0) {
630 log_info("%s: block size %llu is not a divisor of the zone size %"PRIu64"\n",
631 f->file_name, td->o.bs[k],
632 zone_size);
633 return false;
634 }
635 }
636 }
637 }
638 return true;
639}
640
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;
660 uint64_t zone_size = td->o.zone_size;
661 uint64_t zone_capacity = td->o.zone_capacity;
662 struct zoned_block_device_info *zbd_info = NULL;
663 int i;
664
665 if (zone_size == 0) {
666 log_err("%s: Specifying the zone size is mandatory for regular file/block device with --zonemode=zbd\n\n",
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
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",
682 f->file_name, td->o.zone_capacity, td->o.zone_size);
683 return 1;
684 }
685
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
692 nr_zones = (f->real_file_size + zone_size - 1) / zone_size;
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
698 mutex_init_pshared(&zbd_info->mutex);
699 zbd_info->refcount = 1;
700 p = &zbd_info->zone_info[0];
701 for (i = 0; i < nr_zones; i++, p++) {
702 mutex_init_pshared_with_type(&p->mutex,
703 PTHREAD_MUTEX_RECURSIVE);
704 p->start = i * zone_size;
705 p->wp = p->start;
706 p->type = ZBD_ZONE_TYPE_SWR;
707 p->cond = ZBD_ZONE_COND_EMPTY;
708 p->capacity = zone_capacity;
709 p->has_wp = 1;
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) ?
717 ilog2(zone_size) : 0;
718 f->zbd_info->nr_zones = nr_zones;
719 return 0;
720}
721
722/*
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.
730 */
731static int parse_zone_info(struct thread_data *td, struct fio_file *f)
732{
733 int nr_zones, nrz;
734 struct zbd_zone *zones, *z;
735 struct fio_zone_info *p;
736 uint64_t zone_size, offset;
737 struct zoned_block_device_info *zbd_info = NULL;
738 int i, j, ret = -ENOMEM;
739
740 zones = calloc(ZBD_REPORT_MAX_ZONES, sizeof(struct zbd_zone));
741 if (!zones)
742 goto out;
743
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;
750 }
751
752 zone_size = zones[0].len;
753 nr_zones = (f->real_file_size + zone_size - 1) / zone_size;
754
755 if (td->o.zone_size == 0) {
756 td->o.zone_size = zone_size;
757 } else if (td->o.zone_size != zone_size) {
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);
760 ret = -EINVAL;
761 goto out;
762 }
763
764 dprint(FD_ZBD, "Device %s has %d zones of size %"PRIu64" KB\n",
765 f->file_name, nr_zones, zone_size / 1024);
766
767 zbd_info = scalloc(1, sizeof(*zbd_info) +
768 (nr_zones + 1) * sizeof(zbd_info->zone_info[0]));
769 if (!zbd_info)
770 goto out;
771 mutex_init_pshared(&zbd_info->mutex);
772 zbd_info->refcount = 1;
773 p = &zbd_info->zone_info[0];
774 for (offset = 0, j = 0; j < nr_zones;) {
775 z = &zones[0];
776 for (i = 0; i < nrz; i++, j++, z++, p++) {
777 mutex_init_pshared_with_type(&p->mutex,
778 PTHREAD_MUTEX_RECURSIVE);
779 p->start = z->start;
780 p->capacity = z->capacity;
781
782 switch (z->cond) {
783 case ZBD_ZONE_COND_NOT_WP:
784 case ZBD_ZONE_COND_FULL:
785 p->wp = p->start + p->capacity;
786 break;
787 default:
788 assert(z->start <= z->wp);
789 assert(z->wp <= z->start + zone_size);
790 p->wp = z->wp;
791 break;
792 }
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 }
801 p->type = z->type;
802 p->cond = z->cond;
803
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;
808 goto out;
809 }
810 }
811 z--;
812 offset = z->start + z->len;
813 if (j >= nr_zones)
814 break;
815
816 nrz = zbd_report_zones(td, f, offset, zones,
817 min((uint32_t)(nr_zones - j),
818 ZBD_REPORT_MAX_ZONES));
819 if (nrz < 0) {
820 ret = nrz;
821 log_info("fio: report zones (offset %"PRIu64") failed for %s (%d).\n",
822 offset, f->file_name, -ret);
823 goto out;
824 }
825 }
826
827 /* a sentinel */
828 zbd_info->zone_info[nr_zones].start = offset;
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) ?
833 ilog2(zone_size) : 0;
834 f->zbd_info->nr_zones = nr_zones;
835 zbd_info = NULL;
836 ret = 0;
837
838out:
839 sfree(zbd_info);
840 free(zones);
841 return ret;
842}
843
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
850 if (zbd->model != ZBD_HOST_MANAGED || td->o.ignore_zone_limits) {
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 */
881 if (zbd->max_open_zones > ZBD_MAX_OPEN_ZONES) {
882 td_verror(td, EINVAL, "'max_open_zones' value is too large");
883 log_err("'max_open_zones' value is larger than %u\n",
884 ZBD_MAX_OPEN_ZONES);
885 return -EINVAL;
886 }
887
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
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 */
899static int zbd_create_zone_info(struct thread_data *td, struct fio_file *f)
900{
901 enum zbd_zoned_model zbd_model;
902 int ret;
903
904 assert(td->o.zone_mode == ZONE_MODE_ZBD);
905
906 ret = zbd_get_zoned_model(td, f, &zbd_model);
907 if (ret)
908 return ret;
909
910 switch (zbd_model) {
911 case ZBD_HOST_AWARE:
912 case ZBD_HOST_MANAGED:
913 ret = parse_zone_info(td, f);
914 if (ret)
915 return ret;
916 break;
917 case ZBD_NONE:
918 ret = init_zone_info(td, f);
919 if (ret)
920 return ret;
921 break;
922 default:
923 td_verror(td, EINVAL, "Unsupported zoned model");
924 log_err("Unsupported zoned model\n");
925 return -EINVAL;
926 }
927
928 assert(f->zbd_info);
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;
935 }
936
937 return 0;
938}
939
940void zbd_free_zone_info(struct fio_file *f)
941{
942 uint32_t refcount;
943
944 assert(f->zbd_info);
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)
985 td_verror(td, -ret, "zbd_create_zone_info() failed");
986
987 return ret;
988}
989
990int zbd_init_files(struct thread_data *td)
991{
992 struct fio_file *f;
993 int i;
994
995 for_each_file(td, f, i) {
996 if (zbd_init_zone_info(td, f))
997 return 1;
998 }
999
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;
1010 uint64_t zone_size;
1011
1012 /* zonemode=strided doesn't get per-file zone size. */
1013 zone_size = zbd ? zbd->zone_size : td->o.zone_size;
1014 if (zone_size == 0)
1015 continue;
1016
1017 if (td->o.size_nz > 0)
1018 td->o.size = td->o.size_nz * zone_size;
1019 if (td->o.io_size_nz > 0)
1020 td->o.io_size = td->o.io_size_nz * zone_size;
1021 if (td->o.start_offset_nz > 0)
1022 td->o.start_offset = td->o.start_offset_nz * zone_size;
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)
1027 td->o.zone_skip = td->o.zone_skip_nz * zone_size;
1028 }
1029}
1030
1031int zbd_setup_files(struct thread_data *td)
1032{
1033 struct fio_file *f;
1034 int i;
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
1047 for_each_file(td, f, i) {
1048 struct zoned_block_device_info *zbd = f->zbd_info;
1049 struct fio_zone_info *z;
1050 int zi;
1051
1052 assert(zbd);
1053
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);
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
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 }
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");
1080 return 1;
1081 }
1082
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
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;
1096 if (zbd_open_zone(td, f, z))
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 }
1107 }
1108
1109 return 0;
1110}
1111
1112/*
1113 * Reset zbd_info.write_cnt, the counter that counts down towards the next
1114 * zone reset.
1115 */
1116static void _zbd_reset_write_cnt(const struct thread_data *td,
1117 const struct fio_file *f)
1118{
1119 assert(0 <= td->o.zrf.u.f && td->o.zrf.u.f <= 1);
1120
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;
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);
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)
1143 _zbd_reset_write_cnt(td, f);
1144 pthread_mutex_unlock(&f->zbd_info->mutex);
1145
1146 return write_cnt == 0;
1147}
1148
1149enum swd_action {
1150 CHECK_SWD,
1151 SET_SWD,
1152};
1153
1154/* Calculate the number of sectors with data (swd) and perform action 'a' */
1155static uint64_t zbd_process_swd(struct thread_data *td,
1156 const struct fio_file *f, enum swd_action a)
1157{
1158 struct fio_zone_info *zb, *ze, *z;
1159 uint64_t swd = 0;
1160 uint64_t wp_swd = 0;
1161
1162 zb = zbd_get_zone(f, f->min_zone);
1163 ze = zbd_get_zone(f, f->max_zone);
1164 for (z = zb; z < ze; z++) {
1165 if (z->has_wp) {
1166 zone_lock(td, f, z);
1167 wp_swd += z->wp - z->start;
1168 }
1169 swd += z->wp - z->start;
1170 }
1171
1172 pthread_mutex_lock(&f->zbd_info->mutex);
1173 switch (a) {
1174 case CHECK_SWD:
1175 assert(f->zbd_info->sectors_with_data == swd);
1176 assert(f->zbd_info->wp_sectors_with_data == wp_swd);
1177 break;
1178 case SET_SWD:
1179 f->zbd_info->sectors_with_data = swd;
1180 f->zbd_info->wp_sectors_with_data = wp_swd;
1181 break;
1182 }
1183 pthread_mutex_unlock(&f->zbd_info->mutex);
1184
1185 for (z = zb; z < ze; z++)
1186 if (z->has_wp)
1187 zone_unlock(z);
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
1198/* Check whether the values of zbd_info.*sectors_with_data are correct. */
1199static void zbd_check_swd(struct thread_data *td, const struct fio_file *f)
1200{
1201 if (!enable_check_swd)
1202 return;
1203
1204 zbd_process_swd(td, f, CHECK_SWD);
1205}
1206
1207void zbd_file_reset(struct thread_data *td, struct fio_file *f)
1208{
1209 struct fio_zone_info *zb, *ze;
1210 uint64_t swd;
1211
1212 if (!f->zbd_info || !td_write(td))
1213 return;
1214
1215 zb = zbd_get_zone(f, f->min_zone);
1216 ze = zbd_get_zone(f, f->max_zone);
1217 swd = zbd_process_swd(td, f, SET_SWD);
1218
1219 dprint(FD_ZBD, "%s(%s): swd = %" PRIu64 "\n",
1220 __func__, f->file_name, swd);
1221
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 */
1227 if (td->o.verify != VERIFY_NONE && td->runstate != TD_VERIFYING)
1228 zbd_reset_zones(td, f, zb, ze);
1229 zbd_reset_write_cnt(td, f);
1230}
1231
1232/* Return random zone index for one of the open zones. */
1233static uint32_t pick_random_zone_idx(const struct fio_file *f,
1234 const struct io_u *io_u)
1235{
1236 return (io_u->offset - f->file_offset) *
1237 f->zbd_info->num_open_zones / f->io_size;
1238}
1239
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
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
1256 * necessary. The open zone is searched across sequential zones.
1257 * This algorithm can only work correctly if all write pointers are
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 */
1261static struct fio_zone_info *zbd_convert_to_open_zone(struct thread_data *td,
1262 struct io_u *io_u)
1263{
1264 const uint64_t min_bs = td->o.min_bs[io_u->ddir];
1265 struct fio_file *f = io_u->file;
1266 struct zoned_block_device_info *zbdi = f->zbd_info;
1267 struct fio_zone_info *z;
1268 unsigned int open_zone_idx = -1;
1269 uint32_t zone_idx, new_zone_idx;
1270 int i;
1271 bool wait_zone_close;
1272 bool in_flight;
1273 bool should_retry = true;
1274
1275 assert(is_valid_offset(f, io_u->offset));
1276
1277 if (zbdi->max_open_zones || td->o.job_max_open_zones) {
1278 /*
1279 * This statement accesses zbdi->open_zones[] on purpose
1280 * without locking.
1281 */
1282 zone_idx = zbdi->open_zones[pick_random_zone_idx(f, io_u)];
1283 } else {
1284 zone_idx = zbd_offset_to_zone_idx(f, io_u->offset);
1285 }
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;
1290
1291 dprint(FD_ZBD,
1292 "%s(%s): starting from zone %d (offset %lld, buflen %lld)\n",
1293 __func__, f->file_name, zone_idx, io_u->offset, io_u->buflen);
1294
1295 /*
1296 * Since z->mutex is the outer lock and zbdi->mutex the inner
1297 * lock it can happen that the state of the zone with index zone_idx
1298 * has changed after 'z' has been assigned and before zbdi->mutex
1299 * has been obtained. Hence the loop.
1300 */
1301 for (;;) {
1302 uint32_t tmp_idx;
1303
1304 z = zbd_get_zone(f, zone_idx);
1305 if (z->has_wp)
1306 zone_lock(td, f, z);
1307
1308 pthread_mutex_lock(&zbdi->mutex);
1309
1310 if (z->has_wp) {
1311 if (z->cond != ZBD_ZONE_COND_OFFLINE &&
1312 zbdi->max_open_zones == 0 &&
1313 td->o.job_max_open_zones == 0)
1314 goto examine_zone;
1315 if (zbdi->num_open_zones == 0) {
1316 dprint(FD_ZBD, "%s(%s): no zones are open\n",
1317 __func__, f->file_name);
1318 goto open_other_zone;
1319 }
1320 }
1321
1322 /*
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.
1326 */
1327 open_zone_idx = pick_random_zone_idx(f, io_u);
1328 assert(!open_zone_idx ||
1329 open_zone_idx < zbdi->num_open_zones);
1330 tmp_idx = open_zone_idx;
1331
1332 for (i = 0; i < zbdi->num_open_zones; i++) {
1333 uint32_t tmpz;
1334
1335 if (tmp_idx >= zbdi->num_open_zones)
1336 tmp_idx = 0;
1337 tmpz = zbdi->open_zones[tmp_idx];
1338 if (f->min_zone <= tmpz && tmpz < f->max_zone) {
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);
1348
1349 pthread_mutex_unlock(&zbdi->mutex);
1350
1351 if (z->has_wp)
1352 zone_unlock(z);
1353
1354 return NULL;
1355
1356found_candidate_zone:
1357 new_zone_idx = zbdi->open_zones[open_zone_idx];
1358 if (new_zone_idx == zone_idx)
1359 break;
1360 zone_idx = new_zone_idx;
1361
1362 pthread_mutex_unlock(&zbdi->mutex);
1363
1364 if (z->has_wp)
1365 zone_unlock(z);
1366 }
1367
1368 /* Both z->mutex and zbdi->mutex are held. */
1369
1370examine_zone:
1371 if (z->wp + min_bs <= zbd_zone_capacity_end(z)) {
1372 pthread_mutex_unlock(&zbdi->mutex);
1373 goto out;
1374 }
1375
1376open_other_zone:
1377 /* Check if number of open zones reaches one of limits. */
1378 wait_zone_close =
1379 zbdi->num_open_zones == f->max_zone - f->min_zone ||
1380 (zbdi->max_open_zones &&
1381 zbdi->num_open_zones == zbdi->max_open_zones) ||
1382 (td->o.job_max_open_zones &&
1383 td->num_open_zones == td->o.job_max_open_zones);
1384
1385 pthread_mutex_unlock(&zbdi->mutex);
1386
1387 /* Only z->mutex is held. */
1388
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) {
1394 dprint(FD_ZBD,
1395 "%s(%s): quiesce to allow open zones to close\n",
1396 __func__, f->file_name);
1397 io_u_quiesce(td);
1398 }
1399
1400retry:
1401 /* Zone 'z' is full, so try to open a new zone. */
1402 for (i = f->io_size / zbdi->zone_size; i > 0; i--) {
1403 zone_idx++;
1404 if (z->has_wp)
1405 zone_unlock(z);
1406 z++;
1407 if (!is_valid_offset(f, z->start)) {
1408 /* Wrap-around. */
1409 zone_idx = f->min_zone;
1410 z = zbd_get_zone(f, zone_idx);
1411 }
1412 assert(is_valid_offset(f, z->start));
1413 if (!z->has_wp)
1414 continue;
1415 zone_lock(td, f, z);
1416 if (z->open)
1417 continue;
1418 if (zbd_open_zone(td, f, z))
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. */
1425 pthread_mutex_lock(&zbdi->mutex);
1426 for (i = 0; i < zbdi->num_open_zones; i++) {
1427 zone_idx = zbdi->open_zones[i];
1428 if (zone_idx < f->min_zone || zone_idx >= f->max_zone)
1429 continue;
1430 pthread_mutex_unlock(&zbdi->mutex);
1431 zone_unlock(z);
1432
1433 z = zbd_get_zone(f, zone_idx);
1434
1435 zone_lock(td, f, z);
1436 if (z->wp + min_bs <= zbd_zone_capacity_end(z))
1437 goto out;
1438 pthread_mutex_lock(&zbdi->mutex);
1439 }
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) {
1448 dprint(FD_ZBD,
1449 "%s(%s): wait zone close and retry open zones\n",
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
1459 pthread_mutex_unlock(&zbdi->mutex);
1460
1461 zone_unlock(z);
1462
1463 dprint(FD_ZBD, "%s(%s): did not open another zone\n",
1464 __func__, f->file_name);
1465
1466 return NULL;
1467
1468out:
1469 dprint(FD_ZBD, "%s(%s): returning zone %d\n",
1470 __func__, f->file_name, zone_idx);
1471
1472 io_u->offset = z->start;
1473 assert(z->has_wp);
1474 assert(z->cond != ZBD_ZONE_COND_OFFLINE);
1475
1476 return z;
1477}
1478
1479/* The caller must hold z->mutex. */
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)
1483{
1484 const struct fio_file *f = io_u->file;
1485 const uint64_t min_bs = td->o.min_bs[DDIR_WRITE];
1486
1487 if (!zbd_open_zone(td, f, z)) {
1488 zone_unlock(z);
1489 z = zbd_convert_to_open_zone(td, io_u);
1490 assert(z);
1491 }
1492
1493 if (z->verify_block * min_bs >= z->capacity) {
1494 log_err("%s: %d * %"PRIu64" >= %"PRIu64"\n",
1495 f->file_name, z->verify_block, min_bs, z->capacity);
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 }
1502
1503 io_u->offset = z->start + z->verify_block * min_bs;
1504 if (io_u->offset + io_u->buflen >= zbd_zone_capacity_end(z)) {
1505 log_err("%s: %llu + %llu >= %"PRIu64"\n",
1506 f->file_name, io_u->offset, io_u->buflen,
1507 zbd_zone_capacity_end(z));
1508 assert(false);
1509 }
1510 z->verify_block += io_u->buflen / min_bs;
1511
1512 return z;
1513}
1514
1515/*
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.
1518 *
1519 * Either returns NULL or returns a zone pointer. When the zone has write
1520 * pointer, hold the mutex for the zone.
1521 */
1522static struct fio_zone_info *
1523zbd_find_zone(struct thread_data *td, struct io_u *io_u, uint64_t min_bytes,
1524 struct fio_zone_info *zb, struct fio_zone_info *zl)
1525{
1526 struct fio_file *f = io_u->file;
1527 struct fio_zone_info *z1, *z2;
1528 const struct fio_zone_info *const zf = zbd_get_zone(f, f->min_zone);
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--) {
1535 if (z1 < zl && z1->cond != ZBD_ZONE_COND_OFFLINE) {
1536 if (z1->has_wp)
1537 zone_lock(td, f, z1);
1538 if (z1->start + min_bytes <= z1->wp)
1539 return z1;
1540 if (z1->has_wp)
1541 zone_unlock(z1);
1542 } else if (!td_random(td)) {
1543 break;
1544 }
1545
1546 if (td_random(td) && z2 >= zf &&
1547 z2->cond != ZBD_ZONE_COND_OFFLINE) {
1548 if (z2->has_wp)
1549 zone_lock(td, f, z2);
1550 if (z2->start + min_bytes <= z2->wp)
1551 return z2;
1552 if (z2->has_wp)
1553 zone_unlock(z2);
1554 }
1555 }
1556
1557 dprint(FD_ZBD,
1558 "%s: no zone has %"PRIu64" bytes of readable data\n",
1559 f->file_name, min_bytes);
1560
1561 return NULL;
1562}
1563
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);
1581 zbd_close_zone(td, f, z);
1582 pthread_mutex_unlock(&f->zbd_info->mutex);
1583 }
1584}
1585
1586/**
1587 * zbd_queue_io - update the write pointer of a sequential zone
1588 * @io_u: I/O unit
1589 * @success: Whether or not the I/O unit has been queued successfully
1590 * @q: queueing status (busy, completed or queued).
1591 *
1592 * For write and trim operations, update the write pointer of the I/O unit
1593 * target zone.
1594 */
1595static void zbd_queue_io(struct thread_data *td, struct io_u *io_u, int q,
1596 bool success)
1597{
1598 const struct fio_file *f = io_u->file;
1599 struct zoned_block_device_info *zbd_info = f->zbd_info;
1600 struct fio_zone_info *z;
1601 uint64_t zone_end;
1602
1603 assert(zbd_info);
1604
1605 z = zbd_offset_to_zone(f, io_u->offset);
1606 assert(z->has_wp);
1607
1608 if (!success)
1609 goto unlock;
1610
1611 dprint(FD_ZBD,
1612 "%s: queued I/O (%lld, %llu) for zone %u\n",
1613 f->file_name, io_u->offset, io_u->buflen, zbd_zone_idx(f, z));
1614
1615 switch (io_u->ddir) {
1616 case DDIR_WRITE:
1617 zone_end = min((uint64_t)(io_u->offset + io_u->buflen),
1618 zbd_zone_capacity_end(z));
1619
1620 /*
1621 * z->wp > zone_end means that one or more I/O errors
1622 * have occurred.
1623 */
1624 pthread_mutex_lock(&zbd_info->mutex);
1625 if (z->wp <= zone_end) {
1626 zbd_info->sectors_with_data += zone_end - z->wp;
1627 zbd_info->wp_sectors_with_data += zone_end - z->wp;
1628 }
1629 pthread_mutex_unlock(&zbd_info->mutex);
1630 z->wp = zone_end;
1631 break;
1632 default:
1633 break;
1634 }
1635
1636 if (q == FIO_Q_COMPLETED && !io_u->error)
1637 zbd_end_zone_io(td, io_u, z);
1638
1639unlock:
1640 if (!success || q != FIO_Q_QUEUED) {
1641 /* BUSY or COMPLETED: unlock the zone */
1642 zone_unlock(z);
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 */
1651static void zbd_put_io(struct thread_data *td, const struct io_u *io_u)
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;
1656
1657 assert(zbd_info);
1658
1659 z = zbd_offset_to_zone(f, io_u->offset);
1660 assert(z->has_wp);
1661
1662 dprint(FD_ZBD,
1663 "%s: terminate I/O (%lld, %llu) for zone %u\n",
1664 f->file_name, io_u->offset, io_u->buflen, zbd_zone_idx(f, z));
1665
1666 zbd_end_zone_io(td, io_u, z);
1667
1668 zone_unlock(z);
1669 zbd_check_swd(td, f);
1670}
1671
1672/*
1673 * Windows and MacOS do not define this.
1674 */
1675#ifndef EREMOTEIO
1676#define EREMOTEIO 121 /* POSIX value */
1677#endif
1678
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
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;
1706
1707 assert(td->o.zone_mode == ZONE_MODE_ZBD);
1708 assert(td->o.zone_size);
1709 assert(f->zbd_info);
1710
1711 z = zbd_offset_to_zone(f, f->last_pos[ddir]);
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 */
1718 if (z->capacity < f->zbd_info->zone_size &&
1719 !td_random(td) && ddir == DDIR_WRITE &&
1720 f->last_pos[ddir] >= zbd_zone_capacity_end(z)) {
1721 dprint(FD_ZBD,
1722 "%s: Jump from zone capacity limit to zone end:"
1723 " (%"PRIu64" -> %"PRIu64") for zone %u (%"PRIu64")\n",
1724 f->file_name, f->last_pos[ddir],
1725 zbd_zone_end(z), zbd_zone_idx(f, z), z->capacity);
1726 td->io_skip_bytes += zbd_zone_end(z) - f->last_pos[ddir];
1727 f->last_pos[ddir] = zbd_zone_end(z);
1728 }
1729
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 */
1743 if (td->zone_bytes >= td->o.zone_size ||
1744 f->last_pos[ddir] >= zbd_zone_end(z) ||
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
1764/**
1765 * zbd_adjust_ddir - Adjust an I/O direction for zonemode=zbd.
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 */
1782 assert(io_u->file->zbd_info);
1783 if (ddir != DDIR_READ || !td_rw(td))
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
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{
1804 struct fio_file *f = io_u->file;
1805 struct zoned_block_device_info *zbdi = f->zbd_info;
1806 struct fio_zone_info *zb, *zl, *orig_zb;
1807 uint32_t orig_len = io_u->buflen;
1808 uint64_t min_bs = td->o.min_bs[io_u->ddir];
1809 uint64_t new_len;
1810 int64_t range;
1811
1812 assert(zbdi);
1813 assert(min_bs);
1814 assert(is_valid_offset(f, io_u->offset));
1815 assert(io_u->buflen);
1816
1817 zb = zbd_offset_to_zone(f, io_u->offset);
1818 orig_zb = zb;
1819
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;
1824
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,
1835 "%s: off=%llu + min_bs=%"PRIu64" > next zone %"PRIu64"\n",
1836 f->file_name, io_u->offset,
1837 min_bs, (zb + 1)->start);
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);
1842 } else {
1843 new_len = (zb + 1)->start - io_u->offset;
1844 }
1845
1846 io_u->buflen = new_len / min_bs * min_bs;
1847
1848 return io_u_accept;
1849 }
1850
1851 /*
1852 * Accept the I/O offset for reads if reading beyond the write pointer
1853 * is enabled.
1854 */
1855 if (zb->cond != ZBD_ZONE_COND_OFFLINE &&
1856 io_u->ddir == DDIR_READ && td->o.read_beyond_wp)
1857 return io_u_accept;
1858
1859 zbd_check_swd(td, f);
1860
1861 zone_lock(td, f, zb);
1862
1863 switch (io_u->ddir) {
1864 case DDIR_READ:
1865 if (td->runstate == TD_VERIFYING && td_write(td)) {
1866 zb = zbd_replay_write_order(td, io_u, zb);
1867 goto accept;
1868 }
1869
1870 /*
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.
1874 */
1875 range = zb->cond != ZBD_ZONE_COND_OFFLINE ?
1876 zb->wp - zb->start : 0;
1877 if (range < min_bs ||
1878 ((!td_random(td)) && (io_u->offset + min_bs > zb->wp))) {
1879 zone_unlock(zb);
1880 zl = zbd_get_zone(f, f->max_zone);
1881 zb = zbd_find_zone(td, io_u, min_bs, zb, zl);
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 }
1889 /*
1890 * zbd_find_zone() returned a zone with a range of at
1891 * least min_bs.
1892 */
1893 range = zb->wp - zb->start;
1894 assert(range >= min_bs);
1895
1896 if (!td_random(td))
1897 io_u->offset = zb->start;
1898 }
1899
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)
1905 io_u->offset = zb->start;
1906 else if (td_random(td))
1907 io_u->offset = zb->start +
1908 ((io_u->offset - orig_zb->start) %
1909 (range - io_u->buflen)) / min_bs * min_bs;
1910
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;
1917
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,
1922 (unsigned long long)(zb->wp - io_u->offset));
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);
1928 }
1929
1930 assert(zb->start <= io_u->offset);
1931 assert(io_u->offset + io_u->buflen <= zb->wp);
1932
1933 goto accept;
1934
1935 case DDIR_WRITE:
1936 if (io_u->buflen > zbdi->zone_size) {
1937 td_verror(td, EINVAL, "I/O buflen exceeds zone size");
1938 dprint(FD_IO,
1939 "%s: I/O buflen %llu exceeds zone size %"PRIu64"\n",
1940 f->file_name, io_u->buflen, zbdi->zone_size);
1941 goto eof;
1942 }
1943
1944 if (!zbd_open_zone(td, f, zb)) {
1945 zone_unlock(zb);
1946 zb = zbd_convert_to_open_zone(td, io_u);
1947 if (!zb) {
1948 dprint(FD_IO, "%s: can't convert to open zone",
1949 f->file_name);
1950 goto eof;
1951 }
1952 }
1953
1954 /* Check whether the zone reset threshold has been exceeded */
1955 if (td->o.zrf.u.f) {
1956 if (zbdi->wp_sectors_with_data >= f->io_size * td->o.zrt.u.f &&
1957 zbd_dec_and_reset_write_cnt(td, f))
1958 zb->reset_zone = 1;
1959 }
1960
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;
1975
1976 if (zb->capacity < min_bs) {
1977 td_verror(td, EINVAL, "ZCAP is less min_bs");
1978 log_err("zone capacity %"PRIu64" smaller than minimum block size %"PRIu64"\n",
1979 zb->capacity, min_bs);
1980 goto eof;
1981 }
1982 }
1983
1984 /* Make writes occur at the write pointer */
1985 assert(!zbd_zone_full(f, zb, min_bs));
1986 io_u->offset = zb->wp;
1987 if (!is_valid_offset(f, io_u->offset)) {
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);
1991 goto eof;
1992 }
1993
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,
2000 zbd_zone_capacity_end(zb) - io_u->offset);
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 }
2010
2011 td_verror(td, EIO, "zone remainder too small");
2012 log_err("zone remainder %lld smaller than min block size %"PRIu64"\n",
2013 (zbd_zone_capacity_end(zb) - io_u->offset), min_bs);
2014
2015 goto eof;
2016
2017 case DDIR_TRIM:
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);
2024 zl = zbd_get_zone(f, f->max_zone);
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 }
2032
2033 goto eof;
2034
2035 case DDIR_SYNC:
2036 /* fall-through */
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:
2048 assert(zb->has_wp);
2049 assert(zb->cond != ZBD_ZONE_COND_OFFLINE);
2050 assert(!io_u->zbd_queue_io);
2051 assert(!io_u->zbd_put_io);
2052
2053 io_u->zbd_queue_io = zbd_queue_io;
2054 io_u->zbd_put_io = zbd_put_io;
2055
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] */
2062
2063 return io_u_accept;
2064
2065eof:
2066 if (zb && zb->has_wp)
2067 zone_unlock(zb);
2068
2069 return io_u_eof;
2070}
2071
2072/* Return a string with ZBD statistics */
2073char *zbd_write_status(const struct thread_stat *ts)
2074{
2075 char *res;
2076
2077 if (asprintf(&res, "; %"PRIu64" zone resets", ts->nr_zone_resets) < 0)
2078 return NULL;
2079 return res;
2080}
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;
2096 int ret;
2097
2098 z = zbd_offset_to_zone(f, io_u->offset);
2099 if (!z->has_wp)
2100 return 0;
2101
2102 if (io_u->offset != z->start) {
2103 log_err("Trim offset not at zone start (%lld)\n",
2104 io_u->offset);
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}