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>
35 #include <sys/fs/zfs.h>
38 * Virtual device vector for mirroring.
41 typedef struct mirror_child
{
48 uint8_t mc_speculative
;
51 typedef struct mirror_map
{
55 boolean_t mm_replacing
;
57 mirror_child_t mm_child
[];
60 static int vdev_mirror_shift
= 21;
63 * The load configuration settings below are tuned by default for
64 * the case where all devices are of the same rotational type.
66 * If there is a mixture of rotating and non-rotating media, setting
67 * zfs_vdev_mirror_non_rotating_seek_inc to 0 may well provide better results
68 * as it will direct more reads to the non-rotating vdevs which are more likely
69 * to have a higher performance.
72 /* Rotating media load calculation configuration. */
73 static int zfs_vdev_mirror_rotating_inc
= 0;
74 static int zfs_vdev_mirror_rotating_seek_inc
= 5;
75 static int zfs_vdev_mirror_rotating_seek_offset
= 1 * 1024 * 1024;
77 /* Non-rotating media load calculation configuration. */
78 static int zfs_vdev_mirror_non_rotating_inc
= 0;
79 static int zfs_vdev_mirror_non_rotating_seek_inc
= 1;
82 vdev_mirror_map_size(int children
)
84 return (offsetof(mirror_map_t
, mm_child
[children
]) +
85 sizeof (int) * children
);
88 static inline mirror_map_t
*
89 vdev_mirror_map_alloc(int children
, boolean_t replacing
, boolean_t root
)
93 mm
= kmem_zalloc(vdev_mirror_map_size(children
), KM_SLEEP
);
94 mm
->mm_children
= children
;
95 mm
->mm_replacing
= replacing
;
97 mm
->mm_preferred
= (int *)((uintptr_t)mm
+
98 offsetof(mirror_map_t
, mm_child
[children
]));
104 vdev_mirror_map_free(zio_t
*zio
)
106 mirror_map_t
*mm
= zio
->io_vsd
;
108 kmem_free(mm
, vdev_mirror_map_size(mm
->mm_children
));
111 static const zio_vsd_ops_t vdev_mirror_vsd_ops
= {
112 vdev_mirror_map_free
,
113 zio_vsd_default_cksum_report
117 vdev_mirror_load(mirror_map_t
*mm
, vdev_t
*vd
, uint64_t zio_offset
)
122 /* All DVAs have equal weight at the root. */
127 * We don't return INT_MAX if the device is resilvering i.e.
128 * vdev_resilver_txg != 0 as when tested performance was slightly
129 * worse overall when resilvering with compared to without.
132 /* Standard load based on pending queue length. */
133 load
= vdev_queue_length(vd
);
134 lastoffset
= vdev_queue_lastoffset(vd
);
136 if (vd
->vdev_nonrot
) {
137 /* Non-rotating media. */
138 if (lastoffset
== zio_offset
)
139 return (load
+ zfs_vdev_mirror_non_rotating_inc
);
142 * Apply a seek penalty even for non-rotating devices as
143 * sequential I/O's can be aggregated into fewer operations on
144 * the device, thus avoiding unnecessary per-command overhead
145 * and boosting performance.
147 return (load
+ zfs_vdev_mirror_non_rotating_seek_inc
);
150 /* Rotating media I/O's which directly follow the last I/O. */
151 if (lastoffset
== zio_offset
)
152 return (load
+ zfs_vdev_mirror_rotating_inc
);
155 * Apply half the seek increment to I/O's within seek offset
156 * of the last I/O queued to this vdev as they should incur less
157 * of a seek increment.
159 if (ABS(lastoffset
- zio_offset
) <
160 zfs_vdev_mirror_rotating_seek_offset
)
161 return (load
+ (zfs_vdev_mirror_rotating_seek_inc
/ 2));
163 /* Apply the full seek increment to all other I/O's. */
164 return (load
+ zfs_vdev_mirror_rotating_seek_inc
);
168 * Avoid inlining the function to keep vdev_mirror_io_start(), which
169 * is this functions only caller, as small as possible on the stack.
171 noinline
static mirror_map_t
*
172 vdev_mirror_map_init(zio_t
*zio
)
174 mirror_map_t
*mm
= NULL
;
176 vdev_t
*vd
= zio
->io_vd
;
180 dva_t
*dva
= zio
->io_bp
->blk_dva
;
181 spa_t
*spa
= zio
->io_spa
;
183 mm
= vdev_mirror_map_alloc(BP_GET_NDVAS(zio
->io_bp
), B_FALSE
,
185 for (c
= 0; c
< mm
->mm_children
; c
++) {
186 mc
= &mm
->mm_child
[c
];
188 mc
->mc_vd
= vdev_lookup_top(spa
, DVA_GET_VDEV(&dva
[c
]));
189 mc
->mc_offset
= DVA_GET_OFFSET(&dva
[c
]);
192 mm
= vdev_mirror_map_alloc(vd
->vdev_children
,
193 (vd
->vdev_ops
== &vdev_replacing_ops
||
194 vd
->vdev_ops
== &vdev_spare_ops
), B_FALSE
);
195 for (c
= 0; c
< mm
->mm_children
; c
++) {
196 mc
= &mm
->mm_child
[c
];
197 mc
->mc_vd
= vd
->vdev_child
[c
];
198 mc
->mc_offset
= zio
->io_offset
;
203 zio
->io_vsd_ops
= &vdev_mirror_vsd_ops
;
208 vdev_mirror_open(vdev_t
*vd
, uint64_t *asize
, uint64_t *max_asize
,
215 if (vd
->vdev_children
== 0) {
216 vd
->vdev_stat
.vs_aux
= VDEV_AUX_BAD_LABEL
;
217 return (SET_ERROR(EINVAL
));
220 vdev_open_children(vd
);
222 for (c
= 0; c
< vd
->vdev_children
; c
++) {
223 vdev_t
*cvd
= vd
->vdev_child
[c
];
225 if (cvd
->vdev_open_error
) {
226 lasterror
= cvd
->vdev_open_error
;
231 *asize
= MIN(*asize
- 1, cvd
->vdev_asize
- 1) + 1;
232 *max_asize
= MIN(*max_asize
- 1, cvd
->vdev_max_asize
- 1) + 1;
233 *ashift
= MAX(*ashift
, cvd
->vdev_ashift
);
236 if (numerrors
== vd
->vdev_children
) {
237 vd
->vdev_stat
.vs_aux
= VDEV_AUX_NO_REPLICAS
;
245 vdev_mirror_close(vdev_t
*vd
)
249 for (c
= 0; c
< vd
->vdev_children
; c
++)
250 vdev_close(vd
->vdev_child
[c
]);
254 vdev_mirror_child_done(zio_t
*zio
)
256 mirror_child_t
*mc
= zio
->io_private
;
258 mc
->mc_error
= zio
->io_error
;
264 vdev_mirror_scrub_done(zio_t
*zio
)
266 mirror_child_t
*mc
= zio
->io_private
;
268 if (zio
->io_error
== 0) {
270 zio_link_t
*zl
= NULL
;
272 mutex_enter(&zio
->io_lock
);
273 while ((pio
= zio_walk_parents(zio
, &zl
)) != NULL
) {
274 mutex_enter(&pio
->io_lock
);
275 ASSERT3U(zio
->io_size
, >=, pio
->io_size
);
276 abd_copy(pio
->io_abd
, zio
->io_abd
, pio
->io_size
);
277 mutex_exit(&pio
->io_lock
);
279 mutex_exit(&zio
->io_lock
);
282 abd_free(zio
->io_abd
);
284 mc
->mc_error
= zio
->io_error
;
290 * Check the other, lower-index DVAs to see if they're on the same
291 * vdev as the child we picked. If they are, use them since they
292 * are likely to have been allocated from the primary metaslab in
293 * use at the time, and hence are more likely to have locality with
297 vdev_mirror_dva_select(zio_t
*zio
, int p
)
299 dva_t
*dva
= zio
->io_bp
->blk_dva
;
300 mirror_map_t
*mm
= zio
->io_vsd
;
304 preferred
= mm
->mm_preferred
[p
];
305 for (p
--; p
>= 0; p
--) {
306 c
= mm
->mm_preferred
[p
];
307 if (DVA_GET_VDEV(&dva
[c
]) == DVA_GET_VDEV(&dva
[preferred
]))
314 vdev_mirror_preferred_child_randomize(zio_t
*zio
)
316 mirror_map_t
*mm
= zio
->io_vsd
;
320 p
= spa_get_random(mm
->mm_preferred_cnt
);
321 return (vdev_mirror_dva_select(zio
, p
));
325 * To ensure we don't always favour the first matching vdev,
326 * which could lead to wear leveling issues on SSD's, we
327 * use the I/O offset as a pseudo random seed into the vdevs
328 * which have the lowest load.
330 p
= (zio
->io_offset
>> vdev_mirror_shift
) % mm
->mm_preferred_cnt
;
331 return (mm
->mm_preferred
[p
]);
335 * Try to find a vdev whose DTL doesn't contain the block we want to read
336 * prefering vdevs based on determined load.
338 * Try to find a child whose DTL doesn't contain the block we want to read.
339 * If we can't, try the read on any vdev we haven't already tried.
342 vdev_mirror_child_select(zio_t
*zio
)
344 mirror_map_t
*mm
= zio
->io_vsd
;
345 uint64_t txg
= zio
->io_txg
;
348 ASSERT(zio
->io_bp
== NULL
|| BP_PHYSICAL_BIRTH(zio
->io_bp
) == txg
);
350 lowest_load
= INT_MAX
;
351 mm
->mm_preferred_cnt
= 0;
352 for (c
= 0; c
< mm
->mm_children
; c
++) {
355 mc
= &mm
->mm_child
[c
];
356 if (mc
->mc_tried
|| mc
->mc_skipped
)
359 if (mc
->mc_vd
== NULL
|| !vdev_readable(mc
->mc_vd
)) {
360 mc
->mc_error
= SET_ERROR(ENXIO
);
361 mc
->mc_tried
= 1; /* don't even try */
366 if (vdev_dtl_contains(mc
->mc_vd
, DTL_MISSING
, txg
, 1)) {
367 mc
->mc_error
= SET_ERROR(ESTALE
);
369 mc
->mc_speculative
= 1;
373 mc
->mc_load
= vdev_mirror_load(mm
, mc
->mc_vd
, mc
->mc_offset
);
374 if (mc
->mc_load
> lowest_load
)
377 if (mc
->mc_load
< lowest_load
) {
378 lowest_load
= mc
->mc_load
;
379 mm
->mm_preferred_cnt
= 0;
381 mm
->mm_preferred
[mm
->mm_preferred_cnt
] = c
;
382 mm
->mm_preferred_cnt
++;
385 if (mm
->mm_preferred_cnt
== 1) {
386 vdev_queue_register_lastoffset(
387 mm
->mm_child
[mm
->mm_preferred
[0]].mc_vd
, zio
);
388 return (mm
->mm_preferred
[0]);
391 if (mm
->mm_preferred_cnt
> 1) {
392 int c
= vdev_mirror_preferred_child_randomize(zio
);
394 vdev_queue_register_lastoffset(mm
->mm_child
[c
].mc_vd
, zio
);
399 * Every device is either missing or has this txg in its DTL.
400 * Look for any child we haven't already tried before giving up.
402 for (c
= 0; c
< mm
->mm_children
; c
++) {
403 if (!mm
->mm_child
[c
].mc_tried
) {
404 vdev_queue_register_lastoffset(mm
->mm_child
[c
].mc_vd
,
411 * Every child failed. There's no place left to look.
417 vdev_mirror_io_start(zio_t
*zio
)
423 mm
= vdev_mirror_map_init(zio
);
425 if (zio
->io_type
== ZIO_TYPE_READ
) {
426 if ((zio
->io_flags
& ZIO_FLAG_SCRUB
) && !mm
->mm_replacing
) {
428 * For scrubbing reads we need to allocate a read
429 * buffer for each child and issue reads to all
430 * children. If any child succeeds, it will copy its
431 * data into zio->io_data in vdev_mirror_scrub_done.
433 for (c
= 0; c
< mm
->mm_children
; c
++) {
434 mc
= &mm
->mm_child
[c
];
435 zio_nowait(zio_vdev_child_io(zio
, zio
->io_bp
,
436 mc
->mc_vd
, mc
->mc_offset
,
437 abd_alloc_sametype(zio
->io_abd
,
438 zio
->io_size
), zio
->io_size
,
439 zio
->io_type
, zio
->io_priority
, 0,
440 vdev_mirror_scrub_done
, mc
));
446 * For normal reads just pick one child.
448 c
= vdev_mirror_child_select(zio
);
451 ASSERT(zio
->io_type
== ZIO_TYPE_WRITE
);
454 * Writes go to all children.
457 children
= mm
->mm_children
;
461 mc
= &mm
->mm_child
[c
];
462 zio_nowait(zio_vdev_child_io(zio
, zio
->io_bp
,
463 mc
->mc_vd
, mc
->mc_offset
, zio
->io_abd
, zio
->io_size
,
464 zio
->io_type
, zio
->io_priority
, 0,
465 vdev_mirror_child_done
, mc
));
473 vdev_mirror_worst_error(mirror_map_t
*mm
)
475 int c
, error
[2] = { 0, 0 };
477 for (c
= 0; c
< mm
->mm_children
; c
++) {
478 mirror_child_t
*mc
= &mm
->mm_child
[c
];
479 int s
= mc
->mc_speculative
;
480 error
[s
] = zio_worst_error(error
[s
], mc
->mc_error
);
483 return (error
[0] ? error
[0] : error
[1]);
487 vdev_mirror_io_done(zio_t
*zio
)
489 mirror_map_t
*mm
= zio
->io_vsd
;
493 int unexpected_errors
= 0;
495 for (c
= 0; c
< mm
->mm_children
; c
++) {
496 mc
= &mm
->mm_child
[c
];
501 } else if (mc
->mc_tried
) {
506 if (zio
->io_type
== ZIO_TYPE_WRITE
) {
508 * XXX -- for now, treat partial writes as success.
510 * Now that we support write reallocation, it would be better
511 * to treat partial failure as real failure unless there are
512 * no non-degraded top-level vdevs left, and not update DTLs
513 * if we intend to reallocate.
516 if (good_copies
!= mm
->mm_children
) {
518 * Always require at least one good copy.
520 * For ditto blocks (io_vd == NULL), require
521 * all copies to be good.
523 * XXX -- for replacing vdevs, there's no great answer.
524 * If the old device is really dead, we may not even
525 * be able to access it -- so we only want to
526 * require good writes to the new device. But if
527 * the new device turns out to be flaky, we want
528 * to be able to detach it -- which requires all
529 * writes to the old device to have succeeded.
531 if (good_copies
== 0 || zio
->io_vd
== NULL
)
532 zio
->io_error
= vdev_mirror_worst_error(mm
);
537 ASSERT(zio
->io_type
== ZIO_TYPE_READ
);
540 * If we don't have a good copy yet, keep trying other children.
543 if (good_copies
== 0 && (c
= vdev_mirror_child_select(zio
)) != -1) {
544 ASSERT(c
>= 0 && c
< mm
->mm_children
);
545 mc
= &mm
->mm_child
[c
];
546 zio_vdev_io_redone(zio
);
547 zio_nowait(zio_vdev_child_io(zio
, zio
->io_bp
,
548 mc
->mc_vd
, mc
->mc_offset
, zio
->io_abd
, zio
->io_size
,
549 ZIO_TYPE_READ
, zio
->io_priority
, 0,
550 vdev_mirror_child_done
, mc
));
555 if (good_copies
== 0) {
556 zio
->io_error
= vdev_mirror_worst_error(mm
);
557 ASSERT(zio
->io_error
!= 0);
560 if (good_copies
&& spa_writeable(zio
->io_spa
) &&
561 (unexpected_errors
||
562 (zio
->io_flags
& ZIO_FLAG_RESILVER
) ||
563 ((zio
->io_flags
& ZIO_FLAG_SCRUB
) && mm
->mm_replacing
))) {
565 * Use the good data we have in hand to repair damaged children.
567 for (c
= 0; c
< mm
->mm_children
; c
++) {
569 * Don't rewrite known good children.
570 * Not only is it unnecessary, it could
571 * actually be harmful: if the system lost
572 * power while rewriting the only good copy,
573 * there would be no good copies left!
575 mc
= &mm
->mm_child
[c
];
577 if (mc
->mc_error
== 0) {
580 if (!(zio
->io_flags
& ZIO_FLAG_SCRUB
) &&
581 !vdev_dtl_contains(mc
->mc_vd
, DTL_PARTIAL
,
584 mc
->mc_error
= SET_ERROR(ESTALE
);
587 zio_nowait(zio_vdev_child_io(zio
, zio
->io_bp
,
588 mc
->mc_vd
, mc
->mc_offset
,
589 zio
->io_abd
, zio
->io_size
,
590 ZIO_TYPE_WRITE
, ZIO_PRIORITY_ASYNC_WRITE
,
591 ZIO_FLAG_IO_REPAIR
| (unexpected_errors
?
592 ZIO_FLAG_SELF_HEAL
: 0), NULL
, NULL
));
598 vdev_mirror_state_change(vdev_t
*vd
, int faulted
, int degraded
)
600 if (faulted
== vd
->vdev_children
)
601 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_CANT_OPEN
,
602 VDEV_AUX_NO_REPLICAS
);
603 else if (degraded
+ faulted
!= 0)
604 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_DEGRADED
, VDEV_AUX_NONE
);
606 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_HEALTHY
, VDEV_AUX_NONE
);
609 vdev_ops_t vdev_mirror_ops
= {
613 vdev_mirror_io_start
,
615 vdev_mirror_state_change
,
619 VDEV_TYPE_MIRROR
, /* name of this vdev type */
620 B_FALSE
/* not a leaf vdev */
623 vdev_ops_t vdev_replacing_ops
= {
627 vdev_mirror_io_start
,
629 vdev_mirror_state_change
,
633 VDEV_TYPE_REPLACING
, /* name of this vdev type */
634 B_FALSE
/* not a leaf vdev */
637 vdev_ops_t vdev_spare_ops
= {
641 vdev_mirror_io_start
,
643 vdev_mirror_state_change
,
647 VDEV_TYPE_SPARE
, /* name of this vdev type */
648 B_FALSE
/* not a leaf vdev */
651 #if defined(_KERNEL) && defined(HAVE_SPL)
653 module_param(zfs_vdev_mirror_rotating_inc
, int, 0644);
654 MODULE_PARM_DESC(zfs_vdev_mirror_rotating_inc
,
655 "Rotating media load increment for non-seeking I/O's");
657 module_param(zfs_vdev_mirror_rotating_seek_inc
, int, 0644);
658 MODULE_PARM_DESC(zfs_vdev_mirror_rotating_seek_inc
,
659 "Rotating media load increment for seeking I/O's");
661 module_param(zfs_vdev_mirror_rotating_seek_offset
, int, 0644);
663 MODULE_PARM_DESC(zfs_vdev_mirror_rotating_seek_offset
,
664 "Offset in bytes from the last I/O which "
665 "triggers a reduced rotating media seek increment");
667 module_param(zfs_vdev_mirror_non_rotating_inc
, int, 0644);
668 MODULE_PARM_DESC(zfs_vdev_mirror_non_rotating_inc
,
669 "Non-rotating media load increment for non-seeking I/O's");
671 module_param(zfs_vdev_mirror_non_rotating_seek_inc
, int, 0644);
672 MODULE_PARM_DESC(zfs_vdev_mirror_non_rotating_seek_inc
,
673 "Non-rotating media load increment for seeking I/O's");