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