2 * Copyright (C) 2018 Western Digital Corporation or its affiliates.
4 * This file is released under the GPL.
18 #include "oslib/asprintf.h"
24 * zbd_get_zoned_model - Get a device zoned model
25 * @td: FIO thread data
26 * @f: FIO file for which to get model information
28 int zbd_get_zoned_model(struct thread_data *td, struct fio_file *f,
29 enum zbd_zoned_model *model)
33 if (td->io_ops && td->io_ops->get_zoned_model)
34 ret = td->io_ops->get_zoned_model(td, f, model);
36 ret = blkzoned_get_zoned_model(td, f, model);
38 td_verror(td, errno, "get zoned model failed");
39 log_err("%s: get zoned model failed (%d).\n",
47 * zbd_report_zones - Get zone information
48 * @td: FIO thread data.
49 * @f: FIO file for which to get zone information
50 * @offset: offset from which to report zones
51 * @zones: Array of struct zbd_zone
52 * @nr_zones: Size of @zones array
54 * Get zone information into @zones starting from the zone at offset @offset
55 * for the device specified by @f.
57 * Returns the number of zones reported upon success and a negative error code
58 * upon failure. If the zone report is empty, always assume an error (device
59 * problem) and return -EIO.
61 int zbd_report_zones(struct thread_data *td, struct fio_file *f,
62 uint64_t offset, struct zbd_zone *zones,
63 unsigned int nr_zones)
67 if (td->io_ops && td->io_ops->report_zones)
68 ret = td->io_ops->report_zones(td, f, offset, zones, nr_zones);
70 ret = blkzoned_report_zones(td, f, offset, zones, nr_zones);
72 td_verror(td, errno, "report zones failed");
73 log_err("%s: report zones from sector %llu failed (%d).\n",
74 f->file_name, (unsigned long long)offset >> 9, errno);
75 } else if (ret == 0) {
76 td_verror(td, errno, "Empty zone report");
77 log_err("%s: report zones from sector %llu is empty.\n",
78 f->file_name, (unsigned long long)offset >> 9);
86 * zbd_reset_wp - reset the write pointer of a range of zones
87 * @td: FIO thread data.
88 * @f: FIO file for which to reset zones
89 * @offset: Starting offset of the first zone to reset
90 * @length: Length of the range of zones to reset
92 * Reset the write pointer of all zones in the range @offset...@offset+@length.
93 * Returns 0 upon success and a negative error code upon failure.
95 int zbd_reset_wp(struct thread_data *td, struct fio_file *f,
96 uint64_t offset, uint64_t length)
100 if (td->io_ops && td->io_ops->reset_wp)
101 ret = td->io_ops->reset_wp(td, f, offset, length);
103 ret = blkzoned_reset_wp(td, f, offset, length);
105 td_verror(td, errno, "resetting wp failed");
106 log_err("%s: resetting wp for %llu sectors at sector %llu failed (%d).\n",
107 f->file_name, (unsigned long long)length >> 9,
108 (unsigned long long)offset >> 9, errno);
115 * zbd_zone_idx - convert an offset into a zone number
117 * @offset: offset in bytes. If this offset is in the first zone_size bytes
118 * past the disk size then the index of the sentinel is returned.
120 static uint32_t zbd_zone_idx(const struct fio_file *f, uint64_t offset)
124 if (f->zbd_info->zone_size_log2 > 0)
125 zone_idx = offset >> f->zbd_info->zone_size_log2;
127 zone_idx = offset / f->zbd_info->zone_size;
129 return min(zone_idx, f->zbd_info->nr_zones);
133 * zbd_zone_swr - Test whether a zone requires sequential writes
134 * @z: zone info pointer.
136 static inline bool zbd_zone_swr(struct fio_zone_info *z)
138 return z->type == ZBD_ZONE_TYPE_SWR;
142 * zbd_zone_full - verify whether a minimum number of bytes remain in a zone
144 * @z: zone info pointer.
145 * @required: minimum number of bytes that must remain in a zone.
147 * The caller must hold z->mutex.
149 static bool zbd_zone_full(const struct fio_file *f, struct fio_zone_info *z,
152 assert((required & 511) == 0);
154 return zbd_zone_swr(z) &&
155 z->wp + required > z->start + f->zbd_info->zone_size;
158 static void zone_lock(struct thread_data *td, struct fio_zone_info *z)
161 * Lock the io_u target zone. The zone will be unlocked if io_u offset
162 * is changed or when io_u completes and zbd_put_io() executed.
163 * To avoid multiple jobs doing asynchronous I/Os from deadlocking each
164 * other waiting for zone locks when building an io_u batch, first
165 * only trylock the zone. If the zone is already locked by another job,
166 * process the currently queued I/Os so that I/O progress is made and
169 if (pthread_mutex_trylock(&z->mutex) != 0) {
170 if (!td_ioengine_flagged(td, FIO_SYNCIO))
172 pthread_mutex_lock(&z->mutex);
176 static bool is_valid_offset(const struct fio_file *f, uint64_t offset)
178 return (uint64_t)(offset - f->file_offset) < f->io_size;
181 /* Verify whether direct I/O is used for all host-managed zoned drives. */
182 static bool zbd_using_direct_io(void)
184 struct thread_data *td;
189 if (td->o.odirect || !(td->o.td_ddir & TD_DDIR_WRITE))
191 for_each_file(td, f, j) {
193 f->zbd_info->model == ZBD_HOST_MANAGED)
201 /* Whether or not the I/O range for f includes one or more sequential zones */
202 static bool zbd_is_seq_job(struct fio_file *f)
204 uint32_t zone_idx, zone_idx_b, zone_idx_e;
209 zone_idx_b = zbd_zone_idx(f, f->file_offset);
210 zone_idx_e = zbd_zone_idx(f, f->file_offset + f->io_size - 1);
211 for (zone_idx = zone_idx_b; zone_idx <= zone_idx_e; zone_idx++)
212 if (zbd_zone_swr(&f->zbd_info->zone_info[zone_idx]))
219 * Verify whether offset and size parameters are aligned with zone boundaries.
221 static bool zbd_verify_sizes(void)
223 const struct fio_zone_info *z;
224 struct thread_data *td;
226 uint64_t new_offset, new_end;
231 for_each_file(td, f, j) {
234 if (f->file_offset >= f->real_file_size)
236 if (!zbd_is_seq_job(f))
239 if (!td->o.zone_size) {
240 td->o.zone_size = f->zbd_info->zone_size;
241 if (!td->o.zone_size) {
242 log_err("%s: invalid 0 zone size\n",
246 } else if (td->o.zone_size != f->zbd_info->zone_size) {
247 log_err("%s: job parameter zonesize %llu does not match disk zone size %llu.\n",
248 f->file_name, (unsigned long long) td->o.zone_size,
249 (unsigned long long) f->zbd_info->zone_size);
253 if (td->o.zone_skip &&
254 (td->o.zone_skip < td->o.zone_size ||
255 td->o.zone_skip % td->o.zone_size)) {
256 log_err("%s: zoneskip %llu is not a multiple of the device zone size %llu.\n",
257 f->file_name, (unsigned long long) td->o.zone_skip,
258 (unsigned long long) td->o.zone_size);
262 zone_idx = zbd_zone_idx(f, f->file_offset);
263 z = &f->zbd_info->zone_info[zone_idx];
264 if (f->file_offset != z->start) {
265 new_offset = (z+1)->start;
266 if (new_offset >= f->file_offset + f->io_size) {
267 log_info("%s: io_size must be at least one zone\n",
271 log_info("%s: rounded up offset from %llu to %llu\n",
272 f->file_name, (unsigned long long) f->file_offset,
273 (unsigned long long) new_offset);
274 f->io_size -= (new_offset - f->file_offset);
275 f->file_offset = new_offset;
277 zone_idx = zbd_zone_idx(f, f->file_offset + f->io_size);
278 z = &f->zbd_info->zone_info[zone_idx];
280 if (f->file_offset + f->io_size != new_end) {
281 if (new_end <= f->file_offset) {
282 log_info("%s: io_size must be at least one zone\n",
286 log_info("%s: rounded down io_size from %llu to %llu\n",
287 f->file_name, (unsigned long long) f->io_size,
288 (unsigned long long) new_end - f->file_offset);
289 f->io_size = new_end - f->file_offset;
297 static bool zbd_verify_bs(void)
299 struct thread_data *td;
305 for_each_file(td, f, j) {
308 zone_size = f->zbd_info->zone_size;
309 for (k = 0; k < ARRAY_SIZE(td->o.bs); k++) {
310 if (td->o.verify != VERIFY_NONE &&
311 zone_size % td->o.bs[k] != 0) {
312 log_info("%s: block size %llu is not a divisor of the zone size %d\n",
313 f->file_name, td->o.bs[k],
323 static int ilog2(uint64_t i)
335 * Initialize f->zbd_info for devices that are not zoned block devices. This
336 * allows to execute a ZBD workload against a non-ZBD device.
338 static int init_zone_info(struct thread_data *td, struct fio_file *f)
341 struct fio_zone_info *p;
342 uint64_t zone_size = td->o.zone_size;
343 struct zoned_block_device_info *zbd_info = NULL;
344 pthread_mutexattr_t attr;
347 if (zone_size == 0) {
348 log_err("%s: Specifying the zone size is mandatory for regular block devices with --zonemode=zbd\n\n",
353 if (zone_size < 512) {
354 log_err("%s: zone size must be at least 512 bytes for --zonemode=zbd\n\n",
359 nr_zones = (f->real_file_size + zone_size - 1) / zone_size;
360 zbd_info = scalloc(1, sizeof(*zbd_info) +
361 (nr_zones + 1) * sizeof(zbd_info->zone_info[0]));
365 pthread_mutexattr_init(&attr);
366 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
367 pthread_mutexattr_setpshared(&attr, true);
368 pthread_mutex_init(&zbd_info->mutex, &attr);
369 zbd_info->refcount = 1;
370 p = &zbd_info->zone_info[0];
371 for (i = 0; i < nr_zones; i++, p++) {
372 pthread_mutex_init(&p->mutex, &attr);
373 p->start = i * zone_size;
374 p->wp = p->start + zone_size;
375 p->type = ZBD_ZONE_TYPE_SWR;
376 p->cond = ZBD_ZONE_COND_EMPTY;
379 p->start = nr_zones * zone_size;
381 f->zbd_info = zbd_info;
382 f->zbd_info->zone_size = zone_size;
383 f->zbd_info->zone_size_log2 = is_power_of_2(zone_size) ?
384 ilog2(zone_size) : 0;
385 f->zbd_info->nr_zones = nr_zones;
386 pthread_mutexattr_destroy(&attr);
391 * Maximum number of zones to report in one operation.
393 #define ZBD_REPORT_MAX_ZONES 8192U
396 * Parse the device zone report and store it in f->zbd_info. Must be called
397 * only for devices that are zoned, namely those with a model != ZBD_NONE.
399 static int parse_zone_info(struct thread_data *td, struct fio_file *f)
402 struct zbd_zone *zones, *z;
403 struct fio_zone_info *p;
404 uint64_t zone_size, offset;
405 struct zoned_block_device_info *zbd_info = NULL;
406 pthread_mutexattr_t attr;
409 pthread_mutexattr_init(&attr);
410 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
411 pthread_mutexattr_setpshared(&attr, true);
413 zones = calloc(ZBD_REPORT_MAX_ZONES, sizeof(struct zbd_zone));
417 nrz = zbd_report_zones(td, f, 0, zones, ZBD_REPORT_MAX_ZONES);
420 log_info("fio: report zones (offset 0) failed for %s (%d).\n",
425 zone_size = zones[0].len;
426 nr_zones = (f->real_file_size + zone_size - 1) / zone_size;
428 if (td->o.zone_size == 0) {
429 td->o.zone_size = zone_size;
430 } else if (td->o.zone_size != zone_size) {
431 log_err("fio: %s job parameter zonesize %llu does not match disk zone size %llu.\n",
432 f->file_name, (unsigned long long) td->o.zone_size,
433 (unsigned long long) zone_size);
438 dprint(FD_ZBD, "Device %s has %d zones of size %llu KB\n", f->file_name,
439 nr_zones, (unsigned long long) zone_size / 1024);
441 zbd_info = scalloc(1, sizeof(*zbd_info) +
442 (nr_zones + 1) * sizeof(zbd_info->zone_info[0]));
446 pthread_mutex_init(&zbd_info->mutex, &attr);
447 zbd_info->refcount = 1;
448 p = &zbd_info->zone_info[0];
449 for (offset = 0, j = 0; j < nr_zones;) {
451 for (i = 0; i < nrz; i++, j++, z++, p++) {
452 pthread_mutex_init(&p->mutex, &attr);
455 case ZBD_ZONE_COND_NOT_WP:
456 case ZBD_ZONE_COND_FULL:
457 p->wp = p->start + zone_size;
460 assert(z->start <= z->wp);
461 assert(z->wp <= z->start + zone_size);
467 if (j > 0 && p->start != p[-1].start + zone_size) {
468 log_info("%s: invalid zone data\n",
475 offset = z->start + z->len;
478 nrz = zbd_report_zones(td, f, offset,
479 zones, ZBD_REPORT_MAX_ZONES);
482 log_info("fio: report zones (offset %llu) failed for %s (%d).\n",
483 (unsigned long long)offset,
490 zbd_info->zone_info[nr_zones].start = offset;
492 f->zbd_info = zbd_info;
493 f->zbd_info->zone_size = zone_size;
494 f->zbd_info->zone_size_log2 = is_power_of_2(zone_size) ?
495 ilog2(zone_size) : 0;
496 f->zbd_info->nr_zones = nr_zones;
503 pthread_mutexattr_destroy(&attr);
508 * Allocate zone information and store it into f->zbd_info if zonemode=zbd.
510 * Returns 0 upon success and a negative error code upon failure.
512 static int zbd_create_zone_info(struct thread_data *td, struct fio_file *f)
514 enum zbd_zoned_model zbd_model;
517 assert(td->o.zone_mode == ZONE_MODE_ZBD);
519 ret = zbd_get_zoned_model(td, f, &zbd_model);
527 case ZBD_HOST_MANAGED:
528 ret = parse_zone_info(td, f);
531 ret = init_zone_info(td, f);
534 td_verror(td, EINVAL, "Unsupported zoned model");
535 log_err("Unsupported zoned model\n");
540 f->zbd_info->model = zbd_model;
544 void zbd_free_zone_info(struct fio_file *f)
551 pthread_mutex_lock(&f->zbd_info->mutex);
552 refcount = --f->zbd_info->refcount;
553 pthread_mutex_unlock(&f->zbd_info->mutex);
555 assert((int32_t)refcount >= 0);
562 * Initialize f->zbd_info.
564 * Returns 0 upon success and a negative error code upon failure.
566 * Note: this function can only work correctly if it is called before the first
569 static int zbd_init_zone_info(struct thread_data *td, struct fio_file *file)
571 struct thread_data *td2;
575 for_each_td(td2, i) {
576 for_each_file(td2, f2, j) {
577 if (td2 == td && f2 == file)
580 strcmp(f2->file_name, file->file_name) != 0)
582 file->zbd_info = f2->zbd_info;
583 file->zbd_info->refcount++;
588 ret = zbd_create_zone_info(td, file);
590 td_verror(td, -ret, "zbd_create_zone_info() failed");
594 int zbd_init(struct thread_data *td)
599 for_each_file(td, f, i) {
600 if (zbd_init_zone_info(td, f))
604 if (!zbd_using_direct_io()) {
605 log_err("Using direct I/O is mandatory for writing to ZBD drives\n\n");
609 if (!zbd_verify_sizes())
612 if (!zbd_verify_bs())
619 * zbd_reset_range - reset zones for a range of sectors
620 * @td: FIO thread data.
621 * @f: Fio file for which to reset zones
622 * @sector: Starting sector in units of 512 bytes
623 * @nr_sectors: Number of sectors in units of 512 bytes
625 * Returns 0 upon success and a negative error code upon failure.
627 static int zbd_reset_range(struct thread_data *td, struct fio_file *f,
628 uint64_t offset, uint64_t length)
630 uint32_t zone_idx_b, zone_idx_e;
631 struct fio_zone_info *zb, *ze, *z;
634 assert(is_valid_offset(f, offset + length - 1));
636 switch (f->zbd_info->model) {
638 case ZBD_HOST_MANAGED:
639 ret = zbd_reset_wp(td, f, offset, length);
647 zone_idx_b = zbd_zone_idx(f, offset);
648 zb = &f->zbd_info->zone_info[zone_idx_b];
649 zone_idx_e = zbd_zone_idx(f, offset + length);
650 ze = &f->zbd_info->zone_info[zone_idx_e];
651 for (z = zb; z < ze; z++) {
652 pthread_mutex_lock(&z->mutex);
653 pthread_mutex_lock(&f->zbd_info->mutex);
654 f->zbd_info->sectors_with_data -= z->wp - z->start;
655 pthread_mutex_unlock(&f->zbd_info->mutex);
658 pthread_mutex_unlock(&z->mutex);
661 td->ts.nr_zone_resets += ze - zb;
666 static unsigned int zbd_zone_nr(struct zoned_block_device_info *zbd_info,
667 struct fio_zone_info *zone)
669 return zone - zbd_info->zone_info;
673 * zbd_reset_zone - reset the write pointer of a single zone
674 * @td: FIO thread data.
675 * @f: FIO file associated with the disk for which to reset a write pointer.
678 * Returns 0 upon success and a negative error code upon failure.
680 static int zbd_reset_zone(struct thread_data *td, struct fio_file *f,
681 struct fio_zone_info *z)
683 dprint(FD_ZBD, "%s: resetting wp of zone %u.\n", f->file_name,
684 zbd_zone_nr(f->zbd_info, z));
686 return zbd_reset_range(td, f, z->start, (z+1)->start - z->start);
690 * Reset a range of zones. Returns 0 upon success and 1 upon failure.
691 * @td: fio thread data.
692 * @f: fio file for which to reset zones
693 * @zb: first zone to reset.
694 * @ze: first zone not to reset.
695 * @all_zones: whether to reset all zones or only those zones for which the
696 * write pointer is not a multiple of td->o.min_bs[DDIR_WRITE].
698 static int zbd_reset_zones(struct thread_data *td, struct fio_file *f,
699 struct fio_zone_info *const zb,
700 struct fio_zone_info *const ze, bool all_zones)
702 struct fio_zone_info *z;
703 const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
707 dprint(FD_ZBD, "%s: examining zones %u .. %u\n", f->file_name,
708 zbd_zone_nr(f->zbd_info, zb), zbd_zone_nr(f->zbd_info, ze));
709 for (z = zb; z < ze; z++) {
710 if (!zbd_zone_swr(z))
713 reset_wp = all_zones ? z->wp != z->start :
714 (td->o.td_ddir & TD_DDIR_WRITE) &&
717 dprint(FD_ZBD, "%s: resetting zone %u\n",
719 zbd_zone_nr(f->zbd_info, z));
720 if (zbd_reset_zone(td, f, z) < 0)
723 pthread_mutex_unlock(&z->mutex);
730 * Reset zbd_info.write_cnt, the counter that counts down towards the next
733 static void zbd_reset_write_cnt(const struct thread_data *td,
734 const struct fio_file *f)
736 assert(0 <= td->o.zrf.u.f && td->o.zrf.u.f <= 1);
738 pthread_mutex_lock(&f->zbd_info->mutex);
739 f->zbd_info->write_cnt = td->o.zrf.u.f ?
740 min(1.0 / td->o.zrf.u.f, 0.0 + UINT_MAX) : UINT_MAX;
741 pthread_mutex_unlock(&f->zbd_info->mutex);
744 static bool zbd_dec_and_reset_write_cnt(const struct thread_data *td,
745 const struct fio_file *f)
747 uint32_t write_cnt = 0;
749 pthread_mutex_lock(&f->zbd_info->mutex);
750 assert(f->zbd_info->write_cnt);
751 if (f->zbd_info->write_cnt)
752 write_cnt = --f->zbd_info->write_cnt;
754 zbd_reset_write_cnt(td, f);
755 pthread_mutex_unlock(&f->zbd_info->mutex);
757 return write_cnt == 0;
765 /* Calculate the number of sectors with data (swd) and perform action 'a' */
766 static uint64_t zbd_process_swd(const struct fio_file *f, enum swd_action a)
768 struct fio_zone_info *zb, *ze, *z;
771 zb = &f->zbd_info->zone_info[zbd_zone_idx(f, f->file_offset)];
772 ze = &f->zbd_info->zone_info[zbd_zone_idx(f, f->file_offset +
774 for (z = zb; z < ze; z++) {
775 pthread_mutex_lock(&z->mutex);
776 swd += z->wp - z->start;
778 pthread_mutex_lock(&f->zbd_info->mutex);
781 assert(f->zbd_info->sectors_with_data == swd);
784 f->zbd_info->sectors_with_data = swd;
787 pthread_mutex_unlock(&f->zbd_info->mutex);
788 for (z = zb; z < ze; z++)
789 pthread_mutex_unlock(&z->mutex);
795 * The swd check is useful for debugging but takes too much time to leave
796 * it enabled all the time. Hence it is disabled by default.
798 static const bool enable_check_swd = false;
800 /* Check whether the value of zbd_info.sectors_with_data is correct. */
801 static void zbd_check_swd(const struct fio_file *f)
803 if (!enable_check_swd)
806 zbd_process_swd(f, CHECK_SWD);
809 static void zbd_init_swd(struct fio_file *f)
813 if (!enable_check_swd)
816 swd = zbd_process_swd(f, SET_SWD);
817 dprint(FD_ZBD, "%s(%s): swd = %" PRIu64 "\n", __func__, f->file_name,
821 void zbd_file_reset(struct thread_data *td, struct fio_file *f)
823 struct fio_zone_info *zb, *ze;
829 zb = &f->zbd_info->zone_info[zbd_zone_idx(f, f->file_offset)];
830 zone_idx_e = zbd_zone_idx(f, f->file_offset + f->io_size);
831 ze = &f->zbd_info->zone_info[zone_idx_e];
834 * If data verification is enabled reset the affected zones before
835 * writing any data to avoid that a zone reset has to be issued while
836 * writing data, which causes data loss.
838 zbd_reset_zones(td, f, zb, ze, td->o.verify != VERIFY_NONE &&
839 (td->o.td_ddir & TD_DDIR_WRITE) &&
840 td->runstate != TD_VERIFYING);
841 zbd_reset_write_cnt(td, f);
844 /* The caller must hold f->zbd_info->mutex. */
845 static bool is_zone_open(const struct thread_data *td, const struct fio_file *f,
846 unsigned int zone_idx)
848 struct zoned_block_device_info *zbdi = f->zbd_info;
851 assert(td->o.max_open_zones <= ARRAY_SIZE(zbdi->open_zones));
852 assert(zbdi->num_open_zones <= td->o.max_open_zones);
854 for (i = 0; i < zbdi->num_open_zones; i++)
855 if (zbdi->open_zones[i] == zone_idx)
862 * Open a ZBD zone if it was not yet open. Returns true if either the zone was
863 * already open or if opening a new zone is allowed. Returns false if the zone
864 * was not yet open and opening a new zone would cause the zone limit to be
867 static bool zbd_open_zone(struct thread_data *td, const struct io_u *io_u,
870 const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
871 const struct fio_file *f = io_u->file;
872 struct fio_zone_info *z = &f->zbd_info->zone_info[zone_idx];
875 if (z->cond == ZBD_ZONE_COND_OFFLINE)
879 * Skip full zones with data verification enabled because resetting a
880 * zone causes data loss and hence causes verification to fail.
882 if (td->o.verify != VERIFY_NONE && zbd_zone_full(f, z, min_bs))
885 /* Zero means no limit */
886 if (!td->o.max_open_zones)
889 pthread_mutex_lock(&f->zbd_info->mutex);
890 if (is_zone_open(td, f, zone_idx))
893 if (f->zbd_info->num_open_zones >= td->o.max_open_zones)
895 dprint(FD_ZBD, "%s: opening zone %d\n", f->file_name, zone_idx);
896 f->zbd_info->open_zones[f->zbd_info->num_open_zones++] = zone_idx;
901 pthread_mutex_unlock(&f->zbd_info->mutex);
905 /* The caller must hold f->zbd_info->mutex */
906 static void zbd_close_zone(struct thread_data *td, const struct fio_file *f,
907 unsigned int open_zone_idx)
911 assert(open_zone_idx < f->zbd_info->num_open_zones);
912 zone_idx = f->zbd_info->open_zones[open_zone_idx];
913 memmove(f->zbd_info->open_zones + open_zone_idx,
914 f->zbd_info->open_zones + open_zone_idx + 1,
915 (ZBD_MAX_OPEN_ZONES - (open_zone_idx + 1)) *
916 sizeof(f->zbd_info->open_zones[0]));
917 f->zbd_info->num_open_zones--;
918 f->zbd_info->zone_info[zone_idx].open = 0;
921 /* Anything goes as long as it is not a constant. */
922 static uint32_t pick_random_zone_idx(const struct fio_file *f,
923 const struct io_u *io_u)
925 return io_u->offset * f->zbd_info->num_open_zones / f->real_file_size;
929 * Modify the offset of an I/O unit that does not refer to an open zone such
930 * that it refers to an open zone. Close an open zone and open a new zone if
931 * necessary. This algorithm can only work correctly if all write pointers are
932 * a multiple of the fio block size. The caller must neither hold z->mutex
933 * nor f->zbd_info->mutex. Returns with z->mutex held upon success.
935 static struct fio_zone_info *zbd_convert_to_open_zone(struct thread_data *td,
938 const uint32_t min_bs = td->o.min_bs[io_u->ddir];
939 const struct fio_file *f = io_u->file;
940 struct fio_zone_info *z;
941 unsigned int open_zone_idx = -1;
942 uint32_t zone_idx, new_zone_idx;
945 assert(is_valid_offset(f, io_u->offset));
947 if (td->o.max_open_zones) {
949 * This statement accesses f->zbd_info->open_zones[] on purpose
952 zone_idx = f->zbd_info->open_zones[pick_random_zone_idx(f, io_u)];
954 zone_idx = zbd_zone_idx(f, io_u->offset);
956 dprint(FD_ZBD, "%s(%s): starting from zone %d (offset %lld, buflen %lld)\n",
957 __func__, f->file_name, zone_idx, io_u->offset, io_u->buflen);
960 * Since z->mutex is the outer lock and f->zbd_info->mutex the inner
961 * lock it can happen that the state of the zone with index zone_idx
962 * has changed after 'z' has been assigned and before f->zbd_info->mutex
963 * has been obtained. Hence the loop.
968 z = &f->zbd_info->zone_info[zone_idx];
971 pthread_mutex_lock(&f->zbd_info->mutex);
972 if (td->o.max_open_zones == 0)
974 if (f->zbd_info->num_open_zones == 0) {
975 pthread_mutex_unlock(&f->zbd_info->mutex);
976 pthread_mutex_unlock(&z->mutex);
977 dprint(FD_ZBD, "%s(%s): no zones are open\n",
978 __func__, f->file_name);
983 * List of opened zones is per-device, shared across all threads.
984 * Start with quasi-random candidate zone.
985 * Ignore zones which don't belong to thread's offset/size area.
987 open_zone_idx = pick_random_zone_idx(f, io_u);
988 assert(open_zone_idx < f->zbd_info->num_open_zones);
989 tmp_idx = open_zone_idx;
990 for (i = 0; i < f->zbd_info->num_open_zones; i++) {
993 if (tmp_idx >= f->zbd_info->num_open_zones)
995 tmpz = f->zbd_info->open_zones[tmp_idx];
997 if (is_valid_offset(f, f->zbd_info->zone_info[tmpz].start)) {
998 open_zone_idx = tmp_idx;
999 goto found_candidate_zone;
1005 dprint(FD_ZBD, "%s(%s): no candidate zone\n",
1006 __func__, f->file_name);
1009 found_candidate_zone:
1010 new_zone_idx = f->zbd_info->open_zones[open_zone_idx];
1011 if (new_zone_idx == zone_idx)
1013 zone_idx = new_zone_idx;
1014 pthread_mutex_unlock(&f->zbd_info->mutex);
1015 pthread_mutex_unlock(&z->mutex);
1018 /* Both z->mutex and f->zbd_info->mutex are held. */
1021 if (z->wp + min_bs <= (z+1)->start) {
1022 pthread_mutex_unlock(&f->zbd_info->mutex);
1025 dprint(FD_ZBD, "%s(%s): closing zone %d\n", __func__, f->file_name,
1027 if (td->o.max_open_zones)
1028 zbd_close_zone(td, f, open_zone_idx);
1029 pthread_mutex_unlock(&f->zbd_info->mutex);
1031 /* Only z->mutex is held. */
1033 /* Zone 'z' is full, so try to open a new zone. */
1034 for (i = f->io_size / f->zbd_info->zone_size; i > 0; i--) {
1036 pthread_mutex_unlock(&z->mutex);
1038 if (!is_valid_offset(f, z->start)) {
1040 zone_idx = zbd_zone_idx(f, f->file_offset);
1041 z = &f->zbd_info->zone_info[zone_idx];
1043 assert(is_valid_offset(f, z->start));
1047 if (zbd_open_zone(td, io_u, zone_idx))
1051 /* Only z->mutex is held. */
1053 /* Check whether the write fits in any of the already opened zones. */
1054 pthread_mutex_lock(&f->zbd_info->mutex);
1055 for (i = 0; i < f->zbd_info->num_open_zones; i++) {
1056 zone_idx = f->zbd_info->open_zones[i];
1057 pthread_mutex_unlock(&f->zbd_info->mutex);
1058 pthread_mutex_unlock(&z->mutex);
1060 z = &f->zbd_info->zone_info[zone_idx];
1063 if (z->wp + min_bs <= (z+1)->start)
1065 pthread_mutex_lock(&f->zbd_info->mutex);
1067 pthread_mutex_unlock(&f->zbd_info->mutex);
1068 pthread_mutex_unlock(&z->mutex);
1069 dprint(FD_ZBD, "%s(%s): did not open another zone\n", __func__,
1074 dprint(FD_ZBD, "%s(%s): returning zone %d\n", __func__, f->file_name,
1076 io_u->offset = z->start;
1080 /* The caller must hold z->mutex. */
1081 static struct fio_zone_info *zbd_replay_write_order(struct thread_data *td,
1083 struct fio_zone_info *z)
1085 const struct fio_file *f = io_u->file;
1086 const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
1088 if (!zbd_open_zone(td, io_u, z - f->zbd_info->zone_info)) {
1089 pthread_mutex_unlock(&z->mutex);
1090 z = zbd_convert_to_open_zone(td, io_u);
1094 if (z->verify_block * min_bs >= f->zbd_info->zone_size)
1095 log_err("%s: %d * %d >= %llu\n", f->file_name, z->verify_block,
1096 min_bs, (unsigned long long) f->zbd_info->zone_size);
1097 io_u->offset = z->start + z->verify_block++ * min_bs;
1102 * Find another zone for which @io_u fits below the write pointer. Start
1103 * searching in zones @zb + 1 .. @zl and continue searching in zones
1106 * Either returns NULL or returns a zone pointer and holds the mutex for that
1109 static struct fio_zone_info *
1110 zbd_find_zone(struct thread_data *td, struct io_u *io_u,
1111 struct fio_zone_info *zb, struct fio_zone_info *zl)
1113 const uint32_t min_bs = td->o.min_bs[io_u->ddir];
1114 const struct fio_file *f = io_u->file;
1115 struct fio_zone_info *z1, *z2;
1116 const struct fio_zone_info *const zf =
1117 &f->zbd_info->zone_info[zbd_zone_idx(f, f->file_offset)];
1120 * Skip to the next non-empty zone in case of sequential I/O and to
1121 * the nearest non-empty zone in case of random I/O.
1123 for (z1 = zb + 1, z2 = zb - 1; z1 < zl || z2 >= zf; z1++, z2--) {
1124 if (z1 < zl && z1->cond != ZBD_ZONE_COND_OFFLINE) {
1125 pthread_mutex_lock(&z1->mutex);
1126 if (z1->start + min_bs <= z1->wp)
1128 pthread_mutex_unlock(&z1->mutex);
1129 } else if (!td_random(td)) {
1132 if (td_random(td) && z2 >= zf &&
1133 z2->cond != ZBD_ZONE_COND_OFFLINE) {
1134 pthread_mutex_lock(&z2->mutex);
1135 if (z2->start + min_bs <= z2->wp)
1137 pthread_mutex_unlock(&z2->mutex);
1140 dprint(FD_ZBD, "%s: adjusting random read offset failed\n",
1146 * zbd_queue_io - update the write pointer of a sequential zone
1148 * @success: Whether or not the I/O unit has been queued successfully
1149 * @q: queueing status (busy, completed or queued).
1151 * For write and trim operations, update the write pointer of the I/O unit
1154 static void zbd_queue_io(struct io_u *io_u, int q, bool success)
1156 const struct fio_file *f = io_u->file;
1157 struct zoned_block_device_info *zbd_info = f->zbd_info;
1158 struct fio_zone_info *z;
1165 zone_idx = zbd_zone_idx(f, io_u->offset);
1166 assert(zone_idx < zbd_info->nr_zones);
1167 z = &zbd_info->zone_info[zone_idx];
1169 if (!zbd_zone_swr(z))
1176 "%s: queued I/O (%lld, %llu) for zone %u\n",
1177 f->file_name, io_u->offset, io_u->buflen, zone_idx);
1179 switch (io_u->ddir) {
1181 zone_end = min((uint64_t)(io_u->offset + io_u->buflen),
1183 pthread_mutex_lock(&zbd_info->mutex);
1185 * z->wp > zone_end means that one or more I/O errors
1188 if (z->wp <= zone_end)
1189 zbd_info->sectors_with_data += zone_end - z->wp;
1190 pthread_mutex_unlock(&zbd_info->mutex);
1194 assert(z->wp == z->start);
1201 if (!success || q != FIO_Q_QUEUED) {
1202 /* BUSY or COMPLETED: unlock the zone */
1203 pthread_mutex_unlock(&z->mutex);
1204 io_u->zbd_put_io = NULL;
1209 * zbd_put_io - Unlock an I/O unit target zone lock
1212 static void zbd_put_io(const struct io_u *io_u)
1214 const struct fio_file *f = io_u->file;
1215 struct zoned_block_device_info *zbd_info = f->zbd_info;
1216 struct fio_zone_info *z;
1222 zone_idx = zbd_zone_idx(f, io_u->offset);
1223 assert(zone_idx < zbd_info->nr_zones);
1224 z = &zbd_info->zone_info[zone_idx];
1226 if (!zbd_zone_swr(z))
1230 "%s: terminate I/O (%lld, %llu) for zone %u\n",
1231 f->file_name, io_u->offset, io_u->buflen, zone_idx);
1233 assert(pthread_mutex_unlock(&z->mutex) == 0);
1238 * Windows and MacOS do not define this.
1241 #define EREMOTEIO 121 /* POSIX value */
1244 bool zbd_unaligned_write(int error_code)
1246 switch (error_code) {
1255 * setup_zbd_zone_mode - handle zoneskip as necessary for ZBD drives
1256 * @td: FIO thread data.
1257 * @io_u: FIO I/O unit.
1259 * For sequential workloads, change the file offset to skip zoneskip bytes when
1260 * no more IO can be performed in the current zone.
1261 * - For read workloads, zoneskip is applied when the io has reached the end of
1262 * the zone or the zone write position (when td->o.read_beyond_wp is false).
1263 * - For write workloads, zoneskip is applied when the zone is full.
1264 * This applies only to read and write operations.
1266 void setup_zbd_zone_mode(struct thread_data *td, struct io_u *io_u)
1268 struct fio_file *f = io_u->file;
1269 enum fio_ddir ddir = io_u->ddir;
1270 struct fio_zone_info *z;
1273 assert(td->o.zone_mode == ZONE_MODE_ZBD);
1274 assert(td->o.zone_size);
1277 * zone_skip is valid only for sequential workloads.
1279 if (td_random(td) || !td->o.zone_skip)
1283 * It is time to switch to a new zone if:
1284 * - zone_bytes == zone_size bytes have already been accessed
1285 * - The last position reached the end of the current zone.
1286 * - For reads with td->o.read_beyond_wp == false, the last position
1287 * reached the zone write pointer.
1289 zone_idx = zbd_zone_idx(f, f->last_pos[ddir]);
1290 z = &f->zbd_info->zone_info[zone_idx];
1292 if (td->zone_bytes >= td->o.zone_size ||
1293 f->last_pos[ddir] >= (z+1)->start ||
1294 (ddir == DDIR_READ &&
1295 (!td->o.read_beyond_wp) && f->last_pos[ddir] >= z->wp)) {
1300 f->file_offset += td->o.zone_size + td->o.zone_skip;
1303 * Wrap from the beginning, if we exceed the file size
1305 if (f->file_offset >= f->real_file_size)
1306 f->file_offset = get_start_offset(td, f);
1308 f->last_pos[ddir] = f->file_offset;
1309 td->io_skip_bytes += td->o.zone_skip;
1314 * zbd_adjust_block - adjust the offset and length as necessary for ZBD drives
1315 * @td: FIO thread data.
1316 * @io_u: FIO I/O unit.
1318 * Locking strategy: returns with z->mutex locked if and only if z refers
1319 * to a sequential zone and if io_u_accept is returned. z is the zone that
1320 * corresponds to io_u->offset at the end of this function.
1322 enum io_u_action zbd_adjust_block(struct thread_data *td, struct io_u *io_u)
1324 struct fio_file *f = io_u->file;
1325 uint32_t zone_idx_b;
1326 struct fio_zone_info *zb, *zl, *orig_zb;
1327 uint32_t orig_len = io_u->buflen;
1328 uint32_t min_bs = td->o.min_bs[io_u->ddir];
1335 assert(is_valid_offset(f, io_u->offset));
1336 assert(io_u->buflen);
1337 zone_idx_b = zbd_zone_idx(f, io_u->offset);
1338 zb = &f->zbd_info->zone_info[zone_idx_b];
1341 /* Accept the I/O offset for conventional zones. */
1342 if (!zbd_zone_swr(zb))
1346 * Accept the I/O offset for reads if reading beyond the write pointer
1349 if (zb->cond != ZBD_ZONE_COND_OFFLINE &&
1350 io_u->ddir == DDIR_READ && td->o.read_beyond_wp)
1357 switch (io_u->ddir) {
1359 if (td->runstate == TD_VERIFYING) {
1360 zb = zbd_replay_write_order(td, io_u, zb);
1364 * Check that there is enough written data in the zone to do an
1365 * I/O of at least min_bs B. If there isn't, find a new zone for
1368 range = zb->cond != ZBD_ZONE_COND_OFFLINE ?
1369 zb->wp - zb->start : 0;
1370 if (range < min_bs ||
1371 ((!td_random(td)) && (io_u->offset + min_bs > zb->wp))) {
1372 pthread_mutex_unlock(&zb->mutex);
1373 zl = &f->zbd_info->zone_info[zbd_zone_idx(f,
1374 f->file_offset + f->io_size)];
1375 zb = zbd_find_zone(td, io_u, zb, zl);
1378 "%s: zbd_find_zone(%lld, %llu) failed\n",
1379 f->file_name, io_u->offset,
1384 * zbd_find_zone() returned a zone with a range of at
1387 range = zb->wp - zb->start;
1388 assert(range >= min_bs);
1391 io_u->offset = zb->start;
1394 * Make sure the I/O is within the zone valid data range while
1395 * maximizing the I/O size and preserving randomness.
1397 if (range <= io_u->buflen)
1398 io_u->offset = zb->start;
1399 else if (td_random(td))
1400 io_u->offset = zb->start +
1401 ((io_u->offset - orig_zb->start) %
1402 (range - io_u->buflen)) / min_bs * min_bs;
1404 * Make sure the I/O does not cross over the zone wp position.
1406 new_len = min((unsigned long long)io_u->buflen,
1407 (unsigned long long)(zb->wp - io_u->offset));
1408 new_len = new_len / min_bs * min_bs;
1409 if (new_len < io_u->buflen) {
1410 io_u->buflen = new_len;
1411 dprint(FD_IO, "Changed length from %u into %llu\n",
1412 orig_len, io_u->buflen);
1414 assert(zb->start <= io_u->offset);
1415 assert(io_u->offset + io_u->buflen <= zb->wp);
1418 if (io_u->buflen > f->zbd_info->zone_size)
1420 if (!zbd_open_zone(td, io_u, zone_idx_b)) {
1421 pthread_mutex_unlock(&zb->mutex);
1422 zb = zbd_convert_to_open_zone(td, io_u);
1425 zone_idx_b = zb - f->zbd_info->zone_info;
1427 /* Check whether the zone reset threshold has been exceeded */
1428 if (td->o.zrf.u.f) {
1429 if (f->zbd_info->sectors_with_data >=
1430 f->io_size * td->o.zrt.u.f &&
1431 zbd_dec_and_reset_write_cnt(td, f)) {
1435 /* Reset the zone pointer if necessary */
1436 if (zb->reset_zone || zbd_zone_full(f, zb, min_bs)) {
1437 assert(td->o.verify == VERIFY_NONE);
1439 * Since previous write requests may have been submitted
1440 * asynchronously and since we will submit the zone
1441 * reset synchronously, wait until previously submitted
1442 * write requests have completed before issuing a
1447 if (zbd_reset_zone(td, f, zb) < 0)
1450 /* Make writes occur at the write pointer */
1451 assert(!zbd_zone_full(f, zb, min_bs));
1452 io_u->offset = zb->wp;
1453 if (!is_valid_offset(f, io_u->offset)) {
1454 dprint(FD_ZBD, "Dropped request with offset %llu\n",
1459 * Make sure that the buflen is a multiple of the minimal
1460 * block size. Give up if shrinking would make the request too
1463 new_len = min((unsigned long long)io_u->buflen,
1464 (zb + 1)->start - io_u->offset);
1465 new_len = new_len / min_bs * min_bs;
1466 if (new_len == io_u->buflen)
1468 if (new_len >= min_bs) {
1469 io_u->buflen = new_len;
1470 dprint(FD_IO, "Changed length from %u into %llu\n",
1471 orig_len, io_u->buflen);
1474 log_err("Zone remainder %lld smaller than minimum block size %d\n",
1475 ((zb + 1)->start - io_u->offset),
1482 case DDIR_SYNC_FILE_RANGE:
1493 assert(zb->cond != ZBD_ZONE_COND_OFFLINE);
1494 assert(!io_u->zbd_queue_io);
1495 assert(!io_u->zbd_put_io);
1496 io_u->zbd_queue_io = zbd_queue_io;
1497 io_u->zbd_put_io = zbd_put_io;
1502 pthread_mutex_unlock(&zb->mutex);
1506 /* Return a string with ZBD statistics */
1507 char *zbd_write_status(const struct thread_stat *ts)
1511 if (asprintf(&res, "; %llu zone resets", (unsigned long long) ts->nr_zone_resets) < 0)