xref: /freebsd/sys/contrib/openzfs/module/zfs/zfs_fm.c (revision 7a7741af18d6c8a804cc643cb7ecda9d730c6aa6)
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
recent_events_compare(const void * a,const void * b)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 
20315f0b8c3SMartin Matuska /*
20415f0b8c3SMartin Matuska  * workaround: vdev properties don't have inheritance
20515f0b8c3SMartin Matuska  */
20615f0b8c3SMartin Matuska static uint64_t
vdev_prop_get_inherited(vdev_t * vd,vdev_prop_t prop)20715f0b8c3SMartin Matuska vdev_prop_get_inherited(vdev_t *vd, vdev_prop_t prop)
20815f0b8c3SMartin Matuska {
20915f0b8c3SMartin Matuska 	uint64_t propdef, propval;
21015f0b8c3SMartin Matuska 
21115f0b8c3SMartin Matuska 	propdef = vdev_prop_default_numeric(prop);
21215f0b8c3SMartin Matuska 	switch (prop) {
21315f0b8c3SMartin Matuska 		case VDEV_PROP_CHECKSUM_N:
21415f0b8c3SMartin Matuska 			propval = vd->vdev_checksum_n;
21515f0b8c3SMartin Matuska 			break;
21615f0b8c3SMartin Matuska 		case VDEV_PROP_CHECKSUM_T:
21715f0b8c3SMartin Matuska 			propval = vd->vdev_checksum_t;
21815f0b8c3SMartin Matuska 			break;
21915f0b8c3SMartin Matuska 		case VDEV_PROP_IO_N:
22015f0b8c3SMartin Matuska 			propval = vd->vdev_io_n;
22115f0b8c3SMartin Matuska 			break;
22215f0b8c3SMartin Matuska 		case VDEV_PROP_IO_T:
22315f0b8c3SMartin Matuska 			propval = vd->vdev_io_t;
22415f0b8c3SMartin Matuska 			break;
225e2257b31SMartin Matuska 		case VDEV_PROP_SLOW_IO_N:
226e2257b31SMartin Matuska 			propval = vd->vdev_slow_io_n;
227e2257b31SMartin Matuska 			break;
228e2257b31SMartin Matuska 		case VDEV_PROP_SLOW_IO_T:
229e2257b31SMartin Matuska 			propval = vd->vdev_slow_io_t;
230e2257b31SMartin Matuska 			break;
23115f0b8c3SMartin Matuska 		default:
23215f0b8c3SMartin Matuska 			propval = propdef;
23315f0b8c3SMartin Matuska 			break;
23415f0b8c3SMartin Matuska 	}
23515f0b8c3SMartin Matuska 
23615f0b8c3SMartin Matuska 	if (propval != propdef)
23715f0b8c3SMartin Matuska 		return (propval);
23815f0b8c3SMartin Matuska 
23915f0b8c3SMartin Matuska 	if (vd->vdev_parent == NULL)
24015f0b8c3SMartin Matuska 		return (propdef);
24115f0b8c3SMartin Matuska 
24215f0b8c3SMartin Matuska 	return (vdev_prop_get_inherited(vd->vdev_parent, prop));
24315f0b8c3SMartin Matuska }
24415f0b8c3SMartin Matuska 
2452c48331dSMatt Macy static void zfs_ereport_schedule_cleaner(void);
2462c48331dSMatt Macy 
2472c48331dSMatt Macy /*
2482c48331dSMatt Macy  * background task to clean stale recent event nodes.
2492c48331dSMatt Macy  */
2502c48331dSMatt Macy static void
zfs_ereport_cleaner(void * arg)2512c48331dSMatt Macy zfs_ereport_cleaner(void *arg)
2522c48331dSMatt Macy {
2532c48331dSMatt Macy 	recent_events_node_t *entry;
2542c48331dSMatt Macy 	uint64_t now = gethrtime();
2552c48331dSMatt Macy 
2562c48331dSMatt Macy 	/*
2572c48331dSMatt Macy 	 * purge expired entries
2582c48331dSMatt Macy 	 */
2592c48331dSMatt Macy 	mutex_enter(&recent_events_lock);
2602c48331dSMatt Macy 	while ((entry = list_tail(&recent_events_list)) != NULL) {
2612c48331dSMatt Macy 		uint64_t age = NSEC2SEC(now - entry->re_timestamp);
2622c48331dSMatt Macy 		if (age <= zfs_zevent_retain_expire_secs)
2632c48331dSMatt Macy 			break;
2642c48331dSMatt Macy 
2652c48331dSMatt Macy 		/* remove expired node */
2662c48331dSMatt Macy 		avl_remove(&recent_events_tree, entry);
2672c48331dSMatt Macy 		list_remove(&recent_events_list, entry);
2682c48331dSMatt Macy 		kmem_free(entry, sizeof (*entry));
2692c48331dSMatt Macy 	}
2702c48331dSMatt Macy 
2712c48331dSMatt Macy 	/* Restart the cleaner if more entries remain */
2722c48331dSMatt Macy 	recent_events_cleaner_tqid = 0;
2732c48331dSMatt Macy 	if (!list_is_empty(&recent_events_list))
2742c48331dSMatt Macy 		zfs_ereport_schedule_cleaner();
2752c48331dSMatt Macy 
2762c48331dSMatt Macy 	mutex_exit(&recent_events_lock);
2772c48331dSMatt Macy }
2782c48331dSMatt Macy 
2792c48331dSMatt Macy static void
zfs_ereport_schedule_cleaner(void)2802c48331dSMatt Macy zfs_ereport_schedule_cleaner(void)
2812c48331dSMatt Macy {
2822c48331dSMatt Macy 	ASSERT(MUTEX_HELD(&recent_events_lock));
2832c48331dSMatt Macy 
2842c48331dSMatt Macy 	uint64_t timeout = SEC2NSEC(zfs_zevent_retain_expire_secs + 1);
2852c48331dSMatt Macy 
2862c48331dSMatt Macy 	recent_events_cleaner_tqid = taskq_dispatch_delay(
2872c48331dSMatt Macy 	    system_delay_taskq, zfs_ereport_cleaner, NULL, TQ_SLEEP,
2882c48331dSMatt Macy 	    ddi_get_lbolt() + NSEC_TO_TICK(timeout));
2892c48331dSMatt Macy }
2902c48331dSMatt Macy 
2912c48331dSMatt Macy /*
292ba27dd8bSMartin Matuska  * Clear entries for a given vdev or all vdevs in a pool when vdev == NULL
293ba27dd8bSMartin Matuska  */
294ba27dd8bSMartin Matuska void
zfs_ereport_clear(spa_t * spa,vdev_t * vd)295ba27dd8bSMartin Matuska zfs_ereport_clear(spa_t *spa, vdev_t *vd)
296ba27dd8bSMartin Matuska {
297ba27dd8bSMartin Matuska 	uint64_t vdev_guid, pool_guid;
298ba27dd8bSMartin Matuska 
299ba27dd8bSMartin Matuska 	ASSERT(vd != NULL || spa != NULL);
300ba27dd8bSMartin Matuska 	if (vd == NULL) {
301ba27dd8bSMartin Matuska 		vdev_guid = 0;
302ba27dd8bSMartin Matuska 		pool_guid = spa_guid(spa);
303ba27dd8bSMartin Matuska 	} else {
304ba27dd8bSMartin Matuska 		vdev_guid = vd->vdev_guid;
305ba27dd8bSMartin Matuska 		pool_guid = 0;
306ba27dd8bSMartin Matuska 	}
307ba27dd8bSMartin Matuska 
308ba27dd8bSMartin Matuska 	mutex_enter(&recent_events_lock);
309ba27dd8bSMartin Matuska 
310ba27dd8bSMartin Matuska 	recent_events_node_t *next = list_head(&recent_events_list);
311ba27dd8bSMartin Matuska 	while (next != NULL) {
312ba27dd8bSMartin Matuska 		recent_events_node_t *entry = next;
313ba27dd8bSMartin Matuska 
314ba27dd8bSMartin Matuska 		next = list_next(&recent_events_list, next);
315ba27dd8bSMartin Matuska 
316ba27dd8bSMartin Matuska 		if (entry->re_vdev_guid == vdev_guid ||
317ba27dd8bSMartin Matuska 		    entry->re_pool_guid == pool_guid) {
318ba27dd8bSMartin Matuska 			avl_remove(&recent_events_tree, entry);
319ba27dd8bSMartin Matuska 			list_remove(&recent_events_list, entry);
320ba27dd8bSMartin Matuska 			kmem_free(entry, sizeof (*entry));
321ba27dd8bSMartin Matuska 		}
322ba27dd8bSMartin Matuska 	}
323ba27dd8bSMartin Matuska 
324ba27dd8bSMartin Matuska 	mutex_exit(&recent_events_lock);
325ba27dd8bSMartin Matuska }
326ba27dd8bSMartin Matuska 
327ba27dd8bSMartin Matuska /*
3282c48331dSMatt Macy  * Check if an ereport would be a duplicate of one recently posted.
3292c48331dSMatt Macy  *
3302c48331dSMatt Macy  * An ereport is considered a duplicate if the set of criteria in
3312c48331dSMatt Macy  * recent_events_node_t all match.
3322c48331dSMatt Macy  *
3332c48331dSMatt Macy  * Only FM_EREPORT_ZFS_IO, FM_EREPORT_ZFS_DATA, and FM_EREPORT_ZFS_CHECKSUM
3342c48331dSMatt Macy  * are candidates for duplicate checking.
3352c48331dSMatt Macy  */
3362c48331dSMatt Macy static boolean_t
zfs_ereport_is_duplicate(const char * subclass,spa_t * spa,vdev_t * vd,const zbookmark_phys_t * zb,zio_t * zio,uint64_t offset,uint64_t size)3372c48331dSMatt Macy zfs_ereport_is_duplicate(const char *subclass, spa_t *spa, vdev_t *vd,
3382c48331dSMatt Macy     const zbookmark_phys_t *zb, zio_t *zio, uint64_t offset, uint64_t size)
3392c48331dSMatt Macy {
3402c48331dSMatt Macy 	recent_events_node_t search = {0}, *entry;
3412c48331dSMatt Macy 
3422c48331dSMatt Macy 	if (vd == NULL || zio == NULL)
3432c48331dSMatt Macy 		return (B_FALSE);
3442c48331dSMatt Macy 
3452c48331dSMatt Macy 	if (zfs_zevent_retain_max == 0)
3462c48331dSMatt Macy 		return (B_FALSE);
3472c48331dSMatt Macy 
3482c48331dSMatt Macy 	if (strcmp(subclass, FM_EREPORT_ZFS_IO) == 0)
3492c48331dSMatt Macy 		search.re_subclass = ZSC_IO;
3502c48331dSMatt Macy 	else if (strcmp(subclass, FM_EREPORT_ZFS_DATA) == 0)
3512c48331dSMatt Macy 		search.re_subclass = ZSC_DATA;
3522c48331dSMatt Macy 	else if (strcmp(subclass, FM_EREPORT_ZFS_CHECKSUM) == 0)
3532c48331dSMatt Macy 		search.re_subclass = ZSC_CHECKSUM;
3542c48331dSMatt Macy 	else
3552c48331dSMatt Macy 		return (B_FALSE);
3562c48331dSMatt Macy 
3572c48331dSMatt Macy 	search.re_pool_guid = spa_guid(spa);
3582c48331dSMatt Macy 	search.re_vdev_guid = vd->vdev_guid;
3592c48331dSMatt Macy 	search.re_io_error = zio->io_error;
3602c48331dSMatt Macy 	search.re_io_priority = zio->io_priority;
3612c48331dSMatt Macy 	/* if size is supplied use it over what's in zio */
3622c48331dSMatt Macy 	if (size) {
3632c48331dSMatt Macy 		search.re_io_size = size;
3642c48331dSMatt Macy 		search.re_io_offset = offset;
3652c48331dSMatt Macy 	} else {
3662c48331dSMatt Macy 		search.re_io_size = zio->io_size;
3672c48331dSMatt Macy 		search.re_io_offset = zio->io_offset;
3682c48331dSMatt Macy 	}
3692c48331dSMatt Macy 
3702c48331dSMatt Macy 	/* grab optional logical zio criteria */
3712c48331dSMatt Macy 	if (zb != NULL) {
3722c48331dSMatt Macy 		search.re_io_bookmark.zb_objset = zb->zb_objset;
3732c48331dSMatt Macy 		search.re_io_bookmark.zb_object = zb->zb_object;
3742c48331dSMatt Macy 		search.re_io_bookmark.zb_level = zb->zb_level;
3752c48331dSMatt Macy 		search.re_io_bookmark.zb_blkid = zb->zb_blkid;
3762c48331dSMatt Macy 	}
3772c48331dSMatt Macy 
3782c48331dSMatt Macy 	uint64_t now = gethrtime();
3792c48331dSMatt Macy 
3802c48331dSMatt Macy 	mutex_enter(&recent_events_lock);
3812c48331dSMatt Macy 
3822c48331dSMatt Macy 	/* check if we have seen this one recently */
3832c48331dSMatt Macy 	entry = avl_find(&recent_events_tree, &search, NULL);
3842c48331dSMatt Macy 	if (entry != NULL) {
3852c48331dSMatt Macy 		uint64_t age = NSEC2SEC(now - entry->re_timestamp);
3862c48331dSMatt Macy 
3872c48331dSMatt Macy 		/*
3882c48331dSMatt Macy 		 * There is still an active cleaner (since we're here).
3892c48331dSMatt Macy 		 * Reset the last seen time for this duplicate entry
3902c48331dSMatt Macy 		 * so that its lifespand gets extended.
3912c48331dSMatt Macy 		 */
3922c48331dSMatt Macy 		list_remove(&recent_events_list, entry);
3932c48331dSMatt Macy 		list_insert_head(&recent_events_list, entry);
3942c48331dSMatt Macy 		entry->re_timestamp = now;
3952c48331dSMatt Macy 
3962c48331dSMatt Macy 		zfs_zevent_track_duplicate();
3972c48331dSMatt Macy 		mutex_exit(&recent_events_lock);
3982c48331dSMatt Macy 
3992c48331dSMatt Macy 		return (age <= zfs_zevent_retain_expire_secs);
4002c48331dSMatt Macy 	}
4012c48331dSMatt Macy 
4022c48331dSMatt Macy 	if (avl_numnodes(&recent_events_tree) >= zfs_zevent_retain_max) {
4032c48331dSMatt Macy 		/* recycle oldest node */
4042c48331dSMatt Macy 		entry = list_tail(&recent_events_list);
4052c48331dSMatt Macy 		ASSERT(entry != NULL);
4062c48331dSMatt Macy 		list_remove(&recent_events_list, entry);
4072c48331dSMatt Macy 		avl_remove(&recent_events_tree, entry);
4082c48331dSMatt Macy 	} else {
4092c48331dSMatt Macy 		entry = kmem_alloc(sizeof (recent_events_node_t), KM_SLEEP);
4102c48331dSMatt Macy 	}
4112c48331dSMatt Macy 
4122c48331dSMatt Macy 	/* record this as a recent ereport */
4132c48331dSMatt Macy 	*entry = search;
4142c48331dSMatt Macy 	avl_add(&recent_events_tree, entry);
4152c48331dSMatt Macy 	list_insert_head(&recent_events_list, entry);
4162c48331dSMatt Macy 	entry->re_timestamp = now;
4172c48331dSMatt Macy 
4182c48331dSMatt Macy 	/* Start a cleaner if not already scheduled */
4192c48331dSMatt Macy 	if (recent_events_cleaner_tqid == 0)
4202c48331dSMatt Macy 		zfs_ereport_schedule_cleaner();
4212c48331dSMatt Macy 
4222c48331dSMatt Macy 	mutex_exit(&recent_events_lock);
4232c48331dSMatt Macy 	return (B_FALSE);
4242c48331dSMatt Macy }
4252c48331dSMatt Macy 
426eda14cbcSMatt Macy void
zfs_zevent_post_cb(nvlist_t * nvl,nvlist_t * detector)427eda14cbcSMatt Macy zfs_zevent_post_cb(nvlist_t *nvl, nvlist_t *detector)
428eda14cbcSMatt Macy {
429eda14cbcSMatt Macy 	if (nvl)
430eda14cbcSMatt Macy 		fm_nvlist_destroy(nvl, FM_NVA_FREE);
431eda14cbcSMatt Macy 
432eda14cbcSMatt Macy 	if (detector)
433eda14cbcSMatt Macy 		fm_nvlist_destroy(detector, FM_NVA_FREE);
434eda14cbcSMatt Macy }
435eda14cbcSMatt Macy 
436eda14cbcSMatt Macy /*
43716038816SMartin Matuska  * We want to rate limit ZIO delay, deadman, and checksum events so as to not
43816038816SMartin Matuska  * flood zevent consumers when a disk is acting up.
439eda14cbcSMatt Macy  *
440eda14cbcSMatt Macy  * Returns 1 if we're ratelimiting, 0 if not.
441eda14cbcSMatt Macy  */
442eda14cbcSMatt Macy static int
zfs_is_ratelimiting_event(const char * subclass,vdev_t * vd)443eda14cbcSMatt Macy zfs_is_ratelimiting_event(const char *subclass, vdev_t *vd)
444eda14cbcSMatt Macy {
445eda14cbcSMatt Macy 	int rc = 0;
446eda14cbcSMatt Macy 	/*
44716038816SMartin Matuska 	 * zfs_ratelimit() returns 1 if we're *not* ratelimiting and 0 if we
448eda14cbcSMatt Macy 	 * are.  Invert it to get our return value.
449eda14cbcSMatt Macy 	 */
450eda14cbcSMatt Macy 	if (strcmp(subclass, FM_EREPORT_ZFS_DELAY) == 0) {
451eda14cbcSMatt Macy 		rc = !zfs_ratelimit(&vd->vdev_delay_rl);
45216038816SMartin Matuska 	} else if (strcmp(subclass, FM_EREPORT_ZFS_DEADMAN) == 0) {
45316038816SMartin Matuska 		rc = !zfs_ratelimit(&vd->vdev_deadman_rl);
454eda14cbcSMatt Macy 	} else if (strcmp(subclass, FM_EREPORT_ZFS_CHECKSUM) == 0) {
455eda14cbcSMatt Macy 		rc = !zfs_ratelimit(&vd->vdev_checksum_rl);
456eda14cbcSMatt Macy 	}
457eda14cbcSMatt Macy 
458eda14cbcSMatt Macy 	if (rc)	{
459eda14cbcSMatt Macy 		/* We're rate limiting */
460eda14cbcSMatt Macy 		fm_erpt_dropped_increment();
461eda14cbcSMatt Macy 	}
462eda14cbcSMatt Macy 
463eda14cbcSMatt Macy 	return (rc);
464eda14cbcSMatt Macy }
465eda14cbcSMatt Macy 
466eda14cbcSMatt Macy /*
467eda14cbcSMatt Macy  * Return B_TRUE if the event actually posted, B_FALSE if not.
468eda14cbcSMatt Macy  */
469eda14cbcSMatt Macy static boolean_t
zfs_ereport_start(nvlist_t ** ereport_out,nvlist_t ** detector_out,const char * subclass,spa_t * spa,vdev_t * vd,const zbookmark_phys_t * zb,zio_t * zio,uint64_t stateoroffset,uint64_t size)470eda14cbcSMatt Macy zfs_ereport_start(nvlist_t **ereport_out, nvlist_t **detector_out,
471eda14cbcSMatt Macy     const char *subclass, spa_t *spa, vdev_t *vd, const zbookmark_phys_t *zb,
472eda14cbcSMatt Macy     zio_t *zio, uint64_t stateoroffset, uint64_t size)
473eda14cbcSMatt Macy {
474eda14cbcSMatt Macy 	nvlist_t *ereport, *detector;
475eda14cbcSMatt Macy 
476eda14cbcSMatt Macy 	uint64_t ena;
477eda14cbcSMatt Macy 	char class[64];
478eda14cbcSMatt Macy 
479eda14cbcSMatt Macy 	if ((ereport = fm_nvlist_create(NULL)) == NULL)
480eda14cbcSMatt Macy 		return (B_FALSE);
481eda14cbcSMatt Macy 
482eda14cbcSMatt Macy 	if ((detector = fm_nvlist_create(NULL)) == NULL) {
483eda14cbcSMatt Macy 		fm_nvlist_destroy(ereport, FM_NVA_FREE);
484eda14cbcSMatt Macy 		return (B_FALSE);
485eda14cbcSMatt Macy 	}
486eda14cbcSMatt Macy 
487eda14cbcSMatt Macy 	/*
488eda14cbcSMatt Macy 	 * Serialize ereport generation
489eda14cbcSMatt Macy 	 */
490eda14cbcSMatt Macy 	mutex_enter(&spa->spa_errlist_lock);
491eda14cbcSMatt Macy 
492eda14cbcSMatt Macy 	/*
493eda14cbcSMatt Macy 	 * Determine the ENA to use for this event.  If we are in a loading
494eda14cbcSMatt Macy 	 * state, use a SPA-wide ENA.  Otherwise, if we are in an I/O state, use
495eda14cbcSMatt Macy 	 * a root zio-wide ENA.  Otherwise, simply use a unique ENA.
496eda14cbcSMatt Macy 	 */
497eda14cbcSMatt Macy 	if (spa_load_state(spa) != SPA_LOAD_NONE) {
498eda14cbcSMatt Macy 		if (spa->spa_ena == 0)
499eda14cbcSMatt Macy 			spa->spa_ena = fm_ena_generate(0, FM_ENA_FMT1);
500eda14cbcSMatt Macy 		ena = spa->spa_ena;
501eda14cbcSMatt Macy 	} else if (zio != NULL && zio->io_logical != NULL) {
502eda14cbcSMatt Macy 		if (zio->io_logical->io_ena == 0)
503eda14cbcSMatt Macy 			zio->io_logical->io_ena =
504eda14cbcSMatt Macy 			    fm_ena_generate(0, FM_ENA_FMT1);
505eda14cbcSMatt Macy 		ena = zio->io_logical->io_ena;
506eda14cbcSMatt Macy 	} else {
507eda14cbcSMatt Macy 		ena = fm_ena_generate(0, FM_ENA_FMT1);
508eda14cbcSMatt Macy 	}
509eda14cbcSMatt Macy 
510eda14cbcSMatt Macy 	/*
511eda14cbcSMatt Macy 	 * Construct the full class, detector, and other standard FMA fields.
512eda14cbcSMatt Macy 	 */
513eda14cbcSMatt Macy 	(void) snprintf(class, sizeof (class), "%s.%s",
514eda14cbcSMatt Macy 	    ZFS_ERROR_CLASS, subclass);
515eda14cbcSMatt Macy 
516eda14cbcSMatt Macy 	fm_fmri_zfs_set(detector, FM_ZFS_SCHEME_VERSION, spa_guid(spa),
517eda14cbcSMatt Macy 	    vd != NULL ? vd->vdev_guid : 0);
518eda14cbcSMatt Macy 
519eda14cbcSMatt Macy 	fm_ereport_set(ereport, FM_EREPORT_VERSION, class, ena, detector, NULL);
520eda14cbcSMatt Macy 
521eda14cbcSMatt Macy 	/*
522eda14cbcSMatt Macy 	 * Construct the per-ereport payload, depending on which parameters are
523eda14cbcSMatt Macy 	 * passed in.
524eda14cbcSMatt Macy 	 */
525eda14cbcSMatt Macy 
526eda14cbcSMatt Macy 	/*
527eda14cbcSMatt Macy 	 * Generic payload members common to all ereports.
528eda14cbcSMatt Macy 	 */
529eda14cbcSMatt Macy 	fm_payload_set(ereport,
530eda14cbcSMatt Macy 	    FM_EREPORT_PAYLOAD_ZFS_POOL, DATA_TYPE_STRING, spa_name(spa),
531eda14cbcSMatt Macy 	    FM_EREPORT_PAYLOAD_ZFS_POOL_GUID, DATA_TYPE_UINT64, spa_guid(spa),
532eda14cbcSMatt Macy 	    FM_EREPORT_PAYLOAD_ZFS_POOL_STATE, DATA_TYPE_UINT64,
533eda14cbcSMatt Macy 	    (uint64_t)spa_state(spa),
534eda14cbcSMatt Macy 	    FM_EREPORT_PAYLOAD_ZFS_POOL_CONTEXT, DATA_TYPE_INT32,
535eda14cbcSMatt Macy 	    (int32_t)spa_load_state(spa), NULL);
536eda14cbcSMatt Macy 
537eda14cbcSMatt Macy 	fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_POOL_FAILMODE,
538eda14cbcSMatt Macy 	    DATA_TYPE_STRING,
539eda14cbcSMatt Macy 	    spa_get_failmode(spa) == ZIO_FAILURE_MODE_WAIT ?
540eda14cbcSMatt Macy 	    FM_EREPORT_FAILMODE_WAIT :
541eda14cbcSMatt Macy 	    spa_get_failmode(spa) == ZIO_FAILURE_MODE_CONTINUE ?
542eda14cbcSMatt Macy 	    FM_EREPORT_FAILMODE_CONTINUE : FM_EREPORT_FAILMODE_PANIC,
543eda14cbcSMatt Macy 	    NULL);
544eda14cbcSMatt Macy 
545eda14cbcSMatt Macy 	if (vd != NULL) {
546eda14cbcSMatt Macy 		vdev_t *pvd = vd->vdev_parent;
547eda14cbcSMatt Macy 		vdev_queue_t *vq = &vd->vdev_queue;
548eda14cbcSMatt Macy 		vdev_stat_t *vs = &vd->vdev_stat;
549eda14cbcSMatt Macy 		vdev_t *spare_vd;
550eda14cbcSMatt Macy 		uint64_t *spare_guids;
551eda14cbcSMatt Macy 		char **spare_paths;
552eda14cbcSMatt Macy 		int i, spare_count;
553eda14cbcSMatt Macy 
554eda14cbcSMatt Macy 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_VDEV_GUID,
555eda14cbcSMatt Macy 		    DATA_TYPE_UINT64, vd->vdev_guid,
556eda14cbcSMatt Macy 		    FM_EREPORT_PAYLOAD_ZFS_VDEV_TYPE,
557eda14cbcSMatt Macy 		    DATA_TYPE_STRING, vd->vdev_ops->vdev_op_type, NULL);
558eda14cbcSMatt Macy 		if (vd->vdev_path != NULL)
559eda14cbcSMatt Macy 			fm_payload_set(ereport,
560eda14cbcSMatt Macy 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_PATH,
561eda14cbcSMatt Macy 			    DATA_TYPE_STRING, vd->vdev_path, NULL);
562eda14cbcSMatt Macy 		if (vd->vdev_devid != NULL)
563eda14cbcSMatt Macy 			fm_payload_set(ereport,
564eda14cbcSMatt Macy 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_DEVID,
565eda14cbcSMatt Macy 			    DATA_TYPE_STRING, vd->vdev_devid, NULL);
566eda14cbcSMatt Macy 		if (vd->vdev_fru != NULL)
567eda14cbcSMatt Macy 			fm_payload_set(ereport,
568eda14cbcSMatt Macy 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_FRU,
569eda14cbcSMatt Macy 			    DATA_TYPE_STRING, vd->vdev_fru, NULL);
570eda14cbcSMatt Macy 		if (vd->vdev_enc_sysfs_path != NULL)
571eda14cbcSMatt Macy 			fm_payload_set(ereport,
572eda14cbcSMatt Macy 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_ENC_SYSFS_PATH,
573eda14cbcSMatt Macy 			    DATA_TYPE_STRING, vd->vdev_enc_sysfs_path, NULL);
574eda14cbcSMatt Macy 		if (vd->vdev_ashift)
575eda14cbcSMatt Macy 			fm_payload_set(ereport,
576eda14cbcSMatt Macy 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_ASHIFT,
577eda14cbcSMatt Macy 			    DATA_TYPE_UINT64, vd->vdev_ashift, NULL);
578eda14cbcSMatt Macy 
579eda14cbcSMatt Macy 		if (vq != NULL) {
580eda14cbcSMatt Macy 			fm_payload_set(ereport,
581eda14cbcSMatt Macy 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_COMP_TS,
582eda14cbcSMatt Macy 			    DATA_TYPE_UINT64, vq->vq_io_complete_ts, NULL);
583eda14cbcSMatt Macy 			fm_payload_set(ereport,
584eda14cbcSMatt Macy 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_DELTA_TS,
585eda14cbcSMatt Macy 			    DATA_TYPE_UINT64, vq->vq_io_delta_ts, NULL);
586eda14cbcSMatt Macy 		}
587eda14cbcSMatt Macy 
588eda14cbcSMatt Macy 		if (vs != NULL) {
589eda14cbcSMatt Macy 			fm_payload_set(ereport,
590eda14cbcSMatt Macy 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_READ_ERRORS,
591eda14cbcSMatt Macy 			    DATA_TYPE_UINT64, vs->vs_read_errors,
592eda14cbcSMatt Macy 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_WRITE_ERRORS,
593eda14cbcSMatt Macy 			    DATA_TYPE_UINT64, vs->vs_write_errors,
594eda14cbcSMatt Macy 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_CKSUM_ERRORS,
595eda14cbcSMatt Macy 			    DATA_TYPE_UINT64, vs->vs_checksum_errors,
596eda14cbcSMatt Macy 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_DELAYS,
597eda14cbcSMatt Macy 			    DATA_TYPE_UINT64, vs->vs_slow_ios,
598*7a7741afSMartin Matuska 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_DIO_VERIFY_ERRORS,
599*7a7741afSMartin Matuska 			    DATA_TYPE_UINT64, vs->vs_dio_verify_errors,
600eda14cbcSMatt Macy 			    NULL);
601eda14cbcSMatt Macy 		}
602eda14cbcSMatt Macy 
603eda14cbcSMatt Macy 		if (pvd != NULL) {
604eda14cbcSMatt Macy 			fm_payload_set(ereport,
605eda14cbcSMatt Macy 			    FM_EREPORT_PAYLOAD_ZFS_PARENT_GUID,
606eda14cbcSMatt Macy 			    DATA_TYPE_UINT64, pvd->vdev_guid,
607eda14cbcSMatt Macy 			    FM_EREPORT_PAYLOAD_ZFS_PARENT_TYPE,
608eda14cbcSMatt Macy 			    DATA_TYPE_STRING, pvd->vdev_ops->vdev_op_type,
609eda14cbcSMatt Macy 			    NULL);
610eda14cbcSMatt Macy 			if (pvd->vdev_path)
611eda14cbcSMatt Macy 				fm_payload_set(ereport,
612eda14cbcSMatt Macy 				    FM_EREPORT_PAYLOAD_ZFS_PARENT_PATH,
613eda14cbcSMatt Macy 				    DATA_TYPE_STRING, pvd->vdev_path, NULL);
614eda14cbcSMatt Macy 			if (pvd->vdev_devid)
615eda14cbcSMatt Macy 				fm_payload_set(ereport,
616eda14cbcSMatt Macy 				    FM_EREPORT_PAYLOAD_ZFS_PARENT_DEVID,
617eda14cbcSMatt Macy 				    DATA_TYPE_STRING, pvd->vdev_devid, NULL);
618eda14cbcSMatt Macy 		}
619eda14cbcSMatt Macy 
620eda14cbcSMatt Macy 		spare_count = spa->spa_spares.sav_count;
621eda14cbcSMatt Macy 		spare_paths = kmem_zalloc(sizeof (char *) * spare_count,
622eda14cbcSMatt Macy 		    KM_SLEEP);
623eda14cbcSMatt Macy 		spare_guids = kmem_zalloc(sizeof (uint64_t) * spare_count,
624eda14cbcSMatt Macy 		    KM_SLEEP);
625eda14cbcSMatt Macy 
626eda14cbcSMatt Macy 		for (i = 0; i < spare_count; i++) {
627eda14cbcSMatt Macy 			spare_vd = spa->spa_spares.sav_vdevs[i];
628eda14cbcSMatt Macy 			if (spare_vd) {
629eda14cbcSMatt Macy 				spare_paths[i] = spare_vd->vdev_path;
630eda14cbcSMatt Macy 				spare_guids[i] = spare_vd->vdev_guid;
631eda14cbcSMatt Macy 			}
632eda14cbcSMatt Macy 		}
633eda14cbcSMatt Macy 
634eda14cbcSMatt Macy 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_VDEV_SPARE_PATHS,
635eda14cbcSMatt Macy 		    DATA_TYPE_STRING_ARRAY, spare_count, spare_paths,
636eda14cbcSMatt Macy 		    FM_EREPORT_PAYLOAD_ZFS_VDEV_SPARE_GUIDS,
637eda14cbcSMatt Macy 		    DATA_TYPE_UINT64_ARRAY, spare_count, spare_guids, NULL);
638eda14cbcSMatt Macy 
639eda14cbcSMatt Macy 		kmem_free(spare_guids, sizeof (uint64_t) * spare_count);
640eda14cbcSMatt Macy 		kmem_free(spare_paths, sizeof (char *) * spare_count);
641eda14cbcSMatt Macy 	}
642eda14cbcSMatt Macy 
643eda14cbcSMatt Macy 	if (zio != NULL) {
644eda14cbcSMatt Macy 		/*
645eda14cbcSMatt Macy 		 * Payload common to all I/Os.
646eda14cbcSMatt Macy 		 */
647eda14cbcSMatt Macy 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_ERR,
648eda14cbcSMatt Macy 		    DATA_TYPE_INT32, zio->io_error, NULL);
649eda14cbcSMatt Macy 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_FLAGS,
650e2df9bb4SMartin Matuska 		    DATA_TYPE_UINT64, zio->io_flags, NULL);
651eda14cbcSMatt Macy 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_STAGE,
652eda14cbcSMatt Macy 		    DATA_TYPE_UINT32, zio->io_stage, NULL);
653eda14cbcSMatt Macy 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_PIPELINE,
654eda14cbcSMatt Macy 		    DATA_TYPE_UINT32, zio->io_pipeline, NULL);
655eda14cbcSMatt Macy 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_DELAY,
656eda14cbcSMatt Macy 		    DATA_TYPE_UINT64, zio->io_delay, NULL);
657eda14cbcSMatt Macy 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_TIMESTAMP,
658eda14cbcSMatt Macy 		    DATA_TYPE_UINT64, zio->io_timestamp, NULL);
659eda14cbcSMatt Macy 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_DELTA,
660eda14cbcSMatt Macy 		    DATA_TYPE_UINT64, zio->io_delta, NULL);
6612c48331dSMatt Macy 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_PRIORITY,
6622c48331dSMatt Macy 		    DATA_TYPE_UINT32, zio->io_priority, NULL);
663eda14cbcSMatt Macy 
664eda14cbcSMatt Macy 		/*
665eda14cbcSMatt Macy 		 * If the 'size' parameter is non-zero, it indicates this is a
666eda14cbcSMatt Macy 		 * RAID-Z or other I/O where the physical offset and length are
667eda14cbcSMatt Macy 		 * provided for us, instead of within the zio_t.
668eda14cbcSMatt Macy 		 */
669eda14cbcSMatt Macy 		if (vd != NULL) {
670eda14cbcSMatt Macy 			if (size)
671eda14cbcSMatt Macy 				fm_payload_set(ereport,
672eda14cbcSMatt Macy 				    FM_EREPORT_PAYLOAD_ZFS_ZIO_OFFSET,
673eda14cbcSMatt Macy 				    DATA_TYPE_UINT64, stateoroffset,
674eda14cbcSMatt Macy 				    FM_EREPORT_PAYLOAD_ZFS_ZIO_SIZE,
675eda14cbcSMatt Macy 				    DATA_TYPE_UINT64, size, NULL);
676eda14cbcSMatt Macy 			else
677eda14cbcSMatt Macy 				fm_payload_set(ereport,
678eda14cbcSMatt Macy 				    FM_EREPORT_PAYLOAD_ZFS_ZIO_OFFSET,
679eda14cbcSMatt Macy 				    DATA_TYPE_UINT64, zio->io_offset,
680eda14cbcSMatt Macy 				    FM_EREPORT_PAYLOAD_ZFS_ZIO_SIZE,
681eda14cbcSMatt Macy 				    DATA_TYPE_UINT64, zio->io_size, NULL);
682eda14cbcSMatt Macy 		}
683eda14cbcSMatt Macy 	} else if (vd != NULL) {
684eda14cbcSMatt Macy 		/*
685eda14cbcSMatt Macy 		 * If we have a vdev but no zio, this is a device fault, and the
686eda14cbcSMatt Macy 		 * 'stateoroffset' parameter indicates the previous state of the
687eda14cbcSMatt Macy 		 * vdev.
688eda14cbcSMatt Macy 		 */
689eda14cbcSMatt Macy 		fm_payload_set(ereport,
690eda14cbcSMatt Macy 		    FM_EREPORT_PAYLOAD_ZFS_PREV_STATE,
691eda14cbcSMatt Macy 		    DATA_TYPE_UINT64, stateoroffset, NULL);
692eda14cbcSMatt Macy 	}
693eda14cbcSMatt Macy 
694eda14cbcSMatt Macy 	/*
695eda14cbcSMatt Macy 	 * Payload for I/Os with corresponding logical information.
696eda14cbcSMatt Macy 	 */
697eda14cbcSMatt Macy 	if (zb != NULL && (zio == NULL || zio->io_logical != NULL)) {
698eda14cbcSMatt Macy 		fm_payload_set(ereport,
699eda14cbcSMatt Macy 		    FM_EREPORT_PAYLOAD_ZFS_ZIO_OBJSET,
700eda14cbcSMatt Macy 		    DATA_TYPE_UINT64, zb->zb_objset,
701eda14cbcSMatt Macy 		    FM_EREPORT_PAYLOAD_ZFS_ZIO_OBJECT,
702eda14cbcSMatt Macy 		    DATA_TYPE_UINT64, zb->zb_object,
703eda14cbcSMatt Macy 		    FM_EREPORT_PAYLOAD_ZFS_ZIO_LEVEL,
704eda14cbcSMatt Macy 		    DATA_TYPE_INT64, zb->zb_level,
705eda14cbcSMatt Macy 		    FM_EREPORT_PAYLOAD_ZFS_ZIO_BLKID,
706eda14cbcSMatt Macy 		    DATA_TYPE_UINT64, zb->zb_blkid, NULL);
707eda14cbcSMatt Macy 	}
708eda14cbcSMatt Macy 
70915f0b8c3SMartin Matuska 	/*
71015f0b8c3SMartin Matuska 	 * Payload for tuning the zed
71115f0b8c3SMartin Matuska 	 */
71215f0b8c3SMartin Matuska 	if (vd != NULL && strcmp(subclass, FM_EREPORT_ZFS_CHECKSUM) == 0) {
71315f0b8c3SMartin Matuska 		uint64_t cksum_n, cksum_t;
71415f0b8c3SMartin Matuska 
71515f0b8c3SMartin Matuska 		cksum_n = vdev_prop_get_inherited(vd, VDEV_PROP_CHECKSUM_N);
71615f0b8c3SMartin Matuska 		if (cksum_n != vdev_prop_default_numeric(VDEV_PROP_CHECKSUM_N))
71715f0b8c3SMartin Matuska 			fm_payload_set(ereport,
71815f0b8c3SMartin Matuska 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_CKSUM_N,
71915f0b8c3SMartin Matuska 			    DATA_TYPE_UINT64,
72015f0b8c3SMartin Matuska 			    cksum_n,
72115f0b8c3SMartin Matuska 			    NULL);
72215f0b8c3SMartin Matuska 
72315f0b8c3SMartin Matuska 		cksum_t = vdev_prop_get_inherited(vd, VDEV_PROP_CHECKSUM_T);
72415f0b8c3SMartin Matuska 		if (cksum_t != vdev_prop_default_numeric(VDEV_PROP_CHECKSUM_T))
72515f0b8c3SMartin Matuska 			fm_payload_set(ereport,
72615f0b8c3SMartin Matuska 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_CKSUM_T,
72715f0b8c3SMartin Matuska 			    DATA_TYPE_UINT64,
72815f0b8c3SMartin Matuska 			    cksum_t,
72915f0b8c3SMartin Matuska 			    NULL);
73015f0b8c3SMartin Matuska 	}
73115f0b8c3SMartin Matuska 
73215f0b8c3SMartin Matuska 	if (vd != NULL && strcmp(subclass, FM_EREPORT_ZFS_IO) == 0) {
73315f0b8c3SMartin Matuska 		uint64_t io_n, io_t;
73415f0b8c3SMartin Matuska 
73515f0b8c3SMartin Matuska 		io_n = vdev_prop_get_inherited(vd, VDEV_PROP_IO_N);
73615f0b8c3SMartin Matuska 		if (io_n != vdev_prop_default_numeric(VDEV_PROP_IO_N))
73715f0b8c3SMartin Matuska 			fm_payload_set(ereport,
73815f0b8c3SMartin Matuska 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_IO_N,
73915f0b8c3SMartin Matuska 			    DATA_TYPE_UINT64,
74015f0b8c3SMartin Matuska 			    io_n,
74115f0b8c3SMartin Matuska 			    NULL);
74215f0b8c3SMartin Matuska 
74315f0b8c3SMartin Matuska 		io_t = vdev_prop_get_inherited(vd, VDEV_PROP_IO_T);
74415f0b8c3SMartin Matuska 		if (io_t != vdev_prop_default_numeric(VDEV_PROP_IO_T))
74515f0b8c3SMartin Matuska 			fm_payload_set(ereport,
74615f0b8c3SMartin Matuska 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_IO_T,
74715f0b8c3SMartin Matuska 			    DATA_TYPE_UINT64,
74815f0b8c3SMartin Matuska 			    io_t,
74915f0b8c3SMartin Matuska 			    NULL);
75015f0b8c3SMartin Matuska 	}
75115f0b8c3SMartin Matuska 
752e2257b31SMartin Matuska 	if (vd != NULL && strcmp(subclass, FM_EREPORT_ZFS_DELAY) == 0) {
753e2257b31SMartin Matuska 		uint64_t slow_io_n, slow_io_t;
754e2257b31SMartin Matuska 
755e2257b31SMartin Matuska 		slow_io_n = vdev_prop_get_inherited(vd, VDEV_PROP_SLOW_IO_N);
756e2257b31SMartin Matuska 		if (slow_io_n != vdev_prop_default_numeric(VDEV_PROP_SLOW_IO_N))
757e2257b31SMartin Matuska 			fm_payload_set(ereport,
758e2257b31SMartin Matuska 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_SLOW_IO_N,
759e2257b31SMartin Matuska 			    DATA_TYPE_UINT64,
760e2257b31SMartin Matuska 			    slow_io_n,
761e2257b31SMartin Matuska 			    NULL);
762e2257b31SMartin Matuska 
763e2257b31SMartin Matuska 		slow_io_t = vdev_prop_get_inherited(vd, VDEV_PROP_SLOW_IO_T);
764e2257b31SMartin Matuska 		if (slow_io_t != vdev_prop_default_numeric(VDEV_PROP_SLOW_IO_T))
765e2257b31SMartin Matuska 			fm_payload_set(ereport,
766e2257b31SMartin Matuska 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_SLOW_IO_T,
767e2257b31SMartin Matuska 			    DATA_TYPE_UINT64,
768e2257b31SMartin Matuska 			    slow_io_t,
769e2257b31SMartin Matuska 			    NULL);
770e2257b31SMartin Matuska 	}
771e2257b31SMartin Matuska 
772eda14cbcSMatt Macy 	mutex_exit(&spa->spa_errlist_lock);
773eda14cbcSMatt Macy 
774eda14cbcSMatt Macy 	*ereport_out = ereport;
775eda14cbcSMatt Macy 	*detector_out = detector;
776eda14cbcSMatt Macy 	return (B_TRUE);
777eda14cbcSMatt Macy }
778eda14cbcSMatt Macy 
779eda14cbcSMatt Macy /* if it's <= 128 bytes, save the corruption directly */
780eda14cbcSMatt Macy #define	ZFM_MAX_INLINE		(128 / sizeof (uint64_t))
781eda14cbcSMatt Macy 
782eda14cbcSMatt Macy #define	MAX_RANGES		16
783eda14cbcSMatt Macy 
784eda14cbcSMatt Macy typedef struct zfs_ecksum_info {
785eda14cbcSMatt Macy 	/* inline arrays of bits set and cleared. */
786eda14cbcSMatt Macy 	uint64_t zei_bits_set[ZFM_MAX_INLINE];
787eda14cbcSMatt Macy 	uint64_t zei_bits_cleared[ZFM_MAX_INLINE];
788eda14cbcSMatt Macy 
789eda14cbcSMatt Macy 	/*
790eda14cbcSMatt Macy 	 * for each range, the number of bits set and cleared.  The Hamming
791eda14cbcSMatt Macy 	 * distance between the good and bad buffers is the sum of them all.
792eda14cbcSMatt Macy 	 */
793eda14cbcSMatt Macy 	uint32_t zei_range_sets[MAX_RANGES];
794eda14cbcSMatt Macy 	uint32_t zei_range_clears[MAX_RANGES];
795eda14cbcSMatt Macy 
796eda14cbcSMatt Macy 	struct zei_ranges {
797eda14cbcSMatt Macy 		uint32_t	zr_start;
798eda14cbcSMatt Macy 		uint32_t	zr_end;
799eda14cbcSMatt Macy 	} zei_ranges[MAX_RANGES];
800eda14cbcSMatt Macy 
801eda14cbcSMatt Macy 	size_t	zei_range_count;
802eda14cbcSMatt Macy 	uint32_t zei_mingap;
803eda14cbcSMatt Macy 	uint32_t zei_allowed_mingap;
804eda14cbcSMatt Macy 
805eda14cbcSMatt Macy } zfs_ecksum_info_t;
806eda14cbcSMatt Macy 
807eda14cbcSMatt Macy static void
update_bad_bits(uint64_t value_arg,uint32_t * count)808315ee00fSMartin Matuska update_bad_bits(uint64_t value_arg, uint32_t *count)
809eda14cbcSMatt Macy {
810eda14cbcSMatt Macy 	size_t i;
811eda14cbcSMatt Macy 	size_t bits = 0;
812eda14cbcSMatt Macy 	uint64_t value = BE_64(value_arg);
813eda14cbcSMatt Macy 
814eda14cbcSMatt Macy 	/* We store the bits in big-endian (largest-first) order */
815eda14cbcSMatt Macy 	for (i = 0; i < 64; i++) {
816315ee00fSMartin Matuska 		if (value & (1ull << i))
817eda14cbcSMatt Macy 			++bits;
818eda14cbcSMatt Macy 	}
819eda14cbcSMatt Macy 	/* update the count of bits changed */
820eda14cbcSMatt Macy 	*count += bits;
821eda14cbcSMatt Macy }
822eda14cbcSMatt Macy 
823eda14cbcSMatt Macy /*
824eda14cbcSMatt Macy  * We've now filled up the range array, and need to increase "mingap" and
825eda14cbcSMatt Macy  * shrink the range list accordingly.  zei_mingap is always the smallest
826eda14cbcSMatt Macy  * distance between array entries, so we set the new_allowed_gap to be
827eda14cbcSMatt Macy  * one greater than that.  We then go through the list, joining together
828eda14cbcSMatt Macy  * any ranges which are closer than the new_allowed_gap.
829eda14cbcSMatt Macy  *
830eda14cbcSMatt Macy  * By construction, there will be at least one.  We also update zei_mingap
831eda14cbcSMatt Macy  * to the new smallest gap, to prepare for our next invocation.
832eda14cbcSMatt Macy  */
833eda14cbcSMatt Macy static void
zei_shrink_ranges(zfs_ecksum_info_t * eip)834eda14cbcSMatt Macy zei_shrink_ranges(zfs_ecksum_info_t *eip)
835eda14cbcSMatt Macy {
836eda14cbcSMatt Macy 	uint32_t mingap = UINT32_MAX;
837eda14cbcSMatt Macy 	uint32_t new_allowed_gap = eip->zei_mingap + 1;
838eda14cbcSMatt Macy 
839eda14cbcSMatt Macy 	size_t idx, output;
840eda14cbcSMatt Macy 	size_t max = eip->zei_range_count;
841eda14cbcSMatt Macy 
842eda14cbcSMatt Macy 	struct zei_ranges *r = eip->zei_ranges;
843eda14cbcSMatt Macy 
844eda14cbcSMatt Macy 	ASSERT3U(eip->zei_range_count, >, 0);
845eda14cbcSMatt Macy 	ASSERT3U(eip->zei_range_count, <=, MAX_RANGES);
846eda14cbcSMatt Macy 
847eda14cbcSMatt Macy 	output = idx = 0;
848eda14cbcSMatt Macy 	while (idx < max - 1) {
849eda14cbcSMatt Macy 		uint32_t start = r[idx].zr_start;
850eda14cbcSMatt Macy 		uint32_t end = r[idx].zr_end;
851eda14cbcSMatt Macy 
852eda14cbcSMatt Macy 		while (idx < max - 1) {
853eda14cbcSMatt Macy 			idx++;
854eda14cbcSMatt Macy 
855eda14cbcSMatt Macy 			uint32_t nstart = r[idx].zr_start;
856eda14cbcSMatt Macy 			uint32_t nend = r[idx].zr_end;
857eda14cbcSMatt Macy 
858eda14cbcSMatt Macy 			uint32_t gap = nstart - end;
859eda14cbcSMatt Macy 			if (gap < new_allowed_gap) {
860eda14cbcSMatt Macy 				end = nend;
861eda14cbcSMatt Macy 				continue;
862eda14cbcSMatt Macy 			}
863eda14cbcSMatt Macy 			if (gap < mingap)
864eda14cbcSMatt Macy 				mingap = gap;
865eda14cbcSMatt Macy 			break;
866eda14cbcSMatt Macy 		}
867eda14cbcSMatt Macy 		r[output].zr_start = start;
868eda14cbcSMatt Macy 		r[output].zr_end = end;
869eda14cbcSMatt Macy 		output++;
870eda14cbcSMatt Macy 	}
871eda14cbcSMatt Macy 	ASSERT3U(output, <, eip->zei_range_count);
872eda14cbcSMatt Macy 	eip->zei_range_count = output;
873eda14cbcSMatt Macy 	eip->zei_mingap = mingap;
874eda14cbcSMatt Macy 	eip->zei_allowed_mingap = new_allowed_gap;
875eda14cbcSMatt Macy }
876eda14cbcSMatt Macy 
877eda14cbcSMatt Macy static void
zei_add_range(zfs_ecksum_info_t * eip,int start,int end)878eda14cbcSMatt Macy zei_add_range(zfs_ecksum_info_t *eip, int start, int end)
879eda14cbcSMatt Macy {
880eda14cbcSMatt Macy 	struct zei_ranges *r = eip->zei_ranges;
881eda14cbcSMatt Macy 	size_t count = eip->zei_range_count;
882eda14cbcSMatt Macy 
883eda14cbcSMatt Macy 	if (count >= MAX_RANGES) {
884eda14cbcSMatt Macy 		zei_shrink_ranges(eip);
885eda14cbcSMatt Macy 		count = eip->zei_range_count;
886eda14cbcSMatt Macy 	}
887eda14cbcSMatt Macy 	if (count == 0) {
888eda14cbcSMatt Macy 		eip->zei_mingap = UINT32_MAX;
889eda14cbcSMatt Macy 		eip->zei_allowed_mingap = 1;
890eda14cbcSMatt Macy 	} else {
891eda14cbcSMatt Macy 		int gap = start - r[count - 1].zr_end;
892eda14cbcSMatt Macy 
893eda14cbcSMatt Macy 		if (gap < eip->zei_allowed_mingap) {
894eda14cbcSMatt Macy 			r[count - 1].zr_end = end;
895eda14cbcSMatt Macy 			return;
896eda14cbcSMatt Macy 		}
897eda14cbcSMatt Macy 		if (gap < eip->zei_mingap)
898eda14cbcSMatt Macy 			eip->zei_mingap = gap;
899eda14cbcSMatt Macy 	}
900eda14cbcSMatt Macy 	r[count].zr_start = start;
901eda14cbcSMatt Macy 	r[count].zr_end = end;
902eda14cbcSMatt Macy 	eip->zei_range_count++;
903eda14cbcSMatt Macy }
904eda14cbcSMatt Macy 
905eda14cbcSMatt Macy static size_t
zei_range_total_size(zfs_ecksum_info_t * eip)906eda14cbcSMatt Macy zei_range_total_size(zfs_ecksum_info_t *eip)
907eda14cbcSMatt Macy {
908eda14cbcSMatt Macy 	struct zei_ranges *r = eip->zei_ranges;
909eda14cbcSMatt Macy 	size_t count = eip->zei_range_count;
910eda14cbcSMatt Macy 	size_t result = 0;
911eda14cbcSMatt Macy 	size_t idx;
912eda14cbcSMatt Macy 
913eda14cbcSMatt Macy 	for (idx = 0; idx < count; idx++)
914eda14cbcSMatt Macy 		result += (r[idx].zr_end - r[idx].zr_start);
915eda14cbcSMatt Macy 
916eda14cbcSMatt Macy 	return (result);
917eda14cbcSMatt Macy }
918eda14cbcSMatt Macy 
919eda14cbcSMatt Macy static zfs_ecksum_info_t *
annotate_ecksum(nvlist_t * ereport,zio_bad_cksum_t * info,const abd_t * goodabd,const abd_t * badabd,size_t size,boolean_t drop_if_identical)920eda14cbcSMatt Macy annotate_ecksum(nvlist_t *ereport, zio_bad_cksum_t *info,
921eda14cbcSMatt Macy     const abd_t *goodabd, const abd_t *badabd, size_t size,
922eda14cbcSMatt Macy     boolean_t drop_if_identical)
923eda14cbcSMatt Macy {
924eda14cbcSMatt Macy 	const uint64_t *good;
925eda14cbcSMatt Macy 	const uint64_t *bad;
926eda14cbcSMatt Macy 
927eda14cbcSMatt Macy 	size_t nui64s = size / sizeof (uint64_t);
928eda14cbcSMatt Macy 
929eda14cbcSMatt Macy 	size_t inline_size;
930eda14cbcSMatt Macy 	int no_inline = 0;
931eda14cbcSMatt Macy 	size_t idx;
932eda14cbcSMatt Macy 	size_t range;
933eda14cbcSMatt Macy 
934eda14cbcSMatt Macy 	size_t offset = 0;
935eda14cbcSMatt Macy 	ssize_t start = -1;
936eda14cbcSMatt Macy 
937eda14cbcSMatt Macy 	zfs_ecksum_info_t *eip = kmem_zalloc(sizeof (*eip), KM_SLEEP);
938eda14cbcSMatt Macy 
939eda14cbcSMatt Macy 	/* don't do any annotation for injected checksum errors */
940eda14cbcSMatt Macy 	if (info != NULL && info->zbc_injected)
941eda14cbcSMatt Macy 		return (eip);
942eda14cbcSMatt Macy 
943eda14cbcSMatt Macy 	if (info != NULL && info->zbc_has_cksum) {
944eda14cbcSMatt Macy 		fm_payload_set(ereport,
945eda14cbcSMatt Macy 		    FM_EREPORT_PAYLOAD_ZFS_CKSUM_ALGO,
946eda14cbcSMatt Macy 		    DATA_TYPE_STRING,
947eda14cbcSMatt Macy 		    info->zbc_checksum_name,
948eda14cbcSMatt Macy 		    NULL);
949eda14cbcSMatt Macy 
950eda14cbcSMatt Macy 		if (info->zbc_byteswapped) {
951eda14cbcSMatt Macy 			fm_payload_set(ereport,
952eda14cbcSMatt Macy 			    FM_EREPORT_PAYLOAD_ZFS_CKSUM_BYTESWAP,
953eda14cbcSMatt Macy 			    DATA_TYPE_BOOLEAN, 1,
954eda14cbcSMatt Macy 			    NULL);
955eda14cbcSMatt Macy 		}
956eda14cbcSMatt Macy 	}
957eda14cbcSMatt Macy 
958eda14cbcSMatt Macy 	if (badabd == NULL || goodabd == NULL)
959eda14cbcSMatt Macy 		return (eip);
960eda14cbcSMatt Macy 
961eda14cbcSMatt Macy 	ASSERT3U(nui64s, <=, UINT32_MAX);
962eda14cbcSMatt Macy 	ASSERT3U(size, ==, nui64s * sizeof (uint64_t));
963eda14cbcSMatt Macy 	ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
964eda14cbcSMatt Macy 	ASSERT3U(size, <=, UINT32_MAX);
965eda14cbcSMatt Macy 
966eda14cbcSMatt Macy 	good = (const uint64_t *) abd_borrow_buf_copy((abd_t *)goodabd, size);
967eda14cbcSMatt Macy 	bad = (const uint64_t *) abd_borrow_buf_copy((abd_t *)badabd, size);
968eda14cbcSMatt Macy 
969eda14cbcSMatt Macy 	/* build up the range list by comparing the two buffers. */
970eda14cbcSMatt Macy 	for (idx = 0; idx < nui64s; idx++) {
971eda14cbcSMatt Macy 		if (good[idx] == bad[idx]) {
972eda14cbcSMatt Macy 			if (start == -1)
973eda14cbcSMatt Macy 				continue;
974eda14cbcSMatt Macy 
975eda14cbcSMatt Macy 			zei_add_range(eip, start, idx);
976eda14cbcSMatt Macy 			start = -1;
977eda14cbcSMatt Macy 		} else {
978eda14cbcSMatt Macy 			if (start != -1)
979eda14cbcSMatt Macy 				continue;
980eda14cbcSMatt Macy 
981eda14cbcSMatt Macy 			start = idx;
982eda14cbcSMatt Macy 		}
983eda14cbcSMatt Macy 	}
984eda14cbcSMatt Macy 	if (start != -1)
985eda14cbcSMatt Macy 		zei_add_range(eip, start, idx);
986eda14cbcSMatt Macy 
987eda14cbcSMatt Macy 	/* See if it will fit in our inline buffers */
988eda14cbcSMatt Macy 	inline_size = zei_range_total_size(eip);
989eda14cbcSMatt Macy 	if (inline_size > ZFM_MAX_INLINE)
990eda14cbcSMatt Macy 		no_inline = 1;
991eda14cbcSMatt Macy 
992eda14cbcSMatt Macy 	/*
993eda14cbcSMatt Macy 	 * If there is no change and we want to drop if the buffers are
994eda14cbcSMatt Macy 	 * identical, do so.
995eda14cbcSMatt Macy 	 */
996eda14cbcSMatt Macy 	if (inline_size == 0 && drop_if_identical) {
997eda14cbcSMatt Macy 		kmem_free(eip, sizeof (*eip));
998eda14cbcSMatt Macy 		abd_return_buf((abd_t *)goodabd, (void *)good, size);
999eda14cbcSMatt Macy 		abd_return_buf((abd_t *)badabd, (void *)bad, size);
1000eda14cbcSMatt Macy 		return (NULL);
1001eda14cbcSMatt Macy 	}
1002eda14cbcSMatt Macy 
1003eda14cbcSMatt Macy 	/*
1004eda14cbcSMatt Macy 	 * Now walk through the ranges, filling in the details of the
1005eda14cbcSMatt Macy 	 * differences.  Also convert our uint64_t-array offsets to byte
1006eda14cbcSMatt Macy 	 * offsets.
1007eda14cbcSMatt Macy 	 */
1008eda14cbcSMatt Macy 	for (range = 0; range < eip->zei_range_count; range++) {
1009eda14cbcSMatt Macy 		size_t start = eip->zei_ranges[range].zr_start;
1010eda14cbcSMatt Macy 		size_t end = eip->zei_ranges[range].zr_end;
1011eda14cbcSMatt Macy 
1012eda14cbcSMatt Macy 		for (idx = start; idx < end; idx++) {
1013eda14cbcSMatt Macy 			uint64_t set, cleared;
1014eda14cbcSMatt Macy 
1015eda14cbcSMatt Macy 			// bits set in bad, but not in good
1016eda14cbcSMatt Macy 			set = ((~good[idx]) & bad[idx]);
1017eda14cbcSMatt Macy 			// bits set in good, but not in bad
1018eda14cbcSMatt Macy 			cleared = (good[idx] & (~bad[idx]));
1019eda14cbcSMatt Macy 
1020eda14cbcSMatt Macy 			if (!no_inline) {
1021eda14cbcSMatt Macy 				ASSERT3U(offset, <, inline_size);
1022eda14cbcSMatt Macy 				eip->zei_bits_set[offset] = set;
1023eda14cbcSMatt Macy 				eip->zei_bits_cleared[offset] = cleared;
1024eda14cbcSMatt Macy 				offset++;
1025eda14cbcSMatt Macy 			}
1026eda14cbcSMatt Macy 
1027315ee00fSMartin Matuska 			update_bad_bits(set, &eip->zei_range_sets[range]);
1028315ee00fSMartin Matuska 			update_bad_bits(cleared, &eip->zei_range_clears[range]);
1029eda14cbcSMatt Macy 		}
1030eda14cbcSMatt Macy 
1031eda14cbcSMatt Macy 		/* convert to byte offsets */
1032eda14cbcSMatt Macy 		eip->zei_ranges[range].zr_start	*= sizeof (uint64_t);
1033eda14cbcSMatt Macy 		eip->zei_ranges[range].zr_end	*= sizeof (uint64_t);
1034eda14cbcSMatt Macy 	}
1035eda14cbcSMatt Macy 
1036eda14cbcSMatt Macy 	abd_return_buf((abd_t *)goodabd, (void *)good, size);
1037eda14cbcSMatt Macy 	abd_return_buf((abd_t *)badabd, (void *)bad, size);
1038eda14cbcSMatt Macy 
1039eda14cbcSMatt Macy 	eip->zei_allowed_mingap	*= sizeof (uint64_t);
1040eda14cbcSMatt Macy 	inline_size		*= sizeof (uint64_t);
1041eda14cbcSMatt Macy 
1042eda14cbcSMatt Macy 	/* fill in ereport */
1043eda14cbcSMatt Macy 	fm_payload_set(ereport,
1044eda14cbcSMatt Macy 	    FM_EREPORT_PAYLOAD_ZFS_BAD_OFFSET_RANGES,
1045eda14cbcSMatt Macy 	    DATA_TYPE_UINT32_ARRAY, 2 * eip->zei_range_count,
1046eda14cbcSMatt Macy 	    (uint32_t *)eip->zei_ranges,
1047eda14cbcSMatt Macy 	    FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_MIN_GAP,
1048eda14cbcSMatt Macy 	    DATA_TYPE_UINT32, eip->zei_allowed_mingap,
1049eda14cbcSMatt Macy 	    FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_SETS,
1050eda14cbcSMatt Macy 	    DATA_TYPE_UINT32_ARRAY, eip->zei_range_count, eip->zei_range_sets,
1051eda14cbcSMatt Macy 	    FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_CLEARS,
1052eda14cbcSMatt Macy 	    DATA_TYPE_UINT32_ARRAY, eip->zei_range_count, eip->zei_range_clears,
1053eda14cbcSMatt Macy 	    NULL);
1054eda14cbcSMatt Macy 
1055eda14cbcSMatt Macy 	if (!no_inline) {
1056eda14cbcSMatt Macy 		fm_payload_set(ereport,
1057eda14cbcSMatt Macy 		    FM_EREPORT_PAYLOAD_ZFS_BAD_SET_BITS,
1058eda14cbcSMatt Macy 		    DATA_TYPE_UINT8_ARRAY,
1059eda14cbcSMatt Macy 		    inline_size, (uint8_t *)eip->zei_bits_set,
1060eda14cbcSMatt Macy 		    FM_EREPORT_PAYLOAD_ZFS_BAD_CLEARED_BITS,
1061eda14cbcSMatt Macy 		    DATA_TYPE_UINT8_ARRAY,
1062eda14cbcSMatt Macy 		    inline_size, (uint8_t *)eip->zei_bits_cleared,
1063eda14cbcSMatt Macy 		    NULL);
1064eda14cbcSMatt Macy 	}
1065eda14cbcSMatt Macy 	return (eip);
1066eda14cbcSMatt Macy }
1067ba27dd8bSMartin Matuska #else
1068ba27dd8bSMartin Matuska void
zfs_ereport_clear(spa_t * spa,vdev_t * vd)1069ba27dd8bSMartin Matuska zfs_ereport_clear(spa_t *spa, vdev_t *vd)
1070ba27dd8bSMartin Matuska {
1071e92ffd9bSMartin Matuska 	(void) spa, (void) vd;
1072ba27dd8bSMartin Matuska }
1073eda14cbcSMatt Macy #endif
1074eda14cbcSMatt Macy 
1075eda14cbcSMatt Macy /*
1076eda14cbcSMatt Macy  * Make sure our event is still valid for the given zio/vdev/pool.  For example,
1077eda14cbcSMatt Macy  * we don't want to keep logging events for a faulted or missing vdev.
1078eda14cbcSMatt Macy  */
1079eda14cbcSMatt Macy boolean_t
zfs_ereport_is_valid(const char * subclass,spa_t * spa,vdev_t * vd,zio_t * zio)1080eda14cbcSMatt Macy zfs_ereport_is_valid(const char *subclass, spa_t *spa, vdev_t *vd, zio_t *zio)
1081eda14cbcSMatt Macy {
1082eda14cbcSMatt Macy #ifdef _KERNEL
1083eda14cbcSMatt Macy 	/*
1084eda14cbcSMatt Macy 	 * If we are doing a spa_tryimport() or in recovery mode,
1085eda14cbcSMatt Macy 	 * ignore errors.
1086eda14cbcSMatt Macy 	 */
1087eda14cbcSMatt Macy 	if (spa_load_state(spa) == SPA_LOAD_TRYIMPORT ||
1088eda14cbcSMatt Macy 	    spa_load_state(spa) == SPA_LOAD_RECOVER)
1089eda14cbcSMatt Macy 		return (B_FALSE);
1090eda14cbcSMatt Macy 
1091eda14cbcSMatt Macy 	/*
1092eda14cbcSMatt Macy 	 * If we are in the middle of opening a pool, and the previous attempt
1093eda14cbcSMatt Macy 	 * failed, don't bother logging any new ereports - we're just going to
1094eda14cbcSMatt Macy 	 * get the same diagnosis anyway.
1095eda14cbcSMatt Macy 	 */
1096eda14cbcSMatt Macy 	if (spa_load_state(spa) != SPA_LOAD_NONE &&
1097eda14cbcSMatt Macy 	    spa->spa_last_open_failed)
1098eda14cbcSMatt Macy 		return (B_FALSE);
1099eda14cbcSMatt Macy 
1100eda14cbcSMatt Macy 	if (zio != NULL) {
11011719886fSMartin Matuska 		/* If this is not a read or write zio, ignore the error */
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
zfs_ereport_post(const char * subclass,spa_t * spa,vdev_t * vd,const zbookmark_phys_t * zb,zio_t * zio,uint64_t state)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
zfs_ereport_start_checksum(spa_t * spa,vdev_t * vd,const zbookmark_phys_t * zb,struct zio * zio,uint64_t offset,uint64_t length,zio_bad_cksum_t * info)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
zfs_ereport_finish_checksum(zio_cksum_report_t * report,const abd_t * good_data,const abd_t * bad_data,boolean_t drop_if_identical)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
zfs_ereport_free_checksum(zio_cksum_report_t * rpt)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
zfs_ereport_post_checksum(spa_t * spa,vdev_t * vd,const zbookmark_phys_t * zb,struct zio * zio,uint64_t offset,uint64_t length,const abd_t * good_data,const abd_t * bad_data,zio_bad_cksum_t * zbc)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 *
zfs_event_create(spa_t * spa,vdev_t * vd,const char * type,const char * name,nvlist_t * aux)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
zfs_post_common(spa_t * spa,vdev_t * vd,const char * type,const char * name,nvlist_t * aux)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
zfs_post_remove(spa_t * spa,vdev_t * vd)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
zfs_post_autoreplace(spa_t * spa,vdev_t * vd)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
zfs_post_state_change(spa_t * spa,vdev_t * vd,uint64_t laststate)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
zfs_ereport_init(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
zfs_ereport_taskq_fini(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
zfs_ereport_fini(void)15212c48331dSMatt Macy zfs_ereport_fini(void)
15222c48331dSMatt Macy {
15232c48331dSMatt Macy 	recent_events_node_t *entry;
15242c48331dSMatt Macy 
15254e8d558cSMartin Matuska 	while ((entry = list_remove_head(&recent_events_list)) != NULL) {
15262c48331dSMatt Macy 		avl_remove(&recent_events_tree, entry);
15272c48331dSMatt Macy 		kmem_free(entry, sizeof (*entry));
15282c48331dSMatt Macy 	}
15292c48331dSMatt Macy 	avl_destroy(&recent_events_tree);
15302c48331dSMatt Macy 	list_destroy(&recent_events_list);
15312c48331dSMatt Macy 	mutex_destroy(&recent_events_lock);
15322c48331dSMatt Macy }
15332c48331dSMatt Macy 
153453b70c86SMartin Matuska void
zfs_ereport_snapshot_post(const char * subclass,spa_t * spa,const char * name)153553b70c86SMartin Matuska zfs_ereport_snapshot_post(const char *subclass, spa_t *spa, const char *name)
153653b70c86SMartin Matuska {
153753b70c86SMartin Matuska 	nvlist_t *aux;
153853b70c86SMartin Matuska 
153953b70c86SMartin Matuska 	aux = fm_nvlist_create(NULL);
1540be181ee2SMartin Matuska 	fnvlist_add_string(aux, FM_EREPORT_PAYLOAD_ZFS_SNAPSHOT_NAME, name);
154153b70c86SMartin Matuska 
154253b70c86SMartin Matuska 	zfs_post_common(spa, NULL, FM_RSRC_CLASS, subclass, aux);
154353b70c86SMartin Matuska 	fm_nvlist_destroy(aux, FM_NVA_FREE);
154453b70c86SMartin Matuska }
154553b70c86SMartin Matuska 
154653b70c86SMartin Matuska /*
154753b70c86SMartin Matuska  * Post when a event when a zvol is created or removed
154853b70c86SMartin Matuska  *
154953b70c86SMartin Matuska  * This is currently only used by macOS, since it uses the event to create
155053b70c86SMartin Matuska  * symlinks between the volume name (mypool/myvol) and the actual /dev
155153b70c86SMartin Matuska  * device (/dev/disk3).  For example:
155253b70c86SMartin Matuska  *
155353b70c86SMartin Matuska  * /var/run/zfs/dsk/mypool/myvol -> /dev/disk3
155453b70c86SMartin Matuska  *
155553b70c86SMartin Matuska  * name: The full name of the zvol ("mypool/myvol")
155653b70c86SMartin Matuska  * dev_name: The full /dev name for the zvol ("/dev/disk3")
155753b70c86SMartin Matuska  * raw_name: The raw  /dev name for the zvol ("/dev/rdisk3")
155853b70c86SMartin Matuska  */
155953b70c86SMartin Matuska void
zfs_ereport_zvol_post(const char * subclass,const char * name,const char * dev_name,const char * raw_name)156053b70c86SMartin Matuska zfs_ereport_zvol_post(const char *subclass, const char *name,
156153b70c86SMartin Matuska     const char *dev_name, const char *raw_name)
156253b70c86SMartin Matuska {
156353b70c86SMartin Matuska 	nvlist_t *aux;
156453b70c86SMartin Matuska 	char *r;
156553b70c86SMartin Matuska 
156653b70c86SMartin Matuska 	boolean_t locked = mutex_owned(&spa_namespace_lock);
156753b70c86SMartin Matuska 	if (!locked) mutex_enter(&spa_namespace_lock);
156853b70c86SMartin Matuska 	spa_t *spa = spa_lookup(name);
156953b70c86SMartin Matuska 	if (!locked) mutex_exit(&spa_namespace_lock);
157053b70c86SMartin Matuska 
157153b70c86SMartin Matuska 	if (spa == NULL)
157253b70c86SMartin Matuska 		return;
157353b70c86SMartin Matuska 
157453b70c86SMartin Matuska 	aux = fm_nvlist_create(NULL);
1575be181ee2SMartin Matuska 	fnvlist_add_string(aux, FM_EREPORT_PAYLOAD_ZFS_DEVICE_NAME, dev_name);
1576be181ee2SMartin Matuska 	fnvlist_add_string(aux, FM_EREPORT_PAYLOAD_ZFS_RAW_DEVICE_NAME,
157753b70c86SMartin Matuska 	    raw_name);
157853b70c86SMartin Matuska 	r = strchr(name, '/');
157953b70c86SMartin Matuska 	if (r && r[1])
1580be181ee2SMartin Matuska 		fnvlist_add_string(aux, FM_EREPORT_PAYLOAD_ZFS_VOLUME, &r[1]);
158153b70c86SMartin Matuska 
158253b70c86SMartin Matuska 	zfs_post_common(spa, NULL, FM_RSRC_CLASS, subclass, aux);
158353b70c86SMartin Matuska 	fm_nvlist_destroy(aux, FM_NVA_FREE);
158453b70c86SMartin Matuska }
158553b70c86SMartin Matuska 
1586eda14cbcSMatt Macy EXPORT_SYMBOL(zfs_ereport_post);
1587eda14cbcSMatt Macy EXPORT_SYMBOL(zfs_ereport_is_valid);
1588eda14cbcSMatt Macy EXPORT_SYMBOL(zfs_ereport_post_checksum);
1589eda14cbcSMatt Macy EXPORT_SYMBOL(zfs_post_remove);
1590eda14cbcSMatt Macy EXPORT_SYMBOL(zfs_post_autoreplace);
1591eda14cbcSMatt Macy EXPORT_SYMBOL(zfs_post_state_change);
15922c48331dSMatt Macy 
15932c48331dSMatt Macy ZFS_MODULE_PARAM(zfs_zevent, zfs_zevent_, retain_max, UINT, ZMOD_RW,
15942c48331dSMatt Macy 	"Maximum recent zevents records to retain for duplicate checking");
15952c48331dSMatt Macy ZFS_MODULE_PARAM(zfs_zevent, zfs_zevent_, retain_expire_secs, UINT, ZMOD_RW,
15962c48331dSMatt Macy 	"Expiration time for recent zevents records");
1597eda14cbcSMatt Macy #endif /* _KERNEL */
1598