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