xref: /illumos-gate/usr/src/uts/common/fs/zfs/vdev_mirror.c (revision 33c72b7598992897b94815b1f47b7b8077e53808)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
28  */
29 
30 #include <sys/zfs_context.h>
31 #include <sys/spa.h>
32 #include <sys/spa_impl.h>
33 #include <sys/dsl_pool.h>
34 #include <sys/dsl_scan.h>
35 #include <sys/vdev_impl.h>
36 #include <sys/zio.h>
37 #include <sys/abd.h>
38 #include <sys/fs/zfs.h>
39 
40 /*
41  * Vdev mirror kstats
42  */
43 static kstat_t *mirror_ksp = NULL;
44 
45 typedef struct mirror_stats {
46 	kstat_named_t vdev_mirror_stat_rotating_linear;
47 	kstat_named_t vdev_mirror_stat_rotating_offset;
48 	kstat_named_t vdev_mirror_stat_rotating_seek;
49 	kstat_named_t vdev_mirror_stat_non_rotating_linear;
50 	kstat_named_t vdev_mirror_stat_non_rotating_seek;
51 
52 	kstat_named_t vdev_mirror_stat_preferred_found;
53 	kstat_named_t vdev_mirror_stat_preferred_not_found;
54 } mirror_stats_t;
55 
56 static mirror_stats_t mirror_stats = {
57 	/* New I/O follows directly the last I/O */
58 	{ "rotating_linear",			KSTAT_DATA_UINT64 },
59 	/* New I/O is within zfs_vdev_mirror_rotating_seek_offset of the last */
60 	{ "rotating_offset",			KSTAT_DATA_UINT64 },
61 	/* New I/O requires random seek */
62 	{ "rotating_seek",			KSTAT_DATA_UINT64 },
63 	/* New I/O follows directly the last I/O  (nonrot) */
64 	{ "non_rotating_linear",		KSTAT_DATA_UINT64 },
65 	/* New I/O requires random seek (nonrot) */
66 	{ "non_rotating_seek",			KSTAT_DATA_UINT64 },
67 	/* Preferred child vdev found */
68 	{ "preferred_found",			KSTAT_DATA_UINT64 },
69 	/* Preferred child vdev not found or equal load  */
70 	{ "preferred_not_found",		KSTAT_DATA_UINT64 },
71 
72 };
73 
74 #define	MIRROR_STAT(stat)		(mirror_stats.stat.value.ui64)
75 #define	MIRROR_INCR(stat, val)		atomic_add_64(&MIRROR_STAT(stat), val)
76 #define	MIRROR_BUMP(stat)		MIRROR_INCR(stat, 1)
77 
78 void
79 vdev_mirror_stat_init(void)
80 {
81 	mirror_ksp = kstat_create("zfs", 0, "vdev_mirror_stats",
82 	    "misc", KSTAT_TYPE_NAMED,
83 	    sizeof (mirror_stats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL);
84 	if (mirror_ksp != NULL) {
85 		mirror_ksp->ks_data = &mirror_stats;
86 		kstat_install(mirror_ksp);
87 	}
88 }
89 
90 void
91 vdev_mirror_stat_fini(void)
92 {
93 	if (mirror_ksp != NULL) {
94 		kstat_delete(mirror_ksp);
95 		mirror_ksp = NULL;
96 	}
97 }
98 
99 /*
100  * Virtual device vector for mirroring.
101  */
102 
103 typedef struct mirror_child {
104 	vdev_t		*mc_vd;
105 	uint64_t	mc_offset;
106 	int		mc_error;
107 	int		mc_load;
108 	uint8_t		mc_tried;
109 	uint8_t		mc_skipped;
110 	uint8_t		mc_speculative;
111 } mirror_child_t;
112 
113 typedef struct mirror_map {
114 	int		*mm_preferred;
115 	int		mm_preferred_cnt;
116 	int		mm_children;
117 	int		mm_resilvering;
118 	int		mm_root;
119 	mirror_child_t	mm_child[];
120 } mirror_map_t;
121 
122 int vdev_mirror_shift = 21;
123 
124 /*
125  * The load configuration settings below are tuned by default for
126  * the case where all devices are of the same rotational type.
127  *
128  * If there is a mixture of rotating and non-rotating media, setting
129  * zfs_vdev_mirror_non_rotating_seek_inc to 0 may well provide better results
130  * as it will direct more reads to the non-rotating vdevs which are more likely
131  * to have a higher performance.
132  */
133 
134 /* Rotating media load calculation configuration. */
135 static int zfs_vdev_mirror_rotating_inc = 0;
136 static int zfs_vdev_mirror_rotating_seek_inc = 5;
137 static int zfs_vdev_mirror_rotating_seek_offset = 1 * 1024 * 1024;
138 
139 /* Non-rotating media load calculation configuration. */
140 static int zfs_vdev_mirror_non_rotating_inc = 0;
141 static int zfs_vdev_mirror_non_rotating_seek_inc = 1;
142 
143 static inline size_t
144 vdev_mirror_map_size(int children)
145 {
146 	return (offsetof(mirror_map_t, mm_child[children]) +
147 	    sizeof (int) * children);
148 }
149 
150 static inline mirror_map_t *
151 vdev_mirror_map_alloc(int children, boolean_t resilvering, boolean_t root)
152 {
153 	mirror_map_t *mm;
154 
155 	mm = kmem_zalloc(vdev_mirror_map_size(children), KM_SLEEP);
156 	mm->mm_children = children;
157 	mm->mm_resilvering = resilvering;
158 	mm->mm_root = root;
159 	mm->mm_preferred = (int *)((uintptr_t)mm +
160 	    offsetof(mirror_map_t, mm_child[children]));
161 
162 	return (mm);
163 }
164 
165 static void
166 vdev_mirror_map_free(zio_t *zio)
167 {
168 	mirror_map_t *mm = zio->io_vsd;
169 
170 	kmem_free(mm, vdev_mirror_map_size(mm->mm_children));
171 }
172 
173 static const zio_vsd_ops_t vdev_mirror_vsd_ops = {
174 	.vsd_free = vdev_mirror_map_free,
175 	.vsd_cksum_report = zio_vsd_default_cksum_report
176 };
177 
178 static int
179 vdev_mirror_load(mirror_map_t *mm, vdev_t *vd, uint64_t zio_offset)
180 {
181 	uint64_t last_offset;
182 	int64_t offset_diff;
183 	int load;
184 
185 	/* All DVAs have equal weight at the root. */
186 	if (mm->mm_root)
187 		return (INT_MAX);
188 
189 	/*
190 	 * We don't return INT_MAX if the device is resilvering i.e.
191 	 * vdev_resilver_txg != 0 as when tested performance was slightly
192 	 * worse overall when resilvering with compared to without.
193 	 */
194 
195 	/* Fix zio_offset for leaf vdevs */
196 	if (vd->vdev_ops->vdev_op_leaf)
197 		zio_offset += VDEV_LABEL_START_SIZE;
198 
199 	/* Standard load based on pending queue length. */
200 	load = vdev_queue_length(vd);
201 	last_offset = vdev_queue_last_offset(vd);
202 
203 	if (vd->vdev_nonrot) {
204 		/* Non-rotating media. */
205 		if (last_offset == zio_offset) {
206 			MIRROR_BUMP(vdev_mirror_stat_non_rotating_linear);
207 			return (load + zfs_vdev_mirror_non_rotating_inc);
208 		}
209 
210 		/*
211 		 * Apply a seek penalty even for non-rotating devices as
212 		 * sequential I/O's can be aggregated into fewer operations on
213 		 * the device, thus avoiding unnecessary per-command overhead
214 		 * and boosting performance.
215 		 */
216 		MIRROR_BUMP(vdev_mirror_stat_non_rotating_seek);
217 		return (load + zfs_vdev_mirror_non_rotating_seek_inc);
218 	}
219 
220 	/* Rotating media I/O's which directly follow the last I/O. */
221 	if (last_offset == zio_offset) {
222 		MIRROR_BUMP(vdev_mirror_stat_rotating_linear);
223 		return (load + zfs_vdev_mirror_rotating_inc);
224 	}
225 
226 	/*
227 	 * Apply half the seek increment to I/O's within seek offset
228 	 * of the last I/O issued to this vdev as they should incur less
229 	 * of a seek increment.
230 	 */
231 	offset_diff = (int64_t)(last_offset - zio_offset);
232 	if (ABS(offset_diff) < zfs_vdev_mirror_rotating_seek_offset) {
233 		MIRROR_BUMP(vdev_mirror_stat_rotating_offset);
234 		return (load + (zfs_vdev_mirror_rotating_seek_inc / 2));
235 	}
236 
237 	/* Apply the full seek increment to all other I/O's. */
238 	MIRROR_BUMP(vdev_mirror_stat_rotating_seek);
239 	return (load + zfs_vdev_mirror_rotating_seek_inc);
240 }
241 
242 static mirror_map_t *
243 vdev_mirror_map_init(zio_t *zio)
244 {
245 	mirror_map_t *mm = NULL;
246 	mirror_child_t *mc;
247 	vdev_t *vd = zio->io_vd;
248 	int c;
249 
250 	if (vd == NULL) {
251 		dva_t *dva = zio->io_bp->blk_dva;
252 		spa_t *spa = zio->io_spa;
253 		dsl_scan_t *scn = NULL;
254 		dva_t dva_copy[SPA_DVAS_PER_BP];
255 
256 		if (spa->spa_dsl_pool != NULL) {
257 			scn = spa->spa_dsl_pool->dp_scan;
258 		}
259 		/*
260 		 * The sequential scrub code sorts and issues all DVAs
261 		 * of a bp separately. Each of these IOs includes all
262 		 * original DVA copies so that repairs can be performed
263 		 * in the event of an error, but we only actually want
264 		 * to check the first DVA since the others will be
265 		 * checked by their respective sorted IOs. Only if we
266 		 * hit an error will we try all DVAs upon retrying.
267 		 *
268 		 * Note: This check is safe even if the user switches
269 		 * from a legacy scrub to a sequential one in the middle
270 		 * of processing, since scn_is_sorted isn't updated until
271 		 * all outstanding IOs from the previous scrub pass
272 		 * complete.
273 		 */
274 		if ((zio->io_flags & ZIO_FLAG_SCRUB) &&
275 		    !(zio->io_flags & ZIO_FLAG_IO_RETRY) &&
276 		    scn != NULL &&
277 		    scn->scn_is_sorted &&
278 		    dsl_scan_scrubbing(spa->spa_dsl_pool)) {
279 			c = 1;
280 		} else {
281 			c = BP_GET_NDVAS(zio->io_bp);
282 		}
283 
284 		/*
285 		 * If we do not trust the pool config, some DVAs might be
286 		 * invalid or point to vdevs that do not exist. We skip them.
287 		 */
288 		if (!spa_trust_config(spa)) {
289 			ASSERT3U(zio->io_type, ==, ZIO_TYPE_READ);
290 			int j = 0;
291 			for (int i = 0; i < c; i++) {
292 				if (zfs_dva_valid(spa, &dva[i], zio->io_bp))
293 					dva_copy[j++] = dva[i];
294 			}
295 			if (j == 0) {
296 				zio->io_vsd = NULL;
297 				zio->io_error = ENXIO;
298 				return (NULL);
299 			}
300 			if (j < c) {
301 				dva = dva_copy;
302 				c = j;
303 			}
304 		}
305 
306 		mm = vdev_mirror_map_alloc(c, B_FALSE, B_TRUE);
307 		for (c = 0; c < mm->mm_children; c++) {
308 			mc = &mm->mm_child[c];
309 
310 			mc->mc_vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[c]));
311 			mc->mc_offset = DVA_GET_OFFSET(&dva[c]);
312 		}
313 	} else {
314 		/*
315 		 * If we are resilvering, then we should handle scrub reads
316 		 * differently; we shouldn't issue them to the resilvering
317 		 * device because it might not have those blocks.
318 		 *
319 		 * We are resilvering iff:
320 		 * 1) We are a replacing vdev (ie our name is "replacing-1" or
321 		 *    "spare-1" or something like that), and
322 		 * 2) The pool is currently being resilvered.
323 		 *
324 		 * We cannot simply check vd->vdev_resilver_txg, because it's
325 		 * not set in this path.
326 		 *
327 		 * Nor can we just check our vdev_ops; there are cases (such as
328 		 * when a user types "zpool replace pool odev spare_dev" and
329 		 * spare_dev is in the spare list, or when a spare device is
330 		 * automatically used to replace a DEGRADED device) when
331 		 * resilvering is complete but both the original vdev and the
332 		 * spare vdev remain in the pool.  That behavior is intentional.
333 		 * It helps implement the policy that a spare should be
334 		 * automatically removed from the pool after the user replaces
335 		 * the device that originally failed.
336 		 */
337 		boolean_t replacing = (vd->vdev_ops == &vdev_replacing_ops ||
338 		    vd->vdev_ops == &vdev_spare_ops) &&
339 		    spa_load_state(vd->vdev_spa) == SPA_LOAD_NONE &&
340 		    dsl_scan_resilvering(vd->vdev_spa->spa_dsl_pool);
341 		mm = vdev_mirror_map_alloc(vd->vdev_children, replacing,
342 		    B_FALSE);
343 		for (c = 0; c < mm->mm_children; c++) {
344 			mc = &mm->mm_child[c];
345 			mc->mc_vd = vd->vdev_child[c];
346 			mc->mc_offset = zio->io_offset;
347 		}
348 	}
349 
350 	zio->io_vsd = mm;
351 	zio->io_vsd_ops = &vdev_mirror_vsd_ops;
352 	return (mm);
353 }
354 
355 static int
356 vdev_mirror_open(vdev_t *vd, uint64_t *asize, uint64_t *max_asize,
357     uint64_t *ashift)
358 {
359 	int numerrors = 0;
360 	int lasterror = 0;
361 
362 	if (vd->vdev_children == 0) {
363 		vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
364 		return (SET_ERROR(EINVAL));
365 	}
366 
367 	vdev_open_children(vd);
368 
369 	for (int c = 0; c < vd->vdev_children; c++) {
370 		vdev_t *cvd = vd->vdev_child[c];
371 
372 		if (cvd->vdev_open_error) {
373 			lasterror = cvd->vdev_open_error;
374 			numerrors++;
375 			continue;
376 		}
377 
378 		*asize = MIN(*asize - 1, cvd->vdev_asize - 1) + 1;
379 		*max_asize = MIN(*max_asize - 1, cvd->vdev_max_asize - 1) + 1;
380 		*ashift = MAX(*ashift, cvd->vdev_ashift);
381 	}
382 
383 	if (numerrors == vd->vdev_children) {
384 		if (vdev_children_are_offline(vd))
385 			vd->vdev_stat.vs_aux = VDEV_AUX_CHILDREN_OFFLINE;
386 		else
387 			vd->vdev_stat.vs_aux = VDEV_AUX_NO_REPLICAS;
388 		return (lasterror);
389 	}
390 
391 	return (0);
392 }
393 
394 static void
395 vdev_mirror_close(vdev_t *vd)
396 {
397 	for (int c = 0; c < vd->vdev_children; c++)
398 		vdev_close(vd->vdev_child[c]);
399 }
400 
401 static void
402 vdev_mirror_child_done(zio_t *zio)
403 {
404 	mirror_child_t *mc = zio->io_private;
405 
406 	mc->mc_error = zio->io_error;
407 	mc->mc_tried = 1;
408 	mc->mc_skipped = 0;
409 }
410 
411 static void
412 vdev_mirror_scrub_done(zio_t *zio)
413 {
414 	mirror_child_t *mc = zio->io_private;
415 
416 	if (zio->io_error == 0) {
417 		zio_t *pio;
418 		zio_link_t *zl = NULL;
419 
420 		mutex_enter(&zio->io_lock);
421 		while ((pio = zio_walk_parents(zio, &zl)) != NULL) {
422 			mutex_enter(&pio->io_lock);
423 			ASSERT3U(zio->io_size, >=, pio->io_size);
424 			abd_copy(pio->io_abd, zio->io_abd, pio->io_size);
425 			mutex_exit(&pio->io_lock);
426 		}
427 		mutex_exit(&zio->io_lock);
428 	}
429 
430 	abd_free(zio->io_abd);
431 
432 	mc->mc_error = zio->io_error;
433 	mc->mc_tried = 1;
434 	mc->mc_skipped = 0;
435 }
436 
437 /*
438  * Check the other, lower-index DVAs to see if they're on the same
439  * vdev as the child we picked.  If they are, use them since they
440  * are likely to have been allocated from the primary metaslab in
441  * use at the time, and hence are more likely to have locality with
442  * single-copy data.
443  */
444 static int
445 vdev_mirror_dva_select(zio_t *zio, int p)
446 {
447 	dva_t *dva = zio->io_bp->blk_dva;
448 	mirror_map_t *mm = zio->io_vsd;
449 	int preferred;
450 	int c;
451 
452 	preferred = mm->mm_preferred[p];
453 	for (p--; p >= 0; p--) {
454 		c = mm->mm_preferred[p];
455 		if (DVA_GET_VDEV(&dva[c]) == DVA_GET_VDEV(&dva[preferred]))
456 			preferred = c;
457 	}
458 	return (preferred);
459 }
460 
461 static int
462 vdev_mirror_preferred_child_randomize(zio_t *zio)
463 {
464 	mirror_map_t *mm = zio->io_vsd;
465 	int p;
466 
467 	if (mm->mm_root) {
468 		p = spa_get_random(mm->mm_preferred_cnt);
469 		return (vdev_mirror_dva_select(zio, p));
470 	}
471 
472 	/*
473 	 * To ensure we don't always favour the first matching vdev,
474 	 * which could lead to wear leveling issues on SSD's, we
475 	 * use the I/O offset as a pseudo random seed into the vdevs
476 	 * which have the lowest load.
477 	 */
478 	p = (zio->io_offset >> vdev_mirror_shift) % mm->mm_preferred_cnt;
479 	return (mm->mm_preferred[p]);
480 }
481 
482 /*
483  * Try to find a vdev whose DTL doesn't contain the block we want to read
484  * prefering vdevs based on determined load.
485  *
486  * Try to find a child whose DTL doesn't contain the block we want to read.
487  * If we can't, try the read on any vdev we haven't already tried.
488  */
489 static int
490 vdev_mirror_child_select(zio_t *zio)
491 {
492 	mirror_map_t *mm = zio->io_vsd;
493 	uint64_t txg = zio->io_txg;
494 	int c, lowest_load;
495 
496 	ASSERT(zio->io_bp == NULL || BP_PHYSICAL_BIRTH(zio->io_bp) == txg);
497 
498 	lowest_load = INT_MAX;
499 	mm->mm_preferred_cnt = 0;
500 	for (c = 0; c < mm->mm_children; c++) {
501 		mirror_child_t *mc;
502 
503 		mc = &mm->mm_child[c];
504 		if (mc->mc_tried || mc->mc_skipped)
505 			continue;
506 
507 		if (mc->mc_vd == NULL || !vdev_readable(mc->mc_vd)) {
508 			mc->mc_error = SET_ERROR(ENXIO);
509 			mc->mc_tried = 1;	/* don't even try */
510 			mc->mc_skipped = 1;
511 			continue;
512 		}
513 
514 		if (vdev_dtl_contains(mc->mc_vd, DTL_MISSING, txg, 1)) {
515 			mc->mc_error = SET_ERROR(ESTALE);
516 			mc->mc_skipped = 1;
517 			mc->mc_speculative = 1;
518 			continue;
519 		}
520 
521 		mc->mc_load = vdev_mirror_load(mm, mc->mc_vd, mc->mc_offset);
522 		if (mc->mc_load > lowest_load)
523 			continue;
524 
525 		if (mc->mc_load < lowest_load) {
526 			lowest_load = mc->mc_load;
527 			mm->mm_preferred_cnt = 0;
528 		}
529 		mm->mm_preferred[mm->mm_preferred_cnt] = c;
530 		mm->mm_preferred_cnt++;
531 	}
532 
533 	if (mm->mm_preferred_cnt == 1) {
534 		MIRROR_BUMP(vdev_mirror_stat_preferred_found);
535 		return (mm->mm_preferred[0]);
536 	}
537 
538 	if (mm->mm_preferred_cnt > 1) {
539 		MIRROR_BUMP(vdev_mirror_stat_preferred_not_found);
540 		return (vdev_mirror_preferred_child_randomize(zio));
541 	}
542 
543 	/*
544 	 * Every device is either missing or has this txg in its DTL.
545 	 * Look for any child we haven't already tried before giving up.
546 	 */
547 	for (c = 0; c < mm->mm_children; c++) {
548 		if (!mm->mm_child[c].mc_tried)
549 			return (c);
550 	}
551 
552 	/*
553 	 * Every child failed.  There's no place left to look.
554 	 */
555 	return (-1);
556 }
557 
558 static void
559 vdev_mirror_io_start(zio_t *zio)
560 {
561 	mirror_map_t *mm;
562 	mirror_child_t *mc;
563 	int c, children;
564 
565 	mm = vdev_mirror_map_init(zio);
566 
567 	if (mm == NULL) {
568 		ASSERT(!spa_trust_config(zio->io_spa));
569 		ASSERT(zio->io_type == ZIO_TYPE_READ);
570 		zio_execute(zio);
571 		return;
572 	}
573 
574 	if (zio->io_type == ZIO_TYPE_READ) {
575 		if (zio->io_bp != NULL &&
576 		    (zio->io_flags & ZIO_FLAG_SCRUB) && !mm->mm_resilvering) {
577 			/*
578 			 * For scrubbing reads (if we can verify the
579 			 * checksum here, as indicated by io_bp being
580 			 * non-NULL) we need to allocate a read buffer for
581 			 * each child and issue reads to all children.  If
582 			 * any child succeeds, it will copy its data into
583 			 * zio->io_data in vdev_mirror_scrub_done.
584 			 */
585 			for (c = 0; c < mm->mm_children; c++) {
586 				mc = &mm->mm_child[c];
587 				zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
588 				    mc->mc_vd, mc->mc_offset,
589 				    abd_alloc_sametype(zio->io_abd,
590 				    zio->io_size), zio->io_size,
591 				    zio->io_type, zio->io_priority, 0,
592 				    vdev_mirror_scrub_done, mc));
593 			}
594 			zio_execute(zio);
595 			return;
596 		}
597 		/*
598 		 * For normal reads just pick one child.
599 		 */
600 		c = vdev_mirror_child_select(zio);
601 		children = (c >= 0);
602 	} else {
603 		ASSERT(zio->io_type == ZIO_TYPE_WRITE);
604 
605 		/*
606 		 * Writes go to all children.
607 		 */
608 		c = 0;
609 		children = mm->mm_children;
610 	}
611 
612 	while (children--) {
613 		mc = &mm->mm_child[c];
614 		zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
615 		    mc->mc_vd, mc->mc_offset, zio->io_abd, zio->io_size,
616 		    zio->io_type, zio->io_priority, 0,
617 		    vdev_mirror_child_done, mc));
618 		c++;
619 	}
620 
621 	zio_execute(zio);
622 }
623 
624 static int
625 vdev_mirror_worst_error(mirror_map_t *mm)
626 {
627 	int error[2] = { 0, 0 };
628 
629 	for (int c = 0; c < mm->mm_children; c++) {
630 		mirror_child_t *mc = &mm->mm_child[c];
631 		int s = mc->mc_speculative;
632 		error[s] = zio_worst_error(error[s], mc->mc_error);
633 	}
634 
635 	return (error[0] ? error[0] : error[1]);
636 }
637 
638 static void
639 vdev_mirror_io_done(zio_t *zio)
640 {
641 	mirror_map_t *mm = zio->io_vsd;
642 	mirror_child_t *mc;
643 	int c;
644 	int good_copies = 0;
645 	int unexpected_errors = 0;
646 
647 	if (mm == NULL)
648 		return;
649 
650 	for (c = 0; c < mm->mm_children; c++) {
651 		mc = &mm->mm_child[c];
652 
653 		if (mc->mc_error) {
654 			if (!mc->mc_skipped)
655 				unexpected_errors++;
656 		} else if (mc->mc_tried) {
657 			good_copies++;
658 		}
659 	}
660 
661 	if (zio->io_type == ZIO_TYPE_WRITE) {
662 		/*
663 		 * XXX -- for now, treat partial writes as success.
664 		 *
665 		 * Now that we support write reallocation, it would be better
666 		 * to treat partial failure as real failure unless there are
667 		 * no non-degraded top-level vdevs left, and not update DTLs
668 		 * if we intend to reallocate.
669 		 */
670 		/* XXPOLICY */
671 		if (good_copies != mm->mm_children) {
672 			/*
673 			 * Always require at least one good copy.
674 			 *
675 			 * For ditto blocks (io_vd == NULL), require
676 			 * all copies to be good.
677 			 *
678 			 * XXX -- for replacing vdevs, there's no great answer.
679 			 * If the old device is really dead, we may not even
680 			 * be able to access it -- so we only want to
681 			 * require good writes to the new device.  But if
682 			 * the new device turns out to be flaky, we want
683 			 * to be able to detach it -- which requires all
684 			 * writes to the old device to have succeeded.
685 			 */
686 			if (good_copies == 0 || zio->io_vd == NULL)
687 				zio->io_error = vdev_mirror_worst_error(mm);
688 		}
689 		return;
690 	}
691 
692 	ASSERT(zio->io_type == ZIO_TYPE_READ);
693 
694 	/*
695 	 * If we don't have a good copy yet, keep trying other children.
696 	 */
697 	/* XXPOLICY */
698 	if (good_copies == 0 && (c = vdev_mirror_child_select(zio)) != -1) {
699 		ASSERT(c >= 0 && c < mm->mm_children);
700 		mc = &mm->mm_child[c];
701 		zio_vdev_io_redone(zio);
702 		zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
703 		    mc->mc_vd, mc->mc_offset, zio->io_abd, zio->io_size,
704 		    ZIO_TYPE_READ, zio->io_priority, 0,
705 		    vdev_mirror_child_done, mc));
706 		return;
707 	}
708 
709 	/* XXPOLICY */
710 	if (good_copies == 0) {
711 		zio->io_error = vdev_mirror_worst_error(mm);
712 		ASSERT(zio->io_error != 0);
713 	}
714 
715 	if (good_copies && spa_writeable(zio->io_spa) &&
716 	    (unexpected_errors ||
717 	    (zio->io_flags & ZIO_FLAG_RESILVER) ||
718 	    ((zio->io_flags & ZIO_FLAG_SCRUB) && mm->mm_resilvering))) {
719 		/*
720 		 * Use the good data we have in hand to repair damaged children.
721 		 */
722 		for (c = 0; c < mm->mm_children; c++) {
723 			/*
724 			 * Don't rewrite known good children.
725 			 * Not only is it unnecessary, it could
726 			 * actually be harmful: if the system lost
727 			 * power while rewriting the only good copy,
728 			 * there would be no good copies left!
729 			 */
730 			mc = &mm->mm_child[c];
731 
732 			if (mc->mc_error == 0) {
733 				if (mc->mc_tried)
734 					continue;
735 				/*
736 				 * We didn't try this child.  We need to
737 				 * repair it if:
738 				 * 1. it's a scrub (in which case we have
739 				 * tried everything that was healthy)
740 				 *  - or -
741 				 * 2. it's an indirect vdev (in which case
742 				 * it could point to any other vdev, which
743 				 * might have a bad DTL)
744 				 *  - or -
745 				 * 3. the DTL indicates that this data is
746 				 * missing from this vdev
747 				 */
748 				if (!(zio->io_flags & ZIO_FLAG_SCRUB) &&
749 				    mc->mc_vd->vdev_ops != &vdev_indirect_ops &&
750 				    !vdev_dtl_contains(mc->mc_vd, DTL_PARTIAL,
751 				    zio->io_txg, 1))
752 					continue;
753 				mc->mc_error = SET_ERROR(ESTALE);
754 			}
755 
756 			zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
757 			    mc->mc_vd, mc->mc_offset,
758 			    zio->io_abd, zio->io_size,
759 			    ZIO_TYPE_WRITE, ZIO_PRIORITY_ASYNC_WRITE,
760 			    ZIO_FLAG_IO_REPAIR | (unexpected_errors ?
761 			    ZIO_FLAG_SELF_HEAL : 0), NULL, NULL));
762 		}
763 	}
764 }
765 
766 static void
767 vdev_mirror_state_change(vdev_t *vd, int faulted, int degraded)
768 {
769 	if (faulted == vd->vdev_children) {
770 		if (vdev_children_are_offline(vd)) {
771 			vdev_set_state(vd, B_FALSE, VDEV_STATE_OFFLINE,
772 			    VDEV_AUX_CHILDREN_OFFLINE);
773 		} else {
774 			vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
775 			    VDEV_AUX_NO_REPLICAS);
776 		}
777 	} else if (degraded + faulted != 0) {
778 		vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, VDEV_AUX_NONE);
779 	} else {
780 		vdev_set_state(vd, B_FALSE, VDEV_STATE_HEALTHY, VDEV_AUX_NONE);
781 	}
782 }
783 
784 vdev_ops_t vdev_mirror_ops = {
785 	.vdev_op_open = vdev_mirror_open,
786 	.vdev_op_close = vdev_mirror_close,
787 	.vdev_op_asize = vdev_default_asize,
788 	.vdev_op_io_start = vdev_mirror_io_start,
789 	.vdev_op_io_done = vdev_mirror_io_done,
790 	.vdev_op_state_change = vdev_mirror_state_change,
791 	.vdev_op_need_resilver = NULL,
792 	.vdev_op_hold = NULL,
793 	.vdev_op_rele = NULL,
794 	.vdev_op_remap = NULL,
795 	.vdev_op_xlate = vdev_default_xlate,
796 	.vdev_op_type = VDEV_TYPE_MIRROR,	/* name of this vdev type */
797 	.vdev_op_leaf = B_FALSE			/* not a leaf vdev */
798 };
799 
800 vdev_ops_t vdev_replacing_ops = {
801 	.vdev_op_open = vdev_mirror_open,
802 	.vdev_op_close = vdev_mirror_close,
803 	.vdev_op_asize = vdev_default_asize,
804 	.vdev_op_io_start = vdev_mirror_io_start,
805 	.vdev_op_io_done = vdev_mirror_io_done,
806 	.vdev_op_state_change = vdev_mirror_state_change,
807 	.vdev_op_need_resilver = NULL,
808 	.vdev_op_hold = NULL,
809 	.vdev_op_rele = NULL,
810 	.vdev_op_remap = NULL,
811 	.vdev_op_xlate = vdev_default_xlate,
812 	.vdev_op_type = VDEV_TYPE_REPLACING,	/* name of this vdev type */
813 	.vdev_op_leaf = B_FALSE			/* not a leaf vdev */
814 };
815 
816 vdev_ops_t vdev_spare_ops = {
817 	.vdev_op_open = vdev_mirror_open,
818 	.vdev_op_close = vdev_mirror_close,
819 	.vdev_op_asize = vdev_default_asize,
820 	.vdev_op_io_start = vdev_mirror_io_start,
821 	.vdev_op_io_done = vdev_mirror_io_done,
822 	.vdev_op_state_change = vdev_mirror_state_change,
823 	.vdev_op_need_resilver = NULL,
824 	.vdev_op_hold = NULL,
825 	.vdev_op_rele = NULL,
826 	.vdev_op_remap = NULL,
827 	.vdev_op_xlate = vdev_default_xlate,
828 	.vdev_op_type = VDEV_TYPE_SPARE,	/* name of this vdev type */
829 	.vdev_op_leaf = B_FALSE			/* not a leaf vdev */
830 };
831