zbd: count sectors with data for write pointer zones
[fio.git] / zbd.c
... / ...
CommitLineData
1/*
2 * Copyright (C) 2018 Western Digital Corporation or its affiliates.
3 *
4 * This file is released under the GPL.
5 */
6
7#include <errno.h>
8#include <string.h>
9#include <stdlib.h>
10#include <fcntl.h>
11#include <sys/stat.h>
12#include <unistd.h>
13
14#include "os/os.h"
15#include "file.h"
16#include "fio.h"
17#include "lib/pow2.h"
18#include "log.h"
19#include "oslib/asprintf.h"
20#include "smalloc.h"
21#include "verify.h"
22#include "pshared.h"
23#include "zbd.h"
24
25/**
26 * zbd_get_zoned_model - Get a device zoned model
27 * @td: FIO thread data
28 * @f: FIO file for which to get model information
29 */
30int zbd_get_zoned_model(struct thread_data *td, struct fio_file *f,
31 enum zbd_zoned_model *model)
32{
33 int ret;
34
35 if (td->io_ops && td->io_ops->get_zoned_model)
36 ret = td->io_ops->get_zoned_model(td, f, model);
37 else
38 ret = blkzoned_get_zoned_model(td, f, model);
39 if (ret < 0) {
40 td_verror(td, errno, "get zoned model failed");
41 log_err("%s: get zoned model failed (%d).\n",
42 f->file_name, errno);
43 }
44
45 return ret;
46}
47
48/**
49 * zbd_report_zones - Get zone information
50 * @td: FIO thread data.
51 * @f: FIO file for which to get zone information
52 * @offset: offset from which to report zones
53 * @zones: Array of struct zbd_zone
54 * @nr_zones: Size of @zones array
55 *
56 * Get zone information into @zones starting from the zone at offset @offset
57 * for the device specified by @f.
58 *
59 * Returns the number of zones reported upon success and a negative error code
60 * upon failure. If the zone report is empty, always assume an error (device
61 * problem) and return -EIO.
62 */
63int zbd_report_zones(struct thread_data *td, struct fio_file *f,
64 uint64_t offset, struct zbd_zone *zones,
65 unsigned int nr_zones)
66{
67 int ret;
68
69 if (td->io_ops && td->io_ops->report_zones)
70 ret = td->io_ops->report_zones(td, f, offset, zones, nr_zones);
71 else
72 ret = blkzoned_report_zones(td, f, offset, zones, nr_zones);
73 if (ret < 0) {
74 td_verror(td, errno, "report zones failed");
75 log_err("%s: report zones from sector %llu failed (%d).\n",
76 f->file_name, (unsigned long long)offset >> 9, errno);
77 } else if (ret == 0) {
78 td_verror(td, errno, "Empty zone report");
79 log_err("%s: report zones from sector %llu is empty.\n",
80 f->file_name, (unsigned long long)offset >> 9);
81 ret = -EIO;
82 }
83
84 return ret;
85}
86
87/**
88 * zbd_reset_wp - reset the write pointer of a range of zones
89 * @td: FIO thread data.
90 * @f: FIO file for which to reset zones
91 * @offset: Starting offset of the first zone to reset
92 * @length: Length of the range of zones to reset
93 *
94 * Reset the write pointer of all zones in the range @offset...@offset+@length.
95 * Returns 0 upon success and a negative error code upon failure.
96 */
97int zbd_reset_wp(struct thread_data *td, struct fio_file *f,
98 uint64_t offset, uint64_t length)
99{
100 int ret;
101
102 if (td->io_ops && td->io_ops->reset_wp)
103 ret = td->io_ops->reset_wp(td, f, offset, length);
104 else
105 ret = blkzoned_reset_wp(td, f, offset, length);
106 if (ret < 0) {
107 td_verror(td, errno, "resetting wp failed");
108 log_err("%s: resetting wp for %llu sectors at sector %llu failed (%d).\n",
109 f->file_name, (unsigned long long)length >> 9,
110 (unsigned long long)offset >> 9, errno);
111 }
112
113 return ret;
114}
115
116/**
117 * zbd_zone_idx - convert an offset into a zone number
118 * @f: file pointer.
119 * @offset: offset in bytes. If this offset is in the first zone_size bytes
120 * past the disk size then the index of the sentinel is returned.
121 */
122static uint32_t zbd_zone_idx(const struct fio_file *f, uint64_t offset)
123{
124 uint32_t zone_idx;
125
126 if (f->zbd_info->zone_size_log2 > 0)
127 zone_idx = offset >> f->zbd_info->zone_size_log2;
128 else
129 zone_idx = offset / f->zbd_info->zone_size;
130
131 return min(zone_idx, f->zbd_info->nr_zones);
132}
133
134/**
135 * zbd_zone_end - Return zone end location
136 * @z: zone info pointer.
137 */
138static inline uint64_t zbd_zone_end(const struct fio_zone_info *z)
139{
140 return (z+1)->start;
141}
142
143/**
144 * zbd_zone_capacity_end - Return zone capacity limit end location
145 * @z: zone info pointer.
146 */
147static inline uint64_t zbd_zone_capacity_end(const struct fio_zone_info *z)
148{
149 return z->start + z->capacity;
150}
151
152/**
153 * zbd_zone_full - verify whether a minimum number of bytes remain in a zone
154 * @f: file pointer.
155 * @z: zone info pointer.
156 * @required: minimum number of bytes that must remain in a zone.
157 *
158 * The caller must hold z->mutex.
159 */
160static bool zbd_zone_full(const struct fio_file *f, struct fio_zone_info *z,
161 uint64_t required)
162{
163 assert((required & 511) == 0);
164
165 return z->has_wp &&
166 z->wp + required > zbd_zone_capacity_end(z);
167}
168
169static void zone_lock(struct thread_data *td, struct fio_file *f, struct fio_zone_info *z)
170{
171 struct zoned_block_device_info *zbd = f->zbd_info;
172 uint32_t nz = z - zbd->zone_info;
173
174 /* A thread should never lock zones outside its working area. */
175 assert(f->min_zone <= nz && nz < f->max_zone);
176
177 assert(z->has_wp);
178
179 /*
180 * Lock the io_u target zone. The zone will be unlocked if io_u offset
181 * is changed or when io_u completes and zbd_put_io() executed.
182 * To avoid multiple jobs doing asynchronous I/Os from deadlocking each
183 * other waiting for zone locks when building an io_u batch, first
184 * only trylock the zone. If the zone is already locked by another job,
185 * process the currently queued I/Os so that I/O progress is made and
186 * zones unlocked.
187 */
188 if (pthread_mutex_trylock(&z->mutex) != 0) {
189 if (!td_ioengine_flagged(td, FIO_SYNCIO))
190 io_u_quiesce(td);
191 pthread_mutex_lock(&z->mutex);
192 }
193}
194
195static inline void zone_unlock(struct fio_zone_info *z)
196{
197 int ret;
198
199 assert(z->has_wp);
200 ret = pthread_mutex_unlock(&z->mutex);
201 assert(!ret);
202}
203
204static bool is_valid_offset(const struct fio_file *f, uint64_t offset)
205{
206 return (uint64_t)(offset - f->file_offset) < f->io_size;
207}
208
209static inline struct fio_zone_info *get_zone(const struct fio_file *f,
210 unsigned int zone_nr)
211{
212 return &f->zbd_info->zone_info[zone_nr];
213}
214
215/* Verify whether direct I/O is used for all host-managed zoned drives. */
216static bool zbd_using_direct_io(void)
217{
218 struct thread_data *td;
219 struct fio_file *f;
220 int i, j;
221
222 for_each_td(td, i) {
223 if (td->o.odirect || !(td->o.td_ddir & TD_DDIR_WRITE))
224 continue;
225 for_each_file(td, f, j) {
226 if (f->zbd_info &&
227 f->zbd_info->model == ZBD_HOST_MANAGED)
228 return false;
229 }
230 }
231
232 return true;
233}
234
235/* Whether or not the I/O range for f includes one or more sequential zones */
236static bool zbd_is_seq_job(struct fio_file *f)
237{
238 uint32_t zone_idx, zone_idx_b, zone_idx_e;
239
240 assert(f->zbd_info);
241 if (f->io_size == 0)
242 return false;
243 zone_idx_b = zbd_zone_idx(f, f->file_offset);
244 zone_idx_e = zbd_zone_idx(f, f->file_offset + f->io_size - 1);
245 for (zone_idx = zone_idx_b; zone_idx <= zone_idx_e; zone_idx++)
246 if (get_zone(f, zone_idx)->has_wp)
247 return true;
248
249 return false;
250}
251
252/*
253 * Verify whether offset and size parameters are aligned with zone boundaries.
254 */
255static bool zbd_verify_sizes(void)
256{
257 const struct fio_zone_info *z;
258 struct thread_data *td;
259 struct fio_file *f;
260 uint64_t new_offset, new_end;
261 uint32_t zone_idx;
262 int i, j;
263
264 for_each_td(td, i) {
265 for_each_file(td, f, j) {
266 if (!f->zbd_info)
267 continue;
268 if (f->file_offset >= f->real_file_size)
269 continue;
270 if (!zbd_is_seq_job(f))
271 continue;
272
273 if (!td->o.zone_size) {
274 td->o.zone_size = f->zbd_info->zone_size;
275 if (!td->o.zone_size) {
276 log_err("%s: invalid 0 zone size\n",
277 f->file_name);
278 return false;
279 }
280 } else if (td->o.zone_size != f->zbd_info->zone_size) {
281 log_err("%s: job parameter zonesize %llu does not match disk zone size %llu.\n",
282 f->file_name, (unsigned long long) td->o.zone_size,
283 (unsigned long long) f->zbd_info->zone_size);
284 return false;
285 }
286
287 if (td->o.zone_skip &&
288 (td->o.zone_skip < td->o.zone_size ||
289 td->o.zone_skip % td->o.zone_size)) {
290 log_err("%s: zoneskip %llu is not a multiple of the device zone size %llu.\n",
291 f->file_name, (unsigned long long) td->o.zone_skip,
292 (unsigned long long) td->o.zone_size);
293 return false;
294 }
295
296 zone_idx = zbd_zone_idx(f, f->file_offset);
297 z = get_zone(f, zone_idx);
298 if ((f->file_offset != z->start) &&
299 (td->o.td_ddir != TD_DDIR_READ)) {
300 new_offset = zbd_zone_end(z);
301 if (new_offset >= f->file_offset + f->io_size) {
302 log_info("%s: io_size must be at least one zone\n",
303 f->file_name);
304 return false;
305 }
306 log_info("%s: rounded up offset from %llu to %llu\n",
307 f->file_name, (unsigned long long) f->file_offset,
308 (unsigned long long) new_offset);
309 f->io_size -= (new_offset - f->file_offset);
310 f->file_offset = new_offset;
311 }
312 zone_idx = zbd_zone_idx(f, f->file_offset + f->io_size);
313 z = get_zone(f, zone_idx);
314 new_end = z->start;
315 if ((td->o.td_ddir != TD_DDIR_READ) &&
316 (f->file_offset + f->io_size != new_end)) {
317 if (new_end <= f->file_offset) {
318 log_info("%s: io_size must be at least one zone\n",
319 f->file_name);
320 return false;
321 }
322 log_info("%s: rounded down io_size from %llu to %llu\n",
323 f->file_name, (unsigned long long) f->io_size,
324 (unsigned long long) new_end - f->file_offset);
325 f->io_size = new_end - f->file_offset;
326 }
327
328 f->min_zone = zbd_zone_idx(f, f->file_offset);
329 f->max_zone = zbd_zone_idx(f, f->file_offset + f->io_size);
330 assert(f->min_zone < f->max_zone);
331 }
332 }
333
334 return true;
335}
336
337static bool zbd_verify_bs(void)
338{
339 struct thread_data *td;
340 struct fio_file *f;
341 uint32_t zone_size;
342 int i, j, k;
343
344 for_each_td(td, i) {
345 for_each_file(td, f, j) {
346 if (!f->zbd_info)
347 continue;
348 zone_size = f->zbd_info->zone_size;
349 for (k = 0; k < FIO_ARRAY_SIZE(td->o.bs); k++) {
350 if (td->o.verify != VERIFY_NONE &&
351 zone_size % td->o.bs[k] != 0) {
352 log_info("%s: block size %llu is not a divisor of the zone size %d\n",
353 f->file_name, td->o.bs[k],
354 zone_size);
355 return false;
356 }
357 }
358 }
359 }
360 return true;
361}
362
363static int ilog2(uint64_t i)
364{
365 int log = -1;
366
367 while (i) {
368 i >>= 1;
369 log++;
370 }
371 return log;
372}
373
374/*
375 * Initialize f->zbd_info for devices that are not zoned block devices. This
376 * allows to execute a ZBD workload against a non-ZBD device.
377 */
378static int init_zone_info(struct thread_data *td, struct fio_file *f)
379{
380 uint32_t nr_zones;
381 struct fio_zone_info *p;
382 uint64_t zone_size = td->o.zone_size;
383 uint64_t zone_capacity = td->o.zone_capacity;
384 struct zoned_block_device_info *zbd_info = NULL;
385 int i;
386
387 if (zone_size == 0) {
388 log_err("%s: Specifying the zone size is mandatory for regular block devices with --zonemode=zbd\n\n",
389 f->file_name);
390 return 1;
391 }
392
393 if (zone_size < 512) {
394 log_err("%s: zone size must be at least 512 bytes for --zonemode=zbd\n\n",
395 f->file_name);
396 return 1;
397 }
398
399 if (zone_capacity == 0)
400 zone_capacity = zone_size;
401
402 if (zone_capacity > zone_size) {
403 log_err("%s: job parameter zonecapacity %llu is larger than zone size %llu\n",
404 f->file_name, (unsigned long long) td->o.zone_capacity,
405 (unsigned long long) td->o.zone_size);
406 return 1;
407 }
408
409 nr_zones = (f->real_file_size + zone_size - 1) / zone_size;
410 zbd_info = scalloc(1, sizeof(*zbd_info) +
411 (nr_zones + 1) * sizeof(zbd_info->zone_info[0]));
412 if (!zbd_info)
413 return -ENOMEM;
414
415 mutex_init_pshared(&zbd_info->mutex);
416 zbd_info->refcount = 1;
417 p = &zbd_info->zone_info[0];
418 for (i = 0; i < nr_zones; i++, p++) {
419 mutex_init_pshared_with_type(&p->mutex,
420 PTHREAD_MUTEX_RECURSIVE);
421 p->start = i * zone_size;
422 p->wp = p->start;
423 p->type = ZBD_ZONE_TYPE_SWR;
424 p->cond = ZBD_ZONE_COND_EMPTY;
425 p->capacity = zone_capacity;
426 p->has_wp = 1;
427 }
428 /* a sentinel */
429 p->start = nr_zones * zone_size;
430
431 f->zbd_info = zbd_info;
432 f->zbd_info->zone_size = zone_size;
433 f->zbd_info->zone_size_log2 = is_power_of_2(zone_size) ?
434 ilog2(zone_size) : 0;
435 f->zbd_info->nr_zones = nr_zones;
436 return 0;
437}
438
439/*
440 * Maximum number of zones to report in one operation.
441 */
442#define ZBD_REPORT_MAX_ZONES 8192U
443
444/*
445 * Parse the device zone report and store it in f->zbd_info. Must be called
446 * only for devices that are zoned, namely those with a model != ZBD_NONE.
447 */
448static int parse_zone_info(struct thread_data *td, struct fio_file *f)
449{
450 int nr_zones, nrz;
451 struct zbd_zone *zones, *z;
452 struct fio_zone_info *p;
453 uint64_t zone_size, offset;
454 struct zoned_block_device_info *zbd_info = NULL;
455 int i, j, ret = -ENOMEM;
456
457 zones = calloc(ZBD_REPORT_MAX_ZONES, sizeof(struct zbd_zone));
458 if (!zones)
459 goto out;
460
461 nrz = zbd_report_zones(td, f, 0, zones, ZBD_REPORT_MAX_ZONES);
462 if (nrz < 0) {
463 ret = nrz;
464 log_info("fio: report zones (offset 0) failed for %s (%d).\n",
465 f->file_name, -ret);
466 goto out;
467 }
468
469 zone_size = zones[0].len;
470 nr_zones = (f->real_file_size + zone_size - 1) / zone_size;
471
472 if (td->o.zone_size == 0) {
473 td->o.zone_size = zone_size;
474 } else if (td->o.zone_size != zone_size) {
475 log_err("fio: %s job parameter zonesize %llu does not match disk zone size %llu.\n",
476 f->file_name, (unsigned long long) td->o.zone_size,
477 (unsigned long long) zone_size);
478 ret = -EINVAL;
479 goto out;
480 }
481
482 dprint(FD_ZBD, "Device %s has %d zones of size %llu KB\n", f->file_name,
483 nr_zones, (unsigned long long) zone_size / 1024);
484
485 zbd_info = scalloc(1, sizeof(*zbd_info) +
486 (nr_zones + 1) * sizeof(zbd_info->zone_info[0]));
487 if (!zbd_info)
488 goto out;
489 mutex_init_pshared(&zbd_info->mutex);
490 zbd_info->refcount = 1;
491 p = &zbd_info->zone_info[0];
492 for (offset = 0, j = 0; j < nr_zones;) {
493 z = &zones[0];
494 for (i = 0; i < nrz; i++, j++, z++, p++) {
495 mutex_init_pshared_with_type(&p->mutex,
496 PTHREAD_MUTEX_RECURSIVE);
497 p->start = z->start;
498 p->capacity = z->capacity;
499 switch (z->cond) {
500 case ZBD_ZONE_COND_NOT_WP:
501 case ZBD_ZONE_COND_FULL:
502 p->wp = p->start + p->capacity;
503 break;
504 default:
505 assert(z->start <= z->wp);
506 assert(z->wp <= z->start + zone_size);
507 p->wp = z->wp;
508 break;
509 }
510
511 switch (z->type) {
512 case ZBD_ZONE_TYPE_SWR:
513 p->has_wp = 1;
514 break;
515 default:
516 p->has_wp = 0;
517 }
518 p->type = z->type;
519 p->cond = z->cond;
520
521 if (j > 0 && p->start != p[-1].start + zone_size) {
522 log_info("%s: invalid zone data\n",
523 f->file_name);
524 ret = -EINVAL;
525 goto out;
526 }
527 }
528 z--;
529 offset = z->start + z->len;
530 if (j >= nr_zones)
531 break;
532 nrz = zbd_report_zones(td, f, offset, zones,
533 min((uint32_t)(nr_zones - j),
534 ZBD_REPORT_MAX_ZONES));
535 if (nrz < 0) {
536 ret = nrz;
537 log_info("fio: report zones (offset %llu) failed for %s (%d).\n",
538 (unsigned long long)offset,
539 f->file_name, -ret);
540 goto out;
541 }
542 }
543
544 /* a sentinel */
545 zbd_info->zone_info[nr_zones].start = offset;
546
547 f->zbd_info = zbd_info;
548 f->zbd_info->zone_size = zone_size;
549 f->zbd_info->zone_size_log2 = is_power_of_2(zone_size) ?
550 ilog2(zone_size) : 0;
551 f->zbd_info->nr_zones = nr_zones;
552 zbd_info = NULL;
553 ret = 0;
554
555out:
556 sfree(zbd_info);
557 free(zones);
558 return ret;
559}
560
561/*
562 * Allocate zone information and store it into f->zbd_info if zonemode=zbd.
563 *
564 * Returns 0 upon success and a negative error code upon failure.
565 */
566static int zbd_create_zone_info(struct thread_data *td, struct fio_file *f)
567{
568 enum zbd_zoned_model zbd_model;
569 int ret;
570
571 assert(td->o.zone_mode == ZONE_MODE_ZBD);
572
573 ret = zbd_get_zoned_model(td, f, &zbd_model);
574 if (ret)
575 return ret;
576
577 switch (zbd_model) {
578 case ZBD_IGNORE:
579 return 0;
580 case ZBD_HOST_AWARE:
581 case ZBD_HOST_MANAGED:
582 ret = parse_zone_info(td, f);
583 break;
584 case ZBD_NONE:
585 ret = init_zone_info(td, f);
586 break;
587 default:
588 td_verror(td, EINVAL, "Unsupported zoned model");
589 log_err("Unsupported zoned model\n");
590 return -EINVAL;
591 }
592
593 if (ret == 0) {
594 f->zbd_info->model = zbd_model;
595 f->zbd_info->max_open_zones = td->o.max_open_zones;
596 }
597 return ret;
598}
599
600void zbd_free_zone_info(struct fio_file *f)
601{
602 uint32_t refcount;
603
604 assert(f->zbd_info);
605
606 pthread_mutex_lock(&f->zbd_info->mutex);
607 refcount = --f->zbd_info->refcount;
608 pthread_mutex_unlock(&f->zbd_info->mutex);
609
610 assert((int32_t)refcount >= 0);
611 if (refcount == 0)
612 sfree(f->zbd_info);
613 f->zbd_info = NULL;
614}
615
616/*
617 * Initialize f->zbd_info.
618 *
619 * Returns 0 upon success and a negative error code upon failure.
620 *
621 * Note: this function can only work correctly if it is called before the first
622 * fio fork() call.
623 */
624static int zbd_init_zone_info(struct thread_data *td, struct fio_file *file)
625{
626 struct thread_data *td2;
627 struct fio_file *f2;
628 int i, j, ret;
629
630 for_each_td(td2, i) {
631 for_each_file(td2, f2, j) {
632 if (td2 == td && f2 == file)
633 continue;
634 if (!f2->zbd_info ||
635 strcmp(f2->file_name, file->file_name) != 0)
636 continue;
637 file->zbd_info = f2->zbd_info;
638 file->zbd_info->refcount++;
639 return 0;
640 }
641 }
642
643 ret = zbd_create_zone_info(td, file);
644 if (ret < 0)
645 td_verror(td, -ret, "zbd_create_zone_info() failed");
646 return ret;
647}
648
649static bool zbd_open_zone(struct thread_data *td, const struct fio_file *f,
650 uint32_t zone_idx);
651static int zbd_reset_zone(struct thread_data *td, struct fio_file *f,
652 struct fio_zone_info *z);
653
654int zbd_setup_files(struct thread_data *td)
655{
656 struct fio_file *f;
657 int i;
658
659 for_each_file(td, f, i) {
660 if (zbd_init_zone_info(td, f))
661 return 1;
662 }
663
664 if (!zbd_using_direct_io()) {
665 log_err("Using direct I/O is mandatory for writing to ZBD drives\n\n");
666 return 1;
667 }
668
669 if (!zbd_verify_sizes())
670 return 1;
671
672 if (!zbd_verify_bs())
673 return 1;
674
675 for_each_file(td, f, i) {
676 struct zoned_block_device_info *zbd = f->zbd_info;
677 struct fio_zone_info *z;
678 int zi;
679
680 if (!zbd)
681 continue;
682
683 zbd->max_open_zones = zbd->max_open_zones ?: ZBD_MAX_OPEN_ZONES;
684
685 if (td->o.max_open_zones > 0 &&
686 zbd->max_open_zones != td->o.max_open_zones) {
687 log_err("Different 'max_open_zones' values\n");
688 return 1;
689 }
690 if (zbd->max_open_zones > ZBD_MAX_OPEN_ZONES) {
691 log_err("'max_open_zones' value is limited by %u\n", ZBD_MAX_OPEN_ZONES);
692 return 1;
693 }
694
695 for (zi = f->min_zone; zi < f->max_zone; zi++) {
696 z = &zbd->zone_info[zi];
697 if (z->cond != ZBD_ZONE_COND_IMP_OPEN &&
698 z->cond != ZBD_ZONE_COND_EXP_OPEN)
699 continue;
700 if (zbd_open_zone(td, f, zi))
701 continue;
702 /*
703 * If the number of open zones exceeds specified limits,
704 * reset all extra open zones.
705 */
706 if (zbd_reset_zone(td, f, z) < 0) {
707 log_err("Failed to reest zone %d\n", zi);
708 return 1;
709 }
710 }
711 }
712
713 return 0;
714}
715
716static inline unsigned int zbd_zone_nr(const struct fio_file *f,
717 struct fio_zone_info *zone)
718{
719 return zone - f->zbd_info->zone_info;
720}
721
722/**
723 * zbd_reset_zone - reset the write pointer of a single zone
724 * @td: FIO thread data.
725 * @f: FIO file associated with the disk for which to reset a write pointer.
726 * @z: Zone to reset.
727 *
728 * Returns 0 upon success and a negative error code upon failure.
729 *
730 * The caller must hold z->mutex.
731 */
732static int zbd_reset_zone(struct thread_data *td, struct fio_file *f,
733 struct fio_zone_info *z)
734{
735 uint64_t offset = z->start;
736 uint64_t length = (z+1)->start - offset;
737 uint64_t data_in_zone = z->wp - z->start;
738 int ret = 0;
739
740 if (!data_in_zone)
741 return 0;
742
743 assert(is_valid_offset(f, offset + length - 1));
744
745 dprint(FD_ZBD, "%s: resetting wp of zone %u.\n", f->file_name,
746 zbd_zone_nr(f, z));
747 switch (f->zbd_info->model) {
748 case ZBD_HOST_AWARE:
749 case ZBD_HOST_MANAGED:
750 ret = zbd_reset_wp(td, f, offset, length);
751 if (ret < 0)
752 return ret;
753 break;
754 default:
755 break;
756 }
757
758 pthread_mutex_lock(&f->zbd_info->mutex);
759 f->zbd_info->sectors_with_data -= data_in_zone;
760 f->zbd_info->wp_sectors_with_data -= data_in_zone;
761 pthread_mutex_unlock(&f->zbd_info->mutex);
762 z->wp = z->start;
763 z->verify_block = 0;
764
765 td->ts.nr_zone_resets++;
766
767 return ret;
768}
769
770/* The caller must hold f->zbd_info->mutex */
771static void zbd_close_zone(struct thread_data *td, const struct fio_file *f,
772 unsigned int zone_idx)
773{
774 uint32_t open_zone_idx = 0;
775
776 for (; open_zone_idx < f->zbd_info->num_open_zones; open_zone_idx++) {
777 if (f->zbd_info->open_zones[open_zone_idx] == zone_idx)
778 break;
779 }
780 if (open_zone_idx == f->zbd_info->num_open_zones) {
781 dprint(FD_ZBD, "%s: zone %d is not open\n",
782 f->file_name, zone_idx);
783 return;
784 }
785
786 dprint(FD_ZBD, "%s: closing zone %d\n", f->file_name, zone_idx);
787 memmove(f->zbd_info->open_zones + open_zone_idx,
788 f->zbd_info->open_zones + open_zone_idx + 1,
789 (ZBD_MAX_OPEN_ZONES - (open_zone_idx + 1)) *
790 sizeof(f->zbd_info->open_zones[0]));
791 f->zbd_info->num_open_zones--;
792 td->num_open_zones--;
793 get_zone(f, zone_idx)->open = 0;
794}
795
796/*
797 * Reset a range of zones. Returns 0 upon success and 1 upon failure.
798 * @td: fio thread data.
799 * @f: fio file for which to reset zones
800 * @zb: first zone to reset.
801 * @ze: first zone not to reset.
802 * @all_zones: whether to reset all zones or only those zones for which the
803 * write pointer is not a multiple of td->o.min_bs[DDIR_WRITE].
804 */
805static int zbd_reset_zones(struct thread_data *td, struct fio_file *f,
806 struct fio_zone_info *const zb,
807 struct fio_zone_info *const ze, bool all_zones)
808{
809 struct fio_zone_info *z;
810 const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
811 bool reset_wp;
812 int res = 0;
813
814 assert(min_bs);
815
816 dprint(FD_ZBD, "%s: examining zones %u .. %u\n", f->file_name,
817 zbd_zone_nr(f, zb), zbd_zone_nr(f, ze));
818 for (z = zb; z < ze; z++) {
819 uint32_t nz = zbd_zone_nr(f, z);
820
821 if (!z->has_wp)
822 continue;
823 zone_lock(td, f, z);
824 if (all_zones) {
825 pthread_mutex_lock(&f->zbd_info->mutex);
826 zbd_close_zone(td, f, nz);
827 pthread_mutex_unlock(&f->zbd_info->mutex);
828
829 reset_wp = z->wp != z->start;
830 } else {
831 reset_wp = z->wp % min_bs != 0;
832 }
833 if (reset_wp) {
834 dprint(FD_ZBD, "%s: resetting zone %u\n",
835 f->file_name, zbd_zone_nr(f, z));
836 if (zbd_reset_zone(td, f, z) < 0)
837 res = 1;
838 }
839 zone_unlock(z);
840 }
841
842 return res;
843}
844
845/*
846 * Reset zbd_info.write_cnt, the counter that counts down towards the next
847 * zone reset.
848 */
849static void _zbd_reset_write_cnt(const struct thread_data *td,
850 const struct fio_file *f)
851{
852 assert(0 <= td->o.zrf.u.f && td->o.zrf.u.f <= 1);
853
854 f->zbd_info->write_cnt = td->o.zrf.u.f ?
855 min(1.0 / td->o.zrf.u.f, 0.0 + UINT_MAX) : UINT_MAX;
856}
857
858static void zbd_reset_write_cnt(const struct thread_data *td,
859 const struct fio_file *f)
860{
861 pthread_mutex_lock(&f->zbd_info->mutex);
862 _zbd_reset_write_cnt(td, f);
863 pthread_mutex_unlock(&f->zbd_info->mutex);
864}
865
866static bool zbd_dec_and_reset_write_cnt(const struct thread_data *td,
867 const struct fio_file *f)
868{
869 uint32_t write_cnt = 0;
870
871 pthread_mutex_lock(&f->zbd_info->mutex);
872 assert(f->zbd_info->write_cnt);
873 if (f->zbd_info->write_cnt)
874 write_cnt = --f->zbd_info->write_cnt;
875 if (write_cnt == 0)
876 _zbd_reset_write_cnt(td, f);
877 pthread_mutex_unlock(&f->zbd_info->mutex);
878
879 return write_cnt == 0;
880}
881
882enum swd_action {
883 CHECK_SWD,
884 SET_SWD,
885};
886
887/* Calculate the number of sectors with data (swd) and perform action 'a' */
888static uint64_t zbd_process_swd(const struct fio_file *f, enum swd_action a)
889{
890 struct fio_zone_info *zb, *ze, *z;
891 uint64_t swd = 0;
892 uint64_t wp_swd = 0;
893
894 zb = get_zone(f, f->min_zone);
895 ze = get_zone(f, f->max_zone);
896 for (z = zb; z < ze; z++) {
897 if (z->has_wp) {
898 pthread_mutex_lock(&z->mutex);
899 wp_swd += z->wp - z->start;
900 }
901 swd += z->wp - z->start;
902 }
903 pthread_mutex_lock(&f->zbd_info->mutex);
904 switch (a) {
905 case CHECK_SWD:
906 assert(f->zbd_info->sectors_with_data == swd);
907 assert(f->zbd_info->wp_sectors_with_data == wp_swd);
908 break;
909 case SET_SWD:
910 f->zbd_info->sectors_with_data = swd;
911 f->zbd_info->wp_sectors_with_data = wp_swd;
912 break;
913 }
914 pthread_mutex_unlock(&f->zbd_info->mutex);
915 for (z = zb; z < ze; z++)
916 if (z->has_wp)
917 zone_unlock(z);
918
919 return swd;
920}
921
922/*
923 * The swd check is useful for debugging but takes too much time to leave
924 * it enabled all the time. Hence it is disabled by default.
925 */
926static const bool enable_check_swd = false;
927
928/* Check whether the values of zbd_info.*sectors_with_data are correct. */
929static void zbd_check_swd(const struct fio_file *f)
930{
931 if (!enable_check_swd)
932 return;
933
934 zbd_process_swd(f, CHECK_SWD);
935}
936
937static void zbd_init_swd(struct fio_file *f)
938{
939 uint64_t swd;
940
941 if (!enable_check_swd)
942 return;
943
944 swd = zbd_process_swd(f, SET_SWD);
945 dprint(FD_ZBD, "%s(%s): swd = %" PRIu64 "\n", __func__, f->file_name,
946 swd);
947}
948
949void zbd_file_reset(struct thread_data *td, struct fio_file *f)
950{
951 struct fio_zone_info *zb, *ze;
952
953 if (!f->zbd_info || !td_write(td))
954 return;
955
956 zb = get_zone(f, f->min_zone);
957 ze = get_zone(f, f->max_zone);
958 zbd_init_swd(f);
959 /*
960 * If data verification is enabled reset the affected zones before
961 * writing any data to avoid that a zone reset has to be issued while
962 * writing data, which causes data loss.
963 */
964 zbd_reset_zones(td, f, zb, ze, td->o.verify != VERIFY_NONE &&
965 td->runstate != TD_VERIFYING);
966 zbd_reset_write_cnt(td, f);
967}
968
969/* The caller must hold f->zbd_info->mutex. */
970static bool is_zone_open(const struct thread_data *td, const struct fio_file *f,
971 unsigned int zone_idx)
972{
973 struct zoned_block_device_info *zbdi = f->zbd_info;
974 int i;
975
976 assert(td->o.job_max_open_zones == 0 || td->num_open_zones <= td->o.job_max_open_zones);
977 assert(td->o.job_max_open_zones <= zbdi->max_open_zones);
978 assert(zbdi->num_open_zones <= zbdi->max_open_zones);
979
980 for (i = 0; i < zbdi->num_open_zones; i++)
981 if (zbdi->open_zones[i] == zone_idx)
982 return true;
983
984 return false;
985}
986
987/*
988 * Open a ZBD zone if it was not yet open. Returns true if either the zone was
989 * already open or if opening a new zone is allowed. Returns false if the zone
990 * was not yet open and opening a new zone would cause the zone limit to be
991 * exceeded.
992 */
993static bool zbd_open_zone(struct thread_data *td, const struct fio_file *f,
994 uint32_t zone_idx)
995{
996 const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
997 struct fio_zone_info *z = get_zone(f, zone_idx);
998 bool res = true;
999
1000 if (z->cond == ZBD_ZONE_COND_OFFLINE)
1001 return false;
1002
1003 /*
1004 * Skip full zones with data verification enabled because resetting a
1005 * zone causes data loss and hence causes verification to fail.
1006 */
1007 if (td->o.verify != VERIFY_NONE && zbd_zone_full(f, z, min_bs))
1008 return false;
1009
1010 pthread_mutex_lock(&f->zbd_info->mutex);
1011 if (is_zone_open(td, f, zone_idx)) {
1012 /*
1013 * If the zone is already open and going to be full by writes
1014 * in-flight, handle it as a full zone instead of an open zone.
1015 */
1016 if (z->wp >= zbd_zone_capacity_end(z))
1017 res = false;
1018 goto out;
1019 }
1020 res = false;
1021 /* Zero means no limit */
1022 if (td->o.job_max_open_zones > 0 &&
1023 td->num_open_zones >= td->o.job_max_open_zones)
1024 goto out;
1025 if (f->zbd_info->num_open_zones >= f->zbd_info->max_open_zones)
1026 goto out;
1027 dprint(FD_ZBD, "%s: opening zone %d\n", f->file_name, zone_idx);
1028 f->zbd_info->open_zones[f->zbd_info->num_open_zones++] = zone_idx;
1029 td->num_open_zones++;
1030 z->open = 1;
1031 res = true;
1032
1033out:
1034 pthread_mutex_unlock(&f->zbd_info->mutex);
1035 return res;
1036}
1037
1038/* Anything goes as long as it is not a constant. */
1039static uint32_t pick_random_zone_idx(const struct fio_file *f,
1040 const struct io_u *io_u)
1041{
1042 return io_u->offset * f->zbd_info->num_open_zones / f->real_file_size;
1043}
1044
1045/*
1046 * Modify the offset of an I/O unit that does not refer to an open zone such
1047 * that it refers to an open zone. Close an open zone and open a new zone if
1048 * necessary. The open zone is searched across sequential zones.
1049 * This algorithm can only work correctly if all write pointers are
1050 * a multiple of the fio block size. The caller must neither hold z->mutex
1051 * nor f->zbd_info->mutex. Returns with z->mutex held upon success.
1052 */
1053static struct fio_zone_info *zbd_convert_to_open_zone(struct thread_data *td,
1054 struct io_u *io_u)
1055{
1056 const uint32_t min_bs = td->o.min_bs[io_u->ddir];
1057 struct fio_file *f = io_u->file;
1058 struct fio_zone_info *z;
1059 unsigned int open_zone_idx = -1;
1060 uint32_t zone_idx, new_zone_idx;
1061 int i;
1062 bool wait_zone_close;
1063
1064 assert(is_valid_offset(f, io_u->offset));
1065
1066 if (td->o.max_open_zones || td->o.job_max_open_zones) {
1067 /*
1068 * This statement accesses f->zbd_info->open_zones[] on purpose
1069 * without locking.
1070 */
1071 zone_idx = f->zbd_info->open_zones[pick_random_zone_idx(f, io_u)];
1072 } else {
1073 zone_idx = zbd_zone_idx(f, io_u->offset);
1074 }
1075 if (zone_idx < f->min_zone)
1076 zone_idx = f->min_zone;
1077 else if (zone_idx >= f->max_zone)
1078 zone_idx = f->max_zone - 1;
1079 dprint(FD_ZBD, "%s(%s): starting from zone %d (offset %lld, buflen %lld)\n",
1080 __func__, f->file_name, zone_idx, io_u->offset, io_u->buflen);
1081
1082 /*
1083 * Since z->mutex is the outer lock and f->zbd_info->mutex the inner
1084 * lock it can happen that the state of the zone with index zone_idx
1085 * has changed after 'z' has been assigned and before f->zbd_info->mutex
1086 * has been obtained. Hence the loop.
1087 */
1088 for (;;) {
1089 uint32_t tmp_idx;
1090
1091 z = get_zone(f, zone_idx);
1092
1093 zone_lock(td, f, z);
1094 pthread_mutex_lock(&f->zbd_info->mutex);
1095 if (z->cond != ZBD_ZONE_COND_OFFLINE &&
1096 td->o.max_open_zones == 0 && td->o.job_max_open_zones == 0)
1097 goto examine_zone;
1098 if (f->zbd_info->num_open_zones == 0) {
1099 dprint(FD_ZBD, "%s(%s): no zones are open\n",
1100 __func__, f->file_name);
1101 goto open_other_zone;
1102 }
1103
1104 /*
1105 * List of opened zones is per-device, shared across all threads.
1106 * Start with quasi-random candidate zone.
1107 * Ignore zones which don't belong to thread's offset/size area.
1108 */
1109 open_zone_idx = pick_random_zone_idx(f, io_u);
1110 assert(open_zone_idx < f->zbd_info->num_open_zones);
1111 tmp_idx = open_zone_idx;
1112 for (i = 0; i < f->zbd_info->num_open_zones; i++) {
1113 uint32_t tmpz;
1114
1115 if (tmp_idx >= f->zbd_info->num_open_zones)
1116 tmp_idx = 0;
1117 tmpz = f->zbd_info->open_zones[tmp_idx];
1118 if (f->min_zone <= tmpz && tmpz < f->max_zone) {
1119 open_zone_idx = tmp_idx;
1120 goto found_candidate_zone;
1121 }
1122
1123 tmp_idx++;
1124 }
1125
1126 dprint(FD_ZBD, "%s(%s): no candidate zone\n",
1127 __func__, f->file_name);
1128 pthread_mutex_unlock(&f->zbd_info->mutex);
1129 zone_unlock(z);
1130 return NULL;
1131
1132found_candidate_zone:
1133 new_zone_idx = f->zbd_info->open_zones[open_zone_idx];
1134 if (new_zone_idx == zone_idx)
1135 break;
1136 zone_idx = new_zone_idx;
1137 pthread_mutex_unlock(&f->zbd_info->mutex);
1138 zone_unlock(z);
1139 }
1140
1141 /* Both z->mutex and f->zbd_info->mutex are held. */
1142
1143examine_zone:
1144 if (z->wp + min_bs <= zbd_zone_capacity_end(z)) {
1145 pthread_mutex_unlock(&f->zbd_info->mutex);
1146 goto out;
1147 }
1148
1149open_other_zone:
1150 /* Check if number of open zones reaches one of limits. */
1151 wait_zone_close =
1152 f->zbd_info->num_open_zones == f->max_zone - f->min_zone ||
1153 (td->o.max_open_zones &&
1154 f->zbd_info->num_open_zones == td->o.max_open_zones) ||
1155 (td->o.job_max_open_zones &&
1156 td->num_open_zones == td->o.job_max_open_zones);
1157
1158 pthread_mutex_unlock(&f->zbd_info->mutex);
1159
1160 /* Only z->mutex is held. */
1161
1162 /*
1163 * When number of open zones reaches to one of limits, wait for
1164 * zone close before opening a new zone.
1165 */
1166 if (wait_zone_close) {
1167 dprint(FD_ZBD, "%s(%s): quiesce to allow open zones to close\n",
1168 __func__, f->file_name);
1169 io_u_quiesce(td);
1170 }
1171
1172 /* Zone 'z' is full, so try to open a new zone. */
1173 for (i = f->io_size / f->zbd_info->zone_size; i > 0; i--) {
1174 zone_idx++;
1175 if (z->has_wp)
1176 zone_unlock(z);
1177 z++;
1178 if (!is_valid_offset(f, z->start)) {
1179 /* Wrap-around. */
1180 zone_idx = f->min_zone;
1181 z = get_zone(f, zone_idx);
1182 }
1183 assert(is_valid_offset(f, z->start));
1184 if (!z->has_wp)
1185 continue;
1186 zone_lock(td, f, z);
1187 if (z->open)
1188 continue;
1189 if (zbd_open_zone(td, f, zone_idx))
1190 goto out;
1191 }
1192
1193 /* Only z->mutex is held. */
1194
1195 /* Check whether the write fits in any of the already opened zones. */
1196 pthread_mutex_lock(&f->zbd_info->mutex);
1197 for (i = 0; i < f->zbd_info->num_open_zones; i++) {
1198 zone_idx = f->zbd_info->open_zones[i];
1199 if (zone_idx < f->min_zone || zone_idx >= f->max_zone)
1200 continue;
1201 pthread_mutex_unlock(&f->zbd_info->mutex);
1202 zone_unlock(z);
1203
1204 z = get_zone(f, zone_idx);
1205
1206 zone_lock(td, f, z);
1207 if (z->wp + min_bs <= zbd_zone_capacity_end(z))
1208 goto out;
1209 pthread_mutex_lock(&f->zbd_info->mutex);
1210 }
1211 pthread_mutex_unlock(&f->zbd_info->mutex);
1212 zone_unlock(z);
1213 dprint(FD_ZBD, "%s(%s): did not open another zone\n", __func__,
1214 f->file_name);
1215 return NULL;
1216
1217out:
1218 dprint(FD_ZBD, "%s(%s): returning zone %d\n", __func__, f->file_name,
1219 zone_idx);
1220 io_u->offset = z->start;
1221 assert(z->has_wp);
1222 assert(z->cond != ZBD_ZONE_COND_OFFLINE);
1223 return z;
1224}
1225
1226/* The caller must hold z->mutex. */
1227static struct fio_zone_info *zbd_replay_write_order(struct thread_data *td,
1228 struct io_u *io_u,
1229 struct fio_zone_info *z)
1230{
1231 const struct fio_file *f = io_u->file;
1232 const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
1233
1234 if (!zbd_open_zone(td, f, zbd_zone_nr(f, z))) {
1235 zone_unlock(z);
1236 z = zbd_convert_to_open_zone(td, io_u);
1237 assert(z);
1238 }
1239
1240 if (z->verify_block * min_bs >= z->capacity)
1241 log_err("%s: %d * %d >= %llu\n", f->file_name, z->verify_block,
1242 min_bs, (unsigned long long)z->capacity);
1243 io_u->offset = z->start + z->verify_block++ * min_bs;
1244 return z;
1245}
1246
1247/*
1248 * Find another zone for which @io_u fits in the readable data in the zone.
1249 * Search in zones @zb + 1 .. @zl. For random workload, also search in zones
1250 * @zb - 1 .. @zf.
1251 *
1252 * Either returns NULL or returns a zone pointer. When the zone has write
1253 * pointer, hold the mutex for the zone.
1254 */
1255static struct fio_zone_info *
1256zbd_find_zone(struct thread_data *td, struct io_u *io_u,
1257 struct fio_zone_info *zb, struct fio_zone_info *zl)
1258{
1259 const uint32_t min_bs = td->o.min_bs[io_u->ddir];
1260 struct fio_file *f = io_u->file;
1261 struct fio_zone_info *z1, *z2;
1262 const struct fio_zone_info *const zf = get_zone(f, f->min_zone);
1263
1264 /*
1265 * Skip to the next non-empty zone in case of sequential I/O and to
1266 * the nearest non-empty zone in case of random I/O.
1267 */
1268 for (z1 = zb + 1, z2 = zb - 1; z1 < zl || z2 >= zf; z1++, z2--) {
1269 if (z1 < zl && z1->cond != ZBD_ZONE_COND_OFFLINE) {
1270 if (z1->has_wp)
1271 zone_lock(td, f, z1);
1272 if (z1->start + min_bs <= z1->wp)
1273 return z1;
1274 if (z1->has_wp)
1275 zone_unlock(z1);
1276 } else if (!td_random(td)) {
1277 break;
1278 }
1279 if (td_random(td) && z2 >= zf &&
1280 z2->cond != ZBD_ZONE_COND_OFFLINE) {
1281 if (z2->has_wp)
1282 zone_lock(td, f, z2);
1283 if (z2->start + min_bs <= z2->wp)
1284 return z2;
1285 if (z2->has_wp)
1286 zone_unlock(z2);
1287 }
1288 }
1289 dprint(FD_ZBD, "%s: adjusting random read offset failed\n",
1290 f->file_name);
1291 return NULL;
1292}
1293
1294/**
1295 * zbd_end_zone_io - update zone status at command completion
1296 * @io_u: I/O unit
1297 * @z: zone info pointer
1298 *
1299 * If the write command made the zone full, close it.
1300 *
1301 * The caller must hold z->mutex.
1302 */
1303static void zbd_end_zone_io(struct thread_data *td, const struct io_u *io_u,
1304 struct fio_zone_info *z)
1305{
1306 const struct fio_file *f = io_u->file;
1307
1308 if (io_u->ddir == DDIR_WRITE &&
1309 io_u->offset + io_u->buflen >= zbd_zone_capacity_end(z)) {
1310 pthread_mutex_lock(&f->zbd_info->mutex);
1311 zbd_close_zone(td, f, zbd_zone_nr(f, z));
1312 pthread_mutex_unlock(&f->zbd_info->mutex);
1313 }
1314}
1315
1316/**
1317 * zbd_queue_io - update the write pointer of a sequential zone
1318 * @io_u: I/O unit
1319 * @success: Whether or not the I/O unit has been queued successfully
1320 * @q: queueing status (busy, completed or queued).
1321 *
1322 * For write and trim operations, update the write pointer of the I/O unit
1323 * target zone.
1324 */
1325static void zbd_queue_io(struct thread_data *td, struct io_u *io_u, int q,
1326 bool success)
1327{
1328 const struct fio_file *f = io_u->file;
1329 struct zoned_block_device_info *zbd_info = f->zbd_info;
1330 struct fio_zone_info *z;
1331 uint32_t zone_idx;
1332 uint64_t zone_end;
1333
1334 if (!zbd_info)
1335 return;
1336
1337 zone_idx = zbd_zone_idx(f, io_u->offset);
1338 assert(zone_idx < zbd_info->nr_zones);
1339 z = get_zone(f, zone_idx);
1340
1341 assert(z->has_wp);
1342
1343 if (!success)
1344 goto unlock;
1345
1346 dprint(FD_ZBD,
1347 "%s: queued I/O (%lld, %llu) for zone %u\n",
1348 f->file_name, io_u->offset, io_u->buflen, zone_idx);
1349
1350 switch (io_u->ddir) {
1351 case DDIR_WRITE:
1352 zone_end = min((uint64_t)(io_u->offset + io_u->buflen),
1353 zbd_zone_capacity_end(z));
1354 pthread_mutex_lock(&zbd_info->mutex);
1355 /*
1356 * z->wp > zone_end means that one or more I/O errors
1357 * have occurred.
1358 */
1359 if (z->wp <= zone_end) {
1360 zbd_info->sectors_with_data += zone_end - z->wp;
1361 zbd_info->wp_sectors_with_data += zone_end - z->wp;
1362 }
1363 pthread_mutex_unlock(&zbd_info->mutex);
1364 z->wp = zone_end;
1365 break;
1366 case DDIR_TRIM:
1367 assert(z->wp == z->start);
1368 break;
1369 default:
1370 break;
1371 }
1372
1373 if (q == FIO_Q_COMPLETED && !io_u->error)
1374 zbd_end_zone_io(td, io_u, z);
1375
1376unlock:
1377 if (!success || q != FIO_Q_QUEUED) {
1378 /* BUSY or COMPLETED: unlock the zone */
1379 zone_unlock(z);
1380 io_u->zbd_put_io = NULL;
1381 }
1382}
1383
1384/**
1385 * zbd_put_io - Unlock an I/O unit target zone lock
1386 * @io_u: I/O unit
1387 */
1388static void zbd_put_io(struct thread_data *td, const struct io_u *io_u)
1389{
1390 const struct fio_file *f = io_u->file;
1391 struct zoned_block_device_info *zbd_info = f->zbd_info;
1392 struct fio_zone_info *z;
1393 uint32_t zone_idx;
1394
1395 if (!zbd_info)
1396 return;
1397
1398 zone_idx = zbd_zone_idx(f, io_u->offset);
1399 assert(zone_idx < zbd_info->nr_zones);
1400 z = get_zone(f, zone_idx);
1401
1402 assert(z->has_wp);
1403
1404 dprint(FD_ZBD,
1405 "%s: terminate I/O (%lld, %llu) for zone %u\n",
1406 f->file_name, io_u->offset, io_u->buflen, zone_idx);
1407
1408 zbd_end_zone_io(td, io_u, z);
1409
1410 zone_unlock(z);
1411 zbd_check_swd(f);
1412}
1413
1414/*
1415 * Windows and MacOS do not define this.
1416 */
1417#ifndef EREMOTEIO
1418#define EREMOTEIO 121 /* POSIX value */
1419#endif
1420
1421bool zbd_unaligned_write(int error_code)
1422{
1423 switch (error_code) {
1424 case EIO:
1425 case EREMOTEIO:
1426 return true;
1427 }
1428 return false;
1429}
1430
1431/**
1432 * setup_zbd_zone_mode - handle zoneskip as necessary for ZBD drives
1433 * @td: FIO thread data.
1434 * @io_u: FIO I/O unit.
1435 *
1436 * For sequential workloads, change the file offset to skip zoneskip bytes when
1437 * no more IO can be performed in the current zone.
1438 * - For read workloads, zoneskip is applied when the io has reached the end of
1439 * the zone or the zone write position (when td->o.read_beyond_wp is false).
1440 * - For write workloads, zoneskip is applied when the zone is full.
1441 * This applies only to read and write operations.
1442 */
1443void setup_zbd_zone_mode(struct thread_data *td, struct io_u *io_u)
1444{
1445 struct fio_file *f = io_u->file;
1446 enum fio_ddir ddir = io_u->ddir;
1447 struct fio_zone_info *z;
1448 uint32_t zone_idx;
1449
1450 assert(td->o.zone_mode == ZONE_MODE_ZBD);
1451 assert(td->o.zone_size);
1452
1453 zone_idx = zbd_zone_idx(f, f->last_pos[ddir]);
1454 z = get_zone(f, zone_idx);
1455
1456 /*
1457 * When the zone capacity is smaller than the zone size and the I/O is
1458 * sequential write, skip to zone end if the latest position is at the
1459 * zone capacity limit.
1460 */
1461 if (z->capacity < f->zbd_info->zone_size && !td_random(td) &&
1462 ddir == DDIR_WRITE &&
1463 f->last_pos[ddir] >= zbd_zone_capacity_end(z)) {
1464 dprint(FD_ZBD,
1465 "%s: Jump from zone capacity limit to zone end:"
1466 " (%llu -> %llu) for zone %u (%llu)\n",
1467 f->file_name, (unsigned long long) f->last_pos[ddir],
1468 (unsigned long long) zbd_zone_end(z), zone_idx,
1469 (unsigned long long) z->capacity);
1470 td->io_skip_bytes += zbd_zone_end(z) - f->last_pos[ddir];
1471 f->last_pos[ddir] = zbd_zone_end(z);
1472 }
1473
1474 /*
1475 * zone_skip is valid only for sequential workloads.
1476 */
1477 if (td_random(td) || !td->o.zone_skip)
1478 return;
1479
1480 /*
1481 * It is time to switch to a new zone if:
1482 * - zone_bytes == zone_size bytes have already been accessed
1483 * - The last position reached the end of the current zone.
1484 * - For reads with td->o.read_beyond_wp == false, the last position
1485 * reached the zone write pointer.
1486 */
1487 if (td->zone_bytes >= td->o.zone_size ||
1488 f->last_pos[ddir] >= zbd_zone_end(z) ||
1489 (ddir == DDIR_READ &&
1490 (!td->o.read_beyond_wp) && f->last_pos[ddir] >= z->wp)) {
1491 /*
1492 * Skip zones.
1493 */
1494 td->zone_bytes = 0;
1495 f->file_offset += td->o.zone_size + td->o.zone_skip;
1496
1497 /*
1498 * Wrap from the beginning, if we exceed the file size
1499 */
1500 if (f->file_offset >= f->real_file_size)
1501 f->file_offset = get_start_offset(td, f);
1502
1503 f->last_pos[ddir] = f->file_offset;
1504 td->io_skip_bytes += td->o.zone_skip;
1505 }
1506}
1507
1508/**
1509 * zbd_adjust_ddir - Adjust an I/O direction for zonemode=zbd.
1510 *
1511 * @td: FIO thread data.
1512 * @io_u: FIO I/O unit.
1513 * @ddir: I/O direction before adjustment.
1514 *
1515 * Return adjusted I/O direction.
1516 */
1517enum fio_ddir zbd_adjust_ddir(struct thread_data *td, struct io_u *io_u,
1518 enum fio_ddir ddir)
1519{
1520 /*
1521 * In case read direction is chosen for the first random I/O, fio with
1522 * zonemode=zbd stops because no data can be read from zoned block
1523 * devices with all empty zones. Overwrite the first I/O direction as
1524 * write to make sure data to read exists.
1525 */
1526 if (ddir != DDIR_READ || !td_rw(td))
1527 return ddir;
1528
1529 if (io_u->file->zbd_info->sectors_with_data ||
1530 td->o.read_beyond_wp)
1531 return DDIR_READ;
1532
1533 return DDIR_WRITE;
1534}
1535
1536/**
1537 * zbd_adjust_block - adjust the offset and length as necessary for ZBD drives
1538 * @td: FIO thread data.
1539 * @io_u: FIO I/O unit.
1540 *
1541 * Locking strategy: returns with z->mutex locked if and only if z refers
1542 * to a sequential zone and if io_u_accept is returned. z is the zone that
1543 * corresponds to io_u->offset at the end of this function.
1544 */
1545enum io_u_action zbd_adjust_block(struct thread_data *td, struct io_u *io_u)
1546{
1547 struct fio_file *f = io_u->file;
1548 uint32_t zone_idx_b;
1549 struct fio_zone_info *zb, *zl, *orig_zb;
1550 uint32_t orig_len = io_u->buflen;
1551 uint32_t min_bs = td->o.min_bs[io_u->ddir];
1552 uint64_t new_len;
1553 int64_t range;
1554
1555 if (!f->zbd_info)
1556 return io_u_accept;
1557
1558 assert(min_bs);
1559 assert(is_valid_offset(f, io_u->offset));
1560 assert(io_u->buflen);
1561 zone_idx_b = zbd_zone_idx(f, io_u->offset);
1562 zb = get_zone(f, zone_idx_b);
1563 orig_zb = zb;
1564
1565 /* Accept the I/O offset for conventional zones. */
1566 if (!zb->has_wp)
1567 return io_u_accept;
1568
1569 /*
1570 * Accept the I/O offset for reads if reading beyond the write pointer
1571 * is enabled.
1572 */
1573 if (zb->cond != ZBD_ZONE_COND_OFFLINE &&
1574 io_u->ddir == DDIR_READ && td->o.read_beyond_wp)
1575 return io_u_accept;
1576
1577 zbd_check_swd(f);
1578
1579 zone_lock(td, f, zb);
1580
1581 switch (io_u->ddir) {
1582 case DDIR_READ:
1583 if (td->runstate == TD_VERIFYING && td_write(td)) {
1584 zb = zbd_replay_write_order(td, io_u, zb);
1585 /*
1586 * Since we return with the zone lock still held,
1587 * add an annotation to let Coverity know that it
1588 * is intentional.
1589 */
1590 /* coverity[missing_unlock] */
1591 goto accept;
1592 }
1593 /*
1594 * Check that there is enough written data in the zone to do an
1595 * I/O of at least min_bs B. If there isn't, find a new zone for
1596 * the I/O.
1597 */
1598 range = zb->cond != ZBD_ZONE_COND_OFFLINE ?
1599 zb->wp - zb->start : 0;
1600 if (range < min_bs ||
1601 ((!td_random(td)) && (io_u->offset + min_bs > zb->wp))) {
1602 zone_unlock(zb);
1603 zl = get_zone(f, f->max_zone);
1604 zb = zbd_find_zone(td, io_u, zb, zl);
1605 if (!zb) {
1606 dprint(FD_ZBD,
1607 "%s: zbd_find_zone(%lld, %llu) failed\n",
1608 f->file_name, io_u->offset,
1609 io_u->buflen);
1610 goto eof;
1611 }
1612 /*
1613 * zbd_find_zone() returned a zone with a range of at
1614 * least min_bs.
1615 */
1616 range = zb->wp - zb->start;
1617 assert(range >= min_bs);
1618
1619 if (!td_random(td))
1620 io_u->offset = zb->start;
1621 }
1622 /*
1623 * Make sure the I/O is within the zone valid data range while
1624 * maximizing the I/O size and preserving randomness.
1625 */
1626 if (range <= io_u->buflen)
1627 io_u->offset = zb->start;
1628 else if (td_random(td))
1629 io_u->offset = zb->start +
1630 ((io_u->offset - orig_zb->start) %
1631 (range - io_u->buflen)) / min_bs * min_bs;
1632 /*
1633 * When zbd_find_zone() returns a conventional zone,
1634 * we can simply accept the new i/o offset here.
1635 */
1636 if (!zb->has_wp)
1637 return io_u_accept;
1638 /*
1639 * Make sure the I/O does not cross over the zone wp position.
1640 */
1641 new_len = min((unsigned long long)io_u->buflen,
1642 (unsigned long long)(zb->wp - io_u->offset));
1643 new_len = new_len / min_bs * min_bs;
1644 if (new_len < io_u->buflen) {
1645 io_u->buflen = new_len;
1646 dprint(FD_IO, "Changed length from %u into %llu\n",
1647 orig_len, io_u->buflen);
1648 }
1649 assert(zb->start <= io_u->offset);
1650 assert(io_u->offset + io_u->buflen <= zb->wp);
1651 goto accept;
1652 case DDIR_WRITE:
1653 if (io_u->buflen > f->zbd_info->zone_size)
1654 goto eof;
1655 if (!zbd_open_zone(td, f, zone_idx_b)) {
1656 zone_unlock(zb);
1657 zb = zbd_convert_to_open_zone(td, io_u);
1658 if (!zb)
1659 goto eof;
1660 zone_idx_b = zbd_zone_nr(f, zb);
1661 }
1662 /* Check whether the zone reset threshold has been exceeded */
1663 if (td->o.zrf.u.f) {
1664 if (f->zbd_info->wp_sectors_with_data >=
1665 f->io_size * td->o.zrt.u.f &&
1666 zbd_dec_and_reset_write_cnt(td, f)) {
1667 zb->reset_zone = 1;
1668 }
1669 }
1670 /* Reset the zone pointer if necessary */
1671 if (zb->reset_zone || zbd_zone_full(f, zb, min_bs)) {
1672 assert(td->o.verify == VERIFY_NONE);
1673 /*
1674 * Since previous write requests may have been submitted
1675 * asynchronously and since we will submit the zone
1676 * reset synchronously, wait until previously submitted
1677 * write requests have completed before issuing a
1678 * zone reset.
1679 */
1680 io_u_quiesce(td);
1681 zb->reset_zone = 0;
1682 if (zbd_reset_zone(td, f, zb) < 0)
1683 goto eof;
1684
1685 if (zb->capacity < min_bs) {
1686 log_err("zone capacity %llu smaller than minimum block size %d\n",
1687 (unsigned long long)zb->capacity,
1688 min_bs);
1689 goto eof;
1690 }
1691 }
1692 /* Make writes occur at the write pointer */
1693 assert(!zbd_zone_full(f, zb, min_bs));
1694 io_u->offset = zb->wp;
1695 if (!is_valid_offset(f, io_u->offset)) {
1696 dprint(FD_ZBD, "Dropped request with offset %llu\n",
1697 io_u->offset);
1698 goto eof;
1699 }
1700 /*
1701 * Make sure that the buflen is a multiple of the minimal
1702 * block size. Give up if shrinking would make the request too
1703 * small.
1704 */
1705 new_len = min((unsigned long long)io_u->buflen,
1706 zbd_zone_capacity_end(zb) - io_u->offset);
1707 new_len = new_len / min_bs * min_bs;
1708 if (new_len == io_u->buflen)
1709 goto accept;
1710 if (new_len >= min_bs) {
1711 io_u->buflen = new_len;
1712 dprint(FD_IO, "Changed length from %u into %llu\n",
1713 orig_len, io_u->buflen);
1714 goto accept;
1715 }
1716 log_err("Zone remainder %lld smaller than minimum block size %d\n",
1717 (zbd_zone_capacity_end(zb) - io_u->offset),
1718 min_bs);
1719 goto eof;
1720 case DDIR_TRIM:
1721 /* fall-through */
1722 case DDIR_SYNC:
1723 case DDIR_DATASYNC:
1724 case DDIR_SYNC_FILE_RANGE:
1725 case DDIR_WAIT:
1726 case DDIR_LAST:
1727 case DDIR_INVAL:
1728 goto accept;
1729 }
1730
1731 assert(false);
1732
1733accept:
1734 assert(zb->has_wp);
1735 assert(zb->cond != ZBD_ZONE_COND_OFFLINE);
1736 assert(!io_u->zbd_queue_io);
1737 assert(!io_u->zbd_put_io);
1738 io_u->zbd_queue_io = zbd_queue_io;
1739 io_u->zbd_put_io = zbd_put_io;
1740 return io_u_accept;
1741
1742eof:
1743 if (zb && zb->has_wp)
1744 zone_unlock(zb);
1745 return io_u_eof;
1746}
1747
1748/* Return a string with ZBD statistics */
1749char *zbd_write_status(const struct thread_stat *ts)
1750{
1751 char *res;
1752
1753 if (asprintf(&res, "; %llu zone resets", (unsigned long long) ts->nr_zone_resets) < 0)
1754 return NULL;
1755 return res;
1756}