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_zone_info *z)
162 * Lock the io_u target zone. The zone will be unlocked if io_u offset
163 * is changed or when io_u completes and zbd_put_io() executed.
164 * To avoid multiple jobs doing asynchronous I/Os from deadlocking each
165 * other waiting for zone locks when building an io_u batch, first
166 * only trylock the zone. If the zone is already locked by another job,
167 * process the currently queued I/Os so that I/O progress is made and
170 if (pthread_mutex_trylock(&z->mutex) != 0) {
171 if (!td_ioengine_flagged(td, FIO_SYNCIO))
173 pthread_mutex_lock(&z->mutex);
177 static bool is_valid_offset(const struct fio_file *f, uint64_t offset)
179 return (uint64_t)(offset - f->file_offset) < f->io_size;
182 /* Verify whether direct I/O is used for all host-managed zoned drives. */
183 static bool zbd_using_direct_io(void)
185 struct thread_data *td;
190 if (td->o.odirect || !(td->o.td_ddir & TD_DDIR_WRITE))
192 for_each_file(td, f, j) {
194 f->zbd_info->model == ZBD_HOST_MANAGED)
202 /* Whether or not the I/O range for f includes one or more sequential zones */
203 static bool zbd_is_seq_job(struct fio_file *f)
205 uint32_t zone_idx, zone_idx_b, zone_idx_e;
210 zone_idx_b = zbd_zone_idx(f, f->file_offset);
211 zone_idx_e = zbd_zone_idx(f, f->file_offset + f->io_size - 1);
212 for (zone_idx = zone_idx_b; zone_idx <= zone_idx_e; zone_idx++)
213 if (zbd_zone_swr(&f->zbd_info->zone_info[zone_idx]))
220 * Verify whether offset and size parameters are aligned with zone boundaries.
222 static bool zbd_verify_sizes(void)
224 const struct fio_zone_info *z;
225 struct thread_data *td;
227 uint64_t new_offset, new_end;
232 for_each_file(td, f, j) {
235 if (f->file_offset >= f->real_file_size)
237 if (!zbd_is_seq_job(f))
240 if (!td->o.zone_size) {
241 td->o.zone_size = f->zbd_info->zone_size;
242 if (!td->o.zone_size) {
243 log_err("%s: invalid 0 zone size\n",
247 } else if (td->o.zone_size != f->zbd_info->zone_size) {
248 log_err("%s: job parameter zonesize %llu does not match disk zone size %llu.\n",
249 f->file_name, (unsigned long long) td->o.zone_size,
250 (unsigned long long) f->zbd_info->zone_size);
254 if (td->o.zone_skip &&
255 (td->o.zone_skip < td->o.zone_size ||
256 td->o.zone_skip % td->o.zone_size)) {
257 log_err("%s: zoneskip %llu is not a multiple of the device zone size %llu.\n",
258 f->file_name, (unsigned long long) td->o.zone_skip,
259 (unsigned long long) td->o.zone_size);
263 zone_idx = zbd_zone_idx(f, f->file_offset);
264 z = &f->zbd_info->zone_info[zone_idx];
265 if (f->file_offset != z->start) {
266 new_offset = (z+1)->start;
267 if (new_offset >= f->file_offset + f->io_size) {
268 log_info("%s: io_size must be at least one zone\n",
272 log_info("%s: rounded up offset from %llu to %llu\n",
273 f->file_name, (unsigned long long) f->file_offset,
274 (unsigned long long) new_offset);
275 f->io_size -= (new_offset - f->file_offset);
276 f->file_offset = new_offset;
278 zone_idx = zbd_zone_idx(f, f->file_offset + f->io_size);
279 z = &f->zbd_info->zone_info[zone_idx];
281 if (f->file_offset + f->io_size != new_end) {
282 if (new_end <= f->file_offset) {
283 log_info("%s: io_size must be at least one zone\n",
287 log_info("%s: rounded down io_size from %llu to %llu\n",
288 f->file_name, (unsigned long long) f->io_size,
289 (unsigned long long) new_end - f->file_offset);
290 f->io_size = new_end - f->file_offset;
298 static bool zbd_verify_bs(void)
300 struct thread_data *td;
306 for_each_file(td, f, j) {
309 zone_size = f->zbd_info->zone_size;
310 for (k = 0; k < ARRAY_SIZE(td->o.bs); k++) {
311 if (td->o.verify != VERIFY_NONE &&
312 zone_size % td->o.bs[k] != 0) {
313 log_info("%s: block size %llu is not a divisor of the zone size %d\n",
314 f->file_name, td->o.bs[k],
324 static int ilog2(uint64_t i)
336 * Initialize f->zbd_info for devices that are not zoned block devices. This
337 * allows to execute a ZBD workload against a non-ZBD device.
339 static int init_zone_info(struct thread_data *td, struct fio_file *f)
342 struct fio_zone_info *p;
343 uint64_t zone_size = td->o.zone_size;
344 struct zoned_block_device_info *zbd_info = NULL;
345 pthread_mutexattr_t attr;
348 if (zone_size == 0) {
349 log_err("%s: Specifying the zone size is mandatory for regular block devices with --zonemode=zbd\n\n",
354 if (zone_size < 512) {
355 log_err("%s: zone size must be at least 512 bytes for --zonemode=zbd\n\n",
360 nr_zones = (f->real_file_size + zone_size - 1) / zone_size;
361 zbd_info = scalloc(1, sizeof(*zbd_info) +
362 (nr_zones + 1) * sizeof(zbd_info->zone_info[0]));
366 pthread_mutexattr_init(&attr);
367 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
368 pthread_mutexattr_setpshared(&attr, true);
369 pthread_mutex_init(&zbd_info->mutex, &attr);
370 zbd_info->refcount = 1;
371 p = &zbd_info->zone_info[0];
372 for (i = 0; i < nr_zones; i++, p++) {
373 pthread_mutex_init(&p->mutex, &attr);
374 p->start = i * zone_size;
375 p->wp = p->start + zone_size;
376 p->type = ZBD_ZONE_TYPE_SWR;
377 p->cond = ZBD_ZONE_COND_EMPTY;
380 p->start = nr_zones * zone_size;
382 f->zbd_info = zbd_info;
383 f->zbd_info->zone_size = zone_size;
384 f->zbd_info->zone_size_log2 = is_power_of_2(zone_size) ?
385 ilog2(zone_size) : 0;
386 f->zbd_info->nr_zones = nr_zones;
387 pthread_mutexattr_destroy(&attr);
392 * Maximum number of zones to report in one operation.
394 #define ZBD_REPORT_MAX_ZONES 8192U
397 * Parse the device zone report and store it in f->zbd_info. Must be called
398 * only for devices that are zoned, namely those with a model != ZBD_NONE.
400 static int parse_zone_info(struct thread_data *td, struct fio_file *f)
403 struct zbd_zone *zones, *z;
404 struct fio_zone_info *p;
405 uint64_t zone_size, offset;
406 struct zoned_block_device_info *zbd_info = NULL;
407 pthread_mutexattr_t attr;
410 pthread_mutexattr_init(&attr);
411 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
412 pthread_mutexattr_setpshared(&attr, true);
414 zones = calloc(ZBD_REPORT_MAX_ZONES, sizeof(struct zbd_zone));
418 nrz = zbd_report_zones(td, f, 0, zones, ZBD_REPORT_MAX_ZONES);
421 log_info("fio: report zones (offset 0) failed for %s (%d).\n",
426 zone_size = zones[0].len;
427 nr_zones = (f->real_file_size + zone_size - 1) / zone_size;
429 if (td->o.zone_size == 0) {
430 td->o.zone_size = zone_size;
431 } else if (td->o.zone_size != zone_size) {
432 log_err("fio: %s job parameter zonesize %llu does not match disk zone size %llu.\n",
433 f->file_name, (unsigned long long) td->o.zone_size,
434 (unsigned long long) zone_size);
439 dprint(FD_ZBD, "Device %s has %d zones of size %llu KB\n", f->file_name,
440 nr_zones, (unsigned long long) zone_size / 1024);
442 zbd_info = scalloc(1, sizeof(*zbd_info) +
443 (nr_zones + 1) * sizeof(zbd_info->zone_info[0]));
447 pthread_mutex_init(&zbd_info->mutex, &attr);
448 zbd_info->refcount = 1;
449 p = &zbd_info->zone_info[0];
450 for (offset = 0, j = 0; j < nr_zones;) {
452 for (i = 0; i < nrz; i++, j++, z++, p++) {
453 pthread_mutex_init(&p->mutex, &attr);
456 case ZBD_ZONE_COND_NOT_WP:
457 case ZBD_ZONE_COND_FULL:
458 p->wp = p->start + zone_size;
461 assert(z->start <= z->wp);
462 assert(z->wp <= z->start + zone_size);
468 if (j > 0 && p->start != p[-1].start + zone_size) {
469 log_info("%s: invalid zone data\n",
476 offset = z->start + z->len;
479 nrz = zbd_report_zones(td, f, offset,
480 zones, ZBD_REPORT_MAX_ZONES);
483 log_info("fio: report zones (offset %llu) failed for %s (%d).\n",
484 (unsigned long long)offset,
491 zbd_info->zone_info[nr_zones].start = offset;
493 f->zbd_info = zbd_info;
494 f->zbd_info->zone_size = zone_size;
495 f->zbd_info->zone_size_log2 = is_power_of_2(zone_size) ?
496 ilog2(zone_size) : 0;
497 f->zbd_info->nr_zones = nr_zones;
504 pthread_mutexattr_destroy(&attr);
509 * Allocate zone information and store it into f->zbd_info if zonemode=zbd.
511 * Returns 0 upon success and a negative error code upon failure.
513 static int zbd_create_zone_info(struct thread_data *td, struct fio_file *f)
515 enum zbd_zoned_model zbd_model;
518 assert(td->o.zone_mode == ZONE_MODE_ZBD);
520 ret = zbd_get_zoned_model(td, f, &zbd_model);
528 case ZBD_HOST_MANAGED:
529 ret = parse_zone_info(td, f);
532 ret = init_zone_info(td, f);
535 td_verror(td, EINVAL, "Unsupported zoned model");
536 log_err("Unsupported zoned model\n");
541 f->zbd_info->model = zbd_model;
545 void zbd_free_zone_info(struct fio_file *f)
552 pthread_mutex_lock(&f->zbd_info->mutex);
553 refcount = --f->zbd_info->refcount;
554 pthread_mutex_unlock(&f->zbd_info->mutex);
556 assert((int32_t)refcount >= 0);
563 * Initialize f->zbd_info.
565 * Returns 0 upon success and a negative error code upon failure.
567 * Note: this function can only work correctly if it is called before the first
570 static int zbd_init_zone_info(struct thread_data *td, struct fio_file *file)
572 struct thread_data *td2;
576 for_each_td(td2, i) {
577 for_each_file(td2, f2, j) {
578 if (td2 == td && f2 == file)
581 strcmp(f2->file_name, file->file_name) != 0)
583 file->zbd_info = f2->zbd_info;
584 file->zbd_info->refcount++;
589 ret = zbd_create_zone_info(td, file);
591 td_verror(td, -ret, "zbd_create_zone_info() failed");
595 int zbd_init(struct thread_data *td)
600 for_each_file(td, f, i) {
601 if (zbd_init_zone_info(td, f))
605 if (!zbd_using_direct_io()) {
606 log_err("Using direct I/O is mandatory for writing to ZBD drives\n\n");
610 if (!zbd_verify_sizes())
613 if (!zbd_verify_bs())
620 * zbd_reset_range - reset zones for a range of sectors
621 * @td: FIO thread data.
622 * @f: Fio file for which to reset zones
623 * @sector: Starting sector in units of 512 bytes
624 * @nr_sectors: Number of sectors in units of 512 bytes
626 * Returns 0 upon success and a negative error code upon failure.
628 static int zbd_reset_range(struct thread_data *td, struct fio_file *f,
629 uint64_t offset, uint64_t length)
631 uint32_t zone_idx_b, zone_idx_e;
632 struct fio_zone_info *zb, *ze, *z;
635 assert(is_valid_offset(f, offset + length - 1));
637 switch (f->zbd_info->model) {
639 case ZBD_HOST_MANAGED:
640 ret = zbd_reset_wp(td, f, offset, length);
648 zone_idx_b = zbd_zone_idx(f, offset);
649 zb = &f->zbd_info->zone_info[zone_idx_b];
650 zone_idx_e = zbd_zone_idx(f, offset + length);
651 ze = &f->zbd_info->zone_info[zone_idx_e];
652 for (z = zb; z < ze; z++) {
653 pthread_mutex_lock(&z->mutex);
654 pthread_mutex_lock(&f->zbd_info->mutex);
655 f->zbd_info->sectors_with_data -= z->wp - z->start;
656 pthread_mutex_unlock(&f->zbd_info->mutex);
659 pthread_mutex_unlock(&z->mutex);
662 td->ts.nr_zone_resets += ze - zb;
667 static unsigned int zbd_zone_nr(struct zoned_block_device_info *zbd_info,
668 struct fio_zone_info *zone)
670 return zone - zbd_info->zone_info;
674 * zbd_reset_zone - reset the write pointer of a single zone
675 * @td: FIO thread data.
676 * @f: FIO file associated with the disk for which to reset a write pointer.
679 * Returns 0 upon success and a negative error code upon failure.
681 static int zbd_reset_zone(struct thread_data *td, struct fio_file *f,
682 struct fio_zone_info *z)
684 dprint(FD_ZBD, "%s: resetting wp of zone %u.\n", f->file_name,
685 zbd_zone_nr(f->zbd_info, z));
687 return zbd_reset_range(td, f, z->start, (z+1)->start - z->start);
691 * Reset a range of zones. Returns 0 upon success and 1 upon failure.
692 * @td: fio thread data.
693 * @f: fio file for which to reset zones
694 * @zb: first zone to reset.
695 * @ze: first zone not to reset.
696 * @all_zones: whether to reset all zones or only those zones for which the
697 * write pointer is not a multiple of td->o.min_bs[DDIR_WRITE].
699 static int zbd_reset_zones(struct thread_data *td, struct fio_file *f,
700 struct fio_zone_info *const zb,
701 struct fio_zone_info *const ze, bool all_zones)
703 struct fio_zone_info *z;
704 const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
710 dprint(FD_ZBD, "%s: examining zones %u .. %u\n", f->file_name,
711 zbd_zone_nr(f->zbd_info, zb), zbd_zone_nr(f->zbd_info, ze));
712 for (z = zb; z < ze; z++) {
713 if (!zbd_zone_swr(z))
716 reset_wp = all_zones ? z->wp != z->start :
717 (td->o.td_ddir & TD_DDIR_WRITE) &&
720 dprint(FD_ZBD, "%s: resetting zone %u\n",
722 zbd_zone_nr(f->zbd_info, z));
723 if (zbd_reset_zone(td, f, z) < 0)
726 pthread_mutex_unlock(&z->mutex);
733 * Reset zbd_info.write_cnt, the counter that counts down towards the next
736 static void zbd_reset_write_cnt(const struct thread_data *td,
737 const struct fio_file *f)
739 assert(0 <= td->o.zrf.u.f && td->o.zrf.u.f <= 1);
741 pthread_mutex_lock(&f->zbd_info->mutex);
742 f->zbd_info->write_cnt = td->o.zrf.u.f ?
743 min(1.0 / td->o.zrf.u.f, 0.0 + UINT_MAX) : UINT_MAX;
744 pthread_mutex_unlock(&f->zbd_info->mutex);
747 static bool zbd_dec_and_reset_write_cnt(const struct thread_data *td,
748 const struct fio_file *f)
750 uint32_t write_cnt = 0;
752 pthread_mutex_lock(&f->zbd_info->mutex);
753 assert(f->zbd_info->write_cnt);
754 if (f->zbd_info->write_cnt)
755 write_cnt = --f->zbd_info->write_cnt;
757 zbd_reset_write_cnt(td, f);
758 pthread_mutex_unlock(&f->zbd_info->mutex);
760 return write_cnt == 0;
768 /* Calculate the number of sectors with data (swd) and perform action 'a' */
769 static uint64_t zbd_process_swd(const struct fio_file *f, enum swd_action a)
771 struct fio_zone_info *zb, *ze, *z;
774 zb = &f->zbd_info->zone_info[zbd_zone_idx(f, f->file_offset)];
775 ze = &f->zbd_info->zone_info[zbd_zone_idx(f, f->file_offset +
777 for (z = zb; z < ze; z++) {
778 pthread_mutex_lock(&z->mutex);
779 swd += z->wp - z->start;
781 pthread_mutex_lock(&f->zbd_info->mutex);
784 assert(f->zbd_info->sectors_with_data == swd);
787 f->zbd_info->sectors_with_data = swd;
790 pthread_mutex_unlock(&f->zbd_info->mutex);
791 for (z = zb; z < ze; z++)
792 pthread_mutex_unlock(&z->mutex);
798 * The swd check is useful for debugging but takes too much time to leave
799 * it enabled all the time. Hence it is disabled by default.
801 static const bool enable_check_swd = false;
803 /* Check whether the value of zbd_info.sectors_with_data is correct. */
804 static void zbd_check_swd(const struct fio_file *f)
806 if (!enable_check_swd)
809 zbd_process_swd(f, CHECK_SWD);
812 static void zbd_init_swd(struct fio_file *f)
816 if (!enable_check_swd)
819 swd = zbd_process_swd(f, SET_SWD);
820 dprint(FD_ZBD, "%s(%s): swd = %" PRIu64 "\n", __func__, f->file_name,
824 void zbd_file_reset(struct thread_data *td, struct fio_file *f)
826 struct fio_zone_info *zb, *ze;
832 zb = &f->zbd_info->zone_info[zbd_zone_idx(f, f->file_offset)];
833 zone_idx_e = zbd_zone_idx(f, f->file_offset + f->io_size);
834 ze = &f->zbd_info->zone_info[zone_idx_e];
837 * If data verification is enabled reset the affected zones before
838 * writing any data to avoid that a zone reset has to be issued while
839 * writing data, which causes data loss.
841 zbd_reset_zones(td, f, zb, ze, td->o.verify != VERIFY_NONE &&
842 (td->o.td_ddir & TD_DDIR_WRITE) &&
843 td->runstate != TD_VERIFYING);
844 zbd_reset_write_cnt(td, f);
847 /* The caller must hold f->zbd_info->mutex. */
848 static bool is_zone_open(const struct thread_data *td, const struct fio_file *f,
849 unsigned int zone_idx)
851 struct zoned_block_device_info *zbdi = f->zbd_info;
854 assert(td->o.max_open_zones <= ARRAY_SIZE(zbdi->open_zones));
855 assert(zbdi->num_open_zones <= td->o.max_open_zones);
857 for (i = 0; i < zbdi->num_open_zones; i++)
858 if (zbdi->open_zones[i] == zone_idx)
865 * Open a ZBD zone if it was not yet open. Returns true if either the zone was
866 * already open or if opening a new zone is allowed. Returns false if the zone
867 * was not yet open and opening a new zone would cause the zone limit to be
870 static bool zbd_open_zone(struct thread_data *td, const struct io_u *io_u,
873 const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
874 const struct fio_file *f = io_u->file;
875 struct fio_zone_info *z = &f->zbd_info->zone_info[zone_idx];
878 if (z->cond == ZBD_ZONE_COND_OFFLINE)
882 * Skip full zones with data verification enabled because resetting a
883 * zone causes data loss and hence causes verification to fail.
885 if (td->o.verify != VERIFY_NONE && zbd_zone_full(f, z, min_bs))
888 /* Zero means no limit */
889 if (!td->o.max_open_zones)
892 pthread_mutex_lock(&f->zbd_info->mutex);
893 if (is_zone_open(td, f, zone_idx))
896 if (f->zbd_info->num_open_zones >= td->o.max_open_zones)
898 dprint(FD_ZBD, "%s: opening zone %d\n", f->file_name, zone_idx);
899 f->zbd_info->open_zones[f->zbd_info->num_open_zones++] = zone_idx;
904 pthread_mutex_unlock(&f->zbd_info->mutex);
908 /* The caller must hold f->zbd_info->mutex */
909 static void zbd_close_zone(struct thread_data *td, const struct fio_file *f,
910 unsigned int open_zone_idx)
914 assert(open_zone_idx < f->zbd_info->num_open_zones);
915 zone_idx = f->zbd_info->open_zones[open_zone_idx];
916 memmove(f->zbd_info->open_zones + open_zone_idx,
917 f->zbd_info->open_zones + open_zone_idx + 1,
918 (ZBD_MAX_OPEN_ZONES - (open_zone_idx + 1)) *
919 sizeof(f->zbd_info->open_zones[0]));
920 f->zbd_info->num_open_zones--;
921 f->zbd_info->zone_info[zone_idx].open = 0;
924 /* Anything goes as long as it is not a constant. */
925 static uint32_t pick_random_zone_idx(const struct fio_file *f,
926 const struct io_u *io_u)
928 return io_u->offset * f->zbd_info->num_open_zones / f->real_file_size;
932 * Modify the offset of an I/O unit that does not refer to an open zone such
933 * that it refers to an open zone. Close an open zone and open a new zone if
934 * necessary. This algorithm can only work correctly if all write pointers are
935 * a multiple of the fio block size. The caller must neither hold z->mutex
936 * nor f->zbd_info->mutex. Returns with z->mutex held upon success.
938 static struct fio_zone_info *zbd_convert_to_open_zone(struct thread_data *td,
941 const uint32_t min_bs = td->o.min_bs[io_u->ddir];
942 const struct fio_file *f = io_u->file;
943 struct fio_zone_info *z;
944 unsigned int open_zone_idx = -1;
945 uint32_t zone_idx, new_zone_idx;
948 assert(is_valid_offset(f, io_u->offset));
950 if (td->o.max_open_zones) {
952 * This statement accesses f->zbd_info->open_zones[] on purpose
955 zone_idx = f->zbd_info->open_zones[pick_random_zone_idx(f, io_u)];
957 zone_idx = zbd_zone_idx(f, io_u->offset);
959 dprint(FD_ZBD, "%s(%s): starting from zone %d (offset %lld, buflen %lld)\n",
960 __func__, f->file_name, zone_idx, io_u->offset, io_u->buflen);
963 * Since z->mutex is the outer lock and f->zbd_info->mutex the inner
964 * lock it can happen that the state of the zone with index zone_idx
965 * has changed after 'z' has been assigned and before f->zbd_info->mutex
966 * has been obtained. Hence the loop.
971 z = &f->zbd_info->zone_info[zone_idx];
974 pthread_mutex_lock(&f->zbd_info->mutex);
975 if (td->o.max_open_zones == 0)
977 if (f->zbd_info->num_open_zones == 0) {
978 pthread_mutex_unlock(&f->zbd_info->mutex);
979 pthread_mutex_unlock(&z->mutex);
980 dprint(FD_ZBD, "%s(%s): no zones are open\n",
981 __func__, f->file_name);
986 * List of opened zones is per-device, shared across all threads.
987 * Start with quasi-random candidate zone.
988 * Ignore zones which don't belong to thread's offset/size area.
990 open_zone_idx = pick_random_zone_idx(f, io_u);
991 assert(open_zone_idx < f->zbd_info->num_open_zones);
992 tmp_idx = open_zone_idx;
993 for (i = 0; i < f->zbd_info->num_open_zones; i++) {
996 if (tmp_idx >= f->zbd_info->num_open_zones)
998 tmpz = f->zbd_info->open_zones[tmp_idx];
1000 if (is_valid_offset(f, f->zbd_info->zone_info[tmpz].start)) {
1001 open_zone_idx = tmp_idx;
1002 goto found_candidate_zone;
1008 dprint(FD_ZBD, "%s(%s): no candidate zone\n",
1009 __func__, f->file_name);
1010 pthread_mutex_unlock(&f->zbd_info->mutex);
1011 pthread_mutex_unlock(&z->mutex);
1014 found_candidate_zone:
1015 new_zone_idx = f->zbd_info->open_zones[open_zone_idx];
1016 if (new_zone_idx == zone_idx)
1018 zone_idx = new_zone_idx;
1019 pthread_mutex_unlock(&f->zbd_info->mutex);
1020 pthread_mutex_unlock(&z->mutex);
1023 /* Both z->mutex and f->zbd_info->mutex are held. */
1026 if (z->wp + min_bs <= (z+1)->start) {
1027 pthread_mutex_unlock(&f->zbd_info->mutex);
1030 dprint(FD_ZBD, "%s(%s): closing zone %d\n", __func__, f->file_name,
1032 if (td->o.max_open_zones)
1033 zbd_close_zone(td, f, open_zone_idx);
1034 pthread_mutex_unlock(&f->zbd_info->mutex);
1036 /* Only z->mutex is held. */
1038 /* Zone 'z' is full, so try to open a new zone. */
1039 for (i = f->io_size / f->zbd_info->zone_size; i > 0; i--) {
1041 pthread_mutex_unlock(&z->mutex);
1043 if (!is_valid_offset(f, z->start)) {
1045 zone_idx = zbd_zone_idx(f, f->file_offset);
1046 z = &f->zbd_info->zone_info[zone_idx];
1048 assert(is_valid_offset(f, z->start));
1052 if (zbd_open_zone(td, io_u, zone_idx))
1056 /* Only z->mutex is held. */
1058 /* Check whether the write fits in any of the already opened zones. */
1059 pthread_mutex_lock(&f->zbd_info->mutex);
1060 for (i = 0; i < f->zbd_info->num_open_zones; i++) {
1061 zone_idx = f->zbd_info->open_zones[i];
1062 pthread_mutex_unlock(&f->zbd_info->mutex);
1063 pthread_mutex_unlock(&z->mutex);
1065 z = &f->zbd_info->zone_info[zone_idx];
1068 if (z->wp + min_bs <= (z+1)->start)
1070 pthread_mutex_lock(&f->zbd_info->mutex);
1072 pthread_mutex_unlock(&f->zbd_info->mutex);
1073 pthread_mutex_unlock(&z->mutex);
1074 dprint(FD_ZBD, "%s(%s): did not open another zone\n", __func__,
1079 dprint(FD_ZBD, "%s(%s): returning zone %d\n", __func__, f->file_name,
1081 io_u->offset = z->start;
1085 /* The caller must hold z->mutex. */
1086 static struct fio_zone_info *zbd_replay_write_order(struct thread_data *td,
1088 struct fio_zone_info *z)
1090 const struct fio_file *f = io_u->file;
1091 const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
1093 if (!zbd_open_zone(td, io_u, z - f->zbd_info->zone_info)) {
1094 pthread_mutex_unlock(&z->mutex);
1095 z = zbd_convert_to_open_zone(td, io_u);
1099 if (z->verify_block * min_bs >= f->zbd_info->zone_size)
1100 log_err("%s: %d * %d >= %llu\n", f->file_name, z->verify_block,
1101 min_bs, (unsigned long long) f->zbd_info->zone_size);
1102 io_u->offset = z->start + z->verify_block++ * min_bs;
1107 * Find another zone for which @io_u fits below the write pointer. Start
1108 * searching in zones @zb + 1 .. @zl and continue searching in zones
1111 * Either returns NULL or returns a zone pointer and holds the mutex for that
1114 static struct fio_zone_info *
1115 zbd_find_zone(struct thread_data *td, struct io_u *io_u,
1116 struct fio_zone_info *zb, struct fio_zone_info *zl)
1118 const uint32_t min_bs = td->o.min_bs[io_u->ddir];
1119 const struct fio_file *f = io_u->file;
1120 struct fio_zone_info *z1, *z2;
1121 const struct fio_zone_info *const zf =
1122 &f->zbd_info->zone_info[zbd_zone_idx(f, f->file_offset)];
1125 * Skip to the next non-empty zone in case of sequential I/O and to
1126 * the nearest non-empty zone in case of random I/O.
1128 for (z1 = zb + 1, z2 = zb - 1; z1 < zl || z2 >= zf; z1++, z2--) {
1129 if (z1 < zl && z1->cond != ZBD_ZONE_COND_OFFLINE) {
1130 pthread_mutex_lock(&z1->mutex);
1131 if (z1->start + min_bs <= z1->wp)
1133 pthread_mutex_unlock(&z1->mutex);
1134 } else if (!td_random(td)) {
1137 if (td_random(td) && z2 >= zf &&
1138 z2->cond != ZBD_ZONE_COND_OFFLINE) {
1139 pthread_mutex_lock(&z2->mutex);
1140 if (z2->start + min_bs <= z2->wp)
1142 pthread_mutex_unlock(&z2->mutex);
1145 dprint(FD_ZBD, "%s: adjusting random read offset failed\n",
1151 * zbd_queue_io - update the write pointer of a sequential zone
1153 * @success: Whether or not the I/O unit has been queued successfully
1154 * @q: queueing status (busy, completed or queued).
1156 * For write and trim operations, update the write pointer of the I/O unit
1159 static void zbd_queue_io(struct io_u *io_u, int q, bool success)
1161 const struct fio_file *f = io_u->file;
1162 struct zoned_block_device_info *zbd_info = f->zbd_info;
1163 struct fio_zone_info *z;
1170 zone_idx = zbd_zone_idx(f, io_u->offset);
1171 assert(zone_idx < zbd_info->nr_zones);
1172 z = &zbd_info->zone_info[zone_idx];
1174 if (!zbd_zone_swr(z))
1181 "%s: queued I/O (%lld, %llu) for zone %u\n",
1182 f->file_name, io_u->offset, io_u->buflen, zone_idx);
1184 switch (io_u->ddir) {
1186 zone_end = min((uint64_t)(io_u->offset + io_u->buflen),
1188 pthread_mutex_lock(&zbd_info->mutex);
1190 * z->wp > zone_end means that one or more I/O errors
1193 if (z->wp <= zone_end)
1194 zbd_info->sectors_with_data += zone_end - z->wp;
1195 pthread_mutex_unlock(&zbd_info->mutex);
1199 assert(z->wp == z->start);
1206 if (!success || q != FIO_Q_QUEUED) {
1207 /* BUSY or COMPLETED: unlock the zone */
1208 pthread_mutex_unlock(&z->mutex);
1209 io_u->zbd_put_io = NULL;
1214 * zbd_put_io - Unlock an I/O unit target zone lock
1217 static void zbd_put_io(const struct io_u *io_u)
1219 const struct fio_file *f = io_u->file;
1220 struct zoned_block_device_info *zbd_info = f->zbd_info;
1221 struct fio_zone_info *z;
1228 zone_idx = zbd_zone_idx(f, io_u->offset);
1229 assert(zone_idx < zbd_info->nr_zones);
1230 z = &zbd_info->zone_info[zone_idx];
1232 if (!zbd_zone_swr(z))
1236 "%s: terminate I/O (%lld, %llu) for zone %u\n",
1237 f->file_name, io_u->offset, io_u->buflen, zone_idx);
1239 ret = pthread_mutex_unlock(&z->mutex);
1245 * Windows and MacOS do not define this.
1248 #define EREMOTEIO 121 /* POSIX value */
1251 bool zbd_unaligned_write(int error_code)
1253 switch (error_code) {
1262 * setup_zbd_zone_mode - handle zoneskip as necessary for ZBD drives
1263 * @td: FIO thread data.
1264 * @io_u: FIO I/O unit.
1266 * For sequential workloads, change the file offset to skip zoneskip bytes when
1267 * no more IO can be performed in the current zone.
1268 * - For read workloads, zoneskip is applied when the io has reached the end of
1269 * the zone or the zone write position (when td->o.read_beyond_wp is false).
1270 * - For write workloads, zoneskip is applied when the zone is full.
1271 * This applies only to read and write operations.
1273 void setup_zbd_zone_mode(struct thread_data *td, struct io_u *io_u)
1275 struct fio_file *f = io_u->file;
1276 enum fio_ddir ddir = io_u->ddir;
1277 struct fio_zone_info *z;
1280 assert(td->o.zone_mode == ZONE_MODE_ZBD);
1281 assert(td->o.zone_size);
1284 * zone_skip is valid only for sequential workloads.
1286 if (td_random(td) || !td->o.zone_skip)
1290 * It is time to switch to a new zone if:
1291 * - zone_bytes == zone_size bytes have already been accessed
1292 * - The last position reached the end of the current zone.
1293 * - For reads with td->o.read_beyond_wp == false, the last position
1294 * reached the zone write pointer.
1296 zone_idx = zbd_zone_idx(f, f->last_pos[ddir]);
1297 z = &f->zbd_info->zone_info[zone_idx];
1299 if (td->zone_bytes >= td->o.zone_size ||
1300 f->last_pos[ddir] >= (z+1)->start ||
1301 (ddir == DDIR_READ &&
1302 (!td->o.read_beyond_wp) && f->last_pos[ddir] >= z->wp)) {
1307 f->file_offset += td->o.zone_size + td->o.zone_skip;
1310 * Wrap from the beginning, if we exceed the file size
1312 if (f->file_offset >= f->real_file_size)
1313 f->file_offset = get_start_offset(td, f);
1315 f->last_pos[ddir] = f->file_offset;
1316 td->io_skip_bytes += td->o.zone_skip;
1321 * zbd_adjust_block - adjust the offset and length as necessary for ZBD drives
1322 * @td: FIO thread data.
1323 * @io_u: FIO I/O unit.
1325 * Locking strategy: returns with z->mutex locked if and only if z refers
1326 * to a sequential zone and if io_u_accept is returned. z is the zone that
1327 * corresponds to io_u->offset at the end of this function.
1329 enum io_u_action zbd_adjust_block(struct thread_data *td, struct io_u *io_u)
1331 struct fio_file *f = io_u->file;
1332 uint32_t zone_idx_b;
1333 struct fio_zone_info *zb, *zl, *orig_zb;
1334 uint32_t orig_len = io_u->buflen;
1335 uint32_t min_bs = td->o.min_bs[io_u->ddir];
1343 assert(is_valid_offset(f, io_u->offset));
1344 assert(io_u->buflen);
1345 zone_idx_b = zbd_zone_idx(f, io_u->offset);
1346 zb = &f->zbd_info->zone_info[zone_idx_b];
1349 /* Accept the I/O offset for conventional zones. */
1350 if (!zbd_zone_swr(zb))
1354 * In case read direction is chosen for the first random I/O, fio with
1355 * zonemode=zbd stops because no data can be read from zoned block
1356 * devices with all empty zones. Overwrite the first I/O direction as
1357 * write to make sure data to read exists.
1359 if (td_rw(td) && !f->zbd_info->sectors_with_data
1360 && !td->o.read_beyond_wp)
1361 io_u->ddir = DDIR_WRITE;
1364 * Accept the I/O offset for reads if reading beyond the write pointer
1367 if (zb->cond != ZBD_ZONE_COND_OFFLINE &&
1368 io_u->ddir == DDIR_READ && td->o.read_beyond_wp)
1375 switch (io_u->ddir) {
1377 if (td->runstate == TD_VERIFYING) {
1378 zb = zbd_replay_write_order(td, io_u, zb);
1382 * Check that there is enough written data in the zone to do an
1383 * I/O of at least min_bs B. If there isn't, find a new zone for
1386 range = zb->cond != ZBD_ZONE_COND_OFFLINE ?
1387 zb->wp - zb->start : 0;
1388 if (range < min_bs ||
1389 ((!td_random(td)) && (io_u->offset + min_bs > zb->wp))) {
1390 pthread_mutex_unlock(&zb->mutex);
1391 zl = &f->zbd_info->zone_info[zbd_zone_idx(f,
1392 f->file_offset + f->io_size)];
1393 zb = zbd_find_zone(td, io_u, zb, zl);
1396 "%s: zbd_find_zone(%lld, %llu) failed\n",
1397 f->file_name, io_u->offset,
1402 * zbd_find_zone() returned a zone with a range of at
1405 range = zb->wp - zb->start;
1406 assert(range >= min_bs);
1409 io_u->offset = zb->start;
1412 * Make sure the I/O is within the zone valid data range while
1413 * maximizing the I/O size and preserving randomness.
1415 if (range <= io_u->buflen)
1416 io_u->offset = zb->start;
1417 else if (td_random(td))
1418 io_u->offset = zb->start +
1419 ((io_u->offset - orig_zb->start) %
1420 (range - io_u->buflen)) / min_bs * min_bs;
1422 * Make sure the I/O does not cross over the zone wp position.
1424 new_len = min((unsigned long long)io_u->buflen,
1425 (unsigned long long)(zb->wp - io_u->offset));
1426 new_len = new_len / min_bs * min_bs;
1427 if (new_len < io_u->buflen) {
1428 io_u->buflen = new_len;
1429 dprint(FD_IO, "Changed length from %u into %llu\n",
1430 orig_len, io_u->buflen);
1432 assert(zb->start <= io_u->offset);
1433 assert(io_u->offset + io_u->buflen <= zb->wp);
1436 if (io_u->buflen > f->zbd_info->zone_size)
1438 if (!zbd_open_zone(td, io_u, zone_idx_b)) {
1439 pthread_mutex_unlock(&zb->mutex);
1440 zb = zbd_convert_to_open_zone(td, io_u);
1443 zone_idx_b = zb - f->zbd_info->zone_info;
1445 /* Check whether the zone reset threshold has been exceeded */
1446 if (td->o.zrf.u.f) {
1447 if (f->zbd_info->sectors_with_data >=
1448 f->io_size * td->o.zrt.u.f &&
1449 zbd_dec_and_reset_write_cnt(td, f)) {
1453 /* Reset the zone pointer if necessary */
1454 if (zb->reset_zone || zbd_zone_full(f, zb, min_bs)) {
1455 assert(td->o.verify == VERIFY_NONE);
1457 * Since previous write requests may have been submitted
1458 * asynchronously and since we will submit the zone
1459 * reset synchronously, wait until previously submitted
1460 * write requests have completed before issuing a
1465 if (zbd_reset_zone(td, f, zb) < 0)
1468 /* Make writes occur at the write pointer */
1469 assert(!zbd_zone_full(f, zb, min_bs));
1470 io_u->offset = zb->wp;
1471 if (!is_valid_offset(f, io_u->offset)) {
1472 dprint(FD_ZBD, "Dropped request with offset %llu\n",
1477 * Make sure that the buflen is a multiple of the minimal
1478 * block size. Give up if shrinking would make the request too
1481 new_len = min((unsigned long long)io_u->buflen,
1482 (zb + 1)->start - io_u->offset);
1483 new_len = new_len / min_bs * min_bs;
1484 if (new_len == io_u->buflen)
1486 if (new_len >= min_bs) {
1487 io_u->buflen = new_len;
1488 dprint(FD_IO, "Changed length from %u into %llu\n",
1489 orig_len, io_u->buflen);
1492 log_err("Zone remainder %lld smaller than minimum block size %d\n",
1493 ((zb + 1)->start - io_u->offset),
1500 case DDIR_SYNC_FILE_RANGE:
1511 assert(zb->cond != ZBD_ZONE_COND_OFFLINE);
1512 assert(!io_u->zbd_queue_io);
1513 assert(!io_u->zbd_put_io);
1514 io_u->zbd_queue_io = zbd_queue_io;
1515 io_u->zbd_put_io = zbd_put_io;
1520 pthread_mutex_unlock(&zb->mutex);
1524 /* Return a string with ZBD statistics */
1525 char *zbd_write_status(const struct thread_stat *ts)
1529 if (asprintf(&res, "; %llu zone resets", (unsigned long long) ts->nr_zone_resets) < 0)