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>
25 * zbd_zone_idx - convert an offset into a zone number
27 * @offset: offset in bytes. If this offset is in the first zone_size bytes
28 * past the disk size then the index of the sentinel is returned.
30 static uint32_t zbd_zone_idx(const struct fio_file *f, uint64_t offset)
34 if (f->zbd_info->zone_size_log2 > 0)
35 zone_idx = offset >> f->zbd_info->zone_size_log2;
37 zone_idx = offset / f->zbd_info->zone_size;
39 return min(zone_idx, f->zbd_info->nr_zones);
43 * zbd_zone_full - verify whether a minimum number of bytes remain in a zone
45 * @z: zone info pointer.
46 * @required: minimum number of bytes that must remain in a zone.
48 * The caller must hold z->mutex.
50 static bool zbd_zone_full(const struct fio_file *f, struct fio_zone_info *z,
53 assert((required & 511) == 0);
55 return z->type == BLK_ZONE_TYPE_SEQWRITE_REQ &&
56 z->wp + required > z->start + f->zbd_info->zone_size;
59 static bool is_valid_offset(const struct fio_file *f, uint64_t offset)
61 return (uint64_t)(offset - f->file_offset) < f->io_size;
64 /* Verify whether direct I/O is used for all host-managed zoned drives. */
65 static bool zbd_using_direct_io(void)
67 struct thread_data *td;
72 if (td->o.odirect || !(td->o.td_ddir & TD_DDIR_WRITE))
74 for_each_file(td, f, j) {
76 f->zbd_info->model == ZBD_DM_HOST_MANAGED)
84 /* Whether or not the I/O range for f includes one or more sequential zones */
85 static bool zbd_is_seq_job(struct fio_file *f)
87 uint32_t zone_idx, zone_idx_b, zone_idx_e;
92 zone_idx_b = zbd_zone_idx(f, f->file_offset);
93 zone_idx_e = zbd_zone_idx(f, f->file_offset + f->io_size - 1);
94 for (zone_idx = zone_idx_b; zone_idx <= zone_idx_e; zone_idx++)
95 if (f->zbd_info->zone_info[zone_idx].type ==
96 BLK_ZONE_TYPE_SEQWRITE_REQ)
103 * Verify whether offset and size parameters are aligned with zone boundaries.
105 static bool zbd_verify_sizes(void)
107 const struct fio_zone_info *z;
108 struct thread_data *td;
110 uint64_t new_offset, new_end;
115 for_each_file(td, f, j) {
118 if (f->file_offset >= f->real_file_size)
120 if (!zbd_is_seq_job(f))
123 if (td->o.zone_skip &&
124 (td->o.zone_skip < td->o.zone_size ||
125 td->o.zone_skip % td->o.zone_size)) {
126 log_err("%s: zoneskip %llu is not a multiple of the device zone size %llu.\n",
127 f->file_name, (unsigned long long) td->o.zone_skip,
128 (unsigned long long) td->o.zone_size);
132 zone_idx = zbd_zone_idx(f, f->file_offset);
133 z = &f->zbd_info->zone_info[zone_idx];
134 if (f->file_offset != z->start) {
135 new_offset = (z+1)->start;
136 if (new_offset >= f->file_offset + f->io_size) {
137 log_info("%s: io_size must be at least one zone\n",
141 log_info("%s: rounded up offset from %llu to %llu\n",
142 f->file_name, (unsigned long long) f->file_offset,
143 (unsigned long long) new_offset);
144 f->io_size -= (new_offset - f->file_offset);
145 f->file_offset = new_offset;
147 zone_idx = zbd_zone_idx(f, f->file_offset + f->io_size);
148 z = &f->zbd_info->zone_info[zone_idx];
150 if (f->file_offset + f->io_size != new_end) {
151 if (new_end <= f->file_offset) {
152 log_info("%s: io_size must be at least one zone\n",
156 log_info("%s: rounded down io_size from %llu to %llu\n",
157 f->file_name, (unsigned long long) f->io_size,
158 (unsigned long long) new_end - f->file_offset);
159 f->io_size = new_end - f->file_offset;
167 static bool zbd_verify_bs(void)
169 struct thread_data *td;
175 for_each_file(td, f, j) {
178 zone_size = f->zbd_info->zone_size;
179 for (k = 0; k < ARRAY_SIZE(td->o.bs); k++) {
180 if (td->o.verify != VERIFY_NONE &&
181 zone_size % td->o.bs[k] != 0) {
182 log_info("%s: block size %llu is not a divisor of the zone size %d\n",
183 f->file_name, td->o.bs[k],
194 * Read zone information into @buf starting from sector @start_sector.
195 * @fd is a file descriptor that refers to a block device and @bufsz is the
198 * Returns 0 upon success and a negative error code upon failure.
199 * If the zone report is empty, always assume an error (device problem) and
202 static int read_zone_info(int fd, uint64_t start_sector,
203 void *buf, unsigned int bufsz)
205 struct blk_zone_report *hdr = buf;
208 if (bufsz < sizeof(*hdr))
211 memset(hdr, 0, sizeof(*hdr));
213 hdr->nr_zones = (bufsz - sizeof(*hdr)) / sizeof(struct blk_zone);
214 hdr->sector = start_sector;
215 ret = ioctl(fd, BLKREPORTZONE, hdr);
224 * Read up to 255 characters from the first line of a file. Strip the trailing
227 static char *read_file(const char *path)
229 char line[256], *p = line;
232 f = fopen(path, "rb");
235 if (!fgets(line, sizeof(line), f))
243 static enum blk_zoned_model get_zbd_model(const char *file_name)
245 enum blk_zoned_model model = ZBD_DM_NONE;
246 char *zoned_attr_path = NULL;
247 char *model_str = NULL;
249 char *sys_devno_path = NULL;
250 char *part_attr_path = NULL;
251 char *part_str = NULL;
252 char sys_path[PATH_MAX];
256 if (stat(file_name, &statbuf) < 0)
259 if (asprintf(&sys_devno_path, "/sys/dev/block/%d:%d",
260 major(statbuf.st_rdev), minor(statbuf.st_rdev)) < 0)
263 sz = readlink(sys_devno_path, sys_path, sizeof(sys_path) - 1);
269 * If the device is a partition device, cut the device name in the
270 * canonical sysfs path to obtain the sysfs path of the holder device.
271 * e.g.: /sys/devices/.../sda/sda1 -> /sys/devices/.../sda
273 if (asprintf(&part_attr_path, "/sys/dev/block/%s/partition",
276 part_str = read_file(part_attr_path);
277 if (part_str && *part_str == '1') {
278 delim = strrchr(sys_path, '/');
284 if (asprintf(&zoned_attr_path,
285 "/sys/dev/block/%s/queue/zoned", sys_path) < 0)
288 model_str = read_file(zoned_attr_path);
291 dprint(FD_ZBD, "%s: zbd model string: %s\n", file_name, model_str);
292 if (strcmp(model_str, "host-aware") == 0)
293 model = ZBD_DM_HOST_AWARE;
294 else if (strcmp(model_str, "host-managed") == 0)
295 model = ZBD_DM_HOST_MANAGED;
299 free(zoned_attr_path);
301 free(part_attr_path);
302 free(sys_devno_path);
306 static int ilog2(uint64_t i)
318 * Initialize f->zbd_info for devices that are not zoned block devices. This
319 * allows to execute a ZBD workload against a non-ZBD device.
321 static int init_zone_info(struct thread_data *td, struct fio_file *f)
324 struct fio_zone_info *p;
325 uint64_t zone_size = td->o.zone_size;
326 struct zoned_block_device_info *zbd_info = NULL;
327 pthread_mutexattr_t attr;
330 if (zone_size == 0) {
331 log_err("%s: Specifying the zone size is mandatory for regular block devices with --zonemode=zbd\n\n",
336 if (zone_size < 512) {
337 log_err("%s: zone size must be at least 512 bytes for --zonemode=zbd\n\n",
342 nr_zones = (f->real_file_size + zone_size - 1) / zone_size;
343 zbd_info = scalloc(1, sizeof(*zbd_info) +
344 (nr_zones + 1) * sizeof(zbd_info->zone_info[0]));
348 pthread_mutexattr_init(&attr);
349 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
350 pthread_mutexattr_setpshared(&attr, true);
351 pthread_mutex_init(&zbd_info->mutex, &attr);
352 zbd_info->refcount = 1;
353 p = &zbd_info->zone_info[0];
354 for (i = 0; i < nr_zones; i++, p++) {
355 pthread_mutex_init(&p->mutex, &attr);
356 p->start = i * zone_size;
357 p->wp = p->start + zone_size;
358 p->type = BLK_ZONE_TYPE_SEQWRITE_REQ;
359 p->cond = BLK_ZONE_COND_EMPTY;
362 p->start = nr_zones * zone_size;
364 f->zbd_info = zbd_info;
365 f->zbd_info->zone_size = zone_size;
366 f->zbd_info->zone_size_log2 = is_power_of_2(zone_size) ?
367 ilog2(zone_size) : -1;
368 f->zbd_info->nr_zones = nr_zones;
369 pthread_mutexattr_destroy(&attr);
374 * Parse the BLKREPORTZONE output and store it in f->zbd_info. Must be called
375 * only for devices that support this ioctl, namely zoned block devices.
377 static int parse_zone_info(struct thread_data *td, struct fio_file *f)
379 const unsigned int bufsz = sizeof(struct blk_zone_report) +
380 4096 * sizeof(struct blk_zone);
382 struct blk_zone_report *hdr;
383 const struct blk_zone *z;
384 struct fio_zone_info *p;
385 uint64_t zone_size, start_sector;
386 struct zoned_block_device_info *zbd_info = NULL;
387 pthread_mutexattr_t attr;
389 int fd, i, j, ret = 0;
391 pthread_mutexattr_init(&attr);
392 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
393 pthread_mutexattr_setpshared(&attr, true);
399 fd = open(f->file_name, O_RDONLY | O_LARGEFILE);
405 ret = read_zone_info(fd, 0, buf, bufsz);
407 log_info("fio: BLKREPORTZONE(%lu) failed for %s (%d).\n",
408 0UL, f->file_name, -ret);
412 if (hdr->nr_zones < 1) {
413 log_info("fio: %s has invalid zone information.\n",
417 z = (void *)(hdr + 1);
418 zone_size = z->len << 9;
419 nr_zones = (f->real_file_size + zone_size - 1) / zone_size;
421 if (td->o.zone_size == 0) {
422 td->o.zone_size = zone_size;
423 } else if (td->o.zone_size != zone_size) {
424 log_err("fio: %s job parameter zonesize %llu does not match disk zone size %llu.\n",
425 f->file_name, (unsigned long long) td->o.zone_size,
426 (unsigned long long) zone_size);
431 dprint(FD_ZBD, "Device %s has %d zones of size %llu KB\n", f->file_name,
432 nr_zones, (unsigned long long) zone_size / 1024);
434 zbd_info = scalloc(1, sizeof(*zbd_info) +
435 (nr_zones + 1) * sizeof(zbd_info->zone_info[0]));
439 pthread_mutex_init(&zbd_info->mutex, &attr);
440 zbd_info->refcount = 1;
441 p = &zbd_info->zone_info[0];
442 for (start_sector = 0, j = 0; j < nr_zones;) {
443 z = (void *)(hdr + 1);
444 for (i = 0; i < hdr->nr_zones; i++, j++, z++, p++) {
445 pthread_mutex_init(&p->mutex, &attr);
446 p->start = z->start << 9;
448 case BLK_ZONE_COND_NOT_WP:
449 case BLK_ZONE_COND_FULL:
450 p->wp = p->start + zone_size;
453 assert(z->start <= z->wp);
454 assert(z->wp <= z->start + (zone_size >> 9));
460 if (j > 0 && p->start != p[-1].start + zone_size) {
461 log_info("%s: invalid zone data\n",
468 start_sector = z->start + z->len;
471 ret = read_zone_info(fd, start_sector, buf, bufsz);
473 log_info("fio: BLKREPORTZONE(%llu) failed for %s (%d).\n",
474 (unsigned long long) start_sector, f->file_name, -ret);
479 zbd_info->zone_info[nr_zones].start = start_sector << 9;
481 f->zbd_info = zbd_info;
482 f->zbd_info->zone_size = zone_size;
483 f->zbd_info->zone_size_log2 = is_power_of_2(zone_size) ?
484 ilog2(zone_size) : -1;
485 f->zbd_info->nr_zones = nr_zones;
495 pthread_mutexattr_destroy(&attr);
500 * Allocate zone information and store it into f->zbd_info if zonemode=zbd.
502 * Returns 0 upon success and a negative error code upon failure.
504 static int zbd_create_zone_info(struct thread_data *td, struct fio_file *f)
506 enum blk_zoned_model zbd_model;
509 assert(td->o.zone_mode == ZONE_MODE_ZBD);
511 zbd_model = get_zbd_model(f->file_name);
513 case ZBD_DM_HOST_AWARE:
514 case ZBD_DM_HOST_MANAGED:
515 ret = parse_zone_info(td, f);
518 ret = init_zone_info(td, f);
522 f->zbd_info->model = zbd_model;
526 void zbd_free_zone_info(struct fio_file *f)
533 pthread_mutex_lock(&f->zbd_info->mutex);
534 refcount = --f->zbd_info->refcount;
535 pthread_mutex_unlock(&f->zbd_info->mutex);
537 assert((int32_t)refcount >= 0);
544 * Initialize f->zbd_info.
546 * Returns 0 upon success and a negative error code upon failure.
548 * Note: this function can only work correctly if it is called before the first
551 static int zbd_init_zone_info(struct thread_data *td, struct fio_file *file)
553 struct thread_data *td2;
557 for_each_td(td2, i) {
558 for_each_file(td2, f2, j) {
559 if (td2 == td && f2 == file)
562 strcmp(f2->file_name, file->file_name) != 0)
564 file->zbd_info = f2->zbd_info;
565 file->zbd_info->refcount++;
570 ret = zbd_create_zone_info(td, file);
572 td_verror(td, -ret, "zbd_create_zone_info() failed");
576 int zbd_init(struct thread_data *td)
581 for_each_file(td, f, i) {
582 if (f->filetype != FIO_TYPE_BLOCK)
584 if (zbd_init_zone_info(td, f))
588 if (!zbd_using_direct_io()) {
589 log_err("Using direct I/O is mandatory for writing to ZBD drives\n\n");
593 if (!zbd_verify_sizes())
596 if (!zbd_verify_bs())
603 * zbd_reset_range - reset zones for a range of sectors
604 * @td: FIO thread data.
605 * @f: Fio file for which to reset zones
606 * @sector: Starting sector in units of 512 bytes
607 * @nr_sectors: Number of sectors in units of 512 bytes
609 * Returns 0 upon success and a negative error code upon failure.
611 static int zbd_reset_range(struct thread_data *td, const struct fio_file *f,
612 uint64_t offset, uint64_t length)
614 struct blk_zone_range zr = {
615 .sector = offset >> 9,
616 .nr_sectors = length >> 9,
618 uint32_t zone_idx_b, zone_idx_e;
619 struct fio_zone_info *zb, *ze, *z;
623 assert(is_valid_offset(f, offset + length - 1));
624 switch (f->zbd_info->model) {
625 case ZBD_DM_HOST_AWARE:
626 case ZBD_DM_HOST_MANAGED:
627 ret = ioctl(f->fd, BLKRESETZONE, &zr);
629 td_verror(td, errno, "resetting wp failed");
630 log_err("%s: resetting wp for %llu sectors at sector %llu failed (%d).\n",
631 f->file_name, zr.nr_sectors, zr.sector, errno);
639 zone_idx_b = zbd_zone_idx(f, offset);
640 zb = &f->zbd_info->zone_info[zone_idx_b];
641 zone_idx_e = zbd_zone_idx(f, offset + length);
642 ze = &f->zbd_info->zone_info[zone_idx_e];
643 for (z = zb; z < ze; z++) {
644 pthread_mutex_lock(&z->mutex);
645 pthread_mutex_lock(&f->zbd_info->mutex);
646 f->zbd_info->sectors_with_data -= z->wp - z->start;
647 pthread_mutex_unlock(&f->zbd_info->mutex);
650 pthread_mutex_unlock(&z->mutex);
653 td->ts.nr_zone_resets += ze - zb;
658 static unsigned int zbd_zone_nr(struct zoned_block_device_info *zbd_info,
659 struct fio_zone_info *zone)
661 return zone - zbd_info->zone_info;
665 * zbd_reset_zone - reset the write pointer of a single zone
666 * @td: FIO thread data.
667 * @f: FIO file associated with the disk for which to reset a write pointer.
670 * Returns 0 upon success and a negative error code upon failure.
672 static int zbd_reset_zone(struct thread_data *td, const struct fio_file *f,
673 struct fio_zone_info *z)
675 dprint(FD_ZBD, "%s: resetting wp of zone %u.\n", f->file_name,
676 zbd_zone_nr(f->zbd_info, z));
678 return zbd_reset_range(td, f, z->start, (z+1)->start - z->start);
682 * Reset a range of zones. Returns 0 upon success and 1 upon failure.
683 * @td: fio thread data.
684 * @f: fio file for which to reset zones
685 * @zb: first zone to reset.
686 * @ze: first zone not to reset.
687 * @all_zones: whether to reset all zones or only those zones for which the
688 * write pointer is not a multiple of td->o.min_bs[DDIR_WRITE].
690 static int zbd_reset_zones(struct thread_data *td, struct fio_file *f,
691 struct fio_zone_info *const zb,
692 struct fio_zone_info *const ze, bool all_zones)
694 struct fio_zone_info *z, *start_z = ze;
695 const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
699 dprint(FD_ZBD, "%s: examining zones %u .. %u\n", f->file_name,
700 zbd_zone_nr(f->zbd_info, zb), zbd_zone_nr(f->zbd_info, ze));
702 for (z = zb; z < ze; z++) {
703 pthread_mutex_lock(&z->mutex);
705 case BLK_ZONE_TYPE_SEQWRITE_REQ:
706 reset_wp = all_zones ? z->wp != z->start :
707 (td->o.td_ddir & TD_DDIR_WRITE) &&
709 if (start_z == ze && reset_wp) {
711 } else if (start_z < ze && !reset_wp) {
713 "%s: resetting zones %u .. %u\n",
715 zbd_zone_nr(f->zbd_info, start_z),
716 zbd_zone_nr(f->zbd_info, z));
717 if (zbd_reset_range(td, f, start_z->start,
718 z->start - start_z->start) < 0)
726 dprint(FD_ZBD, "%s: resetting zones %u .. %u\n",
727 f->file_name, zbd_zone_nr(f->zbd_info, start_z),
728 zbd_zone_nr(f->zbd_info, z));
729 if (zbd_reset_range(td, f, start_z->start,
730 z->start - start_z->start) < 0)
737 dprint(FD_ZBD, "%s: resetting zones %u .. %u\n", f->file_name,
738 zbd_zone_nr(f->zbd_info, start_z),
739 zbd_zone_nr(f->zbd_info, z));
740 if (zbd_reset_range(td, f, start_z->start,
741 z->start - start_z->start) < 0)
744 for (z = zb; z < ze; z++)
745 pthread_mutex_unlock(&z->mutex);
751 * Reset zbd_info.write_cnt, the counter that counts down towards the next
754 static void zbd_reset_write_cnt(const struct thread_data *td,
755 const struct fio_file *f)
757 assert(0 <= td->o.zrf.u.f && td->o.zrf.u.f <= 1);
759 pthread_mutex_lock(&f->zbd_info->mutex);
760 f->zbd_info->write_cnt = td->o.zrf.u.f ?
761 min(1.0 / td->o.zrf.u.f, 0.0 + UINT_MAX) : UINT_MAX;
762 pthread_mutex_unlock(&f->zbd_info->mutex);
765 static bool zbd_dec_and_reset_write_cnt(const struct thread_data *td,
766 const struct fio_file *f)
768 uint32_t write_cnt = 0;
770 pthread_mutex_lock(&f->zbd_info->mutex);
771 assert(f->zbd_info->write_cnt);
772 if (f->zbd_info->write_cnt)
773 write_cnt = --f->zbd_info->write_cnt;
775 zbd_reset_write_cnt(td, f);
776 pthread_mutex_unlock(&f->zbd_info->mutex);
778 return write_cnt == 0;
786 /* Calculate the number of sectors with data (swd) and perform action 'a' */
787 static uint64_t zbd_process_swd(const struct fio_file *f, enum swd_action a)
789 struct fio_zone_info *zb, *ze, *z;
792 zb = &f->zbd_info->zone_info[zbd_zone_idx(f, f->file_offset)];
793 ze = &f->zbd_info->zone_info[zbd_zone_idx(f, f->file_offset +
795 for (z = zb; z < ze; z++) {
796 pthread_mutex_lock(&z->mutex);
797 swd += z->wp - z->start;
799 pthread_mutex_lock(&f->zbd_info->mutex);
802 assert(f->zbd_info->sectors_with_data == swd);
805 f->zbd_info->sectors_with_data = swd;
808 pthread_mutex_unlock(&f->zbd_info->mutex);
809 for (z = zb; z < ze; z++)
810 pthread_mutex_unlock(&z->mutex);
816 * The swd check is useful for debugging but takes too much time to leave
817 * it enabled all the time. Hence it is disabled by default.
819 static const bool enable_check_swd = false;
821 /* Check whether the value of zbd_info.sectors_with_data is correct. */
822 static void zbd_check_swd(const struct fio_file *f)
824 if (!enable_check_swd)
827 zbd_process_swd(f, CHECK_SWD);
830 static void zbd_init_swd(struct fio_file *f)
834 swd = zbd_process_swd(f, SET_SWD);
835 dprint(FD_ZBD, "%s(%s): swd = %" PRIu64 "\n", __func__, f->file_name,
839 void zbd_file_reset(struct thread_data *td, struct fio_file *f)
841 struct fio_zone_info *zb, *ze;
847 zb = &f->zbd_info->zone_info[zbd_zone_idx(f, f->file_offset)];
848 zone_idx_e = zbd_zone_idx(f, f->file_offset + f->io_size);
849 ze = &f->zbd_info->zone_info[zone_idx_e];
852 * If data verification is enabled reset the affected zones before
853 * writing any data to avoid that a zone reset has to be issued while
854 * writing data, which causes data loss.
856 zbd_reset_zones(td, f, zb, ze, td->o.verify != VERIFY_NONE &&
857 (td->o.td_ddir & TD_DDIR_WRITE) &&
858 td->runstate != TD_VERIFYING);
859 zbd_reset_write_cnt(td, f);
862 /* The caller must hold f->zbd_info->mutex. */
863 static bool is_zone_open(const struct thread_data *td, const struct fio_file *f,
864 unsigned int zone_idx)
866 struct zoned_block_device_info *zbdi = f->zbd_info;
869 assert(td->o.max_open_zones <= ARRAY_SIZE(zbdi->open_zones));
870 assert(zbdi->num_open_zones <= td->o.max_open_zones);
872 for (i = 0; i < zbdi->num_open_zones; i++)
873 if (zbdi->open_zones[i] == zone_idx)
880 * Open a ZBD zone if it was not yet open. Returns true if either the zone was
881 * already open or if opening a new zone is allowed. Returns false if the zone
882 * was not yet open and opening a new zone would cause the zone limit to be
885 static bool zbd_open_zone(struct thread_data *td, const struct io_u *io_u,
888 const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
889 const struct fio_file *f = io_u->file;
890 struct fio_zone_info *z = &f->zbd_info->zone_info[zone_idx];
893 if (z->cond == BLK_ZONE_COND_OFFLINE)
897 * Skip full zones with data verification enabled because resetting a
898 * zone causes data loss and hence causes verification to fail.
900 if (td->o.verify != VERIFY_NONE && zbd_zone_full(f, z, min_bs))
903 /* Zero means no limit */
904 if (!td->o.max_open_zones)
907 pthread_mutex_lock(&f->zbd_info->mutex);
908 if (is_zone_open(td, f, zone_idx))
911 if (f->zbd_info->num_open_zones >= td->o.max_open_zones)
913 dprint(FD_ZBD, "%s: opening zone %d\n", f->file_name, zone_idx);
914 f->zbd_info->open_zones[f->zbd_info->num_open_zones++] = zone_idx;
919 pthread_mutex_unlock(&f->zbd_info->mutex);
923 /* The caller must hold f->zbd_info->mutex */
924 static void zbd_close_zone(struct thread_data *td, const struct fio_file *f,
925 unsigned int open_zone_idx)
929 assert(open_zone_idx < f->zbd_info->num_open_zones);
930 zone_idx = f->zbd_info->open_zones[open_zone_idx];
931 memmove(f->zbd_info->open_zones + open_zone_idx,
932 f->zbd_info->open_zones + open_zone_idx + 1,
933 (FIO_MAX_OPEN_ZBD_ZONES - (open_zone_idx + 1)) *
934 sizeof(f->zbd_info->open_zones[0]));
935 f->zbd_info->num_open_zones--;
936 f->zbd_info->zone_info[zone_idx].open = 0;
940 * Modify the offset of an I/O unit that does not refer to an open zone such
941 * that it refers to an open zone. Close an open zone and open a new zone if
942 * necessary. This algorithm can only work correctly if all write pointers are
943 * a multiple of the fio block size. The caller must neither hold z->mutex
944 * nor f->zbd_info->mutex. Returns with z->mutex held upon success.
946 static struct fio_zone_info *zbd_convert_to_open_zone(struct thread_data *td,
949 const uint32_t min_bs = td->o.min_bs[io_u->ddir];
950 const struct fio_file *f = io_u->file;
951 struct fio_zone_info *z;
952 unsigned int open_zone_idx = -1;
953 uint32_t zone_idx, new_zone_idx;
956 assert(is_valid_offset(f, io_u->offset));
958 if (td->o.max_open_zones) {
960 * This statement accesses f->zbd_info->open_zones[] on purpose
963 zone_idx = f->zbd_info->open_zones[(io_u->offset -
965 f->zbd_info->num_open_zones / f->io_size];
967 zone_idx = zbd_zone_idx(f, io_u->offset);
969 dprint(FD_ZBD, "%s(%s): starting from zone %d (offset %lld, buflen %lld)\n",
970 __func__, f->file_name, zone_idx, io_u->offset, io_u->buflen);
973 * Since z->mutex is the outer lock and f->zbd_info->mutex the inner
974 * lock it can happen that the state of the zone with index zone_idx
975 * has changed after 'z' has been assigned and before f->zbd_info->mutex
976 * has been obtained. Hence the loop.
979 z = &f->zbd_info->zone_info[zone_idx];
981 pthread_mutex_lock(&z->mutex);
982 pthread_mutex_lock(&f->zbd_info->mutex);
983 if (td->o.max_open_zones == 0)
985 if (f->zbd_info->num_open_zones == 0) {
986 pthread_mutex_unlock(&f->zbd_info->mutex);
987 pthread_mutex_unlock(&z->mutex);
988 dprint(FD_ZBD, "%s(%s): no zones are open\n",
989 __func__, f->file_name);
992 open_zone_idx = (io_u->offset - f->file_offset) *
993 f->zbd_info->num_open_zones / f->io_size;
994 assert(open_zone_idx < f->zbd_info->num_open_zones);
995 new_zone_idx = f->zbd_info->open_zones[open_zone_idx];
996 if (new_zone_idx == zone_idx)
998 zone_idx = new_zone_idx;
999 pthread_mutex_unlock(&f->zbd_info->mutex);
1000 pthread_mutex_unlock(&z->mutex);
1003 /* Both z->mutex and f->zbd_info->mutex are held. */
1006 if (z->wp + min_bs <= (z+1)->start) {
1007 pthread_mutex_unlock(&f->zbd_info->mutex);
1010 dprint(FD_ZBD, "%s(%s): closing zone %d\n", __func__, f->file_name,
1012 if (td->o.max_open_zones)
1013 zbd_close_zone(td, f, open_zone_idx);
1014 pthread_mutex_unlock(&f->zbd_info->mutex);
1016 /* Only z->mutex is held. */
1018 /* Zone 'z' is full, so try to open a new zone. */
1019 for (i = f->io_size / f->zbd_info->zone_size; i > 0; i--) {
1021 pthread_mutex_unlock(&z->mutex);
1023 if (!is_valid_offset(f, z->start)) {
1025 zone_idx = zbd_zone_idx(f, f->file_offset);
1026 z = &f->zbd_info->zone_info[zone_idx];
1028 assert(is_valid_offset(f, z->start));
1029 pthread_mutex_lock(&z->mutex);
1032 if (zbd_open_zone(td, io_u, zone_idx))
1036 /* Only z->mutex is held. */
1038 /* Check whether the write fits in any of the already opened zones. */
1039 pthread_mutex_lock(&f->zbd_info->mutex);
1040 for (i = 0; i < f->zbd_info->num_open_zones; i++) {
1041 zone_idx = f->zbd_info->open_zones[i];
1042 pthread_mutex_unlock(&f->zbd_info->mutex);
1043 pthread_mutex_unlock(&z->mutex);
1045 z = &f->zbd_info->zone_info[zone_idx];
1047 pthread_mutex_lock(&z->mutex);
1048 if (z->wp + min_bs <= (z+1)->start)
1050 pthread_mutex_lock(&f->zbd_info->mutex);
1052 pthread_mutex_unlock(&f->zbd_info->mutex);
1053 pthread_mutex_unlock(&z->mutex);
1054 dprint(FD_ZBD, "%s(%s): did not open another zone\n", __func__,
1059 dprint(FD_ZBD, "%s(%s): returning zone %d\n", __func__, f->file_name,
1061 io_u->offset = z->start;
1065 /* The caller must hold z->mutex. */
1066 static struct fio_zone_info *zbd_replay_write_order(struct thread_data *td,
1068 struct fio_zone_info *z)
1070 const struct fio_file *f = io_u->file;
1071 const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
1073 if (!zbd_open_zone(td, io_u, z - f->zbd_info->zone_info)) {
1074 pthread_mutex_unlock(&z->mutex);
1075 z = zbd_convert_to_open_zone(td, io_u);
1079 if (z->verify_block * min_bs >= f->zbd_info->zone_size)
1080 log_err("%s: %d * %d >= %llu\n", f->file_name, z->verify_block,
1081 min_bs, (unsigned long long) f->zbd_info->zone_size);
1082 io_u->offset = z->start + z->verify_block++ * min_bs;
1087 * Find another zone for which @io_u fits below the write pointer. Start
1088 * searching in zones @zb + 1 .. @zl and continue searching in zones
1091 * Either returns NULL or returns a zone pointer and holds the mutex for that
1094 static struct fio_zone_info *
1095 zbd_find_zone(struct thread_data *td, struct io_u *io_u,
1096 struct fio_zone_info *zb, struct fio_zone_info *zl)
1098 const uint32_t min_bs = td->o.min_bs[io_u->ddir];
1099 const struct fio_file *f = io_u->file;
1100 struct fio_zone_info *z1, *z2;
1101 const struct fio_zone_info *const zf =
1102 &f->zbd_info->zone_info[zbd_zone_idx(f, f->file_offset)];
1105 * Skip to the next non-empty zone in case of sequential I/O and to
1106 * the nearest non-empty zone in case of random I/O.
1108 for (z1 = zb + 1, z2 = zb - 1; z1 < zl || z2 >= zf; z1++, z2--) {
1109 if (z1 < zl && z1->cond != BLK_ZONE_COND_OFFLINE) {
1110 pthread_mutex_lock(&z1->mutex);
1111 if (z1->start + min_bs <= z1->wp)
1113 pthread_mutex_unlock(&z1->mutex);
1114 } else if (!td_random(td)) {
1117 if (td_random(td) && z2 >= zf &&
1118 z2->cond != BLK_ZONE_COND_OFFLINE) {
1119 pthread_mutex_lock(&z2->mutex);
1120 if (z2->start + min_bs <= z2->wp)
1122 pthread_mutex_unlock(&z2->mutex);
1125 dprint(FD_ZBD, "%s: adjusting random read offset failed\n",
1131 * zbd_queue_io - update the write pointer of a sequential zone
1133 * @success: Whether or not the I/O unit has been queued successfully
1134 * @q: queueing status (busy, completed or queued).
1136 * For write and trim operations, update the write pointer of the I/O unit
1139 static void zbd_queue_io(struct io_u *io_u, int q, bool success)
1141 const struct fio_file *f = io_u->file;
1142 struct zoned_block_device_info *zbd_info = f->zbd_info;
1143 struct fio_zone_info *z;
1150 zone_idx = zbd_zone_idx(f, io_u->offset);
1151 assert(zone_idx < zbd_info->nr_zones);
1152 z = &zbd_info->zone_info[zone_idx];
1154 if (z->type != BLK_ZONE_TYPE_SEQWRITE_REQ)
1161 "%s: queued I/O (%lld, %llu) for zone %u\n",
1162 f->file_name, io_u->offset, io_u->buflen, zone_idx);
1164 switch (io_u->ddir) {
1166 zone_end = min((uint64_t)(io_u->offset + io_u->buflen),
1168 pthread_mutex_lock(&zbd_info->mutex);
1170 * z->wp > zone_end means that one or more I/O errors
1173 if (z->wp <= zone_end)
1174 zbd_info->sectors_with_data += zone_end - z->wp;
1175 pthread_mutex_unlock(&zbd_info->mutex);
1179 assert(z->wp == z->start);
1186 if (!success || q != FIO_Q_QUEUED) {
1187 /* BUSY or COMPLETED: unlock the zone */
1188 pthread_mutex_unlock(&z->mutex);
1189 io_u->zbd_put_io = NULL;
1194 * zbd_put_io - Unlock an I/O unit target zone lock
1197 static void zbd_put_io(const struct io_u *io_u)
1199 const struct fio_file *f = io_u->file;
1200 struct zoned_block_device_info *zbd_info = f->zbd_info;
1201 struct fio_zone_info *z;
1207 zone_idx = zbd_zone_idx(f, io_u->offset);
1208 assert(zone_idx < zbd_info->nr_zones);
1209 z = &zbd_info->zone_info[zone_idx];
1211 if (z->type != BLK_ZONE_TYPE_SEQWRITE_REQ)
1215 "%s: terminate I/O (%lld, %llu) for zone %u\n",
1216 f->file_name, io_u->offset, io_u->buflen, zone_idx);
1218 assert(pthread_mutex_unlock(&z->mutex) == 0);
1222 bool zbd_unaligned_write(int error_code)
1224 switch (error_code) {
1233 * setup_zbd_zone_mode - handle zoneskip as necessary for ZBD drives
1234 * @td: FIO thread data.
1235 * @io_u: FIO I/O unit.
1237 * For sequential workloads, change the file offset to skip zoneskip bytes when
1238 * no more IO can be performed in the current zone.
1239 * - For read workloads, zoneskip is applied when the io has reached the end of
1240 * the zone or the zone write position (when td->o.read_beyond_wp is false).
1241 * - For write workloads, zoneskip is applied when the zone is full.
1242 * This applies only to read and write operations.
1244 void setup_zbd_zone_mode(struct thread_data *td, struct io_u *io_u)
1246 struct fio_file *f = io_u->file;
1247 enum fio_ddir ddir = io_u->ddir;
1248 struct fio_zone_info *z;
1251 assert(td->o.zone_mode == ZONE_MODE_ZBD);
1252 assert(td->o.zone_size);
1255 * zone_skip is valid only for sequential workloads.
1257 if (td_random(td) || !td->o.zone_skip)
1261 * It is time to switch to a new zone if:
1262 * - zone_bytes == zone_size bytes have already been accessed
1263 * - The last position reached the end of the current zone.
1264 * - For reads with td->o.read_beyond_wp == false, the last position
1265 * reached the zone write pointer.
1267 zone_idx = zbd_zone_idx(f, f->last_pos[ddir]);
1268 z = &f->zbd_info->zone_info[zone_idx];
1270 if (td->zone_bytes >= td->o.zone_size ||
1271 f->last_pos[ddir] >= (z+1)->start ||
1272 (ddir == DDIR_READ &&
1273 (!td->o.read_beyond_wp) && f->last_pos[ddir] >= z->wp)) {
1278 f->file_offset += td->o.zone_size + td->o.zone_skip;
1281 * Wrap from the beginning, if we exceed the file size
1283 if (f->file_offset >= f->real_file_size)
1284 f->file_offset = get_start_offset(td, f);
1286 f->last_pos[ddir] = f->file_offset;
1287 td->io_skip_bytes += td->o.zone_skip;
1292 * zbd_adjust_block - adjust the offset and length as necessary for ZBD drives
1293 * @td: FIO thread data.
1294 * @io_u: FIO I/O unit.
1296 * Locking strategy: returns with z->mutex locked if and only if z refers
1297 * to a sequential zone and if io_u_accept is returned. z is the zone that
1298 * corresponds to io_u->offset at the end of this function.
1300 enum io_u_action zbd_adjust_block(struct thread_data *td, struct io_u *io_u)
1302 const struct fio_file *f = io_u->file;
1303 uint32_t zone_idx_b;
1304 struct fio_zone_info *zb, *zl, *orig_zb;
1305 uint32_t orig_len = io_u->buflen;
1306 uint32_t min_bs = td->o.min_bs[io_u->ddir];
1313 assert(is_valid_offset(f, io_u->offset));
1314 assert(io_u->buflen);
1315 zone_idx_b = zbd_zone_idx(f, io_u->offset);
1316 zb = &f->zbd_info->zone_info[zone_idx_b];
1319 /* Accept the I/O offset for conventional zones. */
1320 if (zb->type == BLK_ZONE_TYPE_CONVENTIONAL)
1324 * Accept the I/O offset for reads if reading beyond the write pointer
1327 if (zb->cond != BLK_ZONE_COND_OFFLINE &&
1328 io_u->ddir == DDIR_READ && td->o.read_beyond_wp)
1334 * Lock the io_u target zone. The zone will be unlocked if io_u offset
1335 * is changed or when io_u completes and zbd_put_io() executed.
1336 * To avoid multiple jobs doing asynchronous I/Os from deadlocking each
1337 * other waiting for zone locks when building an io_u batch, first
1338 * only trylock the zone. If the zone is already locked by another job,
1339 * process the currently queued I/Os so that I/O progress is made and
1342 if (pthread_mutex_trylock(&zb->mutex) != 0) {
1343 if (!td_ioengine_flagged(td, FIO_SYNCIO))
1345 pthread_mutex_lock(&zb->mutex);
1348 switch (io_u->ddir) {
1350 if (td->runstate == TD_VERIFYING) {
1351 zb = zbd_replay_write_order(td, io_u, zb);
1355 * Check that there is enough written data in the zone to do an
1356 * I/O of at least min_bs B. If there isn't, find a new zone for
1359 range = zb->cond != BLK_ZONE_COND_OFFLINE ?
1360 zb->wp - zb->start : 0;
1361 if (range < min_bs ||
1362 ((!td_random(td)) && (io_u->offset + min_bs > zb->wp))) {
1363 pthread_mutex_unlock(&zb->mutex);
1364 zl = &f->zbd_info->zone_info[zbd_zone_idx(f,
1365 f->file_offset + f->io_size)];
1366 zb = zbd_find_zone(td, io_u, zb, zl);
1369 "%s: zbd_find_zone(%lld, %llu) failed\n",
1370 f->file_name, io_u->offset,
1375 * zbd_find_zone() returned a zone with a range of at
1378 range = zb->wp - zb->start;
1379 assert(range >= min_bs);
1382 io_u->offset = zb->start;
1385 * Make sure the I/O is within the zone valid data range while
1386 * maximizing the I/O size and preserving randomness.
1388 if (range <= io_u->buflen)
1389 io_u->offset = zb->start;
1390 else if (td_random(td))
1391 io_u->offset = zb->start +
1392 ((io_u->offset - orig_zb->start) %
1393 (range - io_u->buflen)) / min_bs * min_bs;
1395 * Make sure the I/O does not cross over the zone wp position.
1397 new_len = min((unsigned long long)io_u->buflen,
1398 (unsigned long long)(zb->wp - io_u->offset));
1399 new_len = new_len / min_bs * min_bs;
1400 if (new_len < io_u->buflen) {
1401 io_u->buflen = new_len;
1402 dprint(FD_IO, "Changed length from %u into %llu\n",
1403 orig_len, io_u->buflen);
1405 assert(zb->start <= io_u->offset);
1406 assert(io_u->offset + io_u->buflen <= zb->wp);
1409 if (io_u->buflen > f->zbd_info->zone_size)
1411 if (!zbd_open_zone(td, io_u, zone_idx_b)) {
1412 pthread_mutex_unlock(&zb->mutex);
1413 zb = zbd_convert_to_open_zone(td, io_u);
1416 zone_idx_b = zb - f->zbd_info->zone_info;
1418 /* Check whether the zone reset threshold has been exceeded */
1419 if (td->o.zrf.u.f) {
1420 if (f->zbd_info->sectors_with_data >=
1421 f->io_size * td->o.zrt.u.f &&
1422 zbd_dec_and_reset_write_cnt(td, f)) {
1426 /* Reset the zone pointer if necessary */
1427 if (zb->reset_zone || zbd_zone_full(f, zb, min_bs)) {
1428 assert(td->o.verify == VERIFY_NONE);
1430 * Since previous write requests may have been submitted
1431 * asynchronously and since we will submit the zone
1432 * reset synchronously, wait until previously submitted
1433 * write requests have completed before issuing a
1438 if (zbd_reset_zone(td, f, zb) < 0)
1441 /* Make writes occur at the write pointer */
1442 assert(!zbd_zone_full(f, zb, min_bs));
1443 io_u->offset = zb->wp;
1444 if (!is_valid_offset(f, io_u->offset)) {
1445 dprint(FD_ZBD, "Dropped request with offset %llu\n",
1450 * Make sure that the buflen is a multiple of the minimal
1451 * block size. Give up if shrinking would make the request too
1454 new_len = min((unsigned long long)io_u->buflen,
1455 (zb + 1)->start - io_u->offset);
1456 new_len = new_len / min_bs * min_bs;
1457 if (new_len == io_u->buflen)
1459 if (new_len >= min_bs) {
1460 io_u->buflen = new_len;
1461 dprint(FD_IO, "Changed length from %u into %llu\n",
1462 orig_len, io_u->buflen);
1465 log_err("Zone remainder %lld smaller than minimum block size %d\n",
1466 ((zb + 1)->start - io_u->offset),
1473 case DDIR_SYNC_FILE_RANGE:
1484 assert(zb->cond != BLK_ZONE_COND_OFFLINE);
1485 assert(!io_u->zbd_queue_io);
1486 assert(!io_u->zbd_put_io);
1487 io_u->zbd_queue_io = zbd_queue_io;
1488 io_u->zbd_put_io = zbd_put_io;
1493 pthread_mutex_unlock(&zb->mutex);
1497 /* Return a string with ZBD statistics */
1498 char *zbd_write_status(const struct thread_stat *ts)
1502 if (asprintf(&res, "; %llu zone resets", (unsigned long long) ts->nr_zone_resets) < 0)