2 * Copyright (C) 2018 Western Digital Corporation or its affiliates.
4 * This file is released under the GPL.
19 #include "oslib/asprintf.h"
25 static bool is_valid_offset(const struct fio_file *f, uint64_t offset)
27 return (uint64_t)(offset - f->file_offset) < f->io_size;
30 static inline unsigned int zbd_zone_idx(const struct fio_file *f,
31 struct fio_zone_info *zone)
33 return zone - f->zbd_info->zone_info;
37 * zbd_offset_to_zone_idx - convert an offset into a zone number
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.
42 static unsigned int zbd_offset_to_zone_idx(const struct fio_file *f,
47 if (f->zbd_info->zone_size_log2 > 0)
48 zone_idx = offset >> f->zbd_info->zone_size_log2;
50 zone_idx = offset / f->zbd_info->zone_size;
52 return min(zone_idx, f->zbd_info->nr_zones);
56 * zbd_zone_end - Return zone end location
57 * @z: zone info pointer.
59 static inline uint64_t zbd_zone_end(const struct fio_zone_info *z)
65 * zbd_zone_capacity_end - Return zone capacity limit end location
66 * @z: zone info pointer.
68 static inline uint64_t zbd_zone_capacity_end(const struct fio_zone_info *z)
70 return z->start + z->capacity;
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.
78 static inline uint64_t zbd_zone_remainder(struct fio_zone_info *z)
80 if (z->wp >= zbd_zone_capacity_end(z))
83 return zbd_zone_capacity_end(z) - z->wp;
87 * zbd_zone_full - verify whether a minimum number of bytes remain in a zone
89 * @z: zone info pointer.
90 * @required: minimum number of bytes that must remain in a zone.
92 * The caller must hold z->mutex.
94 static bool zbd_zone_full(const struct fio_file *f, struct fio_zone_info *z,
97 assert((required & 511) == 0);
99 return z->has_wp && required > zbd_zone_remainder(z);
102 static void zone_lock(struct thread_data *td, const struct fio_file *f,
103 struct fio_zone_info *z)
105 struct zoned_block_device_info *zbd = f->zbd_info;
106 uint32_t nz = z - zbd->zone_info;
108 /* A thread should never lock zones outside its working area. */
109 assert(f->min_zone <= nz && nz < f->max_zone);
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
122 if (pthread_mutex_trylock(&z->mutex) != 0) {
123 if (!td_ioengine_flagged(td, FIO_SYNCIO))
125 pthread_mutex_lock(&z->mutex);
129 static inline void zone_unlock(struct fio_zone_info *z)
134 ret = pthread_mutex_unlock(&z->mutex);
138 static inline struct fio_zone_info *zbd_get_zone(const struct fio_file *f,
139 unsigned int zone_idx)
141 return &f->zbd_info->zone_info[zone_idx];
144 static inline struct fio_zone_info *
145 zbd_offset_to_zone(const struct fio_file *f, uint64_t offset)
147 return zbd_get_zone(f, zbd_offset_to_zone_idx(f, offset));
150 static bool accounting_vdb(struct thread_data *td, const struct fio_file *f)
152 return td->o.zrt.u.f && td_write(td);
156 * zbd_get_zoned_model - Get a device zoned model
157 * @td: FIO thread data
158 * @f: FIO file for which to get model information
160 static int zbd_get_zoned_model(struct thread_data *td, struct fio_file *f,
161 enum zbd_zoned_model *model)
165 if (f->filetype == FIO_TYPE_PIPE) {
166 log_err("zonemode=zbd does not support pipes\n");
170 /* If regular file, always emulate zones inside the file. */
171 if (f->filetype == FIO_TYPE_FILE) {
176 if (td->io_ops && td->io_ops->get_zoned_model)
177 ret = td->io_ops->get_zoned_model(td, f, model);
179 ret = blkzoned_get_zoned_model(td, f, model);
181 td_verror(td, errno, "get zoned model failed");
182 log_err("%s: get zoned model failed (%d).\n",
183 f->file_name, errno);
190 * zbd_report_zones - Get zone information
191 * @td: FIO thread data.
192 * @f: FIO file for which to get zone information
193 * @offset: offset from which to report zones
194 * @zones: Array of struct zbd_zone
195 * @nr_zones: Size of @zones array
197 * Get zone information into @zones starting from the zone at offset @offset
198 * for the device specified by @f.
200 * Returns the number of zones reported upon success and a negative error code
201 * upon failure. If the zone report is empty, always assume an error (device
202 * problem) and return -EIO.
204 static int zbd_report_zones(struct thread_data *td, struct fio_file *f,
205 uint64_t offset, struct zbd_zone *zones,
206 unsigned int nr_zones)
210 if (td->io_ops && td->io_ops->report_zones)
211 ret = td->io_ops->report_zones(td, f, offset, zones, nr_zones);
213 ret = blkzoned_report_zones(td, f, offset, zones, nr_zones);
215 td_verror(td, errno, "report zones failed");
216 log_err("%s: report zones from sector %"PRIu64" failed (nr_zones=%d; errno=%d).\n",
217 f->file_name, offset >> 9, nr_zones, errno);
218 } else if (ret == 0) {
219 td_verror(td, errno, "Empty zone report");
220 log_err("%s: report zones from sector %"PRIu64" is empty.\n",
221 f->file_name, offset >> 9);
229 * zbd_reset_wp - reset the write pointer of a range of zones
230 * @td: FIO thread data.
231 * @f: FIO file for which to reset zones
232 * @offset: Starting offset of the first zone to reset
233 * @length: Length of the range of zones to reset
235 * Reset the write pointer of all zones in the range @offset...@offset+@length.
236 * Returns 0 upon success and a negative error code upon failure.
238 static int zbd_reset_wp(struct thread_data *td, struct fio_file *f,
239 uint64_t offset, uint64_t length)
243 if (td->io_ops && td->io_ops->reset_wp)
244 ret = td->io_ops->reset_wp(td, f, offset, length);
246 ret = blkzoned_reset_wp(td, f, offset, length);
248 td_verror(td, errno, "resetting wp failed");
249 log_err("%s: resetting wp for %"PRIu64" sectors at sector %"PRIu64" failed (%d).\n",
250 f->file_name, length >> 9, offset >> 9, errno);
257 * __zbd_reset_zone - reset the write pointer of a single zone
258 * @td: FIO thread data.
259 * @f: FIO file associated with the disk for which to reset a write pointer.
262 * Returns 0 upon success and a negative error code upon failure.
264 * The caller must hold z->mutex.
266 static int __zbd_reset_zone(struct thread_data *td, struct fio_file *f,
267 struct fio_zone_info *z)
269 uint64_t offset = z->start;
270 uint64_t length = (z+1)->start - offset;
271 uint64_t data_in_zone = z->wp - z->start;
277 assert(is_valid_offset(f, offset + length - 1));
279 dprint(FD_ZBD, "%s: resetting wp of zone %u.\n",
280 f->file_name, zbd_zone_idx(f, z));
282 switch (f->zbd_info->model) {
284 case ZBD_HOST_MANAGED:
285 ret = zbd_reset_wp(td, f, offset, length);
293 if (accounting_vdb(td, f)) {
294 pthread_mutex_lock(&f->zbd_info->mutex);
295 f->zbd_info->wp_valid_data_bytes -= data_in_zone;
296 pthread_mutex_unlock(&f->zbd_info->mutex);
301 td->ts.nr_zone_resets++;
307 * zbd_write_zone_put - Remove a zone from the write target zones array.
308 * @td: FIO thread data.
309 * @f: FIO file that has the write zones array to remove.
310 * @zone_idx: Index of the zone to remove.
312 * The caller must hold f->zbd_info->mutex.
314 static void zbd_write_zone_put(struct thread_data *td, const struct fio_file *f,
315 struct fio_zone_info *z)
322 for (zi = 0; zi < f->zbd_info->num_write_zones; zi++) {
323 if (zbd_get_zone(f, f->zbd_info->write_zones[zi]) == z)
326 if (zi == f->zbd_info->num_write_zones)
329 dprint(FD_ZBD, "%s: removing zone %u from write zone array\n",
330 f->file_name, zbd_zone_idx(f, z));
332 memmove(f->zbd_info->write_zones + zi,
333 f->zbd_info->write_zones + zi + 1,
334 (ZBD_MAX_WRITE_ZONES - (zi + 1)) *
335 sizeof(f->zbd_info->write_zones[0]));
337 f->zbd_info->num_write_zones--;
338 td->num_write_zones--;
343 * zbd_reset_zone - reset the write pointer of a single zone and remove the zone
344 * from the array of write zones.
345 * @td: FIO thread data.
346 * @f: FIO file associated with the disk for which to reset a write pointer.
349 * Returns 0 upon success and a negative error code upon failure.
351 * The caller must hold z->mutex.
353 static int zbd_reset_zone(struct thread_data *td, struct fio_file *f,
354 struct fio_zone_info *z)
358 ret = __zbd_reset_zone(td, f, z);
362 pthread_mutex_lock(&f->zbd_info->mutex);
363 zbd_write_zone_put(td, f, z);
364 pthread_mutex_unlock(&f->zbd_info->mutex);
369 * zbd_finish_zone - finish the specified zone
370 * @td: FIO thread data.
371 * @f: FIO file for which to finish a zone
372 * @z: Zone to finish.
374 * Finish the zone at @offset with open or close status.
376 static int zbd_finish_zone(struct thread_data *td, struct fio_file *f,
377 struct fio_zone_info *z)
379 uint64_t offset = z->start;
380 uint64_t length = f->zbd_info->zone_size;
383 switch (f->zbd_info->model) {
385 case ZBD_HOST_MANAGED:
386 if (td->io_ops && td->io_ops->finish_zone)
387 ret = td->io_ops->finish_zone(td, f, offset, length);
389 ret = blkzoned_finish_zone(td, f, offset, length);
396 td_verror(td, errno, "finish zone failed");
397 log_err("%s: finish zone at sector %"PRIu64" failed (%d).\n",
398 f->file_name, offset >> 9, errno);
400 z->wp = (z+1)->start;
407 * zbd_reset_zones - Reset a range of zones.
408 * @td: fio thread data.
409 * @f: fio file for which to reset zones
410 * @zb: first zone to reset.
411 * @ze: first zone not to reset.
413 * Returns 0 upon success and 1 upon failure.
415 static int zbd_reset_zones(struct thread_data *td, struct fio_file *f,
416 struct fio_zone_info *const zb,
417 struct fio_zone_info *const ze)
419 struct fio_zone_info *z;
420 const uint64_t min_bs = td->o.min_bs[DDIR_WRITE];
425 dprint(FD_ZBD, "%s: examining zones %u .. %u\n",
426 f->file_name, zbd_zone_idx(f, zb), zbd_zone_idx(f, ze));
428 for (z = zb; z < ze; z++) {
434 if (z->wp != z->start) {
435 dprint(FD_ZBD, "%s: resetting zone %u\n",
436 f->file_name, zbd_zone_idx(f, z));
437 if (zbd_reset_zone(td, f, z) < 0)
448 * zbd_get_max_open_zones - Get the maximum number of open zones
449 * @td: FIO thread data
450 * @f: FIO file for which to get max open zones
451 * @max_open_zones: Upon success, result will be stored here.
453 * A @max_open_zones value set to zero means no limit.
455 * Returns 0 upon success and a negative error code upon failure.
457 static int zbd_get_max_open_zones(struct thread_data *td, struct fio_file *f,
458 unsigned int *max_open_zones)
462 if (td->io_ops && td->io_ops->get_max_open_zones)
463 ret = td->io_ops->get_max_open_zones(td, f, max_open_zones);
465 ret = blkzoned_get_max_open_zones(td, f, max_open_zones);
467 td_verror(td, errno, "get max open zones failed");
468 log_err("%s: get max open zones failed (%d).\n",
469 f->file_name, errno);
476 * __zbd_write_zone_get - Add a zone to the array of write zones.
477 * @td: fio thread data.
478 * @f: fio file that has the write zones array to add.
479 * @zone_idx: Index of the zone to add.
481 * Do same operation as @zbd_write_zone_get, except it adds the zone at
482 * @zone_idx to write target zones array even when it does not have remainder
483 * space to write one block.
485 static bool __zbd_write_zone_get(struct thread_data *td,
486 const struct fio_file *f,
487 struct fio_zone_info *z)
489 struct zoned_block_device_info *zbdi = f->zbd_info;
490 uint32_t zone_idx = zbd_zone_idx(f, z);
493 if (z->cond == ZBD_ZONE_COND_OFFLINE)
497 * Skip full zones with data verification enabled because resetting a
498 * zone causes data loss and hence causes verification to fail.
500 if (td->o.verify != VERIFY_NONE && zbd_zone_remainder(z) == 0)
504 * zbdi->max_write_zones == 0 means that there is no limit on the
505 * maximum number of write target zones. In this case, do no track write
506 * target zones in zbdi->write_zones array.
508 if (!zbdi->max_write_zones)
511 pthread_mutex_lock(&zbdi->mutex);
515 * If the zone is going to be completely filled by writes
516 * already in-flight, handle it as a full zone instead of a
519 if (!zbd_zone_remainder(z))
525 /* Zero means no limit */
526 if (td->o.job_max_open_zones > 0 &&
527 td->num_write_zones >= td->o.job_max_open_zones)
529 if (zbdi->num_write_zones >= zbdi->max_write_zones)
532 dprint(FD_ZBD, "%s: adding zone %u to write zone array\n",
533 f->file_name, zone_idx);
535 zbdi->write_zones[zbdi->num_write_zones++] = zone_idx;
536 td->num_write_zones++;
541 pthread_mutex_unlock(&zbdi->mutex);
546 * zbd_write_zone_get - Add a zone to the array of write zones.
547 * @td: fio thread data.
548 * @f: fio file that has the open zones to add.
549 * @zone_idx: Index of the zone to add.
551 * Add a ZBD zone to write target zones array, if it is not yet added. Returns
552 * true if either the zone was already added or if the zone was successfully
553 * added to the array without exceeding the maximum number of write zones.
554 * Returns false if the zone was not already added and addition of the zone
555 * would cause the zone limit to be exceeded.
557 static bool zbd_write_zone_get(struct thread_data *td, const struct fio_file *f,
558 struct fio_zone_info *z)
560 const uint64_t min_bs = td->o.min_bs[DDIR_WRITE];
563 * Skip full zones with data verification enabled because resetting a
564 * zone causes data loss and hence causes verification to fail.
566 if (td->o.verify != VERIFY_NONE && zbd_zone_full(f, z, min_bs))
569 return __zbd_write_zone_get(td, f, z);
572 /* Verify whether direct I/O is used for all host-managed zoned block drives. */
573 static bool zbd_using_direct_io(void)
579 if (td->o.odirect || !(td->o.td_ddir & TD_DDIR_WRITE))
581 for_each_file(td, f, j) {
582 if (f->zbd_info && f->filetype == FIO_TYPE_BLOCK &&
583 f->zbd_info->model == ZBD_HOST_MANAGED)
591 /* Whether or not the I/O range for f includes one or more sequential zones */
592 static bool zbd_is_seq_job(const struct fio_file *f)
594 uint32_t zone_idx, zone_idx_b, zone_idx_e;
601 zone_idx_b = zbd_offset_to_zone_idx(f, f->file_offset);
603 zbd_offset_to_zone_idx(f, f->file_offset + f->io_size - 1);
604 for (zone_idx = zone_idx_b; zone_idx <= zone_idx_e; zone_idx++)
605 if (zbd_get_zone(f, zone_idx)->has_wp)
612 * Verify whether the file offset and size parameters are aligned with zone
613 * boundaries. If the file offset is not aligned, align it down to the start of
614 * the zone containing the start offset and align up the file io_size parameter.
616 static bool zbd_zone_align_file_sizes(struct thread_data *td,
619 const struct fio_zone_info *z;
620 uint64_t new_offset, new_end;
624 if (f->file_offset >= f->real_file_size)
626 if (!zbd_is_seq_job(f))
629 if (!td->o.zone_size) {
630 td->o.zone_size = f->zbd_info->zone_size;
631 if (!td->o.zone_size) {
632 log_err("%s: invalid 0 zone size\n",
636 } else if (td->o.zone_size != f->zbd_info->zone_size) {
637 log_err("%s: zonesize %llu does not match the device zone size %"PRIu64".\n",
638 f->file_name, td->o.zone_size,
639 f->zbd_info->zone_size);
643 if (td->o.zone_skip % td->o.zone_size) {
644 log_err("%s: zoneskip %llu is not a multiple of the device zone size %llu.\n",
645 f->file_name, td->o.zone_skip,
650 z = zbd_offset_to_zone(f, f->file_offset);
651 if ((f->file_offset != z->start) &&
652 (td->o.td_ddir != TD_DDIR_READ)) {
653 new_offset = zbd_zone_end(z);
654 if (new_offset >= f->file_offset + f->io_size) {
655 log_info("%s: io_size must be at least one zone\n",
659 log_info("%s: rounded up offset from %"PRIu64" to %"PRIu64"\n",
660 f->file_name, f->file_offset,
662 f->io_size -= (new_offset - f->file_offset);
663 f->file_offset = new_offset;
666 z = zbd_offset_to_zone(f, f->file_offset + f->io_size);
668 if ((td->o.td_ddir != TD_DDIR_READ) &&
669 (f->file_offset + f->io_size != new_end)) {
670 if (new_end <= f->file_offset) {
671 log_info("%s: io_size must be at least one zone\n",
675 log_info("%s: rounded down io_size from %"PRIu64" to %"PRIu64"\n",
676 f->file_name, f->io_size,
677 new_end - f->file_offset);
678 f->io_size = new_end - f->file_offset;
685 * Verify whether offset and size parameters are aligned with zone boundaries.
687 static bool zbd_verify_sizes(void)
693 for_each_file(td, f, j) {
694 if (!zbd_zone_align_file_sizes(td, f))
702 static bool zbd_verify_bs(void)
709 (td->o.min_bs[DDIR_TRIM] != td->o.max_bs[DDIR_TRIM] ||
710 td->o.bssplit_nr[DDIR_TRIM])) {
711 log_info("bsrange and bssplit are not allowed for trim with zonemode=zbd\n");
714 for_each_file(td, f, j) {
720 zone_size = f->zbd_info->zone_size;
721 if (td_trim(td) && td->o.bs[DDIR_TRIM] != zone_size) {
722 log_info("%s: trim block size %llu is not the zone size %"PRIu64"\n",
723 f->file_name, td->o.bs[DDIR_TRIM],
732 static int ilog2(uint64_t i)
744 * Initialize f->zbd_info for devices that are not zoned block devices. This
745 * allows to execute a ZBD workload against a non-ZBD device.
747 static int init_zone_info(struct thread_data *td, struct fio_file *f)
750 struct fio_zone_info *p;
751 uint64_t zone_size = td->o.zone_size;
752 uint64_t zone_capacity = td->o.zone_capacity;
753 struct zoned_block_device_info *zbd_info = NULL;
756 if (zone_size == 0) {
757 log_err("%s: Specifying the zone size is mandatory for regular file/block device with --zonemode=zbd\n\n",
762 if (zone_size < 512) {
763 log_err("%s: zone size must be at least 512 bytes for --zonemode=zbd\n\n",
768 if (zone_capacity == 0)
769 zone_capacity = zone_size;
771 if (zone_capacity > zone_size) {
772 log_err("%s: job parameter zonecapacity %llu is larger than zone size %llu\n",
773 f->file_name, td->o.zone_capacity, td->o.zone_size);
777 if (f->real_file_size < zone_size) {
778 log_err("%s: file/device size %"PRIu64" is smaller than zone size %"PRIu64"\n",
779 f->file_name, f->real_file_size, zone_size);
783 nr_zones = (f->real_file_size + zone_size - 1) / zone_size;
784 zbd_info = scalloc(1, sizeof(*zbd_info) +
785 (nr_zones + 1) * sizeof(zbd_info->zone_info[0]));
789 mutex_init_pshared(&zbd_info->mutex);
790 zbd_info->refcount = 1;
791 p = &zbd_info->zone_info[0];
792 for (i = 0; i < nr_zones; i++, p++) {
793 mutex_init_pshared_with_type(&p->mutex,
794 PTHREAD_MUTEX_RECURSIVE);
795 p->start = i * zone_size;
797 p->type = ZBD_ZONE_TYPE_SWR;
798 p->cond = ZBD_ZONE_COND_EMPTY;
799 p->capacity = zone_capacity;
803 p->start = nr_zones * zone_size;
805 f->zbd_info = zbd_info;
806 f->zbd_info->zone_size = zone_size;
807 f->zbd_info->zone_size_log2 = is_power_of_2(zone_size) ?
808 ilog2(zone_size) : 0;
809 f->zbd_info->nr_zones = nr_zones;
814 * Maximum number of zones to report in one operation.
816 #define ZBD_REPORT_MAX_ZONES 8192U
819 * Parse the device zone report and store it in f->zbd_info. Must be called
820 * only for devices that are zoned, namely those with a model != ZBD_NONE.
822 static int parse_zone_info(struct thread_data *td, struct fio_file *f)
825 struct zbd_zone *zones, *z;
826 struct fio_zone_info *p;
827 uint64_t zone_size, offset, capacity;
828 bool same_zone_cap = true;
829 struct zoned_block_device_info *zbd_info = NULL;
830 int i, j, ret = -ENOMEM;
832 zones = calloc(ZBD_REPORT_MAX_ZONES, sizeof(struct zbd_zone));
836 nrz = zbd_report_zones(td, f, 0, zones, ZBD_REPORT_MAX_ZONES);
839 log_info("fio: report zones (offset 0) failed for %s (%d).\n",
844 zone_size = zones[0].len;
845 capacity = zones[0].capacity;
846 nr_zones = (f->real_file_size + zone_size - 1) / zone_size;
848 if (td->o.zone_size == 0) {
849 td->o.zone_size = zone_size;
850 } else if (td->o.zone_size != zone_size) {
851 log_err("fio: %s job parameter zonesize %llu does not match disk zone size %"PRIu64".\n",
852 f->file_name, td->o.zone_size, zone_size);
857 dprint(FD_ZBD, "Device %s has %d zones of size %"PRIu64" KB\n",
858 f->file_name, nr_zones, zone_size / 1024);
860 zbd_info = scalloc(1, sizeof(*zbd_info) +
861 (nr_zones + 1) * sizeof(zbd_info->zone_info[0]));
864 mutex_init_pshared(&zbd_info->mutex);
865 zbd_info->refcount = 1;
866 p = &zbd_info->zone_info[0];
867 for (offset = 0, j = 0; j < nr_zones;) {
869 for (i = 0; i < nrz; i++, j++, z++, p++) {
870 mutex_init_pshared_with_type(&p->mutex,
871 PTHREAD_MUTEX_RECURSIVE);
873 p->capacity = z->capacity;
874 if (capacity != z->capacity)
875 same_zone_cap = false;
878 case ZBD_ZONE_COND_NOT_WP:
879 case ZBD_ZONE_COND_FULL:
880 p->wp = p->start + p->capacity;
883 assert(z->start <= z->wp);
884 assert(z->wp <= z->start + zone_size);
890 case ZBD_ZONE_TYPE_SWR:
899 if (j > 0 && p->start != p[-1].start + zone_size) {
900 log_info("%s: invalid zone data [%d:%d]: %"PRIu64" + %"PRIu64" != %"PRIu64"\n",
902 p[-1].start, zone_size, p->start);
908 offset = z->start + z->len;
912 nrz = zbd_report_zones(td, f, offset, zones,
913 min((uint32_t)(nr_zones - j),
914 ZBD_REPORT_MAX_ZONES));
917 log_info("fio: report zones (offset %"PRIu64") failed for %s (%d).\n",
918 offset, f->file_name, -ret);
924 zbd_info->zone_info[nr_zones].start = offset;
926 f->zbd_info = zbd_info;
927 f->zbd_info->zone_size = zone_size;
928 f->zbd_info->zone_size_log2 = is_power_of_2(zone_size) ?
929 ilog2(zone_size) : 0;
930 f->zbd_info->nr_zones = nr_zones;
933 dprint(FD_ZBD, "Zone capacity = %"PRIu64" KB\n",
945 static int zbd_set_max_write_zones(struct thread_data *td, struct fio_file *f)
947 struct zoned_block_device_info *zbd = f->zbd_info;
948 unsigned int max_open_zones;
951 if (zbd->model != ZBD_HOST_MANAGED || td->o.ignore_zone_limits) {
952 /* Only host-managed devices have a max open limit */
953 zbd->max_write_zones = td->o.max_open_zones;
957 /* If host-managed, get the max open limit */
958 ret = zbd_get_max_open_zones(td, f, &max_open_zones);
962 if (!max_open_zones) {
963 /* No device limit */
964 zbd->max_write_zones = td->o.max_open_zones;
965 } else if (!td->o.max_open_zones) {
966 /* No user limit. Set limit to device limit */
967 zbd->max_write_zones = max_open_zones;
968 } else if (td->o.max_open_zones <= max_open_zones) {
969 /* Both user limit and dev limit. User limit not too large */
970 zbd->max_write_zones = td->o.max_open_zones;
972 /* Both user limit and dev limit. User limit too large */
973 td_verror(td, EINVAL,
974 "Specified --max_open_zones is too large");
975 log_err("Specified --max_open_zones (%d) is larger than max (%u)\n",
976 td->o.max_open_zones, max_open_zones);
981 /* Ensure that the limit is not larger than FIO's internal limit */
982 if (zbd->max_write_zones > ZBD_MAX_WRITE_ZONES) {
983 td_verror(td, EINVAL, "'max_open_zones' value is too large");
984 log_err("'max_open_zones' value is larger than %u\n",
985 ZBD_MAX_WRITE_ZONES);
989 dprint(FD_ZBD, "%s: using max write zones limit: %"PRIu32"\n",
990 f->file_name, zbd->max_write_zones);
996 * Allocate zone information and store it into f->zbd_info if zonemode=zbd.
998 * Returns 0 upon success and a negative error code upon failure.
1000 static int zbd_create_zone_info(struct thread_data *td, struct fio_file *f)
1002 enum zbd_zoned_model zbd_model;
1005 assert(td->o.zone_mode == ZONE_MODE_ZBD);
1007 ret = zbd_get_zoned_model(td, f, &zbd_model);
1011 switch (zbd_model) {
1012 case ZBD_HOST_AWARE:
1013 case ZBD_HOST_MANAGED:
1014 ret = parse_zone_info(td, f);
1019 ret = init_zone_info(td, f);
1024 td_verror(td, EINVAL, "Unsupported zoned model");
1025 log_err("Unsupported zoned model\n");
1029 assert(f->zbd_info);
1030 f->zbd_info->model = zbd_model;
1032 ret = zbd_set_max_write_zones(td, f);
1034 zbd_free_zone_info(f);
1041 void zbd_free_zone_info(struct fio_file *f)
1045 assert(f->zbd_info);
1047 pthread_mutex_lock(&f->zbd_info->mutex);
1048 refcount = --f->zbd_info->refcount;
1049 pthread_mutex_unlock(&f->zbd_info->mutex);
1051 assert((int32_t)refcount >= 0);
1058 * Initialize f->zbd_info.
1060 * Returns 0 upon success and a negative error code upon failure.
1062 * Note: this function can only work correctly if it is called before the first
1065 static int zbd_init_zone_info(struct thread_data *td, struct fio_file *file)
1067 struct fio_file *f2;
1071 for_each_file(td2, f2, j) {
1072 if (td2 == td && f2 == file)
1074 if (!f2->zbd_info ||
1075 strcmp(f2->file_name, file->file_name) != 0)
1077 file->zbd_info = f2->zbd_info;
1078 file->zbd_info->refcount++;
1083 ret = zbd_create_zone_info(td, file);
1085 td_verror(td, -ret, "zbd_create_zone_info() failed");
1090 int zbd_init_files(struct thread_data *td)
1095 for_each_file(td, f, i) {
1096 if (zbd_init_zone_info(td, f))
1103 void zbd_recalc_options_with_zone_granularity(struct thread_data *td)
1108 for_each_file(td, f, i) {
1109 struct zoned_block_device_info *zbd = f->zbd_info;
1112 /* zonemode=strided doesn't get per-file zone size. */
1113 zone_size = zbd ? zbd->zone_size : td->o.zone_size;
1117 if (td->o.size_nz > 0)
1118 td->o.size = td->o.size_nz * zone_size;
1119 if (td->o.io_size_nz > 0)
1120 td->o.io_size = td->o.io_size_nz * zone_size;
1121 if (td->o.start_offset_nz > 0)
1122 td->o.start_offset = td->o.start_offset_nz * zone_size;
1123 if (td->o.offset_increment_nz > 0)
1124 td->o.offset_increment =
1125 td->o.offset_increment_nz * zone_size;
1126 if (td->o.zone_skip_nz > 0)
1127 td->o.zone_skip = td->o.zone_skip_nz * zone_size;
1131 static uint64_t zbd_verify_and_set_vdb(struct thread_data *td,
1132 const struct fio_file *f)
1134 struct fio_zone_info *zb, *ze, *z;
1135 uint64_t wp_vdb = 0;
1136 struct zoned_block_device_info *zbdi = f->zbd_info;
1138 assert(td->runstate < TD_RUNNING);
1141 if (!accounting_vdb(td, f))
1145 * Ensure that the I/O range includes one or more sequential zones so
1146 * that f->min_zone and f->max_zone have different values.
1148 if (!zbd_is_seq_job(f))
1151 if (zbdi->write_min_zone != zbdi->write_max_zone) {
1152 if (zbdi->write_min_zone != f->min_zone ||
1153 zbdi->write_max_zone != f->max_zone) {
1154 td_verror(td, EINVAL,
1155 "multi-jobs with different write ranges are "
1156 "not supported with zone_reset_threshold");
1157 log_err("multi-jobs with different write ranges are "
1158 "not supported with zone_reset_threshold\n");
1163 zbdi->write_min_zone = f->min_zone;
1164 zbdi->write_max_zone = f->max_zone;
1166 zb = zbd_get_zone(f, f->min_zone);
1167 ze = zbd_get_zone(f, f->max_zone);
1168 for (z = zb; z < ze; z++)
1170 wp_vdb += z->wp - z->start;
1172 zbdi->wp_valid_data_bytes = wp_vdb;
1177 int zbd_setup_files(struct thread_data *td)
1182 if (!zbd_using_direct_io()) {
1183 log_err("Using direct I/O is mandatory for writing to ZBD drives\n\n");
1187 if (!zbd_verify_sizes())
1190 if (!zbd_verify_bs())
1193 if (td->o.experimental_verify) {
1194 log_err("zonemode=zbd does not support experimental verify\n");
1198 for_each_file(td, f, i) {
1199 struct zoned_block_device_info *zbd = f->zbd_info;
1200 struct fio_zone_info *z;
1206 f->min_zone = zbd_offset_to_zone_idx(f, f->file_offset);
1208 zbd_offset_to_zone_idx(f, f->file_offset + f->io_size);
1210 vdb = zbd_verify_and_set_vdb(td, f);
1212 dprint(FD_ZBD, "%s(%s): valid data bytes = %" PRIu64 "\n",
1213 __func__, f->file_name, vdb);
1216 * When all zones in the I/O range are conventional, io_size
1217 * can be smaller than zone size, making min_zone the same
1218 * as max_zone. This is why the assert below needs to be made
1221 if (zbd_is_seq_job(f))
1222 assert(f->min_zone < f->max_zone);
1224 if (td->o.max_open_zones > 0 &&
1225 zbd->max_write_zones != td->o.max_open_zones) {
1226 log_err("Different 'max_open_zones' values\n");
1231 * The per job max open zones limit cannot be used without a
1232 * global max open zones limit. (As the tracking of open zones
1233 * is disabled when there is no global max open zones limit.)
1235 if (td->o.job_max_open_zones && !zbd->max_write_zones) {
1236 log_err("'job_max_open_zones' cannot be used without a global open zones limit\n");
1241 * zbd->max_write_zones is the global limit shared for all jobs
1242 * that target the same zoned block device. Force sync the per
1243 * thread global limit with the actual global limit. (The real
1244 * per thread/job limit is stored in td->o.job_max_open_zones).
1246 td->o.max_open_zones = zbd->max_write_zones;
1248 for (zi = f->min_zone; zi < f->max_zone; zi++) {
1249 z = &zbd->zone_info[zi];
1250 if (z->cond != ZBD_ZONE_COND_IMP_OPEN &&
1251 z->cond != ZBD_ZONE_COND_EXP_OPEN)
1253 if (__zbd_write_zone_get(td, f, z))
1256 * If the number of open zones exceeds specified limits,
1259 log_err("Number of open zones exceeds max_open_zones limit\n");
1268 * Reset zbd_info.write_cnt, the counter that counts down towards the next
1271 static void _zbd_reset_write_cnt(const struct thread_data *td,
1272 const struct fio_file *f)
1274 assert(0 <= td->o.zrf.u.f && td->o.zrf.u.f <= 1);
1276 f->zbd_info->write_cnt = td->o.zrf.u.f ?
1277 min(1.0 / td->o.zrf.u.f, 0.0 + UINT_MAX) : UINT_MAX;
1280 static void zbd_reset_write_cnt(const struct thread_data *td,
1281 const struct fio_file *f)
1283 pthread_mutex_lock(&f->zbd_info->mutex);
1284 _zbd_reset_write_cnt(td, f);
1285 pthread_mutex_unlock(&f->zbd_info->mutex);
1288 static bool zbd_dec_and_reset_write_cnt(const struct thread_data *td,
1289 const struct fio_file *f)
1291 uint32_t write_cnt = 0;
1293 pthread_mutex_lock(&f->zbd_info->mutex);
1294 assert(f->zbd_info->write_cnt);
1295 if (f->zbd_info->write_cnt)
1296 write_cnt = --f->zbd_info->write_cnt;
1298 _zbd_reset_write_cnt(td, f);
1299 pthread_mutex_unlock(&f->zbd_info->mutex);
1301 return write_cnt == 0;
1304 void zbd_file_reset(struct thread_data *td, struct fio_file *f)
1306 struct fio_zone_info *zb, *ze;
1307 bool verify_data_left = false;
1309 if (!f->zbd_info || !td_write(td))
1312 zb = zbd_get_zone(f, f->min_zone);
1313 ze = zbd_get_zone(f, f->max_zone);
1316 * If data verification is enabled reset the affected zones before
1317 * writing any data to avoid that a zone reset has to be issued while
1318 * writing data, which causes data loss.
1320 if (td->o.verify != VERIFY_NONE) {
1321 verify_data_left = td->runstate == TD_VERIFYING ||
1322 td->io_hist_len || td->verify_batch;
1323 if (td->io_hist_len && td->o.verify_backlog)
1325 td->io_hist_len % td->o.verify_backlog;
1326 if (!verify_data_left)
1327 zbd_reset_zones(td, f, zb, ze);
1330 zbd_reset_write_cnt(td, f);
1333 /* Return random zone index for one of the write target zones. */
1334 static uint32_t pick_random_zone_idx(const struct fio_file *f,
1335 const struct io_u *io_u)
1337 return (io_u->offset - f->file_offset) *
1338 f->zbd_info->num_write_zones / f->io_size;
1341 static bool any_io_in_flight(void)
1344 if (td->io_u_in_flight)
1352 * Modify the offset of an I/O unit that does not refer to a zone such that
1353 * in write target zones array. Add a zone to or remove a zone from the lsit if
1354 * necessary. The write target zone is searched across sequential zones.
1355 * This algorithm can only work correctly if all write pointers are
1356 * a multiple of the fio block size. The caller must neither hold z->mutex
1357 * nor f->zbd_info->mutex. Returns with z->mutex held upon success.
1359 static struct fio_zone_info *zbd_convert_to_write_zone(struct thread_data *td,
1362 const uint64_t min_bs = td->o.min_bs[io_u->ddir];
1363 struct fio_file *f = io_u->file;
1364 struct zoned_block_device_info *zbdi = f->zbd_info;
1365 struct fio_zone_info *z;
1366 unsigned int write_zone_idx = -1;
1367 uint32_t zone_idx, new_zone_idx;
1369 bool wait_zone_write;
1371 bool should_retry = true;
1373 assert(is_valid_offset(f, io_u->offset));
1375 if (zbdi->max_write_zones || td->o.job_max_open_zones) {
1377 * This statement accesses zbdi->write_zones[] on purpose
1380 zone_idx = zbdi->write_zones[pick_random_zone_idx(f, io_u)];
1382 zone_idx = zbd_offset_to_zone_idx(f, io_u->offset);
1384 if (zone_idx < f->min_zone)
1385 zone_idx = f->min_zone;
1386 else if (zone_idx >= f->max_zone)
1387 zone_idx = f->max_zone - 1;
1390 "%s(%s): starting from zone %d (offset %lld, buflen %lld)\n",
1391 __func__, f->file_name, zone_idx, io_u->offset, io_u->buflen);
1394 * Since z->mutex is the outer lock and zbdi->mutex the inner
1395 * lock it can happen that the state of the zone with index zone_idx
1396 * has changed after 'z' has been assigned and before zbdi->mutex
1397 * has been obtained. Hence the loop.
1402 z = zbd_get_zone(f, zone_idx);
1404 zone_lock(td, f, z);
1406 pthread_mutex_lock(&zbdi->mutex);
1409 if (z->cond != ZBD_ZONE_COND_OFFLINE &&
1410 zbdi->max_write_zones == 0 &&
1411 td->o.job_max_open_zones == 0)
1413 if (zbdi->num_write_zones == 0) {
1414 dprint(FD_ZBD, "%s(%s): no zone is write target\n",
1415 __func__, f->file_name);
1416 goto choose_other_zone;
1421 * Array of write target zones is per-device, shared across all
1422 * threads. Start with quasi-random candidate zone. Ignore
1423 * zones which don't belong to thread's offset/size area.
1425 write_zone_idx = pick_random_zone_idx(f, io_u);
1426 assert(!write_zone_idx ||
1427 write_zone_idx < zbdi->num_write_zones);
1428 tmp_idx = write_zone_idx;
1430 for (i = 0; i < zbdi->num_write_zones; i++) {
1433 if (tmp_idx >= zbdi->num_write_zones)
1435 tmpz = zbdi->write_zones[tmp_idx];
1436 if (f->min_zone <= tmpz && tmpz < f->max_zone) {
1437 write_zone_idx = tmp_idx;
1438 goto found_candidate_zone;
1444 dprint(FD_ZBD, "%s(%s): no candidate zone\n",
1445 __func__, f->file_name);
1447 pthread_mutex_unlock(&zbdi->mutex);
1454 found_candidate_zone:
1455 new_zone_idx = zbdi->write_zones[write_zone_idx];
1456 if (new_zone_idx == zone_idx)
1458 zone_idx = new_zone_idx;
1460 pthread_mutex_unlock(&zbdi->mutex);
1466 /* Both z->mutex and zbdi->mutex are held. */
1469 if (zbd_zone_remainder(z) >= min_bs) {
1470 pthread_mutex_unlock(&zbdi->mutex);
1475 /* Check if number of write target zones reaches one of limits. */
1477 zbdi->num_write_zones == f->max_zone - f->min_zone ||
1478 (zbdi->max_write_zones &&
1479 zbdi->num_write_zones == zbdi->max_write_zones) ||
1480 (td->o.job_max_open_zones &&
1481 td->num_write_zones == td->o.job_max_open_zones);
1483 pthread_mutex_unlock(&zbdi->mutex);
1485 /* Only z->mutex is held. */
1488 * When number of write target zones reaches to one of limits, wait for
1489 * zone write completion to one of them before trying a new zone.
1491 if (wait_zone_write) {
1493 "%s(%s): quiesce to remove a zone from write target zones array\n",
1494 __func__, f->file_name);
1499 /* Zone 'z' is full, so try to choose a new zone. */
1500 for (i = f->io_size / zbdi->zone_size; i > 0; i--) {
1505 if (!is_valid_offset(f, z->start)) {
1507 zone_idx = f->min_zone;
1508 z = zbd_get_zone(f, zone_idx);
1510 assert(is_valid_offset(f, z->start));
1513 zone_lock(td, f, z);
1516 if (zbd_write_zone_get(td, f, z))
1520 /* Only z->mutex is held. */
1522 /* Check whether the write fits in any of the write target zones. */
1523 pthread_mutex_lock(&zbdi->mutex);
1524 for (i = 0; i < zbdi->num_write_zones; i++) {
1525 zone_idx = zbdi->write_zones[i];
1526 if (zone_idx < f->min_zone || zone_idx >= f->max_zone)
1528 pthread_mutex_unlock(&zbdi->mutex);
1531 z = zbd_get_zone(f, zone_idx);
1533 zone_lock(td, f, z);
1534 if (zbd_zone_remainder(z) >= min_bs)
1536 pthread_mutex_lock(&zbdi->mutex);
1540 * When any I/O is in-flight or when all I/Os in-flight get completed,
1541 * the I/Os might have removed zones from the write target array then
1542 * retry the steps to choose a zone. Before retry, call io_u_quiesce()
1543 * to complete in-flight writes.
1545 in_flight = any_io_in_flight();
1546 if (in_flight || should_retry) {
1548 "%s(%s): wait zone write and retry write target zone selection\n",
1549 __func__, f->file_name);
1550 should_retry = in_flight;
1551 pthread_mutex_unlock(&zbdi->mutex);
1554 zone_lock(td, f, z);
1558 pthread_mutex_unlock(&zbdi->mutex);
1562 dprint(FD_ZBD, "%s(%s): did not choose another write zone\n",
1563 __func__, f->file_name);
1568 dprint(FD_ZBD, "%s(%s): returning zone %d\n",
1569 __func__, f->file_name, zone_idx);
1571 io_u->offset = z->start;
1573 assert(z->cond != ZBD_ZONE_COND_OFFLINE);
1579 * Find another zone which has @min_bytes of readable data. Search in zones
1580 * @zb + 1 .. @zl. For random workload, also search in zones @zb - 1 .. @zf.
1582 * Either returns NULL or returns a zone pointer. When the zone has write
1583 * pointer, hold the mutex for the zone.
1585 static struct fio_zone_info *
1586 zbd_find_zone(struct thread_data *td, struct io_u *io_u, uint64_t min_bytes,
1587 struct fio_zone_info *zb, struct fio_zone_info *zl)
1589 struct fio_file *f = io_u->file;
1590 struct fio_zone_info *z1, *z2;
1591 const struct fio_zone_info *const zf = zbd_get_zone(f, f->min_zone);
1594 * Skip to the next non-empty zone in case of sequential I/O and to
1595 * the nearest non-empty zone in case of random I/O.
1597 for (z1 = zb + 1, z2 = zb - 1; z1 < zl || z2 >= zf; z1++, z2--) {
1598 if (z1 < zl && z1->cond != ZBD_ZONE_COND_OFFLINE) {
1600 zone_lock(td, f, z1);
1601 if (z1->start + min_bytes <= z1->wp)
1605 } else if (!td_random(td)) {
1609 if (td_random(td) && z2 >= zf &&
1610 z2->cond != ZBD_ZONE_COND_OFFLINE) {
1612 zone_lock(td, f, z2);
1613 if (z2->start + min_bytes <= z2->wp)
1621 "%s: no zone has %"PRIu64" bytes of readable data\n",
1622 f->file_name, min_bytes);
1628 * zbd_end_zone_io - update zone status at command completion
1630 * @z: zone info pointer
1632 * If the write command made the zone full, remove it from the write target
1635 * The caller must hold z->mutex.
1637 static void zbd_end_zone_io(struct thread_data *td, const struct io_u *io_u,
1638 struct fio_zone_info *z)
1640 const struct fio_file *f = io_u->file;
1642 if (io_u->ddir == DDIR_WRITE &&
1643 io_u->offset + io_u->buflen >= zbd_zone_capacity_end(z)) {
1644 pthread_mutex_lock(&f->zbd_info->mutex);
1645 zbd_write_zone_put(td, f, z);
1646 pthread_mutex_unlock(&f->zbd_info->mutex);
1651 * zbd_queue_io - update the write pointer of a sequential zone
1653 * @success: Whether or not the I/O unit has been queued successfully
1654 * @q: queueing status (busy, completed or queued).
1656 * For write and trim operations, update the write pointer of the I/O unit
1659 static void zbd_queue_io(struct thread_data *td, struct io_u *io_u, int q,
1662 const struct fio_file *f = io_u->file;
1663 struct zoned_block_device_info *zbd_info = f->zbd_info;
1664 struct fio_zone_info *z;
1669 z = zbd_offset_to_zone(f, io_u->offset);
1676 "%s: queued I/O (%lld, %llu) for zone %u\n",
1677 f->file_name, io_u->offset, io_u->buflen, zbd_zone_idx(f, z));
1679 switch (io_u->ddir) {
1681 zone_end = min((uint64_t)(io_u->offset + io_u->buflen),
1682 zbd_zone_capacity_end(z));
1685 * z->wp > zone_end means that one or more I/O errors
1688 if (accounting_vdb(td, f) && z->wp <= zone_end) {
1689 pthread_mutex_lock(&zbd_info->mutex);
1690 zbd_info->wp_valid_data_bytes += zone_end - z->wp;
1691 pthread_mutex_unlock(&zbd_info->mutex);
1699 if (q == FIO_Q_COMPLETED && !io_u->error)
1700 zbd_end_zone_io(td, io_u, z);
1703 if (!success || q != FIO_Q_QUEUED) {
1704 /* BUSY or COMPLETED: unlock the zone */
1706 io_u->zbd_put_io = NULL;
1711 * zbd_put_io - Unlock an I/O unit target zone lock
1714 static void zbd_put_io(struct thread_data *td, const struct io_u *io_u)
1716 const struct fio_file *f = io_u->file;
1717 struct zoned_block_device_info *zbd_info = f->zbd_info;
1718 struct fio_zone_info *z;
1722 z = zbd_offset_to_zone(f, io_u->offset);
1726 "%s: terminate I/O (%lld, %llu) for zone %u\n",
1727 f->file_name, io_u->offset, io_u->buflen, zbd_zone_idx(f, z));
1729 zbd_end_zone_io(td, io_u, z);
1735 * Windows and MacOS do not define this.
1738 #define EREMOTEIO 121 /* POSIX value */
1741 bool zbd_unaligned_write(int error_code)
1743 switch (error_code) {
1752 * setup_zbd_zone_mode - handle zoneskip as necessary for ZBD drives
1753 * @td: FIO thread data.
1754 * @io_u: FIO I/O unit.
1756 * For sequential workloads, change the file offset to skip zoneskip bytes when
1757 * no more IO can be performed in the current zone.
1758 * - For read workloads, zoneskip is applied when the io has reached the end of
1759 * the zone or the zone write position (when td->o.read_beyond_wp is false).
1760 * - For write workloads, zoneskip is applied when the zone is full.
1761 * This applies only to read and write operations.
1763 void setup_zbd_zone_mode(struct thread_data *td, struct io_u *io_u)
1765 struct fio_file *f = io_u->file;
1766 enum fio_ddir ddir = io_u->ddir;
1767 struct fio_zone_info *z;
1769 assert(td->o.zone_mode == ZONE_MODE_ZBD);
1770 assert(td->o.zone_size);
1771 assert(f->zbd_info);
1773 z = zbd_offset_to_zone(f, f->last_pos[ddir]);
1776 * When the zone capacity is smaller than the zone size and the I/O is
1777 * sequential write, skip to zone end if the latest position is at the
1778 * zone capacity limit.
1780 if (z->capacity < f->zbd_info->zone_size &&
1781 !td_random(td) && ddir == DDIR_WRITE &&
1782 f->last_pos[ddir] >= zbd_zone_capacity_end(z)) {
1784 "%s: Jump from zone capacity limit to zone end:"
1785 " (%"PRIu64" -> %"PRIu64") for zone %u (%"PRIu64")\n",
1786 f->file_name, f->last_pos[ddir],
1787 zbd_zone_end(z), zbd_zone_idx(f, z), z->capacity);
1788 td->io_skip_bytes += zbd_zone_end(z) - f->last_pos[ddir];
1789 f->last_pos[ddir] = zbd_zone_end(z);
1793 * zone_skip is valid only for sequential workloads.
1795 if (td_random(td) || !td->o.zone_skip)
1799 * It is time to switch to a new zone if:
1800 * - zone_bytes == zone_size bytes have already been accessed
1801 * - The last position reached the end of the current zone.
1802 * - For reads with td->o.read_beyond_wp == false, the last position
1803 * reached the zone write pointer.
1805 if (td->zone_bytes >= td->o.zone_size ||
1806 f->last_pos[ddir] >= zbd_zone_end(z) ||
1807 (ddir == DDIR_READ &&
1808 (!td->o.read_beyond_wp) && f->last_pos[ddir] >= z->wp)) {
1813 f->file_offset += td->o.zone_size + td->o.zone_skip;
1816 * Wrap from the beginning, if we exceed the file size
1818 if (f->file_offset >= f->real_file_size)
1819 f->file_offset = get_start_offset(td, f);
1821 f->last_pos[ddir] = f->file_offset;
1822 td->io_skip_bytes += td->o.zone_skip;
1827 * zbd_adjust_ddir - Adjust an I/O direction for zonemode=zbd.
1829 * @td: FIO thread data.
1830 * @io_u: FIO I/O unit.
1831 * @ddir: I/O direction before adjustment.
1833 * Return adjusted I/O direction.
1835 enum fio_ddir zbd_adjust_ddir(struct thread_data *td, struct io_u *io_u,
1839 * In case read direction is chosen for the first random I/O, fio with
1840 * zonemode=zbd stops because no data can be read from zoned block
1841 * devices with all empty zones. Overwrite the first I/O direction as
1842 * write to make sure data to read exists.
1844 assert(io_u->file->zbd_info);
1845 if (ddir != DDIR_READ || !td_rw(td))
1848 if (io_u->file->last_start[DDIR_WRITE] != -1ULL || td->o.read_beyond_wp)
1855 * zbd_adjust_block - adjust the offset and length as necessary for ZBD drives
1856 * @td: FIO thread data.
1857 * @io_u: FIO I/O unit.
1859 * Locking strategy: returns with z->mutex locked if and only if z refers
1860 * to a sequential zone and if io_u_accept is returned. z is the zone that
1861 * corresponds to io_u->offset at the end of this function.
1863 enum io_u_action zbd_adjust_block(struct thread_data *td, struct io_u *io_u)
1865 struct fio_file *f = io_u->file;
1866 struct zoned_block_device_info *zbdi = f->zbd_info;
1867 struct fio_zone_info *zb, *zl, *orig_zb;
1868 uint32_t orig_len = io_u->buflen;
1869 uint64_t min_bs = td->o.min_bs[io_u->ddir];
1875 assert(is_valid_offset(f, io_u->offset));
1876 assert(io_u->buflen);
1878 zb = zbd_offset_to_zone(f, io_u->offset);
1882 /* Accept non-write I/Os for conventional zones. */
1883 if (io_u->ddir != DDIR_WRITE)
1887 * Make sure that writes to conventional zones
1888 * don't cross over to any sequential zones.
1890 if (!(zb + 1)->has_wp ||
1891 io_u->offset + io_u->buflen <= (zb + 1)->start)
1894 if (io_u->offset + min_bs > (zb + 1)->start) {
1896 "%s: off=%llu + min_bs=%"PRIu64" > next zone %"PRIu64"\n",
1897 f->file_name, io_u->offset,
1898 min_bs, (zb + 1)->start);
1900 zb->start + (zb + 1)->start - io_u->offset;
1901 new_len = min(io_u->buflen,
1902 (zb + 1)->start - io_u->offset);
1904 new_len = (zb + 1)->start - io_u->offset;
1907 io_u->buflen = new_len / min_bs * min_bs;
1913 * Accept the I/O offset for reads if reading beyond the write pointer
1916 if (zb->cond != ZBD_ZONE_COND_OFFLINE &&
1917 io_u->ddir == DDIR_READ && td->o.read_beyond_wp)
1920 zone_lock(td, f, zb);
1922 switch (io_u->ddir) {
1924 if (td->runstate == TD_VERIFYING && td_write(td))
1928 * Check that there is enough written data in the zone to do an
1929 * I/O of at least min_bs B. If there isn't, find a new zone for
1932 range = zb->cond != ZBD_ZONE_COND_OFFLINE ?
1933 zb->wp - zb->start : 0;
1934 if (range < min_bs ||
1935 ((!td_random(td)) && (io_u->offset + min_bs > zb->wp))) {
1937 zl = zbd_get_zone(f, f->max_zone);
1938 zb = zbd_find_zone(td, io_u, min_bs, zb, zl);
1941 "%s: zbd_find_zone(%lld, %llu) failed\n",
1942 f->file_name, io_u->offset,
1947 * zbd_find_zone() returned a zone with a range of at
1950 range = zb->wp - zb->start;
1951 assert(range >= min_bs);
1954 io_u->offset = zb->start;
1958 * Make sure the I/O is within the zone valid data range while
1959 * maximizing the I/O size and preserving randomness.
1961 if (range <= io_u->buflen)
1962 io_u->offset = zb->start;
1963 else if (td_random(td))
1964 io_u->offset = zb->start +
1965 ((io_u->offset - orig_zb->start) %
1966 (range - io_u->buflen)) / min_bs * min_bs;
1969 * When zbd_find_zone() returns a conventional zone,
1970 * we can simply accept the new i/o offset here.
1976 * Make sure the I/O does not cross over the zone wp position.
1978 new_len = min((unsigned long long)io_u->buflen,
1979 (unsigned long long)(zb->wp - io_u->offset));
1980 new_len = new_len / min_bs * min_bs;
1981 if (new_len < io_u->buflen) {
1982 io_u->buflen = new_len;
1983 dprint(FD_IO, "Changed length from %u into %llu\n",
1984 orig_len, io_u->buflen);
1987 assert(zb->start <= io_u->offset);
1988 assert(io_u->offset + io_u->buflen <= zb->wp);
1993 if (io_u->buflen > zbdi->zone_size) {
1994 td_verror(td, EINVAL, "I/O buflen exceeds zone size");
1996 "%s: I/O buflen %llu exceeds zone size %"PRIu64"\n",
1997 f->file_name, io_u->buflen, zbdi->zone_size);
2002 if (zbd_zone_remainder(zb) > 0 &&
2003 zbd_zone_remainder(zb) < min_bs) {
2004 pthread_mutex_lock(&f->zbd_info->mutex);
2005 zbd_write_zone_put(td, f, zb);
2006 pthread_mutex_unlock(&f->zbd_info->mutex);
2008 "%s: finish zone %d\n",
2009 f->file_name, zbd_zone_idx(f, zb));
2011 zbd_finish_zone(td, f, zb);
2012 if (zbd_zone_idx(f, zb) + 1 >= f->max_zone) {
2018 /* Find the next write pointer zone */
2021 if (zbd_zone_idx(f, zb) >= f->max_zone)
2022 zb = zbd_get_zone(f, f->min_zone);
2023 } while (!zb->has_wp);
2025 zone_lock(td, f, zb);
2028 if (!zbd_write_zone_get(td, f, zb)) {
2030 zb = zbd_convert_to_write_zone(td, io_u);
2032 dprint(FD_IO, "%s: can't convert to write target zone",
2038 if (zbd_zone_remainder(zb) > 0 &&
2039 zbd_zone_remainder(zb) < min_bs)
2042 /* Check whether the zone reset threshold has been exceeded */
2043 if (td->o.zrf.u.f) {
2044 if (zbdi->wp_valid_data_bytes >=
2045 f->io_size * td->o.zrt.u.f &&
2046 zbd_dec_and_reset_write_cnt(td, f))
2050 /* Reset the zone pointer if necessary */
2051 if (zb->reset_zone || zbd_zone_full(f, zb, min_bs)) {
2052 if (td->o.verify != VERIFY_NONE) {
2054 * Unset io-u->file to tell get_next_verify()
2055 * that this IO is not requeue.
2058 if (!get_next_verify(td, io_u)) {
2066 * Since previous write requests may have been submitted
2067 * asynchronously and since we will submit the zone
2068 * reset synchronously, wait until previously submitted
2069 * write requests have completed before issuing a
2074 if (__zbd_reset_zone(td, f, zb) < 0)
2077 if (zb->capacity < min_bs) {
2078 td_verror(td, EINVAL, "ZCAP is less min_bs");
2079 log_err("zone capacity %"PRIu64" smaller than minimum block size %"PRIu64"\n",
2080 zb->capacity, min_bs);
2085 /* Make writes occur at the write pointer */
2086 assert(!zbd_zone_full(f, zb, min_bs));
2087 io_u->offset = zb->wp;
2088 if (!is_valid_offset(f, io_u->offset)) {
2089 td_verror(td, EINVAL, "invalid WP value");
2090 dprint(FD_ZBD, "%s: dropped request with offset %llu\n",
2091 f->file_name, io_u->offset);
2096 * Make sure that the buflen is a multiple of the minimal
2097 * block size. Give up if shrinking would make the request too
2100 new_len = min((unsigned long long)io_u->buflen,
2101 zbd_zone_capacity_end(zb) - io_u->offset);
2102 new_len = new_len / min_bs * min_bs;
2103 if (new_len == io_u->buflen)
2105 if (new_len >= min_bs) {
2106 io_u->buflen = new_len;
2107 dprint(FD_IO, "Changed length from %u into %llu\n",
2108 orig_len, io_u->buflen);
2112 td_verror(td, EIO, "zone remainder too small");
2113 log_err("zone remainder %lld smaller than min block size %"PRIu64"\n",
2114 (zbd_zone_capacity_end(zb) - io_u->offset), min_bs);
2119 /* Check random trim targets a non-empty zone */
2120 if (!td_random(td) || zb->wp > zb->start)
2123 /* Find out a non-empty zone to trim */
2125 zl = zbd_get_zone(f, f->max_zone);
2126 zb = zbd_find_zone(td, io_u, 1, zb, zl);
2128 io_u->offset = zb->start;
2129 dprint(FD_ZBD, "%s: found new zone(%lld) for trim\n",
2130 f->file_name, io_u->offset);
2139 case DDIR_SYNC_FILE_RANGE:
2150 assert(zb->cond != ZBD_ZONE_COND_OFFLINE);
2151 assert(!io_u->zbd_queue_io);
2152 assert(!io_u->zbd_put_io);
2154 io_u->zbd_queue_io = zbd_queue_io;
2155 io_u->zbd_put_io = zbd_put_io;
2158 * Since we return with the zone lock still held,
2159 * add an annotation to let Coverity know that it
2162 /* coverity[missing_unlock] */
2167 if (zb && zb->has_wp)
2173 /* Return a string with ZBD statistics */
2174 char *zbd_write_status(const struct thread_stat *ts)
2178 if (asprintf(&res, "; %"PRIu64" zone resets", ts->nr_zone_resets) < 0)
2184 * zbd_do_io_u_trim - If reset zone is applicable, do reset zone instead of trim
2186 * @td: FIO thread data.
2187 * @io_u: FIO I/O unit.
2189 * It is assumed that z->mutex is already locked.
2190 * Return io_u_completed when reset zone succeeds. Return 0 when the target zone
2191 * does not have write pointer. On error, return negative errno.
2193 int zbd_do_io_u_trim(struct thread_data *td, struct io_u *io_u)
2195 struct fio_file *f = io_u->file;
2196 struct fio_zone_info *z;
2199 z = zbd_offset_to_zone(f, io_u->offset);
2203 if (io_u->offset != z->start) {
2204 log_err("Trim offset not at zone start (%lld)\n",
2209 ret = zbd_reset_zone((struct thread_data *)td, f, z);
2213 return io_u_completed;