17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55f25dc2aSgavinm  * Common Development and Distribution License (the "License").
65f25dc2aSgavinm  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
215f25dc2aSgavinm 
227c478bd9Sstevel@tonic-gate /*
23*f5961f52SAdrian Frost  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate  *
257c478bd9Sstevel@tonic-gate  * fme.c -- fault management exercise module
267c478bd9Sstevel@tonic-gate  *
277c478bd9Sstevel@tonic-gate  * this module provides the simulated fault management exercise.
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <stdio.h>
317c478bd9Sstevel@tonic-gate #include <stdlib.h>
327c478bd9Sstevel@tonic-gate #include <string.h>
337c478bd9Sstevel@tonic-gate #include <strings.h>
347c478bd9Sstevel@tonic-gate #include <ctype.h>
357c478bd9Sstevel@tonic-gate #include <alloca.h>
367c478bd9Sstevel@tonic-gate #include <libnvpair.h>
377c478bd9Sstevel@tonic-gate #include <sys/fm/protocol.h>
387c478bd9Sstevel@tonic-gate #include <fm/fmd_api.h>
397c478bd9Sstevel@tonic-gate #include "alloc.h"
407c478bd9Sstevel@tonic-gate #include "out.h"
417c478bd9Sstevel@tonic-gate #include "stats.h"
427c478bd9Sstevel@tonic-gate #include "stable.h"
437c478bd9Sstevel@tonic-gate #include "literals.h"
447c478bd9Sstevel@tonic-gate #include "lut.h"
457c478bd9Sstevel@tonic-gate #include "tree.h"
467c478bd9Sstevel@tonic-gate #include "ptree.h"
477c478bd9Sstevel@tonic-gate #include "itree.h"
487c478bd9Sstevel@tonic-gate #include "ipath.h"
497c478bd9Sstevel@tonic-gate #include "fme.h"
507c478bd9Sstevel@tonic-gate #include "evnv.h"
517c478bd9Sstevel@tonic-gate #include "eval.h"
527c478bd9Sstevel@tonic-gate #include "config.h"
537c478bd9Sstevel@tonic-gate #include "platform.h"
54e5ba14ffSstephh #include "esclex.h"
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate /* imported from eft.c... */
577c478bd9Sstevel@tonic-gate extern hrtime_t Hesitate;
58e5ba14ffSstephh extern char *Serd_Override;
597c478bd9Sstevel@tonic-gate extern nv_alloc_t Eft_nv_hdl;
600cc1f05eSjrutt extern int Max_fme;
617aec1d6eScindi extern fmd_hdl_t *Hdl;
627aec1d6eScindi 
637aec1d6eScindi static int Istat_need_save;
64b5016cbbSstephh static int Serd_need_save;
6508f6c065Sgavinm void istat_save(void);
66b5016cbbSstephh void serd_save(void);
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate /* fme under construction is global so we can free it on module abort */
697c478bd9Sstevel@tonic-gate static struct fme *Nfmep;
707c478bd9Sstevel@tonic-gate 
7132d4e834STarik Soydan static int Undiag_reason = UD_VAL_UNKNOWN;
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate static int Nextid = 0;
747c478bd9Sstevel@tonic-gate 
750cc1f05eSjrutt static int Open_fme_count = 0;	/* Count of open FMEs */
760cc1f05eSjrutt 
777c478bd9Sstevel@tonic-gate /* list of fault management exercises underway */
787c478bd9Sstevel@tonic-gate static struct fme {
797c478bd9Sstevel@tonic-gate 	struct fme *next;		/* next exercise */
807c478bd9Sstevel@tonic-gate 	unsigned long long ull;		/* time when fme was created */
817c478bd9Sstevel@tonic-gate 	int id;				/* FME id */
82e5ba14ffSstephh 	struct config *config;		/* cooked configuration data */
837c478bd9Sstevel@tonic-gate 	struct lut *eventtree;		/* propagation tree for this FME */
847c478bd9Sstevel@tonic-gate 	/*
857c478bd9Sstevel@tonic-gate 	 * The initial error report that created this FME is kept in
867c478bd9Sstevel@tonic-gate 	 * two forms.  e0 points to the instance tree node and is used
877c478bd9Sstevel@tonic-gate 	 * by fme_eval() as the starting point for the inference
887c478bd9Sstevel@tonic-gate 	 * algorithm.  e0r is the event handle FMD passed to us when
897c478bd9Sstevel@tonic-gate 	 * the ereport first arrived and is used when setting timers,
907c478bd9Sstevel@tonic-gate 	 * which are always relative to the time of this initial
917c478bd9Sstevel@tonic-gate 	 * report.
927c478bd9Sstevel@tonic-gate 	 */
937c478bd9Sstevel@tonic-gate 	struct event *e0;
947c478bd9Sstevel@tonic-gate 	fmd_event_t *e0r;
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	id_t    timer;			/* for setting an fmd time-out */
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	struct event *ecurrent;		/* ereport under consideration */
997c478bd9Sstevel@tonic-gate 	struct event *suspects;		/* current suspect list */
1007c478bd9Sstevel@tonic-gate 	struct event *psuspects;	/* previous suspect list */
1017c478bd9Sstevel@tonic-gate 	int nsuspects;			/* count of suspects */
1027c478bd9Sstevel@tonic-gate 	int posted_suspects;		/* true if we've posted a diagnosis */
1037c478bd9Sstevel@tonic-gate 	int uniqobs;			/* number of unique events observed */
1047c478bd9Sstevel@tonic-gate 	int peek;			/* just peeking, don't track suspects */
1050cc1f05eSjrutt 	int overflow;			/* true if overflow FME */
1067c478bd9Sstevel@tonic-gate 	enum fme_state {
1077c478bd9Sstevel@tonic-gate 		FME_NOTHING = 5000,	/* not evaluated yet */
1087c478bd9Sstevel@tonic-gate 		FME_WAIT,		/* need to wait for more info */
1097c478bd9Sstevel@tonic-gate 		FME_CREDIBLE,		/* suspect list is credible */
1107aec1d6eScindi 		FME_DISPROVED,		/* no valid suspects found */
1117aec1d6eScindi 		FME_DEFERRED		/* don't know yet (k-count not met) */
1127c478bd9Sstevel@tonic-gate 	} state;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	unsigned long long pull;	/* time passed since created */
1157c478bd9Sstevel@tonic-gate 	unsigned long long wull;	/* wait until this time for re-eval */
1167c478bd9Sstevel@tonic-gate 	struct event *observations;	/* observation list */
1177c478bd9Sstevel@tonic-gate 	struct lut *globals;		/* values of global variables */
1187c478bd9Sstevel@tonic-gate 	/* fmd interfacing */
1197c478bd9Sstevel@tonic-gate 	fmd_hdl_t *hdl;			/* handle for talking with fmd */
1207c478bd9Sstevel@tonic-gate 	fmd_case_t *fmcase;		/* what fmd 'case' we associate with */
1217c478bd9Sstevel@tonic-gate 	/* stats */
1227c478bd9Sstevel@tonic-gate 	struct stats *Rcount;
1237c478bd9Sstevel@tonic-gate 	struct stats *Hcallcount;
1247c478bd9Sstevel@tonic-gate 	struct stats *Rcallcount;
1257c478bd9Sstevel@tonic-gate 	struct stats *Ccallcount;
1267c478bd9Sstevel@tonic-gate 	struct stats *Ecallcount;
1277c478bd9Sstevel@tonic-gate 	struct stats *Tcallcount;
1287c478bd9Sstevel@tonic-gate 	struct stats *Marrowcount;
1297c478bd9Sstevel@tonic-gate 	struct stats *diags;
1307c478bd9Sstevel@tonic-gate } *FMElist, *EFMElist, *ClosedFMEs;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate static struct case_list {
1337c478bd9Sstevel@tonic-gate 	fmd_case_t *fmcase;
1347c478bd9Sstevel@tonic-gate 	struct case_list *next;
1357c478bd9Sstevel@tonic-gate } *Undiagablecaselist;
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate static void fme_eval(struct fme *fmep, fmd_event_t *ffep);
1387c478bd9Sstevel@tonic-gate static enum fme_state hypothesise(struct fme *fmep, struct event *ep,
1397aec1d6eScindi 	unsigned long long at_latest_by, unsigned long long *pdelay);
1407c478bd9Sstevel@tonic-gate static struct node *eventprop_lookup(struct event *ep, const char *propname);
1417c478bd9Sstevel@tonic-gate static struct node *pathstring2epnamenp(char *path);
142b5016cbbSstephh static void publish_undiagnosable(fmd_hdl_t *hdl, fmd_event_t *ffep,
143705e9f42SStephen Hanson 	fmd_case_t *fmcase, nvlist_t *detector, char *arg);
144705e9f42SStephen Hanson static char *undiag_2reason_str(int ud, char *arg);
14532d4e834STarik Soydan static const char *undiag_2defect_str(int ud);
1467c478bd9Sstevel@tonic-gate static void restore_suspects(struct fme *fmep);
1477c478bd9Sstevel@tonic-gate static void save_suspects(struct fme *fmep);
1487c478bd9Sstevel@tonic-gate static void destroy_fme(struct fme *f);
1497c478bd9Sstevel@tonic-gate static void fme_receive_report(fmd_hdl_t *hdl, fmd_event_t *ffep,
1507c478bd9Sstevel@tonic-gate     const char *eventstring, const struct ipath *ipp, nvlist_t *nvl);
15108f6c065Sgavinm static void istat_counter_reset_cb(struct istat_entry *entp,
15208f6c065Sgavinm     struct stats *statp, const struct ipath *ipp);
153e5ba14ffSstephh static void istat_counter_topo_chg_cb(struct istat_entry *entp,
154e5ba14ffSstephh     struct stats *statp, void *unused);
155b5016cbbSstephh static void serd_reset_cb(struct serd_entry *entp, void *unused,
156b5016cbbSstephh     const struct ipath *ipp);
157e5ba14ffSstephh static void serd_topo_chg_cb(struct serd_entry *entp, void *unused,
158e5ba14ffSstephh     void *unused2);
159b5016cbbSstephh static void destroy_fme_bufs(struct fme *fp);
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate static struct fme *
1627c478bd9Sstevel@tonic-gate alloc_fme(void)
1637c478bd9Sstevel@tonic-gate {
1647c478bd9Sstevel@tonic-gate 	struct fme *fmep;
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	fmep = MALLOC(sizeof (*fmep));
1677c478bd9Sstevel@tonic-gate 	bzero(fmep, sizeof (*fmep));
1687c478bd9Sstevel@tonic-gate 	return (fmep);
1697c478bd9Sstevel@tonic-gate }
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate /*
1727c478bd9Sstevel@tonic-gate  * fme_ready -- called when all initialization of the FME (except for
1737c478bd9Sstevel@tonic-gate  *	stats) has completed successfully.  Adds the fme to global lists
1747c478bd9Sstevel@tonic-gate  *	and establishes its stats.
1757c478bd9Sstevel@tonic-gate  */
1767c478bd9Sstevel@tonic-gate static struct fme *
1777c478bd9Sstevel@tonic-gate fme_ready(struct fme *fmep)
1787c478bd9Sstevel@tonic-gate {
1797c478bd9Sstevel@tonic-gate 	char nbuf[100];
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	Nfmep = NULL;	/* don't need to free this on module abort now */
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	if (EFMElist) {
1847c478bd9Sstevel@tonic-gate 		EFMElist->next = fmep;
1857c478bd9Sstevel@tonic-gate 		EFMElist = fmep;
1867c478bd9Sstevel@tonic-gate 	} else
1877c478bd9Sstevel@tonic-gate 		FMElist = EFMElist = fmep;
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	(void) sprintf(nbuf, "fme%d.Rcount", fmep->id);
1907c478bd9Sstevel@tonic-gate 	fmep->Rcount = stats_new_counter(nbuf, "ereports received", 0);
1917c478bd9Sstevel@tonic-gate 	(void) sprintf(nbuf, "fme%d.Hcall", fmep->id);
1927c478bd9Sstevel@tonic-gate 	fmep->Hcallcount = stats_new_counter(nbuf, "calls to hypothesise()", 1);
1937c478bd9Sstevel@tonic-gate 	(void) sprintf(nbuf, "fme%d.Rcall", fmep->id);
1947c478bd9Sstevel@tonic-gate 	fmep->Rcallcount = stats_new_counter(nbuf,
1957c478bd9Sstevel@tonic-gate 	    "calls to requirements_test()", 1);
1967c478bd9Sstevel@tonic-gate 	(void) sprintf(nbuf, "fme%d.Ccall", fmep->id);
1977c478bd9Sstevel@tonic-gate 	fmep->Ccallcount = stats_new_counter(nbuf, "calls to causes_test()", 1);
1987c478bd9Sstevel@tonic-gate 	(void) sprintf(nbuf, "fme%d.Ecall", fmep->id);
1997c478bd9Sstevel@tonic-gate 	fmep->Ecallcount =
2007c478bd9Sstevel@tonic-gate 	    stats_new_counter(nbuf, "calls to effects_test()", 1);
2017c478bd9Sstevel@tonic-gate 	(void) sprintf(nbuf, "fme%d.Tcall", fmep->id);
2027c478bd9Sstevel@tonic-gate 	fmep->Tcallcount = stats_new_counter(nbuf, "calls to triggered()", 1);
2037c478bd9Sstevel@tonic-gate 	(void) sprintf(nbuf, "fme%d.Marrow", fmep->id);
2047c478bd9Sstevel@tonic-gate 	fmep->Marrowcount = stats_new_counter(nbuf,
2057c478bd9Sstevel@tonic-gate 	    "arrows marked by mark_arrows()", 1);
2067c478bd9Sstevel@tonic-gate 	(void) sprintf(nbuf, "fme%d.diags", fmep->id);
2077c478bd9Sstevel@tonic-gate 	fmep->diags = stats_new_counter(nbuf, "suspect lists diagnosed", 0);
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB2, "newfme: config snapshot contains...");
210e5ba14ffSstephh 	config_print(O_ALTFP|O_VERB2, fmep->config);
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	return (fmep);
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate 
215b5016cbbSstephh extern void ipath_dummy_lut(struct arrow *);
216b5016cbbSstephh extern struct lut *itree_create_dummy(const char *, const struct ipath *);
217b5016cbbSstephh 
218b5016cbbSstephh /* ARGSUSED */
219b5016cbbSstephh static void
220b5016cbbSstephh set_needed_arrows(struct event *ep, struct event *ep2, struct fme *fmep)
221b5016cbbSstephh {
222b5016cbbSstephh 	struct bubble *bp;
223b5016cbbSstephh 	struct arrowlist *ap;
224b5016cbbSstephh 
225b5016cbbSstephh 	for (bp = itree_next_bubble(ep, NULL); bp;
226b5016cbbSstephh 	    bp = itree_next_bubble(ep, bp)) {
227b5016cbbSstephh 		if (bp->t != B_FROM)
228b5016cbbSstephh 			continue;
229b5016cbbSstephh 		for (ap = itree_next_arrow(bp, NULL); ap;
230b5016cbbSstephh 		    ap = itree_next_arrow(bp, ap)) {
231b5016cbbSstephh 			ap->arrowp->pnode->u.arrow.needed = 1;
232b5016cbbSstephh 			ipath_dummy_lut(ap->arrowp);
233b5016cbbSstephh 		}
234b5016cbbSstephh 	}
235b5016cbbSstephh }
236b5016cbbSstephh 
237b5016cbbSstephh /* ARGSUSED */
238b5016cbbSstephh static void
239b5016cbbSstephh unset_needed_arrows(struct event *ep, struct event *ep2, struct fme *fmep)
240b5016cbbSstephh {
241b5016cbbSstephh 	struct bubble *bp;
242b5016cbbSstephh 	struct arrowlist *ap;
243b5016cbbSstephh 
244b5016cbbSstephh 	for (bp = itree_next_bubble(ep, NULL); bp;
245b5016cbbSstephh 	    bp = itree_next_bubble(ep, bp)) {
246b5016cbbSstephh 		if (bp->t != B_FROM)
247b5016cbbSstephh 			continue;
248b5016cbbSstephh 		for (ap = itree_next_arrow(bp, NULL); ap;
249b5016cbbSstephh 		    ap = itree_next_arrow(bp, ap))
250b5016cbbSstephh 			ap->arrowp->pnode->u.arrow.needed = 0;
251b5016cbbSstephh 	}
252b5016cbbSstephh }
253b5016cbbSstephh 
254b5016cbbSstephh static void globals_destructor(void *left, void *right, void *arg);
255b5016cbbSstephh static void clear_arrows(struct event *ep, struct event *ep2, struct fme *fmep);
256b5016cbbSstephh 
257705e9f42SStephen Hanson static boolean_t
258b5016cbbSstephh prune_propagations(const char *e0class, const struct ipath *e0ipp)
259b5016cbbSstephh {
260b5016cbbSstephh 	char nbuf[100];
261b5016cbbSstephh 	unsigned long long my_delay = TIMEVAL_EVENTUALLY;
262b5016cbbSstephh 	extern struct lut *Usednames;
263b5016cbbSstephh 
264b5016cbbSstephh 	Nfmep = alloc_fme();
265b5016cbbSstephh 	Nfmep->id = Nextid;
266b5016cbbSstephh 	Nfmep->state = FME_NOTHING;
267b5016cbbSstephh 	Nfmep->eventtree = itree_create_dummy(e0class, e0ipp);
268b5016cbbSstephh 	if ((Nfmep->e0 =
269b5016cbbSstephh 	    itree_lookup(Nfmep->eventtree, e0class, e0ipp)) == NULL) {
270b5016cbbSstephh 		itree_free(Nfmep->eventtree);
271b5016cbbSstephh 		FREE(Nfmep);
272b5016cbbSstephh 		Nfmep = NULL;
273705e9f42SStephen Hanson 		return (B_FALSE);
274b5016cbbSstephh 	}
275b5016cbbSstephh 	Nfmep->ecurrent = Nfmep->observations = Nfmep->e0;
276b5016cbbSstephh 	Nfmep->e0->count++;
277b5016cbbSstephh 
278b5016cbbSstephh 	(void) sprintf(nbuf, "fme%d.Rcount", Nfmep->id);
279b5016cbbSstephh 	Nfmep->Rcount = stats_new_counter(nbuf, "ereports received", 0);
280b5016cbbSstephh 	(void) sprintf(nbuf, "fme%d.Hcall", Nfmep->id);
281b5016cbbSstephh 	Nfmep->Hcallcount =
282b5016cbbSstephh 	    stats_new_counter(nbuf, "calls to hypothesise()", 1);
283b5016cbbSstephh 	(void) sprintf(nbuf, "fme%d.Rcall", Nfmep->id);
284b5016cbbSstephh 	Nfmep->Rcallcount = stats_new_counter(nbuf,
285b5016cbbSstephh 	    "calls to requirements_test()", 1);
286b5016cbbSstephh 	(void) sprintf(nbuf, "fme%d.Ccall", Nfmep->id);
287b5016cbbSstephh 	Nfmep->Ccallcount =
288b5016cbbSstephh 	    stats_new_counter(nbuf, "calls to causes_test()", 1);
289b5016cbbSstephh 	(void) sprintf(nbuf, "fme%d.Ecall", Nfmep->id);
290b5016cbbSstephh 	Nfmep->Ecallcount =
291b5016cbbSstephh 	    stats_new_counter(nbuf, "calls to effects_test()", 1);
292b5016cbbSstephh 	(void) sprintf(nbuf, "fme%d.Tcall", Nfmep->id);
293b5016cbbSstephh 	Nfmep->Tcallcount = stats_new_counter(nbuf, "calls to triggered()", 1);
294b5016cbbSstephh 	(void) sprintf(nbuf, "fme%d.Marrow", Nfmep->id);
295b5016cbbSstephh 	Nfmep->Marrowcount = stats_new_counter(nbuf,
296b5016cbbSstephh 	    "arrows marked by mark_arrows()", 1);
297b5016cbbSstephh 	(void) sprintf(nbuf, "fme%d.diags", Nfmep->id);
298b5016cbbSstephh 	Nfmep->diags = stats_new_counter(nbuf, "suspect lists diagnosed", 0);
299b5016cbbSstephh 
300b5016cbbSstephh 	Nfmep->peek = 1;
301b5016cbbSstephh 	lut_walk(Nfmep->eventtree, (lut_cb)unset_needed_arrows, (void *)Nfmep);
302b5016cbbSstephh 	lut_free(Usednames, NULL, NULL);
303b5016cbbSstephh 	Usednames = NULL;
304b5016cbbSstephh 	lut_walk(Nfmep->eventtree, (lut_cb)clear_arrows, (void *)Nfmep);
305b5016cbbSstephh 	(void) hypothesise(Nfmep, Nfmep->e0, Nfmep->ull, &my_delay);
306b5016cbbSstephh 	itree_prune(Nfmep->eventtree);
307b5016cbbSstephh 	lut_walk(Nfmep->eventtree, (lut_cb)set_needed_arrows, (void *)Nfmep);
308b5016cbbSstephh 
309b5016cbbSstephh 	stats_delete(Nfmep->Rcount);
310b5016cbbSstephh 	stats_delete(Nfmep->Hcallcount);
311b5016cbbSstephh 	stats_delete(Nfmep->Rcallcount);
312b5016cbbSstephh 	stats_delete(Nfmep->Ccallcount);
313b5016cbbSstephh 	stats_delete(Nfmep->Ecallcount);
314b5016cbbSstephh 	stats_delete(Nfmep->Tcallcount);
315b5016cbbSstephh 	stats_delete(Nfmep->Marrowcount);
316b5016cbbSstephh 	stats_delete(Nfmep->diags);
317b5016cbbSstephh 	itree_free(Nfmep->eventtree);
318b5016cbbSstephh 	lut_free(Nfmep->globals, globals_destructor, NULL);
319b5016cbbSstephh 	FREE(Nfmep);
320705e9f42SStephen Hanson 	return (B_TRUE);
321b5016cbbSstephh }
322b5016cbbSstephh 
3237c478bd9Sstevel@tonic-gate static struct fme *
324b5016cbbSstephh newfme(const char *e0class, const struct ipath *e0ipp, fmd_hdl_t *hdl,
325705e9f42SStephen Hanson 	fmd_case_t *fmcase, fmd_event_t *ffep, nvlist_t *nvl)
3267c478bd9Sstevel@tonic-gate {
3277c478bd9Sstevel@tonic-gate 	struct cfgdata *cfgdata;
328b5016cbbSstephh 	int init_size;
329b5016cbbSstephh 	extern int alloc_total();
330705e9f42SStephen Hanson 	nvlist_t *detector = NULL;
331705e9f42SStephen Hanson 	char *pathstr;
332705e9f42SStephen Hanson 	char *arg;
333705e9f42SStephen Hanson 
334705e9f42SStephen Hanson 	/*
335705e9f42SStephen Hanson 	 * First check if e0ipp is actually in the topology so we can give a
336705e9f42SStephen Hanson 	 * more useful error message.
337705e9f42SStephen Hanson 	 */
338705e9f42SStephen Hanson 	ipathlastcomp(e0ipp);
339705e9f42SStephen Hanson 	pathstr = ipath2str(NULL, e0ipp);
340705e9f42SStephen Hanson 	cfgdata = config_snapshot();
341705e9f42SStephen Hanson 	platform_units_translate(0, cfgdata->cooked, NULL, NULL,
342705e9f42SStephen Hanson 	    &detector, pathstr);
343705e9f42SStephen Hanson 	FREE(pathstr);
344705e9f42SStephen Hanson 	structconfig_free(cfgdata->cooked);
345705e9f42SStephen Hanson 	config_free(cfgdata);
346705e9f42SStephen Hanson 	if (detector == NULL) {
347*f5961f52SAdrian Frost 		/* See if class permits silent discard on unknown component. */
348*f5961f52SAdrian Frost 		if (lut_lookup(Ereportenames_discard, (void *)e0class, NULL)) {
349*f5961f52SAdrian Frost 			out(O_ALTFP|O_VERB2, "Unable to map \"%s\" ereport "
350*f5961f52SAdrian Frost 			    "to component path, but silent discard allowed.",
351*f5961f52SAdrian Frost 			    e0class);
352*f5961f52SAdrian Frost 		} else {
353*f5961f52SAdrian Frost 			Undiag_reason = UD_VAL_BADEVENTPATH;
354*f5961f52SAdrian Frost 			(void) nvlist_lookup_nvlist(nvl, FM_EREPORT_DETECTOR,
355*f5961f52SAdrian Frost 			    &detector);
356*f5961f52SAdrian Frost 			arg = ipath2str(e0class, e0ipp);
357*f5961f52SAdrian Frost 			publish_undiagnosable(hdl, ffep, fmcase, detector, arg);
358*f5961f52SAdrian Frost 			FREE(arg);
359*f5961f52SAdrian Frost 		}
360705e9f42SStephen Hanson 		return (NULL);
361705e9f42SStephen Hanson 	}
362705e9f42SStephen Hanson 
363705e9f42SStephen Hanson 	/*
364705e9f42SStephen Hanson 	 * Next run a quick first pass of the rules with a dummy config. This
365705e9f42SStephen Hanson 	 * allows us to prune those rules which can't possibly cause this
366705e9f42SStephen Hanson 	 * ereport.
367705e9f42SStephen Hanson 	 */
368705e9f42SStephen Hanson 	if (!prune_propagations(e0class, e0ipp)) {
369705e9f42SStephen Hanson 		/*
370705e9f42SStephen Hanson 		 * The fault class must have been in the rules or we would
371705e9f42SStephen Hanson 		 * not have registered for it (and got a "nosub"), and the
372705e9f42SStephen Hanson 		 * pathname must be in the topology or we would have failed the
373705e9f42SStephen Hanson 		 * previous test. So to get here means the combination of
374705e9f42SStephen Hanson 		 * class and pathname in the ereport must be invalid.
375705e9f42SStephen Hanson 		 */
376705e9f42SStephen Hanson 		Undiag_reason = UD_VAL_BADEVENTCLASS;
377705e9f42SStephen Hanson 		arg = ipath2str(e0class, e0ipp);
378705e9f42SStephen Hanson 		publish_undiagnosable(hdl, ffep, fmcase, detector, arg);
379705e9f42SStephen Hanson 		nvlist_free(detector);
380705e9f42SStephen Hanson 		FREE(arg);
381705e9f42SStephen Hanson 		return (NULL);
382705e9f42SStephen Hanson 	}
3837c478bd9Sstevel@tonic-gate 
384705e9f42SStephen Hanson 	/*
385705e9f42SStephen Hanson 	 * Now go ahead and create the real fme using the pruned rules.
386705e9f42SStephen Hanson 	 */
387b5016cbbSstephh 	init_size = alloc_total();
388b5016cbbSstephh 	out(O_ALTFP|O_STAMP, "start config_snapshot using %d bytes", init_size);
38932d4e834STarik Soydan 	cfgdata = config_snapshot();
390b5016cbbSstephh 	platform_save_config(hdl, fmcase);
391b5016cbbSstephh 	out(O_ALTFP|O_STAMP, "config_snapshot added %d bytes",
392b5016cbbSstephh 	    alloc_total() - init_size);
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	Nfmep = alloc_fme();
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	Nfmep->id = Nextid++;
397e5ba14ffSstephh 	Nfmep->config = cfgdata->cooked;
398e5ba14ffSstephh 	config_free(cfgdata);
3997c478bd9Sstevel@tonic-gate 	Nfmep->posted_suspects = 0;
4007c478bd9Sstevel@tonic-gate 	Nfmep->uniqobs = 0;
4017c478bd9Sstevel@tonic-gate 	Nfmep->state = FME_NOTHING;
4027c478bd9Sstevel@tonic-gate 	Nfmep->pull = 0ULL;
4030cc1f05eSjrutt 	Nfmep->overflow = 0;
4047c478bd9Sstevel@tonic-gate 
405b5016cbbSstephh 	Nfmep->fmcase = fmcase;
406b5016cbbSstephh 	Nfmep->hdl = hdl;
4077c478bd9Sstevel@tonic-gate 
408e5ba14ffSstephh 	if ((Nfmep->eventtree = itree_create(Nfmep->config)) == NULL) {
40932d4e834STarik Soydan 		Undiag_reason = UD_VAL_INSTFAIL;
410705e9f42SStephen Hanson 		arg = ipath2str(e0class, e0ipp);
411705e9f42SStephen Hanson 		publish_undiagnosable(hdl, ffep, fmcase, detector, arg);
412705e9f42SStephen Hanson 		nvlist_free(detector);
413705e9f42SStephen Hanson 		FREE(arg);
414e5ba14ffSstephh 		structconfig_free(Nfmep->config);
415b5016cbbSstephh 		destroy_fme_bufs(Nfmep);
4167c478bd9Sstevel@tonic-gate 		FREE(Nfmep);
4177c478bd9Sstevel@tonic-gate 		Nfmep = NULL;
4187c478bd9Sstevel@tonic-gate 		return (NULL);
4197c478bd9Sstevel@tonic-gate 	}
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 	itree_ptree(O_ALTFP|O_VERB2, Nfmep->eventtree);
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	if ((Nfmep->e0 =
4247c478bd9Sstevel@tonic-gate 	    itree_lookup(Nfmep->eventtree, e0class, e0ipp)) == NULL) {
42532d4e834STarik Soydan 		Undiag_reason = UD_VAL_BADEVENTI;
426705e9f42SStephen Hanson 		arg = ipath2str(e0class, e0ipp);
427705e9f42SStephen Hanson 		publish_undiagnosable(hdl, ffep, fmcase, detector, arg);
428705e9f42SStephen Hanson 		nvlist_free(detector);
429705e9f42SStephen Hanson 		FREE(arg);
4307c478bd9Sstevel@tonic-gate 		itree_free(Nfmep->eventtree);
431e5ba14ffSstephh 		structconfig_free(Nfmep->config);
432b5016cbbSstephh 		destroy_fme_bufs(Nfmep);
4337c478bd9Sstevel@tonic-gate 		FREE(Nfmep);
4347c478bd9Sstevel@tonic-gate 		Nfmep = NULL;
4357c478bd9Sstevel@tonic-gate 		return (NULL);
4367c478bd9Sstevel@tonic-gate 	}
4377c478bd9Sstevel@tonic-gate 
438705e9f42SStephen Hanson 	nvlist_free(detector);
4397c478bd9Sstevel@tonic-gate 	return (fme_ready(Nfmep));
4407c478bd9Sstevel@tonic-gate }
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate void
4437c478bd9Sstevel@tonic-gate fme_fini(void)
4447c478bd9Sstevel@tonic-gate {
4457c478bd9Sstevel@tonic-gate 	struct fme *sfp, *fp;
4467c478bd9Sstevel@tonic-gate 	struct case_list *ucasep, *nextcasep;
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	ucasep = Undiagablecaselist;
4497c478bd9Sstevel@tonic-gate 	while (ucasep != NULL) {
4507c478bd9Sstevel@tonic-gate 		nextcasep = ucasep->next;
4517c478bd9Sstevel@tonic-gate 		FREE(ucasep);
4527c478bd9Sstevel@tonic-gate 		ucasep = nextcasep;
4537c478bd9Sstevel@tonic-gate 	}
4547c478bd9Sstevel@tonic-gate 	Undiagablecaselist = NULL;
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	/* clean up closed fmes */
4577c478bd9Sstevel@tonic-gate 	fp = ClosedFMEs;
4587c478bd9Sstevel@tonic-gate 	while (fp != NULL) {
4597c478bd9Sstevel@tonic-gate 		sfp = fp->next;
4607c478bd9Sstevel@tonic-gate 		destroy_fme(fp);
4617c478bd9Sstevel@tonic-gate 		fp = sfp;
4627c478bd9Sstevel@tonic-gate 	}
4637c478bd9Sstevel@tonic-gate 	ClosedFMEs = NULL;
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	fp = FMElist;
4667c478bd9Sstevel@tonic-gate 	while (fp != NULL) {
4677c478bd9Sstevel@tonic-gate 		sfp = fp->next;
4687c478bd9Sstevel@tonic-gate 		destroy_fme(fp);
4697c478bd9Sstevel@tonic-gate 		fp = sfp;
4707c478bd9Sstevel@tonic-gate 	}
4717c478bd9Sstevel@tonic-gate 	FMElist = EFMElist = NULL;
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 	/* if we were in the middle of creating an fme, free it now */
4747c478bd9Sstevel@tonic-gate 	if (Nfmep) {
4757c478bd9Sstevel@tonic-gate 		destroy_fme(Nfmep);
4767c478bd9Sstevel@tonic-gate 		Nfmep = NULL;
4777c478bd9Sstevel@tonic-gate 	}
4787c478bd9Sstevel@tonic-gate }
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate /*
4817c478bd9Sstevel@tonic-gate  * Allocated space for a buffer name.  20 bytes allows for
4827c478bd9Sstevel@tonic-gate  * a ridiculous 9,999,999 unique observations.
4837c478bd9Sstevel@tonic-gate  */
4847c478bd9Sstevel@tonic-gate #define	OBBUFNMSZ 20
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate /*
4877c478bd9Sstevel@tonic-gate  *  serialize_observation
4887c478bd9Sstevel@tonic-gate  *
4897c478bd9Sstevel@tonic-gate  *  Create a recoverable version of the current observation
4907c478bd9Sstevel@tonic-gate  *  (f->ecurrent).  We keep a serialized version of each unique
4917c478bd9Sstevel@tonic-gate  *  observation in order that we may resume correctly the fme in the
4927c478bd9Sstevel@tonic-gate  *  correct state if eft or fmd crashes and we're restarted.
4937c478bd9Sstevel@tonic-gate  */
4947c478bd9Sstevel@tonic-gate static void
4957c478bd9Sstevel@tonic-gate serialize_observation(struct fme *fp, const char *cls, const struct ipath *ipp)
4967c478bd9Sstevel@tonic-gate {
4977c478bd9Sstevel@tonic-gate 	size_t pkdlen;
4987c478bd9Sstevel@tonic-gate 	char tmpbuf[OBBUFNMSZ];
4997c478bd9Sstevel@tonic-gate 	char *pkd = NULL;
5007c478bd9Sstevel@tonic-gate 	char *estr;
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 	(void) snprintf(tmpbuf, OBBUFNMSZ, "observed%d", fp->uniqobs);
5037c478bd9Sstevel@tonic-gate 	estr = ipath2str(cls, ipp);
5047c478bd9Sstevel@tonic-gate 	fmd_buf_create(fp->hdl, fp->fmcase, tmpbuf, strlen(estr) + 1);
5057c478bd9Sstevel@tonic-gate 	fmd_buf_write(fp->hdl, fp->fmcase, tmpbuf, (void *)estr,
5067c478bd9Sstevel@tonic-gate 	    strlen(estr) + 1);
5077c478bd9Sstevel@tonic-gate 	FREE(estr);
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 	if (fp->ecurrent != NULL && fp->ecurrent->nvp != NULL) {
5107c478bd9Sstevel@tonic-gate 		(void) snprintf(tmpbuf,
5117c478bd9Sstevel@tonic-gate 		    OBBUFNMSZ, "observed%d.nvp", fp->uniqobs);
5127c478bd9Sstevel@tonic-gate 		if (nvlist_xpack(fp->ecurrent->nvp,
5137c478bd9Sstevel@tonic-gate 		    &pkd, &pkdlen, NV_ENCODE_XDR, &Eft_nv_hdl) != 0)
5147c478bd9Sstevel@tonic-gate 			out(O_DIE|O_SYS, "pack of observed nvl failed");
5157c478bd9Sstevel@tonic-gate 		fmd_buf_create(fp->hdl, fp->fmcase, tmpbuf, pkdlen);
5167c478bd9Sstevel@tonic-gate 		fmd_buf_write(fp->hdl, fp->fmcase, tmpbuf, (void *)pkd, pkdlen);
5177c478bd9Sstevel@tonic-gate 		FREE(pkd);
5187c478bd9Sstevel@tonic-gate 	}
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 	fp->uniqobs++;
5217c478bd9Sstevel@tonic-gate 	fmd_buf_write(fp->hdl, fp->fmcase, WOBUF_NOBS, (void *)&fp->uniqobs,
5227c478bd9Sstevel@tonic-gate 	    sizeof (fp->uniqobs));
5237c478bd9Sstevel@tonic-gate }
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate /*
5267c478bd9Sstevel@tonic-gate  *  init_fme_bufs -- We keep several bits of state about an fme for
5277c478bd9Sstevel@tonic-gate  *	use if eft or fmd crashes and we're restarted.
5287c478bd9Sstevel@tonic-gate  */
5297c478bd9Sstevel@tonic-gate static void
5307c478bd9Sstevel@tonic-gate init_fme_bufs(struct fme *fp)
5317c478bd9Sstevel@tonic-gate {
5327c478bd9Sstevel@tonic-gate 	fmd_buf_create(fp->hdl, fp->fmcase, WOBUF_PULL, sizeof (fp->pull));
5337c478bd9Sstevel@tonic-gate 	fmd_buf_write(fp->hdl, fp->fmcase, WOBUF_PULL, (void *)&fp->pull,
5347c478bd9Sstevel@tonic-gate 	    sizeof (fp->pull));
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	fmd_buf_create(fp->hdl, fp->fmcase, WOBUF_ID, sizeof (fp->id));
5377c478bd9Sstevel@tonic-gate 	fmd_buf_write(fp->hdl, fp->fmcase, WOBUF_ID, (void *)&fp->id,
5387c478bd9Sstevel@tonic-gate 	    sizeof (fp->id));
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 	fmd_buf_create(fp->hdl, fp->fmcase, WOBUF_NOBS, sizeof (fp->uniqobs));
5417c478bd9Sstevel@tonic-gate 	fmd_buf_write(fp->hdl, fp->fmcase, WOBUF_NOBS, (void *)&fp->uniqobs,
5427c478bd9Sstevel@tonic-gate 	    sizeof (fp->uniqobs));
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	fmd_buf_create(fp->hdl, fp->fmcase, WOBUF_POSTD,
5457c478bd9Sstevel@tonic-gate 	    sizeof (fp->posted_suspects));
5467c478bd9Sstevel@tonic-gate 	fmd_buf_write(fp->hdl, fp->fmcase, WOBUF_POSTD,
5477c478bd9Sstevel@tonic-gate 	    (void *)&fp->posted_suspects, sizeof (fp->posted_suspects));
5487c478bd9Sstevel@tonic-gate }
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate static void
5517c478bd9Sstevel@tonic-gate destroy_fme_bufs(struct fme *fp)
5527c478bd9Sstevel@tonic-gate {
5537c478bd9Sstevel@tonic-gate 	char tmpbuf[OBBUFNMSZ];
5547c478bd9Sstevel@tonic-gate 	int o;
5557c478bd9Sstevel@tonic-gate 
556b5016cbbSstephh 	platform_restore_config(fp->hdl, fp->fmcase);
5577c478bd9Sstevel@tonic-gate 	fmd_buf_destroy(fp->hdl, fp->fmcase, WOBUF_CFGLEN);
5587c478bd9Sstevel@tonic-gate 	fmd_buf_destroy(fp->hdl, fp->fmcase, WOBUF_CFG);
5597c478bd9Sstevel@tonic-gate 	fmd_buf_destroy(fp->hdl, fp->fmcase, WOBUF_PULL);
5607c478bd9Sstevel@tonic-gate 	fmd_buf_destroy(fp->hdl, fp->fmcase, WOBUF_ID);
5617c478bd9Sstevel@tonic-gate 	fmd_buf_destroy(fp->hdl, fp->fmcase, WOBUF_POSTD);
5627c478bd9Sstevel@tonic-gate 	fmd_buf_destroy(fp->hdl, fp->fmcase, WOBUF_NOBS);
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 	for (o = 0; o < fp->uniqobs; o++) {
5657c478bd9Sstevel@tonic-gate 		(void) snprintf(tmpbuf, OBBUFNMSZ, "observed%d", o);
5667c478bd9Sstevel@tonic-gate 		fmd_buf_destroy(fp->hdl, fp->fmcase, tmpbuf);
5677c478bd9Sstevel@tonic-gate 		(void) snprintf(tmpbuf, OBBUFNMSZ, "observed%d.nvp", o);
5687c478bd9Sstevel@tonic-gate 		fmd_buf_destroy(fp->hdl, fp->fmcase, tmpbuf);
5697c478bd9Sstevel@tonic-gate 	}
5707c478bd9Sstevel@tonic-gate }
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate /*
5737c478bd9Sstevel@tonic-gate  * reconstitute_observations -- convert a case's serialized observations
5747c478bd9Sstevel@tonic-gate  *	back into struct events.  Returns zero if all observations are
5757c478bd9Sstevel@tonic-gate  *	successfully reconstituted.
5767c478bd9Sstevel@tonic-gate  */
5777c478bd9Sstevel@tonic-gate static int
5787c478bd9Sstevel@tonic-gate reconstitute_observations(struct fme *fmep)
5797c478bd9Sstevel@tonic-gate {
5807c478bd9Sstevel@tonic-gate 	struct event *ep;
5817c478bd9Sstevel@tonic-gate 	struct node *epnamenp = NULL;
5827c478bd9Sstevel@tonic-gate 	size_t pkdlen;
5837c478bd9Sstevel@tonic-gate 	char *pkd = NULL;
5847c478bd9Sstevel@tonic-gate 	char *tmpbuf = alloca(OBBUFNMSZ);
5857c478bd9Sstevel@tonic-gate 	char *sepptr;
5867c478bd9Sstevel@tonic-gate 	char *estr;
5877c478bd9Sstevel@tonic-gate 	int ocnt;
5887c478bd9Sstevel@tonic-gate 	int elen;
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 	for (ocnt = 0; ocnt < fmep->uniqobs; ocnt++) {
5917c478bd9Sstevel@tonic-gate 		(void) snprintf(tmpbuf, OBBUFNMSZ, "observed%d", ocnt);
5927c478bd9Sstevel@tonic-gate 		elen = fmd_buf_size(fmep->hdl, fmep->fmcase, tmpbuf);
5937c478bd9Sstevel@tonic-gate 		if (elen == 0) {
5947c478bd9Sstevel@tonic-gate 			out(O_ALTFP,
5957c478bd9Sstevel@tonic-gate 			    "reconstitute_observation: no %s buffer found.",
5967c478bd9Sstevel@tonic-gate 			    tmpbuf);
59732d4e834STarik Soydan 			Undiag_reason = UD_VAL_MISSINGOBS;
5987c478bd9Sstevel@tonic-gate 			break;
5997c478bd9Sstevel@tonic-gate 		}
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 		estr = MALLOC(elen);
6027c478bd9Sstevel@tonic-gate 		fmd_buf_read(fmep->hdl, fmep->fmcase, tmpbuf, estr, elen);
6037c478bd9Sstevel@tonic-gate 		sepptr = strchr(estr, '@');
6047c478bd9Sstevel@tonic-gate 		if (sepptr == NULL) {
6057c478bd9Sstevel@tonic-gate 			out(O_ALTFP,
6067c478bd9Sstevel@tonic-gate 			    "reconstitute_observation: %s: "
6077c478bd9Sstevel@tonic-gate 			    "missing @ separator in %s.",
6087c478bd9Sstevel@tonic-gate 			    tmpbuf, estr);
60932d4e834STarik Soydan 			Undiag_reason = UD_VAL_MISSINGPATH;
6107c478bd9Sstevel@tonic-gate 			FREE(estr);
6117c478bd9Sstevel@tonic-gate 			break;
6127c478bd9Sstevel@tonic-gate 		}
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 		*sepptr = '\0';
6157c478bd9Sstevel@tonic-gate 		if ((epnamenp = pathstring2epnamenp(sepptr + 1)) == NULL) {
6167c478bd9Sstevel@tonic-gate 			out(O_ALTFP,
6177c478bd9Sstevel@tonic-gate 			    "reconstitute_observation: %s: "
6187c478bd9Sstevel@tonic-gate 			    "trouble converting path string \"%s\" "
6197c478bd9Sstevel@tonic-gate 			    "to internal representation.",
6207c478bd9Sstevel@tonic-gate 			    tmpbuf, sepptr + 1);
62132d4e834STarik Soydan 			Undiag_reason = UD_VAL_MISSINGPATH;
6227c478bd9Sstevel@tonic-gate 			FREE(estr);
6237c478bd9Sstevel@tonic-gate 			break;
6247c478bd9Sstevel@tonic-gate 		}
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 		/* construct the event */
6277c478bd9Sstevel@tonic-gate 		ep = itree_lookup(fmep->eventtree,
6287c478bd9Sstevel@tonic-gate 		    stable(estr), ipath(epnamenp));
6297c478bd9Sstevel@tonic-gate 		if (ep == NULL) {
6307c478bd9Sstevel@tonic-gate 			out(O_ALTFP,
6317c478bd9Sstevel@tonic-gate 			    "reconstitute_observation: %s: "
6327c478bd9Sstevel@tonic-gate 			    "lookup of  \"%s\" in itree failed.",
6337c478bd9Sstevel@tonic-gate 			    tmpbuf, ipath2str(estr, ipath(epnamenp)));
63432d4e834STarik Soydan 			Undiag_reason = UD_VAL_BADOBS;
6357c478bd9Sstevel@tonic-gate 			tree_free(epnamenp);
6367c478bd9Sstevel@tonic-gate 			FREE(estr);
6377c478bd9Sstevel@tonic-gate 			break;
6387c478bd9Sstevel@tonic-gate 		}
6397c478bd9Sstevel@tonic-gate 		tree_free(epnamenp);
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 		/*
6427c478bd9Sstevel@tonic-gate 		 * We may or may not have a saved nvlist for the observation
6437c478bd9Sstevel@tonic-gate 		 */
6447c478bd9Sstevel@tonic-gate 		(void) snprintf(tmpbuf, OBBUFNMSZ, "observed%d.nvp", ocnt);
6457c478bd9Sstevel@tonic-gate 		pkdlen = fmd_buf_size(fmep->hdl, fmep->fmcase, tmpbuf);
6467c478bd9Sstevel@tonic-gate 		if (pkdlen != 0) {
6477c478bd9Sstevel@tonic-gate 			pkd = MALLOC(pkdlen);
6487c478bd9Sstevel@tonic-gate 			fmd_buf_read(fmep->hdl,
6497c478bd9Sstevel@tonic-gate 			    fmep->fmcase, tmpbuf, pkd, pkdlen);
6507aec1d6eScindi 			ASSERT(ep->nvp == NULL);
6517c478bd9Sstevel@tonic-gate 			if (nvlist_xunpack(pkd,
6527c478bd9Sstevel@tonic-gate 			    pkdlen, &ep->nvp, &Eft_nv_hdl) != 0)
6537c478bd9Sstevel@tonic-gate 				out(O_DIE|O_SYS, "pack of observed nvl failed");
6547c478bd9Sstevel@tonic-gate 			FREE(pkd);
6557c478bd9Sstevel@tonic-gate 		}
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 		if (ocnt == 0)
6587c478bd9Sstevel@tonic-gate 			fmep->e0 = ep;
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 		FREE(estr);
6617c478bd9Sstevel@tonic-gate 		fmep->ecurrent = ep;
6627c478bd9Sstevel@tonic-gate 		ep->count++;
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 		/* link it into list of observations seen */
6657c478bd9Sstevel@tonic-gate 		ep->observations = fmep->observations;
6667c478bd9Sstevel@tonic-gate 		fmep->observations = ep;
6677c478bd9Sstevel@tonic-gate 	}
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 	if (ocnt == fmep->uniqobs) {
6707c478bd9Sstevel@tonic-gate 		(void) fme_ready(fmep);
6717c478bd9Sstevel@tonic-gate 		return (0);
6727c478bd9Sstevel@tonic-gate 	}
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate 	return (1);
6757c478bd9Sstevel@tonic-gate }
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate /*
6787c478bd9Sstevel@tonic-gate  * restart_fme -- called during eft initialization.  Reconstitutes
6797c478bd9Sstevel@tonic-gate  *	an in-progress fme.
6807c478bd9Sstevel@tonic-gate  */
6817c478bd9Sstevel@tonic-gate void
6827c478bd9Sstevel@tonic-gate fme_restart(fmd_hdl_t *hdl, fmd_case_t *inprogress)
6837c478bd9Sstevel@tonic-gate {
6847c478bd9Sstevel@tonic-gate 	nvlist_t *defect;
6857c478bd9Sstevel@tonic-gate 	struct case_list *bad;
6867c478bd9Sstevel@tonic-gate 	struct fme *fmep;
687e5ba14ffSstephh 	struct cfgdata *cfgdata;
6887c478bd9Sstevel@tonic-gate 	size_t rawsz;
689b5016cbbSstephh 	struct event *ep;
690b5016cbbSstephh 	char *tmpbuf = alloca(OBBUFNMSZ);
691b5016cbbSstephh 	char *sepptr;
692b5016cbbSstephh 	char *estr;
693b5016cbbSstephh 	int elen;
694b5016cbbSstephh 	struct node *epnamenp = NULL;
695b5016cbbSstephh 	int init_size;
696b5016cbbSstephh 	extern int alloc_total();
697705e9f42SStephen Hanson 	char *reason;
698b5016cbbSstephh 
699b5016cbbSstephh 	/*
700b5016cbbSstephh 	 * ignore solved or closed cases
701b5016cbbSstephh 	 */
702b5016cbbSstephh 	if (fmd_case_solved(hdl, inprogress) ||
703b5016cbbSstephh 	    fmd_case_closed(hdl, inprogress))
704b5016cbbSstephh 		return;
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 	fmep = alloc_fme();
7077c478bd9Sstevel@tonic-gate 	fmep->fmcase = inprogress;
7087c478bd9Sstevel@tonic-gate 	fmep->hdl = hdl;
7097c478bd9Sstevel@tonic-gate 
7109f8ca725Sstephh 	if (fmd_buf_size(hdl, inprogress, WOBUF_POSTD) == 0) {
7119f8ca725Sstephh 		out(O_ALTFP, "restart_fme: no saved posted status");
71232d4e834STarik Soydan 		Undiag_reason = UD_VAL_MISSINGINFO;
7139f8ca725Sstephh 		goto badcase;
7149f8ca725Sstephh 	} else {
7159f8ca725Sstephh 		fmd_buf_read(hdl, inprogress, WOBUF_POSTD,
7169f8ca725Sstephh 		    (void *)&fmep->posted_suspects,
7179f8ca725Sstephh 		    sizeof (fmep->posted_suspects));
7189f8ca725Sstephh 	}
7199f8ca725Sstephh 
720b5016cbbSstephh 	if (fmd_buf_size(hdl, inprogress, WOBUF_ID) == 0) {
721b5016cbbSstephh 		out(O_ALTFP, "restart_fme: no saved id");
72232d4e834STarik Soydan 		Undiag_reason = UD_VAL_MISSINGINFO;
7239f8ca725Sstephh 		goto badcase;
724b5016cbbSstephh 	} else {
725b5016cbbSstephh 		fmd_buf_read(hdl, inprogress, WOBUF_ID, (void *)&fmep->id,
726b5016cbbSstephh 		    sizeof (fmep->id));
727b5016cbbSstephh 	}
728b5016cbbSstephh 	if (Nextid <= fmep->id)
729b5016cbbSstephh 		Nextid = fmep->id + 1;
730b5016cbbSstephh 
731b5016cbbSstephh 	out(O_ALTFP, "Replay FME %d", fmep->id);
7329f8ca725Sstephh 
7337c478bd9Sstevel@tonic-gate 	if (fmd_buf_size(hdl, inprogress, WOBUF_CFGLEN) != sizeof (size_t)) {
7347c478bd9Sstevel@tonic-gate 		out(O_ALTFP, "restart_fme: No config data");
73532d4e834STarik Soydan 		Undiag_reason = UD_VAL_MISSINGINFO;
7367c478bd9Sstevel@tonic-gate 		goto badcase;
7377c478bd9Sstevel@tonic-gate 	}
7387c478bd9Sstevel@tonic-gate 	fmd_buf_read(hdl, inprogress, WOBUF_CFGLEN, (void *)&rawsz,
7397c478bd9Sstevel@tonic-gate 	    sizeof (size_t));
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 	if ((fmep->e0r = fmd_case_getprincipal(hdl, inprogress)) == NULL) {
7427c478bd9Sstevel@tonic-gate 		out(O_ALTFP, "restart_fme: No event zero");
74332d4e834STarik Soydan 		Undiag_reason = UD_VAL_MISSINGZERO;
7447c478bd9Sstevel@tonic-gate 		goto badcase;
7457c478bd9Sstevel@tonic-gate 	}
7467c478bd9Sstevel@tonic-gate 
747b5016cbbSstephh 	if (fmd_buf_size(hdl, inprogress, WOBUF_PULL) == 0) {
748b5016cbbSstephh 		out(O_ALTFP, "restart_fme: no saved wait time");
74932d4e834STarik Soydan 		Undiag_reason = UD_VAL_MISSINGINFO;
750b5016cbbSstephh 		goto badcase;
751b5016cbbSstephh 	} else {
752b5016cbbSstephh 		fmd_buf_read(hdl, inprogress, WOBUF_PULL, (void *)&fmep->pull,
753b5016cbbSstephh 		    sizeof (fmep->pull));
754b5016cbbSstephh 	}
755b5016cbbSstephh 
756b5016cbbSstephh 	if (fmd_buf_size(hdl, inprogress, WOBUF_NOBS) == 0) {
757b5016cbbSstephh 		out(O_ALTFP, "restart_fme: no count of observations");
75832d4e834STarik Soydan 		Undiag_reason = UD_VAL_MISSINGINFO;
759b5016cbbSstephh 		goto badcase;
760b5016cbbSstephh 	} else {
761b5016cbbSstephh 		fmd_buf_read(hdl, inprogress, WOBUF_NOBS,
762b5016cbbSstephh 		    (void *)&fmep->uniqobs, sizeof (fmep->uniqobs));
763b5016cbbSstephh 	}
764b5016cbbSstephh 
765b5016cbbSstephh 	(void) snprintf(tmpbuf, OBBUFNMSZ, "observed0");
766b5016cbbSstephh 	elen = fmd_buf_size(fmep->hdl, fmep->fmcase, tmpbuf);
767b5016cbbSstephh 	if (elen == 0) {
768b5016cbbSstephh 		out(O_ALTFP, "reconstitute_observation: no %s buffer found.",
769b5016cbbSstephh 		    tmpbuf);
77032d4e834STarik Soydan 		Undiag_reason = UD_VAL_MISSINGOBS;
771b5016cbbSstephh 		goto badcase;
772b5016cbbSstephh 	}
773b5016cbbSstephh 	estr = MALLOC(elen);
774b5016cbbSstephh 	fmd_buf_read(fmep->hdl, fmep->fmcase, tmpbuf, estr, elen);
775b5016cbbSstephh 	sepptr = strchr(estr, '@');
776b5016cbbSstephh 	if (sepptr == NULL) {
777b5016cbbSstephh 		out(O_ALTFP, "reconstitute_observation: %s: "
778b5016cbbSstephh 		    "missing @ separator in %s.",
779b5016cbbSstephh 		    tmpbuf, estr);
78032d4e834STarik Soydan 		Undiag_reason = UD_VAL_MISSINGPATH;
781b5016cbbSstephh 		FREE(estr);
782b5016cbbSstephh 		goto badcase;
783b5016cbbSstephh 	}
784b5016cbbSstephh 	*sepptr = '\0';
785b5016cbbSstephh 	if ((epnamenp = pathstring2epnamenp(sepptr + 1)) == NULL) {
786b5016cbbSstephh 		out(O_ALTFP, "reconstitute_observation: %s: "
787b5016cbbSstephh 		    "trouble converting path string \"%s\" "
788b5016cbbSstephh 		    "to internal representation.", tmpbuf, sepptr + 1);
78932d4e834STarik Soydan 		Undiag_reason = UD_VAL_MISSINGPATH;
790b5016cbbSstephh 		FREE(estr);
791b5016cbbSstephh 		goto badcase;
792b5016cbbSstephh 	}
793705e9f42SStephen Hanson 	(void) prune_propagations(stable(estr), ipath(epnamenp));
794b5016cbbSstephh 	tree_free(epnamenp);
795b5016cbbSstephh 	FREE(estr);
796b5016cbbSstephh 
797b5016cbbSstephh 	init_size = alloc_total();
798b5016cbbSstephh 	out(O_ALTFP|O_STAMP, "start config_restore using %d bytes", init_size);
7997c478bd9Sstevel@tonic-gate 	cfgdata = MALLOC(sizeof (struct cfgdata));
8007c478bd9Sstevel@tonic-gate 	cfgdata->cooked = NULL;
8017c478bd9Sstevel@tonic-gate 	cfgdata->devcache = NULL;
802602ca9eaScth 	cfgdata->devidcache = NULL;
80344ed9dbbSStephen Hanson 	cfgdata->tpcache = NULL;
8047c478bd9Sstevel@tonic-gate 	cfgdata->cpucache = NULL;
805b5016cbbSstephh 	cfgdata->raw_refcnt = 1;
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 	if (rawsz > 0) {
8087c478bd9Sstevel@tonic-gate 		if (fmd_buf_size(hdl, inprogress, WOBUF_CFG) != rawsz) {
8097c478bd9Sstevel@tonic-gate 			out(O_ALTFP, "restart_fme: Config data size mismatch");
81032d4e834STarik Soydan 			Undiag_reason = UD_VAL_CFGMISMATCH;
8117c478bd9Sstevel@tonic-gate 			goto badcase;
8127c478bd9Sstevel@tonic-gate 		}
8137c478bd9Sstevel@tonic-gate 		cfgdata->begin = MALLOC(rawsz);
8147c478bd9Sstevel@tonic-gate 		cfgdata->end = cfgdata->nextfree = cfgdata->begin + rawsz;
8157c478bd9Sstevel@tonic-gate 		fmd_buf_read(hdl,
8167c478bd9Sstevel@tonic-gate 		    inprogress, WOBUF_CFG, cfgdata->begin, rawsz);
8177c478bd9Sstevel@tonic-gate 	} else {
8187c478bd9Sstevel@tonic-gate 		cfgdata->begin = cfgdata->end = cfgdata->nextfree = NULL;
8197c478bd9Sstevel@tonic-gate 	}
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate 	config_cook(cfgdata);
822e5ba14ffSstephh 	fmep->config = cfgdata->cooked;
823e5ba14ffSstephh 	config_free(cfgdata);
824b5016cbbSstephh 	out(O_ALTFP|O_STAMP, "config_restore added %d bytes",
825b5016cbbSstephh 	    alloc_total() - init_size);
826b5016cbbSstephh 
827e5ba14ffSstephh 	if ((fmep->eventtree = itree_create(fmep->config)) == NULL) {
8287c478bd9Sstevel@tonic-gate 		/* case not properly saved or irretrievable */
8297c478bd9Sstevel@tonic-gate 		out(O_ALTFP, "restart_fme: NULL instance tree");
83032d4e834STarik Soydan 		Undiag_reason = UD_VAL_INSTFAIL;
8317c478bd9Sstevel@tonic-gate 		goto badcase;
8327c478bd9Sstevel@tonic-gate 	}
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate 	itree_ptree(O_ALTFP|O_VERB2, fmep->eventtree);
8357c478bd9Sstevel@tonic-gate 
836b5016cbbSstephh 	if (reconstitute_observations(fmep) != 0)
8377c478bd9Sstevel@tonic-gate 		goto badcase;
8387c478bd9Sstevel@tonic-gate 
839b5016cbbSstephh 	out(O_ALTFP|O_NONL, "FME %d replay observations: ", fmep->id);
840b5016cbbSstephh 	for (ep = fmep->observations; ep; ep = ep->observations) {
841b5016cbbSstephh 		out(O_ALTFP|O_NONL, " ");
842b5016cbbSstephh 		itree_pevent_brief(O_ALTFP|O_NONL, ep);
8437c478bd9Sstevel@tonic-gate 	}
844b5016cbbSstephh 	out(O_ALTFP, NULL);
8457c478bd9Sstevel@tonic-gate 
8460cc1f05eSjrutt 	Open_fme_count++;
8470cc1f05eSjrutt 
8487c478bd9Sstevel@tonic-gate 	/* give the diagnosis algorithm a shot at the new FME state */
8499f8ca725Sstephh 	fme_eval(fmep, fmep->e0r);
8507c478bd9Sstevel@tonic-gate 	return;
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate badcase:
8537c478bd9Sstevel@tonic-gate 	if (fmep->eventtree != NULL)
8547c478bd9Sstevel@tonic-gate 		itree_free(fmep->eventtree);
855e5ba14ffSstephh 	if (fmep->config)
856e5ba14ffSstephh 		structconfig_free(fmep->config);
8577c478bd9Sstevel@tonic-gate 	destroy_fme_bufs(fmep);
8587c478bd9Sstevel@tonic-gate 	FREE(fmep);
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate 	/*
8617c478bd9Sstevel@tonic-gate 	 * Since we're unable to restart the case, add it to the undiagable
8627c478bd9Sstevel@tonic-gate 	 * list and solve and close it as appropriate.
8637c478bd9Sstevel@tonic-gate 	 */
8647c478bd9Sstevel@tonic-gate 	bad = MALLOC(sizeof (struct case_list));
8657c478bd9Sstevel@tonic-gate 	bad->next = NULL;
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate 	if (Undiagablecaselist != NULL)
8687c478bd9Sstevel@tonic-gate 		bad->next = Undiagablecaselist;
8697c478bd9Sstevel@tonic-gate 	Undiagablecaselist = bad;
8707c478bd9Sstevel@tonic-gate 	bad->fmcase = inprogress;
8717c478bd9Sstevel@tonic-gate 
8729f8ca725Sstephh 	out(O_ALTFP|O_NONL, "[case %s (unable to restart), ",
8737c478bd9Sstevel@tonic-gate 	    fmd_case_uuid(hdl, bad->fmcase));
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 	if (fmd_case_solved(hdl, bad->fmcase)) {
8769f8ca725Sstephh 		out(O_ALTFP|O_NONL, "already solved, ");
8777c478bd9Sstevel@tonic-gate 	} else {
8789f8ca725Sstephh 		out(O_ALTFP|O_NONL, "solving, ");
87932d4e834STarik Soydan 		defect = fmd_nvl_create_fault(hdl,
88032d4e834STarik Soydan 		    undiag_2defect_str(Undiag_reason), 100, NULL, NULL, NULL);
881705e9f42SStephen Hanson 		reason = undiag_2reason_str(Undiag_reason, NULL);
882705e9f42SStephen Hanson 		(void) nvlist_add_string(defect, UNDIAG_REASON, reason);
883705e9f42SStephen Hanson 		FREE(reason);
8847c478bd9Sstevel@tonic-gate 		fmd_case_add_suspect(hdl, bad->fmcase, defect);
8857c478bd9Sstevel@tonic-gate 		fmd_case_solve(hdl, bad->fmcase);
88632d4e834STarik Soydan 		Undiag_reason = UD_VAL_UNKNOWN;
8877c478bd9Sstevel@tonic-gate 	}
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 	if (fmd_case_closed(hdl, bad->fmcase)) {
8907c478bd9Sstevel@tonic-gate 		out(O_ALTFP, "already closed ]");
8917c478bd9Sstevel@tonic-gate 	} else {
8927c478bd9Sstevel@tonic-gate 		out(O_ALTFP, "closing ]");
8937c478bd9Sstevel@tonic-gate 		fmd_case_close(hdl, bad->fmcase);
8947c478bd9Sstevel@tonic-gate 	}
8957c478bd9Sstevel@tonic-gate }
8967c478bd9Sstevel@tonic-gate 
8977aec1d6eScindi /*ARGSUSED*/
8987aec1d6eScindi static void
8997aec1d6eScindi globals_destructor(void *left, void *right, void *arg)
9007aec1d6eScindi {
9017aec1d6eScindi 	struct evalue *evp = (struct evalue *)right;
9027aec1d6eScindi 	if (evp->t == NODEPTR)
90380ab886dSwesolows 		tree_free((struct node *)(uintptr_t)evp->v);
904837416c3Scy 	evp->v = (uintptr_t)NULL;
9057aec1d6eScindi 	FREE(evp);
9067aec1d6eScindi }
9077aec1d6eScindi 
9087c478bd9Sstevel@tonic-gate void
9097c478bd9Sstevel@tonic-gate destroy_fme(struct fme *f)
9107c478bd9Sstevel@tonic-gate {
9117c478bd9Sstevel@tonic-gate 	stats_delete(f->Rcount);
9127c478bd9Sstevel@tonic-gate 	stats_delete(f->Hcallcount);
9137c478bd9Sstevel@tonic-gate 	stats_delete(f->Rcallcount);
9147c478bd9Sstevel@tonic-gate 	stats_delete(f->Ccallcount);
9157c478bd9Sstevel@tonic-gate 	stats_delete(f->Ecallcount);
9167c478bd9Sstevel@tonic-gate 	stats_delete(f->Tcallcount);
9177c478bd9Sstevel@tonic-gate 	stats_delete(f->Marrowcount);
9187c478bd9Sstevel@tonic-gate 	stats_delete(f->diags);
9197c478bd9Sstevel@tonic-gate 
9209f8ca725Sstephh 	if (f->eventtree != NULL)
9219f8ca725Sstephh 		itree_free(f->eventtree);
922e5ba14ffSstephh 	if (f->config)
923e5ba14ffSstephh 		structconfig_free(f->config);
9247aec1d6eScindi 	lut_free(f->globals, globals_destructor, NULL);
9257c478bd9Sstevel@tonic-gate 	FREE(f);
9267c478bd9Sstevel@tonic-gate }
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate static const char *
9297c478bd9Sstevel@tonic-gate fme_state2str(enum fme_state s)
9307c478bd9Sstevel@tonic-gate {
9317c478bd9Sstevel@tonic-gate 	switch (s) {
9327c478bd9Sstevel@tonic-gate 	case FME_NOTHING:	return ("NOTHING");
9337c478bd9Sstevel@tonic-gate 	case FME_WAIT:		return ("WAIT");
9347c478bd9Sstevel@tonic-gate 	case FME_CREDIBLE:	return ("CREDIBLE");
9357c478bd9Sstevel@tonic-gate 	case FME_DISPROVED:	return ("DISPROVED");
9367aec1d6eScindi 	case FME_DEFERRED:	return ("DEFERRED");
9377c478bd9Sstevel@tonic-gate 	default:		return ("UNKNOWN");
9387c478bd9Sstevel@tonic-gate 	}
9397c478bd9Sstevel@tonic-gate }
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate static int
9427c478bd9Sstevel@tonic-gate is_problem(enum nametype t)
9437c478bd9Sstevel@tonic-gate {
9447c478bd9Sstevel@tonic-gate 	return (t == N_FAULT || t == N_DEFECT || t == N_UPSET);
9457c478bd9Sstevel@tonic-gate }
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate static int
9487c478bd9Sstevel@tonic-gate is_defect(enum nametype t)
9497c478bd9Sstevel@tonic-gate {
9507c478bd9Sstevel@tonic-gate 	return (t == N_DEFECT);
9517c478bd9Sstevel@tonic-gate }
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate static int
9547c478bd9Sstevel@tonic-gate is_upset(enum nametype t)
9557c478bd9Sstevel@tonic-gate {
9567c478bd9Sstevel@tonic-gate 	return (t == N_UPSET);
9577c478bd9Sstevel@tonic-gate }
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate static void
9607c478bd9Sstevel@tonic-gate fme_print(int flags, struct fme *fmep)
9617c478bd9Sstevel@tonic-gate {
9627c478bd9Sstevel@tonic-gate 	struct event *ep;
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate 	out(flags, "Fault Management Exercise %d", fmep->id);
9657c478bd9Sstevel@tonic-gate 	out(flags, "\t       State: %s", fme_state2str(fmep->state));
9667c478bd9Sstevel@tonic-gate 	out(flags|O_NONL, "\t  Start time: ");
9677c478bd9Sstevel@tonic-gate 	ptree_timeval(flags|O_NONL, &fmep->ull);
9687c478bd9Sstevel@tonic-gate 	out(flags, NULL);
9697c478bd9Sstevel@tonic-gate 	if (fmep->wull) {
9707c478bd9Sstevel@tonic-gate 		out(flags|O_NONL, "\t   Wait time: ");
9717c478bd9Sstevel@tonic-gate 		ptree_timeval(flags|O_NONL, &fmep->wull);
9727c478bd9Sstevel@tonic-gate 		out(flags, NULL);
9737c478bd9Sstevel@tonic-gate 	}
9747c478bd9Sstevel@tonic-gate 	out(flags|O_NONL, "\t          E0: ");
9757c478bd9Sstevel@tonic-gate 	if (fmep->e0)
9767c478bd9Sstevel@tonic-gate 		itree_pevent_brief(flags|O_NONL, fmep->e0);
9777c478bd9Sstevel@tonic-gate 	else
9787c478bd9Sstevel@tonic-gate 		out(flags|O_NONL, "NULL");
9797c478bd9Sstevel@tonic-gate 	out(flags, NULL);
9807c478bd9Sstevel@tonic-gate 	out(flags|O_NONL, "\tObservations:");
9817c478bd9Sstevel@tonic-gate 	for (ep = fmep->observations; ep; ep = ep->observations) {
9827c478bd9Sstevel@tonic-gate 		out(flags|O_NONL, " ");
9837c478bd9Sstevel@tonic-gate 		itree_pevent_brief(flags|O_NONL, ep);
9847c478bd9Sstevel@tonic-gate 	}
9857c478bd9Sstevel@tonic-gate 	out(flags, NULL);
9867c478bd9Sstevel@tonic-gate 	out(flags|O_NONL, "\tSuspect list:");
9877c478bd9Sstevel@tonic-gate 	for (ep = fmep->suspects; ep; ep = ep->suspects) {
9887c478bd9Sstevel@tonic-gate 		out(flags|O_NONL, " ");
9897c478bd9Sstevel@tonic-gate 		itree_pevent_brief(flags|O_NONL, ep);
9907c478bd9Sstevel@tonic-gate 	}
9917c478bd9Sstevel@tonic-gate 	out(flags, NULL);
9929f8ca725Sstephh 	if (fmep->eventtree != NULL) {
9939f8ca725Sstephh 		out(flags|O_VERB2, "\t        Tree:");
9949f8ca725Sstephh 		itree_ptree(flags|O_VERB2, fmep->eventtree);
9959f8ca725Sstephh 	}
9967c478bd9Sstevel@tonic-gate }
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate static struct node *
9997c478bd9Sstevel@tonic-gate pathstring2epnamenp(char *path)
10007c478bd9Sstevel@tonic-gate {
10017c478bd9Sstevel@tonic-gate 	char *sep = "/";
10027c478bd9Sstevel@tonic-gate 	struct node *ret;
10037c478bd9Sstevel@tonic-gate 	char *ptr;
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 	if ((ptr = strtok(path, sep)) == NULL)
10067c478bd9Sstevel@tonic-gate 		out(O_DIE, "pathstring2epnamenp: invalid empty class");
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate 	ret = tree_iname(stable(ptr), NULL, 0);
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate 	while ((ptr = strtok(NULL, sep)) != NULL)
10117c478bd9Sstevel@tonic-gate 		ret = tree_name_append(ret,
10127c478bd9Sstevel@tonic-gate 		    tree_iname(stable(ptr), NULL, 0));
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate 	return (ret);
10157c478bd9Sstevel@tonic-gate }
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate /*
10187c478bd9Sstevel@tonic-gate  * for a given upset sp, increment the corresponding SERD engine.  if the
10197c478bd9Sstevel@tonic-gate  * SERD engine trips, return the ename and ipp of the resulting ereport.
10207c478bd9Sstevel@tonic-gate  * returns true if engine tripped and *enamep and *ippp were filled in.
10217c478bd9Sstevel@tonic-gate  */
10227c478bd9Sstevel@tonic-gate static int
10237aec1d6eScindi serd_eval(struct fme *fmep, fmd_hdl_t *hdl, fmd_event_t *ffep,
10247aec1d6eScindi     fmd_case_t *fmcase, struct event *sp, const char **enamep,
10257aec1d6eScindi     const struct ipath **ippp)
10267c478bd9Sstevel@tonic-gate {
10277c478bd9Sstevel@tonic-gate 	struct node *serdinst;
10287c478bd9Sstevel@tonic-gate 	char *serdname;
1029049e2ea4SScott Davenport 	char *serdresource;
10308e7248e5SStephen Hanson 	char *serdclass;
10317aec1d6eScindi 	struct node *nid;
1032b5016cbbSstephh 	struct serd_entry *newentp;
1033049e2ea4SScott Davenport 	int i, serdn = -1, serdincrement = 1, len = 0;
10348e7248e5SStephen Hanson 	char *serdsuffix = NULL, *serdt = NULL;
1035b7d3956bSstephh 	struct evalue *ep;
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate 	ASSERT(sp->t == N_UPSET);
10387c478bd9Sstevel@tonic-gate 	ASSERT(ffep != NULL);
10397c478bd9Sstevel@tonic-gate 
1040b7d3956bSstephh 	if ((ep = (struct evalue *)lut_lookup(sp->serdprops,
1041b7d3956bSstephh 	    (void *)"n", (lut_cmp)strcmp)) != NULL) {
1042b7d3956bSstephh 		ASSERT(ep->t == UINT64);
1043b7d3956bSstephh 		serdn = (int)ep->v;
1044b7d3956bSstephh 	}
1045b7d3956bSstephh 	if ((ep = (struct evalue *)lut_lookup(sp->serdprops,
1046b7d3956bSstephh 	    (void *)"t", (lut_cmp)strcmp)) != NULL) {
1047b7d3956bSstephh 		ASSERT(ep->t == STRING);
1048b7d3956bSstephh 		serdt = (char *)(uintptr_t)ep->v;
1049b7d3956bSstephh 	}
1050b7d3956bSstephh 	if ((ep = (struct evalue *)lut_lookup(sp->serdprops,
1051b7d3956bSstephh 	    (void *)"suffix", (lut_cmp)strcmp)) != NULL) {
1052b7d3956bSstephh 		ASSERT(ep->t == STRING);
1053b7d3956bSstephh 		serdsuffix = (char *)(uintptr_t)ep->v;
1054b7d3956bSstephh 	}
1055b7d3956bSstephh 	if ((ep = (struct evalue *)lut_lookup(sp->serdprops,
1056b7d3956bSstephh 	    (void *)"increment", (lut_cmp)strcmp)) != NULL) {
1057b7d3956bSstephh 		ASSERT(ep->t == UINT64);
1058b7d3956bSstephh 		serdincrement = (int)ep->v;
1059b7d3956bSstephh 	}
1060b7d3956bSstephh 
10617c478bd9Sstevel@tonic-gate 	/*
10627c478bd9Sstevel@tonic-gate 	 * obtain instanced SERD engine from the upset sp.  from this
10637c478bd9Sstevel@tonic-gate 	 * derive serdname, the string used to identify the SERD engine.
10647c478bd9Sstevel@tonic-gate 	 */
10657c478bd9Sstevel@tonic-gate 	serdinst = eventprop_lookup(sp, L_engine);
10667c478bd9Sstevel@tonic-gate 
10677c478bd9Sstevel@tonic-gate 	if (serdinst == NULL)
1068b7d3956bSstephh 		return (-1);
10697c478bd9Sstevel@tonic-gate 
10708e7248e5SStephen Hanson 	len = strlen(serdinst->u.stmt.np->u.event.ename->u.name.s) + 1;
1071049e2ea4SScott Davenport 	if (serdsuffix != NULL)
1072049e2ea4SScott Davenport 		len += strlen(serdsuffix);
10738e7248e5SStephen Hanson 	serdclass = MALLOC(len);
10748e7248e5SStephen Hanson 	if (serdsuffix != NULL)
10758e7248e5SStephen Hanson 		(void) snprintf(serdclass, len, "%s%s",
10768e7248e5SStephen Hanson 		    serdinst->u.stmt.np->u.event.ename->u.name.s, serdsuffix);
10778e7248e5SStephen Hanson 	else
10788e7248e5SStephen Hanson 		(void) snprintf(serdclass, len, "%s",
10798e7248e5SStephen Hanson 		    serdinst->u.stmt.np->u.event.ename->u.name.s);
10808e7248e5SStephen Hanson 	serdresource = ipath2str(NULL,
10818e7248e5SStephen Hanson 	    ipath(serdinst->u.stmt.np->u.event.epname));
10828e7248e5SStephen Hanson 	len += strlen(serdresource) + 1;
10838e7248e5SStephen Hanson 	serdname = MALLOC(len);
10848e7248e5SStephen Hanson 	(void) snprintf(serdname, len, "%s@%s", serdclass, serdresource);
1085049e2ea4SScott Davenport 	FREE(serdresource);
1086b7d3956bSstephh 
10877aec1d6eScindi 	/* handle serd engine "id" property, if there is one */
10887aec1d6eScindi 	if ((nid =
10897aec1d6eScindi 	    lut_lookup(serdinst->u.stmt.lutp, (void *)L_id, NULL)) != NULL) {
10907aec1d6eScindi 		struct evalue *gval;
10917aec1d6eScindi 		char suffixbuf[200];
10927aec1d6eScindi 		char *suffix;
10937aec1d6eScindi 		char *nserdname;
10947aec1d6eScindi 		size_t nname;
10957aec1d6eScindi 
10967aec1d6eScindi 		out(O_ALTFP|O_NONL, "serd \"%s\" id: ", serdname);
10977aec1d6eScindi 		ptree_name_iter(O_ALTFP|O_NONL, nid);
10987aec1d6eScindi 
10997aec1d6eScindi 		ASSERTinfo(nid->t == T_GLOBID, ptree_nodetype2str(nid->t));
11007aec1d6eScindi 
11017aec1d6eScindi 		if ((gval = lut_lookup(fmep->globals,
11027aec1d6eScindi 		    (void *)nid->u.globid.s, NULL)) == NULL) {
11037aec1d6eScindi 			out(O_ALTFP, " undefined");
11047aec1d6eScindi 		} else if (gval->t == UINT64) {
11057aec1d6eScindi 			out(O_ALTFP, " %llu", gval->v);
11067aec1d6eScindi 			(void) sprintf(suffixbuf, "%llu", gval->v);
11077aec1d6eScindi 			suffix = suffixbuf;
11087aec1d6eScindi 		} else {
110980ab886dSwesolows 			out(O_ALTFP, " \"%s\"", (char *)(uintptr_t)gval->v);
111080ab886dSwesolows 			suffix = (char *)(uintptr_t)gval->v;
11117aec1d6eScindi 		}
11127aec1d6eScindi 
11137aec1d6eScindi 		nname = strlen(serdname) + strlen(suffix) + 2;
11147aec1d6eScindi 		nserdname = MALLOC(nname);
11157aec1d6eScindi 		(void) snprintf(nserdname, nname, "%s:%s", serdname, suffix);
11167aec1d6eScindi 		FREE(serdname);
11177aec1d6eScindi 		serdname = nserdname;
11187aec1d6eScindi 	}
11197aec1d6eScindi 
1120b7d3956bSstephh 	/*
1121b7d3956bSstephh 	 * if the engine is empty, and we have an override for n/t then
1122b7d3956bSstephh 	 * destroy and recreate it.
1123b7d3956bSstephh 	 */
1124b7d3956bSstephh 	if ((serdn != -1 || serdt != NULL) && fmd_serd_exists(hdl, serdname) &&
1125b7d3956bSstephh 	    fmd_serd_empty(hdl, serdname))
1126b7d3956bSstephh 		fmd_serd_destroy(hdl, serdname);
1127b7d3956bSstephh 
11287c478bd9Sstevel@tonic-gate 	if (!fmd_serd_exists(hdl, serdname)) {
11297c478bd9Sstevel@tonic-gate 		struct node *nN, *nT;
1130e5ba14ffSstephh 		const char *s;
1131e5ba14ffSstephh 		struct node *nodep;
1132e5ba14ffSstephh 		struct config *cp;
1133e5ba14ffSstephh 		char *path;
1134e5ba14ffSstephh 		uint_t nval;
1135e5ba14ffSstephh 		hrtime_t tval;
1136e5ba14ffSstephh 		int i;
1137e5ba14ffSstephh 		char *ptr;
1138e5ba14ffSstephh 		int got_n_override = 0, got_t_override = 0;
11397c478bd9Sstevel@tonic-gate 
11407c478bd9Sstevel@tonic-gate 		/* no SERD engine yet, so create it */
1141e5ba14ffSstephh 		nodep = serdinst->u.stmt.np->u.event.epname;
1142e5ba14ffSstephh 		path = ipath2str(NULL, ipath(nodep));
1143e5ba14ffSstephh 		cp = config_lookup(fmep->config, path, 0);
1144e5ba14ffSstephh 		FREE((void *)path);
1145e5ba14ffSstephh 
1146e5ba14ffSstephh 		/*
1147e5ba14ffSstephh 		 * We allow serd paramaters to be overridden, either from
1148e5ba14ffSstephh 		 * eft.conf file values (if Serd_Override is set) or from
1149e5ba14ffSstephh 		 * driver properties (for "serd.io.device" engines).
1150e5ba14ffSstephh 		 */
1151e5ba14ffSstephh 		if (Serd_Override != NULL) {
1152e5ba14ffSstephh 			char *save_ptr, *ptr1, *ptr2, *ptr3;
1153e5ba14ffSstephh 			ptr3 = save_ptr = STRDUP(Serd_Override);
1154e5ba14ffSstephh 			while (*ptr3 != '\0') {
1155e5ba14ffSstephh 				ptr1 = strchr(ptr3, ',');
1156e5ba14ffSstephh 				*ptr1 = '\0';
11578e7248e5SStephen Hanson 				if (strcmp(ptr3, serdclass) == 0) {
1158e5ba14ffSstephh 					ptr2 =  strchr(ptr1 + 1, ',');
1159e5ba14ffSstephh 					*ptr2 = '\0';
1160e5ba14ffSstephh 					nval = atoi(ptr1 + 1);
1161e5ba14ffSstephh 					out(O_ALTFP, "serd override %s_n %d",
11628e7248e5SStephen Hanson 					    serdclass, nval);
1163e5ba14ffSstephh 					ptr3 =  strchr(ptr2 + 1, ' ');
1164e5ba14ffSstephh 					if (ptr3)
1165e5ba14ffSstephh 						*ptr3 = '\0';
1166e5ba14ffSstephh 					ptr = STRDUP(ptr2 + 1);
1167e5ba14ffSstephh 					out(O_ALTFP, "serd override %s_t %s",
11688e7248e5SStephen Hanson 					    serdclass, ptr);
1169e5ba14ffSstephh 					got_n_override = 1;
1170e5ba14ffSstephh 					got_t_override = 1;
1171e5ba14ffSstephh 					break;
1172e5ba14ffSstephh 				} else {
1173e5ba14ffSstephh 					ptr2 =  strchr(ptr1 + 1, ',');
1174e5ba14ffSstephh 					ptr3 =  strchr(ptr2 + 1, ' ');
1175e5ba14ffSstephh 					if (ptr3 == NULL)
1176e5ba14ffSstephh 						break;
1177e5ba14ffSstephh 				}
1178e5ba14ffSstephh 				ptr3++;
1179e5ba14ffSstephh 			}
1180e5ba14ffSstephh 			FREE(save_ptr);
1181e5ba14ffSstephh 		}
11827c478bd9Sstevel@tonic-gate 
1183e5ba14ffSstephh 		if (cp && got_n_override == 0) {
1184e5ba14ffSstephh 			/*
11858e7248e5SStephen Hanson 			 * convert serd engine class into property name
1186e5ba14ffSstephh 			 */
11878e7248e5SStephen Hanson 			char *prop_name = MALLOC(strlen(serdclass) + 3);
11888e7248e5SStephen Hanson 			for (i = 0; i < strlen(serdclass); i++) {
11898e7248e5SStephen Hanson 				if (serdclass[i] == '.')
11908e7248e5SStephen Hanson 					prop_name[i] = '_';
1191e5ba14ffSstephh 				else
11928e7248e5SStephen Hanson 					prop_name[i] = serdclass[i];
1193e5ba14ffSstephh 			}
11948e7248e5SStephen Hanson 			prop_name[i++] = '_';
11958e7248e5SStephen Hanson 			prop_name[i++] = 'n';
11968e7248e5SStephen Hanson 			prop_name[i] = '\0';
11978e7248e5SStephen Hanson 			if (s = config_getprop(cp, prop_name)) {
1198e5ba14ffSstephh 				nval = atoi(s);
11998e7248e5SStephen Hanson 				out(O_ALTFP, "serd override %s_n %s",
12008e7248e5SStephen Hanson 				    serdclass, s);
1201e5ba14ffSstephh 				got_n_override = 1;
1202e5ba14ffSstephh 			}
12038e7248e5SStephen Hanson 			prop_name[i - 1] = 't';
12048e7248e5SStephen Hanson 			if (s = config_getprop(cp, prop_name)) {
1205e5ba14ffSstephh 				ptr = STRDUP(s);
12068e7248e5SStephen Hanson 				out(O_ALTFP, "serd override %s_t %s",
12078e7248e5SStephen Hanson 				    serdclass, s);
1208e5ba14ffSstephh 				got_t_override = 1;
1209e5ba14ffSstephh 			}
12108e7248e5SStephen Hanson 			FREE(prop_name);
1211e5ba14ffSstephh 		}
12127c478bd9Sstevel@tonic-gate 
1213b7d3956bSstephh 		if (serdn != -1 && got_n_override == 0) {
1214b7d3956bSstephh 			nval = serdn;
12158e7248e5SStephen Hanson 			out(O_ALTFP, "serd override %s_n %d", serdclass, serdn);
1216b7d3956bSstephh 			got_n_override = 1;
1217b7d3956bSstephh 		}
1218b7d3956bSstephh 		if (serdt != NULL && got_t_override == 0) {
1219b7d3956bSstephh 			ptr = STRDUP(serdt);
12208e7248e5SStephen Hanson 			out(O_ALTFP, "serd override %s_t %s", serdclass, serdt);
1221b7d3956bSstephh 			got_t_override = 1;
1222b7d3956bSstephh 		}
1223b7d3956bSstephh 
1224e5ba14ffSstephh 		if (!got_n_override) {
1225e5ba14ffSstephh 			nN = lut_lookup(serdinst->u.stmt.lutp, (void *)L_N,
1226e5ba14ffSstephh 			    NULL);
1227e5ba14ffSstephh 			ASSERT(nN->t == T_NUM);
1228e5ba14ffSstephh 			nval = (uint_t)nN->u.ull;
1229e5ba14ffSstephh 		}
1230e5ba14ffSstephh 		if (!got_t_override) {
1231e5ba14ffSstephh 			nT = lut_lookup(serdinst->u.stmt.lutp, (void *)L_T,
1232e5ba14ffSstephh 			    NULL);
1233e5ba14ffSstephh 			ASSERT(nT->t == T_TIMEVAL);
1234e5ba14ffSstephh 			tval = (hrtime_t)nT->u.ull;
1235e5ba14ffSstephh 		} else {
1236e5ba14ffSstephh 			const unsigned long long *ullp;
1237e5ba14ffSstephh 			const char *suffix;
1238e5ba14ffSstephh 			int len;
1239e5ba14ffSstephh 
1240e5ba14ffSstephh 			len = strspn(ptr, "0123456789");
1241e5ba14ffSstephh 			suffix = stable(&ptr[len]);
1242e5ba14ffSstephh 			ullp = (unsigned long long *)lut_lookup(Timesuffixlut,
1243e5ba14ffSstephh 			    (void *)suffix, NULL);
1244e5ba14ffSstephh 			ptr[len] = '\0';
1245b7d3956bSstephh 			tval = strtoull(ptr, NULL, 0) * (ullp ? *ullp : 1ll);
1246e5ba14ffSstephh 			FREE(ptr);
1247e5ba14ffSstephh 		}
1248e5ba14ffSstephh 		fmd_serd_create(hdl, serdname, nval, tval);
12497c478bd9Sstevel@tonic-gate 	}
12507c478bd9Sstevel@tonic-gate 
1251b5016cbbSstephh 	newentp = MALLOC(sizeof (*newentp));
12528e7248e5SStephen Hanson 	newentp->ename = stable(serdclass);
12538e7248e5SStephen Hanson 	FREE(serdclass);
1254b5016cbbSstephh 	newentp->ipath = ipath(serdinst->u.stmt.np->u.event.epname);
1255b5016cbbSstephh 	newentp->hdl = hdl;
1256b5016cbbSstephh 	if (lut_lookup(SerdEngines, newentp, (lut_cmp)serd_cmp) == NULL) {
1257b5016cbbSstephh 		SerdEngines = lut_add(SerdEngines, (void *)newentp,
1258e5ba14ffSstephh 		    (void *)newentp, (lut_cmp)serd_cmp);
1259b5016cbbSstephh 		Serd_need_save = 1;
1260b5016cbbSstephh 		serd_save();
1261b5016cbbSstephh 	} else {
1262b5016cbbSstephh 		FREE(newentp);
1263b5016cbbSstephh 	}
1264b5016cbbSstephh 
12657c478bd9Sstevel@tonic-gate 
12667c478bd9Sstevel@tonic-gate 	/*
12677c478bd9Sstevel@tonic-gate 	 * increment SERD engine.  if engine fires, reset serd
1268b7d3956bSstephh 	 * engine and return trip_strcode if required.
12697c478bd9Sstevel@tonic-gate 	 */
1270b7d3956bSstephh 	for (i = 0; i < serdincrement; i++) {
1271b7d3956bSstephh 		if (fmd_serd_record(hdl, serdname, ffep)) {
1272b7d3956bSstephh 			fmd_case_add_serd(hdl, fmcase, serdname);
1273b7d3956bSstephh 			fmd_serd_reset(hdl, serdname);
1274b7d3956bSstephh 
1275b7d3956bSstephh 			if (ippp) {
1276b7d3956bSstephh 				struct node *tripinst =
1277b7d3956bSstephh 				    lut_lookup(serdinst->u.stmt.lutp,
1278b7d3956bSstephh 				    (void *)L_trip, NULL);
1279b7d3956bSstephh 				ASSERT(tripinst != NULL);
1280b7d3956bSstephh 				*enamep = tripinst->u.event.ename->u.name.s;
1281b7d3956bSstephh 				*ippp = ipath(tripinst->u.event.epname);
1282b7d3956bSstephh 				out(O_ALTFP|O_NONL,
1283b7d3956bSstephh 				    "[engine fired: %s, sending: ", serdname);
1284b7d3956bSstephh 				ipath_print(O_ALTFP|O_NONL, *enamep, *ippp);
1285b7d3956bSstephh 				out(O_ALTFP, "]");
1286b7d3956bSstephh 			} else {
1287b7d3956bSstephh 				out(O_ALTFP, "[engine fired: %s, no trip]",
1288b7d3956bSstephh 				    serdname);
1289b7d3956bSstephh 			}
1290b7d3956bSstephh 			FREE(serdname);
1291b7d3956bSstephh 			return (1);
1292b7d3956bSstephh 		}
12937c478bd9Sstevel@tonic-gate 	}
12947c478bd9Sstevel@tonic-gate 
12957c478bd9Sstevel@tonic-gate 	FREE(serdname);
12967c478bd9Sstevel@tonic-gate 	return (0);
12977c478bd9Sstevel@tonic-gate }
12987c478bd9Sstevel@tonic-gate 
12997c478bd9Sstevel@tonic-gate /*
13007c478bd9Sstevel@tonic-gate  * search a suspect list for upsets.  feed each upset to serd_eval() and
13017c478bd9Sstevel@tonic-gate  * build up tripped[], an array of ereports produced by the firing of
13027c478bd9Sstevel@tonic-gate  * any SERD engines.  then feed each ereport back into
13037c478bd9Sstevel@tonic-gate  * fme_receive_report().
13047c478bd9Sstevel@tonic-gate  *
13057c478bd9Sstevel@tonic-gate  * returns ntrip, the number of these ereports produced.
13067c478bd9Sstevel@tonic-gate  */
13077c478bd9Sstevel@tonic-gate static int
13087c478bd9Sstevel@tonic-gate upsets_eval(struct fme *fmep, fmd_event_t *ffep)
13097c478bd9Sstevel@tonic-gate {
13107c478bd9Sstevel@tonic-gate 	/* we build an array of tripped ereports that we send ourselves */
13117c478bd9Sstevel@tonic-gate 	struct {
13127c478bd9Sstevel@tonic-gate 		const char *ename;
13137c478bd9Sstevel@tonic-gate 		const struct ipath *ipp;
13147c478bd9Sstevel@tonic-gate 	} *tripped;
13157c478bd9Sstevel@tonic-gate 	struct event *sp;
13167c478bd9Sstevel@tonic-gate 	int ntrip, nupset, i;
13177c478bd9Sstevel@tonic-gate 
13187c478bd9Sstevel@tonic-gate 	/*
13197c478bd9Sstevel@tonic-gate 	 * count the number of upsets to determine the upper limit on
13207c478bd9Sstevel@tonic-gate 	 * expected trip ereport strings.  remember that one upset can
13217c478bd9Sstevel@tonic-gate 	 * lead to at most one ereport.
13227c478bd9Sstevel@tonic-gate 	 */
13237c478bd9Sstevel@tonic-gate 	nupset = 0;
13247c478bd9Sstevel@tonic-gate 	for (sp = fmep->suspects; sp; sp = sp->suspects) {
13257c478bd9Sstevel@tonic-gate 		if (sp->t == N_UPSET)
13267c478bd9Sstevel@tonic-gate 			nupset++;
13277c478bd9Sstevel@tonic-gate 	}
13287c478bd9Sstevel@tonic-gate 
13297c478bd9Sstevel@tonic-gate 	if (nupset == 0)
13307c478bd9Sstevel@tonic-gate 		return (0);
13317c478bd9Sstevel@tonic-gate 
13327c478bd9Sstevel@tonic-gate 	/*
13337c478bd9Sstevel@tonic-gate 	 * get to this point if we have upsets and expect some trip
13347c478bd9Sstevel@tonic-gate 	 * ereports
13357c478bd9Sstevel@tonic-gate 	 */
13367c478bd9Sstevel@tonic-gate 	tripped = alloca(sizeof (*tripped) * nupset);
13377c478bd9Sstevel@tonic-gate 	bzero((void *)tripped, sizeof (*tripped) * nupset);
13387c478bd9Sstevel@tonic-gate 
13397c478bd9Sstevel@tonic-gate 	ntrip = 0;
13407c478bd9Sstevel@tonic-gate 	for (sp = fmep->suspects; sp; sp = sp->suspects)
13413e8d8e18Sdb 		if (sp->t == N_UPSET &&
13427aec1d6eScindi 		    serd_eval(fmep, fmep->hdl, ffep, fmep->fmcase, sp,
1343b7d3956bSstephh 		    &tripped[ntrip].ename, &tripped[ntrip].ipp) == 1)
13447c478bd9Sstevel@tonic-gate 			ntrip++;
13457c478bd9Sstevel@tonic-gate 
134627134bdaSstephh 	for (i = 0; i < ntrip; i++) {
134727134bdaSstephh 		struct event *ep, *nep;
134827134bdaSstephh 		struct fme *nfmep;
134927134bdaSstephh 		fmd_case_t *fmcase;
135027134bdaSstephh 		const struct ipath *ipp;
135127134bdaSstephh 		const char *eventstring;
135227134bdaSstephh 		int prev_verbose;
135327134bdaSstephh 		unsigned long long my_delay = TIMEVAL_EVENTUALLY;
135427134bdaSstephh 		enum fme_state state;
135527134bdaSstephh 
135627134bdaSstephh 		/*
135727134bdaSstephh 		 * First try and evaluate a case with the trip ereport plus
135827134bdaSstephh 		 * all the other ereports that cause the trip. If that fails
135927134bdaSstephh 		 * to evaluate then try again with just this ereport on its own.
136027134bdaSstephh 		 */
136127134bdaSstephh 		out(O_ALTFP|O_NONL, "fme_receive_report_serd: ");
136227134bdaSstephh 		ipath_print(O_ALTFP|O_NONL, tripped[i].ename, tripped[i].ipp);
136327134bdaSstephh 		out(O_ALTFP|O_STAMP, NULL);
136427134bdaSstephh 		ep = fmep->e0;
136527134bdaSstephh 		eventstring = ep->enode->u.event.ename->u.name.s;
136627134bdaSstephh 		ipp = ep->ipp;
136727134bdaSstephh 
136827134bdaSstephh 		/*
136927134bdaSstephh 		 * create a duplicate fme and case
137027134bdaSstephh 		 */
137127134bdaSstephh 		fmcase = fmd_case_open(fmep->hdl, NULL);
137227134bdaSstephh 		out(O_ALTFP|O_NONL, "duplicate fme for event [");
137327134bdaSstephh 		ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
137427134bdaSstephh 		out(O_ALTFP, " ]");
1375705e9f42SStephen Hanson 
137627134bdaSstephh 		if ((nfmep = newfme(eventstring, ipp, fmep->hdl,
1377705e9f42SStephen Hanson 		    fmcase, ffep, ep->nvp)) == NULL) {
137827134bdaSstephh 			out(O_ALTFP|O_NONL, "[");
137927134bdaSstephh 			ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
138027134bdaSstephh 			out(O_ALTFP, " CANNOT DIAGNOSE]");
138127134bdaSstephh 			continue;
138227134bdaSstephh 		}
1383705e9f42SStephen Hanson 
138427134bdaSstephh 		Open_fme_count++;
138527134bdaSstephh 		nfmep->pull = fmep->pull;
138627134bdaSstephh 		init_fme_bufs(nfmep);
138727134bdaSstephh 		out(O_ALTFP|O_NONL, "[");
138827134bdaSstephh 		ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
138927134bdaSstephh 		out(O_ALTFP, " created FME%d, case %s]", nfmep->id,
139027134bdaSstephh 		    fmd_case_uuid(nfmep->hdl, nfmep->fmcase));
139127134bdaSstephh 		if (ffep) {
139227134bdaSstephh 			fmd_case_setprincipal(nfmep->hdl, nfmep->fmcase, ffep);
1393b7d3956bSstephh 			fmd_case_add_ereport(nfmep->hdl, nfmep->fmcase, ffep);
139427134bdaSstephh 			nfmep->e0r = ffep;
139527134bdaSstephh 		}
139627134bdaSstephh 
139727134bdaSstephh 		/*
139827134bdaSstephh 		 * add the original ereports
139927134bdaSstephh 		 */
140027134bdaSstephh 		for (ep = fmep->observations; ep; ep = ep->observations) {
140127134bdaSstephh 			eventstring = ep->enode->u.event.ename->u.name.s;
140227134bdaSstephh 			ipp = ep->ipp;
140327134bdaSstephh 			out(O_ALTFP|O_NONL, "adding event [");
140427134bdaSstephh 			ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
140527134bdaSstephh 			out(O_ALTFP, " ]");
140627134bdaSstephh 			nep = itree_lookup(nfmep->eventtree, eventstring, ipp);
140727134bdaSstephh 			if (nep->count++ == 0) {
140827134bdaSstephh 				nep->observations = nfmep->observations;
140927134bdaSstephh 				nfmep->observations = nep;
141027134bdaSstephh 				serialize_observation(nfmep, eventstring, ipp);
141127134bdaSstephh 				nep->nvp = evnv_dupnvl(ep->nvp);
141227134bdaSstephh 			}
1413b7d3956bSstephh 			if (ep->ffep && ep->ffep != ffep)
141427134bdaSstephh 				fmd_case_add_ereport(nfmep->hdl, nfmep->fmcase,
1415b7d3956bSstephh 				    ep->ffep);
141627134bdaSstephh 			stats_counter_bump(nfmep->Rcount);
141727134bdaSstephh 		}
141827134bdaSstephh 
141927134bdaSstephh 		/*
142027134bdaSstephh 		 * add the serd trigger ereport
142127134bdaSstephh 		 */
142227134bdaSstephh 		if ((ep = itree_lookup(nfmep->eventtree, tripped[i].ename,
142327134bdaSstephh 		    tripped[i].ipp)) == NULL) {
142427134bdaSstephh 			/*
142527134bdaSstephh 			 * The trigger ereport is not in the instance tree. It
142627134bdaSstephh 			 * was presumably removed by prune_propagations() as
142727134bdaSstephh 			 * this combination of events is not present in the
142827134bdaSstephh 			 * rules.
142927134bdaSstephh 			 */
143027134bdaSstephh 			out(O_ALTFP, "upsets_eval: e0 not in instance tree");
143132d4e834STarik Soydan 			Undiag_reason = UD_VAL_BADEVENTI;
143227134bdaSstephh 			goto retry_lone_ereport;
143327134bdaSstephh 		}
143427134bdaSstephh 		out(O_ALTFP|O_NONL, "adding event [");
143527134bdaSstephh 		ipath_print(O_ALTFP|O_NONL, tripped[i].ename, tripped[i].ipp);
143627134bdaSstephh 		out(O_ALTFP, " ]");
143727134bdaSstephh 		nfmep->ecurrent = ep;
143827134bdaSstephh 		ep->nvp = NULL;
143927134bdaSstephh 		ep->count = 1;
144027134bdaSstephh 		ep->observations = nfmep->observations;
144127134bdaSstephh 		nfmep->observations = ep;
144227134bdaSstephh 
144327134bdaSstephh 		/*
144427134bdaSstephh 		 * just peek first.
144527134bdaSstephh 		 */
144627134bdaSstephh 		nfmep->peek = 1;
144727134bdaSstephh 		prev_verbose = Verbose;
144827134bdaSstephh 		if (Debug == 0)
144927134bdaSstephh 			Verbose = 0;
145027134bdaSstephh 		lut_walk(nfmep->eventtree, (lut_cb)clear_arrows, (void *)nfmep);
145127134bdaSstephh 		state = hypothesise(nfmep, nfmep->e0, nfmep->ull, &my_delay);
145227134bdaSstephh 		nfmep->peek = 0;
145327134bdaSstephh 		Verbose = prev_verbose;
145427134bdaSstephh 		if (state == FME_DISPROVED) {
145527134bdaSstephh 			out(O_ALTFP, "upsets_eval: hypothesis disproved");
145632d4e834STarik Soydan 			Undiag_reason = UD_VAL_UNSOLVD;
145727134bdaSstephh retry_lone_ereport:
145827134bdaSstephh 			/*
145927134bdaSstephh 			 * However the trigger ereport on its own might be
146027134bdaSstephh 			 * diagnosable, so check for that. Undo the new fme
146127134bdaSstephh 			 * and case we just created and call fme_receive_report.
146227134bdaSstephh 			 */
146327134bdaSstephh 			out(O_ALTFP|O_NONL, "[");
146427134bdaSstephh 			ipath_print(O_ALTFP|O_NONL, tripped[i].ename,
146527134bdaSstephh 			    tripped[i].ipp);
146627134bdaSstephh 			out(O_ALTFP, " retrying with just trigger ereport]");
146727134bdaSstephh 			itree_free(nfmep->eventtree);
146827134bdaSstephh 			nfmep->eventtree = NULL;
146927134bdaSstephh 			structconfig_free(nfmep->config);
147027134bdaSstephh 			nfmep->config = NULL;
147127134bdaSstephh 			destroy_fme_bufs(nfmep);
147227134bdaSstephh 			fmd_case_close(nfmep->hdl, nfmep->fmcase);
147327134bdaSstephh 			fme_receive_report(fmep->hdl, ffep,
147427134bdaSstephh 			    tripped[i].ename, tripped[i].ipp, NULL);
147527134bdaSstephh 			continue;
147627134bdaSstephh 		}
147727134bdaSstephh 
147827134bdaSstephh 		/*
147927134bdaSstephh 		 * and evaluate
148027134bdaSstephh 		 */
148127134bdaSstephh 		serialize_observation(nfmep, tripped[i].ename, tripped[i].ipp);
148227134bdaSstephh 		fme_eval(nfmep, ffep);
148327134bdaSstephh 	}
14847c478bd9Sstevel@tonic-gate 
14857c478bd9Sstevel@tonic-gate 	return (ntrip);
14867c478bd9Sstevel@tonic-gate }
14877c478bd9Sstevel@tonic-gate 
14887c478bd9Sstevel@tonic-gate /*
14897c478bd9Sstevel@tonic-gate  * fme_receive_external_report -- call when an external ereport comes in
14907c478bd9Sstevel@tonic-gate  *
14917c478bd9Sstevel@tonic-gate  * this routine just converts the relevant information from the ereport
14927c478bd9Sstevel@tonic-gate  * into a format used internally and passes it on to fme_receive_report().
14937c478bd9Sstevel@tonic-gate  */
14947c478bd9Sstevel@tonic-gate void
14957c478bd9Sstevel@tonic-gate fme_receive_external_report(fmd_hdl_t *hdl, fmd_event_t *ffep, nvlist_t *nvl,
1496602ca9eaScth     const char *class)
14977c478bd9Sstevel@tonic-gate {
1498602ca9eaScth 	struct node		*epnamenp;
1499602ca9eaScth 	fmd_case_t		*fmcase;
1500602ca9eaScth 	const struct ipath	*ipp;
1501705e9f42SStephen Hanson 	nvlist_t		*detector = NULL;
15027c478bd9Sstevel@tonic-gate 
1503602ca9eaScth 	class = stable(class);
1504b5016cbbSstephh 
1505602ca9eaScth 	/* Get the component path from the ereport */
1506602ca9eaScth 	epnamenp = platform_getpath(nvl);
1507602ca9eaScth 
1508602ca9eaScth 	/* See if we ended up without a path. */
1509602ca9eaScth 	if (epnamenp == NULL) {
1510602ca9eaScth 		/* See if class permits silent discard on unknown component. */
1511602ca9eaScth 		if (lut_lookup(Ereportenames_discard, (void *)class, NULL)) {
1512602ca9eaScth 			out(O_ALTFP|O_VERB2, "Unable to map \"%s\" ereport "
1513602ca9eaScth 			    "to component path, but silent discard allowed.",
1514602ca9eaScth 			    class);
1515602ca9eaScth 		} else {
1516602ca9eaScth 			/*
1517602ca9eaScth 			 * XFILE: Failure to find a component is bad unless
1518602ca9eaScth 			 * 'discard_if_config_unknown=1' was specified in the
1519602ca9eaScth 			 * ereport definition. Indicate undiagnosable.
1520602ca9eaScth 			 */
152132d4e834STarik Soydan 			Undiag_reason = UD_VAL_NOPATH;
1522602ca9eaScth 			fmcase = fmd_case_open(hdl, NULL);
1523705e9f42SStephen Hanson 
1524705e9f42SStephen Hanson 			/*
1525705e9f42SStephen Hanson 			 * We don't have a component path here (which means that
1526705e9f42SStephen Hanson 			 * the detector was not in hc-scheme and couldn't be
1527705e9f42SStephen Hanson 			 * converted to hc-scheme. Report the raw detector as
1528705e9f42SStephen Hanson 			 * the suspect resource if there is one.
1529705e9f42SStephen Hanson 			 */
1530705e9f42SStephen Hanson 			(void) nvlist_lookup_nvlist(nvl, FM_EREPORT_DETECTOR,
1531705e9f42SStephen Hanson 			    &detector);
1532705e9f42SStephen Hanson 			publish_undiagnosable(hdl, ffep, fmcase, detector,
1533705e9f42SStephen Hanson 			    (char *)class);
1534602ca9eaScth 		}
15357c478bd9Sstevel@tonic-gate 		return;
15367c478bd9Sstevel@tonic-gate 	}
15377c478bd9Sstevel@tonic-gate 
15387c478bd9Sstevel@tonic-gate 	ipp = ipath(epnamenp);
15397c478bd9Sstevel@tonic-gate 	tree_free(epnamenp);
1540602ca9eaScth 	fme_receive_report(hdl, ffep, class, ipp, nvl);
15417c478bd9Sstevel@tonic-gate }
15427c478bd9Sstevel@tonic-gate 
154308f6c065Sgavinm /*ARGSUSED*/
154408f6c065Sgavinm void
154508f6c065Sgavinm fme_receive_repair_list(fmd_hdl_t *hdl, fmd_event_t *ffep, nvlist_t *nvl,
154608f6c065Sgavinm     const char *eventstring)
154708f6c065Sgavinm {
154808f6c065Sgavinm 	char *uuid;
154908f6c065Sgavinm 	nvlist_t **nva;
155008f6c065Sgavinm 	uint_t nvc;
155108f6c065Sgavinm 	const struct ipath *ipp;
155208f6c065Sgavinm 
155308f6c065Sgavinm 	if (nvlist_lookup_string(nvl, FM_SUSPECT_UUID, &uuid) != 0 ||
155408f6c065Sgavinm 	    nvlist_lookup_nvlist_array(nvl, FM_SUSPECT_FAULT_LIST,
155508f6c065Sgavinm 	    &nva, &nvc) != 0) {
155608f6c065Sgavinm 		out(O_ALTFP, "No uuid or fault list for list.repaired event");
155708f6c065Sgavinm 		return;
155808f6c065Sgavinm 	}
155908f6c065Sgavinm 
156008f6c065Sgavinm 	out(O_ALTFP, "Processing list.repaired from case %s", uuid);
156108f6c065Sgavinm 
156208f6c065Sgavinm 	while (nvc-- != 0) {
156308f6c065Sgavinm 		/*
1564b5016cbbSstephh 		 * Reset any istat or serd engine associated with this path.
156508f6c065Sgavinm 		 */
156608f6c065Sgavinm 		char *path;
156708f6c065Sgavinm 
156808f6c065Sgavinm 		if ((ipp = platform_fault2ipath(*nva++)) == NULL)
156908f6c065Sgavinm 			continue;
157008f6c065Sgavinm 
157108f6c065Sgavinm 		path = ipath2str(NULL, ipp);
157208f6c065Sgavinm 		out(O_ALTFP, "fme_receive_repair_list: resetting state for %s",
157308f6c065Sgavinm 		    path);
157408f6c065Sgavinm 		FREE(path);
157508f6c065Sgavinm 
157608f6c065Sgavinm 		lut_walk(Istats, (lut_cb)istat_counter_reset_cb, (void *)ipp);
157708f6c065Sgavinm 		istat_save();
157808f6c065Sgavinm 
1579b5016cbbSstephh 		lut_walk(SerdEngines, (lut_cb)serd_reset_cb, (void *)ipp);
1580b5016cbbSstephh 		serd_save();
158108f6c065Sgavinm 	}
158208f6c065Sgavinm }
158308f6c065Sgavinm 
1584e5ba14ffSstephh /*ARGSUSED*/
1585e5ba14ffSstephh void
1586e5ba14ffSstephh fme_receive_topology_change(void)
1587e5ba14ffSstephh {
1588e5ba14ffSstephh 	lut_walk(Istats, (lut_cb)istat_counter_topo_chg_cb, NULL);
1589e5ba14ffSstephh 	istat_save();
1590e5ba14ffSstephh 
1591e5ba14ffSstephh 	lut_walk(SerdEngines, (lut_cb)serd_topo_chg_cb, NULL);
1592e5ba14ffSstephh 	serd_save();
1593e5ba14ffSstephh }
1594e5ba14ffSstephh 
15957aec1d6eScindi static int mark_arrows(struct fme *fmep, struct event *ep, int mark,
159600d0963fSdilpreet     unsigned long long at_latest_by, unsigned long long *pdelay, int keep);
15977aec1d6eScindi 
15987aec1d6eScindi /* ARGSUSED */
15997aec1d6eScindi static void
16007aec1d6eScindi clear_arrows(struct event *ep, struct event *ep2, struct fme *fmep)
16017aec1d6eScindi {
16027aec1d6eScindi 	struct bubble *bp;
16037aec1d6eScindi 	struct arrowlist *ap;
16047aec1d6eScindi 
16057aec1d6eScindi 	ep->cached_state = 0;
160600d0963fSdilpreet 	ep->keep_in_tree = 0;
16077aec1d6eScindi 	for (bp = itree_next_bubble(ep, NULL); bp;
16087aec1d6eScindi 	    bp = itree_next_bubble(ep, bp)) {
16097aec1d6eScindi 		if (bp->t != B_FROM)
16107aec1d6eScindi 			continue;
16117aec1d6eScindi 		bp->mark = 0;
16127aec1d6eScindi 		for (ap = itree_next_arrow(bp, NULL); ap;
16137aec1d6eScindi 		    ap = itree_next_arrow(bp, ap))
16147aec1d6eScindi 			ap->arrowp->mark = 0;
16157aec1d6eScindi 	}
16167aec1d6eScindi }
16177aec1d6eScindi 
16187c478bd9Sstevel@tonic-gate static void
16197c478bd9Sstevel@tonic-gate fme_receive_report(fmd_hdl_t *hdl, fmd_event_t *ffep,
16207c478bd9Sstevel@tonic-gate     const char *eventstring, const struct ipath *ipp, nvlist_t *nvl)
16217c478bd9Sstevel@tonic-gate {
16227c478bd9Sstevel@tonic-gate 	struct event *ep;
16237c478bd9Sstevel@tonic-gate 	struct fme *fmep = NULL;
16240cc1f05eSjrutt 	struct fme *ofmep = NULL;
16250cc1f05eSjrutt 	struct fme *cfmep, *svfmep;
16267c478bd9Sstevel@tonic-gate 	int matched = 0;
16270cc1f05eSjrutt 	nvlist_t *defect;
1628b5016cbbSstephh 	fmd_case_t *fmcase;
1629705e9f42SStephen Hanson 	char *reason;
16307c478bd9Sstevel@tonic-gate 
16317c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_NONL, "fme_receive_report: ");
16327c478bd9Sstevel@tonic-gate 	ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
16337c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_STAMP, NULL);
16347c478bd9Sstevel@tonic-gate 
16357c478bd9Sstevel@tonic-gate 	/* decide which FME it goes to */
16367c478bd9Sstevel@tonic-gate 	for (fmep = FMElist; fmep; fmep = fmep->next) {
16377c478bd9Sstevel@tonic-gate 		int prev_verbose;
16387c478bd9Sstevel@tonic-gate 		unsigned long long my_delay = TIMEVAL_EVENTUALLY;
16397c478bd9Sstevel@tonic-gate 		enum fme_state state;
16407aec1d6eScindi 		nvlist_t *pre_peek_nvp = NULL;
16417c478bd9Sstevel@tonic-gate 
16420cc1f05eSjrutt 		if (fmep->overflow) {
16430cc1f05eSjrutt 			if (!(fmd_case_closed(fmep->hdl, fmep->fmcase)))
16440cc1f05eSjrutt 				ofmep = fmep;
16450cc1f05eSjrutt 
16460cc1f05eSjrutt 			continue;
16470cc1f05eSjrutt 		}
16480cc1f05eSjrutt 
1649d96ce684Sstephh 		/*
1650d96ce684Sstephh 		 * ignore solved or closed cases
1651d96ce684Sstephh 		 */
1652d96ce684Sstephh 		if (fmep->posted_suspects ||
1653d96ce684Sstephh 		    fmd_case_solved(fmep->hdl, fmep->fmcase) ||
1654d96ce684Sstephh 		    fmd_case_closed(fmep->hdl, fmep->fmcase))
1655d96ce684Sstephh 			continue;
1656d96ce684Sstephh 
16577c478bd9Sstevel@tonic-gate 		/* look up event in event tree for this FME */
16587c478bd9Sstevel@tonic-gate 		if ((ep = itree_lookup(fmep->eventtree,
16597c478bd9Sstevel@tonic-gate 		    eventstring, ipp)) == NULL)
16607c478bd9Sstevel@tonic-gate 			continue;
16617c478bd9Sstevel@tonic-gate 
16627c478bd9Sstevel@tonic-gate 		/* note observation */
16637c478bd9Sstevel@tonic-gate 		fmep->ecurrent = ep;
16647c478bd9Sstevel@tonic-gate 		if (ep->count++ == 0) {
16657c478bd9Sstevel@tonic-gate 			/* link it into list of observations seen */
16667c478bd9Sstevel@tonic-gate 			ep->observations = fmep->observations;
16677c478bd9Sstevel@tonic-gate 			fmep->observations = ep;
16687c478bd9Sstevel@tonic-gate 			ep->nvp = evnv_dupnvl(nvl);
16697aec1d6eScindi 		} else {
16707aec1d6eScindi 			/* use new payload values for peek */
16717aec1d6eScindi 			pre_peek_nvp = ep->nvp;
16727aec1d6eScindi 			ep->nvp = evnv_dupnvl(nvl);
16737c478bd9Sstevel@tonic-gate 		}
16747c478bd9Sstevel@tonic-gate 
16757c478bd9Sstevel@tonic-gate 		/* tell hypothesise() not to mess with suspect list */
16767c478bd9Sstevel@tonic-gate 		fmep->peek = 1;
16777c478bd9Sstevel@tonic-gate 
16787c478bd9Sstevel@tonic-gate 		/* don't want this to be verbose (unless Debug is set) */
16797c478bd9Sstevel@tonic-gate 		prev_verbose = Verbose;
16807c478bd9Sstevel@tonic-gate 		if (Debug == 0)
16817c478bd9Sstevel@tonic-gate 			Verbose = 0;
16827c478bd9Sstevel@tonic-gate 
16837aec1d6eScindi 		lut_walk(fmep->eventtree, (lut_cb)clear_arrows, (void *)fmep);
16847aec1d6eScindi 		state = hypothesise(fmep, fmep->e0, fmep->ull, &my_delay);
16857c478bd9Sstevel@tonic-gate 
16867c478bd9Sstevel@tonic-gate 		fmep->peek = 0;
16877c478bd9Sstevel@tonic-gate 
16887c478bd9Sstevel@tonic-gate 		/* put verbose flag back */
16897c478bd9Sstevel@tonic-gate 		Verbose = prev_verbose;
16907c478bd9Sstevel@tonic-gate 
16917c478bd9Sstevel@tonic-gate 		if (state != FME_DISPROVED) {
16927c478bd9Sstevel@tonic-gate 			/* found an FME that explains the ereport */
16937c478bd9Sstevel@tonic-gate 			matched++;
16947c478bd9Sstevel@tonic-gate 			out(O_ALTFP|O_NONL, "[");
16957c478bd9Sstevel@tonic-gate 			ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
16967c478bd9Sstevel@tonic-gate 			out(O_ALTFP, " explained by FME%d]", fmep->id);
16977c478bd9Sstevel@tonic-gate 
16987aec1d6eScindi 			if (pre_peek_nvp)
16997aec1d6eScindi 				nvlist_free(pre_peek_nvp);
17007aec1d6eScindi 
17017c478bd9Sstevel@tonic-gate 			if (ep->count == 1)
17027c478bd9Sstevel@tonic-gate 				serialize_observation(fmep, eventstring, ipp);
17037c478bd9Sstevel@tonic-gate 
1704b7d3956bSstephh 			if (ffep) {
17057c478bd9Sstevel@tonic-gate 				fmd_case_add_ereport(hdl, fmep->fmcase, ffep);
1706b7d3956bSstephh 				ep->ffep = ffep;
1707b7d3956bSstephh 			}
17087c478bd9Sstevel@tonic-gate 
17097c478bd9Sstevel@tonic-gate 			stats_counter_bump(fmep->Rcount);
17107c478bd9Sstevel@tonic-gate 
17117c478bd9Sstevel@tonic-gate 			/* re-eval FME */
17127c478bd9Sstevel@tonic-gate 			fme_eval(fmep, ffep);
17137c478bd9Sstevel@tonic-gate 		} else {
17147c478bd9Sstevel@tonic-gate 
17157c478bd9Sstevel@tonic-gate 			/* not a match, undo noting of observation */
17167c478bd9Sstevel@tonic-gate 			fmep->ecurrent = NULL;
17177c478bd9Sstevel@tonic-gate 			if (--ep->count == 0) {
17187c478bd9Sstevel@tonic-gate 				/* unlink it from observations */
17197c478bd9Sstevel@tonic-gate 				fmep->observations = ep->observations;
17207c478bd9Sstevel@tonic-gate 				ep->observations = NULL;
17217c478bd9Sstevel@tonic-gate 				nvlist_free(ep->nvp);
17227c478bd9Sstevel@tonic-gate 				ep->nvp = NULL;
17237aec1d6eScindi 			} else {
17247aec1d6eScindi 				nvlist_free(ep->nvp);
17257aec1d6eScindi 				ep->nvp = pre_peek_nvp;
17267c478bd9Sstevel@tonic-gate 			}
17277c478bd9Sstevel@tonic-gate 		}
17287c478bd9Sstevel@tonic-gate 	}
17297c478bd9Sstevel@tonic-gate 
17307c478bd9Sstevel@tonic-gate 	if (matched)
17317c478bd9Sstevel@tonic-gate 		return;	/* explained by at least one existing FME */
17327c478bd9Sstevel@tonic-gate 
17337c478bd9Sstevel@tonic-gate 	/* clean up closed fmes */
17340cc1f05eSjrutt 	cfmep = ClosedFMEs;
17350cc1f05eSjrutt 	while (cfmep != NULL) {
17360cc1f05eSjrutt 		svfmep = cfmep->next;
17370cc1f05eSjrutt 		destroy_fme(cfmep);
17380cc1f05eSjrutt 		cfmep = svfmep;
17397c478bd9Sstevel@tonic-gate 	}
17407c478bd9Sstevel@tonic-gate 	ClosedFMEs = NULL;
17417c478bd9Sstevel@tonic-gate 
17420cc1f05eSjrutt 	if (ofmep) {
17430cc1f05eSjrutt 		out(O_ALTFP|O_NONL, "[");
17440cc1f05eSjrutt 		ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
17450cc1f05eSjrutt 		out(O_ALTFP, " ADDING TO OVERFLOW FME]");
17460cc1f05eSjrutt 		if (ffep)
17470cc1f05eSjrutt 			fmd_case_add_ereport(hdl, ofmep->fmcase, ffep);
17480cc1f05eSjrutt 
17490cc1f05eSjrutt 		return;
17500cc1f05eSjrutt 
17510cc1f05eSjrutt 	} else if (Max_fme && (Open_fme_count >= Max_fme)) {
17520cc1f05eSjrutt 		out(O_ALTFP|O_NONL, "[");
17530cc1f05eSjrutt 		ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
17540cc1f05eSjrutt 		out(O_ALTFP, " MAX OPEN FME REACHED]");
1755b5016cbbSstephh 
1756b5016cbbSstephh 		fmcase = fmd_case_open(hdl, NULL);
1757b5016cbbSstephh 
17580cc1f05eSjrutt 		/* Create overflow fme */
1759705e9f42SStephen Hanson 		if ((fmep = newfme(eventstring, ipp, hdl, fmcase, ffep,
1760705e9f42SStephen Hanson 		    nvl)) == NULL) {
17610cc1f05eSjrutt 			out(O_ALTFP|O_NONL, "[");
17620cc1f05eSjrutt 			ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
17630cc1f05eSjrutt 			out(O_ALTFP, " CANNOT OPEN OVERFLOW FME]");
17640cc1f05eSjrutt 			return;
17650cc1f05eSjrutt 		}
17660cc1f05eSjrutt 
17670cc1f05eSjrutt 		Open_fme_count++;
17680cc1f05eSjrutt 
17690cc1f05eSjrutt 		init_fme_bufs(fmep);
17700cc1f05eSjrutt 		fmep->overflow = B_TRUE;
17710cc1f05eSjrutt 
17720cc1f05eSjrutt 		if (ffep)
17730cc1f05eSjrutt 			fmd_case_add_ereport(hdl, fmep->fmcase, ffep);
17740cc1f05eSjrutt 
177532d4e834STarik Soydan 		Undiag_reason = UD_VAL_MAXFME;
177632d4e834STarik Soydan 		defect = fmd_nvl_create_fault(hdl,
177732d4e834STarik Soydan 		    undiag_2defect_str(Undiag_reason), 100, NULL, NULL, NULL);
1778705e9f42SStephen Hanson 		reason = undiag_2reason_str(Undiag_reason, NULL);
1779705e9f42SStephen Hanson 		(void) nvlist_add_string(defect, UNDIAG_REASON, reason);
1780705e9f42SStephen Hanson 		FREE(reason);
17810cc1f05eSjrutt 		fmd_case_add_suspect(hdl, fmep->fmcase, defect);
17820cc1f05eSjrutt 		fmd_case_solve(hdl, fmep->fmcase);
178332d4e834STarik Soydan 		Undiag_reason = UD_VAL_UNKNOWN;
17840cc1f05eSjrutt 		return;
17850cc1f05eSjrutt 	}
17860cc1f05eSjrutt 
1787b5016cbbSstephh 	/* open a case */
1788b5016cbbSstephh 	fmcase = fmd_case_open(hdl, NULL);
1789b5016cbbSstephh 
17907c478bd9Sstevel@tonic-gate 	/* start a new FME */
1791705e9f42SStephen Hanson 	if ((fmep = newfme(eventstring, ipp, hdl, fmcase, ffep, nvl)) == NULL) {
17927c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_NONL, "[");
17937c478bd9Sstevel@tonic-gate 		ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
17947c478bd9Sstevel@tonic-gate 		out(O_ALTFP, " CANNOT DIAGNOSE]");
17957c478bd9Sstevel@tonic-gate 		return;
17967c478bd9Sstevel@tonic-gate 	}
17977c478bd9Sstevel@tonic-gate 
17980cc1f05eSjrutt 	Open_fme_count++;
17990cc1f05eSjrutt 
18007c478bd9Sstevel@tonic-gate 	init_fme_bufs(fmep);
18017c478bd9Sstevel@tonic-gate 
18027c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_NONL, "[");
18037c478bd9Sstevel@tonic-gate 	ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
18047c478bd9Sstevel@tonic-gate 	out(O_ALTFP, " created FME%d, case %s]", fmep->id,
18057c478bd9Sstevel@tonic-gate 	    fmd_case_uuid(hdl, fmep->fmcase));
18067c478bd9Sstevel@tonic-gate 
18077c478bd9Sstevel@tonic-gate 	ep = fmep->e0;
18087c478bd9Sstevel@tonic-gate 	ASSERT(ep != NULL);
18097c478bd9Sstevel@tonic-gate 
18107c478bd9Sstevel@tonic-gate 	/* note observation */
18117c478bd9Sstevel@tonic-gate 	fmep->ecurrent = ep;
18127c478bd9Sstevel@tonic-gate 	if (ep->count++ == 0) {
18137c478bd9Sstevel@tonic-gate 		/* link it into list of observations seen */
18147c478bd9Sstevel@tonic-gate 		ep->observations = fmep->observations;
18157c478bd9Sstevel@tonic-gate 		fmep->observations = ep;
18167c478bd9Sstevel@tonic-gate 		ep->nvp = evnv_dupnvl(nvl);
18177c478bd9Sstevel@tonic-gate 		serialize_observation(fmep, eventstring, ipp);
18187aec1d6eScindi 	} else {
18197aec1d6eScindi 		/* new payload overrides any previous */
18207aec1d6eScindi 		nvlist_free(ep->nvp);
18217aec1d6eScindi 		ep->nvp = evnv_dupnvl(nvl);
18227c478bd9Sstevel@tonic-gate 	}
18237c478bd9Sstevel@tonic-gate 
18247c478bd9Sstevel@tonic-gate 	stats_counter_bump(fmep->Rcount);
18257c478bd9Sstevel@tonic-gate 
18267c478bd9Sstevel@tonic-gate 	if (ffep) {
18277c478bd9Sstevel@tonic-gate 		fmd_case_add_ereport(hdl, fmep->fmcase, ffep);
18287c478bd9Sstevel@tonic-gate 		fmd_case_setprincipal(hdl, fmep->fmcase, ffep);
18297c478bd9Sstevel@tonic-gate 		fmep->e0r = ffep;
1830b7d3956bSstephh 		ep->ffep = ffep;
18317c478bd9Sstevel@tonic-gate 	}
18327c478bd9Sstevel@tonic-gate 
18337c478bd9Sstevel@tonic-gate 	/* give the diagnosis algorithm a shot at the new FME state */
18347c478bd9Sstevel@tonic-gate 	fme_eval(fmep, ffep);
18357c478bd9Sstevel@tonic-gate }
18367c478bd9Sstevel@tonic-gate 
18377c478bd9Sstevel@tonic-gate void
18387c478bd9Sstevel@tonic-gate fme_status(int flags)
18397c478bd9Sstevel@tonic-gate {
18407c478bd9Sstevel@tonic-gate 	struct fme *fmep;
18417c478bd9Sstevel@tonic-gate 
18427c478bd9Sstevel@tonic-gate 	if (FMElist == NULL) {
18437c478bd9Sstevel@tonic-gate 		out(flags, "No fault management exercises underway.");
18447c478bd9Sstevel@tonic-gate 		return;
18457c478bd9Sstevel@tonic-gate 	}
18467c478bd9Sstevel@tonic-gate 
18477c478bd9Sstevel@tonic-gate 	for (fmep = FMElist; fmep; fmep = fmep->next)
18487c478bd9Sstevel@tonic-gate 		fme_print(flags, fmep);
18497c478bd9Sstevel@tonic-gate }
18507c478bd9Sstevel@tonic-gate 
18517c478bd9Sstevel@tonic-gate /*
18527c478bd9Sstevel@tonic-gate  * "indent" routines used mostly for nicely formatted debug output, but also
18537c478bd9Sstevel@tonic-gate  * for sanity checking for infinite recursion bugs.
18547c478bd9Sstevel@tonic-gate  */
18557c478bd9Sstevel@tonic-gate 
18567c478bd9Sstevel@tonic-gate #define	MAX_INDENT 1024
18577c478bd9Sstevel@tonic-gate static const char *indent_s[MAX_INDENT];
18587c478bd9Sstevel@tonic-gate static int current_indent;
18597c478bd9Sstevel@tonic-gate 
18607c478bd9Sstevel@tonic-gate static void
18617c478bd9Sstevel@tonic-gate indent_push(const char *s)
18627c478bd9Sstevel@tonic-gate {
18637c478bd9Sstevel@tonic-gate 	if (current_indent < MAX_INDENT)
18647c478bd9Sstevel@tonic-gate 		indent_s[current_indent++] = s;
18657c478bd9Sstevel@tonic-gate 	else
18667c478bd9Sstevel@tonic-gate 		out(O_DIE, "unexpected recursion depth (%d)", current_indent);
18677c478bd9Sstevel@tonic-gate }
18687c478bd9Sstevel@tonic-gate 
18697c478bd9Sstevel@tonic-gate static void
18707c478bd9Sstevel@tonic-gate indent_set(const char *s)
18717c478bd9Sstevel@tonic-gate {
18727c478bd9Sstevel@tonic-gate 	current_indent = 0;
18737c478bd9Sstevel@tonic-gate 	indent_push(s);
18747c478bd9Sstevel@tonic-gate }
18757c478bd9Sstevel@tonic-gate 
18767c478bd9Sstevel@tonic-gate static void
18777c478bd9Sstevel@tonic-gate indent_pop(void)
18787c478bd9Sstevel@tonic-gate {
18797c478bd9Sstevel@tonic-gate 	if (current_indent > 0)
18807c478bd9Sstevel@tonic-gate 		current_indent--;
18817c478bd9Sstevel@tonic-gate 	else
18827c478bd9Sstevel@tonic-gate 		out(O_DIE, "recursion underflow");
18837c478bd9Sstevel@tonic-gate }
18847c478bd9Sstevel@tonic-gate 
18857c478bd9Sstevel@tonic-gate static void
18867c478bd9Sstevel@tonic-gate indent(void)
18877c478bd9Sstevel@tonic-gate {
18887c478bd9Sstevel@tonic-gate 	int i;
18897c478bd9Sstevel@tonic-gate 	if (!Verbose)
18907c478bd9Sstevel@tonic-gate 		return;
18917c478bd9Sstevel@tonic-gate 	for (i = 0; i < current_indent; i++)
18927c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB|O_NONL, indent_s[i]);
18937c478bd9Sstevel@tonic-gate }
18947c478bd9Sstevel@tonic-gate 
18957c478bd9Sstevel@tonic-gate #define	SLNEW		1
18967c478bd9Sstevel@tonic-gate #define	SLCHANGED	2
18977c478bd9Sstevel@tonic-gate #define	SLWAIT		3
18987c478bd9Sstevel@tonic-gate #define	SLDISPROVED	4
18997c478bd9Sstevel@tonic-gate 
19007c478bd9Sstevel@tonic-gate static void
19017c478bd9Sstevel@tonic-gate print_suspects(int circumstance, struct fme *fmep)
19027c478bd9Sstevel@tonic-gate {
19037c478bd9Sstevel@tonic-gate 	struct event *ep;
19047c478bd9Sstevel@tonic-gate 
19057c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_NONL, "[");
19067c478bd9Sstevel@tonic-gate 	if (circumstance == SLCHANGED) {
19077c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_NONL, "FME%d diagnosis changed. state: %s, "
19087c478bd9Sstevel@tonic-gate 		    "suspect list:", fmep->id, fme_state2str(fmep->state));
19097c478bd9Sstevel@tonic-gate 	} else if (circumstance == SLWAIT) {
1910b5016cbbSstephh 		out(O_ALTFP|O_NONL, "FME%d set wait timer %ld ", fmep->id,
1911b5016cbbSstephh 		    fmep->timer);
19127c478bd9Sstevel@tonic-gate 		ptree_timeval(O_ALTFP|O_NONL, &fmep->wull);
19137c478bd9Sstevel@tonic-gate 	} else if (circumstance == SLDISPROVED) {
19147c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_NONL, "FME%d DIAGNOSIS UNKNOWN", fmep->id);
19157c478bd9Sstevel@tonic-gate 	} else {
19167c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_NONL, "FME%d DIAGNOSIS PRODUCED:", fmep->id);
19177c478bd9Sstevel@tonic-gate 	}
19187c478bd9Sstevel@tonic-gate 
19197c478bd9Sstevel@tonic-gate 	if (circumstance == SLWAIT || circumstance == SLDISPROVED) {
19207c478bd9Sstevel@tonic-gate 		out(O_ALTFP, "]");
19217c478bd9Sstevel@tonic-gate 		return;
19227c478bd9Sstevel@tonic-gate 	}
19237c478bd9Sstevel@tonic-gate 
19247c478bd9Sstevel@tonic-gate 	for (ep = fmep->suspects; ep; ep = ep->suspects) {
19257c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_NONL, " ");
19267c478bd9Sstevel@tonic-gate 		itree_pevent_brief(O_ALTFP|O_NONL, ep);
19277c478bd9Sstevel@tonic-gate 	}
19287c478bd9Sstevel@tonic-gate 	out(O_ALTFP, "]");
19297c478bd9Sstevel@tonic-gate }
19307c478bd9Sstevel@tonic-gate 
19317c478bd9Sstevel@tonic-gate static struct node *
19327c478bd9Sstevel@tonic-gate eventprop_lookup(struct event *ep, const char *propname)
19337c478bd9Sstevel@tonic-gate {
19347c478bd9Sstevel@tonic-gate 	return (lut_lookup(ep->props, (void *)propname, NULL));
19357c478bd9Sstevel@tonic-gate }
19367c478bd9Sstevel@tonic-gate 
19377c478bd9Sstevel@tonic-gate #define	MAXDIGITIDX	23
19387c478bd9Sstevel@tonic-gate static char numbuf[MAXDIGITIDX + 1];
19397c478bd9Sstevel@tonic-gate 
19407c478bd9Sstevel@tonic-gate static int
19417c478bd9Sstevel@tonic-gate node2uint(struct node *n, uint_t *valp)
19427c478bd9Sstevel@tonic-gate {
19437c478bd9Sstevel@tonic-gate 	struct evalue value;
19447c478bd9Sstevel@tonic-gate 	struct lut *globals = NULL;
19457c478bd9Sstevel@tonic-gate 
19467c478bd9Sstevel@tonic-gate 	if (n == NULL)
19477c478bd9Sstevel@tonic-gate 		return (1);
19487c478bd9Sstevel@tonic-gate 
19497c478bd9Sstevel@tonic-gate 	/*
19507c478bd9Sstevel@tonic-gate 	 * check value.v since we are being asked to convert an unsigned
19517c478bd9Sstevel@tonic-gate 	 * long long int to an unsigned int
19527c478bd9Sstevel@tonic-gate 	 */
19537c478bd9Sstevel@tonic-gate 	if (! eval_expr(n, NULL, NULL, &globals, NULL, NULL, 0, &value) ||
19547c478bd9Sstevel@tonic-gate 	    value.t != UINT64 || value.v > (1ULL << 32))
19557c478bd9Sstevel@tonic-gate 		return (1);
19567c478bd9Sstevel@tonic-gate 
19577c478bd9Sstevel@tonic-gate 	*valp = (uint_t)value.v;
19587c478bd9Sstevel@tonic-gate 
19597c478bd9Sstevel@tonic-gate 	return (0);
19607c478bd9Sstevel@tonic-gate }
19617c478bd9Sstevel@tonic-gate 
19627c478bd9Sstevel@tonic-gate static nvlist_t *
19637c478bd9Sstevel@tonic-gate node2fmri(struct node *n)
19647c478bd9Sstevel@tonic-gate {
19657c478bd9Sstevel@tonic-gate 	nvlist_t **pa, *f, *p;
19667c478bd9Sstevel@tonic-gate 	struct node *nc;
19677c478bd9Sstevel@tonic-gate 	uint_t depth = 0;
19687c478bd9Sstevel@tonic-gate 	char *numstr, *nullbyte;
19697c478bd9Sstevel@tonic-gate 	char *failure;
19707c478bd9Sstevel@tonic-gate 	int err, i;
19717c478bd9Sstevel@tonic-gate 
19727c478bd9Sstevel@tonic-gate 	/* XXX do we need to be able to handle a non-T_NAME node? */
19737c478bd9Sstevel@tonic-gate 	if (n == NULL || n->t != T_NAME)
19747c478bd9Sstevel@tonic-gate 		return (NULL);
19757c478bd9Sstevel@tonic-gate 
19767c478bd9Sstevel@tonic-gate 	for (nc = n; nc != NULL; nc = nc->u.name.next) {
19777c478bd9Sstevel@tonic-gate 		if (nc->u.name.child == NULL || nc->u.name.child->t != T_NUM)
19787c478bd9Sstevel@tonic-gate 			break;
19797c478bd9Sstevel@tonic-gate 		depth++;
19807c478bd9Sstevel@tonic-gate 	}
19817c478bd9Sstevel@tonic-gate 
19827c478bd9Sstevel@tonic-gate 	if (nc != NULL) {
19837c478bd9Sstevel@tonic-gate 		/* We bailed early, something went wrong */
19847c478bd9Sstevel@tonic-gate 		return (NULL);
19857c478bd9Sstevel@tonic-gate 	}
19867c478bd9Sstevel@tonic-gate 
19877c478bd9Sstevel@tonic-gate 	if ((err = nvlist_xalloc(&f, NV_UNIQUE_NAME, &Eft_nv_hdl)) != 0)
19887c478bd9Sstevel@tonic-gate 		out(O_DIE|O_SYS, "alloc of fmri nvl failed");
19897c478bd9Sstevel@tonic-gate 	pa = alloca(depth * sizeof (nvlist_t *));
19907c478bd9Sstevel@tonic-gate 	for (i = 0; i < depth; i++)
19917c478bd9Sstevel@tonic-gate 		pa[i] = NULL;
19927c478bd9Sstevel@tonic-gate 
19937c478bd9Sstevel@tonic-gate 	err = nvlist_add_string(f, FM_FMRI_SCHEME, FM_FMRI_SCHEME_HC);
19947c478bd9Sstevel@tonic-gate 	err |= nvlist_add_uint8(f, FM_VERSION, FM_HC_SCHEME_VERSION);
19957c478bd9Sstevel@tonic-gate 	err |= nvlist_add_string(f, FM_FMRI_HC_ROOT, "");
19967c478bd9Sstevel@tonic-gate 	err |= nvlist_add_uint32(f, FM_FMRI_HC_LIST_SZ, depth);
19977c478bd9Sstevel@tonic-gate 	if (err != 0) {
19987c478bd9Sstevel@tonic-gate 		failure = "basic construction of FMRI failed";
19997c478bd9Sstevel@tonic-gate 		goto boom;
20007c478bd9Sstevel@tonic-gate 	}
20017c478bd9Sstevel@tonic-gate 
20027c478bd9Sstevel@tonic-gate 	numbuf[MAXDIGITIDX] = '\0';
20037c478bd9Sstevel@tonic-gate 	nullbyte = &numbuf[MAXDIGITIDX];
20047c478bd9Sstevel@tonic-gate 	i = 0;
20057c478bd9Sstevel@tonic-gate 
20067c478bd9Sstevel@tonic-gate 	for (nc = n; nc != NULL; nc = nc->u.name.next) {
20077c478bd9Sstevel@tonic-gate 		err = nvlist_xalloc(&p, NV_UNIQUE_NAME, &Eft_nv_hdl);
20087c478bd9Sstevel@tonic-gate 		if (err != 0) {
20097c478bd9Sstevel@tonic-gate 			failure = "alloc of an hc-pair failed";
20107c478bd9Sstevel@tonic-gate 			goto boom;
20117c478bd9Sstevel@tonic-gate 		}
20127c478bd9Sstevel@tonic-gate 		err = nvlist_add_string(p, FM_FMRI_HC_NAME, nc->u.name.s);
20137c478bd9Sstevel@tonic-gate 		numstr = ulltostr(nc->u.name.child->u.ull, nullbyte);
20147c478bd9Sstevel@tonic-gate 		err |= nvlist_add_string(p, FM_FMRI_HC_ID, numstr);
20157c478bd9Sstevel@tonic-gate 		if (err != 0) {
20167c478bd9Sstevel@tonic-gate 			failure = "construction of an hc-pair failed";
20177c478bd9Sstevel@tonic-gate 			goto boom;
20187c478bd9Sstevel@tonic-gate 		}
20197c478bd9Sstevel@tonic-gate 		pa[i++] = p;
20207c478bd9Sstevel@tonic-gate 	}
20217c478bd9Sstevel@tonic-gate 
20227c478bd9Sstevel@tonic-gate 	err = nvlist_add_nvlist_array(f, FM_FMRI_HC_LIST, pa, depth);
20237c478bd9Sstevel@tonic-gate 	if (err == 0) {
20247c478bd9Sstevel@tonic-gate 		for (i = 0; i < depth; i++)
20257c478bd9Sstevel@tonic-gate 			if (pa[i] != NULL)
20267c478bd9Sstevel@tonic-gate 				nvlist_free(pa[i]);
20277c478bd9Sstevel@tonic-gate 		return (f);
20287c478bd9Sstevel@tonic-gate 	}
20297c478bd9Sstevel@tonic-gate 	failure = "addition of hc-pair array to FMRI failed";
20307c478bd9Sstevel@tonic-gate 
20317c478bd9Sstevel@tonic-gate boom:
20327c478bd9Sstevel@tonic-gate 	for (i = 0; i < depth; i++)
20337c478bd9Sstevel@tonic-gate 		if (pa[i] != NULL)
20347c478bd9Sstevel@tonic-gate 			nvlist_free(pa[i]);
20357c478bd9Sstevel@tonic-gate 	nvlist_free(f);
20367c478bd9Sstevel@tonic-gate 	out(O_DIE, "%s", failure);
20377c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
203880ab886dSwesolows 	return (NULL);
20397c478bd9Sstevel@tonic-gate }
20407c478bd9Sstevel@tonic-gate 
2041e5ba14ffSstephh /* an ipath cache entry is an array of these, with s==NULL at the end */
2042e5ba14ffSstephh struct ipath {
2043e5ba14ffSstephh 	const char *s;	/* component name (in stable) */
2044e5ba14ffSstephh 	int i;		/* instance number */
2045e5ba14ffSstephh };
2046e5ba14ffSstephh 
2047e5ba14ffSstephh static nvlist_t *
2048e5ba14ffSstephh ipath2fmri(struct ipath *ipath)
2049e5ba14ffSstephh {
2050e5ba14ffSstephh 	nvlist_t **pa, *f, *p;
2051e5ba14ffSstephh 	uint_t depth = 0;
2052e5ba14ffSstephh 	char *numstr, *nullbyte;
2053e5ba14ffSstephh 	char *failure;
2054e5ba14ffSstephh 	int err, i;
2055e5ba14ffSstephh 	struct ipath *ipp;
2056e5ba14ffSstephh 
2057e5ba14ffSstephh 	for (ipp = ipath; ipp->s != NULL; ipp++)
2058e5ba14ffSstephh 		depth++;
2059e5ba14ffSstephh 
2060e5ba14ffSstephh 	if ((err = nvlist_xalloc(&f, NV_UNIQUE_NAME, &Eft_nv_hdl)) != 0)
2061e5ba14ffSstephh 		out(O_DIE|O_SYS, "alloc of fmri nvl failed");
2062e5ba14ffSstephh 	pa = alloca(depth * sizeof (nvlist_t *));
2063e5ba14ffSstephh 	for (i = 0; i < depth; i++)
2064e5ba14ffSstephh 		pa[i] = NULL;
2065e5ba14ffSstephh 
2066e5ba14ffSstephh 	err = nvlist_add_string(f, FM_FMRI_SCHEME, FM_FMRI_SCHEME_HC);
2067e5ba14ffSstephh 	err |= nvlist_add_uint8(f, FM_VERSION, FM_HC_SCHEME_VERSION);
2068e5ba14ffSstephh 	err |= nvlist_add_string(f, FM_FMRI_HC_ROOT, "");
2069e5ba14ffSstephh 	err |= nvlist_add_uint32(f, FM_FMRI_HC_LIST_SZ, depth);
2070e5ba14ffSstephh 	if (err != 0) {
2071e5ba14ffSstephh 		failure = "basic construction of FMRI failed";
2072e5ba14ffSstephh 		goto boom;
2073e5ba14ffSstephh 	}
2074e5ba14ffSstephh 
2075e5ba14ffSstephh 	numbuf[MAXDIGITIDX] = '\0';
2076e5ba14ffSstephh 	nullbyte = &numbuf[MAXDIGITIDX];
2077e5ba14ffSstephh 	i = 0;
2078e5ba14ffSstephh 
2079e5ba14ffSstephh 	for (ipp = ipath; ipp->s != NULL; ipp++) {
2080e5ba14ffSstephh 		err = nvlist_xalloc(&p, NV_UNIQUE_NAME, &Eft_nv_hdl);
2081e5ba14ffSstephh 		if (err != 0) {
2082e5ba14ffSstephh 			failure = "alloc of an hc-pair failed";
2083e5ba14ffSstephh 			goto boom;
2084e5ba14ffSstephh 		}
2085e5ba14ffSstephh 		err = nvlist_add_string(p, FM_FMRI_HC_NAME, ipp->s);
2086e5ba14ffSstephh 		numstr = ulltostr(ipp->i, nullbyte);
2087e5ba14ffSstephh 		err |= nvlist_add_string(p, FM_FMRI_HC_ID, numstr);
2088e5ba14ffSstephh 		if (err != 0) {
2089e5ba14ffSstephh 			failure = "construction of an hc-pair failed";
2090e5ba14ffSstephh 			goto boom;
2091e5ba14ffSstephh 		}
2092e5ba14ffSstephh 		pa[i++] = p;
2093e5ba14ffSstephh 	}
2094e5ba14ffSstephh 
2095e5ba14ffSstephh 	err = nvlist_add_nvlist_array(f, FM_FMRI_HC_LIST, pa, depth);
2096e5ba14ffSstephh 	if (err == 0) {
2097e5ba14ffSstephh 		for (i = 0; i < depth; i++)
2098e5ba14ffSstephh 			if (pa[i] != NULL)
2099e5ba14ffSstephh 				nvlist_free(pa[i]);
2100e5ba14ffSstephh 		return (f);
2101e5ba14ffSstephh 	}
2102e5ba14ffSstephh 	failure = "addition of hc-pair array to FMRI failed";
2103e5ba14ffSstephh 
2104e5ba14ffSstephh boom:
2105e5ba14ffSstephh 	for (i = 0; i < depth; i++)
2106e5ba14ffSstephh 		if (pa[i] != NULL)
2107e5ba14ffSstephh 			nvlist_free(pa[i]);
2108e5ba14ffSstephh 	nvlist_free(f);
2109e5ba14ffSstephh 	out(O_DIE, "%s", failure);
2110e5ba14ffSstephh 	/*NOTREACHED*/
2111e5ba14ffSstephh 	return (NULL);
2112e5ba14ffSstephh }
2113e5ba14ffSstephh 
21147c478bd9Sstevel@tonic-gate static uint8_t
21157c478bd9Sstevel@tonic-gate percentof(uint_t part, uint_t whole)
21167c478bd9Sstevel@tonic-gate {
21177c478bd9Sstevel@tonic-gate 	unsigned long long p = part * 1000;
21187c478bd9Sstevel@tonic-gate 
21197c478bd9Sstevel@tonic-gate 	return ((p / whole / 10) + (((p / whole % 10) >= 5) ? 1 : 0));
21207c478bd9Sstevel@tonic-gate }
21217c478bd9Sstevel@tonic-gate 
212280ab886dSwesolows struct rsl {
21237c478bd9Sstevel@tonic-gate 	struct event *suspect;
21247c478bd9Sstevel@tonic-gate 	nvlist_t *asru;
21257c478bd9Sstevel@tonic-gate 	nvlist_t *fru;
21267c478bd9Sstevel@tonic-gate 	nvlist_t *rsrc;
21277c478bd9Sstevel@tonic-gate };
21287c478bd9Sstevel@tonic-gate 
2129b7d3956bSstephh static void publish_suspects(struct fme *fmep, struct rsl *srl);
2130b7d3956bSstephh 
21317c478bd9Sstevel@tonic-gate /*
21327c478bd9Sstevel@tonic-gate  *  rslfree -- free internal members of struct rsl not expected to be
21337c478bd9Sstevel@tonic-gate  *	freed elsewhere.
21347c478bd9Sstevel@tonic-gate  */
21357c478bd9Sstevel@tonic-gate static void
21367c478bd9Sstevel@tonic-gate rslfree(struct rsl *freeme)
21377c478bd9Sstevel@tonic-gate {
21387c478bd9Sstevel@tonic-gate 	if (freeme->asru != NULL)
21397c478bd9Sstevel@tonic-gate 		nvlist_free(freeme->asru);
21407c478bd9Sstevel@tonic-gate 	if (freeme->fru != NULL)
21417c478bd9Sstevel@tonic-gate 		nvlist_free(freeme->fru);
21427c478bd9Sstevel@tonic-gate 	if (freeme->rsrc != NULL && freeme->rsrc != freeme->asru)
21437c478bd9Sstevel@tonic-gate 		nvlist_free(freeme->rsrc);
21447c478bd9Sstevel@tonic-gate }
21457c478bd9Sstevel@tonic-gate 
21467c478bd9Sstevel@tonic-gate /*
21477c478bd9Sstevel@tonic-gate  *  rslcmp -- compare two rsl structures.  Use the following
21487c478bd9Sstevel@tonic-gate  *	comparisons to establish cardinality:
21497c478bd9Sstevel@tonic-gate  *
21507c478bd9Sstevel@tonic-gate  *	1. Name of the suspect's class. (simple strcmp)
21517c478bd9Sstevel@tonic-gate  *	2. Name of the suspect's ASRU. (trickier, since nvlist)
21527c478bd9Sstevel@tonic-gate  *
21537c478bd9Sstevel@tonic-gate  */
21547c478bd9Sstevel@tonic-gate static int
21557c478bd9Sstevel@tonic-gate rslcmp(const void *a, const void *b)
21567c478bd9Sstevel@tonic-gate {
21577c478bd9Sstevel@tonic-gate 	struct rsl *r1 = (struct rsl *)a;
21587c478bd9Sstevel@tonic-gate 	struct rsl *r2 = (struct rsl *)b;
21597c478bd9Sstevel@tonic-gate 	int rv;
21607c478bd9Sstevel@tonic-gate 
21617c478bd9Sstevel@tonic-gate 	rv = strcmp(r1->suspect->enode->u.event.ename->u.name.s,
21627c478bd9Sstevel@tonic-gate 	    r2->suspect->enode->u.event.ename->u.name.s);
21637c478bd9Sstevel@tonic-gate 	if (rv != 0)
21647c478bd9Sstevel@tonic-gate 		return (rv);
21657c478bd9Sstevel@tonic-gate 
2166b7d3956bSstephh 	if (r1->rsrc == NULL && r2->rsrc == NULL)
21677c478bd9Sstevel@tonic-gate 		return (0);
2168b7d3956bSstephh 	if (r1->rsrc == NULL)
21697c478bd9Sstevel@tonic-gate 		return (-1);
2170b7d3956bSstephh 	if (r2->rsrc == NULL)
21717c478bd9Sstevel@tonic-gate 		return (1);
2172b7d3956bSstephh 	return (evnv_cmpnvl(r1->rsrc, r2->rsrc, 0));
21737c478bd9Sstevel@tonic-gate }
21747c478bd9Sstevel@tonic-gate 
21757c478bd9Sstevel@tonic-gate /*
21767c478bd9Sstevel@tonic-gate  * get_resources -- for a given suspect, determine what ASRU, FRU and
21777c478bd9Sstevel@tonic-gate  *     RSRC nvlists should be advertised in the final suspect list.
21787c478bd9Sstevel@tonic-gate  */
21797c478bd9Sstevel@tonic-gate void
21807c478bd9Sstevel@tonic-gate get_resources(struct event *sp, struct rsl *rsrcs, struct config *croot)
21817c478bd9Sstevel@tonic-gate {
21827c478bd9Sstevel@tonic-gate 	struct node *asrudef, *frudef;
21837c478bd9Sstevel@tonic-gate 	nvlist_t *asru, *fru;
21847c478bd9Sstevel@tonic-gate 	nvlist_t *rsrc = NULL;
21857c478bd9Sstevel@tonic-gate 	char *pathstr;
21867c478bd9Sstevel@tonic-gate 
21877c478bd9Sstevel@tonic-gate 	/*
21887c478bd9Sstevel@tonic-gate 	 * First find any ASRU and/or FRU defined in the
21897c478bd9Sstevel@tonic-gate 	 * initial fault tree.
21907c478bd9Sstevel@tonic-gate 	 */
21917c478bd9Sstevel@tonic-gate 	asrudef = eventprop_lookup(sp, L_ASRU);
21927c478bd9Sstevel@tonic-gate 	frudef = eventprop_lookup(sp, L_FRU);
21937c478bd9Sstevel@tonic-gate 
21947c478bd9Sstevel@tonic-gate 	/*
21957c478bd9Sstevel@tonic-gate 	 * Create FMRIs based on those definitions
21967c478bd9Sstevel@tonic-gate 	 */
21977c478bd9Sstevel@tonic-gate 	asru = node2fmri(asrudef);
21987c478bd9Sstevel@tonic-gate 	fru = node2fmri(frudef);
21997c478bd9Sstevel@tonic-gate 	pathstr = ipath2str(NULL, sp->ipp);
22007c478bd9Sstevel@tonic-gate 
22017c478bd9Sstevel@tonic-gate 	/*
2202b7d3956bSstephh 	 *  Allow for platform translations of the FMRIs
22037c478bd9Sstevel@tonic-gate 	 */
22047c478bd9Sstevel@tonic-gate 	platform_units_translate(is_defect(sp->t), croot, &asru, &fru, &rsrc,
22057c478bd9Sstevel@tonic-gate 	    pathstr);
22067c478bd9Sstevel@tonic-gate 
22077c478bd9Sstevel@tonic-gate 	FREE(pathstr);
22087c478bd9Sstevel@tonic-gate 	rsrcs->suspect = sp;
22097c478bd9Sstevel@tonic-gate 	rsrcs->asru = asru;
22107c478bd9Sstevel@tonic-gate 	rsrcs->fru = fru;
22117c478bd9Sstevel@tonic-gate 	rsrcs->rsrc = rsrc;
22127c478bd9Sstevel@tonic-gate }
22137c478bd9Sstevel@tonic-gate 
22147c478bd9Sstevel@tonic-gate /*
22157c478bd9Sstevel@tonic-gate  * trim_suspects -- prior to publishing, we may need to remove some
22167c478bd9Sstevel@tonic-gate  *    suspects from the list.  If we're auto-closing upsets, we don't
22177c478bd9Sstevel@tonic-gate  *    want any of those in the published list.  If the ASRUs for multiple
22187c478bd9Sstevel@tonic-gate  *    defects resolve to the same ASRU (driver) we only want to publish
22197c478bd9Sstevel@tonic-gate  *    that as a single suspect.
22207c478bd9Sstevel@tonic-gate  */
2221b7d3956bSstephh static int
2222b7d3956bSstephh trim_suspects(struct fme *fmep, struct rsl *begin, struct rsl *begin2,
22238e7248e5SStephen Hanson     fmd_event_t *ffep)
22247c478bd9Sstevel@tonic-gate {
22257c478bd9Sstevel@tonic-gate 	struct event *ep;
2226b7d3956bSstephh 	struct rsl *rp = begin;
2227b7d3956bSstephh 	struct rsl *rp2 = begin2;
2228b7d3956bSstephh 	int mess_zero_count = 0;
2229b7d3956bSstephh 	int serd_rval;
2230b7d3956bSstephh 	uint_t messval;
22317c478bd9Sstevel@tonic-gate 
2232b7d3956bSstephh 	/* remove any unwanted upsets and populate our array */
22337c478bd9Sstevel@tonic-gate 	for (ep = fmep->psuspects; ep; ep = ep->psuspects) {
2234b7d3956bSstephh 		if (is_upset(ep->t))
22357c478bd9Sstevel@tonic-gate 			continue;
2236b7d3956bSstephh 		serd_rval = serd_eval(fmep, fmep->hdl, ffep, fmep->fmcase, ep,
2237b7d3956bSstephh 		    NULL, NULL);
2238b7d3956bSstephh 		if (serd_rval == 0)
2239b7d3956bSstephh 			continue;
2240b7d3956bSstephh 		if (node2uint(eventprop_lookup(ep, L_message),
2241b7d3956bSstephh 		    &messval) == 0 && messval == 0) {
2242b7d3956bSstephh 			get_resources(ep, rp2, fmep->config);
2243b7d3956bSstephh 			rp2++;
2244b7d3956bSstephh 			mess_zero_count++;
2245b7d3956bSstephh 		} else {
2246b7d3956bSstephh 			get_resources(ep, rp, fmep->config);
2247b7d3956bSstephh 			rp++;
2248b7d3956bSstephh 			fmep->nsuspects++;
2249b7d3956bSstephh 		}
22507c478bd9Sstevel@tonic-gate 	}
2251b7d3956bSstephh 	return (mess_zero_count);
22527c478bd9Sstevel@tonic-gate }
22537c478bd9Sstevel@tonic-gate 
22547aec1d6eScindi /*
22557aec1d6eScindi  * addpayloadprop -- add a payload prop to a problem
22567aec1d6eScindi  */
22577aec1d6eScindi static void
22587aec1d6eScindi addpayloadprop(const char *lhs, struct evalue *rhs, nvlist_t *fault)
22597aec1d6eScindi {
2260b7d3956bSstephh 	nvlist_t *rsrc, *hcs;
2261b7d3956bSstephh 
22627aec1d6eScindi 	ASSERT(fault != NULL);
22637aec1d6eScindi 	ASSERT(lhs != NULL);
22647aec1d6eScindi 	ASSERT(rhs != NULL);
22657aec1d6eScindi 
2266b7d3956bSstephh 	if (nvlist_lookup_nvlist(fault, FM_FAULT_RESOURCE, &rsrc) != 0)
2267b7d3956bSstephh 		out(O_DIE, "cannot add payloadprop \"%s\" to fault", lhs);
2268b7d3956bSstephh 
2269b7d3956bSstephh 	if (nvlist_lookup_nvlist(rsrc, FM_FMRI_HC_SPECIFIC, &hcs) != 0) {
2270b7d3956bSstephh 		out(O_ALTFP|O_VERB2, "addpayloadprop: create hc_specific");
2271b7d3956bSstephh 		if (nvlist_xalloc(&hcs, NV_UNIQUE_NAME, &Eft_nv_hdl) != 0)
2272b7d3956bSstephh 			out(O_DIE,
2273b7d3956bSstephh 			    "cannot add payloadprop \"%s\" to fault", lhs);
2274b7d3956bSstephh 		if (nvlist_add_nvlist(rsrc, FM_FMRI_HC_SPECIFIC, hcs) != 0)
2275b7d3956bSstephh 			out(O_DIE,
2276b7d3956bSstephh 			    "cannot add payloadprop \"%s\" to fault", lhs);
2277b7d3956bSstephh 		nvlist_free(hcs);
2278b7d3956bSstephh 		if (nvlist_lookup_nvlist(rsrc, FM_FMRI_HC_SPECIFIC, &hcs) != 0)
2279b7d3956bSstephh 			out(O_DIE,
2280b7d3956bSstephh 			    "cannot add payloadprop \"%s\" to fault", lhs);
2281b7d3956bSstephh 	} else
2282b7d3956bSstephh 		out(O_ALTFP|O_VERB2, "addpayloadprop: reuse hc_specific");
2283b7d3956bSstephh 
22847aec1d6eScindi 	if (rhs->t == UINT64) {
22857aec1d6eScindi 		out(O_ALTFP|O_VERB2, "addpayloadprop: %s=%llu", lhs, rhs->v);
22867aec1d6eScindi 
2287b7d3956bSstephh 		if (nvlist_add_uint64(hcs, lhs, rhs->v) != 0)
22887aec1d6eScindi 			out(O_DIE,
22897aec1d6eScindi 			    "cannot add payloadprop \"%s\" to fault", lhs);
22907aec1d6eScindi 	} else {
22917aec1d6eScindi 		out(O_ALTFP|O_VERB2, "addpayloadprop: %s=\"%s\"",
229280ab886dSwesolows 		    lhs, (char *)(uintptr_t)rhs->v);
22937aec1d6eScindi 
2294b7d3956bSstephh 		if (nvlist_add_string(hcs, lhs, (char *)(uintptr_t)rhs->v) != 0)
22957aec1d6eScindi 			out(O_DIE,
22967aec1d6eScindi 			    "cannot add payloadprop \"%s\" to fault", lhs);
22977aec1d6eScindi 	}
22987aec1d6eScindi }
22997aec1d6eScindi 
23007aec1d6eScindi static char *Istatbuf;
23017aec1d6eScindi static char *Istatbufptr;
23027aec1d6eScindi static int Istatsz;
23037aec1d6eScindi 
23047aec1d6eScindi /*
23057aec1d6eScindi  * istataddsize -- calculate size of istat and add it to Istatsz
23067aec1d6eScindi  */
23077aec1d6eScindi /*ARGSUSED2*/
23087aec1d6eScindi static void
23097aec1d6eScindi istataddsize(const struct istat_entry *lhs, struct stats *rhs, void *arg)
23107aec1d6eScindi {
23117aec1d6eScindi 	int val;
23127aec1d6eScindi 
23137aec1d6eScindi 	ASSERT(lhs != NULL);
23147aec1d6eScindi 	ASSERT(rhs != NULL);
23157aec1d6eScindi 
23167aec1d6eScindi 	if ((val = stats_counter_value(rhs)) == 0)
23177aec1d6eScindi 		return;	/* skip zero-valued stats */
23187aec1d6eScindi 
23197aec1d6eScindi 	/* count up the size of the stat name */
23207aec1d6eScindi 	Istatsz += ipath2strlen(lhs->ename, lhs->ipath);
23217aec1d6eScindi 	Istatsz++;	/* for the trailing NULL byte */
23227aec1d6eScindi 
23237aec1d6eScindi 	/* count up the size of the stat value */
23247aec1d6eScindi 	Istatsz += snprintf(NULL, 0, "%d", val);
23257aec1d6eScindi 	Istatsz++;	/* for the trailing NULL byte */
23267aec1d6eScindi }
23277aec1d6eScindi 
23287aec1d6eScindi /*
23297aec1d6eScindi  * istat2str -- serialize an istat, writing result to *Istatbufptr
23307aec1d6eScindi  */
23317aec1d6eScindi /*ARGSUSED2*/
23327aec1d6eScindi static void
23337aec1d6eScindi istat2str(const struct istat_entry *lhs, struct stats *rhs, void *arg)
23347aec1d6eScindi {
23357aec1d6eScindi 	char *str;
23367aec1d6eScindi 	int len;
23377aec1d6eScindi 	int val;
23387aec1d6eScindi 
23397aec1d6eScindi 	ASSERT(lhs != NULL);
23407aec1d6eScindi 	ASSERT(rhs != NULL);
23417aec1d6eScindi 
23427aec1d6eScindi 	if ((val = stats_counter_value(rhs)) == 0)
23437aec1d6eScindi 		return;	/* skip zero-valued stats */
23447aec1d6eScindi 
23457aec1d6eScindi 	/* serialize the stat name */
23467aec1d6eScindi 	str = ipath2str(lhs->ename, lhs->ipath);
23477aec1d6eScindi 	len = strlen(str);
23487aec1d6eScindi 
23497aec1d6eScindi 	ASSERT(Istatbufptr + len + 1 < &Istatbuf[Istatsz]);
23507aec1d6eScindi 	(void) strlcpy(Istatbufptr, str, &Istatbuf[Istatsz] - Istatbufptr);
23517aec1d6eScindi 	Istatbufptr += len;
23527aec1d6eScindi 	FREE(str);
23537aec1d6eScindi 	*Istatbufptr++ = '\0';
23547aec1d6eScindi 
23557aec1d6eScindi 	/* serialize the stat value */
23567aec1d6eScindi 	Istatbufptr += snprintf(Istatbufptr, &Istatbuf[Istatsz] - Istatbufptr,
23577aec1d6eScindi 	    "%d", val);
23587aec1d6eScindi 	*Istatbufptr++ = '\0';
23597aec1d6eScindi 
23607aec1d6eScindi 	ASSERT(Istatbufptr <= &Istatbuf[Istatsz]);
23617aec1d6eScindi }
23627aec1d6eScindi 
23637aec1d6eScindi void
23647aec1d6eScindi istat_save()
23657aec1d6eScindi {
23667aec1d6eScindi 	if (Istat_need_save == 0)
23677aec1d6eScindi 		return;
23687aec1d6eScindi 
23697aec1d6eScindi 	/* figure out how big the serialzed info is */
23707aec1d6eScindi 	Istatsz = 0;
23717aec1d6eScindi 	lut_walk(Istats, (lut_cb)istataddsize, NULL);
23727aec1d6eScindi 
23737aec1d6eScindi 	if (Istatsz == 0) {
23747aec1d6eScindi 		/* no stats to save */
23757aec1d6eScindi 		fmd_buf_destroy(Hdl, NULL, WOBUF_ISTATS);
23767aec1d6eScindi 		return;
23777aec1d6eScindi 	}
23787aec1d6eScindi 
23797aec1d6eScindi 	/* create the serialized buffer */
23807aec1d6eScindi 	Istatbufptr = Istatbuf = MALLOC(Istatsz);
23817aec1d6eScindi 	lut_walk(Istats, (lut_cb)istat2str, NULL);
23827aec1d6eScindi 
23837aec1d6eScindi 	/* clear out current saved stats */
23847aec1d6eScindi 	fmd_buf_destroy(Hdl, NULL, WOBUF_ISTATS);
23857aec1d6eScindi 
23867aec1d6eScindi 	/* write out the new version */
23877aec1d6eScindi 	fmd_buf_write(Hdl, NULL, WOBUF_ISTATS, Istatbuf, Istatsz);
23887aec1d6eScindi 	FREE(Istatbuf);
23897aec1d6eScindi 
23907aec1d6eScindi 	Istat_need_save = 0;
23917aec1d6eScindi }
23927aec1d6eScindi 
23937aec1d6eScindi int
23947aec1d6eScindi istat_cmp(struct istat_entry *ent1, struct istat_entry *ent2)
23957aec1d6eScindi {
23967aec1d6eScindi 	if (ent1->ename != ent2->ename)
23977aec1d6eScindi 		return (ent2->ename - ent1->ename);
23987aec1d6eScindi 	if (ent1->ipath != ent2->ipath)
23997aec1d6eScindi 		return ((char *)ent2->ipath - (char *)ent1->ipath);
24007aec1d6eScindi 
24017aec1d6eScindi 	return (0);
24027aec1d6eScindi }
24037aec1d6eScindi 
24047aec1d6eScindi /*
24057aec1d6eScindi  * istat-verify -- verify the component associated with a stat still exists
24067aec1d6eScindi  *
24077aec1d6eScindi  * if the component no longer exists, this routine resets the stat and
24087aec1d6eScindi  * returns 0.  if the component still exists, it returns 1.
24097aec1d6eScindi  */
24107aec1d6eScindi static int
24117aec1d6eScindi istat_verify(struct node *snp, struct istat_entry *entp)
24127aec1d6eScindi {
24137aec1d6eScindi 	struct stats *statp;
24147aec1d6eScindi 	nvlist_t *fmri;
24157aec1d6eScindi 
24167aec1d6eScindi 	fmri = node2fmri(snp->u.event.epname);
24177aec1d6eScindi 	if (platform_path_exists(fmri)) {
24187aec1d6eScindi 		nvlist_free(fmri);
24197aec1d6eScindi 		return (1);
24207aec1d6eScindi 	}
24217aec1d6eScindi 	nvlist_free(fmri);
24227aec1d6eScindi 
24237aec1d6eScindi 	/* component no longer in system.  zero out the associated stats */
24247aec1d6eScindi 	if ((statp = (struct stats *)
24257aec1d6eScindi 	    lut_lookup(Istats, entp, (lut_cmp)istat_cmp)) == NULL ||
24267aec1d6eScindi 	    stats_counter_value(statp) == 0)
24277aec1d6eScindi 		return (0);	/* stat is already reset */
24287aec1d6eScindi 
24297aec1d6eScindi 	Istat_need_save = 1;
24307aec1d6eScindi 	stats_counter_reset(statp);
24317aec1d6eScindi 	return (0);
24327aec1d6eScindi }
24337aec1d6eScindi 
24347aec1d6eScindi static void
24357aec1d6eScindi istat_bump(struct node *snp, int n)
24367aec1d6eScindi {
24377aec1d6eScindi 	struct stats *statp;
24387aec1d6eScindi 	struct istat_entry ent;
24397aec1d6eScindi 
24407aec1d6eScindi 	ASSERT(snp != NULL);
24417aec1d6eScindi 	ASSERTinfo(snp->t == T_EVENT, ptree_nodetype2str(snp->t));
24427aec1d6eScindi 	ASSERT(snp->u.event.epname != NULL);
24437aec1d6eScindi 
24447aec1d6eScindi 	/* class name should be hoisted into a single stable entry */
24457aec1d6eScindi 	ASSERT(snp->u.event.ename->u.name.next == NULL);
24467aec1d6eScindi 	ent.ename = snp->u.event.ename->u.name.s;
24477aec1d6eScindi 	ent.ipath = ipath(snp->u.event.epname);
24487aec1d6eScindi 
24497aec1d6eScindi 	if (!istat_verify(snp, &ent)) {
24507aec1d6eScindi 		/* component no longer exists in system, nothing to do */
24517aec1d6eScindi 		return;
24527aec1d6eScindi 	}
24537aec1d6eScindi 
24547aec1d6eScindi 	if ((statp = (struct stats *)
24557aec1d6eScindi 	    lut_lookup(Istats, &ent, (lut_cmp)istat_cmp)) == NULL) {
24567aec1d6eScindi 		/* need to create the counter */
24577aec1d6eScindi 		int cnt = 0;
24587aec1d6eScindi 		struct node *np;
24597aec1d6eScindi 		char *sname;
24607aec1d6eScindi 		char *snamep;
24617aec1d6eScindi 		struct istat_entry *newentp;
24627aec1d6eScindi 
24637aec1d6eScindi 		/* count up the size of the stat name */
24647aec1d6eScindi 		np = snp->u.event.ename;
24657aec1d6eScindi 		while (np != NULL) {
24667aec1d6eScindi 			cnt += strlen(np->u.name.s);
24677aec1d6eScindi 			cnt++;	/* for the '.' or '@' */
24687aec1d6eScindi 			np = np->u.name.next;
24697aec1d6eScindi 		}
24707aec1d6eScindi 		np = snp->u.event.epname;
24717aec1d6eScindi 		while (np != NULL) {
24727aec1d6eScindi 			cnt += snprintf(NULL, 0, "%s%llu",
24737aec1d6eScindi 			    np->u.name.s, np->u.name.child->u.ull);
24747aec1d6eScindi 			cnt++;	/* for the '/' or trailing NULL byte */
24757aec1d6eScindi 			np = np->u.name.next;
24767aec1d6eScindi 		}
24777aec1d6eScindi 
24787aec1d6eScindi 		/* build the stat name */
24797aec1d6eScindi 		snamep = sname = alloca(cnt);
24807aec1d6eScindi 		np = snp->u.event.ename;
24817aec1d6eScindi 		while (np != NULL) {
24827aec1d6eScindi 			snamep += snprintf(snamep, &sname[cnt] - snamep,
24837aec1d6eScindi 			    "%s", np->u.name.s);
24847aec1d6eScindi 			np = np->u.name.next;
24857aec1d6eScindi 			if (np)
24867aec1d6eScindi 				*snamep++ = '.';
24877aec1d6eScindi 		}
24887aec1d6eScindi 		*snamep++ = '@';
24897aec1d6eScindi 		np = snp->u.event.epname;
24907aec1d6eScindi 		while (np != NULL) {
24917aec1d6eScindi 			snamep += snprintf(snamep, &sname[cnt] - snamep,
24927aec1d6eScindi 			    "%s%llu", np->u.name.s, np->u.name.child->u.ull);
24937aec1d6eScindi 			np = np->u.name.next;
24947aec1d6eScindi 			if (np)
24957aec1d6eScindi 				*snamep++ = '/';
24967aec1d6eScindi 		}
24977aec1d6eScindi 		*snamep++ = '\0';
24987aec1d6eScindi 
24997aec1d6eScindi 		/* create the new stat & add it to our list */
25007aec1d6eScindi 		newentp = MALLOC(sizeof (*newentp));
25017aec1d6eScindi 		*newentp = ent;
25027aec1d6eScindi 		statp = stats_new_counter(NULL, sname, 0);
25037aec1d6eScindi 		Istats = lut_add(Istats, (void *)newentp, (void *)statp,
25047aec1d6eScindi 		    (lut_cmp)istat_cmp);
25057aec1d6eScindi 	}
25067aec1d6eScindi 
25077aec1d6eScindi 	/* if n is non-zero, set that value instead of bumping */
25087aec1d6eScindi 	if (n) {
25097aec1d6eScindi 		stats_counter_reset(statp);
25107aec1d6eScindi 		stats_counter_add(statp, n);
25117aec1d6eScindi 	} else
25127aec1d6eScindi 		stats_counter_bump(statp);
25137aec1d6eScindi 	Istat_need_save = 1;
25148a40a695Sgavinm 
25158a40a695Sgavinm 	ipath_print(O_ALTFP|O_VERB2, ent.ename, ent.ipath);
25168a40a695Sgavinm 	out(O_ALTFP|O_VERB2, " %s to value %d", n ? "set" : "incremented",
25178a40a695Sgavinm 	    stats_counter_value(statp));
25187aec1d6eScindi }
25197aec1d6eScindi 
25207aec1d6eScindi /*ARGSUSED*/
25217aec1d6eScindi static void
25227aec1d6eScindi istat_destructor(void *left, void *right, void *arg)
25237aec1d6eScindi {
25247aec1d6eScindi 	struct istat_entry *entp = (struct istat_entry *)left;
25257aec1d6eScindi 	struct stats *statp = (struct stats *)right;
25267aec1d6eScindi 	FREE(entp);
25277aec1d6eScindi 	stats_delete(statp);
25287aec1d6eScindi }
25297aec1d6eScindi 
253008f6c065Sgavinm /*
253108f6c065Sgavinm  * Callback used in a walk of the Istats to reset matching stat counters.
253208f6c065Sgavinm  */
253308f6c065Sgavinm static void
253408f6c065Sgavinm istat_counter_reset_cb(struct istat_entry *entp, struct stats *statp,
253508f6c065Sgavinm     const struct ipath *ipp)
253608f6c065Sgavinm {
253708f6c065Sgavinm 	char *path;
253808f6c065Sgavinm 
253908f6c065Sgavinm 	if (entp->ipath == ipp) {
254008f6c065Sgavinm 		path = ipath2str(entp->ename, ipp);
254108f6c065Sgavinm 		out(O_ALTFP, "istat_counter_reset_cb: resetting %s", path);
254208f6c065Sgavinm 		FREE(path);
254308f6c065Sgavinm 		stats_counter_reset(statp);
254408f6c065Sgavinm 		Istat_need_save = 1;
254508f6c065Sgavinm 	}
254608f6c065Sgavinm }
254708f6c065Sgavinm 
2548e5ba14ffSstephh /*ARGSUSED*/
2549e5ba14ffSstephh static void
2550e5ba14ffSstephh istat_counter_topo_chg_cb(struct istat_entry *entp, struct stats *statp,
2551e5ba14ffSstephh     void *unused)
2552e5ba14ffSstephh {
2553e5ba14ffSstephh 	char *path;
2554e5ba14ffSstephh 	nvlist_t *fmri;
2555e5ba14ffSstephh 
2556e5ba14ffSstephh 	fmri = ipath2fmri((struct ipath *)(entp->ipath));
2557e5ba14ffSstephh 	if (!platform_path_exists(fmri)) {
2558e5ba14ffSstephh 		path = ipath2str(entp->ename, entp->ipath);
2559e5ba14ffSstephh 		out(O_ALTFP, "istat_counter_topo_chg_cb: not present %s", path);
2560e5ba14ffSstephh 		FREE(path);
2561e5ba14ffSstephh 		stats_counter_reset(statp);
2562e5ba14ffSstephh 		Istat_need_save = 1;
2563e5ba14ffSstephh 	}
2564e5ba14ffSstephh 	nvlist_free(fmri);
2565e5ba14ffSstephh }
2566e5ba14ffSstephh 
25677aec1d6eScindi void
25687aec1d6eScindi istat_fini(void)
25697aec1d6eScindi {
25707aec1d6eScindi 	lut_free(Istats, istat_destructor, NULL);
25717aec1d6eScindi }
25727aec1d6eScindi 
2573b5016cbbSstephh static char *Serdbuf;
2574b5016cbbSstephh static char *Serdbufptr;
2575b5016cbbSstephh static int Serdsz;
2576b5016cbbSstephh 
2577b5016cbbSstephh /*
2578b5016cbbSstephh  * serdaddsize -- calculate size of serd and add it to Serdsz
2579b5016cbbSstephh  */
2580b5016cbbSstephh /*ARGSUSED*/
2581b5016cbbSstephh static void
2582b5016cbbSstephh serdaddsize(const struct serd_entry *lhs, struct stats *rhs, void *arg)
2583b5016cbbSstephh {
2584b5016cbbSstephh 	ASSERT(lhs != NULL);
2585b5016cbbSstephh 
2586b5016cbbSstephh 	/* count up the size of the stat name */
2587b5016cbbSstephh 	Serdsz += ipath2strlen(lhs->ename, lhs->ipath);
2588b5016cbbSstephh 	Serdsz++;	/* for the trailing NULL byte */
2589b5016cbbSstephh }
2590b5016cbbSstephh 
2591b5016cbbSstephh /*
2592b5016cbbSstephh  * serd2str -- serialize a serd engine, writing result to *Serdbufptr
2593b5016cbbSstephh  */
2594b5016cbbSstephh /*ARGSUSED*/
2595b5016cbbSstephh static void
2596b5016cbbSstephh serd2str(const struct serd_entry *lhs, struct stats *rhs, void *arg)
2597b5016cbbSstephh {
2598b5016cbbSstephh 	char *str;
2599b5016cbbSstephh 	int len;
2600b5016cbbSstephh 
2601b5016cbbSstephh 	ASSERT(lhs != NULL);
2602b5016cbbSstephh 
2603b5016cbbSstephh 	/* serialize the serd engine name */
2604b5016cbbSstephh 	str = ipath2str(lhs->ename, lhs->ipath);
2605b5016cbbSstephh 	len = strlen(str);
2606b5016cbbSstephh 
2607b5016cbbSstephh 	ASSERT(Serdbufptr + len + 1 <= &Serdbuf[Serdsz]);
2608b5016cbbSstephh 	(void) strlcpy(Serdbufptr, str, &Serdbuf[Serdsz] - Serdbufptr);
2609b5016cbbSstephh 	Serdbufptr += len;
2610b5016cbbSstephh 	FREE(str);
2611b5016cbbSstephh 	*Serdbufptr++ = '\0';
2612b5016cbbSstephh 	ASSERT(Serdbufptr <= &Serdbuf[Serdsz]);
2613b5016cbbSstephh }
2614b5016cbbSstephh 
2615b5016cbbSstephh void
2616b5016cbbSstephh serd_save()
2617b5016cbbSstephh {
2618b5016cbbSstephh 	if (Serd_need_save == 0)
2619b5016cbbSstephh 		return;
2620b5016cbbSstephh 
2621b5016cbbSstephh 	/* figure out how big the serialzed info is */
2622b5016cbbSstephh 	Serdsz = 0;
2623b5016cbbSstephh 	lut_walk(SerdEngines, (lut_cb)serdaddsize, NULL);
2624b5016cbbSstephh 
2625b5016cbbSstephh 	if (Serdsz == 0) {
2626b5016cbbSstephh 		/* no serd engines to save */
2627b5016cbbSstephh 		fmd_buf_destroy(Hdl, NULL, WOBUF_SERDS);
2628b5016cbbSstephh 		return;
2629b5016cbbSstephh 	}
2630b5016cbbSstephh 
2631b5016cbbSstephh 	/* create the serialized buffer */
2632b5016cbbSstephh 	Serdbufptr = Serdbuf = MALLOC(Serdsz);
2633b5016cbbSstephh 	lut_walk(SerdEngines, (lut_cb)serd2str, NULL);
2634b5016cbbSstephh 
2635b5016cbbSstephh 	/* clear out current saved stats */
2636b5016cbbSstephh 	fmd_buf_destroy(Hdl, NULL, WOBUF_SERDS);
2637b5016cbbSstephh 
2638b5016cbbSstephh 	/* write out the new version */
2639b5016cbbSstephh 	fmd_buf_write(Hdl, NULL, WOBUF_SERDS, Serdbuf, Serdsz);
2640b5016cbbSstephh 	FREE(Serdbuf);
2641b5016cbbSstephh 	Serd_need_save = 0;
2642b5016cbbSstephh }
2643b5016cbbSstephh 
2644b5016cbbSstephh int
2645b5016cbbSstephh serd_cmp(struct serd_entry *ent1, struct serd_entry *ent2)
2646b5016cbbSstephh {
2647b5016cbbSstephh 	if (ent1->ename != ent2->ename)
2648b5016cbbSstephh 		return (ent2->ename - ent1->ename);
2649b5016cbbSstephh 	if (ent1->ipath != ent2->ipath)
2650b5016cbbSstephh 		return ((char *)ent2->ipath - (char *)ent1->ipath);
2651b5016cbbSstephh 
2652b5016cbbSstephh 	return (0);
2653b5016cbbSstephh }
2654b5016cbbSstephh 
2655b5016cbbSstephh void
2656b5016cbbSstephh fme_serd_load(fmd_hdl_t *hdl)
2657b5016cbbSstephh {
2658b5016cbbSstephh 	int sz;
2659b5016cbbSstephh 	char *sbuf;
2660b5016cbbSstephh 	char *sepptr;
2661b5016cbbSstephh 	char *ptr;
2662b5016cbbSstephh 	struct serd_entry *newentp;
2663b5016cbbSstephh 	struct node *epname;
2664b5016cbbSstephh 	nvlist_t *fmri;
2665b5016cbbSstephh 	char *namestring;
2666b5016cbbSstephh 
2667b5016cbbSstephh 	if ((sz = fmd_buf_size(hdl, NULL, WOBUF_SERDS)) == 0)
2668b5016cbbSstephh 		return;
2669b5016cbbSstephh 	sbuf = alloca(sz);
2670b5016cbbSstephh 	fmd_buf_read(hdl, NULL, WOBUF_SERDS, sbuf, sz);
2671b5016cbbSstephh 	ptr = sbuf;
2672b5016cbbSstephh 	while (ptr < &sbuf[sz]) {
2673b5016cbbSstephh 		sepptr = strchr(ptr, '@');
2674b5016cbbSstephh 		*sepptr = '\0';
2675b5016cbbSstephh 		namestring = ptr;
2676b5016cbbSstephh 		sepptr++;
2677b5016cbbSstephh 		ptr = sepptr;
2678b5016cbbSstephh 		ptr += strlen(ptr);
2679b5016cbbSstephh 		ptr++;	/* move past the '\0' separating paths */
2680b5016cbbSstephh 		epname = pathstring2epnamenp(sepptr);
2681b5016cbbSstephh 		fmri = node2fmri(epname);
2682b5016cbbSstephh 		if (platform_path_exists(fmri)) {
2683b5016cbbSstephh 			newentp = MALLOC(sizeof (*newentp));
2684b5016cbbSstephh 			newentp->hdl = hdl;
2685b5016cbbSstephh 			newentp->ipath = ipath(epname);
2686b5016cbbSstephh 			newentp->ename = stable(namestring);
2687b5016cbbSstephh 			SerdEngines = lut_add(SerdEngines, (void *)newentp,
2688e5ba14ffSstephh 			    (void *)newentp, (lut_cmp)serd_cmp);
2689b5016cbbSstephh 		} else
2690b5016cbbSstephh 			Serd_need_save = 1;
2691e5ba14ffSstephh 		tree_free(epname);
2692b5016cbbSstephh 		nvlist_free(fmri);
2693b5016cbbSstephh 	}
2694b5016cbbSstephh 	/* save it back again in case some of the paths no longer exist */
2695b5016cbbSstephh 	serd_save();
2696b5016cbbSstephh }
2697b5016cbbSstephh 
2698b5016cbbSstephh /*ARGSUSED*/
2699b5016cbbSstephh static void
2700b5016cbbSstephh serd_destructor(void *left, void *right, void *arg)
2701b5016cbbSstephh {
2702b5016cbbSstephh 	struct serd_entry *entp = (struct serd_entry *)left;
2703b5016cbbSstephh 	FREE(entp);
2704b5016cbbSstephh }
2705b5016cbbSstephh 
2706b5016cbbSstephh /*
2707b5016cbbSstephh  * Callback used in a walk of the SerdEngines to reset matching serd engines.
2708b5016cbbSstephh  */
2709b5016cbbSstephh /*ARGSUSED*/
2710b5016cbbSstephh static void
2711b5016cbbSstephh serd_reset_cb(struct serd_entry *entp, void *unused, const struct ipath *ipp)
2712b5016cbbSstephh {
2713b5016cbbSstephh 	char *path;
2714b5016cbbSstephh 
2715b5016cbbSstephh 	if (entp->ipath == ipp) {
2716b5016cbbSstephh 		path = ipath2str(entp->ename, ipp);
2717b5016cbbSstephh 		out(O_ALTFP, "serd_reset_cb: resetting %s", path);
2718b5016cbbSstephh 		fmd_serd_reset(entp->hdl, path);
2719b5016cbbSstephh 		FREE(path);
2720b5016cbbSstephh 		Serd_need_save = 1;
2721b5016cbbSstephh 	}
2722b5016cbbSstephh }
2723b5016cbbSstephh 
2724e5ba14ffSstephh /*ARGSUSED*/
2725e5ba14ffSstephh static void
2726e5ba14ffSstephh serd_topo_chg_cb(struct serd_entry *entp, void *unused, void *unused2)
2727e5ba14ffSstephh {
2728e5ba14ffSstephh 	char *path;
2729e5ba14ffSstephh 	nvlist_t *fmri;
2730e5ba14ffSstephh 
2731e5ba14ffSstephh 	fmri = ipath2fmri((struct ipath *)(entp->ipath));
2732e5ba14ffSstephh 	if (!platform_path_exists(fmri)) {
2733e5ba14ffSstephh 		path = ipath2str(entp->ename, entp->ipath);
2734e5ba14ffSstephh 		out(O_ALTFP, "serd_topo_chg_cb: not present %s", path);
2735e5ba14ffSstephh 		fmd_serd_reset(entp->hdl, path);
2736e5ba14ffSstephh 		FREE(path);
2737e5ba14ffSstephh 		Serd_need_save = 1;
2738e5ba14ffSstephh 	}
2739e5ba14ffSstephh 	nvlist_free(fmri);
2740e5ba14ffSstephh }
2741e5ba14ffSstephh 
2742b5016cbbSstephh void
2743b5016cbbSstephh serd_fini(void)
2744b5016cbbSstephh {
2745b5016cbbSstephh 	lut_free(SerdEngines, serd_destructor, NULL);
2746b5016cbbSstephh }
2747b5016cbbSstephh 
27487c478bd9Sstevel@tonic-gate static void
2749b7d3956bSstephh publish_suspects(struct fme *fmep, struct rsl *srl)
27507c478bd9Sstevel@tonic-gate {
27517c478bd9Sstevel@tonic-gate 	struct rsl *rp;
27527c478bd9Sstevel@tonic-gate 	nvlist_t *fault;
27537c478bd9Sstevel@tonic-gate 	uint8_t cert;
27547c478bd9Sstevel@tonic-gate 	uint_t *frs;
27558e7248e5SStephen Hanson 	uint_t frsum, fr;
27567aec1d6eScindi 	uint_t messval;
2757b7d3956bSstephh 	uint_t retireval;
2758b7d3956bSstephh 	uint_t responseval;
27597aec1d6eScindi 	struct node *snp;
27607c478bd9Sstevel@tonic-gate 	int frcnt, fridx;
27617aec1d6eScindi 	boolean_t allfaulty = B_TRUE;
2762b7d3956bSstephh 	struct rsl *erl = srl + fmep->nsuspects - 1;
27637c478bd9Sstevel@tonic-gate 
27647c478bd9Sstevel@tonic-gate 	/*
2765b7d3956bSstephh 	 * sort the array
27667c478bd9Sstevel@tonic-gate 	 */
2767b7d3956bSstephh 	qsort(srl, fmep->nsuspects, sizeof (struct rsl), rslcmp);
27687c478bd9Sstevel@tonic-gate 
27698e7248e5SStephen Hanson 	/* sum the fitrates */
27708e7248e5SStephen Hanson 	frs = alloca(fmep->nsuspects * sizeof (uint_t));
27718e7248e5SStephen Hanson 	fridx = frcnt = frsum = 0;
27727c478bd9Sstevel@tonic-gate 
27738e7248e5SStephen Hanson 	for (rp = srl; rp <= erl; rp++) {
27748e7248e5SStephen Hanson 		struct node *n;
27757c478bd9Sstevel@tonic-gate 
27768e7248e5SStephen Hanson 		n = eventprop_lookup(rp->suspect, L_FITrate);
27778e7248e5SStephen Hanson 		if (node2uint(n, &fr) != 0) {
27788e7248e5SStephen Hanson 			out(O_DEBUG|O_NONL, "event ");
27798e7248e5SStephen Hanson 			ipath_print(O_DEBUG|O_NONL,
27808e7248e5SStephen Hanson 			    rp->suspect->enode->u.event.ename->u.name.s,
27818e7248e5SStephen Hanson 			    rp->suspect->ipp);
27828e7248e5SStephen Hanson 			out(O_DEBUG, " has no FITrate (using 1)");
27838e7248e5SStephen Hanson 			fr = 1;
27848e7248e5SStephen Hanson 		} else if (fr == 0) {
27858e7248e5SStephen Hanson 			out(O_DEBUG|O_NONL, "event ");
27868e7248e5SStephen Hanson 			ipath_print(O_DEBUG|O_NONL,
27878e7248e5SStephen Hanson 			    rp->suspect->enode->u.event.ename->u.name.s,
27888e7248e5SStephen Hanson 			    rp->suspect->ipp);
27898e7248e5SStephen Hanson 			out(O_DEBUG, " has zero FITrate (using 1)");
27908e7248e5SStephen Hanson 			fr = 1;
27917c478bd9Sstevel@tonic-gate 		}
27928e7248e5SStephen Hanson 
27938e7248e5SStephen Hanson 		frs[fridx++] = fr;
27948e7248e5SStephen Hanson 		frsum += fr;
27958e7248e5SStephen Hanson 		frcnt++;
27967c478bd9Sstevel@tonic-gate 	}
27977c478bd9Sstevel@tonic-gate 
27987c478bd9Sstevel@tonic-gate 	/* Add them in reverse order of our sort, as fmd reverses order */
27997c478bd9Sstevel@tonic-gate 	for (rp = erl; rp >= srl; rp--) {
28008e7248e5SStephen Hanson 		cert = percentof(frs[--fridx], frsum);
28017c478bd9Sstevel@tonic-gate 		fault = fmd_nvl_create_fault(fmep->hdl,
28027c478bd9Sstevel@tonic-gate 		    rp->suspect->enode->u.event.ename->u.name.s,
28037c478bd9Sstevel@tonic-gate 		    cert,
28047c478bd9Sstevel@tonic-gate 		    rp->asru,
28057c478bd9Sstevel@tonic-gate 		    rp->fru,
28067c478bd9Sstevel@tonic-gate 		    rp->rsrc);
28077c478bd9Sstevel@tonic-gate 		if (fault == NULL)
28087c478bd9Sstevel@tonic-gate 			out(O_DIE, "fault creation failed");
28097aec1d6eScindi 		/* if "message" property exists, add it to the fault */
28107aec1d6eScindi 		if (node2uint(eventprop_lookup(rp->suspect, L_message),
28117aec1d6eScindi 		    &messval) == 0) {
28127aec1d6eScindi 
28137aec1d6eScindi 			out(O_ALTFP,
28147aec1d6eScindi 			    "[FME%d, %s adds message=%d to suspect list]",
28157aec1d6eScindi 			    fmep->id,
28167aec1d6eScindi 			    rp->suspect->enode->u.event.ename->u.name.s,
28177aec1d6eScindi 			    messval);
28187aec1d6eScindi 			if (nvlist_add_boolean_value(fault,
28197aec1d6eScindi 			    FM_SUSPECT_MESSAGE,
28207aec1d6eScindi 			    (messval) ? B_TRUE : B_FALSE) != 0) {
28217aec1d6eScindi 				out(O_DIE, "cannot add no-message to fault");
28227aec1d6eScindi 			}
28237aec1d6eScindi 		}
2824b7d3956bSstephh 
2825b7d3956bSstephh 		/* if "retire" property exists, add it to the fault */
2826b7d3956bSstephh 		if (node2uint(eventprop_lookup(rp->suspect, L_retire),
2827b7d3956bSstephh 		    &retireval) == 0) {
2828b7d3956bSstephh 
2829b7d3956bSstephh 			out(O_ALTFP,
2830b7d3956bSstephh 			    "[FME%d, %s adds retire=%d to suspect list]",
2831b7d3956bSstephh 			    fmep->id,
2832b7d3956bSstephh 			    rp->suspect->enode->u.event.ename->u.name.s,
2833b7d3956bSstephh 			    retireval);
2834b7d3956bSstephh 			if (nvlist_add_boolean_value(fault,
2835b7d3956bSstephh 			    FM_SUSPECT_RETIRE,
2836b7d3956bSstephh 			    (retireval) ? B_TRUE : B_FALSE) != 0) {
2837b7d3956bSstephh 				out(O_DIE, "cannot add no-retire to fault");
2838b7d3956bSstephh 			}
2839b7d3956bSstephh 		}
2840b7d3956bSstephh 
2841b7d3956bSstephh 		/* if "response" property exists, add it to the fault */
2842b7d3956bSstephh 		if (node2uint(eventprop_lookup(rp->suspect, L_response),
2843b7d3956bSstephh 		    &responseval) == 0) {
2844b7d3956bSstephh 
2845b7d3956bSstephh 			out(O_ALTFP,
2846b7d3956bSstephh 			    "[FME%d, %s adds response=%d to suspect list]",
2847b7d3956bSstephh 			    fmep->id,
2848b7d3956bSstephh 			    rp->suspect->enode->u.event.ename->u.name.s,
2849b7d3956bSstephh 			    responseval);
2850b7d3956bSstephh 			if (nvlist_add_boolean_value(fault,
2851b7d3956bSstephh 			    FM_SUSPECT_RESPONSE,
2852b7d3956bSstephh 			    (responseval) ? B_TRUE : B_FALSE) != 0) {
2853b7d3956bSstephh 				out(O_DIE, "cannot add no-response to fault");
2854b7d3956bSstephh 			}
2855b7d3956bSstephh 		}
2856b7d3956bSstephh 
28577aec1d6eScindi 		/* add any payload properties */
28587aec1d6eScindi 		lut_walk(rp->suspect->payloadprops,
28597aec1d6eScindi 		    (lut_cb)addpayloadprop, (void *)fault);
28607c478bd9Sstevel@tonic-gate 		rslfree(rp);
28615f25dc2aSgavinm 
28625f25dc2aSgavinm 		/*
28635f25dc2aSgavinm 		 * If "action" property exists, evaluate it;  this must be done
2864b7d3956bSstephh 		 * before the allfaulty check below since some actions may
286525c6ff4bSstephh 		 * modify the asru to be used in fmd_nvl_fmri_has_fault.  This
28665f25dc2aSgavinm 		 * needs to be restructured if any new actions are introduced
28675f25dc2aSgavinm 		 * that have effects that we do not want to be visible if
28685f25dc2aSgavinm 		 * we decide not to publish in the dupclose check below.
28695f25dc2aSgavinm 		 */
28707aec1d6eScindi 		if ((snp = eventprop_lookup(rp->suspect, L_action)) != NULL) {
28717aec1d6eScindi 			struct evalue evalue;
28727aec1d6eScindi 
28737aec1d6eScindi 			out(O_ALTFP|O_NONL,
28747aec1d6eScindi 			    "[FME%d, %s action ", fmep->id,
28757aec1d6eScindi 			    rp->suspect->enode->u.event.ename->u.name.s);
28767aec1d6eScindi 			ptree_name_iter(O_ALTFP|O_NONL, snp);
28777aec1d6eScindi 			out(O_ALTFP, "]");
28787aec1d6eScindi 			Action_nvl = fault;
28797aec1d6eScindi 			(void) eval_expr(snp, NULL, NULL, NULL, NULL,
28807aec1d6eScindi 			    NULL, 0, &evalue);
28817aec1d6eScindi 		}
28825f25dc2aSgavinm 
2883b7d3956bSstephh 		fmd_case_add_suspect(fmep->hdl, fmep->fmcase, fault);
2884b7d3956bSstephh 
28857aec1d6eScindi 		/*
2886567cc2e6Sstephh 		 * check if the asru is already marked as "faulty".
28877aec1d6eScindi 		 */
2888567cc2e6Sstephh 		if (allfaulty) {
28897aec1d6eScindi 			nvlist_t *asru;
28907aec1d6eScindi 
2891b7d3956bSstephh 			out(O_ALTFP|O_VERB, "FME%d dup check ", fmep->id);
28927aec1d6eScindi 			itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, rp->suspect);
28937aec1d6eScindi 			out(O_ALTFP|O_VERB|O_NONL, " ");
28947aec1d6eScindi 			if (nvlist_lookup_nvlist(fault,
28957aec1d6eScindi 			    FM_FAULT_ASRU, &asru) != 0) {
28967aec1d6eScindi 				out(O_ALTFP|O_VERB, "NULL asru");
28977aec1d6eScindi 				allfaulty = B_FALSE;
289825c6ff4bSstephh 			} else if (fmd_nvl_fmri_has_fault(fmep->hdl, asru,
289925c6ff4bSstephh 			    FMD_HAS_FAULT_ASRU, NULL)) {
29007aec1d6eScindi 				out(O_ALTFP|O_VERB, "faulty");
29017aec1d6eScindi 			} else {
29027aec1d6eScindi 				out(O_ALTFP|O_VERB, "not faulty");
29037aec1d6eScindi 				allfaulty = B_FALSE;
29047aec1d6eScindi 			}
29057aec1d6eScindi 		}
29067aec1d6eScindi 
29077aec1d6eScindi 	}
29085f25dc2aSgavinm 
2909567cc2e6Sstephh 	if (!allfaulty) {
2910567cc2e6Sstephh 		/*
2911567cc2e6Sstephh 		 * don't update the count stat if all asrus are already
2912567cc2e6Sstephh 		 * present and unrepaired in the asru cache
2913567cc2e6Sstephh 		 */
29145f25dc2aSgavinm 		for (rp = erl; rp >= srl; rp--) {
29155f25dc2aSgavinm 			struct event *suspect = rp->suspect;
29165f25dc2aSgavinm 
29175f25dc2aSgavinm 			if (suspect == NULL)
29185f25dc2aSgavinm 				continue;
29195f25dc2aSgavinm 
29205f25dc2aSgavinm 			/* if "count" exists, increment the appropriate stat */
29215f25dc2aSgavinm 			if ((snp = eventprop_lookup(suspect,
29225f25dc2aSgavinm 			    L_count)) != NULL) {
29235f25dc2aSgavinm 				out(O_ALTFP|O_NONL,
29245f25dc2aSgavinm 				    "[FME%d, %s count ", fmep->id,
29255f25dc2aSgavinm 				    suspect->enode->u.event.ename->u.name.s);
29265f25dc2aSgavinm 				ptree_name_iter(O_ALTFP|O_NONL, snp);
29275f25dc2aSgavinm 				out(O_ALTFP, "]");
29285f25dc2aSgavinm 				istat_bump(snp, 0);
29295f25dc2aSgavinm 
29305f25dc2aSgavinm 			}
29315f25dc2aSgavinm 		}
29325f25dc2aSgavinm 		istat_save();	/* write out any istat changes */
29337c478bd9Sstevel@tonic-gate 	}
29347c478bd9Sstevel@tonic-gate }
29357c478bd9Sstevel@tonic-gate 
293632d4e834STarik Soydan static const char *
293732d4e834STarik Soydan undiag_2defect_str(int ud)
293832d4e834STarik Soydan {
293932d4e834STarik Soydan 	switch (ud) {
294032d4e834STarik Soydan 	case UD_VAL_MISSINGINFO:
294132d4e834STarik Soydan 	case UD_VAL_MISSINGOBS:
294232d4e834STarik Soydan 	case UD_VAL_MISSINGPATH:
294332d4e834STarik Soydan 	case UD_VAL_MISSINGZERO:
294432d4e834STarik Soydan 	case UD_VAL_BADOBS:
294532d4e834STarik Soydan 	case UD_VAL_CFGMISMATCH:
294632d4e834STarik Soydan 		return (UNDIAG_DEFECT_CHKPT);
294732d4e834STarik Soydan 		break;
294832d4e834STarik Soydan 
294932d4e834STarik Soydan 	case UD_VAL_BADEVENTI:
2950705e9f42SStephen Hanson 	case UD_VAL_BADEVENTPATH:
2951705e9f42SStephen Hanson 	case UD_VAL_BADEVENTCLASS:
295232d4e834STarik Soydan 	case UD_VAL_INSTFAIL:
295332d4e834STarik Soydan 	case UD_VAL_NOPATH:
295432d4e834STarik Soydan 	case UD_VAL_UNSOLVD:
295532d4e834STarik Soydan 		return (UNDIAG_DEFECT_FME);
295632d4e834STarik Soydan 		break;
295732d4e834STarik Soydan 
295832d4e834STarik Soydan 	case UD_VAL_MAXFME:
295932d4e834STarik Soydan 		return (UNDIAG_DEFECT_LIMIT);
296032d4e834STarik Soydan 		break;
296132d4e834STarik Soydan 
296232d4e834STarik Soydan 	case UD_VAL_UNKNOWN:
296332d4e834STarik Soydan 	default:
296432d4e834STarik Soydan 		return (UNDIAG_DEFECT_UNKNOWN);
296532d4e834STarik Soydan 		break;
296632d4e834STarik Soydan 	}
296732d4e834STarik Soydan }
296832d4e834STarik Soydan 
2969705e9f42SStephen Hanson static const char *
2970705e9f42SStephen Hanson undiag_2fault_str(int ud)
297132d4e834STarik Soydan {
297232d4e834STarik Soydan 	switch (ud) {
297332d4e834STarik Soydan 	case UD_VAL_BADEVENTI:
2974705e9f42SStephen Hanson 	case UD_VAL_BADEVENTPATH:
2975705e9f42SStephen Hanson 	case UD_VAL_BADEVENTCLASS:
2976705e9f42SStephen Hanson 	case UD_VAL_INSTFAIL:
2977705e9f42SStephen Hanson 	case UD_VAL_NOPATH:
2978705e9f42SStephen Hanson 	case UD_VAL_UNSOLVD:
2979705e9f42SStephen Hanson 		return (UNDIAG_FAULT_FME);
2980705e9f42SStephen Hanson 	default:
2981705e9f42SStephen Hanson 		return (NULL);
2982705e9f42SStephen Hanson 	}
2983705e9f42SStephen Hanson }
2984705e9f42SStephen Hanson 
2985705e9f42SStephen Hanson static char *
2986705e9f42SStephen Hanson undiag_2reason_str(int ud, char *arg)
2987705e9f42SStephen Hanson {
2988705e9f42SStephen Hanson 	const char *ptr;
2989705e9f42SStephen Hanson 	char *buf;
2990705e9f42SStephen Hanson 	int with_arg = 0;
2991705e9f42SStephen Hanson 
2992705e9f42SStephen Hanson 	switch (ud) {
2993705e9f42SStephen Hanson 	case UD_VAL_BADEVENTPATH:
2994705e9f42SStephen Hanson 		ptr = UD_STR_BADEVENTPATH;
2995705e9f42SStephen Hanson 		with_arg = 1;
2996705e9f42SStephen Hanson 		break;
2997705e9f42SStephen Hanson 	case UD_VAL_BADEVENTCLASS:
2998705e9f42SStephen Hanson 		ptr = UD_STR_BADEVENTCLASS;
2999705e9f42SStephen Hanson 		with_arg = 1;
3000705e9f42SStephen Hanson 		break;
3001705e9f42SStephen Hanson 	case UD_VAL_BADEVENTI:
3002705e9f42SStephen Hanson 		ptr = UD_STR_BADEVENTI;
3003705e9f42SStephen Hanson 		with_arg = 1;
3004705e9f42SStephen Hanson 		break;
300532d4e834STarik Soydan 	case UD_VAL_BADOBS:
3006705e9f42SStephen Hanson 		ptr = UD_STR_BADOBS;
3007705e9f42SStephen Hanson 		break;
300832d4e834STarik Soydan 	case UD_VAL_CFGMISMATCH:
3009705e9f42SStephen Hanson 		ptr = UD_STR_CFGMISMATCH;
3010705e9f42SStephen Hanson 		break;
301132d4e834STarik Soydan 	case UD_VAL_INSTFAIL:
3012705e9f42SStephen Hanson 		ptr = UD_STR_INSTFAIL;
3013705e9f42SStephen Hanson 		with_arg = 1;
3014705e9f42SStephen Hanson 		break;
301532d4e834STarik Soydan 	case UD_VAL_MAXFME:
3016705e9f42SStephen Hanson 		ptr = UD_STR_MAXFME;
3017705e9f42SStephen Hanson 		break;
301832d4e834STarik Soydan 	case UD_VAL_MISSINGINFO:
3019705e9f42SStephen Hanson 		ptr = UD_STR_MISSINGINFO;
3020705e9f42SStephen Hanson 		break;
302132d4e834STarik Soydan 	case UD_VAL_MISSINGOBS:
3022705e9f42SStephen Hanson 		ptr = UD_STR_MISSINGOBS;
3023705e9f42SStephen Hanson 		break;
302432d4e834STarik Soydan 	case UD_VAL_MISSINGPATH:
3025705e9f42SStephen Hanson 		ptr = UD_STR_MISSINGPATH;
3026705e9f42SStephen Hanson 		break;
302732d4e834STarik Soydan 	case UD_VAL_MISSINGZERO:
3028705e9f42SStephen Hanson 		ptr = UD_STR_MISSINGZERO;
3029705e9f42SStephen Hanson 		break;
303032d4e834STarik Soydan 	case UD_VAL_NOPATH:
3031705e9f42SStephen Hanson 		ptr = UD_STR_NOPATH;
3032705e9f42SStephen Hanson 		with_arg = 1;
3033705e9f42SStephen Hanson 		break;
303432d4e834STarik Soydan 	case UD_VAL_UNSOLVD:
3035705e9f42SStephen Hanson 		ptr = UD_STR_UNSOLVD;
3036705e9f42SStephen Hanson 		break;
303732d4e834STarik Soydan 	case UD_VAL_UNKNOWN:
303832d4e834STarik Soydan 	default:
3039705e9f42SStephen Hanson 		ptr = UD_STR_UNKNOWN;
3040705e9f42SStephen Hanson 		break;
304132d4e834STarik Soydan 	}
3042705e9f42SStephen Hanson 	if (with_arg) {
3043705e9f42SStephen Hanson 		buf = MALLOC(strlen(ptr) + strlen(arg) - 1);
3044705e9f42SStephen Hanson 		(void) sprintf(buf, ptr, arg);
3045705e9f42SStephen Hanson 	} else {
3046705e9f42SStephen Hanson 		buf = MALLOC(strlen(ptr) + 1);
3047705e9f42SStephen Hanson 		(void) sprintf(buf, ptr);
3048705e9f42SStephen Hanson 	}
3049705e9f42SStephen Hanson 	return (buf);
305032d4e834STarik Soydan }
305132d4e834STarik Soydan 
30527c478bd9Sstevel@tonic-gate static void
3053705e9f42SStephen Hanson publish_undiagnosable(fmd_hdl_t *hdl, fmd_event_t *ffep, fmd_case_t *fmcase,
3054705e9f42SStephen Hanson     nvlist_t *detector, char *arg)
30557c478bd9Sstevel@tonic-gate {
30567c478bd9Sstevel@tonic-gate 	struct case_list *newcase;
3057705e9f42SStephen Hanson 	nvlist_t *defect, *fault;
3058705e9f42SStephen Hanson 	const char *faultstr;
3059705e9f42SStephen Hanson 	char *reason = undiag_2reason_str(Undiag_reason, arg);
30607c478bd9Sstevel@tonic-gate 
30617c478bd9Sstevel@tonic-gate 	out(O_ALTFP,
30627c478bd9Sstevel@tonic-gate 	    "[undiagnosable ereport received, "
3063705e9f42SStephen Hanson 	    "creating and closing a new case (%s)]", reason);
30647c478bd9Sstevel@tonic-gate 
30657c478bd9Sstevel@tonic-gate 	newcase = MALLOC(sizeof (struct case_list));
30667c478bd9Sstevel@tonic-gate 	newcase->next = NULL;
3067b5016cbbSstephh 	newcase->fmcase = fmcase;
30687c478bd9Sstevel@tonic-gate 	if (Undiagablecaselist != NULL)
30697c478bd9Sstevel@tonic-gate 		newcase->next = Undiagablecaselist;
30707c478bd9Sstevel@tonic-gate 	Undiagablecaselist = newcase;
30717c478bd9Sstevel@tonic-gate 
30727c478bd9Sstevel@tonic-gate 	if (ffep != NULL)
30737c478bd9Sstevel@tonic-gate 		fmd_case_add_ereport(hdl, newcase->fmcase, ffep);
30747c478bd9Sstevel@tonic-gate 
3075705e9f42SStephen Hanson 	/* add defect */
307632d4e834STarik Soydan 	defect = fmd_nvl_create_fault(hdl,
3077705e9f42SStephen Hanson 	    undiag_2defect_str(Undiag_reason), 50, NULL, NULL, detector);
3078705e9f42SStephen Hanson 	(void) nvlist_add_string(defect, UNDIAG_REASON, reason);
3079705e9f42SStephen Hanson 	(void) nvlist_add_boolean_value(defect, FM_SUSPECT_RETIRE, B_FALSE);
3080705e9f42SStephen Hanson 	(void) nvlist_add_boolean_value(defect, FM_SUSPECT_RESPONSE, B_FALSE);
30817c478bd9Sstevel@tonic-gate 	fmd_case_add_suspect(hdl, newcase->fmcase, defect);
30827c478bd9Sstevel@tonic-gate 
3083705e9f42SStephen Hanson 	/* add fault if appropriate */
3084705e9f42SStephen Hanson 	faultstr = undiag_2fault_str(Undiag_reason);
3085705e9f42SStephen Hanson 	if (faultstr != NULL) {
3086705e9f42SStephen Hanson 		fault = fmd_nvl_create_fault(hdl, faultstr, 50, NULL, NULL,
3087705e9f42SStephen Hanson 		    detector);
3088705e9f42SStephen Hanson 		(void) nvlist_add_string(fault, UNDIAG_REASON, reason);
3089705e9f42SStephen Hanson 		(void) nvlist_add_boolean_value(fault, FM_SUSPECT_RETIRE,
3090705e9f42SStephen Hanson 		    B_FALSE);
3091705e9f42SStephen Hanson 		(void) nvlist_add_boolean_value(fault, FM_SUSPECT_RESPONSE,
3092705e9f42SStephen Hanson 		    B_FALSE);
3093705e9f42SStephen Hanson 		fmd_case_add_suspect(hdl, newcase->fmcase, fault);
3094705e9f42SStephen Hanson 	}
3095705e9f42SStephen Hanson 	FREE(reason);
3096705e9f42SStephen Hanson 
3097705e9f42SStephen Hanson 	/* solve and close case */
30987c478bd9Sstevel@tonic-gate 	fmd_case_solve(hdl, newcase->fmcase);
30997c478bd9Sstevel@tonic-gate 	fmd_case_close(hdl, newcase->fmcase);
310032d4e834STarik Soydan 	Undiag_reason = UD_VAL_UNKNOWN;
31017c478bd9Sstevel@tonic-gate }
31027c478bd9Sstevel@tonic-gate 
31037c478bd9Sstevel@tonic-gate static void
31047c478bd9Sstevel@tonic-gate fme_undiagnosable(struct fme *f)
31057c478bd9Sstevel@tonic-gate {
3106705e9f42SStephen Hanson 	nvlist_t *defect, *fault, *detector = NULL;
3107705e9f42SStephen Hanson 	struct event *ep;
3108705e9f42SStephen Hanson 	char *pathstr;
3109705e9f42SStephen Hanson 	const char *faultstr;
3110705e9f42SStephen Hanson 	char *reason = undiag_2reason_str(Undiag_reason, NULL);
31117c478bd9Sstevel@tonic-gate 
31127c478bd9Sstevel@tonic-gate 	out(O_ALTFP, "[solving/closing FME%d, case %s (%s)]",
3113705e9f42SStephen Hanson 	    f->id, fmd_case_uuid(f->hdl, f->fmcase), reason);
3114705e9f42SStephen Hanson 
3115705e9f42SStephen Hanson 	for (ep = f->observations; ep; ep = ep->observations) {
3116705e9f42SStephen Hanson 
3117705e9f42SStephen Hanson 		if (ep->ffep != f->e0r)
3118705e9f42SStephen Hanson 			fmd_case_add_ereport(f->hdl, f->fmcase, ep->ffep);
3119705e9f42SStephen Hanson 
3120705e9f42SStephen Hanson 		pathstr = ipath2str(NULL, ipath(platform_getpath(ep->nvp)));
3121705e9f42SStephen Hanson 		platform_units_translate(0, f->config, NULL, NULL, &detector,
3122705e9f42SStephen Hanson 		    pathstr);
3123705e9f42SStephen Hanson 		FREE(pathstr);
3124705e9f42SStephen Hanson 
3125705e9f42SStephen Hanson 		/* add defect */
3126705e9f42SStephen Hanson 		defect = fmd_nvl_create_fault(f->hdl,
3127705e9f42SStephen Hanson 		    undiag_2defect_str(Undiag_reason), 50 / f->uniqobs,
3128705e9f42SStephen Hanson 		    NULL, NULL, detector);
3129705e9f42SStephen Hanson 		(void) nvlist_add_string(defect, UNDIAG_REASON, reason);
3130705e9f42SStephen Hanson 		(void) nvlist_add_boolean_value(defect, FM_SUSPECT_RETIRE,
3131705e9f42SStephen Hanson 		    B_FALSE);
3132705e9f42SStephen Hanson 		(void) nvlist_add_boolean_value(defect, FM_SUSPECT_RESPONSE,
3133705e9f42SStephen Hanson 		    B_FALSE);
3134705e9f42SStephen Hanson 		fmd_case_add_suspect(f->hdl, f->fmcase, defect);
3135705e9f42SStephen Hanson 
3136705e9f42SStephen Hanson 		/* add fault if appropriate */
3137705e9f42SStephen Hanson 		faultstr = undiag_2fault_str(Undiag_reason);
3138705e9f42SStephen Hanson 		if (faultstr == NULL)
3139705e9f42SStephen Hanson 			continue;
3140705e9f42SStephen Hanson 		fault = fmd_nvl_create_fault(f->hdl, faultstr, 50 / f->uniqobs,
3141705e9f42SStephen Hanson 		    NULL, NULL, detector);
3142705e9f42SStephen Hanson 		(void) nvlist_add_string(fault, UNDIAG_REASON, reason);
3143705e9f42SStephen Hanson 		(void) nvlist_add_boolean_value(fault, FM_SUSPECT_RETIRE,
3144705e9f42SStephen Hanson 		    B_FALSE);
3145705e9f42SStephen Hanson 		(void) nvlist_add_boolean_value(fault, FM_SUSPECT_RESPONSE,
3146705e9f42SStephen Hanson 		    B_FALSE);
3147705e9f42SStephen Hanson 		fmd_case_add_suspect(f->hdl, f->fmcase, fault);
3148705e9f42SStephen Hanson 		nvlist_free(detector);
3149705e9f42SStephen Hanson 	}
3150705e9f42SStephen Hanson 	FREE(reason);
31517c478bd9Sstevel@tonic-gate 	fmd_case_solve(f->hdl, f->fmcase);
31527c478bd9Sstevel@tonic-gate 	fmd_case_close(f->hdl, f->fmcase);
315332d4e834STarik Soydan 	Undiag_reason = UD_VAL_UNKNOWN;
31547c478bd9Sstevel@tonic-gate }
31557c478bd9Sstevel@tonic-gate 
31567c478bd9Sstevel@tonic-gate /*
31577c478bd9Sstevel@tonic-gate  * fme_close_case
31587c478bd9Sstevel@tonic-gate  *
31597c478bd9Sstevel@tonic-gate  *	Find the requested case amongst our fmes and close it.  Free up
31607c478bd9Sstevel@tonic-gate  *	the related fme.
31617c478bd9Sstevel@tonic-gate  */
31627c478bd9Sstevel@tonic-gate void
31637c478bd9Sstevel@tonic-gate fme_close_case(fmd_hdl_t *hdl, fmd_case_t *fmcase)
31647c478bd9Sstevel@tonic-gate {
31657c478bd9Sstevel@tonic-gate 	struct case_list *ucasep, *prevcasep = NULL;
31667c478bd9Sstevel@tonic-gate 	struct fme *prev = NULL;
31677c478bd9Sstevel@tonic-gate 	struct fme *fmep;
31687c478bd9Sstevel@tonic-gate 
31697c478bd9Sstevel@tonic-gate 	for (ucasep = Undiagablecaselist; ucasep; ucasep = ucasep->next) {
31707c478bd9Sstevel@tonic-gate 		if (fmcase != ucasep->fmcase) {
31717c478bd9Sstevel@tonic-gate 			prevcasep = ucasep;
31727c478bd9Sstevel@tonic-gate 			continue;
31737c478bd9Sstevel@tonic-gate 		}
31747c478bd9Sstevel@tonic-gate 
31757c478bd9Sstevel@tonic-gate 		if (prevcasep == NULL)
31767c478bd9Sstevel@tonic-gate 			Undiagablecaselist = Undiagablecaselist->next;
31777c478bd9Sstevel@tonic-gate 		else
31787c478bd9Sstevel@tonic-gate 			prevcasep->next = ucasep->next;
31797c478bd9Sstevel@tonic-gate 
31807c478bd9Sstevel@tonic-gate 		FREE(ucasep);
31817c478bd9Sstevel@tonic-gate 		return;
31827c478bd9Sstevel@tonic-gate 	}
31837c478bd9Sstevel@tonic-gate 
31847c478bd9Sstevel@tonic-gate 	for (fmep = FMElist; fmep; fmep = fmep->next) {
31857c478bd9Sstevel@tonic-gate 		if (fmep->hdl == hdl && fmep->fmcase == fmcase)
31867c478bd9Sstevel@tonic-gate 			break;
31877c478bd9Sstevel@tonic-gate 		prev = fmep;
31887c478bd9Sstevel@tonic-gate 	}
31897c478bd9Sstevel@tonic-gate 
31907c478bd9Sstevel@tonic-gate 	if (fmep == NULL) {
31917c478bd9Sstevel@tonic-gate 		out(O_WARN, "Eft asked to close unrecognized case [%s].",
31927c478bd9Sstevel@tonic-gate 		    fmd_case_uuid(hdl, fmcase));
31937c478bd9Sstevel@tonic-gate 		return;
31947c478bd9Sstevel@tonic-gate 	}
31957c478bd9Sstevel@tonic-gate 
31967c478bd9Sstevel@tonic-gate 	if (EFMElist == fmep)
31977c478bd9Sstevel@tonic-gate 		EFMElist = prev;
31987c478bd9Sstevel@tonic-gate 
31997c478bd9Sstevel@tonic-gate 	if (prev == NULL)
32007c478bd9Sstevel@tonic-gate 		FMElist = FMElist->next;
32017c478bd9Sstevel@tonic-gate 	else
32027c478bd9Sstevel@tonic-gate 		prev->next = fmep->next;
32037c478bd9Sstevel@tonic-gate 
32047c478bd9Sstevel@tonic-gate 	fmep->next = NULL;
32057c478bd9Sstevel@tonic-gate 
32067c478bd9Sstevel@tonic-gate 	/* Get rid of any timer this fme has set */
32077c478bd9Sstevel@tonic-gate 	if (fmep->wull != 0)
32087c478bd9Sstevel@tonic-gate 		fmd_timer_remove(fmep->hdl, fmep->timer);
32097c478bd9Sstevel@tonic-gate 
32107c478bd9Sstevel@tonic-gate 	if (ClosedFMEs == NULL) {
32117c478bd9Sstevel@tonic-gate 		ClosedFMEs = fmep;
32127c478bd9Sstevel@tonic-gate 	} else {
32137c478bd9Sstevel@tonic-gate 		fmep->next = ClosedFMEs;
32147c478bd9Sstevel@tonic-gate 		ClosedFMEs = fmep;
32157c478bd9Sstevel@tonic-gate 	}
32160cc1f05eSjrutt 
32170cc1f05eSjrutt 	Open_fme_count--;
32180cc1f05eSjrutt 
32190cc1f05eSjrutt 	/* See if we can close the overflow FME */
32200cc1f05eSjrutt 	if (Open_fme_count <= Max_fme) {
32210cc1f05eSjrutt 		for (fmep = FMElist; fmep; fmep = fmep->next) {
32220cc1f05eSjrutt 			if (fmep->overflow && !(fmd_case_closed(fmep->hdl,
32230cc1f05eSjrutt 			    fmep->fmcase)))
32240cc1f05eSjrutt 				break;
32250cc1f05eSjrutt 		}
32260cc1f05eSjrutt 
32270cc1f05eSjrutt 		if (fmep != NULL)
32280cc1f05eSjrutt 			fmd_case_close(fmep->hdl, fmep->fmcase);
32290cc1f05eSjrutt 	}
32307c478bd9Sstevel@tonic-gate }
32317c478bd9Sstevel@tonic-gate 
32327c478bd9Sstevel@tonic-gate /*
32337c478bd9Sstevel@tonic-gate  * fme_set_timer()
32347c478bd9Sstevel@tonic-gate  *	If the time we need to wait for the given FME is less than the
32357c478bd9Sstevel@tonic-gate  *	current timer, kick that old timer out and establish a new one.
32367c478bd9Sstevel@tonic-gate  */
32377aec1d6eScindi static int
32387c478bd9Sstevel@tonic-gate fme_set_timer(struct fme *fmep, unsigned long long wull)
32397c478bd9Sstevel@tonic-gate {
32407c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB|O_NONL, " fme_set_timer: request to wait ");
32417c478bd9Sstevel@tonic-gate 	ptree_timeval(O_ALTFP|O_VERB, &wull);
32427c478bd9Sstevel@tonic-gate 
32437c478bd9Sstevel@tonic-gate 	if (wull <= fmep->pull) {
32447c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB|O_NONL, "already have waited at least ");
32457c478bd9Sstevel@tonic-gate 		ptree_timeval(O_ALTFP|O_VERB, &fmep->pull);
32467c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB, NULL);
32477c478bd9Sstevel@tonic-gate 		/* we've waited at least wull already, don't need timer */
32487aec1d6eScindi 		return (0);
32497c478bd9Sstevel@tonic-gate 	}
32507c478bd9Sstevel@tonic-gate 
32517c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB|O_NONL, " currently ");
32527c478bd9Sstevel@tonic-gate 	if (fmep->wull != 0) {
32537c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB|O_NONL, "waiting ");
32547c478bd9Sstevel@tonic-gate 		ptree_timeval(O_ALTFP|O_VERB, &fmep->wull);
32557c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB, NULL);
32567c478bd9Sstevel@tonic-gate 	} else {
32577c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB|O_NONL, "not waiting");
32587c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB, NULL);
32597c478bd9Sstevel@tonic-gate 	}
32607c478bd9Sstevel@tonic-gate 
32617c478bd9Sstevel@tonic-gate 	if (fmep->wull != 0)
32627c478bd9Sstevel@tonic-gate 		if (wull >= fmep->wull)
32637c478bd9Sstevel@tonic-gate 			/* New timer would fire later than established timer */
32647aec1d6eScindi 			return (0);
32657c478bd9Sstevel@tonic-gate 
32667aec1d6eScindi 	if (fmep->wull != 0) {
32677c478bd9Sstevel@tonic-gate 		fmd_timer_remove(fmep->hdl, fmep->timer);
32687aec1d6eScindi 	}
32697c478bd9Sstevel@tonic-gate 
32707c478bd9Sstevel@tonic-gate 	fmep->timer = fmd_timer_install(fmep->hdl, (void *)fmep,
32717c478bd9Sstevel@tonic-gate 	    fmep->e0r, wull);
32727c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB, "timer set, id is %ld", fmep->timer);
32737c478bd9Sstevel@tonic-gate 	fmep->wull = wull;
32747aec1d6eScindi 	return (1);
32757c478bd9Sstevel@tonic-gate }
32767c478bd9Sstevel@tonic-gate 
32777c478bd9Sstevel@tonic-gate void
32787c478bd9Sstevel@tonic-gate fme_timer_fired(struct fme *fmep, id_t tid)
32797c478bd9Sstevel@tonic-gate {
32807c478bd9Sstevel@tonic-gate 	struct fme *ffmep = NULL;
32817c478bd9Sstevel@tonic-gate 
32827c478bd9Sstevel@tonic-gate 	for (ffmep = FMElist; ffmep; ffmep = ffmep->next)
32837c478bd9Sstevel@tonic-gate 		if (ffmep == fmep)
32847c478bd9Sstevel@tonic-gate 			break;
32857c478bd9Sstevel@tonic-gate 
32867c478bd9Sstevel@tonic-gate 	if (ffmep == NULL) {
32877c478bd9Sstevel@tonic-gate 		out(O_WARN, "Timer fired for an FME (%p) not in FMEs list.",
32887c478bd9Sstevel@tonic-gate 		    (void *)fmep);
32897c478bd9Sstevel@tonic-gate 		return;
32907c478bd9Sstevel@tonic-gate 	}
32917c478bd9Sstevel@tonic-gate 
3292b5016cbbSstephh 	out(O_ALTFP|O_VERB, "Timer fired %lx", tid);
3293d96ce684Sstephh 	fmep->pull = fmep->wull;
3294d96ce684Sstephh 	fmep->wull = 0;
3295d96ce684Sstephh 	fmd_buf_write(fmep->hdl, fmep->fmcase,
3296d96ce684Sstephh 	    WOBUF_PULL, (void *)&fmep->pull, sizeof (fmep->pull));
3297b5016cbbSstephh 
329800d0963fSdilpreet 	fme_eval(fmep, fmep->e0r);
32997c478bd9Sstevel@tonic-gate }
33007c478bd9Sstevel@tonic-gate 
33017c478bd9Sstevel@tonic-gate /*
33027c478bd9Sstevel@tonic-gate  * Preserve the fme's suspect list in its psuspects list, NULLing the
33037c478bd9Sstevel@tonic-gate  * suspects list in the meantime.
33047c478bd9Sstevel@tonic-gate  */
33057c478bd9Sstevel@tonic-gate static void
33067c478bd9Sstevel@tonic-gate save_suspects(struct fme *fmep)
33077c478bd9Sstevel@tonic-gate {
33087c478bd9Sstevel@tonic-gate 	struct event *ep;
33097c478bd9Sstevel@tonic-gate 	struct event *nextep;
33107c478bd9Sstevel@tonic-gate 
33117c478bd9Sstevel@tonic-gate 	/* zero out the previous suspect list */
33127c478bd9Sstevel@tonic-gate 	for (ep = fmep->psuspects; ep; ep = nextep) {
33137c478bd9Sstevel@tonic-gate 		nextep = ep->psuspects;
33147c478bd9Sstevel@tonic-gate 		ep->psuspects = NULL;
33157c478bd9Sstevel@tonic-gate 	}
33167c478bd9Sstevel@tonic-gate 	fmep->psuspects = NULL;
33177c478bd9Sstevel@tonic-gate 
33187c478bd9Sstevel@tonic-gate 	/* zero out the suspect list, copying it to previous suspect list */
33197c478bd9Sstevel@tonic-gate 	fmep->psuspects = fmep->suspects;
33207c478bd9Sstevel@tonic-gate 	for (ep = fmep->suspects; ep; ep = nextep) {
33217c478bd9Sstevel@tonic-gate 		nextep = ep->suspects;
33227c478bd9Sstevel@tonic-gate 		ep->psuspects = ep->suspects;
33237c478bd9Sstevel@tonic-gate 		ep->suspects = NULL;
33247c478bd9Sstevel@tonic-gate 		ep->is_suspect = 0;
33257c478bd9Sstevel@tonic-gate 	}
33267c478bd9Sstevel@tonic-gate 	fmep->suspects = NULL;
33277c478bd9Sstevel@tonic-gate 	fmep->nsuspects = 0;
33287c478bd9Sstevel@tonic-gate }
33297c478bd9Sstevel@tonic-gate 
33307c478bd9Sstevel@tonic-gate /*
33317c478bd9Sstevel@tonic-gate  * Retrieve the fme's suspect list from its psuspects list.
33327c478bd9Sstevel@tonic-gate  */
33337c478bd9Sstevel@tonic-gate static void
33347c478bd9Sstevel@tonic-gate restore_suspects(struct fme *fmep)
33357c478bd9Sstevel@tonic-gate {
33367c478bd9Sstevel@tonic-gate 	struct event *ep;
33377c478bd9Sstevel@tonic-gate 	struct event *nextep;
33387c478bd9Sstevel@tonic-gate 
33398e7248e5SStephen Hanson 	fmep->nsuspects = 0;
33407c478bd9Sstevel@tonic-gate 	fmep->suspects = fmep->psuspects;
33417c478bd9Sstevel@tonic-gate 	for (ep = fmep->psuspects; ep; ep = nextep) {
33427c478bd9Sstevel@tonic-gate 		fmep->nsuspects++;
33437c478bd9Sstevel@tonic-gate 		nextep = ep->psuspects;
33447c478bd9Sstevel@tonic-gate 		ep->suspects = ep->psuspects;
33457c478bd9Sstevel@tonic-gate 	}
33467c478bd9Sstevel@tonic-gate }
33477c478bd9Sstevel@tonic-gate 
33487c478bd9Sstevel@tonic-gate /*
33497c478bd9Sstevel@tonic-gate  * this is what we use to call the Emrys prototype code instead of main()
33507c478bd9Sstevel@tonic-gate  */
33517c478bd9Sstevel@tonic-gate static void
33527c478bd9Sstevel@tonic-gate fme_eval(struct fme *fmep, fmd_event_t *ffep)
33537c478bd9Sstevel@tonic-gate {
33547c478bd9Sstevel@tonic-gate 	struct event *ep;
33557c478bd9Sstevel@tonic-gate 	unsigned long long my_delay = TIMEVAL_EVENTUALLY;
3356b7d3956bSstephh 	struct rsl *srl = NULL;
3357b7d3956bSstephh 	struct rsl *srl2 = NULL;
3358b7d3956bSstephh 	int mess_zero_count;
3359b7d3956bSstephh 	int rpcnt;
33607c478bd9Sstevel@tonic-gate 
33617c478bd9Sstevel@tonic-gate 	save_suspects(fmep);
33627c478bd9Sstevel@tonic-gate 
3363b5016cbbSstephh 	out(O_ALTFP, "Evaluate FME %d", fmep->id);
33647c478bd9Sstevel@tonic-gate 	indent_set("  ");
33657c478bd9Sstevel@tonic-gate 
33667aec1d6eScindi 	lut_walk(fmep->eventtree, (lut_cb)clear_arrows, (void *)fmep);
33677aec1d6eScindi 	fmep->state = hypothesise(fmep, fmep->e0, fmep->ull, &my_delay);
33687c478bd9Sstevel@tonic-gate 
3369b5016cbbSstephh 	out(O_ALTFP|O_NONL, "FME%d state: %s, suspect list:", fmep->id,
33707c478bd9Sstevel@tonic-gate 	    fme_state2str(fmep->state));
33717c478bd9Sstevel@tonic-gate 	for (ep = fmep->suspects; ep; ep = ep->suspects) {
3372b5016cbbSstephh 		out(O_ALTFP|O_NONL, " ");
3373b5016cbbSstephh 		itree_pevent_brief(O_ALTFP|O_NONL, ep);
33747c478bd9Sstevel@tonic-gate 	}
3375b5016cbbSstephh 	out(O_ALTFP, NULL);
33767c478bd9Sstevel@tonic-gate 
3377d96ce684Sstephh 	switch (fmep->state) {
3378d96ce684Sstephh 	case FME_CREDIBLE:
3379d96ce684Sstephh 		print_suspects(SLNEW, fmep);
3380d96ce684Sstephh 		(void) upsets_eval(fmep, ffep);
3381d96ce684Sstephh 
33827c478bd9Sstevel@tonic-gate 		/*
3383d96ce684Sstephh 		 * we may have already posted suspects in upsets_eval() which
3384d96ce684Sstephh 		 * can recurse into fme_eval() again. If so then just return.
33857c478bd9Sstevel@tonic-gate 		 */
3386d96ce684Sstephh 		if (fmep->posted_suspects)
3387d96ce684Sstephh 			return;
33887c478bd9Sstevel@tonic-gate 
3389b7d3956bSstephh 		stats_counter_bump(fmep->diags);
3390b7d3956bSstephh 		rpcnt = fmep->nsuspects;
3391b7d3956bSstephh 		save_suspects(fmep);
3392b7d3956bSstephh 
3393b7d3956bSstephh 		/*
3394b7d3956bSstephh 		 * create two lists, one for "message=1" faults and one for
3395b7d3956bSstephh 		 * "message=0" faults. If we have a mixture we will generate
3396b7d3956bSstephh 		 * two separate suspect lists.
3397b7d3956bSstephh 		 */
3398b7d3956bSstephh 		srl = MALLOC(rpcnt * sizeof (struct rsl));
3399b7d3956bSstephh 		bzero(srl, rpcnt * sizeof (struct rsl));
3400b7d3956bSstephh 		srl2 = MALLOC(rpcnt * sizeof (struct rsl));
3401b7d3956bSstephh 		bzero(srl2, rpcnt * sizeof (struct rsl));
34028e7248e5SStephen Hanson 		mess_zero_count = trim_suspects(fmep, srl, srl2, ffep);
3403b7d3956bSstephh 
3404b7d3956bSstephh 		/*
3405b7d3956bSstephh 		 * If the resulting suspect list has no members, we're
3406b7d3956bSstephh 		 * done so simply close the case. Otherwise sort and publish.
3407b7d3956bSstephh 		 */
3408b7d3956bSstephh 		if (fmep->nsuspects == 0 && mess_zero_count == 0) {
3409b7d3956bSstephh 			out(O_ALTFP,
3410b7d3956bSstephh 			    "[FME%d, case %s (all suspects are upsets)]",
3411b7d3956bSstephh 			    fmep->id, fmd_case_uuid(fmep->hdl, fmep->fmcase));
3412b7d3956bSstephh 			fmd_case_close(fmep->hdl, fmep->fmcase);
3413b7d3956bSstephh 		} else if (fmep->nsuspects != 0 && mess_zero_count == 0) {
3414b7d3956bSstephh 			publish_suspects(fmep, srl);
3415b7d3956bSstephh 			out(O_ALTFP, "[solving FME%d, case %s]", fmep->id,
3416b7d3956bSstephh 			    fmd_case_uuid(fmep->hdl, fmep->fmcase));
3417b7d3956bSstephh 			fmd_case_solve(fmep->hdl, fmep->fmcase);
3418b7d3956bSstephh 		} else if (fmep->nsuspects == 0 && mess_zero_count != 0) {
3419b7d3956bSstephh 			fmep->nsuspects = mess_zero_count;
3420b7d3956bSstephh 			publish_suspects(fmep, srl2);
3421b7d3956bSstephh 			out(O_ALTFP, "[solving FME%d, case %s]", fmep->id,
3422b7d3956bSstephh 			    fmd_case_uuid(fmep->hdl, fmep->fmcase));
3423b7d3956bSstephh 			fmd_case_solve(fmep->hdl, fmep->fmcase);
3424b7d3956bSstephh 		} else {
3425b7d3956bSstephh 			struct event *obsp;
3426b7d3956bSstephh 			struct fme *nfmep;
3427b7d3956bSstephh 
3428b7d3956bSstephh 			publish_suspects(fmep, srl);
3429b7d3956bSstephh 			out(O_ALTFP, "[solving FME%d, case %s]", fmep->id,
3430b7d3956bSstephh 			    fmd_case_uuid(fmep->hdl, fmep->fmcase));
3431b7d3956bSstephh 			fmd_case_solve(fmep->hdl, fmep->fmcase);
3432b7d3956bSstephh 
3433b7d3956bSstephh 			/*
3434b7d3956bSstephh 			 * Got both message=0 and message=1 so create a
3435b7d3956bSstephh 			 * duplicate case. Also need a temporary duplicate fme
3436b7d3956bSstephh 			 * structure for use by publish_suspects().
3437b7d3956bSstephh 			 */
3438b7d3956bSstephh 			nfmep = alloc_fme();
3439b7d3956bSstephh 			nfmep->id =  Nextid++;
3440b7d3956bSstephh 			nfmep->hdl = fmep->hdl;
3441b7d3956bSstephh 			nfmep->nsuspects = mess_zero_count;
3442b7d3956bSstephh 			nfmep->fmcase = fmd_case_open(fmep->hdl, NULL);
3443b7d3956bSstephh 			out(O_ALTFP|O_STAMP,
3444b7d3956bSstephh 			    "[creating parallel FME%d, case %s]", nfmep->id,
3445b7d3956bSstephh 			    fmd_case_uuid(nfmep->hdl, nfmep->fmcase));
3446b7d3956bSstephh 			Open_fme_count++;
3447b7d3956bSstephh 			if (ffep) {
3448b7d3956bSstephh 				fmd_case_setprincipal(nfmep->hdl,
3449b7d3956bSstephh 				    nfmep->fmcase, ffep);
3450b7d3956bSstephh 				fmd_case_add_ereport(nfmep->hdl,
3451b7d3956bSstephh 				    nfmep->fmcase, ffep);
3452b7d3956bSstephh 			}
3453b7d3956bSstephh 			for (obsp = fmep->observations; obsp;
3454b7d3956bSstephh 			    obsp = obsp->observations)
3455b7d3956bSstephh 				if (obsp->ffep && obsp->ffep != ffep)
3456b7d3956bSstephh 					fmd_case_add_ereport(nfmep->hdl,
3457b7d3956bSstephh 					    nfmep->fmcase, obsp->ffep);
3458b7d3956bSstephh 
3459b7d3956bSstephh 			publish_suspects(nfmep, srl2);
3460b7d3956bSstephh 			out(O_ALTFP, "[solving FME%d, case %s]", nfmep->id,
3461b7d3956bSstephh 			    fmd_case_uuid(nfmep->hdl, nfmep->fmcase));
3462b7d3956bSstephh 			fmd_case_solve(nfmep->hdl, nfmep->fmcase);
3463b7d3956bSstephh 			FREE(nfmep);
3464b7d3956bSstephh 		}
3465b7d3956bSstephh 		FREE(srl);
3466b7d3956bSstephh 		FREE(srl2);
3467b7d3956bSstephh 		restore_suspects(fmep);
3468b7d3956bSstephh 
3469d96ce684Sstephh 		fmep->posted_suspects = 1;
3470d96ce684Sstephh 		fmd_buf_write(fmep->hdl, fmep->fmcase,
3471d96ce684Sstephh 		    WOBUF_POSTD,
3472d96ce684Sstephh 		    (void *)&fmep->posted_suspects,
3473d96ce684Sstephh 		    sizeof (fmep->posted_suspects));
34747c478bd9Sstevel@tonic-gate 
3475d96ce684Sstephh 		/*
3476d96ce684Sstephh 		 * Now the suspects have been posted, we can clear up
3477d96ce684Sstephh 		 * the instance tree as we won't be looking at it again.
3478d96ce684Sstephh 		 * Also cancel the timer as the case is now solved.
3479d96ce684Sstephh 		 */
3480d96ce684Sstephh 		if (fmep->wull != 0) {
3481d96ce684Sstephh 			fmd_timer_remove(fmep->hdl, fmep->timer);
3482d96ce684Sstephh 			fmep->wull = 0;
34837c478bd9Sstevel@tonic-gate 		}
3484d96ce684Sstephh 		break;
3485d96ce684Sstephh 
3486d96ce684Sstephh 	case FME_WAIT:
3487d96ce684Sstephh 		ASSERT(my_delay > fmep->ull);
3488d96ce684Sstephh 		(void) fme_set_timer(fmep, my_delay);
3489d96ce684Sstephh 		print_suspects(SLWAIT, fmep);
3490b5016cbbSstephh 		itree_prune(fmep->eventtree);
3491b5016cbbSstephh 		return;
3492d96ce684Sstephh 
3493d96ce684Sstephh 	case FME_DISPROVED:
3494d96ce684Sstephh 		print_suspects(SLDISPROVED, fmep);
349532d4e834STarik Soydan 		Undiag_reason = UD_VAL_UNSOLVD;
3496d96ce684Sstephh 		fme_undiagnosable(fmep);
3497d96ce684Sstephh 		break;
34987c478bd9Sstevel@tonic-gate 	}
34997c478bd9Sstevel@tonic-gate 
3500b5016cbbSstephh 	itree_free(fmep->eventtree);
3501b5016cbbSstephh 	fmep->eventtree = NULL;
3502e5ba14ffSstephh 	structconfig_free(fmep->config);
3503e5ba14ffSstephh 	fmep->config = NULL;
3504b5016cbbSstephh 	destroy_fme_bufs(fmep);
35057c478bd9Sstevel@tonic-gate }
35067c478bd9Sstevel@tonic-gate 
35077c478bd9Sstevel@tonic-gate static void indent(void);
35087c478bd9Sstevel@tonic-gate static int triggered(struct fme *fmep, struct event *ep, int mark);
35097c478bd9Sstevel@tonic-gate static enum fme_state effects_test(struct fme *fmep,
35107aec1d6eScindi     struct event *fault_event, unsigned long long at_latest_by,
35117aec1d6eScindi     unsigned long long *pdelay);
35127c478bd9Sstevel@tonic-gate static enum fme_state requirements_test(struct fme *fmep, struct event *ep,
35137aec1d6eScindi     unsigned long long at_latest_by, unsigned long long *pdelay);
35147c478bd9Sstevel@tonic-gate static enum fme_state causes_test(struct fme *fmep, struct event *ep,
35157c478bd9Sstevel@tonic-gate     unsigned long long at_latest_by, unsigned long long *pdelay);
35167c478bd9Sstevel@tonic-gate 
35177aec1d6eScindi static int
35187aec1d6eScindi checkconstraints(struct fme *fmep, struct arrow *arrowp)
35197aec1d6eScindi {
35207aec1d6eScindi 	struct constraintlist *ctp;
35217aec1d6eScindi 	struct evalue value;
3522b5016cbbSstephh 	char *sep = "";
35237aec1d6eScindi 
35247aec1d6eScindi 	if (arrowp->forever_false) {
35257aec1d6eScindi 		indent();
35267aec1d6eScindi 		out(O_ALTFP|O_VERB|O_NONL, "  Forever false constraint: ");
35277aec1d6eScindi 		for (ctp = arrowp->constraints; ctp != NULL; ctp = ctp->next) {
35287aec1d6eScindi 			out(O_ALTFP|O_VERB|O_NONL, sep);
35297aec1d6eScindi 			ptree(O_ALTFP|O_VERB|O_NONL, ctp->cnode, 1, 0);
35307aec1d6eScindi 			sep = ", ";
35317aec1d6eScindi 		}
35327aec1d6eScindi 		out(O_ALTFP|O_VERB, NULL);
35337aec1d6eScindi 		return (0);
35347aec1d6eScindi 	}
3535b5016cbbSstephh 	if (arrowp->forever_true) {
3536b5016cbbSstephh 		indent();
3537b5016cbbSstephh 		out(O_ALTFP|O_VERB|O_NONL, "  Forever true constraint: ");
3538b5016cbbSstephh 		for (ctp = arrowp->constraints; ctp != NULL; ctp = ctp->next) {
3539b5016cbbSstephh 			out(O_ALTFP|O_VERB|O_NONL, sep);
3540b5016cbbSstephh 			ptree(O_ALTFP|O_VERB|O_NONL, ctp->cnode, 1, 0);
3541b5016cbbSstephh 			sep = ", ";
3542b5016cbbSstephh 		}
3543b5016cbbSstephh 		out(O_ALTFP|O_VERB, NULL);
3544b5016cbbSstephh 		return (1);
3545b5016cbbSstephh 	}
35467aec1d6eScindi 
35477aec1d6eScindi 	for (ctp = arrowp->constraints; ctp != NULL; ctp = ctp->next) {
35487aec1d6eScindi 		if (eval_expr(ctp->cnode, NULL, NULL,
3549e5ba14ffSstephh 		    &fmep->globals, fmep->config,
35507aec1d6eScindi 		    arrowp, 0, &value)) {
35517aec1d6eScindi 			/* evaluation successful */
35527aec1d6eScindi 			if (value.t == UNDEFINED || value.v == 0) {
35537aec1d6eScindi 				/* known false */
35547aec1d6eScindi 				arrowp->forever_false = 1;
35557aec1d6eScindi 				indent();
35567aec1d6eScindi 				out(O_ALTFP|O_VERB|O_NONL,
35577aec1d6eScindi 				    "  False constraint: ");
35587aec1d6eScindi 				ptree(O_ALTFP|O_VERB|O_NONL, ctp->cnode, 1, 0);
35597aec1d6eScindi 				out(O_ALTFP|O_VERB, NULL);
35607aec1d6eScindi 				return (0);
35617aec1d6eScindi 			}
35627aec1d6eScindi 		} else {
35637aec1d6eScindi 			/* evaluation unsuccessful -- unknown value */
35647aec1d6eScindi 			indent();
35657aec1d6eScindi 			out(O_ALTFP|O_VERB|O_NONL,
35667aec1d6eScindi 			    "  Deferred constraint: ");
35677aec1d6eScindi 			ptree(O_ALTFP|O_VERB|O_NONL, ctp->cnode, 1, 0);
35687aec1d6eScindi 			out(O_ALTFP|O_VERB, NULL);
3569b5016cbbSstephh 			return (1);
35707aec1d6eScindi 		}
35717aec1d6eScindi 	}
35727aec1d6eScindi 	/* known true */
3573b5016cbbSstephh 	arrowp->forever_true = 1;
3574b5016cbbSstephh 	indent();
3575b5016cbbSstephh 	out(O_ALTFP|O_VERB|O_NONL, "  True constraint: ");
3576b5016cbbSstephh 	for (ctp = arrowp->constraints; ctp != NULL; ctp = ctp->next) {
3577b5016cbbSstephh 		out(O_ALTFP|O_VERB|O_NONL, sep);
3578b5016cbbSstephh 		ptree(O_ALTFP|O_VERB|O_NONL, ctp->cnode, 1, 0);
3579b5016cbbSstephh 		sep = ", ";
3580b5016cbbSstephh 	}
3581b5016cbbSstephh 	out(O_ALTFP|O_VERB, NULL);
35827aec1d6eScindi 	return (1);
35837aec1d6eScindi }
35847aec1d6eScindi 
35857c478bd9Sstevel@tonic-gate static int
35867c478bd9Sstevel@tonic-gate triggered(struct fme *fmep, struct event *ep, int mark)
35877c478bd9Sstevel@tonic-gate {
35887c478bd9Sstevel@tonic-gate 	struct bubble *bp;
35897c478bd9Sstevel@tonic-gate 	struct arrowlist *ap;
35907c478bd9Sstevel@tonic-gate 	int count = 0;
35917c478bd9Sstevel@tonic-gate 
35927c478bd9Sstevel@tonic-gate 	stats_counter_bump(fmep->Tcallcount);
35937c478bd9Sstevel@tonic-gate 	for (bp = itree_next_bubble(ep, NULL); bp;
35947c478bd9Sstevel@tonic-gate 	    bp = itree_next_bubble(ep, bp)) {
35957c478bd9Sstevel@tonic-gate 		if (bp->t != B_TO)
35967c478bd9Sstevel@tonic-gate 			continue;
35977c478bd9Sstevel@tonic-gate 		for (ap = itree_next_arrow(bp, NULL); ap;
35987c478bd9Sstevel@tonic-gate 		    ap = itree_next_arrow(bp, ap)) {
35997c478bd9Sstevel@tonic-gate 			/* check count of marks against K in the bubble */
36007aec1d6eScindi 			if ((ap->arrowp->mark & mark) &&
36017c478bd9Sstevel@tonic-gate 			    ++count >= bp->nork)
36027c478bd9Sstevel@tonic-gate 				return (1);
36037c478bd9Sstevel@tonic-gate 		}
36047c478bd9Sstevel@tonic-gate 	}
36057c478bd9Sstevel@tonic-gate 	return (0);
36067c478bd9Sstevel@tonic-gate }
36077c478bd9Sstevel@tonic-gate 
36087aec1d6eScindi static int
36097aec1d6eScindi mark_arrows(struct fme *fmep, struct event *ep, int mark,
361000d0963fSdilpreet     unsigned long long at_latest_by, unsigned long long *pdelay, int keep)
36117c478bd9Sstevel@tonic-gate {
36127c478bd9Sstevel@tonic-gate 	struct bubble *bp;
36137c478bd9Sstevel@tonic-gate 	struct arrowlist *ap;
36147aec1d6eScindi 	unsigned long long overall_delay = TIMEVAL_EVENTUALLY;
36157aec1d6eScindi 	unsigned long long my_delay;
36167aec1d6eScindi 	enum fme_state result;
36177aec1d6eScindi 	int retval = 0;
36187c478bd9Sstevel@tonic-gate 
36197c478bd9Sstevel@tonic-gate 	for (bp = itree_next_bubble(ep, NULL); bp;
36207c478bd9Sstevel@tonic-gate 	    bp = itree_next_bubble(ep, bp)) {
36217c478bd9Sstevel@tonic-gate 		if (bp->t != B_FROM)
36227c478bd9Sstevel@tonic-gate 			continue;
36237aec1d6eScindi 		stats_counter_bump(fmep->Marrowcount);
36247aec1d6eScindi 		for (ap = itree_next_arrow(bp, NULL); ap;
36257aec1d6eScindi 		    ap = itree_next_arrow(bp, ap)) {
36267aec1d6eScindi 			struct event *ep2 = ap->arrowp->head->myevent;
36277aec1d6eScindi 			/*
36287aec1d6eScindi 			 * if we're clearing marks, we can avoid doing
36297aec1d6eScindi 			 * all that work evaluating constraints.
36307aec1d6eScindi 			 */
36317aec1d6eScindi 			if (mark == 0) {
3632b5016cbbSstephh 				if (ap->arrowp->arrow_marked == 0)
3633b5016cbbSstephh 					continue;
3634b5016cbbSstephh 				ap->arrowp->arrow_marked = 0;
36357aec1d6eScindi 				ap->arrowp->mark &= ~EFFECTS_COUNTER;
363600d0963fSdilpreet 				if (keep && (ep2->cached_state &
363700d0963fSdilpreet 				    (WAIT_EFFECT|CREDIBLE_EFFECT|PARENT_WAIT)))
363800d0963fSdilpreet 					ep2->keep_in_tree = 1;
36397aec1d6eScindi 				ep2->cached_state &=
36407aec1d6eScindi 				    ~(WAIT_EFFECT|CREDIBLE_EFFECT|PARENT_WAIT);
364100d0963fSdilpreet 				(void) mark_arrows(fmep, ep2, mark, 0, NULL,
364200d0963fSdilpreet 				    keep);
36437aec1d6eScindi 				continue;
36447aec1d6eScindi 			}
3645b5016cbbSstephh 			ap->arrowp->arrow_marked = 1;
36467aec1d6eScindi 			if (ep2->cached_state & REQMNTS_DISPROVED) {
36477aec1d6eScindi 				indent();
36487aec1d6eScindi 				out(O_ALTFP|O_VERB|O_NONL,
36497aec1d6eScindi 				    "  ALREADY DISPROVED ");
36507aec1d6eScindi 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
36517aec1d6eScindi 				out(O_ALTFP|O_VERB, NULL);
36527aec1d6eScindi 				continue;
36537aec1d6eScindi 			}
36547aec1d6eScindi 			if (ep2->cached_state & WAIT_EFFECT) {
36557aec1d6eScindi 				indent();
36567aec1d6eScindi 				out(O_ALTFP|O_VERB|O_NONL,
36577aec1d6eScindi 				    "  ALREADY EFFECTS WAIT ");
36587aec1d6eScindi 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
36597aec1d6eScindi 				out(O_ALTFP|O_VERB, NULL);
36607aec1d6eScindi 				continue;
36617aec1d6eScindi 			}
36627aec1d6eScindi 			if (ep2->cached_state & CREDIBLE_EFFECT) {
36637aec1d6eScindi 				indent();
36647aec1d6eScindi 				out(O_ALTFP|O_VERB|O_NONL,
36657aec1d6eScindi 				    "  ALREADY EFFECTS CREDIBLE ");
36667aec1d6eScindi 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
36677aec1d6eScindi 				out(O_ALTFP|O_VERB, NULL);
36687aec1d6eScindi 				continue;
36697aec1d6eScindi 			}
36707aec1d6eScindi 			if ((ep2->cached_state & PARENT_WAIT) &&
36717aec1d6eScindi 			    (mark & PARENT_WAIT)) {
36727aec1d6eScindi 				indent();
36737aec1d6eScindi 				out(O_ALTFP|O_VERB|O_NONL,
36747aec1d6eScindi 				    "  ALREADY PARENT EFFECTS WAIT ");
36757aec1d6eScindi 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
36767aec1d6eScindi 				out(O_ALTFP|O_VERB, NULL);
36777aec1d6eScindi 				continue;
36787aec1d6eScindi 			}
36797aec1d6eScindi 			platform_set_payloadnvp(ep2->nvp);
368000d0963fSdilpreet 			if (checkconstraints(fmep, ap->arrowp) == 0) {
36817aec1d6eScindi 				platform_set_payloadnvp(NULL);
36827aec1d6eScindi 				indent();
36837aec1d6eScindi 				out(O_ALTFP|O_VERB|O_NONL,
36847aec1d6eScindi 				    "  CONSTRAINTS FAIL ");
36857aec1d6eScindi 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
36867aec1d6eScindi 				out(O_ALTFP|O_VERB, NULL);
36877aec1d6eScindi 				continue;
36887aec1d6eScindi 			}
36897aec1d6eScindi 			platform_set_payloadnvp(NULL);
36907aec1d6eScindi 			ap->arrowp->mark |= EFFECTS_COUNTER;
36917aec1d6eScindi 			if (!triggered(fmep, ep2, EFFECTS_COUNTER)) {
36927aec1d6eScindi 				indent();
36937aec1d6eScindi 				out(O_ALTFP|O_VERB|O_NONL,
36947aec1d6eScindi 				    "  K-COUNT NOT YET MET ");
36957aec1d6eScindi 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
36967aec1d6eScindi 				out(O_ALTFP|O_VERB, NULL);
36977aec1d6eScindi 				continue;
36987aec1d6eScindi 			}
36997aec1d6eScindi 			ep2->cached_state &= ~PARENT_WAIT;
3700d96ce684Sstephh 			/*
3701d96ce684Sstephh 			 * if we've reached an ereport and no propagation time
3702d96ce684Sstephh 			 * is specified, use the Hesitate value
3703d96ce684Sstephh 			 */
3704d96ce684Sstephh 			if (ep2->t == N_EREPORT && at_latest_by == 0ULL &&
3705d96ce684Sstephh 			    ap->arrowp->maxdelay == 0ULL) {
3706d96ce684Sstephh 				out(O_ALTFP|O_VERB|O_NONL, "  default wait ");
3707d96ce684Sstephh 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
3708d96ce684Sstephh 				out(O_ALTFP|O_VERB, NULL);
3709b5016cbbSstephh 				result = requirements_test(fmep, ep2, Hesitate,
3710b5016cbbSstephh 				    &my_delay);
3711d96ce684Sstephh 			} else {
3712d96ce684Sstephh 				result = requirements_test(fmep, ep2,
3713d96ce684Sstephh 				    at_latest_by + ap->arrowp->maxdelay,
3714d96ce684Sstephh 				    &my_delay);
3715d96ce684Sstephh 			}
37167aec1d6eScindi 			if (result == FME_WAIT) {
37177aec1d6eScindi 				retval = WAIT_EFFECT;
37187aec1d6eScindi 				if (overall_delay > my_delay)
37197aec1d6eScindi 					overall_delay = my_delay;
37207aec1d6eScindi 				ep2->cached_state |= WAIT_EFFECT;
37217aec1d6eScindi 				indent();
37227aec1d6eScindi 				out(O_ALTFP|O_VERB|O_NONL, "  EFFECTS WAIT ");
37237aec1d6eScindi 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
37247aec1d6eScindi 				out(O_ALTFP|O_VERB, NULL);
37257aec1d6eScindi 				indent_push("  E");
37267aec1d6eScindi 				if (mark_arrows(fmep, ep2, PARENT_WAIT,
372700d0963fSdilpreet 				    at_latest_by, &my_delay, 0) ==
372800d0963fSdilpreet 				    WAIT_EFFECT) {
37297aec1d6eScindi 					retval = WAIT_EFFECT;
37307aec1d6eScindi 					if (overall_delay > my_delay)
37317aec1d6eScindi 						overall_delay = my_delay;
37327c478bd9Sstevel@tonic-gate 				}
37337aec1d6eScindi 				indent_pop();
37347aec1d6eScindi 			} else if (result == FME_DISPROVED) {
37357aec1d6eScindi 				indent();
37367aec1d6eScindi 				out(O_ALTFP|O_VERB|O_NONL,
37377aec1d6eScindi 				    "  EFFECTS DISPROVED ");
37387aec1d6eScindi 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
37397aec1d6eScindi 				out(O_ALTFP|O_VERB, NULL);
37407aec1d6eScindi 			} else {
37417aec1d6eScindi 				ep2->cached_state |= mark;
37427aec1d6eScindi 				indent();
37437aec1d6eScindi 				if (mark == CREDIBLE_EFFECT)
37447c478bd9Sstevel@tonic-gate 					out(O_ALTFP|O_VERB|O_NONL,
37457aec1d6eScindi 					    "  EFFECTS CREDIBLE ");
37467aec1d6eScindi 				else
37477aec1d6eScindi 					out(O_ALTFP|O_VERB|O_NONL,
37487aec1d6eScindi 					    "  PARENT EFFECTS WAIT ");
37497aec1d6eScindi 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
37507aec1d6eScindi 				out(O_ALTFP|O_VERB, NULL);
37517aec1d6eScindi 				indent_push("  E");
37527aec1d6eScindi 				if (mark_arrows(fmep, ep2, mark, at_latest_by,
375300d0963fSdilpreet 				    &my_delay, 0) == WAIT_EFFECT) {
37547aec1d6eScindi 					retval = WAIT_EFFECT;
37557aec1d6eScindi 					if (overall_delay > my_delay)
37567aec1d6eScindi 						overall_delay = my_delay;
37577c478bd9Sstevel@tonic-gate 				}
37587aec1d6eScindi 				indent_pop();
37597c478bd9Sstevel@tonic-gate 			}
37607c478bd9Sstevel@tonic-gate 		}
37617c478bd9Sstevel@tonic-gate 	}
37627aec1d6eScindi 	if (retval == WAIT_EFFECT)
37637aec1d6eScindi 		*pdelay = overall_delay;
37647aec1d6eScindi 	return (retval);
37657c478bd9Sstevel@tonic-gate }
37667c478bd9Sstevel@tonic-gate 
37677c478bd9Sstevel@tonic-gate static enum fme_state
37687aec1d6eScindi effects_test(struct fme *fmep, struct event *fault_event,
37697aec1d6eScindi     unsigned long long at_latest_by, unsigned long long *pdelay)
37707c478bd9Sstevel@tonic-gate {
37717c478bd9Sstevel@tonic-gate 	struct event *error_event;
37727c478bd9Sstevel@tonic-gate 	enum fme_state return_value = FME_CREDIBLE;
37737aec1d6eScindi 	unsigned long long overall_delay = TIMEVAL_EVENTUALLY;
37747aec1d6eScindi 	unsigned long long my_delay;
37757c478bd9Sstevel@tonic-gate 
37767c478bd9Sstevel@tonic-gate 	stats_counter_bump(fmep->Ecallcount);
37777c478bd9Sstevel@tonic-gate 	indent_push("  E");
37787c478bd9Sstevel@tonic-gate 	indent();
37797c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB|O_NONL, "->");
37807c478bd9Sstevel@tonic-gate 	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, fault_event);
37817c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB, NULL);
37827c478bd9Sstevel@tonic-gate 
3783d96ce684Sstephh 	if (mark_arrows(fmep, fault_event, CREDIBLE_EFFECT, at_latest_by,
3784d96ce684Sstephh 	    &my_delay, 0) == WAIT_EFFECT) {
3785d96ce684Sstephh 		return_value = FME_WAIT;
3786d96ce684Sstephh 		if (overall_delay > my_delay)
3787d96ce684Sstephh 			overall_delay = my_delay;
3788d96ce684Sstephh 	}
37897c478bd9Sstevel@tonic-gate 	for (error_event = fmep->observations;
37907c478bd9Sstevel@tonic-gate 	    error_event; error_event = error_event->observations) {
37917c478bd9Sstevel@tonic-gate 		indent();
37927c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB|O_NONL, " ");
37937c478bd9Sstevel@tonic-gate 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, error_event);
37947aec1d6eScindi 		if (!(error_event->cached_state & CREDIBLE_EFFECT)) {
37957aec1d6eScindi 			if (error_event->cached_state &
37967aec1d6eScindi 			    (PARENT_WAIT|WAIT_EFFECT)) {
37977aec1d6eScindi 				out(O_ALTFP|O_VERB, " NOT YET triggered");
37987aec1d6eScindi 				continue;
37997aec1d6eScindi 			}
38007c478bd9Sstevel@tonic-gate 			return_value = FME_DISPROVED;
38017c478bd9Sstevel@tonic-gate 			out(O_ALTFP|O_VERB, " NOT triggered");
38027c478bd9Sstevel@tonic-gate 			break;
38037c478bd9Sstevel@tonic-gate 		} else {
38047c478bd9Sstevel@tonic-gate 			out(O_ALTFP|O_VERB, " triggered");
38057c478bd9Sstevel@tonic-gate 		}
38067c478bd9Sstevel@tonic-gate 	}
380700d0963fSdilpreet 	if (return_value == FME_DISPROVED) {
380800d0963fSdilpreet 		(void) mark_arrows(fmep, fault_event, 0, 0, NULL, 0);
380900d0963fSdilpreet 	} else {
381000d0963fSdilpreet 		fault_event->keep_in_tree = 1;
381100d0963fSdilpreet 		(void) mark_arrows(fmep, fault_event, 0, 0, NULL, 1);
381200d0963fSdilpreet 	}
38137c478bd9Sstevel@tonic-gate 
38147c478bd9Sstevel@tonic-gate 	indent();
38157aec1d6eScindi 	out(O_ALTFP|O_VERB|O_NONL, "<-EFFECTS %s ",
38167aec1d6eScindi 	    fme_state2str(return_value));
38177c478bd9Sstevel@tonic-gate 	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, fault_event);
38187c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB, NULL);
38197c478bd9Sstevel@tonic-gate 	indent_pop();
38207aec1d6eScindi 	if (return_value == FME_WAIT)
38217aec1d6eScindi 		*pdelay = overall_delay;
38227c478bd9Sstevel@tonic-gate 	return (return_value);
38237c478bd9Sstevel@tonic-gate }
38247c478bd9Sstevel@tonic-gate 
38257c478bd9Sstevel@tonic-gate static enum fme_state
38267c478bd9Sstevel@tonic-gate requirements_test(struct fme *fmep, struct event *ep,
38277aec1d6eScindi     unsigned long long at_latest_by, unsigned long long *pdelay)
38287c478bd9Sstevel@tonic-gate {
38297c478bd9Sstevel@tonic-gate 	int waiting_events;
38307c478bd9Sstevel@tonic-gate 	int credible_events;
38317aec1d6eScindi 	int deferred_events;
38327c478bd9Sstevel@tonic-gate 	enum fme_state return_value = FME_CREDIBLE;
38337c478bd9Sstevel@tonic-gate 	unsigned long long overall_delay = TIMEVAL_EVENTUALLY;
38347c478bd9Sstevel@tonic-gate 	unsigned long long arrow_delay;
38357c478bd9Sstevel@tonic-gate 	unsigned long long my_delay;
38367c478bd9Sstevel@tonic-gate 	struct event *ep2;
38377c478bd9Sstevel@tonic-gate 	struct bubble *bp;
38387c478bd9Sstevel@tonic-gate 	struct arrowlist *ap;
38397c478bd9Sstevel@tonic-gate 
38407aec1d6eScindi 	if (ep->cached_state & REQMNTS_CREDIBLE) {
38417aec1d6eScindi 		indent();
38427aec1d6eScindi 		out(O_ALTFP|O_VERB|O_NONL, "  REQMNTS ALREADY CREDIBLE ");
38437aec1d6eScindi 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
38447aec1d6eScindi 		out(O_ALTFP|O_VERB, NULL);
38457aec1d6eScindi 		return (FME_CREDIBLE);
38467aec1d6eScindi 	}
38477aec1d6eScindi 	if (ep->cached_state & REQMNTS_DISPROVED) {
38487aec1d6eScindi 		indent();
38497aec1d6eScindi 		out(O_ALTFP|O_VERB|O_NONL, "  REQMNTS ALREADY DISPROVED ");
38507aec1d6eScindi 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
38517aec1d6eScindi 		out(O_ALTFP|O_VERB, NULL);
38527aec1d6eScindi 		return (FME_DISPROVED);
38537aec1d6eScindi 	}
38547aec1d6eScindi 	if (ep->cached_state & REQMNTS_WAIT) {
38557aec1d6eScindi 		indent();
38567aec1d6eScindi 		*pdelay = ep->cached_delay;
38577aec1d6eScindi 		out(O_ALTFP|O_VERB|O_NONL, "  REQMNTS ALREADY WAIT ");
38587aec1d6eScindi 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
38597aec1d6eScindi 		out(O_ALTFP|O_VERB|O_NONL, ", wait for: ");
38607aec1d6eScindi 		ptree_timeval(O_ALTFP|O_VERB|O_NONL, &at_latest_by);
38617aec1d6eScindi 		out(O_ALTFP|O_VERB, NULL);
38627aec1d6eScindi 		return (FME_WAIT);
38637aec1d6eScindi 	}
38647c478bd9Sstevel@tonic-gate 	stats_counter_bump(fmep->Rcallcount);
38657c478bd9Sstevel@tonic-gate 	indent_push("  R");
38667c478bd9Sstevel@tonic-gate 	indent();
38677c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB|O_NONL, "->");
38687c478bd9Sstevel@tonic-gate 	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
38697c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB|O_NONL, ", at latest by: ");
38707c478bd9Sstevel@tonic-gate 	ptree_timeval(O_ALTFP|O_VERB|O_NONL, &at_latest_by);
38717c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB, NULL);
38727c478bd9Sstevel@tonic-gate 
38737c478bd9Sstevel@tonic-gate 	if (ep->t == N_EREPORT) {
38747c478bd9Sstevel@tonic-gate 		if (ep->count == 0) {
38757c478bd9Sstevel@tonic-gate 			if (fmep->pull >= at_latest_by) {
38767c478bd9Sstevel@tonic-gate 				return_value = FME_DISPROVED;
38777c478bd9Sstevel@tonic-gate 			} else {
38787aec1d6eScindi 				ep->cached_delay = *pdelay = at_latest_by;
38797c478bd9Sstevel@tonic-gate 				return_value = FME_WAIT;
38807c478bd9Sstevel@tonic-gate 			}
38817c478bd9Sstevel@tonic-gate 		}
38827c478bd9Sstevel@tonic-gate 
38837c478bd9Sstevel@tonic-gate 		indent();
38847c478bd9Sstevel@tonic-gate 		switch (return_value) {
38857c478bd9Sstevel@tonic-gate 		case FME_CREDIBLE:
38867aec1d6eScindi 			ep->cached_state |= REQMNTS_CREDIBLE;
38877aec1d6eScindi 			out(O_ALTFP|O_VERB|O_NONL, "<-REQMNTS CREDIBLE ");
38887c478bd9Sstevel@tonic-gate 			itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
38897c478bd9Sstevel@tonic-gate 			break;
38907c478bd9Sstevel@tonic-gate 		case FME_DISPROVED:
38917aec1d6eScindi 			ep->cached_state |= REQMNTS_DISPROVED;
38927aec1d6eScindi 			out(O_ALTFP|O_VERB|O_NONL, "<-REQMNTS DISPROVED ");
38937c478bd9Sstevel@tonic-gate 			itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
38947c478bd9Sstevel@tonic-gate 			break;
38957c478bd9Sstevel@tonic-gate 		case FME_WAIT:
38967aec1d6eScindi 			ep->cached_state |= REQMNTS_WAIT;
38977aec1d6eScindi 			out(O_ALTFP|O_VERB|O_NONL, "<-REQMNTS WAIT ");
38987c478bd9Sstevel@tonic-gate 			itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
38997c478bd9Sstevel@tonic-gate 			out(O_ALTFP|O_VERB|O_NONL, " to ");
39007c478bd9Sstevel@tonic-gate 			ptree_timeval(O_ALTFP|O_VERB|O_NONL, &at_latest_by);
39017c478bd9Sstevel@tonic-gate 			break;
39027c478bd9Sstevel@tonic-gate 		default:
39037c478bd9Sstevel@tonic-gate 			out(O_DIE, "requirements_test: unexpected fme_state");
39047c478bd9Sstevel@tonic-gate 			break;
39057c478bd9Sstevel@tonic-gate 		}
39067c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB, NULL);
39077c478bd9Sstevel@tonic-gate 		indent_pop();
39087c478bd9Sstevel@tonic-gate 
39097c478bd9Sstevel@tonic-gate 		return (return_value);
39107c478bd9Sstevel@tonic-gate 	}
39117c478bd9Sstevel@tonic-gate 
39127c478bd9Sstevel@tonic-gate 	/* this event is not a report, descend the tree */
39137c478bd9Sstevel@tonic-gate 	for (bp = itree_next_bubble(ep, NULL); bp;
39147c478bd9Sstevel@tonic-gate 	    bp = itree_next_bubble(ep, bp)) {
39157aec1d6eScindi 		int n;
39167aec1d6eScindi 
39177c478bd9Sstevel@tonic-gate 		if (bp->t != B_FROM)
39187c478bd9Sstevel@tonic-gate 			continue;
39197c478bd9Sstevel@tonic-gate 
39207aec1d6eScindi 		n = bp->nork;
39217aec1d6eScindi 
39227aec1d6eScindi 		credible_events = 0;
39237aec1d6eScindi 		waiting_events = 0;
39247aec1d6eScindi 		deferred_events = 0;
39257aec1d6eScindi 		arrow_delay = TIMEVAL_EVENTUALLY;
39267aec1d6eScindi 		/*
39277aec1d6eScindi 		 * n is -1 for 'A' so adjust it.
39287aec1d6eScindi 		 * XXX just count up the arrows for now.
39297aec1d6eScindi 		 */
39307aec1d6eScindi 		if (n < 0) {
39317aec1d6eScindi 			n = 0;
39327aec1d6eScindi 			for (ap = itree_next_arrow(bp, NULL); ap;
39337aec1d6eScindi 			    ap = itree_next_arrow(bp, ap))
39347aec1d6eScindi 				n++;
39357aec1d6eScindi 			indent();
39367aec1d6eScindi 			out(O_ALTFP|O_VERB, " Bubble Counted N=%d", n);
39377aec1d6eScindi 		} else {
39387aec1d6eScindi 			indent();
39397aec1d6eScindi 			out(O_ALTFP|O_VERB, " Bubble N=%d", n);
39407aec1d6eScindi 		}
39417c478bd9Sstevel@tonic-gate 
39427aec1d6eScindi 		if (n == 0)
39437aec1d6eScindi 			continue;
39447aec1d6eScindi 		if (!(bp->mark & (BUBBLE_ELIDED|BUBBLE_OK))) {
39457c478bd9Sstevel@tonic-gate 			for (ap = itree_next_arrow(bp, NULL); ap;
39467c478bd9Sstevel@tonic-gate 			    ap = itree_next_arrow(bp, ap)) {
39477c478bd9Sstevel@tonic-gate 				ep2 = ap->arrowp->head->myevent;
39487aec1d6eScindi 				platform_set_payloadnvp(ep2->nvp);
3949b7d3956bSstephh 				(void) checkconstraints(fmep, ap->arrowp);
39508cffa125SStephen Hanson 				if (!ap->arrowp->forever_false) {
39517aec1d6eScindi 					/*
3952b7d3956bSstephh 					 * if all arrows are invalidated by the
39537aec1d6eScindi 					 * constraints, then we should elide the
39547aec1d6eScindi 					 * whole bubble to be consistant with
39557aec1d6eScindi 					 * the tree creation time behaviour
39567aec1d6eScindi 					 */
3957b7d3956bSstephh 					bp->mark |= BUBBLE_OK;
39587aec1d6eScindi 					platform_set_payloadnvp(NULL);
39597c478bd9Sstevel@tonic-gate 					break;
39607aec1d6eScindi 				}
39617aec1d6eScindi 				platform_set_payloadnvp(NULL);
39627aec1d6eScindi 			}
39637aec1d6eScindi 		}
39647aec1d6eScindi 		for (ap = itree_next_arrow(bp, NULL); ap;
39657aec1d6eScindi 		    ap = itree_next_arrow(bp, ap)) {
39667aec1d6eScindi 			ep2 = ap->arrowp->head->myevent;
39677aec1d6eScindi 			if (n <= credible_events)
39687aec1d6eScindi 				break;
39697c478bd9Sstevel@tonic-gate 
39707aec1d6eScindi 			ap->arrowp->mark |= REQMNTS_COUNTER;
39717aec1d6eScindi 			if (triggered(fmep, ep2, REQMNTS_COUNTER))
39727aec1d6eScindi 				/* XXX adding max timevals! */
39737aec1d6eScindi 				switch (requirements_test(fmep, ep2,
39747aec1d6eScindi 				    at_latest_by + ap->arrowp->maxdelay,
39757aec1d6eScindi 				    &my_delay)) {
39767aec1d6eScindi 				case FME_DEFERRED:
39777aec1d6eScindi 					deferred_events++;
39787aec1d6eScindi 					break;
39797aec1d6eScindi 				case FME_CREDIBLE:
39807c478bd9Sstevel@tonic-gate 					credible_events++;
39817aec1d6eScindi 					break;
39827aec1d6eScindi 				case FME_DISPROVED:
39837aec1d6eScindi 					break;
39847aec1d6eScindi 				case FME_WAIT:
39857aec1d6eScindi 					if (my_delay < arrow_delay)
39867aec1d6eScindi 						arrow_delay = my_delay;
39877aec1d6eScindi 					waiting_events++;
39887aec1d6eScindi 					break;
39897aec1d6eScindi 				default:
39907aec1d6eScindi 					out(O_DIE,
39917aec1d6eScindi 					"Bug in requirements_test.");
39927aec1d6eScindi 				}
39937aec1d6eScindi 			else
39947aec1d6eScindi 				deferred_events++;
39957aec1d6eScindi 		}
3996b7d3956bSstephh 		if (!(bp->mark & BUBBLE_OK) && waiting_events == 0) {
3997b7d3956bSstephh 			bp->mark |= BUBBLE_ELIDED;
3998b7d3956bSstephh 			continue;
3999b7d3956bSstephh 		}
40007aec1d6eScindi 		indent();
40017aec1d6eScindi 		out(O_ALTFP|O_VERB, " Credible: %d Waiting %d",
40027aec1d6eScindi 		    credible_events + deferred_events, waiting_events);
40037aec1d6eScindi 		if (credible_events + deferred_events + waiting_events < n) {
40047aec1d6eScindi 			/* Can never meet requirements */
40057aec1d6eScindi 			ep->cached_state |= REQMNTS_DISPROVED;
40067c478bd9Sstevel@tonic-gate 			indent();
40077aec1d6eScindi 			out(O_ALTFP|O_VERB|O_NONL, "<-REQMNTS DISPROVED ");
40087c478bd9Sstevel@tonic-gate 			itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
40097c478bd9Sstevel@tonic-gate 			out(O_ALTFP|O_VERB, NULL);
40107aec1d6eScindi 			indent_pop();
40117aec1d6eScindi 			return (FME_DISPROVED);
40127aec1d6eScindi 		}
40137aec1d6eScindi 		if (credible_events + deferred_events < n) {
40147aec1d6eScindi 			/* will have to wait */
40157aec1d6eScindi 			/* wait time is shortest known */
40167aec1d6eScindi 			if (arrow_delay < overall_delay)
40177aec1d6eScindi 				overall_delay = arrow_delay;
40187aec1d6eScindi 			return_value = FME_WAIT;
40197aec1d6eScindi 		} else if (credible_events < n) {
40207aec1d6eScindi 			if (return_value != FME_WAIT)
40217aec1d6eScindi 				return_value = FME_DEFERRED;
40227c478bd9Sstevel@tonic-gate 		}
40237c478bd9Sstevel@tonic-gate 	}
40247c478bd9Sstevel@tonic-gate 
40257c478bd9Sstevel@tonic-gate 	/*
40267aec1d6eScindi 	 * don't mark as FME_DEFERRED. If this event isn't reached by another
40277aec1d6eScindi 	 * path, then this will be considered FME_CREDIBLE. But if it is
40287aec1d6eScindi 	 * reached by a different path so the K-count is met, then might
40297aec1d6eScindi 	 * get overridden by FME_WAIT or FME_DISPROVED.
40307c478bd9Sstevel@tonic-gate 	 */
40317aec1d6eScindi 	if (return_value == FME_WAIT) {
40327aec1d6eScindi 		ep->cached_state |= REQMNTS_WAIT;
40337aec1d6eScindi 		ep->cached_delay = *pdelay = overall_delay;
40347aec1d6eScindi 	} else if (return_value == FME_CREDIBLE) {
40357aec1d6eScindi 		ep->cached_state |= REQMNTS_CREDIBLE;
40367c478bd9Sstevel@tonic-gate 	}
40377c478bd9Sstevel@tonic-gate 	indent();
40387aec1d6eScindi 	out(O_ALTFP|O_VERB|O_NONL, "<-REQMNTS %s ",
40397aec1d6eScindi 	    fme_state2str(return_value));
40407c478bd9Sstevel@tonic-gate 	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
40417c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB, NULL);
40427c478bd9Sstevel@tonic-gate 	indent_pop();
40437c478bd9Sstevel@tonic-gate 	return (return_value);
40447c478bd9Sstevel@tonic-gate }
40457c478bd9Sstevel@tonic-gate 
40467c478bd9Sstevel@tonic-gate static enum fme_state
40477c478bd9Sstevel@tonic-gate causes_test(struct fme *fmep, struct event *ep,
40487c478bd9Sstevel@tonic-gate     unsigned long long at_latest_by, unsigned long long *pdelay)
40497c478bd9Sstevel@tonic-gate {
40507c478bd9Sstevel@tonic-gate 	unsigned long long overall_delay = TIMEVAL_EVENTUALLY;
40517c478bd9Sstevel@tonic-gate 	unsigned long long my_delay;
40527c478bd9Sstevel@tonic-gate 	int credible_results = 0;
40537c478bd9Sstevel@tonic-gate 	int waiting_results = 0;
40547c478bd9Sstevel@tonic-gate 	enum fme_state fstate;
40557c478bd9Sstevel@tonic-gate 	struct event *tail_event;
40567c478bd9Sstevel@tonic-gate 	struct bubble *bp;
40577c478bd9Sstevel@tonic-gate 	struct arrowlist *ap;
40587c478bd9Sstevel@tonic-gate 	int k = 1;
40597c478bd9Sstevel@tonic-gate 
40607c478bd9Sstevel@tonic-gate 	stats_counter_bump(fmep->Ccallcount);
40617c478bd9Sstevel@tonic-gate 	indent_push("  C");
40627c478bd9Sstevel@tonic-gate 	indent();
40637c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB|O_NONL, "->");
40647c478bd9Sstevel@tonic-gate 	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
40657c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB, NULL);
40667c478bd9Sstevel@tonic-gate 
40677c478bd9Sstevel@tonic-gate 	for (bp = itree_next_bubble(ep, NULL); bp;
40687c478bd9Sstevel@tonic-gate 	    bp = itree_next_bubble(ep, bp)) {
40697c478bd9Sstevel@tonic-gate 		if (bp->t != B_TO)
40707c478bd9Sstevel@tonic-gate 			continue;
40717c478bd9Sstevel@tonic-gate 		k = bp->nork;	/* remember the K value */
40727c478bd9Sstevel@tonic-gate 		for (ap = itree_next_arrow(bp, NULL); ap;
40737c478bd9Sstevel@tonic-gate 		    ap = itree_next_arrow(bp, ap)) {
40747c478bd9Sstevel@tonic-gate 			int do_not_follow = 0;
40757aec1d6eScindi 
40767aec1d6eScindi 			/*
40777aec1d6eScindi 			 * if we get to the same event multiple times
40787aec1d6eScindi 			 * only worry about the first one.
40797aec1d6eScindi 			 */
40807aec1d6eScindi 			if (ap->arrowp->tail->myevent->cached_state &
40817aec1d6eScindi 			    CAUSES_TESTED) {
40827aec1d6eScindi 				indent();
40837aec1d6eScindi 				out(O_ALTFP|O_VERB|O_NONL,
40847aec1d6eScindi 				    "  causes test already run for ");
40857aec1d6eScindi 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL,
40867aec1d6eScindi 				    ap->arrowp->tail->myevent);
40877aec1d6eScindi 				out(O_ALTFP|O_VERB, NULL);
40887aec1d6eScindi 				continue;
40897aec1d6eScindi 			}
40907aec1d6eScindi 
40917c478bd9Sstevel@tonic-gate 			/*
40927c478bd9Sstevel@tonic-gate 			 * see if false constraint prevents us
40937c478bd9Sstevel@tonic-gate 			 * from traversing this arrow
40947c478bd9Sstevel@tonic-gate 			 */
40957c478bd9Sstevel@tonic-gate 			platform_set_payloadnvp(ep->nvp);
409600d0963fSdilpreet 			if (checkconstraints(fmep, ap->arrowp) == 0)
40977aec1d6eScindi 				do_not_follow = 1;
40987c478bd9Sstevel@tonic-gate 			platform_set_payloadnvp(NULL);
40997c478bd9Sstevel@tonic-gate 			if (do_not_follow) {
41007c478bd9Sstevel@tonic-gate 				indent();
41017c478bd9Sstevel@tonic-gate 				out(O_ALTFP|O_VERB|O_NONL,
41027c478bd9Sstevel@tonic-gate 				    "  False arrow from ");
41037c478bd9Sstevel@tonic-gate 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL,
41047c478bd9Sstevel@tonic-gate 				    ap->arrowp->tail->myevent);
41057c478bd9Sstevel@tonic-gate 				out(O_ALTFP|O_VERB, NULL);
41067c478bd9Sstevel@tonic-gate 				continue;
41077c478bd9Sstevel@tonic-gate 			}
41087c478bd9Sstevel@tonic-gate 
41097aec1d6eScindi 			ap->arrowp->tail->myevent->cached_state |=
41107aec1d6eScindi 			    CAUSES_TESTED;
41117aec1d6eScindi 			tail_event = ap->arrowp->tail->myevent;
41127aec1d6eScindi 			fstate = hypothesise(fmep, tail_event, at_latest_by,
41137aec1d6eScindi 			    &my_delay);
41147c478bd9Sstevel@tonic-gate 
41157c478bd9Sstevel@tonic-gate 			switch (fstate) {
41167c478bd9Sstevel@tonic-gate 			case FME_WAIT:
41177c478bd9Sstevel@tonic-gate 				if (my_delay < overall_delay)
41187c478bd9Sstevel@tonic-gate 					overall_delay = my_delay;
41197c478bd9Sstevel@tonic-gate 				waiting_results++;
41207c478bd9Sstevel@tonic-gate 				break;
41217c478bd9Sstevel@tonic-gate 			case FME_CREDIBLE:
41227c478bd9Sstevel@tonic-gate 				credible_results++;
41237c478bd9Sstevel@tonic-gate 				break;
41247c478bd9Sstevel@tonic-gate 			case FME_DISPROVED:
41257c478bd9Sstevel@tonic-gate 				break;
41267c478bd9Sstevel@tonic-gate 			default:
41277c478bd9Sstevel@tonic-gate 				out(O_DIE, "Bug in causes_test");
41287c478bd9Sstevel@tonic-gate 			}
41297c478bd9Sstevel@tonic-gate 		}
41307c478bd9Sstevel@tonic-gate 	}
41317c478bd9Sstevel@tonic-gate 	/* compare against K */
41327c478bd9Sstevel@tonic-gate 	if (credible_results + waiting_results < k) {
41337c478bd9Sstevel@tonic-gate 		indent();
41347aec1d6eScindi 		out(O_ALTFP|O_VERB|O_NONL, "<-CAUSES DISPROVED ");
41357c478bd9Sstevel@tonic-gate 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
41367c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB, NULL);
41377c478bd9Sstevel@tonic-gate 		indent_pop();
41387c478bd9Sstevel@tonic-gate 		return (FME_DISPROVED);
41397c478bd9Sstevel@tonic-gate 	}
41407c478bd9Sstevel@tonic-gate 	if (waiting_results != 0) {
41417c478bd9Sstevel@tonic-gate 		*pdelay = overall_delay;
41427c478bd9Sstevel@tonic-gate 		indent();
41437aec1d6eScindi 		out(O_ALTFP|O_VERB|O_NONL, "<-CAUSES WAIT ");
41447c478bd9Sstevel@tonic-gate 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
41457c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB|O_NONL, " to ");
41467c478bd9Sstevel@tonic-gate 		ptree_timeval(O_ALTFP|O_VERB|O_NONL, &at_latest_by);
41477c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB, NULL);
41487c478bd9Sstevel@tonic-gate 		indent_pop();
41497c478bd9Sstevel@tonic-gate 		return (FME_WAIT);
41507c478bd9Sstevel@tonic-gate 	}
41517c478bd9Sstevel@tonic-gate 	indent();
41527aec1d6eScindi 	out(O_ALTFP|O_VERB|O_NONL, "<-CAUSES CREDIBLE ");
41537c478bd9Sstevel@tonic-gate 	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
41547c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB, NULL);
41557c478bd9Sstevel@tonic-gate 	indent_pop();
41567c478bd9Sstevel@tonic-gate 	return (FME_CREDIBLE);
41577c478bd9Sstevel@tonic-gate }
41587c478bd9Sstevel@tonic-gate 
41597c478bd9Sstevel@tonic-gate static enum fme_state
41607c478bd9Sstevel@tonic-gate hypothesise(struct fme *fmep, struct event *ep,
41617aec1d6eScindi 	unsigned long long at_latest_by, unsigned long long *pdelay)
41627c478bd9Sstevel@tonic-gate {
41637c478bd9Sstevel@tonic-gate 	enum fme_state rtr, otr;
41647c478bd9Sstevel@tonic-gate 	unsigned long long my_delay;
41657c478bd9Sstevel@tonic-gate 	unsigned long long overall_delay = TIMEVAL_EVENTUALLY;
41667c478bd9Sstevel@tonic-gate 
41677c478bd9Sstevel@tonic-gate 	stats_counter_bump(fmep->Hcallcount);
41687c478bd9Sstevel@tonic-gate 	indent_push("  H");
41697c478bd9Sstevel@tonic-gate 	indent();
41707c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB|O_NONL, "->");
41717c478bd9Sstevel@tonic-gate 	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
41727c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB|O_NONL, ", at latest by: ");
41737c478bd9Sstevel@tonic-gate 	ptree_timeval(O_ALTFP|O_VERB|O_NONL, &at_latest_by);
41747c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB, NULL);
41757c478bd9Sstevel@tonic-gate 
41767aec1d6eScindi 	rtr = requirements_test(fmep, ep, at_latest_by, &my_delay);
41777c478bd9Sstevel@tonic-gate 	if ((rtr == FME_WAIT) && (my_delay < overall_delay))
41787c478bd9Sstevel@tonic-gate 		overall_delay = my_delay;
41797c478bd9Sstevel@tonic-gate 	if (rtr != FME_DISPROVED) {
41807c478bd9Sstevel@tonic-gate 		if (is_problem(ep->t)) {
41817aec1d6eScindi 			otr = effects_test(fmep, ep, at_latest_by, &my_delay);
41827c478bd9Sstevel@tonic-gate 			if (otr != FME_DISPROVED) {
4183b5016cbbSstephh 				if (fmep->peek == 0 && ep->is_suspect == 0) {
41847c478bd9Sstevel@tonic-gate 					ep->suspects = fmep->suspects;
4185b5016cbbSstephh 					ep->is_suspect = 1;
41867c478bd9Sstevel@tonic-gate 					fmep->suspects = ep;
41877c478bd9Sstevel@tonic-gate 					fmep->nsuspects++;
41887c478bd9Sstevel@tonic-gate 				}
41897c478bd9Sstevel@tonic-gate 			}
41907c478bd9Sstevel@tonic-gate 		} else
41917c478bd9Sstevel@tonic-gate 			otr = causes_test(fmep, ep, at_latest_by, &my_delay);
41927c478bd9Sstevel@tonic-gate 		if ((otr == FME_WAIT) && (my_delay < overall_delay))
41937c478bd9Sstevel@tonic-gate 			overall_delay = my_delay;
41947c478bd9Sstevel@tonic-gate 		if ((otr != FME_DISPROVED) &&
41957c478bd9Sstevel@tonic-gate 		    ((rtr == FME_WAIT) || (otr == FME_WAIT)))
41967c478bd9Sstevel@tonic-gate 			*pdelay = overall_delay;
41977c478bd9Sstevel@tonic-gate 	}
41987c478bd9Sstevel@tonic-gate 	if (rtr == FME_DISPROVED) {
41997c478bd9Sstevel@tonic-gate 		indent();
42007c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB|O_NONL, "<-DISPROVED ");
42017c478bd9Sstevel@tonic-gate 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
42027c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB, " (doesn't meet requirements)");
42037c478bd9Sstevel@tonic-gate 		indent_pop();
42047c478bd9Sstevel@tonic-gate 		return (FME_DISPROVED);
42057c478bd9Sstevel@tonic-gate 	}
42067c478bd9Sstevel@tonic-gate 	if ((otr == FME_DISPROVED) && is_problem(ep->t)) {
42077c478bd9Sstevel@tonic-gate 		indent();
42087c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB|O_NONL, "<-DISPROVED ");
42097c478bd9Sstevel@tonic-gate 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
42107c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB, " (doesn't explain all reports)");
42117c478bd9Sstevel@tonic-gate 		indent_pop();
42127c478bd9Sstevel@tonic-gate 		return (FME_DISPROVED);
42137c478bd9Sstevel@tonic-gate 	}
42147c478bd9Sstevel@tonic-gate 	if (otr == FME_DISPROVED) {
42157c478bd9Sstevel@tonic-gate 		indent();
42167c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB|O_NONL, "<-DISPROVED ");
42177c478bd9Sstevel@tonic-gate 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
42187c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB, " (causes are not credible)");
42197c478bd9Sstevel@tonic-gate 		indent_pop();
42207c478bd9Sstevel@tonic-gate 		return (FME_DISPROVED);
42217c478bd9Sstevel@tonic-gate 	}
42227c478bd9Sstevel@tonic-gate 	if ((rtr == FME_WAIT) || (otr == FME_WAIT)) {
42237c478bd9Sstevel@tonic-gate 		indent();
42247c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB|O_NONL, "<-WAIT ");
42257c478bd9Sstevel@tonic-gate 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
42267c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB|O_NONL, " to ");
42277c478bd9Sstevel@tonic-gate 		ptree_timeval(O_ALTFP|O_VERB|O_NONL, &overall_delay);
42287c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB, NULL);
42297c478bd9Sstevel@tonic-gate 		indent_pop();
42307c478bd9Sstevel@tonic-gate 		return (FME_WAIT);
42317c478bd9Sstevel@tonic-gate 	}
42327c478bd9Sstevel@tonic-gate 	indent();
42337c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB|O_NONL, "<-CREDIBLE ");
42347c478bd9Sstevel@tonic-gate 	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
42357c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB, NULL);
42367c478bd9Sstevel@tonic-gate 	indent_pop();
42377c478bd9Sstevel@tonic-gate 	return (FME_CREDIBLE);
42387c478bd9Sstevel@tonic-gate }
42397aec1d6eScindi 
42407aec1d6eScindi /*
42417aec1d6eScindi  * fme_istat_load -- reconstitute any persistent istats
42427aec1d6eScindi  */
42437aec1d6eScindi void
42447aec1d6eScindi fme_istat_load(fmd_hdl_t *hdl)
42457aec1d6eScindi {
42467aec1d6eScindi 	int sz;
42477aec1d6eScindi 	char *sbuf;
42487aec1d6eScindi 	char *ptr;
42497aec1d6eScindi 
42507aec1d6eScindi 	if ((sz = fmd_buf_size(hdl, NULL, WOBUF_ISTATS)) == 0) {
42517aec1d6eScindi 		out(O_ALTFP, "fme_istat_load: No stats");
42527aec1d6eScindi 		return;
42537aec1d6eScindi 	}
42547aec1d6eScindi 
42557aec1d6eScindi 	sbuf = alloca(sz);
42567aec1d6eScindi 
42577aec1d6eScindi 	fmd_buf_read(hdl, NULL, WOBUF_ISTATS, sbuf, sz);
42587aec1d6eScindi 
42597aec1d6eScindi 	/*
42607aec1d6eScindi 	 * pick apart the serialized stats
42617aec1d6eScindi 	 *
42627aec1d6eScindi 	 * format is:
42637aec1d6eScindi 	 *	<class-name>, '@', <path>, '\0', <value>, '\0'
42647aec1d6eScindi 	 * for example:
42657aec1d6eScindi 	 *	"stat.first@stat0/path0\02\0stat.second@stat0/path1\023\0"
42667aec1d6eScindi 	 *
42677aec1d6eScindi 	 * since this is parsing our own serialized data, any parsing issues
42687aec1d6eScindi 	 * are fatal, so we check for them all with ASSERT() below.
42697aec1d6eScindi 	 */
42707aec1d6eScindi 	ptr = sbuf;
42717aec1d6eScindi 	while (ptr < &sbuf[sz]) {
42727aec1d6eScindi 		char *sepptr;
42737aec1d6eScindi 		struct node *np;
42747aec1d6eScindi 		int val;
42757aec1d6eScindi 
42767aec1d6eScindi 		sepptr = strchr(ptr, '@');
42777aec1d6eScindi 		ASSERT(sepptr != NULL);
42787aec1d6eScindi 		*sepptr = '\0';
42797aec1d6eScindi 
42807aec1d6eScindi 		/* construct the event */
42817aec1d6eScindi 		np = newnode(T_EVENT, NULL, 0);
42827aec1d6eScindi 		np->u.event.ename = newnode(T_NAME, NULL, 0);
42837aec1d6eScindi 		np->u.event.ename->u.name.t = N_STAT;
42847aec1d6eScindi 		np->u.event.ename->u.name.s = stable(ptr);
42857aec1d6eScindi 		np->u.event.ename->u.name.it = IT_ENAME;
42867aec1d6eScindi 		np->u.event.ename->u.name.last = np->u.event.ename;
42877aec1d6eScindi 
42887aec1d6eScindi 		ptr = sepptr + 1;
42897aec1d6eScindi 		ASSERT(ptr < &sbuf[sz]);
42907aec1d6eScindi 		ptr += strlen(ptr);
42917aec1d6eScindi 		ptr++;	/* move past the '\0' separating path from value */
42927aec1d6eScindi 		ASSERT(ptr < &sbuf[sz]);
42937aec1d6eScindi 		ASSERT(isdigit(*ptr));
42947aec1d6eScindi 		val = atoi(ptr);
42957aec1d6eScindi 		ASSERT(val > 0);
42967aec1d6eScindi 		ptr += strlen(ptr);
42977aec1d6eScindi 		ptr++;	/* move past the final '\0' for this entry */
42987aec1d6eScindi 
42997aec1d6eScindi 		np->u.event.epname = pathstring2epnamenp(sepptr + 1);
43007aec1d6eScindi 		ASSERT(np->u.event.epname != NULL);
43017aec1d6eScindi 
43027aec1d6eScindi 		istat_bump(np, val);
43037aec1d6eScindi 		tree_free(np);
43047aec1d6eScindi 	}
43057aec1d6eScindi 
43067aec1d6eScindi 	istat_save();
43077aec1d6eScindi }
4308