xref: /onnv-gate/usr/src/uts/common/fs/zfs/vdev_raidz.c (revision 1775:e51e26b432c0)
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 /*
221544Seschrock  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
26789Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
27789Sahrens 
28789Sahrens #include <sys/zfs_context.h>
29789Sahrens #include <sys/spa.h>
30789Sahrens #include <sys/vdev_impl.h>
31789Sahrens #include <sys/zio.h>
32789Sahrens #include <sys/zio_checksum.h>
33789Sahrens #include <sys/fs/zfs.h>
341544Seschrock #include <sys/fm/fs/zfs.h>
35789Sahrens 
36789Sahrens /*
37789Sahrens  * Virtual device vector for RAID-Z.
38789Sahrens  */
39789Sahrens 
40789Sahrens /*
41789Sahrens  * We currently allow up to two-way replication (i.e. single-fault
42789Sahrens  * reconstruction) models in RAID-Z vdevs.  The blocks in such vdevs
43789Sahrens  * must all be multiples of two times the leaf vdev blocksize.
44789Sahrens  */
45789Sahrens #define	VDEV_RAIDZ_ALIGN	2ULL
46789Sahrens 
47789Sahrens typedef struct raidz_col {
48789Sahrens 	uint64_t	rc_col;
49789Sahrens 	uint64_t	rc_offset;
50789Sahrens 	uint64_t	rc_size;
51789Sahrens 	void		*rc_data;
52789Sahrens 	int		rc_error;
53789Sahrens 	short		rc_tried;
54789Sahrens 	short		rc_skipped;
55789Sahrens } raidz_col_t;
56789Sahrens 
57789Sahrens typedef struct raidz_map {
58789Sahrens 	uint64_t	rm_cols;
59789Sahrens 	uint64_t	rm_bigcols;
60789Sahrens 	uint64_t	rm_asize;
61789Sahrens 	int		rm_missing_child;
62789Sahrens 	int		rm_firstdatacol;
63789Sahrens 	raidz_col_t	rm_col[1];
64789Sahrens } raidz_map_t;
65789Sahrens 
66789Sahrens static raidz_map_t *
671133Seschrock vdev_raidz_map_alloc(zio_t *zio, uint64_t unit_shift, uint64_t dcols)
68789Sahrens {
69789Sahrens 	raidz_map_t *rm;
70789Sahrens 	uint64_t b = zio->io_offset >> unit_shift;
71789Sahrens 	uint64_t s = zio->io_size >> unit_shift;
72789Sahrens 	uint64_t f = b % dcols;
73789Sahrens 	uint64_t o = (b / dcols) << unit_shift;
74789Sahrens 	uint64_t q, r, c, bc, col, acols, coff;
75789Sahrens 	int firstdatacol;
76789Sahrens 
771133Seschrock 	q = s / (dcols - 1);
781133Seschrock 	r = s - q * (dcols - 1);
791133Seschrock 	bc = r + !!r;
801133Seschrock 	firstdatacol = 1;
81789Sahrens 
82789Sahrens 	acols = (q == 0 ? bc : dcols);
83789Sahrens 
84789Sahrens 	rm = kmem_alloc(offsetof(raidz_map_t, rm_col[acols]), KM_SLEEP);
85789Sahrens 
86789Sahrens 	rm->rm_cols = acols;
87789Sahrens 	rm->rm_bigcols = bc;
88789Sahrens 	rm->rm_asize = 0;
89789Sahrens 	rm->rm_missing_child = -1;
90789Sahrens 	rm->rm_firstdatacol = firstdatacol;
91789Sahrens 
92789Sahrens 	for (c = 0; c < acols; c++) {
93789Sahrens 		col = f + c;
94789Sahrens 		coff = o;
95789Sahrens 		if (col >= dcols) {
96789Sahrens 			col -= dcols;
97789Sahrens 			coff += 1ULL << unit_shift;
98789Sahrens 		}
99789Sahrens 		rm->rm_col[c].rc_col = col;
100789Sahrens 		rm->rm_col[c].rc_offset = coff;
101789Sahrens 		rm->rm_col[c].rc_size = (q + (c < bc)) << unit_shift;
102789Sahrens 		rm->rm_col[c].rc_data = NULL;
103789Sahrens 		rm->rm_col[c].rc_error = 0;
104789Sahrens 		rm->rm_col[c].rc_tried = 0;
105789Sahrens 		rm->rm_col[c].rc_skipped = 0;
106789Sahrens 		rm->rm_asize += rm->rm_col[c].rc_size;
107789Sahrens 	}
108789Sahrens 
109789Sahrens 	rm->rm_asize = P2ROUNDUP(rm->rm_asize, VDEV_RAIDZ_ALIGN << unit_shift);
110789Sahrens 
111789Sahrens 	for (c = 0; c < rm->rm_firstdatacol; c++)
112789Sahrens 		rm->rm_col[c].rc_data = zio_buf_alloc(rm->rm_col[c].rc_size);
113789Sahrens 
114789Sahrens 	rm->rm_col[c].rc_data = zio->io_data;
115789Sahrens 
116789Sahrens 	for (c = c + 1; c < acols; c++)
117789Sahrens 		rm->rm_col[c].rc_data = (char *)rm->rm_col[c - 1].rc_data +
118789Sahrens 		    rm->rm_col[c - 1].rc_size;
119789Sahrens 
1201133Seschrock 	/*
1211133Seschrock 	 * To prevent hot parity disks, switch the parity and data
1221133Seschrock 	 * columns every 1MB.
1231133Seschrock 	 */
1241133Seschrock 	ASSERT(rm->rm_cols >= 2);
1251133Seschrock 	ASSERT(rm->rm_col[0].rc_size == rm->rm_col[1].rc_size);
126789Sahrens 
1271133Seschrock 	if (zio->io_offset & (1ULL << 20)) {
1281133Seschrock 		col = rm->rm_col[0].rc_col;
1291133Seschrock 		o = rm->rm_col[0].rc_offset;
1301133Seschrock 		rm->rm_col[0].rc_col = rm->rm_col[1].rc_col;
1311133Seschrock 		rm->rm_col[0].rc_offset = rm->rm_col[1].rc_offset;
1321133Seschrock 		rm->rm_col[1].rc_col = col;
1331133Seschrock 		rm->rm_col[1].rc_offset = o;
134789Sahrens 	}
135789Sahrens 
136789Sahrens 	zio->io_vsd = rm;
137789Sahrens 	return (rm);
138789Sahrens }
139789Sahrens 
140789Sahrens static void
141789Sahrens vdev_raidz_map_free(zio_t *zio)
142789Sahrens {
143789Sahrens 	raidz_map_t *rm = zio->io_vsd;
144789Sahrens 	int c;
145789Sahrens 
146789Sahrens 	for (c = 0; c < rm->rm_firstdatacol; c++)
147789Sahrens 		zio_buf_free(rm->rm_col[c].rc_data, rm->rm_col[c].rc_size);
148789Sahrens 
149789Sahrens 	kmem_free(rm, offsetof(raidz_map_t, rm_col[rm->rm_cols]));
150789Sahrens 	zio->io_vsd = NULL;
151789Sahrens }
152789Sahrens 
153789Sahrens static void
154789Sahrens vdev_raidz_reconstruct(raidz_map_t *rm, int x)
155789Sahrens {
156789Sahrens 	uint64_t *dst, *src, count, xsize, csize;
157789Sahrens 	int i, c;
158789Sahrens 
159789Sahrens 	for (c = 0; c < rm->rm_cols; c++) {
160789Sahrens 		if (c == x)
161789Sahrens 			continue;
162789Sahrens 		src = rm->rm_col[c].rc_data;
163789Sahrens 		dst = rm->rm_col[x].rc_data;
164789Sahrens 		csize = rm->rm_col[c].rc_size;
165789Sahrens 		xsize = rm->rm_col[x].rc_size;
166789Sahrens 		count = MIN(csize, xsize) / sizeof (uint64_t);
167789Sahrens 		if (c == !x) {
168789Sahrens 			/*
169789Sahrens 			 * The initial copy happens at either c == 0 or c == 1.
170789Sahrens 			 * Both of these columns are 'big' columns, so we'll
171789Sahrens 			 * definitely initialize all of column x.
172789Sahrens 			 */
173789Sahrens 			ASSERT3U(xsize, <=, csize);
174789Sahrens 			for (i = 0; i < count; i++)
175789Sahrens 				*dst++ = *src++;
176789Sahrens 		} else {
177789Sahrens 			for (i = 0; i < count; i++)
178789Sahrens 				*dst++ ^= *src++;
179789Sahrens 		}
180789Sahrens 	}
181789Sahrens }
182789Sahrens 
183789Sahrens static int
184789Sahrens vdev_raidz_open(vdev_t *vd, uint64_t *asize, uint64_t *ashift)
185789Sahrens {
186789Sahrens 	vdev_t *cvd;
187789Sahrens 	int c, error;
188789Sahrens 	int lasterror = 0;
189789Sahrens 	int numerrors = 0;
190789Sahrens 
191789Sahrens 	/*
192789Sahrens 	 * XXX -- minimum children should be raid-type-specific
193789Sahrens 	 */
194789Sahrens 	if (vd->vdev_children < 2) {
195789Sahrens 		vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
196789Sahrens 		return (EINVAL);
197789Sahrens 	}
198789Sahrens 
199789Sahrens 	for (c = 0; c < vd->vdev_children; c++) {
200789Sahrens 		cvd = vd->vdev_child[c];
201789Sahrens 
202789Sahrens 		if ((error = vdev_open(cvd)) != 0) {
203789Sahrens 			lasterror = error;
204789Sahrens 			numerrors++;
205789Sahrens 			continue;
206789Sahrens 		}
207789Sahrens 
208789Sahrens 		*asize = MIN(*asize - 1, cvd->vdev_asize - 1) + 1;
2091732Sbonwick 		*ashift = MAX(*ashift, cvd->vdev_ashift);
210789Sahrens 	}
211789Sahrens 
212789Sahrens 	*asize *= vd->vdev_children;
213789Sahrens 
214789Sahrens 	if (numerrors > 1) {
215789Sahrens 		vd->vdev_stat.vs_aux = VDEV_AUX_NO_REPLICAS;
216789Sahrens 		return (lasterror);
217789Sahrens 	}
218789Sahrens 
219789Sahrens 	return (0);
220789Sahrens }
221789Sahrens 
222789Sahrens static void
223789Sahrens vdev_raidz_close(vdev_t *vd)
224789Sahrens {
225789Sahrens 	int c;
226789Sahrens 
227789Sahrens 	for (c = 0; c < vd->vdev_children; c++)
228789Sahrens 		vdev_close(vd->vdev_child[c]);
229789Sahrens }
230789Sahrens 
231789Sahrens static uint64_t
232789Sahrens vdev_raidz_asize(vdev_t *vd, uint64_t psize)
233789Sahrens {
234789Sahrens 	uint64_t asize;
2351732Sbonwick 	uint64_t ashift = vd->vdev_top->vdev_ashift;
236789Sahrens 	uint64_t cols = vd->vdev_children;
237789Sahrens 
2381732Sbonwick 	asize = ((psize - 1) >> ashift) + 1;
239789Sahrens 	asize += (asize + cols - 2) / (cols - 1);
2401732Sbonwick 	asize = P2ROUNDUP(asize, VDEV_RAIDZ_ALIGN) << ashift;
241789Sahrens 
242789Sahrens 	return (asize);
243789Sahrens }
244789Sahrens 
245789Sahrens static void
246789Sahrens vdev_raidz_child_done(zio_t *zio)
247789Sahrens {
248789Sahrens 	raidz_col_t *rc = zio->io_private;
249789Sahrens 
250789Sahrens 	rc->rc_error = zio->io_error;
251789Sahrens 	rc->rc_tried = 1;
252789Sahrens 	rc->rc_skipped = 0;
253789Sahrens }
254789Sahrens 
255789Sahrens static void
256789Sahrens vdev_raidz_repair_done(zio_t *zio)
257789Sahrens {
2581732Sbonwick 	ASSERT(zio->io_private == zio->io_parent);
2591732Sbonwick 	vdev_raidz_map_free(zio->io_private);
260789Sahrens }
261789Sahrens 
262789Sahrens static void
263789Sahrens vdev_raidz_io_start(zio_t *zio)
264789Sahrens {
265789Sahrens 	vdev_t *vd = zio->io_vd;
2661732Sbonwick 	vdev_t *tvd = vd->vdev_top;
267789Sahrens 	vdev_t *cvd;
268789Sahrens 	blkptr_t *bp = zio->io_bp;
269789Sahrens 	raidz_map_t *rm;
270789Sahrens 	raidz_col_t *rc;
271789Sahrens 	int c;
272789Sahrens 
2731732Sbonwick 	rm = vdev_raidz_map_alloc(zio, tvd->vdev_ashift, vd->vdev_children);
274789Sahrens 
275*1775Sbillm 	ASSERT3U(rm->rm_asize, ==, vdev_psize_to_asize(vd, zio->io_size));
276789Sahrens 
277789Sahrens 	if (zio->io_type == ZIO_TYPE_WRITE) {
278789Sahrens 
279789Sahrens 		/*
280789Sahrens 		 * Generate RAID parity in virtual column 0.
281789Sahrens 		 */
282789Sahrens 		vdev_raidz_reconstruct(rm, 0);
283789Sahrens 
284789Sahrens 		for (c = 0; c < rm->rm_cols; c++) {
285789Sahrens 			rc = &rm->rm_col[c];
286789Sahrens 			cvd = vd->vdev_child[rc->rc_col];
287789Sahrens 			zio_nowait(zio_vdev_child_io(zio, NULL, cvd,
288789Sahrens 			    rc->rc_offset, rc->rc_data, rc->rc_size,
289789Sahrens 			    zio->io_type, zio->io_priority, ZIO_FLAG_CANFAIL,
290789Sahrens 			    vdev_raidz_child_done, rc));
291789Sahrens 		}
292789Sahrens 		zio_wait_children_done(zio);
293789Sahrens 		return;
294789Sahrens 	}
295789Sahrens 
296789Sahrens 	ASSERT(zio->io_type == ZIO_TYPE_READ);
297789Sahrens 
298789Sahrens 	for (c = rm->rm_cols - 1; c >= 0; c--) {
299789Sahrens 		rc = &rm->rm_col[c];
300789Sahrens 		cvd = vd->vdev_child[rc->rc_col];
301789Sahrens 		if (vdev_is_dead(cvd)) {
302789Sahrens 			rm->rm_missing_child = c;
303789Sahrens 			rc->rc_error = ENXIO;
304789Sahrens 			rc->rc_tried = 1;	/* don't even try */
305789Sahrens 			rc->rc_skipped = 1;
306789Sahrens 			continue;
307789Sahrens 		}
308789Sahrens 		if (vdev_dtl_contains(&cvd->vdev_dtl_map, bp->blk_birth, 1)) {
309789Sahrens 			rm->rm_missing_child = c;
310789Sahrens 			rc->rc_error = ESTALE;
311789Sahrens 			rc->rc_skipped = 1;
312789Sahrens 			continue;
313789Sahrens 		}
314789Sahrens 		if (c >= rm->rm_firstdatacol || rm->rm_missing_child != -1 ||
315789Sahrens 		    (zio->io_flags & ZIO_FLAG_SCRUB)) {
316789Sahrens 			zio_nowait(zio_vdev_child_io(zio, NULL, cvd,
317789Sahrens 			    rc->rc_offset, rc->rc_data, rc->rc_size,
318789Sahrens 			    zio->io_type, zio->io_priority, ZIO_FLAG_CANFAIL,
319789Sahrens 			    vdev_raidz_child_done, rc));
320789Sahrens 		}
321789Sahrens 	}
322789Sahrens 
323789Sahrens 	zio_wait_children_done(zio);
324789Sahrens }
325789Sahrens 
3261544Seschrock /*
3271544Seschrock  * Report a checksum error for a child of a RAID-Z device.
3281544Seschrock  */
3291544Seschrock static void
3301544Seschrock raidz_checksum_error(zio_t *zio, raidz_col_t *rc)
3311544Seschrock {
3321544Seschrock 	vdev_t *vd = zio->io_vd->vdev_child[rc->rc_col];
3331544Seschrock 	dprintf_bp(zio->io_bp, "imputed checksum error on %s: ",
3341544Seschrock 	    vdev_description(vd));
3351544Seschrock 
3361544Seschrock 	if (!(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
3371544Seschrock 		mutex_enter(&vd->vdev_stat_lock);
3381544Seschrock 		vd->vdev_stat.vs_checksum_errors++;
3391544Seschrock 		mutex_exit(&vd->vdev_stat_lock);
3401544Seschrock 	}
3411544Seschrock 
3421544Seschrock 	if (!(zio->io_flags & ZIO_FLAG_SPECULATIVE))
3431544Seschrock 		zfs_ereport_post(FM_EREPORT_ZFS_CHECKSUM,
3441544Seschrock 		    zio->io_spa, vd, zio, rc->rc_offset, rc->rc_size);
3451544Seschrock }
3461544Seschrock 
3471544Seschrock 
348789Sahrens static void
349789Sahrens vdev_raidz_io_done(zio_t *zio)
350789Sahrens {
351789Sahrens 	vdev_t *vd = zio->io_vd;
352789Sahrens 	vdev_t *cvd;
353789Sahrens 	raidz_map_t *rm = zio->io_vsd;
354789Sahrens 	raidz_col_t *rc;
355789Sahrens 	int unexpected_errors = 0;
356789Sahrens 	int c;
357789Sahrens 
358*1775Sbillm 	ASSERT(zio->io_bp != NULL);  /* XXX need to add code to enforce this */
359789Sahrens 
360789Sahrens 	zio->io_error = 0;
361789Sahrens 	zio->io_numerrors = 0;
362789Sahrens 
363789Sahrens 	for (c = 0; c < rm->rm_cols; c++) {
364789Sahrens 		rc = &rm->rm_col[c];
365789Sahrens 
366789Sahrens 		/*
367789Sahrens 		 * We preserve any EIOs because those may be worth retrying;
368789Sahrens 		 * whereas ECKSUM and ENXIO are more likely to be persistent.
369789Sahrens 		 */
370789Sahrens 		if (rc->rc_error) {
371789Sahrens 			if (zio->io_error != EIO)
372789Sahrens 				zio->io_error = rc->rc_error;
373789Sahrens 			if (!rc->rc_skipped)
374789Sahrens 				unexpected_errors++;
375789Sahrens 			zio->io_numerrors++;
376789Sahrens 		}
377789Sahrens 	}
378789Sahrens 
379789Sahrens 	if (zio->io_type == ZIO_TYPE_WRITE) {
380789Sahrens 		/*
381789Sahrens 		 * If this is not a failfast write, and we were able to
382789Sahrens 		 * write enough columns to reconstruct the data, good enough.
383789Sahrens 		 */
384789Sahrens 		/* XXPOLICY */
385789Sahrens 		if (zio->io_numerrors <= rm->rm_firstdatacol &&
386789Sahrens 		    !(zio->io_flags & ZIO_FLAG_FAILFAST))
387789Sahrens 			zio->io_error = 0;
388789Sahrens 
389789Sahrens 		vdev_raidz_map_free(zio);
390789Sahrens 		zio_next_stage(zio);
391789Sahrens 		return;
392789Sahrens 	}
393789Sahrens 
394789Sahrens 	ASSERT(zio->io_type == ZIO_TYPE_READ);
395789Sahrens 
396789Sahrens 	/*
397789Sahrens 	 * If there were no I/O errors, and the data checksums correctly,
398789Sahrens 	 * the read is complete.
399789Sahrens 	 */
400789Sahrens 	/* XXPOLICY */
401789Sahrens 	if (zio->io_numerrors == 0 && zio_checksum_error(zio) == 0) {
402789Sahrens 		ASSERT(unexpected_errors == 0);
403789Sahrens 		ASSERT(zio->io_error == 0);
404789Sahrens 
405789Sahrens 		/*
406789Sahrens 		 * We know the data's good.  If we read the parity,
407789Sahrens 		 * verify that it's good as well.  If not, fix it.
408789Sahrens 		 */
409789Sahrens 		for (c = 0; c < rm->rm_firstdatacol; c++) {
410789Sahrens 			void *orig;
411789Sahrens 			rc = &rm->rm_col[c];
412789Sahrens 			if (!rc->rc_tried)
413789Sahrens 				continue;
414789Sahrens 			orig = zio_buf_alloc(rc->rc_size);
415789Sahrens 			bcopy(rc->rc_data, orig, rc->rc_size);
416789Sahrens 			vdev_raidz_reconstruct(rm, c);
417789Sahrens 			if (bcmp(orig, rc->rc_data, rc->rc_size) != 0) {
4181544Seschrock 				raidz_checksum_error(zio, rc);
419789Sahrens 				rc->rc_error = ECKSUM;
420789Sahrens 				unexpected_errors++;
421789Sahrens 			}
422789Sahrens 			zio_buf_free(orig, rc->rc_size);
423789Sahrens 		}
424789Sahrens 		goto done;
425789Sahrens 	}
426789Sahrens 
427789Sahrens 	/*
428789Sahrens 	 * If there was exactly one I/O error, it's the one we expected,
429789Sahrens 	 * and the reconstructed data checksums, the read is complete.
430789Sahrens 	 * This happens when one child is offline and vdev_fault_assess()
431789Sahrens 	 * knows it, or when one child has stale data and the DTL knows it.
432789Sahrens 	 */
433789Sahrens 	if (zio->io_numerrors == 1 && (c = rm->rm_missing_child) != -1) {
434789Sahrens 		rc = &rm->rm_col[c];
435789Sahrens 		ASSERT(unexpected_errors == 0);
436789Sahrens 		ASSERT(rc->rc_error == ENXIO || rc->rc_error == ESTALE);
437789Sahrens 		vdev_raidz_reconstruct(rm, c);
438789Sahrens 		if (zio_checksum_error(zio) == 0) {
439789Sahrens 			zio->io_error = 0;
440789Sahrens 			goto done;
441789Sahrens 		}
442789Sahrens 	}
443789Sahrens 
444789Sahrens 	/*
445789Sahrens 	 * This isn't a typical error -- either we got a read error or
446789Sahrens 	 * more than one child claimed a problem.  Read every block we
447789Sahrens 	 * haven't already so we can try combinatorial reconstruction.
448789Sahrens 	 */
449789Sahrens 	unexpected_errors = 1;
450789Sahrens 	rm->rm_missing_child = -1;
451789Sahrens 
452789Sahrens 	for (c = 0; c < rm->rm_cols; c++)
453789Sahrens 		if (!rm->rm_col[c].rc_tried)
454789Sahrens 			break;
455789Sahrens 
456789Sahrens 	if (c != rm->rm_cols) {
457789Sahrens 		zio->io_error = 0;
458789Sahrens 		zio_vdev_io_redone(zio);
459789Sahrens 		for (c = 0; c < rm->rm_cols; c++) {
460789Sahrens 			rc = &rm->rm_col[c];
461789Sahrens 			if (rc->rc_tried)
462789Sahrens 				continue;
463789Sahrens 			zio_nowait(zio_vdev_child_io(zio, NULL,
464789Sahrens 			    vd->vdev_child[rc->rc_col],
465789Sahrens 			    rc->rc_offset, rc->rc_data, rc->rc_size,
466789Sahrens 			    zio->io_type, zio->io_priority, ZIO_FLAG_CANFAIL,
467789Sahrens 			    vdev_raidz_child_done, rc));
468789Sahrens 		}
469789Sahrens 		zio_wait_children_done(zio);
470789Sahrens 		return;
471789Sahrens 	}
472789Sahrens 
473789Sahrens 	/*
474789Sahrens 	 * If there were more errors than parity disks, give up.
475789Sahrens 	 */
476789Sahrens 	if (zio->io_numerrors > rm->rm_firstdatacol) {
477789Sahrens 		ASSERT(zio->io_error != 0);
478789Sahrens 		goto done;
479789Sahrens 	}
480789Sahrens 
481789Sahrens 	/*
482789Sahrens 	 * The number of I/O errors is correctable.  Correct them here.
483789Sahrens 	 */
484789Sahrens 	ASSERT(zio->io_numerrors <= rm->rm_firstdatacol);
485789Sahrens 	for (c = 0; c < rm->rm_cols; c++) {
486789Sahrens 		rc = &rm->rm_col[c];
487789Sahrens 		ASSERT(rc->rc_tried);
488789Sahrens 		if (rc->rc_error) {
489789Sahrens 			vdev_raidz_reconstruct(rm, c);
490789Sahrens 			if (zio_checksum_error(zio) == 0)
491789Sahrens 				zio->io_error = 0;
492789Sahrens 			else
493789Sahrens 				zio->io_error = rc->rc_error;
494789Sahrens 			goto done;
495789Sahrens 		}
496789Sahrens 	}
497789Sahrens 
498789Sahrens 	/*
499789Sahrens 	 * There were no I/O errors, but the data doesn't checksum.
500789Sahrens 	 * Try all permutations to see if we can find one that does.
501789Sahrens 	 */
502789Sahrens 	ASSERT(zio->io_numerrors == 0);
503789Sahrens 	for (c = 0; c < rm->rm_cols; c++) {
504789Sahrens 		void *orig;
505789Sahrens 		rc = &rm->rm_col[c];
506789Sahrens 
507789Sahrens 		orig = zio_buf_alloc(rc->rc_size);
508789Sahrens 		bcopy(rc->rc_data, orig, rc->rc_size);
509789Sahrens 		vdev_raidz_reconstruct(rm, c);
510789Sahrens 
511789Sahrens 		if (zio_checksum_error(zio) == 0) {
512789Sahrens 			zio_buf_free(orig, rc->rc_size);
513789Sahrens 			zio->io_error = 0;
514789Sahrens 			/*
515789Sahrens 			 * If this child didn't know that it returned bad data,
516789Sahrens 			 * inform it.
517789Sahrens 			 */
518789Sahrens 			if (rc->rc_tried && rc->rc_error == 0)
5191544Seschrock 				raidz_checksum_error(zio, rc);
520789Sahrens 			rc->rc_error = ECKSUM;
521789Sahrens 			goto done;
522789Sahrens 		}
523789Sahrens 
524789Sahrens 		bcopy(orig, rc->rc_data, rc->rc_size);
525789Sahrens 		zio_buf_free(orig, rc->rc_size);
526789Sahrens 	}
527789Sahrens 
528789Sahrens 	/*
5291544Seschrock 	 * All combinations failed to checksum.  Generate checksum ereports for
5301544Seschrock 	 * every one.
531789Sahrens 	 */
532789Sahrens 	zio->io_error = ECKSUM;
5331544Seschrock 	if (!(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
5341544Seschrock 		for (c = 0; c < rm->rm_cols; c++) {
5351544Seschrock 			rc = &rm->rm_col[c];
5361544Seschrock 			zfs_ereport_post(FM_EREPORT_ZFS_CHECKSUM,
5371544Seschrock 			    zio->io_spa, vd->vdev_child[rc->rc_col], zio,
5381544Seschrock 			    rc->rc_offset, rc->rc_size);
5391544Seschrock 		}
5401544Seschrock 	}
541789Sahrens 
542789Sahrens done:
543789Sahrens 	zio_checksum_verified(zio);
544789Sahrens 
545789Sahrens 	if (zio->io_error == 0 && (spa_mode & FWRITE) &&
546789Sahrens 	    (unexpected_errors || (zio->io_flags & ZIO_FLAG_RESILVER))) {
5471732Sbonwick 		zio_t *rio;
5481732Sbonwick 
549789Sahrens 		/*
550789Sahrens 		 * Use the good data we have in hand to repair damaged children.
5511732Sbonwick 		 *
5521732Sbonwick 		 * We issue all repair I/Os as children of 'rio' to arrange
5531732Sbonwick 		 * that vdev_raidz_map_free(zio) will be invoked after all
5541732Sbonwick 		 * repairs complete, but before we advance to the next stage.
555789Sahrens 		 */
5561732Sbonwick 		rio = zio_null(zio, zio->io_spa,
5571732Sbonwick 		    vdev_raidz_repair_done, zio, ZIO_FLAG_CANFAIL);
5581732Sbonwick 
559789Sahrens 		for (c = 0; c < rm->rm_cols; c++) {
560789Sahrens 			rc = &rm->rm_col[c];
561789Sahrens 			cvd = vd->vdev_child[rc->rc_col];
562789Sahrens 
5631732Sbonwick 			if (rc->rc_error == 0)
5641732Sbonwick 				continue;
5651732Sbonwick 
5661732Sbonwick 			dprintf("%s resilvered %s @ 0x%llx error %d\n",
5671732Sbonwick 			    vdev_description(vd),
5681732Sbonwick 			    vdev_description(cvd),
5691732Sbonwick 			    zio->io_offset, rc->rc_error);
570789Sahrens 
5711732Sbonwick 			zio_nowait(zio_vdev_child_io(rio, NULL, cvd,
5721732Sbonwick 			    rc->rc_offset, rc->rc_data, rc->rc_size,
5731732Sbonwick 			    ZIO_TYPE_WRITE, zio->io_priority,
5741732Sbonwick 			    ZIO_FLAG_IO_REPAIR | ZIO_FLAG_CANFAIL |
5751732Sbonwick 			    ZIO_FLAG_DONT_PROPAGATE, NULL, NULL));
5761732Sbonwick 		}
577789Sahrens 
5781732Sbonwick 		zio_nowait(rio);
5791732Sbonwick 		zio_wait_children_done(zio);
5801732Sbonwick 		return;
581789Sahrens 	}
582789Sahrens 
583789Sahrens 	vdev_raidz_map_free(zio);
584789Sahrens 	zio_next_stage(zio);
585789Sahrens }
586789Sahrens 
587789Sahrens static void
588789Sahrens vdev_raidz_state_change(vdev_t *vd, int faulted, int degraded)
589789Sahrens {
590789Sahrens 	if (faulted > 1)
5911544Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
5921544Seschrock 		    VDEV_AUX_NO_REPLICAS);
593789Sahrens 	else if (degraded + faulted != 0)
5941544Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, VDEV_AUX_NONE);
595789Sahrens 	else
5961544Seschrock 		vdev_set_state(vd, B_FALSE, VDEV_STATE_HEALTHY, VDEV_AUX_NONE);
597789Sahrens }
598789Sahrens 
599789Sahrens vdev_ops_t vdev_raidz_ops = {
600789Sahrens 	vdev_raidz_open,
601789Sahrens 	vdev_raidz_close,
602789Sahrens 	vdev_raidz_asize,
603789Sahrens 	vdev_raidz_io_start,
604789Sahrens 	vdev_raidz_io_done,
605789Sahrens 	vdev_raidz_state_change,
606789Sahrens 	VDEV_TYPE_RAIDZ,	/* name of this vdev type */
607789Sahrens 	B_FALSE			/* not a leaf vdev */
608789Sahrens };
609