xref: /illumos-gate/usr/src/cmd/fm/modules/common/zfs-diagnosis/zfs_de.c (revision 581cede61ac9c14d8d4ea452562a567189eead78)
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 	if (nvlist_lookup_nvlist_array(vd, ZPOOL_CONFIG_L2CACHE, &child,
175 	    &children) == 0) {
176 		for (c = 0; c < children; c++)
177 			zfs_mark_vdev(pool_guid, child[c]);
178 	}
179 
180 	if (nvlist_lookup_nvlist_array(vd, ZPOOL_CONFIG_SPARES, &child,
181 	    &children) == 0) {
182 		for (c = 0; c < children; c++)
183 			zfs_mark_vdev(pool_guid, child[c]);
184 	}
185 }
186 
187 /*ARGSUSED*/
188 static int
189 zfs_mark_pool(zpool_handle_t *zhp, void *unused)
190 {
191 	zfs_case_t *zcp;
192 	uint64_t pool_guid;
193 	nvlist_t *config, *vd;
194 	int ret;
195 
196 	pool_guid = zpool_get_prop_int(zhp, ZPOOL_PROP_GUID, NULL);
197 	/*
198 	 * Mark any cases associated with just this pool.
199 	 */
200 	for (zcp = uu_list_first(zfs_cases); zcp != NULL;
201 	    zcp = uu_list_next(zfs_cases, zcp)) {
202 		if (zcp->zc_data.zc_pool_guid == pool_guid &&
203 		    zcp->zc_data.zc_vdev_guid == 0)
204 			zcp->zc_present = B_TRUE;
205 	}
206 
207 	if ((config = zpool_get_config(zhp, NULL)) == NULL) {
208 		zpool_close(zhp);
209 		return (-1);
210 	}
211 
212 	ret = nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &vd);
213 	assert(ret == 0);
214 
215 	zfs_mark_vdev(pool_guid, vd);
216 
217 	zpool_close(zhp);
218 
219 	return (0);
220 }
221 
222 static void
223 zfs_purge_cases(fmd_hdl_t *hdl)
224 {
225 	zfs_case_t *zcp;
226 	uu_list_walk_t *walk;
227 	libzfs_handle_t *zhdl = fmd_hdl_getspecific(hdl);
228 
229 	/*
230 	 * There is no way to open a pool by GUID, or lookup a vdev by GUID.  No
231 	 * matter what we do, we're going to have to stomach a O(vdevs * cases)
232 	 * algorithm.  In reality, both quantities are likely so small that
233 	 * neither will matter. Given that iterating over pools is more
234 	 * expensive than iterating over the in-memory case list, we opt for a
235 	 * 'present' flag in each case that starts off cleared.  We then iterate
236 	 * over all pools, marking those that are still present, and removing
237 	 * those that aren't found.
238 	 *
239 	 * Note that we could also construct an FMRI and rely on
240 	 * fmd_nvl_fmri_present(), but this would end up doing the same search.
241 	 */
242 
243 	/*
244 	 * Mark the cases an not present.
245 	 */
246 	for (zcp = uu_list_first(zfs_cases); zcp != NULL;
247 	    zcp = uu_list_next(zfs_cases, zcp))
248 		zcp->zc_present = B_FALSE;
249 
250 	/*
251 	 * Iterate over all pools and mark the pools and vdevs found.  If this
252 	 * fails (most probably because we're out of memory), then don't close
253 	 * any of the cases and we cannot be sure they are accurate.
254 	 */
255 	if (zpool_iter(zhdl, zfs_mark_pool, NULL) != 0)
256 		return;
257 
258 	/*
259 	 * Remove those cases which were not found.
260 	 */
261 	walk = uu_list_walk_start(zfs_cases, UU_WALK_ROBUST);
262 	while ((zcp = uu_list_walk_next(walk)) != NULL) {
263 		if (!zcp->zc_present)
264 			fmd_case_close(hdl, zcp->zc_case);
265 	}
266 	uu_list_walk_end(walk);
267 }
268 
269 /*
270  * Construct the name of a serd engine given the pool/vdev GUID and type (io or
271  * checksum).
272  */
273 static void
274 zfs_serd_name(char *buf, uint64_t pool_guid, uint64_t vdev_guid,
275     const char *type)
276 {
277 	(void) snprintf(buf, MAX_SERDLEN, "zfs_%llx_%llx_%s", pool_guid,
278 	    vdev_guid, type);
279 }
280 
281 /*
282  * Solve a given ZFS case.  This first checks to make sure the diagnosis is
283  * still valid, as well as cleaning up any pending timer associated with the
284  * case.
285  */
286 static void
287 zfs_case_solve(fmd_hdl_t *hdl, zfs_case_t *zcp, const char *faultname,
288     boolean_t checkunusable)
289 {
290 	nvlist_t *detector, *fault;
291 	boolean_t serialize;
292 
293 	/*
294 	 * Construct the detector from the case data.  The detector is in the
295 	 * ZFS scheme, and is either the pool or the vdev, depending on whether
296 	 * this is a vdev or pool fault.
297 	 */
298 	if (nvlist_alloc(&detector, NV_UNIQUE_NAME, 0) != 0)
299 		return;
300 
301 	if (nvlist_add_uint8(detector, FM_VERSION, ZFS_SCHEME_VERSION0) != 0 ||
302 	    nvlist_add_string(detector, FM_FMRI_SCHEME,
303 	    FM_FMRI_SCHEME_ZFS) != 0 ||
304 	    nvlist_add_uint64(detector, FM_FMRI_ZFS_POOL,
305 	    zcp->zc_data.zc_pool_guid) != 0 ||
306 	    (zcp->zc_data.zc_vdev_guid != 0 &&
307 	    nvlist_add_uint64(detector, FM_FMRI_ZFS_VDEV,
308 	    zcp->zc_data.zc_vdev_guid) != 0)) {
309 		nvlist_free(detector);
310 		return;
311 	}
312 
313 	/*
314 	 * We also want to make sure that the detector (pool or vdev) properly
315 	 * reflects the diagnosed state, when the fault corresponds to internal
316 	 * ZFS state (i.e. not checksum or I/O error-induced).  Otherwise, a
317 	 * device which was unavailable early in boot (because the driver/file
318 	 * wasn't available) and is now healthy will be mis-diagnosed.
319 	 */
320 	if (!fmd_nvl_fmri_present(hdl, detector) ||
321 	    (checkunusable && !fmd_nvl_fmri_unusable(hdl, detector))) {
322 		fmd_case_close(hdl, zcp->zc_case);
323 		nvlist_free(detector);
324 		return;
325 	}
326 
327 	fault = fmd_nvl_create_fault(hdl, faultname, 100, detector, NULL,
328 	    detector);
329 	fmd_case_add_suspect(hdl, zcp->zc_case, fault);
330 	fmd_case_solve(hdl, zcp->zc_case);
331 
332 	serialize = B_FALSE;
333 	if (zcp->zc_data.zc_has_remove_timer) {
334 		fmd_timer_remove(hdl, zcp->zc_remove_timer);
335 		zcp->zc_data.zc_has_remove_timer = 0;
336 		serialize = B_TRUE;
337 	}
338 	if (serialize)
339 		zfs_case_serialize(hdl, zcp);
340 
341 	nvlist_free(detector);
342 }
343 
344 /*
345  * Main fmd entry point.
346  */
347 /*ARGSUSED*/
348 static void
349 zfs_fm_recv(fmd_hdl_t *hdl, fmd_event_t *ep, nvlist_t *nvl, const char *class)
350 {
351 	zfs_case_t *zcp, *dcp;
352 	int32_t pool_state;
353 	uint64_t ena, pool_guid, vdev_guid;
354 	nvlist_t *detector;
355 	boolean_t isresource;
356 	char *type;
357 
358 	isresource = fmd_nvl_class_match(hdl, nvl, "resource.fs.zfs.*");
359 
360 	if (isresource) {
361 		/*
362 		 * For resources, we don't have a normal payload.
363 		 */
364 		if (nvlist_lookup_uint64(nvl, FM_EREPORT_PAYLOAD_ZFS_VDEV_GUID,
365 		    &vdev_guid) != 0)
366 			pool_state = SPA_LOAD_OPEN;
367 		else
368 			pool_state = SPA_LOAD_NONE;
369 		detector = NULL;
370 	} else {
371 		(void) nvlist_lookup_nvlist(nvl,
372 		    FM_EREPORT_DETECTOR, &detector);
373 		(void) nvlist_lookup_int32(nvl,
374 		    FM_EREPORT_PAYLOAD_ZFS_POOL_CONTEXT, &pool_state);
375 	}
376 
377 	/*
378 	 * We also ignore all ereports generated during an import of a pool,
379 	 * since the only possible fault (.pool) would result in import failure,
380 	 * and hence no persistent fault.  Some day we may want to do something
381 	 * with these ereports, so we continue generating them internally.
382 	 */
383 	if (pool_state == SPA_LOAD_IMPORT)
384 		return;
385 
386 	/*
387 	 * Device I/O errors are ignored during pool open.
388 	 */
389 	if (pool_state == SPA_LOAD_OPEN &&
390 	    (fmd_nvl_class_match(hdl, nvl,
391 	    ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_CHECKSUM)) ||
392 	    fmd_nvl_class_match(hdl, nvl,
393 	    ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_IO)) ||
394 	    fmd_nvl_class_match(hdl, nvl,
395 	    ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_PROBE_FAILURE))))
396 		return;
397 
398 	/*
399 	 * We ignore ereports for anything except disks and files.
400 	 */
401 	if (nvlist_lookup_string(nvl, FM_EREPORT_PAYLOAD_ZFS_VDEV_TYPE,
402 	    &type) == 0) {
403 		if (strcmp(type, VDEV_TYPE_DISK) != 0 &&
404 		    strcmp(type, VDEV_TYPE_FILE) != 0)
405 			return;
406 	}
407 
408 	/*
409 	 * Determine if this ereport corresponds to an open case.  Cases are
410 	 * indexed by ENA, since ZFS does all the work of chaining together
411 	 * related ereports.
412 	 *
413 	 * We also detect if an ereport corresponds to an open case by context,
414 	 * such as:
415 	 *
416 	 * 	- An error occurred during an open of a pool with an existing
417 	 *	  case.
418 	 *
419 	 * 	- An error occurred for a device which already has an open
420 	 *	  case.
421 	 */
422 	(void) nvlist_lookup_uint64(nvl,
423 	    FM_EREPORT_PAYLOAD_ZFS_POOL_GUID, &pool_guid);
424 	if (nvlist_lookup_uint64(nvl,
425 	    FM_EREPORT_PAYLOAD_ZFS_VDEV_GUID, &vdev_guid) != 0)
426 		vdev_guid = 0;
427 	if (nvlist_lookup_uint64(nvl, FM_EREPORT_ENA, &ena) != 0)
428 		ena = 0;
429 
430 	for (zcp = uu_list_first(zfs_cases); zcp != NULL;
431 	    zcp = uu_list_next(zfs_cases, zcp)) {
432 		/*
433 		 * Matches a known ENA.
434 		 */
435 		if (zcp->zc_data.zc_ena == ena)
436 			break;
437 
438 		/*
439 		 * Matches a case involving load errors for this same pool.
440 		 */
441 		if (zcp->zc_data.zc_pool_guid == pool_guid &&
442 		    zcp->zc_data.zc_pool_state == SPA_LOAD_OPEN &&
443 		    pool_state == SPA_LOAD_OPEN)
444 			break;
445 
446 		/*
447 		 * Device errors for the same device.
448 		 */
449 		if (vdev_guid != 0 && zcp->zc_data.zc_vdev_guid == vdev_guid)
450 			break;
451 	}
452 
453 	if (zcp == NULL) {
454 		fmd_case_t *cs;
455 		zfs_case_data_t data = { 0 };
456 
457 		/*
458 		 * If this is one of our 'fake' resource ereports, and there is
459 		 * no case open, simply discard it.
460 		 */
461 		if (isresource)
462 			return;
463 
464 		/*
465 		 * Open a new case.
466 		 */
467 		cs = fmd_case_open(hdl, NULL);
468 
469 		/*
470 		 * Initialize the case buffer.  To commonize code, we actually
471 		 * create the buffer with existing data, and then call
472 		 * zfs_case_unserialize() to instantiate the in-core structure.
473 		 */
474 		fmd_buf_create(hdl, cs, CASE_DATA,
475 		    sizeof (zfs_case_data_t));
476 
477 		data.zc_version = CASE_DATA_VERSION_SERD;
478 		data.zc_ena = ena;
479 		data.zc_pool_guid = pool_guid;
480 		data.zc_vdev_guid = vdev_guid;
481 		data.zc_pool_state = (int)pool_state;
482 
483 		fmd_buf_write(hdl, cs, CASE_DATA, &data, sizeof (data));
484 
485 		zcp = zfs_case_unserialize(hdl, cs);
486 		assert(zcp != NULL);
487 	}
488 
489 	if (isresource) {
490 		if (fmd_nvl_class_match(hdl, nvl,
491 		    ZFS_MAKE_RSRC(FM_RESOURCE_AUTOREPLACE))) {
492 			/*
493 			 * The 'resource.fs.zfs.autoreplace' event indicates
494 			 * that the pool was loaded with the 'autoreplace'
495 			 * property set.  In this case, any pending device
496 			 * failures should be ignored, as the asynchronous
497 			 * autoreplace handling will take care of them.
498 			 */
499 			fmd_case_close(hdl, zcp->zc_case);
500 		} else if (fmd_nvl_class_match(hdl, nvl,
501 		    ZFS_MAKE_RSRC(FM_RESOURCE_REMOVED))) {
502 			/*
503 			 * The 'resource.fs.zfs.removed' event indicates that
504 			 * device removal was detected, and the device was
505 			 * closed asynchronously.  If this is the case, we
506 			 * assume that any recent I/O errors were due to the
507 			 * device removal, not any fault of the device itself.
508 			 * We reset the SERD engine, and cancel any pending
509 			 * timers.
510 			 */
511 			if (zcp->zc_data.zc_has_remove_timer) {
512 				fmd_timer_remove(hdl, zcp->zc_remove_timer);
513 				zcp->zc_data.zc_has_remove_timer = 0;
514 				zfs_case_serialize(hdl, zcp);
515 			}
516 			if (zcp->zc_data.zc_serd_io[0] != '\0')
517 				fmd_serd_reset(hdl,
518 				    zcp->zc_data.zc_serd_io);
519 			if (zcp->zc_data.zc_serd_checksum[0] != '\0')
520 				fmd_serd_reset(hdl,
521 				    zcp->zc_data.zc_serd_checksum);
522 		}
523 		return;
524 	}
525 
526 	/*
527 	 * Associate the ereport with this case.
528 	 */
529 	fmd_case_add_ereport(hdl, zcp->zc_case, ep);
530 
531 	/*
532 	 * Don't do anything else if this case is already solved.
533 	 */
534 	if (fmd_case_solved(hdl, zcp->zc_case))
535 		return;
536 
537 	/*
538 	 * Determine if we should solve the case and generate a fault.  We solve
539 	 * a case if:
540 	 *
541 	 * 	a. A pool failed to open (ereport.fs.zfs.pool)
542 	 * 	b. A device failed to open (ereport.fs.zfs.pool) while a pool
543 	 *	   was up and running.
544 	 *
545 	 * We may see a series of ereports associated with a pool open, all
546 	 * chained together by the same ENA.  If the pool open succeeds, then
547 	 * we'll see no further ereports.  To detect when a pool open has
548 	 * succeeded, we associate a timer with the event.  When it expires, we
549 	 * close the case.
550 	 */
551 	if (fmd_nvl_class_match(hdl, nvl,
552 	    ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_POOL))) {
553 		/*
554 		 * Pool level fault.  Before solving the case, go through and
555 		 * close any open device cases that may be pending.
556 		 */
557 		for (dcp = uu_list_first(zfs_cases); dcp != NULL;
558 		    dcp = uu_list_next(zfs_cases, dcp)) {
559 			if (dcp->zc_data.zc_pool_guid ==
560 			    zcp->zc_data.zc_pool_guid &&
561 			    dcp->zc_data.zc_vdev_guid != 0)
562 				fmd_case_close(hdl, dcp->zc_case);
563 		}
564 
565 		zfs_case_solve(hdl, zcp, "fault.fs.zfs.pool", B_TRUE);
566 	} else if (fmd_nvl_class_match(hdl, nvl,
567 	    ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_LOG_REPLAY))) {
568 		/*
569 		 * Pool level fault for reading the intent logs.
570 		 */
571 		zfs_case_solve(hdl, zcp, "fault.fs.zfs.log_replay", B_TRUE);
572 	} else if (fmd_nvl_class_match(hdl, nvl, "ereport.fs.zfs.vdev.*")) {
573 		/*
574 		 * Device fault.  If this occurred during pool open, then defer
575 		 * reporting the fault.  If the pool itself could not be opeend,
576 		 * we only report the pool fault, not every device fault that
577 		 * may have caused the problem.  If we do not see a pool fault
578 		 * within the timeout period, then we'll solve the device case.
579 		 */
580 		zfs_case_solve(hdl, zcp, "fault.fs.zfs.device",  B_TRUE);
581 	} else if (fmd_nvl_class_match(hdl, nvl,
582 	    ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_IO)) ||
583 	    fmd_nvl_class_match(hdl, nvl,
584 	    ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_CHECKSUM)) ||
585 	    fmd_nvl_class_match(hdl, nvl,
586 	    ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_IO_FAILURE)) ||
587 	    fmd_nvl_class_match(hdl, nvl,
588 	    ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_PROBE_FAILURE))) {
589 		char *failmode = NULL;
590 		boolean_t checkremove = B_FALSE;
591 
592 		/*
593 		 * If this is a checksum or I/O error, then toss it into the
594 		 * appropriate SERD engine and check to see if it has fired.
595 		 * Ideally, we want to do something more sophisticated,
596 		 * (persistent errors for a single data block, etc).  For now,
597 		 * a single SERD engine is sufficient.
598 		 */
599 		if (fmd_nvl_class_match(hdl, nvl,
600 		    ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_IO))) {
601 			if (zcp->zc_data.zc_serd_io[0] == '\0') {
602 				zfs_serd_name(zcp->zc_data.zc_serd_io,
603 				    pool_guid, vdev_guid, "io");
604 				fmd_serd_create(hdl, zcp->zc_data.zc_serd_io,
605 				    fmd_prop_get_int32(hdl, "io_N"),
606 				    fmd_prop_get_int64(hdl, "io_T"));
607 				zfs_case_serialize(hdl, zcp);
608 			}
609 			if (fmd_serd_record(hdl, zcp->zc_data.zc_serd_io, ep))
610 				checkremove = B_TRUE;
611 		} else if (fmd_nvl_class_match(hdl, nvl,
612 		    ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_CHECKSUM))) {
613 			if (zcp->zc_data.zc_serd_checksum[0] == '\0') {
614 				zfs_serd_name(zcp->zc_data.zc_serd_checksum,
615 				    pool_guid, vdev_guid, "checksum");
616 				fmd_serd_create(hdl,
617 				    zcp->zc_data.zc_serd_checksum,
618 				    fmd_prop_get_int32(hdl, "checksum_N"),
619 				    fmd_prop_get_int64(hdl, "checksum_T"));
620 				zfs_case_serialize(hdl, zcp);
621 			}
622 			if (fmd_serd_record(hdl,
623 			    zcp->zc_data.zc_serd_checksum, ep)) {
624 				zfs_case_solve(hdl, zcp,
625 				    "fault.fs.zfs.vdev.checksum", B_FALSE);
626 			}
627 		} else if (fmd_nvl_class_match(hdl, nvl,
628 		    ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_IO_FAILURE)) &&
629 		    (nvlist_lookup_string(nvl,
630 		    FM_EREPORT_PAYLOAD_ZFS_POOL_FAILMODE, &failmode) == 0) &&
631 		    failmode != NULL) {
632 			if (strncmp(failmode, FM_EREPORT_FAILMODE_CONTINUE,
633 			    strlen(FM_EREPORT_FAILMODE_CONTINUE)) == 0) {
634 				zfs_case_solve(hdl, zcp,
635 				    "fault.fs.zfs.io_failure_continue",
636 				    B_FALSE);
637 			} else if (strncmp(failmode, FM_EREPORT_FAILMODE_WAIT,
638 			    strlen(FM_EREPORT_FAILMODE_WAIT)) == 0) {
639 				zfs_case_solve(hdl, zcp,
640 				    "fault.fs.zfs.io_failure_wait", B_FALSE);
641 			}
642 		} else if (fmd_nvl_class_match(hdl, nvl,
643 		    ZFS_MAKE_EREPORT(FM_EREPORT_ZFS_PROBE_FAILURE))) {
644 			checkremove = B_TRUE;
645 		}
646 
647 		/*
648 		 * Because I/O errors may be due to device removal, we postpone
649 		 * any diagnosis until we're sure that we aren't about to
650 		 * receive a 'resource.fs.zfs.removed' event.
651 		 */
652 		if (checkremove) {
653 			if (zcp->zc_data.zc_has_remove_timer)
654 				fmd_timer_remove(hdl, zcp->zc_remove_timer);
655 			zcp->zc_remove_timer = fmd_timer_install(hdl, zcp, NULL,
656 			    zfs_remove_timeout);
657 			if (!zcp->zc_data.zc_has_remove_timer) {
658 				zcp->zc_data.zc_has_remove_timer = 1;
659 				zfs_case_serialize(hdl, zcp);
660 			}
661 		}
662 	}
663 }
664 
665 /*
666  * The timeout is fired when we diagnosed an I/O error, and it was not due to
667  * device removal (which would cause the timeout to be cancelled).
668  */
669 /* ARGSUSED */
670 static void
671 zfs_fm_timeout(fmd_hdl_t *hdl, id_t id, void *data)
672 {
673 	zfs_case_t *zcp = data;
674 
675 	if (id == zcp->zc_remove_timer)
676 		zfs_case_solve(hdl, zcp, "fault.fs.zfs.vdev.io", B_FALSE);
677 }
678 
679 static void
680 zfs_fm_close(fmd_hdl_t *hdl, fmd_case_t *cs)
681 {
682 	zfs_case_t *zcp = fmd_case_getspecific(hdl, cs);
683 
684 	if (zcp->zc_data.zc_serd_checksum[0] != '\0')
685 		fmd_serd_destroy(hdl, zcp->zc_data.zc_serd_checksum);
686 	if (zcp->zc_data.zc_serd_io[0] != '\0')
687 		fmd_serd_destroy(hdl, zcp->zc_data.zc_serd_io);
688 	if (zcp->zc_data.zc_has_remove_timer)
689 		fmd_timer_remove(hdl, zcp->zc_remove_timer);
690 	uu_list_remove(zfs_cases, zcp);
691 	fmd_hdl_free(hdl, zcp, sizeof (zfs_case_t));
692 }
693 
694 /*
695  * We use the fmd gc entry point to look for old cases that no longer apply.
696  * This allows us to keep our set of case data small in a long running system.
697  */
698 static void
699 zfs_fm_gc(fmd_hdl_t *hdl)
700 {
701 	zfs_purge_cases(hdl);
702 }
703 
704 static const fmd_hdl_ops_t fmd_ops = {
705 	zfs_fm_recv,	/* fmdo_recv */
706 	zfs_fm_timeout,	/* fmdo_timeout */
707 	zfs_fm_close,	/* fmdo_close */
708 	NULL,		/* fmdo_stats */
709 	zfs_fm_gc,	/* fmdo_gc */
710 };
711 
712 static const fmd_prop_t fmd_props[] = {
713 	{ "case_timeout", FMD_TYPE_TIME, "5sec" },
714 	{ "checksum_N", FMD_TYPE_UINT32, "10" },
715 	{ "checksum_T", FMD_TYPE_TIME, "10min" },
716 	{ "io_N", FMD_TYPE_UINT32, "10" },
717 	{ "io_T", FMD_TYPE_TIME, "10min" },
718 	{ "remove_timeout", FMD_TYPE_TIME, "5sec" },
719 	{ NULL, 0, NULL }
720 };
721 
722 static const fmd_hdl_info_t fmd_info = {
723 	"ZFS Diagnosis Engine", "1.0", &fmd_ops, fmd_props
724 };
725 
726 void
727 _fmd_init(fmd_hdl_t *hdl)
728 {
729 	fmd_case_t *cp;
730 	libzfs_handle_t *zhdl;
731 
732 	if ((zhdl = libzfs_init()) == NULL)
733 		return;
734 
735 	if ((zfs_case_pool = uu_list_pool_create("zfs_case_pool",
736 	    sizeof (zfs_case_t), offsetof(zfs_case_t, zc_node),
737 	    NULL, 0)) == NULL) {
738 		libzfs_fini(zhdl);
739 		return;
740 	}
741 
742 	if ((zfs_cases = uu_list_create(zfs_case_pool, NULL, 0)) == NULL) {
743 		uu_list_pool_destroy(zfs_case_pool);
744 		libzfs_fini(zhdl);
745 		return;
746 	}
747 
748 	if (fmd_hdl_register(hdl, FMD_API_VERSION, &fmd_info) != 0) {
749 		uu_list_destroy(zfs_cases);
750 		uu_list_pool_destroy(zfs_case_pool);
751 		libzfs_fini(zhdl);
752 		return;
753 	}
754 
755 	fmd_hdl_setspecific(hdl, zhdl);
756 
757 	/*
758 	 * Iterate over all active cases and unserialize the associated buffers,
759 	 * adding them to our list of open cases.
760 	 */
761 	for (cp = fmd_case_next(hdl, NULL);
762 	    cp != NULL; cp = fmd_case_next(hdl, cp))
763 		(void) zfs_case_unserialize(hdl, cp);
764 
765 	/*
766 	 * Clear out any old cases that are no longer valid.
767 	 */
768 	zfs_purge_cases(hdl);
769 
770 	zfs_remove_timeout = fmd_prop_get_int64(hdl, "remove_timeout");
771 }
772 
773 void
774 _fmd_fini(fmd_hdl_t *hdl)
775 {
776 	zfs_case_t *zcp;
777 	uu_list_walk_t *walk;
778 	libzfs_handle_t *zhdl;
779 
780 	/*
781 	 * Remove all active cases.
782 	 */
783 	walk = uu_list_walk_start(zfs_cases, UU_WALK_ROBUST);
784 	while ((zcp = uu_list_walk_next(walk)) != NULL) {
785 		uu_list_remove(zfs_cases, zcp);
786 		fmd_hdl_free(hdl, zcp, sizeof (zfs_case_t));
787 	}
788 	uu_list_walk_end(walk);
789 
790 	uu_list_destroy(zfs_cases);
791 	uu_list_pool_destroy(zfs_case_pool);
792 
793 	zhdl = fmd_hdl_getspecific(hdl);
794 	libzfs_fini(zhdl);
795 }
796