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