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 2009 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]));
64 vdev_mirror_map_alloc(zio_t *zio)
66 mirror_map_t *mm = NULL;
68 vdev_t *vd = zio->io_vd;
72 dva_t *dva = zio->io_bp->blk_dva;
73 spa_t *spa = zio->io_spa;
75 c = BP_GET_NDVAS(zio->io_bp);
77 mm = kmem_zalloc(offsetof(mirror_map_t, mm_child[c]), KM_SLEEP);
79 mm->mm_replacing = B_FALSE;
80 mm->mm_preferred = spa_get_random(c);
84 * Check the other, lower-index DVAs to see if they're on
85 * the same vdev as the child we picked. If they are, use
86 * them since they are likely to have been allocated from
87 * the primary metaslab in use at the time, and hence are
88 * more likely to have locality with single-copy data.
90 for (c = mm->mm_preferred, d = c - 1; d >= 0; d--) {
91 if (DVA_GET_VDEV(&dva[d]) == DVA_GET_VDEV(&dva[c]))
95 for (c = 0; c < mm->mm_children; c++) {
96 mc = &mm->mm_child[c];
98 mc->mc_vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[c]));
99 mc->mc_offset = DVA_GET_OFFSET(&dva[c]);
102 c = vd->vdev_children;
104 mm = kmem_zalloc(offsetof(mirror_map_t, mm_child[c]), KM_SLEEP);
106 mm->mm_replacing = (vd->vdev_ops == &vdev_replacing_ops ||
107 vd->vdev_ops == &vdev_spare_ops);
108 mm->mm_preferred = mm->mm_replacing ? 0 :
109 (zio->io_offset >> vdev_mirror_shift) % c;
110 mm->mm_root = B_FALSE;
112 for (c = 0; c < mm->mm_children; c++) {
113 mc = &mm->mm_child[c];
114 mc->mc_vd = vd->vdev_child[c];
115 mc->mc_offset = zio->io_offset;
120 zio->io_vsd_free = vdev_mirror_map_free;
125 vdev_mirror_open(vdev_t *vd, uint64_t *asize, uint64_t *ashift)
130 int ret, lasterror = 0;
132 if (vd->vdev_children == 0) {
133 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
137 for (c = 0; c < vd->vdev_children; c++) {
138 cvd = vd->vdev_child[c];
140 if ((ret = vdev_open(cvd)) != 0) {
146 *asize = MIN(*asize - 1, cvd->vdev_asize - 1) + 1;
147 *ashift = MAX(*ashift, cvd->vdev_ashift);
150 if (numerrors == vd->vdev_children) {
151 vd->vdev_stat.vs_aux = VDEV_AUX_NO_REPLICAS;
159 vdev_mirror_close(vdev_t *vd)
163 for (c = 0; c < vd->vdev_children; c++)
164 vdev_close(vd->vdev_child[c]);
168 vdev_mirror_child_done(zio_t *zio)
170 mirror_child_t *mc = zio->io_private;
172 mc->mc_error = zio->io_error;
178 vdev_mirror_scrub_done(zio_t *zio)
180 mirror_child_t *mc = zio->io_private;
182 if (zio->io_error == 0) {
185 mutex_enter(&zio->io_lock);
186 while ((pio = zio_walk_parents(zio)) != NULL) {
187 mutex_enter(&pio->io_lock);
188 ASSERT3U(zio->io_size, >=, pio->io_size);
189 bcopy(zio->io_data, pio->io_data, pio->io_size);
190 mutex_exit(&pio->io_lock);
192 mutex_exit(&zio->io_lock);
195 zio_buf_free(zio->io_data, zio->io_size);
197 mc->mc_error = zio->io_error;
203 * Try to find a child whose DTL doesn't contain the block we want to read.
204 * If we can't, try the read on any vdev we haven't already tried.
207 vdev_mirror_child_select(zio_t *zio)
209 mirror_map_t *mm = zio->io_vsd;
211 uint64_t txg = zio->io_txg;
214 ASSERT(zio->io_bp == NULL || zio->io_bp->blk_birth == txg);
217 * Try to find a child whose DTL doesn't contain the block to read.
218 * If a child is known to be completely inaccessible (indicated by
219 * vdev_readable() returning B_FALSE), don't even try.
221 for (i = 0, c = mm->mm_preferred; i < mm->mm_children; i++, c++) {
222 if (c >= mm->mm_children)
224 mc = &mm->mm_child[c];
225 if (mc->mc_tried || mc->mc_skipped)
227 if (!vdev_readable(mc->mc_vd)) {
228 mc->mc_error = ENXIO;
229 mc->mc_tried = 1; /* don't even try */
233 if (!vdev_dtl_contains(mc->mc_vd, DTL_MISSING, txg, 1))
235 mc->mc_error = ESTALE;
237 mc->mc_speculative = 1;
241 * Every device is either missing or has this txg in its DTL.
242 * Look for any child we haven't already tried before giving up.
244 for (c = 0; c < mm->mm_children; c++)
245 if (!mm->mm_child[c].mc_tried)
249 * Every child failed. There's no place left to look.
255 vdev_mirror_io_start(zio_t *zio)
261 mm = vdev_mirror_map_alloc(zio);
263 if (zio->io_type == ZIO_TYPE_READ) {
264 if ((zio->io_flags & ZIO_FLAG_SCRUB) && !mm->mm_replacing) {
266 * For scrubbing reads we need to allocate a read
267 * buffer for each child and issue reads to all
268 * children. If any child succeeds, it will copy its
269 * data into zio->io_data in vdev_mirror_scrub_done.
271 for (c = 0; c < mm->mm_children; c++) {
272 mc = &mm->mm_child[c];
273 zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
274 mc->mc_vd, mc->mc_offset,
275 zio_buf_alloc(zio->io_size), zio->io_size,
276 zio->io_type, zio->io_priority, 0,
277 vdev_mirror_scrub_done, mc));
279 return (ZIO_PIPELINE_CONTINUE);
282 * For normal reads just pick one child.
284 c = vdev_mirror_child_select(zio);
287 ASSERT(zio->io_type == ZIO_TYPE_WRITE);
290 * Writes go to all children.
293 children = mm->mm_children;
297 mc = &mm->mm_child[c];
298 zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
299 mc->mc_vd, mc->mc_offset, zio->io_data, zio->io_size,
300 zio->io_type, zio->io_priority, 0,
301 vdev_mirror_child_done, mc));
305 return (ZIO_PIPELINE_CONTINUE);
309 vdev_mirror_worst_error(mirror_map_t *mm)
311 int error[2] = { 0, 0 };
313 for (int c = 0; c < mm->mm_children; c++) {
314 mirror_child_t *mc = &mm->mm_child[c];
315 int s = mc->mc_speculative;
316 error[s] = zio_worst_error(error[s], mc->mc_error);
319 return (error[0] ? error[0] : error[1]);
323 vdev_mirror_io_done(zio_t *zio)
325 mirror_map_t *mm = zio->io_vsd;
329 int unexpected_errors = 0;
331 for (c = 0; c < mm->mm_children; c++) {
332 mc = &mm->mm_child[c];
337 } else if (mc->mc_tried) {
342 if (zio->io_type == ZIO_TYPE_WRITE) {
344 * XXX -- for now, treat partial writes as success.
346 * Now that we support write reallocation, it would be better
347 * to treat partial failure as real failure unless there are
348 * no non-degraded top-level vdevs left, and not update DTLs
349 * if we intend to reallocate.
352 if (good_copies != mm->mm_children) {
354 * Always require at least one good copy.
356 * For ditto blocks (io_vd == NULL), require
357 * all copies to be good.
359 * XXX -- for replacing vdevs, there's no great answer.
360 * If the old device is really dead, we may not even
361 * be able to access it -- so we only want to
362 * require good writes to the new device. But if
363 * the new device turns out to be flaky, we want
364 * to be able to detach it -- which requires all
365 * writes to the old device to have succeeded.
367 if (good_copies == 0 || zio->io_vd == NULL)
368 zio->io_error = vdev_mirror_worst_error(mm);
373 ASSERT(zio->io_type == ZIO_TYPE_READ);
376 * If we don't have a good copy yet, keep trying other children.
379 if (good_copies == 0 && (c = vdev_mirror_child_select(zio)) != -1) {
380 ASSERT(c >= 0 && c < mm->mm_children);
381 mc = &mm->mm_child[c];
382 zio_vdev_io_redone(zio);
383 zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
384 mc->mc_vd, mc->mc_offset, zio->io_data, zio->io_size,
385 ZIO_TYPE_READ, zio->io_priority, 0,
386 vdev_mirror_child_done, mc));
391 if (good_copies == 0) {
392 zio->io_error = vdev_mirror_worst_error(mm);
393 ASSERT(zio->io_error != 0);
396 if (good_copies && spa_writeable(zio->io_spa) &&
397 (unexpected_errors ||
398 (zio->io_flags & ZIO_FLAG_RESILVER) ||
399 ((zio->io_flags & ZIO_FLAG_SCRUB) && mm->mm_replacing))) {
401 * Use the good data we have in hand to repair damaged children.
403 for (c = 0; c < mm->mm_children; c++) {
405 * Don't rewrite known good children.
406 * Not only is it unnecessary, it could
407 * actually be harmful: if the system lost
408 * power while rewriting the only good copy,
409 * there would be no good copies left!
411 mc = &mm->mm_child[c];
413 if (mc->mc_error == 0) {
416 if (!(zio->io_flags & ZIO_FLAG_SCRUB) &&
417 !vdev_dtl_contains(mc->mc_vd, DTL_PARTIAL,
420 mc->mc_error = ESTALE;
423 zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
424 mc->mc_vd, mc->mc_offset,
425 zio->io_data, zio->io_size,
426 ZIO_TYPE_WRITE, zio->io_priority,
427 ZIO_FLAG_IO_REPAIR | (unexpected_errors ?
428 ZIO_FLAG_SELF_HEAL : 0), NULL, NULL));
434 vdev_mirror_state_change(vdev_t *vd, int faulted, int degraded)
436 if (faulted == vd->vdev_children)
437 vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
438 VDEV_AUX_NO_REPLICAS);
439 else if (degraded + faulted != 0)
440 vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, VDEV_AUX_NONE);
442 vdev_set_state(vd, B_FALSE, VDEV_STATE_HEALTHY, VDEV_AUX_NONE);
445 vdev_ops_t vdev_mirror_ops = {
449 vdev_mirror_io_start,
451 vdev_mirror_state_change,
452 VDEV_TYPE_MIRROR, /* name of this vdev type */
453 B_FALSE /* not a leaf vdev */
456 vdev_ops_t vdev_replacing_ops = {
460 vdev_mirror_io_start,
462 vdev_mirror_state_change,
463 VDEV_TYPE_REPLACING, /* name of this vdev type */
464 B_FALSE /* not a leaf vdev */
467 vdev_ops_t vdev_spare_ops = {
471 vdev_mirror_io_start,
473 vdev_mirror_state_change,
474 VDEV_TYPE_SPARE, /* name of this vdev type */
475 B_FALSE /* not a leaf vdev */