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 static inline struct fio_zone_info *get_zone(const struct fio_file *f,
208 unsigned int zone_nr)
210 return &f->zbd_info->zone_info[zone_nr];
213 /* Verify whether direct I/O is used for all host-managed zoned drives. */
214 static bool zbd_using_direct_io(void)
216 struct thread_data *td;
221 if (td->o.odirect || !(td->o.td_ddir & TD_DDIR_WRITE))
223 for_each_file(td, f, j) {
225 f->zbd_info->model == ZBD_HOST_MANAGED)
233 /* Whether or not the I/O range for f includes one or more sequential zones */
234 static bool zbd_is_seq_job(struct fio_file *f)
236 uint32_t zone_idx, zone_idx_b, zone_idx_e;
241 zone_idx_b = zbd_zone_idx(f, f->file_offset);
242 zone_idx_e = zbd_zone_idx(f, f->file_offset + f->io_size - 1);
243 for (zone_idx = zone_idx_b; zone_idx <= zone_idx_e; zone_idx++)
244 if (zbd_zone_swr(get_zone(f, zone_idx)))
251 * Verify whether offset and size parameters are aligned with zone boundaries.
253 static bool zbd_verify_sizes(void)
255 const struct fio_zone_info *z;
256 struct thread_data *td;
258 uint64_t new_offset, new_end;
263 for_each_file(td, f, j) {
266 if (f->file_offset >= f->real_file_size)
268 if (!zbd_is_seq_job(f))
271 if (!td->o.zone_size) {
272 td->o.zone_size = f->zbd_info->zone_size;
273 if (!td->o.zone_size) {
274 log_err("%s: invalid 0 zone size\n",
278 } else if (td->o.zone_size != f->zbd_info->zone_size) {
279 log_err("%s: job parameter zonesize %llu does not match disk zone size %llu.\n",
280 f->file_name, (unsigned long long) td->o.zone_size,
281 (unsigned long long) f->zbd_info->zone_size);
285 if (td->o.zone_skip &&
286 (td->o.zone_skip < td->o.zone_size ||
287 td->o.zone_skip % td->o.zone_size)) {
288 log_err("%s: zoneskip %llu is not a multiple of the device zone size %llu.\n",
289 f->file_name, (unsigned long long) td->o.zone_skip,
290 (unsigned long long) td->o.zone_size);
294 zone_idx = zbd_zone_idx(f, f->file_offset);
295 z = get_zone(f, zone_idx);
296 if ((f->file_offset != z->start) &&
297 (td->o.td_ddir != TD_DDIR_READ)) {
298 new_offset = zbd_zone_end(z);
299 if (new_offset >= f->file_offset + f->io_size) {
300 log_info("%s: io_size must be at least one zone\n",
304 log_info("%s: rounded up offset from %llu to %llu\n",
305 f->file_name, (unsigned long long) f->file_offset,
306 (unsigned long long) new_offset);
307 f->io_size -= (new_offset - f->file_offset);
308 f->file_offset = new_offset;
310 zone_idx = zbd_zone_idx(f, f->file_offset + f->io_size);
311 z = get_zone(f, zone_idx);
313 if ((td->o.td_ddir != TD_DDIR_READ) &&
314 (f->file_offset + f->io_size != new_end)) {
315 if (new_end <= f->file_offset) {
316 log_info("%s: io_size must be at least one zone\n",
320 log_info("%s: rounded down io_size from %llu to %llu\n",
321 f->file_name, (unsigned long long) f->io_size,
322 (unsigned long long) new_end - f->file_offset);
323 f->io_size = new_end - f->file_offset;
326 f->min_zone = zbd_zone_idx(f, f->file_offset);
327 f->max_zone = zbd_zone_idx(f, f->file_offset + f->io_size);
328 assert(f->min_zone < f->max_zone);
335 static bool zbd_verify_bs(void)
337 struct thread_data *td;
343 for_each_file(td, f, j) {
346 zone_size = f->zbd_info->zone_size;
347 for (k = 0; k < FIO_ARRAY_SIZE(td->o.bs); k++) {
348 if (td->o.verify != VERIFY_NONE &&
349 zone_size % td->o.bs[k] != 0) {
350 log_info("%s: block size %llu is not a divisor of the zone size %d\n",
351 f->file_name, td->o.bs[k],
361 static int ilog2(uint64_t i)
373 * Initialize f->zbd_info for devices that are not zoned block devices. This
374 * allows to execute a ZBD workload against a non-ZBD device.
376 static int init_zone_info(struct thread_data *td, struct fio_file *f)
379 struct fio_zone_info *p;
380 uint64_t zone_size = td->o.zone_size;
381 uint64_t zone_capacity = td->o.zone_capacity;
382 struct zoned_block_device_info *zbd_info = NULL;
385 if (zone_size == 0) {
386 log_err("%s: Specifying the zone size is mandatory for regular block devices with --zonemode=zbd\n\n",
391 if (zone_size < 512) {
392 log_err("%s: zone size must be at least 512 bytes for --zonemode=zbd\n\n",
397 if (zone_capacity == 0)
398 zone_capacity = zone_size;
400 if (zone_capacity > zone_size) {
401 log_err("%s: job parameter zonecapacity %llu is larger than zone size %llu\n",
402 f->file_name, (unsigned long long) td->o.zone_capacity,
403 (unsigned long long) td->o.zone_size);
407 nr_zones = (f->real_file_size + zone_size - 1) / zone_size;
408 zbd_info = scalloc(1, sizeof(*zbd_info) +
409 (nr_zones + 1) * sizeof(zbd_info->zone_info[0]));
413 mutex_init_pshared(&zbd_info->mutex);
414 zbd_info->refcount = 1;
415 p = &zbd_info->zone_info[0];
416 for (i = 0; i < nr_zones; i++, p++) {
417 mutex_init_pshared_with_type(&p->mutex,
418 PTHREAD_MUTEX_RECURSIVE);
419 p->start = i * zone_size;
421 p->type = ZBD_ZONE_TYPE_SWR;
422 p->cond = ZBD_ZONE_COND_EMPTY;
423 p->capacity = zone_capacity;
426 p->start = nr_zones * zone_size;
428 f->zbd_info = zbd_info;
429 f->zbd_info->zone_size = zone_size;
430 f->zbd_info->zone_size_log2 = is_power_of_2(zone_size) ?
431 ilog2(zone_size) : 0;
432 f->zbd_info->nr_zones = nr_zones;
437 * Maximum number of zones to report in one operation.
439 #define ZBD_REPORT_MAX_ZONES 8192U
442 * Parse the device zone report and store it in f->zbd_info. Must be called
443 * only for devices that are zoned, namely those with a model != ZBD_NONE.
445 static int parse_zone_info(struct thread_data *td, struct fio_file *f)
448 struct zbd_zone *zones, *z;
449 struct fio_zone_info *p;
450 uint64_t zone_size, offset;
451 struct zoned_block_device_info *zbd_info = NULL;
452 int i, j, ret = -ENOMEM;
454 zones = calloc(ZBD_REPORT_MAX_ZONES, sizeof(struct zbd_zone));
458 nrz = zbd_report_zones(td, f, 0, zones, ZBD_REPORT_MAX_ZONES);
461 log_info("fio: report zones (offset 0) failed for %s (%d).\n",
466 zone_size = zones[0].len;
467 nr_zones = (f->real_file_size + zone_size - 1) / zone_size;
469 if (td->o.zone_size == 0) {
470 td->o.zone_size = zone_size;
471 } else if (td->o.zone_size != zone_size) {
472 log_err("fio: %s job parameter zonesize %llu does not match disk zone size %llu.\n",
473 f->file_name, (unsigned long long) td->o.zone_size,
474 (unsigned long long) zone_size);
479 dprint(FD_ZBD, "Device %s has %d zones of size %llu KB\n", f->file_name,
480 nr_zones, (unsigned long long) zone_size / 1024);
482 zbd_info = scalloc(1, sizeof(*zbd_info) +
483 (nr_zones + 1) * sizeof(zbd_info->zone_info[0]));
486 mutex_init_pshared(&zbd_info->mutex);
487 zbd_info->refcount = 1;
488 p = &zbd_info->zone_info[0];
489 for (offset = 0, j = 0; j < nr_zones;) {
491 for (i = 0; i < nrz; i++, j++, z++, p++) {
492 mutex_init_pshared_with_type(&p->mutex,
493 PTHREAD_MUTEX_RECURSIVE);
495 p->capacity = z->capacity;
497 case ZBD_ZONE_COND_NOT_WP:
498 case ZBD_ZONE_COND_FULL:
499 p->wp = p->start + p->capacity;
502 assert(z->start <= z->wp);
503 assert(z->wp <= z->start + zone_size);
509 if (j > 0 && p->start != p[-1].start + zone_size) {
510 log_info("%s: invalid zone data\n",
517 offset = z->start + z->len;
520 nrz = zbd_report_zones(td, f, offset,
521 zones, ZBD_REPORT_MAX_ZONES);
524 log_info("fio: report zones (offset %llu) failed for %s (%d).\n",
525 (unsigned long long)offset,
532 zbd_info->zone_info[nr_zones].start = offset;
534 f->zbd_info = zbd_info;
535 f->zbd_info->zone_size = zone_size;
536 f->zbd_info->zone_size_log2 = is_power_of_2(zone_size) ?
537 ilog2(zone_size) : 0;
538 f->zbd_info->nr_zones = nr_zones;
549 * Allocate zone information and store it into f->zbd_info if zonemode=zbd.
551 * Returns 0 upon success and a negative error code upon failure.
553 static int zbd_create_zone_info(struct thread_data *td, struct fio_file *f)
555 enum zbd_zoned_model zbd_model;
558 assert(td->o.zone_mode == ZONE_MODE_ZBD);
560 ret = zbd_get_zoned_model(td, f, &zbd_model);
568 case ZBD_HOST_MANAGED:
569 ret = parse_zone_info(td, f);
572 ret = init_zone_info(td, f);
575 td_verror(td, EINVAL, "Unsupported zoned model");
576 log_err("Unsupported zoned model\n");
581 f->zbd_info->model = zbd_model;
582 f->zbd_info->max_open_zones = td->o.max_open_zones;
587 void zbd_free_zone_info(struct fio_file *f)
593 pthread_mutex_lock(&f->zbd_info->mutex);
594 refcount = --f->zbd_info->refcount;
595 pthread_mutex_unlock(&f->zbd_info->mutex);
597 assert((int32_t)refcount >= 0);
604 * Initialize f->zbd_info.
606 * Returns 0 upon success and a negative error code upon failure.
608 * Note: this function can only work correctly if it is called before the first
611 static int zbd_init_zone_info(struct thread_data *td, struct fio_file *file)
613 struct thread_data *td2;
617 for_each_td(td2, i) {
618 for_each_file(td2, f2, j) {
619 if (td2 == td && f2 == file)
622 strcmp(f2->file_name, file->file_name) != 0)
624 file->zbd_info = f2->zbd_info;
625 file->zbd_info->refcount++;
630 ret = zbd_create_zone_info(td, file);
632 td_verror(td, -ret, "zbd_create_zone_info() failed");
636 static bool zbd_open_zone(struct thread_data *td, const struct fio_file *f,
638 static int zbd_reset_zone(struct thread_data *td, struct fio_file *f,
639 struct fio_zone_info *z);
641 int zbd_setup_files(struct thread_data *td)
646 for_each_file(td, f, i) {
647 if (zbd_init_zone_info(td, f))
651 if (!zbd_using_direct_io()) {
652 log_err("Using direct I/O is mandatory for writing to ZBD drives\n\n");
656 if (!zbd_verify_sizes())
659 if (!zbd_verify_bs())
662 for_each_file(td, f, i) {
663 struct zoned_block_device_info *zbd = f->zbd_info;
664 struct fio_zone_info *z;
670 zbd->max_open_zones = zbd->max_open_zones ?: ZBD_MAX_OPEN_ZONES;
672 if (td->o.max_open_zones > 0 &&
673 zbd->max_open_zones != td->o.max_open_zones) {
674 log_err("Different 'max_open_zones' values\n");
677 if (zbd->max_open_zones > ZBD_MAX_OPEN_ZONES) {
678 log_err("'max_open_zones' value is limited by %u\n", ZBD_MAX_OPEN_ZONES);
682 for (zi = f->min_zone; zi < f->max_zone; zi++) {
683 z = &zbd->zone_info[zi];
684 if (z->cond != ZBD_ZONE_COND_IMP_OPEN &&
685 z->cond != ZBD_ZONE_COND_EXP_OPEN)
687 if (zbd_open_zone(td, f, zi))
690 * If the number of open zones exceeds specified limits,
691 * reset all extra open zones.
693 if (zbd_reset_zone(td, f, z) < 0) {
694 log_err("Failed to reest zone %d\n", zi);
703 static inline unsigned int zbd_zone_nr(const struct fio_file *f,
704 struct fio_zone_info *zone)
706 return zone - f->zbd_info->zone_info;
710 * zbd_reset_zone - reset the write pointer of a single zone
711 * @td: FIO thread data.
712 * @f: FIO file associated with the disk for which to reset a write pointer.
715 * Returns 0 upon success and a negative error code upon failure.
717 * The caller must hold z->mutex.
719 static int zbd_reset_zone(struct thread_data *td, struct fio_file *f,
720 struct fio_zone_info *z)
722 uint64_t offset = z->start;
723 uint64_t length = (z+1)->start - offset;
726 if (z->wp == z->start)
729 assert(is_valid_offset(f, offset + length - 1));
731 dprint(FD_ZBD, "%s: resetting wp of zone %u.\n", f->file_name,
733 switch (f->zbd_info->model) {
735 case ZBD_HOST_MANAGED:
736 ret = zbd_reset_wp(td, f, offset, length);
744 pthread_mutex_lock(&f->zbd_info->mutex);
745 f->zbd_info->sectors_with_data -= z->wp - z->start;
746 pthread_mutex_unlock(&f->zbd_info->mutex);
750 td->ts.nr_zone_resets++;
755 /* The caller must hold f->zbd_info->mutex */
756 static void zbd_close_zone(struct thread_data *td, const struct fio_file *f,
757 unsigned int zone_idx)
759 uint32_t open_zone_idx = 0;
761 for (; open_zone_idx < f->zbd_info->num_open_zones; open_zone_idx++) {
762 if (f->zbd_info->open_zones[open_zone_idx] == zone_idx)
765 if (open_zone_idx == f->zbd_info->num_open_zones) {
766 dprint(FD_ZBD, "%s: zone %d is not open\n",
767 f->file_name, zone_idx);
771 dprint(FD_ZBD, "%s: closing zone %d\n", f->file_name, zone_idx);
772 memmove(f->zbd_info->open_zones + open_zone_idx,
773 f->zbd_info->open_zones + open_zone_idx + 1,
774 (ZBD_MAX_OPEN_ZONES - (open_zone_idx + 1)) *
775 sizeof(f->zbd_info->open_zones[0]));
776 f->zbd_info->num_open_zones--;
777 td->num_open_zones--;
778 get_zone(f, zone_idx)->open = 0;
782 * Reset a range of zones. Returns 0 upon success and 1 upon failure.
783 * @td: fio thread data.
784 * @f: fio file for which to reset zones
785 * @zb: first zone to reset.
786 * @ze: first zone not to reset.
787 * @all_zones: whether to reset all zones or only those zones for which the
788 * write pointer is not a multiple of td->o.min_bs[DDIR_WRITE].
790 static int zbd_reset_zones(struct thread_data *td, struct fio_file *f,
791 struct fio_zone_info *const zb,
792 struct fio_zone_info *const ze, bool all_zones)
794 struct fio_zone_info *z;
795 const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
801 dprint(FD_ZBD, "%s: examining zones %u .. %u\n", f->file_name,
802 zbd_zone_nr(f, zb), zbd_zone_nr(f, ze));
803 for (z = zb; z < ze; z++) {
804 uint32_t nz = zbd_zone_nr(f, z);
806 if (!zbd_zone_swr(z))
810 pthread_mutex_lock(&f->zbd_info->mutex);
811 zbd_close_zone(td, f, nz);
812 pthread_mutex_unlock(&f->zbd_info->mutex);
814 reset_wp = z->wp != z->start;
816 reset_wp = z->wp % min_bs != 0;
819 dprint(FD_ZBD, "%s: resetting zone %u\n",
820 f->file_name, zbd_zone_nr(f, z));
821 if (zbd_reset_zone(td, f, z) < 0)
824 pthread_mutex_unlock(&z->mutex);
831 * Reset zbd_info.write_cnt, the counter that counts down towards the next
834 static void _zbd_reset_write_cnt(const struct thread_data *td,
835 const struct fio_file *f)
837 assert(0 <= td->o.zrf.u.f && td->o.zrf.u.f <= 1);
839 f->zbd_info->write_cnt = td->o.zrf.u.f ?
840 min(1.0 / td->o.zrf.u.f, 0.0 + UINT_MAX) : UINT_MAX;
843 static void zbd_reset_write_cnt(const struct thread_data *td,
844 const struct fio_file *f)
846 pthread_mutex_lock(&f->zbd_info->mutex);
847 _zbd_reset_write_cnt(td, f);
848 pthread_mutex_unlock(&f->zbd_info->mutex);
851 static bool zbd_dec_and_reset_write_cnt(const struct thread_data *td,
852 const struct fio_file *f)
854 uint32_t write_cnt = 0;
856 pthread_mutex_lock(&f->zbd_info->mutex);
857 assert(f->zbd_info->write_cnt);
858 if (f->zbd_info->write_cnt)
859 write_cnt = --f->zbd_info->write_cnt;
861 _zbd_reset_write_cnt(td, f);
862 pthread_mutex_unlock(&f->zbd_info->mutex);
864 return write_cnt == 0;
872 /* Calculate the number of sectors with data (swd) and perform action 'a' */
873 static uint64_t zbd_process_swd(const struct fio_file *f, enum swd_action a)
875 struct fio_zone_info *zb, *ze, *z;
878 zb = get_zone(f, f->min_zone);
879 ze = get_zone(f, f->max_zone);
880 for (z = zb; z < ze; z++) {
881 pthread_mutex_lock(&z->mutex);
882 swd += z->wp - z->start;
884 pthread_mutex_lock(&f->zbd_info->mutex);
887 assert(f->zbd_info->sectors_with_data == swd);
890 f->zbd_info->sectors_with_data = swd;
893 pthread_mutex_unlock(&f->zbd_info->mutex);
894 for (z = zb; z < ze; z++)
895 pthread_mutex_unlock(&z->mutex);
901 * The swd check is useful for debugging but takes too much time to leave
902 * it enabled all the time. Hence it is disabled by default.
904 static const bool enable_check_swd = false;
906 /* Check whether the value of zbd_info.sectors_with_data is correct. */
907 static void zbd_check_swd(const struct fio_file *f)
909 if (!enable_check_swd)
912 zbd_process_swd(f, CHECK_SWD);
915 static void zbd_init_swd(struct fio_file *f)
919 if (!enable_check_swd)
922 swd = zbd_process_swd(f, SET_SWD);
923 dprint(FD_ZBD, "%s(%s): swd = %" PRIu64 "\n", __func__, f->file_name,
927 void zbd_file_reset(struct thread_data *td, struct fio_file *f)
929 struct fio_zone_info *zb, *ze;
931 if (!f->zbd_info || !td_write(td))
934 zb = get_zone(f, f->min_zone);
935 ze = get_zone(f, f->max_zone);
938 * If data verification is enabled reset the affected zones before
939 * writing any data to avoid that a zone reset has to be issued while
940 * writing data, which causes data loss.
942 zbd_reset_zones(td, f, zb, ze, td->o.verify != VERIFY_NONE &&
943 td->runstate != TD_VERIFYING);
944 zbd_reset_write_cnt(td, f);
947 /* The caller must hold f->zbd_info->mutex. */
948 static bool is_zone_open(const struct thread_data *td, const struct fio_file *f,
949 unsigned int zone_idx)
951 struct zoned_block_device_info *zbdi = f->zbd_info;
954 assert(td->o.job_max_open_zones == 0 || td->num_open_zones <= td->o.job_max_open_zones);
955 assert(td->o.job_max_open_zones <= zbdi->max_open_zones);
956 assert(zbdi->num_open_zones <= zbdi->max_open_zones);
958 for (i = 0; i < zbdi->num_open_zones; i++)
959 if (zbdi->open_zones[i] == zone_idx)
966 * Open a ZBD zone if it was not yet open. Returns true if either the zone was
967 * already open or if opening a new zone is allowed. Returns false if the zone
968 * was not yet open and opening a new zone would cause the zone limit to be
971 static bool zbd_open_zone(struct thread_data *td, const struct fio_file *f,
974 const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
975 struct fio_zone_info *z = get_zone(f, zone_idx);
978 if (z->cond == ZBD_ZONE_COND_OFFLINE)
982 * Skip full zones with data verification enabled because resetting a
983 * zone causes data loss and hence causes verification to fail.
985 if (td->o.verify != VERIFY_NONE && zbd_zone_full(f, z, min_bs))
988 pthread_mutex_lock(&f->zbd_info->mutex);
989 if (is_zone_open(td, f, zone_idx)) {
991 * If the zone is already open and going to be full by writes
992 * in-flight, handle it as a full zone instead of an open zone.
994 if (z->wp >= zbd_zone_capacity_end(z))
999 /* Zero means no limit */
1000 if (td->o.job_max_open_zones > 0 &&
1001 td->num_open_zones >= td->o.job_max_open_zones)
1003 if (f->zbd_info->num_open_zones >= f->zbd_info->max_open_zones)
1005 dprint(FD_ZBD, "%s: opening zone %d\n", f->file_name, zone_idx);
1006 f->zbd_info->open_zones[f->zbd_info->num_open_zones++] = zone_idx;
1007 td->num_open_zones++;
1012 pthread_mutex_unlock(&f->zbd_info->mutex);
1016 /* Anything goes as long as it is not a constant. */
1017 static uint32_t pick_random_zone_idx(const struct fio_file *f,
1018 const struct io_u *io_u)
1020 return io_u->offset * f->zbd_info->num_open_zones / f->real_file_size;
1024 * Modify the offset of an I/O unit that does not refer to an open zone such
1025 * that it refers to an open zone. Close an open zone and open a new zone if
1026 * necessary. This algorithm can only work correctly if all write pointers are
1027 * a multiple of the fio block size. The caller must neither hold z->mutex
1028 * nor f->zbd_info->mutex. Returns with z->mutex held upon success.
1030 static struct fio_zone_info *zbd_convert_to_open_zone(struct thread_data *td,
1033 const uint32_t min_bs = td->o.min_bs[io_u->ddir];
1034 struct fio_file *f = io_u->file;
1035 struct fio_zone_info *z;
1036 unsigned int open_zone_idx = -1;
1037 uint32_t zone_idx, new_zone_idx;
1039 bool wait_zone_close;
1041 assert(is_valid_offset(f, io_u->offset));
1043 if (td->o.max_open_zones || td->o.job_max_open_zones) {
1045 * This statement accesses f->zbd_info->open_zones[] on purpose
1048 zone_idx = f->zbd_info->open_zones[pick_random_zone_idx(f, io_u)];
1050 zone_idx = zbd_zone_idx(f, io_u->offset);
1052 if (zone_idx < f->min_zone)
1053 zone_idx = f->min_zone;
1054 else if (zone_idx >= f->max_zone)
1055 zone_idx = f->max_zone - 1;
1056 dprint(FD_ZBD, "%s(%s): starting from zone %d (offset %lld, buflen %lld)\n",
1057 __func__, f->file_name, zone_idx, io_u->offset, io_u->buflen);
1060 * Since z->mutex is the outer lock and f->zbd_info->mutex the inner
1061 * lock it can happen that the state of the zone with index zone_idx
1062 * has changed after 'z' has been assigned and before f->zbd_info->mutex
1063 * has been obtained. Hence the loop.
1068 z = get_zone(f, zone_idx);
1070 zone_lock(td, f, z);
1071 pthread_mutex_lock(&f->zbd_info->mutex);
1072 if (td->o.max_open_zones == 0 && td->o.job_max_open_zones == 0)
1074 if (f->zbd_info->num_open_zones == 0) {
1075 dprint(FD_ZBD, "%s(%s): no zones are open\n",
1076 __func__, f->file_name);
1077 goto open_other_zone;
1081 * List of opened zones is per-device, shared across all threads.
1082 * Start with quasi-random candidate zone.
1083 * Ignore zones which don't belong to thread's offset/size area.
1085 open_zone_idx = pick_random_zone_idx(f, io_u);
1086 assert(open_zone_idx < f->zbd_info->num_open_zones);
1087 tmp_idx = open_zone_idx;
1088 for (i = 0; i < f->zbd_info->num_open_zones; i++) {
1091 if (tmp_idx >= f->zbd_info->num_open_zones)
1093 tmpz = f->zbd_info->open_zones[tmp_idx];
1094 if (f->min_zone <= tmpz && tmpz < f->max_zone) {
1095 open_zone_idx = tmp_idx;
1096 goto found_candidate_zone;
1102 dprint(FD_ZBD, "%s(%s): no candidate zone\n",
1103 __func__, f->file_name);
1104 pthread_mutex_unlock(&f->zbd_info->mutex);
1105 pthread_mutex_unlock(&z->mutex);
1108 found_candidate_zone:
1109 new_zone_idx = f->zbd_info->open_zones[open_zone_idx];
1110 if (new_zone_idx == zone_idx)
1112 zone_idx = new_zone_idx;
1113 pthread_mutex_unlock(&f->zbd_info->mutex);
1114 pthread_mutex_unlock(&z->mutex);
1117 /* Both z->mutex and f->zbd_info->mutex are held. */
1120 if (z->wp + min_bs <= zbd_zone_capacity_end(z)) {
1121 pthread_mutex_unlock(&f->zbd_info->mutex);
1126 /* Check if number of open zones reaches one of limits. */
1128 f->zbd_info->num_open_zones == f->max_zone - f->min_zone ||
1129 (td->o.max_open_zones &&
1130 f->zbd_info->num_open_zones == td->o.max_open_zones) ||
1131 (td->o.job_max_open_zones &&
1132 td->num_open_zones == td->o.job_max_open_zones);
1134 pthread_mutex_unlock(&f->zbd_info->mutex);
1136 /* Only z->mutex is held. */
1139 * When number of open zones reaches to one of limits, wait for
1140 * zone close before opening a new zone.
1142 if (wait_zone_close) {
1143 dprint(FD_ZBD, "%s(%s): quiesce to allow open zones to close\n",
1144 __func__, f->file_name);
1148 /* Zone 'z' is full, so try to open a new zone. */
1149 for (i = f->io_size / f->zbd_info->zone_size; i > 0; i--) {
1151 pthread_mutex_unlock(&z->mutex);
1153 if (!is_valid_offset(f, z->start)) {
1155 zone_idx = f->min_zone;
1156 z = get_zone(f, zone_idx);
1158 assert(is_valid_offset(f, z->start));
1159 zone_lock(td, f, z);
1162 if (zbd_open_zone(td, f, zone_idx))
1166 /* Only z->mutex is held. */
1168 /* Check whether the write fits in any of the already opened zones. */
1169 pthread_mutex_lock(&f->zbd_info->mutex);
1170 for (i = 0; i < f->zbd_info->num_open_zones; i++) {
1171 zone_idx = f->zbd_info->open_zones[i];
1172 if (zone_idx < f->min_zone || zone_idx >= f->max_zone)
1174 pthread_mutex_unlock(&f->zbd_info->mutex);
1175 pthread_mutex_unlock(&z->mutex);
1177 z = get_zone(f, zone_idx);
1179 zone_lock(td, f, z);
1180 if (z->wp + min_bs <= zbd_zone_capacity_end(z))
1182 pthread_mutex_lock(&f->zbd_info->mutex);
1184 pthread_mutex_unlock(&f->zbd_info->mutex);
1185 pthread_mutex_unlock(&z->mutex);
1186 dprint(FD_ZBD, "%s(%s): did not open another zone\n", __func__,
1191 dprint(FD_ZBD, "%s(%s): returning zone %d\n", __func__, f->file_name,
1193 io_u->offset = z->start;
1197 /* The caller must hold z->mutex. */
1198 static struct fio_zone_info *zbd_replay_write_order(struct thread_data *td,
1200 struct fio_zone_info *z)
1202 const struct fio_file *f = io_u->file;
1203 const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
1205 if (!zbd_open_zone(td, f, zbd_zone_nr(f, z))) {
1206 pthread_mutex_unlock(&z->mutex);
1207 z = zbd_convert_to_open_zone(td, io_u);
1211 if (z->verify_block * min_bs >= z->capacity)
1212 log_err("%s: %d * %d >= %llu\n", f->file_name, z->verify_block,
1213 min_bs, (unsigned long long)z->capacity);
1214 io_u->offset = z->start + z->verify_block++ * min_bs;
1219 * Find another zone for which @io_u fits below the write pointer. Start
1220 * searching in zones @zb + 1 .. @zl and continue searching in zones
1223 * Either returns NULL or returns a zone pointer and holds the mutex for that
1226 static struct fio_zone_info *
1227 zbd_find_zone(struct thread_data *td, struct io_u *io_u,
1228 struct fio_zone_info *zb, struct fio_zone_info *zl)
1230 const uint32_t min_bs = td->o.min_bs[io_u->ddir];
1231 struct fio_file *f = io_u->file;
1232 struct fio_zone_info *z1, *z2;
1233 const struct fio_zone_info *const zf = get_zone(f, f->min_zone);
1236 * Skip to the next non-empty zone in case of sequential I/O and to
1237 * the nearest non-empty zone in case of random I/O.
1239 for (z1 = zb + 1, z2 = zb - 1; z1 < zl || z2 >= zf; z1++, z2--) {
1240 if (z1 < zl && z1->cond != ZBD_ZONE_COND_OFFLINE) {
1241 zone_lock(td, f, z1);
1242 if (z1->start + min_bs <= z1->wp)
1244 pthread_mutex_unlock(&z1->mutex);
1245 } else if (!td_random(td)) {
1248 if (td_random(td) && z2 >= zf &&
1249 z2->cond != ZBD_ZONE_COND_OFFLINE) {
1250 zone_lock(td, f, z2);
1251 if (z2->start + min_bs <= z2->wp)
1253 pthread_mutex_unlock(&z2->mutex);
1256 dprint(FD_ZBD, "%s: adjusting random read offset failed\n",
1262 * zbd_end_zone_io - update zone status at command completion
1264 * @z: zone info pointer
1266 * If the write command made the zone full, close it.
1268 * The caller must hold z->mutex.
1270 static void zbd_end_zone_io(struct thread_data *td, const struct io_u *io_u,
1271 struct fio_zone_info *z)
1273 const struct fio_file *f = io_u->file;
1275 if (io_u->ddir == DDIR_WRITE &&
1276 io_u->offset + io_u->buflen >= zbd_zone_capacity_end(z)) {
1277 pthread_mutex_lock(&f->zbd_info->mutex);
1278 zbd_close_zone(td, f, zbd_zone_nr(f, z));
1279 pthread_mutex_unlock(&f->zbd_info->mutex);
1284 * zbd_queue_io - update the write pointer of a sequential zone
1286 * @success: Whether or not the I/O unit has been queued successfully
1287 * @q: queueing status (busy, completed or queued).
1289 * For write and trim operations, update the write pointer of the I/O unit
1292 static void zbd_queue_io(struct thread_data *td, struct io_u *io_u, int q,
1295 const struct fio_file *f = io_u->file;
1296 struct zoned_block_device_info *zbd_info = f->zbd_info;
1297 struct fio_zone_info *z;
1304 zone_idx = zbd_zone_idx(f, io_u->offset);
1305 assert(zone_idx < zbd_info->nr_zones);
1306 z = get_zone(f, zone_idx);
1308 if (!zbd_zone_swr(z))
1315 "%s: queued I/O (%lld, %llu) for zone %u\n",
1316 f->file_name, io_u->offset, io_u->buflen, zone_idx);
1318 switch (io_u->ddir) {
1320 zone_end = min((uint64_t)(io_u->offset + io_u->buflen),
1321 zbd_zone_capacity_end(z));
1322 pthread_mutex_lock(&zbd_info->mutex);
1324 * z->wp > zone_end means that one or more I/O errors
1327 if (z->wp <= zone_end)
1328 zbd_info->sectors_with_data += zone_end - z->wp;
1329 pthread_mutex_unlock(&zbd_info->mutex);
1333 assert(z->wp == z->start);
1339 if (q == FIO_Q_COMPLETED && !io_u->error)
1340 zbd_end_zone_io(td, io_u, z);
1343 if (!success || q != FIO_Q_QUEUED) {
1344 /* BUSY or COMPLETED: unlock the zone */
1345 pthread_mutex_unlock(&z->mutex);
1346 io_u->zbd_put_io = NULL;
1351 * zbd_put_io - Unlock an I/O unit target zone lock
1354 static void zbd_put_io(struct thread_data *td, const struct io_u *io_u)
1356 const struct fio_file *f = io_u->file;
1357 struct zoned_block_device_info *zbd_info = f->zbd_info;
1358 struct fio_zone_info *z;
1365 zone_idx = zbd_zone_idx(f, io_u->offset);
1366 assert(zone_idx < zbd_info->nr_zones);
1367 z = get_zone(f, zone_idx);
1369 if (!zbd_zone_swr(z))
1373 "%s: terminate I/O (%lld, %llu) for zone %u\n",
1374 f->file_name, io_u->offset, io_u->buflen, zone_idx);
1376 zbd_end_zone_io(td, io_u, z);
1378 ret = pthread_mutex_unlock(&z->mutex);
1384 * Windows and MacOS do not define this.
1387 #define EREMOTEIO 121 /* POSIX value */
1390 bool zbd_unaligned_write(int error_code)
1392 switch (error_code) {
1401 * setup_zbd_zone_mode - handle zoneskip as necessary for ZBD drives
1402 * @td: FIO thread data.
1403 * @io_u: FIO I/O unit.
1405 * For sequential workloads, change the file offset to skip zoneskip bytes when
1406 * no more IO can be performed in the current zone.
1407 * - For read workloads, zoneskip is applied when the io has reached the end of
1408 * the zone or the zone write position (when td->o.read_beyond_wp is false).
1409 * - For write workloads, zoneskip is applied when the zone is full.
1410 * This applies only to read and write operations.
1412 void setup_zbd_zone_mode(struct thread_data *td, struct io_u *io_u)
1414 struct fio_file *f = io_u->file;
1415 enum fio_ddir ddir = io_u->ddir;
1416 struct fio_zone_info *z;
1419 assert(td->o.zone_mode == ZONE_MODE_ZBD);
1420 assert(td->o.zone_size);
1422 zone_idx = zbd_zone_idx(f, f->last_pos[ddir]);
1423 z = get_zone(f, zone_idx);
1426 * When the zone capacity is smaller than the zone size and the I/O is
1427 * sequential write, skip to zone end if the latest position is at the
1428 * zone capacity limit.
1430 if (z->capacity < f->zbd_info->zone_size && !td_random(td) &&
1431 ddir == DDIR_WRITE &&
1432 f->last_pos[ddir] >= zbd_zone_capacity_end(z)) {
1434 "%s: Jump from zone capacity limit to zone end:"
1435 " (%llu -> %llu) for zone %u (%llu)\n",
1436 f->file_name, (unsigned long long) f->last_pos[ddir],
1437 (unsigned long long) zbd_zone_end(z), zone_idx,
1438 (unsigned long long) z->capacity);
1439 td->io_skip_bytes += zbd_zone_end(z) - f->last_pos[ddir];
1440 f->last_pos[ddir] = zbd_zone_end(z);
1444 * zone_skip is valid only for sequential workloads.
1446 if (td_random(td) || !td->o.zone_skip)
1450 * It is time to switch to a new zone if:
1451 * - zone_bytes == zone_size bytes have already been accessed
1452 * - The last position reached the end of the current zone.
1453 * - For reads with td->o.read_beyond_wp == false, the last position
1454 * reached the zone write pointer.
1456 if (td->zone_bytes >= td->o.zone_size ||
1457 f->last_pos[ddir] >= zbd_zone_end(z) ||
1458 (ddir == DDIR_READ &&
1459 (!td->o.read_beyond_wp) && f->last_pos[ddir] >= z->wp)) {
1464 f->file_offset += td->o.zone_size + td->o.zone_skip;
1467 * Wrap from the beginning, if we exceed the file size
1469 if (f->file_offset >= f->real_file_size)
1470 f->file_offset = get_start_offset(td, f);
1472 f->last_pos[ddir] = f->file_offset;
1473 td->io_skip_bytes += td->o.zone_skip;
1478 * zbd_adjust_ddir - Adjust an I/O direction for zonemode=zbd.
1480 * @td: FIO thread data.
1481 * @io_u: FIO I/O unit.
1482 * @ddir: I/O direction before adjustment.
1484 * Return adjusted I/O direction.
1486 enum fio_ddir zbd_adjust_ddir(struct thread_data *td, struct io_u *io_u,
1490 * In case read direction is chosen for the first random I/O, fio with
1491 * zonemode=zbd stops because no data can be read from zoned block
1492 * devices with all empty zones. Overwrite the first I/O direction as
1493 * write to make sure data to read exists.
1495 if (ddir != DDIR_READ || !td_rw(td))
1498 if (io_u->file->zbd_info->sectors_with_data ||
1499 td->o.read_beyond_wp)
1506 * zbd_adjust_block - adjust the offset and length as necessary for ZBD drives
1507 * @td: FIO thread data.
1508 * @io_u: FIO I/O unit.
1510 * Locking strategy: returns with z->mutex locked if and only if z refers
1511 * to a sequential zone and if io_u_accept is returned. z is the zone that
1512 * corresponds to io_u->offset at the end of this function.
1514 enum io_u_action zbd_adjust_block(struct thread_data *td, struct io_u *io_u)
1516 struct fio_file *f = io_u->file;
1517 uint32_t zone_idx_b;
1518 struct fio_zone_info *zb, *zl, *orig_zb;
1519 uint32_t orig_len = io_u->buflen;
1520 uint32_t min_bs = td->o.min_bs[io_u->ddir];
1528 assert(is_valid_offset(f, io_u->offset));
1529 assert(io_u->buflen);
1530 zone_idx_b = zbd_zone_idx(f, io_u->offset);
1531 zb = get_zone(f, zone_idx_b);
1534 /* Accept the I/O offset for conventional zones. */
1535 if (!zbd_zone_swr(zb))
1539 * Accept the I/O offset for reads if reading beyond the write pointer
1542 if (zb->cond != ZBD_ZONE_COND_OFFLINE &&
1543 io_u->ddir == DDIR_READ && td->o.read_beyond_wp)
1548 zone_lock(td, f, zb);
1550 switch (io_u->ddir) {
1552 if (td->runstate == TD_VERIFYING && td_write(td)) {
1553 zb = zbd_replay_write_order(td, io_u, zb);
1554 pthread_mutex_unlock(&zb->mutex);
1558 * Check that there is enough written data in the zone to do an
1559 * I/O of at least min_bs B. If there isn't, find a new zone for
1562 range = zb->cond != ZBD_ZONE_COND_OFFLINE ?
1563 zb->wp - zb->start : 0;
1564 if (range < min_bs ||
1565 ((!td_random(td)) && (io_u->offset + min_bs > zb->wp))) {
1566 pthread_mutex_unlock(&zb->mutex);
1567 zl = get_zone(f, f->max_zone);
1568 zb = zbd_find_zone(td, io_u, zb, zl);
1571 "%s: zbd_find_zone(%lld, %llu) failed\n",
1572 f->file_name, io_u->offset,
1577 * zbd_find_zone() returned a zone with a range of at
1580 range = zb->wp - zb->start;
1581 assert(range >= min_bs);
1584 io_u->offset = zb->start;
1587 * Make sure the I/O is within the zone valid data range while
1588 * maximizing the I/O size and preserving randomness.
1590 if (range <= io_u->buflen)
1591 io_u->offset = zb->start;
1592 else if (td_random(td))
1593 io_u->offset = zb->start +
1594 ((io_u->offset - orig_zb->start) %
1595 (range - io_u->buflen)) / min_bs * min_bs;
1597 * Make sure the I/O does not cross over the zone wp position.
1599 new_len = min((unsigned long long)io_u->buflen,
1600 (unsigned long long)(zb->wp - io_u->offset));
1601 new_len = new_len / min_bs * min_bs;
1602 if (new_len < io_u->buflen) {
1603 io_u->buflen = new_len;
1604 dprint(FD_IO, "Changed length from %u into %llu\n",
1605 orig_len, io_u->buflen);
1607 assert(zb->start <= io_u->offset);
1608 assert(io_u->offset + io_u->buflen <= zb->wp);
1611 if (io_u->buflen > f->zbd_info->zone_size)
1613 if (!zbd_open_zone(td, f, zone_idx_b)) {
1614 pthread_mutex_unlock(&zb->mutex);
1615 zb = zbd_convert_to_open_zone(td, io_u);
1618 zone_idx_b = zbd_zone_nr(f, zb);
1620 /* Check whether the zone reset threshold has been exceeded */
1621 if (td->o.zrf.u.f) {
1622 if (f->zbd_info->sectors_with_data >=
1623 f->io_size * td->o.zrt.u.f &&
1624 zbd_dec_and_reset_write_cnt(td, f)) {
1628 /* Reset the zone pointer if necessary */
1629 if (zb->reset_zone || zbd_zone_full(f, zb, min_bs)) {
1630 assert(td->o.verify == VERIFY_NONE);
1632 * Since previous write requests may have been submitted
1633 * asynchronously and since we will submit the zone
1634 * reset synchronously, wait until previously submitted
1635 * write requests have completed before issuing a
1640 if (zbd_reset_zone(td, f, zb) < 0)
1643 if (zb->capacity < min_bs) {
1644 log_err("zone capacity %llu smaller than minimum block size %d\n",
1645 (unsigned long long)zb->capacity,
1650 /* Make writes occur at the write pointer */
1651 assert(!zbd_zone_full(f, zb, min_bs));
1652 io_u->offset = zb->wp;
1653 if (!is_valid_offset(f, io_u->offset)) {
1654 dprint(FD_ZBD, "Dropped request with offset %llu\n",
1659 * Make sure that the buflen is a multiple of the minimal
1660 * block size. Give up if shrinking would make the request too
1663 new_len = min((unsigned long long)io_u->buflen,
1664 zbd_zone_capacity_end(zb) - io_u->offset);
1665 new_len = new_len / min_bs * min_bs;
1666 if (new_len == io_u->buflen)
1668 if (new_len >= min_bs) {
1669 io_u->buflen = new_len;
1670 dprint(FD_IO, "Changed length from %u into %llu\n",
1671 orig_len, io_u->buflen);
1674 log_err("Zone remainder %lld smaller than minimum block size %d\n",
1675 (zbd_zone_capacity_end(zb) - io_u->offset),
1682 case DDIR_SYNC_FILE_RANGE:
1693 assert(zb->cond != ZBD_ZONE_COND_OFFLINE);
1694 assert(!io_u->zbd_queue_io);
1695 assert(!io_u->zbd_put_io);
1696 io_u->zbd_queue_io = zbd_queue_io;
1697 io_u->zbd_put_io = zbd_put_io;
1702 pthread_mutex_unlock(&zb->mutex);
1706 /* Return a string with ZBD statistics */
1707 char *zbd_write_status(const struct thread_stat *ts)
1711 if (asprintf(&res, "; %llu zone resets", (unsigned long long) ts->nr_zone_resets) < 0)