vdev_mirror.c revision 256956
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/*
27249195Smm * Copyright (c) 2013 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;
77256956SsmhTUNABLE_INT("vfs.zfs.vdev.mirror.rotating_inc", &rotating_inc);
78256956SsmhSYSCTL_INT(_vfs_zfs_vdev_mirror, OID_AUTO, rotating_inc, CTLFLAG_RW,
79256956Ssmh    &rotating_inc, 0, "Rotating media load increment for non-seeking I/O's");
80256956Ssmh
81256956Ssmhstatic int rotating_seek_inc = 5;
82256956SsmhTUNABLE_INT("vfs.zfs.vdev.mirror.rotating_seek_inc", &rotating_seek_inc);
83256956SsmhSYSCTL_INT(_vfs_zfs_vdev_mirror, OID_AUTO, rotating_seek_inc, CTLFLAG_RW,
84256956Ssmh    &rotating_seek_inc, 0, "Rotating media load increment for seeking I/O's");
85256956Ssmh
86256956Ssmhstatic int rotating_seek_offset = 1 * 1024 * 1024;
87256956SsmhTUNABLE_INT("vfs.zfs.vdev.mirror.rotating_seek_offset", &rotating_seek_offset);
88256956SsmhSYSCTL_INT(_vfs_zfs_vdev_mirror, OID_AUTO, rotating_seek_offset, CTLFLAG_RW,
89256956Ssmh    &rotating_seek_offset, 0, "Offset in bytes from the last I/O which "
90256956Ssmh    "triggers a reduced rotating media seek increment");
91256956Ssmh
92256956Ssmh/* Non-rotating media load calculation configuration. */
93256956Ssmhstatic int non_rotating_inc = 0;
94256956SsmhTUNABLE_INT("vfs.zfs.vdev.mirror.non_rotating_inc", &non_rotating_inc);
95256956SsmhSYSCTL_INT(_vfs_zfs_vdev_mirror, OID_AUTO, non_rotating_inc, CTLFLAG_RW,
96256956Ssmh    &non_rotating_inc, 0,
97256956Ssmh    "Non-rotating media load increment for non-seeking I/O's");
98256956Ssmh
99256956Ssmhstatic int non_rotating_seek_inc = 1;
100256956SsmhTUNABLE_INT("vfs.zfs.vdev.mirror.non_rotating_seek_inc",
101256956Ssmh     &non_rotating_seek_inc);
102256956SsmhSYSCTL_INT(_vfs_zfs_vdev_mirror, OID_AUTO, non_rotating_seek_inc, CTLFLAG_RW,
103256956Ssmh    &non_rotating_seek_inc, 0,
104256956Ssmh    "Non-rotating media load increment for seeking I/O's");
105256956Ssmh
106256956Ssmh
107256956Ssmhstatic inline size_t
108256956Ssmhvdev_mirror_map_size(int children)
109256956Ssmh{
110256956Ssmh	return (offsetof(mirror_map_t, mm_child[children]) +
111256956Ssmh	    sizeof(int) * children);
112256956Ssmh}
113256956Ssmh
114256956Ssmhstatic inline mirror_map_t *
115256956Ssmhvdev_mirror_map_alloc(int children, boolean_t replacing, boolean_t root)
116256956Ssmh{
117256956Ssmh	mirror_map_t *mm;
118256956Ssmh
119256956Ssmh	mm = kmem_zalloc(vdev_mirror_map_size(children), KM_SLEEP);
120256956Ssmh	mm->mm_children = children;
121256956Ssmh	mm->mm_replacing = replacing;
122256956Ssmh	mm->mm_root = root;
123256956Ssmh	mm->mm_preferred = (int *)((uintptr_t)mm +
124256956Ssmh	    offsetof(mirror_map_t, mm_child[children]));
125256956Ssmh
126256956Ssmh	return mm;
127256956Ssmh}
128256956Ssmh
129185029Spjdstatic void
130185029Spjdvdev_mirror_map_free(zio_t *zio)
131185029Spjd{
132185029Spjd	mirror_map_t *mm = zio->io_vsd;
133185029Spjd
134256956Ssmh	kmem_free(mm, vdev_mirror_map_size(mm->mm_children));
135185029Spjd}
136185029Spjd
137219089Spjdstatic const zio_vsd_ops_t vdev_mirror_vsd_ops = {
138219089Spjd	vdev_mirror_map_free,
139219089Spjd	zio_vsd_default_cksum_report
140219089Spjd};
141219089Spjd
142256956Ssmhstatic int
143256956Ssmhvdev_mirror_load(mirror_map_t *mm, vdev_t *vd, uint64_t zio_offset)
144256956Ssmh{
145256956Ssmh	uint64_t lastoffset;
146256956Ssmh	int load;
147256956Ssmh
148256956Ssmh	/* All DVAs have equal weight at the root. */
149256956Ssmh	if (mm->mm_root)
150256956Ssmh		return (INT_MAX);
151256956Ssmh
152256956Ssmh	/*
153256956Ssmh	 * We don't return INT_MAX if the device is resilvering i.e.
154256956Ssmh	 * vdev_resilver_txg != 0 as when tested performance was slightly
155256956Ssmh	 * worse overall when resilvering with compared to without.
156256956Ssmh	 */
157256956Ssmh
158256956Ssmh	/* Standard load based on pending queue length. */
159256956Ssmh	load = vdev_queue_length(vd);
160256956Ssmh	lastoffset = vdev_queue_lastoffset(vd);
161256956Ssmh
162256956Ssmh	if (vd->vdev_rotation_rate == VDEV_RATE_NON_ROTATING) {
163256956Ssmh		/* Non-rotating media. */
164256956Ssmh		if (lastoffset == zio_offset)
165256956Ssmh			return (load + non_rotating_inc);
166256956Ssmh
167256956Ssmh		/*
168256956Ssmh		 * Apply a seek penalty even for non-rotating devices as
169256956Ssmh		 * sequential I/O'a can be aggregated into fewer operations
170256956Ssmh		 * on the device, thus avoiding unnecessary per-command
171256956Ssmh		 * overhead and boosting performance.
172256956Ssmh		 */
173256956Ssmh		return (load + non_rotating_seek_inc);
174256956Ssmh	}
175256956Ssmh
176256956Ssmh	/* Rotating media I/O's which directly follow the last I/O. */
177256956Ssmh	if (lastoffset == zio_offset)
178256956Ssmh		return (load + rotating_inc);
179256956Ssmh
180256956Ssmh	/*
181256956Ssmh	 * Apply half the seek increment to I/O's within seek offset
182256956Ssmh	 * of the last I/O queued to this vdev as they should incure less
183256956Ssmh	 * of a seek increment.
184256956Ssmh	 */
185256956Ssmh	if (ABS(lastoffset - zio_offset) < rotating_seek_offset)
186256956Ssmh		return (load + (rotating_seek_inc / 2));
187256956Ssmh
188256956Ssmh	/* Apply the full seek increment to all other I/O's. */
189256956Ssmh	return (load + rotating_seek_inc);
190256956Ssmh}
191256956Ssmh
192256956Ssmh
193168404Spjdstatic mirror_map_t *
194256956Ssmhvdev_mirror_map_init(zio_t *zio)
195168404Spjd{
196168404Spjd	mirror_map_t *mm = NULL;
197168404Spjd	mirror_child_t *mc;
198168404Spjd	vdev_t *vd = zio->io_vd;
199256956Ssmh	int c;
200168404Spjd
201168404Spjd	if (vd == NULL) {
202168404Spjd		dva_t *dva = zio->io_bp->blk_dva;
203168404Spjd		spa_t *spa = zio->io_spa;
204168404Spjd
205256956Ssmh		mm = vdev_mirror_map_alloc(BP_GET_NDVAS(zio->io_bp), B_FALSE,
206256956Ssmh		    B_TRUE);
207168404Spjd		for (c = 0; c < mm->mm_children; c++) {
208168404Spjd			mc = &mm->mm_child[c];
209168404Spjd			mc->mc_vd = vdev_lookup_top(spa, DVA_GET_VDEV(&dva[c]));
210168404Spjd			mc->mc_offset = DVA_GET_OFFSET(&dva[c]);
211168404Spjd		}
212168404Spjd	} else {
213256956Ssmh		mm = vdev_mirror_map_alloc(vd->vdev_children,
214256956Ssmh		    (vd->vdev_ops == &vdev_replacing_ops ||
215256956Ssmh                    vd->vdev_ops == &vdev_spare_ops), B_FALSE);
216168404Spjd		for (c = 0; c < mm->mm_children; c++) {
217168404Spjd			mc = &mm->mm_child[c];
218168404Spjd			mc->mc_vd = vd->vdev_child[c];
219168404Spjd			mc->mc_offset = zio->io_offset;
220168404Spjd		}
221168404Spjd	}
222168404Spjd
223168404Spjd	zio->io_vsd = mm;
224219089Spjd	zio->io_vsd_ops = &vdev_mirror_vsd_ops;
225168404Spjd	return (mm);
226168404Spjd}
227168404Spjd
228168404Spjdstatic int
229236155Smmvdev_mirror_open(vdev_t *vd, uint64_t *asize, uint64_t *max_asize,
230254591Sgibbs    uint64_t *logical_ashift, uint64_t *physical_ashift)
231168404Spjd{
232168404Spjd	int numerrors = 0;
233219089Spjd	int lasterror = 0;
234168404Spjd
235168404Spjd	if (vd->vdev_children == 0) {
236168404Spjd		vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
237249195Smm		return (SET_ERROR(EINVAL));
238168404Spjd	}
239168404Spjd
240219089Spjd	vdev_open_children(vd);
241168404Spjd
242219089Spjd	for (int c = 0; c < vd->vdev_children; c++) {
243219089Spjd		vdev_t *cvd = vd->vdev_child[c];
244219089Spjd
245219089Spjd		if (cvd->vdev_open_error) {
246219089Spjd			lasterror = cvd->vdev_open_error;
247168404Spjd			numerrors++;
248168404Spjd			continue;
249168404Spjd		}
250168404Spjd
251168404Spjd		*asize = MIN(*asize - 1, cvd->vdev_asize - 1) + 1;
252236155Smm		*max_asize = MIN(*max_asize - 1, cvd->vdev_max_asize - 1) + 1;
253254591Sgibbs		*logical_ashift = MAX(*logical_ashift, cvd->vdev_ashift);
254254591Sgibbs		*physical_ashift = MAX(*physical_ashift,
255254591Sgibbs		    cvd->vdev_physical_ashift);
256168404Spjd	}
257168404Spjd
258168404Spjd	if (numerrors == vd->vdev_children) {
259168404Spjd		vd->vdev_stat.vs_aux = VDEV_AUX_NO_REPLICAS;
260168404Spjd		return (lasterror);
261168404Spjd	}
262168404Spjd
263168404Spjd	return (0);
264168404Spjd}
265168404Spjd
266168404Spjdstatic void
267168404Spjdvdev_mirror_close(vdev_t *vd)
268168404Spjd{
269219089Spjd	for (int c = 0; c < vd->vdev_children; c++)
270168404Spjd		vdev_close(vd->vdev_child[c]);
271168404Spjd}
272168404Spjd
273168404Spjdstatic void
274168404Spjdvdev_mirror_child_done(zio_t *zio)
275168404Spjd{
276168404Spjd	mirror_child_t *mc = zio->io_private;
277168404Spjd
278168404Spjd	mc->mc_error = zio->io_error;
279168404Spjd	mc->mc_tried = 1;
280168404Spjd	mc->mc_skipped = 0;
281168404Spjd}
282168404Spjd
283168404Spjdstatic void
284168404Spjdvdev_mirror_scrub_done(zio_t *zio)
285168404Spjd{
286168404Spjd	mirror_child_t *mc = zio->io_private;
287168404Spjd
288168404Spjd	if (zio->io_error == 0) {
289209962Smm		zio_t *pio;
290209962Smm
291209962Smm		mutex_enter(&zio->io_lock);
292209962Smm		while ((pio = zio_walk_parents(zio)) != NULL) {
293209962Smm			mutex_enter(&pio->io_lock);
294209962Smm			ASSERT3U(zio->io_size, >=, pio->io_size);
295209962Smm			bcopy(zio->io_data, pio->io_data, pio->io_size);
296209962Smm			mutex_exit(&pio->io_lock);
297209962Smm		}
298209962Smm		mutex_exit(&zio->io_lock);
299168404Spjd	}
300168404Spjd
301168404Spjd	zio_buf_free(zio->io_data, zio->io_size);
302168404Spjd
303168404Spjd	mc->mc_error = zio->io_error;
304168404Spjd	mc->mc_tried = 1;
305168404Spjd	mc->mc_skipped = 0;
306168404Spjd}
307168404Spjd
308168404Spjd/*
309256956Ssmh * Check the other, lower-index DVAs to see if they're on the same
310256956Ssmh * vdev as the child we picked.  If they are, use them since they
311256956Ssmh * are likely to have been allocated from the primary metaslab in
312256956Ssmh * use at the time, and hence are more likely to have locality with
313256956Ssmh * single-copy data.
314256956Ssmh */
315256956Ssmhstatic int
316256956Ssmhvdev_mirror_dva_select(zio_t *zio, int preferred)
317256956Ssmh{
318256956Ssmh	dva_t *dva = zio->io_bp->blk_dva;
319256956Ssmh	mirror_map_t *mm = zio->io_vsd;
320256956Ssmh	int c;
321256956Ssmh
322256956Ssmh	for (c = preferred - 1; c >= 0; c--) {
323256956Ssmh		if (DVA_GET_VDEV(&dva[c]) == DVA_GET_VDEV(&dva[preferred]))
324256956Ssmh			preferred = c;
325256956Ssmh	}
326256956Ssmh	return (preferred);
327256956Ssmh}
328256956Ssmh
329256956Ssmhstatic int
330256956Ssmhvdev_mirror_preferred_child_randomize(zio_t *zio)
331256956Ssmh{
332256956Ssmh	mirror_map_t *mm = zio->io_vsd;
333256956Ssmh	int p;
334256956Ssmh
335256956Ssmh	if (mm->mm_root) {
336256956Ssmh		p = spa_get_random(mm->mm_preferred_cnt);
337256956Ssmh		return (vdev_mirror_dva_select(zio, mm->mm_preferred[p]));
338256956Ssmh	}
339256956Ssmh
340256956Ssmh	/*
341256956Ssmh	 * To ensure we don't always favour the first matching vdev,
342256956Ssmh	 * which could lead to wear leveling issues on SSD's, we
343256956Ssmh	 * use the I/O offset as a pseudo random seed into the vdevs
344256956Ssmh	 * which have the lowest load.
345256956Ssmh	 */
346256956Ssmh	p = (zio->io_offset >> vdev_mirror_shift) % mm->mm_preferred_cnt;
347256956Ssmh	return (mm->mm_preferred[p]);
348256956Ssmh}
349256956Ssmh
350256956Ssmh/*
351256956Ssmh * Try to find a vdev whose DTL doesn't contain the block we want to read
352256956Ssmh * prefering vdevs based on determined load.
353256956Ssmh *
354168404Spjd * If we can't, try the read on any vdev we haven't already tried.
355168404Spjd */
356168404Spjdstatic int
357168404Spjdvdev_mirror_child_select(zio_t *zio)
358168404Spjd{
359168404Spjd	mirror_map_t *mm = zio->io_vsd;
360168404Spjd	uint64_t txg = zio->io_txg;
361256956Ssmh	int c, lowest_load;
362168404Spjd
363219089Spjd	ASSERT(zio->io_bp == NULL || BP_PHYSICAL_BIRTH(zio->io_bp) == txg);
364168404Spjd
365256956Ssmh	lowest_load = INT_MAX;
366256956Ssmh	mm->mm_preferred_cnt = 0;
367256956Ssmh	for (c = 0; c < mm->mm_children; c++) {
368256956Ssmh		mirror_child_t *mc;
369256956Ssmh
370168404Spjd		mc = &mm->mm_child[c];
371168404Spjd		if (mc->mc_tried || mc->mc_skipped)
372168404Spjd			continue;
373256956Ssmh
374185029Spjd		if (!vdev_readable(mc->mc_vd)) {
375249195Smm			mc->mc_error = SET_ERROR(ENXIO);
376168404Spjd			mc->mc_tried = 1;	/* don't even try */
377168404Spjd			mc->mc_skipped = 1;
378168404Spjd			continue;
379168404Spjd		}
380256956Ssmh
381256956Ssmh		if (vdev_dtl_contains(mc->mc_vd, DTL_MISSING, txg, 1)) {
382256956Ssmh			mc->mc_error = SET_ERROR(ESTALE);
383256956Ssmh			mc->mc_skipped = 1;
384256956Ssmh			mc->mc_speculative = 1;
385256956Ssmh			continue;
386256956Ssmh		}
387256956Ssmh
388256956Ssmh		mc->mc_load = vdev_mirror_load(mm, mc->mc_vd, mc->mc_offset);
389256956Ssmh		if (mc->mc_load > lowest_load)
390256956Ssmh			continue;
391256956Ssmh
392256956Ssmh		if (mc->mc_load < lowest_load) {
393256956Ssmh			lowest_load = mc->mc_load;
394256956Ssmh			mm->mm_preferred_cnt = 0;
395256956Ssmh		}
396256956Ssmh		mm->mm_preferred[mm->mm_preferred_cnt] = c;
397256956Ssmh		mm->mm_preferred_cnt++;
398168404Spjd	}
399168404Spjd
400256956Ssmh	if (mm->mm_preferred_cnt == 1) {
401256956Ssmh		vdev_queue_register_lastoffset(
402256956Ssmh		    mm->mm_child[mm->mm_preferred[0]].mc_vd, zio);
403256956Ssmh		return (mm->mm_preferred[0]);
404256956Ssmh	}
405256956Ssmh
406256956Ssmh	if (mm->mm_preferred_cnt > 1) {
407256956Ssmh		int c = vdev_mirror_preferred_child_randomize(zio);
408256956Ssmh
409256956Ssmh		vdev_queue_register_lastoffset(mm->mm_child[c].mc_vd, zio);
410256956Ssmh		return (c);
411256956Ssmh	}
412256956Ssmh
413168404Spjd	/*
414168404Spjd	 * Every device is either missing or has this txg in its DTL.
415168404Spjd	 * Look for any child we haven't already tried before giving up.
416168404Spjd	 */
417256956Ssmh	for (c = 0; c < mm->mm_children; c++) {
418256956Ssmh		if (!mm->mm_child[c].mc_tried) {
419256956Ssmh			vdev_queue_register_lastoffset(mm->mm_child[c].mc_vd,
420256956Ssmh			    zio);
421168404Spjd			return (c);
422256956Ssmh		}
423256956Ssmh	}
424168404Spjd
425168404Spjd	/*
426168404Spjd	 * Every child failed.  There's no place left to look.
427168404Spjd	 */
428168404Spjd	return (-1);
429168404Spjd}
430168404Spjd
431185029Spjdstatic int
432168404Spjdvdev_mirror_io_start(zio_t *zio)
433168404Spjd{
434168404Spjd	mirror_map_t *mm;
435168404Spjd	mirror_child_t *mc;
436168404Spjd	int c, children;
437168404Spjd
438256956Ssmh	mm = vdev_mirror_map_init(zio);
439168404Spjd
440168404Spjd	if (zio->io_type == ZIO_TYPE_READ) {
441168404Spjd		if ((zio->io_flags & ZIO_FLAG_SCRUB) && !mm->mm_replacing) {
442168404Spjd			/*
443168404Spjd			 * For scrubbing reads we need to allocate a read
444168404Spjd			 * buffer for each child and issue reads to all
445168404Spjd			 * children.  If any child succeeds, it will copy its
446168404Spjd			 * data into zio->io_data in vdev_mirror_scrub_done.
447168404Spjd			 */
448168404Spjd			for (c = 0; c < mm->mm_children; c++) {
449168404Spjd				mc = &mm->mm_child[c];
450168404Spjd				zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
451168404Spjd				    mc->mc_vd, mc->mc_offset,
452168404Spjd				    zio_buf_alloc(zio->io_size), zio->io_size,
453185029Spjd				    zio->io_type, zio->io_priority, 0,
454168404Spjd				    vdev_mirror_scrub_done, mc));
455168404Spjd			}
456185029Spjd			return (ZIO_PIPELINE_CONTINUE);
457168404Spjd		}
458168404Spjd		/*
459168404Spjd		 * For normal reads just pick one child.
460168404Spjd		 */
461168404Spjd		c = vdev_mirror_child_select(zio);
462168404Spjd		children = (c >= 0);
463168404Spjd	} else {
464240868Spjd		ASSERT(zio->io_type == ZIO_TYPE_WRITE ||
465240868Spjd		    zio->io_type == ZIO_TYPE_FREE);
466168404Spjd
467168404Spjd		/*
468240868Spjd		 * Writes and frees go to all children.
469168404Spjd		 */
470209962Smm		c = 0;
471209962Smm		children = mm->mm_children;
472168404Spjd	}
473168404Spjd
474168404Spjd	while (children--) {
475168404Spjd		mc = &mm->mm_child[c];
476168404Spjd		zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
477185029Spjd		    mc->mc_vd, mc->mc_offset, zio->io_data, zio->io_size,
478185029Spjd		    zio->io_type, zio->io_priority, 0,
479185029Spjd		    vdev_mirror_child_done, mc));
480168404Spjd		c++;
481168404Spjd	}
482168404Spjd
483185029Spjd	return (ZIO_PIPELINE_CONTINUE);
484168404Spjd}
485168404Spjd
486185029Spjdstatic int
487185029Spjdvdev_mirror_worst_error(mirror_map_t *mm)
488185029Spjd{
489185029Spjd	int error[2] = { 0, 0 };
490185029Spjd
491185029Spjd	for (int c = 0; c < mm->mm_children; c++) {
492185029Spjd		mirror_child_t *mc = &mm->mm_child[c];
493185029Spjd		int s = mc->mc_speculative;
494185029Spjd		error[s] = zio_worst_error(error[s], mc->mc_error);
495185029Spjd	}
496185029Spjd
497185029Spjd	return (error[0] ? error[0] : error[1]);
498185029Spjd}
499185029Spjd
500168404Spjdstatic void
501168404Spjdvdev_mirror_io_done(zio_t *zio)
502168404Spjd{
503168404Spjd	mirror_map_t *mm = zio->io_vsd;
504168404Spjd	mirror_child_t *mc;
505168404Spjd	int c;
506168404Spjd	int good_copies = 0;
507168404Spjd	int unexpected_errors = 0;
508168404Spjd
509168404Spjd	for (c = 0; c < mm->mm_children; c++) {
510168404Spjd		mc = &mm->mm_child[c];
511168404Spjd
512168404Spjd		if (mc->mc_error) {
513168404Spjd			if (!mc->mc_skipped)
514168404Spjd				unexpected_errors++;
515185029Spjd		} else if (mc->mc_tried) {
516185029Spjd			good_copies++;
517168404Spjd		}
518168404Spjd	}
519168404Spjd
520168404Spjd	if (zio->io_type == ZIO_TYPE_WRITE) {
521168404Spjd		/*
522168404Spjd		 * XXX -- for now, treat partial writes as success.
523185029Spjd		 *
524185029Spjd		 * Now that we support write reallocation, it would be better
525185029Spjd		 * to treat partial failure as real failure unless there are
526185029Spjd		 * no non-degraded top-level vdevs left, and not update DTLs
527185029Spjd		 * if we intend to reallocate.
528168404Spjd		 */
529168404Spjd		/* XXPOLICY */
530185029Spjd		if (good_copies != mm->mm_children) {
531185029Spjd			/*
532185029Spjd			 * Always require at least one good copy.
533185029Spjd			 *
534185029Spjd			 * For ditto blocks (io_vd == NULL), require
535185029Spjd			 * all copies to be good.
536185029Spjd			 *
537185029Spjd			 * XXX -- for replacing vdevs, there's no great answer.
538185029Spjd			 * If the old device is really dead, we may not even
539185029Spjd			 * be able to access it -- so we only want to
540185029Spjd			 * require good writes to the new device.  But if
541185029Spjd			 * the new device turns out to be flaky, we want
542185029Spjd			 * to be able to detach it -- which requires all
543185029Spjd			 * writes to the old device to have succeeded.
544185029Spjd			 */
545185029Spjd			if (good_copies == 0 || zio->io_vd == NULL)
546185029Spjd				zio->io_error = vdev_mirror_worst_error(mm);
547185029Spjd		}
548168404Spjd		return;
549240868Spjd	} else if (zio->io_type == ZIO_TYPE_FREE) {
550240868Spjd		return;
551168404Spjd	}
552168404Spjd
553168404Spjd	ASSERT(zio->io_type == ZIO_TYPE_READ);
554168404Spjd
555168404Spjd	/*
556168404Spjd	 * If we don't have a good copy yet, keep trying other children.
557168404Spjd	 */
558168404Spjd	/* XXPOLICY */
559168404Spjd	if (good_copies == 0 && (c = vdev_mirror_child_select(zio)) != -1) {
560168404Spjd		ASSERT(c >= 0 && c < mm->mm_children);
561168404Spjd		mc = &mm->mm_child[c];
562168404Spjd		zio_vdev_io_redone(zio);
563168404Spjd		zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
564168404Spjd		    mc->mc_vd, mc->mc_offset, zio->io_data, zio->io_size,
565185029Spjd		    ZIO_TYPE_READ, zio->io_priority, 0,
566168404Spjd		    vdev_mirror_child_done, mc));
567168404Spjd		return;
568168404Spjd	}
569168404Spjd
570168404Spjd	/* XXPOLICY */
571185029Spjd	if (good_copies == 0) {
572185029Spjd		zio->io_error = vdev_mirror_worst_error(mm);
573168404Spjd		ASSERT(zio->io_error != 0);
574185029Spjd	}
575168404Spjd
576209962Smm	if (good_copies && spa_writeable(zio->io_spa) &&
577168404Spjd	    (unexpected_errors ||
578168404Spjd	    (zio->io_flags & ZIO_FLAG_RESILVER) ||
579168404Spjd	    ((zio->io_flags & ZIO_FLAG_SCRUB) && mm->mm_replacing))) {
580168404Spjd		/*
581168404Spjd		 * Use the good data we have in hand to repair damaged children.
582168404Spjd		 */
583168404Spjd		for (c = 0; c < mm->mm_children; c++) {
584168404Spjd			/*
585168404Spjd			 * Don't rewrite known good children.
586168404Spjd			 * Not only is it unnecessary, it could
587168404Spjd			 * actually be harmful: if the system lost
588168404Spjd			 * power while rewriting the only good copy,
589168404Spjd			 * there would be no good copies left!
590168404Spjd			 */
591168404Spjd			mc = &mm->mm_child[c];
592168404Spjd
593168404Spjd			if (mc->mc_error == 0) {
594168404Spjd				if (mc->mc_tried)
595168404Spjd					continue;
596168404Spjd				if (!(zio->io_flags & ZIO_FLAG_SCRUB) &&
597209962Smm				    !vdev_dtl_contains(mc->mc_vd, DTL_PARTIAL,
598168404Spjd				    zio->io_txg, 1))
599168404Spjd					continue;
600249195Smm				mc->mc_error = SET_ERROR(ESTALE);
601168404Spjd			}
602168404Spjd
603185029Spjd			zio_nowait(zio_vdev_child_io(zio, zio->io_bp,
604185029Spjd			    mc->mc_vd, mc->mc_offset,
605185029Spjd			    zio->io_data, zio->io_size,
606168404Spjd			    ZIO_TYPE_WRITE, zio->io_priority,
607209962Smm			    ZIO_FLAG_IO_REPAIR | (unexpected_errors ?
608209962Smm			    ZIO_FLAG_SELF_HEAL : 0), NULL, NULL));
609168404Spjd		}
610168404Spjd	}
611168404Spjd}
612168404Spjd
613168404Spjdstatic void
614168404Spjdvdev_mirror_state_change(vdev_t *vd, int faulted, int degraded)
615168404Spjd{
616168404Spjd	if (faulted == vd->vdev_children)
617168404Spjd		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
618168404Spjd		    VDEV_AUX_NO_REPLICAS);
619168404Spjd	else if (degraded + faulted != 0)
620168404Spjd		vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, VDEV_AUX_NONE);
621168404Spjd	else
622168404Spjd		vdev_set_state(vd, B_FALSE, VDEV_STATE_HEALTHY, VDEV_AUX_NONE);
623168404Spjd}
624168404Spjd
625168404Spjdvdev_ops_t vdev_mirror_ops = {
626168404Spjd	vdev_mirror_open,
627168404Spjd	vdev_mirror_close,
628168404Spjd	vdev_default_asize,
629168404Spjd	vdev_mirror_io_start,
630168404Spjd	vdev_mirror_io_done,
631168404Spjd	vdev_mirror_state_change,
632219089Spjd	NULL,
633219089Spjd	NULL,
634168404Spjd	VDEV_TYPE_MIRROR,	/* name of this vdev type */
635168404Spjd	B_FALSE			/* not a leaf vdev */
636168404Spjd};
637168404Spjd
638168404Spjdvdev_ops_t vdev_replacing_ops = {
639168404Spjd	vdev_mirror_open,
640168404Spjd	vdev_mirror_close,
641168404Spjd	vdev_default_asize,
642168404Spjd	vdev_mirror_io_start,
643168404Spjd	vdev_mirror_io_done,
644168404Spjd	vdev_mirror_state_change,
645219089Spjd	NULL,
646219089Spjd	NULL,
647168404Spjd	VDEV_TYPE_REPLACING,	/* name of this vdev type */
648168404Spjd	B_FALSE			/* not a leaf vdev */
649168404Spjd};
650168404Spjd
651168404Spjdvdev_ops_t vdev_spare_ops = {
652168404Spjd	vdev_mirror_open,
653168404Spjd	vdev_mirror_close,
654168404Spjd	vdev_default_asize,
655168404Spjd	vdev_mirror_io_start,
656168404Spjd	vdev_mirror_io_done,
657168404Spjd	vdev_mirror_state_change,
658219089Spjd	NULL,
659219089Spjd	NULL,
660168404Spjd	VDEV_TYPE_SPARE,	/* name of this vdev type */
661168404Spjd	B_FALSE			/* not a leaf vdev */
662168404Spjd};
663