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