xref: /titanic_52/usr/src/cmd/fm/modules/common/eversholt/fme.c (revision 967072a15e834441c4b221b17b3859169a2c9af2)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  *
26  * fme.c -- fault management exercise module
27  *
28  * this module provides the simulated fault management exercise.
29  */
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <strings.h>
37 #include <ctype.h>
38 #include <alloca.h>
39 #include <libnvpair.h>
40 #include <sys/fm/protocol.h>
41 #include <fm/fmd_api.h>
42 #include "alloc.h"
43 #include "out.h"
44 #include "stats.h"
45 #include "stable.h"
46 #include "literals.h"
47 #include "lut.h"
48 #include "tree.h"
49 #include "ptree.h"
50 #include "itree.h"
51 #include "ipath.h"
52 #include "fme.h"
53 #include "evnv.h"
54 #include "eval.h"
55 #include "config.h"
56 #include "platform.h"
57 #include "esclex.h"
58 
59 /* imported from eft.c... */
60 extern char *Autoclose;
61 extern int Dupclose;
62 extern hrtime_t Hesitate;
63 extern char *Serd_Override;
64 extern nv_alloc_t Eft_nv_hdl;
65 extern int Max_fme;
66 extern fmd_hdl_t *Hdl;
67 
68 static int Istat_need_save;
69 static int Serd_need_save;
70 void istat_save(void);
71 void serd_save(void);
72 
73 /* fme under construction is global so we can free it on module abort */
74 static struct fme *Nfmep;
75 
76 static const char *Undiag_reason;
77 
78 static int Nextid = 0;
79 
80 static int Open_fme_count = 0;	/* Count of open FMEs */
81 
82 /* list of fault management exercises underway */
83 static struct fme {
84 	struct fme *next;		/* next exercise */
85 	unsigned long long ull;		/* time when fme was created */
86 	int id;				/* FME id */
87 	struct config *config;		/* cooked configuration data */
88 	struct lut *eventtree;		/* propagation tree for this FME */
89 	/*
90 	 * The initial error report that created this FME is kept in
91 	 * two forms.  e0 points to the instance tree node and is used
92 	 * by fme_eval() as the starting point for the inference
93 	 * algorithm.  e0r is the event handle FMD passed to us when
94 	 * the ereport first arrived and is used when setting timers,
95 	 * which are always relative to the time of this initial
96 	 * report.
97 	 */
98 	struct event *e0;
99 	fmd_event_t *e0r;
100 
101 	id_t    timer;			/* for setting an fmd time-out */
102 
103 	struct event *ecurrent;		/* ereport under consideration */
104 	struct event *suspects;		/* current suspect list */
105 	struct event *psuspects;	/* previous suspect list */
106 	int nsuspects;			/* count of suspects */
107 	int nonfault;			/* zero if all suspects T_FAULT */
108 	int posted_suspects;		/* true if we've posted a diagnosis */
109 	int uniqobs;			/* number of unique events observed */
110 	int peek;			/* just peeking, don't track suspects */
111 	int overflow;			/* true if overflow FME */
112 	enum fme_state {
113 		FME_NOTHING = 5000,	/* not evaluated yet */
114 		FME_WAIT,		/* need to wait for more info */
115 		FME_CREDIBLE,		/* suspect list is credible */
116 		FME_DISPROVED,		/* no valid suspects found */
117 		FME_DEFERRED		/* don't know yet (k-count not met) */
118 	} state;
119 
120 	unsigned long long pull;	/* time passed since created */
121 	unsigned long long wull;	/* wait until this time for re-eval */
122 	struct event *observations;	/* observation list */
123 	struct lut *globals;		/* values of global variables */
124 	/* fmd interfacing */
125 	fmd_hdl_t *hdl;			/* handle for talking with fmd */
126 	fmd_case_t *fmcase;		/* what fmd 'case' we associate with */
127 	/* stats */
128 	struct stats *Rcount;
129 	struct stats *Hcallcount;
130 	struct stats *Rcallcount;
131 	struct stats *Ccallcount;
132 	struct stats *Ecallcount;
133 	struct stats *Tcallcount;
134 	struct stats *Marrowcount;
135 	struct stats *diags;
136 } *FMElist, *EFMElist, *ClosedFMEs;
137 
138 static struct case_list {
139 	fmd_case_t *fmcase;
140 	struct case_list *next;
141 } *Undiagablecaselist;
142 
143 static void fme_eval(struct fme *fmep, fmd_event_t *ffep);
144 static enum fme_state hypothesise(struct fme *fmep, struct event *ep,
145 	unsigned long long at_latest_by, unsigned long long *pdelay);
146 static struct node *eventprop_lookup(struct event *ep, const char *propname);
147 static struct node *pathstring2epnamenp(char *path);
148 static void publish_undiagnosable(fmd_hdl_t *hdl, fmd_event_t *ffep,
149 	fmd_case_t *fmcase);
150 static void restore_suspects(struct fme *fmep);
151 static void save_suspects(struct fme *fmep);
152 static void destroy_fme(struct fme *f);
153 static void fme_receive_report(fmd_hdl_t *hdl, fmd_event_t *ffep,
154     const char *eventstring, const struct ipath *ipp, nvlist_t *nvl);
155 static void istat_counter_reset_cb(struct istat_entry *entp,
156     struct stats *statp, const struct ipath *ipp);
157 static void istat_counter_topo_chg_cb(struct istat_entry *entp,
158     struct stats *statp, void *unused);
159 static void serd_reset_cb(struct serd_entry *entp, void *unused,
160     const struct ipath *ipp);
161 static void serd_topo_chg_cb(struct serd_entry *entp, void *unused,
162     void *unused2);
163 static void destroy_fme_bufs(struct fme *fp);
164 
165 static struct fme *
166 alloc_fme(void)
167 {
168 	struct fme *fmep;
169 
170 	fmep = MALLOC(sizeof (*fmep));
171 	bzero(fmep, sizeof (*fmep));
172 	return (fmep);
173 }
174 
175 /*
176  * fme_ready -- called when all initialization of the FME (except for
177  *	stats) has completed successfully.  Adds the fme to global lists
178  *	and establishes its stats.
179  */
180 static struct fme *
181 fme_ready(struct fme *fmep)
182 {
183 	char nbuf[100];
184 
185 	Nfmep = NULL;	/* don't need to free this on module abort now */
186 
187 	if (EFMElist) {
188 		EFMElist->next = fmep;
189 		EFMElist = fmep;
190 	} else
191 		FMElist = EFMElist = fmep;
192 
193 	(void) sprintf(nbuf, "fme%d.Rcount", fmep->id);
194 	fmep->Rcount = stats_new_counter(nbuf, "ereports received", 0);
195 	(void) sprintf(nbuf, "fme%d.Hcall", fmep->id);
196 	fmep->Hcallcount = stats_new_counter(nbuf, "calls to hypothesise()", 1);
197 	(void) sprintf(nbuf, "fme%d.Rcall", fmep->id);
198 	fmep->Rcallcount = stats_new_counter(nbuf,
199 	    "calls to requirements_test()", 1);
200 	(void) sprintf(nbuf, "fme%d.Ccall", fmep->id);
201 	fmep->Ccallcount = stats_new_counter(nbuf, "calls to causes_test()", 1);
202 	(void) sprintf(nbuf, "fme%d.Ecall", fmep->id);
203 	fmep->Ecallcount =
204 	    stats_new_counter(nbuf, "calls to effects_test()", 1);
205 	(void) sprintf(nbuf, "fme%d.Tcall", fmep->id);
206 	fmep->Tcallcount = stats_new_counter(nbuf, "calls to triggered()", 1);
207 	(void) sprintf(nbuf, "fme%d.Marrow", fmep->id);
208 	fmep->Marrowcount = stats_new_counter(nbuf,
209 	    "arrows marked by mark_arrows()", 1);
210 	(void) sprintf(nbuf, "fme%d.diags", fmep->id);
211 	fmep->diags = stats_new_counter(nbuf, "suspect lists diagnosed", 0);
212 
213 	out(O_ALTFP|O_VERB2, "newfme: config snapshot contains...");
214 	config_print(O_ALTFP|O_VERB2, fmep->config);
215 
216 	return (fmep);
217 }
218 
219 extern void ipath_dummy_lut(struct arrow *);
220 extern struct lut *itree_create_dummy(const char *, const struct ipath *);
221 
222 /* ARGSUSED */
223 static void
224 set_needed_arrows(struct event *ep, struct event *ep2, struct fme *fmep)
225 {
226 	struct bubble *bp;
227 	struct arrowlist *ap;
228 
229 	for (bp = itree_next_bubble(ep, NULL); bp;
230 	    bp = itree_next_bubble(ep, bp)) {
231 		if (bp->t != B_FROM)
232 			continue;
233 		for (ap = itree_next_arrow(bp, NULL); ap;
234 		    ap = itree_next_arrow(bp, ap)) {
235 			ap->arrowp->pnode->u.arrow.needed = 1;
236 			ipath_dummy_lut(ap->arrowp);
237 		}
238 	}
239 }
240 
241 /* ARGSUSED */
242 static void
243 unset_needed_arrows(struct event *ep, struct event *ep2, struct fme *fmep)
244 {
245 	struct bubble *bp;
246 	struct arrowlist *ap;
247 
248 	for (bp = itree_next_bubble(ep, NULL); bp;
249 	    bp = itree_next_bubble(ep, bp)) {
250 		if (bp->t != B_FROM)
251 			continue;
252 		for (ap = itree_next_arrow(bp, NULL); ap;
253 		    ap = itree_next_arrow(bp, ap))
254 			ap->arrowp->pnode->u.arrow.needed = 0;
255 	}
256 }
257 
258 static void globals_destructor(void *left, void *right, void *arg);
259 static void clear_arrows(struct event *ep, struct event *ep2, struct fme *fmep);
260 
261 static void
262 prune_propagations(const char *e0class, const struct ipath *e0ipp)
263 {
264 	char nbuf[100];
265 	unsigned long long my_delay = TIMEVAL_EVENTUALLY;
266 	extern struct lut *Usednames;
267 
268 	Nfmep = alloc_fme();
269 	Nfmep->id = Nextid;
270 	Nfmep->state = FME_NOTHING;
271 	Nfmep->eventtree = itree_create_dummy(e0class, e0ipp);
272 	if ((Nfmep->e0 =
273 	    itree_lookup(Nfmep->eventtree, e0class, e0ipp)) == NULL) {
274 		out(O_ALTFP, "prune_propagations: e0 not in instance tree");
275 		itree_free(Nfmep->eventtree);
276 		FREE(Nfmep);
277 		Nfmep = NULL;
278 		return;
279 	}
280 	Nfmep->ecurrent = Nfmep->observations = Nfmep->e0;
281 	Nfmep->e0->count++;
282 
283 	(void) sprintf(nbuf, "fme%d.Rcount", Nfmep->id);
284 	Nfmep->Rcount = stats_new_counter(nbuf, "ereports received", 0);
285 	(void) sprintf(nbuf, "fme%d.Hcall", Nfmep->id);
286 	Nfmep->Hcallcount =
287 	    stats_new_counter(nbuf, "calls to hypothesise()", 1);
288 	(void) sprintf(nbuf, "fme%d.Rcall", Nfmep->id);
289 	Nfmep->Rcallcount = stats_new_counter(nbuf,
290 	    "calls to requirements_test()", 1);
291 	(void) sprintf(nbuf, "fme%d.Ccall", Nfmep->id);
292 	Nfmep->Ccallcount =
293 	    stats_new_counter(nbuf, "calls to causes_test()", 1);
294 	(void) sprintf(nbuf, "fme%d.Ecall", Nfmep->id);
295 	Nfmep->Ecallcount =
296 	    stats_new_counter(nbuf, "calls to effects_test()", 1);
297 	(void) sprintf(nbuf, "fme%d.Tcall", Nfmep->id);
298 	Nfmep->Tcallcount = stats_new_counter(nbuf, "calls to triggered()", 1);
299 	(void) sprintf(nbuf, "fme%d.Marrow", Nfmep->id);
300 	Nfmep->Marrowcount = stats_new_counter(nbuf,
301 	    "arrows marked by mark_arrows()", 1);
302 	(void) sprintf(nbuf, "fme%d.diags", Nfmep->id);
303 	Nfmep->diags = stats_new_counter(nbuf, "suspect lists diagnosed", 0);
304 
305 	Nfmep->peek = 1;
306 	lut_walk(Nfmep->eventtree, (lut_cb)unset_needed_arrows, (void *)Nfmep);
307 	lut_free(Usednames, NULL, NULL);
308 	Usednames = NULL;
309 	lut_walk(Nfmep->eventtree, (lut_cb)clear_arrows, (void *)Nfmep);
310 	(void) hypothesise(Nfmep, Nfmep->e0, Nfmep->ull, &my_delay);
311 	itree_prune(Nfmep->eventtree);
312 	lut_walk(Nfmep->eventtree, (lut_cb)set_needed_arrows, (void *)Nfmep);
313 
314 	stats_delete(Nfmep->Rcount);
315 	stats_delete(Nfmep->Hcallcount);
316 	stats_delete(Nfmep->Rcallcount);
317 	stats_delete(Nfmep->Ccallcount);
318 	stats_delete(Nfmep->Ecallcount);
319 	stats_delete(Nfmep->Tcallcount);
320 	stats_delete(Nfmep->Marrowcount);
321 	stats_delete(Nfmep->diags);
322 	itree_free(Nfmep->eventtree);
323 	lut_free(Nfmep->globals, globals_destructor, NULL);
324 	FREE(Nfmep);
325 }
326 
327 static struct fme *
328 newfme(const char *e0class, const struct ipath *e0ipp, fmd_hdl_t *hdl,
329 	fmd_case_t *fmcase)
330 {
331 	struct cfgdata *cfgdata;
332 	int init_size;
333 	extern int alloc_total();
334 
335 	init_size = alloc_total();
336 	out(O_ALTFP|O_STAMP, "start config_snapshot using %d bytes", init_size);
337 	if ((cfgdata = config_snapshot()) == NULL) {
338 		out(O_ALTFP, "newfme: NULL configuration");
339 		Undiag_reason = UD_NOCONF;
340 		return (NULL);
341 	}
342 	platform_save_config(hdl, fmcase);
343 	out(O_ALTFP|O_STAMP, "config_snapshot added %d bytes",
344 	    alloc_total() - init_size);
345 
346 	Nfmep = alloc_fme();
347 
348 	Nfmep->id = Nextid++;
349 	Nfmep->config = cfgdata->cooked;
350 	config_free(cfgdata);
351 	Nfmep->posted_suspects = 0;
352 	Nfmep->uniqobs = 0;
353 	Nfmep->state = FME_NOTHING;
354 	Nfmep->pull = 0ULL;
355 	Nfmep->overflow = 0;
356 
357 	Nfmep->fmcase = fmcase;
358 	Nfmep->hdl = hdl;
359 
360 	if ((Nfmep->eventtree = itree_create(Nfmep->config)) == NULL) {
361 		out(O_ALTFP, "newfme: NULL instance tree");
362 		Undiag_reason = UD_INSTFAIL;
363 		structconfig_free(Nfmep->config);
364 		destroy_fme_bufs(Nfmep);
365 		FREE(Nfmep);
366 		Nfmep = NULL;
367 		return (NULL);
368 	}
369 
370 	itree_ptree(O_ALTFP|O_VERB2, Nfmep->eventtree);
371 
372 	if ((Nfmep->e0 =
373 	    itree_lookup(Nfmep->eventtree, e0class, e0ipp)) == NULL) {
374 		out(O_ALTFP, "newfme: e0 not in instance tree");
375 		Undiag_reason = UD_BADEVENTI;
376 		itree_free(Nfmep->eventtree);
377 		structconfig_free(Nfmep->config);
378 		destroy_fme_bufs(Nfmep);
379 		FREE(Nfmep);
380 		Nfmep = NULL;
381 		return (NULL);
382 	}
383 
384 	return (fme_ready(Nfmep));
385 }
386 
387 void
388 fme_fini(void)
389 {
390 	struct fme *sfp, *fp;
391 	struct case_list *ucasep, *nextcasep;
392 
393 	ucasep = Undiagablecaselist;
394 	while (ucasep != NULL) {
395 		nextcasep = ucasep->next;
396 		FREE(ucasep);
397 		ucasep = nextcasep;
398 	}
399 	Undiagablecaselist = NULL;
400 
401 	/* clean up closed fmes */
402 	fp = ClosedFMEs;
403 	while (fp != NULL) {
404 		sfp = fp->next;
405 		destroy_fme(fp);
406 		fp = sfp;
407 	}
408 	ClosedFMEs = NULL;
409 
410 	fp = FMElist;
411 	while (fp != NULL) {
412 		sfp = fp->next;
413 		destroy_fme(fp);
414 		fp = sfp;
415 	}
416 	FMElist = EFMElist = NULL;
417 
418 	/* if we were in the middle of creating an fme, free it now */
419 	if (Nfmep) {
420 		destroy_fme(Nfmep);
421 		Nfmep = NULL;
422 	}
423 }
424 
425 /*
426  * Allocated space for a buffer name.  20 bytes allows for
427  * a ridiculous 9,999,999 unique observations.
428  */
429 #define	OBBUFNMSZ 20
430 
431 /*
432  *  serialize_observation
433  *
434  *  Create a recoverable version of the current observation
435  *  (f->ecurrent).  We keep a serialized version of each unique
436  *  observation in order that we may resume correctly the fme in the
437  *  correct state if eft or fmd crashes and we're restarted.
438  */
439 static void
440 serialize_observation(struct fme *fp, const char *cls, const struct ipath *ipp)
441 {
442 	size_t pkdlen;
443 	char tmpbuf[OBBUFNMSZ];
444 	char *pkd = NULL;
445 	char *estr;
446 
447 	(void) snprintf(tmpbuf, OBBUFNMSZ, "observed%d", fp->uniqobs);
448 	estr = ipath2str(cls, ipp);
449 	fmd_buf_create(fp->hdl, fp->fmcase, tmpbuf, strlen(estr) + 1);
450 	fmd_buf_write(fp->hdl, fp->fmcase, tmpbuf, (void *)estr,
451 	    strlen(estr) + 1);
452 	FREE(estr);
453 
454 	if (fp->ecurrent != NULL && fp->ecurrent->nvp != NULL) {
455 		(void) snprintf(tmpbuf,
456 		    OBBUFNMSZ, "observed%d.nvp", fp->uniqobs);
457 		if (nvlist_xpack(fp->ecurrent->nvp,
458 		    &pkd, &pkdlen, NV_ENCODE_XDR, &Eft_nv_hdl) != 0)
459 			out(O_DIE|O_SYS, "pack of observed nvl failed");
460 		fmd_buf_create(fp->hdl, fp->fmcase, tmpbuf, pkdlen);
461 		fmd_buf_write(fp->hdl, fp->fmcase, tmpbuf, (void *)pkd, pkdlen);
462 		FREE(pkd);
463 	}
464 
465 	fp->uniqobs++;
466 	fmd_buf_write(fp->hdl, fp->fmcase, WOBUF_NOBS, (void *)&fp->uniqobs,
467 	    sizeof (fp->uniqobs));
468 }
469 
470 /*
471  *  init_fme_bufs -- We keep several bits of state about an fme for
472  *	use if eft or fmd crashes and we're restarted.
473  */
474 static void
475 init_fme_bufs(struct fme *fp)
476 {
477 	fmd_buf_create(fp->hdl, fp->fmcase, WOBUF_PULL, sizeof (fp->pull));
478 	fmd_buf_write(fp->hdl, fp->fmcase, WOBUF_PULL, (void *)&fp->pull,
479 	    sizeof (fp->pull));
480 
481 	fmd_buf_create(fp->hdl, fp->fmcase, WOBUF_ID, sizeof (fp->id));
482 	fmd_buf_write(fp->hdl, fp->fmcase, WOBUF_ID, (void *)&fp->id,
483 	    sizeof (fp->id));
484 
485 	fmd_buf_create(fp->hdl, fp->fmcase, WOBUF_NOBS, sizeof (fp->uniqobs));
486 	fmd_buf_write(fp->hdl, fp->fmcase, WOBUF_NOBS, (void *)&fp->uniqobs,
487 	    sizeof (fp->uniqobs));
488 
489 	fmd_buf_create(fp->hdl, fp->fmcase, WOBUF_POSTD,
490 	    sizeof (fp->posted_suspects));
491 	fmd_buf_write(fp->hdl, fp->fmcase, WOBUF_POSTD,
492 	    (void *)&fp->posted_suspects, sizeof (fp->posted_suspects));
493 }
494 
495 static void
496 destroy_fme_bufs(struct fme *fp)
497 {
498 	char tmpbuf[OBBUFNMSZ];
499 	int o;
500 
501 	platform_restore_config(fp->hdl, fp->fmcase);
502 	fmd_buf_destroy(fp->hdl, fp->fmcase, WOBUF_CFGLEN);
503 	fmd_buf_destroy(fp->hdl, fp->fmcase, WOBUF_CFG);
504 	fmd_buf_destroy(fp->hdl, fp->fmcase, WOBUF_PULL);
505 	fmd_buf_destroy(fp->hdl, fp->fmcase, WOBUF_ID);
506 	fmd_buf_destroy(fp->hdl, fp->fmcase, WOBUF_POSTD);
507 	fmd_buf_destroy(fp->hdl, fp->fmcase, WOBUF_NOBS);
508 
509 	for (o = 0; o < fp->uniqobs; o++) {
510 		(void) snprintf(tmpbuf, OBBUFNMSZ, "observed%d", o);
511 		fmd_buf_destroy(fp->hdl, fp->fmcase, tmpbuf);
512 		(void) snprintf(tmpbuf, OBBUFNMSZ, "observed%d.nvp", o);
513 		fmd_buf_destroy(fp->hdl, fp->fmcase, tmpbuf);
514 	}
515 }
516 
517 /*
518  * reconstitute_observations -- convert a case's serialized observations
519  *	back into struct events.  Returns zero if all observations are
520  *	successfully reconstituted.
521  */
522 static int
523 reconstitute_observations(struct fme *fmep)
524 {
525 	struct event *ep;
526 	struct node *epnamenp = NULL;
527 	size_t pkdlen;
528 	char *pkd = NULL;
529 	char *tmpbuf = alloca(OBBUFNMSZ);
530 	char *sepptr;
531 	char *estr;
532 	int ocnt;
533 	int elen;
534 
535 	for (ocnt = 0; ocnt < fmep->uniqobs; ocnt++) {
536 		(void) snprintf(tmpbuf, OBBUFNMSZ, "observed%d", ocnt);
537 		elen = fmd_buf_size(fmep->hdl, fmep->fmcase, tmpbuf);
538 		if (elen == 0) {
539 			out(O_ALTFP,
540 			    "reconstitute_observation: no %s buffer found.",
541 			    tmpbuf);
542 			Undiag_reason = UD_MISSINGOBS;
543 			break;
544 		}
545 
546 		estr = MALLOC(elen);
547 		fmd_buf_read(fmep->hdl, fmep->fmcase, tmpbuf, estr, elen);
548 		sepptr = strchr(estr, '@');
549 		if (sepptr == NULL) {
550 			out(O_ALTFP,
551 			    "reconstitute_observation: %s: "
552 			    "missing @ separator in %s.",
553 			    tmpbuf, estr);
554 			Undiag_reason = UD_MISSINGPATH;
555 			FREE(estr);
556 			break;
557 		}
558 
559 		*sepptr = '\0';
560 		if ((epnamenp = pathstring2epnamenp(sepptr + 1)) == NULL) {
561 			out(O_ALTFP,
562 			    "reconstitute_observation: %s: "
563 			    "trouble converting path string \"%s\" "
564 			    "to internal representation.",
565 			    tmpbuf, sepptr + 1);
566 			Undiag_reason = UD_MISSINGPATH;
567 			FREE(estr);
568 			break;
569 		}
570 
571 		/* construct the event */
572 		ep = itree_lookup(fmep->eventtree,
573 		    stable(estr), ipath(epnamenp));
574 		if (ep == NULL) {
575 			out(O_ALTFP,
576 			    "reconstitute_observation: %s: "
577 			    "lookup of  \"%s\" in itree failed.",
578 			    tmpbuf, ipath2str(estr, ipath(epnamenp)));
579 			Undiag_reason = UD_BADOBS;
580 			tree_free(epnamenp);
581 			FREE(estr);
582 			break;
583 		}
584 		tree_free(epnamenp);
585 
586 		/*
587 		 * We may or may not have a saved nvlist for the observation
588 		 */
589 		(void) snprintf(tmpbuf, OBBUFNMSZ, "observed%d.nvp", ocnt);
590 		pkdlen = fmd_buf_size(fmep->hdl, fmep->fmcase, tmpbuf);
591 		if (pkdlen != 0) {
592 			pkd = MALLOC(pkdlen);
593 			fmd_buf_read(fmep->hdl,
594 			    fmep->fmcase, tmpbuf, pkd, pkdlen);
595 			ASSERT(ep->nvp == NULL);
596 			if (nvlist_xunpack(pkd,
597 			    pkdlen, &ep->nvp, &Eft_nv_hdl) != 0)
598 				out(O_DIE|O_SYS, "pack of observed nvl failed");
599 			FREE(pkd);
600 		}
601 
602 		if (ocnt == 0)
603 			fmep->e0 = ep;
604 
605 		FREE(estr);
606 		fmep->ecurrent = ep;
607 		ep->count++;
608 
609 		/* link it into list of observations seen */
610 		ep->observations = fmep->observations;
611 		fmep->observations = ep;
612 	}
613 
614 	if (ocnt == fmep->uniqobs) {
615 		(void) fme_ready(fmep);
616 		return (0);
617 	}
618 
619 	return (1);
620 }
621 
622 /*
623  * restart_fme -- called during eft initialization.  Reconstitutes
624  *	an in-progress fme.
625  */
626 void
627 fme_restart(fmd_hdl_t *hdl, fmd_case_t *inprogress)
628 {
629 	nvlist_t *defect;
630 	struct case_list *bad;
631 	struct fme *fmep;
632 	struct cfgdata *cfgdata;
633 	size_t rawsz;
634 	struct event *ep;
635 	char *tmpbuf = alloca(OBBUFNMSZ);
636 	char *sepptr;
637 	char *estr;
638 	int elen;
639 	struct node *epnamenp = NULL;
640 	int init_size;
641 	extern int alloc_total();
642 
643 	/*
644 	 * ignore solved or closed cases
645 	 */
646 	if (fmd_case_solved(hdl, inprogress) ||
647 	    fmd_case_closed(hdl, inprogress))
648 		return;
649 
650 	fmep = alloc_fme();
651 	fmep->fmcase = inprogress;
652 	fmep->hdl = hdl;
653 
654 	if (fmd_buf_size(hdl, inprogress, WOBUF_POSTD) == 0) {
655 		out(O_ALTFP, "restart_fme: no saved posted status");
656 		Undiag_reason = UD_MISSINGINFO;
657 		goto badcase;
658 	} else {
659 		fmd_buf_read(hdl, inprogress, WOBUF_POSTD,
660 		    (void *)&fmep->posted_suspects,
661 		    sizeof (fmep->posted_suspects));
662 	}
663 
664 	if (fmd_buf_size(hdl, inprogress, WOBUF_ID) == 0) {
665 		out(O_ALTFP, "restart_fme: no saved id");
666 		Undiag_reason = UD_MISSINGINFO;
667 		goto badcase;
668 	} else {
669 		fmd_buf_read(hdl, inprogress, WOBUF_ID, (void *)&fmep->id,
670 		    sizeof (fmep->id));
671 	}
672 	if (Nextid <= fmep->id)
673 		Nextid = fmep->id + 1;
674 
675 	out(O_ALTFP, "Replay FME %d", fmep->id);
676 
677 	if (fmd_buf_size(hdl, inprogress, WOBUF_CFGLEN) != sizeof (size_t)) {
678 		out(O_ALTFP, "restart_fme: No config data");
679 		Undiag_reason = UD_MISSINGINFO;
680 		goto badcase;
681 	}
682 	fmd_buf_read(hdl, inprogress, WOBUF_CFGLEN, (void *)&rawsz,
683 	    sizeof (size_t));
684 
685 	if ((fmep->e0r = fmd_case_getprincipal(hdl, inprogress)) == NULL) {
686 		out(O_ALTFP, "restart_fme: No event zero");
687 		Undiag_reason = UD_MISSINGZERO;
688 		goto badcase;
689 	}
690 
691 	if (fmd_buf_size(hdl, inprogress, WOBUF_PULL) == 0) {
692 		out(O_ALTFP, "restart_fme: no saved wait time");
693 		Undiag_reason = UD_MISSINGINFO;
694 		goto badcase;
695 	} else {
696 		fmd_buf_read(hdl, inprogress, WOBUF_PULL, (void *)&fmep->pull,
697 		    sizeof (fmep->pull));
698 	}
699 
700 	if (fmd_buf_size(hdl, inprogress, WOBUF_NOBS) == 0) {
701 		out(O_ALTFP, "restart_fme: no count of observations");
702 		Undiag_reason = UD_MISSINGINFO;
703 		goto badcase;
704 	} else {
705 		fmd_buf_read(hdl, inprogress, WOBUF_NOBS,
706 		    (void *)&fmep->uniqobs, sizeof (fmep->uniqobs));
707 	}
708 
709 	(void) snprintf(tmpbuf, OBBUFNMSZ, "observed0");
710 	elen = fmd_buf_size(fmep->hdl, fmep->fmcase, tmpbuf);
711 	if (elen == 0) {
712 		out(O_ALTFP, "reconstitute_observation: no %s buffer found.",
713 		    tmpbuf);
714 		Undiag_reason = UD_MISSINGOBS;
715 		goto badcase;
716 	}
717 	estr = MALLOC(elen);
718 	fmd_buf_read(fmep->hdl, fmep->fmcase, tmpbuf, estr, elen);
719 	sepptr = strchr(estr, '@');
720 	if (sepptr == NULL) {
721 		out(O_ALTFP, "reconstitute_observation: %s: "
722 		    "missing @ separator in %s.",
723 		    tmpbuf, estr);
724 		Undiag_reason = UD_MISSINGPATH;
725 		FREE(estr);
726 		goto badcase;
727 	}
728 	*sepptr = '\0';
729 	if ((epnamenp = pathstring2epnamenp(sepptr + 1)) == NULL) {
730 		out(O_ALTFP, "reconstitute_observation: %s: "
731 		    "trouble converting path string \"%s\" "
732 		    "to internal representation.", tmpbuf, sepptr + 1);
733 		Undiag_reason = UD_MISSINGPATH;
734 		FREE(estr);
735 		goto badcase;
736 	}
737 	prune_propagations(stable(estr), ipath(epnamenp));
738 	tree_free(epnamenp);
739 	FREE(estr);
740 
741 	init_size = alloc_total();
742 	out(O_ALTFP|O_STAMP, "start config_restore using %d bytes", init_size);
743 	cfgdata = MALLOC(sizeof (struct cfgdata));
744 	cfgdata->cooked = NULL;
745 	cfgdata->devcache = NULL;
746 	cfgdata->cpucache = NULL;
747 	cfgdata->raw_refcnt = 1;
748 
749 	if (rawsz > 0) {
750 		if (fmd_buf_size(hdl, inprogress, WOBUF_CFG) != rawsz) {
751 			out(O_ALTFP, "restart_fme: Config data size mismatch");
752 			Undiag_reason = UD_CFGMISMATCH;
753 			goto badcase;
754 		}
755 		cfgdata->begin = MALLOC(rawsz);
756 		cfgdata->end = cfgdata->nextfree = cfgdata->begin + rawsz;
757 		fmd_buf_read(hdl,
758 		    inprogress, WOBUF_CFG, cfgdata->begin, rawsz);
759 	} else {
760 		cfgdata->begin = cfgdata->end = cfgdata->nextfree = NULL;
761 	}
762 
763 	config_cook(cfgdata);
764 	fmep->config = cfgdata->cooked;
765 	config_free(cfgdata);
766 	out(O_ALTFP|O_STAMP, "config_restore added %d bytes",
767 	    alloc_total() - init_size);
768 
769 	if ((fmep->eventtree = itree_create(fmep->config)) == NULL) {
770 		/* case not properly saved or irretrievable */
771 		out(O_ALTFP, "restart_fme: NULL instance tree");
772 		Undiag_reason = UD_INSTFAIL;
773 		goto badcase;
774 	}
775 
776 	itree_ptree(O_ALTFP|O_VERB2, fmep->eventtree);
777 
778 	if (reconstitute_observations(fmep) != 0)
779 		goto badcase;
780 
781 	out(O_ALTFP|O_NONL, "FME %d replay observations: ", fmep->id);
782 	for (ep = fmep->observations; ep; ep = ep->observations) {
783 		out(O_ALTFP|O_NONL, " ");
784 		itree_pevent_brief(O_ALTFP|O_NONL, ep);
785 	}
786 	out(O_ALTFP, NULL);
787 
788 	Open_fme_count++;
789 
790 	/* give the diagnosis algorithm a shot at the new FME state */
791 	fme_eval(fmep, fmep->e0r);
792 	return;
793 
794 badcase:
795 	if (fmep->eventtree != NULL)
796 		itree_free(fmep->eventtree);
797 	if (fmep->config)
798 		structconfig_free(fmep->config);
799 	destroy_fme_bufs(fmep);
800 	FREE(fmep);
801 
802 	/*
803 	 * Since we're unable to restart the case, add it to the undiagable
804 	 * list and solve and close it as appropriate.
805 	 */
806 	bad = MALLOC(sizeof (struct case_list));
807 	bad->next = NULL;
808 
809 	if (Undiagablecaselist != NULL)
810 		bad->next = Undiagablecaselist;
811 	Undiagablecaselist = bad;
812 	bad->fmcase = inprogress;
813 
814 	out(O_ALTFP|O_NONL, "[case %s (unable to restart), ",
815 	    fmd_case_uuid(hdl, bad->fmcase));
816 
817 	if (fmd_case_solved(hdl, bad->fmcase)) {
818 		out(O_ALTFP|O_NONL, "already solved, ");
819 	} else {
820 		out(O_ALTFP|O_NONL, "solving, ");
821 		defect = fmd_nvl_create_fault(hdl, UNDIAGNOSABLE_DEFECT, 100,
822 		    NULL, NULL, NULL);
823 		if (Undiag_reason != NULL)
824 			(void) nvlist_add_string(defect,
825 			    UNDIAG_REASON, Undiag_reason);
826 		fmd_case_add_suspect(hdl, bad->fmcase, defect);
827 		fmd_case_solve(hdl, bad->fmcase);
828 	}
829 
830 	if (fmd_case_closed(hdl, bad->fmcase)) {
831 		out(O_ALTFP, "already closed ]");
832 	} else {
833 		out(O_ALTFP, "closing ]");
834 		fmd_case_close(hdl, bad->fmcase);
835 	}
836 }
837 
838 /*ARGSUSED*/
839 static void
840 globals_destructor(void *left, void *right, void *arg)
841 {
842 	struct evalue *evp = (struct evalue *)right;
843 	if (evp->t == NODEPTR)
844 		tree_free((struct node *)(uintptr_t)evp->v);
845 	evp->v = NULL;
846 	FREE(evp);
847 }
848 
849 void
850 destroy_fme(struct fme *f)
851 {
852 	stats_delete(f->Rcount);
853 	stats_delete(f->Hcallcount);
854 	stats_delete(f->Rcallcount);
855 	stats_delete(f->Ccallcount);
856 	stats_delete(f->Ecallcount);
857 	stats_delete(f->Tcallcount);
858 	stats_delete(f->Marrowcount);
859 	stats_delete(f->diags);
860 
861 	if (f->eventtree != NULL)
862 		itree_free(f->eventtree);
863 	if (f->config)
864 		structconfig_free(f->config);
865 	lut_free(f->globals, globals_destructor, NULL);
866 	FREE(f);
867 }
868 
869 static const char *
870 fme_state2str(enum fme_state s)
871 {
872 	switch (s) {
873 	case FME_NOTHING:	return ("NOTHING");
874 	case FME_WAIT:		return ("WAIT");
875 	case FME_CREDIBLE:	return ("CREDIBLE");
876 	case FME_DISPROVED:	return ("DISPROVED");
877 	case FME_DEFERRED:	return ("DEFERRED");
878 	default:		return ("UNKNOWN");
879 	}
880 }
881 
882 static int
883 is_problem(enum nametype t)
884 {
885 	return (t == N_FAULT || t == N_DEFECT || t == N_UPSET);
886 }
887 
888 static int
889 is_fault(enum nametype t)
890 {
891 	return (t == N_FAULT);
892 }
893 
894 static int
895 is_defect(enum nametype t)
896 {
897 	return (t == N_DEFECT);
898 }
899 
900 static int
901 is_upset(enum nametype t)
902 {
903 	return (t == N_UPSET);
904 }
905 
906 static void
907 fme_print(int flags, struct fme *fmep)
908 {
909 	struct event *ep;
910 
911 	out(flags, "Fault Management Exercise %d", fmep->id);
912 	out(flags, "\t       State: %s", fme_state2str(fmep->state));
913 	out(flags|O_NONL, "\t  Start time: ");
914 	ptree_timeval(flags|O_NONL, &fmep->ull);
915 	out(flags, NULL);
916 	if (fmep->wull) {
917 		out(flags|O_NONL, "\t   Wait time: ");
918 		ptree_timeval(flags|O_NONL, &fmep->wull);
919 		out(flags, NULL);
920 	}
921 	out(flags|O_NONL, "\t          E0: ");
922 	if (fmep->e0)
923 		itree_pevent_brief(flags|O_NONL, fmep->e0);
924 	else
925 		out(flags|O_NONL, "NULL");
926 	out(flags, NULL);
927 	out(flags|O_NONL, "\tObservations:");
928 	for (ep = fmep->observations; ep; ep = ep->observations) {
929 		out(flags|O_NONL, " ");
930 		itree_pevent_brief(flags|O_NONL, ep);
931 	}
932 	out(flags, NULL);
933 	out(flags|O_NONL, "\tSuspect list:");
934 	for (ep = fmep->suspects; ep; ep = ep->suspects) {
935 		out(flags|O_NONL, " ");
936 		itree_pevent_brief(flags|O_NONL, ep);
937 	}
938 	out(flags, NULL);
939 	if (fmep->eventtree != NULL) {
940 		out(flags|O_VERB2, "\t        Tree:");
941 		itree_ptree(flags|O_VERB2, fmep->eventtree);
942 	}
943 }
944 
945 static struct node *
946 pathstring2epnamenp(char *path)
947 {
948 	char *sep = "/";
949 	struct node *ret;
950 	char *ptr;
951 
952 	if ((ptr = strtok(path, sep)) == NULL)
953 		out(O_DIE, "pathstring2epnamenp: invalid empty class");
954 
955 	ret = tree_iname(stable(ptr), NULL, 0);
956 
957 	while ((ptr = strtok(NULL, sep)) != NULL)
958 		ret = tree_name_append(ret,
959 		    tree_iname(stable(ptr), NULL, 0));
960 
961 	return (ret);
962 }
963 
964 /*
965  * for a given upset sp, increment the corresponding SERD engine.  if the
966  * SERD engine trips, return the ename and ipp of the resulting ereport.
967  * returns true if engine tripped and *enamep and *ippp were filled in.
968  */
969 static int
970 serd_eval(struct fme *fmep, fmd_hdl_t *hdl, fmd_event_t *ffep,
971     fmd_case_t *fmcase, struct event *sp, const char **enamep,
972     const struct ipath **ippp)
973 {
974 	struct node *serdinst;
975 	char *serdname;
976 	struct node *nid;
977 	struct serd_entry *newentp;
978 
979 	ASSERT(sp->t == N_UPSET);
980 	ASSERT(ffep != NULL);
981 
982 	/*
983 	 * obtain instanced SERD engine from the upset sp.  from this
984 	 * derive serdname, the string used to identify the SERD engine.
985 	 */
986 	serdinst = eventprop_lookup(sp, L_engine);
987 
988 	if (serdinst == NULL)
989 		return (NULL);
990 
991 	serdname = ipath2str(serdinst->u.stmt.np->u.event.ename->u.name.s,
992 	    ipath(serdinst->u.stmt.np->u.event.epname));
993 
994 	/* handle serd engine "id" property, if there is one */
995 	if ((nid =
996 	    lut_lookup(serdinst->u.stmt.lutp, (void *)L_id, NULL)) != NULL) {
997 		struct evalue *gval;
998 		char suffixbuf[200];
999 		char *suffix;
1000 		char *nserdname;
1001 		size_t nname;
1002 
1003 		out(O_ALTFP|O_NONL, "serd \"%s\" id: ", serdname);
1004 		ptree_name_iter(O_ALTFP|O_NONL, nid);
1005 
1006 		ASSERTinfo(nid->t == T_GLOBID, ptree_nodetype2str(nid->t));
1007 
1008 		if ((gval = lut_lookup(fmep->globals,
1009 		    (void *)nid->u.globid.s, NULL)) == NULL) {
1010 			out(O_ALTFP, " undefined");
1011 		} else if (gval->t == UINT64) {
1012 			out(O_ALTFP, " %llu", gval->v);
1013 			(void) sprintf(suffixbuf, "%llu", gval->v);
1014 			suffix = suffixbuf;
1015 		} else {
1016 			out(O_ALTFP, " \"%s\"", (char *)(uintptr_t)gval->v);
1017 			suffix = (char *)(uintptr_t)gval->v;
1018 		}
1019 
1020 		nname = strlen(serdname) + strlen(suffix) + 2;
1021 		nserdname = MALLOC(nname);
1022 		(void) snprintf(nserdname, nname, "%s:%s", serdname, suffix);
1023 		FREE(serdname);
1024 		serdname = nserdname;
1025 	}
1026 
1027 	if (!fmd_serd_exists(hdl, serdname)) {
1028 		struct node *nN, *nT;
1029 		const char *s;
1030 		struct node *nodep;
1031 		struct config *cp;
1032 		char *path;
1033 		uint_t nval;
1034 		hrtime_t tval;
1035 		const char *name;
1036 		char *serd_name;
1037 		int i;
1038 		char *ptr;
1039 		int got_n_override = 0, got_t_override = 0;
1040 
1041 		/* no SERD engine yet, so create it */
1042 		nodep = serdinst->u.stmt.np->u.event.epname;
1043 		name = serdinst->u.stmt.np->u.event.ename->u.name.s;
1044 		path = ipath2str(NULL, ipath(nodep));
1045 		cp = config_lookup(fmep->config, path, 0);
1046 		FREE((void *)path);
1047 
1048 		/*
1049 		 * We allow serd paramaters to be overridden, either from
1050 		 * eft.conf file values (if Serd_Override is set) or from
1051 		 * driver properties (for "serd.io.device" engines).
1052 		 */
1053 		if (Serd_Override != NULL) {
1054 			char *save_ptr, *ptr1, *ptr2, *ptr3;
1055 			ptr3 = save_ptr = STRDUP(Serd_Override);
1056 			while (*ptr3 != '\0') {
1057 				ptr1 = strchr(ptr3, ',');
1058 				*ptr1 = '\0';
1059 				if (strcmp(ptr3, name) == 0) {
1060 					ptr2 =  strchr(ptr1 + 1, ',');
1061 					*ptr2 = '\0';
1062 					nval = atoi(ptr1 + 1);
1063 					out(O_ALTFP, "serd override %s_n %d",
1064 					    name, nval);
1065 					ptr3 =  strchr(ptr2 + 1, ' ');
1066 					if (ptr3)
1067 						*ptr3 = '\0';
1068 					ptr = STRDUP(ptr2 + 1);
1069 					out(O_ALTFP, "serd override %s_t %s",
1070 					    name, ptr);
1071 					got_n_override = 1;
1072 					got_t_override = 1;
1073 					break;
1074 				} else {
1075 					ptr2 =  strchr(ptr1 + 1, ',');
1076 					ptr3 =  strchr(ptr2 + 1, ' ');
1077 					if (ptr3 == NULL)
1078 						break;
1079 				}
1080 				ptr3++;
1081 			}
1082 			FREE(save_ptr);
1083 		}
1084 
1085 		if (cp && got_n_override == 0) {
1086 			/*
1087 			 * convert serd engine name into property name
1088 			 */
1089 			serd_name = MALLOC(strlen(name) + 3);
1090 			for (i = 0; i < strlen(name); i++) {
1091 				if (name[i] == '.')
1092 					serd_name[i] = '_';
1093 				else
1094 					serd_name[i] = name[i];
1095 			}
1096 			serd_name[i++] = '_';
1097 			serd_name[i++] = 'n';
1098 			serd_name[i] = '\0';
1099 			if (s = config_getprop(cp, serd_name)) {
1100 				nval = atoi(s);
1101 				out(O_ALTFP, "serd override %s_n %s", name, s);
1102 				got_n_override = 1;
1103 			}
1104 			serd_name[i - 1] = 't';
1105 			if (s = config_getprop(cp, serd_name)) {
1106 				ptr = STRDUP(s);
1107 				out(O_ALTFP, "serd override %s_t %s", name, s);
1108 				got_t_override = 1;
1109 			}
1110 			FREE(serd_name);
1111 		}
1112 
1113 		if (!got_n_override) {
1114 			nN = lut_lookup(serdinst->u.stmt.lutp, (void *)L_N,
1115 			    NULL);
1116 			ASSERT(nN->t == T_NUM);
1117 			nval = (uint_t)nN->u.ull;
1118 		}
1119 		if (!got_t_override) {
1120 			nT = lut_lookup(serdinst->u.stmt.lutp, (void *)L_T,
1121 			    NULL);
1122 			ASSERT(nT->t == T_TIMEVAL);
1123 			tval = (hrtime_t)nT->u.ull;
1124 		} else {
1125 			const unsigned long long *ullp;
1126 			const char *suffix;
1127 			int len;
1128 
1129 			len = strspn(ptr, "0123456789");
1130 			suffix = stable(&ptr[len]);
1131 			ullp = (unsigned long long *)lut_lookup(Timesuffixlut,
1132 			    (void *)suffix, NULL);
1133 			ptr[len] = '\0';
1134 			tval = (unsigned long long)strtoul(ptr, NULL, 0) *
1135 			    (ullp ? *ullp : 1ll);
1136 			FREE(ptr);
1137 		}
1138 		fmd_serd_create(hdl, serdname, nval, tval);
1139 	}
1140 
1141 	newentp = MALLOC(sizeof (*newentp));
1142 	newentp->ename = stable(serdinst->u.stmt.np->u.event.ename->u.name.s);
1143 	newentp->ipath = ipath(serdinst->u.stmt.np->u.event.epname);
1144 	newentp->hdl = hdl;
1145 	if (lut_lookup(SerdEngines, newentp, (lut_cmp)serd_cmp) == NULL) {
1146 		SerdEngines = lut_add(SerdEngines, (void *)newentp,
1147 		    (void *)newentp, (lut_cmp)serd_cmp);
1148 		Serd_need_save = 1;
1149 		serd_save();
1150 	} else {
1151 		FREE(newentp);
1152 	}
1153 
1154 
1155 	/*
1156 	 * increment SERD engine.  if engine fires, reset serd
1157 	 * engine and return trip_strcode
1158 	 */
1159 	if (fmd_serd_record(hdl, serdname, ffep)) {
1160 		struct node *tripinst = lut_lookup(serdinst->u.stmt.lutp,
1161 		    (void *)L_trip, NULL);
1162 
1163 		ASSERT(tripinst != NULL);
1164 
1165 		*enamep = tripinst->u.event.ename->u.name.s;
1166 		*ippp = ipath(tripinst->u.event.epname);
1167 
1168 		fmd_case_add_serd(hdl, fmcase, serdname);
1169 		fmd_serd_reset(hdl, serdname);
1170 		out(O_ALTFP|O_NONL, "[engine fired: %s, sending: ", serdname);
1171 		ipath_print(O_ALTFP|O_NONL, *enamep, *ippp);
1172 		out(O_ALTFP, "]");
1173 
1174 		FREE(serdname);
1175 		return (1);
1176 	}
1177 
1178 	FREE(serdname);
1179 	return (0);
1180 }
1181 
1182 /*
1183  * search a suspect list for upsets.  feed each upset to serd_eval() and
1184  * build up tripped[], an array of ereports produced by the firing of
1185  * any SERD engines.  then feed each ereport back into
1186  * fme_receive_report().
1187  *
1188  * returns ntrip, the number of these ereports produced.
1189  */
1190 static int
1191 upsets_eval(struct fme *fmep, fmd_event_t *ffep)
1192 {
1193 	/* we build an array of tripped ereports that we send ourselves */
1194 	struct {
1195 		const char *ename;
1196 		const struct ipath *ipp;
1197 	} *tripped;
1198 	struct event *sp;
1199 	int ntrip, nupset, i;
1200 
1201 	/*
1202 	 * count the number of upsets to determine the upper limit on
1203 	 * expected trip ereport strings.  remember that one upset can
1204 	 * lead to at most one ereport.
1205 	 */
1206 	nupset = 0;
1207 	for (sp = fmep->suspects; sp; sp = sp->suspects) {
1208 		if (sp->t == N_UPSET)
1209 			nupset++;
1210 	}
1211 
1212 	if (nupset == 0)
1213 		return (0);
1214 
1215 	/*
1216 	 * get to this point if we have upsets and expect some trip
1217 	 * ereports
1218 	 */
1219 	tripped = alloca(sizeof (*tripped) * nupset);
1220 	bzero((void *)tripped, sizeof (*tripped) * nupset);
1221 
1222 	ntrip = 0;
1223 	for (sp = fmep->suspects; sp; sp = sp->suspects)
1224 		if (sp->t == N_UPSET &&
1225 		    serd_eval(fmep, fmep->hdl, ffep, fmep->fmcase, sp,
1226 		    &tripped[ntrip].ename, &tripped[ntrip].ipp))
1227 			ntrip++;
1228 
1229 	for (i = 0; i < ntrip; i++)
1230 		fme_receive_report(fmep->hdl, ffep,
1231 		    tripped[i].ename, tripped[i].ipp, NULL);
1232 
1233 	return (ntrip);
1234 }
1235 
1236 /*
1237  * fme_receive_external_report -- call when an external ereport comes in
1238  *
1239  * this routine just converts the relevant information from the ereport
1240  * into a format used internally and passes it on to fme_receive_report().
1241  */
1242 void
1243 fme_receive_external_report(fmd_hdl_t *hdl, fmd_event_t *ffep, nvlist_t *nvl,
1244     const char *eventstring)
1245 {
1246 	struct node *epnamenp = platform_getpath(nvl);
1247 	const struct ipath *ipp;
1248 
1249 	/*
1250 	 * XFILE: If we ended up without a path, it's an X-file.
1251 	 * For now, use our undiagnosable interface.
1252 	 */
1253 	if (epnamenp == NULL) {
1254 		fmd_case_t *fmcase;
1255 
1256 		out(O_ALTFP, "XFILE: Unable to get path from ereport");
1257 		Undiag_reason = UD_NOPATH;
1258 		fmcase = fmd_case_open(hdl, NULL);
1259 		publish_undiagnosable(hdl, ffep, fmcase);
1260 		return;
1261 	}
1262 
1263 	ipp = ipath(epnamenp);
1264 	tree_free(epnamenp);
1265 	fme_receive_report(hdl, ffep, stable(eventstring), ipp, nvl);
1266 }
1267 
1268 /*ARGSUSED*/
1269 void
1270 fme_receive_repair_list(fmd_hdl_t *hdl, fmd_event_t *ffep, nvlist_t *nvl,
1271     const char *eventstring)
1272 {
1273 	char *uuid;
1274 	nvlist_t **nva;
1275 	uint_t nvc;
1276 	const struct ipath *ipp;
1277 
1278 	if (nvlist_lookup_string(nvl, FM_SUSPECT_UUID, &uuid) != 0 ||
1279 	    nvlist_lookup_nvlist_array(nvl, FM_SUSPECT_FAULT_LIST,
1280 	    &nva, &nvc) != 0) {
1281 		out(O_ALTFP, "No uuid or fault list for list.repaired event");
1282 		return;
1283 	}
1284 
1285 	out(O_ALTFP, "Processing list.repaired from case %s", uuid);
1286 
1287 	while (nvc-- != 0) {
1288 		/*
1289 		 * Reset any istat or serd engine associated with this path.
1290 		 */
1291 		char *path;
1292 
1293 		if ((ipp = platform_fault2ipath(*nva++)) == NULL)
1294 			continue;
1295 
1296 		path = ipath2str(NULL, ipp);
1297 		out(O_ALTFP, "fme_receive_repair_list: resetting state for %s",
1298 		    path);
1299 		FREE(path);
1300 
1301 		lut_walk(Istats, (lut_cb)istat_counter_reset_cb, (void *)ipp);
1302 		istat_save();
1303 
1304 		lut_walk(SerdEngines, (lut_cb)serd_reset_cb, (void *)ipp);
1305 		serd_save();
1306 	}
1307 }
1308 
1309 /*ARGSUSED*/
1310 void
1311 fme_receive_topology_change(void)
1312 {
1313 	lut_walk(Istats, (lut_cb)istat_counter_topo_chg_cb, NULL);
1314 	istat_save();
1315 
1316 	lut_walk(SerdEngines, (lut_cb)serd_topo_chg_cb, NULL);
1317 	serd_save();
1318 }
1319 
1320 static int mark_arrows(struct fme *fmep, struct event *ep, int mark,
1321     unsigned long long at_latest_by, unsigned long long *pdelay, int keep);
1322 
1323 /* ARGSUSED */
1324 static void
1325 clear_arrows(struct event *ep, struct event *ep2, struct fme *fmep)
1326 {
1327 	struct bubble *bp;
1328 	struct arrowlist *ap;
1329 
1330 	ep->cached_state = 0;
1331 	ep->keep_in_tree = 0;
1332 	for (bp = itree_next_bubble(ep, NULL); bp;
1333 	    bp = itree_next_bubble(ep, bp)) {
1334 		if (bp->t != B_FROM)
1335 			continue;
1336 		bp->mark = 0;
1337 		for (ap = itree_next_arrow(bp, NULL); ap;
1338 		    ap = itree_next_arrow(bp, ap))
1339 			ap->arrowp->mark = 0;
1340 	}
1341 }
1342 
1343 static void
1344 fme_receive_report(fmd_hdl_t *hdl, fmd_event_t *ffep,
1345     const char *eventstring, const struct ipath *ipp, nvlist_t *nvl)
1346 {
1347 	struct event *ep;
1348 	struct fme *fmep = NULL;
1349 	struct fme *ofmep = NULL;
1350 	struct fme *cfmep, *svfmep;
1351 	int matched = 0;
1352 	nvlist_t *defect;
1353 	fmd_case_t *fmcase;
1354 
1355 	out(O_ALTFP|O_NONL, "fme_receive_report: ");
1356 	ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
1357 	out(O_ALTFP|O_STAMP, NULL);
1358 
1359 	/* decide which FME it goes to */
1360 	for (fmep = FMElist; fmep; fmep = fmep->next) {
1361 		int prev_verbose;
1362 		unsigned long long my_delay = TIMEVAL_EVENTUALLY;
1363 		enum fme_state state;
1364 		nvlist_t *pre_peek_nvp = NULL;
1365 
1366 		if (fmep->overflow) {
1367 			if (!(fmd_case_closed(fmep->hdl, fmep->fmcase)))
1368 				ofmep = fmep;
1369 
1370 			continue;
1371 		}
1372 
1373 		/*
1374 		 * ignore solved or closed cases
1375 		 */
1376 		if (fmep->posted_suspects ||
1377 		    fmd_case_solved(fmep->hdl, fmep->fmcase) ||
1378 		    fmd_case_closed(fmep->hdl, fmep->fmcase))
1379 			continue;
1380 
1381 		/* look up event in event tree for this FME */
1382 		if ((ep = itree_lookup(fmep->eventtree,
1383 		    eventstring, ipp)) == NULL)
1384 			continue;
1385 
1386 		/* note observation */
1387 		fmep->ecurrent = ep;
1388 		if (ep->count++ == 0) {
1389 			/* link it into list of observations seen */
1390 			ep->observations = fmep->observations;
1391 			fmep->observations = ep;
1392 			ep->nvp = evnv_dupnvl(nvl);
1393 		} else {
1394 			/* use new payload values for peek */
1395 			pre_peek_nvp = ep->nvp;
1396 			ep->nvp = evnv_dupnvl(nvl);
1397 		}
1398 
1399 		/* tell hypothesise() not to mess with suspect list */
1400 		fmep->peek = 1;
1401 
1402 		/* don't want this to be verbose (unless Debug is set) */
1403 		prev_verbose = Verbose;
1404 		if (Debug == 0)
1405 			Verbose = 0;
1406 
1407 		lut_walk(fmep->eventtree, (lut_cb)clear_arrows, (void *)fmep);
1408 		state = hypothesise(fmep, fmep->e0, fmep->ull, &my_delay);
1409 
1410 		fmep->peek = 0;
1411 
1412 		/* put verbose flag back */
1413 		Verbose = prev_verbose;
1414 
1415 		if (state != FME_DISPROVED) {
1416 			/* found an FME that explains the ereport */
1417 			matched++;
1418 			out(O_ALTFP|O_NONL, "[");
1419 			ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
1420 			out(O_ALTFP, " explained by FME%d]", fmep->id);
1421 
1422 			if (pre_peek_nvp)
1423 				nvlist_free(pre_peek_nvp);
1424 
1425 			if (ep->count == 1)
1426 				serialize_observation(fmep, eventstring, ipp);
1427 
1428 			if (ffep)
1429 				fmd_case_add_ereport(hdl, fmep->fmcase, ffep);
1430 
1431 			stats_counter_bump(fmep->Rcount);
1432 
1433 			/* re-eval FME */
1434 			fme_eval(fmep, ffep);
1435 		} else {
1436 
1437 			/* not a match, undo noting of observation */
1438 			fmep->ecurrent = NULL;
1439 			if (--ep->count == 0) {
1440 				/* unlink it from observations */
1441 				fmep->observations = ep->observations;
1442 				ep->observations = NULL;
1443 				nvlist_free(ep->nvp);
1444 				ep->nvp = NULL;
1445 			} else {
1446 				nvlist_free(ep->nvp);
1447 				ep->nvp = pre_peek_nvp;
1448 			}
1449 		}
1450 	}
1451 
1452 	if (matched)
1453 		return;	/* explained by at least one existing FME */
1454 
1455 	/* clean up closed fmes */
1456 	cfmep = ClosedFMEs;
1457 	while (cfmep != NULL) {
1458 		svfmep = cfmep->next;
1459 		destroy_fme(cfmep);
1460 		cfmep = svfmep;
1461 	}
1462 	ClosedFMEs = NULL;
1463 	prune_propagations(eventstring, ipp);
1464 
1465 	if (ofmep) {
1466 		out(O_ALTFP|O_NONL, "[");
1467 		ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
1468 		out(O_ALTFP, " ADDING TO OVERFLOW FME]");
1469 		if (ffep)
1470 			fmd_case_add_ereport(hdl, ofmep->fmcase, ffep);
1471 
1472 		return;
1473 
1474 	} else if (Max_fme && (Open_fme_count >= Max_fme)) {
1475 		out(O_ALTFP|O_NONL, "[");
1476 		ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
1477 		out(O_ALTFP, " MAX OPEN FME REACHED]");
1478 
1479 		fmcase = fmd_case_open(hdl, NULL);
1480 
1481 		/* Create overflow fme */
1482 		if ((fmep = newfme(eventstring, ipp, hdl, fmcase)) == NULL) {
1483 			out(O_ALTFP|O_NONL, "[");
1484 			ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
1485 			out(O_ALTFP, " CANNOT OPEN OVERFLOW FME]");
1486 			publish_undiagnosable(hdl, ffep, fmcase);
1487 			return;
1488 		}
1489 
1490 		Open_fme_count++;
1491 
1492 		init_fme_bufs(fmep);
1493 		fmep->overflow = B_TRUE;
1494 
1495 		if (ffep)
1496 			fmd_case_add_ereport(hdl, fmep->fmcase, ffep);
1497 
1498 		defect = fmd_nvl_create_fault(hdl, UNDIAGNOSABLE_DEFECT, 100,
1499 		    NULL, NULL, NULL);
1500 		(void) nvlist_add_string(defect, UNDIAG_REASON, UD_MAXFME);
1501 		fmd_case_add_suspect(hdl, fmep->fmcase, defect);
1502 		fmd_case_solve(hdl, fmep->fmcase);
1503 		return;
1504 	}
1505 
1506 	/* open a case */
1507 	fmcase = fmd_case_open(hdl, NULL);
1508 
1509 	/* start a new FME */
1510 	if ((fmep = newfme(eventstring, ipp, hdl, fmcase)) == NULL) {
1511 		out(O_ALTFP|O_NONL, "[");
1512 		ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
1513 		out(O_ALTFP, " CANNOT DIAGNOSE]");
1514 		publish_undiagnosable(hdl, ffep, fmcase);
1515 		return;
1516 	}
1517 
1518 	Open_fme_count++;
1519 
1520 	init_fme_bufs(fmep);
1521 
1522 	out(O_ALTFP|O_NONL, "[");
1523 	ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
1524 	out(O_ALTFP, " created FME%d, case %s]", fmep->id,
1525 	    fmd_case_uuid(hdl, fmep->fmcase));
1526 
1527 	ep = fmep->e0;
1528 	ASSERT(ep != NULL);
1529 
1530 	/* note observation */
1531 	fmep->ecurrent = ep;
1532 	if (ep->count++ == 0) {
1533 		/* link it into list of observations seen */
1534 		ep->observations = fmep->observations;
1535 		fmep->observations = ep;
1536 		ep->nvp = evnv_dupnvl(nvl);
1537 		serialize_observation(fmep, eventstring, ipp);
1538 	} else {
1539 		/* new payload overrides any previous */
1540 		nvlist_free(ep->nvp);
1541 		ep->nvp = evnv_dupnvl(nvl);
1542 	}
1543 
1544 	stats_counter_bump(fmep->Rcount);
1545 
1546 	if (ffep) {
1547 		fmd_case_add_ereport(hdl, fmep->fmcase, ffep);
1548 		fmd_case_setprincipal(hdl, fmep->fmcase, ffep);
1549 		fmep->e0r = ffep;
1550 	}
1551 
1552 	/* give the diagnosis algorithm a shot at the new FME state */
1553 	fme_eval(fmep, ffep);
1554 }
1555 
1556 void
1557 fme_status(int flags)
1558 {
1559 	struct fme *fmep;
1560 
1561 	if (FMElist == NULL) {
1562 		out(flags, "No fault management exercises underway.");
1563 		return;
1564 	}
1565 
1566 	for (fmep = FMElist; fmep; fmep = fmep->next)
1567 		fme_print(flags, fmep);
1568 }
1569 
1570 /*
1571  * "indent" routines used mostly for nicely formatted debug output, but also
1572  * for sanity checking for infinite recursion bugs.
1573  */
1574 
1575 #define	MAX_INDENT 1024
1576 static const char *indent_s[MAX_INDENT];
1577 static int current_indent;
1578 
1579 static void
1580 indent_push(const char *s)
1581 {
1582 	if (current_indent < MAX_INDENT)
1583 		indent_s[current_indent++] = s;
1584 	else
1585 		out(O_DIE, "unexpected recursion depth (%d)", current_indent);
1586 }
1587 
1588 static void
1589 indent_set(const char *s)
1590 {
1591 	current_indent = 0;
1592 	indent_push(s);
1593 }
1594 
1595 static void
1596 indent_pop(void)
1597 {
1598 	if (current_indent > 0)
1599 		current_indent--;
1600 	else
1601 		out(O_DIE, "recursion underflow");
1602 }
1603 
1604 static void
1605 indent(void)
1606 {
1607 	int i;
1608 	if (!Verbose)
1609 		return;
1610 	for (i = 0; i < current_indent; i++)
1611 		out(O_ALTFP|O_VERB|O_NONL, indent_s[i]);
1612 }
1613 
1614 #define	SLNEW		1
1615 #define	SLCHANGED	2
1616 #define	SLWAIT		3
1617 #define	SLDISPROVED	4
1618 
1619 static void
1620 print_suspects(int circumstance, struct fme *fmep)
1621 {
1622 	struct event *ep;
1623 
1624 	out(O_ALTFP|O_NONL, "[");
1625 	if (circumstance == SLCHANGED) {
1626 		out(O_ALTFP|O_NONL, "FME%d diagnosis changed. state: %s, "
1627 		    "suspect list:", fmep->id, fme_state2str(fmep->state));
1628 	} else if (circumstance == SLWAIT) {
1629 		out(O_ALTFP|O_NONL, "FME%d set wait timer %ld ", fmep->id,
1630 		    fmep->timer);
1631 		ptree_timeval(O_ALTFP|O_NONL, &fmep->wull);
1632 	} else if (circumstance == SLDISPROVED) {
1633 		out(O_ALTFP|O_NONL, "FME%d DIAGNOSIS UNKNOWN", fmep->id);
1634 	} else {
1635 		out(O_ALTFP|O_NONL, "FME%d DIAGNOSIS PRODUCED:", fmep->id);
1636 	}
1637 
1638 	if (circumstance == SLWAIT || circumstance == SLDISPROVED) {
1639 		out(O_ALTFP, "]");
1640 		return;
1641 	}
1642 
1643 	for (ep = fmep->suspects; ep; ep = ep->suspects) {
1644 		out(O_ALTFP|O_NONL, " ");
1645 		itree_pevent_brief(O_ALTFP|O_NONL, ep);
1646 	}
1647 	out(O_ALTFP, "]");
1648 }
1649 
1650 static struct node *
1651 eventprop_lookup(struct event *ep, const char *propname)
1652 {
1653 	return (lut_lookup(ep->props, (void *)propname, NULL));
1654 }
1655 
1656 #define	MAXDIGITIDX	23
1657 static char numbuf[MAXDIGITIDX + 1];
1658 
1659 static int
1660 node2uint(struct node *n, uint_t *valp)
1661 {
1662 	struct evalue value;
1663 	struct lut *globals = NULL;
1664 
1665 	if (n == NULL)
1666 		return (1);
1667 
1668 	/*
1669 	 * check value.v since we are being asked to convert an unsigned
1670 	 * long long int to an unsigned int
1671 	 */
1672 	if (! eval_expr(n, NULL, NULL, &globals, NULL, NULL, 0, &value) ||
1673 	    value.t != UINT64 || value.v > (1ULL << 32))
1674 		return (1);
1675 
1676 	*valp = (uint_t)value.v;
1677 
1678 	return (0);
1679 }
1680 
1681 static nvlist_t *
1682 node2fmri(struct node *n)
1683 {
1684 	nvlist_t **pa, *f, *p;
1685 	struct node *nc;
1686 	uint_t depth = 0;
1687 	char *numstr, *nullbyte;
1688 	char *failure;
1689 	int err, i;
1690 
1691 	/* XXX do we need to be able to handle a non-T_NAME node? */
1692 	if (n == NULL || n->t != T_NAME)
1693 		return (NULL);
1694 
1695 	for (nc = n; nc != NULL; nc = nc->u.name.next) {
1696 		if (nc->u.name.child == NULL || nc->u.name.child->t != T_NUM)
1697 			break;
1698 		depth++;
1699 	}
1700 
1701 	if (nc != NULL) {
1702 		/* We bailed early, something went wrong */
1703 		return (NULL);
1704 	}
1705 
1706 	if ((err = nvlist_xalloc(&f, NV_UNIQUE_NAME, &Eft_nv_hdl)) != 0)
1707 		out(O_DIE|O_SYS, "alloc of fmri nvl failed");
1708 	pa = alloca(depth * sizeof (nvlist_t *));
1709 	for (i = 0; i < depth; i++)
1710 		pa[i] = NULL;
1711 
1712 	err = nvlist_add_string(f, FM_FMRI_SCHEME, FM_FMRI_SCHEME_HC);
1713 	err |= nvlist_add_uint8(f, FM_VERSION, FM_HC_SCHEME_VERSION);
1714 	err |= nvlist_add_string(f, FM_FMRI_HC_ROOT, "");
1715 	err |= nvlist_add_uint32(f, FM_FMRI_HC_LIST_SZ, depth);
1716 	if (err != 0) {
1717 		failure = "basic construction of FMRI failed";
1718 		goto boom;
1719 	}
1720 
1721 	numbuf[MAXDIGITIDX] = '\0';
1722 	nullbyte = &numbuf[MAXDIGITIDX];
1723 	i = 0;
1724 
1725 	for (nc = n; nc != NULL; nc = nc->u.name.next) {
1726 		err = nvlist_xalloc(&p, NV_UNIQUE_NAME, &Eft_nv_hdl);
1727 		if (err != 0) {
1728 			failure = "alloc of an hc-pair failed";
1729 			goto boom;
1730 		}
1731 		err = nvlist_add_string(p, FM_FMRI_HC_NAME, nc->u.name.s);
1732 		numstr = ulltostr(nc->u.name.child->u.ull, nullbyte);
1733 		err |= nvlist_add_string(p, FM_FMRI_HC_ID, numstr);
1734 		if (err != 0) {
1735 			failure = "construction of an hc-pair failed";
1736 			goto boom;
1737 		}
1738 		pa[i++] = p;
1739 	}
1740 
1741 	err = nvlist_add_nvlist_array(f, FM_FMRI_HC_LIST, pa, depth);
1742 	if (err == 0) {
1743 		for (i = 0; i < depth; i++)
1744 			if (pa[i] != NULL)
1745 				nvlist_free(pa[i]);
1746 		return (f);
1747 	}
1748 	failure = "addition of hc-pair array to FMRI failed";
1749 
1750 boom:
1751 	for (i = 0; i < depth; i++)
1752 		if (pa[i] != NULL)
1753 			nvlist_free(pa[i]);
1754 	nvlist_free(f);
1755 	out(O_DIE, "%s", failure);
1756 	/*NOTREACHED*/
1757 	return (NULL);
1758 }
1759 
1760 /* an ipath cache entry is an array of these, with s==NULL at the end */
1761 struct ipath {
1762 	const char *s;	/* component name (in stable) */
1763 	int i;		/* instance number */
1764 };
1765 
1766 static nvlist_t *
1767 ipath2fmri(struct ipath *ipath)
1768 {
1769 	nvlist_t **pa, *f, *p;
1770 	uint_t depth = 0;
1771 	char *numstr, *nullbyte;
1772 	char *failure;
1773 	int err, i;
1774 	struct ipath *ipp;
1775 
1776 	for (ipp = ipath; ipp->s != NULL; ipp++)
1777 		depth++;
1778 
1779 	if ((err = nvlist_xalloc(&f, NV_UNIQUE_NAME, &Eft_nv_hdl)) != 0)
1780 		out(O_DIE|O_SYS, "alloc of fmri nvl failed");
1781 	pa = alloca(depth * sizeof (nvlist_t *));
1782 	for (i = 0; i < depth; i++)
1783 		pa[i] = NULL;
1784 
1785 	err = nvlist_add_string(f, FM_FMRI_SCHEME, FM_FMRI_SCHEME_HC);
1786 	err |= nvlist_add_uint8(f, FM_VERSION, FM_HC_SCHEME_VERSION);
1787 	err |= nvlist_add_string(f, FM_FMRI_HC_ROOT, "");
1788 	err |= nvlist_add_uint32(f, FM_FMRI_HC_LIST_SZ, depth);
1789 	if (err != 0) {
1790 		failure = "basic construction of FMRI failed";
1791 		goto boom;
1792 	}
1793 
1794 	numbuf[MAXDIGITIDX] = '\0';
1795 	nullbyte = &numbuf[MAXDIGITIDX];
1796 	i = 0;
1797 
1798 	for (ipp = ipath; ipp->s != NULL; ipp++) {
1799 		err = nvlist_xalloc(&p, NV_UNIQUE_NAME, &Eft_nv_hdl);
1800 		if (err != 0) {
1801 			failure = "alloc of an hc-pair failed";
1802 			goto boom;
1803 		}
1804 		err = nvlist_add_string(p, FM_FMRI_HC_NAME, ipp->s);
1805 		numstr = ulltostr(ipp->i, nullbyte);
1806 		err |= nvlist_add_string(p, FM_FMRI_HC_ID, numstr);
1807 		if (err != 0) {
1808 			failure = "construction of an hc-pair failed";
1809 			goto boom;
1810 		}
1811 		pa[i++] = p;
1812 	}
1813 
1814 	err = nvlist_add_nvlist_array(f, FM_FMRI_HC_LIST, pa, depth);
1815 	if (err == 0) {
1816 		for (i = 0; i < depth; i++)
1817 			if (pa[i] != NULL)
1818 				nvlist_free(pa[i]);
1819 		return (f);
1820 	}
1821 	failure = "addition of hc-pair array to FMRI failed";
1822 
1823 boom:
1824 	for (i = 0; i < depth; i++)
1825 		if (pa[i] != NULL)
1826 			nvlist_free(pa[i]);
1827 	nvlist_free(f);
1828 	out(O_DIE, "%s", failure);
1829 	/*NOTREACHED*/
1830 	return (NULL);
1831 }
1832 
1833 static uint_t
1834 avg(uint_t sum, uint_t cnt)
1835 {
1836 	unsigned long long s = sum * 10;
1837 
1838 	return ((s / cnt / 10) + (((s / cnt % 10) >= 5) ? 1 : 0));
1839 }
1840 
1841 static uint8_t
1842 percentof(uint_t part, uint_t whole)
1843 {
1844 	unsigned long long p = part * 1000;
1845 
1846 	return ((p / whole / 10) + (((p / whole % 10) >= 5) ? 1 : 0));
1847 }
1848 
1849 struct rsl {
1850 	struct event *suspect;
1851 	nvlist_t *asru;
1852 	nvlist_t *fru;
1853 	nvlist_t *rsrc;
1854 };
1855 
1856 /*
1857  *  rslfree -- free internal members of struct rsl not expected to be
1858  *	freed elsewhere.
1859  */
1860 static void
1861 rslfree(struct rsl *freeme)
1862 {
1863 	if (freeme->asru != NULL)
1864 		nvlist_free(freeme->asru);
1865 	if (freeme->fru != NULL)
1866 		nvlist_free(freeme->fru);
1867 	if (freeme->rsrc != NULL && freeme->rsrc != freeme->asru)
1868 		nvlist_free(freeme->rsrc);
1869 }
1870 
1871 /*
1872  *  rslcmp -- compare two rsl structures.  Use the following
1873  *	comparisons to establish cardinality:
1874  *
1875  *	1. Name of the suspect's class. (simple strcmp)
1876  *	2. Name of the suspect's ASRU. (trickier, since nvlist)
1877  *
1878  */
1879 static int
1880 rslcmp(const void *a, const void *b)
1881 {
1882 	struct rsl *r1 = (struct rsl *)a;
1883 	struct rsl *r2 = (struct rsl *)b;
1884 	int rv;
1885 
1886 	rv = strcmp(r1->suspect->enode->u.event.ename->u.name.s,
1887 	    r2->suspect->enode->u.event.ename->u.name.s);
1888 	if (rv != 0)
1889 		return (rv);
1890 
1891 	if (r1->asru == NULL && r2->asru == NULL)
1892 		return (0);
1893 	if (r1->asru == NULL)
1894 		return (-1);
1895 	if (r2->asru == NULL)
1896 		return (1);
1897 	return (evnv_cmpnvl(r1->asru, r2->asru, 0));
1898 }
1899 
1900 /*
1901  *  rsluniq -- given an array of rsl structures, seek out and "remove"
1902  *	any duplicates.  Dups are "remove"d by NULLing the suspect pointer
1903  *	of the array element.  Removal also means updating the number of
1904  *	problems and the number of problems which are not faults.  User
1905  *	provides the first and last element pointers.
1906  */
1907 static void
1908 rsluniq(struct rsl *first, struct rsl *last, int *nprobs, int *nnonf)
1909 {
1910 	struct rsl *cr;
1911 
1912 	if (*nprobs == 1)
1913 		return;
1914 
1915 	/*
1916 	 *  At this point, we only expect duplicate defects.
1917 	 *  Eversholt's diagnosis algorithm prevents duplicate
1918 	 *  suspects, but we rewrite defects in the platform code after
1919 	 *  the diagnosis is made, and that can introduce new
1920 	 *  duplicates.
1921 	 */
1922 	while (first <= last) {
1923 		if (first->suspect == NULL || !is_defect(first->suspect->t)) {
1924 			first++;
1925 			continue;
1926 		}
1927 		cr = first + 1;
1928 		while (cr <= last) {
1929 			if (is_defect(first->suspect->t)) {
1930 				if (rslcmp(first, cr) == 0) {
1931 					cr->suspect = NULL;
1932 					rslfree(cr);
1933 					(*nprobs)--;
1934 					(*nnonf)--;
1935 				}
1936 			}
1937 			/*
1938 			 * assume all defects are in order after our
1939 			 * sort and short circuit here with "else break" ?
1940 			 */
1941 			cr++;
1942 		}
1943 		first++;
1944 	}
1945 }
1946 
1947 /*
1948  * get_resources -- for a given suspect, determine what ASRU, FRU and
1949  *     RSRC nvlists should be advertised in the final suspect list.
1950  */
1951 void
1952 get_resources(struct event *sp, struct rsl *rsrcs, struct config *croot)
1953 {
1954 	struct node *asrudef, *frudef;
1955 	nvlist_t *asru, *fru;
1956 	nvlist_t *rsrc = NULL;
1957 	char *pathstr;
1958 
1959 	/*
1960 	 * First find any ASRU and/or FRU defined in the
1961 	 * initial fault tree.
1962 	 */
1963 	asrudef = eventprop_lookup(sp, L_ASRU);
1964 	frudef = eventprop_lookup(sp, L_FRU);
1965 
1966 	/*
1967 	 * Create FMRIs based on those definitions
1968 	 */
1969 	asru = node2fmri(asrudef);
1970 	fru = node2fmri(frudef);
1971 	pathstr = ipath2str(NULL, sp->ipp);
1972 
1973 	/*
1974 	 * Allow for platform translations of the FMRIs
1975 	 */
1976 	platform_units_translate(is_defect(sp->t), croot, &asru, &fru, &rsrc,
1977 	    pathstr);
1978 
1979 	FREE(pathstr);
1980 	rsrcs->suspect = sp;
1981 	rsrcs->asru = asru;
1982 	rsrcs->fru = fru;
1983 	rsrcs->rsrc = rsrc;
1984 }
1985 
1986 /*
1987  * trim_suspects -- prior to publishing, we may need to remove some
1988  *    suspects from the list.  If we're auto-closing upsets, we don't
1989  *    want any of those in the published list.  If the ASRUs for multiple
1990  *    defects resolve to the same ASRU (driver) we only want to publish
1991  *    that as a single suspect.
1992  */
1993 static void
1994 trim_suspects(struct fme *fmep, boolean_t no_upsets, struct rsl **begin,
1995     struct rsl **end)
1996 {
1997 	struct event *ep;
1998 	struct rsl *rp;
1999 	int rpcnt;
2000 
2001 	/*
2002 	 * First save the suspects in the psuspects, then copy back
2003 	 * only the ones we wish to retain.  This resets nsuspects to
2004 	 * zero.
2005 	 */
2006 	rpcnt = fmep->nsuspects;
2007 	save_suspects(fmep);
2008 
2009 	/*
2010 	 * allocate an array of resource pointers for the suspects.
2011 	 * We may end up using less than the full allocation, but this
2012 	 * is a very short-lived array.  publish_suspects() will free
2013 	 * this array when it's done using it.
2014 	 */
2015 	rp = *begin = MALLOC(rpcnt * sizeof (struct rsl));
2016 	bzero(rp, rpcnt * sizeof (struct rsl));
2017 
2018 	/* first pass, remove any unwanted upsets and populate our array */
2019 	for (ep = fmep->psuspects; ep; ep = ep->psuspects) {
2020 		if (no_upsets && is_upset(ep->t))
2021 			continue;
2022 		get_resources(ep, rp, fmep->config);
2023 		rp++;
2024 		fmep->nsuspects++;
2025 		if (!is_fault(ep->t))
2026 			fmep->nonfault++;
2027 	}
2028 
2029 	/* if all we had was unwanted upsets, we're done */
2030 	if (fmep->nsuspects == 0)
2031 		return;
2032 
2033 	*end = rp - 1;
2034 
2035 	/* sort the array */
2036 	qsort(*begin, fmep->nsuspects, sizeof (struct rsl), rslcmp);
2037 	rsluniq(*begin, *end, &fmep->nsuspects, &fmep->nonfault);
2038 }
2039 
2040 /*
2041  * addpayloadprop -- add a payload prop to a problem
2042  */
2043 static void
2044 addpayloadprop(const char *lhs, struct evalue *rhs, nvlist_t *fault)
2045 {
2046 	ASSERT(fault != NULL);
2047 	ASSERT(lhs != NULL);
2048 	ASSERT(rhs != NULL);
2049 
2050 	if (rhs->t == UINT64) {
2051 		out(O_ALTFP|O_VERB2, "addpayloadprop: %s=%llu", lhs, rhs->v);
2052 
2053 		if (nvlist_add_uint64(fault, lhs, rhs->v) != 0)
2054 			out(O_DIE,
2055 			    "cannot add payloadprop \"%s\" to fault", lhs);
2056 	} else {
2057 		out(O_ALTFP|O_VERB2, "addpayloadprop: %s=\"%s\"",
2058 		    lhs, (char *)(uintptr_t)rhs->v);
2059 
2060 		if (nvlist_add_string(fault, lhs, (char *)(uintptr_t)rhs->v) !=
2061 		    0)
2062 			out(O_DIE,
2063 			    "cannot add payloadprop \"%s\" to fault", lhs);
2064 	}
2065 }
2066 
2067 static char *Istatbuf;
2068 static char *Istatbufptr;
2069 static int Istatsz;
2070 
2071 /*
2072  * istataddsize -- calculate size of istat and add it to Istatsz
2073  */
2074 /*ARGSUSED2*/
2075 static void
2076 istataddsize(const struct istat_entry *lhs, struct stats *rhs, void *arg)
2077 {
2078 	int val;
2079 
2080 	ASSERT(lhs != NULL);
2081 	ASSERT(rhs != NULL);
2082 
2083 	if ((val = stats_counter_value(rhs)) == 0)
2084 		return;	/* skip zero-valued stats */
2085 
2086 	/* count up the size of the stat name */
2087 	Istatsz += ipath2strlen(lhs->ename, lhs->ipath);
2088 	Istatsz++;	/* for the trailing NULL byte */
2089 
2090 	/* count up the size of the stat value */
2091 	Istatsz += snprintf(NULL, 0, "%d", val);
2092 	Istatsz++;	/* for the trailing NULL byte */
2093 }
2094 
2095 /*
2096  * istat2str -- serialize an istat, writing result to *Istatbufptr
2097  */
2098 /*ARGSUSED2*/
2099 static void
2100 istat2str(const struct istat_entry *lhs, struct stats *rhs, void *arg)
2101 {
2102 	char *str;
2103 	int len;
2104 	int val;
2105 
2106 	ASSERT(lhs != NULL);
2107 	ASSERT(rhs != NULL);
2108 
2109 	if ((val = stats_counter_value(rhs)) == 0)
2110 		return;	/* skip zero-valued stats */
2111 
2112 	/* serialize the stat name */
2113 	str = ipath2str(lhs->ename, lhs->ipath);
2114 	len = strlen(str);
2115 
2116 	ASSERT(Istatbufptr + len + 1 < &Istatbuf[Istatsz]);
2117 	(void) strlcpy(Istatbufptr, str, &Istatbuf[Istatsz] - Istatbufptr);
2118 	Istatbufptr += len;
2119 	FREE(str);
2120 	*Istatbufptr++ = '\0';
2121 
2122 	/* serialize the stat value */
2123 	Istatbufptr += snprintf(Istatbufptr, &Istatbuf[Istatsz] - Istatbufptr,
2124 	    "%d", val);
2125 	*Istatbufptr++ = '\0';
2126 
2127 	ASSERT(Istatbufptr <= &Istatbuf[Istatsz]);
2128 }
2129 
2130 void
2131 istat_save()
2132 {
2133 	if (Istat_need_save == 0)
2134 		return;
2135 
2136 	/* figure out how big the serialzed info is */
2137 	Istatsz = 0;
2138 	lut_walk(Istats, (lut_cb)istataddsize, NULL);
2139 
2140 	if (Istatsz == 0) {
2141 		/* no stats to save */
2142 		fmd_buf_destroy(Hdl, NULL, WOBUF_ISTATS);
2143 		return;
2144 	}
2145 
2146 	/* create the serialized buffer */
2147 	Istatbufptr = Istatbuf = MALLOC(Istatsz);
2148 	lut_walk(Istats, (lut_cb)istat2str, NULL);
2149 
2150 	/* clear out current saved stats */
2151 	fmd_buf_destroy(Hdl, NULL, WOBUF_ISTATS);
2152 
2153 	/* write out the new version */
2154 	fmd_buf_write(Hdl, NULL, WOBUF_ISTATS, Istatbuf, Istatsz);
2155 	FREE(Istatbuf);
2156 
2157 	Istat_need_save = 0;
2158 }
2159 
2160 int
2161 istat_cmp(struct istat_entry *ent1, struct istat_entry *ent2)
2162 {
2163 	if (ent1->ename != ent2->ename)
2164 		return (ent2->ename - ent1->ename);
2165 	if (ent1->ipath != ent2->ipath)
2166 		return ((char *)ent2->ipath - (char *)ent1->ipath);
2167 
2168 	return (0);
2169 }
2170 
2171 /*
2172  * istat-verify -- verify the component associated with a stat still exists
2173  *
2174  * if the component no longer exists, this routine resets the stat and
2175  * returns 0.  if the component still exists, it returns 1.
2176  */
2177 static int
2178 istat_verify(struct node *snp, struct istat_entry *entp)
2179 {
2180 	struct stats *statp;
2181 	nvlist_t *fmri;
2182 
2183 	fmri = node2fmri(snp->u.event.epname);
2184 	if (platform_path_exists(fmri)) {
2185 		nvlist_free(fmri);
2186 		return (1);
2187 	}
2188 	nvlist_free(fmri);
2189 
2190 	/* component no longer in system.  zero out the associated stats */
2191 	if ((statp = (struct stats *)
2192 	    lut_lookup(Istats, entp, (lut_cmp)istat_cmp)) == NULL ||
2193 	    stats_counter_value(statp) == 0)
2194 		return (0);	/* stat is already reset */
2195 
2196 	Istat_need_save = 1;
2197 	stats_counter_reset(statp);
2198 	return (0);
2199 }
2200 
2201 static void
2202 istat_bump(struct node *snp, int n)
2203 {
2204 	struct stats *statp;
2205 	struct istat_entry ent;
2206 
2207 	ASSERT(snp != NULL);
2208 	ASSERTinfo(snp->t == T_EVENT, ptree_nodetype2str(snp->t));
2209 	ASSERT(snp->u.event.epname != NULL);
2210 
2211 	/* class name should be hoisted into a single stable entry */
2212 	ASSERT(snp->u.event.ename->u.name.next == NULL);
2213 	ent.ename = snp->u.event.ename->u.name.s;
2214 	ent.ipath = ipath(snp->u.event.epname);
2215 
2216 	if (!istat_verify(snp, &ent)) {
2217 		/* component no longer exists in system, nothing to do */
2218 		return;
2219 	}
2220 
2221 	if ((statp = (struct stats *)
2222 	    lut_lookup(Istats, &ent, (lut_cmp)istat_cmp)) == NULL) {
2223 		/* need to create the counter */
2224 		int cnt = 0;
2225 		struct node *np;
2226 		char *sname;
2227 		char *snamep;
2228 		struct istat_entry *newentp;
2229 
2230 		/* count up the size of the stat name */
2231 		np = snp->u.event.ename;
2232 		while (np != NULL) {
2233 			cnt += strlen(np->u.name.s);
2234 			cnt++;	/* for the '.' or '@' */
2235 			np = np->u.name.next;
2236 		}
2237 		np = snp->u.event.epname;
2238 		while (np != NULL) {
2239 			cnt += snprintf(NULL, 0, "%s%llu",
2240 			    np->u.name.s, np->u.name.child->u.ull);
2241 			cnt++;	/* for the '/' or trailing NULL byte */
2242 			np = np->u.name.next;
2243 		}
2244 
2245 		/* build the stat name */
2246 		snamep = sname = alloca(cnt);
2247 		np = snp->u.event.ename;
2248 		while (np != NULL) {
2249 			snamep += snprintf(snamep, &sname[cnt] - snamep,
2250 			    "%s", np->u.name.s);
2251 			np = np->u.name.next;
2252 			if (np)
2253 				*snamep++ = '.';
2254 		}
2255 		*snamep++ = '@';
2256 		np = snp->u.event.epname;
2257 		while (np != NULL) {
2258 			snamep += snprintf(snamep, &sname[cnt] - snamep,
2259 			    "%s%llu", np->u.name.s, np->u.name.child->u.ull);
2260 			np = np->u.name.next;
2261 			if (np)
2262 				*snamep++ = '/';
2263 		}
2264 		*snamep++ = '\0';
2265 
2266 		/* create the new stat & add it to our list */
2267 		newentp = MALLOC(sizeof (*newentp));
2268 		*newentp = ent;
2269 		statp = stats_new_counter(NULL, sname, 0);
2270 		Istats = lut_add(Istats, (void *)newentp, (void *)statp,
2271 		    (lut_cmp)istat_cmp);
2272 	}
2273 
2274 	/* if n is non-zero, set that value instead of bumping */
2275 	if (n) {
2276 		stats_counter_reset(statp);
2277 		stats_counter_add(statp, n);
2278 	} else
2279 		stats_counter_bump(statp);
2280 	Istat_need_save = 1;
2281 
2282 	ipath_print(O_ALTFP|O_VERB2, ent.ename, ent.ipath);
2283 	out(O_ALTFP|O_VERB2, " %s to value %d", n ? "set" : "incremented",
2284 	    stats_counter_value(statp));
2285 }
2286 
2287 /*ARGSUSED*/
2288 static void
2289 istat_destructor(void *left, void *right, void *arg)
2290 {
2291 	struct istat_entry *entp = (struct istat_entry *)left;
2292 	struct stats *statp = (struct stats *)right;
2293 	FREE(entp);
2294 	stats_delete(statp);
2295 }
2296 
2297 /*
2298  * Callback used in a walk of the Istats to reset matching stat counters.
2299  */
2300 static void
2301 istat_counter_reset_cb(struct istat_entry *entp, struct stats *statp,
2302     const struct ipath *ipp)
2303 {
2304 	char *path;
2305 
2306 	if (entp->ipath == ipp) {
2307 		path = ipath2str(entp->ename, ipp);
2308 		out(O_ALTFP, "istat_counter_reset_cb: resetting %s", path);
2309 		FREE(path);
2310 		stats_counter_reset(statp);
2311 		Istat_need_save = 1;
2312 	}
2313 }
2314 
2315 /*ARGSUSED*/
2316 static void
2317 istat_counter_topo_chg_cb(struct istat_entry *entp, struct stats *statp,
2318     void *unused)
2319 {
2320 	char *path;
2321 	nvlist_t *fmri;
2322 
2323 	fmri = ipath2fmri((struct ipath *)(entp->ipath));
2324 	if (!platform_path_exists(fmri)) {
2325 		path = ipath2str(entp->ename, entp->ipath);
2326 		out(O_ALTFP, "istat_counter_topo_chg_cb: not present %s", path);
2327 		FREE(path);
2328 		stats_counter_reset(statp);
2329 		Istat_need_save = 1;
2330 	}
2331 	nvlist_free(fmri);
2332 }
2333 
2334 void
2335 istat_fini(void)
2336 {
2337 	lut_free(Istats, istat_destructor, NULL);
2338 }
2339 
2340 static char *Serdbuf;
2341 static char *Serdbufptr;
2342 static int Serdsz;
2343 
2344 /*
2345  * serdaddsize -- calculate size of serd and add it to Serdsz
2346  */
2347 /*ARGSUSED*/
2348 static void
2349 serdaddsize(const struct serd_entry *lhs, struct stats *rhs, void *arg)
2350 {
2351 	ASSERT(lhs != NULL);
2352 
2353 	/* count up the size of the stat name */
2354 	Serdsz += ipath2strlen(lhs->ename, lhs->ipath);
2355 	Serdsz++;	/* for the trailing NULL byte */
2356 }
2357 
2358 /*
2359  * serd2str -- serialize a serd engine, writing result to *Serdbufptr
2360  */
2361 /*ARGSUSED*/
2362 static void
2363 serd2str(const struct serd_entry *lhs, struct stats *rhs, void *arg)
2364 {
2365 	char *str;
2366 	int len;
2367 
2368 	ASSERT(lhs != NULL);
2369 
2370 	/* serialize the serd engine name */
2371 	str = ipath2str(lhs->ename, lhs->ipath);
2372 	len = strlen(str);
2373 
2374 	ASSERT(Serdbufptr + len + 1 <= &Serdbuf[Serdsz]);
2375 	(void) strlcpy(Serdbufptr, str, &Serdbuf[Serdsz] - Serdbufptr);
2376 	Serdbufptr += len;
2377 	FREE(str);
2378 	*Serdbufptr++ = '\0';
2379 	ASSERT(Serdbufptr <= &Serdbuf[Serdsz]);
2380 }
2381 
2382 void
2383 serd_save()
2384 {
2385 	if (Serd_need_save == 0)
2386 		return;
2387 
2388 	/* figure out how big the serialzed info is */
2389 	Serdsz = 0;
2390 	lut_walk(SerdEngines, (lut_cb)serdaddsize, NULL);
2391 
2392 	if (Serdsz == 0) {
2393 		/* no serd engines to save */
2394 		fmd_buf_destroy(Hdl, NULL, WOBUF_SERDS);
2395 		return;
2396 	}
2397 
2398 	/* create the serialized buffer */
2399 	Serdbufptr = Serdbuf = MALLOC(Serdsz);
2400 	lut_walk(SerdEngines, (lut_cb)serd2str, NULL);
2401 
2402 	/* clear out current saved stats */
2403 	fmd_buf_destroy(Hdl, NULL, WOBUF_SERDS);
2404 
2405 	/* write out the new version */
2406 	fmd_buf_write(Hdl, NULL, WOBUF_SERDS, Serdbuf, Serdsz);
2407 	FREE(Serdbuf);
2408 	Serd_need_save = 0;
2409 }
2410 
2411 int
2412 serd_cmp(struct serd_entry *ent1, struct serd_entry *ent2)
2413 {
2414 	if (ent1->ename != ent2->ename)
2415 		return (ent2->ename - ent1->ename);
2416 	if (ent1->ipath != ent2->ipath)
2417 		return ((char *)ent2->ipath - (char *)ent1->ipath);
2418 
2419 	return (0);
2420 }
2421 
2422 void
2423 fme_serd_load(fmd_hdl_t *hdl)
2424 {
2425 	int sz;
2426 	char *sbuf;
2427 	char *sepptr;
2428 	char *ptr;
2429 	struct serd_entry *newentp;
2430 	struct node *epname;
2431 	nvlist_t *fmri;
2432 	char *namestring;
2433 
2434 	if ((sz = fmd_buf_size(hdl, NULL, WOBUF_SERDS)) == 0)
2435 		return;
2436 	sbuf = alloca(sz);
2437 	fmd_buf_read(hdl, NULL, WOBUF_SERDS, sbuf, sz);
2438 	ptr = sbuf;
2439 	while (ptr < &sbuf[sz]) {
2440 		sepptr = strchr(ptr, '@');
2441 		*sepptr = '\0';
2442 		namestring = ptr;
2443 		sepptr++;
2444 		ptr = sepptr;
2445 		ptr += strlen(ptr);
2446 		ptr++;	/* move past the '\0' separating paths */
2447 		epname = pathstring2epnamenp(sepptr);
2448 		fmri = node2fmri(epname);
2449 		if (platform_path_exists(fmri)) {
2450 			newentp = MALLOC(sizeof (*newentp));
2451 			newentp->hdl = hdl;
2452 			newentp->ipath = ipath(epname);
2453 			newentp->ename = stable(namestring);
2454 			SerdEngines = lut_add(SerdEngines, (void *)newentp,
2455 			    (void *)newentp, (lut_cmp)serd_cmp);
2456 		} else
2457 			Serd_need_save = 1;
2458 		tree_free(epname);
2459 		nvlist_free(fmri);
2460 	}
2461 	/* save it back again in case some of the paths no longer exist */
2462 	serd_save();
2463 }
2464 
2465 /*ARGSUSED*/
2466 static void
2467 serd_destructor(void *left, void *right, void *arg)
2468 {
2469 	struct serd_entry *entp = (struct serd_entry *)left;
2470 	FREE(entp);
2471 }
2472 
2473 /*
2474  * Callback used in a walk of the SerdEngines to reset matching serd engines.
2475  */
2476 /*ARGSUSED*/
2477 static void
2478 serd_reset_cb(struct serd_entry *entp, void *unused, const struct ipath *ipp)
2479 {
2480 	char *path;
2481 
2482 	if (entp->ipath == ipp) {
2483 		path = ipath2str(entp->ename, ipp);
2484 		out(O_ALTFP, "serd_reset_cb: resetting %s", path);
2485 		fmd_serd_reset(entp->hdl, path);
2486 		FREE(path);
2487 		Serd_need_save = 1;
2488 	}
2489 }
2490 
2491 /*ARGSUSED*/
2492 static void
2493 serd_topo_chg_cb(struct serd_entry *entp, void *unused, void *unused2)
2494 {
2495 	char *path;
2496 	nvlist_t *fmri;
2497 
2498 	fmri = ipath2fmri((struct ipath *)(entp->ipath));
2499 	if (!platform_path_exists(fmri)) {
2500 		path = ipath2str(entp->ename, entp->ipath);
2501 		out(O_ALTFP, "serd_topo_chg_cb: not present %s", path);
2502 		fmd_serd_reset(entp->hdl, path);
2503 		FREE(path);
2504 		Serd_need_save = 1;
2505 	}
2506 	nvlist_free(fmri);
2507 }
2508 
2509 void
2510 serd_fini(void)
2511 {
2512 	lut_free(SerdEngines, serd_destructor, NULL);
2513 }
2514 
2515 static void
2516 publish_suspects(struct fme *fmep)
2517 {
2518 	struct rsl *srl = NULL;
2519 	struct rsl *erl;
2520 	struct rsl *rp;
2521 	nvlist_t *fault;
2522 	uint8_t cert;
2523 	uint_t *frs;
2524 	uint_t fravg, frsum, fr;
2525 	uint_t messval;
2526 	struct node *snp;
2527 	int frcnt, fridx;
2528 	boolean_t no_upsets = B_FALSE;
2529 	boolean_t allfaulty = B_TRUE;
2530 
2531 	stats_counter_bump(fmep->diags);
2532 
2533 	/*
2534 	 * If we're auto-closing upsets, we don't want to include them
2535 	 * in any produced suspect lists or certainty accounting.
2536 	 */
2537 	if (Autoclose != NULL)
2538 		if (strcmp(Autoclose, "true") == 0 ||
2539 		    strcmp(Autoclose, "all") == 0 ||
2540 		    strcmp(Autoclose, "upsets") == 0)
2541 			no_upsets = B_TRUE;
2542 
2543 	trim_suspects(fmep, no_upsets, &srl, &erl);
2544 
2545 	/*
2546 	 * If the resulting suspect list has no members, we're
2547 	 * done.  Returning here will simply close the case.
2548 	 */
2549 	if (fmep->nsuspects == 0) {
2550 		out(O_ALTFP,
2551 		    "[FME%d, case %s (all suspects are upsets)]",
2552 		    fmep->id, fmd_case_uuid(fmep->hdl, fmep->fmcase));
2553 		FREE(srl);
2554 		restore_suspects(fmep);
2555 		return;
2556 	}
2557 
2558 	/*
2559 	 * If the suspect list is all faults, then for a given fault,
2560 	 * say X of N, X's certainty is computed via:
2561 	 *
2562 	 * fitrate(X) / (fitrate(1) + ... + fitrate(N)) * 100
2563 	 *
2564 	 * If none of the suspects are faults, and there are N suspects,
2565 	 * the certainty of a given suspect is 100/N.
2566 	 *
2567 	 * If there are are a mixture of faults and other problems in
2568 	 * the suspect list, we take an average of the faults'
2569 	 * FITrates and treat this average as the FITrate for any
2570 	 * non-faults.  The fitrate of any given suspect is then
2571 	 * computed per the first formula above.
2572 	 */
2573 	if (fmep->nonfault == fmep->nsuspects) {
2574 		/* NO faults in the suspect list */
2575 		cert = percentof(1, fmep->nsuspects);
2576 	} else {
2577 		/* sum the fitrates */
2578 		frs = alloca(fmep->nsuspects * sizeof (uint_t));
2579 		fridx = frcnt = frsum = 0;
2580 
2581 		for (rp = srl; rp <= erl; rp++) {
2582 			struct node *n;
2583 
2584 			if (rp->suspect == NULL)
2585 				continue;
2586 			if (!is_fault(rp->suspect->t)) {
2587 				frs[fridx++] = 0;
2588 				continue;
2589 			}
2590 			n = eventprop_lookup(rp->suspect, L_FITrate);
2591 			if (node2uint(n, &fr) != 0) {
2592 				out(O_DEBUG|O_NONL, "event ");
2593 				ipath_print(O_DEBUG|O_NONL,
2594 				    rp->suspect->enode->u.event.ename->u.name.s,
2595 				    rp->suspect->ipp);
2596 				out(O_DEBUG, " has no FITrate (using 1)");
2597 				fr = 1;
2598 			} else if (fr == 0) {
2599 				out(O_DEBUG|O_NONL, "event ");
2600 				ipath_print(O_DEBUG|O_NONL,
2601 				    rp->suspect->enode->u.event.ename->u.name.s,
2602 				    rp->suspect->ipp);
2603 				out(O_DEBUG, " has zero FITrate (using 1)");
2604 				fr = 1;
2605 			}
2606 
2607 			frs[fridx++] = fr;
2608 			frsum += fr;
2609 			frcnt++;
2610 		}
2611 		fravg = avg(frsum, frcnt);
2612 		for (fridx = 0; fridx < fmep->nsuspects; fridx++)
2613 			if (frs[fridx] == 0) {
2614 				frs[fridx] = fravg;
2615 				frsum += fravg;
2616 			}
2617 	}
2618 
2619 	/* Add them in reverse order of our sort, as fmd reverses order */
2620 	for (rp = erl; rp >= srl; rp--) {
2621 		if (rp->suspect == NULL)
2622 			continue;
2623 		if (!is_fault(rp->suspect->t))
2624 			allfaulty = B_FALSE;
2625 		if (fmep->nonfault != fmep->nsuspects)
2626 			cert = percentof(frs[--fridx], frsum);
2627 		fault = fmd_nvl_create_fault(fmep->hdl,
2628 		    rp->suspect->enode->u.event.ename->u.name.s,
2629 		    cert,
2630 		    rp->asru,
2631 		    rp->fru,
2632 		    rp->rsrc);
2633 		if (fault == NULL)
2634 			out(O_DIE, "fault creation failed");
2635 		/* if "message" property exists, add it to the fault */
2636 		if (node2uint(eventprop_lookup(rp->suspect, L_message),
2637 		    &messval) == 0) {
2638 
2639 			out(O_ALTFP,
2640 			    "[FME%d, %s adds message=%d to suspect list]",
2641 			    fmep->id,
2642 			    rp->suspect->enode->u.event.ename->u.name.s,
2643 			    messval);
2644 			if (nvlist_add_boolean_value(fault,
2645 			    FM_SUSPECT_MESSAGE,
2646 			    (messval) ? B_TRUE : B_FALSE) != 0) {
2647 				out(O_DIE, "cannot add no-message to fault");
2648 			}
2649 		}
2650 		/* add any payload properties */
2651 		lut_walk(rp->suspect->payloadprops,
2652 		    (lut_cb)addpayloadprop, (void *)fault);
2653 		fmd_case_add_suspect(fmep->hdl, fmep->fmcase, fault);
2654 		rslfree(rp);
2655 
2656 		/*
2657 		 * If "action" property exists, evaluate it;  this must be done
2658 		 * before the dupclose check below since some actions may
2659 		 * modify the asru to be used in fmd_nvl_fmri_faulty.  This
2660 		 * needs to be restructured if any new actions are introduced
2661 		 * that have effects that we do not want to be visible if
2662 		 * we decide not to publish in the dupclose check below.
2663 		 */
2664 		if ((snp = eventprop_lookup(rp->suspect, L_action)) != NULL) {
2665 			struct evalue evalue;
2666 
2667 			out(O_ALTFP|O_NONL,
2668 			    "[FME%d, %s action ", fmep->id,
2669 			    rp->suspect->enode->u.event.ename->u.name.s);
2670 			ptree_name_iter(O_ALTFP|O_NONL, snp);
2671 			out(O_ALTFP, "]");
2672 			Action_nvl = fault;
2673 			(void) eval_expr(snp, NULL, NULL, NULL, NULL,
2674 			    NULL, 0, &evalue);
2675 		}
2676 
2677 		/*
2678 		 * if "dupclose" tunable is set, check if the asru is
2679 		 * already marked as "faulty".
2680 		 */
2681 		if (Dupclose && allfaulty) {
2682 			nvlist_t *asru;
2683 
2684 			out(O_ALTFP|O_VERB, "FMD%d dupclose check ", fmep->id);
2685 			itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, rp->suspect);
2686 			out(O_ALTFP|O_VERB|O_NONL, " ");
2687 			if (nvlist_lookup_nvlist(fault,
2688 			    FM_FAULT_ASRU, &asru) != 0) {
2689 				out(O_ALTFP|O_VERB, "NULL asru");
2690 				allfaulty = B_FALSE;
2691 			} else if (fmd_nvl_fmri_faulty(fmep->hdl, asru)) {
2692 				out(O_ALTFP|O_VERB, "faulty");
2693 			} else {
2694 				out(O_ALTFP|O_VERB, "not faulty");
2695 				allfaulty = B_FALSE;
2696 			}
2697 		}
2698 
2699 	}
2700 
2701 	/*
2702 	 * Close the case if all asrus are already known to be faulty and if
2703 	 * Dupclose is enabled.  Otherwise we are going to publish so take
2704 	 * any pre-publication actions.
2705 	 */
2706 	if (Dupclose && allfaulty) {
2707 		out(O_ALTFP, "[dupclose FME%d, case %s]", fmep->id,
2708 		    fmd_case_uuid(fmep->hdl, fmep->fmcase));
2709 		fmd_case_close(fmep->hdl, fmep->fmcase);
2710 	} else {
2711 		for (rp = erl; rp >= srl; rp--) {
2712 			struct event *suspect = rp->suspect;
2713 
2714 			if (suspect == NULL)
2715 				continue;
2716 
2717 			/* if "count" exists, increment the appropriate stat */
2718 			if ((snp = eventprop_lookup(suspect,
2719 			    L_count)) != NULL) {
2720 				out(O_ALTFP|O_NONL,
2721 				    "[FME%d, %s count ", fmep->id,
2722 				    suspect->enode->u.event.ename->u.name.s);
2723 				ptree_name_iter(O_ALTFP|O_NONL, snp);
2724 				out(O_ALTFP, "]");
2725 				istat_bump(snp, 0);
2726 
2727 			}
2728 		}
2729 		istat_save();	/* write out any istat changes */
2730 
2731 		out(O_ALTFP, "[solving FME%d, case %s]", fmep->id,
2732 		    fmd_case_uuid(fmep->hdl, fmep->fmcase));
2733 		fmd_case_solve(fmep->hdl, fmep->fmcase);
2734 	}
2735 
2736 	/*
2737 	 * revert to the original suspect list
2738 	 */
2739 	FREE(srl);
2740 	restore_suspects(fmep);
2741 }
2742 
2743 static void
2744 publish_undiagnosable(fmd_hdl_t *hdl, fmd_event_t *ffep, fmd_case_t *fmcase)
2745 {
2746 	struct case_list *newcase;
2747 	nvlist_t *defect;
2748 
2749 	out(O_ALTFP,
2750 	    "[undiagnosable ereport received, "
2751 	    "creating and closing a new case (%s)]",
2752 	    Undiag_reason ? Undiag_reason : "reason not provided");
2753 
2754 	newcase = MALLOC(sizeof (struct case_list));
2755 	newcase->next = NULL;
2756 	newcase->fmcase = fmcase;
2757 	if (Undiagablecaselist != NULL)
2758 		newcase->next = Undiagablecaselist;
2759 	Undiagablecaselist = newcase;
2760 
2761 	if (ffep != NULL)
2762 		fmd_case_add_ereport(hdl, newcase->fmcase, ffep);
2763 
2764 	defect = fmd_nvl_create_fault(hdl, UNDIAGNOSABLE_DEFECT, 100,
2765 	    NULL, NULL, NULL);
2766 	if (Undiag_reason != NULL)
2767 		(void) nvlist_add_string(defect, UNDIAG_REASON, Undiag_reason);
2768 	fmd_case_add_suspect(hdl, newcase->fmcase, defect);
2769 
2770 	fmd_case_solve(hdl, newcase->fmcase);
2771 	fmd_case_close(hdl, newcase->fmcase);
2772 }
2773 
2774 static void
2775 fme_undiagnosable(struct fme *f)
2776 {
2777 	nvlist_t *defect;
2778 
2779 	out(O_ALTFP, "[solving/closing FME%d, case %s (%s)]",
2780 	    f->id, fmd_case_uuid(f->hdl, f->fmcase),
2781 	    Undiag_reason ? Undiag_reason : "undiagnosable");
2782 
2783 	defect = fmd_nvl_create_fault(f->hdl, UNDIAGNOSABLE_DEFECT, 100,
2784 	    NULL, NULL, NULL);
2785 	if (Undiag_reason != NULL)
2786 		(void) nvlist_add_string(defect, UNDIAG_REASON, Undiag_reason);
2787 	fmd_case_add_suspect(f->hdl, f->fmcase, defect);
2788 	fmd_case_solve(f->hdl, f->fmcase);
2789 	fmd_case_close(f->hdl, f->fmcase);
2790 }
2791 
2792 /*
2793  * fme_close_case
2794  *
2795  *	Find the requested case amongst our fmes and close it.  Free up
2796  *	the related fme.
2797  */
2798 void
2799 fme_close_case(fmd_hdl_t *hdl, fmd_case_t *fmcase)
2800 {
2801 	struct case_list *ucasep, *prevcasep = NULL;
2802 	struct fme *prev = NULL;
2803 	struct fme *fmep;
2804 
2805 	for (ucasep = Undiagablecaselist; ucasep; ucasep = ucasep->next) {
2806 		if (fmcase != ucasep->fmcase) {
2807 			prevcasep = ucasep;
2808 			continue;
2809 		}
2810 
2811 		if (prevcasep == NULL)
2812 			Undiagablecaselist = Undiagablecaselist->next;
2813 		else
2814 			prevcasep->next = ucasep->next;
2815 
2816 		FREE(ucasep);
2817 		return;
2818 	}
2819 
2820 	for (fmep = FMElist; fmep; fmep = fmep->next) {
2821 		if (fmep->hdl == hdl && fmep->fmcase == fmcase)
2822 			break;
2823 		prev = fmep;
2824 	}
2825 
2826 	if (fmep == NULL) {
2827 		out(O_WARN, "Eft asked to close unrecognized case [%s].",
2828 		    fmd_case_uuid(hdl, fmcase));
2829 		return;
2830 	}
2831 
2832 	if (EFMElist == fmep)
2833 		EFMElist = prev;
2834 
2835 	if (prev == NULL)
2836 		FMElist = FMElist->next;
2837 	else
2838 		prev->next = fmep->next;
2839 
2840 	fmep->next = NULL;
2841 
2842 	/* Get rid of any timer this fme has set */
2843 	if (fmep->wull != 0)
2844 		fmd_timer_remove(fmep->hdl, fmep->timer);
2845 
2846 	if (ClosedFMEs == NULL) {
2847 		ClosedFMEs = fmep;
2848 	} else {
2849 		fmep->next = ClosedFMEs;
2850 		ClosedFMEs = fmep;
2851 	}
2852 
2853 	Open_fme_count--;
2854 
2855 	/* See if we can close the overflow FME */
2856 	if (Open_fme_count <= Max_fme) {
2857 		for (fmep = FMElist; fmep; fmep = fmep->next) {
2858 			if (fmep->overflow && !(fmd_case_closed(fmep->hdl,
2859 			    fmep->fmcase)))
2860 				break;
2861 		}
2862 
2863 		if (fmep != NULL)
2864 			fmd_case_close(fmep->hdl, fmep->fmcase);
2865 	}
2866 }
2867 
2868 /*
2869  * fme_set_timer()
2870  *	If the time we need to wait for the given FME is less than the
2871  *	current timer, kick that old timer out and establish a new one.
2872  */
2873 static int
2874 fme_set_timer(struct fme *fmep, unsigned long long wull)
2875 {
2876 	out(O_ALTFP|O_VERB|O_NONL, " fme_set_timer: request to wait ");
2877 	ptree_timeval(O_ALTFP|O_VERB, &wull);
2878 
2879 	if (wull <= fmep->pull) {
2880 		out(O_ALTFP|O_VERB|O_NONL, "already have waited at least ");
2881 		ptree_timeval(O_ALTFP|O_VERB, &fmep->pull);
2882 		out(O_ALTFP|O_VERB, NULL);
2883 		/* we've waited at least wull already, don't need timer */
2884 		return (0);
2885 	}
2886 
2887 	out(O_ALTFP|O_VERB|O_NONL, " currently ");
2888 	if (fmep->wull != 0) {
2889 		out(O_ALTFP|O_VERB|O_NONL, "waiting ");
2890 		ptree_timeval(O_ALTFP|O_VERB, &fmep->wull);
2891 		out(O_ALTFP|O_VERB, NULL);
2892 	} else {
2893 		out(O_ALTFP|O_VERB|O_NONL, "not waiting");
2894 		out(O_ALTFP|O_VERB, NULL);
2895 	}
2896 
2897 	if (fmep->wull != 0)
2898 		if (wull >= fmep->wull)
2899 			/* New timer would fire later than established timer */
2900 			return (0);
2901 
2902 	if (fmep->wull != 0) {
2903 		fmd_timer_remove(fmep->hdl, fmep->timer);
2904 	}
2905 
2906 	fmep->timer = fmd_timer_install(fmep->hdl, (void *)fmep,
2907 	    fmep->e0r, wull);
2908 	out(O_ALTFP|O_VERB, "timer set, id is %ld", fmep->timer);
2909 	fmep->wull = wull;
2910 	return (1);
2911 }
2912 
2913 void
2914 fme_timer_fired(struct fme *fmep, id_t tid)
2915 {
2916 	struct fme *ffmep = NULL;
2917 
2918 	for (ffmep = FMElist; ffmep; ffmep = ffmep->next)
2919 		if (ffmep == fmep)
2920 			break;
2921 
2922 	if (ffmep == NULL) {
2923 		out(O_WARN, "Timer fired for an FME (%p) not in FMEs list.",
2924 		    (void *)fmep);
2925 		return;
2926 	}
2927 
2928 	out(O_ALTFP|O_VERB, "Timer fired %lx", tid);
2929 	fmep->pull = fmep->wull;
2930 	fmep->wull = 0;
2931 	fmd_buf_write(fmep->hdl, fmep->fmcase,
2932 	    WOBUF_PULL, (void *)&fmep->pull, sizeof (fmep->pull));
2933 
2934 	fme_eval(fmep, fmep->e0r);
2935 }
2936 
2937 /*
2938  * Preserve the fme's suspect list in its psuspects list, NULLing the
2939  * suspects list in the meantime.
2940  */
2941 static void
2942 save_suspects(struct fme *fmep)
2943 {
2944 	struct event *ep;
2945 	struct event *nextep;
2946 
2947 	/* zero out the previous suspect list */
2948 	for (ep = fmep->psuspects; ep; ep = nextep) {
2949 		nextep = ep->psuspects;
2950 		ep->psuspects = NULL;
2951 	}
2952 	fmep->psuspects = NULL;
2953 
2954 	/* zero out the suspect list, copying it to previous suspect list */
2955 	fmep->psuspects = fmep->suspects;
2956 	for (ep = fmep->suspects; ep; ep = nextep) {
2957 		nextep = ep->suspects;
2958 		ep->psuspects = ep->suspects;
2959 		ep->suspects = NULL;
2960 		ep->is_suspect = 0;
2961 	}
2962 	fmep->suspects = NULL;
2963 	fmep->nsuspects = 0;
2964 	fmep->nonfault = 0;
2965 }
2966 
2967 /*
2968  * Retrieve the fme's suspect list from its psuspects list.
2969  */
2970 static void
2971 restore_suspects(struct fme *fmep)
2972 {
2973 	struct event *ep;
2974 	struct event *nextep;
2975 
2976 	fmep->nsuspects = fmep->nonfault = 0;
2977 	fmep->suspects = fmep->psuspects;
2978 	for (ep = fmep->psuspects; ep; ep = nextep) {
2979 		fmep->nsuspects++;
2980 		if (!is_fault(ep->t))
2981 			fmep->nonfault++;
2982 		nextep = ep->psuspects;
2983 		ep->suspects = ep->psuspects;
2984 	}
2985 }
2986 
2987 /*
2988  * this is what we use to call the Emrys prototype code instead of main()
2989  */
2990 static void
2991 fme_eval(struct fme *fmep, fmd_event_t *ffep)
2992 {
2993 	struct event *ep;
2994 	unsigned long long my_delay = TIMEVAL_EVENTUALLY;
2995 
2996 	save_suspects(fmep);
2997 
2998 	out(O_ALTFP, "Evaluate FME %d", fmep->id);
2999 	indent_set("  ");
3000 
3001 	lut_walk(fmep->eventtree, (lut_cb)clear_arrows, (void *)fmep);
3002 	fmep->state = hypothesise(fmep, fmep->e0, fmep->ull, &my_delay);
3003 
3004 	out(O_ALTFP|O_NONL, "FME%d state: %s, suspect list:", fmep->id,
3005 	    fme_state2str(fmep->state));
3006 	for (ep = fmep->suspects; ep; ep = ep->suspects) {
3007 		out(O_ALTFP|O_NONL, " ");
3008 		itree_pevent_brief(O_ALTFP|O_NONL, ep);
3009 	}
3010 	out(O_ALTFP, NULL);
3011 
3012 	switch (fmep->state) {
3013 	case FME_CREDIBLE:
3014 		print_suspects(SLNEW, fmep);
3015 		(void) upsets_eval(fmep, ffep);
3016 
3017 		/*
3018 		 * we may have already posted suspects in upsets_eval() which
3019 		 * can recurse into fme_eval() again. If so then just return.
3020 		 */
3021 		if (fmep->posted_suspects)
3022 			return;
3023 
3024 		publish_suspects(fmep);
3025 		fmep->posted_suspects = 1;
3026 		fmd_buf_write(fmep->hdl, fmep->fmcase,
3027 		    WOBUF_POSTD,
3028 		    (void *)&fmep->posted_suspects,
3029 		    sizeof (fmep->posted_suspects));
3030 
3031 		/*
3032 		 * Now the suspects have been posted, we can clear up
3033 		 * the instance tree as we won't be looking at it again.
3034 		 * Also cancel the timer as the case is now solved.
3035 		 */
3036 		if (fmep->wull != 0) {
3037 			fmd_timer_remove(fmep->hdl, fmep->timer);
3038 			fmep->wull = 0;
3039 		}
3040 		break;
3041 
3042 	case FME_WAIT:
3043 		ASSERT(my_delay > fmep->ull);
3044 		(void) fme_set_timer(fmep, my_delay);
3045 		print_suspects(SLWAIT, fmep);
3046 		itree_prune(fmep->eventtree);
3047 		return;
3048 
3049 	case FME_DISPROVED:
3050 		print_suspects(SLDISPROVED, fmep);
3051 		Undiag_reason = UD_UNSOLVD;
3052 		fme_undiagnosable(fmep);
3053 		break;
3054 	}
3055 
3056 	if (fmep->posted_suspects == 1 && Autoclose != NULL) {
3057 		int doclose = 0;
3058 
3059 		if (strcmp(Autoclose, "true") == 0 ||
3060 		    strcmp(Autoclose, "all") == 0)
3061 			doclose = 1;
3062 
3063 		if (strcmp(Autoclose, "upsets") == 0) {
3064 			doclose = 1;
3065 			for (ep = fmep->suspects; ep; ep = ep->suspects) {
3066 				if (ep->t != N_UPSET) {
3067 					doclose = 0;
3068 					break;
3069 				}
3070 			}
3071 		}
3072 
3073 		if (doclose) {
3074 			out(O_ALTFP, "[closing FME%d, case %s (autoclose)]",
3075 			    fmep->id, fmd_case_uuid(fmep->hdl, fmep->fmcase));
3076 			fmd_case_close(fmep->hdl, fmep->fmcase);
3077 		}
3078 	}
3079 	itree_free(fmep->eventtree);
3080 	fmep->eventtree = NULL;
3081 	structconfig_free(fmep->config);
3082 	fmep->config = NULL;
3083 	destroy_fme_bufs(fmep);
3084 }
3085 
3086 static void indent(void);
3087 static int triggered(struct fme *fmep, struct event *ep, int mark);
3088 static enum fme_state effects_test(struct fme *fmep,
3089     struct event *fault_event, unsigned long long at_latest_by,
3090     unsigned long long *pdelay);
3091 static enum fme_state requirements_test(struct fme *fmep, struct event *ep,
3092     unsigned long long at_latest_by, unsigned long long *pdelay);
3093 static enum fme_state causes_test(struct fme *fmep, struct event *ep,
3094     unsigned long long at_latest_by, unsigned long long *pdelay);
3095 
3096 static int
3097 checkconstraints(struct fme *fmep, struct arrow *arrowp)
3098 {
3099 	struct constraintlist *ctp;
3100 	struct evalue value;
3101 	char *sep = "";
3102 
3103 	if (arrowp->forever_false) {
3104 		indent();
3105 		out(O_ALTFP|O_VERB|O_NONL, "  Forever false constraint: ");
3106 		for (ctp = arrowp->constraints; ctp != NULL; ctp = ctp->next) {
3107 			out(O_ALTFP|O_VERB|O_NONL, sep);
3108 			ptree(O_ALTFP|O_VERB|O_NONL, ctp->cnode, 1, 0);
3109 			sep = ", ";
3110 		}
3111 		out(O_ALTFP|O_VERB, NULL);
3112 		return (0);
3113 	}
3114 	if (arrowp->forever_true) {
3115 		indent();
3116 		out(O_ALTFP|O_VERB|O_NONL, "  Forever true constraint: ");
3117 		for (ctp = arrowp->constraints; ctp != NULL; ctp = ctp->next) {
3118 			out(O_ALTFP|O_VERB|O_NONL, sep);
3119 			ptree(O_ALTFP|O_VERB|O_NONL, ctp->cnode, 1, 0);
3120 			sep = ", ";
3121 		}
3122 		out(O_ALTFP|O_VERB, NULL);
3123 		return (1);
3124 	}
3125 
3126 	for (ctp = arrowp->constraints; ctp != NULL; ctp = ctp->next) {
3127 		if (eval_expr(ctp->cnode, NULL, NULL,
3128 		    &fmep->globals, fmep->config,
3129 		    arrowp, 0, &value)) {
3130 			/* evaluation successful */
3131 			if (value.t == UNDEFINED || value.v == 0) {
3132 				/* known false */
3133 				arrowp->forever_false = 1;
3134 				indent();
3135 				out(O_ALTFP|O_VERB|O_NONL,
3136 				    "  False constraint: ");
3137 				ptree(O_ALTFP|O_VERB|O_NONL, ctp->cnode, 1, 0);
3138 				out(O_ALTFP|O_VERB, NULL);
3139 				return (0);
3140 			}
3141 		} else {
3142 			/* evaluation unsuccessful -- unknown value */
3143 			indent();
3144 			out(O_ALTFP|O_VERB|O_NONL,
3145 			    "  Deferred constraint: ");
3146 			ptree(O_ALTFP|O_VERB|O_NONL, ctp->cnode, 1, 0);
3147 			out(O_ALTFP|O_VERB, NULL);
3148 			return (1);
3149 		}
3150 	}
3151 	/* known true */
3152 	arrowp->forever_true = 1;
3153 	indent();
3154 	out(O_ALTFP|O_VERB|O_NONL, "  True constraint: ");
3155 	for (ctp = arrowp->constraints; ctp != NULL; ctp = ctp->next) {
3156 		out(O_ALTFP|O_VERB|O_NONL, sep);
3157 		ptree(O_ALTFP|O_VERB|O_NONL, ctp->cnode, 1, 0);
3158 		sep = ", ";
3159 	}
3160 	out(O_ALTFP|O_VERB, NULL);
3161 	return (1);
3162 }
3163 
3164 static int
3165 triggered(struct fme *fmep, struct event *ep, int mark)
3166 {
3167 	struct bubble *bp;
3168 	struct arrowlist *ap;
3169 	int count = 0;
3170 
3171 	stats_counter_bump(fmep->Tcallcount);
3172 	for (bp = itree_next_bubble(ep, NULL); bp;
3173 	    bp = itree_next_bubble(ep, bp)) {
3174 		if (bp->t != B_TO)
3175 			continue;
3176 		for (ap = itree_next_arrow(bp, NULL); ap;
3177 		    ap = itree_next_arrow(bp, ap)) {
3178 			/* check count of marks against K in the bubble */
3179 			if ((ap->arrowp->mark & mark) &&
3180 			    ++count >= bp->nork)
3181 				return (1);
3182 		}
3183 	}
3184 	return (0);
3185 }
3186 
3187 static int
3188 mark_arrows(struct fme *fmep, struct event *ep, int mark,
3189     unsigned long long at_latest_by, unsigned long long *pdelay, int keep)
3190 {
3191 	struct bubble *bp;
3192 	struct arrowlist *ap;
3193 	unsigned long long overall_delay = TIMEVAL_EVENTUALLY;
3194 	unsigned long long my_delay;
3195 	enum fme_state result;
3196 	int retval = 0;
3197 
3198 	for (bp = itree_next_bubble(ep, NULL); bp;
3199 	    bp = itree_next_bubble(ep, bp)) {
3200 		if (bp->t != B_FROM)
3201 			continue;
3202 		stats_counter_bump(fmep->Marrowcount);
3203 		for (ap = itree_next_arrow(bp, NULL); ap;
3204 		    ap = itree_next_arrow(bp, ap)) {
3205 			struct event *ep2 = ap->arrowp->head->myevent;
3206 			/*
3207 			 * if we're clearing marks, we can avoid doing
3208 			 * all that work evaluating constraints.
3209 			 */
3210 			if (mark == 0) {
3211 				if (ap->arrowp->arrow_marked == 0)
3212 					continue;
3213 				ap->arrowp->arrow_marked = 0;
3214 				ap->arrowp->mark &= ~EFFECTS_COUNTER;
3215 				if (keep && (ep2->cached_state &
3216 				    (WAIT_EFFECT|CREDIBLE_EFFECT|PARENT_WAIT)))
3217 					ep2->keep_in_tree = 1;
3218 				ep2->cached_state &=
3219 				    ~(WAIT_EFFECT|CREDIBLE_EFFECT|PARENT_WAIT);
3220 				(void) mark_arrows(fmep, ep2, mark, 0, NULL,
3221 				    keep);
3222 				continue;
3223 			}
3224 			ap->arrowp->arrow_marked = 1;
3225 			if (ep2->cached_state & REQMNTS_DISPROVED) {
3226 				indent();
3227 				out(O_ALTFP|O_VERB|O_NONL,
3228 				    "  ALREADY DISPROVED ");
3229 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
3230 				out(O_ALTFP|O_VERB, NULL);
3231 				continue;
3232 			}
3233 			if (ep2->cached_state & WAIT_EFFECT) {
3234 				indent();
3235 				out(O_ALTFP|O_VERB|O_NONL,
3236 				    "  ALREADY EFFECTS WAIT ");
3237 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
3238 				out(O_ALTFP|O_VERB, NULL);
3239 				continue;
3240 			}
3241 			if (ep2->cached_state & CREDIBLE_EFFECT) {
3242 				indent();
3243 				out(O_ALTFP|O_VERB|O_NONL,
3244 				    "  ALREADY EFFECTS CREDIBLE ");
3245 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
3246 				out(O_ALTFP|O_VERB, NULL);
3247 				continue;
3248 			}
3249 			if ((ep2->cached_state & PARENT_WAIT) &&
3250 			    (mark & PARENT_WAIT)) {
3251 				indent();
3252 				out(O_ALTFP|O_VERB|O_NONL,
3253 				    "  ALREADY PARENT EFFECTS WAIT ");
3254 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
3255 				out(O_ALTFP|O_VERB, NULL);
3256 				continue;
3257 			}
3258 			platform_set_payloadnvp(ep2->nvp);
3259 			if (checkconstraints(fmep, ap->arrowp) == 0) {
3260 				platform_set_payloadnvp(NULL);
3261 				indent();
3262 				out(O_ALTFP|O_VERB|O_NONL,
3263 				    "  CONSTRAINTS FAIL ");
3264 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
3265 				out(O_ALTFP|O_VERB, NULL);
3266 				continue;
3267 			}
3268 			platform_set_payloadnvp(NULL);
3269 			ap->arrowp->mark |= EFFECTS_COUNTER;
3270 			if (!triggered(fmep, ep2, EFFECTS_COUNTER)) {
3271 				indent();
3272 				out(O_ALTFP|O_VERB|O_NONL,
3273 				    "  K-COUNT NOT YET MET ");
3274 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
3275 				out(O_ALTFP|O_VERB, NULL);
3276 				continue;
3277 			}
3278 			ep2->cached_state &= ~PARENT_WAIT;
3279 			/*
3280 			 * if we've reached an ereport and no propagation time
3281 			 * is specified, use the Hesitate value
3282 			 */
3283 			if (ep2->t == N_EREPORT && at_latest_by == 0ULL &&
3284 			    ap->arrowp->maxdelay == 0ULL) {
3285 				out(O_ALTFP|O_VERB|O_NONL, "  default wait ");
3286 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
3287 				out(O_ALTFP|O_VERB, NULL);
3288 				result = requirements_test(fmep, ep2, Hesitate,
3289 				    &my_delay);
3290 			} else {
3291 				result = requirements_test(fmep, ep2,
3292 				    at_latest_by + ap->arrowp->maxdelay,
3293 				    &my_delay);
3294 			}
3295 			if (result == FME_WAIT) {
3296 				retval = WAIT_EFFECT;
3297 				if (overall_delay > my_delay)
3298 					overall_delay = my_delay;
3299 				ep2->cached_state |= WAIT_EFFECT;
3300 				indent();
3301 				out(O_ALTFP|O_VERB|O_NONL, "  EFFECTS WAIT ");
3302 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
3303 				out(O_ALTFP|O_VERB, NULL);
3304 				indent_push("  E");
3305 				if (mark_arrows(fmep, ep2, PARENT_WAIT,
3306 				    at_latest_by, &my_delay, 0) ==
3307 				    WAIT_EFFECT) {
3308 					retval = WAIT_EFFECT;
3309 					if (overall_delay > my_delay)
3310 						overall_delay = my_delay;
3311 				}
3312 				indent_pop();
3313 			} else if (result == FME_DISPROVED) {
3314 				indent();
3315 				out(O_ALTFP|O_VERB|O_NONL,
3316 				    "  EFFECTS DISPROVED ");
3317 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
3318 				out(O_ALTFP|O_VERB, NULL);
3319 			} else {
3320 				ep2->cached_state |= mark;
3321 				indent();
3322 				if (mark == CREDIBLE_EFFECT)
3323 					out(O_ALTFP|O_VERB|O_NONL,
3324 					    "  EFFECTS CREDIBLE ");
3325 				else
3326 					out(O_ALTFP|O_VERB|O_NONL,
3327 					    "  PARENT EFFECTS WAIT ");
3328 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
3329 				out(O_ALTFP|O_VERB, NULL);
3330 				indent_push("  E");
3331 				if (mark_arrows(fmep, ep2, mark, at_latest_by,
3332 				    &my_delay, 0) == WAIT_EFFECT) {
3333 					retval = WAIT_EFFECT;
3334 					if (overall_delay > my_delay)
3335 						overall_delay = my_delay;
3336 				}
3337 				indent_pop();
3338 			}
3339 		}
3340 	}
3341 	if (retval == WAIT_EFFECT)
3342 		*pdelay = overall_delay;
3343 	return (retval);
3344 }
3345 
3346 static enum fme_state
3347 effects_test(struct fme *fmep, struct event *fault_event,
3348     unsigned long long at_latest_by, unsigned long long *pdelay)
3349 {
3350 	struct event *error_event;
3351 	enum fme_state return_value = FME_CREDIBLE;
3352 	unsigned long long overall_delay = TIMEVAL_EVENTUALLY;
3353 	unsigned long long my_delay;
3354 
3355 	stats_counter_bump(fmep->Ecallcount);
3356 	indent_push("  E");
3357 	indent();
3358 	out(O_ALTFP|O_VERB|O_NONL, "->");
3359 	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, fault_event);
3360 	out(O_ALTFP|O_VERB, NULL);
3361 
3362 	if (mark_arrows(fmep, fault_event, CREDIBLE_EFFECT, at_latest_by,
3363 	    &my_delay, 0) == WAIT_EFFECT) {
3364 		return_value = FME_WAIT;
3365 		if (overall_delay > my_delay)
3366 			overall_delay = my_delay;
3367 	}
3368 	for (error_event = fmep->observations;
3369 	    error_event; error_event = error_event->observations) {
3370 		indent();
3371 		out(O_ALTFP|O_VERB|O_NONL, " ");
3372 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, error_event);
3373 		if (!(error_event->cached_state & CREDIBLE_EFFECT)) {
3374 			if (error_event->cached_state &
3375 			    (PARENT_WAIT|WAIT_EFFECT)) {
3376 				out(O_ALTFP|O_VERB, " NOT YET triggered");
3377 				continue;
3378 			}
3379 			return_value = FME_DISPROVED;
3380 			out(O_ALTFP|O_VERB, " NOT triggered");
3381 			break;
3382 		} else {
3383 			out(O_ALTFP|O_VERB, " triggered");
3384 		}
3385 	}
3386 	if (return_value == FME_DISPROVED) {
3387 		(void) mark_arrows(fmep, fault_event, 0, 0, NULL, 0);
3388 	} else {
3389 		fault_event->keep_in_tree = 1;
3390 		(void) mark_arrows(fmep, fault_event, 0, 0, NULL, 1);
3391 	}
3392 
3393 	indent();
3394 	out(O_ALTFP|O_VERB|O_NONL, "<-EFFECTS %s ",
3395 	    fme_state2str(return_value));
3396 	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, fault_event);
3397 	out(O_ALTFP|O_VERB, NULL);
3398 	indent_pop();
3399 	if (return_value == FME_WAIT)
3400 		*pdelay = overall_delay;
3401 	return (return_value);
3402 }
3403 
3404 static enum fme_state
3405 requirements_test(struct fme *fmep, struct event *ep,
3406     unsigned long long at_latest_by, unsigned long long *pdelay)
3407 {
3408 	int waiting_events;
3409 	int credible_events;
3410 	int deferred_events;
3411 	enum fme_state return_value = FME_CREDIBLE;
3412 	unsigned long long overall_delay = TIMEVAL_EVENTUALLY;
3413 	unsigned long long arrow_delay;
3414 	unsigned long long my_delay;
3415 	struct event *ep2;
3416 	struct bubble *bp;
3417 	struct arrowlist *ap;
3418 
3419 	if (ep->cached_state & REQMNTS_CREDIBLE) {
3420 		indent();
3421 		out(O_ALTFP|O_VERB|O_NONL, "  REQMNTS ALREADY CREDIBLE ");
3422 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
3423 		out(O_ALTFP|O_VERB, NULL);
3424 		return (FME_CREDIBLE);
3425 	}
3426 	if (ep->cached_state & REQMNTS_DISPROVED) {
3427 		indent();
3428 		out(O_ALTFP|O_VERB|O_NONL, "  REQMNTS ALREADY DISPROVED ");
3429 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
3430 		out(O_ALTFP|O_VERB, NULL);
3431 		return (FME_DISPROVED);
3432 	}
3433 	if (ep->cached_state & REQMNTS_WAIT) {
3434 		indent();
3435 		*pdelay = ep->cached_delay;
3436 		out(O_ALTFP|O_VERB|O_NONL, "  REQMNTS ALREADY WAIT ");
3437 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
3438 		out(O_ALTFP|O_VERB|O_NONL, ", wait for: ");
3439 		ptree_timeval(O_ALTFP|O_VERB|O_NONL, &at_latest_by);
3440 		out(O_ALTFP|O_VERB, NULL);
3441 		return (FME_WAIT);
3442 	}
3443 	stats_counter_bump(fmep->Rcallcount);
3444 	indent_push("  R");
3445 	indent();
3446 	out(O_ALTFP|O_VERB|O_NONL, "->");
3447 	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
3448 	out(O_ALTFP|O_VERB|O_NONL, ", at latest by: ");
3449 	ptree_timeval(O_ALTFP|O_VERB|O_NONL, &at_latest_by);
3450 	out(O_ALTFP|O_VERB, NULL);
3451 
3452 	if (ep->t == N_EREPORT) {
3453 		if (ep->count == 0) {
3454 			if (fmep->pull >= at_latest_by) {
3455 				return_value = FME_DISPROVED;
3456 			} else {
3457 				ep->cached_delay = *pdelay = at_latest_by;
3458 				return_value = FME_WAIT;
3459 			}
3460 		}
3461 
3462 		indent();
3463 		switch (return_value) {
3464 		case FME_CREDIBLE:
3465 			ep->cached_state |= REQMNTS_CREDIBLE;
3466 			out(O_ALTFP|O_VERB|O_NONL, "<-REQMNTS CREDIBLE ");
3467 			itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
3468 			break;
3469 		case FME_DISPROVED:
3470 			ep->cached_state |= REQMNTS_DISPROVED;
3471 			out(O_ALTFP|O_VERB|O_NONL, "<-REQMNTS DISPROVED ");
3472 			itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
3473 			break;
3474 		case FME_WAIT:
3475 			ep->cached_state |= REQMNTS_WAIT;
3476 			out(O_ALTFP|O_VERB|O_NONL, "<-REQMNTS WAIT ");
3477 			itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
3478 			out(O_ALTFP|O_VERB|O_NONL, " to ");
3479 			ptree_timeval(O_ALTFP|O_VERB|O_NONL, &at_latest_by);
3480 			break;
3481 		default:
3482 			out(O_DIE, "requirements_test: unexpected fme_state");
3483 			break;
3484 		}
3485 		out(O_ALTFP|O_VERB, NULL);
3486 		indent_pop();
3487 
3488 		return (return_value);
3489 	}
3490 
3491 	/* this event is not a report, descend the tree */
3492 	for (bp = itree_next_bubble(ep, NULL); bp;
3493 	    bp = itree_next_bubble(ep, bp)) {
3494 		int n;
3495 
3496 		if (bp->t != B_FROM)
3497 			continue;
3498 
3499 		n = bp->nork;
3500 
3501 		credible_events = 0;
3502 		waiting_events = 0;
3503 		deferred_events = 0;
3504 		arrow_delay = TIMEVAL_EVENTUALLY;
3505 		/*
3506 		 * n is -1 for 'A' so adjust it.
3507 		 * XXX just count up the arrows for now.
3508 		 */
3509 		if (n < 0) {
3510 			n = 0;
3511 			for (ap = itree_next_arrow(bp, NULL); ap;
3512 			    ap = itree_next_arrow(bp, ap))
3513 				n++;
3514 			indent();
3515 			out(O_ALTFP|O_VERB, " Bubble Counted N=%d", n);
3516 		} else {
3517 			indent();
3518 			out(O_ALTFP|O_VERB, " Bubble N=%d", n);
3519 		}
3520 
3521 		if (n == 0)
3522 			continue;
3523 		if (!(bp->mark & (BUBBLE_ELIDED|BUBBLE_OK))) {
3524 			for (ap = itree_next_arrow(bp, NULL); ap;
3525 			    ap = itree_next_arrow(bp, ap)) {
3526 				ep2 = ap->arrowp->head->myevent;
3527 				platform_set_payloadnvp(ep2->nvp);
3528 				if (checkconstraints(fmep, ap->arrowp) == 0) {
3529 					/*
3530 					 * if any arrow is invalidated by the
3531 					 * constraints, then we should elide the
3532 					 * whole bubble to be consistant with
3533 					 * the tree creation time behaviour
3534 					 */
3535 					bp->mark |= BUBBLE_ELIDED;
3536 					platform_set_payloadnvp(NULL);
3537 					break;
3538 				}
3539 				platform_set_payloadnvp(NULL);
3540 			}
3541 		}
3542 		if (bp->mark & BUBBLE_ELIDED)
3543 			continue;
3544 		bp->mark |= BUBBLE_OK;
3545 		for (ap = itree_next_arrow(bp, NULL); ap;
3546 		    ap = itree_next_arrow(bp, ap)) {
3547 			ep2 = ap->arrowp->head->myevent;
3548 			if (n <= credible_events)
3549 				break;
3550 
3551 			ap->arrowp->mark |= REQMNTS_COUNTER;
3552 			if (triggered(fmep, ep2, REQMNTS_COUNTER))
3553 				/* XXX adding max timevals! */
3554 				switch (requirements_test(fmep, ep2,
3555 				    at_latest_by + ap->arrowp->maxdelay,
3556 				    &my_delay)) {
3557 				case FME_DEFERRED:
3558 					deferred_events++;
3559 					break;
3560 				case FME_CREDIBLE:
3561 					credible_events++;
3562 					break;
3563 				case FME_DISPROVED:
3564 					break;
3565 				case FME_WAIT:
3566 					if (my_delay < arrow_delay)
3567 						arrow_delay = my_delay;
3568 					waiting_events++;
3569 					break;
3570 				default:
3571 					out(O_DIE,
3572 					"Bug in requirements_test.");
3573 				}
3574 			else
3575 				deferred_events++;
3576 		}
3577 		indent();
3578 		out(O_ALTFP|O_VERB, " Credible: %d Waiting %d",
3579 		    credible_events + deferred_events, waiting_events);
3580 		if (credible_events + deferred_events + waiting_events < n) {
3581 			/* Can never meet requirements */
3582 			ep->cached_state |= REQMNTS_DISPROVED;
3583 			indent();
3584 			out(O_ALTFP|O_VERB|O_NONL, "<-REQMNTS DISPROVED ");
3585 			itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
3586 			out(O_ALTFP|O_VERB, NULL);
3587 			indent_pop();
3588 			return (FME_DISPROVED);
3589 		}
3590 		if (credible_events + deferred_events < n) {
3591 			/* will have to wait */
3592 			/* wait time is shortest known */
3593 			if (arrow_delay < overall_delay)
3594 				overall_delay = arrow_delay;
3595 			return_value = FME_WAIT;
3596 		} else if (credible_events < n) {
3597 			if (return_value != FME_WAIT)
3598 				return_value = FME_DEFERRED;
3599 		}
3600 	}
3601 
3602 	/*
3603 	 * don't mark as FME_DEFERRED. If this event isn't reached by another
3604 	 * path, then this will be considered FME_CREDIBLE. But if it is
3605 	 * reached by a different path so the K-count is met, then might
3606 	 * get overridden by FME_WAIT or FME_DISPROVED.
3607 	 */
3608 	if (return_value == FME_WAIT) {
3609 		ep->cached_state |= REQMNTS_WAIT;
3610 		ep->cached_delay = *pdelay = overall_delay;
3611 	} else if (return_value == FME_CREDIBLE) {
3612 		ep->cached_state |= REQMNTS_CREDIBLE;
3613 	}
3614 	indent();
3615 	out(O_ALTFP|O_VERB|O_NONL, "<-REQMNTS %s ",
3616 	    fme_state2str(return_value));
3617 	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
3618 	out(O_ALTFP|O_VERB, NULL);
3619 	indent_pop();
3620 	return (return_value);
3621 }
3622 
3623 static enum fme_state
3624 causes_test(struct fme *fmep, struct event *ep,
3625     unsigned long long at_latest_by, unsigned long long *pdelay)
3626 {
3627 	unsigned long long overall_delay = TIMEVAL_EVENTUALLY;
3628 	unsigned long long my_delay;
3629 	int credible_results = 0;
3630 	int waiting_results = 0;
3631 	enum fme_state fstate;
3632 	struct event *tail_event;
3633 	struct bubble *bp;
3634 	struct arrowlist *ap;
3635 	int k = 1;
3636 
3637 	stats_counter_bump(fmep->Ccallcount);
3638 	indent_push("  C");
3639 	indent();
3640 	out(O_ALTFP|O_VERB|O_NONL, "->");
3641 	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
3642 	out(O_ALTFP|O_VERB, NULL);
3643 
3644 	for (bp = itree_next_bubble(ep, NULL); bp;
3645 	    bp = itree_next_bubble(ep, bp)) {
3646 		if (bp->t != B_TO)
3647 			continue;
3648 		k = bp->nork;	/* remember the K value */
3649 		for (ap = itree_next_arrow(bp, NULL); ap;
3650 		    ap = itree_next_arrow(bp, ap)) {
3651 			int do_not_follow = 0;
3652 
3653 			/*
3654 			 * if we get to the same event multiple times
3655 			 * only worry about the first one.
3656 			 */
3657 			if (ap->arrowp->tail->myevent->cached_state &
3658 			    CAUSES_TESTED) {
3659 				indent();
3660 				out(O_ALTFP|O_VERB|O_NONL,
3661 				    "  causes test already run for ");
3662 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL,
3663 				    ap->arrowp->tail->myevent);
3664 				out(O_ALTFP|O_VERB, NULL);
3665 				continue;
3666 			}
3667 
3668 			/*
3669 			 * see if false constraint prevents us
3670 			 * from traversing this arrow
3671 			 */
3672 			platform_set_payloadnvp(ep->nvp);
3673 			if (checkconstraints(fmep, ap->arrowp) == 0)
3674 				do_not_follow = 1;
3675 			platform_set_payloadnvp(NULL);
3676 			if (do_not_follow) {
3677 				indent();
3678 				out(O_ALTFP|O_VERB|O_NONL,
3679 				    "  False arrow from ");
3680 				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL,
3681 				    ap->arrowp->tail->myevent);
3682 				out(O_ALTFP|O_VERB, NULL);
3683 				continue;
3684 			}
3685 
3686 			ap->arrowp->tail->myevent->cached_state |=
3687 			    CAUSES_TESTED;
3688 			tail_event = ap->arrowp->tail->myevent;
3689 			fstate = hypothesise(fmep, tail_event, at_latest_by,
3690 			    &my_delay);
3691 
3692 			switch (fstate) {
3693 			case FME_WAIT:
3694 				if (my_delay < overall_delay)
3695 					overall_delay = my_delay;
3696 				waiting_results++;
3697 				break;
3698 			case FME_CREDIBLE:
3699 				credible_results++;
3700 				break;
3701 			case FME_DISPROVED:
3702 				break;
3703 			default:
3704 				out(O_DIE, "Bug in causes_test");
3705 			}
3706 		}
3707 	}
3708 	/* compare against K */
3709 	if (credible_results + waiting_results < k) {
3710 		indent();
3711 		out(O_ALTFP|O_VERB|O_NONL, "<-CAUSES DISPROVED ");
3712 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
3713 		out(O_ALTFP|O_VERB, NULL);
3714 		indent_pop();
3715 		return (FME_DISPROVED);
3716 	}
3717 	if (waiting_results != 0) {
3718 		*pdelay = overall_delay;
3719 		indent();
3720 		out(O_ALTFP|O_VERB|O_NONL, "<-CAUSES WAIT ");
3721 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
3722 		out(O_ALTFP|O_VERB|O_NONL, " to ");
3723 		ptree_timeval(O_ALTFP|O_VERB|O_NONL, &at_latest_by);
3724 		out(O_ALTFP|O_VERB, NULL);
3725 		indent_pop();
3726 		return (FME_WAIT);
3727 	}
3728 	indent();
3729 	out(O_ALTFP|O_VERB|O_NONL, "<-CAUSES CREDIBLE ");
3730 	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
3731 	out(O_ALTFP|O_VERB, NULL);
3732 	indent_pop();
3733 	return (FME_CREDIBLE);
3734 }
3735 
3736 static enum fme_state
3737 hypothesise(struct fme *fmep, struct event *ep,
3738 	unsigned long long at_latest_by, unsigned long long *pdelay)
3739 {
3740 	enum fme_state rtr, otr;
3741 	unsigned long long my_delay;
3742 	unsigned long long overall_delay = TIMEVAL_EVENTUALLY;
3743 
3744 	stats_counter_bump(fmep->Hcallcount);
3745 	indent_push("  H");
3746 	indent();
3747 	out(O_ALTFP|O_VERB|O_NONL, "->");
3748 	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
3749 	out(O_ALTFP|O_VERB|O_NONL, ", at latest by: ");
3750 	ptree_timeval(O_ALTFP|O_VERB|O_NONL, &at_latest_by);
3751 	out(O_ALTFP|O_VERB, NULL);
3752 
3753 	rtr = requirements_test(fmep, ep, at_latest_by, &my_delay);
3754 	if ((rtr == FME_WAIT) && (my_delay < overall_delay))
3755 		overall_delay = my_delay;
3756 	if (rtr != FME_DISPROVED) {
3757 		if (is_problem(ep->t)) {
3758 			otr = effects_test(fmep, ep, at_latest_by, &my_delay);
3759 			if (otr != FME_DISPROVED) {
3760 				if (fmep->peek == 0 && ep->is_suspect == 0) {
3761 					ep->suspects = fmep->suspects;
3762 					ep->is_suspect = 1;
3763 					fmep->suspects = ep;
3764 					fmep->nsuspects++;
3765 					if (!is_fault(ep->t))
3766 						fmep->nonfault++;
3767 				}
3768 			}
3769 		} else
3770 			otr = causes_test(fmep, ep, at_latest_by, &my_delay);
3771 		if ((otr == FME_WAIT) && (my_delay < overall_delay))
3772 			overall_delay = my_delay;
3773 		if ((otr != FME_DISPROVED) &&
3774 		    ((rtr == FME_WAIT) || (otr == FME_WAIT)))
3775 			*pdelay = overall_delay;
3776 	}
3777 	if (rtr == FME_DISPROVED) {
3778 		indent();
3779 		out(O_ALTFP|O_VERB|O_NONL, "<-DISPROVED ");
3780 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
3781 		out(O_ALTFP|O_VERB, " (doesn't meet requirements)");
3782 		indent_pop();
3783 		return (FME_DISPROVED);
3784 	}
3785 	if ((otr == FME_DISPROVED) && is_problem(ep->t)) {
3786 		indent();
3787 		out(O_ALTFP|O_VERB|O_NONL, "<-DISPROVED ");
3788 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
3789 		out(O_ALTFP|O_VERB, " (doesn't explain all reports)");
3790 		indent_pop();
3791 		return (FME_DISPROVED);
3792 	}
3793 	if (otr == FME_DISPROVED) {
3794 		indent();
3795 		out(O_ALTFP|O_VERB|O_NONL, "<-DISPROVED ");
3796 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
3797 		out(O_ALTFP|O_VERB, " (causes are not credible)");
3798 		indent_pop();
3799 		return (FME_DISPROVED);
3800 	}
3801 	if ((rtr == FME_WAIT) || (otr == FME_WAIT)) {
3802 		indent();
3803 		out(O_ALTFP|O_VERB|O_NONL, "<-WAIT ");
3804 		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
3805 		out(O_ALTFP|O_VERB|O_NONL, " to ");
3806 		ptree_timeval(O_ALTFP|O_VERB|O_NONL, &overall_delay);
3807 		out(O_ALTFP|O_VERB, NULL);
3808 		indent_pop();
3809 		return (FME_WAIT);
3810 	}
3811 	indent();
3812 	out(O_ALTFP|O_VERB|O_NONL, "<-CREDIBLE ");
3813 	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
3814 	out(O_ALTFP|O_VERB, NULL);
3815 	indent_pop();
3816 	return (FME_CREDIBLE);
3817 }
3818 
3819 /*
3820  * fme_istat_load -- reconstitute any persistent istats
3821  */
3822 void
3823 fme_istat_load(fmd_hdl_t *hdl)
3824 {
3825 	int sz;
3826 	char *sbuf;
3827 	char *ptr;
3828 
3829 	if ((sz = fmd_buf_size(hdl, NULL, WOBUF_ISTATS)) == 0) {
3830 		out(O_ALTFP, "fme_istat_load: No stats");
3831 		return;
3832 	}
3833 
3834 	sbuf = alloca(sz);
3835 
3836 	fmd_buf_read(hdl, NULL, WOBUF_ISTATS, sbuf, sz);
3837 
3838 	/*
3839 	 * pick apart the serialized stats
3840 	 *
3841 	 * format is:
3842 	 *	<class-name>, '@', <path>, '\0', <value>, '\0'
3843 	 * for example:
3844 	 *	"stat.first@stat0/path0\02\0stat.second@stat0/path1\023\0"
3845 	 *
3846 	 * since this is parsing our own serialized data, any parsing issues
3847 	 * are fatal, so we check for them all with ASSERT() below.
3848 	 */
3849 	ptr = sbuf;
3850 	while (ptr < &sbuf[sz]) {
3851 		char *sepptr;
3852 		struct node *np;
3853 		int val;
3854 
3855 		sepptr = strchr(ptr, '@');
3856 		ASSERT(sepptr != NULL);
3857 		*sepptr = '\0';
3858 
3859 		/* construct the event */
3860 		np = newnode(T_EVENT, NULL, 0);
3861 		np->u.event.ename = newnode(T_NAME, NULL, 0);
3862 		np->u.event.ename->u.name.t = N_STAT;
3863 		np->u.event.ename->u.name.s = stable(ptr);
3864 		np->u.event.ename->u.name.it = IT_ENAME;
3865 		np->u.event.ename->u.name.last = np->u.event.ename;
3866 
3867 		ptr = sepptr + 1;
3868 		ASSERT(ptr < &sbuf[sz]);
3869 		ptr += strlen(ptr);
3870 		ptr++;	/* move past the '\0' separating path from value */
3871 		ASSERT(ptr < &sbuf[sz]);
3872 		ASSERT(isdigit(*ptr));
3873 		val = atoi(ptr);
3874 		ASSERT(val > 0);
3875 		ptr += strlen(ptr);
3876 		ptr++;	/* move past the final '\0' for this entry */
3877 
3878 		np->u.event.epname = pathstring2epnamenp(sepptr + 1);
3879 		ASSERT(np->u.event.epname != NULL);
3880 
3881 		istat_bump(np, val);
3882 		tree_free(np);
3883 	}
3884 
3885 	istat_save();
3886 }
3887