xref: /illumos-gate/usr/src/uts/common/fs/zfs/zfs_fm.c (revision b16da2e29e074fb6eaeadc4fd7d17ae7340ba240)
1ea8dc4b6Seschrock /*
2ea8dc4b6Seschrock  * CDDL HEADER START
3ea8dc4b6Seschrock  *
4ea8dc4b6Seschrock  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7ea8dc4b6Seschrock  *
8ea8dc4b6Seschrock  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9ea8dc4b6Seschrock  * or http://www.opensolaris.org/os/licensing.
10ea8dc4b6Seschrock  * See the License for the specific language governing permissions
11ea8dc4b6Seschrock  * and limitations under the License.
12ea8dc4b6Seschrock  *
13ea8dc4b6Seschrock  * When distributing Covered Code, include this CDDL HEADER in each
14ea8dc4b6Seschrock  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15ea8dc4b6Seschrock  * If applicable, add the following below this CDDL HEADER, with the
16ea8dc4b6Seschrock  * fields enclosed by brackets "[]" replaced with your own identifying
17ea8dc4b6Seschrock  * information: Portions Copyright [yyyy] [name of copyright owner]
18ea8dc4b6Seschrock  *
19ea8dc4b6Seschrock  * CDDL HEADER END
20ea8dc4b6Seschrock  */
21ea8dc4b6Seschrock /*
226809eb4eSEric Schrock  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23ea8dc4b6Seschrock  * Use is subject to license terms.
24ea8dc4b6Seschrock  */
25ea8dc4b6Seschrock 
26ea8dc4b6Seschrock #include <sys/spa.h>
27ea8dc4b6Seschrock #include <sys/spa_impl.h>
28ea8dc4b6Seschrock #include <sys/vdev.h>
29ea8dc4b6Seschrock #include <sys/vdev_impl.h>
30ea8dc4b6Seschrock #include <sys/zio.h>
3122fe2c88SJonathan Adams #include <sys/zio_checksum.h>
32ea8dc4b6Seschrock 
33ea8dc4b6Seschrock #include <sys/fm/fs/zfs.h>
34ea8dc4b6Seschrock #include <sys/fm/protocol.h>
35ea8dc4b6Seschrock #include <sys/fm/util.h>
36ea8dc4b6Seschrock #include <sys/sysevent.h>
37ea8dc4b6Seschrock 
38ea8dc4b6Seschrock /*
39ea8dc4b6Seschrock  * This general routine is responsible for generating all the different ZFS
40ea8dc4b6Seschrock  * ereports.  The payload is dependent on the class, and which arguments are
41ea8dc4b6Seschrock  * supplied to the function:
42ea8dc4b6Seschrock  *
43ea8dc4b6Seschrock  * 	EREPORT			POOL	VDEV	IO
44ea8dc4b6Seschrock  * 	block			X	X	X
45ea8dc4b6Seschrock  * 	data			X		X
46ea8dc4b6Seschrock  * 	device			X	X
47ea8dc4b6Seschrock  * 	pool			X
48ea8dc4b6Seschrock  *
49ea8dc4b6Seschrock  * If we are in a loading state, all errors are chained together by the same
5032b87932Sek  * SPA-wide ENA (Error Numeric Association).
51ea8dc4b6Seschrock  *
52ea8dc4b6Seschrock  * For isolated I/O requests, we get the ENA from the zio_t. The propagation
53ea8dc4b6Seschrock  * gets very complicated due to RAID-Z, gang blocks, and vdev caching.  We want
54ea8dc4b6Seschrock  * to chain together all ereports associated with a logical piece of data.  For
55ea8dc4b6Seschrock  * read I/Os, there  are basically three 'types' of I/O, which form a roughly
56ea8dc4b6Seschrock  * layered diagram:
57ea8dc4b6Seschrock  *
58ea8dc4b6Seschrock  *      +---------------+
59ea8dc4b6Seschrock  * 	| Aggregate I/O |	No associated logical data or device
60ea8dc4b6Seschrock  * 	+---------------+
61ea8dc4b6Seschrock  *              |
62ea8dc4b6Seschrock  *              V
63ea8dc4b6Seschrock  * 	+---------------+	Reads associated with a piece of logical data.
64ea8dc4b6Seschrock  * 	|   Read I/O    |	This includes reads on behalf of RAID-Z,
65ea8dc4b6Seschrock  * 	+---------------+       mirrors, gang blocks, retries, etc.
66ea8dc4b6Seschrock  *              |
67ea8dc4b6Seschrock  *              V
68ea8dc4b6Seschrock  * 	+---------------+	Reads associated with a particular device, but
69ea8dc4b6Seschrock  * 	| Physical I/O  |	no logical data.  Issued as part of vdev caching
70ea8dc4b6Seschrock  * 	+---------------+	and I/O aggregation.
71ea8dc4b6Seschrock  *
72ea8dc4b6Seschrock  * Note that 'physical I/O' here is not the same terminology as used in the rest
73ea8dc4b6Seschrock  * of ZIO.  Typically, 'physical I/O' simply means that there is no attached
74ea8dc4b6Seschrock  * blockpointer.  But I/O with no associated block pointer can still be related
75ea8dc4b6Seschrock  * to a logical piece of data (i.e. RAID-Z requests).
76ea8dc4b6Seschrock  *
77ea8dc4b6Seschrock  * Purely physical I/O always have unique ENAs.  They are not related to a
78ea8dc4b6Seschrock  * particular piece of logical data, and therefore cannot be chained together.
79ea8dc4b6Seschrock  * We still generate an ereport, but the DE doesn't correlate it with any
80ea8dc4b6Seschrock  * logical piece of data.  When such an I/O fails, the delegated I/O requests
81ea8dc4b6Seschrock  * will issue a retry, which will trigger the 'real' ereport with the correct
82ea8dc4b6Seschrock  * ENA.
83ea8dc4b6Seschrock  *
84ea8dc4b6Seschrock  * We keep track of the ENA for a ZIO chain through the 'io_logical' member.
85ea8dc4b6Seschrock  * When a new logical I/O is issued, we set this to point to itself.  Child I/Os
86ea8dc4b6Seschrock  * then inherit this pointer, so that when it is first set subsequent failures
87e14bb325SJeff Bonwick  * will use the same ENA.  For vdev cache fill and queue aggregation I/O,
88e14bb325SJeff Bonwick  * this pointer is set to NULL, and no ereport will be generated (since it
89e14bb325SJeff Bonwick  * doesn't actually correspond to any particular device or piece of data,
90e14bb325SJeff Bonwick  * and the caller will always retry without caching or queueing anyway).
9122fe2c88SJonathan Adams  *
9222fe2c88SJonathan Adams  * For checksum errors, we want to include more information about the actual
9322fe2c88SJonathan Adams  * error which occurs.  Accordingly, we build an ereport when the error is
9422fe2c88SJonathan Adams  * noticed, but instead of sending it in immediately, we hang it off of the
9522fe2c88SJonathan Adams  * io_cksum_report field of the logical IO.  When the logical IO completes
9622fe2c88SJonathan Adams  * (successfully or not), zfs_ereport_finish_checksum() is called with the
9722fe2c88SJonathan Adams  * good and bad versions of the buffer (if available), and we annotate the
9822fe2c88SJonathan Adams  * ereport with information about the differences.
99ea8dc4b6Seschrock  */
10022fe2c88SJonathan Adams #ifdef _KERNEL
10122fe2c88SJonathan Adams static void
10222fe2c88SJonathan Adams zfs_ereport_start(nvlist_t **ereport_out, nvlist_t **detector_out,
10322fe2c88SJonathan Adams     const char *subclass, spa_t *spa, vdev_t *vd, zio_t *zio,
104ea8dc4b6Seschrock     uint64_t stateoroffset, uint64_t size)
105ea8dc4b6Seschrock {
106ea8dc4b6Seschrock 	nvlist_t *ereport, *detector;
10722fe2c88SJonathan Adams 
108ea8dc4b6Seschrock 	uint64_t ena;
109ea8dc4b6Seschrock 	char class[64];
110ea8dc4b6Seschrock 
111ea8dc4b6Seschrock 	/*
112468c413aSTim Haley 	 * If we are doing a spa_tryimport() or in recovery mode,
113468c413aSTim Haley 	 * ignore errors.
114ea8dc4b6Seschrock 	 */
115*b16da2e2SGeorge Wilson 	if (spa_load_state(spa) == SPA_LOAD_TRYIMPORT ||
116*b16da2e2SGeorge Wilson 	    spa_load_state(spa) == SPA_LOAD_RECOVER)
117ea8dc4b6Seschrock 		return;
118ea8dc4b6Seschrock 
119ea8dc4b6Seschrock 	/*
120ea8dc4b6Seschrock 	 * If we are in the middle of opening a pool, and the previous attempt
121ea8dc4b6Seschrock 	 * failed, don't bother logging any new ereports - we're just going to
122ea8dc4b6Seschrock 	 * get the same diagnosis anyway.
123ea8dc4b6Seschrock 	 */
124*b16da2e2SGeorge Wilson 	if (spa_load_state(spa) != SPA_LOAD_NONE &&
125ea8dc4b6Seschrock 	    spa->spa_last_open_failed)
126ea8dc4b6Seschrock 		return;
127ea8dc4b6Seschrock 
128bf82a41bSeschrock 	if (zio != NULL) {
129bf82a41bSeschrock 		/*
130bf82a41bSeschrock 		 * If this is not a read or write zio, ignore the error.  This
131bf82a41bSeschrock 		 * can occur if the DKIOCFLUSHWRITECACHE ioctl fails.
132bf82a41bSeschrock 		 */
133bf82a41bSeschrock 		if (zio->io_type != ZIO_TYPE_READ &&
134bf82a41bSeschrock 		    zio->io_type != ZIO_TYPE_WRITE)
135bf82a41bSeschrock 			return;
136bf82a41bSeschrock 
137bf82a41bSeschrock 		/*
138bf82a41bSeschrock 		 * Ignore any errors from speculative I/Os, as failure is an
139bf82a41bSeschrock 		 * expected result.
140bf82a41bSeschrock 		 */
141bf82a41bSeschrock 		if (zio->io_flags & ZIO_FLAG_SPECULATIVE)
142bf82a41bSeschrock 			return;
14351ece835Seschrock 
1448956713aSEric Schrock 		/*
1458956713aSEric Schrock 		 * If this I/O is not a retry I/O, don't post an ereport.
1468956713aSEric Schrock 		 * Otherwise, we risk making bad diagnoses based on B_FAILFAST
1478956713aSEric Schrock 		 * I/Os.
1488956713aSEric Schrock 		 */
1498956713aSEric Schrock 		if (zio->io_error == EIO &&
1508956713aSEric Schrock 		    !(zio->io_flags & ZIO_FLAG_IO_RETRY))
1518956713aSEric Schrock 			return;
1528956713aSEric Schrock 
1536809eb4eSEric Schrock 		if (vd != NULL) {
1546809eb4eSEric Schrock 			/*
1556809eb4eSEric Schrock 			 * If the vdev has already been marked as failing due
1566809eb4eSEric Schrock 			 * to a failed probe, then ignore any subsequent I/O
1576809eb4eSEric Schrock 			 * errors, as the DE will automatically fault the vdev
1586809eb4eSEric Schrock 			 * on the first such failure.  This also catches cases
1596809eb4eSEric Schrock 			 * where vdev_remove_wanted is set and the device has
1606809eb4eSEric Schrock 			 * not yet been asynchronously placed into the REMOVED
1616809eb4eSEric Schrock 			 * state.
1626809eb4eSEric Schrock 			 */
1631d713200SEric Schrock 			if (zio->io_vd == vd && !vdev_accessible(vd, zio))
1646809eb4eSEric Schrock 				return;
1656809eb4eSEric Schrock 
1666809eb4eSEric Schrock 			/*
1676809eb4eSEric Schrock 			 * Ignore checksum errors for reads from DTL regions of
1686809eb4eSEric Schrock 			 * leaf vdevs.
1696809eb4eSEric Schrock 			 */
1706809eb4eSEric Schrock 			if (zio->io_type == ZIO_TYPE_READ &&
1716809eb4eSEric Schrock 			    zio->io_error == ECKSUM &&
1726809eb4eSEric Schrock 			    vd->vdev_ops->vdev_op_leaf &&
1736809eb4eSEric Schrock 			    vdev_dtl_contains(vd, DTL_MISSING, zio->io_txg, 1))
1746809eb4eSEric Schrock 				return;
1756809eb4eSEric Schrock 		}
176bf82a41bSeschrock 	}
177b468a217Seschrock 
1781d713200SEric Schrock 	/*
1791d713200SEric Schrock 	 * For probe failure, we want to avoid posting ereports if we've
1801d713200SEric Schrock 	 * already removed the device in the meantime.
1811d713200SEric Schrock 	 */
1821d713200SEric Schrock 	if (vd != NULL &&
1831d713200SEric Schrock 	    strcmp(subclass, FM_EREPORT_ZFS_PROBE_FAILURE) == 0 &&
1841d713200SEric Schrock 	    (vd->vdev_remove_wanted || vd->vdev_state == VDEV_STATE_REMOVED))
1851d713200SEric Schrock 		return;
1861d713200SEric Schrock 
187ea8dc4b6Seschrock 	if ((ereport = fm_nvlist_create(NULL)) == NULL)
188ea8dc4b6Seschrock 		return;
189ea8dc4b6Seschrock 
190ea8dc4b6Seschrock 	if ((detector = fm_nvlist_create(NULL)) == NULL) {
191ea8dc4b6Seschrock 		fm_nvlist_destroy(ereport, FM_NVA_FREE);
192ea8dc4b6Seschrock 		return;
193ea8dc4b6Seschrock 	}
194ea8dc4b6Seschrock 
195ea8dc4b6Seschrock 	/*
196ea8dc4b6Seschrock 	 * Serialize ereport generation
197ea8dc4b6Seschrock 	 */
198ea8dc4b6Seschrock 	mutex_enter(&spa->spa_errlist_lock);
199ea8dc4b6Seschrock 
200ea8dc4b6Seschrock 	/*
201ea8dc4b6Seschrock 	 * Determine the ENA to use for this event.  If we are in a loading
202ea8dc4b6Seschrock 	 * state, use a SPA-wide ENA.  Otherwise, if we are in an I/O state, use
203ea8dc4b6Seschrock 	 * a root zio-wide ENA.  Otherwise, simply use a unique ENA.
204ea8dc4b6Seschrock 	 */
205*b16da2e2SGeorge Wilson 	if (spa_load_state(spa) != SPA_LOAD_NONE) {
206ea8dc4b6Seschrock 		if (spa->spa_ena == 0)
207ea8dc4b6Seschrock 			spa->spa_ena = fm_ena_generate(0, FM_ENA_FMT1);
208ea8dc4b6Seschrock 		ena = spa->spa_ena;
209ea8dc4b6Seschrock 	} else if (zio != NULL && zio->io_logical != NULL) {
210ea8dc4b6Seschrock 		if (zio->io_logical->io_ena == 0)
211ea8dc4b6Seschrock 			zio->io_logical->io_ena =
212ea8dc4b6Seschrock 			    fm_ena_generate(0, FM_ENA_FMT1);
213ea8dc4b6Seschrock 		ena = zio->io_logical->io_ena;
214ea8dc4b6Seschrock 	} else {
215ea8dc4b6Seschrock 		ena = fm_ena_generate(0, FM_ENA_FMT1);
216ea8dc4b6Seschrock 	}
217ea8dc4b6Seschrock 
218ea8dc4b6Seschrock 	/*
219ea8dc4b6Seschrock 	 * Construct the full class, detector, and other standard FMA fields.
220ea8dc4b6Seschrock 	 */
221ea8dc4b6Seschrock 	(void) snprintf(class, sizeof (class), "%s.%s",
222ea8dc4b6Seschrock 	    ZFS_ERROR_CLASS, subclass);
223ea8dc4b6Seschrock 
224ea8dc4b6Seschrock 	fm_fmri_zfs_set(detector, FM_ZFS_SCHEME_VERSION, spa_guid(spa),
225ea8dc4b6Seschrock 	    vd != NULL ? vd->vdev_guid : 0);
226ea8dc4b6Seschrock 
227ea8dc4b6Seschrock 	fm_ereport_set(ereport, FM_EREPORT_VERSION, class, ena, detector, NULL);
228ea8dc4b6Seschrock 
229ea8dc4b6Seschrock 	/*
230ea8dc4b6Seschrock 	 * Construct the per-ereport payload, depending on which parameters are
231ea8dc4b6Seschrock 	 * passed in.
232ea8dc4b6Seschrock 	 */
233ea8dc4b6Seschrock 
234ea8dc4b6Seschrock 	/*
235ea8dc4b6Seschrock 	 * Generic payload members common to all ereports.
236ea8dc4b6Seschrock 	 */
237ea8dc4b6Seschrock 	fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_POOL,
238e14bb325SJeff Bonwick 	    DATA_TYPE_STRING, spa_name(spa), FM_EREPORT_PAYLOAD_ZFS_POOL_GUID,
239ea8dc4b6Seschrock 	    DATA_TYPE_UINT64, spa_guid(spa),
240ea8dc4b6Seschrock 	    FM_EREPORT_PAYLOAD_ZFS_POOL_CONTEXT, DATA_TYPE_INT32,
241*b16da2e2SGeorge Wilson 	    spa_load_state(spa), NULL);
242ea8dc4b6Seschrock 
24332b87932Sek 	if (spa != NULL) {
24432b87932Sek 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_POOL_FAILMODE,
24532b87932Sek 		    DATA_TYPE_STRING,
24632b87932Sek 		    spa_get_failmode(spa) == ZIO_FAILURE_MODE_WAIT ?
24732b87932Sek 		    FM_EREPORT_FAILMODE_WAIT :
24832b87932Sek 		    spa_get_failmode(spa) == ZIO_FAILURE_MODE_CONTINUE ?
24932b87932Sek 		    FM_EREPORT_FAILMODE_CONTINUE : FM_EREPORT_FAILMODE_PANIC,
25032b87932Sek 		    NULL);
25132b87932Sek 	}
25232b87932Sek 
253ea8dc4b6Seschrock 	if (vd != NULL) {
254ea8dc4b6Seschrock 		vdev_t *pvd = vd->vdev_parent;
255ea8dc4b6Seschrock 
256ea8dc4b6Seschrock 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_VDEV_GUID,
257ea8dc4b6Seschrock 		    DATA_TYPE_UINT64, vd->vdev_guid,
258ea8dc4b6Seschrock 		    FM_EREPORT_PAYLOAD_ZFS_VDEV_TYPE,
259ea8dc4b6Seschrock 		    DATA_TYPE_STRING, vd->vdev_ops->vdev_op_type, NULL);
2606809eb4eSEric Schrock 		if (vd->vdev_path != NULL)
261ea8dc4b6Seschrock 			fm_payload_set(ereport,
262ea8dc4b6Seschrock 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_PATH,
263ea8dc4b6Seschrock 			    DATA_TYPE_STRING, vd->vdev_path, NULL);
2646809eb4eSEric Schrock 		if (vd->vdev_devid != NULL)
265ea8dc4b6Seschrock 			fm_payload_set(ereport,
266ea8dc4b6Seschrock 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_DEVID,
267ea8dc4b6Seschrock 			    DATA_TYPE_STRING, vd->vdev_devid, NULL);
2686809eb4eSEric Schrock 		if (vd->vdev_fru != NULL)
2696809eb4eSEric Schrock 			fm_payload_set(ereport,
2706809eb4eSEric Schrock 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_FRU,
2716809eb4eSEric Schrock 			    DATA_TYPE_STRING, vd->vdev_fru, NULL);
272ea8dc4b6Seschrock 
273ea8dc4b6Seschrock 		if (pvd != NULL) {
274ea8dc4b6Seschrock 			fm_payload_set(ereport,
275ea8dc4b6Seschrock 			    FM_EREPORT_PAYLOAD_ZFS_PARENT_GUID,
276ea8dc4b6Seschrock 			    DATA_TYPE_UINT64, pvd->vdev_guid,
277ea8dc4b6Seschrock 			    FM_EREPORT_PAYLOAD_ZFS_PARENT_TYPE,
278ea8dc4b6Seschrock 			    DATA_TYPE_STRING, pvd->vdev_ops->vdev_op_type,
279ea8dc4b6Seschrock 			    NULL);
280ea8dc4b6Seschrock 			if (pvd->vdev_path)
281ea8dc4b6Seschrock 				fm_payload_set(ereport,
282ea8dc4b6Seschrock 				    FM_EREPORT_PAYLOAD_ZFS_PARENT_PATH,
283c25056deSgw 				    DATA_TYPE_STRING, pvd->vdev_path, NULL);
284ea8dc4b6Seschrock 			if (pvd->vdev_devid)
285ea8dc4b6Seschrock 				fm_payload_set(ereport,
286ea8dc4b6Seschrock 				    FM_EREPORT_PAYLOAD_ZFS_PARENT_DEVID,
287ea8dc4b6Seschrock 				    DATA_TYPE_STRING, pvd->vdev_devid, NULL);
288ea8dc4b6Seschrock 		}
289ea8dc4b6Seschrock 	}
290ea8dc4b6Seschrock 
291ea8dc4b6Seschrock 	if (zio != NULL) {
292ea8dc4b6Seschrock 		/*
293ea8dc4b6Seschrock 		 * Payload common to all I/Os.
294ea8dc4b6Seschrock 		 */
295ea8dc4b6Seschrock 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_ERR,
296ea8dc4b6Seschrock 		    DATA_TYPE_INT32, zio->io_error, NULL);
297ea8dc4b6Seschrock 
298ea8dc4b6Seschrock 		/*
299ea8dc4b6Seschrock 		 * If the 'size' parameter is non-zero, it indicates this is a
300ea8dc4b6Seschrock 		 * RAID-Z or other I/O where the physical offset and length are
301ea8dc4b6Seschrock 		 * provided for us, instead of within the zio_t.
302ea8dc4b6Seschrock 		 */
303ea8dc4b6Seschrock 		if (vd != NULL) {
304ea8dc4b6Seschrock 			if (size)
305ea8dc4b6Seschrock 				fm_payload_set(ereport,
306ea8dc4b6Seschrock 				    FM_EREPORT_PAYLOAD_ZFS_ZIO_OFFSET,
307ea8dc4b6Seschrock 				    DATA_TYPE_UINT64, stateoroffset,
308ea8dc4b6Seschrock 				    FM_EREPORT_PAYLOAD_ZFS_ZIO_SIZE,
309c3c6d682Seschrock 				    DATA_TYPE_UINT64, size, NULL);
310ea8dc4b6Seschrock 			else
311ea8dc4b6Seschrock 				fm_payload_set(ereport,
312ea8dc4b6Seschrock 				    FM_EREPORT_PAYLOAD_ZFS_ZIO_OFFSET,
313ea8dc4b6Seschrock 				    DATA_TYPE_UINT64, zio->io_offset,
314ea8dc4b6Seschrock 				    FM_EREPORT_PAYLOAD_ZFS_ZIO_SIZE,
315c3c6d682Seschrock 				    DATA_TYPE_UINT64, zio->io_size, NULL);
316ea8dc4b6Seschrock 		}
317ea8dc4b6Seschrock 
318ea8dc4b6Seschrock 		/*
319ea8dc4b6Seschrock 		 * Payload for I/Os with corresponding logical information.
320ea8dc4b6Seschrock 		 */
321ea8dc4b6Seschrock 		if (zio->io_logical != NULL)
322ea8dc4b6Seschrock 			fm_payload_set(ereport,
323e7cbe64fSgw 			    FM_EREPORT_PAYLOAD_ZFS_ZIO_OBJSET,
324e7cbe64fSgw 			    DATA_TYPE_UINT64,
325e7cbe64fSgw 			    zio->io_logical->io_bookmark.zb_objset,
326ea8dc4b6Seschrock 			    FM_EREPORT_PAYLOAD_ZFS_ZIO_OBJECT,
327ea8dc4b6Seschrock 			    DATA_TYPE_UINT64,
328ea8dc4b6Seschrock 			    zio->io_logical->io_bookmark.zb_object,
329ea8dc4b6Seschrock 			    FM_EREPORT_PAYLOAD_ZFS_ZIO_LEVEL,
330c25056deSgw 			    DATA_TYPE_INT64,
331ea8dc4b6Seschrock 			    zio->io_logical->io_bookmark.zb_level,
332ea8dc4b6Seschrock 			    FM_EREPORT_PAYLOAD_ZFS_ZIO_BLKID,
333ea8dc4b6Seschrock 			    DATA_TYPE_UINT64,
334c3c6d682Seschrock 			    zio->io_logical->io_bookmark.zb_blkid, NULL);
335ea8dc4b6Seschrock 	} else if (vd != NULL) {
336ea8dc4b6Seschrock 		/*
337ea8dc4b6Seschrock 		 * If we have a vdev but no zio, this is a device fault, and the
338ea8dc4b6Seschrock 		 * 'stateoroffset' parameter indicates the previous state of the
339ea8dc4b6Seschrock 		 * vdev.
340ea8dc4b6Seschrock 		 */
341ea8dc4b6Seschrock 		fm_payload_set(ereport,
342ea8dc4b6Seschrock 		    FM_EREPORT_PAYLOAD_ZFS_PREV_STATE,
343ea8dc4b6Seschrock 		    DATA_TYPE_UINT64, stateoroffset, NULL);
344ea8dc4b6Seschrock 	}
345468c413aSTim Haley 
346ea8dc4b6Seschrock 	mutex_exit(&spa->spa_errlist_lock);
347ea8dc4b6Seschrock 
34822fe2c88SJonathan Adams 	*ereport_out = ereport;
34922fe2c88SJonathan Adams 	*detector_out = detector;
35022fe2c88SJonathan Adams }
35122fe2c88SJonathan Adams 
35222fe2c88SJonathan Adams /* if it's <= 128 bytes, save the corruption directly */
35322fe2c88SJonathan Adams #define	ZFM_MAX_INLINE		(128 / sizeof (uint64_t))
35422fe2c88SJonathan Adams 
35522fe2c88SJonathan Adams #define	MAX_RANGES		16
35622fe2c88SJonathan Adams 
35722fe2c88SJonathan Adams typedef struct zfs_ecksum_info {
35822fe2c88SJonathan Adams 	/* histograms of set and cleared bits by bit number in a 64-bit word */
35922fe2c88SJonathan Adams 	uint16_t zei_histogram_set[sizeof (uint64_t) * NBBY];
36022fe2c88SJonathan Adams 	uint16_t zei_histogram_cleared[sizeof (uint64_t) * NBBY];
36122fe2c88SJonathan Adams 
36222fe2c88SJonathan Adams 	/* inline arrays of bits set and cleared. */
36322fe2c88SJonathan Adams 	uint64_t zei_bits_set[ZFM_MAX_INLINE];
36422fe2c88SJonathan Adams 	uint64_t zei_bits_cleared[ZFM_MAX_INLINE];
36522fe2c88SJonathan Adams 
36622fe2c88SJonathan Adams 	/*
36722fe2c88SJonathan Adams 	 * for each range, the number of bits set and cleared.  The Hamming
36822fe2c88SJonathan Adams 	 * distance between the good and bad buffers is the sum of them all.
36922fe2c88SJonathan Adams 	 */
37022fe2c88SJonathan Adams 	uint32_t zei_range_sets[MAX_RANGES];
37122fe2c88SJonathan Adams 	uint32_t zei_range_clears[MAX_RANGES];
37222fe2c88SJonathan Adams 
37322fe2c88SJonathan Adams 	struct zei_ranges {
37422fe2c88SJonathan Adams 		uint32_t	zr_start;
37522fe2c88SJonathan Adams 		uint32_t	zr_end;
37622fe2c88SJonathan Adams 	} zei_ranges[MAX_RANGES];
37722fe2c88SJonathan Adams 
37822fe2c88SJonathan Adams 	size_t	zei_range_count;
37922fe2c88SJonathan Adams 	uint32_t zei_mingap;
38022fe2c88SJonathan Adams 	uint32_t zei_allowed_mingap;
38122fe2c88SJonathan Adams 
38222fe2c88SJonathan Adams } zfs_ecksum_info_t;
38322fe2c88SJonathan Adams 
38422fe2c88SJonathan Adams static void
38522fe2c88SJonathan Adams update_histogram(uint64_t value_arg, uint16_t *hist, uint32_t *count)
38622fe2c88SJonathan Adams {
38722fe2c88SJonathan Adams 	size_t i;
38822fe2c88SJonathan Adams 	size_t bits = 0;
38922fe2c88SJonathan Adams 	uint64_t value = BE_64(value_arg);
39022fe2c88SJonathan Adams 
39122fe2c88SJonathan Adams 	/* We store the bits in big-endian (largest-first) order */
39222fe2c88SJonathan Adams 	for (i = 0; i < 64; i++) {
39322fe2c88SJonathan Adams 		if (value & (1ull << i)) {
39422fe2c88SJonathan Adams 			hist[63 - i]++;
39522fe2c88SJonathan Adams 			++bits;
39622fe2c88SJonathan Adams 		}
39722fe2c88SJonathan Adams 	}
39822fe2c88SJonathan Adams 	/* update the count of bits changed */
39922fe2c88SJonathan Adams 	*count += bits;
40022fe2c88SJonathan Adams }
40122fe2c88SJonathan Adams 
40222fe2c88SJonathan Adams /*
40322fe2c88SJonathan Adams  * We've now filled up the range array, and need to increase "mingap" and
40422fe2c88SJonathan Adams  * shrink the range list accordingly.  zei_mingap is always the smallest
40522fe2c88SJonathan Adams  * distance between array entries, so we set the new_allowed_gap to be
40622fe2c88SJonathan Adams  * one greater than that.  We then go through the list, joining together
40722fe2c88SJonathan Adams  * any ranges which are closer than the new_allowed_gap.
40822fe2c88SJonathan Adams  *
40922fe2c88SJonathan Adams  * By construction, there will be at least one.  We also update zei_mingap
41022fe2c88SJonathan Adams  * to the new smallest gap, to prepare for our next invocation.
41122fe2c88SJonathan Adams  */
41222fe2c88SJonathan Adams static void
41322fe2c88SJonathan Adams shrink_ranges(zfs_ecksum_info_t *eip)
41422fe2c88SJonathan Adams {
41522fe2c88SJonathan Adams 	uint32_t mingap = UINT32_MAX;
41622fe2c88SJonathan Adams 	uint32_t new_allowed_gap = eip->zei_mingap + 1;
41722fe2c88SJonathan Adams 
41822fe2c88SJonathan Adams 	size_t idx, output;
41922fe2c88SJonathan Adams 	size_t max = eip->zei_range_count;
42022fe2c88SJonathan Adams 
42122fe2c88SJonathan Adams 	struct zei_ranges *r = eip->zei_ranges;
42222fe2c88SJonathan Adams 
42322fe2c88SJonathan Adams 	ASSERT3U(eip->zei_range_count, >, 0);
42422fe2c88SJonathan Adams 	ASSERT3U(eip->zei_range_count, <=, MAX_RANGES);
42522fe2c88SJonathan Adams 
42622fe2c88SJonathan Adams 	output = idx = 0;
42722fe2c88SJonathan Adams 	while (idx < max - 1) {
42822fe2c88SJonathan Adams 		uint32_t start = r[idx].zr_start;
42922fe2c88SJonathan Adams 		uint32_t end = r[idx].zr_end;
43022fe2c88SJonathan Adams 
43122fe2c88SJonathan Adams 		while (idx < max - 1) {
43222fe2c88SJonathan Adams 			idx++;
43322fe2c88SJonathan Adams 
43422fe2c88SJonathan Adams 			uint32_t nstart = r[idx].zr_start;
43522fe2c88SJonathan Adams 			uint32_t nend = r[idx].zr_end;
43622fe2c88SJonathan Adams 
43722fe2c88SJonathan Adams 			uint32_t gap = nstart - end;
43822fe2c88SJonathan Adams 			if (gap < new_allowed_gap) {
43922fe2c88SJonathan Adams 				end = nend;
44022fe2c88SJonathan Adams 				continue;
44122fe2c88SJonathan Adams 			}
44222fe2c88SJonathan Adams 			if (gap < mingap)
44322fe2c88SJonathan Adams 				mingap = gap;
44422fe2c88SJonathan Adams 			break;
44522fe2c88SJonathan Adams 		}
44622fe2c88SJonathan Adams 		r[output].zr_start = start;
44722fe2c88SJonathan Adams 		r[output].zr_end = end;
44822fe2c88SJonathan Adams 		output++;
44922fe2c88SJonathan Adams 	}
45022fe2c88SJonathan Adams 	ASSERT3U(output, <, eip->zei_range_count);
45122fe2c88SJonathan Adams 	eip->zei_range_count = output;
45222fe2c88SJonathan Adams 	eip->zei_mingap = mingap;
45322fe2c88SJonathan Adams 	eip->zei_allowed_mingap = new_allowed_gap;
45422fe2c88SJonathan Adams }
45522fe2c88SJonathan Adams 
45622fe2c88SJonathan Adams static void
45722fe2c88SJonathan Adams add_range(zfs_ecksum_info_t *eip, int start, int end)
45822fe2c88SJonathan Adams {
45922fe2c88SJonathan Adams 	struct zei_ranges *r = eip->zei_ranges;
46022fe2c88SJonathan Adams 	size_t count = eip->zei_range_count;
46122fe2c88SJonathan Adams 
46222fe2c88SJonathan Adams 	if (count >= MAX_RANGES) {
46322fe2c88SJonathan Adams 		shrink_ranges(eip);
46422fe2c88SJonathan Adams 		count = eip->zei_range_count;
46522fe2c88SJonathan Adams 	}
46622fe2c88SJonathan Adams 	if (count == 0) {
46722fe2c88SJonathan Adams 		eip->zei_mingap = UINT32_MAX;
46822fe2c88SJonathan Adams 		eip->zei_allowed_mingap = 1;
46922fe2c88SJonathan Adams 	} else {
47022fe2c88SJonathan Adams 		int gap = start - r[count - 1].zr_end;
47122fe2c88SJonathan Adams 
47222fe2c88SJonathan Adams 		if (gap < eip->zei_allowed_mingap) {
47322fe2c88SJonathan Adams 			r[count - 1].zr_end = end;
47422fe2c88SJonathan Adams 			return;
47522fe2c88SJonathan Adams 		}
47622fe2c88SJonathan Adams 		if (gap < eip->zei_mingap)
47722fe2c88SJonathan Adams 			eip->zei_mingap = gap;
47822fe2c88SJonathan Adams 	}
47922fe2c88SJonathan Adams 	r[count].zr_start = start;
48022fe2c88SJonathan Adams 	r[count].zr_end = end;
48122fe2c88SJonathan Adams 	eip->zei_range_count++;
48222fe2c88SJonathan Adams }
48322fe2c88SJonathan Adams 
48422fe2c88SJonathan Adams static size_t
48522fe2c88SJonathan Adams range_total_size(zfs_ecksum_info_t *eip)
48622fe2c88SJonathan Adams {
48722fe2c88SJonathan Adams 	struct zei_ranges *r = eip->zei_ranges;
48822fe2c88SJonathan Adams 	size_t count = eip->zei_range_count;
48922fe2c88SJonathan Adams 	size_t result = 0;
49022fe2c88SJonathan Adams 	size_t idx;
49122fe2c88SJonathan Adams 
49222fe2c88SJonathan Adams 	for (idx = 0; idx < count; idx++)
49322fe2c88SJonathan Adams 		result += (r[idx].zr_end - r[idx].zr_start);
49422fe2c88SJonathan Adams 
49522fe2c88SJonathan Adams 	return (result);
49622fe2c88SJonathan Adams }
49722fe2c88SJonathan Adams 
49822fe2c88SJonathan Adams static zfs_ecksum_info_t *
49922fe2c88SJonathan Adams annotate_ecksum(nvlist_t *ereport, zio_bad_cksum_t *info,
50022fe2c88SJonathan Adams     const uint8_t *goodbuf, const uint8_t *badbuf, size_t size,
50122fe2c88SJonathan Adams     boolean_t drop_if_identical)
50222fe2c88SJonathan Adams {
50322fe2c88SJonathan Adams 	const uint64_t *good = (const uint64_t *)goodbuf;
50422fe2c88SJonathan Adams 	const uint64_t *bad = (const uint64_t *)badbuf;
50522fe2c88SJonathan Adams 
50622fe2c88SJonathan Adams 	uint64_t allset = 0;
50722fe2c88SJonathan Adams 	uint64_t allcleared = 0;
50822fe2c88SJonathan Adams 
50922fe2c88SJonathan Adams 	size_t nui64s = size / sizeof (uint64_t);
51022fe2c88SJonathan Adams 
51122fe2c88SJonathan Adams 	size_t inline_size;
51222fe2c88SJonathan Adams 	int no_inline = 0;
51322fe2c88SJonathan Adams 	size_t idx;
51422fe2c88SJonathan Adams 	size_t range;
51522fe2c88SJonathan Adams 
51622fe2c88SJonathan Adams 	size_t offset = 0;
51722fe2c88SJonathan Adams 	ssize_t start = -1;
51822fe2c88SJonathan Adams 
51922fe2c88SJonathan Adams 	zfs_ecksum_info_t *eip = kmem_zalloc(sizeof (*eip), KM_SLEEP);
52022fe2c88SJonathan Adams 
52122fe2c88SJonathan Adams 	/* don't do any annotation for injected checksum errors */
52222fe2c88SJonathan Adams 	if (info != NULL && info->zbc_injected)
52322fe2c88SJonathan Adams 		return (eip);
52422fe2c88SJonathan Adams 
52522fe2c88SJonathan Adams 	if (info != NULL && info->zbc_has_cksum) {
52622fe2c88SJonathan Adams 		fm_payload_set(ereport,
52722fe2c88SJonathan Adams 		    FM_EREPORT_PAYLOAD_ZFS_CKSUM_EXPECTED,
52822fe2c88SJonathan Adams 		    DATA_TYPE_UINT64_ARRAY,
52922fe2c88SJonathan Adams 		    sizeof (info->zbc_expected) / sizeof (uint64_t),
53022fe2c88SJonathan Adams 		    (uint64_t *)&info->zbc_expected,
53122fe2c88SJonathan Adams 		    FM_EREPORT_PAYLOAD_ZFS_CKSUM_ACTUAL,
53222fe2c88SJonathan Adams 		    DATA_TYPE_UINT64_ARRAY,
53322fe2c88SJonathan Adams 		    sizeof (info->zbc_actual) / sizeof (uint64_t),
53422fe2c88SJonathan Adams 		    (uint64_t *)&info->zbc_actual,
53522fe2c88SJonathan Adams 		    FM_EREPORT_PAYLOAD_ZFS_CKSUM_ALGO,
53622fe2c88SJonathan Adams 		    DATA_TYPE_STRING,
53722fe2c88SJonathan Adams 		    info->zbc_checksum_name,
53822fe2c88SJonathan Adams 		    NULL);
53922fe2c88SJonathan Adams 
54022fe2c88SJonathan Adams 		if (info->zbc_byteswapped) {
54122fe2c88SJonathan Adams 			fm_payload_set(ereport,
54222fe2c88SJonathan Adams 			    FM_EREPORT_PAYLOAD_ZFS_CKSUM_BYTESWAP,
54322fe2c88SJonathan Adams 			    DATA_TYPE_BOOLEAN, 1,
54422fe2c88SJonathan Adams 			    NULL);
54522fe2c88SJonathan Adams 		}
54622fe2c88SJonathan Adams 	}
54722fe2c88SJonathan Adams 
54822fe2c88SJonathan Adams 	if (badbuf == NULL || goodbuf == NULL)
54922fe2c88SJonathan Adams 		return (eip);
55022fe2c88SJonathan Adams 
55122fe2c88SJonathan Adams 	ASSERT3U(nui64s, <=, UINT16_MAX);
55222fe2c88SJonathan Adams 	ASSERT3U(size, ==, nui64s * sizeof (uint64_t));
55322fe2c88SJonathan Adams 	ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
55422fe2c88SJonathan Adams 	ASSERT3U(size, <=, UINT32_MAX);
55522fe2c88SJonathan Adams 
55622fe2c88SJonathan Adams 	/* build up the range list by comparing the two buffers. */
55722fe2c88SJonathan Adams 	for (idx = 0; idx < nui64s; idx++) {
55822fe2c88SJonathan Adams 		if (good[idx] == bad[idx]) {
55922fe2c88SJonathan Adams 			if (start == -1)
56022fe2c88SJonathan Adams 				continue;
56122fe2c88SJonathan Adams 
56222fe2c88SJonathan Adams 			add_range(eip, start, idx);
56322fe2c88SJonathan Adams 			start = -1;
56422fe2c88SJonathan Adams 		} else {
56522fe2c88SJonathan Adams 			if (start != -1)
56622fe2c88SJonathan Adams 				continue;
56722fe2c88SJonathan Adams 
56822fe2c88SJonathan Adams 			start = idx;
56922fe2c88SJonathan Adams 		}
57022fe2c88SJonathan Adams 	}
57122fe2c88SJonathan Adams 	if (start != -1)
57222fe2c88SJonathan Adams 		add_range(eip, start, idx);
57322fe2c88SJonathan Adams 
57422fe2c88SJonathan Adams 	/* See if it will fit in our inline buffers */
57522fe2c88SJonathan Adams 	inline_size = range_total_size(eip);
57622fe2c88SJonathan Adams 	if (inline_size > ZFM_MAX_INLINE)
57722fe2c88SJonathan Adams 		no_inline = 1;
57822fe2c88SJonathan Adams 
57922fe2c88SJonathan Adams 	/*
58022fe2c88SJonathan Adams 	 * If there is no change and we want to drop if the buffers are
58122fe2c88SJonathan Adams 	 * identical, do so.
58222fe2c88SJonathan Adams 	 */
58322fe2c88SJonathan Adams 	if (inline_size == 0 && drop_if_identical) {
58422fe2c88SJonathan Adams 		kmem_free(eip, sizeof (*eip));
58522fe2c88SJonathan Adams 		return (NULL);
58622fe2c88SJonathan Adams 	}
58722fe2c88SJonathan Adams 
58822fe2c88SJonathan Adams 	/*
58922fe2c88SJonathan Adams 	 * Now walk through the ranges, filling in the details of the
59022fe2c88SJonathan Adams 	 * differences.  Also convert our uint64_t-array offsets to byte
59122fe2c88SJonathan Adams 	 * offsets.
59222fe2c88SJonathan Adams 	 */
59322fe2c88SJonathan Adams 	for (range = 0; range < eip->zei_range_count; range++) {
59422fe2c88SJonathan Adams 		size_t start = eip->zei_ranges[range].zr_start;
59522fe2c88SJonathan Adams 		size_t end = eip->zei_ranges[range].zr_end;
59622fe2c88SJonathan Adams 
59722fe2c88SJonathan Adams 		for (idx = start; idx < end; idx++) {
59822fe2c88SJonathan Adams 			uint64_t set, cleared;
59922fe2c88SJonathan Adams 
60022fe2c88SJonathan Adams 			// bits set in bad, but not in good
60122fe2c88SJonathan Adams 			set = ((~good[idx]) & bad[idx]);
60222fe2c88SJonathan Adams 			// bits set in good, but not in bad
60322fe2c88SJonathan Adams 			cleared = (good[idx] & (~bad[idx]));
60422fe2c88SJonathan Adams 
60522fe2c88SJonathan Adams 			allset |= set;
60622fe2c88SJonathan Adams 			allcleared |= cleared;
60722fe2c88SJonathan Adams 
60822fe2c88SJonathan Adams 			if (!no_inline) {
60922fe2c88SJonathan Adams 				ASSERT3U(offset, <, inline_size);
61022fe2c88SJonathan Adams 				eip->zei_bits_set[offset] = set;
61122fe2c88SJonathan Adams 				eip->zei_bits_cleared[offset] = cleared;
61222fe2c88SJonathan Adams 				offset++;
61322fe2c88SJonathan Adams 			}
61422fe2c88SJonathan Adams 
61522fe2c88SJonathan Adams 			update_histogram(set, eip->zei_histogram_set,
61622fe2c88SJonathan Adams 			    &eip->zei_range_sets[range]);
61722fe2c88SJonathan Adams 			update_histogram(cleared, eip->zei_histogram_cleared,
61822fe2c88SJonathan Adams 			    &eip->zei_range_clears[range]);
61922fe2c88SJonathan Adams 		}
62022fe2c88SJonathan Adams 
62122fe2c88SJonathan Adams 		/* convert to byte offsets */
62222fe2c88SJonathan Adams 		eip->zei_ranges[range].zr_start	*= sizeof (uint64_t);
62322fe2c88SJonathan Adams 		eip->zei_ranges[range].zr_end	*= sizeof (uint64_t);
62422fe2c88SJonathan Adams 	}
62522fe2c88SJonathan Adams 	eip->zei_allowed_mingap	*= sizeof (uint64_t);
62622fe2c88SJonathan Adams 	inline_size		*= sizeof (uint64_t);
62722fe2c88SJonathan Adams 
62822fe2c88SJonathan Adams 	/* fill in ereport */
62922fe2c88SJonathan Adams 	fm_payload_set(ereport,
63022fe2c88SJonathan Adams 	    FM_EREPORT_PAYLOAD_ZFS_BAD_OFFSET_RANGES,
63122fe2c88SJonathan Adams 	    DATA_TYPE_UINT32_ARRAY, 2 * eip->zei_range_count,
63222fe2c88SJonathan Adams 	    (uint32_t *)eip->zei_ranges,
63322fe2c88SJonathan Adams 	    FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_MIN_GAP,
63422fe2c88SJonathan Adams 	    DATA_TYPE_UINT32, eip->zei_allowed_mingap,
63522fe2c88SJonathan Adams 	    FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_SETS,
63622fe2c88SJonathan Adams 	    DATA_TYPE_UINT32_ARRAY, eip->zei_range_count, eip->zei_range_sets,
63722fe2c88SJonathan Adams 	    FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_CLEARS,
63822fe2c88SJonathan Adams 	    DATA_TYPE_UINT32_ARRAY, eip->zei_range_count, eip->zei_range_clears,
63922fe2c88SJonathan Adams 	    NULL);
64022fe2c88SJonathan Adams 
64122fe2c88SJonathan Adams 	if (!no_inline) {
64222fe2c88SJonathan Adams 		fm_payload_set(ereport,
64322fe2c88SJonathan Adams 		    FM_EREPORT_PAYLOAD_ZFS_BAD_SET_BITS,
64422fe2c88SJonathan Adams 		    DATA_TYPE_UINT8_ARRAY,
64522fe2c88SJonathan Adams 		    inline_size, (uint8_t *)eip->zei_bits_set,
64622fe2c88SJonathan Adams 		    FM_EREPORT_PAYLOAD_ZFS_BAD_CLEARED_BITS,
64722fe2c88SJonathan Adams 		    DATA_TYPE_UINT8_ARRAY,
64822fe2c88SJonathan Adams 		    inline_size, (uint8_t *)eip->zei_bits_cleared,
64922fe2c88SJonathan Adams 		    NULL);
65022fe2c88SJonathan Adams 	} else {
65122fe2c88SJonathan Adams 		fm_payload_set(ereport,
65222fe2c88SJonathan Adams 		    FM_EREPORT_PAYLOAD_ZFS_BAD_SET_HISTOGRAM,
65322fe2c88SJonathan Adams 		    DATA_TYPE_UINT16_ARRAY,
65422fe2c88SJonathan Adams 		    NBBY * sizeof (uint64_t), eip->zei_histogram_set,
65522fe2c88SJonathan Adams 		    FM_EREPORT_PAYLOAD_ZFS_BAD_CLEARED_HISTOGRAM,
65622fe2c88SJonathan Adams 		    DATA_TYPE_UINT16_ARRAY,
65722fe2c88SJonathan Adams 		    NBBY * sizeof (uint64_t), eip->zei_histogram_cleared,
65822fe2c88SJonathan Adams 		    NULL);
65922fe2c88SJonathan Adams 	}
66022fe2c88SJonathan Adams 	return (eip);
66122fe2c88SJonathan Adams }
66222fe2c88SJonathan Adams #endif
66322fe2c88SJonathan Adams 
66422fe2c88SJonathan Adams void
66522fe2c88SJonathan Adams zfs_ereport_post(const char *subclass, spa_t *spa, vdev_t *vd, zio_t *zio,
66622fe2c88SJonathan Adams     uint64_t stateoroffset, uint64_t size)
66722fe2c88SJonathan Adams {
66822fe2c88SJonathan Adams #ifdef _KERNEL
66922fe2c88SJonathan Adams 	nvlist_t *ereport = NULL;
67022fe2c88SJonathan Adams 	nvlist_t *detector = NULL;
67122fe2c88SJonathan Adams 
67222fe2c88SJonathan Adams 	zfs_ereport_start(&ereport, &detector,
67322fe2c88SJonathan Adams 	    subclass, spa, vd, zio, stateoroffset, size);
67422fe2c88SJonathan Adams 
67522fe2c88SJonathan Adams 	if (ereport == NULL)
67622fe2c88SJonathan Adams 		return;
67722fe2c88SJonathan Adams 
678ea8dc4b6Seschrock 	fm_ereport_post(ereport, EVCH_SLEEP);
679ea8dc4b6Seschrock 
680ea8dc4b6Seschrock 	fm_nvlist_destroy(ereport, FM_NVA_FREE);
681ea8dc4b6Seschrock 	fm_nvlist_destroy(detector, FM_NVA_FREE);
682ea8dc4b6Seschrock #endif
683ea8dc4b6Seschrock }
684ea8dc4b6Seschrock 
68522fe2c88SJonathan Adams void
68622fe2c88SJonathan Adams zfs_ereport_start_checksum(spa_t *spa, vdev_t *vd,
68722fe2c88SJonathan Adams     struct zio *zio, uint64_t offset, uint64_t length, void *arg,
68822fe2c88SJonathan Adams     zio_bad_cksum_t *info)
68922fe2c88SJonathan Adams {
69022fe2c88SJonathan Adams 	zio_cksum_report_t *report = kmem_zalloc(sizeof (*report), KM_SLEEP);
69122fe2c88SJonathan Adams 
69222fe2c88SJonathan Adams 	if (zio->io_vsd != NULL)
69322fe2c88SJonathan Adams 		zio->io_vsd_ops->vsd_cksum_report(zio, report, arg);
69422fe2c88SJonathan Adams 	else
69522fe2c88SJonathan Adams 		zio_vsd_default_cksum_report(zio, report, arg);
69622fe2c88SJonathan Adams 
69722fe2c88SJonathan Adams 	/* copy the checksum failure information if it was provided */
69822fe2c88SJonathan Adams 	if (info != NULL) {
69922fe2c88SJonathan Adams 		report->zcr_ckinfo = kmem_zalloc(sizeof (*info), KM_SLEEP);
70022fe2c88SJonathan Adams 		bcopy(info, report->zcr_ckinfo, sizeof (*info));
70122fe2c88SJonathan Adams 	}
70222fe2c88SJonathan Adams 
703b24ab676SJeff Bonwick 	report->zcr_align = 1ULL << vd->vdev_top->vdev_ashift;
70422fe2c88SJonathan Adams 	report->zcr_length = length;
70522fe2c88SJonathan Adams 
70622fe2c88SJonathan Adams #ifdef _KERNEL
70722fe2c88SJonathan Adams 	zfs_ereport_start(&report->zcr_ereport, &report->zcr_detector,
70822fe2c88SJonathan Adams 	    FM_EREPORT_ZFS_CHECKSUM, spa, vd, zio, offset, length);
70922fe2c88SJonathan Adams 
71022fe2c88SJonathan Adams 	if (report->zcr_ereport == NULL) {
71122fe2c88SJonathan Adams 		report->zcr_free(report->zcr_cbdata, report->zcr_cbinfo);
71222fe2c88SJonathan Adams 		kmem_free(report, sizeof (*report));
71322fe2c88SJonathan Adams 		return;
71422fe2c88SJonathan Adams 	}
71522fe2c88SJonathan Adams #endif
71622fe2c88SJonathan Adams 
71722fe2c88SJonathan Adams 	mutex_enter(&spa->spa_errlist_lock);
71822fe2c88SJonathan Adams 	report->zcr_next = zio->io_logical->io_cksum_report;
71922fe2c88SJonathan Adams 	zio->io_logical->io_cksum_report = report;
72022fe2c88SJonathan Adams 	mutex_exit(&spa->spa_errlist_lock);
72122fe2c88SJonathan Adams }
72222fe2c88SJonathan Adams 
72322fe2c88SJonathan Adams void
72422fe2c88SJonathan Adams zfs_ereport_finish_checksum(zio_cksum_report_t *report,
72522fe2c88SJonathan Adams     const void *good_data, const void *bad_data, boolean_t drop_if_identical)
72622fe2c88SJonathan Adams {
72722fe2c88SJonathan Adams #ifdef _KERNEL
72822fe2c88SJonathan Adams 	zfs_ecksum_info_t *info = NULL;
72922fe2c88SJonathan Adams 	info = annotate_ecksum(report->zcr_ereport, report->zcr_ckinfo,
73022fe2c88SJonathan Adams 	    good_data, bad_data, report->zcr_length, drop_if_identical);
73122fe2c88SJonathan Adams 
73222fe2c88SJonathan Adams 	if (info != NULL)
73322fe2c88SJonathan Adams 		fm_ereport_post(report->zcr_ereport, EVCH_SLEEP);
73422fe2c88SJonathan Adams 
73522fe2c88SJonathan Adams 	fm_nvlist_destroy(report->zcr_ereport, FM_NVA_FREE);
73622fe2c88SJonathan Adams 	fm_nvlist_destroy(report->zcr_detector, FM_NVA_FREE);
73722fe2c88SJonathan Adams 	report->zcr_ereport = report->zcr_detector = NULL;
73822fe2c88SJonathan Adams 
73922fe2c88SJonathan Adams 	if (info != NULL)
74022fe2c88SJonathan Adams 		kmem_free(info, sizeof (*info));
74122fe2c88SJonathan Adams #endif
74222fe2c88SJonathan Adams }
74322fe2c88SJonathan Adams 
74422fe2c88SJonathan Adams void
74522fe2c88SJonathan Adams zfs_ereport_free_checksum(zio_cksum_report_t *rpt)
74622fe2c88SJonathan Adams {
74722fe2c88SJonathan Adams #ifdef _KERNEL
74822fe2c88SJonathan Adams 	if (rpt->zcr_ereport != NULL) {
74922fe2c88SJonathan Adams 		fm_nvlist_destroy(rpt->zcr_ereport,
75022fe2c88SJonathan Adams 		    FM_NVA_FREE);
75122fe2c88SJonathan Adams 		fm_nvlist_destroy(rpt->zcr_detector,
75222fe2c88SJonathan Adams 		    FM_NVA_FREE);
75322fe2c88SJonathan Adams 	}
75422fe2c88SJonathan Adams #endif
75522fe2c88SJonathan Adams 	rpt->zcr_free(rpt->zcr_cbdata, rpt->zcr_cbinfo);
75622fe2c88SJonathan Adams 
75722fe2c88SJonathan Adams 	if (rpt->zcr_ckinfo != NULL)
75822fe2c88SJonathan Adams 		kmem_free(rpt->zcr_ckinfo, sizeof (*rpt->zcr_ckinfo));
75922fe2c88SJonathan Adams 
76022fe2c88SJonathan Adams 	kmem_free(rpt, sizeof (*rpt));
76122fe2c88SJonathan Adams }
76222fe2c88SJonathan Adams 
76322fe2c88SJonathan Adams void
76422fe2c88SJonathan Adams zfs_ereport_send_interim_checksum(zio_cksum_report_t *report)
76522fe2c88SJonathan Adams {
76622fe2c88SJonathan Adams #ifdef _KERNEL
76722fe2c88SJonathan Adams 	fm_ereport_post(report->zcr_ereport, EVCH_SLEEP);
76822fe2c88SJonathan Adams #endif
76922fe2c88SJonathan Adams }
77022fe2c88SJonathan Adams 
77122fe2c88SJonathan Adams void
77222fe2c88SJonathan Adams zfs_ereport_post_checksum(spa_t *spa, vdev_t *vd,
77322fe2c88SJonathan Adams     struct zio *zio, uint64_t offset, uint64_t length,
77422fe2c88SJonathan Adams     const void *good_data, const void *bad_data, zio_bad_cksum_t *zbc)
77522fe2c88SJonathan Adams {
77622fe2c88SJonathan Adams #ifdef _KERNEL
77722fe2c88SJonathan Adams 	nvlist_t *ereport = NULL;
77822fe2c88SJonathan Adams 	nvlist_t *detector = NULL;
77922fe2c88SJonathan Adams 	zfs_ecksum_info_t *info;
78022fe2c88SJonathan Adams 
78122fe2c88SJonathan Adams 	zfs_ereport_start(&ereport, &detector,
78222fe2c88SJonathan Adams 	    FM_EREPORT_ZFS_CHECKSUM, spa, vd, zio, offset, length);
78322fe2c88SJonathan Adams 
78422fe2c88SJonathan Adams 	if (ereport == NULL)
78522fe2c88SJonathan Adams 		return;
78622fe2c88SJonathan Adams 
78722fe2c88SJonathan Adams 	info = annotate_ecksum(ereport, zbc, good_data, bad_data, length,
78822fe2c88SJonathan Adams 	    B_FALSE);
78922fe2c88SJonathan Adams 
79022fe2c88SJonathan Adams 	if (info != NULL)
79122fe2c88SJonathan Adams 		fm_ereport_post(ereport, EVCH_SLEEP);
79222fe2c88SJonathan Adams 
79322fe2c88SJonathan Adams 	fm_nvlist_destroy(ereport, FM_NVA_FREE);
79422fe2c88SJonathan Adams 	fm_nvlist_destroy(detector, FM_NVA_FREE);
79522fe2c88SJonathan Adams 
79622fe2c88SJonathan Adams 	if (info != NULL)
79722fe2c88SJonathan Adams 		kmem_free(info, sizeof (*info));
79822fe2c88SJonathan Adams #endif
79922fe2c88SJonathan Adams }
80022fe2c88SJonathan Adams 
8013d7072f8Seschrock static void
8023d7072f8Seschrock zfs_post_common(spa_t *spa, vdev_t *vd, const char *name)
803ea8dc4b6Seschrock {
804ea8dc4b6Seschrock #ifdef _KERNEL
805ea8dc4b6Seschrock 	nvlist_t *resource;
806ea8dc4b6Seschrock 	char class[64];
807ea8dc4b6Seschrock 
808*b16da2e2SGeorge Wilson 	if (spa_load_state(spa) == SPA_LOAD_TRYIMPORT)
8091d713200SEric Schrock 		return;
8101d713200SEric Schrock 
811ea8dc4b6Seschrock 	if ((resource = fm_nvlist_create(NULL)) == NULL)
812ea8dc4b6Seschrock 		return;
813ea8dc4b6Seschrock 
814ea8dc4b6Seschrock 	(void) snprintf(class, sizeof (class), "%s.%s.%s", FM_RSRC_RESOURCE,
8153d7072f8Seschrock 	    ZFS_ERROR_CLASS, name);
816ea8dc4b6Seschrock 	VERIFY(nvlist_add_uint8(resource, FM_VERSION, FM_RSRC_VERSION) == 0);
817ea8dc4b6Seschrock 	VERIFY(nvlist_add_string(resource, FM_CLASS, class) == 0);
818ea8dc4b6Seschrock 	VERIFY(nvlist_add_uint64(resource,
819ea8dc4b6Seschrock 	    FM_EREPORT_PAYLOAD_ZFS_POOL_GUID, spa_guid(spa)) == 0);
820ea8dc4b6Seschrock 	if (vd)
821ea8dc4b6Seschrock 		VERIFY(nvlist_add_uint64(resource,
822ea8dc4b6Seschrock 		    FM_EREPORT_PAYLOAD_ZFS_VDEV_GUID, vd->vdev_guid) == 0);
823ea8dc4b6Seschrock 
824ea8dc4b6Seschrock 	fm_ereport_post(resource, EVCH_SLEEP);
825ea8dc4b6Seschrock 
826ea8dc4b6Seschrock 	fm_nvlist_destroy(resource, FM_NVA_FREE);
827ea8dc4b6Seschrock #endif
828ea8dc4b6Seschrock }
8293d7072f8Seschrock 
8303d7072f8Seschrock /*
8313d7072f8Seschrock  * The 'resource.fs.zfs.removed' event is an internal signal that the given vdev
8323d7072f8Seschrock  * has been removed from the system.  This will cause the DE to ignore any
8333d7072f8Seschrock  * recent I/O errors, inferring that they are due to the asynchronous device
8343d7072f8Seschrock  * removal.
8353d7072f8Seschrock  */
8363d7072f8Seschrock void
8373d7072f8Seschrock zfs_post_remove(spa_t *spa, vdev_t *vd)
8383d7072f8Seschrock {
8393d7072f8Seschrock 	zfs_post_common(spa, vd, FM_RESOURCE_REMOVED);
8403d7072f8Seschrock }
8413d7072f8Seschrock 
8423d7072f8Seschrock /*
8433d7072f8Seschrock  * The 'resource.fs.zfs.autoreplace' event is an internal signal that the pool
8443d7072f8Seschrock  * has the 'autoreplace' property set, and therefore any broken vdevs will be
8453d7072f8Seschrock  * handled by higher level logic, and no vdev fault should be generated.
8463d7072f8Seschrock  */
8473d7072f8Seschrock void
8483d7072f8Seschrock zfs_post_autoreplace(spa_t *spa, vdev_t *vd)
8493d7072f8Seschrock {
8503d7072f8Seschrock 	zfs_post_common(spa, vd, FM_RESOURCE_AUTOREPLACE);
8513d7072f8Seschrock }
852069f55e2SEric Schrock 
853069f55e2SEric Schrock /*
854069f55e2SEric Schrock  * The 'resource.fs.zfs.statechange' event is an internal signal that the
855069f55e2SEric Schrock  * given vdev has transitioned its state to DEGRADED or HEALTHY.  This will
856069f55e2SEric Schrock  * cause the retire agent to repair any outstanding fault management cases
857069f55e2SEric Schrock  * open because the device was not found (fault.fs.zfs.device).
858069f55e2SEric Schrock  */
859069f55e2SEric Schrock void
860069f55e2SEric Schrock zfs_post_state_change(spa_t *spa, vdev_t *vd)
861069f55e2SEric Schrock {
862069f55e2SEric Schrock 	zfs_post_common(spa, vd, FM_RESOURCE_STATECHANGE);
863069f55e2SEric Schrock }
864