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) 2013 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;
61 SYSCTL_DECL(_vfs_zfs_vdev);
62 static SYSCTL_NODE(_vfs_zfs_vdev, OID_AUTO, mirror, CTLFLAG_RD, 0,
66 * The load configuration settings below are tuned by default for
67 * the case where all devices are of the same rotational type.
69 * If there is a mixture of rotating and non-rotating media, setting
70 * non_rotating_seek_inc to 0 may well provide better results as it
71 * will direct more reads to the non-rotating vdevs which are more
72 * likely to have a higher performance.
75 /* Rotating media load calculation configuration. */
76 static int rotating_inc = 0;
77 SYSCTL_INT(_vfs_zfs_vdev_mirror, OID_AUTO, rotating_inc, CTLFLAG_RWTUN,
78 &rotating_inc, 0, "Rotating media load increment for non-seeking I/O's");
80 static int rotating_seek_inc = 5;
81 SYSCTL_INT(_vfs_zfs_vdev_mirror, OID_AUTO, rotating_seek_inc, CTLFLAG_RWTUN,
82 &rotating_seek_inc, 0, "Rotating media load increment for seeking I/O's");
84 static int rotating_seek_offset = 1 * 1024 * 1024;
85 SYSCTL_INT(_vfs_zfs_vdev_mirror, OID_AUTO, rotating_seek_offset, CTLFLAG_RWTUN,
86 &rotating_seek_offset, 0, "Offset in bytes from the last I/O which "
87 "triggers a reduced rotating media seek increment");
89 /* Non-rotating media load calculation configuration. */
90 static int non_rotating_inc = 0;
91 SYSCTL_INT(_vfs_zfs_vdev_mirror, OID_AUTO, non_rotating_inc, CTLFLAG_RWTUN,
93 "Non-rotating media load increment for non-seeking I/O's");
95 static int non_rotating_seek_inc = 1;
96 SYSCTL_INT(_vfs_zfs_vdev_mirror, OID_AUTO, non_rotating_seek_inc, CTLFLAG_RWTUN,
97 &non_rotating_seek_inc, 0,
98 "Non-rotating media load increment for seeking I/O's");
102 vdev_mirror_map_size(int children)
104 return (offsetof(mirror_map_t, mm_child[children]) +
105 sizeof(int) * children);
108 static inline mirror_map_t *
109 vdev_mirror_map_alloc(int children, boolean_t replacing, boolean_t root)
113 mm = kmem_zalloc(vdev_mirror_map_size(children), KM_SLEEP);
114 mm->mm_children = children;
115 mm->mm_replacing = replacing;
117 mm->mm_preferred = (int *)((uintptr_t)mm +
118 offsetof(mirror_map_t, mm_child[children]));
124 vdev_mirror_map_free(zio_t *zio)
126 mirror_map_t *mm = zio->io_vsd;
128 kmem_free(mm, vdev_mirror_map_size(mm->mm_children));
131 static const zio_vsd_ops_t vdev_mirror_vsd_ops = {
132 vdev_mirror_map_free,
133 zio_vsd_default_cksum_report
137 vdev_mirror_load(mirror_map_t *mm, vdev_t *vd, uint64_t zio_offset)
142 /* All DVAs have equal weight at the root. */
147 * We don't return INT_MAX if the device is resilvering i.e.
148 * vdev_resilver_txg != 0 as when tested performance was slightly
149 * worse overall when resilvering with compared to without.
152 /* Standard load based on pending queue length. */
153 load = vdev_queue_length(vd);
154 lastoffset = vdev_queue_lastoffset(vd);
156 if (vd->vdev_rotation_rate == VDEV_RATE_NON_ROTATING) {
157 /* Non-rotating media. */
158 if (lastoffset == zio_offset)
159 return (load + non_rotating_inc);
162 * Apply a seek penalty even for non-rotating devices as
163 * sequential I/O'a can be aggregated into fewer operations
164 * on the device, thus avoiding unnecessary per-command
165 * overhead and boosting performance.
167 return (load + non_rotating_seek_inc);
170 /* Rotating media I/O's which directly follow the last I/O. */
171 if (lastoffset == zio_offset)
172 return (load + rotating_inc);
175 * Apply half the seek increment to I/O's within seek offset
176 * of the last I/O queued to this vdev as they should incure less
177 * of a seek increment.
179 if (ABS(lastoffset - zio_offset) < rotating_seek_offset)
180 return (load + (rotating_seek_inc / 2));
182 /* Apply the full seek increment to all other I/O's. */
183 return (load + rotating_seek_inc);
187 static mirror_map_t *
188 vdev_mirror_map_init(zio_t *zio)
190 mirror_map_t *mm = NULL;
192 vdev_t *vd = zio->io_vd;
196 dva_t *dva = zio->io_bp->blk_dva;
197 spa_t *spa = zio->io_spa;
199 mm = vdev_mirror_map_alloc(BP_GET_NDVAS(zio->io_bp), B_FALSE,
201 for (c = 0; c < mm->mm_children; c++) {
202 mc = &mm->mm_child[c];
203 mc->mc_vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[c]));
204 mc->mc_offset = DVA_GET_OFFSET(&dva[c]);
207 mm = vdev_mirror_map_alloc(vd->vdev_children,
208 (vd->vdev_ops == &vdev_replacing_ops ||
209 vd->vdev_ops == &vdev_spare_ops), B_FALSE);
210 for (c = 0; c < mm->mm_children; c++) {
211 mc = &mm->mm_child[c];
212 mc->mc_vd = vd->vdev_child[c];
213 mc->mc_offset = zio->io_offset;
218 zio->io_vsd_ops = &vdev_mirror_vsd_ops;
223 vdev_mirror_open(vdev_t *vd, uint64_t *asize, uint64_t *max_asize,
224 uint64_t *logical_ashift, uint64_t *physical_ashift)
229 if (vd->vdev_children == 0) {
230 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
231 return (SET_ERROR(EINVAL));
234 vdev_open_children(vd);
236 for (int c = 0; c < vd->vdev_children; c++) {
237 vdev_t *cvd = vd->vdev_child[c];
239 if (cvd->vdev_open_error) {
240 lasterror = cvd->vdev_open_error;
245 *asize = MIN(*asize - 1, cvd->vdev_asize - 1) + 1;
246 *max_asize = MIN(*max_asize - 1, cvd->vdev_max_asize - 1) + 1;
247 *logical_ashift = MAX(*logical_ashift, cvd->vdev_ashift);
248 *physical_ashift = MAX(*physical_ashift,
249 cvd->vdev_physical_ashift);
252 if (numerrors == vd->vdev_children) {
253 vd->vdev_stat.vs_aux = VDEV_AUX_NO_REPLICAS;
261 vdev_mirror_close(vdev_t *vd)
263 for (int c = 0; c < vd->vdev_children; c++)
264 vdev_close(vd->vdev_child[c]);
268 vdev_mirror_child_done(zio_t *zio)
270 mirror_child_t *mc = zio->io_private;
272 mc->mc_error = zio->io_error;
278 vdev_mirror_scrub_done(zio_t *zio)
280 mirror_child_t *mc = zio->io_private;
282 if (zio->io_error == 0) {
285 mutex_enter(&zio->io_lock);
286 while ((pio = zio_walk_parents(zio)) != NULL) {
287 mutex_enter(&pio->io_lock);
288 ASSERT3U(zio->io_size, >=, pio->io_size);
289 bcopy(zio->io_data, pio->io_data, pio->io_size);
290 mutex_exit(&pio->io_lock);
292 mutex_exit(&zio->io_lock);
295 zio_buf_free(zio->io_data, zio->io_size);
297 mc->mc_error = zio->io_error;
303 * Check the other, lower-index DVAs to see if they're on the same
304 * vdev as the child we picked. If they are, use them since they
305 * are likely to have been allocated from the primary metaslab in
306 * use at the time, and hence are more likely to have locality with
310 vdev_mirror_dva_select(zio_t *zio, int p)
312 dva_t *dva = zio->io_bp->blk_dva;
313 mirror_map_t *mm = zio->io_vsd;
317 preferred = mm->mm_preferred[p];
318 for (p-- ; p >= 0; p--) {
319 c = mm->mm_preferred[p];
320 if (DVA_GET_VDEV(&dva[c]) == DVA_GET_VDEV(&dva[preferred]))
327 vdev_mirror_preferred_child_randomize(zio_t *zio)
329 mirror_map_t *mm = zio->io_vsd;
333 p = spa_get_random(mm->mm_preferred_cnt);
334 return (vdev_mirror_dva_select(zio, p));
338 * To ensure we don't always favour the first matching vdev,
339 * which could lead to wear leveling issues on SSD's, we
340 * use the I/O offset as a pseudo random seed into the vdevs
341 * which have the lowest load.
343 p = (zio->io_offset >> vdev_mirror_shift) % mm->mm_preferred_cnt;
344 return (mm->mm_preferred[p]);
348 * Try to find a vdev whose DTL doesn't contain the block we want to read
349 * prefering vdevs based on determined load.
351 * If we can't, try the read on any vdev we haven't already tried.
354 vdev_mirror_child_select(zio_t *zio)
356 mirror_map_t *mm = zio->io_vsd;
357 uint64_t txg = zio->io_txg;
360 ASSERT(zio->io_bp == NULL || BP_PHYSICAL_BIRTH(zio->io_bp) == txg);
362 lowest_load = INT_MAX;
363 mm->mm_preferred_cnt = 0;
364 for (c = 0; c < mm->mm_children; c++) {
367 mc = &mm->mm_child[c];
368 if (mc->mc_tried || mc->mc_skipped)
371 if (!vdev_readable(mc->mc_vd)) {
372 mc->mc_error = SET_ERROR(ENXIO);
373 mc->mc_tried = 1; /* don't even try */
378 if (vdev_dtl_contains(mc->mc_vd, DTL_MISSING, txg, 1)) {
379 mc->mc_error = SET_ERROR(ESTALE);
381 mc->mc_speculative = 1;
385 mc->mc_load = vdev_mirror_load(mm, mc->mc_vd, mc->mc_offset);
386 if (mc->mc_load > lowest_load)
389 if (mc->mc_load < lowest_load) {
390 lowest_load = mc->mc_load;
391 mm->mm_preferred_cnt = 0;
393 mm->mm_preferred[mm->mm_preferred_cnt] = c;
394 mm->mm_preferred_cnt++;
397 if (mm->mm_preferred_cnt == 1) {
398 vdev_queue_register_lastoffset(
399 mm->mm_child[mm->mm_preferred[0]].mc_vd, zio);
400 return (mm->mm_preferred[0]);
403 if (mm->mm_preferred_cnt > 1) {
404 int c = vdev_mirror_preferred_child_randomize(zio);
406 vdev_queue_register_lastoffset(mm->mm_child[c].mc_vd, zio);
411 * Every device is either missing or has this txg in its DTL.
412 * Look for any child we haven't already tried before giving up.
414 for (c = 0; c < mm->mm_children; c++) {
415 if (!mm->mm_child[c].mc_tried) {
416 vdev_queue_register_lastoffset(mm->mm_child[c].mc_vd,
423 * Every child failed. There's no place left to look.
429 vdev_mirror_io_start(zio_t *zio)
435 mm = vdev_mirror_map_init(zio);
437 if (zio->io_type == ZIO_TYPE_READ) {
438 if ((zio->io_flags & ZIO_FLAG_SCRUB) && !mm->mm_replacing) {
440 * For scrubbing reads we need to allocate a read
441 * buffer for each child and issue reads to all
442 * children. If any child succeeds, it will copy its
443 * data into zio->io_data in vdev_mirror_scrub_done.
445 for (c = 0; c < mm->mm_children; c++) {
446 mc = &mm->mm_child[c];
447 zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
448 mc->mc_vd, mc->mc_offset,
449 zio_buf_alloc(zio->io_size), zio->io_size,
450 zio->io_type, zio->io_priority, 0,
451 vdev_mirror_scrub_done, mc));
454 return (ZIO_PIPELINE_STOP);
457 * For normal reads just pick one child.
459 c = vdev_mirror_child_select(zio);
462 ASSERT(zio->io_type == ZIO_TYPE_WRITE ||
463 zio->io_type == ZIO_TYPE_FREE);
466 * Writes and frees go to all children.
469 children = mm->mm_children;
473 mc = &mm->mm_child[c];
474 zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
475 mc->mc_vd, mc->mc_offset, zio->io_data, zio->io_size,
476 zio->io_type, zio->io_priority, 0,
477 vdev_mirror_child_done, mc));
482 return (ZIO_PIPELINE_STOP);
486 vdev_mirror_worst_error(mirror_map_t *mm)
488 int error[2] = { 0, 0 };
490 for (int c = 0; c < mm->mm_children; c++) {
491 mirror_child_t *mc = &mm->mm_child[c];
492 int s = mc->mc_speculative;
493 error[s] = zio_worst_error(error[s], mc->mc_error);
496 return (error[0] ? error[0] : error[1]);
500 vdev_mirror_io_done(zio_t *zio)
502 mirror_map_t *mm = zio->io_vsd;
506 int unexpected_errors = 0;
508 for (c = 0; c < mm->mm_children; c++) {
509 mc = &mm->mm_child[c];
514 } else if (mc->mc_tried) {
519 if (zio->io_type == ZIO_TYPE_WRITE) {
521 * XXX -- for now, treat partial writes as success.
523 * Now that we support write reallocation, it would be better
524 * to treat partial failure as real failure unless there are
525 * no non-degraded top-level vdevs left, and not update DTLs
526 * if we intend to reallocate.
529 if (good_copies != mm->mm_children) {
531 * Always require at least one good copy.
533 * For ditto blocks (io_vd == NULL), require
534 * all copies to be good.
536 * XXX -- for replacing vdevs, there's no great answer.
537 * If the old device is really dead, we may not even
538 * be able to access it -- so we only want to
539 * require good writes to the new device. But if
540 * the new device turns out to be flaky, we want
541 * to be able to detach it -- which requires all
542 * writes to the old device to have succeeded.
544 if (good_copies == 0 || zio->io_vd == NULL)
545 zio->io_error = vdev_mirror_worst_error(mm);
548 } else if (zio->io_type == ZIO_TYPE_FREE) {
552 ASSERT(zio->io_type == ZIO_TYPE_READ);
555 * If we don't have a good copy yet, keep trying other children.
558 if (good_copies == 0 && (c = vdev_mirror_child_select(zio)) != -1) {
559 ASSERT(c >= 0 && c < mm->mm_children);
560 mc = &mm->mm_child[c];
561 zio_vdev_io_redone(zio);
562 zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
563 mc->mc_vd, mc->mc_offset, zio->io_data, zio->io_size,
564 ZIO_TYPE_READ, zio->io_priority, 0,
565 vdev_mirror_child_done, mc));
570 if (good_copies == 0) {
571 zio->io_error = vdev_mirror_worst_error(mm);
572 ASSERT(zio->io_error != 0);
575 if (good_copies && spa_writeable(zio->io_spa) &&
576 (unexpected_errors ||
577 (zio->io_flags & ZIO_FLAG_RESILVER) ||
578 ((zio->io_flags & ZIO_FLAG_SCRUB) && mm->mm_replacing))) {
580 * Use the good data we have in hand to repair damaged children.
582 for (c = 0; c < mm->mm_children; c++) {
584 * Don't rewrite known good children.
585 * Not only is it unnecessary, it could
586 * actually be harmful: if the system lost
587 * power while rewriting the only good copy,
588 * there would be no good copies left!
590 mc = &mm->mm_child[c];
592 if (mc->mc_error == 0) {
595 if (!(zio->io_flags & ZIO_FLAG_SCRUB) &&
596 !vdev_dtl_contains(mc->mc_vd, DTL_PARTIAL,
599 mc->mc_error = SET_ERROR(ESTALE);
602 zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
603 mc->mc_vd, mc->mc_offset,
604 zio->io_data, zio->io_size,
605 ZIO_TYPE_WRITE, ZIO_PRIORITY_ASYNC_WRITE,
606 ZIO_FLAG_IO_REPAIR | (unexpected_errors ?
607 ZIO_FLAG_SELF_HEAL : 0), NULL, NULL));
613 vdev_mirror_state_change(vdev_t *vd, int faulted, int degraded)
615 if (faulted == vd->vdev_children)
616 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
617 VDEV_AUX_NO_REPLICAS);
618 else if (degraded + faulted != 0)
619 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, VDEV_AUX_NONE);
621 vdev_set_state(vd, B_FALSE, VDEV_STATE_HEALTHY, VDEV_AUX_NONE);
624 vdev_ops_t vdev_mirror_ops = {
628 vdev_mirror_io_start,
630 vdev_mirror_state_change,
633 VDEV_TYPE_MIRROR, /* name of this vdev type */
634 B_FALSE /* not a leaf vdev */
637 vdev_ops_t vdev_replacing_ops = {
641 vdev_mirror_io_start,
643 vdev_mirror_state_change,
646 VDEV_TYPE_REPLACING, /* name of this vdev type */
647 B_FALSE /* not a leaf vdev */
650 vdev_ops_t vdev_spare_ops = {
654 vdev_mirror_io_start,
656 vdev_mirror_state_change,
659 VDEV_TYPE_SPARE, /* name of this vdev type */
660 B_FALSE /* not a leaf vdev */