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