1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 /* 27 * Copyright (c) 2012, 2018 by Delphix. All rights reserved. 28 * Copyright (c) 2014 Integros [integros.com] 29 */ 30 31 #include <sys/zfs_context.h> 32 #include <sys/vdev_impl.h> 33 #include <sys/spa_impl.h> 34 #include <sys/zio.h> 35 #include <sys/avl.h> 36 #include <sys/dsl_pool.h> 37 #include <sys/metaslab_impl.h> 38 #include <sys/abd.h> 39 40 /* 41 * ZFS I/O Scheduler 42 * --------------- 43 * 44 * ZFS issues I/O operations to leaf vdevs to satisfy and complete zios. The 45 * I/O scheduler determines when and in what order those operations are 46 * issued. The I/O scheduler divides operations into five I/O classes 47 * prioritized in the following order: sync read, sync write, async read, 48 * async write, and scrub/resilver. Each queue defines the minimum and 49 * maximum number of concurrent operations that may be issued to the device. 50 * In addition, the device has an aggregate maximum. Note that the sum of the 51 * per-queue minimums must not exceed the aggregate maximum, and if the 52 * aggregate maximum is equal to or greater than the sum of the per-queue 53 * maximums, the per-queue minimum has no effect. 54 * 55 * For many physical devices, throughput increases with the number of 56 * concurrent operations, but latency typically suffers. Further, physical 57 * devices typically have a limit at which more concurrent operations have no 58 * effect on throughput or can actually cause it to decrease. 59 * 60 * The scheduler selects the next operation to issue by first looking for an 61 * I/O class whose minimum has not been satisfied. Once all are satisfied and 62 * the aggregate maximum has not been hit, the scheduler looks for classes 63 * whose maximum has not been satisfied. Iteration through the I/O classes is 64 * done in the order specified above. No further operations are issued if the 65 * aggregate maximum number of concurrent operations has been hit or if there 66 * are no operations queued for an I/O class that has not hit its maximum. 67 * Every time an i/o is queued or an operation completes, the I/O scheduler 68 * looks for new operations to issue. 69 * 70 * All I/O classes have a fixed maximum number of outstanding operations 71 * except for the async write class. Asynchronous writes represent the data 72 * that is committed to stable storage during the syncing stage for 73 * transaction groups (see txg.c). Transaction groups enter the syncing state 74 * periodically so the number of queued async writes will quickly burst up and 75 * then bleed down to zero. Rather than servicing them as quickly as possible, 76 * the I/O scheduler changes the maximum number of active async write i/os 77 * according to the amount of dirty data in the pool (see dsl_pool.c). Since 78 * both throughput and latency typically increase with the number of 79 * concurrent operations issued to physical devices, reducing the burstiness 80 * in the number of concurrent operations also stabilizes the response time of 81 * operations from other -- and in particular synchronous -- queues. In broad 82 * strokes, the I/O scheduler will issue more concurrent operations from the 83 * async write queue as there's more dirty data in the pool. 84 * 85 * Async Writes 86 * 87 * The number of concurrent operations issued for the async write I/O class 88 * follows a piece-wise linear function defined by a few adjustable points. 89 * 90 * | o---------| <-- zfs_vdev_async_write_max_active 91 * ^ | /^ | 92 * | | / | | 93 * active | / | | 94 * I/O | / | | 95 * count | / | | 96 * | / | | 97 * |------------o | | <-- zfs_vdev_async_write_min_active 98 * 0|____________^______|_________| 99 * 0% | | 100% of zfs_dirty_data_max 100 * | | 101 * | `-- zfs_vdev_async_write_active_max_dirty_percent 102 * `--------- zfs_vdev_async_write_active_min_dirty_percent 103 * 104 * Until the amount of dirty data exceeds a minimum percentage of the dirty 105 * data allowed in the pool, the I/O scheduler will limit the number of 106 * concurrent operations to the minimum. As that threshold is crossed, the 107 * number of concurrent operations issued increases linearly to the maximum at 108 * the specified maximum percentage of the dirty data allowed in the pool. 109 * 110 * Ideally, the amount of dirty data on a busy pool will stay in the sloped 111 * part of the function between zfs_vdev_async_write_active_min_dirty_percent 112 * and zfs_vdev_async_write_active_max_dirty_percent. If it exceeds the 113 * maximum percentage, this indicates that the rate of incoming data is 114 * greater than the rate that the backend storage can handle. In this case, we 115 * must further throttle incoming writes (see dmu_tx_delay() for details). 116 */ 117 118 /* 119 * The maximum number of i/os active to each device. Ideally, this will be >= 120 * the sum of each queue's max_active. It must be at least the sum of each 121 * queue's min_active. 122 */ 123 uint32_t zfs_vdev_max_active = 1000; 124 125 /* 126 * Per-queue limits on the number of i/os active to each device. If the 127 * sum of the queue's max_active is < zfs_vdev_max_active, then the 128 * min_active comes into play. We will send min_active from each queue, 129 * and then select from queues in the order defined by zio_priority_t. 130 * 131 * In general, smaller max_active's will lead to lower latency of synchronous 132 * operations. Larger max_active's may lead to higher overall throughput, 133 * depending on underlying storage. 134 * 135 * The ratio of the queues' max_actives determines the balance of performance 136 * between reads, writes, and scrubs. E.g., increasing 137 * zfs_vdev_scrub_max_active will cause the scrub or resilver to complete 138 * more quickly, but reads and writes to have higher latency and lower 139 * throughput. 140 */ 141 uint32_t zfs_vdev_sync_read_min_active = 10; 142 uint32_t zfs_vdev_sync_read_max_active = 10; 143 uint32_t zfs_vdev_sync_write_min_active = 10; 144 uint32_t zfs_vdev_sync_write_max_active = 10; 145 uint32_t zfs_vdev_async_read_min_active = 1; 146 uint32_t zfs_vdev_async_read_max_active = 3; 147 uint32_t zfs_vdev_async_write_min_active = 1; 148 uint32_t zfs_vdev_async_write_max_active = 10; 149 uint32_t zfs_vdev_scrub_min_active = 1; 150 uint32_t zfs_vdev_scrub_max_active = 2; 151 uint32_t zfs_vdev_removal_min_active = 1; 152 uint32_t zfs_vdev_removal_max_active = 2; 153 uint32_t zfs_vdev_initializing_min_active = 1; 154 uint32_t zfs_vdev_initializing_max_active = 1; 155 156 /* 157 * When the pool has less than zfs_vdev_async_write_active_min_dirty_percent 158 * dirty data, use zfs_vdev_async_write_min_active. When it has more than 159 * zfs_vdev_async_write_active_max_dirty_percent, use 160 * zfs_vdev_async_write_max_active. The value is linearly interpolated 161 * between min and max. 162 */ 163 int zfs_vdev_async_write_active_min_dirty_percent = 30; 164 int zfs_vdev_async_write_active_max_dirty_percent = 60; 165 166 /* 167 * To reduce IOPs, we aggregate small adjacent I/Os into one large I/O. 168 * For read I/Os, we also aggregate across small adjacency gaps; for writes 169 * we include spans of optional I/Os to aid aggregation at the disk even when 170 * they aren't able to help us aggregate at this level. 171 */ 172 int zfs_vdev_aggregation_limit = 1 << 20; 173 int zfs_vdev_read_gap_limit = 32 << 10; 174 int zfs_vdev_write_gap_limit = 4 << 10; 175 176 /* 177 * Define the queue depth percentage for each top-level. This percentage is 178 * used in conjunction with zfs_vdev_async_max_active to determine how many 179 * allocations a specific top-level vdev should handle. Once the queue depth 180 * reaches zfs_vdev_queue_depth_pct * zfs_vdev_async_write_max_active / 100 181 * then allocator will stop allocating blocks on that top-level device. 182 * The default kernel setting is 1000% which will yield 100 allocations per 183 * device. For userland testing, the default setting is 300% which equates 184 * to 30 allocations per device. 185 */ 186 #ifdef _KERNEL 187 int zfs_vdev_queue_depth_pct = 1000; 188 #else 189 int zfs_vdev_queue_depth_pct = 300; 190 #endif 191 192 /* 193 * When performing allocations for a given metaslab, we want to make sure that 194 * there are enough IOs to aggregate together to improve throughput. We want to 195 * ensure that there are at least 128k worth of IOs that can be aggregated, and 196 * we assume that the average allocation size is 4k, so we need the queue depth 197 * to be 32 per allocator to get good aggregation of sequential writes. 198 */ 199 int zfs_vdev_def_queue_depth = 32; 200 201 202 int 203 vdev_queue_offset_compare(const void *x1, const void *x2) 204 { 205 const zio_t *z1 = (const zio_t *)x1; 206 const zio_t *z2 = (const zio_t *)x2; 207 208 int cmp = AVL_CMP(z1->io_offset, z2->io_offset); 209 210 if (likely(cmp)) 211 return (cmp); 212 213 return (AVL_PCMP(z1, z2)); 214 } 215 216 static inline avl_tree_t * 217 vdev_queue_class_tree(vdev_queue_t *vq, zio_priority_t p) 218 { 219 return (&vq->vq_class[p].vqc_queued_tree); 220 } 221 222 static inline avl_tree_t * 223 vdev_queue_type_tree(vdev_queue_t *vq, zio_type_t t) 224 { 225 ASSERT(t == ZIO_TYPE_READ || t == ZIO_TYPE_WRITE); 226 if (t == ZIO_TYPE_READ) 227 return (&vq->vq_read_offset_tree); 228 else 229 return (&vq->vq_write_offset_tree); 230 } 231 232 int 233 vdev_queue_timestamp_compare(const void *x1, const void *x2) 234 { 235 const zio_t *z1 = (const zio_t *)x1; 236 const zio_t *z2 = (const zio_t *)x2; 237 238 int cmp = AVL_CMP(z1->io_timestamp, z2->io_timestamp); 239 240 if (likely(cmp)) 241 return (cmp); 242 243 return (AVL_PCMP(z1, z2)); 244 } 245 246 void 247 vdev_queue_init(vdev_t *vd) 248 { 249 vdev_queue_t *vq = &vd->vdev_queue; 250 251 mutex_init(&vq->vq_lock, NULL, MUTEX_DEFAULT, NULL); 252 vq->vq_vdev = vd; 253 254 avl_create(&vq->vq_active_tree, vdev_queue_offset_compare, 255 sizeof (zio_t), offsetof(struct zio, io_queue_node)); 256 avl_create(vdev_queue_type_tree(vq, ZIO_TYPE_READ), 257 vdev_queue_offset_compare, sizeof (zio_t), 258 offsetof(struct zio, io_offset_node)); 259 avl_create(vdev_queue_type_tree(vq, ZIO_TYPE_WRITE), 260 vdev_queue_offset_compare, sizeof (zio_t), 261 offsetof(struct zio, io_offset_node)); 262 263 for (zio_priority_t p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) { 264 int (*compfn) (const void *, const void *); 265 266 /* 267 * The synchronous i/o queues are dispatched in FIFO rather 268 * than LBA order. This provides more consistent latency for 269 * these i/os. 270 */ 271 if (p == ZIO_PRIORITY_SYNC_READ || p == ZIO_PRIORITY_SYNC_WRITE) 272 compfn = vdev_queue_timestamp_compare; 273 else 274 compfn = vdev_queue_offset_compare; 275 276 avl_create(vdev_queue_class_tree(vq, p), compfn, 277 sizeof (zio_t), offsetof(struct zio, io_queue_node)); 278 } 279 } 280 281 void 282 vdev_queue_fini(vdev_t *vd) 283 { 284 vdev_queue_t *vq = &vd->vdev_queue; 285 286 for (zio_priority_t p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) 287 avl_destroy(vdev_queue_class_tree(vq, p)); 288 avl_destroy(&vq->vq_active_tree); 289 avl_destroy(vdev_queue_type_tree(vq, ZIO_TYPE_READ)); 290 avl_destroy(vdev_queue_type_tree(vq, ZIO_TYPE_WRITE)); 291 292 mutex_destroy(&vq->vq_lock); 293 } 294 295 static void 296 vdev_queue_io_add(vdev_queue_t *vq, zio_t *zio) 297 { 298 spa_t *spa = zio->io_spa; 299 300 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE); 301 avl_add(vdev_queue_class_tree(vq, zio->io_priority), zio); 302 avl_add(vdev_queue_type_tree(vq, zio->io_type), zio); 303 304 mutex_enter(&spa->spa_iokstat_lock); 305 spa->spa_queue_stats[zio->io_priority].spa_queued++; 306 if (spa->spa_iokstat != NULL) 307 kstat_waitq_enter(spa->spa_iokstat->ks_data); 308 mutex_exit(&spa->spa_iokstat_lock); 309 } 310 311 static void 312 vdev_queue_io_remove(vdev_queue_t *vq, zio_t *zio) 313 { 314 spa_t *spa = zio->io_spa; 315 316 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE); 317 avl_remove(vdev_queue_class_tree(vq, zio->io_priority), zio); 318 avl_remove(vdev_queue_type_tree(vq, zio->io_type), zio); 319 320 mutex_enter(&spa->spa_iokstat_lock); 321 ASSERT3U(spa->spa_queue_stats[zio->io_priority].spa_queued, >, 0); 322 spa->spa_queue_stats[zio->io_priority].spa_queued--; 323 if (spa->spa_iokstat != NULL) 324 kstat_waitq_exit(spa->spa_iokstat->ks_data); 325 mutex_exit(&spa->spa_iokstat_lock); 326 } 327 328 static void 329 vdev_queue_pending_add(vdev_queue_t *vq, zio_t *zio) 330 { 331 spa_t *spa = zio->io_spa; 332 ASSERT(MUTEX_HELD(&vq->vq_lock)); 333 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE); 334 vq->vq_class[zio->io_priority].vqc_active++; 335 avl_add(&vq->vq_active_tree, zio); 336 337 mutex_enter(&spa->spa_iokstat_lock); 338 spa->spa_queue_stats[zio->io_priority].spa_active++; 339 if (spa->spa_iokstat != NULL) 340 kstat_runq_enter(spa->spa_iokstat->ks_data); 341 mutex_exit(&spa->spa_iokstat_lock); 342 } 343 344 static void 345 vdev_queue_pending_remove(vdev_queue_t *vq, zio_t *zio) 346 { 347 spa_t *spa = zio->io_spa; 348 ASSERT(MUTEX_HELD(&vq->vq_lock)); 349 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE); 350 vq->vq_class[zio->io_priority].vqc_active--; 351 avl_remove(&vq->vq_active_tree, zio); 352 353 mutex_enter(&spa->spa_iokstat_lock); 354 ASSERT3U(spa->spa_queue_stats[zio->io_priority].spa_active, >, 0); 355 spa->spa_queue_stats[zio->io_priority].spa_active--; 356 if (spa->spa_iokstat != NULL) { 357 kstat_io_t *ksio = spa->spa_iokstat->ks_data; 358 359 kstat_runq_exit(spa->spa_iokstat->ks_data); 360 if (zio->io_type == ZIO_TYPE_READ) { 361 ksio->reads++; 362 ksio->nread += zio->io_size; 363 } else if (zio->io_type == ZIO_TYPE_WRITE) { 364 ksio->writes++; 365 ksio->nwritten += zio->io_size; 366 } 367 } 368 mutex_exit(&spa->spa_iokstat_lock); 369 } 370 371 static void 372 vdev_queue_agg_io_done(zio_t *aio) 373 { 374 if (aio->io_type == ZIO_TYPE_READ) { 375 zio_t *pio; 376 zio_link_t *zl = NULL; 377 while ((pio = zio_walk_parents(aio, &zl)) != NULL) { 378 abd_copy_off(pio->io_abd, aio->io_abd, 379 0, pio->io_offset - aio->io_offset, pio->io_size); 380 } 381 } 382 383 abd_free(aio->io_abd); 384 } 385 386 static int 387 vdev_queue_class_min_active(zio_priority_t p) 388 { 389 switch (p) { 390 case ZIO_PRIORITY_SYNC_READ: 391 return (zfs_vdev_sync_read_min_active); 392 case ZIO_PRIORITY_SYNC_WRITE: 393 return (zfs_vdev_sync_write_min_active); 394 case ZIO_PRIORITY_ASYNC_READ: 395 return (zfs_vdev_async_read_min_active); 396 case ZIO_PRIORITY_ASYNC_WRITE: 397 return (zfs_vdev_async_write_min_active); 398 case ZIO_PRIORITY_SCRUB: 399 return (zfs_vdev_scrub_min_active); 400 case ZIO_PRIORITY_REMOVAL: 401 return (zfs_vdev_removal_min_active); 402 case ZIO_PRIORITY_INITIALIZING: 403 return (zfs_vdev_initializing_min_active); 404 default: 405 panic("invalid priority %u", p); 406 return (0); 407 } 408 } 409 410 static int 411 vdev_queue_max_async_writes(spa_t *spa) 412 { 413 int writes; 414 uint64_t dirty = spa->spa_dsl_pool->dp_dirty_total; 415 uint64_t min_bytes = zfs_dirty_data_max * 416 zfs_vdev_async_write_active_min_dirty_percent / 100; 417 uint64_t max_bytes = zfs_dirty_data_max * 418 zfs_vdev_async_write_active_max_dirty_percent / 100; 419 420 /* 421 * Sync tasks correspond to interactive user actions. To reduce the 422 * execution time of those actions we push data out as fast as possible. 423 */ 424 if (spa_has_pending_synctask(spa)) { 425 return (zfs_vdev_async_write_max_active); 426 } 427 428 if (dirty < min_bytes) 429 return (zfs_vdev_async_write_min_active); 430 if (dirty > max_bytes) 431 return (zfs_vdev_async_write_max_active); 432 433 /* 434 * linear interpolation: 435 * slope = (max_writes - min_writes) / (max_bytes - min_bytes) 436 * move right by min_bytes 437 * move up by min_writes 438 */ 439 writes = (dirty - min_bytes) * 440 (zfs_vdev_async_write_max_active - 441 zfs_vdev_async_write_min_active) / 442 (max_bytes - min_bytes) + 443 zfs_vdev_async_write_min_active; 444 ASSERT3U(writes, >=, zfs_vdev_async_write_min_active); 445 ASSERT3U(writes, <=, zfs_vdev_async_write_max_active); 446 return (writes); 447 } 448 449 static int 450 vdev_queue_class_max_active(spa_t *spa, zio_priority_t p) 451 { 452 switch (p) { 453 case ZIO_PRIORITY_SYNC_READ: 454 return (zfs_vdev_sync_read_max_active); 455 case ZIO_PRIORITY_SYNC_WRITE: 456 return (zfs_vdev_sync_write_max_active); 457 case ZIO_PRIORITY_ASYNC_READ: 458 return (zfs_vdev_async_read_max_active); 459 case ZIO_PRIORITY_ASYNC_WRITE: 460 return (vdev_queue_max_async_writes(spa)); 461 case ZIO_PRIORITY_SCRUB: 462 return (zfs_vdev_scrub_max_active); 463 case ZIO_PRIORITY_REMOVAL: 464 return (zfs_vdev_removal_max_active); 465 case ZIO_PRIORITY_INITIALIZING: 466 return (zfs_vdev_initializing_max_active); 467 default: 468 panic("invalid priority %u", p); 469 return (0); 470 } 471 } 472 473 /* 474 * Return the i/o class to issue from, or ZIO_PRIORITY_MAX_QUEUEABLE if 475 * there is no eligible class. 476 */ 477 static zio_priority_t 478 vdev_queue_class_to_issue(vdev_queue_t *vq) 479 { 480 spa_t *spa = vq->vq_vdev->vdev_spa; 481 zio_priority_t p; 482 483 if (avl_numnodes(&vq->vq_active_tree) >= zfs_vdev_max_active) 484 return (ZIO_PRIORITY_NUM_QUEUEABLE); 485 486 /* find a queue that has not reached its minimum # outstanding i/os */ 487 for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) { 488 if (avl_numnodes(vdev_queue_class_tree(vq, p)) > 0 && 489 vq->vq_class[p].vqc_active < 490 vdev_queue_class_min_active(p)) 491 return (p); 492 } 493 494 /* 495 * If we haven't found a queue, look for one that hasn't reached its 496 * maximum # outstanding i/os. 497 */ 498 for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) { 499 if (avl_numnodes(vdev_queue_class_tree(vq, p)) > 0 && 500 vq->vq_class[p].vqc_active < 501 vdev_queue_class_max_active(spa, p)) 502 return (p); 503 } 504 505 /* No eligible queued i/os */ 506 return (ZIO_PRIORITY_NUM_QUEUEABLE); 507 } 508 509 /* 510 * Compute the range spanned by two i/os, which is the endpoint of the last 511 * (lio->io_offset + lio->io_size) minus start of the first (fio->io_offset). 512 * Conveniently, the gap between fio and lio is given by -IO_SPAN(lio, fio); 513 * thus fio and lio are adjacent if and only if IO_SPAN(lio, fio) == 0. 514 */ 515 #define IO_SPAN(fio, lio) ((lio)->io_offset + (lio)->io_size - (fio)->io_offset) 516 #define IO_GAP(fio, lio) (-IO_SPAN(lio, fio)) 517 518 static zio_t * 519 vdev_queue_aggregate(vdev_queue_t *vq, zio_t *zio) 520 { 521 zio_t *first, *last, *aio, *dio, *mandatory, *nio; 522 zio_link_t *zl = NULL; 523 uint64_t maxgap = 0; 524 uint64_t size; 525 boolean_t stretch = B_FALSE; 526 avl_tree_t *t = vdev_queue_type_tree(vq, zio->io_type); 527 enum zio_flag flags = zio->io_flags & ZIO_FLAG_AGG_INHERIT; 528 529 if (zio->io_flags & ZIO_FLAG_DONT_AGGREGATE) 530 return (NULL); 531 532 first = last = zio; 533 534 if (zio->io_type == ZIO_TYPE_READ) 535 maxgap = zfs_vdev_read_gap_limit; 536 537 /* 538 * We can aggregate I/Os that are sufficiently adjacent and of 539 * the same flavor, as expressed by the AGG_INHERIT flags. 540 * The latter requirement is necessary so that certain 541 * attributes of the I/O, such as whether it's a normal I/O 542 * or a scrub/resilver, can be preserved in the aggregate. 543 * We can include optional I/Os, but don't allow them 544 * to begin a range as they add no benefit in that situation. 545 */ 546 547 /* 548 * We keep track of the last non-optional I/O. 549 */ 550 mandatory = (first->io_flags & ZIO_FLAG_OPTIONAL) ? NULL : first; 551 552 /* 553 * Walk backwards through sufficiently contiguous I/Os 554 * recording the last non-optional I/O. 555 */ 556 while ((dio = AVL_PREV(t, first)) != NULL && 557 (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags && 558 IO_SPAN(dio, last) <= zfs_vdev_aggregation_limit && 559 IO_GAP(dio, first) <= maxgap && 560 dio->io_type == zio->io_type) { 561 first = dio; 562 if (mandatory == NULL && !(first->io_flags & ZIO_FLAG_OPTIONAL)) 563 mandatory = first; 564 } 565 566 /* 567 * Skip any initial optional I/Os. 568 */ 569 while ((first->io_flags & ZIO_FLAG_OPTIONAL) && first != last) { 570 first = AVL_NEXT(t, first); 571 ASSERT(first != NULL); 572 } 573 574 /* 575 * Walk forward through sufficiently contiguous I/Os. 576 * The aggregation limit does not apply to optional i/os, so that 577 * we can issue contiguous writes even if they are larger than the 578 * aggregation limit. 579 */ 580 while ((dio = AVL_NEXT(t, last)) != NULL && 581 (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags && 582 (IO_SPAN(first, dio) <= zfs_vdev_aggregation_limit || 583 (dio->io_flags & ZIO_FLAG_OPTIONAL)) && 584 IO_GAP(last, dio) <= maxgap && 585 dio->io_type == zio->io_type) { 586 last = dio; 587 if (!(last->io_flags & ZIO_FLAG_OPTIONAL)) 588 mandatory = last; 589 } 590 591 /* 592 * Now that we've established the range of the I/O aggregation 593 * we must decide what to do with trailing optional I/Os. 594 * For reads, there's nothing to do. While we are unable to 595 * aggregate further, it's possible that a trailing optional 596 * I/O would allow the underlying device to aggregate with 597 * subsequent I/Os. We must therefore determine if the next 598 * non-optional I/O is close enough to make aggregation 599 * worthwhile. 600 */ 601 if (zio->io_type == ZIO_TYPE_WRITE && mandatory != NULL) { 602 zio_t *nio = last; 603 while ((dio = AVL_NEXT(t, nio)) != NULL && 604 IO_GAP(nio, dio) == 0 && 605 IO_GAP(mandatory, dio) <= zfs_vdev_write_gap_limit) { 606 nio = dio; 607 if (!(nio->io_flags & ZIO_FLAG_OPTIONAL)) { 608 stretch = B_TRUE; 609 break; 610 } 611 } 612 } 613 614 if (stretch) { 615 /* 616 * We are going to include an optional io in our aggregated 617 * span, thus closing the write gap. Only mandatory i/os can 618 * start aggregated spans, so make sure that the next i/o 619 * after our span is mandatory. 620 */ 621 dio = AVL_NEXT(t, last); 622 dio->io_flags &= ~ZIO_FLAG_OPTIONAL; 623 } else { 624 /* do not include the optional i/o */ 625 while (last != mandatory && last != first) { 626 ASSERT(last->io_flags & ZIO_FLAG_OPTIONAL); 627 last = AVL_PREV(t, last); 628 ASSERT(last != NULL); 629 } 630 } 631 632 if (first == last) 633 return (NULL); 634 635 size = IO_SPAN(first, last); 636 ASSERT3U(size, <=, SPA_MAXBLOCKSIZE); 637 638 aio = zio_vdev_delegated_io(first->io_vd, first->io_offset, 639 abd_alloc_for_io(size, B_TRUE), size, first->io_type, 640 zio->io_priority, flags | ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE, 641 vdev_queue_agg_io_done, NULL); 642 aio->io_timestamp = first->io_timestamp; 643 644 nio = first; 645 do { 646 dio = nio; 647 nio = AVL_NEXT(t, dio); 648 ASSERT3U(dio->io_type, ==, aio->io_type); 649 650 if (dio->io_flags & ZIO_FLAG_NODATA) { 651 ASSERT3U(dio->io_type, ==, ZIO_TYPE_WRITE); 652 abd_zero_off(aio->io_abd, 653 dio->io_offset - aio->io_offset, dio->io_size); 654 } else if (dio->io_type == ZIO_TYPE_WRITE) { 655 abd_copy_off(aio->io_abd, dio->io_abd, 656 dio->io_offset - aio->io_offset, 0, dio->io_size); 657 } 658 659 zio_add_child(dio, aio); 660 vdev_queue_io_remove(vq, dio); 661 } while (dio != last); 662 663 /* 664 * We need to drop the vdev queue's lock to avoid a deadlock that we 665 * could encounter since this I/O will complete immediately. 666 */ 667 mutex_exit(&vq->vq_lock); 668 while ((dio = zio_walk_parents(aio, &zl)) != NULL) { 669 zio_vdev_io_bypass(dio); 670 zio_execute(dio); 671 } 672 mutex_enter(&vq->vq_lock); 673 674 return (aio); 675 } 676 677 static zio_t * 678 vdev_queue_io_to_issue(vdev_queue_t *vq) 679 { 680 zio_t *zio, *aio; 681 zio_priority_t p; 682 avl_index_t idx; 683 avl_tree_t *tree; 684 zio_t search; 685 686 again: 687 ASSERT(MUTEX_HELD(&vq->vq_lock)); 688 689 p = vdev_queue_class_to_issue(vq); 690 691 if (p == ZIO_PRIORITY_NUM_QUEUEABLE) { 692 /* No eligible queued i/os */ 693 return (NULL); 694 } 695 696 /* 697 * For LBA-ordered queues (async / scrub / initializing), issue the 698 * i/o which follows the most recently issued i/o in LBA (offset) order. 699 * 700 * For FIFO queues (sync), issue the i/o with the lowest timestamp. 701 */ 702 tree = vdev_queue_class_tree(vq, p); 703 search.io_timestamp = 0; 704 search.io_offset = vq->vq_last_offset + 1; 705 VERIFY3P(avl_find(tree, &search, &idx), ==, NULL); 706 zio = avl_nearest(tree, idx, AVL_AFTER); 707 if (zio == NULL) 708 zio = avl_first(tree); 709 ASSERT3U(zio->io_priority, ==, p); 710 711 aio = vdev_queue_aggregate(vq, zio); 712 if (aio != NULL) 713 zio = aio; 714 else 715 vdev_queue_io_remove(vq, zio); 716 717 /* 718 * If the I/O is or was optional and therefore has no data, we need to 719 * simply discard it. We need to drop the vdev queue's lock to avoid a 720 * deadlock that we could encounter since this I/O will complete 721 * immediately. 722 */ 723 if (zio->io_flags & ZIO_FLAG_NODATA) { 724 mutex_exit(&vq->vq_lock); 725 zio_vdev_io_bypass(zio); 726 zio_execute(zio); 727 mutex_enter(&vq->vq_lock); 728 goto again; 729 } 730 731 vdev_queue_pending_add(vq, zio); 732 vq->vq_last_offset = zio->io_offset; 733 734 return (zio); 735 } 736 737 zio_t * 738 vdev_queue_io(zio_t *zio) 739 { 740 vdev_queue_t *vq = &zio->io_vd->vdev_queue; 741 zio_t *nio; 742 743 if (zio->io_flags & ZIO_FLAG_DONT_QUEUE) 744 return (zio); 745 746 /* 747 * Children i/os inherent their parent's priority, which might 748 * not match the child's i/o type. Fix it up here. 749 */ 750 if (zio->io_type == ZIO_TYPE_READ) { 751 if (zio->io_priority != ZIO_PRIORITY_SYNC_READ && 752 zio->io_priority != ZIO_PRIORITY_ASYNC_READ && 753 zio->io_priority != ZIO_PRIORITY_SCRUB && 754 zio->io_priority != ZIO_PRIORITY_REMOVAL && 755 zio->io_priority != ZIO_PRIORITY_INITIALIZING) 756 zio->io_priority = ZIO_PRIORITY_ASYNC_READ; 757 } else { 758 ASSERT(zio->io_type == ZIO_TYPE_WRITE); 759 if (zio->io_priority != ZIO_PRIORITY_SYNC_WRITE && 760 zio->io_priority != ZIO_PRIORITY_ASYNC_WRITE && 761 zio->io_priority != ZIO_PRIORITY_REMOVAL && 762 zio->io_priority != ZIO_PRIORITY_INITIALIZING) 763 zio->io_priority = ZIO_PRIORITY_ASYNC_WRITE; 764 } 765 766 zio->io_flags |= ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE; 767 768 mutex_enter(&vq->vq_lock); 769 zio->io_timestamp = gethrtime(); 770 vdev_queue_io_add(vq, zio); 771 nio = vdev_queue_io_to_issue(vq); 772 mutex_exit(&vq->vq_lock); 773 774 if (nio == NULL) 775 return (NULL); 776 777 if (nio->io_done == vdev_queue_agg_io_done) { 778 zio_nowait(nio); 779 return (NULL); 780 } 781 782 return (nio); 783 } 784 785 void 786 vdev_queue_io_done(zio_t *zio) 787 { 788 vdev_queue_t *vq = &zio->io_vd->vdev_queue; 789 zio_t *nio; 790 791 mutex_enter(&vq->vq_lock); 792 793 vdev_queue_pending_remove(vq, zio); 794 795 vq->vq_io_complete_ts = gethrtime(); 796 797 while ((nio = vdev_queue_io_to_issue(vq)) != NULL) { 798 mutex_exit(&vq->vq_lock); 799 if (nio->io_done == vdev_queue_agg_io_done) { 800 zio_nowait(nio); 801 } else { 802 zio_vdev_io_reissue(nio); 803 zio_execute(nio); 804 } 805 mutex_enter(&vq->vq_lock); 806 } 807 808 mutex_exit(&vq->vq_lock); 809 } 810 811 void 812 vdev_queue_change_io_priority(zio_t *zio, zio_priority_t priority) 813 { 814 vdev_queue_t *vq = &zio->io_vd->vdev_queue; 815 avl_tree_t *tree; 816 817 /* 818 * ZIO_PRIORITY_NOW is used by the vdev cache code and the aggregate zio 819 * code to issue IOs without adding them to the vdev queue. In this 820 * case, the zio is already going to be issued as quickly as possible 821 * and so it doesn't need any reprioitization to help. 822 */ 823 if (zio->io_priority == ZIO_PRIORITY_NOW) 824 return; 825 826 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE); 827 ASSERT3U(priority, <, ZIO_PRIORITY_NUM_QUEUEABLE); 828 829 if (zio->io_type == ZIO_TYPE_READ) { 830 if (priority != ZIO_PRIORITY_SYNC_READ && 831 priority != ZIO_PRIORITY_ASYNC_READ && 832 priority != ZIO_PRIORITY_SCRUB) 833 priority = ZIO_PRIORITY_ASYNC_READ; 834 } else { 835 ASSERT(zio->io_type == ZIO_TYPE_WRITE); 836 if (priority != ZIO_PRIORITY_SYNC_WRITE && 837 priority != ZIO_PRIORITY_ASYNC_WRITE) 838 priority = ZIO_PRIORITY_ASYNC_WRITE; 839 } 840 841 mutex_enter(&vq->vq_lock); 842 843 /* 844 * If the zio is in none of the queues we can simply change 845 * the priority. If the zio is waiting to be submitted we must 846 * remove it from the queue and re-insert it with the new priority. 847 * Otherwise, the zio is currently active and we cannot change its 848 * priority. 849 */ 850 tree = vdev_queue_class_tree(vq, zio->io_priority); 851 if (avl_find(tree, zio, NULL) == zio) { 852 avl_remove(vdev_queue_class_tree(vq, zio->io_priority), zio); 853 zio->io_priority = priority; 854 avl_add(vdev_queue_class_tree(vq, zio->io_priority), zio); 855 } else if (avl_find(&vq->vq_active_tree, zio, NULL) != zio) { 856 zio->io_priority = priority; 857 } 858 859 mutex_exit(&vq->vq_lock); 860 } 861