xref: /titanic_44/usr/src/uts/common/fs/zfs/vdev_raidz.c (revision 9df12a23948bd40cbe37ce88d84e272c3894e675)
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 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/zfs_context.h>
29 #include <sys/spa.h>
30 #include <sys/vdev_impl.h>
31 #include <sys/zio.h>
32 #include <sys/zio_checksum.h>
33 #include <sys/fs/zfs.h>
34 #include <sys/fm/fs/zfs.h>
35 
36 /*
37  * Virtual device vector for RAID-Z.
38  */
39 
40 /*
41  * We currently allow up to two-way replication (i.e. single-fault
42  * reconstruction) models in RAID-Z vdevs.  The blocks in such vdevs
43  * must all be multiples of two times the leaf vdev blocksize.
44  */
45 #define	VDEV_RAIDZ_ALIGN	2ULL
46 
47 typedef struct raidz_col {
48 	uint64_t	rc_col;
49 	uint64_t	rc_offset;
50 	uint64_t	rc_size;
51 	void		*rc_data;
52 	int		rc_error;
53 	short		rc_tried;
54 	short		rc_skipped;
55 } raidz_col_t;
56 
57 typedef struct raidz_map {
58 	uint64_t	rm_cols;
59 	uint64_t	rm_bigcols;
60 	uint64_t	rm_asize;
61 	int		rm_missing_child;
62 	int		rm_firstdatacol;
63 	raidz_col_t	rm_col[1];
64 } raidz_map_t;
65 
66 static raidz_map_t *
67 vdev_raidz_map_alloc(zio_t *zio, uint64_t unit_shift, uint64_t dcols)
68 {
69 	raidz_map_t *rm;
70 	uint64_t b = zio->io_offset >> unit_shift;
71 	uint64_t s = zio->io_size >> unit_shift;
72 	uint64_t f = b % dcols;
73 	uint64_t o = (b / dcols) << unit_shift;
74 	uint64_t q, r, c, bc, col, acols, coff;
75 	int firstdatacol;
76 
77 	q = s / (dcols - 1);
78 	r = s - q * (dcols - 1);
79 	bc = r + !!r;
80 	firstdatacol = 1;
81 
82 	acols = (q == 0 ? bc : dcols);
83 
84 	rm = kmem_alloc(offsetof(raidz_map_t, rm_col[acols]), KM_SLEEP);
85 
86 	rm->rm_cols = acols;
87 	rm->rm_bigcols = bc;
88 	rm->rm_asize = 0;
89 	rm->rm_missing_child = -1;
90 	rm->rm_firstdatacol = firstdatacol;
91 
92 	for (c = 0; c < acols; c++) {
93 		col = f + c;
94 		coff = o;
95 		if (col >= dcols) {
96 			col -= dcols;
97 			coff += 1ULL << unit_shift;
98 		}
99 		rm->rm_col[c].rc_col = col;
100 		rm->rm_col[c].rc_offset = coff;
101 		rm->rm_col[c].rc_size = (q + (c < bc)) << unit_shift;
102 		rm->rm_col[c].rc_data = NULL;
103 		rm->rm_col[c].rc_error = 0;
104 		rm->rm_col[c].rc_tried = 0;
105 		rm->rm_col[c].rc_skipped = 0;
106 		rm->rm_asize += rm->rm_col[c].rc_size;
107 	}
108 
109 	rm->rm_asize = P2ROUNDUP(rm->rm_asize, VDEV_RAIDZ_ALIGN << unit_shift);
110 
111 	for (c = 0; c < rm->rm_firstdatacol; c++)
112 		rm->rm_col[c].rc_data = zio_buf_alloc(rm->rm_col[c].rc_size);
113 
114 	rm->rm_col[c].rc_data = zio->io_data;
115 
116 	for (c = c + 1; c < acols; c++)
117 		rm->rm_col[c].rc_data = (char *)rm->rm_col[c - 1].rc_data +
118 		    rm->rm_col[c - 1].rc_size;
119 
120 	/*
121 	 * To prevent hot parity disks, switch the parity and data
122 	 * columns every 1MB.
123 	 */
124 	ASSERT(rm->rm_cols >= 2);
125 	ASSERT(rm->rm_col[0].rc_size == rm->rm_col[1].rc_size);
126 
127 	if (zio->io_offset & (1ULL << 20)) {
128 		col = rm->rm_col[0].rc_col;
129 		o = rm->rm_col[0].rc_offset;
130 		rm->rm_col[0].rc_col = rm->rm_col[1].rc_col;
131 		rm->rm_col[0].rc_offset = rm->rm_col[1].rc_offset;
132 		rm->rm_col[1].rc_col = col;
133 		rm->rm_col[1].rc_offset = o;
134 	}
135 
136 	zio->io_vsd = rm;
137 	return (rm);
138 }
139 
140 static void
141 vdev_raidz_map_free(zio_t *zio)
142 {
143 	raidz_map_t *rm = zio->io_vsd;
144 	int c;
145 
146 	for (c = 0; c < rm->rm_firstdatacol; c++)
147 		zio_buf_free(rm->rm_col[c].rc_data, rm->rm_col[c].rc_size);
148 
149 	kmem_free(rm, offsetof(raidz_map_t, rm_col[rm->rm_cols]));
150 	zio->io_vsd = NULL;
151 }
152 
153 static void
154 vdev_raidz_reconstruct(raidz_map_t *rm, int x)
155 {
156 	uint64_t *dst, *src, count, xsize, csize;
157 	int i, c;
158 
159 	for (c = 0; c < rm->rm_cols; c++) {
160 		if (c == x)
161 			continue;
162 		src = rm->rm_col[c].rc_data;
163 		dst = rm->rm_col[x].rc_data;
164 		csize = rm->rm_col[c].rc_size;
165 		xsize = rm->rm_col[x].rc_size;
166 		count = MIN(csize, xsize) / sizeof (uint64_t);
167 		if (c == !x) {
168 			/*
169 			 * The initial copy happens at either c == 0 or c == 1.
170 			 * Both of these columns are 'big' columns, so we'll
171 			 * definitely initialize all of column x.
172 			 */
173 			ASSERT3U(xsize, <=, csize);
174 			for (i = 0; i < count; i++)
175 				*dst++ = *src++;
176 		} else {
177 			for (i = 0; i < count; i++)
178 				*dst++ ^= *src++;
179 		}
180 	}
181 }
182 
183 static int
184 vdev_raidz_open(vdev_t *vd, uint64_t *asize, uint64_t *ashift)
185 {
186 	vdev_t *cvd;
187 	int c, error;
188 	int lasterror = 0;
189 	int numerrors = 0;
190 
191 	/*
192 	 * XXX -- minimum children should be raid-type-specific
193 	 */
194 	if (vd->vdev_children < 2) {
195 		vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
196 		return (EINVAL);
197 	}
198 
199 	for (c = 0; c < vd->vdev_children; c++) {
200 		cvd = vd->vdev_child[c];
201 
202 		if ((error = vdev_open(cvd)) != 0) {
203 			lasterror = error;
204 			numerrors++;
205 			continue;
206 		}
207 
208 		*asize = MIN(*asize - 1, cvd->vdev_asize - 1) + 1;
209 		*ashift = cvd->vdev_ashift;
210 	}
211 
212 	*asize *= vd->vdev_children;
213 
214 	if (numerrors > 1) {
215 		vd->vdev_stat.vs_aux = VDEV_AUX_NO_REPLICAS;
216 		return (lasterror);
217 	}
218 
219 	return (0);
220 }
221 
222 static void
223 vdev_raidz_close(vdev_t *vd)
224 {
225 	int c;
226 
227 	for (c = 0; c < vd->vdev_children; c++)
228 		vdev_close(vd->vdev_child[c]);
229 }
230 
231 static uint64_t
232 vdev_raidz_asize(vdev_t *vd, uint64_t psize)
233 {
234 	uint64_t asize;
235 	uint64_t cols = vd->vdev_children;
236 
237 	asize = psize >> vd->vdev_ashift;
238 	asize += (asize + cols - 2) / (cols - 1);
239 	asize = P2ROUNDUP(asize, VDEV_RAIDZ_ALIGN) << vd->vdev_ashift;
240 
241 	return (asize);
242 }
243 
244 static void
245 vdev_raidz_child_done(zio_t *zio)
246 {
247 	raidz_col_t *rc = zio->io_private;
248 
249 	rc->rc_error = zio->io_error;
250 	rc->rc_tried = 1;
251 	rc->rc_skipped = 0;
252 }
253 
254 static void
255 vdev_raidz_repair_done(zio_t *zio)
256 {
257 	zio_buf_free(zio->io_data, zio->io_size);
258 }
259 
260 static void
261 vdev_raidz_io_start(zio_t *zio)
262 {
263 	vdev_t *vd = zio->io_vd;
264 	vdev_t *cvd;
265 	blkptr_t *bp = zio->io_bp;
266 	raidz_map_t *rm;
267 	raidz_col_t *rc;
268 	int c;
269 
270 	rm = vdev_raidz_map_alloc(zio, vd->vdev_ashift, vd->vdev_children);
271 
272 	if (DVA_GET_GANG(ZIO_GET_DVA(zio))) {
273 		ASSERT3U(rm->rm_asize, ==,
274 		    vdev_psize_to_asize(vd, SPA_GANGBLOCKSIZE));
275 		ASSERT3U(zio->io_size, ==, SPA_GANGBLOCKSIZE);
276 	} else {
277 		ASSERT3U(rm->rm_asize, ==, DVA_GET_ASIZE(ZIO_GET_DVA(zio)));
278 		ASSERT3U(zio->io_size, ==, BP_GET_PSIZE(bp));
279 	}
280 
281 	if (zio->io_type == ZIO_TYPE_WRITE) {
282 
283 		/*
284 		 * Generate RAID parity in virtual column 0.
285 		 */
286 		vdev_raidz_reconstruct(rm, 0);
287 
288 		for (c = 0; c < rm->rm_cols; c++) {
289 			rc = &rm->rm_col[c];
290 			cvd = vd->vdev_child[rc->rc_col];
291 			zio_nowait(zio_vdev_child_io(zio, NULL, cvd,
292 			    rc->rc_offset, rc->rc_data, rc->rc_size,
293 			    zio->io_type, zio->io_priority, ZIO_FLAG_CANFAIL,
294 			    vdev_raidz_child_done, rc));
295 		}
296 		zio_wait_children_done(zio);
297 		return;
298 	}
299 
300 	ASSERT(zio->io_type == ZIO_TYPE_READ);
301 
302 	for (c = rm->rm_cols - 1; c >= 0; c--) {
303 		rc = &rm->rm_col[c];
304 		cvd = vd->vdev_child[rc->rc_col];
305 		if (vdev_is_dead(cvd)) {
306 			rm->rm_missing_child = c;
307 			rc->rc_error = ENXIO;
308 			rc->rc_tried = 1;	/* don't even try */
309 			rc->rc_skipped = 1;
310 			continue;
311 		}
312 		if (vdev_dtl_contains(&cvd->vdev_dtl_map, bp->blk_birth, 1)) {
313 			rm->rm_missing_child = c;
314 			rc->rc_error = ESTALE;
315 			rc->rc_skipped = 1;
316 			continue;
317 		}
318 		if (c >= rm->rm_firstdatacol || rm->rm_missing_child != -1 ||
319 		    (zio->io_flags & ZIO_FLAG_SCRUB)) {
320 			zio_nowait(zio_vdev_child_io(zio, NULL, cvd,
321 			    rc->rc_offset, rc->rc_data, rc->rc_size,
322 			    zio->io_type, zio->io_priority, ZIO_FLAG_CANFAIL,
323 			    vdev_raidz_child_done, rc));
324 		}
325 	}
326 
327 	zio_wait_children_done(zio);
328 }
329 
330 /*
331  * Report a checksum error for a child of a RAID-Z device.
332  */
333 static void
334 raidz_checksum_error(zio_t *zio, raidz_col_t *rc)
335 {
336 	vdev_t *vd = zio->io_vd->vdev_child[rc->rc_col];
337 	dprintf_bp(zio->io_bp, "imputed checksum error on %s: ",
338 	    vdev_description(vd));
339 
340 	if (!(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
341 		mutex_enter(&vd->vdev_stat_lock);
342 		vd->vdev_stat.vs_checksum_errors++;
343 		mutex_exit(&vd->vdev_stat_lock);
344 	}
345 
346 	if (!(zio->io_flags & ZIO_FLAG_SPECULATIVE))
347 		zfs_ereport_post(FM_EREPORT_ZFS_CHECKSUM,
348 		    zio->io_spa, vd, zio, rc->rc_offset, rc->rc_size);
349 }
350 
351 
352 static void
353 vdev_raidz_io_done(zio_t *zio)
354 {
355 	vdev_t *vd = zio->io_vd;
356 	vdev_t *cvd;
357 	raidz_map_t *rm = zio->io_vsd;
358 	raidz_col_t *rc;
359 	blkptr_t *bp = zio->io_bp;
360 	int unexpected_errors = 0;
361 	int c;
362 
363 	ASSERT(bp != NULL);	/* XXX need to add code to enforce this */
364 
365 	zio->io_error = 0;
366 	zio->io_numerrors = 0;
367 
368 	for (c = 0; c < rm->rm_cols; c++) {
369 		rc = &rm->rm_col[c];
370 
371 		/*
372 		 * We preserve any EIOs because those may be worth retrying;
373 		 * whereas ECKSUM and ENXIO are more likely to be persistent.
374 		 */
375 		if (rc->rc_error) {
376 			if (zio->io_error != EIO)
377 				zio->io_error = rc->rc_error;
378 			if (!rc->rc_skipped)
379 				unexpected_errors++;
380 			zio->io_numerrors++;
381 		}
382 	}
383 
384 	if (zio->io_type == ZIO_TYPE_WRITE) {
385 		/*
386 		 * If this is not a failfast write, and we were able to
387 		 * write enough columns to reconstruct the data, good enough.
388 		 */
389 		/* XXPOLICY */
390 		if (zio->io_numerrors <= rm->rm_firstdatacol &&
391 		    !(zio->io_flags & ZIO_FLAG_FAILFAST))
392 			zio->io_error = 0;
393 
394 		vdev_raidz_map_free(zio);
395 		zio_next_stage(zio);
396 		return;
397 	}
398 
399 	ASSERT(zio->io_type == ZIO_TYPE_READ);
400 
401 	/*
402 	 * If there were no I/O errors, and the data checksums correctly,
403 	 * the read is complete.
404 	 */
405 	/* XXPOLICY */
406 	if (zio->io_numerrors == 0 && zio_checksum_error(zio) == 0) {
407 		ASSERT(unexpected_errors == 0);
408 		ASSERT(zio->io_error == 0);
409 
410 		/*
411 		 * We know the data's good.  If we read the parity,
412 		 * verify that it's good as well.  If not, fix it.
413 		 */
414 		for (c = 0; c < rm->rm_firstdatacol; c++) {
415 			void *orig;
416 			rc = &rm->rm_col[c];
417 			if (!rc->rc_tried)
418 				continue;
419 			orig = zio_buf_alloc(rc->rc_size);
420 			bcopy(rc->rc_data, orig, rc->rc_size);
421 			vdev_raidz_reconstruct(rm, c);
422 			if (bcmp(orig, rc->rc_data, rc->rc_size) != 0) {
423 				raidz_checksum_error(zio, rc);
424 				rc->rc_error = ECKSUM;
425 				unexpected_errors++;
426 			}
427 			zio_buf_free(orig, rc->rc_size);
428 		}
429 		goto done;
430 	}
431 
432 	/*
433 	 * If there was exactly one I/O error, it's the one we expected,
434 	 * and the reconstructed data checksums, the read is complete.
435 	 * This happens when one child is offline and vdev_fault_assess()
436 	 * knows it, or when one child has stale data and the DTL knows it.
437 	 */
438 	if (zio->io_numerrors == 1 && (c = rm->rm_missing_child) != -1) {
439 		rc = &rm->rm_col[c];
440 		ASSERT(unexpected_errors == 0);
441 		ASSERT(rc->rc_error == ENXIO || rc->rc_error == ESTALE);
442 		vdev_raidz_reconstruct(rm, c);
443 		if (zio_checksum_error(zio) == 0) {
444 			zio->io_error = 0;
445 			goto done;
446 		}
447 	}
448 
449 	/*
450 	 * This isn't a typical error -- either we got a read error or
451 	 * more than one child claimed a problem.  Read every block we
452 	 * haven't already so we can try combinatorial reconstruction.
453 	 */
454 	unexpected_errors = 1;
455 	rm->rm_missing_child = -1;
456 
457 	for (c = 0; c < rm->rm_cols; c++)
458 		if (!rm->rm_col[c].rc_tried)
459 			break;
460 
461 	if (c != rm->rm_cols) {
462 		zio->io_error = 0;
463 		zio_vdev_io_redone(zio);
464 		for (c = 0; c < rm->rm_cols; c++) {
465 			rc = &rm->rm_col[c];
466 			if (rc->rc_tried)
467 				continue;
468 			zio_nowait(zio_vdev_child_io(zio, NULL,
469 			    vd->vdev_child[rc->rc_col],
470 			    rc->rc_offset, rc->rc_data, rc->rc_size,
471 			    zio->io_type, zio->io_priority, ZIO_FLAG_CANFAIL,
472 			    vdev_raidz_child_done, rc));
473 		}
474 		zio_wait_children_done(zio);
475 		return;
476 	}
477 
478 	/*
479 	 * If there were more errors than parity disks, give up.
480 	 */
481 	if (zio->io_numerrors > rm->rm_firstdatacol) {
482 		ASSERT(zio->io_error != 0);
483 		goto done;
484 	}
485 
486 	/*
487 	 * The number of I/O errors is correctable.  Correct them here.
488 	 */
489 	ASSERT(zio->io_numerrors <= rm->rm_firstdatacol);
490 	for (c = 0; c < rm->rm_cols; c++) {
491 		rc = &rm->rm_col[c];
492 		ASSERT(rc->rc_tried);
493 		if (rc->rc_error) {
494 			vdev_raidz_reconstruct(rm, c);
495 			if (zio_checksum_error(zio) == 0)
496 				zio->io_error = 0;
497 			else
498 				zio->io_error = rc->rc_error;
499 			goto done;
500 		}
501 	}
502 
503 	/*
504 	 * There were no I/O errors, but the data doesn't checksum.
505 	 * Try all permutations to see if we can find one that does.
506 	 */
507 	ASSERT(zio->io_numerrors == 0);
508 	for (c = 0; c < rm->rm_cols; c++) {
509 		void *orig;
510 		rc = &rm->rm_col[c];
511 
512 		orig = zio_buf_alloc(rc->rc_size);
513 		bcopy(rc->rc_data, orig, rc->rc_size);
514 		vdev_raidz_reconstruct(rm, c);
515 
516 		if (zio_checksum_error(zio) == 0) {
517 			zio_buf_free(orig, rc->rc_size);
518 			zio->io_error = 0;
519 			/*
520 			 * If this child didn't know that it returned bad data,
521 			 * inform it.
522 			 */
523 			if (rc->rc_tried && rc->rc_error == 0)
524 				raidz_checksum_error(zio, rc);
525 			rc->rc_error = ECKSUM;
526 			goto done;
527 		}
528 
529 		bcopy(orig, rc->rc_data, rc->rc_size);
530 		zio_buf_free(orig, rc->rc_size);
531 	}
532 
533 	/*
534 	 * All combinations failed to checksum.  Generate checksum ereports for
535 	 * every one.
536 	 */
537 	zio->io_error = ECKSUM;
538 	if (!(zio->io_flags & ZIO_FLAG_SPECULATIVE)) {
539 		for (c = 0; c < rm->rm_cols; c++) {
540 			rc = &rm->rm_col[c];
541 			zfs_ereport_post(FM_EREPORT_ZFS_CHECKSUM,
542 			    zio->io_spa, vd->vdev_child[rc->rc_col], zio,
543 			    rc->rc_offset, rc->rc_size);
544 		}
545 	}
546 
547 done:
548 	zio_checksum_verified(zio);
549 
550 	if (zio->io_error == 0 && (spa_mode & FWRITE) &&
551 	    (unexpected_errors || (zio->io_flags & ZIO_FLAG_RESILVER))) {
552 		/*
553 		 * Use the good data we have in hand to repair damaged children.
554 		 */
555 		for (c = 0; c < rm->rm_cols; c++) {
556 			rc = &rm->rm_col[c];
557 			cvd = vd->vdev_child[rc->rc_col];
558 
559 			if (rc->rc_error) {
560 				/*
561 				 * Make a copy of the data because we're
562 				 * going to free the RAID-Z map below.
563 				 */
564 				void *data = zio_buf_alloc(rc->rc_size);
565 				bcopy(rc->rc_data, data, rc->rc_size);
566 
567 				dprintf("%s resilvered %s @ 0x%llx error %d\n",
568 				    vdev_description(vd),
569 				    vdev_description(cvd),
570 				    zio->io_offset, rc->rc_error);
571 
572 				zio_nowait(zio_vdev_child_io(zio, NULL, cvd,
573 				    rc->rc_offset, data, rc->rc_size,
574 				    ZIO_TYPE_WRITE, zio->io_priority,
575 				    ZIO_FLAG_IO_REPAIR | ZIO_FLAG_CANFAIL |
576 				    ZIO_FLAG_DONT_PROPAGATE,
577 				    vdev_raidz_repair_done, NULL));
578 			}
579 		}
580 	}
581 
582 	vdev_raidz_map_free(zio);
583 	zio_next_stage(zio);
584 }
585 
586 static void
587 vdev_raidz_state_change(vdev_t *vd, int faulted, int degraded)
588 {
589 	if (faulted > 1)
590 		vdev_set_state(vd, B_FALSE, VDEV_STATE_CANT_OPEN,
591 		    VDEV_AUX_NO_REPLICAS);
592 	else if (degraded + faulted != 0)
593 		vdev_set_state(vd, B_FALSE, VDEV_STATE_DEGRADED, VDEV_AUX_NONE);
594 	else
595 		vdev_set_state(vd, B_FALSE, VDEV_STATE_HEALTHY, VDEV_AUX_NONE);
596 }
597 
598 vdev_ops_t vdev_raidz_ops = {
599 	vdev_raidz_open,
600 	vdev_raidz_close,
601 	vdev_raidz_asize,
602 	vdev_raidz_io_start,
603 	vdev_raidz_io_done,
604 	vdev_raidz_state_change,
605 	VDEV_TYPE_RAIDZ,	/* name of this vdev type */
606 	B_FALSE			/* not a leaf vdev */
607 };
608