| 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 "pshared.h" |
| 23 | #include "zbd.h" |
| 24 | |
| 25 | /** |
| 26 | * zbd_get_zoned_model - Get a device zoned model |
| 27 | * @td: FIO thread data |
| 28 | * @f: FIO file for which to get model information |
| 29 | */ |
| 30 | int zbd_get_zoned_model(struct thread_data *td, struct fio_file *f, |
| 31 | enum zbd_zoned_model *model) |
| 32 | { |
| 33 | int ret; |
| 34 | |
| 35 | if (f->filetype == FIO_TYPE_PIPE) { |
| 36 | log_err("zonemode=zbd does not support pipes\n"); |
| 37 | return -EINVAL; |
| 38 | } |
| 39 | |
| 40 | /* If regular file, always emulate zones inside the file. */ |
| 41 | if (f->filetype == FIO_TYPE_FILE) { |
| 42 | *model = ZBD_NONE; |
| 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | if (td->io_ops && td->io_ops->get_zoned_model) |
| 47 | ret = td->io_ops->get_zoned_model(td, f, model); |
| 48 | else |
| 49 | ret = blkzoned_get_zoned_model(td, f, model); |
| 50 | if (ret < 0) { |
| 51 | td_verror(td, errno, "get zoned model failed"); |
| 52 | log_err("%s: get zoned model failed (%d).\n", |
| 53 | f->file_name, errno); |
| 54 | } |
| 55 | |
| 56 | return ret; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * zbd_report_zones - Get zone information |
| 61 | * @td: FIO thread data. |
| 62 | * @f: FIO file for which to get zone information |
| 63 | * @offset: offset from which to report zones |
| 64 | * @zones: Array of struct zbd_zone |
| 65 | * @nr_zones: Size of @zones array |
| 66 | * |
| 67 | * Get zone information into @zones starting from the zone at offset @offset |
| 68 | * for the device specified by @f. |
| 69 | * |
| 70 | * Returns the number of zones reported upon success and a negative error code |
| 71 | * upon failure. If the zone report is empty, always assume an error (device |
| 72 | * problem) and return -EIO. |
| 73 | */ |
| 74 | int zbd_report_zones(struct thread_data *td, struct fio_file *f, |
| 75 | uint64_t offset, struct zbd_zone *zones, |
| 76 | unsigned int nr_zones) |
| 77 | { |
| 78 | int ret; |
| 79 | |
| 80 | if (td->io_ops && td->io_ops->report_zones) |
| 81 | ret = td->io_ops->report_zones(td, f, offset, zones, nr_zones); |
| 82 | else |
| 83 | ret = blkzoned_report_zones(td, f, offset, zones, nr_zones); |
| 84 | if (ret < 0) { |
| 85 | td_verror(td, errno, "report zones failed"); |
| 86 | log_err("%s: report zones from sector %llu failed (%d).\n", |
| 87 | f->file_name, (unsigned long long)offset >> 9, errno); |
| 88 | } else if (ret == 0) { |
| 89 | td_verror(td, errno, "Empty zone report"); |
| 90 | log_err("%s: report zones from sector %llu is empty.\n", |
| 91 | f->file_name, (unsigned long long)offset >> 9); |
| 92 | ret = -EIO; |
| 93 | } |
| 94 | |
| 95 | return ret; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * zbd_reset_wp - reset the write pointer of a range of zones |
| 100 | * @td: FIO thread data. |
| 101 | * @f: FIO file for which to reset zones |
| 102 | * @offset: Starting offset of the first zone to reset |
| 103 | * @length: Length of the range of zones to reset |
| 104 | * |
| 105 | * Reset the write pointer of all zones in the range @offset...@offset+@length. |
| 106 | * Returns 0 upon success and a negative error code upon failure. |
| 107 | */ |
| 108 | int zbd_reset_wp(struct thread_data *td, struct fio_file *f, |
| 109 | uint64_t offset, uint64_t length) |
| 110 | { |
| 111 | int ret; |
| 112 | |
| 113 | if (td->io_ops && td->io_ops->reset_wp) |
| 114 | ret = td->io_ops->reset_wp(td, f, offset, length); |
| 115 | else |
| 116 | ret = blkzoned_reset_wp(td, f, offset, length); |
| 117 | if (ret < 0) { |
| 118 | td_verror(td, errno, "resetting wp failed"); |
| 119 | log_err("%s: resetting wp for %llu sectors at sector %llu failed (%d).\n", |
| 120 | f->file_name, (unsigned long long)length >> 9, |
| 121 | (unsigned long long)offset >> 9, errno); |
| 122 | } |
| 123 | |
| 124 | return ret; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * zbd_get_max_open_zones - Get the maximum number of open zones |
| 129 | * @td: FIO thread data |
| 130 | * @f: FIO file for which to get max open zones |
| 131 | * @max_open_zones: Upon success, result will be stored here. |
| 132 | * |
| 133 | * A @max_open_zones value set to zero means no limit. |
| 134 | * |
| 135 | * Returns 0 upon success and a negative error code upon failure. |
| 136 | */ |
| 137 | int zbd_get_max_open_zones(struct thread_data *td, struct fio_file *f, |
| 138 | unsigned int *max_open_zones) |
| 139 | { |
| 140 | int ret; |
| 141 | |
| 142 | if (td->io_ops && td->io_ops->get_max_open_zones) |
| 143 | ret = td->io_ops->get_max_open_zones(td, f, max_open_zones); |
| 144 | else |
| 145 | ret = blkzoned_get_max_open_zones(td, f, max_open_zones); |
| 146 | if (ret < 0) { |
| 147 | td_verror(td, errno, "get max open zones failed"); |
| 148 | log_err("%s: get max open zones failed (%d).\n", |
| 149 | f->file_name, errno); |
| 150 | } |
| 151 | |
| 152 | return ret; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * zbd_zone_idx - convert an offset into a zone number |
| 157 | * @f: file pointer. |
| 158 | * @offset: offset in bytes. If this offset is in the first zone_size bytes |
| 159 | * past the disk size then the index of the sentinel is returned. |
| 160 | */ |
| 161 | static uint32_t zbd_zone_idx(const struct fio_file *f, uint64_t offset) |
| 162 | { |
| 163 | uint32_t zone_idx; |
| 164 | |
| 165 | if (f->zbd_info->zone_size_log2 > 0) |
| 166 | zone_idx = offset >> f->zbd_info->zone_size_log2; |
| 167 | else |
| 168 | zone_idx = offset / f->zbd_info->zone_size; |
| 169 | |
| 170 | return min(zone_idx, f->zbd_info->nr_zones); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * zbd_zone_end - Return zone end location |
| 175 | * @z: zone info pointer. |
| 176 | */ |
| 177 | static inline uint64_t zbd_zone_end(const struct fio_zone_info *z) |
| 178 | { |
| 179 | return (z+1)->start; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * zbd_zone_capacity_end - Return zone capacity limit end location |
| 184 | * @z: zone info pointer. |
| 185 | */ |
| 186 | static inline uint64_t zbd_zone_capacity_end(const struct fio_zone_info *z) |
| 187 | { |
| 188 | return z->start + z->capacity; |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * zbd_zone_full - verify whether a minimum number of bytes remain in a zone |
| 193 | * @f: file pointer. |
| 194 | * @z: zone info pointer. |
| 195 | * @required: minimum number of bytes that must remain in a zone. |
| 196 | * |
| 197 | * The caller must hold z->mutex. |
| 198 | */ |
| 199 | static bool zbd_zone_full(const struct fio_file *f, struct fio_zone_info *z, |
| 200 | uint64_t required) |
| 201 | { |
| 202 | assert((required & 511) == 0); |
| 203 | |
| 204 | return z->has_wp && |
| 205 | z->wp + required > zbd_zone_capacity_end(z); |
| 206 | } |
| 207 | |
| 208 | static void zone_lock(struct thread_data *td, const struct fio_file *f, |
| 209 | struct fio_zone_info *z) |
| 210 | { |
| 211 | struct zoned_block_device_info *zbd = f->zbd_info; |
| 212 | uint32_t nz = z - zbd->zone_info; |
| 213 | |
| 214 | /* A thread should never lock zones outside its working area. */ |
| 215 | assert(f->min_zone <= nz && nz < f->max_zone); |
| 216 | |
| 217 | assert(z->has_wp); |
| 218 | |
| 219 | /* |
| 220 | * Lock the io_u target zone. The zone will be unlocked if io_u offset |
| 221 | * is changed or when io_u completes and zbd_put_io() executed. |
| 222 | * To avoid multiple jobs doing asynchronous I/Os from deadlocking each |
| 223 | * other waiting for zone locks when building an io_u batch, first |
| 224 | * only trylock the zone. If the zone is already locked by another job, |
| 225 | * process the currently queued I/Os so that I/O progress is made and |
| 226 | * zones unlocked. |
| 227 | */ |
| 228 | if (pthread_mutex_trylock(&z->mutex) != 0) { |
| 229 | if (!td_ioengine_flagged(td, FIO_SYNCIO)) |
| 230 | io_u_quiesce(td); |
| 231 | pthread_mutex_lock(&z->mutex); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | static inline void zone_unlock(struct fio_zone_info *z) |
| 236 | { |
| 237 | int ret; |
| 238 | |
| 239 | assert(z->has_wp); |
| 240 | ret = pthread_mutex_unlock(&z->mutex); |
| 241 | assert(!ret); |
| 242 | } |
| 243 | |
| 244 | static bool is_valid_offset(const struct fio_file *f, uint64_t offset) |
| 245 | { |
| 246 | return (uint64_t)(offset - f->file_offset) < f->io_size; |
| 247 | } |
| 248 | |
| 249 | static inline struct fio_zone_info *get_zone(const struct fio_file *f, |
| 250 | unsigned int zone_nr) |
| 251 | { |
| 252 | return &f->zbd_info->zone_info[zone_nr]; |
| 253 | } |
| 254 | |
| 255 | /* Verify whether direct I/O is used for all host-managed zoned drives. */ |
| 256 | static bool zbd_using_direct_io(void) |
| 257 | { |
| 258 | struct thread_data *td; |
| 259 | struct fio_file *f; |
| 260 | int i, j; |
| 261 | |
| 262 | for_each_td(td, i) { |
| 263 | if (td->o.odirect || !(td->o.td_ddir & TD_DDIR_WRITE)) |
| 264 | continue; |
| 265 | for_each_file(td, f, j) { |
| 266 | if (f->zbd_info && |
| 267 | f->zbd_info->model == ZBD_HOST_MANAGED) |
| 268 | return false; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | return true; |
| 273 | } |
| 274 | |
| 275 | /* Whether or not the I/O range for f includes one or more sequential zones */ |
| 276 | static bool zbd_is_seq_job(struct fio_file *f) |
| 277 | { |
| 278 | uint32_t zone_idx, zone_idx_b, zone_idx_e; |
| 279 | |
| 280 | assert(f->zbd_info); |
| 281 | if (f->io_size == 0) |
| 282 | return false; |
| 283 | zone_idx_b = zbd_zone_idx(f, f->file_offset); |
| 284 | zone_idx_e = zbd_zone_idx(f, f->file_offset + f->io_size - 1); |
| 285 | for (zone_idx = zone_idx_b; zone_idx <= zone_idx_e; zone_idx++) |
| 286 | if (get_zone(f, zone_idx)->has_wp) |
| 287 | return true; |
| 288 | |
| 289 | return false; |
| 290 | } |
| 291 | |
| 292 | /* |
| 293 | * Verify whether offset and size parameters are aligned with zone boundaries. |
| 294 | */ |
| 295 | static bool zbd_verify_sizes(void) |
| 296 | { |
| 297 | const struct fio_zone_info *z; |
| 298 | struct thread_data *td; |
| 299 | struct fio_file *f; |
| 300 | uint64_t new_offset, new_end; |
| 301 | uint32_t zone_idx; |
| 302 | int i, j; |
| 303 | |
| 304 | for_each_td(td, i) { |
| 305 | for_each_file(td, f, j) { |
| 306 | if (!f->zbd_info) |
| 307 | continue; |
| 308 | if (f->file_offset >= f->real_file_size) |
| 309 | continue; |
| 310 | if (!zbd_is_seq_job(f)) |
| 311 | continue; |
| 312 | |
| 313 | if (!td->o.zone_size) { |
| 314 | td->o.zone_size = f->zbd_info->zone_size; |
| 315 | if (!td->o.zone_size) { |
| 316 | log_err("%s: invalid 0 zone size\n", |
| 317 | f->file_name); |
| 318 | return false; |
| 319 | } |
| 320 | } else if (td->o.zone_size != f->zbd_info->zone_size) { |
| 321 | log_err("%s: job parameter zonesize %llu does not match disk zone size %llu.\n", |
| 322 | f->file_name, (unsigned long long) td->o.zone_size, |
| 323 | (unsigned long long) f->zbd_info->zone_size); |
| 324 | return false; |
| 325 | } |
| 326 | |
| 327 | if (td->o.zone_skip % td->o.zone_size) { |
| 328 | log_err("%s: zoneskip %llu is not a multiple of the device zone size %llu.\n", |
| 329 | f->file_name, (unsigned long long) td->o.zone_skip, |
| 330 | (unsigned long long) td->o.zone_size); |
| 331 | return false; |
| 332 | } |
| 333 | |
| 334 | zone_idx = zbd_zone_idx(f, f->file_offset); |
| 335 | z = get_zone(f, zone_idx); |
| 336 | if ((f->file_offset != z->start) && |
| 337 | (td->o.td_ddir != TD_DDIR_READ)) { |
| 338 | new_offset = zbd_zone_end(z); |
| 339 | if (new_offset >= f->file_offset + f->io_size) { |
| 340 | log_info("%s: io_size must be at least one zone\n", |
| 341 | f->file_name); |
| 342 | return false; |
| 343 | } |
| 344 | log_info("%s: rounded up offset from %llu to %llu\n", |
| 345 | f->file_name, (unsigned long long) f->file_offset, |
| 346 | (unsigned long long) new_offset); |
| 347 | f->io_size -= (new_offset - f->file_offset); |
| 348 | f->file_offset = new_offset; |
| 349 | } |
| 350 | zone_idx = zbd_zone_idx(f, f->file_offset + f->io_size); |
| 351 | z = get_zone(f, zone_idx); |
| 352 | new_end = z->start; |
| 353 | if ((td->o.td_ddir != TD_DDIR_READ) && |
| 354 | (f->file_offset + f->io_size != new_end)) { |
| 355 | if (new_end <= f->file_offset) { |
| 356 | log_info("%s: io_size must be at least one zone\n", |
| 357 | f->file_name); |
| 358 | return false; |
| 359 | } |
| 360 | log_info("%s: rounded down io_size from %llu to %llu\n", |
| 361 | f->file_name, (unsigned long long) f->io_size, |
| 362 | (unsigned long long) new_end - f->file_offset); |
| 363 | f->io_size = new_end - f->file_offset; |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | return true; |
| 369 | } |
| 370 | |
| 371 | static bool zbd_verify_bs(void) |
| 372 | { |
| 373 | struct thread_data *td; |
| 374 | struct fio_file *f; |
| 375 | int i, j, k; |
| 376 | |
| 377 | for_each_td(td, i) { |
| 378 | for_each_file(td, f, j) { |
| 379 | uint64_t zone_size; |
| 380 | |
| 381 | if (!f->zbd_info) |
| 382 | continue; |
| 383 | zone_size = f->zbd_info->zone_size; |
| 384 | for (k = 0; k < FIO_ARRAY_SIZE(td->o.bs); k++) { |
| 385 | if (td->o.verify != VERIFY_NONE && |
| 386 | zone_size % td->o.bs[k] != 0) { |
| 387 | log_info("%s: block size %llu is not a divisor of the zone size %llu\n", |
| 388 | f->file_name, td->o.bs[k], |
| 389 | (unsigned long long)zone_size); |
| 390 | return false; |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | return true; |
| 396 | } |
| 397 | |
| 398 | static int ilog2(uint64_t i) |
| 399 | { |
| 400 | int log = -1; |
| 401 | |
| 402 | while (i) { |
| 403 | i >>= 1; |
| 404 | log++; |
| 405 | } |
| 406 | return log; |
| 407 | } |
| 408 | |
| 409 | /* |
| 410 | * Initialize f->zbd_info for devices that are not zoned block devices. This |
| 411 | * allows to execute a ZBD workload against a non-ZBD device. |
| 412 | */ |
| 413 | static int init_zone_info(struct thread_data *td, struct fio_file *f) |
| 414 | { |
| 415 | uint32_t nr_zones; |
| 416 | struct fio_zone_info *p; |
| 417 | uint64_t zone_size = td->o.zone_size; |
| 418 | uint64_t zone_capacity = td->o.zone_capacity; |
| 419 | struct zoned_block_device_info *zbd_info = NULL; |
| 420 | int i; |
| 421 | |
| 422 | if (zone_size == 0) { |
| 423 | log_err("%s: Specifying the zone size is mandatory for regular file/block device with --zonemode=zbd\n\n", |
| 424 | f->file_name); |
| 425 | return 1; |
| 426 | } |
| 427 | |
| 428 | if (zone_size < 512) { |
| 429 | log_err("%s: zone size must be at least 512 bytes for --zonemode=zbd\n\n", |
| 430 | f->file_name); |
| 431 | return 1; |
| 432 | } |
| 433 | |
| 434 | if (zone_capacity == 0) |
| 435 | zone_capacity = zone_size; |
| 436 | |
| 437 | if (zone_capacity > zone_size) { |
| 438 | log_err("%s: job parameter zonecapacity %llu is larger than zone size %llu\n", |
| 439 | f->file_name, (unsigned long long) td->o.zone_capacity, |
| 440 | (unsigned long long) td->o.zone_size); |
| 441 | return 1; |
| 442 | } |
| 443 | |
| 444 | if (f->real_file_size < zone_size) { |
| 445 | log_err("%s: file/device size %"PRIu64" is smaller than zone size %"PRIu64"\n", |
| 446 | f->file_name, f->real_file_size, zone_size); |
| 447 | return -EINVAL; |
| 448 | } |
| 449 | |
| 450 | nr_zones = (f->real_file_size + zone_size - 1) / zone_size; |
| 451 | zbd_info = scalloc(1, sizeof(*zbd_info) + |
| 452 | (nr_zones + 1) * sizeof(zbd_info->zone_info[0])); |
| 453 | if (!zbd_info) |
| 454 | return -ENOMEM; |
| 455 | |
| 456 | mutex_init_pshared(&zbd_info->mutex); |
| 457 | zbd_info->refcount = 1; |
| 458 | p = &zbd_info->zone_info[0]; |
| 459 | for (i = 0; i < nr_zones; i++, p++) { |
| 460 | mutex_init_pshared_with_type(&p->mutex, |
| 461 | PTHREAD_MUTEX_RECURSIVE); |
| 462 | p->start = i * zone_size; |
| 463 | p->wp = p->start; |
| 464 | p->type = ZBD_ZONE_TYPE_SWR; |
| 465 | p->cond = ZBD_ZONE_COND_EMPTY; |
| 466 | p->capacity = zone_capacity; |
| 467 | p->has_wp = 1; |
| 468 | } |
| 469 | /* a sentinel */ |
| 470 | p->start = nr_zones * zone_size; |
| 471 | |
| 472 | f->zbd_info = zbd_info; |
| 473 | f->zbd_info->zone_size = zone_size; |
| 474 | f->zbd_info->zone_size_log2 = is_power_of_2(zone_size) ? |
| 475 | ilog2(zone_size) : 0; |
| 476 | f->zbd_info->nr_zones = nr_zones; |
| 477 | return 0; |
| 478 | } |
| 479 | |
| 480 | /* |
| 481 | * Maximum number of zones to report in one operation. |
| 482 | */ |
| 483 | #define ZBD_REPORT_MAX_ZONES 8192U |
| 484 | |
| 485 | /* |
| 486 | * Parse the device zone report and store it in f->zbd_info. Must be called |
| 487 | * only for devices that are zoned, namely those with a model != ZBD_NONE. |
| 488 | */ |
| 489 | static int parse_zone_info(struct thread_data *td, struct fio_file *f) |
| 490 | { |
| 491 | int nr_zones, nrz; |
| 492 | struct zbd_zone *zones, *z; |
| 493 | struct fio_zone_info *p; |
| 494 | uint64_t zone_size, offset; |
| 495 | struct zoned_block_device_info *zbd_info = NULL; |
| 496 | int i, j, ret = -ENOMEM; |
| 497 | |
| 498 | zones = calloc(ZBD_REPORT_MAX_ZONES, sizeof(struct zbd_zone)); |
| 499 | if (!zones) |
| 500 | goto out; |
| 501 | |
| 502 | nrz = zbd_report_zones(td, f, 0, zones, ZBD_REPORT_MAX_ZONES); |
| 503 | if (nrz < 0) { |
| 504 | ret = nrz; |
| 505 | log_info("fio: report zones (offset 0) failed for %s (%d).\n", |
| 506 | f->file_name, -ret); |
| 507 | goto out; |
| 508 | } |
| 509 | |
| 510 | zone_size = zones[0].len; |
| 511 | nr_zones = (f->real_file_size + zone_size - 1) / zone_size; |
| 512 | |
| 513 | if (td->o.zone_size == 0) { |
| 514 | td->o.zone_size = zone_size; |
| 515 | } else if (td->o.zone_size != zone_size) { |
| 516 | log_err("fio: %s job parameter zonesize %llu does not match disk zone size %llu.\n", |
| 517 | f->file_name, (unsigned long long) td->o.zone_size, |
| 518 | (unsigned long long) zone_size); |
| 519 | ret = -EINVAL; |
| 520 | goto out; |
| 521 | } |
| 522 | |
| 523 | dprint(FD_ZBD, "Device %s has %d zones of size %llu KB\n", f->file_name, |
| 524 | nr_zones, (unsigned long long) zone_size / 1024); |
| 525 | |
| 526 | zbd_info = scalloc(1, sizeof(*zbd_info) + |
| 527 | (nr_zones + 1) * sizeof(zbd_info->zone_info[0])); |
| 528 | if (!zbd_info) |
| 529 | goto out; |
| 530 | mutex_init_pshared(&zbd_info->mutex); |
| 531 | zbd_info->refcount = 1; |
| 532 | p = &zbd_info->zone_info[0]; |
| 533 | for (offset = 0, j = 0; j < nr_zones;) { |
| 534 | z = &zones[0]; |
| 535 | for (i = 0; i < nrz; i++, j++, z++, p++) { |
| 536 | mutex_init_pshared_with_type(&p->mutex, |
| 537 | PTHREAD_MUTEX_RECURSIVE); |
| 538 | p->start = z->start; |
| 539 | p->capacity = z->capacity; |
| 540 | switch (z->cond) { |
| 541 | case ZBD_ZONE_COND_NOT_WP: |
| 542 | case ZBD_ZONE_COND_FULL: |
| 543 | p->wp = p->start + p->capacity; |
| 544 | break; |
| 545 | default: |
| 546 | assert(z->start <= z->wp); |
| 547 | assert(z->wp <= z->start + zone_size); |
| 548 | p->wp = z->wp; |
| 549 | break; |
| 550 | } |
| 551 | |
| 552 | switch (z->type) { |
| 553 | case ZBD_ZONE_TYPE_SWR: |
| 554 | p->has_wp = 1; |
| 555 | break; |
| 556 | default: |
| 557 | p->has_wp = 0; |
| 558 | } |
| 559 | p->type = z->type; |
| 560 | p->cond = z->cond; |
| 561 | |
| 562 | if (j > 0 && p->start != p[-1].start + zone_size) { |
| 563 | log_info("%s: invalid zone data\n", |
| 564 | f->file_name); |
| 565 | ret = -EINVAL; |
| 566 | goto out; |
| 567 | } |
| 568 | } |
| 569 | z--; |
| 570 | offset = z->start + z->len; |
| 571 | if (j >= nr_zones) |
| 572 | break; |
| 573 | nrz = zbd_report_zones(td, f, offset, zones, |
| 574 | min((uint32_t)(nr_zones - j), |
| 575 | ZBD_REPORT_MAX_ZONES)); |
| 576 | if (nrz < 0) { |
| 577 | ret = nrz; |
| 578 | log_info("fio: report zones (offset %llu) failed for %s (%d).\n", |
| 579 | (unsigned long long)offset, |
| 580 | f->file_name, -ret); |
| 581 | goto out; |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | /* a sentinel */ |
| 586 | zbd_info->zone_info[nr_zones].start = offset; |
| 587 | |
| 588 | f->zbd_info = zbd_info; |
| 589 | f->zbd_info->zone_size = zone_size; |
| 590 | f->zbd_info->zone_size_log2 = is_power_of_2(zone_size) ? |
| 591 | ilog2(zone_size) : 0; |
| 592 | f->zbd_info->nr_zones = nr_zones; |
| 593 | zbd_info = NULL; |
| 594 | ret = 0; |
| 595 | |
| 596 | out: |
| 597 | sfree(zbd_info); |
| 598 | free(zones); |
| 599 | return ret; |
| 600 | } |
| 601 | |
| 602 | static int zbd_set_max_open_zones(struct thread_data *td, struct fio_file *f) |
| 603 | { |
| 604 | struct zoned_block_device_info *zbd = f->zbd_info; |
| 605 | unsigned int max_open_zones; |
| 606 | int ret; |
| 607 | |
| 608 | if (zbd->model != ZBD_HOST_MANAGED || td->o.ignore_zone_limits) { |
| 609 | /* Only host-managed devices have a max open limit */ |
| 610 | zbd->max_open_zones = td->o.max_open_zones; |
| 611 | goto out; |
| 612 | } |
| 613 | |
| 614 | /* If host-managed, get the max open limit */ |
| 615 | ret = zbd_get_max_open_zones(td, f, &max_open_zones); |
| 616 | if (ret) |
| 617 | return ret; |
| 618 | |
| 619 | if (!max_open_zones) { |
| 620 | /* No device limit */ |
| 621 | zbd->max_open_zones = td->o.max_open_zones; |
| 622 | } else if (!td->o.max_open_zones) { |
| 623 | /* No user limit. Set limit to device limit */ |
| 624 | zbd->max_open_zones = max_open_zones; |
| 625 | } else if (td->o.max_open_zones <= max_open_zones) { |
| 626 | /* Both user limit and dev limit. User limit not too large */ |
| 627 | zbd->max_open_zones = td->o.max_open_zones; |
| 628 | } else { |
| 629 | /* Both user limit and dev limit. User limit too large */ |
| 630 | td_verror(td, EINVAL, |
| 631 | "Specified --max_open_zones is too large"); |
| 632 | log_err("Specified --max_open_zones (%d) is larger than max (%u)\n", |
| 633 | td->o.max_open_zones, max_open_zones); |
| 634 | return -EINVAL; |
| 635 | } |
| 636 | |
| 637 | out: |
| 638 | /* Ensure that the limit is not larger than FIO's internal limit */ |
| 639 | if (zbd->max_open_zones > ZBD_MAX_OPEN_ZONES) { |
| 640 | td_verror(td, EINVAL, "'max_open_zones' value is too large"); |
| 641 | log_err("'max_open_zones' value is larger than %u\n", ZBD_MAX_OPEN_ZONES); |
| 642 | return -EINVAL; |
| 643 | } |
| 644 | |
| 645 | dprint(FD_ZBD, "%s: using max open zones limit: %"PRIu32"\n", |
| 646 | f->file_name, zbd->max_open_zones); |
| 647 | |
| 648 | return 0; |
| 649 | } |
| 650 | |
| 651 | /* |
| 652 | * Allocate zone information and store it into f->zbd_info if zonemode=zbd. |
| 653 | * |
| 654 | * Returns 0 upon success and a negative error code upon failure. |
| 655 | */ |
| 656 | static int zbd_create_zone_info(struct thread_data *td, struct fio_file *f) |
| 657 | { |
| 658 | enum zbd_zoned_model zbd_model; |
| 659 | int ret; |
| 660 | |
| 661 | assert(td->o.zone_mode == ZONE_MODE_ZBD); |
| 662 | |
| 663 | ret = zbd_get_zoned_model(td, f, &zbd_model); |
| 664 | if (ret) |
| 665 | return ret; |
| 666 | |
| 667 | switch (zbd_model) { |
| 668 | case ZBD_HOST_AWARE: |
| 669 | case ZBD_HOST_MANAGED: |
| 670 | ret = parse_zone_info(td, f); |
| 671 | if (ret) |
| 672 | return ret; |
| 673 | break; |
| 674 | case ZBD_NONE: |
| 675 | ret = init_zone_info(td, f); |
| 676 | if (ret) |
| 677 | return ret; |
| 678 | break; |
| 679 | default: |
| 680 | td_verror(td, EINVAL, "Unsupported zoned model"); |
| 681 | log_err("Unsupported zoned model\n"); |
| 682 | return -EINVAL; |
| 683 | } |
| 684 | |
| 685 | assert(f->zbd_info); |
| 686 | f->zbd_info->model = zbd_model; |
| 687 | |
| 688 | ret = zbd_set_max_open_zones(td, f); |
| 689 | if (ret) { |
| 690 | zbd_free_zone_info(f); |
| 691 | return ret; |
| 692 | } |
| 693 | |
| 694 | return 0; |
| 695 | } |
| 696 | |
| 697 | void zbd_free_zone_info(struct fio_file *f) |
| 698 | { |
| 699 | uint32_t refcount; |
| 700 | |
| 701 | assert(f->zbd_info); |
| 702 | |
| 703 | pthread_mutex_lock(&f->zbd_info->mutex); |
| 704 | refcount = --f->zbd_info->refcount; |
| 705 | pthread_mutex_unlock(&f->zbd_info->mutex); |
| 706 | |
| 707 | assert((int32_t)refcount >= 0); |
| 708 | if (refcount == 0) |
| 709 | sfree(f->zbd_info); |
| 710 | f->zbd_info = NULL; |
| 711 | } |
| 712 | |
| 713 | /* |
| 714 | * Initialize f->zbd_info. |
| 715 | * |
| 716 | * Returns 0 upon success and a negative error code upon failure. |
| 717 | * |
| 718 | * Note: this function can only work correctly if it is called before the first |
| 719 | * fio fork() call. |
| 720 | */ |
| 721 | static int zbd_init_zone_info(struct thread_data *td, struct fio_file *file) |
| 722 | { |
| 723 | struct thread_data *td2; |
| 724 | struct fio_file *f2; |
| 725 | int i, j, ret; |
| 726 | |
| 727 | for_each_td(td2, i) { |
| 728 | for_each_file(td2, f2, j) { |
| 729 | if (td2 == td && f2 == file) |
| 730 | continue; |
| 731 | if (!f2->zbd_info || |
| 732 | strcmp(f2->file_name, file->file_name) != 0) |
| 733 | continue; |
| 734 | file->zbd_info = f2->zbd_info; |
| 735 | file->zbd_info->refcount++; |
| 736 | return 0; |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | ret = zbd_create_zone_info(td, file); |
| 741 | if (ret < 0) |
| 742 | td_verror(td, -ret, "zbd_create_zone_info() failed"); |
| 743 | return ret; |
| 744 | } |
| 745 | |
| 746 | static bool zbd_open_zone(struct thread_data *td, const struct fio_file *f, |
| 747 | uint32_t zone_idx); |
| 748 | static int zbd_reset_zone(struct thread_data *td, struct fio_file *f, |
| 749 | struct fio_zone_info *z); |
| 750 | |
| 751 | int zbd_init_files(struct thread_data *td) |
| 752 | { |
| 753 | struct fio_file *f; |
| 754 | int i; |
| 755 | |
| 756 | for_each_file(td, f, i) { |
| 757 | if (zbd_init_zone_info(td, f)) |
| 758 | return 1; |
| 759 | } |
| 760 | return 0; |
| 761 | } |
| 762 | |
| 763 | void zbd_recalc_options_with_zone_granularity(struct thread_data *td) |
| 764 | { |
| 765 | struct fio_file *f; |
| 766 | int i; |
| 767 | |
| 768 | for_each_file(td, f, i) { |
| 769 | struct zoned_block_device_info *zbd = f->zbd_info; |
| 770 | // zonemode=strided doesn't get per-file zone size. |
| 771 | uint64_t zone_size = zbd ? zbd->zone_size : td->o.zone_size; |
| 772 | |
| 773 | if (zone_size == 0) |
| 774 | continue; |
| 775 | |
| 776 | if (td->o.size_nz > 0) { |
| 777 | td->o.size = td->o.size_nz * zone_size; |
| 778 | } |
| 779 | if (td->o.io_size_nz > 0) { |
| 780 | td->o.io_size = td->o.io_size_nz * zone_size; |
| 781 | } |
| 782 | if (td->o.start_offset_nz > 0) { |
| 783 | td->o.start_offset = td->o.start_offset_nz * zone_size; |
| 784 | } |
| 785 | if (td->o.offset_increment_nz > 0) { |
| 786 | td->o.offset_increment = td->o.offset_increment_nz * zone_size; |
| 787 | } |
| 788 | if (td->o.zone_skip_nz > 0) { |
| 789 | td->o.zone_skip = td->o.zone_skip_nz * zone_size; |
| 790 | } |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | int zbd_setup_files(struct thread_data *td) |
| 795 | { |
| 796 | struct fio_file *f; |
| 797 | int i; |
| 798 | |
| 799 | if (!zbd_using_direct_io()) { |
| 800 | log_err("Using direct I/O is mandatory for writing to ZBD drives\n\n"); |
| 801 | return 1; |
| 802 | } |
| 803 | |
| 804 | if (!zbd_verify_sizes()) |
| 805 | return 1; |
| 806 | |
| 807 | if (!zbd_verify_bs()) |
| 808 | return 1; |
| 809 | |
| 810 | for_each_file(td, f, i) { |
| 811 | struct zoned_block_device_info *zbd = f->zbd_info; |
| 812 | struct fio_zone_info *z; |
| 813 | int zi; |
| 814 | |
| 815 | assert(zbd); |
| 816 | |
| 817 | f->min_zone = zbd_zone_idx(f, f->file_offset); |
| 818 | f->max_zone = zbd_zone_idx(f, f->file_offset + f->io_size); |
| 819 | |
| 820 | /* |
| 821 | * When all zones in the I/O range are conventional, io_size |
| 822 | * can be smaller than zone size, making min_zone the same |
| 823 | * as max_zone. This is why the assert below needs to be made |
| 824 | * conditional. |
| 825 | */ |
| 826 | if (zbd_is_seq_job(f)) |
| 827 | assert(f->min_zone < f->max_zone); |
| 828 | |
| 829 | if (td->o.max_open_zones > 0 && |
| 830 | zbd->max_open_zones != td->o.max_open_zones) { |
| 831 | log_err("Different 'max_open_zones' values\n"); |
| 832 | return 1; |
| 833 | } |
| 834 | |
| 835 | /* |
| 836 | * The per job max open zones limit cannot be used without a |
| 837 | * global max open zones limit. (As the tracking of open zones |
| 838 | * is disabled when there is no global max open zones limit.) |
| 839 | */ |
| 840 | if (td->o.job_max_open_zones && !zbd->max_open_zones) { |
| 841 | log_err("'job_max_open_zones' cannot be used without a global open zones limit\n"); |
| 842 | return 1; |
| 843 | } |
| 844 | |
| 845 | /* |
| 846 | * zbd->max_open_zones is the global limit shared for all jobs |
| 847 | * that target the same zoned block device. Force sync the per |
| 848 | * thread global limit with the actual global limit. (The real |
| 849 | * per thread/job limit is stored in td->o.job_max_open_zones). |
| 850 | */ |
| 851 | td->o.max_open_zones = zbd->max_open_zones; |
| 852 | |
| 853 | for (zi = f->min_zone; zi < f->max_zone; zi++) { |
| 854 | z = &zbd->zone_info[zi]; |
| 855 | if (z->cond != ZBD_ZONE_COND_IMP_OPEN && |
| 856 | z->cond != ZBD_ZONE_COND_EXP_OPEN) |
| 857 | continue; |
| 858 | if (zbd_open_zone(td, f, zi)) |
| 859 | continue; |
| 860 | /* |
| 861 | * If the number of open zones exceeds specified limits, |
| 862 | * reset all extra open zones. |
| 863 | */ |
| 864 | if (zbd_reset_zone(td, f, z) < 0) { |
| 865 | log_err("Failed to reest zone %d\n", zi); |
| 866 | return 1; |
| 867 | } |
| 868 | } |
| 869 | } |
| 870 | |
| 871 | return 0; |
| 872 | } |
| 873 | |
| 874 | static inline unsigned int zbd_zone_nr(const struct fio_file *f, |
| 875 | struct fio_zone_info *zone) |
| 876 | { |
| 877 | return zone - f->zbd_info->zone_info; |
| 878 | } |
| 879 | |
| 880 | /** |
| 881 | * zbd_reset_zone - reset the write pointer of a single zone |
| 882 | * @td: FIO thread data. |
| 883 | * @f: FIO file associated with the disk for which to reset a write pointer. |
| 884 | * @z: Zone to reset. |
| 885 | * |
| 886 | * Returns 0 upon success and a negative error code upon failure. |
| 887 | * |
| 888 | * The caller must hold z->mutex. |
| 889 | */ |
| 890 | static int zbd_reset_zone(struct thread_data *td, struct fio_file *f, |
| 891 | struct fio_zone_info *z) |
| 892 | { |
| 893 | uint64_t offset = z->start; |
| 894 | uint64_t length = (z+1)->start - offset; |
| 895 | uint64_t data_in_zone = z->wp - z->start; |
| 896 | int ret = 0; |
| 897 | |
| 898 | if (!data_in_zone) |
| 899 | return 0; |
| 900 | |
| 901 | assert(is_valid_offset(f, offset + length - 1)); |
| 902 | |
| 903 | dprint(FD_ZBD, "%s: resetting wp of zone %u.\n", f->file_name, |
| 904 | zbd_zone_nr(f, z)); |
| 905 | switch (f->zbd_info->model) { |
| 906 | case ZBD_HOST_AWARE: |
| 907 | case ZBD_HOST_MANAGED: |
| 908 | ret = zbd_reset_wp(td, f, offset, length); |
| 909 | if (ret < 0) |
| 910 | return ret; |
| 911 | break; |
| 912 | default: |
| 913 | break; |
| 914 | } |
| 915 | |
| 916 | pthread_mutex_lock(&f->zbd_info->mutex); |
| 917 | f->zbd_info->sectors_with_data -= data_in_zone; |
| 918 | f->zbd_info->wp_sectors_with_data -= data_in_zone; |
| 919 | pthread_mutex_unlock(&f->zbd_info->mutex); |
| 920 | z->wp = z->start; |
| 921 | z->verify_block = 0; |
| 922 | |
| 923 | td->ts.nr_zone_resets++; |
| 924 | |
| 925 | return ret; |
| 926 | } |
| 927 | |
| 928 | /* The caller must hold f->zbd_info->mutex */ |
| 929 | static void zbd_close_zone(struct thread_data *td, const struct fio_file *f, |
| 930 | unsigned int zone_idx) |
| 931 | { |
| 932 | uint32_t open_zone_idx = 0; |
| 933 | |
| 934 | for (; open_zone_idx < f->zbd_info->num_open_zones; open_zone_idx++) { |
| 935 | if (f->zbd_info->open_zones[open_zone_idx] == zone_idx) |
| 936 | break; |
| 937 | } |
| 938 | if (open_zone_idx == f->zbd_info->num_open_zones) |
| 939 | return; |
| 940 | |
| 941 | dprint(FD_ZBD, "%s: closing zone %d\n", f->file_name, zone_idx); |
| 942 | memmove(f->zbd_info->open_zones + open_zone_idx, |
| 943 | f->zbd_info->open_zones + open_zone_idx + 1, |
| 944 | (ZBD_MAX_OPEN_ZONES - (open_zone_idx + 1)) * |
| 945 | sizeof(f->zbd_info->open_zones[0])); |
| 946 | f->zbd_info->num_open_zones--; |
| 947 | td->num_open_zones--; |
| 948 | get_zone(f, zone_idx)->open = 0; |
| 949 | } |
| 950 | |
| 951 | /* |
| 952 | * Reset a range of zones. Returns 0 upon success and 1 upon failure. |
| 953 | * @td: fio thread data. |
| 954 | * @f: fio file for which to reset zones |
| 955 | * @zb: first zone to reset. |
| 956 | * @ze: first zone not to reset. |
| 957 | */ |
| 958 | static int zbd_reset_zones(struct thread_data *td, struct fio_file *f, |
| 959 | struct fio_zone_info *const zb, |
| 960 | struct fio_zone_info *const ze) |
| 961 | { |
| 962 | struct fio_zone_info *z; |
| 963 | const uint32_t min_bs = td->o.min_bs[DDIR_WRITE]; |
| 964 | int res = 0; |
| 965 | |
| 966 | assert(min_bs); |
| 967 | |
| 968 | dprint(FD_ZBD, "%s: examining zones %u .. %u\n", f->file_name, |
| 969 | zbd_zone_nr(f, zb), zbd_zone_nr(f, ze)); |
| 970 | for (z = zb; z < ze; z++) { |
| 971 | uint32_t nz = zbd_zone_nr(f, z); |
| 972 | |
| 973 | if (!z->has_wp) |
| 974 | continue; |
| 975 | zone_lock(td, f, z); |
| 976 | pthread_mutex_lock(&f->zbd_info->mutex); |
| 977 | zbd_close_zone(td, f, nz); |
| 978 | pthread_mutex_unlock(&f->zbd_info->mutex); |
| 979 | if (z->wp != z->start) { |
| 980 | dprint(FD_ZBD, "%s: resetting zone %u\n", |
| 981 | f->file_name, zbd_zone_nr(f, z)); |
| 982 | if (zbd_reset_zone(td, f, z) < 0) |
| 983 | res = 1; |
| 984 | } |
| 985 | zone_unlock(z); |
| 986 | } |
| 987 | |
| 988 | return res; |
| 989 | } |
| 990 | |
| 991 | /* |
| 992 | * Reset zbd_info.write_cnt, the counter that counts down towards the next |
| 993 | * zone reset. |
| 994 | */ |
| 995 | static void _zbd_reset_write_cnt(const struct thread_data *td, |
| 996 | const struct fio_file *f) |
| 997 | { |
| 998 | assert(0 <= td->o.zrf.u.f && td->o.zrf.u.f <= 1); |
| 999 | |
| 1000 | f->zbd_info->write_cnt = td->o.zrf.u.f ? |
| 1001 | min(1.0 / td->o.zrf.u.f, 0.0 + UINT_MAX) : UINT_MAX; |
| 1002 | } |
| 1003 | |
| 1004 | static void zbd_reset_write_cnt(const struct thread_data *td, |
| 1005 | const struct fio_file *f) |
| 1006 | { |
| 1007 | pthread_mutex_lock(&f->zbd_info->mutex); |
| 1008 | _zbd_reset_write_cnt(td, f); |
| 1009 | pthread_mutex_unlock(&f->zbd_info->mutex); |
| 1010 | } |
| 1011 | |
| 1012 | static bool zbd_dec_and_reset_write_cnt(const struct thread_data *td, |
| 1013 | const struct fio_file *f) |
| 1014 | { |
| 1015 | uint32_t write_cnt = 0; |
| 1016 | |
| 1017 | pthread_mutex_lock(&f->zbd_info->mutex); |
| 1018 | assert(f->zbd_info->write_cnt); |
| 1019 | if (f->zbd_info->write_cnt) |
| 1020 | write_cnt = --f->zbd_info->write_cnt; |
| 1021 | if (write_cnt == 0) |
| 1022 | _zbd_reset_write_cnt(td, f); |
| 1023 | pthread_mutex_unlock(&f->zbd_info->mutex); |
| 1024 | |
| 1025 | return write_cnt == 0; |
| 1026 | } |
| 1027 | |
| 1028 | enum swd_action { |
| 1029 | CHECK_SWD, |
| 1030 | SET_SWD, |
| 1031 | }; |
| 1032 | |
| 1033 | /* Calculate the number of sectors with data (swd) and perform action 'a' */ |
| 1034 | static uint64_t zbd_process_swd(struct thread_data *td, |
| 1035 | const struct fio_file *f, enum swd_action a) |
| 1036 | { |
| 1037 | struct fio_zone_info *zb, *ze, *z; |
| 1038 | uint64_t swd = 0; |
| 1039 | uint64_t wp_swd = 0; |
| 1040 | |
| 1041 | zb = get_zone(f, f->min_zone); |
| 1042 | ze = get_zone(f, f->max_zone); |
| 1043 | for (z = zb; z < ze; z++) { |
| 1044 | if (z->has_wp) { |
| 1045 | zone_lock(td, f, z); |
| 1046 | wp_swd += z->wp - z->start; |
| 1047 | } |
| 1048 | swd += z->wp - z->start; |
| 1049 | } |
| 1050 | pthread_mutex_lock(&f->zbd_info->mutex); |
| 1051 | switch (a) { |
| 1052 | case CHECK_SWD: |
| 1053 | assert(f->zbd_info->sectors_with_data == swd); |
| 1054 | assert(f->zbd_info->wp_sectors_with_data == wp_swd); |
| 1055 | break; |
| 1056 | case SET_SWD: |
| 1057 | f->zbd_info->sectors_with_data = swd; |
| 1058 | f->zbd_info->wp_sectors_with_data = wp_swd; |
| 1059 | break; |
| 1060 | } |
| 1061 | pthread_mutex_unlock(&f->zbd_info->mutex); |
| 1062 | for (z = zb; z < ze; z++) |
| 1063 | if (z->has_wp) |
| 1064 | zone_unlock(z); |
| 1065 | |
| 1066 | return swd; |
| 1067 | } |
| 1068 | |
| 1069 | /* |
| 1070 | * The swd check is useful for debugging but takes too much time to leave |
| 1071 | * it enabled all the time. Hence it is disabled by default. |
| 1072 | */ |
| 1073 | static const bool enable_check_swd = false; |
| 1074 | |
| 1075 | /* Check whether the values of zbd_info.*sectors_with_data are correct. */ |
| 1076 | static void zbd_check_swd(struct thread_data *td, const struct fio_file *f) |
| 1077 | { |
| 1078 | if (!enable_check_swd) |
| 1079 | return; |
| 1080 | |
| 1081 | zbd_process_swd(td, f, CHECK_SWD); |
| 1082 | } |
| 1083 | |
| 1084 | void zbd_file_reset(struct thread_data *td, struct fio_file *f) |
| 1085 | { |
| 1086 | struct fio_zone_info *zb, *ze; |
| 1087 | uint64_t swd; |
| 1088 | |
| 1089 | if (!f->zbd_info || !td_write(td)) |
| 1090 | return; |
| 1091 | |
| 1092 | zb = get_zone(f, f->min_zone); |
| 1093 | ze = get_zone(f, f->max_zone); |
| 1094 | swd = zbd_process_swd(td, f, SET_SWD); |
| 1095 | dprint(FD_ZBD, "%s(%s): swd = %" PRIu64 "\n", __func__, f->file_name, |
| 1096 | swd); |
| 1097 | /* |
| 1098 | * If data verification is enabled reset the affected zones before |
| 1099 | * writing any data to avoid that a zone reset has to be issued while |
| 1100 | * writing data, which causes data loss. |
| 1101 | */ |
| 1102 | if (td->o.verify != VERIFY_NONE && td->runstate != TD_VERIFYING) |
| 1103 | zbd_reset_zones(td, f, zb, ze); |
| 1104 | zbd_reset_write_cnt(td, f); |
| 1105 | } |
| 1106 | |
| 1107 | /* The caller must hold f->zbd_info->mutex. */ |
| 1108 | static bool is_zone_open(const struct thread_data *td, const struct fio_file *f, |
| 1109 | unsigned int zone_idx) |
| 1110 | { |
| 1111 | struct zoned_block_device_info *zbdi = f->zbd_info; |
| 1112 | int i; |
| 1113 | |
| 1114 | /* This function should never be called when zbdi->max_open_zones == 0 */ |
| 1115 | assert(zbdi->max_open_zones); |
| 1116 | assert(td->o.job_max_open_zones == 0 || td->num_open_zones <= td->o.job_max_open_zones); |
| 1117 | assert(td->o.job_max_open_zones <= zbdi->max_open_zones); |
| 1118 | assert(zbdi->num_open_zones <= zbdi->max_open_zones); |
| 1119 | |
| 1120 | for (i = 0; i < zbdi->num_open_zones; i++) |
| 1121 | if (zbdi->open_zones[i] == zone_idx) |
| 1122 | return true; |
| 1123 | |
| 1124 | return false; |
| 1125 | } |
| 1126 | |
| 1127 | /* |
| 1128 | * Open a ZBD zone if it was not yet open. Returns true if either the zone was |
| 1129 | * already open or if opening a new zone is allowed. Returns false if the zone |
| 1130 | * was not yet open and opening a new zone would cause the zone limit to be |
| 1131 | * exceeded. |
| 1132 | */ |
| 1133 | static bool zbd_open_zone(struct thread_data *td, const struct fio_file *f, |
| 1134 | uint32_t zone_idx) |
| 1135 | { |
| 1136 | const uint32_t min_bs = td->o.min_bs[DDIR_WRITE]; |
| 1137 | struct zoned_block_device_info *zbdi = f->zbd_info; |
| 1138 | struct fio_zone_info *z = get_zone(f, zone_idx); |
| 1139 | bool res = true; |
| 1140 | |
| 1141 | if (z->cond == ZBD_ZONE_COND_OFFLINE) |
| 1142 | return false; |
| 1143 | |
| 1144 | /* |
| 1145 | * Skip full zones with data verification enabled because resetting a |
| 1146 | * zone causes data loss and hence causes verification to fail. |
| 1147 | */ |
| 1148 | if (td->o.verify != VERIFY_NONE && zbd_zone_full(f, z, min_bs)) |
| 1149 | return false; |
| 1150 | |
| 1151 | /* |
| 1152 | * zbdi->max_open_zones == 0 means that there is no limit on the maximum |
| 1153 | * number of open zones. In this case, do no track open zones in |
| 1154 | * zbdi->open_zones array. |
| 1155 | */ |
| 1156 | if (!zbdi->max_open_zones) |
| 1157 | return true; |
| 1158 | |
| 1159 | pthread_mutex_lock(&zbdi->mutex); |
| 1160 | if (is_zone_open(td, f, zone_idx)) { |
| 1161 | /* |
| 1162 | * If the zone is already open and going to be full by writes |
| 1163 | * in-flight, handle it as a full zone instead of an open zone. |
| 1164 | */ |
| 1165 | if (z->wp >= zbd_zone_capacity_end(z)) |
| 1166 | res = false; |
| 1167 | goto out; |
| 1168 | } |
| 1169 | res = false; |
| 1170 | /* Zero means no limit */ |
| 1171 | if (td->o.job_max_open_zones > 0 && |
| 1172 | td->num_open_zones >= td->o.job_max_open_zones) |
| 1173 | goto out; |
| 1174 | if (zbdi->num_open_zones >= zbdi->max_open_zones) |
| 1175 | goto out; |
| 1176 | dprint(FD_ZBD, "%s: opening zone %d\n", f->file_name, zone_idx); |
| 1177 | zbdi->open_zones[zbdi->num_open_zones++] = zone_idx; |
| 1178 | td->num_open_zones++; |
| 1179 | z->open = 1; |
| 1180 | res = true; |
| 1181 | |
| 1182 | out: |
| 1183 | pthread_mutex_unlock(&zbdi->mutex); |
| 1184 | return res; |
| 1185 | } |
| 1186 | |
| 1187 | /* Anything goes as long as it is not a constant. */ |
| 1188 | static uint32_t pick_random_zone_idx(const struct fio_file *f, |
| 1189 | const struct io_u *io_u) |
| 1190 | { |
| 1191 | return io_u->offset * f->zbd_info->num_open_zones / f->real_file_size; |
| 1192 | } |
| 1193 | |
| 1194 | /* |
| 1195 | * Modify the offset of an I/O unit that does not refer to an open zone such |
| 1196 | * that it refers to an open zone. Close an open zone and open a new zone if |
| 1197 | * necessary. The open zone is searched across sequential zones. |
| 1198 | * This algorithm can only work correctly if all write pointers are |
| 1199 | * a multiple of the fio block size. The caller must neither hold z->mutex |
| 1200 | * nor f->zbd_info->mutex. Returns with z->mutex held upon success. |
| 1201 | */ |
| 1202 | static struct fio_zone_info *zbd_convert_to_open_zone(struct thread_data *td, |
| 1203 | struct io_u *io_u) |
| 1204 | { |
| 1205 | const uint32_t min_bs = td->o.min_bs[io_u->ddir]; |
| 1206 | struct fio_file *f = io_u->file; |
| 1207 | struct zoned_block_device_info *zbdi = f->zbd_info; |
| 1208 | struct fio_zone_info *z; |
| 1209 | unsigned int open_zone_idx = -1; |
| 1210 | uint32_t zone_idx, new_zone_idx; |
| 1211 | int i; |
| 1212 | bool wait_zone_close; |
| 1213 | |
| 1214 | assert(is_valid_offset(f, io_u->offset)); |
| 1215 | |
| 1216 | if (zbdi->max_open_zones || td->o.job_max_open_zones) { |
| 1217 | /* |
| 1218 | * This statement accesses zbdi->open_zones[] on purpose |
| 1219 | * without locking. |
| 1220 | */ |
| 1221 | zone_idx = zbdi->open_zones[pick_random_zone_idx(f, io_u)]; |
| 1222 | } else { |
| 1223 | zone_idx = zbd_zone_idx(f, io_u->offset); |
| 1224 | } |
| 1225 | if (zone_idx < f->min_zone) |
| 1226 | zone_idx = f->min_zone; |
| 1227 | else if (zone_idx >= f->max_zone) |
| 1228 | zone_idx = f->max_zone - 1; |
| 1229 | dprint(FD_ZBD, "%s(%s): starting from zone %d (offset %lld, buflen %lld)\n", |
| 1230 | __func__, f->file_name, zone_idx, io_u->offset, io_u->buflen); |
| 1231 | |
| 1232 | /* |
| 1233 | * Since z->mutex is the outer lock and zbdi->mutex the inner |
| 1234 | * lock it can happen that the state of the zone with index zone_idx |
| 1235 | * has changed after 'z' has been assigned and before zbdi->mutex |
| 1236 | * has been obtained. Hence the loop. |
| 1237 | */ |
| 1238 | for (;;) { |
| 1239 | uint32_t tmp_idx; |
| 1240 | |
| 1241 | z = get_zone(f, zone_idx); |
| 1242 | if (z->has_wp) |
| 1243 | zone_lock(td, f, z); |
| 1244 | pthread_mutex_lock(&zbdi->mutex); |
| 1245 | if (z->has_wp) { |
| 1246 | if (z->cond != ZBD_ZONE_COND_OFFLINE && |
| 1247 | zbdi->max_open_zones == 0 && td->o.job_max_open_zones == 0) |
| 1248 | goto examine_zone; |
| 1249 | if (zbdi->num_open_zones == 0) { |
| 1250 | dprint(FD_ZBD, "%s(%s): no zones are open\n", |
| 1251 | __func__, f->file_name); |
| 1252 | goto open_other_zone; |
| 1253 | } |
| 1254 | } |
| 1255 | |
| 1256 | /* |
| 1257 | * List of opened zones is per-device, shared across all threads. |
| 1258 | * Start with quasi-random candidate zone. |
| 1259 | * Ignore zones which don't belong to thread's offset/size area. |
| 1260 | */ |
| 1261 | open_zone_idx = pick_random_zone_idx(f, io_u); |
| 1262 | assert(!open_zone_idx || |
| 1263 | open_zone_idx < zbdi->num_open_zones); |
| 1264 | tmp_idx = open_zone_idx; |
| 1265 | for (i = 0; i < zbdi->num_open_zones; i++) { |
| 1266 | uint32_t tmpz; |
| 1267 | |
| 1268 | if (tmp_idx >= zbdi->num_open_zones) |
| 1269 | tmp_idx = 0; |
| 1270 | tmpz = zbdi->open_zones[tmp_idx]; |
| 1271 | if (f->min_zone <= tmpz && tmpz < f->max_zone) { |
| 1272 | open_zone_idx = tmp_idx; |
| 1273 | goto found_candidate_zone; |
| 1274 | } |
| 1275 | |
| 1276 | tmp_idx++; |
| 1277 | } |
| 1278 | |
| 1279 | dprint(FD_ZBD, "%s(%s): no candidate zone\n", |
| 1280 | __func__, f->file_name); |
| 1281 | pthread_mutex_unlock(&zbdi->mutex); |
| 1282 | if (z->has_wp) |
| 1283 | zone_unlock(z); |
| 1284 | return NULL; |
| 1285 | |
| 1286 | found_candidate_zone: |
| 1287 | new_zone_idx = zbdi->open_zones[open_zone_idx]; |
| 1288 | if (new_zone_idx == zone_idx) |
| 1289 | break; |
| 1290 | zone_idx = new_zone_idx; |
| 1291 | pthread_mutex_unlock(&zbdi->mutex); |
| 1292 | if (z->has_wp) |
| 1293 | zone_unlock(z); |
| 1294 | } |
| 1295 | |
| 1296 | /* Both z->mutex and zbdi->mutex are held. */ |
| 1297 | |
| 1298 | examine_zone: |
| 1299 | if (z->wp + min_bs <= zbd_zone_capacity_end(z)) { |
| 1300 | pthread_mutex_unlock(&zbdi->mutex); |
| 1301 | goto out; |
| 1302 | } |
| 1303 | |
| 1304 | open_other_zone: |
| 1305 | /* Check if number of open zones reaches one of limits. */ |
| 1306 | wait_zone_close = |
| 1307 | zbdi->num_open_zones == f->max_zone - f->min_zone || |
| 1308 | (zbdi->max_open_zones && |
| 1309 | zbdi->num_open_zones == zbdi->max_open_zones) || |
| 1310 | (td->o.job_max_open_zones && |
| 1311 | td->num_open_zones == td->o.job_max_open_zones); |
| 1312 | |
| 1313 | pthread_mutex_unlock(&zbdi->mutex); |
| 1314 | |
| 1315 | /* Only z->mutex is held. */ |
| 1316 | |
| 1317 | /* |
| 1318 | * When number of open zones reaches to one of limits, wait for |
| 1319 | * zone close before opening a new zone. |
| 1320 | */ |
| 1321 | if (wait_zone_close) { |
| 1322 | dprint(FD_ZBD, "%s(%s): quiesce to allow open zones to close\n", |
| 1323 | __func__, f->file_name); |
| 1324 | io_u_quiesce(td); |
| 1325 | } |
| 1326 | |
| 1327 | /* Zone 'z' is full, so try to open a new zone. */ |
| 1328 | for (i = f->io_size / zbdi->zone_size; i > 0; i--) { |
| 1329 | zone_idx++; |
| 1330 | if (z->has_wp) |
| 1331 | zone_unlock(z); |
| 1332 | z++; |
| 1333 | if (!is_valid_offset(f, z->start)) { |
| 1334 | /* Wrap-around. */ |
| 1335 | zone_idx = f->min_zone; |
| 1336 | z = get_zone(f, zone_idx); |
| 1337 | } |
| 1338 | assert(is_valid_offset(f, z->start)); |
| 1339 | if (!z->has_wp) |
| 1340 | continue; |
| 1341 | zone_lock(td, f, z); |
| 1342 | if (z->open) |
| 1343 | continue; |
| 1344 | if (zbd_open_zone(td, f, zone_idx)) |
| 1345 | goto out; |
| 1346 | } |
| 1347 | |
| 1348 | /* Only z->mutex is held. */ |
| 1349 | |
| 1350 | /* Check whether the write fits in any of the already opened zones. */ |
| 1351 | pthread_mutex_lock(&zbdi->mutex); |
| 1352 | for (i = 0; i < zbdi->num_open_zones; i++) { |
| 1353 | zone_idx = zbdi->open_zones[i]; |
| 1354 | if (zone_idx < f->min_zone || zone_idx >= f->max_zone) |
| 1355 | continue; |
| 1356 | pthread_mutex_unlock(&zbdi->mutex); |
| 1357 | zone_unlock(z); |
| 1358 | |
| 1359 | z = get_zone(f, zone_idx); |
| 1360 | |
| 1361 | zone_lock(td, f, z); |
| 1362 | if (z->wp + min_bs <= zbd_zone_capacity_end(z)) |
| 1363 | goto out; |
| 1364 | pthread_mutex_lock(&zbdi->mutex); |
| 1365 | } |
| 1366 | pthread_mutex_unlock(&zbdi->mutex); |
| 1367 | zone_unlock(z); |
| 1368 | dprint(FD_ZBD, "%s(%s): did not open another zone\n", __func__, |
| 1369 | f->file_name); |
| 1370 | return NULL; |
| 1371 | |
| 1372 | out: |
| 1373 | dprint(FD_ZBD, "%s(%s): returning zone %d\n", __func__, f->file_name, |
| 1374 | zone_idx); |
| 1375 | io_u->offset = z->start; |
| 1376 | assert(z->has_wp); |
| 1377 | assert(z->cond != ZBD_ZONE_COND_OFFLINE); |
| 1378 | return z; |
| 1379 | } |
| 1380 | |
| 1381 | /* The caller must hold z->mutex. */ |
| 1382 | static struct fio_zone_info *zbd_replay_write_order(struct thread_data *td, |
| 1383 | struct io_u *io_u, |
| 1384 | struct fio_zone_info *z) |
| 1385 | { |
| 1386 | const struct fio_file *f = io_u->file; |
| 1387 | const uint32_t min_bs = td->o.min_bs[DDIR_WRITE]; |
| 1388 | |
| 1389 | if (!zbd_open_zone(td, f, zbd_zone_nr(f, z))) { |
| 1390 | zone_unlock(z); |
| 1391 | z = zbd_convert_to_open_zone(td, io_u); |
| 1392 | assert(z); |
| 1393 | } |
| 1394 | |
| 1395 | if (z->verify_block * min_bs >= z->capacity) { |
| 1396 | log_err("%s: %d * %d >= %llu\n", f->file_name, z->verify_block, |
| 1397 | min_bs, (unsigned long long)z->capacity); |
| 1398 | /* |
| 1399 | * If the assertion below fails during a test run, adding |
| 1400 | * "--experimental_verify=1" to the command line may help. |
| 1401 | */ |
| 1402 | assert(false); |
| 1403 | } |
| 1404 | io_u->offset = z->start + z->verify_block * min_bs; |
| 1405 | if (io_u->offset + io_u->buflen >= zbd_zone_capacity_end(z)) { |
| 1406 | log_err("%s: %llu + %llu >= %llu\n", f->file_name, io_u->offset, |
| 1407 | io_u->buflen, (unsigned long long) zbd_zone_capacity_end(z)); |
| 1408 | assert(false); |
| 1409 | } |
| 1410 | z->verify_block += io_u->buflen / min_bs; |
| 1411 | |
| 1412 | return z; |
| 1413 | } |
| 1414 | |
| 1415 | /* |
| 1416 | * Find another zone for which @io_u fits in the readable data in the zone. |
| 1417 | * Search in zones @zb + 1 .. @zl. For random workload, also search in zones |
| 1418 | * @zb - 1 .. @zf. |
| 1419 | * |
| 1420 | * Either returns NULL or returns a zone pointer. When the zone has write |
| 1421 | * pointer, hold the mutex for the zone. |
| 1422 | */ |
| 1423 | static struct fio_zone_info * |
| 1424 | zbd_find_zone(struct thread_data *td, struct io_u *io_u, |
| 1425 | struct fio_zone_info *zb, struct fio_zone_info *zl) |
| 1426 | { |
| 1427 | const uint32_t min_bs = td->o.min_bs[io_u->ddir]; |
| 1428 | struct fio_file *f = io_u->file; |
| 1429 | struct fio_zone_info *z1, *z2; |
| 1430 | const struct fio_zone_info *const zf = get_zone(f, f->min_zone); |
| 1431 | |
| 1432 | /* |
| 1433 | * Skip to the next non-empty zone in case of sequential I/O and to |
| 1434 | * the nearest non-empty zone in case of random I/O. |
| 1435 | */ |
| 1436 | for (z1 = zb + 1, z2 = zb - 1; z1 < zl || z2 >= zf; z1++, z2--) { |
| 1437 | if (z1 < zl && z1->cond != ZBD_ZONE_COND_OFFLINE) { |
| 1438 | if (z1->has_wp) |
| 1439 | zone_lock(td, f, z1); |
| 1440 | if (z1->start + min_bs <= z1->wp) |
| 1441 | return z1; |
| 1442 | if (z1->has_wp) |
| 1443 | zone_unlock(z1); |
| 1444 | } else if (!td_random(td)) { |
| 1445 | break; |
| 1446 | } |
| 1447 | if (td_random(td) && z2 >= zf && |
| 1448 | z2->cond != ZBD_ZONE_COND_OFFLINE) { |
| 1449 | if (z2->has_wp) |
| 1450 | zone_lock(td, f, z2); |
| 1451 | if (z2->start + min_bs <= z2->wp) |
| 1452 | return z2; |
| 1453 | if (z2->has_wp) |
| 1454 | zone_unlock(z2); |
| 1455 | } |
| 1456 | } |
| 1457 | dprint(FD_ZBD, "%s: adjusting random read offset failed\n", |
| 1458 | f->file_name); |
| 1459 | return NULL; |
| 1460 | } |
| 1461 | |
| 1462 | /** |
| 1463 | * zbd_end_zone_io - update zone status at command completion |
| 1464 | * @io_u: I/O unit |
| 1465 | * @z: zone info pointer |
| 1466 | * |
| 1467 | * If the write command made the zone full, close it. |
| 1468 | * |
| 1469 | * The caller must hold z->mutex. |
| 1470 | */ |
| 1471 | static void zbd_end_zone_io(struct thread_data *td, const struct io_u *io_u, |
| 1472 | struct fio_zone_info *z) |
| 1473 | { |
| 1474 | const struct fio_file *f = io_u->file; |
| 1475 | |
| 1476 | if (io_u->ddir == DDIR_WRITE && |
| 1477 | io_u->offset + io_u->buflen >= zbd_zone_capacity_end(z)) { |
| 1478 | pthread_mutex_lock(&f->zbd_info->mutex); |
| 1479 | zbd_close_zone(td, f, zbd_zone_nr(f, z)); |
| 1480 | pthread_mutex_unlock(&f->zbd_info->mutex); |
| 1481 | } |
| 1482 | } |
| 1483 | |
| 1484 | /** |
| 1485 | * zbd_queue_io - update the write pointer of a sequential zone |
| 1486 | * @io_u: I/O unit |
| 1487 | * @success: Whether or not the I/O unit has been queued successfully |
| 1488 | * @q: queueing status (busy, completed or queued). |
| 1489 | * |
| 1490 | * For write and trim operations, update the write pointer of the I/O unit |
| 1491 | * target zone. |
| 1492 | */ |
| 1493 | static void zbd_queue_io(struct thread_data *td, struct io_u *io_u, int q, |
| 1494 | bool success) |
| 1495 | { |
| 1496 | const struct fio_file *f = io_u->file; |
| 1497 | struct zoned_block_device_info *zbd_info = f->zbd_info; |
| 1498 | struct fio_zone_info *z; |
| 1499 | uint32_t zone_idx; |
| 1500 | uint64_t zone_end; |
| 1501 | |
| 1502 | assert(zbd_info); |
| 1503 | |
| 1504 | zone_idx = zbd_zone_idx(f, io_u->offset); |
| 1505 | assert(zone_idx < zbd_info->nr_zones); |
| 1506 | z = get_zone(f, zone_idx); |
| 1507 | |
| 1508 | assert(z->has_wp); |
| 1509 | |
| 1510 | if (!success) |
| 1511 | goto unlock; |
| 1512 | |
| 1513 | dprint(FD_ZBD, |
| 1514 | "%s: queued I/O (%lld, %llu) for zone %u\n", |
| 1515 | f->file_name, io_u->offset, io_u->buflen, zone_idx); |
| 1516 | |
| 1517 | switch (io_u->ddir) { |
| 1518 | case DDIR_WRITE: |
| 1519 | zone_end = min((uint64_t)(io_u->offset + io_u->buflen), |
| 1520 | zbd_zone_capacity_end(z)); |
| 1521 | pthread_mutex_lock(&zbd_info->mutex); |
| 1522 | /* |
| 1523 | * z->wp > zone_end means that one or more I/O errors |
| 1524 | * have occurred. |
| 1525 | */ |
| 1526 | if (z->wp <= zone_end) { |
| 1527 | zbd_info->sectors_with_data += zone_end - z->wp; |
| 1528 | zbd_info->wp_sectors_with_data += zone_end - z->wp; |
| 1529 | } |
| 1530 | pthread_mutex_unlock(&zbd_info->mutex); |
| 1531 | z->wp = zone_end; |
| 1532 | break; |
| 1533 | case DDIR_TRIM: |
| 1534 | assert(z->wp == z->start); |
| 1535 | break; |
| 1536 | default: |
| 1537 | break; |
| 1538 | } |
| 1539 | |
| 1540 | if (q == FIO_Q_COMPLETED && !io_u->error) |
| 1541 | zbd_end_zone_io(td, io_u, z); |
| 1542 | |
| 1543 | unlock: |
| 1544 | if (!success || q != FIO_Q_QUEUED) { |
| 1545 | /* BUSY or COMPLETED: unlock the zone */ |
| 1546 | zone_unlock(z); |
| 1547 | io_u->zbd_put_io = NULL; |
| 1548 | } |
| 1549 | } |
| 1550 | |
| 1551 | /** |
| 1552 | * zbd_put_io - Unlock an I/O unit target zone lock |
| 1553 | * @io_u: I/O unit |
| 1554 | */ |
| 1555 | static void zbd_put_io(struct thread_data *td, const struct io_u *io_u) |
| 1556 | { |
| 1557 | const struct fio_file *f = io_u->file; |
| 1558 | struct zoned_block_device_info *zbd_info = f->zbd_info; |
| 1559 | struct fio_zone_info *z; |
| 1560 | uint32_t zone_idx; |
| 1561 | |
| 1562 | assert(zbd_info); |
| 1563 | |
| 1564 | zone_idx = zbd_zone_idx(f, io_u->offset); |
| 1565 | assert(zone_idx < zbd_info->nr_zones); |
| 1566 | z = get_zone(f, zone_idx); |
| 1567 | |
| 1568 | assert(z->has_wp); |
| 1569 | |
| 1570 | dprint(FD_ZBD, |
| 1571 | "%s: terminate I/O (%lld, %llu) for zone %u\n", |
| 1572 | f->file_name, io_u->offset, io_u->buflen, zone_idx); |
| 1573 | |
| 1574 | zbd_end_zone_io(td, io_u, z); |
| 1575 | |
| 1576 | zone_unlock(z); |
| 1577 | zbd_check_swd(td, f); |
| 1578 | } |
| 1579 | |
| 1580 | /* |
| 1581 | * Windows and MacOS do not define this. |
| 1582 | */ |
| 1583 | #ifndef EREMOTEIO |
| 1584 | #define EREMOTEIO 121 /* POSIX value */ |
| 1585 | #endif |
| 1586 | |
| 1587 | bool zbd_unaligned_write(int error_code) |
| 1588 | { |
| 1589 | switch (error_code) { |
| 1590 | case EIO: |
| 1591 | case EREMOTEIO: |
| 1592 | return true; |
| 1593 | } |
| 1594 | return false; |
| 1595 | } |
| 1596 | |
| 1597 | /** |
| 1598 | * setup_zbd_zone_mode - handle zoneskip as necessary for ZBD drives |
| 1599 | * @td: FIO thread data. |
| 1600 | * @io_u: FIO I/O unit. |
| 1601 | * |
| 1602 | * For sequential workloads, change the file offset to skip zoneskip bytes when |
| 1603 | * no more IO can be performed in the current zone. |
| 1604 | * - For read workloads, zoneskip is applied when the io has reached the end of |
| 1605 | * the zone or the zone write position (when td->o.read_beyond_wp is false). |
| 1606 | * - For write workloads, zoneskip is applied when the zone is full. |
| 1607 | * This applies only to read and write operations. |
| 1608 | */ |
| 1609 | void setup_zbd_zone_mode(struct thread_data *td, struct io_u *io_u) |
| 1610 | { |
| 1611 | struct fio_file *f = io_u->file; |
| 1612 | enum fio_ddir ddir = io_u->ddir; |
| 1613 | struct fio_zone_info *z; |
| 1614 | uint32_t zone_idx; |
| 1615 | |
| 1616 | assert(td->o.zone_mode == ZONE_MODE_ZBD); |
| 1617 | assert(td->o.zone_size); |
| 1618 | assert(f->zbd_info); |
| 1619 | |
| 1620 | zone_idx = zbd_zone_idx(f, f->last_pos[ddir]); |
| 1621 | z = get_zone(f, zone_idx); |
| 1622 | |
| 1623 | /* |
| 1624 | * When the zone capacity is smaller than the zone size and the I/O is |
| 1625 | * sequential write, skip to zone end if the latest position is at the |
| 1626 | * zone capacity limit. |
| 1627 | */ |
| 1628 | if (z->capacity < f->zbd_info->zone_size && !td_random(td) && |
| 1629 | ddir == DDIR_WRITE && |
| 1630 | f->last_pos[ddir] >= zbd_zone_capacity_end(z)) { |
| 1631 | dprint(FD_ZBD, |
| 1632 | "%s: Jump from zone capacity limit to zone end:" |
| 1633 | " (%llu -> %llu) for zone %u (%llu)\n", |
| 1634 | f->file_name, (unsigned long long) f->last_pos[ddir], |
| 1635 | (unsigned long long) zbd_zone_end(z), zone_idx, |
| 1636 | (unsigned long long) z->capacity); |
| 1637 | td->io_skip_bytes += zbd_zone_end(z) - f->last_pos[ddir]; |
| 1638 | f->last_pos[ddir] = zbd_zone_end(z); |
| 1639 | } |
| 1640 | |
| 1641 | /* |
| 1642 | * zone_skip is valid only for sequential workloads. |
| 1643 | */ |
| 1644 | if (td_random(td) || !td->o.zone_skip) |
| 1645 | return; |
| 1646 | |
| 1647 | /* |
| 1648 | * It is time to switch to a new zone if: |
| 1649 | * - zone_bytes == zone_size bytes have already been accessed |
| 1650 | * - The last position reached the end of the current zone. |
| 1651 | * - For reads with td->o.read_beyond_wp == false, the last position |
| 1652 | * reached the zone write pointer. |
| 1653 | */ |
| 1654 | if (td->zone_bytes >= td->o.zone_size || |
| 1655 | f->last_pos[ddir] >= zbd_zone_end(z) || |
| 1656 | (ddir == DDIR_READ && |
| 1657 | (!td->o.read_beyond_wp) && f->last_pos[ddir] >= z->wp)) { |
| 1658 | /* |
| 1659 | * Skip zones. |
| 1660 | */ |
| 1661 | td->zone_bytes = 0; |
| 1662 | f->file_offset += td->o.zone_size + td->o.zone_skip; |
| 1663 | |
| 1664 | /* |
| 1665 | * Wrap from the beginning, if we exceed the file size |
| 1666 | */ |
| 1667 | if (f->file_offset >= f->real_file_size) |
| 1668 | f->file_offset = get_start_offset(td, f); |
| 1669 | |
| 1670 | f->last_pos[ddir] = f->file_offset; |
| 1671 | td->io_skip_bytes += td->o.zone_skip; |
| 1672 | } |
| 1673 | } |
| 1674 | |
| 1675 | /** |
| 1676 | * zbd_adjust_ddir - Adjust an I/O direction for zonemode=zbd. |
| 1677 | * |
| 1678 | * @td: FIO thread data. |
| 1679 | * @io_u: FIO I/O unit. |
| 1680 | * @ddir: I/O direction before adjustment. |
| 1681 | * |
| 1682 | * Return adjusted I/O direction. |
| 1683 | */ |
| 1684 | enum fio_ddir zbd_adjust_ddir(struct thread_data *td, struct io_u *io_u, |
| 1685 | enum fio_ddir ddir) |
| 1686 | { |
| 1687 | /* |
| 1688 | * In case read direction is chosen for the first random I/O, fio with |
| 1689 | * zonemode=zbd stops because no data can be read from zoned block |
| 1690 | * devices with all empty zones. Overwrite the first I/O direction as |
| 1691 | * write to make sure data to read exists. |
| 1692 | */ |
| 1693 | assert(io_u->file->zbd_info); |
| 1694 | if (ddir != DDIR_READ || !td_rw(td)) |
| 1695 | return ddir; |
| 1696 | |
| 1697 | if (io_u->file->zbd_info->sectors_with_data || |
| 1698 | td->o.read_beyond_wp) |
| 1699 | return DDIR_READ; |
| 1700 | |
| 1701 | return DDIR_WRITE; |
| 1702 | } |
| 1703 | |
| 1704 | /** |
| 1705 | * zbd_adjust_block - adjust the offset and length as necessary for ZBD drives |
| 1706 | * @td: FIO thread data. |
| 1707 | * @io_u: FIO I/O unit. |
| 1708 | * |
| 1709 | * Locking strategy: returns with z->mutex locked if and only if z refers |
| 1710 | * to a sequential zone and if io_u_accept is returned. z is the zone that |
| 1711 | * corresponds to io_u->offset at the end of this function. |
| 1712 | */ |
| 1713 | enum io_u_action zbd_adjust_block(struct thread_data *td, struct io_u *io_u) |
| 1714 | { |
| 1715 | struct fio_file *f = io_u->file; |
| 1716 | struct zoned_block_device_info *zbdi = f->zbd_info; |
| 1717 | uint32_t zone_idx_b; |
| 1718 | struct fio_zone_info *zb, *zl, *orig_zb; |
| 1719 | uint32_t orig_len = io_u->buflen; |
| 1720 | uint32_t min_bs = td->o.min_bs[io_u->ddir]; |
| 1721 | uint64_t new_len; |
| 1722 | int64_t range; |
| 1723 | |
| 1724 | assert(zbdi); |
| 1725 | assert(min_bs); |
| 1726 | assert(is_valid_offset(f, io_u->offset)); |
| 1727 | assert(io_u->buflen); |
| 1728 | zone_idx_b = zbd_zone_idx(f, io_u->offset); |
| 1729 | zb = get_zone(f, zone_idx_b); |
| 1730 | orig_zb = zb; |
| 1731 | |
| 1732 | if (!zb->has_wp) { |
| 1733 | /* Accept non-write I/Os for conventional zones. */ |
| 1734 | if (io_u->ddir != DDIR_WRITE) |
| 1735 | return io_u_accept; |
| 1736 | /* |
| 1737 | * Make sure that writes to conventional zones |
| 1738 | * don't cross over to any sequential zones. |
| 1739 | */ |
| 1740 | if (!(zb + 1)->has_wp || |
| 1741 | io_u->offset + io_u->buflen <= (zb + 1)->start) |
| 1742 | return io_u_accept; |
| 1743 | |
| 1744 | if (io_u->offset + min_bs > (zb + 1)->start) { |
| 1745 | dprint(FD_IO, |
| 1746 | "%s: off=%llu + min_bs=%u > next zone %llu\n", |
| 1747 | f->file_name, io_u->offset, |
| 1748 | min_bs, (unsigned long long) (zb + 1)->start); |
| 1749 | io_u->offset = zb->start + (zb + 1)->start - io_u->offset; |
| 1750 | new_len = min(io_u->buflen, (zb + 1)->start - io_u->offset); |
| 1751 | } else { |
| 1752 | new_len = (zb + 1)->start - io_u->offset; |
| 1753 | } |
| 1754 | io_u->buflen = new_len / min_bs * min_bs; |
| 1755 | return io_u_accept; |
| 1756 | } |
| 1757 | |
| 1758 | /* |
| 1759 | * Accept the I/O offset for reads if reading beyond the write pointer |
| 1760 | * is enabled. |
| 1761 | */ |
| 1762 | if (zb->cond != ZBD_ZONE_COND_OFFLINE && |
| 1763 | io_u->ddir == DDIR_READ && td->o.read_beyond_wp) |
| 1764 | return io_u_accept; |
| 1765 | |
| 1766 | zbd_check_swd(td, f); |
| 1767 | |
| 1768 | zone_lock(td, f, zb); |
| 1769 | |
| 1770 | switch (io_u->ddir) { |
| 1771 | case DDIR_READ: |
| 1772 | if (td->runstate == TD_VERIFYING && td_write(td)) { |
| 1773 | zb = zbd_replay_write_order(td, io_u, zb); |
| 1774 | goto accept; |
| 1775 | } |
| 1776 | /* |
| 1777 | * Check that there is enough written data in the zone to do an |
| 1778 | * I/O of at least min_bs B. If there isn't, find a new zone for |
| 1779 | * the I/O. |
| 1780 | */ |
| 1781 | range = zb->cond != ZBD_ZONE_COND_OFFLINE ? |
| 1782 | zb->wp - zb->start : 0; |
| 1783 | if (range < min_bs || |
| 1784 | ((!td_random(td)) && (io_u->offset + min_bs > zb->wp))) { |
| 1785 | zone_unlock(zb); |
| 1786 | zl = get_zone(f, f->max_zone); |
| 1787 | zb = zbd_find_zone(td, io_u, zb, zl); |
| 1788 | if (!zb) { |
| 1789 | dprint(FD_ZBD, |
| 1790 | "%s: zbd_find_zone(%lld, %llu) failed\n", |
| 1791 | f->file_name, io_u->offset, |
| 1792 | io_u->buflen); |
| 1793 | goto eof; |
| 1794 | } |
| 1795 | /* |
| 1796 | * zbd_find_zone() returned a zone with a range of at |
| 1797 | * least min_bs. |
| 1798 | */ |
| 1799 | range = zb->wp - zb->start; |
| 1800 | assert(range >= min_bs); |
| 1801 | |
| 1802 | if (!td_random(td)) |
| 1803 | io_u->offset = zb->start; |
| 1804 | } |
| 1805 | /* |
| 1806 | * Make sure the I/O is within the zone valid data range while |
| 1807 | * maximizing the I/O size and preserving randomness. |
| 1808 | */ |
| 1809 | if (range <= io_u->buflen) |
| 1810 | io_u->offset = zb->start; |
| 1811 | else if (td_random(td)) |
| 1812 | io_u->offset = zb->start + |
| 1813 | ((io_u->offset - orig_zb->start) % |
| 1814 | (range - io_u->buflen)) / min_bs * min_bs; |
| 1815 | /* |
| 1816 | * When zbd_find_zone() returns a conventional zone, |
| 1817 | * we can simply accept the new i/o offset here. |
| 1818 | */ |
| 1819 | if (!zb->has_wp) |
| 1820 | return io_u_accept; |
| 1821 | /* |
| 1822 | * Make sure the I/O does not cross over the zone wp position. |
| 1823 | */ |
| 1824 | new_len = min((unsigned long long)io_u->buflen, |
| 1825 | (unsigned long long)(zb->wp - io_u->offset)); |
| 1826 | new_len = new_len / min_bs * min_bs; |
| 1827 | if (new_len < io_u->buflen) { |
| 1828 | io_u->buflen = new_len; |
| 1829 | dprint(FD_IO, "Changed length from %u into %llu\n", |
| 1830 | orig_len, io_u->buflen); |
| 1831 | } |
| 1832 | assert(zb->start <= io_u->offset); |
| 1833 | assert(io_u->offset + io_u->buflen <= zb->wp); |
| 1834 | goto accept; |
| 1835 | case DDIR_WRITE: |
| 1836 | if (io_u->buflen > zbdi->zone_size) { |
| 1837 | td_verror(td, EINVAL, "I/O buflen exceeds zone size"); |
| 1838 | dprint(FD_IO, |
| 1839 | "%s: I/O buflen %llu exceeds zone size %llu\n", |
| 1840 | f->file_name, io_u->buflen, |
| 1841 | (unsigned long long) zbdi->zone_size); |
| 1842 | goto eof; |
| 1843 | } |
| 1844 | if (!zbd_open_zone(td, f, zone_idx_b)) { |
| 1845 | zone_unlock(zb); |
| 1846 | zb = zbd_convert_to_open_zone(td, io_u); |
| 1847 | if (!zb) { |
| 1848 | dprint(FD_IO, "%s: can't convert to open zone", |
| 1849 | f->file_name); |
| 1850 | goto eof; |
| 1851 | } |
| 1852 | zone_idx_b = zbd_zone_nr(f, zb); |
| 1853 | } |
| 1854 | /* Check whether the zone reset threshold has been exceeded */ |
| 1855 | if (td->o.zrf.u.f) { |
| 1856 | if (zbdi->wp_sectors_with_data >= |
| 1857 | f->io_size * td->o.zrt.u.f && |
| 1858 | zbd_dec_and_reset_write_cnt(td, f)) { |
| 1859 | zb->reset_zone = 1; |
| 1860 | } |
| 1861 | } |
| 1862 | /* Reset the zone pointer if necessary */ |
| 1863 | if (zb->reset_zone || zbd_zone_full(f, zb, min_bs)) { |
| 1864 | assert(td->o.verify == VERIFY_NONE); |
| 1865 | /* |
| 1866 | * Since previous write requests may have been submitted |
| 1867 | * asynchronously and since we will submit the zone |
| 1868 | * reset synchronously, wait until previously submitted |
| 1869 | * write requests have completed before issuing a |
| 1870 | * zone reset. |
| 1871 | */ |
| 1872 | io_u_quiesce(td); |
| 1873 | zb->reset_zone = 0; |
| 1874 | if (zbd_reset_zone(td, f, zb) < 0) |
| 1875 | goto eof; |
| 1876 | |
| 1877 | if (zb->capacity < min_bs) { |
| 1878 | td_verror(td, EINVAL, "ZCAP is less min_bs"); |
| 1879 | log_err("zone capacity %llu smaller than minimum block size %d\n", |
| 1880 | (unsigned long long)zb->capacity, |
| 1881 | min_bs); |
| 1882 | goto eof; |
| 1883 | } |
| 1884 | } |
| 1885 | /* Make writes occur at the write pointer */ |
| 1886 | assert(!zbd_zone_full(f, zb, min_bs)); |
| 1887 | io_u->offset = zb->wp; |
| 1888 | if (!is_valid_offset(f, io_u->offset)) { |
| 1889 | td_verror(td, EINVAL, "invalid WP value"); |
| 1890 | dprint(FD_ZBD, "%s: dropped request with offset %llu\n", |
| 1891 | f->file_name, io_u->offset); |
| 1892 | goto eof; |
| 1893 | } |
| 1894 | /* |
| 1895 | * Make sure that the buflen is a multiple of the minimal |
| 1896 | * block size. Give up if shrinking would make the request too |
| 1897 | * small. |
| 1898 | */ |
| 1899 | new_len = min((unsigned long long)io_u->buflen, |
| 1900 | zbd_zone_capacity_end(zb) - io_u->offset); |
| 1901 | new_len = new_len / min_bs * min_bs; |
| 1902 | if (new_len == io_u->buflen) |
| 1903 | goto accept; |
| 1904 | if (new_len >= min_bs) { |
| 1905 | io_u->buflen = new_len; |
| 1906 | dprint(FD_IO, "Changed length from %u into %llu\n", |
| 1907 | orig_len, io_u->buflen); |
| 1908 | goto accept; |
| 1909 | } |
| 1910 | td_verror(td, EIO, "zone remainder too small"); |
| 1911 | log_err("zone remainder %lld smaller than min block size %d\n", |
| 1912 | (zbd_zone_capacity_end(zb) - io_u->offset), min_bs); |
| 1913 | goto eof; |
| 1914 | case DDIR_TRIM: |
| 1915 | /* fall-through */ |
| 1916 | case DDIR_SYNC: |
| 1917 | case DDIR_DATASYNC: |
| 1918 | case DDIR_SYNC_FILE_RANGE: |
| 1919 | case DDIR_WAIT: |
| 1920 | case DDIR_LAST: |
| 1921 | case DDIR_INVAL: |
| 1922 | goto accept; |
| 1923 | } |
| 1924 | |
| 1925 | assert(false); |
| 1926 | |
| 1927 | accept: |
| 1928 | assert(zb->has_wp); |
| 1929 | assert(zb->cond != ZBD_ZONE_COND_OFFLINE); |
| 1930 | assert(!io_u->zbd_queue_io); |
| 1931 | assert(!io_u->zbd_put_io); |
| 1932 | io_u->zbd_queue_io = zbd_queue_io; |
| 1933 | io_u->zbd_put_io = zbd_put_io; |
| 1934 | /* |
| 1935 | * Since we return with the zone lock still held, |
| 1936 | * add an annotation to let Coverity know that it |
| 1937 | * is intentional. |
| 1938 | */ |
| 1939 | /* coverity[missing_unlock] */ |
| 1940 | return io_u_accept; |
| 1941 | |
| 1942 | eof: |
| 1943 | if (zb && zb->has_wp) |
| 1944 | zone_unlock(zb); |
| 1945 | return io_u_eof; |
| 1946 | } |
| 1947 | |
| 1948 | /* Return a string with ZBD statistics */ |
| 1949 | char *zbd_write_status(const struct thread_stat *ts) |
| 1950 | { |
| 1951 | char *res; |
| 1952 | |
| 1953 | if (asprintf(&res, "; %llu zone resets", (unsigned long long) ts->nr_zone_resets) < 0) |
| 1954 | return NULL; |
| 1955 | return res; |
| 1956 | } |