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