vdev_queue.c revision 321574
14Srgrimes/*
2549Srgrimes * CDDL HEADER START
34Srgrimes *
44Srgrimes * The contents of this file are subject to the terms of the
54Srgrimes * Common Development and Distribution License (the "License").
64Srgrimes * You may not use this file except in compliance with the License.
74Srgrimes *
84Srgrimes * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
94Srgrimes * or http://www.opensolaris.org/os/licensing.
104Srgrimes * See the License for the specific language governing permissions
114Srgrimes * and limitations under the License.
124Srgrimes *
134Srgrimes * When distributing Covered Code, include this CDDL HEADER in each
144Srgrimes * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
154Srgrimes * If applicable, add the following below this CDDL HEADER, with the
164Srgrimes * fields enclosed by brackets "[]" replaced with your own identifying
174Srgrimes * information: Portions Copyright [yyyy] [name of copyright owner]
184Srgrimes *
194Srgrimes * CDDL HEADER END
204Srgrimes */
214Srgrimes/*
224Srgrimes * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
234Srgrimes * Use is subject to license terms.
244Srgrimes */
254Srgrimes
264Srgrimes/*
274Srgrimes * Copyright (c) 2012, 2017 by Delphix. All rights reserved.
284Srgrimes * Copyright (c) 2014 Integros [integros.com]
294Srgrimes */
304Srgrimes
314Srgrimes#include <sys/zfs_context.h>
324Srgrimes#include <sys/vdev_impl.h>
334Srgrimes#include <sys/spa_impl.h>
344Srgrimes#include <sys/zio.h>
354Srgrimes#include <sys/avl.h>
364Srgrimes#include <sys/dsl_pool.h>
37549Srgrimes#include <sys/metaslab_impl.h>
3825164Speter
394Srgrimes/*
404Srgrimes * ZFS I/O Scheduler
41556Srgrimes * ---------------
4213226Swollman *
4313228Swollman * ZFS issues I/O operations to leaf vdevs to satisfy and complete zios.  The
4413265Swollman * I/O scheduler determines when and in what order those operations are
4514328Speter * issued.  The I/O scheduler divides operations into six I/O classes
4614825Swollman * prioritized in the following order: sync read, sync write, async read,
4725164Speter * async write, scrub/resilver and trim.  Each queue defines the minimum and
4818252Sphk * maximum number of concurrent operations that may be issued to the device.
494Srgrimes * In addition, the device has an aggregate maximum. Note that the sum of the
501549Srgrimes * per-queue minimums must not exceed the aggregate maximum, and if the
511549Srgrimes * aggregate maximum is equal to or greater than the sum of the per-queue
5211390Sbde * maximums, the per-queue minimum has no effect.
531549Srgrimes *
541549Srgrimes * For many physical devices, throughput increases with the number of
551549Srgrimes * concurrent operations, but latency typically suffers. Further, physical
561549Srgrimes * devices typically have a limit at which more concurrent operations have no
571549Srgrimes * effect on throughput or can actually cause it to decrease.
581549Srgrimes *
591549Srgrimes * The scheduler selects the next operation to issue by first looking for an
601549Srgrimes * I/O class whose minimum has not been satisfied. Once all are satisfied and
611549Srgrimes * the aggregate maximum has not been hit, the scheduler looks for classes
621549Srgrimes * whose maximum has not been satisfied. Iteration through the I/O classes is
637090Sbde * done in the order specified above. No further operations are issued if the
641549Srgrimes * aggregate maximum number of concurrent operations has been hit or if there
652254Ssos * are no operations queued for an I/O class that has not hit its maximum.
661549Srgrimes * Every time an I/O is queued or an operation completes, the I/O scheduler
671549Srgrimes * looks for new operations to issue.
6812662Sdg *
694Srgrimes * All I/O classes have a fixed maximum number of outstanding operations
70556Srgrimes * except for the async write class. Asynchronous writes represent the data
712056Swollman * that is committed to stable storage during the syncing stage for
72556Srgrimes * transaction groups (see txg.c). Transaction groups enter the syncing state
73556Srgrimes * periodically so the number of queued async writes will quickly burst up and
74990Sdg * then bleed down to zero. Rather than servicing them as quickly as possible,
752056Swollman * the I/O scheduler changes the maximum number of active async write I/Os
76990Sdg * according to the amount of dirty data in the pool (see dsl_pool.c). Since
77990Sdg * both throughput and latency typically increase with the number of
78990Sdg * concurrent operations issued to physical devices, reducing the burstiness
792056Swollman * in the number of concurrent operations also stabilizes the response time of
80990Sdg * operations from other -- and in particular synchronous -- queues. In broad
81990Sdg * strokes, the I/O scheduler will issue more concurrent operations from the
822056Swollman * async write queue as there's more dirty data in the pool.
8312662Sdg *
8412662Sdg * Async Writes
8522521Sdyson *
862056Swollman * The number of concurrent operations issued for the async write I/O class
8712662Sdg * follows a piece-wise linear function defined by a few adjustable points.
882056Swollman *
8912662Sdg *        |                   o---------| <-- zfs_vdev_async_write_max_active
909507Sdg *   ^    |                  /^         |
9112662Sdg *   |    |                 / |         |
924Srgrimes * active |                /  |         |
9312662Sdg *  I/O   |               /   |         |
942056Swollman * count  |              /    |         |
952056Swollman *        |             /     |         |
96556Srgrimes *        |------------o      |         | <-- zfs_vdev_async_write_min_active
977090Sbde *       0|____________^______|_________|
987090Sbde *        0%           |      |       100% of zfs_dirty_data_max
992772Swollman *                     |      |
1002772Swollman *                     |      `-- zfs_vdev_async_write_active_max_dirty_percent
1012056Swollman *                     `--------- zfs_vdev_async_write_active_min_dirty_percent
1024193Sbde *
1032056Swollman * Until the amount of dirty data exceeds a minimum percentage of the dirty
1042056Swollman * data allowed in the pool, the I/O scheduler will limit the number of
1054193Sbde * concurrent operations to the minimum. As that threshold is crossed, the
1062056Swollman * number of concurrent operations issued increases linearly to the maximum at
1072056Swollman * the specified maximum percentage of the dirty data allowed in the pool.
1082056Swollman *
1094819Sphk * Ideally, the amount of dirty data on a busy pool will stay in the sloped
1107090Sbde * part of the function between zfs_vdev_async_write_active_min_dirty_percent
11125164Speter * and zfs_vdev_async_write_active_max_dirty_percent. If it exceeds the
11225164Speter * maximum percentage, this indicates that the rate of incoming data is
11325164Speter * greater than the rate that the backend storage can handle. In this case, we
11414825Swollman * must further throttle incoming writes (see dmu_tx_delay() for details).
11514825Swollman */
11614825Swollman
117556Srgrimes/*
1184193Sbde * The maximum number of I/Os active to each device.  Ideally, this will be >=
1192056Swollman * the sum of each queue's max_active.  It must be at least the sum of each
12011875Smarkm * queue's min_active.
1214Srgrimes */
12211390Sbdeuint32_t zfs_vdev_max_active = 1000;
12311390Sbde
12411390Sbde/*
12511390Sbde * Per-queue limits on the number of I/Os active to each device.  If the
12612929Sdg * sum of the queue's max_active is < zfs_vdev_max_active, then the
12710358Sjulian * min_active comes into play.  We will send min_active from each queue,
12824112Skato * and then select from queues in the order defined by zio_priority_t.
12917014Swollman *
13024112Skato * In general, smaller max_active's will lead to lower latency of synchronous
13124112Skato * operations.  Larger max_active's may lead to higher overall throughput,
13224112Skato * depending on underlying storage.
13325083Sjdp *
13413085Sdg * The ratio of the queues' max_actives determines the balance of performance
13510653Sdg * between reads, writes, and scrubs.  E.g., increasing
13610358Sjulian * zfs_vdev_scrub_max_active will cause the scrub or resilver to complete
13710358Sjulian * more quickly, but reads and writes to have higher latency and lower
1384Srgrimes * throughput.
1392426Sdg */
1402426Sdguint32_t zfs_vdev_sync_read_min_active = 10;
1412426Sdguint32_t zfs_vdev_sync_read_max_active = 10;
1421298Sdguint32_t zfs_vdev_sync_write_min_active = 10;
1431298Sdguint32_t zfs_vdev_sync_write_max_active = 10;
1441298Sdguint32_t zfs_vdev_async_read_min_active = 1;
1451298Sdguint32_t zfs_vdev_async_read_max_active = 3;
1461298Sdguint32_t zfs_vdev_async_write_min_active = 1;
1472426Sdguint32_t zfs_vdev_async_write_max_active = 10;
1482426Sdguint32_t zfs_vdev_scrub_min_active = 1;
1492426Sdguint32_t zfs_vdev_scrub_max_active = 2;
1501549Srgrimesuint32_t zfs_vdev_trim_min_active = 1;
151556Srgrimes/*
15215565Sphk * TRIM max active is large in comparison to the other values due to the fact
153556Srgrimes * that TRIM IOs are coalesced at the device layer. This value is set such
1542818Sache * that a typical SSD can process the queued IOs in a single request.
15512623Sphk */
15615392Sphkuint32_t zfs_vdev_trim_max_active = 64;
15712623Sphk
15812623Sphk
15912623Sphk/*
16012623Sphk * When the pool has less than zfs_vdev_async_write_active_min_dirty_percent
16112623Sphk * dirty data, use zfs_vdev_async_write_min_active.  When it has more than
16212623Sphk * zfs_vdev_async_write_active_max_dirty_percent, use
16312623Sphk * zfs_vdev_async_write_max_active. The value is linearly interpolated
16412623Sphk * between min and max.
16512623Sphk */
16612623Sphkint zfs_vdev_async_write_active_min_dirty_percent = 30;
16712623Sphkint zfs_vdev_async_write_active_max_dirty_percent = 60;
16812623Sphk
16912623Sphk/*
17012623Sphk * To reduce IOPs, we aggregate small adjacent I/Os into one large I/O.
17112623Sphk * For read I/Os, we also aggregate across small adjacency gaps; for writes
17212623Sphk * we include spans of optional I/Os to aid aggregation at the disk even when
17312623Sphk * they aren't able to help us aggregate at this level.
17412623Sphk */
17512623Sphkint zfs_vdev_aggregation_limit = SPA_OLD_MAXBLOCKSIZE;
17612623Sphkint zfs_vdev_read_gap_limit = 32 << 10;
17712623Sphkint zfs_vdev_write_gap_limit = 4 << 10;
17812623Sphk
17912722Sphk/*
18012722Sphk * Define the queue depth percentage for each top-level. This percentage is
1814Srgrimes * used in conjunction with zfs_vdev_async_max_active to determine how many
1824Srgrimes * allocations a specific top-level vdev should handle. Once the queue depth
1834Srgrimes * reaches zfs_vdev_queue_depth_pct * zfs_vdev_async_write_max_active / 100
1849744Sdg * then allocator will stop allocating blocks on that top-level device.
185974Sdg * The default kernel setting is 1000% which will yield 100 allocations per
1869578Sdg * device. For userland testing, the default setting is 300% which equates
1879578Sdg * to 30 allocations per device.
1889578Sdg */
18917559Swollman#ifdef _KERNEL
1908426Swollmanint zfs_vdev_queue_depth_pct = 1000;
19112722Sphk#else
1921549Srgrimesint zfs_vdev_queue_depth_pct = 300;
19312722Sphk#endif
19417559Swollman
195556Srgrimes
1961549Srgrimes#ifdef __FreeBSD__
1971549Srgrimes#ifdef _KERNEL
19810358SjulianSYSCTL_DECL(_vfs_zfs_vdev);
19911390Sbde
20011390Sbdestatic int sysctl_zfs_async_write_active_min_dirty_percent(SYSCTL_HANDLER_ARGS);
2014SrgrimesSYSCTL_PROC(_vfs_zfs_vdev, OID_AUTO, async_write_active_min_dirty_percent,
2024Srgrimes    CTLTYPE_UINT | CTLFLAG_MPSAFE | CTLFLAG_RWTUN, 0, sizeof(int),
2034Srgrimes    sysctl_zfs_async_write_active_min_dirty_percent, "I",
2043489Sphk    "Percentage of async write dirty data below which "
205798Swollman    "async_write_min_active is used.");
20612243Sphk
2073502Sphkstatic int sysctl_zfs_async_write_active_max_dirty_percent(SYSCTL_HANDLER_ARGS);
2084SrgrimesSYSCTL_PROC(_vfs_zfs_vdev, OID_AUTO, async_write_active_max_dirty_percent,
2094819Sphk    CTLTYPE_UINT | CTLFLAG_MPSAFE | CTLFLAG_RWTUN, 0, sizeof(int),
2104819Sphk    sysctl_zfs_async_write_active_max_dirty_percent, "I",
2114819Sphk    "Percentage of async write dirty data above which "
2124Srgrimes    "async_write_max_active is used.");
2134Srgrimes
2144SrgrimesSYSCTL_UINT(_vfs_zfs_vdev, OID_AUTO, max_active, CTLFLAG_RWTUN,
215556Srgrimes    &zfs_vdev_max_active, 0,
21625164Speter    "The maximum number of I/Os of all types active for each device.");
21725164Speter
21825164Speter#define ZFS_VDEV_QUEUE_KNOB_MIN(name)					\
21917014SwollmanSYSCTL_UINT(_vfs_zfs_vdev, OID_AUTO, name ## _min_active, CTLFLAG_RWTUN,\
2202014Swollman    &zfs_vdev_ ## name ## _min_active, 0,				\
22124112Skato    "Initial number of I/O requests of type " #name			\
22224112Skato    " active for each device");
22317014Swollman
22417014Swollman#define ZFS_VDEV_QUEUE_KNOB_MAX(name)					\
22517014SwollmanSYSCTL_UINT(_vfs_zfs_vdev, OID_AUTO, name ## _max_active, CTLFLAG_RWTUN,\
22610616Sdg    &zfs_vdev_ ## name ## _max_active, 0,				\
2279578Sdg    "Maximum number of I/O requests of type " #name			\
2289578Sdg    " active for each device");
2299578Sdg
2309578SdgZFS_VDEV_QUEUE_KNOB_MIN(sync_read);
2319578SdgZFS_VDEV_QUEUE_KNOB_MAX(sync_read);
2324SrgrimesZFS_VDEV_QUEUE_KNOB_MIN(sync_write);
2339578SdgZFS_VDEV_QUEUE_KNOB_MAX(sync_write);
2349578SdgZFS_VDEV_QUEUE_KNOB_MIN(async_read);
2359578SdgZFS_VDEV_QUEUE_KNOB_MAX(async_read);
2369578SdgZFS_VDEV_QUEUE_KNOB_MIN(async_write);
2379578SdgZFS_VDEV_QUEUE_KNOB_MAX(async_write);
2389578SdgZFS_VDEV_QUEUE_KNOB_MIN(scrub);
2399578SdgZFS_VDEV_QUEUE_KNOB_MAX(scrub);
2409578SdgZFS_VDEV_QUEUE_KNOB_MIN(trim);
2419578SdgZFS_VDEV_QUEUE_KNOB_MAX(trim);
2429578Sdg
24312290Sphk#undef ZFS_VDEV_QUEUE_KNOB
2449578Sdg
2459578SdgSYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, aggregation_limit, CTLFLAG_RWTUN,
2469578Sdg    &zfs_vdev_aggregation_limit, 0,
2479578Sdg    "I/O requests are aggregated up to this size");
2484SrgrimesSYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, read_gap_limit, CTLFLAG_RWTUN,
24917559Swollman    &zfs_vdev_read_gap_limit, 0,
25017559Swollman    "Acceptable gap between two reads being aggregated");
25117559SwollmanSYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, write_gap_limit, CTLFLAG_RWTUN,
25217559Swollman    &zfs_vdev_write_gap_limit, 0,
25317559Swollman    "Acceptable gap between two writes being aggregated");
2544SrgrimesSYSCTL_INT(_vfs_zfs_vdev, OID_AUTO, queue_depth_pct, CTLFLAG_RWTUN,
2554Srgrimes    &zfs_vdev_queue_depth_pct, 0,
2564Srgrimes    "Queue depth percentage for each top-level");
2574Srgrimes
2584Srgrimesstatic int
2594Srgrimessysctl_zfs_async_write_active_min_dirty_percent(SYSCTL_HANDLER_ARGS)
2604Srgrimes{
2614Srgrimes	int val, err;
2624Srgrimes
2634Srgrimes	val = zfs_vdev_async_write_active_min_dirty_percent;
2644Srgrimes	err = sysctl_handle_int(oidp, &val, 0, req);
2654Srgrimes	if (err != 0 || req->newptr == NULL)
2664Srgrimes		return (err);
2674Srgrimes
2684Srgrimes	if (val < 0 || val > 100 ||
2694Srgrimes	    val >= zfs_vdev_async_write_active_max_dirty_percent)
2704Srgrimes		return (EINVAL);
2714Srgrimes
2724Srgrimes	zfs_vdev_async_write_active_min_dirty_percent = val;
2734Srgrimes
2744Srgrimes	return (0);
2754Srgrimes}
2764Srgrimes
2774Srgrimesstatic int
2784Srgrimessysctl_zfs_async_write_active_max_dirty_percent(SYSCTL_HANDLER_ARGS)
2794Srgrimes{
280990Sdg	int val, err;
281990Sdg
282990Sdg	val = zfs_vdev_async_write_active_max_dirty_percent;
283990Sdg	err = sysctl_handle_int(oidp, &val, 0, req);
284990Sdg	if (err != 0 || req->newptr == NULL)
285990Sdg		return (err);
286990Sdg
287990Sdg	if (val < 0 || val > 100 ||
288990Sdg	    val <= zfs_vdev_async_write_active_min_dirty_percent)
289990Sdg		return (EINVAL);
290990Sdg
291990Sdg	zfs_vdev_async_write_active_max_dirty_percent = val;
2924Srgrimes
2935837Sdg	return (0);
2946327Sdg}
2955837Sdg#endif
29620578Sdg#endif
2975837Sdg
29819828Sdysonint
2995455Sdgvdev_queue_offset_compare(const void *x1, const void *x2)
3004Srgrimes{
3014Srgrimes	const zio_t *z1 = x1;
3024Srgrimes	const zio_t *z2 = x2;
3032422Sdg
3044Srgrimes	if (z1->io_offset < z2->io_offset)
3051298Sdg		return (-1);
3061298Sdg	if (z1->io_offset > z2->io_offset)
3071298Sdg		return (1);
3085455Sdg
3095455Sdg	if (z1 < z2)
3105455Sdg		return (-1);
31124852Sdyson	if (z1 > z2)
31224852Sdyson		return (1);
3135455Sdg
31415583Sphk	return (0);
3151298Sdg}
3161298Sdg
3171298Sdgstatic inline avl_tree_t *
3181298Sdgvdev_queue_class_tree(vdev_queue_t *vq, zio_priority_t p)
3191298Sdg{
3204Srgrimes	return (&vq->vq_class[p].vqc_queued_tree);
3214Srgrimes}
3224Srgrimes
3234Srgrimesstatic inline avl_tree_t *
3244Srgrimesvdev_queue_type_tree(vdev_queue_t *vq, zio_type_t t)
3254Srgrimes{
3264Srgrimes	if (t == ZIO_TYPE_READ)
3274Srgrimes		return (&vq->vq_read_offset_tree);
3284Srgrimes	else if (t == ZIO_TYPE_WRITE)
3291321Sdg		return (&vq->vq_write_offset_tree);
3304Srgrimes	else
3314Srgrimes		return (NULL);
3324Srgrimes}
3334Srgrimes
3344Srgrimesint
335556Srgrimesvdev_queue_timestamp_compare(const void *x1, const void *x2)
3362426Sdg{
3371549Srgrimes	const zio_t *z1 = x1;
33820146Sdyson	const zio_t *z2 = x2;
3391887Sdg
3402426Sdg	if (z1->io_timestamp < z2->io_timestamp)
3412426Sdg		return (-1);
3422426Sdg	if (z1->io_timestamp > z2->io_timestamp)
34320146Sdyson		return (1);
3442426Sdg
3451887Sdg	if (z1->io_offset < z2->io_offset)
34620146Sdyson		return (-1);
3471887Sdg	if (z1->io_offset > z2->io_offset)
3481887Sdg		return (1);
3495455Sdg
3505455Sdg	if (z1 < z2)
3515455Sdg		return (-1);
3525455Sdg	if (z1 > z2)
3531549Srgrimes		return (1);
35425164Speter
35525164Speter	return (0);
35625164Speter}
35725164Speter
35825164Spetervoid
35925164Spetervdev_queue_init(vdev_t *vd)
36025164Speter{
36125164Speter	vdev_queue_t *vq = &vd->vdev_queue;
36225164Speter
36325164Speter	mutex_init(&vq->vq_lock, NULL, MUTEX_DEFAULT, NULL);
36425164Speter	vq->vq_vdev = vd;
36525164Speter
36625164Speter	avl_create(&vq->vq_active_tree, vdev_queue_offset_compare,
36725164Speter	    sizeof (zio_t), offsetof(struct zio, io_queue_node));
36825164Speter	avl_create(vdev_queue_type_tree(vq, ZIO_TYPE_READ),
36925164Speter	    vdev_queue_offset_compare, sizeof (zio_t),
37025164Speter	    offsetof(struct zio, io_offset_node));
3714Srgrimes	avl_create(vdev_queue_type_tree(vq, ZIO_TYPE_WRITE),
3724Srgrimes	    vdev_queue_offset_compare, sizeof (zio_t),
3734Srgrimes	    offsetof(struct zio, io_offset_node));
3744Srgrimes
37515722Swollman	for (zio_priority_t p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
37621737Sdg		int (*compfn) (const void *, const void *);
37721737Sdg
37821737Sdg		/*
37921737Sdg		 * The synchronous i/o queues are dispatched in FIFO rather
38021737Sdg		 * than LBA order.  This provides more consistent latency for
38121737Sdg		 * these i/os.
38221737Sdg		 */
38321737Sdg		if (p == ZIO_PRIORITY_SYNC_READ || p == ZIO_PRIORITY_SYNC_WRITE)
38415722Swollman			compfn = vdev_queue_timestamp_compare;
38515722Swollman		else
3864Srgrimes			compfn = vdev_queue_offset_compare;
3874Srgrimes
3884Srgrimes		avl_create(vdev_queue_class_tree(vq, p), compfn,
3894Srgrimes		    sizeof (zio_t), offsetof(struct zio, io_queue_node));
3904Srgrimes	}
3914Srgrimes
3924475Sbde	vq->vq_lastoffset = 0;
39320471Sjkh}
39420471Sjkh
39520471Sjkhvoid
39620471Sjkhvdev_queue_fini(vdev_t *vd)
39720471Sjkh{
39818702Sjkh	vdev_queue_t *vq = &vd->vdev_queue;
3993907Sjkh
40010666Sbde	for (zio_priority_t p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++)
40110666Sbde		avl_destroy(vdev_queue_class_tree(vq, p));
40220471Sjkh	avl_destroy(&vq->vq_active_tree);
4034Srgrimes	avl_destroy(vdev_queue_type_tree(vq, ZIO_TYPE_READ));
4042422Sdg	avl_destroy(vdev_queue_type_tree(vq, ZIO_TYPE_WRITE));
4054Srgrimes
4061298Sdg	mutex_destroy(&vq->vq_lock);
4071298Sdg}
4081298Sdg
4091298Sdgstatic void
41010782Sdgvdev_queue_io_add(vdev_queue_t *vq, zio_t *zio)
41110616Sdg{
41210616Sdg	spa_t *spa = zio->io_spa;
4131298Sdg	avl_tree_t *qtt;
4141298Sdg
4154Srgrimes	ASSERT(MUTEX_HELD(&vq->vq_lock));
4164Srgrimes	ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
4174Srgrimes	avl_add(vdev_queue_class_tree(vq, zio->io_priority), zio);
4181887Sdg	qtt = vdev_queue_type_tree(vq, zio->io_type);
4194Srgrimes	if (qtt)
4204Srgrimes		avl_add(qtt, zio);
42117559Swollman
42217559Swollman#ifdef illumos
42317559Swollman	mutex_enter(&spa->spa_iokstat_lock);
42417559Swollman	spa->spa_queue_stats[zio->io_priority].spa_queued++;
42517559Swollman	if (spa->spa_iokstat != NULL)
42617559Swollman		kstat_waitq_enter(spa->spa_iokstat->ks_data);
42717559Swollman	mutex_exit(&spa->spa_iokstat_lock);
42817559Swollman#endif
42917559Swollman}
43017559Swollman
43117559Swollmanstatic void
43217559Swollmanvdev_queue_io_remove(vdev_queue_t *vq, zio_t *zio)
43317559Swollman{
43417559Swollman	spa_t *spa = zio->io_spa;
43517559Swollman	avl_tree_t *qtt;
43617559Swollman
43717559Swollman	ASSERT(MUTEX_HELD(&vq->vq_lock));
43817559Swollman	ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
43917559Swollman	avl_remove(vdev_queue_class_tree(vq, zio->io_priority), zio);
44017559Swollman	qtt = vdev_queue_type_tree(vq, zio->io_type);
44117559Swollman	if (qtt)
44217559Swollman		avl_remove(qtt, zio);
44317559Swollman
44417559Swollman#ifdef illumos
44517559Swollman	mutex_enter(&spa->spa_iokstat_lock);
44617559Swollman	ASSERT3U(spa->spa_queue_stats[zio->io_priority].spa_queued, >, 0);
44717559Swollman	spa->spa_queue_stats[zio->io_priority].spa_queued--;
4484Srgrimes	if (spa->spa_iokstat != NULL)
4494Srgrimes		kstat_waitq_exit(spa->spa_iokstat->ks_data);
4504Srgrimes	mutex_exit(&spa->spa_iokstat_lock);
4514Srgrimes#endif
45214331Speter}
4534Srgrimes
4544Srgrimesstatic void
4554Srgrimesvdev_queue_pending_add(vdev_queue_t *vq, zio_t *zio)
4564Srgrimes{
4574Srgrimes	spa_t *spa = zio->io_spa;
4584Srgrimes	ASSERT(MUTEX_HELD(&vq->vq_lock));
4594Srgrimes	ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
4604Srgrimes	vq->vq_class[zio->io_priority].vqc_active++;
4614Srgrimes	avl_add(&vq->vq_active_tree, zio);
46214503Shsu
4634Srgrimes#ifdef illumos
4644Srgrimes	mutex_enter(&spa->spa_iokstat_lock);
4654Srgrimes	spa->spa_queue_stats[zio->io_priority].spa_active++;
4664Srgrimes	if (spa->spa_iokstat != NULL)
4676846Sdg		kstat_runq_enter(spa->spa_iokstat->ks_data);
4681549Srgrimes	mutex_exit(&spa->spa_iokstat_lock);
4693306Sphk#endif
4704Srgrimes}
4711549Srgrimes
47214331Speterstatic void
4734Srgrimesvdev_queue_pending_remove(vdev_queue_t *vq, zio_t *zio)
47414331Speter{
4754Srgrimes	spa_t *spa = zio->io_spa;
47614331Speter	ASSERT(MUTEX_HELD(&vq->vq_lock));
4771549Srgrimes	ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
4785999Sats	vq->vq_class[zio->io_priority].vqc_active--;
4791549Srgrimes	avl_remove(&vq->vq_active_tree, zio);
48014331Speter
4814Srgrimes#ifdef illumos
48214331Speter	mutex_enter(&spa->spa_iokstat_lock);
4834Srgrimes	ASSERT3U(spa->spa_queue_stats[zio->io_priority].spa_active, >, 0);
4844Srgrimes	spa->spa_queue_stats[zio->io_priority].spa_active--;
4851127Sdg	if (spa->spa_iokstat != NULL) {
4861127Sdg		kstat_io_t *ksio = spa->spa_iokstat->ks_data;
4871127Sdg
4881127Sdg		kstat_runq_exit(spa->spa_iokstat->ks_data);
4891127Sdg		if (zio->io_type == ZIO_TYPE_READ) {
4901127Sdg			ksio->reads++;
4911127Sdg			ksio->nread += zio->io_size;
4924Srgrimes		} else if (zio->io_type == ZIO_TYPE_WRITE) {
4934Srgrimes			ksio->writes++;
4944Srgrimes			ksio->nwritten += zio->io_size;
4954Srgrimes		}
4964Srgrimes	}
4974Srgrimes	mutex_exit(&spa->spa_iokstat_lock);
4984Srgrimes#endif
4994Srgrimes}
5004Srgrimes
5014Srgrimesstatic void
5024Srgrimesvdev_queue_agg_io_done(zio_t *aio)
5034Srgrimes{
5044Srgrimes	if (aio->io_type == ZIO_TYPE_READ) {
5058876Srgrimes		zio_t *pio;
5064Srgrimes		zio_link_t *zl = NULL;
5074Srgrimes		while ((pio = zio_walk_parents(aio, &zl)) != NULL) {
5082254Ssos			bcopy((char *)aio->io_data + (pio->io_offset -
5092254Ssos			    aio->io_offset), pio->io_data, pio->io_size);
5102254Ssos		}
5112254Ssos	}
5122254Ssos
5132254Ssos	zio_buf_free(aio->io_data, aio->io_size);
5146846Sdg}
5156846Sdg
5166846Sdgstatic int
5176846Sdgvdev_queue_class_min_active(zio_priority_t p)
5186846Sdg{
5194Srgrimes	switch (p) {
5204Srgrimes	case ZIO_PRIORITY_SYNC_READ:
5216846Sdg		return (zfs_vdev_sync_read_min_active);
5226846Sdg	case ZIO_PRIORITY_SYNC_WRITE:
5236846Sdg		return (zfs_vdev_sync_write_min_active);
5246846Sdg	case ZIO_PRIORITY_ASYNC_READ:
5256846Sdg		return (zfs_vdev_async_read_min_active);
5266846Sdg	case ZIO_PRIORITY_ASYNC_WRITE:
5276846Sdg		return (zfs_vdev_async_write_min_active);
5286846Sdg	case ZIO_PRIORITY_SCRUB:
5296846Sdg		return (zfs_vdev_scrub_min_active);
5306846Sdg	case ZIO_PRIORITY_TRIM:
5316846Sdg		return (zfs_vdev_trim_min_active);
532924Sdg	default:
5334Srgrimes		panic("invalid priority %u", p);
5344Srgrimes		return (0);
5354Srgrimes	}
5366846Sdg}
5376846Sdg
5386846Sdgstatic __noinline int
5396846Sdgvdev_queue_max_async_writes(spa_t *spa)
5406846Sdg{
5416846Sdg	int writes;
5426846Sdg	uint64_t dirty = spa->spa_dsl_pool->dp_dirty_total;
5436846Sdg	uint64_t min_bytes = zfs_dirty_data_max *
5446846Sdg	    zfs_vdev_async_write_active_min_dirty_percent / 100;
5456846Sdg	uint64_t max_bytes = zfs_dirty_data_max *
5466846Sdg	    zfs_vdev_async_write_active_max_dirty_percent / 100;
5476846Sdg
5486846Sdg	/*
5496846Sdg	 * Sync tasks correspond to interactive user actions. To reduce the
5506846Sdg	 * execution time of those actions we push data out as fast as possible.
5516846Sdg	 */
5526846Sdg	if (spa_has_pending_synctask(spa)) {
5536846Sdg		return (zfs_vdev_async_write_max_active);
554924Sdg	}
55514331Speter
5561208Shsu	if (dirty < min_bytes)
5571066Sdg		return (zfs_vdev_async_write_min_active);
5581066Sdg	if (dirty > max_bytes)
5591066Sdg		return (zfs_vdev_async_write_max_active);
5601066Sdg
5614Srgrimes	/*
5624Srgrimes	 * linear interpolation:
5634Srgrimes	 * slope = (max_writes - min_writes) / (max_bytes - min_bytes)
5644Srgrimes	 * move right by min_bytes
5654Srgrimes	 * move up by min_writes
5664Srgrimes	 */
5674Srgrimes	writes = (dirty - min_bytes) *
5684Srgrimes	    (zfs_vdev_async_write_max_active -
5694Srgrimes	    zfs_vdev_async_write_min_active) /
5705675Sbde	    (max_bytes - min_bytes) +
5714Srgrimes	    zfs_vdev_async_write_min_active;
572798Swollman	ASSERT3U(writes, >=, zfs_vdev_async_write_min_active);
5734Srgrimes	ASSERT3U(writes, <=, zfs_vdev_async_write_max_active);
5744Srgrimes	return (writes);
57511390Sbde}
57611390Sbde
57711390Sbdestatic int
5784Srgrimesvdev_queue_class_max_active(spa_t *spa, zio_priority_t p)
5794Srgrimes{
5804Srgrimes	switch (p) {
5814Srgrimes	case ZIO_PRIORITY_SYNC_READ:
5821549Srgrimes		return (zfs_vdev_sync_read_max_active);
5831208Shsu	case ZIO_PRIORITY_SYNC_WRITE:
5844Srgrimes		return (zfs_vdev_sync_write_max_active);
5854Srgrimes	case ZIO_PRIORITY_ASYNC_READ:
586924Sdg		return (zfs_vdev_async_read_max_active);
5874Srgrimes	case ZIO_PRIORITY_ASYNC_WRITE:
5884Srgrimes		return (vdev_queue_max_async_writes(spa));
5894Srgrimes	case ZIO_PRIORITY_SCRUB:
5904Srgrimes		return (zfs_vdev_scrub_max_active);
5914Srgrimes	case ZIO_PRIORITY_TRIM:
5924Srgrimes		return (zfs_vdev_trim_max_active);
5934Srgrimes	default:
5944Srgrimes		panic("invalid priority %u", p);
59520998Sdyson		return (0);
59624283Smpp	}
5974Srgrimes}
5985603Sbde
5995675Sbde/*
6005603Sbde * Return the i/o class to issue from, or ZIO_PRIORITY_MAX_QUEUEABLE if
6015603Sbde * there is no eligible class.
6021208Shsu */
6035675Sbdestatic zio_priority_t
6045675Sbdevdev_queue_class_to_issue(vdev_queue_t *vq)
6055675Sbde{
6065675Sbde	spa_t *spa = vq->vq_vdev->vdev_spa;
6075675Sbde	zio_priority_t p;
6085675Sbde
6095675Sbde	ASSERT(MUTEX_HELD(&vq->vq_lock));
6105675Sbde
6115675Sbde	if (avl_numnodes(&vq->vq_active_tree) >= zfs_vdev_max_active)
6125675Sbde		return (ZIO_PRIORITY_NUM_QUEUEABLE);
6135675Sbde
6141208Shsu	/* find a queue that has not reached its minimum # outstanding i/os */
6155603Sbde	for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
6161208Shsu		if (avl_numnodes(vdev_queue_class_tree(vq, p)) > 0 &&
6171208Shsu		    vq->vq_class[p].vqc_active <
6181208Shsu		    vdev_queue_class_min_active(p))
6191208Shsu			return (p);
6201208Shsu	}
6215603Sbde
6225603Sbde	/*
6235603Sbde	 * If we haven't found a queue, look for one that hasn't reached its
6241208Shsu	 * maximum # outstanding i/os.
6255603Sbde	 */
6265603Sbde	for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
6271208Shsu		if (avl_numnodes(vdev_queue_class_tree(vq, p)) > 0 &&
6285603Sbde		    vq->vq_class[p].vqc_active <
6291208Shsu		    vdev_queue_class_max_active(spa, p))
6301208Shsu			return (p);
6311208Shsu	}
6321208Shsu
6331208Shsu	/* No eligible queued i/os */
6344Srgrimes	return (ZIO_PRIORITY_NUM_QUEUEABLE);
6351051Sdg}
6361051Sdg
6371051Sdg/*
6381051Sdg * Compute the range spanned by two i/os, which is the endpoint of the last
6391051Sdg * (lio->io_offset + lio->io_size) minus start of the first (fio->io_offset).
6401051Sdg * Conveniently, the gap between fio and lio is given by -IO_SPAN(lio, fio);
6411051Sdg * thus fio and lio are adjacent if and only if IO_SPAN(lio, fio) == 0.
6421051Sdg */
6431051Sdg#define	IO_SPAN(fio, lio) ((lio)->io_offset + (lio)->io_size - (fio)->io_offset)
6441051Sdg#define	IO_GAP(fio, lio) (-IO_SPAN(lio, fio))
6451051Sdg
6464Srgrimesstatic zio_t *
64720998Sdysonvdev_queue_aggregate(vdev_queue_t *vq, zio_t *zio)
6484Srgrimes{
6491208Shsu	zio_t *first, *last, *aio, *dio, *mandatory, *nio;
6501549Srgrimes	void *abuf;
65114331Speter	uint64_t maxgap = 0;
6521549Srgrimes	uint64_t size;
65314331Speter	boolean_t stretch;
65420016Sbde	avl_tree_t *t;
655924Sdg	enum zio_flag flags;
656924Sdg
657924Sdg	ASSERT(MUTEX_HELD(&vq->vq_lock));
6581208Shsu
6594Srgrimes	if (zio->io_flags & ZIO_FLAG_DONT_AGGREGATE)
6604Srgrimes		return (NULL);
6614Srgrimes
66212722Sphk	first = last = zio;
66323070Salex
66412722Sphk	if (zio->io_type == ZIO_TYPE_READ)
66523070Salex		maxgap = zfs_vdev_read_gap_limit;
66617677Sjulian
66712722Sphk	/*
66817677Sjulian	 * We can aggregate I/Os that are sufficiently adjacent and of
66917677Sjulian	 * the same flavor, as expressed by the AGG_INHERIT flags.
6704Srgrimes	 * The latter requirement is necessary so that certain
6714Srgrimes	 * attributes of the I/O, such as whether it's a normal I/O
6724Srgrimes	 * or a scrub/resilver, can be preserved in the aggregate.
6734Srgrimes	 * We can include optional I/Os, but don't allow them
67419274Sjulian	 * to begin a range as they add no benefit in that situation.
67519274Sjulian	 */
67619274Sjulian
67719274Sjulian	/*
67819274Sjulian	 * We keep track of the last non-optional I/O.
67919274Sjulian	 */
68019274Sjulian	mandatory = (first->io_flags & ZIO_FLAG_OPTIONAL) ? NULL : first;
68119274Sjulian
68219274Sjulian	/*
68319274Sjulian	 * Walk backwards through sufficiently contiguous I/Os
6844Srgrimes	 * recording the last non-optional I/O.
6854Srgrimes	 */
686556Srgrimes	flags = zio->io_flags & ZIO_FLAG_AGG_INHERIT;
687924Sdg	t = vdev_queue_type_tree(vq, zio->io_type);
6884Srgrimes	while (t != NULL && (dio = AVL_PREV(t, first)) != NULL &&
6894Srgrimes	    (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags &&
690924Sdg	    IO_SPAN(dio, last) <= zfs_vdev_aggregation_limit &&
6914Srgrimes	    IO_GAP(dio, first) <= maxgap) {
6924201Sdg		first = dio;
6934Srgrimes		if (mandatory == NULL && !(first->io_flags & ZIO_FLAG_OPTIONAL))
69414348Sjkh			mandatory = first;
69514348Sjkh	}
69614348Sjkh
69714348Sjkh	/*
69814348Sjkh	 * Skip any initial optional I/Os.
69914348Sjkh	 */
70014348Sjkh	while ((first->io_flags & ZIO_FLAG_OPTIONAL) && first != last) {
70114348Sjkh		first = AVL_NEXT(t, first);
70214348Sjkh		ASSERT(first != NULL);
70314348Sjkh	}
70414348Sjkh
70514348Sjkh	/*
70614348Sjkh	 * Walk forward through sufficiently contiguous I/Os.
7074201Sdg	 * The aggregation limit does not apply to optional i/os, so that
7084201Sdg	 * we can issue contiguous writes even if they are larger than the
7094201Sdg	 * aggregation limit.
7105603Sbde	 */
7114201Sdg	while ((dio = AVL_NEXT(t, last)) != NULL &&
7124201Sdg	    (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags &&
7134201Sdg	    (IO_SPAN(first, dio) <= zfs_vdev_aggregation_limit ||
7144201Sdg	    (dio->io_flags & ZIO_FLAG_OPTIONAL)) &&
7154201Sdg	    IO_GAP(last, dio) <= maxgap) {
71621975Sbde		last = dio;
71721975Sbde		if (!(last->io_flags & ZIO_FLAG_OPTIONAL))
71821975Sbde			mandatory = last;
71921975Sbde	}
72021975Sbde
72121975Sbde	/*
72221975Sbde	 * Now that we've established the range of the I/O aggregation
72321975Sbde	 * we must decide what to do with trailing optional I/Os.
72421975Sbde	 * For reads, there's nothing to do. While we are unable to
72521975Sbde	 * aggregate further, it's possible that a trailing optional
72621975Sbde	 * I/O would allow the underlying device to aggregate with
72721975Sbde	 * subsequent I/Os. We must therefore determine if the next
72821975Sbde	 * non-optional I/O is close enough to make aggregation
72921975Sbde	 * worthwhile.
73021975Sbde	 */
73121975Sbde	stretch = B_FALSE;
73221975Sbde	if (zio->io_type == ZIO_TYPE_WRITE && mandatory != NULL) {
73321975Sbde		zio_t *nio = last;
73421975Sbde		while ((dio = AVL_NEXT(t, nio)) != NULL &&
73521975Sbde		    IO_GAP(nio, dio) == 0 &&
73621975Sbde		    IO_GAP(mandatory, dio) <= zfs_vdev_write_gap_limit) {
73721975Sbde			nio = dio;
7384Srgrimes			if (!(nio->io_flags & ZIO_FLAG_OPTIONAL)) {
73921975Sbde				stretch = B_TRUE;
7404Srgrimes				break;
7414Srgrimes			}
74212186Sphk		}
74312186Sphk	}
7441549Srgrimes
7454038Sache	if (stretch) {
74612186Sphk		/*
74712243Sphk		 * We are going to include an optional io in our aggregated
74812243Sphk		 * span, thus closing the write gap.  Only mandatory i/os can
74912186Sphk		 * start aggregated spans, so make sure that the next i/o
75012186Sphk		 * after our span is mandatory.
75112186Sphk		 */
7521549Srgrimes		dio = AVL_NEXT(t, last);
75312623Sphk		dio->io_flags &= ~ZIO_FLAG_OPTIONAL;
75412623Sphk	} else {
7551549Srgrimes		/* do not include the optional i/o */
75612186Sphk		while (last != mandatory && last != first) {
75712186Sphk			ASSERT(last->io_flags & ZIO_FLAG_OPTIONAL);
75812186Sphk			last = AVL_PREV(t, last);
75912186Sphk			ASSERT(last != NULL);
76012186Sphk		}
76112186Sphk	}
76215045Sache
76315045Sache	if (first == last)
76415045Sache		return (NULL);
7651549Srgrimes
7664Srgrimes	size = IO_SPAN(first, last);
7674Srgrimes	ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
7684Srgrimes
7694Srgrimes	abuf = zio_buf_alloc_nowait(size);
7704Srgrimes	if (abuf == NULL)
7714Srgrimes		return (NULL);
7724Srgrimes
7734501Sbde	aio = zio_vdev_delegated_io(first->io_vd, first->io_offset,
7744501Sbde	    abuf, size, first->io_type, zio->io_priority,
77525164Speter	    flags | ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE,
77625164Speter	    vdev_queue_agg_io_done, NULL);
77725164Speter	aio->io_timestamp = first->io_timestamp;
7784475Sbde
77925164Speter	nio = first;
7801051Sdg	do {
7814501Sbde		dio = nio;
78225164Speter		nio = AVL_NEXT(t, dio);
78325164Speter		ASSERT3U(dio->io_type, ==, aio->io_type);
78425164Speter
78525164Speter		if (dio->io_flags & ZIO_FLAG_NODATA) {
78625164Speter			ASSERT3U(dio->io_type, ==, ZIO_TYPE_WRITE);
78725164Speter			bzero((char *)aio->io_data + (dio->io_offset -
78825164Speter			    aio->io_offset), dio->io_size);
78925164Speter		} else if (dio->io_type == ZIO_TYPE_WRITE) {
79025164Speter			bcopy(dio->io_data, (char *)aio->io_data +
79124690Speter			    (dio->io_offset - aio->io_offset),
79225164Speter			    dio->io_size);
7934Srgrimes		}
79412929Sdg
79512977Sbde		zio_add_child(dio, aio);
7964Srgrimes		vdev_queue_io_remove(vq, dio);
7974Srgrimes		zio_vdev_io_bypass(dio);
7984Srgrimes		zio_execute(dio);
79924691Speter	} while (dio != last);
80024691Speter
80124691Speter	return (aio);
80224691Speter}
80324691Speter
804556Srgrimesstatic zio_t *
80525164Spetervdev_queue_io_to_issue(vdev_queue_t *vq)
80625164Speter{
80725164Speter	zio_t *zio, *aio;
80825164Speter	zio_priority_t p;
80925164Speter	avl_index_t idx;
8103489Sphk	avl_tree_t *tree;
8114Srgrimes	zio_t search;
812556Srgrimes
8134Srgrimesagain:
8144Srgrimes	ASSERT(MUTEX_HELD(&vq->vq_lock));
8154Srgrimes
816556Srgrimes	p = vdev_queue_class_to_issue(vq);
8174Srgrimes
8184Srgrimes	if (p == ZIO_PRIORITY_NUM_QUEUEABLE) {
8193489Sphk		/* No eligible queued i/os */
8204Srgrimes		return (NULL);
8214Srgrimes	}
8224Srgrimes
8234Srgrimes	/*
8244Srgrimes	 * For LBA-ordered queues (async / scrub), issue the i/o which follows
825556Srgrimes	 * the most recently issued i/o in LBA (offset) order.
8264Srgrimes	 *
8274Srgrimes	 * For FIFO queues (sync), issue the i/o with the lowest timestamp.
8283489Sphk	 */
8294Srgrimes	tree = vdev_queue_class_tree(vq, p);
8304Srgrimes	search.io_timestamp = 0;
8314Srgrimes	search.io_offset = vq->vq_last_offset + 1;
8324Srgrimes	VERIFY3P(avl_find(tree, &search, &idx), ==, NULL);
8334Srgrimes	zio = avl_nearest(tree, idx, AVL_AFTER);
834556Srgrimes	if (zio == NULL)
8354Srgrimes		zio = avl_first(tree);
8364Srgrimes	ASSERT3U(zio->io_priority, ==, p);
8373489Sphk
8383489Sphk	aio = vdev_queue_aggregate(vq, zio);
8394Srgrimes	if (aio != NULL)
8404Srgrimes		zio = aio;
84124690Speter	else
8424Srgrimes		vdev_queue_io_remove(vq, zio);
843556Srgrimes
8444Srgrimes	/*
8454Srgrimes	 * If the I/O is or was optional and therefore has no data, we need to
8463489Sphk	 * simply discard it. We need to drop the vdev queue's lock to avoid a
8474Srgrimes	 * deadlock that we could encounter since this I/O will complete
8484Srgrimes	 * immediately.
8494Srgrimes	 */
8504Srgrimes	if (zio->io_flags & ZIO_FLAG_NODATA) {
8514Srgrimes		mutex_exit(&vq->vq_lock);
852556Srgrimes		zio_vdev_io_bypass(zio);
8534Srgrimes		zio_execute(zio);
8544Srgrimes		mutex_enter(&vq->vq_lock);
8553489Sphk		goto again;
85612929Sdg	}
85712929Sdg
8584Srgrimes	vdev_queue_pending_add(vq, zio);
8594Srgrimes	vq->vq_last_offset = zio->io_offset;
8604Srgrimes
861556Srgrimes	return (zio);
8624Srgrimes}
8634Srgrimes
8643489Sphkzio_t *
86525164Spetervdev_queue_io(zio_t *zio)
86625164Speter{
86725164Speter	vdev_queue_t *vq = &zio->io_vd->vdev_queue;
86825164Speter	zio_t *nio;
86925164Speter
87025164Speter	if (zio->io_flags & ZIO_FLAG_DONT_QUEUE)
87112929Sdg		return (zio);
8724Srgrimes
8734Srgrimes	/*
8744Srgrimes	 * Children i/os inherent their parent's priority, which might
875556Srgrimes	 * not match the child's i/o type.  Fix it up here.
8764Srgrimes	 */
8771051Sdg	if (zio->io_type == ZIO_TYPE_READ) {
8783489Sphk		if (zio->io_priority != ZIO_PRIORITY_SYNC_READ &&
8793258Sdg		    zio->io_priority != ZIO_PRIORITY_ASYNC_READ &&
8801051Sdg		    zio->io_priority != ZIO_PRIORITY_SCRUB)
8811051Sdg			zio->io_priority = ZIO_PRIORITY_ASYNC_READ;
8821051Sdg	} else if (zio->io_type == ZIO_TYPE_WRITE) {
8831051Sdg		if (zio->io_priority != ZIO_PRIORITY_SYNC_WRITE &&
8841051Sdg		    zio->io_priority != ZIO_PRIORITY_ASYNC_WRITE)
8851051Sdg			zio->io_priority = ZIO_PRIORITY_ASYNC_WRITE;
8861051Sdg	} else {
8873489Sphk		ASSERT(zio->io_type == ZIO_TYPE_FREE);
8883258Sdg		zio->io_priority = ZIO_PRIORITY_TRIM;
8893489Sphk	}
8903258Sdg
8913258Sdg	zio->io_flags |= ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE;
8923258Sdg
8933258Sdg	mutex_enter(&vq->vq_lock);
8943258Sdg	zio->io_timestamp = gethrtime();
8953489Sphk	vdev_queue_io_add(vq, zio);
8963489Sphk	nio = vdev_queue_io_to_issue(vq);
8973258Sdg	mutex_exit(&vq->vq_lock);
8983489Sphk
8993258Sdg	if (nio == NULL)
9003258Sdg		return (NULL);
9013258Sdg
9023258Sdg	if (nio->io_done == vdev_queue_agg_io_done) {
9033258Sdg		zio_nowait(nio);
9043489Sphk		return (NULL);
9053489Sphk	}
9063258Sdg
9073489Sphk	return (nio);
9083258Sdg}
9093258Sdg
9103258Sdgvoid
9113258Sdgvdev_queue_io_done(zio_t *zio)
9123258Sdg{
9133489Sphk	vdev_queue_t *vq = &zio->io_vd->vdev_queue;
9141051Sdg	zio_t *nio;
9154Srgrimes
91612722Sphk	mutex_enter(&vq->vq_lock);
9174Srgrimes
9184Srgrimes	vdev_queue_pending_remove(vq, zio);
9194Srgrimes
9204Srgrimes	vq->vq_io_complete_ts = gethrtime();
9214Srgrimes
9224Srgrimes	while ((nio = vdev_queue_io_to_issue(vq)) != NULL) {
923556Srgrimes		mutex_exit(&vq->vq_lock);
9244Srgrimes		if (nio->io_done == vdev_queue_agg_io_done) {
9254Srgrimes			zio_nowait(nio);
9264Srgrimes		} else {
9274Srgrimes			zio_vdev_io_reissue(nio);
9284Srgrimes			zio_execute(nio);
9294Srgrimes		}
9304Srgrimes		mutex_enter(&vq->vq_lock);
9314Srgrimes	}
932556Srgrimes
9334Srgrimes	mutex_exit(&vq->vq_lock);
9344Srgrimes}
9354Srgrimes
9364Srgrimes/*
9374Srgrimes * As these three methods are only used for load calculations we're not concerned
9384Srgrimes * if we get an incorrect value on 32bit platforms due to lack of vq_lock mutex
9394Srgrimes * use here, instead we prefer to keep it lock free for performance.
9404Srgrimes */
941556Srgrimesint
9424Srgrimesvdev_queue_length(vdev_t *vd)
9434Srgrimes{
9444Srgrimes	return (avl_numnodes(&vd->vdev_queue.vq_active_tree));
9454Srgrimes}
9464Srgrimes
9474Srgrimesuint64_t
9484Srgrimesvdev_queue_lastoffset(vdev_t *vd)
9494Srgrimes{
950556Srgrimes	return (vd->vdev_queue.vq_lastoffset);
9514Srgrimes}
9524Srgrimes
9534Srgrimesvoid
9544Srgrimesvdev_queue_register_lastoffset(vdev_t *vd, zio_t *zio)
9554Srgrimes{
9564Srgrimes	vd->vdev_queue.vq_lastoffset = zio->io_offset + zio->io_size;
9574Srgrimes}
9584Srgrimes