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