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;
739 if (z->wp == z->start)
742 assert(is_valid_offset(f, offset + length - 1));
744 dprint(FD_ZBD, "%s: resetting wp of zone %u.\n", f->file_name,
746 switch (f->zbd_info->model) {
748 case ZBD_HOST_MANAGED:
749 ret = zbd_reset_wp(td, f, offset, length);
757 pthread_mutex_lock(&f->zbd_info->mutex);
758 f->zbd_info->sectors_with_data -= z->wp - z->start;
759 pthread_mutex_unlock(&f->zbd_info->mutex);
763 td->ts.nr_zone_resets++;
768 /* The caller must hold f->zbd_info->mutex */
769 static void zbd_close_zone(struct thread_data *td, const struct fio_file *f,
770 unsigned int zone_idx)
772 uint32_t open_zone_idx = 0;
774 for (; open_zone_idx < f->zbd_info->num_open_zones; open_zone_idx++) {
775 if (f->zbd_info->open_zones[open_zone_idx] == zone_idx)
778 if (open_zone_idx == f->zbd_info->num_open_zones) {
779 dprint(FD_ZBD, "%s: zone %d is not open\n",
780 f->file_name, zone_idx);
784 dprint(FD_ZBD, "%s: closing zone %d\n", f->file_name, zone_idx);
785 memmove(f->zbd_info->open_zones + open_zone_idx,
786 f->zbd_info->open_zones + open_zone_idx + 1,
787 (ZBD_MAX_OPEN_ZONES - (open_zone_idx + 1)) *
788 sizeof(f->zbd_info->open_zones[0]));
789 f->zbd_info->num_open_zones--;
790 td->num_open_zones--;
791 get_zone(f, zone_idx)->open = 0;
795 * Reset a range of zones. Returns 0 upon success and 1 upon failure.
796 * @td: fio thread data.
797 * @f: fio file for which to reset zones
798 * @zb: first zone to reset.
799 * @ze: first zone not to reset.
800 * @all_zones: whether to reset all zones or only those zones for which the
801 * write pointer is not a multiple of td->o.min_bs[DDIR_WRITE].
803 static int zbd_reset_zones(struct thread_data *td, struct fio_file *f,
804 struct fio_zone_info *const zb,
805 struct fio_zone_info *const ze, bool all_zones)
807 struct fio_zone_info *z;
808 const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
814 dprint(FD_ZBD, "%s: examining zones %u .. %u\n", f->file_name,
815 zbd_zone_nr(f, zb), zbd_zone_nr(f, ze));
816 for (z = zb; z < ze; z++) {
817 uint32_t nz = zbd_zone_nr(f, z);
823 pthread_mutex_lock(&f->zbd_info->mutex);
824 zbd_close_zone(td, f, nz);
825 pthread_mutex_unlock(&f->zbd_info->mutex);
827 reset_wp = z->wp != z->start;
829 reset_wp = z->wp % min_bs != 0;
832 dprint(FD_ZBD, "%s: resetting zone %u\n",
833 f->file_name, zbd_zone_nr(f, z));
834 if (zbd_reset_zone(td, f, z) < 0)
844 * Reset zbd_info.write_cnt, the counter that counts down towards the next
847 static void _zbd_reset_write_cnt(const struct thread_data *td,
848 const struct fio_file *f)
850 assert(0 <= td->o.zrf.u.f && td->o.zrf.u.f <= 1);
852 f->zbd_info->write_cnt = td->o.zrf.u.f ?
853 min(1.0 / td->o.zrf.u.f, 0.0 + UINT_MAX) : UINT_MAX;
856 static void zbd_reset_write_cnt(const struct thread_data *td,
857 const struct fio_file *f)
859 pthread_mutex_lock(&f->zbd_info->mutex);
860 _zbd_reset_write_cnt(td, f);
861 pthread_mutex_unlock(&f->zbd_info->mutex);
864 static bool zbd_dec_and_reset_write_cnt(const struct thread_data *td,
865 const struct fio_file *f)
867 uint32_t write_cnt = 0;
869 pthread_mutex_lock(&f->zbd_info->mutex);
870 assert(f->zbd_info->write_cnt);
871 if (f->zbd_info->write_cnt)
872 write_cnt = --f->zbd_info->write_cnt;
874 _zbd_reset_write_cnt(td, f);
875 pthread_mutex_unlock(&f->zbd_info->mutex);
877 return write_cnt == 0;
885 /* Calculate the number of sectors with data (swd) and perform action 'a' */
886 static uint64_t zbd_process_swd(const struct fio_file *f, enum swd_action a)
888 struct fio_zone_info *zb, *ze, *z;
891 zb = get_zone(f, f->min_zone);
892 ze = get_zone(f, f->max_zone);
893 for (z = zb; z < ze; z++) {
894 pthread_mutex_lock(&z->mutex);
895 swd += z->wp - z->start;
897 pthread_mutex_lock(&f->zbd_info->mutex);
900 assert(f->zbd_info->sectors_with_data == swd);
903 f->zbd_info->sectors_with_data = swd;
906 pthread_mutex_unlock(&f->zbd_info->mutex);
907 for (z = zb; z < ze; z++)
914 * The swd check is useful for debugging but takes too much time to leave
915 * it enabled all the time. Hence it is disabled by default.
917 static const bool enable_check_swd = false;
919 /* Check whether the value of zbd_info.sectors_with_data is correct. */
920 static void zbd_check_swd(const struct fio_file *f)
922 if (!enable_check_swd)
925 zbd_process_swd(f, CHECK_SWD);
928 static void zbd_init_swd(struct fio_file *f)
932 if (!enable_check_swd)
935 swd = zbd_process_swd(f, SET_SWD);
936 dprint(FD_ZBD, "%s(%s): swd = %" PRIu64 "\n", __func__, f->file_name,
940 void zbd_file_reset(struct thread_data *td, struct fio_file *f)
942 struct fio_zone_info *zb, *ze;
944 if (!f->zbd_info || !td_write(td))
947 zb = get_zone(f, f->min_zone);
948 ze = get_zone(f, f->max_zone);
951 * If data verification is enabled reset the affected zones before
952 * writing any data to avoid that a zone reset has to be issued while
953 * writing data, which causes data loss.
955 zbd_reset_zones(td, f, zb, ze, td->o.verify != VERIFY_NONE &&
956 td->runstate != TD_VERIFYING);
957 zbd_reset_write_cnt(td, f);
960 /* The caller must hold f->zbd_info->mutex. */
961 static bool is_zone_open(const struct thread_data *td, const struct fio_file *f,
962 unsigned int zone_idx)
964 struct zoned_block_device_info *zbdi = f->zbd_info;
967 assert(td->o.job_max_open_zones == 0 || td->num_open_zones <= td->o.job_max_open_zones);
968 assert(td->o.job_max_open_zones <= zbdi->max_open_zones);
969 assert(zbdi->num_open_zones <= zbdi->max_open_zones);
971 for (i = 0; i < zbdi->num_open_zones; i++)
972 if (zbdi->open_zones[i] == zone_idx)
979 * Open a ZBD zone if it was not yet open. Returns true if either the zone was
980 * already open or if opening a new zone is allowed. Returns false if the zone
981 * was not yet open and opening a new zone would cause the zone limit to be
984 static bool zbd_open_zone(struct thread_data *td, const struct fio_file *f,
987 const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
988 struct fio_zone_info *z = get_zone(f, zone_idx);
991 if (z->cond == ZBD_ZONE_COND_OFFLINE)
995 * Skip full zones with data verification enabled because resetting a
996 * zone causes data loss and hence causes verification to fail.
998 if (td->o.verify != VERIFY_NONE && zbd_zone_full(f, z, min_bs))
1001 pthread_mutex_lock(&f->zbd_info->mutex);
1002 if (is_zone_open(td, f, zone_idx)) {
1004 * If the zone is already open and going to be full by writes
1005 * in-flight, handle it as a full zone instead of an open zone.
1007 if (z->wp >= zbd_zone_capacity_end(z))
1012 /* Zero means no limit */
1013 if (td->o.job_max_open_zones > 0 &&
1014 td->num_open_zones >= td->o.job_max_open_zones)
1016 if (f->zbd_info->num_open_zones >= f->zbd_info->max_open_zones)
1018 dprint(FD_ZBD, "%s: opening zone %d\n", f->file_name, zone_idx);
1019 f->zbd_info->open_zones[f->zbd_info->num_open_zones++] = zone_idx;
1020 td->num_open_zones++;
1025 pthread_mutex_unlock(&f->zbd_info->mutex);
1029 /* Anything goes as long as it is not a constant. */
1030 static uint32_t pick_random_zone_idx(const struct fio_file *f,
1031 const struct io_u *io_u)
1033 return io_u->offset * f->zbd_info->num_open_zones / f->real_file_size;
1037 * Modify the offset of an I/O unit that does not refer to an open zone such
1038 * that it refers to an open zone. Close an open zone and open a new zone if
1039 * necessary. The open zone is searched across sequential zones.
1040 * This algorithm can only work correctly if all write pointers are
1041 * a multiple of the fio block size. The caller must neither hold z->mutex
1042 * nor f->zbd_info->mutex. Returns with z->mutex held upon success.
1044 static struct fio_zone_info *zbd_convert_to_open_zone(struct thread_data *td,
1047 const uint32_t min_bs = td->o.min_bs[io_u->ddir];
1048 struct fio_file *f = io_u->file;
1049 struct fio_zone_info *z;
1050 unsigned int open_zone_idx = -1;
1051 uint32_t zone_idx, new_zone_idx;
1053 bool wait_zone_close;
1055 assert(is_valid_offset(f, io_u->offset));
1057 if (td->o.max_open_zones || td->o.job_max_open_zones) {
1059 * This statement accesses f->zbd_info->open_zones[] on purpose
1062 zone_idx = f->zbd_info->open_zones[pick_random_zone_idx(f, io_u)];
1064 zone_idx = zbd_zone_idx(f, io_u->offset);
1066 if (zone_idx < f->min_zone)
1067 zone_idx = f->min_zone;
1068 else if (zone_idx >= f->max_zone)
1069 zone_idx = f->max_zone - 1;
1070 dprint(FD_ZBD, "%s(%s): starting from zone %d (offset %lld, buflen %lld)\n",
1071 __func__, f->file_name, zone_idx, io_u->offset, io_u->buflen);
1074 * Since z->mutex is the outer lock and f->zbd_info->mutex the inner
1075 * lock it can happen that the state of the zone with index zone_idx
1076 * has changed after 'z' has been assigned and before f->zbd_info->mutex
1077 * has been obtained. Hence the loop.
1082 z = get_zone(f, zone_idx);
1084 zone_lock(td, f, z);
1085 pthread_mutex_lock(&f->zbd_info->mutex);
1086 if (z->cond != ZBD_ZONE_COND_OFFLINE &&
1087 td->o.max_open_zones == 0 && td->o.job_max_open_zones == 0)
1089 if (f->zbd_info->num_open_zones == 0) {
1090 dprint(FD_ZBD, "%s(%s): no zones are open\n",
1091 __func__, f->file_name);
1092 goto open_other_zone;
1096 * List of opened zones is per-device, shared across all threads.
1097 * Start with quasi-random candidate zone.
1098 * Ignore zones which don't belong to thread's offset/size area.
1100 open_zone_idx = pick_random_zone_idx(f, io_u);
1101 assert(open_zone_idx < f->zbd_info->num_open_zones);
1102 tmp_idx = open_zone_idx;
1103 for (i = 0; i < f->zbd_info->num_open_zones; i++) {
1106 if (tmp_idx >= f->zbd_info->num_open_zones)
1108 tmpz = f->zbd_info->open_zones[tmp_idx];
1109 if (f->min_zone <= tmpz && tmpz < f->max_zone) {
1110 open_zone_idx = tmp_idx;
1111 goto found_candidate_zone;
1117 dprint(FD_ZBD, "%s(%s): no candidate zone\n",
1118 __func__, f->file_name);
1119 pthread_mutex_unlock(&f->zbd_info->mutex);
1123 found_candidate_zone:
1124 new_zone_idx = f->zbd_info->open_zones[open_zone_idx];
1125 if (new_zone_idx == zone_idx)
1127 zone_idx = new_zone_idx;
1128 pthread_mutex_unlock(&f->zbd_info->mutex);
1132 /* Both z->mutex and f->zbd_info->mutex are held. */
1135 if (z->wp + min_bs <= zbd_zone_capacity_end(z)) {
1136 pthread_mutex_unlock(&f->zbd_info->mutex);
1141 /* Check if number of open zones reaches one of limits. */
1143 f->zbd_info->num_open_zones == f->max_zone - f->min_zone ||
1144 (td->o.max_open_zones &&
1145 f->zbd_info->num_open_zones == td->o.max_open_zones) ||
1146 (td->o.job_max_open_zones &&
1147 td->num_open_zones == td->o.job_max_open_zones);
1149 pthread_mutex_unlock(&f->zbd_info->mutex);
1151 /* Only z->mutex is held. */
1154 * When number of open zones reaches to one of limits, wait for
1155 * zone close before opening a new zone.
1157 if (wait_zone_close) {
1158 dprint(FD_ZBD, "%s(%s): quiesce to allow open zones to close\n",
1159 __func__, f->file_name);
1163 /* Zone 'z' is full, so try to open a new zone. */
1164 for (i = f->io_size / f->zbd_info->zone_size; i > 0; i--) {
1169 if (!is_valid_offset(f, z->start)) {
1171 zone_idx = f->min_zone;
1172 z = get_zone(f, zone_idx);
1174 assert(is_valid_offset(f, z->start));
1177 zone_lock(td, f, z);
1180 if (zbd_open_zone(td, f, zone_idx))
1184 /* Only z->mutex is held. */
1186 /* Check whether the write fits in any of the already opened zones. */
1187 pthread_mutex_lock(&f->zbd_info->mutex);
1188 for (i = 0; i < f->zbd_info->num_open_zones; i++) {
1189 zone_idx = f->zbd_info->open_zones[i];
1190 if (zone_idx < f->min_zone || zone_idx >= f->max_zone)
1192 pthread_mutex_unlock(&f->zbd_info->mutex);
1195 z = get_zone(f, zone_idx);
1197 zone_lock(td, f, z);
1198 if (z->wp + min_bs <= zbd_zone_capacity_end(z))
1200 pthread_mutex_lock(&f->zbd_info->mutex);
1202 pthread_mutex_unlock(&f->zbd_info->mutex);
1204 dprint(FD_ZBD, "%s(%s): did not open another zone\n", __func__,
1209 dprint(FD_ZBD, "%s(%s): returning zone %d\n", __func__, f->file_name,
1211 io_u->offset = z->start;
1213 assert(z->cond != ZBD_ZONE_COND_OFFLINE);
1217 /* The caller must hold z->mutex. */
1218 static struct fio_zone_info *zbd_replay_write_order(struct thread_data *td,
1220 struct fio_zone_info *z)
1222 const struct fio_file *f = io_u->file;
1223 const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
1225 if (!zbd_open_zone(td, f, zbd_zone_nr(f, z))) {
1227 z = zbd_convert_to_open_zone(td, io_u);
1231 if (z->verify_block * min_bs >= z->capacity)
1232 log_err("%s: %d * %d >= %llu\n", f->file_name, z->verify_block,
1233 min_bs, (unsigned long long)z->capacity);
1234 io_u->offset = z->start + z->verify_block++ * min_bs;
1239 * Find another zone for which @io_u fits in the readable data in the zone.
1240 * Search in zones @zb + 1 .. @zl. For random workload, also search in zones
1243 * Either returns NULL or returns a zone pointer. When the zone has write
1244 * pointer, hold the mutex for the zone.
1246 static struct fio_zone_info *
1247 zbd_find_zone(struct thread_data *td, struct io_u *io_u,
1248 struct fio_zone_info *zb, struct fio_zone_info *zl)
1250 const uint32_t min_bs = td->o.min_bs[io_u->ddir];
1251 struct fio_file *f = io_u->file;
1252 struct fio_zone_info *z1, *z2;
1253 const struct fio_zone_info *const zf = get_zone(f, f->min_zone);
1256 * Skip to the next non-empty zone in case of sequential I/O and to
1257 * the nearest non-empty zone in case of random I/O.
1259 for (z1 = zb + 1, z2 = zb - 1; z1 < zl || z2 >= zf; z1++, z2--) {
1260 if (z1 < zl && z1->cond != ZBD_ZONE_COND_OFFLINE) {
1262 zone_lock(td, f, z1);
1263 if (z1->start + min_bs <= z1->wp)
1267 } else if (!td_random(td)) {
1270 if (td_random(td) && z2 >= zf &&
1271 z2->cond != ZBD_ZONE_COND_OFFLINE) {
1273 zone_lock(td, f, z2);
1274 if (z2->start + min_bs <= z2->wp)
1280 dprint(FD_ZBD, "%s: adjusting random read offset failed\n",
1286 * zbd_end_zone_io - update zone status at command completion
1288 * @z: zone info pointer
1290 * If the write command made the zone full, close it.
1292 * The caller must hold z->mutex.
1294 static void zbd_end_zone_io(struct thread_data *td, const struct io_u *io_u,
1295 struct fio_zone_info *z)
1297 const struct fio_file *f = io_u->file;
1299 if (io_u->ddir == DDIR_WRITE &&
1300 io_u->offset + io_u->buflen >= zbd_zone_capacity_end(z)) {
1301 pthread_mutex_lock(&f->zbd_info->mutex);
1302 zbd_close_zone(td, f, zbd_zone_nr(f, z));
1303 pthread_mutex_unlock(&f->zbd_info->mutex);
1308 * zbd_queue_io - update the write pointer of a sequential zone
1310 * @success: Whether or not the I/O unit has been queued successfully
1311 * @q: queueing status (busy, completed or queued).
1313 * For write and trim operations, update the write pointer of the I/O unit
1316 static void zbd_queue_io(struct thread_data *td, struct io_u *io_u, int q,
1319 const struct fio_file *f = io_u->file;
1320 struct zoned_block_device_info *zbd_info = f->zbd_info;
1321 struct fio_zone_info *z;
1328 zone_idx = zbd_zone_idx(f, io_u->offset);
1329 assert(zone_idx < zbd_info->nr_zones);
1330 z = get_zone(f, zone_idx);
1338 "%s: queued I/O (%lld, %llu) for zone %u\n",
1339 f->file_name, io_u->offset, io_u->buflen, zone_idx);
1341 switch (io_u->ddir) {
1343 zone_end = min((uint64_t)(io_u->offset + io_u->buflen),
1344 zbd_zone_capacity_end(z));
1345 pthread_mutex_lock(&zbd_info->mutex);
1347 * z->wp > zone_end means that one or more I/O errors
1350 if (z->wp <= zone_end)
1351 zbd_info->sectors_with_data += zone_end - z->wp;
1352 pthread_mutex_unlock(&zbd_info->mutex);
1356 assert(z->wp == z->start);
1362 if (q == FIO_Q_COMPLETED && !io_u->error)
1363 zbd_end_zone_io(td, io_u, z);
1366 if (!success || q != FIO_Q_QUEUED) {
1367 /* BUSY or COMPLETED: unlock the zone */
1369 io_u->zbd_put_io = NULL;
1374 * zbd_put_io - Unlock an I/O unit target zone lock
1377 static void zbd_put_io(struct thread_data *td, const struct io_u *io_u)
1379 const struct fio_file *f = io_u->file;
1380 struct zoned_block_device_info *zbd_info = f->zbd_info;
1381 struct fio_zone_info *z;
1387 zone_idx = zbd_zone_idx(f, io_u->offset);
1388 assert(zone_idx < zbd_info->nr_zones);
1389 z = get_zone(f, zone_idx);
1394 "%s: terminate I/O (%lld, %llu) for zone %u\n",
1395 f->file_name, io_u->offset, io_u->buflen, zone_idx);
1397 zbd_end_zone_io(td, io_u, z);
1404 * Windows and MacOS do not define this.
1407 #define EREMOTEIO 121 /* POSIX value */
1410 bool zbd_unaligned_write(int error_code)
1412 switch (error_code) {
1421 * setup_zbd_zone_mode - handle zoneskip as necessary for ZBD drives
1422 * @td: FIO thread data.
1423 * @io_u: FIO I/O unit.
1425 * For sequential workloads, change the file offset to skip zoneskip bytes when
1426 * no more IO can be performed in the current zone.
1427 * - For read workloads, zoneskip is applied when the io has reached the end of
1428 * the zone or the zone write position (when td->o.read_beyond_wp is false).
1429 * - For write workloads, zoneskip is applied when the zone is full.
1430 * This applies only to read and write operations.
1432 void setup_zbd_zone_mode(struct thread_data *td, struct io_u *io_u)
1434 struct fio_file *f = io_u->file;
1435 enum fio_ddir ddir = io_u->ddir;
1436 struct fio_zone_info *z;
1439 assert(td->o.zone_mode == ZONE_MODE_ZBD);
1440 assert(td->o.zone_size);
1442 zone_idx = zbd_zone_idx(f, f->last_pos[ddir]);
1443 z = get_zone(f, zone_idx);
1446 * When the zone capacity is smaller than the zone size and the I/O is
1447 * sequential write, skip to zone end if the latest position is at the
1448 * zone capacity limit.
1450 if (z->capacity < f->zbd_info->zone_size && !td_random(td) &&
1451 ddir == DDIR_WRITE &&
1452 f->last_pos[ddir] >= zbd_zone_capacity_end(z)) {
1454 "%s: Jump from zone capacity limit to zone end:"
1455 " (%llu -> %llu) for zone %u (%llu)\n",
1456 f->file_name, (unsigned long long) f->last_pos[ddir],
1457 (unsigned long long) zbd_zone_end(z), zone_idx,
1458 (unsigned long long) z->capacity);
1459 td->io_skip_bytes += zbd_zone_end(z) - f->last_pos[ddir];
1460 f->last_pos[ddir] = zbd_zone_end(z);
1464 * zone_skip is valid only for sequential workloads.
1466 if (td_random(td) || !td->o.zone_skip)
1470 * It is time to switch to a new zone if:
1471 * - zone_bytes == zone_size bytes have already been accessed
1472 * - The last position reached the end of the current zone.
1473 * - For reads with td->o.read_beyond_wp == false, the last position
1474 * reached the zone write pointer.
1476 if (td->zone_bytes >= td->o.zone_size ||
1477 f->last_pos[ddir] >= zbd_zone_end(z) ||
1478 (ddir == DDIR_READ &&
1479 (!td->o.read_beyond_wp) && f->last_pos[ddir] >= z->wp)) {
1484 f->file_offset += td->o.zone_size + td->o.zone_skip;
1487 * Wrap from the beginning, if we exceed the file size
1489 if (f->file_offset >= f->real_file_size)
1490 f->file_offset = get_start_offset(td, f);
1492 f->last_pos[ddir] = f->file_offset;
1493 td->io_skip_bytes += td->o.zone_skip;
1498 * zbd_adjust_ddir - Adjust an I/O direction for zonemode=zbd.
1500 * @td: FIO thread data.
1501 * @io_u: FIO I/O unit.
1502 * @ddir: I/O direction before adjustment.
1504 * Return adjusted I/O direction.
1506 enum fio_ddir zbd_adjust_ddir(struct thread_data *td, struct io_u *io_u,
1510 * In case read direction is chosen for the first random I/O, fio with
1511 * zonemode=zbd stops because no data can be read from zoned block
1512 * devices with all empty zones. Overwrite the first I/O direction as
1513 * write to make sure data to read exists.
1515 if (ddir != DDIR_READ || !td_rw(td))
1518 if (io_u->file->zbd_info->sectors_with_data ||
1519 td->o.read_beyond_wp)
1526 * zbd_adjust_block - adjust the offset and length as necessary for ZBD drives
1527 * @td: FIO thread data.
1528 * @io_u: FIO I/O unit.
1530 * Locking strategy: returns with z->mutex locked if and only if z refers
1531 * to a sequential zone and if io_u_accept is returned. z is the zone that
1532 * corresponds to io_u->offset at the end of this function.
1534 enum io_u_action zbd_adjust_block(struct thread_data *td, struct io_u *io_u)
1536 struct fio_file *f = io_u->file;
1537 uint32_t zone_idx_b;
1538 struct fio_zone_info *zb, *zl, *orig_zb;
1539 uint32_t orig_len = io_u->buflen;
1540 uint32_t min_bs = td->o.min_bs[io_u->ddir];
1548 assert(is_valid_offset(f, io_u->offset));
1549 assert(io_u->buflen);
1550 zone_idx_b = zbd_zone_idx(f, io_u->offset);
1551 zb = get_zone(f, zone_idx_b);
1554 /* Accept the I/O offset for conventional zones. */
1559 * Accept the I/O offset for reads if reading beyond the write pointer
1562 if (zb->cond != ZBD_ZONE_COND_OFFLINE &&
1563 io_u->ddir == DDIR_READ && td->o.read_beyond_wp)
1568 zone_lock(td, f, zb);
1570 switch (io_u->ddir) {
1572 if (td->runstate == TD_VERIFYING && td_write(td)) {
1573 zb = zbd_replay_write_order(td, io_u, zb);
1575 * Since we return with the zone lock still held,
1576 * add an annotation to let Coverity know that it
1579 /* coverity[missing_unlock] */
1583 * Check that there is enough written data in the zone to do an
1584 * I/O of at least min_bs B. If there isn't, find a new zone for
1587 range = zb->cond != ZBD_ZONE_COND_OFFLINE ?
1588 zb->wp - zb->start : 0;
1589 if (range < min_bs ||
1590 ((!td_random(td)) && (io_u->offset + min_bs > zb->wp))) {
1592 zl = get_zone(f, f->max_zone);
1593 zb = zbd_find_zone(td, io_u, zb, zl);
1596 "%s: zbd_find_zone(%lld, %llu) failed\n",
1597 f->file_name, io_u->offset,
1602 * zbd_find_zone() returned a zone with a range of at
1605 range = zb->wp - zb->start;
1606 assert(range >= min_bs);
1609 io_u->offset = zb->start;
1612 * Make sure the I/O is within the zone valid data range while
1613 * maximizing the I/O size and preserving randomness.
1615 if (range <= io_u->buflen)
1616 io_u->offset = zb->start;
1617 else if (td_random(td))
1618 io_u->offset = zb->start +
1619 ((io_u->offset - orig_zb->start) %
1620 (range - io_u->buflen)) / min_bs * min_bs;
1622 * When zbd_find_zone() returns a conventional zone,
1623 * we can simply accept the new i/o offset here.
1628 * Make sure the I/O does not cross over the zone wp position.
1630 new_len = min((unsigned long long)io_u->buflen,
1631 (unsigned long long)(zb->wp - io_u->offset));
1632 new_len = new_len / min_bs * min_bs;
1633 if (new_len < io_u->buflen) {
1634 io_u->buflen = new_len;
1635 dprint(FD_IO, "Changed length from %u into %llu\n",
1636 orig_len, io_u->buflen);
1638 assert(zb->start <= io_u->offset);
1639 assert(io_u->offset + io_u->buflen <= zb->wp);
1642 if (io_u->buflen > f->zbd_info->zone_size)
1644 if (!zbd_open_zone(td, f, zone_idx_b)) {
1646 zb = zbd_convert_to_open_zone(td, io_u);
1649 zone_idx_b = zbd_zone_nr(f, zb);
1651 /* Check whether the zone reset threshold has been exceeded */
1652 if (td->o.zrf.u.f) {
1653 if (f->zbd_info->sectors_with_data >=
1654 f->io_size * td->o.zrt.u.f &&
1655 zbd_dec_and_reset_write_cnt(td, f)) {
1659 /* Reset the zone pointer if necessary */
1660 if (zb->reset_zone || zbd_zone_full(f, zb, min_bs)) {
1661 assert(td->o.verify == VERIFY_NONE);
1663 * Since previous write requests may have been submitted
1664 * asynchronously and since we will submit the zone
1665 * reset synchronously, wait until previously submitted
1666 * write requests have completed before issuing a
1671 if (zbd_reset_zone(td, f, zb) < 0)
1674 if (zb->capacity < min_bs) {
1675 log_err("zone capacity %llu smaller than minimum block size %d\n",
1676 (unsigned long long)zb->capacity,
1681 /* Make writes occur at the write pointer */
1682 assert(!zbd_zone_full(f, zb, min_bs));
1683 io_u->offset = zb->wp;
1684 if (!is_valid_offset(f, io_u->offset)) {
1685 dprint(FD_ZBD, "Dropped request with offset %llu\n",
1690 * Make sure that the buflen is a multiple of the minimal
1691 * block size. Give up if shrinking would make the request too
1694 new_len = min((unsigned long long)io_u->buflen,
1695 zbd_zone_capacity_end(zb) - io_u->offset);
1696 new_len = new_len / min_bs * min_bs;
1697 if (new_len == io_u->buflen)
1699 if (new_len >= min_bs) {
1700 io_u->buflen = new_len;
1701 dprint(FD_IO, "Changed length from %u into %llu\n",
1702 orig_len, io_u->buflen);
1705 log_err("Zone remainder %lld smaller than minimum block size %d\n",
1706 (zbd_zone_capacity_end(zb) - io_u->offset),
1713 case DDIR_SYNC_FILE_RANGE:
1724 assert(zb->cond != ZBD_ZONE_COND_OFFLINE);
1725 assert(!io_u->zbd_queue_io);
1726 assert(!io_u->zbd_put_io);
1727 io_u->zbd_queue_io = zbd_queue_io;
1728 io_u->zbd_put_io = zbd_put_io;
1732 if (zb && zb->has_wp)
1737 /* Return a string with ZBD statistics */
1738 char *zbd_write_status(const struct thread_stat *ts)
1742 if (asprintf(&res, "; %llu zone resets", (unsigned long long) ts->nr_zone_resets) < 0)