2 * Copyright (C) 2018 Western Digital Corporation or its affiliates.
4 * This file is released under the GPL.
12 #include <sys/ioctl.h>
15 #include <linux/blkzoned.h>
21 #include "oslib/asprintf.h"
27 * zbd_zone_idx - convert an offset into a zone number
29 * @offset: offset in bytes. If this offset is in the first zone_size bytes
30 * past the disk size then the index of the sentinel is returned.
32 static uint32_t zbd_zone_idx(const struct fio_file *f, uint64_t offset)
36 if (f->zbd_info->zone_size_log2 > 0)
37 zone_idx = offset >> f->zbd_info->zone_size_log2;
39 zone_idx = offset / f->zbd_info->zone_size;
41 return min(zone_idx, f->zbd_info->nr_zones);
45 * zbd_zone_full - verify whether a minimum number of bytes remain in a zone
47 * @z: zone info pointer.
48 * @required: minimum number of bytes that must remain in a zone.
50 * The caller must hold z->mutex.
52 static bool zbd_zone_full(const struct fio_file *f, struct fio_zone_info *z,
55 assert((required & 511) == 0);
57 return z->type == BLK_ZONE_TYPE_SEQWRITE_REQ &&
58 z->wp + required > z->start + f->zbd_info->zone_size;
61 static bool is_valid_offset(const struct fio_file *f, uint64_t offset)
63 return (uint64_t)(offset - f->file_offset) < f->io_size;
66 /* Verify whether direct I/O is used for all host-managed zoned drives. */
67 static bool zbd_using_direct_io(void)
69 struct thread_data *td;
74 if (td->o.odirect || !(td->o.td_ddir & TD_DDIR_WRITE))
76 for_each_file(td, f, j) {
78 f->zbd_info->model == ZBD_DM_HOST_MANAGED)
86 /* Whether or not the I/O range for f includes one or more sequential zones */
87 static bool zbd_is_seq_job(struct fio_file *f)
89 uint32_t zone_idx, zone_idx_b, zone_idx_e;
94 zone_idx_b = zbd_zone_idx(f, f->file_offset);
95 zone_idx_e = zbd_zone_idx(f, f->file_offset + f->io_size - 1);
96 for (zone_idx = zone_idx_b; zone_idx <= zone_idx_e; zone_idx++)
97 if (f->zbd_info->zone_info[zone_idx].type ==
98 BLK_ZONE_TYPE_SEQWRITE_REQ)
105 * Verify whether offset and size parameters are aligned with zone boundaries.
107 static bool zbd_verify_sizes(void)
109 const struct fio_zone_info *z;
110 struct thread_data *td;
112 uint64_t new_offset, new_end;
117 for_each_file(td, f, j) {
120 if (f->file_offset >= f->real_file_size)
122 if (!zbd_is_seq_job(f))
125 if (!td->o.zone_size) {
126 td->o.zone_size = f->zbd_info->zone_size;
127 if (!td->o.zone_size) {
128 log_err("%s: invalid 0 zone size\n",
132 } else if (td->o.zone_size != f->zbd_info->zone_size) {
133 log_err("%s: job parameter zonesize %llu does not match disk zone size %llu.\n",
134 f->file_name, (unsigned long long) td->o.zone_size,
135 (unsigned long long) f->zbd_info->zone_size);
139 if (td->o.zone_skip &&
140 (td->o.zone_skip < td->o.zone_size ||
141 td->o.zone_skip % td->o.zone_size)) {
142 log_err("%s: zoneskip %llu is not a multiple of the device zone size %llu.\n",
143 f->file_name, (unsigned long long) td->o.zone_skip,
144 (unsigned long long) td->o.zone_size);
148 zone_idx = zbd_zone_idx(f, f->file_offset);
149 z = &f->zbd_info->zone_info[zone_idx];
150 if (f->file_offset != z->start) {
151 new_offset = (z+1)->start;
152 if (new_offset >= f->file_offset + f->io_size) {
153 log_info("%s: io_size must be at least one zone\n",
157 log_info("%s: rounded up offset from %llu to %llu\n",
158 f->file_name, (unsigned long long) f->file_offset,
159 (unsigned long long) new_offset);
160 f->io_size -= (new_offset - f->file_offset);
161 f->file_offset = new_offset;
163 zone_idx = zbd_zone_idx(f, f->file_offset + f->io_size);
164 z = &f->zbd_info->zone_info[zone_idx];
166 if (f->file_offset + f->io_size != new_end) {
167 if (new_end <= f->file_offset) {
168 log_info("%s: io_size must be at least one zone\n",
172 log_info("%s: rounded down io_size from %llu to %llu\n",
173 f->file_name, (unsigned long long) f->io_size,
174 (unsigned long long) new_end - f->file_offset);
175 f->io_size = new_end - f->file_offset;
183 static bool zbd_verify_bs(void)
185 struct thread_data *td;
191 for_each_file(td, f, j) {
194 zone_size = f->zbd_info->zone_size;
195 for (k = 0; k < ARRAY_SIZE(td->o.bs); k++) {
196 if (td->o.verify != VERIFY_NONE &&
197 zone_size % td->o.bs[k] != 0) {
198 log_info("%s: block size %llu is not a divisor of the zone size %d\n",
199 f->file_name, td->o.bs[k],
210 * Read zone information into @buf starting from sector @start_sector.
211 * @fd is a file descriptor that refers to a block device and @bufsz is the
214 * Returns 0 upon success and a negative error code upon failure.
215 * If the zone report is empty, always assume an error (device problem) and
218 static int read_zone_info(int fd, uint64_t start_sector,
219 void *buf, unsigned int bufsz)
221 struct blk_zone_report *hdr = buf;
224 if (bufsz < sizeof(*hdr))
227 memset(hdr, 0, sizeof(*hdr));
229 hdr->nr_zones = (bufsz - sizeof(*hdr)) / sizeof(struct blk_zone);
230 hdr->sector = start_sector;
231 ret = ioctl(fd, BLKREPORTZONE, hdr);
240 * Read up to 255 characters from the first line of a file. Strip the trailing
243 static char *read_file(const char *path)
245 char line[256], *p = line;
248 f = fopen(path, "rb");
251 if (!fgets(line, sizeof(line), f))
259 static enum blk_zoned_model get_zbd_model(const char *file_name)
261 enum blk_zoned_model model = ZBD_DM_NONE;
262 char *zoned_attr_path = NULL;
263 char *model_str = NULL;
265 char *sys_devno_path = NULL;
266 char *part_attr_path = NULL;
267 char *part_str = NULL;
268 char sys_path[PATH_MAX];
272 if (stat(file_name, &statbuf) < 0)
275 if (asprintf(&sys_devno_path, "/sys/dev/block/%d:%d",
276 major(statbuf.st_rdev), minor(statbuf.st_rdev)) < 0)
279 sz = readlink(sys_devno_path, sys_path, sizeof(sys_path) - 1);
285 * If the device is a partition device, cut the device name in the
286 * canonical sysfs path to obtain the sysfs path of the holder device.
287 * e.g.: /sys/devices/.../sda/sda1 -> /sys/devices/.../sda
289 if (asprintf(&part_attr_path, "/sys/dev/block/%s/partition",
292 part_str = read_file(part_attr_path);
293 if (part_str && *part_str == '1') {
294 delim = strrchr(sys_path, '/');
300 if (asprintf(&zoned_attr_path,
301 "/sys/dev/block/%s/queue/zoned", sys_path) < 0)
304 model_str = read_file(zoned_attr_path);
307 dprint(FD_ZBD, "%s: zbd model string: %s\n", file_name, model_str);
308 if (strcmp(model_str, "host-aware") == 0)
309 model = ZBD_DM_HOST_AWARE;
310 else if (strcmp(model_str, "host-managed") == 0)
311 model = ZBD_DM_HOST_MANAGED;
315 free(zoned_attr_path);
317 free(part_attr_path);
318 free(sys_devno_path);
322 static int ilog2(uint64_t i)
334 * Initialize f->zbd_info for devices that are not zoned block devices. This
335 * allows to execute a ZBD workload against a non-ZBD device.
337 static int init_zone_info(struct thread_data *td, struct fio_file *f)
340 struct fio_zone_info *p;
341 uint64_t zone_size = td->o.zone_size;
342 struct zoned_block_device_info *zbd_info = NULL;
343 pthread_mutexattr_t attr;
346 if (zone_size == 0) {
347 log_err("%s: Specifying the zone size is mandatory for regular block devices with --zonemode=zbd\n\n",
352 if (zone_size < 512) {
353 log_err("%s: zone size must be at least 512 bytes for --zonemode=zbd\n\n",
358 nr_zones = (f->real_file_size + zone_size - 1) / zone_size;
359 zbd_info = scalloc(1, sizeof(*zbd_info) +
360 (nr_zones + 1) * sizeof(zbd_info->zone_info[0]));
364 pthread_mutexattr_init(&attr);
365 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
366 pthread_mutexattr_setpshared(&attr, true);
367 pthread_mutex_init(&zbd_info->mutex, &attr);
368 zbd_info->refcount = 1;
369 p = &zbd_info->zone_info[0];
370 for (i = 0; i < nr_zones; i++, p++) {
371 pthread_mutex_init(&p->mutex, &attr);
372 p->start = i * zone_size;
373 p->wp = p->start + zone_size;
374 p->type = BLK_ZONE_TYPE_SEQWRITE_REQ;
375 p->cond = BLK_ZONE_COND_EMPTY;
378 p->start = nr_zones * zone_size;
380 f->zbd_info = zbd_info;
381 f->zbd_info->zone_size = zone_size;
382 f->zbd_info->zone_size_log2 = is_power_of_2(zone_size) ?
383 ilog2(zone_size) : -1;
384 f->zbd_info->nr_zones = nr_zones;
385 pthread_mutexattr_destroy(&attr);
390 * Parse the BLKREPORTZONE output and store it in f->zbd_info. Must be called
391 * only for devices that support this ioctl, namely zoned block devices.
393 static int parse_zone_info(struct thread_data *td, struct fio_file *f)
395 const unsigned int bufsz = sizeof(struct blk_zone_report) +
396 4096 * sizeof(struct blk_zone);
398 struct blk_zone_report *hdr;
399 const struct blk_zone *z;
400 struct fio_zone_info *p;
401 uint64_t zone_size, start_sector;
402 struct zoned_block_device_info *zbd_info = NULL;
403 pthread_mutexattr_t attr;
405 int fd, i, j, ret = 0;
407 pthread_mutexattr_init(&attr);
408 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
409 pthread_mutexattr_setpshared(&attr, true);
415 fd = open(f->file_name, O_RDONLY | O_LARGEFILE);
421 ret = read_zone_info(fd, 0, buf, bufsz);
423 log_info("fio: BLKREPORTZONE(%lu) failed for %s (%d).\n",
424 0UL, f->file_name, -ret);
428 if (hdr->nr_zones < 1) {
429 log_info("fio: %s has invalid zone information.\n",
433 z = (void *)(hdr + 1);
434 zone_size = z->len << 9;
435 nr_zones = (f->real_file_size + zone_size - 1) / zone_size;
437 if (td->o.zone_size == 0) {
438 td->o.zone_size = zone_size;
439 } else if (td->o.zone_size != zone_size) {
440 log_err("fio: %s job parameter zonesize %llu does not match disk zone size %llu.\n",
441 f->file_name, (unsigned long long) td->o.zone_size,
442 (unsigned long long) zone_size);
447 dprint(FD_ZBD, "Device %s has %d zones of size %llu KB\n", f->file_name,
448 nr_zones, (unsigned long long) zone_size / 1024);
450 zbd_info = scalloc(1, sizeof(*zbd_info) +
451 (nr_zones + 1) * sizeof(zbd_info->zone_info[0]));
455 pthread_mutex_init(&zbd_info->mutex, &attr);
456 zbd_info->refcount = 1;
457 p = &zbd_info->zone_info[0];
458 for (start_sector = 0, j = 0; j < nr_zones;) {
459 z = (void *)(hdr + 1);
460 for (i = 0; i < hdr->nr_zones; i++, j++, z++, p++) {
461 pthread_mutex_init(&p->mutex, &attr);
462 p->start = z->start << 9;
464 case BLK_ZONE_COND_NOT_WP:
465 case BLK_ZONE_COND_FULL:
466 p->wp = p->start + zone_size;
469 assert(z->start <= z->wp);
470 assert(z->wp <= z->start + (zone_size >> 9));
476 if (j > 0 && p->start != p[-1].start + zone_size) {
477 log_info("%s: invalid zone data\n",
484 start_sector = z->start + z->len;
487 ret = read_zone_info(fd, start_sector, buf, bufsz);
489 log_info("fio: BLKREPORTZONE(%llu) failed for %s (%d).\n",
490 (unsigned long long) start_sector, f->file_name, -ret);
495 zbd_info->zone_info[nr_zones].start = start_sector << 9;
497 f->zbd_info = zbd_info;
498 f->zbd_info->zone_size = zone_size;
499 f->zbd_info->zone_size_log2 = is_power_of_2(zone_size) ?
500 ilog2(zone_size) : -1;
501 f->zbd_info->nr_zones = nr_zones;
511 pthread_mutexattr_destroy(&attr);
516 * Allocate zone information and store it into f->zbd_info if zonemode=zbd.
518 * Returns 0 upon success and a negative error code upon failure.
520 static int zbd_create_zone_info(struct thread_data *td, struct fio_file *f)
522 enum blk_zoned_model zbd_model;
525 assert(td->o.zone_mode == ZONE_MODE_ZBD);
527 zbd_model = get_zbd_model(f->file_name);
529 case ZBD_DM_HOST_AWARE:
530 case ZBD_DM_HOST_MANAGED:
531 ret = parse_zone_info(td, f);
534 ret = init_zone_info(td, f);
538 f->zbd_info->model = zbd_model;
542 void zbd_free_zone_info(struct fio_file *f)
549 pthread_mutex_lock(&f->zbd_info->mutex);
550 refcount = --f->zbd_info->refcount;
551 pthread_mutex_unlock(&f->zbd_info->mutex);
553 assert((int32_t)refcount >= 0);
560 * Initialize f->zbd_info.
562 * Returns 0 upon success and a negative error code upon failure.
564 * Note: this function can only work correctly if it is called before the first
567 static int zbd_init_zone_info(struct thread_data *td, struct fio_file *file)
569 struct thread_data *td2;
573 for_each_td(td2, i) {
574 for_each_file(td2, f2, j) {
575 if (td2 == td && f2 == file)
578 strcmp(f2->file_name, file->file_name) != 0)
580 file->zbd_info = f2->zbd_info;
581 file->zbd_info->refcount++;
586 ret = zbd_create_zone_info(td, file);
588 td_verror(td, -ret, "zbd_create_zone_info() failed");
592 int zbd_init(struct thread_data *td)
597 for_each_file(td, f, i) {
598 if (f->filetype != FIO_TYPE_BLOCK)
600 if (zbd_init_zone_info(td, f))
604 if (!zbd_using_direct_io()) {
605 log_err("Using direct I/O is mandatory for writing to ZBD drives\n\n");
609 if (!zbd_verify_sizes())
612 if (!zbd_verify_bs())
619 * zbd_reset_range - reset zones for a range of sectors
620 * @td: FIO thread data.
621 * @f: Fio file for which to reset zones
622 * @sector: Starting sector in units of 512 bytes
623 * @nr_sectors: Number of sectors in units of 512 bytes
625 * Returns 0 upon success and a negative error code upon failure.
627 static int zbd_reset_range(struct thread_data *td, const struct fio_file *f,
628 uint64_t offset, uint64_t length)
630 struct blk_zone_range zr = {
631 .sector = offset >> 9,
632 .nr_sectors = length >> 9,
634 uint32_t zone_idx_b, zone_idx_e;
635 struct fio_zone_info *zb, *ze, *z;
639 assert(is_valid_offset(f, offset + length - 1));
640 switch (f->zbd_info->model) {
641 case ZBD_DM_HOST_AWARE:
642 case ZBD_DM_HOST_MANAGED:
643 ret = ioctl(f->fd, BLKRESETZONE, &zr);
645 td_verror(td, errno, "resetting wp failed");
646 log_err("%s: resetting wp for %llu sectors at sector %llu failed (%d).\n",
647 f->file_name, zr.nr_sectors, zr.sector, errno);
655 zone_idx_b = zbd_zone_idx(f, offset);
656 zb = &f->zbd_info->zone_info[zone_idx_b];
657 zone_idx_e = zbd_zone_idx(f, offset + length);
658 ze = &f->zbd_info->zone_info[zone_idx_e];
659 for (z = zb; z < ze; z++) {
660 pthread_mutex_lock(&z->mutex);
661 pthread_mutex_lock(&f->zbd_info->mutex);
662 f->zbd_info->sectors_with_data -= z->wp - z->start;
663 pthread_mutex_unlock(&f->zbd_info->mutex);
666 pthread_mutex_unlock(&z->mutex);
669 td->ts.nr_zone_resets += ze - zb;
674 static unsigned int zbd_zone_nr(struct zoned_block_device_info *zbd_info,
675 struct fio_zone_info *zone)
677 return zone - zbd_info->zone_info;
681 * zbd_reset_zone - reset the write pointer of a single zone
682 * @td: FIO thread data.
683 * @f: FIO file associated with the disk for which to reset a write pointer.
686 * Returns 0 upon success and a negative error code upon failure.
688 static int zbd_reset_zone(struct thread_data *td, const struct fio_file *f,
689 struct fio_zone_info *z)
691 dprint(FD_ZBD, "%s: resetting wp of zone %u.\n", f->file_name,
692 zbd_zone_nr(f->zbd_info, z));
694 return zbd_reset_range(td, f, z->start, (z+1)->start - z->start);
698 * Reset a range of zones. Returns 0 upon success and 1 upon failure.
699 * @td: fio thread data.
700 * @f: fio file for which to reset zones
701 * @zb: first zone to reset.
702 * @ze: first zone not to reset.
703 * @all_zones: whether to reset all zones or only those zones for which the
704 * write pointer is not a multiple of td->o.min_bs[DDIR_WRITE].
706 static int zbd_reset_zones(struct thread_data *td, struct fio_file *f,
707 struct fio_zone_info *const zb,
708 struct fio_zone_info *const ze, bool all_zones)
710 struct fio_zone_info *z;
711 const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
715 dprint(FD_ZBD, "%s: examining zones %u .. %u\n", f->file_name,
716 zbd_zone_nr(f->zbd_info, zb), zbd_zone_nr(f->zbd_info, ze));
718 for (z = zb; z < ze; z++) {
719 pthread_mutex_lock(&z->mutex);
720 if (z->type == BLK_ZONE_TYPE_SEQWRITE_REQ) {
721 reset_wp = all_zones ? z->wp != z->start :
722 (td->o.td_ddir & TD_DDIR_WRITE) &&
725 dprint(FD_ZBD, "%s: resetting zone %u\n",
727 zbd_zone_nr(f->zbd_info, z));
728 if (zbd_reset_zone(td, f, z) < 0)
732 pthread_mutex_unlock(&z->mutex);
739 * Reset zbd_info.write_cnt, the counter that counts down towards the next
742 static void zbd_reset_write_cnt(const struct thread_data *td,
743 const struct fio_file *f)
745 assert(0 <= td->o.zrf.u.f && td->o.zrf.u.f <= 1);
747 pthread_mutex_lock(&f->zbd_info->mutex);
748 f->zbd_info->write_cnt = td->o.zrf.u.f ?
749 min(1.0 / td->o.zrf.u.f, 0.0 + UINT_MAX) : UINT_MAX;
750 pthread_mutex_unlock(&f->zbd_info->mutex);
753 static bool zbd_dec_and_reset_write_cnt(const struct thread_data *td,
754 const struct fio_file *f)
756 uint32_t write_cnt = 0;
758 pthread_mutex_lock(&f->zbd_info->mutex);
759 assert(f->zbd_info->write_cnt);
760 if (f->zbd_info->write_cnt)
761 write_cnt = --f->zbd_info->write_cnt;
763 zbd_reset_write_cnt(td, f);
764 pthread_mutex_unlock(&f->zbd_info->mutex);
766 return write_cnt == 0;
774 /* Calculate the number of sectors with data (swd) and perform action 'a' */
775 static uint64_t zbd_process_swd(const struct fio_file *f, enum swd_action a)
777 struct fio_zone_info *zb, *ze, *z;
780 zb = &f->zbd_info->zone_info[zbd_zone_idx(f, f->file_offset)];
781 ze = &f->zbd_info->zone_info[zbd_zone_idx(f, f->file_offset +
783 for (z = zb; z < ze; z++) {
784 pthread_mutex_lock(&z->mutex);
785 swd += z->wp - z->start;
787 pthread_mutex_lock(&f->zbd_info->mutex);
790 assert(f->zbd_info->sectors_with_data == swd);
793 f->zbd_info->sectors_with_data = swd;
796 pthread_mutex_unlock(&f->zbd_info->mutex);
797 for (z = zb; z < ze; z++)
798 pthread_mutex_unlock(&z->mutex);
804 * The swd check is useful for debugging but takes too much time to leave
805 * it enabled all the time. Hence it is disabled by default.
807 static const bool enable_check_swd = false;
809 /* Check whether the value of zbd_info.sectors_with_data is correct. */
810 static void zbd_check_swd(const struct fio_file *f)
812 if (!enable_check_swd)
815 zbd_process_swd(f, CHECK_SWD);
818 static void zbd_init_swd(struct fio_file *f)
822 if (!enable_check_swd)
825 swd = zbd_process_swd(f, SET_SWD);
826 dprint(FD_ZBD, "%s(%s): swd = %" PRIu64 "\n", __func__, f->file_name,
830 void zbd_file_reset(struct thread_data *td, struct fio_file *f)
832 struct fio_zone_info *zb, *ze;
838 zb = &f->zbd_info->zone_info[zbd_zone_idx(f, f->file_offset)];
839 zone_idx_e = zbd_zone_idx(f, f->file_offset + f->io_size);
840 ze = &f->zbd_info->zone_info[zone_idx_e];
843 * If data verification is enabled reset the affected zones before
844 * writing any data to avoid that a zone reset has to be issued while
845 * writing data, which causes data loss.
847 zbd_reset_zones(td, f, zb, ze, td->o.verify != VERIFY_NONE &&
848 (td->o.td_ddir & TD_DDIR_WRITE) &&
849 td->runstate != TD_VERIFYING);
850 zbd_reset_write_cnt(td, f);
853 /* The caller must hold f->zbd_info->mutex. */
854 static bool is_zone_open(const struct thread_data *td, const struct fio_file *f,
855 unsigned int zone_idx)
857 struct zoned_block_device_info *zbdi = f->zbd_info;
860 assert(td->o.max_open_zones <= ARRAY_SIZE(zbdi->open_zones));
861 assert(zbdi->num_open_zones <= td->o.max_open_zones);
863 for (i = 0; i < zbdi->num_open_zones; i++)
864 if (zbdi->open_zones[i] == zone_idx)
871 * Open a ZBD zone if it was not yet open. Returns true if either the zone was
872 * already open or if opening a new zone is allowed. Returns false if the zone
873 * was not yet open and opening a new zone would cause the zone limit to be
876 static bool zbd_open_zone(struct thread_data *td, const struct io_u *io_u,
879 const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
880 const struct fio_file *f = io_u->file;
881 struct fio_zone_info *z = &f->zbd_info->zone_info[zone_idx];
884 if (z->cond == BLK_ZONE_COND_OFFLINE)
888 * Skip full zones with data verification enabled because resetting a
889 * zone causes data loss and hence causes verification to fail.
891 if (td->o.verify != VERIFY_NONE && zbd_zone_full(f, z, min_bs))
894 /* Zero means no limit */
895 if (!td->o.max_open_zones)
898 pthread_mutex_lock(&f->zbd_info->mutex);
899 if (is_zone_open(td, f, zone_idx))
902 if (f->zbd_info->num_open_zones >= td->o.max_open_zones)
904 dprint(FD_ZBD, "%s: opening zone %d\n", f->file_name, zone_idx);
905 f->zbd_info->open_zones[f->zbd_info->num_open_zones++] = zone_idx;
910 pthread_mutex_unlock(&f->zbd_info->mutex);
914 /* The caller must hold f->zbd_info->mutex */
915 static void zbd_close_zone(struct thread_data *td, const struct fio_file *f,
916 unsigned int open_zone_idx)
920 assert(open_zone_idx < f->zbd_info->num_open_zones);
921 zone_idx = f->zbd_info->open_zones[open_zone_idx];
922 memmove(f->zbd_info->open_zones + open_zone_idx,
923 f->zbd_info->open_zones + open_zone_idx + 1,
924 (FIO_MAX_OPEN_ZBD_ZONES - (open_zone_idx + 1)) *
925 sizeof(f->zbd_info->open_zones[0]));
926 f->zbd_info->num_open_zones--;
927 f->zbd_info->zone_info[zone_idx].open = 0;
930 static void zone_lock(struct thread_data *td, struct fio_zone_info *z)
933 * Lock the io_u target zone. The zone will be unlocked if io_u offset
934 * is changed or when io_u completes and zbd_put_io() executed.
935 * To avoid multiple jobs doing asynchronous I/Os from deadlocking each
936 * other waiting for zone locks when building an io_u batch, first
937 * only trylock the zone. If the zone is already locked by another job,
938 * process the currently queued I/Os so that I/O progress is made and
941 if (pthread_mutex_trylock(&z->mutex) != 0) {
942 if (!td_ioengine_flagged(td, FIO_SYNCIO))
944 pthread_mutex_lock(&z->mutex);
948 /* Anything goes as long as it is not a constant. */
949 static uint32_t pick_random_zone_idx(const struct fio_file *f,
950 const struct io_u *io_u)
952 return io_u->offset * f->zbd_info->num_open_zones / f->real_file_size;
956 * Modify the offset of an I/O unit that does not refer to an open zone such
957 * that it refers to an open zone. Close an open zone and open a new zone if
958 * necessary. This algorithm can only work correctly if all write pointers are
959 * a multiple of the fio block size. The caller must neither hold z->mutex
960 * nor f->zbd_info->mutex. Returns with z->mutex held upon success.
962 static struct fio_zone_info *zbd_convert_to_open_zone(struct thread_data *td,
965 const uint32_t min_bs = td->o.min_bs[io_u->ddir];
966 const struct fio_file *f = io_u->file;
967 struct fio_zone_info *z;
968 unsigned int open_zone_idx = -1;
969 uint32_t zone_idx, new_zone_idx;
972 assert(is_valid_offset(f, io_u->offset));
974 if (td->o.max_open_zones) {
976 * This statement accesses f->zbd_info->open_zones[] on purpose
979 zone_idx = f->zbd_info->open_zones[pick_random_zone_idx(f, io_u)];
981 zone_idx = zbd_zone_idx(f, io_u->offset);
983 dprint(FD_ZBD, "%s(%s): starting from zone %d (offset %lld, buflen %lld)\n",
984 __func__, f->file_name, zone_idx, io_u->offset, io_u->buflen);
987 * Since z->mutex is the outer lock and f->zbd_info->mutex the inner
988 * lock it can happen that the state of the zone with index zone_idx
989 * has changed after 'z' has been assigned and before f->zbd_info->mutex
990 * has been obtained. Hence the loop.
995 z = &f->zbd_info->zone_info[zone_idx];
998 pthread_mutex_lock(&f->zbd_info->mutex);
999 if (td->o.max_open_zones == 0)
1001 if (f->zbd_info->num_open_zones == 0) {
1002 pthread_mutex_unlock(&f->zbd_info->mutex);
1003 pthread_mutex_unlock(&z->mutex);
1004 dprint(FD_ZBD, "%s(%s): no zones are open\n",
1005 __func__, f->file_name);
1010 * List of opened zones is per-device, shared across all threads.
1011 * Start with quasi-random candidate zone.
1012 * Ignore zones which don't belong to thread's offset/size area.
1014 open_zone_idx = pick_random_zone_idx(f, io_u);
1015 assert(open_zone_idx < f->zbd_info->num_open_zones);
1016 tmp_idx = open_zone_idx;
1017 for (i = 0; i < f->zbd_info->num_open_zones; i++) {
1020 if (tmp_idx >= f->zbd_info->num_open_zones)
1022 tmpz = f->zbd_info->open_zones[tmp_idx];
1024 if (is_valid_offset(f, f->zbd_info->zone_info[tmpz].start)) {
1025 open_zone_idx = tmp_idx;
1026 goto found_candidate_zone;
1032 dprint(FD_ZBD, "%s(%s): no candidate zone\n",
1033 __func__, f->file_name);
1036 found_candidate_zone:
1037 new_zone_idx = f->zbd_info->open_zones[open_zone_idx];
1038 if (new_zone_idx == zone_idx)
1040 zone_idx = new_zone_idx;
1041 pthread_mutex_unlock(&f->zbd_info->mutex);
1042 pthread_mutex_unlock(&z->mutex);
1045 /* Both z->mutex and f->zbd_info->mutex are held. */
1048 if (z->wp + min_bs <= (z+1)->start) {
1049 pthread_mutex_unlock(&f->zbd_info->mutex);
1052 dprint(FD_ZBD, "%s(%s): closing zone %d\n", __func__, f->file_name,
1054 if (td->o.max_open_zones)
1055 zbd_close_zone(td, f, open_zone_idx);
1056 pthread_mutex_unlock(&f->zbd_info->mutex);
1058 /* Only z->mutex is held. */
1060 /* Zone 'z' is full, so try to open a new zone. */
1061 for (i = f->io_size / f->zbd_info->zone_size; i > 0; i--) {
1063 pthread_mutex_unlock(&z->mutex);
1065 if (!is_valid_offset(f, z->start)) {
1067 zone_idx = zbd_zone_idx(f, f->file_offset);
1068 z = &f->zbd_info->zone_info[zone_idx];
1070 assert(is_valid_offset(f, z->start));
1074 if (zbd_open_zone(td, io_u, zone_idx))
1078 /* Only z->mutex is held. */
1080 /* Check whether the write fits in any of the already opened zones. */
1081 pthread_mutex_lock(&f->zbd_info->mutex);
1082 for (i = 0; i < f->zbd_info->num_open_zones; i++) {
1083 zone_idx = f->zbd_info->open_zones[i];
1084 pthread_mutex_unlock(&f->zbd_info->mutex);
1085 pthread_mutex_unlock(&z->mutex);
1087 z = &f->zbd_info->zone_info[zone_idx];
1090 if (z->wp + min_bs <= (z+1)->start)
1092 pthread_mutex_lock(&f->zbd_info->mutex);
1094 pthread_mutex_unlock(&f->zbd_info->mutex);
1095 pthread_mutex_unlock(&z->mutex);
1096 dprint(FD_ZBD, "%s(%s): did not open another zone\n", __func__,
1101 dprint(FD_ZBD, "%s(%s): returning zone %d\n", __func__, f->file_name,
1103 io_u->offset = z->start;
1107 /* The caller must hold z->mutex. */
1108 static struct fio_zone_info *zbd_replay_write_order(struct thread_data *td,
1110 struct fio_zone_info *z)
1112 const struct fio_file *f = io_u->file;
1113 const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
1115 if (!zbd_open_zone(td, io_u, z - f->zbd_info->zone_info)) {
1116 pthread_mutex_unlock(&z->mutex);
1117 z = zbd_convert_to_open_zone(td, io_u);
1121 if (z->verify_block * min_bs >= f->zbd_info->zone_size)
1122 log_err("%s: %d * %d >= %llu\n", f->file_name, z->verify_block,
1123 min_bs, (unsigned long long) f->zbd_info->zone_size);
1124 io_u->offset = z->start + z->verify_block++ * min_bs;
1129 * Find another zone for which @io_u fits below the write pointer. Start
1130 * searching in zones @zb + 1 .. @zl and continue searching in zones
1133 * Either returns NULL or returns a zone pointer and holds the mutex for that
1136 static struct fio_zone_info *
1137 zbd_find_zone(struct thread_data *td, struct io_u *io_u,
1138 struct fio_zone_info *zb, struct fio_zone_info *zl)
1140 const uint32_t min_bs = td->o.min_bs[io_u->ddir];
1141 const struct fio_file *f = io_u->file;
1142 struct fio_zone_info *z1, *z2;
1143 const struct fio_zone_info *const zf =
1144 &f->zbd_info->zone_info[zbd_zone_idx(f, f->file_offset)];
1147 * Skip to the next non-empty zone in case of sequential I/O and to
1148 * the nearest non-empty zone in case of random I/O.
1150 for (z1 = zb + 1, z2 = zb - 1; z1 < zl || z2 >= zf; z1++, z2--) {
1151 if (z1 < zl && z1->cond != BLK_ZONE_COND_OFFLINE) {
1152 pthread_mutex_lock(&z1->mutex);
1153 if (z1->start + min_bs <= z1->wp)
1155 pthread_mutex_unlock(&z1->mutex);
1156 } else if (!td_random(td)) {
1159 if (td_random(td) && z2 >= zf &&
1160 z2->cond != BLK_ZONE_COND_OFFLINE) {
1161 pthread_mutex_lock(&z2->mutex);
1162 if (z2->start + min_bs <= z2->wp)
1164 pthread_mutex_unlock(&z2->mutex);
1167 dprint(FD_ZBD, "%s: adjusting random read offset failed\n",
1173 * zbd_queue_io - update the write pointer of a sequential zone
1175 * @success: Whether or not the I/O unit has been queued successfully
1176 * @q: queueing status (busy, completed or queued).
1178 * For write and trim operations, update the write pointer of the I/O unit
1181 static void zbd_queue_io(struct io_u *io_u, int q, bool success)
1183 const struct fio_file *f = io_u->file;
1184 struct zoned_block_device_info *zbd_info = f->zbd_info;
1185 struct fio_zone_info *z;
1192 zone_idx = zbd_zone_idx(f, io_u->offset);
1193 assert(zone_idx < zbd_info->nr_zones);
1194 z = &zbd_info->zone_info[zone_idx];
1196 if (z->type != BLK_ZONE_TYPE_SEQWRITE_REQ)
1203 "%s: queued I/O (%lld, %llu) for zone %u\n",
1204 f->file_name, io_u->offset, io_u->buflen, zone_idx);
1206 switch (io_u->ddir) {
1208 zone_end = min((uint64_t)(io_u->offset + io_u->buflen),
1210 pthread_mutex_lock(&zbd_info->mutex);
1212 * z->wp > zone_end means that one or more I/O errors
1215 if (z->wp <= zone_end)
1216 zbd_info->sectors_with_data += zone_end - z->wp;
1217 pthread_mutex_unlock(&zbd_info->mutex);
1221 assert(z->wp == z->start);
1228 if (!success || q != FIO_Q_QUEUED) {
1229 /* BUSY or COMPLETED: unlock the zone */
1230 pthread_mutex_unlock(&z->mutex);
1231 io_u->zbd_put_io = NULL;
1236 * zbd_put_io - Unlock an I/O unit target zone lock
1239 static void zbd_put_io(const struct io_u *io_u)
1241 const struct fio_file *f = io_u->file;
1242 struct zoned_block_device_info *zbd_info = f->zbd_info;
1243 struct fio_zone_info *z;
1249 zone_idx = zbd_zone_idx(f, io_u->offset);
1250 assert(zone_idx < zbd_info->nr_zones);
1251 z = &zbd_info->zone_info[zone_idx];
1253 if (z->type != BLK_ZONE_TYPE_SEQWRITE_REQ)
1257 "%s: terminate I/O (%lld, %llu) for zone %u\n",
1258 f->file_name, io_u->offset, io_u->buflen, zone_idx);
1260 assert(pthread_mutex_unlock(&z->mutex) == 0);
1264 bool zbd_unaligned_write(int error_code)
1266 switch (error_code) {
1275 * setup_zbd_zone_mode - handle zoneskip as necessary for ZBD drives
1276 * @td: FIO thread data.
1277 * @io_u: FIO I/O unit.
1279 * For sequential workloads, change the file offset to skip zoneskip bytes when
1280 * no more IO can be performed in the current zone.
1281 * - For read workloads, zoneskip is applied when the io has reached the end of
1282 * the zone or the zone write position (when td->o.read_beyond_wp is false).
1283 * - For write workloads, zoneskip is applied when the zone is full.
1284 * This applies only to read and write operations.
1286 void setup_zbd_zone_mode(struct thread_data *td, struct io_u *io_u)
1288 struct fio_file *f = io_u->file;
1289 enum fio_ddir ddir = io_u->ddir;
1290 struct fio_zone_info *z;
1293 assert(td->o.zone_mode == ZONE_MODE_ZBD);
1294 assert(td->o.zone_size);
1297 * zone_skip is valid only for sequential workloads.
1299 if (td_random(td) || !td->o.zone_skip)
1303 * It is time to switch to a new zone if:
1304 * - zone_bytes == zone_size bytes have already been accessed
1305 * - The last position reached the end of the current zone.
1306 * - For reads with td->o.read_beyond_wp == false, the last position
1307 * reached the zone write pointer.
1309 zone_idx = zbd_zone_idx(f, f->last_pos[ddir]);
1310 z = &f->zbd_info->zone_info[zone_idx];
1312 if (td->zone_bytes >= td->o.zone_size ||
1313 f->last_pos[ddir] >= (z+1)->start ||
1314 (ddir == DDIR_READ &&
1315 (!td->o.read_beyond_wp) && f->last_pos[ddir] >= z->wp)) {
1320 f->file_offset += td->o.zone_size + td->o.zone_skip;
1323 * Wrap from the beginning, if we exceed the file size
1325 if (f->file_offset >= f->real_file_size)
1326 f->file_offset = get_start_offset(td, f);
1328 f->last_pos[ddir] = f->file_offset;
1329 td->io_skip_bytes += td->o.zone_skip;
1334 * zbd_adjust_block - adjust the offset and length as necessary for ZBD drives
1335 * @td: FIO thread data.
1336 * @io_u: FIO I/O unit.
1338 * Locking strategy: returns with z->mutex locked if and only if z refers
1339 * to a sequential zone and if io_u_accept is returned. z is the zone that
1340 * corresponds to io_u->offset at the end of this function.
1342 enum io_u_action zbd_adjust_block(struct thread_data *td, struct io_u *io_u)
1344 const struct fio_file *f = io_u->file;
1345 uint32_t zone_idx_b;
1346 struct fio_zone_info *zb, *zl, *orig_zb;
1347 uint32_t orig_len = io_u->buflen;
1348 uint32_t min_bs = td->o.min_bs[io_u->ddir];
1355 assert(is_valid_offset(f, io_u->offset));
1356 assert(io_u->buflen);
1357 zone_idx_b = zbd_zone_idx(f, io_u->offset);
1358 zb = &f->zbd_info->zone_info[zone_idx_b];
1361 /* Accept the I/O offset for conventional zones. */
1362 if (zb->type == BLK_ZONE_TYPE_CONVENTIONAL)
1366 * Accept the I/O offset for reads if reading beyond the write pointer
1369 if (zb->cond != BLK_ZONE_COND_OFFLINE &&
1370 io_u->ddir == DDIR_READ && td->o.read_beyond_wp)
1377 switch (io_u->ddir) {
1379 if (td->runstate == TD_VERIFYING) {
1380 zb = zbd_replay_write_order(td, io_u, zb);
1384 * Check that there is enough written data in the zone to do an
1385 * I/O of at least min_bs B. If there isn't, find a new zone for
1388 range = zb->cond != BLK_ZONE_COND_OFFLINE ?
1389 zb->wp - zb->start : 0;
1390 if (range < min_bs ||
1391 ((!td_random(td)) && (io_u->offset + min_bs > zb->wp))) {
1392 pthread_mutex_unlock(&zb->mutex);
1393 zl = &f->zbd_info->zone_info[zbd_zone_idx(f,
1394 f->file_offset + f->io_size)];
1395 zb = zbd_find_zone(td, io_u, zb, zl);
1398 "%s: zbd_find_zone(%lld, %llu) failed\n",
1399 f->file_name, io_u->offset,
1404 * zbd_find_zone() returned a zone with a range of at
1407 range = zb->wp - zb->start;
1408 assert(range >= min_bs);
1411 io_u->offset = zb->start;
1414 * Make sure the I/O is within the zone valid data range while
1415 * maximizing the I/O size and preserving randomness.
1417 if (range <= io_u->buflen)
1418 io_u->offset = zb->start;
1419 else if (td_random(td))
1420 io_u->offset = zb->start +
1421 ((io_u->offset - orig_zb->start) %
1422 (range - io_u->buflen)) / min_bs * min_bs;
1424 * Make sure the I/O does not cross over the zone wp position.
1426 new_len = min((unsigned long long)io_u->buflen,
1427 (unsigned long long)(zb->wp - io_u->offset));
1428 new_len = new_len / min_bs * min_bs;
1429 if (new_len < io_u->buflen) {
1430 io_u->buflen = new_len;
1431 dprint(FD_IO, "Changed length from %u into %llu\n",
1432 orig_len, io_u->buflen);
1434 assert(zb->start <= io_u->offset);
1435 assert(io_u->offset + io_u->buflen <= zb->wp);
1438 if (io_u->buflen > f->zbd_info->zone_size)
1440 if (!zbd_open_zone(td, io_u, zone_idx_b)) {
1441 pthread_mutex_unlock(&zb->mutex);
1442 zb = zbd_convert_to_open_zone(td, io_u);
1445 zone_idx_b = zb - f->zbd_info->zone_info;
1447 /* Check whether the zone reset threshold has been exceeded */
1448 if (td->o.zrf.u.f) {
1449 if (f->zbd_info->sectors_with_data >=
1450 f->io_size * td->o.zrt.u.f &&
1451 zbd_dec_and_reset_write_cnt(td, f)) {
1455 /* Reset the zone pointer if necessary */
1456 if (zb->reset_zone || zbd_zone_full(f, zb, min_bs)) {
1457 assert(td->o.verify == VERIFY_NONE);
1459 * Since previous write requests may have been submitted
1460 * asynchronously and since we will submit the zone
1461 * reset synchronously, wait until previously submitted
1462 * write requests have completed before issuing a
1467 if (zbd_reset_zone(td, f, zb) < 0)
1470 /* Make writes occur at the write pointer */
1471 assert(!zbd_zone_full(f, zb, min_bs));
1472 io_u->offset = zb->wp;
1473 if (!is_valid_offset(f, io_u->offset)) {
1474 dprint(FD_ZBD, "Dropped request with offset %llu\n",
1479 * Make sure that the buflen is a multiple of the minimal
1480 * block size. Give up if shrinking would make the request too
1483 new_len = min((unsigned long long)io_u->buflen,
1484 (zb + 1)->start - io_u->offset);
1485 new_len = new_len / min_bs * min_bs;
1486 if (new_len == io_u->buflen)
1488 if (new_len >= min_bs) {
1489 io_u->buflen = new_len;
1490 dprint(FD_IO, "Changed length from %u into %llu\n",
1491 orig_len, io_u->buflen);
1494 log_err("Zone remainder %lld smaller than minimum block size %d\n",
1495 ((zb + 1)->start - io_u->offset),
1502 case DDIR_SYNC_FILE_RANGE:
1513 assert(zb->cond != BLK_ZONE_COND_OFFLINE);
1514 assert(!io_u->zbd_queue_io);
1515 assert(!io_u->zbd_put_io);
1516 io_u->zbd_queue_io = zbd_queue_io;
1517 io_u->zbd_put_io = zbd_put_io;
1522 pthread_mutex_unlock(&zb->mutex);
1526 /* Return a string with ZBD statistics */
1527 char *zbd_write_status(const struct thread_stat *ts)
1531 if (asprintf(&res, "; %llu zone resets", (unsigned long long) ts->nr_zone_resets) < 0)