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