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