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