1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy * CDDL HEADER START
3eda14cbcSMatt Macy *
4eda14cbcSMatt Macy * The contents of this file are subject to the terms of the
5eda14cbcSMatt Macy * Common Development and Distribution License (the "License").
6eda14cbcSMatt Macy * You may not use this file except in compliance with the License.
7eda14cbcSMatt Macy *
8eda14cbcSMatt Macy * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9271171e0SMartin Matuska * or https://opensource.org/licenses/CDDL-1.0.
10eda14cbcSMatt Macy * See the License for the specific language governing permissions
11eda14cbcSMatt Macy * and limitations under the License.
12eda14cbcSMatt Macy *
13eda14cbcSMatt Macy * When distributing Covered Code, include this CDDL HEADER in each
14eda14cbcSMatt Macy * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15eda14cbcSMatt Macy * If applicable, add the following below this CDDL HEADER, with the
16eda14cbcSMatt Macy * fields enclosed by brackets "[]" replaced with your own identifying
17eda14cbcSMatt Macy * information: Portions Copyright [yyyy] [name of copyright owner]
18eda14cbcSMatt Macy *
19eda14cbcSMatt Macy * CDDL HEADER END
20eda14cbcSMatt Macy */
21eda14cbcSMatt Macy /*
22eda14cbcSMatt Macy * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23eda14cbcSMatt Macy * Use is subject to license terms.
24eda14cbcSMatt Macy */
25eda14cbcSMatt Macy
26eda14cbcSMatt Macy /*
27eda14cbcSMatt Macy * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
28eda14cbcSMatt Macy */
29eda14cbcSMatt Macy
30eda14cbcSMatt Macy #include <sys/zfs_context.h>
31eda14cbcSMatt Macy #include <sys/vdev_impl.h>
32eda14cbcSMatt Macy #include <sys/spa_impl.h>
33eda14cbcSMatt Macy #include <sys/zio.h>
34eda14cbcSMatt Macy #include <sys/avl.h>
35eda14cbcSMatt Macy #include <sys/dsl_pool.h>
36eda14cbcSMatt Macy #include <sys/metaslab_impl.h>
37eda14cbcSMatt Macy #include <sys/spa.h>
38eda14cbcSMatt Macy #include <sys/abd.h>
39eda14cbcSMatt Macy
40eda14cbcSMatt Macy /*
41eda14cbcSMatt Macy * ZFS I/O Scheduler
42eda14cbcSMatt Macy * ---------------
43eda14cbcSMatt Macy *
44eda14cbcSMatt Macy * ZFS issues I/O operations to leaf vdevs to satisfy and complete zios. The
45eda14cbcSMatt Macy * I/O scheduler determines when and in what order those operations are
46eda14cbcSMatt Macy * issued. The I/O scheduler divides operations into five I/O classes
47eda14cbcSMatt Macy * prioritized in the following order: sync read, sync write, async read,
48eda14cbcSMatt Macy * async write, and scrub/resilver. Each queue defines the minimum and
49eda14cbcSMatt Macy * maximum number of concurrent operations that may be issued to the device.
50eda14cbcSMatt Macy * In addition, the device has an aggregate maximum. Note that the sum of the
51eda14cbcSMatt Macy * per-queue minimums must not exceed the aggregate maximum. If the
52eda14cbcSMatt Macy * sum of the per-queue maximums exceeds the aggregate maximum, then the
53eda14cbcSMatt Macy * number of active i/os may reach zfs_vdev_max_active, in which case no
54eda14cbcSMatt Macy * further i/os will be issued regardless of whether all per-queue
55eda14cbcSMatt Macy * minimums have been met.
56eda14cbcSMatt Macy *
57eda14cbcSMatt Macy * For many physical devices, throughput increases with the number of
58eda14cbcSMatt Macy * concurrent operations, but latency typically suffers. Further, physical
59eda14cbcSMatt Macy * devices typically have a limit at which more concurrent operations have no
60eda14cbcSMatt Macy * effect on throughput or can actually cause it to decrease.
61eda14cbcSMatt Macy *
62eda14cbcSMatt Macy * The scheduler selects the next operation to issue by first looking for an
63eda14cbcSMatt Macy * I/O class whose minimum has not been satisfied. Once all are satisfied and
64eda14cbcSMatt Macy * the aggregate maximum has not been hit, the scheduler looks for classes
65eda14cbcSMatt Macy * whose maximum has not been satisfied. Iteration through the I/O classes is
66eda14cbcSMatt Macy * done in the order specified above. No further operations are issued if the
67eda14cbcSMatt Macy * aggregate maximum number of concurrent operations has been hit or if there
68eda14cbcSMatt Macy * are no operations queued for an I/O class that has not hit its maximum.
69eda14cbcSMatt Macy * Every time an i/o is queued or an operation completes, the I/O scheduler
70eda14cbcSMatt Macy * looks for new operations to issue.
71eda14cbcSMatt Macy *
72eda14cbcSMatt Macy * All I/O classes have a fixed maximum number of outstanding operations
73eda14cbcSMatt Macy * except for the async write class. Asynchronous writes represent the data
74eda14cbcSMatt Macy * that is committed to stable storage during the syncing stage for
75eda14cbcSMatt Macy * transaction groups (see txg.c). Transaction groups enter the syncing state
76eda14cbcSMatt Macy * periodically so the number of queued async writes will quickly burst up and
77eda14cbcSMatt Macy * then bleed down to zero. Rather than servicing them as quickly as possible,
78eda14cbcSMatt Macy * the I/O scheduler changes the maximum number of active async write i/os
79eda14cbcSMatt Macy * according to the amount of dirty data in the pool (see dsl_pool.c). Since
80eda14cbcSMatt Macy * both throughput and latency typically increase with the number of
81eda14cbcSMatt Macy * concurrent operations issued to physical devices, reducing the burstiness
82eda14cbcSMatt Macy * in the number of concurrent operations also stabilizes the response time of
83eda14cbcSMatt Macy * operations from other -- and in particular synchronous -- queues. In broad
84eda14cbcSMatt Macy * strokes, the I/O scheduler will issue more concurrent operations from the
85eda14cbcSMatt Macy * async write queue as there's more dirty data in the pool.
86eda14cbcSMatt Macy *
87eda14cbcSMatt Macy * Async Writes
88eda14cbcSMatt Macy *
89eda14cbcSMatt Macy * The number of concurrent operations issued for the async write I/O class
90eda14cbcSMatt Macy * follows a piece-wise linear function defined by a few adjustable points.
91eda14cbcSMatt Macy *
92eda14cbcSMatt Macy * | o---------| <-- zfs_vdev_async_write_max_active
93eda14cbcSMatt Macy * ^ | /^ |
94eda14cbcSMatt Macy * | | / | |
95eda14cbcSMatt Macy * active | / | |
96eda14cbcSMatt Macy * I/O | / | |
97eda14cbcSMatt Macy * count | / | |
98eda14cbcSMatt Macy * | / | |
99eda14cbcSMatt Macy * |------------o | | <-- zfs_vdev_async_write_min_active
100eda14cbcSMatt Macy * 0|____________^______|_________|
101eda14cbcSMatt Macy * 0% | | 100% of zfs_dirty_data_max
102eda14cbcSMatt Macy * | |
103eda14cbcSMatt Macy * | `-- zfs_vdev_async_write_active_max_dirty_percent
104eda14cbcSMatt Macy * `--------- zfs_vdev_async_write_active_min_dirty_percent
105eda14cbcSMatt Macy *
106eda14cbcSMatt Macy * Until the amount of dirty data exceeds a minimum percentage of the dirty
107eda14cbcSMatt Macy * data allowed in the pool, the I/O scheduler will limit the number of
108eda14cbcSMatt Macy * concurrent operations to the minimum. As that threshold is crossed, the
109eda14cbcSMatt Macy * number of concurrent operations issued increases linearly to the maximum at
110eda14cbcSMatt Macy * the specified maximum percentage of the dirty data allowed in the pool.
111eda14cbcSMatt Macy *
112eda14cbcSMatt Macy * Ideally, the amount of dirty data on a busy pool will stay in the sloped
113eda14cbcSMatt Macy * part of the function between zfs_vdev_async_write_active_min_dirty_percent
114eda14cbcSMatt Macy * and zfs_vdev_async_write_active_max_dirty_percent. If it exceeds the
115eda14cbcSMatt Macy * maximum percentage, this indicates that the rate of incoming data is
116eda14cbcSMatt Macy * greater than the rate that the backend storage can handle. In this case, we
117eda14cbcSMatt Macy * must further throttle incoming writes (see dmu_tx_delay() for details).
118eda14cbcSMatt Macy */
119eda14cbcSMatt Macy
120eda14cbcSMatt Macy /*
121eda14cbcSMatt Macy * The maximum number of i/os active to each device. Ideally, this will be >=
1227877fdebSMatt Macy * the sum of each queue's max_active.
123eda14cbcSMatt Macy */
124be181ee2SMartin Matuska uint_t zfs_vdev_max_active = 1000;
125eda14cbcSMatt Macy
126eda14cbcSMatt Macy /*
127eda14cbcSMatt Macy * Per-queue limits on the number of i/os active to each device. If the
128eda14cbcSMatt Macy * number of active i/os is < zfs_vdev_max_active, then the min_active comes
1297877fdebSMatt Macy * into play. We will send min_active from each queue round-robin, and then
1307877fdebSMatt Macy * send from queues in the order defined by zio_priority_t up to max_active.
1317877fdebSMatt Macy * Some queues have additional mechanisms to limit number of active I/Os in
1327877fdebSMatt Macy * addition to min_active and max_active, see below.
133eda14cbcSMatt Macy *
134eda14cbcSMatt Macy * In general, smaller max_active's will lead to lower latency of synchronous
135eda14cbcSMatt Macy * operations. Larger max_active's may lead to higher overall throughput,
136eda14cbcSMatt Macy * depending on underlying storage.
137eda14cbcSMatt Macy *
138eda14cbcSMatt Macy * The ratio of the queues' max_actives determines the balance of performance
139eda14cbcSMatt Macy * between reads, writes, and scrubs. E.g., increasing
140eda14cbcSMatt Macy * zfs_vdev_scrub_max_active will cause the scrub or resilver to complete
141eda14cbcSMatt Macy * more quickly, but reads and writes to have higher latency and lower
142eda14cbcSMatt Macy * throughput.
143eda14cbcSMatt Macy */
144be181ee2SMartin Matuska static uint_t zfs_vdev_sync_read_min_active = 10;
145be181ee2SMartin Matuska static uint_t zfs_vdev_sync_read_max_active = 10;
146be181ee2SMartin Matuska static uint_t zfs_vdev_sync_write_min_active = 10;
147be181ee2SMartin Matuska static uint_t zfs_vdev_sync_write_max_active = 10;
148be181ee2SMartin Matuska static uint_t zfs_vdev_async_read_min_active = 1;
149be181ee2SMartin Matuska /* */ uint_t zfs_vdev_async_read_max_active = 3;
150be181ee2SMartin Matuska static uint_t zfs_vdev_async_write_min_active = 2;
151be181ee2SMartin Matuska /* */ uint_t zfs_vdev_async_write_max_active = 10;
152be181ee2SMartin Matuska static uint_t zfs_vdev_scrub_min_active = 1;
153be181ee2SMartin Matuska static uint_t zfs_vdev_scrub_max_active = 3;
154be181ee2SMartin Matuska static uint_t zfs_vdev_removal_min_active = 1;
155be181ee2SMartin Matuska static uint_t zfs_vdev_removal_max_active = 2;
156be181ee2SMartin Matuska static uint_t zfs_vdev_initializing_min_active = 1;
157be181ee2SMartin Matuska static uint_t zfs_vdev_initializing_max_active = 1;
158be181ee2SMartin Matuska static uint_t zfs_vdev_trim_min_active = 1;
159be181ee2SMartin Matuska static uint_t zfs_vdev_trim_max_active = 2;
160be181ee2SMartin Matuska static uint_t zfs_vdev_rebuild_min_active = 1;
161be181ee2SMartin Matuska static uint_t zfs_vdev_rebuild_max_active = 3;
162eda14cbcSMatt Macy
163eda14cbcSMatt Macy /*
164eda14cbcSMatt Macy * When the pool has less than zfs_vdev_async_write_active_min_dirty_percent
165eda14cbcSMatt Macy * dirty data, use zfs_vdev_async_write_min_active. When it has more than
166eda14cbcSMatt Macy * zfs_vdev_async_write_active_max_dirty_percent, use
167eda14cbcSMatt Macy * zfs_vdev_async_write_max_active. The value is linearly interpolated
168eda14cbcSMatt Macy * between min and max.
169eda14cbcSMatt Macy */
170be181ee2SMartin Matuska uint_t zfs_vdev_async_write_active_min_dirty_percent = 30;
171be181ee2SMartin Matuska uint_t zfs_vdev_async_write_active_max_dirty_percent = 60;
172eda14cbcSMatt Macy
173eda14cbcSMatt Macy /*
1747877fdebSMatt Macy * For non-interactive I/O (scrub, resilver, removal, initialize and rebuild),
1757877fdebSMatt Macy * the number of concurrently-active I/O's is limited to *_min_active, unless
1767877fdebSMatt Macy * the vdev is "idle". When there are no interactive I/Os active (sync or
1777877fdebSMatt Macy * async), and zfs_vdev_nia_delay I/Os have completed since the last
1787877fdebSMatt Macy * interactive I/O, then the vdev is considered to be "idle", and the number
1797877fdebSMatt Macy * of concurrently-active non-interactive I/O's is increased to *_max_active.
1807877fdebSMatt Macy */
181e92ffd9bSMartin Matuska static uint_t zfs_vdev_nia_delay = 5;
1827877fdebSMatt Macy
1837877fdebSMatt Macy /*
1847877fdebSMatt Macy * Some HDDs tend to prioritize sequential I/O so high that concurrent
1857877fdebSMatt Macy * random I/O latency reaches several seconds. On some HDDs it happens
1867877fdebSMatt Macy * even if sequential I/Os are submitted one at a time, and so setting
1877877fdebSMatt Macy * *_max_active to 1 does not help. To prevent non-interactive I/Os, like
1887877fdebSMatt Macy * scrub, from monopolizing the device no more than zfs_vdev_nia_credit
1897877fdebSMatt Macy * I/Os can be sent while there are outstanding incomplete interactive
1907877fdebSMatt Macy * I/Os. This enforced wait ensures the HDD services the interactive I/O
1917877fdebSMatt Macy * within a reasonable amount of time.
1927877fdebSMatt Macy */
193e92ffd9bSMartin Matuska static uint_t zfs_vdev_nia_credit = 5;
1947877fdebSMatt Macy
1957877fdebSMatt Macy /*
196eda14cbcSMatt Macy * To reduce IOPs, we aggregate small adjacent I/Os into one large I/O.
197eda14cbcSMatt Macy * For read I/Os, we also aggregate across small adjacency gaps; for writes
198eda14cbcSMatt Macy * we include spans of optional I/Os to aid aggregation at the disk even when
199eda14cbcSMatt Macy * they aren't able to help us aggregate at this level.
200eda14cbcSMatt Macy */
201be181ee2SMartin Matuska static uint_t zfs_vdev_aggregation_limit = 1 << 20;
202be181ee2SMartin Matuska static uint_t zfs_vdev_aggregation_limit_non_rotating = SPA_OLD_MAXBLOCKSIZE;
203be181ee2SMartin Matuska static uint_t zfs_vdev_read_gap_limit = 32 << 10;
204be181ee2SMartin Matuska static uint_t zfs_vdev_write_gap_limit = 4 << 10;
205eda14cbcSMatt Macy
206eda14cbcSMatt Macy /*
207eda14cbcSMatt Macy * Define the queue depth percentage for each top-level. This percentage is
208eda14cbcSMatt Macy * used in conjunction with zfs_vdev_async_max_active to determine how many
209eda14cbcSMatt Macy * allocations a specific top-level vdev should handle. Once the queue depth
210eda14cbcSMatt Macy * reaches zfs_vdev_queue_depth_pct * zfs_vdev_async_write_max_active / 100
211eda14cbcSMatt Macy * then allocator will stop allocating blocks on that top-level device.
212eda14cbcSMatt Macy * The default kernel setting is 1000% which will yield 100 allocations per
213eda14cbcSMatt Macy * device. For userland testing, the default setting is 300% which equates
214eda14cbcSMatt Macy * to 30 allocations per device.
215eda14cbcSMatt Macy */
216eda14cbcSMatt Macy #ifdef _KERNEL
217be181ee2SMartin Matuska uint_t zfs_vdev_queue_depth_pct = 1000;
218eda14cbcSMatt Macy #else
219be181ee2SMartin Matuska uint_t zfs_vdev_queue_depth_pct = 300;
220eda14cbcSMatt Macy #endif
221eda14cbcSMatt Macy
222eda14cbcSMatt Macy /*
223eda14cbcSMatt Macy * When performing allocations for a given metaslab, we want to make sure that
224eda14cbcSMatt Macy * there are enough IOs to aggregate together to improve throughput. We want to
225eda14cbcSMatt Macy * ensure that there are at least 128k worth of IOs that can be aggregated, and
226eda14cbcSMatt Macy * we assume that the average allocation size is 4k, so we need the queue depth
227eda14cbcSMatt Macy * to be 32 per allocator to get good aggregation of sequential writes.
228eda14cbcSMatt Macy */
229be181ee2SMartin Matuska uint_t zfs_vdev_def_queue_depth = 32;
230eda14cbcSMatt Macy
231eda14cbcSMatt Macy static int
vdev_queue_offset_compare(const void * x1,const void * x2)232eda14cbcSMatt Macy vdev_queue_offset_compare(const void *x1, const void *x2)
233eda14cbcSMatt Macy {
234eda14cbcSMatt Macy const zio_t *z1 = (const zio_t *)x1;
235eda14cbcSMatt Macy const zio_t *z2 = (const zio_t *)x2;
236eda14cbcSMatt Macy
237eda14cbcSMatt Macy int cmp = TREE_CMP(z1->io_offset, z2->io_offset);
238eda14cbcSMatt Macy
239eda14cbcSMatt Macy if (likely(cmp))
240eda14cbcSMatt Macy return (cmp);
241eda14cbcSMatt Macy
242eda14cbcSMatt Macy return (TREE_PCMP(z1, z2));
243eda14cbcSMatt Macy }
244eda14cbcSMatt Macy
2457b5e6873SMartin Matuska #define VDQ_T_SHIFT 29
246eda14cbcSMatt Macy
247eda14cbcSMatt Macy static int
vdev_queue_to_compare(const void * x1,const void * x2)2487b5e6873SMartin Matuska vdev_queue_to_compare(const void *x1, const void *x2)
249eda14cbcSMatt Macy {
250eda14cbcSMatt Macy const zio_t *z1 = (const zio_t *)x1;
251eda14cbcSMatt Macy const zio_t *z2 = (const zio_t *)x2;
252eda14cbcSMatt Macy
2537b5e6873SMartin Matuska int tcmp = TREE_CMP(z1->io_timestamp >> VDQ_T_SHIFT,
2547b5e6873SMartin Matuska z2->io_timestamp >> VDQ_T_SHIFT);
2557b5e6873SMartin Matuska int ocmp = TREE_CMP(z1->io_offset, z2->io_offset);
2567b5e6873SMartin Matuska int cmp = tcmp ? tcmp : ocmp;
257eda14cbcSMatt Macy
2587b5e6873SMartin Matuska if (likely(cmp | (z1->io_queue_state == ZIO_QS_NONE)))
259eda14cbcSMatt Macy return (cmp);
260eda14cbcSMatt Macy
261eda14cbcSMatt Macy return (TREE_PCMP(z1, z2));
262eda14cbcSMatt Macy }
263eda14cbcSMatt Macy
2647b5e6873SMartin Matuska static inline boolean_t
vdev_queue_class_fifo(zio_priority_t p)2657b5e6873SMartin Matuska vdev_queue_class_fifo(zio_priority_t p)
2667b5e6873SMartin Matuska {
2677b5e6873SMartin Matuska return (p == ZIO_PRIORITY_SYNC_READ || p == ZIO_PRIORITY_SYNC_WRITE ||
2687b5e6873SMartin Matuska p == ZIO_PRIORITY_TRIM);
2697b5e6873SMartin Matuska }
2707b5e6873SMartin Matuska
2717b5e6873SMartin Matuska static void
vdev_queue_class_add(vdev_queue_t * vq,zio_t * zio)2727b5e6873SMartin Matuska vdev_queue_class_add(vdev_queue_t *vq, zio_t *zio)
2737b5e6873SMartin Matuska {
2747b5e6873SMartin Matuska zio_priority_t p = zio->io_priority;
2757b5e6873SMartin Matuska vq->vq_cqueued |= 1U << p;
276*14c2e0a0SMartin Matuska if (vdev_queue_class_fifo(p)) {
2777b5e6873SMartin Matuska list_insert_tail(&vq->vq_class[p].vqc_list, zio);
278*14c2e0a0SMartin Matuska vq->vq_class[p].vqc_list_numnodes++;
279*14c2e0a0SMartin Matuska }
2807b5e6873SMartin Matuska else
2817b5e6873SMartin Matuska avl_add(&vq->vq_class[p].vqc_tree, zio);
2827b5e6873SMartin Matuska }
2837b5e6873SMartin Matuska
2847b5e6873SMartin Matuska static void
vdev_queue_class_remove(vdev_queue_t * vq,zio_t * zio)2857b5e6873SMartin Matuska vdev_queue_class_remove(vdev_queue_t *vq, zio_t *zio)
2867b5e6873SMartin Matuska {
2877b5e6873SMartin Matuska zio_priority_t p = zio->io_priority;
2887b5e6873SMartin Matuska uint32_t empty;
2897b5e6873SMartin Matuska if (vdev_queue_class_fifo(p)) {
2907b5e6873SMartin Matuska list_t *list = &vq->vq_class[p].vqc_list;
2917b5e6873SMartin Matuska list_remove(list, zio);
2927b5e6873SMartin Matuska empty = list_is_empty(list);
293*14c2e0a0SMartin Matuska vq->vq_class[p].vqc_list_numnodes--;
2947b5e6873SMartin Matuska } else {
2957b5e6873SMartin Matuska avl_tree_t *tree = &vq->vq_class[p].vqc_tree;
2967b5e6873SMartin Matuska avl_remove(tree, zio);
2977b5e6873SMartin Matuska empty = avl_is_empty(tree);
2987b5e6873SMartin Matuska }
2997b5e6873SMartin Matuska vq->vq_cqueued &= ~(empty << p);
3007b5e6873SMartin Matuska }
3017b5e6873SMartin Matuska
302be181ee2SMartin Matuska static uint_t
vdev_queue_class_min_active(vdev_queue_t * vq,zio_priority_t p)3037877fdebSMatt Macy vdev_queue_class_min_active(vdev_queue_t *vq, zio_priority_t p)
304eda14cbcSMatt Macy {
305eda14cbcSMatt Macy switch (p) {
306eda14cbcSMatt Macy case ZIO_PRIORITY_SYNC_READ:
307eda14cbcSMatt Macy return (zfs_vdev_sync_read_min_active);
308eda14cbcSMatt Macy case ZIO_PRIORITY_SYNC_WRITE:
309eda14cbcSMatt Macy return (zfs_vdev_sync_write_min_active);
310eda14cbcSMatt Macy case ZIO_PRIORITY_ASYNC_READ:
311eda14cbcSMatt Macy return (zfs_vdev_async_read_min_active);
312eda14cbcSMatt Macy case ZIO_PRIORITY_ASYNC_WRITE:
313eda14cbcSMatt Macy return (zfs_vdev_async_write_min_active);
314eda14cbcSMatt Macy case ZIO_PRIORITY_SCRUB:
3157877fdebSMatt Macy return (vq->vq_ia_active == 0 ? zfs_vdev_scrub_min_active :
3167877fdebSMatt Macy MIN(vq->vq_nia_credit, zfs_vdev_scrub_min_active));
317eda14cbcSMatt Macy case ZIO_PRIORITY_REMOVAL:
3187877fdebSMatt Macy return (vq->vq_ia_active == 0 ? zfs_vdev_removal_min_active :
3197877fdebSMatt Macy MIN(vq->vq_nia_credit, zfs_vdev_removal_min_active));
320eda14cbcSMatt Macy case ZIO_PRIORITY_INITIALIZING:
3217877fdebSMatt Macy return (vq->vq_ia_active == 0 ?zfs_vdev_initializing_min_active:
3227877fdebSMatt Macy MIN(vq->vq_nia_credit, zfs_vdev_initializing_min_active));
323eda14cbcSMatt Macy case ZIO_PRIORITY_TRIM:
324eda14cbcSMatt Macy return (zfs_vdev_trim_min_active);
325eda14cbcSMatt Macy case ZIO_PRIORITY_REBUILD:
3267877fdebSMatt Macy return (vq->vq_ia_active == 0 ? zfs_vdev_rebuild_min_active :
3277877fdebSMatt Macy MIN(vq->vq_nia_credit, zfs_vdev_rebuild_min_active));
328eda14cbcSMatt Macy default:
329eda14cbcSMatt Macy panic("invalid priority %u", p);
330eda14cbcSMatt Macy return (0);
331eda14cbcSMatt Macy }
332eda14cbcSMatt Macy }
333eda14cbcSMatt Macy
334be181ee2SMartin Matuska static uint_t
vdev_queue_max_async_writes(spa_t * spa)335eda14cbcSMatt Macy vdev_queue_max_async_writes(spa_t *spa)
336eda14cbcSMatt Macy {
337be181ee2SMartin Matuska uint_t writes;
338eda14cbcSMatt Macy uint64_t dirty = 0;
339eda14cbcSMatt Macy dsl_pool_t *dp = spa_get_dsl(spa);
340eda14cbcSMatt Macy uint64_t min_bytes = zfs_dirty_data_max *
341eda14cbcSMatt Macy zfs_vdev_async_write_active_min_dirty_percent / 100;
342eda14cbcSMatt Macy uint64_t max_bytes = zfs_dirty_data_max *
343eda14cbcSMatt Macy zfs_vdev_async_write_active_max_dirty_percent / 100;
344eda14cbcSMatt Macy
345eda14cbcSMatt Macy /*
346eda14cbcSMatt Macy * Async writes may occur before the assignment of the spa's
347eda14cbcSMatt Macy * dsl_pool_t if a self-healing zio is issued prior to the
348eda14cbcSMatt Macy * completion of dmu_objset_open_impl().
349eda14cbcSMatt Macy */
350eda14cbcSMatt Macy if (dp == NULL)
351eda14cbcSMatt Macy return (zfs_vdev_async_write_max_active);
352eda14cbcSMatt Macy
353eda14cbcSMatt Macy /*
354eda14cbcSMatt Macy * Sync tasks correspond to interactive user actions. To reduce the
355eda14cbcSMatt Macy * execution time of those actions we push data out as fast as possible.
356eda14cbcSMatt Macy */
3577877fdebSMatt Macy dirty = dp->dp_dirty_total;
3587877fdebSMatt Macy if (dirty > max_bytes || spa_has_pending_synctask(spa))
359eda14cbcSMatt Macy return (zfs_vdev_async_write_max_active);
360eda14cbcSMatt Macy
361eda14cbcSMatt Macy if (dirty < min_bytes)
362eda14cbcSMatt Macy return (zfs_vdev_async_write_min_active);
363eda14cbcSMatt Macy
364eda14cbcSMatt Macy /*
365eda14cbcSMatt Macy * linear interpolation:
366eda14cbcSMatt Macy * slope = (max_writes - min_writes) / (max_bytes - min_bytes)
367eda14cbcSMatt Macy * move right by min_bytes
368eda14cbcSMatt Macy * move up by min_writes
369eda14cbcSMatt Macy */
370eda14cbcSMatt Macy writes = (dirty - min_bytes) *
371eda14cbcSMatt Macy (zfs_vdev_async_write_max_active -
372eda14cbcSMatt Macy zfs_vdev_async_write_min_active) /
373eda14cbcSMatt Macy (max_bytes - min_bytes) +
374eda14cbcSMatt Macy zfs_vdev_async_write_min_active;
375eda14cbcSMatt Macy ASSERT3U(writes, >=, zfs_vdev_async_write_min_active);
376eda14cbcSMatt Macy ASSERT3U(writes, <=, zfs_vdev_async_write_max_active);
377eda14cbcSMatt Macy return (writes);
378eda14cbcSMatt Macy }
379eda14cbcSMatt Macy
380be181ee2SMartin Matuska static uint_t
vdev_queue_class_max_active(vdev_queue_t * vq,zio_priority_t p)3817b5e6873SMartin Matuska vdev_queue_class_max_active(vdev_queue_t *vq, zio_priority_t p)
382eda14cbcSMatt Macy {
383eda14cbcSMatt Macy switch (p) {
384eda14cbcSMatt Macy case ZIO_PRIORITY_SYNC_READ:
385eda14cbcSMatt Macy return (zfs_vdev_sync_read_max_active);
386eda14cbcSMatt Macy case ZIO_PRIORITY_SYNC_WRITE:
387eda14cbcSMatt Macy return (zfs_vdev_sync_write_max_active);
388eda14cbcSMatt Macy case ZIO_PRIORITY_ASYNC_READ:
389eda14cbcSMatt Macy return (zfs_vdev_async_read_max_active);
390eda14cbcSMatt Macy case ZIO_PRIORITY_ASYNC_WRITE:
3917b5e6873SMartin Matuska return (vdev_queue_max_async_writes(vq->vq_vdev->vdev_spa));
392eda14cbcSMatt Macy case ZIO_PRIORITY_SCRUB:
3937877fdebSMatt Macy if (vq->vq_ia_active > 0) {
3947877fdebSMatt Macy return (MIN(vq->vq_nia_credit,
3957877fdebSMatt Macy zfs_vdev_scrub_min_active));
3967877fdebSMatt Macy } else if (vq->vq_nia_credit < zfs_vdev_nia_delay)
3977877fdebSMatt Macy return (MAX(1, zfs_vdev_scrub_min_active));
398eda14cbcSMatt Macy return (zfs_vdev_scrub_max_active);
399eda14cbcSMatt Macy case ZIO_PRIORITY_REMOVAL:
4007877fdebSMatt Macy if (vq->vq_ia_active > 0) {
4017877fdebSMatt Macy return (MIN(vq->vq_nia_credit,
4027877fdebSMatt Macy zfs_vdev_removal_min_active));
4037877fdebSMatt Macy } else if (vq->vq_nia_credit < zfs_vdev_nia_delay)
4047877fdebSMatt Macy return (MAX(1, zfs_vdev_removal_min_active));
405eda14cbcSMatt Macy return (zfs_vdev_removal_max_active);
406eda14cbcSMatt Macy case ZIO_PRIORITY_INITIALIZING:
4077877fdebSMatt Macy if (vq->vq_ia_active > 0) {
4087877fdebSMatt Macy return (MIN(vq->vq_nia_credit,
4097877fdebSMatt Macy zfs_vdev_initializing_min_active));
4107877fdebSMatt Macy } else if (vq->vq_nia_credit < zfs_vdev_nia_delay)
4117877fdebSMatt Macy return (MAX(1, zfs_vdev_initializing_min_active));
412eda14cbcSMatt Macy return (zfs_vdev_initializing_max_active);
413eda14cbcSMatt Macy case ZIO_PRIORITY_TRIM:
414eda14cbcSMatt Macy return (zfs_vdev_trim_max_active);
415eda14cbcSMatt Macy case ZIO_PRIORITY_REBUILD:
4167877fdebSMatt Macy if (vq->vq_ia_active > 0) {
4177877fdebSMatt Macy return (MIN(vq->vq_nia_credit,
4187877fdebSMatt Macy zfs_vdev_rebuild_min_active));
4197877fdebSMatt Macy } else if (vq->vq_nia_credit < zfs_vdev_nia_delay)
4207877fdebSMatt Macy return (MAX(1, zfs_vdev_rebuild_min_active));
421eda14cbcSMatt Macy return (zfs_vdev_rebuild_max_active);
422eda14cbcSMatt Macy default:
423eda14cbcSMatt Macy panic("invalid priority %u", p);
424eda14cbcSMatt Macy return (0);
425eda14cbcSMatt Macy }
426eda14cbcSMatt Macy }
427eda14cbcSMatt Macy
428eda14cbcSMatt Macy /*
429681ce946SMartin Matuska * Return the i/o class to issue from, or ZIO_PRIORITY_NUM_QUEUEABLE if
430eda14cbcSMatt Macy * there is no eligible class.
431eda14cbcSMatt Macy */
432eda14cbcSMatt Macy static zio_priority_t
vdev_queue_class_to_issue(vdev_queue_t * vq)433eda14cbcSMatt Macy vdev_queue_class_to_issue(vdev_queue_t *vq)
434eda14cbcSMatt Macy {
4357b5e6873SMartin Matuska uint32_t cq = vq->vq_cqueued;
4367b5e6873SMartin Matuska zio_priority_t p, p1;
437eda14cbcSMatt Macy
4387b5e6873SMartin Matuska if (cq == 0 || vq->vq_active >= zfs_vdev_max_active)
439eda14cbcSMatt Macy return (ZIO_PRIORITY_NUM_QUEUEABLE);
440eda14cbcSMatt Macy
4417877fdebSMatt Macy /*
4427877fdebSMatt Macy * Find a queue that has not reached its minimum # outstanding i/os.
4437877fdebSMatt Macy * Do round-robin to reduce starvation due to zfs_vdev_max_active
4447877fdebSMatt Macy * and vq_nia_credit limits.
4457877fdebSMatt Macy */
4467b5e6873SMartin Matuska p1 = vq->vq_last_prio + 1;
4477b5e6873SMartin Matuska if (p1 >= ZIO_PRIORITY_NUM_QUEUEABLE)
4487b5e6873SMartin Matuska p1 = 0;
4497b5e6873SMartin Matuska for (p = p1; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
4507b5e6873SMartin Matuska if ((cq & (1U << p)) != 0 && vq->vq_cactive[p] <
4517b5e6873SMartin Matuska vdev_queue_class_min_active(vq, p))
4527b5e6873SMartin Matuska goto found;
453eda14cbcSMatt Macy }
4547b5e6873SMartin Matuska for (p = 0; p < p1; p++) {
4557b5e6873SMartin Matuska if ((cq & (1U << p)) != 0 && vq->vq_cactive[p] <
4567b5e6873SMartin Matuska vdev_queue_class_min_active(vq, p))
4577b5e6873SMartin Matuska goto found;
4587877fdebSMatt Macy }
459eda14cbcSMatt Macy
460eda14cbcSMatt Macy /*
461eda14cbcSMatt Macy * If we haven't found a queue, look for one that hasn't reached its
462eda14cbcSMatt Macy * maximum # outstanding i/os.
463eda14cbcSMatt Macy */
464eda14cbcSMatt Macy for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
4657b5e6873SMartin Matuska if ((cq & (1U << p)) != 0 && vq->vq_cactive[p] <
4667b5e6873SMartin Matuska vdev_queue_class_max_active(vq, p))
4677b5e6873SMartin Matuska break;
4687877fdebSMatt Macy }
469eda14cbcSMatt Macy
4707b5e6873SMartin Matuska found:
4717b5e6873SMartin Matuska vq->vq_last_prio = p;
4727b5e6873SMartin Matuska return (p);
473eda14cbcSMatt Macy }
474eda14cbcSMatt Macy
475eda14cbcSMatt Macy void
vdev_queue_init(vdev_t * vd)476eda14cbcSMatt Macy vdev_queue_init(vdev_t *vd)
477eda14cbcSMatt Macy {
478eda14cbcSMatt Macy vdev_queue_t *vq = &vd->vdev_queue;
479eda14cbcSMatt Macy zio_priority_t p;
480eda14cbcSMatt Macy
481eda14cbcSMatt Macy vq->vq_vdev = vd;
482eda14cbcSMatt Macy
483eda14cbcSMatt Macy for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
4847b5e6873SMartin Matuska if (vdev_queue_class_fifo(p)) {
4857b5e6873SMartin Matuska list_create(&vq->vq_class[p].vqc_list,
4867b5e6873SMartin Matuska sizeof (zio_t),
4877b5e6873SMartin Matuska offsetof(struct zio, io_queue_node.l));
488eda14cbcSMatt Macy } else {
4897b5e6873SMartin Matuska avl_create(&vq->vq_class[p].vqc_tree,
4907b5e6873SMartin Matuska vdev_queue_to_compare, sizeof (zio_t),
4917b5e6873SMartin Matuska offsetof(struct zio, io_queue_node.a));
492eda14cbcSMatt Macy }
493eda14cbcSMatt Macy }
4947b5e6873SMartin Matuska avl_create(&vq->vq_read_offset_tree,
4957b5e6873SMartin Matuska vdev_queue_offset_compare, sizeof (zio_t),
4967b5e6873SMartin Matuska offsetof(struct zio, io_offset_node));
4977b5e6873SMartin Matuska avl_create(&vq->vq_write_offset_tree,
4987b5e6873SMartin Matuska vdev_queue_offset_compare, sizeof (zio_t),
4997b5e6873SMartin Matuska offsetof(struct zio, io_offset_node));
500eda14cbcSMatt Macy
501eda14cbcSMatt Macy vq->vq_last_offset = 0;
5027b5e6873SMartin Matuska list_create(&vq->vq_active_list, sizeof (struct zio),
5037b5e6873SMartin Matuska offsetof(struct zio, io_queue_node.l));
5047b5e6873SMartin Matuska mutex_init(&vq->vq_lock, NULL, MUTEX_DEFAULT, NULL);
505eda14cbcSMatt Macy }
506eda14cbcSMatt Macy
507eda14cbcSMatt Macy void
vdev_queue_fini(vdev_t * vd)508eda14cbcSMatt Macy vdev_queue_fini(vdev_t *vd)
509eda14cbcSMatt Macy {
510eda14cbcSMatt Macy vdev_queue_t *vq = &vd->vdev_queue;
511eda14cbcSMatt Macy
5127b5e6873SMartin Matuska for (zio_priority_t p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
5137b5e6873SMartin Matuska if (vdev_queue_class_fifo(p))
5147b5e6873SMartin Matuska list_destroy(&vq->vq_class[p].vqc_list);
5157b5e6873SMartin Matuska else
5167b5e6873SMartin Matuska avl_destroy(&vq->vq_class[p].vqc_tree);
5177b5e6873SMartin Matuska }
5187b5e6873SMartin Matuska avl_destroy(&vq->vq_read_offset_tree);
5197b5e6873SMartin Matuska avl_destroy(&vq->vq_write_offset_tree);
520eda14cbcSMatt Macy
5217b5e6873SMartin Matuska list_destroy(&vq->vq_active_list);
522eda14cbcSMatt Macy mutex_destroy(&vq->vq_lock);
523eda14cbcSMatt Macy }
524eda14cbcSMatt Macy
525eda14cbcSMatt Macy static void
vdev_queue_io_add(vdev_queue_t * vq,zio_t * zio)526eda14cbcSMatt Macy vdev_queue_io_add(vdev_queue_t *vq, zio_t *zio)
527eda14cbcSMatt Macy {
5287b5e6873SMartin Matuska zio->io_queue_state = ZIO_QS_QUEUED;
5297b5e6873SMartin Matuska vdev_queue_class_add(vq, zio);
5307b5e6873SMartin Matuska if (zio->io_type == ZIO_TYPE_READ)
5317b5e6873SMartin Matuska avl_add(&vq->vq_read_offset_tree, zio);
5327b5e6873SMartin Matuska else if (zio->io_type == ZIO_TYPE_WRITE)
5337b5e6873SMartin Matuska avl_add(&vq->vq_write_offset_tree, zio);
534eda14cbcSMatt Macy }
535eda14cbcSMatt Macy
536eda14cbcSMatt Macy static void
vdev_queue_io_remove(vdev_queue_t * vq,zio_t * zio)537eda14cbcSMatt Macy vdev_queue_io_remove(vdev_queue_t *vq, zio_t *zio)
538eda14cbcSMatt Macy {
5397b5e6873SMartin Matuska vdev_queue_class_remove(vq, zio);
5407b5e6873SMartin Matuska if (zio->io_type == ZIO_TYPE_READ)
5417b5e6873SMartin Matuska avl_remove(&vq->vq_read_offset_tree, zio);
5427b5e6873SMartin Matuska else if (zio->io_type == ZIO_TYPE_WRITE)
5437b5e6873SMartin Matuska avl_remove(&vq->vq_write_offset_tree, zio);
5447b5e6873SMartin Matuska zio->io_queue_state = ZIO_QS_NONE;
545eda14cbcSMatt Macy }
546eda14cbcSMatt Macy
5477877fdebSMatt Macy static boolean_t
vdev_queue_is_interactive(zio_priority_t p)5487877fdebSMatt Macy vdev_queue_is_interactive(zio_priority_t p)
5497877fdebSMatt Macy {
5507877fdebSMatt Macy switch (p) {
5517877fdebSMatt Macy case ZIO_PRIORITY_SCRUB:
5527877fdebSMatt Macy case ZIO_PRIORITY_REMOVAL:
5537877fdebSMatt Macy case ZIO_PRIORITY_INITIALIZING:
5547877fdebSMatt Macy case ZIO_PRIORITY_REBUILD:
5557877fdebSMatt Macy return (B_FALSE);
5567877fdebSMatt Macy default:
5577877fdebSMatt Macy return (B_TRUE);
5587877fdebSMatt Macy }
5597877fdebSMatt Macy }
5607877fdebSMatt Macy
561eda14cbcSMatt Macy static void
vdev_queue_pending_add(vdev_queue_t * vq,zio_t * zio)562eda14cbcSMatt Macy vdev_queue_pending_add(vdev_queue_t *vq, zio_t *zio)
563eda14cbcSMatt Macy {
564eda14cbcSMatt Macy ASSERT(MUTEX_HELD(&vq->vq_lock));
565eda14cbcSMatt Macy ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
5667b5e6873SMartin Matuska vq->vq_cactive[zio->io_priority]++;
5677b5e6873SMartin Matuska vq->vq_active++;
5687877fdebSMatt Macy if (vdev_queue_is_interactive(zio->io_priority)) {
5697877fdebSMatt Macy if (++vq->vq_ia_active == 1)
5707877fdebSMatt Macy vq->vq_nia_credit = 1;
5717877fdebSMatt Macy } else if (vq->vq_ia_active > 0) {
5727877fdebSMatt Macy vq->vq_nia_credit--;
5737877fdebSMatt Macy }
5747b5e6873SMartin Matuska zio->io_queue_state = ZIO_QS_ACTIVE;
5757b5e6873SMartin Matuska list_insert_tail(&vq->vq_active_list, zio);
576eda14cbcSMatt Macy }
577eda14cbcSMatt Macy
578eda14cbcSMatt Macy static void
vdev_queue_pending_remove(vdev_queue_t * vq,zio_t * zio)579eda14cbcSMatt Macy vdev_queue_pending_remove(vdev_queue_t *vq, zio_t *zio)
580eda14cbcSMatt Macy {
581eda14cbcSMatt Macy ASSERT(MUTEX_HELD(&vq->vq_lock));
582eda14cbcSMatt Macy ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
5837b5e6873SMartin Matuska vq->vq_cactive[zio->io_priority]--;
5847b5e6873SMartin Matuska vq->vq_active--;
5857877fdebSMatt Macy if (vdev_queue_is_interactive(zio->io_priority)) {
5867877fdebSMatt Macy if (--vq->vq_ia_active == 0)
5877877fdebSMatt Macy vq->vq_nia_credit = 0;
5887877fdebSMatt Macy else
5897877fdebSMatt Macy vq->vq_nia_credit = zfs_vdev_nia_credit;
5907877fdebSMatt Macy } else if (vq->vq_ia_active == 0)
5917877fdebSMatt Macy vq->vq_nia_credit++;
5927b5e6873SMartin Matuska list_remove(&vq->vq_active_list, zio);
5937b5e6873SMartin Matuska zio->io_queue_state = ZIO_QS_NONE;
594eda14cbcSMatt Macy }
595eda14cbcSMatt Macy
596eda14cbcSMatt Macy static void
vdev_queue_agg_io_done(zio_t * aio)597eda14cbcSMatt Macy vdev_queue_agg_io_done(zio_t *aio)
598eda14cbcSMatt Macy {
599eda14cbcSMatt Macy abd_free(aio->io_abd);
600eda14cbcSMatt Macy }
601eda14cbcSMatt Macy
602eda14cbcSMatt Macy /*
603eda14cbcSMatt Macy * Compute the range spanned by two i/os, which is the endpoint of the last
604eda14cbcSMatt Macy * (lio->io_offset + lio->io_size) minus start of the first (fio->io_offset).
605eda14cbcSMatt Macy * Conveniently, the gap between fio and lio is given by -IO_SPAN(lio, fio);
606eda14cbcSMatt Macy * thus fio and lio are adjacent if and only if IO_SPAN(lio, fio) == 0.
607eda14cbcSMatt Macy */
608eda14cbcSMatt Macy #define IO_SPAN(fio, lio) ((lio)->io_offset + (lio)->io_size - (fio)->io_offset)
609eda14cbcSMatt Macy #define IO_GAP(fio, lio) (-IO_SPAN(lio, fio))
610eda14cbcSMatt Macy
611eda14cbcSMatt Macy /*
612eda14cbcSMatt Macy * Sufficiently adjacent io_offset's in ZIOs will be aggregated. We do this
613eda14cbcSMatt Macy * by creating a gang ABD from the adjacent ZIOs io_abd's. By using
614eda14cbcSMatt Macy * a gang ABD we avoid doing memory copies to and from the parent,
615eda14cbcSMatt Macy * child ZIOs. The gang ABD also accounts for gaps between adjacent
616eda14cbcSMatt Macy * io_offsets by simply getting the zero ABD for writes or allocating
617eda14cbcSMatt Macy * a new ABD for reads and placing them in the gang ABD as well.
618eda14cbcSMatt Macy */
619eda14cbcSMatt Macy static zio_t *
vdev_queue_aggregate(vdev_queue_t * vq,zio_t * zio)620eda14cbcSMatt Macy vdev_queue_aggregate(vdev_queue_t *vq, zio_t *zio)
621eda14cbcSMatt Macy {
622eda14cbcSMatt Macy zio_t *first, *last, *aio, *dio, *mandatory, *nio;
623eda14cbcSMatt Macy uint64_t maxgap = 0;
624eda14cbcSMatt Macy uint64_t size;
625eda14cbcSMatt Macy uint64_t limit;
626eda14cbcSMatt Macy boolean_t stretch = B_FALSE;
627eda14cbcSMatt Macy uint64_t next_offset;
628eda14cbcSMatt Macy abd_t *abd;
6297b5e6873SMartin Matuska avl_tree_t *t;
630eda14cbcSMatt Macy
6317b5e6873SMartin Matuska /*
6327b5e6873SMartin Matuska * TRIM aggregation should not be needed since code in zfs_trim.c can
6337b5e6873SMartin Matuska * submit TRIM I/O for extents up to zfs_trim_extent_bytes_max (128M).
6347b5e6873SMartin Matuska */
6357b5e6873SMartin Matuska if (zio->io_type == ZIO_TYPE_TRIM)
6367b5e6873SMartin Matuska return (NULL);
6377b5e6873SMartin Matuska
6387b5e6873SMartin Matuska if (zio->io_flags & ZIO_FLAG_DONT_AGGREGATE)
6397b5e6873SMartin Matuska return (NULL);
6407b5e6873SMartin Matuska
641eda14cbcSMatt Macy if (vq->vq_vdev->vdev_nonrot)
642eda14cbcSMatt Macy limit = zfs_vdev_aggregation_limit_non_rotating;
643eda14cbcSMatt Macy else
644eda14cbcSMatt Macy limit = zfs_vdev_aggregation_limit;
6457b5e6873SMartin Matuska if (limit == 0)
646eda14cbcSMatt Macy return (NULL);
6477b5e6873SMartin Matuska limit = MIN(limit, SPA_MAXBLOCKSIZE);
648eda14cbcSMatt Macy
6497877fdebSMatt Macy /*
6507877fdebSMatt Macy * I/Os to distributed spares are directly dispatched to the dRAID
6517877fdebSMatt Macy * leaf vdevs for aggregation. See the comment at the end of the
6527877fdebSMatt Macy * zio_vdev_io_start() function.
6537877fdebSMatt Macy */
6547877fdebSMatt Macy ASSERT(vq->vq_vdev->vdev_ops != &vdev_draid_spare_ops);
6557877fdebSMatt Macy
656eda14cbcSMatt Macy first = last = zio;
657eda14cbcSMatt Macy
6587b5e6873SMartin Matuska if (zio->io_type == ZIO_TYPE_READ) {
659eda14cbcSMatt Macy maxgap = zfs_vdev_read_gap_limit;
6607b5e6873SMartin Matuska t = &vq->vq_read_offset_tree;
6617b5e6873SMartin Matuska } else {
6627b5e6873SMartin Matuska ASSERT3U(zio->io_type, ==, ZIO_TYPE_WRITE);
6637b5e6873SMartin Matuska t = &vq->vq_write_offset_tree;
6647b5e6873SMartin Matuska }
665eda14cbcSMatt Macy
666eda14cbcSMatt Macy /*
667eda14cbcSMatt Macy * We can aggregate I/Os that are sufficiently adjacent and of
668eda14cbcSMatt Macy * the same flavor, as expressed by the AGG_INHERIT flags.
669eda14cbcSMatt Macy * The latter requirement is necessary so that certain
670eda14cbcSMatt Macy * attributes of the I/O, such as whether it's a normal I/O
671eda14cbcSMatt Macy * or a scrub/resilver, can be preserved in the aggregate.
672eda14cbcSMatt Macy * We can include optional I/Os, but don't allow them
673eda14cbcSMatt Macy * to begin a range as they add no benefit in that situation.
674eda14cbcSMatt Macy */
675eda14cbcSMatt Macy
676eda14cbcSMatt Macy /*
677eda14cbcSMatt Macy * We keep track of the last non-optional I/O.
678eda14cbcSMatt Macy */
679eda14cbcSMatt Macy mandatory = (first->io_flags & ZIO_FLAG_OPTIONAL) ? NULL : first;
680eda14cbcSMatt Macy
681eda14cbcSMatt Macy /*
682eda14cbcSMatt Macy * Walk backwards through sufficiently contiguous I/Os
683eda14cbcSMatt Macy * recording the last non-optional I/O.
684eda14cbcSMatt Macy */
6857b5e6873SMartin Matuska zio_flag_t flags = zio->io_flags & ZIO_FLAG_AGG_INHERIT;
686eda14cbcSMatt Macy while ((dio = AVL_PREV(t, first)) != NULL &&
687eda14cbcSMatt Macy (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags &&
688eda14cbcSMatt Macy IO_SPAN(dio, last) <= limit &&
689eda14cbcSMatt Macy IO_GAP(dio, first) <= maxgap &&
690eda14cbcSMatt Macy dio->io_type == zio->io_type) {
691eda14cbcSMatt Macy first = dio;
692eda14cbcSMatt Macy if (mandatory == NULL && !(first->io_flags & ZIO_FLAG_OPTIONAL))
693eda14cbcSMatt Macy mandatory = first;
694eda14cbcSMatt Macy }
695eda14cbcSMatt Macy
696eda14cbcSMatt Macy /*
697eda14cbcSMatt Macy * Skip any initial optional I/Os.
698eda14cbcSMatt Macy */
699eda14cbcSMatt Macy while ((first->io_flags & ZIO_FLAG_OPTIONAL) && first != last) {
700eda14cbcSMatt Macy first = AVL_NEXT(t, first);
701eda14cbcSMatt Macy ASSERT(first != NULL);
702eda14cbcSMatt Macy }
703eda14cbcSMatt Macy
704eda14cbcSMatt Macy
705eda14cbcSMatt Macy /*
706eda14cbcSMatt Macy * Walk forward through sufficiently contiguous I/Os.
707eda14cbcSMatt Macy * The aggregation limit does not apply to optional i/os, so that
708eda14cbcSMatt Macy * we can issue contiguous writes even if they are larger than the
709eda14cbcSMatt Macy * aggregation limit.
710eda14cbcSMatt Macy */
711eda14cbcSMatt Macy while ((dio = AVL_NEXT(t, last)) != NULL &&
712eda14cbcSMatt Macy (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags &&
713eda14cbcSMatt Macy (IO_SPAN(first, dio) <= limit ||
714eda14cbcSMatt Macy (dio->io_flags & ZIO_FLAG_OPTIONAL)) &&
7157b5e6873SMartin Matuska IO_SPAN(first, dio) <= SPA_MAXBLOCKSIZE &&
716eda14cbcSMatt Macy IO_GAP(last, dio) <= maxgap &&
717eda14cbcSMatt Macy dio->io_type == zio->io_type) {
718eda14cbcSMatt Macy last = dio;
719eda14cbcSMatt Macy if (!(last->io_flags & ZIO_FLAG_OPTIONAL))
720eda14cbcSMatt Macy mandatory = last;
721eda14cbcSMatt Macy }
722eda14cbcSMatt Macy
723eda14cbcSMatt Macy /*
724eda14cbcSMatt Macy * Now that we've established the range of the I/O aggregation
725eda14cbcSMatt Macy * we must decide what to do with trailing optional I/Os.
726eda14cbcSMatt Macy * For reads, there's nothing to do. While we are unable to
727eda14cbcSMatt Macy * aggregate further, it's possible that a trailing optional
728eda14cbcSMatt Macy * I/O would allow the underlying device to aggregate with
729eda14cbcSMatt Macy * subsequent I/Os. We must therefore determine if the next
730eda14cbcSMatt Macy * non-optional I/O is close enough to make aggregation
731eda14cbcSMatt Macy * worthwhile.
732eda14cbcSMatt Macy */
733eda14cbcSMatt Macy if (zio->io_type == ZIO_TYPE_WRITE && mandatory != NULL) {
734eda14cbcSMatt Macy zio_t *nio = last;
735eda14cbcSMatt Macy while ((dio = AVL_NEXT(t, nio)) != NULL &&
736eda14cbcSMatt Macy IO_GAP(nio, dio) == 0 &&
737eda14cbcSMatt Macy IO_GAP(mandatory, dio) <= zfs_vdev_write_gap_limit) {
738eda14cbcSMatt Macy nio = dio;
739eda14cbcSMatt Macy if (!(nio->io_flags & ZIO_FLAG_OPTIONAL)) {
740eda14cbcSMatt Macy stretch = B_TRUE;
741eda14cbcSMatt Macy break;
742eda14cbcSMatt Macy }
743eda14cbcSMatt Macy }
744eda14cbcSMatt Macy }
745eda14cbcSMatt Macy
746eda14cbcSMatt Macy if (stretch) {
747eda14cbcSMatt Macy /*
748eda14cbcSMatt Macy * We are going to include an optional io in our aggregated
749eda14cbcSMatt Macy * span, thus closing the write gap. Only mandatory i/os can
750eda14cbcSMatt Macy * start aggregated spans, so make sure that the next i/o
751eda14cbcSMatt Macy * after our span is mandatory.
752eda14cbcSMatt Macy */
753eda14cbcSMatt Macy dio = AVL_NEXT(t, last);
754dbd5678dSMartin Matuska ASSERT3P(dio, !=, NULL);
755eda14cbcSMatt Macy dio->io_flags &= ~ZIO_FLAG_OPTIONAL;
756eda14cbcSMatt Macy } else {
757eda14cbcSMatt Macy /* do not include the optional i/o */
758eda14cbcSMatt Macy while (last != mandatory && last != first) {
759eda14cbcSMatt Macy ASSERT(last->io_flags & ZIO_FLAG_OPTIONAL);
760eda14cbcSMatt Macy last = AVL_PREV(t, last);
761eda14cbcSMatt Macy ASSERT(last != NULL);
762eda14cbcSMatt Macy }
763eda14cbcSMatt Macy }
764eda14cbcSMatt Macy
765eda14cbcSMatt Macy if (first == last)
766eda14cbcSMatt Macy return (NULL);
767eda14cbcSMatt Macy
768eda14cbcSMatt Macy size = IO_SPAN(first, last);
7697b5e6873SMartin Matuska ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
770eda14cbcSMatt Macy
771184c1b94SMartin Matuska abd = abd_alloc_gang();
772eda14cbcSMatt Macy if (abd == NULL)
773eda14cbcSMatt Macy return (NULL);
774eda14cbcSMatt Macy
775eda14cbcSMatt Macy aio = zio_vdev_delegated_io(first->io_vd, first->io_offset,
776eda14cbcSMatt Macy abd, size, first->io_type, zio->io_priority,
7774e8d558cSMartin Matuska flags | ZIO_FLAG_DONT_QUEUE, vdev_queue_agg_io_done, NULL);
778eda14cbcSMatt Macy aio->io_timestamp = first->io_timestamp;
779eda14cbcSMatt Macy
780eda14cbcSMatt Macy nio = first;
781eda14cbcSMatt Macy next_offset = first->io_offset;
782eda14cbcSMatt Macy do {
783eda14cbcSMatt Macy dio = nio;
784eda14cbcSMatt Macy nio = AVL_NEXT(t, dio);
785dbd5678dSMartin Matuska ASSERT3P(dio, !=, NULL);
786eda14cbcSMatt Macy zio_add_child(dio, aio);
787eda14cbcSMatt Macy vdev_queue_io_remove(vq, dio);
788eda14cbcSMatt Macy
789eda14cbcSMatt Macy if (dio->io_offset != next_offset) {
790eda14cbcSMatt Macy /* allocate a buffer for a read gap */
791eda14cbcSMatt Macy ASSERT3U(dio->io_type, ==, ZIO_TYPE_READ);
792eda14cbcSMatt Macy ASSERT3U(dio->io_offset, >, next_offset);
793eda14cbcSMatt Macy abd = abd_alloc_for_io(
794eda14cbcSMatt Macy dio->io_offset - next_offset, B_TRUE);
795eda14cbcSMatt Macy abd_gang_add(aio->io_abd, abd, B_TRUE);
796eda14cbcSMatt Macy }
797eda14cbcSMatt Macy if (dio->io_abd &&
798eda14cbcSMatt Macy (dio->io_size != abd_get_size(dio->io_abd))) {
799eda14cbcSMatt Macy /* abd size not the same as IO size */
800eda14cbcSMatt Macy ASSERT3U(abd_get_size(dio->io_abd), >, dio->io_size);
801eda14cbcSMatt Macy abd = abd_get_offset_size(dio->io_abd, 0, dio->io_size);
802eda14cbcSMatt Macy abd_gang_add(aio->io_abd, abd, B_TRUE);
803eda14cbcSMatt Macy } else {
804eda14cbcSMatt Macy if (dio->io_flags & ZIO_FLAG_NODATA) {
805eda14cbcSMatt Macy /* allocate a buffer for a write gap */
806eda14cbcSMatt Macy ASSERT3U(dio->io_type, ==, ZIO_TYPE_WRITE);
807eda14cbcSMatt Macy ASSERT3P(dio->io_abd, ==, NULL);
808eda14cbcSMatt Macy abd_gang_add(aio->io_abd,
809eda14cbcSMatt Macy abd_get_zeros(dio->io_size), B_TRUE);
810eda14cbcSMatt Macy } else {
811eda14cbcSMatt Macy /*
812eda14cbcSMatt Macy * We pass B_FALSE to abd_gang_add()
813eda14cbcSMatt Macy * because we did not allocate a new
814eda14cbcSMatt Macy * ABD, so it is assumed the caller
815eda14cbcSMatt Macy * will free this ABD.
816eda14cbcSMatt Macy */
817eda14cbcSMatt Macy abd_gang_add(aio->io_abd, dio->io_abd,
818eda14cbcSMatt Macy B_FALSE);
819eda14cbcSMatt Macy }
820eda14cbcSMatt Macy }
821eda14cbcSMatt Macy next_offset = dio->io_offset + dio->io_size;
822eda14cbcSMatt Macy } while (dio != last);
823eda14cbcSMatt Macy ASSERT3U(abd_get_size(aio->io_abd), ==, aio->io_size);
824eda14cbcSMatt Macy
825eda14cbcSMatt Macy /*
8262faf504dSMartin Matuska * Callers must call zio_vdev_io_bypass() and zio_execute() for
8272faf504dSMartin Matuska * aggregated (parent) I/Os so that we could avoid dropping the
8282faf504dSMartin Matuska * queue's lock here to avoid a deadlock that we could encounter
8292faf504dSMartin Matuska * due to lock order reversal between vq_lock and io_lock in
8302faf504dSMartin Matuska * zio_change_priority().
831eda14cbcSMatt Macy */
832eda14cbcSMatt Macy return (aio);
833eda14cbcSMatt Macy }
834eda14cbcSMatt Macy
835eda14cbcSMatt Macy static zio_t *
vdev_queue_io_to_issue(vdev_queue_t * vq)836eda14cbcSMatt Macy vdev_queue_io_to_issue(vdev_queue_t *vq)
837eda14cbcSMatt Macy {
838eda14cbcSMatt Macy zio_t *zio, *aio;
839eda14cbcSMatt Macy zio_priority_t p;
840eda14cbcSMatt Macy avl_index_t idx;
841eda14cbcSMatt Macy avl_tree_t *tree;
842eda14cbcSMatt Macy
843eda14cbcSMatt Macy again:
844eda14cbcSMatt Macy ASSERT(MUTEX_HELD(&vq->vq_lock));
845eda14cbcSMatt Macy
846eda14cbcSMatt Macy p = vdev_queue_class_to_issue(vq);
847eda14cbcSMatt Macy
848eda14cbcSMatt Macy if (p == ZIO_PRIORITY_NUM_QUEUEABLE) {
849eda14cbcSMatt Macy /* No eligible queued i/os */
850eda14cbcSMatt Macy return (NULL);
851eda14cbcSMatt Macy }
852eda14cbcSMatt Macy
8537b5e6873SMartin Matuska if (vdev_queue_class_fifo(p)) {
8547b5e6873SMartin Matuska zio = list_head(&vq->vq_class[p].vqc_list);
8557b5e6873SMartin Matuska } else {
856eda14cbcSMatt Macy /*
8577b5e6873SMartin Matuska * For LBA-ordered queues (async / scrub / initializing),
8587b5e6873SMartin Matuska * issue the I/O which follows the most recently issued I/O
8597b5e6873SMartin Matuska * in LBA (offset) order, but to avoid starvation only within
8607b5e6873SMartin Matuska * the same 0.5 second interval as the first I/O.
861eda14cbcSMatt Macy */
8627b5e6873SMartin Matuska tree = &vq->vq_class[p].vqc_tree;
8637b5e6873SMartin Matuska zio = aio = avl_first(tree);
8647b5e6873SMartin Matuska if (zio->io_offset < vq->vq_last_offset) {
8657b5e6873SMartin Matuska vq->vq_io_search.io_timestamp = zio->io_timestamp;
8667b5e6873SMartin Matuska vq->vq_io_search.io_offset = vq->vq_last_offset;
8677b5e6873SMartin Matuska zio = avl_find(tree, &vq->vq_io_search, &idx);
8687b5e6873SMartin Matuska if (zio == NULL) {
869eda14cbcSMatt Macy zio = avl_nearest(tree, idx, AVL_AFTER);
8707b5e6873SMartin Matuska if (zio == NULL ||
8717b5e6873SMartin Matuska (zio->io_timestamp >> VDQ_T_SHIFT) !=
8727b5e6873SMartin Matuska (aio->io_timestamp >> VDQ_T_SHIFT))
8737b5e6873SMartin Matuska zio = aio;
8747b5e6873SMartin Matuska }
8757b5e6873SMartin Matuska }
8767b5e6873SMartin Matuska }
877eda14cbcSMatt Macy ASSERT3U(zio->io_priority, ==, p);
878eda14cbcSMatt Macy
879eda14cbcSMatt Macy aio = vdev_queue_aggregate(vq, zio);
8802faf504dSMartin Matuska if (aio != NULL) {
881eda14cbcSMatt Macy zio = aio;
8822faf504dSMartin Matuska } else {
883eda14cbcSMatt Macy vdev_queue_io_remove(vq, zio);
884eda14cbcSMatt Macy
885eda14cbcSMatt Macy /*
8862faf504dSMartin Matuska * If the I/O is or was optional and therefore has no data, we
8872faf504dSMartin Matuska * need to simply discard it. We need to drop the vdev queue's
8882faf504dSMartin Matuska * lock to avoid a deadlock that we could encounter since this
8892faf504dSMartin Matuska * I/O will complete immediately.
890eda14cbcSMatt Macy */
891eda14cbcSMatt Macy if (zio->io_flags & ZIO_FLAG_NODATA) {
892eda14cbcSMatt Macy mutex_exit(&vq->vq_lock);
893eda14cbcSMatt Macy zio_vdev_io_bypass(zio);
894eda14cbcSMatt Macy zio_execute(zio);
895eda14cbcSMatt Macy mutex_enter(&vq->vq_lock);
896eda14cbcSMatt Macy goto again;
897eda14cbcSMatt Macy }
8982faf504dSMartin Matuska }
899eda14cbcSMatt Macy
900eda14cbcSMatt Macy vdev_queue_pending_add(vq, zio);
901eda14cbcSMatt Macy vq->vq_last_offset = zio->io_offset + zio->io_size;
902eda14cbcSMatt Macy
903eda14cbcSMatt Macy return (zio);
904eda14cbcSMatt Macy }
905eda14cbcSMatt Macy
906eda14cbcSMatt Macy zio_t *
vdev_queue_io(zio_t * zio)907eda14cbcSMatt Macy vdev_queue_io(zio_t *zio)
908eda14cbcSMatt Macy {
909eda14cbcSMatt Macy vdev_queue_t *vq = &zio->io_vd->vdev_queue;
9102faf504dSMartin Matuska zio_t *dio, *nio;
9112faf504dSMartin Matuska zio_link_t *zl = NULL;
912eda14cbcSMatt Macy
913eda14cbcSMatt Macy if (zio->io_flags & ZIO_FLAG_DONT_QUEUE)
914eda14cbcSMatt Macy return (zio);
915eda14cbcSMatt Macy
916eda14cbcSMatt Macy /*
917eda14cbcSMatt Macy * Children i/os inherent their parent's priority, which might
918eda14cbcSMatt Macy * not match the child's i/o type. Fix it up here.
919eda14cbcSMatt Macy */
920eda14cbcSMatt Macy if (zio->io_type == ZIO_TYPE_READ) {
921eda14cbcSMatt Macy ASSERT(zio->io_priority != ZIO_PRIORITY_TRIM);
922eda14cbcSMatt Macy
923eda14cbcSMatt Macy if (zio->io_priority != ZIO_PRIORITY_SYNC_READ &&
924eda14cbcSMatt Macy zio->io_priority != ZIO_PRIORITY_ASYNC_READ &&
925eda14cbcSMatt Macy zio->io_priority != ZIO_PRIORITY_SCRUB &&
926eda14cbcSMatt Macy zio->io_priority != ZIO_PRIORITY_REMOVAL &&
927eda14cbcSMatt Macy zio->io_priority != ZIO_PRIORITY_INITIALIZING &&
928eda14cbcSMatt Macy zio->io_priority != ZIO_PRIORITY_REBUILD) {
929eda14cbcSMatt Macy zio->io_priority = ZIO_PRIORITY_ASYNC_READ;
930eda14cbcSMatt Macy }
931eda14cbcSMatt Macy } else if (zio->io_type == ZIO_TYPE_WRITE) {
932eda14cbcSMatt Macy ASSERT(zio->io_priority != ZIO_PRIORITY_TRIM);
933eda14cbcSMatt Macy
934eda14cbcSMatt Macy if (zio->io_priority != ZIO_PRIORITY_SYNC_WRITE &&
935eda14cbcSMatt Macy zio->io_priority != ZIO_PRIORITY_ASYNC_WRITE &&
936eda14cbcSMatt Macy zio->io_priority != ZIO_PRIORITY_REMOVAL &&
937eda14cbcSMatt Macy zio->io_priority != ZIO_PRIORITY_INITIALIZING &&
938eda14cbcSMatt Macy zio->io_priority != ZIO_PRIORITY_REBUILD) {
939eda14cbcSMatt Macy zio->io_priority = ZIO_PRIORITY_ASYNC_WRITE;
940eda14cbcSMatt Macy }
941eda14cbcSMatt Macy } else {
942eda14cbcSMatt Macy ASSERT(zio->io_type == ZIO_TYPE_TRIM);
943eda14cbcSMatt Macy ASSERT(zio->io_priority == ZIO_PRIORITY_TRIM);
944eda14cbcSMatt Macy }
945eda14cbcSMatt Macy
9464e8d558cSMartin Matuska zio->io_flags |= ZIO_FLAG_DONT_QUEUE;
9477cd22ac4SMartin Matuska zio->io_timestamp = gethrtime();
948eda14cbcSMatt Macy
949eda14cbcSMatt Macy mutex_enter(&vq->vq_lock);
950eda14cbcSMatt Macy vdev_queue_io_add(vq, zio);
951eda14cbcSMatt Macy nio = vdev_queue_io_to_issue(vq);
952eda14cbcSMatt Macy mutex_exit(&vq->vq_lock);
953eda14cbcSMatt Macy
954eda14cbcSMatt Macy if (nio == NULL)
955eda14cbcSMatt Macy return (NULL);
956eda14cbcSMatt Macy
957eda14cbcSMatt Macy if (nio->io_done == vdev_queue_agg_io_done) {
9582faf504dSMartin Matuska while ((dio = zio_walk_parents(nio, &zl)) != NULL) {
9592faf504dSMartin Matuska ASSERT3U(dio->io_type, ==, nio->io_type);
9602faf504dSMartin Matuska zio_vdev_io_bypass(dio);
9612faf504dSMartin Matuska zio_execute(dio);
9622faf504dSMartin Matuska }
963eda14cbcSMatt Macy zio_nowait(nio);
964eda14cbcSMatt Macy return (NULL);
965eda14cbcSMatt Macy }
966eda14cbcSMatt Macy
967eda14cbcSMatt Macy return (nio);
968eda14cbcSMatt Macy }
969eda14cbcSMatt Macy
970eda14cbcSMatt Macy void
vdev_queue_io_done(zio_t * zio)971eda14cbcSMatt Macy vdev_queue_io_done(zio_t *zio)
972eda14cbcSMatt Macy {
973eda14cbcSMatt Macy vdev_queue_t *vq = &zio->io_vd->vdev_queue;
9742faf504dSMartin Matuska zio_t *dio, *nio;
9752faf504dSMartin Matuska zio_link_t *zl = NULL;
976eda14cbcSMatt Macy
9777cd22ac4SMartin Matuska hrtime_t now = gethrtime();
9787cd22ac4SMartin Matuska vq->vq_io_complete_ts = now;
9797cd22ac4SMartin Matuska vq->vq_io_delta_ts = zio->io_delta = now - zio->io_timestamp;
9807cd22ac4SMartin Matuska
981eda14cbcSMatt Macy mutex_enter(&vq->vq_lock);
982eda14cbcSMatt Macy vdev_queue_pending_remove(vq, zio);
983eda14cbcSMatt Macy
984eda14cbcSMatt Macy while ((nio = vdev_queue_io_to_issue(vq)) != NULL) {
985eda14cbcSMatt Macy mutex_exit(&vq->vq_lock);
986eda14cbcSMatt Macy if (nio->io_done == vdev_queue_agg_io_done) {
9872faf504dSMartin Matuska while ((dio = zio_walk_parents(nio, &zl)) != NULL) {
9882faf504dSMartin Matuska ASSERT3U(dio->io_type, ==, nio->io_type);
9892faf504dSMartin Matuska zio_vdev_io_bypass(dio);
9902faf504dSMartin Matuska zio_execute(dio);
9912faf504dSMartin Matuska }
992eda14cbcSMatt Macy zio_nowait(nio);
993eda14cbcSMatt Macy } else {
994eda14cbcSMatt Macy zio_vdev_io_reissue(nio);
995eda14cbcSMatt Macy zio_execute(nio);
996eda14cbcSMatt Macy }
997eda14cbcSMatt Macy mutex_enter(&vq->vq_lock);
998eda14cbcSMatt Macy }
999eda14cbcSMatt Macy
1000eda14cbcSMatt Macy mutex_exit(&vq->vq_lock);
1001eda14cbcSMatt Macy }
1002eda14cbcSMatt Macy
1003eda14cbcSMatt Macy void
vdev_queue_change_io_priority(zio_t * zio,zio_priority_t priority)1004eda14cbcSMatt Macy vdev_queue_change_io_priority(zio_t *zio, zio_priority_t priority)
1005eda14cbcSMatt Macy {
1006eda14cbcSMatt Macy vdev_queue_t *vq = &zio->io_vd->vdev_queue;
1007eda14cbcSMatt Macy
1008eda14cbcSMatt Macy /*
1009eda14cbcSMatt Macy * ZIO_PRIORITY_NOW is used by the vdev cache code and the aggregate zio
1010eda14cbcSMatt Macy * code to issue IOs without adding them to the vdev queue. In this
1011eda14cbcSMatt Macy * case, the zio is already going to be issued as quickly as possible
1012eda14cbcSMatt Macy * and so it doesn't need any reprioritization to help.
1013eda14cbcSMatt Macy */
1014eda14cbcSMatt Macy if (zio->io_priority == ZIO_PRIORITY_NOW)
1015eda14cbcSMatt Macy return;
1016eda14cbcSMatt Macy
1017eda14cbcSMatt Macy ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
1018eda14cbcSMatt Macy ASSERT3U(priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
1019eda14cbcSMatt Macy
1020eda14cbcSMatt Macy if (zio->io_type == ZIO_TYPE_READ) {
1021eda14cbcSMatt Macy if (priority != ZIO_PRIORITY_SYNC_READ &&
1022eda14cbcSMatt Macy priority != ZIO_PRIORITY_ASYNC_READ &&
1023eda14cbcSMatt Macy priority != ZIO_PRIORITY_SCRUB)
1024eda14cbcSMatt Macy priority = ZIO_PRIORITY_ASYNC_READ;
1025eda14cbcSMatt Macy } else {
1026eda14cbcSMatt Macy ASSERT(zio->io_type == ZIO_TYPE_WRITE);
1027eda14cbcSMatt Macy if (priority != ZIO_PRIORITY_SYNC_WRITE &&
1028eda14cbcSMatt Macy priority != ZIO_PRIORITY_ASYNC_WRITE)
1029eda14cbcSMatt Macy priority = ZIO_PRIORITY_ASYNC_WRITE;
1030eda14cbcSMatt Macy }
1031eda14cbcSMatt Macy
1032eda14cbcSMatt Macy mutex_enter(&vq->vq_lock);
1033eda14cbcSMatt Macy
1034eda14cbcSMatt Macy /*
1035eda14cbcSMatt Macy * If the zio is in none of the queues we can simply change
1036eda14cbcSMatt Macy * the priority. If the zio is waiting to be submitted we must
1037eda14cbcSMatt Macy * remove it from the queue and re-insert it with the new priority.
1038eda14cbcSMatt Macy * Otherwise, the zio is currently active and we cannot change its
1039eda14cbcSMatt Macy * priority.
1040eda14cbcSMatt Macy */
10417b5e6873SMartin Matuska if (zio->io_queue_state == ZIO_QS_QUEUED) {
10427b5e6873SMartin Matuska vdev_queue_class_remove(vq, zio);
1043eda14cbcSMatt Macy zio->io_priority = priority;
10447b5e6873SMartin Matuska vdev_queue_class_add(vq, zio);
10457b5e6873SMartin Matuska } else if (zio->io_queue_state == ZIO_QS_NONE) {
1046eda14cbcSMatt Macy zio->io_priority = priority;
1047eda14cbcSMatt Macy }
1048eda14cbcSMatt Macy
1049eda14cbcSMatt Macy mutex_exit(&vq->vq_lock);
1050eda14cbcSMatt Macy }
1051eda14cbcSMatt Macy
1052eda14cbcSMatt Macy /*
1053eda14cbcSMatt Macy * As these two methods are only used for load calculations we're not
1054eda14cbcSMatt Macy * concerned if we get an incorrect value on 32bit platforms due to lack of
1055eda14cbcSMatt Macy * vq_lock mutex use here, instead we prefer to keep it lock free for
1056eda14cbcSMatt Macy * performance.
1057eda14cbcSMatt Macy */
10587b5e6873SMartin Matuska uint32_t
vdev_queue_length(vdev_t * vd)1059eda14cbcSMatt Macy vdev_queue_length(vdev_t *vd)
1060eda14cbcSMatt Macy {
10617b5e6873SMartin Matuska return (vd->vdev_queue.vq_active);
1062eda14cbcSMatt Macy }
1063eda14cbcSMatt Macy
1064eda14cbcSMatt Macy uint64_t
vdev_queue_last_offset(vdev_t * vd)1065eda14cbcSMatt Macy vdev_queue_last_offset(vdev_t *vd)
1066eda14cbcSMatt Macy {
1067eda14cbcSMatt Macy return (vd->vdev_queue.vq_last_offset);
1068eda14cbcSMatt Macy }
1069eda14cbcSMatt Macy
10707b5e6873SMartin Matuska uint64_t
vdev_queue_class_length(vdev_t * vd,zio_priority_t p)10717b5e6873SMartin Matuska vdev_queue_class_length(vdev_t *vd, zio_priority_t p)
10727b5e6873SMartin Matuska {
10737b5e6873SMartin Matuska vdev_queue_t *vq = &vd->vdev_queue;
10747b5e6873SMartin Matuska if (vdev_queue_class_fifo(p))
1075*14c2e0a0SMartin Matuska return (vq->vq_class[p].vqc_list_numnodes);
10767b5e6873SMartin Matuska else
10777b5e6873SMartin Matuska return (avl_numnodes(&vq->vq_class[p].vqc_tree));
10787b5e6873SMartin Matuska }
10797b5e6873SMartin Matuska
1080be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, aggregation_limit, UINT, ZMOD_RW,
1081eda14cbcSMatt Macy "Max vdev I/O aggregation size");
1082eda14cbcSMatt Macy
1083be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, aggregation_limit_non_rotating, UINT,
1084c03c5b1cSMartin Matuska ZMOD_RW, "Max vdev I/O aggregation size for non-rotating media");
1085eda14cbcSMatt Macy
1086be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, read_gap_limit, UINT, ZMOD_RW,
1087eda14cbcSMatt Macy "Aggregate read I/O over gap");
1088eda14cbcSMatt Macy
1089be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, write_gap_limit, UINT, ZMOD_RW,
1090eda14cbcSMatt Macy "Aggregate write I/O over gap");
1091eda14cbcSMatt Macy
1092be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, max_active, UINT, ZMOD_RW,
1093eda14cbcSMatt Macy "Maximum number of active I/Os per vdev");
1094eda14cbcSMatt Macy
1095be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, async_write_active_max_dirty_percent,
1096be181ee2SMartin Matuska UINT, ZMOD_RW, "Async write concurrency max threshold");
1097eda14cbcSMatt Macy
1098be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, async_write_active_min_dirty_percent,
1099be181ee2SMartin Matuska UINT, ZMOD_RW, "Async write concurrency min threshold");
1100eda14cbcSMatt Macy
1101be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, async_read_max_active, UINT, ZMOD_RW,
1102eda14cbcSMatt Macy "Max active async read I/Os per vdev");
1103eda14cbcSMatt Macy
1104be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, async_read_min_active, UINT, ZMOD_RW,
1105eda14cbcSMatt Macy "Min active async read I/Os per vdev");
1106eda14cbcSMatt Macy
1107be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, async_write_max_active, UINT, ZMOD_RW,
1108eda14cbcSMatt Macy "Max active async write I/Os per vdev");
1109eda14cbcSMatt Macy
1110be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, async_write_min_active, UINT, ZMOD_RW,
1111eda14cbcSMatt Macy "Min active async write I/Os per vdev");
1112eda14cbcSMatt Macy
1113be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, initializing_max_active, UINT, ZMOD_RW,
1114eda14cbcSMatt Macy "Max active initializing I/Os per vdev");
1115eda14cbcSMatt Macy
1116be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, initializing_min_active, UINT, ZMOD_RW,
1117eda14cbcSMatt Macy "Min active initializing I/Os per vdev");
1118eda14cbcSMatt Macy
1119be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, removal_max_active, UINT, ZMOD_RW,
1120eda14cbcSMatt Macy "Max active removal I/Os per vdev");
1121eda14cbcSMatt Macy
1122be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, removal_min_active, UINT, ZMOD_RW,
1123eda14cbcSMatt Macy "Min active removal I/Os per vdev");
1124eda14cbcSMatt Macy
1125be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, scrub_max_active, UINT, ZMOD_RW,
1126eda14cbcSMatt Macy "Max active scrub I/Os per vdev");
1127eda14cbcSMatt Macy
1128be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, scrub_min_active, UINT, ZMOD_RW,
1129eda14cbcSMatt Macy "Min active scrub I/Os per vdev");
1130eda14cbcSMatt Macy
1131be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, sync_read_max_active, UINT, ZMOD_RW,
1132eda14cbcSMatt Macy "Max active sync read I/Os per vdev");
1133eda14cbcSMatt Macy
1134be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, sync_read_min_active, UINT, ZMOD_RW,
1135eda14cbcSMatt Macy "Min active sync read I/Os per vdev");
1136eda14cbcSMatt Macy
1137be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, sync_write_max_active, UINT, ZMOD_RW,
1138eda14cbcSMatt Macy "Max active sync write I/Os per vdev");
1139eda14cbcSMatt Macy
1140be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, sync_write_min_active, UINT, ZMOD_RW,
1141eda14cbcSMatt Macy "Min active sync write I/Os per vdev");
1142eda14cbcSMatt Macy
1143be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, trim_max_active, UINT, ZMOD_RW,
1144eda14cbcSMatt Macy "Max active trim/discard I/Os per vdev");
1145eda14cbcSMatt Macy
1146be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, trim_min_active, UINT, ZMOD_RW,
1147eda14cbcSMatt Macy "Min active trim/discard I/Os per vdev");
1148eda14cbcSMatt Macy
1149be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, rebuild_max_active, UINT, ZMOD_RW,
1150eda14cbcSMatt Macy "Max active rebuild I/Os per vdev");
1151eda14cbcSMatt Macy
1152be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, rebuild_min_active, UINT, ZMOD_RW,
1153eda14cbcSMatt Macy "Min active rebuild I/Os per vdev");
1154eda14cbcSMatt Macy
1155be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, nia_credit, UINT, ZMOD_RW,
11567877fdebSMatt Macy "Number of non-interactive I/Os to allow in sequence");
11577877fdebSMatt Macy
1158be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, nia_delay, UINT, ZMOD_RW,
11597877fdebSMatt Macy "Number of non-interactive I/Os before _max_active");
11607877fdebSMatt Macy
1161be181ee2SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, queue_depth_pct, UINT, ZMOD_RW,
1162eda14cbcSMatt Macy "Queue depth percentage for each top-level vdev");
1163d411c1d6SMartin Matuska
1164d411c1d6SMartin Matuska ZFS_MODULE_PARAM(zfs_vdev, zfs_vdev_, def_queue_depth, UINT, ZMOD_RW,
1165d411c1d6SMartin Matuska "Default queue depth for each allocator");
1166