5027 zfs large block support (add copyright)
[unleashed.git] / usr / src / uts / common / fs / zfs / vdev_queue.c
blob4917cc9284147d13fb0e17cf5227fde7114242d9
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, 2014 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>
39 * ZFS I/O Scheduler
40 * ---------------
42 * ZFS issues I/O operations to leaf vdevs to satisfy and complete zios. The
43 * I/O scheduler determines when and in what order those operations are
44 * issued. The I/O scheduler divides operations into five I/O classes
45 * prioritized in the following order: sync read, sync write, async read,
46 * async write, and scrub/resilver. Each queue defines the minimum and
47 * maximum number of concurrent operations that may be issued to the device.
48 * In addition, the device has an aggregate maximum. Note that the sum of the
49 * per-queue minimums must not exceed the aggregate maximum, and if the
50 * aggregate maximum is equal to or greater than the sum of the per-queue
51 * maximums, the per-queue minimum has no effect.
53 * For many physical devices, throughput increases with the number of
54 * concurrent operations, but latency typically suffers. Further, physical
55 * devices typically have a limit at which more concurrent operations have no
56 * effect on throughput or can actually cause it to decrease.
58 * The scheduler selects the next operation to issue by first looking for an
59 * I/O class whose minimum has not been satisfied. Once all are satisfied and
60 * the aggregate maximum has not been hit, the scheduler looks for classes
61 * whose maximum has not been satisfied. Iteration through the I/O classes is
62 * done in the order specified above. No further operations are issued if the
63 * aggregate maximum number of concurrent operations has been hit or if there
64 * are no operations queued for an I/O class that has not hit its maximum.
65 * Every time an i/o is queued or an operation completes, the I/O scheduler
66 * looks for new operations to issue.
68 * All I/O classes have a fixed maximum number of outstanding operations
69 * except for the async write class. Asynchronous writes represent the data
70 * that is committed to stable storage during the syncing stage for
71 * transaction groups (see txg.c). Transaction groups enter the syncing state
72 * periodically so the number of queued async writes will quickly burst up and
73 * then bleed down to zero. Rather than servicing them as quickly as possible,
74 * the I/O scheduler changes the maximum number of active async write i/os
75 * according to the amount of dirty data in the pool (see dsl_pool.c). Since
76 * both throughput and latency typically increase with the number of
77 * concurrent operations issued to physical devices, reducing the burstiness
78 * in the number of concurrent operations also stabilizes the response time of
79 * operations from other -- and in particular synchronous -- queues. In broad
80 * strokes, the I/O scheduler will issue more concurrent operations from the
81 * async write queue as there's more dirty data in the pool.
83 * Async Writes
85 * The number of concurrent operations issued for the async write I/O class
86 * follows a piece-wise linear function defined by a few adjustable points.
88 * | o---------| <-- zfs_vdev_async_write_max_active
89 * ^ | /^ |
90 * | | / | |
91 * active | / | |
92 * I/O | / | |
93 * count | / | |
94 * | / | |
95 * |------------o | | <-- zfs_vdev_async_write_min_active
96 * 0|____________^______|_________|
97 * 0% | | 100% of zfs_dirty_data_max
98 * | |
99 * | `-- zfs_vdev_async_write_active_max_dirty_percent
100 * `--------- zfs_vdev_async_write_active_min_dirty_percent
102 * Until the amount of dirty data exceeds a minimum percentage of the dirty
103 * data allowed in the pool, the I/O scheduler will limit the number of
104 * concurrent operations to the minimum. As that threshold is crossed, the
105 * number of concurrent operations issued increases linearly to the maximum at
106 * the specified maximum percentage of the dirty data allowed in the pool.
108 * Ideally, the amount of dirty data on a busy pool will stay in the sloped
109 * part of the function between zfs_vdev_async_write_active_min_dirty_percent
110 * and zfs_vdev_async_write_active_max_dirty_percent. If it exceeds the
111 * maximum percentage, this indicates that the rate of incoming data is
112 * greater than the rate that the backend storage can handle. In this case, we
113 * must further throttle incoming writes (see dmu_tx_delay() for details).
117 * The maximum number of i/os active to each device. Ideally, this will be >=
118 * the sum of each queue's max_active. It must be at least the sum of each
119 * queue's min_active.
121 uint32_t zfs_vdev_max_active = 1000;
124 * Per-queue limits on the number of i/os active to each device. If the
125 * sum of the queue's max_active is < zfs_vdev_max_active, then the
126 * min_active comes into play. We will send min_active from each queue,
127 * and then select from queues in the order defined by zio_priority_t.
129 * In general, smaller max_active's will lead to lower latency of synchronous
130 * operations. Larger max_active's may lead to higher overall throughput,
131 * depending on underlying storage.
133 * The ratio of the queues' max_actives determines the balance of performance
134 * between reads, writes, and scrubs. E.g., increasing
135 * zfs_vdev_scrub_max_active will cause the scrub or resilver to complete
136 * more quickly, but reads and writes to have higher latency and lower
137 * throughput.
139 uint32_t zfs_vdev_sync_read_min_active = 10;
140 uint32_t zfs_vdev_sync_read_max_active = 10;
141 uint32_t zfs_vdev_sync_write_min_active = 10;
142 uint32_t zfs_vdev_sync_write_max_active = 10;
143 uint32_t zfs_vdev_async_read_min_active = 1;
144 uint32_t zfs_vdev_async_read_max_active = 3;
145 uint32_t zfs_vdev_async_write_min_active = 1;
146 uint32_t zfs_vdev_async_write_max_active = 10;
147 uint32_t zfs_vdev_scrub_min_active = 1;
148 uint32_t zfs_vdev_scrub_max_active = 2;
151 * When the pool has less than zfs_vdev_async_write_active_min_dirty_percent
152 * dirty data, use zfs_vdev_async_write_min_active. When it has more than
153 * zfs_vdev_async_write_active_max_dirty_percent, use
154 * zfs_vdev_async_write_max_active. The value is linearly interpolated
155 * between min and max.
157 int zfs_vdev_async_write_active_min_dirty_percent = 30;
158 int zfs_vdev_async_write_active_max_dirty_percent = 60;
161 * To reduce IOPs, we aggregate small adjacent I/Os into one large I/O.
162 * For read I/Os, we also aggregate across small adjacency gaps; for writes
163 * we include spans of optional I/Os to aid aggregation at the disk even when
164 * they aren't able to help us aggregate at this level.
166 int zfs_vdev_aggregation_limit = SPA_OLD_MAXBLOCKSIZE;
167 int zfs_vdev_read_gap_limit = 32 << 10;
168 int zfs_vdev_write_gap_limit = 4 << 10;
171 vdev_queue_offset_compare(const void *x1, const void *x2)
173 const zio_t *z1 = x1;
174 const zio_t *z2 = x2;
176 if (z1->io_offset < z2->io_offset)
177 return (-1);
178 if (z1->io_offset > z2->io_offset)
179 return (1);
181 if (z1 < z2)
182 return (-1);
183 if (z1 > z2)
184 return (1);
186 return (0);
189 static inline avl_tree_t *
190 vdev_queue_class_tree(vdev_queue_t *vq, zio_priority_t p)
192 return (&vq->vq_class[p].vqc_queued_tree);
195 static inline avl_tree_t *
196 vdev_queue_type_tree(vdev_queue_t *vq, zio_type_t t)
198 ASSERT(t == ZIO_TYPE_READ || t == ZIO_TYPE_WRITE);
199 if (t == ZIO_TYPE_READ)
200 return (&vq->vq_read_offset_tree);
201 else
202 return (&vq->vq_write_offset_tree);
206 vdev_queue_timestamp_compare(const void *x1, const void *x2)
208 const zio_t *z1 = x1;
209 const zio_t *z2 = x2;
211 if (z1->io_timestamp < z2->io_timestamp)
212 return (-1);
213 if (z1->io_timestamp > z2->io_timestamp)
214 return (1);
216 if (z1 < z2)
217 return (-1);
218 if (z1 > z2)
219 return (1);
221 return (0);
224 void
225 vdev_queue_init(vdev_t *vd)
227 vdev_queue_t *vq = &vd->vdev_queue;
229 mutex_init(&vq->vq_lock, NULL, MUTEX_DEFAULT, NULL);
230 vq->vq_vdev = vd;
232 avl_create(&vq->vq_active_tree, vdev_queue_offset_compare,
233 sizeof (zio_t), offsetof(struct zio, io_queue_node));
234 avl_create(vdev_queue_type_tree(vq, ZIO_TYPE_READ),
235 vdev_queue_offset_compare, sizeof (zio_t),
236 offsetof(struct zio, io_offset_node));
237 avl_create(vdev_queue_type_tree(vq, ZIO_TYPE_WRITE),
238 vdev_queue_offset_compare, sizeof (zio_t),
239 offsetof(struct zio, io_offset_node));
241 for (zio_priority_t p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
242 int (*compfn) (const void *, const void *);
245 * The synchronous i/o queues are dispatched in FIFO rather
246 * than LBA order. This provides more consistent latency for
247 * these i/os.
249 if (p == ZIO_PRIORITY_SYNC_READ || p == ZIO_PRIORITY_SYNC_WRITE)
250 compfn = vdev_queue_timestamp_compare;
251 else
252 compfn = vdev_queue_offset_compare;
254 avl_create(vdev_queue_class_tree(vq, p), compfn,
255 sizeof (zio_t), offsetof(struct zio, io_queue_node));
259 void
260 vdev_queue_fini(vdev_t *vd)
262 vdev_queue_t *vq = &vd->vdev_queue;
264 for (zio_priority_t p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++)
265 avl_destroy(vdev_queue_class_tree(vq, p));
266 avl_destroy(&vq->vq_active_tree);
267 avl_destroy(vdev_queue_type_tree(vq, ZIO_TYPE_READ));
268 avl_destroy(vdev_queue_type_tree(vq, ZIO_TYPE_WRITE));
270 mutex_destroy(&vq->vq_lock);
273 static void
274 vdev_queue_io_add(vdev_queue_t *vq, zio_t *zio)
276 spa_t *spa = zio->io_spa;
277 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
278 avl_add(vdev_queue_class_tree(vq, zio->io_priority), zio);
279 avl_add(vdev_queue_type_tree(vq, zio->io_type), zio);
281 mutex_enter(&spa->spa_iokstat_lock);
282 spa->spa_queue_stats[zio->io_priority].spa_queued++;
283 if (spa->spa_iokstat != NULL)
284 kstat_waitq_enter(spa->spa_iokstat->ks_data);
285 mutex_exit(&spa->spa_iokstat_lock);
288 static void
289 vdev_queue_io_remove(vdev_queue_t *vq, zio_t *zio)
291 spa_t *spa = zio->io_spa;
292 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
293 avl_remove(vdev_queue_class_tree(vq, zio->io_priority), zio);
294 avl_remove(vdev_queue_type_tree(vq, zio->io_type), zio);
296 mutex_enter(&spa->spa_iokstat_lock);
297 ASSERT3U(spa->spa_queue_stats[zio->io_priority].spa_queued, >, 0);
298 spa->spa_queue_stats[zio->io_priority].spa_queued--;
299 if (spa->spa_iokstat != NULL)
300 kstat_waitq_exit(spa->spa_iokstat->ks_data);
301 mutex_exit(&spa->spa_iokstat_lock);
304 static void
305 vdev_queue_pending_add(vdev_queue_t *vq, zio_t *zio)
307 spa_t *spa = zio->io_spa;
308 ASSERT(MUTEX_HELD(&vq->vq_lock));
309 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
310 vq->vq_class[zio->io_priority].vqc_active++;
311 avl_add(&vq->vq_active_tree, zio);
313 mutex_enter(&spa->spa_iokstat_lock);
314 spa->spa_queue_stats[zio->io_priority].spa_active++;
315 if (spa->spa_iokstat != NULL)
316 kstat_runq_enter(spa->spa_iokstat->ks_data);
317 mutex_exit(&spa->spa_iokstat_lock);
320 static void
321 vdev_queue_pending_remove(vdev_queue_t *vq, zio_t *zio)
323 spa_t *spa = zio->io_spa;
324 ASSERT(MUTEX_HELD(&vq->vq_lock));
325 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
326 vq->vq_class[zio->io_priority].vqc_active--;
327 avl_remove(&vq->vq_active_tree, zio);
329 mutex_enter(&spa->spa_iokstat_lock);
330 ASSERT3U(spa->spa_queue_stats[zio->io_priority].spa_active, >, 0);
331 spa->spa_queue_stats[zio->io_priority].spa_active--;
332 if (spa->spa_iokstat != NULL) {
333 kstat_io_t *ksio = spa->spa_iokstat->ks_data;
335 kstat_runq_exit(spa->spa_iokstat->ks_data);
336 if (zio->io_type == ZIO_TYPE_READ) {
337 ksio->reads++;
338 ksio->nread += zio->io_size;
339 } else if (zio->io_type == ZIO_TYPE_WRITE) {
340 ksio->writes++;
341 ksio->nwritten += zio->io_size;
344 mutex_exit(&spa->spa_iokstat_lock);
347 static void
348 vdev_queue_agg_io_done(zio_t *aio)
350 if (aio->io_type == ZIO_TYPE_READ) {
351 zio_t *pio;
352 while ((pio = zio_walk_parents(aio)) != NULL) {
353 bcopy((char *)aio->io_data + (pio->io_offset -
354 aio->io_offset), pio->io_data, pio->io_size);
358 zio_buf_free(aio->io_data, aio->io_size);
361 static int
362 vdev_queue_class_min_active(zio_priority_t p)
364 switch (p) {
365 case ZIO_PRIORITY_SYNC_READ:
366 return (zfs_vdev_sync_read_min_active);
367 case ZIO_PRIORITY_SYNC_WRITE:
368 return (zfs_vdev_sync_write_min_active);
369 case ZIO_PRIORITY_ASYNC_READ:
370 return (zfs_vdev_async_read_min_active);
371 case ZIO_PRIORITY_ASYNC_WRITE:
372 return (zfs_vdev_async_write_min_active);
373 case ZIO_PRIORITY_SCRUB:
374 return (zfs_vdev_scrub_min_active);
375 default:
376 panic("invalid priority %u", p);
377 return (0);
381 static int
382 vdev_queue_max_async_writes(spa_t *spa)
384 int writes;
385 uint64_t dirty = spa->spa_dsl_pool->dp_dirty_total;
386 uint64_t min_bytes = zfs_dirty_data_max *
387 zfs_vdev_async_write_active_min_dirty_percent / 100;
388 uint64_t max_bytes = zfs_dirty_data_max *
389 zfs_vdev_async_write_active_max_dirty_percent / 100;
392 * Sync tasks correspond to interactive user actions. To reduce the
393 * execution time of those actions we push data out as fast as possible.
395 if (spa_has_pending_synctask(spa)) {
396 return (zfs_vdev_async_write_max_active);
399 if (dirty < min_bytes)
400 return (zfs_vdev_async_write_min_active);
401 if (dirty > max_bytes)
402 return (zfs_vdev_async_write_max_active);
405 * linear interpolation:
406 * slope = (max_writes - min_writes) / (max_bytes - min_bytes)
407 * move right by min_bytes
408 * move up by min_writes
410 writes = (dirty - min_bytes) *
411 (zfs_vdev_async_write_max_active -
412 zfs_vdev_async_write_min_active) /
413 (max_bytes - min_bytes) +
414 zfs_vdev_async_write_min_active;
415 ASSERT3U(writes, >=, zfs_vdev_async_write_min_active);
416 ASSERT3U(writes, <=, zfs_vdev_async_write_max_active);
417 return (writes);
420 static int
421 vdev_queue_class_max_active(spa_t *spa, zio_priority_t p)
423 switch (p) {
424 case ZIO_PRIORITY_SYNC_READ:
425 return (zfs_vdev_sync_read_max_active);
426 case ZIO_PRIORITY_SYNC_WRITE:
427 return (zfs_vdev_sync_write_max_active);
428 case ZIO_PRIORITY_ASYNC_READ:
429 return (zfs_vdev_async_read_max_active);
430 case ZIO_PRIORITY_ASYNC_WRITE:
431 return (vdev_queue_max_async_writes(spa));
432 case ZIO_PRIORITY_SCRUB:
433 return (zfs_vdev_scrub_max_active);
434 default:
435 panic("invalid priority %u", p);
436 return (0);
441 * Return the i/o class to issue from, or ZIO_PRIORITY_MAX_QUEUEABLE if
442 * there is no eligible class.
444 static zio_priority_t
445 vdev_queue_class_to_issue(vdev_queue_t *vq)
447 spa_t *spa = vq->vq_vdev->vdev_spa;
448 zio_priority_t p;
450 if (avl_numnodes(&vq->vq_active_tree) >= zfs_vdev_max_active)
451 return (ZIO_PRIORITY_NUM_QUEUEABLE);
453 /* find a queue that has not reached its minimum # outstanding i/os */
454 for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
455 if (avl_numnodes(vdev_queue_class_tree(vq, p)) > 0 &&
456 vq->vq_class[p].vqc_active <
457 vdev_queue_class_min_active(p))
458 return (p);
462 * If we haven't found a queue, look for one that hasn't reached its
463 * maximum # outstanding i/os.
465 for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
466 if (avl_numnodes(vdev_queue_class_tree(vq, p)) > 0 &&
467 vq->vq_class[p].vqc_active <
468 vdev_queue_class_max_active(spa, p))
469 return (p);
472 /* No eligible queued i/os */
473 return (ZIO_PRIORITY_NUM_QUEUEABLE);
477 * Compute the range spanned by two i/os, which is the endpoint of the last
478 * (lio->io_offset + lio->io_size) minus start of the first (fio->io_offset).
479 * Conveniently, the gap between fio and lio is given by -IO_SPAN(lio, fio);
480 * thus fio and lio are adjacent if and only if IO_SPAN(lio, fio) == 0.
482 #define IO_SPAN(fio, lio) ((lio)->io_offset + (lio)->io_size - (fio)->io_offset)
483 #define IO_GAP(fio, lio) (-IO_SPAN(lio, fio))
485 static zio_t *
486 vdev_queue_aggregate(vdev_queue_t *vq, zio_t *zio)
488 zio_t *first, *last, *aio, *dio, *mandatory, *nio;
489 uint64_t maxgap = 0;
490 uint64_t size;
491 boolean_t stretch = B_FALSE;
492 avl_tree_t *t = vdev_queue_type_tree(vq, zio->io_type);
493 enum zio_flag flags = zio->io_flags & ZIO_FLAG_AGG_INHERIT;
495 if (zio->io_flags & ZIO_FLAG_DONT_AGGREGATE)
496 return (NULL);
498 first = last = zio;
500 if (zio->io_type == ZIO_TYPE_READ)
501 maxgap = zfs_vdev_read_gap_limit;
504 * We can aggregate I/Os that are sufficiently adjacent and of
505 * the same flavor, as expressed by the AGG_INHERIT flags.
506 * The latter requirement is necessary so that certain
507 * attributes of the I/O, such as whether it's a normal I/O
508 * or a scrub/resilver, can be preserved in the aggregate.
509 * We can include optional I/Os, but don't allow them
510 * to begin a range as they add no benefit in that situation.
514 * We keep track of the last non-optional I/O.
516 mandatory = (first->io_flags & ZIO_FLAG_OPTIONAL) ? NULL : first;
519 * Walk backwards through sufficiently contiguous I/Os
520 * recording the last non-option I/O.
522 while ((dio = AVL_PREV(t, first)) != NULL &&
523 (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags &&
524 IO_SPAN(dio, last) <= zfs_vdev_aggregation_limit &&
525 IO_GAP(dio, first) <= maxgap) {
526 first = dio;
527 if (mandatory == NULL && !(first->io_flags & ZIO_FLAG_OPTIONAL))
528 mandatory = first;
532 * Skip any initial optional I/Os.
534 while ((first->io_flags & ZIO_FLAG_OPTIONAL) && first != last) {
535 first = AVL_NEXT(t, first);
536 ASSERT(first != NULL);
540 * Walk forward through sufficiently contiguous I/Os.
542 while ((dio = AVL_NEXT(t, last)) != NULL &&
543 (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags &&
544 IO_SPAN(first, dio) <= zfs_vdev_aggregation_limit &&
545 IO_GAP(last, dio) <= maxgap) {
546 last = dio;
547 if (!(last->io_flags & ZIO_FLAG_OPTIONAL))
548 mandatory = last;
552 * Now that we've established the range of the I/O aggregation
553 * we must decide what to do with trailing optional I/Os.
554 * For reads, there's nothing to do. While we are unable to
555 * aggregate further, it's possible that a trailing optional
556 * I/O would allow the underlying device to aggregate with
557 * subsequent I/Os. We must therefore determine if the next
558 * non-optional I/O is close enough to make aggregation
559 * worthwhile.
561 if (zio->io_type == ZIO_TYPE_WRITE && mandatory != NULL) {
562 zio_t *nio = last;
563 while ((dio = AVL_NEXT(t, nio)) != NULL &&
564 IO_GAP(nio, dio) == 0 &&
565 IO_GAP(mandatory, dio) <= zfs_vdev_write_gap_limit) {
566 nio = dio;
567 if (!(nio->io_flags & ZIO_FLAG_OPTIONAL)) {
568 stretch = B_TRUE;
569 break;
574 if (stretch) {
575 /* This may be a no-op. */
576 dio = AVL_NEXT(t, last);
577 dio->io_flags &= ~ZIO_FLAG_OPTIONAL;
578 } else {
579 while (last != mandatory && last != first) {
580 ASSERT(last->io_flags & ZIO_FLAG_OPTIONAL);
581 last = AVL_PREV(t, last);
582 ASSERT(last != NULL);
586 if (first == last)
587 return (NULL);
589 size = IO_SPAN(first, last);
590 ASSERT3U(size, <=, zfs_vdev_aggregation_limit);
592 aio = zio_vdev_delegated_io(first->io_vd, first->io_offset,
593 zio_buf_alloc(size), size, first->io_type, zio->io_priority,
594 flags | ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE,
595 vdev_queue_agg_io_done, NULL);
596 aio->io_timestamp = first->io_timestamp;
598 nio = first;
599 do {
600 dio = nio;
601 nio = AVL_NEXT(t, dio);
602 ASSERT3U(dio->io_type, ==, aio->io_type);
604 if (dio->io_flags & ZIO_FLAG_NODATA) {
605 ASSERT3U(dio->io_type, ==, ZIO_TYPE_WRITE);
606 bzero((char *)aio->io_data + (dio->io_offset -
607 aio->io_offset), dio->io_size);
608 } else if (dio->io_type == ZIO_TYPE_WRITE) {
609 bcopy(dio->io_data, (char *)aio->io_data +
610 (dio->io_offset - aio->io_offset),
611 dio->io_size);
614 zio_add_child(dio, aio);
615 vdev_queue_io_remove(vq, dio);
616 zio_vdev_io_bypass(dio);
617 zio_execute(dio);
618 } while (dio != last);
620 return (aio);
623 static zio_t *
624 vdev_queue_io_to_issue(vdev_queue_t *vq)
626 zio_t *zio, *aio;
627 zio_priority_t p;
628 avl_index_t idx;
629 avl_tree_t *tree;
630 zio_t search;
632 again:
633 ASSERT(MUTEX_HELD(&vq->vq_lock));
635 p = vdev_queue_class_to_issue(vq);
637 if (p == ZIO_PRIORITY_NUM_QUEUEABLE) {
638 /* No eligible queued i/os */
639 return (NULL);
643 * For LBA-ordered queues (async / scrub), issue the i/o which follows
644 * the most recently issued i/o in LBA (offset) order.
646 * For FIFO queues (sync), issue the i/o with the lowest timestamp.
648 tree = vdev_queue_class_tree(vq, p);
649 search.io_timestamp = 0;
650 search.io_offset = vq->vq_last_offset + 1;
651 VERIFY3P(avl_find(tree, &search, &idx), ==, NULL);
652 zio = avl_nearest(tree, idx, AVL_AFTER);
653 if (zio == NULL)
654 zio = avl_first(tree);
655 ASSERT3U(zio->io_priority, ==, p);
657 aio = vdev_queue_aggregate(vq, zio);
658 if (aio != NULL)
659 zio = aio;
660 else
661 vdev_queue_io_remove(vq, zio);
664 * If the I/O is or was optional and therefore has no data, we need to
665 * simply discard it. We need to drop the vdev queue's lock to avoid a
666 * deadlock that we could encounter since this I/O will complete
667 * immediately.
669 if (zio->io_flags & ZIO_FLAG_NODATA) {
670 mutex_exit(&vq->vq_lock);
671 zio_vdev_io_bypass(zio);
672 zio_execute(zio);
673 mutex_enter(&vq->vq_lock);
674 goto again;
677 vdev_queue_pending_add(vq, zio);
678 vq->vq_last_offset = zio->io_offset;
680 return (zio);
683 zio_t *
684 vdev_queue_io(zio_t *zio)
686 vdev_queue_t *vq = &zio->io_vd->vdev_queue;
687 zio_t *nio;
689 if (zio->io_flags & ZIO_FLAG_DONT_QUEUE)
690 return (zio);
693 * Children i/os inherent their parent's priority, which might
694 * not match the child's i/o type. Fix it up here.
696 if (zio->io_type == ZIO_TYPE_READ) {
697 if (zio->io_priority != ZIO_PRIORITY_SYNC_READ &&
698 zio->io_priority != ZIO_PRIORITY_ASYNC_READ &&
699 zio->io_priority != ZIO_PRIORITY_SCRUB)
700 zio->io_priority = ZIO_PRIORITY_ASYNC_READ;
701 } else {
702 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
703 if (zio->io_priority != ZIO_PRIORITY_SYNC_WRITE &&
704 zio->io_priority != ZIO_PRIORITY_ASYNC_WRITE)
705 zio->io_priority = ZIO_PRIORITY_ASYNC_WRITE;
708 zio->io_flags |= ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE;
710 mutex_enter(&vq->vq_lock);
711 zio->io_timestamp = gethrtime();
712 vdev_queue_io_add(vq, zio);
713 nio = vdev_queue_io_to_issue(vq);
714 mutex_exit(&vq->vq_lock);
716 if (nio == NULL)
717 return (NULL);
719 if (nio->io_done == vdev_queue_agg_io_done) {
720 zio_nowait(nio);
721 return (NULL);
724 return (nio);
727 void
728 vdev_queue_io_done(zio_t *zio)
730 vdev_queue_t *vq = &zio->io_vd->vdev_queue;
731 zio_t *nio;
733 mutex_enter(&vq->vq_lock);
735 vdev_queue_pending_remove(vq, zio);
737 vq->vq_io_complete_ts = gethrtime();
739 while ((nio = vdev_queue_io_to_issue(vq)) != NULL) {
740 mutex_exit(&vq->vq_lock);
741 if (nio->io_done == vdev_queue_agg_io_done) {
742 zio_nowait(nio);
743 } else {
744 zio_vdev_io_reissue(nio);
745 zio_execute(nio);
747 mutex_enter(&vq->vq_lock);
750 mutex_exit(&vq->vq_lock);