2 * Copyright (C) 2018 Western Digital Corporation or its affiliates.
4 * This file is released under the GPL.
19 #include "oslib/asprintf.h"
26 * zbd_get_zoned_model - Get a device zoned model
27 * @td: FIO thread data
28 * @f: FIO file for which to get model information
30 int zbd_get_zoned_model(struct thread_data *td, struct fio_file *f,
31 enum zbd_zoned_model *model)
35 if (f->filetype == FIO_TYPE_PIPE) {
36 log_err("zonemode=zbd does not support pipes\n");
40 /* If regular file, always emulate zones inside the file. */
41 if (f->filetype == FIO_TYPE_FILE) {
46 if (td->io_ops && td->io_ops->get_zoned_model)
47 ret = td->io_ops->get_zoned_model(td, f, model);
49 ret = blkzoned_get_zoned_model(td, f, model);
51 td_verror(td, errno, "get zoned model failed");
52 log_err("%s: get zoned model failed (%d).\n",
60 * zbd_report_zones - Get zone information
61 * @td: FIO thread data.
62 * @f: FIO file for which to get zone information
63 * @offset: offset from which to report zones
64 * @zones: Array of struct zbd_zone
65 * @nr_zones: Size of @zones array
67 * Get zone information into @zones starting from the zone at offset @offset
68 * for the device specified by @f.
70 * Returns the number of zones reported upon success and a negative error code
71 * upon failure. If the zone report is empty, always assume an error (device
72 * problem) and return -EIO.
74 int zbd_report_zones(struct thread_data *td, struct fio_file *f,
75 uint64_t offset, struct zbd_zone *zones,
76 unsigned int nr_zones)
80 if (td->io_ops && td->io_ops->report_zones)
81 ret = td->io_ops->report_zones(td, f, offset, zones, nr_zones);
83 ret = blkzoned_report_zones(td, f, offset, zones, nr_zones);
85 td_verror(td, errno, "report zones failed");
86 log_err("%s: report zones from sector %llu failed (%d).\n",
87 f->file_name, (unsigned long long)offset >> 9, errno);
88 } else if (ret == 0) {
89 td_verror(td, errno, "Empty zone report");
90 log_err("%s: report zones from sector %llu is empty.\n",
91 f->file_name, (unsigned long long)offset >> 9);
99 * zbd_reset_wp - reset the write pointer of a range of zones
100 * @td: FIO thread data.
101 * @f: FIO file for which to reset zones
102 * @offset: Starting offset of the first zone to reset
103 * @length: Length of the range of zones to reset
105 * Reset the write pointer of all zones in the range @offset...@offset+@length.
106 * Returns 0 upon success and a negative error code upon failure.
108 int zbd_reset_wp(struct thread_data *td, struct fio_file *f,
109 uint64_t offset, uint64_t length)
113 if (td->io_ops && td->io_ops->reset_wp)
114 ret = td->io_ops->reset_wp(td, f, offset, length);
116 ret = blkzoned_reset_wp(td, f, offset, length);
118 td_verror(td, errno, "resetting wp failed");
119 log_err("%s: resetting wp for %llu sectors at sector %llu failed (%d).\n",
120 f->file_name, (unsigned long long)length >> 9,
121 (unsigned long long)offset >> 9, errno);
128 * zbd_get_max_open_zones - Get the maximum number of open zones
129 * @td: FIO thread data
130 * @f: FIO file for which to get max open zones
131 * @max_open_zones: Upon success, result will be stored here.
133 * A @max_open_zones value set to zero means no limit.
135 * Returns 0 upon success and a negative error code upon failure.
137 int zbd_get_max_open_zones(struct thread_data *td, struct fio_file *f,
138 unsigned int *max_open_zones)
142 if (td->io_ops && td->io_ops->get_max_open_zones)
143 ret = td->io_ops->get_max_open_zones(td, f, max_open_zones);
145 ret = blkzoned_get_max_open_zones(td, f, max_open_zones);
147 td_verror(td, errno, "get max open zones failed");
148 log_err("%s: get max open zones failed (%d).\n",
149 f->file_name, errno);
156 * zbd_zone_idx - convert an offset into a zone number
158 * @offset: offset in bytes. If this offset is in the first zone_size bytes
159 * past the disk size then the index of the sentinel is returned.
161 static uint32_t zbd_zone_idx(const struct fio_file *f, uint64_t offset)
165 if (f->zbd_info->zone_size_log2 > 0)
166 zone_idx = offset >> f->zbd_info->zone_size_log2;
168 zone_idx = offset / f->zbd_info->zone_size;
170 return min(zone_idx, f->zbd_info->nr_zones);
174 * zbd_zone_end - Return zone end location
175 * @z: zone info pointer.
177 static inline uint64_t zbd_zone_end(const struct fio_zone_info *z)
183 * zbd_zone_capacity_end - Return zone capacity limit end location
184 * @z: zone info pointer.
186 static inline uint64_t zbd_zone_capacity_end(const struct fio_zone_info *z)
188 return z->start + z->capacity;
192 * zbd_zone_full - verify whether a minimum number of bytes remain in a zone
194 * @z: zone info pointer.
195 * @required: minimum number of bytes that must remain in a zone.
197 * The caller must hold z->mutex.
199 static bool zbd_zone_full(const struct fio_file *f, struct fio_zone_info *z,
202 assert((required & 511) == 0);
205 z->wp + required > zbd_zone_capacity_end(z);
208 static void zone_lock(struct thread_data *td, const struct fio_file *f,
209 struct fio_zone_info *z)
211 struct zoned_block_device_info *zbd = f->zbd_info;
212 uint32_t nz = z - zbd->zone_info;
214 /* A thread should never lock zones outside its working area. */
215 assert(f->min_zone <= nz && nz < f->max_zone);
220 * Lock the io_u target zone. The zone will be unlocked if io_u offset
221 * is changed or when io_u completes and zbd_put_io() executed.
222 * To avoid multiple jobs doing asynchronous I/Os from deadlocking each
223 * other waiting for zone locks when building an io_u batch, first
224 * only trylock the zone. If the zone is already locked by another job,
225 * process the currently queued I/Os so that I/O progress is made and
228 if (pthread_mutex_trylock(&z->mutex) != 0) {
229 if (!td_ioengine_flagged(td, FIO_SYNCIO))
231 pthread_mutex_lock(&z->mutex);
235 static inline void zone_unlock(struct fio_zone_info *z)
240 ret = pthread_mutex_unlock(&z->mutex);
244 static bool is_valid_offset(const struct fio_file *f, uint64_t offset)
246 return (uint64_t)(offset - f->file_offset) < f->io_size;
249 static inline struct fio_zone_info *get_zone(const struct fio_file *f,
250 unsigned int zone_nr)
252 return &f->zbd_info->zone_info[zone_nr];
255 /* Verify whether direct I/O is used for all host-managed zoned drives. */
256 static bool zbd_using_direct_io(void)
258 struct thread_data *td;
263 if (td->o.odirect || !(td->o.td_ddir & TD_DDIR_WRITE))
265 for_each_file(td, f, j) {
267 f->zbd_info->model == ZBD_HOST_MANAGED)
275 /* Whether or not the I/O range for f includes one or more sequential zones */
276 static bool zbd_is_seq_job(struct fio_file *f)
278 uint32_t zone_idx, zone_idx_b, zone_idx_e;
283 zone_idx_b = zbd_zone_idx(f, f->file_offset);
284 zone_idx_e = zbd_zone_idx(f, f->file_offset + f->io_size - 1);
285 for (zone_idx = zone_idx_b; zone_idx <= zone_idx_e; zone_idx++)
286 if (get_zone(f, zone_idx)->has_wp)
293 * Verify whether offset and size parameters are aligned with zone boundaries.
295 static bool zbd_verify_sizes(void)
297 const struct fio_zone_info *z;
298 struct thread_data *td;
300 uint64_t new_offset, new_end;
305 for_each_file(td, f, j) {
308 if (f->file_offset >= f->real_file_size)
310 if (!zbd_is_seq_job(f))
313 if (!td->o.zone_size) {
314 td->o.zone_size = f->zbd_info->zone_size;
315 if (!td->o.zone_size) {
316 log_err("%s: invalid 0 zone size\n",
320 } else if (td->o.zone_size != f->zbd_info->zone_size) {
321 log_err("%s: job parameter zonesize %llu does not match disk zone size %llu.\n",
322 f->file_name, (unsigned long long) td->o.zone_size,
323 (unsigned long long) f->zbd_info->zone_size);
327 if (td->o.zone_skip % td->o.zone_size) {
328 log_err("%s: zoneskip %llu is not a multiple of the device zone size %llu.\n",
329 f->file_name, (unsigned long long) td->o.zone_skip,
330 (unsigned long long) td->o.zone_size);
334 zone_idx = zbd_zone_idx(f, f->file_offset);
335 z = get_zone(f, zone_idx);
336 if ((f->file_offset != z->start) &&
337 (td->o.td_ddir != TD_DDIR_READ)) {
338 new_offset = zbd_zone_end(z);
339 if (new_offset >= f->file_offset + f->io_size) {
340 log_info("%s: io_size must be at least one zone\n",
344 log_info("%s: rounded up offset from %llu to %llu\n",
345 f->file_name, (unsigned long long) f->file_offset,
346 (unsigned long long) new_offset);
347 f->io_size -= (new_offset - f->file_offset);
348 f->file_offset = new_offset;
350 zone_idx = zbd_zone_idx(f, f->file_offset + f->io_size);
351 z = get_zone(f, zone_idx);
353 if ((td->o.td_ddir != TD_DDIR_READ) &&
354 (f->file_offset + f->io_size != new_end)) {
355 if (new_end <= f->file_offset) {
356 log_info("%s: io_size must be at least one zone\n",
360 log_info("%s: rounded down io_size from %llu to %llu\n",
361 f->file_name, (unsigned long long) f->io_size,
362 (unsigned long long) new_end - f->file_offset);
363 f->io_size = new_end - f->file_offset;
371 static bool zbd_verify_bs(void)
373 struct thread_data *td;
378 for_each_file(td, f, j) {
383 zone_size = f->zbd_info->zone_size;
384 for (k = 0; k < FIO_ARRAY_SIZE(td->o.bs); k++) {
385 if (td->o.verify != VERIFY_NONE &&
386 zone_size % td->o.bs[k] != 0) {
387 log_info("%s: block size %llu is not a divisor of the zone size %llu\n",
388 f->file_name, td->o.bs[k],
389 (unsigned long long)zone_size);
398 static int ilog2(uint64_t i)
410 * Initialize f->zbd_info for devices that are not zoned block devices. This
411 * allows to execute a ZBD workload against a non-ZBD device.
413 static int init_zone_info(struct thread_data *td, struct fio_file *f)
416 struct fio_zone_info *p;
417 uint64_t zone_size = td->o.zone_size;
418 uint64_t zone_capacity = td->o.zone_capacity;
419 struct zoned_block_device_info *zbd_info = NULL;
422 if (zone_size == 0) {
423 log_err("%s: Specifying the zone size is mandatory for regular file/block device with --zonemode=zbd\n\n",
428 if (zone_size < 512) {
429 log_err("%s: zone size must be at least 512 bytes for --zonemode=zbd\n\n",
434 if (zone_capacity == 0)
435 zone_capacity = zone_size;
437 if (zone_capacity > zone_size) {
438 log_err("%s: job parameter zonecapacity %llu is larger than zone size %llu\n",
439 f->file_name, (unsigned long long) td->o.zone_capacity,
440 (unsigned long long) td->o.zone_size);
444 if (f->real_file_size < zone_size) {
445 log_err("%s: file/device size %"PRIu64" is smaller than zone size %"PRIu64"\n",
446 f->file_name, f->real_file_size, zone_size);
450 nr_zones = (f->real_file_size + zone_size - 1) / zone_size;
451 zbd_info = scalloc(1, sizeof(*zbd_info) +
452 (nr_zones + 1) * sizeof(zbd_info->zone_info[0]));
456 mutex_init_pshared(&zbd_info->mutex);
457 zbd_info->refcount = 1;
458 p = &zbd_info->zone_info[0];
459 for (i = 0; i < nr_zones; i++, p++) {
460 mutex_init_pshared_with_type(&p->mutex,
461 PTHREAD_MUTEX_RECURSIVE);
462 p->start = i * zone_size;
464 p->type = ZBD_ZONE_TYPE_SWR;
465 p->cond = ZBD_ZONE_COND_EMPTY;
466 p->capacity = zone_capacity;
470 p->start = nr_zones * zone_size;
472 f->zbd_info = zbd_info;
473 f->zbd_info->zone_size = zone_size;
474 f->zbd_info->zone_size_log2 = is_power_of_2(zone_size) ?
475 ilog2(zone_size) : 0;
476 f->zbd_info->nr_zones = nr_zones;
481 * Maximum number of zones to report in one operation.
483 #define ZBD_REPORT_MAX_ZONES 8192U
486 * Parse the device zone report and store it in f->zbd_info. Must be called
487 * only for devices that are zoned, namely those with a model != ZBD_NONE.
489 static int parse_zone_info(struct thread_data *td, struct fio_file *f)
492 struct zbd_zone *zones, *z;
493 struct fio_zone_info *p;
494 uint64_t zone_size, offset;
495 struct zoned_block_device_info *zbd_info = NULL;
496 int i, j, ret = -ENOMEM;
498 zones = calloc(ZBD_REPORT_MAX_ZONES, sizeof(struct zbd_zone));
502 nrz = zbd_report_zones(td, f, 0, zones, ZBD_REPORT_MAX_ZONES);
505 log_info("fio: report zones (offset 0) failed for %s (%d).\n",
510 zone_size = zones[0].len;
511 nr_zones = (f->real_file_size + zone_size - 1) / zone_size;
513 if (td->o.zone_size == 0) {
514 td->o.zone_size = zone_size;
515 } else if (td->o.zone_size != zone_size) {
516 log_err("fio: %s job parameter zonesize %llu does not match disk zone size %llu.\n",
517 f->file_name, (unsigned long long) td->o.zone_size,
518 (unsigned long long) zone_size);
523 dprint(FD_ZBD, "Device %s has %d zones of size %llu KB\n", f->file_name,
524 nr_zones, (unsigned long long) zone_size / 1024);
526 zbd_info = scalloc(1, sizeof(*zbd_info) +
527 (nr_zones + 1) * sizeof(zbd_info->zone_info[0]));
530 mutex_init_pshared(&zbd_info->mutex);
531 zbd_info->refcount = 1;
532 p = &zbd_info->zone_info[0];
533 for (offset = 0, j = 0; j < nr_zones;) {
535 for (i = 0; i < nrz; i++, j++, z++, p++) {
536 mutex_init_pshared_with_type(&p->mutex,
537 PTHREAD_MUTEX_RECURSIVE);
539 p->capacity = z->capacity;
541 case ZBD_ZONE_COND_NOT_WP:
542 case ZBD_ZONE_COND_FULL:
543 p->wp = p->start + p->capacity;
546 assert(z->start <= z->wp);
547 assert(z->wp <= z->start + zone_size);
553 case ZBD_ZONE_TYPE_SWR:
562 if (j > 0 && p->start != p[-1].start + zone_size) {
563 log_info("%s: invalid zone data\n",
570 offset = z->start + z->len;
573 nrz = zbd_report_zones(td, f, offset, zones,
574 min((uint32_t)(nr_zones - j),
575 ZBD_REPORT_MAX_ZONES));
578 log_info("fio: report zones (offset %llu) failed for %s (%d).\n",
579 (unsigned long long)offset,
586 zbd_info->zone_info[nr_zones].start = offset;
588 f->zbd_info = zbd_info;
589 f->zbd_info->zone_size = zone_size;
590 f->zbd_info->zone_size_log2 = is_power_of_2(zone_size) ?
591 ilog2(zone_size) : 0;
592 f->zbd_info->nr_zones = nr_zones;
602 static int zbd_set_max_open_zones(struct thread_data *td, struct fio_file *f)
604 struct zoned_block_device_info *zbd = f->zbd_info;
605 unsigned int max_open_zones;
608 if (zbd->model != ZBD_HOST_MANAGED || td->o.ignore_zone_limits) {
609 /* Only host-managed devices have a max open limit */
610 zbd->max_open_zones = td->o.max_open_zones;
614 /* If host-managed, get the max open limit */
615 ret = zbd_get_max_open_zones(td, f, &max_open_zones);
619 if (!max_open_zones) {
620 /* No device limit */
621 zbd->max_open_zones = td->o.max_open_zones;
622 } else if (!td->o.max_open_zones) {
623 /* No user limit. Set limit to device limit */
624 zbd->max_open_zones = max_open_zones;
625 } else if (td->o.max_open_zones <= max_open_zones) {
626 /* Both user limit and dev limit. User limit not too large */
627 zbd->max_open_zones = td->o.max_open_zones;
629 /* Both user limit and dev limit. User limit too large */
630 td_verror(td, EINVAL,
631 "Specified --max_open_zones is too large");
632 log_err("Specified --max_open_zones (%d) is larger than max (%u)\n",
633 td->o.max_open_zones, max_open_zones);
638 /* Ensure that the limit is not larger than FIO's internal limit */
639 if (zbd->max_open_zones > ZBD_MAX_OPEN_ZONES) {
640 td_verror(td, EINVAL, "'max_open_zones' value is too large");
641 log_err("'max_open_zones' value is larger than %u\n", ZBD_MAX_OPEN_ZONES);
645 dprint(FD_ZBD, "%s: using max open zones limit: %"PRIu32"\n",
646 f->file_name, zbd->max_open_zones);
652 * Allocate zone information and store it into f->zbd_info if zonemode=zbd.
654 * Returns 0 upon success and a negative error code upon failure.
656 static int zbd_create_zone_info(struct thread_data *td, struct fio_file *f)
658 enum zbd_zoned_model zbd_model;
661 assert(td->o.zone_mode == ZONE_MODE_ZBD);
663 ret = zbd_get_zoned_model(td, f, &zbd_model);
669 case ZBD_HOST_MANAGED:
670 ret = parse_zone_info(td, f);
675 ret = init_zone_info(td, f);
680 td_verror(td, EINVAL, "Unsupported zoned model");
681 log_err("Unsupported zoned model\n");
686 f->zbd_info->model = zbd_model;
688 ret = zbd_set_max_open_zones(td, f);
690 zbd_free_zone_info(f);
697 void zbd_free_zone_info(struct fio_file *f)
703 pthread_mutex_lock(&f->zbd_info->mutex);
704 refcount = --f->zbd_info->refcount;
705 pthread_mutex_unlock(&f->zbd_info->mutex);
707 assert((int32_t)refcount >= 0);
714 * Initialize f->zbd_info.
716 * Returns 0 upon success and a negative error code upon failure.
718 * Note: this function can only work correctly if it is called before the first
721 static int zbd_init_zone_info(struct thread_data *td, struct fio_file *file)
723 struct thread_data *td2;
727 for_each_td(td2, i) {
728 for_each_file(td2, f2, j) {
729 if (td2 == td && f2 == file)
732 strcmp(f2->file_name, file->file_name) != 0)
734 file->zbd_info = f2->zbd_info;
735 file->zbd_info->refcount++;
740 ret = zbd_create_zone_info(td, file);
742 td_verror(td, -ret, "zbd_create_zone_info() failed");
746 static bool zbd_open_zone(struct thread_data *td, const struct fio_file *f,
748 static int zbd_reset_zone(struct thread_data *td, struct fio_file *f,
749 struct fio_zone_info *z);
751 int zbd_init_files(struct thread_data *td)
756 for_each_file(td, f, i) {
757 if (zbd_init_zone_info(td, f))
763 void zbd_recalc_options_with_zone_granularity(struct thread_data *td)
768 for_each_file(td, f, i) {
769 struct zoned_block_device_info *zbd = f->zbd_info;
770 // zonemode=strided doesn't get per-file zone size.
771 uint64_t zone_size = zbd ? zbd->zone_size : td->o.zone_size;
776 if (td->o.size_nz > 0) {
777 td->o.size = td->o.size_nz * zone_size;
779 if (td->o.io_size_nz > 0) {
780 td->o.io_size = td->o.io_size_nz * zone_size;
782 if (td->o.start_offset_nz > 0) {
783 td->o.start_offset = td->o.start_offset_nz * zone_size;
785 if (td->o.offset_increment_nz > 0) {
786 td->o.offset_increment = td->o.offset_increment_nz * zone_size;
788 if (td->o.zone_skip_nz > 0) {
789 td->o.zone_skip = td->o.zone_skip_nz * zone_size;
794 int zbd_setup_files(struct thread_data *td)
799 if (!zbd_using_direct_io()) {
800 log_err("Using direct I/O is mandatory for writing to ZBD drives\n\n");
804 if (!zbd_verify_sizes())
807 if (!zbd_verify_bs())
810 for_each_file(td, f, i) {
811 struct zoned_block_device_info *zbd = f->zbd_info;
812 struct fio_zone_info *z;
817 f->min_zone = zbd_zone_idx(f, f->file_offset);
818 f->max_zone = zbd_zone_idx(f, f->file_offset + f->io_size);
821 * When all zones in the I/O range are conventional, io_size
822 * can be smaller than zone size, making min_zone the same
823 * as max_zone. This is why the assert below needs to be made
826 if (zbd_is_seq_job(f))
827 assert(f->min_zone < f->max_zone);
829 if (td->o.max_open_zones > 0 &&
830 zbd->max_open_zones != td->o.max_open_zones) {
831 log_err("Different 'max_open_zones' values\n");
836 * The per job max open zones limit cannot be used without a
837 * global max open zones limit. (As the tracking of open zones
838 * is disabled when there is no global max open zones limit.)
840 if (td->o.job_max_open_zones && !zbd->max_open_zones) {
841 log_err("'job_max_open_zones' cannot be used without a global open zones limit\n");
846 * zbd->max_open_zones is the global limit shared for all jobs
847 * that target the same zoned block device. Force sync the per
848 * thread global limit with the actual global limit. (The real
849 * per thread/job limit is stored in td->o.job_max_open_zones).
851 td->o.max_open_zones = zbd->max_open_zones;
853 for (zi = f->min_zone; zi < f->max_zone; zi++) {
854 z = &zbd->zone_info[zi];
855 if (z->cond != ZBD_ZONE_COND_IMP_OPEN &&
856 z->cond != ZBD_ZONE_COND_EXP_OPEN)
858 if (zbd_open_zone(td, f, zi))
861 * If the number of open zones exceeds specified limits,
862 * reset all extra open zones.
864 if (zbd_reset_zone(td, f, z) < 0) {
865 log_err("Failed to reest zone %d\n", zi);
874 static inline unsigned int zbd_zone_nr(const struct fio_file *f,
875 struct fio_zone_info *zone)
877 return zone - f->zbd_info->zone_info;
881 * zbd_reset_zone - reset the write pointer of a single zone
882 * @td: FIO thread data.
883 * @f: FIO file associated with the disk for which to reset a write pointer.
886 * Returns 0 upon success and a negative error code upon failure.
888 * The caller must hold z->mutex.
890 static int zbd_reset_zone(struct thread_data *td, struct fio_file *f,
891 struct fio_zone_info *z)
893 uint64_t offset = z->start;
894 uint64_t length = (z+1)->start - offset;
895 uint64_t data_in_zone = z->wp - z->start;
901 assert(is_valid_offset(f, offset + length - 1));
903 dprint(FD_ZBD, "%s: resetting wp of zone %u.\n", f->file_name,
905 switch (f->zbd_info->model) {
907 case ZBD_HOST_MANAGED:
908 ret = zbd_reset_wp(td, f, offset, length);
916 pthread_mutex_lock(&f->zbd_info->mutex);
917 f->zbd_info->sectors_with_data -= data_in_zone;
918 f->zbd_info->wp_sectors_with_data -= data_in_zone;
919 pthread_mutex_unlock(&f->zbd_info->mutex);
923 td->ts.nr_zone_resets++;
928 /* The caller must hold f->zbd_info->mutex */
929 static void zbd_close_zone(struct thread_data *td, const struct fio_file *f,
930 unsigned int zone_idx)
932 uint32_t open_zone_idx = 0;
934 for (; open_zone_idx < f->zbd_info->num_open_zones; open_zone_idx++) {
935 if (f->zbd_info->open_zones[open_zone_idx] == zone_idx)
938 if (open_zone_idx == f->zbd_info->num_open_zones)
941 dprint(FD_ZBD, "%s: closing zone %d\n", f->file_name, zone_idx);
942 memmove(f->zbd_info->open_zones + open_zone_idx,
943 f->zbd_info->open_zones + open_zone_idx + 1,
944 (ZBD_MAX_OPEN_ZONES - (open_zone_idx + 1)) *
945 sizeof(f->zbd_info->open_zones[0]));
946 f->zbd_info->num_open_zones--;
947 td->num_open_zones--;
948 get_zone(f, zone_idx)->open = 0;
952 * Reset a range of zones. Returns 0 upon success and 1 upon failure.
953 * @td: fio thread data.
954 * @f: fio file for which to reset zones
955 * @zb: first zone to reset.
956 * @ze: first zone not to reset.
958 static int zbd_reset_zones(struct thread_data *td, struct fio_file *f,
959 struct fio_zone_info *const zb,
960 struct fio_zone_info *const ze)
962 struct fio_zone_info *z;
963 const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
968 dprint(FD_ZBD, "%s: examining zones %u .. %u\n", f->file_name,
969 zbd_zone_nr(f, zb), zbd_zone_nr(f, ze));
970 for (z = zb; z < ze; z++) {
971 uint32_t nz = zbd_zone_nr(f, z);
976 pthread_mutex_lock(&f->zbd_info->mutex);
977 zbd_close_zone(td, f, nz);
978 pthread_mutex_unlock(&f->zbd_info->mutex);
979 if (z->wp != z->start) {
980 dprint(FD_ZBD, "%s: resetting zone %u\n",
981 f->file_name, zbd_zone_nr(f, z));
982 if (zbd_reset_zone(td, f, z) < 0)
992 * Reset zbd_info.write_cnt, the counter that counts down towards the next
995 static void _zbd_reset_write_cnt(const struct thread_data *td,
996 const struct fio_file *f)
998 assert(0 <= td->o.zrf.u.f && td->o.zrf.u.f <= 1);
1000 f->zbd_info->write_cnt = td->o.zrf.u.f ?
1001 min(1.0 / td->o.zrf.u.f, 0.0 + UINT_MAX) : UINT_MAX;
1004 static void zbd_reset_write_cnt(const struct thread_data *td,
1005 const struct fio_file *f)
1007 pthread_mutex_lock(&f->zbd_info->mutex);
1008 _zbd_reset_write_cnt(td, f);
1009 pthread_mutex_unlock(&f->zbd_info->mutex);
1012 static bool zbd_dec_and_reset_write_cnt(const struct thread_data *td,
1013 const struct fio_file *f)
1015 uint32_t write_cnt = 0;
1017 pthread_mutex_lock(&f->zbd_info->mutex);
1018 assert(f->zbd_info->write_cnt);
1019 if (f->zbd_info->write_cnt)
1020 write_cnt = --f->zbd_info->write_cnt;
1022 _zbd_reset_write_cnt(td, f);
1023 pthread_mutex_unlock(&f->zbd_info->mutex);
1025 return write_cnt == 0;
1033 /* Calculate the number of sectors with data (swd) and perform action 'a' */
1034 static uint64_t zbd_process_swd(struct thread_data *td,
1035 const struct fio_file *f, enum swd_action a)
1037 struct fio_zone_info *zb, *ze, *z;
1039 uint64_t wp_swd = 0;
1041 zb = get_zone(f, f->min_zone);
1042 ze = get_zone(f, f->max_zone);
1043 for (z = zb; z < ze; z++) {
1045 zone_lock(td, f, z);
1046 wp_swd += z->wp - z->start;
1048 swd += z->wp - z->start;
1050 pthread_mutex_lock(&f->zbd_info->mutex);
1053 assert(f->zbd_info->sectors_with_data == swd);
1054 assert(f->zbd_info->wp_sectors_with_data == wp_swd);
1057 f->zbd_info->sectors_with_data = swd;
1058 f->zbd_info->wp_sectors_with_data = wp_swd;
1061 pthread_mutex_unlock(&f->zbd_info->mutex);
1062 for (z = zb; z < ze; z++)
1070 * The swd check is useful for debugging but takes too much time to leave
1071 * it enabled all the time. Hence it is disabled by default.
1073 static const bool enable_check_swd = false;
1075 /* Check whether the values of zbd_info.*sectors_with_data are correct. */
1076 static void zbd_check_swd(struct thread_data *td, const struct fio_file *f)
1078 if (!enable_check_swd)
1081 zbd_process_swd(td, f, CHECK_SWD);
1084 void zbd_file_reset(struct thread_data *td, struct fio_file *f)
1086 struct fio_zone_info *zb, *ze;
1089 if (!f->zbd_info || !td_write(td))
1092 zb = get_zone(f, f->min_zone);
1093 ze = get_zone(f, f->max_zone);
1094 swd = zbd_process_swd(td, f, SET_SWD);
1095 dprint(FD_ZBD, "%s(%s): swd = %" PRIu64 "\n", __func__, f->file_name,
1098 * If data verification is enabled reset the affected zones before
1099 * writing any data to avoid that a zone reset has to be issued while
1100 * writing data, which causes data loss.
1102 if (td->o.verify != VERIFY_NONE && td->runstate != TD_VERIFYING)
1103 zbd_reset_zones(td, f, zb, ze);
1104 zbd_reset_write_cnt(td, f);
1107 /* The caller must hold f->zbd_info->mutex. */
1108 static bool is_zone_open(const struct thread_data *td, const struct fio_file *f,
1109 unsigned int zone_idx)
1111 struct zoned_block_device_info *zbdi = f->zbd_info;
1114 /* This function should never be called when zbdi->max_open_zones == 0 */
1115 assert(zbdi->max_open_zones);
1116 assert(td->o.job_max_open_zones == 0 || td->num_open_zones <= td->o.job_max_open_zones);
1117 assert(td->o.job_max_open_zones <= zbdi->max_open_zones);
1118 assert(zbdi->num_open_zones <= zbdi->max_open_zones);
1120 for (i = 0; i < zbdi->num_open_zones; i++)
1121 if (zbdi->open_zones[i] == zone_idx)
1128 * Open a ZBD zone if it was not yet open. Returns true if either the zone was
1129 * already open or if opening a new zone is allowed. Returns false if the zone
1130 * was not yet open and opening a new zone would cause the zone limit to be
1133 static bool zbd_open_zone(struct thread_data *td, const struct fio_file *f,
1136 const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
1137 struct zoned_block_device_info *zbdi = f->zbd_info;
1138 struct fio_zone_info *z = get_zone(f, zone_idx);
1141 if (z->cond == ZBD_ZONE_COND_OFFLINE)
1145 * Skip full zones with data verification enabled because resetting a
1146 * zone causes data loss and hence causes verification to fail.
1148 if (td->o.verify != VERIFY_NONE && zbd_zone_full(f, z, min_bs))
1152 * zbdi->max_open_zones == 0 means that there is no limit on the maximum
1153 * number of open zones. In this case, do no track open zones in
1154 * zbdi->open_zones array.
1156 if (!zbdi->max_open_zones)
1159 pthread_mutex_lock(&zbdi->mutex);
1160 if (is_zone_open(td, f, zone_idx)) {
1162 * If the zone is already open and going to be full by writes
1163 * in-flight, handle it as a full zone instead of an open zone.
1165 if (z->wp >= zbd_zone_capacity_end(z))
1170 /* Zero means no limit */
1171 if (td->o.job_max_open_zones > 0 &&
1172 td->num_open_zones >= td->o.job_max_open_zones)
1174 if (zbdi->num_open_zones >= zbdi->max_open_zones)
1176 dprint(FD_ZBD, "%s: opening zone %d\n", f->file_name, zone_idx);
1177 zbdi->open_zones[zbdi->num_open_zones++] = zone_idx;
1178 td->num_open_zones++;
1183 pthread_mutex_unlock(&zbdi->mutex);
1187 /* Return random zone index for one of the open zones. */
1188 static uint32_t pick_random_zone_idx(const struct fio_file *f,
1189 const struct io_u *io_u)
1191 return (io_u->offset - f->file_offset) * f->zbd_info->num_open_zones /
1196 * Modify the offset of an I/O unit that does not refer to an open zone such
1197 * that it refers to an open zone. Close an open zone and open a new zone if
1198 * necessary. The open zone is searched across sequential zones.
1199 * This algorithm can only work correctly if all write pointers are
1200 * a multiple of the fio block size. The caller must neither hold z->mutex
1201 * nor f->zbd_info->mutex. Returns with z->mutex held upon success.
1203 static struct fio_zone_info *zbd_convert_to_open_zone(struct thread_data *td,
1206 const uint32_t min_bs = td->o.min_bs[io_u->ddir];
1207 struct fio_file *f = io_u->file;
1208 struct zoned_block_device_info *zbdi = f->zbd_info;
1209 struct fio_zone_info *z;
1210 unsigned int open_zone_idx = -1;
1211 uint32_t zone_idx, new_zone_idx;
1213 bool wait_zone_close;
1215 assert(is_valid_offset(f, io_u->offset));
1217 if (zbdi->max_open_zones || td->o.job_max_open_zones) {
1219 * This statement accesses zbdi->open_zones[] on purpose
1222 zone_idx = zbdi->open_zones[pick_random_zone_idx(f, io_u)];
1224 zone_idx = zbd_zone_idx(f, io_u->offset);
1226 if (zone_idx < f->min_zone)
1227 zone_idx = f->min_zone;
1228 else if (zone_idx >= f->max_zone)
1229 zone_idx = f->max_zone - 1;
1230 dprint(FD_ZBD, "%s(%s): starting from zone %d (offset %lld, buflen %lld)\n",
1231 __func__, f->file_name, zone_idx, io_u->offset, io_u->buflen);
1234 * Since z->mutex is the outer lock and zbdi->mutex the inner
1235 * lock it can happen that the state of the zone with index zone_idx
1236 * has changed after 'z' has been assigned and before zbdi->mutex
1237 * has been obtained. Hence the loop.
1242 z = get_zone(f, zone_idx);
1244 zone_lock(td, f, z);
1245 pthread_mutex_lock(&zbdi->mutex);
1247 if (z->cond != ZBD_ZONE_COND_OFFLINE &&
1248 zbdi->max_open_zones == 0 && td->o.job_max_open_zones == 0)
1250 if (zbdi->num_open_zones == 0) {
1251 dprint(FD_ZBD, "%s(%s): no zones are open\n",
1252 __func__, f->file_name);
1253 goto open_other_zone;
1258 * List of opened zones is per-device, shared across all threads.
1259 * Start with quasi-random candidate zone.
1260 * Ignore zones which don't belong to thread's offset/size area.
1262 open_zone_idx = pick_random_zone_idx(f, io_u);
1263 assert(!open_zone_idx ||
1264 open_zone_idx < zbdi->num_open_zones);
1265 tmp_idx = open_zone_idx;
1266 for (i = 0; i < zbdi->num_open_zones; i++) {
1269 if (tmp_idx >= zbdi->num_open_zones)
1271 tmpz = zbdi->open_zones[tmp_idx];
1272 if (f->min_zone <= tmpz && tmpz < f->max_zone) {
1273 open_zone_idx = tmp_idx;
1274 goto found_candidate_zone;
1280 dprint(FD_ZBD, "%s(%s): no candidate zone\n",
1281 __func__, f->file_name);
1282 pthread_mutex_unlock(&zbdi->mutex);
1287 found_candidate_zone:
1288 new_zone_idx = zbdi->open_zones[open_zone_idx];
1289 if (new_zone_idx == zone_idx)
1291 zone_idx = new_zone_idx;
1292 pthread_mutex_unlock(&zbdi->mutex);
1297 /* Both z->mutex and zbdi->mutex are held. */
1300 if (z->wp + min_bs <= zbd_zone_capacity_end(z)) {
1301 pthread_mutex_unlock(&zbdi->mutex);
1306 /* Check if number of open zones reaches one of limits. */
1308 zbdi->num_open_zones == f->max_zone - f->min_zone ||
1309 (zbdi->max_open_zones &&
1310 zbdi->num_open_zones == zbdi->max_open_zones) ||
1311 (td->o.job_max_open_zones &&
1312 td->num_open_zones == td->o.job_max_open_zones);
1314 pthread_mutex_unlock(&zbdi->mutex);
1316 /* Only z->mutex is held. */
1319 * When number of open zones reaches to one of limits, wait for
1320 * zone close before opening a new zone.
1322 if (wait_zone_close) {
1323 dprint(FD_ZBD, "%s(%s): quiesce to allow open zones to close\n",
1324 __func__, f->file_name);
1328 /* Zone 'z' is full, so try to open a new zone. */
1329 for (i = f->io_size / zbdi->zone_size; i > 0; i--) {
1334 if (!is_valid_offset(f, z->start)) {
1336 zone_idx = f->min_zone;
1337 z = get_zone(f, zone_idx);
1339 assert(is_valid_offset(f, z->start));
1342 zone_lock(td, f, z);
1345 if (zbd_open_zone(td, f, zone_idx))
1349 /* Only z->mutex is held. */
1351 /* Check whether the write fits in any of the already opened zones. */
1352 pthread_mutex_lock(&zbdi->mutex);
1353 for (i = 0; i < zbdi->num_open_zones; i++) {
1354 zone_idx = zbdi->open_zones[i];
1355 if (zone_idx < f->min_zone || zone_idx >= f->max_zone)
1357 pthread_mutex_unlock(&zbdi->mutex);
1360 z = get_zone(f, zone_idx);
1362 zone_lock(td, f, z);
1363 if (z->wp + min_bs <= zbd_zone_capacity_end(z))
1365 pthread_mutex_lock(&zbdi->mutex);
1367 pthread_mutex_unlock(&zbdi->mutex);
1369 dprint(FD_ZBD, "%s(%s): did not open another zone\n", __func__,
1374 dprint(FD_ZBD, "%s(%s): returning zone %d\n", __func__, f->file_name,
1376 io_u->offset = z->start;
1378 assert(z->cond != ZBD_ZONE_COND_OFFLINE);
1382 /* The caller must hold z->mutex. */
1383 static struct fio_zone_info *zbd_replay_write_order(struct thread_data *td,
1385 struct fio_zone_info *z)
1387 const struct fio_file *f = io_u->file;
1388 const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
1390 if (!zbd_open_zone(td, f, zbd_zone_nr(f, z))) {
1392 z = zbd_convert_to_open_zone(td, io_u);
1396 if (z->verify_block * min_bs >= z->capacity) {
1397 log_err("%s: %d * %d >= %llu\n", f->file_name, z->verify_block,
1398 min_bs, (unsigned long long)z->capacity);
1400 * If the assertion below fails during a test run, adding
1401 * "--experimental_verify=1" to the command line may help.
1405 io_u->offset = z->start + z->verify_block * min_bs;
1406 if (io_u->offset + io_u->buflen >= zbd_zone_capacity_end(z)) {
1407 log_err("%s: %llu + %llu >= %llu\n", f->file_name, io_u->offset,
1408 io_u->buflen, (unsigned long long) zbd_zone_capacity_end(z));
1411 z->verify_block += io_u->buflen / min_bs;
1417 * Find another zone which has @min_bytes of readable data. Search in zones
1418 * @zb + 1 .. @zl. For random workload, also search in zones @zb - 1 .. @zf.
1420 * Either returns NULL or returns a zone pointer. When the zone has write
1421 * pointer, hold the mutex for the zone.
1423 static struct fio_zone_info *
1424 zbd_find_zone(struct thread_data *td, struct io_u *io_u, uint32_t min_bytes,
1425 struct fio_zone_info *zb, struct fio_zone_info *zl)
1427 struct fio_file *f = io_u->file;
1428 struct fio_zone_info *z1, *z2;
1429 const struct fio_zone_info *const zf = get_zone(f, f->min_zone);
1432 * Skip to the next non-empty zone in case of sequential I/O and to
1433 * the nearest non-empty zone in case of random I/O.
1435 for (z1 = zb + 1, z2 = zb - 1; z1 < zl || z2 >= zf; z1++, z2--) {
1436 if (z1 < zl && z1->cond != ZBD_ZONE_COND_OFFLINE) {
1438 zone_lock(td, f, z1);
1439 if (z1->start + min_bytes <= z1->wp)
1443 } else if (!td_random(td)) {
1446 if (td_random(td) && z2 >= zf &&
1447 z2->cond != ZBD_ZONE_COND_OFFLINE) {
1449 zone_lock(td, f, z2);
1450 if (z2->start + min_bytes <= z2->wp)
1456 dprint(FD_ZBD, "%s: no zone has %d bytes of readable data\n",
1457 f->file_name, min_bytes);
1462 * zbd_end_zone_io - update zone status at command completion
1464 * @z: zone info pointer
1466 * If the write command made the zone full, close it.
1468 * The caller must hold z->mutex.
1470 static void zbd_end_zone_io(struct thread_data *td, const struct io_u *io_u,
1471 struct fio_zone_info *z)
1473 const struct fio_file *f = io_u->file;
1475 if (io_u->ddir == DDIR_WRITE &&
1476 io_u->offset + io_u->buflen >= zbd_zone_capacity_end(z)) {
1477 pthread_mutex_lock(&f->zbd_info->mutex);
1478 zbd_close_zone(td, f, zbd_zone_nr(f, z));
1479 pthread_mutex_unlock(&f->zbd_info->mutex);
1484 * zbd_queue_io - update the write pointer of a sequential zone
1486 * @success: Whether or not the I/O unit has been queued successfully
1487 * @q: queueing status (busy, completed or queued).
1489 * For write and trim operations, update the write pointer of the I/O unit
1492 static void zbd_queue_io(struct thread_data *td, struct io_u *io_u, int q,
1495 const struct fio_file *f = io_u->file;
1496 struct zoned_block_device_info *zbd_info = f->zbd_info;
1497 struct fio_zone_info *z;
1503 zone_idx = zbd_zone_idx(f, io_u->offset);
1504 assert(zone_idx < zbd_info->nr_zones);
1505 z = get_zone(f, zone_idx);
1513 "%s: queued I/O (%lld, %llu) for zone %u\n",
1514 f->file_name, io_u->offset, io_u->buflen, zone_idx);
1516 switch (io_u->ddir) {
1518 zone_end = min((uint64_t)(io_u->offset + io_u->buflen),
1519 zbd_zone_capacity_end(z));
1520 pthread_mutex_lock(&zbd_info->mutex);
1522 * z->wp > zone_end means that one or more I/O errors
1525 if (z->wp <= zone_end) {
1526 zbd_info->sectors_with_data += zone_end - z->wp;
1527 zbd_info->wp_sectors_with_data += zone_end - z->wp;
1529 pthread_mutex_unlock(&zbd_info->mutex);
1533 assert(z->wp == z->start);
1539 if (q == FIO_Q_COMPLETED && !io_u->error)
1540 zbd_end_zone_io(td, io_u, z);
1543 if (!success || q != FIO_Q_QUEUED) {
1544 /* BUSY or COMPLETED: unlock the zone */
1546 io_u->zbd_put_io = NULL;
1551 * zbd_put_io - Unlock an I/O unit target zone lock
1554 static void zbd_put_io(struct thread_data *td, const struct io_u *io_u)
1556 const struct fio_file *f = io_u->file;
1557 struct zoned_block_device_info *zbd_info = f->zbd_info;
1558 struct fio_zone_info *z;
1563 zone_idx = zbd_zone_idx(f, io_u->offset);
1564 assert(zone_idx < zbd_info->nr_zones);
1565 z = get_zone(f, zone_idx);
1570 "%s: terminate I/O (%lld, %llu) for zone %u\n",
1571 f->file_name, io_u->offset, io_u->buflen, zone_idx);
1573 zbd_end_zone_io(td, io_u, z);
1576 zbd_check_swd(td, f);
1580 * Windows and MacOS do not define this.
1583 #define EREMOTEIO 121 /* POSIX value */
1586 bool zbd_unaligned_write(int error_code)
1588 switch (error_code) {
1597 * setup_zbd_zone_mode - handle zoneskip as necessary for ZBD drives
1598 * @td: FIO thread data.
1599 * @io_u: FIO I/O unit.
1601 * For sequential workloads, change the file offset to skip zoneskip bytes when
1602 * no more IO can be performed in the current zone.
1603 * - For read workloads, zoneskip is applied when the io has reached the end of
1604 * the zone or the zone write position (when td->o.read_beyond_wp is false).
1605 * - For write workloads, zoneskip is applied when the zone is full.
1606 * This applies only to read and write operations.
1608 void setup_zbd_zone_mode(struct thread_data *td, struct io_u *io_u)
1610 struct fio_file *f = io_u->file;
1611 enum fio_ddir ddir = io_u->ddir;
1612 struct fio_zone_info *z;
1615 assert(td->o.zone_mode == ZONE_MODE_ZBD);
1616 assert(td->o.zone_size);
1617 assert(f->zbd_info);
1619 zone_idx = zbd_zone_idx(f, f->last_pos[ddir]);
1620 z = get_zone(f, zone_idx);
1623 * When the zone capacity is smaller than the zone size and the I/O is
1624 * sequential write, skip to zone end if the latest position is at the
1625 * zone capacity limit.
1627 if (z->capacity < f->zbd_info->zone_size && !td_random(td) &&
1628 ddir == DDIR_WRITE &&
1629 f->last_pos[ddir] >= zbd_zone_capacity_end(z)) {
1631 "%s: Jump from zone capacity limit to zone end:"
1632 " (%llu -> %llu) for zone %u (%llu)\n",
1633 f->file_name, (unsigned long long) f->last_pos[ddir],
1634 (unsigned long long) zbd_zone_end(z), zone_idx,
1635 (unsigned long long) z->capacity);
1636 td->io_skip_bytes += zbd_zone_end(z) - f->last_pos[ddir];
1637 f->last_pos[ddir] = zbd_zone_end(z);
1641 * zone_skip is valid only for sequential workloads.
1643 if (td_random(td) || !td->o.zone_skip)
1647 * It is time to switch to a new zone if:
1648 * - zone_bytes == zone_size bytes have already been accessed
1649 * - The last position reached the end of the current zone.
1650 * - For reads with td->o.read_beyond_wp == false, the last position
1651 * reached the zone write pointer.
1653 if (td->zone_bytes >= td->o.zone_size ||
1654 f->last_pos[ddir] >= zbd_zone_end(z) ||
1655 (ddir == DDIR_READ &&
1656 (!td->o.read_beyond_wp) && f->last_pos[ddir] >= z->wp)) {
1661 f->file_offset += td->o.zone_size + td->o.zone_skip;
1664 * Wrap from the beginning, if we exceed the file size
1666 if (f->file_offset >= f->real_file_size)
1667 f->file_offset = get_start_offset(td, f);
1669 f->last_pos[ddir] = f->file_offset;
1670 td->io_skip_bytes += td->o.zone_skip;
1675 * zbd_adjust_ddir - Adjust an I/O direction for zonemode=zbd.
1677 * @td: FIO thread data.
1678 * @io_u: FIO I/O unit.
1679 * @ddir: I/O direction before adjustment.
1681 * Return adjusted I/O direction.
1683 enum fio_ddir zbd_adjust_ddir(struct thread_data *td, struct io_u *io_u,
1687 * In case read direction is chosen for the first random I/O, fio with
1688 * zonemode=zbd stops because no data can be read from zoned block
1689 * devices with all empty zones. Overwrite the first I/O direction as
1690 * write to make sure data to read exists.
1692 assert(io_u->file->zbd_info);
1693 if (ddir != DDIR_READ || !td_rw(td))
1696 if (io_u->file->zbd_info->sectors_with_data ||
1697 td->o.read_beyond_wp)
1704 * zbd_adjust_block - adjust the offset and length as necessary for ZBD drives
1705 * @td: FIO thread data.
1706 * @io_u: FIO I/O unit.
1708 * Locking strategy: returns with z->mutex locked if and only if z refers
1709 * to a sequential zone and if io_u_accept is returned. z is the zone that
1710 * corresponds to io_u->offset at the end of this function.
1712 enum io_u_action zbd_adjust_block(struct thread_data *td, struct io_u *io_u)
1714 struct fio_file *f = io_u->file;
1715 struct zoned_block_device_info *zbdi = f->zbd_info;
1716 uint32_t zone_idx_b;
1717 struct fio_zone_info *zb, *zl, *orig_zb;
1718 uint32_t orig_len = io_u->buflen;
1719 uint32_t min_bs = td->o.min_bs[io_u->ddir];
1725 assert(is_valid_offset(f, io_u->offset));
1726 assert(io_u->buflen);
1727 zone_idx_b = zbd_zone_idx(f, io_u->offset);
1728 zb = get_zone(f, zone_idx_b);
1732 /* Accept non-write I/Os for conventional zones. */
1733 if (io_u->ddir != DDIR_WRITE)
1736 * Make sure that writes to conventional zones
1737 * don't cross over to any sequential zones.
1739 if (!(zb + 1)->has_wp ||
1740 io_u->offset + io_u->buflen <= (zb + 1)->start)
1743 if (io_u->offset + min_bs > (zb + 1)->start) {
1745 "%s: off=%llu + min_bs=%u > next zone %llu\n",
1746 f->file_name, io_u->offset,
1747 min_bs, (unsigned long long) (zb + 1)->start);
1748 io_u->offset = zb->start + (zb + 1)->start - io_u->offset;
1749 new_len = min(io_u->buflen, (zb + 1)->start - io_u->offset);
1751 new_len = (zb + 1)->start - io_u->offset;
1753 io_u->buflen = new_len / min_bs * min_bs;
1758 * Accept the I/O offset for reads if reading beyond the write pointer
1761 if (zb->cond != ZBD_ZONE_COND_OFFLINE &&
1762 io_u->ddir == DDIR_READ && td->o.read_beyond_wp)
1765 zbd_check_swd(td, f);
1767 zone_lock(td, f, zb);
1769 switch (io_u->ddir) {
1771 if (td->runstate == TD_VERIFYING && td_write(td)) {
1772 zb = zbd_replay_write_order(td, io_u, zb);
1776 * Check that there is enough written data in the zone to do an
1777 * I/O of at least min_bs B. If there isn't, find a new zone for
1780 range = zb->cond != ZBD_ZONE_COND_OFFLINE ?
1781 zb->wp - zb->start : 0;
1782 if (range < min_bs ||
1783 ((!td_random(td)) && (io_u->offset + min_bs > zb->wp))) {
1785 zl = get_zone(f, f->max_zone);
1786 zb = zbd_find_zone(td, io_u, min_bs, zb, zl);
1789 "%s: zbd_find_zone(%lld, %llu) failed\n",
1790 f->file_name, io_u->offset,
1795 * zbd_find_zone() returned a zone with a range of at
1798 range = zb->wp - zb->start;
1799 assert(range >= min_bs);
1802 io_u->offset = zb->start;
1805 * Make sure the I/O is within the zone valid data range while
1806 * maximizing the I/O size and preserving randomness.
1808 if (range <= io_u->buflen)
1809 io_u->offset = zb->start;
1810 else if (td_random(td))
1811 io_u->offset = zb->start +
1812 ((io_u->offset - orig_zb->start) %
1813 (range - io_u->buflen)) / min_bs * min_bs;
1815 * When zbd_find_zone() returns a conventional zone,
1816 * we can simply accept the new i/o offset here.
1821 * Make sure the I/O does not cross over the zone wp position.
1823 new_len = min((unsigned long long)io_u->buflen,
1824 (unsigned long long)(zb->wp - io_u->offset));
1825 new_len = new_len / min_bs * min_bs;
1826 if (new_len < io_u->buflen) {
1827 io_u->buflen = new_len;
1828 dprint(FD_IO, "Changed length from %u into %llu\n",
1829 orig_len, io_u->buflen);
1831 assert(zb->start <= io_u->offset);
1832 assert(io_u->offset + io_u->buflen <= zb->wp);
1835 if (io_u->buflen > zbdi->zone_size) {
1836 td_verror(td, EINVAL, "I/O buflen exceeds zone size");
1838 "%s: I/O buflen %llu exceeds zone size %llu\n",
1839 f->file_name, io_u->buflen,
1840 (unsigned long long) zbdi->zone_size);
1843 if (!zbd_open_zone(td, f, zone_idx_b)) {
1845 zb = zbd_convert_to_open_zone(td, io_u);
1847 dprint(FD_IO, "%s: can't convert to open zone",
1851 zone_idx_b = zbd_zone_nr(f, zb);
1853 /* Check whether the zone reset threshold has been exceeded */
1854 if (td->o.zrf.u.f) {
1855 if (zbdi->wp_sectors_with_data >=
1856 f->io_size * td->o.zrt.u.f &&
1857 zbd_dec_and_reset_write_cnt(td, f)) {
1861 /* Reset the zone pointer if necessary */
1862 if (zb->reset_zone || zbd_zone_full(f, zb, min_bs)) {
1863 assert(td->o.verify == VERIFY_NONE);
1865 * Since previous write requests may have been submitted
1866 * asynchronously and since we will submit the zone
1867 * reset synchronously, wait until previously submitted
1868 * write requests have completed before issuing a
1873 if (zbd_reset_zone(td, f, zb) < 0)
1876 if (zb->capacity < min_bs) {
1877 td_verror(td, EINVAL, "ZCAP is less min_bs");
1878 log_err("zone capacity %llu smaller than minimum block size %d\n",
1879 (unsigned long long)zb->capacity,
1884 /* Make writes occur at the write pointer */
1885 assert(!zbd_zone_full(f, zb, min_bs));
1886 io_u->offset = zb->wp;
1887 if (!is_valid_offset(f, io_u->offset)) {
1888 td_verror(td, EINVAL, "invalid WP value");
1889 dprint(FD_ZBD, "%s: dropped request with offset %llu\n",
1890 f->file_name, io_u->offset);
1894 * Make sure that the buflen is a multiple of the minimal
1895 * block size. Give up if shrinking would make the request too
1898 new_len = min((unsigned long long)io_u->buflen,
1899 zbd_zone_capacity_end(zb) - io_u->offset);
1900 new_len = new_len / min_bs * min_bs;
1901 if (new_len == io_u->buflen)
1903 if (new_len >= min_bs) {
1904 io_u->buflen = new_len;
1905 dprint(FD_IO, "Changed length from %u into %llu\n",
1906 orig_len, io_u->buflen);
1909 td_verror(td, EIO, "zone remainder too small");
1910 log_err("zone remainder %lld smaller than min block size %d\n",
1911 (zbd_zone_capacity_end(zb) - io_u->offset), min_bs);
1917 case DDIR_SYNC_FILE_RANGE:
1928 assert(zb->cond != ZBD_ZONE_COND_OFFLINE);
1929 assert(!io_u->zbd_queue_io);
1930 assert(!io_u->zbd_put_io);
1931 io_u->zbd_queue_io = zbd_queue_io;
1932 io_u->zbd_put_io = zbd_put_io;
1934 * Since we return with the zone lock still held,
1935 * add an annotation to let Coverity know that it
1938 /* coverity[missing_unlock] */
1942 if (zb && zb->has_wp)
1947 /* Return a string with ZBD statistics */
1948 char *zbd_write_status(const struct thread_stat *ts)
1952 if (asprintf(&res, "; %llu zone resets", (unsigned long long) ts->nr_zone_resets) < 0)