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