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;
379 (td->o.min_bs[DDIR_TRIM] != td->o.max_bs[DDIR_TRIM] ||
380 td->o.bssplit_nr[DDIR_TRIM])) {
381 log_info("bsrange and bssplit are not allowed for trim with zonemode=zbd\n");
384 for_each_file(td, f, j) {
389 zone_size = f->zbd_info->zone_size;
390 if (td_trim(td) && td->o.bs[DDIR_TRIM] != zone_size) {
391 log_info("%s: trim block size %llu is not the zone size %llu\n",
392 f->file_name, td->o.bs[DDIR_TRIM],
393 (unsigned long long)zone_size);
396 for (k = 0; k < FIO_ARRAY_SIZE(td->o.bs); k++) {
397 if (td->o.verify != VERIFY_NONE &&
398 zone_size % td->o.bs[k] != 0) {
399 log_info("%s: block size %llu is not a divisor of the zone size %llu\n",
400 f->file_name, td->o.bs[k],
401 (unsigned long long)zone_size);
410 static int ilog2(uint64_t i)
422 * Initialize f->zbd_info for devices that are not zoned block devices. This
423 * allows to execute a ZBD workload against a non-ZBD device.
425 static int init_zone_info(struct thread_data *td, struct fio_file *f)
428 struct fio_zone_info *p;
429 uint64_t zone_size = td->o.zone_size;
430 uint64_t zone_capacity = td->o.zone_capacity;
431 struct zoned_block_device_info *zbd_info = NULL;
434 if (zone_size == 0) {
435 log_err("%s: Specifying the zone size is mandatory for regular file/block device with --zonemode=zbd\n\n",
440 if (zone_size < 512) {
441 log_err("%s: zone size must be at least 512 bytes for --zonemode=zbd\n\n",
446 if (zone_capacity == 0)
447 zone_capacity = zone_size;
449 if (zone_capacity > zone_size) {
450 log_err("%s: job parameter zonecapacity %llu is larger than zone size %llu\n",
451 f->file_name, (unsigned long long) td->o.zone_capacity,
452 (unsigned long long) td->o.zone_size);
456 if (f->real_file_size < zone_size) {
457 log_err("%s: file/device size %"PRIu64" is smaller than zone size %"PRIu64"\n",
458 f->file_name, f->real_file_size, zone_size);
462 nr_zones = (f->real_file_size + zone_size - 1) / zone_size;
463 zbd_info = scalloc(1, sizeof(*zbd_info) +
464 (nr_zones + 1) * sizeof(zbd_info->zone_info[0]));
468 mutex_init_pshared(&zbd_info->mutex);
469 zbd_info->refcount = 1;
470 p = &zbd_info->zone_info[0];
471 for (i = 0; i < nr_zones; i++, p++) {
472 mutex_init_pshared_with_type(&p->mutex,
473 PTHREAD_MUTEX_RECURSIVE);
474 p->start = i * zone_size;
476 p->type = ZBD_ZONE_TYPE_SWR;
477 p->cond = ZBD_ZONE_COND_EMPTY;
478 p->capacity = zone_capacity;
482 p->start = nr_zones * zone_size;
484 f->zbd_info = zbd_info;
485 f->zbd_info->zone_size = zone_size;
486 f->zbd_info->zone_size_log2 = is_power_of_2(zone_size) ?
487 ilog2(zone_size) : 0;
488 f->zbd_info->nr_zones = nr_zones;
493 * Maximum number of zones to report in one operation.
495 #define ZBD_REPORT_MAX_ZONES 8192U
498 * Parse the device zone report and store it in f->zbd_info. Must be called
499 * only for devices that are zoned, namely those with a model != ZBD_NONE.
501 static int parse_zone_info(struct thread_data *td, struct fio_file *f)
504 struct zbd_zone *zones, *z;
505 struct fio_zone_info *p;
506 uint64_t zone_size, offset;
507 struct zoned_block_device_info *zbd_info = NULL;
508 int i, j, ret = -ENOMEM;
510 zones = calloc(ZBD_REPORT_MAX_ZONES, sizeof(struct zbd_zone));
514 nrz = zbd_report_zones(td, f, 0, zones, ZBD_REPORT_MAX_ZONES);
517 log_info("fio: report zones (offset 0) failed for %s (%d).\n",
522 zone_size = zones[0].len;
523 nr_zones = (f->real_file_size + zone_size - 1) / zone_size;
525 if (td->o.zone_size == 0) {
526 td->o.zone_size = zone_size;
527 } else if (td->o.zone_size != zone_size) {
528 log_err("fio: %s job parameter zonesize %llu does not match disk zone size %llu.\n",
529 f->file_name, (unsigned long long) td->o.zone_size,
530 (unsigned long long) zone_size);
535 dprint(FD_ZBD, "Device %s has %d zones of size %llu KB\n", f->file_name,
536 nr_zones, (unsigned long long) zone_size / 1024);
538 zbd_info = scalloc(1, sizeof(*zbd_info) +
539 (nr_zones + 1) * sizeof(zbd_info->zone_info[0]));
542 mutex_init_pshared(&zbd_info->mutex);
543 zbd_info->refcount = 1;
544 p = &zbd_info->zone_info[0];
545 for (offset = 0, j = 0; j < nr_zones;) {
547 for (i = 0; i < nrz; i++, j++, z++, p++) {
548 mutex_init_pshared_with_type(&p->mutex,
549 PTHREAD_MUTEX_RECURSIVE);
551 p->capacity = z->capacity;
553 case ZBD_ZONE_COND_NOT_WP:
554 case ZBD_ZONE_COND_FULL:
555 p->wp = p->start + p->capacity;
558 assert(z->start <= z->wp);
559 assert(z->wp <= z->start + zone_size);
565 case ZBD_ZONE_TYPE_SWR:
574 if (j > 0 && p->start != p[-1].start + zone_size) {
575 log_info("%s: invalid zone data\n",
582 offset = z->start + z->len;
585 nrz = zbd_report_zones(td, f, offset, zones,
586 min((uint32_t)(nr_zones - j),
587 ZBD_REPORT_MAX_ZONES));
590 log_info("fio: report zones (offset %llu) failed for %s (%d).\n",
591 (unsigned long long)offset,
598 zbd_info->zone_info[nr_zones].start = offset;
600 f->zbd_info = zbd_info;
601 f->zbd_info->zone_size = zone_size;
602 f->zbd_info->zone_size_log2 = is_power_of_2(zone_size) ?
603 ilog2(zone_size) : 0;
604 f->zbd_info->nr_zones = nr_zones;
614 static int zbd_set_max_open_zones(struct thread_data *td, struct fio_file *f)
616 struct zoned_block_device_info *zbd = f->zbd_info;
617 unsigned int max_open_zones;
620 if (zbd->model != ZBD_HOST_MANAGED || td->o.ignore_zone_limits) {
621 /* Only host-managed devices have a max open limit */
622 zbd->max_open_zones = td->o.max_open_zones;
626 /* If host-managed, get the max open limit */
627 ret = zbd_get_max_open_zones(td, f, &max_open_zones);
631 if (!max_open_zones) {
632 /* No device limit */
633 zbd->max_open_zones = td->o.max_open_zones;
634 } else if (!td->o.max_open_zones) {
635 /* No user limit. Set limit to device limit */
636 zbd->max_open_zones = max_open_zones;
637 } else if (td->o.max_open_zones <= max_open_zones) {
638 /* Both user limit and dev limit. User limit not too large */
639 zbd->max_open_zones = td->o.max_open_zones;
641 /* Both user limit and dev limit. User limit too large */
642 td_verror(td, EINVAL,
643 "Specified --max_open_zones is too large");
644 log_err("Specified --max_open_zones (%d) is larger than max (%u)\n",
645 td->o.max_open_zones, max_open_zones);
650 /* Ensure that the limit is not larger than FIO's internal limit */
651 if (zbd->max_open_zones > ZBD_MAX_OPEN_ZONES) {
652 td_verror(td, EINVAL, "'max_open_zones' value is too large");
653 log_err("'max_open_zones' value is larger than %u\n", ZBD_MAX_OPEN_ZONES);
657 dprint(FD_ZBD, "%s: using max open zones limit: %"PRIu32"\n",
658 f->file_name, zbd->max_open_zones);
664 * Allocate zone information and store it into f->zbd_info if zonemode=zbd.
666 * Returns 0 upon success and a negative error code upon failure.
668 static int zbd_create_zone_info(struct thread_data *td, struct fio_file *f)
670 enum zbd_zoned_model zbd_model;
673 assert(td->o.zone_mode == ZONE_MODE_ZBD);
675 ret = zbd_get_zoned_model(td, f, &zbd_model);
681 case ZBD_HOST_MANAGED:
682 ret = parse_zone_info(td, f);
687 ret = init_zone_info(td, f);
692 td_verror(td, EINVAL, "Unsupported zoned model");
693 log_err("Unsupported zoned model\n");
698 f->zbd_info->model = zbd_model;
700 ret = zbd_set_max_open_zones(td, f);
702 zbd_free_zone_info(f);
709 void zbd_free_zone_info(struct fio_file *f)
715 pthread_mutex_lock(&f->zbd_info->mutex);
716 refcount = --f->zbd_info->refcount;
717 pthread_mutex_unlock(&f->zbd_info->mutex);
719 assert((int32_t)refcount >= 0);
726 * Initialize f->zbd_info.
728 * Returns 0 upon success and a negative error code upon failure.
730 * Note: this function can only work correctly if it is called before the first
733 static int zbd_init_zone_info(struct thread_data *td, struct fio_file *file)
735 struct thread_data *td2;
739 for_each_td(td2, i) {
740 for_each_file(td2, f2, j) {
741 if (td2 == td && f2 == file)
744 strcmp(f2->file_name, file->file_name) != 0)
746 file->zbd_info = f2->zbd_info;
747 file->zbd_info->refcount++;
752 ret = zbd_create_zone_info(td, file);
754 td_verror(td, -ret, "zbd_create_zone_info() failed");
758 static bool zbd_open_zone(struct thread_data *td, const struct fio_file *f,
760 static int zbd_reset_zone(struct thread_data *td, struct fio_file *f,
761 struct fio_zone_info *z);
763 int zbd_init_files(struct thread_data *td)
768 for_each_file(td, f, i) {
769 if (zbd_init_zone_info(td, f))
775 void zbd_recalc_options_with_zone_granularity(struct thread_data *td)
780 for_each_file(td, f, i) {
781 struct zoned_block_device_info *zbd = f->zbd_info;
782 // zonemode=strided doesn't get per-file zone size.
783 uint64_t zone_size = zbd ? zbd->zone_size : td->o.zone_size;
788 if (td->o.size_nz > 0) {
789 td->o.size = td->o.size_nz * zone_size;
791 if (td->o.io_size_nz > 0) {
792 td->o.io_size = td->o.io_size_nz * zone_size;
794 if (td->o.start_offset_nz > 0) {
795 td->o.start_offset = td->o.start_offset_nz * zone_size;
797 if (td->o.offset_increment_nz > 0) {
798 td->o.offset_increment = td->o.offset_increment_nz * zone_size;
800 if (td->o.zone_skip_nz > 0) {
801 td->o.zone_skip = td->o.zone_skip_nz * zone_size;
806 int zbd_setup_files(struct thread_data *td)
811 if (!zbd_using_direct_io()) {
812 log_err("Using direct I/O is mandatory for writing to ZBD drives\n\n");
816 if (!zbd_verify_sizes())
819 if (!zbd_verify_bs())
822 for_each_file(td, f, i) {
823 struct zoned_block_device_info *zbd = f->zbd_info;
824 struct fio_zone_info *z;
829 f->min_zone = zbd_zone_idx(f, f->file_offset);
830 f->max_zone = zbd_zone_idx(f, f->file_offset + f->io_size);
833 * When all zones in the I/O range are conventional, io_size
834 * can be smaller than zone size, making min_zone the same
835 * as max_zone. This is why the assert below needs to be made
838 if (zbd_is_seq_job(f))
839 assert(f->min_zone < f->max_zone);
841 if (td->o.max_open_zones > 0 &&
842 zbd->max_open_zones != td->o.max_open_zones) {
843 log_err("Different 'max_open_zones' values\n");
848 * The per job max open zones limit cannot be used without a
849 * global max open zones limit. (As the tracking of open zones
850 * is disabled when there is no global max open zones limit.)
852 if (td->o.job_max_open_zones && !zbd->max_open_zones) {
853 log_err("'job_max_open_zones' cannot be used without a global open zones limit\n");
858 * zbd->max_open_zones is the global limit shared for all jobs
859 * that target the same zoned block device. Force sync the per
860 * thread global limit with the actual global limit. (The real
861 * per thread/job limit is stored in td->o.job_max_open_zones).
863 td->o.max_open_zones = zbd->max_open_zones;
865 for (zi = f->min_zone; zi < f->max_zone; zi++) {
866 z = &zbd->zone_info[zi];
867 if (z->cond != ZBD_ZONE_COND_IMP_OPEN &&
868 z->cond != ZBD_ZONE_COND_EXP_OPEN)
870 if (zbd_open_zone(td, f, zi))
873 * If the number of open zones exceeds specified limits,
874 * reset all extra open zones.
876 if (zbd_reset_zone(td, f, z) < 0) {
877 log_err("Failed to reest zone %d\n", zi);
886 static inline unsigned int zbd_zone_nr(const struct fio_file *f,
887 struct fio_zone_info *zone)
889 return zone - f->zbd_info->zone_info;
893 * zbd_reset_zone - reset the write pointer of a single zone
894 * @td: FIO thread data.
895 * @f: FIO file associated with the disk for which to reset a write pointer.
898 * Returns 0 upon success and a negative error code upon failure.
900 * The caller must hold z->mutex.
902 static int zbd_reset_zone(struct thread_data *td, struct fio_file *f,
903 struct fio_zone_info *z)
905 uint64_t offset = z->start;
906 uint64_t length = (z+1)->start - offset;
907 uint64_t data_in_zone = z->wp - z->start;
913 assert(is_valid_offset(f, offset + length - 1));
915 dprint(FD_ZBD, "%s: resetting wp of zone %u.\n", f->file_name,
917 switch (f->zbd_info->model) {
919 case ZBD_HOST_MANAGED:
920 ret = zbd_reset_wp(td, f, offset, length);
928 pthread_mutex_lock(&f->zbd_info->mutex);
929 f->zbd_info->sectors_with_data -= data_in_zone;
930 f->zbd_info->wp_sectors_with_data -= data_in_zone;
931 pthread_mutex_unlock(&f->zbd_info->mutex);
935 td->ts.nr_zone_resets++;
940 /* The caller must hold f->zbd_info->mutex */
941 static void zbd_close_zone(struct thread_data *td, const struct fio_file *f,
942 unsigned int zone_idx)
944 uint32_t open_zone_idx = 0;
946 for (; open_zone_idx < f->zbd_info->num_open_zones; open_zone_idx++) {
947 if (f->zbd_info->open_zones[open_zone_idx] == zone_idx)
950 if (open_zone_idx == f->zbd_info->num_open_zones)
953 dprint(FD_ZBD, "%s: closing zone %d\n", f->file_name, zone_idx);
954 memmove(f->zbd_info->open_zones + open_zone_idx,
955 f->zbd_info->open_zones + open_zone_idx + 1,
956 (ZBD_MAX_OPEN_ZONES - (open_zone_idx + 1)) *
957 sizeof(f->zbd_info->open_zones[0]));
958 f->zbd_info->num_open_zones--;
959 td->num_open_zones--;
960 get_zone(f, zone_idx)->open = 0;
964 * Reset a range of zones. Returns 0 upon success and 1 upon failure.
965 * @td: fio thread data.
966 * @f: fio file for which to reset zones
967 * @zb: first zone to reset.
968 * @ze: first zone not to reset.
970 static int zbd_reset_zones(struct thread_data *td, struct fio_file *f,
971 struct fio_zone_info *const zb,
972 struct fio_zone_info *const ze)
974 struct fio_zone_info *z;
975 const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
980 dprint(FD_ZBD, "%s: examining zones %u .. %u\n", f->file_name,
981 zbd_zone_nr(f, zb), zbd_zone_nr(f, ze));
982 for (z = zb; z < ze; z++) {
983 uint32_t nz = zbd_zone_nr(f, z);
988 pthread_mutex_lock(&f->zbd_info->mutex);
989 zbd_close_zone(td, f, nz);
990 pthread_mutex_unlock(&f->zbd_info->mutex);
991 if (z->wp != z->start) {
992 dprint(FD_ZBD, "%s: resetting zone %u\n",
993 f->file_name, zbd_zone_nr(f, z));
994 if (zbd_reset_zone(td, f, z) < 0)
1004 * Reset zbd_info.write_cnt, the counter that counts down towards the next
1007 static void _zbd_reset_write_cnt(const struct thread_data *td,
1008 const struct fio_file *f)
1010 assert(0 <= td->o.zrf.u.f && td->o.zrf.u.f <= 1);
1012 f->zbd_info->write_cnt = td->o.zrf.u.f ?
1013 min(1.0 / td->o.zrf.u.f, 0.0 + UINT_MAX) : UINT_MAX;
1016 static void zbd_reset_write_cnt(const struct thread_data *td,
1017 const struct fio_file *f)
1019 pthread_mutex_lock(&f->zbd_info->mutex);
1020 _zbd_reset_write_cnt(td, f);
1021 pthread_mutex_unlock(&f->zbd_info->mutex);
1024 static bool zbd_dec_and_reset_write_cnt(const struct thread_data *td,
1025 const struct fio_file *f)
1027 uint32_t write_cnt = 0;
1029 pthread_mutex_lock(&f->zbd_info->mutex);
1030 assert(f->zbd_info->write_cnt);
1031 if (f->zbd_info->write_cnt)
1032 write_cnt = --f->zbd_info->write_cnt;
1034 _zbd_reset_write_cnt(td, f);
1035 pthread_mutex_unlock(&f->zbd_info->mutex);
1037 return write_cnt == 0;
1045 /* Calculate the number of sectors with data (swd) and perform action 'a' */
1046 static uint64_t zbd_process_swd(struct thread_data *td,
1047 const struct fio_file *f, enum swd_action a)
1049 struct fio_zone_info *zb, *ze, *z;
1051 uint64_t wp_swd = 0;
1053 zb = get_zone(f, f->min_zone);
1054 ze = get_zone(f, f->max_zone);
1055 for (z = zb; z < ze; z++) {
1057 zone_lock(td, f, z);
1058 wp_swd += z->wp - z->start;
1060 swd += z->wp - z->start;
1062 pthread_mutex_lock(&f->zbd_info->mutex);
1065 assert(f->zbd_info->sectors_with_data == swd);
1066 assert(f->zbd_info->wp_sectors_with_data == wp_swd);
1069 f->zbd_info->sectors_with_data = swd;
1070 f->zbd_info->wp_sectors_with_data = wp_swd;
1073 pthread_mutex_unlock(&f->zbd_info->mutex);
1074 for (z = zb; z < ze; z++)
1082 * The swd check is useful for debugging but takes too much time to leave
1083 * it enabled all the time. Hence it is disabled by default.
1085 static const bool enable_check_swd = false;
1087 /* Check whether the values of zbd_info.*sectors_with_data are correct. */
1088 static void zbd_check_swd(struct thread_data *td, const struct fio_file *f)
1090 if (!enable_check_swd)
1093 zbd_process_swd(td, f, CHECK_SWD);
1096 void zbd_file_reset(struct thread_data *td, struct fio_file *f)
1098 struct fio_zone_info *zb, *ze;
1101 if (!f->zbd_info || !td_write(td))
1104 zb = get_zone(f, f->min_zone);
1105 ze = get_zone(f, f->max_zone);
1106 swd = zbd_process_swd(td, f, SET_SWD);
1107 dprint(FD_ZBD, "%s(%s): swd = %" PRIu64 "\n", __func__, f->file_name,
1110 * If data verification is enabled reset the affected zones before
1111 * writing any data to avoid that a zone reset has to be issued while
1112 * writing data, which causes data loss.
1114 if (td->o.verify != VERIFY_NONE && td->runstate != TD_VERIFYING)
1115 zbd_reset_zones(td, f, zb, ze);
1116 zbd_reset_write_cnt(td, f);
1119 /* The caller must hold f->zbd_info->mutex. */
1120 static bool is_zone_open(const struct thread_data *td, const struct fio_file *f,
1121 unsigned int zone_idx)
1123 struct zoned_block_device_info *zbdi = f->zbd_info;
1126 /* This function should never be called when zbdi->max_open_zones == 0 */
1127 assert(zbdi->max_open_zones);
1128 assert(td->o.job_max_open_zones == 0 || td->num_open_zones <= td->o.job_max_open_zones);
1129 assert(td->o.job_max_open_zones <= zbdi->max_open_zones);
1130 assert(zbdi->num_open_zones <= zbdi->max_open_zones);
1132 for (i = 0; i < zbdi->num_open_zones; i++)
1133 if (zbdi->open_zones[i] == zone_idx)
1140 * Open a ZBD zone if it was not yet open. Returns true if either the zone was
1141 * already open or if opening a new zone is allowed. Returns false if the zone
1142 * was not yet open and opening a new zone would cause the zone limit to be
1145 static bool zbd_open_zone(struct thread_data *td, const struct fio_file *f,
1148 const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
1149 struct zoned_block_device_info *zbdi = f->zbd_info;
1150 struct fio_zone_info *z = get_zone(f, zone_idx);
1153 if (z->cond == ZBD_ZONE_COND_OFFLINE)
1157 * Skip full zones with data verification enabled because resetting a
1158 * zone causes data loss and hence causes verification to fail.
1160 if (td->o.verify != VERIFY_NONE && zbd_zone_full(f, z, min_bs))
1164 * zbdi->max_open_zones == 0 means that there is no limit on the maximum
1165 * number of open zones. In this case, do no track open zones in
1166 * zbdi->open_zones array.
1168 if (!zbdi->max_open_zones)
1171 pthread_mutex_lock(&zbdi->mutex);
1172 if (is_zone_open(td, f, zone_idx)) {
1174 * If the zone is already open and going to be full by writes
1175 * in-flight, handle it as a full zone instead of an open zone.
1177 if (z->wp >= zbd_zone_capacity_end(z))
1182 /* Zero means no limit */
1183 if (td->o.job_max_open_zones > 0 &&
1184 td->num_open_zones >= td->o.job_max_open_zones)
1186 if (zbdi->num_open_zones >= zbdi->max_open_zones)
1188 dprint(FD_ZBD, "%s: opening zone %d\n", f->file_name, zone_idx);
1189 zbdi->open_zones[zbdi->num_open_zones++] = zone_idx;
1190 td->num_open_zones++;
1195 pthread_mutex_unlock(&zbdi->mutex);
1199 /* Return random zone index for one of the open zones. */
1200 static uint32_t pick_random_zone_idx(const struct fio_file *f,
1201 const struct io_u *io_u)
1203 return (io_u->offset - f->file_offset) * f->zbd_info->num_open_zones /
1208 * Modify the offset of an I/O unit that does not refer to an open zone such
1209 * that it refers to an open zone. Close an open zone and open a new zone if
1210 * necessary. The open zone is searched across sequential zones.
1211 * This algorithm can only work correctly if all write pointers are
1212 * a multiple of the fio block size. The caller must neither hold z->mutex
1213 * nor f->zbd_info->mutex. Returns with z->mutex held upon success.
1215 static struct fio_zone_info *zbd_convert_to_open_zone(struct thread_data *td,
1218 const uint32_t min_bs = td->o.min_bs[io_u->ddir];
1219 struct fio_file *f = io_u->file;
1220 struct zoned_block_device_info *zbdi = f->zbd_info;
1221 struct fio_zone_info *z;
1222 unsigned int open_zone_idx = -1;
1223 uint32_t zone_idx, new_zone_idx;
1225 bool wait_zone_close;
1227 assert(is_valid_offset(f, io_u->offset));
1229 if (zbdi->max_open_zones || td->o.job_max_open_zones) {
1231 * This statement accesses zbdi->open_zones[] on purpose
1234 zone_idx = zbdi->open_zones[pick_random_zone_idx(f, io_u)];
1236 zone_idx = zbd_zone_idx(f, io_u->offset);
1238 if (zone_idx < f->min_zone)
1239 zone_idx = f->min_zone;
1240 else if (zone_idx >= f->max_zone)
1241 zone_idx = f->max_zone - 1;
1242 dprint(FD_ZBD, "%s(%s): starting from zone %d (offset %lld, buflen %lld)\n",
1243 __func__, f->file_name, zone_idx, io_u->offset, io_u->buflen);
1246 * Since z->mutex is the outer lock and zbdi->mutex the inner
1247 * lock it can happen that the state of the zone with index zone_idx
1248 * has changed after 'z' has been assigned and before zbdi->mutex
1249 * has been obtained. Hence the loop.
1254 z = get_zone(f, zone_idx);
1256 zone_lock(td, f, z);
1257 pthread_mutex_lock(&zbdi->mutex);
1259 if (z->cond != ZBD_ZONE_COND_OFFLINE &&
1260 zbdi->max_open_zones == 0 && td->o.job_max_open_zones == 0)
1262 if (zbdi->num_open_zones == 0) {
1263 dprint(FD_ZBD, "%s(%s): no zones are open\n",
1264 __func__, f->file_name);
1265 goto open_other_zone;
1270 * List of opened zones is per-device, shared across all threads.
1271 * Start with quasi-random candidate zone.
1272 * Ignore zones which don't belong to thread's offset/size area.
1274 open_zone_idx = pick_random_zone_idx(f, io_u);
1275 assert(!open_zone_idx ||
1276 open_zone_idx < zbdi->num_open_zones);
1277 tmp_idx = open_zone_idx;
1278 for (i = 0; i < zbdi->num_open_zones; i++) {
1281 if (tmp_idx >= zbdi->num_open_zones)
1283 tmpz = zbdi->open_zones[tmp_idx];
1284 if (f->min_zone <= tmpz && tmpz < f->max_zone) {
1285 open_zone_idx = tmp_idx;
1286 goto found_candidate_zone;
1292 dprint(FD_ZBD, "%s(%s): no candidate zone\n",
1293 __func__, f->file_name);
1294 pthread_mutex_unlock(&zbdi->mutex);
1299 found_candidate_zone:
1300 new_zone_idx = zbdi->open_zones[open_zone_idx];
1301 if (new_zone_idx == zone_idx)
1303 zone_idx = new_zone_idx;
1304 pthread_mutex_unlock(&zbdi->mutex);
1309 /* Both z->mutex and zbdi->mutex are held. */
1312 if (z->wp + min_bs <= zbd_zone_capacity_end(z)) {
1313 pthread_mutex_unlock(&zbdi->mutex);
1318 /* Check if number of open zones reaches one of limits. */
1320 zbdi->num_open_zones == f->max_zone - f->min_zone ||
1321 (zbdi->max_open_zones &&
1322 zbdi->num_open_zones == zbdi->max_open_zones) ||
1323 (td->o.job_max_open_zones &&
1324 td->num_open_zones == td->o.job_max_open_zones);
1326 pthread_mutex_unlock(&zbdi->mutex);
1328 /* Only z->mutex is held. */
1331 * When number of open zones reaches to one of limits, wait for
1332 * zone close before opening a new zone.
1334 if (wait_zone_close) {
1335 dprint(FD_ZBD, "%s(%s): quiesce to allow open zones to close\n",
1336 __func__, f->file_name);
1340 /* Zone 'z' is full, so try to open a new zone. */
1341 for (i = f->io_size / zbdi->zone_size; i > 0; i--) {
1346 if (!is_valid_offset(f, z->start)) {
1348 zone_idx = f->min_zone;
1349 z = get_zone(f, zone_idx);
1351 assert(is_valid_offset(f, z->start));
1354 zone_lock(td, f, z);
1357 if (zbd_open_zone(td, f, zone_idx))
1361 /* Only z->mutex is held. */
1363 /* Check whether the write fits in any of the already opened zones. */
1364 pthread_mutex_lock(&zbdi->mutex);
1365 for (i = 0; i < zbdi->num_open_zones; i++) {
1366 zone_idx = zbdi->open_zones[i];
1367 if (zone_idx < f->min_zone || zone_idx >= f->max_zone)
1369 pthread_mutex_unlock(&zbdi->mutex);
1372 z = get_zone(f, zone_idx);
1374 zone_lock(td, f, z);
1375 if (z->wp + min_bs <= zbd_zone_capacity_end(z))
1377 pthread_mutex_lock(&zbdi->mutex);
1379 pthread_mutex_unlock(&zbdi->mutex);
1381 dprint(FD_ZBD, "%s(%s): did not open another zone\n", __func__,
1386 dprint(FD_ZBD, "%s(%s): returning zone %d\n", __func__, f->file_name,
1388 io_u->offset = z->start;
1390 assert(z->cond != ZBD_ZONE_COND_OFFLINE);
1394 /* The caller must hold z->mutex. */
1395 static struct fio_zone_info *zbd_replay_write_order(struct thread_data *td,
1397 struct fio_zone_info *z)
1399 const struct fio_file *f = io_u->file;
1400 const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
1402 if (!zbd_open_zone(td, f, zbd_zone_nr(f, z))) {
1404 z = zbd_convert_to_open_zone(td, io_u);
1408 if (z->verify_block * min_bs >= z->capacity) {
1409 log_err("%s: %d * %d >= %llu\n", f->file_name, z->verify_block,
1410 min_bs, (unsigned long long)z->capacity);
1412 * If the assertion below fails during a test run, adding
1413 * "--experimental_verify=1" to the command line may help.
1417 io_u->offset = z->start + z->verify_block * min_bs;
1418 if (io_u->offset + io_u->buflen >= zbd_zone_capacity_end(z)) {
1419 log_err("%s: %llu + %llu >= %llu\n", f->file_name, io_u->offset,
1420 io_u->buflen, (unsigned long long) zbd_zone_capacity_end(z));
1423 z->verify_block += io_u->buflen / min_bs;
1429 * Find another zone which has @min_bytes of readable data. Search in zones
1430 * @zb + 1 .. @zl. For random workload, also search in zones @zb - 1 .. @zf.
1432 * Either returns NULL or returns a zone pointer. When the zone has write
1433 * pointer, hold the mutex for the zone.
1435 static struct fio_zone_info *
1436 zbd_find_zone(struct thread_data *td, struct io_u *io_u, uint32_t min_bytes,
1437 struct fio_zone_info *zb, struct fio_zone_info *zl)
1439 struct fio_file *f = io_u->file;
1440 struct fio_zone_info *z1, *z2;
1441 const struct fio_zone_info *const zf = get_zone(f, f->min_zone);
1444 * Skip to the next non-empty zone in case of sequential I/O and to
1445 * the nearest non-empty zone in case of random I/O.
1447 for (z1 = zb + 1, z2 = zb - 1; z1 < zl || z2 >= zf; z1++, z2--) {
1448 if (z1 < zl && z1->cond != ZBD_ZONE_COND_OFFLINE) {
1450 zone_lock(td, f, z1);
1451 if (z1->start + min_bytes <= z1->wp)
1455 } else if (!td_random(td)) {
1458 if (td_random(td) && z2 >= zf &&
1459 z2->cond != ZBD_ZONE_COND_OFFLINE) {
1461 zone_lock(td, f, z2);
1462 if (z2->start + min_bytes <= z2->wp)
1468 dprint(FD_ZBD, "%s: no zone has %d bytes of readable data\n",
1469 f->file_name, min_bytes);
1474 * zbd_end_zone_io - update zone status at command completion
1476 * @z: zone info pointer
1478 * If the write command made the zone full, close it.
1480 * The caller must hold z->mutex.
1482 static void zbd_end_zone_io(struct thread_data *td, const struct io_u *io_u,
1483 struct fio_zone_info *z)
1485 const struct fio_file *f = io_u->file;
1487 if (io_u->ddir == DDIR_WRITE &&
1488 io_u->offset + io_u->buflen >= zbd_zone_capacity_end(z)) {
1489 pthread_mutex_lock(&f->zbd_info->mutex);
1490 zbd_close_zone(td, f, zbd_zone_nr(f, z));
1491 pthread_mutex_unlock(&f->zbd_info->mutex);
1496 * zbd_queue_io - update the write pointer of a sequential zone
1498 * @success: Whether or not the I/O unit has been queued successfully
1499 * @q: queueing status (busy, completed or queued).
1501 * For write and trim operations, update the write pointer of the I/O unit
1504 static void zbd_queue_io(struct thread_data *td, struct io_u *io_u, int q,
1507 const struct fio_file *f = io_u->file;
1508 struct zoned_block_device_info *zbd_info = f->zbd_info;
1509 struct fio_zone_info *z;
1515 zone_idx = zbd_zone_idx(f, io_u->offset);
1516 assert(zone_idx < zbd_info->nr_zones);
1517 z = get_zone(f, zone_idx);
1525 "%s: queued I/O (%lld, %llu) for zone %u\n",
1526 f->file_name, io_u->offset, io_u->buflen, zone_idx);
1528 switch (io_u->ddir) {
1530 zone_end = min((uint64_t)(io_u->offset + io_u->buflen),
1531 zbd_zone_capacity_end(z));
1532 pthread_mutex_lock(&zbd_info->mutex);
1534 * z->wp > zone_end means that one or more I/O errors
1537 if (z->wp <= zone_end) {
1538 zbd_info->sectors_with_data += zone_end - z->wp;
1539 zbd_info->wp_sectors_with_data += zone_end - z->wp;
1541 pthread_mutex_unlock(&zbd_info->mutex);
1548 if (q == FIO_Q_COMPLETED && !io_u->error)
1549 zbd_end_zone_io(td, io_u, z);
1552 if (!success || q != FIO_Q_QUEUED) {
1553 /* BUSY or COMPLETED: unlock the zone */
1555 io_u->zbd_put_io = NULL;
1560 * zbd_put_io - Unlock an I/O unit target zone lock
1563 static void zbd_put_io(struct thread_data *td, const struct io_u *io_u)
1565 const struct fio_file *f = io_u->file;
1566 struct zoned_block_device_info *zbd_info = f->zbd_info;
1567 struct fio_zone_info *z;
1572 zone_idx = zbd_zone_idx(f, io_u->offset);
1573 assert(zone_idx < zbd_info->nr_zones);
1574 z = get_zone(f, zone_idx);
1579 "%s: terminate I/O (%lld, %llu) for zone %u\n",
1580 f->file_name, io_u->offset, io_u->buflen, zone_idx);
1582 zbd_end_zone_io(td, io_u, z);
1585 zbd_check_swd(td, f);
1589 * Windows and MacOS do not define this.
1592 #define EREMOTEIO 121 /* POSIX value */
1595 bool zbd_unaligned_write(int error_code)
1597 switch (error_code) {
1606 * setup_zbd_zone_mode - handle zoneskip as necessary for ZBD drives
1607 * @td: FIO thread data.
1608 * @io_u: FIO I/O unit.
1610 * For sequential workloads, change the file offset to skip zoneskip bytes when
1611 * no more IO can be performed in the current zone.
1612 * - For read workloads, zoneskip is applied when the io has reached the end of
1613 * the zone or the zone write position (when td->o.read_beyond_wp is false).
1614 * - For write workloads, zoneskip is applied when the zone is full.
1615 * This applies only to read and write operations.
1617 void setup_zbd_zone_mode(struct thread_data *td, struct io_u *io_u)
1619 struct fio_file *f = io_u->file;
1620 enum fio_ddir ddir = io_u->ddir;
1621 struct fio_zone_info *z;
1624 assert(td->o.zone_mode == ZONE_MODE_ZBD);
1625 assert(td->o.zone_size);
1626 assert(f->zbd_info);
1628 zone_idx = zbd_zone_idx(f, f->last_pos[ddir]);
1629 z = get_zone(f, zone_idx);
1632 * When the zone capacity is smaller than the zone size and the I/O is
1633 * sequential write, skip to zone end if the latest position is at the
1634 * zone capacity limit.
1636 if (z->capacity < f->zbd_info->zone_size && !td_random(td) &&
1637 ddir == DDIR_WRITE &&
1638 f->last_pos[ddir] >= zbd_zone_capacity_end(z)) {
1640 "%s: Jump from zone capacity limit to zone end:"
1641 " (%llu -> %llu) for zone %u (%llu)\n",
1642 f->file_name, (unsigned long long) f->last_pos[ddir],
1643 (unsigned long long) zbd_zone_end(z), zone_idx,
1644 (unsigned long long) z->capacity);
1645 td->io_skip_bytes += zbd_zone_end(z) - f->last_pos[ddir];
1646 f->last_pos[ddir] = zbd_zone_end(z);
1650 * zone_skip is valid only for sequential workloads.
1652 if (td_random(td) || !td->o.zone_skip)
1656 * It is time to switch to a new zone if:
1657 * - zone_bytes == zone_size bytes have already been accessed
1658 * - The last position reached the end of the current zone.
1659 * - For reads with td->o.read_beyond_wp == false, the last position
1660 * reached the zone write pointer.
1662 if (td->zone_bytes >= td->o.zone_size ||
1663 f->last_pos[ddir] >= zbd_zone_end(z) ||
1664 (ddir == DDIR_READ &&
1665 (!td->o.read_beyond_wp) && f->last_pos[ddir] >= z->wp)) {
1670 f->file_offset += td->o.zone_size + td->o.zone_skip;
1673 * Wrap from the beginning, if we exceed the file size
1675 if (f->file_offset >= f->real_file_size)
1676 f->file_offset = get_start_offset(td, f);
1678 f->last_pos[ddir] = f->file_offset;
1679 td->io_skip_bytes += td->o.zone_skip;
1684 * zbd_adjust_ddir - Adjust an I/O direction for zonemode=zbd.
1686 * @td: FIO thread data.
1687 * @io_u: FIO I/O unit.
1688 * @ddir: I/O direction before adjustment.
1690 * Return adjusted I/O direction.
1692 enum fio_ddir zbd_adjust_ddir(struct thread_data *td, struct io_u *io_u,
1696 * In case read direction is chosen for the first random I/O, fio with
1697 * zonemode=zbd stops because no data can be read from zoned block
1698 * devices with all empty zones. Overwrite the first I/O direction as
1699 * write to make sure data to read exists.
1701 assert(io_u->file->zbd_info);
1702 if (ddir != DDIR_READ || !td_rw(td))
1705 if (io_u->file->zbd_info->sectors_with_data ||
1706 td->o.read_beyond_wp)
1713 * zbd_adjust_block - adjust the offset and length as necessary for ZBD drives
1714 * @td: FIO thread data.
1715 * @io_u: FIO I/O unit.
1717 * Locking strategy: returns with z->mutex locked if and only if z refers
1718 * to a sequential zone and if io_u_accept is returned. z is the zone that
1719 * corresponds to io_u->offset at the end of this function.
1721 enum io_u_action zbd_adjust_block(struct thread_data *td, struct io_u *io_u)
1723 struct fio_file *f = io_u->file;
1724 struct zoned_block_device_info *zbdi = f->zbd_info;
1725 uint32_t zone_idx_b;
1726 struct fio_zone_info *zb, *zl, *orig_zb;
1727 uint32_t orig_len = io_u->buflen;
1728 uint32_t min_bs = td->o.min_bs[io_u->ddir];
1734 assert(is_valid_offset(f, io_u->offset));
1735 assert(io_u->buflen);
1736 zone_idx_b = zbd_zone_idx(f, io_u->offset);
1737 zb = get_zone(f, zone_idx_b);
1741 /* Accept non-write I/Os for conventional zones. */
1742 if (io_u->ddir != DDIR_WRITE)
1745 * Make sure that writes to conventional zones
1746 * don't cross over to any sequential zones.
1748 if (!(zb + 1)->has_wp ||
1749 io_u->offset + io_u->buflen <= (zb + 1)->start)
1752 if (io_u->offset + min_bs > (zb + 1)->start) {
1754 "%s: off=%llu + min_bs=%u > next zone %llu\n",
1755 f->file_name, io_u->offset,
1756 min_bs, (unsigned long long) (zb + 1)->start);
1757 io_u->offset = zb->start + (zb + 1)->start - io_u->offset;
1758 new_len = min(io_u->buflen, (zb + 1)->start - io_u->offset);
1760 new_len = (zb + 1)->start - io_u->offset;
1762 io_u->buflen = new_len / min_bs * min_bs;
1767 * Accept the I/O offset for reads if reading beyond the write pointer
1770 if (zb->cond != ZBD_ZONE_COND_OFFLINE &&
1771 io_u->ddir == DDIR_READ && td->o.read_beyond_wp)
1774 zbd_check_swd(td, f);
1776 zone_lock(td, f, zb);
1778 switch (io_u->ddir) {
1780 if (td->runstate == TD_VERIFYING && td_write(td)) {
1781 zb = zbd_replay_write_order(td, io_u, zb);
1785 * Check that there is enough written data in the zone to do an
1786 * I/O of at least min_bs B. If there isn't, find a new zone for
1789 range = zb->cond != ZBD_ZONE_COND_OFFLINE ?
1790 zb->wp - zb->start : 0;
1791 if (range < min_bs ||
1792 ((!td_random(td)) && (io_u->offset + min_bs > zb->wp))) {
1794 zl = get_zone(f, f->max_zone);
1795 zb = zbd_find_zone(td, io_u, min_bs, zb, zl);
1798 "%s: zbd_find_zone(%lld, %llu) failed\n",
1799 f->file_name, io_u->offset,
1804 * zbd_find_zone() returned a zone with a range of at
1807 range = zb->wp - zb->start;
1808 assert(range >= min_bs);
1811 io_u->offset = zb->start;
1814 * Make sure the I/O is within the zone valid data range while
1815 * maximizing the I/O size and preserving randomness.
1817 if (range <= io_u->buflen)
1818 io_u->offset = zb->start;
1819 else if (td_random(td))
1820 io_u->offset = zb->start +
1821 ((io_u->offset - orig_zb->start) %
1822 (range - io_u->buflen)) / min_bs * min_bs;
1824 * When zbd_find_zone() returns a conventional zone,
1825 * we can simply accept the new i/o offset here.
1830 * Make sure the I/O does not cross over the zone wp position.
1832 new_len = min((unsigned long long)io_u->buflen,
1833 (unsigned long long)(zb->wp - io_u->offset));
1834 new_len = new_len / min_bs * min_bs;
1835 if (new_len < io_u->buflen) {
1836 io_u->buflen = new_len;
1837 dprint(FD_IO, "Changed length from %u into %llu\n",
1838 orig_len, io_u->buflen);
1840 assert(zb->start <= io_u->offset);
1841 assert(io_u->offset + io_u->buflen <= zb->wp);
1844 if (io_u->buflen > zbdi->zone_size) {
1845 td_verror(td, EINVAL, "I/O buflen exceeds zone size");
1847 "%s: I/O buflen %llu exceeds zone size %llu\n",
1848 f->file_name, io_u->buflen,
1849 (unsigned long long) zbdi->zone_size);
1852 if (!zbd_open_zone(td, f, zone_idx_b)) {
1854 zb = zbd_convert_to_open_zone(td, io_u);
1856 dprint(FD_IO, "%s: can't convert to open zone",
1860 zone_idx_b = zbd_zone_nr(f, zb);
1862 /* Check whether the zone reset threshold has been exceeded */
1863 if (td->o.zrf.u.f) {
1864 if (zbdi->wp_sectors_with_data >=
1865 f->io_size * td->o.zrt.u.f &&
1866 zbd_dec_and_reset_write_cnt(td, f)) {
1870 /* Reset the zone pointer if necessary */
1871 if (zb->reset_zone || zbd_zone_full(f, zb, min_bs)) {
1872 assert(td->o.verify == VERIFY_NONE);
1874 * Since previous write requests may have been submitted
1875 * asynchronously and since we will submit the zone
1876 * reset synchronously, wait until previously submitted
1877 * write requests have completed before issuing a
1882 if (zbd_reset_zone(td, f, zb) < 0)
1885 if (zb->capacity < min_bs) {
1886 td_verror(td, EINVAL, "ZCAP is less min_bs");
1887 log_err("zone capacity %llu smaller than minimum block size %d\n",
1888 (unsigned long long)zb->capacity,
1893 /* Make writes occur at the write pointer */
1894 assert(!zbd_zone_full(f, zb, min_bs));
1895 io_u->offset = zb->wp;
1896 if (!is_valid_offset(f, io_u->offset)) {
1897 td_verror(td, EINVAL, "invalid WP value");
1898 dprint(FD_ZBD, "%s: dropped request with offset %llu\n",
1899 f->file_name, io_u->offset);
1903 * Make sure that the buflen is a multiple of the minimal
1904 * block size. Give up if shrinking would make the request too
1907 new_len = min((unsigned long long)io_u->buflen,
1908 zbd_zone_capacity_end(zb) - io_u->offset);
1909 new_len = new_len / min_bs * min_bs;
1910 if (new_len == io_u->buflen)
1912 if (new_len >= min_bs) {
1913 io_u->buflen = new_len;
1914 dprint(FD_IO, "Changed length from %u into %llu\n",
1915 orig_len, io_u->buflen);
1918 td_verror(td, EIO, "zone remainder too small");
1919 log_err("zone remainder %lld smaller than min block size %d\n",
1920 (zbd_zone_capacity_end(zb) - io_u->offset), min_bs);
1923 /* Check random trim targets a non-empty zone */
1924 if (!td_random(td) || zb->wp > zb->start)
1927 /* Find out a non-empty zone to trim */
1929 zl = get_zone(f, f->max_zone);
1930 zb = zbd_find_zone(td, io_u, 1, zb, zl);
1932 io_u->offset = zb->start;
1933 dprint(FD_ZBD, "%s: found new zone(%lld) for trim\n",
1934 f->file_name, io_u->offset);
1941 case DDIR_SYNC_FILE_RANGE:
1952 assert(zb->cond != ZBD_ZONE_COND_OFFLINE);
1953 assert(!io_u->zbd_queue_io);
1954 assert(!io_u->zbd_put_io);
1955 io_u->zbd_queue_io = zbd_queue_io;
1956 io_u->zbd_put_io = zbd_put_io;
1958 * Since we return with the zone lock still held,
1959 * add an annotation to let Coverity know that it
1962 /* coverity[missing_unlock] */
1966 if (zb && zb->has_wp)
1971 /* Return a string with ZBD statistics */
1972 char *zbd_write_status(const struct thread_stat *ts)
1976 if (asprintf(&res, "; %llu zone resets", (unsigned long long) ts->nr_zone_resets) < 0)
1982 * zbd_do_io_u_trim - If reset zone is applicable, do reset zone instead of trim
1984 * @td: FIO thread data.
1985 * @io_u: FIO I/O unit.
1987 * It is assumed that z->mutex is already locked.
1988 * Return io_u_completed when reset zone succeeds. Return 0 when the target zone
1989 * does not have write pointer. On error, return negative errno.
1991 int zbd_do_io_u_trim(const struct thread_data *td, struct io_u *io_u)
1993 struct fio_file *f = io_u->file;
1994 struct fio_zone_info *z;
1998 zone_idx = zbd_zone_idx(f, io_u->offset);
1999 z = get_zone(f, zone_idx);
2004 if (io_u->offset != z->start) {
2005 log_err("Trim offset not at zone start (%lld)\n", io_u->offset);
2009 ret = zbd_reset_zone((struct thread_data *)td, f, z);
2013 return io_u_completed;