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