xref: /illumos-gate/usr/src/cmd/fm/modules/common/zfs-diagnosis/zfs_de.c (revision 51ece83525fa18f5e72627610f480dffc7e492fd)
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 http://www.opensolaris.org/os/licensing.
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  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <assert.h>
29 #include <stddef.h>
30 #include <strings.h>
31 #include <libuutil.h>
32 #include <libzfs.h>
33 #include <fm/fmd_api.h>
34 #include <sys/fs/zfs.h>
35 #include <sys/fm/protocol.h>
36 #include <sys/fm/fs/zfs.h>
37 
38 /*
39  * Our serd engines are named 'zfs_<pool_guid>_<vdev_guid>_{checksum,io}'.  This
40  * #define reserves enough space for two 64-bit hex values plus the length of
41  * the longest string.
42  */
43 #define	MAX_SERDLEN	(16 * 2 + sizeof ("zfs___checksum"))
44 
45 /*
46  * On-disk case structure.  This must maintain backwards compatibility with
47  * previous versions of the DE.  By default, any members appended to the end
48  * will be filled with zeros if they don't exist in a previous version.
49  */
50 typedef struct zfs_case_data {
51 	uint64_t	zc_version;
52 	uint64_t	zc_ena;
53 	uint64_t	zc_pool_guid;
54 	uint64_t	zc_vdev_guid;
55 	int		zc_has_timer;		/* defunct */
56 	int		zc_pool_state;
57 	char		zc_serd_checksum[MAX_SERDLEN];
58 	char		zc_serd_io[MAX_SERDLEN];
59 	int		zc_has_remove_timer;
60 } zfs_case_data_t;
61 
62 /*
63  * In-core case structure.
64  */
65 typedef struct zfs_case {
66 	boolean_t	zc_present;
67 	uint32_t	zc_version;
68 	zfs_case_data_t	zc_data;
69 	fmd_case_t	*zc_case;
70 	uu_list_node_t	zc_node;
71 	id_t		zc_remove_timer;
72 } zfs_case_t;
73 
74 #define	CASE_DATA			"data"
75 #define	CASE_DATA_VERSION_INITIAL	1
76 #define	CASE_DATA_VERSION_SERD		2
77 
78 static hrtime_t zfs_remove_timeout;
79 
80 uu_list_pool_t *zfs_case_pool;
81 uu_list_t *zfs_cases;
82 
83 #define	ZFS_MAKE_RSRC(type)	\
84     FM_RSRC_CLASS "." ZFS_ERROR_CLASS "." type
85 #define	ZFS_MAKE_EREPORT(type)	\
86     FM_EREPORT_CLASS "." ZFS_ERROR_CLASS "." type
87 
88 /*
89  * Write out the persistent representation of an active case.
90  */
91 static void
92 zfs_case_serialize(fmd_hdl_t *hdl, zfs_case_t *zcp)
93 {
94 	/*
95 	 * Always update cases to the latest version, even if they were the
96 	 * previous version when unserialized.
97 	 */
98 	zcp->zc_data.zc_version = CASE_DATA_VERSION_SERD;
99 	fmd_buf_write(hdl, zcp->zc_case, CASE_DATA, &zcp->zc_data,
100 	    sizeof (zcp->zc_data));
101 }
102 
103 /*
104  * Read back the persistent representation of an active case.
105  */
106 static zfs_case_t *
107 zfs_case_unserialize(fmd_hdl_t *hdl, fmd_case_t *cp)
108 {
109 	zfs_case_t *zcp;
110 
111 	zcp = fmd_hdl_zalloc(hdl, sizeof (zfs_case_t), FMD_SLEEP);
112 	zcp->zc_case = cp;
113 
114 	fmd_buf_read(hdl, cp, CASE_DATA, &zcp->zc_data,
115 	    sizeof (zcp->zc_data));
116 
117 	if (zcp->zc_data.zc_version > CASE_DATA_VERSION_SERD) {
118 		fmd_hdl_free(hdl, zcp, sizeof (zfs_case_t));
119 		return (NULL);
120 	}
121 
122 	/*
123 	 * fmd_buf_read() will have already zeroed out the remainder of the
124 	 * buffer, so we don't have to do anything special if the version
125 	 * doesn't include the SERD engine name.
126 	 */
127 
128 	if (zcp->zc_data.zc_has_remove_timer)
129 		zcp->zc_remove_timer = fmd_timer_install(hdl, zcp,
130 		    NULL, zfs_remove_timeout);
131 
132 	(void) uu_list_insert_before(zfs_cases, NULL, zcp);
133 
134 	fmd_case_setspecific(hdl, cp, zcp);
135 
136 	return (zcp);
137 }
138 
139 /*
140  * Iterate over any active cases.  If any cases are associated with a pool or
141  * vdev which is no longer present on the system, close the associated case.
142  */
143 static void
144 zfs_mark_vdev(uint64_t pool_guid, nvlist_t *vd)
145 {
146 	uint64_t vdev_guid;
147 	uint_t c, children;
148 	nvlist_t **child;
149 	zfs_case_t *zcp;
150 	int ret;
151 
152 	ret = nvlist_lookup_uint64(vd, ZPOOL_CONFIG_GUID, &vdev_guid);
153 	assert(ret == 0);
154 
155 	/*
156 	 * Mark any cases associated with this (pool, vdev) pair.
157 	 */
158 	for (zcp = uu_list_first(zfs_cases); zcp != NULL;
159 	    zcp = uu_list_next(zfs_cases, zcp)) {
160 		if (zcp->zc_data.zc_pool_guid == pool_guid &&
161 		    zcp->zc_data.zc_vdev_guid == vdev_guid)
162 			zcp->zc_present = B_TRUE;
163 	}
164 
165 	/*
166 	 * Iterate over all children.
167 	 */
168 	if (nvlist_lookup_nvlist_array(vd, ZPOOL_CONFIG_CHILDREN, &child,
169 	    &children) != 0) {
170 		for (c = 0; c < children; c++)
171 			zfs_mark_vdev(pool_guid, child[c]);
172 	}
173 }
174 
175 /*ARGSUSED*/
176 static int
177 zfs_mark_pool(zpool_handle_t *zhp, void *unused)
178 {
179 	zfs_case_t *zcp;
180 	uint64_t pool_guid;
181 	nvlist_t *config, *vd;
182 	int ret;
183 
184 	pool_guid = zpool_get_prop_int(zhp, ZPOOL_PROP_GUID, NULL);
185 	/*
186 	 * Mark any cases associated with just this pool.
187 	 */
188 	for (zcp = uu_list_first(zfs_cases); zcp != NULL;
189 	    zcp = uu_list_next(zfs_cases, zcp)) {
190 		if (zcp->zc_data.zc_pool_guid == pool_guid &&
191 		    zcp->zc_data.zc_vdev_guid == 0)
192 			zcp->zc_present = B_TRUE;
193 	}
194 
195 	if ((config = zpool_get_config(zhp, NULL)) == NULL) {
196 		zpool_close(zhp);
197 		return (-1);
198 	}
199 
200 	ret = nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &vd);
201 	assert(ret == 0);
202 
203 	zfs_mark_vdev(pool_guid, vd);
204 
205 	zpool_close(zhp);
206 
207 	return (0);
208 }
209 
210 static void
211 zfs_purge_cases(fmd_hdl_t *hdl)
212 {
213 	zfs_case_t *zcp;
214 	uu_list_walk_t *walk;
215 	libzfs_handle_t *zhdl = fmd_hdl_getspecific(hdl);
216 
217 	/*
218 	 * There is no way to open a pool by GUID, or lookup a vdev by GUID.  No
219 	 * matter what we do, we're going to have to stomach a O(vdevs * cases)
220 	 * algorithm.  In reality, both quantities are likely so small that
221 	 * neither will matter. Given that iterating over pools is more
222 	 * expensive than iterating over the in-memory case list, we opt for a
223 	 * 'present' flag in each case that starts off cleared.  We then iterate
224 	 * over all pools, marking those that are still present, and removing
225 	 * those that aren't found.
226 	 *
227 	 * Note that we could also construct an FMRI and rely on
228 	 * fmd_nvl_fmri_present(), but this would end up doing the same search.
229 	 */
230 
231 	/*
232 	 * Mark the cases an not present.
233 	 */
234 	for (zcp = uu_list_first(zfs_cases); zcp != NULL;
235 	    zcp = uu_list_next(zfs_cases, zcp))
236 		zcp->zc_present = B_FALSE;
237 
238 	/*
239 	 * Iterate over all pools and mark the pools and vdevs found.  If this
240 	 * fails (most probably because we're out of memory), then don't close
241 	 * any of the cases and we cannot be sure they are accurate.
242 	 */
243 	if (zpool_iter(zhdl, zfs_mark_pool, NULL) != 0)
244 		return;
245 
246 	/*
247 	 * Remove those cases which were not found.
248 	 */
249 	walk = uu_list_walk_start(zfs_cases, UU_WALK_ROBUST);
250 	while ((zcp = uu_list_walk_next(walk)) != NULL) {
251 		if (!zcp->zc_present)
252 			fmd_case_close(hdl, zcp->zc_case);
253 	}
254 	uu_list_walk_end(walk);
255 }
256 
257 /*
258  * Construct the name of a serd engine given the pool/vdev GUID and type (io or
259  * checksum).
260  */
261 static void
262 zfs_serd_name(char *buf, uint64_t pool_guid, uint64_t vdev_guid,
263     const char *type)
264 {
265 	(void) snprintf(buf, MAX_SERDLEN, "zfs_%llx_%llx_%s", pool_guid,
266 	    vdev_guid, type);
267 }
268 
269 /*
270  * Solve a given ZFS case.  This first checks to make sure the diagnosis is
271  * still valid, as well as cleaning up any pending timer associated with the
272  * case.
273  */
274 static void
275 zfs_case_solve(fmd_hdl_t *hdl, zfs_case_t *zcp, const char *faultname,
276     boolean_t checkunusable)
277 {
278 	nvlist_t *detector, *fault;
279 	boolean_t serialize;
280 
281 	/*
282 	 * Construct the detector from the case data.  The detector is in the
283 	 * ZFS scheme, and is either the pool or the vdev, depending on whether
284 	 * this is a vdev or pool fault.
285 	 */
286 	if (nvlist_alloc(&detector, NV_UNIQUE_NAME, 0) != 0)
287 		return;
288 
289 	if (nvlist_add_uint8(detector, FM_VERSION, ZFS_SCHEME_VERSION0) != 0 ||
290 	    nvlist_add_string(detector, FM_FMRI_SCHEME,
291 	    FM_FMRI_SCHEME_ZFS) != 0 ||
292 	    nvlist_add_uint64(detector, FM_FMRI_ZFS_POOL,
293 	    zcp->zc_data.zc_pool_guid) != 0 ||
294 	    (zcp->zc_data.zc_vdev_guid != 0 &&
295 	    nvlist_add_uint64(detector, FM_FMRI_ZFS_VDEV,
296 	    zcp->zc_data.zc_vdev_guid) != 0)) {
297 		nvlist_free(detector);
298 		return;
299 	}
300 
301 	/*
302 	 * We also want to make sure that the detector (pool or vdev) properly
303 	 * reflects the diagnosed state, when the fault corresponds to internal
304 	 * ZFS state (i.e. not checksum or I/O error-induced).  Otherwise, a
305 	 * device which was unavailable early in boot (because the driver/file
306 	 * wasn't available) and is now healthy will be mis-diagnosed.
307 	 */
308 	if (!fmd_nvl_fmri_present(hdl, detector) ||
309 	    (checkunusable && !fmd_nvl_fmri_unusable(hdl, detector))) {
310 		fmd_case_close(hdl, zcp->zc_case);
311 		nvlist_free(detector);
312 		return;
313 	}
314 
315 	fault = fmd_nvl_create_fault(hdl, faultname, 100, detector, NULL,
316 	    detector);
317 	fmd_case_add_suspect(hdl, zcp->zc_case, fault);
318 	fmd_case_solve(hdl, zcp->zc_case);
319 
320 	serialize = B_FALSE;
321 	if (zcp->zc_data.zc_has_remove_timer) {
322 		fmd_timer_remove(hdl, zcp->zc_remove_timer);
323 		zcp->zc_data.zc_has_remove_timer = 0;
324 		serialize = B_TRUE;
325 	}
326 	if (serialize)
327 		zfs_case_serialize(hdl, zcp);
328 
329 	nvlist_free(detector);
330 }
331 
332 /*
333  * Main fmd entry point.
334  */
335 /*ARGSUSED*/
336 static void
337 zfs_fm_recv(fmd_hdl_t *hdl, fmd_event_t *ep, nvlist_t *nvl, const char *class)
338 {
339 	zfs_case_t *zcp, *dcp;
340 	int32_t pool_state;
341 	uint64_t ena, pool_guid, vdev_guid;
342 	nvlist_t *detector;
343 	boolean_t isresource;
344 	boolean_t checkremove;
345 
346 	isresource = fmd_nvl_class_match(hdl, nvl, "resource.fs.zfs.*");
347 
348 	if (isresource) {
349 		/*
350 		 * For resources, we don't have a normal payload.
351 		 */
352 		if (nvlist_lookup_uint64(nvl, FM_EREPORT_PAYLOAD_ZFS_VDEV_GUID,
353 		    &vdev_guid) != 0)
354 			pool_state = SPA_LOAD_OPEN;
355 		else
356 			pool_state = SPA_LOAD_NONE;
357 		detector = NULL;
358 	} else {
359 		(void) nvlist_lookup_nvlist(nvl,
360 		    FM_EREPORT_DETECTOR, &detector);
361 		(void) nvlist_lookup_int32(nvl,
362 		    FM_EREPORT_PAYLOAD_ZFS_POOL_CONTEXT, &pool_state);
363 	}
364 
365 	/*
366 	 * We also ignore all ereports generated during an import of a pool,
367 	 * since the only possible fault (.pool) would result in import failure,
368 	 * and hence no persistent fault.  Some day we may want to do something
369 	 * with these ereports, so we continue generating them internally.
370 	 */
371 	if (pool_state == SPA_LOAD_IMPORT)
372 		return;
373 
374 	/*
375 	 * Device I/O errors are ignored during pool open.
376 	 */
377 	if (pool_state == SPA_LOAD_OPEN &&
378 	    (fmd_nvl_class_match(hdl, nvl, "ereport.fs.zfs.checksum") ||
379 	    fmd_nvl_class_match(hdl, nvl, "ereport.fs.zfs.io")))
380 		return;
381 
382 	/*
383 	 * Determine if this ereport corresponds to an open case.  Cases are
384 	 * indexed by ENA, since ZFS does all the work of chaining together
385 	 * related ereports.
386 	 *
387 	 * We also detect if an ereport corresponds to an open case by context,
388 	 * such as:
389 	 *
390 	 * 	- An error occurred during an open of a pool with an existing
391 	 *	  case.
392 	 *
393 	 * 	- An error occurred for a device which already has an open
394 	 *	  case.
395 	 */
396 	(void) nvlist_lookup_uint64(nvl,
397 	    FM_EREPORT_PAYLOAD_ZFS_POOL_GUID, &pool_guid);
398 	if (nvlist_lookup_uint64(nvl,
399 	    FM_EREPORT_PAYLOAD_ZFS_VDEV_GUID, &vdev_guid) != 0)
400 		vdev_guid = 0;
401 	if (nvlist_lookup_uint64(nvl, FM_EREPORT_ENA, &ena) != 0)
402 		ena = 0;
403 
404 	for (zcp = uu_list_first(zfs_cases); zcp != NULL;
405 	    zcp = uu_list_next(zfs_cases, zcp)) {
406 		/*
407 		 * Matches a known ENA.
408 		 */
409 		if (zcp->zc_data.zc_ena == ena)
410 			break;
411 
412 		/*
413 		 * Matches a case involving load errors for this same pool.
414 		 */
415 		if (zcp->zc_data.zc_pool_guid == pool_guid &&
416 		    zcp->zc_data.zc_pool_state == SPA_LOAD_OPEN &&
417 		    pool_state == SPA_LOAD_OPEN)
418 			break;
419 
420 		/*
421 		 * Device errors for the same device.
422 		 */
423 		if (vdev_guid != 0 && zcp->zc_data.zc_vdev_guid == vdev_guid)
424 			break;
425 	}
426 
427 	if (zcp == NULL) {
428 		fmd_case_t *cs;
429 		zfs_case_data_t data = { 0 };
430 
431 		/*
432 		 * If this is one of our 'fake' resource ereports, and there is
433 		 * no case open, simply discard it.
434 		 */
435 		if (isresource)
436 			return;
437 
438 		/*
439 		 * Open a new case.
440 		 */
441 		cs = fmd_case_open(hdl, NULL);
442 
443 		/*
444 		 * Initialize the case buffer.  To commonize code, we actually
445 		 * create the buffer with existing data, and then call
446 		 * zfs_case_unserialize() to instantiate the in-core structure.
447 		 */
448 		fmd_buf_create(hdl, cs, CASE_DATA,
449 		    sizeof (zfs_case_data_t));
450 
451 		data.zc_version = CASE_DATA_VERSION_SERD;
452 		data.zc_ena = ena;
453 		data.zc_pool_guid = pool_guid;
454 		data.zc_vdev_guid = vdev_guid;
455 		data.zc_pool_state = (int)pool_state;
456 
457 		fmd_buf_write(hdl, cs, CASE_DATA, &data, sizeof (data));
458 
459 		zcp = zfs_case_unserialize(hdl, cs);
460 		assert(zcp != NULL);
461 	}
462 
463 	if (isresource) {
464 		if (fmd_nvl_class_match(hdl, nvl,
465 		    ZFS_MAKE_RSRC(FM_RESOURCE_AUTOREPLACE))) {
466 			/*
467 			 * The 'resource.fs.zfs.autoreplace' event indicates
468 			 * that the pool was loaded with the 'autoreplace'
469 			 * property set.  In this case, any pending device
470 			 * failures should be ignored, as the asynchronous
471 			 * autoreplace handling will take care of them.
472 			 */
473 			fmd_case_close(hdl, zcp->zc_case);
474 		} else if (fmd_nvl_class_match(hdl, nvl,
475 		    ZFS_MAKE_RSRC(FM_RESOURCE_REMOVED))) {
476 			/*
477 			 * The 'resource.fs.zfs.removed' event indicates that
478 			 * device removal was detected, and the device was
479 			 * closed asynchronously.  If this is the case, we
480 			 * assume that any recent I/O errors were due to the
481 			 * device removal, not any fault of the device itself.
482 			 * We reset the SERD engine, and cancel any pending
483 			 * timers.
484 			 */
485 			if (zcp->zc_data.zc_has_remove_timer) {
486 				fmd_timer_remove(hdl, zcp->zc_remove_timer);
487 				zcp->zc_data.zc_has_remove_timer = 0;
488 				zfs_case_serialize(hdl, zcp);
489 			}
490 			if (zcp->zc_data.zc_serd_io[0] != '\0')
491 				fmd_serd_reset(hdl,
492 				    zcp->zc_data.zc_serd_io);
493 			if (zcp->zc_data.zc_serd_checksum[0] != '\0')
494 				fmd_serd_reset(hdl,
495 				    zcp->zc_data.zc_serd_checksum);
496 		}
497 		return;
498 	}
499 
500 	/*
501 	 * Associate the ereport with this case.
502 	 */
503 	fmd_case_add_ereport(hdl, zcp->zc_case, ep);
504 
505 	/*
506 	 * Don't do anything else if this case is already solved.
507 	 */
508 	if (fmd_case_solved(hdl, zcp->zc_case))
509 		return;
510 
511 	/*
512 	 * Determine if we should solve the case and generate a fault.  We solve
513 	 * a case if:
514 	 *
515 	 * 	a. A pool failed to open (ereport.fs.zfs.pool)
516 	 * 	b. A device failed to open (ereport.fs.zfs.pool) while a pool
517 	 *	   was up and running.
518 	 *
519 	 * We may see a series of ereports associated with a pool open, all
520 	 * chained together by the same ENA.  If the pool open succeeds, then
521 	 * we'll see no further ereports.  To detect when a pool open has
522 	 * succeeded, we associate a timer with the event.  When it expires, we
523 	 * close the case.
524 	 */
525 	if (fmd_nvl_class_match(hdl, nvl,
526 	    ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_POOL))) {
527 		/*
528 		 * Pool level fault.  Before solving the case, go through and
529 		 * close any open device cases that may be pending.
530 		 */
531 		for (dcp = uu_list_first(zfs_cases); dcp != NULL;
532 		    dcp = uu_list_next(zfs_cases, dcp)) {
533 			if (dcp->zc_data.zc_pool_guid ==
534 			    zcp->zc_data.zc_pool_guid &&
535 			    dcp->zc_data.zc_vdev_guid != 0)
536 				fmd_case_close(hdl, dcp->zc_case);
537 		}
538 
539 		zfs_case_solve(hdl, zcp, "fault.fs.zfs.pool", B_TRUE);
540 	} else if (fmd_nvl_class_match(hdl, nvl, "ereport.fs.zfs.vdev.*")) {
541 		/*
542 		 * Device fault.  If this occurred during pool open, then defer
543 		 * reporting the fault.  If the pool itself could not be opeend,
544 		 * we only report the pool fault, not every device fault that
545 		 * may have caused the problem.  If we do not see a pool fault
546 		 * within the timeout period, then we'll solve the device case.
547 		 */
548 		zfs_case_solve(hdl, zcp, "fault.fs.zfs.device",  B_TRUE);
549 	} else if (fmd_nvl_class_match(hdl, nvl,
550 	    ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_IO)) ||
551 	    fmd_nvl_class_match(hdl, nvl,
552 	    ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_CHECKSUM)) ||
553 	    fmd_nvl_class_match(hdl, nvl,
554 	    ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_IO_FAILURE)) ||
555 	    fmd_nvl_class_match(hdl, nvl,
556 	    ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_PROBE_FAILURE))) {
557 		char *failmode = NULL;
558 
559 		/*
560 		 * If this is a checksum or I/O error, then toss it into the
561 		 * appropriate SERD engine and check to see if it has fired.
562 		 * Ideally, we want to do something more sophisticated,
563 		 * (persistent errors for a single data block, etc).  For now,
564 		 * a single SERD engine is sufficient.
565 		 */
566 		if (fmd_nvl_class_match(hdl, nvl,
567 		    ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_IO))) {
568 			if (zcp->zc_data.zc_serd_io[0] == '\0') {
569 				zfs_serd_name(zcp->zc_data.zc_serd_io,
570 				    pool_guid, vdev_guid, "io");
571 				fmd_serd_create(hdl, zcp->zc_data.zc_serd_io,
572 				    fmd_prop_get_int32(hdl, "io_N"),
573 				    fmd_prop_get_int64(hdl, "io_T"));
574 				zfs_case_serialize(hdl, zcp);
575 			}
576 			if (fmd_serd_record(hdl, zcp->zc_data.zc_serd_io, ep))
577 				checkremove = B_TRUE;
578 		} else if (fmd_nvl_class_match(hdl, nvl,
579 		    ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_CHECKSUM))) {
580 			if (zcp->zc_data.zc_serd_checksum[0] == '\0') {
581 				zfs_serd_name(zcp->zc_data.zc_serd_checksum,
582 				    pool_guid, vdev_guid, "checksum");
583 				fmd_serd_create(hdl,
584 				    zcp->zc_data.zc_serd_checksum,
585 				    fmd_prop_get_int32(hdl, "checksum_N"),
586 				    fmd_prop_get_int64(hdl, "checksum_T"));
587 				zfs_case_serialize(hdl, zcp);
588 			}
589 			if (fmd_serd_record(hdl,
590 			    zcp->zc_data.zc_serd_checksum, ep)) {
591 				zfs_case_solve(hdl, zcp,
592 				    "fault.fs.zfs.vdev.checksum", B_FALSE);
593 			}
594 		} else if (fmd_nvl_class_match(hdl, nvl,
595 		    ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_IO_FAILURE)) &&
596 		    (nvlist_lookup_string(nvl,
597 		    FM_EREPORT_PAYLOAD_ZFS_POOL_FAILMODE, &failmode) == 0) &&
598 		    failmode != NULL) {
599 			if (strncmp(failmode, FM_EREPORT_FAILMODE_CONTINUE,
600 			    strlen(FM_EREPORT_FAILMODE_CONTINUE)) == 0) {
601 				zfs_case_solve(hdl, zcp,
602 				    "fault.fs.zfs.io_failure_continue",
603 				    B_FALSE);
604 			} else if (strncmp(failmode, FM_EREPORT_FAILMODE_WAIT,
605 			    strlen(FM_EREPORT_FAILMODE_WAIT)) == 0) {
606 				zfs_case_solve(hdl, zcp,
607 				    "fault.fs.zfs.io_failure_wait", B_FALSE);
608 			}
609 		} else if (fmd_nvl_class_match(hdl, nvl,
610 		    ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_PROBE_FAILURE))) {
611 			checkremove = B_TRUE;
612 		}
613 
614 		/*
615 		 * Because I/O errors may be due to device removal, we postpone
616 		 * any diagnosis until we're sure that we aren't about to
617 		 * receive a 'resource.fs.zfs.removed' event.
618 		 */
619 		if (checkremove) {
620 			if (zcp->zc_data.zc_has_remove_timer)
621 				fmd_timer_remove(hdl, zcp->zc_remove_timer);
622 			zcp->zc_remove_timer = fmd_timer_install(hdl, zcp, NULL,
623 			    zfs_remove_timeout);
624 			if (!zcp->zc_data.zc_has_remove_timer) {
625 				zcp->zc_data.zc_has_remove_timer = 1;
626 				zfs_case_serialize(hdl, zcp);
627 			}
628 		}
629 	}
630 }
631 
632 /*
633  * The timeout is fired when we diagnosed an I/O error, and it was not due to
634  * device removal (which would cause the timeout to be cancelled).
635  */
636 /* ARGSUSED */
637 static void
638 zfs_fm_timeout(fmd_hdl_t *hdl, id_t id, void *data)
639 {
640 	zfs_case_t *zcp = data;
641 
642 	if (id == zcp->zc_remove_timer)
643 		zfs_case_solve(hdl, zcp, "fault.fs.zfs.vdev.io", B_FALSE);
644 }
645 
646 static void
647 zfs_fm_close(fmd_hdl_t *hdl, fmd_case_t *cs)
648 {
649 	zfs_case_t *zcp = fmd_case_getspecific(hdl, cs);
650 
651 	if (zcp->zc_data.zc_serd_checksum[0] != '\0')
652 		fmd_serd_destroy(hdl, zcp->zc_data.zc_serd_checksum);
653 	if (zcp->zc_data.zc_serd_io[0] != '\0')
654 		fmd_serd_destroy(hdl, zcp->zc_data.zc_serd_io);
655 	if (zcp->zc_data.zc_has_remove_timer)
656 		fmd_timer_remove(hdl, zcp->zc_remove_timer);
657 	uu_list_remove(zfs_cases, zcp);
658 	fmd_hdl_free(hdl, zcp, sizeof (zfs_case_t));
659 }
660 
661 /*
662  * We use the fmd gc entry point to look for old cases that no longer apply.
663  * This allows us to keep our set of case data small in a long running system.
664  */
665 static void
666 zfs_fm_gc(fmd_hdl_t *hdl)
667 {
668 	zfs_purge_cases(hdl);
669 }
670 
671 static const fmd_hdl_ops_t fmd_ops = {
672 	zfs_fm_recv,	/* fmdo_recv */
673 	zfs_fm_timeout,	/* fmdo_timeout */
674 	zfs_fm_close,	/* fmdo_close */
675 	NULL,		/* fmdo_stats */
676 	zfs_fm_gc,	/* fmdo_gc */
677 };
678 
679 static const fmd_prop_t fmd_props[] = {
680 	{ "case_timeout", FMD_TYPE_TIME, "5sec" },
681 	{ "checksum_N", FMD_TYPE_UINT32, "10" },
682 	{ "checksum_T", FMD_TYPE_TIME, "10min" },
683 	{ "io_N", FMD_TYPE_UINT32, "10" },
684 	{ "io_T", FMD_TYPE_TIME, "10min" },
685 	{ "remove_timeout", FMD_TYPE_TIME, "5sec" },
686 	{ NULL, 0, NULL }
687 };
688 
689 static const fmd_hdl_info_t fmd_info = {
690 	"ZFS Diagnosis Engine", "1.0", &fmd_ops, fmd_props
691 };
692 
693 void
694 _fmd_init(fmd_hdl_t *hdl)
695 {
696 	fmd_case_t *cp;
697 	libzfs_handle_t *zhdl;
698 
699 	if ((zhdl = libzfs_init()) == NULL)
700 		return;
701 
702 	if ((zfs_case_pool = uu_list_pool_create("zfs_case_pool",
703 	    sizeof (zfs_case_t), offsetof(zfs_case_t, zc_node),
704 	    NULL, 0)) == NULL) {
705 		libzfs_fini(zhdl);
706 		return;
707 	}
708 
709 	if ((zfs_cases = uu_list_create(zfs_case_pool, NULL, 0)) == NULL) {
710 		uu_list_pool_destroy(zfs_case_pool);
711 		libzfs_fini(zhdl);
712 		return;
713 	}
714 
715 	if (fmd_hdl_register(hdl, FMD_API_VERSION, &fmd_info) != 0) {
716 		uu_list_destroy(zfs_cases);
717 		uu_list_pool_destroy(zfs_case_pool);
718 		libzfs_fini(zhdl);
719 		return;
720 	}
721 
722 	fmd_hdl_setspecific(hdl, zhdl);
723 
724 	/*
725 	 * Iterate over all active cases and unserialize the associated buffers,
726 	 * adding them to our list of open cases.
727 	 */
728 	for (cp = fmd_case_next(hdl, NULL);
729 	    cp != NULL; cp = fmd_case_next(hdl, cp))
730 		(void) zfs_case_unserialize(hdl, cp);
731 
732 	/*
733 	 * Clear out any old cases that are no longer valid.
734 	 */
735 	zfs_purge_cases(hdl);
736 
737 	zfs_remove_timeout = fmd_prop_get_int64(hdl, "remove_timeout");
738 }
739 
740 void
741 _fmd_fini(fmd_hdl_t *hdl)
742 {
743 	zfs_case_t *zcp;
744 	uu_list_walk_t *walk;
745 	libzfs_handle_t *zhdl;
746 
747 	/*
748 	 * Remove all active cases.
749 	 */
750 	walk = uu_list_walk_start(zfs_cases, UU_WALK_ROBUST);
751 	while ((zcp = uu_list_walk_next(walk)) != NULL) {
752 		uu_list_remove(zfs_cases, zcp);
753 		fmd_hdl_free(hdl, zcp, sizeof (zfs_case_t));
754 	}
755 	uu_list_walk_end(walk);
756 
757 	uu_list_destroy(zfs_cases);
758 	uu_list_pool_destroy(zfs_case_pool);
759 
760 	zhdl = fmd_hdl_getspecific(hdl);
761 	libzfs_fini(zhdl);
762 }
763