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