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