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