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