xref: /illumos-gate/usr/src/uts/common/fs/zfs/zfs_fm.c (revision 533affcbc7fc4d0c8132976ea454aaa715fe2307)
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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * Copyright (c) 2012 by Delphix. All rights reserved.
28  */
29 
30 #include <sys/spa.h>
31 #include <sys/spa_impl.h>
32 #include <sys/vdev.h>
33 #include <sys/vdev_impl.h>
34 #include <sys/zio.h>
35 #include <sys/zio_checksum.h>
36 
37 #include <sys/fm/fs/zfs.h>
38 #include <sys/fm/protocol.h>
39 #include <sys/fm/util.h>
40 #include <sys/sysevent.h>
41 
42 /*
43  * This general routine is responsible for generating all the different ZFS
44  * ereports.  The payload is dependent on the class, and which arguments are
45  * supplied to the function:
46  *
47  *	EREPORT			POOL	VDEV	IO
48  *	block			X	X	X
49  *	data			X		X
50  *	device			X	X
51  *	pool			X
52  *
53  * If we are in a loading state, all errors are chained together by the same
54  * SPA-wide ENA (Error Numeric Association).
55  *
56  * For isolated I/O requests, we get the ENA from the zio_t. The propagation
57  * gets very complicated due to RAID-Z, gang blocks, and vdev caching.  We want
58  * to chain together all ereports associated with a logical piece of data.  For
59  * read I/Os, there  are basically three 'types' of I/O, which form a roughly
60  * layered diagram:
61  *
62  *      +---------------+
63  *	| Aggregate I/O |	No associated logical data or device
64  *	+---------------+
65  *              |
66  *              V
67  *	+---------------+	Reads associated with a piece of logical data.
68  *	|   Read I/O    |	This includes reads on behalf of RAID-Z,
69  *	+---------------+       mirrors, gang blocks, retries, etc.
70  *              |
71  *              V
72  *	+---------------+	Reads associated with a particular device, but
73  *	| Physical I/O  |	no logical data.  Issued as part of vdev caching
74  *	+---------------+	and I/O aggregation.
75  *
76  * Note that 'physical I/O' here is not the same terminology as used in the rest
77  * of ZIO.  Typically, 'physical I/O' simply means that there is no attached
78  * blockpointer.  But I/O with no associated block pointer can still be related
79  * to a logical piece of data (i.e. RAID-Z requests).
80  *
81  * Purely physical I/O always have unique ENAs.  They are not related to a
82  * particular piece of logical data, and therefore cannot be chained together.
83  * We still generate an ereport, but the DE doesn't correlate it with any
84  * logical piece of data.  When such an I/O fails, the delegated I/O requests
85  * will issue a retry, which will trigger the 'real' ereport with the correct
86  * ENA.
87  *
88  * We keep track of the ENA for a ZIO chain through the 'io_logical' member.
89  * When a new logical I/O is issued, we set this to point to itself.  Child I/Os
90  * then inherit this pointer, so that when it is first set subsequent failures
91  * will use the same ENA.  For vdev cache fill and queue aggregation I/O,
92  * this pointer is set to NULL, and no ereport will be generated (since it
93  * doesn't actually correspond to any particular device or piece of data,
94  * and the caller will always retry without caching or queueing anyway).
95  *
96  * For checksum errors, we want to include more information about the actual
97  * error which occurs.  Accordingly, we build an ereport when the error is
98  * noticed, but instead of sending it in immediately, we hang it off of the
99  * io_cksum_report field of the logical IO.  When the logical IO completes
100  * (successfully or not), zfs_ereport_finish_checksum() is called with the
101  * good and bad versions of the buffer (if available), and we annotate the
102  * ereport with information about the differences.
103  */
104 #ifdef _KERNEL
105 
106 /*
107  * Return B_TRUE if the event actually posted, B_FALSE if not.
108  */
109 static boolean_t
110 zfs_ereport_start(nvlist_t **ereport_out, nvlist_t **detector_out,
111     const char *subclass, spa_t *spa, vdev_t *vd, const zbookmark_phys_t *zb,
112     zio_t *zio, uint64_t stateoroffset, uint64_t size)
113 {
114 	nvlist_t *ereport, *detector;
115 
116 	uint64_t ena;
117 	char class[64];
118 
119 	if (!zfs_ereport_is_valid(subclass, spa, vd, zio))
120 		return (B_FALSE);
121 
122 	if ((ereport = fm_nvlist_create(NULL)) == NULL)
123 		return (B_FALSE);
124 
125 	if ((detector = fm_nvlist_create(NULL)) == NULL) {
126 		fm_nvlist_destroy(ereport, FM_NVA_FREE);
127 		return (B_FALSE);
128 	}
129 
130 	/*
131 	 * Serialize ereport generation
132 	 */
133 	mutex_enter(&spa->spa_errlist_lock);
134 
135 	/*
136 	 * Determine the ENA to use for this event.  If we are in a loading
137 	 * state, use a SPA-wide ENA.  Otherwise, if we are in an I/O state, use
138 	 * a root zio-wide ENA.  Otherwise, simply use a unique ENA.
139 	 */
140 	if (spa_load_state(spa) != SPA_LOAD_NONE) {
141 		if (spa->spa_ena == 0)
142 			spa->spa_ena = fm_ena_generate(0, FM_ENA_FMT1);
143 		ena = spa->spa_ena;
144 	} else if (zio != NULL && zio->io_logical != NULL) {
145 		if (zio->io_logical->io_ena == 0)
146 			zio->io_logical->io_ena =
147 			    fm_ena_generate(0, FM_ENA_FMT1);
148 		ena = zio->io_logical->io_ena;
149 	} else {
150 		ena = fm_ena_generate(0, FM_ENA_FMT1);
151 	}
152 
153 	/*
154 	 * Construct the full class, detector, and other standard FMA fields.
155 	 */
156 	(void) snprintf(class, sizeof (class), "%s.%s",
157 	    ZFS_ERROR_CLASS, subclass);
158 
159 	fm_fmri_zfs_set(detector, FM_ZFS_SCHEME_VERSION, spa_guid(spa),
160 	    vd != NULL ? vd->vdev_guid : 0);
161 
162 	fm_ereport_set(ereport, FM_EREPORT_VERSION, class, ena, detector, NULL);
163 
164 	/*
165 	 * Construct the per-ereport payload, depending on which parameters are
166 	 * passed in.
167 	 */
168 
169 	/*
170 	 * Generic payload members common to all ereports.
171 	 */
172 	fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_POOL,
173 	    DATA_TYPE_STRING, spa_name(spa), FM_EREPORT_PAYLOAD_ZFS_POOL_GUID,
174 	    DATA_TYPE_UINT64, spa_guid(spa),
175 	    FM_EREPORT_PAYLOAD_ZFS_POOL_CONTEXT, DATA_TYPE_INT32,
176 	    spa_load_state(spa), NULL);
177 
178 	if (spa != NULL) {
179 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_POOL_FAILMODE,
180 		    DATA_TYPE_STRING,
181 		    spa_get_failmode(spa) == ZIO_FAILURE_MODE_WAIT ?
182 		    FM_EREPORT_FAILMODE_WAIT :
183 		    spa_get_failmode(spa) == ZIO_FAILURE_MODE_CONTINUE ?
184 		    FM_EREPORT_FAILMODE_CONTINUE : FM_EREPORT_FAILMODE_PANIC,
185 		    NULL);
186 	}
187 
188 	if (vd != NULL) {
189 		vdev_t *pvd = vd->vdev_parent;
190 
191 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_VDEV_GUID,
192 		    DATA_TYPE_UINT64, vd->vdev_guid,
193 		    FM_EREPORT_PAYLOAD_ZFS_VDEV_TYPE,
194 		    DATA_TYPE_STRING, vd->vdev_ops->vdev_op_type, NULL);
195 		if (vd->vdev_path != NULL)
196 			fm_payload_set(ereport,
197 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_PATH,
198 			    DATA_TYPE_STRING, vd->vdev_path, NULL);
199 		if (vd->vdev_devid != NULL)
200 			fm_payload_set(ereport,
201 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_DEVID,
202 			    DATA_TYPE_STRING, vd->vdev_devid, NULL);
203 		if (vd->vdev_fru != NULL)
204 			fm_payload_set(ereport,
205 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_FRU,
206 			    DATA_TYPE_STRING, vd->vdev_fru, NULL);
207 		if (vd->vdev_ashift)
208 			fm_payload_set(ereport,
209 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_ASHIFT,
210 			    DATA_TYPE_UINT64, vd->vdev_ashift, NULL);
211 
212 		if (pvd != NULL) {
213 			fm_payload_set(ereport,
214 			    FM_EREPORT_PAYLOAD_ZFS_PARENT_GUID,
215 			    DATA_TYPE_UINT64, pvd->vdev_guid,
216 			    FM_EREPORT_PAYLOAD_ZFS_PARENT_TYPE,
217 			    DATA_TYPE_STRING, pvd->vdev_ops->vdev_op_type,
218 			    NULL);
219 			if (pvd->vdev_path)
220 				fm_payload_set(ereport,
221 				    FM_EREPORT_PAYLOAD_ZFS_PARENT_PATH,
222 				    DATA_TYPE_STRING, pvd->vdev_path, NULL);
223 			if (pvd->vdev_devid)
224 				fm_payload_set(ereport,
225 				    FM_EREPORT_PAYLOAD_ZFS_PARENT_DEVID,
226 				    DATA_TYPE_STRING, pvd->vdev_devid, NULL);
227 		}
228 	}
229 
230 	if (zio != NULL) {
231 		/*
232 		 * Payload common to all I/Os.
233 		 */
234 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_ERR,
235 		    DATA_TYPE_INT32, zio->io_error, NULL);
236 
237 		/*
238 		 * If the 'size' parameter is non-zero, it indicates this is a
239 		 * RAID-Z or other I/O where the physical offset and length are
240 		 * provided for us, instead of within the zio_t.
241 		 */
242 		if (vd != NULL) {
243 			if (size)
244 				fm_payload_set(ereport,
245 				    FM_EREPORT_PAYLOAD_ZFS_ZIO_OFFSET,
246 				    DATA_TYPE_UINT64, stateoroffset,
247 				    FM_EREPORT_PAYLOAD_ZFS_ZIO_SIZE,
248 				    DATA_TYPE_UINT64, size, NULL);
249 			else
250 				fm_payload_set(ereport,
251 				    FM_EREPORT_PAYLOAD_ZFS_ZIO_OFFSET,
252 				    DATA_TYPE_UINT64, zio->io_offset,
253 				    FM_EREPORT_PAYLOAD_ZFS_ZIO_SIZE,
254 				    DATA_TYPE_UINT64, zio->io_size, NULL);
255 		}
256 	} else if (vd != NULL) {
257 		/*
258 		 * If we have a vdev but no zio, this is a device fault, and the
259 		 * 'stateoroffset' parameter indicates the previous state of the
260 		 * vdev.
261 		 */
262 		fm_payload_set(ereport,
263 		    FM_EREPORT_PAYLOAD_ZFS_PREV_STATE,
264 		    DATA_TYPE_UINT64, stateoroffset, NULL);
265 	}
266 
267 	/*
268 	 * Payload for I/Os with corresponding logical information.
269 	 */
270 	if (zb != NULL && (zio == NULL || zio->io_logical != NULL)) {
271 		fm_payload_set(ereport,
272 		    FM_EREPORT_PAYLOAD_ZFS_ZIO_OBJSET,
273 		    DATA_TYPE_UINT64, zb->zb_objset,
274 		    FM_EREPORT_PAYLOAD_ZFS_ZIO_OBJECT,
275 		    DATA_TYPE_UINT64, zb->zb_object,
276 		    FM_EREPORT_PAYLOAD_ZFS_ZIO_LEVEL,
277 		    DATA_TYPE_INT64, zb->zb_level,
278 		    FM_EREPORT_PAYLOAD_ZFS_ZIO_BLKID,
279 		    DATA_TYPE_UINT64, zb->zb_blkid, NULL);
280 	}
281 
282 	mutex_exit(&spa->spa_errlist_lock);
283 
284 	*ereport_out = ereport;
285 	*detector_out = detector;
286 	return (B_TRUE);
287 }
288 
289 /* if it's <= 128 bytes, save the corruption directly */
290 #define	ZFM_MAX_INLINE		(128 / sizeof (uint64_t))
291 
292 #define	MAX_RANGES		16
293 
294 typedef struct zfs_ecksum_info {
295 	/* histograms of set and cleared bits by bit number in a 64-bit word */
296 	uint32_t zei_histogram_set[sizeof (uint64_t) * NBBY];
297 	uint32_t zei_histogram_cleared[sizeof (uint64_t) * NBBY];
298 
299 	/* inline arrays of bits set and cleared. */
300 	uint64_t zei_bits_set[ZFM_MAX_INLINE];
301 	uint64_t zei_bits_cleared[ZFM_MAX_INLINE];
302 
303 	/*
304 	 * for each range, the number of bits set and cleared.  The Hamming
305 	 * distance between the good and bad buffers is the sum of them all.
306 	 */
307 	uint32_t zei_range_sets[MAX_RANGES];
308 	uint32_t zei_range_clears[MAX_RANGES];
309 
310 	struct zei_ranges {
311 		uint32_t	zr_start;
312 		uint32_t	zr_end;
313 	} zei_ranges[MAX_RANGES];
314 
315 	size_t	zei_range_count;
316 	uint32_t zei_mingap;
317 	uint32_t zei_allowed_mingap;
318 
319 } zfs_ecksum_info_t;
320 
321 static void
322 update_histogram(uint64_t value_arg, uint32_t *hist, uint32_t *count)
323 {
324 	size_t i;
325 	size_t bits = 0;
326 	uint64_t value = BE_64(value_arg);
327 
328 	/* We store the bits in big-endian (largest-first) order */
329 	for (i = 0; i < 64; i++) {
330 		if (value & (1ull << i)) {
331 			hist[63 - i]++;
332 			++bits;
333 		}
334 	}
335 	/* update the count of bits changed */
336 	*count += bits;
337 }
338 
339 /*
340  * We've now filled up the range array, and need to increase "mingap" and
341  * shrink the range list accordingly.  zei_mingap is always the smallest
342  * distance between array entries, so we set the new_allowed_gap to be
343  * one greater than that.  We then go through the list, joining together
344  * any ranges which are closer than the new_allowed_gap.
345  *
346  * By construction, there will be at least one.  We also update zei_mingap
347  * to the new smallest gap, to prepare for our next invocation.
348  */
349 static void
350 shrink_ranges(zfs_ecksum_info_t *eip)
351 {
352 	uint32_t mingap = UINT32_MAX;
353 	uint32_t new_allowed_gap = eip->zei_mingap + 1;
354 
355 	size_t idx, output;
356 	size_t max = eip->zei_range_count;
357 
358 	struct zei_ranges *r = eip->zei_ranges;
359 
360 	ASSERT3U(eip->zei_range_count, >, 0);
361 	ASSERT3U(eip->zei_range_count, <=, MAX_RANGES);
362 
363 	output = idx = 0;
364 	while (idx < max - 1) {
365 		uint32_t start = r[idx].zr_start;
366 		uint32_t end = r[idx].zr_end;
367 
368 		while (idx < max - 1) {
369 			idx++;
370 
371 			uint32_t nstart = r[idx].zr_start;
372 			uint32_t nend = r[idx].zr_end;
373 
374 			uint32_t gap = nstart - end;
375 			if (gap < new_allowed_gap) {
376 				end = nend;
377 				continue;
378 			}
379 			if (gap < mingap)
380 				mingap = gap;
381 			break;
382 		}
383 		r[output].zr_start = start;
384 		r[output].zr_end = end;
385 		output++;
386 	}
387 	ASSERT3U(output, <, eip->zei_range_count);
388 	eip->zei_range_count = output;
389 	eip->zei_mingap = mingap;
390 	eip->zei_allowed_mingap = new_allowed_gap;
391 }
392 
393 static void
394 add_range(zfs_ecksum_info_t *eip, int start, int end)
395 {
396 	struct zei_ranges *r = eip->zei_ranges;
397 	size_t count = eip->zei_range_count;
398 
399 	if (count >= MAX_RANGES) {
400 		shrink_ranges(eip);
401 		count = eip->zei_range_count;
402 	}
403 	if (count == 0) {
404 		eip->zei_mingap = UINT32_MAX;
405 		eip->zei_allowed_mingap = 1;
406 	} else {
407 		int gap = start - r[count - 1].zr_end;
408 
409 		if (gap < eip->zei_allowed_mingap) {
410 			r[count - 1].zr_end = end;
411 			return;
412 		}
413 		if (gap < eip->zei_mingap)
414 			eip->zei_mingap = gap;
415 	}
416 	r[count].zr_start = start;
417 	r[count].zr_end = end;
418 	eip->zei_range_count++;
419 }
420 
421 static size_t
422 range_total_size(zfs_ecksum_info_t *eip)
423 {
424 	struct zei_ranges *r = eip->zei_ranges;
425 	size_t count = eip->zei_range_count;
426 	size_t result = 0;
427 	size_t idx;
428 
429 	for (idx = 0; idx < count; idx++)
430 		result += (r[idx].zr_end - r[idx].zr_start);
431 
432 	return (result);
433 }
434 
435 static zfs_ecksum_info_t *
436 annotate_ecksum(nvlist_t *ereport, zio_bad_cksum_t *info,
437     const abd_t *goodabd, const abd_t *badabd, size_t size,
438     boolean_t drop_if_identical)
439 {
440 	const uint64_t *good;
441 	const uint64_t *bad;
442 
443 	uint64_t allset = 0;
444 	uint64_t allcleared = 0;
445 
446 	size_t nui64s = size / sizeof (uint64_t);
447 
448 	size_t inline_size;
449 	int no_inline = 0;
450 	size_t idx;
451 	size_t range;
452 
453 	size_t offset = 0;
454 	ssize_t start = -1;
455 
456 	zfs_ecksum_info_t *eip = kmem_zalloc(sizeof (*eip), KM_SLEEP);
457 
458 	/* don't do any annotation for injected checksum errors */
459 	if (info != NULL && info->zbc_injected)
460 		return (eip);
461 
462 	if (info != NULL && info->zbc_has_cksum) {
463 		fm_payload_set(ereport,
464 		    FM_EREPORT_PAYLOAD_ZFS_CKSUM_EXPECTED,
465 		    DATA_TYPE_UINT64_ARRAY,
466 		    sizeof (info->zbc_expected) / sizeof (uint64_t),
467 		    (uint64_t *)&info->zbc_expected,
468 		    FM_EREPORT_PAYLOAD_ZFS_CKSUM_ACTUAL,
469 		    DATA_TYPE_UINT64_ARRAY,
470 		    sizeof (info->zbc_actual) / sizeof (uint64_t),
471 		    (uint64_t *)&info->zbc_actual,
472 		    FM_EREPORT_PAYLOAD_ZFS_CKSUM_ALGO,
473 		    DATA_TYPE_STRING,
474 		    info->zbc_checksum_name,
475 		    NULL);
476 
477 		if (info->zbc_byteswapped) {
478 			fm_payload_set(ereport,
479 			    FM_EREPORT_PAYLOAD_ZFS_CKSUM_BYTESWAP,
480 			    DATA_TYPE_BOOLEAN, 1,
481 			    NULL);
482 		}
483 	}
484 
485 	if (badabd == NULL || goodabd == NULL)
486 		return (eip);
487 
488 	ASSERT3U(nui64s, <=, UINT32_MAX);
489 	ASSERT3U(size, ==, nui64s * sizeof (uint64_t));
490 	ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
491 	ASSERT3U(size, <=, UINT32_MAX);
492 
493 	good = (const uint64_t *) abd_borrow_buf_copy((abd_t *)goodabd, size);
494 	bad = (const uint64_t *) abd_borrow_buf_copy((abd_t *)badabd, size);
495 
496 	/* build up the range list by comparing the two buffers. */
497 	for (idx = 0; idx < nui64s; idx++) {
498 		if (good[idx] == bad[idx]) {
499 			if (start == -1)
500 				continue;
501 
502 			add_range(eip, start, idx);
503 			start = -1;
504 		} else {
505 			if (start != -1)
506 				continue;
507 
508 			start = idx;
509 		}
510 	}
511 	if (start != -1)
512 		add_range(eip, start, idx);
513 
514 	/* See if it will fit in our inline buffers */
515 	inline_size = range_total_size(eip);
516 	if (inline_size > ZFM_MAX_INLINE)
517 		no_inline = 1;
518 
519 	/*
520 	 * If there is no change and we want to drop if the buffers are
521 	 * identical, do so.
522 	 */
523 	if (inline_size == 0 && drop_if_identical) {
524 		kmem_free(eip, sizeof (*eip));
525 		abd_return_buf((abd_t *)goodabd, (void *)good, size);
526 		abd_return_buf((abd_t *)badabd, (void *)bad, size);
527 		return (NULL);
528 	}
529 
530 	/*
531 	 * Now walk through the ranges, filling in the details of the
532 	 * differences.  Also convert our uint64_t-array offsets to byte
533 	 * offsets.
534 	 */
535 	for (range = 0; range < eip->zei_range_count; range++) {
536 		size_t start = eip->zei_ranges[range].zr_start;
537 		size_t end = eip->zei_ranges[range].zr_end;
538 
539 		for (idx = start; idx < end; idx++) {
540 			uint64_t set, cleared;
541 
542 			// bits set in bad, but not in good
543 			set = ((~good[idx]) & bad[idx]);
544 			// bits set in good, but not in bad
545 			cleared = (good[idx] & (~bad[idx]));
546 
547 			allset |= set;
548 			allcleared |= cleared;
549 
550 			if (!no_inline) {
551 				ASSERT3U(offset, <, inline_size);
552 				eip->zei_bits_set[offset] = set;
553 				eip->zei_bits_cleared[offset] = cleared;
554 				offset++;
555 			}
556 
557 			update_histogram(set, eip->zei_histogram_set,
558 			    &eip->zei_range_sets[range]);
559 			update_histogram(cleared, eip->zei_histogram_cleared,
560 			    &eip->zei_range_clears[range]);
561 		}
562 
563 		/* convert to byte offsets */
564 		eip->zei_ranges[range].zr_start	*= sizeof (uint64_t);
565 		eip->zei_ranges[range].zr_end	*= sizeof (uint64_t);
566 	}
567 
568 	abd_return_buf((abd_t *)goodabd, (void *)good, size);
569 	abd_return_buf((abd_t *)badabd, (void *)bad, size);
570 
571 	eip->zei_allowed_mingap	*= sizeof (uint64_t);
572 	inline_size		*= sizeof (uint64_t);
573 
574 	/* fill in ereport */
575 	fm_payload_set(ereport,
576 	    FM_EREPORT_PAYLOAD_ZFS_BAD_OFFSET_RANGES,
577 	    DATA_TYPE_UINT32_ARRAY, 2 * eip->zei_range_count,
578 	    (uint32_t *)eip->zei_ranges,
579 	    FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_MIN_GAP,
580 	    DATA_TYPE_UINT32, eip->zei_allowed_mingap,
581 	    FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_SETS,
582 	    DATA_TYPE_UINT32_ARRAY, eip->zei_range_count, eip->zei_range_sets,
583 	    FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_CLEARS,
584 	    DATA_TYPE_UINT32_ARRAY, eip->zei_range_count, eip->zei_range_clears,
585 	    NULL);
586 
587 	if (!no_inline) {
588 		fm_payload_set(ereport,
589 		    FM_EREPORT_PAYLOAD_ZFS_BAD_SET_BITS,
590 		    DATA_TYPE_UINT8_ARRAY,
591 		    inline_size, (uint8_t *)eip->zei_bits_set,
592 		    FM_EREPORT_PAYLOAD_ZFS_BAD_CLEARED_BITS,
593 		    DATA_TYPE_UINT8_ARRAY,
594 		    inline_size, (uint8_t *)eip->zei_bits_cleared,
595 		    NULL);
596 	} else {
597 		fm_payload_set(ereport,
598 		    FM_EREPORT_PAYLOAD_ZFS_BAD_SET_HISTOGRAM,
599 		    DATA_TYPE_UINT32_ARRAY,
600 		    NBBY * sizeof (uint64_t), eip->zei_histogram_set,
601 		    FM_EREPORT_PAYLOAD_ZFS_BAD_CLEARED_HISTOGRAM,
602 		    DATA_TYPE_UINT32_ARRAY,
603 		    NBBY * sizeof (uint64_t), eip->zei_histogram_cleared,
604 		    NULL);
605 	}
606 	return (eip);
607 }
608 #endif
609 
610 /*
611  * Make sure our event is still valid for the given zio/vdev/pool.  For example,
612  * we don't want to keep logging events for a faulted or missing vdev.
613  */
614 boolean_t
615 zfs_ereport_is_valid(const char *subclass, spa_t *spa, vdev_t *vd, zio_t *zio)
616 {
617 #ifdef _KERNEL
618 	/*
619 	 * If we are doing a spa_tryimport() or in recovery mode,
620 	 * ignore errors.
621 	 */
622 	if (spa_load_state(spa) == SPA_LOAD_TRYIMPORT ||
623 	    spa_load_state(spa) == SPA_LOAD_RECOVER)
624 		return (B_FALSE);
625 
626 	/*
627 	 * If we are in the middle of opening a pool, and the previous attempt
628 	 * failed, don't bother logging any new ereports - we're just going to
629 	 * get the same diagnosis anyway.
630 	 */
631 	if (spa_load_state(spa) != SPA_LOAD_NONE &&
632 	    spa->spa_last_open_failed)
633 		return (B_FALSE);
634 
635 	if (zio != NULL) {
636 		/*
637 		 * If this is not a read or write zio, ignore the error.  This
638 		 * can occur if the DKIOCFLUSHWRITECACHE ioctl fails.
639 		 */
640 		if (zio->io_type != ZIO_TYPE_READ &&
641 		    zio->io_type != ZIO_TYPE_WRITE)
642 			return (B_FALSE);
643 
644 		if (vd != NULL) {
645 			/*
646 			 * If the vdev has already been marked as failing due
647 			 * to a failed probe, then ignore any subsequent I/O
648 			 * errors, as the DE will automatically fault the vdev
649 			 * on the first such failure.  This also catches cases
650 			 * where vdev_remove_wanted is set and the device has
651 			 * not yet been asynchronously placed into the REMOVED
652 			 * state.
653 			 */
654 			if (zio->io_vd == vd && !vdev_accessible(vd, zio))
655 				return (B_FALSE);
656 
657 			/*
658 			 * Ignore checksum errors for reads from DTL regions of
659 			 * leaf vdevs.
660 			 */
661 			if (zio->io_type == ZIO_TYPE_READ &&
662 			    zio->io_error == ECKSUM &&
663 			    vd->vdev_ops->vdev_op_leaf &&
664 			    vdev_dtl_contains(vd, DTL_MISSING, zio->io_txg, 1))
665 				return (B_FALSE);
666 		}
667 	}
668 
669 	/*
670 	 * For probe failure, we want to avoid posting ereports if we've
671 	 * already removed the device in the meantime.
672 	 */
673 	if (vd != NULL &&
674 	    strcmp(subclass, FM_EREPORT_ZFS_PROBE_FAILURE) == 0 &&
675 	    (vd->vdev_remove_wanted || vd->vdev_state == VDEV_STATE_REMOVED))
676 		return (B_FALSE);
677 
678 	/* Ignore bogus delay events (like from ioctls or unqueued IOs) */
679 	if ((strcmp(subclass, FM_EREPORT_ZFS_DELAY) == 0) &&
680 	    (zio != NULL) && (!zio->io_timestamp)) {
681 		return (B_FALSE);
682 	}
683 #endif
684 	return (B_TRUE);
685 }
686 
687 /*
688  * Return 0 if event was posted, EINVAL if there was a problem posting it or
689  * EBUSY if the event was rate limited.
690  */
691 int
692 zfs_ereport_post(const char *subclass, spa_t *spa, vdev_t *vd,
693     const struct zbookmark_phys *zb, zio_t *zio, uint64_t stateoroffset,
694     uint64_t size)
695 {
696 	int rc = 0;
697 #ifdef _KERNEL
698 	nvlist_t *ereport = NULL;
699 	nvlist_t *detector = NULL;
700 
701 	if (!zfs_ereport_start(&ereport, &detector, subclass, spa, vd,
702 	    zb, zio, stateoroffset, size))
703 		return (SET_ERROR(EINVAL));	/* couldn't post event */
704 
705 	if (ereport == NULL)
706 		return (SET_ERROR(EINVAL));
707 
708 	fm_ereport_post(ereport, EVCH_SLEEP);
709 
710 	fm_nvlist_destroy(ereport, FM_NVA_FREE);
711 	fm_nvlist_destroy(detector, FM_NVA_FREE);
712 #endif
713 	return (rc);
714 }
715 
716 void
717 zfs_ereport_start_checksum(spa_t *spa, vdev_t *vd, const zbookmark_phys_t *zb,
718     struct zio *zio, uint64_t offset, uint64_t length, void *arg,
719     zio_bad_cksum_t *info)
720 {
721 	zio_cksum_report_t *report = kmem_zalloc(sizeof (*report), KM_SLEEP);
722 
723 	if (zio->io_vsd != NULL)
724 		zio->io_vsd_ops->vsd_cksum_report(zio, report, arg);
725 	else
726 		zio_vsd_default_cksum_report(zio, report, arg);
727 
728 	/* copy the checksum failure information if it was provided */
729 	if (info != NULL) {
730 		report->zcr_ckinfo = kmem_zalloc(sizeof (*info), KM_SLEEP);
731 		bcopy(info, report->zcr_ckinfo, sizeof (*info));
732 	}
733 
734 	report->zcr_align = 1ULL << vd->vdev_top->vdev_ashift;
735 	report->zcr_length = length;
736 
737 #ifdef _KERNEL
738 	(void) zfs_ereport_start(&report->zcr_ereport, &report->zcr_detector,
739 	    FM_EREPORT_ZFS_CHECKSUM, spa, vd, zb, zio, offset, length);
740 
741 	if (report->zcr_ereport == NULL) {
742 		report->zcr_free(report->zcr_cbdata, report->zcr_cbinfo);
743 		if (report->zcr_ckinfo != NULL) {
744 			kmem_free(report->zcr_ckinfo,
745 			    sizeof (*report->zcr_ckinfo));
746 		}
747 		kmem_free(report, sizeof (*report));
748 		return;
749 	}
750 #endif
751 
752 	mutex_enter(&spa->spa_errlist_lock);
753 	report->zcr_next = zio->io_logical->io_cksum_report;
754 	zio->io_logical->io_cksum_report = report;
755 	mutex_exit(&spa->spa_errlist_lock);
756 }
757 
758 void
759 zfs_ereport_finish_checksum(zio_cksum_report_t *report, const abd_t *good_data,
760     const abd_t *bad_data, boolean_t drop_if_identical)
761 {
762 #ifdef _KERNEL
763 	zfs_ecksum_info_t *info = NULL;
764 	info = annotate_ecksum(report->zcr_ereport, report->zcr_ckinfo,
765 	    good_data, bad_data, report->zcr_length, drop_if_identical);
766 
767 	if (info != NULL)
768 		fm_ereport_post(report->zcr_ereport, EVCH_SLEEP);
769 
770 	fm_nvlist_destroy(report->zcr_ereport, FM_NVA_FREE);
771 	fm_nvlist_destroy(report->zcr_detector, FM_NVA_FREE);
772 	report->zcr_ereport = report->zcr_detector = NULL;
773 
774 	if (info != NULL)
775 		kmem_free(info, sizeof (*info));
776 #endif
777 }
778 
779 void
780 zfs_ereport_free_checksum(zio_cksum_report_t *rpt)
781 {
782 #ifdef _KERNEL
783 	if (rpt->zcr_ereport != NULL) {
784 		fm_nvlist_destroy(rpt->zcr_ereport,
785 		    FM_NVA_FREE);
786 		fm_nvlist_destroy(rpt->zcr_detector,
787 		    FM_NVA_FREE);
788 	}
789 #endif
790 	rpt->zcr_free(rpt->zcr_cbdata, rpt->zcr_cbinfo);
791 
792 	if (rpt->zcr_ckinfo != NULL)
793 		kmem_free(rpt->zcr_ckinfo, sizeof (*rpt->zcr_ckinfo));
794 
795 	kmem_free(rpt, sizeof (*rpt));
796 }
797 
798 void
799 zfs_ereport_send_interim_checksum(zio_cksum_report_t *report)
800 {
801 #ifdef _KERNEL
802 	fm_ereport_post(report->zcr_ereport, EVCH_SLEEP);
803 #endif
804 }
805 
806 int
807 zfs_ereport_post_checksum(spa_t *spa, vdev_t *vd, const zbookmark_phys_t *zb,
808     struct zio *zio, uint64_t offset, uint64_t length,
809     const abd_t *good_data, const abd_t *bad_data, zio_bad_cksum_t *zbc)
810 {
811 	int rc = 0;
812 #ifdef _KERNEL
813 	nvlist_t *ereport = NULL;
814 	nvlist_t *detector = NULL;
815 	zfs_ecksum_info_t *info;
816 
817 	if (!zfs_ereport_start(&ereport, &detector, FM_EREPORT_ZFS_CHECKSUM,
818 	    spa, vd, zb, zio, offset, length) || (ereport == NULL)) {
819 		return (SET_ERROR(EINVAL));
820 	}
821 
822 	info = annotate_ecksum(ereport, zbc, good_data, bad_data, length,
823 	    B_FALSE);
824 
825 	if (info != NULL)
826 		fm_ereport_post(ereport, EVCH_SLEEP);
827 
828 	fm_nvlist_destroy(ereport, FM_NVA_FREE);
829 	fm_nvlist_destroy(detector, FM_NVA_FREE);
830 
831 	if (info != NULL)
832 		kmem_free(info, sizeof (*info));
833 #endif
834 	return (rc);
835 }
836 
837 static void
838 zfs_post_common(spa_t *spa, vdev_t *vd, const char *name)
839 {
840 #ifdef _KERNEL
841 	nvlist_t *resource;
842 	char class[64];
843 
844 	if (spa_load_state(spa) == SPA_LOAD_TRYIMPORT)
845 		return;
846 
847 	if ((resource = fm_nvlist_create(NULL)) == NULL)
848 		return;
849 
850 	(void) snprintf(class, sizeof (class), "%s.%s.%s", FM_RSRC_RESOURCE,
851 	    ZFS_ERROR_CLASS, name);
852 	VERIFY(nvlist_add_uint8(resource, FM_VERSION, FM_RSRC_VERSION) == 0);
853 	VERIFY(nvlist_add_string(resource, FM_CLASS, class) == 0);
854 	VERIFY(nvlist_add_uint64(resource,
855 	    FM_EREPORT_PAYLOAD_ZFS_POOL_GUID, spa_guid(spa)) == 0);
856 	if (vd)
857 		VERIFY(nvlist_add_uint64(resource,
858 		    FM_EREPORT_PAYLOAD_ZFS_VDEV_GUID, vd->vdev_guid) == 0);
859 
860 	fm_ereport_post(resource, EVCH_SLEEP);
861 
862 	fm_nvlist_destroy(resource, FM_NVA_FREE);
863 #endif
864 }
865 
866 /*
867  * The 'resource.fs.zfs.removed' event is an internal signal that the given vdev
868  * has been removed from the system.  This will cause the DE to ignore any
869  * recent I/O errors, inferring that they are due to the asynchronous device
870  * removal.
871  */
872 void
873 zfs_post_remove(spa_t *spa, vdev_t *vd)
874 {
875 	zfs_post_common(spa, vd, FM_RESOURCE_REMOVED);
876 }
877 
878 /*
879  * The 'resource.fs.zfs.autoreplace' event is an internal signal that the pool
880  * has the 'autoreplace' property set, and therefore any broken vdevs will be
881  * handled by higher level logic, and no vdev fault should be generated.
882  */
883 void
884 zfs_post_autoreplace(spa_t *spa, vdev_t *vd)
885 {
886 	zfs_post_common(spa, vd, FM_RESOURCE_AUTOREPLACE);
887 }
888 
889 /*
890  * The 'resource.fs.zfs.statechange' event is an internal signal that the
891  * given vdev has transitioned its state to DEGRADED or HEALTHY.  This will
892  * cause the retire agent to repair any outstanding fault management cases
893  * open because the device was not found (fault.fs.zfs.device).
894  */
895 void
896 zfs_post_state_change(spa_t *spa, vdev_t *vd)
897 {
898 	zfs_post_common(spa, vd, FM_RESOURCE_STATECHANGE);
899 }
900