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_size) {
124 td->o.zone_size = f->zbd_info->zone_size;
125 if (!td->o.zone_size) {
126 log_err("%s: invalid 0 zone size\n",
130 } else if (td->o.zone_size != f->zbd_info->zone_size) {
131 log_err("%s: job parameter zonesize %llu does not match disk zone size %llu.\n",
132 f->file_name, (unsigned long long) td->o.zone_size,
133 (unsigned long long) f->zbd_info->zone_size);
137 if (td->o.zone_skip &&
138 (td->o.zone_skip < td->o.zone_size ||
139 td->o.zone_skip % td->o.zone_size)) {
140 log_err("%s: zoneskip %llu is not a multiple of the device zone size %llu.\n",
141 f->file_name, (unsigned long long) td->o.zone_skip,
142 (unsigned long long) td->o.zone_size);
146 zone_idx = zbd_zone_idx(f, f->file_offset);
147 z = &f->zbd_info->zone_info[zone_idx];
148 if (f->file_offset != z->start) {
149 new_offset = (z+1)->start;
150 if (new_offset >= f->file_offset + f->io_size) {
151 log_info("%s: io_size must be at least one zone\n",
155 log_info("%s: rounded up offset from %llu to %llu\n",
156 f->file_name, (unsigned long long) f->file_offset,
157 (unsigned long long) new_offset);
158 f->io_size -= (new_offset - f->file_offset);
159 f->file_offset = new_offset;
161 zone_idx = zbd_zone_idx(f, f->file_offset + f->io_size);
162 z = &f->zbd_info->zone_info[zone_idx];
164 if (f->file_offset + f->io_size != new_end) {
165 if (new_end <= f->file_offset) {
166 log_info("%s: io_size must be at least one zone\n",
170 log_info("%s: rounded down io_size from %llu to %llu\n",
171 f->file_name, (unsigned long long) f->io_size,
172 (unsigned long long) new_end - f->file_offset);
173 f->io_size = new_end - f->file_offset;
181 static bool zbd_verify_bs(void)
183 struct thread_data *td;
189 for_each_file(td, f, j) {
192 zone_size = f->zbd_info->zone_size;
193 for (k = 0; k < ARRAY_SIZE(td->o.bs); k++) {
194 if (td->o.verify != VERIFY_NONE &&
195 zone_size % td->o.bs[k] != 0) {
196 log_info("%s: block size %llu is not a divisor of the zone size %d\n",
197 f->file_name, td->o.bs[k],
208 * Read zone information into @buf starting from sector @start_sector.
209 * @fd is a file descriptor that refers to a block device and @bufsz is the
212 * Returns 0 upon success and a negative error code upon failure.
213 * If the zone report is empty, always assume an error (device problem) and
216 static int read_zone_info(int fd, uint64_t start_sector,
217 void *buf, unsigned int bufsz)
219 struct blk_zone_report *hdr = buf;
222 if (bufsz < sizeof(*hdr))
225 memset(hdr, 0, sizeof(*hdr));
227 hdr->nr_zones = (bufsz - sizeof(*hdr)) / sizeof(struct blk_zone);
228 hdr->sector = start_sector;
229 ret = ioctl(fd, BLKREPORTZONE, hdr);
238 * Read up to 255 characters from the first line of a file. Strip the trailing
241 static char *read_file(const char *path)
243 char line[256], *p = line;
246 f = fopen(path, "rb");
249 if (!fgets(line, sizeof(line), f))
257 static enum blk_zoned_model get_zbd_model(const char *file_name)
259 enum blk_zoned_model model = ZBD_DM_NONE;
260 char *zoned_attr_path = NULL;
261 char *model_str = NULL;
263 char *sys_devno_path = NULL;
264 char *part_attr_path = NULL;
265 char *part_str = NULL;
266 char sys_path[PATH_MAX];
270 if (stat(file_name, &statbuf) < 0)
273 if (asprintf(&sys_devno_path, "/sys/dev/block/%d:%d",
274 major(statbuf.st_rdev), minor(statbuf.st_rdev)) < 0)
277 sz = readlink(sys_devno_path, sys_path, sizeof(sys_path) - 1);
283 * If the device is a partition device, cut the device name in the
284 * canonical sysfs path to obtain the sysfs path of the holder device.
285 * e.g.: /sys/devices/.../sda/sda1 -> /sys/devices/.../sda
287 if (asprintf(&part_attr_path, "/sys/dev/block/%s/partition",
290 part_str = read_file(part_attr_path);
291 if (part_str && *part_str == '1') {
292 delim = strrchr(sys_path, '/');
298 if (asprintf(&zoned_attr_path,
299 "/sys/dev/block/%s/queue/zoned", sys_path) < 0)
302 model_str = read_file(zoned_attr_path);
305 dprint(FD_ZBD, "%s: zbd model string: %s\n", file_name, model_str);
306 if (strcmp(model_str, "host-aware") == 0)
307 model = ZBD_DM_HOST_AWARE;
308 else if (strcmp(model_str, "host-managed") == 0)
309 model = ZBD_DM_HOST_MANAGED;
313 free(zoned_attr_path);
315 free(part_attr_path);
316 free(sys_devno_path);
320 static int ilog2(uint64_t i)
332 * Initialize f->zbd_info for devices that are not zoned block devices. This
333 * allows to execute a ZBD workload against a non-ZBD device.
335 static int init_zone_info(struct thread_data *td, struct fio_file *f)
338 struct fio_zone_info *p;
339 uint64_t zone_size = td->o.zone_size;
340 struct zoned_block_device_info *zbd_info = NULL;
341 pthread_mutexattr_t attr;
344 if (zone_size == 0) {
345 log_err("%s: Specifying the zone size is mandatory for regular block devices with --zonemode=zbd\n\n",
350 if (zone_size < 512) {
351 log_err("%s: zone size must be at least 512 bytes for --zonemode=zbd\n\n",
356 nr_zones = (f->real_file_size + zone_size - 1) / zone_size;
357 zbd_info = scalloc(1, sizeof(*zbd_info) +
358 (nr_zones + 1) * sizeof(zbd_info->zone_info[0]));
362 pthread_mutexattr_init(&attr);
363 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
364 pthread_mutexattr_setpshared(&attr, true);
365 pthread_mutex_init(&zbd_info->mutex, &attr);
366 zbd_info->refcount = 1;
367 p = &zbd_info->zone_info[0];
368 for (i = 0; i < nr_zones; i++, p++) {
369 pthread_mutex_init(&p->mutex, &attr);
370 p->start = i * zone_size;
371 p->wp = p->start + zone_size;
372 p->type = BLK_ZONE_TYPE_SEQWRITE_REQ;
373 p->cond = BLK_ZONE_COND_EMPTY;
376 p->start = nr_zones * zone_size;
378 f->zbd_info = zbd_info;
379 f->zbd_info->zone_size = zone_size;
380 f->zbd_info->zone_size_log2 = is_power_of_2(zone_size) ?
381 ilog2(zone_size) : -1;
382 f->zbd_info->nr_zones = nr_zones;
383 pthread_mutexattr_destroy(&attr);
388 * Parse the BLKREPORTZONE output and store it in f->zbd_info. Must be called
389 * only for devices that support this ioctl, namely zoned block devices.
391 static int parse_zone_info(struct thread_data *td, struct fio_file *f)
393 const unsigned int bufsz = sizeof(struct blk_zone_report) +
394 4096 * sizeof(struct blk_zone);
396 struct blk_zone_report *hdr;
397 const struct blk_zone *z;
398 struct fio_zone_info *p;
399 uint64_t zone_size, start_sector;
400 struct zoned_block_device_info *zbd_info = NULL;
401 pthread_mutexattr_t attr;
403 int fd, i, j, ret = 0;
405 pthread_mutexattr_init(&attr);
406 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
407 pthread_mutexattr_setpshared(&attr, true);
413 fd = open(f->file_name, O_RDONLY | O_LARGEFILE);
419 ret = read_zone_info(fd, 0, buf, bufsz);
421 log_info("fio: BLKREPORTZONE(%lu) failed for %s (%d).\n",
422 0UL, f->file_name, -ret);
426 if (hdr->nr_zones < 1) {
427 log_info("fio: %s has invalid zone information.\n",
431 z = (void *)(hdr + 1);
432 zone_size = z->len << 9;
433 nr_zones = (f->real_file_size + zone_size - 1) / zone_size;
435 if (td->o.zone_size == 0) {
436 td->o.zone_size = zone_size;
437 } else if (td->o.zone_size != zone_size) {
438 log_err("fio: %s job parameter zonesize %llu does not match disk zone size %llu.\n",
439 f->file_name, (unsigned long long) td->o.zone_size,
440 (unsigned long long) zone_size);
445 dprint(FD_ZBD, "Device %s has %d zones of size %llu KB\n", f->file_name,
446 nr_zones, (unsigned long long) zone_size / 1024);
448 zbd_info = scalloc(1, sizeof(*zbd_info) +
449 (nr_zones + 1) * sizeof(zbd_info->zone_info[0]));
453 pthread_mutex_init(&zbd_info->mutex, &attr);
454 zbd_info->refcount = 1;
455 p = &zbd_info->zone_info[0];
456 for (start_sector = 0, j = 0; j < nr_zones;) {
457 z = (void *)(hdr + 1);
458 for (i = 0; i < hdr->nr_zones; i++, j++, z++, p++) {
459 pthread_mutex_init(&p->mutex, &attr);
460 p->start = z->start << 9;
462 case BLK_ZONE_COND_NOT_WP:
463 case BLK_ZONE_COND_FULL:
464 p->wp = p->start + zone_size;
467 assert(z->start <= z->wp);
468 assert(z->wp <= z->start + (zone_size >> 9));
474 if (j > 0 && p->start != p[-1].start + zone_size) {
475 log_info("%s: invalid zone data\n",
482 start_sector = z->start + z->len;
485 ret = read_zone_info(fd, start_sector, buf, bufsz);
487 log_info("fio: BLKREPORTZONE(%llu) failed for %s (%d).\n",
488 (unsigned long long) start_sector, f->file_name, -ret);
493 zbd_info->zone_info[nr_zones].start = start_sector << 9;
495 f->zbd_info = zbd_info;
496 f->zbd_info->zone_size = zone_size;
497 f->zbd_info->zone_size_log2 = is_power_of_2(zone_size) ?
498 ilog2(zone_size) : -1;
499 f->zbd_info->nr_zones = nr_zones;
509 pthread_mutexattr_destroy(&attr);
514 * Allocate zone information and store it into f->zbd_info if zonemode=zbd.
516 * Returns 0 upon success and a negative error code upon failure.
518 static int zbd_create_zone_info(struct thread_data *td, struct fio_file *f)
520 enum blk_zoned_model zbd_model;
523 assert(td->o.zone_mode == ZONE_MODE_ZBD);
525 zbd_model = get_zbd_model(f->file_name);
527 case ZBD_DM_HOST_AWARE:
528 case ZBD_DM_HOST_MANAGED:
529 ret = parse_zone_info(td, f);
532 ret = init_zone_info(td, f);
536 f->zbd_info->model = zbd_model;
540 void zbd_free_zone_info(struct fio_file *f)
547 pthread_mutex_lock(&f->zbd_info->mutex);
548 refcount = --f->zbd_info->refcount;
549 pthread_mutex_unlock(&f->zbd_info->mutex);
551 assert((int32_t)refcount >= 0);
558 * Initialize f->zbd_info.
560 * Returns 0 upon success and a negative error code upon failure.
562 * Note: this function can only work correctly if it is called before the first
565 static int zbd_init_zone_info(struct thread_data *td, struct fio_file *file)
567 struct thread_data *td2;
571 for_each_td(td2, i) {
572 for_each_file(td2, f2, j) {
573 if (td2 == td && f2 == file)
576 strcmp(f2->file_name, file->file_name) != 0)
578 file->zbd_info = f2->zbd_info;
579 file->zbd_info->refcount++;
584 ret = zbd_create_zone_info(td, file);
586 td_verror(td, -ret, "zbd_create_zone_info() failed");
590 int zbd_init(struct thread_data *td)
595 for_each_file(td, f, i) {
596 if (f->filetype != FIO_TYPE_BLOCK)
598 if (zbd_init_zone_info(td, f))
602 if (!zbd_using_direct_io()) {
603 log_err("Using direct I/O is mandatory for writing to ZBD drives\n\n");
607 if (!zbd_verify_sizes())
610 if (!zbd_verify_bs())
617 * zbd_reset_range - reset zones for a range of sectors
618 * @td: FIO thread data.
619 * @f: Fio file for which to reset zones
620 * @sector: Starting sector in units of 512 bytes
621 * @nr_sectors: Number of sectors in units of 512 bytes
623 * Returns 0 upon success and a negative error code upon failure.
625 static int zbd_reset_range(struct thread_data *td, const struct fio_file *f,
626 uint64_t offset, uint64_t length)
628 struct blk_zone_range zr = {
629 .sector = offset >> 9,
630 .nr_sectors = length >> 9,
632 uint32_t zone_idx_b, zone_idx_e;
633 struct fio_zone_info *zb, *ze, *z;
637 assert(is_valid_offset(f, offset + length - 1));
638 switch (f->zbd_info->model) {
639 case ZBD_DM_HOST_AWARE:
640 case ZBD_DM_HOST_MANAGED:
641 ret = ioctl(f->fd, BLKRESETZONE, &zr);
643 td_verror(td, errno, "resetting wp failed");
644 log_err("%s: resetting wp for %llu sectors at sector %llu failed (%d).\n",
645 f->file_name, zr.nr_sectors, zr.sector, errno);
653 zone_idx_b = zbd_zone_idx(f, offset);
654 zb = &f->zbd_info->zone_info[zone_idx_b];
655 zone_idx_e = zbd_zone_idx(f, offset + length);
656 ze = &f->zbd_info->zone_info[zone_idx_e];
657 for (z = zb; z < ze; z++) {
658 pthread_mutex_lock(&z->mutex);
659 pthread_mutex_lock(&f->zbd_info->mutex);
660 f->zbd_info->sectors_with_data -= z->wp - z->start;
661 pthread_mutex_unlock(&f->zbd_info->mutex);
664 pthread_mutex_unlock(&z->mutex);
667 td->ts.nr_zone_resets += ze - zb;
672 static unsigned int zbd_zone_nr(struct zoned_block_device_info *zbd_info,
673 struct fio_zone_info *zone)
675 return zone - zbd_info->zone_info;
679 * zbd_reset_zone - reset the write pointer of a single zone
680 * @td: FIO thread data.
681 * @f: FIO file associated with the disk for which to reset a write pointer.
684 * Returns 0 upon success and a negative error code upon failure.
686 static int zbd_reset_zone(struct thread_data *td, const struct fio_file *f,
687 struct fio_zone_info *z)
689 dprint(FD_ZBD, "%s: resetting wp of zone %u.\n", f->file_name,
690 zbd_zone_nr(f->zbd_info, z));
692 return zbd_reset_range(td, f, z->start, (z+1)->start - z->start);
696 * Reset a range of zones. Returns 0 upon success and 1 upon failure.
697 * @td: fio thread data.
698 * @f: fio file for which to reset zones
699 * @zb: first zone to reset.
700 * @ze: first zone not to reset.
701 * @all_zones: whether to reset all zones or only those zones for which the
702 * write pointer is not a multiple of td->o.min_bs[DDIR_WRITE].
704 static int zbd_reset_zones(struct thread_data *td, struct fio_file *f,
705 struct fio_zone_info *const zb,
706 struct fio_zone_info *const ze, bool all_zones)
708 struct fio_zone_info *z, *start_z = ze;
709 const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
713 dprint(FD_ZBD, "%s: examining zones %u .. %u\n", f->file_name,
714 zbd_zone_nr(f->zbd_info, zb), zbd_zone_nr(f->zbd_info, ze));
716 for (z = zb; z < ze; z++) {
717 pthread_mutex_lock(&z->mutex);
719 case BLK_ZONE_TYPE_SEQWRITE_REQ:
720 reset_wp = all_zones ? z->wp != z->start :
721 (td->o.td_ddir & TD_DDIR_WRITE) &&
723 if (start_z == ze && reset_wp) {
725 } else if (start_z < ze && !reset_wp) {
727 "%s: resetting zones %u .. %u\n",
729 zbd_zone_nr(f->zbd_info, start_z),
730 zbd_zone_nr(f->zbd_info, z));
731 if (zbd_reset_range(td, f, start_z->start,
732 z->start - start_z->start) < 0)
740 dprint(FD_ZBD, "%s: resetting zones %u .. %u\n",
741 f->file_name, zbd_zone_nr(f->zbd_info, start_z),
742 zbd_zone_nr(f->zbd_info, z));
743 if (zbd_reset_range(td, f, start_z->start,
744 z->start - start_z->start) < 0)
751 dprint(FD_ZBD, "%s: resetting zones %u .. %u\n", f->file_name,
752 zbd_zone_nr(f->zbd_info, start_z),
753 zbd_zone_nr(f->zbd_info, z));
754 if (zbd_reset_range(td, f, start_z->start,
755 z->start - start_z->start) < 0)
758 for (z = zb; z < ze; z++)
759 pthread_mutex_unlock(&z->mutex);
765 * Reset zbd_info.write_cnt, the counter that counts down towards the next
768 static void zbd_reset_write_cnt(const struct thread_data *td,
769 const struct fio_file *f)
771 assert(0 <= td->o.zrf.u.f && td->o.zrf.u.f <= 1);
773 pthread_mutex_lock(&f->zbd_info->mutex);
774 f->zbd_info->write_cnt = td->o.zrf.u.f ?
775 min(1.0 / td->o.zrf.u.f, 0.0 + UINT_MAX) : UINT_MAX;
776 pthread_mutex_unlock(&f->zbd_info->mutex);
779 static bool zbd_dec_and_reset_write_cnt(const struct thread_data *td,
780 const struct fio_file *f)
782 uint32_t write_cnt = 0;
784 pthread_mutex_lock(&f->zbd_info->mutex);
785 assert(f->zbd_info->write_cnt);
786 if (f->zbd_info->write_cnt)
787 write_cnt = --f->zbd_info->write_cnt;
789 zbd_reset_write_cnt(td, f);
790 pthread_mutex_unlock(&f->zbd_info->mutex);
792 return write_cnt == 0;
800 /* Calculate the number of sectors with data (swd) and perform action 'a' */
801 static uint64_t zbd_process_swd(const struct fio_file *f, enum swd_action a)
803 struct fio_zone_info *zb, *ze, *z;
806 zb = &f->zbd_info->zone_info[zbd_zone_idx(f, f->file_offset)];
807 ze = &f->zbd_info->zone_info[zbd_zone_idx(f, f->file_offset +
809 for (z = zb; z < ze; z++) {
810 pthread_mutex_lock(&z->mutex);
811 swd += z->wp - z->start;
813 pthread_mutex_lock(&f->zbd_info->mutex);
816 assert(f->zbd_info->sectors_with_data == swd);
819 f->zbd_info->sectors_with_data = swd;
822 pthread_mutex_unlock(&f->zbd_info->mutex);
823 for (z = zb; z < ze; z++)
824 pthread_mutex_unlock(&z->mutex);
830 * The swd check is useful for debugging but takes too much time to leave
831 * it enabled all the time. Hence it is disabled by default.
833 static const bool enable_check_swd = false;
835 /* Check whether the value of zbd_info.sectors_with_data is correct. */
836 static void zbd_check_swd(const struct fio_file *f)
838 if (!enable_check_swd)
841 zbd_process_swd(f, CHECK_SWD);
844 static void zbd_init_swd(struct fio_file *f)
848 swd = zbd_process_swd(f, SET_SWD);
849 dprint(FD_ZBD, "%s(%s): swd = %" PRIu64 "\n", __func__, f->file_name,
853 void zbd_file_reset(struct thread_data *td, struct fio_file *f)
855 struct fio_zone_info *zb, *ze;
861 zb = &f->zbd_info->zone_info[zbd_zone_idx(f, f->file_offset)];
862 zone_idx_e = zbd_zone_idx(f, f->file_offset + f->io_size);
863 ze = &f->zbd_info->zone_info[zone_idx_e];
866 * If data verification is enabled reset the affected zones before
867 * writing any data to avoid that a zone reset has to be issued while
868 * writing data, which causes data loss.
870 zbd_reset_zones(td, f, zb, ze, td->o.verify != VERIFY_NONE &&
871 (td->o.td_ddir & TD_DDIR_WRITE) &&
872 td->runstate != TD_VERIFYING);
873 zbd_reset_write_cnt(td, f);
876 /* The caller must hold f->zbd_info->mutex. */
877 static bool is_zone_open(const struct thread_data *td, const struct fio_file *f,
878 unsigned int zone_idx)
880 struct zoned_block_device_info *zbdi = f->zbd_info;
883 assert(td->o.max_open_zones <= ARRAY_SIZE(zbdi->open_zones));
884 assert(zbdi->num_open_zones <= td->o.max_open_zones);
886 for (i = 0; i < zbdi->num_open_zones; i++)
887 if (zbdi->open_zones[i] == zone_idx)
894 * Open a ZBD zone if it was not yet open. Returns true if either the zone was
895 * already open or if opening a new zone is allowed. Returns false if the zone
896 * was not yet open and opening a new zone would cause the zone limit to be
899 static bool zbd_open_zone(struct thread_data *td, const struct io_u *io_u,
902 const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
903 const struct fio_file *f = io_u->file;
904 struct fio_zone_info *z = &f->zbd_info->zone_info[zone_idx];
907 if (z->cond == BLK_ZONE_COND_OFFLINE)
911 * Skip full zones with data verification enabled because resetting a
912 * zone causes data loss and hence causes verification to fail.
914 if (td->o.verify != VERIFY_NONE && zbd_zone_full(f, z, min_bs))
917 /* Zero means no limit */
918 if (!td->o.max_open_zones)
921 pthread_mutex_lock(&f->zbd_info->mutex);
922 if (is_zone_open(td, f, zone_idx))
925 if (f->zbd_info->num_open_zones >= td->o.max_open_zones)
927 dprint(FD_ZBD, "%s: opening zone %d\n", f->file_name, zone_idx);
928 f->zbd_info->open_zones[f->zbd_info->num_open_zones++] = zone_idx;
933 pthread_mutex_unlock(&f->zbd_info->mutex);
937 /* The caller must hold f->zbd_info->mutex */
938 static void zbd_close_zone(struct thread_data *td, const struct fio_file *f,
939 unsigned int open_zone_idx)
943 assert(open_zone_idx < f->zbd_info->num_open_zones);
944 zone_idx = f->zbd_info->open_zones[open_zone_idx];
945 memmove(f->zbd_info->open_zones + open_zone_idx,
946 f->zbd_info->open_zones + open_zone_idx + 1,
947 (FIO_MAX_OPEN_ZBD_ZONES - (open_zone_idx + 1)) *
948 sizeof(f->zbd_info->open_zones[0]));
949 f->zbd_info->num_open_zones--;
950 f->zbd_info->zone_info[zone_idx].open = 0;
954 * Modify the offset of an I/O unit that does not refer to an open zone such
955 * that it refers to an open zone. Close an open zone and open a new zone if
956 * necessary. This algorithm can only work correctly if all write pointers are
957 * a multiple of the fio block size. The caller must neither hold z->mutex
958 * nor f->zbd_info->mutex. Returns with z->mutex held upon success.
960 static struct fio_zone_info *zbd_convert_to_open_zone(struct thread_data *td,
963 const uint32_t min_bs = td->o.min_bs[io_u->ddir];
964 const struct fio_file *f = io_u->file;
965 struct fio_zone_info *z;
966 unsigned int open_zone_idx = -1;
967 uint32_t zone_idx, new_zone_idx;
970 assert(is_valid_offset(f, io_u->offset));
972 if (td->o.max_open_zones) {
974 * This statement accesses f->zbd_info->open_zones[] on purpose
977 zone_idx = f->zbd_info->open_zones[(io_u->offset -
979 f->zbd_info->num_open_zones / f->io_size];
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.
993 z = &f->zbd_info->zone_info[zone_idx];
995 pthread_mutex_lock(&z->mutex);
996 pthread_mutex_lock(&f->zbd_info->mutex);
997 if (td->o.max_open_zones == 0)
999 if (f->zbd_info->num_open_zones == 0) {
1000 pthread_mutex_unlock(&f->zbd_info->mutex);
1001 pthread_mutex_unlock(&z->mutex);
1002 dprint(FD_ZBD, "%s(%s): no zones are open\n",
1003 __func__, f->file_name);
1006 open_zone_idx = (io_u->offset - f->file_offset) *
1007 f->zbd_info->num_open_zones / f->io_size;
1008 assert(open_zone_idx < f->zbd_info->num_open_zones);
1009 new_zone_idx = f->zbd_info->open_zones[open_zone_idx];
1010 if (new_zone_idx == zone_idx)
1012 zone_idx = new_zone_idx;
1013 pthread_mutex_unlock(&f->zbd_info->mutex);
1014 pthread_mutex_unlock(&z->mutex);
1017 /* Both z->mutex and f->zbd_info->mutex are held. */
1020 if (z->wp + min_bs <= (z+1)->start) {
1021 pthread_mutex_unlock(&f->zbd_info->mutex);
1024 dprint(FD_ZBD, "%s(%s): closing zone %d\n", __func__, f->file_name,
1026 if (td->o.max_open_zones)
1027 zbd_close_zone(td, f, open_zone_idx);
1028 pthread_mutex_unlock(&f->zbd_info->mutex);
1030 /* Only z->mutex is held. */
1032 /* Zone 'z' is full, so try to open a new zone. */
1033 for (i = f->io_size / f->zbd_info->zone_size; i > 0; i--) {
1035 pthread_mutex_unlock(&z->mutex);
1037 if (!is_valid_offset(f, z->start)) {
1039 zone_idx = zbd_zone_idx(f, f->file_offset);
1040 z = &f->zbd_info->zone_info[zone_idx];
1042 assert(is_valid_offset(f, z->start));
1043 pthread_mutex_lock(&z->mutex);
1046 if (zbd_open_zone(td, io_u, zone_idx))
1050 /* Only z->mutex is held. */
1052 /* Check whether the write fits in any of the already opened zones. */
1053 pthread_mutex_lock(&f->zbd_info->mutex);
1054 for (i = 0; i < f->zbd_info->num_open_zones; i++) {
1055 zone_idx = f->zbd_info->open_zones[i];
1056 pthread_mutex_unlock(&f->zbd_info->mutex);
1057 pthread_mutex_unlock(&z->mutex);
1059 z = &f->zbd_info->zone_info[zone_idx];
1061 pthread_mutex_lock(&z->mutex);
1062 if (z->wp + min_bs <= (z+1)->start)
1064 pthread_mutex_lock(&f->zbd_info->mutex);
1066 pthread_mutex_unlock(&f->zbd_info->mutex);
1067 pthread_mutex_unlock(&z->mutex);
1068 dprint(FD_ZBD, "%s(%s): did not open another zone\n", __func__,
1073 dprint(FD_ZBD, "%s(%s): returning zone %d\n", __func__, f->file_name,
1075 io_u->offset = z->start;
1079 /* The caller must hold z->mutex. */
1080 static struct fio_zone_info *zbd_replay_write_order(struct thread_data *td,
1082 struct fio_zone_info *z)
1084 const struct fio_file *f = io_u->file;
1085 const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
1087 if (!zbd_open_zone(td, io_u, z - f->zbd_info->zone_info)) {
1088 pthread_mutex_unlock(&z->mutex);
1089 z = zbd_convert_to_open_zone(td, io_u);
1093 if (z->verify_block * min_bs >= f->zbd_info->zone_size)
1094 log_err("%s: %d * %d >= %llu\n", f->file_name, z->verify_block,
1095 min_bs, (unsigned long long) f->zbd_info->zone_size);
1096 io_u->offset = z->start + z->verify_block++ * min_bs;
1101 * Find another zone for which @io_u fits below the write pointer. Start
1102 * searching in zones @zb + 1 .. @zl and continue searching in zones
1105 * Either returns NULL or returns a zone pointer and holds the mutex for that
1108 static struct fio_zone_info *
1109 zbd_find_zone(struct thread_data *td, struct io_u *io_u,
1110 struct fio_zone_info *zb, struct fio_zone_info *zl)
1112 const uint32_t min_bs = td->o.min_bs[io_u->ddir];
1113 const struct fio_file *f = io_u->file;
1114 struct fio_zone_info *z1, *z2;
1115 const struct fio_zone_info *const zf =
1116 &f->zbd_info->zone_info[zbd_zone_idx(f, f->file_offset)];
1119 * Skip to the next non-empty zone in case of sequential I/O and to
1120 * the nearest non-empty zone in case of random I/O.
1122 for (z1 = zb + 1, z2 = zb - 1; z1 < zl || z2 >= zf; z1++, z2--) {
1123 if (z1 < zl && z1->cond != BLK_ZONE_COND_OFFLINE) {
1124 pthread_mutex_lock(&z1->mutex);
1125 if (z1->start + min_bs <= z1->wp)
1127 pthread_mutex_unlock(&z1->mutex);
1128 } else if (!td_random(td)) {
1131 if (td_random(td) && z2 >= zf &&
1132 z2->cond != BLK_ZONE_COND_OFFLINE) {
1133 pthread_mutex_lock(&z2->mutex);
1134 if (z2->start + min_bs <= z2->wp)
1136 pthread_mutex_unlock(&z2->mutex);
1139 dprint(FD_ZBD, "%s: adjusting random read offset failed\n",
1145 * zbd_queue_io - update the write pointer of a sequential zone
1147 * @success: Whether or not the I/O unit has been queued successfully
1148 * @q: queueing status (busy, completed or queued).
1150 * For write and trim operations, update the write pointer of the I/O unit
1153 static void zbd_queue_io(struct io_u *io_u, int q, bool success)
1155 const struct fio_file *f = io_u->file;
1156 struct zoned_block_device_info *zbd_info = f->zbd_info;
1157 struct fio_zone_info *z;
1164 zone_idx = zbd_zone_idx(f, io_u->offset);
1165 assert(zone_idx < zbd_info->nr_zones);
1166 z = &zbd_info->zone_info[zone_idx];
1168 if (z->type != BLK_ZONE_TYPE_SEQWRITE_REQ)
1175 "%s: queued I/O (%lld, %llu) for zone %u\n",
1176 f->file_name, io_u->offset, io_u->buflen, zone_idx);
1178 switch (io_u->ddir) {
1180 zone_end = min((uint64_t)(io_u->offset + io_u->buflen),
1182 pthread_mutex_lock(&zbd_info->mutex);
1184 * z->wp > zone_end means that one or more I/O errors
1187 if (z->wp <= zone_end)
1188 zbd_info->sectors_with_data += zone_end - z->wp;
1189 pthread_mutex_unlock(&zbd_info->mutex);
1193 assert(z->wp == z->start);
1200 if (!success || q != FIO_Q_QUEUED) {
1201 /* BUSY or COMPLETED: unlock the zone */
1202 pthread_mutex_unlock(&z->mutex);
1203 io_u->zbd_put_io = NULL;
1208 * zbd_put_io - Unlock an I/O unit target zone lock
1211 static void zbd_put_io(const struct io_u *io_u)
1213 const struct fio_file *f = io_u->file;
1214 struct zoned_block_device_info *zbd_info = f->zbd_info;
1215 struct fio_zone_info *z;
1221 zone_idx = zbd_zone_idx(f, io_u->offset);
1222 assert(zone_idx < zbd_info->nr_zones);
1223 z = &zbd_info->zone_info[zone_idx];
1225 if (z->type != BLK_ZONE_TYPE_SEQWRITE_REQ)
1229 "%s: terminate I/O (%lld, %llu) for zone %u\n",
1230 f->file_name, io_u->offset, io_u->buflen, zone_idx);
1232 assert(pthread_mutex_unlock(&z->mutex) == 0);
1236 bool zbd_unaligned_write(int error_code)
1238 switch (error_code) {
1247 * setup_zbd_zone_mode - handle zoneskip as necessary for ZBD drives
1248 * @td: FIO thread data.
1249 * @io_u: FIO I/O unit.
1251 * For sequential workloads, change the file offset to skip zoneskip bytes when
1252 * no more IO can be performed in the current zone.
1253 * - For read workloads, zoneskip is applied when the io has reached the end of
1254 * the zone or the zone write position (when td->o.read_beyond_wp is false).
1255 * - For write workloads, zoneskip is applied when the zone is full.
1256 * This applies only to read and write operations.
1258 void setup_zbd_zone_mode(struct thread_data *td, struct io_u *io_u)
1260 struct fio_file *f = io_u->file;
1261 enum fio_ddir ddir = io_u->ddir;
1262 struct fio_zone_info *z;
1265 assert(td->o.zone_mode == ZONE_MODE_ZBD);
1266 assert(td->o.zone_size);
1269 * zone_skip is valid only for sequential workloads.
1271 if (td_random(td) || !td->o.zone_skip)
1275 * It is time to switch to a new zone if:
1276 * - zone_bytes == zone_size bytes have already been accessed
1277 * - The last position reached the end of the current zone.
1278 * - For reads with td->o.read_beyond_wp == false, the last position
1279 * reached the zone write pointer.
1281 zone_idx = zbd_zone_idx(f, f->last_pos[ddir]);
1282 z = &f->zbd_info->zone_info[zone_idx];
1284 if (td->zone_bytes >= td->o.zone_size ||
1285 f->last_pos[ddir] >= (z+1)->start ||
1286 (ddir == DDIR_READ &&
1287 (!td->o.read_beyond_wp) && f->last_pos[ddir] >= z->wp)) {
1292 f->file_offset += td->o.zone_size + td->o.zone_skip;
1295 * Wrap from the beginning, if we exceed the file size
1297 if (f->file_offset >= f->real_file_size)
1298 f->file_offset = get_start_offset(td, f);
1300 f->last_pos[ddir] = f->file_offset;
1301 td->io_skip_bytes += td->o.zone_skip;
1306 * zbd_adjust_block - adjust the offset and length as necessary for ZBD drives
1307 * @td: FIO thread data.
1308 * @io_u: FIO I/O unit.
1310 * Locking strategy: returns with z->mutex locked if and only if z refers
1311 * to a sequential zone and if io_u_accept is returned. z is the zone that
1312 * corresponds to io_u->offset at the end of this function.
1314 enum io_u_action zbd_adjust_block(struct thread_data *td, struct io_u *io_u)
1316 const struct fio_file *f = io_u->file;
1317 uint32_t zone_idx_b;
1318 struct fio_zone_info *zb, *zl, *orig_zb;
1319 uint32_t orig_len = io_u->buflen;
1320 uint32_t min_bs = td->o.min_bs[io_u->ddir];
1327 assert(is_valid_offset(f, io_u->offset));
1328 assert(io_u->buflen);
1329 zone_idx_b = zbd_zone_idx(f, io_u->offset);
1330 zb = &f->zbd_info->zone_info[zone_idx_b];
1333 /* Accept the I/O offset for conventional zones. */
1334 if (zb->type == BLK_ZONE_TYPE_CONVENTIONAL)
1338 * Accept the I/O offset for reads if reading beyond the write pointer
1341 if (zb->cond != BLK_ZONE_COND_OFFLINE &&
1342 io_u->ddir == DDIR_READ && td->o.read_beyond_wp)
1348 * Lock the io_u target zone. The zone will be unlocked if io_u offset
1349 * is changed or when io_u completes and zbd_put_io() executed.
1350 * To avoid multiple jobs doing asynchronous I/Os from deadlocking each
1351 * other waiting for zone locks when building an io_u batch, first
1352 * only trylock the zone. If the zone is already locked by another job,
1353 * process the currently queued I/Os so that I/O progress is made and
1356 if (pthread_mutex_trylock(&zb->mutex) != 0) {
1357 if (!td_ioengine_flagged(td, FIO_SYNCIO))
1359 pthread_mutex_lock(&zb->mutex);
1362 switch (io_u->ddir) {
1364 if (td->runstate == TD_VERIFYING) {
1365 zb = zbd_replay_write_order(td, io_u, zb);
1369 * Check that there is enough written data in the zone to do an
1370 * I/O of at least min_bs B. If there isn't, find a new zone for
1373 range = zb->cond != BLK_ZONE_COND_OFFLINE ?
1374 zb->wp - zb->start : 0;
1375 if (range < min_bs ||
1376 ((!td_random(td)) && (io_u->offset + min_bs > zb->wp))) {
1377 pthread_mutex_unlock(&zb->mutex);
1378 zl = &f->zbd_info->zone_info[zbd_zone_idx(f,
1379 f->file_offset + f->io_size)];
1380 zb = zbd_find_zone(td, io_u, zb, zl);
1383 "%s: zbd_find_zone(%lld, %llu) failed\n",
1384 f->file_name, io_u->offset,
1389 * zbd_find_zone() returned a zone with a range of at
1392 range = zb->wp - zb->start;
1393 assert(range >= min_bs);
1396 io_u->offset = zb->start;
1399 * Make sure the I/O is within the zone valid data range while
1400 * maximizing the I/O size and preserving randomness.
1402 if (range <= io_u->buflen)
1403 io_u->offset = zb->start;
1404 else if (td_random(td))
1405 io_u->offset = zb->start +
1406 ((io_u->offset - orig_zb->start) %
1407 (range - io_u->buflen)) / min_bs * min_bs;
1409 * Make sure the I/O does not cross over the zone wp position.
1411 new_len = min((unsigned long long)io_u->buflen,
1412 (unsigned long long)(zb->wp - io_u->offset));
1413 new_len = new_len / min_bs * min_bs;
1414 if (new_len < io_u->buflen) {
1415 io_u->buflen = new_len;
1416 dprint(FD_IO, "Changed length from %u into %llu\n",
1417 orig_len, io_u->buflen);
1419 assert(zb->start <= io_u->offset);
1420 assert(io_u->offset + io_u->buflen <= zb->wp);
1423 if (io_u->buflen > f->zbd_info->zone_size)
1425 if (!zbd_open_zone(td, io_u, zone_idx_b)) {
1426 pthread_mutex_unlock(&zb->mutex);
1427 zb = zbd_convert_to_open_zone(td, io_u);
1430 zone_idx_b = zb - f->zbd_info->zone_info;
1432 /* Check whether the zone reset threshold has been exceeded */
1433 if (td->o.zrf.u.f) {
1434 if (f->zbd_info->sectors_with_data >=
1435 f->io_size * td->o.zrt.u.f &&
1436 zbd_dec_and_reset_write_cnt(td, f)) {
1440 /* Reset the zone pointer if necessary */
1441 if (zb->reset_zone || zbd_zone_full(f, zb, min_bs)) {
1442 assert(td->o.verify == VERIFY_NONE);
1444 * Since previous write requests may have been submitted
1445 * asynchronously and since we will submit the zone
1446 * reset synchronously, wait until previously submitted
1447 * write requests have completed before issuing a
1452 if (zbd_reset_zone(td, f, zb) < 0)
1455 /* Make writes occur at the write pointer */
1456 assert(!zbd_zone_full(f, zb, min_bs));
1457 io_u->offset = zb->wp;
1458 if (!is_valid_offset(f, io_u->offset)) {
1459 dprint(FD_ZBD, "Dropped request with offset %llu\n",
1464 * Make sure that the buflen is a multiple of the minimal
1465 * block size. Give up if shrinking would make the request too
1468 new_len = min((unsigned long long)io_u->buflen,
1469 (zb + 1)->start - io_u->offset);
1470 new_len = new_len / min_bs * min_bs;
1471 if (new_len == io_u->buflen)
1473 if (new_len >= min_bs) {
1474 io_u->buflen = new_len;
1475 dprint(FD_IO, "Changed length from %u into %llu\n",
1476 orig_len, io_u->buflen);
1479 log_err("Zone remainder %lld smaller than minimum block size %d\n",
1480 ((zb + 1)->start - io_u->offset),
1487 case DDIR_SYNC_FILE_RANGE:
1498 assert(zb->cond != BLK_ZONE_COND_OFFLINE);
1499 assert(!io_u->zbd_queue_io);
1500 assert(!io_u->zbd_put_io);
1501 io_u->zbd_queue_io = zbd_queue_io;
1502 io_u->zbd_put_io = zbd_put_io;
1507 pthread_mutex_unlock(&zb->mutex);
1511 /* Return a string with ZBD statistics */
1512 char *zbd_write_status(const struct thread_stat *ts)
1516 if (asprintf(&res, "; %llu zone resets", (unsigned long long) ts->nr_zone_resets) < 0)