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/vdev_impl.h>
34 #include <sys/fs/zfs.h>
37 * Virtual device vector for mirroring.
40 typedef struct mirror_child {
47 uint8_t mc_speculative;
50 typedef struct mirror_map {
54 boolean_t mm_replacing;
56 mirror_child_t mm_child[];
59 static int vdev_mirror_shift = 21;
62 SYSCTL_DECL(_vfs_zfs_vdev);
63 static SYSCTL_NODE(_vfs_zfs_vdev, OID_AUTO, mirror, CTLFLAG_RD, 0,
68 * The load configuration settings below are tuned by default for
69 * the case where all devices are of the same rotational type.
71 * If there is a mixture of rotating and non-rotating media, setting
72 * non_rotating_seek_inc to 0 may well provide better results as it
73 * will direct more reads to the non-rotating vdevs which are more
74 * likely to have a higher performance.
77 /* Rotating media load calculation configuration. */
78 static int rotating_inc = 0;
80 SYSCTL_INT(_vfs_zfs_vdev_mirror, OID_AUTO, rotating_inc, CTLFLAG_RWTUN,
81 &rotating_inc, 0, "Rotating media load increment for non-seeking I/O's");
84 static int rotating_seek_inc = 5;
86 SYSCTL_INT(_vfs_zfs_vdev_mirror, OID_AUTO, rotating_seek_inc, CTLFLAG_RWTUN,
87 &rotating_seek_inc, 0, "Rotating media load increment for seeking I/O's");
90 static int rotating_seek_offset = 1 * 1024 * 1024;
92 SYSCTL_INT(_vfs_zfs_vdev_mirror, OID_AUTO, rotating_seek_offset, CTLFLAG_RWTUN,
93 &rotating_seek_offset, 0, "Offset in bytes from the last I/O which "
94 "triggers a reduced rotating media seek increment");
97 /* Non-rotating media load calculation configuration. */
98 static int non_rotating_inc = 0;
100 SYSCTL_INT(_vfs_zfs_vdev_mirror, OID_AUTO, non_rotating_inc, CTLFLAG_RWTUN,
101 &non_rotating_inc, 0,
102 "Non-rotating media load increment for non-seeking I/O's");
105 static int non_rotating_seek_inc = 1;
107 SYSCTL_INT(_vfs_zfs_vdev_mirror, OID_AUTO, non_rotating_seek_inc, CTLFLAG_RWTUN,
108 &non_rotating_seek_inc, 0,
109 "Non-rotating media load increment for seeking I/O's");
114 vdev_mirror_map_size(int children)
116 return (offsetof(mirror_map_t, mm_child[children]) +
117 sizeof(int) * children);
120 static inline mirror_map_t *
121 vdev_mirror_map_alloc(int children, boolean_t replacing, boolean_t root)
125 mm = kmem_zalloc(vdev_mirror_map_size(children), KM_SLEEP);
126 mm->mm_children = children;
127 mm->mm_replacing = replacing;
129 mm->mm_preferred = (int *)((uintptr_t)mm +
130 offsetof(mirror_map_t, mm_child[children]));
136 vdev_mirror_map_free(zio_t *zio)
138 mirror_map_t *mm = zio->io_vsd;
140 kmem_free(mm, vdev_mirror_map_size(mm->mm_children));
143 static const zio_vsd_ops_t vdev_mirror_vsd_ops = {
144 vdev_mirror_map_free,
145 zio_vsd_default_cksum_report
149 vdev_mirror_load(mirror_map_t *mm, vdev_t *vd, uint64_t zio_offset)
154 /* All DVAs have equal weight at the root. */
159 * We don't return INT_MAX if the device is resilvering i.e.
160 * vdev_resilver_txg != 0 as when tested performance was slightly
161 * worse overall when resilvering with compared to without.
164 /* Standard load based on pending queue length. */
165 load = vdev_queue_length(vd);
166 lastoffset = vdev_queue_lastoffset(vd);
168 if (vd->vdev_rotation_rate == VDEV_RATE_NON_ROTATING) {
169 /* Non-rotating media. */
170 if (lastoffset == zio_offset)
171 return (load + non_rotating_inc);
174 * Apply a seek penalty even for non-rotating devices as
175 * sequential I/O'a can be aggregated into fewer operations
176 * on the device, thus avoiding unnecessary per-command
177 * overhead and boosting performance.
179 return (load + non_rotating_seek_inc);
182 /* Rotating media I/O's which directly follow the last I/O. */
183 if (lastoffset == zio_offset)
184 return (load + rotating_inc);
187 * Apply half the seek increment to I/O's within seek offset
188 * of the last I/O queued to this vdev as they should incure less
189 * of a seek increment.
191 if (ABS(lastoffset - zio_offset) < rotating_seek_offset)
192 return (load + (rotating_seek_inc / 2));
194 /* Apply the full seek increment to all other I/O's. */
195 return (load + rotating_seek_inc);
199 static mirror_map_t *
200 vdev_mirror_map_init(zio_t *zio)
202 mirror_map_t *mm = NULL;
204 vdev_t *vd = zio->io_vd;
208 dva_t *dva = zio->io_bp->blk_dva;
209 spa_t *spa = zio->io_spa;
211 mm = vdev_mirror_map_alloc(BP_GET_NDVAS(zio->io_bp), B_FALSE,
213 for (c = 0; c < mm->mm_children; c++) {
214 mc = &mm->mm_child[c];
215 mc->mc_vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[c]));
216 mc->mc_offset = DVA_GET_OFFSET(&dva[c]);
219 mm = vdev_mirror_map_alloc(vd->vdev_children,
220 (vd->vdev_ops == &vdev_replacing_ops ||
221 vd->vdev_ops == &vdev_spare_ops), B_FALSE);
222 for (c = 0; c < mm->mm_children; c++) {
223 mc = &mm->mm_child[c];
224 mc->mc_vd = vd->vdev_child[c];
225 mc->mc_offset = zio->io_offset;
230 zio->io_vsd_ops = &vdev_mirror_vsd_ops;
235 vdev_mirror_open(vdev_t *vd, uint64_t *asize, uint64_t *max_asize,
236 uint64_t *logical_ashift, uint64_t *physical_ashift)
241 if (vd->vdev_children == 0) {
242 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
243 return (SET_ERROR(EINVAL));
246 vdev_open_children(vd);
248 for (int c = 0; c < vd->vdev_children; c++) {
249 vdev_t *cvd = vd->vdev_child[c];
251 if (cvd->vdev_open_error) {
252 lasterror = cvd->vdev_open_error;
257 *asize = MIN(*asize - 1, cvd->vdev_asize - 1) + 1;
258 *max_asize = MIN(*max_asize - 1, cvd->vdev_max_asize - 1) + 1;
259 *logical_ashift = MAX(*logical_ashift, cvd->vdev_ashift);
260 *physical_ashift = MAX(*physical_ashift,
261 cvd->vdev_physical_ashift);
264 if (numerrors == vd->vdev_children) {
265 vd->vdev_stat.vs_aux = VDEV_AUX_NO_REPLICAS;
273 vdev_mirror_close(vdev_t *vd)
275 for (int c = 0; c < vd->vdev_children; c++)
276 vdev_close(vd->vdev_child[c]);
280 vdev_mirror_child_done(zio_t *zio)
282 mirror_child_t *mc = zio->io_private;
284 mc->mc_error = zio->io_error;
290 vdev_mirror_scrub_done(zio_t *zio)
292 mirror_child_t *mc = zio->io_private;
294 if (zio->io_error == 0) {
296 zio_link_t *zl = NULL;
298 mutex_enter(&zio->io_lock);
299 while ((pio = zio_walk_parents(zio, &zl)) != NULL) {
300 mutex_enter(&pio->io_lock);
301 ASSERT3U(zio->io_size, >=, pio->io_size);
302 bcopy(zio->io_data, pio->io_data, pio->io_size);
303 mutex_exit(&pio->io_lock);
305 mutex_exit(&zio->io_lock);
308 zio_buf_free(zio->io_data, zio->io_size);
310 mc->mc_error = zio->io_error;
316 * Check the other, lower-index DVAs to see if they're on the same
317 * vdev as the child we picked. If they are, use them since they
318 * are likely to have been allocated from the primary metaslab in
319 * use at the time, and hence are more likely to have locality with
323 vdev_mirror_dva_select(zio_t *zio, int p)
325 dva_t *dva = zio->io_bp->blk_dva;
326 mirror_map_t *mm = zio->io_vsd;
330 preferred = mm->mm_preferred[p];
331 for (p-- ; p >= 0; p--) {
332 c = mm->mm_preferred[p];
333 if (DVA_GET_VDEV(&dva[c]) == DVA_GET_VDEV(&dva[preferred]))
340 vdev_mirror_preferred_child_randomize(zio_t *zio)
342 mirror_map_t *mm = zio->io_vsd;
346 p = spa_get_random(mm->mm_preferred_cnt);
347 return (vdev_mirror_dva_select(zio, p));
351 * To ensure we don't always favour the first matching vdev,
352 * which could lead to wear leveling issues on SSD's, we
353 * use the I/O offset as a pseudo random seed into the vdevs
354 * which have the lowest load.
356 p = (zio->io_offset >> vdev_mirror_shift) % mm->mm_preferred_cnt;
357 return (mm->mm_preferred[p]);
361 * Try to find a vdev whose DTL doesn't contain the block we want to read
362 * prefering vdevs based on determined load.
364 * If we can't, try the read on any vdev we haven't already tried.
367 vdev_mirror_child_select(zio_t *zio)
369 mirror_map_t *mm = zio->io_vsd;
370 uint64_t txg = zio->io_txg;
373 ASSERT(zio->io_bp == NULL || BP_PHYSICAL_BIRTH(zio->io_bp) == txg);
375 lowest_load = INT_MAX;
376 mm->mm_preferred_cnt = 0;
377 for (c = 0; c < mm->mm_children; c++) {
380 mc = &mm->mm_child[c];
381 if (mc->mc_tried || mc->mc_skipped)
384 if (!vdev_readable(mc->mc_vd)) {
385 mc->mc_error = SET_ERROR(ENXIO);
386 mc->mc_tried = 1; /* don't even try */
391 if (vdev_dtl_contains(mc->mc_vd, DTL_MISSING, txg, 1)) {
392 mc->mc_error = SET_ERROR(ESTALE);
394 mc->mc_speculative = 1;
398 mc->mc_load = vdev_mirror_load(mm, mc->mc_vd, mc->mc_offset);
399 if (mc->mc_load > lowest_load)
402 if (mc->mc_load < lowest_load) {
403 lowest_load = mc->mc_load;
404 mm->mm_preferred_cnt = 0;
406 mm->mm_preferred[mm->mm_preferred_cnt] = c;
407 mm->mm_preferred_cnt++;
410 if (mm->mm_preferred_cnt == 1) {
411 vdev_queue_register_lastoffset(
412 mm->mm_child[mm->mm_preferred[0]].mc_vd, zio);
413 return (mm->mm_preferred[0]);
416 if (mm->mm_preferred_cnt > 1) {
417 int c = vdev_mirror_preferred_child_randomize(zio);
419 vdev_queue_register_lastoffset(mm->mm_child[c].mc_vd, zio);
424 * Every device is either missing or has this txg in its DTL.
425 * Look for any child we haven't already tried before giving up.
427 for (c = 0; c < mm->mm_children; c++) {
428 if (!mm->mm_child[c].mc_tried) {
429 vdev_queue_register_lastoffset(mm->mm_child[c].mc_vd,
436 * Every child failed. There's no place left to look.
442 vdev_mirror_io_start(zio_t *zio)
448 mm = vdev_mirror_map_init(zio);
450 if (zio->io_type == ZIO_TYPE_READ) {
451 if ((zio->io_flags & ZIO_FLAG_SCRUB) && !mm->mm_replacing &&
452 mm->mm_children > 1) {
454 * For scrubbing reads we need to allocate a read
455 * buffer for each child and issue reads to all
456 * children. If any child succeeds, it will copy its
457 * data into zio->io_data in vdev_mirror_scrub_done.
459 for (c = 0; c < mm->mm_children; c++) {
460 mc = &mm->mm_child[c];
461 zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
462 mc->mc_vd, mc->mc_offset,
463 zio_buf_alloc(zio->io_size), zio->io_size,
464 zio->io_type, zio->io_priority, 0,
465 vdev_mirror_scrub_done, mc));
471 * For normal reads just pick one child.
473 c = vdev_mirror_child_select(zio);
476 ASSERT(zio->io_type == ZIO_TYPE_WRITE ||
477 zio->io_type == ZIO_TYPE_FREE);
480 * Writes and frees go to all children.
483 children = mm->mm_children;
487 mc = &mm->mm_child[c];
488 zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
489 mc->mc_vd, mc->mc_offset, zio->io_data, zio->io_size,
490 zio->io_type, zio->io_priority, 0,
491 vdev_mirror_child_done, mc));
499 vdev_mirror_worst_error(mirror_map_t *mm)
501 int error[2] = { 0, 0 };
503 for (int c = 0; c < mm->mm_children; c++) {
504 mirror_child_t *mc = &mm->mm_child[c];
505 int s = mc->mc_speculative;
506 error[s] = zio_worst_error(error[s], mc->mc_error);
509 return (error[0] ? error[0] : error[1]);
513 vdev_mirror_io_done(zio_t *zio)
515 mirror_map_t *mm = zio->io_vsd;
519 int unexpected_errors = 0;
521 for (c = 0; c < mm->mm_children; c++) {
522 mc = &mm->mm_child[c];
527 } else if (mc->mc_tried) {
532 if (zio->io_type == ZIO_TYPE_WRITE) {
534 * XXX -- for now, treat partial writes as success.
536 * Now that we support write reallocation, it would be better
537 * to treat partial failure as real failure unless there are
538 * no non-degraded top-level vdevs left, and not update DTLs
539 * if we intend to reallocate.
542 if (good_copies != mm->mm_children) {
544 * Always require at least one good copy.
546 * For ditto blocks (io_vd == NULL), require
547 * all copies to be good.
549 * XXX -- for replacing vdevs, there's no great answer.
550 * If the old device is really dead, we may not even
551 * be able to access it -- so we only want to
552 * require good writes to the new device. But if
553 * the new device turns out to be flaky, we want
554 * to be able to detach it -- which requires all
555 * writes to the old device to have succeeded.
557 if (good_copies == 0 || zio->io_vd == NULL)
558 zio->io_error = vdev_mirror_worst_error(mm);
561 } else if (zio->io_type == ZIO_TYPE_FREE) {
565 ASSERT(zio->io_type == ZIO_TYPE_READ);
568 * If we don't have a good copy yet, keep trying other children.
571 if (good_copies == 0 && (c = vdev_mirror_child_select(zio)) != -1) {
572 ASSERT(c >= 0 && c < mm->mm_children);
573 mc = &mm->mm_child[c];
574 zio_vdev_io_redone(zio);
575 zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
576 mc->mc_vd, mc->mc_offset, zio->io_data, zio->io_size,
577 ZIO_TYPE_READ, zio->io_priority, 0,
578 vdev_mirror_child_done, mc));
583 if (good_copies == 0) {
584 zio->io_error = vdev_mirror_worst_error(mm);
585 ASSERT(zio->io_error != 0);
588 if (good_copies && spa_writeable(zio->io_spa) &&
589 (unexpected_errors ||
590 (zio->io_flags & ZIO_FLAG_RESILVER) ||
591 ((zio->io_flags & ZIO_FLAG_SCRUB) && mm->mm_replacing))) {
593 * Use the good data we have in hand to repair damaged children.
595 for (c = 0; c < mm->mm_children; c++) {
597 * Don't rewrite known good children.
598 * Not only is it unnecessary, it could
599 * actually be harmful: if the system lost
600 * power while rewriting the only good copy,
601 * there would be no good copies left!
603 mc = &mm->mm_child[c];
605 if (mc->mc_error == 0) {
608 if (!(zio->io_flags & ZIO_FLAG_SCRUB) &&
609 !vdev_dtl_contains(mc->mc_vd, DTL_PARTIAL,
612 mc->mc_error = SET_ERROR(ESTALE);
615 zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
616 mc->mc_vd, mc->mc_offset,
617 zio->io_data, zio->io_size,
618 ZIO_TYPE_WRITE, ZIO_PRIORITY_ASYNC_WRITE,
619 ZIO_FLAG_IO_REPAIR | (unexpected_errors ?
620 ZIO_FLAG_SELF_HEAL : 0), NULL, NULL));
626 vdev_mirror_state_change(vdev_t *vd, int faulted, int degraded)
628 if (faulted == vd->vdev_children)
629 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
630 VDEV_AUX_NO_REPLICAS);
631 else if (degraded + faulted != 0)
632 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, VDEV_AUX_NONE);
634 vdev_set_state(vd, B_FALSE, VDEV_STATE_HEALTHY, VDEV_AUX_NONE);
637 vdev_ops_t vdev_mirror_ops = {
641 vdev_mirror_io_start,
643 vdev_mirror_state_change,
646 VDEV_TYPE_MIRROR, /* name of this vdev type */
647 B_FALSE /* not a leaf vdev */
650 vdev_ops_t vdev_replacing_ops = {
654 vdev_mirror_io_start,
656 vdev_mirror_state_change,
659 VDEV_TYPE_REPLACING, /* name of this vdev type */
660 B_FALSE /* not a leaf vdev */
663 vdev_ops_t vdev_spare_ops = {
667 vdev_mirror_io_start,
669 vdev_mirror_state_change,
672 VDEV_TYPE_SPARE, /* name of this vdev type */
673 B_FALSE /* not a leaf vdev */