Merge commit '7d815089a43a963b49eaddf97e514194ec29805b'
[unleashed.git] / kernel / fs / zfs / vdev_queue.c
blobf0cca0538d75911ba1351d849cdbf323a14f4204
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;
155 * When the pool has less than zfs_vdev_async_write_active_min_dirty_percent
156 * dirty data, use zfs_vdev_async_write_min_active. When it has more than
157 * zfs_vdev_async_write_active_max_dirty_percent, use
158 * zfs_vdev_async_write_max_active. The value is linearly interpolated
159 * between min and max.
161 int zfs_vdev_async_write_active_min_dirty_percent = 30;
162 int zfs_vdev_async_write_active_max_dirty_percent = 60;
165 * To reduce IOPs, we aggregate small adjacent I/Os into one large I/O.
166 * For read I/Os, we also aggregate across small adjacency gaps; for writes
167 * we include spans of optional I/Os to aid aggregation at the disk even when
168 * they aren't able to help us aggregate at this level.
170 int zfs_vdev_aggregation_limit = SPA_OLD_MAXBLOCKSIZE;
171 int zfs_vdev_read_gap_limit = 32 << 10;
172 int zfs_vdev_write_gap_limit = 4 << 10;
175 * Define the queue depth percentage for each top-level. This percentage is
176 * used in conjunction with zfs_vdev_async_max_active to determine how many
177 * allocations a specific top-level vdev should handle. Once the queue depth
178 * reaches zfs_vdev_queue_depth_pct * zfs_vdev_async_write_max_active / 100
179 * then allocator will stop allocating blocks on that top-level device.
180 * The default kernel setting is 1000% which will yield 100 allocations per
181 * device. For userland testing, the default setting is 300% which equates
182 * to 30 allocations per device.
184 #ifdef _KERNEL
185 int zfs_vdev_queue_depth_pct = 1000;
186 #else
187 int zfs_vdev_queue_depth_pct = 300;
188 #endif
191 * When performing allocations for a given metaslab, we want to make sure that
192 * there are enough IOs to aggregate together to improve throughput. We want to
193 * ensure that there are at least 128k worth of IOs that can be aggregated, and
194 * we assume that the average allocation size is 4k, so we need the queue depth
195 * to be 32 per allocator to get good aggregation of sequential writes.
197 int zfs_vdev_def_queue_depth = 32;
201 vdev_queue_offset_compare(const void *x1, const void *x2)
203 const zio_t *z1 = x1;
204 const zio_t *z2 = x2;
206 if (z1->io_offset < z2->io_offset)
207 return (-1);
208 if (z1->io_offset > z2->io_offset)
209 return (1);
211 if (z1 < z2)
212 return (-1);
213 if (z1 > z2)
214 return (1);
216 return (0);
219 static inline avl_tree_t *
220 vdev_queue_class_tree(vdev_queue_t *vq, zio_priority_t p)
222 return (&vq->vq_class[p].vqc_queued_tree);
225 static inline avl_tree_t *
226 vdev_queue_type_tree(vdev_queue_t *vq, zio_type_t t)
228 ASSERT(t == ZIO_TYPE_READ || t == ZIO_TYPE_WRITE);
229 if (t == ZIO_TYPE_READ)
230 return (&vq->vq_read_offset_tree);
231 else
232 return (&vq->vq_write_offset_tree);
236 vdev_queue_timestamp_compare(const void *x1, const void *x2)
238 const zio_t *z1 = x1;
239 const zio_t *z2 = x2;
241 if (z1->io_timestamp < z2->io_timestamp)
242 return (-1);
243 if (z1->io_timestamp > z2->io_timestamp)
244 return (1);
246 if (z1 < z2)
247 return (-1);
248 if (z1 > z2)
249 return (1);
251 return (0);
254 void
255 vdev_queue_init(vdev_t *vd)
257 vdev_queue_t *vq = &vd->vdev_queue;
259 mutex_init(&vq->vq_lock, NULL, MUTEX_DEFAULT, NULL);
260 vq->vq_vdev = vd;
262 avl_create(&vq->vq_active_tree, vdev_queue_offset_compare,
263 sizeof (zio_t), offsetof(struct zio, io_queue_node));
264 avl_create(vdev_queue_type_tree(vq, ZIO_TYPE_READ),
265 vdev_queue_offset_compare, sizeof (zio_t),
266 offsetof(struct zio, io_offset_node));
267 avl_create(vdev_queue_type_tree(vq, ZIO_TYPE_WRITE),
268 vdev_queue_offset_compare, sizeof (zio_t),
269 offsetof(struct zio, io_offset_node));
271 for (zio_priority_t p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
272 int (*compfn) (const void *, const void *);
275 * The synchronous i/o queues are dispatched in FIFO rather
276 * than LBA order. This provides more consistent latency for
277 * these i/os.
279 if (p == ZIO_PRIORITY_SYNC_READ || p == ZIO_PRIORITY_SYNC_WRITE)
280 compfn = vdev_queue_timestamp_compare;
281 else
282 compfn = vdev_queue_offset_compare;
284 avl_create(vdev_queue_class_tree(vq, p), compfn,
285 sizeof (zio_t), offsetof(struct zio, io_queue_node));
289 void
290 vdev_queue_fini(vdev_t *vd)
292 vdev_queue_t *vq = &vd->vdev_queue;
294 for (zio_priority_t p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++)
295 avl_destroy(vdev_queue_class_tree(vq, p));
296 avl_destroy(&vq->vq_active_tree);
297 avl_destroy(vdev_queue_type_tree(vq, ZIO_TYPE_READ));
298 avl_destroy(vdev_queue_type_tree(vq, ZIO_TYPE_WRITE));
300 mutex_destroy(&vq->vq_lock);
303 static void
304 vdev_queue_io_add(vdev_queue_t *vq, zio_t *zio)
306 spa_t *spa = zio->io_spa;
308 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
309 avl_add(vdev_queue_class_tree(vq, zio->io_priority), zio);
310 avl_add(vdev_queue_type_tree(vq, zio->io_type), zio);
312 mutex_enter(&spa->spa_iokstat_lock);
313 spa->spa_queue_stats[zio->io_priority].spa_queued++;
314 if (spa->spa_iokstat != NULL)
315 kstat_waitq_enter(spa->spa_iokstat->ks_data);
316 mutex_exit(&spa->spa_iokstat_lock);
319 static void
320 vdev_queue_io_remove(vdev_queue_t *vq, zio_t *zio)
322 spa_t *spa = zio->io_spa;
324 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
325 avl_remove(vdev_queue_class_tree(vq, zio->io_priority), zio);
326 avl_remove(vdev_queue_type_tree(vq, zio->io_type), zio);
328 mutex_enter(&spa->spa_iokstat_lock);
329 ASSERT3U(spa->spa_queue_stats[zio->io_priority].spa_queued, >, 0);
330 spa->spa_queue_stats[zio->io_priority].spa_queued--;
331 if (spa->spa_iokstat != NULL)
332 kstat_waitq_exit(spa->spa_iokstat->ks_data);
333 mutex_exit(&spa->spa_iokstat_lock);
336 static void
337 vdev_queue_pending_add(vdev_queue_t *vq, zio_t *zio)
339 spa_t *spa = zio->io_spa;
340 ASSERT(MUTEX_HELD(&vq->vq_lock));
341 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
342 vq->vq_class[zio->io_priority].vqc_active++;
343 avl_add(&vq->vq_active_tree, zio);
345 mutex_enter(&spa->spa_iokstat_lock);
346 spa->spa_queue_stats[zio->io_priority].spa_active++;
347 if (spa->spa_iokstat != NULL)
348 kstat_runq_enter(spa->spa_iokstat->ks_data);
349 mutex_exit(&spa->spa_iokstat_lock);
352 static void
353 vdev_queue_pending_remove(vdev_queue_t *vq, zio_t *zio)
355 spa_t *spa = zio->io_spa;
356 ASSERT(MUTEX_HELD(&vq->vq_lock));
357 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
358 vq->vq_class[zio->io_priority].vqc_active--;
359 avl_remove(&vq->vq_active_tree, zio);
361 mutex_enter(&spa->spa_iokstat_lock);
362 ASSERT3U(spa->spa_queue_stats[zio->io_priority].spa_active, >, 0);
363 spa->spa_queue_stats[zio->io_priority].spa_active--;
364 if (spa->spa_iokstat != NULL) {
365 kstat_io_t *ksio = spa->spa_iokstat->ks_data;
367 kstat_runq_exit(spa->spa_iokstat->ks_data);
368 if (zio->io_type == ZIO_TYPE_READ) {
369 ksio->reads++;
370 ksio->nread += zio->io_size;
371 } else if (zio->io_type == ZIO_TYPE_WRITE) {
372 ksio->writes++;
373 ksio->nwritten += zio->io_size;
376 mutex_exit(&spa->spa_iokstat_lock);
379 static void
380 vdev_queue_agg_io_done(zio_t *aio)
382 if (aio->io_type == ZIO_TYPE_READ) {
383 zio_t *pio;
384 zio_link_t *zl = NULL;
385 while ((pio = zio_walk_parents(aio, &zl)) != NULL) {
386 abd_copy_off(pio->io_abd, aio->io_abd,
387 0, pio->io_offset - aio->io_offset, pio->io_size);
391 abd_free(aio->io_abd);
394 static int
395 vdev_queue_class_min_active(zio_priority_t p)
397 switch (p) {
398 case ZIO_PRIORITY_SYNC_READ:
399 return (zfs_vdev_sync_read_min_active);
400 case ZIO_PRIORITY_SYNC_WRITE:
401 return (zfs_vdev_sync_write_min_active);
402 case ZIO_PRIORITY_ASYNC_READ:
403 return (zfs_vdev_async_read_min_active);
404 case ZIO_PRIORITY_ASYNC_WRITE:
405 return (zfs_vdev_async_write_min_active);
406 case ZIO_PRIORITY_SCRUB:
407 return (zfs_vdev_scrub_min_active);
408 case ZIO_PRIORITY_REMOVAL:
409 return (zfs_vdev_removal_min_active);
410 default:
411 panic("invalid priority %u", p);
412 return (0);
416 static int
417 vdev_queue_max_async_writes(spa_t *spa)
419 int writes;
420 uint64_t dirty = spa->spa_dsl_pool->dp_dirty_total;
421 uint64_t min_bytes = zfs_dirty_data_max *
422 zfs_vdev_async_write_active_min_dirty_percent / 100;
423 uint64_t max_bytes = zfs_dirty_data_max *
424 zfs_vdev_async_write_active_max_dirty_percent / 100;
427 * Sync tasks correspond to interactive user actions. To reduce the
428 * execution time of those actions we push data out as fast as possible.
430 if (spa_has_pending_synctask(spa)) {
431 return (zfs_vdev_async_write_max_active);
434 if (dirty < min_bytes)
435 return (zfs_vdev_async_write_min_active);
436 if (dirty > max_bytes)
437 return (zfs_vdev_async_write_max_active);
440 * linear interpolation:
441 * slope = (max_writes - min_writes) / (max_bytes - min_bytes)
442 * move right by min_bytes
443 * move up by min_writes
445 writes = (dirty - min_bytes) *
446 (zfs_vdev_async_write_max_active -
447 zfs_vdev_async_write_min_active) /
448 (max_bytes - min_bytes) +
449 zfs_vdev_async_write_min_active;
450 ASSERT3U(writes, >=, zfs_vdev_async_write_min_active);
451 ASSERT3U(writes, <=, zfs_vdev_async_write_max_active);
452 return (writes);
455 static int
456 vdev_queue_class_max_active(spa_t *spa, zio_priority_t p)
458 switch (p) {
459 case ZIO_PRIORITY_SYNC_READ:
460 return (zfs_vdev_sync_read_max_active);
461 case ZIO_PRIORITY_SYNC_WRITE:
462 return (zfs_vdev_sync_write_max_active);
463 case ZIO_PRIORITY_ASYNC_READ:
464 return (zfs_vdev_async_read_max_active);
465 case ZIO_PRIORITY_ASYNC_WRITE:
466 return (vdev_queue_max_async_writes(spa));
467 case ZIO_PRIORITY_SCRUB:
468 return (zfs_vdev_scrub_max_active);
469 case ZIO_PRIORITY_REMOVAL:
470 return (zfs_vdev_removal_max_active);
471 default:
472 panic("invalid priority %u", p);
473 return (0);
478 * Return the i/o class to issue from, or ZIO_PRIORITY_MAX_QUEUEABLE if
479 * there is no eligible class.
481 static zio_priority_t
482 vdev_queue_class_to_issue(vdev_queue_t *vq)
484 spa_t *spa = vq->vq_vdev->vdev_spa;
485 zio_priority_t p;
487 if (avl_numnodes(&vq->vq_active_tree) >= zfs_vdev_max_active)
488 return (ZIO_PRIORITY_NUM_QUEUEABLE);
490 /* find a queue that has not reached its minimum # outstanding i/os */
491 for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
492 if (avl_numnodes(vdev_queue_class_tree(vq, p)) > 0 &&
493 vq->vq_class[p].vqc_active <
494 vdev_queue_class_min_active(p))
495 return (p);
499 * If we haven't found a queue, look for one that hasn't reached its
500 * maximum # outstanding i/os.
502 for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
503 if (avl_numnodes(vdev_queue_class_tree(vq, p)) > 0 &&
504 vq->vq_class[p].vqc_active <
505 vdev_queue_class_max_active(spa, p))
506 return (p);
509 /* No eligible queued i/os */
510 return (ZIO_PRIORITY_NUM_QUEUEABLE);
514 * Compute the range spanned by two i/os, which is the endpoint of the last
515 * (lio->io_offset + lio->io_size) minus start of the first (fio->io_offset).
516 * Conveniently, the gap between fio and lio is given by -IO_SPAN(lio, fio);
517 * thus fio and lio are adjacent if and only if IO_SPAN(lio, fio) == 0.
519 #define IO_SPAN(fio, lio) ((lio)->io_offset + (lio)->io_size - (fio)->io_offset)
520 #define IO_GAP(fio, lio) (-IO_SPAN(lio, fio))
522 static zio_t *
523 vdev_queue_aggregate(vdev_queue_t *vq, zio_t *zio)
525 zio_t *first, *last, *aio, *dio, *mandatory, *nio;
526 uint64_t maxgap = 0;
527 uint64_t size;
528 boolean_t stretch = B_FALSE;
529 avl_tree_t *t = vdev_queue_type_tree(vq, zio->io_type);
530 enum zio_flag flags = zio->io_flags & ZIO_FLAG_AGG_INHERIT;
532 if (zio->io_flags & ZIO_FLAG_DONT_AGGREGATE)
533 return (NULL);
535 first = last = zio;
537 if (zio->io_type == ZIO_TYPE_READ)
538 maxgap = zfs_vdev_read_gap_limit;
541 * We can aggregate I/Os that are sufficiently adjacent and of
542 * the same flavor, as expressed by the AGG_INHERIT flags.
543 * The latter requirement is necessary so that certain
544 * attributes of the I/O, such as whether it's a normal I/O
545 * or a scrub/resilver, can be preserved in the aggregate.
546 * We can include optional I/Os, but don't allow them
547 * to begin a range as they add no benefit in that situation.
551 * We keep track of the last non-optional I/O.
553 mandatory = (first->io_flags & ZIO_FLAG_OPTIONAL) ? NULL : first;
556 * Walk backwards through sufficiently contiguous I/Os
557 * recording the last non-optional I/O.
559 while ((dio = AVL_PREV(t, first)) != NULL &&
560 (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags &&
561 IO_SPAN(dio, last) <= zfs_vdev_aggregation_limit &&
562 IO_GAP(dio, first) <= maxgap &&
563 dio->io_type == zio->io_type) {
564 first = dio;
565 if (mandatory == NULL && !(first->io_flags & ZIO_FLAG_OPTIONAL))
566 mandatory = first;
570 * Skip any initial optional I/Os.
572 while ((first->io_flags & ZIO_FLAG_OPTIONAL) && first != last) {
573 first = AVL_NEXT(t, first);
574 ASSERT(first != NULL);
578 * Walk forward through sufficiently contiguous I/Os.
579 * The aggregation limit does not apply to optional i/os, so that
580 * we can issue contiguous writes even if they are larger than the
581 * aggregation limit.
583 while ((dio = AVL_NEXT(t, last)) != NULL &&
584 (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags &&
585 (IO_SPAN(first, dio) <= zfs_vdev_aggregation_limit ||
586 (dio->io_flags & ZIO_FLAG_OPTIONAL)) &&
587 IO_GAP(last, dio) <= maxgap &&
588 dio->io_type == zio->io_type) {
589 last = dio;
590 if (!(last->io_flags & ZIO_FLAG_OPTIONAL))
591 mandatory = last;
595 * Now that we've established the range of the I/O aggregation
596 * we must decide what to do with trailing optional I/Os.
597 * For reads, there's nothing to do. While we are unable to
598 * aggregate further, it's possible that a trailing optional
599 * I/O would allow the underlying device to aggregate with
600 * subsequent I/Os. We must therefore determine if the next
601 * non-optional I/O is close enough to make aggregation
602 * worthwhile.
604 if (zio->io_type == ZIO_TYPE_WRITE && mandatory != NULL) {
605 zio_t *nio = last;
606 while ((dio = AVL_NEXT(t, nio)) != NULL &&
607 IO_GAP(nio, dio) == 0 &&
608 IO_GAP(mandatory, dio) <= zfs_vdev_write_gap_limit) {
609 nio = dio;
610 if (!(nio->io_flags & ZIO_FLAG_OPTIONAL)) {
611 stretch = B_TRUE;
612 break;
617 if (stretch) {
619 * We are going to include an optional io in our aggregated
620 * span, thus closing the write gap. Only mandatory i/os can
621 * start aggregated spans, so make sure that the next i/o
622 * after our span is mandatory.
624 dio = AVL_NEXT(t, last);
625 dio->io_flags &= ~ZIO_FLAG_OPTIONAL;
626 } else {
627 /* do not include the optional i/o */
628 while (last != mandatory && last != first) {
629 ASSERT(last->io_flags & ZIO_FLAG_OPTIONAL);
630 last = AVL_PREV(t, last);
631 ASSERT(last != NULL);
635 if (first == last)
636 return (NULL);
638 size = IO_SPAN(first, last);
639 ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
641 aio = zio_vdev_delegated_io(first->io_vd, first->io_offset,
642 abd_alloc_for_io(size, B_TRUE), size, first->io_type,
643 zio->io_priority, flags | ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE,
644 vdev_queue_agg_io_done, NULL);
645 aio->io_timestamp = first->io_timestamp;
647 nio = first;
648 do {
649 dio = nio;
650 nio = AVL_NEXT(t, dio);
651 ASSERT3U(dio->io_type, ==, aio->io_type);
653 if (dio->io_flags & ZIO_FLAG_NODATA) {
654 ASSERT3U(dio->io_type, ==, ZIO_TYPE_WRITE);
655 abd_zero_off(aio->io_abd,
656 dio->io_offset - aio->io_offset, dio->io_size);
657 } else if (dio->io_type == ZIO_TYPE_WRITE) {
658 abd_copy_off(aio->io_abd, dio->io_abd,
659 dio->io_offset - aio->io_offset, 0, dio->io_size);
662 zio_add_child(dio, aio);
663 vdev_queue_io_remove(vq, dio);
664 zio_vdev_io_bypass(dio);
665 zio_execute(dio);
666 } while (dio != last);
668 return (aio);
671 static zio_t *
672 vdev_queue_io_to_issue(vdev_queue_t *vq)
674 zio_t *zio, *aio;
675 zio_priority_t p;
676 avl_index_t idx;
677 avl_tree_t *tree;
678 zio_t search;
680 again:
681 ASSERT(MUTEX_HELD(&vq->vq_lock));
683 p = vdev_queue_class_to_issue(vq);
685 if (p == ZIO_PRIORITY_NUM_QUEUEABLE) {
686 /* No eligible queued i/os */
687 return (NULL);
691 * For LBA-ordered queues (async / scrub), issue the i/o which follows
692 * the most recently issued i/o in LBA (offset) order.
694 * For FIFO queues (sync), issue the i/o with the lowest timestamp.
696 tree = vdev_queue_class_tree(vq, p);
697 search.io_timestamp = 0;
698 search.io_offset = vq->vq_last_offset + 1;
699 VERIFY3P(avl_find(tree, &search, &idx), ==, NULL);
700 zio = avl_nearest(tree, idx, AVL_AFTER);
701 if (zio == NULL)
702 zio = avl_first(tree);
703 ASSERT3U(zio->io_priority, ==, p);
705 aio = vdev_queue_aggregate(vq, zio);
706 if (aio != NULL)
707 zio = aio;
708 else
709 vdev_queue_io_remove(vq, zio);
712 * If the I/O is or was optional and therefore has no data, we need to
713 * simply discard it. We need to drop the vdev queue's lock to avoid a
714 * deadlock that we could encounter since this I/O will complete
715 * immediately.
717 if (zio->io_flags & ZIO_FLAG_NODATA) {
718 mutex_exit(&vq->vq_lock);
719 zio_vdev_io_bypass(zio);
720 zio_execute(zio);
721 mutex_enter(&vq->vq_lock);
722 goto again;
725 vdev_queue_pending_add(vq, zio);
726 vq->vq_last_offset = zio->io_offset;
728 return (zio);
731 zio_t *
732 vdev_queue_io(zio_t *zio)
734 vdev_queue_t *vq = &zio->io_vd->vdev_queue;
735 zio_t *nio;
737 if (zio->io_flags & ZIO_FLAG_DONT_QUEUE)
738 return (zio);
741 * Children i/os inherent their parent's priority, which might
742 * not match the child's i/o type. Fix it up here.
744 if (zio->io_type == ZIO_TYPE_READ) {
745 if (zio->io_priority != ZIO_PRIORITY_SYNC_READ &&
746 zio->io_priority != ZIO_PRIORITY_ASYNC_READ &&
747 zio->io_priority != ZIO_PRIORITY_SCRUB &&
748 zio->io_priority != ZIO_PRIORITY_REMOVAL)
749 zio->io_priority = ZIO_PRIORITY_ASYNC_READ;
750 } else {
751 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
752 if (zio->io_priority != ZIO_PRIORITY_SYNC_WRITE &&
753 zio->io_priority != ZIO_PRIORITY_ASYNC_WRITE &&
754 zio->io_priority != ZIO_PRIORITY_REMOVAL)
755 zio->io_priority = ZIO_PRIORITY_ASYNC_WRITE;
758 zio->io_flags |= ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE;
760 mutex_enter(&vq->vq_lock);
761 zio->io_timestamp = gethrtime();
762 vdev_queue_io_add(vq, zio);
763 nio = vdev_queue_io_to_issue(vq);
764 mutex_exit(&vq->vq_lock);
766 if (nio == NULL)
767 return (NULL);
769 if (nio->io_done == vdev_queue_agg_io_done) {
770 zio_nowait(nio);
771 return (NULL);
774 return (nio);
777 void
778 vdev_queue_io_done(zio_t *zio)
780 vdev_queue_t *vq = &zio->io_vd->vdev_queue;
781 zio_t *nio;
783 mutex_enter(&vq->vq_lock);
785 vdev_queue_pending_remove(vq, zio);
787 vq->vq_io_complete_ts = gethrtime();
789 while ((nio = vdev_queue_io_to_issue(vq)) != NULL) {
790 mutex_exit(&vq->vq_lock);
791 if (nio->io_done == vdev_queue_agg_io_done) {
792 zio_nowait(nio);
793 } else {
794 zio_vdev_io_reissue(nio);
795 zio_execute(nio);
797 mutex_enter(&vq->vq_lock);
800 mutex_exit(&vq->vq_lock);