xref: /freebsd/sys/contrib/openzfs/module/zfs/zfs_fm.c (revision da5137abdf463bb5fee85061958a14dd12bc043e)
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
9eda14cbcSMatt Macy  * or http://www.opensolaris.org/os/licensing.
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 
2032c48331dSMatt Macy static void zfs_ereport_schedule_cleaner(void);
2042c48331dSMatt Macy 
2052c48331dSMatt Macy /*
2062c48331dSMatt Macy  * background task to clean stale recent event nodes.
2072c48331dSMatt Macy  */
2082c48331dSMatt Macy static void
2092c48331dSMatt Macy zfs_ereport_cleaner(void *arg)
2102c48331dSMatt Macy {
2112c48331dSMatt Macy 	recent_events_node_t *entry;
2122c48331dSMatt Macy 	uint64_t now = gethrtime();
2132c48331dSMatt Macy 
2142c48331dSMatt Macy 	/*
2152c48331dSMatt Macy 	 * purge expired entries
2162c48331dSMatt Macy 	 */
2172c48331dSMatt Macy 	mutex_enter(&recent_events_lock);
2182c48331dSMatt Macy 	while ((entry = list_tail(&recent_events_list)) != NULL) {
2192c48331dSMatt Macy 		uint64_t age = NSEC2SEC(now - entry->re_timestamp);
2202c48331dSMatt Macy 		if (age <= zfs_zevent_retain_expire_secs)
2212c48331dSMatt Macy 			break;
2222c48331dSMatt Macy 
2232c48331dSMatt Macy 		/* remove expired node */
2242c48331dSMatt Macy 		avl_remove(&recent_events_tree, entry);
2252c48331dSMatt Macy 		list_remove(&recent_events_list, entry);
2262c48331dSMatt Macy 		kmem_free(entry, sizeof (*entry));
2272c48331dSMatt Macy 	}
2282c48331dSMatt Macy 
2292c48331dSMatt Macy 	/* Restart the cleaner if more entries remain */
2302c48331dSMatt Macy 	recent_events_cleaner_tqid = 0;
2312c48331dSMatt Macy 	if (!list_is_empty(&recent_events_list))
2322c48331dSMatt Macy 		zfs_ereport_schedule_cleaner();
2332c48331dSMatt Macy 
2342c48331dSMatt Macy 	mutex_exit(&recent_events_lock);
2352c48331dSMatt Macy }
2362c48331dSMatt Macy 
2372c48331dSMatt Macy static void
2382c48331dSMatt Macy zfs_ereport_schedule_cleaner(void)
2392c48331dSMatt Macy {
2402c48331dSMatt Macy 	ASSERT(MUTEX_HELD(&recent_events_lock));
2412c48331dSMatt Macy 
2422c48331dSMatt Macy 	uint64_t timeout = SEC2NSEC(zfs_zevent_retain_expire_secs + 1);
2432c48331dSMatt Macy 
2442c48331dSMatt Macy 	recent_events_cleaner_tqid = taskq_dispatch_delay(
2452c48331dSMatt Macy 	    system_delay_taskq, zfs_ereport_cleaner, NULL, TQ_SLEEP,
2462c48331dSMatt Macy 	    ddi_get_lbolt() + NSEC_TO_TICK(timeout));
2472c48331dSMatt Macy }
2482c48331dSMatt Macy 
2492c48331dSMatt Macy /*
250ba27dd8bSMartin Matuska  * Clear entries for a given vdev or all vdevs in a pool when vdev == NULL
251ba27dd8bSMartin Matuska  */
252ba27dd8bSMartin Matuska void
253ba27dd8bSMartin Matuska zfs_ereport_clear(spa_t *spa, vdev_t *vd)
254ba27dd8bSMartin Matuska {
255ba27dd8bSMartin Matuska 	uint64_t vdev_guid, pool_guid;
256ba27dd8bSMartin Matuska 	int cnt = 0;
257ba27dd8bSMartin Matuska 
258ba27dd8bSMartin Matuska 	ASSERT(vd != NULL || spa != NULL);
259ba27dd8bSMartin Matuska 	if (vd == NULL) {
260ba27dd8bSMartin Matuska 		vdev_guid = 0;
261ba27dd8bSMartin Matuska 		pool_guid = spa_guid(spa);
262ba27dd8bSMartin Matuska 	} else {
263ba27dd8bSMartin Matuska 		vdev_guid = vd->vdev_guid;
264ba27dd8bSMartin Matuska 		pool_guid = 0;
265ba27dd8bSMartin Matuska 	}
266ba27dd8bSMartin Matuska 
267ba27dd8bSMartin Matuska 	mutex_enter(&recent_events_lock);
268ba27dd8bSMartin Matuska 
269ba27dd8bSMartin Matuska 	recent_events_node_t *next = list_head(&recent_events_list);
270ba27dd8bSMartin Matuska 	while (next != NULL) {
271ba27dd8bSMartin Matuska 		recent_events_node_t *entry = next;
272ba27dd8bSMartin Matuska 
273ba27dd8bSMartin Matuska 		next = list_next(&recent_events_list, next);
274ba27dd8bSMartin Matuska 
275ba27dd8bSMartin Matuska 		if (entry->re_vdev_guid == vdev_guid ||
276ba27dd8bSMartin Matuska 		    entry->re_pool_guid == pool_guid) {
277ba27dd8bSMartin Matuska 			avl_remove(&recent_events_tree, entry);
278ba27dd8bSMartin Matuska 			list_remove(&recent_events_list, entry);
279ba27dd8bSMartin Matuska 			kmem_free(entry, sizeof (*entry));
280ba27dd8bSMartin Matuska 			cnt++;
281ba27dd8bSMartin Matuska 		}
282ba27dd8bSMartin Matuska 	}
283ba27dd8bSMartin Matuska 
284ba27dd8bSMartin Matuska 	mutex_exit(&recent_events_lock);
285ba27dd8bSMartin Matuska }
286ba27dd8bSMartin Matuska 
287ba27dd8bSMartin Matuska /*
2882c48331dSMatt Macy  * Check if an ereport would be a duplicate of one recently posted.
2892c48331dSMatt Macy  *
2902c48331dSMatt Macy  * An ereport is considered a duplicate if the set of criteria in
2912c48331dSMatt Macy  * recent_events_node_t all match.
2922c48331dSMatt Macy  *
2932c48331dSMatt Macy  * Only FM_EREPORT_ZFS_IO, FM_EREPORT_ZFS_DATA, and FM_EREPORT_ZFS_CHECKSUM
2942c48331dSMatt Macy  * are candidates for duplicate checking.
2952c48331dSMatt Macy  */
2962c48331dSMatt Macy static boolean_t
2972c48331dSMatt Macy zfs_ereport_is_duplicate(const char *subclass, spa_t *spa, vdev_t *vd,
2982c48331dSMatt Macy     const zbookmark_phys_t *zb, zio_t *zio, uint64_t offset, uint64_t size)
2992c48331dSMatt Macy {
3002c48331dSMatt Macy 	recent_events_node_t search = {0}, *entry;
3012c48331dSMatt Macy 
3022c48331dSMatt Macy 	if (vd == NULL || zio == NULL)
3032c48331dSMatt Macy 		return (B_FALSE);
3042c48331dSMatt Macy 
3052c48331dSMatt Macy 	if (zfs_zevent_retain_max == 0)
3062c48331dSMatt Macy 		return (B_FALSE);
3072c48331dSMatt Macy 
3082c48331dSMatt Macy 	if (strcmp(subclass, FM_EREPORT_ZFS_IO) == 0)
3092c48331dSMatt Macy 		search.re_subclass = ZSC_IO;
3102c48331dSMatt Macy 	else if (strcmp(subclass, FM_EREPORT_ZFS_DATA) == 0)
3112c48331dSMatt Macy 		search.re_subclass = ZSC_DATA;
3122c48331dSMatt Macy 	else if (strcmp(subclass, FM_EREPORT_ZFS_CHECKSUM) == 0)
3132c48331dSMatt Macy 		search.re_subclass = ZSC_CHECKSUM;
3142c48331dSMatt Macy 	else
3152c48331dSMatt Macy 		return (B_FALSE);
3162c48331dSMatt Macy 
3172c48331dSMatt Macy 	search.re_pool_guid = spa_guid(spa);
3182c48331dSMatt Macy 	search.re_vdev_guid = vd->vdev_guid;
3192c48331dSMatt Macy 	search.re_io_error = zio->io_error;
3202c48331dSMatt Macy 	search.re_io_priority = zio->io_priority;
3212c48331dSMatt Macy 	/* if size is supplied use it over what's in zio */
3222c48331dSMatt Macy 	if (size) {
3232c48331dSMatt Macy 		search.re_io_size = size;
3242c48331dSMatt Macy 		search.re_io_offset = offset;
3252c48331dSMatt Macy 	} else {
3262c48331dSMatt Macy 		search.re_io_size = zio->io_size;
3272c48331dSMatt Macy 		search.re_io_offset = zio->io_offset;
3282c48331dSMatt Macy 	}
3292c48331dSMatt Macy 
3302c48331dSMatt Macy 	/* grab optional logical zio criteria */
3312c48331dSMatt Macy 	if (zb != NULL) {
3322c48331dSMatt Macy 		search.re_io_bookmark.zb_objset = zb->zb_objset;
3332c48331dSMatt Macy 		search.re_io_bookmark.zb_object = zb->zb_object;
3342c48331dSMatt Macy 		search.re_io_bookmark.zb_level = zb->zb_level;
3352c48331dSMatt Macy 		search.re_io_bookmark.zb_blkid = zb->zb_blkid;
3362c48331dSMatt Macy 	}
3372c48331dSMatt Macy 
3382c48331dSMatt Macy 	uint64_t now = gethrtime();
3392c48331dSMatt Macy 
3402c48331dSMatt Macy 	mutex_enter(&recent_events_lock);
3412c48331dSMatt Macy 
3422c48331dSMatt Macy 	/* check if we have seen this one recently */
3432c48331dSMatt Macy 	entry = avl_find(&recent_events_tree, &search, NULL);
3442c48331dSMatt Macy 	if (entry != NULL) {
3452c48331dSMatt Macy 		uint64_t age = NSEC2SEC(now - entry->re_timestamp);
3462c48331dSMatt Macy 
3472c48331dSMatt Macy 		/*
3482c48331dSMatt Macy 		 * There is still an active cleaner (since we're here).
3492c48331dSMatt Macy 		 * Reset the last seen time for this duplicate entry
3502c48331dSMatt Macy 		 * so that its lifespand gets extended.
3512c48331dSMatt Macy 		 */
3522c48331dSMatt Macy 		list_remove(&recent_events_list, entry);
3532c48331dSMatt Macy 		list_insert_head(&recent_events_list, entry);
3542c48331dSMatt Macy 		entry->re_timestamp = now;
3552c48331dSMatt Macy 
3562c48331dSMatt Macy 		zfs_zevent_track_duplicate();
3572c48331dSMatt Macy 		mutex_exit(&recent_events_lock);
3582c48331dSMatt Macy 
3592c48331dSMatt Macy 		return (age <= zfs_zevent_retain_expire_secs);
3602c48331dSMatt Macy 	}
3612c48331dSMatt Macy 
3622c48331dSMatt Macy 	if (avl_numnodes(&recent_events_tree) >= zfs_zevent_retain_max) {
3632c48331dSMatt Macy 		/* recycle oldest node */
3642c48331dSMatt Macy 		entry = list_tail(&recent_events_list);
3652c48331dSMatt Macy 		ASSERT(entry != NULL);
3662c48331dSMatt Macy 		list_remove(&recent_events_list, entry);
3672c48331dSMatt Macy 		avl_remove(&recent_events_tree, entry);
3682c48331dSMatt Macy 	} else {
3692c48331dSMatt Macy 		entry = kmem_alloc(sizeof (recent_events_node_t), KM_SLEEP);
3702c48331dSMatt Macy 	}
3712c48331dSMatt Macy 
3722c48331dSMatt Macy 	/* record this as a recent ereport */
3732c48331dSMatt Macy 	*entry = search;
3742c48331dSMatt Macy 	avl_add(&recent_events_tree, entry);
3752c48331dSMatt Macy 	list_insert_head(&recent_events_list, entry);
3762c48331dSMatt Macy 	entry->re_timestamp = now;
3772c48331dSMatt Macy 
3782c48331dSMatt Macy 	/* Start a cleaner if not already scheduled */
3792c48331dSMatt Macy 	if (recent_events_cleaner_tqid == 0)
3802c48331dSMatt Macy 		zfs_ereport_schedule_cleaner();
3812c48331dSMatt Macy 
3822c48331dSMatt Macy 	mutex_exit(&recent_events_lock);
3832c48331dSMatt Macy 	return (B_FALSE);
3842c48331dSMatt Macy }
3852c48331dSMatt Macy 
386eda14cbcSMatt Macy void
387eda14cbcSMatt Macy zfs_zevent_post_cb(nvlist_t *nvl, nvlist_t *detector)
388eda14cbcSMatt Macy {
389eda14cbcSMatt Macy 	if (nvl)
390eda14cbcSMatt Macy 		fm_nvlist_destroy(nvl, FM_NVA_FREE);
391eda14cbcSMatt Macy 
392eda14cbcSMatt Macy 	if (detector)
393eda14cbcSMatt Macy 		fm_nvlist_destroy(detector, FM_NVA_FREE);
394eda14cbcSMatt Macy }
395eda14cbcSMatt Macy 
396eda14cbcSMatt Macy /*
39716038816SMartin Matuska  * We want to rate limit ZIO delay, deadman, and checksum events so as to not
39816038816SMartin Matuska  * flood zevent consumers when a disk is acting up.
399eda14cbcSMatt Macy  *
400eda14cbcSMatt Macy  * Returns 1 if we're ratelimiting, 0 if not.
401eda14cbcSMatt Macy  */
402eda14cbcSMatt Macy static int
403eda14cbcSMatt Macy zfs_is_ratelimiting_event(const char *subclass, vdev_t *vd)
404eda14cbcSMatt Macy {
405eda14cbcSMatt Macy 	int rc = 0;
406eda14cbcSMatt Macy 	/*
40716038816SMartin Matuska 	 * zfs_ratelimit() returns 1 if we're *not* ratelimiting and 0 if we
408eda14cbcSMatt Macy 	 * are.  Invert it to get our return value.
409eda14cbcSMatt Macy 	 */
410eda14cbcSMatt Macy 	if (strcmp(subclass, FM_EREPORT_ZFS_DELAY) == 0) {
411eda14cbcSMatt Macy 		rc = !zfs_ratelimit(&vd->vdev_delay_rl);
41216038816SMartin Matuska 	} else if (strcmp(subclass, FM_EREPORT_ZFS_DEADMAN) == 0) {
41316038816SMartin Matuska 		rc = !zfs_ratelimit(&vd->vdev_deadman_rl);
414eda14cbcSMatt Macy 	} else if (strcmp(subclass, FM_EREPORT_ZFS_CHECKSUM) == 0) {
415eda14cbcSMatt Macy 		rc = !zfs_ratelimit(&vd->vdev_checksum_rl);
416eda14cbcSMatt Macy 	}
417eda14cbcSMatt Macy 
418eda14cbcSMatt Macy 	if (rc)	{
419eda14cbcSMatt Macy 		/* We're rate limiting */
420eda14cbcSMatt Macy 		fm_erpt_dropped_increment();
421eda14cbcSMatt Macy 	}
422eda14cbcSMatt Macy 
423eda14cbcSMatt Macy 	return (rc);
424eda14cbcSMatt Macy }
425eda14cbcSMatt Macy 
426eda14cbcSMatt Macy /*
427eda14cbcSMatt Macy  * Return B_TRUE if the event actually posted, B_FALSE if not.
428eda14cbcSMatt Macy  */
429eda14cbcSMatt Macy static boolean_t
430eda14cbcSMatt Macy zfs_ereport_start(nvlist_t **ereport_out, nvlist_t **detector_out,
431eda14cbcSMatt Macy     const char *subclass, spa_t *spa, vdev_t *vd, const zbookmark_phys_t *zb,
432eda14cbcSMatt Macy     zio_t *zio, uint64_t stateoroffset, uint64_t size)
433eda14cbcSMatt Macy {
434eda14cbcSMatt Macy 	nvlist_t *ereport, *detector;
435eda14cbcSMatt Macy 
436eda14cbcSMatt Macy 	uint64_t ena;
437eda14cbcSMatt Macy 	char class[64];
438eda14cbcSMatt Macy 
439eda14cbcSMatt Macy 	if ((ereport = fm_nvlist_create(NULL)) == NULL)
440eda14cbcSMatt Macy 		return (B_FALSE);
441eda14cbcSMatt Macy 
442eda14cbcSMatt Macy 	if ((detector = fm_nvlist_create(NULL)) == NULL) {
443eda14cbcSMatt Macy 		fm_nvlist_destroy(ereport, FM_NVA_FREE);
444eda14cbcSMatt Macy 		return (B_FALSE);
445eda14cbcSMatt Macy 	}
446eda14cbcSMatt Macy 
447eda14cbcSMatt Macy 	/*
448eda14cbcSMatt Macy 	 * Serialize ereport generation
449eda14cbcSMatt Macy 	 */
450eda14cbcSMatt Macy 	mutex_enter(&spa->spa_errlist_lock);
451eda14cbcSMatt Macy 
452eda14cbcSMatt Macy 	/*
453eda14cbcSMatt Macy 	 * Determine the ENA to use for this event.  If we are in a loading
454eda14cbcSMatt Macy 	 * state, use a SPA-wide ENA.  Otherwise, if we are in an I/O state, use
455eda14cbcSMatt Macy 	 * a root zio-wide ENA.  Otherwise, simply use a unique ENA.
456eda14cbcSMatt Macy 	 */
457eda14cbcSMatt Macy 	if (spa_load_state(spa) != SPA_LOAD_NONE) {
458eda14cbcSMatt Macy 		if (spa->spa_ena == 0)
459eda14cbcSMatt Macy 			spa->spa_ena = fm_ena_generate(0, FM_ENA_FMT1);
460eda14cbcSMatt Macy 		ena = spa->spa_ena;
461eda14cbcSMatt Macy 	} else if (zio != NULL && zio->io_logical != NULL) {
462eda14cbcSMatt Macy 		if (zio->io_logical->io_ena == 0)
463eda14cbcSMatt Macy 			zio->io_logical->io_ena =
464eda14cbcSMatt Macy 			    fm_ena_generate(0, FM_ENA_FMT1);
465eda14cbcSMatt Macy 		ena = zio->io_logical->io_ena;
466eda14cbcSMatt Macy 	} else {
467eda14cbcSMatt Macy 		ena = fm_ena_generate(0, FM_ENA_FMT1);
468eda14cbcSMatt Macy 	}
469eda14cbcSMatt Macy 
470eda14cbcSMatt Macy 	/*
471eda14cbcSMatt Macy 	 * Construct the full class, detector, and other standard FMA fields.
472eda14cbcSMatt Macy 	 */
473eda14cbcSMatt Macy 	(void) snprintf(class, sizeof (class), "%s.%s",
474eda14cbcSMatt Macy 	    ZFS_ERROR_CLASS, subclass);
475eda14cbcSMatt Macy 
476eda14cbcSMatt Macy 	fm_fmri_zfs_set(detector, FM_ZFS_SCHEME_VERSION, spa_guid(spa),
477eda14cbcSMatt Macy 	    vd != NULL ? vd->vdev_guid : 0);
478eda14cbcSMatt Macy 
479eda14cbcSMatt Macy 	fm_ereport_set(ereport, FM_EREPORT_VERSION, class, ena, detector, NULL);
480eda14cbcSMatt Macy 
481eda14cbcSMatt Macy 	/*
482eda14cbcSMatt Macy 	 * Construct the per-ereport payload, depending on which parameters are
483eda14cbcSMatt Macy 	 * passed in.
484eda14cbcSMatt Macy 	 */
485eda14cbcSMatt Macy 
486eda14cbcSMatt Macy 	/*
487eda14cbcSMatt Macy 	 * Generic payload members common to all ereports.
488eda14cbcSMatt Macy 	 */
489eda14cbcSMatt Macy 	fm_payload_set(ereport,
490eda14cbcSMatt Macy 	    FM_EREPORT_PAYLOAD_ZFS_POOL, DATA_TYPE_STRING, spa_name(spa),
491eda14cbcSMatt Macy 	    FM_EREPORT_PAYLOAD_ZFS_POOL_GUID, DATA_TYPE_UINT64, spa_guid(spa),
492eda14cbcSMatt Macy 	    FM_EREPORT_PAYLOAD_ZFS_POOL_STATE, DATA_TYPE_UINT64,
493eda14cbcSMatt Macy 	    (uint64_t)spa_state(spa),
494eda14cbcSMatt Macy 	    FM_EREPORT_PAYLOAD_ZFS_POOL_CONTEXT, DATA_TYPE_INT32,
495eda14cbcSMatt Macy 	    (int32_t)spa_load_state(spa), NULL);
496eda14cbcSMatt Macy 
497eda14cbcSMatt Macy 	fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_POOL_FAILMODE,
498eda14cbcSMatt Macy 	    DATA_TYPE_STRING,
499eda14cbcSMatt Macy 	    spa_get_failmode(spa) == ZIO_FAILURE_MODE_WAIT ?
500eda14cbcSMatt Macy 	    FM_EREPORT_FAILMODE_WAIT :
501eda14cbcSMatt Macy 	    spa_get_failmode(spa) == ZIO_FAILURE_MODE_CONTINUE ?
502eda14cbcSMatt Macy 	    FM_EREPORT_FAILMODE_CONTINUE : FM_EREPORT_FAILMODE_PANIC,
503eda14cbcSMatt Macy 	    NULL);
504eda14cbcSMatt Macy 
505eda14cbcSMatt Macy 	if (vd != NULL) {
506eda14cbcSMatt Macy 		vdev_t *pvd = vd->vdev_parent;
507eda14cbcSMatt Macy 		vdev_queue_t *vq = &vd->vdev_queue;
508eda14cbcSMatt Macy 		vdev_stat_t *vs = &vd->vdev_stat;
509eda14cbcSMatt Macy 		vdev_t *spare_vd;
510eda14cbcSMatt Macy 		uint64_t *spare_guids;
511eda14cbcSMatt Macy 		char **spare_paths;
512eda14cbcSMatt Macy 		int i, spare_count;
513eda14cbcSMatt Macy 
514eda14cbcSMatt Macy 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_VDEV_GUID,
515eda14cbcSMatt Macy 		    DATA_TYPE_UINT64, vd->vdev_guid,
516eda14cbcSMatt Macy 		    FM_EREPORT_PAYLOAD_ZFS_VDEV_TYPE,
517eda14cbcSMatt Macy 		    DATA_TYPE_STRING, vd->vdev_ops->vdev_op_type, NULL);
518eda14cbcSMatt Macy 		if (vd->vdev_path != NULL)
519eda14cbcSMatt Macy 			fm_payload_set(ereport,
520eda14cbcSMatt Macy 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_PATH,
521eda14cbcSMatt Macy 			    DATA_TYPE_STRING, vd->vdev_path, NULL);
522eda14cbcSMatt Macy 		if (vd->vdev_devid != NULL)
523eda14cbcSMatt Macy 			fm_payload_set(ereport,
524eda14cbcSMatt Macy 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_DEVID,
525eda14cbcSMatt Macy 			    DATA_TYPE_STRING, vd->vdev_devid, NULL);
526eda14cbcSMatt Macy 		if (vd->vdev_fru != NULL)
527eda14cbcSMatt Macy 			fm_payload_set(ereport,
528eda14cbcSMatt Macy 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_FRU,
529eda14cbcSMatt Macy 			    DATA_TYPE_STRING, vd->vdev_fru, NULL);
530eda14cbcSMatt Macy 		if (vd->vdev_enc_sysfs_path != NULL)
531eda14cbcSMatt Macy 			fm_payload_set(ereport,
532eda14cbcSMatt Macy 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_ENC_SYSFS_PATH,
533eda14cbcSMatt Macy 			    DATA_TYPE_STRING, vd->vdev_enc_sysfs_path, NULL);
534eda14cbcSMatt Macy 		if (vd->vdev_ashift)
535eda14cbcSMatt Macy 			fm_payload_set(ereport,
536eda14cbcSMatt Macy 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_ASHIFT,
537eda14cbcSMatt Macy 			    DATA_TYPE_UINT64, vd->vdev_ashift, NULL);
538eda14cbcSMatt Macy 
539eda14cbcSMatt Macy 		if (vq != NULL) {
540eda14cbcSMatt Macy 			fm_payload_set(ereport,
541eda14cbcSMatt Macy 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_COMP_TS,
542eda14cbcSMatt Macy 			    DATA_TYPE_UINT64, vq->vq_io_complete_ts, NULL);
543eda14cbcSMatt Macy 			fm_payload_set(ereport,
544eda14cbcSMatt Macy 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_DELTA_TS,
545eda14cbcSMatt Macy 			    DATA_TYPE_UINT64, vq->vq_io_delta_ts, NULL);
546eda14cbcSMatt Macy 		}
547eda14cbcSMatt Macy 
548eda14cbcSMatt Macy 		if (vs != NULL) {
549eda14cbcSMatt Macy 			fm_payload_set(ereport,
550eda14cbcSMatt Macy 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_READ_ERRORS,
551eda14cbcSMatt Macy 			    DATA_TYPE_UINT64, vs->vs_read_errors,
552eda14cbcSMatt Macy 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_WRITE_ERRORS,
553eda14cbcSMatt Macy 			    DATA_TYPE_UINT64, vs->vs_write_errors,
554eda14cbcSMatt Macy 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_CKSUM_ERRORS,
555eda14cbcSMatt Macy 			    DATA_TYPE_UINT64, vs->vs_checksum_errors,
556eda14cbcSMatt Macy 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_DELAYS,
557eda14cbcSMatt Macy 			    DATA_TYPE_UINT64, vs->vs_slow_ios,
558eda14cbcSMatt Macy 			    NULL);
559eda14cbcSMatt Macy 		}
560eda14cbcSMatt Macy 
561eda14cbcSMatt Macy 		if (pvd != NULL) {
562eda14cbcSMatt Macy 			fm_payload_set(ereport,
563eda14cbcSMatt Macy 			    FM_EREPORT_PAYLOAD_ZFS_PARENT_GUID,
564eda14cbcSMatt Macy 			    DATA_TYPE_UINT64, pvd->vdev_guid,
565eda14cbcSMatt Macy 			    FM_EREPORT_PAYLOAD_ZFS_PARENT_TYPE,
566eda14cbcSMatt Macy 			    DATA_TYPE_STRING, pvd->vdev_ops->vdev_op_type,
567eda14cbcSMatt Macy 			    NULL);
568eda14cbcSMatt Macy 			if (pvd->vdev_path)
569eda14cbcSMatt Macy 				fm_payload_set(ereport,
570eda14cbcSMatt Macy 				    FM_EREPORT_PAYLOAD_ZFS_PARENT_PATH,
571eda14cbcSMatt Macy 				    DATA_TYPE_STRING, pvd->vdev_path, NULL);
572eda14cbcSMatt Macy 			if (pvd->vdev_devid)
573eda14cbcSMatt Macy 				fm_payload_set(ereport,
574eda14cbcSMatt Macy 				    FM_EREPORT_PAYLOAD_ZFS_PARENT_DEVID,
575eda14cbcSMatt Macy 				    DATA_TYPE_STRING, pvd->vdev_devid, NULL);
576eda14cbcSMatt Macy 		}
577eda14cbcSMatt Macy 
578eda14cbcSMatt Macy 		spare_count = spa->spa_spares.sav_count;
579eda14cbcSMatt Macy 		spare_paths = kmem_zalloc(sizeof (char *) * spare_count,
580eda14cbcSMatt Macy 		    KM_SLEEP);
581eda14cbcSMatt Macy 		spare_guids = kmem_zalloc(sizeof (uint64_t) * spare_count,
582eda14cbcSMatt Macy 		    KM_SLEEP);
583eda14cbcSMatt Macy 
584eda14cbcSMatt Macy 		for (i = 0; i < spare_count; i++) {
585eda14cbcSMatt Macy 			spare_vd = spa->spa_spares.sav_vdevs[i];
586eda14cbcSMatt Macy 			if (spare_vd) {
587eda14cbcSMatt Macy 				spare_paths[i] = spare_vd->vdev_path;
588eda14cbcSMatt Macy 				spare_guids[i] = spare_vd->vdev_guid;
589eda14cbcSMatt Macy 			}
590eda14cbcSMatt Macy 		}
591eda14cbcSMatt Macy 
592eda14cbcSMatt Macy 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_VDEV_SPARE_PATHS,
593eda14cbcSMatt Macy 		    DATA_TYPE_STRING_ARRAY, spare_count, spare_paths,
594eda14cbcSMatt Macy 		    FM_EREPORT_PAYLOAD_ZFS_VDEV_SPARE_GUIDS,
595eda14cbcSMatt Macy 		    DATA_TYPE_UINT64_ARRAY, spare_count, spare_guids, NULL);
596eda14cbcSMatt Macy 
597eda14cbcSMatt Macy 		kmem_free(spare_guids, sizeof (uint64_t) * spare_count);
598eda14cbcSMatt Macy 		kmem_free(spare_paths, sizeof (char *) * spare_count);
599eda14cbcSMatt Macy 	}
600eda14cbcSMatt Macy 
601eda14cbcSMatt Macy 	if (zio != NULL) {
602eda14cbcSMatt Macy 		/*
603eda14cbcSMatt Macy 		 * Payload common to all I/Os.
604eda14cbcSMatt Macy 		 */
605eda14cbcSMatt Macy 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_ERR,
606eda14cbcSMatt Macy 		    DATA_TYPE_INT32, zio->io_error, NULL);
607eda14cbcSMatt Macy 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_FLAGS,
608eda14cbcSMatt Macy 		    DATA_TYPE_INT32, zio->io_flags, NULL);
609eda14cbcSMatt Macy 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_STAGE,
610eda14cbcSMatt Macy 		    DATA_TYPE_UINT32, zio->io_stage, NULL);
611eda14cbcSMatt Macy 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_PIPELINE,
612eda14cbcSMatt Macy 		    DATA_TYPE_UINT32, zio->io_pipeline, NULL);
613eda14cbcSMatt Macy 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_DELAY,
614eda14cbcSMatt Macy 		    DATA_TYPE_UINT64, zio->io_delay, NULL);
615eda14cbcSMatt Macy 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_TIMESTAMP,
616eda14cbcSMatt Macy 		    DATA_TYPE_UINT64, zio->io_timestamp, NULL);
617eda14cbcSMatt Macy 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_DELTA,
618eda14cbcSMatt Macy 		    DATA_TYPE_UINT64, zio->io_delta, NULL);
6192c48331dSMatt Macy 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_PRIORITY,
6202c48331dSMatt Macy 		    DATA_TYPE_UINT32, zio->io_priority, NULL);
621eda14cbcSMatt Macy 
622eda14cbcSMatt Macy 		/*
623eda14cbcSMatt Macy 		 * If the 'size' parameter is non-zero, it indicates this is a
624eda14cbcSMatt Macy 		 * RAID-Z or other I/O where the physical offset and length are
625eda14cbcSMatt Macy 		 * provided for us, instead of within the zio_t.
626eda14cbcSMatt Macy 		 */
627eda14cbcSMatt Macy 		if (vd != NULL) {
628eda14cbcSMatt Macy 			if (size)
629eda14cbcSMatt Macy 				fm_payload_set(ereport,
630eda14cbcSMatt Macy 				    FM_EREPORT_PAYLOAD_ZFS_ZIO_OFFSET,
631eda14cbcSMatt Macy 				    DATA_TYPE_UINT64, stateoroffset,
632eda14cbcSMatt Macy 				    FM_EREPORT_PAYLOAD_ZFS_ZIO_SIZE,
633eda14cbcSMatt Macy 				    DATA_TYPE_UINT64, size, NULL);
634eda14cbcSMatt Macy 			else
635eda14cbcSMatt Macy 				fm_payload_set(ereport,
636eda14cbcSMatt Macy 				    FM_EREPORT_PAYLOAD_ZFS_ZIO_OFFSET,
637eda14cbcSMatt Macy 				    DATA_TYPE_UINT64, zio->io_offset,
638eda14cbcSMatt Macy 				    FM_EREPORT_PAYLOAD_ZFS_ZIO_SIZE,
639eda14cbcSMatt Macy 				    DATA_TYPE_UINT64, zio->io_size, NULL);
640eda14cbcSMatt Macy 		}
641eda14cbcSMatt Macy 	} else if (vd != NULL) {
642eda14cbcSMatt Macy 		/*
643eda14cbcSMatt Macy 		 * If we have a vdev but no zio, this is a device fault, and the
644eda14cbcSMatt Macy 		 * 'stateoroffset' parameter indicates the previous state of the
645eda14cbcSMatt Macy 		 * vdev.
646eda14cbcSMatt Macy 		 */
647eda14cbcSMatt Macy 		fm_payload_set(ereport,
648eda14cbcSMatt Macy 		    FM_EREPORT_PAYLOAD_ZFS_PREV_STATE,
649eda14cbcSMatt Macy 		    DATA_TYPE_UINT64, stateoroffset, NULL);
650eda14cbcSMatt Macy 	}
651eda14cbcSMatt Macy 
652eda14cbcSMatt Macy 	/*
653eda14cbcSMatt Macy 	 * Payload for I/Os with corresponding logical information.
654eda14cbcSMatt Macy 	 */
655eda14cbcSMatt Macy 	if (zb != NULL && (zio == NULL || zio->io_logical != NULL)) {
656eda14cbcSMatt Macy 		fm_payload_set(ereport,
657eda14cbcSMatt Macy 		    FM_EREPORT_PAYLOAD_ZFS_ZIO_OBJSET,
658eda14cbcSMatt Macy 		    DATA_TYPE_UINT64, zb->zb_objset,
659eda14cbcSMatt Macy 		    FM_EREPORT_PAYLOAD_ZFS_ZIO_OBJECT,
660eda14cbcSMatt Macy 		    DATA_TYPE_UINT64, zb->zb_object,
661eda14cbcSMatt Macy 		    FM_EREPORT_PAYLOAD_ZFS_ZIO_LEVEL,
662eda14cbcSMatt Macy 		    DATA_TYPE_INT64, zb->zb_level,
663eda14cbcSMatt Macy 		    FM_EREPORT_PAYLOAD_ZFS_ZIO_BLKID,
664eda14cbcSMatt Macy 		    DATA_TYPE_UINT64, zb->zb_blkid, NULL);
665eda14cbcSMatt Macy 	}
666eda14cbcSMatt Macy 
667eda14cbcSMatt Macy 	mutex_exit(&spa->spa_errlist_lock);
668eda14cbcSMatt Macy 
669eda14cbcSMatt Macy 	*ereport_out = ereport;
670eda14cbcSMatt Macy 	*detector_out = detector;
671eda14cbcSMatt Macy 	return (B_TRUE);
672eda14cbcSMatt Macy }
673eda14cbcSMatt Macy 
674eda14cbcSMatt Macy /* if it's <= 128 bytes, save the corruption directly */
675eda14cbcSMatt Macy #define	ZFM_MAX_INLINE		(128 / sizeof (uint64_t))
676eda14cbcSMatt Macy 
677eda14cbcSMatt Macy #define	MAX_RANGES		16
678eda14cbcSMatt Macy 
679eda14cbcSMatt Macy typedef struct zfs_ecksum_info {
680eda14cbcSMatt Macy 	/* histograms of set and cleared bits by bit number in a 64-bit word */
681eda14cbcSMatt Macy 	uint32_t zei_histogram_set[sizeof (uint64_t) * NBBY];
682eda14cbcSMatt Macy 	uint32_t zei_histogram_cleared[sizeof (uint64_t) * NBBY];
683eda14cbcSMatt Macy 
684eda14cbcSMatt Macy 	/* inline arrays of bits set and cleared. */
685eda14cbcSMatt Macy 	uint64_t zei_bits_set[ZFM_MAX_INLINE];
686eda14cbcSMatt Macy 	uint64_t zei_bits_cleared[ZFM_MAX_INLINE];
687eda14cbcSMatt Macy 
688eda14cbcSMatt Macy 	/*
689eda14cbcSMatt Macy 	 * for each range, the number of bits set and cleared.  The Hamming
690eda14cbcSMatt Macy 	 * distance between the good and bad buffers is the sum of them all.
691eda14cbcSMatt Macy 	 */
692eda14cbcSMatt Macy 	uint32_t zei_range_sets[MAX_RANGES];
693eda14cbcSMatt Macy 	uint32_t zei_range_clears[MAX_RANGES];
694eda14cbcSMatt Macy 
695eda14cbcSMatt Macy 	struct zei_ranges {
696eda14cbcSMatt Macy 		uint32_t	zr_start;
697eda14cbcSMatt Macy 		uint32_t	zr_end;
698eda14cbcSMatt Macy 	} zei_ranges[MAX_RANGES];
699eda14cbcSMatt Macy 
700eda14cbcSMatt Macy 	size_t	zei_range_count;
701eda14cbcSMatt Macy 	uint32_t zei_mingap;
702eda14cbcSMatt Macy 	uint32_t zei_allowed_mingap;
703eda14cbcSMatt Macy 
704eda14cbcSMatt Macy } zfs_ecksum_info_t;
705eda14cbcSMatt Macy 
706eda14cbcSMatt Macy static void
707eda14cbcSMatt Macy update_histogram(uint64_t value_arg, uint32_t *hist, uint32_t *count)
708eda14cbcSMatt Macy {
709eda14cbcSMatt Macy 	size_t i;
710eda14cbcSMatt Macy 	size_t bits = 0;
711eda14cbcSMatt Macy 	uint64_t value = BE_64(value_arg);
712eda14cbcSMatt Macy 
713eda14cbcSMatt Macy 	/* We store the bits in big-endian (largest-first) order */
714eda14cbcSMatt Macy 	for (i = 0; i < 64; i++) {
715eda14cbcSMatt Macy 		if (value & (1ull << i)) {
716eda14cbcSMatt Macy 			hist[63 - i]++;
717eda14cbcSMatt Macy 			++bits;
718eda14cbcSMatt Macy 		}
719eda14cbcSMatt Macy 	}
720eda14cbcSMatt Macy 	/* update the count of bits changed */
721eda14cbcSMatt Macy 	*count += bits;
722eda14cbcSMatt Macy }
723eda14cbcSMatt Macy 
724eda14cbcSMatt Macy /*
725eda14cbcSMatt Macy  * We've now filled up the range array, and need to increase "mingap" and
726eda14cbcSMatt Macy  * shrink the range list accordingly.  zei_mingap is always the smallest
727eda14cbcSMatt Macy  * distance between array entries, so we set the new_allowed_gap to be
728eda14cbcSMatt Macy  * one greater than that.  We then go through the list, joining together
729eda14cbcSMatt Macy  * any ranges which are closer than the new_allowed_gap.
730eda14cbcSMatt Macy  *
731eda14cbcSMatt Macy  * By construction, there will be at least one.  We also update zei_mingap
732eda14cbcSMatt Macy  * to the new smallest gap, to prepare for our next invocation.
733eda14cbcSMatt Macy  */
734eda14cbcSMatt Macy static void
735eda14cbcSMatt Macy zei_shrink_ranges(zfs_ecksum_info_t *eip)
736eda14cbcSMatt Macy {
737eda14cbcSMatt Macy 	uint32_t mingap = UINT32_MAX;
738eda14cbcSMatt Macy 	uint32_t new_allowed_gap = eip->zei_mingap + 1;
739eda14cbcSMatt Macy 
740eda14cbcSMatt Macy 	size_t idx, output;
741eda14cbcSMatt Macy 	size_t max = eip->zei_range_count;
742eda14cbcSMatt Macy 
743eda14cbcSMatt Macy 	struct zei_ranges *r = eip->zei_ranges;
744eda14cbcSMatt Macy 
745eda14cbcSMatt Macy 	ASSERT3U(eip->zei_range_count, >, 0);
746eda14cbcSMatt Macy 	ASSERT3U(eip->zei_range_count, <=, MAX_RANGES);
747eda14cbcSMatt Macy 
748eda14cbcSMatt Macy 	output = idx = 0;
749eda14cbcSMatt Macy 	while (idx < max - 1) {
750eda14cbcSMatt Macy 		uint32_t start = r[idx].zr_start;
751eda14cbcSMatt Macy 		uint32_t end = r[idx].zr_end;
752eda14cbcSMatt Macy 
753eda14cbcSMatt Macy 		while (idx < max - 1) {
754eda14cbcSMatt Macy 			idx++;
755eda14cbcSMatt Macy 
756eda14cbcSMatt Macy 			uint32_t nstart = r[idx].zr_start;
757eda14cbcSMatt Macy 			uint32_t nend = r[idx].zr_end;
758eda14cbcSMatt Macy 
759eda14cbcSMatt Macy 			uint32_t gap = nstart - end;
760eda14cbcSMatt Macy 			if (gap < new_allowed_gap) {
761eda14cbcSMatt Macy 				end = nend;
762eda14cbcSMatt Macy 				continue;
763eda14cbcSMatt Macy 			}
764eda14cbcSMatt Macy 			if (gap < mingap)
765eda14cbcSMatt Macy 				mingap = gap;
766eda14cbcSMatt Macy 			break;
767eda14cbcSMatt Macy 		}
768eda14cbcSMatt Macy 		r[output].zr_start = start;
769eda14cbcSMatt Macy 		r[output].zr_end = end;
770eda14cbcSMatt Macy 		output++;
771eda14cbcSMatt Macy 	}
772eda14cbcSMatt Macy 	ASSERT3U(output, <, eip->zei_range_count);
773eda14cbcSMatt Macy 	eip->zei_range_count = output;
774eda14cbcSMatt Macy 	eip->zei_mingap = mingap;
775eda14cbcSMatt Macy 	eip->zei_allowed_mingap = new_allowed_gap;
776eda14cbcSMatt Macy }
777eda14cbcSMatt Macy 
778eda14cbcSMatt Macy static void
779eda14cbcSMatt Macy zei_add_range(zfs_ecksum_info_t *eip, int start, int end)
780eda14cbcSMatt Macy {
781eda14cbcSMatt Macy 	struct zei_ranges *r = eip->zei_ranges;
782eda14cbcSMatt Macy 	size_t count = eip->zei_range_count;
783eda14cbcSMatt Macy 
784eda14cbcSMatt Macy 	if (count >= MAX_RANGES) {
785eda14cbcSMatt Macy 		zei_shrink_ranges(eip);
786eda14cbcSMatt Macy 		count = eip->zei_range_count;
787eda14cbcSMatt Macy 	}
788eda14cbcSMatt Macy 	if (count == 0) {
789eda14cbcSMatt Macy 		eip->zei_mingap = UINT32_MAX;
790eda14cbcSMatt Macy 		eip->zei_allowed_mingap = 1;
791eda14cbcSMatt Macy 	} else {
792eda14cbcSMatt Macy 		int gap = start - r[count - 1].zr_end;
793eda14cbcSMatt Macy 
794eda14cbcSMatt Macy 		if (gap < eip->zei_allowed_mingap) {
795eda14cbcSMatt Macy 			r[count - 1].zr_end = end;
796eda14cbcSMatt Macy 			return;
797eda14cbcSMatt Macy 		}
798eda14cbcSMatt Macy 		if (gap < eip->zei_mingap)
799eda14cbcSMatt Macy 			eip->zei_mingap = gap;
800eda14cbcSMatt Macy 	}
801eda14cbcSMatt Macy 	r[count].zr_start = start;
802eda14cbcSMatt Macy 	r[count].zr_end = end;
803eda14cbcSMatt Macy 	eip->zei_range_count++;
804eda14cbcSMatt Macy }
805eda14cbcSMatt Macy 
806eda14cbcSMatt Macy static size_t
807eda14cbcSMatt Macy zei_range_total_size(zfs_ecksum_info_t *eip)
808eda14cbcSMatt Macy {
809eda14cbcSMatt Macy 	struct zei_ranges *r = eip->zei_ranges;
810eda14cbcSMatt Macy 	size_t count = eip->zei_range_count;
811eda14cbcSMatt Macy 	size_t result = 0;
812eda14cbcSMatt Macy 	size_t idx;
813eda14cbcSMatt Macy 
814eda14cbcSMatt Macy 	for (idx = 0; idx < count; idx++)
815eda14cbcSMatt Macy 		result += (r[idx].zr_end - r[idx].zr_start);
816eda14cbcSMatt Macy 
817eda14cbcSMatt Macy 	return (result);
818eda14cbcSMatt Macy }
819eda14cbcSMatt Macy 
820eda14cbcSMatt Macy static zfs_ecksum_info_t *
821eda14cbcSMatt Macy annotate_ecksum(nvlist_t *ereport, zio_bad_cksum_t *info,
822eda14cbcSMatt Macy     const abd_t *goodabd, const abd_t *badabd, size_t size,
823eda14cbcSMatt Macy     boolean_t drop_if_identical)
824eda14cbcSMatt Macy {
825eda14cbcSMatt Macy 	const uint64_t *good;
826eda14cbcSMatt Macy 	const uint64_t *bad;
827eda14cbcSMatt Macy 
828eda14cbcSMatt Macy 	size_t nui64s = size / sizeof (uint64_t);
829eda14cbcSMatt Macy 
830eda14cbcSMatt Macy 	size_t inline_size;
831eda14cbcSMatt Macy 	int no_inline = 0;
832eda14cbcSMatt Macy 	size_t idx;
833eda14cbcSMatt Macy 	size_t range;
834eda14cbcSMatt Macy 
835eda14cbcSMatt Macy 	size_t offset = 0;
836eda14cbcSMatt Macy 	ssize_t start = -1;
837eda14cbcSMatt Macy 
838eda14cbcSMatt Macy 	zfs_ecksum_info_t *eip = kmem_zalloc(sizeof (*eip), KM_SLEEP);
839eda14cbcSMatt Macy 
840eda14cbcSMatt Macy 	/* don't do any annotation for injected checksum errors */
841eda14cbcSMatt Macy 	if (info != NULL && info->zbc_injected)
842eda14cbcSMatt Macy 		return (eip);
843eda14cbcSMatt Macy 
844eda14cbcSMatt Macy 	if (info != NULL && info->zbc_has_cksum) {
845eda14cbcSMatt Macy 		fm_payload_set(ereport,
846eda14cbcSMatt Macy 		    FM_EREPORT_PAYLOAD_ZFS_CKSUM_EXPECTED,
847eda14cbcSMatt Macy 		    DATA_TYPE_UINT64_ARRAY,
848eda14cbcSMatt Macy 		    sizeof (info->zbc_expected) / sizeof (uint64_t),
849eda14cbcSMatt Macy 		    (uint64_t *)&info->zbc_expected,
850eda14cbcSMatt Macy 		    FM_EREPORT_PAYLOAD_ZFS_CKSUM_ACTUAL,
851eda14cbcSMatt Macy 		    DATA_TYPE_UINT64_ARRAY,
852eda14cbcSMatt Macy 		    sizeof (info->zbc_actual) / sizeof (uint64_t),
853eda14cbcSMatt Macy 		    (uint64_t *)&info->zbc_actual,
854eda14cbcSMatt Macy 		    FM_EREPORT_PAYLOAD_ZFS_CKSUM_ALGO,
855eda14cbcSMatt Macy 		    DATA_TYPE_STRING,
856eda14cbcSMatt Macy 		    info->zbc_checksum_name,
857eda14cbcSMatt Macy 		    NULL);
858eda14cbcSMatt Macy 
859eda14cbcSMatt Macy 		if (info->zbc_byteswapped) {
860eda14cbcSMatt Macy 			fm_payload_set(ereport,
861eda14cbcSMatt Macy 			    FM_EREPORT_PAYLOAD_ZFS_CKSUM_BYTESWAP,
862eda14cbcSMatt Macy 			    DATA_TYPE_BOOLEAN, 1,
863eda14cbcSMatt Macy 			    NULL);
864eda14cbcSMatt Macy 		}
865eda14cbcSMatt Macy 	}
866eda14cbcSMatt Macy 
867eda14cbcSMatt Macy 	if (badabd == NULL || goodabd == NULL)
868eda14cbcSMatt Macy 		return (eip);
869eda14cbcSMatt Macy 
870eda14cbcSMatt Macy 	ASSERT3U(nui64s, <=, UINT32_MAX);
871eda14cbcSMatt Macy 	ASSERT3U(size, ==, nui64s * sizeof (uint64_t));
872eda14cbcSMatt Macy 	ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
873eda14cbcSMatt Macy 	ASSERT3U(size, <=, UINT32_MAX);
874eda14cbcSMatt Macy 
875eda14cbcSMatt Macy 	good = (const uint64_t *) abd_borrow_buf_copy((abd_t *)goodabd, size);
876eda14cbcSMatt Macy 	bad = (const uint64_t *) abd_borrow_buf_copy((abd_t *)badabd, size);
877eda14cbcSMatt Macy 
878eda14cbcSMatt Macy 	/* build up the range list by comparing the two buffers. */
879eda14cbcSMatt Macy 	for (idx = 0; idx < nui64s; idx++) {
880eda14cbcSMatt Macy 		if (good[idx] == bad[idx]) {
881eda14cbcSMatt Macy 			if (start == -1)
882eda14cbcSMatt Macy 				continue;
883eda14cbcSMatt Macy 
884eda14cbcSMatt Macy 			zei_add_range(eip, start, idx);
885eda14cbcSMatt Macy 			start = -1;
886eda14cbcSMatt Macy 		} else {
887eda14cbcSMatt Macy 			if (start != -1)
888eda14cbcSMatt Macy 				continue;
889eda14cbcSMatt Macy 
890eda14cbcSMatt Macy 			start = idx;
891eda14cbcSMatt Macy 		}
892eda14cbcSMatt Macy 	}
893eda14cbcSMatt Macy 	if (start != -1)
894eda14cbcSMatt Macy 		zei_add_range(eip, start, idx);
895eda14cbcSMatt Macy 
896eda14cbcSMatt Macy 	/* See if it will fit in our inline buffers */
897eda14cbcSMatt Macy 	inline_size = zei_range_total_size(eip);
898eda14cbcSMatt Macy 	if (inline_size > ZFM_MAX_INLINE)
899eda14cbcSMatt Macy 		no_inline = 1;
900eda14cbcSMatt Macy 
901eda14cbcSMatt Macy 	/*
902eda14cbcSMatt Macy 	 * If there is no change and we want to drop if the buffers are
903eda14cbcSMatt Macy 	 * identical, do so.
904eda14cbcSMatt Macy 	 */
905eda14cbcSMatt Macy 	if (inline_size == 0 && drop_if_identical) {
906eda14cbcSMatt Macy 		kmem_free(eip, sizeof (*eip));
907eda14cbcSMatt Macy 		abd_return_buf((abd_t *)goodabd, (void *)good, size);
908eda14cbcSMatt Macy 		abd_return_buf((abd_t *)badabd, (void *)bad, size);
909eda14cbcSMatt Macy 		return (NULL);
910eda14cbcSMatt Macy 	}
911eda14cbcSMatt Macy 
912eda14cbcSMatt Macy 	/*
913eda14cbcSMatt Macy 	 * Now walk through the ranges, filling in the details of the
914eda14cbcSMatt Macy 	 * differences.  Also convert our uint64_t-array offsets to byte
915eda14cbcSMatt Macy 	 * offsets.
916eda14cbcSMatt Macy 	 */
917eda14cbcSMatt Macy 	for (range = 0; range < eip->zei_range_count; range++) {
918eda14cbcSMatt Macy 		size_t start = eip->zei_ranges[range].zr_start;
919eda14cbcSMatt Macy 		size_t end = eip->zei_ranges[range].zr_end;
920eda14cbcSMatt Macy 
921eda14cbcSMatt Macy 		for (idx = start; idx < end; idx++) {
922eda14cbcSMatt Macy 			uint64_t set, cleared;
923eda14cbcSMatt Macy 
924eda14cbcSMatt Macy 			// bits set in bad, but not in good
925eda14cbcSMatt Macy 			set = ((~good[idx]) & bad[idx]);
926eda14cbcSMatt Macy 			// bits set in good, but not in bad
927eda14cbcSMatt Macy 			cleared = (good[idx] & (~bad[idx]));
928eda14cbcSMatt Macy 
929eda14cbcSMatt Macy 			if (!no_inline) {
930eda14cbcSMatt Macy 				ASSERT3U(offset, <, inline_size);
931eda14cbcSMatt Macy 				eip->zei_bits_set[offset] = set;
932eda14cbcSMatt Macy 				eip->zei_bits_cleared[offset] = cleared;
933eda14cbcSMatt Macy 				offset++;
934eda14cbcSMatt Macy 			}
935eda14cbcSMatt Macy 
936eda14cbcSMatt Macy 			update_histogram(set, eip->zei_histogram_set,
937eda14cbcSMatt Macy 			    &eip->zei_range_sets[range]);
938eda14cbcSMatt Macy 			update_histogram(cleared, eip->zei_histogram_cleared,
939eda14cbcSMatt Macy 			    &eip->zei_range_clears[range]);
940eda14cbcSMatt Macy 		}
941eda14cbcSMatt Macy 
942eda14cbcSMatt Macy 		/* convert to byte offsets */
943eda14cbcSMatt Macy 		eip->zei_ranges[range].zr_start	*= sizeof (uint64_t);
944eda14cbcSMatt Macy 		eip->zei_ranges[range].zr_end	*= sizeof (uint64_t);
945eda14cbcSMatt Macy 	}
946eda14cbcSMatt Macy 
947eda14cbcSMatt Macy 	abd_return_buf((abd_t *)goodabd, (void *)good, size);
948eda14cbcSMatt Macy 	abd_return_buf((abd_t *)badabd, (void *)bad, size);
949eda14cbcSMatt Macy 
950eda14cbcSMatt Macy 	eip->zei_allowed_mingap	*= sizeof (uint64_t);
951eda14cbcSMatt Macy 	inline_size		*= sizeof (uint64_t);
952eda14cbcSMatt Macy 
953eda14cbcSMatt Macy 	/* fill in ereport */
954eda14cbcSMatt Macy 	fm_payload_set(ereport,
955eda14cbcSMatt Macy 	    FM_EREPORT_PAYLOAD_ZFS_BAD_OFFSET_RANGES,
956eda14cbcSMatt Macy 	    DATA_TYPE_UINT32_ARRAY, 2 * eip->zei_range_count,
957eda14cbcSMatt Macy 	    (uint32_t *)eip->zei_ranges,
958eda14cbcSMatt Macy 	    FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_MIN_GAP,
959eda14cbcSMatt Macy 	    DATA_TYPE_UINT32, eip->zei_allowed_mingap,
960eda14cbcSMatt Macy 	    FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_SETS,
961eda14cbcSMatt Macy 	    DATA_TYPE_UINT32_ARRAY, eip->zei_range_count, eip->zei_range_sets,
962eda14cbcSMatt Macy 	    FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_CLEARS,
963eda14cbcSMatt Macy 	    DATA_TYPE_UINT32_ARRAY, eip->zei_range_count, eip->zei_range_clears,
964eda14cbcSMatt Macy 	    NULL);
965eda14cbcSMatt Macy 
966eda14cbcSMatt Macy 	if (!no_inline) {
967eda14cbcSMatt Macy 		fm_payload_set(ereport,
968eda14cbcSMatt Macy 		    FM_EREPORT_PAYLOAD_ZFS_BAD_SET_BITS,
969eda14cbcSMatt Macy 		    DATA_TYPE_UINT8_ARRAY,
970eda14cbcSMatt Macy 		    inline_size, (uint8_t *)eip->zei_bits_set,
971eda14cbcSMatt Macy 		    FM_EREPORT_PAYLOAD_ZFS_BAD_CLEARED_BITS,
972eda14cbcSMatt Macy 		    DATA_TYPE_UINT8_ARRAY,
973eda14cbcSMatt Macy 		    inline_size, (uint8_t *)eip->zei_bits_cleared,
974eda14cbcSMatt Macy 		    NULL);
975eda14cbcSMatt Macy 	} else {
976eda14cbcSMatt Macy 		fm_payload_set(ereport,
977eda14cbcSMatt Macy 		    FM_EREPORT_PAYLOAD_ZFS_BAD_SET_HISTOGRAM,
978eda14cbcSMatt Macy 		    DATA_TYPE_UINT32_ARRAY,
979eda14cbcSMatt Macy 		    NBBY * sizeof (uint64_t), eip->zei_histogram_set,
980eda14cbcSMatt Macy 		    FM_EREPORT_PAYLOAD_ZFS_BAD_CLEARED_HISTOGRAM,
981eda14cbcSMatt Macy 		    DATA_TYPE_UINT32_ARRAY,
982eda14cbcSMatt Macy 		    NBBY * sizeof (uint64_t), eip->zei_histogram_cleared,
983eda14cbcSMatt Macy 		    NULL);
984eda14cbcSMatt Macy 	}
985eda14cbcSMatt Macy 	return (eip);
986eda14cbcSMatt Macy }
987ba27dd8bSMartin Matuska #else
988ba27dd8bSMartin Matuska void
989ba27dd8bSMartin Matuska zfs_ereport_clear(spa_t *spa, vdev_t *vd)
990ba27dd8bSMartin Matuska {
991e92ffd9bSMartin Matuska 	(void) spa, (void) vd;
992ba27dd8bSMartin Matuska }
993eda14cbcSMatt Macy #endif
994eda14cbcSMatt Macy 
995eda14cbcSMatt Macy /*
996eda14cbcSMatt Macy  * Make sure our event is still valid for the given zio/vdev/pool.  For example,
997eda14cbcSMatt Macy  * we don't want to keep logging events for a faulted or missing vdev.
998eda14cbcSMatt Macy  */
999eda14cbcSMatt Macy boolean_t
1000eda14cbcSMatt Macy zfs_ereport_is_valid(const char *subclass, spa_t *spa, vdev_t *vd, zio_t *zio)
1001eda14cbcSMatt Macy {
1002eda14cbcSMatt Macy #ifdef _KERNEL
1003eda14cbcSMatt Macy 	/*
1004eda14cbcSMatt Macy 	 * If we are doing a spa_tryimport() or in recovery mode,
1005eda14cbcSMatt Macy 	 * ignore errors.
1006eda14cbcSMatt Macy 	 */
1007eda14cbcSMatt Macy 	if (spa_load_state(spa) == SPA_LOAD_TRYIMPORT ||
1008eda14cbcSMatt Macy 	    spa_load_state(spa) == SPA_LOAD_RECOVER)
1009eda14cbcSMatt Macy 		return (B_FALSE);
1010eda14cbcSMatt Macy 
1011eda14cbcSMatt Macy 	/*
1012eda14cbcSMatt Macy 	 * If we are in the middle of opening a pool, and the previous attempt
1013eda14cbcSMatt Macy 	 * failed, don't bother logging any new ereports - we're just going to
1014eda14cbcSMatt Macy 	 * get the same diagnosis anyway.
1015eda14cbcSMatt Macy 	 */
1016eda14cbcSMatt Macy 	if (spa_load_state(spa) != SPA_LOAD_NONE &&
1017eda14cbcSMatt Macy 	    spa->spa_last_open_failed)
1018eda14cbcSMatt Macy 		return (B_FALSE);
1019eda14cbcSMatt Macy 
1020eda14cbcSMatt Macy 	if (zio != NULL) {
1021eda14cbcSMatt Macy 		/*
1022eda14cbcSMatt Macy 		 * If this is not a read or write zio, ignore the error.  This
1023eda14cbcSMatt Macy 		 * can occur if the DKIOCFLUSHWRITECACHE ioctl fails.
1024eda14cbcSMatt Macy 		 */
1025eda14cbcSMatt Macy 		if (zio->io_type != ZIO_TYPE_READ &&
1026eda14cbcSMatt Macy 		    zio->io_type != ZIO_TYPE_WRITE)
1027eda14cbcSMatt Macy 			return (B_FALSE);
1028eda14cbcSMatt Macy 
1029eda14cbcSMatt Macy 		if (vd != NULL) {
1030eda14cbcSMatt Macy 			/*
1031eda14cbcSMatt Macy 			 * If the vdev has already been marked as failing due
1032eda14cbcSMatt Macy 			 * to a failed probe, then ignore any subsequent I/O
1033eda14cbcSMatt Macy 			 * errors, as the DE will automatically fault the vdev
1034eda14cbcSMatt Macy 			 * on the first such failure.  This also catches cases
1035eda14cbcSMatt Macy 			 * where vdev_remove_wanted is set and the device has
1036eda14cbcSMatt Macy 			 * not yet been asynchronously placed into the REMOVED
1037eda14cbcSMatt Macy 			 * state.
1038eda14cbcSMatt Macy 			 */
1039eda14cbcSMatt Macy 			if (zio->io_vd == vd && !vdev_accessible(vd, zio))
1040eda14cbcSMatt Macy 				return (B_FALSE);
1041eda14cbcSMatt Macy 
1042eda14cbcSMatt Macy 			/*
1043eda14cbcSMatt Macy 			 * Ignore checksum errors for reads from DTL regions of
1044eda14cbcSMatt Macy 			 * leaf vdevs.
1045eda14cbcSMatt Macy 			 */
1046eda14cbcSMatt Macy 			if (zio->io_type == ZIO_TYPE_READ &&
1047eda14cbcSMatt Macy 			    zio->io_error == ECKSUM &&
1048eda14cbcSMatt Macy 			    vd->vdev_ops->vdev_op_leaf &&
1049eda14cbcSMatt Macy 			    vdev_dtl_contains(vd, DTL_MISSING, zio->io_txg, 1))
1050eda14cbcSMatt Macy 				return (B_FALSE);
1051eda14cbcSMatt Macy 		}
1052eda14cbcSMatt Macy 	}
1053eda14cbcSMatt Macy 
1054eda14cbcSMatt Macy 	/*
1055eda14cbcSMatt Macy 	 * For probe failure, we want to avoid posting ereports if we've
1056eda14cbcSMatt Macy 	 * already removed the device in the meantime.
1057eda14cbcSMatt Macy 	 */
1058eda14cbcSMatt Macy 	if (vd != NULL &&
1059eda14cbcSMatt Macy 	    strcmp(subclass, FM_EREPORT_ZFS_PROBE_FAILURE) == 0 &&
1060eda14cbcSMatt Macy 	    (vd->vdev_remove_wanted || vd->vdev_state == VDEV_STATE_REMOVED))
1061eda14cbcSMatt Macy 		return (B_FALSE);
1062eda14cbcSMatt Macy 
1063eda14cbcSMatt Macy 	/* Ignore bogus delay events (like from ioctls or unqueued IOs) */
1064eda14cbcSMatt Macy 	if ((strcmp(subclass, FM_EREPORT_ZFS_DELAY) == 0) &&
1065eda14cbcSMatt Macy 	    (zio != NULL) && (!zio->io_timestamp)) {
1066eda14cbcSMatt Macy 		return (B_FALSE);
1067eda14cbcSMatt Macy 	}
1068e92ffd9bSMartin Matuska #else
1069e92ffd9bSMartin Matuska 	(void) subclass, (void) spa, (void) vd, (void) zio;
1070eda14cbcSMatt Macy #endif
1071eda14cbcSMatt Macy 	return (B_TRUE);
1072eda14cbcSMatt Macy }
1073eda14cbcSMatt Macy 
1074eda14cbcSMatt Macy /*
10752c48331dSMatt Macy  * Post an ereport for the given subclass
10762c48331dSMatt Macy  *
10772c48331dSMatt Macy  * Returns
10782c48331dSMatt Macy  * - 0 if an event was posted
10792c48331dSMatt Macy  * - EINVAL if there was a problem posting event
10802c48331dSMatt Macy  * - EBUSY if the event was rate limited
10812c48331dSMatt Macy  * - EALREADY if the event was already posted (duplicate)
1082eda14cbcSMatt Macy  */
1083eda14cbcSMatt Macy int
1084eda14cbcSMatt Macy zfs_ereport_post(const char *subclass, spa_t *spa, vdev_t *vd,
10852c48331dSMatt Macy     const zbookmark_phys_t *zb, zio_t *zio, uint64_t state)
1086eda14cbcSMatt Macy {
1087eda14cbcSMatt Macy 	int rc = 0;
1088eda14cbcSMatt Macy #ifdef _KERNEL
1089eda14cbcSMatt Macy 	nvlist_t *ereport = NULL;
1090eda14cbcSMatt Macy 	nvlist_t *detector = NULL;
1091eda14cbcSMatt Macy 
10922c48331dSMatt Macy 	if (!zfs_ereport_is_valid(subclass, spa, vd, zio))
10932c48331dSMatt Macy 		return (EINVAL);
10942c48331dSMatt Macy 
10952c48331dSMatt Macy 	if (zfs_ereport_is_duplicate(subclass, spa, vd, zb, zio, 0, 0))
10962c48331dSMatt Macy 		return (SET_ERROR(EALREADY));
10972c48331dSMatt Macy 
1098eda14cbcSMatt Macy 	if (zfs_is_ratelimiting_event(subclass, vd))
1099eda14cbcSMatt Macy 		return (SET_ERROR(EBUSY));
1100eda14cbcSMatt Macy 
1101eda14cbcSMatt Macy 	if (!zfs_ereport_start(&ereport, &detector, subclass, spa, vd,
11022c48331dSMatt Macy 	    zb, zio, state, 0))
1103eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));	/* couldn't post event */
1104eda14cbcSMatt Macy 
1105eda14cbcSMatt Macy 	if (ereport == NULL)
1106eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
1107eda14cbcSMatt Macy 
1108eda14cbcSMatt Macy 	/* Cleanup is handled by the callback function */
1109eda14cbcSMatt Macy 	rc = zfs_zevent_post(ereport, detector, zfs_zevent_post_cb);
1110e92ffd9bSMartin Matuska #else
1111e92ffd9bSMartin Matuska 	(void) subclass, (void) spa, (void) vd, (void) zb, (void) zio,
1112e92ffd9bSMartin Matuska 	    (void) state;
1113eda14cbcSMatt Macy #endif
1114eda14cbcSMatt Macy 	return (rc);
1115eda14cbcSMatt Macy }
1116eda14cbcSMatt Macy 
11172c48331dSMatt Macy /*
11182c48331dSMatt Macy  * Prepare a checksum ereport
11192c48331dSMatt Macy  *
11202c48331dSMatt Macy  * Returns
11212c48331dSMatt Macy  * - 0 if an event was posted
11222c48331dSMatt Macy  * - EINVAL if there was a problem posting event
11232c48331dSMatt Macy  * - EBUSY if the event was rate limited
11242c48331dSMatt Macy  * - EALREADY if the event was already posted (duplicate)
11252c48331dSMatt Macy  */
11262c48331dSMatt Macy int
1127eda14cbcSMatt Macy zfs_ereport_start_checksum(spa_t *spa, vdev_t *vd, const zbookmark_phys_t *zb,
1128f9693befSMartin Matuska     struct zio *zio, uint64_t offset, uint64_t length, zio_bad_cksum_t *info)
1129eda14cbcSMatt Macy {
1130eda14cbcSMatt Macy 	zio_cksum_report_t *report;
1131eda14cbcSMatt Macy 
1132eda14cbcSMatt Macy #ifdef _KERNEL
11332c48331dSMatt Macy 	if (!zfs_ereport_is_valid(FM_EREPORT_ZFS_CHECKSUM, spa, vd, zio))
11342c48331dSMatt Macy 		return (SET_ERROR(EINVAL));
11352c48331dSMatt Macy 
11362c48331dSMatt Macy 	if (zfs_ereport_is_duplicate(FM_EREPORT_ZFS_CHECKSUM, spa, vd, zb, zio,
11372c48331dSMatt Macy 	    offset, length))
11382c48331dSMatt Macy 		return (SET_ERROR(EALREADY));
11392c48331dSMatt Macy 
1140eda14cbcSMatt Macy 	if (zfs_is_ratelimiting_event(FM_EREPORT_ZFS_CHECKSUM, vd))
11412c48331dSMatt Macy 		return (SET_ERROR(EBUSY));
1142e92ffd9bSMartin Matuska #else
1143e92ffd9bSMartin Matuska 	(void) zb, (void) offset;
1144eda14cbcSMatt Macy #endif
1145eda14cbcSMatt Macy 
1146eda14cbcSMatt Macy 	report = kmem_zalloc(sizeof (*report), KM_SLEEP);
1147eda14cbcSMatt Macy 
1148f9693befSMartin Matuska 	zio_vsd_default_cksum_report(zio, report);
1149eda14cbcSMatt Macy 
1150eda14cbcSMatt Macy 	/* copy the checksum failure information if it was provided */
1151eda14cbcSMatt Macy 	if (info != NULL) {
1152eda14cbcSMatt Macy 		report->zcr_ckinfo = kmem_zalloc(sizeof (*info), KM_SLEEP);
1153*da5137abSMartin Matuska 		memcpy(report->zcr_ckinfo, info, sizeof (*info));
1154eda14cbcSMatt Macy 	}
1155eda14cbcSMatt Macy 
11567877fdebSMatt Macy 	report->zcr_sector = 1ULL << vd->vdev_top->vdev_ashift;
11577877fdebSMatt Macy 	report->zcr_align =
11587877fdebSMatt Macy 	    vdev_psize_to_asize(vd->vdev_top, report->zcr_sector);
1159eda14cbcSMatt Macy 	report->zcr_length = length;
1160eda14cbcSMatt Macy 
1161eda14cbcSMatt Macy #ifdef _KERNEL
1162eac7052fSMatt Macy 	(void) zfs_ereport_start(&report->zcr_ereport, &report->zcr_detector,
1163eda14cbcSMatt Macy 	    FM_EREPORT_ZFS_CHECKSUM, spa, vd, zb, zio, offset, length);
1164eda14cbcSMatt Macy 
1165eda14cbcSMatt Macy 	if (report->zcr_ereport == NULL) {
1166eda14cbcSMatt Macy 		zfs_ereport_free_checksum(report);
11672c48331dSMatt Macy 		return (0);
1168eda14cbcSMatt Macy 	}
1169eda14cbcSMatt Macy #endif
1170eda14cbcSMatt Macy 
1171eda14cbcSMatt Macy 	mutex_enter(&spa->spa_errlist_lock);
1172eda14cbcSMatt Macy 	report->zcr_next = zio->io_logical->io_cksum_report;
1173eda14cbcSMatt Macy 	zio->io_logical->io_cksum_report = report;
1174eda14cbcSMatt Macy 	mutex_exit(&spa->spa_errlist_lock);
11752c48331dSMatt Macy 	return (0);
1176eda14cbcSMatt Macy }
1177eda14cbcSMatt Macy 
1178eda14cbcSMatt Macy void
1179eda14cbcSMatt Macy zfs_ereport_finish_checksum(zio_cksum_report_t *report, const abd_t *good_data,
1180eda14cbcSMatt Macy     const abd_t *bad_data, boolean_t drop_if_identical)
1181eda14cbcSMatt Macy {
1182eda14cbcSMatt Macy #ifdef _KERNEL
1183eda14cbcSMatt Macy 	zfs_ecksum_info_t *info;
1184eda14cbcSMatt Macy 
1185eda14cbcSMatt Macy 	info = annotate_ecksum(report->zcr_ereport, report->zcr_ckinfo,
1186eda14cbcSMatt Macy 	    good_data, bad_data, report->zcr_length, drop_if_identical);
1187eda14cbcSMatt Macy 	if (info != NULL)
1188eda14cbcSMatt Macy 		zfs_zevent_post(report->zcr_ereport,
1189eda14cbcSMatt Macy 		    report->zcr_detector, zfs_zevent_post_cb);
1190eda14cbcSMatt Macy 	else
1191eda14cbcSMatt Macy 		zfs_zevent_post_cb(report->zcr_ereport, report->zcr_detector);
1192eda14cbcSMatt Macy 
1193eda14cbcSMatt Macy 	report->zcr_ereport = report->zcr_detector = NULL;
1194eda14cbcSMatt Macy 	if (info != NULL)
1195eda14cbcSMatt Macy 		kmem_free(info, sizeof (*info));
1196e92ffd9bSMartin Matuska #else
1197e92ffd9bSMartin Matuska 	(void) report, (void) good_data, (void) bad_data,
1198e92ffd9bSMartin Matuska 	    (void) drop_if_identical;
1199eda14cbcSMatt Macy #endif
1200eda14cbcSMatt Macy }
1201eda14cbcSMatt Macy 
1202eda14cbcSMatt Macy void
1203eda14cbcSMatt Macy zfs_ereport_free_checksum(zio_cksum_report_t *rpt)
1204eda14cbcSMatt Macy {
1205eda14cbcSMatt Macy #ifdef _KERNEL
1206eda14cbcSMatt Macy 	if (rpt->zcr_ereport != NULL) {
1207eda14cbcSMatt Macy 		fm_nvlist_destroy(rpt->zcr_ereport,
1208eda14cbcSMatt Macy 		    FM_NVA_FREE);
1209eda14cbcSMatt Macy 		fm_nvlist_destroy(rpt->zcr_detector,
1210eda14cbcSMatt Macy 		    FM_NVA_FREE);
1211eda14cbcSMatt Macy 	}
1212eda14cbcSMatt Macy #endif
1213eda14cbcSMatt Macy 	rpt->zcr_free(rpt->zcr_cbdata, rpt->zcr_cbinfo);
1214eda14cbcSMatt Macy 
1215eda14cbcSMatt Macy 	if (rpt->zcr_ckinfo != NULL)
1216eda14cbcSMatt Macy 		kmem_free(rpt->zcr_ckinfo, sizeof (*rpt->zcr_ckinfo));
1217eda14cbcSMatt Macy 
1218eda14cbcSMatt Macy 	kmem_free(rpt, sizeof (*rpt));
1219eda14cbcSMatt Macy }
1220eda14cbcSMatt Macy 
12212c48331dSMatt Macy /*
12222c48331dSMatt Macy  * Post a checksum ereport
12232c48331dSMatt Macy  *
12242c48331dSMatt Macy  * Returns
12252c48331dSMatt Macy  * - 0 if an event was posted
12262c48331dSMatt Macy  * - EINVAL if there was a problem posting event
12272c48331dSMatt Macy  * - EBUSY if the event was rate limited
12282c48331dSMatt Macy  * - EALREADY if the event was already posted (duplicate)
12292c48331dSMatt Macy  */
1230eda14cbcSMatt Macy int
1231eda14cbcSMatt Macy zfs_ereport_post_checksum(spa_t *spa, vdev_t *vd, const zbookmark_phys_t *zb,
1232eda14cbcSMatt Macy     struct zio *zio, uint64_t offset, uint64_t length,
1233eda14cbcSMatt Macy     const abd_t *good_data, const abd_t *bad_data, zio_bad_cksum_t *zbc)
1234eda14cbcSMatt Macy {
1235eda14cbcSMatt Macy 	int rc = 0;
1236eda14cbcSMatt Macy #ifdef _KERNEL
1237eda14cbcSMatt Macy 	nvlist_t *ereport = NULL;
1238eda14cbcSMatt Macy 	nvlist_t *detector = NULL;
1239eda14cbcSMatt Macy 	zfs_ecksum_info_t *info;
1240eda14cbcSMatt Macy 
12412c48331dSMatt Macy 	if (!zfs_ereport_is_valid(FM_EREPORT_ZFS_CHECKSUM, spa, vd, zio))
12422c48331dSMatt Macy 		return (SET_ERROR(EINVAL));
12432c48331dSMatt Macy 
12442c48331dSMatt Macy 	if (zfs_ereport_is_duplicate(FM_EREPORT_ZFS_CHECKSUM, spa, vd, zb, zio,
12452c48331dSMatt Macy 	    offset, length))
12462c48331dSMatt Macy 		return (SET_ERROR(EALREADY));
12472c48331dSMatt Macy 
1248eda14cbcSMatt Macy 	if (zfs_is_ratelimiting_event(FM_EREPORT_ZFS_CHECKSUM, vd))
12492c48331dSMatt Macy 		return (SET_ERROR(EBUSY));
1250eda14cbcSMatt Macy 
1251eda14cbcSMatt Macy 	if (!zfs_ereport_start(&ereport, &detector, FM_EREPORT_ZFS_CHECKSUM,
1252eda14cbcSMatt Macy 	    spa, vd, zb, zio, offset, length) || (ereport == NULL)) {
1253eda14cbcSMatt Macy 		return (SET_ERROR(EINVAL));
1254eda14cbcSMatt Macy 	}
1255eda14cbcSMatt Macy 
1256eda14cbcSMatt Macy 	info = annotate_ecksum(ereport, zbc, good_data, bad_data, length,
1257eda14cbcSMatt Macy 	    B_FALSE);
1258eda14cbcSMatt Macy 
1259eda14cbcSMatt Macy 	if (info != NULL) {
1260eda14cbcSMatt Macy 		rc = zfs_zevent_post(ereport, detector, zfs_zevent_post_cb);
1261eda14cbcSMatt Macy 		kmem_free(info, sizeof (*info));
1262eda14cbcSMatt Macy 	}
1263e92ffd9bSMartin Matuska #else
1264e92ffd9bSMartin Matuska 	(void) spa, (void) vd, (void) zb, (void) zio, (void) offset,
1265e92ffd9bSMartin Matuska 	    (void) length, (void) good_data, (void) bad_data, (void) zbc;
1266eda14cbcSMatt Macy #endif
1267eda14cbcSMatt Macy 	return (rc);
1268eda14cbcSMatt Macy }
1269eda14cbcSMatt Macy 
1270eda14cbcSMatt Macy /*
1271eda14cbcSMatt Macy  * The 'sysevent.fs.zfs.*' events are signals posted to notify user space of
1272eda14cbcSMatt Macy  * change in the pool.  All sysevents are listed in sys/sysevent/eventdefs.h
1273eda14cbcSMatt Macy  * and are designed to be consumed by the ZFS Event Daemon (ZED).  For
1274eda14cbcSMatt Macy  * additional details refer to the zed(8) man page.
1275eda14cbcSMatt Macy  */
1276eda14cbcSMatt Macy nvlist_t *
1277eda14cbcSMatt Macy zfs_event_create(spa_t *spa, vdev_t *vd, const char *type, const char *name,
1278eda14cbcSMatt Macy     nvlist_t *aux)
1279eda14cbcSMatt Macy {
1280eda14cbcSMatt Macy 	nvlist_t *resource = NULL;
1281eda14cbcSMatt Macy #ifdef _KERNEL
1282eda14cbcSMatt Macy 	char class[64];
1283eda14cbcSMatt Macy 
1284eda14cbcSMatt Macy 	if (spa_load_state(spa) == SPA_LOAD_TRYIMPORT)
1285eda14cbcSMatt Macy 		return (NULL);
1286eda14cbcSMatt Macy 
1287eda14cbcSMatt Macy 	if ((resource = fm_nvlist_create(NULL)) == NULL)
1288eda14cbcSMatt Macy 		return (NULL);
1289eda14cbcSMatt Macy 
1290eda14cbcSMatt Macy 	(void) snprintf(class, sizeof (class), "%s.%s.%s", type,
1291eda14cbcSMatt Macy 	    ZFS_ERROR_CLASS, name);
1292eda14cbcSMatt Macy 	VERIFY0(nvlist_add_uint8(resource, FM_VERSION, FM_RSRC_VERSION));
1293eda14cbcSMatt Macy 	VERIFY0(nvlist_add_string(resource, FM_CLASS, class));
1294eda14cbcSMatt Macy 	VERIFY0(nvlist_add_string(resource,
1295eda14cbcSMatt Macy 	    FM_EREPORT_PAYLOAD_ZFS_POOL, spa_name(spa)));
1296eda14cbcSMatt Macy 	VERIFY0(nvlist_add_uint64(resource,
1297eda14cbcSMatt Macy 	    FM_EREPORT_PAYLOAD_ZFS_POOL_GUID, spa_guid(spa)));
1298eda14cbcSMatt Macy 	VERIFY0(nvlist_add_uint64(resource,
1299eda14cbcSMatt Macy 	    FM_EREPORT_PAYLOAD_ZFS_POOL_STATE, spa_state(spa)));
1300eda14cbcSMatt Macy 	VERIFY0(nvlist_add_int32(resource,
1301eda14cbcSMatt Macy 	    FM_EREPORT_PAYLOAD_ZFS_POOL_CONTEXT, spa_load_state(spa)));
1302eda14cbcSMatt Macy 
1303eda14cbcSMatt Macy 	if (vd) {
1304eda14cbcSMatt Macy 		VERIFY0(nvlist_add_uint64(resource,
1305eda14cbcSMatt Macy 		    FM_EREPORT_PAYLOAD_ZFS_VDEV_GUID, vd->vdev_guid));
1306eda14cbcSMatt Macy 		VERIFY0(nvlist_add_uint64(resource,
1307eda14cbcSMatt Macy 		    FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE, vd->vdev_state));
1308eda14cbcSMatt Macy 		if (vd->vdev_path != NULL)
1309eda14cbcSMatt Macy 			VERIFY0(nvlist_add_string(resource,
1310eda14cbcSMatt Macy 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_PATH, vd->vdev_path));
1311eda14cbcSMatt Macy 		if (vd->vdev_devid != NULL)
1312eda14cbcSMatt Macy 			VERIFY0(nvlist_add_string(resource,
1313eda14cbcSMatt Macy 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_DEVID, vd->vdev_devid));
1314eda14cbcSMatt Macy 		if (vd->vdev_fru != NULL)
1315eda14cbcSMatt Macy 			VERIFY0(nvlist_add_string(resource,
1316eda14cbcSMatt Macy 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_FRU, vd->vdev_fru));
1317eda14cbcSMatt Macy 		if (vd->vdev_enc_sysfs_path != NULL)
1318eda14cbcSMatt Macy 			VERIFY0(nvlist_add_string(resource,
1319eda14cbcSMatt Macy 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_ENC_SYSFS_PATH,
1320eda14cbcSMatt Macy 			    vd->vdev_enc_sysfs_path));
1321eda14cbcSMatt Macy 	}
1322eda14cbcSMatt Macy 
1323eda14cbcSMatt Macy 	/* also copy any optional payload data */
1324eda14cbcSMatt Macy 	if (aux) {
1325eda14cbcSMatt Macy 		nvpair_t *elem = NULL;
1326eda14cbcSMatt Macy 
1327eda14cbcSMatt Macy 		while ((elem = nvlist_next_nvpair(aux, elem)) != NULL)
1328eda14cbcSMatt Macy 			(void) nvlist_add_nvpair(resource, elem);
1329eda14cbcSMatt Macy 	}
1330e92ffd9bSMartin Matuska #else
1331e92ffd9bSMartin Matuska 	(void) spa, (void) vd, (void) type, (void) name, (void) aux;
1332eda14cbcSMatt Macy #endif
1333eda14cbcSMatt Macy 	return (resource);
1334eda14cbcSMatt Macy }
1335eda14cbcSMatt Macy 
1336eda14cbcSMatt Macy static void
1337eda14cbcSMatt Macy zfs_post_common(spa_t *spa, vdev_t *vd, const char *type, const char *name,
1338eda14cbcSMatt Macy     nvlist_t *aux)
1339eda14cbcSMatt Macy {
1340eda14cbcSMatt Macy #ifdef _KERNEL
1341eda14cbcSMatt Macy 	nvlist_t *resource;
1342eda14cbcSMatt Macy 
1343eda14cbcSMatt Macy 	resource = zfs_event_create(spa, vd, type, name, aux);
1344eda14cbcSMatt Macy 	if (resource)
1345eda14cbcSMatt Macy 		zfs_zevent_post(resource, NULL, zfs_zevent_post_cb);
1346e92ffd9bSMartin Matuska #else
1347e92ffd9bSMartin Matuska 	(void) spa, (void) vd, (void) type, (void) name, (void) aux;
1348eda14cbcSMatt Macy #endif
1349eda14cbcSMatt Macy }
1350eda14cbcSMatt Macy 
1351eda14cbcSMatt Macy /*
1352eda14cbcSMatt Macy  * The 'resource.fs.zfs.removed' event is an internal signal that the given vdev
1353eda14cbcSMatt Macy  * has been removed from the system.  This will cause the DE to ignore any
1354eda14cbcSMatt Macy  * recent I/O errors, inferring that they are due to the asynchronous device
1355eda14cbcSMatt Macy  * removal.
1356eda14cbcSMatt Macy  */
1357eda14cbcSMatt Macy void
1358eda14cbcSMatt Macy zfs_post_remove(spa_t *spa, vdev_t *vd)
1359eda14cbcSMatt Macy {
1360eda14cbcSMatt Macy 	zfs_post_common(spa, vd, FM_RSRC_CLASS, FM_RESOURCE_REMOVED, NULL);
1361eda14cbcSMatt Macy }
1362eda14cbcSMatt Macy 
1363eda14cbcSMatt Macy /*
1364eda14cbcSMatt Macy  * The 'resource.fs.zfs.autoreplace' event is an internal signal that the pool
1365eda14cbcSMatt Macy  * has the 'autoreplace' property set, and therefore any broken vdevs will be
1366eda14cbcSMatt Macy  * handled by higher level logic, and no vdev fault should be generated.
1367eda14cbcSMatt Macy  */
1368eda14cbcSMatt Macy void
1369eda14cbcSMatt Macy zfs_post_autoreplace(spa_t *spa, vdev_t *vd)
1370eda14cbcSMatt Macy {
1371eda14cbcSMatt Macy 	zfs_post_common(spa, vd, FM_RSRC_CLASS, FM_RESOURCE_AUTOREPLACE, NULL);
1372eda14cbcSMatt Macy }
1373eda14cbcSMatt Macy 
1374eda14cbcSMatt Macy /*
1375eda14cbcSMatt Macy  * The 'resource.fs.zfs.statechange' event is an internal signal that the
1376eda14cbcSMatt Macy  * given vdev has transitioned its state to DEGRADED or HEALTHY.  This will
1377eda14cbcSMatt Macy  * cause the retire agent to repair any outstanding fault management cases
1378eda14cbcSMatt Macy  * open because the device was not found (fault.fs.zfs.device).
1379eda14cbcSMatt Macy  */
1380eda14cbcSMatt Macy void
1381eda14cbcSMatt Macy zfs_post_state_change(spa_t *spa, vdev_t *vd, uint64_t laststate)
1382eda14cbcSMatt Macy {
1383eda14cbcSMatt Macy #ifdef _KERNEL
1384eda14cbcSMatt Macy 	nvlist_t *aux;
1385eda14cbcSMatt Macy 
1386eda14cbcSMatt Macy 	/*
1387eda14cbcSMatt Macy 	 * Add optional supplemental keys to payload
1388eda14cbcSMatt Macy 	 */
1389eda14cbcSMatt Macy 	aux = fm_nvlist_create(NULL);
1390eda14cbcSMatt Macy 	if (vd && aux) {
1391eda14cbcSMatt Macy 		if (vd->vdev_physpath) {
1392eda14cbcSMatt Macy 			(void) nvlist_add_string(aux,
1393eda14cbcSMatt Macy 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_PHYSPATH,
1394eda14cbcSMatt Macy 			    vd->vdev_physpath);
1395eda14cbcSMatt Macy 		}
1396eda14cbcSMatt Macy 		if (vd->vdev_enc_sysfs_path) {
1397eda14cbcSMatt Macy 			(void) nvlist_add_string(aux,
1398eda14cbcSMatt Macy 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_ENC_SYSFS_PATH,
1399eda14cbcSMatt Macy 			    vd->vdev_enc_sysfs_path);
1400eda14cbcSMatt Macy 		}
1401eda14cbcSMatt Macy 
1402eda14cbcSMatt Macy 		(void) nvlist_add_uint64(aux,
1403eda14cbcSMatt Macy 		    FM_EREPORT_PAYLOAD_ZFS_VDEV_LASTSTATE, laststate);
1404eda14cbcSMatt Macy 	}
1405eda14cbcSMatt Macy 
1406eda14cbcSMatt Macy 	zfs_post_common(spa, vd, FM_RSRC_CLASS, FM_RESOURCE_STATECHANGE,
1407eda14cbcSMatt Macy 	    aux);
1408eda14cbcSMatt Macy 
1409eda14cbcSMatt Macy 	if (aux)
1410eda14cbcSMatt Macy 		fm_nvlist_destroy(aux, FM_NVA_FREE);
1411e92ffd9bSMartin Matuska #else
1412e92ffd9bSMartin Matuska 	(void) spa, (void) vd, (void) laststate;
1413eda14cbcSMatt Macy #endif
1414eda14cbcSMatt Macy }
1415eda14cbcSMatt Macy 
14162c48331dSMatt Macy #ifdef _KERNEL
14172c48331dSMatt Macy void
14182c48331dSMatt Macy zfs_ereport_init(void)
14192c48331dSMatt Macy {
14202c48331dSMatt Macy 	mutex_init(&recent_events_lock, NULL, MUTEX_DEFAULT, NULL);
14212c48331dSMatt Macy 	list_create(&recent_events_list, sizeof (recent_events_node_t),
14222c48331dSMatt Macy 	    offsetof(recent_events_node_t, re_list_link));
14232c48331dSMatt Macy 	avl_create(&recent_events_tree,  recent_events_compare,
14242c48331dSMatt Macy 	    sizeof (recent_events_node_t), offsetof(recent_events_node_t,
14252c48331dSMatt Macy 	    re_tree_link));
14262c48331dSMatt Macy }
14272c48331dSMatt Macy 
14282c48331dSMatt Macy /*
14292c48331dSMatt Macy  * This 'early' fini needs to run before zfs_fini() which on Linux waits
14302c48331dSMatt Macy  * for the system_delay_taskq to drain.
14312c48331dSMatt Macy  */
14322c48331dSMatt Macy void
14332c48331dSMatt Macy zfs_ereport_taskq_fini(void)
14342c48331dSMatt Macy {
14352c48331dSMatt Macy 	mutex_enter(&recent_events_lock);
14362c48331dSMatt Macy 	if (recent_events_cleaner_tqid != 0) {
14372c48331dSMatt Macy 		taskq_cancel_id(system_delay_taskq, recent_events_cleaner_tqid);
14382c48331dSMatt Macy 		recent_events_cleaner_tqid = 0;
14392c48331dSMatt Macy 	}
14402c48331dSMatt Macy 	mutex_exit(&recent_events_lock);
14412c48331dSMatt Macy }
14422c48331dSMatt Macy 
14432c48331dSMatt Macy void
14442c48331dSMatt Macy zfs_ereport_fini(void)
14452c48331dSMatt Macy {
14462c48331dSMatt Macy 	recent_events_node_t *entry;
14472c48331dSMatt Macy 
14482c48331dSMatt Macy 	while ((entry = list_head(&recent_events_list)) != NULL) {
14492c48331dSMatt Macy 		avl_remove(&recent_events_tree, entry);
14502c48331dSMatt Macy 		list_remove(&recent_events_list, entry);
14512c48331dSMatt Macy 		kmem_free(entry, sizeof (*entry));
14522c48331dSMatt Macy 	}
14532c48331dSMatt Macy 	avl_destroy(&recent_events_tree);
14542c48331dSMatt Macy 	list_destroy(&recent_events_list);
14552c48331dSMatt Macy 	mutex_destroy(&recent_events_lock);
14562c48331dSMatt Macy }
14572c48331dSMatt Macy 
145853b70c86SMartin Matuska void
145953b70c86SMartin Matuska zfs_ereport_snapshot_post(const char *subclass, spa_t *spa, const char *name)
146053b70c86SMartin Matuska {
146153b70c86SMartin Matuska 	nvlist_t *aux;
146253b70c86SMartin Matuska 
146353b70c86SMartin Matuska 	aux = fm_nvlist_create(NULL);
146453b70c86SMartin Matuska 	nvlist_add_string(aux, FM_EREPORT_PAYLOAD_ZFS_SNAPSHOT_NAME, name);
146553b70c86SMartin Matuska 
146653b70c86SMartin Matuska 	zfs_post_common(spa, NULL, FM_RSRC_CLASS, subclass, aux);
146753b70c86SMartin Matuska 	fm_nvlist_destroy(aux, FM_NVA_FREE);
146853b70c86SMartin Matuska }
146953b70c86SMartin Matuska 
147053b70c86SMartin Matuska /*
147153b70c86SMartin Matuska  * Post when a event when a zvol is created or removed
147253b70c86SMartin Matuska  *
147353b70c86SMartin Matuska  * This is currently only used by macOS, since it uses the event to create
147453b70c86SMartin Matuska  * symlinks between the volume name (mypool/myvol) and the actual /dev
147553b70c86SMartin Matuska  * device (/dev/disk3).  For example:
147653b70c86SMartin Matuska  *
147753b70c86SMartin Matuska  * /var/run/zfs/dsk/mypool/myvol -> /dev/disk3
147853b70c86SMartin Matuska  *
147953b70c86SMartin Matuska  * name: The full name of the zvol ("mypool/myvol")
148053b70c86SMartin Matuska  * dev_name: The full /dev name for the zvol ("/dev/disk3")
148153b70c86SMartin Matuska  * raw_name: The raw  /dev name for the zvol ("/dev/rdisk3")
148253b70c86SMartin Matuska  */
148353b70c86SMartin Matuska void
148453b70c86SMartin Matuska zfs_ereport_zvol_post(const char *subclass, const char *name,
148553b70c86SMartin Matuska     const char *dev_name, const char *raw_name)
148653b70c86SMartin Matuska {
148753b70c86SMartin Matuska 	nvlist_t *aux;
148853b70c86SMartin Matuska 	char *r;
148953b70c86SMartin Matuska 
149053b70c86SMartin Matuska 	boolean_t locked = mutex_owned(&spa_namespace_lock);
149153b70c86SMartin Matuska 	if (!locked) mutex_enter(&spa_namespace_lock);
149253b70c86SMartin Matuska 	spa_t *spa = spa_lookup(name);
149353b70c86SMartin Matuska 	if (!locked) mutex_exit(&spa_namespace_lock);
149453b70c86SMartin Matuska 
149553b70c86SMartin Matuska 	if (spa == NULL)
149653b70c86SMartin Matuska 		return;
149753b70c86SMartin Matuska 
149853b70c86SMartin Matuska 	aux = fm_nvlist_create(NULL);
149953b70c86SMartin Matuska 	nvlist_add_string(aux, FM_EREPORT_PAYLOAD_ZFS_DEVICE_NAME, dev_name);
150053b70c86SMartin Matuska 	nvlist_add_string(aux, FM_EREPORT_PAYLOAD_ZFS_RAW_DEVICE_NAME,
150153b70c86SMartin Matuska 	    raw_name);
150253b70c86SMartin Matuska 	r = strchr(name, '/');
150353b70c86SMartin Matuska 	if (r && r[1])
150453b70c86SMartin Matuska 		nvlist_add_string(aux, FM_EREPORT_PAYLOAD_ZFS_VOLUME, &r[1]);
150553b70c86SMartin Matuska 
150653b70c86SMartin Matuska 	zfs_post_common(spa, NULL, FM_RSRC_CLASS, subclass, aux);
150753b70c86SMartin Matuska 	fm_nvlist_destroy(aux, FM_NVA_FREE);
150853b70c86SMartin Matuska }
150953b70c86SMartin Matuska 
1510eda14cbcSMatt Macy EXPORT_SYMBOL(zfs_ereport_post);
1511eda14cbcSMatt Macy EXPORT_SYMBOL(zfs_ereport_is_valid);
1512eda14cbcSMatt Macy EXPORT_SYMBOL(zfs_ereport_post_checksum);
1513eda14cbcSMatt Macy EXPORT_SYMBOL(zfs_post_remove);
1514eda14cbcSMatt Macy EXPORT_SYMBOL(zfs_post_autoreplace);
1515eda14cbcSMatt Macy EXPORT_SYMBOL(zfs_post_state_change);
15162c48331dSMatt Macy 
15172c48331dSMatt Macy ZFS_MODULE_PARAM(zfs_zevent, zfs_zevent_, retain_max, UINT, ZMOD_RW,
15182c48331dSMatt Macy 	"Maximum recent zevents records to retain for duplicate checking");
15192c48331dSMatt Macy ZFS_MODULE_PARAM(zfs_zevent, zfs_zevent_, retain_expire_secs, UINT, ZMOD_RW,
15202c48331dSMatt Macy 	"Expiration time for recent zevents records");
1521eda14cbcSMatt Macy #endif /* _KERNEL */
1522