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