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