xref: /onnv-gate/usr/src/uts/common/fs/zfs/vdev_mirror.c (revision 11958:575ffe1e978d)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51544Seschrock  * Common Development and Distribution License (the "License").
61544Seschrock  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
21789Sahrens /*
22*11958SGeorge.Wilson@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
26789Sahrens #include <sys/zfs_context.h>
27789Sahrens #include <sys/spa.h>
28789Sahrens #include <sys/vdev_impl.h>
29789Sahrens #include <sys/zio.h>
30789Sahrens #include <sys/fs/zfs.h>
31789Sahrens 
32789Sahrens /*
33789Sahrens  * Virtual device vector for mirroring.
34789Sahrens  */
35789Sahrens 
361775Sbillm typedef struct mirror_child {
371775Sbillm 	vdev_t		*mc_vd;
381775Sbillm 	uint64_t	mc_offset;
391775Sbillm 	int		mc_error;
407754SJeff.Bonwick@Sun.COM 	uint8_t		mc_tried;
417754SJeff.Bonwick@Sun.COM 	uint8_t		mc_skipped;
427754SJeff.Bonwick@Sun.COM 	uint8_t		mc_speculative;
431775Sbillm } mirror_child_t;
441775Sbillm 
45789Sahrens typedef struct mirror_map {
461775Sbillm 	int		mm_children;
471775Sbillm 	int		mm_replacing;
481775Sbillm 	int		mm_preferred;
491775Sbillm 	int		mm_root;
501775Sbillm 	mirror_child_t	mm_child[1];
51789Sahrens } mirror_map_t;
52789Sahrens 
532391Smaybee int vdev_mirror_shift = 21;
542391Smaybee 
557754SJeff.Bonwick@Sun.COM static void
vdev_mirror_map_free(zio_t * zio)567754SJeff.Bonwick@Sun.COM vdev_mirror_map_free(zio_t *zio)
577754SJeff.Bonwick@Sun.COM {
587754SJeff.Bonwick@Sun.COM 	mirror_map_t *mm = zio->io_vsd;
597754SJeff.Bonwick@Sun.COM 
607754SJeff.Bonwick@Sun.COM 	kmem_free(mm, offsetof(mirror_map_t, mm_child[mm->mm_children]));
617754SJeff.Bonwick@Sun.COM }
627754SJeff.Bonwick@Sun.COM 
6310614SJonathan.Adams@Sun.COM static const zio_vsd_ops_t vdev_mirror_vsd_ops = {
6410614SJonathan.Adams@Sun.COM 	vdev_mirror_map_free,
6510614SJonathan.Adams@Sun.COM 	zio_vsd_default_cksum_report
6610614SJonathan.Adams@Sun.COM };
6710614SJonathan.Adams@Sun.COM 
68789Sahrens static mirror_map_t *
vdev_mirror_map_alloc(zio_t * zio)69789Sahrens vdev_mirror_map_alloc(zio_t *zio)
70789Sahrens {
711775Sbillm 	mirror_map_t *mm = NULL;
721775Sbillm 	mirror_child_t *mc;
731775Sbillm 	vdev_t *vd = zio->io_vd;
741775Sbillm 	int c, d;
751775Sbillm 
761775Sbillm 	if (vd == NULL) {
771775Sbillm 		dva_t *dva = zio->io_bp->blk_dva;
781775Sbillm 		spa_t *spa = zio->io_spa;
791775Sbillm 
801775Sbillm 		c = BP_GET_NDVAS(zio->io_bp);
811775Sbillm 
821775Sbillm 		mm = kmem_zalloc(offsetof(mirror_map_t, mm_child[c]), KM_SLEEP);
831775Sbillm 		mm->mm_children = c;
841775Sbillm 		mm->mm_replacing = B_FALSE;
851775Sbillm 		mm->mm_preferred = spa_get_random(c);
861775Sbillm 		mm->mm_root = B_TRUE;
871775Sbillm 
881775Sbillm 		/*
891775Sbillm 		 * Check the other, lower-index DVAs to see if they're on
901775Sbillm 		 * the same vdev as the child we picked.  If they are, use
911775Sbillm 		 * them since they are likely to have been allocated from
921775Sbillm 		 * the primary metaslab in use at the time, and hence are
931775Sbillm 		 * more likely to have locality with single-copy data.
941775Sbillm 		 */
951775Sbillm 		for (c = mm->mm_preferred, d = c - 1; d >= 0; d--) {
961775Sbillm 			if (DVA_GET_VDEV(&dva[d]) == DVA_GET_VDEV(&dva[c]))
971775Sbillm 				mm->mm_preferred = d;
981775Sbillm 		}
991775Sbillm 
1001775Sbillm 		for (c = 0; c < mm->mm_children; c++) {
1011775Sbillm 			mc = &mm->mm_child[c];
1022082Seschrock 
1031775Sbillm 			mc->mc_vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[c]));
1041775Sbillm 			mc->mc_offset = DVA_GET_OFFSET(&dva[c]);
1051775Sbillm 		}
1061775Sbillm 	} else {
1071775Sbillm 		c = vd->vdev_children;
1081775Sbillm 
1091775Sbillm 		mm = kmem_zalloc(offsetof(mirror_map_t, mm_child[c]), KM_SLEEP);
1101775Sbillm 		mm->mm_children = c;
1112082Seschrock 		mm->mm_replacing = (vd->vdev_ops == &vdev_replacing_ops ||
1122082Seschrock 		    vd->vdev_ops == &vdev_spare_ops);
1132391Smaybee 		mm->mm_preferred = mm->mm_replacing ? 0 :
1142391Smaybee 		    (zio->io_offset >> vdev_mirror_shift) % c;
1151775Sbillm 		mm->mm_root = B_FALSE;
1161775Sbillm 
1171775Sbillm 		for (c = 0; c < mm->mm_children; c++) {
1181775Sbillm 			mc = &mm->mm_child[c];
1191775Sbillm 			mc->mc_vd = vd->vdev_child[c];
1201775Sbillm 			mc->mc_offset = zio->io_offset;
1211775Sbillm 		}
1221775Sbillm 	}
1231775Sbillm 
1241775Sbillm 	zio->io_vsd = mm;
12510614SJonathan.Adams@Sun.COM 	zio->io_vsd_ops = &vdev_mirror_vsd_ops;
1261775Sbillm 	return (mm);
127789Sahrens }
128789Sahrens 
129789Sahrens static int
vdev_mirror_open(vdev_t * vd,uint64_t * asize,uint64_t * ashift)130789Sahrens vdev_mirror_open(vdev_t *vd, uint64_t *asize, uint64_t *ashift)
131789Sahrens {
132789Sahrens 	int numerrors = 0;
1339846SEric.Taylor@Sun.COM 	int lasterror = 0;
134789Sahrens 
135789Sahrens 	if (vd->vdev_children == 0) {
136789Sahrens 		vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
137789Sahrens 		return (EINVAL);
138789Sahrens 	}
139789Sahrens 
1409846SEric.Taylor@Sun.COM 	vdev_open_children(vd);
141789Sahrens 
1429846SEric.Taylor@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++) {
1439846SEric.Taylor@Sun.COM 		vdev_t *cvd = vd->vdev_child[c];
1449846SEric.Taylor@Sun.COM 
1459846SEric.Taylor@Sun.COM 		if (cvd->vdev_open_error) {
1469846SEric.Taylor@Sun.COM 			lasterror = cvd->vdev_open_error;
147789Sahrens 			numerrors++;
148789Sahrens 			continue;
149789Sahrens 		}
150789Sahrens 
151789Sahrens 		*asize = MIN(*asize - 1, cvd->vdev_asize - 1) + 1;
1521732Sbonwick 		*ashift = MAX(*ashift, cvd->vdev_ashift);
153789Sahrens 	}
154789Sahrens 
155789Sahrens 	if (numerrors == vd->vdev_children) {
156789Sahrens 		vd->vdev_stat.vs_aux = VDEV_AUX_NO_REPLICAS;
157789Sahrens 		return (lasterror);
158789Sahrens 	}
159789Sahrens 
160789Sahrens 	return (0);
161789Sahrens }
162789Sahrens 
163789Sahrens static void
vdev_mirror_close(vdev_t * vd)164789Sahrens vdev_mirror_close(vdev_t *vd)
165789Sahrens {
1669846SEric.Taylor@Sun.COM 	for (int c = 0; c < vd->vdev_children; c++)
167789Sahrens 		vdev_close(vd->vdev_child[c]);
168789Sahrens }
169789Sahrens 
170789Sahrens static void
vdev_mirror_child_done(zio_t * zio)171789Sahrens vdev_mirror_child_done(zio_t *zio)
172789Sahrens {
1731775Sbillm 	mirror_child_t *mc = zio->io_private;
174789Sahrens 
1751775Sbillm 	mc->mc_error = zio->io_error;
1761775Sbillm 	mc->mc_tried = 1;
1771775Sbillm 	mc->mc_skipped = 0;
178789Sahrens }
179789Sahrens 
180789Sahrens static void
vdev_mirror_scrub_done(zio_t * zio)181789Sahrens vdev_mirror_scrub_done(zio_t *zio)
182789Sahrens {
1831775Sbillm 	mirror_child_t *mc = zio->io_private;
184789Sahrens 
185789Sahrens 	if (zio->io_error == 0) {
1868632SBill.Moore@Sun.COM 		zio_t *pio;
1878632SBill.Moore@Sun.COM 
1888632SBill.Moore@Sun.COM 		mutex_enter(&zio->io_lock);
1898632SBill.Moore@Sun.COM 		while ((pio = zio_walk_parents(zio)) != NULL) {
1908632SBill.Moore@Sun.COM 			mutex_enter(&pio->io_lock);
1918632SBill.Moore@Sun.COM 			ASSERT3U(zio->io_size, >=, pio->io_size);
1928632SBill.Moore@Sun.COM 			bcopy(zio->io_data, pio->io_data, pio->io_size);
1938632SBill.Moore@Sun.COM 			mutex_exit(&pio->io_lock);
1948632SBill.Moore@Sun.COM 		}
1958632SBill.Moore@Sun.COM 		mutex_exit(&zio->io_lock);
196789Sahrens 	}
197789Sahrens 
198789Sahrens 	zio_buf_free(zio->io_data, zio->io_size);
199789Sahrens 
2001775Sbillm 	mc->mc_error = zio->io_error;
2011775Sbillm 	mc->mc_tried = 1;
2021775Sbillm 	mc->mc_skipped = 0;
203789Sahrens }
204789Sahrens 
205789Sahrens /*
206789Sahrens  * Try to find a child whose DTL doesn't contain the block we want to read.
207789Sahrens  * If we can't, try the read on any vdev we haven't already tried.
208789Sahrens  */
209789Sahrens static int
vdev_mirror_child_select(zio_t * zio)210789Sahrens vdev_mirror_child_select(zio_t *zio)
211789Sahrens {
212789Sahrens 	mirror_map_t *mm = zio->io_vsd;
2131775Sbillm 	mirror_child_t *mc;
214789Sahrens 	uint64_t txg = zio->io_txg;
215789Sahrens 	int i, c;
216789Sahrens 
21710922SJeff.Bonwick@Sun.COM 	ASSERT(zio->io_bp == NULL || BP_PHYSICAL_BIRTH(zio->io_bp) == txg);
218789Sahrens 
219789Sahrens 	/*
220789Sahrens 	 * Try to find a child whose DTL doesn't contain the block to read.
221789Sahrens 	 * If a child is known to be completely inaccessible (indicated by
2225329Sgw25295 	 * vdev_readable() returning B_FALSE), don't even try.
223789Sahrens 	 */
2241775Sbillm 	for (i = 0, c = mm->mm_preferred; i < mm->mm_children; i++, c++) {
2251775Sbillm 		if (c >= mm->mm_children)
226789Sahrens 			c = 0;
2271775Sbillm 		mc = &mm->mm_child[c];
2281775Sbillm 		if (mc->mc_tried || mc->mc_skipped)
229789Sahrens 			continue;
2307754SJeff.Bonwick@Sun.COM 		if (!vdev_readable(mc->mc_vd)) {
2311775Sbillm 			mc->mc_error = ENXIO;
2321775Sbillm 			mc->mc_tried = 1;	/* don't even try */
2331775Sbillm 			mc->mc_skipped = 1;
234789Sahrens 			continue;
235789Sahrens 		}
2368241SJeff.Bonwick@Sun.COM 		if (!vdev_dtl_contains(mc->mc_vd, DTL_MISSING, txg, 1))
237789Sahrens 			return (c);
2381775Sbillm 		mc->mc_error = ESTALE;
2391775Sbillm 		mc->mc_skipped = 1;
2407754SJeff.Bonwick@Sun.COM 		mc->mc_speculative = 1;
241789Sahrens 	}
242789Sahrens 
243789Sahrens 	/*
244789Sahrens 	 * Every device is either missing or has this txg in its DTL.
2451775Sbillm 	 * Look for any child we haven't already tried before giving up.
246789Sahrens 	 */
2471775Sbillm 	for (c = 0; c < mm->mm_children; c++)
2481775Sbillm 		if (!mm->mm_child[c].mc_tried)
2491775Sbillm 			return (c);
250789Sahrens 
251789Sahrens 	/*
252789Sahrens 	 * Every child failed.  There's no place left to look.
253789Sahrens 	 */
254789Sahrens 	return (-1);
255789Sahrens }
256789Sahrens 
2575530Sbonwick static int
vdev_mirror_io_start(zio_t * zio)258789Sahrens vdev_mirror_io_start(zio_t *zio)
259789Sahrens {
260789Sahrens 	mirror_map_t *mm;
2611775Sbillm 	mirror_child_t *mc;
262789Sahrens 	int c, children;
263789Sahrens 
264789Sahrens 	mm = vdev_mirror_map_alloc(zio);
265789Sahrens 
266789Sahrens 	if (zio->io_type == ZIO_TYPE_READ) {
2671775Sbillm 		if ((zio->io_flags & ZIO_FLAG_SCRUB) && !mm->mm_replacing) {
268789Sahrens 			/*
269789Sahrens 			 * For scrubbing reads we need to allocate a read
270789Sahrens 			 * buffer for each child and issue reads to all
271789Sahrens 			 * children.  If any child succeeds, it will copy its
272789Sahrens 			 * data into zio->io_data in vdev_mirror_scrub_done.
273789Sahrens 			 */
2741775Sbillm 			for (c = 0; c < mm->mm_children; c++) {
2751775Sbillm 				mc = &mm->mm_child[c];
276789Sahrens 				zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
2771775Sbillm 				    mc->mc_vd, mc->mc_offset,
278789Sahrens 				    zio_buf_alloc(zio->io_size), zio->io_size,
2797754SJeff.Bonwick@Sun.COM 				    zio->io_type, zio->io_priority, 0,
2801775Sbillm 				    vdev_mirror_scrub_done, mc));
281789Sahrens 			}
2827754SJeff.Bonwick@Sun.COM 			return (ZIO_PIPELINE_CONTINUE);
283789Sahrens 		}
284789Sahrens 		/*
285789Sahrens 		 * For normal reads just pick one child.
286789Sahrens 		 */
287789Sahrens 		c = vdev_mirror_child_select(zio);
288789Sahrens 		children = (c >= 0);
289789Sahrens 	} else {
290789Sahrens 		ASSERT(zio->io_type == ZIO_TYPE_WRITE);
291789Sahrens 
292789Sahrens 		/*
2938241SJeff.Bonwick@Sun.COM 		 * Writes go to all children.
294789Sahrens 		 */
2958241SJeff.Bonwick@Sun.COM 		c = 0;
2968241SJeff.Bonwick@Sun.COM 		children = mm->mm_children;
297789Sahrens 	}
298789Sahrens 
299789Sahrens 	while (children--) {
3001775Sbillm 		mc = &mm->mm_child[c];
301789Sahrens 		zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
3027754SJeff.Bonwick@Sun.COM 		    mc->mc_vd, mc->mc_offset, zio->io_data, zio->io_size,
3037754SJeff.Bonwick@Sun.COM 		    zio->io_type, zio->io_priority, 0,
3047754SJeff.Bonwick@Sun.COM 		    vdev_mirror_child_done, mc));
305789Sahrens 		c++;
306789Sahrens 	}
307789Sahrens 
3087754SJeff.Bonwick@Sun.COM 	return (ZIO_PIPELINE_CONTINUE);
309789Sahrens }
310789Sahrens 
3115530Sbonwick static int
vdev_mirror_worst_error(mirror_map_t * mm)3127754SJeff.Bonwick@Sun.COM vdev_mirror_worst_error(mirror_map_t *mm)
3137754SJeff.Bonwick@Sun.COM {
3147754SJeff.Bonwick@Sun.COM 	int error[2] = { 0, 0 };
3157754SJeff.Bonwick@Sun.COM 
3167754SJeff.Bonwick@Sun.COM 	for (int c = 0; c < mm->mm_children; c++) {
3177754SJeff.Bonwick@Sun.COM 		mirror_child_t *mc = &mm->mm_child[c];
3187754SJeff.Bonwick@Sun.COM 		int s = mc->mc_speculative;
3197754SJeff.Bonwick@Sun.COM 		error[s] = zio_worst_error(error[s], mc->mc_error);
3207754SJeff.Bonwick@Sun.COM 	}
3217754SJeff.Bonwick@Sun.COM 
3227754SJeff.Bonwick@Sun.COM 	return (error[0] ? error[0] : error[1]);
3237754SJeff.Bonwick@Sun.COM }
3247754SJeff.Bonwick@Sun.COM 
3257754SJeff.Bonwick@Sun.COM static void
vdev_mirror_io_done(zio_t * zio)326789Sahrens vdev_mirror_io_done(zio_t *zio)
327789Sahrens {
328789Sahrens 	mirror_map_t *mm = zio->io_vsd;
3291775Sbillm 	mirror_child_t *mc;
330789Sahrens 	int c;
331789Sahrens 	int good_copies = 0;
332789Sahrens 	int unexpected_errors = 0;
333789Sahrens 
3341775Sbillm 	for (c = 0; c < mm->mm_children; c++) {
3351775Sbillm 		mc = &mm->mm_child[c];
3361775Sbillm 
3371775Sbillm 		if (mc->mc_error) {
3381775Sbillm 			if (!mc->mc_skipped)
339789Sahrens 				unexpected_errors++;
3407754SJeff.Bonwick@Sun.COM 		} else if (mc->mc_tried) {
3417754SJeff.Bonwick@Sun.COM 			good_copies++;
342789Sahrens 		}
343789Sahrens 	}
344789Sahrens 
345789Sahrens 	if (zio->io_type == ZIO_TYPE_WRITE) {
346789Sahrens 		/*
347789Sahrens 		 * XXX -- for now, treat partial writes as success.
3487754SJeff.Bonwick@Sun.COM 		 *
3497754SJeff.Bonwick@Sun.COM 		 * Now that we support write reallocation, it would be better
3507754SJeff.Bonwick@Sun.COM 		 * to treat partial failure as real failure unless there are
3517754SJeff.Bonwick@Sun.COM 		 * no non-degraded top-level vdevs left, and not update DTLs
3527754SJeff.Bonwick@Sun.COM 		 * if we intend to reallocate.
353789Sahrens 		 */
354789Sahrens 		/* XXPOLICY */
3557754SJeff.Bonwick@Sun.COM 		if (good_copies != mm->mm_children) {
3567754SJeff.Bonwick@Sun.COM 			/*
3577754SJeff.Bonwick@Sun.COM 			 * Always require at least one good copy.
3587754SJeff.Bonwick@Sun.COM 			 *
3597754SJeff.Bonwick@Sun.COM 			 * For ditto blocks (io_vd == NULL), require
3607754SJeff.Bonwick@Sun.COM 			 * all copies to be good.
3617754SJeff.Bonwick@Sun.COM 			 *
3627754SJeff.Bonwick@Sun.COM 			 * XXX -- for replacing vdevs, there's no great answer.
3637754SJeff.Bonwick@Sun.COM 			 * If the old device is really dead, we may not even
3647754SJeff.Bonwick@Sun.COM 			 * be able to access it -- so we only want to
3657754SJeff.Bonwick@Sun.COM 			 * require good writes to the new device.  But if
3667754SJeff.Bonwick@Sun.COM 			 * the new device turns out to be flaky, we want
3677754SJeff.Bonwick@Sun.COM 			 * to be able to detach it -- which requires all
3687754SJeff.Bonwick@Sun.COM 			 * writes to the old device to have succeeded.
3697754SJeff.Bonwick@Sun.COM 			 */
3707754SJeff.Bonwick@Sun.COM 			if (good_copies == 0 || zio->io_vd == NULL)
3717754SJeff.Bonwick@Sun.COM 				zio->io_error = vdev_mirror_worst_error(mm);
3727754SJeff.Bonwick@Sun.COM 		}
3737754SJeff.Bonwick@Sun.COM 		return;
374789Sahrens 	}
375789Sahrens 
376789Sahrens 	ASSERT(zio->io_type == ZIO_TYPE_READ);
377789Sahrens 
378789Sahrens 	/*
379789Sahrens 	 * If we don't have a good copy yet, keep trying other children.
380789Sahrens 	 */
381789Sahrens 	/* XXPOLICY */
382789Sahrens 	if (good_copies == 0 && (c = vdev_mirror_child_select(zio)) != -1) {
3831775Sbillm 		ASSERT(c >= 0 && c < mm->mm_children);
3841775Sbillm 		mc = &mm->mm_child[c];
385789Sahrens 		zio_vdev_io_redone(zio);
3861775Sbillm 		zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
3871775Sbillm 		    mc->mc_vd, mc->mc_offset, zio->io_data, zio->io_size,
3887754SJeff.Bonwick@Sun.COM 		    ZIO_TYPE_READ, zio->io_priority, 0,
3891775Sbillm 		    vdev_mirror_child_done, mc));
3907754SJeff.Bonwick@Sun.COM 		return;
391789Sahrens 	}
392789Sahrens 
393789Sahrens 	/* XXPOLICY */
3947754SJeff.Bonwick@Sun.COM 	if (good_copies == 0) {
3957754SJeff.Bonwick@Sun.COM 		zio->io_error = vdev_mirror_worst_error(mm);
396789Sahrens 		ASSERT(zio->io_error != 0);
3977754SJeff.Bonwick@Sun.COM 	}
398789Sahrens 
3998241SJeff.Bonwick@Sun.COM 	if (good_copies && spa_writeable(zio->io_spa) &&
4001807Sbonwick 	    (unexpected_errors ||
4011807Sbonwick 	    (zio->io_flags & ZIO_FLAG_RESILVER) ||
4021807Sbonwick 	    ((zio->io_flags & ZIO_FLAG_SCRUB) && mm->mm_replacing))) {
403789Sahrens 		/*
404789Sahrens 		 * Use the good data we have in hand to repair damaged children.
405789Sahrens 		 */
4061775Sbillm 		for (c = 0; c < mm->mm_children; c++) {
407789Sahrens 			/*
408789Sahrens 			 * Don't rewrite known good children.
409789Sahrens 			 * Not only is it unnecessary, it could
410789Sahrens 			 * actually be harmful: if the system lost
411789Sahrens 			 * power while rewriting the only good copy,
412789Sahrens 			 * there would be no good copies left!
413789Sahrens 			 */
4141775Sbillm 			mc = &mm->mm_child[c];
415789Sahrens 
4161775Sbillm 			if (mc->mc_error == 0) {
4171775Sbillm 				if (mc->mc_tried)
418789Sahrens 					continue;
4191807Sbonwick 				if (!(zio->io_flags & ZIO_FLAG_SCRUB) &&
4208241SJeff.Bonwick@Sun.COM 				    !vdev_dtl_contains(mc->mc_vd, DTL_PARTIAL,
421789Sahrens 				    zio->io_txg, 1))
422789Sahrens 					continue;
4231775Sbillm 				mc->mc_error = ESTALE;
424789Sahrens 			}
425789Sahrens 
4267754SJeff.Bonwick@Sun.COM 			zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
4277754SJeff.Bonwick@Sun.COM 			    mc->mc_vd, mc->mc_offset,
4287754SJeff.Bonwick@Sun.COM 			    zio->io_data, zio->io_size,
429789Sahrens 			    ZIO_TYPE_WRITE, zio->io_priority,
4308241SJeff.Bonwick@Sun.COM 			    ZIO_FLAG_IO_REPAIR | (unexpected_errors ?
4318241SJeff.Bonwick@Sun.COM 			    ZIO_FLAG_SELF_HEAL : 0), NULL, NULL));
432789Sahrens 		}
433789Sahrens 	}
434789Sahrens }
435789Sahrens 
436789Sahrens static void
vdev_mirror_state_change(vdev_t * vd,int faulted,int degraded)437789Sahrens vdev_mirror_state_change(vdev_t *vd, int faulted, int degraded)
438789Sahrens {
439789Sahrens 	if (faulted == vd->vdev_children)
4401544Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
4411544Seschrock 		    VDEV_AUX_NO_REPLICAS);
442789Sahrens 	else if (degraded + faulted != 0)
4431544Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, VDEV_AUX_NONE);
444789Sahrens 	else
4451544Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_HEALTHY, VDEV_AUX_NONE);
446789Sahrens }
447789Sahrens 
448789Sahrens vdev_ops_t vdev_mirror_ops = {
449789Sahrens 	vdev_mirror_open,
450789Sahrens 	vdev_mirror_close,
451789Sahrens 	vdev_default_asize,
452789Sahrens 	vdev_mirror_io_start,
453789Sahrens 	vdev_mirror_io_done,
454789Sahrens 	vdev_mirror_state_change,
455*11958SGeorge.Wilson@Sun.COM 	NULL,
456*11958SGeorge.Wilson@Sun.COM 	NULL,
457789Sahrens 	VDEV_TYPE_MIRROR,	/* name of this vdev type */
458789Sahrens 	B_FALSE			/* not a leaf vdev */
459789Sahrens };
460789Sahrens 
461789Sahrens vdev_ops_t vdev_replacing_ops = {
462789Sahrens 	vdev_mirror_open,
463789Sahrens 	vdev_mirror_close,
464789Sahrens 	vdev_default_asize,
465789Sahrens 	vdev_mirror_io_start,
466789Sahrens 	vdev_mirror_io_done,
467789Sahrens 	vdev_mirror_state_change,
468*11958SGeorge.Wilson@Sun.COM 	NULL,
469*11958SGeorge.Wilson@Sun.COM 	NULL,
470789Sahrens 	VDEV_TYPE_REPLACING,	/* name of this vdev type */
471789Sahrens 	B_FALSE			/* not a leaf vdev */
472789Sahrens };
4732082Seschrock 
4742082Seschrock vdev_ops_t vdev_spare_ops = {
4752082Seschrock 	vdev_mirror_open,
4762082Seschrock 	vdev_mirror_close,
4772082Seschrock 	vdev_default_asize,
4782082Seschrock 	vdev_mirror_io_start,
4792082Seschrock 	vdev_mirror_io_done,
4802082Seschrock 	vdev_mirror_state_change,
481*11958SGeorge.Wilson@Sun.COM 	NULL,
482*11958SGeorge.Wilson@Sun.COM 	NULL,
4832082Seschrock 	VDEV_TYPE_SPARE,	/* name of this vdev type */
4842082Seschrock 	B_FALSE			/* not a leaf vdev */
4852082Seschrock };
486