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 = SPA_OLD_MAXBLOCKSIZE; 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 uint64_t maxgap = 0; 523 uint64_t size; 524 boolean_t stretch = B_FALSE; 525 avl_tree_t *t = vdev_queue_type_tree(vq, zio->io_type); 526 enum zio_flag flags = zio->io_flags & ZIO_FLAG_AGG_INHERIT; 527 528 if (zio->io_flags & ZIO_FLAG_DONT_AGGREGATE) 529 return (NULL); 530 531 first = last = zio; 532 533 if (zio->io_type == ZIO_TYPE_READ) 534 maxgap = zfs_vdev_read_gap_limit; 535 536 /* 537 * We can aggregate I/Os that are sufficiently adjacent and of 538 * the same flavor, as expressed by the AGG_INHERIT flags. 539 * The latter requirement is necessary so that certain 540 * attributes of the I/O, such as whether it's a normal I/O 541 * or a scrub/resilver, can be preserved in the aggregate. 542 * We can include optional I/Os, but don't allow them 543 * to begin a range as they add no benefit in that situation. 544 */ 545 546 /* 547 * We keep track of the last non-optional I/O. 548 */ 549 mandatory = (first->io_flags & ZIO_FLAG_OPTIONAL) ? NULL : first; 550 551 /* 552 * Walk backwards through sufficiently contiguous I/Os 553 * recording the last non-optional I/O. 554 */ 555 while ((dio = AVL_PREV(t, first)) != NULL && 556 (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags && 557 IO_SPAN(dio, last) <= zfs_vdev_aggregation_limit && 558 IO_GAP(dio, first) <= maxgap && 559 dio->io_type == zio->io_type) { 560 first = dio; 561 if (mandatory == NULL && !(first->io_flags & ZIO_FLAG_OPTIONAL)) 562 mandatory = first; 563 } 564 565 /* 566 * Skip any initial optional I/Os. 567 */ 568 while ((first->io_flags & ZIO_FLAG_OPTIONAL) && first != last) { 569 first = AVL_NEXT(t, first); 570 ASSERT(first != NULL); 571 } 572 573 /* 574 * Walk forward through sufficiently contiguous I/Os. 575 * The aggregation limit does not apply to optional i/os, so that 576 * we can issue contiguous writes even if they are larger than the 577 * aggregation limit. 578 */ 579 while ((dio = AVL_NEXT(t, last)) != NULL && 580 (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags && 581 (IO_SPAN(first, dio) <= zfs_vdev_aggregation_limit || 582 (dio->io_flags & ZIO_FLAG_OPTIONAL)) && 583 IO_GAP(last, dio) <= maxgap && 584 dio->io_type == zio->io_type) { 585 last = dio; 586 if (!(last->io_flags & ZIO_FLAG_OPTIONAL)) 587 mandatory = last; 588 } 589 590 /* 591 * Now that we've established the range of the I/O aggregation 592 * we must decide what to do with trailing optional I/Os. 593 * For reads, there's nothing to do. While we are unable to 594 * aggregate further, it's possible that a trailing optional 595 * I/O would allow the underlying device to aggregate with 596 * subsequent I/Os. We must therefore determine if the next 597 * non-optional I/O is close enough to make aggregation 598 * worthwhile. 599 */ 600 if (zio->io_type == ZIO_TYPE_WRITE && mandatory != NULL) { 601 zio_t *nio = last; 602 while ((dio = AVL_NEXT(t, nio)) != NULL && 603 IO_GAP(nio, dio) == 0 && 604 IO_GAP(mandatory, dio) <= zfs_vdev_write_gap_limit) { 605 nio = dio; 606 if (!(nio->io_flags & ZIO_FLAG_OPTIONAL)) { 607 stretch = B_TRUE; 608 break; 609 } 610 } 611 } 612 613 if (stretch) { 614 /* 615 * We are going to include an optional io in our aggregated 616 * span, thus closing the write gap. Only mandatory i/os can 617 * start aggregated spans, so make sure that the next i/o 618 * after our span is mandatory. 619 */ 620 dio = AVL_NEXT(t, last); 621 dio->io_flags &= ~ZIO_FLAG_OPTIONAL; 622 } else { 623 /* do not include the optional i/o */ 624 while (last != mandatory && last != first) { 625 ASSERT(last->io_flags & ZIO_FLAG_OPTIONAL); 626 last = AVL_PREV(t, last); 627 ASSERT(last != NULL); 628 } 629 } 630 631 if (first == last) 632 return (NULL); 633 634 size = IO_SPAN(first, last); 635 ASSERT3U(size, <=, SPA_MAXBLOCKSIZE); 636 637 aio = zio_vdev_delegated_io(first->io_vd, first->io_offset, 638 abd_alloc_for_io(size, B_TRUE), size, first->io_type, 639 zio->io_priority, flags | ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE, 640 vdev_queue_agg_io_done, NULL); 641 aio->io_timestamp = first->io_timestamp; 642 643 nio = first; 644 do { 645 dio = nio; 646 nio = AVL_NEXT(t, dio); 647 ASSERT3U(dio->io_type, ==, aio->io_type); 648 649 if (dio->io_flags & ZIO_FLAG_NODATA) { 650 ASSERT3U(dio->io_type, ==, ZIO_TYPE_WRITE); 651 abd_zero_off(aio->io_abd, 652 dio->io_offset - aio->io_offset, dio->io_size); 653 } else if (dio->io_type == ZIO_TYPE_WRITE) { 654 abd_copy_off(aio->io_abd, dio->io_abd, 655 dio->io_offset - aio->io_offset, 0, dio->io_size); 656 } 657 658 zio_add_child(dio, aio); 659 vdev_queue_io_remove(vq, dio); 660 zio_vdev_io_bypass(dio); 661 zio_execute(dio); 662 } while (dio != last); 663 664 return (aio); 665 } 666 667 static zio_t * 668 vdev_queue_io_to_issue(vdev_queue_t *vq) 669 { 670 zio_t *zio, *aio; 671 zio_priority_t p; 672 avl_index_t idx; 673 avl_tree_t *tree; 674 zio_t search; 675 676 again: 677 ASSERT(MUTEX_HELD(&vq->vq_lock)); 678 679 p = vdev_queue_class_to_issue(vq); 680 681 if (p == ZIO_PRIORITY_NUM_QUEUEABLE) { 682 /* No eligible queued i/os */ 683 return (NULL); 684 } 685 686 /* 687 * For LBA-ordered queues (async / scrub / initializing), issue the 688 * i/o which follows the most recently issued i/o in LBA (offset) order. 689 * 690 * For FIFO queues (sync), issue the i/o with the lowest timestamp. 691 */ 692 tree = vdev_queue_class_tree(vq, p); 693 search.io_timestamp = 0; 694 search.io_offset = vq->vq_last_offset + 1; 695 VERIFY3P(avl_find(tree, &search, &idx), ==, NULL); 696 zio = avl_nearest(tree, idx, AVL_AFTER); 697 if (zio == NULL) 698 zio = avl_first(tree); 699 ASSERT3U(zio->io_priority, ==, p); 700 701 aio = vdev_queue_aggregate(vq, zio); 702 if (aio != NULL) 703 zio = aio; 704 else 705 vdev_queue_io_remove(vq, zio); 706 707 /* 708 * If the I/O is or was optional and therefore has no data, we need to 709 * simply discard it. We need to drop the vdev queue's lock to avoid a 710 * deadlock that we could encounter since this I/O will complete 711 * immediately. 712 */ 713 if (zio->io_flags & ZIO_FLAG_NODATA) { 714 mutex_exit(&vq->vq_lock); 715 zio_vdev_io_bypass(zio); 716 zio_execute(zio); 717 mutex_enter(&vq->vq_lock); 718 goto again; 719 } 720 721 vdev_queue_pending_add(vq, zio); 722 vq->vq_last_offset = zio->io_offset; 723 724 return (zio); 725 } 726 727 zio_t * 728 vdev_queue_io(zio_t *zio) 729 { 730 vdev_queue_t *vq = &zio->io_vd->vdev_queue; 731 zio_t *nio; 732 733 if (zio->io_flags & ZIO_FLAG_DONT_QUEUE) 734 return (zio); 735 736 /* 737 * Children i/os inherent their parent's priority, which might 738 * not match the child's i/o type. Fix it up here. 739 */ 740 if (zio->io_type == ZIO_TYPE_READ) { 741 if (zio->io_priority != ZIO_PRIORITY_SYNC_READ && 742 zio->io_priority != ZIO_PRIORITY_ASYNC_READ && 743 zio->io_priority != ZIO_PRIORITY_SCRUB && 744 zio->io_priority != ZIO_PRIORITY_REMOVAL && 745 zio->io_priority != ZIO_PRIORITY_INITIALIZING) 746 zio->io_priority = ZIO_PRIORITY_ASYNC_READ; 747 } else { 748 ASSERT(zio->io_type == ZIO_TYPE_WRITE); 749 if (zio->io_priority != ZIO_PRIORITY_SYNC_WRITE && 750 zio->io_priority != ZIO_PRIORITY_ASYNC_WRITE && 751 zio->io_priority != ZIO_PRIORITY_REMOVAL && 752 zio->io_priority != ZIO_PRIORITY_INITIALIZING) 753 zio->io_priority = ZIO_PRIORITY_ASYNC_WRITE; 754 } 755 756 zio->io_flags |= ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE; 757 758 mutex_enter(&vq->vq_lock); 759 zio->io_timestamp = gethrtime(); 760 vdev_queue_io_add(vq, zio); 761 nio = vdev_queue_io_to_issue(vq); 762 mutex_exit(&vq->vq_lock); 763 764 if (nio == NULL) 765 return (NULL); 766 767 if (nio->io_done == vdev_queue_agg_io_done) { 768 zio_nowait(nio); 769 return (NULL); 770 } 771 772 return (nio); 773 } 774 775 void 776 vdev_queue_io_done(zio_t *zio) 777 { 778 vdev_queue_t *vq = &zio->io_vd->vdev_queue; 779 zio_t *nio; 780 781 mutex_enter(&vq->vq_lock); 782 783 vdev_queue_pending_remove(vq, zio); 784 785 vq->vq_io_complete_ts = gethrtime(); 786 787 while ((nio = vdev_queue_io_to_issue(vq)) != NULL) { 788 mutex_exit(&vq->vq_lock); 789 if (nio->io_done == vdev_queue_agg_io_done) { 790 zio_nowait(nio); 791 } else { 792 zio_vdev_io_reissue(nio); 793 zio_execute(nio); 794 } 795 mutex_enter(&vq->vq_lock); 796 } 797 798 mutex_exit(&vq->vq_lock); 799 } 800