xref: /freebsd/sys/contrib/openzfs/module/zfs/zfs_fm.c (revision 22649d4dba730d46244fd2dff4fd174903c8379f)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * This file and its contents are supplied under the terms of the
4  * Common Development and Distribution License ("CDDL"), version 1.0.
5  * You may only use this file in accordance with the terms of version
6  * 1.0 of the CDDL.
7  *
8  * A full copy of the text of the CDDL should have accompanied this
9  * source.  A copy of the CDDL is also available via the Internet at
10  * https://opensource.org/license/CDDL-1.0.
11  */
12 /*
13  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
14  * Use is subject to license terms.
15  */
16 
17 /*
18  * Copyright (c) 2012,2021 by Delphix. All rights reserved.
19  */
20 
21 #include <sys/spa.h>
22 #include <sys/spa_impl.h>
23 #include <sys/vdev.h>
24 #include <sys/vdev_impl.h>
25 #include <sys/zio.h>
26 #include <sys/zio_checksum.h>
27 
28 #include <sys/fm/fs/zfs.h>
29 #include <sys/fm/protocol.h>
30 #include <sys/fm/util.h>
31 #include <sys/sysevent.h>
32 
33 /*
34  * This general routine is responsible for generating all the different ZFS
35  * ereports.  The payload is dependent on the class, and which arguments are
36  * supplied to the function:
37  *
38  * 	EREPORT			POOL	VDEV	IO
39  * 	block			X	X	X
40  * 	data			X		X
41  * 	device			X	X
42  * 	pool			X
43  *
44  * If we are in a loading state, all errors are chained together by the same
45  * SPA-wide ENA (Error Numeric Association).
46  *
47  * For isolated I/O requests, we get the ENA from the zio_t. The propagation
48  * gets very complicated due to RAID-Z, gang blocks, and vdev caching.  We want
49  * to chain together all ereports associated with a logical piece of data.  For
50  * read I/Os, there  are basically three 'types' of I/O, which form a roughly
51  * layered diagram:
52  *
53  * 	+---------------+
54  * 	| Aggregate I/O |	No associated logical data or device
55  * 	+---------------+
56  *              |
57  *              V
58  * 	+---------------+	Reads associated with a piece of logical data.
59  * 	|   Read I/O    |	This includes reads on behalf of RAID-Z,
60  * 	+---------------+       mirrors, gang blocks, retries, etc.
61  *              |
62  *              V
63  * 	+---------------+	Reads associated with a particular device, but
64  * 	| Physical I/O  |	no logical data.  Issued as part of vdev caching
65  * 	+---------------+	and I/O aggregation.
66  *
67  * Note that 'physical I/O' here is not the same terminology as used in the rest
68  * of ZIO.  Typically, 'physical I/O' simply means that there is no attached
69  * blockpointer.  But I/O with no associated block pointer can still be related
70  * to a logical piece of data (i.e. RAID-Z requests).
71  *
72  * Purely physical I/O always have unique ENAs.  They are not related to a
73  * particular piece of logical data, and therefore cannot be chained together.
74  * We still generate an ereport, but the DE doesn't correlate it with any
75  * logical piece of data.  When such an I/O fails, the delegated I/O requests
76  * will issue a retry, which will trigger the 'real' ereport with the correct
77  * ENA.
78  *
79  * We keep track of the ENA for a ZIO chain through the 'io_logical' member.
80  * When a new logical I/O is issued, we set this to point to itself.  Child I/Os
81  * then inherit this pointer, so that when it is first set subsequent failures
82  * will use the same ENA.  For vdev cache fill and queue aggregation I/O,
83  * this pointer is set to NULL, and no ereport will be generated (since it
84  * doesn't actually correspond to any particular device or piece of data,
85  * and the caller will always retry without caching or queueing anyway).
86  *
87  * For checksum errors, we want to include more information about the actual
88  * error which occurs.  Accordingly, we build an ereport when the error is
89  * noticed, but instead of sending it in immediately, we hang it off of the
90  * io_cksum_report field of the logical IO.  When the logical IO completes
91  * (successfully or not), zfs_ereport_finish_checksum() is called with the
92  * good and bad versions of the buffer (if available), and we annotate the
93  * ereport with information about the differences.
94  */
95 
96 #ifdef _KERNEL
97 /*
98  * Duplicate ereport Detection
99  *
100  * Some ereports are retained momentarily for detecting duplicates.  These
101  * are kept in a recent_events_node_t in both a time-ordered list and an AVL
102  * tree of recent unique ereports.
103  *
104  * The lifespan of these recent ereports is bounded (15 mins) and a cleaner
105  * task is used to purge stale entries.
106  */
107 static list_t recent_events_list;
108 static avl_tree_t recent_events_tree;
109 static kmutex_t recent_events_lock;
110 static taskqid_t recent_events_cleaner_tqid;
111 
112 /*
113  * Each node is about 128 bytes so 2,000 would consume 1/4 MiB.
114  *
115  * This setting can be changed dynamically and setting it to zero
116  * disables duplicate detection.
117  */
118 static unsigned int zfs_zevent_retain_max = 2000;
119 
120 /*
121  * The lifespan for a recent ereport entry. The default of 15 minutes is
122  * intended to outlive the zfs diagnosis engine's threshold of 10 errors
123  * over a period of 10 minutes.
124  */
125 static unsigned int zfs_zevent_retain_expire_secs = 900;
126 
127 typedef enum zfs_subclass {
128 	ZSC_IO,
129 	ZSC_DATA,
130 	ZSC_CHECKSUM
131 } zfs_subclass_t;
132 
133 typedef struct {
134 	/* common criteria */
135 	uint64_t	re_pool_guid;
136 	uint64_t	re_vdev_guid;
137 	int		re_io_error;
138 	uint64_t	re_io_size;
139 	uint64_t	re_io_offset;
140 	zfs_subclass_t	re_subclass;
141 	zio_priority_t	re_io_priority;
142 
143 	/* logical zio criteria (optional) */
144 	zbookmark_phys_t re_io_bookmark;
145 
146 	/* internal state */
147 	avl_node_t	re_tree_link;
148 	list_node_t	re_list_link;
149 	uint64_t	re_timestamp;
150 } recent_events_node_t;
151 
152 static int
recent_events_compare(const void * a,const void * b)153 recent_events_compare(const void *a, const void *b)
154 {
155 	const recent_events_node_t *node1 = a;
156 	const recent_events_node_t *node2 = b;
157 	int cmp;
158 
159 	/*
160 	 * The comparison order here is somewhat arbitrary.
161 	 * What's important is that if every criteria matches, then it
162 	 * is a duplicate (i.e. compare returns 0)
163 	 */
164 	if ((cmp = TREE_CMP(node1->re_subclass, node2->re_subclass)) != 0)
165 		return (cmp);
166 	if ((cmp = TREE_CMP(node1->re_pool_guid, node2->re_pool_guid)) != 0)
167 		return (cmp);
168 	if ((cmp = TREE_CMP(node1->re_vdev_guid, node2->re_vdev_guid)) != 0)
169 		return (cmp);
170 	if ((cmp = TREE_CMP(node1->re_io_error, node2->re_io_error)) != 0)
171 		return (cmp);
172 	if ((cmp = TREE_CMP(node1->re_io_priority, node2->re_io_priority)) != 0)
173 		return (cmp);
174 	if ((cmp = TREE_CMP(node1->re_io_size, node2->re_io_size)) != 0)
175 		return (cmp);
176 	if ((cmp = TREE_CMP(node1->re_io_offset, node2->re_io_offset)) != 0)
177 		return (cmp);
178 
179 	const zbookmark_phys_t *zb1 = &node1->re_io_bookmark;
180 	const zbookmark_phys_t *zb2 = &node2->re_io_bookmark;
181 
182 	if ((cmp = TREE_CMP(zb1->zb_objset, zb2->zb_objset)) != 0)
183 		return (cmp);
184 	if ((cmp = TREE_CMP(zb1->zb_object, zb2->zb_object)) != 0)
185 		return (cmp);
186 	if ((cmp = TREE_CMP(zb1->zb_level, zb2->zb_level)) != 0)
187 		return (cmp);
188 	if ((cmp = TREE_CMP(zb1->zb_blkid, zb2->zb_blkid)) != 0)
189 		return (cmp);
190 
191 	return (0);
192 }
193 
194 /*
195  * workaround: vdev properties don't have inheritance
196  */
197 static uint64_t
vdev_prop_get_inherited(vdev_t * vd,vdev_prop_t prop)198 vdev_prop_get_inherited(vdev_t *vd, vdev_prop_t prop)
199 {
200 	uint64_t propdef, propval;
201 
202 	propdef = vdev_prop_default_numeric(prop);
203 	switch (prop) {
204 		case VDEV_PROP_CHECKSUM_N:
205 			propval = vd->vdev_checksum_n;
206 			break;
207 		case VDEV_PROP_CHECKSUM_T:
208 			propval = vd->vdev_checksum_t;
209 			break;
210 		case VDEV_PROP_IO_N:
211 			propval = vd->vdev_io_n;
212 			break;
213 		case VDEV_PROP_IO_T:
214 			propval = vd->vdev_io_t;
215 			break;
216 		case VDEV_PROP_SLOW_IO_EVENTS:
217 			propval = vd->vdev_slow_io_events;
218 			break;
219 		case VDEV_PROP_SLOW_IO_N:
220 			propval = vd->vdev_slow_io_n;
221 			break;
222 		case VDEV_PROP_SLOW_IO_T:
223 			propval = vd->vdev_slow_io_t;
224 			break;
225 		default:
226 			propval = propdef;
227 			break;
228 	}
229 
230 	if (propval != propdef)
231 		return (propval);
232 
233 	if (vd->vdev_parent == NULL)
234 		return (propdef);
235 
236 	return (vdev_prop_get_inherited(vd->vdev_parent, prop));
237 }
238 
239 static void zfs_ereport_schedule_cleaner(void);
240 
241 /*
242  * background task to clean stale recent event nodes.
243  */
244 static void
zfs_ereport_cleaner(void * arg)245 zfs_ereport_cleaner(void *arg)
246 {
247 	recent_events_node_t *entry;
248 	uint64_t now = gethrtime();
249 
250 	/*
251 	 * purge expired entries
252 	 */
253 	mutex_enter(&recent_events_lock);
254 	while ((entry = list_tail(&recent_events_list)) != NULL) {
255 		uint64_t age = NSEC2SEC(now - entry->re_timestamp);
256 		if (age <= zfs_zevent_retain_expire_secs)
257 			break;
258 
259 		/* remove expired node */
260 		avl_remove(&recent_events_tree, entry);
261 		list_remove(&recent_events_list, entry);
262 		kmem_free(entry, sizeof (*entry));
263 	}
264 
265 	/* Restart the cleaner if more entries remain */
266 	recent_events_cleaner_tqid = 0;
267 	if (!list_is_empty(&recent_events_list))
268 		zfs_ereport_schedule_cleaner();
269 
270 	mutex_exit(&recent_events_lock);
271 }
272 
273 static void
zfs_ereport_schedule_cleaner(void)274 zfs_ereport_schedule_cleaner(void)
275 {
276 	ASSERT(MUTEX_HELD(&recent_events_lock));
277 
278 	uint64_t timeout = SEC2NSEC(zfs_zevent_retain_expire_secs + 1);
279 
280 	recent_events_cleaner_tqid = taskq_dispatch_delay(
281 	    system_delay_taskq, zfs_ereport_cleaner, NULL, TQ_SLEEP,
282 	    ddi_get_lbolt() + NSEC_TO_TICK(timeout));
283 }
284 
285 /*
286  * Clear entries for a given vdev or all vdevs in a pool when vdev == NULL
287  */
288 void
zfs_ereport_clear(spa_t * spa,vdev_t * vd)289 zfs_ereport_clear(spa_t *spa, vdev_t *vd)
290 {
291 	uint64_t vdev_guid, pool_guid;
292 
293 	ASSERT(vd != NULL || spa != NULL);
294 	if (vd == NULL) {
295 		vdev_guid = 0;
296 		pool_guid = spa_guid(spa);
297 	} else {
298 		vdev_guid = vd->vdev_guid;
299 		pool_guid = 0;
300 	}
301 
302 	mutex_enter(&recent_events_lock);
303 
304 	recent_events_node_t *next = list_head(&recent_events_list);
305 	while (next != NULL) {
306 		recent_events_node_t *entry = next;
307 
308 		next = list_next(&recent_events_list, next);
309 
310 		if (entry->re_vdev_guid == vdev_guid ||
311 		    entry->re_pool_guid == pool_guid) {
312 			avl_remove(&recent_events_tree, entry);
313 			list_remove(&recent_events_list, entry);
314 			kmem_free(entry, sizeof (*entry));
315 		}
316 	}
317 
318 	mutex_exit(&recent_events_lock);
319 }
320 
321 /*
322  * Check if an ereport would be a duplicate of one recently posted.
323  *
324  * An ereport is considered a duplicate if the set of criteria in
325  * recent_events_node_t all match.
326  *
327  * Only FM_EREPORT_ZFS_IO, FM_EREPORT_ZFS_DATA, and FM_EREPORT_ZFS_CHECKSUM
328  * are candidates for duplicate checking.
329  */
330 static boolean_t
zfs_ereport_is_duplicate(const char * subclass,spa_t * spa,vdev_t * vd,const zbookmark_phys_t * zb,zio_t * zio,uint64_t offset,uint64_t size)331 zfs_ereport_is_duplicate(const char *subclass, spa_t *spa, vdev_t *vd,
332     const zbookmark_phys_t *zb, zio_t *zio, uint64_t offset, uint64_t size)
333 {
334 	recent_events_node_t search = {0}, *entry;
335 
336 	if (vd == NULL || zio == NULL)
337 		return (B_FALSE);
338 
339 	if (zfs_zevent_retain_max == 0)
340 		return (B_FALSE);
341 
342 	if (strcmp(subclass, FM_EREPORT_ZFS_IO) == 0)
343 		search.re_subclass = ZSC_IO;
344 	else if (strcmp(subclass, FM_EREPORT_ZFS_DATA) == 0)
345 		search.re_subclass = ZSC_DATA;
346 	else if (strcmp(subclass, FM_EREPORT_ZFS_CHECKSUM) == 0)
347 		search.re_subclass = ZSC_CHECKSUM;
348 	else
349 		return (B_FALSE);
350 
351 	search.re_pool_guid = spa_guid(spa);
352 	search.re_vdev_guid = vd->vdev_guid;
353 	search.re_io_error = zio->io_error;
354 	search.re_io_priority = zio->io_priority;
355 	/* if size is supplied use it over what's in zio */
356 	if (size) {
357 		search.re_io_size = size;
358 		search.re_io_offset = offset;
359 	} else {
360 		search.re_io_size = zio->io_size;
361 		search.re_io_offset = zio->io_offset;
362 	}
363 
364 	/* grab optional logical zio criteria */
365 	if (zb != NULL) {
366 		search.re_io_bookmark.zb_objset = zb->zb_objset;
367 		search.re_io_bookmark.zb_object = zb->zb_object;
368 		search.re_io_bookmark.zb_level = zb->zb_level;
369 		search.re_io_bookmark.zb_blkid = zb->zb_blkid;
370 	}
371 
372 	uint64_t now = gethrtime();
373 
374 	mutex_enter(&recent_events_lock);
375 
376 	/* check if we have seen this one recently */
377 	entry = avl_find(&recent_events_tree, &search, NULL);
378 	if (entry != NULL) {
379 		uint64_t age = NSEC2SEC(now - entry->re_timestamp);
380 
381 		/*
382 		 * There is still an active cleaner (since we're here).
383 		 * Reset the last seen time for this duplicate entry
384 		 * so that its lifespand gets extended.
385 		 */
386 		list_remove(&recent_events_list, entry);
387 		list_insert_head(&recent_events_list, entry);
388 		entry->re_timestamp = now;
389 
390 		zfs_zevent_track_duplicate();
391 		mutex_exit(&recent_events_lock);
392 
393 		return (age <= zfs_zevent_retain_expire_secs);
394 	}
395 
396 	if (avl_numnodes(&recent_events_tree) >= zfs_zevent_retain_max) {
397 		/* recycle oldest node */
398 		entry = list_tail(&recent_events_list);
399 		ASSERT(entry != NULL);
400 		list_remove(&recent_events_list, entry);
401 		avl_remove(&recent_events_tree, entry);
402 	} else {
403 		entry = kmem_alloc(sizeof (recent_events_node_t), KM_SLEEP);
404 	}
405 
406 	/* record this as a recent ereport */
407 	*entry = search;
408 	avl_add(&recent_events_tree, entry);
409 	list_insert_head(&recent_events_list, entry);
410 	entry->re_timestamp = now;
411 
412 	/* Start a cleaner if not already scheduled */
413 	if (recent_events_cleaner_tqid == 0)
414 		zfs_ereport_schedule_cleaner();
415 
416 	mutex_exit(&recent_events_lock);
417 	return (B_FALSE);
418 }
419 
420 void
zfs_zevent_post_cb(nvlist_t * nvl,nvlist_t * detector)421 zfs_zevent_post_cb(nvlist_t *nvl, nvlist_t *detector)
422 {
423 	if (nvl)
424 		fm_nvlist_destroy(nvl, FM_NVA_FREE);
425 
426 	if (detector)
427 		fm_nvlist_destroy(detector, FM_NVA_FREE);
428 }
429 
430 /*
431  * We want to rate limit ZIO delay, deadman, and checksum events so as to not
432  * flood zevent consumers when a disk is acting up.
433  *
434  * Returns 1 if we're ratelimiting, 0 if not.
435  */
436 static int
zfs_is_ratelimiting_event(const char * subclass,vdev_t * vd)437 zfs_is_ratelimiting_event(const char *subclass, vdev_t *vd)
438 {
439 	int rc = 0;
440 	/*
441 	 * zfs_ratelimit() returns 1 if we're *not* ratelimiting and 0 if we
442 	 * are.  Invert it to get our return value.
443 	 */
444 	if (strcmp(subclass, FM_EREPORT_ZFS_DELAY) == 0) {
445 		rc = !zfs_ratelimit(&vd->vdev_delay_rl);
446 	} else if (strcmp(subclass, FM_EREPORT_ZFS_DEADMAN) == 0) {
447 		rc = !zfs_ratelimit(&vd->vdev_deadman_rl);
448 	} else if (strcmp(subclass, FM_EREPORT_ZFS_CHECKSUM) == 0) {
449 		rc = !zfs_ratelimit(&vd->vdev_checksum_rl);
450 	}
451 
452 	if (rc)	{
453 		/* We're rate limiting */
454 		fm_erpt_dropped_increment();
455 	}
456 
457 	return (rc);
458 }
459 
460 /*
461  * Return B_TRUE if the event actually posted, B_FALSE if not.
462  */
463 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)464 zfs_ereport_start(nvlist_t **ereport_out, nvlist_t **detector_out,
465     const char *subclass, spa_t *spa, vdev_t *vd, const zbookmark_phys_t *zb,
466     zio_t *zio, uint64_t stateoroffset, uint64_t size)
467 {
468 	nvlist_t *ereport, *detector;
469 
470 	uint64_t ena;
471 	char class[64];
472 
473 	if ((ereport = fm_nvlist_create(NULL)) == NULL)
474 		return (B_FALSE);
475 
476 	if ((detector = fm_nvlist_create(NULL)) == NULL) {
477 		fm_nvlist_destroy(ereport, FM_NVA_FREE);
478 		return (B_FALSE);
479 	}
480 
481 	/*
482 	 * Serialize ereport generation
483 	 */
484 	mutex_enter(&spa->spa_errlist_lock);
485 
486 	/*
487 	 * Determine the ENA to use for this event.  If we are in a loading
488 	 * state, use a SPA-wide ENA.  Otherwise, if we are in an I/O state, use
489 	 * a root zio-wide ENA.  Otherwise, simply use a unique ENA.
490 	 */
491 	if (spa_load_state(spa) != SPA_LOAD_NONE) {
492 		if (spa->spa_ena == 0)
493 			spa->spa_ena = fm_ena_generate(0, FM_ENA_FMT1);
494 		ena = spa->spa_ena;
495 	} else if (zio != NULL && zio->io_logical != NULL) {
496 		if (zio->io_logical->io_ena == 0)
497 			zio->io_logical->io_ena =
498 			    fm_ena_generate(0, FM_ENA_FMT1);
499 		ena = zio->io_logical->io_ena;
500 	} else {
501 		ena = fm_ena_generate(0, FM_ENA_FMT1);
502 	}
503 
504 	/*
505 	 * Construct the full class, detector, and other standard FMA fields.
506 	 */
507 	(void) snprintf(class, sizeof (class), "%s.%s",
508 	    ZFS_ERROR_CLASS, subclass);
509 
510 	fm_fmri_zfs_set(detector, FM_ZFS_SCHEME_VERSION, spa_guid(spa),
511 	    vd != NULL ? vd->vdev_guid : 0);
512 
513 	fm_ereport_set(ereport, FM_EREPORT_VERSION, class, ena, detector, NULL);
514 
515 	/*
516 	 * Construct the per-ereport payload, depending on which parameters are
517 	 * passed in.
518 	 */
519 
520 	/*
521 	 * Generic payload members common to all ereports.
522 	 */
523 	fm_payload_set(ereport,
524 	    FM_EREPORT_PAYLOAD_ZFS_POOL, DATA_TYPE_STRING, spa_name(spa),
525 	    FM_EREPORT_PAYLOAD_ZFS_POOL_GUID, DATA_TYPE_UINT64, spa_guid(spa),
526 	    FM_EREPORT_PAYLOAD_ZFS_POOL_STATE, DATA_TYPE_UINT64,
527 	    (uint64_t)spa_state(spa),
528 	    FM_EREPORT_PAYLOAD_ZFS_POOL_CONTEXT, DATA_TYPE_INT32,
529 	    (int32_t)spa_load_state(spa), NULL);
530 
531 	fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_POOL_FAILMODE,
532 	    DATA_TYPE_STRING,
533 	    spa_get_failmode(spa) == ZIO_FAILURE_MODE_WAIT ?
534 	    FM_EREPORT_FAILMODE_WAIT :
535 	    spa_get_failmode(spa) == ZIO_FAILURE_MODE_CONTINUE ?
536 	    FM_EREPORT_FAILMODE_CONTINUE : FM_EREPORT_FAILMODE_PANIC,
537 	    NULL);
538 
539 	if (vd != NULL) {
540 		vdev_t *pvd = vd->vdev_parent;
541 		vdev_queue_t *vq = &vd->vdev_queue;
542 		vdev_stat_t *vs = &vd->vdev_stat;
543 		vdev_t *spare_vd;
544 		uint64_t *spare_guids;
545 		char **spare_paths;
546 		int i, spare_count;
547 
548 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_VDEV_GUID,
549 		    DATA_TYPE_UINT64, vd->vdev_guid,
550 		    FM_EREPORT_PAYLOAD_ZFS_VDEV_TYPE,
551 		    DATA_TYPE_STRING, vd->vdev_ops->vdev_op_type, NULL);
552 		if (vd->vdev_path != NULL)
553 			fm_payload_set(ereport,
554 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_PATH,
555 			    DATA_TYPE_STRING, vd->vdev_path, NULL);
556 		if (vd->vdev_devid != NULL)
557 			fm_payload_set(ereport,
558 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_DEVID,
559 			    DATA_TYPE_STRING, vd->vdev_devid, NULL);
560 		if (vd->vdev_fru != NULL)
561 			fm_payload_set(ereport,
562 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_FRU,
563 			    DATA_TYPE_STRING, vd->vdev_fru, NULL);
564 		if (vd->vdev_enc_sysfs_path != NULL)
565 			fm_payload_set(ereport,
566 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_ENC_SYSFS_PATH,
567 			    DATA_TYPE_STRING, vd->vdev_enc_sysfs_path, NULL);
568 		if (vd->vdev_ashift)
569 			fm_payload_set(ereport,
570 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_ASHIFT,
571 			    DATA_TYPE_UINT64, vd->vdev_ashift, NULL);
572 
573 		if (vq != NULL) {
574 			fm_payload_set(ereport,
575 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_COMP_TS,
576 			    DATA_TYPE_UINT64, vq->vq_io_complete_ts, NULL);
577 			fm_payload_set(ereport,
578 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_DELTA_TS,
579 			    DATA_TYPE_UINT64, vq->vq_io_delta_ts, NULL);
580 		}
581 
582 		if (vs != NULL) {
583 			fm_payload_set(ereport,
584 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_READ_ERRORS,
585 			    DATA_TYPE_UINT64, vs->vs_read_errors,
586 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_WRITE_ERRORS,
587 			    DATA_TYPE_UINT64, vs->vs_write_errors,
588 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_CKSUM_ERRORS,
589 			    DATA_TYPE_UINT64, vs->vs_checksum_errors,
590 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_DELAYS,
591 			    DATA_TYPE_UINT64, vs->vs_slow_ios,
592 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_DIO_VERIFY_ERRORS,
593 			    DATA_TYPE_UINT64, vs->vs_dio_verify_errors,
594 			    NULL);
595 		}
596 
597 		if (pvd != NULL) {
598 			fm_payload_set(ereport,
599 			    FM_EREPORT_PAYLOAD_ZFS_PARENT_GUID,
600 			    DATA_TYPE_UINT64, pvd->vdev_guid,
601 			    FM_EREPORT_PAYLOAD_ZFS_PARENT_TYPE,
602 			    DATA_TYPE_STRING, pvd->vdev_ops->vdev_op_type,
603 			    NULL);
604 			if (pvd->vdev_path)
605 				fm_payload_set(ereport,
606 				    FM_EREPORT_PAYLOAD_ZFS_PARENT_PATH,
607 				    DATA_TYPE_STRING, pvd->vdev_path, NULL);
608 			if (pvd->vdev_devid)
609 				fm_payload_set(ereport,
610 				    FM_EREPORT_PAYLOAD_ZFS_PARENT_DEVID,
611 				    DATA_TYPE_STRING, pvd->vdev_devid, NULL);
612 		}
613 
614 		spare_count = spa->spa_spares.sav_count;
615 		spare_paths = kmem_zalloc(sizeof (char *) * spare_count,
616 		    KM_SLEEP);
617 		spare_guids = kmem_zalloc(sizeof (uint64_t) * spare_count,
618 		    KM_SLEEP);
619 
620 		for (i = 0; i < spare_count; i++) {
621 			spare_vd = spa->spa_spares.sav_vdevs[i];
622 			if (spare_vd) {
623 				spare_paths[i] = spare_vd->vdev_path;
624 				spare_guids[i] = spare_vd->vdev_guid;
625 			}
626 		}
627 
628 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_VDEV_SPARE_PATHS,
629 		    DATA_TYPE_STRING_ARRAY, spare_count, spare_paths,
630 		    FM_EREPORT_PAYLOAD_ZFS_VDEV_SPARE_GUIDS,
631 		    DATA_TYPE_UINT64_ARRAY, spare_count, spare_guids, NULL);
632 
633 		kmem_free(spare_guids, sizeof (uint64_t) * spare_count);
634 		kmem_free(spare_paths, sizeof (char *) * spare_count);
635 	}
636 
637 	if (zio != NULL) {
638 		/*
639 		 * Payload common to all I/Os.
640 		 */
641 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_ERR,
642 		    DATA_TYPE_INT32, zio->io_error, NULL);
643 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_FLAGS,
644 		    DATA_TYPE_UINT64, zio->io_flags, NULL);
645 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_STAGE,
646 		    DATA_TYPE_UINT32, zio->io_stage, NULL);
647 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_PIPELINE,
648 		    DATA_TYPE_UINT32, zio->io_pipeline, NULL);
649 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_DELAY,
650 		    DATA_TYPE_UINT64, zio->io_delay, NULL);
651 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_TIMESTAMP,
652 		    DATA_TYPE_UINT64, zio->io_timestamp, NULL);
653 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_DELTA,
654 		    DATA_TYPE_UINT64, zio->io_delta, NULL);
655 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_TYPE,
656 		    DATA_TYPE_UINT32, zio->io_type, NULL);
657 		fm_payload_set(ereport, FM_EREPORT_PAYLOAD_ZFS_ZIO_PRIORITY,
658 		    DATA_TYPE_UINT32, zio->io_priority, NULL);
659 
660 		/*
661 		 * If the 'size' parameter is non-zero, it indicates this is a
662 		 * RAID-Z or other I/O where the physical offset and length are
663 		 * provided for us, instead of within the zio_t.
664 		 */
665 		if (vd != NULL) {
666 			if (size)
667 				fm_payload_set(ereport,
668 				    FM_EREPORT_PAYLOAD_ZFS_ZIO_OFFSET,
669 				    DATA_TYPE_UINT64, stateoroffset,
670 				    FM_EREPORT_PAYLOAD_ZFS_ZIO_SIZE,
671 				    DATA_TYPE_UINT64, size, NULL);
672 			else
673 				fm_payload_set(ereport,
674 				    FM_EREPORT_PAYLOAD_ZFS_ZIO_OFFSET,
675 				    DATA_TYPE_UINT64, zio->io_offset,
676 				    FM_EREPORT_PAYLOAD_ZFS_ZIO_SIZE,
677 				    DATA_TYPE_UINT64, zio->io_size, NULL);
678 		}
679 	} else if (vd != NULL) {
680 		/*
681 		 * If we have a vdev but no zio, this is a device fault, and the
682 		 * 'stateoroffset' parameter indicates the previous state of the
683 		 * vdev.
684 		 */
685 		fm_payload_set(ereport,
686 		    FM_EREPORT_PAYLOAD_ZFS_PREV_STATE,
687 		    DATA_TYPE_UINT64, stateoroffset, NULL);
688 	}
689 
690 	/*
691 	 * Payload for I/Os with corresponding logical information.
692 	 */
693 	if (zb != NULL && (zio == NULL || zio->io_logical != NULL)) {
694 		fm_payload_set(ereport,
695 		    FM_EREPORT_PAYLOAD_ZFS_ZIO_OBJSET,
696 		    DATA_TYPE_UINT64, zb->zb_objset,
697 		    FM_EREPORT_PAYLOAD_ZFS_ZIO_OBJECT,
698 		    DATA_TYPE_UINT64, zb->zb_object,
699 		    FM_EREPORT_PAYLOAD_ZFS_ZIO_LEVEL,
700 		    DATA_TYPE_INT64, zb->zb_level,
701 		    FM_EREPORT_PAYLOAD_ZFS_ZIO_BLKID,
702 		    DATA_TYPE_UINT64, zb->zb_blkid, NULL);
703 	}
704 
705 	/*
706 	 * Payload for tuning the zed
707 	 */
708 	if (vd != NULL && strcmp(subclass, FM_EREPORT_ZFS_CHECKSUM) == 0) {
709 		uint64_t cksum_n, cksum_t;
710 
711 		cksum_n = vdev_prop_get_inherited(vd, VDEV_PROP_CHECKSUM_N);
712 		if (cksum_n != vdev_prop_default_numeric(VDEV_PROP_CHECKSUM_N))
713 			fm_payload_set(ereport,
714 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_CKSUM_N,
715 			    DATA_TYPE_UINT64,
716 			    cksum_n,
717 			    NULL);
718 
719 		cksum_t = vdev_prop_get_inherited(vd, VDEV_PROP_CHECKSUM_T);
720 		if (cksum_t != vdev_prop_default_numeric(VDEV_PROP_CHECKSUM_T))
721 			fm_payload_set(ereport,
722 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_CKSUM_T,
723 			    DATA_TYPE_UINT64,
724 			    cksum_t,
725 			    NULL);
726 	}
727 
728 	if (vd != NULL && strcmp(subclass, FM_EREPORT_ZFS_IO) == 0) {
729 		uint64_t io_n, io_t;
730 
731 		io_n = vdev_prop_get_inherited(vd, VDEV_PROP_IO_N);
732 		if (io_n != vdev_prop_default_numeric(VDEV_PROP_IO_N))
733 			fm_payload_set(ereport,
734 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_IO_N,
735 			    DATA_TYPE_UINT64,
736 			    io_n,
737 			    NULL);
738 
739 		io_t = vdev_prop_get_inherited(vd, VDEV_PROP_IO_T);
740 		if (io_t != vdev_prop_default_numeric(VDEV_PROP_IO_T))
741 			fm_payload_set(ereport,
742 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_IO_T,
743 			    DATA_TYPE_UINT64,
744 			    io_t,
745 			    NULL);
746 	}
747 
748 	if (vd != NULL && strcmp(subclass, FM_EREPORT_ZFS_DELAY) == 0) {
749 		uint64_t slow_io_n, slow_io_t;
750 
751 		slow_io_n = vdev_prop_get_inherited(vd, VDEV_PROP_SLOW_IO_N);
752 		if (slow_io_n != vdev_prop_default_numeric(VDEV_PROP_SLOW_IO_N))
753 			fm_payload_set(ereport,
754 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_SLOW_IO_N,
755 			    DATA_TYPE_UINT64,
756 			    slow_io_n,
757 			    NULL);
758 
759 		slow_io_t = vdev_prop_get_inherited(vd, VDEV_PROP_SLOW_IO_T);
760 		if (slow_io_t != vdev_prop_default_numeric(VDEV_PROP_SLOW_IO_T))
761 			fm_payload_set(ereport,
762 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_SLOW_IO_T,
763 			    DATA_TYPE_UINT64,
764 			    slow_io_t,
765 			    NULL);
766 	}
767 
768 	mutex_exit(&spa->spa_errlist_lock);
769 
770 	*ereport_out = ereport;
771 	*detector_out = detector;
772 	return (B_TRUE);
773 }
774 
775 /* if it's <= 128 bytes, save the corruption directly */
776 #define	ZFM_MAX_INLINE		(128 / sizeof (uint64_t))
777 
778 #define	MAX_RANGES		16
779 
780 typedef struct zfs_ecksum_info {
781 	/* inline arrays of bits set and cleared. */
782 	uint64_t zei_bits_set[ZFM_MAX_INLINE];
783 	uint64_t zei_bits_cleared[ZFM_MAX_INLINE];
784 
785 	/*
786 	 * for each range, the number of bits set and cleared.  The Hamming
787 	 * distance between the good and bad buffers is the sum of them all.
788 	 */
789 	uint32_t zei_range_sets[MAX_RANGES];
790 	uint32_t zei_range_clears[MAX_RANGES];
791 
792 	struct zei_ranges {
793 		uint32_t	zr_start;
794 		uint32_t	zr_end;
795 	} zei_ranges[MAX_RANGES];
796 
797 	size_t	zei_range_count;
798 	uint32_t zei_mingap;
799 	uint32_t zei_allowed_mingap;
800 
801 } zfs_ecksum_info_t;
802 
803 static void
update_bad_bits(uint64_t value_arg,uint32_t * count)804 update_bad_bits(uint64_t value_arg, uint32_t *count)
805 {
806 	size_t i;
807 	size_t bits = 0;
808 	uint64_t value = BE_64(value_arg);
809 
810 	/* We store the bits in big-endian (largest-first) order */
811 	for (i = 0; i < 64; i++) {
812 		if (value & (1ull << i))
813 			++bits;
814 	}
815 	/* update the count of bits changed */
816 	*count += bits;
817 }
818 
819 /*
820  * We've now filled up the range array, and need to increase "mingap" and
821  * shrink the range list accordingly.  zei_mingap is always the smallest
822  * distance between array entries, so we set the new_allowed_gap to be
823  * one greater than that.  We then go through the list, joining together
824  * any ranges which are closer than the new_allowed_gap.
825  *
826  * By construction, there will be at least one.  We also update zei_mingap
827  * to the new smallest gap, to prepare for our next invocation.
828  */
829 static void
zei_shrink_ranges(zfs_ecksum_info_t * eip)830 zei_shrink_ranges(zfs_ecksum_info_t *eip)
831 {
832 	uint32_t mingap = UINT32_MAX;
833 	uint32_t new_allowed_gap = eip->zei_mingap + 1;
834 
835 	size_t idx, output;
836 	size_t max = eip->zei_range_count;
837 
838 	struct zei_ranges *r = eip->zei_ranges;
839 
840 	ASSERT3U(eip->zei_range_count, >, 0);
841 	ASSERT3U(eip->zei_range_count, <=, MAX_RANGES);
842 
843 	output = idx = 0;
844 	while (idx < max - 1) {
845 		uint32_t start = r[idx].zr_start;
846 		uint32_t end = r[idx].zr_end;
847 
848 		while (idx < max - 1) {
849 			idx++;
850 
851 			uint32_t nstart = r[idx].zr_start;
852 			uint32_t nend = r[idx].zr_end;
853 
854 			uint32_t gap = nstart - end;
855 			if (gap < new_allowed_gap) {
856 				end = nend;
857 				continue;
858 			}
859 			if (gap < mingap)
860 				mingap = gap;
861 			break;
862 		}
863 		r[output].zr_start = start;
864 		r[output].zr_end = end;
865 		output++;
866 	}
867 	ASSERT3U(output, <, eip->zei_range_count);
868 	eip->zei_range_count = output;
869 	eip->zei_mingap = mingap;
870 	eip->zei_allowed_mingap = new_allowed_gap;
871 }
872 
873 static void
zei_add_range(zfs_ecksum_info_t * eip,int start,int end)874 zei_add_range(zfs_ecksum_info_t *eip, int start, int end)
875 {
876 	struct zei_ranges *r = eip->zei_ranges;
877 	size_t count = eip->zei_range_count;
878 
879 	if (count >= MAX_RANGES) {
880 		zei_shrink_ranges(eip);
881 		count = eip->zei_range_count;
882 	}
883 	if (count == 0) {
884 		eip->zei_mingap = UINT32_MAX;
885 		eip->zei_allowed_mingap = 1;
886 	} else {
887 		int gap = start - r[count - 1].zr_end;
888 
889 		if (gap < eip->zei_allowed_mingap) {
890 			r[count - 1].zr_end = end;
891 			return;
892 		}
893 		if (gap < eip->zei_mingap)
894 			eip->zei_mingap = gap;
895 	}
896 	r[count].zr_start = start;
897 	r[count].zr_end = end;
898 	eip->zei_range_count++;
899 }
900 
901 static size_t
zei_range_total_size(zfs_ecksum_info_t * eip)902 zei_range_total_size(zfs_ecksum_info_t *eip)
903 {
904 	struct zei_ranges *r = eip->zei_ranges;
905 	size_t count = eip->zei_range_count;
906 	size_t result = 0;
907 	size_t idx;
908 
909 	for (idx = 0; idx < count; idx++)
910 		result += (r[idx].zr_end - r[idx].zr_start);
911 
912 	return (result);
913 }
914 
915 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)916 annotate_ecksum(nvlist_t *ereport, zio_bad_cksum_t *info,
917     const abd_t *goodabd, const abd_t *badabd, size_t size,
918     boolean_t drop_if_identical)
919 {
920 	const uint64_t *good;
921 	const uint64_t *bad;
922 
923 	size_t nui64s = size / sizeof (uint64_t);
924 
925 	size_t inline_size;
926 	int no_inline = 0;
927 	size_t idx;
928 	size_t range;
929 
930 	size_t offset = 0;
931 	ssize_t start = -1;
932 
933 	zfs_ecksum_info_t *eip = kmem_zalloc(sizeof (*eip), KM_SLEEP);
934 
935 	/* don't do any annotation for injected checksum errors */
936 	if (info != NULL && info->zbc_injected)
937 		return (eip);
938 
939 	if (info != NULL && info->zbc_has_cksum) {
940 		fm_payload_set(ereport,
941 		    FM_EREPORT_PAYLOAD_ZFS_CKSUM_ALGO,
942 		    DATA_TYPE_STRING,
943 		    info->zbc_checksum_name,
944 		    NULL);
945 
946 		if (info->zbc_byteswapped) {
947 			fm_payload_set(ereport,
948 			    FM_EREPORT_PAYLOAD_ZFS_CKSUM_BYTESWAP,
949 			    DATA_TYPE_BOOLEAN, 1,
950 			    NULL);
951 		}
952 	}
953 
954 	if (badabd == NULL || goodabd == NULL)
955 		return (eip);
956 
957 	ASSERT3U(nui64s, <=, UINT32_MAX);
958 	ASSERT3U(size, ==, nui64s * sizeof (uint64_t));
959 	ASSERT3U(size, <=, SPA_MAXBLOCKSIZE);
960 	ASSERT3U(size, <=, UINT32_MAX);
961 
962 	good = (const uint64_t *) abd_borrow_buf_copy((abd_t *)goodabd, size);
963 	bad = (const uint64_t *) abd_borrow_buf_copy((abd_t *)badabd, size);
964 
965 	/* build up the range list by comparing the two buffers. */
966 	for (idx = 0; idx < nui64s; idx++) {
967 		if (good[idx] == bad[idx]) {
968 			if (start == -1)
969 				continue;
970 
971 			zei_add_range(eip, start, idx);
972 			start = -1;
973 		} else {
974 			if (start != -1)
975 				continue;
976 
977 			start = idx;
978 		}
979 	}
980 	if (start != -1)
981 		zei_add_range(eip, start, idx);
982 
983 	/* See if it will fit in our inline buffers */
984 	inline_size = zei_range_total_size(eip);
985 	if (inline_size > ZFM_MAX_INLINE)
986 		no_inline = 1;
987 
988 	/*
989 	 * If there is no change and we want to drop if the buffers are
990 	 * identical, do so.
991 	 */
992 	if (inline_size == 0 && drop_if_identical) {
993 		kmem_free(eip, sizeof (*eip));
994 		abd_return_buf((abd_t *)goodabd, (void *)good, size);
995 		abd_return_buf((abd_t *)badabd, (void *)bad, size);
996 		return (NULL);
997 	}
998 
999 	/*
1000 	 * Now walk through the ranges, filling in the details of the
1001 	 * differences.  Also convert our uint64_t-array offsets to byte
1002 	 * offsets.
1003 	 */
1004 	for (range = 0; range < eip->zei_range_count; range++) {
1005 		size_t start = eip->zei_ranges[range].zr_start;
1006 		size_t end = eip->zei_ranges[range].zr_end;
1007 
1008 		for (idx = start; idx < end; idx++) {
1009 			uint64_t set, cleared;
1010 
1011 			// bits set in bad, but not in good
1012 			set = ((~good[idx]) & bad[idx]);
1013 			// bits set in good, but not in bad
1014 			cleared = (good[idx] & (~bad[idx]));
1015 
1016 			if (!no_inline) {
1017 				ASSERT3U(offset, <, inline_size);
1018 				eip->zei_bits_set[offset] = set;
1019 				eip->zei_bits_cleared[offset] = cleared;
1020 				offset++;
1021 			}
1022 
1023 			update_bad_bits(set, &eip->zei_range_sets[range]);
1024 			update_bad_bits(cleared, &eip->zei_range_clears[range]);
1025 		}
1026 
1027 		/* convert to byte offsets */
1028 		eip->zei_ranges[range].zr_start	*= sizeof (uint64_t);
1029 		eip->zei_ranges[range].zr_end	*= sizeof (uint64_t);
1030 	}
1031 
1032 	abd_return_buf((abd_t *)goodabd, (void *)good, size);
1033 	abd_return_buf((abd_t *)badabd, (void *)bad, size);
1034 
1035 	eip->zei_allowed_mingap	*= sizeof (uint64_t);
1036 	inline_size		*= sizeof (uint64_t);
1037 
1038 	/* fill in ereport */
1039 	fm_payload_set(ereport,
1040 	    FM_EREPORT_PAYLOAD_ZFS_BAD_OFFSET_RANGES,
1041 	    DATA_TYPE_UINT32_ARRAY, 2 * eip->zei_range_count,
1042 	    (uint32_t *)eip->zei_ranges,
1043 	    FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_MIN_GAP,
1044 	    DATA_TYPE_UINT32, eip->zei_allowed_mingap,
1045 	    FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_SETS,
1046 	    DATA_TYPE_UINT32_ARRAY, eip->zei_range_count, eip->zei_range_sets,
1047 	    FM_EREPORT_PAYLOAD_ZFS_BAD_RANGE_CLEARS,
1048 	    DATA_TYPE_UINT32_ARRAY, eip->zei_range_count, eip->zei_range_clears,
1049 	    NULL);
1050 
1051 	if (!no_inline) {
1052 		fm_payload_set(ereport,
1053 		    FM_EREPORT_PAYLOAD_ZFS_BAD_SET_BITS,
1054 		    DATA_TYPE_UINT8_ARRAY,
1055 		    inline_size, (uint8_t *)eip->zei_bits_set,
1056 		    FM_EREPORT_PAYLOAD_ZFS_BAD_CLEARED_BITS,
1057 		    DATA_TYPE_UINT8_ARRAY,
1058 		    inline_size, (uint8_t *)eip->zei_bits_cleared,
1059 		    NULL);
1060 	}
1061 	return (eip);
1062 }
1063 #else
1064 void
zfs_ereport_clear(spa_t * spa,vdev_t * vd)1065 zfs_ereport_clear(spa_t *spa, vdev_t *vd)
1066 {
1067 	(void) spa, (void) vd;
1068 }
1069 #endif
1070 
1071 /*
1072  * Make sure our event is still valid for the given zio/vdev/pool.  For example,
1073  * we don't want to keep logging events for a faulted or missing vdev.
1074  */
1075 boolean_t
zfs_ereport_is_valid(const char * subclass,spa_t * spa,vdev_t * vd,zio_t * zio)1076 zfs_ereport_is_valid(const char *subclass, spa_t *spa, vdev_t *vd, zio_t *zio)
1077 {
1078 #ifdef _KERNEL
1079 	/*
1080 	 * If we are doing a spa_tryimport() or in recovery mode,
1081 	 * ignore errors.
1082 	 */
1083 	if (spa_load_state(spa) == SPA_LOAD_TRYIMPORT ||
1084 	    spa_load_state(spa) == SPA_LOAD_RECOVER)
1085 		return (B_FALSE);
1086 
1087 	/*
1088 	 * If we are in the middle of opening a pool, and the previous attempt
1089 	 * failed, don't bother logging any new ereports - we're just going to
1090 	 * get the same diagnosis anyway.
1091 	 */
1092 	if (spa_load_state(spa) != SPA_LOAD_NONE &&
1093 	    spa->spa_last_open_failed)
1094 		return (B_FALSE);
1095 
1096 	if (zio != NULL) {
1097 		/* If this is not a read or write zio, ignore the error */
1098 		if (zio->io_type != ZIO_TYPE_READ &&
1099 		    zio->io_type != ZIO_TYPE_WRITE)
1100 			return (B_FALSE);
1101 
1102 		if (vd != NULL) {
1103 			/*
1104 			 * If the vdev has already been marked as failing due
1105 			 * to a failed probe, then ignore any subsequent I/O
1106 			 * errors, as the DE will automatically fault the vdev
1107 			 * on the first such failure.  This also catches cases
1108 			 * where vdev_remove_wanted is set and the device has
1109 			 * not yet been asynchronously placed into the REMOVED
1110 			 * state.
1111 			 */
1112 			if (zio->io_vd == vd && !vdev_accessible(vd, zio))
1113 				return (B_FALSE);
1114 
1115 			/*
1116 			 * Ignore checksum errors for reads from DTL regions of
1117 			 * leaf vdevs.
1118 			 */
1119 			if (zio->io_type == ZIO_TYPE_READ &&
1120 			    zio->io_error == ECKSUM &&
1121 			    vd->vdev_ops->vdev_op_leaf &&
1122 			    vdev_dtl_contains(vd, DTL_MISSING, zio->io_txg, 1))
1123 				return (B_FALSE);
1124 		}
1125 	}
1126 
1127 	/*
1128 	 * For probe failure, we want to avoid posting ereports if we've
1129 	 * already removed the device in the meantime.
1130 	 */
1131 	if (vd != NULL &&
1132 	    strcmp(subclass, FM_EREPORT_ZFS_PROBE_FAILURE) == 0 &&
1133 	    (vd->vdev_remove_wanted || vd->vdev_state == VDEV_STATE_REMOVED))
1134 		return (B_FALSE);
1135 
1136 	/* Ignore bogus delay events (like from ioctls or unqueued IOs) */
1137 	if ((strcmp(subclass, FM_EREPORT_ZFS_DELAY) == 0) &&
1138 	    (zio != NULL) && (!zio->io_timestamp)) {
1139 		return (B_FALSE);
1140 	}
1141 #else
1142 	(void) subclass, (void) spa, (void) vd, (void) zio;
1143 #endif
1144 	return (B_TRUE);
1145 }
1146 
1147 /*
1148  * Post an ereport for the given subclass
1149  *
1150  * Returns
1151  * - 0 if an event was posted
1152  * - EINVAL if there was a problem posting event
1153  * - EBUSY if the event was rate limited
1154  * - EALREADY if the event was already posted (duplicate)
1155  */
1156 int
zfs_ereport_post(const char * subclass,spa_t * spa,vdev_t * vd,const zbookmark_phys_t * zb,zio_t * zio,uint64_t state)1157 zfs_ereport_post(const char *subclass, spa_t *spa, vdev_t *vd,
1158     const zbookmark_phys_t *zb, zio_t *zio, uint64_t state)
1159 {
1160 	int rc = 0;
1161 #ifdef _KERNEL
1162 	nvlist_t *ereport = NULL;
1163 	nvlist_t *detector = NULL;
1164 
1165 	if (!zfs_ereport_is_valid(subclass, spa, vd, zio))
1166 		return (EINVAL);
1167 
1168 	if (zfs_ereport_is_duplicate(subclass, spa, vd, zb, zio, 0, 0))
1169 		return (SET_ERROR(EALREADY));
1170 
1171 	if (zfs_is_ratelimiting_event(subclass, vd))
1172 		return (SET_ERROR(EBUSY));
1173 
1174 	if (!zfs_ereport_start(&ereport, &detector, subclass, spa, vd,
1175 	    zb, zio, state, 0))
1176 		return (SET_ERROR(EINVAL));	/* couldn't post event */
1177 
1178 	if (ereport == NULL)
1179 		return (SET_ERROR(EINVAL));
1180 
1181 	/* Cleanup is handled by the callback function */
1182 	rc = zfs_zevent_post(ereport, detector, zfs_zevent_post_cb);
1183 #else
1184 	(void) subclass, (void) spa, (void) vd, (void) zb, (void) zio,
1185 	    (void) state;
1186 #endif
1187 	return (rc);
1188 }
1189 
1190 /*
1191  * Prepare a checksum ereport
1192  *
1193  * Returns
1194  * - 0 if an event was posted
1195  * - EINVAL if there was a problem posting event
1196  * - EBUSY if the event was rate limited
1197  * - EALREADY if the event was already posted (duplicate)
1198  */
1199 int
zfs_ereport_start_checksum(spa_t * spa,vdev_t * vd,const zbookmark_phys_t * zb,struct zio * zio,uint64_t offset,uint64_t length,zio_bad_cksum_t * info)1200 zfs_ereport_start_checksum(spa_t *spa, vdev_t *vd, const zbookmark_phys_t *zb,
1201     struct zio *zio, uint64_t offset, uint64_t length, zio_bad_cksum_t *info)
1202 {
1203 	zio_cksum_report_t *report;
1204 
1205 #ifdef _KERNEL
1206 	if (!zfs_ereport_is_valid(FM_EREPORT_ZFS_CHECKSUM, spa, vd, zio))
1207 		return (SET_ERROR(EINVAL));
1208 
1209 	if (zfs_ereport_is_duplicate(FM_EREPORT_ZFS_CHECKSUM, spa, vd, zb, zio,
1210 	    offset, length))
1211 		return (SET_ERROR(EALREADY));
1212 
1213 	if (zfs_is_ratelimiting_event(FM_EREPORT_ZFS_CHECKSUM, vd))
1214 		return (SET_ERROR(EBUSY));
1215 #else
1216 	(void) zb, (void) offset;
1217 #endif
1218 
1219 	report = kmem_zalloc(sizeof (*report), KM_SLEEP);
1220 
1221 	zio_vsd_default_cksum_report(zio, report);
1222 
1223 	/* copy the checksum failure information if it was provided */
1224 	if (info != NULL) {
1225 		report->zcr_ckinfo = kmem_zalloc(sizeof (*info), KM_SLEEP);
1226 		memcpy(report->zcr_ckinfo, info, sizeof (*info));
1227 	}
1228 
1229 	report->zcr_sector = 1ULL << vd->vdev_top->vdev_ashift;
1230 	report->zcr_align =
1231 	    vdev_psize_to_asize(vd->vdev_top, report->zcr_sector);
1232 	report->zcr_length = length;
1233 
1234 #ifdef _KERNEL
1235 	(void) zfs_ereport_start(&report->zcr_ereport, &report->zcr_detector,
1236 	    FM_EREPORT_ZFS_CHECKSUM, spa, vd, zb, zio, offset, length);
1237 
1238 	if (report->zcr_ereport == NULL) {
1239 		zfs_ereport_free_checksum(report);
1240 		return (0);
1241 	}
1242 #endif
1243 
1244 	mutex_enter(&spa->spa_errlist_lock);
1245 	report->zcr_next = zio->io_logical->io_cksum_report;
1246 	zio->io_logical->io_cksum_report = report;
1247 	mutex_exit(&spa->spa_errlist_lock);
1248 	return (0);
1249 }
1250 
1251 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)1252 zfs_ereport_finish_checksum(zio_cksum_report_t *report, const abd_t *good_data,
1253     const abd_t *bad_data, boolean_t drop_if_identical)
1254 {
1255 #ifdef _KERNEL
1256 	zfs_ecksum_info_t *info;
1257 
1258 	info = annotate_ecksum(report->zcr_ereport, report->zcr_ckinfo,
1259 	    good_data, bad_data, report->zcr_length, drop_if_identical);
1260 	if (info != NULL)
1261 		zfs_zevent_post(report->zcr_ereport,
1262 		    report->zcr_detector, zfs_zevent_post_cb);
1263 	else
1264 		zfs_zevent_post_cb(report->zcr_ereport, report->zcr_detector);
1265 
1266 	report->zcr_ereport = report->zcr_detector = NULL;
1267 	if (info != NULL)
1268 		kmem_free(info, sizeof (*info));
1269 #else
1270 	(void) report, (void) good_data, (void) bad_data,
1271 	    (void) drop_if_identical;
1272 #endif
1273 }
1274 
1275 void
zfs_ereport_free_checksum(zio_cksum_report_t * rpt)1276 zfs_ereport_free_checksum(zio_cksum_report_t *rpt)
1277 {
1278 #ifdef _KERNEL
1279 	if (rpt->zcr_ereport != NULL) {
1280 		fm_nvlist_destroy(rpt->zcr_ereport,
1281 		    FM_NVA_FREE);
1282 		fm_nvlist_destroy(rpt->zcr_detector,
1283 		    FM_NVA_FREE);
1284 	}
1285 #endif
1286 	rpt->zcr_free(rpt->zcr_cbdata, rpt->zcr_cbinfo);
1287 
1288 	if (rpt->zcr_ckinfo != NULL)
1289 		kmem_free(rpt->zcr_ckinfo, sizeof (*rpt->zcr_ckinfo));
1290 
1291 	kmem_free(rpt, sizeof (*rpt));
1292 }
1293 
1294 /*
1295  * Post a checksum ereport
1296  *
1297  * Returns
1298  * - 0 if an event was posted
1299  * - EINVAL if there was a problem posting event
1300  * - EBUSY if the event was rate limited
1301  * - EALREADY if the event was already posted (duplicate)
1302  */
1303 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)1304 zfs_ereport_post_checksum(spa_t *spa, vdev_t *vd, const zbookmark_phys_t *zb,
1305     struct zio *zio, uint64_t offset, uint64_t length,
1306     const abd_t *good_data, const abd_t *bad_data, zio_bad_cksum_t *zbc)
1307 {
1308 	int rc = 0;
1309 #ifdef _KERNEL
1310 	nvlist_t *ereport = NULL;
1311 	nvlist_t *detector = NULL;
1312 	zfs_ecksum_info_t *info;
1313 
1314 	if (!zfs_ereport_is_valid(FM_EREPORT_ZFS_CHECKSUM, spa, vd, zio))
1315 		return (SET_ERROR(EINVAL));
1316 
1317 	if (zfs_ereport_is_duplicate(FM_EREPORT_ZFS_CHECKSUM, spa, vd, zb, zio,
1318 	    offset, length))
1319 		return (SET_ERROR(EALREADY));
1320 
1321 	if (zfs_is_ratelimiting_event(FM_EREPORT_ZFS_CHECKSUM, vd))
1322 		return (SET_ERROR(EBUSY));
1323 
1324 	if (!zfs_ereport_start(&ereport, &detector, FM_EREPORT_ZFS_CHECKSUM,
1325 	    spa, vd, zb, zio, offset, length) || (ereport == NULL)) {
1326 		return (SET_ERROR(EINVAL));
1327 	}
1328 
1329 	info = annotate_ecksum(ereport, zbc, good_data, bad_data, length,
1330 	    B_FALSE);
1331 
1332 	if (info != NULL) {
1333 		rc = zfs_zevent_post(ereport, detector, zfs_zevent_post_cb);
1334 		kmem_free(info, sizeof (*info));
1335 	}
1336 #else
1337 	(void) spa, (void) vd, (void) zb, (void) zio, (void) offset,
1338 	    (void) length, (void) good_data, (void) bad_data, (void) zbc;
1339 #endif
1340 	return (rc);
1341 }
1342 
1343 /*
1344  * The 'sysevent.fs.zfs.*' events are signals posted to notify user space of
1345  * change in the pool.  All sysevents are listed in sys/sysevent/eventdefs.h
1346  * and are designed to be consumed by the ZFS Event Daemon (ZED).  For
1347  * additional details refer to the zed(8) man page.
1348  */
1349 nvlist_t *
zfs_event_create(spa_t * spa,vdev_t * vd,const char * type,const char * name,nvlist_t * aux)1350 zfs_event_create(spa_t *spa, vdev_t *vd, const char *type, const char *name,
1351     nvlist_t *aux)
1352 {
1353 	nvlist_t *resource = NULL;
1354 #ifdef _KERNEL
1355 	char class[64];
1356 
1357 	if (spa_load_state(spa) == SPA_LOAD_TRYIMPORT)
1358 		return (NULL);
1359 
1360 	if ((resource = fm_nvlist_create(NULL)) == NULL)
1361 		return (NULL);
1362 
1363 	(void) snprintf(class, sizeof (class), "%s.%s.%s", type,
1364 	    ZFS_ERROR_CLASS, name);
1365 	VERIFY0(nvlist_add_uint8(resource, FM_VERSION, FM_RSRC_VERSION));
1366 	VERIFY0(nvlist_add_string(resource, FM_CLASS, class));
1367 	VERIFY0(nvlist_add_string(resource,
1368 	    FM_EREPORT_PAYLOAD_ZFS_POOL, spa_name(spa)));
1369 	VERIFY0(nvlist_add_uint64(resource,
1370 	    FM_EREPORT_PAYLOAD_ZFS_POOL_GUID, spa_guid(spa)));
1371 	VERIFY0(nvlist_add_uint64(resource,
1372 	    FM_EREPORT_PAYLOAD_ZFS_POOL_STATE, spa_state(spa)));
1373 	VERIFY0(nvlist_add_int32(resource,
1374 	    FM_EREPORT_PAYLOAD_ZFS_POOL_CONTEXT, spa_load_state(spa)));
1375 
1376 	if (vd) {
1377 		VERIFY0(nvlist_add_uint64(resource,
1378 		    FM_EREPORT_PAYLOAD_ZFS_VDEV_GUID, vd->vdev_guid));
1379 		VERIFY0(nvlist_add_uint64(resource,
1380 		    FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE, vd->vdev_state));
1381 		if (vd->vdev_path != NULL)
1382 			VERIFY0(nvlist_add_string(resource,
1383 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_PATH, vd->vdev_path));
1384 		if (vd->vdev_devid != NULL)
1385 			VERIFY0(nvlist_add_string(resource,
1386 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_DEVID, vd->vdev_devid));
1387 		if (vd->vdev_fru != NULL)
1388 			VERIFY0(nvlist_add_string(resource,
1389 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_FRU, vd->vdev_fru));
1390 		if (vd->vdev_enc_sysfs_path != NULL)
1391 			VERIFY0(nvlist_add_string(resource,
1392 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_ENC_SYSFS_PATH,
1393 			    vd->vdev_enc_sysfs_path));
1394 	}
1395 
1396 	/* also copy any optional payload data */
1397 	if (aux) {
1398 		nvpair_t *elem = NULL;
1399 
1400 		while ((elem = nvlist_next_nvpair(aux, elem)) != NULL)
1401 			(void) nvlist_add_nvpair(resource, elem);
1402 	}
1403 #else
1404 	(void) spa, (void) vd, (void) type, (void) name, (void) aux;
1405 #endif
1406 	return (resource);
1407 }
1408 
1409 static void
zfs_post_common(spa_t * spa,vdev_t * vd,const char * type,const char * name,nvlist_t * aux)1410 zfs_post_common(spa_t *spa, vdev_t *vd, const char *type, const char *name,
1411     nvlist_t *aux)
1412 {
1413 #ifdef _KERNEL
1414 	nvlist_t *resource;
1415 
1416 	resource = zfs_event_create(spa, vd, type, name, aux);
1417 	if (resource)
1418 		zfs_zevent_post(resource, NULL, zfs_zevent_post_cb);
1419 #else
1420 	(void) spa, (void) vd, (void) type, (void) name, (void) aux;
1421 #endif
1422 }
1423 
1424 /*
1425  * The 'resource.fs.zfs.removed' event is an internal signal that the given vdev
1426  * has been removed from the system.  This will cause the DE to ignore any
1427  * recent I/O errors, inferring that they are due to the asynchronous device
1428  * removal.
1429  */
1430 void
zfs_post_remove(spa_t * spa,vdev_t * vd,boolean_t by_kernel)1431 zfs_post_remove(spa_t *spa, vdev_t *vd, boolean_t by_kernel)
1432 {
1433 	nvlist_t *aux = NULL;
1434 
1435 	if (by_kernel) {
1436 		/*
1437 		 * Add optional supplemental keys to payload
1438 		 */
1439 		aux = fm_nvlist_create(NULL);
1440 		if (aux)
1441 			fnvlist_add_boolean(aux, "by_kernel");
1442 	}
1443 
1444 	zfs_post_common(spa, vd, FM_RSRC_CLASS, FM_RESOURCE_REMOVED, aux);
1445 
1446 	if (by_kernel && aux)
1447 		fm_nvlist_destroy(aux, FM_NVA_FREE);
1448 }
1449 
1450 /*
1451  * The 'resource.fs.zfs.autoreplace' event is an internal signal that the pool
1452  * has the 'autoreplace' property set, and therefore any broken vdevs will be
1453  * handled by higher level logic, and no vdev fault should be generated.
1454  */
1455 void
zfs_post_autoreplace(spa_t * spa,vdev_t * vd)1456 zfs_post_autoreplace(spa_t *spa, vdev_t *vd)
1457 {
1458 	zfs_post_common(spa, vd, FM_RSRC_CLASS, FM_RESOURCE_AUTOREPLACE, NULL);
1459 }
1460 
1461 /*
1462  * The 'resource.fs.zfs.statechange' event is an internal signal that the
1463  * given vdev has transitioned its state to DEGRADED or HEALTHY.  This will
1464  * cause the retire agent to repair any outstanding fault management cases
1465  * open because the device was not found (fault.fs.zfs.device).
1466  */
1467 void
zfs_post_state_change(spa_t * spa,vdev_t * vd,uint64_t laststate)1468 zfs_post_state_change(spa_t *spa, vdev_t *vd, uint64_t laststate)
1469 {
1470 #ifdef _KERNEL
1471 	nvlist_t *aux;
1472 
1473 	/*
1474 	 * Add optional supplemental keys to payload
1475 	 */
1476 	aux = fm_nvlist_create(NULL);
1477 	if (vd && aux) {
1478 		if (vd->vdev_physpath) {
1479 			fnvlist_add_string(aux,
1480 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_PHYSPATH,
1481 			    vd->vdev_physpath);
1482 		}
1483 		if (vd->vdev_enc_sysfs_path) {
1484 			fnvlist_add_string(aux,
1485 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_ENC_SYSFS_PATH,
1486 			    vd->vdev_enc_sysfs_path);
1487 		}
1488 
1489 		fnvlist_add_uint64(aux,
1490 		    FM_EREPORT_PAYLOAD_ZFS_VDEV_LASTSTATE, laststate);
1491 	}
1492 
1493 	zfs_post_common(spa, vd, FM_RSRC_CLASS, FM_RESOURCE_STATECHANGE,
1494 	    aux);
1495 
1496 	if (aux)
1497 		fm_nvlist_destroy(aux, FM_NVA_FREE);
1498 #else
1499 	(void) spa, (void) vd, (void) laststate;
1500 #endif
1501 }
1502 
1503 #ifdef _KERNEL
1504 void
zfs_ereport_init(void)1505 zfs_ereport_init(void)
1506 {
1507 	mutex_init(&recent_events_lock, NULL, MUTEX_DEFAULT, NULL);
1508 	list_create(&recent_events_list, sizeof (recent_events_node_t),
1509 	    offsetof(recent_events_node_t, re_list_link));
1510 	avl_create(&recent_events_tree,  recent_events_compare,
1511 	    sizeof (recent_events_node_t), offsetof(recent_events_node_t,
1512 	    re_tree_link));
1513 }
1514 
1515 /*
1516  * This 'early' fini needs to run before zfs_fini() which on Linux waits
1517  * for the system_delay_taskq to drain.
1518  */
1519 void
zfs_ereport_taskq_fini(void)1520 zfs_ereport_taskq_fini(void)
1521 {
1522 	mutex_enter(&recent_events_lock);
1523 	if (recent_events_cleaner_tqid != 0) {
1524 		taskq_cancel_id(system_delay_taskq, recent_events_cleaner_tqid,
1525 		    B_TRUE);
1526 		recent_events_cleaner_tqid = 0;
1527 	}
1528 	mutex_exit(&recent_events_lock);
1529 }
1530 
1531 void
zfs_ereport_fini(void)1532 zfs_ereport_fini(void)
1533 {
1534 	recent_events_node_t *entry;
1535 
1536 	while ((entry = list_remove_head(&recent_events_list)) != NULL) {
1537 		avl_remove(&recent_events_tree, entry);
1538 		kmem_free(entry, sizeof (*entry));
1539 	}
1540 	avl_destroy(&recent_events_tree);
1541 	list_destroy(&recent_events_list);
1542 	mutex_destroy(&recent_events_lock);
1543 }
1544 
1545 void
zfs_ereport_snapshot_post(const char * subclass,spa_t * spa,const char * name)1546 zfs_ereport_snapshot_post(const char *subclass, spa_t *spa, const char *name)
1547 {
1548 	nvlist_t *aux;
1549 
1550 	aux = fm_nvlist_create(NULL);
1551 	fnvlist_add_string(aux, FM_EREPORT_PAYLOAD_ZFS_SNAPSHOT_NAME, name);
1552 
1553 	zfs_post_common(spa, NULL, FM_RSRC_CLASS, subclass, aux);
1554 	fm_nvlist_destroy(aux, FM_NVA_FREE);
1555 }
1556 
1557 /*
1558  * Post when a event when a zvol is created or removed
1559  *
1560  * This is currently only used by macOS, since it uses the event to create
1561  * symlinks between the volume name (mypool/myvol) and the actual /dev
1562  * device (/dev/disk3).  For example:
1563  *
1564  * /var/run/zfs/dsk/mypool/myvol -> /dev/disk3
1565  *
1566  * name: The full name of the zvol ("mypool/myvol")
1567  * dev_name: The full /dev name for the zvol ("/dev/disk3")
1568  * raw_name: The raw  /dev name for the zvol ("/dev/rdisk3")
1569  */
1570 void
zfs_ereport_zvol_post(const char * subclass,const char * name,const char * dev_name,const char * raw_name)1571 zfs_ereport_zvol_post(const char *subclass, const char *name,
1572     const char *dev_name, const char *raw_name)
1573 {
1574 	nvlist_t *aux;
1575 	char *r;
1576 
1577 	boolean_t locked = spa_namespace_held();
1578 	if (!locked) spa_namespace_enter(FTAG);
1579 	spa_t *spa = spa_lookup(name);
1580 	if (!locked) spa_namespace_exit(FTAG);
1581 
1582 	if (spa == NULL)
1583 		return;
1584 
1585 	aux = fm_nvlist_create(NULL);
1586 	fnvlist_add_string(aux, FM_EREPORT_PAYLOAD_ZFS_DEVICE_NAME, dev_name);
1587 	fnvlist_add_string(aux, FM_EREPORT_PAYLOAD_ZFS_RAW_DEVICE_NAME,
1588 	    raw_name);
1589 	r = strchr(name, '/');
1590 	if (r && r[1])
1591 		fnvlist_add_string(aux, FM_EREPORT_PAYLOAD_ZFS_VOLUME, &r[1]);
1592 
1593 	zfs_post_common(spa, NULL, FM_RSRC_CLASS, subclass, aux);
1594 	fm_nvlist_destroy(aux, FM_NVA_FREE);
1595 }
1596 
1597 EXPORT_SYMBOL(zfs_ereport_post);
1598 EXPORT_SYMBOL(zfs_ereport_is_valid);
1599 EXPORT_SYMBOL(zfs_ereport_post_checksum);
1600 EXPORT_SYMBOL(zfs_post_remove);
1601 EXPORT_SYMBOL(zfs_post_autoreplace);
1602 EXPORT_SYMBOL(zfs_post_state_change);
1603 
1604 ZFS_MODULE_PARAM(zfs_zevent, zfs_zevent_, retain_max, UINT, ZMOD_RW,
1605 	"Maximum recent zevents records to retain for duplicate checking");
1606 ZFS_MODULE_PARAM(zfs_zevent, zfs_zevent_, retain_expire_secs, UINT, ZMOD_RW,
1607 	"Expiration time for recent zevents records");
1608 #endif /* _KERNEL */
1609