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_end - Return zone end location
136 * @z: zone info pointer.
138 static inline uint64_t zbd_zone_end(const struct fio_zone_info *z)
144 * zbd_zone_capacity_end - Return zone capacity limit end location
145 * @z: zone info pointer.
147 static inline uint64_t zbd_zone_capacity_end(const struct fio_zone_info *z)
149 return z->start + z->capacity;
153 * zbd_zone_full - verify whether a minimum number of bytes remain in a zone
155 * @z: zone info pointer.
156 * @required: minimum number of bytes that must remain in a zone.
158 * The caller must hold z->mutex.
160 static bool zbd_zone_full(const struct fio_file *f, struct fio_zone_info *z,
163 assert((required & 511) == 0);
166 z->wp + required > zbd_zone_capacity_end(z);
169 static void zone_lock(struct thread_data *td, struct fio_file *f, struct fio_zone_info *z)
171 struct zoned_block_device_info *zbd = f->zbd_info;
172 uint32_t nz = z - zbd->zone_info;
174 /* A thread should never lock zones outside its working area. */
175 assert(f->min_zone <= nz && nz < f->max_zone);
180 * Lock the io_u target zone. The zone will be unlocked if io_u offset
181 * is changed or when io_u completes and zbd_put_io() executed.
182 * To avoid multiple jobs doing asynchronous I/Os from deadlocking each
183 * other waiting for zone locks when building an io_u batch, first
184 * only trylock the zone. If the zone is already locked by another job,
185 * process the currently queued I/Os so that I/O progress is made and
188 if (pthread_mutex_trylock(&z->mutex) != 0) {
189 if (!td_ioengine_flagged(td, FIO_SYNCIO))
191 pthread_mutex_lock(&z->mutex);
195 static inline void zone_unlock(struct fio_zone_info *z)
200 ret = pthread_mutex_unlock(&z->mutex);
204 static bool is_valid_offset(const struct fio_file *f, uint64_t offset)
206 return (uint64_t)(offset - f->file_offset) < f->io_size;
209 static inline struct fio_zone_info *get_zone(const struct fio_file *f,
210 unsigned int zone_nr)
212 return &f->zbd_info->zone_info[zone_nr];
215 /* Verify whether direct I/O is used for all host-managed zoned drives. */
216 static bool zbd_using_direct_io(void)
218 struct thread_data *td;
223 if (td->o.odirect || !(td->o.td_ddir & TD_DDIR_WRITE))
225 for_each_file(td, f, j) {
227 f->zbd_info->model == ZBD_HOST_MANAGED)
235 /* Whether or not the I/O range for f includes one or more sequential zones */
236 static bool zbd_is_seq_job(struct fio_file *f)
238 uint32_t zone_idx, zone_idx_b, zone_idx_e;
243 zone_idx_b = zbd_zone_idx(f, f->file_offset);
244 zone_idx_e = zbd_zone_idx(f, f->file_offset + f->io_size - 1);
245 for (zone_idx = zone_idx_b; zone_idx <= zone_idx_e; zone_idx++)
246 if (get_zone(f, zone_idx)->has_wp)
253 * Verify whether offset and size parameters are aligned with zone boundaries.
255 static bool zbd_verify_sizes(void)
257 const struct fio_zone_info *z;
258 struct thread_data *td;
260 uint64_t new_offset, new_end;
265 for_each_file(td, f, j) {
268 if (f->file_offset >= f->real_file_size)
270 if (!zbd_is_seq_job(f))
273 if (!td->o.zone_size) {
274 td->o.zone_size = f->zbd_info->zone_size;
275 if (!td->o.zone_size) {
276 log_err("%s: invalid 0 zone size\n",
280 } else if (td->o.zone_size != f->zbd_info->zone_size) {
281 log_err("%s: job parameter zonesize %llu does not match disk zone size %llu.\n",
282 f->file_name, (unsigned long long) td->o.zone_size,
283 (unsigned long long) f->zbd_info->zone_size);
287 if (td->o.zone_skip &&
288 (td->o.zone_skip < td->o.zone_size ||
289 td->o.zone_skip % td->o.zone_size)) {
290 log_err("%s: zoneskip %llu is not a multiple of the device zone size %llu.\n",
291 f->file_name, (unsigned long long) td->o.zone_skip,
292 (unsigned long long) td->o.zone_size);
296 zone_idx = zbd_zone_idx(f, f->file_offset);
297 z = get_zone(f, zone_idx);
298 if ((f->file_offset != z->start) &&
299 (td->o.td_ddir != TD_DDIR_READ)) {
300 new_offset = zbd_zone_end(z);
301 if (new_offset >= f->file_offset + f->io_size) {
302 log_info("%s: io_size must be at least one zone\n",
306 log_info("%s: rounded up offset from %llu to %llu\n",
307 f->file_name, (unsigned long long) f->file_offset,
308 (unsigned long long) new_offset);
309 f->io_size -= (new_offset - f->file_offset);
310 f->file_offset = new_offset;
312 zone_idx = zbd_zone_idx(f, f->file_offset + f->io_size);
313 z = get_zone(f, zone_idx);
315 if ((td->o.td_ddir != TD_DDIR_READ) &&
316 (f->file_offset + f->io_size != new_end)) {
317 if (new_end <= f->file_offset) {
318 log_info("%s: io_size must be at least one zone\n",
322 log_info("%s: rounded down io_size from %llu to %llu\n",
323 f->file_name, (unsigned long long) f->io_size,
324 (unsigned long long) new_end - f->file_offset);
325 f->io_size = new_end - f->file_offset;
328 f->min_zone = zbd_zone_idx(f, f->file_offset);
329 f->max_zone = zbd_zone_idx(f, f->file_offset + f->io_size);
330 assert(f->min_zone < f->max_zone);
337 static bool zbd_verify_bs(void)
339 struct thread_data *td;
345 for_each_file(td, f, j) {
348 zone_size = f->zbd_info->zone_size;
349 for (k = 0; k < FIO_ARRAY_SIZE(td->o.bs); k++) {
350 if (td->o.verify != VERIFY_NONE &&
351 zone_size % td->o.bs[k] != 0) {
352 log_info("%s: block size %llu is not a divisor of the zone size %d\n",
353 f->file_name, td->o.bs[k],
363 static int ilog2(uint64_t i)
375 * Initialize f->zbd_info for devices that are not zoned block devices. This
376 * allows to execute a ZBD workload against a non-ZBD device.
378 static int init_zone_info(struct thread_data *td, struct fio_file *f)
381 struct fio_zone_info *p;
382 uint64_t zone_size = td->o.zone_size;
383 uint64_t zone_capacity = td->o.zone_capacity;
384 struct zoned_block_device_info *zbd_info = NULL;
387 if (zone_size == 0) {
388 log_err("%s: Specifying the zone size is mandatory for regular block devices with --zonemode=zbd\n\n",
393 if (zone_size < 512) {
394 log_err("%s: zone size must be at least 512 bytes for --zonemode=zbd\n\n",
399 if (zone_capacity == 0)
400 zone_capacity = zone_size;
402 if (zone_capacity > zone_size) {
403 log_err("%s: job parameter zonecapacity %llu is larger than zone size %llu\n",
404 f->file_name, (unsigned long long) td->o.zone_capacity,
405 (unsigned long long) td->o.zone_size);
409 nr_zones = (f->real_file_size + zone_size - 1) / zone_size;
410 zbd_info = scalloc(1, sizeof(*zbd_info) +
411 (nr_zones + 1) * sizeof(zbd_info->zone_info[0]));
415 mutex_init_pshared(&zbd_info->mutex);
416 zbd_info->refcount = 1;
417 p = &zbd_info->zone_info[0];
418 for (i = 0; i < nr_zones; i++, p++) {
419 mutex_init_pshared_with_type(&p->mutex,
420 PTHREAD_MUTEX_RECURSIVE);
421 p->start = i * zone_size;
423 p->type = ZBD_ZONE_TYPE_SWR;
424 p->cond = ZBD_ZONE_COND_EMPTY;
425 p->capacity = zone_capacity;
429 p->start = nr_zones * zone_size;
431 f->zbd_info = zbd_info;
432 f->zbd_info->zone_size = zone_size;
433 f->zbd_info->zone_size_log2 = is_power_of_2(zone_size) ?
434 ilog2(zone_size) : 0;
435 f->zbd_info->nr_zones = nr_zones;
440 * Maximum number of zones to report in one operation.
442 #define ZBD_REPORT_MAX_ZONES 8192U
445 * Parse the device zone report and store it in f->zbd_info. Must be called
446 * only for devices that are zoned, namely those with a model != ZBD_NONE.
448 static int parse_zone_info(struct thread_data *td, struct fio_file *f)
451 struct zbd_zone *zones, *z;
452 struct fio_zone_info *p;
453 uint64_t zone_size, offset;
454 struct zoned_block_device_info *zbd_info = NULL;
455 int i, j, ret = -ENOMEM;
457 zones = calloc(ZBD_REPORT_MAX_ZONES, sizeof(struct zbd_zone));
461 nrz = zbd_report_zones(td, f, 0, zones, ZBD_REPORT_MAX_ZONES);
464 log_info("fio: report zones (offset 0) failed for %s (%d).\n",
469 zone_size = zones[0].len;
470 nr_zones = (f->real_file_size + zone_size - 1) / zone_size;
472 if (td->o.zone_size == 0) {
473 td->o.zone_size = zone_size;
474 } else if (td->o.zone_size != zone_size) {
475 log_err("fio: %s job parameter zonesize %llu does not match disk zone size %llu.\n",
476 f->file_name, (unsigned long long) td->o.zone_size,
477 (unsigned long long) zone_size);
482 dprint(FD_ZBD, "Device %s has %d zones of size %llu KB\n", f->file_name,
483 nr_zones, (unsigned long long) zone_size / 1024);
485 zbd_info = scalloc(1, sizeof(*zbd_info) +
486 (nr_zones + 1) * sizeof(zbd_info->zone_info[0]));
489 mutex_init_pshared(&zbd_info->mutex);
490 zbd_info->refcount = 1;
491 p = &zbd_info->zone_info[0];
492 for (offset = 0, j = 0; j < nr_zones;) {
494 for (i = 0; i < nrz; i++, j++, z++, p++) {
495 mutex_init_pshared_with_type(&p->mutex,
496 PTHREAD_MUTEX_RECURSIVE);
498 p->capacity = z->capacity;
500 case ZBD_ZONE_COND_NOT_WP:
501 case ZBD_ZONE_COND_FULL:
502 p->wp = p->start + p->capacity;
505 assert(z->start <= z->wp);
506 assert(z->wp <= z->start + zone_size);
512 case ZBD_ZONE_TYPE_SWR:
521 if (j > 0 && p->start != p[-1].start + zone_size) {
522 log_info("%s: invalid zone data\n",
529 offset = z->start + z->len;
532 nrz = zbd_report_zones(td, f, offset, zones,
533 min((uint32_t)(nr_zones - j),
534 ZBD_REPORT_MAX_ZONES));
537 log_info("fio: report zones (offset %llu) failed for %s (%d).\n",
538 (unsigned long long)offset,
545 zbd_info->zone_info[nr_zones].start = offset;
547 f->zbd_info = zbd_info;
548 f->zbd_info->zone_size = zone_size;
549 f->zbd_info->zone_size_log2 = is_power_of_2(zone_size) ?
550 ilog2(zone_size) : 0;
551 f->zbd_info->nr_zones = nr_zones;
562 * Allocate zone information and store it into f->zbd_info if zonemode=zbd.
564 * Returns 0 upon success and a negative error code upon failure.
566 static int zbd_create_zone_info(struct thread_data *td, struct fio_file *f)
568 enum zbd_zoned_model zbd_model;
571 assert(td->o.zone_mode == ZONE_MODE_ZBD);
573 ret = zbd_get_zoned_model(td, f, &zbd_model);
581 case ZBD_HOST_MANAGED:
582 ret = parse_zone_info(td, f);
585 ret = init_zone_info(td, f);
588 td_verror(td, EINVAL, "Unsupported zoned model");
589 log_err("Unsupported zoned model\n");
594 f->zbd_info->model = zbd_model;
595 f->zbd_info->max_open_zones = td->o.max_open_zones;
600 void zbd_free_zone_info(struct fio_file *f)
606 pthread_mutex_lock(&f->zbd_info->mutex);
607 refcount = --f->zbd_info->refcount;
608 pthread_mutex_unlock(&f->zbd_info->mutex);
610 assert((int32_t)refcount >= 0);
617 * Initialize f->zbd_info.
619 * Returns 0 upon success and a negative error code upon failure.
621 * Note: this function can only work correctly if it is called before the first
624 static int zbd_init_zone_info(struct thread_data *td, struct fio_file *file)
626 struct thread_data *td2;
630 for_each_td(td2, i) {
631 for_each_file(td2, f2, j) {
632 if (td2 == td && f2 == file)
635 strcmp(f2->file_name, file->file_name) != 0)
637 file->zbd_info = f2->zbd_info;
638 file->zbd_info->refcount++;
643 ret = zbd_create_zone_info(td, file);
645 td_verror(td, -ret, "zbd_create_zone_info() failed");
649 static bool zbd_open_zone(struct thread_data *td, const struct fio_file *f,
651 static int zbd_reset_zone(struct thread_data *td, struct fio_file *f,
652 struct fio_zone_info *z);
654 int zbd_setup_files(struct thread_data *td)
659 for_each_file(td, f, i) {
660 if (zbd_init_zone_info(td, f))
664 if (!zbd_using_direct_io()) {
665 log_err("Using direct I/O is mandatory for writing to ZBD drives\n\n");
669 if (!zbd_verify_sizes())
672 if (!zbd_verify_bs())
675 for_each_file(td, f, i) {
676 struct zoned_block_device_info *zbd = f->zbd_info;
677 struct fio_zone_info *z;
683 zbd->max_open_zones = zbd->max_open_zones ?: ZBD_MAX_OPEN_ZONES;
685 if (td->o.max_open_zones > 0 &&
686 zbd->max_open_zones != td->o.max_open_zones) {
687 log_err("Different 'max_open_zones' values\n");
690 if (zbd->max_open_zones > ZBD_MAX_OPEN_ZONES) {
691 log_err("'max_open_zones' value is limited by %u\n", ZBD_MAX_OPEN_ZONES);
695 for (zi = f->min_zone; zi < f->max_zone; zi++) {
696 z = &zbd->zone_info[zi];
697 if (z->cond != ZBD_ZONE_COND_IMP_OPEN &&
698 z->cond != ZBD_ZONE_COND_EXP_OPEN)
700 if (zbd_open_zone(td, f, zi))
703 * If the number of open zones exceeds specified limits,
704 * reset all extra open zones.
706 if (zbd_reset_zone(td, f, z) < 0) {
707 log_err("Failed to reest zone %d\n", zi);
716 static inline unsigned int zbd_zone_nr(const struct fio_file *f,
717 struct fio_zone_info *zone)
719 return zone - f->zbd_info->zone_info;
723 * zbd_reset_zone - reset the write pointer of a single zone
724 * @td: FIO thread data.
725 * @f: FIO file associated with the disk for which to reset a write pointer.
728 * Returns 0 upon success and a negative error code upon failure.
730 * The caller must hold z->mutex.
732 static int zbd_reset_zone(struct thread_data *td, struct fio_file *f,
733 struct fio_zone_info *z)
735 uint64_t offset = z->start;
736 uint64_t length = (z+1)->start - offset;
737 uint64_t data_in_zone = z->wp - z->start;
743 assert(is_valid_offset(f, offset + length - 1));
745 dprint(FD_ZBD, "%s: resetting wp of zone %u.\n", f->file_name,
747 switch (f->zbd_info->model) {
749 case ZBD_HOST_MANAGED:
750 ret = zbd_reset_wp(td, f, offset, length);
758 pthread_mutex_lock(&f->zbd_info->mutex);
759 f->zbd_info->sectors_with_data -= data_in_zone;
760 f->zbd_info->wp_sectors_with_data -= data_in_zone;
761 pthread_mutex_unlock(&f->zbd_info->mutex);
765 td->ts.nr_zone_resets++;
770 /* The caller must hold f->zbd_info->mutex */
771 static void zbd_close_zone(struct thread_data *td, const struct fio_file *f,
772 unsigned int zone_idx)
774 uint32_t open_zone_idx = 0;
776 for (; open_zone_idx < f->zbd_info->num_open_zones; open_zone_idx++) {
777 if (f->zbd_info->open_zones[open_zone_idx] == zone_idx)
780 if (open_zone_idx == f->zbd_info->num_open_zones) {
781 dprint(FD_ZBD, "%s: zone %d is not open\n",
782 f->file_name, zone_idx);
786 dprint(FD_ZBD, "%s: closing zone %d\n", f->file_name, zone_idx);
787 memmove(f->zbd_info->open_zones + open_zone_idx,
788 f->zbd_info->open_zones + open_zone_idx + 1,
789 (ZBD_MAX_OPEN_ZONES - (open_zone_idx + 1)) *
790 sizeof(f->zbd_info->open_zones[0]));
791 f->zbd_info->num_open_zones--;
792 td->num_open_zones--;
793 get_zone(f, zone_idx)->open = 0;
797 * Reset a range of zones. Returns 0 upon success and 1 upon failure.
798 * @td: fio thread data.
799 * @f: fio file for which to reset zones
800 * @zb: first zone to reset.
801 * @ze: first zone not to reset.
802 * @all_zones: whether to reset all zones or only those zones for which the
803 * write pointer is not a multiple of td->o.min_bs[DDIR_WRITE].
805 static int zbd_reset_zones(struct thread_data *td, struct fio_file *f,
806 struct fio_zone_info *const zb,
807 struct fio_zone_info *const ze, bool all_zones)
809 struct fio_zone_info *z;
810 const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
816 dprint(FD_ZBD, "%s: examining zones %u .. %u\n", f->file_name,
817 zbd_zone_nr(f, zb), zbd_zone_nr(f, ze));
818 for (z = zb; z < ze; z++) {
819 uint32_t nz = zbd_zone_nr(f, z);
825 pthread_mutex_lock(&f->zbd_info->mutex);
826 zbd_close_zone(td, f, nz);
827 pthread_mutex_unlock(&f->zbd_info->mutex);
829 reset_wp = z->wp != z->start;
831 reset_wp = z->wp % min_bs != 0;
834 dprint(FD_ZBD, "%s: resetting zone %u\n",
835 f->file_name, zbd_zone_nr(f, z));
836 if (zbd_reset_zone(td, f, z) < 0)
846 * Reset zbd_info.write_cnt, the counter that counts down towards the next
849 static void _zbd_reset_write_cnt(const struct thread_data *td,
850 const struct fio_file *f)
852 assert(0 <= td->o.zrf.u.f && td->o.zrf.u.f <= 1);
854 f->zbd_info->write_cnt = td->o.zrf.u.f ?
855 min(1.0 / td->o.zrf.u.f, 0.0 + UINT_MAX) : UINT_MAX;
858 static void zbd_reset_write_cnt(const struct thread_data *td,
859 const struct fio_file *f)
861 pthread_mutex_lock(&f->zbd_info->mutex);
862 _zbd_reset_write_cnt(td, f);
863 pthread_mutex_unlock(&f->zbd_info->mutex);
866 static bool zbd_dec_and_reset_write_cnt(const struct thread_data *td,
867 const struct fio_file *f)
869 uint32_t write_cnt = 0;
871 pthread_mutex_lock(&f->zbd_info->mutex);
872 assert(f->zbd_info->write_cnt);
873 if (f->zbd_info->write_cnt)
874 write_cnt = --f->zbd_info->write_cnt;
876 _zbd_reset_write_cnt(td, f);
877 pthread_mutex_unlock(&f->zbd_info->mutex);
879 return write_cnt == 0;
887 /* Calculate the number of sectors with data (swd) and perform action 'a' */
888 static uint64_t zbd_process_swd(const struct fio_file *f, enum swd_action a)
890 struct fio_zone_info *zb, *ze, *z;
894 zb = get_zone(f, f->min_zone);
895 ze = get_zone(f, f->max_zone);
896 for (z = zb; z < ze; z++) {
898 pthread_mutex_lock(&z->mutex);
899 wp_swd += z->wp - z->start;
901 swd += z->wp - z->start;
903 pthread_mutex_lock(&f->zbd_info->mutex);
906 assert(f->zbd_info->sectors_with_data == swd);
907 assert(f->zbd_info->wp_sectors_with_data == wp_swd);
910 f->zbd_info->sectors_with_data = swd;
911 f->zbd_info->wp_sectors_with_data = wp_swd;
914 pthread_mutex_unlock(&f->zbd_info->mutex);
915 for (z = zb; z < ze; z++)
923 * The swd check is useful for debugging but takes too much time to leave
924 * it enabled all the time. Hence it is disabled by default.
926 static const bool enable_check_swd = false;
928 /* Check whether the values of zbd_info.*sectors_with_data are correct. */
929 static void zbd_check_swd(const struct fio_file *f)
931 if (!enable_check_swd)
934 zbd_process_swd(f, CHECK_SWD);
937 static void zbd_init_swd(struct fio_file *f)
941 if (!enable_check_swd)
944 swd = zbd_process_swd(f, SET_SWD);
945 dprint(FD_ZBD, "%s(%s): swd = %" PRIu64 "\n", __func__, f->file_name,
949 void zbd_file_reset(struct thread_data *td, struct fio_file *f)
951 struct fio_zone_info *zb, *ze;
953 if (!f->zbd_info || !td_write(td))
956 zb = get_zone(f, f->min_zone);
957 ze = get_zone(f, f->max_zone);
960 * If data verification is enabled reset the affected zones before
961 * writing any data to avoid that a zone reset has to be issued while
962 * writing data, which causes data loss.
964 zbd_reset_zones(td, f, zb, ze, td->o.verify != VERIFY_NONE &&
965 td->runstate != TD_VERIFYING);
966 zbd_reset_write_cnt(td, f);
969 /* The caller must hold f->zbd_info->mutex. */
970 static bool is_zone_open(const struct thread_data *td, const struct fio_file *f,
971 unsigned int zone_idx)
973 struct zoned_block_device_info *zbdi = f->zbd_info;
976 assert(td->o.job_max_open_zones == 0 || td->num_open_zones <= td->o.job_max_open_zones);
977 assert(td->o.job_max_open_zones <= zbdi->max_open_zones);
978 assert(zbdi->num_open_zones <= zbdi->max_open_zones);
980 for (i = 0; i < zbdi->num_open_zones; i++)
981 if (zbdi->open_zones[i] == zone_idx)
988 * Open a ZBD zone if it was not yet open. Returns true if either the zone was
989 * already open or if opening a new zone is allowed. Returns false if the zone
990 * was not yet open and opening a new zone would cause the zone limit to be
993 static bool zbd_open_zone(struct thread_data *td, const struct fio_file *f,
996 const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
997 struct fio_zone_info *z = get_zone(f, zone_idx);
1000 if (z->cond == ZBD_ZONE_COND_OFFLINE)
1004 * Skip full zones with data verification enabled because resetting a
1005 * zone causes data loss and hence causes verification to fail.
1007 if (td->o.verify != VERIFY_NONE && zbd_zone_full(f, z, min_bs))
1010 pthread_mutex_lock(&f->zbd_info->mutex);
1011 if (is_zone_open(td, f, zone_idx)) {
1013 * If the zone is already open and going to be full by writes
1014 * in-flight, handle it as a full zone instead of an open zone.
1016 if (z->wp >= zbd_zone_capacity_end(z))
1021 /* Zero means no limit */
1022 if (td->o.job_max_open_zones > 0 &&
1023 td->num_open_zones >= td->o.job_max_open_zones)
1025 if (f->zbd_info->num_open_zones >= f->zbd_info->max_open_zones)
1027 dprint(FD_ZBD, "%s: opening zone %d\n", f->file_name, zone_idx);
1028 f->zbd_info->open_zones[f->zbd_info->num_open_zones++] = zone_idx;
1029 td->num_open_zones++;
1034 pthread_mutex_unlock(&f->zbd_info->mutex);
1038 /* Anything goes as long as it is not a constant. */
1039 static uint32_t pick_random_zone_idx(const struct fio_file *f,
1040 const struct io_u *io_u)
1042 return io_u->offset * f->zbd_info->num_open_zones / f->real_file_size;
1046 * Modify the offset of an I/O unit that does not refer to an open zone such
1047 * that it refers to an open zone. Close an open zone and open a new zone if
1048 * necessary. The open zone is searched across sequential zones.
1049 * This algorithm can only work correctly if all write pointers are
1050 * a multiple of the fio block size. The caller must neither hold z->mutex
1051 * nor f->zbd_info->mutex. Returns with z->mutex held upon success.
1053 static struct fio_zone_info *zbd_convert_to_open_zone(struct thread_data *td,
1056 const uint32_t min_bs = td->o.min_bs[io_u->ddir];
1057 struct fio_file *f = io_u->file;
1058 struct fio_zone_info *z;
1059 unsigned int open_zone_idx = -1;
1060 uint32_t zone_idx, new_zone_idx;
1062 bool wait_zone_close;
1064 assert(is_valid_offset(f, io_u->offset));
1066 if (td->o.max_open_zones || td->o.job_max_open_zones) {
1068 * This statement accesses f->zbd_info->open_zones[] on purpose
1071 zone_idx = f->zbd_info->open_zones[pick_random_zone_idx(f, io_u)];
1073 zone_idx = zbd_zone_idx(f, io_u->offset);
1075 if (zone_idx < f->min_zone)
1076 zone_idx = f->min_zone;
1077 else if (zone_idx >= f->max_zone)
1078 zone_idx = f->max_zone - 1;
1079 dprint(FD_ZBD, "%s(%s): starting from zone %d (offset %lld, buflen %lld)\n",
1080 __func__, f->file_name, zone_idx, io_u->offset, io_u->buflen);
1083 * Since z->mutex is the outer lock and f->zbd_info->mutex the inner
1084 * lock it can happen that the state of the zone with index zone_idx
1085 * has changed after 'z' has been assigned and before f->zbd_info->mutex
1086 * has been obtained. Hence the loop.
1091 z = get_zone(f, zone_idx);
1093 zone_lock(td, f, z);
1094 pthread_mutex_lock(&f->zbd_info->mutex);
1095 if (z->cond != ZBD_ZONE_COND_OFFLINE &&
1096 td->o.max_open_zones == 0 && td->o.job_max_open_zones == 0)
1098 if (f->zbd_info->num_open_zones == 0) {
1099 dprint(FD_ZBD, "%s(%s): no zones are open\n",
1100 __func__, f->file_name);
1101 goto open_other_zone;
1105 * List of opened zones is per-device, shared across all threads.
1106 * Start with quasi-random candidate zone.
1107 * Ignore zones which don't belong to thread's offset/size area.
1109 open_zone_idx = pick_random_zone_idx(f, io_u);
1110 assert(open_zone_idx < f->zbd_info->num_open_zones);
1111 tmp_idx = open_zone_idx;
1112 for (i = 0; i < f->zbd_info->num_open_zones; i++) {
1115 if (tmp_idx >= f->zbd_info->num_open_zones)
1117 tmpz = f->zbd_info->open_zones[tmp_idx];
1118 if (f->min_zone <= tmpz && tmpz < f->max_zone) {
1119 open_zone_idx = tmp_idx;
1120 goto found_candidate_zone;
1126 dprint(FD_ZBD, "%s(%s): no candidate zone\n",
1127 __func__, f->file_name);
1128 pthread_mutex_unlock(&f->zbd_info->mutex);
1132 found_candidate_zone:
1133 new_zone_idx = f->zbd_info->open_zones[open_zone_idx];
1134 if (new_zone_idx == zone_idx)
1136 zone_idx = new_zone_idx;
1137 pthread_mutex_unlock(&f->zbd_info->mutex);
1141 /* Both z->mutex and f->zbd_info->mutex are held. */
1144 if (z->wp + min_bs <= zbd_zone_capacity_end(z)) {
1145 pthread_mutex_unlock(&f->zbd_info->mutex);
1150 /* Check if number of open zones reaches one of limits. */
1152 f->zbd_info->num_open_zones == f->max_zone - f->min_zone ||
1153 (td->o.max_open_zones &&
1154 f->zbd_info->num_open_zones == td->o.max_open_zones) ||
1155 (td->o.job_max_open_zones &&
1156 td->num_open_zones == td->o.job_max_open_zones);
1158 pthread_mutex_unlock(&f->zbd_info->mutex);
1160 /* Only z->mutex is held. */
1163 * When number of open zones reaches to one of limits, wait for
1164 * zone close before opening a new zone.
1166 if (wait_zone_close) {
1167 dprint(FD_ZBD, "%s(%s): quiesce to allow open zones to close\n",
1168 __func__, f->file_name);
1172 /* Zone 'z' is full, so try to open a new zone. */
1173 for (i = f->io_size / f->zbd_info->zone_size; i > 0; i--) {
1178 if (!is_valid_offset(f, z->start)) {
1180 zone_idx = f->min_zone;
1181 z = get_zone(f, zone_idx);
1183 assert(is_valid_offset(f, z->start));
1186 zone_lock(td, f, z);
1189 if (zbd_open_zone(td, f, zone_idx))
1193 /* Only z->mutex is held. */
1195 /* Check whether the write fits in any of the already opened zones. */
1196 pthread_mutex_lock(&f->zbd_info->mutex);
1197 for (i = 0; i < f->zbd_info->num_open_zones; i++) {
1198 zone_idx = f->zbd_info->open_zones[i];
1199 if (zone_idx < f->min_zone || zone_idx >= f->max_zone)
1201 pthread_mutex_unlock(&f->zbd_info->mutex);
1204 z = get_zone(f, zone_idx);
1206 zone_lock(td, f, z);
1207 if (z->wp + min_bs <= zbd_zone_capacity_end(z))
1209 pthread_mutex_lock(&f->zbd_info->mutex);
1211 pthread_mutex_unlock(&f->zbd_info->mutex);
1213 dprint(FD_ZBD, "%s(%s): did not open another zone\n", __func__,
1218 dprint(FD_ZBD, "%s(%s): returning zone %d\n", __func__, f->file_name,
1220 io_u->offset = z->start;
1222 assert(z->cond != ZBD_ZONE_COND_OFFLINE);
1226 /* The caller must hold z->mutex. */
1227 static struct fio_zone_info *zbd_replay_write_order(struct thread_data *td,
1229 struct fio_zone_info *z)
1231 const struct fio_file *f = io_u->file;
1232 const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
1234 if (!zbd_open_zone(td, f, zbd_zone_nr(f, z))) {
1236 z = zbd_convert_to_open_zone(td, io_u);
1240 if (z->verify_block * min_bs >= z->capacity)
1241 log_err("%s: %d * %d >= %llu\n", f->file_name, z->verify_block,
1242 min_bs, (unsigned long long)z->capacity);
1243 io_u->offset = z->start + z->verify_block++ * min_bs;
1248 * Find another zone for which @io_u fits in the readable data in the zone.
1249 * Search in zones @zb + 1 .. @zl. For random workload, also search in zones
1252 * Either returns NULL or returns a zone pointer. When the zone has write
1253 * pointer, hold the mutex for the zone.
1255 static struct fio_zone_info *
1256 zbd_find_zone(struct thread_data *td, struct io_u *io_u,
1257 struct fio_zone_info *zb, struct fio_zone_info *zl)
1259 const uint32_t min_bs = td->o.min_bs[io_u->ddir];
1260 struct fio_file *f = io_u->file;
1261 struct fio_zone_info *z1, *z2;
1262 const struct fio_zone_info *const zf = get_zone(f, f->min_zone);
1265 * Skip to the next non-empty zone in case of sequential I/O and to
1266 * the nearest non-empty zone in case of random I/O.
1268 for (z1 = zb + 1, z2 = zb - 1; z1 < zl || z2 >= zf; z1++, z2--) {
1269 if (z1 < zl && z1->cond != ZBD_ZONE_COND_OFFLINE) {
1271 zone_lock(td, f, z1);
1272 if (z1->start + min_bs <= z1->wp)
1276 } else if (!td_random(td)) {
1279 if (td_random(td) && z2 >= zf &&
1280 z2->cond != ZBD_ZONE_COND_OFFLINE) {
1282 zone_lock(td, f, z2);
1283 if (z2->start + min_bs <= z2->wp)
1289 dprint(FD_ZBD, "%s: adjusting random read offset failed\n",
1295 * zbd_end_zone_io - update zone status at command completion
1297 * @z: zone info pointer
1299 * If the write command made the zone full, close it.
1301 * The caller must hold z->mutex.
1303 static void zbd_end_zone_io(struct thread_data *td, const struct io_u *io_u,
1304 struct fio_zone_info *z)
1306 const struct fio_file *f = io_u->file;
1308 if (io_u->ddir == DDIR_WRITE &&
1309 io_u->offset + io_u->buflen >= zbd_zone_capacity_end(z)) {
1310 pthread_mutex_lock(&f->zbd_info->mutex);
1311 zbd_close_zone(td, f, zbd_zone_nr(f, z));
1312 pthread_mutex_unlock(&f->zbd_info->mutex);
1317 * zbd_queue_io - update the write pointer of a sequential zone
1319 * @success: Whether or not the I/O unit has been queued successfully
1320 * @q: queueing status (busy, completed or queued).
1322 * For write and trim operations, update the write pointer of the I/O unit
1325 static void zbd_queue_io(struct thread_data *td, struct io_u *io_u, int q,
1328 const struct fio_file *f = io_u->file;
1329 struct zoned_block_device_info *zbd_info = f->zbd_info;
1330 struct fio_zone_info *z;
1337 zone_idx = zbd_zone_idx(f, io_u->offset);
1338 assert(zone_idx < zbd_info->nr_zones);
1339 z = get_zone(f, zone_idx);
1347 "%s: queued I/O (%lld, %llu) for zone %u\n",
1348 f->file_name, io_u->offset, io_u->buflen, zone_idx);
1350 switch (io_u->ddir) {
1352 zone_end = min((uint64_t)(io_u->offset + io_u->buflen),
1353 zbd_zone_capacity_end(z));
1354 pthread_mutex_lock(&zbd_info->mutex);
1356 * z->wp > zone_end means that one or more I/O errors
1359 if (z->wp <= zone_end) {
1360 zbd_info->sectors_with_data += zone_end - z->wp;
1361 zbd_info->wp_sectors_with_data += zone_end - z->wp;
1363 pthread_mutex_unlock(&zbd_info->mutex);
1367 assert(z->wp == z->start);
1373 if (q == FIO_Q_COMPLETED && !io_u->error)
1374 zbd_end_zone_io(td, io_u, z);
1377 if (!success || q != FIO_Q_QUEUED) {
1378 /* BUSY or COMPLETED: unlock the zone */
1380 io_u->zbd_put_io = NULL;
1385 * zbd_put_io - Unlock an I/O unit target zone lock
1388 static void zbd_put_io(struct thread_data *td, const struct io_u *io_u)
1390 const struct fio_file *f = io_u->file;
1391 struct zoned_block_device_info *zbd_info = f->zbd_info;
1392 struct fio_zone_info *z;
1398 zone_idx = zbd_zone_idx(f, io_u->offset);
1399 assert(zone_idx < zbd_info->nr_zones);
1400 z = get_zone(f, zone_idx);
1405 "%s: terminate I/O (%lld, %llu) for zone %u\n",
1406 f->file_name, io_u->offset, io_u->buflen, zone_idx);
1408 zbd_end_zone_io(td, io_u, z);
1415 * Windows and MacOS do not define this.
1418 #define EREMOTEIO 121 /* POSIX value */
1421 bool zbd_unaligned_write(int error_code)
1423 switch (error_code) {
1432 * setup_zbd_zone_mode - handle zoneskip as necessary for ZBD drives
1433 * @td: FIO thread data.
1434 * @io_u: FIO I/O unit.
1436 * For sequential workloads, change the file offset to skip zoneskip bytes when
1437 * no more IO can be performed in the current zone.
1438 * - For read workloads, zoneskip is applied when the io has reached the end of
1439 * the zone or the zone write position (when td->o.read_beyond_wp is false).
1440 * - For write workloads, zoneskip is applied when the zone is full.
1441 * This applies only to read and write operations.
1443 void setup_zbd_zone_mode(struct thread_data *td, struct io_u *io_u)
1445 struct fio_file *f = io_u->file;
1446 enum fio_ddir ddir = io_u->ddir;
1447 struct fio_zone_info *z;
1450 assert(td->o.zone_mode == ZONE_MODE_ZBD);
1451 assert(td->o.zone_size);
1453 zone_idx = zbd_zone_idx(f, f->last_pos[ddir]);
1454 z = get_zone(f, zone_idx);
1457 * When the zone capacity is smaller than the zone size and the I/O is
1458 * sequential write, skip to zone end if the latest position is at the
1459 * zone capacity limit.
1461 if (z->capacity < f->zbd_info->zone_size && !td_random(td) &&
1462 ddir == DDIR_WRITE &&
1463 f->last_pos[ddir] >= zbd_zone_capacity_end(z)) {
1465 "%s: Jump from zone capacity limit to zone end:"
1466 " (%llu -> %llu) for zone %u (%llu)\n",
1467 f->file_name, (unsigned long long) f->last_pos[ddir],
1468 (unsigned long long) zbd_zone_end(z), zone_idx,
1469 (unsigned long long) z->capacity);
1470 td->io_skip_bytes += zbd_zone_end(z) - f->last_pos[ddir];
1471 f->last_pos[ddir] = zbd_zone_end(z);
1475 * zone_skip is valid only for sequential workloads.
1477 if (td_random(td) || !td->o.zone_skip)
1481 * It is time to switch to a new zone if:
1482 * - zone_bytes == zone_size bytes have already been accessed
1483 * - The last position reached the end of the current zone.
1484 * - For reads with td->o.read_beyond_wp == false, the last position
1485 * reached the zone write pointer.
1487 if (td->zone_bytes >= td->o.zone_size ||
1488 f->last_pos[ddir] >= zbd_zone_end(z) ||
1489 (ddir == DDIR_READ &&
1490 (!td->o.read_beyond_wp) && f->last_pos[ddir] >= z->wp)) {
1495 f->file_offset += td->o.zone_size + td->o.zone_skip;
1498 * Wrap from the beginning, if we exceed the file size
1500 if (f->file_offset >= f->real_file_size)
1501 f->file_offset = get_start_offset(td, f);
1503 f->last_pos[ddir] = f->file_offset;
1504 td->io_skip_bytes += td->o.zone_skip;
1509 * zbd_adjust_ddir - Adjust an I/O direction for zonemode=zbd.
1511 * @td: FIO thread data.
1512 * @io_u: FIO I/O unit.
1513 * @ddir: I/O direction before adjustment.
1515 * Return adjusted I/O direction.
1517 enum fio_ddir zbd_adjust_ddir(struct thread_data *td, struct io_u *io_u,
1521 * In case read direction is chosen for the first random I/O, fio with
1522 * zonemode=zbd stops because no data can be read from zoned block
1523 * devices with all empty zones. Overwrite the first I/O direction as
1524 * write to make sure data to read exists.
1526 if (ddir != DDIR_READ || !td_rw(td))
1529 if (io_u->file->zbd_info->sectors_with_data ||
1530 td->o.read_beyond_wp)
1537 * zbd_adjust_block - adjust the offset and length as necessary for ZBD drives
1538 * @td: FIO thread data.
1539 * @io_u: FIO I/O unit.
1541 * Locking strategy: returns with z->mutex locked if and only if z refers
1542 * to a sequential zone and if io_u_accept is returned. z is the zone that
1543 * corresponds to io_u->offset at the end of this function.
1545 enum io_u_action zbd_adjust_block(struct thread_data *td, struct io_u *io_u)
1547 struct fio_file *f = io_u->file;
1548 uint32_t zone_idx_b;
1549 struct fio_zone_info *zb, *zl, *orig_zb;
1550 uint32_t orig_len = io_u->buflen;
1551 uint32_t min_bs = td->o.min_bs[io_u->ddir];
1559 assert(is_valid_offset(f, io_u->offset));
1560 assert(io_u->buflen);
1561 zone_idx_b = zbd_zone_idx(f, io_u->offset);
1562 zb = get_zone(f, zone_idx_b);
1565 /* Accept the I/O offset for conventional zones. */
1570 * Accept the I/O offset for reads if reading beyond the write pointer
1573 if (zb->cond != ZBD_ZONE_COND_OFFLINE &&
1574 io_u->ddir == DDIR_READ && td->o.read_beyond_wp)
1579 zone_lock(td, f, zb);
1581 switch (io_u->ddir) {
1583 if (td->runstate == TD_VERIFYING && td_write(td)) {
1584 zb = zbd_replay_write_order(td, io_u, zb);
1586 * Since we return with the zone lock still held,
1587 * add an annotation to let Coverity know that it
1590 /* coverity[missing_unlock] */
1594 * Check that there is enough written data in the zone to do an
1595 * I/O of at least min_bs B. If there isn't, find a new zone for
1598 range = zb->cond != ZBD_ZONE_COND_OFFLINE ?
1599 zb->wp - zb->start : 0;
1600 if (range < min_bs ||
1601 ((!td_random(td)) && (io_u->offset + min_bs > zb->wp))) {
1603 zl = get_zone(f, f->max_zone);
1604 zb = zbd_find_zone(td, io_u, zb, zl);
1607 "%s: zbd_find_zone(%lld, %llu) failed\n",
1608 f->file_name, io_u->offset,
1613 * zbd_find_zone() returned a zone with a range of at
1616 range = zb->wp - zb->start;
1617 assert(range >= min_bs);
1620 io_u->offset = zb->start;
1623 * Make sure the I/O is within the zone valid data range while
1624 * maximizing the I/O size and preserving randomness.
1626 if (range <= io_u->buflen)
1627 io_u->offset = zb->start;
1628 else if (td_random(td))
1629 io_u->offset = zb->start +
1630 ((io_u->offset - orig_zb->start) %
1631 (range - io_u->buflen)) / min_bs * min_bs;
1633 * When zbd_find_zone() returns a conventional zone,
1634 * we can simply accept the new i/o offset here.
1639 * Make sure the I/O does not cross over the zone wp position.
1641 new_len = min((unsigned long long)io_u->buflen,
1642 (unsigned long long)(zb->wp - io_u->offset));
1643 new_len = new_len / min_bs * min_bs;
1644 if (new_len < io_u->buflen) {
1645 io_u->buflen = new_len;
1646 dprint(FD_IO, "Changed length from %u into %llu\n",
1647 orig_len, io_u->buflen);
1649 assert(zb->start <= io_u->offset);
1650 assert(io_u->offset + io_u->buflen <= zb->wp);
1653 if (io_u->buflen > f->zbd_info->zone_size)
1655 if (!zbd_open_zone(td, f, zone_idx_b)) {
1657 zb = zbd_convert_to_open_zone(td, io_u);
1660 zone_idx_b = zbd_zone_nr(f, zb);
1662 /* Check whether the zone reset threshold has been exceeded */
1663 if (td->o.zrf.u.f) {
1664 if (f->zbd_info->wp_sectors_with_data >=
1665 f->io_size * td->o.zrt.u.f &&
1666 zbd_dec_and_reset_write_cnt(td, f)) {
1670 /* Reset the zone pointer if necessary */
1671 if (zb->reset_zone || zbd_zone_full(f, zb, min_bs)) {
1672 assert(td->o.verify == VERIFY_NONE);
1674 * Since previous write requests may have been submitted
1675 * asynchronously and since we will submit the zone
1676 * reset synchronously, wait until previously submitted
1677 * write requests have completed before issuing a
1682 if (zbd_reset_zone(td, f, zb) < 0)
1685 if (zb->capacity < min_bs) {
1686 log_err("zone capacity %llu smaller than minimum block size %d\n",
1687 (unsigned long long)zb->capacity,
1692 /* Make writes occur at the write pointer */
1693 assert(!zbd_zone_full(f, zb, min_bs));
1694 io_u->offset = zb->wp;
1695 if (!is_valid_offset(f, io_u->offset)) {
1696 dprint(FD_ZBD, "Dropped request with offset %llu\n",
1701 * Make sure that the buflen is a multiple of the minimal
1702 * block size. Give up if shrinking would make the request too
1705 new_len = min((unsigned long long)io_u->buflen,
1706 zbd_zone_capacity_end(zb) - io_u->offset);
1707 new_len = new_len / min_bs * min_bs;
1708 if (new_len == io_u->buflen)
1710 if (new_len >= min_bs) {
1711 io_u->buflen = new_len;
1712 dprint(FD_IO, "Changed length from %u into %llu\n",
1713 orig_len, io_u->buflen);
1716 log_err("Zone remainder %lld smaller than minimum block size %d\n",
1717 (zbd_zone_capacity_end(zb) - io_u->offset),
1724 case DDIR_SYNC_FILE_RANGE:
1735 assert(zb->cond != ZBD_ZONE_COND_OFFLINE);
1736 assert(!io_u->zbd_queue_io);
1737 assert(!io_u->zbd_put_io);
1738 io_u->zbd_queue_io = zbd_queue_io;
1739 io_u->zbd_put_io = zbd_put_io;
1743 if (zb && zb->has_wp)
1748 /* Return a string with ZBD statistics */
1749 char *zbd_write_status(const struct thread_stat *ts)
1753 if (asprintf(&res, "; %llu zone resets", (unsigned long long) ts->nr_zone_resets) < 0)