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]
22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
27 * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
30 #include <sys/zfs_context.h>
32 #include <sys/spa_impl.h>
33 #include <sys/dsl_pool.h>
34 #include <sys/dsl_scan.h>
35 #include <sys/vdev_impl.h>
38 #include <sys/fs/zfs.h>
41 * Virtual device vector for mirroring.
44 typedef struct mirror_child {
51 uint8_t mc_speculative;
54 typedef struct mirror_map {
58 boolean_t mm_resilvering;
60 mirror_child_t mm_child[];
63 static int vdev_mirror_shift = 21;
66 SYSCTL_DECL(_vfs_zfs_vdev);
67 static SYSCTL_NODE(_vfs_zfs_vdev, OID_AUTO, mirror, CTLFLAG_RD, 0,
72 * The load configuration settings below are tuned by default for
73 * the case where all devices are of the same rotational type.
75 * If there is a mixture of rotating and non-rotating media, setting
76 * non_rotating_seek_inc to 0 may well provide better results as it
77 * will direct more reads to the non-rotating vdevs which are more
78 * likely to have a higher performance.
81 /* Rotating media load calculation configuration. */
82 static int rotating_inc = 0;
84 SYSCTL_INT(_vfs_zfs_vdev_mirror, OID_AUTO, rotating_inc, CTLFLAG_RWTUN,
85 &rotating_inc, 0, "Rotating media load increment for non-seeking I/O's");
88 static int rotating_seek_inc = 5;
90 SYSCTL_INT(_vfs_zfs_vdev_mirror, OID_AUTO, rotating_seek_inc, CTLFLAG_RWTUN,
91 &rotating_seek_inc, 0, "Rotating media load increment for seeking I/O's");
94 static int rotating_seek_offset = 1 * 1024 * 1024;
96 SYSCTL_INT(_vfs_zfs_vdev_mirror, OID_AUTO, rotating_seek_offset, CTLFLAG_RWTUN,
97 &rotating_seek_offset, 0, "Offset in bytes from the last I/O which "
98 "triggers a reduced rotating media seek increment");
101 /* Non-rotating media load calculation configuration. */
102 static int non_rotating_inc = 0;
104 SYSCTL_INT(_vfs_zfs_vdev_mirror, OID_AUTO, non_rotating_inc, CTLFLAG_RWTUN,
105 &non_rotating_inc, 0,
106 "Non-rotating media load increment for non-seeking I/O's");
109 static int non_rotating_seek_inc = 1;
111 SYSCTL_INT(_vfs_zfs_vdev_mirror, OID_AUTO, non_rotating_seek_inc, CTLFLAG_RWTUN,
112 &non_rotating_seek_inc, 0,
113 "Non-rotating media load increment for seeking I/O's");
118 vdev_mirror_map_size(int children)
120 return (offsetof(mirror_map_t, mm_child[children]) +
121 sizeof(int) * children);
124 static inline mirror_map_t *
125 vdev_mirror_map_alloc(int children, boolean_t resilvering, boolean_t root)
129 mm = kmem_zalloc(vdev_mirror_map_size(children), KM_SLEEP);
130 mm->mm_children = children;
131 mm->mm_resilvering = resilvering;
133 mm->mm_preferred = (int *)((uintptr_t)mm +
134 offsetof(mirror_map_t, mm_child[children]));
140 vdev_mirror_map_free(zio_t *zio)
142 mirror_map_t *mm = zio->io_vsd;
144 kmem_free(mm, vdev_mirror_map_size(mm->mm_children));
147 static const zio_vsd_ops_t vdev_mirror_vsd_ops = {
148 vdev_mirror_map_free,
149 zio_vsd_default_cksum_report
153 vdev_mirror_load(mirror_map_t *mm, vdev_t *vd, uint64_t zio_offset)
158 /* All DVAs have equal weight at the root. */
163 * We don't return INT_MAX if the device is resilvering i.e.
164 * vdev_resilver_txg != 0 as when tested performance was slightly
165 * worse overall when resilvering with compared to without.
168 /* Standard load based on pending queue length. */
169 load = vdev_queue_length(vd);
170 lastoffset = vdev_queue_lastoffset(vd);
172 if (vd->vdev_rotation_rate == VDEV_RATE_NON_ROTATING) {
173 /* Non-rotating media. */
174 if (lastoffset == zio_offset)
175 return (load + non_rotating_inc);
178 * Apply a seek penalty even for non-rotating devices as
179 * sequential I/O'a can be aggregated into fewer operations
180 * on the device, thus avoiding unnecessary per-command
181 * overhead and boosting performance.
183 return (load + non_rotating_seek_inc);
186 /* Rotating media I/O's which directly follow the last I/O. */
187 if (lastoffset == zio_offset)
188 return (load + rotating_inc);
191 * Apply half the seek increment to I/O's within seek offset
192 * of the last I/O queued to this vdev as they should incure less
193 * of a seek increment.
195 if (ABS(lastoffset - zio_offset) < rotating_seek_offset)
196 return (load + (rotating_seek_inc / 2));
198 /* Apply the full seek increment to all other I/O's. */
199 return (load + rotating_seek_inc);
203 static mirror_map_t *
204 vdev_mirror_map_init(zio_t *zio)
206 mirror_map_t *mm = NULL;
208 vdev_t *vd = zio->io_vd;
212 dva_t *dva = zio->io_bp->blk_dva;
213 spa_t *spa = zio->io_spa;
215 mm = vdev_mirror_map_alloc(BP_GET_NDVAS(zio->io_bp), B_FALSE,
217 for (c = 0; c < mm->mm_children; c++) {
218 mc = &mm->mm_child[c];
219 mc->mc_vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[c]));
220 mc->mc_offset = DVA_GET_OFFSET(&dva[c]);
224 * If we are resilvering, then we should handle scrub reads
225 * differently; we shouldn't issue them to the resilvering
226 * device because it might not have those blocks.
228 * We are resilvering iff:
229 * 1) We are a replacing vdev (ie our name is "replacing-1" or
230 * "spare-1" or something like that), and
231 * 2) The pool is currently being resilvered.
233 * We cannot simply check vd->vdev_resilver_txg, because it's
234 * not set in this path.
236 * Nor can we just check our vdev_ops; there are cases (such as
237 * when a user types "zpool replace pool odev spare_dev" and
238 * spare_dev is in the spare list, or when a spare device is
239 * automatically used to replace a DEGRADED device) when
240 * resilvering is complete but both the original vdev and the
241 * spare vdev remain in the pool. That behavior is intentional.
242 * It helps implement the policy that a spare should be
243 * automatically removed from the pool after the user replaces
244 * the device that originally failed.
246 * If a spa load is in progress, then spa_dsl_pool may be
247 * uninitialized. But we shouldn't be resilvering during a spa
250 boolean_t replacing = (vd->vdev_ops == &vdev_replacing_ops ||
251 vd->vdev_ops == &vdev_spare_ops) &&
252 spa_load_state(vd->vdev_spa) == SPA_LOAD_NONE &&
253 dsl_scan_resilvering(vd->vdev_spa->spa_dsl_pool);
254 mm = vdev_mirror_map_alloc(vd->vdev_children, replacing,
256 for (c = 0; c < mm->mm_children; c++) {
257 mc = &mm->mm_child[c];
258 mc->mc_vd = vd->vdev_child[c];
259 mc->mc_offset = zio->io_offset;
264 zio->io_vsd_ops = &vdev_mirror_vsd_ops;
269 vdev_mirror_open(vdev_t *vd, uint64_t *asize, uint64_t *max_asize,
270 uint64_t *logical_ashift, uint64_t *physical_ashift)
275 if (vd->vdev_children == 0) {
276 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
277 return (SET_ERROR(EINVAL));
280 vdev_open_children(vd);
282 for (int c = 0; c < vd->vdev_children; c++) {
283 vdev_t *cvd = vd->vdev_child[c];
285 if (cvd->vdev_open_error) {
286 lasterror = cvd->vdev_open_error;
291 *asize = MIN(*asize - 1, cvd->vdev_asize - 1) + 1;
292 *max_asize = MIN(*max_asize - 1, cvd->vdev_max_asize - 1) + 1;
293 *logical_ashift = MAX(*logical_ashift, cvd->vdev_ashift);
294 *physical_ashift = MAX(*physical_ashift,
295 cvd->vdev_physical_ashift);
298 if (numerrors == vd->vdev_children) {
299 vd->vdev_stat.vs_aux = VDEV_AUX_NO_REPLICAS;
307 vdev_mirror_close(vdev_t *vd)
309 for (int c = 0; c < vd->vdev_children; c++)
310 vdev_close(vd->vdev_child[c]);
314 vdev_mirror_child_done(zio_t *zio)
316 mirror_child_t *mc = zio->io_private;
318 mc->mc_error = zio->io_error;
324 vdev_mirror_scrub_done(zio_t *zio)
326 mirror_child_t *mc = zio->io_private;
328 if (zio->io_error == 0) {
330 zio_link_t *zl = NULL;
332 mutex_enter(&zio->io_lock);
333 while ((pio = zio_walk_parents(zio, &zl)) != NULL) {
334 mutex_enter(&pio->io_lock);
335 ASSERT3U(zio->io_size, >=, pio->io_size);
336 abd_copy(pio->io_abd, zio->io_abd, pio->io_size);
337 mutex_exit(&pio->io_lock);
339 mutex_exit(&zio->io_lock);
341 abd_free(zio->io_abd);
343 mc->mc_error = zio->io_error;
349 * Check the other, lower-index DVAs to see if they're on the same
350 * vdev as the child we picked. If they are, use them since they
351 * are likely to have been allocated from the primary metaslab in
352 * use at the time, and hence are more likely to have locality with
356 vdev_mirror_dva_select(zio_t *zio, int p)
358 dva_t *dva = zio->io_bp->blk_dva;
359 mirror_map_t *mm = zio->io_vsd;
363 preferred = mm->mm_preferred[p];
364 for (p-- ; p >= 0; p--) {
365 c = mm->mm_preferred[p];
366 if (DVA_GET_VDEV(&dva[c]) == DVA_GET_VDEV(&dva[preferred]))
373 vdev_mirror_preferred_child_randomize(zio_t *zio)
375 mirror_map_t *mm = zio->io_vsd;
379 p = spa_get_random(mm->mm_preferred_cnt);
380 return (vdev_mirror_dva_select(zio, p));
384 * To ensure we don't always favour the first matching vdev,
385 * which could lead to wear leveling issues on SSD's, we
386 * use the I/O offset as a pseudo random seed into the vdevs
387 * which have the lowest load.
389 p = (zio->io_offset >> vdev_mirror_shift) % mm->mm_preferred_cnt;
390 return (mm->mm_preferred[p]);
394 * Try to find a vdev whose DTL doesn't contain the block we want to read
395 * prefering vdevs based on determined load.
397 * If we can't, try the read on any vdev we haven't already tried.
400 vdev_mirror_child_select(zio_t *zio)
402 mirror_map_t *mm = zio->io_vsd;
403 uint64_t txg = zio->io_txg;
406 ASSERT(zio->io_bp == NULL || BP_PHYSICAL_BIRTH(zio->io_bp) == txg);
408 lowest_load = INT_MAX;
409 mm->mm_preferred_cnt = 0;
410 for (c = 0; c < mm->mm_children; c++) {
413 mc = &mm->mm_child[c];
414 if (mc->mc_tried || mc->mc_skipped)
417 if (!vdev_readable(mc->mc_vd)) {
418 mc->mc_error = SET_ERROR(ENXIO);
419 mc->mc_tried = 1; /* don't even try */
424 if (vdev_dtl_contains(mc->mc_vd, DTL_MISSING, txg, 1)) {
425 mc->mc_error = SET_ERROR(ESTALE);
427 mc->mc_speculative = 1;
431 mc->mc_load = vdev_mirror_load(mm, mc->mc_vd, mc->mc_offset);
432 if (mc->mc_load > lowest_load)
435 if (mc->mc_load < lowest_load) {
436 lowest_load = mc->mc_load;
437 mm->mm_preferred_cnt = 0;
439 mm->mm_preferred[mm->mm_preferred_cnt] = c;
440 mm->mm_preferred_cnt++;
443 if (mm->mm_preferred_cnt == 1) {
444 vdev_queue_register_lastoffset(
445 mm->mm_child[mm->mm_preferred[0]].mc_vd, zio);
446 return (mm->mm_preferred[0]);
449 if (mm->mm_preferred_cnt > 1) {
450 int c = vdev_mirror_preferred_child_randomize(zio);
452 vdev_queue_register_lastoffset(mm->mm_child[c].mc_vd, zio);
457 * Every device is either missing or has this txg in its DTL.
458 * Look for any child we haven't already tried before giving up.
460 for (c = 0; c < mm->mm_children; c++) {
461 if (!mm->mm_child[c].mc_tried) {
462 vdev_queue_register_lastoffset(mm->mm_child[c].mc_vd,
469 * Every child failed. There's no place left to look.
475 vdev_mirror_io_start(zio_t *zio)
481 mm = vdev_mirror_map_init(zio);
483 if (zio->io_type == ZIO_TYPE_READ) {
484 if ((zio->io_flags & ZIO_FLAG_SCRUB) && !mm->mm_resilvering &&
485 mm->mm_children > 1) {
487 * For scrubbing reads we need to allocate a read
488 * buffer for each child and issue reads to all
489 * children. If any child succeeds, it will copy its
490 * data into zio->io_data in vdev_mirror_scrub_done.
492 for (c = 0; c < mm->mm_children; c++) {
493 mc = &mm->mm_child[c];
494 zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
495 mc->mc_vd, mc->mc_offset,
496 abd_alloc_sametype(zio->io_abd,
497 zio->io_size), zio->io_size,
498 zio->io_type, zio->io_priority, 0,
499 vdev_mirror_scrub_done, mc));
505 * For normal reads just pick one child.
507 c = vdev_mirror_child_select(zio);
510 ASSERT(zio->io_type == ZIO_TYPE_WRITE ||
511 zio->io_type == ZIO_TYPE_FREE);
514 * Writes and frees go to all children.
517 children = mm->mm_children;
521 mc = &mm->mm_child[c];
522 zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
523 mc->mc_vd, mc->mc_offset, zio->io_abd, zio->io_size,
524 zio->io_type, zio->io_priority, 0,
525 vdev_mirror_child_done, mc));
533 vdev_mirror_worst_error(mirror_map_t *mm)
535 int error[2] = { 0, 0 };
537 for (int c = 0; c < mm->mm_children; c++) {
538 mirror_child_t *mc = &mm->mm_child[c];
539 int s = mc->mc_speculative;
540 error[s] = zio_worst_error(error[s], mc->mc_error);
543 return (error[0] ? error[0] : error[1]);
547 vdev_mirror_io_done(zio_t *zio)
549 mirror_map_t *mm = zio->io_vsd;
553 int unexpected_errors = 0;
555 for (c = 0; c < mm->mm_children; c++) {
556 mc = &mm->mm_child[c];
561 } else if (mc->mc_tried) {
566 if (zio->io_type == ZIO_TYPE_WRITE) {
568 * XXX -- for now, treat partial writes as success.
570 * Now that we support write reallocation, it would be better
571 * to treat partial failure as real failure unless there are
572 * no non-degraded top-level vdevs left, and not update DTLs
573 * if we intend to reallocate.
576 if (good_copies != mm->mm_children) {
578 * Always require at least one good copy.
580 * For ditto blocks (io_vd == NULL), require
581 * all copies to be good.
583 * XXX -- for replacing vdevs, there's no great answer.
584 * If the old device is really dead, we may not even
585 * be able to access it -- so we only want to
586 * require good writes to the new device. But if
587 * the new device turns out to be flaky, we want
588 * to be able to detach it -- which requires all
589 * writes to the old device to have succeeded.
591 if (good_copies == 0 || zio->io_vd == NULL)
592 zio->io_error = vdev_mirror_worst_error(mm);
595 } else if (zio->io_type == ZIO_TYPE_FREE) {
599 ASSERT(zio->io_type == ZIO_TYPE_READ);
602 * If we don't have a good copy yet, keep trying other children.
605 if (good_copies == 0 && (c = vdev_mirror_child_select(zio)) != -1) {
606 ASSERT(c >= 0 && c < mm->mm_children);
607 mc = &mm->mm_child[c];
608 zio_vdev_io_redone(zio);
609 zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
610 mc->mc_vd, mc->mc_offset, zio->io_abd, zio->io_size,
611 ZIO_TYPE_READ, zio->io_priority, 0,
612 vdev_mirror_child_done, mc));
617 if (good_copies == 0) {
618 zio->io_error = vdev_mirror_worst_error(mm);
619 ASSERT(zio->io_error != 0);
622 if (good_copies && spa_writeable(zio->io_spa) &&
623 (unexpected_errors ||
624 (zio->io_flags & ZIO_FLAG_RESILVER) ||
625 ((zio->io_flags & ZIO_FLAG_SCRUB) && mm->mm_resilvering))) {
627 * Use the good data we have in hand to repair damaged children.
629 for (c = 0; c < mm->mm_children; c++) {
631 * Don't rewrite known good children.
632 * Not only is it unnecessary, it could
633 * actually be harmful: if the system lost
634 * power while rewriting the only good copy,
635 * there would be no good copies left!
637 mc = &mm->mm_child[c];
639 if (mc->mc_error == 0) {
642 if (!(zio->io_flags & ZIO_FLAG_SCRUB) &&
643 !vdev_dtl_contains(mc->mc_vd, DTL_PARTIAL,
646 mc->mc_error = SET_ERROR(ESTALE);
649 zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
650 mc->mc_vd, mc->mc_offset,
651 zio->io_abd, zio->io_size,
652 ZIO_TYPE_WRITE, ZIO_PRIORITY_ASYNC_WRITE,
653 ZIO_FLAG_IO_REPAIR | (unexpected_errors ?
654 ZIO_FLAG_SELF_HEAL : 0), NULL, NULL));
660 vdev_mirror_state_change(vdev_t *vd, int faulted, int degraded)
662 if (faulted == vd->vdev_children)
663 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
664 VDEV_AUX_NO_REPLICAS);
665 else if (degraded + faulted != 0)
666 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, VDEV_AUX_NONE);
668 vdev_set_state(vd, B_FALSE, VDEV_STATE_HEALTHY, VDEV_AUX_NONE);
671 vdev_ops_t vdev_mirror_ops = {
675 vdev_mirror_io_start,
677 vdev_mirror_state_change,
681 VDEV_TYPE_MIRROR, /* name of this vdev type */
682 B_FALSE /* not a leaf vdev */
685 vdev_ops_t vdev_replacing_ops = {
689 vdev_mirror_io_start,
691 vdev_mirror_state_change,
695 VDEV_TYPE_REPLACING, /* name of this vdev type */
696 B_FALSE /* not a leaf vdev */
699 vdev_ops_t vdev_spare_ops = {
703 vdev_mirror_io_start,
705 vdev_mirror_state_change,
709 VDEV_TYPE_SPARE, /* name of this vdev type */
710 B_FALSE /* not a leaf vdev */