xref: /freebsd/sys/contrib/openzfs/module/zfs/zfs_fm.c (revision 15f0b8c309dea1dcb14d3e374686576ff68ac43f)
1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy  * CDDL HEADER START
3eda14cbcSMatt Macy  *
4eda14cbcSMatt Macy  * The contents of this file are subject to the terms of the
5eda14cbcSMatt Macy  * Common Development and Distribution License (the "License").
6eda14cbcSMatt Macy  * You may not use this file except in compliance with the License.
7eda14cbcSMatt Macy  *
8eda14cbcSMatt Macy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9271171e0SMartin Matuska  * or https://opensource.org/licenses/CDDL-1.0.
10eda14cbcSMatt Macy  * See the License for the specific language governing permissions
11eda14cbcSMatt Macy  * and limitations under the License.
12eda14cbcSMatt Macy  *
13eda14cbcSMatt Macy  * When distributing Covered Code, include this CDDL HEADER in each
14eda14cbcSMatt Macy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15eda14cbcSMatt Macy  * If applicable, add the following below this CDDL HEADER, with the
16eda14cbcSMatt Macy  * fields enclosed by brackets "[]" replaced with your own identifying
17eda14cbcSMatt Macy  * information: Portions Copyright [yyyy] [name of copyright owner]
18eda14cbcSMatt Macy  *
19eda14cbcSMatt Macy  * CDDL HEADER END
20eda14cbcSMatt Macy  */
21eda14cbcSMatt Macy /*
22eda14cbcSMatt Macy  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23eda14cbcSMatt Macy  * Use is subject to license terms.
24eda14cbcSMatt Macy  */
25eda14cbcSMatt Macy 
26eda14cbcSMatt Macy /*
27ba27dd8bSMartin Matuska  * Copyright (c) 2012,2021 by Delphix. All rights reserved.
28eda14cbcSMatt Macy  */
29eda14cbcSMatt Macy 
30eda14cbcSMatt Macy #include <sys/spa.h>
31eda14cbcSMatt Macy #include <sys/spa_impl.h>
32eda14cbcSMatt Macy #include <sys/vdev.h>
33eda14cbcSMatt Macy #include <sys/vdev_impl.h>
34eda14cbcSMatt Macy #include <sys/zio.h>
35eda14cbcSMatt Macy #include <sys/zio_checksum.h>
36eda14cbcSMatt Macy 
37eda14cbcSMatt Macy #include <sys/fm/fs/zfs.h>
38eda14cbcSMatt Macy #include <sys/fm/protocol.h>
39eda14cbcSMatt Macy #include <sys/fm/util.h>
40eda14cbcSMatt Macy #include <sys/sysevent.h>
41eda14cbcSMatt Macy 
42eda14cbcSMatt Macy /*
43eda14cbcSMatt Macy  * This general routine is responsible for generating all the different ZFS
44eda14cbcSMatt Macy  * ereports.  The payload is dependent on the class, and which arguments are
45eda14cbcSMatt Macy  * supplied to the function:
46eda14cbcSMatt Macy  *
47eda14cbcSMatt Macy  * 	EREPORT			POOL	VDEV	IO
48eda14cbcSMatt Macy  * 	block			X	X	X
49eda14cbcSMatt Macy  * 	data			X		X
50eda14cbcSMatt Macy  * 	device			X	X
51eda14cbcSMatt Macy  * 	pool			X
52eda14cbcSMatt Macy  *
53eda14cbcSMatt Macy  * If we are in a loading state, all errors are chained together by the same
54eda14cbcSMatt Macy  * SPA-wide ENA (Error Numeric Association).
55eda14cbcSMatt Macy  *
56eda14cbcSMatt Macy  * For isolated I/O requests, we get the ENA from the zio_t. The propagation
57eda14cbcSMatt Macy  * gets very complicated due to RAID-Z, gang blocks, and vdev caching.  We want
58eda14cbcSMatt Macy  * to chain together all ereports associated with a logical piece of data.  For
59eda14cbcSMatt Macy  * read I/Os, there  are basically three 'types' of I/O, which form a roughly
60eda14cbcSMatt Macy  * layered diagram:
61eda14cbcSMatt Macy  *
62eda14cbcSMatt Macy  * 	+---------------+
63eda14cbcSMatt Macy  * 	| Aggregate I/O |	No associated logical data or device
64eda14cbcSMatt Macy  * 	+---------------+
65eda14cbcSMatt Macy  *              |
66eda14cbcSMatt Macy  *              V
67eda14cbcSMatt Macy  * 	+---------------+	Reads associated with a piece of logical data.
68eda14cbcSMatt Macy  * 	|   Read I/O    |	This includes reads on behalf of RAID-Z,
69eda14cbcSMatt Macy  * 	+---------------+       mirrors, gang blocks, retries, etc.
70eda14cbcSMatt Macy  *              |
71eda14cbcSMatt Macy  *              V
72eda14cbcSMatt Macy  * 	+---------------+	Reads associated with a particular device, but
73eda14cbcSMatt Macy  * 	| Physical I/O  |	no logical data.  Issued as part of vdev caching
74eda14cbcSMatt Macy  * 	+---------------+	and I/O aggregation.
75eda14cbcSMatt Macy  *
76eda14cbcSMatt Macy  * Note that 'physical I/O' here is not the same terminology as used in the rest
77eda14cbcSMatt Macy  * of ZIO.  Typically, 'physical I/O' simply means that there is no attached
78eda14cbcSMatt Macy  * blockpointer.  But I/O with no associated block pointer can still be related
79eda14cbcSMatt Macy  * to a logical piece of data (i.e. RAID-Z requests).
80eda14cbcSMatt Macy  *
81eda14cbcSMatt Macy  * Purely physical I/O always have unique ENAs.  They are not related to a
82eda14cbcSMatt Macy  * particular piece of logical data, and therefore cannot be chained together.
83eda14cbcSMatt Macy  * We still generate an ereport, but the DE doesn't correlate it with any
84eda14cbcSMatt Macy  * logical piece of data.  When such an I/O fails, the delegated I/O requests
85eda14cbcSMatt Macy  * will issue a retry, which will trigger the 'real' ereport with the correct
86eda14cbcSMatt Macy  * ENA.
87eda14cbcSMatt Macy  *
88eda14cbcSMatt Macy  * We keep track of the ENA for a ZIO chain through the 'io_logical' member.
89eda14cbcSMatt Macy  * When a new logical I/O is issued, we set this to point to itself.  Child I/Os
90eda14cbcSMatt Macy  * then inherit this pointer, so that when it is first set subsequent failures
91eda14cbcSMatt Macy  * will use the same ENA.  For vdev cache fill and queue aggregation I/O,
92eda14cbcSMatt Macy  * this pointer is set to NULL, and no ereport will be generated (since it
93eda14cbcSMatt Macy  * doesn't actually correspond to any particular device or piece of data,
94eda14cbcSMatt Macy  * and the caller will always retry without caching or queueing anyway).
95eda14cbcSMatt Macy  *
96eda14cbcSMatt Macy  * For checksum errors, we want to include more information about the actual
97eda14cbcSMatt Macy  * error which occurs.  Accordingly, we build an ereport when the error is
98eda14cbcSMatt Macy  * noticed, but instead of sending it in immediately, we hang it off of the
99eda14cbcSMatt Macy  * io_cksum_report field of the logical IO.  When the logical IO completes
100eda14cbcSMatt Macy  * (successfully or not), zfs_ereport_finish_checksum() is called with the
101eda14cbcSMatt Macy  * good and bad versions of the buffer (if available), and we annotate the
102eda14cbcSMatt Macy  * ereport with information about the differences.
103eda14cbcSMatt Macy  */
1042c48331dSMatt Macy 
105eda14cbcSMatt Macy #ifdef _KERNEL
1062c48331dSMatt Macy /*
1072c48331dSMatt Macy  * Duplicate ereport Detection
1082c48331dSMatt Macy  *
1092c48331dSMatt Macy  * Some ereports are retained momentarily for detecting duplicates.  These
1102c48331dSMatt Macy  * are kept in a recent_events_node_t in both a time-ordered list and an AVL
1112c48331dSMatt Macy  * tree of recent unique ereports.
1122c48331dSMatt Macy  *
1132c48331dSMatt Macy  * The lifespan of these recent ereports is bounded (15 mins) and a cleaner
1142c48331dSMatt Macy  * task is used to purge stale entries.
1152c48331dSMatt Macy  */
1162c48331dSMatt Macy static list_t recent_events_list;
1172c48331dSMatt Macy static avl_tree_t recent_events_tree;
1182c48331dSMatt Macy static kmutex_t recent_events_lock;
1192c48331dSMatt Macy static taskqid_t recent_events_cleaner_tqid;
1202c48331dSMatt Macy 
1212c48331dSMatt Macy /*
1222c48331dSMatt Macy  * Each node is about 128 bytes so 2,000 would consume 1/4 MiB.
1232c48331dSMatt Macy  *
1242c48331dSMatt Macy  * This setting can be changed dynamically and setting it to zero
1252c48331dSMatt Macy  * disables duplicate detection.
1262c48331dSMatt Macy  */
127e92ffd9bSMartin Matuska static unsigned int zfs_zevent_retain_max = 2000;
1282c48331dSMatt Macy 
1292c48331dSMatt Macy /*
1302c48331dSMatt Macy  * The lifespan for a recent ereport entry. The default of 15 minutes is
1312c48331dSMatt Macy  * intended to outlive the zfs diagnosis engine's threshold of 10 errors
1322c48331dSMatt Macy  * over a period of 10 minutes.
1332c48331dSMatt Macy  */
134e92ffd9bSMartin Matuska static unsigned int zfs_zevent_retain_expire_secs = 900;
1352c48331dSMatt Macy 
1362c48331dSMatt Macy typedef enum zfs_subclass {
1372c48331dSMatt Macy 	ZSC_IO,
1382c48331dSMatt Macy 	ZSC_DATA,
1392c48331dSMatt Macy 	ZSC_CHECKSUM
1402c48331dSMatt Macy } zfs_subclass_t;
1412c48331dSMatt Macy 
1422c48331dSMatt Macy typedef struct {
1432c48331dSMatt Macy 	/* common criteria */
1442c48331dSMatt Macy 	uint64_t	re_pool_guid;
1452c48331dSMatt Macy 	uint64_t	re_vdev_guid;
1462c48331dSMatt Macy 	int		re_io_error;
1472c48331dSMatt Macy 	uint64_t	re_io_size;
1482c48331dSMatt Macy 	uint64_t	re_io_offset;
1492c48331dSMatt Macy 	zfs_subclass_t	re_subclass;
1502c48331dSMatt Macy 	zio_priority_t	re_io_priority;
1512c48331dSMatt Macy 
1522c48331dSMatt Macy 	/* logical zio criteria (optional) */
1532c48331dSMatt Macy 	zbookmark_phys_t re_io_bookmark;
1542c48331dSMatt Macy 
1552c48331dSMatt Macy 	/* internal state */
1562c48331dSMatt Macy 	avl_node_t	re_tree_link;
1572c48331dSMatt Macy 	list_node_t	re_list_link;
1582c48331dSMatt Macy 	uint64_t	re_timestamp;
1592c48331dSMatt Macy } recent_events_node_t;
1602c48331dSMatt Macy 
1612c48331dSMatt Macy static int
1622c48331dSMatt Macy recent_events_compare(const void *a, const void *b)
1632c48331dSMatt Macy {
1642c48331dSMatt Macy 	const recent_events_node_t *node1 = a;
1652c48331dSMatt Macy 	const recent_events_node_t *node2 = b;
1662c48331dSMatt Macy 	int cmp;
1672c48331dSMatt Macy 
1682c48331dSMatt Macy 	/*
1692c48331dSMatt Macy 	 * The comparison order here is somewhat arbitrary.
1702c48331dSMatt Macy 	 * What's important is that if every criteria matches, then it
1712c48331dSMatt Macy 	 * is a duplicate (i.e. compare returns 0)
1722c48331dSMatt Macy 	 */
1732c48331dSMatt Macy 	if ((cmp = TREE_CMP(node1->re_subclass, node2->re_subclass)) != 0)
1742c48331dSMatt Macy 		return (cmp);
1752c48331dSMatt Macy 	if ((cmp = TREE_CMP(node1->re_pool_guid, node2->re_pool_guid)) != 0)
1762c48331dSMatt Macy 		return (cmp);
1772c48331dSMatt Macy 	if ((cmp = TREE_CMP(node1->re_vdev_guid, node2->re_vdev_guid)) != 0)
1782c48331dSMatt Macy 		return (cmp);
1792c48331dSMatt Macy 	if ((cmp = TREE_CMP(node1->re_io_error, node2->re_io_error)) != 0)
1802c48331dSMatt Macy 		return (cmp);
1812c48331dSMatt Macy 	if ((cmp = TREE_CMP(node1->re_io_priority, node2->re_io_priority)) != 0)
1822c48331dSMatt Macy 		return (cmp);
1832c48331dSMatt Macy 	if ((cmp = TREE_CMP(node1->re_io_size, node2->re_io_size)) != 0)
1842c48331dSMatt Macy 		return (cmp);
1852c48331dSMatt Macy 	if ((cmp = TREE_CMP(node1->re_io_offset, node2->re_io_offset)) != 0)
1862c48331dSMatt Macy 		return (cmp);
1872c48331dSMatt Macy 
1882c48331dSMatt Macy 	const zbookmark_phys_t *zb1 = &node1->re_io_bookmark;
1892c48331dSMatt Macy 	const zbookmark_phys_t *zb2 = &node2->re_io_bookmark;
1902c48331dSMatt Macy 
1912c48331dSMatt Macy 	if ((cmp = TREE_CMP(zb1->zb_objset, zb2->zb_objset)) != 0)
1922c48331dSMatt Macy 		return (cmp);
1932c48331dSMatt Macy 	if ((cmp = TREE_CMP(zb1->zb_object, zb2->zb_object)) != 0)
1942c48331dSMatt Macy 		return (cmp);
1952c48331dSMatt Macy 	if ((cmp = TREE_CMP(zb1->zb_level, zb2->zb_level)) != 0)
1962c48331dSMatt Macy 		return (cmp);
1972c48331dSMatt Macy 	if ((cmp = TREE_CMP(zb1->zb_blkid, zb2->zb_blkid)) != 0)
1982c48331dSMatt Macy 		return (cmp);
1992c48331dSMatt Macy 
2002c48331dSMatt Macy 	return (0);
2012c48331dSMatt Macy }
2022c48331dSMatt Macy 
203*15f0b8c3SMartin Matuska /*
204*15f0b8c3SMartin Matuska  * workaround: vdev properties don't have inheritance
205*15f0b8c3SMartin Matuska  */
206*15f0b8c3SMartin Matuska static uint64_t
207*15f0b8c3SMartin Matuska vdev_prop_get_inherited(vdev_t *vd, vdev_prop_t prop)
208*15f0b8c3SMartin Matuska {
209*15f0b8c3SMartin Matuska 	uint64_t propdef, propval;
210*15f0b8c3SMartin Matuska 
211*15f0b8c3SMartin Matuska 	propdef = vdev_prop_default_numeric(prop);
212*15f0b8c3SMartin Matuska 	switch (prop) {
213*15f0b8c3SMartin Matuska 		case VDEV_PROP_CHECKSUM_N:
214*15f0b8c3SMartin Matuska 			propval = vd->vdev_checksum_n;
215*15f0b8c3SMartin Matuska 			break;
216*15f0b8c3SMartin Matuska 		case VDEV_PROP_CHECKSUM_T:
217*15f0b8c3SMartin Matuska 			propval = vd->vdev_checksum_t;
218*15f0b8c3SMartin Matuska 			break;
219*15f0b8c3SMartin Matuska 		case VDEV_PROP_IO_N:
220*15f0b8c3SMartin Matuska 			propval = vd->vdev_io_n;
221*15f0b8c3SMartin Matuska 			break;
222*15f0b8c3SMartin Matuska 		case VDEV_PROP_IO_T:
223*15f0b8c3SMartin Matuska 			propval = vd->vdev_io_t;
224*15f0b8c3SMartin Matuska 			break;
225*15f0b8c3SMartin Matuska 		default:
226*15f0b8c3SMartin Matuska 			propval = propdef;
227*15f0b8c3SMartin Matuska 			break;
228*15f0b8c3SMartin Matuska 	}
229*15f0b8c3SMartin Matuska 
230*15f0b8c3SMartin Matuska 	if (propval != propdef)
231*15f0b8c3SMartin Matuska 		return (propval);
232*15f0b8c3SMartin Matuska 
233*15f0b8c3SMartin Matuska 	if (vd->vdev_parent == NULL)
234*15f0b8c3SMartin Matuska 		return (propdef);
235*15f0b8c3SMartin Matuska 
236*15f0b8c3SMartin Matuska 	return (vdev_prop_get_inherited(vd->vdev_parent, prop));
237*15f0b8c3SMartin Matuska }
238*15f0b8c3SMartin Matuska 
2392c48331dSMatt Macy static void zfs_ereport_schedule_cleaner(void);
2402c48331dSMatt Macy 
2412c48331dSMatt Macy /*
2422c48331dSMatt Macy  * background task to clean stale recent event nodes.
2432c48331dSMatt Macy  */
2442c48331dSMatt Macy static void
2452c48331dSMatt Macy zfs_ereport_cleaner(void *arg)
2462c48331dSMatt Macy {
2472c48331dSMatt Macy 	recent_events_node_t *entry;
2482c48331dSMatt Macy 	uint64_t now = gethrtime();
2492c48331dSMatt Macy 
2502c48331dSMatt Macy 	/*
2512c48331dSMatt Macy 	 * purge expired entries
2522c48331dSMatt Macy 	 */
2532c48331dSMatt Macy 	mutex_enter(&recent_events_lock);
2542c48331dSMatt Macy 	while ((entry = list_tail(&recent_events_list)) != NULL) {
2552c48331dSMatt Macy 		uint64_t age = NSEC2SEC(now - entry->re_timestamp);
2562c48331dSMatt Macy 		if (age <= zfs_zevent_retain_expire_secs)
2572c48331dSMatt Macy 			break;
2582c48331dSMatt Macy 
2592c48331dSMatt Macy 		/* remove expired node */
2602c48331dSMatt Macy 		avl_remove(&recent_events_tree, entry);
2612c48331dSMatt Macy 		list_remove(&recent_events_list, entry);
2622c48331dSMatt Macy 		kmem_free(entry, sizeof (*entry));
2632c48331dSMatt Macy 	}
2642c48331dSMatt Macy 
2652c48331dSMatt Macy 	/* Restart the cleaner if more entries remain */
2662c48331dSMatt Macy 	recent_events_cleaner_tqid = 0;
2672c48331dSMatt Macy 	if (!list_is_empty(&recent_events_list))
2682c48331dSMatt Macy 		zfs_ereport_schedule_cleaner();
2692c48331dSMatt Macy 
2702c48331dSMatt Macy 	mutex_exit(&recent_events_lock);
2712c48331dSMatt Macy }
2722c48331dSMatt Macy 
2732c48331dSMatt Macy static void
2742c48331dSMatt Macy zfs_ereport_schedule_cleaner(void)
2752c48331dSMatt Macy {
2762c48331dSMatt Macy 	ASSERT(MUTEX_HELD(&recent_events_lock));
2772c48331dSMatt Macy 
2782c48331dSMatt Macy 	uint64_t timeout = SEC2NSEC(zfs_zevent_retain_expire_secs + 1);
2792c48331dSMatt Macy 
2802c48331dSMatt Macy 	recent_events_cleaner_tqid = taskq_dispatch_delay(
2812c48331dSMatt Macy 	    system_delay_taskq, zfs_ereport_cleaner, NULL, TQ_SLEEP,
2822c48331dSMatt Macy 	    ddi_get_lbolt() + NSEC_TO_TICK(timeout));
2832c48331dSMatt Macy }
2842c48331dSMatt Macy 
2852c48331dSMatt Macy /*
286ba27dd8bSMartin Matuska  * Clear entries for a given vdev or all vdevs in a pool when vdev == NULL
287ba27dd8bSMartin Matuska  */
288ba27dd8bSMartin Matuska void
289ba27dd8bSMartin Matuska zfs_ereport_clear(spa_t *spa, vdev_t *vd)
290ba27dd8bSMartin Matuska {
291ba27dd8bSMartin Matuska 	uint64_t vdev_guid, pool_guid;
292ba27dd8bSMartin Matuska 
293ba27dd8bSMartin Matuska 	ASSERT(vd != NULL || spa != NULL);
294ba27dd8bSMartin Matuska 	if (vd == NULL) {
295ba27dd8bSMartin Matuska 		vdev_guid = 0;
296ba27dd8bSMartin Matuska 		pool_guid = spa_guid(spa);
297ba27dd8bSMartin Matuska 	} else {
298ba27dd8bSMartin Matuska 		vdev_guid = vd->vdev_guid;
299ba27dd8bSMartin Matuska 		pool_guid = 0;
300ba27dd8bSMartin Matuska 	}
301ba27dd8bSMartin Matuska 
302ba27dd8bSMartin Matuska 	mutex_enter(&recent_events_lock);
303ba27dd8bSMartin Matuska 
304ba27dd8bSMartin Matuska 	recent_events_node_t *next = list_head(&recent_events_list);
305ba27dd8bSMartin Matuska 	while (next != NULL) {
306ba27dd8bSMartin Matuska 		recent_events_node_t *entry = next;
307ba27dd8bSMartin Matuska 
308ba27dd8bSMartin Matuska 		next = list_next(&recent_events_list, next);
309ba27dd8bSMartin Matuska 
310ba27dd8bSMartin Matuska 		if (entry->re_vdev_guid == vdev_guid ||
311ba27dd8bSMartin Matuska 		    entry->re_pool_guid == pool_guid) {
312ba27dd8bSMartin Matuska 			avl_remove(&recent_events_tree, entry);
313ba27dd8bSMartin Matuska 			list_remove(&recent_events_list, entry);
314ba27dd8bSMartin Matuska 			kmem_free(entry, sizeof (*entry));
315ba27dd8bSMartin Matuska 		}
316ba27dd8bSMartin Matuska 	}
317ba27dd8bSMartin Matuska 
318ba27dd8bSMartin Matuska 	mutex_exit(&recent_events_lock);
319ba27dd8bSMartin Matuska }
320ba27dd8bSMartin Matuska 
321ba27dd8bSMartin Matuska /*
3222c48331dSMatt Macy  * Check if an ereport would be a duplicate of one recently posted.
3232c48331dSMatt Macy  *
3242c48331dSMatt Macy  * An ereport is considered a duplicate if the set of criteria in
3252c48331dSMatt Macy  * recent_events_node_t all match.
3262c48331dSMatt Macy  *
3272c48331dSMatt Macy  * Only FM_EREPORT_ZFS_IO, FM_EREPORT_ZFS_DATA, and FM_EREPORT_ZFS_CHECKSUM
3282c48331dSMatt Macy  * are candidates for duplicate checking.
3292c48331dSMatt Macy  */
3302c48331dSMatt Macy static boolean_t
3312c48331dSMatt Macy zfs_ereport_is_duplicate(const char *subclass, spa_t *spa, vdev_t *vd,
3322c48331dSMatt Macy     const zbookmark_phys_t *zb, zio_t *zio, uint64_t offset, uint64_t size)
3332c48331dSMatt Macy {
3342c48331dSMatt Macy 	recent_events_node_t search = {0}, *entry;
3352c48331dSMatt Macy 
3362c48331dSMatt Macy 	if (vd == NULL || zio == NULL)
3372c48331dSMatt Macy 		return (B_FALSE);
3382c48331dSMatt Macy 
3392c48331dSMatt Macy 	if (zfs_zevent_retain_max == 0)
3402c48331dSMatt Macy 		return (B_FALSE);
3412c48331dSMatt Macy 
3422c48331dSMatt Macy 	if (strcmp(subclass, FM_EREPORT_ZFS_IO) == 0)
3432c48331dSMatt Macy 		search.re_subclass = ZSC_IO;
3442c48331dSMatt Macy 	else if (strcmp(subclass, FM_EREPORT_ZFS_DATA) == 0)
3452c48331dSMatt Macy 		search.re_subclass = ZSC_DATA;
3462c48331dSMatt Macy 	else if (strcmp(subclass, FM_EREPORT_ZFS_CHECKSUM) == 0)
3472c48331dSMatt Macy 		search.re_subclass = ZSC_CHECKSUM;
3482c48331dSMatt Macy 	else
3492c48331dSMatt Macy 		return (B_FALSE);
3502c48331dSMatt Macy 
3512c48331dSMatt Macy 	search.re_pool_guid = spa_guid(spa);
3522c48331dSMatt Macy 	search.re_vdev_guid = vd->vdev_guid;
3532c48331dSMatt Macy 	search.re_io_error = zio->io_error;
3542c48331dSMatt Macy 	search.re_io_priority = zio->io_priority;
3552c48331dSMatt Macy 	/* if size is supplied use it over what's in zio */
3562c48331dSMatt Macy 	if (size) {
3572c48331dSMatt Macy 		search.re_io_size = size;
3582c48331dSMatt Macy 		search.re_io_offset = offset;
3592c48331dSMatt Macy 	} else {
3602c48331dSMatt Macy 		search.re_io_size = zio->io_size;
3612c48331dSMatt Macy 		search.re_io_offset = zio->io_offset;
3622c48331dSMatt Macy 	}
3632c48331dSMatt Macy 
3642c48331dSMatt Macy 	/* grab optional logical zio criteria */
3652c48331dSMatt Macy 	if (zb != NULL) {
3662c48331dSMatt Macy 		search.re_io_bookmark.zb_objset = zb->zb_objset;
3672c48331dSMatt Macy 		search.re_io_bookmark.zb_object = zb->zb_object;
3682c48331dSMatt Macy 		search.re_io_bookmark.zb_level = zb->zb_level;
3692c48331dSMatt Macy 		search.re_io_bookmark.zb_blkid = zb->zb_blkid;
3702c48331dSMatt Macy 	}
3712c48331dSMatt Macy 
3722c48331dSMatt Macy 	uint64_t now = gethrtime();
3732c48331dSMatt Macy 
3742c48331dSMatt Macy 	mutex_enter(&recent_events_lock);
3752c48331dSMatt Macy 
3762c48331dSMatt Macy 	/* check if we have seen this one recently */
3772c48331dSMatt Macy 	entry = avl_find(&recent_events_tree, &search, NULL);
3782c48331dSMatt Macy 	if (entry != NULL) {
3792c48331dSMatt Macy 		uint64_t age = NSEC2SEC(now - entry->re_timestamp);
3802c48331dSMatt Macy 
3812c48331dSMatt Macy 		/*
3822c48331dSMatt Macy 		 * There is still an active cleaner (since we're here).
3832c48331dSMatt Macy 		 * Reset the last seen time for this duplicate entry
3842c48331dSMatt Macy 		 * so that its lifespand gets extended.
3852c48331dSMatt Macy 		 */
3862c48331dSMatt Macy 		list_remove(&recent_events_list, entry);
3872c48331dSMatt Macy 		list_insert_head(&recent_events_list, entry);
3882c48331dSMatt Macy 		entry->re_timestamp = now;
3892c48331dSMatt Macy 
3902c48331dSMatt Macy 		zfs_zevent_track_duplicate();
3912c48331dSMatt Macy 		mutex_exit(&recent_events_lock);
3922c48331dSMatt Macy 
3932c48331dSMatt Macy 		return (age <= zfs_zevent_retain_expire_secs);
3942c48331dSMatt Macy 	}
3952c48331dSMatt Macy 
3962c48331dSMatt Macy 	if (avl_numnodes(&recent_events_tree) >= zfs_zevent_retain_max) {
3972c48331dSMatt Macy 		/* recycle oldest node */
3982c48331dSMatt Macy 		entry = list_tail(&recent_events_list);
3992c48331dSMatt Macy 		ASSERT(entry != NULL);
4002c48331dSMatt Macy 		list_remove(&recent_events_list, entry);
4012c48331dSMatt Macy 		avl_remove(&recent_events_tree, entry);
4022c48331dSMatt Macy 	} else {
4032c48331dSMatt Macy 		entry = kmem_alloc(sizeof (recent_events_node_t), KM_SLEEP);
4042c48331dSMatt Macy 	}
4052c48331dSMatt Macy 
4062c48331dSMatt Macy 	/* record this as a recent ereport */
4072c48331dSMatt Macy 	*entry = search;
4082c48331dSMatt Macy 	avl_add(&recent_events_tree, entry);
4092c48331dSMatt Macy 	list_insert_head(&recent_events_list, entry);
4102c48331dSMatt Macy 	entry->re_timestamp = now;
4112c48331dSMatt Macy 
4122c48331dSMatt Macy 	/* Start a cleaner if not already scheduled */
4132c48331dSMatt Macy 	if (recent_events_cleaner_tqid == 0)
4142c48331dSMatt Macy 		zfs_ereport_schedule_cleaner();
4152c48331dSMatt Macy 
4162c48331dSMatt Macy 	mutex_exit(&recent_events_lock);
4172c48331dSMatt Macy 	return (B_FALSE);
4182c48331dSMatt Macy }
4192c48331dSMatt Macy 
420eda14cbcSMatt Macy void
421eda14cbcSMatt Macy zfs_zevent_post_cb(nvlist_t *nvl, nvlist_t *detector)
422eda14cbcSMatt Macy {
423eda14cbcSMatt Macy 	if (nvl)
424eda14cbcSMatt Macy 		fm_nvlist_destroy(nvl, FM_NVA_FREE);
425eda14cbcSMatt Macy 
426eda14cbcSMatt Macy 	if (detector)
427eda14cbcSMatt Macy 		fm_nvlist_destroy(detector, FM_NVA_FREE);
428eda14cbcSMatt Macy }
429eda14cbcSMatt Macy 
430eda14cbcSMatt Macy /*
43116038816SMartin Matuska  * We want to rate limit ZIO delay, deadman, and checksum events so as to not
43216038816SMartin Matuska  * flood zevent consumers when a disk is acting up.
433eda14cbcSMatt Macy  *
434eda14cbcSMatt Macy  * Returns 1 if we're ratelimiting, 0 if not.
435eda14cbcSMatt Macy  */
436eda14cbcSMatt Macy static int
437eda14cbcSMatt Macy zfs_is_ratelimiting_event(const char *subclass, vdev_t *vd)
438eda14cbcSMatt Macy {
439eda14cbcSMatt Macy 	int rc = 0;
440eda14cbcSMatt Macy 	/*
44116038816SMartin Matuska 	 * zfs_ratelimit() returns 1 if we're *not* ratelimiting and 0 if we
442eda14cbcSMatt Macy 	 * are.  Invert it to get our return value.
443eda14cbcSMatt Macy 	 */
444eda14cbcSMatt Macy 	if (strcmp(subclass, FM_EREPORT_ZFS_DELAY) == 0) {
445eda14cbcSMatt Macy 		rc = !zfs_ratelimit(&vd->vdev_delay_rl);
44616038816SMartin Matuska 	} else if (strcmp(subclass, FM_EREPORT_ZFS_DEADMAN) == 0) {
44716038816SMartin Matuska 		rc = !zfs_ratelimit(&vd->vdev_deadman_rl);
448eda14cbcSMatt Macy 	} else if (strcmp(subclass, FM_EREPORT_ZFS_CHECKSUM) == 0) {
449eda14cbcSMatt Macy 		rc = !zfs_ratelimit(&vd->vdev_checksum_rl);
450eda14cbcSMatt Macy 	}
451eda14cbcSMatt Macy 
452eda14cbcSMatt Macy 	if (rc)	{
453eda14cbcSMatt Macy 		/* We're rate limiting */
454eda14cbcSMatt Macy 		fm_erpt_dropped_increment();
455eda14cbcSMatt Macy 	}
456eda14cbcSMatt Macy 
457eda14cbcSMatt Macy 	return (rc);
458eda14cbcSMatt Macy }
459eda14cbcSMatt Macy 
460eda14cbcSMatt Macy /*
461eda14cbcSMatt Macy  * Return B_TRUE if the event actually posted, B_FALSE if not.
462eda14cbcSMatt Macy  */
463eda14cbcSMatt Macy static boolean_t
464eda14cbcSMatt Macy zfs_ereport_start(nvlist_t **ereport_out, nvlist_t **detector_out,
465eda14cbcSMatt Macy     const char *subclass, spa_t *spa, vdev_t *vd, const zbookmark_phys_t *zb,
466eda14cbcSMatt Macy     zio_t *zio, uint64_t stateoroffset, uint64_t size)
467eda14cbcSMatt Macy {
468eda14cbcSMatt Macy 	nvlist_t *ereport, *detector;
469eda14cbcSMatt Macy 
470eda14cbcSMatt Macy 	uint64_t ena;
471eda14cbcSMatt Macy 	char class[64];
472eda14cbcSMatt Macy 
473eda14cbcSMatt Macy 	if ((ereport = fm_nvlist_create(NULL)) == NULL)
474eda14cbcSMatt Macy 		return (B_FALSE);
475eda14cbcSMatt Macy 
476eda14cbcSMatt Macy 	if ((detector = fm_nvlist_create(NULL)) == NULL) {
477eda14cbcSMatt Macy 		fm_nvlist_destroy(ereport, FM_NVA_FREE);
478eda14cbcSMatt Macy 		return (B_FALSE);
479eda14cbcSMatt Macy 	}
480eda14cbcSMatt Macy 
481eda14cbcSMatt Macy 	/*
482eda14cbcSMatt Macy 	 * Serialize ereport generation
483eda14cbcSMatt Macy 	 */
484eda14cbcSMatt Macy 	mutex_enter(&spa->spa_errlist_lock);
485eda14cbcSMatt Macy 
486eda14cbcSMatt Macy 	/*
487eda14cbcSMatt Macy 	 * Determine the ENA to use for this event.  If we are in a loading
488eda14cbcSMatt Macy 	 * state, use a SPA-wide ENA.  Otherwise, if we are in an I/O state, use
489eda14cbcSMatt Macy 	 * a root zio-wide ENA.  Otherwise, simply use a unique ENA.
490eda14cbcSMatt Macy 	 */
491eda14cbcSMatt Macy 	if (spa_load_state(spa) != SPA_LOAD_NONE) {
492eda14cbcSMatt Macy 		if (spa->spa_ena == 0)
493eda14cbcSMatt Macy 			spa->spa_ena = fm_ena_generate(0, FM_ENA_FMT1);
494eda14cbcSMatt Macy 		ena = spa->spa_ena;
495eda14cbcSMatt Macy 	} else if (zio != NULL && zio->io_logical != NULL) {
496eda14cbcSMatt Macy 		if (zio->io_logical->io_ena == 0)
497eda14cbcSMatt Macy 			zio->io_logical->io_ena =
498eda14cbcSMatt Macy 			    fm_ena_generate(0, FM_ENA_FMT1);
499eda14cbcSMatt Macy 		ena = zio->io_logical->io_ena;
500eda14cbcSMatt Macy 	} else {
501eda14cbcSMatt Macy 		ena = fm_ena_generate(0, FM_ENA_FMT1);
502eda14cbcSMatt Macy 	}
503eda14cbcSMatt Macy 
504eda14cbcSMatt Macy 	/*
505eda14cbcSMatt Macy 	 * Construct the full class, detector, and other standard FMA fields.
506eda14cbcSMatt Macy 	 */
507eda14cbcSMatt Macy 	(void) snprintf(class, sizeof (class), "%s.%s",
508eda14cbcSMatt Macy 	    ZFS_ERROR_CLASS, subclass);
509eda14cbcSMatt Macy 
510eda14cbcSMatt Macy 	fm_fmri_zfs_set(detector, FM_ZFS_SCHEME_VERSION, spa_guid(spa),
511eda14cbcSMatt Macy 	    vd != NULL ? vd->vdev_guid : 0);
512eda14cbcSMatt Macy 
513eda14cbcSMatt Macy 	fm_ereport_set(ereport, FM_EREPORT_VERSION, class, ena, detector, NULL);
514eda14cbcSMatt Macy 
515eda14cbcSMatt Macy 	/*
516eda14cbcSMatt Macy 	 * Construct the per-ereport payload, depending on which parameters are
517eda14cbcSMatt Macy 	 * passed in.
518eda14cbcSMatt Macy 	 */
519eda14cbcSMatt Macy 
520eda14cbcSMatt Macy 	/*
521eda14cbcSMatt Macy 	 * Generic payload members common to all ereports.
522eda14cbcSMatt Macy 	 */
523eda14cbcSMatt Macy 	fm_payload_set(ereport,
524eda14cbcSMatt Macy 	    FM_EREPORT_PAYLOAD_ZFS_POOL, DATA_TYPE_STRING, spa_name(spa),
525eda14cbcSMatt Macy 	    FM_EREPORT_PAYLOAD_ZFS_POOL_GUID, DATA_TYPE_UINT64, spa_guid(spa),
526eda14cbcSMatt Macy 	    FM_EREPORT_PAYLOAD_ZFS_POOL_STATE, DATA_TYPE_UINT64,
527eda14cbcSMatt Macy 	    (uint64_t)spa_state(spa),
528eda14cbcSMatt Macy 	    FM_EREPORT_PAYLOAD_ZFS_POOL_CONTEXT, DATA_TYPE_INT32,
529eda14cbcSMatt Macy 	    (int32_t)spa_load_state(spa), NULL);
530eda14cbcSMatt Macy 
531eda14cbcSMatt Macy 	fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_POOL_FAILMODE,
532eda14cbcSMatt Macy 	    DATA_TYPE_STRING,
533eda14cbcSMatt Macy 	    spa_get_failmode(spa) == ZIO_FAILURE_MODE_WAIT ?
534eda14cbcSMatt Macy 	    FM_EREPORT_FAILMODE_WAIT :
535eda14cbcSMatt Macy 	    spa_get_failmode(spa) == ZIO_FAILURE_MODE_CONTINUE ?
536eda14cbcSMatt Macy 	    FM_EREPORT_FAILMODE_CONTINUE : FM_EREPORT_FAILMODE_PANIC,
537eda14cbcSMatt Macy 	    NULL);
538eda14cbcSMatt Macy 
539eda14cbcSMatt Macy 	if (vd != NULL) {
540eda14cbcSMatt Macy 		vdev_t *pvd = vd->vdev_parent;
541eda14cbcSMatt Macy 		vdev_queue_t *vq = &vd->vdev_queue;
542eda14cbcSMatt Macy 		vdev_stat_t *vs = &vd->vdev_stat;
543eda14cbcSMatt Macy 		vdev_t *spare_vd;
544eda14cbcSMatt Macy 		uint64_t *spare_guids;
545eda14cbcSMatt Macy 		char **spare_paths;
546eda14cbcSMatt Macy 		int i, spare_count;
547eda14cbcSMatt Macy 
548eda14cbcSMatt Macy 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_VDEV_GUID,
549eda14cbcSMatt Macy 		    DATA_TYPE_UINT64, vd->vdev_guid,
550eda14cbcSMatt Macy 		    FM_EREPORT_PAYLOAD_ZFS_VDEV_TYPE,
551eda14cbcSMatt Macy 		    DATA_TYPE_STRING, vd->vdev_ops->vdev_op_type, NULL);
552eda14cbcSMatt Macy 		if (vd->vdev_path != NULL)
553eda14cbcSMatt Macy 			fm_payload_set(ereport,
554eda14cbcSMatt Macy 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_PATH,
555eda14cbcSMatt Macy 			    DATA_TYPE_STRING, vd->vdev_path, NULL);
556eda14cbcSMatt Macy 		if (vd->vdev_devid != NULL)
557eda14cbcSMatt Macy 			fm_payload_set(ereport,
558eda14cbcSMatt Macy 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_DEVID,
559eda14cbcSMatt Macy 			    DATA_TYPE_STRING, vd->vdev_devid, NULL);
560eda14cbcSMatt Macy 		if (vd->vdev_fru != NULL)
561eda14cbcSMatt Macy 			fm_payload_set(ereport,
562eda14cbcSMatt Macy 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_FRU,
563eda14cbcSMatt Macy 			    DATA_TYPE_STRING, vd->vdev_fru, NULL);
564eda14cbcSMatt Macy 		if (vd->vdev_enc_sysfs_path != NULL)
565eda14cbcSMatt Macy 			fm_payload_set(ereport,
566eda14cbcSMatt Macy 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_ENC_SYSFS_PATH,
567eda14cbcSMatt Macy 			    DATA_TYPE_STRING, vd->vdev_enc_sysfs_path, NULL);
568eda14cbcSMatt Macy 		if (vd->vdev_ashift)
569eda14cbcSMatt Macy 			fm_payload_set(ereport,
570eda14cbcSMatt Macy 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_ASHIFT,
571eda14cbcSMatt Macy 			    DATA_TYPE_UINT64, vd->vdev_ashift, NULL);
572eda14cbcSMatt Macy 
573eda14cbcSMatt Macy 		if (vq != NULL) {
574eda14cbcSMatt Macy 			fm_payload_set(ereport,
575eda14cbcSMatt Macy 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_COMP_TS,
576eda14cbcSMatt Macy 			    DATA_TYPE_UINT64, vq->vq_io_complete_ts, NULL);
577eda14cbcSMatt Macy 			fm_payload_set(ereport,
578eda14cbcSMatt Macy 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_DELTA_TS,
579eda14cbcSMatt Macy 			    DATA_TYPE_UINT64, vq->vq_io_delta_ts, NULL);
580eda14cbcSMatt Macy 		}
581eda14cbcSMatt Macy 
582eda14cbcSMatt Macy 		if (vs != NULL) {
583eda14cbcSMatt Macy 			fm_payload_set(ereport,
584eda14cbcSMatt Macy 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_READ_ERRORS,
585eda14cbcSMatt Macy 			    DATA_TYPE_UINT64, vs->vs_read_errors,
586eda14cbcSMatt Macy 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_WRITE_ERRORS,
587eda14cbcSMatt Macy 			    DATA_TYPE_UINT64, vs->vs_write_errors,
588eda14cbcSMatt Macy 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_CKSUM_ERRORS,
589eda14cbcSMatt Macy 			    DATA_TYPE_UINT64, vs->vs_checksum_errors,
590eda14cbcSMatt Macy 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_DELAYS,
591eda14cbcSMatt Macy 			    DATA_TYPE_UINT64, vs->vs_slow_ios,
592eda14cbcSMatt Macy 			    NULL);
593eda14cbcSMatt Macy 		}
594eda14cbcSMatt Macy 
595eda14cbcSMatt Macy 		if (pvd != NULL) {
596eda14cbcSMatt Macy 			fm_payload_set(ereport,
597eda14cbcSMatt Macy 			    FM_EREPORT_PAYLOAD_ZFS_PARENT_GUID,
598eda14cbcSMatt Macy 			    DATA_TYPE_UINT64, pvd->vdev_guid,
599eda14cbcSMatt Macy 			    FM_EREPORT_PAYLOAD_ZFS_PARENT_TYPE,
600eda14cbcSMatt Macy 			    DATA_TYPE_STRING, pvd->vdev_ops->vdev_op_type,
601eda14cbcSMatt Macy 			    NULL);
602eda14cbcSMatt Macy 			if (pvd->vdev_path)
603eda14cbcSMatt Macy 				fm_payload_set(ereport,
604eda14cbcSMatt Macy 				    FM_EREPORT_PAYLOAD_ZFS_PARENT_PATH,
605eda14cbcSMatt Macy 				    DATA_TYPE_STRING, pvd->vdev_path, NULL);
606eda14cbcSMatt Macy 			if (pvd->vdev_devid)
607eda14cbcSMatt Macy 				fm_payload_set(ereport,
608eda14cbcSMatt Macy 				    FM_EREPORT_PAYLOAD_ZFS_PARENT_DEVID,
609eda14cbcSMatt Macy 				    DATA_TYPE_STRING, pvd->vdev_devid, NULL);
610eda14cbcSMatt Macy 		}
611eda14cbcSMatt Macy 
612eda14cbcSMatt Macy 		spare_count = spa->spa_spares.sav_count;
613eda14cbcSMatt Macy 		spare_paths = kmem_zalloc(sizeof (char *) * spare_count,
614eda14cbcSMatt Macy 		    KM_SLEEP);
615eda14cbcSMatt Macy 		spare_guids = kmem_zalloc(sizeof (uint64_t) * spare_count,
616eda14cbcSMatt Macy 		    KM_SLEEP);
617eda14cbcSMatt Macy 
618eda14cbcSMatt Macy 		for (i = 0; i < spare_count; i++) {
619eda14cbcSMatt Macy 			spare_vd = spa->spa_spares.sav_vdevs[i];
620eda14cbcSMatt Macy 			if (spare_vd) {
621eda14cbcSMatt Macy 				spare_paths[i] = spare_vd->vdev_path;
622eda14cbcSMatt Macy 				spare_guids[i] = spare_vd->vdev_guid;
623eda14cbcSMatt Macy 			}
624eda14cbcSMatt Macy 		}
625eda14cbcSMatt Macy 
626eda14cbcSMatt Macy 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_VDEV_SPARE_PATHS,
627eda14cbcSMatt Macy 		    DATA_TYPE_STRING_ARRAY, spare_count, spare_paths,
628eda14cbcSMatt Macy 		    FM_EREPORT_PAYLOAD_ZFS_VDEV_SPARE_GUIDS,
629eda14cbcSMatt Macy 		    DATA_TYPE_UINT64_ARRAY, spare_count, spare_guids, NULL);
630eda14cbcSMatt Macy 
631eda14cbcSMatt Macy 		kmem_free(spare_guids, sizeof (uint64_t) * spare_count);
632eda14cbcSMatt Macy 		kmem_free(spare_paths, sizeof (char *) * spare_count);
633eda14cbcSMatt Macy 	}
634eda14cbcSMatt Macy 
635eda14cbcSMatt Macy 	if (zio != NULL) {
636eda14cbcSMatt Macy 		/*
637eda14cbcSMatt Macy 		 * Payload common to all I/Os.
638eda14cbcSMatt Macy 		 */
639eda14cbcSMatt Macy 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_ERR,
640eda14cbcSMatt Macy 		    DATA_TYPE_INT32, zio->io_error, NULL);
641eda14cbcSMatt Macy 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_FLAGS,
642eda14cbcSMatt Macy 		    DATA_TYPE_INT32, zio->io_flags, NULL);
643eda14cbcSMatt Macy 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_STAGE,
644eda14cbcSMatt Macy 		    DATA_TYPE_UINT32, zio->io_stage, NULL);
645eda14cbcSMatt Macy 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_PIPELINE,
646eda14cbcSMatt Macy 		    DATA_TYPE_UINT32, zio->io_pipeline, NULL);
647eda14cbcSMatt Macy 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_DELAY,
648eda14cbcSMatt Macy 		    DATA_TYPE_UINT64, zio->io_delay, NULL);
649eda14cbcSMatt Macy 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_TIMESTAMP,
650eda14cbcSMatt Macy 		    DATA_TYPE_UINT64, zio->io_timestamp, NULL);
651eda14cbcSMatt Macy 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_DELTA,
652eda14cbcSMatt Macy 		    DATA_TYPE_UINT64, zio->io_delta, NULL);
6532c48331dSMatt Macy 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_PRIORITY,
6542c48331dSMatt Macy 		    DATA_TYPE_UINT32, zio->io_priority, NULL);
655eda14cbcSMatt Macy 
656eda14cbcSMatt Macy 		/*
657eda14cbcSMatt Macy 		 * If the 'size' parameter is non-zero, it indicates this is a
658eda14cbcSMatt Macy 		 * RAID-Z or other I/O where the physical offset and length are
659eda14cbcSMatt Macy 		 * provided for us, instead of within the zio_t.
660eda14cbcSMatt Macy 		 */
661eda14cbcSMatt Macy 		if (vd != NULL) {
662eda14cbcSMatt Macy 			if (size)
663eda14cbcSMatt Macy 				fm_payload_set(ereport,
664eda14cbcSMatt Macy 				    FM_EREPORT_PAYLOAD_ZFS_ZIO_OFFSET,
665eda14cbcSMatt Macy 				    DATA_TYPE_UINT64, stateoroffset,
666eda14cbcSMatt Macy 				    FM_EREPORT_PAYLOAD_ZFS_ZIO_SIZE,
667eda14cbcSMatt Macy 				    DATA_TYPE_UINT64, size, NULL);
668eda14cbcSMatt Macy 			else
669eda14cbcSMatt Macy 				fm_payload_set(ereport,
670eda14cbcSMatt Macy 				    FM_EREPORT_PAYLOAD_ZFS_ZIO_OFFSET,
671eda14cbcSMatt Macy 				    DATA_TYPE_UINT64, zio->io_offset,
672eda14cbcSMatt Macy 				    FM_EREPORT_PAYLOAD_ZFS_ZIO_SIZE,
673eda14cbcSMatt Macy 				    DATA_TYPE_UINT64, zio->io_size, NULL);
674eda14cbcSMatt Macy 		}
675eda14cbcSMatt Macy 	} else if (vd != NULL) {
676eda14cbcSMatt Macy 		/*
677eda14cbcSMatt Macy 		 * If we have a vdev but no zio, this is a device fault, and the
678eda14cbcSMatt Macy 		 * 'stateoroffset' parameter indicates the previous state of the
679eda14cbcSMatt Macy 		 * vdev.
680eda14cbcSMatt Macy 		 */
681eda14cbcSMatt Macy 		fm_payload_set(ereport,
682eda14cbcSMatt Macy 		    FM_EREPORT_PAYLOAD_ZFS_PREV_STATE,
683eda14cbcSMatt Macy 		    DATA_TYPE_UINT64, stateoroffset, NULL);
684eda14cbcSMatt Macy 	}
685eda14cbcSMatt Macy 
686eda14cbcSMatt Macy 	/*
687eda14cbcSMatt Macy 	 * Payload for I/Os with corresponding logical information.
688eda14cbcSMatt Macy 	 */
689eda14cbcSMatt Macy 	if (zb != NULL && (zio == NULL || zio->io_logical != NULL)) {
690eda14cbcSMatt Macy 		fm_payload_set(ereport,
691eda14cbcSMatt Macy 		    FM_EREPORT_PAYLOAD_ZFS_ZIO_OBJSET,
692eda14cbcSMatt Macy 		    DATA_TYPE_UINT64, zb->zb_objset,
693eda14cbcSMatt Macy 		    FM_EREPORT_PAYLOAD_ZFS_ZIO_OBJECT,
694eda14cbcSMatt Macy 		    DATA_TYPE_UINT64, zb->zb_object,
695eda14cbcSMatt Macy 		    FM_EREPORT_PAYLOAD_ZFS_ZIO_LEVEL,
696eda14cbcSMatt Macy 		    DATA_TYPE_INT64, zb->zb_level,
697eda14cbcSMatt Macy 		    FM_EREPORT_PAYLOAD_ZFS_ZIO_BLKID,
698eda14cbcSMatt Macy 		    DATA_TYPE_UINT64, zb->zb_blkid, NULL);
699eda14cbcSMatt Macy 	}
700eda14cbcSMatt Macy 
701*15f0b8c3SMartin Matuska 	/*
702*15f0b8c3SMartin Matuska 	 * Payload for tuning the zed
703*15f0b8c3SMartin Matuska 	 */
704*15f0b8c3SMartin Matuska 	if (vd != NULL && strcmp(subclass, FM_EREPORT_ZFS_CHECKSUM) == 0) {
705*15f0b8c3SMartin Matuska 		uint64_t cksum_n, cksum_t;
706*15f0b8c3SMartin Matuska 
707*15f0b8c3SMartin Matuska 		cksum_n = vdev_prop_get_inherited(vd, VDEV_PROP_CHECKSUM_N);
708*15f0b8c3SMartin Matuska 		if (cksum_n != vdev_prop_default_numeric(VDEV_PROP_CHECKSUM_N))
709*15f0b8c3SMartin Matuska 			fm_payload_set(ereport,
710*15f0b8c3SMartin Matuska 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_CKSUM_N,
711*15f0b8c3SMartin Matuska 			    DATA_TYPE_UINT64,
712*15f0b8c3SMartin Matuska 			    cksum_n,
713*15f0b8c3SMartin Matuska 			    NULL);
714*15f0b8c3SMartin Matuska 
715*15f0b8c3SMartin Matuska 		cksum_t = vdev_prop_get_inherited(vd, VDEV_PROP_CHECKSUM_T);
716*15f0b8c3SMartin Matuska 		if (cksum_t != vdev_prop_default_numeric(VDEV_PROP_CHECKSUM_T))
717*15f0b8c3SMartin Matuska 			fm_payload_set(ereport,
718*15f0b8c3SMartin Matuska 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_CKSUM_T,
719*15f0b8c3SMartin Matuska 			    DATA_TYPE_UINT64,
720*15f0b8c3SMartin Matuska 			    cksum_t,
721*15f0b8c3SMartin Matuska 			    NULL);
722*15f0b8c3SMartin Matuska 	}
723*15f0b8c3SMartin Matuska 
724*15f0b8c3SMartin Matuska 	if (vd != NULL && strcmp(subclass, FM_EREPORT_ZFS_IO) == 0) {
725*15f0b8c3SMartin Matuska 		uint64_t io_n, io_t;
726*15f0b8c3SMartin Matuska 
727*15f0b8c3SMartin Matuska 		io_n = vdev_prop_get_inherited(vd, VDEV_PROP_IO_N);
728*15f0b8c3SMartin Matuska 		if (io_n != vdev_prop_default_numeric(VDEV_PROP_IO_N))
729*15f0b8c3SMartin Matuska 			fm_payload_set(ereport,
730*15f0b8c3SMartin Matuska 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_IO_N,
731*15f0b8c3SMartin Matuska 			    DATA_TYPE_UINT64,
732*15f0b8c3SMartin Matuska 			    io_n,
733*15f0b8c3SMartin Matuska 			    NULL);
734*15f0b8c3SMartin Matuska 
735*15f0b8c3SMartin Matuska 		io_t = vdev_prop_get_inherited(vd, VDEV_PROP_IO_T);
736*15f0b8c3SMartin Matuska 		if (io_t != vdev_prop_default_numeric(VDEV_PROP_IO_T))
737*15f0b8c3SMartin Matuska 			fm_payload_set(ereport,
738*15f0b8c3SMartin Matuska 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_IO_T,
739*15f0b8c3SMartin Matuska 			    DATA_TYPE_UINT64,
740*15f0b8c3SMartin Matuska 			    io_t,
741*15f0b8c3SMartin Matuska 			    NULL);
742*15f0b8c3SMartin Matuska 	}
743*15f0b8c3SMartin Matuska 
744eda14cbcSMatt Macy 	mutex_exit(&spa->spa_errlist_lock);
745eda14cbcSMatt Macy 
746eda14cbcSMatt Macy 	*ereport_out = ereport;
747eda14cbcSMatt Macy 	*detector_out = detector;
748eda14cbcSMatt Macy 	return (B_TRUE);
749eda14cbcSMatt Macy }
750eda14cbcSMatt Macy 
751eda14cbcSMatt Macy /* if it's <= 128 bytes, save the corruption directly */
752eda14cbcSMatt Macy #define	ZFM_MAX_INLINE		(128 / sizeof (uint64_t))
753eda14cbcSMatt Macy 
754eda14cbcSMatt Macy #define	MAX_RANGES		16
755eda14cbcSMatt Macy 
756eda14cbcSMatt Macy typedef struct zfs_ecksum_info {
757eda14cbcSMatt Macy 	/* histograms of set and cleared bits by bit number in a 64-bit word */
758eda14cbcSMatt Macy 	uint32_t zei_histogram_set[sizeof (uint64_t) * NBBY];
759eda14cbcSMatt Macy 	uint32_t zei_histogram_cleared[sizeof (uint64_t) * NBBY];
760eda14cbcSMatt Macy 
761eda14cbcSMatt Macy 	/* inline arrays of bits set and cleared. */
762eda14cbcSMatt Macy 	uint64_t zei_bits_set[ZFM_MAX_INLINE];
763eda14cbcSMatt Macy 	uint64_t zei_bits_cleared[ZFM_MAX_INLINE];
764eda14cbcSMatt Macy 
765eda14cbcSMatt Macy 	/*
766eda14cbcSMatt Macy 	 * for each range, the number of bits set and cleared.  The Hamming
767eda14cbcSMatt Macy 	 * distance between the good and bad buffers is the sum of them all.
768eda14cbcSMatt Macy 	 */
769eda14cbcSMatt Macy 	uint32_t zei_range_sets[MAX_RANGES];
770eda14cbcSMatt Macy 	uint32_t zei_range_clears[MAX_RANGES];
771eda14cbcSMatt Macy 
772eda14cbcSMatt Macy 	struct zei_ranges {
773eda14cbcSMatt Macy 		uint32_t	zr_start;
774eda14cbcSMatt Macy 		uint32_t	zr_end;
775eda14cbcSMatt Macy 	} zei_ranges[MAX_RANGES];
776eda14cbcSMatt Macy 
777eda14cbcSMatt Macy 	size_t	zei_range_count;
778eda14cbcSMatt Macy 	uint32_t zei_mingap;
779eda14cbcSMatt Macy 	uint32_t zei_allowed_mingap;
780eda14cbcSMatt Macy 
781eda14cbcSMatt Macy } zfs_ecksum_info_t;
782eda14cbcSMatt Macy 
783eda14cbcSMatt Macy static void
784eda14cbcSMatt Macy update_histogram(uint64_t value_arg, uint32_t *hist, uint32_t *count)
785eda14cbcSMatt Macy {
786eda14cbcSMatt Macy 	size_t i;
787eda14cbcSMatt Macy 	size_t bits = 0;
788eda14cbcSMatt Macy 	uint64_t value = BE_64(value_arg);
789eda14cbcSMatt Macy 
790eda14cbcSMatt Macy 	/* We store the bits in big-endian (largest-first) order */
791eda14cbcSMatt Macy 	for (i = 0; i < 64; i++) {
792eda14cbcSMatt Macy 		if (value & (1ull << i)) {
793eda14cbcSMatt Macy 			hist[63 - i]++;
794eda14cbcSMatt Macy 			++bits;
795eda14cbcSMatt Macy 		}
796eda14cbcSMatt Macy 	}
797eda14cbcSMatt Macy 	/* update the count of bits changed */
798eda14cbcSMatt Macy 	*count += bits;
799eda14cbcSMatt Macy }
800eda14cbcSMatt Macy 
801eda14cbcSMatt Macy /*
802eda14cbcSMatt Macy  * We've now filled up the range array, and need to increase "mingap" and
803eda14cbcSMatt Macy  * shrink the range list accordingly.  zei_mingap is always the smallest
804eda14cbcSMatt Macy  * distance between array entries, so we set the new_allowed_gap to be
805eda14cbcSMatt Macy  * one greater than that.  We then go through the list, joining together
806eda14cbcSMatt Macy  * any ranges which are closer than the new_allowed_gap.
807eda14cbcSMatt Macy  *
808eda14cbcSMatt Macy  * By construction, there will be at least one.  We also update zei_mingap
809eda14cbcSMatt Macy  * to the new smallest gap, to prepare for our next invocation.
810eda14cbcSMatt Macy  */
811eda14cbcSMatt Macy static void
812eda14cbcSMatt Macy zei_shrink_ranges(zfs_ecksum_info_t *eip)
813eda14cbcSMatt Macy {
814eda14cbcSMatt Macy 	uint32_t mingap = UINT32_MAX;
815eda14cbcSMatt Macy 	uint32_t new_allowed_gap = eip->zei_mingap + 1;
816eda14cbcSMatt Macy 
817eda14cbcSMatt Macy 	size_t idx, output;
818eda14cbcSMatt Macy 	size_t max = eip->zei_range_count;
819eda14cbcSMatt Macy 
820eda14cbcSMatt Macy 	struct zei_ranges *r = eip->zei_ranges;
821eda14cbcSMatt Macy 
822eda14cbcSMatt Macy 	ASSERT3U(eip->zei_range_count, >, 0);
823eda14cbcSMatt Macy 	ASSERT3U(eip->zei_range_count, <=, MAX_RANGES);
824eda14cbcSMatt Macy 
825eda14cbcSMatt Macy 	output = idx = 0;
826eda14cbcSMatt Macy 	while (idx < max - 1) {
827eda14cbcSMatt Macy 		uint32_t start = r[idx].zr_start;
828eda14cbcSMatt Macy 		uint32_t end = r[idx].zr_end;
829eda14cbcSMatt Macy 
830eda14cbcSMatt Macy 		while (idx < max - 1) {
831eda14cbcSMatt Macy 			idx++;
832eda14cbcSMatt Macy 
833eda14cbcSMatt Macy 			uint32_t nstart = r[idx].zr_start;
834eda14cbcSMatt Macy 			uint32_t nend = r[idx].zr_end;
835eda14cbcSMatt Macy 
836eda14cbcSMatt Macy 			uint32_t gap = nstart - end;
837eda14cbcSMatt Macy 			if (gap < new_allowed_gap) {
838eda14cbcSMatt Macy 				end = nend;
839eda14cbcSMatt Macy 				continue;
840eda14cbcSMatt Macy 			}
841eda14cbcSMatt Macy 			if (gap < mingap)
842eda14cbcSMatt Macy 				mingap = gap;
843eda14cbcSMatt Macy 			break;
844eda14cbcSMatt Macy 		}
845eda14cbcSMatt Macy 		r[output].zr_start = start;
846eda14cbcSMatt Macy 		r[output].zr_end = end;
847eda14cbcSMatt Macy 		output++;
848eda14cbcSMatt Macy 	}
849eda14cbcSMatt Macy 	ASSERT3U(output, <, eip->zei_range_count);
850eda14cbcSMatt Macy 	eip->zei_range_count = output;
851eda14cbcSMatt Macy 	eip->zei_mingap = mingap;
852eda14cbcSMatt Macy 	eip->zei_allowed_mingap = new_allowed_gap;
853eda14cbcSMatt Macy }
854eda14cbcSMatt Macy 
855eda14cbcSMatt Macy static void
856eda14cbcSMatt Macy zei_add_range(zfs_ecksum_info_t *eip, int start, int end)
857eda14cbcSMatt Macy {
858eda14cbcSMatt Macy 	struct zei_ranges *r = eip->zei_ranges;
859eda14cbcSMatt Macy 	size_t count = eip->zei_range_count;
860eda14cbcSMatt Macy 
861eda14cbcSMatt Macy 	if (count >= MAX_RANGES) {
862eda14cbcSMatt Macy 		zei_shrink_ranges(eip);
863eda14cbcSMatt Macy 		count = eip->zei_range_count;
864eda14cbcSMatt Macy 	}
865eda14cbcSMatt Macy 	if (count == 0) {
866eda14cbcSMatt Macy 		eip->zei_mingap = UINT32_MAX;
867eda14cbcSMatt Macy 		eip->zei_allowed_mingap = 1;
868eda14cbcSMatt Macy 	} else {
869eda14cbcSMatt Macy 		int gap = start - r[count - 1].zr_end;
870eda14cbcSMatt Macy 
871eda14cbcSMatt Macy 		if (gap < eip->zei_allowed_mingap) {
872eda14cbcSMatt Macy 			r[count - 1].zr_end = end;
873eda14cbcSMatt Macy 			return;
874eda14cbcSMatt Macy 		}
875eda14cbcSMatt Macy 		if (gap < eip->zei_mingap)
876eda14cbcSMatt Macy 			eip->zei_mingap = gap;
877eda14cbcSMatt Macy 	}
878eda14cbcSMatt Macy 	r[count].zr_start = start;
879eda14cbcSMatt Macy 	r[count].zr_end = end;
880eda14cbcSMatt Macy 	eip->zei_range_count++;
881eda14cbcSMatt Macy }
882eda14cbcSMatt Macy 
883eda14cbcSMatt Macy static size_t
884eda14cbcSMatt Macy zei_range_total_size(zfs_ecksum_info_t *eip)
885eda14cbcSMatt Macy {
886eda14cbcSMatt Macy 	struct zei_ranges *r = eip->zei_ranges;
887eda14cbcSMatt Macy 	size_t count = eip->zei_range_count;
888eda14cbcSMatt Macy 	size_t result = 0;
889eda14cbcSMatt Macy 	size_t idx;
890eda14cbcSMatt Macy 
891eda14cbcSMatt Macy 	for (idx = 0; idx < count; idx++)
892eda14cbcSMatt Macy 		result += (r[idx].zr_end - r[idx].zr_start);
893eda14cbcSMatt Macy 
894eda14cbcSMatt Macy 	return (result);
895eda14cbcSMatt Macy }
896eda14cbcSMatt Macy 
897eda14cbcSMatt Macy static zfs_ecksum_info_t *
898eda14cbcSMatt Macy annotate_ecksum(nvlist_t *ereport, zio_bad_cksum_t *info,
899eda14cbcSMatt Macy     const abd_t *goodabd, const abd_t *badabd, size_t size,
900eda14cbcSMatt Macy     boolean_t drop_if_identical)
901eda14cbcSMatt Macy {
902eda14cbcSMatt Macy 	const uint64_t *good;
903eda14cbcSMatt Macy 	const uint64_t *bad;
904eda14cbcSMatt Macy 
905eda14cbcSMatt Macy 	size_t nui64s = size / sizeof (uint64_t);
906eda14cbcSMatt Macy 
907eda14cbcSMatt Macy 	size_t inline_size;
908eda14cbcSMatt Macy 	int no_inline = 0;
909eda14cbcSMatt Macy 	size_t idx;
910eda14cbcSMatt Macy 	size_t range;
911eda14cbcSMatt Macy 
912eda14cbcSMatt Macy 	size_t offset = 0;
913eda14cbcSMatt Macy 	ssize_t start = -1;
914eda14cbcSMatt Macy 
915eda14cbcSMatt Macy 	zfs_ecksum_info_t *eip = kmem_zalloc(sizeof (*eip), KM_SLEEP);
916eda14cbcSMatt Macy 
917eda14cbcSMatt Macy 	/* don't do any annotation for injected checksum errors */
918eda14cbcSMatt Macy 	if (info != NULL && info->zbc_injected)
919eda14cbcSMatt Macy 		return (eip);
920eda14cbcSMatt Macy 
921eda14cbcSMatt Macy 	if (info != NULL && info->zbc_has_cksum) {
922eda14cbcSMatt Macy 		fm_payload_set(ereport,
923eda14cbcSMatt Macy 		    FM_EREPORT_PAYLOAD_ZFS_CKSUM_EXPECTED,
924eda14cbcSMatt Macy 		    DATA_TYPE_UINT64_ARRAY,
925eda14cbcSMatt Macy 		    sizeof (info->zbc_expected) / sizeof (uint64_t),
926eda14cbcSMatt Macy 		    (uint64_t *)&info->zbc_expected,
927eda14cbcSMatt Macy 		    FM_EREPORT_PAYLOAD_ZFS_CKSUM_ACTUAL,
928eda14cbcSMatt Macy 		    DATA_TYPE_UINT64_ARRAY,
929eda14cbcSMatt Macy 		    sizeof (info->zbc_actual) / sizeof (uint64_t),
930eda14cbcSMatt Macy 		    (uint64_t *)&info->zbc_actual,
931eda14cbcSMatt Macy 		    FM_EREPORT_PAYLOAD_ZFS_CKSUM_ALGO,
932eda14cbcSMatt Macy 		    DATA_TYPE_STRING,
933eda14cbcSMatt Macy 		    info->zbc_checksum_name,
934eda14cbcSMatt Macy 		    NULL);
935eda14cbcSMatt Macy 
936eda14cbcSMatt Macy 		if (info->zbc_byteswapped) {
937eda14cbcSMatt Macy 			fm_payload_set(ereport,
938eda14cbcSMatt Macy 			    FM_EREPORT_PAYLOAD_ZFS_CKSUM_BYTESWAP,
939eda14cbcSMatt Macy 			    DATA_TYPE_BOOLEAN, 1,
940eda14cbcSMatt Macy 			    NULL);
941eda14cbcSMatt Macy 		}
942eda14cbcSMatt Macy 	}
943eda14cbcSMatt Macy 
944eda14cbcSMatt Macy 	if (badabd == NULL || goodabd == NULL)
945eda14cbcSMatt Macy 		return (eip);
946eda14cbcSMatt Macy 
947eda14cbcSMatt Macy 	ASSERT3U(nui64s, <=, UINT32_MAX);
948eda14cbcSMatt Macy 	ASSERT3U(size, ==, nui64s * sizeof (uint64_t));
949eda14cbcSMatt Macy 	ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
950eda14cbcSMatt Macy 	ASSERT3U(size, <=, UINT32_MAX);
951eda14cbcSMatt Macy 
952eda14cbcSMatt Macy 	good = (const uint64_t *) abd_borrow_buf_copy((abd_t *)goodabd, size);
953eda14cbcSMatt Macy 	bad = (const uint64_t *) abd_borrow_buf_copy((abd_t *)badabd, size);
954eda14cbcSMatt Macy 
955eda14cbcSMatt Macy 	/* build up the range list by comparing the two buffers. */
956eda14cbcSMatt Macy 	for (idx = 0; idx < nui64s; idx++) {
957eda14cbcSMatt Macy 		if (good[idx] == bad[idx]) {
958eda14cbcSMatt Macy 			if (start == -1)
959eda14cbcSMatt Macy 				continue;
960eda14cbcSMatt Macy 
961eda14cbcSMatt Macy 			zei_add_range(eip, start, idx);
962eda14cbcSMatt Macy 			start = -1;
963eda14cbcSMatt Macy 		} else {
964eda14cbcSMatt Macy 			if (start != -1)
965eda14cbcSMatt Macy 				continue;
966eda14cbcSMatt Macy 
967eda14cbcSMatt Macy 			start = idx;
968eda14cbcSMatt Macy 		}
969eda14cbcSMatt Macy 	}
970eda14cbcSMatt Macy 	if (start != -1)
971eda14cbcSMatt Macy 		zei_add_range(eip, start, idx);
972eda14cbcSMatt Macy 
973eda14cbcSMatt Macy 	/* See if it will fit in our inline buffers */
974eda14cbcSMatt Macy 	inline_size = zei_range_total_size(eip);
975eda14cbcSMatt Macy 	if (inline_size > ZFM_MAX_INLINE)
976eda14cbcSMatt Macy 		no_inline = 1;
977eda14cbcSMatt Macy 
978eda14cbcSMatt Macy 	/*
979eda14cbcSMatt Macy 	 * If there is no change and we want to drop if the buffers are
980eda14cbcSMatt Macy 	 * identical, do so.
981eda14cbcSMatt Macy 	 */
982eda14cbcSMatt Macy 	if (inline_size == 0 && drop_if_identical) {
983eda14cbcSMatt Macy 		kmem_free(eip, sizeof (*eip));
984eda14cbcSMatt Macy 		abd_return_buf((abd_t *)goodabd, (void *)good, size);
985eda14cbcSMatt Macy 		abd_return_buf((abd_t *)badabd, (void *)bad, size);
986eda14cbcSMatt Macy 		return (NULL);
987eda14cbcSMatt Macy 	}
988eda14cbcSMatt Macy 
989eda14cbcSMatt Macy 	/*
990eda14cbcSMatt Macy 	 * Now walk through the ranges, filling in the details of the
991eda14cbcSMatt Macy 	 * differences.  Also convert our uint64_t-array offsets to byte
992eda14cbcSMatt Macy 	 * offsets.
993eda14cbcSMatt Macy 	 */
994eda14cbcSMatt Macy 	for (range = 0; range < eip->zei_range_count; range++) {
995eda14cbcSMatt Macy 		size_t start = eip->zei_ranges[range].zr_start;
996eda14cbcSMatt Macy 		size_t end = eip->zei_ranges[range].zr_end;
997eda14cbcSMatt Macy 
998eda14cbcSMatt Macy 		for (idx = start; idx < end; idx++) {
999eda14cbcSMatt Macy 			uint64_t set, cleared;
1000eda14cbcSMatt Macy 
1001eda14cbcSMatt Macy 			// bits set in bad, but not in good
1002eda14cbcSMatt Macy 			set = ((~good[idx]) & bad[idx]);
1003eda14cbcSMatt Macy 			// bits set in good, but not in bad
1004eda14cbcSMatt Macy 			cleared = (good[idx] & (~bad[idx]));
1005eda14cbcSMatt Macy 
1006eda14cbcSMatt Macy 			if (!no_inline) {
1007eda14cbcSMatt Macy 				ASSERT3U(offset, <, inline_size);
1008eda14cbcSMatt Macy 				eip->zei_bits_set[offset] = set;
1009eda14cbcSMatt Macy 				eip->zei_bits_cleared[offset] = cleared;
1010eda14cbcSMatt Macy 				offset++;
1011eda14cbcSMatt Macy 			}
1012eda14cbcSMatt Macy 
1013eda14cbcSMatt Macy 			update_histogram(set, eip->zei_histogram_set,
1014eda14cbcSMatt Macy 			    &eip->zei_range_sets[range]);
1015eda14cbcSMatt Macy 			update_histogram(cleared, eip->zei_histogram_cleared,
1016eda14cbcSMatt Macy 			    &eip->zei_range_clears[range]);
1017eda14cbcSMatt Macy 		}
1018eda14cbcSMatt Macy 
1019eda14cbcSMatt Macy 		/* convert to byte offsets */
1020eda14cbcSMatt Macy 		eip->zei_ranges[range].zr_start	*= sizeof (uint64_t);
1021eda14cbcSMatt Macy 		eip->zei_ranges[range].zr_end	*= sizeof (uint64_t);
1022eda14cbcSMatt Macy 	}
1023eda14cbcSMatt Macy 
1024eda14cbcSMatt Macy 	abd_return_buf((abd_t *)goodabd, (void *)good, size);
1025eda14cbcSMatt Macy 	abd_return_buf((abd_t *)badabd, (void *)bad, size);
1026eda14cbcSMatt Macy 
1027eda14cbcSMatt Macy 	eip->zei_allowed_mingap	*= sizeof (uint64_t);
1028eda14cbcSMatt Macy 	inline_size		*= sizeof (uint64_t);
1029eda14cbcSMatt Macy 
1030eda14cbcSMatt Macy 	/* fill in ereport */
1031eda14cbcSMatt Macy 	fm_payload_set(ereport,
1032eda14cbcSMatt Macy 	    FM_EREPORT_PAYLOAD_ZFS_BAD_OFFSET_RANGES,
1033eda14cbcSMatt Macy 	    DATA_TYPE_UINT32_ARRAY, 2 * eip->zei_range_count,
1034eda14cbcSMatt Macy 	    (uint32_t *)eip->zei_ranges,
1035eda14cbcSMatt Macy 	    FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_MIN_GAP,
1036eda14cbcSMatt Macy 	    DATA_TYPE_UINT32, eip->zei_allowed_mingap,
1037eda14cbcSMatt Macy 	    FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_SETS,
1038eda14cbcSMatt Macy 	    DATA_TYPE_UINT32_ARRAY, eip->zei_range_count, eip->zei_range_sets,
1039eda14cbcSMatt Macy 	    FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_CLEARS,
1040eda14cbcSMatt Macy 	    DATA_TYPE_UINT32_ARRAY, eip->zei_range_count, eip->zei_range_clears,
1041eda14cbcSMatt Macy 	    NULL);
1042eda14cbcSMatt Macy 
1043eda14cbcSMatt Macy 	if (!no_inline) {
1044eda14cbcSMatt Macy 		fm_payload_set(ereport,
1045eda14cbcSMatt Macy 		    FM_EREPORT_PAYLOAD_ZFS_BAD_SET_BITS,
1046eda14cbcSMatt Macy 		    DATA_TYPE_UINT8_ARRAY,
1047eda14cbcSMatt Macy 		    inline_size, (uint8_t *)eip->zei_bits_set,
1048eda14cbcSMatt Macy 		    FM_EREPORT_PAYLOAD_ZFS_BAD_CLEARED_BITS,
1049eda14cbcSMatt Macy 		    DATA_TYPE_UINT8_ARRAY,
1050eda14cbcSMatt Macy 		    inline_size, (uint8_t *)eip->zei_bits_cleared,
1051eda14cbcSMatt Macy 		    NULL);
1052eda14cbcSMatt Macy 	} else {
1053eda14cbcSMatt Macy 		fm_payload_set(ereport,
1054eda14cbcSMatt Macy 		    FM_EREPORT_PAYLOAD_ZFS_BAD_SET_HISTOGRAM,
1055eda14cbcSMatt Macy 		    DATA_TYPE_UINT32_ARRAY,
1056eda14cbcSMatt Macy 		    NBBY * sizeof (uint64_t), eip->zei_histogram_set,
1057eda14cbcSMatt Macy 		    FM_EREPORT_PAYLOAD_ZFS_BAD_CLEARED_HISTOGRAM,
1058eda14cbcSMatt Macy 		    DATA_TYPE_UINT32_ARRAY,
1059eda14cbcSMatt Macy 		    NBBY * sizeof (uint64_t), eip->zei_histogram_cleared,
1060eda14cbcSMatt Macy 		    NULL);
1061eda14cbcSMatt Macy 	}
1062eda14cbcSMatt Macy 	return (eip);
1063eda14cbcSMatt Macy }
1064ba27dd8bSMartin Matuska #else
1065ba27dd8bSMartin Matuska void
1066ba27dd8bSMartin Matuska zfs_ereport_clear(spa_t *spa, vdev_t *vd)
1067ba27dd8bSMartin Matuska {
1068e92ffd9bSMartin Matuska 	(void) spa, (void) vd;
1069ba27dd8bSMartin Matuska }
1070eda14cbcSMatt Macy #endif
1071eda14cbcSMatt Macy 
1072eda14cbcSMatt Macy /*
1073eda14cbcSMatt Macy  * Make sure our event is still valid for the given zio/vdev/pool.  For example,
1074eda14cbcSMatt Macy  * we don't want to keep logging events for a faulted or missing vdev.
1075eda14cbcSMatt Macy  */
1076eda14cbcSMatt Macy boolean_t
1077eda14cbcSMatt Macy zfs_ereport_is_valid(const char *subclass, spa_t *spa, vdev_t *vd, zio_t *zio)
1078eda14cbcSMatt Macy {
1079eda14cbcSMatt Macy #ifdef _KERNEL
1080eda14cbcSMatt Macy 	/*
1081eda14cbcSMatt Macy 	 * If we are doing a spa_tryimport() or in recovery mode,
1082eda14cbcSMatt Macy 	 * ignore errors.
1083eda14cbcSMatt Macy 	 */
1084eda14cbcSMatt Macy 	if (spa_load_state(spa) == SPA_LOAD_TRYIMPORT ||
1085eda14cbcSMatt Macy 	    spa_load_state(spa) == SPA_LOAD_RECOVER)
1086eda14cbcSMatt Macy 		return (B_FALSE);
1087eda14cbcSMatt Macy 
1088eda14cbcSMatt Macy 	/*
1089eda14cbcSMatt Macy 	 * If we are in the middle of opening a pool, and the previous attempt
1090eda14cbcSMatt Macy 	 * failed, don't bother logging any new ereports - we're just going to
1091eda14cbcSMatt Macy 	 * get the same diagnosis anyway.
1092eda14cbcSMatt Macy 	 */
1093eda14cbcSMatt Macy 	if (spa_load_state(spa) != SPA_LOAD_NONE &&
1094eda14cbcSMatt Macy 	    spa->spa_last_open_failed)
1095eda14cbcSMatt Macy 		return (B_FALSE);
1096eda14cbcSMatt Macy 
1097eda14cbcSMatt Macy 	if (zio != NULL) {
1098eda14cbcSMatt Macy 		/*
1099eda14cbcSMatt Macy 		 * If this is not a read or write zio, ignore the error.  This
1100eda14cbcSMatt Macy 		 * can occur if the DKIOCFLUSHWRITECACHE ioctl fails.
1101eda14cbcSMatt Macy 		 */
1102eda14cbcSMatt Macy 		if (zio->io_type != ZIO_TYPE_READ &&
1103eda14cbcSMatt Macy 		    zio->io_type != ZIO_TYPE_WRITE)
1104eda14cbcSMatt Macy 			return (B_FALSE);
1105eda14cbcSMatt Macy 
1106eda14cbcSMatt Macy 		if (vd != NULL) {
1107eda14cbcSMatt Macy 			/*
1108eda14cbcSMatt Macy 			 * If the vdev has already been marked as failing due
1109eda14cbcSMatt Macy 			 * to a failed probe, then ignore any subsequent I/O
1110eda14cbcSMatt Macy 			 * errors, as the DE will automatically fault the vdev
1111eda14cbcSMatt Macy 			 * on the first such failure.  This also catches cases
1112eda14cbcSMatt Macy 			 * where vdev_remove_wanted is set and the device has
1113eda14cbcSMatt Macy 			 * not yet been asynchronously placed into the REMOVED
1114eda14cbcSMatt Macy 			 * state.
1115eda14cbcSMatt Macy 			 */
1116eda14cbcSMatt Macy 			if (zio->io_vd == vd && !vdev_accessible(vd, zio))
1117eda14cbcSMatt Macy 				return (B_FALSE);
1118eda14cbcSMatt Macy 
1119eda14cbcSMatt Macy 			/*
1120eda14cbcSMatt Macy 			 * Ignore checksum errors for reads from DTL regions of
1121eda14cbcSMatt Macy 			 * leaf vdevs.
1122eda14cbcSMatt Macy 			 */
1123eda14cbcSMatt Macy 			if (zio->io_type == ZIO_TYPE_READ &&
1124eda14cbcSMatt Macy 			    zio->io_error == ECKSUM &&
1125eda14cbcSMatt Macy 			    vd->vdev_ops->vdev_op_leaf &&
1126eda14cbcSMatt Macy 			    vdev_dtl_contains(vd, DTL_MISSING, zio->io_txg, 1))
1127eda14cbcSMatt Macy 				return (B_FALSE);
1128eda14cbcSMatt Macy 		}
1129eda14cbcSMatt Macy 	}
1130eda14cbcSMatt Macy 
1131eda14cbcSMatt Macy 	/*
1132eda14cbcSMatt Macy 	 * For probe failure, we want to avoid posting ereports if we've
1133eda14cbcSMatt Macy 	 * already removed the device in the meantime.
1134eda14cbcSMatt Macy 	 */
1135eda14cbcSMatt Macy 	if (vd != NULL &&
1136eda14cbcSMatt Macy 	    strcmp(subclass, FM_EREPORT_ZFS_PROBE_FAILURE) == 0 &&
1137eda14cbcSMatt Macy 	    (vd->vdev_remove_wanted || vd->vdev_state == VDEV_STATE_REMOVED))
1138eda14cbcSMatt Macy 		return (B_FALSE);
1139eda14cbcSMatt Macy 
1140eda14cbcSMatt Macy 	/* Ignore bogus delay events (like from ioctls or unqueued IOs) */
1141eda14cbcSMatt Macy 	if ((strcmp(subclass, FM_EREPORT_ZFS_DELAY) == 0) &&
1142eda14cbcSMatt Macy 	    (zio != NULL) && (!zio->io_timestamp)) {
1143eda14cbcSMatt Macy 		return (B_FALSE);
1144eda14cbcSMatt Macy 	}
1145e92ffd9bSMartin Matuska #else
1146e92ffd9bSMartin Matuska 	(void) subclass, (void) spa, (void) vd, (void) zio;
1147eda14cbcSMatt Macy #endif
1148eda14cbcSMatt Macy 	return (B_TRUE);
1149eda14cbcSMatt Macy }
1150eda14cbcSMatt Macy 
1151eda14cbcSMatt Macy /*
11522c48331dSMatt Macy  * Post an ereport for the given subclass
11532c48331dSMatt Macy  *
11542c48331dSMatt Macy  * Returns
11552c48331dSMatt Macy  * - 0 if an event was posted
11562c48331dSMatt Macy  * - EINVAL if there was a problem posting event
11572c48331dSMatt Macy  * - EBUSY if the event was rate limited
11582c48331dSMatt Macy  * - EALREADY if the event was already posted (duplicate)
1159eda14cbcSMatt Macy  */
1160eda14cbcSMatt Macy int
1161eda14cbcSMatt Macy zfs_ereport_post(const char *subclass, spa_t *spa, vdev_t *vd,
11622c48331dSMatt Macy     const zbookmark_phys_t *zb, zio_t *zio, uint64_t state)
1163eda14cbcSMatt Macy {
1164eda14cbcSMatt Macy 	int rc = 0;
1165eda14cbcSMatt Macy #ifdef _KERNEL
1166eda14cbcSMatt Macy 	nvlist_t *ereport = NULL;
1167eda14cbcSMatt Macy 	nvlist_t *detector = NULL;
1168eda14cbcSMatt Macy 
11692c48331dSMatt Macy 	if (!zfs_ereport_is_valid(subclass, spa, vd, zio))
11702c48331dSMatt Macy 		return (EINVAL);
11712c48331dSMatt Macy 
11722c48331dSMatt Macy 	if (zfs_ereport_is_duplicate(subclass, spa, vd, zb, zio, 0, 0))
11732c48331dSMatt Macy 		return (SET_ERROR(EALREADY));
11742c48331dSMatt Macy 
1175eda14cbcSMatt Macy 	if (zfs_is_ratelimiting_event(subclass, vd))
1176eda14cbcSMatt Macy 		return (SET_ERROR(EBUSY));
1177eda14cbcSMatt Macy 
1178eda14cbcSMatt Macy 	if (!zfs_ereport_start(&ereport, &detector, subclass, spa, vd,
11792c48331dSMatt Macy 	    zb, zio, state, 0))
1180eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));	/* couldn't post event */
1181eda14cbcSMatt Macy 
1182eda14cbcSMatt Macy 	if (ereport == NULL)
1183eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
1184eda14cbcSMatt Macy 
1185eda14cbcSMatt Macy 	/* Cleanup is handled by the callback function */
1186eda14cbcSMatt Macy 	rc = zfs_zevent_post(ereport, detector, zfs_zevent_post_cb);
1187e92ffd9bSMartin Matuska #else
1188e92ffd9bSMartin Matuska 	(void) subclass, (void) spa, (void) vd, (void) zb, (void) zio,
1189e92ffd9bSMartin Matuska 	    (void) state;
1190eda14cbcSMatt Macy #endif
1191eda14cbcSMatt Macy 	return (rc);
1192eda14cbcSMatt Macy }
1193eda14cbcSMatt Macy 
11942c48331dSMatt Macy /*
11952c48331dSMatt Macy  * Prepare a checksum ereport
11962c48331dSMatt Macy  *
11972c48331dSMatt Macy  * Returns
11982c48331dSMatt Macy  * - 0 if an event was posted
11992c48331dSMatt Macy  * - EINVAL if there was a problem posting event
12002c48331dSMatt Macy  * - EBUSY if the event was rate limited
12012c48331dSMatt Macy  * - EALREADY if the event was already posted (duplicate)
12022c48331dSMatt Macy  */
12032c48331dSMatt Macy int
1204eda14cbcSMatt Macy zfs_ereport_start_checksum(spa_t *spa, vdev_t *vd, const zbookmark_phys_t *zb,
1205f9693befSMartin Matuska     struct zio *zio, uint64_t offset, uint64_t length, zio_bad_cksum_t *info)
1206eda14cbcSMatt Macy {
1207eda14cbcSMatt Macy 	zio_cksum_report_t *report;
1208eda14cbcSMatt Macy 
1209eda14cbcSMatt Macy #ifdef _KERNEL
12102c48331dSMatt Macy 	if (!zfs_ereport_is_valid(FM_EREPORT_ZFS_CHECKSUM, spa, vd, zio))
12112c48331dSMatt Macy 		return (SET_ERROR(EINVAL));
12122c48331dSMatt Macy 
12132c48331dSMatt Macy 	if (zfs_ereport_is_duplicate(FM_EREPORT_ZFS_CHECKSUM, spa, vd, zb, zio,
12142c48331dSMatt Macy 	    offset, length))
12152c48331dSMatt Macy 		return (SET_ERROR(EALREADY));
12162c48331dSMatt Macy 
1217eda14cbcSMatt Macy 	if (zfs_is_ratelimiting_event(FM_EREPORT_ZFS_CHECKSUM, vd))
12182c48331dSMatt Macy 		return (SET_ERROR(EBUSY));
1219e92ffd9bSMartin Matuska #else
1220e92ffd9bSMartin Matuska 	(void) zb, (void) offset;
1221eda14cbcSMatt Macy #endif
1222eda14cbcSMatt Macy 
1223eda14cbcSMatt Macy 	report = kmem_zalloc(sizeof (*report), KM_SLEEP);
1224eda14cbcSMatt Macy 
1225f9693befSMartin Matuska 	zio_vsd_default_cksum_report(zio, report);
1226eda14cbcSMatt Macy 
1227eda14cbcSMatt Macy 	/* copy the checksum failure information if it was provided */
1228eda14cbcSMatt Macy 	if (info != NULL) {
1229eda14cbcSMatt Macy 		report->zcr_ckinfo = kmem_zalloc(sizeof (*info), KM_SLEEP);
1230da5137abSMartin Matuska 		memcpy(report->zcr_ckinfo, info, sizeof (*info));
1231eda14cbcSMatt Macy 	}
1232eda14cbcSMatt Macy 
12337877fdebSMatt Macy 	report->zcr_sector = 1ULL << vd->vdev_top->vdev_ashift;
12347877fdebSMatt Macy 	report->zcr_align =
12357877fdebSMatt Macy 	    vdev_psize_to_asize(vd->vdev_top, report->zcr_sector);
1236eda14cbcSMatt Macy 	report->zcr_length = length;
1237eda14cbcSMatt Macy 
1238eda14cbcSMatt Macy #ifdef _KERNEL
1239eac7052fSMatt Macy 	(void) zfs_ereport_start(&report->zcr_ereport, &report->zcr_detector,
1240eda14cbcSMatt Macy 	    FM_EREPORT_ZFS_CHECKSUM, spa, vd, zb, zio, offset, length);
1241eda14cbcSMatt Macy 
1242eda14cbcSMatt Macy 	if (report->zcr_ereport == NULL) {
1243eda14cbcSMatt Macy 		zfs_ereport_free_checksum(report);
12442c48331dSMatt Macy 		return (0);
1245eda14cbcSMatt Macy 	}
1246eda14cbcSMatt Macy #endif
1247eda14cbcSMatt Macy 
1248eda14cbcSMatt Macy 	mutex_enter(&spa->spa_errlist_lock);
1249eda14cbcSMatt Macy 	report->zcr_next = zio->io_logical->io_cksum_report;
1250eda14cbcSMatt Macy 	zio->io_logical->io_cksum_report = report;
1251eda14cbcSMatt Macy 	mutex_exit(&spa->spa_errlist_lock);
12522c48331dSMatt Macy 	return (0);
1253eda14cbcSMatt Macy }
1254eda14cbcSMatt Macy 
1255eda14cbcSMatt Macy void
1256eda14cbcSMatt Macy zfs_ereport_finish_checksum(zio_cksum_report_t *report, const abd_t *good_data,
1257eda14cbcSMatt Macy     const abd_t *bad_data, boolean_t drop_if_identical)
1258eda14cbcSMatt Macy {
1259eda14cbcSMatt Macy #ifdef _KERNEL
1260eda14cbcSMatt Macy 	zfs_ecksum_info_t *info;
1261eda14cbcSMatt Macy 
1262eda14cbcSMatt Macy 	info = annotate_ecksum(report->zcr_ereport, report->zcr_ckinfo,
1263eda14cbcSMatt Macy 	    good_data, bad_data, report->zcr_length, drop_if_identical);
1264eda14cbcSMatt Macy 	if (info != NULL)
1265eda14cbcSMatt Macy 		zfs_zevent_post(report->zcr_ereport,
1266eda14cbcSMatt Macy 		    report->zcr_detector, zfs_zevent_post_cb);
1267eda14cbcSMatt Macy 	else
1268eda14cbcSMatt Macy 		zfs_zevent_post_cb(report->zcr_ereport, report->zcr_detector);
1269eda14cbcSMatt Macy 
1270eda14cbcSMatt Macy 	report->zcr_ereport = report->zcr_detector = NULL;
1271eda14cbcSMatt Macy 	if (info != NULL)
1272eda14cbcSMatt Macy 		kmem_free(info, sizeof (*info));
1273e92ffd9bSMartin Matuska #else
1274e92ffd9bSMartin Matuska 	(void) report, (void) good_data, (void) bad_data,
1275e92ffd9bSMartin Matuska 	    (void) drop_if_identical;
1276eda14cbcSMatt Macy #endif
1277eda14cbcSMatt Macy }
1278eda14cbcSMatt Macy 
1279eda14cbcSMatt Macy void
1280eda14cbcSMatt Macy zfs_ereport_free_checksum(zio_cksum_report_t *rpt)
1281eda14cbcSMatt Macy {
1282eda14cbcSMatt Macy #ifdef _KERNEL
1283eda14cbcSMatt Macy 	if (rpt->zcr_ereport != NULL) {
1284eda14cbcSMatt Macy 		fm_nvlist_destroy(rpt->zcr_ereport,
1285eda14cbcSMatt Macy 		    FM_NVA_FREE);
1286eda14cbcSMatt Macy 		fm_nvlist_destroy(rpt->zcr_detector,
1287eda14cbcSMatt Macy 		    FM_NVA_FREE);
1288eda14cbcSMatt Macy 	}
1289eda14cbcSMatt Macy #endif
1290eda14cbcSMatt Macy 	rpt->zcr_free(rpt->zcr_cbdata, rpt->zcr_cbinfo);
1291eda14cbcSMatt Macy 
1292eda14cbcSMatt Macy 	if (rpt->zcr_ckinfo != NULL)
1293eda14cbcSMatt Macy 		kmem_free(rpt->zcr_ckinfo, sizeof (*rpt->zcr_ckinfo));
1294eda14cbcSMatt Macy 
1295eda14cbcSMatt Macy 	kmem_free(rpt, sizeof (*rpt));
1296eda14cbcSMatt Macy }
1297eda14cbcSMatt Macy 
12982c48331dSMatt Macy /*
12992c48331dSMatt Macy  * Post a checksum ereport
13002c48331dSMatt Macy  *
13012c48331dSMatt Macy  * Returns
13022c48331dSMatt Macy  * - 0 if an event was posted
13032c48331dSMatt Macy  * - EINVAL if there was a problem posting event
13042c48331dSMatt Macy  * - EBUSY if the event was rate limited
13052c48331dSMatt Macy  * - EALREADY if the event was already posted (duplicate)
13062c48331dSMatt Macy  */
1307eda14cbcSMatt Macy int
1308eda14cbcSMatt Macy zfs_ereport_post_checksum(spa_t *spa, vdev_t *vd, const zbookmark_phys_t *zb,
1309eda14cbcSMatt Macy     struct zio *zio, uint64_t offset, uint64_t length,
1310eda14cbcSMatt Macy     const abd_t *good_data, const abd_t *bad_data, zio_bad_cksum_t *zbc)
1311eda14cbcSMatt Macy {
1312eda14cbcSMatt Macy 	int rc = 0;
1313eda14cbcSMatt Macy #ifdef _KERNEL
1314eda14cbcSMatt Macy 	nvlist_t *ereport = NULL;
1315eda14cbcSMatt Macy 	nvlist_t *detector = NULL;
1316eda14cbcSMatt Macy 	zfs_ecksum_info_t *info;
1317eda14cbcSMatt Macy 
13182c48331dSMatt Macy 	if (!zfs_ereport_is_valid(FM_EREPORT_ZFS_CHECKSUM, spa, vd, zio))
13192c48331dSMatt Macy 		return (SET_ERROR(EINVAL));
13202c48331dSMatt Macy 
13212c48331dSMatt Macy 	if (zfs_ereport_is_duplicate(FM_EREPORT_ZFS_CHECKSUM, spa, vd, zb, zio,
13222c48331dSMatt Macy 	    offset, length))
13232c48331dSMatt Macy 		return (SET_ERROR(EALREADY));
13242c48331dSMatt Macy 
1325eda14cbcSMatt Macy 	if (zfs_is_ratelimiting_event(FM_EREPORT_ZFS_CHECKSUM, vd))
13262c48331dSMatt Macy 		return (SET_ERROR(EBUSY));
1327eda14cbcSMatt Macy 
1328eda14cbcSMatt Macy 	if (!zfs_ereport_start(&ereport, &detector, FM_EREPORT_ZFS_CHECKSUM,
1329eda14cbcSMatt Macy 	    spa, vd, zb, zio, offset, length) || (ereport == NULL)) {
1330eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
1331eda14cbcSMatt Macy 	}
1332eda14cbcSMatt Macy 
1333eda14cbcSMatt Macy 	info = annotate_ecksum(ereport, zbc, good_data, bad_data, length,
1334eda14cbcSMatt Macy 	    B_FALSE);
1335eda14cbcSMatt Macy 
1336eda14cbcSMatt Macy 	if (info != NULL) {
1337eda14cbcSMatt Macy 		rc = zfs_zevent_post(ereport, detector, zfs_zevent_post_cb);
1338eda14cbcSMatt Macy 		kmem_free(info, sizeof (*info));
1339eda14cbcSMatt Macy 	}
1340e92ffd9bSMartin Matuska #else
1341e92ffd9bSMartin Matuska 	(void) spa, (void) vd, (void) zb, (void) zio, (void) offset,
1342e92ffd9bSMartin Matuska 	    (void) length, (void) good_data, (void) bad_data, (void) zbc;
1343eda14cbcSMatt Macy #endif
1344eda14cbcSMatt Macy 	return (rc);
1345eda14cbcSMatt Macy }
1346eda14cbcSMatt Macy 
1347eda14cbcSMatt Macy /*
1348eda14cbcSMatt Macy  * The 'sysevent.fs.zfs.*' events are signals posted to notify user space of
1349eda14cbcSMatt Macy  * change in the pool.  All sysevents are listed in sys/sysevent/eventdefs.h
1350eda14cbcSMatt Macy  * and are designed to be consumed by the ZFS Event Daemon (ZED).  For
1351eda14cbcSMatt Macy  * additional details refer to the zed(8) man page.
1352eda14cbcSMatt Macy  */
1353eda14cbcSMatt Macy nvlist_t *
1354eda14cbcSMatt Macy zfs_event_create(spa_t *spa, vdev_t *vd, const char *type, const char *name,
1355eda14cbcSMatt Macy     nvlist_t *aux)
1356eda14cbcSMatt Macy {
1357eda14cbcSMatt Macy 	nvlist_t *resource = NULL;
1358eda14cbcSMatt Macy #ifdef _KERNEL
1359eda14cbcSMatt Macy 	char class[64];
1360eda14cbcSMatt Macy 
1361eda14cbcSMatt Macy 	if (spa_load_state(spa) == SPA_LOAD_TRYIMPORT)
1362eda14cbcSMatt Macy 		return (NULL);
1363eda14cbcSMatt Macy 
1364eda14cbcSMatt Macy 	if ((resource = fm_nvlist_create(NULL)) == NULL)
1365eda14cbcSMatt Macy 		return (NULL);
1366eda14cbcSMatt Macy 
1367eda14cbcSMatt Macy 	(void) snprintf(class, sizeof (class), "%s.%s.%s", type,
1368eda14cbcSMatt Macy 	    ZFS_ERROR_CLASS, name);
1369eda14cbcSMatt Macy 	VERIFY0(nvlist_add_uint8(resource, FM_VERSION, FM_RSRC_VERSION));
1370eda14cbcSMatt Macy 	VERIFY0(nvlist_add_string(resource, FM_CLASS, class));
1371eda14cbcSMatt Macy 	VERIFY0(nvlist_add_string(resource,
1372eda14cbcSMatt Macy 	    FM_EREPORT_PAYLOAD_ZFS_POOL, spa_name(spa)));
1373eda14cbcSMatt Macy 	VERIFY0(nvlist_add_uint64(resource,
1374eda14cbcSMatt Macy 	    FM_EREPORT_PAYLOAD_ZFS_POOL_GUID, spa_guid(spa)));
1375eda14cbcSMatt Macy 	VERIFY0(nvlist_add_uint64(resource,
1376eda14cbcSMatt Macy 	    FM_EREPORT_PAYLOAD_ZFS_POOL_STATE, spa_state(spa)));
1377eda14cbcSMatt Macy 	VERIFY0(nvlist_add_int32(resource,
1378eda14cbcSMatt Macy 	    FM_EREPORT_PAYLOAD_ZFS_POOL_CONTEXT, spa_load_state(spa)));
1379eda14cbcSMatt Macy 
1380eda14cbcSMatt Macy 	if (vd) {
1381eda14cbcSMatt Macy 		VERIFY0(nvlist_add_uint64(resource,
1382eda14cbcSMatt Macy 		    FM_EREPORT_PAYLOAD_ZFS_VDEV_GUID, vd->vdev_guid));
1383eda14cbcSMatt Macy 		VERIFY0(nvlist_add_uint64(resource,
1384eda14cbcSMatt Macy 		    FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE, vd->vdev_state));
1385eda14cbcSMatt Macy 		if (vd->vdev_path != NULL)
1386eda14cbcSMatt Macy 			VERIFY0(nvlist_add_string(resource,
1387eda14cbcSMatt Macy 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_PATH, vd->vdev_path));
1388eda14cbcSMatt Macy 		if (vd->vdev_devid != NULL)
1389eda14cbcSMatt Macy 			VERIFY0(nvlist_add_string(resource,
1390eda14cbcSMatt Macy 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_DEVID, vd->vdev_devid));
1391eda14cbcSMatt Macy 		if (vd->vdev_fru != NULL)
1392eda14cbcSMatt Macy 			VERIFY0(nvlist_add_string(resource,
1393eda14cbcSMatt Macy 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_FRU, vd->vdev_fru));
1394eda14cbcSMatt Macy 		if (vd->vdev_enc_sysfs_path != NULL)
1395eda14cbcSMatt Macy 			VERIFY0(nvlist_add_string(resource,
1396eda14cbcSMatt Macy 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_ENC_SYSFS_PATH,
1397eda14cbcSMatt Macy 			    vd->vdev_enc_sysfs_path));
1398eda14cbcSMatt Macy 	}
1399eda14cbcSMatt Macy 
1400eda14cbcSMatt Macy 	/* also copy any optional payload data */
1401eda14cbcSMatt Macy 	if (aux) {
1402eda14cbcSMatt Macy 		nvpair_t *elem = NULL;
1403eda14cbcSMatt Macy 
1404eda14cbcSMatt Macy 		while ((elem = nvlist_next_nvpair(aux, elem)) != NULL)
1405eda14cbcSMatt Macy 			(void) nvlist_add_nvpair(resource, elem);
1406eda14cbcSMatt Macy 	}
1407e92ffd9bSMartin Matuska #else
1408e92ffd9bSMartin Matuska 	(void) spa, (void) vd, (void) type, (void) name, (void) aux;
1409eda14cbcSMatt Macy #endif
1410eda14cbcSMatt Macy 	return (resource);
1411eda14cbcSMatt Macy }
1412eda14cbcSMatt Macy 
1413eda14cbcSMatt Macy static void
1414eda14cbcSMatt Macy zfs_post_common(spa_t *spa, vdev_t *vd, const char *type, const char *name,
1415eda14cbcSMatt Macy     nvlist_t *aux)
1416eda14cbcSMatt Macy {
1417eda14cbcSMatt Macy #ifdef _KERNEL
1418eda14cbcSMatt Macy 	nvlist_t *resource;
1419eda14cbcSMatt Macy 
1420eda14cbcSMatt Macy 	resource = zfs_event_create(spa, vd, type, name, aux);
1421eda14cbcSMatt Macy 	if (resource)
1422eda14cbcSMatt Macy 		zfs_zevent_post(resource, NULL, zfs_zevent_post_cb);
1423e92ffd9bSMartin Matuska #else
1424e92ffd9bSMartin Matuska 	(void) spa, (void) vd, (void) type, (void) name, (void) aux;
1425eda14cbcSMatt Macy #endif
1426eda14cbcSMatt Macy }
1427eda14cbcSMatt Macy 
1428eda14cbcSMatt Macy /*
1429eda14cbcSMatt Macy  * The 'resource.fs.zfs.removed' event is an internal signal that the given vdev
1430eda14cbcSMatt Macy  * has been removed from the system.  This will cause the DE to ignore any
1431eda14cbcSMatt Macy  * recent I/O errors, inferring that they are due to the asynchronous device
1432eda14cbcSMatt Macy  * removal.
1433eda14cbcSMatt Macy  */
1434eda14cbcSMatt Macy void
1435eda14cbcSMatt Macy zfs_post_remove(spa_t *spa, vdev_t *vd)
1436eda14cbcSMatt Macy {
1437eda14cbcSMatt Macy 	zfs_post_common(spa, vd, FM_RSRC_CLASS, FM_RESOURCE_REMOVED, NULL);
1438eda14cbcSMatt Macy }
1439eda14cbcSMatt Macy 
1440eda14cbcSMatt Macy /*
1441eda14cbcSMatt Macy  * The 'resource.fs.zfs.autoreplace' event is an internal signal that the pool
1442eda14cbcSMatt Macy  * has the 'autoreplace' property set, and therefore any broken vdevs will be
1443eda14cbcSMatt Macy  * handled by higher level logic, and no vdev fault should be generated.
1444eda14cbcSMatt Macy  */
1445eda14cbcSMatt Macy void
1446eda14cbcSMatt Macy zfs_post_autoreplace(spa_t *spa, vdev_t *vd)
1447eda14cbcSMatt Macy {
1448eda14cbcSMatt Macy 	zfs_post_common(spa, vd, FM_RSRC_CLASS, FM_RESOURCE_AUTOREPLACE, NULL);
1449eda14cbcSMatt Macy }
1450eda14cbcSMatt Macy 
1451eda14cbcSMatt Macy /*
1452eda14cbcSMatt Macy  * The 'resource.fs.zfs.statechange' event is an internal signal that the
1453eda14cbcSMatt Macy  * given vdev has transitioned its state to DEGRADED or HEALTHY.  This will
1454eda14cbcSMatt Macy  * cause the retire agent to repair any outstanding fault management cases
1455eda14cbcSMatt Macy  * open because the device was not found (fault.fs.zfs.device).
1456eda14cbcSMatt Macy  */
1457eda14cbcSMatt Macy void
1458eda14cbcSMatt Macy zfs_post_state_change(spa_t *spa, vdev_t *vd, uint64_t laststate)
1459eda14cbcSMatt Macy {
1460eda14cbcSMatt Macy #ifdef _KERNEL
1461eda14cbcSMatt Macy 	nvlist_t *aux;
1462eda14cbcSMatt Macy 
1463eda14cbcSMatt Macy 	/*
1464eda14cbcSMatt Macy 	 * Add optional supplemental keys to payload
1465eda14cbcSMatt Macy 	 */
1466eda14cbcSMatt Macy 	aux = fm_nvlist_create(NULL);
1467eda14cbcSMatt Macy 	if (vd && aux) {
1468eda14cbcSMatt Macy 		if (vd->vdev_physpath) {
1469be181ee2SMartin Matuska 			fnvlist_add_string(aux,
1470eda14cbcSMatt Macy 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_PHYSPATH,
1471eda14cbcSMatt Macy 			    vd->vdev_physpath);
1472eda14cbcSMatt Macy 		}
1473eda14cbcSMatt Macy 		if (vd->vdev_enc_sysfs_path) {
1474be181ee2SMartin Matuska 			fnvlist_add_string(aux,
1475eda14cbcSMatt Macy 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_ENC_SYSFS_PATH,
1476eda14cbcSMatt Macy 			    vd->vdev_enc_sysfs_path);
1477eda14cbcSMatt Macy 		}
1478eda14cbcSMatt Macy 
1479be181ee2SMartin Matuska 		fnvlist_add_uint64(aux,
1480eda14cbcSMatt Macy 		    FM_EREPORT_PAYLOAD_ZFS_VDEV_LASTSTATE, laststate);
1481eda14cbcSMatt Macy 	}
1482eda14cbcSMatt Macy 
1483eda14cbcSMatt Macy 	zfs_post_common(spa, vd, FM_RSRC_CLASS, FM_RESOURCE_STATECHANGE,
1484eda14cbcSMatt Macy 	    aux);
1485eda14cbcSMatt Macy 
1486eda14cbcSMatt Macy 	if (aux)
1487eda14cbcSMatt Macy 		fm_nvlist_destroy(aux, FM_NVA_FREE);
1488e92ffd9bSMartin Matuska #else
1489e92ffd9bSMartin Matuska 	(void) spa, (void) vd, (void) laststate;
1490eda14cbcSMatt Macy #endif
1491eda14cbcSMatt Macy }
1492eda14cbcSMatt Macy 
14932c48331dSMatt Macy #ifdef _KERNEL
14942c48331dSMatt Macy void
14952c48331dSMatt Macy zfs_ereport_init(void)
14962c48331dSMatt Macy {
14972c48331dSMatt Macy 	mutex_init(&recent_events_lock, NULL, MUTEX_DEFAULT, NULL);
14982c48331dSMatt Macy 	list_create(&recent_events_list, sizeof (recent_events_node_t),
14992c48331dSMatt Macy 	    offsetof(recent_events_node_t, re_list_link));
15002c48331dSMatt Macy 	avl_create(&recent_events_tree,  recent_events_compare,
15012c48331dSMatt Macy 	    sizeof (recent_events_node_t), offsetof(recent_events_node_t,
15022c48331dSMatt Macy 	    re_tree_link));
15032c48331dSMatt Macy }
15042c48331dSMatt Macy 
15052c48331dSMatt Macy /*
15062c48331dSMatt Macy  * This 'early' fini needs to run before zfs_fini() which on Linux waits
15072c48331dSMatt Macy  * for the system_delay_taskq to drain.
15082c48331dSMatt Macy  */
15092c48331dSMatt Macy void
15102c48331dSMatt Macy zfs_ereport_taskq_fini(void)
15112c48331dSMatt Macy {
15122c48331dSMatt Macy 	mutex_enter(&recent_events_lock);
15132c48331dSMatt Macy 	if (recent_events_cleaner_tqid != 0) {
15142c48331dSMatt Macy 		taskq_cancel_id(system_delay_taskq, recent_events_cleaner_tqid);
15152c48331dSMatt Macy 		recent_events_cleaner_tqid = 0;
15162c48331dSMatt Macy 	}
15172c48331dSMatt Macy 	mutex_exit(&recent_events_lock);
15182c48331dSMatt Macy }
15192c48331dSMatt Macy 
15202c48331dSMatt Macy void
15212c48331dSMatt Macy zfs_ereport_fini(void)
15222c48331dSMatt Macy {
15232c48331dSMatt Macy 	recent_events_node_t *entry;
15242c48331dSMatt Macy 
15252c48331dSMatt Macy 	while ((entry = list_head(&recent_events_list)) != NULL) {
15262c48331dSMatt Macy 		avl_remove(&recent_events_tree, entry);
15272c48331dSMatt Macy 		list_remove(&recent_events_list, entry);
15282c48331dSMatt Macy 		kmem_free(entry, sizeof (*entry));
15292c48331dSMatt Macy 	}
15302c48331dSMatt Macy 	avl_destroy(&recent_events_tree);
15312c48331dSMatt Macy 	list_destroy(&recent_events_list);
15322c48331dSMatt Macy 	mutex_destroy(&recent_events_lock);
15332c48331dSMatt Macy }
15342c48331dSMatt Macy 
153553b70c86SMartin Matuska void
153653b70c86SMartin Matuska zfs_ereport_snapshot_post(const char *subclass, spa_t *spa, const char *name)
153753b70c86SMartin Matuska {
153853b70c86SMartin Matuska 	nvlist_t *aux;
153953b70c86SMartin Matuska 
154053b70c86SMartin Matuska 	aux = fm_nvlist_create(NULL);
1541be181ee2SMartin Matuska 	fnvlist_add_string(aux, FM_EREPORT_PAYLOAD_ZFS_SNAPSHOT_NAME, name);
154253b70c86SMartin Matuska 
154353b70c86SMartin Matuska 	zfs_post_common(spa, NULL, FM_RSRC_CLASS, subclass, aux);
154453b70c86SMartin Matuska 	fm_nvlist_destroy(aux, FM_NVA_FREE);
154553b70c86SMartin Matuska }
154653b70c86SMartin Matuska 
154753b70c86SMartin Matuska /*
154853b70c86SMartin Matuska  * Post when a event when a zvol is created or removed
154953b70c86SMartin Matuska  *
155053b70c86SMartin Matuska  * This is currently only used by macOS, since it uses the event to create
155153b70c86SMartin Matuska  * symlinks between the volume name (mypool/myvol) and the actual /dev
155253b70c86SMartin Matuska  * device (/dev/disk3).  For example:
155353b70c86SMartin Matuska  *
155453b70c86SMartin Matuska  * /var/run/zfs/dsk/mypool/myvol -> /dev/disk3
155553b70c86SMartin Matuska  *
155653b70c86SMartin Matuska  * name: The full name of the zvol ("mypool/myvol")
155753b70c86SMartin Matuska  * dev_name: The full /dev name for the zvol ("/dev/disk3")
155853b70c86SMartin Matuska  * raw_name: The raw  /dev name for the zvol ("/dev/rdisk3")
155953b70c86SMartin Matuska  */
156053b70c86SMartin Matuska void
156153b70c86SMartin Matuska zfs_ereport_zvol_post(const char *subclass, const char *name,
156253b70c86SMartin Matuska     const char *dev_name, const char *raw_name)
156353b70c86SMartin Matuska {
156453b70c86SMartin Matuska 	nvlist_t *aux;
156553b70c86SMartin Matuska 	char *r;
156653b70c86SMartin Matuska 
156753b70c86SMartin Matuska 	boolean_t locked = mutex_owned(&spa_namespace_lock);
156853b70c86SMartin Matuska 	if (!locked) mutex_enter(&spa_namespace_lock);
156953b70c86SMartin Matuska 	spa_t *spa = spa_lookup(name);
157053b70c86SMartin Matuska 	if (!locked) mutex_exit(&spa_namespace_lock);
157153b70c86SMartin Matuska 
157253b70c86SMartin Matuska 	if (spa == NULL)
157353b70c86SMartin Matuska 		return;
157453b70c86SMartin Matuska 
157553b70c86SMartin Matuska 	aux = fm_nvlist_create(NULL);
1576be181ee2SMartin Matuska 	fnvlist_add_string(aux, FM_EREPORT_PAYLOAD_ZFS_DEVICE_NAME, dev_name);
1577be181ee2SMartin Matuska 	fnvlist_add_string(aux, FM_EREPORT_PAYLOAD_ZFS_RAW_DEVICE_NAME,
157853b70c86SMartin Matuska 	    raw_name);
157953b70c86SMartin Matuska 	r = strchr(name, '/');
158053b70c86SMartin Matuska 	if (r && r[1])
1581be181ee2SMartin Matuska 		fnvlist_add_string(aux, FM_EREPORT_PAYLOAD_ZFS_VOLUME, &r[1]);
158253b70c86SMartin Matuska 
158353b70c86SMartin Matuska 	zfs_post_common(spa, NULL, FM_RSRC_CLASS, subclass, aux);
158453b70c86SMartin Matuska 	fm_nvlist_destroy(aux, FM_NVA_FREE);
158553b70c86SMartin Matuska }
158653b70c86SMartin Matuska 
1587eda14cbcSMatt Macy EXPORT_SYMBOL(zfs_ereport_post);
1588eda14cbcSMatt Macy EXPORT_SYMBOL(zfs_ereport_is_valid);
1589eda14cbcSMatt Macy EXPORT_SYMBOL(zfs_ereport_post_checksum);
1590eda14cbcSMatt Macy EXPORT_SYMBOL(zfs_post_remove);
1591eda14cbcSMatt Macy EXPORT_SYMBOL(zfs_post_autoreplace);
1592eda14cbcSMatt Macy EXPORT_SYMBOL(zfs_post_state_change);
15932c48331dSMatt Macy 
15942c48331dSMatt Macy ZFS_MODULE_PARAM(zfs_zevent, zfs_zevent_, retain_max, UINT, ZMOD_RW,
15952c48331dSMatt Macy 	"Maximum recent zevents records to retain for duplicate checking");
15962c48331dSMatt Macy ZFS_MODULE_PARAM(zfs_zevent, zfs_zevent_, retain_expire_secs, UINT, ZMOD_RW,
15972c48331dSMatt Macy 	"Expiration time for recent zevents records");
1598eda14cbcSMatt Macy #endif /* _KERNEL */
1599