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 /*
23f5961f52SAdrian Frost  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
2433f5ff17SMilan Jurik  * Copyright 2012 Milan Jurik. All rights reserved.
25a3f6a2a4SRob Johnston  * Copyright (c) 2018, Joyent, Inc.
267c478bd9Sstevel@tonic-gate  *
277c478bd9Sstevel@tonic-gate  * fme.c -- fault management exercise module
287c478bd9Sstevel@tonic-gate  *
297c478bd9Sstevel@tonic-gate  * this module provides the simulated fault management exercise.
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <stdio.h>
337c478bd9Sstevel@tonic-gate #include <stdlib.h>
347c478bd9Sstevel@tonic-gate #include <string.h>
357c478bd9Sstevel@tonic-gate #include <strings.h>
367c478bd9Sstevel@tonic-gate #include <ctype.h>
377c478bd9Sstevel@tonic-gate #include <alloca.h>
387c478bd9Sstevel@tonic-gate #include <libnvpair.h>
397c478bd9Sstevel@tonic-gate #include <sys/fm/protocol.h>
407c478bd9Sstevel@tonic-gate #include <fm/fmd_api.h>
41da40b264SAlex Wilson #include <fm/libtopo.h>
427c478bd9Sstevel@tonic-gate #include "alloc.h"
437c478bd9Sstevel@tonic-gate #include "out.h"
447c478bd9Sstevel@tonic-gate #include "stats.h"
457c478bd9Sstevel@tonic-gate #include "stable.h"
467c478bd9Sstevel@tonic-gate #include "literals.h"
477c478bd9Sstevel@tonic-gate #include "lut.h"
487c478bd9Sstevel@tonic-gate #include "tree.h"
497c478bd9Sstevel@tonic-gate #include "ptree.h"
507c478bd9Sstevel@tonic-gate #include "itree.h"
517c478bd9Sstevel@tonic-gate #include "ipath.h"
527c478bd9Sstevel@tonic-gate #include "fme.h"
537c478bd9Sstevel@tonic-gate #include "evnv.h"
547c478bd9Sstevel@tonic-gate #include "eval.h"
557c478bd9Sstevel@tonic-gate #include "config.h"
567c478bd9Sstevel@tonic-gate #include "platform.h"
57e5ba14ffSstephh #include "esclex.h"
587c478bd9Sstevel@tonic-gate 
59*cfc9ef1dSToomas Soome struct lut *Istats;
60*cfc9ef1dSToomas Soome struct lut *SerdEngines;
61*cfc9ef1dSToomas Soome nvlist_t *Action_nvl;
62*cfc9ef1dSToomas Soome 
637c478bd9Sstevel@tonic-gate /* imported from eft.c... */
647c478bd9Sstevel@tonic-gate extern hrtime_t Hesitate;
65e5ba14ffSstephh extern char *Serd_Override;
667c478bd9Sstevel@tonic-gate extern nv_alloc_t Eft_nv_hdl;
670cc1f05eSjrutt extern int Max_fme;
687aec1d6eScindi extern fmd_hdl_t *Hdl;
697aec1d6eScindi 
707aec1d6eScindi static int Istat_need_save;
71b5016cbbSstephh static int Serd_need_save;
7208f6c065Sgavinm void istat_save(void);
73b5016cbbSstephh void serd_save(void);
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /* fme under construction is global so we can free it on module abort */
767c478bd9Sstevel@tonic-gate static struct fme *Nfmep;
777c478bd9Sstevel@tonic-gate 
7832d4e834STarik Soydan static int Undiag_reason = UD_VAL_UNKNOWN;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate static int Nextid = 0;
817c478bd9Sstevel@tonic-gate 
820cc1f05eSjrutt static int Open_fme_count = 0;	/* Count of open FMEs */
830cc1f05eSjrutt 
847c478bd9Sstevel@tonic-gate /* list of fault management exercises underway */
857c478bd9Sstevel@tonic-gate static struct fme {
867c478bd9Sstevel@tonic-gate 	struct fme *next;		/* next exercise */
877c478bd9Sstevel@tonic-gate 	unsigned long long ull;		/* time when fme was created */
887c478bd9Sstevel@tonic-gate 	int id;				/* FME id */
89e5ba14ffSstephh 	struct config *config;		/* cooked configuration data */
907c478bd9Sstevel@tonic-gate 	struct lut *eventtree;		/* propagation tree for this FME */
917c478bd9Sstevel@tonic-gate 	/*
927c478bd9Sstevel@tonic-gate 	 * The initial error report that created this FME is kept in
937c478bd9Sstevel@tonic-gate 	 * two forms.  e0 points to the instance tree node and is used
947c478bd9Sstevel@tonic-gate 	 * by fme_eval() as the starting point for the inference
957c478bd9Sstevel@tonic-gate 	 * algorithm.  e0r is the event handle FMD passed to us when
967c478bd9Sstevel@tonic-gate 	 * the ereport first arrived and is used when setting timers,
977c478bd9Sstevel@tonic-gate 	 * which are always relative to the time of this initial
987c478bd9Sstevel@tonic-gate 	 * report.
997c478bd9Sstevel@tonic-gate 	 */
1007c478bd9Sstevel@tonic-gate 	struct event *e0;
1017c478bd9Sstevel@tonic-gate 	fmd_event_t *e0r;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	id_t    timer;			/* for setting an fmd time-out */
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	struct event *ecurrent;		/* ereport under consideration */
1067c478bd9Sstevel@tonic-gate 	struct event *suspects;		/* current suspect list */
1077c478bd9Sstevel@tonic-gate 	struct event *psuspects;	/* previous suspect list */
1087c478bd9Sstevel@tonic-gate 	int nsuspects;			/* count of suspects */
1097c478bd9Sstevel@tonic-gate 	int posted_suspects;		/* true if we've posted a diagnosis */
1107c478bd9Sstevel@tonic-gate 	int uniqobs;			/* number of unique events observed */
1117c478bd9Sstevel@tonic-gate 	int peek;			/* just peeking, don't track suspects */
1120cc1f05eSjrutt 	int overflow;			/* true if overflow FME */
1137c478bd9Sstevel@tonic-gate 	enum fme_state {
1147c478bd9Sstevel@tonic-gate 		FME_NOTHING = 5000,	/* not evaluated yet */
1157c478bd9Sstevel@tonic-gate 		FME_WAIT,		/* need to wait for more info */
1167c478bd9Sstevel@tonic-gate 		FME_CREDIBLE,		/* suspect list is credible */
1177aec1d6eScindi 		FME_DISPROVED,		/* no valid suspects found */
1187aec1d6eScindi 		FME_DEFERRED		/* don't know yet (k-count not met) */
1197c478bd9Sstevel@tonic-gate 	} state;
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	unsigned long long pull;	/* time passed since created */
1227c478bd9Sstevel@tonic-gate 	unsigned long long wull;	/* wait until this time for re-eval */
1237c478bd9Sstevel@tonic-gate 	struct event *observations;	/* observation list */
1247c478bd9Sstevel@tonic-gate 	struct lut *globals;		/* values of global variables */
1257c478bd9Sstevel@tonic-gate 	/* fmd interfacing */
1267c478bd9Sstevel@tonic-gate 	fmd_hdl_t *hdl;			/* handle for talking with fmd */
1277c478bd9Sstevel@tonic-gate 	fmd_case_t *fmcase;		/* what fmd 'case' we associate with */
1287c478bd9Sstevel@tonic-gate 	/* stats */
1297c478bd9Sstevel@tonic-gate 	struct stats *Rcount;
1307c478bd9Sstevel@tonic-gate 	struct stats *Hcallcount;
1317c478bd9Sstevel@tonic-gate 	struct stats *Rcallcount;
1327c478bd9Sstevel@tonic-gate 	struct stats *Ccallcount;
1337c478bd9Sstevel@tonic-gate 	struct stats *Ecallcount;
1347c478bd9Sstevel@tonic-gate 	struct stats *Tcallcount;
1357c478bd9Sstevel@tonic-gate 	struct stats *Marrowcount;
1367c478bd9Sstevel@tonic-gate 	struct stats *diags;
1377c478bd9Sstevel@tonic-gate } *FMElist, *EFMElist, *ClosedFMEs;
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate static struct case_list {
1407c478bd9Sstevel@tonic-gate 	fmd_case_t *fmcase;
1417c478bd9Sstevel@tonic-gate 	struct case_list *next;
1427c478bd9Sstevel@tonic-gate } *Undiagablecaselist;
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate static void fme_eval(struct fme *fmep, fmd_event_t *ffep);
1457c478bd9Sstevel@tonic-gate static enum fme_state hypothesise(struct fme *fmep, struct event *ep,
1467aec1d6eScindi 	unsigned long long at_latest_by, unsigned long long *pdelay);
1477c478bd9Sstevel@tonic-gate static struct node *eventprop_lookup(struct event *ep, const char *propname);
1487c478bd9Sstevel@tonic-gate static struct node *pathstring2epnamenp(char *path);
149b5016cbbSstephh static void publish_undiagnosable(fmd_hdl_t *hdl, fmd_event_t *ffep,
150705e9f42SStephen Hanson 	fmd_case_t *fmcase, nvlist_t *detector, char *arg);
151705e9f42SStephen Hanson static char *undiag_2reason_str(int ud, char *arg);
15232d4e834STarik Soydan static const char *undiag_2defect_str(int ud);
1537c478bd9Sstevel@tonic-gate static void restore_suspects(struct fme *fmep);
1547c478bd9Sstevel@tonic-gate static void save_suspects(struct fme *fmep);
1557c478bd9Sstevel@tonic-gate static void destroy_fme(struct fme *f);
1567c478bd9Sstevel@tonic-gate static void fme_receive_report(fmd_hdl_t *hdl, fmd_event_t *ffep,
1577c478bd9Sstevel@tonic-gate     const char *eventstring, const struct ipath *ipp, nvlist_t *nvl);
15808f6c065Sgavinm static void istat_counter_reset_cb(struct istat_entry *entp,
15908f6c065Sgavinm     struct stats *statp, const struct ipath *ipp);
160e5ba14ffSstephh static void istat_counter_topo_chg_cb(struct istat_entry *entp,
161e5ba14ffSstephh     struct stats *statp, void *unused);
162b5016cbbSstephh static void serd_reset_cb(struct serd_entry *entp, void *unused,
163b5016cbbSstephh     const struct ipath *ipp);
164e5ba14ffSstephh static void serd_topo_chg_cb(struct serd_entry *entp, void *unused,
165e5ba14ffSstephh     void *unused2);
166b5016cbbSstephh static void destroy_fme_bufs(struct fme *fp);
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate static struct fme *
alloc_fme(void)1697c478bd9Sstevel@tonic-gate alloc_fme(void)
1707c478bd9Sstevel@tonic-gate {
1717c478bd9Sstevel@tonic-gate 	struct fme *fmep;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	fmep = MALLOC(sizeof (*fmep));
1747c478bd9Sstevel@tonic-gate 	bzero(fmep, sizeof (*fmep));
1757c478bd9Sstevel@tonic-gate 	return (fmep);
1767c478bd9Sstevel@tonic-gate }
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate /*
1797c478bd9Sstevel@tonic-gate  * fme_ready -- called when all initialization of the FME (except for
1807c478bd9Sstevel@tonic-gate  *	stats) has completed successfully.  Adds the fme to global lists
1817c478bd9Sstevel@tonic-gate  *	and establishes its stats.
1827c478bd9Sstevel@tonic-gate  */
1837c478bd9Sstevel@tonic-gate static struct fme *
fme_ready(struct fme * fmep)1847c478bd9Sstevel@tonic-gate fme_ready(struct fme *fmep)
1857c478bd9Sstevel@tonic-gate {
1867c478bd9Sstevel@tonic-gate 	char nbuf[100];
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	Nfmep = NULL;	/* don't need to free this on module abort now */
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	if (EFMElist) {
1917c478bd9Sstevel@tonic-gate 		EFMElist->next = fmep;
1927c478bd9Sstevel@tonic-gate 		EFMElist = fmep;
1937c478bd9Sstevel@tonic-gate 	} else
1947c478bd9Sstevel@tonic-gate 		FMElist = EFMElist = fmep;
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	(void) sprintf(nbuf, "fme%d.Rcount", fmep->id);
1977c478bd9Sstevel@tonic-gate 	fmep->Rcount = stats_new_counter(nbuf, "ereports received", 0);
1987c478bd9Sstevel@tonic-gate 	(void) sprintf(nbuf, "fme%d.Hcall", fmep->id);
1997c478bd9Sstevel@tonic-gate 	fmep->Hcallcount = stats_new_counter(nbuf, "calls to hypothesise()", 1);
2007c478bd9Sstevel@tonic-gate 	(void) sprintf(nbuf, "fme%d.Rcall", fmep->id);
2017c478bd9Sstevel@tonic-gate 	fmep->Rcallcount = stats_new_counter(nbuf,
2027c478bd9Sstevel@tonic-gate 	    "calls to requirements_test()", 1);
2037c478bd9Sstevel@tonic-gate 	(void) sprintf(nbuf, "fme%d.Ccall", fmep->id);
2047c478bd9Sstevel@tonic-gate 	fmep->Ccallcount = stats_new_counter(nbuf, "calls to causes_test()", 1);
2057c478bd9Sstevel@tonic-gate 	(void) sprintf(nbuf, "fme%d.Ecall", fmep->id);
2067c478bd9Sstevel@tonic-gate 	fmep->Ecallcount =
2077c478bd9Sstevel@tonic-gate 	    stats_new_counter(nbuf, "calls to effects_test()", 1);
2087c478bd9Sstevel@tonic-gate 	(void) sprintf(nbuf, "fme%d.Tcall", fmep->id);
2097c478bd9Sstevel@tonic-gate 	fmep->Tcallcount = stats_new_counter(nbuf, "calls to triggered()", 1);
2107c478bd9Sstevel@tonic-gate 	(void) sprintf(nbuf, "fme%d.Marrow", fmep->id);
2117c478bd9Sstevel@tonic-gate 	fmep->Marrowcount = stats_new_counter(nbuf,
2127c478bd9Sstevel@tonic-gate 	    "arrows marked by mark_arrows()", 1);
2137c478bd9Sstevel@tonic-gate 	(void) sprintf(nbuf, "fme%d.diags", fmep->id);
2147c478bd9Sstevel@tonic-gate 	fmep->diags = stats_new_counter(nbuf, "suspect lists diagnosed", 0);
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB2, "newfme: config snapshot contains...");
217e5ba14ffSstephh 	config_print(O_ALTFP|O_VERB2, fmep->config);
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	return (fmep);
2207c478bd9Sstevel@tonic-gate }
2217c478bd9Sstevel@tonic-gate 
222b5016cbbSstephh extern void ipath_dummy_lut(struct arrow *);
223b5016cbbSstephh extern struct lut *itree_create_dummy(const char *, const struct ipath *);
224b5016cbbSstephh 
225b5016cbbSstephh /* ARGSUSED */
226b5016cbbSstephh static void
set_needed_arrows(struct event * ep,struct event * ep2,struct fme * fmep)227b5016cbbSstephh set_needed_arrows(struct event *ep, struct event *ep2, struct fme *fmep)
228b5016cbbSstephh {
229b5016cbbSstephh 	struct bubble *bp;
230b5016cbbSstephh 	struct arrowlist *ap;
231b5016cbbSstephh 
232b5016cbbSstephh 	for (bp = itree_next_bubble(ep, NULL); bp;
233b5016cbbSstephh 	    bp = itree_next_bubble(ep, bp)) {
234b5016cbbSstephh 		if (bp->t != B_FROM)
235b5016cbbSstephh 			continue;
236b5016cbbSstephh 		for (ap = itree_next_arrow(bp, NULL); ap;
237b5016cbbSstephh 		    ap = itree_next_arrow(bp, ap)) {
238b5016cbbSstephh 			ap->arrowp->pnode->u.arrow.needed = 1;
239b5016cbbSstephh 			ipath_dummy_lut(ap->arrowp);
240b5016cbbSstephh 		}
241b5016cbbSstephh 	}
242b5016cbbSstephh }
243b5016cbbSstephh 
244b5016cbbSstephh /* ARGSUSED */
245b5016cbbSstephh static void
unset_needed_arrows(struct event * ep,struct event * ep2,struct fme * fmep)246b5016cbbSstephh unset_needed_arrows(struct event *ep, struct event *ep2, struct fme *fmep)
247b5016cbbSstephh {
248b5016cbbSstephh 	struct bubble *bp;
249b5016cbbSstephh 	struct arrowlist *ap;
250b5016cbbSstephh 
251b5016cbbSstephh 	for (bp = itree_next_bubble(ep, NULL); bp;
252b5016cbbSstephh 	    bp = itree_next_bubble(ep, bp)) {
253b5016cbbSstephh 		if (bp->t != B_FROM)
254b5016cbbSstephh 			continue;
255b5016cbbSstephh 		for (ap = itree_next_arrow(bp, NULL); ap;
256b5016cbbSstephh 		    ap = itree_next_arrow(bp, ap))
257b5016cbbSstephh 			ap->arrowp->pnode->u.arrow.needed = 0;
258b5016cbbSstephh 	}
259b5016cbbSstephh }
260b5016cbbSstephh 
261b5016cbbSstephh static void globals_destructor(void *left, void *right, void *arg);
262b5016cbbSstephh static void clear_arrows(struct event *ep, struct event *ep2, struct fme *fmep);
263b5016cbbSstephh 
264705e9f42SStephen Hanson static boolean_t
prune_propagations(const char * e0class,const struct ipath * e0ipp)265b5016cbbSstephh prune_propagations(const char *e0class, const struct ipath *e0ipp)
266b5016cbbSstephh {
267b5016cbbSstephh 	char nbuf[100];
268b5016cbbSstephh 	unsigned long long my_delay = TIMEVAL_EVENTUALLY;
269b5016cbbSstephh 	extern struct lut *Usednames;
270b5016cbbSstephh 
271b5016cbbSstephh 	Nfmep = alloc_fme();
272b5016cbbSstephh 	Nfmep->id = Nextid;
273b5016cbbSstephh 	Nfmep->state = FME_NOTHING;
274b5016cbbSstephh 	Nfmep->eventtree = itree_create_dummy(e0class, e0ipp);
275b5016cbbSstephh 	if ((Nfmep->e0 =
276b5016cbbSstephh 	    itree_lookup(Nfmep->eventtree, e0class, e0ipp)) == NULL) {
277b5016cbbSstephh 		itree_free(Nfmep->eventtree);
278b5016cbbSstephh 		FREE(Nfmep);
279b5016cbbSstephh 		Nfmep = NULL;
280705e9f42SStephen Hanson 		return (B_FALSE);
281b5016cbbSstephh 	}
282b5016cbbSstephh 	Nfmep->ecurrent = Nfmep->observations = Nfmep->e0;
283b5016cbbSstephh 	Nfmep->e0->count++;
284b5016cbbSstephh 
285b5016cbbSstephh 	(void) sprintf(nbuf, "fme%d.Rcount", Nfmep->id);
286b5016cbbSstephh 	Nfmep->Rcount = stats_new_counter(nbuf, "ereports received", 0);
287b5016cbbSstephh 	(void) sprintf(nbuf, "fme%d.Hcall", Nfmep->id);
288b5016cbbSstephh 	Nfmep->Hcallcount =
289b5016cbbSstephh 	    stats_new_counter(nbuf, "calls to hypothesise()", 1);
290b5016cbbSstephh 	(void) sprintf(nbuf, "fme%d.Rcall", Nfmep->id);
291b5016cbbSstephh 	Nfmep->Rcallcount = stats_new_counter(nbuf,
292b5016cbbSstephh 	    "calls to requirements_test()", 1);
293b5016cbbSstephh 	(void) sprintf(nbuf, "fme%d.Ccall", Nfmep->id);
294b5016cbbSstephh 	Nfmep->Ccallcount =
295b5016cbbSstephh 	    stats_new_counter(nbuf, "calls to causes_test()", 1);
296b5016cbbSstephh 	(void) sprintf(nbuf, "fme%d.Ecall", Nfmep->id);
297b5016cbbSstephh 	Nfmep->Ecallcount =
298b5016cbbSstephh 	    stats_new_counter(nbuf, "calls to effects_test()", 1);
299b5016cbbSstephh 	(void) sprintf(nbuf, "fme%d.Tcall", Nfmep->id);
300b5016cbbSstephh 	Nfmep->Tcallcount = stats_new_counter(nbuf, "calls to triggered()", 1);
301b5016cbbSstephh 	(void) sprintf(nbuf, "fme%d.Marrow", Nfmep->id);
302b5016cbbSstephh 	Nfmep->Marrowcount = stats_new_counter(nbuf,
303b5016cbbSstephh 	    "arrows marked by mark_arrows()", 1);
304b5016cbbSstephh 	(void) sprintf(nbuf, "fme%d.diags", Nfmep->id);
305b5016cbbSstephh 	Nfmep->diags = stats_new_counter(nbuf, "suspect lists diagnosed", 0);
306b5016cbbSstephh 
307b5016cbbSstephh 	Nfmep->peek = 1;
308b5016cbbSstephh 	lut_walk(Nfmep->eventtree, (lut_cb)unset_needed_arrows, (void *)Nfmep);
309b5016cbbSstephh 	lut_free(Usednames, NULL, NULL);
310b5016cbbSstephh 	Usednames = NULL;
311b5016cbbSstephh 	lut_walk(Nfmep->eventtree, (lut_cb)clear_arrows, (void *)Nfmep);
312b5016cbbSstephh 	(void) hypothesise(Nfmep, Nfmep->e0, Nfmep->ull, &my_delay);
313b5016cbbSstephh 	itree_prune(Nfmep->eventtree);
314b5016cbbSstephh 	lut_walk(Nfmep->eventtree, (lut_cb)set_needed_arrows, (void *)Nfmep);
315b5016cbbSstephh 
316b5016cbbSstephh 	stats_delete(Nfmep->Rcount);
317b5016cbbSstephh 	stats_delete(Nfmep->Hcallcount);
318b5016cbbSstephh 	stats_delete(Nfmep->Rcallcount);
319b5016cbbSstephh 	stats_delete(Nfmep->Ccallcount);
320b5016cbbSstephh 	stats_delete(Nfmep->Ecallcount);
321b5016cbbSstephh 	stats_delete(Nfmep->Tcallcount);
322b5016cbbSstephh 	stats_delete(Nfmep->Marrowcount);
323b5016cbbSstephh 	stats_delete(Nfmep->diags);
324b5016cbbSstephh 	itree_free(Nfmep->eventtree);
325b5016cbbSstephh 	lut_free(Nfmep->globals, globals_destructor, NULL);
326b5016cbbSstephh 	FREE(Nfmep);
327705e9f42SStephen Hanson 	return (B_TRUE);
328b5016cbbSstephh }
329b5016cbbSstephh 
3307c478bd9Sstevel@tonic-gate static struct fme *
newfme(const char * e0class,const struct ipath * e0ipp,fmd_hdl_t * hdl,fmd_case_t * fmcase,fmd_event_t * ffep,nvlist_t * nvl)331b5016cbbSstephh newfme(const char *e0class, const struct ipath *e0ipp, fmd_hdl_t *hdl,
332a3f6a2a4SRob Johnston     fmd_case_t *fmcase, fmd_event_t *ffep, nvlist_t *nvl)
3337c478bd9Sstevel@tonic-gate {
3347c478bd9Sstevel@tonic-gate 	struct cfgdata *cfgdata;
335b5016cbbSstephh 	int init_size;
336b5016cbbSstephh 	extern int alloc_total();
337705e9f42SStephen Hanson 	nvlist_t *detector = NULL;
338705e9f42SStephen Hanson 	char *pathstr;
339705e9f42SStephen Hanson 	char *arg;
340705e9f42SStephen Hanson 
341705e9f42SStephen Hanson 	/*
342705e9f42SStephen Hanson 	 * First check if e0ipp is actually in the topology so we can give a
343705e9f42SStephen Hanson 	 * more useful error message.
344705e9f42SStephen Hanson 	 */
345705e9f42SStephen Hanson 	ipathlastcomp(e0ipp);
346705e9f42SStephen Hanson 	pathstr = ipath2str(NULL, e0ipp);
347705e9f42SStephen Hanson 	cfgdata = config_snapshot();
348da40b264SAlex Wilson 	platform_unit_translate(0, cfgdata->cooked, TOPO_PROP_RESOURCE,
349705e9f42SStephen Hanson 	    &detector, pathstr);
350705e9f42SStephen Hanson 	FREE(pathstr);
351705e9f42SStephen Hanson 	structconfig_free(cfgdata->cooked);
352705e9f42SStephen Hanson 	config_free(cfgdata);
353705e9f42SStephen Hanson 	if (detector == NULL) {
354f5961f52SAdrian Frost 		/* See if class permits silent discard on unknown component. */
355f5961f52SAdrian Frost 		if (lut_lookup(Ereportenames_discard, (void *)e0class, NULL)) {
356f5961f52SAdrian Frost 			out(O_ALTFP|O_VERB2, "Unable to map \"%s\" ereport "
357f5961f52SAdrian Frost 			    "to component path, but silent discard allowed.",
358f5961f52SAdrian Frost 			    e0class);
359a3f6a2a4SRob Johnston 			fmd_case_close(hdl, fmcase);
360f5961f52SAdrian Frost 		} else {
361f5961f52SAdrian Frost 			Undiag_reason = UD_VAL_BADEVENTPATH;
362f5961f52SAdrian Frost 			(void) nvlist_lookup_nvlist(nvl, FM_EREPORT_DETECTOR,
363f5961f52SAdrian Frost 			    &detector);
364f5961f52SAdrian Frost 			arg = ipath2str(e0class, e0ipp);
365f5961f52SAdrian Frost 			publish_undiagnosable(hdl, ffep, fmcase, detector, arg);
366f5961f52SAdrian Frost 			FREE(arg);
367f5961f52SAdrian Frost 		}
368705e9f42SStephen Hanson 		return (NULL);
369705e9f42SStephen Hanson 	}
370705e9f42SStephen Hanson 
371705e9f42SStephen Hanson 	/*
372705e9f42SStephen Hanson 	 * Next run a quick first pass of the rules with a dummy config. This
373705e9f42SStephen Hanson 	 * allows us to prune those rules which can't possibly cause this
374705e9f42SStephen Hanson 	 * ereport.
375705e9f42SStephen Hanson 	 */
376705e9f42SStephen Hanson 	if (!prune_propagations(e0class, e0ipp)) {
377705e9f42SStephen Hanson 		/*
378705e9f42SStephen Hanson 		 * The fault class must have been in the rules or we would
379705e9f42SStephen Hanson 		 * not have registered for it (and got a "nosub"), and the
380705e9f42SStephen Hanson 		 * pathname must be in the topology or we would have failed the
381705e9f42SStephen Hanson 		 * previous test. So to get here means the combination of
382705e9f42SStephen Hanson 		 * class and pathname in the ereport must be invalid.
383705e9f42SStephen Hanson 		 */
384705e9f42SStephen Hanson 		Undiag_reason = UD_VAL_BADEVENTCLASS;
385705e9f42SStephen Hanson 		arg = ipath2str(e0class, e0ipp);
386705e9f42SStephen Hanson 		publish_undiagnosable(hdl, ffep, fmcase, detector, arg);
387705e9f42SStephen Hanson 		nvlist_free(detector);
388705e9f42SStephen Hanson 		FREE(arg);
389705e9f42SStephen Hanson 		return (NULL);
390705e9f42SStephen Hanson 	}
3917c478bd9Sstevel@tonic-gate 
392705e9f42SStephen Hanson 	/*
393705e9f42SStephen Hanson 	 * Now go ahead and create the real fme using the pruned rules.
394705e9f42SStephen Hanson 	 */
395b5016cbbSstephh 	init_size = alloc_total();
396b5016cbbSstephh 	out(O_ALTFP|O_STAMP, "start config_snapshot using %d bytes", init_size);
39735f59e50SStephen Hanson 	nvlist_free(detector);
39835f59e50SStephen Hanson 	pathstr = ipath2str(NULL, e0ipp);
39932d4e834STarik Soydan 	cfgdata = config_snapshot();
400da40b264SAlex Wilson 	platform_unit_translate(0, cfgdata->cooked, TOPO_PROP_RESOURCE,
40135f59e50SStephen Hanson 	    &detector, pathstr);
40235f59e50SStephen Hanson 	FREE(pathstr);
403b5016cbbSstephh 	platform_save_config(hdl, fmcase);
404b5016cbbSstephh 	out(O_ALTFP|O_STAMP, "config_snapshot added %d bytes",
405b5016cbbSstephh 	    alloc_total() - init_size);
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	Nfmep = alloc_fme();
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	Nfmep->id = Nextid++;
410e5ba14ffSstephh 	Nfmep->config = cfgdata->cooked;
411e5ba14ffSstephh 	config_free(cfgdata);
4127c478bd9Sstevel@tonic-gate 	Nfmep->posted_suspects = 0;
4137c478bd9Sstevel@tonic-gate 	Nfmep->uniqobs = 0;
4147c478bd9Sstevel@tonic-gate 	Nfmep->state = FME_NOTHING;
4157c478bd9Sstevel@tonic-gate 	Nfmep->pull = 0ULL;
4160cc1f05eSjrutt 	Nfmep->overflow = 0;
4177c478bd9Sstevel@tonic-gate 
418b5016cbbSstephh 	Nfmep->fmcase = fmcase;
419b5016cbbSstephh 	Nfmep->hdl = hdl;
4207c478bd9Sstevel@tonic-gate 
421e5ba14ffSstephh 	if ((Nfmep->eventtree = itree_create(Nfmep->config)) == NULL) {
42232d4e834STarik Soydan 		Undiag_reason = UD_VAL_INSTFAIL;
423705e9f42SStephen Hanson 		arg = ipath2str(e0class, e0ipp);
424705e9f42SStephen Hanson 		publish_undiagnosable(hdl, ffep, fmcase, detector, arg);
425705e9f42SStephen Hanson 		nvlist_free(detector);
426705e9f42SStephen Hanson 		FREE(arg);
427e5ba14ffSstephh 		structconfig_free(Nfmep->config);
428b5016cbbSstephh 		destroy_fme_bufs(Nfmep);
4297c478bd9Sstevel@tonic-gate 		FREE(Nfmep);
4307c478bd9Sstevel@tonic-gate 		Nfmep = NULL;
4317c478bd9Sstevel@tonic-gate 		return (NULL);
4327c478bd9Sstevel@tonic-gate 	}
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	itree_ptree(O_ALTFP|O_VERB2, Nfmep->eventtree);
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	if ((Nfmep->e0 =
4377c478bd9Sstevel@tonic-gate 	    itree_lookup(Nfmep->eventtree, e0class, e0ipp)) == NULL) {
43832d4e834STarik Soydan 		Undiag_reason = UD_VAL_BADEVENTI;
439705e9f42SStephen Hanson 		arg = ipath2str(e0class, e0ipp);
440705e9f42SStephen Hanson 		publish_undiagnosable(hdl, ffep, fmcase, detector, arg);
441705e9f42SStephen Hanson 		nvlist_free(detector);
442705e9f42SStephen Hanson 		FREE(arg);
4437c478bd9Sstevel@tonic-gate 		itree_free(Nfmep->eventtree);
444e5ba14ffSstephh 		structconfig_free(Nfmep->config);
445b5016cbbSstephh 		destroy_fme_bufs(Nfmep);
4467c478bd9Sstevel@tonic-gate 		FREE(Nfmep);
4477c478bd9Sstevel@tonic-gate 		Nfmep = NULL;
4487c478bd9Sstevel@tonic-gate 		return (NULL);
4497c478bd9Sstevel@tonic-gate 	}
4507c478bd9Sstevel@tonic-gate 
451705e9f42SStephen Hanson 	nvlist_free(detector);
4527c478bd9Sstevel@tonic-gate 	return (fme_ready(Nfmep));
4537c478bd9Sstevel@tonic-gate }
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate void
fme_fini(void)4567c478bd9Sstevel@tonic-gate fme_fini(void)
4577c478bd9Sstevel@tonic-gate {
4587c478bd9Sstevel@tonic-gate 	struct fme *sfp, *fp;
4597c478bd9Sstevel@tonic-gate 	struct case_list *ucasep, *nextcasep;
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	ucasep = Undiagablecaselist;
4627c478bd9Sstevel@tonic-gate 	while (ucasep != NULL) {
4637c478bd9Sstevel@tonic-gate 		nextcasep = ucasep->next;
4647c478bd9Sstevel@tonic-gate 		FREE(ucasep);
4657c478bd9Sstevel@tonic-gate 		ucasep = nextcasep;
4667c478bd9Sstevel@tonic-gate 	}
4677c478bd9Sstevel@tonic-gate 	Undiagablecaselist = NULL;
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	/* clean up closed fmes */
4707c478bd9Sstevel@tonic-gate 	fp = ClosedFMEs;
4717c478bd9Sstevel@tonic-gate 	while (fp != NULL) {
4727c478bd9Sstevel@tonic-gate 		sfp = fp->next;
4737c478bd9Sstevel@tonic-gate 		destroy_fme(fp);
4747c478bd9Sstevel@tonic-gate 		fp = sfp;
4757c478bd9Sstevel@tonic-gate 	}
4767c478bd9Sstevel@tonic-gate 	ClosedFMEs = NULL;
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 	fp = FMElist;
4797c478bd9Sstevel@tonic-gate 	while (fp != NULL) {
4807c478bd9Sstevel@tonic-gate 		sfp = fp->next;
4817c478bd9Sstevel@tonic-gate 		destroy_fme(fp);
4827c478bd9Sstevel@tonic-gate 		fp = sfp;
4837c478bd9Sstevel@tonic-gate 	}
4847c478bd9Sstevel@tonic-gate 	FMElist = EFMElist = NULL;
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 	/* if we were in the middle of creating an fme, free it now */
4877c478bd9Sstevel@tonic-gate 	if (Nfmep) {
4887c478bd9Sstevel@tonic-gate 		destroy_fme(Nfmep);
4897c478bd9Sstevel@tonic-gate 		Nfmep = NULL;
4907c478bd9Sstevel@tonic-gate 	}
4917c478bd9Sstevel@tonic-gate }
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate /*
4947c478bd9Sstevel@tonic-gate  * Allocated space for a buffer name.  20 bytes allows for
4957c478bd9Sstevel@tonic-gate  * a ridiculous 9,999,999 unique observations.
4967c478bd9Sstevel@tonic-gate  */
4977c478bd9Sstevel@tonic-gate #define	OBBUFNMSZ 20
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate /*
5007c478bd9Sstevel@tonic-gate  *  serialize_observation
5017c478bd9Sstevel@tonic-gate  *
5027c478bd9Sstevel@tonic-gate  *  Create a recoverable version of the current observation
5037c478bd9Sstevel@tonic-gate  *  (f->ecurrent).  We keep a serialized version of each unique
5047c478bd9Sstevel@tonic-gate  *  observation in order that we may resume correctly the fme in the
5057c478bd9Sstevel@tonic-gate  *  correct state if eft or fmd crashes and we're restarted.
5067c478bd9Sstevel@tonic-gate  */
5077c478bd9Sstevel@tonic-gate static void
serialize_observation(struct fme * fp,const char * cls,const struct ipath * ipp)5087c478bd9Sstevel@tonic-gate serialize_observation(struct fme *fp, const char *cls, const struct ipath *ipp)
5097c478bd9Sstevel@tonic-gate {
5107c478bd9Sstevel@tonic-gate 	size_t pkdlen;
5117c478bd9Sstevel@tonic-gate 	char tmpbuf[OBBUFNMSZ];
5127c478bd9Sstevel@tonic-gate 	char *pkd = NULL;
5137c478bd9Sstevel@tonic-gate 	char *estr;
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 	(void) snprintf(tmpbuf, OBBUFNMSZ, "observed%d", fp->uniqobs);
5167c478bd9Sstevel@tonic-gate 	estr = ipath2str(cls, ipp);
5177c478bd9Sstevel@tonic-gate 	fmd_buf_create(fp->hdl, fp->fmcase, tmpbuf, strlen(estr) + 1);
5187c478bd9Sstevel@tonic-gate 	fmd_buf_write(fp->hdl, fp->fmcase, tmpbuf, (void *)estr,
5197c478bd9Sstevel@tonic-gate 	    strlen(estr) + 1);
5207c478bd9Sstevel@tonic-gate 	FREE(estr);
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 	if (fp->ecurrent != NULL && fp->ecurrent->nvp != NULL) {
5237c478bd9Sstevel@tonic-gate 		(void) snprintf(tmpbuf,
5247c478bd9Sstevel@tonic-gate 		    OBBUFNMSZ, "observed%d.nvp", fp->uniqobs);
5257c478bd9Sstevel@tonic-gate 		if (nvlist_xpack(fp->ecurrent->nvp,
5267c478bd9Sstevel@tonic-gate 		    &pkd, &pkdlen, NV_ENCODE_XDR, &Eft_nv_hdl) != 0)
5277c478bd9Sstevel@tonic-gate 			out(O_DIE|O_SYS, "pack of observed nvl failed");
5287c478bd9Sstevel@tonic-gate 		fmd_buf_create(fp->hdl, fp->fmcase, tmpbuf, pkdlen);
5297c478bd9Sstevel@tonic-gate 		fmd_buf_write(fp->hdl, fp->fmcase, tmpbuf, (void *)pkd, pkdlen);
5307c478bd9Sstevel@tonic-gate 		FREE(pkd);
5317c478bd9Sstevel@tonic-gate 	}
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	fp->uniqobs++;
5347c478bd9Sstevel@tonic-gate 	fmd_buf_write(fp->hdl, fp->fmcase, WOBUF_NOBS, (void *)&fp->uniqobs,
5357c478bd9Sstevel@tonic-gate 	    sizeof (fp->uniqobs));
5367c478bd9Sstevel@tonic-gate }
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate /*
5397c478bd9Sstevel@tonic-gate  *  init_fme_bufs -- We keep several bits of state about an fme for
5407c478bd9Sstevel@tonic-gate  *	use if eft or fmd crashes and we're restarted.
5417c478bd9Sstevel@tonic-gate  */
5427c478bd9Sstevel@tonic-gate static void
init_fme_bufs(struct fme * fp)5437c478bd9Sstevel@tonic-gate init_fme_bufs(struct fme *fp)
5447c478bd9Sstevel@tonic-gate {
5457c478bd9Sstevel@tonic-gate 	fmd_buf_create(fp->hdl, fp->fmcase, WOBUF_PULL, sizeof (fp->pull));
5467c478bd9Sstevel@tonic-gate 	fmd_buf_write(fp->hdl, fp->fmcase, WOBUF_PULL, (void *)&fp->pull,
5477c478bd9Sstevel@tonic-gate 	    sizeof (fp->pull));
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	fmd_buf_create(fp->hdl, fp->fmcase, WOBUF_ID, sizeof (fp->id));
5507c478bd9Sstevel@tonic-gate 	fmd_buf_write(fp->hdl, fp->fmcase, WOBUF_ID, (void *)&fp->id,
5517c478bd9Sstevel@tonic-gate 	    sizeof (fp->id));
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	fmd_buf_create(fp->hdl, fp->fmcase, WOBUF_NOBS, sizeof (fp->uniqobs));
5547c478bd9Sstevel@tonic-gate 	fmd_buf_write(fp->hdl, fp->fmcase, WOBUF_NOBS, (void *)&fp->uniqobs,
5557c478bd9Sstevel@tonic-gate 	    sizeof (fp->uniqobs));
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 	fmd_buf_create(fp->hdl, fp->fmcase, WOBUF_POSTD,
5587c478bd9Sstevel@tonic-gate 	    sizeof (fp->posted_suspects));
5597c478bd9Sstevel@tonic-gate 	fmd_buf_write(fp->hdl, fp->fmcase, WOBUF_POSTD,
5607c478bd9Sstevel@tonic-gate 	    (void *)&fp->posted_suspects, sizeof (fp->posted_suspects));
5617c478bd9Sstevel@tonic-gate }
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate static void
destroy_fme_bufs(struct fme * fp)5647c478bd9Sstevel@tonic-gate destroy_fme_bufs(struct fme *fp)
5657c478bd9Sstevel@tonic-gate {
5667c478bd9Sstevel@tonic-gate 	char tmpbuf[OBBUFNMSZ];
5677c478bd9Sstevel@tonic-gate 	int o;
5687c478bd9Sstevel@tonic-gate 
569b5016cbbSstephh 	platform_restore_config(fp->hdl, fp->fmcase);
5707c478bd9Sstevel@tonic-gate 	fmd_buf_destroy(fp->hdl, fp->fmcase, WOBUF_CFGLEN);
5717c478bd9Sstevel@tonic-gate 	fmd_buf_destroy(fp->hdl, fp->fmcase, WOBUF_CFG);
5727c478bd9Sstevel@tonic-gate 	fmd_buf_destroy(fp->hdl, fp->fmcase, WOBUF_PULL);
5737c478bd9Sstevel@tonic-gate 	fmd_buf_destroy(fp->hdl, fp->fmcase, WOBUF_ID);
5747c478bd9Sstevel@tonic-gate 	fmd_buf_destroy(fp->hdl, fp->fmcase, WOBUF_POSTD);
5757c478bd9Sstevel@tonic-gate 	fmd_buf_destroy(fp->hdl, fp->fmcase, WOBUF_NOBS);
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 	for (o = 0; o < fp->uniqobs; o++) {
5787c478bd9Sstevel@tonic-gate 		(void) snprintf(tmpbuf, OBBUFNMSZ, "observed%d", o);
5797c478bd9Sstevel@tonic-gate 		fmd_buf_destroy(fp->hdl, fp->fmcase, tmpbuf);
5807c478bd9Sstevel@tonic-gate 		(void) snprintf(tmpbuf, OBBUFNMSZ, "observed%d.nvp", o);
5817c478bd9Sstevel@tonic-gate 		fmd_buf_destroy(fp->hdl, fp->fmcase, tmpbuf);
5827c478bd9Sstevel@tonic-gate 	}
5837c478bd9Sstevel@tonic-gate }
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate /*
5867c478bd9Sstevel@tonic-gate  * reconstitute_observations -- convert a case's serialized observations
5877c478bd9Sstevel@tonic-gate  *	back into struct events.  Returns zero if all observations are
5887c478bd9Sstevel@tonic-gate  *	successfully reconstituted.
5897c478bd9Sstevel@tonic-gate  */
5907c478bd9Sstevel@tonic-gate static int
reconstitute_observations(struct fme * fmep)5917c478bd9Sstevel@tonic-gate reconstitute_observations(struct fme *fmep)
5927c478bd9Sstevel@tonic-gate {
5937c478bd9Sstevel@tonic-gate 	struct event *ep;
5947c478bd9Sstevel@tonic-gate 	struct node *epnamenp = NULL;
5957c478bd9Sstevel@tonic-gate 	size_t pkdlen;
5967c478bd9Sstevel@tonic-gate 	char *pkd = NULL;
5977c478bd9Sstevel@tonic-gate 	char *tmpbuf = alloca(OBBUFNMSZ);
5987c478bd9Sstevel@tonic-gate 	char *sepptr;
5997c478bd9Sstevel@tonic-gate 	char *estr;
6007c478bd9Sstevel@tonic-gate 	int ocnt;
6017c478bd9Sstevel@tonic-gate 	int elen;
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 	for (ocnt = 0; ocnt < fmep->uniqobs; ocnt++) {
6047c478bd9Sstevel@tonic-gate 		(void) snprintf(tmpbuf, OBBUFNMSZ, "observed%d", ocnt);
6057c478bd9Sstevel@tonic-gate 		elen = fmd_buf_size(fmep->hdl, fmep->fmcase, tmpbuf);
6067c478bd9Sstevel@tonic-gate 		if (elen == 0) {
6077c478bd9Sstevel@tonic-gate 			out(O_ALTFP,
6087c478bd9Sstevel@tonic-gate 			    "reconstitute_observation: no %s buffer found.",
6097c478bd9Sstevel@tonic-gate 			    tmpbuf);
61032d4e834STarik Soydan 			Undiag_reason = UD_VAL_MISSINGOBS;
6117c478bd9Sstevel@tonic-gate 			break;
6127c478bd9Sstevel@tonic-gate 		}
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 		estr = MALLOC(elen);
6157c478bd9Sstevel@tonic-gate 		fmd_buf_read(fmep->hdl, fmep->fmcase, tmpbuf, estr, elen);
6167c478bd9Sstevel@tonic-gate 		sepptr = strchr(estr, '@');
6177c478bd9Sstevel@tonic-gate 		if (sepptr == NULL) {
6187c478bd9Sstevel@tonic-gate 			out(O_ALTFP,
6197c478bd9Sstevel@tonic-gate 			    "reconstitute_observation: %s: "
6207c478bd9Sstevel@tonic-gate 			    "missing @ separator in %s.",
6217c478bd9Sstevel@tonic-gate 			    tmpbuf, estr);
62232d4e834STarik Soydan 			Undiag_reason = UD_VAL_MISSINGPATH;
6237c478bd9Sstevel@tonic-gate 			FREE(estr);
6247c478bd9Sstevel@tonic-gate 			break;
6257c478bd9Sstevel@tonic-gate 		}
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 		*sepptr = '\0';
6287c478bd9Sstevel@tonic-gate 		if ((epnamenp = pathstring2epnamenp(sepptr + 1)) == NULL) {
6297c478bd9Sstevel@tonic-gate 			out(O_ALTFP,
6307c478bd9Sstevel@tonic-gate 			    "reconstitute_observation: %s: "
6317c478bd9Sstevel@tonic-gate 			    "trouble converting path string \"%s\" "
6327c478bd9Sstevel@tonic-gate 			    "to internal representation.",
6337c478bd9Sstevel@tonic-gate 			    tmpbuf, sepptr + 1);
63432d4e834STarik Soydan 			Undiag_reason = UD_VAL_MISSINGPATH;
6357c478bd9Sstevel@tonic-gate 			FREE(estr);
6367c478bd9Sstevel@tonic-gate 			break;
6377c478bd9Sstevel@tonic-gate 		}
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate 		/* construct the event */
6407c478bd9Sstevel@tonic-gate 		ep = itree_lookup(fmep->eventtree,
6417c478bd9Sstevel@tonic-gate 		    stable(estr), ipath(epnamenp));
6427c478bd9Sstevel@tonic-gate 		if (ep == NULL) {
6437c478bd9Sstevel@tonic-gate 			out(O_ALTFP,
6447c478bd9Sstevel@tonic-gate 			    "reconstitute_observation: %s: "
6457c478bd9Sstevel@tonic-gate 			    "lookup of  \"%s\" in itree failed.",
6467c478bd9Sstevel@tonic-gate 			    tmpbuf, ipath2str(estr, ipath(epnamenp)));
64732d4e834STarik Soydan 			Undiag_reason = UD_VAL_BADOBS;
6487c478bd9Sstevel@tonic-gate 			tree_free(epnamenp);
6497c478bd9Sstevel@tonic-gate 			FREE(estr);
6507c478bd9Sstevel@tonic-gate 			break;
6517c478bd9Sstevel@tonic-gate 		}
6527c478bd9Sstevel@tonic-gate 		tree_free(epnamenp);
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 		/*
6557c478bd9Sstevel@tonic-gate 		 * We may or may not have a saved nvlist for the observation
6567c478bd9Sstevel@tonic-gate 		 */
6577c478bd9Sstevel@tonic-gate 		(void) snprintf(tmpbuf, OBBUFNMSZ, "observed%d.nvp", ocnt);
6587c478bd9Sstevel@tonic-gate 		pkdlen = fmd_buf_size(fmep->hdl, fmep->fmcase, tmpbuf);
6597c478bd9Sstevel@tonic-gate 		if (pkdlen != 0) {
6607c478bd9Sstevel@tonic-gate 			pkd = MALLOC(pkdlen);
6617c478bd9Sstevel@tonic-gate 			fmd_buf_read(fmep->hdl,
6627c478bd9Sstevel@tonic-gate 			    fmep->fmcase, tmpbuf, pkd, pkdlen);
6637aec1d6eScindi 			ASSERT(ep->nvp == NULL);
6647c478bd9Sstevel@tonic-gate 			if (nvlist_xunpack(pkd,
6657c478bd9Sstevel@tonic-gate 			    pkdlen, &ep->nvp, &Eft_nv_hdl) != 0)
6667c478bd9Sstevel@tonic-gate 				out(O_DIE|O_SYS, "pack of observed nvl failed");
6677c478bd9Sstevel@tonic-gate 			FREE(pkd);
6687c478bd9Sstevel@tonic-gate 		}
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 		if (ocnt == 0)
6717c478bd9Sstevel@tonic-gate 			fmep->e0 = ep;
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 		FREE(estr);
6747c478bd9Sstevel@tonic-gate 		fmep->ecurrent = ep;
6757c478bd9Sstevel@tonic-gate 		ep->count++;
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 		/* link it into list of observations seen */
6787c478bd9Sstevel@tonic-gate 		ep->observations = fmep->observations;
6797c478bd9Sstevel@tonic-gate 		fmep->observations = ep;
6807c478bd9Sstevel@tonic-gate 	}
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 	if (ocnt == fmep->uniqobs) {
6837c478bd9Sstevel@tonic-gate 		(void) fme_ready(fmep);
6847c478bd9Sstevel@tonic-gate 		return (0);
6857c478bd9Sstevel@tonic-gate 	}
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 	return (1);
6887c478bd9Sstevel@tonic-gate }
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate /*
6917c478bd9Sstevel@tonic-gate  * restart_fme -- called during eft initialization.  Reconstitutes
6927c478bd9Sstevel@tonic-gate  *	an in-progress fme.
6937c478bd9Sstevel@tonic-gate  */
6947c478bd9Sstevel@tonic-gate void
fme_restart(fmd_hdl_t * hdl,fmd_case_t * inprogress)6957c478bd9Sstevel@tonic-gate fme_restart(fmd_hdl_t *hdl, fmd_case_t *inprogress)
6967c478bd9Sstevel@tonic-gate {
6977c478bd9Sstevel@tonic-gate 	nvlist_t *defect;
6987c478bd9Sstevel@tonic-gate 	struct case_list *bad;
6997c478bd9Sstevel@tonic-gate 	struct fme *fmep;
700e5ba14ffSstephh 	struct cfgdata *cfgdata;
7017c478bd9Sstevel@tonic-gate 	size_t rawsz;
702b5016cbbSstephh 	struct event *ep;
703b5016cbbSstephh 	char *tmpbuf = alloca(OBBUFNMSZ);
704b5016cbbSstephh 	char *sepptr;
705b5016cbbSstephh 	char *estr;
706b5016cbbSstephh 	int elen;
707b5016cbbSstephh 	struct node *epnamenp = NULL;
708b5016cbbSstephh 	int init_size;
709b5016cbbSstephh 	extern int alloc_total();
710705e9f42SStephen Hanson 	char *reason;
711b5016cbbSstephh 
712b5016cbbSstephh 	/*
713b5016cbbSstephh 	 * ignore solved or closed cases
714b5016cbbSstephh 	 */
715b5016cbbSstephh 	if (fmd_case_solved(hdl, inprogress) ||
716b5016cbbSstephh 	    fmd_case_closed(hdl, inprogress))
717b5016cbbSstephh 		return;
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 	fmep = alloc_fme();
7207c478bd9Sstevel@tonic-gate 	fmep->fmcase = inprogress;
7217c478bd9Sstevel@tonic-gate 	fmep->hdl = hdl;
7227c478bd9Sstevel@tonic-gate 
7239f8ca725Sstephh 	if (fmd_buf_size(hdl, inprogress, WOBUF_POSTD) == 0) {
7249f8ca725Sstephh 		out(O_ALTFP, "restart_fme: no saved posted status");
72532d4e834STarik Soydan 		Undiag_reason = UD_VAL_MISSINGINFO;
7269f8ca725Sstephh 		goto badcase;
7279f8ca725Sstephh 	} else {
7289f8ca725Sstephh 		fmd_buf_read(hdl, inprogress, WOBUF_POSTD,
7299f8ca725Sstephh 		    (void *)&fmep->posted_suspects,
7309f8ca725Sstephh 		    sizeof (fmep->posted_suspects));
7319f8ca725Sstephh 	}
7329f8ca725Sstephh 
733b5016cbbSstephh 	if (fmd_buf_size(hdl, inprogress, WOBUF_ID) == 0) {
734b5016cbbSstephh 		out(O_ALTFP, "restart_fme: no saved id");
73532d4e834STarik Soydan 		Undiag_reason = UD_VAL_MISSINGINFO;
7369f8ca725Sstephh 		goto badcase;
737b5016cbbSstephh 	} else {
738b5016cbbSstephh 		fmd_buf_read(hdl, inprogress, WOBUF_ID, (void *)&fmep->id,
739b5016cbbSstephh 		    sizeof (fmep->id));
740b5016cbbSstephh 	}
741b5016cbbSstephh 	if (Nextid <= fmep->id)
742b5016cbbSstephh 		Nextid = fmep->id + 1;
743b5016cbbSstephh 
744b5016cbbSstephh 	out(O_ALTFP, "Replay FME %d", fmep->id);
7459f8ca725Sstephh 
7467c478bd9Sstevel@tonic-gate 	if (fmd_buf_size(hdl, inprogress, WOBUF_CFGLEN) != sizeof (size_t)) {
7477c478bd9Sstevel@tonic-gate 		out(O_ALTFP, "restart_fme: No config data");
74832d4e834STarik Soydan 		Undiag_reason = UD_VAL_MISSINGINFO;
7497c478bd9Sstevel@tonic-gate 		goto badcase;
7507c478bd9Sstevel@tonic-gate 	}
7517c478bd9Sstevel@tonic-gate 	fmd_buf_read(hdl, inprogress, WOBUF_CFGLEN, (void *)&rawsz,
7527c478bd9Sstevel@tonic-gate 	    sizeof (size_t));
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 	if ((fmep->e0r = fmd_case_getprincipal(hdl, inprogress)) == NULL) {
7557c478bd9Sstevel@tonic-gate 		out(O_ALTFP, "restart_fme: No event zero");
75632d4e834STarik Soydan 		Undiag_reason = UD_VAL_MISSINGZERO;
7577c478bd9Sstevel@tonic-gate 		goto badcase;
7587c478bd9Sstevel@tonic-gate 	}
7597c478bd9Sstevel@tonic-gate 
760b5016cbbSstephh 	if (fmd_buf_size(hdl, inprogress, WOBUF_PULL) == 0) {
761b5016cbbSstephh 		out(O_ALTFP, "restart_fme: no saved wait time");
76232d4e834STarik Soydan 		Undiag_reason = UD_VAL_MISSINGINFO;
763b5016cbbSstephh 		goto badcase;
764b5016cbbSstephh 	} else {
765b5016cbbSstephh 		fmd_buf_read(hdl, inprogress, WOBUF_PULL, (void *)&fmep->pull,
766b5016cbbSstephh 		    sizeof (fmep->pull));
767b5016cbbSstephh 	}
768b5016cbbSstephh 
769b5016cbbSstephh 	if (fmd_buf_size(hdl, inprogress, WOBUF_NOBS) == 0) {
770b5016cbbSstephh 		out(O_ALTFP, "restart_fme: no count of observations");
77132d4e834STarik Soydan 		Undiag_reason = UD_VAL_MISSINGINFO;
772b5016cbbSstephh 		goto badcase;
773b5016cbbSstephh 	} else {
774b5016cbbSstephh 		fmd_buf_read(hdl, inprogress, WOBUF_NOBS,
775b5016cbbSstephh 		    (void *)&fmep->uniqobs, sizeof (fmep->uniqobs));
776b5016cbbSstephh 	}
777b5016cbbSstephh 
778b5016cbbSstephh 	(void) snprintf(tmpbuf, OBBUFNMSZ, "observed0");
779b5016cbbSstephh 	elen = fmd_buf_size(fmep->hdl, fmep->fmcase, tmpbuf);
780b5016cbbSstephh 	if (elen == 0) {
781b5016cbbSstephh 		out(O_ALTFP, "reconstitute_observation: no %s buffer found.",
782b5016cbbSstephh 		    tmpbuf);
78332d4e834STarik Soydan 		Undiag_reason = UD_VAL_MISSINGOBS;
784b5016cbbSstephh 		goto badcase;
785b5016cbbSstephh 	}
786b5016cbbSstephh 	estr = MALLOC(elen);
787b5016cbbSstephh 	fmd_buf_read(fmep->hdl, fmep->fmcase, tmpbuf, estr, elen);
788b5016cbbSstephh 	sepptr = strchr(estr, '@');
789b5016cbbSstephh 	if (sepptr == NULL) {
790b5016cbbSstephh 		out(O_ALTFP, "reconstitute_observation: %s: "
791b5016cbbSstephh 		    "missing @ separator in %s.",
792b5016cbbSstephh 		    tmpbuf, estr);
79332d4e834STarik Soydan 		Undiag_reason = UD_VAL_MISSINGPATH;
794b5016cbbSstephh 		FREE(estr);
795b5016cbbSstephh 		goto badcase;
796b5016cbbSstephh 	}
797b5016cbbSstephh 	*sepptr = '\0';
798b5016cbbSstephh 	if ((epnamenp = pathstring2epnamenp(sepptr + 1)) == NULL) {
799b5016cbbSstephh 		out(O_ALTFP, "reconstitute_observation: %s: "
800b5016cbbSstephh 		    "trouble converting path string \"%s\" "
801b5016cbbSstephh 		    "to internal representation.", tmpbuf, sepptr + 1);
80232d4e834STarik Soydan 		Undiag_reason = UD_VAL_MISSINGPATH;
803b5016cbbSstephh 		FREE(estr);
804b5016cbbSstephh 		goto badcase;
805b5016cbbSstephh 	}
806705e9f42SStephen Hanson 	(void) prune_propagations(stable(estr), ipath(epnamenp));
807b5016cbbSstephh 	tree_free(epnamenp);
808b5016cbbSstephh 	FREE(estr);
809b5016cbbSstephh 
810b5016cbbSstephh 	init_size = alloc_total();
811b5016cbbSstephh 	out(O_ALTFP|O_STAMP, "start config_restore using %d bytes", init_size);
8127c478bd9Sstevel@tonic-gate 	cfgdata = MALLOC(sizeof (struct cfgdata));
8137c478bd9Sstevel@tonic-gate 	cfgdata->cooked = NULL;
8147c478bd9Sstevel@tonic-gate 	cfgdata->devcache = NULL;
815602ca9eaScth 	cfgdata->devidcache = NULL;
81644ed9dbbSStephen Hanson 	cfgdata->tpcache = NULL;
8177c478bd9Sstevel@tonic-gate 	cfgdata->cpucache = NULL;
818b5016cbbSstephh 	cfgdata->raw_refcnt = 1;
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 	if (rawsz > 0) {
8217c478bd9Sstevel@tonic-gate 		if (fmd_buf_size(hdl, inprogress, WOBUF_CFG) != rawsz) {
8227c478bd9Sstevel@tonic-gate 			out(O_ALTFP, "restart_fme: Config data size mismatch");
82332d4e834STarik Soydan 			Undiag_reason = UD_VAL_CFGMISMATCH;
8247c478bd9Sstevel@tonic-gate 			goto badcase;
8257c478bd9Sstevel@tonic-gate 		}
8267c478bd9Sstevel@tonic-gate 		cfgdata->begin = MALLOC(rawsz);
8277c478bd9Sstevel@tonic-gate 		cfgdata->end = cfgdata->nextfree = cfgdata->begin + rawsz;
8287c478bd9Sstevel@tonic-gate 		fmd_buf_read(hdl,
8297c478bd9Sstevel@tonic-gate 		    inprogress, WOBUF_CFG, cfgdata->begin, rawsz);
8307c478bd9Sstevel@tonic-gate 	} else {
8317c478bd9Sstevel@tonic-gate 		cfgdata->begin = cfgdata->end = cfgdata->nextfree = NULL;
8327c478bd9Sstevel@tonic-gate 	}
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate 	config_cook(cfgdata);
835e5ba14ffSstephh 	fmep->config = cfgdata->cooked;
836e5ba14ffSstephh 	config_free(cfgdata);
837b5016cbbSstephh 	out(O_ALTFP|O_STAMP, "config_restore added %d bytes",
838b5016cbbSstephh 	    alloc_total() - init_size);
839b5016cbbSstephh 
840e5ba14ffSstephh 	if ((fmep->eventtree = itree_create(fmep->config)) == NULL) {
8417c478bd9Sstevel@tonic-gate 		/* case not properly saved or irretrievable */
8427c478bd9Sstevel@tonic-gate 		out(O_ALTFP, "restart_fme: NULL instance tree");
84332d4e834STarik Soydan 		Undiag_reason = UD_VAL_INSTFAIL;
8447c478bd9Sstevel@tonic-gate 		goto badcase;
8457c478bd9Sstevel@tonic-gate 	}
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate 	itree_ptree(O_ALTFP|O_VERB2, fmep->eventtree);
8487c478bd9Sstevel@tonic-gate 
849b5016cbbSstephh 	if (reconstitute_observations(fmep) != 0)
8507c478bd9Sstevel@tonic-gate 		goto badcase;
8517c478bd9Sstevel@tonic-gate 
852b5016cbbSstephh 	out(O_ALTFP|O_NONL, "FME %d replay observations: ", fmep->id);
853b5016cbbSstephh 	for (ep = fmep->observations; ep; ep = ep->observations) {
854b5016cbbSstephh 		out(O_ALTFP|O_NONL, " ");
855b5016cbbSstephh 		itree_pevent_brief(O_ALTFP|O_NONL, ep);
8567c478bd9Sstevel@tonic-gate 	}
857b5016cbbSstephh 	out(O_ALTFP, NULL);
8587c478bd9Sstevel@tonic-gate 
8590cc1f05eSjrutt 	Open_fme_count++;
8600cc1f05eSjrutt 
8617c478bd9Sstevel@tonic-gate 	/* give the diagnosis algorithm a shot at the new FME state */
8629f8ca725Sstephh 	fme_eval(fmep, fmep->e0r);
8637c478bd9Sstevel@tonic-gate 	return;
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate badcase:
8667c478bd9Sstevel@tonic-gate 	if (fmep->eventtree != NULL)
8677c478bd9Sstevel@tonic-gate 		itree_free(fmep->eventtree);
868e5ba14ffSstephh 	if (fmep->config)
869e5ba14ffSstephh 		structconfig_free(fmep->config);
8707c478bd9Sstevel@tonic-gate 	destroy_fme_bufs(fmep);
8717c478bd9Sstevel@tonic-gate 	FREE(fmep);
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate 	/*
8747c478bd9Sstevel@tonic-gate 	 * Since we're unable to restart the case, add it to the undiagable
8757c478bd9Sstevel@tonic-gate 	 * list and solve and close it as appropriate.
8767c478bd9Sstevel@tonic-gate 	 */
8777c478bd9Sstevel@tonic-gate 	bad = MALLOC(sizeof (struct case_list));
8787c478bd9Sstevel@tonic-gate 	bad->next = NULL;
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate 	if (Undiagablecaselist != NULL)
8817c478bd9Sstevel@tonic-gate 		bad->next = Undiagablecaselist;
8827c478bd9Sstevel@tonic-gate 	Undiagablecaselist = bad;
8837c478bd9Sstevel@tonic-gate 	bad->fmcase = inprogress;
8847c478bd9Sstevel@tonic-gate 
8859f8ca725Sstephh 	out(O_ALTFP|O_NONL, "[case %s (unable to restart), ",
8867c478bd9Sstevel@tonic-gate 	    fmd_case_uuid(hdl, bad->fmcase));
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 	if (fmd_case_solved(hdl, bad->fmcase)) {
8899f8ca725Sstephh 		out(O_ALTFP|O_NONL, "already solved, ");
8907c478bd9Sstevel@tonic-gate 	} else {
8919f8ca725Sstephh 		out(O_ALTFP|O_NONL, "solving, ");
89232d4e834STarik Soydan 		defect = fmd_nvl_create_fault(hdl,
89332d4e834STarik Soydan 		    undiag_2defect_str(Undiag_reason), 100, NULL, NULL, NULL);
894705e9f42SStephen Hanson 		reason = undiag_2reason_str(Undiag_reason, NULL);
895705e9f42SStephen Hanson 		(void) nvlist_add_string(defect, UNDIAG_REASON, reason);
896705e9f42SStephen Hanson 		FREE(reason);
8977c478bd9Sstevel@tonic-gate 		fmd_case_add_suspect(hdl, bad->fmcase, defect);
8987c478bd9Sstevel@tonic-gate 		fmd_case_solve(hdl, bad->fmcase);
89932d4e834STarik Soydan 		Undiag_reason = UD_VAL_UNKNOWN;
9007c478bd9Sstevel@tonic-gate 	}
9017c478bd9Sstevel@tonic-gate 
9027c478bd9Sstevel@tonic-gate 	if (fmd_case_closed(hdl, bad->fmcase)) {
9037c478bd9Sstevel@tonic-gate 		out(O_ALTFP, "already closed ]");
9047c478bd9Sstevel@tonic-gate 	} else {
9057c478bd9Sstevel@tonic-gate 		out(O_ALTFP, "closing ]");
9067c478bd9Sstevel@tonic-gate 		fmd_case_close(hdl, bad->fmcase);
9077c478bd9Sstevel@tonic-gate 	}
9087c478bd9Sstevel@tonic-gate }
9097c478bd9Sstevel@tonic-gate 
9107aec1d6eScindi /*ARGSUSED*/
9117aec1d6eScindi static void
globals_destructor(void * left,void * right,void * arg)9127aec1d6eScindi globals_destructor(void *left, void *right, void *arg)
9137aec1d6eScindi {
9147aec1d6eScindi 	struct evalue *evp = (struct evalue *)right;
9157aec1d6eScindi 	if (evp->t == NODEPTR)
91680ab886dSwesolows 		tree_free((struct node *)(uintptr_t)evp->v);
917837416c3Scy 	evp->v = (uintptr_t)NULL;
9187aec1d6eScindi 	FREE(evp);
9197aec1d6eScindi }
9207aec1d6eScindi 
9217c478bd9Sstevel@tonic-gate void
destroy_fme(struct fme * f)9227c478bd9Sstevel@tonic-gate destroy_fme(struct fme *f)
9237c478bd9Sstevel@tonic-gate {
9247c478bd9Sstevel@tonic-gate 	stats_delete(f->Rcount);
9257c478bd9Sstevel@tonic-gate 	stats_delete(f->Hcallcount);
9267c478bd9Sstevel@tonic-gate 	stats_delete(f->Rcallcount);
9277c478bd9Sstevel@tonic-gate 	stats_delete(f->Ccallcount);
9287c478bd9Sstevel@tonic-gate 	stats_delete(f->Ecallcount);
9297c478bd9Sstevel@tonic-gate 	stats_delete(f->Tcallcount);
9307c478bd9Sstevel@tonic-gate 	stats_delete(f->Marrowcount);
9317c478bd9Sstevel@tonic-gate 	stats_delete(f->diags);
9327c478bd9Sstevel@tonic-gate 
9339f8ca725Sstephh 	if (f->eventtree != NULL)
9349f8ca725Sstephh 		itree_free(f->eventtree);
935e5ba14ffSstephh 	if (f->config)
936e5ba14ffSstephh 		structconfig_free(f->config);
9377aec1d6eScindi 	lut_free(f->globals, globals_destructor, NULL);
9387c478bd9Sstevel@tonic-gate 	FREE(f);
9397c478bd9Sstevel@tonic-gate }
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate static const char *
fme_state2str(enum fme_state s)9427c478bd9Sstevel@tonic-gate fme_state2str(enum fme_state s)
9437c478bd9Sstevel@tonic-gate {
9447c478bd9Sstevel@tonic-gate 	switch (s) {
9457c478bd9Sstevel@tonic-gate 	case FME_NOTHING:	return ("NOTHING");
9467c478bd9Sstevel@tonic-gate 	case FME_WAIT:		return ("WAIT");
9477c478bd9Sstevel@tonic-gate 	case FME_CREDIBLE:	return ("CREDIBLE");
9487c478bd9Sstevel@tonic-gate 	case FME_DISPROVED:	return ("DISPROVED");
9497aec1d6eScindi 	case FME_DEFERRED:	return ("DEFERRED");
9507c478bd9Sstevel@tonic-gate 	default:		return ("UNKNOWN");
9517c478bd9Sstevel@tonic-gate 	}
9527c478bd9Sstevel@tonic-gate }
9537c478bd9Sstevel@tonic-gate 
9547c478bd9Sstevel@tonic-gate static int
is_problem(enum nametype t)9557c478bd9Sstevel@tonic-gate is_problem(enum nametype t)
9567c478bd9Sstevel@tonic-gate {
9577c478bd9Sstevel@tonic-gate 	return (t == N_FAULT || t == N_DEFECT || t == N_UPSET);
9587c478bd9Sstevel@tonic-gate }
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate static int
is_defect(enum nametype t)9617c478bd9Sstevel@tonic-gate is_defect(enum nametype t)
9627c478bd9Sstevel@tonic-gate {
9637c478bd9Sstevel@tonic-gate 	return (t == N_DEFECT);
9647c478bd9Sstevel@tonic-gate }
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate static int
is_upset(enum nametype t)9677c478bd9Sstevel@tonic-gate is_upset(enum nametype t)
9687c478bd9Sstevel@tonic-gate {
9697c478bd9Sstevel@tonic-gate 	return (t == N_UPSET);
9707c478bd9Sstevel@tonic-gate }
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate static void
fme_print(int flags,struct fme * fmep)9737c478bd9Sstevel@tonic-gate fme_print(int flags, struct fme *fmep)
9747c478bd9Sstevel@tonic-gate {
9757c478bd9Sstevel@tonic-gate 	struct event *ep;
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate 	out(flags, "Fault Management Exercise %d", fmep->id);
9787c478bd9Sstevel@tonic-gate 	out(flags, "\t       State: %s", fme_state2str(fmep->state));
9797c478bd9Sstevel@tonic-gate 	out(flags|O_NONL, "\t  Start time: ");
9807c478bd9Sstevel@tonic-gate 	ptree_timeval(flags|O_NONL, &fmep->ull);
9817c478bd9Sstevel@tonic-gate 	out(flags, NULL);
9827c478bd9Sstevel@tonic-gate 	if (fmep->wull) {
9837c478bd9Sstevel@tonic-gate 		out(flags|O_NONL, "\t   Wait time: ");
9847c478bd9Sstevel@tonic-gate 		ptree_timeval(flags|O_NONL, &fmep->wull);
9857c478bd9Sstevel@tonic-gate 		out(flags, NULL);
9867c478bd9Sstevel@tonic-gate 	}
9877c478bd9Sstevel@tonic-gate 	out(flags|O_NONL, "\t          E0: ");
9887c478bd9Sstevel@tonic-gate 	if (fmep->e0)
9897c478bd9Sstevel@tonic-gate 		itree_pevent_brief(flags|O_NONL, fmep->e0);
9907c478bd9Sstevel@tonic-gate 	else
9917c478bd9Sstevel@tonic-gate 		out(flags|O_NONL, "NULL");
9927c478bd9Sstevel@tonic-gate 	out(flags, NULL);
9937c478bd9Sstevel@tonic-gate 	out(flags|O_NONL, "\tObservations:");
9947c478bd9Sstevel@tonic-gate 	for (ep = fmep->observations; ep; ep = ep->observations) {
9957c478bd9Sstevel@tonic-gate 		out(flags|O_NONL, " ");
9967c478bd9Sstevel@tonic-gate 		itree_pevent_brief(flags|O_NONL, ep);
9977c478bd9Sstevel@tonic-gate 	}
9987c478bd9Sstevel@tonic-gate 	out(flags, NULL);
9997c478bd9Sstevel@tonic-gate 	out(flags|O_NONL, "\tSuspect list:");
10007c478bd9Sstevel@tonic-gate 	for (ep = fmep->suspects; ep; ep = ep->suspects) {
10017c478bd9Sstevel@tonic-gate 		out(flags|O_NONL, " ");
10027c478bd9Sstevel@tonic-gate 		itree_pevent_brief(flags|O_NONL, ep);
10037c478bd9Sstevel@tonic-gate 	}
10047c478bd9Sstevel@tonic-gate 	out(flags, NULL);
10059f8ca725Sstephh 	if (fmep->eventtree != NULL) {
10069f8ca725Sstephh 		out(flags|O_VERB2, "\t        Tree:");
10079f8ca725Sstephh 		itree_ptree(flags|O_VERB2, fmep->eventtree);
10089f8ca725Sstephh 	}
10097c478bd9Sstevel@tonic-gate }
10107c478bd9Sstevel@tonic-gate 
10117c478bd9Sstevel@tonic-gate static struct node *
pathstring2epnamenp(char * path)10127c478bd9Sstevel@tonic-gate pathstring2epnamenp(char *path)
10137c478bd9Sstevel@tonic-gate {
10147c478bd9Sstevel@tonic-gate 	char *sep = "/";
10157c478bd9Sstevel@tonic-gate 	struct node *ret;
10167c478bd9Sstevel@tonic-gate 	char *ptr;
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate 	if ((ptr = strtok(path, sep)) == NULL)
10197c478bd9Sstevel@tonic-gate 		out(O_DIE, "pathstring2epnamenp: invalid empty class");
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 	ret = tree_iname(stable(ptr), NULL, 0);
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate 	while ((ptr = strtok(NULL, sep)) != NULL)
10247c478bd9Sstevel@tonic-gate 		ret = tree_name_append(ret,
10257c478bd9Sstevel@tonic-gate 		    tree_iname(stable(ptr), NULL, 0));
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate 	return (ret);
10287c478bd9Sstevel@tonic-gate }
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate /*
10317c478bd9Sstevel@tonic-gate  * for a given upset sp, increment the corresponding SERD engine.  if the
10327c478bd9Sstevel@tonic-gate  * SERD engine trips, return the ename and ipp of the resulting ereport.
10337c478bd9Sstevel@tonic-gate  * returns true if engine tripped and *enamep and *ippp were filled in.
10347c478bd9Sstevel@tonic-gate  */
10357c478bd9Sstevel@tonic-gate static int
serd_eval(struct fme * fmep,fmd_hdl_t * hdl,fmd_event_t * ffep,fmd_case_t * fmcase,struct event * sp,const char ** enamep,const struct ipath ** ippp)10367aec1d6eScindi serd_eval(struct fme *fmep, fmd_hdl_t *hdl, fmd_event_t *ffep,
10377aec1d6eScindi     fmd_case_t *fmcase, struct event *sp, const char **enamep,
10387aec1d6eScindi     const struct ipath **ippp)
10397c478bd9Sstevel@tonic-gate {
10407c478bd9Sstevel@tonic-gate 	struct node *serdinst;
10417c478bd9Sstevel@tonic-gate 	char *serdname;
1042049e2ea4SScott Davenport 	char *serdresource;
10438e7248e5SStephen Hanson 	char *serdclass;
10447aec1d6eScindi 	struct node *nid;
1045b5016cbbSstephh 	struct serd_entry *newentp;
1046049e2ea4SScott Davenport 	int i, serdn = -1, serdincrement = 1, len = 0;
10478e7248e5SStephen Hanson 	char *serdsuffix = NULL, *serdt = NULL;
1048b7d3956bSstephh 	struct evalue *ep;
10497c478bd9Sstevel@tonic-gate 
10507c478bd9Sstevel@tonic-gate 	ASSERT(sp->t == N_UPSET);
10517c478bd9Sstevel@tonic-gate 	ASSERT(ffep != NULL);
10527c478bd9Sstevel@tonic-gate 
1053b7d3956bSstephh 	if ((ep = (struct evalue *)lut_lookup(sp->serdprops,
1054b7d3956bSstephh 	    (void *)"n", (lut_cmp)strcmp)) != NULL) {
1055b7d3956bSstephh 		ASSERT(ep->t == UINT64);
1056b7d3956bSstephh 		serdn = (int)ep->v;
1057b7d3956bSstephh 	}
1058b7d3956bSstephh 	if ((ep = (struct evalue *)lut_lookup(sp->serdprops,
1059b7d3956bSstephh 	    (void *)"t", (lut_cmp)strcmp)) != NULL) {
1060b7d3956bSstephh 		ASSERT(ep->t == STRING);
1061b7d3956bSstephh 		serdt = (char *)(uintptr_t)ep->v;
1062b7d3956bSstephh 	}
1063b7d3956bSstephh 	if ((ep = (struct evalue *)lut_lookup(sp->serdprops,
1064b7d3956bSstephh 	    (void *)"suffix", (lut_cmp)strcmp)) != NULL) {
1065b7d3956bSstephh 		ASSERT(ep->t == STRING);
1066b7d3956bSstephh 		serdsuffix = (char *)(uintptr_t)ep->v;
1067b7d3956bSstephh 	}
1068b7d3956bSstephh 	if ((ep = (struct evalue *)lut_lookup(sp->serdprops,
1069b7d3956bSstephh 	    (void *)"increment", (lut_cmp)strcmp)) != NULL) {
1070b7d3956bSstephh 		ASSERT(ep->t == UINT64);
1071b7d3956bSstephh 		serdincrement = (int)ep->v;
1072b7d3956bSstephh 	}
1073b7d3956bSstephh 
10747c478bd9Sstevel@tonic-gate 	/*
10757c478bd9Sstevel@tonic-gate 	 * obtain instanced SERD engine from the upset sp.  from this
10767c478bd9Sstevel@tonic-gate 	 * derive serdname, the string used to identify the SERD engine.
10777c478bd9Sstevel@tonic-gate 	 */
10787c478bd9Sstevel@tonic-gate 	serdinst = eventprop_lookup(sp, L_engine);
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate 	if (serdinst == NULL)
1081b7d3956bSstephh 		return (-1);
10827c478bd9Sstevel@tonic-gate 
10838e7248e5SStephen Hanson 	len = strlen(serdinst->u.stmt.np->u.event.ename->u.name.s) + 1;
1084049e2ea4SScott Davenport 	if (serdsuffix != NULL)
1085049e2ea4SScott Davenport 		len += strlen(serdsuffix);
10868e7248e5SStephen Hanson 	serdclass = MALLOC(len);
10878e7248e5SStephen Hanson 	if (serdsuffix != NULL)
10888e7248e5SStephen Hanson 		(void) snprintf(serdclass, len, "%s%s",
10898e7248e5SStephen Hanson 		    serdinst->u.stmt.np->u.event.ename->u.name.s, serdsuffix);
10908e7248e5SStephen Hanson 	else
10918e7248e5SStephen Hanson 		(void) snprintf(serdclass, len, "%s",
10928e7248e5SStephen Hanson 		    serdinst->u.stmt.np->u.event.ename->u.name.s);
10938e7248e5SStephen Hanson 	serdresource = ipath2str(NULL,
10948e7248e5SStephen Hanson 	    ipath(serdinst->u.stmt.np->u.event.epname));
10958e7248e5SStephen Hanson 	len += strlen(serdresource) + 1;
10968e7248e5SStephen Hanson 	serdname = MALLOC(len);
10978e7248e5SStephen Hanson 	(void) snprintf(serdname, len, "%s@%s", serdclass, serdresource);
1098049e2ea4SScott Davenport 	FREE(serdresource);
1099b7d3956bSstephh 
11007aec1d6eScindi 	/* handle serd engine "id" property, if there is one */
11017aec1d6eScindi 	if ((nid =
11027aec1d6eScindi 	    lut_lookup(serdinst->u.stmt.lutp, (void *)L_id, NULL)) != NULL) {
11037aec1d6eScindi 		struct evalue *gval;
11047aec1d6eScindi 		char suffixbuf[200];
11057aec1d6eScindi 		char *suffix;
11067aec1d6eScindi 		char *nserdname;
11077aec1d6eScindi 		size_t nname;
11087aec1d6eScindi 
11097aec1d6eScindi 		out(O_ALTFP|O_NONL, "serd \"%s\" id: ", serdname);
11107aec1d6eScindi 		ptree_name_iter(O_ALTFP|O_NONL, nid);
11117aec1d6eScindi 
11127aec1d6eScindi 		ASSERTinfo(nid->t == T_GLOBID, ptree_nodetype2str(nid->t));
11137aec1d6eScindi 
11147aec1d6eScindi 		if ((gval = lut_lookup(fmep->globals,
11157aec1d6eScindi 		    (void *)nid->u.globid.s, NULL)) == NULL) {
11167aec1d6eScindi 			out(O_ALTFP, " undefined");
11177aec1d6eScindi 		} else if (gval->t == UINT64) {
11187aec1d6eScindi 			out(O_ALTFP, " %llu", gval->v);
11197aec1d6eScindi 			(void) sprintf(suffixbuf, "%llu", gval->v);
11207aec1d6eScindi 			suffix = suffixbuf;
11217aec1d6eScindi 		} else {
112280ab886dSwesolows 			out(O_ALTFP, " \"%s\"", (char *)(uintptr_t)gval->v);
112380ab886dSwesolows 			suffix = (char *)(uintptr_t)gval->v;
11247aec1d6eScindi 		}
11257aec1d6eScindi 
11267aec1d6eScindi 		nname = strlen(serdname) + strlen(suffix) + 2;
11277aec1d6eScindi 		nserdname = MALLOC(nname);
11287aec1d6eScindi 		(void) snprintf(nserdname, nname, "%s:%s", serdname, suffix);
11297aec1d6eScindi 		FREE(serdname);
11307aec1d6eScindi 		serdname = nserdname;
11317aec1d6eScindi 	}
11327aec1d6eScindi 
1133b7d3956bSstephh 	/*
1134b7d3956bSstephh 	 * if the engine is empty, and we have an override for n/t then
1135b7d3956bSstephh 	 * destroy and recreate it.
1136b7d3956bSstephh 	 */
1137b7d3956bSstephh 	if ((serdn != -1 || serdt != NULL) && fmd_serd_exists(hdl, serdname) &&
1138b7d3956bSstephh 	    fmd_serd_empty(hdl, serdname))
1139b7d3956bSstephh 		fmd_serd_destroy(hdl, serdname);
1140b7d3956bSstephh 
11417c478bd9Sstevel@tonic-gate 	if (!fmd_serd_exists(hdl, serdname)) {
11427c478bd9Sstevel@tonic-gate 		struct node *nN, *nT;
1143e5ba14ffSstephh 		const char *s;
1144e5ba14ffSstephh 		struct node *nodep;
1145e5ba14ffSstephh 		struct config *cp;
1146e5ba14ffSstephh 		char *path;
1147e5ba14ffSstephh 		uint_t nval;
1148e5ba14ffSstephh 		hrtime_t tval;
1149e5ba14ffSstephh 		int i;
1150e5ba14ffSstephh 		char *ptr;
1151e5ba14ffSstephh 		int got_n_override = 0, got_t_override = 0;
11527c478bd9Sstevel@tonic-gate 
11537c478bd9Sstevel@tonic-gate 		/* no SERD engine yet, so create it */
1154e5ba14ffSstephh 		nodep = serdinst->u.stmt.np->u.event.epname;
1155e5ba14ffSstephh 		path = ipath2str(NULL, ipath(nodep));
1156e5ba14ffSstephh 		cp = config_lookup(fmep->config, path, 0);
1157e5ba14ffSstephh 		FREE((void *)path);
1158e5ba14ffSstephh 
1159e5ba14ffSstephh 		/*
1160e5ba14ffSstephh 		 * We allow serd paramaters to be overridden, either from
1161e5ba14ffSstephh 		 * eft.conf file values (if Serd_Override is set) or from
1162e5ba14ffSstephh 		 * driver properties (for "serd.io.device" engines).
1163e5ba14ffSstephh 		 */
1164e5ba14ffSstephh 		if (Serd_Override != NULL) {
1165e5ba14ffSstephh 			char *save_ptr, *ptr1, *ptr2, *ptr3;
1166e5ba14ffSstephh 			ptr3 = save_ptr = STRDUP(Serd_Override);
1167e5ba14ffSstephh 			while (*ptr3 != '\0') {
1168e5ba14ffSstephh 				ptr1 = strchr(ptr3, ',');
1169e5ba14ffSstephh 				*ptr1 = '\0';
11708e7248e5SStephen Hanson 				if (strcmp(ptr3, serdclass) == 0) {
1171e5ba14ffSstephh 					ptr2 =  strchr(ptr1 + 1, ',');
1172e5ba14ffSstephh 					*ptr2 = '\0';
1173e5ba14ffSstephh 					nval = atoi(ptr1 + 1);
1174e5ba14ffSstephh 					out(O_ALTFP, "serd override %s_n %d",
11758e7248e5SStephen Hanson 					    serdclass, nval);
1176e5ba14ffSstephh 					ptr3 =  strchr(ptr2 + 1, ' ');
1177e5ba14ffSstephh 					if (ptr3)
1178e5ba14ffSstephh 						*ptr3 = '\0';
1179e5ba14ffSstephh 					ptr = STRDUP(ptr2 + 1);
1180e5ba14ffSstephh 					out(O_ALTFP, "serd override %s_t %s",
11818e7248e5SStephen Hanson 					    serdclass, ptr);
1182e5ba14ffSstephh 					got_n_override = 1;
1183e5ba14ffSstephh 					got_t_override = 1;
1184e5ba14ffSstephh 					break;
1185e5ba14ffSstephh 				} else {
1186e5ba14ffSstephh 					ptr2 =  strchr(ptr1 + 1, ',');
1187e5ba14ffSstephh 					ptr3 =  strchr(ptr2 + 1, ' ');
1188e5ba14ffSstephh 					if (ptr3 == NULL)
1189e5ba14ffSstephh 						break;
1190e5ba14ffSstephh 				}
1191e5ba14ffSstephh 				ptr3++;
1192e5ba14ffSstephh 			}
1193e5ba14ffSstephh 			FREE(save_ptr);
1194e5ba14ffSstephh 		}
11957c478bd9Sstevel@tonic-gate 
1196e5ba14ffSstephh 		if (cp && got_n_override == 0) {
1197e5ba14ffSstephh 			/*
11988e7248e5SStephen Hanson 			 * convert serd engine class into property name
1199e5ba14ffSstephh 			 */
12008e7248e5SStephen Hanson 			char *prop_name = MALLOC(strlen(serdclass) + 3);
12018e7248e5SStephen Hanson 			for (i = 0; i < strlen(serdclass); i++) {
12028e7248e5SStephen Hanson 				if (serdclass[i] == '.')
12038e7248e5SStephen Hanson 					prop_name[i] = '_';
1204e5ba14ffSstephh 				else
12058e7248e5SStephen Hanson 					prop_name[i] = serdclass[i];
1206e5ba14ffSstephh 			}
12078e7248e5SStephen Hanson 			prop_name[i++] = '_';
12088e7248e5SStephen Hanson 			prop_name[i++] = 'n';
12098e7248e5SStephen Hanson 			prop_name[i] = '\0';
12108e7248e5SStephen Hanson 			if (s = config_getprop(cp, prop_name)) {
1211e5ba14ffSstephh 				nval = atoi(s);
12128e7248e5SStephen Hanson 				out(O_ALTFP, "serd override %s_n %s",
12138e7248e5SStephen Hanson 				    serdclass, s);
1214e5ba14ffSstephh 				got_n_override = 1;
1215e5ba14ffSstephh 			}
12168e7248e5SStephen Hanson 			prop_name[i - 1] = 't';
12178e7248e5SStephen Hanson 			if (s = config_getprop(cp, prop_name)) {
1218e5ba14ffSstephh 				ptr = STRDUP(s);
12198e7248e5SStephen Hanson 				out(O_ALTFP, "serd override %s_t %s",
12208e7248e5SStephen Hanson 				    serdclass, s);
1221e5ba14ffSstephh 				got_t_override = 1;
1222e5ba14ffSstephh 			}
12238e7248e5SStephen Hanson 			FREE(prop_name);
1224e5ba14ffSstephh 		}
12257c478bd9Sstevel@tonic-gate 
1226b7d3956bSstephh 		if (serdn != -1 && got_n_override == 0) {
1227b7d3956bSstephh 			nval = serdn;
12288e7248e5SStephen Hanson 			out(O_ALTFP, "serd override %s_n %d", serdclass, serdn);
1229b7d3956bSstephh 			got_n_override = 1;
1230b7d3956bSstephh 		}
1231b7d3956bSstephh 		if (serdt != NULL && got_t_override == 0) {
1232b7d3956bSstephh 			ptr = STRDUP(serdt);
12338e7248e5SStephen Hanson 			out(O_ALTFP, "serd override %s_t %s", serdclass, serdt);
1234b7d3956bSstephh 			got_t_override = 1;
1235b7d3956bSstephh 		}
1236b7d3956bSstephh 
1237e5ba14ffSstephh 		if (!got_n_override) {
1238e5ba14ffSstephh 			nN = lut_lookup(serdinst->u.stmt.lutp, (void *)L_N,
1239e5ba14ffSstephh 			    NULL);
1240e5ba14ffSstephh 			ASSERT(nN->t == T_NUM);
1241e5ba14ffSstephh 			nval = (uint_t)nN->u.ull;
1242e5ba14ffSstephh 		}
1243e5ba14ffSstephh 		if (!got_t_override) {
1244e5ba14ffSstephh 			nT = lut_lookup(serdinst->u.stmt.lutp, (void *)L_T,
1245e5ba14ffSstephh 			    NULL);
1246e5ba14ffSstephh 			ASSERT(nT->t == T_TIMEVAL);
1247e5ba14ffSstephh 			tval = (hrtime_t)nT->u.ull;
1248e5ba14ffSstephh 		} else {
1249e5ba14ffSstephh 			const unsigned long long *ullp;
1250e5ba14ffSstephh 			const char *suffix;
1251e5ba14ffSstephh 			int len;
1252e5ba14ffSstephh 
1253e5ba14ffSstephh 			len = strspn(ptr, "0123456789");
1254e5ba14ffSstephh 			suffix = stable(&ptr[len]);
1255e5ba14ffSstephh 			ullp = (unsigned long long *)lut_lookup(Timesuffixlut,
1256e5ba14ffSstephh 			    (void *)suffix, NULL);
1257e5ba14ffSstephh 			ptr[len] = '\0';
1258b7d3956bSstephh 			tval = strtoull(ptr, NULL, 0) * (ullp ? *ullp : 1ll);
1259e5ba14ffSstephh 			FREE(ptr);
1260e5ba14ffSstephh 		}
1261e5ba14ffSstephh 		fmd_serd_create(hdl, serdname, nval, tval);
12627c478bd9Sstevel@tonic-gate 	}
12637c478bd9Sstevel@tonic-gate 
1264b5016cbbSstephh 	newentp = MALLOC(sizeof (*newentp));
12658e7248e5SStephen Hanson 	newentp->ename = stable(serdclass);
12668e7248e5SStephen Hanson 	FREE(serdclass);
1267b5016cbbSstephh 	newentp->ipath = ipath(serdinst->u.stmt.np->u.event.epname);
1268b5016cbbSstephh 	newentp->hdl = hdl;
1269b5016cbbSstephh 	if (lut_lookup(SerdEngines, newentp, (lut_cmp)serd_cmp) == NULL) {
1270b5016cbbSstephh 		SerdEngines = lut_add(SerdEngines, (void *)newentp,
1271e5ba14ffSstephh 		    (void *)newentp, (lut_cmp)serd_cmp);
1272b5016cbbSstephh 		Serd_need_save = 1;
1273b5016cbbSstephh 		serd_save();
1274b5016cbbSstephh 	} else {
1275b5016cbbSstephh 		FREE(newentp);
1276b5016cbbSstephh 	}
1277b5016cbbSstephh 
12787c478bd9Sstevel@tonic-gate 
12797c478bd9Sstevel@tonic-gate 	/*
12807c478bd9Sstevel@tonic-gate 	 * increment SERD engine.  if engine fires, reset serd
1281b7d3956bSstephh 	 * engine and return trip_strcode if required.
12827c478bd9Sstevel@tonic-gate 	 */
1283b7d3956bSstephh 	for (i = 0; i < serdincrement; i++) {
1284b7d3956bSstephh 		if (fmd_serd_record(hdl, serdname, ffep)) {
1285b7d3956bSstephh 			fmd_case_add_serd(hdl, fmcase, serdname);
1286b7d3956bSstephh 			fmd_serd_reset(hdl, serdname);
1287b7d3956bSstephh 
1288b7d3956bSstephh 			if (ippp) {
1289b7d3956bSstephh 				struct node *tripinst =
1290b7d3956bSstephh 				    lut_lookup(serdinst->u.stmt.lutp,
1291b7d3956bSstephh 				    (void *)L_trip, NULL);
1292b7d3956bSstephh 				ASSERT(tripinst != NULL);
1293b7d3956bSstephh 				*enamep = tripinst->u.event.ename->u.name.s;
1294b7d3956bSstephh 				*ippp = ipath(tripinst->u.event.epname);
1295b7d3956bSstephh 				out(O_ALTFP|O_NONL,
1296b7d3956bSstephh 				    "[engine fired: %s, sending: ", serdname);
1297b7d3956bSstephh 				ipath_print(O_ALTFP|O_NONL, *enamep, *ippp);
1298b7d3956bSstephh 				out(O_ALTFP, "]");
1299b7d3956bSstephh 			} else {
1300b7d3956bSstephh 				out(O_ALTFP, "[engine fired: %s, no trip]",
1301b7d3956bSstephh 				    serdname);
1302b7d3956bSstephh 			}
1303b7d3956bSstephh 			FREE(serdname);
1304b7d3956bSstephh 			return (1);
1305b7d3956bSstephh 		}
13067c478bd9Sstevel@tonic-gate 	}
13077c478bd9Sstevel@tonic-gate 
13087c478bd9Sstevel@tonic-gate 	FREE(serdname);
13097c478bd9Sstevel@tonic-gate 	return (0);
13107c478bd9Sstevel@tonic-gate }
13117c478bd9Sstevel@tonic-gate 
13127c478bd9Sstevel@tonic-gate /*
13137c478bd9Sstevel@tonic-gate  * search a suspect list for upsets.  feed each upset to serd_eval() and
13147c478bd9Sstevel@tonic-gate  * build up tripped[], an array of ereports produced by the firing of
13157c478bd9Sstevel@tonic-gate  * any SERD engines.  then feed each ereport back into
13167c478bd9Sstevel@tonic-gate  * fme_receive_report().
13177c478bd9Sstevel@tonic-gate  *
13187c478bd9Sstevel@tonic-gate  * returns ntrip, the number of these ereports produced.
13197c478bd9Sstevel@tonic-gate  */
13207c478bd9Sstevel@tonic-gate static int
upsets_eval(struct fme * fmep,fmd_event_t * ffep)13217c478bd9Sstevel@tonic-gate upsets_eval(struct fme *fmep, fmd_event_t *ffep)
13227c478bd9Sstevel@tonic-gate {
13237c478bd9Sstevel@tonic-gate 	/* we build an array of tripped ereports that we send ourselves */
13247c478bd9Sstevel@tonic-gate 	struct {
13257c478bd9Sstevel@tonic-gate 		const char *ename;
13267c478bd9Sstevel@tonic-gate 		const struct ipath *ipp;
13277c478bd9Sstevel@tonic-gate 	} *tripped;
13287c478bd9Sstevel@tonic-gate 	struct event *sp;
13297c478bd9Sstevel@tonic-gate 	int ntrip, nupset, i;
13307c478bd9Sstevel@tonic-gate 
13317c478bd9Sstevel@tonic-gate 	/*
13327c478bd9Sstevel@tonic-gate 	 * count the number of upsets to determine the upper limit on
13337c478bd9Sstevel@tonic-gate 	 * expected trip ereport strings.  remember that one upset can
13347c478bd9Sstevel@tonic-gate 	 * lead to at most one ereport.
13357c478bd9Sstevel@tonic-gate 	 */
13367c478bd9Sstevel@tonic-gate 	nupset = 0;
13377c478bd9Sstevel@tonic-gate 	for (sp = fmep->suspects; sp; sp = sp->suspects) {
13387c478bd9Sstevel@tonic-gate 		if (sp->t == N_UPSET)
13397c478bd9Sstevel@tonic-gate 			nupset++;
13407c478bd9Sstevel@tonic-gate 	}
13417c478bd9Sstevel@tonic-gate 
13427c478bd9Sstevel@tonic-gate 	if (nupset == 0)
13437c478bd9Sstevel@tonic-gate 		return (0);
13447c478bd9Sstevel@tonic-gate 
13457c478bd9Sstevel@tonic-gate 	/*
13467c478bd9Sstevel@tonic-gate 	 * get to this point if we have upsets and expect some trip
13477c478bd9Sstevel@tonic-gate 	 * ereports
13487c478bd9Sstevel@tonic-gate 	 */
13497c478bd9Sstevel@tonic-gate 	tripped = alloca(sizeof (*tripped) * nupset);
13507c478bd9Sstevel@tonic-gate 	bzero((void *)tripped, sizeof (*tripped) * nupset);
13517c478bd9Sstevel@tonic-gate 
13527c478bd9Sstevel@tonic-gate 	ntrip = 0;
13537c478bd9Sstevel@tonic-gate 	for (sp = fmep->suspects; sp; sp = sp->suspects)
13543e8d8e18Sdb 		if (sp->t == N_UPSET &&
13557aec1d6eScindi 		    serd_eval(fmep, fmep->hdl, ffep, fmep->fmcase, sp,
1356b7d3956bSstephh 		    &tripped[ntrip].ename, &tripped[ntrip].ipp) == 1)
13577c478bd9Sstevel@tonic-gate 			ntrip++;
13587c478bd9Sstevel@tonic-gate 
135927134bdaSstephh 	for (i = 0; i < ntrip; i++) {
136027134bdaSstephh 		struct event *ep, *nep;
136127134bdaSstephh 		struct fme *nfmep;
136227134bdaSstephh 		fmd_case_t *fmcase;
136327134bdaSstephh 		const struct ipath *ipp;
136427134bdaSstephh 		const char *eventstring;
136527134bdaSstephh 		int prev_verbose;
136627134bdaSstephh 		unsigned long long my_delay = TIMEVAL_EVENTUALLY;
136727134bdaSstephh 		enum fme_state state;
136827134bdaSstephh 
136927134bdaSstephh 		/*
137027134bdaSstephh 		 * First try and evaluate a case with the trip ereport plus
137127134bdaSstephh 		 * all the other ereports that cause the trip. If that fails
137227134bdaSstephh 		 * to evaluate then try again with just this ereport on its own.
137327134bdaSstephh 		 */
137427134bdaSstephh 		out(O_ALTFP|O_NONL, "fme_receive_report_serd: ");
137527134bdaSstephh 		ipath_print(O_ALTFP|O_NONL, tripped[i].ename, tripped[i].ipp);
137627134bdaSstephh 		out(O_ALTFP|O_STAMP, NULL);
137727134bdaSstephh 		ep = fmep->e0;
137827134bdaSstephh 		eventstring = ep->enode->u.event.ename->u.name.s;
137927134bdaSstephh 		ipp = ep->ipp;
138027134bdaSstephh 
138127134bdaSstephh 		/*
138227134bdaSstephh 		 * create a duplicate fme and case
138327134bdaSstephh 		 */
138427134bdaSstephh 		fmcase = fmd_case_open(fmep->hdl, NULL);
138527134bdaSstephh 		out(O_ALTFP|O_NONL, "duplicate fme for event [");
138627134bdaSstephh 		ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
138727134bdaSstephh 		out(O_ALTFP, " ]");
1388705e9f42SStephen Hanson 
138927134bdaSstephh 		if ((nfmep = newfme(eventstring, ipp, fmep->hdl,
1390705e9f42SStephen Hanson 		    fmcase, ffep, ep->nvp)) == NULL) {
139127134bdaSstephh 			out(O_ALTFP|O_NONL, "[");
139227134bdaSstephh 			ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
139327134bdaSstephh 			out(O_ALTFP, " CANNOT DIAGNOSE]");
139427134bdaSstephh 			continue;
139527134bdaSstephh 		}
1396705e9f42SStephen Hanson 
139727134bdaSstephh 		Open_fme_count++;
139827134bdaSstephh 		nfmep->pull = fmep->pull;
139927134bdaSstephh 		init_fme_bufs(nfmep);
140027134bdaSstephh 		out(O_ALTFP|O_NONL, "[");
140127134bdaSstephh 		ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
140227134bdaSstephh 		out(O_ALTFP, " created FME%d, case %s]", nfmep->id,
140327134bdaSstephh 		    fmd_case_uuid(nfmep->hdl, nfmep->fmcase));
140427134bdaSstephh 		if (ffep) {
140527134bdaSstephh 			fmd_case_setprincipal(nfmep->hdl, nfmep->fmcase, ffep);
1406b7d3956bSstephh 			fmd_case_add_ereport(nfmep->hdl, nfmep->fmcase, ffep);
140727134bdaSstephh 			nfmep->e0r = ffep;
140827134bdaSstephh 		}
140927134bdaSstephh 
141027134bdaSstephh 		/*
141127134bdaSstephh 		 * add the original ereports
141227134bdaSstephh 		 */
141327134bdaSstephh 		for (ep = fmep->observations; ep; ep = ep->observations) {
141427134bdaSstephh 			eventstring = ep->enode->u.event.ename->u.name.s;
141527134bdaSstephh 			ipp = ep->ipp;
141627134bdaSstephh 			out(O_ALTFP|O_NONL, "adding event [");
141727134bdaSstephh 			ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
141827134bdaSstephh 			out(O_ALTFP, " ]");
141927134bdaSstephh 			nep = itree_lookup(nfmep->eventtree, eventstring, ipp);
142027134bdaSstephh 			if (nep->count++ == 0) {
142127134bdaSstephh 				nep->observations = nfmep->observations;
142227134bdaSstephh 				nfmep->observations = nep;
142327134bdaSstephh 				serialize_observation(nfmep, eventstring, ipp);
142427134bdaSstephh 				nep->nvp = evnv_dupnvl(ep->nvp);
142527134bdaSstephh 			}
1426b7d3956bSstephh 			if (ep->ffep && ep->ffep != ffep)
142727134bdaSstephh 				fmd_case_add_ereport(nfmep->hdl, nfmep->fmcase,
1428b7d3956bSstephh 				    ep->ffep);
142927134bdaSstephh 			stats_counter_bump(nfmep->Rcount);
143027134bdaSstephh 		}
143127134bdaSstephh 
143227134bdaSstephh 		/*
143327134bdaSstephh 		 * add the serd trigger ereport
143427134bdaSstephh 		 */
143527134bdaSstephh 		if ((ep = itree_lookup(nfmep->eventtree, tripped[i].ename,
143627134bdaSstephh 		    tripped[i].ipp)) == NULL) {
143727134bdaSstephh 			/*
143827134bdaSstephh 			 * The trigger ereport is not in the instance tree. It
143927134bdaSstephh 			 * was presumably removed by prune_propagations() as
144027134bdaSstephh 			 * this combination of events is not present in the
144127134bdaSstephh 			 * rules.
144227134bdaSstephh 			 */
144327134bdaSstephh 			out(O_ALTFP, "upsets_eval: e0 not in instance tree");
144432d4e834STarik Soydan 			Undiag_reason = UD_VAL_BADEVENTI;
144527134bdaSstephh 			goto retry_lone_ereport;
144627134bdaSstephh 		}
144727134bdaSstephh 		out(O_ALTFP|O_NONL, "adding event [");
144827134bdaSstephh 		ipath_print(O_ALTFP|O_NONL, tripped[i].ename, tripped[i].ipp);
144927134bdaSstephh 		out(O_ALTFP, " ]");
145027134bdaSstephh 		nfmep->ecurrent = ep;
145127134bdaSstephh 		ep->nvp = NULL;
145227134bdaSstephh 		ep->count = 1;
145327134bdaSstephh 		ep->observations = nfmep->observations;
145427134bdaSstephh 		nfmep->observations = ep;
145527134bdaSstephh 
145627134bdaSstephh 		/*
145727134bdaSstephh 		 * just peek first.
145827134bdaSstephh 		 */
145927134bdaSstephh 		nfmep->peek = 1;
146027134bdaSstephh 		prev_verbose = Verbose;
146127134bdaSstephh 		if (Debug == 0)
146227134bdaSstephh 			Verbose = 0;
146327134bdaSstephh 		lut_walk(nfmep->eventtree, (lut_cb)clear_arrows, (void *)nfmep);
146427134bdaSstephh 		state = hypothesise(nfmep, nfmep->e0, nfmep->ull, &my_delay);
146527134bdaSstephh 		nfmep->peek = 0;
146627134bdaSstephh 		Verbose = prev_verbose;
146727134bdaSstephh 		if (state == FME_DISPROVED) {
146827134bdaSstephh 			out(O_ALTFP, "upsets_eval: hypothesis disproved");
146932d4e834STarik Soydan 			Undiag_reason = UD_VAL_UNSOLVD;
147027134bdaSstephh retry_lone_ereport:
147127134bdaSstephh 			/*
147227134bdaSstephh 			 * However the trigger ereport on its own might be
147327134bdaSstephh 			 * diagnosable, so check for that. Undo the new fme
147427134bdaSstephh 			 * and case we just created and call fme_receive_report.
147527134bdaSstephh 			 */
147627134bdaSstephh 			out(O_ALTFP|O_NONL, "[");
147727134bdaSstephh 			ipath_print(O_ALTFP|O_NONL, tripped[i].ename,
147827134bdaSstephh 			    tripped[i].ipp);
147927134bdaSstephh 			out(O_ALTFP, " retrying with just trigger ereport]");
148027134bdaSstephh 			itree_free(nfmep->eventtree);
148127134bdaSstephh 			nfmep->eventtree = NULL;
148227134bdaSstephh 			structconfig_free(nfmep->config);
148327134bdaSstephh 			nfmep->config = NULL;
148427134bdaSstephh 			destroy_fme_bufs(nfmep);
148527134bdaSstephh 			fmd_case_close(nfmep->hdl, nfmep->fmcase);
148627134bdaSstephh 			fme_receive_report(fmep->hdl, ffep,
148727134bdaSstephh 			    tripped[i].ename, tripped[i].ipp, NULL);
148827134bdaSstephh 			continue;
148927134bdaSstephh 		}
149027134bdaSstephh 
149127134bdaSstephh 		/*
149227134bdaSstephh 		 * and evaluate
149327134bdaSstephh 		 */
149427134bdaSstephh 		serialize_observation(nfmep, tripped[i].ename, tripped[i].ipp);
149527134bdaSstephh 		fme_eval(nfmep, ffep);
149627134bdaSstephh 	}
14977c478bd9Sstevel@tonic-gate 
14987c478bd9Sstevel@tonic-gate 	return (ntrip);
14997c478bd9Sstevel@tonic-gate }
15007c478bd9Sstevel@tonic-gate 
15017c478bd9Sstevel@tonic-gate /*
15027c478bd9Sstevel@tonic-gate  * fme_receive_external_report -- call when an external ereport comes in
15037c478bd9Sstevel@tonic-gate  *
15047c478bd9Sstevel@tonic-gate  * this routine just converts the relevant information from the ereport
15057c478bd9Sstevel@tonic-gate  * into a format used internally and passes it on to fme_receive_report().
15067c478bd9Sstevel@tonic-gate  */
15077c478bd9Sstevel@tonic-gate void
fme_receive_external_report(fmd_hdl_t * hdl,fmd_event_t * ffep,nvlist_t * nvl,const char * class)15087c478bd9Sstevel@tonic-gate fme_receive_external_report(fmd_hdl_t *hdl, fmd_event_t *ffep, nvlist_t *nvl,
1509602ca9eaScth     const char *class)
15107c478bd9Sstevel@tonic-gate {
1511602ca9eaScth 	struct node		*epnamenp;
1512602ca9eaScth 	fmd_case_t		*fmcase;
1513602ca9eaScth 	const struct ipath	*ipp;
1514705e9f42SStephen Hanson 	nvlist_t		*detector = NULL;
15157c478bd9Sstevel@tonic-gate 
1516602ca9eaScth 	class = stable(class);
1517b5016cbbSstephh 
1518602ca9eaScth 	/* Get the component path from the ereport */
1519602ca9eaScth 	epnamenp = platform_getpath(nvl);
1520602ca9eaScth 
1521602ca9eaScth 	/* See if we ended up without a path. */
1522602ca9eaScth 	if (epnamenp == NULL) {
1523602ca9eaScth 		/* See if class permits silent discard on unknown component. */
1524602ca9eaScth 		if (lut_lookup(Ereportenames_discard, (void *)class, NULL)) {
1525602ca9eaScth 			out(O_ALTFP|O_VERB2, "Unable to map \"%s\" ereport "
1526602ca9eaScth 			    "to component path, but silent discard allowed.",
1527602ca9eaScth 			    class);
1528602ca9eaScth 		} else {
1529602ca9eaScth 			/*
1530602ca9eaScth 			 * XFILE: Failure to find a component is bad unless
1531602ca9eaScth 			 * 'discard_if_config_unknown=1' was specified in the
1532602ca9eaScth 			 * ereport definition. Indicate undiagnosable.
1533602ca9eaScth 			 */
153432d4e834STarik Soydan 			Undiag_reason = UD_VAL_NOPATH;
1535602ca9eaScth 			fmcase = fmd_case_open(hdl, NULL);
1536705e9f42SStephen Hanson 
1537705e9f42SStephen Hanson 			/*
1538705e9f42SStephen Hanson 			 * We don't have a component path here (which means that
1539705e9f42SStephen Hanson 			 * the detector was not in hc-scheme and couldn't be
1540705e9f42SStephen Hanson 			 * converted to hc-scheme. Report the raw detector as
1541705e9f42SStephen Hanson 			 * the suspect resource if there is one.
1542705e9f42SStephen Hanson 			 */
1543705e9f42SStephen Hanson 			(void) nvlist_lookup_nvlist(nvl, FM_EREPORT_DETECTOR,
1544705e9f42SStephen Hanson 			    &detector);
1545705e9f42SStephen Hanson 			publish_undiagnosable(hdl, ffep, fmcase, detector,
1546705e9f42SStephen Hanson 			    (char *)class);
1547602ca9eaScth 		}
15487c478bd9Sstevel@tonic-gate 		return;
15497c478bd9Sstevel@tonic-gate 	}
15507c478bd9Sstevel@tonic-gate 
15517c478bd9Sstevel@tonic-gate 	ipp = ipath(epnamenp);
15527c478bd9Sstevel@tonic-gate 	tree_free(epnamenp);
1553602ca9eaScth 	fme_receive_report(hdl, ffep, class, ipp, nvl);
15547c478bd9Sstevel@tonic-gate }
15557c478bd9Sstevel@tonic-gate 
155608f6c065Sgavinm /*ARGSUSED*/
155708f6c065Sgavinm void
fme_receive_repair_list(fmd_hdl_t * hdl,fmd_event_t * ffep,nvlist_t * nvl,const char * eventstring)155808f6c065Sgavinm fme_receive_repair_list(fmd_hdl_t *hdl, fmd_event_t *ffep, nvlist_t *nvl,
155908f6c065Sgavinm     const char *eventstring)
156008f6c065Sgavinm {
156108f6c065Sgavinm 	char *uuid;
156208f6c065Sgavinm 	nvlist_t **nva;
156308f6c065Sgavinm 	uint_t nvc;
156408f6c065Sgavinm 	const struct ipath *ipp;
156508f6c065Sgavinm 
156608f6c065Sgavinm 	if (nvlist_lookup_string(nvl, FM_SUSPECT_UUID, &uuid) != 0 ||
156708f6c065Sgavinm 	    nvlist_lookup_nvlist_array(nvl, FM_SUSPECT_FAULT_LIST,
156808f6c065Sgavinm 	    &nva, &nvc) != 0) {
156908f6c065Sgavinm 		out(O_ALTFP, "No uuid or fault list for list.repaired event");
157008f6c065Sgavinm 		return;
157108f6c065Sgavinm 	}
157208f6c065Sgavinm 
157308f6c065Sgavinm 	out(O_ALTFP, "Processing list.repaired from case %s", uuid);
157408f6c065Sgavinm 
157508f6c065Sgavinm 	while (nvc-- != 0) {
157608f6c065Sgavinm 		/*
1577b5016cbbSstephh 		 * Reset any istat or serd engine associated with this path.
157808f6c065Sgavinm 		 */
157908f6c065Sgavinm 		char *path;
158008f6c065Sgavinm 
158108f6c065Sgavinm 		if ((ipp = platform_fault2ipath(*nva++)) == NULL)
158208f6c065Sgavinm 			continue;
158308f6c065Sgavinm 
158408f6c065Sgavinm 		path = ipath2str(NULL, ipp);
158508f6c065Sgavinm 		out(O_ALTFP, "fme_receive_repair_list: resetting state for %s",
158608f6c065Sgavinm 		    path);
158708f6c065Sgavinm 		FREE(path);
158808f6c065Sgavinm 
158908f6c065Sgavinm 		lut_walk(Istats, (lut_cb)istat_counter_reset_cb, (void *)ipp);
159008f6c065Sgavinm 		istat_save();
159108f6c065Sgavinm 
1592b5016cbbSstephh 		lut_walk(SerdEngines, (lut_cb)serd_reset_cb, (void *)ipp);
1593b5016cbbSstephh 		serd_save();
159408f6c065Sgavinm 	}
159508f6c065Sgavinm }
159608f6c065Sgavinm 
1597e5ba14ffSstephh /*ARGSUSED*/
1598e5ba14ffSstephh void
fme_receive_topology_change(void)1599e5ba14ffSstephh fme_receive_topology_change(void)
1600e5ba14ffSstephh {
1601e5ba14ffSstephh 	lut_walk(Istats, (lut_cb)istat_counter_topo_chg_cb, NULL);
1602e5ba14ffSstephh 	istat_save();
1603e5ba14ffSstephh 
1604e5ba14ffSstephh 	lut_walk(SerdEngines, (lut_cb)serd_topo_chg_cb, NULL);
1605e5ba14ffSstephh 	serd_save();
1606e5ba14ffSstephh }
1607e5ba14ffSstephh 
16087aec1d6eScindi static int mark_arrows(struct fme *fmep, struct event *ep, int mark,
160900d0963fSdilpreet     unsigned long long at_latest_by, unsigned long long *pdelay, int keep);
16107aec1d6eScindi 
16117aec1d6eScindi /* ARGSUSED */
16127aec1d6eScindi static void
clear_arrows(struct event * ep,struct event * ep2,struct fme * fmep)16137aec1d6eScindi clear_arrows(struct event *ep, struct event *ep2, struct fme *fmep)
16147aec1d6eScindi {
16157aec1d6eScindi 	struct bubble *bp;
16167aec1d6eScindi 	struct arrowlist *ap;
16177aec1d6eScindi 
16187aec1d6eScindi 	ep->cached_state = 0;
161900d0963fSdilpreet 	ep->keep_in_tree = 0;
16207aec1d6eScindi 	for (bp = itree_next_bubble(ep, NULL); bp;
16217aec1d6eScindi 	    bp = itree_next_bubble(ep, bp)) {
16227aec1d6eScindi 		if (bp->t != B_FROM)
16237aec1d6eScindi 			continue;
16247aec1d6eScindi 		bp->mark = 0;
16257aec1d6eScindi 		for (ap = itree_next_arrow(bp, NULL); ap;
16267aec1d6eScindi 		    ap = itree_next_arrow(bp, ap))
16277aec1d6eScindi 			ap->arrowp->mark = 0;
16287aec1d6eScindi 	}
16297aec1d6eScindi }
16307aec1d6eScindi 
16317c478bd9Sstevel@tonic-gate static void
fme_receive_report(fmd_hdl_t * hdl,fmd_event_t * ffep,const char * eventstring,const struct ipath * ipp,nvlist_t * nvl)16327c478bd9Sstevel@tonic-gate fme_receive_report(fmd_hdl_t *hdl, fmd_event_t *ffep,
16337c478bd9Sstevel@tonic-gate     const char *eventstring, const struct ipath *ipp, nvlist_t *nvl)
16347c478bd9Sstevel@tonic-gate {
16357c478bd9Sstevel@tonic-gate 	struct event *ep;
16367c478bd9Sstevel@tonic-gate 	struct fme *fmep = NULL;
16370cc1f05eSjrutt 	struct fme *ofmep = NULL;
16380cc1f05eSjrutt 	struct fme *cfmep, *svfmep;
16397c478bd9Sstevel@tonic-gate 	int matched = 0;
16400cc1f05eSjrutt 	nvlist_t *defect;
1641b5016cbbSstephh 	fmd_case_t *fmcase;
1642705e9f42SStephen Hanson 	char *reason;
16437c478bd9Sstevel@tonic-gate 
16447c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_NONL, "fme_receive_report: ");
16457c478bd9Sstevel@tonic-gate 	ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
16467c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_STAMP, NULL);
16477c478bd9Sstevel@tonic-gate 
16487c478bd9Sstevel@tonic-gate 	/* decide which FME it goes to */
16497c478bd9Sstevel@tonic-gate 	for (fmep = FMElist; fmep; fmep = fmep->next) {
16507c478bd9Sstevel@tonic-gate 		int prev_verbose;
16517c478bd9Sstevel@tonic-gate 		unsigned long long my_delay = TIMEVAL_EVENTUALLY;
16527c478bd9Sstevel@tonic-gate 		enum fme_state state;
16537aec1d6eScindi 		nvlist_t *pre_peek_nvp = NULL;
16547c478bd9Sstevel@tonic-gate 
16550cc1f05eSjrutt 		if (fmep->overflow) {
16560cc1f05eSjrutt 			if (!(fmd_case_closed(fmep->hdl, fmep->fmcase)))
16570cc1f05eSjrutt 				ofmep = fmep;
16580cc1f05eSjrutt 
16590cc1f05eSjrutt 			continue;
16600cc1f05eSjrutt 		}
16610cc1f05eSjrutt 
1662d96ce684Sstephh 		/*
1663d96ce684Sstephh 		 * ignore solved or closed cases
1664d96ce684Sstephh 		 */
1665d96ce684Sstephh 		if (fmep->posted_suspects ||
1666d96ce684Sstephh 		    fmd_case_solved(fmep->hdl, fmep->fmcase) ||
1667d96ce684Sstephh 		    fmd_case_closed(fmep->hdl, fmep->fmcase))
1668d96ce684Sstephh 			continue;
1669d96ce684Sstephh 
16707c478bd9Sstevel@tonic-gate 		/* look up event in event tree for this FME */
16717c478bd9Sstevel@tonic-gate 		if ((ep = itree_lookup(fmep->eventtree,
16727c478bd9Sstevel@tonic-gate 		    eventstring, ipp)) == NULL)
16737c478bd9Sstevel@tonic-gate 			continue;
16747c478bd9Sstevel@tonic-gate 
16757c478bd9Sstevel@tonic-gate 		/* note observation */
16767c478bd9Sstevel@tonic-gate 		fmep->ecurrent = ep;
16777c478bd9Sstevel@tonic-gate 		if (ep->count++ == 0) {
16787c478bd9Sstevel@tonic-gate 			/* link it into list of observations seen */
16797c478bd9Sstevel@tonic-gate 			ep->observations = fmep->observations;
16807c478bd9Sstevel@tonic-gate 			fmep->observations = ep;
16817c478bd9Sstevel@tonic-gate 			ep->nvp = evnv_dupnvl(nvl);
16827aec1d6eScindi 		} else {
16837aec1d6eScindi 			/* use new payload values for peek */
16847aec1d6eScindi 			pre_peek_nvp = ep->nvp;
16857aec1d6eScindi 			ep->nvp = evnv_dupnvl(nvl);
16867c478bd9Sstevel@tonic-gate 		}
16877c478bd9Sstevel@tonic-gate 
16887c478bd9Sstevel@tonic-gate 		/* tell hypothesise() not to mess with suspect list */
16897c478bd9Sstevel@tonic-gate 		fmep->peek = 1;
16907c478bd9Sstevel@tonic-gate 
16917c478bd9Sstevel@tonic-gate 		/* don't want this to be verbose (unless Debug is set) */
16927c478bd9Sstevel@tonic-gate 		prev_verbose = Verbose;
16937c478bd9Sstevel@tonic-gate 		if (Debug == 0)
16947c478bd9Sstevel@tonic-gate 			Verbose = 0;
16957c478bd9Sstevel@tonic-gate 
16967aec1d6eScindi 		lut_walk(fmep->eventtree, (lut_cb)clear_arrows, (void *)fmep);
16977aec1d6eScindi 		state = hypothesise(fmep, fmep->e0, fmep->ull, &my_delay);
16987c478bd9Sstevel@tonic-gate 
16997c478bd9Sstevel@tonic-gate 		fmep->peek = 0;
17007c478bd9Sstevel@tonic-gate 
17017c478bd9Sstevel@tonic-gate 		/* put verbose flag back */
17027c478bd9Sstevel@tonic-gate 		Verbose = prev_verbose;
17037c478bd9Sstevel@tonic-gate 
17047c478bd9Sstevel@tonic-gate 		if (state != FME_DISPROVED) {
17057c478bd9Sstevel@tonic-gate 			/* found an FME that explains the ereport */
17067c478bd9Sstevel@tonic-gate 			matched++;
17077c478bd9Sstevel@tonic-gate 			out(O_ALTFP|O_NONL, "[");
17087c478bd9Sstevel@tonic-gate 			ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
17097c478bd9Sstevel@tonic-gate 			out(O_ALTFP, " explained by FME%d]", fmep->id);
17107c478bd9Sstevel@tonic-gate 
1711aab83bb8SJosef 'Jeff' Sipek 			nvlist_free(pre_peek_nvp);
17127aec1d6eScindi 
17137c478bd9Sstevel@tonic-gate 			if (ep->count == 1)
17147c478bd9Sstevel@tonic-gate 				serialize_observation(fmep, eventstring, ipp);
17157c478bd9Sstevel@tonic-gate 
1716b7d3956bSstephh 			if (ffep) {
17177c478bd9Sstevel@tonic-gate 				fmd_case_add_ereport(hdl, fmep->fmcase, ffep);
1718b7d3956bSstephh 				ep->ffep = ffep;
1719b7d3956bSstephh 			}
17207c478bd9Sstevel@tonic-gate 
17217c478bd9Sstevel@tonic-gate 			stats_counter_bump(fmep->Rcount);
17227c478bd9Sstevel@tonic-gate 
17237c478bd9Sstevel@tonic-gate 			/* re-eval FME */
17247c478bd9Sstevel@tonic-gate 			fme_eval(fmep, ffep);
17257c478bd9Sstevel@tonic-gate 		} else {
17267c478bd9Sstevel@tonic-gate 
17277c478bd9Sstevel@tonic-gate 			/* not a match, undo noting of observation */
17287c478bd9Sstevel@tonic-gate 			fmep->ecurrent = NULL;
17297c478bd9Sstevel@tonic-gate 			if (--ep->count == 0) {
17307c478bd9Sstevel@tonic-gate 				/* unlink it from observations */
17317c478bd9Sstevel@tonic-gate 				fmep->observations = ep->observations;
17327c478bd9Sstevel@tonic-gate 				ep->observations = NULL;
17337c478bd9Sstevel@tonic-gate 				nvlist_free(ep->nvp);
17347c478bd9Sstevel@tonic-gate 				ep->nvp = NULL;
17357aec1d6eScindi 			} else {
17367aec1d6eScindi 				nvlist_free(ep->nvp);
17377aec1d6eScindi 				ep->nvp = pre_peek_nvp;
17387c478bd9Sstevel@tonic-gate 			}
17397c478bd9Sstevel@tonic-gate 		}
17407c478bd9Sstevel@tonic-gate 	}
17417c478bd9Sstevel@tonic-gate 
17427c478bd9Sstevel@tonic-gate 	if (matched)
17437c478bd9Sstevel@tonic-gate 		return;	/* explained by at least one existing FME */
17447c478bd9Sstevel@tonic-gate 
17457c478bd9Sstevel@tonic-gate 	/* clean up closed fmes */
17460cc1f05eSjrutt 	cfmep = ClosedFMEs;
17470cc1f05eSjrutt 	while (cfmep != NULL) {
17480cc1f05eSjrutt 		svfmep = cfmep->next;
17490cc1f05eSjrutt 		destroy_fme(cfmep);
17500cc1f05eSjrutt 		cfmep = svfmep;
17517c478bd9Sstevel@tonic-gate 	}
17527c478bd9Sstevel@tonic-gate 	ClosedFMEs = NULL;
17537c478bd9Sstevel@tonic-gate 
17540cc1f05eSjrutt 	if (ofmep) {
17550cc1f05eSjrutt 		out(O_ALTFP|O_NONL, "[");
17560cc1f05eSjrutt 		ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
17570cc1f05eSjrutt 		out(O_ALTFP, " ADDING TO OVERFLOW FME]");
17580cc1f05eSjrutt 		if (ffep)
17590cc1f05eSjrutt 			fmd_case_add_ereport(hdl, ofmep->fmcase, ffep);
17600cc1f05eSjrutt 
17610cc1f05eSjrutt 		return;
17620cc1f05eSjrutt 
17630cc1f05eSjrutt 	} else if (Max_fme && (Open_fme_count >= Max_fme)) {
17640cc1f05eSjrutt 		out(O_ALTFP|O_NONL, "[");
17650cc1f05eSjrutt 		ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
17660cc1f05eSjrutt 		out(O_ALTFP, " MAX OPEN FME REACHED]");
1767b5016cbbSstephh 
1768b5016cbbSstephh 		fmcase = fmd_case_open(hdl, NULL);
1769b5016cbbSstephh 
17700cc1f05eSjrutt 		/* Create overflow fme */
1771705e9f42SStephen Hanson 		if ((fmep = newfme(eventstring, ipp, hdl, fmcase, ffep,
1772705e9f42SStephen Hanson 		    nvl)) == NULL) {
17730cc1f05eSjrutt 			out(O_ALTFP|O_NONL, "[");
17740cc1f05eSjrutt 			ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
17750cc1f05eSjrutt 			out(O_ALTFP, " CANNOT OPEN OVERFLOW FME]");
17760cc1f05eSjrutt 			return;
17770cc1f05eSjrutt 		}
17780cc1f05eSjrutt 
17790cc1f05eSjrutt 		Open_fme_count++;
17800cc1f05eSjrutt 
17810cc1f05eSjrutt 		init_fme_bufs(fmep);
17820cc1f05eSjrutt 		fmep->overflow = B_TRUE;
17830cc1f05eSjrutt 
17840cc1f05eSjrutt 		if (ffep)
17850cc1f05eSjrutt 			fmd_case_add_ereport(hdl, fmep->fmcase, ffep);
17860cc1f05eSjrutt 
178732d4e834STarik Soydan 		Undiag_reason = UD_VAL_MAXFME;
178832d4e834STarik Soydan 		defect = fmd_nvl_create_fault(hdl,
178932d4e834STarik Soydan 		    undiag_2defect_str(Undiag_reason), 100, NULL, NULL, NULL);
1790705e9f42SStephen Hanson 		reason = undiag_2reason_str(Undiag_reason, NULL);
1791705e9f42SStephen Hanson 		(void) nvlist_add_string(defect, UNDIAG_REASON, reason);
1792705e9f42SStephen Hanson 		FREE(reason);
17930cc1f05eSjrutt 		fmd_case_add_suspect(hdl, fmep->fmcase, defect);
17940cc1f05eSjrutt 		fmd_case_solve(hdl, fmep->fmcase);
179532d4e834STarik Soydan 		Undiag_reason = UD_VAL_UNKNOWN;
17960cc1f05eSjrutt 		return;
17970cc1f05eSjrutt 	}
17980cc1f05eSjrutt 
1799b5016cbbSstephh 	/* open a case */
1800b5016cbbSstephh 	fmcase = fmd_case_open(hdl, NULL);
1801b5016cbbSstephh 
18027c478bd9Sstevel@tonic-gate 	/* start a new FME */
1803705e9f42SStephen Hanson 	if ((fmep = newfme(eventstring, ipp, hdl, fmcase, ffep, nvl)) == NULL) {
18047c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_NONL, "[");
18057c478bd9Sstevel@tonic-gate 		ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
18067c478bd9Sstevel@tonic-gate 		out(O_ALTFP, " CANNOT DIAGNOSE]");
18077c478bd9Sstevel@tonic-gate 		return;
18087c478bd9Sstevel@tonic-gate 	}
18097c478bd9Sstevel@tonic-gate 
18100cc1f05eSjrutt 	Open_fme_count++;
18110cc1f05eSjrutt 
18127c478bd9Sstevel@tonic-gate 	init_fme_bufs(fmep);
18137c478bd9Sstevel@tonic-gate 
18147c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_NONL, "[");
18157c478bd9Sstevel@tonic-gate 	ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
18167c478bd9Sstevel@tonic-gate 	out(O_ALTFP, " created FME%d, case %s]", fmep->id,
18177c478bd9Sstevel@tonic-gate 	    fmd_case_uuid(hdl, fmep->fmcase));
18187c478bd9Sstevel@tonic-gate 
18197c478bd9Sstevel@tonic-gate 	ep = fmep->e0;
18207c478bd9Sstevel@tonic-gate 	ASSERT(ep != NULL);
18217c478bd9Sstevel@tonic-gate 
18227c478bd9Sstevel@tonic-gate 	/* note observation */
18237c478bd9Sstevel@tonic-gate 	fmep->ecurrent = ep;
18247c478bd9Sstevel@tonic-gate 	if (ep->count++ == 0) {
18257c478bd9Sstevel@tonic-gate 		/* link it into list of observations seen */
18267c478bd9Sstevel@tonic-gate 		ep->observations = fmep->observations;
18277c478bd9Sstevel@tonic-gate 		fmep->observations = ep;
18287c478bd9Sstevel@tonic-gate 		ep->nvp = evnv_dupnvl(nvl);
18297c478bd9Sstevel@tonic-gate 		serialize_observation(fmep, eventstring, ipp);
18307aec1d6eScindi 	} else {
18317aec1d6eScindi 		/* new payload overrides any previous */
18327aec1d6eScindi 		nvlist_free(ep->nvp);
18337aec1d6eScindi 		ep->nvp = evnv_dupnvl(nvl);
18347c478bd9Sstevel@tonic-gate 	}
18357c478bd9Sstevel@tonic-gate 
18367c478bd9Sstevel@tonic-gate 	stats_counter_bump(fmep->Rcount);
18377c478bd9Sstevel@tonic-gate 
18387c478bd9Sstevel@tonic-gate 	if (ffep) {
18397c478bd9Sstevel@tonic-gate 		fmd_case_add_ereport(hdl, fmep->fmcase, ffep);
18407c478bd9Sstevel@tonic-gate 		fmd_case_setprincipal(hdl, fmep->fmcase, ffep);
18417c478bd9Sstevel@tonic-gate 		fmep->e0r = ffep;
1842b7d3956bSstephh 		ep->ffep = ffep;
18437c478bd9Sstevel@tonic-gate 	}
18447c478bd9Sstevel@tonic-gate 
18457c478bd9Sstevel@tonic-gate 	/* give the diagnosis algorithm a shot at the new FME state */
18467c478bd9Sstevel@tonic-gate 	fme_eval(fmep, ffep);
18477c478bd9Sstevel@tonic-gate }
18487c478bd9Sstevel@tonic-gate 
18497c478bd9Sstevel@tonic-gate void
fme_status(int flags)18507c478bd9Sstevel@tonic-gate fme_status(int flags)
18517c478bd9Sstevel@tonic-gate {
18527c478bd9Sstevel@tonic-gate 	struct fme *fmep;
18537c478bd9Sstevel@tonic-gate 
18547c478bd9Sstevel@tonic-gate 	if (FMElist == NULL) {
18557c478bd9Sstevel@tonic-gate 		out(flags, "No fault management exercises underway.");
18567c478bd9Sstevel@tonic-gate 		return;
18577c478bd9Sstevel@tonic-gate 	}
18587c478bd9Sstevel@tonic-gate 
18597c478bd9Sstevel@tonic-gate 	for (fmep = FMElist; fmep; fmep = fmep->next)
18607c478bd9Sstevel@tonic-gate 		fme_print(flags, fmep);
18617c478bd9Sstevel@tonic-gate }
18627c478bd9Sstevel@tonic-gate 
18637c478bd9Sstevel@tonic-gate /*
18647c478bd9Sstevel@tonic-gate  * "indent" routines used mostly for nicely formatted debug output, but also
18657c478bd9Sstevel@tonic-gate  * for sanity checking for infinite recursion bugs.
18667c478bd9Sstevel@tonic-gate  */
18677c478bd9Sstevel@tonic-gate 
18687c478bd9Sstevel@tonic-gate #define	MAX_INDENT 1024
18697c478bd9Sstevel@tonic-gate static const char *indent_s[MAX_INDENT];
18707c478bd9Sstevel@tonic-gate static int current_indent;
18717c478bd9Sstevel@tonic-gate 
18727c478bd9Sstevel@tonic-gate static void
indent_push(const char * s)18737c478bd9Sstevel@tonic-gate indent_push(const char *s)
18747c478bd9Sstevel@tonic-gate {
18757c478bd9Sstevel@tonic-gate 	if (current_indent < MAX_INDENT)
18767c478bd9Sstevel@tonic-gate 		indent_s[current_indent++] = s;
18777c478bd9Sstevel@tonic-gate 	else
18787c478bd9Sstevel@tonic-gate 		out(O_DIE, "unexpected recursion depth (%d)", current_indent);
18797c478bd9Sstevel@tonic-gate }
18807c478bd9Sstevel@tonic-gate 
18817c478bd9Sstevel@tonic-gate static void
indent_set(const char * s)18827c478bd9Sstevel@tonic-gate indent_set(const char *s)
18837c478bd9Sstevel@tonic-gate {
18847c478bd9Sstevel@tonic-gate 	current_indent = 0;
18857c478bd9Sstevel@tonic-gate 	indent_push(s);
18867c478bd9Sstevel@tonic-gate }
18877c478bd9Sstevel@tonic-gate 
18887c478bd9Sstevel@tonic-gate static void
indent_pop(void)18897c478bd9Sstevel@tonic-gate indent_pop(void)
18907c478bd9Sstevel@tonic-gate {
18917c478bd9Sstevel@tonic-gate 	if (current_indent > 0)
18927c478bd9Sstevel@tonic-gate 		current_indent--;
18937c478bd9Sstevel@tonic-gate 	else
18947c478bd9Sstevel@tonic-gate 		out(O_DIE, "recursion underflow");
18957c478bd9Sstevel@tonic-gate }
18967c478bd9Sstevel@tonic-gate 
18977c478bd9Sstevel@tonic-gate static void
indent(void)18987c478bd9Sstevel@tonic-gate indent(void)
18997c478bd9Sstevel@tonic-gate {
19007c478bd9Sstevel@tonic-gate 	int i;
19017c478bd9Sstevel@tonic-gate 	if (!Verbose)
19027c478bd9Sstevel@tonic-gate 		return;
19037c478bd9Sstevel@tonic-gate 	for (i = 0; i < current_indent; i++)
19047c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB|O_NONL, indent_s[i]);
19057c478bd9Sstevel@tonic-gate }
19067c478bd9Sstevel@tonic-gate 
19077c478bd9Sstevel@tonic-gate #define	SLNEW		1
19087c478bd9Sstevel@tonic-gate #define	SLCHANGED	2
19097c478bd9Sstevel@tonic-gate #define	SLWAIT		3
19107c478bd9Sstevel@tonic-gate #define	SLDISPROVED	4
19117c478bd9Sstevel@tonic-gate 
19127c478bd9Sstevel@tonic-gate static void
print_suspects(int circumstance,struct fme * fmep)19137c478bd9Sstevel@tonic-gate print_suspects(int circumstance, struct fme *fmep)
19147c478bd9Sstevel@tonic-gate {
19157c478bd9Sstevel@tonic-gate 	struct event *ep;
19167c478bd9Sstevel@tonic-gate 
19177c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_NONL, "[");
19187c478bd9Sstevel@tonic-gate 	if (circumstance == SLCHANGED) {
19197c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_NONL, "FME%d diagnosis changed. state: %s, "
19207c478bd9Sstevel@tonic-gate 		    "suspect list:", fmep->id, fme_state2str(fmep->state));
19217c478bd9Sstevel@tonic-gate 	} else if (circumstance == SLWAIT) {
1922b5016cbbSstephh 		out(O_ALTFP|O_NONL, "FME%d set wait timer %ld ", fmep->id,
1923b5016cbbSstephh 		    fmep->timer);
19247c478bd9Sstevel@tonic-gate 		ptree_timeval(O_ALTFP|O_NONL, &fmep->wull);
19257c478bd9Sstevel@tonic-gate 	} else if (circumstance == SLDISPROVED) {
19267c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_NONL, "FME%d DIAGNOSIS UNKNOWN", fmep->id);
19277c478bd9Sstevel@tonic-gate 	} else {
19287c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_NONL, "FME%d DIAGNOSIS PRODUCED:", fmep->id);
19297c478bd9Sstevel@tonic-gate 	}
19307c478bd9Sstevel@tonic-gate 
19317c478bd9Sstevel@tonic-gate 	if (circumstance == SLWAIT || circumstance == SLDISPROVED) {
19327c478bd9Sstevel@tonic-gate 		out(O_ALTFP, "]");
19337c478bd9Sstevel@tonic-gate 		return;
19347c478bd9Sstevel@tonic-gate 	}
19357c478bd9Sstevel@tonic-gate 
19367c478bd9Sstevel@tonic-gate 	for (ep = fmep->suspects; ep; ep = ep->suspects) {
19377c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_NONL, " ");
19387c478bd9Sstevel@tonic-gate 		itree_pevent_brief(O_ALTFP|O_NONL, ep);
19397c478bd9Sstevel@tonic-gate 	}
19407c478bd9Sstevel@tonic-gate 	out(O_ALTFP, "]");
19417c478bd9Sstevel@tonic-gate }
19427c478bd9Sstevel@tonic-gate 
19437c478bd9Sstevel@tonic-gate static struct node *
eventprop_lookup(struct event * ep,const char * propname)19447c478bd9Sstevel@tonic-gate eventprop_lookup(struct event *ep, const char *propname)
19457c478bd9Sstevel@tonic-gate {
19467c478bd9Sstevel@tonic-gate 	return (lut_lookup(ep->props, (void *)propname, NULL));
19477c478bd9Sstevel@tonic-gate }
19487c478bd9Sstevel@tonic-gate 
19497c478bd9Sstevel@tonic-gate #define	MAXDIGITIDX	23
19507c478bd9Sstevel@tonic-gate static char numbuf[MAXDIGITIDX + 1];
19517c478bd9Sstevel@tonic-gate 
19527c478bd9Sstevel@tonic-gate static int
node2uint(struct node * n,uint_t * valp)19537c478bd9Sstevel@tonic-gate node2uint(struct node *n, uint_t *valp)
19547c478bd9Sstevel@tonic-gate {
19557c478bd9Sstevel@tonic-gate 	struct evalue value;
19567c478bd9Sstevel@tonic-gate 	struct lut *globals = NULL;
19577c478bd9Sstevel@tonic-gate 
19587c478bd9Sstevel@tonic-gate 	if (n == NULL)
19597c478bd9Sstevel@tonic-gate 		return (1);
19607c478bd9Sstevel@tonic-gate 
19617c478bd9Sstevel@tonic-gate 	/*
19627c478bd9Sstevel@tonic-gate 	 * check value.v since we are being asked to convert an unsigned
19637c478bd9Sstevel@tonic-gate 	 * long long int to an unsigned int
19647c478bd9Sstevel@tonic-gate 	 */
19657c478bd9Sstevel@tonic-gate 	if (! eval_expr(n, NULL, NULL, &globals, NULL, NULL, 0, &value) ||
19667c478bd9Sstevel@tonic-gate 	    value.t != UINT64 || value.v > (1ULL << 32))
19677c478bd9Sstevel@tonic-gate 		return (1);
19687c478bd9Sstevel@tonic-gate 
19697c478bd9Sstevel@tonic-gate 	*valp = (uint_t)value.v;
19707c478bd9Sstevel@tonic-gate 
19717c478bd9Sstevel@tonic-gate 	return (0);
19727c478bd9Sstevel@tonic-gate }
19737c478bd9Sstevel@tonic-gate 
19747c478bd9Sstevel@tonic-gate static nvlist_t *
node2fmri(struct node * n)19757c478bd9Sstevel@tonic-gate node2fmri(struct node *n)
19767c478bd9Sstevel@tonic-gate {
19777c478bd9Sstevel@tonic-gate 	nvlist_t **pa, *f, *p;
19787c478bd9Sstevel@tonic-gate 	struct node *nc;
19797c478bd9Sstevel@tonic-gate 	uint_t depth = 0;
19807c478bd9Sstevel@tonic-gate 	char *numstr, *nullbyte;
19817c478bd9Sstevel@tonic-gate 	char *failure;
19827c478bd9Sstevel@tonic-gate 	int err, i;
19837c478bd9Sstevel@tonic-gate 
19847c478bd9Sstevel@tonic-gate 	/* XXX do we need to be able to handle a non-T_NAME node? */
19857c478bd9Sstevel@tonic-gate 	if (n == NULL || n->t != T_NAME)
19867c478bd9Sstevel@tonic-gate 		return (NULL);
19877c478bd9Sstevel@tonic-gate 
19887c478bd9Sstevel@tonic-gate 	for (nc = n; nc != NULL; nc = nc->u.name.next) {
19897c478bd9Sstevel@tonic-gate 		if (nc->u.name.child == NULL || nc->u.name.child->t != T_NUM)
19907c478bd9Sstevel@tonic-gate 			break;
19917c478bd9Sstevel@tonic-gate 		depth++;
19927c478bd9Sstevel@tonic-gate 	}
19937c478bd9Sstevel@tonic-gate 
19947c478bd9Sstevel@tonic-gate 	if (nc != NULL) {
19957c478bd9Sstevel@tonic-gate 		/* We bailed early, something went wrong */
19967c478bd9Sstevel@tonic-gate 		return (NULL);
19977c478bd9Sstevel@tonic-gate 	}
19987c478bd9Sstevel@tonic-gate 
19997c478bd9Sstevel@tonic-gate 	if ((err = nvlist_xalloc(&f, NV_UNIQUE_NAME, &Eft_nv_hdl)) != 0)
20007c478bd9Sstevel@tonic-gate 		out(O_DIE|O_SYS, "alloc of fmri nvl failed");
20017c478bd9Sstevel@tonic-gate 	pa = alloca(depth * sizeof (nvlist_t *));
20027c478bd9Sstevel@tonic-gate 	for (i = 0; i < depth; i++)
20037c478bd9Sstevel@tonic-gate 		pa[i] = NULL;
20047c478bd9Sstevel@tonic-gate 
20057c478bd9Sstevel@tonic-gate 	err = nvlist_add_string(f, FM_FMRI_SCHEME, FM_FMRI_SCHEME_HC);
20067c478bd9Sstevel@tonic-gate 	err |= nvlist_add_uint8(f, FM_VERSION, FM_HC_SCHEME_VERSION);
20077c478bd9Sstevel@tonic-gate 	err |= nvlist_add_string(f, FM_FMRI_HC_ROOT, "");
20087c478bd9Sstevel@tonic-gate 	err |= nvlist_add_uint32(f, FM_FMRI_HC_LIST_SZ, depth);
20097c478bd9Sstevel@tonic-gate 	if (err != 0) {
20107c478bd9Sstevel@tonic-gate 		failure = "basic construction of FMRI failed";
20117c478bd9Sstevel@tonic-gate 		goto boom;
20127c478bd9Sstevel@tonic-gate 	}
20137c478bd9Sstevel@tonic-gate 
20147c478bd9Sstevel@tonic-gate 	numbuf[MAXDIGITIDX] = '\0';
20157c478bd9Sstevel@tonic-gate 	nullbyte = &numbuf[MAXDIGITIDX];
20167c478bd9Sstevel@tonic-gate 	i = 0;
20177c478bd9Sstevel@tonic-gate 
20187c478bd9Sstevel@tonic-gate 	for (nc = n; nc != NULL; nc = nc->u.name.next) {
20197c478bd9Sstevel@tonic-gate 		err = nvlist_xalloc(&p, NV_UNIQUE_NAME, &Eft_nv_hdl);
20207c478bd9Sstevel@tonic-gate 		if (err != 0) {
20217c478bd9Sstevel@tonic-gate 			failure = "alloc of an hc-pair failed";
20227c478bd9Sstevel@tonic-gate 			goto boom;
20237c478bd9Sstevel@tonic-gate 		}
20247c478bd9Sstevel@tonic-gate 		err = nvlist_add_string(p, FM_FMRI_HC_NAME, nc->u.name.s);
20257c478bd9Sstevel@tonic-gate 		numstr = ulltostr(nc->u.name.child->u.ull, nullbyte);
20267c478bd9Sstevel@tonic-gate 		err |= nvlist_add_string(p, FM_FMRI_HC_ID, numstr);
20277c478bd9Sstevel@tonic-gate 		if (err != 0) {
20287c478bd9Sstevel@tonic-gate 			failure = "construction of an hc-pair failed";
20297c478bd9Sstevel@tonic-gate 			goto boom;
20307c478bd9Sstevel@tonic-gate 		}
20317c478bd9Sstevel@tonic-gate 		pa[i++] = p;
20327c478bd9Sstevel@tonic-gate 	}
20337c478bd9Sstevel@tonic-gate 
20347c478bd9Sstevel@tonic-gate 	err = nvlist_add_nvlist_array(f, FM_FMRI_HC_LIST, pa, depth);
20357c478bd9Sstevel@tonic-gate 	if (err == 0) {
20367c478bd9Sstevel@tonic-gate 		for (i = 0; i < depth; i++)
2037aab83bb8SJosef 'Jeff' Sipek 			nvlist_free(pa[i]);
20387c478bd9Sstevel@tonic-gate 		return (f);
20397c478bd9Sstevel@tonic-gate 	}
20407c478bd9Sstevel@tonic-gate 	failure = "addition of hc-pair array to FMRI failed";
20417c478bd9Sstevel@tonic-gate 
20427c478bd9Sstevel@tonic-gate boom:
20437c478bd9Sstevel@tonic-gate 	for (i = 0; i < depth; i++)
2044aab83bb8SJosef 'Jeff' Sipek 		nvlist_free(pa[i]);
20457c478bd9Sstevel@tonic-gate 	nvlist_free(f);
20467c478bd9Sstevel@tonic-gate 	out(O_DIE, "%s", failure);
20477c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
204880ab886dSwesolows 	return (NULL);
20497c478bd9Sstevel@tonic-gate }
20507c478bd9Sstevel@tonic-gate 
2051e5ba14ffSstephh /* an ipath cache entry is an array of these, with s==NULL at the end */
2052e5ba14ffSstephh struct ipath {
2053e5ba14ffSstephh 	const char *s;	/* component name (in stable) */
2054e5ba14ffSstephh 	int i;		/* instance number */
2055e5ba14ffSstephh };
2056e5ba14ffSstephh 
2057e5ba14ffSstephh static nvlist_t *
ipath2fmri(struct ipath * ipath)2058e5ba14ffSstephh ipath2fmri(struct ipath *ipath)
2059e5ba14ffSstephh {
2060e5ba14ffSstephh 	nvlist_t **pa, *f, *p;
2061e5ba14ffSstephh 	uint_t depth = 0;
2062e5ba14ffSstephh 	char *numstr, *nullbyte;
2063e5ba14ffSstephh 	char *failure;
2064e5ba14ffSstephh 	int err, i;
2065e5ba14ffSstephh 	struct ipath *ipp;
2066e5ba14ffSstephh 
2067e5ba14ffSstephh 	for (ipp = ipath; ipp->s != NULL; ipp++)
2068e5ba14ffSstephh 		depth++;
2069e5ba14ffSstephh 
2070e5ba14ffSstephh 	if ((err = nvlist_xalloc(&f, NV_UNIQUE_NAME, &Eft_nv_hdl)) != 0)
2071e5ba14ffSstephh 		out(O_DIE|O_SYS, "alloc of fmri nvl failed");
2072e5ba14ffSstephh 	pa = alloca(depth * sizeof (nvlist_t *));
2073e5ba14ffSstephh 	for (i = 0; i < depth; i++)
2074e5ba14ffSstephh 		pa[i] = NULL;
2075e5ba14ffSstephh 
2076e5ba14ffSstephh 	err = nvlist_add_string(f, FM_FMRI_SCHEME, FM_FMRI_SCHEME_HC);
2077e5ba14ffSstephh 	err |= nvlist_add_uint8(f, FM_VERSION, FM_HC_SCHEME_VERSION);
2078e5ba14ffSstephh 	err |= nvlist_add_string(f, FM_FMRI_HC_ROOT, "");
2079e5ba14ffSstephh 	err |= nvlist_add_uint32(f, FM_FMRI_HC_LIST_SZ, depth);
2080e5ba14ffSstephh 	if (err != 0) {
2081e5ba14ffSstephh 		failure = "basic construction of FMRI failed";
2082e5ba14ffSstephh 		goto boom;
2083e5ba14ffSstephh 	}
2084e5ba14ffSstephh 
2085e5ba14ffSstephh 	numbuf[MAXDIGITIDX] = '\0';
2086e5ba14ffSstephh 	nullbyte = &numbuf[MAXDIGITIDX];
2087e5ba14ffSstephh 	i = 0;
2088e5ba14ffSstephh 
2089e5ba14ffSstephh 	for (ipp = ipath; ipp->s != NULL; ipp++) {
2090e5ba14ffSstephh 		err = nvlist_xalloc(&p, NV_UNIQUE_NAME, &Eft_nv_hdl);
2091e5ba14ffSstephh 		if (err != 0) {
2092e5ba14ffSstephh 			failure = "alloc of an hc-pair failed";
2093e5ba14ffSstephh 			goto boom;
2094e5ba14ffSstephh 		}
2095e5ba14ffSstephh 		err = nvlist_add_string(p, FM_FMRI_HC_NAME, ipp->s);
2096e5ba14ffSstephh 		numstr = ulltostr(ipp->i, nullbyte);
2097e5ba14ffSstephh 		err |= nvlist_add_string(p, FM_FMRI_HC_ID, numstr);
2098e5ba14ffSstephh 		if (err != 0) {
2099e5ba14ffSstephh 			failure = "construction of an hc-pair failed";
2100e5ba14ffSstephh 			goto boom;
2101e5ba14ffSstephh 		}
2102e5ba14ffSstephh 		pa[i++] = p;
2103e5ba14ffSstephh 	}
2104e5ba14ffSstephh 
2105e5ba14ffSstephh 	err = nvlist_add_nvlist_array(f, FM_FMRI_HC_LIST, pa, depth);
2106e5ba14ffSstephh 	if (err == 0) {
2107e5ba14ffSstephh 		for (i = 0; i < depth; i++)
2108aab83bb8SJosef 'Jeff' Sipek 			nvlist_free(pa[i]);
2109e5ba14ffSstephh 		return (f);
2110e5ba14ffSstephh 	}
2111e5ba14ffSstephh 	failure = "addition of hc-pair array to FMRI failed";
2112e5ba14ffSstephh 
2113e5ba14ffSstephh boom:
2114e5ba14ffSstephh 	for (i = 0; i < depth; i++)
2115aab83bb8SJosef 'Jeff' Sipek 		nvlist_free(pa[i]);
2116e5ba14ffSstephh 	nvlist_free(f);
2117e5ba14ffSstephh 	out(O_DIE, "%s", failure);
2118e5ba14ffSstephh 	/*NOTREACHED*/
2119e5ba14ffSstephh 	return (NULL);
2120e5ba14ffSstephh }
2121e5ba14ffSstephh 
21227c478bd9Sstevel@tonic-gate static uint8_t
percentof(uint_t part,uint_t whole)21237c478bd9Sstevel@tonic-gate percentof(uint_t part, uint_t whole)
21247c478bd9Sstevel@tonic-gate {
21257c478bd9Sstevel@tonic-gate 	unsigned long long p = part * 1000;
21267c478bd9Sstevel@tonic-gate 
21277c478bd9Sstevel@tonic-gate 	return ((p / whole / 10) + (((p / whole % 10) >= 5) ? 1 : 0));
21287c478bd9Sstevel@tonic-gate }
21297c478bd9Sstevel@tonic-gate 
213080ab886dSwesolows struct rsl {
21317c478bd9Sstevel@tonic-gate 	struct event *suspect;
21327c478bd9Sstevel@tonic-gate 	nvlist_t *asru;
21337c478bd9Sstevel@tonic-gate 	nvlist_t *fru;
21347c478bd9Sstevel@tonic-gate 	nvlist_t *rsrc;
21357c478bd9Sstevel@tonic-gate };
21367c478bd9Sstevel@tonic-gate 
2137b7d3956bSstephh static void publish_suspects(struct fme *fmep, struct rsl *srl);
2138b7d3956bSstephh 
21397c478bd9Sstevel@tonic-gate /*
21407c478bd9Sstevel@tonic-gate  *  rslfree -- free internal members of struct rsl not expected to be
21417c478bd9Sstevel@tonic-gate  *	freed elsewhere.
21427c478bd9Sstevel@tonic-gate  */
21437c478bd9Sstevel@tonic-gate static void
rslfree(struct rsl * freeme)21447c478bd9Sstevel@tonic-gate rslfree(struct rsl *freeme)
21457c478bd9Sstevel@tonic-gate {
2146aab83bb8SJosef 'Jeff' Sipek 	nvlist_free(freeme->asru);
2147aab83bb8SJosef 'Jeff' Sipek 	nvlist_free(freeme->fru);
2148aab83bb8SJosef 'Jeff' Sipek 	if (freeme->rsrc != freeme->asru)
21497c478bd9Sstevel@tonic-gate 		nvlist_free(freeme->rsrc);
21507c478bd9Sstevel@tonic-gate }
21517c478bd9Sstevel@tonic-gate 
21527c478bd9Sstevel@tonic-gate /*
21537c478bd9Sstevel@tonic-gate  *  rslcmp -- compare two rsl structures.  Use the following
21547c478bd9Sstevel@tonic-gate  *	comparisons to establish cardinality:
21557c478bd9Sstevel@tonic-gate  *
21567c478bd9Sstevel@tonic-gate  *	1. Name of the suspect's class. (simple strcmp)
21577c478bd9Sstevel@tonic-gate  *	2. Name of the suspect's ASRU. (trickier, since nvlist)
21587c478bd9Sstevel@tonic-gate  *
21597c478bd9Sstevel@tonic-gate  */
21607c478bd9Sstevel@tonic-gate static int
rslcmp(const void * a,const void * b)21617c478bd9Sstevel@tonic-gate rslcmp(const void *a, const void *b)
21627c478bd9Sstevel@tonic-gate {
21637c478bd9Sstevel@tonic-gate 	struct rsl *r1 = (struct rsl *)a;
21647c478bd9Sstevel@tonic-gate 	struct rsl *r2 = (struct rsl *)b;
21657c478bd9Sstevel@tonic-gate 	int rv;
21667c478bd9Sstevel@tonic-gate 
21677c478bd9Sstevel@tonic-gate 	rv = strcmp(r1->suspect->enode->u.event.ename->u.name.s,
21687c478bd9Sstevel@tonic-gate 	    r2->suspect->enode->u.event.ename->u.name.s);
21697c478bd9Sstevel@tonic-gate 	if (rv != 0)
21707c478bd9Sstevel@tonic-gate 		return (rv);
21717c478bd9Sstevel@tonic-gate 
2172b7d3956bSstephh 	if (r1->rsrc == NULL && r2->rsrc == NULL)
21737c478bd9Sstevel@tonic-gate 		return (0);
2174b7d3956bSstephh 	if (r1->rsrc == NULL)
21757c478bd9Sstevel@tonic-gate 		return (-1);
2176b7d3956bSstephh 	if (r2->rsrc == NULL)
21777c478bd9Sstevel@tonic-gate 		return (1);
2178b7d3956bSstephh 	return (evnv_cmpnvl(r1->rsrc, r2->rsrc, 0));
21797c478bd9Sstevel@tonic-gate }
21807c478bd9Sstevel@tonic-gate 
21817c478bd9Sstevel@tonic-gate /*
21827c478bd9Sstevel@tonic-gate  * get_resources -- for a given suspect, determine what ASRU, FRU and
21837c478bd9Sstevel@tonic-gate  *     RSRC nvlists should be advertised in the final suspect list.
21847c478bd9Sstevel@tonic-gate  */
21857c478bd9Sstevel@tonic-gate void
get_resources(struct event * sp,struct rsl * rsrcs,struct config * croot)21867c478bd9Sstevel@tonic-gate get_resources(struct event *sp, struct rsl *rsrcs, struct config *croot)
21877c478bd9Sstevel@tonic-gate {
21887c478bd9Sstevel@tonic-gate 	struct node *asrudef, *frudef;
2189da40b264SAlex Wilson 	const struct ipath *asrupath, *frupath;
2190da40b264SAlex Wilson 	nvlist_t *asru = NULL, *fru = NULL;
21917c478bd9Sstevel@tonic-gate 	nvlist_t *rsrc = NULL;
21927c478bd9Sstevel@tonic-gate 	char *pathstr;
21937c478bd9Sstevel@tonic-gate 
21947c478bd9Sstevel@tonic-gate 	/*
21957c478bd9Sstevel@tonic-gate 	 * First find any ASRU and/or FRU defined in the
21967c478bd9Sstevel@tonic-gate 	 * initial fault tree.
21977c478bd9Sstevel@tonic-gate 	 */
21987c478bd9Sstevel@tonic-gate 	asrudef = eventprop_lookup(sp, L_ASRU);
21997c478bd9Sstevel@tonic-gate 	frudef = eventprop_lookup(sp, L_FRU);
22007c478bd9Sstevel@tonic-gate 
22017c478bd9Sstevel@tonic-gate 	/*
2202da40b264SAlex Wilson 	 * Create ipaths based on those definitions
22037c478bd9Sstevel@tonic-gate 	 */
2204da40b264SAlex Wilson 	asrupath = ipath(asrudef);
2205da40b264SAlex Wilson 	frupath = ipath(frudef);
22067c478bd9Sstevel@tonic-gate 
22077c478bd9Sstevel@tonic-gate 	/*
2208b7d3956bSstephh 	 *  Allow for platform translations of the FMRIs
22097c478bd9Sstevel@tonic-gate 	 */
2210da40b264SAlex Wilson 	pathstr = ipath2str(NULL, sp->ipp);
2211da40b264SAlex Wilson 	platform_unit_translate(is_defect(sp->t), croot, TOPO_PROP_RESOURCE,
2212da40b264SAlex Wilson 	    &rsrc, pathstr);
2213da40b264SAlex Wilson 	FREE(pathstr);
22147c478bd9Sstevel@tonic-gate 
2215da40b264SAlex Wilson 	pathstr = ipath2str(NULL, asrupath);
2216da40b264SAlex Wilson 	platform_unit_translate(is_defect(sp->t), croot, TOPO_PROP_ASRU,
2217da40b264SAlex Wilson 	    &asru, pathstr);
22187c478bd9Sstevel@tonic-gate 	FREE(pathstr);
2219da40b264SAlex Wilson 
2220da40b264SAlex Wilson 	pathstr = ipath2str(NULL, frupath);
2221da40b264SAlex Wilson 	platform_unit_translate(is_defect(sp->t), croot, TOPO_PROP_FRU,
2222da40b264SAlex Wilson 	    &fru, pathstr);
2223da40b264SAlex Wilson 	FREE(pathstr);
2224da40b264SAlex Wilson 
22257c478bd9Sstevel@tonic-gate 	rsrcs->suspect = sp;
22267c478bd9Sstevel@tonic-gate 	rsrcs->asru = asru;
22277c478bd9Sstevel@tonic-gate 	rsrcs->fru = fru;
22287c478bd9Sstevel@tonic-gate 	rsrcs->rsrc = rsrc;
22297c478bd9Sstevel@tonic-gate }
22307c478bd9Sstevel@tonic-gate 
22317c478bd9Sstevel@tonic-gate /*
22327c478bd9Sstevel@tonic-gate  * trim_suspects -- prior to publishing, we may need to remove some
22337c478bd9Sstevel@tonic-gate  *    suspects from the list.  If we're auto-closing upsets, we don't
22347c478bd9Sstevel@tonic-gate  *    want any of those in the published list.  If the ASRUs for multiple
22357c478bd9Sstevel@tonic-gate  *    defects resolve to the same ASRU (driver) we only want to publish
22367c478bd9Sstevel@tonic-gate  *    that as a single suspect.
22377c478bd9Sstevel@tonic-gate  */
2238b7d3956bSstephh static int
trim_suspects(struct fme * fmep,struct rsl * begin,struct rsl * begin2,fmd_event_t * ffep)2239b7d3956bSstephh trim_suspects(struct fme *fmep, struct rsl *begin, struct rsl *begin2,
22408e7248e5SStephen Hanson     fmd_event_t *ffep)
22417c478bd9Sstevel@tonic-gate {
22427c478bd9Sstevel@tonic-gate 	struct event *ep;
2243b7d3956bSstephh 	struct rsl *rp = begin;
2244b7d3956bSstephh 	struct rsl *rp2 = begin2;
2245b7d3956bSstephh 	int mess_zero_count = 0;
2246b7d3956bSstephh 	int serd_rval;
2247b7d3956bSstephh 	uint_t messval;
22487c478bd9Sstevel@tonic-gate 
2249b7d3956bSstephh 	/* remove any unwanted upsets and populate our array */
22507c478bd9Sstevel@tonic-gate 	for (ep = fmep->psuspects; ep; ep = ep->psuspects) {
2251b7d3956bSstephh 		if (is_upset(ep->t))
22527c478bd9Sstevel@tonic-gate 			continue;
2253b7d3956bSstephh 		serd_rval = serd_eval(fmep, fmep->hdl, ffep, fmep->fmcase, ep,
2254b7d3956bSstephh 		    NULL, NULL);
2255b7d3956bSstephh 		if (serd_rval == 0)
2256b7d3956bSstephh 			continue;
2257b7d3956bSstephh 		if (node2uint(eventprop_lookup(ep, L_message),
2258b7d3956bSstephh 		    &messval) == 0 && messval == 0) {
2259b7d3956bSstephh 			get_resources(ep, rp2, fmep->config);
2260b7d3956bSstephh 			rp2++;
2261b7d3956bSstephh 			mess_zero_count++;
2262b7d3956bSstephh 		} else {
2263b7d3956bSstephh 			get_resources(ep, rp, fmep->config);
2264b7d3956bSstephh 			rp++;
2265b7d3956bSstephh 			fmep->nsuspects++;
2266b7d3956bSstephh 		}
22677c478bd9Sstevel@tonic-gate 	}
2268b7d3956bSstephh 	return (mess_zero_count);
22697c478bd9Sstevel@tonic-gate }
22707c478bd9Sstevel@tonic-gate 
22717aec1d6eScindi /*
22727aec1d6eScindi  * addpayloadprop -- add a payload prop to a problem
22737aec1d6eScindi  */
22747aec1d6eScindi static void
addpayloadprop(const char * lhs,struct evalue * rhs,nvlist_t * fault)22757aec1d6eScindi addpayloadprop(const char *lhs, struct evalue *rhs, nvlist_t *fault)
22767aec1d6eScindi {
2277b7d3956bSstephh 	nvlist_t *rsrc, *hcs;
2278b7d3956bSstephh 
22797aec1d6eScindi 	ASSERT(fault != NULL);
22807aec1d6eScindi 	ASSERT(lhs != NULL);
22817aec1d6eScindi 	ASSERT(rhs != NULL);
22827aec1d6eScindi 
2283b7d3956bSstephh 	if (nvlist_lookup_nvlist(fault, FM_FAULT_RESOURCE, &rsrc) != 0)
2284b7d3956bSstephh 		out(O_DIE, "cannot add payloadprop \"%s\" to fault", lhs);
2285b7d3956bSstephh 
2286b7d3956bSstephh 	if (nvlist_lookup_nvlist(rsrc, FM_FMRI_HC_SPECIFIC, &hcs) != 0) {
2287b7d3956bSstephh 		out(O_ALTFP|O_VERB2, "addpayloadprop: create hc_specific");
2288b7d3956bSstephh 		if (nvlist_xalloc(&hcs, NV_UNIQUE_NAME, &Eft_nv_hdl) != 0)
2289b7d3956bSstephh 			out(O_DIE,
2290b7d3956bSstephh 			    "cannot add payloadprop \"%s\" to fault", lhs);
2291b7d3956bSstephh 		if (nvlist_add_nvlist(rsrc, FM_FMRI_HC_SPECIFIC, hcs) != 0)
2292b7d3956bSstephh 			out(O_DIE,
2293b7d3956bSstephh 			    "cannot add payloadprop \"%s\" to fault", lhs);
2294b7d3956bSstephh 		nvlist_free(hcs);
2295b7d3956bSstephh 		if (nvlist_lookup_nvlist(rsrc, FM_FMRI_HC_SPECIFIC, &hcs) != 0)
2296b7d3956bSstephh 			out(O_DIE,
2297b7d3956bSstephh 			    "cannot add payloadprop \"%s\" to fault", lhs);
2298b7d3956bSstephh 	} else
2299b7d3956bSstephh 		out(O_ALTFP|O_VERB2, "addpayloadprop: reuse hc_specific");
2300b7d3956bSstephh 
23017aec1d6eScindi 	if (rhs->t == UINT64) {
23027aec1d6eScindi 		out(O_ALTFP|O_VERB2, "addpayloadprop: %s=%llu", lhs, rhs->v);
23037aec1d6eScindi 
2304b7d3956bSstephh 		if (nvlist_add_uint64(hcs, lhs, rhs->v) != 0)
23057aec1d6eScindi 			out(O_DIE,
23067aec1d6eScindi 			    "cannot add payloadprop \"%s\" to fault", lhs);
23077aec1d6eScindi 	} else {
23087aec1d6eScindi 		out(O_ALTFP|O_VERB2, "addpayloadprop: %s=\"%s\"",
230980ab886dSwesolows 		    lhs, (char *)(uintptr_t)rhs->v);
23107aec1d6eScindi 
2311b7d3956bSstephh 		if (nvlist_add_string(hcs, lhs, (char *)(uintptr_t)rhs->v) != 0)
23127aec1d6eScindi 			out(O_DIE,
23137aec1d6eScindi 			    "cannot add payloadprop \"%s\" to fault", lhs);
23147aec1d6eScindi 	}
23157aec1d6eScindi }
23167aec1d6eScindi 
23177aec1d6eScindi static char *Istatbuf;
23187aec1d6eScindi static char *Istatbufptr;
23197aec1d6eScindi static int Istatsz;
23207aec1d6eScindi 
23217aec1d6eScindi /*
23227aec1d6eScindi  * istataddsize -- calculate size of istat and add it to Istatsz
23237aec1d6eScindi  */
23247aec1d6eScindi /*ARGSUSED2*/
23257aec1d6eScindi static void
istataddsize(const struct istat_entry * lhs,struct stats * rhs,void * arg)23267aec1d6eScindi istataddsize(const struct istat_entry *lhs, struct stats *rhs, void *arg)
23277aec1d6eScindi {
23287aec1d6eScindi 	int val;
23297aec1d6eScindi 
23307aec1d6eScindi 	ASSERT(lhs != NULL);
23317aec1d6eScindi 	ASSERT(rhs != NULL);
23327aec1d6eScindi 
23337aec1d6eScindi 	if ((val = stats_counter_value(rhs)) == 0)
23347aec1d6eScindi 		return;	/* skip zero-valued stats */
23357aec1d6eScindi 
23367aec1d6eScindi 	/* count up the size of the stat name */
23377aec1d6eScindi 	Istatsz += ipath2strlen(lhs->ename, lhs->ipath);
23387aec1d6eScindi 	Istatsz++;	/* for the trailing NULL byte */
23397aec1d6eScindi 
23407aec1d6eScindi 	/* count up the size of the stat value */
23417aec1d6eScindi 	Istatsz += snprintf(NULL, 0, "%d", val);
23427aec1d6eScindi 	Istatsz++;	/* for the trailing NULL byte */
23437aec1d6eScindi }
23447aec1d6eScindi 
23457aec1d6eScindi /*
23467aec1d6eScindi  * istat2str -- serialize an istat, writing result to *Istatbufptr
23477aec1d6eScindi  */
23487aec1d6eScindi /*ARGSUSED2*/
23497aec1d6eScindi static void
istat2str(const struct istat_entry * lhs,struct stats * rhs,void * arg)23507aec1d6eScindi istat2str(const struct istat_entry *lhs, struct stats *rhs, void *arg)
23517aec1d6eScindi {
23527aec1d6eScindi 	char *str;
23537aec1d6eScindi 	int len;
23547aec1d6eScindi 	int val;
23557aec1d6eScindi 
23567aec1d6eScindi 	ASSERT(lhs != NULL);
23577aec1d6eScindi 	ASSERT(rhs != NULL);
23587aec1d6eScindi 
23597aec1d6eScindi 	if ((val = stats_counter_value(rhs)) == 0)
23607aec1d6eScindi 		return;	/* skip zero-valued stats */
23617aec1d6eScindi 
23627aec1d6eScindi 	/* serialize the stat name */
23637aec1d6eScindi 	str = ipath2str(lhs->ename, lhs->ipath);
23647aec1d6eScindi 	len = strlen(str);
23657aec1d6eScindi 
23667aec1d6eScindi 	ASSERT(Istatbufptr + len + 1 < &Istatbuf[Istatsz]);
23677aec1d6eScindi 	(void) strlcpy(Istatbufptr, str, &Istatbuf[Istatsz] - Istatbufptr);
23687aec1d6eScindi 	Istatbufptr += len;
23697aec1d6eScindi 	FREE(str);
23707aec1d6eScindi 	*Istatbufptr++ = '\0';
23717aec1d6eScindi 
23727aec1d6eScindi 	/* serialize the stat value */
23737aec1d6eScindi 	Istatbufptr += snprintf(Istatbufptr, &Istatbuf[Istatsz] - Istatbufptr,
23747aec1d6eScindi 	    "%d", val);
23757aec1d6eScindi 	*Istatbufptr++ = '\0';
23767aec1d6eScindi 
23777aec1d6eScindi 	ASSERT(Istatbufptr <= &Istatbuf[Istatsz]);
23787aec1d6eScindi }
23797aec1d6eScindi 
23807aec1d6eScindi void
istat_save()23817aec1d6eScindi istat_save()
23827aec1d6eScindi {
23837aec1d6eScindi 	if (Istat_need_save == 0)
23847aec1d6eScindi 		return;
23857aec1d6eScindi 
23867aec1d6eScindi 	/* figure out how big the serialzed info is */
23877aec1d6eScindi 	Istatsz = 0;
23887aec1d6eScindi 	lut_walk(Istats, (lut_cb)istataddsize, NULL);
23897aec1d6eScindi 
23907aec1d6eScindi 	if (Istatsz == 0) {
23917aec1d6eScindi 		/* no stats to save */
23927aec1d6eScindi 		fmd_buf_destroy(Hdl, NULL, WOBUF_ISTATS);
23937aec1d6eScindi 		return;
23947aec1d6eScindi 	}
23957aec1d6eScindi 
23967aec1d6eScindi 	/* create the serialized buffer */
23977aec1d6eScindi 	Istatbufptr = Istatbuf = MALLOC(Istatsz);
23987aec1d6eScindi 	lut_walk(Istats, (lut_cb)istat2str, NULL);
23997aec1d6eScindi 
24007aec1d6eScindi 	/* clear out current saved stats */
24017aec1d6eScindi 	fmd_buf_destroy(Hdl, NULL, WOBUF_ISTATS);
24027aec1d6eScindi 
24037aec1d6eScindi 	/* write out the new version */
24047aec1d6eScindi 	fmd_buf_write(Hdl, NULL, WOBUF_ISTATS, Istatbuf, Istatsz);
24057aec1d6eScindi 	FREE(Istatbuf);
24067aec1d6eScindi 
24077aec1d6eScindi 	Istat_need_save = 0;
24087aec1d6eScindi }
24097aec1d6eScindi 
24107aec1d6eScindi int
istat_cmp(struct istat_entry * ent1,struct istat_entry * ent2)24117aec1d6eScindi istat_cmp(struct istat_entry *ent1, struct istat_entry *ent2)
24127aec1d6eScindi {
24137aec1d6eScindi 	if (ent1->ename != ent2->ename)
24147aec1d6eScindi 		return (ent2->ename - ent1->ename);
24157aec1d6eScindi 	if (ent1->ipath != ent2->ipath)
24167aec1d6eScindi 		return ((char *)ent2->ipath - (char *)ent1->ipath);
24177aec1d6eScindi 
24187aec1d6eScindi 	return (0);
24197aec1d6eScindi }
24207aec1d6eScindi 
24217aec1d6eScindi /*
24227aec1d6eScindi  * istat-verify -- verify the component associated with a stat still exists
24237aec1d6eScindi  *
24247aec1d6eScindi  * if the component no longer exists, this routine resets the stat and
24257aec1d6eScindi  * returns 0.  if the component still exists, it returns 1.
24267aec1d6eScindi  */
24277aec1d6eScindi static int
istat_verify(struct node * snp,struct istat_entry * entp)24287aec1d6eScindi istat_verify(struct node *snp, struct istat_entry *entp)
24297aec1d6eScindi {
24307aec1d6eScindi 	struct stats *statp;
24317aec1d6eScindi 	nvlist_t *fmri;
24327aec1d6eScindi 
24337aec1d6eScindi 	fmri = node2fmri(snp->u.event.epname);
24347aec1d6eScindi 	if (platform_path_exists(fmri)) {
24357aec1d6eScindi 		nvlist_free(fmri);
24367aec1d6eScindi 		return (1);
24377aec1d6eScindi 	}
24387aec1d6eScindi 	nvlist_free(fmri);
24397aec1d6eScindi 
24407aec1d6eScindi 	/* component no longer in system.  zero out the associated stats */
24417aec1d6eScindi 	if ((statp = (struct stats *)
24427aec1d6eScindi 	    lut_lookup(Istats, entp, (lut_cmp)istat_cmp)) == NULL ||
24437aec1d6eScindi 	    stats_counter_value(statp) == 0)
24447aec1d6eScindi 		return (0);	/* stat is already reset */
24457aec1d6eScindi 
24467aec1d6eScindi 	Istat_need_save = 1;
24477aec1d6eScindi 	stats_counter_reset(statp);
24487aec1d6eScindi 	return (0);
24497aec1d6eScindi }
24507aec1d6eScindi 
24517aec1d6eScindi static void
istat_bump(struct node * snp,int n)24527aec1d6eScindi istat_bump(struct node *snp, int n)
24537aec1d6eScindi {
24547aec1d6eScindi 	struct stats *statp;
24557aec1d6eScindi 	struct istat_entry ent;
24567aec1d6eScindi 
24577aec1d6eScindi 	ASSERT(snp != NULL);
24587aec1d6eScindi 	ASSERTinfo(snp->t == T_EVENT, ptree_nodetype2str(snp->t));
24597aec1d6eScindi 	ASSERT(snp->u.event.epname != NULL);
24607aec1d6eScindi 
24617aec1d6eScindi 	/* class name should be hoisted into a single stable entry */
24627aec1d6eScindi 	ASSERT(snp->u.event.ename->u.name.next == NULL);
24637aec1d6eScindi 	ent.ename = snp->u.event.ename->u.name.s;
24647aec1d6eScindi 	ent.ipath = ipath(snp->u.event.epname);
24657aec1d6eScindi 
24667aec1d6eScindi 	if (!istat_verify(snp, &ent)) {
24677aec1d6eScindi 		/* component no longer exists in system, nothing to do */
24687aec1d6eScindi 		return;
24697aec1d6eScindi 	}
24707aec1d6eScindi 
24717aec1d6eScindi 	if ((statp = (struct stats *)
24727aec1d6eScindi 	    lut_lookup(Istats, &ent, (lut_cmp)istat_cmp)) == NULL) {
24737aec1d6eScindi 		/* need to create the counter */
24747aec1d6eScindi 		int cnt = 0;
24757aec1d6eScindi 		struct node *np;
24767aec1d6eScindi 		char *sname;
24777aec1d6eScindi 		char *snamep;
24787aec1d6eScindi 		struct istat_entry *newentp;
24797aec1d6eScindi 
24807aec1d6eScindi 		/* count up the size of the stat name */
24817aec1d6eScindi 		np = snp->u.event.ename;
24827aec1d6eScindi 		while (np != NULL) {
24837aec1d6eScindi 			cnt += strlen(np->u.name.s);
24847aec1d6eScindi 			cnt++;	/* for the '.' or '@' */
24857aec1d6eScindi 			np = np->u.name.next;
24867aec1d6eScindi 		}
24877aec1d6eScindi 		np = snp->u.event.epname;
24887aec1d6eScindi 		while (np != NULL) {
24897aec1d6eScindi 			cnt += snprintf(NULL, 0, "%s%llu",
24907aec1d6eScindi 			    np->u.name.s, np->u.name.child->u.ull);
24917aec1d6eScindi 			cnt++;	/* for the '/' or trailing NULL byte */
24927aec1d6eScindi 			np = np->u.name.next;
24937aec1d6eScindi 		}
24947aec1d6eScindi 
24957aec1d6eScindi 		/* build the stat name */
24967aec1d6eScindi 		snamep = sname = alloca(cnt);
24977aec1d6eScindi 		np = snp->u.event.ename;
24987aec1d6eScindi 		while (np != NULL) {
24997aec1d6eScindi 			snamep += snprintf(snamep, &sname[cnt] - snamep,
25007aec1d6eScindi 			    "%s", np->u.name.s);
25017aec1d6eScindi 			np = np->u.name.next;
25027aec1d6eScindi 			if (np)
25037aec1d6eScindi 				*snamep++ = '.';
25047aec1d6eScindi 		}
25057aec1d6eScindi 		*snamep++ = '@';
25067aec1d6eScindi 		np = snp->u.event.epname;
25077aec1d6eScindi 		while (np != NULL) {
25087aec1d6eScindi 			snamep += snprintf(snamep, &sname[cnt] - snamep,
25097aec1d6eScindi 			    "%s%llu", np->u.name.s, np->u.name.child->u.ull);
25107aec1d6eScindi 			np = np->u.name.next;
25117aec1d6eScindi 			if (np)
25127aec1d6eScindi 				*snamep++ = '/';
25137aec1d6eScindi 		}
25147aec1d6eScindi 		*snamep++ = '\0';
25157aec1d6eScindi 
25167aec1d6eScindi 		/* create the new stat & add it to our list */
25177aec1d6eScindi 		newentp = MALLOC(sizeof (*newentp));
25187aec1d6eScindi 		*newentp = ent;
25197aec1d6eScindi 		statp = stats_new_counter(NULL, sname, 0);
25207aec1d6eScindi 		Istats = lut_add(Istats, (void *)newentp, (void *)statp,
25217aec1d6eScindi 		    (lut_cmp)istat_cmp);
25227aec1d6eScindi 	}
25237aec1d6eScindi 
25247aec1d6eScindi 	/* if n is non-zero, set that value instead of bumping */
25257aec1d6eScindi 	if (n) {
25267aec1d6eScindi 		stats_counter_reset(statp);
25277aec1d6eScindi 		stats_counter_add(statp, n);
25287aec1d6eScindi 	} else
25297aec1d6eScindi 		stats_counter_bump(statp);
25307aec1d6eScindi 	Istat_need_save = 1;
25318a40a695Sgavinm 
25328a40a695Sgavinm 	ipath_print(O_ALTFP|O_VERB2, ent.ename, ent.ipath);
25338a40a695Sgavinm 	out(O_ALTFP|O_VERB2, " %s to value %d", n ? "set" : "incremented",
25348a40a695Sgavinm 	    stats_counter_value(statp));
25357aec1d6eScindi }
25367aec1d6eScindi 
25377aec1d6eScindi /*ARGSUSED*/
25387aec1d6eScindi static void
istat_destructor(void * left,void * right,void * arg)25397aec1d6eScindi istat_destructor(void *left, void *right, void *arg)
25407aec1d6eScindi {
25417aec1d6eScindi 	struct istat_entry *entp = (struct istat_entry *)left;
25427aec1d6eScindi 	struct stats *statp = (struct stats *)right;
25437aec1d6eScindi 	FREE(entp);
25447aec1d6eScindi 	stats_delete(statp);
25457aec1d6eScindi }
25467aec1d6eScindi 
254708f6c065Sgavinm /*
254808f6c065Sgavinm  * Callback used in a walk of the Istats to reset matching stat counters.
254908f6c065Sgavinm  */
255008f6c065Sgavinm static void
istat_counter_reset_cb(struct istat_entry * entp,struct stats * statp,const struct ipath * ipp)255108f6c065Sgavinm istat_counter_reset_cb(struct istat_entry *entp, struct stats *statp,
255208f6c065Sgavinm     const struct ipath *ipp)
255308f6c065Sgavinm {
255408f6c065Sgavinm 	char *path;
255508f6c065Sgavinm 
255608f6c065Sgavinm 	if (entp->ipath == ipp) {
255708f6c065Sgavinm 		path = ipath2str(entp->ename, ipp);
255808f6c065Sgavinm 		out(O_ALTFP, "istat_counter_reset_cb: resetting %s", path);
255908f6c065Sgavinm 		FREE(path);
256008f6c065Sgavinm 		stats_counter_reset(statp);
256108f6c065Sgavinm 		Istat_need_save = 1;
256208f6c065Sgavinm 	}
256308f6c065Sgavinm }
256408f6c065Sgavinm 
2565e5ba14ffSstephh /*ARGSUSED*/
2566e5ba14ffSstephh static void
istat_counter_topo_chg_cb(struct istat_entry * entp,struct stats * statp,void * unused)2567e5ba14ffSstephh istat_counter_topo_chg_cb(struct istat_entry *entp, struct stats *statp,
2568e5ba14ffSstephh     void *unused)
2569e5ba14ffSstephh {
2570e5ba14ffSstephh 	char *path;
2571e5ba14ffSstephh 	nvlist_t *fmri;
2572e5ba14ffSstephh 
2573e5ba14ffSstephh 	fmri = ipath2fmri((struct ipath *)(entp->ipath));
2574e5ba14ffSstephh 	if (!platform_path_exists(fmri)) {
2575e5ba14ffSstephh 		path = ipath2str(entp->ename, entp->ipath);
2576e5ba14ffSstephh 		out(O_ALTFP, "istat_counter_topo_chg_cb: not present %s", path);
2577e5ba14ffSstephh 		FREE(path);
2578e5ba14ffSstephh 		stats_counter_reset(statp);
2579e5ba14ffSstephh 		Istat_need_save = 1;
2580e5ba14ffSstephh 	}
2581e5ba14ffSstephh 	nvlist_free(fmri);
2582e5ba14ffSstephh }
2583e5ba14ffSstephh 
25847aec1d6eScindi void
istat_fini(void)25857aec1d6eScindi istat_fini(void)
25867aec1d6eScindi {
25877aec1d6eScindi 	lut_free(Istats, istat_destructor, NULL);
25887aec1d6eScindi }
25897aec1d6eScindi 
2590b5016cbbSstephh static char *Serdbuf;
2591b5016cbbSstephh static char *Serdbufptr;
2592b5016cbbSstephh static int Serdsz;
2593b5016cbbSstephh 
2594b5016cbbSstephh /*
2595b5016cbbSstephh  * serdaddsize -- calculate size of serd and add it to Serdsz
2596b5016cbbSstephh  */
2597b5016cbbSstephh /*ARGSUSED*/
2598b5016cbbSstephh static void
serdaddsize(const struct serd_entry * lhs,struct stats * rhs,void * arg)2599b5016cbbSstephh serdaddsize(const struct serd_entry *lhs, struct stats *rhs, void *arg)
2600b5016cbbSstephh {
2601b5016cbbSstephh 	ASSERT(lhs != NULL);
2602b5016cbbSstephh 
2603b5016cbbSstephh 	/* count up the size of the stat name */
2604b5016cbbSstephh 	Serdsz += ipath2strlen(lhs->ename, lhs->ipath);
2605b5016cbbSstephh 	Serdsz++;	/* for the trailing NULL byte */
2606b5016cbbSstephh }
2607b5016cbbSstephh 
2608b5016cbbSstephh /*
2609b5016cbbSstephh  * serd2str -- serialize a serd engine, writing result to *Serdbufptr
2610b5016cbbSstephh  */
2611b5016cbbSstephh /*ARGSUSED*/
2612b5016cbbSstephh static void
serd2str(const struct serd_entry * lhs,struct stats * rhs,void * arg)2613b5016cbbSstephh serd2str(const struct serd_entry *lhs, struct stats *rhs, void *arg)
2614b5016cbbSstephh {
2615b5016cbbSstephh 	char *str;
2616b5016cbbSstephh 	int len;
2617b5016cbbSstephh 
2618b5016cbbSstephh 	ASSERT(lhs != NULL);
2619b5016cbbSstephh 
2620b5016cbbSstephh 	/* serialize the serd engine name */
2621b5016cbbSstephh 	str = ipath2str(lhs->ename, lhs->ipath);
2622b5016cbbSstephh 	len = strlen(str);
2623b5016cbbSstephh 
2624b5016cbbSstephh 	ASSERT(Serdbufptr + len + 1 <= &Serdbuf[Serdsz]);
2625b5016cbbSstephh 	(void) strlcpy(Serdbufptr, str, &Serdbuf[Serdsz] - Serdbufptr);
2626b5016cbbSstephh 	Serdbufptr += len;
2627b5016cbbSstephh 	FREE(str);
2628b5016cbbSstephh 	*Serdbufptr++ = '\0';
2629b5016cbbSstephh 	ASSERT(Serdbufptr <= &Serdbuf[Serdsz]);
2630b5016cbbSstephh }
2631b5016cbbSstephh 
2632b5016cbbSstephh void
serd_save()2633b5016cbbSstephh serd_save()
2634b5016cbbSstephh {
2635b5016cbbSstephh 	if (Serd_need_save == 0)
2636b5016cbbSstephh 		return;
2637b5016cbbSstephh 
2638b5016cbbSstephh 	/* figure out how big the serialzed info is */
2639b5016cbbSstephh 	Serdsz = 0;
2640b5016cbbSstephh 	lut_walk(SerdEngines, (lut_cb)serdaddsize, NULL);
2641b5016cbbSstephh 
2642b5016cbbSstephh 	if (Serdsz == 0) {
2643b5016cbbSstephh 		/* no serd engines to save */
2644b5016cbbSstephh 		fmd_buf_destroy(Hdl, NULL, WOBUF_SERDS);
2645b5016cbbSstephh 		return;
2646b5016cbbSstephh 	}
2647b5016cbbSstephh 
2648b5016cbbSstephh 	/* create the serialized buffer */
2649b5016cbbSstephh 	Serdbufptr = Serdbuf = MALLOC(Serdsz);
2650b5016cbbSstephh 	lut_walk(SerdEngines, (lut_cb)serd2str, NULL);
2651b5016cbbSstephh 
2652b5016cbbSstephh 	/* clear out current saved stats */
2653b5016cbbSstephh 	fmd_buf_destroy(Hdl, NULL, WOBUF_SERDS);
2654b5016cbbSstephh 
2655b5016cbbSstephh 	/* write out the new version */
2656b5016cbbSstephh 	fmd_buf_write(Hdl, NULL, WOBUF_SERDS, Serdbuf, Serdsz);
2657b5016cbbSstephh 	FREE(Serdbuf);
2658b5016cbbSstephh 	Serd_need_save = 0;
2659b5016cbbSstephh }
2660b5016cbbSstephh 
2661b5016cbbSstephh int
serd_cmp(struct serd_entry * ent1,struct serd_entry * ent2)2662b5016cbbSstephh serd_cmp(struct serd_entry *ent1, struct serd_entry *ent2)
2663b5016cbbSstephh {
2664b5016cbbSstephh 	if (ent1->ename != ent2->ename)
2665b5016cbbSstephh 		return (ent2->ename - ent1->ename);
2666b5016cbbSstephh 	if (ent1->ipath != ent2->ipath)
2667b5016cbbSstephh 		return ((char *)ent2->ipath - (char *)ent1->ipath);
2668b5016cbbSstephh 
2669b5016cbbSstephh 	return (0);
2670b5016cbbSstephh }
2671b5016cbbSstephh 
2672b5016cbbSstephh void
fme_serd_load(fmd_hdl_t * hdl)2673b5016cbbSstephh fme_serd_load(fmd_hdl_t *hdl)
2674b5016cbbSstephh {
2675b5016cbbSstephh 	int sz;
2676b5016cbbSstephh 	char *sbuf;
2677b5016cbbSstephh 	char *sepptr;
2678b5016cbbSstephh 	char *ptr;
2679b5016cbbSstephh 	struct serd_entry *newentp;
2680b5016cbbSstephh 	struct node *epname;
2681b5016cbbSstephh 	nvlist_t *fmri;
2682b5016cbbSstephh 	char *namestring;
2683b5016cbbSstephh 
2684b5016cbbSstephh 	if ((sz = fmd_buf_size(hdl, NULL, WOBUF_SERDS)) == 0)
2685b5016cbbSstephh 		return;
2686b5016cbbSstephh 	sbuf = alloca(sz);
2687b5016cbbSstephh 	fmd_buf_read(hdl, NULL, WOBUF_SERDS, sbuf, sz);
2688b5016cbbSstephh 	ptr = sbuf;
2689b5016cbbSstephh 	while (ptr < &sbuf[sz]) {
2690b5016cbbSstephh 		sepptr = strchr(ptr, '@');
2691b5016cbbSstephh 		*sepptr = '\0';
2692b5016cbbSstephh 		namestring = ptr;
2693b5016cbbSstephh 		sepptr++;
2694b5016cbbSstephh 		ptr = sepptr;
2695b5016cbbSstephh 		ptr += strlen(ptr);
2696b5016cbbSstephh 		ptr++;	/* move past the '\0' separating paths */
2697b5016cbbSstephh 		epname = pathstring2epnamenp(sepptr);
2698b5016cbbSstephh 		fmri = node2fmri(epname);
2699b5016cbbSstephh 		if (platform_path_exists(fmri)) {
2700b5016cbbSstephh 			newentp = MALLOC(sizeof (*newentp));
2701b5016cbbSstephh 			newentp->hdl = hdl;
2702b5016cbbSstephh 			newentp->ipath = ipath(epname);
2703b5016cbbSstephh 			newentp->ename = stable(namestring);
2704b5016cbbSstephh 			SerdEngines = lut_add(SerdEngines, (void *)newentp,
2705e5ba14ffSstephh 			    (void *)newentp, (lut_cmp)serd_cmp);
2706b5016cbbSstephh 		} else
2707b5016cbbSstephh 			Serd_need_save = 1;
2708e5ba14ffSstephh 		tree_free(epname);
2709b5016cbbSstephh 		nvlist_free(fmri);
2710b5016cbbSstephh 	}
2711b5016cbbSstephh 	/* save it back again in case some of the paths no longer exist */
2712b5016cbbSstephh 	serd_save();
2713b5016cbbSstephh }
2714b5016cbbSstephh 
2715b5016cbbSstephh /*ARGSUSED*/
2716b5016cbbSstephh static void
serd_destructor(void * left,void * right,void * arg)2717b5016cbbSstephh serd_destructor(void *left, void *right, void *arg)
2718b5016cbbSstephh {
2719b5016cbbSstephh 	struct serd_entry *entp = (struct serd_entry *)left;
2720b5016cbbSstephh 	FREE(entp);
2721b5016cbbSstephh }
2722b5016cbbSstephh 
2723b5016cbbSstephh /*
2724b5016cbbSstephh  * Callback used in a walk of the SerdEngines to reset matching serd engines.
2725b5016cbbSstephh  */
2726b5016cbbSstephh /*ARGSUSED*/
2727b5016cbbSstephh static void
serd_reset_cb(struct serd_entry * entp,void * unused,const struct ipath * ipp)2728b5016cbbSstephh serd_reset_cb(struct serd_entry *entp, void *unused, const struct ipath *ipp)
2729b5016cbbSstephh {
2730b5016cbbSstephh 	char *path;
2731b5016cbbSstephh 
2732b5016cbbSstephh 	if (entp->ipath == ipp) {
2733b5016cbbSstephh 		path = ipath2str(entp->ename, ipp);
2734b5016cbbSstephh 		out(O_ALTFP, "serd_reset_cb: resetting %s", path);
2735b5016cbbSstephh 		fmd_serd_reset(entp->hdl, path);
2736b5016cbbSstephh 		FREE(path);
2737b5016cbbSstephh 		Serd_need_save = 1;
2738b5016cbbSstephh 	}
2739b5016cbbSstephh }
2740b5016cbbSstephh 
2741e5ba14ffSstephh /*ARGSUSED*/
2742e5ba14ffSstephh static void
serd_topo_chg_cb(struct serd_entry * entp,void * unused,void * unused2)2743e5ba14ffSstephh serd_topo_chg_cb(struct serd_entry *entp, void *unused, void *unused2)
2744e5ba14ffSstephh {
2745e5ba14ffSstephh 	char *path;
2746e5ba14ffSstephh 	nvlist_t *fmri;
2747e5ba14ffSstephh 
2748e5ba14ffSstephh 	fmri = ipath2fmri((struct ipath *)(entp->ipath));
2749e5ba14ffSstephh 	if (!platform_path_exists(fmri)) {
2750e5ba14ffSstephh 		path = ipath2str(entp->ename, entp->ipath);
2751e5ba14ffSstephh 		out(O_ALTFP, "serd_topo_chg_cb: not present %s", path);
2752e5ba14ffSstephh 		fmd_serd_reset(entp->hdl, path);
2753e5ba14ffSstephh 		FREE(path);
2754e5ba14ffSstephh 		Serd_need_save = 1;
2755e5ba14ffSstephh 	}
2756e5ba14ffSstephh 	nvlist_free(fmri);
2757e5ba14ffSstephh }
2758e5ba14ffSstephh 
2759b5016cbbSstephh void
serd_fini(void)2760b5016cbbSstephh serd_fini(void)
2761b5016cbbSstephh {
2762b5016cbbSstephh 	lut_free(SerdEngines, serd_destructor, NULL);
2763b5016cbbSstephh }
2764b5016cbbSstephh 
27657c478bd9Sstevel@tonic-gate static void
publish_suspects(struct fme * fmep,struct rsl * srl)2766b7d3956bSstephh publish_suspects(struct fme *fmep, struct rsl *srl)
27677c478bd9Sstevel@tonic-gate {
27687c478bd9Sstevel@tonic-gate 	struct rsl *rp;
27697c478bd9Sstevel@tonic-gate 	nvlist_t *fault;
27707c478bd9Sstevel@tonic-gate 	uint8_t cert;
27717c478bd9Sstevel@tonic-gate 	uint_t *frs;
27728e7248e5SStephen Hanson 	uint_t frsum, fr;
27737aec1d6eScindi 	uint_t messval;
2774b7d3956bSstephh 	uint_t retireval;
2775b7d3956bSstephh 	uint_t responseval;
27767aec1d6eScindi 	struct node *snp;
27777c478bd9Sstevel@tonic-gate 	int frcnt, fridx;
27787aec1d6eScindi 	boolean_t allfaulty = B_TRUE;
2779b7d3956bSstephh 	struct rsl *erl = srl + fmep->nsuspects - 1;
27807c478bd9Sstevel@tonic-gate 
27817c478bd9Sstevel@tonic-gate 	/*
2782b7d3956bSstephh 	 * sort the array
27837c478bd9Sstevel@tonic-gate 	 */
2784b7d3956bSstephh 	qsort(srl, fmep->nsuspects, sizeof (struct rsl), rslcmp);
27857c478bd9Sstevel@tonic-gate 
27868e7248e5SStephen Hanson 	/* sum the fitrates */
27878e7248e5SStephen Hanson 	frs = alloca(fmep->nsuspects * sizeof (uint_t));
27888e7248e5SStephen Hanson 	fridx = frcnt = frsum = 0;
27897c478bd9Sstevel@tonic-gate 
27908e7248e5SStephen Hanson 	for (rp = srl; rp <= erl; rp++) {
27918e7248e5SStephen Hanson 		struct node *n;
27927c478bd9Sstevel@tonic-gate 
27938e7248e5SStephen Hanson 		n = eventprop_lookup(rp->suspect, L_FITrate);
27948e7248e5SStephen Hanson 		if (node2uint(n, &fr) != 0) {
27958e7248e5SStephen Hanson 			out(O_DEBUG|O_NONL, "event ");
27968e7248e5SStephen Hanson 			ipath_print(O_DEBUG|O_NONL,
27978e7248e5SStephen Hanson 			    rp->suspect->enode->u.event.ename->u.name.s,
27988e7248e5SStephen Hanson 			    rp->suspect->ipp);
2799f6e214c7SGavin Maltby 			out(O_VERB, " has no FITrate (using 1)");
28008e7248e5SStephen Hanson 			fr = 1;
28018e7248e5SStephen Hanson 		} else if (fr == 0) {
28028e7248e5SStephen Hanson 			out(O_DEBUG|O_NONL, "event ");
28038e7248e5SStephen Hanson 			ipath_print(O_DEBUG|O_NONL,
28048e7248e5SStephen Hanson 			    rp->suspect->enode->u.event.ename->u.name.s,
28058e7248e5SStephen Hanson 			    rp->suspect->ipp);
2806f6e214c7SGavin Maltby 			out(O_VERB, " has zero FITrate (using 1)");
28078e7248e5SStephen Hanson 			fr = 1;
28087c478bd9Sstevel@tonic-gate 		}
28098e7248e5SStephen Hanson 
28108e7248e5SStephen Hanson 		frs[fridx++] = fr;
28118e7248e5SStephen Hanson 		frsum += fr;
28128e7248e5SStephen Hanson 		frcnt++;
28137c478bd9Sstevel@tonic-gate 	}
28147c478bd9Sstevel@tonic-gate 
28157c478bd9Sstevel@tonic-gate 	/* Add them in reverse order of our sort, as fmd reverses order */
28167c478bd9Sstevel@tonic-gate 	for (rp = erl; rp >= srl; rp--) {
28178e7248e5SStephen Hanson 		cert = percentof(frs[--fridx], frsum);
28187c478bd9Sstevel@tonic-gate 		fault = fmd_nvl_create_fault(fmep->hdl,
28197c478bd9Sstevel@tonic-gate 		    rp->suspect->enode->u.event.ename->u.name.s,
28207c478bd9Sstevel@tonic-gate 		    cert,
28217c478bd9Sstevel@tonic-gate 		    rp->asru,
28227c478bd9Sstevel@tonic-gate 		    rp->fru,
28237c478bd9Sstevel@tonic-gate 		    rp->rsrc);
28247c478bd9Sstevel@tonic-gate 		if (fault == NULL)
28257c478bd9Sstevel@tonic-gate 			out(O_DIE, "fault creation failed");
28267aec1d6eScindi 		/* if "message" property exists, add it to the fault */
28277aec1d6eScindi 		if (node2uint(eventprop_lookup(rp->suspect, L_message),
28287aec1d6eScindi 		    &messval) == 0) {
28297aec1d6eScindi 
28307aec1d6eScindi 			out(O_ALTFP,
28317aec1d6eScindi 			    "[FME%d, %s adds message=%d to suspect list]",
28327aec1d6eScindi 			    fmep->id,
28337aec1d6eScindi 			    rp->suspect->enode->u.event.ename->u.name.s,
28347aec1d6eScindi 			    messval);
28357aec1d6eScindi 			if (nvlist_add_boolean_value(fault,
28367aec1d6eScindi 			    FM_SUSPECT_MESSAGE,
28377aec1d6eScindi 			    (messval) ? B_TRUE : B_FALSE) != 0) {
28387aec1d6eScindi 				out(O_DIE, "cannot add no-message to fault");
28397aec1d6eScindi 			}
28407aec1d6eScindi 		}
2841b7d3956bSstephh 
2842b7d3956bSstephh 		/* if "retire" property exists, add it to the fault */
2843b7d3956bSstephh 		if (node2uint(eventprop_lookup(rp->suspect, L_retire),
2844b7d3956bSstephh 		    &retireval) == 0) {
2845b7d3956bSstephh 
2846b7d3956bSstephh 			out(O_ALTFP,
2847b7d3956bSstephh 			    "[FME%d, %s adds retire=%d to suspect list]",
2848b7d3956bSstephh 			    fmep->id,
2849b7d3956bSstephh 			    rp->suspect->enode->u.event.ename->u.name.s,
2850b7d3956bSstephh 			    retireval);
2851b7d3956bSstephh 			if (nvlist_add_boolean_value(fault,
2852b7d3956bSstephh 			    FM_SUSPECT_RETIRE,
2853b7d3956bSstephh 			    (retireval) ? B_TRUE : B_FALSE) != 0) {
2854b7d3956bSstephh 				out(O_DIE, "cannot add no-retire to fault");
2855b7d3956bSstephh 			}
2856b7d3956bSstephh 		}
2857b7d3956bSstephh 
2858b7d3956bSstephh 		/* if "response" property exists, add it to the fault */
2859b7d3956bSstephh 		if (node2uint(eventprop_lookup(rp->suspect, L_response),
2860b7d3956bSstephh 		    &responseval) == 0) {
2861b7d3956bSstephh 
2862b7d3956bSstephh 			out(O_ALTFP,
2863b7d3956bSstephh 			    "[FME%d, %s adds response=%d to suspect list]",
2864b7d3956bSstephh 			    fmep->id,
2865b7d3956bSstephh 			    rp->suspect->enode->u.event.ename->u.name.s,
2866b7d3956bSstephh 			    responseval);
2867b7d3956bSstephh 			if (nvlist_add_boolean_value(fault,
2868b7d3956bSstephh 			    FM_SUSPECT_RESPONSE,
2869b7d3956bSstephh 			    (responseval) ? B_TRUE : B_FALSE) != 0) {
2870b7d3956bSstephh 				out(O_DIE, "cannot add no-response to fault");
2871b7d3956bSstephh 			}
2872b7d3956bSstephh 		}
2873b7d3956bSstephh 
28747aec1d6eScindi 		/* add any payload properties */
28757aec1d6eScindi 		lut_walk(rp->suspect->payloadprops,
28767aec1d6eScindi 		    (lut_cb)addpayloadprop, (void *)fault);
28777c478bd9Sstevel@tonic-gate 		rslfree(rp);
28785f25dc2aSgavinm 
28795f25dc2aSgavinm 		/*
28805f25dc2aSgavinm 		 * If "action" property exists, evaluate it;  this must be done
2881b7d3956bSstephh 		 * before the allfaulty check below since some actions may
288225c6ff4bSstephh 		 * modify the asru to be used in fmd_nvl_fmri_has_fault.  This
28835f25dc2aSgavinm 		 * needs to be restructured if any new actions are introduced
28845f25dc2aSgavinm 		 * that have effects that we do not want to be visible if
28855f25dc2aSgavinm 		 * we decide not to publish in the dupclose check below.
28865f25dc2aSgavinm 		 */
28877aec1d6eScindi 		if ((snp = eventprop_lookup(rp->suspect, L_action)) != NULL) {
28887aec1d6eScindi 			struct evalue evalue;
28897aec1d6eScindi 
28907aec1d6eScindi 			out(O_ALTFP|O_NONL,
28917aec1d6eScindi 			    "[FME%d, %s action ", fmep->id,
28927aec1d6eScindi 			    rp->suspect->enode->u.event.ename->u.name.s);
28937aec1d6eScindi 			ptree_name_iter(O_ALTFP|O_NONL, snp);
28947aec1d6eScindi 			out(O_ALTFP, "]");
28957aec1d6eScindi 			Action_nvl = fault;
28967aec1d6eScindi 			(void) eval_expr(snp, NULL, NULL, NULL, NULL,
28977aec1d6eScindi 			    NULL, 0, &evalue);
28987aec1d6eScindi 		}
28995f25dc2aSgavinm 
2900b7d3956bSstephh 		fmd_case_add_suspect(fmep->hdl, fmep->fmcase, fault);
2901b7d3956bSstephh 
29027aec1d6eScindi 		/*
2903567cc2e6Sstephh 		 * check if the asru is already marked as "faulty".
29047aec1d6eScindi 		 */
2905567cc2e6Sstephh 		if (allfaulty) {
29067aec1d6eScindi 			nvlist_t *asru;
29077aec1d6eScindi 
2908b7d3956bSstephh 			out(O_ALTFP|O_VERB, "FME%d dup check ", fmep->id);
29097aec1d6eScindi 			itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, rp->suspect);
29107aec1d6eScindi 			out(O_ALTFP|O_VERB|O_NONL, " ");
29117aec1d6eScindi 			if (nvlist_lookup_nvlist(fault,
29127aec1d6eScindi 			    FM_FAULT_ASRU, &asru) != 0) {
29137aec1d6eScindi 				out(O_ALTFP|O_VERB, "NULL asru");
29147aec1d6eScindi 				allfaulty = B_FALSE;
291525c6ff4bSstephh 			} else if (fmd_nvl_fmri_has_fault(fmep->hdl, asru,
291625c6ff4bSstephh 			    FMD_HAS_FAULT_ASRU, NULL)) {
29177aec1d6eScindi 				out(O_ALTFP|O_VERB, "faulty");
29187aec1d6eScindi 			} else {
29197aec1d6eScindi 				out(O_ALTFP|O_VERB, "not faulty");
29207aec1d6eScindi 				allfaulty = B_FALSE;
29217aec1d6eScindi 			}
29227aec1d6eScindi 		}
29237aec1d6eScindi 
29247aec1d6eScindi 	}
29255f25dc2aSgavinm 
2926567cc2e6Sstephh 	if (!allfaulty) {
2927567cc2e6Sstephh 		/*
2928567cc2e6Sstephh 		 * don't update the count stat if all asrus are already
2929567cc2e6Sstephh 		 * present and unrepaired in the asru cache
2930567cc2e6Sstephh 		 */
29315f25dc2aSgavinm 		for (rp = erl; rp >= srl; rp--) {
29325f25dc2aSgavinm 			struct event *suspect = rp->suspect;
29335f25dc2aSgavinm 
29345f25dc2aSgavinm 			if (suspect == NULL)
29355f25dc2aSgavinm 				continue;
29365f25dc2aSgavinm 
29375f25dc2aSgavinm 			/* if "count" exists, increment the appropriate stat */
29385f25dc2aSgavinm 			if ((snp = eventprop_lookup(suspect,
29395f25dc2aSgavinm 			    L_count)) != NULL) {
29405f25dc2aSgavinm 				out(O_ALTFP|O_NONL,
29415f25dc2aSgavinm 				    "[FME%d, %s count ", fmep->id,
29425f25dc2aSgavinm 				    suspect->enode->u.event.ename->u.name.s);
29435f25dc2aSgavinm 				ptree_name_iter(O_ALTFP|O_NONL, snp);
29445f25dc2aSgavinm 				out(O_ALTFP, "]");
29455f25dc2aSgavinm 				istat_bump(snp, 0);
29465f25dc2aSgavinm 
29475f25dc2aSgavinm 			}
29485f25dc2aSgavinm 		}
29495f25dc2aSgavinm 		istat_save();	/* write out any istat changes */
29507c478bd9Sstevel@tonic-gate 	}
29517c478bd9Sstevel@tonic-gate }
29527c478bd9Sstevel@tonic-gate 
295332d4e834STarik Soydan static const char *
undiag_2defect_str(int ud)295432d4e834STarik Soydan undiag_2defect_str(int ud)
295532d4e834STarik Soydan {
295632d4e834STarik Soydan 	switch (ud) {
295732d4e834STarik Soydan 	case UD_VAL_MISSINGINFO:
295832d4e834STarik Soydan 	case UD_VAL_MISSINGOBS:
295932d4e834STarik Soydan 	case UD_VAL_MISSINGPATH:
296032d4e834STarik Soydan 	case UD_VAL_MISSINGZERO:
296132d4e834STarik Soydan 	case UD_VAL_BADOBS:
296232d4e834STarik Soydan 	case UD_VAL_CFGMISMATCH:
296332d4e834STarik Soydan 		return (UNDIAG_DEFECT_CHKPT);
296432d4e834STarik Soydan 
296532d4e834STarik Soydan 	case UD_VAL_BADEVENTI:
2966705e9f42SStephen Hanson 	case UD_VAL_BADEVENTPATH:
2967705e9f42SStephen Hanson 	case UD_VAL_BADEVENTCLASS:
296832d4e834STarik Soydan 	case UD_VAL_INSTFAIL:
296932d4e834STarik Soydan 	case UD_VAL_NOPATH:
297032d4e834STarik Soydan 	case UD_VAL_UNSOLVD:
297132d4e834STarik Soydan 		return (UNDIAG_DEFECT_FME);
297232d4e834STarik Soydan 
297332d4e834STarik Soydan 	case UD_VAL_MAXFME:
297432d4e834STarik Soydan 		return (UNDIAG_DEFECT_LIMIT);
297532d4e834STarik Soydan 
297632d4e834STarik Soydan 	case UD_VAL_UNKNOWN:
297732d4e834STarik Soydan 	default:
297832d4e834STarik Soydan 		return (UNDIAG_DEFECT_UNKNOWN);
297932d4e834STarik Soydan 	}
298032d4e834STarik Soydan }
298132d4e834STarik Soydan 
2982705e9f42SStephen Hanson static const char *
undiag_2fault_str(int ud)2983705e9f42SStephen Hanson undiag_2fault_str(int ud)
298432d4e834STarik Soydan {
298532d4e834STarik Soydan 	switch (ud) {
298632d4e834STarik Soydan 	case UD_VAL_BADEVENTI:
2987705e9f42SStephen Hanson 	case UD_VAL_BADEVENTPATH:
2988705e9f42SStephen Hanson 	case UD_VAL_BADEVENTCLASS:
2989705e9f42SStephen Hanson 	case UD_VAL_INSTFAIL:
2990705e9f42SStephen Hanson 	case UD_VAL_NOPATH:
2991705e9f42SStephen Hanson 	case UD_VAL_UNSOLVD:
2992705e9f42SStephen Hanson 		return (UNDIAG_FAULT_FME);
2993705e9f42SStephen Hanson 	default:
2994705e9f42SStephen Hanson 		return (NULL);
2995705e9f42SStephen Hanson 	}
2996705e9f42SStephen Hanson }
2997705e9f42SStephen Hanson 
2998705e9f42SStephen Hanson static char *
undiag_2reason_str(int ud,char * arg)2999705e9f42SStephen Hanson undiag_2reason_str(int ud, char *arg)
3000705e9f42SStephen Hanson {
3001705e9f42SStephen Hanson 	const char *ptr;
3002705e9f42SStephen Hanson 	char *buf;
3003705e9f42SStephen Hanson 	int with_arg = 0;
3004705e9f42SStephen Hanson 
3005705e9f42SStephen Hanson 	switch (ud) {
3006705e9f42SStephen Hanson 	case UD_VAL_BADEVENTPATH:
3007705e9f42SStephen Hanson 		ptr = UD_STR_BADEVENTPATH;
3008705e9f42SStephen Hanson 		with_arg = 1;
3009705e9f42SStephen Hanson 		break;
3010705e9f42SStephen Hanson 	case UD_VAL_BADEVENTCLASS:
3011705e9f42SStephen Hanson 		ptr = UD_STR_BADEVENTCLASS;
3012705e9f42SStephen Hanson 		with_arg = 1;
3013705e9f42SStephen Hanson 		break;
3014705e9f42SStephen Hanson 	case UD_VAL_BADEVENTI:
3015705e9f42SStephen Hanson 		ptr = UD_STR_BADEVENTI;
3016705e9f42SStephen Hanson 		with_arg = 1;
3017705e9f42SStephen Hanson 		break;
301832d4e834STarik Soydan 	case UD_VAL_BADOBS:
3019705e9f42SStephen Hanson 		ptr = UD_STR_BADOBS;
3020705e9f42SStephen Hanson 		break;
302132d4e834STarik Soydan 	case UD_VAL_CFGMISMATCH:
3022705e9f42SStephen Hanson 		ptr = UD_STR_CFGMISMATCH;
3023705e9f42SStephen Hanson 		break;
302432d4e834STarik Soydan 	case UD_VAL_INSTFAIL:
3025705e9f42SStephen Hanson 		ptr = UD_STR_INSTFAIL;
3026705e9f42SStephen Hanson 		with_arg = 1;
3027705e9f42SStephen Hanson 		break;
302832d4e834STarik Soydan 	case UD_VAL_MAXFME:
3029705e9f42SStephen Hanson 		ptr = UD_STR_MAXFME;
3030705e9f42SStephen Hanson 		break;
303132d4e834STarik Soydan 	case UD_VAL_MISSINGINFO:
3032705e9f42SStephen Hanson 		ptr = UD_STR_MISSINGINFO;
3033705e9f42SStephen Hanson 		break;
303432d4e834STarik Soydan 	case UD_VAL_MISSINGOBS:
3035705e9f42SStephen Hanson 		ptr = UD_STR_MISSINGOBS;
3036705e9f42SStephen Hanson 		break;
303732d4e834STarik Soydan 	case UD_VAL_MISSINGPATH:
3038705e9f42SStephen Hanson 		ptr = UD_STR_MISSINGPATH;
3039705e9f42SStephen Hanson 		break;
304032d4e834STarik Soydan 	case UD_VAL_MISSINGZERO:
3041705e9f42SStephen Hanson 		ptr = UD_STR_MISSINGZERO;
3042705e9f42SStephen Hanson 		break;
304332d4e834STarik Soydan 	case UD_VAL_NOPATH:
3044705e9f42SStephen Hanson 		ptr = UD_STR_NOPATH;
3045705e9f42SStephen Hanson 		with_arg = 1;
3046705e9f42SStephen Hanson 		break;
304732d4e834STarik Soydan 	case UD_VAL_UNSOLVD:
3048705e9f42SStephen Hanson 		ptr = UD_STR_UNSOLVD;
3049705e9f42SStephen Hanson 		break;
305032d4e834STarik Soydan 	case UD_VAL_UNKNOWN:
305132d4e834STarik Soydan 	default:
3052705e9f42SStephen Hanson 		ptr = UD_STR_UNKNOWN;
3053705e9f42SStephen Hanson 		break;
305432d4e834STarik Soydan 	}
3055705e9f42SStephen Hanson 	if (with_arg) {
3056705e9f42SStephen Hanson 		buf = MALLOC(strlen(ptr) + strlen(arg) - 1);
3057705e9f42SStephen Hanson 		(void) sprintf(buf, ptr, arg);
3058705e9f42SStephen Hanson 	} else {
3059705e9f42SStephen Hanson 		buf = MALLOC(strlen(ptr) + 1);
3060705e9f42SStephen Hanson 		(void) sprintf(buf, ptr);
3061705e9f42SStephen Hanson 	}
3062705e9f42SStephen Hanson 	return (buf);
306332d4e834STarik Soydan }
306432d4e834STarik Soydan 
30657c478bd9Sstevel@tonic-gate static void
publish_undiagnosable(fmd_hdl_t * hdl,fmd_event_t * ffep,fmd_case_t * fmcase,nvlist_t * detector,char * arg)3066705e9f42SStephen Hanson publish_undiagnosable(fmd_hdl_t *hdl, fmd_event_t *ffep, fmd_case_t *fmcase,
3067705e9f42SStephen Hanson     nvlist_t *detector, char *arg)
30687c478bd9Sstevel@tonic-gate {
30697c478bd9Sstevel@tonic-gate 	struct case_list *newcase;
3070705e9f42SStephen Hanson 	nvlist_t *defect, *fault;
3071705e9f42SStephen Hanson 	const char *faultstr;
3072705e9f42SStephen Hanson 	char *reason = undiag_2reason_str(Undiag_reason, arg);
30737c478bd9Sstevel@tonic-gate 
30747c478bd9Sstevel@tonic-gate 	out(O_ALTFP,
30757c478bd9Sstevel@tonic-gate 	    "[undiagnosable ereport received, "
3076705e9f42SStephen Hanson 	    "creating and closing a new case (%s)]", reason);
30777c478bd9Sstevel@tonic-gate 
30787c478bd9Sstevel@tonic-gate 	newcase = MALLOC(sizeof (struct case_list));
30797c478bd9Sstevel@tonic-gate 	newcase->next = NULL;
3080b5016cbbSstephh 	newcase->fmcase = fmcase;
30817c478bd9Sstevel@tonic-gate 	if (Undiagablecaselist != NULL)
30827c478bd9Sstevel@tonic-gate 		newcase->next = Undiagablecaselist;
30837c478bd9Sstevel@tonic-gate 	Undiagablecaselist = newcase;
30847c478bd9Sstevel@tonic-gate 
30857c478bd9Sstevel@tonic-gate 	if (ffep != NULL)
30867c478bd9Sstevel@tonic-gate 		fmd_case_add_ereport(hdl, newcase->fmcase, ffep);
30877c478bd9Sstevel@tonic-gate 
3088705e9f42SStephen Hanson 	/* add defect */
308932d4e834STarik Soydan 	defect = fmd_nvl_create_fault(hdl,
3090705e9f42SStephen Hanson 	    undiag_2defect_str(Undiag_reason), 50, NULL, NULL, detector);
3091705e9f42SStephen Hanson 	(void) nvlist_add_string(defect, UNDIAG_REASON, reason);
3092705e9f42SStephen Hanson 	(void) nvlist_add_boolean_value(defect, FM_SUSPECT_RETIRE, B_FALSE);
3093705e9f42SStephen Hanson 	(void) nvlist_add_boolean_value(defect, FM_SUSPECT_RESPONSE, B_FALSE);
30947c478bd9Sstevel@tonic-gate 	fmd_case_add_suspect(hdl, newcase->fmcase, defect);
30957c478bd9Sstevel@tonic-gate 
3096705e9f42SStephen Hanson 	/* add fault if appropriate */
3097705e9f42SStephen Hanson 	faultstr = undiag_2fault_str(Undiag_reason);
3098705e9f42SStephen Hanson 	if (faultstr != NULL) {
3099705e9f42SStephen Hanson 		fault = fmd_nvl_create_fault(hdl, faultstr, 50, NULL, NULL,
3100705e9f42SStephen Hanson 		    detector);
3101705e9f42SStephen Hanson 		(void) nvlist_add_string(fault, UNDIAG_REASON, reason);
3102705e9f42SStephen Hanson 		(void) nvlist_add_boolean_value(fault, FM_SUSPECT_RETIRE,
3103705e9f42SStephen Hanson 		    B_FALSE);
3104705e9f42SStephen Hanson 		(void) nvlist_add_boolean_value(fault, FM_SUSPECT_RESPONSE,
3105705e9f42SStephen Hanson 		    B_FALSE);
3106705e9f42SStephen Hanson 		fmd_case_add_suspect(hdl, newcase->fmcase, fault);
3107705e9f42SStephen Hanson 	}
3108705e9f42SStephen Hanson 	FREE(reason);
3109705e9f42SStephen Hanson 
3110705e9f42SStephen Hanson 	/* solve and close case */
31117c478bd9Sstevel@tonic-gate 	fmd_case_solve(hdl, newcase->fmcase);
31127c478bd9Sstevel@tonic-gate 	fmd_case_close(hdl, newcase->fmcase);
311332d4e834STarik Soydan 	Undiag_reason = UD_VAL_UNKNOWN;
31147c478bd9Sstevel@tonic-gate }
31157c478bd9Sstevel@tonic-gate 
31167c478bd9Sstevel@tonic-gate static void
fme_undiagnosable(struct fme * f)31177c478bd9Sstevel@tonic-gate fme_undiagnosable(struct fme *f)
31187c478bd9Sstevel@tonic-gate {
3119705e9f42SStephen Hanson 	nvlist_t *defect, *fault, *detector = NULL;
3120705e9f42SStephen Hanson 	struct event *ep;
3121705e9f42SStephen Hanson 	char *pathstr;
3122705e9f42SStephen Hanson 	const char *faultstr;
3123705e9f42SStephen Hanson 	char *reason = undiag_2reason_str(Undiag_reason, NULL);
31247c478bd9Sstevel@tonic-gate 
31257c478bd9Sstevel@tonic-gate 	out(O_ALTFP, "[solving/closing FME%d, case %s (%s)]",
3126705e9f42SStephen Hanson 	    f->id, fmd_case_uuid(f->hdl, f->fmcase), reason);
3127705e9f42SStephen Hanson 
3128705e9f42SStephen Hanson 	for (ep = f->observations; ep; ep = ep->observations) {
3129705e9f42SStephen Hanson 
3130705e9f42SStephen Hanson 		if (ep->ffep != f->e0r)
3131705e9f42SStephen Hanson 			fmd_case_add_ereport(f->hdl, f->fmcase, ep->ffep);
3132705e9f42SStephen Hanson 
3133705e9f42SStephen Hanson 		pathstr = ipath2str(NULL, ipath(platform_getpath(ep->nvp)));
3134da40b264SAlex Wilson 		platform_unit_translate(0, f->config, TOPO_PROP_RESOURCE,
3135da40b264SAlex Wilson 		    &detector, pathstr);
3136705e9f42SStephen Hanson 		FREE(pathstr);
3137705e9f42SStephen Hanson 
3138705e9f42SStephen Hanson 		/* add defect */
3139705e9f42SStephen Hanson 		defect = fmd_nvl_create_fault(f->hdl,
3140705e9f42SStephen Hanson 		    undiag_2defect_str(Undiag_reason), 50 / f->uniqobs,
3141705e9f42SStephen Hanson 		    NULL, NULL, detector);
3142705e9f42SStephen Hanson 		(void) nvlist_add_string(defect, UNDIAG_REASON, reason);
3143705e9f42SStephen Hanson 		(void) nvlist_add_boolean_value(defect, FM_SUSPECT_RETIRE,
3144705e9f42SStephen Hanson 		    B_FALSE);
3145705e9f42SStephen Hanson 		(void) nvlist_add_boolean_value(defect, FM_SUSPECT_RESPONSE,
3146705e9f42SStephen Hanson 		    B_FALSE);
3147705e9f42SStephen Hanson 		fmd_case_add_suspect(f->hdl, f->fmcase, defect);
3148705e9f42SStephen Hanson 
3149705e9f42SStephen Hanson 		/* add fault if appropriate */
3150705e9f42SStephen Hanson 		faultstr = undiag_2fault_str(Undiag_reason);
3151705e9f42SStephen Hanson 		if (faultstr == NULL)
3152705e9f42SStephen Hanson 			continue;
3153705e9f42SStephen Hanson 		fault = fmd_nvl_create_fault(f->hdl, faultstr, 50 / f->uniqobs,
3154705e9f42SStephen Hanson 		    NULL, NULL, detector);
3155705e9f42SStephen Hanson 		(void) nvlist_add_string(fault, UNDIAG_REASON, reason);
3156705e9f42SStephen Hanson 		(void) nvlist_add_boolean_value(fault, FM_SUSPECT_RETIRE,
3157705e9f42SStephen Hanson 		    B_FALSE);
3158705e9f42SStephen Hanson 		(void) nvlist_add_boolean_value(fault, FM_SUSPECT_RESPONSE,
3159705e9f42SStephen Hanson 		    B_FALSE);
3160705e9f42SStephen Hanson 		fmd_case_add_suspect(f->hdl, f->fmcase, fault);
3161705e9f42SStephen Hanson 		nvlist_free(detector);
3162705e9f42SStephen Hanson 	}
3163705e9f42SStephen Hanson 	FREE(reason);
31647c478bd9Sstevel@tonic-gate 	fmd_case_solve(f->hdl, f->fmcase);
31657c478bd9Sstevel@tonic-gate 	fmd_case_close(f->hdl, f->fmcase);
316632d4e834STarik Soydan 	Undiag_reason = UD_VAL_UNKNOWN;
31677c478bd9Sstevel@tonic-gate }
31687c478bd9Sstevel@tonic-gate 
31697c478bd9Sstevel@tonic-gate /*
31707c478bd9Sstevel@tonic-gate  * fme_close_case
31717c478bd9Sstevel@tonic-gate  *
31727c478bd9Sstevel@tonic-gate  *	Find the requested case amongst our fmes and close it.  Free up
31737c478bd9Sstevel@tonic-gate  *	the related fme.
31747c478bd9Sstevel@tonic-gate  */
31757c478bd9Sstevel@tonic-gate void
fme_close_case(fmd_hdl_t * hdl,fmd_case_t * fmcase)31767c478bd9Sstevel@tonic-gate fme_close_case(fmd_hdl_t *hdl, fmd_case_t *fmcase)
31777c478bd9Sstevel@tonic-gate {
31787c478bd9Sstevel@tonic-gate 	struct case_list *ucasep, *prevcasep = NULL;
31797c478bd9Sstevel@tonic-gate 	struct fme *prev = NULL;
31807c478bd9Sstevel@tonic-gate 	struct fme *fmep;
31817c478bd9Sstevel@tonic-gate 
31827c478bd9Sstevel@tonic-gate 	for (ucasep = Undiagablecaselist; ucasep; ucasep = ucasep->next) {
31837c478bd9Sstevel@tonic-gate 		if (fmcase != ucasep->fmcase) {
31847c478bd9Sstevel@tonic-gate 			prevcasep = ucasep;
31857c478bd9Sstevel@tonic-gate 			continue;
31867c478bd9Sstevel@tonic-gate 		}
31877c478bd9Sstevel@tonic-gate 
31887c478bd9Sstevel@tonic-gate 		if (prevcasep == NULL)
31897c478bd9Sstevel@tonic-gate 			Undiagablecaselist = Undiagablecaselist->next;
31907c478bd9Sstevel@tonic-gate 		else
31917c478bd9Sstevel@tonic-gate 			prevcasep->next = ucasep->next;
31927c478bd9Sstevel@tonic-gate 
31937c478bd9Sstevel@tonic-gate 		FREE(ucasep);
31947c478bd9Sstevel@tonic-gate 		return;
31957c478bd9Sstevel@tonic-gate 	}
31967c478bd9Sstevel@tonic-gate 
31977c478bd9Sstevel@tonic-gate 	for (fmep = FMElist; fmep; fmep = fmep->next) {
31987c478bd9Sstevel@tonic-gate 		if (fmep->hdl == hdl && fmep->fmcase == fmcase)
31997c478bd9Sstevel@tonic-gate 			break;
32007c478bd9Sstevel@tonic-gate 		prev = fmep;
32017c478bd9Sstevel@tonic-gate 	}
32027c478bd9Sstevel@tonic-gate 
32037c478bd9Sstevel@tonic-gate 	if (fmep == NULL) {
32047c478bd9Sstevel@tonic-gate 		out(O_WARN, "Eft asked to close unrecognized case [%s].",
32057c478bd9Sstevel@tonic-gate 		    fmd_case_uuid(hdl, fmcase));
32067c478bd9Sstevel@tonic-gate 		return;
32077c478bd9Sstevel@tonic-gate 	}
32087c478bd9Sstevel@tonic-gate 
32097c478bd9Sstevel@tonic-gate 	if (EFMElist == fmep)
32107c478bd9Sstevel@tonic-gate 		EFMElist = prev;
32117c478bd9Sstevel@tonic-gate 
32127c478bd9Sstevel@tonic-gate 	if (prev == NULL)
32137c478bd9Sstevel@tonic-gate 		FMElist = FMElist->next;
32147c478bd9Sstevel@tonic-gate 	else
32157c478bd9Sstevel@tonic-gate 		prev->next = fmep->next;
32167c478bd9Sstevel@tonic-gate 
32177c478bd9Sstevel@tonic-gate 	fmep->next = NULL;
32187c478bd9Sstevel@tonic-gate 
32197c478bd9Sstevel@tonic-gate 	/* Get rid of any timer this fme has set */
32207c478bd9Sstevel@tonic-gate 	if (fmep->wull != 0)
32217c478bd9Sstevel@tonic-gate 		fmd_timer_remove(fmep->hdl, fmep->timer);
32227c478bd9Sstevel@tonic-gate 
32237c478bd9Sstevel@tonic-gate 	if (ClosedFMEs == NULL) {
32247c478bd9Sstevel@tonic-gate 		ClosedFMEs = fmep;
32257c478bd9Sstevel@tonic-gate 	} else {
32267c478bd9Sstevel@tonic-gate 		fmep->next = ClosedFMEs;
32277c478bd9Sstevel@tonic-gate 		ClosedFMEs = fmep;
32287c478bd9Sstevel@tonic-gate 	}
32290cc1f05eSjrutt 
32300cc1f05eSjrutt 	Open_fme_count--;
32310cc1f05eSjrutt 
32320cc1f05eSjrutt 	/* See if we can close the overflow FME */
32330cc1f05eSjrutt 	if (Open_fme_count <= Max_fme) {
32340cc1f05eSjrutt 		for (fmep = FMElist; fmep; fmep = fmep->next) {
32350cc1f05eSjrutt 			if (fmep->overflow && !(fmd_case_closed(fmep->hdl,
32360cc1f05eSjrutt 			    fmep->fmcase)))
32370cc1f05eSjrutt 				break;
32380cc1f05eSjrutt 		}
32390cc1f05eSjrutt 
32400cc1f05eSjrutt 		if (fmep != NULL)
32410cc1f05eSjrutt 			fmd_case_close(fmep->hdl, fmep->fmcase);
32420cc1f05eSjrutt 	}
32437c478bd9Sstevel@tonic-gate }
32447c478bd9Sstevel@tonic-gate 
32457c478bd9Sstevel@tonic-gate /*
32467c478bd9Sstevel@tonic-gate  * fme_set_timer()
32477c478bd9Sstevel@tonic-gate  *	If the time we need to wait for the given FME is less than the
32487c478bd9Sstevel@tonic-gate  *	current timer, kick that old timer out and establish a new one.
32497c478bd9Sstevel@tonic-gate  */
32507aec1d6eScindi static int
fme_set_timer(struct fme * fmep,unsigned long long wull)32517c478bd9Sstevel@tonic-gate fme_set_timer(struct fme *fmep, unsigned long long wull)
32527c478bd9Sstevel@tonic-gate {
32537c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB|O_NONL, " fme_set_timer: request to wait ");
32547c478bd9Sstevel@tonic-gate 	ptree_timeval(O_ALTFP|O_VERB, &wull);
32557c478bd9Sstevel@tonic-gate 
32567c478bd9Sstevel@tonic-gate 	if (wull <= fmep->pull) {
32577c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB|O_NONL, "already have waited at least ");
32587c478bd9Sstevel@tonic-gate 		ptree_timeval(O_ALTFP|O_VERB, &fmep->pull);
32597c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB, NULL);
32607c478bd9Sstevel@tonic-gate 		/* we've waited at least wull already, don't need timer */
32617aec1d6eScindi 		return (0);
32627c478bd9Sstevel@tonic-gate 	}
32637c478bd9Sstevel@tonic-gate 
32647c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB|O_NONL, " currently ");
32657c478bd9Sstevel@tonic-gate 	if (fmep->wull != 0) {
32667c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB|O_NONL, "waiting ");
32677c478bd9Sstevel@tonic-gate 		ptree_timeval(O_ALTFP|O_VERB, &fmep->wull);
32687c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB, NULL);
32697c478bd9Sstevel@tonic-gate 	} else {
32707c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB|O_NONL, "not waiting");
32717c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB, NULL);
32727c478bd9Sstevel@tonic-gate 	}
32737c478bd9Sstevel@tonic-gate 
32747c478bd9Sstevel@tonic-gate 	if (fmep->wull != 0)
32757c478bd9Sstevel@tonic-gate 		if (wull >= fmep->wull)
32767c478bd9Sstevel@tonic-gate 			/* New timer would fire later than established timer */
32777aec1d6eScindi 			return (0);
32787c478bd9Sstevel@tonic-gate 
32797aec1d6eScindi 	if (fmep->wull != 0) {
32807c478bd9Sstevel@tonic-gate 		fmd_timer_remove(fmep->hdl, fmep->timer);
32817aec1d6eScindi 	}
32827c478bd9Sstevel@tonic-gate 
32837c478bd9Sstevel@tonic-gate 	fmep->timer = fmd_timer_install(fmep->hdl, (void *)fmep,
32847c478bd9Sstevel@tonic-gate 	    fmep->e0r, wull);
32857c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB, "timer set, id is %ld", fmep->timer);
32867c478bd9Sstevel@tonic-gate 	fmep->wull = wull;
32877aec1d6eScindi 	return (1);
32887c478bd9Sstevel@tonic-gate }
32897c478bd9Sstevel@tonic-gate 
32907c478bd9Sstevel@tonic-gate void
fme_timer_fired(struct fme * fmep,id_t tid)32917c478bd9Sstevel@tonic-gate fme_timer_fired(struct fme *fmep, id_t tid)
32927c478bd9Sstevel@tonic-gate {
32937c478bd9Sstevel@tonic-gate 	struct fme *ffmep = NULL;
32947c478bd9Sstevel@tonic-gate 
32957c478bd9Sstevel@tonic-gate 	for (ffmep = FMElist; ffmep; ffmep = ffmep->next)
32967c478bd9Sstevel@tonic-gate 		if (ffmep == fmep)
32977c478bd9Sstevel@tonic-gate 			break;
32987c478bd9Sstevel@tonic-gate 
32997c478bd9Sstevel@tonic-gate 	if (ffmep == NULL) {
33007c478bd9Sstevel@tonic-gate 		out(O_WARN, "Timer fired for an FME (%p) not in FMEs list.",
33017c478bd9Sstevel@tonic-gate 		    (void *)fmep);
33027c478bd9Sstevel@tonic-gate 		return;
33037c478bd9Sstevel@tonic-gate 	}
33047c478bd9Sstevel@tonic-gate 
3305b5016cbbSstephh 	out(O_ALTFP|O_VERB, "Timer fired %lx", tid);
3306d96ce684Sstephh 	fmep->pull = fmep->wull;
3307d96ce684Sstephh 	fmep->wull = 0;
3308d96ce684Sstephh 	fmd_buf_write(fmep->hdl, fmep->fmcase,
3309d96ce684Sstephh 	    WOBUF_PULL, (void *)&fmep->pull, sizeof (fmep->pull));
3310b5016cbbSstephh 
331100d0963fSdilpreet 	fme_eval(fmep, fmep->e0r);
33127c478bd9Sstevel@tonic-gate }
33137c478bd9Sstevel@tonic-gate 
33147c478bd9Sstevel@tonic-gate /*
33157c478bd9Sstevel@tonic-gate  * Preserve the fme's suspect list in its psuspects list, NULLing the
33167c478bd9Sstevel@tonic-gate  * suspects list in the meantime.
33177c478bd9Sstevel@tonic-gate  */
33187c478bd9Sstevel@tonic-gate static void
save_suspects(struct fme * fmep)33197c478bd9Sstevel@tonic-gate save_suspects(struct fme *fmep)
33207c478bd9Sstevel@tonic-gate {
33217c478bd9Sstevel@tonic-gate 	struct event *ep;
33227c478bd9Sstevel@tonic-gate 	struct event *nextep;
33237c478bd9Sstevel@tonic-gate 
33247c478bd9Sstevel@tonic-gate 	/* zero out the previous suspect list */
33257c478bd9Sstevel@tonic-gate 	for (ep = fmep->psuspects; ep; ep = nextep) {
33267c478bd9Sstevel@tonic-gate 		nextep = ep->psuspects;
33277c478bd9Sstevel@tonic-gate 		ep->psuspects = NULL;
33287c478bd9Sstevel@tonic-gate 	}
33297c478bd9Sstevel@tonic-gate 	fmep->psuspects = NULL;
33307c478bd9Sstevel@tonic-gate 
33317c478bd9Sstevel@tonic-gate 	/* zero out the suspect list, copying it to previous suspect list */
33327c478bd9Sstevel@tonic-gate 	fmep->psuspects = fmep->suspects;
33337c478bd9Sstevel@tonic-gate 	for (ep = fmep->suspects; ep; ep = nextep) {
33347c478bd9Sstevel@tonic-gate 		nextep = ep->suspects;
33357c478bd9Sstevel@tonic-gate 		ep->psuspects = ep->suspects;
33367c478bd9Sstevel@tonic-gate 		ep->suspects = NULL;
33377c478bd9Sstevel@tonic-gate 		ep->is_suspect = 0;
33387c478bd9Sstevel@tonic-gate 	}
33397c478bd9Sstevel@tonic-gate 	fmep->suspects = NULL;
33407c478bd9Sstevel@tonic-gate 	fmep->nsuspects = 0;
33417c478bd9Sstevel@tonic-gate }
33427c478bd9Sstevel@tonic-gate 
33437c478bd9Sstevel@tonic-gate /*
33447c478bd9Sstevel@tonic-gate  * Retrieve the fme's suspect list from its psuspects list.
33457c478bd9Sstevel@tonic-gate  */
33467c478bd9Sstevel@tonic-gate static void
restore_suspects(struct fme * fmep)33477c478bd9Sstevel@tonic-gate restore_suspects(struct fme *fmep)
33487c478bd9Sstevel@tonic-gate {
33497c478bd9Sstevel@tonic-gate 	struct event *ep;
33507c478bd9Sstevel@tonic-gate 	struct event *nextep;
33517c478bd9Sstevel@tonic-gate 
33528e7248e5SStephen Hanson 	fmep->nsuspects = 0;
33537c478bd9Sstevel@tonic-gate 	fmep->suspects = fmep->psuspects;
33547c478bd9Sstevel@tonic-gate 	for (ep = fmep->psuspects; ep; ep = nextep) {
33557c478bd9Sstevel@tonic-gate 		fmep->nsuspects++;
33567c478bd9Sstevel@tonic-gate 		nextep = ep->psuspects;
33577c478bd9Sstevel@tonic-gate 		ep->suspects = ep->psuspects;
33587c478bd9Sstevel@tonic-gate 	}
33597c478bd9Sstevel@tonic-gate }
33607c478bd9Sstevel@tonic-gate 
33617c478bd9Sstevel@tonic-gate /*
33627c478bd9Sstevel@tonic-gate  * this is what we use to call the Emrys prototype code instead of main()
33637c478bd9Sstevel@tonic-gate  */
33647c478bd9Sstevel@tonic-gate static void
fme_eval(struct fme * fmep,fmd_event_t * ffep)33657c478bd9Sstevel@tonic-gate fme_eval(struct fme *fmep, fmd_event_t *ffep)
33667c478bd9Sstevel@tonic-gate {
33677c478bd9Sstevel@tonic-gate 	struct event *ep;
33687c478bd9Sstevel@tonic-gate 	unsigned long long my_delay = TIMEVAL_EVENTUALLY;
3369b7d3956bSstephh 	struct rsl *srl = NULL;
3370b7d3956bSstephh 	struct rsl *srl2 = NULL;
3371b7d3956bSstephh 	int mess_zero_count;
3372b7d3956bSstephh 	int rpcnt;
33737c478bd9Sstevel@tonic-gate 
33747c478bd9Sstevel@tonic-gate 	save_suspects(fmep);
33757c478bd9Sstevel@tonic-gate 
3376b5016cbbSstephh 	out(O_ALTFP, "Evaluate FME %d", fmep->id);
33777c478bd9Sstevel@tonic-gate 	indent_set("  ");
33787c478bd9Sstevel@tonic-gate 
33797aec1d6eScindi 	lut_walk(fmep->eventtree, (lut_cb)clear_arrows, (void *)fmep);
33807aec1d6eScindi 	fmep->state = hypothesise(fmep, fmep->e0, fmep->ull, &my_delay);
33817c478bd9Sstevel@tonic-gate 
3382b5016cbbSstephh 	out(O_ALTFP|O_NONL, "FME%d state: %s, suspect list:", fmep->id,
33837c478bd9Sstevel@tonic-gate 	    fme_state2str(fmep->state));
33847c478bd9Sstevel@tonic-gate 	for (ep = fmep->suspects; ep; ep = ep->suspects) {
3385b5016cbbSstephh 		out(O_ALTFP|O_NONL, " ");
3386b5016cbbSstephh 		itree_pevent_brief(O_ALTFP|O_NONL, ep);
33877c478bd9Sstevel@tonic-gate 	}
3388b5016cbbSstephh 	out(O_ALTFP, NULL);
33897c478bd9Sstevel@tonic-gate 
3390d96ce684Sstephh 	switch (fmep->state) {
3391d96ce684Sstephh 	case FME_CREDIBLE:
3392d96ce684Sstephh 		print_suspects(SLNEW, fmep);
3393d96ce684Sstephh 		(void) upsets_eval(fmep, ffep);
3394d96ce684Sstephh 
33957c478bd9Sstevel@tonic-gate 		/*
3396d96ce684Sstephh 		 * we may have already posted suspects in upsets_eval() which
3397d96ce684Sstephh 		 * can recurse into fme_eval() again. If so then just return.
33987c478bd9Sstevel@tonic-gate 		 */
3399d96ce684Sstephh 		if (fmep->posted_suspects)
3400d96ce684Sstephh 			return;
34017c478bd9Sstevel@tonic-gate 
3402b7d3956bSstephh 		stats_counter_bump(fmep->diags);
3403b7d3956bSstephh 		rpcnt = fmep->nsuspects;
3404b7d3956bSstephh 		save_suspects(fmep);
3405b7d3956bSstephh 
3406b7d3956bSstephh 		/*
3407b7d3956bSstephh 		 * create two lists, one for "message=1" faults and one for
3408b7d3956bSstephh 		 * "message=0" faults. If we have a mixture we will generate
3409b7d3956bSstephh 		 * two separate suspect lists.
3410b7d3956bSstephh 		 */
3411b7d3956bSstephh 		srl = MALLOC(rpcnt * sizeof (struct rsl));
3412b7d3956bSstephh 		bzero(srl, rpcnt * sizeof (struct rsl));
3413b7d3956bSstephh 		srl2 = MALLOC(rpcnt * sizeof (struct rsl));
3414b7d3956bSstephh 		bzero(srl2, rpcnt * sizeof (struct rsl));
34158e7248e5SStephen Hanson 		mess_zero_count = trim_suspects(fmep, srl, srl2, ffep);
3416b7d3956bSstephh 
3417b7d3956bSstephh 		/*
3418b7d3956bSstephh 		 * If the resulting suspect list has no members, we're
3419b7d3956bSstephh 		 * done so simply close the case. Otherwise sort and publish.
3420b7d3956bSstephh 		 */
3421b7d3956bSstephh 		if (fmep->nsuspects == 0 && mess_zero_count == 0) {
3422b7d3956bSstephh 			out(O_ALTFP,
3423b7d3956bSstephh 			    "[FME%d, case %s (all suspects are upsets)]",
3424b7d3956bSstephh 			    fmep->id, fmd_case_uuid(fmep->hdl, fmep->fmcase));
3425b7d3956bSstephh 			fmd_case_close(fmep->hdl, fmep->fmcase);
3426b7d3956bSstephh 		} else if (fmep->nsuspects != 0 && mess_zero_count == 0) {
3427b7d3956bSstephh 			publish_suspects(fmep, srl);
3428b7d3956bSstephh 			out(O_ALTFP, "[solving FME%d, case %s]", fmep->id,
3429b7d3956bSstephh 			    fmd_case_uuid(fmep->hdl, fmep->fmcase));
3430b7d3956bSstephh 			fmd_case_solve(fmep->hdl, fmep->fmcase);
3431b7d3956bSstephh 		} else if (fmep->nsuspects == 0 && mess_zero_count != 0) {
3432b7d3956bSstephh 			fmep->nsuspects = mess_zero_count;
3433b7d3956bSstephh 			publish_suspects(fmep, srl2);
3434b7d3956bSstephh 			out(O_ALTFP, "[solving FME%d, case %s]", fmep->id,
3435b7d3956bSstephh 			    fmd_case_uuid(fmep->hdl, fmep->fmcase));
3436b7d3956bSstephh 			fmd_case_solve(fmep->hdl, fmep->fmcase);
3437b7d3956bSstephh 		} else {
3438b7d3956bSstephh 			struct event *obsp;
3439b7d3956bSstephh 			struct fme *nfmep;
3440b7d3956bSstephh 
3441b7d3956bSstephh 			publish_suspects(fmep, srl);
3442b7d3956bSstephh 			out(O_ALTFP, "[solving FME%d, case %s]", fmep->id,
3443b7d3956bSstephh 			    fmd_case_uuid(fmep->hdl, fmep->fmcase));
3444b7d3956bSstephh 			fmd_case_solve(fmep->hdl, fmep->fmcase);
3445b7d3956bSstephh 
3446b7d3956bSstephh 			/*
3447b7d3956bSstephh 			 * Got both message=0 and message=1 so create a
3448b7d3956bSstephh 			 * duplicate case. Also need a temporary duplicate fme
3449b7d3956bSstephh 			 * structure for use by publish_suspects().
3450b7d3956bSstephh 			 */
3451b7d3956bSstephh 			nfmep = alloc_fme();
3452b7d3956bSstephh 			nfmep->id =  Nextid++;
3453b7d3956bSstephh 			nfmep->hdl = fmep->hdl;
3454b7d3956bSstephh 			nfmep->nsuspects = mess_zero_count;
3455b7d3956bSstephh 			nfmep->fmcase = fmd_case_open(fmep->hdl, NULL);
3456b7d3956bSstephh 			out(O_ALTFP|O_STAMP,
3457b7d3956bSstephh 			    "[creating parallel FME%d, case %s]", nfmep->id,
3458b7d3956bSstephh 			    fmd_case_uuid(nfmep->hdl, nfmep->fmcase));
3459b7d3956bSstephh 			Open_fme_count++;
3460b7d3956bSstephh 			if (ffep) {
3461b7d3956bSstephh 				fmd_case_setprincipal(nfmep->hdl,
3462b7d3956bSstephh 				    nfmep->fmcase, ffep);
3463b7d3956bSstephh 				fmd_case_add_ereport(nfmep->hdl,
3464b7d3956bSstephh 				    nfmep->fmcase, ffep);
3465b7d3956bSstephh 			}
3466b7d3956bSstephh 			for (obsp = fmep->observations; obsp;
3467b7d3956bSstephh 			    obsp = obsp->observations)
3468b7d3956bSstephh 				if (obsp->ffep && obsp->ffep != ffep)
3469b7d3956bSstephh 					fmd_case_add_ereport(nfmep->hdl,
3470b7d3956bSstephh 					    nfmep->fmcase, obsp->ffep);
3471b7d3956bSstephh 
3472b7d3956bSstephh 			publish_suspects(nfmep, srl2);
3473b7d3956bSstephh 			out(O_ALTFP, "[solving FME%d, case %s]", nfmep->id,
3474b7d3956bSstephh 			    fmd_case_uuid(nfmep->hdl, nfmep->fmcase));
3475b7d3956bSstephh 			fmd_case_solve(nfmep->hdl, nfmep->fmcase);
3476b7d3956bSstephh 			FREE(nfmep);
3477b7d3956bSstephh 		}
3478b7d3956bSstephh 		FREE(srl);
3479b7d3956bSstephh 		FREE(srl2);
3480b7d3956bSstephh 		restore_suspects(fmep);
3481b7d3956bSstephh 
3482d96ce684Sstephh 		fmep->posted_suspects = 1;
3483d96ce684Sstephh 		fmd_buf_write(fmep->hdl, fmep->fmcase,
3484d96ce684Sstephh 		    WOBUF_POSTD,
3485d96ce684Sstephh 		    (void *)&fmep->posted_suspects,
3486d96ce684Sstephh 		    sizeof (fmep->posted_suspects));
34877c478bd9Sstevel@tonic-gate 
3488d96ce684Sstephh 		/*
3489d96ce684Sstephh 		 * Now the suspects have been posted, we can clear up
3490d96ce684Sstephh 		 * the instance tree as we won't be looking at it again.
3491d96ce684Sstephh 		 * Also cancel the timer as the case is now solved.
3492d96ce684Sstephh 		 */
3493d96ce684Sstephh 		if (fmep->wull != 0) {
3494d96ce684Sstephh 			fmd_timer_remove(fmep->hdl, fmep->timer);
3495d96ce684Sstephh 			fmep->wull = 0;
34967c478bd9Sstevel@tonic-gate 		}
3497d96ce684Sstephh 		break;
3498d96ce684Sstephh 
3499d96ce684Sstephh 	case FME_WAIT:
3500d96ce684Sstephh 		ASSERT(my_delay > fmep->ull);
3501d96ce684Sstephh 		(void) fme_set_timer(fmep, my_delay);
3502d96ce684Sstephh 		print_suspects(SLWAIT, fmep);
3503b5016cbbSstephh 		itree_prune(fmep->eventtree);
3504b5016cbbSstephh 		return;
3505d96ce684Sstephh 
3506d96ce684Sstephh 	case FME_DISPROVED:
3507d96ce684Sstephh 		print_suspects(SLDISPROVED, fmep);
350832d4e834STarik Soydan 		Undiag_reason = UD_VAL_UNSOLVD;
3509d96ce684Sstephh 		fme_undiagnosable(fmep);
3510d96ce684Sstephh 		break;
35117c478bd9Sstevel@tonic-gate 	}
35127c478bd9Sstevel@tonic-gate 
3513b5016cbbSstephh 	itree_free(fmep->eventtree);
3514b5016cbbSstephh 	fmep->eventtree = NULL;
3515e5ba14ffSstephh 	structconfig_free(fmep->config);
3516e5ba14ffSstephh 	fmep->config = NULL;
3517b5016cbbSstephh 	destroy_fme_bufs(fmep);
35187c478bd9Sstevel@tonic-gate }
35197c478bd9Sstevel@tonic-gate 
35207c478bd9Sstevel@tonic-gate static void indent(void);
35217c478bd9Sstevel@tonic-gate static int triggered(struct fme *fmep, struct event *ep, int mark);
35227c478bd9Sstevel@tonic-gate static enum fme_state effects_test(struct fme *fmep,
35237aec1d6eScindi     struct event *fault_event, unsigned long long at_latest_by,
35247aec1d6eScindi     unsigned long long *pdelay);
35257c478bd9Sstevel@tonic-gate static enum fme_state requirements_test(struct fme *fmep, struct event *ep,
35267aec1d6eScindi     unsigned long long at_latest_by, unsigned long long *pdelay);
35277c478bd9Sstevel@tonic-gate static enum fme_state causes_test(struct fme *fmep, struct event *ep,
35287c478bd9Sstevel@tonic-gate     unsigned long long at_latest_by, unsigned long long *pdelay);
35297c478bd9Sstevel@tonic-gate 
35307aec1d6eScindi static int
checkconstraints(struct fme * fmep,struct arrow * arrowp)35317aec1d6eScindi checkconstraints(struct fme *fmep, struct arrow *arrowp)
35327aec1d6eScindi {
35337aec1d6eScindi 	struct constraintlist *ctp;
35347aec1d6eScindi 	struct evalue value;
3535b5016cbbSstephh 	char *sep = "";
35367aec1d6eScindi 
35377aec1d6eScindi 	if (arrowp->forever_false) {
35387aec1d6eScindi 		indent();
35397aec1d6eScindi 		out(O_ALTFP|O_VERB|O_NONL, "  Forever false constraint: ");
35407aec1d6eScindi 		for (ctp = arrowp->constraints; ctp != NULL; ctp = ctp->next) {
35417aec1d6eScindi 			out(O_ALTFP|O_VERB|O_NONL, sep);
35427aec1d6eScindi 			ptree(O_ALTFP|O_VERB|O_NONL, ctp->cnode, 1, 0);
35437aec1d6eScindi 			sep = ", ";
35447aec1d6eScindi 		}
35457aec1d6eScindi 		out(O_ALTFP|O_VERB, NULL);
35467aec1d6eScindi 		return (0);
35477aec1d6eScindi 	}
3548b5016cbbSstephh 	if (arrowp->forever_true) {
3549b5016cbbSstephh 		indent();
3550b5016cbbSstephh 		out(O_ALTFP|O_VERB|O_NONL, "  Forever true constraint: ");
3551b5016cbbSstephh 		for (ctp = arrowp->constraints; ctp != NULL; ctp = ctp->next) {
3552b5016cbbSstephh 			out(O_ALTFP|O_VERB|O_NONL, sep);
3553b5016cbbSstephh 			ptree(O_ALTFP|O_VERB|O_NONL, ctp->cnode, 1, 0);
3554b5016cbbSstephh 			sep = ", ";
3555b5016cbbSstephh 		}
3556b5016cbbSstephh 		out(O_ALTFP|O_VERB, NULL);
3557b5016cbbSstephh 		return (1);
3558b5016cbbSstephh 	}
35597aec1d6eScindi 
35607aec1d6eScindi 	for (ctp = arrowp->constraints; ctp != NULL; ctp = ctp->next) {
35617aec1d6eScindi 		if (eval_expr(ctp->cnode, NULL, NULL,
3562e5ba14ffSstephh 		    &fmep->globals, fmep->config,
35637aec1d6eScindi 		    arrowp, 0, &value)) {
35647aec1d6eScindi 			/* evaluation successful */
35657aec1d6eScindi 			if (value.t == UNDEFINED || value.v == 0) {
35667aec1d6eScindi 				/* known false */
35677aec1d6eScindi 				arrowp->forever_false = 1;
35687aec1d6eScindi 				indent();
35697aec1d6eScindi 				out(O_ALTFP|O_VERB|O_NONL,
35707aec1d6eScindi 				    "  False constraint: ");
35717aec1d6eScindi 				ptree(O_ALTFP|O_VERB|O_NONL, ctp->cnode, 1, 0);
35727aec1d6eScindi 				out(O_ALTFP|O_VERB, NULL);
35737aec1d6eScindi 				return (0);
35747aec1d6eScindi 			}
35757aec1d6eScindi 		} else {
35767aec1d6eScindi 			/* evaluation unsuccessful -- unknown value */
35777aec1d6eScindi 			indent();
35787aec1d6eScindi 			out(O_ALTFP|O_VERB|O_NONL,
35797aec1d6eScindi 			    "  Deferred constraint: ");
35807aec1d6eScindi 			ptree(O_ALTFP|O_VERB|O_NONL, ctp->cnode, 1, 0);
35817aec1d6eScindi 			out(O_ALTFP|O_VERB, NULL);
3582b5016cbbSstephh 			return (1);
35837aec1d6eScindi 		}
35847aec1d6eScindi 	}
35857aec1d6eScindi 	/* known true */
3586b5016cbbSstephh 	arrowp->forever_true = 1;
3587b5016cbbSstephh 	indent();
3588b5016cbbSstephh 	out(O_ALTFP|O_VERB|O_NONL, "  True constraint: ");
3589b5016cbbSstephh 	for (ctp = arrowp->constraints; ctp != NULL; ctp = ctp->next) {
3590b5016cbbSstephh 		out(O_ALTFP|O_VERB|O_NONL, sep);
3591b5016cbbSstephh 		ptree(O_ALTFP|O_VERB|O_NONL, ctp->cnode, 1, 0);
3592b5016cbbSstephh 		sep = ", ";
3593b5016cbbSstephh 	}
3594b5016cbbSstephh 	out(O_ALTFP|O_VERB, NULL);
35957aec1d6eScindi 	return (1);
35967aec1d6eScindi }
35977aec1d6eScindi 
35987c478bd9Sstevel@tonic-gate static int
triggered(struct fme * fmep,struct event * ep,int mark)35997c478bd9Sstevel@tonic-gate triggered(struct fme *fmep, struct event *ep, int mark)
36007c478bd9Sstevel@tonic-gate {
36017c478bd9Sstevel@tonic-gate 	struct bubble *bp;
36027c478bd9Sstevel@tonic-gate 	struct arrowlist *ap;
36037c478bd9Sstevel@tonic-gate 	int count = 0;
36047c478bd9Sstevel@tonic-gate 
36057c478bd9Sstevel@tonic-gate 	stats_counter_bump(fmep->Tcallcount);
36067c478bd9Sstevel@tonic-gate 	for (bp = itree_next_bubble(ep, NULL); bp;
36077c478bd9Sstevel@tonic-gate 	    bp = itree_next_bubble(ep, bp)) {
36087c478bd9Sstevel@tonic-gate 		if (bp->t != B_TO)
36097c478bd9Sstevel@tonic-gate 			continue;
36107c478bd9Sstevel@tonic-gate 		for (ap = itree_next_arrow(bp, NULL); ap;
36117c478bd9Sstevel@tonic-gate 		    ap = itree_next_arrow(bp, ap)) {
36127c478bd9Sstevel@tonic-gate 			/* check count of marks against K in the bubble */
36137aec1d6eScindi 			if ((ap->arrowp->mark & mark) &&
36147c478bd9Sstevel@tonic-gate 			    ++count >= bp->nork)
36157c478bd9Sstevel@tonic-gate 				return (1);
36167c478bd9Sstevel@tonic-gate 		}
36177c478bd9Sstevel@tonic-gate 	}
36187c478bd9Sstevel@tonic-gate 	return (0);
36197c478bd9Sstevel@tonic-gate }
36207c478bd9Sstevel@tonic-gate 
36217aec1d6eScindi static int
mark_arrows(struct fme * fmep,struct event * ep,int mark,unsigned long long at_latest_by,unsigned long long * pdelay,int keep)36227aec1d6eScindi mark_arrows(struct fme *fmep, struct event *ep, int mark,
362300d0963fSdilpreet     unsigned long long at_latest_by, unsigned long long *pdelay, int keep)
36247c478bd9Sstevel@tonic-gate {
36257c478bd9Sstevel@tonic-gate 	struct bubble *bp;
36267c478bd9Sstevel@tonic-gate 	struct arrowlist *ap;
36277aec1d6eScindi 	unsigned long long overall_delay = TIMEVAL_EVENTUALLY;
36287aec1d6eScindi 	unsigned long long my_delay;
36297aec1d6eScindi 	enum fme_state result;
36307aec1d6eScindi 	int retval = 0;
36317c478bd9Sstevel@tonic-gate 
36327c478bd9Sstevel@tonic-gate 	for (bp = itree_next_bubble(ep, NULL); bp;
36337c478bd9Sstevel@tonic-gate 	    bp = itree_next_bubble(ep, bp)) {
36347c478bd9Sstevel@tonic-gate 		if (bp->t != B_FROM)
36357c478bd9Sstevel@tonic-gate 			continue;
36367aec1d6eScindi 		stats_counter_bump(fmep->Marrowcount);
36377aec1d6eScindi 		for (ap = itree_next_arrow(bp, NULL); ap;
36387aec1d6eScindi 		    ap = itree_next_arrow(bp, ap)) {
36397aec1d6eScindi 			struct event *ep2 = ap->arrowp->head->myevent;
36407aec1d6eScindi 			/*
36417aec1d6eScindi 			 * if we're clearing marks, we can avoid doing
36427aec1d6eScindi 			 * all that work evaluating constraints.
36437aec1d6eScindi 			 */
36447aec1d6eScindi 			if (mark == 0) {
3645b5016cbbSstephh 				if (ap->arrowp->arrow_marked == 0)
3646b5016cbbSstephh 					continue;
3647b5016cbbSstephh 				ap->arrowp->arrow_marked = 0;
36487aec1d6eScindi 				ap->arrowp->mark &= ~EFFECTS_COUNTER;
364900d0963fSdilpreet 				if (keep && (ep2->cached_state &
365000d0963fSdilpreet 				    (WAIT_EFFECT|CREDIBLE_EFFECT|PARENT_WAIT)))
365100d0963fSdilpreet 					ep2->keep_in_tree = 1;
36527aec1d6eScindi 				ep2->cached_state &=
36537aec1d6eScindi 				    ~(WAIT_EFFECT|CREDIBLE_EFFECT|PARENT_WAIT);
365400d0963fSdilpreet 				(void) mark_arrows(fmep, ep2, mark, 0, NULL,
365500d0963fSdilpreet 				    keep);
36567aec1d6eScindi 				continue;
36577aec1d6eScindi 			}
3658b5016cbbSstephh 			ap->arrowp->arrow_marked = 1;
36597aec1d6eScindi 			if (ep2->cached_state & REQMNTS_DISPROVED) {
36607aec1d6eScindi 				indent();
36617aec1d6eScindi 				out(O_ALTFP|O_VERB|O_NONL,
36627aec1d6eScindi 				    "  ALREADY DISPROVED ");
36637aec1d6eScindi 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
36647aec1d6eScindi 				out(O_ALTFP|O_VERB, NULL);
36657aec1d6eScindi 				continue;
36667aec1d6eScindi 			}
36677aec1d6eScindi 			if (ep2->cached_state & WAIT_EFFECT) {
36687aec1d6eScindi 				indent();
36697aec1d6eScindi 				out(O_ALTFP|O_VERB|O_NONL,
36707aec1d6eScindi 				    "  ALREADY EFFECTS WAIT ");
36717aec1d6eScindi 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
36727aec1d6eScindi 				out(O_ALTFP|O_VERB, NULL);
36737aec1d6eScindi 				continue;
36747aec1d6eScindi 			}
36757aec1d6eScindi 			if (ep2->cached_state & CREDIBLE_EFFECT) {
36767aec1d6eScindi 				indent();
36777aec1d6eScindi 				out(O_ALTFP|O_VERB|O_NONL,
36787aec1d6eScindi 				    "  ALREADY EFFECTS CREDIBLE ");
36797aec1d6eScindi 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
36807aec1d6eScindi 				out(O_ALTFP|O_VERB, NULL);
36817aec1d6eScindi 				continue;
36827aec1d6eScindi 			}
36837aec1d6eScindi 			if ((ep2->cached_state & PARENT_WAIT) &&
36847aec1d6eScindi 			    (mark & PARENT_WAIT)) {
36857aec1d6eScindi 				indent();
36867aec1d6eScindi 				out(O_ALTFP|O_VERB|O_NONL,
36877aec1d6eScindi 				    "  ALREADY PARENT EFFECTS WAIT ");
36887aec1d6eScindi 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
36897aec1d6eScindi 				out(O_ALTFP|O_VERB, NULL);
36907aec1d6eScindi 				continue;
36917aec1d6eScindi 			}
36927aec1d6eScindi 			platform_set_payloadnvp(ep2->nvp);
369300d0963fSdilpreet 			if (checkconstraints(fmep, ap->arrowp) == 0) {
36947aec1d6eScindi 				platform_set_payloadnvp(NULL);
36957aec1d6eScindi 				indent();
36967aec1d6eScindi 				out(O_ALTFP|O_VERB|O_NONL,
36977aec1d6eScindi 				    "  CONSTRAINTS FAIL ");
36987aec1d6eScindi 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
36997aec1d6eScindi 				out(O_ALTFP|O_VERB, NULL);
37007aec1d6eScindi 				continue;
37017aec1d6eScindi 			}
37027aec1d6eScindi 			platform_set_payloadnvp(NULL);
37037aec1d6eScindi 			ap->arrowp->mark |= EFFECTS_COUNTER;
37047aec1d6eScindi 			if (!triggered(fmep, ep2, EFFECTS_COUNTER)) {
37057aec1d6eScindi 				indent();
37067aec1d6eScindi 				out(O_ALTFP|O_VERB|O_NONL,
37077aec1d6eScindi 				    "  K-COUNT NOT YET MET ");
37087aec1d6eScindi 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
37097aec1d6eScindi 				out(O_ALTFP|O_VERB, NULL);
37107aec1d6eScindi 				continue;
37117aec1d6eScindi 			}
37127aec1d6eScindi 			ep2->cached_state &= ~PARENT_WAIT;
3713d96ce684Sstephh 			/*
3714d96ce684Sstephh 			 * if we've reached an ereport and no propagation time
3715d96ce684Sstephh 			 * is specified, use the Hesitate value
3716d96ce684Sstephh 			 */
3717d96ce684Sstephh 			if (ep2->t == N_EREPORT && at_latest_by == 0ULL &&
3718d96ce684Sstephh 			    ap->arrowp->maxdelay == 0ULL) {
3719d96ce684Sstephh 				out(O_ALTFP|O_VERB|O_NONL, "  default wait ");
3720d96ce684Sstephh 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
3721d96ce684Sstephh 				out(O_ALTFP|O_VERB, NULL);
3722b5016cbbSstephh 				result = requirements_test(fmep, ep2, Hesitate,
3723b5016cbbSstephh 				    &my_delay);
3724d96ce684Sstephh 			} else {
3725d96ce684Sstephh 				result = requirements_test(fmep, ep2,
3726d96ce684Sstephh 				    at_latest_by + ap->arrowp->maxdelay,
3727d96ce684Sstephh 				    &my_delay);
3728d96ce684Sstephh 			}
37297aec1d6eScindi 			if (result == FME_WAIT) {
37307aec1d6eScindi 				retval = WAIT_EFFECT;
37317aec1d6eScindi 				if (overall_delay > my_delay)
37327aec1d6eScindi 					overall_delay = my_delay;
37337aec1d6eScindi 				ep2->cached_state |= WAIT_EFFECT;
37347aec1d6eScindi 				indent();
37357aec1d6eScindi 				out(O_ALTFP|O_VERB|O_NONL, "  EFFECTS WAIT ");
37367aec1d6eScindi 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
37377aec1d6eScindi 				out(O_ALTFP|O_VERB, NULL);
37387aec1d6eScindi 				indent_push("  E");
37397aec1d6eScindi 				if (mark_arrows(fmep, ep2, PARENT_WAIT,
374000d0963fSdilpreet 				    at_latest_by, &my_delay, 0) ==
374100d0963fSdilpreet 				    WAIT_EFFECT) {
37427aec1d6eScindi 					retval = WAIT_EFFECT;
37437aec1d6eScindi 					if (overall_delay > my_delay)
37447aec1d6eScindi 						overall_delay = my_delay;
37457c478bd9Sstevel@tonic-gate 				}
37467aec1d6eScindi 				indent_pop();
37477aec1d6eScindi 			} else if (result == FME_DISPROVED) {
37487aec1d6eScindi 				indent();
37497aec1d6eScindi 				out(O_ALTFP|O_VERB|O_NONL,
37507aec1d6eScindi 				    "  EFFECTS DISPROVED ");
37517aec1d6eScindi 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
37527aec1d6eScindi 				out(O_ALTFP|O_VERB, NULL);
37537aec1d6eScindi 			} else {
37547aec1d6eScindi 				ep2->cached_state |= mark;
37557aec1d6eScindi 				indent();
37567aec1d6eScindi 				if (mark == CREDIBLE_EFFECT)
37577c478bd9Sstevel@tonic-gate 					out(O_ALTFP|O_VERB|O_NONL,
37587aec1d6eScindi 					    "  EFFECTS CREDIBLE ");
37597aec1d6eScindi 				else
37607aec1d6eScindi 					out(O_ALTFP|O_VERB|O_NONL,
37617aec1d6eScindi 					    "  PARENT EFFECTS WAIT ");
37627aec1d6eScindi 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
37637aec1d6eScindi 				out(O_ALTFP|O_VERB, NULL);
37647aec1d6eScindi 				indent_push("  E");
37657aec1d6eScindi 				if (mark_arrows(fmep, ep2, mark, at_latest_by,
376600d0963fSdilpreet 				    &my_delay, 0) == WAIT_EFFECT) {
37677aec1d6eScindi 					retval = WAIT_EFFECT;
37687aec1d6eScindi 					if (overall_delay > my_delay)
37697aec1d6eScindi 						overall_delay = my_delay;
37707c478bd9Sstevel@tonic-gate 				}
37717aec1d6eScindi 				indent_pop();
37727c478bd9Sstevel@tonic-gate 			}
37737c478bd9Sstevel@tonic-gate 		}
37747c478bd9Sstevel@tonic-gate 	}
37757aec1d6eScindi 	if (retval == WAIT_EFFECT)
37767aec1d6eScindi 		*pdelay = overall_delay;
37777aec1d6eScindi 	return (retval);
37787c478bd9Sstevel@tonic-gate }
37797c478bd9Sstevel@tonic-gate 
37807c478bd9Sstevel@tonic-gate static enum fme_state
effects_test(struct fme * fmep,struct event * fault_event,unsigned long long at_latest_by,unsigned long long * pdelay)37817aec1d6eScindi effects_test(struct fme *fmep, struct event *fault_event,
37827aec1d6eScindi     unsigned long long at_latest_by, unsigned long long *pdelay)
37837c478bd9Sstevel@tonic-gate {
37847c478bd9Sstevel@tonic-gate 	struct event *error_event;
37857c478bd9Sstevel@tonic-gate 	enum fme_state return_value = FME_CREDIBLE;
37867aec1d6eScindi 	unsigned long long overall_delay = TIMEVAL_EVENTUALLY;
37877aec1d6eScindi 	unsigned long long my_delay;
37887c478bd9Sstevel@tonic-gate 
37897c478bd9Sstevel@tonic-gate 	stats_counter_bump(fmep->Ecallcount);
37907c478bd9Sstevel@tonic-gate 	indent_push("  E");
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, fault_event);
37947c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB, NULL);
37957c478bd9Sstevel@tonic-gate 
3796d96ce684Sstephh 	if (mark_arrows(fmep, fault_event, CREDIBLE_EFFECT, at_latest_by,
3797d96ce684Sstephh 	    &my_delay, 0) == WAIT_EFFECT) {
3798d96ce684Sstephh 		return_value = FME_WAIT;
3799d96ce684Sstephh 		if (overall_delay > my_delay)
3800d96ce684Sstephh 			overall_delay = my_delay;
3801d96ce684Sstephh 	}
38027c478bd9Sstevel@tonic-gate 	for (error_event = fmep->observations;
38037c478bd9Sstevel@tonic-gate 	    error_event; error_event = error_event->observations) {
38047c478bd9Sstevel@tonic-gate 		indent();
38057c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB|O_NONL, " ");
38067c478bd9Sstevel@tonic-gate 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, error_event);
38077aec1d6eScindi 		if (!(error_event->cached_state & CREDIBLE_EFFECT)) {
38087aec1d6eScindi 			if (error_event->cached_state &
38097aec1d6eScindi 			    (PARENT_WAIT|WAIT_EFFECT)) {
38107aec1d6eScindi 				out(O_ALTFP|O_VERB, " NOT YET triggered");
38117aec1d6eScindi 				continue;
38127aec1d6eScindi 			}
38137c478bd9Sstevel@tonic-gate 			return_value = FME_DISPROVED;
38147c478bd9Sstevel@tonic-gate 			out(O_ALTFP|O_VERB, " NOT triggered");
38157c478bd9Sstevel@tonic-gate 			break;
38167c478bd9Sstevel@tonic-gate 		} else {
38177c478bd9Sstevel@tonic-gate 			out(O_ALTFP|O_VERB, " triggered");
38187c478bd9Sstevel@tonic-gate 		}
38197c478bd9Sstevel@tonic-gate 	}
382000d0963fSdilpreet 	if (return_value == FME_DISPROVED) {
382100d0963fSdilpreet 		(void) mark_arrows(fmep, fault_event, 0, 0, NULL, 0);
382200d0963fSdilpreet 	} else {
382300d0963fSdilpreet 		fault_event->keep_in_tree = 1;
382400d0963fSdilpreet 		(void) mark_arrows(fmep, fault_event, 0, 0, NULL, 1);
382500d0963fSdilpreet 	}
38267c478bd9Sstevel@tonic-gate 
38277c478bd9Sstevel@tonic-gate 	indent();
38287aec1d6eScindi 	out(O_ALTFP|O_VERB|O_NONL, "<-EFFECTS %s ",
38297aec1d6eScindi 	    fme_state2str(return_value));
38307c478bd9Sstevel@tonic-gate 	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, fault_event);
38317c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB, NULL);
38327c478bd9Sstevel@tonic-gate 	indent_pop();
38337aec1d6eScindi 	if (return_value == FME_WAIT)
38347aec1d6eScindi 		*pdelay = overall_delay;
38357c478bd9Sstevel@tonic-gate 	return (return_value);
38367c478bd9Sstevel@tonic-gate }
38377c478bd9Sstevel@tonic-gate 
38387c478bd9Sstevel@tonic-gate static enum fme_state
requirements_test(struct fme * fmep,struct event * ep,unsigned long long at_latest_by,unsigned long long * pdelay)38397c478bd9Sstevel@tonic-gate requirements_test(struct fme *fmep, struct event *ep,
38407aec1d6eScindi     unsigned long long at_latest_by, unsigned long long *pdelay)
38417c478bd9Sstevel@tonic-gate {
38427c478bd9Sstevel@tonic-gate 	int waiting_events;
38437c478bd9Sstevel@tonic-gate 	int credible_events;
38447aec1d6eScindi 	int deferred_events;
38457c478bd9Sstevel@tonic-gate 	enum fme_state return_value = FME_CREDIBLE;
38467c478bd9Sstevel@tonic-gate 	unsigned long long overall_delay = TIMEVAL_EVENTUALLY;
38477c478bd9Sstevel@tonic-gate 	unsigned long long arrow_delay;
38487c478bd9Sstevel@tonic-gate 	unsigned long long my_delay;
38497c478bd9Sstevel@tonic-gate 	struct event *ep2;
38507c478bd9Sstevel@tonic-gate 	struct bubble *bp;
38517c478bd9Sstevel@tonic-gate 	struct arrowlist *ap;
38527c478bd9Sstevel@tonic-gate 
38537aec1d6eScindi 	if (ep->cached_state & REQMNTS_CREDIBLE) {
38547aec1d6eScindi 		indent();
38557aec1d6eScindi 		out(O_ALTFP|O_VERB|O_NONL, "  REQMNTS ALREADY CREDIBLE ");
38567aec1d6eScindi 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
38577aec1d6eScindi 		out(O_ALTFP|O_VERB, NULL);
38587aec1d6eScindi 		return (FME_CREDIBLE);
38597aec1d6eScindi 	}
38607aec1d6eScindi 	if (ep->cached_state & REQMNTS_DISPROVED) {
38617aec1d6eScindi 		indent();
38627aec1d6eScindi 		out(O_ALTFP|O_VERB|O_NONL, "  REQMNTS ALREADY DISPROVED ");
38637aec1d6eScindi 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
38647aec1d6eScindi 		out(O_ALTFP|O_VERB, NULL);
38657aec1d6eScindi 		return (FME_DISPROVED);
38667aec1d6eScindi 	}
38677aec1d6eScindi 	if (ep->cached_state & REQMNTS_WAIT) {
38687aec1d6eScindi 		indent();
38697aec1d6eScindi 		*pdelay = ep->cached_delay;
38707aec1d6eScindi 		out(O_ALTFP|O_VERB|O_NONL, "  REQMNTS ALREADY WAIT ");
38717aec1d6eScindi 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
38727aec1d6eScindi 		out(O_ALTFP|O_VERB|O_NONL, ", wait for: ");
38737aec1d6eScindi 		ptree_timeval(O_ALTFP|O_VERB|O_NONL, &at_latest_by);
38747aec1d6eScindi 		out(O_ALTFP|O_VERB, NULL);
38757aec1d6eScindi 		return (FME_WAIT);
38767aec1d6eScindi 	}
38777c478bd9Sstevel@tonic-gate 	stats_counter_bump(fmep->Rcallcount);
38787c478bd9Sstevel@tonic-gate 	indent_push("  R");
38797c478bd9Sstevel@tonic-gate 	indent();
38807c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB|O_NONL, "->");
38817c478bd9Sstevel@tonic-gate 	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
38827c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB|O_NONL, ", at latest by: ");
38837c478bd9Sstevel@tonic-gate 	ptree_timeval(O_ALTFP|O_VERB|O_NONL, &at_latest_by);
38847c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB, NULL);
38857c478bd9Sstevel@tonic-gate 
38867c478bd9Sstevel@tonic-gate 	if (ep->t == N_EREPORT) {
38877c478bd9Sstevel@tonic-gate 		if (ep->count == 0) {
38887c478bd9Sstevel@tonic-gate 			if (fmep->pull >= at_latest_by) {
38897c478bd9Sstevel@tonic-gate 				return_value = FME_DISPROVED;
38907c478bd9Sstevel@tonic-gate 			} else {
38917aec1d6eScindi 				ep->cached_delay = *pdelay = at_latest_by;
38927c478bd9Sstevel@tonic-gate 				return_value = FME_WAIT;
38937c478bd9Sstevel@tonic-gate 			}
38947c478bd9Sstevel@tonic-gate 		}
38957c478bd9Sstevel@tonic-gate 
38967c478bd9Sstevel@tonic-gate 		indent();
38977c478bd9Sstevel@tonic-gate 		switch (return_value) {
38987c478bd9Sstevel@tonic-gate 		case FME_CREDIBLE:
38997aec1d6eScindi 			ep->cached_state |= REQMNTS_CREDIBLE;
39007aec1d6eScindi 			out(O_ALTFP|O_VERB|O_NONL, "<-REQMNTS CREDIBLE ");
39017c478bd9Sstevel@tonic-gate 			itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
39027c478bd9Sstevel@tonic-gate 			break;
39037c478bd9Sstevel@tonic-gate 		case FME_DISPROVED:
39047aec1d6eScindi 			ep->cached_state |= REQMNTS_DISPROVED;
39057aec1d6eScindi 			out(O_ALTFP|O_VERB|O_NONL, "<-REQMNTS DISPROVED ");
39067c478bd9Sstevel@tonic-gate 			itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
39077c478bd9Sstevel@tonic-gate 			break;
39087c478bd9Sstevel@tonic-gate 		case FME_WAIT:
39097aec1d6eScindi 			ep->cached_state |= REQMNTS_WAIT;
39107aec1d6eScindi 			out(O_ALTFP|O_VERB|O_NONL, "<-REQMNTS WAIT ");
39117c478bd9Sstevel@tonic-gate 			itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
39127c478bd9Sstevel@tonic-gate 			out(O_ALTFP|O_VERB|O_NONL, " to ");
39137c478bd9Sstevel@tonic-gate 			ptree_timeval(O_ALTFP|O_VERB|O_NONL, &at_latest_by);
39147c478bd9Sstevel@tonic-gate 			break;
39157c478bd9Sstevel@tonic-gate 		default:
39167c478bd9Sstevel@tonic-gate 			out(O_DIE, "requirements_test: unexpected fme_state");
39177c478bd9Sstevel@tonic-gate 			break;
39187c478bd9Sstevel@tonic-gate 		}
39197c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB, NULL);
39207c478bd9Sstevel@tonic-gate 		indent_pop();
39217c478bd9Sstevel@tonic-gate 
39227c478bd9Sstevel@tonic-gate 		return (return_value);
39237c478bd9Sstevel@tonic-gate 	}
39247c478bd9Sstevel@tonic-gate 
39257c478bd9Sstevel@tonic-gate 	/* this event is not a report, descend the tree */
39267c478bd9Sstevel@tonic-gate 	for (bp = itree_next_bubble(ep, NULL); bp;
39277c478bd9Sstevel@tonic-gate 	    bp = itree_next_bubble(ep, bp)) {
39287aec1d6eScindi 		int n;
39297aec1d6eScindi 
39307c478bd9Sstevel@tonic-gate 		if (bp->t != B_FROM)
39317c478bd9Sstevel@tonic-gate 			continue;
39327c478bd9Sstevel@tonic-gate 
39337aec1d6eScindi 		n = bp->nork;
39347aec1d6eScindi 
39357aec1d6eScindi 		credible_events = 0;
39367aec1d6eScindi 		waiting_events = 0;
39377aec1d6eScindi 		deferred_events = 0;
39387aec1d6eScindi 		arrow_delay = TIMEVAL_EVENTUALLY;
39397aec1d6eScindi 		/*
39407aec1d6eScindi 		 * n is -1 for 'A' so adjust it.
39417aec1d6eScindi 		 * XXX just count up the arrows for now.
39427aec1d6eScindi 		 */
39437aec1d6eScindi 		if (n < 0) {
39447aec1d6eScindi 			n = 0;
39457aec1d6eScindi 			for (ap = itree_next_arrow(bp, NULL); ap;
39467aec1d6eScindi 			    ap = itree_next_arrow(bp, ap))
39477aec1d6eScindi 				n++;
39487aec1d6eScindi 			indent();
39497aec1d6eScindi 			out(O_ALTFP|O_VERB, " Bubble Counted N=%d", n);
39507aec1d6eScindi 		} else {
39517aec1d6eScindi 			indent();
39527aec1d6eScindi 			out(O_ALTFP|O_VERB, " Bubble N=%d", n);
39537aec1d6eScindi 		}
39547c478bd9Sstevel@tonic-gate 
39557aec1d6eScindi 		if (n == 0)
39567aec1d6eScindi 			continue;
39577aec1d6eScindi 		if (!(bp->mark & (BUBBLE_ELIDED|BUBBLE_OK))) {
39587c478bd9Sstevel@tonic-gate 			for (ap = itree_next_arrow(bp, NULL); ap;
39597c478bd9Sstevel@tonic-gate 			    ap = itree_next_arrow(bp, ap)) {
39607c478bd9Sstevel@tonic-gate 				ep2 = ap->arrowp->head->myevent;
39617aec1d6eScindi 				platform_set_payloadnvp(ep2->nvp);
3962b7d3956bSstephh 				(void) checkconstraints(fmep, ap->arrowp);
39638cffa125SStephen Hanson 				if (!ap->arrowp->forever_false) {
39647aec1d6eScindi 					/*
3965b7d3956bSstephh 					 * if all arrows are invalidated by the
39667aec1d6eScindi 					 * constraints, then we should elide the
39677aec1d6eScindi 					 * whole bubble to be consistant with
39687aec1d6eScindi 					 * the tree creation time behaviour
39697aec1d6eScindi 					 */
3970b7d3956bSstephh 					bp->mark |= BUBBLE_OK;
39717aec1d6eScindi 					platform_set_payloadnvp(NULL);
39727c478bd9Sstevel@tonic-gate 					break;
39737aec1d6eScindi 				}
39747aec1d6eScindi 				platform_set_payloadnvp(NULL);
39757aec1d6eScindi 			}
39767aec1d6eScindi 		}
39777aec1d6eScindi 		for (ap = itree_next_arrow(bp, NULL); ap;
39787aec1d6eScindi 		    ap = itree_next_arrow(bp, ap)) {
39797aec1d6eScindi 			ep2 = ap->arrowp->head->myevent;
39807aec1d6eScindi 			if (n <= credible_events)
39817aec1d6eScindi 				break;
39827c478bd9Sstevel@tonic-gate 
39837aec1d6eScindi 			ap->arrowp->mark |= REQMNTS_COUNTER;
39847aec1d6eScindi 			if (triggered(fmep, ep2, REQMNTS_COUNTER))
39857aec1d6eScindi 				/* XXX adding max timevals! */
39867aec1d6eScindi 				switch (requirements_test(fmep, ep2,
39877aec1d6eScindi 				    at_latest_by + ap->arrowp->maxdelay,
39887aec1d6eScindi 				    &my_delay)) {
39897aec1d6eScindi 				case FME_DEFERRED:
39907aec1d6eScindi 					deferred_events++;
39917aec1d6eScindi 					break;
39927aec1d6eScindi 				case FME_CREDIBLE:
39937c478bd9Sstevel@tonic-gate 					credible_events++;
39947aec1d6eScindi 					break;
39957aec1d6eScindi 				case FME_DISPROVED:
39967aec1d6eScindi 					break;
39977aec1d6eScindi 				case FME_WAIT:
39987aec1d6eScindi 					if (my_delay < arrow_delay)
39997aec1d6eScindi 						arrow_delay = my_delay;
40007aec1d6eScindi 					waiting_events++;
40017aec1d6eScindi 					break;
40027aec1d6eScindi 				default:
40037aec1d6eScindi 					out(O_DIE,
40047aec1d6eScindi 					"Bug in requirements_test.");
40057aec1d6eScindi 				}
40067aec1d6eScindi 			else
40077aec1d6eScindi 				deferred_events++;
40087aec1d6eScindi 		}
4009b7d3956bSstephh 		if (!(bp->mark & BUBBLE_OK) && waiting_events == 0) {
4010b7d3956bSstephh 			bp->mark |= BUBBLE_ELIDED;
4011b7d3956bSstephh 			continue;
4012b7d3956bSstephh 		}
40137aec1d6eScindi 		indent();
40147aec1d6eScindi 		out(O_ALTFP|O_VERB, " Credible: %d Waiting %d",
40157aec1d6eScindi 		    credible_events + deferred_events, waiting_events);
40167aec1d6eScindi 		if (credible_events + deferred_events + waiting_events < n) {
40177aec1d6eScindi 			/* Can never meet requirements */
40187aec1d6eScindi 			ep->cached_state |= REQMNTS_DISPROVED;
40197c478bd9Sstevel@tonic-gate 			indent();
40207aec1d6eScindi 			out(O_ALTFP|O_VERB|O_NONL, "<-REQMNTS DISPROVED ");
40217c478bd9Sstevel@tonic-gate 			itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
40227c478bd9Sstevel@tonic-gate 			out(O_ALTFP|O_VERB, NULL);
40237aec1d6eScindi 			indent_pop();
40247aec1d6eScindi 			return (FME_DISPROVED);
40257aec1d6eScindi 		}
40267aec1d6eScindi 		if (credible_events + deferred_events < n) {
40277aec1d6eScindi 			/* will have to wait */
40287aec1d6eScindi 			/* wait time is shortest known */
40297aec1d6eScindi 			if (arrow_delay < overall_delay)
40307aec1d6eScindi 				overall_delay = arrow_delay;
40317aec1d6eScindi 			return_value = FME_WAIT;
40327aec1d6eScindi 		} else if (credible_events < n) {
40337aec1d6eScindi 			if (return_value != FME_WAIT)
40347aec1d6eScindi 				return_value = FME_DEFERRED;
40357c478bd9Sstevel@tonic-gate 		}
40367c478bd9Sstevel@tonic-gate 	}
40377c478bd9Sstevel@tonic-gate 
40387c478bd9Sstevel@tonic-gate 	/*
40397aec1d6eScindi 	 * don't mark as FME_DEFERRED. If this event isn't reached by another
40407aec1d6eScindi 	 * path, then this will be considered FME_CREDIBLE. But if it is
40417aec1d6eScindi 	 * reached by a different path so the K-count is met, then might
40427aec1d6eScindi 	 * get overridden by FME_WAIT or FME_DISPROVED.
40437c478bd9Sstevel@tonic-gate 	 */
40447aec1d6eScindi 	if (return_value == FME_WAIT) {
40457aec1d6eScindi 		ep->cached_state |= REQMNTS_WAIT;
40467aec1d6eScindi 		ep->cached_delay = *pdelay = overall_delay;
40477aec1d6eScindi 	} else if (return_value == FME_CREDIBLE) {
40487aec1d6eScindi 		ep->cached_state |= REQMNTS_CREDIBLE;
40497c478bd9Sstevel@tonic-gate 	}
40507c478bd9Sstevel@tonic-gate 	indent();
40517aec1d6eScindi 	out(O_ALTFP|O_VERB|O_NONL, "<-REQMNTS %s ",
40527aec1d6eScindi 	    fme_state2str(return_value));
40537c478bd9Sstevel@tonic-gate 	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
40547c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB, NULL);
40557c478bd9Sstevel@tonic-gate 	indent_pop();
40567c478bd9Sstevel@tonic-gate 	return (return_value);
40577c478bd9Sstevel@tonic-gate }
40587c478bd9Sstevel@tonic-gate 
40597c478bd9Sstevel@tonic-gate static enum fme_state
causes_test(struct fme * fmep,struct event * ep,unsigned long long at_latest_by,unsigned long long * pdelay)40607c478bd9Sstevel@tonic-gate causes_test(struct fme *fmep, struct event *ep,
40617c478bd9Sstevel@tonic-gate     unsigned long long at_latest_by, unsigned long long *pdelay)
40627c478bd9Sstevel@tonic-gate {
40637c478bd9Sstevel@tonic-gate 	unsigned long long overall_delay = TIMEVAL_EVENTUALLY;
40647c478bd9Sstevel@tonic-gate 	unsigned long long my_delay;
40657c478bd9Sstevel@tonic-gate 	int credible_results = 0;
40667c478bd9Sstevel@tonic-gate 	int waiting_results = 0;
40677c478bd9Sstevel@tonic-gate 	enum fme_state fstate;
40687c478bd9Sstevel@tonic-gate 	struct event *tail_event;
40697c478bd9Sstevel@tonic-gate 	struct bubble *bp;
40707c478bd9Sstevel@tonic-gate 	struct arrowlist *ap;
40717c478bd9Sstevel@tonic-gate 	int k = 1;
40727c478bd9Sstevel@tonic-gate 
40737c478bd9Sstevel@tonic-gate 	stats_counter_bump(fmep->Ccallcount);
40747c478bd9Sstevel@tonic-gate 	indent_push("  C");
40757c478bd9Sstevel@tonic-gate 	indent();
40767c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB|O_NONL, "->");
40777c478bd9Sstevel@tonic-gate 	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
40787c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB, NULL);
40797c478bd9Sstevel@tonic-gate 
40807c478bd9Sstevel@tonic-gate 	for (bp = itree_next_bubble(ep, NULL); bp;
40817c478bd9Sstevel@tonic-gate 	    bp = itree_next_bubble(ep, bp)) {
40827c478bd9Sstevel@tonic-gate 		if (bp->t != B_TO)
40837c478bd9Sstevel@tonic-gate 			continue;
40847c478bd9Sstevel@tonic-gate 		k = bp->nork;	/* remember the K value */
40857c478bd9Sstevel@tonic-gate 		for (ap = itree_next_arrow(bp, NULL); ap;
40867c478bd9Sstevel@tonic-gate 		    ap = itree_next_arrow(bp, ap)) {
40877c478bd9Sstevel@tonic-gate 			int do_not_follow = 0;
40887aec1d6eScindi 
40897aec1d6eScindi 			/*
40907aec1d6eScindi 			 * if we get to the same event multiple times
40917aec1d6eScindi 			 * only worry about the first one.
40927aec1d6eScindi 			 */
40937aec1d6eScindi 			if (ap->arrowp->tail->myevent->cached_state &
40947aec1d6eScindi 			    CAUSES_TESTED) {
40957aec1d6eScindi 				indent();
40967aec1d6eScindi 				out(O_ALTFP|O_VERB|O_NONL,
40977aec1d6eScindi 				    "  causes test already run for ");
40987aec1d6eScindi 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL,
40997aec1d6eScindi 				    ap->arrowp->tail->myevent);
41007aec1d6eScindi 				out(O_ALTFP|O_VERB, NULL);
41017aec1d6eScindi 				continue;
41027aec1d6eScindi 			}
41037aec1d6eScindi 
41047c478bd9Sstevel@tonic-gate 			/*
41057c478bd9Sstevel@tonic-gate 			 * see if false constraint prevents us
41067c478bd9Sstevel@tonic-gate 			 * from traversing this arrow
41077c478bd9Sstevel@tonic-gate 			 */
41087c478bd9Sstevel@tonic-gate 			platform_set_payloadnvp(ep->nvp);
410900d0963fSdilpreet 			if (checkconstraints(fmep, ap->arrowp) == 0)
41107aec1d6eScindi 				do_not_follow = 1;
41117c478bd9Sstevel@tonic-gate 			platform_set_payloadnvp(NULL);
41127c478bd9Sstevel@tonic-gate 			if (do_not_follow) {
41137c478bd9Sstevel@tonic-gate 				indent();
41147c478bd9Sstevel@tonic-gate 				out(O_ALTFP|O_VERB|O_NONL,
41157c478bd9Sstevel@tonic-gate 				    "  False arrow from ");
41167c478bd9Sstevel@tonic-gate 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL,
41177c478bd9Sstevel@tonic-gate 				    ap->arrowp->tail->myevent);
41187c478bd9Sstevel@tonic-gate 				out(O_ALTFP|O_VERB, NULL);
41197c478bd9Sstevel@tonic-gate 				continue;
41207c478bd9Sstevel@tonic-gate 			}
41217c478bd9Sstevel@tonic-gate 
41227aec1d6eScindi 			ap->arrowp->tail->myevent->cached_state |=
41237aec1d6eScindi 			    CAUSES_TESTED;
41247aec1d6eScindi 			tail_event = ap->arrowp->tail->myevent;
41257aec1d6eScindi 			fstate = hypothesise(fmep, tail_event, at_latest_by,
41267aec1d6eScindi 			    &my_delay);
41277c478bd9Sstevel@tonic-gate 
41287c478bd9Sstevel@tonic-gate 			switch (fstate) {
41297c478bd9Sstevel@tonic-gate 			case FME_WAIT:
41307c478bd9Sstevel@tonic-gate 				if (my_delay < overall_delay)
41317c478bd9Sstevel@tonic-gate 					overall_delay = my_delay;
41327c478bd9Sstevel@tonic-gate 				waiting_results++;
41337c478bd9Sstevel@tonic-gate 				break;
41347c478bd9Sstevel@tonic-gate 			case FME_CREDIBLE:
41357c478bd9Sstevel@tonic-gate 				credible_results++;
41367c478bd9Sstevel@tonic-gate 				break;
41377c478bd9Sstevel@tonic-gate 			case FME_DISPROVED:
41387c478bd9Sstevel@tonic-gate 				break;
41397c478bd9Sstevel@tonic-gate 			default:
41407c478bd9Sstevel@tonic-gate 				out(O_DIE, "Bug in causes_test");
41417c478bd9Sstevel@tonic-gate 			}
41427c478bd9Sstevel@tonic-gate 		}
41437c478bd9Sstevel@tonic-gate 	}
41447c478bd9Sstevel@tonic-gate 	/* compare against K */
41457c478bd9Sstevel@tonic-gate 	if (credible_results + waiting_results < k) {
41467c478bd9Sstevel@tonic-gate 		indent();
41477aec1d6eScindi 		out(O_ALTFP|O_VERB|O_NONL, "<-CAUSES DISPROVED ");
41487c478bd9Sstevel@tonic-gate 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
41497c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB, NULL);
41507c478bd9Sstevel@tonic-gate 		indent_pop();
41517c478bd9Sstevel@tonic-gate 		return (FME_DISPROVED);
41527c478bd9Sstevel@tonic-gate 	}
41537c478bd9Sstevel@tonic-gate 	if (waiting_results != 0) {
41547c478bd9Sstevel@tonic-gate 		*pdelay = overall_delay;
41557c478bd9Sstevel@tonic-gate 		indent();
41567aec1d6eScindi 		out(O_ALTFP|O_VERB|O_NONL, "<-CAUSES WAIT ");
41577c478bd9Sstevel@tonic-gate 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
41587c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB|O_NONL, " to ");
41597c478bd9Sstevel@tonic-gate 		ptree_timeval(O_ALTFP|O_VERB|O_NONL, &at_latest_by);
41607c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB, NULL);
41617c478bd9Sstevel@tonic-gate 		indent_pop();
41627c478bd9Sstevel@tonic-gate 		return (FME_WAIT);
41637c478bd9Sstevel@tonic-gate 	}
41647c478bd9Sstevel@tonic-gate 	indent();
41657aec1d6eScindi 	out(O_ALTFP|O_VERB|O_NONL, "<-CAUSES CREDIBLE ");
41667c478bd9Sstevel@tonic-gate 	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
41677c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB, NULL);
41687c478bd9Sstevel@tonic-gate 	indent_pop();
41697c478bd9Sstevel@tonic-gate 	return (FME_CREDIBLE);
41707c478bd9Sstevel@tonic-gate }
41717c478bd9Sstevel@tonic-gate 
41727c478bd9Sstevel@tonic-gate static enum fme_state
hypothesise(struct fme * fmep,struct event * ep,unsigned long long at_latest_by,unsigned long long * pdelay)41737c478bd9Sstevel@tonic-gate hypothesise(struct fme *fmep, struct event *ep,
4174a3f6a2a4SRob Johnston     unsigned long long at_latest_by, unsigned long long *pdelay)
41757c478bd9Sstevel@tonic-gate {
41767c478bd9Sstevel@tonic-gate 	enum fme_state rtr, otr;
41777c478bd9Sstevel@tonic-gate 	unsigned long long my_delay;
41787c478bd9Sstevel@tonic-gate 	unsigned long long overall_delay = TIMEVAL_EVENTUALLY;
41797c478bd9Sstevel@tonic-gate 
41807c478bd9Sstevel@tonic-gate 	stats_counter_bump(fmep->Hcallcount);
41817c478bd9Sstevel@tonic-gate 	indent_push("  H");
41827c478bd9Sstevel@tonic-gate 	indent();
41837c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB|O_NONL, "->");
41847c478bd9Sstevel@tonic-gate 	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
41857c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB|O_NONL, ", at latest by: ");
41867c478bd9Sstevel@tonic-gate 	ptree_timeval(O_ALTFP|O_VERB|O_NONL, &at_latest_by);
41877c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB, NULL);
41887c478bd9Sstevel@tonic-gate 
41897aec1d6eScindi 	rtr = requirements_test(fmep, ep, at_latest_by, &my_delay);
41907c478bd9Sstevel@tonic-gate 	if ((rtr == FME_WAIT) && (my_delay < overall_delay))
41917c478bd9Sstevel@tonic-gate 		overall_delay = my_delay;
41927c478bd9Sstevel@tonic-gate 	if (rtr != FME_DISPROVED) {
41937c478bd9Sstevel@tonic-gate 		if (is_problem(ep->t)) {
41947aec1d6eScindi 			otr = effects_test(fmep, ep, at_latest_by, &my_delay);
41957c478bd9Sstevel@tonic-gate 			if (otr != FME_DISPROVED) {
4196b5016cbbSstephh 				if (fmep->peek == 0 && ep->is_suspect == 0) {
41977c478bd9Sstevel@tonic-gate 					ep->suspects = fmep->suspects;
4198b5016cbbSstephh 					ep->is_suspect = 1;
41997c478bd9Sstevel@tonic-gate 					fmep->suspects = ep;
42007c478bd9Sstevel@tonic-gate 					fmep->nsuspects++;
42017c478bd9Sstevel@tonic-gate 				}
42027c478bd9Sstevel@tonic-gate 			}
42037c478bd9Sstevel@tonic-gate 		} else
42047c478bd9Sstevel@tonic-gate 			otr = causes_test(fmep, ep, at_latest_by, &my_delay);
42057c478bd9Sstevel@tonic-gate 		if ((otr == FME_WAIT) && (my_delay < overall_delay))
42067c478bd9Sstevel@tonic-gate 			overall_delay = my_delay;
42077c478bd9Sstevel@tonic-gate 		if ((otr != FME_DISPROVED) &&
42087c478bd9Sstevel@tonic-gate 		    ((rtr == FME_WAIT) || (otr == FME_WAIT)))
42097c478bd9Sstevel@tonic-gate 			*pdelay = overall_delay;
42107c478bd9Sstevel@tonic-gate 	}
42117c478bd9Sstevel@tonic-gate 	if (rtr == FME_DISPROVED) {
42127c478bd9Sstevel@tonic-gate 		indent();
42137c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB|O_NONL, "<-DISPROVED ");
42147c478bd9Sstevel@tonic-gate 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
42157c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB, " (doesn't meet requirements)");
42167c478bd9Sstevel@tonic-gate 		indent_pop();
42177c478bd9Sstevel@tonic-gate 		return (FME_DISPROVED);
42187c478bd9Sstevel@tonic-gate 	}
42197c478bd9Sstevel@tonic-gate 	if ((otr == FME_DISPROVED) && is_problem(ep->t)) {
42207c478bd9Sstevel@tonic-gate 		indent();
42217c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB|O_NONL, "<-DISPROVED ");
42227c478bd9Sstevel@tonic-gate 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
42237c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB, " (doesn't explain all reports)");
42247c478bd9Sstevel@tonic-gate 		indent_pop();
42257c478bd9Sstevel@tonic-gate 		return (FME_DISPROVED);
42267c478bd9Sstevel@tonic-gate 	}
42277c478bd9Sstevel@tonic-gate 	if (otr == FME_DISPROVED) {
42287c478bd9Sstevel@tonic-gate 		indent();
42297c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB|O_NONL, "<-DISPROVED ");
42307c478bd9Sstevel@tonic-gate 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
42317c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB, " (causes are not credible)");
42327c478bd9Sstevel@tonic-gate 		indent_pop();
42337c478bd9Sstevel@tonic-gate 		return (FME_DISPROVED);
42347c478bd9Sstevel@tonic-gate 	}
42357c478bd9Sstevel@tonic-gate 	if ((rtr == FME_WAIT) || (otr == FME_WAIT)) {
42367c478bd9Sstevel@tonic-gate 		indent();
42377c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB|O_NONL, "<-WAIT ");
42387c478bd9Sstevel@tonic-gate 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
42397c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB|O_NONL, " to ");
42407c478bd9Sstevel@tonic-gate 		ptree_timeval(O_ALTFP|O_VERB|O_NONL, &overall_delay);
42417c478bd9Sstevel@tonic-gate 		out(O_ALTFP|O_VERB, NULL);
42427c478bd9Sstevel@tonic-gate 		indent_pop();
42437c478bd9Sstevel@tonic-gate 		return (FME_WAIT);
42447c478bd9Sstevel@tonic-gate 	}
42457c478bd9Sstevel@tonic-gate 	indent();
42467c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB|O_NONL, "<-CREDIBLE ");
42477c478bd9Sstevel@tonic-gate 	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
42487c478bd9Sstevel@tonic-gate 	out(O_ALTFP|O_VERB, NULL);
42497c478bd9Sstevel@tonic-gate 	indent_pop();
42507c478bd9Sstevel@tonic-gate 	return (FME_CREDIBLE);
42517c478bd9Sstevel@tonic-gate }
42527aec1d6eScindi 
42537aec1d6eScindi /*
42547aec1d6eScindi  * fme_istat_load -- reconstitute any persistent istats
42557aec1d6eScindi  */
42567aec1d6eScindi void
fme_istat_load(fmd_hdl_t * hdl)42577aec1d6eScindi fme_istat_load(fmd_hdl_t *hdl)
42587aec1d6eScindi {
42597aec1d6eScindi 	int sz;
42607aec1d6eScindi 	char *sbuf;
42617aec1d6eScindi 	char *ptr;
42627aec1d6eScindi 
42637aec1d6eScindi 	if ((sz = fmd_buf_size(hdl, NULL, WOBUF_ISTATS)) == 0) {
42647aec1d6eScindi 		out(O_ALTFP, "fme_istat_load: No stats");
42657aec1d6eScindi 		return;
42667aec1d6eScindi 	}
42677aec1d6eScindi 
42687aec1d6eScindi 	sbuf = alloca(sz);
42697aec1d6eScindi 
42707aec1d6eScindi 	fmd_buf_read(hdl, NULL, WOBUF_ISTATS, sbuf, sz);
42717aec1d6eScindi 
42727aec1d6eScindi 	/*
42737aec1d6eScindi 	 * pick apart the serialized stats
42747aec1d6eScindi 	 *
42757aec1d6eScindi 	 * format is:
42767aec1d6eScindi 	 *	<class-name>, '@', <path>, '\0', <value>, '\0'
42777aec1d6eScindi 	 * for example:
42787aec1d6eScindi 	 *	"stat.first@stat0/path0\02\0stat.second@stat0/path1\023\0"
42797aec1d6eScindi 	 *
42807aec1d6eScindi 	 * since this is parsing our own serialized data, any parsing issues
42817aec1d6eScindi 	 * are fatal, so we check for them all with ASSERT() below.
42827aec1d6eScindi 	 */
42837aec1d6eScindi 	ptr = sbuf;
42847aec1d6eScindi 	while (ptr < &sbuf[sz]) {
42857aec1d6eScindi 		char *sepptr;
42867aec1d6eScindi 		struct node *np;
42877aec1d6eScindi 		int val;
42887aec1d6eScindi 
42897aec1d6eScindi 		sepptr = strchr(ptr, '@');
42907aec1d6eScindi 		ASSERT(sepptr != NULL);
42917aec1d6eScindi 		*sepptr = '\0';
42927aec1d6eScindi 
42937aec1d6eScindi 		/* construct the event */
42947aec1d6eScindi 		np = newnode(T_EVENT, NULL, 0);
42957aec1d6eScindi 		np->u.event.ename = newnode(T_NAME, NULL, 0);
42967aec1d6eScindi 		np->u.event.ename->u.name.t = N_STAT;
42977aec1d6eScindi 		np->u.event.ename->u.name.s = stable(ptr);
42987aec1d6eScindi 		np->u.event.ename->u.name.it = IT_ENAME;
42997aec1d6eScindi 		np->u.event.ename->u.name.last = np->u.event.ename;
43007aec1d6eScindi 
43017aec1d6eScindi 		ptr = sepptr + 1;
43027aec1d6eScindi 		ASSERT(ptr < &sbuf[sz]);
43037aec1d6eScindi 		ptr += strlen(ptr);
43047aec1d6eScindi 		ptr++;	/* move past the '\0' separating path from value */
43057aec1d6eScindi 		ASSERT(ptr < &sbuf[sz]);
43067aec1d6eScindi 		ASSERT(isdigit(*ptr));
43077aec1d6eScindi 		val = atoi(ptr);
43087aec1d6eScindi 		ASSERT(val > 0);
43097aec1d6eScindi 		ptr += strlen(ptr);
43107aec1d6eScindi 		ptr++;	/* move past the final '\0' for this entry */
43117aec1d6eScindi 
43127aec1d6eScindi 		np->u.event.epname = pathstring2epnamenp(sepptr + 1);
43137aec1d6eScindi 		ASSERT(np->u.event.epname != NULL);
43147aec1d6eScindi 
43157aec1d6eScindi 		istat_bump(np, val);
43167aec1d6eScindi 		tree_free(np);
43177aec1d6eScindi 	}
43187aec1d6eScindi 
43197aec1d6eScindi 	istat_save();
43207aec1d6eScindi }
4321