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.
26 #include <sys/zfs_context.h>
28 #include <sys/vdev_impl.h>
30 #include <sys/fs/zfs.h>
33 * Virtual device vector for mirroring.
36 typedef struct mirror_child {
42 uint8_t mc_speculative;
45 typedef struct mirror_map {
50 mirror_child_t mm_child[1];
53 int vdev_mirror_shift = 21;
56 vdev_mirror_map_free(zio_t *zio)
58 mirror_map_t *mm = zio->io_vsd;
60 kmem_free(mm, offsetof(mirror_map_t, mm_child[mm->mm_children]));
63 static const zio_vsd_ops_t vdev_mirror_vsd_ops = {
65 zio_vsd_default_cksum_report
69 vdev_mirror_map_alloc(zio_t *zio)
71 mirror_map_t *mm = NULL;
73 vdev_t *vd = zio->io_vd;
77 dva_t *dva = zio->io_bp->blk_dva;
78 spa_t *spa = zio->io_spa;
80 c = BP_GET_NDVAS(zio->io_bp);
82 mm = kmem_zalloc(offsetof(mirror_map_t, mm_child[c]), KM_SLEEP);
84 mm->mm_replacing = B_FALSE;
85 mm->mm_preferred = spa_get_random(c);
89 * Check the other, lower-index DVAs to see if they're on
90 * the same vdev as the child we picked. If they are, use
91 * them since they are likely to have been allocated from
92 * the primary metaslab in use at the time, and hence are
93 * more likely to have locality with single-copy data.
95 for (c = mm->mm_preferred, d = c - 1; d >= 0; d--) {
96 if (DVA_GET_VDEV(&dva[d]) == DVA_GET_VDEV(&dva[c]))
100 for (c = 0; c < mm->mm_children; c++) {
101 mc = &mm->mm_child[c];
103 mc->mc_vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[c]));
104 mc->mc_offset = DVA_GET_OFFSET(&dva[c]);
107 c = vd->vdev_children;
109 mm = kmem_zalloc(offsetof(mirror_map_t, mm_child[c]), KM_SLEEP);
111 mm->mm_replacing = (vd->vdev_ops == &vdev_replacing_ops ||
112 vd->vdev_ops == &vdev_spare_ops);
113 mm->mm_preferred = mm->mm_replacing ? 0 :
114 (zio->io_offset >> vdev_mirror_shift) % c;
115 mm->mm_root = B_FALSE;
117 for (c = 0; c < mm->mm_children; c++) {
118 mc = &mm->mm_child[c];
119 mc->mc_vd = vd->vdev_child[c];
120 mc->mc_offset = zio->io_offset;
125 zio->io_vsd_ops = &vdev_mirror_vsd_ops;
130 vdev_mirror_open(vdev_t *vd, uint64_t *asize, uint64_t *ashift)
136 if (vd->vdev_children == 0) {
137 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
141 vdev_open_children(vd);
143 for (c = 0; c < vd->vdev_children; c++) {
144 vdev_t *cvd = vd->vdev_child[c];
146 if (cvd->vdev_open_error) {
147 lasterror = cvd->vdev_open_error;
152 *asize = MIN(*asize - 1, cvd->vdev_asize - 1) + 1;
153 *ashift = MAX(*ashift, cvd->vdev_ashift);
156 if (numerrors == vd->vdev_children) {
157 vd->vdev_stat.vs_aux = VDEV_AUX_NO_REPLICAS;
165 vdev_mirror_close(vdev_t *vd)
169 for (c = 0; c < vd->vdev_children; c++)
170 vdev_close(vd->vdev_child[c]);
174 vdev_mirror_child_done(zio_t *zio)
176 mirror_child_t *mc = zio->io_private;
178 mc->mc_error = zio->io_error;
184 vdev_mirror_scrub_done(zio_t *zio)
186 mirror_child_t *mc = zio->io_private;
188 if (zio->io_error == 0) {
191 mutex_enter(&zio->io_lock);
192 while ((pio = zio_walk_parents(zio)) != NULL) {
193 mutex_enter(&pio->io_lock);
194 ASSERT3U(zio->io_size, >=, pio->io_size);
195 bcopy(zio->io_data, pio->io_data, pio->io_size);
196 mutex_exit(&pio->io_lock);
198 mutex_exit(&zio->io_lock);
201 zio_buf_free(zio->io_data, zio->io_size);
203 mc->mc_error = zio->io_error;
209 * Try to find a child whose DTL doesn't contain the block we want to read.
210 * If we can't, try the read on any vdev we haven't already tried.
213 vdev_mirror_child_select(zio_t *zio)
215 mirror_map_t *mm = zio->io_vsd;
217 uint64_t txg = zio->io_txg;
220 ASSERT(zio->io_bp == NULL || BP_PHYSICAL_BIRTH(zio->io_bp) == txg);
223 * Try to find a child whose DTL doesn't contain the block to read.
224 * If a child is known to be completely inaccessible (indicated by
225 * vdev_readable() returning B_FALSE), don't even try.
227 for (i = 0, c = mm->mm_preferred; i < mm->mm_children; i++, c++) {
228 if (c >= mm->mm_children)
230 mc = &mm->mm_child[c];
231 if (mc->mc_tried || mc->mc_skipped)
233 if (!vdev_readable(mc->mc_vd)) {
234 mc->mc_error = ENXIO;
235 mc->mc_tried = 1; /* don't even try */
239 if (!vdev_dtl_contains(mc->mc_vd, DTL_MISSING, txg, 1))
241 mc->mc_error = ESTALE;
243 mc->mc_speculative = 1;
247 * Every device is either missing or has this txg in its DTL.
248 * Look for any child we haven't already tried before giving up.
250 for (c = 0; c < mm->mm_children; c++)
251 if (!mm->mm_child[c].mc_tried)
255 * Every child failed. There's no place left to look.
261 vdev_mirror_io_start(zio_t *zio)
267 mm = vdev_mirror_map_alloc(zio);
269 if (zio->io_type == ZIO_TYPE_READ) {
270 if ((zio->io_flags & ZIO_FLAG_SCRUB) && !mm->mm_replacing) {
272 * For scrubbing reads we need to allocate a read
273 * buffer for each child and issue reads to all
274 * children. If any child succeeds, it will copy its
275 * data into zio->io_data in vdev_mirror_scrub_done.
277 for (c = 0; c < mm->mm_children; c++) {
278 mc = &mm->mm_child[c];
279 zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
280 mc->mc_vd, mc->mc_offset,
281 zio_buf_alloc(zio->io_size), zio->io_size,
282 zio->io_type, zio->io_priority, 0,
283 vdev_mirror_scrub_done, mc));
285 return (ZIO_PIPELINE_CONTINUE);
288 * For normal reads just pick one child.
290 c = vdev_mirror_child_select(zio);
293 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
296 * Writes go to all children.
299 children = mm->mm_children;
303 mc = &mm->mm_child[c];
304 zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
305 mc->mc_vd, mc->mc_offset, zio->io_data, zio->io_size,
306 zio->io_type, zio->io_priority, 0,
307 vdev_mirror_child_done, mc));
311 return (ZIO_PIPELINE_CONTINUE);
315 vdev_mirror_worst_error(mirror_map_t *mm)
317 int c, error[2] = { 0, 0 };
319 for (c = 0; c < mm->mm_children; c++) {
320 mirror_child_t *mc = &mm->mm_child[c];
321 int s = mc->mc_speculative;
322 error[s] = zio_worst_error(error[s], mc->mc_error);
325 return (error[0] ? error[0] : error[1]);
329 vdev_mirror_io_done(zio_t *zio)
331 mirror_map_t *mm = zio->io_vsd;
335 int unexpected_errors = 0;
337 for (c = 0; c < mm->mm_children; c++) {
338 mc = &mm->mm_child[c];
343 } else if (mc->mc_tried) {
348 if (zio->io_type == ZIO_TYPE_WRITE) {
350 * XXX -- for now, treat partial writes as success.
352 * Now that we support write reallocation, it would be better
353 * to treat partial failure as real failure unless there are
354 * no non-degraded top-level vdevs left, and not update DTLs
355 * if we intend to reallocate.
358 if (good_copies != mm->mm_children) {
360 * Always require at least one good copy.
362 * For ditto blocks (io_vd == NULL), require
363 * all copies to be good.
365 * XXX -- for replacing vdevs, there's no great answer.
366 * If the old device is really dead, we may not even
367 * be able to access it -- so we only want to
368 * require good writes to the new device. But if
369 * the new device turns out to be flaky, we want
370 * to be able to detach it -- which requires all
371 * writes to the old device to have succeeded.
373 if (good_copies == 0 || zio->io_vd == NULL)
374 zio->io_error = vdev_mirror_worst_error(mm);
379 ASSERT(zio->io_type == ZIO_TYPE_READ);
382 * If we don't have a good copy yet, keep trying other children.
385 if (good_copies == 0 && (c = vdev_mirror_child_select(zio)) != -1) {
386 ASSERT(c >= 0 && c < mm->mm_children);
387 mc = &mm->mm_child[c];
388 zio_vdev_io_redone(zio);
389 zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
390 mc->mc_vd, mc->mc_offset, zio->io_data, zio->io_size,
391 ZIO_TYPE_READ, zio->io_priority, 0,
392 vdev_mirror_child_done, mc));
397 if (good_copies == 0) {
398 zio->io_error = vdev_mirror_worst_error(mm);
399 ASSERT(zio->io_error != 0);
402 if (good_copies && spa_writeable(zio->io_spa) &&
403 (unexpected_errors ||
404 (zio->io_flags & ZIO_FLAG_RESILVER) ||
405 ((zio->io_flags & ZIO_FLAG_SCRUB) && mm->mm_replacing))) {
407 * Use the good data we have in hand to repair damaged children.
409 for (c = 0; c < mm->mm_children; c++) {
411 * Don't rewrite known good children.
412 * Not only is it unnecessary, it could
413 * actually be harmful: if the system lost
414 * power while rewriting the only good copy,
415 * there would be no good copies left!
417 mc = &mm->mm_child[c];
419 if (mc->mc_error == 0) {
422 if (!(zio->io_flags & ZIO_FLAG_SCRUB) &&
423 !vdev_dtl_contains(mc->mc_vd, DTL_PARTIAL,
426 mc->mc_error = ESTALE;
429 zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
430 mc->mc_vd, mc->mc_offset,
431 zio->io_data, zio->io_size,
432 ZIO_TYPE_WRITE, zio->io_priority,
433 ZIO_FLAG_IO_REPAIR | (unexpected_errors ?
434 ZIO_FLAG_SELF_HEAL : 0), NULL, NULL));
440 vdev_mirror_state_change(vdev_t *vd, int faulted, int degraded)
442 if (faulted == vd->vdev_children)
443 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
444 VDEV_AUX_NO_REPLICAS);
445 else if (degraded + faulted != 0)
446 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, VDEV_AUX_NONE);
448 vdev_set_state(vd, B_FALSE, VDEV_STATE_HEALTHY, VDEV_AUX_NONE);
451 vdev_ops_t vdev_mirror_ops = {
455 vdev_mirror_io_start,
457 vdev_mirror_state_change,
460 VDEV_TYPE_MIRROR, /* name of this vdev type */
461 B_FALSE /* not a leaf vdev */
464 vdev_ops_t vdev_replacing_ops = {
468 vdev_mirror_io_start,
470 vdev_mirror_state_change,
473 VDEV_TYPE_REPLACING, /* name of this vdev type */
474 B_FALSE /* not a leaf vdev */
477 vdev_ops_t vdev_spare_ops = {
481 vdev_mirror_io_start,
483 vdev_mirror_state_change,
486 VDEV_TYPE_SPARE, /* name of this vdev type */
487 B_FALSE /* not a leaf vdev */