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_full - verify whether a minimum number of bytes remain in a zone
76 * @z: zone info pointer.
77 * @required: minimum number of bytes that must remain in a zone.
79 * The caller must hold z->mutex.
81 static bool zbd_zone_full(const struct fio_file *f, struct fio_zone_info *z,
84 assert((required & 511) == 0);
87 z->wp + required > zbd_zone_capacity_end(z);
90 static void zone_lock(struct thread_data *td, const struct fio_file *f,
91 struct fio_zone_info *z)
93 struct zoned_block_device_info *zbd = f->zbd_info;
94 uint32_t nz = z - zbd->zone_info;
96 /* A thread should never lock zones outside its working area. */
97 assert(f->min_zone <= nz && nz < f->max_zone);
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
110 if (pthread_mutex_trylock(&z->mutex) != 0) {
111 if (!td_ioengine_flagged(td, FIO_SYNCIO))
113 pthread_mutex_lock(&z->mutex);
117 static inline void zone_unlock(struct fio_zone_info *z)
122 ret = pthread_mutex_unlock(&z->mutex);
126 static inline struct fio_zone_info *zbd_get_zone(const struct fio_file *f,
127 unsigned int zone_idx)
129 return &f->zbd_info->zone_info[zone_idx];
132 static inline struct fio_zone_info *
133 zbd_offset_to_zone(const struct fio_file *f, uint64_t offset)
135 return zbd_get_zone(f, zbd_offset_to_zone_idx(f, offset));
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
143 static int zbd_get_zoned_model(struct thread_data *td, struct fio_file *f,
144 enum zbd_zoned_model *model)
148 if (f->filetype == FIO_TYPE_PIPE) {
149 log_err("zonemode=zbd does not support pipes\n");
153 /* If regular file, always emulate zones inside the file. */
154 if (f->filetype == FIO_TYPE_FILE) {
159 if (td->io_ops && td->io_ops->get_zoned_model)
160 ret = td->io_ops->get_zoned_model(td, f, model);
162 ret = blkzoned_get_zoned_model(td, f, model);
164 td_verror(td, errno, "get zoned model failed");
165 log_err("%s: get zoned model failed (%d).\n",
166 f->file_name, errno);
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
180 * Get zone information into @zones starting from the zone at offset @offset
181 * for the device specified by @f.
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.
187 static 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)
193 if (td->io_ops && td->io_ops->report_zones)
194 ret = td->io_ops->report_zones(td, f, offset, zones, nr_zones);
196 ret = blkzoned_report_zones(td, f, offset, zones, nr_zones);
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);
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
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.
221 static int zbd_reset_wp(struct thread_data *td, struct fio_file *f,
222 uint64_t offset, uint64_t length)
226 if (td->io_ops && td->io_ops->reset_wp)
227 ret = td->io_ops->reset_wp(td, f, offset, length);
229 ret = blkzoned_reset_wp(td, f, offset, length);
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);
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.
245 * Returns 0 upon success and a negative error code upon failure.
247 * The caller must hold z->mutex.
249 static int zbd_reset_zone(struct thread_data *td, struct fio_file *f,
250 struct fio_zone_info *z)
252 uint64_t offset = z->start;
253 uint64_t length = (z+1)->start - offset;
254 uint64_t data_in_zone = z->wp - z->start;
260 assert(is_valid_offset(f, offset + length - 1));
262 dprint(FD_ZBD, "%s: resetting wp of zone %u.\n",
263 f->file_name, zbd_zone_idx(f, z));
265 switch (f->zbd_info->model) {
267 case ZBD_HOST_MANAGED:
268 ret = zbd_reset_wp(td, f, offset, length);
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);
284 td->ts.nr_zone_resets++;
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.
295 * The caller must hold f->zbd_info->mutex.
297 static void zbd_close_zone(struct thread_data *td, const struct fio_file *f,
298 struct fio_zone_info *z)
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)
309 if (ozi == f->zbd_info->num_open_zones)
312 dprint(FD_ZBD, "%s: closing zone %u\n",
313 f->file_name, zbd_zone_idx(f, z));
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]));
320 f->zbd_info->num_open_zones--;
321 td->num_open_zones--;
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.
332 * Returns 0 upon success and 1 upon failure.
334 static 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)
338 struct fio_zone_info *z;
339 const uint64_t min_bs = td->o.min_bs[DDIR_WRITE];
344 dprint(FD_ZBD, "%s: examining zones %u .. %u\n",
345 f->file_name, zbd_zone_idx(f, zb), zbd_zone_idx(f, ze));
347 for (z = zb; z < ze; z++) {
352 pthread_mutex_lock(&f->zbd_info->mutex);
353 zbd_close_zone(td, f, z);
354 pthread_mutex_unlock(&f->zbd_info->mutex);
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)
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.
375 * A @max_open_zones value set to zero means no limit.
377 * Returns 0 upon success and a negative error code upon failure.
379 static int zbd_get_max_open_zones(struct thread_data *td, struct fio_file *f,
380 unsigned int *max_open_zones)
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);
387 ret = blkzoned_get_max_open_zones(td, f, max_open_zones);
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);
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.
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
409 static bool zbd_open_zone(struct thread_data *td, const struct fio_file *f,
410 struct fio_zone_info *z)
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);
417 if (z->cond == ZBD_ZONE_COND_OFFLINE)
421 * Skip full zones with data verification enabled because resetting a
422 * zone causes data loss and hence causes verification to fail.
424 if (td->o.verify != VERIFY_NONE && zbd_zone_full(f, z, min_bs))
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.
432 if (!zbdi->max_open_zones)
435 pthread_mutex_lock(&zbdi->mutex);
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
443 if (z->wp >= zbd_zone_capacity_end(z))
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)
453 if (zbdi->num_open_zones >= zbdi->max_open_zones)
456 dprint(FD_ZBD, "%s: opening zone %u\n",
457 f->file_name, zone_idx);
459 zbdi->open_zones[zbdi->num_open_zones++] = zone_idx;
460 td->num_open_zones++;
465 pthread_mutex_unlock(&zbdi->mutex);
469 /* Verify whether direct I/O is used for all host-managed zoned block drives. */
470 static bool zbd_using_direct_io(void)
472 struct thread_data *td;
477 if (td->o.odirect || !(td->o.td_ddir & TD_DDIR_WRITE))
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)
489 /* Whether or not the I/O range for f includes one or more sequential zones */
490 static bool zbd_is_seq_job(struct fio_file *f)
492 uint32_t zone_idx, zone_idx_b, zone_idx_e;
499 zone_idx_b = zbd_offset_to_zone_idx(f, f->file_offset);
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)
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.
514 static bool zbd_zone_align_file_sizes(struct thread_data *td,
517 const struct fio_zone_info *z;
518 uint64_t new_offset, new_end;
522 if (f->file_offset >= f->real_file_size)
524 if (!zbd_is_seq_job(f))
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",
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);
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,
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",
557 log_info("%s: rounded up offset from %"PRIu64" to %"PRIu64"\n",
558 f->file_name, f->file_offset,
560 f->io_size -= (new_offset - f->file_offset);
561 f->file_offset = new_offset;
564 z = zbd_offset_to_zone(f, f->file_offset + f->io_size);
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",
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;
583 * Verify whether offset and size parameters are aligned with zone boundaries.
585 static bool zbd_verify_sizes(void)
587 struct thread_data *td;
592 for_each_file(td, f, j) {
593 if (!zbd_zone_align_file_sizes(td, f))
601 static bool zbd_verify_bs(void)
603 struct thread_data *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");
614 for_each_file(td, f, j) {
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],
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],
641 static int ilog2(uint64_t i)
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.
656 static int init_zone_info(struct thread_data *td, struct fio_file *f)
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;
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",
671 if (zone_size < 512) {
672 log_err("%s: zone size must be at least 512 bytes for --zonemode=zbd\n\n",
677 if (zone_capacity == 0)
678 zone_capacity = zone_size;
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);
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);
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]));
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;
706 p->type = ZBD_ZONE_TYPE_SWR;
707 p->cond = ZBD_ZONE_COND_EMPTY;
708 p->capacity = zone_capacity;
712 p->start = nr_zones * zone_size;
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;
723 * Maximum number of zones to report in one operation.
725 #define ZBD_REPORT_MAX_ZONES 8192U
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.
731 static int parse_zone_info(struct thread_data *td, struct fio_file *f)
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;
740 zones = calloc(ZBD_REPORT_MAX_ZONES, sizeof(struct zbd_zone));
744 nrz = zbd_report_zones(td, f, 0, zones, ZBD_REPORT_MAX_ZONES);
747 log_info("fio: report zones (offset 0) failed for %s (%d).\n",
752 zone_size = zones[0].len;
753 nr_zones = (f->real_file_size + zone_size - 1) / zone_size;
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);
764 dprint(FD_ZBD, "Device %s has %d zones of size %"PRIu64" KB\n",
765 f->file_name, nr_zones, zone_size / 1024);
767 zbd_info = scalloc(1, sizeof(*zbd_info) +
768 (nr_zones + 1) * sizeof(zbd_info->zone_info[0]));
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;) {
776 for (i = 0; i < nrz; i++, j++, z++, p++) {
777 mutex_init_pshared_with_type(&p->mutex,
778 PTHREAD_MUTEX_RECURSIVE);
780 p->capacity = z->capacity;
783 case ZBD_ZONE_COND_NOT_WP:
784 case ZBD_ZONE_COND_FULL:
785 p->wp = p->start + p->capacity;
788 assert(z->start <= z->wp);
789 assert(z->wp <= z->start + zone_size);
795 case ZBD_ZONE_TYPE_SWR:
804 if (j > 0 && p->start != p[-1].start + zone_size) {
805 log_info("%s: invalid zone data\n",
812 offset = z->start + z->len;
816 nrz = zbd_report_zones(td, f, offset, zones,
817 min((uint32_t)(nr_zones - j),
818 ZBD_REPORT_MAX_ZONES));
821 log_info("fio: report zones (offset %"PRIu64") failed for %s (%d).\n",
822 offset, f->file_name, -ret);
828 zbd_info->zone_info[nr_zones].start = offset;
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;
844 static int zbd_set_max_open_zones(struct thread_data *td, struct fio_file *f)
846 struct zoned_block_device_info *zbd = f->zbd_info;
847 unsigned int max_open_zones;
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;
856 /* If host-managed, get the max open limit */
857 ret = zbd_get_max_open_zones(td, f, &max_open_zones);
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;
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);
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",
888 dprint(FD_ZBD, "%s: using max open zones limit: %"PRIu32"\n",
889 f->file_name, zbd->max_open_zones);
895 * Allocate zone information and store it into f->zbd_info if zonemode=zbd.
897 * Returns 0 upon success and a negative error code upon failure.
899 static int zbd_create_zone_info(struct thread_data *td, struct fio_file *f)
901 enum zbd_zoned_model zbd_model;
904 assert(td->o.zone_mode == ZONE_MODE_ZBD);
906 ret = zbd_get_zoned_model(td, f, &zbd_model);
912 case ZBD_HOST_MANAGED:
913 ret = parse_zone_info(td, f);
918 ret = init_zone_info(td, f);
923 td_verror(td, EINVAL, "Unsupported zoned model");
924 log_err("Unsupported zoned model\n");
929 f->zbd_info->model = zbd_model;
931 ret = zbd_set_max_open_zones(td, f);
933 zbd_free_zone_info(f);
940 void zbd_free_zone_info(struct fio_file *f)
946 pthread_mutex_lock(&f->zbd_info->mutex);
947 refcount = --f->zbd_info->refcount;
948 pthread_mutex_unlock(&f->zbd_info->mutex);
950 assert((int32_t)refcount >= 0);
957 * Initialize f->zbd_info.
959 * Returns 0 upon success and a negative error code upon failure.
961 * Note: this function can only work correctly if it is called before the first
964 static int zbd_init_zone_info(struct thread_data *td, struct fio_file *file)
966 struct thread_data *td2;
970 for_each_td(td2, i) {
971 for_each_file(td2, f2, j) {
972 if (td2 == td && f2 == file)
975 strcmp(f2->file_name, file->file_name) != 0)
977 file->zbd_info = f2->zbd_info;
978 file->zbd_info->refcount++;
983 ret = zbd_create_zone_info(td, file);
985 td_verror(td, -ret, "zbd_create_zone_info() failed");
990 int zbd_init_files(struct thread_data *td)
995 for_each_file(td, f, i) {
996 if (zbd_init_zone_info(td, f))
1003 void zbd_recalc_options_with_zone_granularity(struct thread_data *td)
1008 for_each_file(td, f, i) {
1009 struct zoned_block_device_info *zbd = f->zbd_info;
1012 /* zonemode=strided doesn't get per-file zone size. */
1013 zone_size = zbd ? zbd->zone_size : td->o.zone_size;
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;
1031 int zbd_setup_files(struct thread_data *td)
1036 if (!zbd_using_direct_io()) {
1037 log_err("Using direct I/O is mandatory for writing to ZBD drives\n\n");
1041 if (!zbd_verify_sizes())
1044 if (!zbd_verify_bs())
1047 for_each_file(td, f, i) {
1048 struct zoned_block_device_info *zbd = f->zbd_info;
1049 struct fio_zone_info *z;
1054 f->min_zone = zbd_offset_to_zone_idx(f, f->file_offset);
1056 zbd_offset_to_zone_idx(f, f->file_offset + f->io_size);
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
1064 if (zbd_is_seq_job(f))
1065 assert(f->min_zone < f->max_zone);
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");
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.)
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");
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).
1089 td->o.max_open_zones = zbd->max_open_zones;
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)
1096 if (zbd_open_zone(td, f, z))
1099 * If the number of open zones exceeds specified limits,
1100 * reset all extra open zones.
1102 if (zbd_reset_zone(td, f, z) < 0) {
1103 log_err("Failed to reest zone %d\n", zi);
1113 * Reset zbd_info.write_cnt, the counter that counts down towards the next
1116 static void _zbd_reset_write_cnt(const struct thread_data *td,
1117 const struct fio_file *f)
1119 assert(0 <= td->o.zrf.u.f && td->o.zrf.u.f <= 1);
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;
1125 static void zbd_reset_write_cnt(const struct thread_data *td,
1126 const struct fio_file *f)
1128 pthread_mutex_lock(&f->zbd_info->mutex);
1129 _zbd_reset_write_cnt(td, f);
1130 pthread_mutex_unlock(&f->zbd_info->mutex);
1133 static bool zbd_dec_and_reset_write_cnt(const struct thread_data *td,
1134 const struct fio_file *f)
1136 uint32_t write_cnt = 0;
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;
1143 _zbd_reset_write_cnt(td, f);
1144 pthread_mutex_unlock(&f->zbd_info->mutex);
1146 return write_cnt == 0;
1154 /* Calculate the number of sectors with data (swd) and perform action 'a' */
1155 static uint64_t zbd_process_swd(struct thread_data *td,
1156 const struct fio_file *f, enum swd_action a)
1158 struct fio_zone_info *zb, *ze, *z;
1160 uint64_t wp_swd = 0;
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++) {
1166 zone_lock(td, f, z);
1167 wp_swd += z->wp - z->start;
1169 swd += z->wp - z->start;
1172 pthread_mutex_lock(&f->zbd_info->mutex);
1175 assert(f->zbd_info->sectors_with_data == swd);
1176 assert(f->zbd_info->wp_sectors_with_data == wp_swd);
1179 f->zbd_info->sectors_with_data = swd;
1180 f->zbd_info->wp_sectors_with_data = wp_swd;
1183 pthread_mutex_unlock(&f->zbd_info->mutex);
1185 for (z = zb; z < ze; z++)
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.
1196 static const bool enable_check_swd = false;
1198 /* Check whether the values of zbd_info.*sectors_with_data are correct. */
1199 static void zbd_check_swd(struct thread_data *td, const struct fio_file *f)
1201 if (!enable_check_swd)
1204 zbd_process_swd(td, f, CHECK_SWD);
1207 void zbd_file_reset(struct thread_data *td, struct fio_file *f)
1209 struct fio_zone_info *zb, *ze;
1212 if (!f->zbd_info || !td_write(td))
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);
1219 dprint(FD_ZBD, "%s(%s): swd = %" PRIu64 "\n",
1220 __func__, f->file_name, swd);
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.
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);
1232 /* Return random zone index for one of the open zones. */
1233 static uint32_t pick_random_zone_idx(const struct fio_file *f,
1234 const struct io_u *io_u)
1236 return (io_u->offset - f->file_offset) *
1237 f->zbd_info->num_open_zones / f->io_size;
1240 static bool any_io_in_flight(void)
1242 struct thread_data *td;
1245 for_each_td(td, i) {
1246 if (td->io_u_in_flight)
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.
1261 static struct fio_zone_info *zbd_convert_to_open_zone(struct thread_data *td,
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;
1271 bool wait_zone_close;
1273 bool should_retry = true;
1275 assert(is_valid_offset(f, io_u->offset));
1277 if (zbdi->max_open_zones || td->o.job_max_open_zones) {
1279 * This statement accesses zbdi->open_zones[] on purpose
1282 zone_idx = zbdi->open_zones[pick_random_zone_idx(f, io_u)];
1284 zone_idx = zbd_offset_to_zone_idx(f, io_u->offset);
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;
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);
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.
1304 z = zbd_get_zone(f, zone_idx);
1306 zone_lock(td, f, z);
1308 pthread_mutex_lock(&zbdi->mutex);
1311 if (z->cond != ZBD_ZONE_COND_OFFLINE &&
1312 zbdi->max_open_zones == 0 &&
1313 td->o.job_max_open_zones == 0)
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;
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.
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;
1332 for (i = 0; i < zbdi->num_open_zones; i++) {
1335 if (tmp_idx >= zbdi->num_open_zones)
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;
1346 dprint(FD_ZBD, "%s(%s): no candidate zone\n",
1347 __func__, f->file_name);
1349 pthread_mutex_unlock(&zbdi->mutex);
1356 found_candidate_zone:
1357 new_zone_idx = zbdi->open_zones[open_zone_idx];
1358 if (new_zone_idx == zone_idx)
1360 zone_idx = new_zone_idx;
1362 pthread_mutex_unlock(&zbdi->mutex);
1368 /* Both z->mutex and zbdi->mutex are held. */
1371 if (z->wp + min_bs <= zbd_zone_capacity_end(z)) {
1372 pthread_mutex_unlock(&zbdi->mutex);
1377 /* Check if number of open zones reaches one of limits. */
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);
1385 pthread_mutex_unlock(&zbdi->mutex);
1387 /* Only z->mutex is held. */
1390 * When number of open zones reaches to one of limits, wait for
1391 * zone close before opening a new zone.
1393 if (wait_zone_close) {
1395 "%s(%s): quiesce to allow open zones to close\n",
1396 __func__, f->file_name);
1401 /* Zone 'z' is full, so try to open a new zone. */
1402 for (i = f->io_size / zbdi->zone_size; i > 0; i--) {
1407 if (!is_valid_offset(f, z->start)) {
1409 zone_idx = f->min_zone;
1410 z = zbd_get_zone(f, zone_idx);
1412 assert(is_valid_offset(f, z->start));
1415 zone_lock(td, f, z);
1418 if (zbd_open_zone(td, f, z))
1422 /* Only z->mutex is held. */
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)
1430 pthread_mutex_unlock(&zbdi->mutex);
1433 z = zbd_get_zone(f, zone_idx);
1435 zone_lock(td, f, z);
1436 if (z->wp + min_bs <= zbd_zone_capacity_end(z))
1438 pthread_mutex_lock(&zbdi->mutex);
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.
1446 in_flight = any_io_in_flight();
1447 if (in_flight || should_retry) {
1449 "%s(%s): wait zone close and retry open zones\n",
1450 __func__, f->file_name);
1451 pthread_mutex_unlock(&zbdi->mutex);
1454 zone_lock(td, f, z);
1455 should_retry = in_flight;
1459 pthread_mutex_unlock(&zbdi->mutex);
1463 dprint(FD_ZBD, "%s(%s): did not open another zone\n",
1464 __func__, f->file_name);
1469 dprint(FD_ZBD, "%s(%s): returning zone %d\n",
1470 __func__, f->file_name, zone_idx);
1472 io_u->offset = z->start;
1474 assert(z->cond != ZBD_ZONE_COND_OFFLINE);
1479 /* The caller must hold z->mutex. */
1480 static struct fio_zone_info *zbd_replay_write_order(struct thread_data *td,
1482 struct fio_zone_info *z)
1484 const struct fio_file *f = io_u->file;
1485 const uint64_t min_bs = td->o.min_bs[DDIR_WRITE];
1487 if (!zbd_open_zone(td, f, z)) {
1489 z = zbd_convert_to_open_zone(td, io_u);
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);
1497 * If the assertion below fails during a test run, adding
1498 * "--experimental_verify=1" to the command line may help.
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));
1510 z->verify_block += io_u->buflen / min_bs;
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.
1519 * Either returns NULL or returns a zone pointer. When the zone has write
1520 * pointer, hold the mutex for the zone.
1522 static struct fio_zone_info *
1523 zbd_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)
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);
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.
1534 for (z1 = zb + 1, z2 = zb - 1; z1 < zl || z2 >= zf; z1++, z2--) {
1535 if (z1 < zl && z1->cond != ZBD_ZONE_COND_OFFLINE) {
1537 zone_lock(td, f, z1);
1538 if (z1->start + min_bytes <= z1->wp)
1542 } else if (!td_random(td)) {
1546 if (td_random(td) && z2 >= zf &&
1547 z2->cond != ZBD_ZONE_COND_OFFLINE) {
1549 zone_lock(td, f, z2);
1550 if (z2->start + min_bytes <= z2->wp)
1558 "%s: no zone has %"PRIu64" bytes of readable data\n",
1559 f->file_name, min_bytes);
1565 * zbd_end_zone_io - update zone status at command completion
1567 * @z: zone info pointer
1569 * If the write command made the zone full, close it.
1571 * The caller must hold z->mutex.
1573 static void zbd_end_zone_io(struct thread_data *td, const struct io_u *io_u,
1574 struct fio_zone_info *z)
1576 const struct fio_file *f = io_u->file;
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);
1587 * zbd_queue_io - update the write pointer of a sequential zone
1589 * @success: Whether or not the I/O unit has been queued successfully
1590 * @q: queueing status (busy, completed or queued).
1592 * For write and trim operations, update the write pointer of the I/O unit
1595 static void zbd_queue_io(struct thread_data *td, struct io_u *io_u, int q,
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;
1605 z = zbd_offset_to_zone(f, io_u->offset);
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));
1615 switch (io_u->ddir) {
1617 zone_end = min((uint64_t)(io_u->offset + io_u->buflen),
1618 zbd_zone_capacity_end(z));
1621 * z->wp > zone_end means that one or more I/O errors
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;
1629 pthread_mutex_unlock(&zbd_info->mutex);
1636 if (q == FIO_Q_COMPLETED && !io_u->error)
1637 zbd_end_zone_io(td, io_u, z);
1640 if (!success || q != FIO_Q_QUEUED) {
1641 /* BUSY or COMPLETED: unlock the zone */
1643 io_u->zbd_put_io = NULL;
1648 * zbd_put_io - Unlock an I/O unit target zone lock
1651 static void zbd_put_io(struct thread_data *td, const struct io_u *io_u)
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;
1659 z = zbd_offset_to_zone(f, io_u->offset);
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));
1666 zbd_end_zone_io(td, io_u, z);
1669 zbd_check_swd(td, f);
1673 * Windows and MacOS do not define this.
1676 #define EREMOTEIO 121 /* POSIX value */
1679 bool zbd_unaligned_write(int error_code)
1681 switch (error_code) {
1690 * setup_zbd_zone_mode - handle zoneskip as necessary for ZBD drives
1691 * @td: FIO thread data.
1692 * @io_u: FIO I/O unit.
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.
1701 void setup_zbd_zone_mode(struct thread_data *td, struct io_u *io_u)
1703 struct fio_file *f = io_u->file;
1704 enum fio_ddir ddir = io_u->ddir;
1705 struct fio_zone_info *z;
1707 assert(td->o.zone_mode == ZONE_MODE_ZBD);
1708 assert(td->o.zone_size);
1709 assert(f->zbd_info);
1711 z = zbd_offset_to_zone(f, f->last_pos[ddir]);
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.
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)) {
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);
1731 * zone_skip is valid only for sequential workloads.
1733 if (td_random(td) || !td->o.zone_skip)
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.
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)) {
1751 f->file_offset += td->o.zone_size + td->o.zone_skip;
1754 * Wrap from the beginning, if we exceed the file size
1756 if (f->file_offset >= f->real_file_size)
1757 f->file_offset = get_start_offset(td, f);
1759 f->last_pos[ddir] = f->file_offset;
1760 td->io_skip_bytes += td->o.zone_skip;
1765 * zbd_adjust_ddir - Adjust an I/O direction for zonemode=zbd.
1767 * @td: FIO thread data.
1768 * @io_u: FIO I/O unit.
1769 * @ddir: I/O direction before adjustment.
1771 * Return adjusted I/O direction.
1773 enum fio_ddir zbd_adjust_ddir(struct thread_data *td, struct io_u *io_u,
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.
1782 assert(io_u->file->zbd_info);
1783 if (ddir != DDIR_READ || !td_rw(td))
1786 if (io_u->file->zbd_info->sectors_with_data ||
1787 td->o.read_beyond_wp)
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.
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.
1802 enum io_u_action zbd_adjust_block(struct thread_data *td, struct io_u *io_u)
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];
1814 assert(is_valid_offset(f, io_u->offset));
1815 assert(io_u->buflen);
1817 zb = zbd_offset_to_zone(f, io_u->offset);
1821 /* Accept non-write I/Os for conventional zones. */
1822 if (io_u->ddir != DDIR_WRITE)
1826 * Make sure that writes to conventional zones
1827 * don't cross over to any sequential zones.
1829 if (!(zb + 1)->has_wp ||
1830 io_u->offset + io_u->buflen <= (zb + 1)->start)
1833 if (io_u->offset + min_bs > (zb + 1)->start) {
1835 "%s: off=%llu + min_bs=%"PRIu64" > next zone %"PRIu64"\n",
1836 f->file_name, io_u->offset,
1837 min_bs, (zb + 1)->start);
1839 zb->start + (zb + 1)->start - io_u->offset;
1840 new_len = min(io_u->buflen,
1841 (zb + 1)->start - io_u->offset);
1843 new_len = (zb + 1)->start - io_u->offset;
1846 io_u->buflen = new_len / min_bs * min_bs;
1852 * Accept the I/O offset for reads if reading beyond the write pointer
1855 if (zb->cond != ZBD_ZONE_COND_OFFLINE &&
1856 io_u->ddir == DDIR_READ && td->o.read_beyond_wp)
1859 zbd_check_swd(td, f);
1861 zone_lock(td, f, zb);
1863 switch (io_u->ddir) {
1865 if (td->runstate == TD_VERIFYING && td_write(td)) {
1866 zb = zbd_replay_write_order(td, io_u, zb);
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
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))) {
1880 zl = zbd_get_zone(f, f->max_zone);
1881 zb = zbd_find_zone(td, io_u, min_bs, zb, zl);
1884 "%s: zbd_find_zone(%lld, %llu) failed\n",
1885 f->file_name, io_u->offset,
1890 * zbd_find_zone() returned a zone with a range of at
1893 range = zb->wp - zb->start;
1894 assert(range >= min_bs);
1897 io_u->offset = zb->start;
1901 * Make sure the I/O is within the zone valid data range while
1902 * maximizing the I/O size and preserving randomness.
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;
1912 * When zbd_find_zone() returns a conventional zone,
1913 * we can simply accept the new i/o offset here.
1919 * Make sure the I/O does not cross over the zone wp position.
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);
1930 assert(zb->start <= io_u->offset);
1931 assert(io_u->offset + io_u->buflen <= zb->wp);
1936 if (io_u->buflen > zbdi->zone_size) {
1937 td_verror(td, EINVAL, "I/O buflen exceeds zone size");
1939 "%s: I/O buflen %llu exceeds zone size %"PRIu64"\n",
1940 f->file_name, io_u->buflen, zbdi->zone_size);
1944 if (!zbd_open_zone(td, f, zb)) {
1946 zb = zbd_convert_to_open_zone(td, io_u);
1948 dprint(FD_IO, "%s: can't convert to open zone",
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))
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);
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
1973 if (zbd_reset_zone(td, f, zb) < 0)
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);
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);
1995 * Make sure that the buflen is a multiple of the minimal
1996 * block size. Give up if shrinking would make the request too
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)
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);
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);
2018 /* Check random trim targets a non-empty zone */
2019 if (!td_random(td) || zb->wp > zb->start)
2022 /* Find out a non-empty zone to trim */
2024 zl = zbd_get_zone(f, f->max_zone);
2025 zb = zbd_find_zone(td, io_u, 1, zb, zl);
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);
2038 case DDIR_SYNC_FILE_RANGE:
2049 assert(zb->cond != ZBD_ZONE_COND_OFFLINE);
2050 assert(!io_u->zbd_queue_io);
2051 assert(!io_u->zbd_put_io);
2053 io_u->zbd_queue_io = zbd_queue_io;
2054 io_u->zbd_put_io = zbd_put_io;
2057 * Since we return with the zone lock still held,
2058 * add an annotation to let Coverity know that it
2061 /* coverity[missing_unlock] */
2066 if (zb && zb->has_wp)
2072 /* Return a string with ZBD statistics */
2073 char *zbd_write_status(const struct thread_stat *ts)
2077 if (asprintf(&res, "; %"PRIu64" zone resets", ts->nr_zone_resets) < 0)
2083 * zbd_do_io_u_trim - If reset zone is applicable, do reset zone instead of trim
2085 * @td: FIO thread data.
2086 * @io_u: FIO I/O unit.
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.
2092 int zbd_do_io_u_trim(const struct thread_data *td, struct io_u *io_u)
2094 struct fio_file *f = io_u->file;
2095 struct fio_zone_info *z;
2098 z = zbd_offset_to_zone(f, io_u->offset);
2102 if (io_u->offset != z->start) {
2103 log_err("Trim offset not at zone start (%lld)\n",
2108 ret = zbd_reset_zone((struct thread_data *)td, f, z);
2112 return io_u_completed;