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