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