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