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