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