xref: /freebsd/sys/contrib/openzfs/module/zfs/vdev_queue.c (revision 4e8d558c9d1cf3e7e424e3fb123b01979c3d57f2)
1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy  * CDDL HEADER START
3eda14cbcSMatt Macy  *
4eda14cbcSMatt Macy  * The contents of this file are subject to the terms of the
5eda14cbcSMatt Macy  * Common Development and Distribution License (the "License").
6eda14cbcSMatt Macy  * You may not use this file except in compliance with the License.
7eda14cbcSMatt Macy  *
8eda14cbcSMatt Macy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9271171e0SMartin Matuska  * or https://opensource.org/licenses/CDDL-1.0.
10eda14cbcSMatt Macy  * See the License for the specific language governing permissions
11eda14cbcSMatt Macy  * and limitations under the License.
12eda14cbcSMatt Macy  *
13eda14cbcSMatt Macy  * When distributing Covered Code, include this CDDL HEADER in each
14eda14cbcSMatt Macy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15eda14cbcSMatt Macy  * If applicable, add the following below this CDDL HEADER, with the
16eda14cbcSMatt Macy  * fields enclosed by brackets "[]" replaced with your own identifying
17eda14cbcSMatt Macy  * information: Portions Copyright [yyyy] [name of copyright owner]
18eda14cbcSMatt Macy  *
19eda14cbcSMatt Macy  * CDDL HEADER END
20eda14cbcSMatt Macy  */
21eda14cbcSMatt Macy /*
22eda14cbcSMatt Macy  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23eda14cbcSMatt Macy  * Use is subject to license terms.
24eda14cbcSMatt Macy  */
25eda14cbcSMatt Macy 
26eda14cbcSMatt Macy /*
27eda14cbcSMatt Macy  * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
28eda14cbcSMatt Macy  */
29eda14cbcSMatt Macy 
30eda14cbcSMatt Macy #include <sys/zfs_context.h>
31eda14cbcSMatt Macy #include <sys/vdev_impl.h>
32eda14cbcSMatt Macy #include <sys/spa_impl.h>
33eda14cbcSMatt Macy #include <sys/zio.h>
34eda14cbcSMatt Macy #include <sys/avl.h>
35eda14cbcSMatt Macy #include <sys/dsl_pool.h>
36eda14cbcSMatt Macy #include <sys/metaslab_impl.h>
37eda14cbcSMatt Macy #include <sys/spa.h>
38eda14cbcSMatt Macy #include <sys/abd.h>
39eda14cbcSMatt Macy 
40eda14cbcSMatt Macy /*
41eda14cbcSMatt Macy  * ZFS I/O Scheduler
42eda14cbcSMatt Macy  * ---------------
43eda14cbcSMatt Macy  *
44eda14cbcSMatt Macy  * ZFS issues I/O operations to leaf vdevs to satisfy and complete zios.  The
45eda14cbcSMatt Macy  * I/O scheduler determines when and in what order those operations are
46eda14cbcSMatt Macy  * issued.  The I/O scheduler divides operations into five I/O classes
47eda14cbcSMatt Macy  * prioritized in the following order: sync read, sync write, async read,
48eda14cbcSMatt Macy  * async write, and scrub/resilver.  Each queue defines the minimum and
49eda14cbcSMatt Macy  * maximum number of concurrent operations that may be issued to the device.
50eda14cbcSMatt Macy  * In addition, the device has an aggregate maximum. Note that the sum of the
51eda14cbcSMatt Macy  * per-queue minimums must not exceed the aggregate maximum. If the
52eda14cbcSMatt Macy  * sum of the per-queue maximums exceeds the aggregate maximum, then the
53eda14cbcSMatt Macy  * number of active i/os may reach zfs_vdev_max_active, in which case no
54eda14cbcSMatt Macy  * further i/os will be issued regardless of whether all per-queue
55eda14cbcSMatt Macy  * minimums have been met.
56eda14cbcSMatt Macy  *
57eda14cbcSMatt Macy  * For many physical devices, throughput increases with the number of
58eda14cbcSMatt Macy  * concurrent operations, but latency typically suffers. Further, physical
59eda14cbcSMatt Macy  * devices typically have a limit at which more concurrent operations have no
60eda14cbcSMatt Macy  * effect on throughput or can actually cause it to decrease.
61eda14cbcSMatt Macy  *
62eda14cbcSMatt Macy  * The scheduler selects the next operation to issue by first looking for an
63eda14cbcSMatt Macy  * I/O class whose minimum has not been satisfied. Once all are satisfied and
64eda14cbcSMatt Macy  * the aggregate maximum has not been hit, the scheduler looks for classes
65eda14cbcSMatt Macy  * whose maximum has not been satisfied. Iteration through the I/O classes is
66eda14cbcSMatt Macy  * done in the order specified above. No further operations are issued if the
67eda14cbcSMatt Macy  * aggregate maximum number of concurrent operations has been hit or if there
68eda14cbcSMatt Macy  * are no operations queued for an I/O class that has not hit its maximum.
69eda14cbcSMatt Macy  * Every time an i/o is queued or an operation completes, the I/O scheduler
70eda14cbcSMatt Macy  * looks for new operations to issue.
71eda14cbcSMatt Macy  *
72eda14cbcSMatt Macy  * All I/O classes have a fixed maximum number of outstanding operations
73eda14cbcSMatt Macy  * except for the async write class. Asynchronous writes represent the data
74eda14cbcSMatt Macy  * that is committed to stable storage during the syncing stage for
75eda14cbcSMatt Macy  * transaction groups (see txg.c). Transaction groups enter the syncing state
76eda14cbcSMatt Macy  * periodically so the number of queued async writes will quickly burst up and
77eda14cbcSMatt Macy  * then bleed down to zero. Rather than servicing them as quickly as possible,
78eda14cbcSMatt Macy  * the I/O scheduler changes the maximum number of active async write i/os
79eda14cbcSMatt Macy  * according to the amount of dirty data in the pool (see dsl_pool.c). Since
80eda14cbcSMatt Macy  * both throughput and latency typically increase with the number of
81eda14cbcSMatt Macy  * concurrent operations issued to physical devices, reducing the burstiness
82eda14cbcSMatt Macy  * in the number of concurrent operations also stabilizes the response time of
83eda14cbcSMatt Macy  * operations from other -- and in particular synchronous -- queues. In broad
84eda14cbcSMatt Macy  * strokes, the I/O scheduler will issue more concurrent operations from the
85eda14cbcSMatt Macy  * async write queue as there's more dirty data in the pool.
86eda14cbcSMatt Macy  *
87eda14cbcSMatt Macy  * Async Writes
88eda14cbcSMatt Macy  *
89eda14cbcSMatt Macy  * The number of concurrent operations issued for the async write I/O class
90eda14cbcSMatt Macy  * follows a piece-wise linear function defined by a few adjustable points.
91eda14cbcSMatt Macy  *
92eda14cbcSMatt Macy  *        |                   o---------| <-- zfs_vdev_async_write_max_active
93eda14cbcSMatt Macy  *   ^    |                  /^         |
94eda14cbcSMatt Macy  *   |    |                 / |         |
95eda14cbcSMatt Macy  * active |                /  |         |
96eda14cbcSMatt Macy  *  I/O   |               /   |         |
97eda14cbcSMatt Macy  * count  |              /    |         |
98eda14cbcSMatt Macy  *        |             /     |         |
99eda14cbcSMatt Macy  *        |------------o      |         | <-- zfs_vdev_async_write_min_active
100eda14cbcSMatt Macy  *       0|____________^______|_________|
101eda14cbcSMatt Macy  *        0%           |      |       100% of zfs_dirty_data_max
102eda14cbcSMatt Macy  *                     |      |
103eda14cbcSMatt Macy  *                     |      `-- zfs_vdev_async_write_active_max_dirty_percent
104eda14cbcSMatt Macy  *                     `--------- zfs_vdev_async_write_active_min_dirty_percent
105eda14cbcSMatt Macy  *
106eda14cbcSMatt Macy  * Until the amount of dirty data exceeds a minimum percentage of the dirty
107eda14cbcSMatt Macy  * data allowed in the pool, the I/O scheduler will limit the number of
108eda14cbcSMatt Macy  * concurrent operations to the minimum. As that threshold is crossed, the
109eda14cbcSMatt Macy  * number of concurrent operations issued increases linearly to the maximum at
110eda14cbcSMatt Macy  * the specified maximum percentage of the dirty data allowed in the pool.
111eda14cbcSMatt Macy  *
112eda14cbcSMatt Macy  * Ideally, the amount of dirty data on a busy pool will stay in the sloped
113eda14cbcSMatt Macy  * part of the function between zfs_vdev_async_write_active_min_dirty_percent
114eda14cbcSMatt Macy  * and zfs_vdev_async_write_active_max_dirty_percent. If it exceeds the
115eda14cbcSMatt Macy  * maximum percentage, this indicates that the rate of incoming data is
116eda14cbcSMatt Macy  * greater than the rate that the backend storage can handle. In this case, we
117eda14cbcSMatt Macy  * must further throttle incoming writes (see dmu_tx_delay() for details).
118eda14cbcSMatt Macy  */
119eda14cbcSMatt Macy 
120eda14cbcSMatt Macy /*
121eda14cbcSMatt Macy  * The maximum number of i/os active to each device.  Ideally, this will be >=
1227877fdebSMatt Macy  * the sum of each queue's max_active.
123eda14cbcSMatt Macy  */
124be181ee2SMartin Matuska uint_t zfs_vdev_max_active = 1000;
125eda14cbcSMatt Macy 
126eda14cbcSMatt Macy /*
127eda14cbcSMatt Macy  * Per-queue limits on the number of i/os active to each device.  If the
128eda14cbcSMatt Macy  * number of active i/os is < zfs_vdev_max_active, then the min_active comes
1297877fdebSMatt Macy  * into play.  We will send min_active from each queue round-robin, and then
1307877fdebSMatt Macy  * send from queues in the order defined by zio_priority_t up to max_active.
1317877fdebSMatt Macy  * Some queues have additional mechanisms to limit number of active I/Os in
1327877fdebSMatt Macy  * addition to min_active and max_active, see below.
133eda14cbcSMatt Macy  *
134eda14cbcSMatt Macy  * In general, smaller max_active's will lead to lower latency of synchronous
135eda14cbcSMatt Macy  * operations.  Larger max_active's may lead to higher overall throughput,
136eda14cbcSMatt Macy  * depending on underlying storage.
137eda14cbcSMatt Macy  *
138eda14cbcSMatt Macy  * The ratio of the queues' max_actives determines the balance of performance
139eda14cbcSMatt Macy  * between reads, writes, and scrubs.  E.g., increasing
140eda14cbcSMatt Macy  * zfs_vdev_scrub_max_active will cause the scrub or resilver to complete
141eda14cbcSMatt Macy  * more quickly, but reads and writes to have higher latency and lower
142eda14cbcSMatt Macy  * throughput.
143eda14cbcSMatt Macy  */
144be181ee2SMartin Matuska static uint_t zfs_vdev_sync_read_min_active = 10;
145be181ee2SMartin Matuska static uint_t zfs_vdev_sync_read_max_active = 10;
146be181ee2SMartin Matuska static uint_t zfs_vdev_sync_write_min_active = 10;
147be181ee2SMartin Matuska static uint_t zfs_vdev_sync_write_max_active = 10;
148be181ee2SMartin Matuska static uint_t zfs_vdev_async_read_min_active = 1;
149be181ee2SMartin Matuska /*  */ uint_t zfs_vdev_async_read_max_active = 3;
150be181ee2SMartin Matuska static uint_t zfs_vdev_async_write_min_active = 2;
151be181ee2SMartin Matuska /*  */ uint_t zfs_vdev_async_write_max_active = 10;
152be181ee2SMartin Matuska static uint_t zfs_vdev_scrub_min_active = 1;
153be181ee2SMartin Matuska static uint_t zfs_vdev_scrub_max_active = 3;
154be181ee2SMartin Matuska static uint_t zfs_vdev_removal_min_active = 1;
155be181ee2SMartin Matuska static uint_t zfs_vdev_removal_max_active = 2;
156be181ee2SMartin Matuska static uint_t zfs_vdev_initializing_min_active = 1;
157be181ee2SMartin Matuska static uint_t zfs_vdev_initializing_max_active = 1;
158be181ee2SMartin Matuska static uint_t zfs_vdev_trim_min_active = 1;
159be181ee2SMartin Matuska static uint_t zfs_vdev_trim_max_active = 2;
160be181ee2SMartin Matuska static uint_t zfs_vdev_rebuild_min_active = 1;
161be181ee2SMartin Matuska static uint_t zfs_vdev_rebuild_max_active = 3;
162eda14cbcSMatt Macy 
163eda14cbcSMatt Macy /*
164eda14cbcSMatt Macy  * When the pool has less than zfs_vdev_async_write_active_min_dirty_percent
165eda14cbcSMatt Macy  * dirty data, use zfs_vdev_async_write_min_active.  When it has more than
166eda14cbcSMatt Macy  * zfs_vdev_async_write_active_max_dirty_percent, use
167eda14cbcSMatt Macy  * zfs_vdev_async_write_max_active. The value is linearly interpolated
168eda14cbcSMatt Macy  * between min and max.
169eda14cbcSMatt Macy  */
170be181ee2SMartin Matuska uint_t zfs_vdev_async_write_active_min_dirty_percent = 30;
171be181ee2SMartin Matuska uint_t zfs_vdev_async_write_active_max_dirty_percent = 60;
172eda14cbcSMatt Macy 
173eda14cbcSMatt Macy /*
1747877fdebSMatt Macy  * For non-interactive I/O (scrub, resilver, removal, initialize and rebuild),
1757877fdebSMatt Macy  * the number of concurrently-active I/O's is limited to *_min_active, unless
1767877fdebSMatt Macy  * the vdev is "idle".  When there are no interactive I/Os active (sync or
1777877fdebSMatt Macy  * async), and zfs_vdev_nia_delay I/Os have completed since the last
1787877fdebSMatt Macy  * interactive I/O, then the vdev is considered to be "idle", and the number
1797877fdebSMatt Macy  * of concurrently-active non-interactive I/O's is increased to *_max_active.
1807877fdebSMatt Macy  */
181e92ffd9bSMartin Matuska static uint_t zfs_vdev_nia_delay = 5;
1827877fdebSMatt Macy 
1837877fdebSMatt Macy /*
1847877fdebSMatt Macy  * Some HDDs tend to prioritize sequential I/O so high that concurrent
1857877fdebSMatt Macy  * random I/O latency reaches several seconds.  On some HDDs it happens
1867877fdebSMatt Macy  * even if sequential I/Os are submitted one at a time, and so setting
1877877fdebSMatt Macy  * *_max_active to 1 does not help.  To prevent non-interactive I/Os, like
1887877fdebSMatt Macy  * scrub, from monopolizing the device no more than zfs_vdev_nia_credit
1897877fdebSMatt Macy  * I/Os can be sent while there are outstanding incomplete interactive
1907877fdebSMatt Macy  * I/Os.  This enforced wait ensures the HDD services the interactive I/O
1917877fdebSMatt Macy  * within a reasonable amount of time.
1927877fdebSMatt Macy  */
193e92ffd9bSMartin Matuska static uint_t zfs_vdev_nia_credit = 5;
1947877fdebSMatt Macy 
1957877fdebSMatt Macy /*
196eda14cbcSMatt Macy  * To reduce IOPs, we aggregate small adjacent I/Os into one large I/O.
197eda14cbcSMatt Macy  * For read I/Os, we also aggregate across small adjacency gaps; for writes
198eda14cbcSMatt Macy  * we include spans of optional I/Os to aid aggregation at the disk even when
199eda14cbcSMatt Macy  * they aren't able to help us aggregate at this level.
200eda14cbcSMatt Macy  */
201be181ee2SMartin Matuska static uint_t zfs_vdev_aggregation_limit = 1 << 20;
202be181ee2SMartin Matuska static uint_t zfs_vdev_aggregation_limit_non_rotating = SPA_OLD_MAXBLOCKSIZE;
203be181ee2SMartin Matuska static uint_t zfs_vdev_read_gap_limit = 32 << 10;
204be181ee2SMartin Matuska static uint_t zfs_vdev_write_gap_limit = 4 << 10;
205eda14cbcSMatt Macy 
206eda14cbcSMatt Macy /*
207eda14cbcSMatt Macy  * Define the queue depth percentage for each top-level. This percentage is
208eda14cbcSMatt Macy  * used in conjunction with zfs_vdev_async_max_active to determine how many
209eda14cbcSMatt Macy  * allocations a specific top-level vdev should handle. Once the queue depth
210eda14cbcSMatt Macy  * reaches zfs_vdev_queue_depth_pct * zfs_vdev_async_write_max_active / 100
211eda14cbcSMatt Macy  * then allocator will stop allocating blocks on that top-level device.
212eda14cbcSMatt Macy  * The default kernel setting is 1000% which will yield 100 allocations per
213eda14cbcSMatt Macy  * device. For userland testing, the default setting is 300% which equates
214eda14cbcSMatt Macy  * to 30 allocations per device.
215eda14cbcSMatt Macy  */
216eda14cbcSMatt Macy #ifdef _KERNEL
217be181ee2SMartin Matuska uint_t zfs_vdev_queue_depth_pct = 1000;
218eda14cbcSMatt Macy #else
219be181ee2SMartin Matuska uint_t zfs_vdev_queue_depth_pct = 300;
220eda14cbcSMatt Macy #endif
221eda14cbcSMatt Macy 
222eda14cbcSMatt Macy /*
223eda14cbcSMatt Macy  * When performing allocations for a given metaslab, we want to make sure that
224eda14cbcSMatt Macy  * there are enough IOs to aggregate together to improve throughput. We want to
225eda14cbcSMatt Macy  * ensure that there are at least 128k worth of IOs that can be aggregated, and
226eda14cbcSMatt Macy  * we assume that the average allocation size is 4k, so we need the queue depth
227eda14cbcSMatt Macy  * to be 32 per allocator to get good aggregation of sequential writes.
228eda14cbcSMatt Macy  */
229be181ee2SMartin Matuska uint_t zfs_vdev_def_queue_depth = 32;
230eda14cbcSMatt Macy 
231eda14cbcSMatt Macy /*
232eda14cbcSMatt Macy  * Allow TRIM I/Os to be aggregated.  This should normally not be needed since
233eda14cbcSMatt Macy  * TRIM I/O for extents up to zfs_trim_extent_bytes_max (128M) can be submitted
234eda14cbcSMatt Macy  * by the TRIM code in zfs_trim.c.
235eda14cbcSMatt Macy  */
236be181ee2SMartin Matuska static uint_t zfs_vdev_aggregate_trim = 0;
237eda14cbcSMatt Macy 
238eda14cbcSMatt Macy static int
239eda14cbcSMatt Macy vdev_queue_offset_compare(const void *x1, const void *x2)
240eda14cbcSMatt Macy {
241eda14cbcSMatt Macy 	const zio_t *z1 = (const zio_t *)x1;
242eda14cbcSMatt Macy 	const zio_t *z2 = (const zio_t *)x2;
243eda14cbcSMatt Macy 
244eda14cbcSMatt Macy 	int cmp = TREE_CMP(z1->io_offset, z2->io_offset);
245eda14cbcSMatt Macy 
246eda14cbcSMatt Macy 	if (likely(cmp))
247eda14cbcSMatt Macy 		return (cmp);
248eda14cbcSMatt Macy 
249eda14cbcSMatt Macy 	return (TREE_PCMP(z1, z2));
250eda14cbcSMatt Macy }
251eda14cbcSMatt Macy 
252eda14cbcSMatt Macy static inline avl_tree_t *
253eda14cbcSMatt Macy vdev_queue_class_tree(vdev_queue_t *vq, zio_priority_t p)
254eda14cbcSMatt Macy {
255eda14cbcSMatt Macy 	return (&vq->vq_class[p].vqc_queued_tree);
256eda14cbcSMatt Macy }
257eda14cbcSMatt Macy 
258eda14cbcSMatt Macy static inline avl_tree_t *
259eda14cbcSMatt Macy vdev_queue_type_tree(vdev_queue_t *vq, zio_type_t t)
260eda14cbcSMatt Macy {
261eda14cbcSMatt Macy 	ASSERT(t == ZIO_TYPE_READ || t == ZIO_TYPE_WRITE || t == ZIO_TYPE_TRIM);
262eda14cbcSMatt Macy 	if (t == ZIO_TYPE_READ)
263eda14cbcSMatt Macy 		return (&vq->vq_read_offset_tree);
264eda14cbcSMatt Macy 	else if (t == ZIO_TYPE_WRITE)
265eda14cbcSMatt Macy 		return (&vq->vq_write_offset_tree);
266eda14cbcSMatt Macy 	else
267eda14cbcSMatt Macy 		return (&vq->vq_trim_offset_tree);
268eda14cbcSMatt Macy }
269eda14cbcSMatt Macy 
270eda14cbcSMatt Macy static int
271eda14cbcSMatt Macy vdev_queue_timestamp_compare(const void *x1, const void *x2)
272eda14cbcSMatt Macy {
273eda14cbcSMatt Macy 	const zio_t *z1 = (const zio_t *)x1;
274eda14cbcSMatt Macy 	const zio_t *z2 = (const zio_t *)x2;
275eda14cbcSMatt Macy 
276eda14cbcSMatt Macy 	int cmp = TREE_CMP(z1->io_timestamp, z2->io_timestamp);
277eda14cbcSMatt Macy 
278eda14cbcSMatt Macy 	if (likely(cmp))
279eda14cbcSMatt Macy 		return (cmp);
280eda14cbcSMatt Macy 
281eda14cbcSMatt Macy 	return (TREE_PCMP(z1, z2));
282eda14cbcSMatt Macy }
283eda14cbcSMatt Macy 
284be181ee2SMartin Matuska static uint_t
2857877fdebSMatt Macy vdev_queue_class_min_active(vdev_queue_t *vq, zio_priority_t p)
286eda14cbcSMatt Macy {
287eda14cbcSMatt Macy 	switch (p) {
288eda14cbcSMatt Macy 	case ZIO_PRIORITY_SYNC_READ:
289eda14cbcSMatt Macy 		return (zfs_vdev_sync_read_min_active);
290eda14cbcSMatt Macy 	case ZIO_PRIORITY_SYNC_WRITE:
291eda14cbcSMatt Macy 		return (zfs_vdev_sync_write_min_active);
292eda14cbcSMatt Macy 	case ZIO_PRIORITY_ASYNC_READ:
293eda14cbcSMatt Macy 		return (zfs_vdev_async_read_min_active);
294eda14cbcSMatt Macy 	case ZIO_PRIORITY_ASYNC_WRITE:
295eda14cbcSMatt Macy 		return (zfs_vdev_async_write_min_active);
296eda14cbcSMatt Macy 	case ZIO_PRIORITY_SCRUB:
2977877fdebSMatt Macy 		return (vq->vq_ia_active == 0 ? zfs_vdev_scrub_min_active :
2987877fdebSMatt Macy 		    MIN(vq->vq_nia_credit, zfs_vdev_scrub_min_active));
299eda14cbcSMatt Macy 	case ZIO_PRIORITY_REMOVAL:
3007877fdebSMatt Macy 		return (vq->vq_ia_active == 0 ? zfs_vdev_removal_min_active :
3017877fdebSMatt Macy 		    MIN(vq->vq_nia_credit, zfs_vdev_removal_min_active));
302eda14cbcSMatt Macy 	case ZIO_PRIORITY_INITIALIZING:
3037877fdebSMatt Macy 		return (vq->vq_ia_active == 0 ?zfs_vdev_initializing_min_active:
3047877fdebSMatt Macy 		    MIN(vq->vq_nia_credit, zfs_vdev_initializing_min_active));
305eda14cbcSMatt Macy 	case ZIO_PRIORITY_TRIM:
306eda14cbcSMatt Macy 		return (zfs_vdev_trim_min_active);
307eda14cbcSMatt Macy 	case ZIO_PRIORITY_REBUILD:
3087877fdebSMatt Macy 		return (vq->vq_ia_active == 0 ? zfs_vdev_rebuild_min_active :
3097877fdebSMatt Macy 		    MIN(vq->vq_nia_credit, zfs_vdev_rebuild_min_active));
310eda14cbcSMatt Macy 	default:
311eda14cbcSMatt Macy 		panic("invalid priority %u", p);
312eda14cbcSMatt Macy 		return (0);
313eda14cbcSMatt Macy 	}
314eda14cbcSMatt Macy }
315eda14cbcSMatt Macy 
316be181ee2SMartin Matuska static uint_t
317eda14cbcSMatt Macy vdev_queue_max_async_writes(spa_t *spa)
318eda14cbcSMatt Macy {
319be181ee2SMartin Matuska 	uint_t writes;
320eda14cbcSMatt Macy 	uint64_t dirty = 0;
321eda14cbcSMatt Macy 	dsl_pool_t *dp = spa_get_dsl(spa);
322eda14cbcSMatt Macy 	uint64_t min_bytes = zfs_dirty_data_max *
323eda14cbcSMatt Macy 	    zfs_vdev_async_write_active_min_dirty_percent / 100;
324eda14cbcSMatt Macy 	uint64_t max_bytes = zfs_dirty_data_max *
325eda14cbcSMatt Macy 	    zfs_vdev_async_write_active_max_dirty_percent / 100;
326eda14cbcSMatt Macy 
327eda14cbcSMatt Macy 	/*
328eda14cbcSMatt Macy 	 * Async writes may occur before the assignment of the spa's
329eda14cbcSMatt Macy 	 * dsl_pool_t if a self-healing zio is issued prior to the
330eda14cbcSMatt Macy 	 * completion of dmu_objset_open_impl().
331eda14cbcSMatt Macy 	 */
332eda14cbcSMatt Macy 	if (dp == NULL)
333eda14cbcSMatt Macy 		return (zfs_vdev_async_write_max_active);
334eda14cbcSMatt Macy 
335eda14cbcSMatt Macy 	/*
336eda14cbcSMatt Macy 	 * Sync tasks correspond to interactive user actions. To reduce the
337eda14cbcSMatt Macy 	 * execution time of those actions we push data out as fast as possible.
338eda14cbcSMatt Macy 	 */
3397877fdebSMatt Macy 	dirty = dp->dp_dirty_total;
3407877fdebSMatt Macy 	if (dirty > max_bytes || spa_has_pending_synctask(spa))
341eda14cbcSMatt Macy 		return (zfs_vdev_async_write_max_active);
342eda14cbcSMatt Macy 
343eda14cbcSMatt Macy 	if (dirty < min_bytes)
344eda14cbcSMatt Macy 		return (zfs_vdev_async_write_min_active);
345eda14cbcSMatt Macy 
346eda14cbcSMatt Macy 	/*
347eda14cbcSMatt Macy 	 * linear interpolation:
348eda14cbcSMatt Macy 	 * slope = (max_writes - min_writes) / (max_bytes - min_bytes)
349eda14cbcSMatt Macy 	 * move right by min_bytes
350eda14cbcSMatt Macy 	 * move up by min_writes
351eda14cbcSMatt Macy 	 */
352eda14cbcSMatt Macy 	writes = (dirty - min_bytes) *
353eda14cbcSMatt Macy 	    (zfs_vdev_async_write_max_active -
354eda14cbcSMatt Macy 	    zfs_vdev_async_write_min_active) /
355eda14cbcSMatt Macy 	    (max_bytes - min_bytes) +
356eda14cbcSMatt Macy 	    zfs_vdev_async_write_min_active;
357eda14cbcSMatt Macy 	ASSERT3U(writes, >=, zfs_vdev_async_write_min_active);
358eda14cbcSMatt Macy 	ASSERT3U(writes, <=, zfs_vdev_async_write_max_active);
359eda14cbcSMatt Macy 	return (writes);
360eda14cbcSMatt Macy }
361eda14cbcSMatt Macy 
362be181ee2SMartin Matuska static uint_t
3637877fdebSMatt Macy vdev_queue_class_max_active(spa_t *spa, vdev_queue_t *vq, zio_priority_t p)
364eda14cbcSMatt Macy {
365eda14cbcSMatt Macy 	switch (p) {
366eda14cbcSMatt Macy 	case ZIO_PRIORITY_SYNC_READ:
367eda14cbcSMatt Macy 		return (zfs_vdev_sync_read_max_active);
368eda14cbcSMatt Macy 	case ZIO_PRIORITY_SYNC_WRITE:
369eda14cbcSMatt Macy 		return (zfs_vdev_sync_write_max_active);
370eda14cbcSMatt Macy 	case ZIO_PRIORITY_ASYNC_READ:
371eda14cbcSMatt Macy 		return (zfs_vdev_async_read_max_active);
372eda14cbcSMatt Macy 	case ZIO_PRIORITY_ASYNC_WRITE:
373eda14cbcSMatt Macy 		return (vdev_queue_max_async_writes(spa));
374eda14cbcSMatt Macy 	case ZIO_PRIORITY_SCRUB:
3757877fdebSMatt Macy 		if (vq->vq_ia_active > 0) {
3767877fdebSMatt Macy 			return (MIN(vq->vq_nia_credit,
3777877fdebSMatt Macy 			    zfs_vdev_scrub_min_active));
3787877fdebSMatt Macy 		} else if (vq->vq_nia_credit < zfs_vdev_nia_delay)
3797877fdebSMatt Macy 			return (MAX(1, zfs_vdev_scrub_min_active));
380eda14cbcSMatt Macy 		return (zfs_vdev_scrub_max_active);
381eda14cbcSMatt Macy 	case ZIO_PRIORITY_REMOVAL:
3827877fdebSMatt Macy 		if (vq->vq_ia_active > 0) {
3837877fdebSMatt Macy 			return (MIN(vq->vq_nia_credit,
3847877fdebSMatt Macy 			    zfs_vdev_removal_min_active));
3857877fdebSMatt Macy 		} else if (vq->vq_nia_credit < zfs_vdev_nia_delay)
3867877fdebSMatt Macy 			return (MAX(1, zfs_vdev_removal_min_active));
387eda14cbcSMatt Macy 		return (zfs_vdev_removal_max_active);
388eda14cbcSMatt Macy 	case ZIO_PRIORITY_INITIALIZING:
3897877fdebSMatt Macy 		if (vq->vq_ia_active > 0) {
3907877fdebSMatt Macy 			return (MIN(vq->vq_nia_credit,
3917877fdebSMatt Macy 			    zfs_vdev_initializing_min_active));
3927877fdebSMatt Macy 		} else if (vq->vq_nia_credit < zfs_vdev_nia_delay)
3937877fdebSMatt Macy 			return (MAX(1, zfs_vdev_initializing_min_active));
394eda14cbcSMatt Macy 		return (zfs_vdev_initializing_max_active);
395eda14cbcSMatt Macy 	case ZIO_PRIORITY_TRIM:
396eda14cbcSMatt Macy 		return (zfs_vdev_trim_max_active);
397eda14cbcSMatt Macy 	case ZIO_PRIORITY_REBUILD:
3987877fdebSMatt Macy 		if (vq->vq_ia_active > 0) {
3997877fdebSMatt Macy 			return (MIN(vq->vq_nia_credit,
4007877fdebSMatt Macy 			    zfs_vdev_rebuild_min_active));
4017877fdebSMatt Macy 		} else if (vq->vq_nia_credit < zfs_vdev_nia_delay)
4027877fdebSMatt Macy 			return (MAX(1, zfs_vdev_rebuild_min_active));
403eda14cbcSMatt Macy 		return (zfs_vdev_rebuild_max_active);
404eda14cbcSMatt Macy 	default:
405eda14cbcSMatt Macy 		panic("invalid priority %u", p);
406eda14cbcSMatt Macy 		return (0);
407eda14cbcSMatt Macy 	}
408eda14cbcSMatt Macy }
409eda14cbcSMatt Macy 
410eda14cbcSMatt Macy /*
411681ce946SMartin Matuska  * Return the i/o class to issue from, or ZIO_PRIORITY_NUM_QUEUEABLE if
412eda14cbcSMatt Macy  * there is no eligible class.
413eda14cbcSMatt Macy  */
414eda14cbcSMatt Macy static zio_priority_t
415eda14cbcSMatt Macy vdev_queue_class_to_issue(vdev_queue_t *vq)
416eda14cbcSMatt Macy {
417eda14cbcSMatt Macy 	spa_t *spa = vq->vq_vdev->vdev_spa;
4187877fdebSMatt Macy 	zio_priority_t p, n;
419eda14cbcSMatt Macy 
420eda14cbcSMatt Macy 	if (avl_numnodes(&vq->vq_active_tree) >= zfs_vdev_max_active)
421eda14cbcSMatt Macy 		return (ZIO_PRIORITY_NUM_QUEUEABLE);
422eda14cbcSMatt Macy 
4237877fdebSMatt Macy 	/*
4247877fdebSMatt Macy 	 * Find a queue that has not reached its minimum # outstanding i/os.
4257877fdebSMatt Macy 	 * Do round-robin to reduce starvation due to zfs_vdev_max_active
4267877fdebSMatt Macy 	 * and vq_nia_credit limits.
4277877fdebSMatt Macy 	 */
4287877fdebSMatt Macy 	for (n = 0; n < ZIO_PRIORITY_NUM_QUEUEABLE; n++) {
4297877fdebSMatt Macy 		p = (vq->vq_last_prio + n + 1) % ZIO_PRIORITY_NUM_QUEUEABLE;
430eda14cbcSMatt Macy 		if (avl_numnodes(vdev_queue_class_tree(vq, p)) > 0 &&
431eda14cbcSMatt Macy 		    vq->vq_class[p].vqc_active <
4327877fdebSMatt Macy 		    vdev_queue_class_min_active(vq, p)) {
4337877fdebSMatt Macy 			vq->vq_last_prio = p;
434eda14cbcSMatt Macy 			return (p);
435eda14cbcSMatt Macy 		}
4367877fdebSMatt Macy 	}
437eda14cbcSMatt Macy 
438eda14cbcSMatt Macy 	/*
439eda14cbcSMatt Macy 	 * If we haven't found a queue, look for one that hasn't reached its
440eda14cbcSMatt Macy 	 * maximum # outstanding i/os.
441eda14cbcSMatt Macy 	 */
442eda14cbcSMatt Macy 	for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
443eda14cbcSMatt Macy 		if (avl_numnodes(vdev_queue_class_tree(vq, p)) > 0 &&
444eda14cbcSMatt Macy 		    vq->vq_class[p].vqc_active <
4457877fdebSMatt Macy 		    vdev_queue_class_max_active(spa, vq, p)) {
4467877fdebSMatt Macy 			vq->vq_last_prio = p;
447eda14cbcSMatt Macy 			return (p);
448eda14cbcSMatt Macy 		}
4497877fdebSMatt Macy 	}
450eda14cbcSMatt Macy 
451eda14cbcSMatt Macy 	/* No eligible queued i/os */
452eda14cbcSMatt Macy 	return (ZIO_PRIORITY_NUM_QUEUEABLE);
453eda14cbcSMatt Macy }
454eda14cbcSMatt Macy 
455eda14cbcSMatt Macy void
456eda14cbcSMatt Macy vdev_queue_init(vdev_t *vd)
457eda14cbcSMatt Macy {
458eda14cbcSMatt Macy 	vdev_queue_t *vq = &vd->vdev_queue;
459eda14cbcSMatt Macy 	zio_priority_t p;
460eda14cbcSMatt Macy 
461eda14cbcSMatt Macy 	mutex_init(&vq->vq_lock, NULL, MUTEX_DEFAULT, NULL);
462eda14cbcSMatt Macy 	vq->vq_vdev = vd;
463eda14cbcSMatt Macy 	taskq_init_ent(&vd->vdev_queue.vq_io_search.io_tqent);
464eda14cbcSMatt Macy 
465eda14cbcSMatt Macy 	avl_create(&vq->vq_active_tree, vdev_queue_offset_compare,
466eda14cbcSMatt Macy 	    sizeof (zio_t), offsetof(struct zio, io_queue_node));
467eda14cbcSMatt Macy 	avl_create(vdev_queue_type_tree(vq, ZIO_TYPE_READ),
468eda14cbcSMatt Macy 	    vdev_queue_offset_compare, sizeof (zio_t),
469eda14cbcSMatt Macy 	    offsetof(struct zio, io_offset_node));
470eda14cbcSMatt Macy 	avl_create(vdev_queue_type_tree(vq, ZIO_TYPE_WRITE),
471eda14cbcSMatt Macy 	    vdev_queue_offset_compare, sizeof (zio_t),
472eda14cbcSMatt Macy 	    offsetof(struct zio, io_offset_node));
473eda14cbcSMatt Macy 	avl_create(vdev_queue_type_tree(vq, ZIO_TYPE_TRIM),
474eda14cbcSMatt Macy 	    vdev_queue_offset_compare, sizeof (zio_t),
475eda14cbcSMatt Macy 	    offsetof(struct zio, io_offset_node));
476eda14cbcSMatt Macy 
477eda14cbcSMatt Macy 	for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
478eda14cbcSMatt Macy 		int (*compfn) (const void *, const void *);
479eda14cbcSMatt Macy 
480eda14cbcSMatt Macy 		/*
481eda14cbcSMatt Macy 		 * The synchronous/trim i/o queues are dispatched in FIFO rather
482eda14cbcSMatt Macy 		 * than LBA order. This provides more consistent latency for
483eda14cbcSMatt Macy 		 * these i/os.
484eda14cbcSMatt Macy 		 */
485eda14cbcSMatt Macy 		if (p == ZIO_PRIORITY_SYNC_READ ||
486eda14cbcSMatt Macy 		    p == ZIO_PRIORITY_SYNC_WRITE ||
487eda14cbcSMatt Macy 		    p == ZIO_PRIORITY_TRIM) {
488eda14cbcSMatt Macy 			compfn = vdev_queue_timestamp_compare;
489eda14cbcSMatt Macy 		} else {
490eda14cbcSMatt Macy 			compfn = vdev_queue_offset_compare;
491eda14cbcSMatt Macy 		}
492eda14cbcSMatt Macy 		avl_create(vdev_queue_class_tree(vq, p), compfn,
493eda14cbcSMatt Macy 		    sizeof (zio_t), offsetof(struct zio, io_queue_node));
494eda14cbcSMatt Macy 	}
495eda14cbcSMatt Macy 
496eda14cbcSMatt Macy 	vq->vq_last_offset = 0;
497eda14cbcSMatt Macy }
498eda14cbcSMatt Macy 
499eda14cbcSMatt Macy void
500eda14cbcSMatt Macy vdev_queue_fini(vdev_t *vd)
501eda14cbcSMatt Macy {
502eda14cbcSMatt Macy 	vdev_queue_t *vq = &vd->vdev_queue;
503eda14cbcSMatt Macy 
504eda14cbcSMatt Macy 	for (zio_priority_t p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++)
505eda14cbcSMatt Macy 		avl_destroy(vdev_queue_class_tree(vq, p));
506eda14cbcSMatt Macy 	avl_destroy(&vq->vq_active_tree);
507eda14cbcSMatt Macy 	avl_destroy(vdev_queue_type_tree(vq, ZIO_TYPE_READ));
508eda14cbcSMatt Macy 	avl_destroy(vdev_queue_type_tree(vq, ZIO_TYPE_WRITE));
509eda14cbcSMatt Macy 	avl_destroy(vdev_queue_type_tree(vq, ZIO_TYPE_TRIM));
510eda14cbcSMatt Macy 
511eda14cbcSMatt Macy 	mutex_destroy(&vq->vq_lock);
512eda14cbcSMatt Macy }
513eda14cbcSMatt Macy 
514eda14cbcSMatt Macy static void
515eda14cbcSMatt Macy vdev_queue_io_add(vdev_queue_t *vq, zio_t *zio)
516eda14cbcSMatt Macy {
517eda14cbcSMatt Macy 	ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
518eda14cbcSMatt Macy 	avl_add(vdev_queue_class_tree(vq, zio->io_priority), zio);
519eda14cbcSMatt Macy 	avl_add(vdev_queue_type_tree(vq, zio->io_type), zio);
520eda14cbcSMatt Macy }
521eda14cbcSMatt Macy 
522eda14cbcSMatt Macy static void
523eda14cbcSMatt Macy vdev_queue_io_remove(vdev_queue_t *vq, zio_t *zio)
524eda14cbcSMatt Macy {
525eda14cbcSMatt Macy 	ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
526eda14cbcSMatt Macy 	avl_remove(vdev_queue_class_tree(vq, zio->io_priority), zio);
527eda14cbcSMatt Macy 	avl_remove(vdev_queue_type_tree(vq, zio->io_type), zio);
528eda14cbcSMatt Macy }
529eda14cbcSMatt Macy 
5307877fdebSMatt Macy static boolean_t
5317877fdebSMatt Macy vdev_queue_is_interactive(zio_priority_t p)
5327877fdebSMatt Macy {
5337877fdebSMatt Macy 	switch (p) {
5347877fdebSMatt Macy 	case ZIO_PRIORITY_SCRUB:
5357877fdebSMatt Macy 	case ZIO_PRIORITY_REMOVAL:
5367877fdebSMatt Macy 	case ZIO_PRIORITY_INITIALIZING:
5377877fdebSMatt Macy 	case ZIO_PRIORITY_REBUILD:
5387877fdebSMatt Macy 		return (B_FALSE);
5397877fdebSMatt Macy 	default:
5407877fdebSMatt Macy 		return (B_TRUE);
5417877fdebSMatt Macy 	}
5427877fdebSMatt Macy }
5437877fdebSMatt Macy 
544eda14cbcSMatt Macy static void
545eda14cbcSMatt Macy vdev_queue_pending_add(vdev_queue_t *vq, zio_t *zio)
546eda14cbcSMatt Macy {
547eda14cbcSMatt Macy 	ASSERT(MUTEX_HELD(&vq->vq_lock));
548eda14cbcSMatt Macy 	ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
549eda14cbcSMatt Macy 	vq->vq_class[zio->io_priority].vqc_active++;
5507877fdebSMatt Macy 	if (vdev_queue_is_interactive(zio->io_priority)) {
5517877fdebSMatt Macy 		if (++vq->vq_ia_active == 1)
5527877fdebSMatt Macy 			vq->vq_nia_credit = 1;
5537877fdebSMatt Macy 	} else if (vq->vq_ia_active > 0) {
5547877fdebSMatt Macy 		vq->vq_nia_credit--;
5557877fdebSMatt Macy 	}
556eda14cbcSMatt Macy 	avl_add(&vq->vq_active_tree, zio);
557eda14cbcSMatt Macy }
558eda14cbcSMatt Macy 
559eda14cbcSMatt Macy static void
560eda14cbcSMatt Macy vdev_queue_pending_remove(vdev_queue_t *vq, zio_t *zio)
561eda14cbcSMatt Macy {
562eda14cbcSMatt Macy 	ASSERT(MUTEX_HELD(&vq->vq_lock));
563eda14cbcSMatt Macy 	ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
564eda14cbcSMatt Macy 	vq->vq_class[zio->io_priority].vqc_active--;
5657877fdebSMatt Macy 	if (vdev_queue_is_interactive(zio->io_priority)) {
5667877fdebSMatt Macy 		if (--vq->vq_ia_active == 0)
5677877fdebSMatt Macy 			vq->vq_nia_credit = 0;
5687877fdebSMatt Macy 		else
5697877fdebSMatt Macy 			vq->vq_nia_credit = zfs_vdev_nia_credit;
5707877fdebSMatt Macy 	} else if (vq->vq_ia_active == 0)
5717877fdebSMatt Macy 		vq->vq_nia_credit++;
572eda14cbcSMatt Macy 	avl_remove(&vq->vq_active_tree, zio);
573eda14cbcSMatt Macy }
574eda14cbcSMatt Macy 
575eda14cbcSMatt Macy static void
576eda14cbcSMatt Macy vdev_queue_agg_io_done(zio_t *aio)
577eda14cbcSMatt Macy {
578eda14cbcSMatt Macy 	abd_free(aio->io_abd);
579eda14cbcSMatt Macy }
580eda14cbcSMatt Macy 
581eda14cbcSMatt Macy /*
582eda14cbcSMatt Macy  * Compute the range spanned by two i/os, which is the endpoint of the last
583eda14cbcSMatt Macy  * (lio->io_offset + lio->io_size) minus start of the first (fio->io_offset).
584eda14cbcSMatt Macy  * Conveniently, the gap between fio and lio is given by -IO_SPAN(lio, fio);
585eda14cbcSMatt Macy  * thus fio and lio are adjacent if and only if IO_SPAN(lio, fio) == 0.
586eda14cbcSMatt Macy  */
587eda14cbcSMatt Macy #define	IO_SPAN(fio, lio) ((lio)->io_offset + (lio)->io_size - (fio)->io_offset)
588eda14cbcSMatt Macy #define	IO_GAP(fio, lio) (-IO_SPAN(lio, fio))
589eda14cbcSMatt Macy 
590eda14cbcSMatt Macy /*
591eda14cbcSMatt Macy  * Sufficiently adjacent io_offset's in ZIOs will be aggregated. We do this
592eda14cbcSMatt Macy  * by creating a gang ABD from the adjacent ZIOs io_abd's. By using
593eda14cbcSMatt Macy  * a gang ABD we avoid doing memory copies to and from the parent,
594eda14cbcSMatt Macy  * child ZIOs. The gang ABD also accounts for gaps between adjacent
595eda14cbcSMatt Macy  * io_offsets by simply getting the zero ABD for writes or allocating
596eda14cbcSMatt Macy  * a new ABD for reads and placing them in the gang ABD as well.
597eda14cbcSMatt Macy  */
598eda14cbcSMatt Macy static zio_t *
599eda14cbcSMatt Macy vdev_queue_aggregate(vdev_queue_t *vq, zio_t *zio)
600eda14cbcSMatt Macy {
601eda14cbcSMatt Macy 	zio_t *first, *last, *aio, *dio, *mandatory, *nio;
602eda14cbcSMatt Macy 	uint64_t maxgap = 0;
603eda14cbcSMatt Macy 	uint64_t size;
604eda14cbcSMatt Macy 	uint64_t limit;
605eda14cbcSMatt Macy 	int maxblocksize;
606eda14cbcSMatt Macy 	boolean_t stretch = B_FALSE;
607eda14cbcSMatt Macy 	avl_tree_t *t = vdev_queue_type_tree(vq, zio->io_type);
608dbd5678dSMartin Matuska 	zio_flag_t flags = zio->io_flags & ZIO_FLAG_AGG_INHERIT;
609eda14cbcSMatt Macy 	uint64_t next_offset;
610eda14cbcSMatt Macy 	abd_t *abd;
611eda14cbcSMatt Macy 
612eda14cbcSMatt Macy 	maxblocksize = spa_maxblocksize(vq->vq_vdev->vdev_spa);
613eda14cbcSMatt Macy 	if (vq->vq_vdev->vdev_nonrot)
614eda14cbcSMatt Macy 		limit = zfs_vdev_aggregation_limit_non_rotating;
615eda14cbcSMatt Macy 	else
616eda14cbcSMatt Macy 		limit = zfs_vdev_aggregation_limit;
617be181ee2SMartin Matuska 	limit = MIN(limit, maxblocksize);
618eda14cbcSMatt Macy 
619eda14cbcSMatt Macy 	if (zio->io_flags & ZIO_FLAG_DONT_AGGREGATE || limit == 0)
620eda14cbcSMatt Macy 		return (NULL);
621eda14cbcSMatt Macy 
622eda14cbcSMatt Macy 	/*
623eda14cbcSMatt Macy 	 * While TRIM commands could be aggregated based on offset this
624eda14cbcSMatt Macy 	 * behavior is disabled until it's determined to be beneficial.
625eda14cbcSMatt Macy 	 */
626eda14cbcSMatt Macy 	if (zio->io_type == ZIO_TYPE_TRIM && !zfs_vdev_aggregate_trim)
627eda14cbcSMatt Macy 		return (NULL);
628eda14cbcSMatt Macy 
6297877fdebSMatt Macy 	/*
6307877fdebSMatt Macy 	 * I/Os to distributed spares are directly dispatched to the dRAID
6317877fdebSMatt Macy 	 * leaf vdevs for aggregation.  See the comment at the end of the
6327877fdebSMatt Macy 	 * zio_vdev_io_start() function.
6337877fdebSMatt Macy 	 */
6347877fdebSMatt Macy 	ASSERT(vq->vq_vdev->vdev_ops != &vdev_draid_spare_ops);
6357877fdebSMatt Macy 
636eda14cbcSMatt Macy 	first = last = zio;
637eda14cbcSMatt Macy 
638eda14cbcSMatt Macy 	if (zio->io_type == ZIO_TYPE_READ)
639eda14cbcSMatt Macy 		maxgap = zfs_vdev_read_gap_limit;
640eda14cbcSMatt Macy 
641eda14cbcSMatt Macy 	/*
642eda14cbcSMatt Macy 	 * We can aggregate I/Os that are sufficiently adjacent and of
643eda14cbcSMatt Macy 	 * the same flavor, as expressed by the AGG_INHERIT flags.
644eda14cbcSMatt Macy 	 * The latter requirement is necessary so that certain
645eda14cbcSMatt Macy 	 * attributes of the I/O, such as whether it's a normal I/O
646eda14cbcSMatt Macy 	 * or a scrub/resilver, can be preserved in the aggregate.
647eda14cbcSMatt Macy 	 * We can include optional I/Os, but don't allow them
648eda14cbcSMatt Macy 	 * to begin a range as they add no benefit in that situation.
649eda14cbcSMatt Macy 	 */
650eda14cbcSMatt Macy 
651eda14cbcSMatt Macy 	/*
652eda14cbcSMatt Macy 	 * We keep track of the last non-optional I/O.
653eda14cbcSMatt Macy 	 */
654eda14cbcSMatt Macy 	mandatory = (first->io_flags & ZIO_FLAG_OPTIONAL) ? NULL : first;
655eda14cbcSMatt Macy 
656eda14cbcSMatt Macy 	/*
657eda14cbcSMatt Macy 	 * Walk backwards through sufficiently contiguous I/Os
658eda14cbcSMatt Macy 	 * recording the last non-optional I/O.
659eda14cbcSMatt Macy 	 */
660eda14cbcSMatt Macy 	while ((dio = AVL_PREV(t, first)) != NULL &&
661eda14cbcSMatt Macy 	    (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags &&
662eda14cbcSMatt Macy 	    IO_SPAN(dio, last) <= limit &&
663eda14cbcSMatt Macy 	    IO_GAP(dio, first) <= maxgap &&
664eda14cbcSMatt Macy 	    dio->io_type == zio->io_type) {
665eda14cbcSMatt Macy 		first = dio;
666eda14cbcSMatt Macy 		if (mandatory == NULL && !(first->io_flags & ZIO_FLAG_OPTIONAL))
667eda14cbcSMatt Macy 			mandatory = first;
668eda14cbcSMatt Macy 	}
669eda14cbcSMatt Macy 
670eda14cbcSMatt Macy 	/*
671eda14cbcSMatt Macy 	 * Skip any initial optional I/Os.
672eda14cbcSMatt Macy 	 */
673eda14cbcSMatt Macy 	while ((first->io_flags & ZIO_FLAG_OPTIONAL) && first != last) {
674eda14cbcSMatt Macy 		first = AVL_NEXT(t, first);
675eda14cbcSMatt Macy 		ASSERT(first != NULL);
676eda14cbcSMatt Macy 	}
677eda14cbcSMatt Macy 
678eda14cbcSMatt Macy 
679eda14cbcSMatt Macy 	/*
680eda14cbcSMatt Macy 	 * Walk forward through sufficiently contiguous I/Os.
681eda14cbcSMatt Macy 	 * The aggregation limit does not apply to optional i/os, so that
682eda14cbcSMatt Macy 	 * we can issue contiguous writes even if they are larger than the
683eda14cbcSMatt Macy 	 * aggregation limit.
684eda14cbcSMatt Macy 	 */
685eda14cbcSMatt Macy 	while ((dio = AVL_NEXT(t, last)) != NULL &&
686eda14cbcSMatt Macy 	    (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags &&
687eda14cbcSMatt Macy 	    (IO_SPAN(first, dio) <= limit ||
688eda14cbcSMatt Macy 	    (dio->io_flags & ZIO_FLAG_OPTIONAL)) &&
689eda14cbcSMatt Macy 	    IO_SPAN(first, dio) <= maxblocksize &&
690eda14cbcSMatt Macy 	    IO_GAP(last, dio) <= maxgap &&
691eda14cbcSMatt Macy 	    dio->io_type == zio->io_type) {
692eda14cbcSMatt Macy 		last = dio;
693eda14cbcSMatt Macy 		if (!(last->io_flags & ZIO_FLAG_OPTIONAL))
694eda14cbcSMatt Macy 			mandatory = last;
695eda14cbcSMatt Macy 	}
696eda14cbcSMatt Macy 
697eda14cbcSMatt Macy 	/*
698eda14cbcSMatt Macy 	 * Now that we've established the range of the I/O aggregation
699eda14cbcSMatt Macy 	 * we must decide what to do with trailing optional I/Os.
700eda14cbcSMatt Macy 	 * For reads, there's nothing to do. While we are unable to
701eda14cbcSMatt Macy 	 * aggregate further, it's possible that a trailing optional
702eda14cbcSMatt Macy 	 * I/O would allow the underlying device to aggregate with
703eda14cbcSMatt Macy 	 * subsequent I/Os. We must therefore determine if the next
704eda14cbcSMatt Macy 	 * non-optional I/O is close enough to make aggregation
705eda14cbcSMatt Macy 	 * worthwhile.
706eda14cbcSMatt Macy 	 */
707eda14cbcSMatt Macy 	if (zio->io_type == ZIO_TYPE_WRITE && mandatory != NULL) {
708eda14cbcSMatt Macy 		zio_t *nio = last;
709eda14cbcSMatt Macy 		while ((dio = AVL_NEXT(t, nio)) != NULL &&
710eda14cbcSMatt Macy 		    IO_GAP(nio, dio) == 0 &&
711eda14cbcSMatt Macy 		    IO_GAP(mandatory, dio) <= zfs_vdev_write_gap_limit) {
712eda14cbcSMatt Macy 			nio = dio;
713eda14cbcSMatt Macy 			if (!(nio->io_flags & ZIO_FLAG_OPTIONAL)) {
714eda14cbcSMatt Macy 				stretch = B_TRUE;
715eda14cbcSMatt Macy 				break;
716eda14cbcSMatt Macy 			}
717eda14cbcSMatt Macy 		}
718eda14cbcSMatt Macy 	}
719eda14cbcSMatt Macy 
720eda14cbcSMatt Macy 	if (stretch) {
721eda14cbcSMatt Macy 		/*
722eda14cbcSMatt Macy 		 * We are going to include an optional io in our aggregated
723eda14cbcSMatt Macy 		 * span, thus closing the write gap.  Only mandatory i/os can
724eda14cbcSMatt Macy 		 * start aggregated spans, so make sure that the next i/o
725eda14cbcSMatt Macy 		 * after our span is mandatory.
726eda14cbcSMatt Macy 		 */
727eda14cbcSMatt Macy 		dio = AVL_NEXT(t, last);
728dbd5678dSMartin Matuska 		ASSERT3P(dio, !=, NULL);
729eda14cbcSMatt Macy 		dio->io_flags &= ~ZIO_FLAG_OPTIONAL;
730eda14cbcSMatt Macy 	} else {
731eda14cbcSMatt Macy 		/* do not include the optional i/o */
732eda14cbcSMatt Macy 		while (last != mandatory && last != first) {
733eda14cbcSMatt Macy 			ASSERT(last->io_flags & ZIO_FLAG_OPTIONAL);
734eda14cbcSMatt Macy 			last = AVL_PREV(t, last);
735eda14cbcSMatt Macy 			ASSERT(last != NULL);
736eda14cbcSMatt Macy 		}
737eda14cbcSMatt Macy 	}
738eda14cbcSMatt Macy 
739eda14cbcSMatt Macy 	if (first == last)
740eda14cbcSMatt Macy 		return (NULL);
741eda14cbcSMatt Macy 
742eda14cbcSMatt Macy 	size = IO_SPAN(first, last);
743eda14cbcSMatt Macy 	ASSERT3U(size, <=, maxblocksize);
744eda14cbcSMatt Macy 
745184c1b94SMartin Matuska 	abd = abd_alloc_gang();
746eda14cbcSMatt Macy 	if (abd == NULL)
747eda14cbcSMatt Macy 		return (NULL);
748eda14cbcSMatt Macy 
749eda14cbcSMatt Macy 	aio = zio_vdev_delegated_io(first->io_vd, first->io_offset,
750eda14cbcSMatt Macy 	    abd, size, first->io_type, zio->io_priority,
751*4e8d558cSMartin Matuska 	    flags | ZIO_FLAG_DONT_QUEUE, vdev_queue_agg_io_done, NULL);
752eda14cbcSMatt Macy 	aio->io_timestamp = first->io_timestamp;
753eda14cbcSMatt Macy 
754eda14cbcSMatt Macy 	nio = first;
755eda14cbcSMatt Macy 	next_offset = first->io_offset;
756eda14cbcSMatt Macy 	do {
757eda14cbcSMatt Macy 		dio = nio;
758eda14cbcSMatt Macy 		nio = AVL_NEXT(t, dio);
759dbd5678dSMartin Matuska 		ASSERT3P(dio, !=, NULL);
760eda14cbcSMatt Macy 		zio_add_child(dio, aio);
761eda14cbcSMatt Macy 		vdev_queue_io_remove(vq, dio);
762eda14cbcSMatt Macy 
763eda14cbcSMatt Macy 		if (dio->io_offset != next_offset) {
764eda14cbcSMatt Macy 			/* allocate a buffer for a read gap */
765eda14cbcSMatt Macy 			ASSERT3U(dio->io_type, ==, ZIO_TYPE_READ);
766eda14cbcSMatt Macy 			ASSERT3U(dio->io_offset, >, next_offset);
767eda14cbcSMatt Macy 			abd = abd_alloc_for_io(
768eda14cbcSMatt Macy 			    dio->io_offset - next_offset, B_TRUE);
769eda14cbcSMatt Macy 			abd_gang_add(aio->io_abd, abd, B_TRUE);
770eda14cbcSMatt Macy 		}
771eda14cbcSMatt Macy 		if (dio->io_abd &&
772eda14cbcSMatt Macy 		    (dio->io_size != abd_get_size(dio->io_abd))) {
773eda14cbcSMatt Macy 			/* abd size not the same as IO size */
774eda14cbcSMatt Macy 			ASSERT3U(abd_get_size(dio->io_abd), >, dio->io_size);
775eda14cbcSMatt Macy 			abd = abd_get_offset_size(dio->io_abd, 0, dio->io_size);
776eda14cbcSMatt Macy 			abd_gang_add(aio->io_abd, abd, B_TRUE);
777eda14cbcSMatt Macy 		} else {
778eda14cbcSMatt Macy 			if (dio->io_flags & ZIO_FLAG_NODATA) {
779eda14cbcSMatt Macy 				/* allocate a buffer for a write gap */
780eda14cbcSMatt Macy 				ASSERT3U(dio->io_type, ==, ZIO_TYPE_WRITE);
781eda14cbcSMatt Macy 				ASSERT3P(dio->io_abd, ==, NULL);
782eda14cbcSMatt Macy 				abd_gang_add(aio->io_abd,
783eda14cbcSMatt Macy 				    abd_get_zeros(dio->io_size), B_TRUE);
784eda14cbcSMatt Macy 			} else {
785eda14cbcSMatt Macy 				/*
786eda14cbcSMatt Macy 				 * We pass B_FALSE to abd_gang_add()
787eda14cbcSMatt Macy 				 * because we did not allocate a new
788eda14cbcSMatt Macy 				 * ABD, so it is assumed the caller
789eda14cbcSMatt Macy 				 * will free this ABD.
790eda14cbcSMatt Macy 				 */
791eda14cbcSMatt Macy 				abd_gang_add(aio->io_abd, dio->io_abd,
792eda14cbcSMatt Macy 				    B_FALSE);
793eda14cbcSMatt Macy 			}
794eda14cbcSMatt Macy 		}
795eda14cbcSMatt Macy 		next_offset = dio->io_offset + dio->io_size;
796eda14cbcSMatt Macy 	} while (dio != last);
797eda14cbcSMatt Macy 	ASSERT3U(abd_get_size(aio->io_abd), ==, aio->io_size);
798eda14cbcSMatt Macy 
799eda14cbcSMatt Macy 	/*
8002faf504dSMartin Matuska 	 * Callers must call zio_vdev_io_bypass() and zio_execute() for
8012faf504dSMartin Matuska 	 * aggregated (parent) I/Os so that we could avoid dropping the
8022faf504dSMartin Matuska 	 * queue's lock here to avoid a deadlock that we could encounter
8032faf504dSMartin Matuska 	 * due to lock order reversal between vq_lock and io_lock in
8042faf504dSMartin Matuska 	 * zio_change_priority().
805eda14cbcSMatt Macy 	 */
806eda14cbcSMatt Macy 	return (aio);
807eda14cbcSMatt Macy }
808eda14cbcSMatt Macy 
809eda14cbcSMatt Macy static zio_t *
810eda14cbcSMatt Macy vdev_queue_io_to_issue(vdev_queue_t *vq)
811eda14cbcSMatt Macy {
812eda14cbcSMatt Macy 	zio_t *zio, *aio;
813eda14cbcSMatt Macy 	zio_priority_t p;
814eda14cbcSMatt Macy 	avl_index_t idx;
815eda14cbcSMatt Macy 	avl_tree_t *tree;
816eda14cbcSMatt Macy 
817eda14cbcSMatt Macy again:
818eda14cbcSMatt Macy 	ASSERT(MUTEX_HELD(&vq->vq_lock));
819eda14cbcSMatt Macy 
820eda14cbcSMatt Macy 	p = vdev_queue_class_to_issue(vq);
821eda14cbcSMatt Macy 
822eda14cbcSMatt Macy 	if (p == ZIO_PRIORITY_NUM_QUEUEABLE) {
823eda14cbcSMatt Macy 		/* No eligible queued i/os */
824eda14cbcSMatt Macy 		return (NULL);
825eda14cbcSMatt Macy 	}
826eda14cbcSMatt Macy 
827eda14cbcSMatt Macy 	/*
828eda14cbcSMatt Macy 	 * For LBA-ordered queues (async / scrub / initializing), issue the
829eda14cbcSMatt Macy 	 * i/o which follows the most recently issued i/o in LBA (offset) order.
830eda14cbcSMatt Macy 	 *
831eda14cbcSMatt Macy 	 * For FIFO queues (sync/trim), issue the i/o with the lowest timestamp.
832eda14cbcSMatt Macy 	 */
833eda14cbcSMatt Macy 	tree = vdev_queue_class_tree(vq, p);
834eda14cbcSMatt Macy 	vq->vq_io_search.io_timestamp = 0;
835eda14cbcSMatt Macy 	vq->vq_io_search.io_offset = vq->vq_last_offset - 1;
836eda14cbcSMatt Macy 	VERIFY3P(avl_find(tree, &vq->vq_io_search, &idx), ==, NULL);
837eda14cbcSMatt Macy 	zio = avl_nearest(tree, idx, AVL_AFTER);
838eda14cbcSMatt Macy 	if (zio == NULL)
839eda14cbcSMatt Macy 		zio = avl_first(tree);
840eda14cbcSMatt Macy 	ASSERT3U(zio->io_priority, ==, p);
841eda14cbcSMatt Macy 
842eda14cbcSMatt Macy 	aio = vdev_queue_aggregate(vq, zio);
8432faf504dSMartin Matuska 	if (aio != NULL) {
844eda14cbcSMatt Macy 		zio = aio;
8452faf504dSMartin Matuska 	} else {
846eda14cbcSMatt Macy 		vdev_queue_io_remove(vq, zio);
847eda14cbcSMatt Macy 
848eda14cbcSMatt Macy 		/*
8492faf504dSMartin Matuska 		 * If the I/O is or was optional and therefore has no data, we
8502faf504dSMartin Matuska 		 * need to simply discard it. We need to drop the vdev queue's
8512faf504dSMartin Matuska 		 * lock to avoid a deadlock that we could encounter since this
8522faf504dSMartin Matuska 		 * I/O will complete immediately.
853eda14cbcSMatt Macy 		 */
854eda14cbcSMatt Macy 		if (zio->io_flags & ZIO_FLAG_NODATA) {
855eda14cbcSMatt Macy 			mutex_exit(&vq->vq_lock);
856eda14cbcSMatt Macy 			zio_vdev_io_bypass(zio);
857eda14cbcSMatt Macy 			zio_execute(zio);
858eda14cbcSMatt Macy 			mutex_enter(&vq->vq_lock);
859eda14cbcSMatt Macy 			goto again;
860eda14cbcSMatt Macy 		}
8612faf504dSMartin Matuska 	}
862eda14cbcSMatt Macy 
863eda14cbcSMatt Macy 	vdev_queue_pending_add(vq, zio);
864eda14cbcSMatt Macy 	vq->vq_last_offset = zio->io_offset + zio->io_size;
865eda14cbcSMatt Macy 
866eda14cbcSMatt Macy 	return (zio);
867eda14cbcSMatt Macy }
868eda14cbcSMatt Macy 
869eda14cbcSMatt Macy zio_t *
870eda14cbcSMatt Macy vdev_queue_io(zio_t *zio)
871eda14cbcSMatt Macy {
872eda14cbcSMatt Macy 	vdev_queue_t *vq = &zio->io_vd->vdev_queue;
8732faf504dSMartin Matuska 	zio_t *dio, *nio;
8742faf504dSMartin Matuska 	zio_link_t *zl = NULL;
875eda14cbcSMatt Macy 
876eda14cbcSMatt Macy 	if (zio->io_flags & ZIO_FLAG_DONT_QUEUE)
877eda14cbcSMatt Macy 		return (zio);
878eda14cbcSMatt Macy 
879eda14cbcSMatt Macy 	/*
880eda14cbcSMatt Macy 	 * Children i/os inherent their parent's priority, which might
881eda14cbcSMatt Macy 	 * not match the child's i/o type.  Fix it up here.
882eda14cbcSMatt Macy 	 */
883eda14cbcSMatt Macy 	if (zio->io_type == ZIO_TYPE_READ) {
884eda14cbcSMatt Macy 		ASSERT(zio->io_priority != ZIO_PRIORITY_TRIM);
885eda14cbcSMatt Macy 
886eda14cbcSMatt Macy 		if (zio->io_priority != ZIO_PRIORITY_SYNC_READ &&
887eda14cbcSMatt Macy 		    zio->io_priority != ZIO_PRIORITY_ASYNC_READ &&
888eda14cbcSMatt Macy 		    zio->io_priority != ZIO_PRIORITY_SCRUB &&
889eda14cbcSMatt Macy 		    zio->io_priority != ZIO_PRIORITY_REMOVAL &&
890eda14cbcSMatt Macy 		    zio->io_priority != ZIO_PRIORITY_INITIALIZING &&
891eda14cbcSMatt Macy 		    zio->io_priority != ZIO_PRIORITY_REBUILD) {
892eda14cbcSMatt Macy 			zio->io_priority = ZIO_PRIORITY_ASYNC_READ;
893eda14cbcSMatt Macy 		}
894eda14cbcSMatt Macy 	} else if (zio->io_type == ZIO_TYPE_WRITE) {
895eda14cbcSMatt Macy 		ASSERT(zio->io_priority != ZIO_PRIORITY_TRIM);
896eda14cbcSMatt Macy 
897eda14cbcSMatt Macy 		if (zio->io_priority != ZIO_PRIORITY_SYNC_WRITE &&
898eda14cbcSMatt Macy 		    zio->io_priority != ZIO_PRIORITY_ASYNC_WRITE &&
899eda14cbcSMatt Macy 		    zio->io_priority != ZIO_PRIORITY_REMOVAL &&
900eda14cbcSMatt Macy 		    zio->io_priority != ZIO_PRIORITY_INITIALIZING &&
901eda14cbcSMatt Macy 		    zio->io_priority != ZIO_PRIORITY_REBUILD) {
902eda14cbcSMatt Macy 			zio->io_priority = ZIO_PRIORITY_ASYNC_WRITE;
903eda14cbcSMatt Macy 		}
904eda14cbcSMatt Macy 	} else {
905eda14cbcSMatt Macy 		ASSERT(zio->io_type == ZIO_TYPE_TRIM);
906eda14cbcSMatt Macy 		ASSERT(zio->io_priority == ZIO_PRIORITY_TRIM);
907eda14cbcSMatt Macy 	}
908eda14cbcSMatt Macy 
909*4e8d558cSMartin Matuska 	zio->io_flags |= ZIO_FLAG_DONT_QUEUE;
9107cd22ac4SMartin Matuska 	zio->io_timestamp = gethrtime();
911eda14cbcSMatt Macy 
912eda14cbcSMatt Macy 	mutex_enter(&vq->vq_lock);
913eda14cbcSMatt Macy 	vdev_queue_io_add(vq, zio);
914eda14cbcSMatt Macy 	nio = vdev_queue_io_to_issue(vq);
915eda14cbcSMatt Macy 	mutex_exit(&vq->vq_lock);
916eda14cbcSMatt Macy 
917eda14cbcSMatt Macy 	if (nio == NULL)
918eda14cbcSMatt Macy 		return (NULL);
919eda14cbcSMatt Macy 
920eda14cbcSMatt Macy 	if (nio->io_done == vdev_queue_agg_io_done) {
9212faf504dSMartin Matuska 		while ((dio = zio_walk_parents(nio, &zl)) != NULL) {
9222faf504dSMartin Matuska 			ASSERT3U(dio->io_type, ==, nio->io_type);
9232faf504dSMartin Matuska 			zio_vdev_io_bypass(dio);
9242faf504dSMartin Matuska 			zio_execute(dio);
9252faf504dSMartin Matuska 		}
926eda14cbcSMatt Macy 		zio_nowait(nio);
927eda14cbcSMatt Macy 		return (NULL);
928eda14cbcSMatt Macy 	}
929eda14cbcSMatt Macy 
930eda14cbcSMatt Macy 	return (nio);
931eda14cbcSMatt Macy }
932eda14cbcSMatt Macy 
933eda14cbcSMatt Macy void
934eda14cbcSMatt Macy vdev_queue_io_done(zio_t *zio)
935eda14cbcSMatt Macy {
936eda14cbcSMatt Macy 	vdev_queue_t *vq = &zio->io_vd->vdev_queue;
9372faf504dSMartin Matuska 	zio_t *dio, *nio;
9382faf504dSMartin Matuska 	zio_link_t *zl = NULL;
939eda14cbcSMatt Macy 
9407cd22ac4SMartin Matuska 	hrtime_t now = gethrtime();
9417cd22ac4SMartin Matuska 	vq->vq_io_complete_ts = now;
9427cd22ac4SMartin Matuska 	vq->vq_io_delta_ts = zio->io_delta = now - zio->io_timestamp;
9437cd22ac4SMartin Matuska 
944eda14cbcSMatt Macy 	mutex_enter(&vq->vq_lock);
945eda14cbcSMatt Macy 	vdev_queue_pending_remove(vq, zio);
946eda14cbcSMatt Macy 
947eda14cbcSMatt Macy 	while ((nio = vdev_queue_io_to_issue(vq)) != NULL) {
948eda14cbcSMatt Macy 		mutex_exit(&vq->vq_lock);
949eda14cbcSMatt Macy 		if (nio->io_done == vdev_queue_agg_io_done) {
9502faf504dSMartin Matuska 			while ((dio = zio_walk_parents(nio, &zl)) != NULL) {
9512faf504dSMartin Matuska 				ASSERT3U(dio->io_type, ==, nio->io_type);
9522faf504dSMartin Matuska 				zio_vdev_io_bypass(dio);
9532faf504dSMartin Matuska 				zio_execute(dio);
9542faf504dSMartin Matuska 			}
955eda14cbcSMatt Macy 			zio_nowait(nio);
956eda14cbcSMatt Macy 		} else {
957eda14cbcSMatt Macy 			zio_vdev_io_reissue(nio);
958eda14cbcSMatt Macy 			zio_execute(nio);
959eda14cbcSMatt Macy 		}
960eda14cbcSMatt Macy 		mutex_enter(&vq->vq_lock);
961eda14cbcSMatt Macy 	}
962eda14cbcSMatt Macy 
963eda14cbcSMatt Macy 	mutex_exit(&vq->vq_lock);
964eda14cbcSMatt Macy }
965eda14cbcSMatt Macy 
966eda14cbcSMatt Macy void
967eda14cbcSMatt Macy vdev_queue_change_io_priority(zio_t *zio, zio_priority_t priority)
968eda14cbcSMatt Macy {
969eda14cbcSMatt Macy 	vdev_queue_t *vq = &zio->io_vd->vdev_queue;
970eda14cbcSMatt Macy 	avl_tree_t *tree;
971eda14cbcSMatt Macy 
972eda14cbcSMatt Macy 	/*
973eda14cbcSMatt Macy 	 * ZIO_PRIORITY_NOW is used by the vdev cache code and the aggregate zio
974eda14cbcSMatt Macy 	 * code to issue IOs without adding them to the vdev queue. In this
975eda14cbcSMatt Macy 	 * case, the zio is already going to be issued as quickly as possible
976eda14cbcSMatt Macy 	 * and so it doesn't need any reprioritization to help.
977eda14cbcSMatt Macy 	 */
978eda14cbcSMatt Macy 	if (zio->io_priority == ZIO_PRIORITY_NOW)
979eda14cbcSMatt Macy 		return;
980eda14cbcSMatt Macy 
981eda14cbcSMatt Macy 	ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
982eda14cbcSMatt Macy 	ASSERT3U(priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
983eda14cbcSMatt Macy 
984eda14cbcSMatt Macy 	if (zio->io_type == ZIO_TYPE_READ) {
985eda14cbcSMatt Macy 		if (priority != ZIO_PRIORITY_SYNC_READ &&
986eda14cbcSMatt Macy 		    priority != ZIO_PRIORITY_ASYNC_READ &&
987eda14cbcSMatt Macy 		    priority != ZIO_PRIORITY_SCRUB)
988eda14cbcSMatt Macy 			priority = ZIO_PRIORITY_ASYNC_READ;
989eda14cbcSMatt Macy 	} else {
990eda14cbcSMatt Macy 		ASSERT(zio->io_type == ZIO_TYPE_WRITE);
991eda14cbcSMatt Macy 		if (priority != ZIO_PRIORITY_SYNC_WRITE &&
992eda14cbcSMatt Macy 		    priority != ZIO_PRIORITY_ASYNC_WRITE)
993eda14cbcSMatt Macy 			priority = ZIO_PRIORITY_ASYNC_WRITE;
994eda14cbcSMatt Macy 	}
995eda14cbcSMatt Macy 
996eda14cbcSMatt Macy 	mutex_enter(&vq->vq_lock);
997eda14cbcSMatt Macy 
998eda14cbcSMatt Macy 	/*
999eda14cbcSMatt Macy 	 * If the zio is in none of the queues we can simply change
1000eda14cbcSMatt Macy 	 * the priority. If the zio is waiting to be submitted we must
1001eda14cbcSMatt Macy 	 * remove it from the queue and re-insert it with the new priority.
1002eda14cbcSMatt Macy 	 * Otherwise, the zio is currently active and we cannot change its
1003eda14cbcSMatt Macy 	 * priority.
1004eda14cbcSMatt Macy 	 */
1005eda14cbcSMatt Macy 	tree = vdev_queue_class_tree(vq, zio->io_priority);
1006eda14cbcSMatt Macy 	if (avl_find(tree, zio, NULL) == zio) {
1007eda14cbcSMatt Macy 		avl_remove(vdev_queue_class_tree(vq, zio->io_priority), zio);
1008eda14cbcSMatt Macy 		zio->io_priority = priority;
1009eda14cbcSMatt Macy 		avl_add(vdev_queue_class_tree(vq, zio->io_priority), zio);
1010eda14cbcSMatt Macy 	} else if (avl_find(&vq->vq_active_tree, zio, NULL) != zio) {
1011eda14cbcSMatt Macy 		zio->io_priority = priority;
1012eda14cbcSMatt Macy 	}
1013eda14cbcSMatt Macy 
1014eda14cbcSMatt Macy 	mutex_exit(&vq->vq_lock);
1015eda14cbcSMatt Macy }
1016eda14cbcSMatt Macy 
1017eda14cbcSMatt Macy /*
1018eda14cbcSMatt Macy  * As these two methods are only used for load calculations we're not
1019eda14cbcSMatt Macy  * concerned if we get an incorrect value on 32bit platforms due to lack of
1020eda14cbcSMatt Macy  * vq_lock mutex use here, instead we prefer to keep it lock free for
1021eda14cbcSMatt Macy  * performance.
1022eda14cbcSMatt Macy  */
1023eda14cbcSMatt Macy int
1024eda14cbcSMatt Macy vdev_queue_length(vdev_t *vd)
1025eda14cbcSMatt Macy {
1026eda14cbcSMatt Macy 	return (avl_numnodes(&vd->vdev_queue.vq_active_tree));
1027eda14cbcSMatt Macy }
1028eda14cbcSMatt Macy 
1029eda14cbcSMatt Macy uint64_t
1030eda14cbcSMatt Macy vdev_queue_last_offset(vdev_t *vd)
1031eda14cbcSMatt Macy {
1032eda14cbcSMatt Macy 	return (vd->vdev_queue.vq_last_offset);
1033eda14cbcSMatt Macy }
1034eda14cbcSMatt Macy 
1035be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, aggregation_limit, UINT, ZMOD_RW,
1036eda14cbcSMatt Macy 	"Max vdev I/O aggregation size");
1037eda14cbcSMatt Macy 
1038be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, aggregation_limit_non_rotating, UINT,
1039c03c5b1cSMartin Matuska 	ZMOD_RW, "Max vdev I/O aggregation size for non-rotating media");
1040eda14cbcSMatt Macy 
1041be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, aggregate_trim, UINT, ZMOD_RW,
1042eda14cbcSMatt Macy 	"Allow TRIM I/O to be aggregated");
1043eda14cbcSMatt Macy 
1044be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, read_gap_limit, UINT, ZMOD_RW,
1045eda14cbcSMatt Macy 	"Aggregate read I/O over gap");
1046eda14cbcSMatt Macy 
1047be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, write_gap_limit, UINT, ZMOD_RW,
1048eda14cbcSMatt Macy 	"Aggregate write I/O over gap");
1049eda14cbcSMatt Macy 
1050be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, max_active, UINT, ZMOD_RW,
1051eda14cbcSMatt Macy 	"Maximum number of active I/Os per vdev");
1052eda14cbcSMatt Macy 
1053be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, async_write_active_max_dirty_percent,
1054be181ee2SMartin Matuska 	UINT, ZMOD_RW, "Async write concurrency max threshold");
1055eda14cbcSMatt Macy 
1056be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, async_write_active_min_dirty_percent,
1057be181ee2SMartin Matuska 	UINT, ZMOD_RW, "Async write concurrency min threshold");
1058eda14cbcSMatt Macy 
1059be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, async_read_max_active, UINT, ZMOD_RW,
1060eda14cbcSMatt Macy 	"Max active async read I/Os per vdev");
1061eda14cbcSMatt Macy 
1062be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, async_read_min_active, UINT, ZMOD_RW,
1063eda14cbcSMatt Macy 	"Min active async read I/Os per vdev");
1064eda14cbcSMatt Macy 
1065be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, async_write_max_active, UINT, ZMOD_RW,
1066eda14cbcSMatt Macy 	"Max active async write I/Os per vdev");
1067eda14cbcSMatt Macy 
1068be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, async_write_min_active, UINT, ZMOD_RW,
1069eda14cbcSMatt Macy 	"Min active async write I/Os per vdev");
1070eda14cbcSMatt Macy 
1071be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, initializing_max_active, UINT, ZMOD_RW,
1072eda14cbcSMatt Macy 	"Max active initializing I/Os per vdev");
1073eda14cbcSMatt Macy 
1074be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, initializing_min_active, UINT, ZMOD_RW,
1075eda14cbcSMatt Macy 	"Min active initializing I/Os per vdev");
1076eda14cbcSMatt Macy 
1077be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, removal_max_active, UINT, ZMOD_RW,
1078eda14cbcSMatt Macy 	"Max active removal I/Os per vdev");
1079eda14cbcSMatt Macy 
1080be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, removal_min_active, UINT, ZMOD_RW,
1081eda14cbcSMatt Macy 	"Min active removal I/Os per vdev");
1082eda14cbcSMatt Macy 
1083be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, scrub_max_active, UINT, ZMOD_RW,
1084eda14cbcSMatt Macy 	"Max active scrub I/Os per vdev");
1085eda14cbcSMatt Macy 
1086be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, scrub_min_active, UINT, ZMOD_RW,
1087eda14cbcSMatt Macy 	"Min active scrub I/Os per vdev");
1088eda14cbcSMatt Macy 
1089be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, sync_read_max_active, UINT, ZMOD_RW,
1090eda14cbcSMatt Macy 	"Max active sync read I/Os per vdev");
1091eda14cbcSMatt Macy 
1092be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, sync_read_min_active, UINT, ZMOD_RW,
1093eda14cbcSMatt Macy 	"Min active sync read I/Os per vdev");
1094eda14cbcSMatt Macy 
1095be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, sync_write_max_active, UINT, ZMOD_RW,
1096eda14cbcSMatt Macy 	"Max active sync write I/Os per vdev");
1097eda14cbcSMatt Macy 
1098be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, sync_write_min_active, UINT, ZMOD_RW,
1099eda14cbcSMatt Macy 	"Min active sync write I/Os per vdev");
1100eda14cbcSMatt Macy 
1101be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, trim_max_active, UINT, ZMOD_RW,
1102eda14cbcSMatt Macy 	"Max active trim/discard I/Os per vdev");
1103eda14cbcSMatt Macy 
1104be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, trim_min_active, UINT, ZMOD_RW,
1105eda14cbcSMatt Macy 	"Min active trim/discard I/Os per vdev");
1106eda14cbcSMatt Macy 
1107be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, rebuild_max_active, UINT, ZMOD_RW,
1108eda14cbcSMatt Macy 	"Max active rebuild I/Os per vdev");
1109eda14cbcSMatt Macy 
1110be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, rebuild_min_active, UINT, ZMOD_RW,
1111eda14cbcSMatt Macy 	"Min active rebuild I/Os per vdev");
1112eda14cbcSMatt Macy 
1113be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, nia_credit, UINT, ZMOD_RW,
11147877fdebSMatt Macy 	"Number of non-interactive I/Os to allow in sequence");
11157877fdebSMatt Macy 
1116be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, nia_delay, UINT, ZMOD_RW,
11177877fdebSMatt Macy 	"Number of non-interactive I/Os before _max_active");
11187877fdebSMatt Macy 
1119be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, queue_depth_pct, UINT, ZMOD_RW,
1120eda14cbcSMatt Macy 	"Queue depth percentage for each top-level vdev");
1121d411c1d6SMartin Matuska 
1122d411c1d6SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, def_queue_depth, UINT, ZMOD_RW,
1123d411c1d6SMartin Matuska 	"Default queue depth for each allocator");
1124