]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/vdev_queue.c
Fix self-healing IO prior to dsl_pool_init() completion
[mirror_zfs.git] / module / zfs / vdev_queue.c
1 /*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 */
25
26 /*
27 * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
28 */
29
30 #include <sys/zfs_context.h>
31 #include <sys/vdev_impl.h>
32 #include <sys/spa_impl.h>
33 #include <sys/zio.h>
34 #include <sys/avl.h>
35 #include <sys/dsl_pool.h>
36 #include <sys/spa.h>
37 #include <sys/spa_impl.h>
38 #include <sys/kstat.h>
39
40 /*
41 * ZFS I/O Scheduler
42 * ---------------
43 *
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. If the
52 * sum of the per-queue maximums exceeds the aggregate maximum, then the
53 * number of active i/os may reach zfs_vdev_max_active, in which case no
54 * further i/os will be issued regardless of whether all per-queue
55 * minimums have been met.
56 *
57 * For many physical devices, throughput increases with the number of
58 * concurrent operations, but latency typically suffers. Further, physical
59 * devices typically have a limit at which more concurrent operations have no
60 * effect on throughput or can actually cause it to decrease.
61 *
62 * The scheduler selects the next operation to issue by first looking for an
63 * I/O class whose minimum has not been satisfied. Once all are satisfied and
64 * the aggregate maximum has not been hit, the scheduler looks for classes
65 * whose maximum has not been satisfied. Iteration through the I/O classes is
66 * done in the order specified above. No further operations are issued if the
67 * aggregate maximum number of concurrent operations has been hit or if there
68 * are no operations queued for an I/O class that has not hit its maximum.
69 * Every time an i/o is queued or an operation completes, the I/O scheduler
70 * looks for new operations to issue.
71 *
72 * All I/O classes have a fixed maximum number of outstanding operations
73 * except for the async write class. Asynchronous writes represent the data
74 * that is committed to stable storage during the syncing stage for
75 * transaction groups (see txg.c). Transaction groups enter the syncing state
76 * periodically so the number of queued async writes will quickly burst up and
77 * then bleed down to zero. Rather than servicing them as quickly as possible,
78 * the I/O scheduler changes the maximum number of active async write i/os
79 * according to the amount of dirty data in the pool (see dsl_pool.c). Since
80 * both throughput and latency typically increase with the number of
81 * concurrent operations issued to physical devices, reducing the burstiness
82 * in the number of concurrent operations also stabilizes the response time of
83 * operations from other -- and in particular synchronous -- queues. In broad
84 * strokes, the I/O scheduler will issue more concurrent operations from the
85 * async write queue as there's more dirty data in the pool.
86 *
87 * Async Writes
88 *
89 * The number of concurrent operations issued for the async write I/O class
90 * follows a piece-wise linear function defined by a few adjustable points.
91 *
92 * | o---------| <-- zfs_vdev_async_write_max_active
93 * ^ | /^ |
94 * | | / | |
95 * active | / | |
96 * I/O | / | |
97 * count | / | |
98 * | / | |
99 * |------------o | | <-- zfs_vdev_async_write_min_active
100 * 0|____________^______|_________|
101 * 0% | | 100% of zfs_dirty_data_max
102 * | |
103 * | `-- zfs_vdev_async_write_active_max_dirty_percent
104 * `--------- zfs_vdev_async_write_active_min_dirty_percent
105 *
106 * Until the amount of dirty data exceeds a minimum percentage of the dirty
107 * data allowed in the pool, the I/O scheduler will limit the number of
108 * concurrent operations to the minimum. As that threshold is crossed, the
109 * number of concurrent operations issued increases linearly to the maximum at
110 * the specified maximum percentage of the dirty data allowed in the pool.
111 *
112 * Ideally, the amount of dirty data on a busy pool will stay in the sloped
113 * part of the function between zfs_vdev_async_write_active_min_dirty_percent
114 * and zfs_vdev_async_write_active_max_dirty_percent. If it exceeds the
115 * maximum percentage, this indicates that the rate of incoming data is
116 * greater than the rate that the backend storage can handle. In this case, we
117 * must further throttle incoming writes (see dmu_tx_delay() for details).
118 */
119
120 /*
121 * The maximum number of i/os active to each device. Ideally, this will be >=
122 * the sum of each queue's max_active. It must be at least the sum of each
123 * queue's min_active.
124 */
125 uint32_t zfs_vdev_max_active = 1000;
126
127 /*
128 * Per-queue limits on the number of i/os active to each device. If the
129 * number of active i/os is < zfs_vdev_max_active, then the min_active comes
130 * into play. We will send min_active from each queue, and then select from
131 * queues in the order defined by zio_priority_t.
132 *
133 * In general, smaller max_active's will lead to lower latency of synchronous
134 * operations. Larger max_active's may lead to higher overall throughput,
135 * depending on underlying storage.
136 *
137 * The ratio of the queues' max_actives determines the balance of performance
138 * between reads, writes, and scrubs. E.g., increasing
139 * zfs_vdev_scrub_max_active will cause the scrub or resilver to complete
140 * more quickly, but reads and writes to have higher latency and lower
141 * throughput.
142 */
143 uint32_t zfs_vdev_sync_read_min_active = 10;
144 uint32_t zfs_vdev_sync_read_max_active = 10;
145 uint32_t zfs_vdev_sync_write_min_active = 10;
146 uint32_t zfs_vdev_sync_write_max_active = 10;
147 uint32_t zfs_vdev_async_read_min_active = 1;
148 uint32_t zfs_vdev_async_read_max_active = 3;
149 uint32_t zfs_vdev_async_write_min_active = 1;
150 uint32_t zfs_vdev_async_write_max_active = 10;
151 uint32_t zfs_vdev_scrub_min_active = 1;
152 uint32_t zfs_vdev_scrub_max_active = 2;
153
154 /*
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.
160 */
161 int zfs_vdev_async_write_active_min_dirty_percent = 30;
162 int zfs_vdev_async_write_active_max_dirty_percent = 60;
163
164 /*
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.
169 */
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;
173
174 int
175 vdev_queue_offset_compare(const void *x1, const void *x2)
176 {
177 const zio_t *z1 = x1;
178 const zio_t *z2 = x2;
179
180 if (z1->io_offset < z2->io_offset)
181 return (-1);
182 if (z1->io_offset > z2->io_offset)
183 return (1);
184
185 if (z1 < z2)
186 return (-1);
187 if (z1 > z2)
188 return (1);
189
190 return (0);
191 }
192
193 static inline avl_tree_t *
194 vdev_queue_class_tree(vdev_queue_t *vq, zio_priority_t p)
195 {
196 return (&vq->vq_class[p].vqc_queued_tree);
197 }
198
199 static inline avl_tree_t *
200 vdev_queue_type_tree(vdev_queue_t *vq, zio_type_t t)
201 {
202 ASSERT(t == ZIO_TYPE_READ || t == ZIO_TYPE_WRITE);
203 if (t == ZIO_TYPE_READ)
204 return (&vq->vq_read_offset_tree);
205 else
206 return (&vq->vq_write_offset_tree);
207 }
208
209 int
210 vdev_queue_timestamp_compare(const void *x1, const void *x2)
211 {
212 const zio_t *z1 = x1;
213 const zio_t *z2 = x2;
214
215 if (z1->io_timestamp < z2->io_timestamp)
216 return (-1);
217 if (z1->io_timestamp > z2->io_timestamp)
218 return (1);
219
220 if (z1 < z2)
221 return (-1);
222 if (z1 > z2)
223 return (1);
224
225 return (0);
226 }
227
228 static int
229 vdev_queue_class_min_active(zio_priority_t p)
230 {
231 switch (p) {
232 case ZIO_PRIORITY_SYNC_READ:
233 return (zfs_vdev_sync_read_min_active);
234 case ZIO_PRIORITY_SYNC_WRITE:
235 return (zfs_vdev_sync_write_min_active);
236 case ZIO_PRIORITY_ASYNC_READ:
237 return (zfs_vdev_async_read_min_active);
238 case ZIO_PRIORITY_ASYNC_WRITE:
239 return (zfs_vdev_async_write_min_active);
240 case ZIO_PRIORITY_SCRUB:
241 return (zfs_vdev_scrub_min_active);
242 default:
243 panic("invalid priority %u", p);
244 return (0);
245 }
246 }
247
248 static int
249 vdev_queue_max_async_writes(spa_t *spa)
250 {
251 int writes;
252 uint64_t dirty = 0;
253 dsl_pool_t *dp = spa_get_dsl(spa);
254 uint64_t min_bytes = zfs_dirty_data_max *
255 zfs_vdev_async_write_active_min_dirty_percent / 100;
256 uint64_t max_bytes = zfs_dirty_data_max *
257 zfs_vdev_async_write_active_max_dirty_percent / 100;
258
259 /*
260 * Async writes may occur before the assignment of the spa's
261 * dsl_pool_t if a self-healing zio is issued prior to the
262 * completion of dmu_objset_open_impl().
263 */
264 if (dp == NULL)
265 return (zfs_vdev_async_write_max_active);
266
267 /*
268 * Sync tasks correspond to interactive user actions. To reduce the
269 * execution time of those actions we push data out as fast as possible.
270 */
271 if (spa_has_pending_synctask(spa))
272 return (zfs_vdev_async_write_max_active);
273
274 dirty = dp->dp_dirty_total;
275 if (dirty < min_bytes)
276 return (zfs_vdev_async_write_min_active);
277 if (dirty > max_bytes)
278 return (zfs_vdev_async_write_max_active);
279
280 /*
281 * linear interpolation:
282 * slope = (max_writes - min_writes) / (max_bytes - min_bytes)
283 * move right by min_bytes
284 * move up by min_writes
285 */
286 writes = (dirty - min_bytes) *
287 (zfs_vdev_async_write_max_active -
288 zfs_vdev_async_write_min_active) /
289 (max_bytes - min_bytes) +
290 zfs_vdev_async_write_min_active;
291 ASSERT3U(writes, >=, zfs_vdev_async_write_min_active);
292 ASSERT3U(writes, <=, zfs_vdev_async_write_max_active);
293 return (writes);
294 }
295
296 static int
297 vdev_queue_class_max_active(spa_t *spa, zio_priority_t p)
298 {
299 switch (p) {
300 case ZIO_PRIORITY_SYNC_READ:
301 return (zfs_vdev_sync_read_max_active);
302 case ZIO_PRIORITY_SYNC_WRITE:
303 return (zfs_vdev_sync_write_max_active);
304 case ZIO_PRIORITY_ASYNC_READ:
305 return (zfs_vdev_async_read_max_active);
306 case ZIO_PRIORITY_ASYNC_WRITE:
307 return (vdev_queue_max_async_writes(spa));
308 case ZIO_PRIORITY_SCRUB:
309 return (zfs_vdev_scrub_max_active);
310 default:
311 panic("invalid priority %u", p);
312 return (0);
313 }
314 }
315
316 /*
317 * Return the i/o class to issue from, or ZIO_PRIORITY_MAX_QUEUEABLE if
318 * there is no eligible class.
319 */
320 static zio_priority_t
321 vdev_queue_class_to_issue(vdev_queue_t *vq)
322 {
323 spa_t *spa = vq->vq_vdev->vdev_spa;
324 zio_priority_t p;
325
326 if (avl_numnodes(&vq->vq_active_tree) >= zfs_vdev_max_active)
327 return (ZIO_PRIORITY_NUM_QUEUEABLE);
328
329 /* find a queue that has not reached its minimum # outstanding i/os */
330 for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
331 if (avl_numnodes(vdev_queue_class_tree(vq, p)) > 0 &&
332 vq->vq_class[p].vqc_active <
333 vdev_queue_class_min_active(p))
334 return (p);
335 }
336
337 /*
338 * If we haven't found a queue, look for one that hasn't reached its
339 * maximum # outstanding i/os.
340 */
341 for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
342 if (avl_numnodes(vdev_queue_class_tree(vq, p)) > 0 &&
343 vq->vq_class[p].vqc_active <
344 vdev_queue_class_max_active(spa, p))
345 return (p);
346 }
347
348 /* No eligible queued i/os */
349 return (ZIO_PRIORITY_NUM_QUEUEABLE);
350 }
351
352 void
353 vdev_queue_init(vdev_t *vd)
354 {
355 vdev_queue_t *vq = &vd->vdev_queue;
356 zio_priority_t p;
357
358 mutex_init(&vq->vq_lock, NULL, MUTEX_DEFAULT, NULL);
359 vq->vq_vdev = vd;
360 taskq_init_ent(&vd->vdev_queue.vq_io_search.io_tqent);
361
362 avl_create(&vq->vq_active_tree, vdev_queue_offset_compare,
363 sizeof (zio_t), offsetof(struct zio, io_queue_node));
364 avl_create(vdev_queue_type_tree(vq, ZIO_TYPE_READ),
365 vdev_queue_offset_compare, sizeof (zio_t),
366 offsetof(struct zio, io_offset_node));
367 avl_create(vdev_queue_type_tree(vq, ZIO_TYPE_WRITE),
368 vdev_queue_offset_compare, sizeof (zio_t),
369 offsetof(struct zio, io_offset_node));
370
371 for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++) {
372 int (*compfn) (const void *, const void *);
373
374 /*
375 * The synchronous i/o queues are dispatched in FIFO rather
376 * than LBA order. This provides more consistent latency for
377 * these i/os.
378 */
379 if (p == ZIO_PRIORITY_SYNC_READ || p == ZIO_PRIORITY_SYNC_WRITE)
380 compfn = vdev_queue_timestamp_compare;
381 else
382 compfn = vdev_queue_offset_compare;
383 avl_create(vdev_queue_class_tree(vq, p), compfn,
384 sizeof (zio_t), offsetof(struct zio, io_queue_node));
385 }
386
387 vq->vq_lastoffset = 0;
388 }
389
390 void
391 vdev_queue_fini(vdev_t *vd)
392 {
393 vdev_queue_t *vq = &vd->vdev_queue;
394 zio_priority_t p;
395
396 for (p = 0; p < ZIO_PRIORITY_NUM_QUEUEABLE; p++)
397 avl_destroy(vdev_queue_class_tree(vq, p));
398 avl_destroy(&vq->vq_active_tree);
399 avl_destroy(vdev_queue_type_tree(vq, ZIO_TYPE_READ));
400 avl_destroy(vdev_queue_type_tree(vq, ZIO_TYPE_WRITE));
401
402 mutex_destroy(&vq->vq_lock);
403 }
404
405 static void
406 vdev_queue_io_add(vdev_queue_t *vq, zio_t *zio)
407 {
408 spa_t *spa = zio->io_spa;
409 spa_stats_history_t *ssh = &spa->spa_stats.io_history;
410
411 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
412 avl_add(vdev_queue_class_tree(vq, zio->io_priority), zio);
413 avl_add(vdev_queue_type_tree(vq, zio->io_type), zio);
414
415 if (ssh->kstat != NULL) {
416 mutex_enter(&ssh->lock);
417 kstat_waitq_enter(ssh->kstat->ks_data);
418 mutex_exit(&ssh->lock);
419 }
420 }
421
422 static void
423 vdev_queue_io_remove(vdev_queue_t *vq, zio_t *zio)
424 {
425 spa_t *spa = zio->io_spa;
426 spa_stats_history_t *ssh = &spa->spa_stats.io_history;
427
428 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
429 avl_remove(vdev_queue_class_tree(vq, zio->io_priority), zio);
430 avl_remove(vdev_queue_type_tree(vq, zio->io_type), zio);
431
432 if (ssh->kstat != NULL) {
433 mutex_enter(&ssh->lock);
434 kstat_waitq_exit(ssh->kstat->ks_data);
435 mutex_exit(&ssh->lock);
436 }
437 }
438
439 static void
440 vdev_queue_pending_add(vdev_queue_t *vq, zio_t *zio)
441 {
442 spa_t *spa = zio->io_spa;
443 spa_stats_history_t *ssh = &spa->spa_stats.io_history;
444
445 ASSERT(MUTEX_HELD(&vq->vq_lock));
446 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
447 vq->vq_class[zio->io_priority].vqc_active++;
448 avl_add(&vq->vq_active_tree, zio);
449
450 if (ssh->kstat != NULL) {
451 mutex_enter(&ssh->lock);
452 kstat_runq_enter(ssh->kstat->ks_data);
453 mutex_exit(&ssh->lock);
454 }
455 }
456
457 static void
458 vdev_queue_pending_remove(vdev_queue_t *vq, zio_t *zio)
459 {
460 spa_t *spa = zio->io_spa;
461 spa_stats_history_t *ssh = &spa->spa_stats.io_history;
462
463 ASSERT(MUTEX_HELD(&vq->vq_lock));
464 ASSERT3U(zio->io_priority, <, ZIO_PRIORITY_NUM_QUEUEABLE);
465 vq->vq_class[zio->io_priority].vqc_active--;
466 avl_remove(&vq->vq_active_tree, zio);
467
468 if (ssh->kstat != NULL) {
469 kstat_io_t *ksio = ssh->kstat->ks_data;
470
471 mutex_enter(&ssh->lock);
472 kstat_runq_exit(ksio);
473 if (zio->io_type == ZIO_TYPE_READ) {
474 ksio->reads++;
475 ksio->nread += zio->io_size;
476 } else if (zio->io_type == ZIO_TYPE_WRITE) {
477 ksio->writes++;
478 ksio->nwritten += zio->io_size;
479 }
480 mutex_exit(&ssh->lock);
481 }
482 }
483
484 static void
485 vdev_queue_agg_io_done(zio_t *aio)
486 {
487 if (aio->io_type == ZIO_TYPE_READ) {
488 zio_t *pio;
489 while ((pio = zio_walk_parents(aio)) != NULL) {
490 bcopy((char *)aio->io_data + (pio->io_offset -
491 aio->io_offset), pio->io_data, pio->io_size);
492 }
493 }
494
495 zio_buf_free(aio->io_data, aio->io_size);
496 }
497
498 /*
499 * Compute the range spanned by two i/os, which is the endpoint of the last
500 * (lio->io_offset + lio->io_size) minus start of the first (fio->io_offset).
501 * Conveniently, the gap between fio and lio is given by -IO_SPAN(lio, fio);
502 * thus fio and lio are adjacent if and only if IO_SPAN(lio, fio) == 0.
503 */
504 #define IO_SPAN(fio, lio) ((lio)->io_offset + (lio)->io_size - (fio)->io_offset)
505 #define IO_GAP(fio, lio) (-IO_SPAN(lio, fio))
506
507 static zio_t *
508 vdev_queue_aggregate(vdev_queue_t *vq, zio_t *zio)
509 {
510 zio_t *first, *last, *aio, *dio, *mandatory, *nio;
511 uint64_t maxgap = 0;
512 uint64_t size;
513 uint64_t limit;
514 boolean_t stretch = B_FALSE;
515 avl_tree_t *t = vdev_queue_type_tree(vq, zio->io_type);
516 enum zio_flag flags = zio->io_flags & ZIO_FLAG_AGG_INHERIT;
517 void *buf;
518
519 limit = MAX(MIN(zfs_vdev_aggregation_limit,
520 spa_maxblocksize(vq->vq_vdev->vdev_spa)), 0);
521
522 if (zio->io_flags & ZIO_FLAG_DONT_AGGREGATE || limit == 0)
523 return (NULL);
524
525 first = last = zio;
526
527 if (zio->io_type == ZIO_TYPE_READ)
528 maxgap = zfs_vdev_read_gap_limit;
529
530 /*
531 * We can aggregate I/Os that are sufficiently adjacent and of
532 * the same flavor, as expressed by the AGG_INHERIT flags.
533 * The latter requirement is necessary so that certain
534 * attributes of the I/O, such as whether it's a normal I/O
535 * or a scrub/resilver, can be preserved in the aggregate.
536 * We can include optional I/Os, but don't allow them
537 * to begin a range as they add no benefit in that situation.
538 */
539
540 /*
541 * We keep track of the last non-optional I/O.
542 */
543 mandatory = (first->io_flags & ZIO_FLAG_OPTIONAL) ? NULL : first;
544
545 /*
546 * Walk backwards through sufficiently contiguous I/Os
547 * recording the last non-option I/O.
548 */
549 while ((dio = AVL_PREV(t, first)) != NULL &&
550 (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags &&
551 IO_SPAN(dio, last) <= limit &&
552 IO_GAP(dio, first) <= maxgap) {
553 first = dio;
554 if (mandatory == NULL && !(first->io_flags & ZIO_FLAG_OPTIONAL))
555 mandatory = first;
556 }
557
558 /*
559 * Skip any initial optional I/Os.
560 */
561 while ((first->io_flags & ZIO_FLAG_OPTIONAL) && first != last) {
562 first = AVL_NEXT(t, first);
563 ASSERT(first != NULL);
564 }
565
566
567 /*
568 * Walk forward through sufficiently contiguous I/Os.
569 */
570 while ((dio = AVL_NEXT(t, last)) != NULL &&
571 (dio->io_flags & ZIO_FLAG_AGG_INHERIT) == flags &&
572 IO_SPAN(first, dio) <= limit &&
573 IO_GAP(last, dio) <= maxgap) {
574 last = dio;
575 if (!(last->io_flags & ZIO_FLAG_OPTIONAL))
576 mandatory = last;
577 }
578
579 /*
580 * Now that we've established the range of the I/O aggregation
581 * we must decide what to do with trailing optional I/Os.
582 * For reads, there's nothing to do. While we are unable to
583 * aggregate further, it's possible that a trailing optional
584 * I/O would allow the underlying device to aggregate with
585 * subsequent I/Os. We must therefore determine if the next
586 * non-optional I/O is close enough to make aggregation
587 * worthwhile.
588 */
589 if (zio->io_type == ZIO_TYPE_WRITE && mandatory != NULL) {
590 zio_t *nio = last;
591 while ((dio = AVL_NEXT(t, nio)) != NULL &&
592 IO_GAP(nio, dio) == 0 &&
593 IO_GAP(mandatory, dio) <= zfs_vdev_write_gap_limit) {
594 nio = dio;
595 if (!(nio->io_flags & ZIO_FLAG_OPTIONAL)) {
596 stretch = B_TRUE;
597 break;
598 }
599 }
600 }
601
602 if (stretch) {
603 /* This may be a no-op. */
604 dio = AVL_NEXT(t, last);
605 dio->io_flags &= ~ZIO_FLAG_OPTIONAL;
606 } else {
607 while (last != mandatory && last != first) {
608 ASSERT(last->io_flags & ZIO_FLAG_OPTIONAL);
609 last = AVL_PREV(t, last);
610 ASSERT(last != NULL);
611 }
612 }
613
614 if (first == last)
615 return (NULL);
616
617 size = IO_SPAN(first, last);
618 ASSERT3U(size, <=, limit);
619
620 buf = zio_buf_alloc_flags(size, KM_NOSLEEP);
621 if (buf == NULL)
622 return (NULL);
623
624 aio = zio_vdev_delegated_io(first->io_vd, first->io_offset,
625 buf, size, first->io_type, zio->io_priority,
626 flags | ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE,
627 vdev_queue_agg_io_done, NULL);
628 aio->io_timestamp = first->io_timestamp;
629
630 nio = first;
631 do {
632 dio = nio;
633 nio = AVL_NEXT(t, dio);
634 ASSERT3U(dio->io_type, ==, aio->io_type);
635
636 if (dio->io_flags & ZIO_FLAG_NODATA) {
637 ASSERT3U(dio->io_type, ==, ZIO_TYPE_WRITE);
638 bzero((char *)aio->io_data + (dio->io_offset -
639 aio->io_offset), dio->io_size);
640 } else if (dio->io_type == ZIO_TYPE_WRITE) {
641 bcopy(dio->io_data, (char *)aio->io_data +
642 (dio->io_offset - aio->io_offset),
643 dio->io_size);
644 }
645
646 zio_add_child(dio, aio);
647 vdev_queue_io_remove(vq, dio);
648 zio_vdev_io_bypass(dio);
649 zio_execute(dio);
650 } while (dio != last);
651
652 return (aio);
653 }
654
655 static zio_t *
656 vdev_queue_io_to_issue(vdev_queue_t *vq)
657 {
658 zio_t *zio, *aio;
659 zio_priority_t p;
660 avl_index_t idx;
661 avl_tree_t *tree;
662
663 again:
664 ASSERT(MUTEX_HELD(&vq->vq_lock));
665
666 p = vdev_queue_class_to_issue(vq);
667
668 if (p == ZIO_PRIORITY_NUM_QUEUEABLE) {
669 /* No eligible queued i/os */
670 return (NULL);
671 }
672
673 /*
674 * For LBA-ordered queues (async / scrub), issue the i/o which follows
675 * the most recently issued i/o in LBA (offset) order.
676 *
677 * For FIFO queues (sync), issue the i/o with the lowest timestamp.
678 */
679 tree = vdev_queue_class_tree(vq, p);
680 vq->vq_io_search.io_timestamp = 0;
681 vq->vq_io_search.io_offset = vq->vq_last_offset + 1;
682 VERIFY3P(avl_find(tree, &vq->vq_io_search,
683 &idx), ==, NULL);
684 zio = avl_nearest(tree, idx, AVL_AFTER);
685 if (zio == NULL)
686 zio = avl_first(tree);
687 ASSERT3U(zio->io_priority, ==, p);
688
689 aio = vdev_queue_aggregate(vq, zio);
690 if (aio != NULL)
691 zio = aio;
692 else
693 vdev_queue_io_remove(vq, zio);
694
695 /*
696 * If the I/O is or was optional and therefore has no data, we need to
697 * simply discard it. We need to drop the vdev queue's lock to avoid a
698 * deadlock that we could encounter since this I/O will complete
699 * immediately.
700 */
701 if (zio->io_flags & ZIO_FLAG_NODATA) {
702 mutex_exit(&vq->vq_lock);
703 zio_vdev_io_bypass(zio);
704 zio_execute(zio);
705 mutex_enter(&vq->vq_lock);
706 goto again;
707 }
708
709 vdev_queue_pending_add(vq, zio);
710 vq->vq_last_offset = zio->io_offset;
711
712 return (zio);
713 }
714
715 zio_t *
716 vdev_queue_io(zio_t *zio)
717 {
718 vdev_queue_t *vq = &zio->io_vd->vdev_queue;
719 zio_t *nio;
720
721 if (zio->io_flags & ZIO_FLAG_DONT_QUEUE)
722 return (zio);
723
724 /*
725 * Children i/os inherent their parent's priority, which might
726 * not match the child's i/o type. Fix it up here.
727 */
728 if (zio->io_type == ZIO_TYPE_READ) {
729 if (zio->io_priority != ZIO_PRIORITY_SYNC_READ &&
730 zio->io_priority != ZIO_PRIORITY_ASYNC_READ &&
731 zio->io_priority != ZIO_PRIORITY_SCRUB)
732 zio->io_priority = ZIO_PRIORITY_ASYNC_READ;
733 } else {
734 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
735 if (zio->io_priority != ZIO_PRIORITY_SYNC_WRITE &&
736 zio->io_priority != ZIO_PRIORITY_ASYNC_WRITE)
737 zio->io_priority = ZIO_PRIORITY_ASYNC_WRITE;
738 }
739
740 zio->io_flags |= ZIO_FLAG_DONT_CACHE | ZIO_FLAG_DONT_QUEUE;
741
742 mutex_enter(&vq->vq_lock);
743 zio->io_timestamp = gethrtime();
744 vdev_queue_io_add(vq, zio);
745 nio = vdev_queue_io_to_issue(vq);
746 mutex_exit(&vq->vq_lock);
747
748 if (nio == NULL)
749 return (NULL);
750
751 if (nio->io_done == vdev_queue_agg_io_done) {
752 zio_nowait(nio);
753 return (NULL);
754 }
755
756 return (nio);
757 }
758
759 void
760 vdev_queue_io_done(zio_t *zio)
761 {
762 vdev_queue_t *vq = &zio->io_vd->vdev_queue;
763 zio_t *nio;
764
765 mutex_enter(&vq->vq_lock);
766
767 vdev_queue_pending_remove(vq, zio);
768
769 zio->io_delta = gethrtime() - zio->io_timestamp;
770 vq->vq_io_complete_ts = gethrtime();
771 vq->vq_io_delta_ts = vq->vq_io_complete_ts - zio->io_timestamp;
772
773 while ((nio = vdev_queue_io_to_issue(vq)) != NULL) {
774 mutex_exit(&vq->vq_lock);
775 if (nio->io_done == vdev_queue_agg_io_done) {
776 zio_nowait(nio);
777 } else {
778 zio_vdev_io_reissue(nio);
779 zio_execute(nio);
780 }
781 mutex_enter(&vq->vq_lock);
782 }
783
784 mutex_exit(&vq->vq_lock);
785 }
786
787 /*
788 * As these three methods are only used for load calculations we're not
789 * concerned if we get an incorrect value on 32bit platforms due to lack of
790 * vq_lock mutex use here, instead we prefer to keep it lock free for
791 * performance.
792 */
793 int
794 vdev_queue_length(vdev_t *vd)
795 {
796 return (avl_numnodes(&vd->vdev_queue.vq_active_tree));
797 }
798
799 uint64_t
800 vdev_queue_lastoffset(vdev_t *vd)
801 {
802 return (vd->vdev_queue.vq_lastoffset);
803 }
804
805 void
806 vdev_queue_register_lastoffset(vdev_t *vd, zio_t *zio)
807 {
808 vd->vdev_queue.vq_lastoffset = zio->io_offset + zio->io_size;
809 }
810
811 #if defined(_KERNEL) && defined(HAVE_SPL)
812 module_param(zfs_vdev_aggregation_limit, int, 0644);
813 MODULE_PARM_DESC(zfs_vdev_aggregation_limit, "Max vdev I/O aggregation size");
814
815 module_param(zfs_vdev_read_gap_limit, int, 0644);
816 MODULE_PARM_DESC(zfs_vdev_read_gap_limit, "Aggregate read I/O over gap");
817
818 module_param(zfs_vdev_write_gap_limit, int, 0644);
819 MODULE_PARM_DESC(zfs_vdev_write_gap_limit, "Aggregate write I/O over gap");
820
821 module_param(zfs_vdev_max_active, int, 0644);
822 MODULE_PARM_DESC(zfs_vdev_max_active, "Maximum number of active I/Os per vdev");
823
824 module_param(zfs_vdev_async_write_active_max_dirty_percent, int, 0644);
825 MODULE_PARM_DESC(zfs_vdev_async_write_active_max_dirty_percent,
826 "Async write concurrency max threshold");
827
828 module_param(zfs_vdev_async_write_active_min_dirty_percent, int, 0644);
829 MODULE_PARM_DESC(zfs_vdev_async_write_active_min_dirty_percent,
830 "Async write concurrency min threshold");
831
832 module_param(zfs_vdev_async_read_max_active, int, 0644);
833 MODULE_PARM_DESC(zfs_vdev_async_read_max_active,
834 "Max active async read I/Os per vdev");
835
836 module_param(zfs_vdev_async_read_min_active, int, 0644);
837 MODULE_PARM_DESC(zfs_vdev_async_read_min_active,
838 "Min active async read I/Os per vdev");
839
840 module_param(zfs_vdev_async_write_max_active, int, 0644);
841 MODULE_PARM_DESC(zfs_vdev_async_write_max_active,
842 "Max active async write I/Os per vdev");
843
844 module_param(zfs_vdev_async_write_min_active, int, 0644);
845 MODULE_PARM_DESC(zfs_vdev_async_write_min_active,
846 "Min active async write I/Os per vdev");
847
848 module_param(zfs_vdev_scrub_max_active, int, 0644);
849 MODULE_PARM_DESC(zfs_vdev_scrub_max_active, "Max active scrub I/Os per vdev");
850
851 module_param(zfs_vdev_scrub_min_active, int, 0644);
852 MODULE_PARM_DESC(zfs_vdev_scrub_min_active, "Min active scrub I/Os per vdev");
853
854 module_param(zfs_vdev_sync_read_max_active, int, 0644);
855 MODULE_PARM_DESC(zfs_vdev_sync_read_max_active,
856 "Max active sync read I/Os per vdev");
857
858 module_param(zfs_vdev_sync_read_min_active, int, 0644);
859 MODULE_PARM_DESC(zfs_vdev_sync_read_min_active,
860 "Min active sync read I/Os per vdev");
861
862 module_param(zfs_vdev_sync_write_max_active, int, 0644);
863 MODULE_PARM_DESC(zfs_vdev_sync_write_max_active,
864 "Max active sync write I/Os per vdev");
865
866 module_param(zfs_vdev_sync_write_min_active, int, 0644);
867 MODULE_PARM_DESC(zfs_vdev_sync_write_min_active,
868 "Min active sync write I/Os per vdev");
869 #endif