1c1cb2cd8Shaad /*
2c1cb2cd8Shaad  * CDDL HEADER START
3c1cb2cd8Shaad  *
4c1cb2cd8Shaad  * The contents of this file are subject to the terms of the
5c1cb2cd8Shaad  * Common Development and Distribution License (the "License").
6c1cb2cd8Shaad  * You may not use this file except in compliance with the License.
7c1cb2cd8Shaad  *
8c1cb2cd8Shaad  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9c1cb2cd8Shaad  * or http://www.opensolaris.org/os/licensing.
10c1cb2cd8Shaad  * See the License for the specific language governing permissions
11c1cb2cd8Shaad  * and limitations under the License.
12c1cb2cd8Shaad  *
13c1cb2cd8Shaad  * When distributing Covered Code, include this CDDL HEADER in each
14c1cb2cd8Shaad  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15c1cb2cd8Shaad  * If applicable, add the following below this CDDL HEADER, with the
16c1cb2cd8Shaad  * fields enclosed by brackets "[]" replaced with your own identifying
17c1cb2cd8Shaad  * information: Portions Copyright [yyyy] [name of copyright owner]
18c1cb2cd8Shaad  *
19c1cb2cd8Shaad  * CDDL HEADER END
20c1cb2cd8Shaad  */
21c1cb2cd8Shaad /*
22f59c7639Shaad  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23c1cb2cd8Shaad  * Use is subject to license terms.
24c1cb2cd8Shaad  */
25c1cb2cd8Shaad 
26*93f3d2b8Schs /*
27*93f3d2b8Schs  * Copyright (c) 2012 by Delphix. All rights reserved.
28*93f3d2b8Schs  */
29*93f3d2b8Schs 
30c1cb2cd8Shaad #include <sys/spa.h>
31c1cb2cd8Shaad #include <sys/spa_impl.h>
32c1cb2cd8Shaad #include <sys/vdev.h>
33c1cb2cd8Shaad #include <sys/vdev_impl.h>
34c1cb2cd8Shaad #include <sys/zio.h>
35f59c7639Shaad #include <sys/zio_checksum.h>
36c1cb2cd8Shaad 
37c1cb2cd8Shaad #include <sys/fm/fs/zfs.h>
38c1cb2cd8Shaad #include <sys/fm/protocol.h>
39c1cb2cd8Shaad #include <sys/fm/util.h>
40c1cb2cd8Shaad #include <sys/sysevent.h>
41c1cb2cd8Shaad 
42c1cb2cd8Shaad /*
43c1cb2cd8Shaad  * This general routine is responsible for generating all the different ZFS
44c1cb2cd8Shaad  * ereports.  The payload is dependent on the class, and which arguments are
45c1cb2cd8Shaad  * supplied to the function:
46c1cb2cd8Shaad  *
47c1cb2cd8Shaad  * 	EREPORT			POOL	VDEV	IO
48c1cb2cd8Shaad  * 	block			X	X	X
49c1cb2cd8Shaad  * 	data			X		X
50c1cb2cd8Shaad  * 	device			X	X
51c1cb2cd8Shaad  * 	pool			X
52c1cb2cd8Shaad  *
53c1cb2cd8Shaad  * If we are in a loading state, all errors are chained together by the same
54c1cb2cd8Shaad  * SPA-wide ENA (Error Numeric Association).
55c1cb2cd8Shaad  *
56c1cb2cd8Shaad  * For isolated I/O requests, we get the ENA from the zio_t. The propagation
57c1cb2cd8Shaad  * gets very complicated due to RAID-Z, gang blocks, and vdev caching.  We want
58c1cb2cd8Shaad  * to chain together all ereports associated with a logical piece of data.  For
59c1cb2cd8Shaad  * read I/Os, there  are basically three 'types' of I/O, which form a roughly
60c1cb2cd8Shaad  * layered diagram:
61c1cb2cd8Shaad  *
62c1cb2cd8Shaad  *      +---------------+
63c1cb2cd8Shaad  * 	| Aggregate I/O |	No associated logical data or device
64c1cb2cd8Shaad  * 	+---------------+
65c1cb2cd8Shaad  *              |
66c1cb2cd8Shaad  *              V
67c1cb2cd8Shaad  * 	+---------------+	Reads associated with a piece of logical data.
68c1cb2cd8Shaad  * 	|   Read I/O    |	This includes reads on behalf of RAID-Z,
69c1cb2cd8Shaad  * 	+---------------+       mirrors, gang blocks, retries, etc.
70c1cb2cd8Shaad  *              |
71c1cb2cd8Shaad  *              V
72c1cb2cd8Shaad  * 	+---------------+	Reads associated with a particular device, but
73c1cb2cd8Shaad  * 	| Physical I/O  |	no logical data.  Issued as part of vdev caching
74c1cb2cd8Shaad  * 	+---------------+	and I/O aggregation.
75c1cb2cd8Shaad  *
76c1cb2cd8Shaad  * Note that 'physical I/O' here is not the same terminology as used in the rest
77c1cb2cd8Shaad  * of ZIO.  Typically, 'physical I/O' simply means that there is no attached
78c1cb2cd8Shaad  * blockpointer.  But I/O with no associated block pointer can still be related
79c1cb2cd8Shaad  * to a logical piece of data (i.e. RAID-Z requests).
80c1cb2cd8Shaad  *
81c1cb2cd8Shaad  * Purely physical I/O always have unique ENAs.  They are not related to a
82c1cb2cd8Shaad  * particular piece of logical data, and therefore cannot be chained together.
83c1cb2cd8Shaad  * We still generate an ereport, but the DE doesn't correlate it with any
84c1cb2cd8Shaad  * logical piece of data.  When such an I/O fails, the delegated I/O requests
85c1cb2cd8Shaad  * will issue a retry, which will trigger the 'real' ereport with the correct
86c1cb2cd8Shaad  * ENA.
87c1cb2cd8Shaad  *
88c1cb2cd8Shaad  * We keep track of the ENA for a ZIO chain through the 'io_logical' member.
89c1cb2cd8Shaad  * When a new logical I/O is issued, we set this to point to itself.  Child I/Os
90c1cb2cd8Shaad  * then inherit this pointer, so that when it is first set subsequent failures
91c1cb2cd8Shaad  * will use the same ENA.  For vdev cache fill and queue aggregation I/O,
92c1cb2cd8Shaad  * this pointer is set to NULL, and no ereport will be generated (since it
93c1cb2cd8Shaad  * doesn't actually correspond to any particular device or piece of data,
94c1cb2cd8Shaad  * and the caller will always retry without caching or queueing anyway).
95f59c7639Shaad  *
96f59c7639Shaad  * For checksum errors, we want to include more information about the actual
97f59c7639Shaad  * error which occurs.  Accordingly, we build an ereport when the error is
98f59c7639Shaad  * noticed, but instead of sending it in immediately, we hang it off of the
99f59c7639Shaad  * io_cksum_report field of the logical IO.  When the logical IO completes
100f59c7639Shaad  * (successfully or not), zfs_ereport_finish_checksum() is called with the
101f59c7639Shaad  * good and bad versions of the buffer (if available), and we annotate the
102f59c7639Shaad  * ereport with information about the differences.
103c1cb2cd8Shaad  */
104f59c7639Shaad #ifdef _KERNEL
105f59c7639Shaad static void
zfs_ereport_start(nvlist_t ** ereport_out,nvlist_t ** detector_out,const char * subclass,spa_t * spa,vdev_t * vd,zio_t * zio,uint64_t stateoroffset,uint64_t size)106f59c7639Shaad zfs_ereport_start(nvlist_t **ereport_out, nvlist_t **detector_out,
107f59c7639Shaad     const char *subclass, spa_t *spa, vdev_t *vd, zio_t *zio,
108c1cb2cd8Shaad     uint64_t stateoroffset, uint64_t size)
109c1cb2cd8Shaad {
110c1cb2cd8Shaad 	nvlist_t *ereport, *detector;
111f59c7639Shaad 
112c1cb2cd8Shaad 	uint64_t ena;
113c1cb2cd8Shaad 	char class[64];
114c1cb2cd8Shaad 
115c1cb2cd8Shaad 	/*
116f59c7639Shaad 	 * If we are doing a spa_tryimport() or in recovery mode,
117f59c7639Shaad 	 * ignore errors.
118c1cb2cd8Shaad 	 */
119f59c7639Shaad 	if (spa_load_state(spa) == SPA_LOAD_TRYIMPORT ||
120f59c7639Shaad 	    spa_load_state(spa) == SPA_LOAD_RECOVER)
121c1cb2cd8Shaad 		return;
122c1cb2cd8Shaad 
123c1cb2cd8Shaad 	/*
124c1cb2cd8Shaad 	 * If we are in the middle of opening a pool, and the previous attempt
125c1cb2cd8Shaad 	 * failed, don't bother logging any new ereports - we're just going to
126c1cb2cd8Shaad 	 * get the same diagnosis anyway.
127c1cb2cd8Shaad 	 */
128f59c7639Shaad 	if (spa_load_state(spa) != SPA_LOAD_NONE &&
129c1cb2cd8Shaad 	    spa->spa_last_open_failed)
130c1cb2cd8Shaad 		return;
131c1cb2cd8Shaad 
132c1cb2cd8Shaad 	if (zio != NULL) {
133c1cb2cd8Shaad 		/*
134c1cb2cd8Shaad 		 * If this is not a read or write zio, ignore the error.  This
135c1cb2cd8Shaad 		 * can occur if the DKIOCFLUSHWRITECACHE ioctl fails.
136c1cb2cd8Shaad 		 */
137c1cb2cd8Shaad 		if (zio->io_type != ZIO_TYPE_READ &&
138c1cb2cd8Shaad 		    zio->io_type != ZIO_TYPE_WRITE)
139c1cb2cd8Shaad 			return;
140c1cb2cd8Shaad 
141c1cb2cd8Shaad 		/*
142c1cb2cd8Shaad 		 * Ignore any errors from speculative I/Os, as failure is an
143c1cb2cd8Shaad 		 * expected result.
144c1cb2cd8Shaad 		 */
145c1cb2cd8Shaad 		if (zio->io_flags & ZIO_FLAG_SPECULATIVE)
146c1cb2cd8Shaad 			return;
147c1cb2cd8Shaad 
148c1cb2cd8Shaad 		/*
149f59c7639Shaad 		 * If this I/O is not a retry I/O, don't post an ereport.
150f59c7639Shaad 		 * Otherwise, we risk making bad diagnoses based on B_FAILFAST
151f59c7639Shaad 		 * I/Os.
152c1cb2cd8Shaad 		 */
153f59c7639Shaad 		if (zio->io_error == EIO &&
154f59c7639Shaad 		    !(zio->io_flags & ZIO_FLAG_IO_RETRY))
155f59c7639Shaad 			return;
156f59c7639Shaad 
157f59c7639Shaad 		if (vd != NULL) {
158f59c7639Shaad 			/*
159f59c7639Shaad 			 * If the vdev has already been marked as failing due
160f59c7639Shaad 			 * to a failed probe, then ignore any subsequent I/O
161f59c7639Shaad 			 * errors, as the DE will automatically fault the vdev
162f59c7639Shaad 			 * on the first such failure.  This also catches cases
163f59c7639Shaad 			 * where vdev_remove_wanted is set and the device has
164f59c7639Shaad 			 * not yet been asynchronously placed into the REMOVED
165f59c7639Shaad 			 * state.
166f59c7639Shaad 			 */
167f59c7639Shaad 			if (zio->io_vd == vd && !vdev_accessible(vd, zio))
168f59c7639Shaad 				return;
169f59c7639Shaad 
170f59c7639Shaad 			/*
171f59c7639Shaad 			 * Ignore checksum errors for reads from DTL regions of
172f59c7639Shaad 			 * leaf vdevs.
173f59c7639Shaad 			 */
174f59c7639Shaad 			if (zio->io_type == ZIO_TYPE_READ &&
175f59c7639Shaad 			    zio->io_error == ECKSUM &&
176f59c7639Shaad 			    vd->vdev_ops->vdev_op_leaf &&
177f59c7639Shaad 			    vdev_dtl_contains(vd, DTL_MISSING, zio->io_txg, 1))
178c1cb2cd8Shaad 				return;
179c1cb2cd8Shaad 		}
180f59c7639Shaad 	}
181f59c7639Shaad 
182f59c7639Shaad 	/*
183f59c7639Shaad 	 * For probe failure, we want to avoid posting ereports if we've
184f59c7639Shaad 	 * already removed the device in the meantime.
185f59c7639Shaad 	 */
186f59c7639Shaad 	if (vd != NULL &&
187f59c7639Shaad 	    strcmp(subclass, FM_EREPORT_ZFS_PROBE_FAILURE) == 0 &&
188f59c7639Shaad 	    (vd->vdev_remove_wanted || vd->vdev_state == VDEV_STATE_REMOVED))
189f59c7639Shaad 		return;
190c1cb2cd8Shaad 
191c1cb2cd8Shaad 	if ((ereport = fm_nvlist_create(NULL)) == NULL)
192c1cb2cd8Shaad 		return;
193c1cb2cd8Shaad 
194c1cb2cd8Shaad 	if ((detector = fm_nvlist_create(NULL)) == NULL) {
195c1cb2cd8Shaad 		fm_nvlist_destroy(ereport, FM_NVA_FREE);
196c1cb2cd8Shaad 		return;
197c1cb2cd8Shaad 	}
198c1cb2cd8Shaad 
199c1cb2cd8Shaad 	/*
200c1cb2cd8Shaad 	 * Serialize ereport generation
201c1cb2cd8Shaad 	 */
202c1cb2cd8Shaad 	mutex_enter(&spa->spa_errlist_lock);
203c1cb2cd8Shaad 
204c1cb2cd8Shaad 	/*
205c1cb2cd8Shaad 	 * Determine the ENA to use for this event.  If we are in a loading
206c1cb2cd8Shaad 	 * state, use a SPA-wide ENA.  Otherwise, if we are in an I/O state, use
207c1cb2cd8Shaad 	 * a root zio-wide ENA.  Otherwise, simply use a unique ENA.
208c1cb2cd8Shaad 	 */
209f59c7639Shaad 	if (spa_load_state(spa) != SPA_LOAD_NONE) {
210c1cb2cd8Shaad 		if (spa->spa_ena == 0)
211c1cb2cd8Shaad 			spa->spa_ena = fm_ena_generate(0, FM_ENA_FMT1);
212c1cb2cd8Shaad 		ena = spa->spa_ena;
213c1cb2cd8Shaad 	} else if (zio != NULL && zio->io_logical != NULL) {
214c1cb2cd8Shaad 		if (zio->io_logical->io_ena == 0)
215c1cb2cd8Shaad 			zio->io_logical->io_ena =
216c1cb2cd8Shaad 			    fm_ena_generate(0, FM_ENA_FMT1);
217c1cb2cd8Shaad 		ena = zio->io_logical->io_ena;
218c1cb2cd8Shaad 	} else {
219c1cb2cd8Shaad 		ena = fm_ena_generate(0, FM_ENA_FMT1);
220c1cb2cd8Shaad 	}
221c1cb2cd8Shaad 
222c1cb2cd8Shaad 	/*
223c1cb2cd8Shaad 	 * Construct the full class, detector, and other standard FMA fields.
224c1cb2cd8Shaad 	 */
225c1cb2cd8Shaad 	(void) snprintf(class, sizeof (class), "%s.%s",
226c1cb2cd8Shaad 	    ZFS_ERROR_CLASS, subclass);
227c1cb2cd8Shaad 
228c1cb2cd8Shaad 	fm_fmri_zfs_set(detector, FM_ZFS_SCHEME_VERSION, spa_guid(spa),
229c1cb2cd8Shaad 	    vd != NULL ? vd->vdev_guid : 0);
230c1cb2cd8Shaad 
231c1cb2cd8Shaad 	fm_ereport_set(ereport, FM_EREPORT_VERSION, class, ena, detector, NULL);
232c1cb2cd8Shaad 
233c1cb2cd8Shaad 	/*
234c1cb2cd8Shaad 	 * Construct the per-ereport payload, depending on which parameters are
235c1cb2cd8Shaad 	 * passed in.
236c1cb2cd8Shaad 	 */
237c1cb2cd8Shaad 
238c1cb2cd8Shaad 	/*
239c1cb2cd8Shaad 	 * Generic payload members common to all ereports.
240c1cb2cd8Shaad 	 */
241c1cb2cd8Shaad 	fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_POOL,
242c1cb2cd8Shaad 	    DATA_TYPE_STRING, spa_name(spa), FM_EREPORT_PAYLOAD_ZFS_POOL_GUID,
243c1cb2cd8Shaad 	    DATA_TYPE_UINT64, spa_guid(spa),
244c1cb2cd8Shaad 	    FM_EREPORT_PAYLOAD_ZFS_POOL_CONTEXT, DATA_TYPE_INT32,
245f59c7639Shaad 	    spa_load_state(spa), NULL);
246c1cb2cd8Shaad 
247c1cb2cd8Shaad 	if (spa != NULL) {
248c1cb2cd8Shaad 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_POOL_FAILMODE,
249c1cb2cd8Shaad 		    DATA_TYPE_STRING,
250c1cb2cd8Shaad 		    spa_get_failmode(spa) == ZIO_FAILURE_MODE_WAIT ?
251c1cb2cd8Shaad 		    FM_EREPORT_FAILMODE_WAIT :
252c1cb2cd8Shaad 		    spa_get_failmode(spa) == ZIO_FAILURE_MODE_CONTINUE ?
253c1cb2cd8Shaad 		    FM_EREPORT_FAILMODE_CONTINUE : FM_EREPORT_FAILMODE_PANIC,
254c1cb2cd8Shaad 		    NULL);
255c1cb2cd8Shaad 	}
256c1cb2cd8Shaad 
257c1cb2cd8Shaad 	if (vd != NULL) {
258c1cb2cd8Shaad 		vdev_t *pvd = vd->vdev_parent;
259c1cb2cd8Shaad 
260c1cb2cd8Shaad 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_VDEV_GUID,
261c1cb2cd8Shaad 		    DATA_TYPE_UINT64, vd->vdev_guid,
262c1cb2cd8Shaad 		    FM_EREPORT_PAYLOAD_ZFS_VDEV_TYPE,
263c1cb2cd8Shaad 		    DATA_TYPE_STRING, vd->vdev_ops->vdev_op_type, NULL);
264f59c7639Shaad 		if (vd->vdev_path != NULL)
265c1cb2cd8Shaad 			fm_payload_set(ereport,
266c1cb2cd8Shaad 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_PATH,
267c1cb2cd8Shaad 			    DATA_TYPE_STRING, vd->vdev_path, NULL);
268f59c7639Shaad 		if (vd->vdev_devid != NULL)
269c1cb2cd8Shaad 			fm_payload_set(ereport,
270c1cb2cd8Shaad 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_DEVID,
271c1cb2cd8Shaad 			    DATA_TYPE_STRING, vd->vdev_devid, NULL);
272f59c7639Shaad 		if (vd->vdev_fru != NULL)
273f59c7639Shaad 			fm_payload_set(ereport,
274f59c7639Shaad 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_FRU,
275f59c7639Shaad 			    DATA_TYPE_STRING, vd->vdev_fru, NULL);
276c1cb2cd8Shaad 
277c1cb2cd8Shaad 		if (pvd != NULL) {
278c1cb2cd8Shaad 			fm_payload_set(ereport,
279c1cb2cd8Shaad 			    FM_EREPORT_PAYLOAD_ZFS_PARENT_GUID,
280c1cb2cd8Shaad 			    DATA_TYPE_UINT64, pvd->vdev_guid,
281c1cb2cd8Shaad 			    FM_EREPORT_PAYLOAD_ZFS_PARENT_TYPE,
282c1cb2cd8Shaad 			    DATA_TYPE_STRING, pvd->vdev_ops->vdev_op_type,
283c1cb2cd8Shaad 			    NULL);
284c1cb2cd8Shaad 			if (pvd->vdev_path)
285c1cb2cd8Shaad 				fm_payload_set(ereport,
286c1cb2cd8Shaad 				    FM_EREPORT_PAYLOAD_ZFS_PARENT_PATH,
287c1cb2cd8Shaad 				    DATA_TYPE_STRING, pvd->vdev_path, NULL);
288c1cb2cd8Shaad 			if (pvd->vdev_devid)
289c1cb2cd8Shaad 				fm_payload_set(ereport,
290c1cb2cd8Shaad 				    FM_EREPORT_PAYLOAD_ZFS_PARENT_DEVID,
291c1cb2cd8Shaad 				    DATA_TYPE_STRING, pvd->vdev_devid, NULL);
292c1cb2cd8Shaad 		}
293c1cb2cd8Shaad 	}
294c1cb2cd8Shaad 
295c1cb2cd8Shaad 	if (zio != NULL) {
296c1cb2cd8Shaad 		/*
297c1cb2cd8Shaad 		 * Payload common to all I/Os.
298c1cb2cd8Shaad 		 */
299c1cb2cd8Shaad 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_ERR,
300c1cb2cd8Shaad 		    DATA_TYPE_INT32, zio->io_error, NULL);
301c1cb2cd8Shaad 
302c1cb2cd8Shaad 		/*
303c1cb2cd8Shaad 		 * If the 'size' parameter is non-zero, it indicates this is a
304c1cb2cd8Shaad 		 * RAID-Z or other I/O where the physical offset and length are
305c1cb2cd8Shaad 		 * provided for us, instead of within the zio_t.
306c1cb2cd8Shaad 		 */
307c1cb2cd8Shaad 		if (vd != NULL) {
308c1cb2cd8Shaad 			if (size)
309c1cb2cd8Shaad 				fm_payload_set(ereport,
310c1cb2cd8Shaad 				    FM_EREPORT_PAYLOAD_ZFS_ZIO_OFFSET,
311c1cb2cd8Shaad 				    DATA_TYPE_UINT64, stateoroffset,
312c1cb2cd8Shaad 				    FM_EREPORT_PAYLOAD_ZFS_ZIO_SIZE,
313c1cb2cd8Shaad 				    DATA_TYPE_UINT64, size, NULL);
314c1cb2cd8Shaad 			else
315c1cb2cd8Shaad 				fm_payload_set(ereport,
316c1cb2cd8Shaad 				    FM_EREPORT_PAYLOAD_ZFS_ZIO_OFFSET,
317c1cb2cd8Shaad 				    DATA_TYPE_UINT64, zio->io_offset,
318c1cb2cd8Shaad 				    FM_EREPORT_PAYLOAD_ZFS_ZIO_SIZE,
319c1cb2cd8Shaad 				    DATA_TYPE_UINT64, zio->io_size, NULL);
320c1cb2cd8Shaad 		}
321c1cb2cd8Shaad 
322c1cb2cd8Shaad 		/*
323c1cb2cd8Shaad 		 * Payload for I/Os with corresponding logical information.
324c1cb2cd8Shaad 		 */
325c1cb2cd8Shaad 		if (zio->io_logical != NULL)
326c1cb2cd8Shaad 			fm_payload_set(ereport,
327c1cb2cd8Shaad 			    FM_EREPORT_PAYLOAD_ZFS_ZIO_OBJSET,
328c1cb2cd8Shaad 			    DATA_TYPE_UINT64,
329c1cb2cd8Shaad 			    zio->io_logical->io_bookmark.zb_objset,
330c1cb2cd8Shaad 			    FM_EREPORT_PAYLOAD_ZFS_ZIO_OBJECT,
331c1cb2cd8Shaad 			    DATA_TYPE_UINT64,
332c1cb2cd8Shaad 			    zio->io_logical->io_bookmark.zb_object,
333c1cb2cd8Shaad 			    FM_EREPORT_PAYLOAD_ZFS_ZIO_LEVEL,
334c1cb2cd8Shaad 			    DATA_TYPE_INT64,
335c1cb2cd8Shaad 			    zio->io_logical->io_bookmark.zb_level,
336c1cb2cd8Shaad 			    FM_EREPORT_PAYLOAD_ZFS_ZIO_BLKID,
337c1cb2cd8Shaad 			    DATA_TYPE_UINT64,
338c1cb2cd8Shaad 			    zio->io_logical->io_bookmark.zb_blkid, NULL);
339c1cb2cd8Shaad 	} else if (vd != NULL) {
340c1cb2cd8Shaad 		/*
341c1cb2cd8Shaad 		 * If we have a vdev but no zio, this is a device fault, and the
342c1cb2cd8Shaad 		 * 'stateoroffset' parameter indicates the previous state of the
343c1cb2cd8Shaad 		 * vdev.
344c1cb2cd8Shaad 		 */
345c1cb2cd8Shaad 		fm_payload_set(ereport,
346c1cb2cd8Shaad 		    FM_EREPORT_PAYLOAD_ZFS_PREV_STATE,
347c1cb2cd8Shaad 		    DATA_TYPE_UINT64, stateoroffset, NULL);
348c1cb2cd8Shaad 	}
349f59c7639Shaad 
350c1cb2cd8Shaad 	mutex_exit(&spa->spa_errlist_lock);
351c1cb2cd8Shaad 
352f59c7639Shaad 	*ereport_out = ereport;
353f59c7639Shaad 	*detector_out = detector;
354f59c7639Shaad }
355f59c7639Shaad 
356f59c7639Shaad /* if it's <= 128 bytes, save the corruption directly */
357f59c7639Shaad #define	ZFM_MAX_INLINE		(128 / sizeof (uint64_t))
358f59c7639Shaad 
359f59c7639Shaad #define	MAX_RANGES		16
360f59c7639Shaad 
361f59c7639Shaad typedef struct zfs_ecksum_info {
362f59c7639Shaad 	/* histograms of set and cleared bits by bit number in a 64-bit word */
363f59c7639Shaad 	uint16_t zei_histogram_set[sizeof (uint64_t) * NBBY];
364f59c7639Shaad 	uint16_t zei_histogram_cleared[sizeof (uint64_t) * NBBY];
365f59c7639Shaad 
366f59c7639Shaad 	/* inline arrays of bits set and cleared. */
367f59c7639Shaad 	uint64_t zei_bits_set[ZFM_MAX_INLINE];
368f59c7639Shaad 	uint64_t zei_bits_cleared[ZFM_MAX_INLINE];
369f59c7639Shaad 
370f59c7639Shaad 	/*
371f59c7639Shaad 	 * for each range, the number of bits set and cleared.  The Hamming
372f59c7639Shaad 	 * distance between the good and bad buffers is the sum of them all.
373f59c7639Shaad 	 */
374f59c7639Shaad 	uint32_t zei_range_sets[MAX_RANGES];
375f59c7639Shaad 	uint32_t zei_range_clears[MAX_RANGES];
376f59c7639Shaad 
377f59c7639Shaad 	struct zei_ranges {
378f59c7639Shaad 		uint32_t	zr_start;
379f59c7639Shaad 		uint32_t	zr_end;
380f59c7639Shaad 	} zei_ranges[MAX_RANGES];
381f59c7639Shaad 
382f59c7639Shaad 	size_t	zei_range_count;
383f59c7639Shaad 	uint32_t zei_mingap;
384f59c7639Shaad 	uint32_t zei_allowed_mingap;
385f59c7639Shaad 
386f59c7639Shaad } zfs_ecksum_info_t;
387f59c7639Shaad 
388f59c7639Shaad static void
update_histogram(uint64_t value_arg,uint16_t * hist,uint32_t * count)389f59c7639Shaad update_histogram(uint64_t value_arg, uint16_t *hist, uint32_t *count)
390f59c7639Shaad {
391f59c7639Shaad 	size_t i;
392f59c7639Shaad 	size_t bits = 0;
393f59c7639Shaad 	uint64_t value = BE_64(value_arg);
394f59c7639Shaad 
395f59c7639Shaad 	/* We store the bits in big-endian (largest-first) order */
396f59c7639Shaad 	for (i = 0; i < 64; i++) {
397f59c7639Shaad 		if (value & (1ull << i)) {
398f59c7639Shaad 			hist[63 - i]++;
399f59c7639Shaad 			++bits;
400f59c7639Shaad 		}
401f59c7639Shaad 	}
402f59c7639Shaad 	/* update the count of bits changed */
403f59c7639Shaad 	*count += bits;
404f59c7639Shaad }
405f59c7639Shaad 
406f59c7639Shaad /*
407f59c7639Shaad  * We've now filled up the range array, and need to increase "mingap" and
408f59c7639Shaad  * shrink the range list accordingly.  zei_mingap is always the smallest
409f59c7639Shaad  * distance between array entries, so we set the new_allowed_gap to be
410f59c7639Shaad  * one greater than that.  We then go through the list, joining together
411f59c7639Shaad  * any ranges which are closer than the new_allowed_gap.
412f59c7639Shaad  *
413f59c7639Shaad  * By construction, there will be at least one.  We also update zei_mingap
414f59c7639Shaad  * to the new smallest gap, to prepare for our next invocation.
415f59c7639Shaad  */
416f59c7639Shaad static void
shrink_ranges(zfs_ecksum_info_t * eip)417f59c7639Shaad shrink_ranges(zfs_ecksum_info_t *eip)
418f59c7639Shaad {
419f59c7639Shaad 	uint32_t mingap = UINT32_MAX;
420f59c7639Shaad 	uint32_t new_allowed_gap = eip->zei_mingap + 1;
421f59c7639Shaad 
422f59c7639Shaad 	size_t idx, output;
423f59c7639Shaad 	size_t max = eip->zei_range_count;
424f59c7639Shaad 
425f59c7639Shaad 	struct zei_ranges *r = eip->zei_ranges;
426f59c7639Shaad 
427f59c7639Shaad 	ASSERT3U(eip->zei_range_count, >, 0);
428f59c7639Shaad 	ASSERT3U(eip->zei_range_count, <=, MAX_RANGES);
429f59c7639Shaad 
430f59c7639Shaad 	output = idx = 0;
431f59c7639Shaad 	while (idx < max - 1) {
432f59c7639Shaad 		uint32_t start = r[idx].zr_start;
433f59c7639Shaad 		uint32_t end = r[idx].zr_end;
434f59c7639Shaad 
435f59c7639Shaad 		while (idx < max - 1) {
436f59c7639Shaad 			idx++;
437f59c7639Shaad 
438f59c7639Shaad 			uint32_t nstart = r[idx].zr_start;
439f59c7639Shaad 			uint32_t nend = r[idx].zr_end;
440f59c7639Shaad 
441f59c7639Shaad 			uint32_t gap = nstart - end;
442f59c7639Shaad 			if (gap < new_allowed_gap) {
443f59c7639Shaad 				end = nend;
444f59c7639Shaad 				continue;
445f59c7639Shaad 			}
446f59c7639Shaad 			if (gap < mingap)
447f59c7639Shaad 				mingap = gap;
448f59c7639Shaad 			break;
449f59c7639Shaad 		}
450f59c7639Shaad 		r[output].zr_start = start;
451f59c7639Shaad 		r[output].zr_end = end;
452f59c7639Shaad 		output++;
453f59c7639Shaad 	}
454f59c7639Shaad 	ASSERT3U(output, <, eip->zei_range_count);
455f59c7639Shaad 	eip->zei_range_count = output;
456f59c7639Shaad 	eip->zei_mingap = mingap;
457f59c7639Shaad 	eip->zei_allowed_mingap = new_allowed_gap;
458f59c7639Shaad }
459f59c7639Shaad 
460f59c7639Shaad static void
add_range(zfs_ecksum_info_t * eip,int start,int end)461f59c7639Shaad add_range(zfs_ecksum_info_t *eip, int start, int end)
462f59c7639Shaad {
463f59c7639Shaad 	struct zei_ranges *r = eip->zei_ranges;
464f59c7639Shaad 	size_t count = eip->zei_range_count;
465f59c7639Shaad 
466f59c7639Shaad 	if (count >= MAX_RANGES) {
467f59c7639Shaad 		shrink_ranges(eip);
468f59c7639Shaad 		count = eip->zei_range_count;
469f59c7639Shaad 	}
470f59c7639Shaad 	if (count == 0) {
471f59c7639Shaad 		eip->zei_mingap = UINT32_MAX;
472f59c7639Shaad 		eip->zei_allowed_mingap = 1;
473f59c7639Shaad 	} else {
474f59c7639Shaad 		int gap = start - r[count - 1].zr_end;
475f59c7639Shaad 
476f59c7639Shaad 		if (gap < eip->zei_allowed_mingap) {
477f59c7639Shaad 			r[count - 1].zr_end = end;
478f59c7639Shaad 			return;
479f59c7639Shaad 		}
480f59c7639Shaad 		if (gap < eip->zei_mingap)
481f59c7639Shaad 			eip->zei_mingap = gap;
482f59c7639Shaad 	}
483f59c7639Shaad 	r[count].zr_start = start;
484f59c7639Shaad 	r[count].zr_end = end;
485f59c7639Shaad 	eip->zei_range_count++;
486f59c7639Shaad }
487f59c7639Shaad 
488f59c7639Shaad static size_t
range_total_size(zfs_ecksum_info_t * eip)489f59c7639Shaad range_total_size(zfs_ecksum_info_t *eip)
490f59c7639Shaad {
491f59c7639Shaad 	struct zei_ranges *r = eip->zei_ranges;
492f59c7639Shaad 	size_t count = eip->zei_range_count;
493f59c7639Shaad 	size_t result = 0;
494f59c7639Shaad 	size_t idx;
495f59c7639Shaad 
496f59c7639Shaad 	for (idx = 0; idx < count; idx++)
497f59c7639Shaad 		result += (r[idx].zr_end - r[idx].zr_start);
498f59c7639Shaad 
499f59c7639Shaad 	return (result);
500f59c7639Shaad }
501f59c7639Shaad 
502f59c7639Shaad static zfs_ecksum_info_t *
annotate_ecksum(nvlist_t * ereport,zio_bad_cksum_t * info,const uint8_t * goodbuf,const uint8_t * badbuf,size_t size,boolean_t drop_if_identical)503f59c7639Shaad annotate_ecksum(nvlist_t *ereport, zio_bad_cksum_t *info,
504f59c7639Shaad     const uint8_t *goodbuf, const uint8_t *badbuf, size_t size,
505f59c7639Shaad     boolean_t drop_if_identical)
506f59c7639Shaad {
507f59c7639Shaad 	const uint64_t *good = (const uint64_t *)goodbuf;
508f59c7639Shaad 	const uint64_t *bad = (const uint64_t *)badbuf;
509f59c7639Shaad 
510f59c7639Shaad 	uint64_t allset = 0;
511f59c7639Shaad 	uint64_t allcleared = 0;
512f59c7639Shaad 
513f59c7639Shaad 	size_t nui64s = size / sizeof (uint64_t);
514f59c7639Shaad 
515f59c7639Shaad 	size_t inline_size;
516f59c7639Shaad 	int no_inline = 0;
517f59c7639Shaad 	size_t idx;
518f59c7639Shaad 	size_t range;
519f59c7639Shaad 
520f59c7639Shaad 	size_t offset = 0;
521f59c7639Shaad 	ssize_t start = -1;
522f59c7639Shaad 
523f59c7639Shaad 	zfs_ecksum_info_t *eip = kmem_zalloc(sizeof (*eip), KM_SLEEP);
524f59c7639Shaad 
525f59c7639Shaad 	/* don't do any annotation for injected checksum errors */
526f59c7639Shaad 	if (info != NULL && info->zbc_injected)
527f59c7639Shaad 		return (eip);
528f59c7639Shaad 
529f59c7639Shaad 	if (info != NULL && info->zbc_has_cksum) {
530f59c7639Shaad 		fm_payload_set(ereport,
531f59c7639Shaad 		    FM_EREPORT_PAYLOAD_ZFS_CKSUM_EXPECTED,
532f59c7639Shaad 		    DATA_TYPE_UINT64_ARRAY,
533f59c7639Shaad 		    sizeof (info->zbc_expected) / sizeof (uint64_t),
534f59c7639Shaad 		    (uint64_t *)&info->zbc_expected,
535f59c7639Shaad 		    FM_EREPORT_PAYLOAD_ZFS_CKSUM_ACTUAL,
536f59c7639Shaad 		    DATA_TYPE_UINT64_ARRAY,
537f59c7639Shaad 		    sizeof (info->zbc_actual) / sizeof (uint64_t),
538f59c7639Shaad 		    (uint64_t *)&info->zbc_actual,
539f59c7639Shaad 		    FM_EREPORT_PAYLOAD_ZFS_CKSUM_ALGO,
540f59c7639Shaad 		    DATA_TYPE_STRING,
541f59c7639Shaad 		    info->zbc_checksum_name,
542f59c7639Shaad 		    NULL);
543f59c7639Shaad 
544f59c7639Shaad 		if (info->zbc_byteswapped) {
545f59c7639Shaad 			fm_payload_set(ereport,
546f59c7639Shaad 			    FM_EREPORT_PAYLOAD_ZFS_CKSUM_BYTESWAP,
547f59c7639Shaad 			    DATA_TYPE_BOOLEAN, 1,
548f59c7639Shaad 			    NULL);
549f59c7639Shaad 		}
550f59c7639Shaad 	}
551f59c7639Shaad 
552f59c7639Shaad 	if (badbuf == NULL || goodbuf == NULL)
553f59c7639Shaad 		return (eip);
554f59c7639Shaad 
555f59c7639Shaad 	ASSERT3U(nui64s, <=, UINT16_MAX);
556f59c7639Shaad 	ASSERT3U(size, ==, nui64s * sizeof (uint64_t));
557f59c7639Shaad 	ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
558f59c7639Shaad 	ASSERT3U(size, <=, UINT32_MAX);
559f59c7639Shaad 
560f59c7639Shaad 	/* build up the range list by comparing the two buffers. */
561f59c7639Shaad 	for (idx = 0; idx < nui64s; idx++) {
562f59c7639Shaad 		if (good[idx] == bad[idx]) {
563f59c7639Shaad 			if (start == -1)
564f59c7639Shaad 				continue;
565f59c7639Shaad 
566f59c7639Shaad 			add_range(eip, start, idx);
567f59c7639Shaad 			start = -1;
568f59c7639Shaad 		} else {
569f59c7639Shaad 			if (start != -1)
570f59c7639Shaad 				continue;
571f59c7639Shaad 
572f59c7639Shaad 			start = idx;
573f59c7639Shaad 		}
574f59c7639Shaad 	}
575f59c7639Shaad 	if (start != -1)
576f59c7639Shaad 		add_range(eip, start, idx);
577f59c7639Shaad 
578f59c7639Shaad 	/* See if it will fit in our inline buffers */
579f59c7639Shaad 	inline_size = range_total_size(eip);
580f59c7639Shaad 	if (inline_size > ZFM_MAX_INLINE)
581f59c7639Shaad 		no_inline = 1;
582f59c7639Shaad 
583f59c7639Shaad 	/*
584f59c7639Shaad 	 * If there is no change and we want to drop if the buffers are
585f59c7639Shaad 	 * identical, do so.
586f59c7639Shaad 	 */
587f59c7639Shaad 	if (inline_size == 0 && drop_if_identical) {
588f59c7639Shaad 		kmem_free(eip, sizeof (*eip));
589f59c7639Shaad 		return (NULL);
590f59c7639Shaad 	}
591f59c7639Shaad 
592f59c7639Shaad 	/*
593f59c7639Shaad 	 * Now walk through the ranges, filling in the details of the
594f59c7639Shaad 	 * differences.  Also convert our uint64_t-array offsets to byte
595f59c7639Shaad 	 * offsets.
596f59c7639Shaad 	 */
597f59c7639Shaad 	for (range = 0; range < eip->zei_range_count; range++) {
598f59c7639Shaad 		size_t start = eip->zei_ranges[range].zr_start;
599f59c7639Shaad 		size_t end = eip->zei_ranges[range].zr_end;
600f59c7639Shaad 
601f59c7639Shaad 		for (idx = start; idx < end; idx++) {
602f59c7639Shaad 			uint64_t set, cleared;
603f59c7639Shaad 
604f59c7639Shaad 			// bits set in bad, but not in good
605f59c7639Shaad 			set = ((~good[idx]) & bad[idx]);
606f59c7639Shaad 			// bits set in good, but not in bad
607f59c7639Shaad 			cleared = (good[idx] & (~bad[idx]));
608f59c7639Shaad 
609f59c7639Shaad 			allset |= set;
610f59c7639Shaad 			allcleared |= cleared;
611f59c7639Shaad 
612f59c7639Shaad 			if (!no_inline) {
613f59c7639Shaad 				ASSERT3U(offset, <, inline_size);
614f59c7639Shaad 				eip->zei_bits_set[offset] = set;
615f59c7639Shaad 				eip->zei_bits_cleared[offset] = cleared;
616f59c7639Shaad 				offset++;
617f59c7639Shaad 			}
618f59c7639Shaad 
619f59c7639Shaad 			update_histogram(set, eip->zei_histogram_set,
620f59c7639Shaad 			    &eip->zei_range_sets[range]);
621f59c7639Shaad 			update_histogram(cleared, eip->zei_histogram_cleared,
622f59c7639Shaad 			    &eip->zei_range_clears[range]);
623f59c7639Shaad 		}
624f59c7639Shaad 
625f59c7639Shaad 		/* convert to byte offsets */
626f59c7639Shaad 		eip->zei_ranges[range].zr_start	*= sizeof (uint64_t);
627f59c7639Shaad 		eip->zei_ranges[range].zr_end	*= sizeof (uint64_t);
628f59c7639Shaad 	}
629f59c7639Shaad 	eip->zei_allowed_mingap	*= sizeof (uint64_t);
630f59c7639Shaad 	inline_size		*= sizeof (uint64_t);
631f59c7639Shaad 
632f59c7639Shaad 	/* fill in ereport */
633f59c7639Shaad 	fm_payload_set(ereport,
634f59c7639Shaad 	    FM_EREPORT_PAYLOAD_ZFS_BAD_OFFSET_RANGES,
635f59c7639Shaad 	    DATA_TYPE_UINT32_ARRAY, 2 * eip->zei_range_count,
636f59c7639Shaad 	    (uint32_t *)eip->zei_ranges,
637f59c7639Shaad 	    FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_MIN_GAP,
638f59c7639Shaad 	    DATA_TYPE_UINT32, eip->zei_allowed_mingap,
639f59c7639Shaad 	    FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_SETS,
640f59c7639Shaad 	    DATA_TYPE_UINT32_ARRAY, eip->zei_range_count, eip->zei_range_sets,
641f59c7639Shaad 	    FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_CLEARS,
642f59c7639Shaad 	    DATA_TYPE_UINT32_ARRAY, eip->zei_range_count, eip->zei_range_clears,
643f59c7639Shaad 	    NULL);
644f59c7639Shaad 
645f59c7639Shaad 	if (!no_inline) {
646f59c7639Shaad 		fm_payload_set(ereport,
647f59c7639Shaad 		    FM_EREPORT_PAYLOAD_ZFS_BAD_SET_BITS,
648f59c7639Shaad 		    DATA_TYPE_UINT8_ARRAY,
649f59c7639Shaad 		    inline_size, (uint8_t *)eip->zei_bits_set,
650f59c7639Shaad 		    FM_EREPORT_PAYLOAD_ZFS_BAD_CLEARED_BITS,
651f59c7639Shaad 		    DATA_TYPE_UINT8_ARRAY,
652f59c7639Shaad 		    inline_size, (uint8_t *)eip->zei_bits_cleared,
653f59c7639Shaad 		    NULL);
654f59c7639Shaad 	} else {
655f59c7639Shaad 		fm_payload_set(ereport,
656f59c7639Shaad 		    FM_EREPORT_PAYLOAD_ZFS_BAD_SET_HISTOGRAM,
657f59c7639Shaad 		    DATA_TYPE_UINT16_ARRAY,
658f59c7639Shaad 		    NBBY * sizeof (uint64_t), eip->zei_histogram_set,
659f59c7639Shaad 		    FM_EREPORT_PAYLOAD_ZFS_BAD_CLEARED_HISTOGRAM,
660f59c7639Shaad 		    DATA_TYPE_UINT16_ARRAY,
661f59c7639Shaad 		    NBBY * sizeof (uint64_t), eip->zei_histogram_cleared,
662f59c7639Shaad 		    NULL);
663f59c7639Shaad 	}
664f59c7639Shaad 	return (eip);
665f59c7639Shaad }
666f59c7639Shaad #endif
667f59c7639Shaad 
668f59c7639Shaad void
zfs_ereport_post(const char * subclass,spa_t * spa,vdev_t * vd,zio_t * zio,uint64_t stateoroffset,uint64_t size)669f59c7639Shaad zfs_ereport_post(const char *subclass, spa_t *spa, vdev_t *vd, zio_t *zio,
670f59c7639Shaad     uint64_t stateoroffset, uint64_t size)
671f59c7639Shaad {
672f59c7639Shaad #ifdef _KERNEL
673f59c7639Shaad 	nvlist_t *ereport = NULL;
674f59c7639Shaad 	nvlist_t *detector = NULL;
675f59c7639Shaad 
676f59c7639Shaad 	zfs_ereport_start(&ereport, &detector,
677f59c7639Shaad 	    subclass, spa, vd, zio, stateoroffset, size);
678f59c7639Shaad 
679f59c7639Shaad 	if (ereport == NULL)
680f59c7639Shaad 		return;
681f59c7639Shaad 
682c1cb2cd8Shaad 	fm_ereport_post(ereport, EVCH_SLEEP);
683c1cb2cd8Shaad 
684c1cb2cd8Shaad 	fm_nvlist_destroy(ereport, FM_NVA_FREE);
685c1cb2cd8Shaad 	fm_nvlist_destroy(detector, FM_NVA_FREE);
686c1cb2cd8Shaad #endif
687c1cb2cd8Shaad }
688c1cb2cd8Shaad 
689f59c7639Shaad void
zfs_ereport_start_checksum(spa_t * spa,vdev_t * vd,struct zio * zio,uint64_t offset,uint64_t length,void * arg,zio_bad_cksum_t * info)690f59c7639Shaad zfs_ereport_start_checksum(spa_t *spa, vdev_t *vd,
691f59c7639Shaad     struct zio *zio, uint64_t offset, uint64_t length, void *arg,
692f59c7639Shaad     zio_bad_cksum_t *info)
693f59c7639Shaad {
694f59c7639Shaad 	zio_cksum_report_t *report = kmem_zalloc(sizeof (*report), KM_SLEEP);
695f59c7639Shaad 
696f59c7639Shaad 	if (zio->io_vsd != NULL)
697f59c7639Shaad 		zio->io_vsd_ops->vsd_cksum_report(zio, report, arg);
698f59c7639Shaad 	else
699f59c7639Shaad 		zio_vsd_default_cksum_report(zio, report, arg);
700f59c7639Shaad 
701f59c7639Shaad 	/* copy the checksum failure information if it was provided */
702f59c7639Shaad 	if (info != NULL) {
703f59c7639Shaad 		report->zcr_ckinfo = kmem_zalloc(sizeof (*info), KM_SLEEP);
704f59c7639Shaad 		bcopy(info, report->zcr_ckinfo, sizeof (*info));
705f59c7639Shaad 	}
706f59c7639Shaad 
707f59c7639Shaad 	report->zcr_align = 1ULL << vd->vdev_top->vdev_ashift;
708f59c7639Shaad 	report->zcr_length = length;
709f59c7639Shaad 
710f59c7639Shaad #ifdef _KERNEL
711f59c7639Shaad 	zfs_ereport_start(&report->zcr_ereport, &report->zcr_detector,
712f59c7639Shaad 	    FM_EREPORT_ZFS_CHECKSUM, spa, vd, zio, offset, length);
713f59c7639Shaad 
714f59c7639Shaad 	if (report->zcr_ereport == NULL) {
715f59c7639Shaad 		report->zcr_free(report->zcr_cbdata, report->zcr_cbinfo);
716*93f3d2b8Schs 		if (report->zcr_ckinfo != NULL) {
717*93f3d2b8Schs 			kmem_free(report->zcr_ckinfo,
718*93f3d2b8Schs 			    sizeof (*report->zcr_ckinfo));
719*93f3d2b8Schs 		}
720f59c7639Shaad 		kmem_free(report, sizeof (*report));
721f59c7639Shaad 		return;
722f59c7639Shaad 	}
723f59c7639Shaad #endif
724f59c7639Shaad 
725f59c7639Shaad 	mutex_enter(&spa->spa_errlist_lock);
726f59c7639Shaad 	report->zcr_next = zio->io_logical->io_cksum_report;
727f59c7639Shaad 	zio->io_logical->io_cksum_report = report;
728f59c7639Shaad 	mutex_exit(&spa->spa_errlist_lock);
729f59c7639Shaad }
730f59c7639Shaad 
731f59c7639Shaad void
zfs_ereport_finish_checksum(zio_cksum_report_t * report,const void * good_data,const void * bad_data,boolean_t drop_if_identical)732f59c7639Shaad zfs_ereport_finish_checksum(zio_cksum_report_t *report,
733f59c7639Shaad     const void *good_data, const void *bad_data, boolean_t drop_if_identical)
734f59c7639Shaad {
735f59c7639Shaad #ifdef _KERNEL
736f59c7639Shaad 	zfs_ecksum_info_t *info = NULL;
737f59c7639Shaad 	info = annotate_ecksum(report->zcr_ereport, report->zcr_ckinfo,
738f59c7639Shaad 	    good_data, bad_data, report->zcr_length, drop_if_identical);
739f59c7639Shaad 
740f59c7639Shaad 	if (info != NULL)
741f59c7639Shaad 		fm_ereport_post(report->zcr_ereport, EVCH_SLEEP);
742f59c7639Shaad 
743f59c7639Shaad 	fm_nvlist_destroy(report->zcr_ereport, FM_NVA_FREE);
744f59c7639Shaad 	fm_nvlist_destroy(report->zcr_detector, FM_NVA_FREE);
745f59c7639Shaad 	report->zcr_ereport = report->zcr_detector = NULL;
746f59c7639Shaad 
747f59c7639Shaad 	if (info != NULL)
748f59c7639Shaad 		kmem_free(info, sizeof (*info));
749f59c7639Shaad #endif
750f59c7639Shaad }
751f59c7639Shaad 
752f59c7639Shaad void
zfs_ereport_free_checksum(zio_cksum_report_t * rpt)753f59c7639Shaad zfs_ereport_free_checksum(zio_cksum_report_t *rpt)
754f59c7639Shaad {
755f59c7639Shaad #ifdef _KERNEL
756f59c7639Shaad 	if (rpt->zcr_ereport != NULL) {
757f59c7639Shaad 		fm_nvlist_destroy(rpt->zcr_ereport,
758f59c7639Shaad 		    FM_NVA_FREE);
759f59c7639Shaad 		fm_nvlist_destroy(rpt->zcr_detector,
760f59c7639Shaad 		    FM_NVA_FREE);
761f59c7639Shaad 	}
762f59c7639Shaad #endif
763f59c7639Shaad 	rpt->zcr_free(rpt->zcr_cbdata, rpt->zcr_cbinfo);
764f59c7639Shaad 
765f59c7639Shaad 	if (rpt->zcr_ckinfo != NULL)
766f59c7639Shaad 		kmem_free(rpt->zcr_ckinfo, sizeof (*rpt->zcr_ckinfo));
767f59c7639Shaad 
768f59c7639Shaad 	kmem_free(rpt, sizeof (*rpt));
769f59c7639Shaad }
770f59c7639Shaad 
771f59c7639Shaad void
zfs_ereport_send_interim_checksum(zio_cksum_report_t * report)772f59c7639Shaad zfs_ereport_send_interim_checksum(zio_cksum_report_t *report)
773f59c7639Shaad {
774f59c7639Shaad #ifdef _KERNEL
775f59c7639Shaad 	fm_ereport_post(report->zcr_ereport, EVCH_SLEEP);
776f59c7639Shaad #endif
777f59c7639Shaad }
778f59c7639Shaad 
779f59c7639Shaad void
zfs_ereport_post_checksum(spa_t * spa,vdev_t * vd,struct zio * zio,uint64_t offset,uint64_t length,const void * good_data,const void * bad_data,zio_bad_cksum_t * zbc)780f59c7639Shaad zfs_ereport_post_checksum(spa_t *spa, vdev_t *vd,
781f59c7639Shaad     struct zio *zio, uint64_t offset, uint64_t length,
782f59c7639Shaad     const void *good_data, const void *bad_data, zio_bad_cksum_t *zbc)
783f59c7639Shaad {
784f59c7639Shaad #ifdef _KERNEL
785f59c7639Shaad 	nvlist_t *ereport = NULL;
786f59c7639Shaad 	nvlist_t *detector = NULL;
787f59c7639Shaad 	zfs_ecksum_info_t *info;
788f59c7639Shaad 
789f59c7639Shaad 	zfs_ereport_start(&ereport, &detector,
790f59c7639Shaad 	    FM_EREPORT_ZFS_CHECKSUM, spa, vd, zio, offset, length);
791f59c7639Shaad 
792f59c7639Shaad 	if (ereport == NULL)
793f59c7639Shaad 		return;
794f59c7639Shaad 
795f59c7639Shaad 	info = annotate_ecksum(ereport, zbc, good_data, bad_data, length,
796f59c7639Shaad 	    B_FALSE);
797f59c7639Shaad 
798f59c7639Shaad 	if (info != NULL)
799f59c7639Shaad 		fm_ereport_post(ereport, EVCH_SLEEP);
800f59c7639Shaad 
801f59c7639Shaad 	fm_nvlist_destroy(ereport, FM_NVA_FREE);
802f59c7639Shaad 	fm_nvlist_destroy(detector, FM_NVA_FREE);
803f59c7639Shaad 
804f59c7639Shaad 	if (info != NULL)
805f59c7639Shaad 		kmem_free(info, sizeof (*info));
806f59c7639Shaad #endif
807f59c7639Shaad }
808f59c7639Shaad 
809c1cb2cd8Shaad static void
zfs_post_common(spa_t * spa,vdev_t * vd,const char * name)810c1cb2cd8Shaad zfs_post_common(spa_t *spa, vdev_t *vd, const char *name)
811c1cb2cd8Shaad {
812c1cb2cd8Shaad #ifdef _KERNEL
813c1cb2cd8Shaad 	nvlist_t *resource;
814c1cb2cd8Shaad 	char class[64];
815c1cb2cd8Shaad 
816f59c7639Shaad 	if (spa_load_state(spa) == SPA_LOAD_TRYIMPORT)
817f59c7639Shaad 		return;
818f59c7639Shaad 
819c1cb2cd8Shaad 	if ((resource = fm_nvlist_create(NULL)) == NULL)
820c1cb2cd8Shaad 		return;
821c1cb2cd8Shaad 
822c1cb2cd8Shaad 	(void) snprintf(class, sizeof (class), "%s.%s.%s", FM_RSRC_RESOURCE,
823c1cb2cd8Shaad 	    ZFS_ERROR_CLASS, name);
824c1cb2cd8Shaad 	VERIFY(nvlist_add_uint8(resource, FM_VERSION, FM_RSRC_VERSION) == 0);
825c1cb2cd8Shaad 	VERIFY(nvlist_add_string(resource, FM_CLASS, class) == 0);
826c1cb2cd8Shaad 	VERIFY(nvlist_add_uint64(resource,
827c1cb2cd8Shaad 	    FM_EREPORT_PAYLOAD_ZFS_POOL_GUID, spa_guid(spa)) == 0);
828c1cb2cd8Shaad 	if (vd)
829c1cb2cd8Shaad 		VERIFY(nvlist_add_uint64(resource,
830c1cb2cd8Shaad 		    FM_EREPORT_PAYLOAD_ZFS_VDEV_GUID, vd->vdev_guid) == 0);
831c1cb2cd8Shaad 
832c1cb2cd8Shaad 	fm_ereport_post(resource, EVCH_SLEEP);
833c1cb2cd8Shaad 
834c1cb2cd8Shaad 	fm_nvlist_destroy(resource, FM_NVA_FREE);
835c1cb2cd8Shaad #endif
836c1cb2cd8Shaad }
837c1cb2cd8Shaad 
838c1cb2cd8Shaad /*
839c1cb2cd8Shaad  * The 'resource.fs.zfs.removed' event is an internal signal that the given vdev
840c1cb2cd8Shaad  * has been removed from the system.  This will cause the DE to ignore any
841c1cb2cd8Shaad  * recent I/O errors, inferring that they are due to the asynchronous device
842c1cb2cd8Shaad  * removal.
843c1cb2cd8Shaad  */
844c1cb2cd8Shaad void
zfs_post_remove(spa_t * spa,vdev_t * vd)845c1cb2cd8Shaad zfs_post_remove(spa_t *spa, vdev_t *vd)
846c1cb2cd8Shaad {
847c1cb2cd8Shaad 	zfs_post_common(spa, vd, FM_RESOURCE_REMOVED);
848c1cb2cd8Shaad }
849c1cb2cd8Shaad 
850c1cb2cd8Shaad /*
851c1cb2cd8Shaad  * The 'resource.fs.zfs.autoreplace' event is an internal signal that the pool
852c1cb2cd8Shaad  * has the 'autoreplace' property set, and therefore any broken vdevs will be
853c1cb2cd8Shaad  * handled by higher level logic, and no vdev fault should be generated.
854c1cb2cd8Shaad  */
855c1cb2cd8Shaad void
zfs_post_autoreplace(spa_t * spa,vdev_t * vd)856c1cb2cd8Shaad zfs_post_autoreplace(spa_t *spa, vdev_t *vd)
857c1cb2cd8Shaad {
858c1cb2cd8Shaad 	zfs_post_common(spa, vd, FM_RESOURCE_AUTOREPLACE);
859c1cb2cd8Shaad }
860f59c7639Shaad 
861f59c7639Shaad /*
862f59c7639Shaad  * The 'resource.fs.zfs.statechange' event is an internal signal that the
863f59c7639Shaad  * given vdev has transitioned its state to DEGRADED or HEALTHY.  This will
864f59c7639Shaad  * cause the retire agent to repair any outstanding fault management cases
865f59c7639Shaad  * open because the device was not found (fault.fs.zfs.device).
866f59c7639Shaad  */
867f59c7639Shaad void
zfs_post_state_change(spa_t * spa,vdev_t * vd)868f59c7639Shaad zfs_post_state_change(spa_t *spa, vdev_t *vd)
869f59c7639Shaad {
870f59c7639Shaad 	zfs_post_common(spa, vd, FM_RESOURCE_STATECHANGE);
871f59c7639Shaad }
872