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