Unleashed v1.4
[unleashed.git] / kernel / fs / zfs / vdev_queue.c
blobf29f4eeb9db53fc43b724fa3f0519e8e8bd3966d
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
27 * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
28 * Copyright (c) 2014 Integros [integros.com]
31 #include <sys/zfs_context.h>
32 #include <sys/vdev_impl.h>
33 #include <sys/spa_impl.h>
34 #include <sys/zio.h>
35 #include <sys/avl.h>
36 #include <sys/dsl_pool.h>
37 #include <sys/metaslab_impl.h>
38 #include <sys/abd.h>
41 * ZFS I/O Scheduler
42 * ---------------
44 * ZFS issues I/O operations to leaf vdevs to satisfy and complete zios. The
45 * I/O scheduler determines when and in what order those operations are
46 * issued. The I/O scheduler divides operations into five I/O classes
47 * prioritized in the following order: sync read, sync write, async read,
48 * async write, and scrub/resilver. Each queue defines the minimum and
49 * maximum number of concurrent operations that may be issued to the device.
50 * In addition, the device has an aggregate maximum. Note that the sum of the
51 * per-queue minimums must not exceed the aggregate maximum, and if the
52 * aggregate maximum is equal to or greater than the sum of the per-queue
53 * maximums, the per-queue minimum has no effect.
55 * For many physical devices, throughput increases with the number of
56 * concurrent operations, but latency typically suffers. Further, physical
57 * devices typically have a limit at which more concurrent operations have no
58 * effect on throughput or can actually cause it to decrease.
60 * The scheduler selects the next operation to issue by first looking for an
61 * I/O class whose minimum has not been satisfied. Once all are satisfied and
62 * the aggregate maximum has not been hit, the scheduler looks for classes
63 * whose maximum has not been satisfied. Iteration through the I/O classes is
64 * done in the order specified above. No further operations are issued if the
65 * aggregate maximum number of concurrent operations has been hit or if there
66 * are no operations queued for an I/O class that has not hit its maximum.
67 * Every time an i/o is queued or an operation completes, the I/O scheduler
68 * looks for new operations to issue.
70 * All I/O classes have a fixed maximum number of outstanding operations
71 * except for the async write class. Asynchronous writes represent the data
72 * that is committed to stable storage during the syncing stage for
73 * transaction groups (see txg.c). Transaction groups enter the syncing state
74 * periodically so the number of queued async writes will quickly burst up and
75 * then bleed down to zero. Rather than servicing them as quickly as possible,
76 * the I/O scheduler changes the maximum number of active async write i/os
77 * according to the amount of dirty data in the pool (see dsl_pool.c). Since
78 * both throughput and latency typically increase with the number of
79 * concurrent operations issued to physical devices, reducing the burstiness
80 * in the number of concurrent operations also stabilizes the response time of
81 * operations from other -- and in particular synchronous -- queues. In broad
82 * strokes, the I/O scheduler will issue more concurrent operations from the
83 * async write queue as there's more dirty data in the pool.
85 * Async Writes
87 * The number of concurrent operations issued for the async write I/O class
88 * follows a piece-wise linear function defined by a few adjustable points.
90 * | o---------| <-- zfs_vdev_async_write_max_active
91 * ^ | /^ |
92 * | | / | |
93 * active | / | |
94 * I/O | / | |
95 * count | / | |
96 * | / | |
97 * |------------o | | <-- zfs_vdev_async_write_min_active
98 * 0|____________^______|_________|
99 * 0% | | 100% of zfs_dirty_data_max
100 * | |
101 * | `-- zfs_vdev_async_write_active_max_dirty_percent
102 * `--------- zfs_vdev_async_write_active_min_dirty_percent
104 * Until the amount of dirty data exceeds a minimum percentage of the dirty
105 * data allowed in the pool, the I/O scheduler will limit the number of
106 * concurrent operations to the minimum. As that threshold is crossed, the
107 * number of concurrent operations issued increases linearly to the maximum at
108 * the specified maximum percentage of the dirty data allowed in the pool.
110 * Ideally, the amount of dirty data on a busy pool will stay in the sloped
111 * part of the function between zfs_vdev_async_write_active_min_dirty_percent
112 * and zfs_vdev_async_write_active_max_dirty_percent. If it exceeds the
113 * maximum percentage, this indicates that the rate of incoming data is
114 * greater than the rate that the backend storage can handle. In this case, we
115 * must further throttle incoming writes (see dmu_tx_delay() for details).
119 * The maximum number of i/os active to each device. Ideally, this will be >=
120 * the sum of each queue's max_active. It must be at least the sum of each
121 * queue's min_active.
123 uint32_t zfs_vdev_max_active = 1000;
126 * Per-queue limits on the number of i/os active to each device. If the
127 * sum of the queue's max_active is < zfs_vdev_max_active, then the
128 * min_active comes into play. We will send min_active from each queue,
129 * and then select from queues in the order defined by zio_priority_t.
131 * In general, smaller max_active's will lead to lower latency of synchronous
132 * operations. Larger max_active's may lead to higher overall throughput,
133 * depending on underlying storage.
135 * The ratio of the queues' max_actives determines the balance of performance
136 * between reads, writes, and scrubs. E.g., increasing
137 * zfs_vdev_scrub_max_active will cause the scrub or resilver to complete
138 * more quickly, but reads and writes to have higher latency and lower
139 * throughput.
141 uint32_t zfs_vdev_sync_read_min_active = 10;
142 uint32_t zfs_vdev_sync_read_max_active = 10;
143 uint32_t zfs_vdev_sync_write_min_active = 10;
144 uint32_t zfs_vdev_sync_write_max_active = 10;
145 uint32_t zfs_vdev_async_read_min_active = 1;
146 uint32_t zfs_vdev_async_read_max_active = 3;
147 uint32_t zfs_vdev_async_write_min_active = 1;
148 uint32_t zfs_vdev_async_write_max_active = 10;
149 uint32_t zfs_vdev_scrub_min_active = 1;
150 uint32_t zfs_vdev_scrub_max_active = 2;
151 uint32_t zfs_vdev_removal_min_active = 1;
152 uint32_t zfs_vdev_removal_max_active = 2;
153 uint32_t zfs_vdev_initializing_min_active = 1;
154 uint32_t zfs_vdev_initializing_max_active = 1;
157 * When the pool has less than zfs_vdev_async_write_active_min_dirty_percent
158 * dirty data, use zfs_vdev_async_write_min_active. When it has more than
159 * zfs_vdev_async_write_active_max_dirty_percent, use
160 * zfs_vdev_async_write_max_active. The value is linearly interpolated
161 * between min and max.
163 int zfs_vdev_async_write_active_min_dirty_percent = 30;
164 int zfs_vdev_async_write_active_max_dirty_percent = 60;
167 * To reduce IOPs, we aggregate small adjacent I/Os into one large I/O.
168 * For read I/Os, we also aggregate across small adjacency gaps; for writes
169 * we include spans of optional I/Os to aid aggregation at the disk even when
170 * they aren't able to help us aggregate at this level.
172 int zfs_vdev_aggregation_limit = SPA_OLD_MAXBLOCKSIZE;
173 int zfs_vdev_read_gap_limit = 32 << 10;
174 int zfs_vdev_write_gap_limit = 4 << 10;
177 * Define the queue depth percentage for each top-level. This percentage is
178 * used in conjunction with zfs_vdev_async_max_active to determine how many
179 * allocations a specific top-level vdev should handle. Once the queue depth
180 * reaches zfs_vdev_queue_depth_pct * zfs_vdev_async_write_max_active / 100
181 * then allocator will stop allocating blocks on that top-level device.
182 * The default kernel setting is 1000% which will yield 100 allocations per
183 * device. For userland testing, the default setting is 300% which equates
184 * to 30 allocations per device.
186 #ifdef _KERNEL
187 int zfs_vdev_queue_depth_pct = 1000;
188 #else
189 int zfs_vdev_queue_depth_pct = 300;
190 #endif
193 * When performing allocations for a given metaslab, we want to make sure that
194 * there are enough IOs to aggregate together to improve throughput. We want to
195 * ensure that there are at least 128k worth of IOs that can be aggregated, and
196 * we assume that the average allocation size is 4k, so we need the queue depth
197 * to be 32 per allocator to get good aggregation of sequential writes.
199 int zfs_vdev_def_queue_depth = 32;
203 vdev_queue_offset_compare(const void *x1, const void *x2)
205 const zio_t *z1 = x1;
206 const zio_t *z2 = x2;
208 if (z1->io_offset < z2->io_offset)
209 return (-1);
210 if (z1->io_offset > z2->io_offset)
211 return (1);
213 if (z1 < z2)
214 return (-1);
215 if (z1 > z2)
216 return (1);
218 return (0);
221 static inline avl_tree_t *
222 vdev_queue_class_tree(vdev_queue_t *vq, zio_priority_t p)
224 return (&vq->vq_class[p].vqc_queued_tree);
227 static inline avl_tree_t *
228 vdev_queue_type_tree(vdev_queue_t *vq, zio_type_t t)
230 ASSERT(t == ZIO_TYPE_READ || t == ZIO_TYPE_WRITE);
231 if (t == ZIO_TYPE_READ)
232 return (&vq->vq_read_offset_tree);
233 else
234 return (&vq->vq_write_offset_tree);
238 vdev_queue_timestamp_compare(const void *x1, const void *x2)
240 const zio_t *z1 = x1;
241 const zio_t *z2 = x2;
243 if (z1->io_timestamp < z2->io_timestamp)
244 return (-1);
245 if (z1->io_timestamp > z2->io_timestamp)
246 return (1);
248 if (z1 < z2)
249 return (-1);
250 if (z1 > z2)
251 return (1);
253 return (0);
256 void
257 vdev_queue_init(vdev_t *vd)
259 vdev_queue_t *vq = &vd->vdev_queue;
261 mutex_init(&vq->vq_lock, NULL, MUTEX_DEFAULT, NULL);
262 vq->vq_vdev = vd;
264 avl_create(&vq->vq_active_tree, vdev_queue_offset_compare,
265 sizeof (zio_t), offsetof(struct zio, io_queue_node));
266 avl_create(vdev_queue_type_tree(vq, ZIO_TYPE_READ),
267 vdev_queue_offset_compare, sizeof (zio_t),
268 offsetof(struct zio, io_offset_node));
269 avl_create(vdev_queue_type_tree(vq, ZIO_TYPE_WRITE),
270 vdev_queue_offset_compare, sizeof (zio_t),
271 offsetof(struct zio, io_offset_node));
273 for (zio_priority_t p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
274 int (*compfn) (const void *, const void *);
277 * The synchronous i/o queues are dispatched in FIFO rather
278 * than LBA order. This provides more consistent latency for
279 * these i/os.
281 if (p == ZIO_PRIORITY_SYNC_READ || p == ZIO_PRIORITY_SYNC_WRITE)
282 compfn = vdev_queue_timestamp_compare;
283 else
284 compfn = vdev_queue_offset_compare;
286 avl_create(vdev_queue_class_tree(vq, p), compfn,
287 sizeof (zio_t), offsetof(struct zio, io_queue_node));
291 void
292 vdev_queue_fini(vdev_t *vd)
294 vdev_queue_t *vq = &vd->vdev_queue;
296 for (zio_priority_t p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++)
297 avl_destroy(vdev_queue_class_tree(vq, p));
298 avl_destroy(&vq->vq_active_tree);
299 avl_destroy(vdev_queue_type_tree(vq, ZIO_TYPE_READ));
300 avl_destroy(vdev_queue_type_tree(vq, ZIO_TYPE_WRITE));
302 mutex_destroy(&vq->vq_lock);
305 static void
306 vdev_queue_io_add(vdev_queue_t *vq, zio_t *zio)
308 spa_t *spa = zio->io_spa;
310 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
311 avl_add(vdev_queue_class_tree(vq, zio->io_priority), zio);
312 avl_add(vdev_queue_type_tree(vq, zio->io_type), zio);
314 mutex_enter(&spa->spa_iokstat_lock);
315 spa->spa_queue_stats[zio->io_priority].spa_queued++;
316 if (spa->spa_iokstat != NULL)
317 kstat_waitq_enter(spa->spa_iokstat->ks_data);
318 mutex_exit(&spa->spa_iokstat_lock);
321 static void
322 vdev_queue_io_remove(vdev_queue_t *vq, zio_t *zio)
324 spa_t *spa = zio->io_spa;
326 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
327 avl_remove(vdev_queue_class_tree(vq, zio->io_priority), zio);
328 avl_remove(vdev_queue_type_tree(vq, zio->io_type), zio);
330 mutex_enter(&spa->spa_iokstat_lock);
331 ASSERT3U(spa->spa_queue_stats[zio->io_priority].spa_queued, >, 0);
332 spa->spa_queue_stats[zio->io_priority].spa_queued--;
333 if (spa->spa_iokstat != NULL)
334 kstat_waitq_exit(spa->spa_iokstat->ks_data);
335 mutex_exit(&spa->spa_iokstat_lock);
338 static void
339 vdev_queue_pending_add(vdev_queue_t *vq, zio_t *zio)
341 spa_t *spa = zio->io_spa;
342 ASSERT(MUTEX_HELD(&vq->vq_lock));
343 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
344 vq->vq_class[zio->io_priority].vqc_active++;
345 avl_add(&vq->vq_active_tree, zio);
347 mutex_enter(&spa->spa_iokstat_lock);
348 spa->spa_queue_stats[zio->io_priority].spa_active++;
349 if (spa->spa_iokstat != NULL)
350 kstat_runq_enter(spa->spa_iokstat->ks_data);
351 mutex_exit(&spa->spa_iokstat_lock);
354 static void
355 vdev_queue_pending_remove(vdev_queue_t *vq, zio_t *zio)
357 spa_t *spa = zio->io_spa;
358 ASSERT(MUTEX_HELD(&vq->vq_lock));
359 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
360 vq->vq_class[zio->io_priority].vqc_active--;
361 avl_remove(&vq->vq_active_tree, zio);
363 mutex_enter(&spa->spa_iokstat_lock);
364 ASSERT3U(spa->spa_queue_stats[zio->io_priority].spa_active, >, 0);
365 spa->spa_queue_stats[zio->io_priority].spa_active--;
366 if (spa->spa_iokstat != NULL) {
367 kstat_io_t *ksio = spa->spa_iokstat->ks_data;
369 kstat_runq_exit(spa->spa_iokstat->ks_data);
370 if (zio->io_type == ZIO_TYPE_READ) {
371 ksio->reads++;
372 ksio->nread += zio->io_size;
373 } else if (zio->io_type == ZIO_TYPE_WRITE) {
374 ksio->writes++;
375 ksio->nwritten += zio->io_size;
378 mutex_exit(&spa->spa_iokstat_lock);
381 static void
382 vdev_queue_agg_io_done(zio_t *aio)
384 if (aio->io_type == ZIO_TYPE_READ) {
385 zio_t *pio;
386 zio_link_t *zl = NULL;
387 while ((pio = zio_walk_parents(aio, &zl)) != NULL) {
388 abd_copy_off(pio->io_abd, aio->io_abd,
389 0, pio->io_offset - aio->io_offset, pio->io_size);
393 abd_free(aio->io_abd);
396 static int
397 vdev_queue_class_min_active(zio_priority_t p)
399 switch (p) {
400 case ZIO_PRIORITY_SYNC_READ:
401 return (zfs_vdev_sync_read_min_active);
402 case ZIO_PRIORITY_SYNC_WRITE:
403 return (zfs_vdev_sync_write_min_active);
404 case ZIO_PRIORITY_ASYNC_READ:
405 return (zfs_vdev_async_read_min_active);
406 case ZIO_PRIORITY_ASYNC_WRITE:
407 return (zfs_vdev_async_write_min_active);
408 case ZIO_PRIORITY_SCRUB:
409 return (zfs_vdev_scrub_min_active);
410 case ZIO_PRIORITY_REMOVAL:
411 return (zfs_vdev_removal_min_active);
412 case ZIO_PRIORITY_INITIALIZING:
413 return (zfs_vdev_initializing_min_active);
414 default:
415 panic("invalid priority %u", p);
416 return (0);
420 static int
421 vdev_queue_max_async_writes(spa_t *spa)
423 int writes;
424 uint64_t dirty = spa->spa_dsl_pool->dp_dirty_total;
425 uint64_t min_bytes = zfs_dirty_data_max *
426 zfs_vdev_async_write_active_min_dirty_percent / 100;
427 uint64_t max_bytes = zfs_dirty_data_max *
428 zfs_vdev_async_write_active_max_dirty_percent / 100;
431 * Sync tasks correspond to interactive user actions. To reduce the
432 * execution time of those actions we push data out as fast as possible.
434 if (spa_has_pending_synctask(spa)) {
435 return (zfs_vdev_async_write_max_active);
438 if (dirty < min_bytes)
439 return (zfs_vdev_async_write_min_active);
440 if (dirty > max_bytes)
441 return (zfs_vdev_async_write_max_active);
444 * linear interpolation:
445 * slope = (max_writes - min_writes) / (max_bytes - min_bytes)
446 * move right by min_bytes
447 * move up by min_writes
449 writes = (dirty - min_bytes) *
450 (zfs_vdev_async_write_max_active -
451 zfs_vdev_async_write_min_active) /
452 (max_bytes - min_bytes) +
453 zfs_vdev_async_write_min_active;
454 ASSERT3U(writes, >=, zfs_vdev_async_write_min_active);
455 ASSERT3U(writes, <=, zfs_vdev_async_write_max_active);
456 return (writes);
459 static int
460 vdev_queue_class_max_active(spa_t *spa, zio_priority_t p)
462 switch (p) {
463 case ZIO_PRIORITY_SYNC_READ:
464 return (zfs_vdev_sync_read_max_active);
465 case ZIO_PRIORITY_SYNC_WRITE:
466 return (zfs_vdev_sync_write_max_active);
467 case ZIO_PRIORITY_ASYNC_READ:
468 return (zfs_vdev_async_read_max_active);
469 case ZIO_PRIORITY_ASYNC_WRITE:
470 return (vdev_queue_max_async_writes(spa));
471 case ZIO_PRIORITY_SCRUB:
472 return (zfs_vdev_scrub_max_active);
473 case ZIO_PRIORITY_REMOVAL:
474 return (zfs_vdev_removal_max_active);
475 case ZIO_PRIORITY_INITIALIZING:
476 return (zfs_vdev_initializing_max_active);
477 default:
478 panic("invalid priority %u", p);
479 return (0);
484 * Return the i/o class to issue from, or ZIO_PRIORITY_MAX_QUEUEABLE if
485 * there is no eligible class.
487 static zio_priority_t
488 vdev_queue_class_to_issue(vdev_queue_t *vq)
490 spa_t *spa = vq->vq_vdev->vdev_spa;
491 zio_priority_t p;
493 if (avl_numnodes(&vq->vq_active_tree) >= zfs_vdev_max_active)
494 return (ZIO_PRIORITY_NUM_QUEUEABLE);
496 /* find a queue that has not reached its minimum # outstanding i/os */
497 for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
498 if (avl_numnodes(vdev_queue_class_tree(vq, p)) > 0 &&
499 vq->vq_class[p].vqc_active <
500 vdev_queue_class_min_active(p))
501 return (p);
505 * If we haven't found a queue, look for one that hasn't reached its
506 * maximum # outstanding i/os.
508 for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
509 if (avl_numnodes(vdev_queue_class_tree(vq, p)) > 0 &&
510 vq->vq_class[p].vqc_active <
511 vdev_queue_class_max_active(spa, p))
512 return (p);
515 /* No eligible queued i/os */
516 return (ZIO_PRIORITY_NUM_QUEUEABLE);
520 * Compute the range spanned by two i/os, which is the endpoint of the last
521 * (lio->io_offset + lio->io_size) minus start of the first (fio->io_offset).
522 * Conveniently, the gap between fio and lio is given by -IO_SPAN(lio, fio);
523 * thus fio and lio are adjacent if and only if IO_SPAN(lio, fio) == 0.
525 #define IO_SPAN(fio, lio) ((lio)->io_offset + (lio)->io_size - (fio)->io_offset)
526 #define IO_GAP(fio, lio) (-IO_SPAN(lio, fio))
528 static zio_t *
529 vdev_queue_aggregate(vdev_queue_t *vq, zio_t *zio)
531 zio_t *first, *last, *aio, *dio, *mandatory, *nio;
532 uint64_t maxgap = 0;
533 uint64_t size;
534 boolean_t stretch = B_FALSE;
535 avl_tree_t *t = vdev_queue_type_tree(vq, zio->io_type);
536 enum zio_flag flags = zio->io_flags & ZIO_FLAG_AGG_INHERIT;
538 if (zio->io_flags & ZIO_FLAG_DONT_AGGREGATE)
539 return (NULL);
541 first = last = zio;
543 if (zio->io_type == ZIO_TYPE_READ)
544 maxgap = zfs_vdev_read_gap_limit;
547 * We can aggregate I/Os that are sufficiently adjacent and of
548 * the same flavor, as expressed by the AGG_INHERIT flags.
549 * The latter requirement is necessary so that certain
550 * attributes of the I/O, such as whether it's a normal I/O
551 * or a scrub/resilver, can be preserved in the aggregate.
552 * We can include optional I/Os, but don't allow them
553 * to begin a range as they add no benefit in that situation.
557 * We keep track of the last non-optional I/O.
559 mandatory = (first->io_flags & ZIO_FLAG_OPTIONAL) ? NULL : first;
562 * Walk backwards through sufficiently contiguous I/Os
563 * recording the last non-optional I/O.
565 while ((dio = AVL_PREV(t, first)) != NULL &&
566 (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags &&
567 IO_SPAN(dio, last) <= zfs_vdev_aggregation_limit &&
568 IO_GAP(dio, first) <= maxgap &&
569 dio->io_type == zio->io_type) {
570 first = dio;
571 if (mandatory == NULL && !(first->io_flags & ZIO_FLAG_OPTIONAL))
572 mandatory = first;
576 * Skip any initial optional I/Os.
578 while ((first->io_flags & ZIO_FLAG_OPTIONAL) && first != last) {
579 first = AVL_NEXT(t, first);
580 ASSERT(first != NULL);
584 * Walk forward through sufficiently contiguous I/Os.
585 * The aggregation limit does not apply to optional i/os, so that
586 * we can issue contiguous writes even if they are larger than the
587 * aggregation limit.
589 while ((dio = AVL_NEXT(t, last)) != NULL &&
590 (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags &&
591 (IO_SPAN(first, dio) <= zfs_vdev_aggregation_limit ||
592 (dio->io_flags & ZIO_FLAG_OPTIONAL)) &&
593 IO_GAP(last, dio) <= maxgap &&
594 dio->io_type == zio->io_type) {
595 last = dio;
596 if (!(last->io_flags & ZIO_FLAG_OPTIONAL))
597 mandatory = last;
601 * Now that we've established the range of the I/O aggregation
602 * we must decide what to do with trailing optional I/Os.
603 * For reads, there's nothing to do. While we are unable to
604 * aggregate further, it's possible that a trailing optional
605 * I/O would allow the underlying device to aggregate with
606 * subsequent I/Os. We must therefore determine if the next
607 * non-optional I/O is close enough to make aggregation
608 * worthwhile.
610 if (zio->io_type == ZIO_TYPE_WRITE && mandatory != NULL) {
611 zio_t *nio = last;
612 while ((dio = AVL_NEXT(t, nio)) != NULL &&
613 IO_GAP(nio, dio) == 0 &&
614 IO_GAP(mandatory, dio) <= zfs_vdev_write_gap_limit) {
615 nio = dio;
616 if (!(nio->io_flags & ZIO_FLAG_OPTIONAL)) {
617 stretch = B_TRUE;
618 break;
623 if (stretch) {
625 * We are going to include an optional io in our aggregated
626 * span, thus closing the write gap. Only mandatory i/os can
627 * start aggregated spans, so make sure that the next i/o
628 * after our span is mandatory.
630 dio = AVL_NEXT(t, last);
631 dio->io_flags &= ~ZIO_FLAG_OPTIONAL;
632 } else {
633 /* do not include the optional i/o */
634 while (last != mandatory && last != first) {
635 ASSERT(last->io_flags & ZIO_FLAG_OPTIONAL);
636 last = AVL_PREV(t, last);
637 ASSERT(last != NULL);
641 if (first == last)
642 return (NULL);
644 size = IO_SPAN(first, last);
645 ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
647 aio = zio_vdev_delegated_io(first->io_vd, first->io_offset,
648 abd_alloc_for_io(size, B_TRUE), size, first->io_type,
649 zio->io_priority, flags | ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE,
650 vdev_queue_agg_io_done, NULL);
651 aio->io_timestamp = first->io_timestamp;
653 nio = first;
654 do {
655 dio = nio;
656 nio = AVL_NEXT(t, dio);
657 ASSERT3U(dio->io_type, ==, aio->io_type);
659 if (dio->io_flags & ZIO_FLAG_NODATA) {
660 ASSERT3U(dio->io_type, ==, ZIO_TYPE_WRITE);
661 abd_zero_off(aio->io_abd,
662 dio->io_offset - aio->io_offset, dio->io_size);
663 } else if (dio->io_type == ZIO_TYPE_WRITE) {
664 abd_copy_off(aio->io_abd, dio->io_abd,
665 dio->io_offset - aio->io_offset, 0, dio->io_size);
668 zio_add_child(dio, aio);
669 vdev_queue_io_remove(vq, dio);
670 zio_vdev_io_bypass(dio);
671 zio_execute(dio);
672 } while (dio != last);
674 return (aio);
677 static zio_t *
678 vdev_queue_io_to_issue(vdev_queue_t *vq)
680 zio_t *zio, *aio;
681 zio_priority_t p;
682 avl_index_t idx;
683 avl_tree_t *tree;
684 zio_t search;
686 again:
687 ASSERT(MUTEX_HELD(&vq->vq_lock));
689 p = vdev_queue_class_to_issue(vq);
691 if (p == ZIO_PRIORITY_NUM_QUEUEABLE) {
692 /* No eligible queued i/os */
693 return (NULL);
697 * For LBA-ordered queues (async / scrub / initializing), issue the
698 * i/o which follows the most recently issued i/o in LBA (offset) order.
700 * For FIFO queues (sync), issue the i/o with the lowest timestamp.
702 tree = vdev_queue_class_tree(vq, p);
703 search.io_timestamp = 0;
704 search.io_offset = vq->vq_last_offset + 1;
705 VERIFY3P(avl_find(tree, &search, &idx), ==, NULL);
706 zio = avl_nearest(tree, idx, AVL_AFTER);
707 if (zio == NULL)
708 zio = avl_first(tree);
709 ASSERT3U(zio->io_priority, ==, p);
711 aio = vdev_queue_aggregate(vq, zio);
712 if (aio != NULL)
713 zio = aio;
714 else
715 vdev_queue_io_remove(vq, zio);
718 * If the I/O is or was optional and therefore has no data, we need to
719 * simply discard it. We need to drop the vdev queue's lock to avoid a
720 * deadlock that we could encounter since this I/O will complete
721 * immediately.
723 if (zio->io_flags & ZIO_FLAG_NODATA) {
724 mutex_exit(&vq->vq_lock);
725 zio_vdev_io_bypass(zio);
726 zio_execute(zio);
727 mutex_enter(&vq->vq_lock);
728 goto again;
731 vdev_queue_pending_add(vq, zio);
732 vq->vq_last_offset = zio->io_offset;
734 return (zio);
737 zio_t *
738 vdev_queue_io(zio_t *zio)
740 vdev_queue_t *vq = &zio->io_vd->vdev_queue;
741 zio_t *nio;
743 if (zio->io_flags & ZIO_FLAG_DONT_QUEUE)
744 return (zio);
747 * Children i/os inherent their parent's priority, which might
748 * not match the child's i/o type. Fix it up here.
750 if (zio->io_type == ZIO_TYPE_READ) {
751 if (zio->io_priority != ZIO_PRIORITY_SYNC_READ &&
752 zio->io_priority != ZIO_PRIORITY_ASYNC_READ &&
753 zio->io_priority != ZIO_PRIORITY_SCRUB &&
754 zio->io_priority != ZIO_PRIORITY_REMOVAL &&
755 zio->io_priority != ZIO_PRIORITY_INITIALIZING)
756 zio->io_priority = ZIO_PRIORITY_ASYNC_READ;
757 } else {
758 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
759 if (zio->io_priority != ZIO_PRIORITY_SYNC_WRITE &&
760 zio->io_priority != ZIO_PRIORITY_ASYNC_WRITE &&
761 zio->io_priority != ZIO_PRIORITY_REMOVAL &&
762 zio->io_priority != ZIO_PRIORITY_INITIALIZING)
763 zio->io_priority = ZIO_PRIORITY_ASYNC_WRITE;
766 zio->io_flags |= ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE;
768 mutex_enter(&vq->vq_lock);
769 zio->io_timestamp = gethrtime();
770 vdev_queue_io_add(vq, zio);
771 nio = vdev_queue_io_to_issue(vq);
772 mutex_exit(&vq->vq_lock);
774 if (nio == NULL)
775 return (NULL);
777 if (nio->io_done == vdev_queue_agg_io_done) {
778 zio_nowait(nio);
779 return (NULL);
782 return (nio);
785 void
786 vdev_queue_io_done(zio_t *zio)
788 vdev_queue_t *vq = &zio->io_vd->vdev_queue;
789 zio_t *nio;
791 mutex_enter(&vq->vq_lock);
793 vdev_queue_pending_remove(vq, zio);
795 vq->vq_io_complete_ts = gethrtime();
797 while ((nio = vdev_queue_io_to_issue(vq)) != NULL) {
798 mutex_exit(&vq->vq_lock);
799 if (nio->io_done == vdev_queue_agg_io_done) {
800 zio_nowait(nio);
801 } else {
802 zio_vdev_io_reissue(nio);
803 zio_execute(nio);
805 mutex_enter(&vq->vq_lock);
808 mutex_exit(&vq->vq_lock);