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