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_nr(const struct fio_file *f,
31 struct fio_zone_info *zone)
33 return zone - f->zbd_info->zone_info;
37 * zbd_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 uint32_t zbd_zone_idx(const struct fio_file *f, uint64_t offset)
46 if (f->zbd_info->zone_size_log2 > 0)
47 zone_idx = offset >> f->zbd_info->zone_size_log2;
49 zone_idx = offset / f->zbd_info->zone_size;
51 return min(zone_idx, f->zbd_info->nr_zones);
55 * zbd_zone_end - Return zone end location
56 * @z: zone info pointer.
58 static inline uint64_t zbd_zone_end(const struct fio_zone_info *z)
64 * zbd_zone_capacity_end - Return zone capacity limit end location
65 * @z: zone info pointer.
67 static inline uint64_t zbd_zone_capacity_end(const struct fio_zone_info *z)
69 return z->start + z->capacity;
73 * zbd_zone_full - verify whether a minimum number of bytes remain in a zone
75 * @z: zone info pointer.
76 * @required: minimum number of bytes that must remain in a zone.
78 * The caller must hold z->mutex.
80 static bool zbd_zone_full(const struct fio_file *f, struct fio_zone_info *z,
83 assert((required & 511) == 0);
86 z->wp + required > zbd_zone_capacity_end(z);
89 static void zone_lock(struct thread_data *td, const struct fio_file *f,
90 struct fio_zone_info *z)
92 struct zoned_block_device_info *zbd = f->zbd_info;
93 uint32_t nz = z - zbd->zone_info;
95 /* A thread should never lock zones outside its working area. */
96 assert(f->min_zone <= nz && nz < f->max_zone);
101 * Lock the io_u target zone. The zone will be unlocked if io_u offset
102 * is changed or when io_u completes and zbd_put_io() executed.
103 * To avoid multiple jobs doing asynchronous I/Os from deadlocking each
104 * other waiting for zone locks when building an io_u batch, first
105 * only trylock the zone. If the zone is already locked by another job,
106 * process the currently queued I/Os so that I/O progress is made and
109 if (pthread_mutex_trylock(&z->mutex) != 0) {
110 if (!td_ioengine_flagged(td, FIO_SYNCIO))
112 pthread_mutex_lock(&z->mutex);
116 static inline void zone_unlock(struct fio_zone_info *z)
121 ret = pthread_mutex_unlock(&z->mutex);
125 static inline struct fio_zone_info *get_zone(const struct fio_file *f,
126 unsigned int zone_nr)
128 return &f->zbd_info->zone_info[zone_nr];
132 * zbd_get_zoned_model - Get a device zoned model
133 * @td: FIO thread data
134 * @f: FIO file for which to get model information
136 static int zbd_get_zoned_model(struct thread_data *td, struct fio_file *f,
137 enum zbd_zoned_model *model)
141 if (f->filetype == FIO_TYPE_PIPE) {
142 log_err("zonemode=zbd does not support pipes\n");
146 /* If regular file, always emulate zones inside the file. */
147 if (f->filetype == FIO_TYPE_FILE) {
152 if (td->io_ops && td->io_ops->get_zoned_model)
153 ret = td->io_ops->get_zoned_model(td, f, model);
155 ret = blkzoned_get_zoned_model(td, f, model);
157 td_verror(td, errno, "get zoned model failed");
158 log_err("%s: get zoned model failed (%d).\n",
159 f->file_name, errno);
166 * zbd_report_zones - Get zone information
167 * @td: FIO thread data.
168 * @f: FIO file for which to get zone information
169 * @offset: offset from which to report zones
170 * @zones: Array of struct zbd_zone
171 * @nr_zones: Size of @zones array
173 * Get zone information into @zones starting from the zone at offset @offset
174 * for the device specified by @f.
176 * Returns the number of zones reported upon success and a negative error code
177 * upon failure. If the zone report is empty, always assume an error (device
178 * problem) and return -EIO.
180 static int zbd_report_zones(struct thread_data *td, struct fio_file *f,
181 uint64_t offset, struct zbd_zone *zones,
182 unsigned int nr_zones)
186 if (td->io_ops && td->io_ops->report_zones)
187 ret = td->io_ops->report_zones(td, f, offset, zones, nr_zones);
189 ret = blkzoned_report_zones(td, f, offset, zones, nr_zones);
191 td_verror(td, errno, "report zones failed");
192 log_err("%s: report zones from sector %"PRIu64" failed (%d).\n",
193 f->file_name, offset >> 9, errno);
194 } else if (ret == 0) {
195 td_verror(td, errno, "Empty zone report");
196 log_err("%s: report zones from sector %"PRIu64" is empty.\n",
197 f->file_name, offset >> 9);
205 * zbd_reset_wp - reset the write pointer of a range of zones
206 * @td: FIO thread data.
207 * @f: FIO file for which to reset zones
208 * @offset: Starting offset of the first zone to reset
209 * @length: Length of the range of zones to reset
211 * Reset the write pointer of all zones in the range @offset...@offset+@length.
212 * Returns 0 upon success and a negative error code upon failure.
214 static int zbd_reset_wp(struct thread_data *td, struct fio_file *f,
215 uint64_t offset, uint64_t length)
219 if (td->io_ops && td->io_ops->reset_wp)
220 ret = td->io_ops->reset_wp(td, f, offset, length);
222 ret = blkzoned_reset_wp(td, f, offset, length);
224 td_verror(td, errno, "resetting wp failed");
225 log_err("%s: resetting wp for %"PRIu64" sectors at sector %"PRIu64" failed (%d).\n",
226 f->file_name, length >> 9, offset >> 9, errno);
233 * zbd_reset_zone - reset the write pointer of a single zone
234 * @td: FIO thread data.
235 * @f: FIO file associated with the disk for which to reset a write pointer.
238 * Returns 0 upon success and a negative error code upon failure.
240 * The caller must hold z->mutex.
242 static int zbd_reset_zone(struct thread_data *td, struct fio_file *f,
243 struct fio_zone_info *z)
245 uint64_t offset = z->start;
246 uint64_t length = (z+1)->start - offset;
247 uint64_t data_in_zone = z->wp - z->start;
253 assert(is_valid_offset(f, offset + length - 1));
255 dprint(FD_ZBD, "%s: resetting wp of zone %u.\n",
256 f->file_name, zbd_zone_nr(f, z));
258 switch (f->zbd_info->model) {
260 case ZBD_HOST_MANAGED:
261 ret = zbd_reset_wp(td, f, offset, length);
269 pthread_mutex_lock(&f->zbd_info->mutex);
270 f->zbd_info->sectors_with_data -= data_in_zone;
271 f->zbd_info->wp_sectors_with_data -= data_in_zone;
272 pthread_mutex_unlock(&f->zbd_info->mutex);
277 td->ts.nr_zone_resets++;
283 * zbd_close_zone - Remove a zone from the open zones array.
284 * @td: FIO thread data.
285 * @f: FIO file associated with the disk for which to reset a write pointer.
286 * @zone_idx: Index of the zone to remove.
288 * The caller must hold f->zbd_info->mutex.
290 static void zbd_close_zone(struct thread_data *td, const struct fio_file *f,
291 struct fio_zone_info *z)
298 for (ozi = 0; ozi < f->zbd_info->num_open_zones; ozi++) {
299 if (get_zone(f, f->zbd_info->open_zones[ozi]) == z)
302 if (ozi == f->zbd_info->num_open_zones)
305 dprint(FD_ZBD, "%s: closing zone %u\n",
306 f->file_name, zbd_zone_nr(f, z));
308 memmove(f->zbd_info->open_zones + ozi,
309 f->zbd_info->open_zones + ozi + 1,
310 (ZBD_MAX_OPEN_ZONES - (ozi + 1)) *
311 sizeof(f->zbd_info->open_zones[0]));
313 f->zbd_info->num_open_zones--;
314 td->num_open_zones--;
319 * zbd_reset_zones - Reset a range of zones.
320 * @td: fio thread data.
321 * @f: fio file for which to reset zones
322 * @zb: first zone to reset.
323 * @ze: first zone not to reset.
325 * Returns 0 upon success and 1 upon failure.
327 static int zbd_reset_zones(struct thread_data *td, struct fio_file *f,
328 struct fio_zone_info *const zb,
329 struct fio_zone_info *const ze)
331 struct fio_zone_info *z;
332 const uint64_t min_bs = td->o.min_bs[DDIR_WRITE];
337 dprint(FD_ZBD, "%s: examining zones %u .. %u\n",
338 f->file_name, zbd_zone_nr(f, zb), zbd_zone_nr(f, ze));
340 for (z = zb; z < ze; z++) {
345 pthread_mutex_lock(&f->zbd_info->mutex);
346 zbd_close_zone(td, f, z);
347 pthread_mutex_unlock(&f->zbd_info->mutex);
349 if (z->wp != z->start) {
350 dprint(FD_ZBD, "%s: resetting zone %u\n",
351 f->file_name, zbd_zone_nr(f, z));
352 if (zbd_reset_zone(td, f, z) < 0)
363 * zbd_get_max_open_zones - Get the maximum number of open zones
364 * @td: FIO thread data
365 * @f: FIO file for which to get max open zones
366 * @max_open_zones: Upon success, result will be stored here.
368 * A @max_open_zones value set to zero means no limit.
370 * Returns 0 upon success and a negative error code upon failure.
372 static int zbd_get_max_open_zones(struct thread_data *td, struct fio_file *f,
373 unsigned int *max_open_zones)
377 if (td->io_ops && td->io_ops->get_max_open_zones)
378 ret = td->io_ops->get_max_open_zones(td, f, max_open_zones);
380 ret = blkzoned_get_max_open_zones(td, f, max_open_zones);
382 td_verror(td, errno, "get max open zones failed");
383 log_err("%s: get max open zones failed (%d).\n",
384 f->file_name, errno);
391 * zbd_open_zone - Add a zone to the array of open zones.
392 * @td: fio thread data.
393 * @f: fio file that has the open zones to add.
394 * @zone_idx: Index of the zone to add.
396 * Open a ZBD zone if it is not already open. Returns true if either the zone
397 * was already open or if the zone was successfully added to the array of open
398 * zones without exceeding the maximum number of open zones. Returns false if
399 * the zone was not already open and opening the zone would cause the zone limit
402 static bool zbd_open_zone(struct thread_data *td, const struct fio_file *f,
405 const uint64_t min_bs = td->o.min_bs[DDIR_WRITE];
406 struct zoned_block_device_info *zbdi = f->zbd_info;
407 struct fio_zone_info *z = get_zone(f, zone_idx);
410 if (z->cond == ZBD_ZONE_COND_OFFLINE)
414 * Skip full zones with data verification enabled because resetting a
415 * zone causes data loss and hence causes verification to fail.
417 if (td->o.verify != VERIFY_NONE && zbd_zone_full(f, z, min_bs))
421 * zbdi->max_open_zones == 0 means that there is no limit on the maximum
422 * number of open zones. In this case, do no track open zones in
423 * zbdi->open_zones array.
425 if (!zbdi->max_open_zones)
428 pthread_mutex_lock(&zbdi->mutex);
432 * If the zone is going to be completely filled by writes
433 * already in-flight, handle it as a full zone instead of an
436 if (z->wp >= zbd_zone_capacity_end(z))
442 /* Zero means no limit */
443 if (td->o.job_max_open_zones > 0 &&
444 td->num_open_zones >= td->o.job_max_open_zones)
446 if (zbdi->num_open_zones >= zbdi->max_open_zones)
449 dprint(FD_ZBD, "%s: opening zone %d\n",
450 f->file_name, zone_idx);
452 zbdi->open_zones[zbdi->num_open_zones++] = zone_idx;
453 td->num_open_zones++;
458 pthread_mutex_unlock(&zbdi->mutex);
462 /* Verify whether direct I/O is used for all host-managed zoned drives. */
463 static bool zbd_using_direct_io(void)
465 struct thread_data *td;
470 if (td->o.odirect || !(td->o.td_ddir & TD_DDIR_WRITE))
472 for_each_file(td, f, j) {
474 f->zbd_info->model == ZBD_HOST_MANAGED)
482 /* Whether or not the I/O range for f includes one or more sequential zones */
483 static bool zbd_is_seq_job(struct fio_file *f)
485 uint32_t zone_idx, zone_idx_b, zone_idx_e;
492 zone_idx_b = zbd_zone_idx(f, f->file_offset);
493 zone_idx_e = zbd_zone_idx(f, f->file_offset + f->io_size - 1);
494 for (zone_idx = zone_idx_b; zone_idx <= zone_idx_e; zone_idx++)
495 if (get_zone(f, zone_idx)->has_wp)
502 * Verify whether the file offset and size parameters are aligned with zone
503 * boundaries. If the file offset is not aligned, align it down to the start of
504 * the zone containing the start offset and align up the file io_size parameter.
506 static bool zbd_zone_align_file_sizes(struct thread_data *td,
509 const struct fio_zone_info *z;
510 uint64_t new_offset, new_end;
515 if (f->file_offset >= f->real_file_size)
517 if (!zbd_is_seq_job(f))
520 if (!td->o.zone_size) {
521 td->o.zone_size = f->zbd_info->zone_size;
522 if (!td->o.zone_size) {
523 log_err("%s: invalid 0 zone size\n",
527 } else if (td->o.zone_size != f->zbd_info->zone_size) {
528 log_err("%s: zonesize %llu does not match the device zone size %"PRIu64".\n",
529 f->file_name, td->o.zone_size,
530 f->zbd_info->zone_size);
534 if (td->o.zone_skip % td->o.zone_size) {
535 log_err("%s: zoneskip %llu is not a multiple of the device zone size %llu.\n",
536 f->file_name, td->o.zone_skip,
541 zone_idx = zbd_zone_idx(f, f->file_offset);
542 z = get_zone(f, zone_idx);
543 if ((f->file_offset != z->start) &&
544 (td->o.td_ddir != TD_DDIR_READ)) {
545 new_offset = zbd_zone_end(z);
546 if (new_offset >= f->file_offset + f->io_size) {
547 log_info("%s: io_size must be at least one zone\n",
551 log_info("%s: rounded up offset from %"PRIu64" to %"PRIu64"\n",
552 f->file_name, f->file_offset,
554 f->io_size -= (new_offset - f->file_offset);
555 f->file_offset = new_offset;
558 zone_idx = zbd_zone_idx(f, f->file_offset + f->io_size);
559 z = get_zone(f, zone_idx);
561 if ((td->o.td_ddir != TD_DDIR_READ) &&
562 (f->file_offset + f->io_size != new_end)) {
563 if (new_end <= f->file_offset) {
564 log_info("%s: io_size must be at least one zone\n",
568 log_info("%s: rounded down io_size from %"PRIu64" to %"PRIu64"\n",
569 f->file_name, f->io_size,
570 new_end - f->file_offset);
571 f->io_size = new_end - f->file_offset;
578 * Verify whether offset and size parameters are aligned with zone boundaries.
580 static bool zbd_verify_sizes(void)
582 struct thread_data *td;
587 for_each_file(td, f, j) {
588 if (!zbd_zone_align_file_sizes(td, f))
596 static bool zbd_verify_bs(void)
598 struct thread_data *td;
604 (td->o.min_bs[DDIR_TRIM] != td->o.max_bs[DDIR_TRIM] ||
605 td->o.bssplit_nr[DDIR_TRIM])) {
606 log_info("bsrange and bssplit are not allowed for trim with zonemode=zbd\n");
609 for_each_file(td, f, j) {
615 zone_size = f->zbd_info->zone_size;
616 if (td_trim(td) && td->o.bs[DDIR_TRIM] != zone_size) {
617 log_info("%s: trim block size %llu is not the zone size %"PRIu64"\n",
618 f->file_name, td->o.bs[DDIR_TRIM],
622 for (k = 0; k < FIO_ARRAY_SIZE(td->o.bs); k++) {
623 if (td->o.verify != VERIFY_NONE &&
624 zone_size % td->o.bs[k] != 0) {
625 log_info("%s: block size %llu is not a divisor of the zone size %"PRIu64"\n",
626 f->file_name, td->o.bs[k],
636 static int ilog2(uint64_t i)
648 * Initialize f->zbd_info for devices that are not zoned block devices. This
649 * allows to execute a ZBD workload against a non-ZBD device.
651 static int init_zone_info(struct thread_data *td, struct fio_file *f)
654 struct fio_zone_info *p;
655 uint64_t zone_size = td->o.zone_size;
656 uint64_t zone_capacity = td->o.zone_capacity;
657 struct zoned_block_device_info *zbd_info = NULL;
660 if (zone_size == 0) {
661 log_err("%s: Specifying the zone size is mandatory for regular file/block device with --zonemode=zbd\n\n",
666 if (zone_size < 512) {
667 log_err("%s: zone size must be at least 512 bytes for --zonemode=zbd\n\n",
672 if (zone_capacity == 0)
673 zone_capacity = zone_size;
675 if (zone_capacity > zone_size) {
676 log_err("%s: job parameter zonecapacity %llu is larger than zone size %llu\n",
677 f->file_name, td->o.zone_capacity, td->o.zone_size);
681 if (f->real_file_size < zone_size) {
682 log_err("%s: file/device size %"PRIu64" is smaller than zone size %"PRIu64"\n",
683 f->file_name, f->real_file_size, zone_size);
687 nr_zones = (f->real_file_size + zone_size - 1) / zone_size;
688 zbd_info = scalloc(1, sizeof(*zbd_info) +
689 (nr_zones + 1) * sizeof(zbd_info->zone_info[0]));
693 mutex_init_pshared(&zbd_info->mutex);
694 zbd_info->refcount = 1;
695 p = &zbd_info->zone_info[0];
696 for (i = 0; i < nr_zones; i++, p++) {
697 mutex_init_pshared_with_type(&p->mutex,
698 PTHREAD_MUTEX_RECURSIVE);
699 p->start = i * zone_size;
701 p->type = ZBD_ZONE_TYPE_SWR;
702 p->cond = ZBD_ZONE_COND_EMPTY;
703 p->capacity = zone_capacity;
707 p->start = nr_zones * zone_size;
709 f->zbd_info = zbd_info;
710 f->zbd_info->zone_size = zone_size;
711 f->zbd_info->zone_size_log2 = is_power_of_2(zone_size) ?
712 ilog2(zone_size) : 0;
713 f->zbd_info->nr_zones = nr_zones;
718 * Maximum number of zones to report in one operation.
720 #define ZBD_REPORT_MAX_ZONES 8192U
723 * Parse the device zone report and store it in f->zbd_info. Must be called
724 * only for devices that are zoned, namely those with a model != ZBD_NONE.
726 static int parse_zone_info(struct thread_data *td, struct fio_file *f)
729 struct zbd_zone *zones, *z;
730 struct fio_zone_info *p;
731 uint64_t zone_size, offset;
732 struct zoned_block_device_info *zbd_info = NULL;
733 int i, j, ret = -ENOMEM;
735 zones = calloc(ZBD_REPORT_MAX_ZONES, sizeof(struct zbd_zone));
739 nrz = zbd_report_zones(td, f, 0, zones, ZBD_REPORT_MAX_ZONES);
742 log_info("fio: report zones (offset 0) failed for %s (%d).\n",
747 zone_size = zones[0].len;
748 nr_zones = (f->real_file_size + zone_size - 1) / zone_size;
750 if (td->o.zone_size == 0) {
751 td->o.zone_size = zone_size;
752 } else if (td->o.zone_size != zone_size) {
753 log_err("fio: %s job parameter zonesize %llu does not match disk zone size %"PRIu64".\n",
754 f->file_name, td->o.zone_size, zone_size);
759 dprint(FD_ZBD, "Device %s has %d zones of size %"PRIu64" KB\n",
760 f->file_name, nr_zones, zone_size / 1024);
762 zbd_info = scalloc(1, sizeof(*zbd_info) +
763 (nr_zones + 1) * sizeof(zbd_info->zone_info[0]));
766 mutex_init_pshared(&zbd_info->mutex);
767 zbd_info->refcount = 1;
768 p = &zbd_info->zone_info[0];
769 for (offset = 0, j = 0; j < nr_zones;) {
771 for (i = 0; i < nrz; i++, j++, z++, p++) {
772 mutex_init_pshared_with_type(&p->mutex,
773 PTHREAD_MUTEX_RECURSIVE);
775 p->capacity = z->capacity;
778 case ZBD_ZONE_COND_NOT_WP:
779 case ZBD_ZONE_COND_FULL:
780 p->wp = p->start + p->capacity;
783 assert(z->start <= z->wp);
784 assert(z->wp <= z->start + zone_size);
790 case ZBD_ZONE_TYPE_SWR:
799 if (j > 0 && p->start != p[-1].start + zone_size) {
800 log_info("%s: invalid zone data\n",
807 offset = z->start + z->len;
811 nrz = zbd_report_zones(td, f, offset, zones,
812 min((uint32_t)(nr_zones - j),
813 ZBD_REPORT_MAX_ZONES));
816 log_info("fio: report zones (offset %"PRIu64") failed for %s (%d).\n",
817 offset, f->file_name, -ret);
823 zbd_info->zone_info[nr_zones].start = offset;
825 f->zbd_info = zbd_info;
826 f->zbd_info->zone_size = zone_size;
827 f->zbd_info->zone_size_log2 = is_power_of_2(zone_size) ?
828 ilog2(zone_size) : 0;
829 f->zbd_info->nr_zones = nr_zones;
839 static int zbd_set_max_open_zones(struct thread_data *td, struct fio_file *f)
841 struct zoned_block_device_info *zbd = f->zbd_info;
842 unsigned int max_open_zones;
845 if (zbd->model != ZBD_HOST_MANAGED || td->o.ignore_zone_limits) {
846 /* Only host-managed devices have a max open limit */
847 zbd->max_open_zones = td->o.max_open_zones;
851 /* If host-managed, get the max open limit */
852 ret = zbd_get_max_open_zones(td, f, &max_open_zones);
856 if (!max_open_zones) {
857 /* No device limit */
858 zbd->max_open_zones = td->o.max_open_zones;
859 } else if (!td->o.max_open_zones) {
860 /* No user limit. Set limit to device limit */
861 zbd->max_open_zones = max_open_zones;
862 } else if (td->o.max_open_zones <= max_open_zones) {
863 /* Both user limit and dev limit. User limit not too large */
864 zbd->max_open_zones = td->o.max_open_zones;
866 /* Both user limit and dev limit. User limit too large */
867 td_verror(td, EINVAL,
868 "Specified --max_open_zones is too large");
869 log_err("Specified --max_open_zones (%d) is larger than max (%u)\n",
870 td->o.max_open_zones, max_open_zones);
875 /* Ensure that the limit is not larger than FIO's internal limit */
876 if (zbd->max_open_zones > ZBD_MAX_OPEN_ZONES) {
877 td_verror(td, EINVAL, "'max_open_zones' value is too large");
878 log_err("'max_open_zones' value is larger than %u\n",
883 dprint(FD_ZBD, "%s: using max open zones limit: %"PRIu32"\n",
884 f->file_name, zbd->max_open_zones);
890 * Allocate zone information and store it into f->zbd_info if zonemode=zbd.
892 * Returns 0 upon success and a negative error code upon failure.
894 static int zbd_create_zone_info(struct thread_data *td, struct fio_file *f)
896 enum zbd_zoned_model zbd_model;
899 assert(td->o.zone_mode == ZONE_MODE_ZBD);
901 ret = zbd_get_zoned_model(td, f, &zbd_model);
907 case ZBD_HOST_MANAGED:
908 ret = parse_zone_info(td, f);
913 ret = init_zone_info(td, f);
918 td_verror(td, EINVAL, "Unsupported zoned model");
919 log_err("Unsupported zoned model\n");
924 f->zbd_info->model = zbd_model;
926 ret = zbd_set_max_open_zones(td, f);
928 zbd_free_zone_info(f);
935 void zbd_free_zone_info(struct fio_file *f)
941 pthread_mutex_lock(&f->zbd_info->mutex);
942 refcount = --f->zbd_info->refcount;
943 pthread_mutex_unlock(&f->zbd_info->mutex);
945 assert((int32_t)refcount >= 0);
952 * Initialize f->zbd_info.
954 * Returns 0 upon success and a negative error code upon failure.
956 * Note: this function can only work correctly if it is called before the first
959 static int zbd_init_zone_info(struct thread_data *td, struct fio_file *file)
961 struct thread_data *td2;
965 for_each_td(td2, i) {
966 for_each_file(td2, f2, j) {
967 if (td2 == td && f2 == file)
970 strcmp(f2->file_name, file->file_name) != 0)
972 file->zbd_info = f2->zbd_info;
973 file->zbd_info->refcount++;
978 ret = zbd_create_zone_info(td, file);
980 td_verror(td, -ret, "zbd_create_zone_info() failed");
985 int zbd_init_files(struct thread_data *td)
990 for_each_file(td, f, i) {
991 if (zbd_init_zone_info(td, f))
998 void zbd_recalc_options_with_zone_granularity(struct thread_data *td)
1003 for_each_file(td, f, i) {
1004 struct zoned_block_device_info *zbd = f->zbd_info;
1007 /* zonemode=strided doesn't get per-file zone size. */
1008 zone_size = zbd ? zbd->zone_size : td->o.zone_size;
1012 if (td->o.size_nz > 0)
1013 td->o.size = td->o.size_nz * zone_size;
1014 if (td->o.io_size_nz > 0)
1015 td->o.io_size = td->o.io_size_nz * zone_size;
1016 if (td->o.start_offset_nz > 0)
1017 td->o.start_offset = td->o.start_offset_nz * zone_size;
1018 if (td->o.offset_increment_nz > 0)
1019 td->o.offset_increment =
1020 td->o.offset_increment_nz * zone_size;
1021 if (td->o.zone_skip_nz > 0)
1022 td->o.zone_skip = td->o.zone_skip_nz * zone_size;
1026 int zbd_setup_files(struct thread_data *td)
1031 if (!zbd_using_direct_io()) {
1032 log_err("Using direct I/O is mandatory for writing to ZBD drives\n\n");
1036 if (!zbd_verify_sizes())
1039 if (!zbd_verify_bs())
1042 for_each_file(td, f, i) {
1043 struct zoned_block_device_info *zbd = f->zbd_info;
1044 struct fio_zone_info *z;
1049 f->min_zone = zbd_zone_idx(f, f->file_offset);
1050 f->max_zone = zbd_zone_idx(f, f->file_offset + f->io_size);
1053 * When all zones in the I/O range are conventional, io_size
1054 * can be smaller than zone size, making min_zone the same
1055 * as max_zone. This is why the assert below needs to be made
1058 if (zbd_is_seq_job(f))
1059 assert(f->min_zone < f->max_zone);
1061 if (td->o.max_open_zones > 0 &&
1062 zbd->max_open_zones != td->o.max_open_zones) {
1063 log_err("Different 'max_open_zones' values\n");
1068 * The per job max open zones limit cannot be used without a
1069 * global max open zones limit. (As the tracking of open zones
1070 * is disabled when there is no global max open zones limit.)
1072 if (td->o.job_max_open_zones && !zbd->max_open_zones) {
1073 log_err("'job_max_open_zones' cannot be used without a global open zones limit\n");
1078 * zbd->max_open_zones is the global limit shared for all jobs
1079 * that target the same zoned block device. Force sync the per
1080 * thread global limit with the actual global limit. (The real
1081 * per thread/job limit is stored in td->o.job_max_open_zones).
1083 td->o.max_open_zones = zbd->max_open_zones;
1085 for (zi = f->min_zone; zi < f->max_zone; zi++) {
1086 z = &zbd->zone_info[zi];
1087 if (z->cond != ZBD_ZONE_COND_IMP_OPEN &&
1088 z->cond != ZBD_ZONE_COND_EXP_OPEN)
1090 if (zbd_open_zone(td, f, zi))
1093 * If the number of open zones exceeds specified limits,
1094 * reset all extra open zones.
1096 if (zbd_reset_zone(td, f, z) < 0) {
1097 log_err("Failed to reest zone %d\n", zi);
1107 * Reset zbd_info.write_cnt, the counter that counts down towards the next
1110 static void _zbd_reset_write_cnt(const struct thread_data *td,
1111 const struct fio_file *f)
1113 assert(0 <= td->o.zrf.u.f && td->o.zrf.u.f <= 1);
1115 f->zbd_info->write_cnt = td->o.zrf.u.f ?
1116 min(1.0 / td->o.zrf.u.f, 0.0 + UINT_MAX) : UINT_MAX;
1119 static void zbd_reset_write_cnt(const struct thread_data *td,
1120 const struct fio_file *f)
1122 pthread_mutex_lock(&f->zbd_info->mutex);
1123 _zbd_reset_write_cnt(td, f);
1124 pthread_mutex_unlock(&f->zbd_info->mutex);
1127 static bool zbd_dec_and_reset_write_cnt(const struct thread_data *td,
1128 const struct fio_file *f)
1130 uint32_t write_cnt = 0;
1132 pthread_mutex_lock(&f->zbd_info->mutex);
1133 assert(f->zbd_info->write_cnt);
1134 if (f->zbd_info->write_cnt)
1135 write_cnt = --f->zbd_info->write_cnt;
1137 _zbd_reset_write_cnt(td, f);
1138 pthread_mutex_unlock(&f->zbd_info->mutex);
1140 return write_cnt == 0;
1148 /* Calculate the number of sectors with data (swd) and perform action 'a' */
1149 static uint64_t zbd_process_swd(struct thread_data *td,
1150 const struct fio_file *f, enum swd_action a)
1152 struct fio_zone_info *zb, *ze, *z;
1154 uint64_t wp_swd = 0;
1156 zb = get_zone(f, f->min_zone);
1157 ze = get_zone(f, f->max_zone);
1158 for (z = zb; z < ze; z++) {
1160 zone_lock(td, f, z);
1161 wp_swd += z->wp - z->start;
1163 swd += z->wp - z->start;
1166 pthread_mutex_lock(&f->zbd_info->mutex);
1169 assert(f->zbd_info->sectors_with_data == swd);
1170 assert(f->zbd_info->wp_sectors_with_data == wp_swd);
1173 f->zbd_info->sectors_with_data = swd;
1174 f->zbd_info->wp_sectors_with_data = wp_swd;
1177 pthread_mutex_unlock(&f->zbd_info->mutex);
1179 for (z = zb; z < ze; z++)
1187 * The swd check is useful for debugging but takes too much time to leave
1188 * it enabled all the time. Hence it is disabled by default.
1190 static const bool enable_check_swd = false;
1192 /* Check whether the values of zbd_info.*sectors_with_data are correct. */
1193 static void zbd_check_swd(struct thread_data *td, const struct fio_file *f)
1195 if (!enable_check_swd)
1198 zbd_process_swd(td, f, CHECK_SWD);
1201 void zbd_file_reset(struct thread_data *td, struct fio_file *f)
1203 struct fio_zone_info *zb, *ze;
1206 if (!f->zbd_info || !td_write(td))
1209 zb = get_zone(f, f->min_zone);
1210 ze = get_zone(f, f->max_zone);
1211 swd = zbd_process_swd(td, f, SET_SWD);
1213 dprint(FD_ZBD, "%s(%s): swd = %" PRIu64 "\n",
1214 __func__, f->file_name, swd);
1217 * If data verification is enabled reset the affected zones before
1218 * writing any data to avoid that a zone reset has to be issued while
1219 * writing data, which causes data loss.
1221 if (td->o.verify != VERIFY_NONE && td->runstate != TD_VERIFYING)
1222 zbd_reset_zones(td, f, zb, ze);
1223 zbd_reset_write_cnt(td, f);
1226 /* Return random zone index for one of the open zones. */
1227 static uint32_t pick_random_zone_idx(const struct fio_file *f,
1228 const struct io_u *io_u)
1230 return (io_u->offset - f->file_offset) *
1231 f->zbd_info->num_open_zones / f->io_size;
1234 static bool any_io_in_flight(void)
1236 struct thread_data *td;
1239 for_each_td(td, i) {
1240 if (td->io_u_in_flight)
1248 * Modify the offset of an I/O unit that does not refer to an open zone such
1249 * that it refers to an open zone. Close an open zone and open a new zone if
1250 * necessary. The open zone is searched across sequential zones.
1251 * This algorithm can only work correctly if all write pointers are
1252 * a multiple of the fio block size. The caller must neither hold z->mutex
1253 * nor f->zbd_info->mutex. Returns with z->mutex held upon success.
1255 static struct fio_zone_info *zbd_convert_to_open_zone(struct thread_data *td,
1258 const uint64_t min_bs = td->o.min_bs[io_u->ddir];
1259 struct fio_file *f = io_u->file;
1260 struct zoned_block_device_info *zbdi = f->zbd_info;
1261 struct fio_zone_info *z;
1262 unsigned int open_zone_idx = -1;
1263 uint32_t zone_idx, new_zone_idx;
1265 bool wait_zone_close;
1267 bool should_retry = true;
1269 assert(is_valid_offset(f, io_u->offset));
1271 if (zbdi->max_open_zones || td->o.job_max_open_zones) {
1273 * This statement accesses zbdi->open_zones[] on purpose
1276 zone_idx = zbdi->open_zones[pick_random_zone_idx(f, io_u)];
1278 zone_idx = zbd_zone_idx(f, io_u->offset);
1280 if (zone_idx < f->min_zone)
1281 zone_idx = f->min_zone;
1282 else if (zone_idx >= f->max_zone)
1283 zone_idx = f->max_zone - 1;
1286 "%s(%s): starting from zone %d (offset %lld, buflen %lld)\n",
1287 __func__, f->file_name, zone_idx, io_u->offset, io_u->buflen);
1290 * Since z->mutex is the outer lock and zbdi->mutex the inner
1291 * lock it can happen that the state of the zone with index zone_idx
1292 * has changed after 'z' has been assigned and before zbdi->mutex
1293 * has been obtained. Hence the loop.
1298 z = get_zone(f, zone_idx);
1300 zone_lock(td, f, z);
1302 pthread_mutex_lock(&zbdi->mutex);
1305 if (z->cond != ZBD_ZONE_COND_OFFLINE &&
1306 zbdi->max_open_zones == 0 &&
1307 td->o.job_max_open_zones == 0)
1309 if (zbdi->num_open_zones == 0) {
1310 dprint(FD_ZBD, "%s(%s): no zones are open\n",
1311 __func__, f->file_name);
1312 goto open_other_zone;
1317 * List of opened zones is per-device, shared across all
1318 * threads. Start with quasi-random candidate zone. Ignore
1319 * zones which don't belong to thread's offset/size area.
1321 open_zone_idx = pick_random_zone_idx(f, io_u);
1322 assert(!open_zone_idx ||
1323 open_zone_idx < zbdi->num_open_zones);
1324 tmp_idx = open_zone_idx;
1326 for (i = 0; i < zbdi->num_open_zones; i++) {
1329 if (tmp_idx >= zbdi->num_open_zones)
1331 tmpz = zbdi->open_zones[tmp_idx];
1332 if (f->min_zone <= tmpz && tmpz < f->max_zone) {
1333 open_zone_idx = tmp_idx;
1334 goto found_candidate_zone;
1340 dprint(FD_ZBD, "%s(%s): no candidate zone\n",
1341 __func__, f->file_name);
1343 pthread_mutex_unlock(&zbdi->mutex);
1350 found_candidate_zone:
1351 new_zone_idx = zbdi->open_zones[open_zone_idx];
1352 if (new_zone_idx == zone_idx)
1354 zone_idx = new_zone_idx;
1356 pthread_mutex_unlock(&zbdi->mutex);
1362 /* Both z->mutex and zbdi->mutex are held. */
1365 if (z->wp + min_bs <= zbd_zone_capacity_end(z)) {
1366 pthread_mutex_unlock(&zbdi->mutex);
1371 /* Check if number of open zones reaches one of limits. */
1373 zbdi->num_open_zones == f->max_zone - f->min_zone ||
1374 (zbdi->max_open_zones &&
1375 zbdi->num_open_zones == zbdi->max_open_zones) ||
1376 (td->o.job_max_open_zones &&
1377 td->num_open_zones == td->o.job_max_open_zones);
1379 pthread_mutex_unlock(&zbdi->mutex);
1381 /* Only z->mutex is held. */
1384 * When number of open zones reaches to one of limits, wait for
1385 * zone close before opening a new zone.
1387 if (wait_zone_close) {
1389 "%s(%s): quiesce to allow open zones to close\n",
1390 __func__, f->file_name);
1395 /* Zone 'z' is full, so try to open a new zone. */
1396 for (i = f->io_size / zbdi->zone_size; i > 0; i--) {
1401 if (!is_valid_offset(f, z->start)) {
1403 zone_idx = f->min_zone;
1404 z = get_zone(f, zone_idx);
1406 assert(is_valid_offset(f, z->start));
1409 zone_lock(td, f, z);
1412 if (zbd_open_zone(td, f, zone_idx))
1416 /* Only z->mutex is held. */
1418 /* Check whether the write fits in any of the already opened zones. */
1419 pthread_mutex_lock(&zbdi->mutex);
1420 for (i = 0; i < zbdi->num_open_zones; i++) {
1421 zone_idx = zbdi->open_zones[i];
1422 if (zone_idx < f->min_zone || zone_idx >= f->max_zone)
1424 pthread_mutex_unlock(&zbdi->mutex);
1427 z = get_zone(f, zone_idx);
1429 zone_lock(td, f, z);
1430 if (z->wp + min_bs <= zbd_zone_capacity_end(z))
1432 pthread_mutex_lock(&zbdi->mutex);
1436 * When any I/O is in-flight or when all I/Os in-flight get completed,
1437 * the I/Os might have closed zones then retry the steps to open a zone.
1438 * Before retry, call io_u_quiesce() to complete in-flight writes.
1440 in_flight = any_io_in_flight();
1441 if (in_flight || should_retry) {
1443 "%s(%s): wait zone close and retry open zones\n",
1444 __func__, f->file_name);
1445 pthread_mutex_unlock(&zbdi->mutex);
1448 zone_lock(td, f, z);
1449 should_retry = in_flight;
1453 pthread_mutex_unlock(&zbdi->mutex);
1457 dprint(FD_ZBD, "%s(%s): did not open another zone\n",
1458 __func__, f->file_name);
1463 dprint(FD_ZBD, "%s(%s): returning zone %d\n",
1464 __func__, f->file_name, zone_idx);
1466 io_u->offset = z->start;
1468 assert(z->cond != ZBD_ZONE_COND_OFFLINE);
1473 /* The caller must hold z->mutex. */
1474 static struct fio_zone_info *zbd_replay_write_order(struct thread_data *td,
1476 struct fio_zone_info *z)
1478 const struct fio_file *f = io_u->file;
1479 const uint64_t min_bs = td->o.min_bs[DDIR_WRITE];
1481 if (!zbd_open_zone(td, f, zbd_zone_nr(f, z))) {
1483 z = zbd_convert_to_open_zone(td, io_u);
1487 if (z->verify_block * min_bs >= z->capacity) {
1488 log_err("%s: %d * %"PRIu64" >= %"PRIu64"\n",
1489 f->file_name, z->verify_block, min_bs, z->capacity);
1491 * If the assertion below fails during a test run, adding
1492 * "--experimental_verify=1" to the command line may help.
1497 io_u->offset = z->start + z->verify_block * min_bs;
1498 if (io_u->offset + io_u->buflen >= zbd_zone_capacity_end(z)) {
1499 log_err("%s: %llu + %llu >= %"PRIu64"\n",
1500 f->file_name, io_u->offset, io_u->buflen,
1501 zbd_zone_capacity_end(z));
1504 z->verify_block += io_u->buflen / min_bs;
1510 * Find another zone which has @min_bytes of readable data. Search in zones
1511 * @zb + 1 .. @zl. For random workload, also search in zones @zb - 1 .. @zf.
1513 * Either returns NULL or returns a zone pointer. When the zone has write
1514 * pointer, hold the mutex for the zone.
1516 static struct fio_zone_info *
1517 zbd_find_zone(struct thread_data *td, struct io_u *io_u, uint64_t min_bytes,
1518 struct fio_zone_info *zb, struct fio_zone_info *zl)
1520 struct fio_file *f = io_u->file;
1521 struct fio_zone_info *z1, *z2;
1522 const struct fio_zone_info *const zf = get_zone(f, f->min_zone);
1525 * Skip to the next non-empty zone in case of sequential I/O and to
1526 * the nearest non-empty zone in case of random I/O.
1528 for (z1 = zb + 1, z2 = zb - 1; z1 < zl || z2 >= zf; z1++, z2--) {
1529 if (z1 < zl && z1->cond != ZBD_ZONE_COND_OFFLINE) {
1531 zone_lock(td, f, z1);
1532 if (z1->start + min_bytes <= z1->wp)
1536 } else if (!td_random(td)) {
1540 if (td_random(td) && z2 >= zf &&
1541 z2->cond != ZBD_ZONE_COND_OFFLINE) {
1543 zone_lock(td, f, z2);
1544 if (z2->start + min_bytes <= z2->wp)
1552 "%s: no zone has %"PRIu64" bytes of readable data\n",
1553 f->file_name, min_bytes);
1559 * zbd_end_zone_io - update zone status at command completion
1561 * @z: zone info pointer
1563 * If the write command made the zone full, close it.
1565 * The caller must hold z->mutex.
1567 static void zbd_end_zone_io(struct thread_data *td, const struct io_u *io_u,
1568 struct fio_zone_info *z)
1570 const struct fio_file *f = io_u->file;
1572 if (io_u->ddir == DDIR_WRITE &&
1573 io_u->offset + io_u->buflen >= zbd_zone_capacity_end(z)) {
1574 pthread_mutex_lock(&f->zbd_info->mutex);
1575 zbd_close_zone(td, f, z);
1576 pthread_mutex_unlock(&f->zbd_info->mutex);
1581 * zbd_queue_io - update the write pointer of a sequential zone
1583 * @success: Whether or not the I/O unit has been queued successfully
1584 * @q: queueing status (busy, completed or queued).
1586 * For write and trim operations, update the write pointer of the I/O unit
1589 static void zbd_queue_io(struct thread_data *td, struct io_u *io_u, int q,
1592 const struct fio_file *f = io_u->file;
1593 struct zoned_block_device_info *zbd_info = f->zbd_info;
1594 struct fio_zone_info *z;
1600 zone_idx = zbd_zone_idx(f, io_u->offset);
1601 assert(zone_idx < zbd_info->nr_zones);
1602 z = get_zone(f, zone_idx);
1610 "%s: queued I/O (%lld, %llu) for zone %u\n",
1611 f->file_name, io_u->offset, io_u->buflen, zone_idx);
1613 switch (io_u->ddir) {
1615 zone_end = min((uint64_t)(io_u->offset + io_u->buflen),
1616 zbd_zone_capacity_end(z));
1619 * z->wp > zone_end means that one or more I/O errors
1622 pthread_mutex_lock(&zbd_info->mutex);
1623 if (z->wp <= zone_end) {
1624 zbd_info->sectors_with_data += zone_end - z->wp;
1625 zbd_info->wp_sectors_with_data += zone_end - z->wp;
1627 pthread_mutex_unlock(&zbd_info->mutex);
1634 if (q == FIO_Q_COMPLETED && !io_u->error)
1635 zbd_end_zone_io(td, io_u, z);
1638 if (!success || q != FIO_Q_QUEUED) {
1639 /* BUSY or COMPLETED: unlock the zone */
1641 io_u->zbd_put_io = NULL;
1646 * zbd_put_io - Unlock an I/O unit target zone lock
1649 static void zbd_put_io(struct thread_data *td, const struct io_u *io_u)
1651 const struct fio_file *f = io_u->file;
1652 struct zoned_block_device_info *zbd_info = f->zbd_info;
1653 struct fio_zone_info *z;
1658 zone_idx = zbd_zone_idx(f, io_u->offset);
1659 assert(zone_idx < zbd_info->nr_zones);
1660 z = get_zone(f, zone_idx);
1665 "%s: terminate I/O (%lld, %llu) for zone %u\n",
1666 f->file_name, io_u->offset, io_u->buflen, zone_idx);
1668 zbd_end_zone_io(td, io_u, z);
1671 zbd_check_swd(td, f);
1675 * Windows and MacOS do not define this.
1678 #define EREMOTEIO 121 /* POSIX value */
1681 bool zbd_unaligned_write(int error_code)
1683 switch (error_code) {
1692 * setup_zbd_zone_mode - handle zoneskip as necessary for ZBD drives
1693 * @td: FIO thread data.
1694 * @io_u: FIO I/O unit.
1696 * For sequential workloads, change the file offset to skip zoneskip bytes when
1697 * no more IO can be performed in the current zone.
1698 * - For read workloads, zoneskip is applied when the io has reached the end of
1699 * the zone or the zone write position (when td->o.read_beyond_wp is false).
1700 * - For write workloads, zoneskip is applied when the zone is full.
1701 * This applies only to read and write operations.
1703 void setup_zbd_zone_mode(struct thread_data *td, struct io_u *io_u)
1705 struct fio_file *f = io_u->file;
1706 enum fio_ddir ddir = io_u->ddir;
1707 struct fio_zone_info *z;
1710 assert(td->o.zone_mode == ZONE_MODE_ZBD);
1711 assert(td->o.zone_size);
1712 assert(f->zbd_info);
1714 zone_idx = zbd_zone_idx(f, f->last_pos[ddir]);
1715 z = get_zone(f, zone_idx);
1718 * When the zone capacity is smaller than the zone size and the I/O is
1719 * sequential write, skip to zone end if the latest position is at the
1720 * zone capacity limit.
1722 if (z->capacity < f->zbd_info->zone_size &&
1723 !td_random(td) && ddir == DDIR_WRITE &&
1724 f->last_pos[ddir] >= zbd_zone_capacity_end(z)) {
1726 "%s: Jump from zone capacity limit to zone end:"
1727 " (%"PRIu64" -> %"PRIu64") for zone %u (%"PRIu64")\n",
1728 f->file_name, f->last_pos[ddir],
1729 zbd_zone_end(z), zone_idx, z->capacity);
1730 td->io_skip_bytes += zbd_zone_end(z) - f->last_pos[ddir];
1731 f->last_pos[ddir] = zbd_zone_end(z);
1735 * zone_skip is valid only for sequential workloads.
1737 if (td_random(td) || !td->o.zone_skip)
1741 * It is time to switch to a new zone if:
1742 * - zone_bytes == zone_size bytes have already been accessed
1743 * - The last position reached the end of the current zone.
1744 * - For reads with td->o.read_beyond_wp == false, the last position
1745 * reached the zone write pointer.
1747 if (td->zone_bytes >= td->o.zone_size ||
1748 f->last_pos[ddir] >= zbd_zone_end(z) ||
1749 (ddir == DDIR_READ &&
1750 (!td->o.read_beyond_wp) && f->last_pos[ddir] >= z->wp)) {
1755 f->file_offset += td->o.zone_size + td->o.zone_skip;
1758 * Wrap from the beginning, if we exceed the file size
1760 if (f->file_offset >= f->real_file_size)
1761 f->file_offset = get_start_offset(td, f);
1763 f->last_pos[ddir] = f->file_offset;
1764 td->io_skip_bytes += td->o.zone_skip;
1769 * zbd_adjust_ddir - Adjust an I/O direction for zonemode=zbd.
1771 * @td: FIO thread data.
1772 * @io_u: FIO I/O unit.
1773 * @ddir: I/O direction before adjustment.
1775 * Return adjusted I/O direction.
1777 enum fio_ddir zbd_adjust_ddir(struct thread_data *td, struct io_u *io_u,
1781 * In case read direction is chosen for the first random I/O, fio with
1782 * zonemode=zbd stops because no data can be read from zoned block
1783 * devices with all empty zones. Overwrite the first I/O direction as
1784 * write to make sure data to read exists.
1786 assert(io_u->file->zbd_info);
1787 if (ddir != DDIR_READ || !td_rw(td))
1790 if (io_u->file->zbd_info->sectors_with_data ||
1791 td->o.read_beyond_wp)
1798 * zbd_adjust_block - adjust the offset and length as necessary for ZBD drives
1799 * @td: FIO thread data.
1800 * @io_u: FIO I/O unit.
1802 * Locking strategy: returns with z->mutex locked if and only if z refers
1803 * to a sequential zone and if io_u_accept is returned. z is the zone that
1804 * corresponds to io_u->offset at the end of this function.
1806 enum io_u_action zbd_adjust_block(struct thread_data *td, struct io_u *io_u)
1808 struct fio_file *f = io_u->file;
1809 struct zoned_block_device_info *zbdi = f->zbd_info;
1810 uint32_t zone_idx_b;
1811 struct fio_zone_info *zb, *zl, *orig_zb;
1812 uint32_t orig_len = io_u->buflen;
1813 uint64_t min_bs = td->o.min_bs[io_u->ddir];
1819 assert(is_valid_offset(f, io_u->offset));
1820 assert(io_u->buflen);
1822 zone_idx_b = zbd_zone_idx(f, io_u->offset);
1823 zb = get_zone(f, zone_idx_b);
1827 /* Accept non-write I/Os for conventional zones. */
1828 if (io_u->ddir != DDIR_WRITE)
1832 * Make sure that writes to conventional zones
1833 * don't cross over to any sequential zones.
1835 if (!(zb + 1)->has_wp ||
1836 io_u->offset + io_u->buflen <= (zb + 1)->start)
1839 if (io_u->offset + min_bs > (zb + 1)->start) {
1841 "%s: off=%llu + min_bs=%"PRIu64" > next zone %"PRIu64"\n",
1842 f->file_name, io_u->offset,
1843 min_bs, (zb + 1)->start);
1845 zb->start + (zb + 1)->start - io_u->offset;
1846 new_len = min(io_u->buflen,
1847 (zb + 1)->start - io_u->offset);
1849 new_len = (zb + 1)->start - io_u->offset;
1852 io_u->buflen = new_len / min_bs * min_bs;
1858 * Accept the I/O offset for reads if reading beyond the write pointer
1861 if (zb->cond != ZBD_ZONE_COND_OFFLINE &&
1862 io_u->ddir == DDIR_READ && td->o.read_beyond_wp)
1865 zbd_check_swd(td, f);
1867 zone_lock(td, f, zb);
1869 switch (io_u->ddir) {
1871 if (td->runstate == TD_VERIFYING && td_write(td)) {
1872 zb = zbd_replay_write_order(td, io_u, zb);
1877 * Check that there is enough written data in the zone to do an
1878 * I/O of at least min_bs B. If there isn't, find a new zone for
1881 range = zb->cond != ZBD_ZONE_COND_OFFLINE ?
1882 zb->wp - zb->start : 0;
1883 if (range < min_bs ||
1884 ((!td_random(td)) && (io_u->offset + min_bs > zb->wp))) {
1886 zl = get_zone(f, f->max_zone);
1887 zb = zbd_find_zone(td, io_u, min_bs, zb, zl);
1890 "%s: zbd_find_zone(%lld, %llu) failed\n",
1891 f->file_name, io_u->offset,
1896 * zbd_find_zone() returned a zone with a range of at
1899 range = zb->wp - zb->start;
1900 assert(range >= min_bs);
1903 io_u->offset = zb->start;
1907 * Make sure the I/O is within the zone valid data range while
1908 * maximizing the I/O size and preserving randomness.
1910 if (range <= io_u->buflen)
1911 io_u->offset = zb->start;
1912 else if (td_random(td))
1913 io_u->offset = zb->start +
1914 ((io_u->offset - orig_zb->start) %
1915 (range - io_u->buflen)) / min_bs * min_bs;
1918 * When zbd_find_zone() returns a conventional zone,
1919 * we can simply accept the new i/o offset here.
1925 * Make sure the I/O does not cross over the zone wp position.
1927 new_len = min((unsigned long long)io_u->buflen,
1928 (unsigned long long)(zb->wp - io_u->offset));
1929 new_len = new_len / min_bs * min_bs;
1930 if (new_len < io_u->buflen) {
1931 io_u->buflen = new_len;
1932 dprint(FD_IO, "Changed length from %u into %llu\n",
1933 orig_len, io_u->buflen);
1936 assert(zb->start <= io_u->offset);
1937 assert(io_u->offset + io_u->buflen <= zb->wp);
1942 if (io_u->buflen > zbdi->zone_size) {
1943 td_verror(td, EINVAL, "I/O buflen exceeds zone size");
1945 "%s: I/O buflen %llu exceeds zone size %"PRIu64"\n",
1946 f->file_name, io_u->buflen, zbdi->zone_size);
1950 if (!zbd_open_zone(td, f, zone_idx_b)) {
1952 zb = zbd_convert_to_open_zone(td, io_u);
1954 dprint(FD_IO, "%s: can't convert to open zone",
1960 /* Check whether the zone reset threshold has been exceeded */
1961 if (td->o.zrf.u.f) {
1962 if (zbdi->wp_sectors_with_data >= f->io_size * td->o.zrt.u.f &&
1963 zbd_dec_and_reset_write_cnt(td, f))
1967 /* Reset the zone pointer if necessary */
1968 if (zb->reset_zone || zbd_zone_full(f, zb, min_bs)) {
1969 assert(td->o.verify == VERIFY_NONE);
1971 * Since previous write requests may have been submitted
1972 * asynchronously and since we will submit the zone
1973 * reset synchronously, wait until previously submitted
1974 * write requests have completed before issuing a
1979 if (zbd_reset_zone(td, f, zb) < 0)
1982 if (zb->capacity < min_bs) {
1983 td_verror(td, EINVAL, "ZCAP is less min_bs");
1984 log_err("zone capacity %"PRIu64" smaller than minimum block size %"PRIu64"\n",
1985 zb->capacity, min_bs);
1990 /* Make writes occur at the write pointer */
1991 assert(!zbd_zone_full(f, zb, min_bs));
1992 io_u->offset = zb->wp;
1993 if (!is_valid_offset(f, io_u->offset)) {
1994 td_verror(td, EINVAL, "invalid WP value");
1995 dprint(FD_ZBD, "%s: dropped request with offset %llu\n",
1996 f->file_name, io_u->offset);
2001 * Make sure that the buflen is a multiple of the minimal
2002 * block size. Give up if shrinking would make the request too
2005 new_len = min((unsigned long long)io_u->buflen,
2006 zbd_zone_capacity_end(zb) - io_u->offset);
2007 new_len = new_len / min_bs * min_bs;
2008 if (new_len == io_u->buflen)
2010 if (new_len >= min_bs) {
2011 io_u->buflen = new_len;
2012 dprint(FD_IO, "Changed length from %u into %llu\n",
2013 orig_len, io_u->buflen);
2017 td_verror(td, EIO, "zone remainder too small");
2018 log_err("zone remainder %lld smaller than min block size %"PRIu64"\n",
2019 (zbd_zone_capacity_end(zb) - io_u->offset), min_bs);
2024 /* Check random trim targets a non-empty zone */
2025 if (!td_random(td) || zb->wp > zb->start)
2028 /* Find out a non-empty zone to trim */
2030 zl = get_zone(f, f->max_zone);
2031 zb = zbd_find_zone(td, io_u, 1, zb, zl);
2033 io_u->offset = zb->start;
2034 dprint(FD_ZBD, "%s: found new zone(%lld) for trim\n",
2035 f->file_name, io_u->offset);
2044 case DDIR_SYNC_FILE_RANGE:
2055 assert(zb->cond != ZBD_ZONE_COND_OFFLINE);
2056 assert(!io_u->zbd_queue_io);
2057 assert(!io_u->zbd_put_io);
2059 io_u->zbd_queue_io = zbd_queue_io;
2060 io_u->zbd_put_io = zbd_put_io;
2063 * Since we return with the zone lock still held,
2064 * add an annotation to let Coverity know that it
2067 /* coverity[missing_unlock] */
2072 if (zb && zb->has_wp)
2078 /* Return a string with ZBD statistics */
2079 char *zbd_write_status(const struct thread_stat *ts)
2083 if (asprintf(&res, "; %"PRIu64" zone resets", ts->nr_zone_resets) < 0)
2089 * zbd_do_io_u_trim - If reset zone is applicable, do reset zone instead of trim
2091 * @td: FIO thread data.
2092 * @io_u: FIO I/O unit.
2094 * It is assumed that z->mutex is already locked.
2095 * Return io_u_completed when reset zone succeeds. Return 0 when the target zone
2096 * does not have write pointer. On error, return negative errno.
2098 int zbd_do_io_u_trim(const struct thread_data *td, struct io_u *io_u)
2100 struct fio_file *f = io_u->file;
2101 struct fio_zone_info *z;
2105 zone_idx = zbd_zone_idx(f, io_u->offset);
2106 z = get_zone(f, zone_idx);
2111 if (io_u->offset != z->start) {
2112 log_err("Trim offset not at zone start (%lld)\n",
2117 ret = zbd_reset_zone((struct thread_data *)td, f, z);
2121 return io_u_completed;