2 * Copyright (C) 2018 Western Digital Corporation or its affiliates.
4 * This file is released under the GPL.
19 #include "oslib/asprintf.h"
25 * zbd_get_zoned_model - Get a device zoned model
26 * @td: FIO thread data
27 * @f: FIO file for which to get model information
29 int zbd_get_zoned_model(struct thread_data *td, struct fio_file *f,
30 enum zbd_zoned_model *model)
34 if (td->io_ops && td->io_ops->get_zoned_model)
35 ret = td->io_ops->get_zoned_model(td, f, model);
37 ret = blkzoned_get_zoned_model(td, f, model);
39 td_verror(td, errno, "get zoned model failed");
40 log_err("%s: get zoned model failed (%d).\n",
48 * zbd_report_zones - Get zone information
49 * @td: FIO thread data.
50 * @f: FIO file for which to get zone information
51 * @offset: offset from which to report zones
52 * @zones: Array of struct zbd_zone
53 * @nr_zones: Size of @zones array
55 * Get zone information into @zones starting from the zone at offset @offset
56 * for the device specified by @f.
58 * Returns the number of zones reported upon success and a negative error code
59 * upon failure. If the zone report is empty, always assume an error (device
60 * problem) and return -EIO.
62 int zbd_report_zones(struct thread_data *td, struct fio_file *f,
63 uint64_t offset, struct zbd_zone *zones,
64 unsigned int nr_zones)
68 if (td->io_ops && td->io_ops->report_zones)
69 ret = td->io_ops->report_zones(td, f, offset, zones, nr_zones);
71 ret = blkzoned_report_zones(td, f, offset, zones, nr_zones);
73 td_verror(td, errno, "report zones failed");
74 log_err("%s: report zones from sector %llu failed (%d).\n",
75 f->file_name, (unsigned long long)offset >> 9, errno);
76 } else if (ret == 0) {
77 td_verror(td, errno, "Empty zone report");
78 log_err("%s: report zones from sector %llu is empty.\n",
79 f->file_name, (unsigned long long)offset >> 9);
87 * zbd_reset_wp - reset the write pointer of a range of zones
88 * @td: FIO thread data.
89 * @f: FIO file for which to reset zones
90 * @offset: Starting offset of the first zone to reset
91 * @length: Length of the range of zones to reset
93 * Reset the write pointer of all zones in the range @offset...@offset+@length.
94 * Returns 0 upon success and a negative error code upon failure.
96 int zbd_reset_wp(struct thread_data *td, struct fio_file *f,
97 uint64_t offset, uint64_t length)
101 if (td->io_ops && td->io_ops->reset_wp)
102 ret = td->io_ops->reset_wp(td, f, offset, length);
104 ret = blkzoned_reset_wp(td, f, offset, length);
106 td_verror(td, errno, "resetting wp failed");
107 log_err("%s: resetting wp for %llu sectors at sector %llu failed (%d).\n",
108 f->file_name, (unsigned long long)length >> 9,
109 (unsigned long long)offset >> 9, errno);
116 * zbd_zone_idx - convert an offset into a zone number
118 * @offset: offset in bytes. If this offset is in the first zone_size bytes
119 * past the disk size then the index of the sentinel is returned.
121 static uint32_t zbd_zone_idx(const struct fio_file *f, uint64_t offset)
125 if (f->zbd_info->zone_size_log2 > 0)
126 zone_idx = offset >> f->zbd_info->zone_size_log2;
128 zone_idx = offset / f->zbd_info->zone_size;
130 return min(zone_idx, f->zbd_info->nr_zones);
134 * zbd_zone_swr - Test whether a zone requires sequential writes
135 * @z: zone info pointer.
137 static inline bool zbd_zone_swr(struct fio_zone_info *z)
139 return z->type == ZBD_ZONE_TYPE_SWR;
143 * zbd_zone_full - verify whether a minimum number of bytes remain in a zone
145 * @z: zone info pointer.
146 * @required: minimum number of bytes that must remain in a zone.
148 * The caller must hold z->mutex.
150 static bool zbd_zone_full(const struct fio_file *f, struct fio_zone_info *z,
153 assert((required & 511) == 0);
155 return zbd_zone_swr(z) &&
156 z->wp + required > z->start + f->zbd_info->zone_size;
159 static void zone_lock(struct thread_data *td, struct fio_file *f, struct fio_zone_info *z)
161 struct zoned_block_device_info *zbd = f->zbd_info;
162 uint32_t nz = z - zbd->zone_info;
164 /* A thread should never lock zones outside its working area. */
165 assert(f->min_zone <= nz && nz < f->max_zone);
168 * Lock the io_u target zone. The zone will be unlocked if io_u offset
169 * is changed or when io_u completes and zbd_put_io() executed.
170 * To avoid multiple jobs doing asynchronous I/Os from deadlocking each
171 * other waiting for zone locks when building an io_u batch, first
172 * only trylock the zone. If the zone is already locked by another job,
173 * process the currently queued I/Os so that I/O progress is made and
176 if (pthread_mutex_trylock(&z->mutex) != 0) {
177 if (!td_ioengine_flagged(td, FIO_SYNCIO))
179 pthread_mutex_lock(&z->mutex);
183 static bool is_valid_offset(const struct fio_file *f, uint64_t offset)
185 return (uint64_t)(offset - f->file_offset) < f->io_size;
188 /* Verify whether direct I/O is used for all host-managed zoned drives. */
189 static bool zbd_using_direct_io(void)
191 struct thread_data *td;
196 if (td->o.odirect || !(td->o.td_ddir & TD_DDIR_WRITE))
198 for_each_file(td, f, j) {
200 f->zbd_info->model == ZBD_HOST_MANAGED)
208 /* Whether or not the I/O range for f includes one or more sequential zones */
209 static bool zbd_is_seq_job(struct fio_file *f)
211 uint32_t zone_idx, zone_idx_b, zone_idx_e;
216 zone_idx_b = zbd_zone_idx(f, f->file_offset);
217 zone_idx_e = zbd_zone_idx(f, f->file_offset + f->io_size - 1);
218 for (zone_idx = zone_idx_b; zone_idx <= zone_idx_e; zone_idx++)
219 if (zbd_zone_swr(&f->zbd_info->zone_info[zone_idx]))
226 * Verify whether offset and size parameters are aligned with zone boundaries.
228 static bool zbd_verify_sizes(void)
230 const struct fio_zone_info *z;
231 struct thread_data *td;
233 uint64_t new_offset, new_end;
238 for_each_file(td, f, j) {
241 if (f->file_offset >= f->real_file_size)
243 if (!zbd_is_seq_job(f))
246 if (!td->o.zone_size) {
247 td->o.zone_size = f->zbd_info->zone_size;
248 if (!td->o.zone_size) {
249 log_err("%s: invalid 0 zone size\n",
253 } else if (td->o.zone_size != f->zbd_info->zone_size) {
254 log_err("%s: job parameter zonesize %llu does not match disk zone size %llu.\n",
255 f->file_name, (unsigned long long) td->o.zone_size,
256 (unsigned long long) f->zbd_info->zone_size);
260 if (td->o.zone_skip &&
261 (td->o.zone_skip < td->o.zone_size ||
262 td->o.zone_skip % td->o.zone_size)) {
263 log_err("%s: zoneskip %llu is not a multiple of the device zone size %llu.\n",
264 f->file_name, (unsigned long long) td->o.zone_skip,
265 (unsigned long long) td->o.zone_size);
269 zone_idx = zbd_zone_idx(f, f->file_offset);
270 z = &f->zbd_info->zone_info[zone_idx];
271 if ((f->file_offset != z->start) &&
272 (td->o.td_ddir != TD_DDIR_READ)) {
273 new_offset = (z+1)->start;
274 if (new_offset >= f->file_offset + f->io_size) {
275 log_info("%s: io_size must be at least one zone\n",
279 log_info("%s: rounded up offset from %llu to %llu\n",
280 f->file_name, (unsigned long long) f->file_offset,
281 (unsigned long long) new_offset);
282 f->io_size -= (new_offset - f->file_offset);
283 f->file_offset = new_offset;
285 zone_idx = zbd_zone_idx(f, f->file_offset + f->io_size);
286 z = &f->zbd_info->zone_info[zone_idx];
288 if ((td->o.td_ddir != TD_DDIR_READ) &&
289 (f->file_offset + f->io_size != new_end)) {
290 if (new_end <= f->file_offset) {
291 log_info("%s: io_size must be at least one zone\n",
295 log_info("%s: rounded down io_size from %llu to %llu\n",
296 f->file_name, (unsigned long long) f->io_size,
297 (unsigned long long) new_end - f->file_offset);
298 f->io_size = new_end - f->file_offset;
301 f->min_zone = zbd_zone_idx(f, f->file_offset);
302 f->max_zone = zbd_zone_idx(f, f->file_offset + f->io_size);
309 static bool zbd_verify_bs(void)
311 struct thread_data *td;
317 for_each_file(td, f, j) {
320 zone_size = f->zbd_info->zone_size;
321 for (k = 0; k < ARRAY_SIZE(td->o.bs); k++) {
322 if (td->o.verify != VERIFY_NONE &&
323 zone_size % td->o.bs[k] != 0) {
324 log_info("%s: block size %llu is not a divisor of the zone size %d\n",
325 f->file_name, td->o.bs[k],
335 static int ilog2(uint64_t i)
347 * Initialize f->zbd_info for devices that are not zoned block devices. This
348 * allows to execute a ZBD workload against a non-ZBD device.
350 static int init_zone_info(struct thread_data *td, struct fio_file *f)
353 struct fio_zone_info *p;
354 uint64_t zone_size = td->o.zone_size;
355 struct zoned_block_device_info *zbd_info = NULL;
356 pthread_mutexattr_t attr;
359 if (zone_size == 0) {
360 log_err("%s: Specifying the zone size is mandatory for regular block devices with --zonemode=zbd\n\n",
365 if (zone_size < 512) {
366 log_err("%s: zone size must be at least 512 bytes for --zonemode=zbd\n\n",
371 nr_zones = (f->real_file_size + zone_size - 1) / zone_size;
372 zbd_info = scalloc(1, sizeof(*zbd_info) +
373 (nr_zones + 1) * sizeof(zbd_info->zone_info[0]));
377 pthread_mutexattr_init(&attr);
378 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
379 pthread_mutexattr_setpshared(&attr, true);
380 pthread_mutex_init(&zbd_info->mutex, &attr);
381 zbd_info->refcount = 1;
382 p = &zbd_info->zone_info[0];
383 for (i = 0; i < nr_zones; i++, p++) {
384 pthread_mutex_init(&p->mutex, &attr);
385 p->start = i * zone_size;
386 p->wp = p->start + zone_size;
387 p->type = ZBD_ZONE_TYPE_SWR;
388 p->cond = ZBD_ZONE_COND_EMPTY;
391 p->start = nr_zones * zone_size;
393 f->zbd_info = zbd_info;
394 f->zbd_info->zone_size = zone_size;
395 f->zbd_info->zone_size_log2 = is_power_of_2(zone_size) ?
396 ilog2(zone_size) : 0;
397 f->zbd_info->nr_zones = nr_zones;
398 pthread_mutexattr_destroy(&attr);
403 * Maximum number of zones to report in one operation.
405 #define ZBD_REPORT_MAX_ZONES 8192U
408 * Parse the device zone report and store it in f->zbd_info. Must be called
409 * only for devices that are zoned, namely those with a model != ZBD_NONE.
411 static int parse_zone_info(struct thread_data *td, struct fio_file *f)
414 struct zbd_zone *zones, *z;
415 struct fio_zone_info *p;
416 uint64_t zone_size, offset;
417 struct zoned_block_device_info *zbd_info = NULL;
418 pthread_mutexattr_t attr;
421 pthread_mutexattr_init(&attr);
422 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
423 pthread_mutexattr_setpshared(&attr, true);
425 zones = calloc(ZBD_REPORT_MAX_ZONES, sizeof(struct zbd_zone));
429 nrz = zbd_report_zones(td, f, 0, zones, ZBD_REPORT_MAX_ZONES);
432 log_info("fio: report zones (offset 0) failed for %s (%d).\n",
437 zone_size = zones[0].len;
438 nr_zones = (f->real_file_size + zone_size - 1) / zone_size;
440 if (td->o.zone_size == 0) {
441 td->o.zone_size = zone_size;
442 } else if (td->o.zone_size != zone_size) {
443 log_err("fio: %s job parameter zonesize %llu does not match disk zone size %llu.\n",
444 f->file_name, (unsigned long long) td->o.zone_size,
445 (unsigned long long) zone_size);
450 dprint(FD_ZBD, "Device %s has %d zones of size %llu KB\n", f->file_name,
451 nr_zones, (unsigned long long) zone_size / 1024);
453 zbd_info = scalloc(1, sizeof(*zbd_info) +
454 (nr_zones + 1) * sizeof(zbd_info->zone_info[0]));
458 pthread_mutex_init(&zbd_info->mutex, &attr);
459 zbd_info->refcount = 1;
460 p = &zbd_info->zone_info[0];
461 for (offset = 0, j = 0; j < nr_zones;) {
463 for (i = 0; i < nrz; i++, j++, z++, p++) {
464 pthread_mutex_init(&p->mutex, &attr);
467 case ZBD_ZONE_COND_NOT_WP:
468 case ZBD_ZONE_COND_FULL:
469 p->wp = p->start + zone_size;
472 assert(z->start <= z->wp);
473 assert(z->wp <= z->start + zone_size);
479 if (j > 0 && p->start != p[-1].start + zone_size) {
480 log_info("%s: invalid zone data\n",
487 offset = z->start + z->len;
490 nrz = zbd_report_zones(td, f, offset,
491 zones, ZBD_REPORT_MAX_ZONES);
494 log_info("fio: report zones (offset %llu) failed for %s (%d).\n",
495 (unsigned long long)offset,
502 zbd_info->zone_info[nr_zones].start = offset;
504 f->zbd_info = zbd_info;
505 f->zbd_info->zone_size = zone_size;
506 f->zbd_info->zone_size_log2 = is_power_of_2(zone_size) ?
507 ilog2(zone_size) : 0;
508 f->zbd_info->nr_zones = nr_zones;
515 pthread_mutexattr_destroy(&attr);
520 * Allocate zone information and store it into f->zbd_info if zonemode=zbd.
522 * Returns 0 upon success and a negative error code upon failure.
524 static int zbd_create_zone_info(struct thread_data *td, struct fio_file *f)
526 enum zbd_zoned_model zbd_model;
529 assert(td->o.zone_mode == ZONE_MODE_ZBD);
531 ret = zbd_get_zoned_model(td, f, &zbd_model);
539 case ZBD_HOST_MANAGED:
540 ret = parse_zone_info(td, f);
543 ret = init_zone_info(td, f);
546 td_verror(td, EINVAL, "Unsupported zoned model");
547 log_err("Unsupported zoned model\n");
552 f->zbd_info->model = zbd_model;
556 void zbd_free_zone_info(struct fio_file *f)
562 pthread_mutex_lock(&f->zbd_info->mutex);
563 refcount = --f->zbd_info->refcount;
564 pthread_mutex_unlock(&f->zbd_info->mutex);
566 assert((int32_t)refcount >= 0);
573 * Initialize f->zbd_info.
575 * Returns 0 upon success and a negative error code upon failure.
577 * Note: this function can only work correctly if it is called before the first
580 static int zbd_init_zone_info(struct thread_data *td, struct fio_file *file)
582 struct thread_data *td2;
586 for_each_td(td2, i) {
587 for_each_file(td2, f2, j) {
588 if (td2 == td && f2 == file)
591 strcmp(f2->file_name, file->file_name) != 0)
593 file->zbd_info = f2->zbd_info;
594 file->zbd_info->refcount++;
599 ret = zbd_create_zone_info(td, file);
601 td_verror(td, -ret, "zbd_create_zone_info() failed");
605 int zbd_setup_files(struct thread_data *td)
610 for_each_file(td, f, i) {
611 if (zbd_init_zone_info(td, f))
615 if (!zbd_using_direct_io()) {
616 log_err("Using direct I/O is mandatory for writing to ZBD drives\n\n");
620 if (!zbd_verify_sizes())
623 if (!zbd_verify_bs())
630 * zbd_reset_range - reset zones for a range of sectors
631 * @td: FIO thread data.
632 * @f: Fio file for which to reset zones
633 * @sector: Starting sector in units of 512 bytes
634 * @nr_sectors: Number of sectors in units of 512 bytes
636 * Returns 0 upon success and a negative error code upon failure.
638 static int zbd_reset_range(struct thread_data *td, struct fio_file *f,
639 uint64_t offset, uint64_t length)
641 uint32_t zone_idx_b, zone_idx_e;
642 struct fio_zone_info *zb, *ze, *z;
645 assert(is_valid_offset(f, offset + length - 1));
647 switch (f->zbd_info->model) {
649 case ZBD_HOST_MANAGED:
650 ret = zbd_reset_wp(td, f, offset, length);
658 zone_idx_b = zbd_zone_idx(f, offset);
659 zb = &f->zbd_info->zone_info[zone_idx_b];
660 zone_idx_e = zbd_zone_idx(f, offset + length);
661 ze = &f->zbd_info->zone_info[zone_idx_e];
662 for (z = zb; z < ze; z++) {
663 pthread_mutex_lock(&z->mutex);
664 pthread_mutex_lock(&f->zbd_info->mutex);
665 f->zbd_info->sectors_with_data -= z->wp - z->start;
666 pthread_mutex_unlock(&f->zbd_info->mutex);
669 pthread_mutex_unlock(&z->mutex);
672 td->ts.nr_zone_resets += ze - zb;
677 static unsigned int zbd_zone_nr(struct zoned_block_device_info *zbd_info,
678 struct fio_zone_info *zone)
680 return zone - zbd_info->zone_info;
684 * zbd_reset_zone - reset the write pointer of a single zone
685 * @td: FIO thread data.
686 * @f: FIO file associated with the disk for which to reset a write pointer.
689 * Returns 0 upon success and a negative error code upon failure.
691 static int zbd_reset_zone(struct thread_data *td, struct fio_file *f,
692 struct fio_zone_info *z)
694 dprint(FD_ZBD, "%s: resetting wp of zone %u.\n", f->file_name,
695 zbd_zone_nr(f->zbd_info, z));
697 return zbd_reset_range(td, f, z->start, (z+1)->start - z->start);
700 /* The caller must hold f->zbd_info->mutex */
701 static void zbd_close_zone(struct thread_data *td, const struct fio_file *f,
702 unsigned int open_zone_idx)
706 assert(open_zone_idx < f->zbd_info->num_open_zones);
707 zone_idx = f->zbd_info->open_zones[open_zone_idx];
708 memmove(f->zbd_info->open_zones + open_zone_idx,
709 f->zbd_info->open_zones + open_zone_idx + 1,
710 (ZBD_MAX_OPEN_ZONES - (open_zone_idx + 1)) *
711 sizeof(f->zbd_info->open_zones[0]));
712 f->zbd_info->num_open_zones--;
713 f->zbd_info->zone_info[zone_idx].open = 0;
717 * Reset a range of zones. Returns 0 upon success and 1 upon failure.
718 * @td: fio thread data.
719 * @f: fio file for which to reset zones
720 * @zb: first zone to reset.
721 * @ze: first zone not to reset.
722 * @all_zones: whether to reset all zones or only those zones for which the
723 * write pointer is not a multiple of td->o.min_bs[DDIR_WRITE].
725 static int zbd_reset_zones(struct thread_data *td, struct fio_file *f,
726 struct fio_zone_info *const zb,
727 struct fio_zone_info *const ze, bool all_zones)
729 struct fio_zone_info *z;
730 const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
736 dprint(FD_ZBD, "%s: examining zones %u .. %u\n", f->file_name,
737 zbd_zone_nr(f->zbd_info, zb), zbd_zone_nr(f->zbd_info, ze));
738 for (z = zb; z < ze; z++) {
739 uint32_t nz = z - f->zbd_info->zone_info;
741 if (!zbd_zone_swr(z))
747 pthread_mutex_lock(&f->zbd_info->mutex);
748 for (i = 0; i < f->zbd_info->num_open_zones; i++) {
749 if (f->zbd_info->open_zones[i] == nz)
750 zbd_close_zone(td, f, i);
752 pthread_mutex_unlock(&f->zbd_info->mutex);
754 reset_wp = z->wp != z->start;
756 reset_wp = z->wp % min_bs != 0;
759 dprint(FD_ZBD, "%s: resetting zone %u\n",
761 zbd_zone_nr(f->zbd_info, z));
762 if (zbd_reset_zone(td, f, z) < 0)
765 pthread_mutex_unlock(&z->mutex);
772 * Reset zbd_info.write_cnt, the counter that counts down towards the next
775 static void zbd_reset_write_cnt(const struct thread_data *td,
776 const struct fio_file *f)
778 assert(0 <= td->o.zrf.u.f && td->o.zrf.u.f <= 1);
780 pthread_mutex_lock(&f->zbd_info->mutex);
781 f->zbd_info->write_cnt = td->o.zrf.u.f ?
782 min(1.0 / td->o.zrf.u.f, 0.0 + UINT_MAX) : UINT_MAX;
783 pthread_mutex_unlock(&f->zbd_info->mutex);
786 static bool zbd_dec_and_reset_write_cnt(const struct thread_data *td,
787 const struct fio_file *f)
789 uint32_t write_cnt = 0;
791 pthread_mutex_lock(&f->zbd_info->mutex);
792 assert(f->zbd_info->write_cnt);
793 if (f->zbd_info->write_cnt)
794 write_cnt = --f->zbd_info->write_cnt;
796 zbd_reset_write_cnt(td, f);
797 pthread_mutex_unlock(&f->zbd_info->mutex);
799 return write_cnt == 0;
807 /* Calculate the number of sectors with data (swd) and perform action 'a' */
808 static uint64_t zbd_process_swd(const struct fio_file *f, enum swd_action a)
810 struct fio_zone_info *zb, *ze, *z;
813 zb = &f->zbd_info->zone_info[zbd_zone_idx(f, f->file_offset)];
814 ze = &f->zbd_info->zone_info[zbd_zone_idx(f, f->file_offset +
816 for (z = zb; z < ze; z++) {
817 pthread_mutex_lock(&z->mutex);
818 swd += z->wp - z->start;
820 pthread_mutex_lock(&f->zbd_info->mutex);
823 assert(f->zbd_info->sectors_with_data == swd);
826 f->zbd_info->sectors_with_data = swd;
829 pthread_mutex_unlock(&f->zbd_info->mutex);
830 for (z = zb; z < ze; z++)
831 pthread_mutex_unlock(&z->mutex);
837 * The swd check is useful for debugging but takes too much time to leave
838 * it enabled all the time. Hence it is disabled by default.
840 static const bool enable_check_swd = false;
842 /* Check whether the value of zbd_info.sectors_with_data is correct. */
843 static void zbd_check_swd(const struct fio_file *f)
845 if (!enable_check_swd)
848 zbd_process_swd(f, CHECK_SWD);
851 static void zbd_init_swd(struct fio_file *f)
855 if (!enable_check_swd)
858 swd = zbd_process_swd(f, SET_SWD);
859 dprint(FD_ZBD, "%s(%s): swd = %" PRIu64 "\n", __func__, f->file_name,
863 void zbd_file_reset(struct thread_data *td, struct fio_file *f)
865 struct fio_zone_info *zb, *ze;
867 if (!f->zbd_info || !td_write(td))
870 zb = &f->zbd_info->zone_info[f->min_zone];
871 ze = &f->zbd_info->zone_info[f->max_zone];
874 * If data verification is enabled reset the affected zones before
875 * writing any data to avoid that a zone reset has to be issued while
876 * writing data, which causes data loss.
878 zbd_reset_zones(td, f, zb, ze, td->o.verify != VERIFY_NONE &&
879 td->runstate != TD_VERIFYING);
880 zbd_reset_write_cnt(td, f);
883 /* The caller must hold f->zbd_info->mutex. */
884 static bool is_zone_open(const struct thread_data *td, const struct fio_file *f,
885 unsigned int zone_idx)
887 struct zoned_block_device_info *zbdi = f->zbd_info;
890 assert(td->o.max_open_zones <= ARRAY_SIZE(zbdi->open_zones));
891 assert(zbdi->num_open_zones <= td->o.max_open_zones);
893 for (i = 0; i < zbdi->num_open_zones; i++)
894 if (zbdi->open_zones[i] == zone_idx)
901 * Open a ZBD zone if it was not yet open. Returns true if either the zone was
902 * already open or if opening a new zone is allowed. Returns false if the zone
903 * was not yet open and opening a new zone would cause the zone limit to be
906 static bool zbd_open_zone(struct thread_data *td, const struct io_u *io_u,
909 const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
910 const struct fio_file *f = io_u->file;
911 struct fio_zone_info *z = &f->zbd_info->zone_info[zone_idx];
914 if (z->cond == ZBD_ZONE_COND_OFFLINE)
918 * Skip full zones with data verification enabled because resetting a
919 * zone causes data loss and hence causes verification to fail.
921 if (td->o.verify != VERIFY_NONE && zbd_zone_full(f, z, min_bs))
924 /* Zero means no limit */
925 if (!td->o.max_open_zones)
928 pthread_mutex_lock(&f->zbd_info->mutex);
929 if (is_zone_open(td, f, zone_idx))
932 if (f->zbd_info->num_open_zones >= td->o.max_open_zones)
934 dprint(FD_ZBD, "%s: opening zone %d\n", f->file_name, zone_idx);
935 f->zbd_info->open_zones[f->zbd_info->num_open_zones++] = zone_idx;
940 pthread_mutex_unlock(&f->zbd_info->mutex);
944 /* Anything goes as long as it is not a constant. */
945 static uint32_t pick_random_zone_idx(const struct fio_file *f,
946 const struct io_u *io_u)
948 return io_u->offset * f->zbd_info->num_open_zones / f->real_file_size;
952 * Modify the offset of an I/O unit that does not refer to an open zone such
953 * that it refers to an open zone. Close an open zone and open a new zone if
954 * necessary. This algorithm can only work correctly if all write pointers are
955 * a multiple of the fio block size. The caller must neither hold z->mutex
956 * nor f->zbd_info->mutex. Returns with z->mutex held upon success.
958 static struct fio_zone_info *zbd_convert_to_open_zone(struct thread_data *td,
961 const uint32_t min_bs = td->o.min_bs[io_u->ddir];
962 struct fio_file *f = io_u->file;
963 struct fio_zone_info *z;
964 unsigned int open_zone_idx = -1;
965 uint32_t zone_idx, new_zone_idx;
968 assert(is_valid_offset(f, io_u->offset));
970 if (td->o.max_open_zones) {
972 * This statement accesses f->zbd_info->open_zones[] on purpose
975 zone_idx = f->zbd_info->open_zones[pick_random_zone_idx(f, io_u)];
977 zone_idx = zbd_zone_idx(f, io_u->offset);
979 if (zone_idx < f->min_zone)
980 zone_idx = f->min_zone;
981 else if (zone_idx >= f->max_zone)
982 zone_idx = f->max_zone - 1;
983 dprint(FD_ZBD, "%s(%s): starting from zone %d (offset %lld, buflen %lld)\n",
984 __func__, f->file_name, zone_idx, io_u->offset, io_u->buflen);
987 * Since z->mutex is the outer lock and f->zbd_info->mutex the inner
988 * lock it can happen that the state of the zone with index zone_idx
989 * has changed after 'z' has been assigned and before f->zbd_info->mutex
990 * has been obtained. Hence the loop.
995 z = &f->zbd_info->zone_info[zone_idx];
998 pthread_mutex_lock(&f->zbd_info->mutex);
999 if (td->o.max_open_zones == 0)
1001 if (f->zbd_info->num_open_zones == 0) {
1002 pthread_mutex_unlock(&f->zbd_info->mutex);
1003 pthread_mutex_unlock(&z->mutex);
1004 dprint(FD_ZBD, "%s(%s): no zones are open\n",
1005 __func__, f->file_name);
1010 * List of opened zones is per-device, shared across all threads.
1011 * Start with quasi-random candidate zone.
1012 * Ignore zones which don't belong to thread's offset/size area.
1014 open_zone_idx = pick_random_zone_idx(f, io_u);
1015 assert(open_zone_idx < f->zbd_info->num_open_zones);
1016 tmp_idx = open_zone_idx;
1017 for (i = 0; i < f->zbd_info->num_open_zones; i++) {
1020 if (tmp_idx >= f->zbd_info->num_open_zones)
1022 tmpz = f->zbd_info->open_zones[tmp_idx];
1023 if (f->min_zone <= tmpz && tmpz < f->max_zone) {
1024 open_zone_idx = tmp_idx;
1025 goto found_candidate_zone;
1031 dprint(FD_ZBD, "%s(%s): no candidate zone\n",
1032 __func__, f->file_name);
1033 pthread_mutex_unlock(&f->zbd_info->mutex);
1034 pthread_mutex_unlock(&z->mutex);
1037 found_candidate_zone:
1038 new_zone_idx = f->zbd_info->open_zones[open_zone_idx];
1039 if (new_zone_idx == zone_idx)
1041 zone_idx = new_zone_idx;
1042 pthread_mutex_unlock(&f->zbd_info->mutex);
1043 pthread_mutex_unlock(&z->mutex);
1046 /* Both z->mutex and f->zbd_info->mutex are held. */
1049 if (z->wp + min_bs <= (z+1)->start) {
1050 pthread_mutex_unlock(&f->zbd_info->mutex);
1053 dprint(FD_ZBD, "%s(%s): closing zone %d\n", __func__, f->file_name,
1055 if (td->o.max_open_zones)
1056 zbd_close_zone(td, f, open_zone_idx);
1057 pthread_mutex_unlock(&f->zbd_info->mutex);
1059 /* Only z->mutex is held. */
1061 /* Zone 'z' is full, so try to open a new zone. */
1062 for (i = f->io_size / f->zbd_info->zone_size; i > 0; i--) {
1064 pthread_mutex_unlock(&z->mutex);
1066 if (!is_valid_offset(f, z->start)) {
1068 zone_idx = f->min_zone;
1069 z = &f->zbd_info->zone_info[zone_idx];
1071 assert(is_valid_offset(f, z->start));
1072 zone_lock(td, f, z);
1075 if (zbd_open_zone(td, io_u, zone_idx))
1079 /* Only z->mutex is held. */
1081 /* Check whether the write fits in any of the already opened zones. */
1082 pthread_mutex_lock(&f->zbd_info->mutex);
1083 for (i = 0; i < f->zbd_info->num_open_zones; i++) {
1084 zone_idx = f->zbd_info->open_zones[i];
1085 if (zone_idx < f->min_zone || zone_idx >= f->max_zone)
1087 pthread_mutex_unlock(&f->zbd_info->mutex);
1088 pthread_mutex_unlock(&z->mutex);
1090 z = &f->zbd_info->zone_info[zone_idx];
1092 zone_lock(td, f, z);
1093 if (z->wp + min_bs <= (z+1)->start)
1095 pthread_mutex_lock(&f->zbd_info->mutex);
1097 pthread_mutex_unlock(&f->zbd_info->mutex);
1098 pthread_mutex_unlock(&z->mutex);
1099 dprint(FD_ZBD, "%s(%s): did not open another zone\n", __func__,
1104 dprint(FD_ZBD, "%s(%s): returning zone %d\n", __func__, f->file_name,
1106 io_u->offset = z->start;
1110 /* The caller must hold z->mutex. */
1111 static struct fio_zone_info *zbd_replay_write_order(struct thread_data *td,
1113 struct fio_zone_info *z)
1115 const struct fio_file *f = io_u->file;
1116 const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
1118 if (!zbd_open_zone(td, io_u, z - f->zbd_info->zone_info)) {
1119 pthread_mutex_unlock(&z->mutex);
1120 z = zbd_convert_to_open_zone(td, io_u);
1124 if (z->verify_block * min_bs >= f->zbd_info->zone_size)
1125 log_err("%s: %d * %d >= %llu\n", f->file_name, z->verify_block,
1126 min_bs, (unsigned long long) f->zbd_info->zone_size);
1127 io_u->offset = z->start + z->verify_block++ * min_bs;
1132 * Find another zone for which @io_u fits below the write pointer. Start
1133 * searching in zones @zb + 1 .. @zl and continue searching in zones
1136 * Either returns NULL or returns a zone pointer and holds the mutex for that
1139 static struct fio_zone_info *
1140 zbd_find_zone(struct thread_data *td, struct io_u *io_u,
1141 struct fio_zone_info *zb, struct fio_zone_info *zl)
1143 const uint32_t min_bs = td->o.min_bs[io_u->ddir];
1144 struct fio_file *f = io_u->file;
1145 struct fio_zone_info *z1, *z2;
1146 const struct fio_zone_info *const zf =
1147 &f->zbd_info->zone_info[zbd_zone_idx(f, f->file_offset)];
1150 * Skip to the next non-empty zone in case of sequential I/O and to
1151 * the nearest non-empty zone in case of random I/O.
1153 for (z1 = zb + 1, z2 = zb - 1; z1 < zl || z2 >= zf; z1++, z2--) {
1154 if (z1 < zl && z1->cond != ZBD_ZONE_COND_OFFLINE) {
1155 zone_lock(td, f, z1);
1156 if (z1->start + min_bs <= z1->wp)
1158 pthread_mutex_unlock(&z1->mutex);
1159 } else if (!td_random(td)) {
1162 if (td_random(td) && z2 >= zf &&
1163 z2->cond != ZBD_ZONE_COND_OFFLINE) {
1164 zone_lock(td, f, z2);
1165 if (z2->start + min_bs <= z2->wp)
1167 pthread_mutex_unlock(&z2->mutex);
1170 dprint(FD_ZBD, "%s: adjusting random read offset failed\n",
1176 * zbd_queue_io - update the write pointer of a sequential zone
1178 * @success: Whether or not the I/O unit has been queued successfully
1179 * @q: queueing status (busy, completed or queued).
1181 * For write and trim operations, update the write pointer of the I/O unit
1184 static void zbd_queue_io(struct io_u *io_u, int q, bool success)
1186 const struct fio_file *f = io_u->file;
1187 struct zoned_block_device_info *zbd_info = f->zbd_info;
1188 struct fio_zone_info *z;
1195 zone_idx = zbd_zone_idx(f, io_u->offset);
1196 assert(zone_idx < zbd_info->nr_zones);
1197 z = &zbd_info->zone_info[zone_idx];
1199 if (!zbd_zone_swr(z))
1206 "%s: queued I/O (%lld, %llu) for zone %u\n",
1207 f->file_name, io_u->offset, io_u->buflen, zone_idx);
1209 switch (io_u->ddir) {
1211 zone_end = min((uint64_t)(io_u->offset + io_u->buflen),
1213 pthread_mutex_lock(&zbd_info->mutex);
1215 * z->wp > zone_end means that one or more I/O errors
1218 if (z->wp <= zone_end)
1219 zbd_info->sectors_with_data += zone_end - z->wp;
1220 pthread_mutex_unlock(&zbd_info->mutex);
1224 assert(z->wp == z->start);
1231 if (!success || q != FIO_Q_QUEUED) {
1232 /* BUSY or COMPLETED: unlock the zone */
1233 pthread_mutex_unlock(&z->mutex);
1234 io_u->zbd_put_io = NULL;
1239 * zbd_put_io - Unlock an I/O unit target zone lock
1242 static void zbd_put_io(const struct io_u *io_u)
1244 const struct fio_file *f = io_u->file;
1245 struct zoned_block_device_info *zbd_info = f->zbd_info;
1246 struct fio_zone_info *z;
1253 zone_idx = zbd_zone_idx(f, io_u->offset);
1254 assert(zone_idx < zbd_info->nr_zones);
1255 z = &zbd_info->zone_info[zone_idx];
1257 if (!zbd_zone_swr(z))
1261 "%s: terminate I/O (%lld, %llu) for zone %u\n",
1262 f->file_name, io_u->offset, io_u->buflen, zone_idx);
1264 ret = pthread_mutex_unlock(&z->mutex);
1270 * Windows and MacOS do not define this.
1273 #define EREMOTEIO 121 /* POSIX value */
1276 bool zbd_unaligned_write(int error_code)
1278 switch (error_code) {
1287 * setup_zbd_zone_mode - handle zoneskip as necessary for ZBD drives
1288 * @td: FIO thread data.
1289 * @io_u: FIO I/O unit.
1291 * For sequential workloads, change the file offset to skip zoneskip bytes when
1292 * no more IO can be performed in the current zone.
1293 * - For read workloads, zoneskip is applied when the io has reached the end of
1294 * the zone or the zone write position (when td->o.read_beyond_wp is false).
1295 * - For write workloads, zoneskip is applied when the zone is full.
1296 * This applies only to read and write operations.
1298 void setup_zbd_zone_mode(struct thread_data *td, struct io_u *io_u)
1300 struct fio_file *f = io_u->file;
1301 enum fio_ddir ddir = io_u->ddir;
1302 struct fio_zone_info *z;
1305 assert(td->o.zone_mode == ZONE_MODE_ZBD);
1306 assert(td->o.zone_size);
1309 * zone_skip is valid only for sequential workloads.
1311 if (td_random(td) || !td->o.zone_skip)
1315 * It is time to switch to a new zone if:
1316 * - zone_bytes == zone_size bytes have already been accessed
1317 * - The last position reached the end of the current zone.
1318 * - For reads with td->o.read_beyond_wp == false, the last position
1319 * reached the zone write pointer.
1321 zone_idx = zbd_zone_idx(f, f->last_pos[ddir]);
1322 z = &f->zbd_info->zone_info[zone_idx];
1324 if (td->zone_bytes >= td->o.zone_size ||
1325 f->last_pos[ddir] >= (z+1)->start ||
1326 (ddir == DDIR_READ &&
1327 (!td->o.read_beyond_wp) && f->last_pos[ddir] >= z->wp)) {
1332 f->file_offset += td->o.zone_size + td->o.zone_skip;
1335 * Wrap from the beginning, if we exceed the file size
1337 if (f->file_offset >= f->real_file_size)
1338 f->file_offset = get_start_offset(td, f);
1340 f->last_pos[ddir] = f->file_offset;
1341 td->io_skip_bytes += td->o.zone_skip;
1346 * zbd_adjust_ddir - Adjust an I/O direction for zonedmode=zbd.
1348 * @td: FIO thread data.
1349 * @io_u: FIO I/O unit.
1350 * @ddir: I/O direction before adjustment.
1352 * Return adjusted I/O direction.
1354 enum fio_ddir zbd_adjust_ddir(struct thread_data *td, struct io_u *io_u,
1358 * In case read direction is chosen for the first random I/O, fio with
1359 * zonemode=zbd stops because no data can be read from zoned block
1360 * devices with all empty zones. Overwrite the first I/O direction as
1361 * write to make sure data to read exists.
1363 if (ddir != DDIR_READ || !td_rw(td))
1366 if (io_u->file->zbd_info->sectors_with_data ||
1367 td->o.read_beyond_wp)
1374 * zbd_adjust_block - adjust the offset and length as necessary for ZBD drives
1375 * @td: FIO thread data.
1376 * @io_u: FIO I/O unit.
1378 * Locking strategy: returns with z->mutex locked if and only if z refers
1379 * to a sequential zone and if io_u_accept is returned. z is the zone that
1380 * corresponds to io_u->offset at the end of this function.
1382 enum io_u_action zbd_adjust_block(struct thread_data *td, struct io_u *io_u)
1384 struct fio_file *f = io_u->file;
1385 uint32_t zone_idx_b;
1386 struct fio_zone_info *zb, *zl, *orig_zb;
1387 uint32_t orig_len = io_u->buflen;
1388 uint32_t min_bs = td->o.min_bs[io_u->ddir];
1396 assert(is_valid_offset(f, io_u->offset));
1397 assert(io_u->buflen);
1398 zone_idx_b = zbd_zone_idx(f, io_u->offset);
1399 zb = &f->zbd_info->zone_info[zone_idx_b];
1402 /* Accept the I/O offset for conventional zones. */
1403 if (!zbd_zone_swr(zb))
1407 * Accept the I/O offset for reads if reading beyond the write pointer
1410 if (zb->cond != ZBD_ZONE_COND_OFFLINE &&
1411 io_u->ddir == DDIR_READ && td->o.read_beyond_wp)
1416 zone_lock(td, f, zb);
1418 switch (io_u->ddir) {
1420 if (td->runstate == TD_VERIFYING) {
1422 zb = zbd_replay_write_order(td, io_u, zb);
1426 * Check that there is enough written data in the zone to do an
1427 * I/O of at least min_bs B. If there isn't, find a new zone for
1430 range = zb->cond != ZBD_ZONE_COND_OFFLINE ?
1431 zb->wp - zb->start : 0;
1432 if (range < min_bs ||
1433 ((!td_random(td)) && (io_u->offset + min_bs > zb->wp))) {
1434 pthread_mutex_unlock(&zb->mutex);
1435 zl = &f->zbd_info->zone_info[zbd_zone_idx(f,
1436 f->file_offset + f->io_size)];
1437 zb = zbd_find_zone(td, io_u, zb, zl);
1440 "%s: zbd_find_zone(%lld, %llu) failed\n",
1441 f->file_name, io_u->offset,
1446 * zbd_find_zone() returned a zone with a range of at
1449 range = zb->wp - zb->start;
1450 assert(range >= min_bs);
1453 io_u->offset = zb->start;
1456 * Make sure the I/O is within the zone valid data range while
1457 * maximizing the I/O size and preserving randomness.
1459 if (range <= io_u->buflen)
1460 io_u->offset = zb->start;
1461 else if (td_random(td))
1462 io_u->offset = zb->start +
1463 ((io_u->offset - orig_zb->start) %
1464 (range - io_u->buflen)) / min_bs * min_bs;
1466 * Make sure the I/O does not cross over the zone wp position.
1468 new_len = min((unsigned long long)io_u->buflen,
1469 (unsigned long long)(zb->wp - io_u->offset));
1470 new_len = new_len / min_bs * min_bs;
1471 if (new_len < io_u->buflen) {
1472 io_u->buflen = new_len;
1473 dprint(FD_IO, "Changed length from %u into %llu\n",
1474 orig_len, io_u->buflen);
1476 assert(zb->start <= io_u->offset);
1477 assert(io_u->offset + io_u->buflen <= zb->wp);
1480 if (io_u->buflen > f->zbd_info->zone_size)
1482 if (!zbd_open_zone(td, io_u, zone_idx_b)) {
1483 pthread_mutex_unlock(&zb->mutex);
1484 zb = zbd_convert_to_open_zone(td, io_u);
1487 zone_idx_b = zb - f->zbd_info->zone_info;
1489 /* Check whether the zone reset threshold has been exceeded */
1490 if (td->o.zrf.u.f) {
1491 if (f->zbd_info->sectors_with_data >=
1492 f->io_size * td->o.zrt.u.f &&
1493 zbd_dec_and_reset_write_cnt(td, f)) {
1497 /* Reset the zone pointer if necessary */
1498 if (zb->reset_zone || zbd_zone_full(f, zb, min_bs)) {
1499 assert(td->o.verify == VERIFY_NONE);
1501 * Since previous write requests may have been submitted
1502 * asynchronously and since we will submit the zone
1503 * reset synchronously, wait until previously submitted
1504 * write requests have completed before issuing a
1509 if (zbd_reset_zone(td, f, zb) < 0)
1512 /* Make writes occur at the write pointer */
1513 assert(!zbd_zone_full(f, zb, min_bs));
1514 io_u->offset = zb->wp;
1515 if (!is_valid_offset(f, io_u->offset)) {
1516 dprint(FD_ZBD, "Dropped request with offset %llu\n",
1521 * Make sure that the buflen is a multiple of the minimal
1522 * block size. Give up if shrinking would make the request too
1525 new_len = min((unsigned long long)io_u->buflen,
1526 (zb + 1)->start - io_u->offset);
1527 new_len = new_len / min_bs * min_bs;
1528 if (new_len == io_u->buflen)
1530 if (new_len >= min_bs) {
1531 io_u->buflen = new_len;
1532 dprint(FD_IO, "Changed length from %u into %llu\n",
1533 orig_len, io_u->buflen);
1536 log_err("Zone remainder %lld smaller than minimum block size %d\n",
1537 ((zb + 1)->start - io_u->offset),
1544 case DDIR_SYNC_FILE_RANGE:
1555 assert(zb->cond != ZBD_ZONE_COND_OFFLINE);
1556 assert(!io_u->zbd_queue_io);
1557 assert(!io_u->zbd_put_io);
1558 io_u->zbd_queue_io = zbd_queue_io;
1559 io_u->zbd_put_io = zbd_put_io;
1564 pthread_mutex_unlock(&zb->mutex);
1568 /* Return a string with ZBD statistics */
1569 char *zbd_write_status(const struct thread_stat *ts)
1573 if (asprintf(&res, "; %llu zone resets", (unsigned long long) ts->nr_zone_resets) < 0)