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