xref: /titanic_50/usr/src/cmd/fm/fmd/common/fmd_case.c (revision 7c4dcc5546f9f002dfc2b95de47c90f00d07c066)
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 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * FMD Case Subsystem
31  *
32  * Diagnosis engines are expected to group telemetry events related to the
33  * diagnosis of a particular problem on the system into a set of cases.  The
34  * diagnosis engine may have any number of cases open at a given point in time.
35  * Some cases may eventually be *solved* by associating a suspect list of one
36  * or more problems with the case, at which point fmd publishes a list.suspect
37  * event for the case and it becomes visible to administrators and agents.
38  *
39  * Every case is named using a UUID, and is globally visible in the case hash.
40  * Cases are reference-counted, except for the reference from the case hash
41  * itself.  Consumers of case references include modules, which store active
42  * cases on the mod_cases list, ASRUs in the resource cache, and the RPC code.
43  *
44  * Cases obey the following state machine.  In states UNSOLVED, SOLVED, and
45  * CLOSE_WAIT, a case's module refers to the owning module (a diagnosis engine
46  * or transport) and the case is referenced by the mod_cases list.  Once the
47  * case reaches the CLOSED or REPAIRED states, a case's module changes to refer
48  * to the root module (fmd.d_rmod) and is deleted from the owner's mod_cases.
49  *
50  *			+------------+
51  *	     +----------|  UNSOLVED  |
52  *	     |		+------------+
53  *	   1 |	             4 |
54  *           |                 |
55  *	+----v---+ /-2->+------v-----+	  3	+--------+
56  *      | SOLVED |<     | CLOSE_WAIT |--------->| CLOSED |
57  *	+--------+ \-5->+------------+		+--------+
58  *	                       |                    |
59  *                           6 |                    | 7
60  *      		+------v-----+              |
61  *	                |  REPAIRED  |<-------------+
62  *			+------------+
63  *
64  * The state machine changes are triggered by calls to fmd_case_transition()
65  * from various locations inside of fmd, as described below:
66  *
67  * [1] Called by: fmd_case_solve()
68  *       Actions: FMD_CF_SOLVED flag is set in ci_flags
69  *                conviction policy is applied to suspect list
70  *                suspects convicted are marked faulty (F) in R$
71  *                list.suspect event logged and dispatched
72  *
73  * [2] Called by: fmd_case_close(), fmd_case_uuclose(), fmd_xprt_event_uuclose()
74  *       Actions: FMD_CF_ISOLATED flag is set in ci_flags
75  *                suspects convicted (F) are marked unusable (U) in R$
76  *                diagnosis engine fmdo_close() entry point scheduled
77  *                case transitions to CLOSED [3] upon exit from CLOSE_WAIT
78  *
79  * [3] Called by: fmd_case_delete() (after fmdo_close() entry point returns)
80  *       Actions: list.isolated event dispatched
81  *                case deleted from module's list of open cases
82  *
83  * [4] Called by: fmd_case_close(), fmd_case_uuclose()
84  *       Actions: diagnosis engine fmdo_close() entry point scheduled
85  *                case is subsequently discarded by fmd_case_delete()
86  *
87  * [5] Called by: fmd_case_repair(), fmd_case_update()
88  *       Actions: FMD_CF_REPAIR flag is set in ci_flags
89  *                diagnosis engine fmdo_close() entry point scheduled
90  *                case transitions to REPAIRED [6] upon exit from CLOSE_WAIT
91  *
92  * [6] Called by: fmd_case_repair(), fmd_case_update()
93  *       Actions: FMD_CF_REPAIR flag is set in ci_flags
94  *                suspects convicted are marked non faulty (!F) in R$
95  *                list.repaired event dispatched
96  *
97  * [7] Called by: fmd_case_repair(), fmd_case_update()
98  *       Actions: FMD_CF_REPAIR flag is set in ci_flags
99  *                suspects convicted are marked non faulty (!F) in R$
100  *                list.repaired event dispatched
101  */
102 
103 #include <sys/fm/protocol.h>
104 #include <uuid/uuid.h>
105 #include <alloca.h>
106 
107 #include <fmd_alloc.h>
108 #include <fmd_module.h>
109 #include <fmd_error.h>
110 #include <fmd_conf.h>
111 #include <fmd_case.h>
112 #include <fmd_string.h>
113 #include <fmd_subr.h>
114 #include <fmd_protocol.h>
115 #include <fmd_event.h>
116 #include <fmd_eventq.h>
117 #include <fmd_dispq.h>
118 #include <fmd_buf.h>
119 #include <fmd_log.h>
120 #include <fmd_asru.h>
121 #include <fmd_fmri.h>
122 #include <fmd_xprt.h>
123 
124 #include <fmd.h>
125 
126 static const char *const _fmd_case_snames[] = {
127 	"UNSOLVED",	/* FMD_CASE_UNSOLVED */
128 	"SOLVED",	/* FMD_CASE_SOLVED */
129 	"CLOSE_WAIT",	/* FMD_CASE_CLOSE_WAIT */
130 	"CLOSED",	/* FMD_CASE_CLOSED */
131 	"REPAIRED"	/* FMD_CASE_REPAIRED */
132 };
133 
134 fmd_case_hash_t *
135 fmd_case_hash_create(void)
136 {
137 	fmd_case_hash_t *chp = fmd_alloc(sizeof (fmd_case_hash_t), FMD_SLEEP);
138 
139 	(void) pthread_rwlock_init(&chp->ch_lock, NULL);
140 	chp->ch_hashlen = fmd.d_str_buckets;
141 	chp->ch_hash = fmd_zalloc(sizeof (void *) * chp->ch_hashlen, FMD_SLEEP);
142 	chp->ch_count = 0;
143 
144 	return (chp);
145 }
146 
147 /*
148  * Destroy the case hash.  Unlike most of our hash tables, no active references
149  * are kept by the case hash itself; all references come from other subsystems.
150  * The hash must be destroyed after all modules are unloaded; if anything was
151  * present in the hash it would be by definition a reference count leak.
152  */
153 void
154 fmd_case_hash_destroy(fmd_case_hash_t *chp)
155 {
156 	fmd_free(chp->ch_hash, sizeof (void *) * chp->ch_hashlen);
157 	fmd_free(chp, sizeof (fmd_case_hash_t));
158 }
159 
160 /*
161  * Take a snapshot of the case hash by placing an additional hold on each
162  * member in an auxiliary array, and then call 'func' for each case.
163  */
164 void
165 fmd_case_hash_apply(fmd_case_hash_t *chp,
166     void (*func)(fmd_case_t *, void *), void *arg)
167 {
168 	fmd_case_impl_t *cp, **cps, **cpp;
169 	uint_t cpc, i;
170 
171 	(void) pthread_rwlock_rdlock(&chp->ch_lock);
172 
173 	cps = cpp = fmd_alloc(chp->ch_count * sizeof (fmd_case_t *), FMD_SLEEP);
174 	cpc = chp->ch_count;
175 
176 	for (i = 0; i < chp->ch_hashlen; i++) {
177 		for (cp = chp->ch_hash[i]; cp != NULL; cp = cp->ci_next) {
178 			fmd_case_hold((fmd_case_t *)cp);
179 			*cpp++ = cp;
180 		}
181 	}
182 
183 	ASSERT(cpp == cps + cpc);
184 	(void) pthread_rwlock_unlock(&chp->ch_lock);
185 
186 	for (i = 0; i < cpc; i++) {
187 		func((fmd_case_t *)cps[i], arg);
188 		fmd_case_rele((fmd_case_t *)cps[i]);
189 	}
190 
191 	fmd_free(cps, cpc * sizeof (fmd_case_t *));
192 }
193 
194 /*
195  * Look up the diagcode for this case and cache it in ci_code.  If no suspects
196  * were defined for this case or if the lookup fails, the event dictionary or
197  * module code is broken, and we set the event code to a precomputed default.
198  */
199 static const char *
200 fmd_case_mkcode(fmd_case_t *cp)
201 {
202 	fmd_case_impl_t *cip = (fmd_case_impl_t *)cp;
203 	fmd_case_susp_t *cis;
204 
205 	char **keys, **keyp;
206 	const char *s;
207 
208 	ASSERT(MUTEX_HELD(&cip->ci_lock));
209 	ASSERT(cip->ci_state >= FMD_CASE_SOLVED);
210 
211 	fmd_free(cip->ci_code, cip->ci_codelen);
212 	cip->ci_codelen = cip->ci_mod->mod_codelen;
213 	cip->ci_code = fmd_zalloc(cip->ci_codelen, FMD_SLEEP);
214 	keys = keyp = alloca(sizeof (char *) * (cip->ci_nsuspects + 1));
215 
216 	for (cis = cip->ci_suspects; cis != NULL; cis = cis->cis_next) {
217 		if (nvlist_lookup_string(cis->cis_nvl, FM_CLASS, keyp) == 0)
218 			keyp++;
219 	}
220 
221 	*keyp = NULL; /* mark end of keys[] array for libdiagcode */
222 
223 	if (cip->ci_nsuspects == 0 || fmd_module_dc_key2code(
224 	    cip->ci_mod, keys, cip->ci_code, cip->ci_codelen) != 0) {
225 		(void) fmd_conf_getprop(fmd.d_conf, "nodiagcode", &s);
226 		fmd_free(cip->ci_code, cip->ci_codelen);
227 		cip->ci_codelen = strlen(s) + 1;
228 		cip->ci_code = fmd_zalloc(cip->ci_codelen, FMD_SLEEP);
229 		(void) strcpy(cip->ci_code, s);
230 	}
231 
232 	return (cip->ci_code);
233 }
234 
235 nvlist_t *
236 fmd_case_mkevent(fmd_case_t *cp, const char *class)
237 {
238 	fmd_case_impl_t *cip = (fmd_case_impl_t *)cp;
239 	fmd_case_susp_t *cis;
240 
241 	fmd_asru_hash_t *ahp = fmd.d_asrus;
242 	fmd_asru_t *asru;
243 
244 	nvlist_t **nva, **nvp, *nvl, *fmri;
245 	uint8_t *ba, *bp;
246 
247 	int msg = B_TRUE;
248 	boolean_t b;
249 
250 	(void) pthread_mutex_lock(&cip->ci_lock);
251 	ASSERT(cip->ci_state >= FMD_CASE_SOLVED);
252 
253 	nva = nvp = alloca(sizeof (nvlist_t *) * cip->ci_nsuspects);
254 	ba = bp = alloca(sizeof (uint8_t) * cip->ci_nsuspects);
255 
256 	/*
257 	 * For each suspect associated with the case, store its fault event
258 	 * nvlist in 'nva'.  We also look to see if any of the suspect faults
259 	 * have asked not to be messaged.  If any of them have made such a
260 	 * request, propagate that attribute to the composite list.* event.
261 	 * Finally, store each suspect's faulty status into the bitmap 'ba'.
262 	 */
263 	for (cis = cip->ci_suspects; cis != NULL; cis = cis->cis_next) {
264 		if (nvlist_lookup_boolean_value(cis->cis_nvl,
265 		    FM_SUSPECT_MESSAGE, &b) == 0 && b == B_FALSE)
266 			msg = B_FALSE;
267 
268 		if (nvlist_lookup_nvlist(cis->cis_nvl,
269 		    FM_FAULT_ASRU, &fmri) == 0 && (asru =
270 		    fmd_asru_hash_lookup_nvl(ahp, fmri, FMD_B_FALSE)) != NULL) {
271 			*bp++ = (asru->asru_flags & FMD_ASRU_FAULTY) != 0;
272 			fmd_asru_hash_release(ahp, asru);
273 		} else
274 			*bp++ = 0;
275 
276 		*nvp++ = cis->cis_nvl;
277 	}
278 
279 	if (cip->ci_code == NULL)
280 		(void) fmd_case_mkcode(cp);
281 
282 	nvl = fmd_protocol_list(class, cip->ci_mod->mod_fmri,
283 	    cip->ci_uuid, cip->ci_code, cip->ci_nsuspects, nva, ba, msg);
284 
285 	(void) pthread_mutex_unlock(&cip->ci_lock);
286 	return (nvl);
287 }
288 
289 /*
290  * Convict suspects in a case by applying a conviction policy and updating the
291  * resource cache prior to emitting the list.suspect event for the given case.
292  * At present, our policy is very simple: convict every suspect in the case.
293  * In the future, this policy can be extended and made configurable to permit:
294  *
295  * - convicting the suspect with the highest FIT rate
296  * - convicting the suspect with the cheapest FRU
297  * - convicting the suspect with the FRU that is in a depot's inventory
298  * - convicting the suspect with the longest lifetime
299  *
300  * and so forth.  A word to the wise: this problem is significantly harder that
301  * it seems at first glance.  Future work should heed the following advice:
302  *
303  * Hacking the policy into C code here is a very bad idea.  The policy needs to
304  * be decided upon very carefully and fundamentally encodes knowledge of what
305  * suspect list combinations can be emitted by what diagnosis engines.  As such
306  * fmd's code is the wrong location, because that would require fmd itself to
307  * be updated for every diagnosis engine change, defeating the entire design.
308  * The FMA Event Registry knows the suspect list combinations: policy inputs
309  * can be derived from it and used to produce per-module policy configuration.
310  *
311  * If the policy needs to be dynamic and not statically fixed at either fmd
312  * startup or module load time, any implementation of dynamic policy retrieval
313  * must employ some kind of caching mechanism or be part of a built-in module.
314  * The fmd_case_convict() function is called with locks held inside of fmd and
315  * is not a place where unbounded blocking on some inter-process or inter-
316  * system communication to another service (e.g. another daemon) can occur.
317  */
318 static void
319 fmd_case_convict(fmd_case_t *cp)
320 {
321 	fmd_case_impl_t *cip = (fmd_case_impl_t *)cp;
322 	fmd_asru_hash_t *ahp = fmd.d_asrus;
323 
324 	fmd_case_susp_t *cis;
325 	fmd_asru_t *asru;
326 	nvlist_t *fmri;
327 
328 	(void) pthread_mutex_lock(&cip->ci_lock);
329 	(void) fmd_case_mkcode(cp);
330 
331 	for (cis = cip->ci_suspects; cis != NULL; cis = cis->cis_next) {
332 		if (nvlist_lookup_nvlist(cis->cis_nvl, FM_FAULT_ASRU, &fmri))
333 			continue; /* no ASRU provided by diagnosis engine */
334 
335 		if ((asru = fmd_asru_hash_lookup_nvl(ahp,
336 		    fmri, FMD_B_TRUE)) == NULL) {
337 			fmd_error(EFMD_CASE_EVENT, "cannot convict suspect in "
338 			    "%s: %s\n", cip->ci_uuid, fmd_strerror(errno));
339 			continue;
340 		}
341 
342 		(void) fmd_asru_clrflags(asru,
343 		    FMD_ASRU_UNUSABLE, cp, cis->cis_nvl);
344 		(void) fmd_asru_setflags(asru,
345 		    FMD_ASRU_FAULTY, cp, cis->cis_nvl);
346 
347 		fmd_asru_hash_release(ahp, asru);
348 	}
349 
350 	(void) pthread_mutex_unlock(&cip->ci_lock);
351 }
352 
353 void
354 fmd_case_publish(fmd_case_t *cp, uint_t state)
355 {
356 	fmd_case_impl_t *cip = (fmd_case_impl_t *)cp;
357 	fmd_event_t *e;
358 	nvlist_t *nvl;
359 	char *class;
360 
361 	if (state == FMD_CASE_CURRENT)
362 		state = cip->ci_state; /* use current state */
363 
364 	switch (state) {
365 	case FMD_CASE_SOLVED:
366 		fmd_case_convict(cp);
367 		nvl = fmd_case_mkevent(cp, FM_LIST_SUSPECT_CLASS);
368 		(void) pthread_mutex_lock(&cip->ci_lock);
369 		if (cip->ci_diag == NULL)
370 			(void) nvlist_xdup(nvl, &cip->ci_diag, &fmd.d_nva);
371 		(void) pthread_mutex_unlock(&cip->ci_lock);
372 		(void) nvlist_lookup_string(nvl, FM_CLASS, &class);
373 
374 		e = fmd_event_create(FMD_EVT_PROTOCOL, FMD_HRT_NOW, nvl, class);
375 		(void) pthread_rwlock_rdlock(&fmd.d_log_lock);
376 		fmd_log_append(fmd.d_fltlog, e, cp);
377 		(void) pthread_rwlock_unlock(&fmd.d_log_lock);
378 		fmd_dispq_dispatch(fmd.d_disp, e, class);
379 
380 		(void) pthread_mutex_lock(&cip->ci_mod->mod_stats_lock);
381 		cip->ci_mod->mod_stats->ms_casesolved.fmds_value.ui64++;
382 		(void) pthread_mutex_unlock(&cip->ci_mod->mod_stats_lock);
383 
384 		break;
385 
386 	case FMD_CASE_CLOSE_WAIT:
387 		fmd_case_hold(cp);
388 		e = fmd_event_create(FMD_EVT_CLOSE, FMD_HRT_NOW, NULL, cp);
389 		fmd_eventq_insert_at_head(cip->ci_mod->mod_queue, e);
390 
391 		(void) pthread_mutex_lock(&cip->ci_mod->mod_stats_lock);
392 		cip->ci_mod->mod_stats->ms_caseclosed.fmds_value.ui64++;
393 		(void) pthread_mutex_unlock(&cip->ci_mod->mod_stats_lock);
394 
395 		break;
396 
397 	case FMD_CASE_CLOSED:
398 		nvl = fmd_case_mkevent(cp, FM_LIST_ISOLATED_CLASS);
399 		(void) nvlist_lookup_string(nvl, FM_CLASS, &class);
400 		e = fmd_event_create(FMD_EVT_PROTOCOL, FMD_HRT_NOW, nvl, class);
401 		fmd_dispq_dispatch(fmd.d_disp, e, class);
402 		break;
403 
404 	case FMD_CASE_REPAIRED:
405 		nvl = fmd_case_mkevent(cp, FM_LIST_REPAIRED_CLASS);
406 		(void) nvlist_lookup_string(nvl, FM_CLASS, &class);
407 		e = fmd_event_create(FMD_EVT_PROTOCOL, FMD_HRT_NOW, nvl, class);
408 		fmd_dispq_dispatch(fmd.d_disp, e, class);
409 		break;
410 	}
411 }
412 
413 fmd_case_t *
414 fmd_case_hash_lookup(fmd_case_hash_t *chp, const char *uuid)
415 {
416 	fmd_case_impl_t *cip;
417 	uint_t h;
418 
419 	(void) pthread_rwlock_rdlock(&chp->ch_lock);
420 	h = fmd_strhash(uuid) % chp->ch_hashlen;
421 
422 	for (cip = chp->ch_hash[h]; cip != NULL; cip = cip->ci_next) {
423 		if (strcmp(cip->ci_uuid, uuid) == 0)
424 			break;
425 	}
426 
427 	if (cip != NULL)
428 		fmd_case_hold((fmd_case_t *)cip);
429 	else
430 		(void) fmd_set_errno(EFMD_CASE_INVAL);
431 
432 	(void) pthread_rwlock_unlock(&chp->ch_lock);
433 	return ((fmd_case_t *)cip);
434 }
435 
436 static fmd_case_impl_t *
437 fmd_case_hash_insert(fmd_case_hash_t *chp, fmd_case_impl_t *cip)
438 {
439 	fmd_case_impl_t *eip;
440 	uint_t h;
441 
442 	(void) pthread_rwlock_wrlock(&chp->ch_lock);
443 	h = fmd_strhash(cip->ci_uuid) % chp->ch_hashlen;
444 
445 	for (eip = chp->ch_hash[h]; eip != NULL; eip = eip->ci_next) {
446 		if (strcmp(cip->ci_uuid, eip->ci_uuid) == 0) {
447 			fmd_case_hold((fmd_case_t *)eip);
448 			(void) pthread_rwlock_unlock(&chp->ch_lock);
449 			return (eip); /* uuid already present */
450 		}
451 	}
452 
453 	cip->ci_next = chp->ch_hash[h];
454 	chp->ch_hash[h] = cip;
455 
456 	chp->ch_count++;
457 	ASSERT(chp->ch_count != 0);
458 
459 	(void) pthread_rwlock_unlock(&chp->ch_lock);
460 	return (cip);
461 }
462 
463 static void
464 fmd_case_hash_delete(fmd_case_hash_t *chp, fmd_case_impl_t *cip)
465 {
466 	fmd_case_impl_t *cp, **pp;
467 	uint_t h;
468 
469 	(void) pthread_rwlock_wrlock(&chp->ch_lock);
470 
471 	h = fmd_strhash(cip->ci_uuid) % chp->ch_hashlen;
472 	pp = &chp->ch_hash[h];
473 
474 	for (cp = *pp; cp != NULL; cp = cp->ci_next) {
475 		if (cp != cip)
476 			pp = &cp->ci_next;
477 		else
478 			break;
479 	}
480 
481 	if (cp == NULL) {
482 		fmd_panic("case %p (%s) not found on hash chain %u\n",
483 		    (void *)cip, cip->ci_uuid, h);
484 	}
485 
486 	*pp = cp->ci_next;
487 	cp->ci_next = NULL;
488 
489 	ASSERT(chp->ch_count != 0);
490 	chp->ch_count--;
491 
492 	(void) pthread_rwlock_unlock(&chp->ch_lock);
493 }
494 
495 fmd_case_t *
496 fmd_case_create(fmd_module_t *mp, void *data)
497 {
498 	fmd_case_impl_t *cip = fmd_zalloc(sizeof (fmd_case_impl_t), FMD_SLEEP);
499 	fmd_case_impl_t *eip = NULL;
500 	uuid_t uuid;
501 
502 	(void) pthread_mutex_init(&cip->ci_lock, NULL);
503 	fmd_buf_hash_create(&cip->ci_bufs);
504 
505 	fmd_module_hold(mp);
506 	cip->ci_mod = mp;
507 	cip->ci_refs = 1;
508 	cip->ci_state = FMD_CASE_UNSOLVED;
509 	cip->ci_flags = FMD_CF_DIRTY;
510 	cip->ci_data = data;
511 
512 	/*
513 	 * Calling libuuid: get a clue.  The library interfaces cleverly do not
514 	 * define any constant for the length of an unparse string, and do not
515 	 * permit the caller to specify a buffer length for safety.  The spec
516 	 * says it will be 36 bytes, but we make it tunable just in case.
517 	 */
518 	(void) fmd_conf_getprop(fmd.d_conf, "uuidlen", &cip->ci_uuidlen);
519 	cip->ci_uuid = fmd_zalloc(cip->ci_uuidlen + 1, FMD_SLEEP);
520 
521 	/*
522 	 * We expect this loop to execute only once, but code it defensively
523 	 * against the possibility of libuuid bugs.  Keep generating uuids and
524 	 * attempting to do a hash insert until we get a unique one.
525 	 */
526 	do {
527 		if (eip != NULL)
528 			fmd_case_rele((fmd_case_t *)eip);
529 		uuid_generate(uuid);
530 		uuid_unparse(uuid, cip->ci_uuid);
531 	} while ((eip = fmd_case_hash_insert(fmd.d_cases, cip)) != cip);
532 
533 	ASSERT(fmd_module_locked(mp));
534 	fmd_list_append(&mp->mod_cases, cip);
535 	fmd_module_setcdirty(mp);
536 
537 	(void) pthread_mutex_lock(&cip->ci_mod->mod_stats_lock);
538 	cip->ci_mod->mod_stats->ms_caseopen.fmds_value.ui64++;
539 	(void) pthread_mutex_unlock(&cip->ci_mod->mod_stats_lock);
540 
541 	return ((fmd_case_t *)cip);
542 }
543 
544 fmd_case_t *
545 fmd_case_recreate(fmd_module_t *mp, fmd_xprt_t *xp,
546     uint_t state, const char *uuid, const char *code)
547 {
548 	fmd_case_impl_t *cip = fmd_zalloc(sizeof (fmd_case_impl_t), FMD_SLEEP);
549 	fmd_case_impl_t *eip;
550 
551 	ASSERT(state < FMD_CASE_REPAIRED);
552 
553 	(void) pthread_mutex_init(&cip->ci_lock, NULL);
554 	fmd_buf_hash_create(&cip->ci_bufs);
555 
556 	fmd_module_hold(mp);
557 	cip->ci_mod = mp;
558 	cip->ci_xprt = xp;
559 	cip->ci_refs = 1;
560 	cip->ci_state = state;
561 	cip->ci_uuid = fmd_strdup(uuid, FMD_SLEEP);
562 	cip->ci_uuidlen = strlen(cip->ci_uuid);
563 	cip->ci_code = fmd_strdup(code, FMD_SLEEP);
564 	cip->ci_codelen = cip->ci_code ? strlen(cip->ci_code) + 1 : 0;
565 
566 	if (state > FMD_CASE_CLOSE_WAIT)
567 		cip->ci_flags |= FMD_CF_SOLVED;
568 
569 	/*
570 	 * Insert the case into the global case hash.  If the specified UUID is
571 	 * already present, check to see if it is an orphan: if so, reclaim it;
572 	 * otherwise if it is owned by a different module then return NULL.
573 	 */
574 	if ((eip = fmd_case_hash_insert(fmd.d_cases, cip)) != cip) {
575 		(void) pthread_mutex_lock(&cip->ci_lock);
576 		cip->ci_refs--; /* decrement to zero */
577 		fmd_case_destroy((fmd_case_t *)cip, B_FALSE);
578 
579 		cip = eip; /* switch 'cip' to the existing case */
580 		(void) pthread_mutex_lock(&cip->ci_lock);
581 
582 		/*
583 		 * If the ASRU cache is trying to recreate an orphan, then just
584 		 * return the existing case that we found without changing it.
585 		 */
586 		if (mp == fmd.d_rmod) {
587 			(void) pthread_mutex_unlock(&cip->ci_lock);
588 			fmd_case_rele((fmd_case_t *)cip);
589 			return ((fmd_case_t *)cip);
590 		}
591 
592 		/*
593 		 * If the existing case isn't an orphan or is being proxied,
594 		 * then we have a UUID conflict: return failure to the caller.
595 		 */
596 		if (cip->ci_mod != fmd.d_rmod || xp != NULL) {
597 			(void) pthread_mutex_unlock(&cip->ci_lock);
598 			fmd_case_rele((fmd_case_t *)cip);
599 			return (NULL);
600 		}
601 
602 		/*
603 		 * If the new module is reclaiming an orphaned case, remove
604 		 * the case from the root module, switch ci_mod, and then fall
605 		 * through to adding the case to the new owner module 'mp'.
606 		 */
607 		fmd_module_lock(cip->ci_mod);
608 		fmd_list_delete(&cip->ci_mod->mod_cases, cip);
609 		fmd_module_unlock(cip->ci_mod);
610 
611 		fmd_module_rele(cip->ci_mod);
612 		cip->ci_mod = mp;
613 		fmd_module_hold(mp);
614 
615 		(void) pthread_mutex_unlock(&cip->ci_lock);
616 		fmd_case_rele((fmd_case_t *)cip);
617 	}
618 
619 	ASSERT(fmd_module_locked(mp));
620 	fmd_list_append(&mp->mod_cases, cip);
621 
622 	(void) pthread_mutex_lock(&cip->ci_mod->mod_stats_lock);
623 	cip->ci_mod->mod_stats->ms_caseopen.fmds_value.ui64++;
624 	(void) pthread_mutex_unlock(&cip->ci_mod->mod_stats_lock);
625 
626 	return ((fmd_case_t *)cip);
627 }
628 
629 void
630 fmd_case_destroy(fmd_case_t *cp, int visible)
631 {
632 	fmd_case_impl_t *cip = (fmd_case_impl_t *)cp;
633 	fmd_case_item_t *cit, *ncit;
634 	fmd_case_susp_t *cis, *ncis;
635 
636 	ASSERT(MUTEX_HELD(&cip->ci_lock));
637 	ASSERT(cip->ci_refs == 0);
638 
639 	if (visible) {
640 		TRACE((FMD_DBG_CASE, "deleting case %s", cip->ci_uuid));
641 		fmd_case_hash_delete(fmd.d_cases, cip);
642 	}
643 
644 	for (cit = cip->ci_items; cit != NULL; cit = ncit) {
645 		ncit = cit->cit_next;
646 		fmd_event_rele(cit->cit_event);
647 		fmd_free(cit, sizeof (fmd_case_item_t));
648 	}
649 
650 	for (cis = cip->ci_suspects; cis != NULL; cis = ncis) {
651 		ncis = cis->cis_next;
652 		nvlist_free(cis->cis_nvl);
653 		fmd_free(cis, sizeof (fmd_case_susp_t));
654 	}
655 
656 	if (cip->ci_principal != NULL)
657 		fmd_event_rele(cip->ci_principal);
658 
659 	fmd_free(cip->ci_uuid, cip->ci_uuidlen + 1);
660 	fmd_free(cip->ci_code, cip->ci_codelen);
661 	(void) fmd_buf_hash_destroy(&cip->ci_bufs);
662 
663 	if (cip->ci_diag != NULL)
664 		nvlist_free(cip->ci_diag);
665 
666 	fmd_module_rele(cip->ci_mod);
667 	fmd_free(cip, sizeof (fmd_case_impl_t));
668 }
669 
670 void
671 fmd_case_hold(fmd_case_t *cp)
672 {
673 	fmd_case_impl_t *cip = (fmd_case_impl_t *)cp;
674 
675 	(void) pthread_mutex_lock(&cip->ci_lock);
676 	cip->ci_refs++;
677 	ASSERT(cip->ci_refs != 0);
678 	(void) pthread_mutex_unlock(&cip->ci_lock);
679 }
680 
681 void
682 fmd_case_hold_locked(fmd_case_t *cp)
683 {
684 	fmd_case_impl_t *cip = (fmd_case_impl_t *)cp;
685 
686 	ASSERT(MUTEX_HELD(&cip->ci_lock));
687 	cip->ci_refs++;
688 	ASSERT(cip->ci_refs != 0);
689 }
690 
691 void
692 fmd_case_rele(fmd_case_t *cp)
693 {
694 	fmd_case_impl_t *cip = (fmd_case_impl_t *)cp;
695 
696 	(void) pthread_mutex_lock(&cip->ci_lock);
697 	ASSERT(cip->ci_refs != 0);
698 
699 	if (--cip->ci_refs == 0)
700 		fmd_case_destroy((fmd_case_t *)cip, B_TRUE);
701 	else
702 		(void) pthread_mutex_unlock(&cip->ci_lock);
703 }
704 
705 int
706 fmd_case_insert_principal(fmd_case_t *cp, fmd_event_t *ep)
707 {
708 	fmd_case_impl_t *cip = (fmd_case_impl_t *)cp;
709 	fmd_case_item_t *cit;
710 	fmd_event_t *oep;
711 	uint_t state;
712 	int new;
713 
714 	fmd_event_hold(ep);
715 	(void) pthread_mutex_lock(&cip->ci_lock);
716 
717 	if (cip->ci_flags & FMD_CF_SOLVED)
718 		state = FMD_EVS_DIAGNOSED;
719 	else
720 		state = FMD_EVS_ACCEPTED;
721 
722 	oep = cip->ci_principal;
723 	cip->ci_principal = ep;
724 
725 	for (cit = cip->ci_items; cit != NULL; cit = cit->cit_next) {
726 		if (cit->cit_event == ep)
727 			break;
728 	}
729 
730 	cip->ci_flags |= FMD_CF_DIRTY;
731 	new = cit == NULL && ep != oep;
732 
733 	(void) pthread_mutex_unlock(&cip->ci_lock);
734 
735 	fmd_module_setcdirty(cip->ci_mod);
736 	fmd_event_transition(ep, state);
737 
738 	if (oep != NULL)
739 		fmd_event_rele(oep);
740 
741 	return (new);
742 }
743 
744 int
745 fmd_case_insert_event(fmd_case_t *cp, fmd_event_t *ep)
746 {
747 	fmd_case_impl_t *cip = (fmd_case_impl_t *)cp;
748 	fmd_case_item_t *cit;
749 	uint_t state;
750 	int new;
751 
752 	(void) pthread_mutex_lock(&cip->ci_lock);
753 
754 	if (cip->ci_flags & FMD_CF_SOLVED)
755 		state = FMD_EVS_DIAGNOSED;
756 	else
757 		state = FMD_EVS_ACCEPTED;
758 
759 	for (cit = cip->ci_items; cit != NULL; cit = cit->cit_next) {
760 		if (cit->cit_event == ep)
761 			break;
762 	}
763 
764 	new = cit == NULL && ep != cip->ci_principal;
765 
766 	/*
767 	 * If the event is already in the case or the case is already solved,
768 	 * there is no reason to save it: just transition it appropriately.
769 	 */
770 	if (cit != NULL || (cip->ci_flags & FMD_CF_SOLVED)) {
771 		(void) pthread_mutex_unlock(&cip->ci_lock);
772 		fmd_event_transition(ep, state);
773 		return (new);
774 	}
775 
776 	cit = fmd_alloc(sizeof (fmd_case_item_t), FMD_SLEEP);
777 	fmd_event_hold(ep);
778 
779 	cit->cit_next = cip->ci_items;
780 	cit->cit_event = ep;
781 
782 	cip->ci_items = cit;
783 	cip->ci_nitems++;
784 
785 	cip->ci_flags |= FMD_CF_DIRTY;
786 	(void) pthread_mutex_unlock(&cip->ci_lock);
787 
788 	fmd_module_setcdirty(cip->ci_mod);
789 	fmd_event_transition(ep, state);
790 
791 	return (new);
792 }
793 
794 void
795 fmd_case_insert_suspect(fmd_case_t *cp, nvlist_t *nvl)
796 {
797 	fmd_case_impl_t *cip = (fmd_case_impl_t *)cp;
798 	fmd_case_susp_t *cis = fmd_alloc(sizeof (fmd_case_susp_t), FMD_SLEEP);
799 
800 	(void) pthread_mutex_lock(&cip->ci_lock);
801 	ASSERT(cip->ci_state < FMD_CASE_SOLVED);
802 	cip->ci_flags |= FMD_CF_DIRTY;
803 
804 	cis->cis_next = cip->ci_suspects;
805 	cis->cis_nvl = nvl;
806 
807 	cip->ci_suspects = cis;
808 	cip->ci_nsuspects++;
809 
810 	(void) pthread_mutex_unlock(&cip->ci_lock);
811 	fmd_module_setcdirty(cip->ci_mod);
812 }
813 
814 void
815 fmd_case_recreate_suspect(fmd_case_t *cp, nvlist_t *nvl)
816 {
817 	fmd_case_impl_t *cip = (fmd_case_impl_t *)cp;
818 	fmd_case_susp_t *cis = fmd_alloc(sizeof (fmd_case_susp_t), FMD_SLEEP);
819 
820 	(void) pthread_mutex_lock(&cip->ci_lock);
821 	ASSERT(cip->ci_state == FMD_CASE_CLOSED);
822 	ASSERT(cip->ci_mod == fmd.d_rmod);
823 
824 	cis->cis_next = cip->ci_suspects;
825 	cis->cis_nvl = nvl;
826 
827 	cip->ci_suspects = cis;
828 	cip->ci_nsuspects++;
829 
830 	(void) pthread_mutex_unlock(&cip->ci_lock);
831 }
832 
833 void
834 fmd_case_reset_suspects(fmd_case_t *cp)
835 {
836 	fmd_case_impl_t *cip = (fmd_case_impl_t *)cp;
837 	fmd_case_susp_t *cis, *ncis;
838 
839 	(void) pthread_mutex_lock(&cip->ci_lock);
840 	ASSERT(cip->ci_state < FMD_CASE_SOLVED);
841 
842 	for (cis = cip->ci_suspects; cis != NULL; cis = ncis) {
843 		ncis = cis->cis_next;
844 		nvlist_free(cis->cis_nvl);
845 		fmd_free(cis, sizeof (fmd_case_susp_t));
846 	}
847 
848 	cip->ci_flags |= FMD_CF_DIRTY;
849 	cip->ci_suspects = NULL;
850 	cip->ci_nsuspects = 0;
851 
852 	(void) pthread_mutex_unlock(&cip->ci_lock);
853 	fmd_module_setcdirty(cip->ci_mod);
854 }
855 
856 /*
857  * Grab ci_lock and update the case state and set the dirty bit.  Then perform
858  * whatever actions and emit whatever events are appropriate for the state.
859  * Refer to the topmost block comment explaining the state machine for details.
860  */
861 void
862 fmd_case_transition(fmd_case_t *cp, uint_t state, uint_t flags)
863 {
864 	fmd_case_impl_t *cip = (fmd_case_impl_t *)cp;
865 
866 	uint_t old_state;
867 	fmd_case_susp_t *cis;
868 	fmd_case_item_t *cit;
869 	fmd_asru_t *asru;
870 	fmd_event_t *e;
871 	nvlist_t *nvl;
872 
873 	ASSERT(state <= FMD_CASE_REPAIRED);
874 	(void) pthread_mutex_lock(&cip->ci_lock);
875 	cip->ci_flags |= flags;
876 
877 	if (cip->ci_state >= state) {
878 		(void) pthread_mutex_unlock(&cip->ci_lock);
879 		return; /* already in specified state */
880 	}
881 
882 	TRACE((FMD_DBG_CASE, "case %s %s->%s", cip->ci_uuid,
883 	    _fmd_case_snames[cip->ci_state], _fmd_case_snames[state]));
884 
885 	old_state = cip->ci_state;
886 	cip->ci_state = state;
887 	cip->ci_flags |= FMD_CF_DIRTY;
888 
889 	if (cip->ci_xprt == NULL && cip->ci_mod != fmd.d_rmod)
890 		fmd_module_setcdirty(cip->ci_mod);
891 
892 	switch (state) {
893 	case FMD_CASE_SOLVED:
894 		for (cit = cip->ci_items; cit != NULL; cit = cit->cit_next)
895 			fmd_event_transition(cit->cit_event, FMD_EVS_DIAGNOSED);
896 
897 		if (cip->ci_principal != NULL) {
898 			fmd_event_transition(cip->ci_principal,
899 			    FMD_EVS_DIAGNOSED);
900 		}
901 		break;
902 
903 	case FMD_CASE_CLOSE_WAIT:
904 		/*
905 		 * If the case was never solved, do not change ASRUs.
906 		 * If the case was never fmd_case_closed, do not change ASRUs.
907 		 * If the case was repaired, do not change ASRUs.
908 		 */
909 		if ((cip->ci_flags & (FMD_CF_SOLVED | FMD_CF_ISOLATED |
910 		    FMD_CF_REPAIRED)) != (FMD_CF_SOLVED | FMD_CF_ISOLATED))
911 			goto close_wait_finish;
912 
913 		/*
914 		 * For each fault event in the suspect list, attempt to look up
915 		 * the corresponding ASRU in the ASRU dictionary.  If the ASRU
916 		 * is found there and is marked faulty, we now mark it unusable
917 		 * and record the case meta-data and fault event with the ASRU.
918 		 */
919 		for (cis = cip->ci_suspects; cis != NULL; cis = cis->cis_next) {
920 			if (nvlist_lookup_nvlist(cis->cis_nvl, FM_FAULT_ASRU,
921 			    &nvl) == 0 && (asru = fmd_asru_hash_lookup_nvl(
922 			    fmd.d_asrus, nvl, FMD_B_FALSE)) != NULL) {
923 				(void) fmd_asru_setflags(asru,
924 				    FMD_ASRU_UNUSABLE, cp, cis->cis_nvl);
925 				fmd_asru_hash_release(fmd.d_asrus, asru);
926 			}
927 		}
928 
929 	close_wait_finish:
930 		if (!fmd_case_orphaned(cp))
931 			break; /* state transition complete */
932 
933 		/*
934 		 * If an orphaned case transitions to CLOSE_WAIT, the owning
935 		 * module is no longer loaded: continue on to CASE_CLOSED.
936 		 */
937 		state = cip->ci_state = FMD_CASE_CLOSED;
938 		/*FALLTHRU*/
939 
940 	case FMD_CASE_CLOSED:
941 		ASSERT(fmd_case_orphaned(cp));
942 		fmd_module_lock(cip->ci_mod);
943 		fmd_list_append(&cip->ci_mod->mod_cases, cip);
944 		fmd_module_unlock(cip->ci_mod);
945 		break;
946 
947 	case FMD_CASE_REPAIRED:
948 		ASSERT(fmd_case_orphaned(cp));
949 
950 		if (old_state == FMD_CASE_CLOSE_WAIT)
951 			break; /* case was never closed (transition 6 above) */
952 
953 		fmd_module_lock(cip->ci_mod);
954 		fmd_list_delete(&cip->ci_mod->mod_cases, cip);
955 		fmd_module_unlock(cip->ci_mod);
956 		break;
957 	}
958 
959 	(void) pthread_mutex_unlock(&cip->ci_lock);
960 
961 	/*
962 	 * If the module has initialized, then publish the appropriate event
963 	 * for the new case state.  If not, we are being called from the
964 	 * checkpoint code during module load, in which case the module's
965 	 * _fmd_init() routine hasn't finished yet, and our event dictionaries
966 	 * may not be open yet, which will prevent us from computing the event
967 	 * code.  Defer the call to fmd_case_publish() by enqueuing a PUBLISH
968 	 * event in our queue: this won't be processed until _fmd_init is done.
969 	 */
970 	if (cip->ci_mod->mod_flags & FMD_MOD_INIT)
971 		fmd_case_publish(cp, state);
972 	else {
973 		fmd_case_hold(cp);
974 		e = fmd_event_create(FMD_EVT_PUBLISH, FMD_HRT_NOW, NULL, cp);
975 		fmd_eventq_insert_at_head(cip->ci_mod->mod_queue, e);
976 	}
977 
978 	/*
979 	 * If we transitioned to CLOSED or REPAIRED, adjust the reference count
980 	 * to reflect our addition to or removal from fmd.d_rmod->mod_cases.
981 	 */
982 	if (state == FMD_CASE_CLOSED)
983 		fmd_case_hold(cp);
984 	else if (state == FMD_CASE_REPAIRED && old_state != FMD_CASE_CLOSE_WAIT)
985 		fmd_case_rele(cp);
986 }
987 
988 /*
989  * Transition the specified case to *at least* the specified state by first
990  * re-validating the suspect list using the resource cache.  This function is
991  * employed by the checkpoint code when restoring a saved, solved case to see
992  * if the state of the case has effectively changed while fmd was not running
993  * or the module was not loaded.  If none of the suspects are present anymore,
994  * advance the state to REPAIRED.  If none are usable, advance to CLOSE_WAIT.
995  */
996 void
997 fmd_case_transition_update(fmd_case_t *cp, uint_t state, uint_t flags)
998 {
999 	fmd_case_impl_t *cip = (fmd_case_impl_t *)cp;
1000 	fmd_case_susp_t *cis;
1001 	fmd_asru_t *asru;
1002 	nvlist_t *nvl;
1003 
1004 	int present = 0;	/* are any suspects present? */
1005 	int usable = 0;		/* are any suspects usable? */
1006 
1007 	ASSERT(state >= FMD_CASE_SOLVED);
1008 	(void) pthread_mutex_lock(&cip->ci_lock);
1009 
1010 	for (cis = cip->ci_suspects; cis != NULL; cis = cis->cis_next) {
1011 		if (nvlist_lookup_nvlist(cis->cis_nvl, FM_FAULT_ASRU,
1012 		    &nvl) == 0 && (asru = fmd_asru_hash_lookup_nvl(
1013 		    fmd.d_asrus, nvl, FMD_B_TRUE)) != NULL) {
1014 
1015 			if ((asru->asru_flags & FMD_ASRU_INTERNAL) ||
1016 			    fmd_fmri_present(asru->asru_fmri) > 0)
1017 				present++;
1018 
1019 			if (fmd_fmri_unusable(asru->asru_fmri) <= 0)
1020 				usable++;
1021 
1022 			fmd_asru_hash_release(fmd.d_asrus, asru);
1023 		}
1024 	}
1025 
1026 	(void) pthread_mutex_unlock(&cip->ci_lock);
1027 
1028 	if (!present) {
1029 		state = MAX(state, FMD_CASE_CLOSE_WAIT);
1030 		flags |= FMD_CF_REPAIRED;
1031 	} else if (!usable) {
1032 		state = MAX(state, FMD_CASE_CLOSE_WAIT);
1033 		flags |= FMD_CF_ISOLATED;
1034 	}
1035 
1036 	fmd_case_transition(cp, state, flags);
1037 }
1038 
1039 void
1040 fmd_case_setdirty(fmd_case_t *cp)
1041 {
1042 	fmd_case_impl_t *cip = (fmd_case_impl_t *)cp;
1043 
1044 	(void) pthread_mutex_lock(&cip->ci_lock);
1045 	cip->ci_flags |= FMD_CF_DIRTY;
1046 	(void) pthread_mutex_unlock(&cip->ci_lock);
1047 
1048 	fmd_module_setcdirty(cip->ci_mod);
1049 }
1050 
1051 void
1052 fmd_case_clrdirty(fmd_case_t *cp)
1053 {
1054 	fmd_case_impl_t *cip = (fmd_case_impl_t *)cp;
1055 
1056 	(void) pthread_mutex_lock(&cip->ci_lock);
1057 	cip->ci_flags &= ~FMD_CF_DIRTY;
1058 	(void) pthread_mutex_unlock(&cip->ci_lock);
1059 }
1060 
1061 void
1062 fmd_case_commit(fmd_case_t *cp)
1063 {
1064 	fmd_case_impl_t *cip = (fmd_case_impl_t *)cp;
1065 	fmd_case_item_t *cit;
1066 
1067 	(void) pthread_mutex_lock(&cip->ci_lock);
1068 
1069 	if (cip->ci_flags & FMD_CF_DIRTY) {
1070 		for (cit = cip->ci_items; cit != NULL; cit = cit->cit_next)
1071 			fmd_event_commit(cit->cit_event);
1072 
1073 		if (cip->ci_principal != NULL)
1074 			fmd_event_commit(cip->ci_principal);
1075 
1076 		fmd_buf_hash_commit(&cip->ci_bufs);
1077 		cip->ci_flags &= ~FMD_CF_DIRTY;
1078 	}
1079 
1080 	(void) pthread_mutex_unlock(&cip->ci_lock);
1081 }
1082 
1083 /*
1084  * Indicate that the case may need to change state because one or more of the
1085  * ASRUs named as a suspect has changed state.  We examine all the suspects
1086  * and if none are still faulty, we initiate a case close transition.
1087  */
1088 void
1089 fmd_case_update(fmd_case_t *cp)
1090 {
1091 	fmd_case_impl_t *cip = (fmd_case_impl_t *)cp;
1092 	fmd_case_susp_t *cis;
1093 	fmd_asru_t *asru;
1094 	nvlist_t *nvl;
1095 
1096 	int astate = 0;
1097 	uint_t cstate;
1098 
1099 	(void) pthread_mutex_lock(&cip->ci_lock);
1100 	cstate = cip->ci_state;
1101 
1102 	if (cip->ci_xprt != NULL || cip->ci_state < FMD_CASE_SOLVED) {
1103 		(void) pthread_mutex_unlock(&cip->ci_lock);
1104 		return; /* update is not appropriate */
1105 	}
1106 
1107 	for (cis = cip->ci_suspects; cis != NULL; cis = cis->cis_next) {
1108 		if (nvlist_lookup_nvlist(cis->cis_nvl, FM_FAULT_ASRU,
1109 		    &nvl) == 0 && (asru = fmd_asru_hash_lookup_nvl(
1110 		    fmd.d_asrus, nvl, FMD_B_FALSE)) != NULL) {
1111 			astate |= fmd_asru_getstate(asru);
1112 			fmd_asru_hash_release(fmd.d_asrus, asru);
1113 		}
1114 	}
1115 
1116 	(void) pthread_mutex_unlock(&cip->ci_lock);
1117 
1118 	if (astate & FMD_ASRU_FAULTY)
1119 		return; /* one or more suspects are still marked faulty */
1120 
1121 	if (cstate == FMD_CASE_CLOSED)
1122 		fmd_case_transition(cp, FMD_CASE_REPAIRED, FMD_CF_REPAIRED);
1123 	else
1124 		fmd_case_transition(cp, FMD_CASE_CLOSE_WAIT, FMD_CF_REPAIRED);
1125 }
1126 
1127 /*
1128  * Delete a closed case from the module's case list once the fmdo_close() entry
1129  * point has run to completion.  If the case is owned by a transport module,
1130  * tell the transport to proxy a case close on the other end of the transport.
1131  * If not, transition to the appropriate next state based on ci_flags.  This
1132  * function represents the end of CLOSE_WAIT and transitions the case to either
1133  * CLOSED or REPAIRED or discards it entirely because it was never solved;
1134  * refer to the topmost block comment explaining the state machine for details.
1135  */
1136 void
1137 fmd_case_delete(fmd_case_t *cp)
1138 {
1139 	fmd_case_impl_t *cip = (fmd_case_impl_t *)cp;
1140 	fmd_modstat_t *msp;
1141 	size_t buftotal;
1142 
1143 	ASSERT(fmd_module_locked(cip->ci_mod));
1144 	fmd_list_delete(&cip->ci_mod->mod_cases, cip);
1145 	buftotal = fmd_buf_hash_destroy(&cip->ci_bufs);
1146 
1147 	(void) pthread_mutex_lock(&cip->ci_mod->mod_stats_lock);
1148 	msp = cip->ci_mod->mod_stats;
1149 
1150 	ASSERT(msp->ms_caseopen.fmds_value.ui64 != 0);
1151 	msp->ms_caseopen.fmds_value.ui64--;
1152 
1153 	ASSERT(msp->ms_buftotal.fmds_value.ui64 >= buftotal);
1154 	msp->ms_buftotal.fmds_value.ui64 -= buftotal;
1155 
1156 	(void) pthread_mutex_unlock(&cip->ci_mod->mod_stats_lock);
1157 
1158 	if (cip->ci_xprt == NULL)
1159 		fmd_module_setcdirty(cip->ci_mod);
1160 
1161 	fmd_module_rele(cip->ci_mod);
1162 	cip->ci_mod = fmd.d_rmod;
1163 	fmd_module_hold(cip->ci_mod);
1164 
1165 	/*
1166 	 * If a proxied case finishes CLOSE_WAIT, then it can be discarded
1167 	 * rather than orphaned because by definition it can have no entries
1168 	 * in the resource cache of the current fault manager.
1169 	 */
1170 	if (cip->ci_xprt != NULL)
1171 		fmd_xprt_uuclose(cip->ci_xprt, cip->ci_uuid);
1172 	else if (cip->ci_flags & FMD_CF_REPAIRED)
1173 		fmd_case_transition(cp, FMD_CASE_REPAIRED, 0);
1174 	else if (cip->ci_flags & FMD_CF_ISOLATED)
1175 		fmd_case_transition(cp, FMD_CASE_CLOSED, 0);
1176 
1177 	fmd_case_rele(cp);
1178 }
1179 
1180 void
1181 fmd_case_discard(fmd_case_t *cp)
1182 {
1183 	fmd_case_impl_t *cip = (fmd_case_impl_t *)cp;
1184 
1185 	(void) pthread_mutex_lock(&cip->ci_mod->mod_stats_lock);
1186 	cip->ci_mod->mod_stats->ms_caseopen.fmds_value.ui64--;
1187 	(void) pthread_mutex_unlock(&cip->ci_mod->mod_stats_lock);
1188 
1189 	ASSERT(fmd_module_locked(cip->ci_mod));
1190 	fmd_list_delete(&cip->ci_mod->mod_cases, cip);
1191 	fmd_case_rele(cp);
1192 }
1193 
1194 /*
1195  * Indicate that the problem corresponding to a case has been repaired by
1196  * clearing the faulty bit on each ASRU named as a suspect.  If the case hasn't
1197  * already been closed, this function initiates the transition to CLOSE_WAIT.
1198  * The caller must have the case held from fmd_case_hash_lookup(), so we can
1199  * grab and drop ci_lock without the case being able to be freed in between.
1200  */
1201 int
1202 fmd_case_repair(fmd_case_t *cp)
1203 {
1204 	fmd_case_impl_t *cip = (fmd_case_impl_t *)cp;
1205 	fmd_case_susp_t *cis;
1206 	nvlist_t *nvl;
1207 	uint_t cstate;
1208 
1209 	fmd_asru_hash_t *ahp = fmd.d_asrus;
1210 	fmd_asru_t **aa;
1211 	uint_t i, an;
1212 
1213 	(void) pthread_mutex_lock(&cip->ci_lock);
1214 	cstate = cip->ci_state;
1215 
1216 	if (cip->ci_xprt != NULL) {
1217 		(void) pthread_mutex_unlock(&cip->ci_lock);
1218 		return (fmd_set_errno(EFMD_CASE_OWNER));
1219 	}
1220 
1221 	if (cstate < FMD_CASE_SOLVED) {
1222 		(void) pthread_mutex_unlock(&cip->ci_lock);
1223 		return (fmd_set_errno(EFMD_CASE_STATE));
1224 	}
1225 
1226 	/*
1227 	 * Take a snapshot of any ASRUs referenced by the case that are present
1228 	 * in the resource cache.  Then drop ci_lock and clear the faulty bit
1229 	 * on each ASRU (we can't call fmd_asru_clrflags() with ci_lock held).
1230 	 */
1231 	an = cip->ci_nsuspects;
1232 	aa = alloca(sizeof (fmd_asru_t *) * an);
1233 	bzero(aa, sizeof (fmd_asru_t *) * an);
1234 
1235 	for (i = 0, cis = cip->ci_suspects;
1236 	    cis != NULL; cis = cis->cis_next, i++) {
1237 		if (nvlist_lookup_nvlist(cis->cis_nvl,
1238 		    FM_FAULT_ASRU, &nvl) == 0)
1239 			aa[i] = fmd_asru_hash_lookup_nvl(ahp, nvl, FMD_B_FALSE);
1240 	}
1241 
1242 	(void) pthread_mutex_unlock(&cip->ci_lock);
1243 
1244 	for (i = 0; i < an; i++) {
1245 		if (aa[i] == NULL)
1246 			continue; /* no asru was found */
1247 		(void) fmd_asru_clrflags(aa[i], FMD_ASRU_FAULTY, NULL, NULL);
1248 		fmd_asru_hash_release(ahp, aa[i]);
1249 	}
1250 
1251 	if (cstate == FMD_CASE_CLOSED)
1252 		fmd_case_transition(cp, FMD_CASE_REPAIRED, FMD_CF_REPAIRED);
1253 	else
1254 		fmd_case_transition(cp, FMD_CASE_CLOSE_WAIT, FMD_CF_REPAIRED);
1255 
1256 	return (0);
1257 }
1258 
1259 int
1260 fmd_case_contains(fmd_case_t *cp, fmd_event_t *ep)
1261 {
1262 	fmd_case_impl_t *cip = (fmd_case_impl_t *)cp;
1263 	fmd_case_item_t *cit;
1264 	uint_t state;
1265 	int rv = 0;
1266 
1267 	(void) pthread_mutex_lock(&cip->ci_lock);
1268 
1269 	if (cip->ci_state >= FMD_CASE_SOLVED)
1270 		state = FMD_EVS_DIAGNOSED;
1271 	else
1272 		state = FMD_EVS_ACCEPTED;
1273 
1274 	for (cit = cip->ci_items; cit != NULL; cit = cit->cit_next) {
1275 		if ((rv = fmd_event_equal(ep, cit->cit_event)) != 0)
1276 			break;
1277 	}
1278 
1279 	if (rv == 0 && cip->ci_principal != NULL)
1280 		rv = fmd_event_equal(ep, cip->ci_principal);
1281 
1282 	(void) pthread_mutex_unlock(&cip->ci_lock);
1283 
1284 	if (rv != 0)
1285 		fmd_event_transition(ep, state);
1286 
1287 	return (rv);
1288 }
1289 
1290 int
1291 fmd_case_orphaned(fmd_case_t *cp)
1292 {
1293 	return (((fmd_case_impl_t *)cp)->ci_mod == fmd.d_rmod);
1294 }
1295