xref: /freebsd/sys/contrib/openzfs/cmd/zed/agents/zfs_diagnosis.c (revision d5b0e70f7e04d971691517ce1304d86a1e367e2e)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or https://opensource.org/licenses/CDDL-1.0.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
25  * Copyright (c) 2016, Intel Corporation.
26  */
27 
28 #include <stddef.h>
29 #include <string.h>
30 #include <libuutil.h>
31 #include <libzfs.h>
32 #include <sys/types.h>
33 #include <sys/time.h>
34 #include <sys/fs/zfs.h>
35 #include <sys/fm/protocol.h>
36 #include <sys/fm/fs/zfs.h>
37 #include <sys/zio.h>
38 
39 #include "zfs_agents.h"
40 #include "fmd_api.h"
41 
42 /*
43  * Our serd engines are named 'zfs_<pool_guid>_<vdev_guid>_{checksum,io}'.  This
44  * #define reserves enough space for two 64-bit hex values plus the length of
45  * the longest string.
46  */
47 #define	MAX_SERDLEN	(16 * 2 + sizeof ("zfs___checksum"))
48 
49 /*
50  * On-disk case structure.  This must maintain backwards compatibility with
51  * previous versions of the DE.  By default, any members appended to the end
52  * will be filled with zeros if they don't exist in a previous version.
53  */
54 typedef struct zfs_case_data {
55 	uint64_t	zc_version;
56 	uint64_t	zc_ena;
57 	uint64_t	zc_pool_guid;
58 	uint64_t	zc_vdev_guid;
59 	int		zc_pool_state;
60 	char		zc_serd_checksum[MAX_SERDLEN];
61 	char		zc_serd_io[MAX_SERDLEN];
62 	int		zc_has_remove_timer;
63 } zfs_case_data_t;
64 
65 /*
66  * Time-of-day
67  */
68 typedef struct er_timeval {
69 	uint64_t	ertv_sec;
70 	uint64_t	ertv_nsec;
71 } er_timeval_t;
72 
73 /*
74  * In-core case structure.
75  */
76 typedef struct zfs_case {
77 	boolean_t	zc_present;
78 	uint32_t	zc_version;
79 	zfs_case_data_t	zc_data;
80 	fmd_case_t	*zc_case;
81 	uu_list_node_t	zc_node;
82 	id_t		zc_remove_timer;
83 	char		*zc_fru;
84 	er_timeval_t	zc_when;
85 } zfs_case_t;
86 
87 #define	CASE_DATA			"data"
88 #define	CASE_FRU			"fru"
89 #define	CASE_DATA_VERSION_INITIAL	1
90 #define	CASE_DATA_VERSION_SERD		2
91 
92 typedef struct zfs_de_stats {
93 	fmd_stat_t	old_drops;
94 	fmd_stat_t	dev_drops;
95 	fmd_stat_t	vdev_drops;
96 	fmd_stat_t	import_drops;
97 	fmd_stat_t	resource_drops;
98 } zfs_de_stats_t;
99 
100 zfs_de_stats_t zfs_stats = {
101 	{ "old_drops", FMD_TYPE_UINT64, "ereports dropped (from before load)" },
102 	{ "dev_drops", FMD_TYPE_UINT64, "ereports dropped (dev during open)"},
103 	{ "vdev_drops", FMD_TYPE_UINT64, "ereports dropped (weird vdev types)"},
104 	{ "import_drops", FMD_TYPE_UINT64, "ereports dropped (during import)" },
105 	{ "resource_drops", FMD_TYPE_UINT64, "resource related ereports" }
106 };
107 
108 static hrtime_t zfs_remove_timeout;
109 
110 uu_list_pool_t *zfs_case_pool;
111 uu_list_t *zfs_cases;
112 
113 #define	ZFS_MAKE_RSRC(type)	\
114     FM_RSRC_CLASS "." ZFS_ERROR_CLASS "." type
115 #define	ZFS_MAKE_EREPORT(type)	\
116     FM_EREPORT_CLASS "." ZFS_ERROR_CLASS "." type
117 
118 /*
119  * Write out the persistent representation of an active case.
120  */
121 static void
122 zfs_case_serialize(zfs_case_t *zcp)
123 {
124 	zcp->zc_data.zc_version = CASE_DATA_VERSION_SERD;
125 }
126 
127 /*
128  * Read back the persistent representation of an active case.
129  */
130 static zfs_case_t *
131 zfs_case_unserialize(fmd_hdl_t *hdl, fmd_case_t *cp)
132 {
133 	zfs_case_t *zcp;
134 
135 	zcp = fmd_hdl_zalloc(hdl, sizeof (zfs_case_t), FMD_SLEEP);
136 	zcp->zc_case = cp;
137 
138 	fmd_buf_read(hdl, cp, CASE_DATA, &zcp->zc_data,
139 	    sizeof (zcp->zc_data));
140 
141 	if (zcp->zc_data.zc_version > CASE_DATA_VERSION_SERD) {
142 		fmd_hdl_free(hdl, zcp, sizeof (zfs_case_t));
143 		return (NULL);
144 	}
145 
146 	/*
147 	 * fmd_buf_read() will have already zeroed out the remainder of the
148 	 * buffer, so we don't have to do anything special if the version
149 	 * doesn't include the SERD engine name.
150 	 */
151 
152 	if (zcp->zc_data.zc_has_remove_timer)
153 		zcp->zc_remove_timer = fmd_timer_install(hdl, zcp,
154 		    NULL, zfs_remove_timeout);
155 
156 	uu_list_node_init(zcp, &zcp->zc_node, zfs_case_pool);
157 	(void) uu_list_insert_before(zfs_cases, NULL, zcp);
158 
159 	fmd_case_setspecific(hdl, cp, zcp);
160 
161 	return (zcp);
162 }
163 
164 /*
165  * Iterate over any active cases.  If any cases are associated with a pool or
166  * vdev which is no longer present on the system, close the associated case.
167  */
168 static void
169 zfs_mark_vdev(uint64_t pool_guid, nvlist_t *vd, er_timeval_t *loaded)
170 {
171 	uint64_t vdev_guid = 0;
172 	uint_t c, children;
173 	nvlist_t **child;
174 	zfs_case_t *zcp;
175 
176 	(void) nvlist_lookup_uint64(vd, ZPOOL_CONFIG_GUID, &vdev_guid);
177 
178 	/*
179 	 * Mark any cases associated with this (pool, vdev) pair.
180 	 */
181 	for (zcp = uu_list_first(zfs_cases); zcp != NULL;
182 	    zcp = uu_list_next(zfs_cases, zcp)) {
183 		if (zcp->zc_data.zc_pool_guid == pool_guid &&
184 		    zcp->zc_data.zc_vdev_guid == vdev_guid) {
185 			zcp->zc_present = B_TRUE;
186 			zcp->zc_when = *loaded;
187 		}
188 	}
189 
190 	/*
191 	 * Iterate over all children.
192 	 */
193 	if (nvlist_lookup_nvlist_array(vd, ZPOOL_CONFIG_CHILDREN, &child,
194 	    &children) == 0) {
195 		for (c = 0; c < children; c++)
196 			zfs_mark_vdev(pool_guid, child[c], loaded);
197 	}
198 
199 	if (nvlist_lookup_nvlist_array(vd, ZPOOL_CONFIG_L2CACHE, &child,
200 	    &children) == 0) {
201 		for (c = 0; c < children; c++)
202 			zfs_mark_vdev(pool_guid, child[c], loaded);
203 	}
204 
205 	if (nvlist_lookup_nvlist_array(vd, ZPOOL_CONFIG_SPARES, &child,
206 	    &children) == 0) {
207 		for (c = 0; c < children; c++)
208 			zfs_mark_vdev(pool_guid, child[c], loaded);
209 	}
210 }
211 
212 static int
213 zfs_mark_pool(zpool_handle_t *zhp, void *unused)
214 {
215 	(void) unused;
216 	zfs_case_t *zcp;
217 	uint64_t pool_guid;
218 	uint64_t *tod;
219 	er_timeval_t loaded = { 0 };
220 	nvlist_t *config, *vd;
221 	uint_t nelem = 0;
222 	int ret;
223 
224 	pool_guid = zpool_get_prop_int(zhp, ZPOOL_PROP_GUID, NULL);
225 	/*
226 	 * Mark any cases associated with just this pool.
227 	 */
228 	for (zcp = uu_list_first(zfs_cases); zcp != NULL;
229 	    zcp = uu_list_next(zfs_cases, zcp)) {
230 		if (zcp->zc_data.zc_pool_guid == pool_guid &&
231 		    zcp->zc_data.zc_vdev_guid == 0)
232 			zcp->zc_present = B_TRUE;
233 	}
234 
235 	if ((config = zpool_get_config(zhp, NULL)) == NULL) {
236 		zpool_close(zhp);
237 		return (-1);
238 	}
239 
240 	(void) nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_LOADED_TIME,
241 	    &tod, &nelem);
242 	if (nelem == 2) {
243 		loaded.ertv_sec = tod[0];
244 		loaded.ertv_nsec = tod[1];
245 		for (zcp = uu_list_first(zfs_cases); zcp != NULL;
246 		    zcp = uu_list_next(zfs_cases, zcp)) {
247 			if (zcp->zc_data.zc_pool_guid == pool_guid &&
248 			    zcp->zc_data.zc_vdev_guid == 0) {
249 				zcp->zc_when = loaded;
250 			}
251 		}
252 	}
253 
254 	ret = nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &vd);
255 	if (ret) {
256 		zpool_close(zhp);
257 		return (-1);
258 	}
259 
260 	zfs_mark_vdev(pool_guid, vd, &loaded);
261 
262 	zpool_close(zhp);
263 
264 	return (0);
265 }
266 
267 struct load_time_arg {
268 	uint64_t lt_guid;
269 	er_timeval_t *lt_time;
270 	boolean_t lt_found;
271 };
272 
273 static int
274 zpool_find_load_time(zpool_handle_t *zhp, void *arg)
275 {
276 	struct load_time_arg *lta = arg;
277 	uint64_t pool_guid;
278 	uint64_t *tod;
279 	nvlist_t *config;
280 	uint_t nelem;
281 
282 	if (lta->lt_found) {
283 		zpool_close(zhp);
284 		return (0);
285 	}
286 
287 	pool_guid = zpool_get_prop_int(zhp, ZPOOL_PROP_GUID, NULL);
288 	if (pool_guid != lta->lt_guid) {
289 		zpool_close(zhp);
290 		return (0);
291 	}
292 
293 	if ((config = zpool_get_config(zhp, NULL)) == NULL) {
294 		zpool_close(zhp);
295 		return (-1);
296 	}
297 
298 	if (nvlist_lookup_uint64_array(config, ZPOOL_CONFIG_LOADED_TIME,
299 	    &tod, &nelem) == 0 && nelem == 2) {
300 		lta->lt_found = B_TRUE;
301 		lta->lt_time->ertv_sec = tod[0];
302 		lta->lt_time->ertv_nsec = tod[1];
303 	}
304 
305 	zpool_close(zhp);
306 
307 	return (0);
308 }
309 
310 static void
311 zfs_purge_cases(fmd_hdl_t *hdl)
312 {
313 	zfs_case_t *zcp;
314 	uu_list_walk_t *walk;
315 	libzfs_handle_t *zhdl = fmd_hdl_getspecific(hdl);
316 
317 	/*
318 	 * There is no way to open a pool by GUID, or lookup a vdev by GUID.  No
319 	 * matter what we do, we're going to have to stomach an O(vdevs * cases)
320 	 * algorithm.  In reality, both quantities are likely so small that
321 	 * neither will matter. Given that iterating over pools is more
322 	 * expensive than iterating over the in-memory case list, we opt for a
323 	 * 'present' flag in each case that starts off cleared.  We then iterate
324 	 * over all pools, marking those that are still present, and removing
325 	 * those that aren't found.
326 	 *
327 	 * Note that we could also construct an FMRI and rely on
328 	 * fmd_nvl_fmri_present(), but this would end up doing the same search.
329 	 */
330 
331 	/*
332 	 * Mark the cases as not present.
333 	 */
334 	for (zcp = uu_list_first(zfs_cases); zcp != NULL;
335 	    zcp = uu_list_next(zfs_cases, zcp))
336 		zcp->zc_present = B_FALSE;
337 
338 	/*
339 	 * Iterate over all pools and mark the pools and vdevs found.  If this
340 	 * fails (most probably because we're out of memory), then don't close
341 	 * any of the cases and we cannot be sure they are accurate.
342 	 */
343 	if (zpool_iter(zhdl, zfs_mark_pool, NULL) != 0)
344 		return;
345 
346 	/*
347 	 * Remove those cases which were not found.
348 	 */
349 	walk = uu_list_walk_start(zfs_cases, UU_WALK_ROBUST);
350 	while ((zcp = uu_list_walk_next(walk)) != NULL) {
351 		if (!zcp->zc_present)
352 			fmd_case_close(hdl, zcp->zc_case);
353 	}
354 	uu_list_walk_end(walk);
355 }
356 
357 /*
358  * Construct the name of a serd engine given the pool/vdev GUID and type (io or
359  * checksum).
360  */
361 static void
362 zfs_serd_name(char *buf, uint64_t pool_guid, uint64_t vdev_guid,
363     const char *type)
364 {
365 	(void) snprintf(buf, MAX_SERDLEN, "zfs_%llx_%llx_%s",
366 	    (long long unsigned int)pool_guid,
367 	    (long long unsigned int)vdev_guid, type);
368 }
369 
370 /*
371  * Solve a given ZFS case.  This first checks to make sure the diagnosis is
372  * still valid, as well as cleaning up any pending timer associated with the
373  * case.
374  */
375 static void
376 zfs_case_solve(fmd_hdl_t *hdl, zfs_case_t *zcp, const char *faultname)
377 {
378 	nvlist_t *detector, *fault;
379 	boolean_t serialize;
380 	nvlist_t *fru = NULL;
381 	fmd_hdl_debug(hdl, "solving fault '%s'", faultname);
382 
383 	/*
384 	 * Construct the detector from the case data.  The detector is in the
385 	 * ZFS scheme, and is either the pool or the vdev, depending on whether
386 	 * this is a vdev or pool fault.
387 	 */
388 	detector = fmd_nvl_alloc(hdl, FMD_SLEEP);
389 
390 	(void) nvlist_add_uint8(detector, FM_VERSION, ZFS_SCHEME_VERSION0);
391 	(void) nvlist_add_string(detector, FM_FMRI_SCHEME, FM_FMRI_SCHEME_ZFS);
392 	(void) nvlist_add_uint64(detector, FM_FMRI_ZFS_POOL,
393 	    zcp->zc_data.zc_pool_guid);
394 	if (zcp->zc_data.zc_vdev_guid != 0) {
395 		(void) nvlist_add_uint64(detector, FM_FMRI_ZFS_VDEV,
396 		    zcp->zc_data.zc_vdev_guid);
397 	}
398 
399 	fault = fmd_nvl_create_fault(hdl, faultname, 100, detector,
400 	    fru, detector);
401 	fmd_case_add_suspect(hdl, zcp->zc_case, fault);
402 
403 	nvlist_free(fru);
404 
405 	fmd_case_solve(hdl, zcp->zc_case);
406 
407 	serialize = B_FALSE;
408 	if (zcp->zc_data.zc_has_remove_timer) {
409 		fmd_timer_remove(hdl, zcp->zc_remove_timer);
410 		zcp->zc_data.zc_has_remove_timer = 0;
411 		serialize = B_TRUE;
412 	}
413 	if (serialize)
414 		zfs_case_serialize(zcp);
415 
416 	nvlist_free(detector);
417 }
418 
419 static boolean_t
420 timeval_earlier(er_timeval_t *a, er_timeval_t *b)
421 {
422 	return (a->ertv_sec < b->ertv_sec ||
423 	    (a->ertv_sec == b->ertv_sec && a->ertv_nsec < b->ertv_nsec));
424 }
425 
426 static void
427 zfs_ereport_when(fmd_hdl_t *hdl, nvlist_t *nvl, er_timeval_t *when)
428 {
429 	(void) hdl;
430 	int64_t *tod;
431 	uint_t	nelem;
432 
433 	if (nvlist_lookup_int64_array(nvl, FM_EREPORT_TIME, &tod,
434 	    &nelem) == 0 && nelem == 2) {
435 		when->ertv_sec = tod[0];
436 		when->ertv_nsec = tod[1];
437 	} else {
438 		when->ertv_sec = when->ertv_nsec = UINT64_MAX;
439 	}
440 }
441 
442 /*
443  * Main fmd entry point.
444  */
445 static void
446 zfs_fm_recv(fmd_hdl_t *hdl, fmd_event_t *ep, nvlist_t *nvl, const char *class)
447 {
448 	zfs_case_t *zcp, *dcp;
449 	int32_t pool_state;
450 	uint64_t ena, pool_guid, vdev_guid;
451 	er_timeval_t pool_load;
452 	er_timeval_t er_when;
453 	nvlist_t *detector;
454 	boolean_t pool_found = B_FALSE;
455 	boolean_t isresource;
456 	char *type;
457 
458 	/*
459 	 * We subscribe to notifications for vdev or pool removal.  In these
460 	 * cases, there may be cases that no longer apply.  Purge any cases
461 	 * that no longer apply.
462 	 */
463 	if (fmd_nvl_class_match(hdl, nvl, "sysevent.fs.zfs.*")) {
464 		fmd_hdl_debug(hdl, "purging orphaned cases from %s",
465 		    strrchr(class, '.') + 1);
466 		zfs_purge_cases(hdl);
467 		zfs_stats.resource_drops.fmds_value.ui64++;
468 		return;
469 	}
470 
471 	isresource = fmd_nvl_class_match(hdl, nvl, "resource.fs.zfs.*");
472 
473 	if (isresource) {
474 		/*
475 		 * For resources, we don't have a normal payload.
476 		 */
477 		if (nvlist_lookup_uint64(nvl, FM_EREPORT_PAYLOAD_ZFS_VDEV_GUID,
478 		    &vdev_guid) != 0)
479 			pool_state = SPA_LOAD_OPEN;
480 		else
481 			pool_state = SPA_LOAD_NONE;
482 		detector = NULL;
483 	} else {
484 		(void) nvlist_lookup_nvlist(nvl,
485 		    FM_EREPORT_DETECTOR, &detector);
486 		(void) nvlist_lookup_int32(nvl,
487 		    FM_EREPORT_PAYLOAD_ZFS_POOL_CONTEXT, &pool_state);
488 	}
489 
490 	/*
491 	 * We also ignore all ereports generated during an import of a pool,
492 	 * since the only possible fault (.pool) would result in import failure,
493 	 * and hence no persistent fault.  Some day we may want to do something
494 	 * with these ereports, so we continue generating them internally.
495 	 */
496 	if (pool_state == SPA_LOAD_IMPORT) {
497 		zfs_stats.import_drops.fmds_value.ui64++;
498 		fmd_hdl_debug(hdl, "ignoring '%s' during import", class);
499 		return;
500 	}
501 
502 	/*
503 	 * Device I/O errors are ignored during pool open.
504 	 */
505 	if (pool_state == SPA_LOAD_OPEN &&
506 	    (fmd_nvl_class_match(hdl, nvl,
507 	    ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_CHECKSUM)) ||
508 	    fmd_nvl_class_match(hdl, nvl,
509 	    ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_IO)) ||
510 	    fmd_nvl_class_match(hdl, nvl,
511 	    ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_PROBE_FAILURE)))) {
512 		fmd_hdl_debug(hdl, "ignoring '%s' during pool open", class);
513 		zfs_stats.dev_drops.fmds_value.ui64++;
514 		return;
515 	}
516 
517 	/*
518 	 * We ignore ereports for anything except disks and files.
519 	 */
520 	if (nvlist_lookup_string(nvl, FM_EREPORT_PAYLOAD_ZFS_VDEV_TYPE,
521 	    &type) == 0) {
522 		if (strcmp(type, VDEV_TYPE_DISK) != 0 &&
523 		    strcmp(type, VDEV_TYPE_FILE) != 0) {
524 			zfs_stats.vdev_drops.fmds_value.ui64++;
525 			return;
526 		}
527 	}
528 
529 	/*
530 	 * Determine if this ereport corresponds to an open case.
531 	 * Each vdev or pool can have a single case.
532 	 */
533 	(void) nvlist_lookup_uint64(nvl,
534 	    FM_EREPORT_PAYLOAD_ZFS_POOL_GUID, &pool_guid);
535 	if (nvlist_lookup_uint64(nvl,
536 	    FM_EREPORT_PAYLOAD_ZFS_VDEV_GUID, &vdev_guid) != 0)
537 		vdev_guid = 0;
538 	if (nvlist_lookup_uint64(nvl, FM_EREPORT_ENA, &ena) != 0)
539 		ena = 0;
540 
541 	zfs_ereport_when(hdl, nvl, &er_when);
542 
543 	for (zcp = uu_list_first(zfs_cases); zcp != NULL;
544 	    zcp = uu_list_next(zfs_cases, zcp)) {
545 		if (zcp->zc_data.zc_pool_guid == pool_guid) {
546 			pool_found = B_TRUE;
547 			pool_load = zcp->zc_when;
548 		}
549 		if (zcp->zc_data.zc_vdev_guid == vdev_guid)
550 			break;
551 	}
552 
553 	/*
554 	 * Avoid falsely accusing a pool of being faulty.  Do so by
555 	 * not replaying ereports that were generated prior to the
556 	 * current import.  If the failure that generated them was
557 	 * transient because the device was actually removed but we
558 	 * didn't receive the normal asynchronous notification, we
559 	 * don't want to mark it as faulted and potentially panic. If
560 	 * there is still a problem we'd expect not to be able to
561 	 * import the pool, or that new ereports will be generated
562 	 * once the pool is used.
563 	 */
564 	if (pool_found && timeval_earlier(&er_when, &pool_load)) {
565 		fmd_hdl_debug(hdl, "ignoring pool %llx, "
566 		    "ereport time %lld.%lld, pool load time = %lld.%lld",
567 		    pool_guid, er_when.ertv_sec, er_when.ertv_nsec,
568 		    pool_load.ertv_sec, pool_load.ertv_nsec);
569 		zfs_stats.old_drops.fmds_value.ui64++;
570 		return;
571 	}
572 
573 	if (!pool_found) {
574 		/*
575 		 * Haven't yet seen this pool, but same situation
576 		 * may apply.
577 		 */
578 		libzfs_handle_t *zhdl = fmd_hdl_getspecific(hdl);
579 		struct load_time_arg la;
580 
581 		la.lt_guid = pool_guid;
582 		la.lt_time = &pool_load;
583 		la.lt_found = B_FALSE;
584 
585 		if (zhdl != NULL &&
586 		    zpool_iter(zhdl, zpool_find_load_time, &la) == 0 &&
587 		    la.lt_found == B_TRUE) {
588 			pool_found = B_TRUE;
589 
590 			if (timeval_earlier(&er_when, &pool_load)) {
591 				fmd_hdl_debug(hdl, "ignoring pool %llx, "
592 				    "ereport time %lld.%lld, "
593 				    "pool load time = %lld.%lld",
594 				    pool_guid, er_when.ertv_sec,
595 				    er_when.ertv_nsec, pool_load.ertv_sec,
596 				    pool_load.ertv_nsec);
597 				zfs_stats.old_drops.fmds_value.ui64++;
598 				return;
599 			}
600 		}
601 	}
602 
603 	if (zcp == NULL) {
604 		fmd_case_t *cs;
605 		zfs_case_data_t data = { 0 };
606 
607 		/*
608 		 * If this is one of our 'fake' resource ereports, and there is
609 		 * no case open, simply discard it.
610 		 */
611 		if (isresource) {
612 			zfs_stats.resource_drops.fmds_value.ui64++;
613 			fmd_hdl_debug(hdl, "discarding '%s for vdev %llu",
614 			    class, vdev_guid);
615 			return;
616 		}
617 
618 		/*
619 		 * Skip tracking some ereports
620 		 */
621 		if (strcmp(class,
622 		    ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_DATA)) == 0 ||
623 		    strcmp(class,
624 		    ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_CONFIG_CACHE_WRITE)) == 0 ||
625 		    strcmp(class,
626 		    ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_DELAY)) == 0) {
627 			zfs_stats.resource_drops.fmds_value.ui64++;
628 			return;
629 		}
630 
631 		/*
632 		 * Open a new case.
633 		 */
634 		cs = fmd_case_open(hdl, NULL);
635 
636 		fmd_hdl_debug(hdl, "opening case for vdev %llu due to '%s'",
637 		    vdev_guid, class);
638 
639 		/*
640 		 * Initialize the case buffer.  To commonize code, we actually
641 		 * create the buffer with existing data, and then call
642 		 * zfs_case_unserialize() to instantiate the in-core structure.
643 		 */
644 		fmd_buf_create(hdl, cs, CASE_DATA, sizeof (zfs_case_data_t));
645 
646 		data.zc_version = CASE_DATA_VERSION_SERD;
647 		data.zc_ena = ena;
648 		data.zc_pool_guid = pool_guid;
649 		data.zc_vdev_guid = vdev_guid;
650 		data.zc_pool_state = (int)pool_state;
651 
652 		fmd_buf_write(hdl, cs, CASE_DATA, &data, sizeof (data));
653 
654 		zcp = zfs_case_unserialize(hdl, cs);
655 		assert(zcp != NULL);
656 		if (pool_found)
657 			zcp->zc_when = pool_load;
658 	}
659 
660 	if (isresource) {
661 		fmd_hdl_debug(hdl, "resource event '%s'", class);
662 
663 		if (fmd_nvl_class_match(hdl, nvl,
664 		    ZFS_MAKE_RSRC(FM_RESOURCE_AUTOREPLACE))) {
665 			/*
666 			 * The 'resource.fs.zfs.autoreplace' event indicates
667 			 * that the pool was loaded with the 'autoreplace'
668 			 * property set.  In this case, any pending device
669 			 * failures should be ignored, as the asynchronous
670 			 * autoreplace handling will take care of them.
671 			 */
672 			fmd_case_close(hdl, zcp->zc_case);
673 		} else if (fmd_nvl_class_match(hdl, nvl,
674 		    ZFS_MAKE_RSRC(FM_RESOURCE_REMOVED))) {
675 			/*
676 			 * The 'resource.fs.zfs.removed' event indicates that
677 			 * device removal was detected, and the device was
678 			 * closed asynchronously.  If this is the case, we
679 			 * assume that any recent I/O errors were due to the
680 			 * device removal, not any fault of the device itself.
681 			 * We reset the SERD engine, and cancel any pending
682 			 * timers.
683 			 */
684 			if (zcp->zc_data.zc_has_remove_timer) {
685 				fmd_timer_remove(hdl, zcp->zc_remove_timer);
686 				zcp->zc_data.zc_has_remove_timer = 0;
687 				zfs_case_serialize(zcp);
688 			}
689 			if (zcp->zc_data.zc_serd_io[0] != '\0')
690 				fmd_serd_reset(hdl, zcp->zc_data.zc_serd_io);
691 			if (zcp->zc_data.zc_serd_checksum[0] != '\0')
692 				fmd_serd_reset(hdl,
693 				    zcp->zc_data.zc_serd_checksum);
694 		} else if (fmd_nvl_class_match(hdl, nvl,
695 		    ZFS_MAKE_RSRC(FM_RESOURCE_STATECHANGE))) {
696 			uint64_t state = 0;
697 
698 			if (zcp != NULL &&
699 			    nvlist_lookup_uint64(nvl,
700 			    FM_EREPORT_PAYLOAD_ZFS_VDEV_STATE, &state) == 0 &&
701 			    state == VDEV_STATE_HEALTHY) {
702 				fmd_hdl_debug(hdl, "closing case after a "
703 				    "device statechange to healthy");
704 				fmd_case_close(hdl, zcp->zc_case);
705 			}
706 		}
707 		zfs_stats.resource_drops.fmds_value.ui64++;
708 		return;
709 	}
710 
711 	/*
712 	 * Associate the ereport with this case.
713 	 */
714 	fmd_case_add_ereport(hdl, zcp->zc_case, ep);
715 
716 	/*
717 	 * Don't do anything else if this case is already solved.
718 	 */
719 	if (fmd_case_solved(hdl, zcp->zc_case))
720 		return;
721 
722 	fmd_hdl_debug(hdl, "error event '%s'", class);
723 
724 	/*
725 	 * Determine if we should solve the case and generate a fault.  We solve
726 	 * a case if:
727 	 *
728 	 * 	a. A pool failed to open (ereport.fs.zfs.pool)
729 	 * 	b. A device failed to open (ereport.fs.zfs.pool) while a pool
730 	 *	   was up and running.
731 	 *
732 	 * We may see a series of ereports associated with a pool open, all
733 	 * chained together by the same ENA.  If the pool open succeeds, then
734 	 * we'll see no further ereports.  To detect when a pool open has
735 	 * succeeded, we associate a timer with the event.  When it expires, we
736 	 * close the case.
737 	 */
738 	if (fmd_nvl_class_match(hdl, nvl,
739 	    ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_POOL))) {
740 		/*
741 		 * Pool level fault.  Before solving the case, go through and
742 		 * close any open device cases that may be pending.
743 		 */
744 		for (dcp = uu_list_first(zfs_cases); dcp != NULL;
745 		    dcp = uu_list_next(zfs_cases, dcp)) {
746 			if (dcp->zc_data.zc_pool_guid ==
747 			    zcp->zc_data.zc_pool_guid &&
748 			    dcp->zc_data.zc_vdev_guid != 0)
749 				fmd_case_close(hdl, dcp->zc_case);
750 		}
751 
752 		zfs_case_solve(hdl, zcp, "fault.fs.zfs.pool");
753 	} else if (fmd_nvl_class_match(hdl, nvl,
754 	    ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_LOG_REPLAY))) {
755 		/*
756 		 * Pool level fault for reading the intent logs.
757 		 */
758 		zfs_case_solve(hdl, zcp, "fault.fs.zfs.log_replay");
759 	} else if (fmd_nvl_class_match(hdl, nvl, "ereport.fs.zfs.vdev.*")) {
760 		/*
761 		 * Device fault.
762 		 */
763 		zfs_case_solve(hdl, zcp, "fault.fs.zfs.device");
764 	} else if (fmd_nvl_class_match(hdl, nvl,
765 	    ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_IO)) ||
766 	    fmd_nvl_class_match(hdl, nvl,
767 	    ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_CHECKSUM)) ||
768 	    fmd_nvl_class_match(hdl, nvl,
769 	    ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_IO_FAILURE)) ||
770 	    fmd_nvl_class_match(hdl, nvl,
771 	    ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_PROBE_FAILURE))) {
772 		char *failmode = NULL;
773 		boolean_t checkremove = B_FALSE;
774 		uint32_t pri = 0;
775 		int32_t flags = 0;
776 
777 		/*
778 		 * If this is a checksum or I/O error, then toss it into the
779 		 * appropriate SERD engine and check to see if it has fired.
780 		 * Ideally, we want to do something more sophisticated,
781 		 * (persistent errors for a single data block, etc).  For now,
782 		 * a single SERD engine is sufficient.
783 		 */
784 		if (fmd_nvl_class_match(hdl, nvl,
785 		    ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_IO))) {
786 			if (zcp->zc_data.zc_serd_io[0] == '\0') {
787 				zfs_serd_name(zcp->zc_data.zc_serd_io,
788 				    pool_guid, vdev_guid, "io");
789 				fmd_serd_create(hdl, zcp->zc_data.zc_serd_io,
790 				    fmd_prop_get_int32(hdl, "io_N"),
791 				    fmd_prop_get_int64(hdl, "io_T"));
792 				zfs_case_serialize(zcp);
793 			}
794 			if (fmd_serd_record(hdl, zcp->zc_data.zc_serd_io, ep))
795 				checkremove = B_TRUE;
796 		} else if (fmd_nvl_class_match(hdl, nvl,
797 		    ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_CHECKSUM))) {
798 			/*
799 			 * We ignore ereports for checksum errors generated by
800 			 * scrub/resilver I/O to avoid potentially further
801 			 * degrading the pool while it's being repaired.
802 			 */
803 			if (((nvlist_lookup_uint32(nvl,
804 			    FM_EREPORT_PAYLOAD_ZFS_ZIO_PRIORITY, &pri) == 0) &&
805 			    (pri == ZIO_PRIORITY_SCRUB ||
806 			    pri == ZIO_PRIORITY_REBUILD)) ||
807 			    ((nvlist_lookup_int32(nvl,
808 			    FM_EREPORT_PAYLOAD_ZFS_ZIO_FLAGS, &flags) == 0) &&
809 			    (flags & (ZIO_FLAG_SCRUB | ZIO_FLAG_RESILVER)))) {
810 				fmd_hdl_debug(hdl, "ignoring '%s' for "
811 				    "scrub/resilver I/O", class);
812 				return;
813 			}
814 
815 			if (zcp->zc_data.zc_serd_checksum[0] == '\0') {
816 				zfs_serd_name(zcp->zc_data.zc_serd_checksum,
817 				    pool_guid, vdev_guid, "checksum");
818 				fmd_serd_create(hdl,
819 				    zcp->zc_data.zc_serd_checksum,
820 				    fmd_prop_get_int32(hdl, "checksum_N"),
821 				    fmd_prop_get_int64(hdl, "checksum_T"));
822 				zfs_case_serialize(zcp);
823 			}
824 			if (fmd_serd_record(hdl,
825 			    zcp->zc_data.zc_serd_checksum, ep)) {
826 				zfs_case_solve(hdl, zcp,
827 				    "fault.fs.zfs.vdev.checksum");
828 			}
829 		} else if (fmd_nvl_class_match(hdl, nvl,
830 		    ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_IO_FAILURE)) &&
831 		    (nvlist_lookup_string(nvl,
832 		    FM_EREPORT_PAYLOAD_ZFS_POOL_FAILMODE, &failmode) == 0) &&
833 		    failmode != NULL) {
834 			if (strncmp(failmode, FM_EREPORT_FAILMODE_CONTINUE,
835 			    strlen(FM_EREPORT_FAILMODE_CONTINUE)) == 0) {
836 				zfs_case_solve(hdl, zcp,
837 				    "fault.fs.zfs.io_failure_continue");
838 			} else if (strncmp(failmode, FM_EREPORT_FAILMODE_WAIT,
839 			    strlen(FM_EREPORT_FAILMODE_WAIT)) == 0) {
840 				zfs_case_solve(hdl, zcp,
841 				    "fault.fs.zfs.io_failure_wait");
842 			}
843 		} else if (fmd_nvl_class_match(hdl, nvl,
844 		    ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_PROBE_FAILURE))) {
845 #ifndef __linux__
846 			/* This causes an unexpected fault diagnosis on linux */
847 			checkremove = B_TRUE;
848 #endif
849 		}
850 
851 		/*
852 		 * Because I/O errors may be due to device removal, we postpone
853 		 * any diagnosis until we're sure that we aren't about to
854 		 * receive a 'resource.fs.zfs.removed' event.
855 		 */
856 		if (checkremove) {
857 			if (zcp->zc_data.zc_has_remove_timer)
858 				fmd_timer_remove(hdl, zcp->zc_remove_timer);
859 			zcp->zc_remove_timer = fmd_timer_install(hdl, zcp, NULL,
860 			    zfs_remove_timeout);
861 			if (!zcp->zc_data.zc_has_remove_timer) {
862 				zcp->zc_data.zc_has_remove_timer = 1;
863 				zfs_case_serialize(zcp);
864 			}
865 		}
866 	}
867 }
868 
869 /*
870  * The timeout is fired when we diagnosed an I/O error, and it was not due to
871  * device removal (which would cause the timeout to be cancelled).
872  */
873 static void
874 zfs_fm_timeout(fmd_hdl_t *hdl, id_t id, void *data)
875 {
876 	zfs_case_t *zcp = data;
877 
878 	if (id == zcp->zc_remove_timer)
879 		zfs_case_solve(hdl, zcp, "fault.fs.zfs.vdev.io");
880 }
881 
882 /*
883  * The specified case has been closed and any case-specific
884  * data structures should be deallocated.
885  */
886 static void
887 zfs_fm_close(fmd_hdl_t *hdl, fmd_case_t *cs)
888 {
889 	zfs_case_t *zcp = fmd_case_getspecific(hdl, cs);
890 
891 	if (zcp->zc_data.zc_serd_checksum[0] != '\0')
892 		fmd_serd_destroy(hdl, zcp->zc_data.zc_serd_checksum);
893 	if (zcp->zc_data.zc_serd_io[0] != '\0')
894 		fmd_serd_destroy(hdl, zcp->zc_data.zc_serd_io);
895 	if (zcp->zc_data.zc_has_remove_timer)
896 		fmd_timer_remove(hdl, zcp->zc_remove_timer);
897 
898 	uu_list_remove(zfs_cases, zcp);
899 	uu_list_node_fini(zcp, &zcp->zc_node, zfs_case_pool);
900 	fmd_hdl_free(hdl, zcp, sizeof (zfs_case_t));
901 }
902 
903 /*
904  * We use the fmd gc entry point to look for old cases that no longer apply.
905  * This allows us to keep our set of case data small in a long running system.
906  */
907 static void
908 zfs_fm_gc(fmd_hdl_t *hdl)
909 {
910 	zfs_purge_cases(hdl);
911 }
912 
913 static const fmd_hdl_ops_t fmd_ops = {
914 	zfs_fm_recv,	/* fmdo_recv */
915 	zfs_fm_timeout,	/* fmdo_timeout */
916 	zfs_fm_close,	/* fmdo_close */
917 	NULL,		/* fmdo_stats */
918 	zfs_fm_gc,	/* fmdo_gc */
919 };
920 
921 static const fmd_prop_t fmd_props[] = {
922 	{ "checksum_N", FMD_TYPE_UINT32, "10" },
923 	{ "checksum_T", FMD_TYPE_TIME, "10min" },
924 	{ "io_N", FMD_TYPE_UINT32, "10" },
925 	{ "io_T", FMD_TYPE_TIME, "10min" },
926 	{ "remove_timeout", FMD_TYPE_TIME, "15sec" },
927 	{ NULL, 0, NULL }
928 };
929 
930 static const fmd_hdl_info_t fmd_info = {
931 	"ZFS Diagnosis Engine", "1.0", &fmd_ops, fmd_props
932 };
933 
934 void
935 _zfs_diagnosis_init(fmd_hdl_t *hdl)
936 {
937 	libzfs_handle_t *zhdl;
938 
939 	if ((zhdl = libzfs_init()) == NULL)
940 		return;
941 
942 	if ((zfs_case_pool = uu_list_pool_create("zfs_case_pool",
943 	    sizeof (zfs_case_t), offsetof(zfs_case_t, zc_node),
944 	    NULL, UU_LIST_POOL_DEBUG)) == NULL) {
945 		libzfs_fini(zhdl);
946 		return;
947 	}
948 
949 	if ((zfs_cases = uu_list_create(zfs_case_pool, NULL,
950 	    UU_LIST_DEBUG)) == NULL) {
951 		uu_list_pool_destroy(zfs_case_pool);
952 		libzfs_fini(zhdl);
953 		return;
954 	}
955 
956 	if (fmd_hdl_register(hdl, FMD_API_VERSION, &fmd_info) != 0) {
957 		uu_list_destroy(zfs_cases);
958 		uu_list_pool_destroy(zfs_case_pool);
959 		libzfs_fini(zhdl);
960 		return;
961 	}
962 
963 	fmd_hdl_setspecific(hdl, zhdl);
964 
965 	(void) fmd_stat_create(hdl, FMD_STAT_NOALLOC, sizeof (zfs_stats) /
966 	    sizeof (fmd_stat_t), (fmd_stat_t *)&zfs_stats);
967 
968 	zfs_remove_timeout = fmd_prop_get_int64(hdl, "remove_timeout");
969 }
970 
971 void
972 _zfs_diagnosis_fini(fmd_hdl_t *hdl)
973 {
974 	zfs_case_t *zcp;
975 	uu_list_walk_t *walk;
976 	libzfs_handle_t *zhdl;
977 
978 	/*
979 	 * Remove all active cases.
980 	 */
981 	walk = uu_list_walk_start(zfs_cases, UU_WALK_ROBUST);
982 	while ((zcp = uu_list_walk_next(walk)) != NULL) {
983 		fmd_hdl_debug(hdl, "removing case ena %llu",
984 		    (long long unsigned)zcp->zc_data.zc_ena);
985 		uu_list_remove(zfs_cases, zcp);
986 		uu_list_node_fini(zcp, &zcp->zc_node, zfs_case_pool);
987 		fmd_hdl_free(hdl, zcp, sizeof (zfs_case_t));
988 	}
989 	uu_list_walk_end(walk);
990 
991 	uu_list_destroy(zfs_cases);
992 	uu_list_pool_destroy(zfs_case_pool);
993 
994 	zhdl = fmd_hdl_getspecific(hdl);
995 	libzfs_fini(zhdl);
996 }
997