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> | |
10 | #include <dirent.h> | |
11 | #include <fcntl.h> | |
bfbdd35b BVA |
12 | #include <sys/stat.h> |
13 | #include <unistd.h> | |
f5bff36e | 14 | |
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" | |
22 | #include "zbd.h" | |
23 | ||
b7694961 DLM |
24 | /** |
25 | * zbd_get_zoned_model - Get a device zoned model | |
26 | * @td: FIO thread data | |
27 | * @f: FIO file for which to get model information | |
28 | */ | |
29 | int zbd_get_zoned_model(struct thread_data *td, struct fio_file *f, | |
30 | enum zbd_zoned_model *model) | |
31 | { | |
32 | int ret; | |
33 | ||
34 | ret = blkzoned_get_zoned_model(td, f, model); | |
35 | if (ret < 0) { | |
36 | td_verror(td, errno, "get zoned model failed"); | |
37 | log_err("%s: get zoned model failed (%d).\n", | |
38 | f->file_name, errno); | |
39 | } | |
40 | ||
41 | return ret; | |
42 | } | |
43 | ||
44 | /** | |
45 | * zbd_report_zones - Get zone information | |
46 | * @td: FIO thread data. | |
47 | * @f: FIO file for which to get zone information | |
48 | * @offset: offset from which to report zones | |
49 | * @zones: Array of struct zbd_zone | |
50 | * @nr_zones: Size of @zones array | |
51 | * | |
52 | * Get zone information into @zones starting from the zone at offset @offset | |
53 | * for the device specified by @f. | |
54 | * | |
55 | * Returns the number of zones reported upon success and a negative error code | |
56 | * upon failure. If the zone report is empty, always assume an error (device | |
57 | * problem) and return -EIO. | |
58 | */ | |
59 | int zbd_report_zones(struct thread_data *td, struct fio_file *f, | |
60 | uint64_t offset, struct zbd_zone *zones, | |
61 | unsigned int nr_zones) | |
62 | { | |
63 | int ret; | |
64 | ||
65 | ret = blkzoned_report_zones(td, f, offset, zones, nr_zones); | |
66 | if (ret < 0) { | |
67 | td_verror(td, errno, "report zones failed"); | |
68 | log_err("%s: report zones from sector %llu failed (%d).\n", | |
69 | f->file_name, (unsigned long long)offset >> 9, errno); | |
70 | } else if (ret == 0) { | |
71 | td_verror(td, errno, "Empty zone report"); | |
72 | log_err("%s: report zones from sector %llu is empty.\n", | |
73 | f->file_name, (unsigned long long)offset >> 9); | |
74 | ret = -EIO; | |
75 | } | |
76 | ||
77 | return ret; | |
78 | } | |
79 | ||
80 | /** | |
81 | * zbd_reset_wp - reset the write pointer of a range of zones | |
82 | * @td: FIO thread data. | |
83 | * @f: FIO file for which to reset zones | |
84 | * @offset: Starting offset of the first zone to reset | |
85 | * @length: Length of the range of zones to reset | |
86 | * | |
87 | * Reset the write pointer of all zones in the range @offset...@offset+@length. | |
88 | * Returns 0 upon success and a negative error code upon failure. | |
89 | */ | |
90 | int zbd_reset_wp(struct thread_data *td, struct fio_file *f, | |
91 | uint64_t offset, uint64_t length) | |
92 | { | |
93 | int ret; | |
94 | ||
95 | ret = blkzoned_reset_wp(td, f, offset, length); | |
96 | if (ret < 0) { | |
97 | td_verror(td, errno, "resetting wp failed"); | |
98 | log_err("%s: resetting wp for %llu sectors at sector %llu failed (%d).\n", | |
99 | f->file_name, (unsigned long long)length >> 9, | |
100 | (unsigned long long)offset >> 9, errno); | |
101 | } | |
102 | ||
103 | return ret; | |
104 | } | |
105 | ||
bfbdd35b BVA |
106 | /** |
107 | * zbd_zone_idx - convert an offset into a zone number | |
108 | * @f: file pointer. | |
109 | * @offset: offset in bytes. If this offset is in the first zone_size bytes | |
110 | * past the disk size then the index of the sentinel is returned. | |
111 | */ | |
112 | static uint32_t zbd_zone_idx(const struct fio_file *f, uint64_t offset) | |
113 | { | |
114 | uint32_t zone_idx; | |
115 | ||
cd775e06 | 116 | if (f->zbd_info->zone_size_log2 > 0) |
bfbdd35b BVA |
117 | zone_idx = offset >> f->zbd_info->zone_size_log2; |
118 | else | |
ee3696bd | 119 | zone_idx = offset / f->zbd_info->zone_size; |
bfbdd35b BVA |
120 | |
121 | return min(zone_idx, f->zbd_info->nr_zones); | |
122 | } | |
123 | ||
b7694961 DLM |
124 | /** |
125 | * zbd_zone_swr - Test whether a zone requires sequential writes | |
126 | * @z: zone info pointer. | |
127 | */ | |
128 | static inline bool zbd_zone_swr(struct fio_zone_info *z) | |
129 | { | |
130 | return z->type == ZBD_ZONE_TYPE_SWR; | |
131 | } | |
132 | ||
bfbdd35b BVA |
133 | /** |
134 | * zbd_zone_full - verify whether a minimum number of bytes remain in a zone | |
135 | * @f: file pointer. | |
136 | * @z: zone info pointer. | |
137 | * @required: minimum number of bytes that must remain in a zone. | |
138 | * | |
139 | * The caller must hold z->mutex. | |
140 | */ | |
141 | static bool zbd_zone_full(const struct fio_file *f, struct fio_zone_info *z, | |
142 | uint64_t required) | |
143 | { | |
144 | assert((required & 511) == 0); | |
145 | ||
b7694961 | 146 | return zbd_zone_swr(z) && |
ee3696bd | 147 | z->wp + required > z->start + f->zbd_info->zone_size; |
bfbdd35b BVA |
148 | } |
149 | ||
1f57803b DLM |
150 | static void zone_lock(struct thread_data *td, struct fio_zone_info *z) |
151 | { | |
152 | /* | |
153 | * Lock the io_u target zone. The zone will be unlocked if io_u offset | |
154 | * is changed or when io_u completes and zbd_put_io() executed. | |
155 | * To avoid multiple jobs doing asynchronous I/Os from deadlocking each | |
156 | * other waiting for zone locks when building an io_u batch, first | |
157 | * only trylock the zone. If the zone is already locked by another job, | |
158 | * process the currently queued I/Os so that I/O progress is made and | |
159 | * zones unlocked. | |
160 | */ | |
161 | if (pthread_mutex_trylock(&z->mutex) != 0) { | |
162 | if (!td_ioengine_flagged(td, FIO_SYNCIO)) | |
163 | io_u_quiesce(td); | |
164 | pthread_mutex_lock(&z->mutex); | |
165 | } | |
166 | } | |
167 | ||
bfbdd35b BVA |
168 | static bool is_valid_offset(const struct fio_file *f, uint64_t offset) |
169 | { | |
170 | return (uint64_t)(offset - f->file_offset) < f->io_size; | |
171 | } | |
172 | ||
173 | /* Verify whether direct I/O is used for all host-managed zoned drives. */ | |
174 | static bool zbd_using_direct_io(void) | |
175 | { | |
176 | struct thread_data *td; | |
177 | struct fio_file *f; | |
178 | int i, j; | |
179 | ||
180 | for_each_td(td, i) { | |
181 | if (td->o.odirect || !(td->o.td_ddir & TD_DDIR_WRITE)) | |
182 | continue; | |
183 | for_each_file(td, f, j) { | |
184 | if (f->zbd_info && | |
b7694961 | 185 | f->zbd_info->model == ZBD_HOST_MANAGED) |
bfbdd35b BVA |
186 | return false; |
187 | } | |
188 | } | |
189 | ||
190 | return true; | |
191 | } | |
192 | ||
193 | /* Whether or not the I/O range for f includes one or more sequential zones */ | |
194 | static bool zbd_is_seq_job(struct fio_file *f) | |
195 | { | |
196 | uint32_t zone_idx, zone_idx_b, zone_idx_e; | |
197 | ||
198 | assert(f->zbd_info); | |
199 | if (f->io_size == 0) | |
200 | return false; | |
201 | zone_idx_b = zbd_zone_idx(f, f->file_offset); | |
202 | zone_idx_e = zbd_zone_idx(f, f->file_offset + f->io_size - 1); | |
203 | for (zone_idx = zone_idx_b; zone_idx <= zone_idx_e; zone_idx++) | |
b7694961 | 204 | if (zbd_zone_swr(&f->zbd_info->zone_info[zone_idx])) |
bfbdd35b BVA |
205 | return true; |
206 | ||
207 | return false; | |
208 | } | |
209 | ||
210 | /* | |
211 | * Verify whether offset and size parameters are aligned with zone boundaries. | |
212 | */ | |
213 | static bool zbd_verify_sizes(void) | |
214 | { | |
215 | const struct fio_zone_info *z; | |
216 | struct thread_data *td; | |
217 | struct fio_file *f; | |
218 | uint64_t new_offset, new_end; | |
219 | uint32_t zone_idx; | |
220 | int i, j; | |
221 | ||
222 | for_each_td(td, i) { | |
223 | for_each_file(td, f, j) { | |
224 | if (!f->zbd_info) | |
225 | continue; | |
226 | if (f->file_offset >= f->real_file_size) | |
227 | continue; | |
228 | if (!zbd_is_seq_job(f)) | |
229 | continue; | |
4d37720a | 230 | |
fc52e449 DLM |
231 | if (!td->o.zone_size) { |
232 | td->o.zone_size = f->zbd_info->zone_size; | |
233 | if (!td->o.zone_size) { | |
234 | log_err("%s: invalid 0 zone size\n", | |
235 | f->file_name); | |
236 | return false; | |
237 | } | |
971d6a22 DLM |
238 | } else if (td->o.zone_size != f->zbd_info->zone_size) { |
239 | log_err("%s: job parameter zonesize %llu does not match disk zone size %llu.\n", | |
240 | f->file_name, (unsigned long long) td->o.zone_size, | |
241 | (unsigned long long) f->zbd_info->zone_size); | |
242 | return false; | |
fc52e449 DLM |
243 | } |
244 | ||
4d37720a DLM |
245 | if (td->o.zone_skip && |
246 | (td->o.zone_skip < td->o.zone_size || | |
247 | td->o.zone_skip % td->o.zone_size)) { | |
248 | log_err("%s: zoneskip %llu is not a multiple of the device zone size %llu.\n", | |
249 | f->file_name, (unsigned long long) td->o.zone_skip, | |
250 | (unsigned long long) td->o.zone_size); | |
251 | return false; | |
252 | } | |
253 | ||
bfbdd35b BVA |
254 | zone_idx = zbd_zone_idx(f, f->file_offset); |
255 | z = &f->zbd_info->zone_info[zone_idx]; | |
ee3696bd DLM |
256 | if (f->file_offset != z->start) { |
257 | new_offset = (z+1)->start; | |
bfbdd35b BVA |
258 | if (new_offset >= f->file_offset + f->io_size) { |
259 | log_info("%s: io_size must be at least one zone\n", | |
260 | f->file_name); | |
261 | return false; | |
262 | } | |
a0c84dd4 JA |
263 | log_info("%s: rounded up offset from %llu to %llu\n", |
264 | f->file_name, (unsigned long long) f->file_offset, | |
265 | (unsigned long long) new_offset); | |
bfbdd35b BVA |
266 | f->io_size -= (new_offset - f->file_offset); |
267 | f->file_offset = new_offset; | |
268 | } | |
269 | zone_idx = zbd_zone_idx(f, f->file_offset + f->io_size); | |
270 | z = &f->zbd_info->zone_info[zone_idx]; | |
ee3696bd | 271 | new_end = z->start; |
bfbdd35b BVA |
272 | if (f->file_offset + f->io_size != new_end) { |
273 | if (new_end <= f->file_offset) { | |
274 | log_info("%s: io_size must be at least one zone\n", | |
275 | f->file_name); | |
276 | return false; | |
277 | } | |
a0c84dd4 JA |
278 | log_info("%s: rounded down io_size from %llu to %llu\n", |
279 | f->file_name, (unsigned long long) f->io_size, | |
280 | (unsigned long long) new_end - f->file_offset); | |
bfbdd35b BVA |
281 | f->io_size = new_end - f->file_offset; |
282 | } | |
283 | } | |
284 | } | |
285 | ||
286 | return true; | |
287 | } | |
288 | ||
289 | static bool zbd_verify_bs(void) | |
290 | { | |
291 | struct thread_data *td; | |
292 | struct fio_file *f; | |
293 | uint32_t zone_size; | |
294 | int i, j, k; | |
295 | ||
296 | for_each_td(td, i) { | |
297 | for_each_file(td, f, j) { | |
298 | if (!f->zbd_info) | |
299 | continue; | |
300 | zone_size = f->zbd_info->zone_size; | |
301 | for (k = 0; k < ARRAY_SIZE(td->o.bs); k++) { | |
302 | if (td->o.verify != VERIFY_NONE && | |
ee3696bd | 303 | zone_size % td->o.bs[k] != 0) { |
bfbdd35b BVA |
304 | log_info("%s: block size %llu is not a divisor of the zone size %d\n", |
305 | f->file_name, td->o.bs[k], | |
ee3696bd | 306 | zone_size); |
bfbdd35b BVA |
307 | return false; |
308 | } | |
309 | } | |
310 | } | |
311 | } | |
312 | return true; | |
313 | } | |
314 | ||
bfbdd35b BVA |
315 | static int ilog2(uint64_t i) |
316 | { | |
317 | int log = -1; | |
318 | ||
319 | while (i) { | |
320 | i >>= 1; | |
321 | log++; | |
322 | } | |
323 | return log; | |
324 | } | |
325 | ||
326 | /* | |
327 | * Initialize f->zbd_info for devices that are not zoned block devices. This | |
328 | * allows to execute a ZBD workload against a non-ZBD device. | |
329 | */ | |
330 | static int init_zone_info(struct thread_data *td, struct fio_file *f) | |
331 | { | |
332 | uint32_t nr_zones; | |
333 | struct fio_zone_info *p; | |
a4b7f12b | 334 | uint64_t zone_size = td->o.zone_size; |
bfbdd35b BVA |
335 | struct zoned_block_device_info *zbd_info = NULL; |
336 | pthread_mutexattr_t attr; | |
337 | int i; | |
338 | ||
a4b7f12b DLM |
339 | if (zone_size == 0) { |
340 | log_err("%s: Specifying the zone size is mandatory for regular block devices with --zonemode=zbd\n\n", | |
341 | f->file_name); | |
342 | return 1; | |
343 | } | |
344 | ||
345 | if (zone_size < 512) { | |
346 | log_err("%s: zone size must be at least 512 bytes for --zonemode=zbd\n\n", | |
347 | f->file_name); | |
348 | return 1; | |
349 | } | |
350 | ||
ee3696bd | 351 | nr_zones = (f->real_file_size + zone_size - 1) / zone_size; |
bfbdd35b BVA |
352 | zbd_info = scalloc(1, sizeof(*zbd_info) + |
353 | (nr_zones + 1) * sizeof(zbd_info->zone_info[0])); | |
354 | if (!zbd_info) | |
355 | return -ENOMEM; | |
356 | ||
357 | pthread_mutexattr_init(&attr); | |
358 | pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); | |
359 | pthread_mutexattr_setpshared(&attr, true); | |
360 | pthread_mutex_init(&zbd_info->mutex, &attr); | |
361 | zbd_info->refcount = 1; | |
362 | p = &zbd_info->zone_info[0]; | |
363 | for (i = 0; i < nr_zones; i++, p++) { | |
364 | pthread_mutex_init(&p->mutex, &attr); | |
365 | p->start = i * zone_size; | |
366 | p->wp = p->start + zone_size; | |
b7694961 DLM |
367 | p->type = ZBD_ZONE_TYPE_SWR; |
368 | p->cond = ZBD_ZONE_COND_EMPTY; | |
bfbdd35b BVA |
369 | } |
370 | /* a sentinel */ | |
371 | p->start = nr_zones * zone_size; | |
372 | ||
373 | f->zbd_info = zbd_info; | |
374 | f->zbd_info->zone_size = zone_size; | |
375 | f->zbd_info->zone_size_log2 = is_power_of_2(zone_size) ? | |
ebc403fe | 376 | ilog2(zone_size) : 0; |
bfbdd35b BVA |
377 | f->zbd_info->nr_zones = nr_zones; |
378 | pthread_mutexattr_destroy(&attr); | |
379 | return 0; | |
380 | } | |
381 | ||
382 | /* | |
b7694961 DLM |
383 | * Maximum number of zones to report in one operation. |
384 | */ | |
385 | #define ZBD_REPORT_MAX_ZONES 8192U | |
386 | ||
387 | /* | |
388 | * Parse the device zone report and store it in f->zbd_info. Must be called | |
389 | * only for devices that are zoned, namely those with a model != ZBD_NONE. | |
bfbdd35b BVA |
390 | */ |
391 | static int parse_zone_info(struct thread_data *td, struct fio_file *f) | |
392 | { | |
b7694961 DLM |
393 | int nr_zones, nrz; |
394 | struct zbd_zone *zones, *z; | |
bfbdd35b | 395 | struct fio_zone_info *p; |
b7694961 | 396 | uint64_t zone_size, offset; |
bfbdd35b BVA |
397 | struct zoned_block_device_info *zbd_info = NULL; |
398 | pthread_mutexattr_t attr; | |
b7694961 | 399 | int i, j, ret = 0; |
bfbdd35b BVA |
400 | |
401 | pthread_mutexattr_init(&attr); | |
402 | pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); | |
403 | pthread_mutexattr_setpshared(&attr, true); | |
404 | ||
b7694961 DLM |
405 | zones = calloc(ZBD_REPORT_MAX_ZONES, sizeof(struct zbd_zone)); |
406 | if (!zones) | |
bfbdd35b BVA |
407 | goto out; |
408 | ||
b7694961 DLM |
409 | nrz = zbd_report_zones(td, f, 0, zones, ZBD_REPORT_MAX_ZONES); |
410 | if (nrz < 0) { | |
411 | ret = nrz; | |
412 | log_info("fio: report zones (offset 0) failed for %s (%d).\n", | |
413 | f->file_name, -ret); | |
414 | goto out; | |
bfbdd35b BVA |
415 | } |
416 | ||
b7694961 | 417 | zone_size = zones[0].len; |
ee3696bd | 418 | nr_zones = (f->real_file_size + zone_size - 1) / zone_size; |
bfbdd35b BVA |
419 | |
420 | if (td->o.zone_size == 0) { | |
ee3696bd DLM |
421 | td->o.zone_size = zone_size; |
422 | } else if (td->o.zone_size != zone_size) { | |
4580ff92 DLM |
423 | log_err("fio: %s job parameter zonesize %llu does not match disk zone size %llu.\n", |
424 | f->file_name, (unsigned long long) td->o.zone_size, | |
a0c84dd4 | 425 | (unsigned long long) zone_size); |
bfbdd35b | 426 | ret = -EINVAL; |
b7694961 | 427 | goto out; |
bfbdd35b BVA |
428 | } |
429 | ||
a0c84dd4 JA |
430 | dprint(FD_ZBD, "Device %s has %d zones of size %llu KB\n", f->file_name, |
431 | nr_zones, (unsigned long long) zone_size / 1024); | |
bfbdd35b BVA |
432 | |
433 | zbd_info = scalloc(1, sizeof(*zbd_info) + | |
434 | (nr_zones + 1) * sizeof(zbd_info->zone_info[0])); | |
435 | ret = -ENOMEM; | |
436 | if (!zbd_info) | |
b7694961 | 437 | goto out; |
bfbdd35b BVA |
438 | pthread_mutex_init(&zbd_info->mutex, &attr); |
439 | zbd_info->refcount = 1; | |
440 | p = &zbd_info->zone_info[0]; | |
b7694961 DLM |
441 | for (offset = 0, j = 0; j < nr_zones;) { |
442 | z = &zones[0]; | |
443 | for (i = 0; i < nrz; i++, j++, z++, p++) { | |
bfbdd35b | 444 | pthread_mutex_init(&p->mutex, &attr); |
b7694961 | 445 | p->start = z->start; |
bfbdd35b | 446 | switch (z->cond) { |
b7694961 DLM |
447 | case ZBD_ZONE_COND_NOT_WP: |
448 | case ZBD_ZONE_COND_FULL: | |
ee3696bd | 449 | p->wp = p->start + zone_size; |
bfbdd35b BVA |
450 | break; |
451 | default: | |
452 | assert(z->start <= z->wp); | |
b7694961 DLM |
453 | assert(z->wp <= z->start + zone_size); |
454 | p->wp = z->wp; | |
bfbdd35b BVA |
455 | break; |
456 | } | |
457 | p->type = z->type; | |
458 | p->cond = z->cond; | |
459 | if (j > 0 && p->start != p[-1].start + zone_size) { | |
460 | log_info("%s: invalid zone data\n", | |
461 | f->file_name); | |
462 | ret = -EINVAL; | |
b7694961 | 463 | goto out; |
bfbdd35b BVA |
464 | } |
465 | } | |
466 | z--; | |
b7694961 | 467 | offset = z->start + z->len; |
bfbdd35b BVA |
468 | if (j >= nr_zones) |
469 | break; | |
b7694961 DLM |
470 | nrz = zbd_report_zones(td, f, offset, |
471 | zones, ZBD_REPORT_MAX_ZONES); | |
472 | if (nrz < 0) { | |
473 | ret = nrz; | |
474 | log_info("fio: report zones (offset %llu) failed for %s (%d).\n", | |
475 | (unsigned long long)offset, | |
476 | f->file_name, -ret); | |
477 | goto out; | |
bfbdd35b BVA |
478 | } |
479 | } | |
b7694961 | 480 | |
bfbdd35b | 481 | /* a sentinel */ |
b7694961 | 482 | zbd_info->zone_info[nr_zones].start = offset; |
bfbdd35b BVA |
483 | |
484 | f->zbd_info = zbd_info; | |
485 | f->zbd_info->zone_size = zone_size; | |
486 | f->zbd_info->zone_size_log2 = is_power_of_2(zone_size) ? | |
ebc403fe | 487 | ilog2(zone_size) : 0; |
bfbdd35b BVA |
488 | f->zbd_info->nr_zones = nr_zones; |
489 | zbd_info = NULL; | |
490 | ret = 0; | |
491 | ||
bfbdd35b | 492 | out: |
b7694961 DLM |
493 | sfree(zbd_info); |
494 | free(zones); | |
bfbdd35b BVA |
495 | pthread_mutexattr_destroy(&attr); |
496 | return ret; | |
497 | } | |
498 | ||
499 | /* | |
500 | * Allocate zone information and store it into f->zbd_info if zonemode=zbd. | |
501 | * | |
502 | * Returns 0 upon success and a negative error code upon failure. | |
503 | */ | |
379e5f09 | 504 | static int zbd_create_zone_info(struct thread_data *td, struct fio_file *f) |
bfbdd35b | 505 | { |
b7694961 DLM |
506 | enum zbd_zoned_model zbd_model; |
507 | int ret; | |
bfbdd35b BVA |
508 | |
509 | assert(td->o.zone_mode == ZONE_MODE_ZBD); | |
510 | ||
b7694961 DLM |
511 | ret = zbd_get_zoned_model(td, f, &zbd_model); |
512 | if (ret) | |
513 | return ret; | |
514 | ||
bfbdd35b | 515 | switch (zbd_model) { |
b7694961 DLM |
516 | case ZBD_IGNORE: |
517 | return 0; | |
518 | case ZBD_HOST_AWARE: | |
519 | case ZBD_HOST_MANAGED: | |
bfbdd35b BVA |
520 | ret = parse_zone_info(td, f); |
521 | break; | |
b7694961 | 522 | case ZBD_NONE: |
bfbdd35b BVA |
523 | ret = init_zone_info(td, f); |
524 | break; | |
b7694961 DLM |
525 | default: |
526 | td_verror(td, EINVAL, "Unsupported zoned model"); | |
527 | log_err("Unsupported zoned model\n"); | |
528 | return -EINVAL; | |
bfbdd35b | 529 | } |
b7694961 | 530 | |
bfbdd35b BVA |
531 | if (ret == 0) |
532 | f->zbd_info->model = zbd_model; | |
533 | return ret; | |
534 | } | |
535 | ||
536 | void zbd_free_zone_info(struct fio_file *f) | |
537 | { | |
538 | uint32_t refcount; | |
539 | ||
540 | if (!f->zbd_info) | |
541 | return; | |
542 | ||
543 | pthread_mutex_lock(&f->zbd_info->mutex); | |
544 | refcount = --f->zbd_info->refcount; | |
545 | pthread_mutex_unlock(&f->zbd_info->mutex); | |
546 | ||
547 | assert((int32_t)refcount >= 0); | |
548 | if (refcount == 0) | |
549 | sfree(f->zbd_info); | |
550 | f->zbd_info = NULL; | |
551 | } | |
552 | ||
553 | /* | |
554 | * Initialize f->zbd_info. | |
555 | * | |
556 | * Returns 0 upon success and a negative error code upon failure. | |
557 | * | |
558 | * Note: this function can only work correctly if it is called before the first | |
559 | * fio fork() call. | |
560 | */ | |
561 | static int zbd_init_zone_info(struct thread_data *td, struct fio_file *file) | |
562 | { | |
563 | struct thread_data *td2; | |
564 | struct fio_file *f2; | |
565 | int i, j, ret; | |
566 | ||
567 | for_each_td(td2, i) { | |
568 | for_each_file(td2, f2, j) { | |
569 | if (td2 == td && f2 == file) | |
570 | continue; | |
571 | if (!f2->zbd_info || | |
572 | strcmp(f2->file_name, file->file_name) != 0) | |
573 | continue; | |
574 | file->zbd_info = f2->zbd_info; | |
575 | file->zbd_info->refcount++; | |
576 | return 0; | |
577 | } | |
578 | } | |
579 | ||
580 | ret = zbd_create_zone_info(td, file); | |
581 | if (ret < 0) | |
c5837eec | 582 | td_verror(td, -ret, "zbd_create_zone_info() failed"); |
bfbdd35b BVA |
583 | return ret; |
584 | } | |
585 | ||
586 | int zbd_init(struct thread_data *td) | |
587 | { | |
588 | struct fio_file *f; | |
589 | int i; | |
590 | ||
591 | for_each_file(td, f, i) { | |
a4b7f12b | 592 | if (zbd_init_zone_info(td, f)) |
bfbdd35b | 593 | return 1; |
bfbdd35b BVA |
594 | } |
595 | ||
596 | if (!zbd_using_direct_io()) { | |
597 | log_err("Using direct I/O is mandatory for writing to ZBD drives\n\n"); | |
598 | return 1; | |
599 | } | |
600 | ||
601 | if (!zbd_verify_sizes()) | |
602 | return 1; | |
603 | ||
604 | if (!zbd_verify_bs()) | |
605 | return 1; | |
606 | ||
607 | return 0; | |
608 | } | |
609 | ||
610 | /** | |
611 | * zbd_reset_range - reset zones for a range of sectors | |
612 | * @td: FIO thread data. | |
613 | * @f: Fio file for which to reset zones | |
614 | * @sector: Starting sector in units of 512 bytes | |
615 | * @nr_sectors: Number of sectors in units of 512 bytes | |
616 | * | |
617 | * Returns 0 upon success and a negative error code upon failure. | |
618 | */ | |
b7694961 | 619 | static int zbd_reset_range(struct thread_data *td, struct fio_file *f, |
ee3696bd | 620 | uint64_t offset, uint64_t length) |
bfbdd35b | 621 | { |
bfbdd35b BVA |
622 | uint32_t zone_idx_b, zone_idx_e; |
623 | struct fio_zone_info *zb, *ze, *z; | |
624 | int ret = 0; | |
625 | ||
626 | assert(f->fd != -1); | |
ee3696bd | 627 | assert(is_valid_offset(f, offset + length - 1)); |
b7694961 | 628 | |
bfbdd35b | 629 | switch (f->zbd_info->model) { |
b7694961 DLM |
630 | case ZBD_HOST_AWARE: |
631 | case ZBD_HOST_MANAGED: | |
632 | ret = zbd_reset_wp(td, f, offset, length); | |
633 | if (ret < 0) | |
bfbdd35b | 634 | return ret; |
bfbdd35b | 635 | break; |
b7694961 | 636 | default: |
bfbdd35b BVA |
637 | break; |
638 | } | |
639 | ||
ee3696bd | 640 | zone_idx_b = zbd_zone_idx(f, offset); |
bfbdd35b | 641 | zb = &f->zbd_info->zone_info[zone_idx_b]; |
ee3696bd | 642 | zone_idx_e = zbd_zone_idx(f, offset + length); |
bfbdd35b BVA |
643 | ze = &f->zbd_info->zone_info[zone_idx_e]; |
644 | for (z = zb; z < ze; z++) { | |
645 | pthread_mutex_lock(&z->mutex); | |
a7c2b6fc BVA |
646 | pthread_mutex_lock(&f->zbd_info->mutex); |
647 | f->zbd_info->sectors_with_data -= z->wp - z->start; | |
648 | pthread_mutex_unlock(&f->zbd_info->mutex); | |
bfbdd35b BVA |
649 | z->wp = z->start; |
650 | z->verify_block = 0; | |
651 | pthread_mutex_unlock(&z->mutex); | |
652 | } | |
653 | ||
fd5d733f BVA |
654 | td->ts.nr_zone_resets += ze - zb; |
655 | ||
bfbdd35b BVA |
656 | return ret; |
657 | } | |
658 | ||
a0c84dd4 JA |
659 | static unsigned int zbd_zone_nr(struct zoned_block_device_info *zbd_info, |
660 | struct fio_zone_info *zone) | |
661 | { | |
089ddd95 | 662 | return zone - zbd_info->zone_info; |
a0c84dd4 JA |
663 | } |
664 | ||
bfbdd35b BVA |
665 | /** |
666 | * zbd_reset_zone - reset the write pointer of a single zone | |
667 | * @td: FIO thread data. | |
668 | * @f: FIO file associated with the disk for which to reset a write pointer. | |
669 | * @z: Zone to reset. | |
670 | * | |
671 | * Returns 0 upon success and a negative error code upon failure. | |
672 | */ | |
b7694961 | 673 | static int zbd_reset_zone(struct thread_data *td, struct fio_file *f, |
bfbdd35b BVA |
674 | struct fio_zone_info *z) |
675 | { | |
a0c84dd4 JA |
676 | dprint(FD_ZBD, "%s: resetting wp of zone %u.\n", f->file_name, |
677 | zbd_zone_nr(f->zbd_info, z)); | |
d60be7d5 DLM |
678 | |
679 | return zbd_reset_range(td, f, z->start, (z+1)->start - z->start); | |
bfbdd35b BVA |
680 | } |
681 | ||
682 | /* | |
683 | * Reset a range of zones. Returns 0 upon success and 1 upon failure. | |
684 | * @td: fio thread data. | |
685 | * @f: fio file for which to reset zones | |
686 | * @zb: first zone to reset. | |
687 | * @ze: first zone not to reset. | |
688 | * @all_zones: whether to reset all zones or only those zones for which the | |
689 | * write pointer is not a multiple of td->o.min_bs[DDIR_WRITE]. | |
690 | */ | |
691 | static int zbd_reset_zones(struct thread_data *td, struct fio_file *f, | |
692 | struct fio_zone_info *const zb, | |
693 | struct fio_zone_info *const ze, bool all_zones) | |
694 | { | |
4803b841 | 695 | struct fio_zone_info *z; |
ee3696bd | 696 | const uint32_t min_bs = td->o.min_bs[DDIR_WRITE]; |
bfbdd35b BVA |
697 | bool reset_wp; |
698 | int res = 0; | |
699 | ||
a0c84dd4 JA |
700 | dprint(FD_ZBD, "%s: examining zones %u .. %u\n", f->file_name, |
701 | zbd_zone_nr(f->zbd_info, zb), zbd_zone_nr(f->zbd_info, ze)); | |
bfbdd35b BVA |
702 | assert(f->fd != -1); |
703 | for (z = zb; z < ze; z++) { | |
b7694961 | 704 | if (!zbd_zone_swr(z)) |
1f57803b DLM |
705 | continue; |
706 | zone_lock(td, z); | |
707 | reset_wp = all_zones ? z->wp != z->start : | |
708 | (td->o.td_ddir & TD_DDIR_WRITE) && | |
709 | z->wp % min_bs != 0; | |
710 | if (reset_wp) { | |
711 | dprint(FD_ZBD, "%s: resetting zone %u\n", | |
712 | f->file_name, | |
713 | zbd_zone_nr(f->zbd_info, z)); | |
714 | if (zbd_reset_zone(td, f, z) < 0) | |
715 | res = 1; | |
bfbdd35b | 716 | } |
bfbdd35b | 717 | pthread_mutex_unlock(&z->mutex); |
4803b841 | 718 | } |
bfbdd35b BVA |
719 | |
720 | return res; | |
721 | } | |
722 | ||
a7c2b6fc BVA |
723 | /* |
724 | * Reset zbd_info.write_cnt, the counter that counts down towards the next | |
725 | * zone reset. | |
726 | */ | |
727 | static void zbd_reset_write_cnt(const struct thread_data *td, | |
728 | const struct fio_file *f) | |
729 | { | |
730 | assert(0 <= td->o.zrf.u.f && td->o.zrf.u.f <= 1); | |
731 | ||
732 | pthread_mutex_lock(&f->zbd_info->mutex); | |
733 | f->zbd_info->write_cnt = td->o.zrf.u.f ? | |
734 | min(1.0 / td->o.zrf.u.f, 0.0 + UINT_MAX) : UINT_MAX; | |
735 | pthread_mutex_unlock(&f->zbd_info->mutex); | |
736 | } | |
737 | ||
738 | static bool zbd_dec_and_reset_write_cnt(const struct thread_data *td, | |
739 | const struct fio_file *f) | |
740 | { | |
741 | uint32_t write_cnt = 0; | |
742 | ||
743 | pthread_mutex_lock(&f->zbd_info->mutex); | |
744 | assert(f->zbd_info->write_cnt); | |
745 | if (f->zbd_info->write_cnt) | |
746 | write_cnt = --f->zbd_info->write_cnt; | |
747 | if (write_cnt == 0) | |
748 | zbd_reset_write_cnt(td, f); | |
749 | pthread_mutex_unlock(&f->zbd_info->mutex); | |
750 | ||
751 | return write_cnt == 0; | |
752 | } | |
753 | ||
91d25131 BVA |
754 | enum swd_action { |
755 | CHECK_SWD, | |
756 | SET_SWD, | |
757 | }; | |
758 | ||
759 | /* Calculate the number of sectors with data (swd) and perform action 'a' */ | |
760 | static uint64_t zbd_process_swd(const struct fio_file *f, enum swd_action a) | |
615555bb | 761 | { |
615555bb BVA |
762 | struct fio_zone_info *zb, *ze, *z; |
763 | uint64_t swd = 0; | |
764 | ||
765 | zb = &f->zbd_info->zone_info[zbd_zone_idx(f, f->file_offset)]; | |
766 | ze = &f->zbd_info->zone_info[zbd_zone_idx(f, f->file_offset + | |
767 | f->io_size)]; | |
768 | for (z = zb; z < ze; z++) { | |
769 | pthread_mutex_lock(&z->mutex); | |
770 | swd += z->wp - z->start; | |
771 | } | |
772 | pthread_mutex_lock(&f->zbd_info->mutex); | |
91d25131 BVA |
773 | switch (a) { |
774 | case CHECK_SWD: | |
775 | assert(f->zbd_info->sectors_with_data == swd); | |
776 | break; | |
777 | case SET_SWD: | |
778 | f->zbd_info->sectors_with_data = swd; | |
779 | break; | |
780 | } | |
615555bb BVA |
781 | pthread_mutex_unlock(&f->zbd_info->mutex); |
782 | for (z = zb; z < ze; z++) | |
783 | pthread_mutex_unlock(&z->mutex); | |
91d25131 BVA |
784 | |
785 | return swd; | |
786 | } | |
787 | ||
788 | /* | |
789 | * The swd check is useful for debugging but takes too much time to leave | |
790 | * it enabled all the time. Hence it is disabled by default. | |
791 | */ | |
792 | static const bool enable_check_swd = false; | |
793 | ||
794 | /* Check whether the value of zbd_info.sectors_with_data is correct. */ | |
795 | static void zbd_check_swd(const struct fio_file *f) | |
796 | { | |
797 | if (!enable_check_swd) | |
798 | return; | |
799 | ||
800 | zbd_process_swd(f, CHECK_SWD); | |
801 | } | |
802 | ||
803 | static void zbd_init_swd(struct fio_file *f) | |
804 | { | |
805 | uint64_t swd; | |
806 | ||
409a4f29 NA |
807 | if (!enable_check_swd) |
808 | return; | |
809 | ||
91d25131 BVA |
810 | swd = zbd_process_swd(f, SET_SWD); |
811 | dprint(FD_ZBD, "%s(%s): swd = %" PRIu64 "\n", __func__, f->file_name, | |
812 | swd); | |
615555bb BVA |
813 | } |
814 | ||
bfbdd35b BVA |
815 | void zbd_file_reset(struct thread_data *td, struct fio_file *f) |
816 | { | |
91d25131 | 817 | struct fio_zone_info *zb, *ze; |
bfbdd35b BVA |
818 | uint32_t zone_idx_e; |
819 | ||
820 | if (!f->zbd_info) | |
821 | return; | |
822 | ||
823 | zb = &f->zbd_info->zone_info[zbd_zone_idx(f, f->file_offset)]; | |
824 | zone_idx_e = zbd_zone_idx(f, f->file_offset + f->io_size); | |
825 | ze = &f->zbd_info->zone_info[zone_idx_e]; | |
91d25131 | 826 | zbd_init_swd(f); |
bfbdd35b BVA |
827 | /* |
828 | * If data verification is enabled reset the affected zones before | |
829 | * writing any data to avoid that a zone reset has to be issued while | |
830 | * writing data, which causes data loss. | |
831 | */ | |
832 | zbd_reset_zones(td, f, zb, ze, td->o.verify != VERIFY_NONE && | |
833 | (td->o.td_ddir & TD_DDIR_WRITE) && | |
834 | td->runstate != TD_VERIFYING); | |
a7c2b6fc | 835 | zbd_reset_write_cnt(td, f); |
bfbdd35b BVA |
836 | } |
837 | ||
59b07544 BVA |
838 | /* The caller must hold f->zbd_info->mutex. */ |
839 | static bool is_zone_open(const struct thread_data *td, const struct fio_file *f, | |
840 | unsigned int zone_idx) | |
841 | { | |
842 | struct zoned_block_device_info *zbdi = f->zbd_info; | |
843 | int i; | |
844 | ||
845 | assert(td->o.max_open_zones <= ARRAY_SIZE(zbdi->open_zones)); | |
846 | assert(zbdi->num_open_zones <= td->o.max_open_zones); | |
847 | ||
848 | for (i = 0; i < zbdi->num_open_zones; i++) | |
849 | if (zbdi->open_zones[i] == zone_idx) | |
850 | return true; | |
851 | ||
852 | return false; | |
853 | } | |
854 | ||
855 | /* | |
856 | * Open a ZBD zone if it was not yet open. Returns true if either the zone was | |
857 | * already open or if opening a new zone is allowed. Returns false if the zone | |
858 | * was not yet open and opening a new zone would cause the zone limit to be | |
859 | * exceeded. | |
860 | */ | |
861 | static bool zbd_open_zone(struct thread_data *td, const struct io_u *io_u, | |
862 | uint32_t zone_idx) | |
863 | { | |
864 | const uint32_t min_bs = td->o.min_bs[DDIR_WRITE]; | |
865 | const struct fio_file *f = io_u->file; | |
866 | struct fio_zone_info *z = &f->zbd_info->zone_info[zone_idx]; | |
867 | bool res = true; | |
868 | ||
b7694961 | 869 | if (z->cond == ZBD_ZONE_COND_OFFLINE) |
59b07544 BVA |
870 | return false; |
871 | ||
872 | /* | |
873 | * Skip full zones with data verification enabled because resetting a | |
874 | * zone causes data loss and hence causes verification to fail. | |
875 | */ | |
876 | if (td->o.verify != VERIFY_NONE && zbd_zone_full(f, z, min_bs)) | |
877 | return false; | |
878 | ||
879 | /* Zero means no limit */ | |
880 | if (!td->o.max_open_zones) | |
881 | return true; | |
882 | ||
883 | pthread_mutex_lock(&f->zbd_info->mutex); | |
884 | if (is_zone_open(td, f, zone_idx)) | |
885 | goto out; | |
886 | res = false; | |
887 | if (f->zbd_info->num_open_zones >= td->o.max_open_zones) | |
888 | goto out; | |
889 | dprint(FD_ZBD, "%s: opening zone %d\n", f->file_name, zone_idx); | |
890 | f->zbd_info->open_zones[f->zbd_info->num_open_zones++] = zone_idx; | |
891 | z->open = 1; | |
892 | res = true; | |
893 | ||
894 | out: | |
895 | pthread_mutex_unlock(&f->zbd_info->mutex); | |
896 | return res; | |
897 | } | |
898 | ||
899 | /* The caller must hold f->zbd_info->mutex */ | |
900 | static void zbd_close_zone(struct thread_data *td, const struct fio_file *f, | |
901 | unsigned int open_zone_idx) | |
902 | { | |
903 | uint32_t zone_idx; | |
904 | ||
905 | assert(open_zone_idx < f->zbd_info->num_open_zones); | |
906 | zone_idx = f->zbd_info->open_zones[open_zone_idx]; | |
907 | memmove(f->zbd_info->open_zones + open_zone_idx, | |
908 | f->zbd_info->open_zones + open_zone_idx + 1, | |
b7694961 | 909 | (ZBD_MAX_OPEN_ZONES - (open_zone_idx + 1)) * |
59b07544 BVA |
910 | sizeof(f->zbd_info->open_zones[0])); |
911 | f->zbd_info->num_open_zones--; | |
912 | f->zbd_info->zone_info[zone_idx].open = 0; | |
913 | } | |
914 | ||
6463db6c AD |
915 | /* Anything goes as long as it is not a constant. */ |
916 | static uint32_t pick_random_zone_idx(const struct fio_file *f, | |
917 | const struct io_u *io_u) | |
918 | { | |
919 | return io_u->offset * f->zbd_info->num_open_zones / f->real_file_size; | |
920 | } | |
921 | ||
59b07544 BVA |
922 | /* |
923 | * Modify the offset of an I/O unit that does not refer to an open zone such | |
924 | * that it refers to an open zone. Close an open zone and open a new zone if | |
925 | * necessary. This algorithm can only work correctly if all write pointers are | |
926 | * a multiple of the fio block size. The caller must neither hold z->mutex | |
927 | * nor f->zbd_info->mutex. Returns with z->mutex held upon success. | |
928 | */ | |
379e5f09 BVA |
929 | static struct fio_zone_info *zbd_convert_to_open_zone(struct thread_data *td, |
930 | struct io_u *io_u) | |
59b07544 BVA |
931 | { |
932 | const uint32_t min_bs = td->o.min_bs[io_u->ddir]; | |
933 | const struct fio_file *f = io_u->file; | |
934 | struct fio_zone_info *z; | |
935 | unsigned int open_zone_idx = -1; | |
936 | uint32_t zone_idx, new_zone_idx; | |
937 | int i; | |
938 | ||
939 | assert(is_valid_offset(f, io_u->offset)); | |
940 | ||
941 | if (td->o.max_open_zones) { | |
942 | /* | |
943 | * This statement accesses f->zbd_info->open_zones[] on purpose | |
944 | * without locking. | |
945 | */ | |
6463db6c | 946 | zone_idx = f->zbd_info->open_zones[pick_random_zone_idx(f, io_u)]; |
59b07544 BVA |
947 | } else { |
948 | zone_idx = zbd_zone_idx(f, io_u->offset); | |
949 | } | |
950 | dprint(FD_ZBD, "%s(%s): starting from zone %d (offset %lld, buflen %lld)\n", | |
951 | __func__, f->file_name, zone_idx, io_u->offset, io_u->buflen); | |
952 | ||
953 | /* | |
954 | * Since z->mutex is the outer lock and f->zbd_info->mutex the inner | |
955 | * lock it can happen that the state of the zone with index zone_idx | |
956 | * has changed after 'z' has been assigned and before f->zbd_info->mutex | |
957 | * has been obtained. Hence the loop. | |
958 | */ | |
959 | for (;;) { | |
6463db6c AD |
960 | uint32_t tmp_idx; |
961 | ||
59b07544 BVA |
962 | z = &f->zbd_info->zone_info[zone_idx]; |
963 | ||
b27aef6a | 964 | zone_lock(td, z); |
59b07544 BVA |
965 | pthread_mutex_lock(&f->zbd_info->mutex); |
966 | if (td->o.max_open_zones == 0) | |
967 | goto examine_zone; | |
968 | if (f->zbd_info->num_open_zones == 0) { | |
969 | pthread_mutex_unlock(&f->zbd_info->mutex); | |
970 | pthread_mutex_unlock(&z->mutex); | |
971 | dprint(FD_ZBD, "%s(%s): no zones are open\n", | |
972 | __func__, f->file_name); | |
973 | return NULL; | |
974 | } | |
6463db6c AD |
975 | |
976 | /* | |
977 | * List of opened zones is per-device, shared across all threads. | |
978 | * Start with quasi-random candidate zone. | |
979 | * Ignore zones which don't belong to thread's offset/size area. | |
980 | */ | |
981 | open_zone_idx = pick_random_zone_idx(f, io_u); | |
59b07544 | 982 | assert(open_zone_idx < f->zbd_info->num_open_zones); |
6463db6c AD |
983 | tmp_idx = open_zone_idx; |
984 | for (i = 0; i < f->zbd_info->num_open_zones; i++) { | |
985 | uint32_t tmpz; | |
986 | ||
987 | if (tmp_idx >= f->zbd_info->num_open_zones) | |
988 | tmp_idx = 0; | |
989 | tmpz = f->zbd_info->open_zones[tmp_idx]; | |
990 | ||
991 | if (is_valid_offset(f, f->zbd_info->zone_info[tmpz].start)) { | |
992 | open_zone_idx = tmp_idx; | |
993 | goto found_candidate_zone; | |
994 | } | |
995 | ||
996 | tmp_idx++; | |
997 | } | |
998 | ||
999 | dprint(FD_ZBD, "%s(%s): no candidate zone\n", | |
1000 | __func__, f->file_name); | |
1001 | return NULL; | |
1002 | ||
1003 | found_candidate_zone: | |
59b07544 BVA |
1004 | new_zone_idx = f->zbd_info->open_zones[open_zone_idx]; |
1005 | if (new_zone_idx == zone_idx) | |
1006 | break; | |
1007 | zone_idx = new_zone_idx; | |
1008 | pthread_mutex_unlock(&f->zbd_info->mutex); | |
1009 | pthread_mutex_unlock(&z->mutex); | |
1010 | } | |
1011 | ||
1012 | /* Both z->mutex and f->zbd_info->mutex are held. */ | |
1013 | ||
1014 | examine_zone: | |
ee3696bd | 1015 | if (z->wp + min_bs <= (z+1)->start) { |
59b07544 BVA |
1016 | pthread_mutex_unlock(&f->zbd_info->mutex); |
1017 | goto out; | |
1018 | } | |
1019 | dprint(FD_ZBD, "%s(%s): closing zone %d\n", __func__, f->file_name, | |
1020 | zone_idx); | |
1021 | if (td->o.max_open_zones) | |
1022 | zbd_close_zone(td, f, open_zone_idx); | |
1023 | pthread_mutex_unlock(&f->zbd_info->mutex); | |
1024 | ||
1025 | /* Only z->mutex is held. */ | |
1026 | ||
1027 | /* Zone 'z' is full, so try to open a new zone. */ | |
1028 | for (i = f->io_size / f->zbd_info->zone_size; i > 0; i--) { | |
1029 | zone_idx++; | |
1030 | pthread_mutex_unlock(&z->mutex); | |
1031 | z++; | |
ee3696bd | 1032 | if (!is_valid_offset(f, z->start)) { |
59b07544 BVA |
1033 | /* Wrap-around. */ |
1034 | zone_idx = zbd_zone_idx(f, f->file_offset); | |
1035 | z = &f->zbd_info->zone_info[zone_idx]; | |
1036 | } | |
ee3696bd | 1037 | assert(is_valid_offset(f, z->start)); |
b27aef6a | 1038 | zone_lock(td, z); |
59b07544 BVA |
1039 | if (z->open) |
1040 | continue; | |
1041 | if (zbd_open_zone(td, io_u, zone_idx)) | |
1042 | goto out; | |
1043 | } | |
1044 | ||
1045 | /* Only z->mutex is held. */ | |
1046 | ||
1047 | /* Check whether the write fits in any of the already opened zones. */ | |
1048 | pthread_mutex_lock(&f->zbd_info->mutex); | |
1049 | for (i = 0; i < f->zbd_info->num_open_zones; i++) { | |
1050 | zone_idx = f->zbd_info->open_zones[i]; | |
1051 | pthread_mutex_unlock(&f->zbd_info->mutex); | |
1052 | pthread_mutex_unlock(&z->mutex); | |
1053 | ||
1054 | z = &f->zbd_info->zone_info[zone_idx]; | |
1055 | ||
b27aef6a | 1056 | zone_lock(td, z); |
ee3696bd | 1057 | if (z->wp + min_bs <= (z+1)->start) |
59b07544 BVA |
1058 | goto out; |
1059 | pthread_mutex_lock(&f->zbd_info->mutex); | |
1060 | } | |
1061 | pthread_mutex_unlock(&f->zbd_info->mutex); | |
1062 | pthread_mutex_unlock(&z->mutex); | |
1063 | dprint(FD_ZBD, "%s(%s): did not open another zone\n", __func__, | |
1064 | f->file_name); | |
1065 | return NULL; | |
1066 | ||
1067 | out: | |
1068 | dprint(FD_ZBD, "%s(%s): returning zone %d\n", __func__, f->file_name, | |
1069 | zone_idx); | |
ee3696bd | 1070 | io_u->offset = z->start; |
59b07544 BVA |
1071 | return z; |
1072 | } | |
1073 | ||
bfbdd35b | 1074 | /* The caller must hold z->mutex. */ |
59b07544 BVA |
1075 | static struct fio_zone_info *zbd_replay_write_order(struct thread_data *td, |
1076 | struct io_u *io_u, | |
1077 | struct fio_zone_info *z) | |
bfbdd35b BVA |
1078 | { |
1079 | const struct fio_file *f = io_u->file; | |
1080 | const uint32_t min_bs = td->o.min_bs[DDIR_WRITE]; | |
1081 | ||
59b07544 BVA |
1082 | if (!zbd_open_zone(td, io_u, z - f->zbd_info->zone_info)) { |
1083 | pthread_mutex_unlock(&z->mutex); | |
1084 | z = zbd_convert_to_open_zone(td, io_u); | |
1085 | assert(z); | |
1086 | } | |
1087 | ||
bfbdd35b | 1088 | if (z->verify_block * min_bs >= f->zbd_info->zone_size) |
a0c84dd4 JA |
1089 | log_err("%s: %d * %d >= %llu\n", f->file_name, z->verify_block, |
1090 | min_bs, (unsigned long long) f->zbd_info->zone_size); | |
ee3696bd | 1091 | io_u->offset = z->start + z->verify_block++ * min_bs; |
59b07544 | 1092 | return z; |
bfbdd35b BVA |
1093 | } |
1094 | ||
1095 | /* | |
1096 | * Find another zone for which @io_u fits below the write pointer. Start | |
1097 | * searching in zones @zb + 1 .. @zl and continue searching in zones | |
1098 | * @zf .. @zb - 1. | |
1099 | * | |
1100 | * Either returns NULL or returns a zone pointer and holds the mutex for that | |
1101 | * zone. | |
1102 | */ | |
1103 | static struct fio_zone_info * | |
1104 | zbd_find_zone(struct thread_data *td, struct io_u *io_u, | |
1105 | struct fio_zone_info *zb, struct fio_zone_info *zl) | |
1106 | { | |
1107 | const uint32_t min_bs = td->o.min_bs[io_u->ddir]; | |
1108 | const struct fio_file *f = io_u->file; | |
1109 | struct fio_zone_info *z1, *z2; | |
1110 | const struct fio_zone_info *const zf = | |
1111 | &f->zbd_info->zone_info[zbd_zone_idx(f, f->file_offset)]; | |
1112 | ||
1113 | /* | |
1114 | * Skip to the next non-empty zone in case of sequential I/O and to | |
1115 | * the nearest non-empty zone in case of random I/O. | |
1116 | */ | |
1117 | for (z1 = zb + 1, z2 = zb - 1; z1 < zl || z2 >= zf; z1++, z2--) { | |
b7694961 | 1118 | if (z1 < zl && z1->cond != ZBD_ZONE_COND_OFFLINE) { |
bfbdd35b | 1119 | pthread_mutex_lock(&z1->mutex); |
ee3696bd | 1120 | if (z1->start + min_bs <= z1->wp) |
bfbdd35b BVA |
1121 | return z1; |
1122 | pthread_mutex_unlock(&z1->mutex); | |
1123 | } else if (!td_random(td)) { | |
1124 | break; | |
1125 | } | |
1126 | if (td_random(td) && z2 >= zf && | |
b7694961 | 1127 | z2->cond != ZBD_ZONE_COND_OFFLINE) { |
bfbdd35b | 1128 | pthread_mutex_lock(&z2->mutex); |
ee3696bd | 1129 | if (z2->start + min_bs <= z2->wp) |
bfbdd35b BVA |
1130 | return z2; |
1131 | pthread_mutex_unlock(&z2->mutex); | |
1132 | } | |
1133 | } | |
1134 | dprint(FD_ZBD, "%s: adjusting random read offset failed\n", | |
1135 | f->file_name); | |
1136 | return NULL; | |
1137 | } | |
1138 | ||
bfbdd35b | 1139 | /** |
d9ed3e63 | 1140 | * zbd_queue_io - update the write pointer of a sequential zone |
bfbdd35b | 1141 | * @io_u: I/O unit |
d9ed3e63 DLM |
1142 | * @success: Whether or not the I/O unit has been queued successfully |
1143 | * @q: queueing status (busy, completed or queued). | |
bfbdd35b | 1144 | * |
d9ed3e63 DLM |
1145 | * For write and trim operations, update the write pointer of the I/O unit |
1146 | * target zone. | |
bfbdd35b | 1147 | */ |
d9ed3e63 | 1148 | static void zbd_queue_io(struct io_u *io_u, int q, bool success) |
bfbdd35b | 1149 | { |
d9ed3e63 DLM |
1150 | const struct fio_file *f = io_u->file; |
1151 | struct zoned_block_device_info *zbd_info = f->zbd_info; | |
bfbdd35b BVA |
1152 | struct fio_zone_info *z; |
1153 | uint32_t zone_idx; | |
d9ed3e63 | 1154 | uint64_t zone_end; |
bfbdd35b | 1155 | |
bfbdd35b BVA |
1156 | if (!zbd_info) |
1157 | return; | |
1158 | ||
d9ed3e63 | 1159 | zone_idx = zbd_zone_idx(f, io_u->offset); |
bfbdd35b | 1160 | assert(zone_idx < zbd_info->nr_zones); |
d9ed3e63 DLM |
1161 | z = &zbd_info->zone_info[zone_idx]; |
1162 | ||
b7694961 | 1163 | if (!zbd_zone_swr(z)) |
bfbdd35b | 1164 | return; |
d9ed3e63 | 1165 | |
bfbdd35b BVA |
1166 | if (!success) |
1167 | goto unlock; | |
d9ed3e63 DLM |
1168 | |
1169 | dprint(FD_ZBD, | |
1170 | "%s: queued I/O (%lld, %llu) for zone %u\n", | |
1171 | f->file_name, io_u->offset, io_u->buflen, zone_idx); | |
1172 | ||
bfbdd35b BVA |
1173 | switch (io_u->ddir) { |
1174 | case DDIR_WRITE: | |
d9ed3e63 DLM |
1175 | zone_end = min((uint64_t)(io_u->offset + io_u->buflen), |
1176 | (z + 1)->start); | |
a7c2b6fc BVA |
1177 | pthread_mutex_lock(&zbd_info->mutex); |
1178 | /* | |
1179 | * z->wp > zone_end means that one or more I/O errors | |
1180 | * have occurred. | |
1181 | */ | |
1182 | if (z->wp <= zone_end) | |
1183 | zbd_info->sectors_with_data += zone_end - z->wp; | |
1184 | pthread_mutex_unlock(&zbd_info->mutex); | |
bfbdd35b BVA |
1185 | z->wp = zone_end; |
1186 | break; | |
1187 | case DDIR_TRIM: | |
1188 | assert(z->wp == z->start); | |
1189 | break; | |
1190 | default: | |
1191 | break; | |
1192 | } | |
d9ed3e63 | 1193 | |
bfbdd35b | 1194 | unlock: |
d9ed3e63 DLM |
1195 | if (!success || q != FIO_Q_QUEUED) { |
1196 | /* BUSY or COMPLETED: unlock the zone */ | |
1197 | pthread_mutex_unlock(&z->mutex); | |
1198 | io_u->zbd_put_io = NULL; | |
1199 | } | |
1200 | } | |
1201 | ||
1202 | /** | |
1203 | * zbd_put_io - Unlock an I/O unit target zone lock | |
1204 | * @io_u: I/O unit | |
1205 | */ | |
1206 | static void zbd_put_io(const struct io_u *io_u) | |
1207 | { | |
1208 | const struct fio_file *f = io_u->file; | |
1209 | struct zoned_block_device_info *zbd_info = f->zbd_info; | |
1210 | struct fio_zone_info *z; | |
1211 | uint32_t zone_idx; | |
1212 | ||
1213 | if (!zbd_info) | |
1214 | return; | |
615555bb | 1215 | |
d9ed3e63 DLM |
1216 | zone_idx = zbd_zone_idx(f, io_u->offset); |
1217 | assert(zone_idx < zbd_info->nr_zones); | |
1218 | z = &zbd_info->zone_info[zone_idx]; | |
1219 | ||
b7694961 | 1220 | if (!zbd_zone_swr(z)) |
d9ed3e63 DLM |
1221 | return; |
1222 | ||
1223 | dprint(FD_ZBD, | |
1224 | "%s: terminate I/O (%lld, %llu) for zone %u\n", | |
1225 | f->file_name, io_u->offset, io_u->buflen, zone_idx); | |
1226 | ||
1227 | assert(pthread_mutex_unlock(&z->mutex) == 0); | |
1228 | zbd_check_swd(f); | |
bfbdd35b BVA |
1229 | } |
1230 | ||
1231 | bool zbd_unaligned_write(int error_code) | |
1232 | { | |
1233 | switch (error_code) { | |
1234 | case EIO: | |
1235 | case EREMOTEIO: | |
1236 | return true; | |
1237 | } | |
1238 | return false; | |
1239 | } | |
1240 | ||
4d37720a DLM |
1241 | /** |
1242 | * setup_zbd_zone_mode - handle zoneskip as necessary for ZBD drives | |
1243 | * @td: FIO thread data. | |
1244 | * @io_u: FIO I/O unit. | |
1245 | * | |
1246 | * For sequential workloads, change the file offset to skip zoneskip bytes when | |
1247 | * no more IO can be performed in the current zone. | |
1248 | * - For read workloads, zoneskip is applied when the io has reached the end of | |
1249 | * the zone or the zone write position (when td->o.read_beyond_wp is false). | |
1250 | * - For write workloads, zoneskip is applied when the zone is full. | |
1251 | * This applies only to read and write operations. | |
1252 | */ | |
1253 | void setup_zbd_zone_mode(struct thread_data *td, struct io_u *io_u) | |
1254 | { | |
1255 | struct fio_file *f = io_u->file; | |
1256 | enum fio_ddir ddir = io_u->ddir; | |
1257 | struct fio_zone_info *z; | |
1258 | uint32_t zone_idx; | |
1259 | ||
1260 | assert(td->o.zone_mode == ZONE_MODE_ZBD); | |
1261 | assert(td->o.zone_size); | |
1262 | ||
1263 | /* | |
1264 | * zone_skip is valid only for sequential workloads. | |
1265 | */ | |
1266 | if (td_random(td) || !td->o.zone_skip) | |
1267 | return; | |
1268 | ||
1269 | /* | |
1270 | * It is time to switch to a new zone if: | |
1271 | * - zone_bytes == zone_size bytes have already been accessed | |
1272 | * - The last position reached the end of the current zone. | |
1273 | * - For reads with td->o.read_beyond_wp == false, the last position | |
1274 | * reached the zone write pointer. | |
1275 | */ | |
1276 | zone_idx = zbd_zone_idx(f, f->last_pos[ddir]); | |
1277 | z = &f->zbd_info->zone_info[zone_idx]; | |
1278 | ||
1279 | if (td->zone_bytes >= td->o.zone_size || | |
1280 | f->last_pos[ddir] >= (z+1)->start || | |
1281 | (ddir == DDIR_READ && | |
1282 | (!td->o.read_beyond_wp) && f->last_pos[ddir] >= z->wp)) { | |
1283 | /* | |
1284 | * Skip zones. | |
1285 | */ | |
1286 | td->zone_bytes = 0; | |
1287 | f->file_offset += td->o.zone_size + td->o.zone_skip; | |
1288 | ||
1289 | /* | |
1290 | * Wrap from the beginning, if we exceed the file size | |
1291 | */ | |
1292 | if (f->file_offset >= f->real_file_size) | |
1293 | f->file_offset = get_start_offset(td, f); | |
1294 | ||
1295 | f->last_pos[ddir] = f->file_offset; | |
1296 | td->io_skip_bytes += td->o.zone_skip; | |
1297 | } | |
1298 | } | |
1299 | ||
bfbdd35b BVA |
1300 | /** |
1301 | * zbd_adjust_block - adjust the offset and length as necessary for ZBD drives | |
1302 | * @td: FIO thread data. | |
1303 | * @io_u: FIO I/O unit. | |
1304 | * | |
1305 | * Locking strategy: returns with z->mutex locked if and only if z refers | |
1306 | * to a sequential zone and if io_u_accept is returned. z is the zone that | |
1307 | * corresponds to io_u->offset at the end of this function. | |
1308 | */ | |
1309 | enum io_u_action zbd_adjust_block(struct thread_data *td, struct io_u *io_u) | |
1310 | { | |
b7694961 | 1311 | struct fio_file *f = io_u->file; |
bfbdd35b | 1312 | uint32_t zone_idx_b; |
de65f7b7 | 1313 | struct fio_zone_info *zb, *zl, *orig_zb; |
bfbdd35b BVA |
1314 | uint32_t orig_len = io_u->buflen; |
1315 | uint32_t min_bs = td->o.min_bs[io_u->ddir]; | |
1316 | uint64_t new_len; | |
1317 | int64_t range; | |
1318 | ||
1319 | if (!f->zbd_info) | |
1320 | return io_u_accept; | |
1321 | ||
1322 | assert(is_valid_offset(f, io_u->offset)); | |
1323 | assert(io_u->buflen); | |
1324 | zone_idx_b = zbd_zone_idx(f, io_u->offset); | |
1325 | zb = &f->zbd_info->zone_info[zone_idx_b]; | |
de65f7b7 | 1326 | orig_zb = zb; |
bfbdd35b BVA |
1327 | |
1328 | /* Accept the I/O offset for conventional zones. */ | |
b7694961 | 1329 | if (!zbd_zone_swr(zb)) |
bfbdd35b BVA |
1330 | return io_u_accept; |
1331 | ||
1332 | /* | |
1333 | * Accept the I/O offset for reads if reading beyond the write pointer | |
1334 | * is enabled. | |
1335 | */ | |
b7694961 | 1336 | if (zb->cond != ZBD_ZONE_COND_OFFLINE && |
bfbdd35b BVA |
1337 | io_u->ddir == DDIR_READ && td->o.read_beyond_wp) |
1338 | return io_u_accept; | |
1339 | ||
615555bb BVA |
1340 | zbd_check_swd(f); |
1341 | ||
b27aef6a | 1342 | zone_lock(td, zb); |
6f0c6085 | 1343 | |
bfbdd35b BVA |
1344 | switch (io_u->ddir) { |
1345 | case DDIR_READ: | |
1346 | if (td->runstate == TD_VERIFYING) { | |
59b07544 | 1347 | zb = zbd_replay_write_order(td, io_u, zb); |
bfbdd35b BVA |
1348 | goto accept; |
1349 | } | |
1350 | /* | |
de65f7b7 DLM |
1351 | * Check that there is enough written data in the zone to do an |
1352 | * I/O of at least min_bs B. If there isn't, find a new zone for | |
1353 | * the I/O. | |
bfbdd35b | 1354 | */ |
b7694961 | 1355 | range = zb->cond != ZBD_ZONE_COND_OFFLINE ? |
ee3696bd | 1356 | zb->wp - zb->start : 0; |
de65f7b7 | 1357 | if (range < min_bs || |
ee3696bd | 1358 | ((!td_random(td)) && (io_u->offset + min_bs > zb->wp))) { |
bfbdd35b BVA |
1359 | pthread_mutex_unlock(&zb->mutex); |
1360 | zl = &f->zbd_info->zone_info[zbd_zone_idx(f, | |
1361 | f->file_offset + f->io_size)]; | |
1362 | zb = zbd_find_zone(td, io_u, zb, zl); | |
1363 | if (!zb) { | |
1364 | dprint(FD_ZBD, | |
1365 | "%s: zbd_find_zone(%lld, %llu) failed\n", | |
1366 | f->file_name, io_u->offset, | |
1367 | io_u->buflen); | |
1368 | goto eof; | |
1369 | } | |
de65f7b7 DLM |
1370 | /* |
1371 | * zbd_find_zone() returned a zone with a range of at | |
1372 | * least min_bs. | |
1373 | */ | |
ee3696bd | 1374 | range = zb->wp - zb->start; |
de65f7b7 DLM |
1375 | assert(range >= min_bs); |
1376 | ||
1377 | if (!td_random(td)) | |
ee3696bd | 1378 | io_u->offset = zb->start; |
bfbdd35b | 1379 | } |
de65f7b7 DLM |
1380 | /* |
1381 | * Make sure the I/O is within the zone valid data range while | |
1382 | * maximizing the I/O size and preserving randomness. | |
1383 | */ | |
1384 | if (range <= io_u->buflen) | |
ee3696bd | 1385 | io_u->offset = zb->start; |
de65f7b7 | 1386 | else if (td_random(td)) |
ee3696bd DLM |
1387 | io_u->offset = zb->start + |
1388 | ((io_u->offset - orig_zb->start) % | |
de65f7b7 DLM |
1389 | (range - io_u->buflen)) / min_bs * min_bs; |
1390 | /* | |
1391 | * Make sure the I/O does not cross over the zone wp position. | |
1392 | */ | |
1393 | new_len = min((unsigned long long)io_u->buflen, | |
ee3696bd | 1394 | (unsigned long long)(zb->wp - io_u->offset)); |
de65f7b7 DLM |
1395 | new_len = new_len / min_bs * min_bs; |
1396 | if (new_len < io_u->buflen) { | |
1397 | io_u->buflen = new_len; | |
1398 | dprint(FD_IO, "Changed length from %u into %llu\n", | |
1399 | orig_len, io_u->buflen); | |
bfbdd35b | 1400 | } |
ee3696bd DLM |
1401 | assert(zb->start <= io_u->offset); |
1402 | assert(io_u->offset + io_u->buflen <= zb->wp); | |
bfbdd35b BVA |
1403 | goto accept; |
1404 | case DDIR_WRITE: | |
ee3696bd | 1405 | if (io_u->buflen > f->zbd_info->zone_size) |
bfbdd35b | 1406 | goto eof; |
59b07544 BVA |
1407 | if (!zbd_open_zone(td, io_u, zone_idx_b)) { |
1408 | pthread_mutex_unlock(&zb->mutex); | |
1409 | zb = zbd_convert_to_open_zone(td, io_u); | |
1410 | if (!zb) | |
1411 | goto eof; | |
1412 | zone_idx_b = zb - f->zbd_info->zone_info; | |
1413 | } | |
a7c2b6fc BVA |
1414 | /* Check whether the zone reset threshold has been exceeded */ |
1415 | if (td->o.zrf.u.f) { | |
ee3696bd | 1416 | if (f->zbd_info->sectors_with_data >= |
a7c2b6fc BVA |
1417 | f->io_size * td->o.zrt.u.f && |
1418 | zbd_dec_and_reset_write_cnt(td, f)) { | |
1419 | zb->reset_zone = 1; | |
1420 | } | |
1421 | } | |
bfbdd35b BVA |
1422 | /* Reset the zone pointer if necessary */ |
1423 | if (zb->reset_zone || zbd_zone_full(f, zb, min_bs)) { | |
1424 | assert(td->o.verify == VERIFY_NONE); | |
1425 | /* | |
1426 | * Since previous write requests may have been submitted | |
1427 | * asynchronously and since we will submit the zone | |
1428 | * reset synchronously, wait until previously submitted | |
1429 | * write requests have completed before issuing a | |
1430 | * zone reset. | |
1431 | */ | |
1432 | io_u_quiesce(td); | |
1433 | zb->reset_zone = 0; | |
1434 | if (zbd_reset_zone(td, f, zb) < 0) | |
1435 | goto eof; | |
1436 | } | |
1437 | /* Make writes occur at the write pointer */ | |
1438 | assert(!zbd_zone_full(f, zb, min_bs)); | |
ee3696bd | 1439 | io_u->offset = zb->wp; |
bfbdd35b BVA |
1440 | if (!is_valid_offset(f, io_u->offset)) { |
1441 | dprint(FD_ZBD, "Dropped request with offset %llu\n", | |
1442 | io_u->offset); | |
1443 | goto eof; | |
1444 | } | |
1445 | /* | |
1446 | * Make sure that the buflen is a multiple of the minimal | |
1447 | * block size. Give up if shrinking would make the request too | |
1448 | * small. | |
1449 | */ | |
1450 | new_len = min((unsigned long long)io_u->buflen, | |
ee3696bd | 1451 | (zb + 1)->start - io_u->offset); |
bfbdd35b BVA |
1452 | new_len = new_len / min_bs * min_bs; |
1453 | if (new_len == io_u->buflen) | |
1454 | goto accept; | |
1455 | if (new_len >= min_bs) { | |
1456 | io_u->buflen = new_len; | |
1457 | dprint(FD_IO, "Changed length from %u into %llu\n", | |
1458 | orig_len, io_u->buflen); | |
1459 | goto accept; | |
1460 | } | |
1461 | log_err("Zone remainder %lld smaller than minimum block size %d\n", | |
ee3696bd | 1462 | ((zb + 1)->start - io_u->offset), |
bfbdd35b BVA |
1463 | min_bs); |
1464 | goto eof; | |
1465 | case DDIR_TRIM: | |
1466 | /* fall-through */ | |
1467 | case DDIR_SYNC: | |
1468 | case DDIR_DATASYNC: | |
1469 | case DDIR_SYNC_FILE_RANGE: | |
1470 | case DDIR_WAIT: | |
1471 | case DDIR_LAST: | |
1472 | case DDIR_INVAL: | |
1473 | goto accept; | |
1474 | } | |
1475 | ||
1476 | assert(false); | |
1477 | ||
1478 | accept: | |
1479 | assert(zb); | |
b7694961 | 1480 | assert(zb->cond != ZBD_ZONE_COND_OFFLINE); |
d9ed3e63 DLM |
1481 | assert(!io_u->zbd_queue_io); |
1482 | assert(!io_u->zbd_put_io); | |
1483 | io_u->zbd_queue_io = zbd_queue_io; | |
1484 | io_u->zbd_put_io = zbd_put_io; | |
bfbdd35b BVA |
1485 | return io_u_accept; |
1486 | ||
1487 | eof: | |
1488 | if (zb) | |
1489 | pthread_mutex_unlock(&zb->mutex); | |
1490 | return io_u_eof; | |
1491 | } | |
fd5d733f BVA |
1492 | |
1493 | /* Return a string with ZBD statistics */ | |
1494 | char *zbd_write_status(const struct thread_stat *ts) | |
1495 | { | |
1496 | char *res; | |
1497 | ||
a0c84dd4 | 1498 | if (asprintf(&res, "; %llu zone resets", (unsigned long long) ts->nr_zone_resets) < 0) |
fd5d733f BVA |
1499 | return NULL; |
1500 | return res; | |
1501 | } |