xref: /titanic_44/usr/src/uts/common/fs/zfs/vdev_queue.c (revision fe319232d24f4ae183730a5a24a09423d8ab4429)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
22a3f829aeSBill Moore  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23fa9e4066Sahrens  * Use is subject to license terms.
24fa9e4066Sahrens  */
25fa9e4066Sahrens 
26283b8460SGeorge.Wilson /*
2773527f44SAlex Reece  * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
28283b8460SGeorge.Wilson  */
29283b8460SGeorge.Wilson 
30fa9e4066Sahrens #include <sys/zfs_context.h>
31fa9e4066Sahrens #include <sys/vdev_impl.h>
32c3a66015SMatthew Ahrens #include <sys/spa_impl.h>
33fa9e4066Sahrens #include <sys/zio.h>
34fa9e4066Sahrens #include <sys/avl.h>
3569962b56SMatthew Ahrens #include <sys/dsl_pool.h>
36fa9e4066Sahrens 
37fa9e4066Sahrens /*
3869962b56SMatthew Ahrens  * ZFS I/O Scheduler
3969962b56SMatthew Ahrens  * ---------------
4069962b56SMatthew Ahrens  *
4169962b56SMatthew Ahrens  * ZFS issues I/O operations to leaf vdevs to satisfy and complete zios.  The
4269962b56SMatthew Ahrens  * I/O scheduler determines when and in what order those operations are
4369962b56SMatthew Ahrens  * issued.  The I/O scheduler divides operations into five I/O classes
4469962b56SMatthew Ahrens  * prioritized in the following order: sync read, sync write, async read,
4569962b56SMatthew Ahrens  * async write, and scrub/resilver.  Each queue defines the minimum and
4669962b56SMatthew Ahrens  * maximum number of concurrent operations that may be issued to the device.
4769962b56SMatthew Ahrens  * In addition, the device has an aggregate maximum. Note that the sum of the
4869962b56SMatthew Ahrens  * per-queue minimums must not exceed the aggregate maximum, and if the
4969962b56SMatthew Ahrens  * aggregate maximum is equal to or greater than the sum of the per-queue
5069962b56SMatthew Ahrens  * maximums, the per-queue minimum has no effect.
5169962b56SMatthew Ahrens  *
5269962b56SMatthew Ahrens  * For many physical devices, throughput increases with the number of
5369962b56SMatthew Ahrens  * concurrent operations, but latency typically suffers. Further, physical
5469962b56SMatthew Ahrens  * devices typically have a limit at which more concurrent operations have no
5569962b56SMatthew Ahrens  * effect on throughput or can actually cause it to decrease.
5669962b56SMatthew Ahrens  *
5769962b56SMatthew Ahrens  * The scheduler selects the next operation to issue by first looking for an
5869962b56SMatthew Ahrens  * I/O class whose minimum has not been satisfied. Once all are satisfied and
5969962b56SMatthew Ahrens  * the aggregate maximum has not been hit, the scheduler looks for classes
6069962b56SMatthew Ahrens  * whose maximum has not been satisfied. Iteration through the I/O classes is
6169962b56SMatthew Ahrens  * done in the order specified above. No further operations are issued if the
6269962b56SMatthew Ahrens  * aggregate maximum number of concurrent operations has been hit or if there
6369962b56SMatthew Ahrens  * are no operations queued for an I/O class that has not hit its maximum.
6469962b56SMatthew Ahrens  * Every time an i/o is queued or an operation completes, the I/O scheduler
6569962b56SMatthew Ahrens  * looks for new operations to issue.
6669962b56SMatthew Ahrens  *
6769962b56SMatthew Ahrens  * All I/O classes have a fixed maximum number of outstanding operations
6869962b56SMatthew Ahrens  * except for the async write class. Asynchronous writes represent the data
6969962b56SMatthew Ahrens  * that is committed to stable storage during the syncing stage for
7069962b56SMatthew Ahrens  * transaction groups (see txg.c). Transaction groups enter the syncing state
7169962b56SMatthew Ahrens  * periodically so the number of queued async writes will quickly burst up and
7269962b56SMatthew Ahrens  * then bleed down to zero. Rather than servicing them as quickly as possible,
7369962b56SMatthew Ahrens  * the I/O scheduler changes the maximum number of active async write i/os
7469962b56SMatthew Ahrens  * according to the amount of dirty data in the pool (see dsl_pool.c). Since
7569962b56SMatthew Ahrens  * both throughput and latency typically increase with the number of
7669962b56SMatthew Ahrens  * concurrent operations issued to physical devices, reducing the burstiness
7769962b56SMatthew Ahrens  * in the number of concurrent operations also stabilizes the response time of
7869962b56SMatthew Ahrens  * operations from other -- and in particular synchronous -- queues. In broad
7969962b56SMatthew Ahrens  * strokes, the I/O scheduler will issue more concurrent operations from the
8069962b56SMatthew Ahrens  * async write queue as there's more dirty data in the pool.
8169962b56SMatthew Ahrens  *
8269962b56SMatthew Ahrens  * Async Writes
8369962b56SMatthew Ahrens  *
8469962b56SMatthew Ahrens  * The number of concurrent operations issued for the async write I/O class
8569962b56SMatthew Ahrens  * follows a piece-wise linear function defined by a few adjustable points.
8669962b56SMatthew Ahrens  *
8769962b56SMatthew Ahrens  *        |                   o---------| <-- zfs_vdev_async_write_max_active
8869962b56SMatthew Ahrens  *   ^    |                  /^         |
8969962b56SMatthew Ahrens  *   |    |                 / |         |
9069962b56SMatthew Ahrens  * active |                /  |         |
9169962b56SMatthew Ahrens  *  I/O   |               /   |         |
9269962b56SMatthew Ahrens  * count  |              /    |         |
9369962b56SMatthew Ahrens  *        |             /     |         |
9469962b56SMatthew Ahrens  *        |------------o      |         | <-- zfs_vdev_async_write_min_active
9569962b56SMatthew Ahrens  *       0|____________^______|_________|
9669962b56SMatthew Ahrens  *        0%           |      |       100% of zfs_dirty_data_max
9769962b56SMatthew Ahrens  *                     |      |
9869962b56SMatthew Ahrens  *                     |      `-- zfs_vdev_async_write_active_max_dirty_percent
9969962b56SMatthew Ahrens  *                     `--------- zfs_vdev_async_write_active_min_dirty_percent
10069962b56SMatthew Ahrens  *
10169962b56SMatthew Ahrens  * Until the amount of dirty data exceeds a minimum percentage of the dirty
10269962b56SMatthew Ahrens  * data allowed in the pool, the I/O scheduler will limit the number of
10369962b56SMatthew Ahrens  * concurrent operations to the minimum. As that threshold is crossed, the
10469962b56SMatthew Ahrens  * number of concurrent operations issued increases linearly to the maximum at
10569962b56SMatthew Ahrens  * the specified maximum percentage of the dirty data allowed in the pool.
10669962b56SMatthew Ahrens  *
10769962b56SMatthew Ahrens  * Ideally, the amount of dirty data on a busy pool will stay in the sloped
10869962b56SMatthew Ahrens  * part of the function between zfs_vdev_async_write_active_min_dirty_percent
10969962b56SMatthew Ahrens  * and zfs_vdev_async_write_active_max_dirty_percent. If it exceeds the
11069962b56SMatthew Ahrens  * maximum percentage, this indicates that the rate of incoming data is
11169962b56SMatthew Ahrens  * greater than the rate that the backend storage can handle. In this case, we
11269962b56SMatthew Ahrens  * must further throttle incoming writes (see dmu_tx_delay() for details).
113614409b5Sahrens  */
114f7170741SWill Andrews 
115f7170741SWill Andrews /*
11669962b56SMatthew Ahrens  * The maximum number of i/os active to each device.  Ideally, this will be >=
11769962b56SMatthew Ahrens  * the sum of each queue's max_active.  It must be at least the sum of each
11869962b56SMatthew Ahrens  * queue's min_active.
119f7170741SWill Andrews  */
12069962b56SMatthew Ahrens uint32_t zfs_vdev_max_active = 1000;
121614409b5Sahrens 
122c55e05cbSMatthew Ahrens /*
12369962b56SMatthew Ahrens  * Per-queue limits on the number of i/os active to each device.  If the
12469962b56SMatthew Ahrens  * sum of the queue's max_active is < zfs_vdev_max_active, then the
12569962b56SMatthew Ahrens  * min_active comes into play.  We will send min_active from each queue,
12669962b56SMatthew Ahrens  * and then select from queues in the order defined by zio_priority_t.
12769962b56SMatthew Ahrens  *
12869962b56SMatthew Ahrens  * In general, smaller max_active's will lead to lower latency of synchronous
12969962b56SMatthew Ahrens  * operations.  Larger max_active's may lead to higher overall throughput,
13069962b56SMatthew Ahrens  * depending on underlying storage.
13169962b56SMatthew Ahrens  *
13269962b56SMatthew Ahrens  * The ratio of the queues' max_actives determines the balance of performance
13369962b56SMatthew Ahrens  * between reads, writes, and scrubs.  E.g., increasing
13469962b56SMatthew Ahrens  * zfs_vdev_scrub_max_active will cause the scrub or resilver to complete
13569962b56SMatthew Ahrens  * more quickly, but reads and writes to have higher latency and lower
13669962b56SMatthew Ahrens  * throughput.
137c55e05cbSMatthew Ahrens  */
13869962b56SMatthew Ahrens uint32_t zfs_vdev_sync_read_min_active = 10;
13969962b56SMatthew Ahrens uint32_t zfs_vdev_sync_read_max_active = 10;
14069962b56SMatthew Ahrens uint32_t zfs_vdev_sync_write_min_active = 10;
14169962b56SMatthew Ahrens uint32_t zfs_vdev_sync_write_max_active = 10;
14269962b56SMatthew Ahrens uint32_t zfs_vdev_async_read_min_active = 1;
14369962b56SMatthew Ahrens uint32_t zfs_vdev_async_read_max_active = 3;
14469962b56SMatthew Ahrens uint32_t zfs_vdev_async_write_min_active = 1;
14569962b56SMatthew Ahrens uint32_t zfs_vdev_async_write_max_active = 10;
14669962b56SMatthew Ahrens uint32_t zfs_vdev_scrub_min_active = 1;
14769962b56SMatthew Ahrens uint32_t zfs_vdev_scrub_max_active = 2;
148614409b5Sahrens 
14969962b56SMatthew Ahrens /*
15069962b56SMatthew Ahrens  * When the pool has less than zfs_vdev_async_write_active_min_dirty_percent
15169962b56SMatthew Ahrens  * dirty data, use zfs_vdev_async_write_min_active.  When it has more than
15269962b56SMatthew Ahrens  * zfs_vdev_async_write_active_max_dirty_percent, use
15369962b56SMatthew Ahrens  * zfs_vdev_async_write_max_active. The value is linearly interpolated
15469962b56SMatthew Ahrens  * between min and max.
15569962b56SMatthew Ahrens  */
15669962b56SMatthew Ahrens int zfs_vdev_async_write_active_min_dirty_percent = 30;
15769962b56SMatthew Ahrens int zfs_vdev_async_write_active_max_dirty_percent = 60;
158614409b5Sahrens 
159614409b5Sahrens /*
160f94275ceSAdam Leventhal  * To reduce IOPs, we aggregate small adjacent I/Os into one large I/O.
161f94275ceSAdam Leventhal  * For read I/Os, we also aggregate across small adjacency gaps; for writes
162f94275ceSAdam Leventhal  * we include spans of optional I/Os to aid aggregation at the disk even when
163f94275ceSAdam Leventhal  * they aren't able to help us aggregate at this level.
164614409b5Sahrens  */
165b5152584SMatthew Ahrens int zfs_vdev_aggregation_limit = SPA_OLD_MAXBLOCKSIZE;
1666f708f7cSJeff Bonwick int zfs_vdev_read_gap_limit = 32 << 10;
167f94275ceSAdam Leventhal int zfs_vdev_write_gap_limit = 4 << 10;
168614409b5Sahrens 
169fa9e4066Sahrens int
vdev_queue_offset_compare(const void * x1,const void * x2)170fa9e4066Sahrens vdev_queue_offset_compare(const void *x1, const void *x2)
171fa9e4066Sahrens {
172fa9e4066Sahrens 	const zio_t *z1 = x1;
173fa9e4066Sahrens 	const zio_t *z2 = x2;
174fa9e4066Sahrens 
175fa9e4066Sahrens 	if (z1->io_offset < z2->io_offset)
176fa9e4066Sahrens 		return (-1);
177fa9e4066Sahrens 	if (z1->io_offset > z2->io_offset)
178fa9e4066Sahrens 		return (1);
179fa9e4066Sahrens 
180fa9e4066Sahrens 	if (z1 < z2)
181fa9e4066Sahrens 		return (-1);
182fa9e4066Sahrens 	if (z1 > z2)
183fa9e4066Sahrens 		return (1);
184fa9e4066Sahrens 
185fa9e4066Sahrens 	return (0);
186fa9e4066Sahrens }
187fa9e4066Sahrens 
188*fe319232SJustin T. Gibbs static inline avl_tree_t *
vdev_queue_class_tree(vdev_queue_t * vq,zio_priority_t p)189*fe319232SJustin T. Gibbs vdev_queue_class_tree(vdev_queue_t *vq, zio_priority_t p)
190*fe319232SJustin T. Gibbs {
191*fe319232SJustin T. Gibbs 	return (&vq->vq_class[p].vqc_queued_tree);
192*fe319232SJustin T. Gibbs }
193*fe319232SJustin T. Gibbs 
194*fe319232SJustin T. Gibbs static inline avl_tree_t *
vdev_queue_type_tree(vdev_queue_t * vq,zio_type_t t)195*fe319232SJustin T. Gibbs vdev_queue_type_tree(vdev_queue_t *vq, zio_type_t t)
196*fe319232SJustin T. Gibbs {
197*fe319232SJustin T. Gibbs 	ASSERT(t == ZIO_TYPE_READ || t == ZIO_TYPE_WRITE);
198*fe319232SJustin T. Gibbs 	if (t == ZIO_TYPE_READ)
199*fe319232SJustin T. Gibbs 		return (&vq->vq_read_offset_tree);
200*fe319232SJustin T. Gibbs 	else
201*fe319232SJustin T. Gibbs 		return (&vq->vq_write_offset_tree);
202*fe319232SJustin T. Gibbs }
203*fe319232SJustin T. Gibbs 
20469962b56SMatthew Ahrens int
vdev_queue_timestamp_compare(const void * x1,const void * x2)20569962b56SMatthew Ahrens vdev_queue_timestamp_compare(const void *x1, const void *x2)
20669962b56SMatthew Ahrens {
20769962b56SMatthew Ahrens 	const zio_t *z1 = x1;
20869962b56SMatthew Ahrens 	const zio_t *z2 = x2;
20969962b56SMatthew Ahrens 
21069962b56SMatthew Ahrens 	if (z1->io_timestamp < z2->io_timestamp)
21169962b56SMatthew Ahrens 		return (-1);
21269962b56SMatthew Ahrens 	if (z1->io_timestamp > z2->io_timestamp)
21369962b56SMatthew Ahrens 		return (1);
21469962b56SMatthew Ahrens 
21569962b56SMatthew Ahrens 	if (z1 < z2)
21669962b56SMatthew Ahrens 		return (-1);
21769962b56SMatthew Ahrens 	if (z1 > z2)
21869962b56SMatthew Ahrens 		return (1);
21969962b56SMatthew Ahrens 
22069962b56SMatthew Ahrens 	return (0);
22169962b56SMatthew Ahrens }
22269962b56SMatthew Ahrens 
223fa9e4066Sahrens void
vdev_queue_init(vdev_t * vd)224fa9e4066Sahrens vdev_queue_init(vdev_t *vd)
225fa9e4066Sahrens {
226fa9e4066Sahrens 	vdev_queue_t *vq = &vd->vdev_queue;
227fa9e4066Sahrens 
228fa9e4066Sahrens 	mutex_init(&vq->vq_lock, NULL, MUTEX_DEFAULT, NULL);
22969962b56SMatthew Ahrens 	vq->vq_vdev = vd;
230fa9e4066Sahrens 
23169962b56SMatthew Ahrens 	avl_create(&vq->vq_active_tree, vdev_queue_offset_compare,
23269962b56SMatthew Ahrens 	    sizeof (zio_t), offsetof(struct zio, io_queue_node));
233*fe319232SJustin T. Gibbs 	avl_create(vdev_queue_type_tree(vq, ZIO_TYPE_READ),
234*fe319232SJustin T. Gibbs 	    vdev_queue_offset_compare, sizeof (zio_t),
235*fe319232SJustin T. Gibbs 	    offsetof(struct zio, io_offset_node));
236*fe319232SJustin T. Gibbs 	avl_create(vdev_queue_type_tree(vq, ZIO_TYPE_WRITE),
237*fe319232SJustin T. Gibbs 	    vdev_queue_offset_compare, sizeof (zio_t),
238*fe319232SJustin T. Gibbs 	    offsetof(struct zio, io_offset_node));
239fa9e4066Sahrens 
24069962b56SMatthew Ahrens 	for (zio_priority_t p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
241*fe319232SJustin T. Gibbs 		int (*compfn) (const void *, const void *);
242*fe319232SJustin T. Gibbs 
24369962b56SMatthew Ahrens 		/*
244*fe319232SJustin T. Gibbs 		 * The synchronous i/o queues are dispatched in FIFO rather
245*fe319232SJustin T. Gibbs 		 * than LBA order.  This provides more consistent latency for
246*fe319232SJustin T. Gibbs 		 * these i/os.
24769962b56SMatthew Ahrens 		 */
248*fe319232SJustin T. Gibbs 		if (p == ZIO_PRIORITY_SYNC_READ || p == ZIO_PRIORITY_SYNC_WRITE)
249*fe319232SJustin T. Gibbs 			compfn = vdev_queue_timestamp_compare;
250*fe319232SJustin T. Gibbs 		else
251*fe319232SJustin T. Gibbs 			compfn = vdev_queue_offset_compare;
252*fe319232SJustin T. Gibbs 
253*fe319232SJustin T. Gibbs 		avl_create(vdev_queue_class_tree(vq, p), compfn,
25469962b56SMatthew Ahrens 		    sizeof (zio_t), offsetof(struct zio, io_queue_node));
25569962b56SMatthew Ahrens 	}
256fa9e4066Sahrens }
257fa9e4066Sahrens 
258fa9e4066Sahrens void
vdev_queue_fini(vdev_t * vd)259fa9e4066Sahrens vdev_queue_fini(vdev_t *vd)
260fa9e4066Sahrens {
261fa9e4066Sahrens 	vdev_queue_t *vq = &vd->vdev_queue;
262fa9e4066Sahrens 
26369962b56SMatthew Ahrens 	for (zio_priority_t p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++)
264*fe319232SJustin T. Gibbs 		avl_destroy(vdev_queue_class_tree(vq, p));
26569962b56SMatthew Ahrens 	avl_destroy(&vq->vq_active_tree);
266*fe319232SJustin T. Gibbs 	avl_destroy(vdev_queue_type_tree(vq, ZIO_TYPE_READ));
267*fe319232SJustin T. Gibbs 	avl_destroy(vdev_queue_type_tree(vq, ZIO_TYPE_WRITE));
268fa9e4066Sahrens 
269fa9e4066Sahrens 	mutex_destroy(&vq->vq_lock);
270fa9e4066Sahrens }
271fa9e4066Sahrens 
272fa9e4066Sahrens static void
vdev_queue_io_add(vdev_queue_t * vq,zio_t * zio)273ea8dc4b6Seschrock vdev_queue_io_add(vdev_queue_t *vq, zio_t *zio)
274ea8dc4b6Seschrock {
275c3a66015SMatthew Ahrens 	spa_t *spa = zio->io_spa;
27669962b56SMatthew Ahrens 	ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
277*fe319232SJustin T. Gibbs 	avl_add(vdev_queue_class_tree(vq, zio->io_priority), zio);
278*fe319232SJustin T. Gibbs 	avl_add(vdev_queue_type_tree(vq, zio->io_type), zio);
279c3a66015SMatthew Ahrens 
280c3a66015SMatthew Ahrens 	mutex_enter(&spa->spa_iokstat_lock);
28169962b56SMatthew Ahrens 	spa->spa_queue_stats[zio->io_priority].spa_queued++;
28269962b56SMatthew Ahrens 	if (spa->spa_iokstat != NULL)
283c3a66015SMatthew Ahrens 		kstat_waitq_enter(spa->spa_iokstat->ks_data);
284c3a66015SMatthew Ahrens 	mutex_exit(&spa->spa_iokstat_lock);
285c3a66015SMatthew Ahrens }
286ea8dc4b6Seschrock 
287ea8dc4b6Seschrock static void
vdev_queue_io_remove(vdev_queue_t * vq,zio_t * zio)288ea8dc4b6Seschrock vdev_queue_io_remove(vdev_queue_t *vq, zio_t *zio)
289ea8dc4b6Seschrock {
290c3a66015SMatthew Ahrens 	spa_t *spa = zio->io_spa;
29169962b56SMatthew Ahrens 	ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
292*fe319232SJustin T. Gibbs 	avl_remove(vdev_queue_class_tree(vq, zio->io_priority), zio);
293*fe319232SJustin T. Gibbs 	avl_remove(vdev_queue_type_tree(vq, zio->io_type), zio);
294c3a66015SMatthew Ahrens 
295c3a66015SMatthew Ahrens 	mutex_enter(&spa->spa_iokstat_lock);
29669962b56SMatthew Ahrens 	ASSERT3U(spa->spa_queue_stats[zio->io_priority].spa_queued, >, 0);
29769962b56SMatthew Ahrens 	spa->spa_queue_stats[zio->io_priority].spa_queued--;
29869962b56SMatthew Ahrens 	if (spa->spa_iokstat != NULL)
299c3a66015SMatthew Ahrens 		kstat_waitq_exit(spa->spa_iokstat->ks_data);
300c3a66015SMatthew Ahrens 	mutex_exit(&spa->spa_iokstat_lock);
301c3a66015SMatthew Ahrens }
302c3a66015SMatthew Ahrens 
303c3a66015SMatthew Ahrens static void
vdev_queue_pending_add(vdev_queue_t * vq,zio_t * zio)304c3a66015SMatthew Ahrens vdev_queue_pending_add(vdev_queue_t *vq, zio_t *zio)
305c3a66015SMatthew Ahrens {
306c3a66015SMatthew Ahrens 	spa_t *spa = zio->io_spa;
30769962b56SMatthew Ahrens 	ASSERT(MUTEX_HELD(&vq->vq_lock));
30869962b56SMatthew Ahrens 	ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
30969962b56SMatthew Ahrens 	vq->vq_class[zio->io_priority].vqc_active++;
31069962b56SMatthew Ahrens 	avl_add(&vq->vq_active_tree, zio);
31169962b56SMatthew Ahrens 
312c3a66015SMatthew Ahrens 	mutex_enter(&spa->spa_iokstat_lock);
31369962b56SMatthew Ahrens 	spa->spa_queue_stats[zio->io_priority].spa_active++;
31469962b56SMatthew Ahrens 	if (spa->spa_iokstat != NULL)
315c3a66015SMatthew Ahrens 		kstat_runq_enter(spa->spa_iokstat->ks_data);
316c3a66015SMatthew Ahrens 	mutex_exit(&spa->spa_iokstat_lock);
317c3a66015SMatthew Ahrens }
318c3a66015SMatthew Ahrens 
319c3a66015SMatthew Ahrens static void
vdev_queue_pending_remove(vdev_queue_t * vq,zio_t * zio)320c3a66015SMatthew Ahrens vdev_queue_pending_remove(vdev_queue_t *vq, zio_t *zio)
321c3a66015SMatthew Ahrens {
322c3a66015SMatthew Ahrens 	spa_t *spa = zio->io_spa;
32369962b56SMatthew Ahrens 	ASSERT(MUTEX_HELD(&vq->vq_lock));
32469962b56SMatthew Ahrens 	ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
32569962b56SMatthew Ahrens 	vq->vq_class[zio->io_priority].vqc_active--;
32669962b56SMatthew Ahrens 	avl_remove(&vq->vq_active_tree, zio);
32769962b56SMatthew Ahrens 
32869962b56SMatthew Ahrens 	mutex_enter(&spa->spa_iokstat_lock);
32969962b56SMatthew Ahrens 	ASSERT3U(spa->spa_queue_stats[zio->io_priority].spa_active, >, 0);
33069962b56SMatthew Ahrens 	spa->spa_queue_stats[zio->io_priority].spa_active--;
331c3a66015SMatthew Ahrens 	if (spa->spa_iokstat != NULL) {
332c3a66015SMatthew Ahrens 		kstat_io_t *ksio = spa->spa_iokstat->ks_data;
333c3a66015SMatthew Ahrens 
334c3a66015SMatthew Ahrens 		kstat_runq_exit(spa->spa_iokstat->ks_data);
335c3a66015SMatthew Ahrens 		if (zio->io_type == ZIO_TYPE_READ) {
336c3a66015SMatthew Ahrens 			ksio->reads++;
337c3a66015SMatthew Ahrens 			ksio->nread += zio->io_size;
338c3a66015SMatthew Ahrens 		} else if (zio->io_type == ZIO_TYPE_WRITE) {
339c3a66015SMatthew Ahrens 			ksio->writes++;
340c3a66015SMatthew Ahrens 			ksio->nwritten += zio->io_size;
341c3a66015SMatthew Ahrens 		}
342c3a66015SMatthew Ahrens 	}
34369962b56SMatthew Ahrens 	mutex_exit(&spa->spa_iokstat_lock);
344ea8dc4b6Seschrock }
345ea8dc4b6Seschrock 
346ea8dc4b6Seschrock static void
vdev_queue_agg_io_done(zio_t * aio)347fa9e4066Sahrens vdev_queue_agg_io_done(zio_t *aio)
348fa9e4066Sahrens {
34969962b56SMatthew Ahrens 	if (aio->io_type == ZIO_TYPE_READ) {
350a3f829aeSBill Moore 		zio_t *pio;
35169962b56SMatthew Ahrens 		while ((pio = zio_walk_parents(aio)) != NULL) {
352a3f829aeSBill Moore 			bcopy((char *)aio->io_data + (pio->io_offset -
353a3f829aeSBill Moore 			    aio->io_offset), pio->io_data, pio->io_size);
35469962b56SMatthew Ahrens 		}
35569962b56SMatthew Ahrens 	}
356fa9e4066Sahrens 
357fa9e4066Sahrens 	zio_buf_free(aio->io_data, aio->io_size);
358fa9e4066Sahrens }
359fa9e4066Sahrens 
36069962b56SMatthew Ahrens static int
vdev_queue_class_min_active(zio_priority_t p)36169962b56SMatthew Ahrens vdev_queue_class_min_active(zio_priority_t p)
36269962b56SMatthew Ahrens {
36369962b56SMatthew Ahrens 	switch (p) {
36469962b56SMatthew Ahrens 	case ZIO_PRIORITY_SYNC_READ:
36569962b56SMatthew Ahrens 		return (zfs_vdev_sync_read_min_active);
36669962b56SMatthew Ahrens 	case ZIO_PRIORITY_SYNC_WRITE:
36769962b56SMatthew Ahrens 		return (zfs_vdev_sync_write_min_active);
36869962b56SMatthew Ahrens 	case ZIO_PRIORITY_ASYNC_READ:
36969962b56SMatthew Ahrens 		return (zfs_vdev_async_read_min_active);
37069962b56SMatthew Ahrens 	case ZIO_PRIORITY_ASYNC_WRITE:
37169962b56SMatthew Ahrens 		return (zfs_vdev_async_write_min_active);
37269962b56SMatthew Ahrens 	case ZIO_PRIORITY_SCRUB:
37369962b56SMatthew Ahrens 		return (zfs_vdev_scrub_min_active);
37469962b56SMatthew Ahrens 	default:
37569962b56SMatthew Ahrens 		panic("invalid priority %u", p);
37669962b56SMatthew Ahrens 		return (0);
37769962b56SMatthew Ahrens 	}
37869962b56SMatthew Ahrens }
37969962b56SMatthew Ahrens 
38069962b56SMatthew Ahrens static int
vdev_queue_max_async_writes(spa_t * spa)38173527f44SAlex Reece vdev_queue_max_async_writes(spa_t *spa)
38269962b56SMatthew Ahrens {
38369962b56SMatthew Ahrens 	int writes;
38473527f44SAlex Reece 	uint64_t dirty = spa->spa_dsl_pool->dp_dirty_total;
38569962b56SMatthew Ahrens 	uint64_t min_bytes = zfs_dirty_data_max *
38669962b56SMatthew Ahrens 	    zfs_vdev_async_write_active_min_dirty_percent / 100;
38769962b56SMatthew Ahrens 	uint64_t max_bytes = zfs_dirty_data_max *
38869962b56SMatthew Ahrens 	    zfs_vdev_async_write_active_max_dirty_percent / 100;
38969962b56SMatthew Ahrens 
39073527f44SAlex Reece 	/*
39173527f44SAlex Reece 	 * Sync tasks correspond to interactive user actions. To reduce the
39273527f44SAlex Reece 	 * execution time of those actions we push data out as fast as possible.
39373527f44SAlex Reece 	 */
39473527f44SAlex Reece 	if (spa_has_pending_synctask(spa)) {
39573527f44SAlex Reece 		return (zfs_vdev_async_write_max_active);
39673527f44SAlex Reece 	}
39773527f44SAlex Reece 
39869962b56SMatthew Ahrens 	if (dirty < min_bytes)
39969962b56SMatthew Ahrens 		return (zfs_vdev_async_write_min_active);
40069962b56SMatthew Ahrens 	if (dirty > max_bytes)
40169962b56SMatthew Ahrens 		return (zfs_vdev_async_write_max_active);
40269962b56SMatthew Ahrens 
40369962b56SMatthew Ahrens 	/*
40469962b56SMatthew Ahrens 	 * linear interpolation:
40569962b56SMatthew Ahrens 	 * slope = (max_writes - min_writes) / (max_bytes - min_bytes)
40669962b56SMatthew Ahrens 	 * move right by min_bytes
40769962b56SMatthew Ahrens 	 * move up by min_writes
40869962b56SMatthew Ahrens 	 */
40969962b56SMatthew Ahrens 	writes = (dirty - min_bytes) *
41069962b56SMatthew Ahrens 	    (zfs_vdev_async_write_max_active -
41169962b56SMatthew Ahrens 	    zfs_vdev_async_write_min_active) /
41269962b56SMatthew Ahrens 	    (max_bytes - min_bytes) +
41369962b56SMatthew Ahrens 	    zfs_vdev_async_write_min_active;
41469962b56SMatthew Ahrens 	ASSERT3U(writes, >=, zfs_vdev_async_write_min_active);
41569962b56SMatthew Ahrens 	ASSERT3U(writes, <=, zfs_vdev_async_write_max_active);
41669962b56SMatthew Ahrens 	return (writes);
41769962b56SMatthew Ahrens }
41869962b56SMatthew Ahrens 
41969962b56SMatthew Ahrens static int
vdev_queue_class_max_active(spa_t * spa,zio_priority_t p)42069962b56SMatthew Ahrens vdev_queue_class_max_active(spa_t *spa, zio_priority_t p)
42169962b56SMatthew Ahrens {
42269962b56SMatthew Ahrens 	switch (p) {
42369962b56SMatthew Ahrens 	case ZIO_PRIORITY_SYNC_READ:
42469962b56SMatthew Ahrens 		return (zfs_vdev_sync_read_max_active);
42569962b56SMatthew Ahrens 	case ZIO_PRIORITY_SYNC_WRITE:
42669962b56SMatthew Ahrens 		return (zfs_vdev_sync_write_max_active);
42769962b56SMatthew Ahrens 	case ZIO_PRIORITY_ASYNC_READ:
42869962b56SMatthew Ahrens 		return (zfs_vdev_async_read_max_active);
42969962b56SMatthew Ahrens 	case ZIO_PRIORITY_ASYNC_WRITE:
43073527f44SAlex Reece 		return (vdev_queue_max_async_writes(spa));
43169962b56SMatthew Ahrens 	case ZIO_PRIORITY_SCRUB:
43269962b56SMatthew Ahrens 		return (zfs_vdev_scrub_max_active);
43369962b56SMatthew Ahrens 	default:
43469962b56SMatthew Ahrens 		panic("invalid priority %u", p);
43569962b56SMatthew Ahrens 		return (0);
43669962b56SMatthew Ahrens 	}
43769962b56SMatthew Ahrens }
43869962b56SMatthew Ahrens 
43969962b56SMatthew Ahrens /*
44069962b56SMatthew Ahrens  * Return the i/o class to issue from, or ZIO_PRIORITY_MAX_QUEUEABLE if
44169962b56SMatthew Ahrens  * there is no eligible class.
44269962b56SMatthew Ahrens  */
44369962b56SMatthew Ahrens static zio_priority_t
vdev_queue_class_to_issue(vdev_queue_t * vq)44469962b56SMatthew Ahrens vdev_queue_class_to_issue(vdev_queue_t *vq)
44569962b56SMatthew Ahrens {
44669962b56SMatthew Ahrens 	spa_t *spa = vq->vq_vdev->vdev_spa;
44769962b56SMatthew Ahrens 	zio_priority_t p;
44869962b56SMatthew Ahrens 
44969962b56SMatthew Ahrens 	if (avl_numnodes(&vq->vq_active_tree) >= zfs_vdev_max_active)
45069962b56SMatthew Ahrens 		return (ZIO_PRIORITY_NUM_QUEUEABLE);
45169962b56SMatthew Ahrens 
45269962b56SMatthew Ahrens 	/* find a queue that has not reached its minimum # outstanding i/os */
45369962b56SMatthew Ahrens 	for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
454*fe319232SJustin T. Gibbs 		if (avl_numnodes(vdev_queue_class_tree(vq, p)) > 0 &&
45569962b56SMatthew Ahrens 		    vq->vq_class[p].vqc_active <
45669962b56SMatthew Ahrens 		    vdev_queue_class_min_active(p))
45769962b56SMatthew Ahrens 			return (p);
45869962b56SMatthew Ahrens 	}
45969962b56SMatthew Ahrens 
46069962b56SMatthew Ahrens 	/*
46169962b56SMatthew Ahrens 	 * If we haven't found a queue, look for one that hasn't reached its
46269962b56SMatthew Ahrens 	 * maximum # outstanding i/os.
46369962b56SMatthew Ahrens 	 */
46469962b56SMatthew Ahrens 	for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
465*fe319232SJustin T. Gibbs 		if (avl_numnodes(vdev_queue_class_tree(vq, p)) > 0 &&
46669962b56SMatthew Ahrens 		    vq->vq_class[p].vqc_active <
46769962b56SMatthew Ahrens 		    vdev_queue_class_max_active(spa, p))
46869962b56SMatthew Ahrens 			return (p);
46969962b56SMatthew Ahrens 	}
47069962b56SMatthew Ahrens 
47169962b56SMatthew Ahrens 	/* No eligible queued i/os */
47269962b56SMatthew Ahrens 	return (ZIO_PRIORITY_NUM_QUEUEABLE);
47369962b56SMatthew Ahrens }
47469962b56SMatthew Ahrens 
4756f708f7cSJeff Bonwick /*
4766f708f7cSJeff Bonwick  * Compute the range spanned by two i/os, which is the endpoint of the last
4776f708f7cSJeff Bonwick  * (lio->io_offset + lio->io_size) minus start of the first (fio->io_offset).
4786f708f7cSJeff Bonwick  * Conveniently, the gap between fio and lio is given by -IO_SPAN(lio, fio);
4796f708f7cSJeff Bonwick  * thus fio and lio are adjacent if and only if IO_SPAN(lio, fio) == 0.
4806f708f7cSJeff Bonwick  */
4816f708f7cSJeff Bonwick #define	IO_SPAN(fio, lio) ((lio)->io_offset + (lio)->io_size - (fio)->io_offset)
4826f708f7cSJeff Bonwick #define	IO_GAP(fio, lio) (-IO_SPAN(lio, fio))
483fa9e4066Sahrens 
484fa9e4066Sahrens static zio_t *
vdev_queue_aggregate(vdev_queue_t * vq,zio_t * zio)48569962b56SMatthew Ahrens vdev_queue_aggregate(vdev_queue_t *vq, zio_t *zio)
486fa9e4066Sahrens {
48769962b56SMatthew Ahrens 	zio_t *first, *last, *aio, *dio, *mandatory, *nio;
48869962b56SMatthew Ahrens 	uint64_t maxgap = 0;
48969962b56SMatthew Ahrens 	uint64_t size;
49069962b56SMatthew Ahrens 	boolean_t stretch = B_FALSE;
491*fe319232SJustin T. Gibbs 	avl_tree_t *t = vdev_queue_type_tree(vq, zio->io_type);
49269962b56SMatthew Ahrens 	enum zio_flag flags = zio->io_flags & ZIO_FLAG_AGG_INHERIT;
493fa9e4066Sahrens 
49469962b56SMatthew Ahrens 	if (zio->io_flags & ZIO_FLAG_DONT_AGGREGATE)
495fa9e4066Sahrens 		return (NULL);
496fa9e4066Sahrens 
49769962b56SMatthew Ahrens 	first = last = zio;
498fa9e4066Sahrens 
49969962b56SMatthew Ahrens 	if (zio->io_type == ZIO_TYPE_READ)
50069962b56SMatthew Ahrens 		maxgap = zfs_vdev_read_gap_limit;
50169962b56SMatthew Ahrens 
5028ad4d6ddSJeff Bonwick 	/*
503f94275ceSAdam Leventhal 	 * We can aggregate I/Os that are sufficiently adjacent and of
504f94275ceSAdam Leventhal 	 * the same flavor, as expressed by the AGG_INHERIT flags.
505f94275ceSAdam Leventhal 	 * The latter requirement is necessary so that certain
506f94275ceSAdam Leventhal 	 * attributes of the I/O, such as whether it's a normal I/O
507f94275ceSAdam Leventhal 	 * or a scrub/resilver, can be preserved in the aggregate.
508f94275ceSAdam Leventhal 	 * We can include optional I/Os, but don't allow them
509f94275ceSAdam Leventhal 	 * to begin a range as they add no benefit in that situation.
510f94275ceSAdam Leventhal 	 */
511f94275ceSAdam Leventhal 
512f94275ceSAdam Leventhal 	/*
513f94275ceSAdam Leventhal 	 * We keep track of the last non-optional I/O.
514f94275ceSAdam Leventhal 	 */
51569962b56SMatthew Ahrens 	mandatory = (first->io_flags & ZIO_FLAG_OPTIONAL) ? NULL : first;
516f94275ceSAdam Leventhal 
517f94275ceSAdam Leventhal 	/*
518f94275ceSAdam Leventhal 	 * Walk backwards through sufficiently contiguous I/Os
519f94275ceSAdam Leventhal 	 * recording the last non-option I/O.
5208ad4d6ddSJeff Bonwick 	 */
52169962b56SMatthew Ahrens 	while ((dio = AVL_PREV(t, first)) != NULL &&
5228ad4d6ddSJeff Bonwick 	    (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags &&
52369962b56SMatthew Ahrens 	    IO_SPAN(dio, last) <= zfs_vdev_aggregation_limit &&
52469962b56SMatthew Ahrens 	    IO_GAP(dio, first) <= maxgap) {
52569962b56SMatthew Ahrens 		first = dio;
52669962b56SMatthew Ahrens 		if (mandatory == NULL && !(first->io_flags & ZIO_FLAG_OPTIONAL))
52769962b56SMatthew Ahrens 			mandatory = first;
528f94275ceSAdam Leventhal 	}
5296f708f7cSJeff Bonwick 
530f94275ceSAdam Leventhal 	/*
531f94275ceSAdam Leventhal 	 * Skip any initial optional I/Os.
532f94275ceSAdam Leventhal 	 */
53369962b56SMatthew Ahrens 	while ((first->io_flags & ZIO_FLAG_OPTIONAL) && first != last) {
53469962b56SMatthew Ahrens 		first = AVL_NEXT(t, first);
53569962b56SMatthew Ahrens 		ASSERT(first != NULL);
536f94275ceSAdam Leventhal 	}
537f94275ceSAdam Leventhal 
538f94275ceSAdam Leventhal 	/*
539f94275ceSAdam Leventhal 	 * Walk forward through sufficiently contiguous I/Os.
540f94275ceSAdam Leventhal 	 */
54169962b56SMatthew Ahrens 	while ((dio = AVL_NEXT(t, last)) != NULL &&
5428ad4d6ddSJeff Bonwick 	    (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags &&
54369962b56SMatthew Ahrens 	    IO_SPAN(first, dio) <= zfs_vdev_aggregation_limit &&
54469962b56SMatthew Ahrens 	    IO_GAP(last, dio) <= maxgap) {
54569962b56SMatthew Ahrens 		last = dio;
54669962b56SMatthew Ahrens 		if (!(last->io_flags & ZIO_FLAG_OPTIONAL))
54769962b56SMatthew Ahrens 			mandatory = last;
548f94275ceSAdam Leventhal 	}
549f94275ceSAdam Leventhal 
550f94275ceSAdam Leventhal 	/*
551f94275ceSAdam Leventhal 	 * Now that we've established the range of the I/O aggregation
552f94275ceSAdam Leventhal 	 * we must decide what to do with trailing optional I/Os.
553f94275ceSAdam Leventhal 	 * For reads, there's nothing to do. While we are unable to
554f94275ceSAdam Leventhal 	 * aggregate further, it's possible that a trailing optional
555f94275ceSAdam Leventhal 	 * I/O would allow the underlying device to aggregate with
556f94275ceSAdam Leventhal 	 * subsequent I/Os. We must therefore determine if the next
557f94275ceSAdam Leventhal 	 * non-optional I/O is close enough to make aggregation
558f94275ceSAdam Leventhal 	 * worthwhile.
559f94275ceSAdam Leventhal 	 */
56069962b56SMatthew Ahrens 	if (zio->io_type == ZIO_TYPE_WRITE && mandatory != NULL) {
56169962b56SMatthew Ahrens 		zio_t *nio = last;
562f94275ceSAdam Leventhal 		while ((dio = AVL_NEXT(t, nio)) != NULL &&
563f94275ceSAdam Leventhal 		    IO_GAP(nio, dio) == 0 &&
56469962b56SMatthew Ahrens 		    IO_GAP(mandatory, dio) <= zfs_vdev_write_gap_limit) {
565f94275ceSAdam Leventhal 			nio = dio;
566f94275ceSAdam Leventhal 			if (!(nio->io_flags & ZIO_FLAG_OPTIONAL)) {
567f94275ceSAdam Leventhal 				stretch = B_TRUE;
568f94275ceSAdam Leventhal 				break;
569f94275ceSAdam Leventhal 			}
570f94275ceSAdam Leventhal 		}
571f94275ceSAdam Leventhal 	}
572f94275ceSAdam Leventhal 
573f94275ceSAdam Leventhal 	if (stretch) {
574f94275ceSAdam Leventhal 		/* This may be a no-op. */
57569962b56SMatthew Ahrens 		dio = AVL_NEXT(t, last);
576f94275ceSAdam Leventhal 		dio->io_flags &= ~ZIO_FLAG_OPTIONAL;
577f94275ceSAdam Leventhal 	} else {
57869962b56SMatthew Ahrens 		while (last != mandatory && last != first) {
57969962b56SMatthew Ahrens 			ASSERT(last->io_flags & ZIO_FLAG_OPTIONAL);
58069962b56SMatthew Ahrens 			last = AVL_PREV(t, last);
58169962b56SMatthew Ahrens 			ASSERT(last != NULL);
582f94275ceSAdam Leventhal 		}
5838ad4d6ddSJeff Bonwick 	}
584fa9e4066Sahrens 
58569962b56SMatthew Ahrens 	if (first == last)
58669962b56SMatthew Ahrens 		return (NULL);
587fa9e4066Sahrens 
58869962b56SMatthew Ahrens 	size = IO_SPAN(first, last);
58969962b56SMatthew Ahrens 	ASSERT3U(size, <=, zfs_vdev_aggregation_limit);
59069962b56SMatthew Ahrens 
59169962b56SMatthew Ahrens 	aio = zio_vdev_delegated_io(first->io_vd, first->io_offset,
59269962b56SMatthew Ahrens 	    zio_buf_alloc(size), size, first->io_type, zio->io_priority,
5938ad4d6ddSJeff Bonwick 	    flags | ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE,
594fa9e4066Sahrens 	    vdev_queue_agg_io_done, NULL);
59569962b56SMatthew Ahrens 	aio->io_timestamp = first->io_timestamp;
596fa9e4066Sahrens 
59769962b56SMatthew Ahrens 	nio = first;
5986f708f7cSJeff Bonwick 	do {
5996f708f7cSJeff Bonwick 		dio = nio;
6006f708f7cSJeff Bonwick 		nio = AVL_NEXT(t, dio);
60169962b56SMatthew Ahrens 		ASSERT3U(dio->io_type, ==, aio->io_type);
602a3f829aeSBill Moore 
603f94275ceSAdam Leventhal 		if (dio->io_flags & ZIO_FLAG_NODATA) {
60469962b56SMatthew Ahrens 			ASSERT3U(dio->io_type, ==, ZIO_TYPE_WRITE);
605f94275ceSAdam Leventhal 			bzero((char *)aio->io_data + (dio->io_offset -
606f94275ceSAdam Leventhal 			    aio->io_offset), dio->io_size);
607f94275ceSAdam Leventhal 		} else if (dio->io_type == ZIO_TYPE_WRITE) {
608a3f829aeSBill Moore 			bcopy(dio->io_data, (char *)aio->io_data +
609a3f829aeSBill Moore 			    (dio->io_offset - aio->io_offset),
610a3f829aeSBill Moore 			    dio->io_size);
611f94275ceSAdam Leventhal 		}
612a3f829aeSBill Moore 
613a3f829aeSBill Moore 		zio_add_child(dio, aio);
614ea8dc4b6Seschrock 		vdev_queue_io_remove(vq, dio);
615fa9e4066Sahrens 		zio_vdev_io_bypass(dio);
616a3f829aeSBill Moore 		zio_execute(dio);
61769962b56SMatthew Ahrens 	} while (dio != last);
618fa9e4066Sahrens 
619fa9e4066Sahrens 	return (aio);
620fa9e4066Sahrens }
621fa9e4066Sahrens 
62269962b56SMatthew Ahrens static zio_t *
vdev_queue_io_to_issue(vdev_queue_t * vq)62369962b56SMatthew Ahrens vdev_queue_io_to_issue(vdev_queue_t *vq)
62469962b56SMatthew Ahrens {
62569962b56SMatthew Ahrens 	zio_t *zio, *aio;
62669962b56SMatthew Ahrens 	zio_priority_t p;
62769962b56SMatthew Ahrens 	avl_index_t idx;
628*fe319232SJustin T. Gibbs 	avl_tree_t *tree;
62969962b56SMatthew Ahrens 	zio_t search;
63069962b56SMatthew Ahrens 
63169962b56SMatthew Ahrens again:
63269962b56SMatthew Ahrens 	ASSERT(MUTEX_HELD(&vq->vq_lock));
63369962b56SMatthew Ahrens 
63469962b56SMatthew Ahrens 	p = vdev_queue_class_to_issue(vq);
63569962b56SMatthew Ahrens 
63669962b56SMatthew Ahrens 	if (p == ZIO_PRIORITY_NUM_QUEUEABLE) {
63769962b56SMatthew Ahrens 		/* No eligible queued i/os */
63869962b56SMatthew Ahrens 		return (NULL);
63969962b56SMatthew Ahrens 	}
64069962b56SMatthew Ahrens 
64169962b56SMatthew Ahrens 	/*
64269962b56SMatthew Ahrens 	 * For LBA-ordered queues (async / scrub), issue the i/o which follows
64369962b56SMatthew Ahrens 	 * the most recently issued i/o in LBA (offset) order.
64469962b56SMatthew Ahrens 	 *
64569962b56SMatthew Ahrens 	 * For FIFO queues (sync), issue the i/o with the lowest timestamp.
64669962b56SMatthew Ahrens 	 */
647*fe319232SJustin T. Gibbs 	tree = vdev_queue_class_tree(vq, p);
64869962b56SMatthew Ahrens 	search.io_timestamp = 0;
64969962b56SMatthew Ahrens 	search.io_offset = vq->vq_last_offset + 1;
650*fe319232SJustin T. Gibbs 	VERIFY3P(avl_find(tree, &search, &idx), ==, NULL);
651*fe319232SJustin T. Gibbs 	zio = avl_nearest(tree, idx, AVL_AFTER);
65269962b56SMatthew Ahrens 	if (zio == NULL)
653*fe319232SJustin T. Gibbs 		zio = avl_first(tree);
65469962b56SMatthew Ahrens 	ASSERT3U(zio->io_priority, ==, p);
65569962b56SMatthew Ahrens 
65669962b56SMatthew Ahrens 	aio = vdev_queue_aggregate(vq, zio);
65769962b56SMatthew Ahrens 	if (aio != NULL)
65869962b56SMatthew Ahrens 		zio = aio;
65969962b56SMatthew Ahrens 	else
66069962b56SMatthew Ahrens 		vdev_queue_io_remove(vq, zio);
661fa9e4066Sahrens 
662f94275ceSAdam Leventhal 	/*
663f94275ceSAdam Leventhal 	 * If the I/O is or was optional and therefore has no data, we need to
664f94275ceSAdam Leventhal 	 * simply discard it. We need to drop the vdev queue's lock to avoid a
665f94275ceSAdam Leventhal 	 * deadlock that we could encounter since this I/O will complete
666f94275ceSAdam Leventhal 	 * immediately.
667f94275ceSAdam Leventhal 	 */
66869962b56SMatthew Ahrens 	if (zio->io_flags & ZIO_FLAG_NODATA) {
669f94275ceSAdam Leventhal 		mutex_exit(&vq->vq_lock);
67069962b56SMatthew Ahrens 		zio_vdev_io_bypass(zio);
67169962b56SMatthew Ahrens 		zio_execute(zio);
672f94275ceSAdam Leventhal 		mutex_enter(&vq->vq_lock);
673f94275ceSAdam Leventhal 		goto again;
674f94275ceSAdam Leventhal 	}
675f94275ceSAdam Leventhal 
67669962b56SMatthew Ahrens 	vdev_queue_pending_add(vq, zio);
67769962b56SMatthew Ahrens 	vq->vq_last_offset = zio->io_offset;
678fa9e4066Sahrens 
67969962b56SMatthew Ahrens 	return (zio);
680fa9e4066Sahrens }
681fa9e4066Sahrens 
682fa9e4066Sahrens zio_t *
vdev_queue_io(zio_t * zio)683fa9e4066Sahrens vdev_queue_io(zio_t *zio)
684fa9e4066Sahrens {
685fa9e4066Sahrens 	vdev_queue_t *vq = &zio->io_vd->vdev_queue;
686fa9e4066Sahrens 	zio_t *nio;
687fa9e4066Sahrens 
688fa9e4066Sahrens 	if (zio->io_flags & ZIO_FLAG_DONT_QUEUE)
689fa9e4066Sahrens 		return (zio);
690fa9e4066Sahrens 
69169962b56SMatthew Ahrens 	/*
69269962b56SMatthew Ahrens 	 * Children i/os inherent their parent's priority, which might
69369962b56SMatthew Ahrens 	 * not match the child's i/o type.  Fix it up here.
69469962b56SMatthew Ahrens 	 */
69569962b56SMatthew Ahrens 	if (zio->io_type == ZIO_TYPE_READ) {
69669962b56SMatthew Ahrens 		if (zio->io_priority != ZIO_PRIORITY_SYNC_READ &&
69769962b56SMatthew Ahrens 		    zio->io_priority != ZIO_PRIORITY_ASYNC_READ &&
69869962b56SMatthew Ahrens 		    zio->io_priority != ZIO_PRIORITY_SCRUB)
69969962b56SMatthew Ahrens 			zio->io_priority = ZIO_PRIORITY_ASYNC_READ;
70069962b56SMatthew Ahrens 	} else {
70169962b56SMatthew Ahrens 		ASSERT(zio->io_type == ZIO_TYPE_WRITE);
70269962b56SMatthew Ahrens 		if (zio->io_priority != ZIO_PRIORITY_SYNC_WRITE &&
70369962b56SMatthew Ahrens 		    zio->io_priority != ZIO_PRIORITY_ASYNC_WRITE)
70469962b56SMatthew Ahrens 			zio->io_priority = ZIO_PRIORITY_ASYNC_WRITE;
70569962b56SMatthew Ahrens 	}
70669962b56SMatthew Ahrens 
707fa9e4066Sahrens 	zio->io_flags |= ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE;
708fa9e4066Sahrens 
709fa9e4066Sahrens 	mutex_enter(&vq->vq_lock);
710c55e05cbSMatthew Ahrens 	zio->io_timestamp = gethrtime();
711ea8dc4b6Seschrock 	vdev_queue_io_add(vq, zio);
71269962b56SMatthew Ahrens 	nio = vdev_queue_io_to_issue(vq);
713fa9e4066Sahrens 	mutex_exit(&vq->vq_lock);
714fa9e4066Sahrens 
715e05725b1Sbonwick 	if (nio == NULL)
716fa9e4066Sahrens 		return (NULL);
717e05725b1Sbonwick 
718e05725b1Sbonwick 	if (nio->io_done == vdev_queue_agg_io_done) {
719e05725b1Sbonwick 		zio_nowait(nio);
720e05725b1Sbonwick 		return (NULL);
721e05725b1Sbonwick 	}
722e05725b1Sbonwick 
723e05725b1Sbonwick 	return (nio);
724fa9e4066Sahrens }
725fa9e4066Sahrens 
726fa9e4066Sahrens void
vdev_queue_io_done(zio_t * zio)727fa9e4066Sahrens vdev_queue_io_done(zio_t *zio)
728fa9e4066Sahrens {
729fa9e4066Sahrens 	vdev_queue_t *vq = &zio->io_vd->vdev_queue;
73069962b56SMatthew Ahrens 	zio_t *nio;
731fa9e4066Sahrens 
732283b8460SGeorge.Wilson 	if (zio_injection_enabled)
733283b8460SGeorge.Wilson 		delay(SEC_TO_TICK(zio_handle_io_delay(zio)));
734283b8460SGeorge.Wilson 
735fa9e4066Sahrens 	mutex_enter(&vq->vq_lock);
736fa9e4066Sahrens 
737c3a66015SMatthew Ahrens 	vdev_queue_pending_remove(vq, zio);
738fa9e4066Sahrens 
739c55e05cbSMatthew Ahrens 	vq->vq_io_complete_ts = gethrtime();
740283b8460SGeorge.Wilson 
74169962b56SMatthew Ahrens 	while ((nio = vdev_queue_io_to_issue(vq)) != NULL) {
742fa9e4066Sahrens 		mutex_exit(&vq->vq_lock);
743e05725b1Sbonwick 		if (nio->io_done == vdev_queue_agg_io_done) {
744e05725b1Sbonwick 			zio_nowait(nio);
745e05725b1Sbonwick 		} else {
746fa9e4066Sahrens 			zio_vdev_io_reissue(nio);
747e05725b1Sbonwick 			zio_execute(nio);
748e05725b1Sbonwick 		}
749fa9e4066Sahrens 		mutex_enter(&vq->vq_lock);
750fa9e4066Sahrens 	}
751fa9e4066Sahrens 
752fa9e4066Sahrens 	mutex_exit(&vq->vq_lock);
753fa9e4066Sahrens }
754