2 * Copyright (C) 2018 Western Digital Corporation or its affiliates.
4 * This file is released under the GPL.
19 #include "oslib/asprintf.h"
26 * zbd_get_zoned_model - Get a device zoned model
27 * @td: FIO thread data
28 * @f: FIO file for which to get model information
30 int zbd_get_zoned_model(struct thread_data *td, struct fio_file *f,
31 enum zbd_zoned_model *model)
35 if (td->io_ops && td->io_ops->get_zoned_model)
36 ret = td->io_ops->get_zoned_model(td, f, model);
38 ret = blkzoned_get_zoned_model(td, f, model);
40 td_verror(td, errno, "get zoned model failed");
41 log_err("%s: get zoned model failed (%d).\n",
49 * zbd_report_zones - Get zone information
50 * @td: FIO thread data.
51 * @f: FIO file for which to get zone information
52 * @offset: offset from which to report zones
53 * @zones: Array of struct zbd_zone
54 * @nr_zones: Size of @zones array
56 * Get zone information into @zones starting from the zone at offset @offset
57 * for the device specified by @f.
59 * Returns the number of zones reported upon success and a negative error code
60 * upon failure. If the zone report is empty, always assume an error (device
61 * problem) and return -EIO.
63 int zbd_report_zones(struct thread_data *td, struct fio_file *f,
64 uint64_t offset, struct zbd_zone *zones,
65 unsigned int nr_zones)
69 if (td->io_ops && td->io_ops->report_zones)
70 ret = td->io_ops->report_zones(td, f, offset, zones, nr_zones);
72 ret = blkzoned_report_zones(td, f, offset, zones, nr_zones);
74 td_verror(td, errno, "report zones failed");
75 log_err("%s: report zones from sector %llu failed (%d).\n",
76 f->file_name, (unsigned long long)offset >> 9, errno);
77 } else if (ret == 0) {
78 td_verror(td, errno, "Empty zone report");
79 log_err("%s: report zones from sector %llu is empty.\n",
80 f->file_name, (unsigned long long)offset >> 9);
88 * zbd_reset_wp - reset the write pointer of a range of zones
89 * @td: FIO thread data.
90 * @f: FIO file for which to reset zones
91 * @offset: Starting offset of the first zone to reset
92 * @length: Length of the range of zones to reset
94 * Reset the write pointer of all zones in the range @offset...@offset+@length.
95 * Returns 0 upon success and a negative error code upon failure.
97 int zbd_reset_wp(struct thread_data *td, struct fio_file *f,
98 uint64_t offset, uint64_t length)
102 if (td->io_ops && td->io_ops->reset_wp)
103 ret = td->io_ops->reset_wp(td, f, offset, length);
105 ret = blkzoned_reset_wp(td, f, offset, length);
107 td_verror(td, errno, "resetting wp failed");
108 log_err("%s: resetting wp for %llu sectors at sector %llu failed (%d).\n",
109 f->file_name, (unsigned long long)length >> 9,
110 (unsigned long long)offset >> 9, errno);
117 * zbd_zone_idx - convert an offset into a zone number
119 * @offset: offset in bytes. If this offset is in the first zone_size bytes
120 * past the disk size then the index of the sentinel is returned.
122 static uint32_t zbd_zone_idx(const struct fio_file *f, uint64_t offset)
126 if (f->zbd_info->zone_size_log2 > 0)
127 zone_idx = offset >> f->zbd_info->zone_size_log2;
129 zone_idx = offset / f->zbd_info->zone_size;
131 return min(zone_idx, f->zbd_info->nr_zones);
135 * zbd_zone_swr - Test whether a zone requires sequential writes
136 * @z: zone info pointer.
138 static inline bool zbd_zone_swr(struct fio_zone_info *z)
140 return z->type == ZBD_ZONE_TYPE_SWR;
144 * zbd_zone_end - Return zone end location
145 * @z: zone info pointer.
147 static inline uint64_t zbd_zone_end(const struct fio_zone_info *z)
153 * zbd_zone_capacity_end - Return zone capacity limit end location
154 * @z: zone info pointer.
156 static inline uint64_t zbd_zone_capacity_end(const struct fio_zone_info *z)
158 return z->start + z->capacity;
162 * zbd_zone_full - verify whether a minimum number of bytes remain in a zone
164 * @z: zone info pointer.
165 * @required: minimum number of bytes that must remain in a zone.
167 * The caller must hold z->mutex.
169 static bool zbd_zone_full(const struct fio_file *f, struct fio_zone_info *z,
172 assert((required & 511) == 0);
174 return zbd_zone_swr(z) &&
175 z->wp + required > zbd_zone_capacity_end(z);
178 static void zone_lock(struct thread_data *td, struct fio_file *f, struct fio_zone_info *z)
180 struct zoned_block_device_info *zbd = f->zbd_info;
181 uint32_t nz = z - zbd->zone_info;
183 /* A thread should never lock zones outside its working area. */
184 assert(f->min_zone <= nz && nz < f->max_zone);
187 * Lock the io_u target zone. The zone will be unlocked if io_u offset
188 * is changed or when io_u completes and zbd_put_io() executed.
189 * To avoid multiple jobs doing asynchronous I/Os from deadlocking each
190 * other waiting for zone locks when building an io_u batch, first
191 * only trylock the zone. If the zone is already locked by another job,
192 * process the currently queued I/Os so that I/O progress is made and
195 if (pthread_mutex_trylock(&z->mutex) != 0) {
196 if (!td_ioengine_flagged(td, FIO_SYNCIO))
198 pthread_mutex_lock(&z->mutex);
202 static bool is_valid_offset(const struct fio_file *f, uint64_t offset)
204 return (uint64_t)(offset - f->file_offset) < f->io_size;
207 /* Verify whether direct I/O is used for all host-managed zoned drives. */
208 static bool zbd_using_direct_io(void)
210 struct thread_data *td;
215 if (td->o.odirect || !(td->o.td_ddir & TD_DDIR_WRITE))
217 for_each_file(td, f, j) {
219 f->zbd_info->model == ZBD_HOST_MANAGED)
227 /* Whether or not the I/O range for f includes one or more sequential zones */
228 static bool zbd_is_seq_job(struct fio_file *f)
230 uint32_t zone_idx, zone_idx_b, zone_idx_e;
235 zone_idx_b = zbd_zone_idx(f, f->file_offset);
236 zone_idx_e = zbd_zone_idx(f, f->file_offset + f->io_size - 1);
237 for (zone_idx = zone_idx_b; zone_idx <= zone_idx_e; zone_idx++)
238 if (zbd_zone_swr(&f->zbd_info->zone_info[zone_idx]))
245 * Verify whether offset and size parameters are aligned with zone boundaries.
247 static bool zbd_verify_sizes(void)
249 const struct fio_zone_info *z;
250 struct thread_data *td;
252 uint64_t new_offset, new_end;
257 for_each_file(td, f, j) {
260 if (f->file_offset >= f->real_file_size)
262 if (!zbd_is_seq_job(f))
265 if (!td->o.zone_size) {
266 td->o.zone_size = f->zbd_info->zone_size;
267 if (!td->o.zone_size) {
268 log_err("%s: invalid 0 zone size\n",
272 } else if (td->o.zone_size != f->zbd_info->zone_size) {
273 log_err("%s: job parameter zonesize %llu does not match disk zone size %llu.\n",
274 f->file_name, (unsigned long long) td->o.zone_size,
275 (unsigned long long) f->zbd_info->zone_size);
279 if (td->o.zone_skip &&
280 (td->o.zone_skip < td->o.zone_size ||
281 td->o.zone_skip % td->o.zone_size)) {
282 log_err("%s: zoneskip %llu is not a multiple of the device zone size %llu.\n",
283 f->file_name, (unsigned long long) td->o.zone_skip,
284 (unsigned long long) td->o.zone_size);
288 zone_idx = zbd_zone_idx(f, f->file_offset);
289 z = &f->zbd_info->zone_info[zone_idx];
290 if ((f->file_offset != z->start) &&
291 (td->o.td_ddir != TD_DDIR_READ)) {
292 new_offset = zbd_zone_end(z);
293 if (new_offset >= f->file_offset + f->io_size) {
294 log_info("%s: io_size must be at least one zone\n",
298 log_info("%s: rounded up offset from %llu to %llu\n",
299 f->file_name, (unsigned long long) f->file_offset,
300 (unsigned long long) new_offset);
301 f->io_size -= (new_offset - f->file_offset);
302 f->file_offset = new_offset;
304 zone_idx = zbd_zone_idx(f, f->file_offset + f->io_size);
305 z = &f->zbd_info->zone_info[zone_idx];
307 if ((td->o.td_ddir != TD_DDIR_READ) &&
308 (f->file_offset + f->io_size != new_end)) {
309 if (new_end <= f->file_offset) {
310 log_info("%s: io_size must be at least one zone\n",
314 log_info("%s: rounded down io_size from %llu to %llu\n",
315 f->file_name, (unsigned long long) f->io_size,
316 (unsigned long long) new_end - f->file_offset);
317 f->io_size = new_end - f->file_offset;
320 f->min_zone = zbd_zone_idx(f, f->file_offset);
321 f->max_zone = zbd_zone_idx(f, f->file_offset + f->io_size);
322 assert(f->min_zone < f->max_zone);
329 static bool zbd_verify_bs(void)
331 struct thread_data *td;
337 for_each_file(td, f, j) {
340 zone_size = f->zbd_info->zone_size;
341 for (k = 0; k < ARRAY_SIZE(td->o.bs); k++) {
342 if (td->o.verify != VERIFY_NONE &&
343 zone_size % td->o.bs[k] != 0) {
344 log_info("%s: block size %llu is not a divisor of the zone size %d\n",
345 f->file_name, td->o.bs[k],
355 static int ilog2(uint64_t i)
367 * Initialize f->zbd_info for devices that are not zoned block devices. This
368 * allows to execute a ZBD workload against a non-ZBD device.
370 static int init_zone_info(struct thread_data *td, struct fio_file *f)
373 struct fio_zone_info *p;
374 uint64_t zone_size = td->o.zone_size;
375 uint64_t zone_capacity = td->o.zone_capacity;
376 struct zoned_block_device_info *zbd_info = NULL;
379 if (zone_size == 0) {
380 log_err("%s: Specifying the zone size is mandatory for regular block devices with --zonemode=zbd\n\n",
385 if (zone_size < 512) {
386 log_err("%s: zone size must be at least 512 bytes for --zonemode=zbd\n\n",
391 if (zone_capacity == 0)
392 zone_capacity = zone_size;
394 if (zone_capacity > zone_size) {
395 log_err("%s: job parameter zonecapacity %llu is larger than zone size %llu\n",
396 f->file_name, (unsigned long long) td->o.zone_capacity,
397 (unsigned long long) td->o.zone_size);
401 nr_zones = (f->real_file_size + zone_size - 1) / zone_size;
402 zbd_info = scalloc(1, sizeof(*zbd_info) +
403 (nr_zones + 1) * sizeof(zbd_info->zone_info[0]));
407 mutex_init_pshared(&zbd_info->mutex);
408 zbd_info->refcount = 1;
409 p = &zbd_info->zone_info[0];
410 for (i = 0; i < nr_zones; i++, p++) {
411 mutex_init_pshared_with_type(&p->mutex,
412 PTHREAD_MUTEX_RECURSIVE);
413 p->start = i * zone_size;
415 p->type = ZBD_ZONE_TYPE_SWR;
416 p->cond = ZBD_ZONE_COND_EMPTY;
417 p->capacity = zone_capacity;
420 p->start = nr_zones * zone_size;
422 f->zbd_info = zbd_info;
423 f->zbd_info->zone_size = zone_size;
424 f->zbd_info->zone_size_log2 = is_power_of_2(zone_size) ?
425 ilog2(zone_size) : 0;
426 f->zbd_info->nr_zones = nr_zones;
431 * Maximum number of zones to report in one operation.
433 #define ZBD_REPORT_MAX_ZONES 8192U
436 * Parse the device zone report and store it in f->zbd_info. Must be called
437 * only for devices that are zoned, namely those with a model != ZBD_NONE.
439 static int parse_zone_info(struct thread_data *td, struct fio_file *f)
442 struct zbd_zone *zones, *z;
443 struct fio_zone_info *p;
444 uint64_t zone_size, offset;
445 struct zoned_block_device_info *zbd_info = NULL;
448 zones = calloc(ZBD_REPORT_MAX_ZONES, sizeof(struct zbd_zone));
452 nrz = zbd_report_zones(td, f, 0, zones, ZBD_REPORT_MAX_ZONES);
455 log_info("fio: report zones (offset 0) failed for %s (%d).\n",
460 zone_size = zones[0].len;
461 nr_zones = (f->real_file_size + zone_size - 1) / zone_size;
463 if (td->o.zone_size == 0) {
464 td->o.zone_size = zone_size;
465 } else if (td->o.zone_size != zone_size) {
466 log_err("fio: %s job parameter zonesize %llu does not match disk zone size %llu.\n",
467 f->file_name, (unsigned long long) td->o.zone_size,
468 (unsigned long long) zone_size);
473 dprint(FD_ZBD, "Device %s has %d zones of size %llu KB\n", f->file_name,
474 nr_zones, (unsigned long long) zone_size / 1024);
476 zbd_info = scalloc(1, sizeof(*zbd_info) +
477 (nr_zones + 1) * sizeof(zbd_info->zone_info[0]));
481 mutex_init_pshared(&zbd_info->mutex);
482 zbd_info->refcount = 1;
483 p = &zbd_info->zone_info[0];
484 for (offset = 0, j = 0; j < nr_zones;) {
486 for (i = 0; i < nrz; i++, j++, z++, p++) {
487 mutex_init_pshared_with_type(&p->mutex,
488 PTHREAD_MUTEX_RECURSIVE);
490 p->capacity = z->capacity;
492 case ZBD_ZONE_COND_NOT_WP:
493 case ZBD_ZONE_COND_FULL:
494 p->wp = p->start + p->capacity;
497 assert(z->start <= z->wp);
498 assert(z->wp <= z->start + zone_size);
504 if (j > 0 && p->start != p[-1].start + zone_size) {
505 log_info("%s: invalid zone data\n",
512 offset = z->start + z->len;
515 nrz = zbd_report_zones(td, f, offset,
516 zones, ZBD_REPORT_MAX_ZONES);
519 log_info("fio: report zones (offset %llu) failed for %s (%d).\n",
520 (unsigned long long)offset,
527 zbd_info->zone_info[nr_zones].start = offset;
529 f->zbd_info = zbd_info;
530 f->zbd_info->zone_size = zone_size;
531 f->zbd_info->zone_size_log2 = is_power_of_2(zone_size) ?
532 ilog2(zone_size) : 0;
533 f->zbd_info->nr_zones = nr_zones;
544 * Allocate zone information and store it into f->zbd_info if zonemode=zbd.
546 * Returns 0 upon success and a negative error code upon failure.
548 static int zbd_create_zone_info(struct thread_data *td, struct fio_file *f)
550 enum zbd_zoned_model zbd_model;
553 assert(td->o.zone_mode == ZONE_MODE_ZBD);
555 ret = zbd_get_zoned_model(td, f, &zbd_model);
563 case ZBD_HOST_MANAGED:
564 ret = parse_zone_info(td, f);
567 ret = init_zone_info(td, f);
570 td_verror(td, EINVAL, "Unsupported zoned model");
571 log_err("Unsupported zoned model\n");
576 f->zbd_info->model = zbd_model;
577 f->zbd_info->max_open_zones = td->o.max_open_zones;
582 void zbd_free_zone_info(struct fio_file *f)
588 pthread_mutex_lock(&f->zbd_info->mutex);
589 refcount = --f->zbd_info->refcount;
590 pthread_mutex_unlock(&f->zbd_info->mutex);
592 assert((int32_t)refcount >= 0);
599 * Initialize f->zbd_info.
601 * Returns 0 upon success and a negative error code upon failure.
603 * Note: this function can only work correctly if it is called before the first
606 static int zbd_init_zone_info(struct thread_data *td, struct fio_file *file)
608 struct thread_data *td2;
612 for_each_td(td2, i) {
613 for_each_file(td2, f2, j) {
614 if (td2 == td && f2 == file)
617 strcmp(f2->file_name, file->file_name) != 0)
619 file->zbd_info = f2->zbd_info;
620 file->zbd_info->refcount++;
625 ret = zbd_create_zone_info(td, file);
627 td_verror(td, -ret, "zbd_create_zone_info() failed");
631 static bool zbd_open_zone(struct thread_data *td, const struct fio_file *f,
633 static int zbd_reset_zone(struct thread_data *td, struct fio_file *f,
634 struct fio_zone_info *z);
636 int zbd_setup_files(struct thread_data *td)
641 for_each_file(td, f, i) {
642 if (zbd_init_zone_info(td, f))
646 if (!zbd_using_direct_io()) {
647 log_err("Using direct I/O is mandatory for writing to ZBD drives\n\n");
651 if (!zbd_verify_sizes())
654 if (!zbd_verify_bs())
657 for_each_file(td, f, i) {
658 struct zoned_block_device_info *zbd = f->zbd_info;
659 struct fio_zone_info *z;
665 zbd->max_open_zones = zbd->max_open_zones ?: ZBD_MAX_OPEN_ZONES;
667 if (td->o.max_open_zones > 0 &&
668 zbd->max_open_zones != td->o.max_open_zones) {
669 log_err("Different 'max_open_zones' values\n");
672 if (zbd->max_open_zones > ZBD_MAX_OPEN_ZONES) {
673 log_err("'max_open_zones' value is limited by %u\n", ZBD_MAX_OPEN_ZONES);
677 for (zi = f->min_zone; zi < f->max_zone; zi++) {
678 z = &zbd->zone_info[zi];
679 if (z->cond != ZBD_ZONE_COND_IMP_OPEN &&
680 z->cond != ZBD_ZONE_COND_EXP_OPEN)
682 if (zbd_open_zone(td, f, zi))
685 * If the number of open zones exceeds specified limits,
686 * reset all extra open zones.
688 if (zbd_reset_zone(td, f, z) < 0) {
689 log_err("Failed to reest zone %d\n", zi);
698 static unsigned int zbd_zone_nr(struct zoned_block_device_info *zbd_info,
699 struct fio_zone_info *zone)
701 return zone - zbd_info->zone_info;
705 * zbd_reset_zone - reset the write pointer of a single zone
706 * @td: FIO thread data.
707 * @f: FIO file associated with the disk for which to reset a write pointer.
710 * Returns 0 upon success and a negative error code upon failure.
712 * The caller must hold z->mutex.
714 static int zbd_reset_zone(struct thread_data *td, struct fio_file *f,
715 struct fio_zone_info *z)
717 uint64_t offset = z->start;
718 uint64_t length = (z+1)->start - offset;
721 assert(is_valid_offset(f, offset + length - 1));
723 dprint(FD_ZBD, "%s: resetting wp of zone %u.\n", f->file_name,
724 zbd_zone_nr(f->zbd_info, z));
725 switch (f->zbd_info->model) {
727 case ZBD_HOST_MANAGED:
728 ret = zbd_reset_wp(td, f, offset, length);
736 pthread_mutex_lock(&f->zbd_info->mutex);
737 f->zbd_info->sectors_with_data -= z->wp - z->start;
738 pthread_mutex_unlock(&f->zbd_info->mutex);
742 td->ts.nr_zone_resets++;
747 /* The caller must hold f->zbd_info->mutex */
748 static void zbd_close_zone(struct thread_data *td, const struct fio_file *f,
749 unsigned int zone_idx)
751 uint32_t open_zone_idx = 0;
753 for (; open_zone_idx < f->zbd_info->num_open_zones; open_zone_idx++) {
754 if (f->zbd_info->open_zones[open_zone_idx] == zone_idx)
757 if (open_zone_idx == f->zbd_info->num_open_zones) {
758 dprint(FD_ZBD, "%s: zone %d is not open\n",
759 f->file_name, zone_idx);
763 dprint(FD_ZBD, "%s: closing zone %d\n", f->file_name, zone_idx);
764 memmove(f->zbd_info->open_zones + open_zone_idx,
765 f->zbd_info->open_zones + open_zone_idx + 1,
766 (ZBD_MAX_OPEN_ZONES - (open_zone_idx + 1)) *
767 sizeof(f->zbd_info->open_zones[0]));
768 f->zbd_info->num_open_zones--;
769 td->num_open_zones--;
770 f->zbd_info->zone_info[zone_idx].open = 0;
774 * Reset a range of zones. Returns 0 upon success and 1 upon failure.
775 * @td: fio thread data.
776 * @f: fio file for which to reset zones
777 * @zb: first zone to reset.
778 * @ze: first zone not to reset.
779 * @all_zones: whether to reset all zones or only those zones for which the
780 * write pointer is not a multiple of td->o.min_bs[DDIR_WRITE].
782 static int zbd_reset_zones(struct thread_data *td, struct fio_file *f,
783 struct fio_zone_info *const zb,
784 struct fio_zone_info *const ze, bool all_zones)
786 struct fio_zone_info *z;
787 const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
793 dprint(FD_ZBD, "%s: examining zones %u .. %u\n", f->file_name,
794 zbd_zone_nr(f->zbd_info, zb), zbd_zone_nr(f->zbd_info, ze));
795 for (z = zb; z < ze; z++) {
796 uint32_t nz = z - f->zbd_info->zone_info;
798 if (!zbd_zone_swr(z))
802 pthread_mutex_lock(&f->zbd_info->mutex);
803 zbd_close_zone(td, f, nz);
804 pthread_mutex_unlock(&f->zbd_info->mutex);
806 reset_wp = z->wp != z->start;
808 reset_wp = z->wp % min_bs != 0;
811 dprint(FD_ZBD, "%s: resetting zone %u\n",
813 zbd_zone_nr(f->zbd_info, z));
814 if (zbd_reset_zone(td, f, z) < 0)
817 pthread_mutex_unlock(&z->mutex);
824 * Reset zbd_info.write_cnt, the counter that counts down towards the next
827 static void _zbd_reset_write_cnt(const struct thread_data *td,
828 const struct fio_file *f)
830 assert(0 <= td->o.zrf.u.f && td->o.zrf.u.f <= 1);
832 f->zbd_info->write_cnt = td->o.zrf.u.f ?
833 min(1.0 / td->o.zrf.u.f, 0.0 + UINT_MAX) : UINT_MAX;
836 static void zbd_reset_write_cnt(const struct thread_data *td,
837 const struct fio_file *f)
839 pthread_mutex_lock(&f->zbd_info->mutex);
840 _zbd_reset_write_cnt(td, f);
841 pthread_mutex_unlock(&f->zbd_info->mutex);
844 static bool zbd_dec_and_reset_write_cnt(const struct thread_data *td,
845 const struct fio_file *f)
847 uint32_t write_cnt = 0;
849 pthread_mutex_lock(&f->zbd_info->mutex);
850 assert(f->zbd_info->write_cnt);
851 if (f->zbd_info->write_cnt)
852 write_cnt = --f->zbd_info->write_cnt;
854 _zbd_reset_write_cnt(td, f);
855 pthread_mutex_unlock(&f->zbd_info->mutex);
857 return write_cnt == 0;
865 /* Calculate the number of sectors with data (swd) and perform action 'a' */
866 static uint64_t zbd_process_swd(const struct fio_file *f, enum swd_action a)
868 struct fio_zone_info *zb, *ze, *z;
871 zb = &f->zbd_info->zone_info[f->min_zone];
872 ze = &f->zbd_info->zone_info[f->max_zone];
873 for (z = zb; z < ze; z++) {
874 pthread_mutex_lock(&z->mutex);
875 swd += z->wp - z->start;
877 pthread_mutex_lock(&f->zbd_info->mutex);
880 assert(f->zbd_info->sectors_with_data == swd);
883 f->zbd_info->sectors_with_data = swd;
886 pthread_mutex_unlock(&f->zbd_info->mutex);
887 for (z = zb; z < ze; z++)
888 pthread_mutex_unlock(&z->mutex);
894 * The swd check is useful for debugging but takes too much time to leave
895 * it enabled all the time. Hence it is disabled by default.
897 static const bool enable_check_swd = false;
899 /* Check whether the value of zbd_info.sectors_with_data is correct. */
900 static void zbd_check_swd(const struct fio_file *f)
902 if (!enable_check_swd)
905 zbd_process_swd(f, CHECK_SWD);
908 static void zbd_init_swd(struct fio_file *f)
912 if (!enable_check_swd)
915 swd = zbd_process_swd(f, SET_SWD);
916 dprint(FD_ZBD, "%s(%s): swd = %" PRIu64 "\n", __func__, f->file_name,
920 void zbd_file_reset(struct thread_data *td, struct fio_file *f)
922 struct fio_zone_info *zb, *ze;
924 if (!f->zbd_info || !td_write(td))
927 zb = &f->zbd_info->zone_info[f->min_zone];
928 ze = &f->zbd_info->zone_info[f->max_zone];
931 * If data verification is enabled reset the affected zones before
932 * writing any data to avoid that a zone reset has to be issued while
933 * writing data, which causes data loss.
935 zbd_reset_zones(td, f, zb, ze, td->o.verify != VERIFY_NONE &&
936 td->runstate != TD_VERIFYING);
937 zbd_reset_write_cnt(td, f);
940 /* The caller must hold f->zbd_info->mutex. */
941 static bool is_zone_open(const struct thread_data *td, const struct fio_file *f,
942 unsigned int zone_idx)
944 struct zoned_block_device_info *zbdi = f->zbd_info;
947 assert(td->o.job_max_open_zones == 0 || td->num_open_zones <= td->o.job_max_open_zones);
948 assert(td->o.job_max_open_zones <= zbdi->max_open_zones);
949 assert(zbdi->num_open_zones <= zbdi->max_open_zones);
951 for (i = 0; i < zbdi->num_open_zones; i++)
952 if (zbdi->open_zones[i] == zone_idx)
959 * Open a ZBD zone if it was not yet open. Returns true if either the zone was
960 * already open or if opening a new zone is allowed. Returns false if the zone
961 * was not yet open and opening a new zone would cause the zone limit to be
964 static bool zbd_open_zone(struct thread_data *td, const struct fio_file *f,
967 const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
968 struct fio_zone_info *z = &f->zbd_info->zone_info[zone_idx];
971 if (z->cond == ZBD_ZONE_COND_OFFLINE)
975 * Skip full zones with data verification enabled because resetting a
976 * zone causes data loss and hence causes verification to fail.
978 if (td->o.verify != VERIFY_NONE && zbd_zone_full(f, z, min_bs))
981 pthread_mutex_lock(&f->zbd_info->mutex);
982 if (is_zone_open(td, f, zone_idx)) {
984 * If the zone is already open and going to be full by writes
985 * in-flight, handle it as a full zone instead of an open zone.
987 if (z->wp >= zbd_zone_capacity_end(z))
992 /* Zero means no limit */
993 if (td->o.job_max_open_zones > 0 &&
994 td->num_open_zones >= td->o.job_max_open_zones)
996 if (f->zbd_info->num_open_zones >= f->zbd_info->max_open_zones)
998 dprint(FD_ZBD, "%s: opening zone %d\n", f->file_name, zone_idx);
999 f->zbd_info->open_zones[f->zbd_info->num_open_zones++] = zone_idx;
1000 td->num_open_zones++;
1005 pthread_mutex_unlock(&f->zbd_info->mutex);
1009 /* Anything goes as long as it is not a constant. */
1010 static uint32_t pick_random_zone_idx(const struct fio_file *f,
1011 const struct io_u *io_u)
1013 return io_u->offset * f->zbd_info->num_open_zones / f->real_file_size;
1017 * Modify the offset of an I/O unit that does not refer to an open zone such
1018 * that it refers to an open zone. Close an open zone and open a new zone if
1019 * necessary. This algorithm can only work correctly if all write pointers are
1020 * a multiple of the fio block size. The caller must neither hold z->mutex
1021 * nor f->zbd_info->mutex. Returns with z->mutex held upon success.
1023 static struct fio_zone_info *zbd_convert_to_open_zone(struct thread_data *td,
1026 const uint32_t min_bs = td->o.min_bs[io_u->ddir];
1027 struct fio_file *f = io_u->file;
1028 struct fio_zone_info *z;
1029 unsigned int open_zone_idx = -1;
1030 uint32_t zone_idx, new_zone_idx;
1032 bool wait_zone_close;
1034 assert(is_valid_offset(f, io_u->offset));
1036 if (td->o.max_open_zones || td->o.job_max_open_zones) {
1038 * This statement accesses f->zbd_info->open_zones[] on purpose
1041 zone_idx = f->zbd_info->open_zones[pick_random_zone_idx(f, io_u)];
1043 zone_idx = zbd_zone_idx(f, io_u->offset);
1045 if (zone_idx < f->min_zone)
1046 zone_idx = f->min_zone;
1047 else if (zone_idx >= f->max_zone)
1048 zone_idx = f->max_zone - 1;
1049 dprint(FD_ZBD, "%s(%s): starting from zone %d (offset %lld, buflen %lld)\n",
1050 __func__, f->file_name, zone_idx, io_u->offset, io_u->buflen);
1053 * Since z->mutex is the outer lock and f->zbd_info->mutex the inner
1054 * lock it can happen that the state of the zone with index zone_idx
1055 * has changed after 'z' has been assigned and before f->zbd_info->mutex
1056 * has been obtained. Hence the loop.
1061 z = &f->zbd_info->zone_info[zone_idx];
1063 zone_lock(td, f, z);
1064 pthread_mutex_lock(&f->zbd_info->mutex);
1065 if (td->o.max_open_zones == 0 && td->o.job_max_open_zones == 0)
1067 if (f->zbd_info->num_open_zones == 0) {
1068 dprint(FD_ZBD, "%s(%s): no zones are open\n",
1069 __func__, f->file_name);
1070 goto open_other_zone;
1074 * List of opened zones is per-device, shared across all threads.
1075 * Start with quasi-random candidate zone.
1076 * Ignore zones which don't belong to thread's offset/size area.
1078 open_zone_idx = pick_random_zone_idx(f, io_u);
1079 assert(open_zone_idx < f->zbd_info->num_open_zones);
1080 tmp_idx = open_zone_idx;
1081 for (i = 0; i < f->zbd_info->num_open_zones; i++) {
1084 if (tmp_idx >= f->zbd_info->num_open_zones)
1086 tmpz = f->zbd_info->open_zones[tmp_idx];
1087 if (f->min_zone <= tmpz && tmpz < f->max_zone) {
1088 open_zone_idx = tmp_idx;
1089 goto found_candidate_zone;
1095 dprint(FD_ZBD, "%s(%s): no candidate zone\n",
1096 __func__, f->file_name);
1097 pthread_mutex_unlock(&f->zbd_info->mutex);
1098 pthread_mutex_unlock(&z->mutex);
1101 found_candidate_zone:
1102 new_zone_idx = f->zbd_info->open_zones[open_zone_idx];
1103 if (new_zone_idx == zone_idx)
1105 zone_idx = new_zone_idx;
1106 pthread_mutex_unlock(&f->zbd_info->mutex);
1107 pthread_mutex_unlock(&z->mutex);
1110 /* Both z->mutex and f->zbd_info->mutex are held. */
1113 if (z->wp + min_bs <= zbd_zone_capacity_end(z)) {
1114 pthread_mutex_unlock(&f->zbd_info->mutex);
1119 /* Check if number of open zones reaches one of limits. */
1121 f->zbd_info->num_open_zones == f->max_zone - f->min_zone ||
1122 (td->o.max_open_zones &&
1123 f->zbd_info->num_open_zones == td->o.max_open_zones) ||
1124 (td->o.job_max_open_zones &&
1125 td->num_open_zones == td->o.job_max_open_zones);
1127 pthread_mutex_unlock(&f->zbd_info->mutex);
1129 /* Only z->mutex is held. */
1132 * When number of open zones reaches to one of limits, wait for
1133 * zone close before opening a new zone.
1135 if (wait_zone_close) {
1136 dprint(FD_ZBD, "%s(%s): quiesce to allow open zones to close\n",
1137 __func__, f->file_name);
1141 /* Zone 'z' is full, so try to open a new zone. */
1142 for (i = f->io_size / f->zbd_info->zone_size; i > 0; i--) {
1144 pthread_mutex_unlock(&z->mutex);
1146 if (!is_valid_offset(f, z->start)) {
1148 zone_idx = f->min_zone;
1149 z = &f->zbd_info->zone_info[zone_idx];
1151 assert(is_valid_offset(f, z->start));
1152 zone_lock(td, f, z);
1155 if (zbd_open_zone(td, f, zone_idx))
1159 /* Only z->mutex is held. */
1161 /* Check whether the write fits in any of the already opened zones. */
1162 pthread_mutex_lock(&f->zbd_info->mutex);
1163 for (i = 0; i < f->zbd_info->num_open_zones; i++) {
1164 zone_idx = f->zbd_info->open_zones[i];
1165 if (zone_idx < f->min_zone || zone_idx >= f->max_zone)
1167 pthread_mutex_unlock(&f->zbd_info->mutex);
1168 pthread_mutex_unlock(&z->mutex);
1170 z = &f->zbd_info->zone_info[zone_idx];
1172 zone_lock(td, f, z);
1173 if (z->wp + min_bs <= zbd_zone_capacity_end(z))
1175 pthread_mutex_lock(&f->zbd_info->mutex);
1177 pthread_mutex_unlock(&f->zbd_info->mutex);
1178 pthread_mutex_unlock(&z->mutex);
1179 dprint(FD_ZBD, "%s(%s): did not open another zone\n", __func__,
1184 dprint(FD_ZBD, "%s(%s): returning zone %d\n", __func__, f->file_name,
1186 io_u->offset = z->start;
1190 /* The caller must hold z->mutex. */
1191 static struct fio_zone_info *zbd_replay_write_order(struct thread_data *td,
1193 struct fio_zone_info *z)
1195 const struct fio_file *f = io_u->file;
1196 const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
1198 if (!zbd_open_zone(td, f, z - f->zbd_info->zone_info)) {
1199 pthread_mutex_unlock(&z->mutex);
1200 z = zbd_convert_to_open_zone(td, io_u);
1204 if (z->verify_block * min_bs >= z->capacity)
1205 log_err("%s: %d * %d >= %llu\n", f->file_name, z->verify_block,
1206 min_bs, (unsigned long long)z->capacity);
1207 io_u->offset = z->start + z->verify_block++ * min_bs;
1212 * Find another zone for which @io_u fits below the write pointer. Start
1213 * searching in zones @zb + 1 .. @zl and continue searching in zones
1216 * Either returns NULL or returns a zone pointer and holds the mutex for that
1219 static struct fio_zone_info *
1220 zbd_find_zone(struct thread_data *td, struct io_u *io_u,
1221 struct fio_zone_info *zb, struct fio_zone_info *zl)
1223 const uint32_t min_bs = td->o.min_bs[io_u->ddir];
1224 struct fio_file *f = io_u->file;
1225 struct fio_zone_info *z1, *z2;
1226 const struct fio_zone_info *const zf =
1227 &f->zbd_info->zone_info[f->min_zone];
1230 * Skip to the next non-empty zone in case of sequential I/O and to
1231 * the nearest non-empty zone in case of random I/O.
1233 for (z1 = zb + 1, z2 = zb - 1; z1 < zl || z2 >= zf; z1++, z2--) {
1234 if (z1 < zl && z1->cond != ZBD_ZONE_COND_OFFLINE) {
1235 zone_lock(td, f, z1);
1236 if (z1->start + min_bs <= z1->wp)
1238 pthread_mutex_unlock(&z1->mutex);
1239 } else if (!td_random(td)) {
1242 if (td_random(td) && z2 >= zf &&
1243 z2->cond != ZBD_ZONE_COND_OFFLINE) {
1244 zone_lock(td, f, z2);
1245 if (z2->start + min_bs <= z2->wp)
1247 pthread_mutex_unlock(&z2->mutex);
1250 dprint(FD_ZBD, "%s: adjusting random read offset failed\n",
1256 * zbd_end_zone_io - update zone status at command completion
1258 * @z: zone info pointer
1260 * If the write command made the zone full, close it.
1262 * The caller must hold z->mutex.
1264 static void zbd_end_zone_io(struct thread_data *td, const struct io_u *io_u,
1265 struct fio_zone_info *z)
1267 const struct fio_file *f = io_u->file;
1269 if (io_u->ddir == DDIR_WRITE &&
1270 io_u->offset + io_u->buflen >= zbd_zone_capacity_end(z)) {
1271 pthread_mutex_lock(&f->zbd_info->mutex);
1272 zbd_close_zone(td, f, z - f->zbd_info->zone_info);
1273 pthread_mutex_unlock(&f->zbd_info->mutex);
1278 * zbd_queue_io - update the write pointer of a sequential zone
1280 * @success: Whether or not the I/O unit has been queued successfully
1281 * @q: queueing status (busy, completed or queued).
1283 * For write and trim operations, update the write pointer of the I/O unit
1286 static void zbd_queue_io(struct thread_data *td, struct io_u *io_u, int q,
1289 const struct fio_file *f = io_u->file;
1290 struct zoned_block_device_info *zbd_info = f->zbd_info;
1291 struct fio_zone_info *z;
1298 zone_idx = zbd_zone_idx(f, io_u->offset);
1299 assert(zone_idx < zbd_info->nr_zones);
1300 z = &zbd_info->zone_info[zone_idx];
1302 if (!zbd_zone_swr(z))
1309 "%s: queued I/O (%lld, %llu) for zone %u\n",
1310 f->file_name, io_u->offset, io_u->buflen, zone_idx);
1312 switch (io_u->ddir) {
1314 zone_end = min((uint64_t)(io_u->offset + io_u->buflen),
1315 zbd_zone_capacity_end(z));
1316 pthread_mutex_lock(&zbd_info->mutex);
1318 * z->wp > zone_end means that one or more I/O errors
1321 if (z->wp <= zone_end)
1322 zbd_info->sectors_with_data += zone_end - z->wp;
1323 pthread_mutex_unlock(&zbd_info->mutex);
1327 assert(z->wp == z->start);
1333 if (q == FIO_Q_COMPLETED && !io_u->error)
1334 zbd_end_zone_io(td, io_u, z);
1337 if (!success || q != FIO_Q_QUEUED) {
1338 /* BUSY or COMPLETED: unlock the zone */
1339 pthread_mutex_unlock(&z->mutex);
1340 io_u->zbd_put_io = NULL;
1345 * zbd_put_io - Unlock an I/O unit target zone lock
1348 static void zbd_put_io(struct thread_data *td, const struct io_u *io_u)
1350 const struct fio_file *f = io_u->file;
1351 struct zoned_block_device_info *zbd_info = f->zbd_info;
1352 struct fio_zone_info *z;
1359 zone_idx = zbd_zone_idx(f, io_u->offset);
1360 assert(zone_idx < zbd_info->nr_zones);
1361 z = &zbd_info->zone_info[zone_idx];
1363 if (!zbd_zone_swr(z))
1367 "%s: terminate I/O (%lld, %llu) for zone %u\n",
1368 f->file_name, io_u->offset, io_u->buflen, zone_idx);
1370 zbd_end_zone_io(td, io_u, z);
1372 ret = pthread_mutex_unlock(&z->mutex);
1378 * Windows and MacOS do not define this.
1381 #define EREMOTEIO 121 /* POSIX value */
1384 bool zbd_unaligned_write(int error_code)
1386 switch (error_code) {
1395 * setup_zbd_zone_mode - handle zoneskip as necessary for ZBD drives
1396 * @td: FIO thread data.
1397 * @io_u: FIO I/O unit.
1399 * For sequential workloads, change the file offset to skip zoneskip bytes when
1400 * no more IO can be performed in the current zone.
1401 * - For read workloads, zoneskip is applied when the io has reached the end of
1402 * the zone or the zone write position (when td->o.read_beyond_wp is false).
1403 * - For write workloads, zoneskip is applied when the zone is full.
1404 * This applies only to read and write operations.
1406 void setup_zbd_zone_mode(struct thread_data *td, struct io_u *io_u)
1408 struct fio_file *f = io_u->file;
1409 enum fio_ddir ddir = io_u->ddir;
1410 struct fio_zone_info *z;
1413 assert(td->o.zone_mode == ZONE_MODE_ZBD);
1414 assert(td->o.zone_size);
1416 zone_idx = zbd_zone_idx(f, f->last_pos[ddir]);
1417 z = &f->zbd_info->zone_info[zone_idx];
1420 * When the zone capacity is smaller than the zone size and the I/O is
1421 * sequential write, skip to zone end if the latest position is at the
1422 * zone capacity limit.
1424 if (z->capacity < f->zbd_info->zone_size && !td_random(td) &&
1425 ddir == DDIR_WRITE &&
1426 f->last_pos[ddir] >= zbd_zone_capacity_end(z)) {
1428 "%s: Jump from zone capacity limit to zone end:"
1429 " (%llu -> %llu) for zone %u (%llu)\n",
1430 f->file_name, (unsigned long long) f->last_pos[ddir],
1431 (unsigned long long) zbd_zone_end(z),
1432 zbd_zone_nr(f->zbd_info, z),
1433 (unsigned long long) z->capacity);
1434 td->io_skip_bytes += zbd_zone_end(z) - f->last_pos[ddir];
1435 f->last_pos[ddir] = zbd_zone_end(z);
1439 * zone_skip is valid only for sequential workloads.
1441 if (td_random(td) || !td->o.zone_skip)
1445 * It is time to switch to a new zone if:
1446 * - zone_bytes == zone_size bytes have already been accessed
1447 * - The last position reached the end of the current zone.
1448 * - For reads with td->o.read_beyond_wp == false, the last position
1449 * reached the zone write pointer.
1451 if (td->zone_bytes >= td->o.zone_size ||
1452 f->last_pos[ddir] >= zbd_zone_end(z) ||
1453 (ddir == DDIR_READ &&
1454 (!td->o.read_beyond_wp) && f->last_pos[ddir] >= z->wp)) {
1459 f->file_offset += td->o.zone_size + td->o.zone_skip;
1462 * Wrap from the beginning, if we exceed the file size
1464 if (f->file_offset >= f->real_file_size)
1465 f->file_offset = get_start_offset(td, f);
1467 f->last_pos[ddir] = f->file_offset;
1468 td->io_skip_bytes += td->o.zone_skip;
1473 * zbd_adjust_ddir - Adjust an I/O direction for zonemode=zbd.
1475 * @td: FIO thread data.
1476 * @io_u: FIO I/O unit.
1477 * @ddir: I/O direction before adjustment.
1479 * Return adjusted I/O direction.
1481 enum fio_ddir zbd_adjust_ddir(struct thread_data *td, struct io_u *io_u,
1485 * In case read direction is chosen for the first random I/O, fio with
1486 * zonemode=zbd stops because no data can be read from zoned block
1487 * devices with all empty zones. Overwrite the first I/O direction as
1488 * write to make sure data to read exists.
1490 if (ddir != DDIR_READ || !td_rw(td))
1493 if (io_u->file->zbd_info->sectors_with_data ||
1494 td->o.read_beyond_wp)
1501 * zbd_adjust_block - adjust the offset and length as necessary for ZBD drives
1502 * @td: FIO thread data.
1503 * @io_u: FIO I/O unit.
1505 * Locking strategy: returns with z->mutex locked if and only if z refers
1506 * to a sequential zone and if io_u_accept is returned. z is the zone that
1507 * corresponds to io_u->offset at the end of this function.
1509 enum io_u_action zbd_adjust_block(struct thread_data *td, struct io_u *io_u)
1511 struct fio_file *f = io_u->file;
1512 uint32_t zone_idx_b;
1513 struct fio_zone_info *zb, *zl, *orig_zb;
1514 uint32_t orig_len = io_u->buflen;
1515 uint32_t min_bs = td->o.min_bs[io_u->ddir];
1523 assert(is_valid_offset(f, io_u->offset));
1524 assert(io_u->buflen);
1525 zone_idx_b = zbd_zone_idx(f, io_u->offset);
1526 zb = &f->zbd_info->zone_info[zone_idx_b];
1529 /* Accept the I/O offset for conventional zones. */
1530 if (!zbd_zone_swr(zb))
1534 * Accept the I/O offset for reads if reading beyond the write pointer
1537 if (zb->cond != ZBD_ZONE_COND_OFFLINE &&
1538 io_u->ddir == DDIR_READ && td->o.read_beyond_wp)
1543 zone_lock(td, f, zb);
1545 switch (io_u->ddir) {
1547 if (td->runstate == TD_VERIFYING && td_write(td)) {
1548 zb = zbd_replay_write_order(td, io_u, zb);
1552 * Check that there is enough written data in the zone to do an
1553 * I/O of at least min_bs B. If there isn't, find a new zone for
1556 range = zb->cond != ZBD_ZONE_COND_OFFLINE ?
1557 zb->wp - zb->start : 0;
1558 if (range < min_bs ||
1559 ((!td_random(td)) && (io_u->offset + min_bs > zb->wp))) {
1560 pthread_mutex_unlock(&zb->mutex);
1561 zl = &f->zbd_info->zone_info[f->max_zone];
1562 zb = zbd_find_zone(td, io_u, zb, zl);
1565 "%s: zbd_find_zone(%lld, %llu) failed\n",
1566 f->file_name, io_u->offset,
1571 * zbd_find_zone() returned a zone with a range of at
1574 range = zb->wp - zb->start;
1575 assert(range >= min_bs);
1578 io_u->offset = zb->start;
1581 * Make sure the I/O is within the zone valid data range while
1582 * maximizing the I/O size and preserving randomness.
1584 if (range <= io_u->buflen)
1585 io_u->offset = zb->start;
1586 else if (td_random(td))
1587 io_u->offset = zb->start +
1588 ((io_u->offset - orig_zb->start) %
1589 (range - io_u->buflen)) / min_bs * min_bs;
1591 * Make sure the I/O does not cross over the zone wp position.
1593 new_len = min((unsigned long long)io_u->buflen,
1594 (unsigned long long)(zb->wp - io_u->offset));
1595 new_len = new_len / min_bs * min_bs;
1596 if (new_len < io_u->buflen) {
1597 io_u->buflen = new_len;
1598 dprint(FD_IO, "Changed length from %u into %llu\n",
1599 orig_len, io_u->buflen);
1601 assert(zb->start <= io_u->offset);
1602 assert(io_u->offset + io_u->buflen <= zb->wp);
1605 if (io_u->buflen > f->zbd_info->zone_size)
1607 if (!zbd_open_zone(td, f, zone_idx_b)) {
1608 pthread_mutex_unlock(&zb->mutex);
1609 zb = zbd_convert_to_open_zone(td, io_u);
1612 zone_idx_b = zb - f->zbd_info->zone_info;
1614 /* Check whether the zone reset threshold has been exceeded */
1615 if (td->o.zrf.u.f) {
1616 if (f->zbd_info->sectors_with_data >=
1617 f->io_size * td->o.zrt.u.f &&
1618 zbd_dec_and_reset_write_cnt(td, f)) {
1622 /* Reset the zone pointer if necessary */
1623 if (zb->reset_zone || zbd_zone_full(f, zb, min_bs)) {
1624 assert(td->o.verify == VERIFY_NONE);
1626 * Since previous write requests may have been submitted
1627 * asynchronously and since we will submit the zone
1628 * reset synchronously, wait until previously submitted
1629 * write requests have completed before issuing a
1634 if (zbd_reset_zone(td, f, zb) < 0)
1637 if (zb->capacity < min_bs) {
1638 log_err("zone capacity %llu smaller than minimum block size %d\n",
1639 (unsigned long long)zb->capacity,
1644 /* Make writes occur at the write pointer */
1645 assert(!zbd_zone_full(f, zb, min_bs));
1646 io_u->offset = zb->wp;
1647 if (!is_valid_offset(f, io_u->offset)) {
1648 dprint(FD_ZBD, "Dropped request with offset %llu\n",
1653 * Make sure that the buflen is a multiple of the minimal
1654 * block size. Give up if shrinking would make the request too
1657 new_len = min((unsigned long long)io_u->buflen,
1658 zbd_zone_capacity_end(zb) - io_u->offset);
1659 new_len = new_len / min_bs * min_bs;
1660 if (new_len == io_u->buflen)
1662 if (new_len >= min_bs) {
1663 io_u->buflen = new_len;
1664 dprint(FD_IO, "Changed length from %u into %llu\n",
1665 orig_len, io_u->buflen);
1668 log_err("Zone remainder %lld smaller than minimum block size %d\n",
1669 (zbd_zone_capacity_end(zb) - io_u->offset),
1676 case DDIR_SYNC_FILE_RANGE:
1687 assert(zb->cond != ZBD_ZONE_COND_OFFLINE);
1688 assert(!io_u->zbd_queue_io);
1689 assert(!io_u->zbd_put_io);
1690 io_u->zbd_queue_io = zbd_queue_io;
1691 io_u->zbd_put_io = zbd_put_io;
1696 pthread_mutex_unlock(&zb->mutex);
1700 /* Return a string with ZBD statistics */
1701 char *zbd_write_status(const struct thread_stat *ts)
1705 if (asprintf(&res, "; %llu zone resets", (unsigned long long) ts->nr_zone_resets) < 0)