fa9e4066f08beec538e775443c5be79dd423fcabahrens/*
fa9e4066f08beec538e775443c5be79dd423fcabahrens * CDDL HEADER START
fa9e4066f08beec538e775443c5be79dd423fcabahrens *
fa9e4066f08beec538e775443c5be79dd423fcabahrens * The contents of this file are subject to the terms of the
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock * Common Development and Distribution License (the "License").
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock * You may not use this file except in compliance with the License.
fa9e4066f08beec538e775443c5be79dd423fcabahrens *
fa9e4066f08beec538e775443c5be79dd423fcabahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
fa9e4066f08beec538e775443c5be79dd423fcabahrens * or http://www.opensolaris.org/os/licensing.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * See the License for the specific language governing permissions
fa9e4066f08beec538e775443c5be79dd423fcabahrens * and limitations under the License.
fa9e4066f08beec538e775443c5be79dd423fcabahrens *
fa9e4066f08beec538e775443c5be79dd423fcabahrens * When distributing Covered Code, include this CDDL HEADER in each
fa9e4066f08beec538e775443c5be79dd423fcabahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * If applicable, add the following below this CDDL HEADER, with the
fa9e4066f08beec538e775443c5be79dd423fcabahrens * fields enclosed by brackets "[]" replaced with your own identifying
fa9e4066f08beec538e775443c5be79dd423fcabahrens * information: Portions Copyright [yyyy] [name of copyright owner]
fa9e4066f08beec538e775443c5be79dd423fcabahrens *
fa9e4066f08beec538e775443c5be79dd423fcabahrens * CDDL HEADER END
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
fa9e4066f08beec538e775443c5be79dd423fcabahrens/*
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Use is subject to license terms.
fa9e4066f08beec538e775443c5be79dd423fcabahrens */
fa9e4066f08beec538e775443c5be79dd423fcabahrens
283b84606b6fc326692c03273de1774e8c122f9aGeorge.Wilson/*
73527f441cbbd953fa42cc5a30a413bad75f24a9Alex Reece * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
c3d26abc9ee97b4f60233556aadeb57e0bd30bb9Matthew Ahrens * Copyright (c) 2014 Integros [integros.com]
283b84606b6fc326692c03273de1774e8c122f9aGeorge.Wilson */
283b84606b6fc326692c03273de1774e8c122f9aGeorge.Wilson
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/zfs_context.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/vdev_impl.h>
c3a66015199e1814c2af8f7e6929af2004302e7eMatthew Ahrens#include <sys/spa_impl.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/zio.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens#include <sys/avl.h>
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens#include <sys/dsl_pool.h>
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson#include <sys/metaslab_impl.h>
fa9e4066f08beec538e775443c5be79dd423fcabahrens
614409b5be5411058e7e9b6cc93dddaff9fb13f7ahrens/*
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * ZFS I/O Scheduler
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * ---------------
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens *
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * ZFS issues I/O operations to leaf vdevs to satisfy and complete zios. The
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * I/O scheduler determines when and in what order those operations are
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * issued. The I/O scheduler divides operations into five I/O classes
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * prioritized in the following order: sync read, sync write, async read,
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * async write, and scrub/resilver. Each queue defines the minimum and
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * maximum number of concurrent operations that may be issued to the device.
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * In addition, the device has an aggregate maximum. Note that the sum of the
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * per-queue minimums must not exceed the aggregate maximum, and if the
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * aggregate maximum is equal to or greater than the sum of the per-queue
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * maximums, the per-queue minimum has no effect.
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens *
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * For many physical devices, throughput increases with the number of
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * concurrent operations, but latency typically suffers. Further, physical
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * devices typically have a limit at which more concurrent operations have no
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * effect on throughput or can actually cause it to decrease.
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens *
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * The scheduler selects the next operation to issue by first looking for an
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * I/O class whose minimum has not been satisfied. Once all are satisfied and
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * the aggregate maximum has not been hit, the scheduler looks for classes
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * whose maximum has not been satisfied. Iteration through the I/O classes is
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * done in the order specified above. No further operations are issued if the
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * aggregate maximum number of concurrent operations has been hit or if there
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * are no operations queued for an I/O class that has not hit its maximum.
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * Every time an i/o is queued or an operation completes, the I/O scheduler
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * looks for new operations to issue.
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens *
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * All I/O classes have a fixed maximum number of outstanding operations
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * except for the async write class. Asynchronous writes represent the data
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * that is committed to stable storage during the syncing stage for
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * transaction groups (see txg.c). Transaction groups enter the syncing state
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * periodically so the number of queued async writes will quickly burst up and
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * then bleed down to zero. Rather than servicing them as quickly as possible,
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * the I/O scheduler changes the maximum number of active async write i/os
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * according to the amount of dirty data in the pool (see dsl_pool.c). Since
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * both throughput and latency typically increase with the number of
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * concurrent operations issued to physical devices, reducing the burstiness
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * in the number of concurrent operations also stabilizes the response time of
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * operations from other -- and in particular synchronous -- queues. In broad
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * strokes, the I/O scheduler will issue more concurrent operations from the
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * async write queue as there's more dirty data in the pool.
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens *
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * Async Writes
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens *
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * The number of concurrent operations issued for the async write I/O class
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * follows a piece-wise linear function defined by a few adjustable points.
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens *
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * | o---------| <-- zfs_vdev_async_write_max_active
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * ^ | /^ |
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * | | / | |
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * active | / | |
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * I/O | / | |
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * count | / | |
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * | / | |
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * |------------o | | <-- zfs_vdev_async_write_min_active
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * 0|____________^______|_________|
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * 0% | | 100% of zfs_dirty_data_max
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * | |
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * | `-- zfs_vdev_async_write_active_max_dirty_percent
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * `--------- zfs_vdev_async_write_active_min_dirty_percent
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens *
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * Until the amount of dirty data exceeds a minimum percentage of the dirty
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * data allowed in the pool, the I/O scheduler will limit the number of
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * concurrent operations to the minimum. As that threshold is crossed, the
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * number of concurrent operations issued increases linearly to the maximum at
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * the specified maximum percentage of the dirty data allowed in the pool.
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens *
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * Ideally, the amount of dirty data on a busy pool will stay in the sloped
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * part of the function between zfs_vdev_async_write_active_min_dirty_percent
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * and zfs_vdev_async_write_active_max_dirty_percent. If it exceeds the
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * maximum percentage, this indicates that the rate of incoming data is
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * greater than the rate that the backend storage can handle. In this case, we
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * must further throttle incoming writes (see dmu_tx_delay() for details).
614409b5be5411058e7e9b6cc93dddaff9fb13f7ahrens */
f7170741490edba9d1d9c697c177c887172bc741Will Andrews
614409b5be5411058e7e9b6cc93dddaff9fb13f7ahrens/*
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * The maximum number of i/os active to each device. Ideally, this will be >=
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * the sum of each queue's max_active. It must be at least the sum of each
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * queue's min_active.
614409b5be5411058e7e9b6cc93dddaff9fb13f7ahrens */
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrensuint32_t zfs_vdev_max_active = 1000;
614409b5be5411058e7e9b6cc93dddaff9fb13f7ahrens
c55e05cb35da47582b7afd38734d2f0d9c6deb40Matthew Ahrens/*
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * Per-queue limits on the number of i/os active to each device. If the
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * sum of the queue's max_active is < zfs_vdev_max_active, then the
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * min_active comes into play. We will send min_active from each queue,
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * and then select from queues in the order defined by zio_priority_t.
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens *
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * In general, smaller max_active's will lead to lower latency of synchronous
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * operations. Larger max_active's may lead to higher overall throughput,
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * depending on underlying storage.
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens *
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * The ratio of the queues' max_actives determines the balance of performance
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * between reads, writes, and scrubs. E.g., increasing
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * zfs_vdev_scrub_max_active will cause the scrub or resilver to complete
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * more quickly, but reads and writes to have higher latency and lower
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * throughput.
c55e05cb35da47582b7afd38734d2f0d9c6deb40Matthew Ahrens */
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrensuint32_t zfs_vdev_sync_read_min_active = 10;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrensuint32_t zfs_vdev_sync_read_max_active = 10;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrensuint32_t zfs_vdev_sync_write_min_active = 10;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrensuint32_t zfs_vdev_sync_write_max_active = 10;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrensuint32_t zfs_vdev_async_read_min_active = 1;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrensuint32_t zfs_vdev_async_read_max_active = 3;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrensuint32_t zfs_vdev_async_write_min_active = 1;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrensuint32_t zfs_vdev_async_write_max_active = 10;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrensuint32_t zfs_vdev_scrub_min_active = 1;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrensuint32_t zfs_vdev_scrub_max_active = 2;
614409b5be5411058e7e9b6cc93dddaff9fb13f7ahrens
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens/*
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * When the pool has less than zfs_vdev_async_write_active_min_dirty_percent
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * dirty data, use zfs_vdev_async_write_min_active. When it has more than
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * zfs_vdev_async_write_active_max_dirty_percent, use
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * zfs_vdev_async_write_max_active. The value is linearly interpolated
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * between min and max.
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens */
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrensint zfs_vdev_async_write_active_min_dirty_percent = 30;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrensint zfs_vdev_async_write_active_max_dirty_percent = 60;
614409b5be5411058e7e9b6cc93dddaff9fb13f7ahrens
614409b5be5411058e7e9b6cc93dddaff9fb13f7ahrens/*
f94275ce205810a201404c5f35f4cc96057022b1Adam Leventhal * To reduce IOPs, we aggregate small adjacent I/Os into one large I/O.
f94275ce205810a201404c5f35f4cc96057022b1Adam Leventhal * For read I/Os, we also aggregate across small adjacency gaps; for writes
f94275ce205810a201404c5f35f4cc96057022b1Adam Leventhal * we include spans of optional I/Os to aid aggregation at the disk even when
f94275ce205810a201404c5f35f4cc96057022b1Adam Leventhal * they aren't able to help us aggregate at this level.
614409b5be5411058e7e9b6cc93dddaff9fb13f7ahrens */
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrensint zfs_vdev_aggregation_limit = SPA_OLD_MAXBLOCKSIZE;
6f708f7c79bbe8e239e0f588bb846b4ccbc14bf3Jeff Bonwickint zfs_vdev_read_gap_limit = 32 << 10;
f94275ce205810a201404c5f35f4cc96057022b1Adam Leventhalint zfs_vdev_write_gap_limit = 4 << 10;
614409b5be5411058e7e9b6cc93dddaff9fb13f7ahrens
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson/*
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * Define the queue depth percentage for each top-level. This percentage is
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * used in conjunction with zfs_vdev_async_max_active to determine how many
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * allocations a specific top-level vdev should handle. Once the queue depth
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * reaches zfs_vdev_queue_depth_pct * zfs_vdev_async_write_max_active / 100
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * then allocator will stop allocating blocks on that top-level device.
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * The default kernel setting is 1000% which will yield 100 allocations per
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * device. For userland testing, the default setting is 300% which equates
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson * to 30 allocations per device.
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson */
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson#ifdef _KERNEL
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilsonint zfs_vdev_queue_depth_pct = 1000;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson#else
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilsonint zfs_vdev_queue_depth_pct = 300;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson#endif
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
fa9e4066f08beec538e775443c5be79dd423fcabahrensint
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrensvdev_queue_offset_compare(const void *x1, const void *x2)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens const zio_t *z1 = x1;
fa9e4066f08beec538e775443c5be79dd423fcabahrens const zio_t *z2 = x2;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (z1->io_offset < z2->io_offset)
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (-1);
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (z1->io_offset > z2->io_offset)
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (1);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (z1 < z2)
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (-1);
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (z1 > z2)
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (1);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (0);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fe319232d24f4ae183730a5a24a09423d8ab4429Justin T. Gibbsstatic inline avl_tree_t *
fe319232d24f4ae183730a5a24a09423d8ab4429Justin T. Gibbsvdev_queue_class_tree(vdev_queue_t *vq, zio_priority_t p)
fe319232d24f4ae183730a5a24a09423d8ab4429Justin T. Gibbs{
fe319232d24f4ae183730a5a24a09423d8ab4429Justin T. Gibbs return (&vq->vq_class[p].vqc_queued_tree);
fe319232d24f4ae183730a5a24a09423d8ab4429Justin T. Gibbs}
fe319232d24f4ae183730a5a24a09423d8ab4429Justin T. Gibbs
fe319232d24f4ae183730a5a24a09423d8ab4429Justin T. Gibbsstatic inline avl_tree_t *
fe319232d24f4ae183730a5a24a09423d8ab4429Justin T. Gibbsvdev_queue_type_tree(vdev_queue_t *vq, zio_type_t t)
fe319232d24f4ae183730a5a24a09423d8ab4429Justin T. Gibbs{
fe319232d24f4ae183730a5a24a09423d8ab4429Justin T. Gibbs ASSERT(t == ZIO_TYPE_READ || t == ZIO_TYPE_WRITE);
fe319232d24f4ae183730a5a24a09423d8ab4429Justin T. Gibbs if (t == ZIO_TYPE_READ)
fe319232d24f4ae183730a5a24a09423d8ab4429Justin T. Gibbs return (&vq->vq_read_offset_tree);
fe319232d24f4ae183730a5a24a09423d8ab4429Justin T. Gibbs else
fe319232d24f4ae183730a5a24a09423d8ab4429Justin T. Gibbs return (&vq->vq_write_offset_tree);
fe319232d24f4ae183730a5a24a09423d8ab4429Justin T. Gibbs}
fe319232d24f4ae183730a5a24a09423d8ab4429Justin T. Gibbs
fa9e4066f08beec538e775443c5be79dd423fcabahrensint
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrensvdev_queue_timestamp_compare(const void *x1, const void *x2)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens const zio_t *z1 = x1;
fa9e4066f08beec538e775443c5be79dd423fcabahrens const zio_t *z2 = x2;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens if (z1->io_timestamp < z2->io_timestamp)
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (-1);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens if (z1->io_timestamp > z2->io_timestamp)
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (1);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (z1 < z2)
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (-1);
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (z1 > z2)
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (1);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (0);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrensvoid
fa9e4066f08beec538e775443c5be79dd423fcabahrensvdev_queue_init(vdev_t *vd)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens vdev_queue_t *vq = &vd->vdev_queue;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_init(&vq->vq_lock, NULL, MUTEX_DEFAULT, NULL);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens vq->vq_vdev = vd;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens avl_create(&vq->vq_active_tree, vdev_queue_offset_compare,
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens sizeof (zio_t), offsetof(struct zio, io_queue_node));
fe319232d24f4ae183730a5a24a09423d8ab4429Justin T. Gibbs avl_create(vdev_queue_type_tree(vq, ZIO_TYPE_READ),
fe319232d24f4ae183730a5a24a09423d8ab4429Justin T. Gibbs vdev_queue_offset_compare, sizeof (zio_t),
fe319232d24f4ae183730a5a24a09423d8ab4429Justin T. Gibbs offsetof(struct zio, io_offset_node));
fe319232d24f4ae183730a5a24a09423d8ab4429Justin T. Gibbs avl_create(vdev_queue_type_tree(vq, ZIO_TYPE_WRITE),
fe319232d24f4ae183730a5a24a09423d8ab4429Justin T. Gibbs vdev_queue_offset_compare, sizeof (zio_t),
fe319232d24f4ae183730a5a24a09423d8ab4429Justin T. Gibbs offsetof(struct zio, io_offset_node));
fa9e4066f08beec538e775443c5be79dd423fcabahrens
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens for (zio_priority_t p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
fe319232d24f4ae183730a5a24a09423d8ab4429Justin T. Gibbs int (*compfn) (const void *, const void *);
fe319232d24f4ae183730a5a24a09423d8ab4429Justin T. Gibbs
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens /*
fe319232d24f4ae183730a5a24a09423d8ab4429Justin T. Gibbs * The synchronous i/o queues are dispatched in FIFO rather
fe319232d24f4ae183730a5a24a09423d8ab4429Justin T. Gibbs * than LBA order. This provides more consistent latency for
fe319232d24f4ae183730a5a24a09423d8ab4429Justin T. Gibbs * these i/os.
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens */
fe319232d24f4ae183730a5a24a09423d8ab4429Justin T. Gibbs if (p == ZIO_PRIORITY_SYNC_READ || p == ZIO_PRIORITY_SYNC_WRITE)
fe319232d24f4ae183730a5a24a09423d8ab4429Justin T. Gibbs compfn = vdev_queue_timestamp_compare;
fe319232d24f4ae183730a5a24a09423d8ab4429Justin T. Gibbs else
fe319232d24f4ae183730a5a24a09423d8ab4429Justin T. Gibbs compfn = vdev_queue_offset_compare;
fe319232d24f4ae183730a5a24a09423d8ab4429Justin T. Gibbs
fe319232d24f4ae183730a5a24a09423d8ab4429Justin T. Gibbs avl_create(vdev_queue_class_tree(vq, p), compfn,
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens sizeof (zio_t), offsetof(struct zio, io_queue_node));
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrensvoid
fa9e4066f08beec538e775443c5be79dd423fcabahrensvdev_queue_fini(vdev_t *vd)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens vdev_queue_t *vq = &vd->vdev_queue;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens for (zio_priority_t p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++)
fe319232d24f4ae183730a5a24a09423d8ab4429Justin T. Gibbs avl_destroy(vdev_queue_class_tree(vq, p));
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens avl_destroy(&vq->vq_active_tree);
fe319232d24f4ae183730a5a24a09423d8ab4429Justin T. Gibbs avl_destroy(vdev_queue_type_tree(vq, ZIO_TYPE_READ));
fe319232d24f4ae183730a5a24a09423d8ab4429Justin T. Gibbs avl_destroy(vdev_queue_type_tree(vq, ZIO_TYPE_WRITE));
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_destroy(&vq->vq_lock);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrockstatic void
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrockvdev_queue_io_add(vdev_queue_t *vq, zio_t *zio)
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock{
c3a66015199e1814c2af8f7e6929af2004302e7eMatthew Ahrens spa_t *spa = zio->io_spa;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
fe319232d24f4ae183730a5a24a09423d8ab4429Justin T. Gibbs avl_add(vdev_queue_class_tree(vq, zio->io_priority), zio);
fe319232d24f4ae183730a5a24a09423d8ab4429Justin T. Gibbs avl_add(vdev_queue_type_tree(vq, zio->io_type), zio);
c3a66015199e1814c2af8f7e6929af2004302e7eMatthew Ahrens
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens mutex_enter(&spa->spa_iokstat_lock);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens spa->spa_queue_stats[zio->io_priority].spa_queued++;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens if (spa->spa_iokstat != NULL)
c3a66015199e1814c2af8f7e6929af2004302e7eMatthew Ahrens kstat_waitq_enter(spa->spa_iokstat->ks_data);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens mutex_exit(&spa->spa_iokstat_lock);
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock}
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrockstatic void
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrockvdev_queue_io_remove(vdev_queue_t *vq, zio_t *zio)
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock{
c3a66015199e1814c2af8f7e6929af2004302e7eMatthew Ahrens spa_t *spa = zio->io_spa;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
fe319232d24f4ae183730a5a24a09423d8ab4429Justin T. Gibbs avl_remove(vdev_queue_class_tree(vq, zio->io_priority), zio);
fe319232d24f4ae183730a5a24a09423d8ab4429Justin T. Gibbs avl_remove(vdev_queue_type_tree(vq, zio->io_type), zio);
c3a66015199e1814c2af8f7e6929af2004302e7eMatthew Ahrens
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens mutex_enter(&spa->spa_iokstat_lock);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens ASSERT3U(spa->spa_queue_stats[zio->io_priority].spa_queued, >, 0);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens spa->spa_queue_stats[zio->io_priority].spa_queued--;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens if (spa->spa_iokstat != NULL)
c3a66015199e1814c2af8f7e6929af2004302e7eMatthew Ahrens kstat_waitq_exit(spa->spa_iokstat->ks_data);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens mutex_exit(&spa->spa_iokstat_lock);
c3a66015199e1814c2af8f7e6929af2004302e7eMatthew Ahrens}
c3a66015199e1814c2af8f7e6929af2004302e7eMatthew Ahrens
c3a66015199e1814c2af8f7e6929af2004302e7eMatthew Ahrensstatic void
c3a66015199e1814c2af8f7e6929af2004302e7eMatthew Ahrensvdev_queue_pending_add(vdev_queue_t *vq, zio_t *zio)
c3a66015199e1814c2af8f7e6929af2004302e7eMatthew Ahrens{
c3a66015199e1814c2af8f7e6929af2004302e7eMatthew Ahrens spa_t *spa = zio->io_spa;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens ASSERT(MUTEX_HELD(&vq->vq_lock));
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens vq->vq_class[zio->io_priority].vqc_active++;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens avl_add(&vq->vq_active_tree, zio);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens mutex_enter(&spa->spa_iokstat_lock);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens spa->spa_queue_stats[zio->io_priority].spa_active++;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens if (spa->spa_iokstat != NULL)
c3a66015199e1814c2af8f7e6929af2004302e7eMatthew Ahrens kstat_runq_enter(spa->spa_iokstat->ks_data);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens mutex_exit(&spa->spa_iokstat_lock);
c3a66015199e1814c2af8f7e6929af2004302e7eMatthew Ahrens}
c3a66015199e1814c2af8f7e6929af2004302e7eMatthew Ahrens
c3a66015199e1814c2af8f7e6929af2004302e7eMatthew Ahrensstatic void
c3a66015199e1814c2af8f7e6929af2004302e7eMatthew Ahrensvdev_queue_pending_remove(vdev_queue_t *vq, zio_t *zio)
c3a66015199e1814c2af8f7e6929af2004302e7eMatthew Ahrens{
c3a66015199e1814c2af8f7e6929af2004302e7eMatthew Ahrens spa_t *spa = zio->io_spa;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens ASSERT(MUTEX_HELD(&vq->vq_lock));
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens vq->vq_class[zio->io_priority].vqc_active--;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens avl_remove(&vq->vq_active_tree, zio);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens mutex_enter(&spa->spa_iokstat_lock);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens ASSERT3U(spa->spa_queue_stats[zio->io_priority].spa_active, >, 0);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens spa->spa_queue_stats[zio->io_priority].spa_active--;
c3a66015199e1814c2af8f7e6929af2004302e7eMatthew Ahrens if (spa->spa_iokstat != NULL) {
c3a66015199e1814c2af8f7e6929af2004302e7eMatthew Ahrens kstat_io_t *ksio = spa->spa_iokstat->ks_data;
c3a66015199e1814c2af8f7e6929af2004302e7eMatthew Ahrens
c3a66015199e1814c2af8f7e6929af2004302e7eMatthew Ahrens kstat_runq_exit(spa->spa_iokstat->ks_data);
c3a66015199e1814c2af8f7e6929af2004302e7eMatthew Ahrens if (zio->io_type == ZIO_TYPE_READ) {
c3a66015199e1814c2af8f7e6929af2004302e7eMatthew Ahrens ksio->reads++;
c3a66015199e1814c2af8f7e6929af2004302e7eMatthew Ahrens ksio->nread += zio->io_size;
c3a66015199e1814c2af8f7e6929af2004302e7eMatthew Ahrens } else if (zio->io_type == ZIO_TYPE_WRITE) {
c3a66015199e1814c2af8f7e6929af2004302e7eMatthew Ahrens ksio->writes++;
c3a66015199e1814c2af8f7e6929af2004302e7eMatthew Ahrens ksio->nwritten += zio->io_size;
c3a66015199e1814c2af8f7e6929af2004302e7eMatthew Ahrens }
c3a66015199e1814c2af8f7e6929af2004302e7eMatthew Ahrens }
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens mutex_exit(&spa->spa_iokstat_lock);
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock}
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock
fa9e4066f08beec538e775443c5be79dd423fcabahrensstatic void
fa9e4066f08beec538e775443c5be79dd423fcabahrensvdev_queue_agg_io_done(zio_t *aio)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens if (aio->io_type == ZIO_TYPE_READ) {
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens zio_t *pio;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson zio_link_t *zl = NULL;
0f7643c7376dd69a08acbfc9d1d7d548b10c846aGeorge Wilson while ((pio = zio_walk_parents(aio, &zl)) != NULL) {
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore bcopy((char *)aio->io_data + (pio->io_offset -
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore aio->io_offset), pio->io_data, pio->io_size);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens }
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens zio_buf_free(aio->io_data, aio->io_size);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrensstatic int
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrensvdev_queue_class_min_active(zio_priority_t p)
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens{
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens switch (p) {
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens case ZIO_PRIORITY_SYNC_READ:
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens return (zfs_vdev_sync_read_min_active);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens case ZIO_PRIORITY_SYNC_WRITE:
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens return (zfs_vdev_sync_write_min_active);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens case ZIO_PRIORITY_ASYNC_READ:
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens return (zfs_vdev_async_read_min_active);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens case ZIO_PRIORITY_ASYNC_WRITE:
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens return (zfs_vdev_async_write_min_active);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens case ZIO_PRIORITY_SCRUB:
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens return (zfs_vdev_scrub_min_active);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens default:
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens panic("invalid priority %u", p);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens return (0);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens }
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens}
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrensstatic int
73527f441cbbd953fa42cc5a30a413bad75f24a9Alex Reecevdev_queue_max_async_writes(spa_t *spa)
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens{
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens int writes;
73527f441cbbd953fa42cc5a30a413bad75f24a9Alex Reece uint64_t dirty = spa->spa_dsl_pool->dp_dirty_total;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens uint64_t min_bytes = zfs_dirty_data_max *
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens zfs_vdev_async_write_active_min_dirty_percent / 100;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens uint64_t max_bytes = zfs_dirty_data_max *
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens zfs_vdev_async_write_active_max_dirty_percent / 100;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens
73527f441cbbd953fa42cc5a30a413bad75f24a9Alex Reece /*
73527f441cbbd953fa42cc5a30a413bad75f24a9Alex Reece * Sync tasks correspond to interactive user actions. To reduce the
73527f441cbbd953fa42cc5a30a413bad75f24a9Alex Reece * execution time of those actions we push data out as fast as possible.
73527f441cbbd953fa42cc5a30a413bad75f24a9Alex Reece */
73527f441cbbd953fa42cc5a30a413bad75f24a9Alex Reece if (spa_has_pending_synctask(spa)) {
73527f441cbbd953fa42cc5a30a413bad75f24a9Alex Reece return (zfs_vdev_async_write_max_active);
73527f441cbbd953fa42cc5a30a413bad75f24a9Alex Reece }
73527f441cbbd953fa42cc5a30a413bad75f24a9Alex Reece
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens if (dirty < min_bytes)
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens return (zfs_vdev_async_write_min_active);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens if (dirty > max_bytes)
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens return (zfs_vdev_async_write_max_active);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens /*
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * linear interpolation:
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * slope = (max_writes - min_writes) / (max_bytes - min_bytes)
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * move right by min_bytes
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * move up by min_writes
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens */
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens writes = (dirty - min_bytes) *
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens (zfs_vdev_async_write_max_active -
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens zfs_vdev_async_write_min_active) /
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens (max_bytes - min_bytes) +
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens zfs_vdev_async_write_min_active;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens ASSERT3U(writes, >=, zfs_vdev_async_write_min_active);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens ASSERT3U(writes, <=, zfs_vdev_async_write_max_active);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens return (writes);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens}
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrensstatic int
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrensvdev_queue_class_max_active(spa_t *spa, zio_priority_t p)
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens{
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens switch (p) {
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens case ZIO_PRIORITY_SYNC_READ:
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens return (zfs_vdev_sync_read_max_active);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens case ZIO_PRIORITY_SYNC_WRITE:
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens return (zfs_vdev_sync_write_max_active);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens case ZIO_PRIORITY_ASYNC_READ:
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens return (zfs_vdev_async_read_max_active);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens case ZIO_PRIORITY_ASYNC_WRITE:
73527f441cbbd953fa42cc5a30a413bad75f24a9Alex Reece return (vdev_queue_max_async_writes(spa));
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens case ZIO_PRIORITY_SCRUB:
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens return (zfs_vdev_scrub_max_active);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens default:
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens panic("invalid priority %u", p);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens return (0);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens }
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens}
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens/*
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * Return the i/o class to issue from, or ZIO_PRIORITY_MAX_QUEUEABLE if
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * there is no eligible class.
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens */
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrensstatic zio_priority_t
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrensvdev_queue_class_to_issue(vdev_queue_t *vq)
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens{
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens spa_t *spa = vq->vq_vdev->vdev_spa;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens zio_priority_t p;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens if (avl_numnodes(&vq->vq_active_tree) >= zfs_vdev_max_active)
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens return (ZIO_PRIORITY_NUM_QUEUEABLE);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens /* find a queue that has not reached its minimum # outstanding i/os */
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
fe319232d24f4ae183730a5a24a09423d8ab4429Justin T. Gibbs if (avl_numnodes(vdev_queue_class_tree(vq, p)) > 0 &&
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens vq->vq_class[p].vqc_active <
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens vdev_queue_class_min_active(p))
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens return (p);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens }
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens /*
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * If we haven't found a queue, look for one that hasn't reached its
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * maximum # outstanding i/os.
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens */
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
fe319232d24f4ae183730a5a24a09423d8ab4429Justin T. Gibbs if (avl_numnodes(vdev_queue_class_tree(vq, p)) > 0 &&
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens vq->vq_class[p].vqc_active <
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens vdev_queue_class_max_active(spa, p))
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens return (p);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens }
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens /* No eligible queued i/os */
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens return (ZIO_PRIORITY_NUM_QUEUEABLE);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens}
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens
6f708f7c79bbe8e239e0f588bb846b4ccbc14bf3Jeff Bonwick/*
6f708f7c79bbe8e239e0f588bb846b4ccbc14bf3Jeff Bonwick * Compute the range spanned by two i/os, which is the endpoint of the last
6f708f7c79bbe8e239e0f588bb846b4ccbc14bf3Jeff Bonwick * (lio->io_offset + lio->io_size) minus start of the first (fio->io_offset).
6f708f7c79bbe8e239e0f588bb846b4ccbc14bf3Jeff Bonwick * Conveniently, the gap between fio and lio is given by -IO_SPAN(lio, fio);
6f708f7c79bbe8e239e0f588bb846b4ccbc14bf3Jeff Bonwick * thus fio and lio are adjacent if and only if IO_SPAN(lio, fio) == 0.
6f708f7c79bbe8e239e0f588bb846b4ccbc14bf3Jeff Bonwick */
6f708f7c79bbe8e239e0f588bb846b4ccbc14bf3Jeff Bonwick#define IO_SPAN(fio, lio) ((lio)->io_offset + (lio)->io_size - (fio)->io_offset)
6f708f7c79bbe8e239e0f588bb846b4ccbc14bf3Jeff Bonwick#define IO_GAP(fio, lio) (-IO_SPAN(lio, fio))
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrensstatic zio_t *
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrensvdev_queue_aggregate(vdev_queue_t *vq, zio_t *zio)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens zio_t *first, *last, *aio, *dio, *mandatory, *nio;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens uint64_t maxgap = 0;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens uint64_t size;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens boolean_t stretch = B_FALSE;
fe319232d24f4ae183730a5a24a09423d8ab4429Justin T. Gibbs avl_tree_t *t = vdev_queue_type_tree(vq, zio->io_type);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens enum zio_flag flags = zio->io_flags & ZIO_FLAG_AGG_INHERIT;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens if (zio->io_flags & ZIO_FLAG_DONT_AGGREGATE)
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens return (NULL);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens first = last = zio;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens if (zio->io_type == ZIO_TYPE_READ)
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens maxgap = zfs_vdev_read_gap_limit;
8ad4d6dd86f5bc65fb3afa566c8133f3bac21648Jeff Bonwick
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens /*
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * We can aggregate I/Os that are sufficiently adjacent and of
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * the same flavor, as expressed by the AGG_INHERIT flags.
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * The latter requirement is necessary so that certain
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * attributes of the I/O, such as whether it's a normal I/O
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * or a scrub/resilver, can be preserved in the aggregate.
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * We can include optional I/Os, but don't allow them
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * to begin a range as they add no benefit in that situation.
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens */
f94275ce205810a201404c5f35f4cc96057022b1Adam Leventhal
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens /*
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * We keep track of the last non-optional I/O.
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens */
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens mandatory = (first->io_flags & ZIO_FLAG_OPTIONAL) ? NULL : first;
f94275ce205810a201404c5f35f4cc96057022b1Adam Leventhal
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens /*
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * Walk backwards through sufficiently contiguous I/Os
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * recording the last non-option I/O.
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens */
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens while ((dio = AVL_PREV(t, first)) != NULL &&
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags &&
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens IO_SPAN(dio, last) <= zfs_vdev_aggregation_limit &&
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens IO_GAP(dio, first) <= maxgap) {
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens first = dio;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens if (mandatory == NULL && !(first->io_flags & ZIO_FLAG_OPTIONAL))
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens mandatory = first;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens }
f94275ce205810a201404c5f35f4cc96057022b1Adam Leventhal
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens /*
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * Skip any initial optional I/Os.
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens */
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens while ((first->io_flags & ZIO_FLAG_OPTIONAL) && first != last) {
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens first = AVL_NEXT(t, first);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens ASSERT(first != NULL);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens }
6f708f7c79bbe8e239e0f588bb846b4ccbc14bf3Jeff Bonwick
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens /*
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * Walk forward through sufficiently contiguous I/Os.
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens */
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens while ((dio = AVL_NEXT(t, last)) != NULL &&
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags &&
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens IO_SPAN(first, dio) <= zfs_vdev_aggregation_limit &&
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens IO_GAP(last, dio) <= maxgap) {
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens last = dio;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens if (!(last->io_flags & ZIO_FLAG_OPTIONAL))
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens mandatory = last;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens }
f94275ce205810a201404c5f35f4cc96057022b1Adam Leventhal
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens /*
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * Now that we've established the range of the I/O aggregation
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * we must decide what to do with trailing optional I/Os.
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * For reads, there's nothing to do. While we are unable to
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * aggregate further, it's possible that a trailing optional
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * I/O would allow the underlying device to aggregate with
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * subsequent I/Os. We must therefore determine if the next
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * non-optional I/O is close enough to make aggregation
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * worthwhile.
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens */
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens if (zio->io_type == ZIO_TYPE_WRITE && mandatory != NULL) {
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens zio_t *nio = last;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens while ((dio = AVL_NEXT(t, nio)) != NULL &&
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens IO_GAP(nio, dio) == 0 &&
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens IO_GAP(mandatory, dio) <= zfs_vdev_write_gap_limit) {
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens nio = dio;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens if (!(nio->io_flags & ZIO_FLAG_OPTIONAL)) {
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens stretch = B_TRUE;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens break;
f94275ce205810a201404c5f35f4cc96057022b1Adam Leventhal }
f94275ce205810a201404c5f35f4cc96057022b1Adam Leventhal }
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens }
f94275ce205810a201404c5f35f4cc96057022b1Adam Leventhal
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens if (stretch) {
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens /* This may be a no-op. */
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens dio = AVL_NEXT(t, last);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens dio->io_flags &= ~ZIO_FLAG_OPTIONAL;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens } else {
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens while (last != mandatory && last != first) {
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens ASSERT(last->io_flags & ZIO_FLAG_OPTIONAL);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens last = AVL_PREV(t, last);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens ASSERT(last != NULL);
f94275ce205810a201404c5f35f4cc96057022b1Adam Leventhal }
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens if (first == last)
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens return (NULL);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens size = IO_SPAN(first, last);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens ASSERT3U(size, <=, zfs_vdev_aggregation_limit);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens aio = zio_vdev_delegated_io(first->io_vd, first->io_offset,
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens zio_buf_alloc(size), size, first->io_type, zio->io_priority,
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens flags | ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE,
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens vdev_queue_agg_io_done, NULL);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens aio->io_timestamp = first->io_timestamp;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens nio = first;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens do {
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens dio = nio;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens nio = AVL_NEXT(t, dio);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens ASSERT3U(dio->io_type, ==, aio->io_type);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens if (dio->io_flags & ZIO_FLAG_NODATA) {
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens ASSERT3U(dio->io_type, ==, ZIO_TYPE_WRITE);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens bzero((char *)aio->io_data + (dio->io_offset -
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens aio->io_offset), dio->io_size);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens } else if (dio->io_type == ZIO_TYPE_WRITE) {
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens bcopy(dio->io_data, (char *)aio->io_data +
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens (dio->io_offset - aio->io_offset),
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens dio->io_size);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens }
a3f829ae41ece20e7f5f63604e177aeeb8b24628Bill Moore
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens zio_add_child(dio, aio);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens vdev_queue_io_remove(vq, dio);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens zio_vdev_io_bypass(dio);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens zio_execute(dio);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens } while (dio != last);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens return (aio);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens}
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrensstatic zio_t *
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrensvdev_queue_io_to_issue(vdev_queue_t *vq)
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens{
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens zio_t *zio, *aio;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens zio_priority_t p;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens avl_index_t idx;
fe319232d24f4ae183730a5a24a09423d8ab4429Justin T. Gibbs avl_tree_t *tree;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens zio_t search;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrensagain:
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens ASSERT(MUTEX_HELD(&vq->vq_lock));
fa9e4066f08beec538e775443c5be79dd423fcabahrens
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens p = vdev_queue_class_to_issue(vq);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens if (p == ZIO_PRIORITY_NUM_QUEUEABLE) {
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens /* No eligible queued i/os */
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens return (NULL);
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens /*
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * For LBA-ordered queues (async / scrub), issue the i/o which follows
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * the most recently issued i/o in LBA (offset) order.
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens *
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * For FIFO queues (sync), issue the i/o with the lowest timestamp.
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens */
fe319232d24f4ae183730a5a24a09423d8ab4429Justin T. Gibbs tree = vdev_queue_class_tree(vq, p);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens search.io_timestamp = 0;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens search.io_offset = vq->vq_last_offset + 1;
fe319232d24f4ae183730a5a24a09423d8ab4429Justin T. Gibbs VERIFY3P(avl_find(tree, &search, &idx), ==, NULL);
fe319232d24f4ae183730a5a24a09423d8ab4429Justin T. Gibbs zio = avl_nearest(tree, idx, AVL_AFTER);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens if (zio == NULL)
fe319232d24f4ae183730a5a24a09423d8ab4429Justin T. Gibbs zio = avl_first(tree);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens ASSERT3U(zio->io_priority, ==, p);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens aio = vdev_queue_aggregate(vq, zio);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens if (aio != NULL)
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens zio = aio;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens else
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens vdev_queue_io_remove(vq, zio);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
f94275ce205810a201404c5f35f4cc96057022b1Adam Leventhal /*
f94275ce205810a201404c5f35f4cc96057022b1Adam Leventhal * If the I/O is or was optional and therefore has no data, we need to
f94275ce205810a201404c5f35f4cc96057022b1Adam Leventhal * simply discard it. We need to drop the vdev queue's lock to avoid a
f94275ce205810a201404c5f35f4cc96057022b1Adam Leventhal * deadlock that we could encounter since this I/O will complete
f94275ce205810a201404c5f35f4cc96057022b1Adam Leventhal * immediately.
f94275ce205810a201404c5f35f4cc96057022b1Adam Leventhal */
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens if (zio->io_flags & ZIO_FLAG_NODATA) {
f94275ce205810a201404c5f35f4cc96057022b1Adam Leventhal mutex_exit(&vq->vq_lock);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens zio_vdev_io_bypass(zio);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens zio_execute(zio);
f94275ce205810a201404c5f35f4cc96057022b1Adam Leventhal mutex_enter(&vq->vq_lock);
f94275ce205810a201404c5f35f4cc96057022b1Adam Leventhal goto again;
f94275ce205810a201404c5f35f4cc96057022b1Adam Leventhal }
f94275ce205810a201404c5f35f4cc96057022b1Adam Leventhal
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens vdev_queue_pending_add(vq, zio);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens vq->vq_last_offset = zio->io_offset;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens return (zio);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrenszio_t *
fa9e4066f08beec538e775443c5be79dd423fcabahrensvdev_queue_io(zio_t *zio)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens vdev_queue_t *vq = &zio->io_vd->vdev_queue;
fa9e4066f08beec538e775443c5be79dd423fcabahrens zio_t *nio;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (zio->io_flags & ZIO_FLAG_DONT_QUEUE)
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (zio);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens /*
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * Children i/os inherent their parent's priority, which might
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens * not match the child's i/o type. Fix it up here.
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens */
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens if (zio->io_type == ZIO_TYPE_READ) {
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens if (zio->io_priority != ZIO_PRIORITY_SYNC_READ &&
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens zio->io_priority != ZIO_PRIORITY_ASYNC_READ &&
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens zio->io_priority != ZIO_PRIORITY_SCRUB)
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens zio->io_priority = ZIO_PRIORITY_ASYNC_READ;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens } else {
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens ASSERT(zio->io_type == ZIO_TYPE_WRITE);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens if (zio->io_priority != ZIO_PRIORITY_SYNC_WRITE &&
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens zio->io_priority != ZIO_PRIORITY_ASYNC_WRITE)
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens zio->io_priority = ZIO_PRIORITY_ASYNC_WRITE;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens zio->io_flags |= ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_enter(&vq->vq_lock);
c55e05cb35da47582b7afd38734d2f0d9c6deb40Matthew Ahrens zio->io_timestamp = gethrtime();
ea8dc4b6d2251b437950c0056bc626b311c73c27eschrock vdev_queue_io_add(vq, zio);
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens nio = vdev_queue_io_to_issue(vq);
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_exit(&vq->vq_lock);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e05725b117836db173257fae43fb0746eb857fb5bonwick if (nio == NULL)
e05725b117836db173257fae43fb0746eb857fb5bonwick return (NULL);
e05725b117836db173257fae43fb0746eb857fb5bonwick
e05725b117836db173257fae43fb0746eb857fb5bonwick if (nio->io_done == vdev_queue_agg_io_done) {
e05725b117836db173257fae43fb0746eb857fb5bonwick zio_nowait(nio);
e05725b117836db173257fae43fb0746eb857fb5bonwick return (NULL);
e05725b117836db173257fae43fb0746eb857fb5bonwick }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
e05725b117836db173257fae43fb0746eb857fb5bonwick return (nio);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrensvoid
fa9e4066f08beec538e775443c5be79dd423fcabahrensvdev_queue_io_done(zio_t *zio)
fa9e4066f08beec538e775443c5be79dd423fcabahrens{
fa9e4066f08beec538e775443c5be79dd423fcabahrens vdev_queue_t *vq = &zio->io_vd->vdev_queue;
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens zio_t *nio;
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_enter(&vq->vq_lock);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
c3a66015199e1814c2af8f7e6929af2004302e7eMatthew Ahrens vdev_queue_pending_remove(vq, zio);
fa9e4066f08beec538e775443c5be79dd423fcabahrens
c55e05cb35da47582b7afd38734d2f0d9c6deb40Matthew Ahrens vq->vq_io_complete_ts = gethrtime();
283b84606b6fc326692c03273de1774e8c122f9aGeorge.Wilson
69962b5647e4a8b9b14998733b765925381b727eMatthew Ahrens while ((nio = vdev_queue_io_to_issue(vq)) != NULL) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_exit(&vq->vq_lock);
e05725b117836db173257fae43fb0746eb857fb5bonwick if (nio->io_done == vdev_queue_agg_io_done) {
e05725b117836db173257fae43fb0746eb857fb5bonwick zio_nowait(nio);
e05725b117836db173257fae43fb0746eb857fb5bonwick } else {
fa9e4066f08beec538e775443c5be79dd423fcabahrens zio_vdev_io_reissue(nio);
e05725b117836db173257fae43fb0746eb857fb5bonwick zio_execute(nio);
e05725b117836db173257fae43fb0746eb857fb5bonwick }
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_enter(&vq->vq_lock);
fa9e4066f08beec538e775443c5be79dd423fcabahrens }
fa9e4066f08beec538e775443c5be79dd423fcabahrens
fa9e4066f08beec538e775443c5be79dd423fcabahrens mutex_exit(&vq->vq_lock);
fa9e4066f08beec538e775443c5be79dd423fcabahrens}