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