1168404Spjd/*
2168404Spjd * CDDL HEADER START
3168404Spjd *
4168404Spjd * The contents of this file are subject to the terms of the
5168404Spjd * Common Development and Distribution License (the "License").
6168404Spjd * You may not use this file except in compliance with the License.
7168404Spjd *
8168404Spjd * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9168404Spjd * or http://www.opensolaris.org/os/licensing.
10168404Spjd * See the License for the specific language governing permissions
11168404Spjd * and limitations under the License.
12168404Spjd *
13168404Spjd * When distributing Covered Code, include this CDDL HEADER in each
14168404Spjd * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15168404Spjd * If applicable, add the following below this CDDL HEADER, with the
16168404Spjd * fields enclosed by brackets "[]" replaced with your own identifying
17168404Spjd * information: Portions Copyright [yyyy] [name of copyright owner]
18168404Spjd *
19168404Spjd * CDDL HEADER END
20168404Spjd */
21168404Spjd/*
22219089Spjd * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
23168404Spjd * Use is subject to license terms.
24168404Spjd */
25168404Spjd
26236155Smm/*
27274304Sdelphij * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
28236155Smm */
29236155Smm
30168404Spjd#include <sys/zfs_context.h>
31168404Spjd#include <sys/spa.h>
32168404Spjd#include <sys/vdev_impl.h>
33168404Spjd#include <sys/zio.h>
34168404Spjd#include <sys/fs/zfs.h>
35168404Spjd
36168404Spjd/*
37168404Spjd * Virtual device vector for mirroring.
38168404Spjd */
39168404Spjd
40168404Spjdtypedef struct mirror_child {
41168404Spjd	vdev_t		*mc_vd;
42168404Spjd	uint64_t	mc_offset;
43168404Spjd	int		mc_error;
44256956Ssmh	int		mc_load;
45185029Spjd	uint8_t		mc_tried;
46185029Spjd	uint8_t		mc_skipped;
47185029Spjd	uint8_t		mc_speculative;
48168404Spjd} mirror_child_t;
49168404Spjd
50168404Spjdtypedef struct mirror_map {
51256956Ssmh	int		*mm_preferred;
52256956Ssmh	int		mm_preferred_cnt;
53168404Spjd	int		mm_children;
54256956Ssmh	boolean_t	mm_replacing;
55256956Ssmh	boolean_t	mm_root;
56256956Ssmh	mirror_child_t	mm_child[];
57168404Spjd} mirror_map_t;
58168404Spjd
59256956Ssmhstatic int vdev_mirror_shift = 21;
60168404Spjd
61256956SsmhSYSCTL_DECL(_vfs_zfs_vdev);
62256956Ssmhstatic SYSCTL_NODE(_vfs_zfs_vdev, OID_AUTO, mirror, CTLFLAG_RD, 0,
63256956Ssmh    "ZFS VDEV Mirror");
64256956Ssmh
65256956Ssmh/*
66256956Ssmh * The load configuration settings below are tuned by default for
67256956Ssmh * the case where all devices are of the same rotational type.
68256956Ssmh *
69256956Ssmh * If there is a mixture of rotating and non-rotating media, setting
70256956Ssmh * non_rotating_seek_inc to 0 may well provide better results as it
71256956Ssmh * will direct more reads to the non-rotating vdevs which are more
72256956Ssmh * likely to have a higher performance.
73256956Ssmh */
74256956Ssmh
75256956Ssmh/* Rotating media load calculation configuration. */
76256956Ssmhstatic int rotating_inc = 0;
77267992ShselaskySYSCTL_INT(_vfs_zfs_vdev_mirror, OID_AUTO, rotating_inc, CTLFLAG_RWTUN,
78256956Ssmh    &rotating_inc, 0, "Rotating media load increment for non-seeking I/O's");
79256956Ssmh
80256956Ssmhstatic int rotating_seek_inc = 5;
81267992ShselaskySYSCTL_INT(_vfs_zfs_vdev_mirror, OID_AUTO, rotating_seek_inc, CTLFLAG_RWTUN,
82256956Ssmh    &rotating_seek_inc, 0, "Rotating media load increment for seeking I/O's");
83256956Ssmh
84256956Ssmhstatic int rotating_seek_offset = 1 * 1024 * 1024;
85267992ShselaskySYSCTL_INT(_vfs_zfs_vdev_mirror, OID_AUTO, rotating_seek_offset, CTLFLAG_RWTUN,
86256956Ssmh    &rotating_seek_offset, 0, "Offset in bytes from the last I/O which "
87256956Ssmh    "triggers a reduced rotating media seek increment");
88256956Ssmh
89256956Ssmh/* Non-rotating media load calculation configuration. */
90256956Ssmhstatic int non_rotating_inc = 0;
91267992ShselaskySYSCTL_INT(_vfs_zfs_vdev_mirror, OID_AUTO, non_rotating_inc, CTLFLAG_RWTUN,
92256956Ssmh    &non_rotating_inc, 0,
93256956Ssmh    "Non-rotating media load increment for non-seeking I/O's");
94256956Ssmh
95256956Ssmhstatic int non_rotating_seek_inc = 1;
96267992ShselaskySYSCTL_INT(_vfs_zfs_vdev_mirror, OID_AUTO, non_rotating_seek_inc, CTLFLAG_RWTUN,
97256956Ssmh    &non_rotating_seek_inc, 0,
98256956Ssmh    "Non-rotating media load increment for seeking I/O's");
99256956Ssmh
100256956Ssmh
101256956Ssmhstatic inline size_t
102256956Ssmhvdev_mirror_map_size(int children)
103256956Ssmh{
104256956Ssmh	return (offsetof(mirror_map_t, mm_child[children]) +
105256956Ssmh	    sizeof(int) * children);
106256956Ssmh}
107256956Ssmh
108256956Ssmhstatic inline mirror_map_t *
109256956Ssmhvdev_mirror_map_alloc(int children, boolean_t replacing, boolean_t root)
110256956Ssmh{
111256956Ssmh	mirror_map_t *mm;
112256956Ssmh
113256956Ssmh	mm = kmem_zalloc(vdev_mirror_map_size(children), KM_SLEEP);
114256956Ssmh	mm->mm_children = children;
115256956Ssmh	mm->mm_replacing = replacing;
116256956Ssmh	mm->mm_root = root;
117256956Ssmh	mm->mm_preferred = (int *)((uintptr_t)mm +
118256956Ssmh	    offsetof(mirror_map_t, mm_child[children]));
119256956Ssmh
120256956Ssmh	return mm;
121256956Ssmh}
122256956Ssmh
123185029Spjdstatic void
124185029Spjdvdev_mirror_map_free(zio_t *zio)
125185029Spjd{
126185029Spjd	mirror_map_t *mm = zio->io_vsd;
127185029Spjd
128256956Ssmh	kmem_free(mm, vdev_mirror_map_size(mm->mm_children));
129185029Spjd}
130185029Spjd
131219089Spjdstatic const zio_vsd_ops_t vdev_mirror_vsd_ops = {
132219089Spjd	vdev_mirror_map_free,
133219089Spjd	zio_vsd_default_cksum_report
134219089Spjd};
135219089Spjd
136256956Ssmhstatic int
137256956Ssmhvdev_mirror_load(mirror_map_t *mm, vdev_t *vd, uint64_t zio_offset)
138256956Ssmh{
139256956Ssmh	uint64_t lastoffset;
140256956Ssmh	int load;
141256956Ssmh
142256956Ssmh	/* All DVAs have equal weight at the root. */
143256956Ssmh	if (mm->mm_root)
144256956Ssmh		return (INT_MAX);
145256956Ssmh
146256956Ssmh	/*
147256956Ssmh	 * We don't return INT_MAX if the device is resilvering i.e.
148256956Ssmh	 * vdev_resilver_txg != 0 as when tested performance was slightly
149256956Ssmh	 * worse overall when resilvering with compared to without.
150256956Ssmh	 */
151256956Ssmh
152256956Ssmh	/* Standard load based on pending queue length. */
153256956Ssmh	load = vdev_queue_length(vd);
154256956Ssmh	lastoffset = vdev_queue_lastoffset(vd);
155256956Ssmh
156256956Ssmh	if (vd->vdev_rotation_rate == VDEV_RATE_NON_ROTATING) {
157256956Ssmh		/* Non-rotating media. */
158256956Ssmh		if (lastoffset == zio_offset)
159256956Ssmh			return (load + non_rotating_inc);
160256956Ssmh
161256956Ssmh		/*
162256956Ssmh		 * Apply a seek penalty even for non-rotating devices as
163256956Ssmh		 * sequential I/O'a can be aggregated into fewer operations
164256956Ssmh		 * on the device, thus avoiding unnecessary per-command
165256956Ssmh		 * overhead and boosting performance.
166256956Ssmh		 */
167256956Ssmh		return (load + non_rotating_seek_inc);
168256956Ssmh	}
169256956Ssmh
170256956Ssmh	/* Rotating media I/O's which directly follow the last I/O. */
171256956Ssmh	if (lastoffset == zio_offset)
172256956Ssmh		return (load + rotating_inc);
173256956Ssmh
174256956Ssmh	/*
175256956Ssmh	 * Apply half the seek increment to I/O's within seek offset
176256956Ssmh	 * of the last I/O queued to this vdev as they should incure less
177256956Ssmh	 * of a seek increment.
178256956Ssmh	 */
179256956Ssmh	if (ABS(lastoffset - zio_offset) < rotating_seek_offset)
180256956Ssmh		return (load + (rotating_seek_inc / 2));
181256956Ssmh
182256956Ssmh	/* Apply the full seek increment to all other I/O's. */
183256956Ssmh	return (load + rotating_seek_inc);
184256956Ssmh}
185256956Ssmh
186256956Ssmh
187168404Spjdstatic mirror_map_t *
188256956Ssmhvdev_mirror_map_init(zio_t *zio)
189168404Spjd{
190168404Spjd	mirror_map_t *mm = NULL;
191168404Spjd	mirror_child_t *mc;
192168404Spjd	vdev_t *vd = zio->io_vd;
193256956Ssmh	int c;
194168404Spjd
195168404Spjd	if (vd == NULL) {
196168404Spjd		dva_t *dva = zio->io_bp->blk_dva;
197168404Spjd		spa_t *spa = zio->io_spa;
198168404Spjd
199256956Ssmh		mm = vdev_mirror_map_alloc(BP_GET_NDVAS(zio->io_bp), B_FALSE,
200256956Ssmh		    B_TRUE);
201168404Spjd		for (c = 0; c < mm->mm_children; c++) {
202168404Spjd			mc = &mm->mm_child[c];
203168404Spjd			mc->mc_vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[c]));
204168404Spjd			mc->mc_offset = DVA_GET_OFFSET(&dva[c]);
205168404Spjd		}
206168404Spjd	} else {
207256956Ssmh		mm = vdev_mirror_map_alloc(vd->vdev_children,
208256956Ssmh		    (vd->vdev_ops == &vdev_replacing_ops ||
209256956Ssmh                    vd->vdev_ops == &vdev_spare_ops), B_FALSE);
210168404Spjd		for (c = 0; c < mm->mm_children; c++) {
211168404Spjd			mc = &mm->mm_child[c];
212168404Spjd			mc->mc_vd = vd->vdev_child[c];
213168404Spjd			mc->mc_offset = zio->io_offset;
214168404Spjd		}
215168404Spjd	}
216168404Spjd
217168404Spjd	zio->io_vsd = mm;
218219089Spjd	zio->io_vsd_ops = &vdev_mirror_vsd_ops;
219168404Spjd	return (mm);
220168404Spjd}
221168404Spjd
222168404Spjdstatic int
223236155Smmvdev_mirror_open(vdev_t *vd, uint64_t *asize, uint64_t *max_asize,
224254591Sgibbs    uint64_t *logical_ashift, uint64_t *physical_ashift)
225168404Spjd{
226168404Spjd	int numerrors = 0;
227219089Spjd	int lasterror = 0;
228168404Spjd
229168404Spjd	if (vd->vdev_children == 0) {
230168404Spjd		vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
231249195Smm		return (SET_ERROR(EINVAL));
232168404Spjd	}
233168404Spjd
234219089Spjd	vdev_open_children(vd);
235168404Spjd
236219089Spjd	for (int c = 0; c < vd->vdev_children; c++) {
237219089Spjd		vdev_t *cvd = vd->vdev_child[c];
238219089Spjd
239219089Spjd		if (cvd->vdev_open_error) {
240219089Spjd			lasterror = cvd->vdev_open_error;
241168404Spjd			numerrors++;
242168404Spjd			continue;
243168404Spjd		}
244168404Spjd
245168404Spjd		*asize = MIN(*asize - 1, cvd->vdev_asize - 1) + 1;
246236155Smm		*max_asize = MIN(*max_asize - 1, cvd->vdev_max_asize - 1) + 1;
247254591Sgibbs		*logical_ashift = MAX(*logical_ashift, cvd->vdev_ashift);
248254591Sgibbs		*physical_ashift = MAX(*physical_ashift,
249254591Sgibbs		    cvd->vdev_physical_ashift);
250168404Spjd	}
251168404Spjd
252168404Spjd	if (numerrors == vd->vdev_children) {
253168404Spjd		vd->vdev_stat.vs_aux = VDEV_AUX_NO_REPLICAS;
254168404Spjd		return (lasterror);
255168404Spjd	}
256168404Spjd
257168404Spjd	return (0);
258168404Spjd}
259168404Spjd
260168404Spjdstatic void
261168404Spjdvdev_mirror_close(vdev_t *vd)
262168404Spjd{
263219089Spjd	for (int c = 0; c < vd->vdev_children; c++)
264168404Spjd		vdev_close(vd->vdev_child[c]);
265168404Spjd}
266168404Spjd
267168404Spjdstatic void
268168404Spjdvdev_mirror_child_done(zio_t *zio)
269168404Spjd{
270168404Spjd	mirror_child_t *mc = zio->io_private;
271168404Spjd
272168404Spjd	mc->mc_error = zio->io_error;
273168404Spjd	mc->mc_tried = 1;
274168404Spjd	mc->mc_skipped = 0;
275168404Spjd}
276168404Spjd
277168404Spjdstatic void
278168404Spjdvdev_mirror_scrub_done(zio_t *zio)
279168404Spjd{
280168404Spjd	mirror_child_t *mc = zio->io_private;
281168404Spjd
282168404Spjd	if (zio->io_error == 0) {
283209962Smm		zio_t *pio;
284209962Smm
285209962Smm		mutex_enter(&zio->io_lock);
286209962Smm		while ((pio = zio_walk_parents(zio)) != NULL) {
287209962Smm			mutex_enter(&pio->io_lock);
288209962Smm			ASSERT3U(zio->io_size, >=, pio->io_size);
289209962Smm			bcopy(zio->io_data, pio->io_data, pio->io_size);
290209962Smm			mutex_exit(&pio->io_lock);
291209962Smm		}
292209962Smm		mutex_exit(&zio->io_lock);
293168404Spjd	}
294168404Spjd
295168404Spjd	zio_buf_free(zio->io_data, zio->io_size);
296168404Spjd
297168404Spjd	mc->mc_error = zio->io_error;
298168404Spjd	mc->mc_tried = 1;
299168404Spjd	mc->mc_skipped = 0;
300168404Spjd}
301168404Spjd
302168404Spjd/*
303256956Ssmh * Check the other, lower-index DVAs to see if they're on the same
304256956Ssmh * vdev as the child we picked.  If they are, use them since they
305256956Ssmh * are likely to have been allocated from the primary metaslab in
306256956Ssmh * use at the time, and hence are more likely to have locality with
307256956Ssmh * single-copy data.
308256956Ssmh */
309256956Ssmhstatic int
310260713Savgvdev_mirror_dva_select(zio_t *zio, int p)
311256956Ssmh{
312256956Ssmh	dva_t *dva = zio->io_bp->blk_dva;
313256956Ssmh	mirror_map_t *mm = zio->io_vsd;
314260713Savg	int preferred;
315256956Ssmh	int c;
316256956Ssmh
317260713Savg	preferred = mm->mm_preferred[p];
318260713Savg	for (p-- ; p >= 0; p--) {
319260713Savg		c = mm->mm_preferred[p];
320256956Ssmh		if (DVA_GET_VDEV(&dva[c]) == DVA_GET_VDEV(&dva[preferred]))
321256956Ssmh			preferred = c;
322256956Ssmh	}
323256956Ssmh	return (preferred);
324256956Ssmh}
325256956Ssmh
326256956Ssmhstatic int
327256956Ssmhvdev_mirror_preferred_child_randomize(zio_t *zio)
328256956Ssmh{
329256956Ssmh	mirror_map_t *mm = zio->io_vsd;
330256956Ssmh	int p;
331256956Ssmh
332256956Ssmh	if (mm->mm_root) {
333256956Ssmh		p = spa_get_random(mm->mm_preferred_cnt);
334260713Savg		return (vdev_mirror_dva_select(zio, p));
335256956Ssmh	}
336256956Ssmh
337256956Ssmh	/*
338256956Ssmh	 * To ensure we don't always favour the first matching vdev,
339256956Ssmh	 * which could lead to wear leveling issues on SSD's, we
340256956Ssmh	 * use the I/O offset as a pseudo random seed into the vdevs
341256956Ssmh	 * which have the lowest load.
342256956Ssmh	 */
343256956Ssmh	p = (zio->io_offset >> vdev_mirror_shift) % mm->mm_preferred_cnt;
344256956Ssmh	return (mm->mm_preferred[p]);
345256956Ssmh}
346256956Ssmh
347256956Ssmh/*
348256956Ssmh * Try to find a vdev whose DTL doesn't contain the block we want to read
349256956Ssmh * prefering vdevs based on determined load.
350256956Ssmh *
351168404Spjd * If we can't, try the read on any vdev we haven't already tried.
352168404Spjd */
353168404Spjdstatic int
354168404Spjdvdev_mirror_child_select(zio_t *zio)
355168404Spjd{
356168404Spjd	mirror_map_t *mm = zio->io_vsd;
357168404Spjd	uint64_t txg = zio->io_txg;
358256956Ssmh	int c, lowest_load;
359168404Spjd
360219089Spjd	ASSERT(zio->io_bp == NULL || BP_PHYSICAL_BIRTH(zio->io_bp) == txg);
361168404Spjd
362256956Ssmh	lowest_load = INT_MAX;
363256956Ssmh	mm->mm_preferred_cnt = 0;
364256956Ssmh	for (c = 0; c < mm->mm_children; c++) {
365256956Ssmh		mirror_child_t *mc;
366256956Ssmh
367168404Spjd		mc = &mm->mm_child[c];
368168404Spjd		if (mc->mc_tried || mc->mc_skipped)
369168404Spjd			continue;
370256956Ssmh
371185029Spjd		if (!vdev_readable(mc->mc_vd)) {
372249195Smm			mc->mc_error = SET_ERROR(ENXIO);
373168404Spjd			mc->mc_tried = 1;	/* don't even try */
374168404Spjd			mc->mc_skipped = 1;
375168404Spjd			continue;
376168404Spjd		}
377256956Ssmh
378256956Ssmh		if (vdev_dtl_contains(mc->mc_vd, DTL_MISSING, txg, 1)) {
379256956Ssmh			mc->mc_error = SET_ERROR(ESTALE);
380256956Ssmh			mc->mc_skipped = 1;
381256956Ssmh			mc->mc_speculative = 1;
382256956Ssmh			continue;
383256956Ssmh		}
384256956Ssmh
385256956Ssmh		mc->mc_load = vdev_mirror_load(mm, mc->mc_vd, mc->mc_offset);
386256956Ssmh		if (mc->mc_load > lowest_load)
387256956Ssmh			continue;
388256956Ssmh
389256956Ssmh		if (mc->mc_load < lowest_load) {
390256956Ssmh			lowest_load = mc->mc_load;
391256956Ssmh			mm->mm_preferred_cnt = 0;
392256956Ssmh		}
393256956Ssmh		mm->mm_preferred[mm->mm_preferred_cnt] = c;
394256956Ssmh		mm->mm_preferred_cnt++;
395168404Spjd	}
396168404Spjd
397256956Ssmh	if (mm->mm_preferred_cnt == 1) {
398256956Ssmh		vdev_queue_register_lastoffset(
399256956Ssmh		    mm->mm_child[mm->mm_preferred[0]].mc_vd, zio);
400256956Ssmh		return (mm->mm_preferred[0]);
401256956Ssmh	}
402256956Ssmh
403256956Ssmh	if (mm->mm_preferred_cnt > 1) {
404256956Ssmh		int c = vdev_mirror_preferred_child_randomize(zio);
405256956Ssmh
406256956Ssmh		vdev_queue_register_lastoffset(mm->mm_child[c].mc_vd, zio);
407256956Ssmh		return (c);
408256956Ssmh	}
409256956Ssmh
410168404Spjd	/*
411168404Spjd	 * Every device is either missing or has this txg in its DTL.
412168404Spjd	 * Look for any child we haven't already tried before giving up.
413168404Spjd	 */
414256956Ssmh	for (c = 0; c < mm->mm_children; c++) {
415256956Ssmh		if (!mm->mm_child[c].mc_tried) {
416256956Ssmh			vdev_queue_register_lastoffset(mm->mm_child[c].mc_vd,
417256956Ssmh			    zio);
418168404Spjd			return (c);
419256956Ssmh		}
420256956Ssmh	}
421168404Spjd
422168404Spjd	/*
423168404Spjd	 * Every child failed.  There's no place left to look.
424168404Spjd	 */
425168404Spjd	return (-1);
426168404Spjd}
427168404Spjd
428274304Sdelphijstatic void
429168404Spjdvdev_mirror_io_start(zio_t *zio)
430168404Spjd{
431168404Spjd	mirror_map_t *mm;
432168404Spjd	mirror_child_t *mc;
433168404Spjd	int c, children;
434168404Spjd
435256956Ssmh	mm = vdev_mirror_map_init(zio);
436168404Spjd
437168404Spjd	if (zio->io_type == ZIO_TYPE_READ) {
438277096Smav		if ((zio->io_flags & ZIO_FLAG_SCRUB) && !mm->mm_replacing &&
439277096Smav		    mm->mm_children > 1) {
440168404Spjd			/*
441168404Spjd			 * For scrubbing reads we need to allocate a read
442168404Spjd			 * buffer for each child and issue reads to all
443168404Spjd			 * children.  If any child succeeds, it will copy its
444168404Spjd			 * data into zio->io_data in vdev_mirror_scrub_done.
445168404Spjd			 */
446168404Spjd			for (c = 0; c < mm->mm_children; c++) {
447168404Spjd				mc = &mm->mm_child[c];
448168404Spjd				zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
449168404Spjd				    mc->mc_vd, mc->mc_offset,
450168404Spjd				    zio_buf_alloc(zio->io_size), zio->io_size,
451185029Spjd				    zio->io_type, zio->io_priority, 0,
452168404Spjd				    vdev_mirror_scrub_done, mc));
453168404Spjd			}
454274304Sdelphij			zio_execute(zio);
455274304Sdelphij			return;
456168404Spjd		}
457168404Spjd		/*
458168404Spjd		 * For normal reads just pick one child.
459168404Spjd		 */
460168404Spjd		c = vdev_mirror_child_select(zio);
461168404Spjd		children = (c >= 0);
462168404Spjd	} else {
463240868Spjd		ASSERT(zio->io_type == ZIO_TYPE_WRITE ||
464240868Spjd		    zio->io_type == ZIO_TYPE_FREE);
465168404Spjd
466168404Spjd		/*
467240868Spjd		 * Writes and frees go to all children.
468168404Spjd		 */
469209962Smm		c = 0;
470209962Smm		children = mm->mm_children;
471168404Spjd	}
472168404Spjd
473168404Spjd	while (children--) {
474168404Spjd		mc = &mm->mm_child[c];
475168404Spjd		zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
476185029Spjd		    mc->mc_vd, mc->mc_offset, zio->io_data, zio->io_size,
477185029Spjd		    zio->io_type, zio->io_priority, 0,
478185029Spjd		    vdev_mirror_child_done, mc));
479168404Spjd		c++;
480168404Spjd	}
481168404Spjd
482274304Sdelphij	zio_execute(zio);
483168404Spjd}
484168404Spjd
485185029Spjdstatic int
486185029Spjdvdev_mirror_worst_error(mirror_map_t *mm)
487185029Spjd{
488185029Spjd	int error[2] = { 0, 0 };
489185029Spjd
490185029Spjd	for (int c = 0; c < mm->mm_children; c++) {
491185029Spjd		mirror_child_t *mc = &mm->mm_child[c];
492185029Spjd		int s = mc->mc_speculative;
493185029Spjd		error[s] = zio_worst_error(error[s], mc->mc_error);
494185029Spjd	}
495185029Spjd
496185029Spjd	return (error[0] ? error[0] : error[1]);
497185029Spjd}
498185029Spjd
499168404Spjdstatic void
500168404Spjdvdev_mirror_io_done(zio_t *zio)
501168404Spjd{
502168404Spjd	mirror_map_t *mm = zio->io_vsd;
503168404Spjd	mirror_child_t *mc;
504168404Spjd	int c;
505168404Spjd	int good_copies = 0;
506168404Spjd	int unexpected_errors = 0;
507168404Spjd
508168404Spjd	for (c = 0; c < mm->mm_children; c++) {
509168404Spjd		mc = &mm->mm_child[c];
510168404Spjd
511168404Spjd		if (mc->mc_error) {
512168404Spjd			if (!mc->mc_skipped)
513168404Spjd				unexpected_errors++;
514185029Spjd		} else if (mc->mc_tried) {
515185029Spjd			good_copies++;
516168404Spjd		}
517168404Spjd	}
518168404Spjd
519168404Spjd	if (zio->io_type == ZIO_TYPE_WRITE) {
520168404Spjd		/*
521168404Spjd		 * XXX -- for now, treat partial writes as success.
522185029Spjd		 *
523185029Spjd		 * Now that we support write reallocation, it would be better
524185029Spjd		 * to treat partial failure as real failure unless there are
525185029Spjd		 * no non-degraded top-level vdevs left, and not update DTLs
526185029Spjd		 * if we intend to reallocate.
527168404Spjd		 */
528168404Spjd		/* XXPOLICY */
529185029Spjd		if (good_copies != mm->mm_children) {
530185029Spjd			/*
531185029Spjd			 * Always require at least one good copy.
532185029Spjd			 *
533185029Spjd			 * For ditto blocks (io_vd == NULL), require
534185029Spjd			 * all copies to be good.
535185029Spjd			 *
536185029Spjd			 * XXX -- for replacing vdevs, there's no great answer.
537185029Spjd			 * If the old device is really dead, we may not even
538185029Spjd			 * be able to access it -- so we only want to
539185029Spjd			 * require good writes to the new device.  But if
540185029Spjd			 * the new device turns out to be flaky, we want
541185029Spjd			 * to be able to detach it -- which requires all
542185029Spjd			 * writes to the old device to have succeeded.
543185029Spjd			 */
544185029Spjd			if (good_copies == 0 || zio->io_vd == NULL)
545185029Spjd				zio->io_error = vdev_mirror_worst_error(mm);
546185029Spjd		}
547168404Spjd		return;
548240868Spjd	} else if (zio->io_type == ZIO_TYPE_FREE) {
549240868Spjd		return;
550168404Spjd	}
551168404Spjd
552168404Spjd	ASSERT(zio->io_type == ZIO_TYPE_READ);
553168404Spjd
554168404Spjd	/*
555168404Spjd	 * If we don't have a good copy yet, keep trying other children.
556168404Spjd	 */
557168404Spjd	/* XXPOLICY */
558168404Spjd	if (good_copies == 0 && (c = vdev_mirror_child_select(zio)) != -1) {
559168404Spjd		ASSERT(c >= 0 && c < mm->mm_children);
560168404Spjd		mc = &mm->mm_child[c];
561168404Spjd		zio_vdev_io_redone(zio);
562168404Spjd		zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
563168404Spjd		    mc->mc_vd, mc->mc_offset, zio->io_data, zio->io_size,
564185029Spjd		    ZIO_TYPE_READ, zio->io_priority, 0,
565168404Spjd		    vdev_mirror_child_done, mc));
566168404Spjd		return;
567168404Spjd	}
568168404Spjd
569168404Spjd	/* XXPOLICY */
570185029Spjd	if (good_copies == 0) {
571185029Spjd		zio->io_error = vdev_mirror_worst_error(mm);
572168404Spjd		ASSERT(zio->io_error != 0);
573185029Spjd	}
574168404Spjd
575209962Smm	if (good_copies && spa_writeable(zio->io_spa) &&
576168404Spjd	    (unexpected_errors ||
577168404Spjd	    (zio->io_flags & ZIO_FLAG_RESILVER) ||
578168404Spjd	    ((zio->io_flags & ZIO_FLAG_SCRUB) && mm->mm_replacing))) {
579168404Spjd		/*
580168404Spjd		 * Use the good data we have in hand to repair damaged children.
581168404Spjd		 */
582168404Spjd		for (c = 0; c < mm->mm_children; c++) {
583168404Spjd			/*
584168404Spjd			 * Don't rewrite known good children.
585168404Spjd			 * Not only is it unnecessary, it could
586168404Spjd			 * actually be harmful: if the system lost
587168404Spjd			 * power while rewriting the only good copy,
588168404Spjd			 * there would be no good copies left!
589168404Spjd			 */
590168404Spjd			mc = &mm->mm_child[c];
591168404Spjd
592168404Spjd			if (mc->mc_error == 0) {
593168404Spjd				if (mc->mc_tried)
594168404Spjd					continue;
595168404Spjd				if (!(zio->io_flags & ZIO_FLAG_SCRUB) &&
596209962Smm				    !vdev_dtl_contains(mc->mc_vd, DTL_PARTIAL,
597168404Spjd				    zio->io_txg, 1))
598168404Spjd					continue;
599249195Smm				mc->mc_error = SET_ERROR(ESTALE);
600168404Spjd			}
601168404Spjd
602185029Spjd			zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
603185029Spjd			    mc->mc_vd, mc->mc_offset,
604185029Spjd			    zio->io_data, zio->io_size,
605258632Savg			    ZIO_TYPE_WRITE, ZIO_PRIORITY_ASYNC_WRITE,
606209962Smm			    ZIO_FLAG_IO_REPAIR | (unexpected_errors ?
607209962Smm			    ZIO_FLAG_SELF_HEAL : 0), NULL, NULL));
608168404Spjd		}
609168404Spjd	}
610168404Spjd}
611168404Spjd
612168404Spjdstatic void
613168404Spjdvdev_mirror_state_change(vdev_t *vd, int faulted, int degraded)
614168404Spjd{
615168404Spjd	if (faulted == vd->vdev_children)
616168404Spjd		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
617168404Spjd		    VDEV_AUX_NO_REPLICAS);
618168404Spjd	else if (degraded + faulted != 0)
619168404Spjd		vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, VDEV_AUX_NONE);
620168404Spjd	else
621168404Spjd		vdev_set_state(vd, B_FALSE, VDEV_STATE_HEALTHY, VDEV_AUX_NONE);
622168404Spjd}
623168404Spjd
624168404Spjdvdev_ops_t vdev_mirror_ops = {
625168404Spjd	vdev_mirror_open,
626168404Spjd	vdev_mirror_close,
627168404Spjd	vdev_default_asize,
628168404Spjd	vdev_mirror_io_start,
629168404Spjd	vdev_mirror_io_done,
630168404Spjd	vdev_mirror_state_change,
631219089Spjd	NULL,
632219089Spjd	NULL,
633168404Spjd	VDEV_TYPE_MIRROR,	/* name of this vdev type */
634168404Spjd	B_FALSE			/* not a leaf vdev */
635168404Spjd};
636168404Spjd
637168404Spjdvdev_ops_t vdev_replacing_ops = {
638168404Spjd	vdev_mirror_open,
639168404Spjd	vdev_mirror_close,
640168404Spjd	vdev_default_asize,
641168404Spjd	vdev_mirror_io_start,
642168404Spjd	vdev_mirror_io_done,
643168404Spjd	vdev_mirror_state_change,
644219089Spjd	NULL,
645219089Spjd	NULL,
646168404Spjd	VDEV_TYPE_REPLACING,	/* name of this vdev type */
647168404Spjd	B_FALSE			/* not a leaf vdev */
648168404Spjd};
649168404Spjd
650168404Spjdvdev_ops_t vdev_spare_ops = {
651168404Spjd	vdev_mirror_open,
652168404Spjd	vdev_mirror_close,
653168404Spjd	vdev_default_asize,
654168404Spjd	vdev_mirror_io_start,
655168404Spjd	vdev_mirror_io_done,
656168404Spjd	vdev_mirror_state_change,
657219089Spjd	NULL,
658219089Spjd	NULL,
659168404Spjd	VDEV_TYPE_SPARE,	/* name of this vdev type */
660168404Spjd	B_FALSE			/* not a leaf vdev */
661168404Spjd};
662