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