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