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))
122 zone_idx = zbd_zone_idx(f, f->file_offset);
123 z = &f->zbd_info->zone_info[zone_idx];
124 if (f->file_offset != z->start) {
125 new_offset = (z+1)->start;
126 if (new_offset >= f->file_offset + f->io_size) {
127 log_info("%s: io_size must be at least one zone\n",
131 log_info("%s: rounded up offset from %llu to %llu\n",
132 f->file_name, (unsigned long long) f->file_offset,
133 (unsigned long long) new_offset);
134 f->io_size -= (new_offset - f->file_offset);
135 f->file_offset = new_offset;
137 zone_idx = zbd_zone_idx(f, f->file_offset + f->io_size);
138 z = &f->zbd_info->zone_info[zone_idx];
140 if (f->file_offset + f->io_size != new_end) {
141 if (new_end <= f->file_offset) {
142 log_info("%s: io_size must be at least one zone\n",
146 log_info("%s: rounded down io_size from %llu to %llu\n",
147 f->file_name, (unsigned long long) f->io_size,
148 (unsigned long long) new_end - f->file_offset);
149 f->io_size = new_end - f->file_offset;
157 static bool zbd_verify_bs(void)
159 struct thread_data *td;
165 for_each_file(td, f, j) {
168 zone_size = f->zbd_info->zone_size;
169 for (k = 0; k < ARRAY_SIZE(td->o.bs); k++) {
170 if (td->o.verify != VERIFY_NONE &&
171 zone_size % td->o.bs[k] != 0) {
172 log_info("%s: block size %llu is not a divisor of the zone size %d\n",
173 f->file_name, td->o.bs[k],
184 * Read zone information into @buf starting from sector @start_sector.
185 * @fd is a file descriptor that refers to a block device and @bufsz is the
188 * Returns 0 upon success and a negative error code upon failure.
190 static int read_zone_info(int fd, uint64_t start_sector,
191 void *buf, unsigned int bufsz)
193 struct blk_zone_report *hdr = buf;
195 if (bufsz < sizeof(*hdr))
198 memset(hdr, 0, sizeof(*hdr));
200 hdr->nr_zones = (bufsz - sizeof(*hdr)) / sizeof(struct blk_zone);
201 hdr->sector = start_sector;
202 return ioctl(fd, BLKREPORTZONE, hdr) >= 0 ? 0 : -errno;
206 * Read up to 255 characters from the first line of a file. Strip the trailing
209 static char *read_file(const char *path)
211 char line[256], *p = line;
214 f = fopen(path, "rb");
217 if (!fgets(line, sizeof(line), f))
225 static enum blk_zoned_model get_zbd_model(const char *file_name)
227 enum blk_zoned_model model = ZBD_DM_NONE;
228 char *zoned_attr_path = NULL;
229 char *model_str = NULL;
232 if (stat(file_name, &statbuf) < 0)
234 if (asprintf(&zoned_attr_path, "/sys/dev/block/%d:%d/queue/zoned",
235 major(statbuf.st_rdev), minor(statbuf.st_rdev)) < 0)
237 model_str = read_file(zoned_attr_path);
240 dprint(FD_ZBD, "%s: zbd model string: %s\n", file_name, model_str);
241 if (strcmp(model_str, "host-aware") == 0)
242 model = ZBD_DM_HOST_AWARE;
243 else if (strcmp(model_str, "host-managed") == 0)
244 model = ZBD_DM_HOST_MANAGED;
248 free(zoned_attr_path);
252 static int ilog2(uint64_t i)
264 * Initialize f->zbd_info for devices that are not zoned block devices. This
265 * allows to execute a ZBD workload against a non-ZBD device.
267 static int init_zone_info(struct thread_data *td, struct fio_file *f)
270 struct fio_zone_info *p;
272 struct zoned_block_device_info *zbd_info = NULL;
273 pthread_mutexattr_t attr;
276 zone_size = td->o.zone_size;
278 nr_zones = (f->real_file_size + zone_size - 1) / zone_size;
279 zbd_info = scalloc(1, sizeof(*zbd_info) +
280 (nr_zones + 1) * sizeof(zbd_info->zone_info[0]));
284 pthread_mutexattr_init(&attr);
285 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
286 pthread_mutexattr_setpshared(&attr, true);
287 pthread_mutex_init(&zbd_info->mutex, &attr);
288 zbd_info->refcount = 1;
289 p = &zbd_info->zone_info[0];
290 for (i = 0; i < nr_zones; i++, p++) {
291 pthread_mutex_init(&p->mutex, &attr);
292 p->start = i * zone_size;
293 p->wp = p->start + zone_size;
294 p->type = BLK_ZONE_TYPE_SEQWRITE_REQ;
295 p->cond = BLK_ZONE_COND_EMPTY;
298 p->start = nr_zones * zone_size;
300 f->zbd_info = zbd_info;
301 f->zbd_info->zone_size = zone_size;
302 f->zbd_info->zone_size_log2 = is_power_of_2(zone_size) ?
303 ilog2(zone_size) : -1;
304 f->zbd_info->nr_zones = nr_zones;
305 pthread_mutexattr_destroy(&attr);
310 * Parse the BLKREPORTZONE output and store it in f->zbd_info. Must be called
311 * only for devices that support this ioctl, namely zoned block devices.
313 static int parse_zone_info(struct thread_data *td, struct fio_file *f)
315 const unsigned int bufsz = sizeof(struct blk_zone_report) +
316 4096 * sizeof(struct blk_zone);
318 struct blk_zone_report *hdr;
319 const struct blk_zone *z;
320 struct fio_zone_info *p;
321 uint64_t zone_size, start_sector;
322 struct zoned_block_device_info *zbd_info = NULL;
323 pthread_mutexattr_t attr;
325 int fd, i, j, ret = 0;
327 pthread_mutexattr_init(&attr);
328 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
329 pthread_mutexattr_setpshared(&attr, true);
335 fd = open(f->file_name, O_RDONLY | O_LARGEFILE);
341 ret = read_zone_info(fd, 0, buf, bufsz);
343 log_info("fio: BLKREPORTZONE(%lu) failed for %s (%d).\n",
344 0UL, f->file_name, -ret);
348 if (hdr->nr_zones < 1) {
349 log_info("fio: %s has invalid zone information.\n",
353 z = (void *)(hdr + 1);
354 zone_size = z->len << 9;
355 nr_zones = (f->real_file_size + zone_size - 1) / zone_size;
357 if (td->o.zone_size == 0) {
358 td->o.zone_size = zone_size;
359 } else if (td->o.zone_size != zone_size) {
360 log_info("fio: %s job parameter zonesize %llu does not match disk zone size %llu.\n",
361 f->file_name, (unsigned long long) td->o.zone_size,
362 (unsigned long long) zone_size);
367 dprint(FD_ZBD, "Device %s has %d zones of size %llu KB\n", f->file_name,
368 nr_zones, (unsigned long long) zone_size / 1024);
370 zbd_info = scalloc(1, sizeof(*zbd_info) +
371 (nr_zones + 1) * sizeof(zbd_info->zone_info[0]));
375 pthread_mutex_init(&zbd_info->mutex, &attr);
376 zbd_info->refcount = 1;
377 p = &zbd_info->zone_info[0];
378 for (start_sector = 0, j = 0; j < nr_zones;) {
379 z = (void *)(hdr + 1);
380 for (i = 0; i < hdr->nr_zones; i++, j++, z++, p++) {
381 pthread_mutex_init(&p->mutex, &attr);
382 p->start = z->start << 9;
384 case BLK_ZONE_COND_NOT_WP:
387 case BLK_ZONE_COND_FULL:
388 p->wp = p->start + zone_size;
391 assert(z->start <= z->wp);
392 assert(z->wp <= z->start + (zone_size >> 9));
398 if (j > 0 && p->start != p[-1].start + zone_size) {
399 log_info("%s: invalid zone data\n",
406 start_sector = z->start + z->len;
409 ret = read_zone_info(fd, start_sector, buf, bufsz);
411 log_info("fio: BLKREPORTZONE(%llu) failed for %s (%d).\n",
412 (unsigned long long) start_sector, f->file_name, -ret);
417 zbd_info->zone_info[nr_zones].start = start_sector << 9;
419 f->zbd_info = zbd_info;
420 f->zbd_info->zone_size = zone_size;
421 f->zbd_info->zone_size_log2 = is_power_of_2(zone_size) ?
422 ilog2(zone_size) : -1;
423 f->zbd_info->nr_zones = nr_zones;
433 pthread_mutexattr_destroy(&attr);
438 * Allocate zone information and store it into f->zbd_info if zonemode=zbd.
440 * Returns 0 upon success and a negative error code upon failure.
442 int zbd_create_zone_info(struct thread_data *td, struct fio_file *f)
444 enum blk_zoned_model zbd_model;
447 assert(td->o.zone_mode == ZONE_MODE_ZBD);
449 zbd_model = get_zbd_model(f->file_name);
451 case ZBD_DM_HOST_AWARE:
452 case ZBD_DM_HOST_MANAGED:
453 ret = parse_zone_info(td, f);
456 ret = init_zone_info(td, f);
460 f->zbd_info->model = zbd_model;
464 void zbd_free_zone_info(struct fio_file *f)
471 pthread_mutex_lock(&f->zbd_info->mutex);
472 refcount = --f->zbd_info->refcount;
473 pthread_mutex_unlock(&f->zbd_info->mutex);
475 assert((int32_t)refcount >= 0);
482 * Initialize f->zbd_info.
484 * Returns 0 upon success and a negative error code upon failure.
486 * Note: this function can only work correctly if it is called before the first
489 static int zbd_init_zone_info(struct thread_data *td, struct fio_file *file)
491 struct thread_data *td2;
495 for_each_td(td2, i) {
496 for_each_file(td2, f2, j) {
497 if (td2 == td && f2 == file)
500 strcmp(f2->file_name, file->file_name) != 0)
502 file->zbd_info = f2->zbd_info;
503 file->zbd_info->refcount++;
508 ret = zbd_create_zone_info(td, file);
510 td_verror(td, -ret, "BLKREPORTZONE failed");
514 int zbd_init(struct thread_data *td)
519 for_each_file(td, f, i) {
520 if (f->filetype != FIO_TYPE_BLOCK)
522 if (td->o.zone_size && td->o.zone_size < 512) {
523 log_err("%s: zone size must be at least 512 bytes for --zonemode=zbd\n\n",
527 if (td->o.zone_size == 0 &&
528 get_zbd_model(f->file_name) == ZBD_DM_NONE) {
529 log_err("%s: Specifying the zone size is mandatory for regular block devices with --zonemode=zbd\n\n",
533 zbd_init_zone_info(td, f);
536 if (!zbd_using_direct_io()) {
537 log_err("Using direct I/O is mandatory for writing to ZBD drives\n\n");
541 if (!zbd_verify_sizes())
544 if (!zbd_verify_bs())
551 * zbd_reset_range - reset zones for a range of sectors
552 * @td: FIO thread data.
553 * @f: Fio file for which to reset zones
554 * @sector: Starting sector in units of 512 bytes
555 * @nr_sectors: Number of sectors in units of 512 bytes
557 * Returns 0 upon success and a negative error code upon failure.
559 static int zbd_reset_range(struct thread_data *td, const struct fio_file *f,
560 uint64_t offset, uint64_t length)
562 struct blk_zone_range zr = {
563 .sector = offset >> 9,
564 .nr_sectors = length >> 9,
566 uint32_t zone_idx_b, zone_idx_e;
567 struct fio_zone_info *zb, *ze, *z;
571 assert(is_valid_offset(f, offset + length - 1));
572 switch (f->zbd_info->model) {
573 case ZBD_DM_HOST_AWARE:
574 case ZBD_DM_HOST_MANAGED:
575 ret = ioctl(f->fd, BLKRESETZONE, &zr);
577 td_verror(td, errno, "resetting wp failed");
578 log_err("%s: resetting wp for %llu sectors at sector %llu failed (%d).\n",
579 f->file_name, zr.nr_sectors, zr.sector, errno);
587 zone_idx_b = zbd_zone_idx(f, offset);
588 zb = &f->zbd_info->zone_info[zone_idx_b];
589 zone_idx_e = zbd_zone_idx(f, offset + length);
590 ze = &f->zbd_info->zone_info[zone_idx_e];
591 for (z = zb; z < ze; z++) {
592 pthread_mutex_lock(&z->mutex);
593 pthread_mutex_lock(&f->zbd_info->mutex);
594 f->zbd_info->sectors_with_data -= z->wp - z->start;
595 pthread_mutex_unlock(&f->zbd_info->mutex);
598 pthread_mutex_unlock(&z->mutex);
601 td->ts.nr_zone_resets += ze - zb;
606 static unsigned int zbd_zone_nr(struct zoned_block_device_info *zbd_info,
607 struct fio_zone_info *zone)
609 return (uintptr_t) zone - (uintptr_t) zbd_info->zone_info;
613 * zbd_reset_zone - reset the write pointer of a single zone
614 * @td: FIO thread data.
615 * @f: FIO file associated with the disk for which to reset a write pointer.
618 * Returns 0 upon success and a negative error code upon failure.
620 static int zbd_reset_zone(struct thread_data *td, const struct fio_file *f,
621 struct fio_zone_info *z)
625 dprint(FD_ZBD, "%s: resetting wp of zone %u.\n", f->file_name,
626 zbd_zone_nr(f->zbd_info, z));
627 ret = zbd_reset_range(td, f, z->start, (z+1)->start - z->start);
632 * Reset a range of zones. Returns 0 upon success and 1 upon failure.
633 * @td: fio thread data.
634 * @f: fio file for which to reset zones
635 * @zb: first zone to reset.
636 * @ze: first zone not to reset.
637 * @all_zones: whether to reset all zones or only those zones for which the
638 * write pointer is not a multiple of td->o.min_bs[DDIR_WRITE].
640 static int zbd_reset_zones(struct thread_data *td, struct fio_file *f,
641 struct fio_zone_info *const zb,
642 struct fio_zone_info *const ze, bool all_zones)
644 struct fio_zone_info *z, *start_z = ze;
645 const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
649 dprint(FD_ZBD, "%s: examining zones %u .. %u\n", f->file_name,
650 zbd_zone_nr(f->zbd_info, zb), zbd_zone_nr(f->zbd_info, ze));
652 for (z = zb; z < ze; z++) {
653 pthread_mutex_lock(&z->mutex);
655 case BLK_ZONE_TYPE_SEQWRITE_REQ:
656 reset_wp = all_zones ? z->wp != z->start :
657 (td->o.td_ddir & TD_DDIR_WRITE) &&
659 if (start_z == ze && reset_wp) {
661 } else if (start_z < ze && !reset_wp) {
663 "%s: resetting zones %u .. %u\n",
665 zbd_zone_nr(f->zbd_info, start_z),
666 zbd_zone_nr(f->zbd_info, z));
667 if (zbd_reset_range(td, f, start_z->start,
668 z->start - start_z->start) < 0)
676 dprint(FD_ZBD, "%s: resetting zones %u .. %u\n",
677 f->file_name, zbd_zone_nr(f->zbd_info, start_z),
678 zbd_zone_nr(f->zbd_info, z));
679 if (zbd_reset_range(td, f, start_z->start,
680 z->start - start_z->start) < 0)
687 dprint(FD_ZBD, "%s: resetting zones %u .. %u\n", f->file_name,
688 zbd_zone_nr(f->zbd_info, start_z),
689 zbd_zone_nr(f->zbd_info, z));
690 if (zbd_reset_range(td, f, start_z->start,
691 z->start - start_z->start) < 0)
694 for (z = zb; z < ze; z++)
695 pthread_mutex_unlock(&z->mutex);
701 * Reset zbd_info.write_cnt, the counter that counts down towards the next
704 static void zbd_reset_write_cnt(const struct thread_data *td,
705 const struct fio_file *f)
707 assert(0 <= td->o.zrf.u.f && td->o.zrf.u.f <= 1);
709 pthread_mutex_lock(&f->zbd_info->mutex);
710 f->zbd_info->write_cnt = td->o.zrf.u.f ?
711 min(1.0 / td->o.zrf.u.f, 0.0 + UINT_MAX) : UINT_MAX;
712 pthread_mutex_unlock(&f->zbd_info->mutex);
715 static bool zbd_dec_and_reset_write_cnt(const struct thread_data *td,
716 const struct fio_file *f)
718 uint32_t write_cnt = 0;
720 pthread_mutex_lock(&f->zbd_info->mutex);
721 assert(f->zbd_info->write_cnt);
722 if (f->zbd_info->write_cnt)
723 write_cnt = --f->zbd_info->write_cnt;
725 zbd_reset_write_cnt(td, f);
726 pthread_mutex_unlock(&f->zbd_info->mutex);
728 return write_cnt == 0;
731 /* Check whether the value of zbd_info.sectors_with_data is correct. */
732 static void check_swd(const struct thread_data *td, const struct fio_file *f)
735 struct fio_zone_info *zb, *ze, *z;
738 zb = &f->zbd_info->zone_info[zbd_zone_idx(f, f->file_offset)];
739 ze = &f->zbd_info->zone_info[zbd_zone_idx(f, f->file_offset +
742 for (z = zb; z < ze; z++) {
743 pthread_mutex_lock(&z->mutex);
744 swd += z->wp - z->start;
746 pthread_mutex_lock(&f->zbd_info->mutex);
747 assert(f->zbd_info->sectors_with_data == swd);
748 pthread_mutex_unlock(&f->zbd_info->mutex);
749 for (z = zb; z < ze; z++)
750 pthread_mutex_unlock(&z->mutex);
754 void zbd_file_reset(struct thread_data *td, struct fio_file *f)
756 struct fio_zone_info *zb, *ze, *z;
763 zb = &f->zbd_info->zone_info[zbd_zone_idx(f, f->file_offset)];
764 zone_idx_e = zbd_zone_idx(f, f->file_offset + f->io_size);
765 ze = &f->zbd_info->zone_info[zone_idx_e];
766 for (z = zb ; z < ze; z++) {
767 pthread_mutex_lock(&z->mutex);
768 swd += z->wp - z->start;
770 pthread_mutex_lock(&f->zbd_info->mutex);
771 f->zbd_info->sectors_with_data = swd;
772 pthread_mutex_unlock(&f->zbd_info->mutex);
773 for (z = zb ; z < ze; z++)
774 pthread_mutex_unlock(&z->mutex);
775 dprint(FD_ZBD, "%s(%s): swd = %llu\n", __func__, f->file_name,
776 (unsigned long long) swd);
778 * If data verification is enabled reset the affected zones before
779 * writing any data to avoid that a zone reset has to be issued while
780 * writing data, which causes data loss.
782 zbd_reset_zones(td, f, zb, ze, td->o.verify != VERIFY_NONE &&
783 (td->o.td_ddir & TD_DDIR_WRITE) &&
784 td->runstate != TD_VERIFYING);
785 zbd_reset_write_cnt(td, f);
788 /* The caller must hold f->zbd_info->mutex. */
789 static bool is_zone_open(const struct thread_data *td, const struct fio_file *f,
790 unsigned int zone_idx)
792 struct zoned_block_device_info *zbdi = f->zbd_info;
795 assert(td->o.max_open_zones <= ARRAY_SIZE(zbdi->open_zones));
796 assert(zbdi->num_open_zones <= td->o.max_open_zones);
798 for (i = 0; i < zbdi->num_open_zones; i++)
799 if (zbdi->open_zones[i] == zone_idx)
806 * Open a ZBD zone if it was not yet open. Returns true if either the zone was
807 * already open or if opening a new zone is allowed. Returns false if the zone
808 * was not yet open and opening a new zone would cause the zone limit to be
811 static bool zbd_open_zone(struct thread_data *td, const struct io_u *io_u,
814 const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
815 const struct fio_file *f = io_u->file;
816 struct fio_zone_info *z = &f->zbd_info->zone_info[zone_idx];
819 if (z->cond == BLK_ZONE_COND_OFFLINE)
823 * Skip full zones with data verification enabled because resetting a
824 * zone causes data loss and hence causes verification to fail.
826 if (td->o.verify != VERIFY_NONE && zbd_zone_full(f, z, min_bs))
829 /* Zero means no limit */
830 if (!td->o.max_open_zones)
833 pthread_mutex_lock(&f->zbd_info->mutex);
834 if (is_zone_open(td, f, zone_idx))
837 if (f->zbd_info->num_open_zones >= td->o.max_open_zones)
839 dprint(FD_ZBD, "%s: opening zone %d\n", f->file_name, zone_idx);
840 f->zbd_info->open_zones[f->zbd_info->num_open_zones++] = zone_idx;
845 pthread_mutex_unlock(&f->zbd_info->mutex);
849 /* The caller must hold f->zbd_info->mutex */
850 static void zbd_close_zone(struct thread_data *td, const struct fio_file *f,
851 unsigned int open_zone_idx)
855 assert(open_zone_idx < f->zbd_info->num_open_zones);
856 zone_idx = f->zbd_info->open_zones[open_zone_idx];
857 memmove(f->zbd_info->open_zones + open_zone_idx,
858 f->zbd_info->open_zones + open_zone_idx + 1,
859 (FIO_MAX_OPEN_ZBD_ZONES - (open_zone_idx + 1)) *
860 sizeof(f->zbd_info->open_zones[0]));
861 f->zbd_info->num_open_zones--;
862 f->zbd_info->zone_info[zone_idx].open = 0;
866 * Modify the offset of an I/O unit that does not refer to an open zone such
867 * that it refers to an open zone. Close an open zone and open a new zone if
868 * necessary. This algorithm can only work correctly if all write pointers are
869 * a multiple of the fio block size. The caller must neither hold z->mutex
870 * nor f->zbd_info->mutex. Returns with z->mutex held upon success.
872 struct fio_zone_info *zbd_convert_to_open_zone(struct thread_data *td,
875 const uint32_t min_bs = td->o.min_bs[io_u->ddir];
876 const struct fio_file *f = io_u->file;
877 struct fio_zone_info *z;
878 unsigned int open_zone_idx = -1;
879 uint32_t zone_idx, new_zone_idx;
882 assert(is_valid_offset(f, io_u->offset));
884 if (td->o.max_open_zones) {
886 * This statement accesses f->zbd_info->open_zones[] on purpose
889 zone_idx = f->zbd_info->open_zones[(io_u->offset -
891 f->zbd_info->num_open_zones / f->io_size];
893 zone_idx = zbd_zone_idx(f, io_u->offset);
895 dprint(FD_ZBD, "%s(%s): starting from zone %d (offset %lld, buflen %lld)\n",
896 __func__, f->file_name, zone_idx, io_u->offset, io_u->buflen);
899 * Since z->mutex is the outer lock and f->zbd_info->mutex the inner
900 * lock it can happen that the state of the zone with index zone_idx
901 * has changed after 'z' has been assigned and before f->zbd_info->mutex
902 * has been obtained. Hence the loop.
905 z = &f->zbd_info->zone_info[zone_idx];
907 pthread_mutex_lock(&z->mutex);
908 pthread_mutex_lock(&f->zbd_info->mutex);
909 if (td->o.max_open_zones == 0)
911 if (f->zbd_info->num_open_zones == 0) {
912 pthread_mutex_unlock(&f->zbd_info->mutex);
913 pthread_mutex_unlock(&z->mutex);
914 dprint(FD_ZBD, "%s(%s): no zones are open\n",
915 __func__, f->file_name);
918 open_zone_idx = (io_u->offset - f->file_offset) *
919 f->zbd_info->num_open_zones / f->io_size;
920 assert(open_zone_idx < f->zbd_info->num_open_zones);
921 new_zone_idx = f->zbd_info->open_zones[open_zone_idx];
922 if (new_zone_idx == zone_idx)
924 zone_idx = new_zone_idx;
925 pthread_mutex_unlock(&f->zbd_info->mutex);
926 pthread_mutex_unlock(&z->mutex);
929 /* Both z->mutex and f->zbd_info->mutex are held. */
932 if (z->wp + min_bs <= (z+1)->start) {
933 pthread_mutex_unlock(&f->zbd_info->mutex);
936 dprint(FD_ZBD, "%s(%s): closing zone %d\n", __func__, f->file_name,
938 if (td->o.max_open_zones)
939 zbd_close_zone(td, f, open_zone_idx);
940 pthread_mutex_unlock(&f->zbd_info->mutex);
942 /* Only z->mutex is held. */
944 /* Zone 'z' is full, so try to open a new zone. */
945 for (i = f->io_size / f->zbd_info->zone_size; i > 0; i--) {
947 pthread_mutex_unlock(&z->mutex);
949 if (!is_valid_offset(f, z->start)) {
951 zone_idx = zbd_zone_idx(f, f->file_offset);
952 z = &f->zbd_info->zone_info[zone_idx];
954 assert(is_valid_offset(f, z->start));
955 pthread_mutex_lock(&z->mutex);
958 if (zbd_open_zone(td, io_u, zone_idx))
962 /* Only z->mutex is held. */
964 /* Check whether the write fits in any of the already opened zones. */
965 pthread_mutex_lock(&f->zbd_info->mutex);
966 for (i = 0; i < f->zbd_info->num_open_zones; i++) {
967 zone_idx = f->zbd_info->open_zones[i];
968 pthread_mutex_unlock(&f->zbd_info->mutex);
969 pthread_mutex_unlock(&z->mutex);
971 z = &f->zbd_info->zone_info[zone_idx];
973 pthread_mutex_lock(&z->mutex);
974 if (z->wp + min_bs <= (z+1)->start)
976 pthread_mutex_lock(&f->zbd_info->mutex);
978 pthread_mutex_unlock(&f->zbd_info->mutex);
979 pthread_mutex_unlock(&z->mutex);
980 dprint(FD_ZBD, "%s(%s): did not open another zone\n", __func__,
985 dprint(FD_ZBD, "%s(%s): returning zone %d\n", __func__, f->file_name,
987 io_u->offset = z->start;
991 /* The caller must hold z->mutex. */
992 static struct fio_zone_info *zbd_replay_write_order(struct thread_data *td,
994 struct fio_zone_info *z)
996 const struct fio_file *f = io_u->file;
997 const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
999 if (!zbd_open_zone(td, io_u, z - f->zbd_info->zone_info)) {
1000 pthread_mutex_unlock(&z->mutex);
1001 z = zbd_convert_to_open_zone(td, io_u);
1005 if (z->verify_block * min_bs >= f->zbd_info->zone_size)
1006 log_err("%s: %d * %d >= %llu\n", f->file_name, z->verify_block,
1007 min_bs, (unsigned long long) f->zbd_info->zone_size);
1008 io_u->offset = z->start + z->verify_block++ * min_bs;
1013 * Find another zone for which @io_u fits below the write pointer. Start
1014 * searching in zones @zb + 1 .. @zl and continue searching in zones
1017 * Either returns NULL or returns a zone pointer and holds the mutex for that
1020 static struct fio_zone_info *
1021 zbd_find_zone(struct thread_data *td, struct io_u *io_u,
1022 struct fio_zone_info *zb, struct fio_zone_info *zl)
1024 const uint32_t min_bs = td->o.min_bs[io_u->ddir];
1025 const struct fio_file *f = io_u->file;
1026 struct fio_zone_info *z1, *z2;
1027 const struct fio_zone_info *const zf =
1028 &f->zbd_info->zone_info[zbd_zone_idx(f, f->file_offset)];
1031 * Skip to the next non-empty zone in case of sequential I/O and to
1032 * the nearest non-empty zone in case of random I/O.
1034 for (z1 = zb + 1, z2 = zb - 1; z1 < zl || z2 >= zf; z1++, z2--) {
1035 if (z1 < zl && z1->cond != BLK_ZONE_COND_OFFLINE) {
1036 pthread_mutex_lock(&z1->mutex);
1037 if (z1->start + min_bs <= z1->wp)
1039 pthread_mutex_unlock(&z1->mutex);
1040 } else if (!td_random(td)) {
1043 if (td_random(td) && z2 >= zf &&
1044 z2->cond != BLK_ZONE_COND_OFFLINE) {
1045 pthread_mutex_lock(&z2->mutex);
1046 if (z2->start + min_bs <= z2->wp)
1048 pthread_mutex_unlock(&z2->mutex);
1051 dprint(FD_ZBD, "%s: adjusting random read offset failed\n",
1058 * zbd_post_submit - update the write pointer and unlock the zone lock
1060 * @success: Whether or not the I/O unit has been executed successfully
1062 * For write and trim operations, update the write pointer of all affected
1065 static void zbd_post_submit(const struct io_u *io_u, bool success)
1067 struct zoned_block_device_info *zbd_info;
1068 struct fio_zone_info *z;
1070 uint64_t end, zone_end;
1072 zbd_info = io_u->file->zbd_info;
1076 zone_idx = zbd_zone_idx(io_u->file, io_u->offset);
1077 end = io_u->offset + io_u->buflen;
1078 z = &zbd_info->zone_info[zone_idx];
1079 assert(zone_idx < zbd_info->nr_zones);
1080 if (z->type != BLK_ZONE_TYPE_SEQWRITE_REQ)
1084 switch (io_u->ddir) {
1086 zone_end = min(end, (z + 1)->start);
1087 pthread_mutex_lock(&zbd_info->mutex);
1089 * z->wp > zone_end means that one or more I/O errors
1092 if (z->wp <= zone_end)
1093 zbd_info->sectors_with_data += zone_end - z->wp;
1094 pthread_mutex_unlock(&zbd_info->mutex);
1098 assert(z->wp == z->start);
1104 pthread_mutex_unlock(&z->mutex);
1107 bool zbd_unaligned_write(int error_code)
1109 switch (error_code) {
1118 * zbd_adjust_block - adjust the offset and length as necessary for ZBD drives
1119 * @td: FIO thread data.
1120 * @io_u: FIO I/O unit.
1122 * Locking strategy: returns with z->mutex locked if and only if z refers
1123 * to a sequential zone and if io_u_accept is returned. z is the zone that
1124 * corresponds to io_u->offset at the end of this function.
1126 enum io_u_action zbd_adjust_block(struct thread_data *td, struct io_u *io_u)
1128 const struct fio_file *f = io_u->file;
1129 uint32_t zone_idx_b;
1130 struct fio_zone_info *zb, *zl, *orig_zb;
1131 uint32_t orig_len = io_u->buflen;
1132 uint32_t min_bs = td->o.min_bs[io_u->ddir];
1139 assert(is_valid_offset(f, io_u->offset));
1140 assert(io_u->buflen);
1141 zone_idx_b = zbd_zone_idx(f, io_u->offset);
1142 zb = &f->zbd_info->zone_info[zone_idx_b];
1145 /* Accept the I/O offset for conventional zones. */
1146 if (zb->type == BLK_ZONE_TYPE_CONVENTIONAL)
1150 * Accept the I/O offset for reads if reading beyond the write pointer
1153 if (zb->cond != BLK_ZONE_COND_OFFLINE &&
1154 io_u->ddir == DDIR_READ && td->o.read_beyond_wp)
1157 pthread_mutex_lock(&zb->mutex);
1158 switch (io_u->ddir) {
1160 if (td->runstate == TD_VERIFYING) {
1161 zb = zbd_replay_write_order(td, io_u, zb);
1165 * Check that there is enough written data in the zone to do an
1166 * I/O of at least min_bs B. If there isn't, find a new zone for
1169 range = zb->cond != BLK_ZONE_COND_OFFLINE ?
1170 zb->wp - zb->start : 0;
1171 if (range < min_bs ||
1172 ((!td_random(td)) && (io_u->offset + min_bs > zb->wp))) {
1173 pthread_mutex_unlock(&zb->mutex);
1174 zl = &f->zbd_info->zone_info[zbd_zone_idx(f,
1175 f->file_offset + f->io_size)];
1176 zb = zbd_find_zone(td, io_u, zb, zl);
1179 "%s: zbd_find_zone(%lld, %llu) failed\n",
1180 f->file_name, io_u->offset,
1185 * zbd_find_zone() returned a zone with a range of at
1188 range = zb->wp - zb->start;
1189 assert(range >= min_bs);
1192 io_u->offset = zb->start;
1195 * Make sure the I/O is within the zone valid data range while
1196 * maximizing the I/O size and preserving randomness.
1198 if (range <= io_u->buflen)
1199 io_u->offset = zb->start;
1200 else if (td_random(td))
1201 io_u->offset = zb->start +
1202 ((io_u->offset - orig_zb->start) %
1203 (range - io_u->buflen)) / min_bs * min_bs;
1205 * Make sure the I/O does not cross over the zone wp position.
1207 new_len = min((unsigned long long)io_u->buflen,
1208 (unsigned long long)(zb->wp - io_u->offset));
1209 new_len = new_len / min_bs * min_bs;
1210 if (new_len < io_u->buflen) {
1211 io_u->buflen = new_len;
1212 dprint(FD_IO, "Changed length from %u into %llu\n",
1213 orig_len, io_u->buflen);
1215 assert(zb->start <= io_u->offset);
1216 assert(io_u->offset + io_u->buflen <= zb->wp);
1219 if (io_u->buflen > f->zbd_info->zone_size)
1221 if (!zbd_open_zone(td, io_u, zone_idx_b)) {
1222 pthread_mutex_unlock(&zb->mutex);
1223 zb = zbd_convert_to_open_zone(td, io_u);
1226 zone_idx_b = zb - f->zbd_info->zone_info;
1228 /* Check whether the zone reset threshold has been exceeded */
1229 if (td->o.zrf.u.f) {
1231 if (f->zbd_info->sectors_with_data >=
1232 f->io_size * td->o.zrt.u.f &&
1233 zbd_dec_and_reset_write_cnt(td, f)) {
1237 /* Reset the zone pointer if necessary */
1238 if (zb->reset_zone || zbd_zone_full(f, zb, min_bs)) {
1239 assert(td->o.verify == VERIFY_NONE);
1241 * Since previous write requests may have been submitted
1242 * asynchronously and since we will submit the zone
1243 * reset synchronously, wait until previously submitted
1244 * write requests have completed before issuing a
1249 if (zbd_reset_zone(td, f, zb) < 0)
1253 /* Make writes occur at the write pointer */
1254 assert(!zbd_zone_full(f, zb, min_bs));
1255 io_u->offset = zb->wp;
1256 if (!is_valid_offset(f, io_u->offset)) {
1257 dprint(FD_ZBD, "Dropped request with offset %llu\n",
1262 * Make sure that the buflen is a multiple of the minimal
1263 * block size. Give up if shrinking would make the request too
1266 new_len = min((unsigned long long)io_u->buflen,
1267 (zb + 1)->start - io_u->offset);
1268 new_len = new_len / min_bs * min_bs;
1269 if (new_len == io_u->buflen)
1271 if (new_len >= min_bs) {
1272 io_u->buflen = new_len;
1273 dprint(FD_IO, "Changed length from %u into %llu\n",
1274 orig_len, io_u->buflen);
1277 log_err("Zone remainder %lld smaller than minimum block size %d\n",
1278 ((zb + 1)->start - io_u->offset),
1285 case DDIR_SYNC_FILE_RANGE:
1296 assert(zb->cond != BLK_ZONE_COND_OFFLINE);
1297 assert(!io_u->post_submit);
1298 io_u->post_submit = zbd_post_submit;
1303 pthread_mutex_unlock(&zb->mutex);
1307 /* Return a string with ZBD statistics */
1308 char *zbd_write_status(const struct thread_stat *ts)
1312 if (asprintf(&res, "; %llu zone resets", (unsigned long long) ts->nr_zone_resets) < 0)