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 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "@(#)vdev_mirror.c 1.9 07/11/27 SMI"
28 #include <sys/zfs_context.h>
30 #include <sys/vdev_impl.h>
32 #include <sys/fs/zfs.h>
35 * Virtual device vector for mirroring.
38 typedef struct mirror_child {
46 typedef struct mirror_map {
51 mirror_child_t mm_child[1];
54 int vdev_mirror_shift = 21;
57 vdev_mirror_map_alloc(zio_t *zio)
59 mirror_map_t *mm = NULL;
61 vdev_t *vd = zio->io_vd;
65 dva_t *dva = zio->io_bp->blk_dva;
66 spa_t *spa = zio->io_spa;
68 c = BP_GET_NDVAS(zio->io_bp);
70 mm = kmem_zalloc(offsetof(mirror_map_t, mm_child[c]), KM_SLEEP);
72 mm->mm_replacing = B_FALSE;
73 mm->mm_preferred = spa_get_random(c);
77 * Check the other, lower-index DVAs to see if they're on
78 * the same vdev as the child we picked. If they are, use
79 * them since they are likely to have been allocated from
80 * the primary metaslab in use at the time, and hence are
81 * more likely to have locality with single-copy data.
83 for (c = mm->mm_preferred, d = c - 1; d >= 0; d--) {
84 if (DVA_GET_VDEV(&dva[d]) == DVA_GET_VDEV(&dva[c]))
88 for (c = 0; c < mm->mm_children; c++) {
89 mc = &mm->mm_child[c];
91 mc->mc_vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[c]));
92 mc->mc_offset = DVA_GET_OFFSET(&dva[c]);
95 c = vd->vdev_children;
97 mm = kmem_zalloc(offsetof(mirror_map_t, mm_child[c]), KM_SLEEP);
99 mm->mm_replacing = (vd->vdev_ops == &vdev_replacing_ops ||
100 vd->vdev_ops == &vdev_spare_ops);
101 mm->mm_preferred = mm->mm_replacing ? 0 :
102 (zio->io_offset >> vdev_mirror_shift) % c;
103 mm->mm_root = B_FALSE;
105 for (c = 0; c < mm->mm_children; c++) {
106 mc = &mm->mm_child[c];
107 mc->mc_vd = vd->vdev_child[c];
108 mc->mc_offset = zio->io_offset;
117 vdev_mirror_map_free(zio_t *zio)
119 mirror_map_t *mm = zio->io_vsd;
121 kmem_free(mm, offsetof(mirror_map_t, mm_child[mm->mm_children]));
126 vdev_mirror_open(vdev_t *vd, uint64_t *asize, uint64_t *ashift)
131 int ret, lasterror = 0;
133 if (vd->vdev_children == 0) {
134 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
138 for (c = 0; c < vd->vdev_children; c++) {
139 cvd = vd->vdev_child[c];
141 if ((ret = vdev_open(cvd)) != 0) {
147 *asize = MIN(*asize - 1, cvd->vdev_asize - 1) + 1;
148 *ashift = MAX(*ashift, cvd->vdev_ashift);
151 if (numerrors == vd->vdev_children) {
152 vd->vdev_stat.vs_aux = VDEV_AUX_NO_REPLICAS;
160 vdev_mirror_close(vdev_t *vd)
164 for (c = 0; c < vd->vdev_children; c++)
165 vdev_close(vd->vdev_child[c]);
169 vdev_mirror_child_done(zio_t *zio)
171 mirror_child_t *mc = zio->io_private;
173 mc->mc_error = zio->io_error;
179 vdev_mirror_scrub_done(zio_t *zio)
181 mirror_child_t *mc = zio->io_private;
183 if (zio->io_error == 0) {
184 zio_t *pio = zio->io_parent;
185 mutex_enter(&pio->io_lock);
186 ASSERT3U(zio->io_size, >=, pio->io_size);
187 bcopy(zio->io_data, pio->io_data, pio->io_size);
188 mutex_exit(&pio->io_lock);
191 zio_buf_free(zio->io_data, zio->io_size);
193 mc->mc_error = zio->io_error;
199 vdev_mirror_repair_done(zio_t *zio)
201 ASSERT(zio->io_private == zio->io_parent);
202 vdev_mirror_map_free(zio->io_private);
206 * Try to find a child whose DTL doesn't contain the block we want to read.
207 * If we can't, try the read on any vdev we haven't already tried.
210 vdev_mirror_child_select(zio_t *zio)
212 mirror_map_t *mm = zio->io_vsd;
214 uint64_t txg = zio->io_txg;
217 ASSERT(zio->io_bp == NULL || zio->io_bp->blk_birth == txg);
220 * Try to find a child whose DTL doesn't contain the block to read.
221 * If a child is known to be completely inaccessible (indicated by
222 * vdev_readable() returning B_FALSE), don't even try.
224 for (i = 0, c = mm->mm_preferred; i < mm->mm_children; i++, c++) {
225 if (c >= mm->mm_children)
227 mc = &mm->mm_child[c];
228 if (mc->mc_tried || mc->mc_skipped)
230 if (vdev_is_dead(mc->mc_vd) && !vdev_readable(mc->mc_vd)) {
231 mc->mc_error = ENXIO;
232 mc->mc_tried = 1; /* don't even try */
236 if (!vdev_dtl_contains(&mc->mc_vd->vdev_dtl_map, txg, 1))
238 mc->mc_error = ESTALE;
243 * Every device is either missing or has this txg in its DTL.
244 * Look for any child we haven't already tried before giving up.
246 for (c = 0; c < mm->mm_children; c++)
247 if (!mm->mm_child[c].mc_tried)
251 * Every child failed. There's no place left to look.
257 vdev_mirror_io_start(zio_t *zio)
263 mm = vdev_mirror_map_alloc(zio);
265 if (zio->io_type == ZIO_TYPE_READ) {
266 if ((zio->io_flags & ZIO_FLAG_SCRUB) && !mm->mm_replacing) {
268 * For scrubbing reads we need to allocate a read
269 * buffer for each child and issue reads to all
270 * children. If any child succeeds, it will copy its
271 * data into zio->io_data in vdev_mirror_scrub_done.
273 for (c = 0; c < mm->mm_children; c++) {
274 mc = &mm->mm_child[c];
275 zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
276 mc->mc_vd, mc->mc_offset,
277 zio_buf_alloc(zio->io_size), zio->io_size,
278 zio->io_type, zio->io_priority,
280 vdev_mirror_scrub_done, mc));
282 return (zio_wait_for_children_done(zio));
285 * For normal reads just pick one child.
287 c = vdev_mirror_child_select(zio);
290 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
293 * If this is a resilvering I/O to a replacing vdev,
294 * only the last child should be written -- unless the
295 * first child happens to have a DTL entry here as well.
296 * All other writes go to all children.
298 if ((zio->io_flags & ZIO_FLAG_RESILVER) && mm->mm_replacing &&
299 !vdev_dtl_contains(&mm->mm_child[0].mc_vd->vdev_dtl_map,
301 c = mm->mm_children - 1;
305 children = mm->mm_children;
310 mc = &mm->mm_child[c];
311 zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
312 mc->mc_vd, mc->mc_offset,
313 zio->io_data, zio->io_size, zio->io_type, zio->io_priority,
314 ZIO_FLAG_CANFAIL, vdev_mirror_child_done, mc));
318 return (zio_wait_for_children_done(zio));
322 vdev_mirror_io_done(zio_t *zio)
324 mirror_map_t *mm = zio->io_vsd;
328 int unexpected_errors = 0;
331 zio->io_numerrors = 0;
333 for (c = 0; c < mm->mm_children; c++) {
334 mc = &mm->mm_child[c];
336 if (mc->mc_tried && mc->mc_error == 0) {
342 * We preserve any EIOs because those may be worth retrying;
343 * whereas ECKSUM and ENXIO are more likely to be persistent.
346 if (zio->io_error != EIO)
347 zio->io_error = mc->mc_error;
354 if (zio->io_type == ZIO_TYPE_WRITE) {
356 * XXX -- for now, treat partial writes as success.
357 * XXX -- For a replacing vdev, we need to make sure the
358 * new child succeeds.
361 if (good_copies != 0)
363 vdev_mirror_map_free(zio);
364 return (ZIO_PIPELINE_CONTINUE);
367 ASSERT(zio->io_type == ZIO_TYPE_READ);
370 * If we don't have a good copy yet, keep trying other children.
373 if (good_copies == 0 && (c = vdev_mirror_child_select(zio)) != -1) {
374 ASSERT(c >= 0 && c < mm->mm_children);
375 mc = &mm->mm_child[c];
376 dprintf("retrying i/o (err=%d) on child %s\n",
377 zio->io_error, vdev_description(mc->mc_vd));
379 zio_vdev_io_redone(zio);
380 zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
381 mc->mc_vd, mc->mc_offset, zio->io_data, zio->io_size,
382 ZIO_TYPE_READ, zio->io_priority, ZIO_FLAG_CANFAIL,
383 vdev_mirror_child_done, mc));
384 return (zio_wait_for_children_done(zio));
391 ASSERT(zio->io_error != 0);
393 if (good_copies && (spa_mode & FWRITE) &&
394 (unexpected_errors ||
395 (zio->io_flags & ZIO_FLAG_RESILVER) ||
396 ((zio->io_flags & ZIO_FLAG_SCRUB) && mm->mm_replacing))) {
400 * Use the good data we have in hand to repair damaged children.
402 * We issue all repair I/Os as children of 'rio' to arrange
403 * that vdev_mirror_map_free(zio) will be invoked after all
404 * repairs complete, but before we advance to the next stage.
406 rio = zio_null(zio, zio->io_spa,
407 vdev_mirror_repair_done, zio, ZIO_FLAG_CANFAIL);
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->vdev_dtl_map,
426 mc->mc_error = ESTALE;
429 dprintf("resilvered %s @ 0x%llx error %d\n",
430 vdev_description(mc->mc_vd), mc->mc_offset,
433 zio_nowait(zio_vdev_child_io(rio, zio->io_bp, mc->mc_vd,
434 mc->mc_offset, zio->io_data, zio->io_size,
435 ZIO_TYPE_WRITE, zio->io_priority,
436 ZIO_FLAG_IO_REPAIR | ZIO_FLAG_CANFAIL |
437 ZIO_FLAG_DONT_PROPAGATE, NULL, NULL));
442 return (zio_wait_for_children_done(zio));
445 vdev_mirror_map_free(zio);
447 return (ZIO_PIPELINE_CONTINUE);
451 vdev_mirror_state_change(vdev_t *vd, int faulted, int degraded)
453 if (faulted == vd->vdev_children)
454 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
455 VDEV_AUX_NO_REPLICAS);
456 else if (degraded + faulted != 0)
457 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, VDEV_AUX_NONE);
459 vdev_set_state(vd, B_FALSE, VDEV_STATE_HEALTHY, VDEV_AUX_NONE);
462 vdev_ops_t vdev_mirror_ops = {
467 vdev_mirror_io_start,
469 vdev_mirror_state_change,
470 VDEV_TYPE_MIRROR, /* name of this vdev type */
471 B_FALSE /* not a leaf vdev */
474 vdev_ops_t vdev_replacing_ops = {
479 vdev_mirror_io_start,
481 vdev_mirror_state_change,
482 VDEV_TYPE_REPLACING, /* name of this vdev type */
483 B_FALSE /* not a leaf vdev */
486 vdev_ops_t vdev_spare_ops = {
491 vdev_mirror_io_start,
493 vdev_mirror_state_change,
494 VDEV_TYPE_SPARE, /* name of this vdev type */
495 B_FALSE /* not a leaf vdev */