xref: /illumos-gate/usr/src/uts/common/fs/zfs/zfs_fm.c (revision 9b088140)
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 
26cd0837ccSGeorge Wilson /*
27cd0837ccSGeorge Wilson  * Copyright (c) 2012 by Delphix. All rights reserved.
28cd0837ccSGeorge Wilson  */
29cd0837ccSGeorge 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  *
475711d393Sloli  *	EREPORT			POOL	VDEV	IO
485711d393Sloli  *	block			X	X	X
495711d393Sloli  *	data			X		X
505711d393Sloli  *	device			X	X
515711d393Sloli  *	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  *      +---------------+
635711d393Sloli  *	| Aggregate I/O |	No associated logical data or device
645711d393Sloli  *	+---------------+
65ea8dc4b6Seschrock  *              |
66ea8dc4b6Seschrock  *              V
675711d393Sloli  *	+---------------+	Reads associated with a piece of logical data.
685711d393Sloli  *	|   Read I/O    |	This includes reads on behalf of RAID-Z,
695711d393Sloli  *	+---------------+       mirrors, gang blocks, retries, etc.
70ea8dc4b6Seschrock  *              |
71ea8dc4b6Seschrock  *              V
725711d393Sloli  *	+---------------+	Reads associated with a particular device, but
735711d393Sloli  *	| Physical I/O  |	no logical data.  Issued as part of vdev caching
745711d393Sloli  *	+---------------+	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
105dd50e0ccSTony Hutter 
106dd50e0ccSTony Hutter /*
107dd50e0ccSTony Hutter  * Return B_TRUE if the event actually posted, B_FALSE if not.
108dd50e0ccSTony Hutter  */
109dd50e0ccSTony Hutter static boolean_t
zfs_ereport_start(nvlist_t ** ereport_out,nvlist_t ** detector_out,const char * subclass,spa_t * spa,vdev_t * vd,const zbookmark_phys_t * zb,zio_t * zio,uint64_t stateoroffset,uint64_t size)11022fe2c88SJonathan Adams zfs_ereport_start(nvlist_t **ereport_out, nvlist_t **detector_out,
111eb633035STom Caputi     const char *subclass, spa_t *spa, vdev_t *vd, const zbookmark_phys_t *zb,
112eb633035STom Caputi     zio_t *zio, uint64_t stateoroffset, uint64_t size)
113ea8dc4b6Seschrock {
114ea8dc4b6Seschrock 	nvlist_t *ereport, *detector;
11522fe2c88SJonathan Adams 
116ea8dc4b6Seschrock 	uint64_t ena;
117ea8dc4b6Seschrock 	char class[64];
118ea8dc4b6Seschrock 
119dd50e0ccSTony Hutter 	if (!zfs_ereport_is_valid(subclass, spa, vd, zio))
120dd50e0ccSTony Hutter 		return (B_FALSE);
1211d713200SEric Schrock 
122ea8dc4b6Seschrock 	if ((ereport = fm_nvlist_create(NULL)) == NULL)
123dd50e0ccSTony Hutter 		return (B_FALSE);
124ea8dc4b6Seschrock 
125ea8dc4b6Seschrock 	if ((detector = fm_nvlist_create(NULL)) == NULL) {
126ea8dc4b6Seschrock 		fm_nvlist_destroy(ereport, FM_NVA_FREE);
127dd50e0ccSTony Hutter 		return (B_FALSE);
128ea8dc4b6Seschrock 	}
129ea8dc4b6Seschrock 
130ea8dc4b6Seschrock 	/*
131ea8dc4b6Seschrock 	 * Serialize ereport generation
132ea8dc4b6Seschrock 	 */
133ea8dc4b6Seschrock 	mutex_enter(&spa->spa_errlist_lock);
134ea8dc4b6Seschrock 
135ea8dc4b6Seschrock 	/*
136ea8dc4b6Seschrock 	 * Determine the ENA to use for this event.  If we are in a loading
137ea8dc4b6Seschrock 	 * state, use a SPA-wide ENA.  Otherwise, if we are in an I/O state, use
138ea8dc4b6Seschrock 	 * a root zio-wide ENA.  Otherwise, simply use a unique ENA.
139ea8dc4b6Seschrock 	 */
140b16da2e2SGeorge Wilson 	if (spa_load_state(spa) != SPA_LOAD_NONE) {
141ea8dc4b6Seschrock 		if (spa->spa_ena == 0)
142ea8dc4b6Seschrock 			spa->spa_ena = fm_ena_generate(0, FM_ENA_FMT1);
143ea8dc4b6Seschrock 		ena = spa->spa_ena;
144ea8dc4b6Seschrock 	} else if (zio != NULL && zio->io_logical != NULL) {
145ea8dc4b6Seschrock 		if (zio->io_logical->io_ena == 0)
146ea8dc4b6Seschrock 			zio->io_logical->io_ena =
147ea8dc4b6Seschrock 			    fm_ena_generate(0, FM_ENA_FMT1);
148ea8dc4b6Seschrock 		ena = zio->io_logical->io_ena;
149ea8dc4b6Seschrock 	} else {
150ea8dc4b6Seschrock 		ena = fm_ena_generate(0, FM_ENA_FMT1);
151ea8dc4b6Seschrock 	}
152ea8dc4b6Seschrock 
153ea8dc4b6Seschrock 	/*
154ea8dc4b6Seschrock 	 * Construct the full class, detector, and other standard FMA fields.
155ea8dc4b6Seschrock 	 */
156ea8dc4b6Seschrock 	(void) snprintf(class, sizeof (class), "%s.%s",
157ea8dc4b6Seschrock 	    ZFS_ERROR_CLASS, subclass);
158ea8dc4b6Seschrock 
159ea8dc4b6Seschrock 	fm_fmri_zfs_set(detector, FM_ZFS_SCHEME_VERSION, spa_guid(spa),
160ea8dc4b6Seschrock 	    vd != NULL ? vd->vdev_guid : 0);
161ea8dc4b6Seschrock 
162ea8dc4b6Seschrock 	fm_ereport_set(ereport, FM_EREPORT_VERSION, class, ena, detector, NULL);
163ea8dc4b6Seschrock 
164ea8dc4b6Seschrock 	/*
165ea8dc4b6Seschrock 	 * Construct the per-ereport payload, depending on which parameters are
166ea8dc4b6Seschrock 	 * passed in.
167ea8dc4b6Seschrock 	 */
168ea8dc4b6Seschrock 
169ea8dc4b6Seschrock 	/*
170ea8dc4b6Seschrock 	 * Generic payload members common to all ereports.
171ea8dc4b6Seschrock 	 */
172ea8dc4b6Seschrock 	fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_POOL,
173e14bb325SJeff Bonwick 	    DATA_TYPE_STRING, spa_name(spa), FM_EREPORT_PAYLOAD_ZFS_POOL_GUID,
174ea8dc4b6Seschrock 	    DATA_TYPE_UINT64, spa_guid(spa),
175ea8dc4b6Seschrock 	    FM_EREPORT_PAYLOAD_ZFS_POOL_CONTEXT, DATA_TYPE_INT32,
176b16da2e2SGeorge Wilson 	    spa_load_state(spa), NULL);
177ea8dc4b6Seschrock 
17832b87932Sek 	if (spa != NULL) {
17932b87932Sek 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_POOL_FAILMODE,
18032b87932Sek 		    DATA_TYPE_STRING,
18132b87932Sek 		    spa_get_failmode(spa) == ZIO_FAILURE_MODE_WAIT ?
18232b87932Sek 		    FM_EREPORT_FAILMODE_WAIT :
18332b87932Sek 		    spa_get_failmode(spa) == ZIO_FAILURE_MODE_CONTINUE ?
18432b87932Sek 		    FM_EREPORT_FAILMODE_CONTINUE : FM_EREPORT_FAILMODE_PANIC,
18532b87932Sek 		    NULL);
18632b87932Sek 	}
18732b87932Sek 
188ea8dc4b6Seschrock 	if (vd != NULL) {
189ea8dc4b6Seschrock 		vdev_t *pvd = vd->vdev_parent;
190ea8dc4b6Seschrock 
191ea8dc4b6Seschrock 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_VDEV_GUID,
192ea8dc4b6Seschrock 		    DATA_TYPE_UINT64, vd->vdev_guid,
193ea8dc4b6Seschrock 		    FM_EREPORT_PAYLOAD_ZFS_VDEV_TYPE,
194ea8dc4b6Seschrock 		    DATA_TYPE_STRING, vd->vdev_ops->vdev_op_type, NULL);
1956809eb4eSEric Schrock 		if (vd->vdev_path != NULL)
196ea8dc4b6Seschrock 			fm_payload_set(ereport,
197ea8dc4b6Seschrock 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_PATH,
198ea8dc4b6Seschrock 			    DATA_TYPE_STRING, vd->vdev_path, NULL);
1996809eb4eSEric Schrock 		if (vd->vdev_devid != NULL)
200ea8dc4b6Seschrock 			fm_payload_set(ereport,
201ea8dc4b6Seschrock 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_DEVID,
202ea8dc4b6Seschrock 			    DATA_TYPE_STRING, vd->vdev_devid, NULL);
2036809eb4eSEric Schrock 		if (vd->vdev_fru != NULL)
2046809eb4eSEric Schrock 			fm_payload_set(ereport,
2056809eb4eSEric Schrock 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_FRU,
2066809eb4eSEric Schrock 			    DATA_TYPE_STRING, vd->vdev_fru, NULL);
2075711d393Sloli 		if (vd->vdev_ashift)
2085711d393Sloli 			fm_payload_set(ereport,
2095711d393Sloli 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_ASHIFT,
2105711d393Sloli 			    DATA_TYPE_UINT64, vd->vdev_ashift, NULL);
211ea8dc4b6Seschrock 
212ea8dc4b6Seschrock 		if (pvd != NULL) {
213ea8dc4b6Seschrock 			fm_payload_set(ereport,
214ea8dc4b6Seschrock 			    FM_EREPORT_PAYLOAD_ZFS_PARENT_GUID,
215ea8dc4b6Seschrock 			    DATA_TYPE_UINT64, pvd->vdev_guid,
216ea8dc4b6Seschrock 			    FM_EREPORT_PAYLOAD_ZFS_PARENT_TYPE,
217ea8dc4b6Seschrock 			    DATA_TYPE_STRING, pvd->vdev_ops->vdev_op_type,
218ea8dc4b6Seschrock 			    NULL);
219ea8dc4b6Seschrock 			if (pvd->vdev_path)
220ea8dc4b6Seschrock 				fm_payload_set(ereport,
221ea8dc4b6Seschrock 				    FM_EREPORT_PAYLOAD_ZFS_PARENT_PATH,
222c25056deSgw 				    DATA_TYPE_STRING, pvd->vdev_path, NULL);
223ea8dc4b6Seschrock 			if (pvd->vdev_devid)
224ea8dc4b6Seschrock 				fm_payload_set(ereport,
225ea8dc4b6Seschrock 				    FM_EREPORT_PAYLOAD_ZFS_PARENT_DEVID,
226ea8dc4b6Seschrock 				    DATA_TYPE_STRING, pvd->vdev_devid, NULL);
227ea8dc4b6Seschrock 		}
228ea8dc4b6Seschrock 	}
229ea8dc4b6Seschrock 
230ea8dc4b6Seschrock 	if (zio != NULL) {
231ea8dc4b6Seschrock 		/*
232ea8dc4b6Seschrock 		 * Payload common to all I/Os.
233ea8dc4b6Seschrock 		 */
234ea8dc4b6Seschrock 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_ERR,
235ea8dc4b6Seschrock 		    DATA_TYPE_INT32, zio->io_error, NULL);
236ea8dc4b6Seschrock 
237ea8dc4b6Seschrock 		/*
238ea8dc4b6Seschrock 		 * If the 'size' parameter is non-zero, it indicates this is a
239ea8dc4b6Seschrock 		 * RAID-Z or other I/O where the physical offset and length are
240ea8dc4b6Seschrock 		 * provided for us, instead of within the zio_t.
241ea8dc4b6Seschrock 		 */
242ea8dc4b6Seschrock 		if (vd != NULL) {
243ea8dc4b6Seschrock 			if (size)
244ea8dc4b6Seschrock 				fm_payload_set(ereport,
245ea8dc4b6Seschrock 				    FM_EREPORT_PAYLOAD_ZFS_ZIO_OFFSET,
246ea8dc4b6Seschrock 				    DATA_TYPE_UINT64, stateoroffset,
247ea8dc4b6Seschrock 				    FM_EREPORT_PAYLOAD_ZFS_ZIO_SIZE,
248c3c6d682Seschrock 				    DATA_TYPE_UINT64, size, NULL);
249ea8dc4b6Seschrock 			else
250ea8dc4b6Seschrock 				fm_payload_set(ereport,
251ea8dc4b6Seschrock 				    FM_EREPORT_PAYLOAD_ZFS_ZIO_OFFSET,
252ea8dc4b6Seschrock 				    DATA_TYPE_UINT64, zio->io_offset,
253ea8dc4b6Seschrock 				    FM_EREPORT_PAYLOAD_ZFS_ZIO_SIZE,
254c3c6d682Seschrock 				    DATA_TYPE_UINT64, zio->io_size, NULL);
255ea8dc4b6Seschrock 		}
256ea8dc4b6Seschrock 	} else if (vd != NULL) {
257ea8dc4b6Seschrock 		/*
258ea8dc4b6Seschrock 		 * If we have a vdev but no zio, this is a device fault, and the
259ea8dc4b6Seschrock 		 * 'stateoroffset' parameter indicates the previous state of the
260ea8dc4b6Seschrock 		 * vdev.
261ea8dc4b6Seschrock 		 */
262ea8dc4b6Seschrock 		fm_payload_set(ereport,
263ea8dc4b6Seschrock 		    FM_EREPORT_PAYLOAD_ZFS_PREV_STATE,
264ea8dc4b6Seschrock 		    DATA_TYPE_UINT64, stateoroffset, NULL);
265ea8dc4b6Seschrock 	}
266468c413aSTim Haley 
267eb633035STom Caputi 	/*
268eb633035STom Caputi 	 * Payload for I/Os with corresponding logical information.
269eb633035STom Caputi 	 */
270dd50e0ccSTony Hutter 	if (zb != NULL && (zio == NULL || zio->io_logical != NULL)) {
271eb633035STom Caputi 		fm_payload_set(ereport,
272eb633035STom Caputi 		    FM_EREPORT_PAYLOAD_ZFS_ZIO_OBJSET,
273eb633035STom Caputi 		    DATA_TYPE_UINT64, zb->zb_objset,
274eb633035STom Caputi 		    FM_EREPORT_PAYLOAD_ZFS_ZIO_OBJECT,
275eb633035STom Caputi 		    DATA_TYPE_UINT64, zb->zb_object,
276eb633035STom Caputi 		    FM_EREPORT_PAYLOAD_ZFS_ZIO_LEVEL,
277eb633035STom Caputi 		    DATA_TYPE_INT64, zb->zb_level,
278eb633035STom Caputi 		    FM_EREPORT_PAYLOAD_ZFS_ZIO_BLKID,
279eb633035STom Caputi 		    DATA_TYPE_UINT64, zb->zb_blkid, NULL);
280dd50e0ccSTony Hutter 	}
281eb633035STom Caputi 
282ea8dc4b6Seschrock 	mutex_exit(&spa->spa_errlist_lock);
283ea8dc4b6Seschrock 
28422fe2c88SJonathan Adams 	*ereport_out = ereport;
28522fe2c88SJonathan Adams 	*detector_out = detector;
286dd50e0ccSTony Hutter 	return (B_TRUE);
28722fe2c88SJonathan Adams }
28822fe2c88SJonathan Adams 
28922fe2c88SJonathan Adams /* if it's <= 128 bytes, save the corruption directly */
29022fe2c88SJonathan Adams #define	ZFM_MAX_INLINE		(128 / sizeof (uint64_t))
29122fe2c88SJonathan Adams 
29222fe2c88SJonathan Adams #define	MAX_RANGES		16
29322fe2c88SJonathan Adams 
29422fe2c88SJonathan Adams typedef struct zfs_ecksum_info {
29522fe2c88SJonathan Adams 	/* histograms of set and cleared bits by bit number in a 64-bit word */
296a6c1eb3cSAndriy Gapon 	uint32_t zei_histogram_set[sizeof (uint64_t) * NBBY];
297a6c1eb3cSAndriy Gapon 	uint32_t zei_histogram_cleared[sizeof (uint64_t) * NBBY];
29822fe2c88SJonathan Adams 
29922fe2c88SJonathan Adams 	/* inline arrays of bits set and cleared. */
30022fe2c88SJonathan Adams 	uint64_t zei_bits_set[ZFM_MAX_INLINE];
30122fe2c88SJonathan Adams 	uint64_t zei_bits_cleared[ZFM_MAX_INLINE];
30222fe2c88SJonathan Adams 
30322fe2c88SJonathan Adams 	/*
30422fe2c88SJonathan Adams 	 * for each range, the number of bits set and cleared.  The Hamming
30522fe2c88SJonathan Adams 	 * distance between the good and bad buffers is the sum of them all.
30622fe2c88SJonathan Adams 	 */
30722fe2c88SJonathan Adams 	uint32_t zei_range_sets[MAX_RANGES];
30822fe2c88SJonathan Adams 	uint32_t zei_range_clears[MAX_RANGES];
30922fe2c88SJonathan Adams 
31022fe2c88SJonathan Adams 	struct zei_ranges {
31122fe2c88SJonathan Adams 		uint32_t	zr_start;
31222fe2c88SJonathan Adams 		uint32_t	zr_end;
31322fe2c88SJonathan Adams 	} zei_ranges[MAX_RANGES];
31422fe2c88SJonathan Adams 
31522fe2c88SJonathan Adams 	size_t	zei_range_count;
31622fe2c88SJonathan Adams 	uint32_t zei_mingap;
31722fe2c88SJonathan Adams 	uint32_t zei_allowed_mingap;
31822fe2c88SJonathan Adams 
31922fe2c88SJonathan Adams } zfs_ecksum_info_t;
32022fe2c88SJonathan Adams 
32122fe2c88SJonathan Adams static void
update_histogram(uint64_t value_arg,uint32_t * hist,uint32_t * count)322a6c1eb3cSAndriy Gapon update_histogram(uint64_t value_arg, uint32_t *hist, uint32_t *count)
32322fe2c88SJonathan Adams {
32422fe2c88SJonathan Adams 	size_t i;
32522fe2c88SJonathan Adams 	size_t bits = 0;
32622fe2c88SJonathan Adams 	uint64_t value = BE_64(value_arg);
32722fe2c88SJonathan Adams 
32822fe2c88SJonathan Adams 	/* We store the bits in big-endian (largest-first) order */
32922fe2c88SJonathan Adams 	for (i = 0; i < 64; i++) {
33022fe2c88SJonathan Adams 		if (value & (1ull << i)) {
33122fe2c88SJonathan Adams 			hist[63 - i]++;
33222fe2c88SJonathan Adams 			++bits;
33322fe2c88SJonathan Adams 		}
33422fe2c88SJonathan Adams 	}
33522fe2c88SJonathan Adams 	/* update the count of bits changed */
33622fe2c88SJonathan Adams 	*count += bits;
33722fe2c88SJonathan Adams }
33822fe2c88SJonathan Adams 
33922fe2c88SJonathan Adams /*
34022fe2c88SJonathan Adams  * We've now filled up the range array, and need to increase "mingap" and
34122fe2c88SJonathan Adams  * shrink the range list accordingly.  zei_mingap is always the smallest
34222fe2c88SJonathan Adams  * distance between array entries, so we set the new_allowed_gap to be
34322fe2c88SJonathan Adams  * one greater than that.  We then go through the list, joining together
34422fe2c88SJonathan Adams  * any ranges which are closer than the new_allowed_gap.
34522fe2c88SJonathan Adams  *
34622fe2c88SJonathan Adams  * By construction, there will be at least one.  We also update zei_mingap
34722fe2c88SJonathan Adams  * to the new smallest gap, to prepare for our next invocation.
34822fe2c88SJonathan Adams  */
34922fe2c88SJonathan Adams static void
shrink_ranges(zfs_ecksum_info_t * eip)35022fe2c88SJonathan Adams shrink_ranges(zfs_ecksum_info_t *eip)
35122fe2c88SJonathan Adams {
35222fe2c88SJonathan Adams 	uint32_t mingap = UINT32_MAX;
35322fe2c88SJonathan Adams 	uint32_t new_allowed_gap = eip->zei_mingap + 1;
35422fe2c88SJonathan Adams 
35522fe2c88SJonathan Adams 	size_t idx, output;
35622fe2c88SJonathan Adams 	size_t max = eip->zei_range_count;
35722fe2c88SJonathan Adams 
35822fe2c88SJonathan Adams 	struct zei_ranges *r = eip->zei_ranges;
35922fe2c88SJonathan Adams 
36022fe2c88SJonathan Adams 	ASSERT3U(eip->zei_range_count, >, 0);
36122fe2c88SJonathan Adams 	ASSERT3U(eip->zei_range_count, <=, MAX_RANGES);
36222fe2c88SJonathan Adams 
36322fe2c88SJonathan Adams 	output = idx = 0;
36422fe2c88SJonathan Adams 	while (idx < max - 1) {
36522fe2c88SJonathan Adams 		uint32_t start = r[idx].zr_start;
36622fe2c88SJonathan Adams 		uint32_t end = r[idx].zr_end;
36722fe2c88SJonathan Adams 
36822fe2c88SJonathan Adams 		while (idx < max - 1) {
36922fe2c88SJonathan Adams 			idx++;
37022fe2c88SJonathan Adams 
37122fe2c88SJonathan Adams 			uint32_t nstart = r[idx].zr_start;
37222fe2c88SJonathan Adams 			uint32_t nend = r[idx].zr_end;
37322fe2c88SJonathan Adams 
37422fe2c88SJonathan Adams 			uint32_t gap = nstart - end;
37522fe2c88SJonathan Adams 			if (gap < new_allowed_gap) {
37622fe2c88SJonathan Adams 				end = nend;
37722fe2c88SJonathan Adams 				continue;
37822fe2c88SJonathan Adams 			}
37922fe2c88SJonathan Adams 			if (gap < mingap)
38022fe2c88SJonathan Adams 				mingap = gap;
38122fe2c88SJonathan Adams 			break;
38222fe2c88SJonathan Adams 		}
38322fe2c88SJonathan Adams 		r[output].zr_start = start;
38422fe2c88SJonathan Adams 		r[output].zr_end = end;
38522fe2c88SJonathan Adams 		output++;
38622fe2c88SJonathan Adams 	}
38722fe2c88SJonathan Adams 	ASSERT3U(output, <, eip->zei_range_count);
38822fe2c88SJonathan Adams 	eip->zei_range_count = output;
38922fe2c88SJonathan Adams 	eip->zei_mingap = mingap;
39022fe2c88SJonathan Adams 	eip->zei_allowed_mingap = new_allowed_gap;
39122fe2c88SJonathan Adams }
39222fe2c88SJonathan Adams 
39322fe2c88SJonathan Adams static void
add_range(zfs_ecksum_info_t * eip,int start,int end)39422fe2c88SJonathan Adams add_range(zfs_ecksum_info_t *eip, int start, int end)
39522fe2c88SJonathan Adams {
39622fe2c88SJonathan Adams 	struct zei_ranges *r = eip->zei_ranges;
39722fe2c88SJonathan Adams 	size_t count = eip->zei_range_count;
39822fe2c88SJonathan Adams 
39922fe2c88SJonathan Adams 	if (count >= MAX_RANGES) {
40022fe2c88SJonathan Adams 		shrink_ranges(eip);
40122fe2c88SJonathan Adams 		count = eip->zei_range_count;
40222fe2c88SJonathan Adams 	}
40322fe2c88SJonathan Adams 	if (count == 0) {
40422fe2c88SJonathan Adams 		eip->zei_mingap = UINT32_MAX;
40522fe2c88SJonathan Adams 		eip->zei_allowed_mingap = 1;
40622fe2c88SJonathan Adams 	} else {
40722fe2c88SJonathan Adams 		int gap = start - r[count - 1].zr_end;
40822fe2c88SJonathan Adams 
40922fe2c88SJonathan Adams 		if (gap < eip->zei_allowed_mingap) {
41022fe2c88SJonathan Adams 			r[count - 1].zr_end = end;
41122fe2c88SJonathan Adams 			return;
41222fe2c88SJonathan Adams 		}
41322fe2c88SJonathan Adams 		if (gap < eip->zei_mingap)
41422fe2c88SJonathan Adams 			eip->zei_mingap = gap;
41522fe2c88SJonathan Adams 	}
41622fe2c88SJonathan Adams 	r[count].zr_start = start;
41722fe2c88SJonathan Adams 	r[count].zr_end = end;
41822fe2c88SJonathan Adams 	eip->zei_range_count++;
41922fe2c88SJonathan Adams }
42022fe2c88SJonathan Adams 
42122fe2c88SJonathan Adams static size_t
range_total_size(zfs_ecksum_info_t * eip)42222fe2c88SJonathan Adams range_total_size(zfs_ecksum_info_t *eip)
42322fe2c88SJonathan Adams {
42422fe2c88SJonathan Adams 	struct zei_ranges *r = eip->zei_ranges;
42522fe2c88SJonathan Adams 	size_t count = eip->zei_range_count;
42622fe2c88SJonathan Adams 	size_t result = 0;
42722fe2c88SJonathan Adams 	size_t idx;
42822fe2c88SJonathan Adams 
42922fe2c88SJonathan Adams 	for (idx = 0; idx < count; idx++)
43022fe2c88SJonathan Adams 		result += (r[idx].zr_end - r[idx].zr_start);
43122fe2c88SJonathan Adams 
43222fe2c88SJonathan Adams 	return (result);
43322fe2c88SJonathan Adams }
43422fe2c88SJonathan Adams 
43522fe2c88SJonathan Adams static zfs_ecksum_info_t *
annotate_ecksum(nvlist_t * ereport,zio_bad_cksum_t * info,const abd_t * goodabd,const abd_t * badabd,size_t size,boolean_t drop_if_identical)43622fe2c88SJonathan Adams annotate_ecksum(nvlist_t *ereport, zio_bad_cksum_t *info,
437eb633035STom Caputi     const abd_t *goodabd, const abd_t *badabd, size_t size,
43822fe2c88SJonathan Adams     boolean_t drop_if_identical)
43922fe2c88SJonathan Adams {
440eb633035STom Caputi 	const uint64_t *good;
441eb633035STom Caputi 	const uint64_t *bad;
44222fe2c88SJonathan Adams 
44322fe2c88SJonathan Adams 	uint64_t allset = 0;
44422fe2c88SJonathan Adams 	uint64_t allcleared = 0;
44522fe2c88SJonathan Adams 
44622fe2c88SJonathan Adams 	size_t nui64s = size / sizeof (uint64_t);
44722fe2c88SJonathan Adams 
44822fe2c88SJonathan Adams 	size_t inline_size;
44922fe2c88SJonathan Adams 	int no_inline = 0;
45022fe2c88SJonathan Adams 	size_t idx;
45122fe2c88SJonathan Adams 	size_t range;
45222fe2c88SJonathan Adams 
45322fe2c88SJonathan Adams 	size_t offset = 0;
45422fe2c88SJonathan Adams 	ssize_t start = -1;
45522fe2c88SJonathan Adams 
45622fe2c88SJonathan Adams 	zfs_ecksum_info_t *eip = kmem_zalloc(sizeof (*eip), KM_SLEEP);
45722fe2c88SJonathan Adams 
45822fe2c88SJonathan Adams 	/* don't do any annotation for injected checksum errors */
45922fe2c88SJonathan Adams 	if (info != NULL && info->zbc_injected)
46022fe2c88SJonathan Adams 		return (eip);
46122fe2c88SJonathan Adams 
46222fe2c88SJonathan Adams 	if (info != NULL && info->zbc_has_cksum) {
46322fe2c88SJonathan Adams 		fm_payload_set(ereport,
46422fe2c88SJonathan Adams 		    FM_EREPORT_PAYLOAD_ZFS_CKSUM_EXPECTED,
46522fe2c88SJonathan Adams 		    DATA_TYPE_UINT64_ARRAY,
46622fe2c88SJonathan Adams 		    sizeof (info->zbc_expected) / sizeof (uint64_t),
46722fe2c88SJonathan Adams 		    (uint64_t *)&info->zbc_expected,
46822fe2c88SJonathan Adams 		    FM_EREPORT_PAYLOAD_ZFS_CKSUM_ACTUAL,
46922fe2c88SJonathan Adams 		    DATA_TYPE_UINT64_ARRAY,
47022fe2c88SJonathan Adams 		    sizeof (info->zbc_actual) / sizeof (uint64_t),
47122fe2c88SJonathan Adams 		    (uint64_t *)&info->zbc_actual,
47222fe2c88SJonathan Adams 		    FM_EREPORT_PAYLOAD_ZFS_CKSUM_ALGO,
47322fe2c88SJonathan Adams 		    DATA_TYPE_STRING,
47422fe2c88SJonathan Adams 		    info->zbc_checksum_name,
47522fe2c88SJonathan Adams 		    NULL);
47622fe2c88SJonathan Adams 
47722fe2c88SJonathan Adams 		if (info->zbc_byteswapped) {
47822fe2c88SJonathan Adams 			fm_payload_set(ereport,
47922fe2c88SJonathan Adams 			    FM_EREPORT_PAYLOAD_ZFS_CKSUM_BYTESWAP,
48022fe2c88SJonathan Adams 			    DATA_TYPE_BOOLEAN, 1,
48122fe2c88SJonathan Adams 			    NULL);
48222fe2c88SJonathan Adams 		}
48322fe2c88SJonathan Adams 	}
48422fe2c88SJonathan Adams 
485eb633035STom Caputi 	if (badabd == NULL || goodabd == NULL)
48622fe2c88SJonathan Adams 		return (eip);
48722fe2c88SJonathan Adams 
488a6c1eb3cSAndriy Gapon 	ASSERT3U(nui64s, <=, UINT32_MAX);
48922fe2c88SJonathan Adams 	ASSERT3U(size, ==, nui64s * sizeof (uint64_t));
49022fe2c88SJonathan Adams 	ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
49122fe2c88SJonathan Adams 	ASSERT3U(size, <=, UINT32_MAX);
49222fe2c88SJonathan Adams 
493eb633035STom Caputi 	good = (const uint64_t *) abd_borrow_buf_copy((abd_t *)goodabd, size);
494eb633035STom Caputi 	bad = (const uint64_t *) abd_borrow_buf_copy((abd_t *)badabd, size);
495eb633035STom Caputi 
49622fe2c88SJonathan Adams 	/* build up the range list by comparing the two buffers. */
49722fe2c88SJonathan Adams 	for (idx = 0; idx < nui64s; idx++) {
49822fe2c88SJonathan Adams 		if (good[idx] == bad[idx]) {
49922fe2c88SJonathan Adams 			if (start == -1)
50022fe2c88SJonathan Adams 				continue;
50122fe2c88SJonathan Adams 
50222fe2c88SJonathan Adams 			add_range(eip, start, idx);
50322fe2c88SJonathan Adams 			start = -1;
50422fe2c88SJonathan Adams 		} else {
50522fe2c88SJonathan Adams 			if (start != -1)
50622fe2c88SJonathan Adams 				continue;
50722fe2c88SJonathan Adams 
50822fe2c88SJonathan Adams 			start = idx;
50922fe2c88SJonathan Adams 		}
51022fe2c88SJonathan Adams 	}
51122fe2c88SJonathan Adams 	if (start != -1)
51222fe2c88SJonathan Adams 		add_range(eip, start, idx);
51322fe2c88SJonathan Adams 
51422fe2c88SJonathan Adams 	/* See if it will fit in our inline buffers */
51522fe2c88SJonathan Adams 	inline_size = range_total_size(eip);
51622fe2c88SJonathan Adams 	if (inline_size > ZFM_MAX_INLINE)
51722fe2c88SJonathan Adams 		no_inline = 1;
51822fe2c88SJonathan Adams 
51922fe2c88SJonathan Adams 	/*
52022fe2c88SJonathan Adams 	 * If there is no change and we want to drop if the buffers are
52122fe2c88SJonathan Adams 	 * identical, do so.
52222fe2c88SJonathan Adams 	 */
52322fe2c88SJonathan Adams 	if (inline_size == 0 && drop_if_identical) {
52422fe2c88SJonathan Adams 		kmem_free(eip, sizeof (*eip));
525eb633035STom Caputi 		abd_return_buf((abd_t *)goodabd, (void *)good, size);
526eb633035STom Caputi 		abd_return_buf((abd_t *)badabd, (void *)bad, size);
52722fe2c88SJonathan Adams 		return (NULL);
52822fe2c88SJonathan Adams 	}
52922fe2c88SJonathan Adams 
53022fe2c88SJonathan Adams 	/*
53122fe2c88SJonathan Adams 	 * Now walk through the ranges, filling in the details of the
53222fe2c88SJonathan Adams 	 * differences.  Also convert our uint64_t-array offsets to byte
53322fe2c88SJonathan Adams 	 * offsets.
53422fe2c88SJonathan Adams 	 */
53522fe2c88SJonathan Adams 	for (range = 0; range < eip->zei_range_count; range++) {
53622fe2c88SJonathan Adams 		size_t start = eip->zei_ranges[range].zr_start;
53722fe2c88SJonathan Adams 		size_t end = eip->zei_ranges[range].zr_end;
53822fe2c88SJonathan Adams 
53922fe2c88SJonathan Adams 		for (idx = start; idx < end; idx++) {
54022fe2c88SJonathan Adams 			uint64_t set, cleared;
54122fe2c88SJonathan Adams 
54222fe2c88SJonathan Adams 			// bits set in bad, but not in good
54322fe2c88SJonathan Adams 			set = ((~good[idx]) & bad[idx]);
54422fe2c88SJonathan Adams 			// bits set in good, but not in bad
54522fe2c88SJonathan Adams 			cleared = (good[idx] & (~bad[idx]));
54622fe2c88SJonathan Adams 
54722fe2c88SJonathan Adams 			allset |= set;
54822fe2c88SJonathan Adams 			allcleared |= cleared;
54922fe2c88SJonathan Adams 
55022fe2c88SJonathan Adams 			if (!no_inline) {
55122fe2c88SJonathan Adams 				ASSERT3U(offset, <, inline_size);
55222fe2c88SJonathan Adams 				eip->zei_bits_set[offset] = set;
55322fe2c88SJonathan Adams 				eip->zei_bits_cleared[offset] = cleared;
55422fe2c88SJonathan Adams 				offset++;
55522fe2c88SJonathan Adams 			}
55622fe2c88SJonathan Adams 
55722fe2c88SJonathan Adams 			update_histogram(set, eip->zei_histogram_set,
55822fe2c88SJonathan Adams 			    &eip->zei_range_sets[range]);
55922fe2c88SJonathan Adams 			update_histogram(cleared, eip->zei_histogram_cleared,
56022fe2c88SJonathan Adams 			    &eip->zei_range_clears[range]);
56122fe2c88SJonathan Adams 		}
56222fe2c88SJonathan Adams 
56322fe2c88SJonathan Adams 		/* convert to byte offsets */
56422fe2c88SJonathan Adams 		eip->zei_ranges[range].zr_start	*= sizeof (uint64_t);
56522fe2c88SJonathan Adams 		eip->zei_ranges[range].zr_end	*= sizeof (uint64_t);
56622fe2c88SJonathan Adams 	}
567eb633035STom Caputi 
568eb633035STom Caputi 	abd_return_buf((abd_t *)goodabd, (void *)good, size);
569eb633035STom Caputi 	abd_return_buf((abd_t *)badabd, (void *)bad, size);
570eb633035STom Caputi 
57122fe2c88SJonathan Adams 	eip->zei_allowed_mingap	*= sizeof (uint64_t);
57222fe2c88SJonathan Adams 	inline_size		*= sizeof (uint64_t);
57322fe2c88SJonathan Adams 
57422fe2c88SJonathan Adams 	/* fill in ereport */
57522fe2c88SJonathan Adams 	fm_payload_set(ereport,
57622fe2c88SJonathan Adams 	    FM_EREPORT_PAYLOAD_ZFS_BAD_OFFSET_RANGES,
57722fe2c88SJonathan Adams 	    DATA_TYPE_UINT32_ARRAY, 2 * eip->zei_range_count,
57822fe2c88SJonathan Adams 	    (uint32_t *)eip->zei_ranges,
57922fe2c88SJonathan Adams 	    FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_MIN_GAP,
58022fe2c88SJonathan Adams 	    DATA_TYPE_UINT32, eip->zei_allowed_mingap,
58122fe2c88SJonathan Adams 	    FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_SETS,
58222fe2c88SJonathan Adams 	    DATA_TYPE_UINT32_ARRAY, eip->zei_range_count, eip->zei_range_sets,
58322fe2c88SJonathan Adams 	    FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_CLEARS,
58422fe2c88SJonathan Adams 	    DATA_TYPE_UINT32_ARRAY, eip->zei_range_count, eip->zei_range_clears,
58522fe2c88SJonathan Adams 	    NULL);
58622fe2c88SJonathan Adams 
58722fe2c88SJonathan Adams 	if (!no_inline) {
58822fe2c88SJonathan Adams 		fm_payload_set(ereport,
58922fe2c88SJonathan Adams 		    FM_EREPORT_PAYLOAD_ZFS_BAD_SET_BITS,
59022fe2c88SJonathan Adams 		    DATA_TYPE_UINT8_ARRAY,
59122fe2c88SJonathan Adams 		    inline_size, (uint8_t *)eip->zei_bits_set,
59222fe2c88SJonathan Adams 		    FM_EREPORT_PAYLOAD_ZFS_BAD_CLEARED_BITS,
59322fe2c88SJonathan Adams 		    DATA_TYPE_UINT8_ARRAY,
59422fe2c88SJonathan Adams 		    inline_size, (uint8_t *)eip->zei_bits_cleared,
59522fe2c88SJonathan Adams 		    NULL);
59622fe2c88SJonathan Adams 	} else {
59722fe2c88SJonathan Adams 		fm_payload_set(ereport,
59822fe2c88SJonathan Adams 		    FM_EREPORT_PAYLOAD_ZFS_BAD_SET_HISTOGRAM,
599a6c1eb3cSAndriy Gapon 		    DATA_TYPE_UINT32_ARRAY,
60022fe2c88SJonathan Adams 		    NBBY * sizeof (uint64_t), eip->zei_histogram_set,
60122fe2c88SJonathan Adams 		    FM_EREPORT_PAYLOAD_ZFS_BAD_CLEARED_HISTOGRAM,
602a6c1eb3cSAndriy Gapon 		    DATA_TYPE_UINT32_ARRAY,
60322fe2c88SJonathan Adams 		    NBBY * sizeof (uint64_t), eip->zei_histogram_cleared,
60422fe2c88SJonathan Adams 		    NULL);
60522fe2c88SJonathan Adams 	}
60622fe2c88SJonathan Adams 	return (eip);
60722fe2c88SJonathan Adams }
60822fe2c88SJonathan Adams #endif
60922fe2c88SJonathan Adams 
610dd50e0ccSTony Hutter /*
611dd50e0ccSTony Hutter  * Make sure our event is still valid for the given zio/vdev/pool.  For example,
612dd50e0ccSTony Hutter  * we don't want to keep logging events for a faulted or missing vdev.
613dd50e0ccSTony Hutter  */
614dd50e0ccSTony Hutter boolean_t
zfs_ereport_is_valid(const char * subclass,spa_t * spa,vdev_t * vd,zio_t * zio)615dd50e0ccSTony Hutter zfs_ereport_is_valid(const char *subclass, spa_t *spa, vdev_t *vd, zio_t *zio)
616dd50e0ccSTony Hutter {
617dd50e0ccSTony Hutter #ifdef _KERNEL
618dd50e0ccSTony Hutter 	/*
619dd50e0ccSTony Hutter 	 * If we are doing a spa_tryimport() or in recovery mode,
620dd50e0ccSTony Hutter 	 * ignore errors.
621dd50e0ccSTony Hutter 	 */
622dd50e0ccSTony Hutter 	if (spa_load_state(spa) == SPA_LOAD_TRYIMPORT ||
623dd50e0ccSTony Hutter 	    spa_load_state(spa) == SPA_LOAD_RECOVER)
624dd50e0ccSTony Hutter 		return (B_FALSE);
625dd50e0ccSTony Hutter 
626dd50e0ccSTony Hutter 	/*
627dd50e0ccSTony Hutter 	 * If we are in the middle of opening a pool, and the previous attempt
628dd50e0ccSTony Hutter 	 * failed, don't bother logging any new ereports - we're just going to
629dd50e0ccSTony Hutter 	 * get the same diagnosis anyway.
630dd50e0ccSTony Hutter 	 */
631dd50e0ccSTony Hutter 	if (spa_load_state(spa) != SPA_LOAD_NONE &&
632dd50e0ccSTony Hutter 	    spa->spa_last_open_failed)
633dd50e0ccSTony Hutter 		return (B_FALSE);
634dd50e0ccSTony Hutter 
635dd50e0ccSTony Hutter 	if (zio != NULL) {
636dd50e0ccSTony Hutter 		/*
637dd50e0ccSTony Hutter 		 * If this is not a read or write zio, ignore the error.  This
638dd50e0ccSTony Hutter 		 * can occur if the DKIOCFLUSHWRITECACHE ioctl fails.
639dd50e0ccSTony Hutter 		 */
640dd50e0ccSTony Hutter 		if (zio->io_type != ZIO_TYPE_READ &&
641dd50e0ccSTony Hutter 		    zio->io_type != ZIO_TYPE_WRITE)
642dd50e0ccSTony Hutter 			return (B_FALSE);
643dd50e0ccSTony Hutter 
644dd50e0ccSTony Hutter 		if (vd != NULL) {
645dd50e0ccSTony Hutter 			/*
646dd50e0ccSTony Hutter 			 * If the vdev has already been marked as failing due
647dd50e0ccSTony Hutter 			 * to a failed probe, then ignore any subsequent I/O
648dd50e0ccSTony Hutter 			 * errors, as the DE will automatically fault the vdev
649dd50e0ccSTony Hutter 			 * on the first such failure.  This also catches cases
650dd50e0ccSTony Hutter 			 * where vdev_remove_wanted is set and the device has
651dd50e0ccSTony Hutter 			 * not yet been asynchronously placed into the REMOVED
652dd50e0ccSTony Hutter 			 * state.
653dd50e0ccSTony Hutter 			 */
654dd50e0ccSTony Hutter 			if (zio->io_vd == vd && !vdev_accessible(vd, zio))
655dd50e0ccSTony Hutter 				return (B_FALSE);
656dd50e0ccSTony Hutter 
657dd50e0ccSTony Hutter 			/*
658dd50e0ccSTony Hutter 			 * Ignore checksum errors for reads from DTL regions of
659dd50e0ccSTony Hutter 			 * leaf vdevs.
660dd50e0ccSTony Hutter 			 */
661dd50e0ccSTony Hutter 			if (zio->io_type == ZIO_TYPE_READ &&
662dd50e0ccSTony Hutter 			    zio->io_error == ECKSUM &&
663dd50e0ccSTony Hutter 			    vd->vdev_ops->vdev_op_leaf &&
664dd50e0ccSTony Hutter 			    vdev_dtl_contains(vd, DTL_MISSING, zio->io_txg, 1))
665dd50e0ccSTony Hutter 				return (B_FALSE);
666dd50e0ccSTony Hutter 		}
667dd50e0ccSTony Hutter 	}
668dd50e0ccSTony Hutter 
669dd50e0ccSTony Hutter 	/*
670dd50e0ccSTony Hutter 	 * For probe failure, we want to avoid posting ereports if we've
671dd50e0ccSTony Hutter 	 * already removed the device in the meantime.
672dd50e0ccSTony Hutter 	 */
673dd50e0ccSTony Hutter 	if (vd != NULL &&
674dd50e0ccSTony Hutter 	    strcmp(subclass, FM_EREPORT_ZFS_PROBE_FAILURE) == 0 &&
675dd50e0ccSTony Hutter 	    (vd->vdev_remove_wanted || vd->vdev_state == VDEV_STATE_REMOVED))
676dd50e0ccSTony Hutter 		return (B_FALSE);
677dd50e0ccSTony Hutter 
678dd50e0ccSTony Hutter 	/* Ignore bogus delay events (like from ioctls or unqueued IOs) */
679dd50e0ccSTony Hutter 	if ((strcmp(subclass, FM_EREPORT_ZFS_DELAY) == 0) &&
680dd50e0ccSTony Hutter 	    (zio != NULL) && (!zio->io_timestamp)) {
681dd50e0ccSTony Hutter 		return (B_FALSE);
682dd50e0ccSTony Hutter 	}
683dd50e0ccSTony Hutter #endif
684dd50e0ccSTony Hutter 	return (B_TRUE);
685dd50e0ccSTony Hutter }
686dd50e0ccSTony Hutter 
687dd50e0ccSTony Hutter /*
688dd50e0ccSTony Hutter  * Return 0 if event was posted, EINVAL if there was a problem posting it or
689dd50e0ccSTony Hutter  * EBUSY if the event was rate limited.
690dd50e0ccSTony Hutter  */
691dd50e0ccSTony Hutter int
zfs_ereport_post(const char * subclass,spa_t * spa,vdev_t * vd,const struct zbookmark_phys * zb,zio_t * zio,uint64_t stateoroffset,uint64_t size)692eb633035STom Caputi zfs_ereport_post(const char *subclass, spa_t *spa, vdev_t *vd,
693eb633035STom Caputi     const struct zbookmark_phys *zb, zio_t *zio, uint64_t stateoroffset,
694eb633035STom Caputi     uint64_t size)
69522fe2c88SJonathan Adams {
696dd50e0ccSTony Hutter 	int rc = 0;
69722fe2c88SJonathan Adams #ifdef _KERNEL
69822fe2c88SJonathan Adams 	nvlist_t *ereport = NULL;
69922fe2c88SJonathan Adams 	nvlist_t *detector = NULL;
70022fe2c88SJonathan Adams 
701dd50e0ccSTony Hutter 	if (!zfs_ereport_start(&ereport, &detector, subclass, spa, vd,
702dd50e0ccSTony Hutter 	    zb, zio, stateoroffset, size))
703dd50e0ccSTony Hutter 		return (SET_ERROR(EINVAL));	/* couldn't post event */
70422fe2c88SJonathan Adams 
70522fe2c88SJonathan Adams 	if (ereport == NULL)
706dd50e0ccSTony Hutter 		return (SET_ERROR(EINVAL));
70722fe2c88SJonathan Adams 
708ea8dc4b6Seschrock 	fm_ereport_post(ereport, EVCH_SLEEP);
709ea8dc4b6Seschrock 
710ea8dc4b6Seschrock 	fm_nvlist_destroy(ereport, FM_NVA_FREE);
711ea8dc4b6Seschrock 	fm_nvlist_destroy(detector, FM_NVA_FREE);
712ea8dc4b6Seschrock #endif
713dd50e0ccSTony Hutter 	return (rc);
714ea8dc4b6Seschrock }
715ea8dc4b6Seschrock 
71622fe2c88SJonathan Adams void
zfs_ereport_start_checksum(spa_t * spa,vdev_t * vd,const zbookmark_phys_t * zb,struct zio * zio,uint64_t offset,uint64_t length,void * arg,zio_bad_cksum_t * info)717eb633035STom Caputi zfs_ereport_start_checksum(spa_t *spa, vdev_t *vd, const zbookmark_phys_t *zb,
71822fe2c88SJonathan Adams     struct zio *zio, uint64_t offset, uint64_t length, void *arg,
71922fe2c88SJonathan Adams     zio_bad_cksum_t *info)
72022fe2c88SJonathan Adams {
72122fe2c88SJonathan Adams 	zio_cksum_report_t *report = kmem_zalloc(sizeof (*report), KM_SLEEP);
72222fe2c88SJonathan Adams 
72322fe2c88SJonathan Adams 	if (zio->io_vsd != NULL)
72422fe2c88SJonathan Adams 		zio->io_vsd_ops->vsd_cksum_report(zio, report, arg);
72522fe2c88SJonathan Adams 	else
72622fe2c88SJonathan Adams 		zio_vsd_default_cksum_report(zio, report, arg);
72722fe2c88SJonathan Adams 
72822fe2c88SJonathan Adams 	/* copy the checksum failure information if it was provided */
72922fe2c88SJonathan Adams 	if (info != NULL) {
73022fe2c88SJonathan Adams 		report->zcr_ckinfo = kmem_zalloc(sizeof (*info), KM_SLEEP);
73122fe2c88SJonathan Adams 		bcopy(info, report->zcr_ckinfo, sizeof (*info));
73222fe2c88SJonathan Adams 	}
73322fe2c88SJonathan Adams 
734b24ab676SJeff Bonwick 	report->zcr_align = 1ULL << vd->vdev_top->vdev_ashift;
73522fe2c88SJonathan Adams 	report->zcr_length = length;
73622fe2c88SJonathan Adams 
73722fe2c88SJonathan Adams #ifdef _KERNEL
738*9b088140SToomas Soome 	(void) zfs_ereport_start(&report->zcr_ereport, &report->zcr_detector,
739eb633035STom Caputi 	    FM_EREPORT_ZFS_CHECKSUM, spa, vd, zb, zio, offset, length);
74022fe2c88SJonathan Adams 
74122fe2c88SJonathan Adams 	if (report->zcr_ereport == NULL) {
74222fe2c88SJonathan Adams 		report->zcr_free(report->zcr_cbdata, report->zcr_cbinfo);
743cd0837ccSGeorge Wilson 		if (report->zcr_ckinfo != NULL) {
744cd0837ccSGeorge Wilson 			kmem_free(report->zcr_ckinfo,
745cd0837ccSGeorge Wilson 			    sizeof (*report->zcr_ckinfo));
746cd0837ccSGeorge Wilson 		}
74722fe2c88SJonathan Adams 		kmem_free(report, sizeof (*report));
74822fe2c88SJonathan Adams 		return;
74922fe2c88SJonathan Adams 	}
75022fe2c88SJonathan Adams #endif
75122fe2c88SJonathan Adams 
75222fe2c88SJonathan Adams 	mutex_enter(&spa->spa_errlist_lock);
75322fe2c88SJonathan Adams 	report->zcr_next = zio->io_logical->io_cksum_report;
75422fe2c88SJonathan Adams 	zio->io_logical->io_cksum_report = report;
75522fe2c88SJonathan Adams 	mutex_exit(&spa->spa_errlist_lock);
75622fe2c88SJonathan Adams }
75722fe2c88SJonathan Adams 
75822fe2c88SJonathan Adams void
zfs_ereport_finish_checksum(zio_cksum_report_t * report,const abd_t * good_data,const abd_t * bad_data,boolean_t drop_if_identical)759eb633035STom Caputi zfs_ereport_finish_checksum(zio_cksum_report_t *report, const abd_t *good_data,
760eb633035STom Caputi     const abd_t *bad_data, boolean_t drop_if_identical)
76122fe2c88SJonathan Adams {
76222fe2c88SJonathan Adams #ifdef _KERNEL
76322fe2c88SJonathan Adams 	zfs_ecksum_info_t *info = NULL;
76422fe2c88SJonathan Adams 	info = annotate_ecksum(report->zcr_ereport, report->zcr_ckinfo,
76522fe2c88SJonathan Adams 	    good_data, bad_data, report->zcr_length, drop_if_identical);
76622fe2c88SJonathan Adams 
76722fe2c88SJonathan Adams 	if (info != NULL)
76822fe2c88SJonathan Adams 		fm_ereport_post(report->zcr_ereport, EVCH_SLEEP);
76922fe2c88SJonathan Adams 
77022fe2c88SJonathan Adams 	fm_nvlist_destroy(report->zcr_ereport, FM_NVA_FREE);
77122fe2c88SJonathan Adams 	fm_nvlist_destroy(report->zcr_detector, FM_NVA_FREE);
77222fe2c88SJonathan Adams 	report->zcr_ereport = report->zcr_detector = NULL;
77322fe2c88SJonathan Adams 
77422fe2c88SJonathan Adams 	if (info != NULL)
77522fe2c88SJonathan Adams 		kmem_free(info, sizeof (*info));
77622fe2c88SJonathan Adams #endif
77722fe2c88SJonathan Adams }
77822fe2c88SJonathan Adams 
77922fe2c88SJonathan Adams void
zfs_ereport_free_checksum(zio_cksum_report_t * rpt)78022fe2c88SJonathan Adams zfs_ereport_free_checksum(zio_cksum_report_t *rpt)
78122fe2c88SJonathan Adams {
78222fe2c88SJonathan Adams #ifdef _KERNEL
78322fe2c88SJonathan Adams 	if (rpt->zcr_ereport != NULL) {
78422fe2c88SJonathan Adams 		fm_nvlist_destroy(rpt->zcr_ereport,
78522fe2c88SJonathan Adams 		    FM_NVA_FREE);
78622fe2c88SJonathan Adams 		fm_nvlist_destroy(rpt->zcr_detector,
78722fe2c88SJonathan Adams 		    FM_NVA_FREE);
78822fe2c88SJonathan Adams 	}
78922fe2c88SJonathan Adams #endif
79022fe2c88SJonathan Adams 	rpt->zcr_free(rpt->zcr_cbdata, rpt->zcr_cbinfo);
79122fe2c88SJonathan Adams 
79222fe2c88SJonathan Adams 	if (rpt->zcr_ckinfo != NULL)
79322fe2c88SJonathan Adams 		kmem_free(rpt->zcr_ckinfo, sizeof (*rpt->zcr_ckinfo));
79422fe2c88SJonathan Adams 
79522fe2c88SJonathan Adams 	kmem_free(rpt, sizeof (*rpt));
79622fe2c88SJonathan Adams }
79722fe2c88SJonathan Adams 
79822fe2c88SJonathan Adams void
zfs_ereport_send_interim_checksum(zio_cksum_report_t * report)79922fe2c88SJonathan Adams zfs_ereport_send_interim_checksum(zio_cksum_report_t *report)
80022fe2c88SJonathan Adams {
80122fe2c88SJonathan Adams #ifdef _KERNEL
80222fe2c88SJonathan Adams 	fm_ereport_post(report->zcr_ereport, EVCH_SLEEP);
80322fe2c88SJonathan Adams #endif
80422fe2c88SJonathan Adams }
80522fe2c88SJonathan Adams 
806dd50e0ccSTony Hutter int
zfs_ereport_post_checksum(spa_t * spa,vdev_t * vd,const zbookmark_phys_t * zb,struct zio * zio,uint64_t offset,uint64_t length,const abd_t * good_data,const abd_t * bad_data,zio_bad_cksum_t * zbc)807eb633035STom Caputi zfs_ereport_post_checksum(spa_t *spa, vdev_t *vd, const zbookmark_phys_t *zb,
80822fe2c88SJonathan Adams     struct zio *zio, uint64_t offset, uint64_t length,
809eb633035STom Caputi     const abd_t *good_data, const abd_t *bad_data, zio_bad_cksum_t *zbc)
81022fe2c88SJonathan Adams {
811dd50e0ccSTony Hutter 	int rc = 0;
81222fe2c88SJonathan Adams #ifdef _KERNEL
81322fe2c88SJonathan Adams 	nvlist_t *ereport = NULL;
81422fe2c88SJonathan Adams 	nvlist_t *detector = NULL;
81522fe2c88SJonathan Adams 	zfs_ecksum_info_t *info;
81622fe2c88SJonathan Adams 
817dd50e0ccSTony Hutter 	if (!zfs_ereport_start(&ereport, &detector, FM_EREPORT_ZFS_CHECKSUM,
818dd50e0ccSTony Hutter 	    spa, vd, zb, zio, offset, length) || (ereport == NULL)) {
819dd50e0ccSTony Hutter 		return (SET_ERROR(EINVAL));
820dd50e0ccSTony Hutter 	}
82122fe2c88SJonathan Adams 
82222fe2c88SJonathan Adams 	info = annotate_ecksum(ereport, zbc, good_data, bad_data, length,
82322fe2c88SJonathan Adams 	    B_FALSE);
82422fe2c88SJonathan Adams 
82522fe2c88SJonathan Adams 	if (info != NULL)
82622fe2c88SJonathan Adams 		fm_ereport_post(ereport, EVCH_SLEEP);
82722fe2c88SJonathan Adams 
82822fe2c88SJonathan Adams 	fm_nvlist_destroy(ereport, FM_NVA_FREE);
82922fe2c88SJonathan Adams 	fm_nvlist_destroy(detector, FM_NVA_FREE);
83022fe2c88SJonathan Adams 
83122fe2c88SJonathan Adams 	if (info != NULL)
83222fe2c88SJonathan Adams 		kmem_free(info, sizeof (*info));
83322fe2c88SJonathan Adams #endif
834dd50e0ccSTony Hutter 	return (rc);
83522fe2c88SJonathan Adams }
83622fe2c88SJonathan Adams 
8373d7072f8Seschrock static void
zfs_post_common(spa_t * spa,vdev_t * vd,const char * name)8383d7072f8Seschrock zfs_post_common(spa_t *spa, vdev_t *vd, const char *name)
839ea8dc4b6Seschrock {
840ea8dc4b6Seschrock #ifdef _KERNEL
841ea8dc4b6Seschrock 	nvlist_t *resource;
842ea8dc4b6Seschrock 	char class[64];
843ea8dc4b6Seschrock 
844b16da2e2SGeorge Wilson 	if (spa_load_state(spa) == SPA_LOAD_TRYIMPORT)
8451d713200SEric Schrock 		return;
8461d713200SEric Schrock 
847ea8dc4b6Seschrock 	if ((resource = fm_nvlist_create(NULL)) == NULL)
848ea8dc4b6Seschrock 		return;
849ea8dc4b6Seschrock 
850ea8dc4b6Seschrock 	(void) snprintf(class, sizeof (class), "%s.%s.%s", FM_RSRC_RESOURCE,
8513d7072f8Seschrock 	    ZFS_ERROR_CLASS, name);
852ea8dc4b6Seschrock 	VERIFY(nvlist_add_uint8(resource, FM_VERSION, FM_RSRC_VERSION) == 0);
853ea8dc4b6Seschrock 	VERIFY(nvlist_add_string(resource, FM_CLASS, class) == 0);
854ea8dc4b6Seschrock 	VERIFY(nvlist_add_uint64(resource,
855ea8dc4b6Seschrock 	    FM_EREPORT_PAYLOAD_ZFS_POOL_GUID, spa_guid(spa)) == 0);
856ea8dc4b6Seschrock 	if (vd)
857ea8dc4b6Seschrock 		VERIFY(nvlist_add_uint64(resource,
858ea8dc4b6Seschrock 		    FM_EREPORT_PAYLOAD_ZFS_VDEV_GUID, vd->vdev_guid) == 0);
859ea8dc4b6Seschrock 
860ea8dc4b6Seschrock 	fm_ereport_post(resource, EVCH_SLEEP);
861ea8dc4b6Seschrock 
862ea8dc4b6Seschrock 	fm_nvlist_destroy(resource, FM_NVA_FREE);
863ea8dc4b6Seschrock #endif
864ea8dc4b6Seschrock }
8653d7072f8Seschrock 
8663d7072f8Seschrock /*
8673d7072f8Seschrock  * The 'resource.fs.zfs.removed' event is an internal signal that the given vdev
8683d7072f8Seschrock  * has been removed from the system.  This will cause the DE to ignore any
8693d7072f8Seschrock  * recent I/O errors, inferring that they are due to the asynchronous device
8703d7072f8Seschrock  * removal.
8713d7072f8Seschrock  */
8723d7072f8Seschrock void
zfs_post_remove(spa_t * spa,vdev_t * vd)8733d7072f8Seschrock zfs_post_remove(spa_t *spa, vdev_t *vd)
8743d7072f8Seschrock {
8753d7072f8Seschrock 	zfs_post_common(spa, vd, FM_RESOURCE_REMOVED);
8763d7072f8Seschrock }
8773d7072f8Seschrock 
8783d7072f8Seschrock /*
8793d7072f8Seschrock  * The 'resource.fs.zfs.autoreplace' event is an internal signal that the pool
8803d7072f8Seschrock  * has the 'autoreplace' property set, and therefore any broken vdevs will be
8813d7072f8Seschrock  * handled by higher level logic, and no vdev fault should be generated.
8823d7072f8Seschrock  */
8833d7072f8Seschrock void
zfs_post_autoreplace(spa_t * spa,vdev_t * vd)8843d7072f8Seschrock zfs_post_autoreplace(spa_t *spa, vdev_t *vd)
8853d7072f8Seschrock {
8863d7072f8Seschrock 	zfs_post_common(spa, vd, FM_RESOURCE_AUTOREPLACE);
8873d7072f8Seschrock }
888069f55e2SEric Schrock 
889069f55e2SEric Schrock /*
890069f55e2SEric Schrock  * The 'resource.fs.zfs.statechange' event is an internal signal that the
891069f55e2SEric Schrock  * given vdev has transitioned its state to DEGRADED or HEALTHY.  This will
892069f55e2SEric Schrock  * cause the retire agent to repair any outstanding fault management cases
893069f55e2SEric Schrock  * open because the device was not found (fault.fs.zfs.device).
894069f55e2SEric Schrock  */
895069f55e2SEric Schrock void
zfs_post_state_change(spa_t * spa,vdev_t * vd)896069f55e2SEric Schrock zfs_post_state_change(spa_t *spa, vdev_t *vd)
897069f55e2SEric Schrock {
898069f55e2SEric Schrock 	zfs_post_common(spa, vd, FM_RESOURCE_STATECHANGE);
899069f55e2SEric Schrock }
900