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