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 (td->io_ops && td->io_ops->get_zoned_model)
36 ret = td->io_ops->get_zoned_model(td, f, model);
38 ret = blkzoned_get_zoned_model(td, f, model);
40 td_verror(td, errno, "get zoned model failed");
41 log_err("%s: get zoned model failed (%d).\n",
49 * zbd_report_zones - Get zone information
50 * @td: FIO thread data.
51 * @f: FIO file for which to get zone information
52 * @offset: offset from which to report zones
53 * @zones: Array of struct zbd_zone
54 * @nr_zones: Size of @zones array
56 * Get zone information into @zones starting from the zone at offset @offset
57 * for the device specified by @f.
59 * Returns the number of zones reported upon success and a negative error code
60 * upon failure. If the zone report is empty, always assume an error (device
61 * problem) and return -EIO.
63 int zbd_report_zones(struct thread_data *td, struct fio_file *f,
64 uint64_t offset, struct zbd_zone *zones,
65 unsigned int nr_zones)
69 if (td->io_ops && td->io_ops->report_zones)
70 ret = td->io_ops->report_zones(td, f, offset, zones, nr_zones);
72 ret = blkzoned_report_zones(td, f, offset, zones, nr_zones);
74 td_verror(td, errno, "report zones failed");
75 log_err("%s: report zones from sector %llu failed (%d).\n",
76 f->file_name, (unsigned long long)offset >> 9, errno);
77 } else if (ret == 0) {
78 td_verror(td, errno, "Empty zone report");
79 log_err("%s: report zones from sector %llu is empty.\n",
80 f->file_name, (unsigned long long)offset >> 9);
88 * zbd_reset_wp - reset the write pointer of a range of zones
89 * @td: FIO thread data.
90 * @f: FIO file for which to reset zones
91 * @offset: Starting offset of the first zone to reset
92 * @length: Length of the range of zones to reset
94 * Reset the write pointer of all zones in the range @offset...@offset+@length.
95 * Returns 0 upon success and a negative error code upon failure.
97 int zbd_reset_wp(struct thread_data *td, struct fio_file *f,
98 uint64_t offset, uint64_t length)
102 if (td->io_ops && td->io_ops->reset_wp)
103 ret = td->io_ops->reset_wp(td, f, offset, length);
105 ret = blkzoned_reset_wp(td, f, offset, length);
107 td_verror(td, errno, "resetting wp failed");
108 log_err("%s: resetting wp for %llu sectors at sector %llu failed (%d).\n",
109 f->file_name, (unsigned long long)length >> 9,
110 (unsigned long long)offset >> 9, errno);
117 * zbd_get_max_open_zones - Get the maximum number of open zones
118 * @td: FIO thread data
119 * @f: FIO file for which to get max open zones
120 * @max_open_zones: Upon success, result will be stored here.
122 * A @max_open_zones value set to zero means no limit.
124 * Returns 0 upon success and a negative error code upon failure.
126 int zbd_get_max_open_zones(struct thread_data *td, struct fio_file *f,
127 unsigned int *max_open_zones)
131 if (td->io_ops && td->io_ops->get_max_open_zones)
132 ret = td->io_ops->get_max_open_zones(td, f, max_open_zones);
134 ret = blkzoned_get_max_open_zones(td, f, max_open_zones);
136 td_verror(td, errno, "get max open zones failed");
137 log_err("%s: get max open zones failed (%d).\n",
138 f->file_name, errno);
145 * zbd_zone_idx - convert an offset into a zone number
147 * @offset: offset in bytes. If this offset is in the first zone_size bytes
148 * past the disk size then the index of the sentinel is returned.
150 static uint32_t zbd_zone_idx(const struct fio_file *f, uint64_t offset)
154 if (f->zbd_info->zone_size_log2 > 0)
155 zone_idx = offset >> f->zbd_info->zone_size_log2;
157 zone_idx = offset / f->zbd_info->zone_size;
159 return min(zone_idx, f->zbd_info->nr_zones);
163 * zbd_zone_end - Return zone end location
164 * @z: zone info pointer.
166 static inline uint64_t zbd_zone_end(const struct fio_zone_info *z)
172 * zbd_zone_capacity_end - Return zone capacity limit end location
173 * @z: zone info pointer.
175 static inline uint64_t zbd_zone_capacity_end(const struct fio_zone_info *z)
177 return z->start + z->capacity;
181 * zbd_zone_full - verify whether a minimum number of bytes remain in a zone
183 * @z: zone info pointer.
184 * @required: minimum number of bytes that must remain in a zone.
186 * The caller must hold z->mutex.
188 static bool zbd_zone_full(const struct fio_file *f, struct fio_zone_info *z,
191 assert((required & 511) == 0);
194 z->wp + required > zbd_zone_capacity_end(z);
197 static void zone_lock(struct thread_data *td, const struct fio_file *f,
198 struct fio_zone_info *z)
200 struct zoned_block_device_info *zbd = f->zbd_info;
201 uint32_t nz = z - zbd->zone_info;
203 /* A thread should never lock zones outside its working area. */
204 assert(f->min_zone <= nz && nz < f->max_zone);
209 * Lock the io_u target zone. The zone will be unlocked if io_u offset
210 * is changed or when io_u completes and zbd_put_io() executed.
211 * To avoid multiple jobs doing asynchronous I/Os from deadlocking each
212 * other waiting for zone locks when building an io_u batch, first
213 * only trylock the zone. If the zone is already locked by another job,
214 * process the currently queued I/Os so that I/O progress is made and
217 if (pthread_mutex_trylock(&z->mutex) != 0) {
218 if (!td_ioengine_flagged(td, FIO_SYNCIO))
220 pthread_mutex_lock(&z->mutex);
224 static inline void zone_unlock(struct fio_zone_info *z)
229 ret = pthread_mutex_unlock(&z->mutex);
233 static bool is_valid_offset(const struct fio_file *f, uint64_t offset)
235 return (uint64_t)(offset - f->file_offset) < f->io_size;
238 static inline struct fio_zone_info *get_zone(const struct fio_file *f,
239 unsigned int zone_nr)
241 return &f->zbd_info->zone_info[zone_nr];
244 /* Verify whether direct I/O is used for all host-managed zoned drives. */
245 static bool zbd_using_direct_io(void)
247 struct thread_data *td;
252 if (td->o.odirect || !(td->o.td_ddir & TD_DDIR_WRITE))
254 for_each_file(td, f, j) {
256 f->zbd_info->model == ZBD_HOST_MANAGED)
264 /* Whether or not the I/O range for f includes one or more sequential zones */
265 static bool zbd_is_seq_job(struct fio_file *f)
267 uint32_t zone_idx, zone_idx_b, zone_idx_e;
272 zone_idx_b = zbd_zone_idx(f, f->file_offset);
273 zone_idx_e = zbd_zone_idx(f, f->file_offset + f->io_size - 1);
274 for (zone_idx = zone_idx_b; zone_idx <= zone_idx_e; zone_idx++)
275 if (get_zone(f, zone_idx)->has_wp)
282 * Verify whether offset and size parameters are aligned with zone boundaries.
284 static bool zbd_verify_sizes(void)
286 const struct fio_zone_info *z;
287 struct thread_data *td;
289 uint64_t new_offset, new_end;
294 for_each_file(td, f, j) {
297 if (f->file_offset >= f->real_file_size)
299 if (!zbd_is_seq_job(f))
302 if (!td->o.zone_size) {
303 td->o.zone_size = f->zbd_info->zone_size;
304 if (!td->o.zone_size) {
305 log_err("%s: invalid 0 zone size\n",
309 } else if (td->o.zone_size != f->zbd_info->zone_size) {
310 log_err("%s: job parameter zonesize %llu does not match disk zone size %llu.\n",
311 f->file_name, (unsigned long long) td->o.zone_size,
312 (unsigned long long) f->zbd_info->zone_size);
316 if (td->o.zone_skip % td->o.zone_size) {
317 log_err("%s: zoneskip %llu is not a multiple of the device zone size %llu.\n",
318 f->file_name, (unsigned long long) td->o.zone_skip,
319 (unsigned long long) td->o.zone_size);
323 zone_idx = zbd_zone_idx(f, f->file_offset);
324 z = get_zone(f, zone_idx);
325 if ((f->file_offset != z->start) &&
326 (td->o.td_ddir != TD_DDIR_READ)) {
327 new_offset = zbd_zone_end(z);
328 if (new_offset >= f->file_offset + f->io_size) {
329 log_info("%s: io_size must be at least one zone\n",
333 log_info("%s: rounded up offset from %llu to %llu\n",
334 f->file_name, (unsigned long long) f->file_offset,
335 (unsigned long long) new_offset);
336 f->io_size -= (new_offset - f->file_offset);
337 f->file_offset = new_offset;
339 zone_idx = zbd_zone_idx(f, f->file_offset + f->io_size);
340 z = get_zone(f, zone_idx);
342 if ((td->o.td_ddir != TD_DDIR_READ) &&
343 (f->file_offset + f->io_size != new_end)) {
344 if (new_end <= f->file_offset) {
345 log_info("%s: io_size must be at least one zone\n",
349 log_info("%s: rounded down io_size from %llu to %llu\n",
350 f->file_name, (unsigned long long) f->io_size,
351 (unsigned long long) new_end - f->file_offset);
352 f->io_size = new_end - f->file_offset;
360 static bool zbd_verify_bs(void)
362 struct thread_data *td;
367 for_each_file(td, f, j) {
372 zone_size = f->zbd_info->zone_size;
373 for (k = 0; k < FIO_ARRAY_SIZE(td->o.bs); k++) {
374 if (td->o.verify != VERIFY_NONE &&
375 zone_size % td->o.bs[k] != 0) {
376 log_info("%s: block size %llu is not a divisor of the zone size %llu\n",
377 f->file_name, td->o.bs[k],
378 (unsigned long long)zone_size);
387 static int ilog2(uint64_t i)
399 * Initialize f->zbd_info for devices that are not zoned block devices. This
400 * allows to execute a ZBD workload against a non-ZBD device.
402 static int init_zone_info(struct thread_data *td, struct fio_file *f)
405 struct fio_zone_info *p;
406 uint64_t zone_size = td->o.zone_size;
407 uint64_t zone_capacity = td->o.zone_capacity;
408 struct zoned_block_device_info *zbd_info = NULL;
411 if (zone_size == 0) {
412 log_err("%s: Specifying the zone size is mandatory for regular block devices with --zonemode=zbd\n\n",
417 if (zone_size < 512) {
418 log_err("%s: zone size must be at least 512 bytes for --zonemode=zbd\n\n",
423 if (zone_capacity == 0)
424 zone_capacity = zone_size;
426 if (zone_capacity > zone_size) {
427 log_err("%s: job parameter zonecapacity %llu is larger than zone size %llu\n",
428 f->file_name, (unsigned long long) td->o.zone_capacity,
429 (unsigned long long) td->o.zone_size);
433 nr_zones = (f->real_file_size + zone_size - 1) / zone_size;
434 zbd_info = scalloc(1, sizeof(*zbd_info) +
435 (nr_zones + 1) * sizeof(zbd_info->zone_info[0]));
439 mutex_init_pshared(&zbd_info->mutex);
440 zbd_info->refcount = 1;
441 p = &zbd_info->zone_info[0];
442 for (i = 0; i < nr_zones; i++, p++) {
443 mutex_init_pshared_with_type(&p->mutex,
444 PTHREAD_MUTEX_RECURSIVE);
445 p->start = i * zone_size;
447 p->type = ZBD_ZONE_TYPE_SWR;
448 p->cond = ZBD_ZONE_COND_EMPTY;
449 p->capacity = zone_capacity;
453 p->start = nr_zones * zone_size;
455 f->zbd_info = zbd_info;
456 f->zbd_info->zone_size = zone_size;
457 f->zbd_info->zone_size_log2 = is_power_of_2(zone_size) ?
458 ilog2(zone_size) : 0;
459 f->zbd_info->nr_zones = nr_zones;
464 * Maximum number of zones to report in one operation.
466 #define ZBD_REPORT_MAX_ZONES 8192U
469 * Parse the device zone report and store it in f->zbd_info. Must be called
470 * only for devices that are zoned, namely those with a model != ZBD_NONE.
472 static int parse_zone_info(struct thread_data *td, struct fio_file *f)
475 struct zbd_zone *zones, *z;
476 struct fio_zone_info *p;
477 uint64_t zone_size, offset;
478 struct zoned_block_device_info *zbd_info = NULL;
479 int i, j, ret = -ENOMEM;
481 zones = calloc(ZBD_REPORT_MAX_ZONES, sizeof(struct zbd_zone));
485 nrz = zbd_report_zones(td, f, 0, zones, ZBD_REPORT_MAX_ZONES);
488 log_info("fio: report zones (offset 0) failed for %s (%d).\n",
493 zone_size = zones[0].len;
494 nr_zones = (f->real_file_size + zone_size - 1) / zone_size;
496 if (td->o.zone_size == 0) {
497 td->o.zone_size = zone_size;
498 } else if (td->o.zone_size != zone_size) {
499 log_err("fio: %s job parameter zonesize %llu does not match disk zone size %llu.\n",
500 f->file_name, (unsigned long long) td->o.zone_size,
501 (unsigned long long) zone_size);
506 dprint(FD_ZBD, "Device %s has %d zones of size %llu KB\n", f->file_name,
507 nr_zones, (unsigned long long) zone_size / 1024);
509 zbd_info = scalloc(1, sizeof(*zbd_info) +
510 (nr_zones + 1) * sizeof(zbd_info->zone_info[0]));
513 mutex_init_pshared(&zbd_info->mutex);
514 zbd_info->refcount = 1;
515 p = &zbd_info->zone_info[0];
516 for (offset = 0, j = 0; j < nr_zones;) {
518 for (i = 0; i < nrz; i++, j++, z++, p++) {
519 mutex_init_pshared_with_type(&p->mutex,
520 PTHREAD_MUTEX_RECURSIVE);
522 p->capacity = z->capacity;
524 case ZBD_ZONE_COND_NOT_WP:
525 case ZBD_ZONE_COND_FULL:
526 p->wp = p->start + p->capacity;
529 assert(z->start <= z->wp);
530 assert(z->wp <= z->start + zone_size);
536 case ZBD_ZONE_TYPE_SWR:
545 if (j > 0 && p->start != p[-1].start + zone_size) {
546 log_info("%s: invalid zone data\n",
553 offset = z->start + z->len;
556 nrz = zbd_report_zones(td, f, offset, zones,
557 min((uint32_t)(nr_zones - j),
558 ZBD_REPORT_MAX_ZONES));
561 log_info("fio: report zones (offset %llu) failed for %s (%d).\n",
562 (unsigned long long)offset,
569 zbd_info->zone_info[nr_zones].start = offset;
571 f->zbd_info = zbd_info;
572 f->zbd_info->zone_size = zone_size;
573 f->zbd_info->zone_size_log2 = is_power_of_2(zone_size) ?
574 ilog2(zone_size) : 0;
575 f->zbd_info->nr_zones = nr_zones;
585 static int zbd_set_max_open_zones(struct thread_data *td, struct fio_file *f)
587 struct zoned_block_device_info *zbd = f->zbd_info;
588 unsigned int max_open_zones;
591 if (zbd->model != ZBD_HOST_MANAGED) {
592 /* Only host-managed devices have a max open limit */
593 zbd->max_open_zones = td->o.max_open_zones;
597 /* If host-managed, get the max open limit */
598 ret = zbd_get_max_open_zones(td, f, &max_open_zones);
602 if (!max_open_zones) {
603 /* No device limit */
604 zbd->max_open_zones = td->o.max_open_zones;
605 } else if (!td->o.max_open_zones) {
606 /* No user limit. Set limit to device limit */
607 zbd->max_open_zones = max_open_zones;
608 } else if (td->o.max_open_zones <= max_open_zones) {
609 /* Both user limit and dev limit. User limit not too large */
610 zbd->max_open_zones = td->o.max_open_zones;
612 /* Both user limit and dev limit. User limit too large */
613 td_verror(td, EINVAL,
614 "Specified --max_open_zones is too large");
615 log_err("Specified --max_open_zones (%d) is larger than max (%u)\n",
616 td->o.max_open_zones, max_open_zones);
621 /* Ensure that the limit is not larger than FIO's internal limit */
622 zbd->max_open_zones = min_not_zero(zbd->max_open_zones,
623 (uint32_t) ZBD_MAX_OPEN_ZONES);
624 dprint(FD_ZBD, "%s: using max open zones limit: %"PRIu32"\n",
625 f->file_name, zbd->max_open_zones);
631 * Allocate zone information and store it into f->zbd_info if zonemode=zbd.
633 * Returns 0 upon success and a negative error code upon failure.
635 static int zbd_create_zone_info(struct thread_data *td, struct fio_file *f)
637 enum zbd_zoned_model zbd_model;
640 assert(td->o.zone_mode == ZONE_MODE_ZBD);
642 ret = zbd_get_zoned_model(td, f, &zbd_model);
650 case ZBD_HOST_MANAGED:
651 ret = parse_zone_info(td, f);
656 ret = init_zone_info(td, f);
661 td_verror(td, EINVAL, "Unsupported zoned model");
662 log_err("Unsupported zoned model\n");
666 f->zbd_info->model = zbd_model;
668 ret = zbd_set_max_open_zones(td, f);
670 zbd_free_zone_info(f);
677 void zbd_free_zone_info(struct fio_file *f)
683 pthread_mutex_lock(&f->zbd_info->mutex);
684 refcount = --f->zbd_info->refcount;
685 pthread_mutex_unlock(&f->zbd_info->mutex);
687 assert((int32_t)refcount >= 0);
694 * Initialize f->zbd_info.
696 * Returns 0 upon success and a negative error code upon failure.
698 * Note: this function can only work correctly if it is called before the first
701 static int zbd_init_zone_info(struct thread_data *td, struct fio_file *file)
703 struct thread_data *td2;
707 for_each_td(td2, i) {
708 for_each_file(td2, f2, j) {
709 if (td2 == td && f2 == file)
712 strcmp(f2->file_name, file->file_name) != 0)
714 file->zbd_info = f2->zbd_info;
715 file->zbd_info->refcount++;
720 ret = zbd_create_zone_info(td, file);
722 td_verror(td, -ret, "zbd_create_zone_info() failed");
726 static bool zbd_open_zone(struct thread_data *td, const struct fio_file *f,
728 static int zbd_reset_zone(struct thread_data *td, struct fio_file *f,
729 struct fio_zone_info *z);
731 int zbd_init_files(struct thread_data *td)
736 for_each_file(td, f, i) {
737 if (zbd_init_zone_info(td, f))
743 void zbd_recalc_options_with_zone_granularity(struct thread_data *td)
748 for_each_file(td, f, i) {
749 struct zoned_block_device_info *zbd = f->zbd_info;
750 // zonemode=strided doesn't get per-file zone size.
751 uint64_t zone_size = zbd ? zbd->zone_size : td->o.zone_size;
756 if (td->o.size_nz > 0) {
757 td->o.size = td->o.size_nz * zone_size;
759 if (td->o.io_size_nz > 0) {
760 td->o.io_size = td->o.io_size_nz * zone_size;
762 if (td->o.start_offset_nz > 0) {
763 td->o.start_offset = td->o.start_offset_nz * zone_size;
765 if (td->o.offset_increment_nz > 0) {
766 td->o.offset_increment = td->o.offset_increment_nz * zone_size;
768 if (td->o.zone_skip_nz > 0) {
769 td->o.zone_skip = td->o.zone_skip_nz * zone_size;
774 int zbd_setup_files(struct thread_data *td)
779 if (!zbd_using_direct_io()) {
780 log_err("Using direct I/O is mandatory for writing to ZBD drives\n\n");
784 if (!zbd_verify_sizes())
787 if (!zbd_verify_bs())
790 for_each_file(td, f, i) {
791 struct zoned_block_device_info *zbd = f->zbd_info;
792 struct fio_zone_info *z;
798 f->min_zone = zbd_zone_idx(f, f->file_offset);
799 f->max_zone = zbd_zone_idx(f, f->file_offset + f->io_size);
802 * When all zones in the I/O range are conventional, io_size
803 * can be smaller than zone size, making min_zone the same
804 * as max_zone. This is why the assert below needs to be made
807 if (zbd_is_seq_job(f))
808 assert(f->min_zone < f->max_zone);
810 if (td->o.max_open_zones > 0 &&
811 zbd->max_open_zones != td->o.max_open_zones) {
812 log_err("Different 'max_open_zones' values\n");
815 if (zbd->max_open_zones > ZBD_MAX_OPEN_ZONES) {
816 log_err("'max_open_zones' value is limited by %u\n", ZBD_MAX_OPEN_ZONES);
820 for (zi = f->min_zone; zi < f->max_zone; zi++) {
821 z = &zbd->zone_info[zi];
822 if (z->cond != ZBD_ZONE_COND_IMP_OPEN &&
823 z->cond != ZBD_ZONE_COND_EXP_OPEN)
825 if (zbd_open_zone(td, f, zi))
828 * If the number of open zones exceeds specified limits,
829 * reset all extra open zones.
831 if (zbd_reset_zone(td, f, z) < 0) {
832 log_err("Failed to reest zone %d\n", zi);
841 static inline unsigned int zbd_zone_nr(const struct fio_file *f,
842 struct fio_zone_info *zone)
844 return zone - f->zbd_info->zone_info;
848 * zbd_reset_zone - reset the write pointer of a single zone
849 * @td: FIO thread data.
850 * @f: FIO file associated with the disk for which to reset a write pointer.
853 * Returns 0 upon success and a negative error code upon failure.
855 * The caller must hold z->mutex.
857 static int zbd_reset_zone(struct thread_data *td, struct fio_file *f,
858 struct fio_zone_info *z)
860 uint64_t offset = z->start;
861 uint64_t length = (z+1)->start - offset;
862 uint64_t data_in_zone = z->wp - z->start;
868 assert(is_valid_offset(f, offset + length - 1));
870 dprint(FD_ZBD, "%s: resetting wp of zone %u.\n", f->file_name,
872 switch (f->zbd_info->model) {
874 case ZBD_HOST_MANAGED:
875 ret = zbd_reset_wp(td, f, offset, length);
883 pthread_mutex_lock(&f->zbd_info->mutex);
884 f->zbd_info->sectors_with_data -= data_in_zone;
885 f->zbd_info->wp_sectors_with_data -= data_in_zone;
886 pthread_mutex_unlock(&f->zbd_info->mutex);
890 td->ts.nr_zone_resets++;
895 /* The caller must hold f->zbd_info->mutex */
896 static void zbd_close_zone(struct thread_data *td, const struct fio_file *f,
897 unsigned int zone_idx)
899 uint32_t open_zone_idx = 0;
901 for (; open_zone_idx < f->zbd_info->num_open_zones; open_zone_idx++) {
902 if (f->zbd_info->open_zones[open_zone_idx] == zone_idx)
905 if (open_zone_idx == f->zbd_info->num_open_zones)
908 dprint(FD_ZBD, "%s: closing zone %d\n", f->file_name, zone_idx);
909 memmove(f->zbd_info->open_zones + open_zone_idx,
910 f->zbd_info->open_zones + open_zone_idx + 1,
911 (ZBD_MAX_OPEN_ZONES - (open_zone_idx + 1)) *
912 sizeof(f->zbd_info->open_zones[0]));
913 f->zbd_info->num_open_zones--;
914 td->num_open_zones--;
915 get_zone(f, zone_idx)->open = 0;
919 * Reset a range of zones. Returns 0 upon success and 1 upon failure.
920 * @td: fio thread data.
921 * @f: fio file for which to reset zones
922 * @zb: first zone to reset.
923 * @ze: first zone not to reset.
925 static int zbd_reset_zones(struct thread_data *td, struct fio_file *f,
926 struct fio_zone_info *const zb,
927 struct fio_zone_info *const ze)
929 struct fio_zone_info *z;
930 const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
935 dprint(FD_ZBD, "%s: examining zones %u .. %u\n", f->file_name,
936 zbd_zone_nr(f, zb), zbd_zone_nr(f, ze));
937 for (z = zb; z < ze; z++) {
938 uint32_t nz = zbd_zone_nr(f, z);
943 pthread_mutex_lock(&f->zbd_info->mutex);
944 zbd_close_zone(td, f, nz);
945 pthread_mutex_unlock(&f->zbd_info->mutex);
946 if (z->wp != z->start) {
947 dprint(FD_ZBD, "%s: resetting zone %u\n",
948 f->file_name, zbd_zone_nr(f, z));
949 if (zbd_reset_zone(td, f, z) < 0)
959 * Reset zbd_info.write_cnt, the counter that counts down towards the next
962 static void _zbd_reset_write_cnt(const struct thread_data *td,
963 const struct fio_file *f)
965 assert(0 <= td->o.zrf.u.f && td->o.zrf.u.f <= 1);
967 f->zbd_info->write_cnt = td->o.zrf.u.f ?
968 min(1.0 / td->o.zrf.u.f, 0.0 + UINT_MAX) : UINT_MAX;
971 static void zbd_reset_write_cnt(const struct thread_data *td,
972 const struct fio_file *f)
974 pthread_mutex_lock(&f->zbd_info->mutex);
975 _zbd_reset_write_cnt(td, f);
976 pthread_mutex_unlock(&f->zbd_info->mutex);
979 static bool zbd_dec_and_reset_write_cnt(const struct thread_data *td,
980 const struct fio_file *f)
982 uint32_t write_cnt = 0;
984 pthread_mutex_lock(&f->zbd_info->mutex);
985 assert(f->zbd_info->write_cnt);
986 if (f->zbd_info->write_cnt)
987 write_cnt = --f->zbd_info->write_cnt;
989 _zbd_reset_write_cnt(td, f);
990 pthread_mutex_unlock(&f->zbd_info->mutex);
992 return write_cnt == 0;
1000 /* Calculate the number of sectors with data (swd) and perform action 'a' */
1001 static uint64_t zbd_process_swd(struct thread_data *td,
1002 const struct fio_file *f, enum swd_action a)
1004 struct fio_zone_info *zb, *ze, *z;
1006 uint64_t wp_swd = 0;
1008 zb = get_zone(f, f->min_zone);
1009 ze = get_zone(f, f->max_zone);
1010 for (z = zb; z < ze; z++) {
1012 zone_lock(td, f, z);
1013 wp_swd += z->wp - z->start;
1015 swd += z->wp - z->start;
1017 pthread_mutex_lock(&f->zbd_info->mutex);
1020 assert(f->zbd_info->sectors_with_data == swd);
1021 assert(f->zbd_info->wp_sectors_with_data == wp_swd);
1024 f->zbd_info->sectors_with_data = swd;
1025 f->zbd_info->wp_sectors_with_data = wp_swd;
1028 pthread_mutex_unlock(&f->zbd_info->mutex);
1029 for (z = zb; z < ze; z++)
1037 * The swd check is useful for debugging but takes too much time to leave
1038 * it enabled all the time. Hence it is disabled by default.
1040 static const bool enable_check_swd = false;
1042 /* Check whether the values of zbd_info.*sectors_with_data are correct. */
1043 static void zbd_check_swd(struct thread_data *td, const struct fio_file *f)
1045 if (!enable_check_swd)
1048 zbd_process_swd(td, f, CHECK_SWD);
1051 void zbd_file_reset(struct thread_data *td, struct fio_file *f)
1053 struct fio_zone_info *zb, *ze;
1056 if (!f->zbd_info || !td_write(td))
1059 zb = get_zone(f, f->min_zone);
1060 ze = get_zone(f, f->max_zone);
1061 swd = zbd_process_swd(td, f, SET_SWD);
1062 dprint(FD_ZBD, "%s(%s): swd = %" PRIu64 "\n", __func__, f->file_name,
1065 * If data verification is enabled reset the affected zones before
1066 * writing any data to avoid that a zone reset has to be issued while
1067 * writing data, which causes data loss.
1069 if (td->o.verify != VERIFY_NONE && td->runstate != TD_VERIFYING)
1070 zbd_reset_zones(td, f, zb, ze);
1071 zbd_reset_write_cnt(td, f);
1074 /* The caller must hold f->zbd_info->mutex. */
1075 static bool is_zone_open(const struct thread_data *td, const struct fio_file *f,
1076 unsigned int zone_idx)
1078 struct zoned_block_device_info *zbdi = f->zbd_info;
1081 assert(td->o.job_max_open_zones == 0 || td->num_open_zones <= td->o.job_max_open_zones);
1082 assert(td->o.job_max_open_zones <= zbdi->max_open_zones);
1083 assert(zbdi->num_open_zones <= zbdi->max_open_zones);
1085 for (i = 0; i < zbdi->num_open_zones; i++)
1086 if (zbdi->open_zones[i] == zone_idx)
1093 * Open a ZBD zone if it was not yet open. Returns true if either the zone was
1094 * already open or if opening a new zone is allowed. Returns false if the zone
1095 * was not yet open and opening a new zone would cause the zone limit to be
1098 static bool zbd_open_zone(struct thread_data *td, const struct fio_file *f,
1101 const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
1102 struct fio_zone_info *z = get_zone(f, zone_idx);
1105 if (z->cond == ZBD_ZONE_COND_OFFLINE)
1109 * Skip full zones with data verification enabled because resetting a
1110 * zone causes data loss and hence causes verification to fail.
1112 if (td->o.verify != VERIFY_NONE && zbd_zone_full(f, z, min_bs))
1115 pthread_mutex_lock(&f->zbd_info->mutex);
1116 if (is_zone_open(td, f, zone_idx)) {
1118 * If the zone is already open and going to be full by writes
1119 * in-flight, handle it as a full zone instead of an open zone.
1121 if (z->wp >= zbd_zone_capacity_end(z))
1126 /* Zero means no limit */
1127 if (td->o.job_max_open_zones > 0 &&
1128 td->num_open_zones >= td->o.job_max_open_zones)
1130 if (f->zbd_info->num_open_zones >= f->zbd_info->max_open_zones)
1132 dprint(FD_ZBD, "%s: opening zone %d\n", f->file_name, zone_idx);
1133 f->zbd_info->open_zones[f->zbd_info->num_open_zones++] = zone_idx;
1134 td->num_open_zones++;
1139 pthread_mutex_unlock(&f->zbd_info->mutex);
1143 /* Anything goes as long as it is not a constant. */
1144 static uint32_t pick_random_zone_idx(const struct fio_file *f,
1145 const struct io_u *io_u)
1147 return io_u->offset * f->zbd_info->num_open_zones / f->real_file_size;
1151 * Modify the offset of an I/O unit that does not refer to an open zone such
1152 * that it refers to an open zone. Close an open zone and open a new zone if
1153 * necessary. The open zone is searched across sequential zones.
1154 * This algorithm can only work correctly if all write pointers are
1155 * a multiple of the fio block size. The caller must neither hold z->mutex
1156 * nor f->zbd_info->mutex. Returns with z->mutex held upon success.
1158 static struct fio_zone_info *zbd_convert_to_open_zone(struct thread_data *td,
1161 const uint32_t min_bs = td->o.min_bs[io_u->ddir];
1162 struct fio_file *f = io_u->file;
1163 struct fio_zone_info *z;
1164 unsigned int open_zone_idx = -1;
1165 uint32_t zone_idx, new_zone_idx;
1167 bool wait_zone_close;
1169 assert(is_valid_offset(f, io_u->offset));
1171 if (td->o.max_open_zones || td->o.job_max_open_zones) {
1173 * This statement accesses f->zbd_info->open_zones[] on purpose
1176 zone_idx = f->zbd_info->open_zones[pick_random_zone_idx(f, io_u)];
1178 zone_idx = zbd_zone_idx(f, io_u->offset);
1180 if (zone_idx < f->min_zone)
1181 zone_idx = f->min_zone;
1182 else if (zone_idx >= f->max_zone)
1183 zone_idx = f->max_zone - 1;
1184 dprint(FD_ZBD, "%s(%s): starting from zone %d (offset %lld, buflen %lld)\n",
1185 __func__, f->file_name, zone_idx, io_u->offset, io_u->buflen);
1188 * Since z->mutex is the outer lock and f->zbd_info->mutex the inner
1189 * lock it can happen that the state of the zone with index zone_idx
1190 * has changed after 'z' has been assigned and before f->zbd_info->mutex
1191 * has been obtained. Hence the loop.
1196 z = get_zone(f, zone_idx);
1198 zone_lock(td, f, z);
1199 pthread_mutex_lock(&f->zbd_info->mutex);
1201 if (z->cond != ZBD_ZONE_COND_OFFLINE &&
1202 td->o.max_open_zones == 0 && td->o.job_max_open_zones == 0)
1204 if (f->zbd_info->num_open_zones == 0) {
1205 dprint(FD_ZBD, "%s(%s): no zones are open\n",
1206 __func__, f->file_name);
1207 goto open_other_zone;
1212 * List of opened zones is per-device, shared across all threads.
1213 * Start with quasi-random candidate zone.
1214 * Ignore zones which don't belong to thread's offset/size area.
1216 open_zone_idx = pick_random_zone_idx(f, io_u);
1217 assert(!open_zone_idx ||
1218 open_zone_idx < f->zbd_info->num_open_zones);
1219 tmp_idx = open_zone_idx;
1220 for (i = 0; i < f->zbd_info->num_open_zones; i++) {
1223 if (tmp_idx >= f->zbd_info->num_open_zones)
1225 tmpz = f->zbd_info->open_zones[tmp_idx];
1226 if (f->min_zone <= tmpz && tmpz < f->max_zone) {
1227 open_zone_idx = tmp_idx;
1228 goto found_candidate_zone;
1234 dprint(FD_ZBD, "%s(%s): no candidate zone\n",
1235 __func__, f->file_name);
1236 pthread_mutex_unlock(&f->zbd_info->mutex);
1241 found_candidate_zone:
1242 new_zone_idx = f->zbd_info->open_zones[open_zone_idx];
1243 if (new_zone_idx == zone_idx)
1245 zone_idx = new_zone_idx;
1246 pthread_mutex_unlock(&f->zbd_info->mutex);
1251 /* Both z->mutex and f->zbd_info->mutex are held. */
1254 if (z->wp + min_bs <= zbd_zone_capacity_end(z)) {
1255 pthread_mutex_unlock(&f->zbd_info->mutex);
1260 /* Check if number of open zones reaches one of limits. */
1262 f->zbd_info->num_open_zones == f->max_zone - f->min_zone ||
1263 (td->o.max_open_zones &&
1264 f->zbd_info->num_open_zones == td->o.max_open_zones) ||
1265 (td->o.job_max_open_zones &&
1266 td->num_open_zones == td->o.job_max_open_zones);
1268 pthread_mutex_unlock(&f->zbd_info->mutex);
1270 /* Only z->mutex is held. */
1273 * When number of open zones reaches to one of limits, wait for
1274 * zone close before opening a new zone.
1276 if (wait_zone_close) {
1277 dprint(FD_ZBD, "%s(%s): quiesce to allow open zones to close\n",
1278 __func__, f->file_name);
1282 /* Zone 'z' is full, so try to open a new zone. */
1283 for (i = f->io_size / f->zbd_info->zone_size; i > 0; i--) {
1288 if (!is_valid_offset(f, z->start)) {
1290 zone_idx = f->min_zone;
1291 z = get_zone(f, zone_idx);
1293 assert(is_valid_offset(f, z->start));
1296 zone_lock(td, f, z);
1299 if (zbd_open_zone(td, f, zone_idx))
1303 /* Only z->mutex is held. */
1305 /* Check whether the write fits in any of the already opened zones. */
1306 pthread_mutex_lock(&f->zbd_info->mutex);
1307 for (i = 0; i < f->zbd_info->num_open_zones; i++) {
1308 zone_idx = f->zbd_info->open_zones[i];
1309 if (zone_idx < f->min_zone || zone_idx >= f->max_zone)
1311 pthread_mutex_unlock(&f->zbd_info->mutex);
1314 z = get_zone(f, zone_idx);
1316 zone_lock(td, f, z);
1317 if (z->wp + min_bs <= zbd_zone_capacity_end(z))
1319 pthread_mutex_lock(&f->zbd_info->mutex);
1321 pthread_mutex_unlock(&f->zbd_info->mutex);
1323 dprint(FD_ZBD, "%s(%s): did not open another zone\n", __func__,
1328 dprint(FD_ZBD, "%s(%s): returning zone %d\n", __func__, f->file_name,
1330 io_u->offset = z->start;
1332 assert(z->cond != ZBD_ZONE_COND_OFFLINE);
1336 /* The caller must hold z->mutex. */
1337 static struct fio_zone_info *zbd_replay_write_order(struct thread_data *td,
1339 struct fio_zone_info *z)
1341 const struct fio_file *f = io_u->file;
1342 const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
1344 if (!zbd_open_zone(td, f, zbd_zone_nr(f, z))) {
1346 z = zbd_convert_to_open_zone(td, io_u);
1350 if (z->verify_block * min_bs >= z->capacity) {
1351 log_err("%s: %d * %d >= %llu\n", f->file_name, z->verify_block,
1352 min_bs, (unsigned long long)z->capacity);
1354 * If the assertion below fails during a test run, adding
1355 * "--experimental_verify=1" to the command line may help.
1359 io_u->offset = z->start + z->verify_block * min_bs;
1360 if (io_u->offset + io_u->buflen >= zbd_zone_capacity_end(z)) {
1361 log_err("%s: %llu + %llu >= %llu\n", f->file_name, io_u->offset,
1362 io_u->buflen, (unsigned long long) zbd_zone_capacity_end(z));
1365 z->verify_block += io_u->buflen / min_bs;
1371 * Find another zone for which @io_u fits in the readable data in the zone.
1372 * Search in zones @zb + 1 .. @zl. For random workload, also search in zones
1375 * Either returns NULL or returns a zone pointer. When the zone has write
1376 * pointer, hold the mutex for the zone.
1378 static struct fio_zone_info *
1379 zbd_find_zone(struct thread_data *td, struct io_u *io_u,
1380 struct fio_zone_info *zb, struct fio_zone_info *zl)
1382 const uint32_t min_bs = td->o.min_bs[io_u->ddir];
1383 struct fio_file *f = io_u->file;
1384 struct fio_zone_info *z1, *z2;
1385 const struct fio_zone_info *const zf = get_zone(f, f->min_zone);
1388 * Skip to the next non-empty zone in case of sequential I/O and to
1389 * the nearest non-empty zone in case of random I/O.
1391 for (z1 = zb + 1, z2 = zb - 1; z1 < zl || z2 >= zf; z1++, z2--) {
1392 if (z1 < zl && z1->cond != ZBD_ZONE_COND_OFFLINE) {
1394 zone_lock(td, f, z1);
1395 if (z1->start + min_bs <= z1->wp)
1399 } else if (!td_random(td)) {
1402 if (td_random(td) && z2 >= zf &&
1403 z2->cond != ZBD_ZONE_COND_OFFLINE) {
1405 zone_lock(td, f, z2);
1406 if (z2->start + min_bs <= z2->wp)
1412 dprint(FD_ZBD, "%s: adjusting random read offset failed\n",
1418 * zbd_end_zone_io - update zone status at command completion
1420 * @z: zone info pointer
1422 * If the write command made the zone full, close it.
1424 * The caller must hold z->mutex.
1426 static void zbd_end_zone_io(struct thread_data *td, const struct io_u *io_u,
1427 struct fio_zone_info *z)
1429 const struct fio_file *f = io_u->file;
1431 if (io_u->ddir == DDIR_WRITE &&
1432 io_u->offset + io_u->buflen >= zbd_zone_capacity_end(z)) {
1433 pthread_mutex_lock(&f->zbd_info->mutex);
1434 zbd_close_zone(td, f, zbd_zone_nr(f, z));
1435 pthread_mutex_unlock(&f->zbd_info->mutex);
1440 * zbd_queue_io - update the write pointer of a sequential zone
1442 * @success: Whether or not the I/O unit has been queued successfully
1443 * @q: queueing status (busy, completed or queued).
1445 * For write and trim operations, update the write pointer of the I/O unit
1448 static void zbd_queue_io(struct thread_data *td, struct io_u *io_u, int q,
1451 const struct fio_file *f = io_u->file;
1452 struct zoned_block_device_info *zbd_info = f->zbd_info;
1453 struct fio_zone_info *z;
1460 zone_idx = zbd_zone_idx(f, io_u->offset);
1461 assert(zone_idx < zbd_info->nr_zones);
1462 z = get_zone(f, zone_idx);
1470 "%s: queued I/O (%lld, %llu) for zone %u\n",
1471 f->file_name, io_u->offset, io_u->buflen, zone_idx);
1473 switch (io_u->ddir) {
1475 zone_end = min((uint64_t)(io_u->offset + io_u->buflen),
1476 zbd_zone_capacity_end(z));
1477 pthread_mutex_lock(&zbd_info->mutex);
1479 * z->wp > zone_end means that one or more I/O errors
1482 if (z->wp <= zone_end) {
1483 zbd_info->sectors_with_data += zone_end - z->wp;
1484 zbd_info->wp_sectors_with_data += zone_end - z->wp;
1486 pthread_mutex_unlock(&zbd_info->mutex);
1490 assert(z->wp == z->start);
1496 if (q == FIO_Q_COMPLETED && !io_u->error)
1497 zbd_end_zone_io(td, io_u, z);
1500 if (!success || q != FIO_Q_QUEUED) {
1501 /* BUSY or COMPLETED: unlock the zone */
1503 io_u->zbd_put_io = NULL;
1508 * zbd_put_io - Unlock an I/O unit target zone lock
1511 static void zbd_put_io(struct thread_data *td, const struct io_u *io_u)
1513 const struct fio_file *f = io_u->file;
1514 struct zoned_block_device_info *zbd_info = f->zbd_info;
1515 struct fio_zone_info *z;
1521 zone_idx = zbd_zone_idx(f, io_u->offset);
1522 assert(zone_idx < zbd_info->nr_zones);
1523 z = get_zone(f, zone_idx);
1528 "%s: terminate I/O (%lld, %llu) for zone %u\n",
1529 f->file_name, io_u->offset, io_u->buflen, zone_idx);
1531 zbd_end_zone_io(td, io_u, z);
1534 zbd_check_swd(td, f);
1538 * Windows and MacOS do not define this.
1541 #define EREMOTEIO 121 /* POSIX value */
1544 bool zbd_unaligned_write(int error_code)
1546 switch (error_code) {
1555 * setup_zbd_zone_mode - handle zoneskip as necessary for ZBD drives
1556 * @td: FIO thread data.
1557 * @io_u: FIO I/O unit.
1559 * For sequential workloads, change the file offset to skip zoneskip bytes when
1560 * no more IO can be performed in the current zone.
1561 * - For read workloads, zoneskip is applied when the io has reached the end of
1562 * the zone or the zone write position (when td->o.read_beyond_wp is false).
1563 * - For write workloads, zoneskip is applied when the zone is full.
1564 * This applies only to read and write operations.
1566 void setup_zbd_zone_mode(struct thread_data *td, struct io_u *io_u)
1568 struct fio_file *f = io_u->file;
1569 enum fio_ddir ddir = io_u->ddir;
1570 struct fio_zone_info *z;
1573 assert(td->o.zone_mode == ZONE_MODE_ZBD);
1574 assert(td->o.zone_size);
1576 zone_idx = zbd_zone_idx(f, f->last_pos[ddir]);
1577 z = get_zone(f, zone_idx);
1580 * When the zone capacity is smaller than the zone size and the I/O is
1581 * sequential write, skip to zone end if the latest position is at the
1582 * zone capacity limit.
1584 if (z->capacity < f->zbd_info->zone_size && !td_random(td) &&
1585 ddir == DDIR_WRITE &&
1586 f->last_pos[ddir] >= zbd_zone_capacity_end(z)) {
1588 "%s: Jump from zone capacity limit to zone end:"
1589 " (%llu -> %llu) for zone %u (%llu)\n",
1590 f->file_name, (unsigned long long) f->last_pos[ddir],
1591 (unsigned long long) zbd_zone_end(z), zone_idx,
1592 (unsigned long long) z->capacity);
1593 td->io_skip_bytes += zbd_zone_end(z) - f->last_pos[ddir];
1594 f->last_pos[ddir] = zbd_zone_end(z);
1598 * zone_skip is valid only for sequential workloads.
1600 if (td_random(td) || !td->o.zone_skip)
1604 * It is time to switch to a new zone if:
1605 * - zone_bytes == zone_size bytes have already been accessed
1606 * - The last position reached the end of the current zone.
1607 * - For reads with td->o.read_beyond_wp == false, the last position
1608 * reached the zone write pointer.
1610 if (td->zone_bytes >= td->o.zone_size ||
1611 f->last_pos[ddir] >= zbd_zone_end(z) ||
1612 (ddir == DDIR_READ &&
1613 (!td->o.read_beyond_wp) && f->last_pos[ddir] >= z->wp)) {
1618 f->file_offset += td->o.zone_size + td->o.zone_skip;
1621 * Wrap from the beginning, if we exceed the file size
1623 if (f->file_offset >= f->real_file_size)
1624 f->file_offset = get_start_offset(td, f);
1626 f->last_pos[ddir] = f->file_offset;
1627 td->io_skip_bytes += td->o.zone_skip;
1632 * zbd_adjust_ddir - Adjust an I/O direction for zonemode=zbd.
1634 * @td: FIO thread data.
1635 * @io_u: FIO I/O unit.
1636 * @ddir: I/O direction before adjustment.
1638 * Return adjusted I/O direction.
1640 enum fio_ddir zbd_adjust_ddir(struct thread_data *td, struct io_u *io_u,
1644 * In case read direction is chosen for the first random I/O, fio with
1645 * zonemode=zbd stops because no data can be read from zoned block
1646 * devices with all empty zones. Overwrite the first I/O direction as
1647 * write to make sure data to read exists.
1649 if (ddir != DDIR_READ || !td_rw(td))
1652 if (io_u->file->zbd_info->sectors_with_data ||
1653 td->o.read_beyond_wp)
1660 * zbd_adjust_block - adjust the offset and length as necessary for ZBD drives
1661 * @td: FIO thread data.
1662 * @io_u: FIO I/O unit.
1664 * Locking strategy: returns with z->mutex locked if and only if z refers
1665 * to a sequential zone and if io_u_accept is returned. z is the zone that
1666 * corresponds to io_u->offset at the end of this function.
1668 enum io_u_action zbd_adjust_block(struct thread_data *td, struct io_u *io_u)
1670 struct fio_file *f = io_u->file;
1671 uint32_t zone_idx_b;
1672 struct fio_zone_info *zb, *zl, *orig_zb;
1673 uint32_t orig_len = io_u->buflen;
1674 uint32_t min_bs = td->o.min_bs[io_u->ddir];
1682 assert(is_valid_offset(f, io_u->offset));
1683 assert(io_u->buflen);
1684 zone_idx_b = zbd_zone_idx(f, io_u->offset);
1685 zb = get_zone(f, zone_idx_b);
1689 /* Accept non-write I/Os for conventional zones. */
1690 if (io_u->ddir != DDIR_WRITE)
1693 * Make sure that writes to conventional zones
1694 * don't cross over to any sequential zones.
1696 if (!(zb + 1)->has_wp ||
1697 io_u->offset + io_u->buflen <= (zb + 1)->start)
1700 if (io_u->offset + min_bs > (zb + 1)->start) {
1702 "%s: off=%llu + min_bs=%u > next zone %llu\n",
1703 f->file_name, io_u->offset,
1704 min_bs, (unsigned long long) (zb + 1)->start);
1705 io_u->offset = zb->start + (zb + 1)->start - io_u->offset;
1706 new_len = min(io_u->buflen, (zb + 1)->start - io_u->offset);
1708 new_len = (zb + 1)->start - io_u->offset;
1710 io_u->buflen = new_len / min_bs * min_bs;
1715 * Accept the I/O offset for reads if reading beyond the write pointer
1718 if (zb->cond != ZBD_ZONE_COND_OFFLINE &&
1719 io_u->ddir == DDIR_READ && td->o.read_beyond_wp)
1722 zbd_check_swd(td, f);
1724 zone_lock(td, f, zb);
1726 switch (io_u->ddir) {
1728 if (td->runstate == TD_VERIFYING && td_write(td)) {
1729 zb = zbd_replay_write_order(td, io_u, zb);
1733 * Check that there is enough written data in the zone to do an
1734 * I/O of at least min_bs B. If there isn't, find a new zone for
1737 range = zb->cond != ZBD_ZONE_COND_OFFLINE ?
1738 zb->wp - zb->start : 0;
1739 if (range < min_bs ||
1740 ((!td_random(td)) && (io_u->offset + min_bs > zb->wp))) {
1742 zl = get_zone(f, f->max_zone);
1743 zb = zbd_find_zone(td, io_u, zb, zl);
1746 "%s: zbd_find_zone(%lld, %llu) failed\n",
1747 f->file_name, io_u->offset,
1752 * zbd_find_zone() returned a zone with a range of at
1755 range = zb->wp - zb->start;
1756 assert(range >= min_bs);
1759 io_u->offset = zb->start;
1762 * Make sure the I/O is within the zone valid data range while
1763 * maximizing the I/O size and preserving randomness.
1765 if (range <= io_u->buflen)
1766 io_u->offset = zb->start;
1767 else if (td_random(td))
1768 io_u->offset = zb->start +
1769 ((io_u->offset - orig_zb->start) %
1770 (range - io_u->buflen)) / min_bs * min_bs;
1772 * When zbd_find_zone() returns a conventional zone,
1773 * we can simply accept the new i/o offset here.
1778 * Make sure the I/O does not cross over the zone wp position.
1780 new_len = min((unsigned long long)io_u->buflen,
1781 (unsigned long long)(zb->wp - io_u->offset));
1782 new_len = new_len / min_bs * min_bs;
1783 if (new_len < io_u->buflen) {
1784 io_u->buflen = new_len;
1785 dprint(FD_IO, "Changed length from %u into %llu\n",
1786 orig_len, io_u->buflen);
1788 assert(zb->start <= io_u->offset);
1789 assert(io_u->offset + io_u->buflen <= zb->wp);
1792 if (io_u->buflen > f->zbd_info->zone_size) {
1793 td_verror(td, EINVAL, "I/O buflen exceeds zone size");
1795 "%s: I/O buflen %llu exceeds zone size %llu\n",
1796 f->file_name, io_u->buflen,
1797 (unsigned long long) f->zbd_info->zone_size);
1800 if (!zbd_open_zone(td, f, zone_idx_b)) {
1802 zb = zbd_convert_to_open_zone(td, io_u);
1804 dprint(FD_IO, "%s: can't convert to open zone",
1808 zone_idx_b = zbd_zone_nr(f, zb);
1810 /* Check whether the zone reset threshold has been exceeded */
1811 if (td->o.zrf.u.f) {
1812 if (f->zbd_info->wp_sectors_with_data >=
1813 f->io_size * td->o.zrt.u.f &&
1814 zbd_dec_and_reset_write_cnt(td, f)) {
1818 /* Reset the zone pointer if necessary */
1819 if (zb->reset_zone || zbd_zone_full(f, zb, min_bs)) {
1820 assert(td->o.verify == VERIFY_NONE);
1822 * Since previous write requests may have been submitted
1823 * asynchronously and since we will submit the zone
1824 * reset synchronously, wait until previously submitted
1825 * write requests have completed before issuing a
1830 if (zbd_reset_zone(td, f, zb) < 0)
1833 if (zb->capacity < min_bs) {
1834 td_verror(td, EINVAL, "ZCAP is less min_bs");
1835 log_err("zone capacity %llu smaller than minimum block size %d\n",
1836 (unsigned long long)zb->capacity,
1841 /* Make writes occur at the write pointer */
1842 assert(!zbd_zone_full(f, zb, min_bs));
1843 io_u->offset = zb->wp;
1844 if (!is_valid_offset(f, io_u->offset)) {
1845 td_verror(td, EINVAL, "invalid WP value");
1846 dprint(FD_ZBD, "%s: dropped request with offset %llu\n",
1847 f->file_name, io_u->offset);
1851 * Make sure that the buflen is a multiple of the minimal
1852 * block size. Give up if shrinking would make the request too
1855 new_len = min((unsigned long long)io_u->buflen,
1856 zbd_zone_capacity_end(zb) - io_u->offset);
1857 new_len = new_len / min_bs * min_bs;
1858 if (new_len == io_u->buflen)
1860 if (new_len >= min_bs) {
1861 io_u->buflen = new_len;
1862 dprint(FD_IO, "Changed length from %u into %llu\n",
1863 orig_len, io_u->buflen);
1866 td_verror(td, EIO, "zone remainder too small");
1867 log_err("zone remainder %lld smaller than min block size %d\n",
1868 (zbd_zone_capacity_end(zb) - io_u->offset), min_bs);
1874 case DDIR_SYNC_FILE_RANGE:
1885 assert(zb->cond != ZBD_ZONE_COND_OFFLINE);
1886 assert(!io_u->zbd_queue_io);
1887 assert(!io_u->zbd_put_io);
1888 io_u->zbd_queue_io = zbd_queue_io;
1889 io_u->zbd_put_io = zbd_put_io;
1891 * Since we return with the zone lock still held,
1892 * add an annotation to let Coverity know that it
1895 /* coverity[missing_unlock] */
1899 if (zb && zb->has_wp)
1904 /* Return a string with ZBD statistics */
1905 char *zbd_write_status(const struct thread_stat *ts)
1909 if (asprintf(&res, "; %llu zone resets", (unsigned long long) ts->nr_zone_resets) < 0)