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);
328 static bool zbd_verify_bs(void)
330 struct thread_data *td;
336 for_each_file(td, f, j) {
339 zone_size = f->zbd_info->zone_size;
340 for (k = 0; k < ARRAY_SIZE(td->o.bs); k++) {
341 if (td->o.verify != VERIFY_NONE &&
342 zone_size % td->o.bs[k] != 0) {
343 log_info("%s: block size %llu is not a divisor of the zone size %d\n",
344 f->file_name, td->o.bs[k],
354 static int ilog2(uint64_t i)
366 * Initialize f->zbd_info for devices that are not zoned block devices. This
367 * allows to execute a ZBD workload against a non-ZBD device.
369 static int init_zone_info(struct thread_data *td, struct fio_file *f)
372 struct fio_zone_info *p;
373 uint64_t zone_size = td->o.zone_size;
374 uint64_t zone_capacity = td->o.zone_capacity;
375 struct zoned_block_device_info *zbd_info = NULL;
378 if (zone_size == 0) {
379 log_err("%s: Specifying the zone size is mandatory for regular block devices with --zonemode=zbd\n\n",
384 if (zone_size < 512) {
385 log_err("%s: zone size must be at least 512 bytes for --zonemode=zbd\n\n",
390 if (zone_capacity == 0)
391 zone_capacity = zone_size;
393 if (zone_capacity > zone_size) {
394 log_err("%s: job parameter zonecapacity %llu is larger than zone size %llu\n",
395 f->file_name, (unsigned long long) td->o.zone_capacity,
396 (unsigned long long) td->o.zone_size);
400 nr_zones = (f->real_file_size + zone_size - 1) / zone_size;
401 zbd_info = scalloc(1, sizeof(*zbd_info) +
402 (nr_zones + 1) * sizeof(zbd_info->zone_info[0]));
406 mutex_init_pshared(&zbd_info->mutex);
407 zbd_info->refcount = 1;
408 p = &zbd_info->zone_info[0];
409 for (i = 0; i < nr_zones; i++, p++) {
410 mutex_init_pshared_with_type(&p->mutex,
411 PTHREAD_MUTEX_RECURSIVE);
412 p->start = i * zone_size;
414 p->type = ZBD_ZONE_TYPE_SWR;
415 p->cond = ZBD_ZONE_COND_EMPTY;
416 p->capacity = zone_capacity;
419 p->start = nr_zones * zone_size;
421 f->zbd_info = zbd_info;
422 f->zbd_info->zone_size = zone_size;
423 f->zbd_info->zone_size_log2 = is_power_of_2(zone_size) ?
424 ilog2(zone_size) : 0;
425 f->zbd_info->nr_zones = nr_zones;
430 * Maximum number of zones to report in one operation.
432 #define ZBD_REPORT_MAX_ZONES 8192U
435 * Parse the device zone report and store it in f->zbd_info. Must be called
436 * only for devices that are zoned, namely those with a model != ZBD_NONE.
438 static int parse_zone_info(struct thread_data *td, struct fio_file *f)
441 struct zbd_zone *zones, *z;
442 struct fio_zone_info *p;
443 uint64_t zone_size, offset;
444 struct zoned_block_device_info *zbd_info = NULL;
447 zones = calloc(ZBD_REPORT_MAX_ZONES, sizeof(struct zbd_zone));
451 nrz = zbd_report_zones(td, f, 0, zones, ZBD_REPORT_MAX_ZONES);
454 log_info("fio: report zones (offset 0) failed for %s (%d).\n",
459 zone_size = zones[0].len;
460 nr_zones = (f->real_file_size + zone_size - 1) / zone_size;
462 if (td->o.zone_size == 0) {
463 td->o.zone_size = zone_size;
464 } else if (td->o.zone_size != zone_size) {
465 log_err("fio: %s job parameter zonesize %llu does not match disk zone size %llu.\n",
466 f->file_name, (unsigned long long) td->o.zone_size,
467 (unsigned long long) zone_size);
472 dprint(FD_ZBD, "Device %s has %d zones of size %llu KB\n", f->file_name,
473 nr_zones, (unsigned long long) zone_size / 1024);
475 zbd_info = scalloc(1, sizeof(*zbd_info) +
476 (nr_zones + 1) * sizeof(zbd_info->zone_info[0]));
480 mutex_init_pshared(&zbd_info->mutex);
481 zbd_info->refcount = 1;
482 p = &zbd_info->zone_info[0];
483 for (offset = 0, j = 0; j < nr_zones;) {
485 for (i = 0; i < nrz; i++, j++, z++, p++) {
486 mutex_init_pshared_with_type(&p->mutex,
487 PTHREAD_MUTEX_RECURSIVE);
489 p->capacity = z->capacity;
491 case ZBD_ZONE_COND_NOT_WP:
492 case ZBD_ZONE_COND_FULL:
493 p->wp = p->start + p->capacity;
496 assert(z->start <= z->wp);
497 assert(z->wp <= z->start + zone_size);
503 if (j > 0 && p->start != p[-1].start + zone_size) {
504 log_info("%s: invalid zone data\n",
511 offset = z->start + z->len;
514 nrz = zbd_report_zones(td, f, offset,
515 zones, ZBD_REPORT_MAX_ZONES);
518 log_info("fio: report zones (offset %llu) failed for %s (%d).\n",
519 (unsigned long long)offset,
526 zbd_info->zone_info[nr_zones].start = offset;
528 f->zbd_info = zbd_info;
529 f->zbd_info->zone_size = zone_size;
530 f->zbd_info->zone_size_log2 = is_power_of_2(zone_size) ?
531 ilog2(zone_size) : 0;
532 f->zbd_info->nr_zones = nr_zones;
543 * Allocate zone information and store it into f->zbd_info if zonemode=zbd.
545 * Returns 0 upon success and a negative error code upon failure.
547 static int zbd_create_zone_info(struct thread_data *td, struct fio_file *f)
549 enum zbd_zoned_model zbd_model;
552 assert(td->o.zone_mode == ZONE_MODE_ZBD);
554 ret = zbd_get_zoned_model(td, f, &zbd_model);
562 case ZBD_HOST_MANAGED:
563 ret = parse_zone_info(td, f);
566 ret = init_zone_info(td, f);
569 td_verror(td, EINVAL, "Unsupported zoned model");
570 log_err("Unsupported zoned model\n");
575 f->zbd_info->model = zbd_model;
576 f->zbd_info->max_open_zones = td->o.max_open_zones;
581 void zbd_free_zone_info(struct fio_file *f)
587 pthread_mutex_lock(&f->zbd_info->mutex);
588 refcount = --f->zbd_info->refcount;
589 pthread_mutex_unlock(&f->zbd_info->mutex);
591 assert((int32_t)refcount >= 0);
598 * Initialize f->zbd_info.
600 * Returns 0 upon success and a negative error code upon failure.
602 * Note: this function can only work correctly if it is called before the first
605 static int zbd_init_zone_info(struct thread_data *td, struct fio_file *file)
607 struct thread_data *td2;
611 for_each_td(td2, i) {
612 for_each_file(td2, f2, j) {
613 if (td2 == td && f2 == file)
616 strcmp(f2->file_name, file->file_name) != 0)
618 file->zbd_info = f2->zbd_info;
619 file->zbd_info->refcount++;
624 ret = zbd_create_zone_info(td, file);
626 td_verror(td, -ret, "zbd_create_zone_info() failed");
630 int zbd_setup_files(struct thread_data *td)
635 for_each_file(td, f, i) {
636 if (zbd_init_zone_info(td, f))
640 if (!zbd_using_direct_io()) {
641 log_err("Using direct I/O is mandatory for writing to ZBD drives\n\n");
645 if (!zbd_verify_sizes())
648 if (!zbd_verify_bs())
651 for_each_file(td, f, i) {
652 struct zoned_block_device_info *zbd = f->zbd_info;
657 zbd->max_open_zones = zbd->max_open_zones ?: ZBD_MAX_OPEN_ZONES;
659 if (td->o.max_open_zones > 0 &&
660 zbd->max_open_zones != td->o.max_open_zones) {
661 log_err("Different 'max_open_zones' values\n");
664 if (zbd->max_open_zones > ZBD_MAX_OPEN_ZONES) {
665 log_err("'max_open_zones' value is limited by %u\n", ZBD_MAX_OPEN_ZONES);
674 * zbd_reset_range - reset zones for a range of sectors
675 * @td: FIO thread data.
676 * @f: Fio file for which to reset zones
677 * @sector: Starting sector in units of 512 bytes
678 * @nr_sectors: Number of sectors in units of 512 bytes
680 * Returns 0 upon success and a negative error code upon failure.
682 static int zbd_reset_range(struct thread_data *td, struct fio_file *f,
683 uint64_t offset, uint64_t length)
685 uint32_t zone_idx_b, zone_idx_e;
686 struct fio_zone_info *zb, *ze, *z;
689 assert(is_valid_offset(f, offset + length - 1));
691 switch (f->zbd_info->model) {
693 case ZBD_HOST_MANAGED:
694 ret = zbd_reset_wp(td, f, offset, length);
702 zone_idx_b = zbd_zone_idx(f, offset);
703 zb = &f->zbd_info->zone_info[zone_idx_b];
704 zone_idx_e = zbd_zone_idx(f, offset + length);
705 ze = &f->zbd_info->zone_info[zone_idx_e];
706 for (z = zb; z < ze; z++) {
707 pthread_mutex_lock(&z->mutex);
708 pthread_mutex_lock(&f->zbd_info->mutex);
709 f->zbd_info->sectors_with_data -= z->wp - z->start;
710 pthread_mutex_unlock(&f->zbd_info->mutex);
713 pthread_mutex_unlock(&z->mutex);
716 td->ts.nr_zone_resets += ze - zb;
721 static unsigned int zbd_zone_nr(struct zoned_block_device_info *zbd_info,
722 struct fio_zone_info *zone)
724 return zone - zbd_info->zone_info;
728 * zbd_reset_zone - reset the write pointer of a single zone
729 * @td: FIO thread data.
730 * @f: FIO file associated with the disk for which to reset a write pointer.
733 * Returns 0 upon success and a negative error code upon failure.
735 static int zbd_reset_zone(struct thread_data *td, struct fio_file *f,
736 struct fio_zone_info *z)
738 dprint(FD_ZBD, "%s: resetting wp of zone %u.\n", f->file_name,
739 zbd_zone_nr(f->zbd_info, z));
741 return zbd_reset_range(td, f, z->start, zbd_zone_end(z) - z->start);
744 /* The caller must hold f->zbd_info->mutex */
745 static void zbd_close_zone(struct thread_data *td, const struct fio_file *f,
746 unsigned int open_zone_idx)
750 assert(open_zone_idx < f->zbd_info->num_open_zones);
751 zone_idx = f->zbd_info->open_zones[open_zone_idx];
752 memmove(f->zbd_info->open_zones + open_zone_idx,
753 f->zbd_info->open_zones + open_zone_idx + 1,
754 (ZBD_MAX_OPEN_ZONES - (open_zone_idx + 1)) *
755 sizeof(f->zbd_info->open_zones[0]));
756 f->zbd_info->num_open_zones--;
757 td->num_open_zones--;
758 f->zbd_info->zone_info[zone_idx].open = 0;
762 * Reset a range of zones. Returns 0 upon success and 1 upon failure.
763 * @td: fio thread data.
764 * @f: fio file for which to reset zones
765 * @zb: first zone to reset.
766 * @ze: first zone not to reset.
767 * @all_zones: whether to reset all zones or only those zones for which the
768 * write pointer is not a multiple of td->o.min_bs[DDIR_WRITE].
770 static int zbd_reset_zones(struct thread_data *td, struct fio_file *f,
771 struct fio_zone_info *const zb,
772 struct fio_zone_info *const ze, bool all_zones)
774 struct fio_zone_info *z;
775 const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
781 dprint(FD_ZBD, "%s: examining zones %u .. %u\n", f->file_name,
782 zbd_zone_nr(f->zbd_info, zb), zbd_zone_nr(f->zbd_info, ze));
783 for (z = zb; z < ze; z++) {
784 uint32_t nz = z - f->zbd_info->zone_info;
786 if (!zbd_zone_swr(z))
792 pthread_mutex_lock(&f->zbd_info->mutex);
793 for (i = 0; i < f->zbd_info->num_open_zones; i++) {
794 if (f->zbd_info->open_zones[i] == nz)
795 zbd_close_zone(td, f, i);
797 pthread_mutex_unlock(&f->zbd_info->mutex);
799 reset_wp = z->wp != z->start;
801 reset_wp = z->wp % min_bs != 0;
804 dprint(FD_ZBD, "%s: resetting zone %u\n",
806 zbd_zone_nr(f->zbd_info, z));
807 if (zbd_reset_zone(td, f, z) < 0)
810 pthread_mutex_unlock(&z->mutex);
817 * Reset zbd_info.write_cnt, the counter that counts down towards the next
820 static void _zbd_reset_write_cnt(const struct thread_data *td,
821 const struct fio_file *f)
823 assert(0 <= td->o.zrf.u.f && td->o.zrf.u.f <= 1);
825 f->zbd_info->write_cnt = td->o.zrf.u.f ?
826 min(1.0 / td->o.zrf.u.f, 0.0 + UINT_MAX) : UINT_MAX;
829 static void zbd_reset_write_cnt(const struct thread_data *td,
830 const struct fio_file *f)
832 pthread_mutex_lock(&f->zbd_info->mutex);
833 _zbd_reset_write_cnt(td, f);
834 pthread_mutex_unlock(&f->zbd_info->mutex);
837 static bool zbd_dec_and_reset_write_cnt(const struct thread_data *td,
838 const struct fio_file *f)
840 uint32_t write_cnt = 0;
842 pthread_mutex_lock(&f->zbd_info->mutex);
843 assert(f->zbd_info->write_cnt);
844 if (f->zbd_info->write_cnt)
845 write_cnt = --f->zbd_info->write_cnt;
847 _zbd_reset_write_cnt(td, f);
848 pthread_mutex_unlock(&f->zbd_info->mutex);
850 return write_cnt == 0;
858 /* Calculate the number of sectors with data (swd) and perform action 'a' */
859 static uint64_t zbd_process_swd(const struct fio_file *f, enum swd_action a)
861 struct fio_zone_info *zb, *ze, *z;
864 zb = &f->zbd_info->zone_info[zbd_zone_idx(f, f->file_offset)];
865 ze = &f->zbd_info->zone_info[zbd_zone_idx(f, f->file_offset +
867 for (z = zb; z < ze; z++) {
868 pthread_mutex_lock(&z->mutex);
869 swd += z->wp - z->start;
871 pthread_mutex_lock(&f->zbd_info->mutex);
874 assert(f->zbd_info->sectors_with_data == swd);
877 f->zbd_info->sectors_with_data = swd;
880 pthread_mutex_unlock(&f->zbd_info->mutex);
881 for (z = zb; z < ze; z++)
882 pthread_mutex_unlock(&z->mutex);
888 * The swd check is useful for debugging but takes too much time to leave
889 * it enabled all the time. Hence it is disabled by default.
891 static const bool enable_check_swd = false;
893 /* Check whether the value of zbd_info.sectors_with_data is correct. */
894 static void zbd_check_swd(const struct fio_file *f)
896 if (!enable_check_swd)
899 zbd_process_swd(f, CHECK_SWD);
902 static void zbd_init_swd(struct fio_file *f)
906 if (!enable_check_swd)
909 swd = zbd_process_swd(f, SET_SWD);
910 dprint(FD_ZBD, "%s(%s): swd = %" PRIu64 "\n", __func__, f->file_name,
914 void zbd_file_reset(struct thread_data *td, struct fio_file *f)
916 struct fio_zone_info *zb, *ze;
918 if (!f->zbd_info || !td_write(td))
921 zb = &f->zbd_info->zone_info[f->min_zone];
922 ze = &f->zbd_info->zone_info[f->max_zone];
925 * If data verification is enabled reset the affected zones before
926 * writing any data to avoid that a zone reset has to be issued while
927 * writing data, which causes data loss.
929 zbd_reset_zones(td, f, zb, ze, td->o.verify != VERIFY_NONE &&
930 td->runstate != TD_VERIFYING);
931 zbd_reset_write_cnt(td, f);
934 /* The caller must hold f->zbd_info->mutex. */
935 static bool is_zone_open(const struct thread_data *td, const struct fio_file *f,
936 unsigned int zone_idx)
938 struct zoned_block_device_info *zbdi = f->zbd_info;
941 assert(td->o.job_max_open_zones == 0 || td->num_open_zones <= td->o.job_max_open_zones);
942 assert(td->o.job_max_open_zones <= zbdi->max_open_zones);
943 assert(zbdi->num_open_zones <= zbdi->max_open_zones);
945 for (i = 0; i < zbdi->num_open_zones; i++)
946 if (zbdi->open_zones[i] == zone_idx)
953 * Open a ZBD zone if it was not yet open. Returns true if either the zone was
954 * already open or if opening a new zone is allowed. Returns false if the zone
955 * was not yet open and opening a new zone would cause the zone limit to be
958 static bool zbd_open_zone(struct thread_data *td, const struct io_u *io_u,
961 const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
962 const struct fio_file *f = io_u->file;
963 struct fio_zone_info *z = &f->zbd_info->zone_info[zone_idx];
966 if (z->cond == ZBD_ZONE_COND_OFFLINE)
970 * Skip full zones with data verification enabled because resetting a
971 * zone causes data loss and hence causes verification to fail.
973 if (td->o.verify != VERIFY_NONE && zbd_zone_full(f, z, min_bs))
976 pthread_mutex_lock(&f->zbd_info->mutex);
977 if (is_zone_open(td, f, zone_idx))
980 /* Zero means no limit */
981 if (td->o.job_max_open_zones > 0 &&
982 td->num_open_zones >= td->o.job_max_open_zones)
984 if (f->zbd_info->num_open_zones >= f->zbd_info->max_open_zones)
986 dprint(FD_ZBD, "%s: opening zone %d\n", f->file_name, zone_idx);
987 f->zbd_info->open_zones[f->zbd_info->num_open_zones++] = zone_idx;
988 td->num_open_zones++;
993 pthread_mutex_unlock(&f->zbd_info->mutex);
997 /* Anything goes as long as it is not a constant. */
998 static uint32_t pick_random_zone_idx(const struct fio_file *f,
999 const struct io_u *io_u)
1001 return io_u->offset * f->zbd_info->num_open_zones / f->real_file_size;
1005 * Modify the offset of an I/O unit that does not refer to an open zone such
1006 * that it refers to an open zone. Close an open zone and open a new zone if
1007 * necessary. This algorithm can only work correctly if all write pointers are
1008 * a multiple of the fio block size. The caller must neither hold z->mutex
1009 * nor f->zbd_info->mutex. Returns with z->mutex held upon success.
1011 static struct fio_zone_info *zbd_convert_to_open_zone(struct thread_data *td,
1014 const uint32_t min_bs = td->o.min_bs[io_u->ddir];
1015 struct fio_file *f = io_u->file;
1016 struct fio_zone_info *z;
1017 unsigned int open_zone_idx = -1;
1018 uint32_t zone_idx, new_zone_idx;
1021 assert(is_valid_offset(f, io_u->offset));
1023 if (td->o.max_open_zones || td->o.job_max_open_zones) {
1025 * This statement accesses f->zbd_info->open_zones[] on purpose
1028 zone_idx = f->zbd_info->open_zones[pick_random_zone_idx(f, io_u)];
1030 zone_idx = zbd_zone_idx(f, io_u->offset);
1032 if (zone_idx < f->min_zone)
1033 zone_idx = f->min_zone;
1034 else if (zone_idx >= f->max_zone)
1035 zone_idx = f->max_zone - 1;
1036 dprint(FD_ZBD, "%s(%s): starting from zone %d (offset %lld, buflen %lld)\n",
1037 __func__, f->file_name, zone_idx, io_u->offset, io_u->buflen);
1040 * Since z->mutex is the outer lock and f->zbd_info->mutex the inner
1041 * lock it can happen that the state of the zone with index zone_idx
1042 * has changed after 'z' has been assigned and before f->zbd_info->mutex
1043 * has been obtained. Hence the loop.
1048 z = &f->zbd_info->zone_info[zone_idx];
1050 zone_lock(td, f, z);
1051 pthread_mutex_lock(&f->zbd_info->mutex);
1052 if (td->o.max_open_zones == 0 && td->o.job_max_open_zones == 0)
1054 if (f->zbd_info->num_open_zones == 0) {
1055 pthread_mutex_unlock(&f->zbd_info->mutex);
1056 pthread_mutex_unlock(&z->mutex);
1057 dprint(FD_ZBD, "%s(%s): no zones are open\n",
1058 __func__, f->file_name);
1063 * List of opened zones is per-device, shared across all threads.
1064 * Start with quasi-random candidate zone.
1065 * Ignore zones which don't belong to thread's offset/size area.
1067 open_zone_idx = pick_random_zone_idx(f, io_u);
1068 assert(open_zone_idx < f->zbd_info->num_open_zones);
1069 tmp_idx = open_zone_idx;
1070 for (i = 0; i < f->zbd_info->num_open_zones; i++) {
1073 if (tmp_idx >= f->zbd_info->num_open_zones)
1075 tmpz = f->zbd_info->open_zones[tmp_idx];
1076 if (f->min_zone <= tmpz && tmpz < f->max_zone) {
1077 open_zone_idx = tmp_idx;
1078 goto found_candidate_zone;
1084 dprint(FD_ZBD, "%s(%s): no candidate zone\n",
1085 __func__, f->file_name);
1086 pthread_mutex_unlock(&f->zbd_info->mutex);
1087 pthread_mutex_unlock(&z->mutex);
1090 found_candidate_zone:
1091 new_zone_idx = f->zbd_info->open_zones[open_zone_idx];
1092 if (new_zone_idx == zone_idx)
1094 zone_idx = new_zone_idx;
1095 pthread_mutex_unlock(&f->zbd_info->mutex);
1096 pthread_mutex_unlock(&z->mutex);
1099 /* Both z->mutex and f->zbd_info->mutex are held. */
1102 if (z->wp + min_bs <= zbd_zone_capacity_end(z)) {
1103 pthread_mutex_unlock(&f->zbd_info->mutex);
1106 dprint(FD_ZBD, "%s(%s): closing zone %d\n", __func__, f->file_name,
1108 if (td->o.max_open_zones || td->o.job_max_open_zones)
1109 zbd_close_zone(td, f, open_zone_idx);
1110 pthread_mutex_unlock(&f->zbd_info->mutex);
1112 /* Only z->mutex is held. */
1114 /* Zone 'z' is full, so try to open a new zone. */
1115 for (i = f->io_size / f->zbd_info->zone_size; i > 0; i--) {
1117 pthread_mutex_unlock(&z->mutex);
1119 if (!is_valid_offset(f, z->start)) {
1121 zone_idx = f->min_zone;
1122 z = &f->zbd_info->zone_info[zone_idx];
1124 assert(is_valid_offset(f, z->start));
1125 zone_lock(td, f, z);
1128 if (zbd_open_zone(td, io_u, zone_idx))
1132 /* Only z->mutex is held. */
1134 /* Check whether the write fits in any of the already opened zones. */
1135 pthread_mutex_lock(&f->zbd_info->mutex);
1136 for (i = 0; i < f->zbd_info->num_open_zones; i++) {
1137 zone_idx = f->zbd_info->open_zones[i];
1138 if (zone_idx < f->min_zone || zone_idx >= f->max_zone)
1140 pthread_mutex_unlock(&f->zbd_info->mutex);
1141 pthread_mutex_unlock(&z->mutex);
1143 z = &f->zbd_info->zone_info[zone_idx];
1145 zone_lock(td, f, z);
1146 if (z->wp + min_bs <= zbd_zone_capacity_end(z))
1148 pthread_mutex_lock(&f->zbd_info->mutex);
1150 pthread_mutex_unlock(&f->zbd_info->mutex);
1151 pthread_mutex_unlock(&z->mutex);
1152 dprint(FD_ZBD, "%s(%s): did not open another zone\n", __func__,
1157 dprint(FD_ZBD, "%s(%s): returning zone %d\n", __func__, f->file_name,
1159 io_u->offset = z->start;
1163 /* The caller must hold z->mutex. */
1164 static struct fio_zone_info *zbd_replay_write_order(struct thread_data *td,
1166 struct fio_zone_info *z)
1168 const struct fio_file *f = io_u->file;
1169 const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
1171 if (!zbd_open_zone(td, io_u, z - f->zbd_info->zone_info)) {
1172 pthread_mutex_unlock(&z->mutex);
1173 z = zbd_convert_to_open_zone(td, io_u);
1177 if (z->verify_block * min_bs >= z->capacity)
1178 log_err("%s: %d * %d >= %llu\n", f->file_name, z->verify_block,
1179 min_bs, (unsigned long long)z->capacity);
1180 io_u->offset = z->start + z->verify_block++ * min_bs;
1185 * Find another zone for which @io_u fits below the write pointer. Start
1186 * searching in zones @zb + 1 .. @zl and continue searching in zones
1189 * Either returns NULL or returns a zone pointer and holds the mutex for that
1192 static struct fio_zone_info *
1193 zbd_find_zone(struct thread_data *td, struct io_u *io_u,
1194 struct fio_zone_info *zb, struct fio_zone_info *zl)
1196 const uint32_t min_bs = td->o.min_bs[io_u->ddir];
1197 struct fio_file *f = io_u->file;
1198 struct fio_zone_info *z1, *z2;
1199 const struct fio_zone_info *const zf =
1200 &f->zbd_info->zone_info[zbd_zone_idx(f, f->file_offset)];
1203 * Skip to the next non-empty zone in case of sequential I/O and to
1204 * the nearest non-empty zone in case of random I/O.
1206 for (z1 = zb + 1, z2 = zb - 1; z1 < zl || z2 >= zf; z1++, z2--) {
1207 if (z1 < zl && z1->cond != ZBD_ZONE_COND_OFFLINE) {
1208 zone_lock(td, f, z1);
1209 if (z1->start + min_bs <= z1->wp)
1211 pthread_mutex_unlock(&z1->mutex);
1212 } else if (!td_random(td)) {
1215 if (td_random(td) && z2 >= zf &&
1216 z2->cond != ZBD_ZONE_COND_OFFLINE) {
1217 zone_lock(td, f, z2);
1218 if (z2->start + min_bs <= z2->wp)
1220 pthread_mutex_unlock(&z2->mutex);
1223 dprint(FD_ZBD, "%s: adjusting random read offset failed\n",
1229 * zbd_queue_io - update the write pointer of a sequential zone
1231 * @success: Whether or not the I/O unit has been queued successfully
1232 * @q: queueing status (busy, completed or queued).
1234 * For write and trim operations, update the write pointer of the I/O unit
1237 static void zbd_queue_io(struct io_u *io_u, int q, bool success)
1239 const struct fio_file *f = io_u->file;
1240 struct zoned_block_device_info *zbd_info = f->zbd_info;
1241 struct fio_zone_info *z;
1248 zone_idx = zbd_zone_idx(f, io_u->offset);
1249 assert(zone_idx < zbd_info->nr_zones);
1250 z = &zbd_info->zone_info[zone_idx];
1252 if (!zbd_zone_swr(z))
1259 "%s: queued I/O (%lld, %llu) for zone %u\n",
1260 f->file_name, io_u->offset, io_u->buflen, zone_idx);
1262 switch (io_u->ddir) {
1264 zone_end = min((uint64_t)(io_u->offset + io_u->buflen),
1265 zbd_zone_capacity_end(z));
1266 pthread_mutex_lock(&zbd_info->mutex);
1268 * z->wp > zone_end means that one or more I/O errors
1271 if (z->wp <= zone_end)
1272 zbd_info->sectors_with_data += zone_end - z->wp;
1273 pthread_mutex_unlock(&zbd_info->mutex);
1277 assert(z->wp == z->start);
1284 if (!success || q != FIO_Q_QUEUED) {
1285 /* BUSY or COMPLETED: unlock the zone */
1286 pthread_mutex_unlock(&z->mutex);
1287 io_u->zbd_put_io = NULL;
1292 * zbd_put_io - Unlock an I/O unit target zone lock
1295 static void zbd_put_io(const struct io_u *io_u)
1297 const struct fio_file *f = io_u->file;
1298 struct zoned_block_device_info *zbd_info = f->zbd_info;
1299 struct fio_zone_info *z;
1306 zone_idx = zbd_zone_idx(f, io_u->offset);
1307 assert(zone_idx < zbd_info->nr_zones);
1308 z = &zbd_info->zone_info[zone_idx];
1310 if (!zbd_zone_swr(z))
1314 "%s: terminate I/O (%lld, %llu) for zone %u\n",
1315 f->file_name, io_u->offset, io_u->buflen, zone_idx);
1317 ret = pthread_mutex_unlock(&z->mutex);
1323 * Windows and MacOS do not define this.
1326 #define EREMOTEIO 121 /* POSIX value */
1329 bool zbd_unaligned_write(int error_code)
1331 switch (error_code) {
1340 * setup_zbd_zone_mode - handle zoneskip as necessary for ZBD drives
1341 * @td: FIO thread data.
1342 * @io_u: FIO I/O unit.
1344 * For sequential workloads, change the file offset to skip zoneskip bytes when
1345 * no more IO can be performed in the current zone.
1346 * - For read workloads, zoneskip is applied when the io has reached the end of
1347 * the zone or the zone write position (when td->o.read_beyond_wp is false).
1348 * - For write workloads, zoneskip is applied when the zone is full.
1349 * This applies only to read and write operations.
1351 void setup_zbd_zone_mode(struct thread_data *td, struct io_u *io_u)
1353 struct fio_file *f = io_u->file;
1354 enum fio_ddir ddir = io_u->ddir;
1355 struct fio_zone_info *z;
1358 assert(td->o.zone_mode == ZONE_MODE_ZBD);
1359 assert(td->o.zone_size);
1361 zone_idx = zbd_zone_idx(f, f->last_pos[ddir]);
1362 z = &f->zbd_info->zone_info[zone_idx];
1365 * When the zone capacity is smaller than the zone size and the I/O is
1366 * sequential write, skip to zone end if the latest position is at the
1367 * zone capacity limit.
1369 if (z->capacity < f->zbd_info->zone_size && !td_random(td) &&
1370 ddir == DDIR_WRITE &&
1371 f->last_pos[ddir] >= zbd_zone_capacity_end(z)) {
1373 "%s: Jump from zone capacity limit to zone end:"
1374 " (%lu -> %lu) for zone %u (%ld)\n",
1375 f->file_name, f->last_pos[ddir], zbd_zone_end(z),
1376 zbd_zone_nr(f->zbd_info, z), z->capacity);
1377 td->io_skip_bytes += zbd_zone_end(z) - f->last_pos[ddir];
1378 f->last_pos[ddir] = zbd_zone_end(z);
1382 * zone_skip is valid only for sequential workloads.
1384 if (td_random(td) || !td->o.zone_skip)
1388 * It is time to switch to a new zone if:
1389 * - zone_bytes == zone_size bytes have already been accessed
1390 * - The last position reached the end of the current zone.
1391 * - For reads with td->o.read_beyond_wp == false, the last position
1392 * reached the zone write pointer.
1394 if (td->zone_bytes >= td->o.zone_size ||
1395 f->last_pos[ddir] >= zbd_zone_end(z) ||
1396 (ddir == DDIR_READ &&
1397 (!td->o.read_beyond_wp) && f->last_pos[ddir] >= z->wp)) {
1402 f->file_offset += td->o.zone_size + td->o.zone_skip;
1405 * Wrap from the beginning, if we exceed the file size
1407 if (f->file_offset >= f->real_file_size)
1408 f->file_offset = get_start_offset(td, f);
1410 f->last_pos[ddir] = f->file_offset;
1411 td->io_skip_bytes += td->o.zone_skip;
1416 * zbd_adjust_ddir - Adjust an I/O direction for zonemode=zbd.
1418 * @td: FIO thread data.
1419 * @io_u: FIO I/O unit.
1420 * @ddir: I/O direction before adjustment.
1422 * Return adjusted I/O direction.
1424 enum fio_ddir zbd_adjust_ddir(struct thread_data *td, struct io_u *io_u,
1428 * In case read direction is chosen for the first random I/O, fio with
1429 * zonemode=zbd stops because no data can be read from zoned block
1430 * devices with all empty zones. Overwrite the first I/O direction as
1431 * write to make sure data to read exists.
1433 if (ddir != DDIR_READ || !td_rw(td))
1436 if (io_u->file->zbd_info->sectors_with_data ||
1437 td->o.read_beyond_wp)
1444 * zbd_adjust_block - adjust the offset and length as necessary for ZBD drives
1445 * @td: FIO thread data.
1446 * @io_u: FIO I/O unit.
1448 * Locking strategy: returns with z->mutex locked if and only if z refers
1449 * to a sequential zone and if io_u_accept is returned. z is the zone that
1450 * corresponds to io_u->offset at the end of this function.
1452 enum io_u_action zbd_adjust_block(struct thread_data *td, struct io_u *io_u)
1454 struct fio_file *f = io_u->file;
1455 uint32_t zone_idx_b;
1456 struct fio_zone_info *zb, *zl, *orig_zb;
1457 uint32_t orig_len = io_u->buflen;
1458 uint32_t min_bs = td->o.min_bs[io_u->ddir];
1466 assert(is_valid_offset(f, io_u->offset));
1467 assert(io_u->buflen);
1468 zone_idx_b = zbd_zone_idx(f, io_u->offset);
1469 zb = &f->zbd_info->zone_info[zone_idx_b];
1472 /* Accept the I/O offset for conventional zones. */
1473 if (!zbd_zone_swr(zb))
1477 * Accept the I/O offset for reads if reading beyond the write pointer
1480 if (zb->cond != ZBD_ZONE_COND_OFFLINE &&
1481 io_u->ddir == DDIR_READ && td->o.read_beyond_wp)
1486 zone_lock(td, f, zb);
1488 switch (io_u->ddir) {
1490 if (td->runstate == TD_VERIFYING) {
1492 zb = zbd_replay_write_order(td, io_u, zb);
1496 * Check that there is enough written data in the zone to do an
1497 * I/O of at least min_bs B. If there isn't, find a new zone for
1500 range = zb->cond != ZBD_ZONE_COND_OFFLINE ?
1501 zb->wp - zb->start : 0;
1502 if (range < min_bs ||
1503 ((!td_random(td)) && (io_u->offset + min_bs > zb->wp))) {
1504 pthread_mutex_unlock(&zb->mutex);
1505 zl = &f->zbd_info->zone_info[zbd_zone_idx(f,
1506 f->file_offset + f->io_size)];
1507 zb = zbd_find_zone(td, io_u, zb, zl);
1510 "%s: zbd_find_zone(%lld, %llu) failed\n",
1511 f->file_name, io_u->offset,
1516 * zbd_find_zone() returned a zone with a range of at
1519 range = zb->wp - zb->start;
1520 assert(range >= min_bs);
1523 io_u->offset = zb->start;
1526 * Make sure the I/O is within the zone valid data range while
1527 * maximizing the I/O size and preserving randomness.
1529 if (range <= io_u->buflen)
1530 io_u->offset = zb->start;
1531 else if (td_random(td))
1532 io_u->offset = zb->start +
1533 ((io_u->offset - orig_zb->start) %
1534 (range - io_u->buflen)) / min_bs * min_bs;
1536 * Make sure the I/O does not cross over the zone wp position.
1538 new_len = min((unsigned long long)io_u->buflen,
1539 (unsigned long long)(zb->wp - io_u->offset));
1540 new_len = new_len / min_bs * min_bs;
1541 if (new_len < io_u->buflen) {
1542 io_u->buflen = new_len;
1543 dprint(FD_IO, "Changed length from %u into %llu\n",
1544 orig_len, io_u->buflen);
1546 assert(zb->start <= io_u->offset);
1547 assert(io_u->offset + io_u->buflen <= zb->wp);
1550 if (io_u->buflen > f->zbd_info->zone_size)
1552 if (!zbd_open_zone(td, io_u, zone_idx_b)) {
1553 pthread_mutex_unlock(&zb->mutex);
1554 zb = zbd_convert_to_open_zone(td, io_u);
1557 zone_idx_b = zb - f->zbd_info->zone_info;
1559 /* Check whether the zone reset threshold has been exceeded */
1560 if (td->o.zrf.u.f) {
1561 if (f->zbd_info->sectors_with_data >=
1562 f->io_size * td->o.zrt.u.f &&
1563 zbd_dec_and_reset_write_cnt(td, f)) {
1567 /* Reset the zone pointer if necessary */
1568 if (zb->reset_zone || zbd_zone_full(f, zb, min_bs)) {
1569 assert(td->o.verify == VERIFY_NONE);
1571 * Since previous write requests may have been submitted
1572 * asynchronously and since we will submit the zone
1573 * reset synchronously, wait until previously submitted
1574 * write requests have completed before issuing a
1579 if (zbd_reset_zone(td, f, zb) < 0)
1582 if (zb->capacity < min_bs) {
1583 log_err("zone capacity %llu smaller than minimum block size %d\n",
1584 (unsigned long long)zb->capacity,
1589 /* Make writes occur at the write pointer */
1590 assert(!zbd_zone_full(f, zb, min_bs));
1591 io_u->offset = zb->wp;
1592 if (!is_valid_offset(f, io_u->offset)) {
1593 dprint(FD_ZBD, "Dropped request with offset %llu\n",
1598 * Make sure that the buflen is a multiple of the minimal
1599 * block size. Give up if shrinking would make the request too
1602 new_len = min((unsigned long long)io_u->buflen,
1603 zbd_zone_capacity_end(zb) - io_u->offset);
1604 new_len = new_len / min_bs * min_bs;
1605 if (new_len == io_u->buflen)
1607 if (new_len >= min_bs) {
1608 io_u->buflen = new_len;
1609 dprint(FD_IO, "Changed length from %u into %llu\n",
1610 orig_len, io_u->buflen);
1613 log_err("Zone remainder %lld smaller than minimum block size %d\n",
1614 (zbd_zone_capacity_end(zb) - io_u->offset),
1621 case DDIR_SYNC_FILE_RANGE:
1632 assert(zb->cond != ZBD_ZONE_COND_OFFLINE);
1633 assert(!io_u->zbd_queue_io);
1634 assert(!io_u->zbd_put_io);
1635 io_u->zbd_queue_io = zbd_queue_io;
1636 io_u->zbd_put_io = zbd_put_io;
1641 pthread_mutex_unlock(&zb->mutex);
1645 /* Return a string with ZBD statistics */
1646 char *zbd_write_status(const struct thread_stat *ts)
1650 if (asprintf(&res, "; %llu zone resets", (unsigned long long) ts->nr_zone_resets) < 0)