t/io_uring: Fix the parameters calculation for multiple threads scenario
[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
50cc48d5
NC
35 if (f->filetype == FIO_TYPE_PIPE) {
36 log_err("zonemode=zbd does not support pipes\n");
37 return -EINVAL;
38 }
39
9db0cde8
NC
40 /* If regular file, always emulate zones inside the file. */
41 if (f->filetype == FIO_TYPE_FILE) {
42 *model = ZBD_NONE;
43 return 0;
44 }
45
6c5b11d3
DLM
46 if (td->io_ops && td->io_ops->get_zoned_model)
47 ret = td->io_ops->get_zoned_model(td, f, model);
48 else
49 ret = blkzoned_get_zoned_model(td, f, model);
b7694961
DLM
50 if (ret < 0) {
51 td_verror(td, errno, "get zoned model failed");
52 log_err("%s: get zoned model failed (%d).\n",
53 f->file_name, errno);
54 }
55
56 return ret;
57}
58
59/**
60 * zbd_report_zones - Get zone information
61 * @td: FIO thread data.
62 * @f: FIO file for which to get zone information
63 * @offset: offset from which to report zones
64 * @zones: Array of struct zbd_zone
65 * @nr_zones: Size of @zones array
66 *
67 * Get zone information into @zones starting from the zone at offset @offset
68 * for the device specified by @f.
69 *
70 * Returns the number of zones reported upon success and a negative error code
71 * upon failure. If the zone report is empty, always assume an error (device
72 * problem) and return -EIO.
73 */
74int zbd_report_zones(struct thread_data *td, struct fio_file *f,
75 uint64_t offset, struct zbd_zone *zones,
76 unsigned int nr_zones)
77{
78 int ret;
79
6c5b11d3
DLM
80 if (td->io_ops && td->io_ops->report_zones)
81 ret = td->io_ops->report_zones(td, f, offset, zones, nr_zones);
82 else
83 ret = blkzoned_report_zones(td, f, offset, zones, nr_zones);
b7694961
DLM
84 if (ret < 0) {
85 td_verror(td, errno, "report zones failed");
86 log_err("%s: report zones from sector %llu failed (%d).\n",
87 f->file_name, (unsigned long long)offset >> 9, errno);
88 } else if (ret == 0) {
89 td_verror(td, errno, "Empty zone report");
90 log_err("%s: report zones from sector %llu is empty.\n",
91 f->file_name, (unsigned long long)offset >> 9);
92 ret = -EIO;
93 }
94
95 return ret;
96}
97
98/**
99 * zbd_reset_wp - reset the write pointer of a range of zones
100 * @td: FIO thread data.
101 * @f: FIO file for which to reset zones
102 * @offset: Starting offset of the first zone to reset
103 * @length: Length of the range of zones to reset
104 *
105 * Reset the write pointer of all zones in the range @offset...@offset+@length.
106 * Returns 0 upon success and a negative error code upon failure.
107 */
108int zbd_reset_wp(struct thread_data *td, struct fio_file *f,
109 uint64_t offset, uint64_t length)
110{
111 int ret;
112
6c5b11d3
DLM
113 if (td->io_ops && td->io_ops->reset_wp)
114 ret = td->io_ops->reset_wp(td, f, offset, length);
115 else
116 ret = blkzoned_reset_wp(td, f, offset, length);
b7694961
DLM
117 if (ret < 0) {
118 td_verror(td, errno, "resetting wp failed");
119 log_err("%s: resetting wp for %llu sectors at sector %llu failed (%d).\n",
120 f->file_name, (unsigned long long)length >> 9,
121 (unsigned long long)offset >> 9, errno);
122 }
123
124 return ret;
125}
126
d2f442bc
NC
127/**
128 * zbd_get_max_open_zones - Get the maximum number of open zones
129 * @td: FIO thread data
130 * @f: FIO file for which to get max open zones
131 * @max_open_zones: Upon success, result will be stored here.
132 *
133 * A @max_open_zones value set to zero means no limit.
134 *
135 * Returns 0 upon success and a negative error code upon failure.
136 */
137int zbd_get_max_open_zones(struct thread_data *td, struct fio_file *f,
138 unsigned int *max_open_zones)
139{
140 int ret;
141
142 if (td->io_ops && td->io_ops->get_max_open_zones)
143 ret = td->io_ops->get_max_open_zones(td, f, max_open_zones);
144 else
145 ret = blkzoned_get_max_open_zones(td, f, max_open_zones);
146 if (ret < 0) {
147 td_verror(td, errno, "get max open zones failed");
148 log_err("%s: get max open zones failed (%d).\n",
149 f->file_name, errno);
150 }
151
152 return ret;
153}
154
bfbdd35b
BVA
155/**
156 * zbd_zone_idx - convert an offset into a zone number
157 * @f: file pointer.
158 * @offset: offset in bytes. If this offset is in the first zone_size bytes
159 * past the disk size then the index of the sentinel is returned.
160 */
161static uint32_t zbd_zone_idx(const struct fio_file *f, uint64_t offset)
162{
163 uint32_t zone_idx;
164
cd775e06 165 if (f->zbd_info->zone_size_log2 > 0)
bfbdd35b
BVA
166 zone_idx = offset >> f->zbd_info->zone_size_log2;
167 else
ee3696bd 168 zone_idx = offset / f->zbd_info->zone_size;
bfbdd35b
BVA
169
170 return min(zone_idx, f->zbd_info->nr_zones);
171}
172
236d23a8
SK
173/**
174 * zbd_zone_end - Return zone end location
175 * @z: zone info pointer.
176 */
177static inline uint64_t zbd_zone_end(const struct fio_zone_info *z)
178{
179 return (z+1)->start;
180}
181
182/**
183 * zbd_zone_capacity_end - Return zone capacity limit end location
184 * @z: zone info pointer.
185 */
186static inline uint64_t zbd_zone_capacity_end(const struct fio_zone_info *z)
187{
188 return z->start + z->capacity;
189}
190
bfbdd35b
BVA
191/**
192 * zbd_zone_full - verify whether a minimum number of bytes remain in a zone
193 * @f: file pointer.
194 * @z: zone info pointer.
195 * @required: minimum number of bytes that must remain in a zone.
196 *
197 * The caller must hold z->mutex.
198 */
199static bool zbd_zone_full(const struct fio_file *f, struct fio_zone_info *z,
200 uint64_t required)
201{
202 assert((required & 511) == 0);
203
be7a6bae 204 return z->has_wp &&
236d23a8 205 z->wp + required > zbd_zone_capacity_end(z);
bfbdd35b
BVA
206}
207
11afb212
DF
208static void zone_lock(struct thread_data *td, const struct fio_file *f,
209 struct fio_zone_info *z)
1f57803b 210{
fae3b9a0
AD
211 struct zoned_block_device_info *zbd = f->zbd_info;
212 uint32_t nz = z - zbd->zone_info;
213
214 /* A thread should never lock zones outside its working area. */
215 assert(f->min_zone <= nz && nz < f->max_zone);
216
43bcbd5b
SK
217 assert(z->has_wp);
218
1f57803b
DLM
219 /*
220 * Lock the io_u target zone. The zone will be unlocked if io_u offset
221 * is changed or when io_u completes and zbd_put_io() executed.
222 * To avoid multiple jobs doing asynchronous I/Os from deadlocking each
223 * other waiting for zone locks when building an io_u batch, first
224 * only trylock the zone. If the zone is already locked by another job,
225 * process the currently queued I/Os so that I/O progress is made and
226 * zones unlocked.
227 */
228 if (pthread_mutex_trylock(&z->mutex) != 0) {
229 if (!td_ioengine_flagged(td, FIO_SYNCIO))
230 io_u_quiesce(td);
231 pthread_mutex_lock(&z->mutex);
232 }
233}
234
4d4c71e6
DF
235static inline void zone_unlock(struct fio_zone_info *z)
236{
237 int ret;
238
43bcbd5b 239 assert(z->has_wp);
4d4c71e6
DF
240 ret = pthread_mutex_unlock(&z->mutex);
241 assert(!ret);
242}
243
bfbdd35b
BVA
244static bool is_valid_offset(const struct fio_file *f, uint64_t offset)
245{
246 return (uint64_t)(offset - f->file_offset) < f->io_size;
247}
248
923f7c1e
DF
249static inline struct fio_zone_info *get_zone(const struct fio_file *f,
250 unsigned int zone_nr)
251{
252 return &f->zbd_info->zone_info[zone_nr];
253}
254
bfbdd35b
BVA
255/* Verify whether direct I/O is used for all host-managed zoned drives. */
256static bool zbd_using_direct_io(void)
257{
258 struct thread_data *td;
259 struct fio_file *f;
260 int i, j;
261
262 for_each_td(td, i) {
263 if (td->o.odirect || !(td->o.td_ddir & TD_DDIR_WRITE))
264 continue;
265 for_each_file(td, f, j) {
266 if (f->zbd_info &&
b7694961 267 f->zbd_info->model == ZBD_HOST_MANAGED)
bfbdd35b
BVA
268 return false;
269 }
270 }
271
272 return true;
273}
274
275/* Whether or not the I/O range for f includes one or more sequential zones */
276static bool zbd_is_seq_job(struct fio_file *f)
277{
278 uint32_t zone_idx, zone_idx_b, zone_idx_e;
279
280 assert(f->zbd_info);
281 if (f->io_size == 0)
282 return false;
283 zone_idx_b = zbd_zone_idx(f, f->file_offset);
284 zone_idx_e = zbd_zone_idx(f, f->file_offset + f->io_size - 1);
285 for (zone_idx = zone_idx_b; zone_idx <= zone_idx_e; zone_idx++)
be7a6bae 286 if (get_zone(f, zone_idx)->has_wp)
bfbdd35b
BVA
287 return true;
288
289 return false;
290}
291
292/*
293 * Verify whether offset and size parameters are aligned with zone boundaries.
294 */
295static bool zbd_verify_sizes(void)
296{
297 const struct fio_zone_info *z;
298 struct thread_data *td;
299 struct fio_file *f;
300 uint64_t new_offset, new_end;
301 uint32_t zone_idx;
302 int i, j;
303
304 for_each_td(td, i) {
305 for_each_file(td, f, j) {
306 if (!f->zbd_info)
307 continue;
308 if (f->file_offset >= f->real_file_size)
309 continue;
310 if (!zbd_is_seq_job(f))
311 continue;
4d37720a 312
fc52e449
DLM
313 if (!td->o.zone_size) {
314 td->o.zone_size = f->zbd_info->zone_size;
315 if (!td->o.zone_size) {
316 log_err("%s: invalid 0 zone size\n",
317 f->file_name);
318 return false;
319 }
971d6a22
DLM
320 } else if (td->o.zone_size != f->zbd_info->zone_size) {
321 log_err("%s: job parameter zonesize %llu does not match disk zone size %llu.\n",
322 f->file_name, (unsigned long long) td->o.zone_size,
323 (unsigned long long) f->zbd_info->zone_size);
324 return false;
fc52e449
DLM
325 }
326
4316f640 327 if (td->o.zone_skip % td->o.zone_size) {
4d37720a
DLM
328 log_err("%s: zoneskip %llu is not a multiple of the device zone size %llu.\n",
329 f->file_name, (unsigned long long) td->o.zone_skip,
330 (unsigned long long) td->o.zone_size);
331 return false;
332 }
333
bfbdd35b 334 zone_idx = zbd_zone_idx(f, f->file_offset);
923f7c1e 335 z = get_zone(f, zone_idx);
f09a7773
PL
336 if ((f->file_offset != z->start) &&
337 (td->o.td_ddir != TD_DDIR_READ)) {
236d23a8 338 new_offset = zbd_zone_end(z);
bfbdd35b
BVA
339 if (new_offset >= f->file_offset + f->io_size) {
340 log_info("%s: io_size must be at least one zone\n",
341 f->file_name);
342 return false;
343 }
a0c84dd4
JA
344 log_info("%s: rounded up offset from %llu to %llu\n",
345 f->file_name, (unsigned long long) f->file_offset,
346 (unsigned long long) new_offset);
bfbdd35b
BVA
347 f->io_size -= (new_offset - f->file_offset);
348 f->file_offset = new_offset;
349 }
350 zone_idx = zbd_zone_idx(f, f->file_offset + f->io_size);
923f7c1e 351 z = get_zone(f, zone_idx);
ee3696bd 352 new_end = z->start;
f09a7773
PL
353 if ((td->o.td_ddir != TD_DDIR_READ) &&
354 (f->file_offset + f->io_size != new_end)) {
bfbdd35b
BVA
355 if (new_end <= f->file_offset) {
356 log_info("%s: io_size must be at least one zone\n",
357 f->file_name);
358 return false;
359 }
a0c84dd4
JA
360 log_info("%s: rounded down io_size from %llu to %llu\n",
361 f->file_name, (unsigned long long) f->io_size,
362 (unsigned long long) new_end - f->file_offset);
bfbdd35b
BVA
363 f->io_size = new_end - f->file_offset;
364 }
365 }
366 }
367
368 return true;
369}
370
371static bool zbd_verify_bs(void)
372{
373 struct thread_data *td;
374 struct fio_file *f;
bfbdd35b
BVA
375 int i, j, k;
376
377 for_each_td(td, i) {
e3be810b
SK
378 if (td_trim(td) &&
379 (td->o.min_bs[DDIR_TRIM] != td->o.max_bs[DDIR_TRIM] ||
380 td->o.bssplit_nr[DDIR_TRIM])) {
381 log_info("bsrange and bssplit are not allowed for trim with zonemode=zbd\n");
382 return false;
383 }
bfbdd35b 384 for_each_file(td, f, j) {
1ddd225e
AD
385 uint64_t zone_size;
386
bfbdd35b
BVA
387 if (!f->zbd_info)
388 continue;
389 zone_size = f->zbd_info->zone_size;
e3be810b
SK
390 if (td_trim(td) && td->o.bs[DDIR_TRIM] != zone_size) {
391 log_info("%s: trim block size %llu is not the zone size %llu\n",
392 f->file_name, td->o.bs[DDIR_TRIM],
393 (unsigned long long)zone_size);
394 return false;
395 }
59f94d26 396 for (k = 0; k < FIO_ARRAY_SIZE(td->o.bs); k++) {
bfbdd35b 397 if (td->o.verify != VERIFY_NONE &&
ee3696bd 398 zone_size % td->o.bs[k] != 0) {
1ddd225e 399 log_info("%s: block size %llu is not a divisor of the zone size %llu\n",
bfbdd35b 400 f->file_name, td->o.bs[k],
1ddd225e 401 (unsigned long long)zone_size);
bfbdd35b
BVA
402 return false;
403 }
404 }
405 }
406 }
407 return true;
408}
409
bfbdd35b
BVA
410static int ilog2(uint64_t i)
411{
412 int log = -1;
413
414 while (i) {
415 i >>= 1;
416 log++;
417 }
418 return log;
419}
420
421/*
422 * Initialize f->zbd_info for devices that are not zoned block devices. This
423 * allows to execute a ZBD workload against a non-ZBD device.
424 */
425static int init_zone_info(struct thread_data *td, struct fio_file *f)
426{
427 uint32_t nr_zones;
428 struct fio_zone_info *p;
a4b7f12b 429 uint64_t zone_size = td->o.zone_size;
b8dd9750 430 uint64_t zone_capacity = td->o.zone_capacity;
bfbdd35b 431 struct zoned_block_device_info *zbd_info = NULL;
bfbdd35b
BVA
432 int i;
433
a4b7f12b 434 if (zone_size == 0) {
9db0cde8 435 log_err("%s: Specifying the zone size is mandatory for regular file/block device with --zonemode=zbd\n\n",
a4b7f12b
DLM
436 f->file_name);
437 return 1;
438 }
439
440 if (zone_size < 512) {
441 log_err("%s: zone size must be at least 512 bytes for --zonemode=zbd\n\n",
442 f->file_name);
443 return 1;
444 }
445
b8dd9750
HH
446 if (zone_capacity == 0)
447 zone_capacity = zone_size;
448
449 if (zone_capacity > zone_size) {
450 log_err("%s: job parameter zonecapacity %llu is larger than zone size %llu\n",
451 f->file_name, (unsigned long long) td->o.zone_capacity,
452 (unsigned long long) td->o.zone_size);
453 return 1;
454 }
455
9db0cde8
NC
456 if (f->real_file_size < zone_size) {
457 log_err("%s: file/device size %"PRIu64" is smaller than zone size %"PRIu64"\n",
458 f->file_name, f->real_file_size, zone_size);
459 return -EINVAL;
460 }
461
ee3696bd 462 nr_zones = (f->real_file_size + zone_size - 1) / zone_size;
bfbdd35b
BVA
463 zbd_info = scalloc(1, sizeof(*zbd_info) +
464 (nr_zones + 1) * sizeof(zbd_info->zone_info[0]));
465 if (!zbd_info)
466 return -ENOMEM;
467
44ec32cb 468 mutex_init_pshared(&zbd_info->mutex);
bfbdd35b
BVA
469 zbd_info->refcount = 1;
470 p = &zbd_info->zone_info[0];
471 for (i = 0; i < nr_zones; i++, p++) {
44ec32cb
SK
472 mutex_init_pshared_with_type(&p->mutex,
473 PTHREAD_MUTEX_RECURSIVE);
bfbdd35b 474 p->start = i * zone_size;
b14651a2 475 p->wp = p->start;
b7694961
DLM
476 p->type = ZBD_ZONE_TYPE_SWR;
477 p->cond = ZBD_ZONE_COND_EMPTY;
b8dd9750 478 p->capacity = zone_capacity;
be7a6bae 479 p->has_wp = 1;
bfbdd35b
BVA
480 }
481 /* a sentinel */
482 p->start = nr_zones * zone_size;
483
484 f->zbd_info = zbd_info;
485 f->zbd_info->zone_size = zone_size;
486 f->zbd_info->zone_size_log2 = is_power_of_2(zone_size) ?
ebc403fe 487 ilog2(zone_size) : 0;
bfbdd35b 488 f->zbd_info->nr_zones = nr_zones;
bfbdd35b
BVA
489 return 0;
490}
491
492/*
b7694961
DLM
493 * Maximum number of zones to report in one operation.
494 */
495#define ZBD_REPORT_MAX_ZONES 8192U
496
497/*
498 * Parse the device zone report and store it in f->zbd_info. Must be called
499 * only for devices that are zoned, namely those with a model != ZBD_NONE.
bfbdd35b
BVA
500 */
501static int parse_zone_info(struct thread_data *td, struct fio_file *f)
502{
b7694961
DLM
503 int nr_zones, nrz;
504 struct zbd_zone *zones, *z;
bfbdd35b 505 struct fio_zone_info *p;
b7694961 506 uint64_t zone_size, offset;
bfbdd35b 507 struct zoned_block_device_info *zbd_info = NULL;
d060babc 508 int i, j, ret = -ENOMEM;
bfbdd35b 509
b7694961
DLM
510 zones = calloc(ZBD_REPORT_MAX_ZONES, sizeof(struct zbd_zone));
511 if (!zones)
bfbdd35b
BVA
512 goto out;
513
b7694961
DLM
514 nrz = zbd_report_zones(td, f, 0, zones, ZBD_REPORT_MAX_ZONES);
515 if (nrz < 0) {
516 ret = nrz;
517 log_info("fio: report zones (offset 0) failed for %s (%d).\n",
518 f->file_name, -ret);
519 goto out;
bfbdd35b
BVA
520 }
521
b7694961 522 zone_size = zones[0].len;
ee3696bd 523 nr_zones = (f->real_file_size + zone_size - 1) / zone_size;
bfbdd35b
BVA
524
525 if (td->o.zone_size == 0) {
ee3696bd
DLM
526 td->o.zone_size = zone_size;
527 } else if (td->o.zone_size != zone_size) {
4580ff92
DLM
528 log_err("fio: %s job parameter zonesize %llu does not match disk zone size %llu.\n",
529 f->file_name, (unsigned long long) td->o.zone_size,
a0c84dd4 530 (unsigned long long) zone_size);
bfbdd35b 531 ret = -EINVAL;
b7694961 532 goto out;
bfbdd35b
BVA
533 }
534
a0c84dd4
JA
535 dprint(FD_ZBD, "Device %s has %d zones of size %llu KB\n", f->file_name,
536 nr_zones, (unsigned long long) zone_size / 1024);
bfbdd35b
BVA
537
538 zbd_info = scalloc(1, sizeof(*zbd_info) +
539 (nr_zones + 1) * sizeof(zbd_info->zone_info[0]));
bfbdd35b 540 if (!zbd_info)
b7694961 541 goto out;
44ec32cb 542 mutex_init_pshared(&zbd_info->mutex);
bfbdd35b
BVA
543 zbd_info->refcount = 1;
544 p = &zbd_info->zone_info[0];
b7694961
DLM
545 for (offset = 0, j = 0; j < nr_zones;) {
546 z = &zones[0];
547 for (i = 0; i < nrz; i++, j++, z++, p++) {
44ec32cb
SK
548 mutex_init_pshared_with_type(&p->mutex,
549 PTHREAD_MUTEX_RECURSIVE);
b7694961 550 p->start = z->start;
236d23a8 551 p->capacity = z->capacity;
bfbdd35b 552 switch (z->cond) {
b7694961
DLM
553 case ZBD_ZONE_COND_NOT_WP:
554 case ZBD_ZONE_COND_FULL:
236d23a8 555 p->wp = p->start + p->capacity;
bfbdd35b
BVA
556 break;
557 default:
558 assert(z->start <= z->wp);
b7694961
DLM
559 assert(z->wp <= z->start + zone_size);
560 p->wp = z->wp;
bfbdd35b
BVA
561 break;
562 }
be7a6bae
DF
563
564 switch (z->type) {
565 case ZBD_ZONE_TYPE_SWR:
566 p->has_wp = 1;
567 break;
568 default:
569 p->has_wp = 0;
570 }
bfbdd35b
BVA
571 p->type = z->type;
572 p->cond = z->cond;
be7a6bae 573
bfbdd35b
BVA
574 if (j > 0 && p->start != p[-1].start + zone_size) {
575 log_info("%s: invalid zone data\n",
576 f->file_name);
577 ret = -EINVAL;
b7694961 578 goto out;
bfbdd35b
BVA
579 }
580 }
581 z--;
b7694961 582 offset = z->start + z->len;
bfbdd35b
BVA
583 if (j >= nr_zones)
584 break;
6c3f1cc1
DF
585 nrz = zbd_report_zones(td, f, offset, zones,
586 min((uint32_t)(nr_zones - j),
587 ZBD_REPORT_MAX_ZONES));
b7694961
DLM
588 if (nrz < 0) {
589 ret = nrz;
590 log_info("fio: report zones (offset %llu) failed for %s (%d).\n",
591 (unsigned long long)offset,
592 f->file_name, -ret);
593 goto out;
bfbdd35b
BVA
594 }
595 }
b7694961 596
bfbdd35b 597 /* a sentinel */
b7694961 598 zbd_info->zone_info[nr_zones].start = offset;
bfbdd35b
BVA
599
600 f->zbd_info = zbd_info;
601 f->zbd_info->zone_size = zone_size;
602 f->zbd_info->zone_size_log2 = is_power_of_2(zone_size) ?
ebc403fe 603 ilog2(zone_size) : 0;
bfbdd35b
BVA
604 f->zbd_info->nr_zones = nr_zones;
605 zbd_info = NULL;
606 ret = 0;
607
bfbdd35b 608out:
b7694961
DLM
609 sfree(zbd_info);
610 free(zones);
bfbdd35b
BVA
611 return ret;
612}
613
d2f442bc
NC
614static int zbd_set_max_open_zones(struct thread_data *td, struct fio_file *f)
615{
616 struct zoned_block_device_info *zbd = f->zbd_info;
617 unsigned int max_open_zones;
618 int ret;
619
575686bb 620 if (zbd->model != ZBD_HOST_MANAGED || td->o.ignore_zone_limits) {
d2f442bc
NC
621 /* Only host-managed devices have a max open limit */
622 zbd->max_open_zones = td->o.max_open_zones;
623 goto out;
624 }
625
626 /* If host-managed, get the max open limit */
627 ret = zbd_get_max_open_zones(td, f, &max_open_zones);
628 if (ret)
629 return ret;
630
631 if (!max_open_zones) {
632 /* No device limit */
633 zbd->max_open_zones = td->o.max_open_zones;
634 } else if (!td->o.max_open_zones) {
635 /* No user limit. Set limit to device limit */
636 zbd->max_open_zones = max_open_zones;
637 } else if (td->o.max_open_zones <= max_open_zones) {
638 /* Both user limit and dev limit. User limit not too large */
639 zbd->max_open_zones = td->o.max_open_zones;
640 } else {
641 /* Both user limit and dev limit. User limit too large */
642 td_verror(td, EINVAL,
643 "Specified --max_open_zones is too large");
644 log_err("Specified --max_open_zones (%d) is larger than max (%u)\n",
645 td->o.max_open_zones, max_open_zones);
646 return -EINVAL;
647 }
648
649out:
650 /* Ensure that the limit is not larger than FIO's internal limit */
b346af90
NC
651 if (zbd->max_open_zones > ZBD_MAX_OPEN_ZONES) {
652 td_verror(td, EINVAL, "'max_open_zones' value is too large");
653 log_err("'max_open_zones' value is larger than %u\n", ZBD_MAX_OPEN_ZONES);
654 return -EINVAL;
655 }
656
d2f442bc
NC
657 dprint(FD_ZBD, "%s: using max open zones limit: %"PRIu32"\n",
658 f->file_name, zbd->max_open_zones);
659
660 return 0;
661}
662
bfbdd35b
BVA
663/*
664 * Allocate zone information and store it into f->zbd_info if zonemode=zbd.
665 *
666 * Returns 0 upon success and a negative error code upon failure.
667 */
379e5f09 668static int zbd_create_zone_info(struct thread_data *td, struct fio_file *f)
bfbdd35b 669{
b7694961
DLM
670 enum zbd_zoned_model zbd_model;
671 int ret;
bfbdd35b
BVA
672
673 assert(td->o.zone_mode == ZONE_MODE_ZBD);
674
b7694961
DLM
675 ret = zbd_get_zoned_model(td, f, &zbd_model);
676 if (ret)
677 return ret;
678
bfbdd35b 679 switch (zbd_model) {
b7694961
DLM
680 case ZBD_HOST_AWARE:
681 case ZBD_HOST_MANAGED:
bfbdd35b 682 ret = parse_zone_info(td, f);
d2f442bc
NC
683 if (ret)
684 return ret;
bfbdd35b 685 break;
b7694961 686 case ZBD_NONE:
bfbdd35b 687 ret = init_zone_info(td, f);
d2f442bc
NC
688 if (ret)
689 return ret;
bfbdd35b 690 break;
b7694961
DLM
691 default:
692 td_verror(td, EINVAL, "Unsupported zoned model");
693 log_err("Unsupported zoned model\n");
694 return -EINVAL;
bfbdd35b 695 }
b7694961 696
2c7dd23e 697 assert(f->zbd_info);
d2f442bc
NC
698 f->zbd_info->model = zbd_model;
699
700 ret = zbd_set_max_open_zones(td, f);
701 if (ret) {
702 zbd_free_zone_info(f);
703 return ret;
219c662d 704 }
d2f442bc
NC
705
706 return 0;
bfbdd35b
BVA
707}
708
709void zbd_free_zone_info(struct fio_file *f)
710{
711 uint32_t refcount;
712
3c1dc34c 713 assert(f->zbd_info);
bfbdd35b
BVA
714
715 pthread_mutex_lock(&f->zbd_info->mutex);
716 refcount = --f->zbd_info->refcount;
717 pthread_mutex_unlock(&f->zbd_info->mutex);
718
719 assert((int32_t)refcount >= 0);
720 if (refcount == 0)
721 sfree(f->zbd_info);
722 f->zbd_info = NULL;
723}
724
725/*
726 * Initialize f->zbd_info.
727 *
728 * Returns 0 upon success and a negative error code upon failure.
729 *
730 * Note: this function can only work correctly if it is called before the first
731 * fio fork() call.
732 */
733static int zbd_init_zone_info(struct thread_data *td, struct fio_file *file)
734{
735 struct thread_data *td2;
736 struct fio_file *f2;
737 int i, j, ret;
738
739 for_each_td(td2, i) {
740 for_each_file(td2, f2, j) {
741 if (td2 == td && f2 == file)
742 continue;
743 if (!f2->zbd_info ||
744 strcmp(f2->file_name, file->file_name) != 0)
745 continue;
746 file->zbd_info = f2->zbd_info;
747 file->zbd_info->refcount++;
748 return 0;
749 }
750 }
751
752 ret = zbd_create_zone_info(td, file);
753 if (ret < 0)
c5837eec 754 td_verror(td, -ret, "zbd_create_zone_info() failed");
bfbdd35b
BVA
755 return ret;
756}
757
954217b9
SK
758static bool zbd_open_zone(struct thread_data *td, const struct fio_file *f,
759 uint32_t zone_idx);
760static int zbd_reset_zone(struct thread_data *td, struct fio_file *f,
761 struct fio_zone_info *z);
762
8f39afa7 763int zbd_init_files(struct thread_data *td)
bfbdd35b
BVA
764{
765 struct fio_file *f;
766 int i;
767
768 for_each_file(td, f, i) {
a4b7f12b 769 if (zbd_init_zone_info(td, f))
bfbdd35b 770 return 1;
bfbdd35b 771 }
8f39afa7
AD
772 return 0;
773}
774
775void zbd_recalc_options_with_zone_granularity(struct thread_data *td)
776{
777 struct fio_file *f;
778 int i;
779
780 for_each_file(td, f, i) {
781 struct zoned_block_device_info *zbd = f->zbd_info;
782 // zonemode=strided doesn't get per-file zone size.
783 uint64_t zone_size = zbd ? zbd->zone_size : td->o.zone_size;
784
785 if (zone_size == 0)
786 continue;
787
788 if (td->o.size_nz > 0) {
789 td->o.size = td->o.size_nz * zone_size;
790 }
791 if (td->o.io_size_nz > 0) {
792 td->o.io_size = td->o.io_size_nz * zone_size;
793 }
794 if (td->o.start_offset_nz > 0) {
795 td->o.start_offset = td->o.start_offset_nz * zone_size;
796 }
797 if (td->o.offset_increment_nz > 0) {
798 td->o.offset_increment = td->o.offset_increment_nz * zone_size;
799 }
800 if (td->o.zone_skip_nz > 0) {
801 td->o.zone_skip = td->o.zone_skip_nz * zone_size;
802 }
803 }
804}
805
806int zbd_setup_files(struct thread_data *td)
807{
808 struct fio_file *f;
809 int i;
bfbdd35b
BVA
810
811 if (!zbd_using_direct_io()) {
812 log_err("Using direct I/O is mandatory for writing to ZBD drives\n\n");
813 return 1;
814 }
815
816 if (!zbd_verify_sizes())
817 return 1;
818
819 if (!zbd_verify_bs())
820 return 1;
821
219c662d
AD
822 for_each_file(td, f, i) {
823 struct zoned_block_device_info *zbd = f->zbd_info;
954217b9
SK
824 struct fio_zone_info *z;
825 int zi;
219c662d 826
5ddf46d0 827 assert(zbd);
219c662d 828
f952800a
SK
829 f->min_zone = zbd_zone_idx(f, f->file_offset);
830 f->max_zone = zbd_zone_idx(f, f->file_offset + f->io_size);
831
832 /*
833 * When all zones in the I/O range are conventional, io_size
834 * can be smaller than zone size, making min_zone the same
835 * as max_zone. This is why the assert below needs to be made
836 * conditional.
837 */
838 if (zbd_is_seq_job(f))
839 assert(f->min_zone < f->max_zone);
840
219c662d
AD
841 if (td->o.max_open_zones > 0 &&
842 zbd->max_open_zones != td->o.max_open_zones) {
843 log_err("Different 'max_open_zones' values\n");
844 return 1;
845 }
b346af90
NC
846
847 /*
848 * The per job max open zones limit cannot be used without a
849 * global max open zones limit. (As the tracking of open zones
850 * is disabled when there is no global max open zones limit.)
851 */
852 if (td->o.job_max_open_zones && !zbd->max_open_zones) {
853 log_err("'job_max_open_zones' cannot be used without a global open zones limit\n");
219c662d
AD
854 return 1;
855 }
954217b9 856
ea51055c
NC
857 /*
858 * zbd->max_open_zones is the global limit shared for all jobs
859 * that target the same zoned block device. Force sync the per
860 * thread global limit with the actual global limit. (The real
861 * per thread/job limit is stored in td->o.job_max_open_zones).
862 */
863 td->o.max_open_zones = zbd->max_open_zones;
864
954217b9
SK
865 for (zi = f->min_zone; zi < f->max_zone; zi++) {
866 z = &zbd->zone_info[zi];
867 if (z->cond != ZBD_ZONE_COND_IMP_OPEN &&
868 z->cond != ZBD_ZONE_COND_EXP_OPEN)
869 continue;
870 if (zbd_open_zone(td, f, zi))
871 continue;
872 /*
873 * If the number of open zones exceeds specified limits,
874 * reset all extra open zones.
875 */
876 if (zbd_reset_zone(td, f, z) < 0) {
877 log_err("Failed to reest zone %d\n", zi);
878 return 1;
879 }
880 }
219c662d
AD
881 }
882
bfbdd35b
BVA
883 return 0;
884}
885
122c3539
DF
886static inline unsigned int zbd_zone_nr(const struct fio_file *f,
887 struct fio_zone_info *zone)
ceb35a11 888{
122c3539 889 return zone - f->zbd_info->zone_info;
ceb35a11
DF
890}
891
bfbdd35b 892/**
ceb35a11 893 * zbd_reset_zone - reset the write pointer of a single zone
bfbdd35b 894 * @td: FIO thread data.
ceb35a11
DF
895 * @f: FIO file associated with the disk for which to reset a write pointer.
896 * @z: Zone to reset.
bfbdd35b
BVA
897 *
898 * Returns 0 upon success and a negative error code upon failure.
ceb35a11
DF
899 *
900 * The caller must hold z->mutex.
bfbdd35b 901 */
ceb35a11
DF
902static int zbd_reset_zone(struct thread_data *td, struct fio_file *f,
903 struct fio_zone_info *z)
bfbdd35b 904{
ceb35a11
DF
905 uint64_t offset = z->start;
906 uint64_t length = (z+1)->start - offset;
403ea795 907 uint64_t data_in_zone = z->wp - z->start;
bfbdd35b
BVA
908 int ret = 0;
909
403ea795 910 if (!data_in_zone)
071c9d29
SK
911 return 0;
912
ee3696bd 913 assert(is_valid_offset(f, offset + length - 1));
b7694961 914
ceb35a11 915 dprint(FD_ZBD, "%s: resetting wp of zone %u.\n", f->file_name,
122c3539 916 zbd_zone_nr(f, z));
bfbdd35b 917 switch (f->zbd_info->model) {
b7694961
DLM
918 case ZBD_HOST_AWARE:
919 case ZBD_HOST_MANAGED:
920 ret = zbd_reset_wp(td, f, offset, length);
921 if (ret < 0)
bfbdd35b 922 return ret;
bfbdd35b 923 break;
b7694961 924 default:
bfbdd35b
BVA
925 break;
926 }
927
ceb35a11 928 pthread_mutex_lock(&f->zbd_info->mutex);
403ea795
SK
929 f->zbd_info->sectors_with_data -= data_in_zone;
930 f->zbd_info->wp_sectors_with_data -= data_in_zone;
ceb35a11
DF
931 pthread_mutex_unlock(&f->zbd_info->mutex);
932 z->wp = z->start;
933 z->verify_block = 0;
bfbdd35b 934
ceb35a11 935 td->ts.nr_zone_resets++;
fd5d733f 936
bfbdd35b
BVA
937 return ret;
938}
939
25dc6606
AD
940/* The caller must hold f->zbd_info->mutex */
941static void zbd_close_zone(struct thread_data *td, const struct fio_file *f,
b2da58c4 942 unsigned int zone_idx)
25dc6606 943{
b2da58c4 944 uint32_t open_zone_idx = 0;
25dc6606 945
b2da58c4
SK
946 for (; open_zone_idx < f->zbd_info->num_open_zones; open_zone_idx++) {
947 if (f->zbd_info->open_zones[open_zone_idx] == zone_idx)
948 break;
949 }
9fc5c3fd 950 if (open_zone_idx == f->zbd_info->num_open_zones)
b2da58c4 951 return;
b2da58c4
SK
952
953 dprint(FD_ZBD, "%s: closing zone %d\n", f->file_name, zone_idx);
25dc6606
AD
954 memmove(f->zbd_info->open_zones + open_zone_idx,
955 f->zbd_info->open_zones + open_zone_idx + 1,
956 (ZBD_MAX_OPEN_ZONES - (open_zone_idx + 1)) *
957 sizeof(f->zbd_info->open_zones[0]));
958 f->zbd_info->num_open_zones--;
219c662d 959 td->num_open_zones--;
923f7c1e 960 get_zone(f, zone_idx)->open = 0;
25dc6606
AD
961}
962
bfbdd35b
BVA
963/*
964 * Reset a range of zones. Returns 0 upon success and 1 upon failure.
965 * @td: fio thread data.
966 * @f: fio file for which to reset zones
967 * @zb: first zone to reset.
968 * @ze: first zone not to reset.
bfbdd35b
BVA
969 */
970static int zbd_reset_zones(struct thread_data *td, struct fio_file *f,
971 struct fio_zone_info *const zb,
57f88219 972 struct fio_zone_info *const ze)
bfbdd35b 973{
4803b841 974 struct fio_zone_info *z;
ee3696bd 975 const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
bfbdd35b
BVA
976 int res = 0;
977
adc6adcb
DLM
978 assert(min_bs);
979
a0c84dd4 980 dprint(FD_ZBD, "%s: examining zones %u .. %u\n", f->file_name,
122c3539 981 zbd_zone_nr(f, zb), zbd_zone_nr(f, ze));
bfbdd35b 982 for (z = zb; z < ze; z++) {
122c3539 983 uint32_t nz = zbd_zone_nr(f, z);
25dc6606 984
be7a6bae 985 if (!z->has_wp)
1f57803b 986 continue;
fae3b9a0 987 zone_lock(td, f, z);
57f88219
SK
988 pthread_mutex_lock(&f->zbd_info->mutex);
989 zbd_close_zone(td, f, nz);
990 pthread_mutex_unlock(&f->zbd_info->mutex);
991 if (z->wp != z->start) {
1f57803b 992 dprint(FD_ZBD, "%s: resetting zone %u\n",
122c3539 993 f->file_name, zbd_zone_nr(f, z));
1f57803b
DLM
994 if (zbd_reset_zone(td, f, z) < 0)
995 res = 1;
bfbdd35b 996 }
4d4c71e6 997 zone_unlock(z);
4803b841 998 }
bfbdd35b
BVA
999
1000 return res;
1001}
1002
a7c2b6fc
BVA
1003/*
1004 * Reset zbd_info.write_cnt, the counter that counts down towards the next
1005 * zone reset.
1006 */
1bb1bcad
AD
1007static void _zbd_reset_write_cnt(const struct thread_data *td,
1008 const struct fio_file *f)
a7c2b6fc
BVA
1009{
1010 assert(0 <= td->o.zrf.u.f && td->o.zrf.u.f <= 1);
1011
a7c2b6fc
BVA
1012 f->zbd_info->write_cnt = td->o.zrf.u.f ?
1013 min(1.0 / td->o.zrf.u.f, 0.0 + UINT_MAX) : UINT_MAX;
1bb1bcad
AD
1014}
1015
1016static void zbd_reset_write_cnt(const struct thread_data *td,
1017 const struct fio_file *f)
1018{
1019 pthread_mutex_lock(&f->zbd_info->mutex);
1020 _zbd_reset_write_cnt(td, f);
a7c2b6fc
BVA
1021 pthread_mutex_unlock(&f->zbd_info->mutex);
1022}
1023
1024static bool zbd_dec_and_reset_write_cnt(const struct thread_data *td,
1025 const struct fio_file *f)
1026{
1027 uint32_t write_cnt = 0;
1028
1029 pthread_mutex_lock(&f->zbd_info->mutex);
1030 assert(f->zbd_info->write_cnt);
1031 if (f->zbd_info->write_cnt)
1032 write_cnt = --f->zbd_info->write_cnt;
1033 if (write_cnt == 0)
1bb1bcad 1034 _zbd_reset_write_cnt(td, f);
a7c2b6fc
BVA
1035 pthread_mutex_unlock(&f->zbd_info->mutex);
1036
1037 return write_cnt == 0;
1038}
1039
91d25131
BVA
1040enum swd_action {
1041 CHECK_SWD,
1042 SET_SWD,
1043};
1044
1045/* Calculate the number of sectors with data (swd) and perform action 'a' */
11afb212
DF
1046static uint64_t zbd_process_swd(struct thread_data *td,
1047 const struct fio_file *f, enum swd_action a)
615555bb 1048{
615555bb
BVA
1049 struct fio_zone_info *zb, *ze, *z;
1050 uint64_t swd = 0;
403ea795 1051 uint64_t wp_swd = 0;
615555bb 1052
923f7c1e
DF
1053 zb = get_zone(f, f->min_zone);
1054 ze = get_zone(f, f->max_zone);
615555bb 1055 for (z = zb; z < ze; z++) {
403ea795 1056 if (z->has_wp) {
11afb212 1057 zone_lock(td, f, z);
403ea795
SK
1058 wp_swd += z->wp - z->start;
1059 }
615555bb
BVA
1060 swd += z->wp - z->start;
1061 }
1062 pthread_mutex_lock(&f->zbd_info->mutex);
91d25131
BVA
1063 switch (a) {
1064 case CHECK_SWD:
1065 assert(f->zbd_info->sectors_with_data == swd);
403ea795 1066 assert(f->zbd_info->wp_sectors_with_data == wp_swd);
91d25131
BVA
1067 break;
1068 case SET_SWD:
1069 f->zbd_info->sectors_with_data = swd;
403ea795 1070 f->zbd_info->wp_sectors_with_data = wp_swd;
91d25131
BVA
1071 break;
1072 }
615555bb
BVA
1073 pthread_mutex_unlock(&f->zbd_info->mutex);
1074 for (z = zb; z < ze; z++)
403ea795
SK
1075 if (z->has_wp)
1076 zone_unlock(z);
91d25131
BVA
1077
1078 return swd;
1079}
1080
1081/*
1082 * The swd check is useful for debugging but takes too much time to leave
1083 * it enabled all the time. Hence it is disabled by default.
1084 */
1085static const bool enable_check_swd = false;
1086
403ea795 1087/* Check whether the values of zbd_info.*sectors_with_data are correct. */
11afb212 1088static void zbd_check_swd(struct thread_data *td, const struct fio_file *f)
91d25131
BVA
1089{
1090 if (!enable_check_swd)
1091 return;
1092
11afb212 1093 zbd_process_swd(td, f, CHECK_SWD);
615555bb
BVA
1094}
1095
bfbdd35b
BVA
1096void zbd_file_reset(struct thread_data *td, struct fio_file *f)
1097{
91d25131 1098 struct fio_zone_info *zb, *ze;
11afb212 1099 uint64_t swd;
bfbdd35b 1100
767d1372 1101 if (!f->zbd_info || !td_write(td))
bfbdd35b
BVA
1102 return;
1103
923f7c1e
DF
1104 zb = get_zone(f, f->min_zone);
1105 ze = get_zone(f, f->max_zone);
11afb212
DF
1106 swd = zbd_process_swd(td, f, SET_SWD);
1107 dprint(FD_ZBD, "%s(%s): swd = %" PRIu64 "\n", __func__, f->file_name,
1108 swd);
bfbdd35b
BVA
1109 /*
1110 * If data verification is enabled reset the affected zones before
1111 * writing any data to avoid that a zone reset has to be issued while
1112 * writing data, which causes data loss.
1113 */
57f88219
SK
1114 if (td->o.verify != VERIFY_NONE && td->runstate != TD_VERIFYING)
1115 zbd_reset_zones(td, f, zb, ze);
a7c2b6fc 1116 zbd_reset_write_cnt(td, f);
bfbdd35b
BVA
1117}
1118
59b07544
BVA
1119/* The caller must hold f->zbd_info->mutex. */
1120static bool is_zone_open(const struct thread_data *td, const struct fio_file *f,
1121 unsigned int zone_idx)
1122{
1123 struct zoned_block_device_info *zbdi = f->zbd_info;
1124 int i;
1125
b346af90
NC
1126 /* This function should never be called when zbdi->max_open_zones == 0 */
1127 assert(zbdi->max_open_zones);
219c662d
AD
1128 assert(td->o.job_max_open_zones == 0 || td->num_open_zones <= td->o.job_max_open_zones);
1129 assert(td->o.job_max_open_zones <= zbdi->max_open_zones);
1130 assert(zbdi->num_open_zones <= zbdi->max_open_zones);
59b07544
BVA
1131
1132 for (i = 0; i < zbdi->num_open_zones; i++)
1133 if (zbdi->open_zones[i] == zone_idx)
1134 return true;
1135
1136 return false;
1137}
1138
1139/*
1140 * Open a ZBD zone if it was not yet open. Returns true if either the zone was
1141 * already open or if opening a new zone is allowed. Returns false if the zone
1142 * was not yet open and opening a new zone would cause the zone limit to be
1143 * exceeded.
1144 */
954217b9 1145static bool zbd_open_zone(struct thread_data *td, const struct fio_file *f,
59b07544
BVA
1146 uint32_t zone_idx)
1147{
1148 const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
af94a8c3 1149 struct zoned_block_device_info *zbdi = f->zbd_info;
923f7c1e 1150 struct fio_zone_info *z = get_zone(f, zone_idx);
59b07544
BVA
1151 bool res = true;
1152
b7694961 1153 if (z->cond == ZBD_ZONE_COND_OFFLINE)
59b07544
BVA
1154 return false;
1155
1156 /*
1157 * Skip full zones with data verification enabled because resetting a
1158 * zone causes data loss and hence causes verification to fail.
1159 */
1160 if (td->o.verify != VERIFY_NONE && zbd_zone_full(f, z, min_bs))
1161 return false;
1162
b346af90
NC
1163 /*
1164 * zbdi->max_open_zones == 0 means that there is no limit on the maximum
1165 * number of open zones. In this case, do no track open zones in
1166 * zbdi->open_zones array.
1167 */
1168 if (!zbdi->max_open_zones)
1169 return true;
1170
af94a8c3 1171 pthread_mutex_lock(&zbdi->mutex);
b2da58c4
SK
1172 if (is_zone_open(td, f, zone_idx)) {
1173 /*
1174 * If the zone is already open and going to be full by writes
1175 * in-flight, handle it as a full zone instead of an open zone.
1176 */
1177 if (z->wp >= zbd_zone_capacity_end(z))
1178 res = false;
59b07544 1179 goto out;
b2da58c4 1180 }
59b07544 1181 res = false;
219c662d
AD
1182 /* Zero means no limit */
1183 if (td->o.job_max_open_zones > 0 &&
1184 td->num_open_zones >= td->o.job_max_open_zones)
1185 goto out;
af94a8c3 1186 if (zbdi->num_open_zones >= zbdi->max_open_zones)
59b07544
BVA
1187 goto out;
1188 dprint(FD_ZBD, "%s: opening zone %d\n", f->file_name, zone_idx);
af94a8c3 1189 zbdi->open_zones[zbdi->num_open_zones++] = zone_idx;
219c662d 1190 td->num_open_zones++;
59b07544
BVA
1191 z->open = 1;
1192 res = true;
1193
1194out:
af94a8c3 1195 pthread_mutex_unlock(&zbdi->mutex);
59b07544
BVA
1196 return res;
1197}
1198
cb765e41 1199/* Return random zone index for one of the open zones. */
6463db6c
AD
1200static uint32_t pick_random_zone_idx(const struct fio_file *f,
1201 const struct io_u *io_u)
1202{
cb765e41
AK
1203 return (io_u->offset - f->file_offset) * f->zbd_info->num_open_zones /
1204 f->io_size;
6463db6c
AD
1205}
1206
0f77c977
SK
1207static bool any_io_in_flight(void)
1208{
1209 struct thread_data *td;
1210 int i;
1211
1212 for_each_td(td, i) {
1213 if (td->io_u_in_flight)
1214 return true;
1215 }
1216
1217 return false;
1218}
1219
59b07544
BVA
1220/*
1221 * Modify the offset of an I/O unit that does not refer to an open zone such
1222 * that it refers to an open zone. Close an open zone and open a new zone if
21c0c884
SK
1223 * necessary. The open zone is searched across sequential zones.
1224 * This algorithm can only work correctly if all write pointers are
59b07544
BVA
1225 * a multiple of the fio block size. The caller must neither hold z->mutex
1226 * nor f->zbd_info->mutex. Returns with z->mutex held upon success.
1227 */
379e5f09
BVA
1228static struct fio_zone_info *zbd_convert_to_open_zone(struct thread_data *td,
1229 struct io_u *io_u)
59b07544
BVA
1230{
1231 const uint32_t min_bs = td->o.min_bs[io_u->ddir];
fae3b9a0 1232 struct fio_file *f = io_u->file;
af94a8c3 1233 struct zoned_block_device_info *zbdi = f->zbd_info;
59b07544
BVA
1234 struct fio_zone_info *z;
1235 unsigned int open_zone_idx = -1;
1236 uint32_t zone_idx, new_zone_idx;
1237 int i;
b2da58c4 1238 bool wait_zone_close;
0f77c977
SK
1239 bool in_flight;
1240 bool should_retry = true;
59b07544
BVA
1241
1242 assert(is_valid_offset(f, io_u->offset));
1243
ea51055c 1244 if (zbdi->max_open_zones || td->o.job_max_open_zones) {
59b07544 1245 /*
af94a8c3 1246 * This statement accesses zbdi->open_zones[] on purpose
59b07544
BVA
1247 * without locking.
1248 */
af94a8c3 1249 zone_idx = zbdi->open_zones[pick_random_zone_idx(f, io_u)];
59b07544
BVA
1250 } else {
1251 zone_idx = zbd_zone_idx(f, io_u->offset);
1252 }
fae3b9a0
AD
1253 if (zone_idx < f->min_zone)
1254 zone_idx = f->min_zone;
1255 else if (zone_idx >= f->max_zone)
1256 zone_idx = f->max_zone - 1;
59b07544
BVA
1257 dprint(FD_ZBD, "%s(%s): starting from zone %d (offset %lld, buflen %lld)\n",
1258 __func__, f->file_name, zone_idx, io_u->offset, io_u->buflen);
1259
1260 /*
af94a8c3 1261 * Since z->mutex is the outer lock and zbdi->mutex the inner
59b07544 1262 * lock it can happen that the state of the zone with index zone_idx
af94a8c3 1263 * has changed after 'z' has been assigned and before zbdi->mutex
59b07544
BVA
1264 * has been obtained. Hence the loop.
1265 */
1266 for (;;) {
6463db6c
AD
1267 uint32_t tmp_idx;
1268
923f7c1e 1269 z = get_zone(f, zone_idx);
14351148
DF
1270 if (z->has_wp)
1271 zone_lock(td, f, z);
af94a8c3 1272 pthread_mutex_lock(&zbdi->mutex);
14351148
DF
1273 if (z->has_wp) {
1274 if (z->cond != ZBD_ZONE_COND_OFFLINE &&
ea51055c 1275 zbdi->max_open_zones == 0 && td->o.job_max_open_zones == 0)
14351148 1276 goto examine_zone;
af94a8c3 1277 if (zbdi->num_open_zones == 0) {
14351148
DF
1278 dprint(FD_ZBD, "%s(%s): no zones are open\n",
1279 __func__, f->file_name);
1280 goto open_other_zone;
1281 }
59b07544 1282 }
6463db6c
AD
1283
1284 /*
1285 * List of opened zones is per-device, shared across all threads.
1286 * Start with quasi-random candidate zone.
1287 * Ignore zones which don't belong to thread's offset/size area.
1288 */
1289 open_zone_idx = pick_random_zone_idx(f, io_u);
1a8d510a 1290 assert(!open_zone_idx ||
af94a8c3 1291 open_zone_idx < zbdi->num_open_zones);
6463db6c 1292 tmp_idx = open_zone_idx;
af94a8c3 1293 for (i = 0; i < zbdi->num_open_zones; i++) {
6463db6c
AD
1294 uint32_t tmpz;
1295
af94a8c3 1296 if (tmp_idx >= zbdi->num_open_zones)
6463db6c 1297 tmp_idx = 0;
af94a8c3 1298 tmpz = zbdi->open_zones[tmp_idx];
fae3b9a0 1299 if (f->min_zone <= tmpz && tmpz < f->max_zone) {
6463db6c
AD
1300 open_zone_idx = tmp_idx;
1301 goto found_candidate_zone;
1302 }
1303
1304 tmp_idx++;
1305 }
1306
1307 dprint(FD_ZBD, "%s(%s): no candidate zone\n",
1308 __func__, f->file_name);
af94a8c3 1309 pthread_mutex_unlock(&zbdi->mutex);
14351148
DF
1310 if (z->has_wp)
1311 zone_unlock(z);
6463db6c
AD
1312 return NULL;
1313
1314found_candidate_zone:
af94a8c3 1315 new_zone_idx = zbdi->open_zones[open_zone_idx];
59b07544
BVA
1316 if (new_zone_idx == zone_idx)
1317 break;
1318 zone_idx = new_zone_idx;
af94a8c3 1319 pthread_mutex_unlock(&zbdi->mutex);
14351148
DF
1320 if (z->has_wp)
1321 zone_unlock(z);
59b07544
BVA
1322 }
1323
af94a8c3 1324 /* Both z->mutex and zbdi->mutex are held. */
59b07544
BVA
1325
1326examine_zone:
236d23a8 1327 if (z->wp + min_bs <= zbd_zone_capacity_end(z)) {
af94a8c3 1328 pthread_mutex_unlock(&zbdi->mutex);
59b07544
BVA
1329 goto out;
1330 }
b2da58c4
SK
1331
1332open_other_zone:
1333 /* Check if number of open zones reaches one of limits. */
1334 wait_zone_close =
af94a8c3 1335 zbdi->num_open_zones == f->max_zone - f->min_zone ||
ea51055c
NC
1336 (zbdi->max_open_zones &&
1337 zbdi->num_open_zones == zbdi->max_open_zones) ||
b2da58c4
SK
1338 (td->o.job_max_open_zones &&
1339 td->num_open_zones == td->o.job_max_open_zones);
1340
af94a8c3 1341 pthread_mutex_unlock(&zbdi->mutex);
59b07544
BVA
1342
1343 /* Only z->mutex is held. */
1344
b2da58c4
SK
1345 /*
1346 * When number of open zones reaches to one of limits, wait for
1347 * zone close before opening a new zone.
1348 */
1349 if (wait_zone_close) {
1350 dprint(FD_ZBD, "%s(%s): quiesce to allow open zones to close\n",
1351 __func__, f->file_name);
1352 io_u_quiesce(td);
1353 }
1354
0f77c977 1355retry:
59b07544 1356 /* Zone 'z' is full, so try to open a new zone. */
af94a8c3 1357 for (i = f->io_size / zbdi->zone_size; i > 0; i--) {
59b07544 1358 zone_idx++;
21c0c884
SK
1359 if (z->has_wp)
1360 zone_unlock(z);
59b07544 1361 z++;
ee3696bd 1362 if (!is_valid_offset(f, z->start)) {
59b07544 1363 /* Wrap-around. */
fae3b9a0 1364 zone_idx = f->min_zone;
923f7c1e 1365 z = get_zone(f, zone_idx);
59b07544 1366 }
ee3696bd 1367 assert(is_valid_offset(f, z->start));
21c0c884
SK
1368 if (!z->has_wp)
1369 continue;
fae3b9a0 1370 zone_lock(td, f, z);
59b07544
BVA
1371 if (z->open)
1372 continue;
954217b9 1373 if (zbd_open_zone(td, f, zone_idx))
59b07544
BVA
1374 goto out;
1375 }
1376
1377 /* Only z->mutex is held. */
1378
1379 /* Check whether the write fits in any of the already opened zones. */
af94a8c3
NC
1380 pthread_mutex_lock(&zbdi->mutex);
1381 for (i = 0; i < zbdi->num_open_zones; i++) {
1382 zone_idx = zbdi->open_zones[i];
fae3b9a0
AD
1383 if (zone_idx < f->min_zone || zone_idx >= f->max_zone)
1384 continue;
af94a8c3 1385 pthread_mutex_unlock(&zbdi->mutex);
4d4c71e6 1386 zone_unlock(z);
59b07544 1387
923f7c1e 1388 z = get_zone(f, zone_idx);
59b07544 1389
fae3b9a0 1390 zone_lock(td, f, z);
236d23a8 1391 if (z->wp + min_bs <= zbd_zone_capacity_end(z))
59b07544 1392 goto out;
af94a8c3 1393 pthread_mutex_lock(&zbdi->mutex);
59b07544 1394 }
0f77c977
SK
1395
1396 /*
1397 * When any I/O is in-flight or when all I/Os in-flight get completed,
1398 * the I/Os might have closed zones then retry the steps to open a zone.
1399 * Before retry, call io_u_quiesce() to complete in-flight writes.
1400 */
1401 in_flight = any_io_in_flight();
1402 if (in_flight || should_retry) {
1403 dprint(FD_ZBD, "%s(%s): wait zone close and retry open zones\n",
1404 __func__, f->file_name);
1405 pthread_mutex_unlock(&zbdi->mutex);
1406 zone_unlock(z);
1407 io_u_quiesce(td);
1408 zone_lock(td, f, z);
1409 should_retry = in_flight;
1410 goto retry;
1411 }
1412
af94a8c3 1413 pthread_mutex_unlock(&zbdi->mutex);
4d4c71e6 1414 zone_unlock(z);
59b07544
BVA
1415 dprint(FD_ZBD, "%s(%s): did not open another zone\n", __func__,
1416 f->file_name);
1417 return NULL;
1418
1419out:
1420 dprint(FD_ZBD, "%s(%s): returning zone %d\n", __func__, f->file_name,
1421 zone_idx);
ee3696bd 1422 io_u->offset = z->start;
21c0c884 1423 assert(z->has_wp);
8a866de7 1424 assert(z->cond != ZBD_ZONE_COND_OFFLINE);
59b07544
BVA
1425 return z;
1426}
1427
bfbdd35b 1428/* The caller must hold z->mutex. */
59b07544
BVA
1429static struct fio_zone_info *zbd_replay_write_order(struct thread_data *td,
1430 struct io_u *io_u,
1431 struct fio_zone_info *z)
bfbdd35b
BVA
1432{
1433 const struct fio_file *f = io_u->file;
1434 const uint32_t min_bs = td->o.min_bs[DDIR_WRITE];
1435
122c3539 1436 if (!zbd_open_zone(td, f, zbd_zone_nr(f, z))) {
4d4c71e6 1437 zone_unlock(z);
59b07544
BVA
1438 z = zbd_convert_to_open_zone(td, io_u);
1439 assert(z);
1440 }
1441
0cac10b4 1442 if (z->verify_block * min_bs >= z->capacity) {
a0c84dd4 1443 log_err("%s: %d * %d >= %llu\n", f->file_name, z->verify_block,
236d23a8 1444 min_bs, (unsigned long long)z->capacity);
0cac10b4
DF
1445 /*
1446 * If the assertion below fails during a test run, adding
1447 * "--experimental_verify=1" to the command line may help.
1448 */
1449 assert(false);
1450 }
1451 io_u->offset = z->start + z->verify_block * min_bs;
1452 if (io_u->offset + io_u->buflen >= zbd_zone_capacity_end(z)) {
1e3d6e03
JA
1453 log_err("%s: %llu + %llu >= %llu\n", f->file_name, io_u->offset,
1454 io_u->buflen, (unsigned long long) zbd_zone_capacity_end(z));
0cac10b4
DF
1455 assert(false);
1456 }
1457 z->verify_block += io_u->buflen / min_bs;
1458
59b07544 1459 return z;
bfbdd35b
BVA
1460}
1461
1462/*
5c86fdf6
SK
1463 * Find another zone which has @min_bytes of readable data. Search in zones
1464 * @zb + 1 .. @zl. For random workload, also search in zones @zb - 1 .. @zf.
bfbdd35b 1465 *
21c0c884
SK
1466 * Either returns NULL or returns a zone pointer. When the zone has write
1467 * pointer, hold the mutex for the zone.
bfbdd35b
BVA
1468 */
1469static struct fio_zone_info *
5c86fdf6 1470zbd_find_zone(struct thread_data *td, struct io_u *io_u, uint32_t min_bytes,
bfbdd35b
BVA
1471 struct fio_zone_info *zb, struct fio_zone_info *zl)
1472{
fae3b9a0 1473 struct fio_file *f = io_u->file;
bfbdd35b 1474 struct fio_zone_info *z1, *z2;
923f7c1e 1475 const struct fio_zone_info *const zf = get_zone(f, f->min_zone);
bfbdd35b
BVA
1476
1477 /*
1478 * Skip to the next non-empty zone in case of sequential I/O and to
1479 * the nearest non-empty zone in case of random I/O.
1480 */
1481 for (z1 = zb + 1, z2 = zb - 1; z1 < zl || z2 >= zf; z1++, z2--) {
b7694961 1482 if (z1 < zl && z1->cond != ZBD_ZONE_COND_OFFLINE) {
21c0c884
SK
1483 if (z1->has_wp)
1484 zone_lock(td, f, z1);
5c86fdf6 1485 if (z1->start + min_bytes <= z1->wp)
bfbdd35b 1486 return z1;
21c0c884
SK
1487 if (z1->has_wp)
1488 zone_unlock(z1);
bfbdd35b
BVA
1489 } else if (!td_random(td)) {
1490 break;
1491 }
1492 if (td_random(td) && z2 >= zf &&
b7694961 1493 z2->cond != ZBD_ZONE_COND_OFFLINE) {
21c0c884
SK
1494 if (z2->has_wp)
1495 zone_lock(td, f, z2);
5c86fdf6 1496 if (z2->start + min_bytes <= z2->wp)
bfbdd35b 1497 return z2;
21c0c884
SK
1498 if (z2->has_wp)
1499 zone_unlock(z2);
bfbdd35b
BVA
1500 }
1501 }
5c86fdf6
SK
1502 dprint(FD_ZBD, "%s: no zone has %d bytes of readable data\n",
1503 f->file_name, min_bytes);
bfbdd35b
BVA
1504 return NULL;
1505}
1506
b2da58c4
SK
1507/**
1508 * zbd_end_zone_io - update zone status at command completion
1509 * @io_u: I/O unit
1510 * @z: zone info pointer
1511 *
1512 * If the write command made the zone full, close it.
1513 *
1514 * The caller must hold z->mutex.
1515 */
1516static void zbd_end_zone_io(struct thread_data *td, const struct io_u *io_u,
1517 struct fio_zone_info *z)
1518{
1519 const struct fio_file *f = io_u->file;
1520
1521 if (io_u->ddir == DDIR_WRITE &&
1522 io_u->offset + io_u->buflen >= zbd_zone_capacity_end(z)) {
1523 pthread_mutex_lock(&f->zbd_info->mutex);
122c3539 1524 zbd_close_zone(td, f, zbd_zone_nr(f, z));
b2da58c4
SK
1525 pthread_mutex_unlock(&f->zbd_info->mutex);
1526 }
1527}
1528
bfbdd35b 1529/**
d9ed3e63 1530 * zbd_queue_io - update the write pointer of a sequential zone
bfbdd35b 1531 * @io_u: I/O unit
d9ed3e63
DLM
1532 * @success: Whether or not the I/O unit has been queued successfully
1533 * @q: queueing status (busy, completed or queued).
bfbdd35b 1534 *
d9ed3e63
DLM
1535 * For write and trim operations, update the write pointer of the I/O unit
1536 * target zone.
bfbdd35b 1537 */
b2da58c4
SK
1538static void zbd_queue_io(struct thread_data *td, struct io_u *io_u, int q,
1539 bool success)
bfbdd35b 1540{
d9ed3e63
DLM
1541 const struct fio_file *f = io_u->file;
1542 struct zoned_block_device_info *zbd_info = f->zbd_info;
bfbdd35b
BVA
1543 struct fio_zone_info *z;
1544 uint32_t zone_idx;
d9ed3e63 1545 uint64_t zone_end;
bfbdd35b 1546
5ddf46d0 1547 assert(zbd_info);
bfbdd35b 1548
d9ed3e63 1549 zone_idx = zbd_zone_idx(f, io_u->offset);
bfbdd35b 1550 assert(zone_idx < zbd_info->nr_zones);
923f7c1e 1551 z = get_zone(f, zone_idx);
d9ed3e63 1552
43bcbd5b 1553 assert(z->has_wp);
d9ed3e63 1554
bfbdd35b
BVA
1555 if (!success)
1556 goto unlock;
d9ed3e63
DLM
1557
1558 dprint(FD_ZBD,
1559 "%s: queued I/O (%lld, %llu) for zone %u\n",
1560 f->file_name, io_u->offset, io_u->buflen, zone_idx);
1561
bfbdd35b
BVA
1562 switch (io_u->ddir) {
1563 case DDIR_WRITE:
d9ed3e63 1564 zone_end = min((uint64_t)(io_u->offset + io_u->buflen),
236d23a8 1565 zbd_zone_capacity_end(z));
a7c2b6fc
BVA
1566 pthread_mutex_lock(&zbd_info->mutex);
1567 /*
1568 * z->wp > zone_end means that one or more I/O errors
1569 * have occurred.
1570 */
403ea795 1571 if (z->wp <= zone_end) {
a7c2b6fc 1572 zbd_info->sectors_with_data += zone_end - z->wp;
403ea795
SK
1573 zbd_info->wp_sectors_with_data += zone_end - z->wp;
1574 }
a7c2b6fc 1575 pthread_mutex_unlock(&zbd_info->mutex);
bfbdd35b
BVA
1576 z->wp = zone_end;
1577 break;
bfbdd35b
BVA
1578 default:
1579 break;
1580 }
d9ed3e63 1581
b2da58c4
SK
1582 if (q == FIO_Q_COMPLETED && !io_u->error)
1583 zbd_end_zone_io(td, io_u, z);
1584
bfbdd35b 1585unlock:
d9ed3e63
DLM
1586 if (!success || q != FIO_Q_QUEUED) {
1587 /* BUSY or COMPLETED: unlock the zone */
4d4c71e6 1588 zone_unlock(z);
d9ed3e63
DLM
1589 io_u->zbd_put_io = NULL;
1590 }
1591}
1592
1593/**
1594 * zbd_put_io - Unlock an I/O unit target zone lock
1595 * @io_u: I/O unit
1596 */
b2da58c4 1597static void zbd_put_io(struct thread_data *td, const struct io_u *io_u)
d9ed3e63
DLM
1598{
1599 const struct fio_file *f = io_u->file;
1600 struct zoned_block_device_info *zbd_info = f->zbd_info;
1601 struct fio_zone_info *z;
1602 uint32_t zone_idx;
1603
5ddf46d0 1604 assert(zbd_info);
615555bb 1605
d9ed3e63
DLM
1606 zone_idx = zbd_zone_idx(f, io_u->offset);
1607 assert(zone_idx < zbd_info->nr_zones);
923f7c1e 1608 z = get_zone(f, zone_idx);
d9ed3e63 1609
43bcbd5b 1610 assert(z->has_wp);
d9ed3e63
DLM
1611
1612 dprint(FD_ZBD,
1613 "%s: terminate I/O (%lld, %llu) for zone %u\n",
1614 f->file_name, io_u->offset, io_u->buflen, zone_idx);
1615
b2da58c4
SK
1616 zbd_end_zone_io(td, io_u, z);
1617
4d4c71e6 1618 zone_unlock(z);
11afb212 1619 zbd_check_swd(td, f);
bfbdd35b
BVA
1620}
1621
9d87c646
DLM
1622/*
1623 * Windows and MacOS do not define this.
1624 */
1625#ifndef EREMOTEIO
1626#define EREMOTEIO 121 /* POSIX value */
1627#endif
1628
bfbdd35b
BVA
1629bool zbd_unaligned_write(int error_code)
1630{
1631 switch (error_code) {
1632 case EIO:
1633 case EREMOTEIO:
1634 return true;
1635 }
1636 return false;
1637}
1638
4d37720a
DLM
1639/**
1640 * setup_zbd_zone_mode - handle zoneskip as necessary for ZBD drives
1641 * @td: FIO thread data.
1642 * @io_u: FIO I/O unit.
1643 *
1644 * For sequential workloads, change the file offset to skip zoneskip bytes when
1645 * no more IO can be performed in the current zone.
1646 * - For read workloads, zoneskip is applied when the io has reached the end of
1647 * the zone or the zone write position (when td->o.read_beyond_wp is false).
1648 * - For write workloads, zoneskip is applied when the zone is full.
1649 * This applies only to read and write operations.
1650 */
1651void setup_zbd_zone_mode(struct thread_data *td, struct io_u *io_u)
1652{
1653 struct fio_file *f = io_u->file;
1654 enum fio_ddir ddir = io_u->ddir;
1655 struct fio_zone_info *z;
1656 uint32_t zone_idx;
1657
1658 assert(td->o.zone_mode == ZONE_MODE_ZBD);
1659 assert(td->o.zone_size);
5ddf46d0 1660 assert(f->zbd_info);
4d37720a 1661
236d23a8 1662 zone_idx = zbd_zone_idx(f, f->last_pos[ddir]);
923f7c1e 1663 z = get_zone(f, zone_idx);
236d23a8
SK
1664
1665 /*
1666 * When the zone capacity is smaller than the zone size and the I/O is
1667 * sequential write, skip to zone end if the latest position is at the
1668 * zone capacity limit.
1669 */
1670 if (z->capacity < f->zbd_info->zone_size && !td_random(td) &&
1671 ddir == DDIR_WRITE &&
1672 f->last_pos[ddir] >= zbd_zone_capacity_end(z)) {
1673 dprint(FD_ZBD,
1674 "%s: Jump from zone capacity limit to zone end:"
5090d1d0
JA
1675 " (%llu -> %llu) for zone %u (%llu)\n",
1676 f->file_name, (unsigned long long) f->last_pos[ddir],
122c3539 1677 (unsigned long long) zbd_zone_end(z), zone_idx,
5090d1d0 1678 (unsigned long long) z->capacity);
236d23a8
SK
1679 td->io_skip_bytes += zbd_zone_end(z) - f->last_pos[ddir];
1680 f->last_pos[ddir] = zbd_zone_end(z);
1681 }
1682
4d37720a
DLM
1683 /*
1684 * zone_skip is valid only for sequential workloads.
1685 */
1686 if (td_random(td) || !td->o.zone_skip)
1687 return;
1688
1689 /*
1690 * It is time to switch to a new zone if:
1691 * - zone_bytes == zone_size bytes have already been accessed
1692 * - The last position reached the end of the current zone.
1693 * - For reads with td->o.read_beyond_wp == false, the last position
1694 * reached the zone write pointer.
1695 */
4d37720a 1696 if (td->zone_bytes >= td->o.zone_size ||
236d23a8 1697 f->last_pos[ddir] >= zbd_zone_end(z) ||
4d37720a
DLM
1698 (ddir == DDIR_READ &&
1699 (!td->o.read_beyond_wp) && f->last_pos[ddir] >= z->wp)) {
1700 /*
1701 * Skip zones.
1702 */
1703 td->zone_bytes = 0;
1704 f->file_offset += td->o.zone_size + td->o.zone_skip;
1705
1706 /*
1707 * Wrap from the beginning, if we exceed the file size
1708 */
1709 if (f->file_offset >= f->real_file_size)
1710 f->file_offset = get_start_offset(td, f);
1711
1712 f->last_pos[ddir] = f->file_offset;
1713 td->io_skip_bytes += td->o.zone_skip;
1714 }
1715}
1716
c65057f9 1717/**
c7d5e152 1718 * zbd_adjust_ddir - Adjust an I/O direction for zonemode=zbd.
c65057f9
SK
1719 *
1720 * @td: FIO thread data.
1721 * @io_u: FIO I/O unit.
1722 * @ddir: I/O direction before adjustment.
1723 *
1724 * Return adjusted I/O direction.
1725 */
1726enum fio_ddir zbd_adjust_ddir(struct thread_data *td, struct io_u *io_u,
1727 enum fio_ddir ddir)
1728{
1729 /*
1730 * In case read direction is chosen for the first random I/O, fio with
1731 * zonemode=zbd stops because no data can be read from zoned block
1732 * devices with all empty zones. Overwrite the first I/O direction as
1733 * write to make sure data to read exists.
1734 */
5ddf46d0 1735 assert(io_u->file->zbd_info);
731461cc 1736 if (ddir != DDIR_READ || !td_rw(td))
c65057f9
SK
1737 return ddir;
1738
1739 if (io_u->file->zbd_info->sectors_with_data ||
1740 td->o.read_beyond_wp)
1741 return DDIR_READ;
1742
1743 return DDIR_WRITE;
1744}
1745
bfbdd35b
BVA
1746/**
1747 * zbd_adjust_block - adjust the offset and length as necessary for ZBD drives
1748 * @td: FIO thread data.
1749 * @io_u: FIO I/O unit.
1750 *
1751 * Locking strategy: returns with z->mutex locked if and only if z refers
1752 * to a sequential zone and if io_u_accept is returned. z is the zone that
1753 * corresponds to io_u->offset at the end of this function.
1754 */
1755enum io_u_action zbd_adjust_block(struct thread_data *td, struct io_u *io_u)
1756{
b7694961 1757 struct fio_file *f = io_u->file;
af94a8c3 1758 struct zoned_block_device_info *zbdi = f->zbd_info;
bfbdd35b 1759 uint32_t zone_idx_b;
de65f7b7 1760 struct fio_zone_info *zb, *zl, *orig_zb;
bfbdd35b
BVA
1761 uint32_t orig_len = io_u->buflen;
1762 uint32_t min_bs = td->o.min_bs[io_u->ddir];
1763 uint64_t new_len;
1764 int64_t range;
1765
af94a8c3 1766 assert(zbdi);
adc6adcb 1767 assert(min_bs);
bfbdd35b
BVA
1768 assert(is_valid_offset(f, io_u->offset));
1769 assert(io_u->buflen);
1770 zone_idx_b = zbd_zone_idx(f, io_u->offset);
923f7c1e 1771 zb = get_zone(f, zone_idx_b);
de65f7b7 1772 orig_zb = zb;
bfbdd35b 1773
2efcf74b
SK
1774 if (!zb->has_wp) {
1775 /* Accept non-write I/Os for conventional zones. */
1776 if (io_u->ddir != DDIR_WRITE)
1777 return io_u_accept;
1778 /*
1779 * Make sure that writes to conventional zones
1780 * don't cross over to any sequential zones.
1781 */
1782 if (!(zb + 1)->has_wp ||
1783 io_u->offset + io_u->buflen <= (zb + 1)->start)
1784 return io_u_accept;
1785
1786 if (io_u->offset + min_bs > (zb + 1)->start) {
1787 dprint(FD_IO,
1e3d6e03
JA
1788 "%s: off=%llu + min_bs=%u > next zone %llu\n",
1789 f->file_name, io_u->offset,
1790 min_bs, (unsigned long long) (zb + 1)->start);
2efcf74b
SK
1791 io_u->offset = zb->start + (zb + 1)->start - io_u->offset;
1792 new_len = min(io_u->buflen, (zb + 1)->start - io_u->offset);
1793 } else {
1794 new_len = (zb + 1)->start - io_u->offset;
1795 }
1796 io_u->buflen = new_len / min_bs * min_bs;
bfbdd35b 1797 return io_u_accept;
2efcf74b 1798 }
bfbdd35b
BVA
1799
1800 /*
1801 * Accept the I/O offset for reads if reading beyond the write pointer
1802 * is enabled.
1803 */
b7694961 1804 if (zb->cond != ZBD_ZONE_COND_OFFLINE &&
bfbdd35b
BVA
1805 io_u->ddir == DDIR_READ && td->o.read_beyond_wp)
1806 return io_u_accept;
1807
11afb212 1808 zbd_check_swd(td, f);
615555bb 1809
fae3b9a0 1810 zone_lock(td, f, zb);
6f0c6085 1811
bfbdd35b
BVA
1812 switch (io_u->ddir) {
1813 case DDIR_READ:
0ed7d55e
AR
1814 if (td->runstate == TD_VERIFYING && td_write(td)) {
1815 zb = zbd_replay_write_order(td, io_u, zb);
bfbdd35b
BVA
1816 goto accept;
1817 }
1818 /*
de65f7b7
DLM
1819 * Check that there is enough written data in the zone to do an
1820 * I/O of at least min_bs B. If there isn't, find a new zone for
1821 * the I/O.
bfbdd35b 1822 */
b7694961 1823 range = zb->cond != ZBD_ZONE_COND_OFFLINE ?
ee3696bd 1824 zb->wp - zb->start : 0;
de65f7b7 1825 if (range < min_bs ||
ee3696bd 1826 ((!td_random(td)) && (io_u->offset + min_bs > zb->wp))) {
4d4c71e6 1827 zone_unlock(zb);
923f7c1e 1828 zl = get_zone(f, f->max_zone);
5c86fdf6 1829 zb = zbd_find_zone(td, io_u, min_bs, zb, zl);
bfbdd35b
BVA
1830 if (!zb) {
1831 dprint(FD_ZBD,
1832 "%s: zbd_find_zone(%lld, %llu) failed\n",
1833 f->file_name, io_u->offset,
1834 io_u->buflen);
1835 goto eof;
1836 }
de65f7b7
DLM
1837 /*
1838 * zbd_find_zone() returned a zone with a range of at
1839 * least min_bs.
1840 */
ee3696bd 1841 range = zb->wp - zb->start;
de65f7b7
DLM
1842 assert(range >= min_bs);
1843
1844 if (!td_random(td))
ee3696bd 1845 io_u->offset = zb->start;
bfbdd35b 1846 }
de65f7b7
DLM
1847 /*
1848 * Make sure the I/O is within the zone valid data range while
1849 * maximizing the I/O size and preserving randomness.
1850 */
1851 if (range <= io_u->buflen)
ee3696bd 1852 io_u->offset = zb->start;
de65f7b7 1853 else if (td_random(td))
ee3696bd
DLM
1854 io_u->offset = zb->start +
1855 ((io_u->offset - orig_zb->start) %
de65f7b7 1856 (range - io_u->buflen)) / min_bs * min_bs;
43bcbd5b
SK
1857 /*
1858 * When zbd_find_zone() returns a conventional zone,
1859 * we can simply accept the new i/o offset here.
1860 */
1861 if (!zb->has_wp)
1862 return io_u_accept;
de65f7b7
DLM
1863 /*
1864 * Make sure the I/O does not cross over the zone wp position.
1865 */
1866 new_len = min((unsigned long long)io_u->buflen,
ee3696bd 1867 (unsigned long long)(zb->wp - io_u->offset));
de65f7b7
DLM
1868 new_len = new_len / min_bs * min_bs;
1869 if (new_len < io_u->buflen) {
1870 io_u->buflen = new_len;
1871 dprint(FD_IO, "Changed length from %u into %llu\n",
1872 orig_len, io_u->buflen);
bfbdd35b 1873 }
ee3696bd
DLM
1874 assert(zb->start <= io_u->offset);
1875 assert(io_u->offset + io_u->buflen <= zb->wp);
bfbdd35b
BVA
1876 goto accept;
1877 case DDIR_WRITE:
af94a8c3 1878 if (io_u->buflen > zbdi->zone_size) {
1c74aadc
DF
1879 td_verror(td, EINVAL, "I/O buflen exceeds zone size");
1880 dprint(FD_IO,
1e3d6e03 1881 "%s: I/O buflen %llu exceeds zone size %llu\n",
1c74aadc 1882 f->file_name, io_u->buflen,
af94a8c3 1883 (unsigned long long) zbdi->zone_size);
bfbdd35b 1884 goto eof;
1c74aadc 1885 }
954217b9 1886 if (!zbd_open_zone(td, f, zone_idx_b)) {
4d4c71e6 1887 zone_unlock(zb);
59b07544 1888 zb = zbd_convert_to_open_zone(td, io_u);
1c74aadc
DF
1889 if (!zb) {
1890 dprint(FD_IO, "%s: can't convert to open zone",
1891 f->file_name);
59b07544 1892 goto eof;
1c74aadc 1893 }
59b07544 1894 }
a7c2b6fc
BVA
1895 /* Check whether the zone reset threshold has been exceeded */
1896 if (td->o.zrf.u.f) {
af94a8c3 1897 if (zbdi->wp_sectors_with_data >=
a7c2b6fc
BVA
1898 f->io_size * td->o.zrt.u.f &&
1899 zbd_dec_and_reset_write_cnt(td, f)) {
1900 zb->reset_zone = 1;
1901 }
1902 }
bfbdd35b
BVA
1903 /* Reset the zone pointer if necessary */
1904 if (zb->reset_zone || zbd_zone_full(f, zb, min_bs)) {
1905 assert(td->o.verify == VERIFY_NONE);
1906 /*
1907 * Since previous write requests may have been submitted
1908 * asynchronously and since we will submit the zone
1909 * reset synchronously, wait until previously submitted
1910 * write requests have completed before issuing a
1911 * zone reset.
1912 */
1913 io_u_quiesce(td);
1914 zb->reset_zone = 0;
1915 if (zbd_reset_zone(td, f, zb) < 0)
1916 goto eof;
236d23a8
SK
1917
1918 if (zb->capacity < min_bs) {
1c74aadc 1919 td_verror(td, EINVAL, "ZCAP is less min_bs");
236d23a8
SK
1920 log_err("zone capacity %llu smaller than minimum block size %d\n",
1921 (unsigned long long)zb->capacity,
1922 min_bs);
1923 goto eof;
1924 }
bfbdd35b
BVA
1925 }
1926 /* Make writes occur at the write pointer */
1927 assert(!zbd_zone_full(f, zb, min_bs));
ee3696bd 1928 io_u->offset = zb->wp;
bfbdd35b 1929 if (!is_valid_offset(f, io_u->offset)) {
1c74aadc
DF
1930 td_verror(td, EINVAL, "invalid WP value");
1931 dprint(FD_ZBD, "%s: dropped request with offset %llu\n",
1932 f->file_name, io_u->offset);
bfbdd35b
BVA
1933 goto eof;
1934 }
1935 /*
1936 * Make sure that the buflen is a multiple of the minimal
1937 * block size. Give up if shrinking would make the request too
1938 * small.
1939 */
1940 new_len = min((unsigned long long)io_u->buflen,
236d23a8 1941 zbd_zone_capacity_end(zb) - io_u->offset);
bfbdd35b
BVA
1942 new_len = new_len / min_bs * min_bs;
1943 if (new_len == io_u->buflen)
1944 goto accept;
1945 if (new_len >= min_bs) {
1946 io_u->buflen = new_len;
1947 dprint(FD_IO, "Changed length from %u into %llu\n",
1948 orig_len, io_u->buflen);
1949 goto accept;
1950 }
1c74aadc
DF
1951 td_verror(td, EIO, "zone remainder too small");
1952 log_err("zone remainder %lld smaller than min block size %d\n",
1953 (zbd_zone_capacity_end(zb) - io_u->offset), min_bs);
bfbdd35b
BVA
1954 goto eof;
1955 case DDIR_TRIM:
e3be810b
SK
1956 /* Check random trim targets a non-empty zone */
1957 if (!td_random(td) || zb->wp > zb->start)
1958 goto accept;
1959
1960 /* Find out a non-empty zone to trim */
1961 zone_unlock(zb);
1962 zl = get_zone(f, f->max_zone);
1963 zb = zbd_find_zone(td, io_u, 1, zb, zl);
1964 if (zb) {
1965 io_u->offset = zb->start;
1966 dprint(FD_ZBD, "%s: found new zone(%lld) for trim\n",
1967 f->file_name, io_u->offset);
1968 goto accept;
1969 }
1970 goto eof;
bfbdd35b 1971 case DDIR_SYNC:
e3be810b 1972 /* fall-through */
bfbdd35b
BVA
1973 case DDIR_DATASYNC:
1974 case DDIR_SYNC_FILE_RANGE:
1975 case DDIR_WAIT:
1976 case DDIR_LAST:
1977 case DDIR_INVAL:
1978 goto accept;
1979 }
1980
1981 assert(false);
1982
1983accept:
43bcbd5b 1984 assert(zb->has_wp);
b7694961 1985 assert(zb->cond != ZBD_ZONE_COND_OFFLINE);
d9ed3e63
DLM
1986 assert(!io_u->zbd_queue_io);
1987 assert(!io_u->zbd_put_io);
1988 io_u->zbd_queue_io = zbd_queue_io;
1989 io_u->zbd_put_io = zbd_put_io;
2ef3c1b0
DF
1990 /*
1991 * Since we return with the zone lock still held,
1992 * add an annotation to let Coverity know that it
1993 * is intentional.
1994 */
1995 /* coverity[missing_unlock] */
bfbdd35b
BVA
1996 return io_u_accept;
1997
1998eof:
43bcbd5b 1999 if (zb && zb->has_wp)
4d4c71e6 2000 zone_unlock(zb);
bfbdd35b
BVA
2001 return io_u_eof;
2002}
fd5d733f
BVA
2003
2004/* Return a string with ZBD statistics */
2005char *zbd_write_status(const struct thread_stat *ts)
2006{
2007 char *res;
2008
a0c84dd4 2009 if (asprintf(&res, "; %llu zone resets", (unsigned long long) ts->nr_zone_resets) < 0)
fd5d733f
BVA
2010 return NULL;
2011 return res;
2012}
e3be810b
SK
2013
2014/**
2015 * zbd_do_io_u_trim - If reset zone is applicable, do reset zone instead of trim
2016 *
2017 * @td: FIO thread data.
2018 * @io_u: FIO I/O unit.
2019 *
2020 * It is assumed that z->mutex is already locked.
2021 * Return io_u_completed when reset zone succeeds. Return 0 when the target zone
2022 * does not have write pointer. On error, return negative errno.
2023 */
2024int zbd_do_io_u_trim(const struct thread_data *td, struct io_u *io_u)
2025{
2026 struct fio_file *f = io_u->file;
2027 struct fio_zone_info *z;
2028 uint32_t zone_idx;
2029 int ret;
2030
2031 zone_idx = zbd_zone_idx(f, io_u->offset);
2032 z = get_zone(f, zone_idx);
2033
2034 if (!z->has_wp)
2035 return 0;
2036
2037 if (io_u->offset != z->start) {
2038 log_err("Trim offset not at zone start (%lld)\n", io_u->offset);
2039 return -EINVAL;
2040 }
2041
2042 ret = zbd_reset_zone((struct thread_data *)td, f, z);
2043 if (ret < 0)
2044 return ret;
2045
2046 return io_u_completed;
2047}