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