summaryrefslogtreecommitdiff
path: root/usr/src/cmd/fm/modules/common/eversholt/fme.c
blob: 39654219bb06310e77543555a71eb88fba3afeb7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 *
 * fme.c -- fault management exercise module
 *
 * this module provides the simulated fault management exercise.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <ctype.h>
#include <alloca.h>
#include <libnvpair.h>
#include <sys/fm/protocol.h>
#include <fm/fmd_api.h>
#include "alloc.h"
#include "out.h"
#include "stats.h"
#include "stable.h"
#include "literals.h"
#include "lut.h"
#include "tree.h"
#include "ptree.h"
#include "itree.h"
#include "ipath.h"
#include "fme.h"
#include "evnv.h"
#include "eval.h"
#include "config.h"
#include "platform.h"
#include "esclex.h"

/* imported from eft.c... */
extern char *Autoclose;
extern hrtime_t Hesitate;
extern char *Serd_Override;
extern nv_alloc_t Eft_nv_hdl;
extern int Max_fme;
extern fmd_hdl_t *Hdl;

static int Istat_need_save;
static int Serd_need_save;
void istat_save(void);
void serd_save(void);

/* fme under construction is global so we can free it on module abort */
static struct fme *Nfmep;

static const char *Undiag_reason;

static int Nextid = 0;

static int Open_fme_count = 0;	/* Count of open FMEs */

/* list of fault management exercises underway */
static struct fme {
	struct fme *next;		/* next exercise */
	unsigned long long ull;		/* time when fme was created */
	int id;				/* FME id */
	struct config *config;		/* cooked configuration data */
	struct lut *eventtree;		/* propagation tree for this FME */
	/*
	 * The initial error report that created this FME is kept in
	 * two forms.  e0 points to the instance tree node and is used
	 * by fme_eval() as the starting point for the inference
	 * algorithm.  e0r is the event handle FMD passed to us when
	 * the ereport first arrived and is used when setting timers,
	 * which are always relative to the time of this initial
	 * report.
	 */
	struct event *e0;
	fmd_event_t *e0r;

	id_t    timer;			/* for setting an fmd time-out */

	struct event *ecurrent;		/* ereport under consideration */
	struct event *suspects;		/* current suspect list */
	struct event *psuspects;	/* previous suspect list */
	int nsuspects;			/* count of suspects */
	int nonfault;			/* zero if all suspects T_FAULT */
	int posted_suspects;		/* true if we've posted a diagnosis */
	int uniqobs;			/* number of unique events observed */
	int peek;			/* just peeking, don't track suspects */
	int overflow;			/* true if overflow FME */
	enum fme_state {
		FME_NOTHING = 5000,	/* not evaluated yet */
		FME_WAIT,		/* need to wait for more info */
		FME_CREDIBLE,		/* suspect list is credible */
		FME_DISPROVED,		/* no valid suspects found */
		FME_DEFERRED		/* don't know yet (k-count not met) */
	} state;

	unsigned long long pull;	/* time passed since created */
	unsigned long long wull;	/* wait until this time for re-eval */
	struct event *observations;	/* observation list */
	struct lut *globals;		/* values of global variables */
	/* fmd interfacing */
	fmd_hdl_t *hdl;			/* handle for talking with fmd */
	fmd_case_t *fmcase;		/* what fmd 'case' we associate with */
	/* stats */
	struct stats *Rcount;
	struct stats *Hcallcount;
	struct stats *Rcallcount;
	struct stats *Ccallcount;
	struct stats *Ecallcount;
	struct stats *Tcallcount;
	struct stats *Marrowcount;
	struct stats *diags;
} *FMElist, *EFMElist, *ClosedFMEs;

static struct case_list {
	fmd_case_t *fmcase;
	struct case_list *next;
} *Undiagablecaselist;

static void fme_eval(struct fme *fmep, fmd_event_t *ffep);
static enum fme_state hypothesise(struct fme *fmep, struct event *ep,
	unsigned long long at_latest_by, unsigned long long *pdelay);
static struct node *eventprop_lookup(struct event *ep, const char *propname);
static struct node *pathstring2epnamenp(char *path);
static void publish_undiagnosable(fmd_hdl_t *hdl, fmd_event_t *ffep,
	fmd_case_t *fmcase);
static void restore_suspects(struct fme *fmep);
static void save_suspects(struct fme *fmep);
static void destroy_fme(struct fme *f);
static void fme_receive_report(fmd_hdl_t *hdl, fmd_event_t *ffep,
    const char *eventstring, const struct ipath *ipp, nvlist_t *nvl);
static void istat_counter_reset_cb(struct istat_entry *entp,
    struct stats *statp, const struct ipath *ipp);
static void istat_counter_topo_chg_cb(struct istat_entry *entp,
    struct stats *statp, void *unused);
static void serd_reset_cb(struct serd_entry *entp, void *unused,
    const struct ipath *ipp);
static void serd_topo_chg_cb(struct serd_entry *entp, void *unused,
    void *unused2);
static void destroy_fme_bufs(struct fme *fp);

static struct fme *
alloc_fme(void)
{
	struct fme *fmep;

	fmep = MALLOC(sizeof (*fmep));
	bzero(fmep, sizeof (*fmep));
	return (fmep);
}

/*
 * fme_ready -- called when all initialization of the FME (except for
 *	stats) has completed successfully.  Adds the fme to global lists
 *	and establishes its stats.
 */
static struct fme *
fme_ready(struct fme *fmep)
{
	char nbuf[100];

	Nfmep = NULL;	/* don't need to free this on module abort now */

	if (EFMElist) {
		EFMElist->next = fmep;
		EFMElist = fmep;
	} else
		FMElist = EFMElist = fmep;

	(void) sprintf(nbuf, "fme%d.Rcount", fmep->id);
	fmep->Rcount = stats_new_counter(nbuf, "ereports received", 0);
	(void) sprintf(nbuf, "fme%d.Hcall", fmep->id);
	fmep->Hcallcount = stats_new_counter(nbuf, "calls to hypothesise()", 1);
	(void) sprintf(nbuf, "fme%d.Rcall", fmep->id);
	fmep->Rcallcount = stats_new_counter(nbuf,
	    "calls to requirements_test()", 1);
	(void) sprintf(nbuf, "fme%d.Ccall", fmep->id);
	fmep->Ccallcount = stats_new_counter(nbuf, "calls to causes_test()", 1);
	(void) sprintf(nbuf, "fme%d.Ecall", fmep->id);
	fmep->Ecallcount =
	    stats_new_counter(nbuf, "calls to effects_test()", 1);
	(void) sprintf(nbuf, "fme%d.Tcall", fmep->id);
	fmep->Tcallcount = stats_new_counter(nbuf, "calls to triggered()", 1);
	(void) sprintf(nbuf, "fme%d.Marrow", fmep->id);
	fmep->Marrowcount = stats_new_counter(nbuf,
	    "arrows marked by mark_arrows()", 1);
	(void) sprintf(nbuf, "fme%d.diags", fmep->id);
	fmep->diags = stats_new_counter(nbuf, "suspect lists diagnosed", 0);

	out(O_ALTFP|O_VERB2, "newfme: config snapshot contains...");
	config_print(O_ALTFP|O_VERB2, fmep->config);

	return (fmep);
}

extern void ipath_dummy_lut(struct arrow *);
extern struct lut *itree_create_dummy(const char *, const struct ipath *);

/* ARGSUSED */
static void
set_needed_arrows(struct event *ep, struct event *ep2, struct fme *fmep)
{
	struct bubble *bp;
	struct arrowlist *ap;

	for (bp = itree_next_bubble(ep, NULL); bp;
	    bp = itree_next_bubble(ep, bp)) {
		if (bp->t != B_FROM)
			continue;
		for (ap = itree_next_arrow(bp, NULL); ap;
		    ap = itree_next_arrow(bp, ap)) {
			ap->arrowp->pnode->u.arrow.needed = 1;
			ipath_dummy_lut(ap->arrowp);
		}
	}
}

/* ARGSUSED */
static void
unset_needed_arrows(struct event *ep, struct event *ep2, struct fme *fmep)
{
	struct bubble *bp;
	struct arrowlist *ap;

	for (bp = itree_next_bubble(ep, NULL); bp;
	    bp = itree_next_bubble(ep, bp)) {
		if (bp->t != B_FROM)
			continue;
		for (ap = itree_next_arrow(bp, NULL); ap;
		    ap = itree_next_arrow(bp, ap))
			ap->arrowp->pnode->u.arrow.needed = 0;
	}
}

static void globals_destructor(void *left, void *right, void *arg);
static void clear_arrows(struct event *ep, struct event *ep2, struct fme *fmep);

static void
prune_propagations(const char *e0class, const struct ipath *e0ipp)
{
	char nbuf[100];
	unsigned long long my_delay = TIMEVAL_EVENTUALLY;
	extern struct lut *Usednames;

	Nfmep = alloc_fme();
	Nfmep->id = Nextid;
	Nfmep->state = FME_NOTHING;
	Nfmep->eventtree = itree_create_dummy(e0class, e0ipp);
	if ((Nfmep->e0 =
	    itree_lookup(Nfmep->eventtree, e0class, e0ipp)) == NULL) {
		out(O_ALTFP, "prune_propagations: e0 not in instance tree");
		itree_free(Nfmep->eventtree);
		FREE(Nfmep);
		Nfmep = NULL;
		return;
	}
	Nfmep->ecurrent = Nfmep->observations = Nfmep->e0;
	Nfmep->e0->count++;

	(void) sprintf(nbuf, "fme%d.Rcount", Nfmep->id);
	Nfmep->Rcount = stats_new_counter(nbuf, "ereports received", 0);
	(void) sprintf(nbuf, "fme%d.Hcall", Nfmep->id);
	Nfmep->Hcallcount =
	    stats_new_counter(nbuf, "calls to hypothesise()", 1);
	(void) sprintf(nbuf, "fme%d.Rcall", Nfmep->id);
	Nfmep->Rcallcount = stats_new_counter(nbuf,
	    "calls to requirements_test()", 1);
	(void) sprintf(nbuf, "fme%d.Ccall", Nfmep->id);
	Nfmep->Ccallcount =
	    stats_new_counter(nbuf, "calls to causes_test()", 1);
	(void) sprintf(nbuf, "fme%d.Ecall", Nfmep->id);
	Nfmep->Ecallcount =
	    stats_new_counter(nbuf, "calls to effects_test()", 1);
	(void) sprintf(nbuf, "fme%d.Tcall", Nfmep->id);
	Nfmep->Tcallcount = stats_new_counter(nbuf, "calls to triggered()", 1);
	(void) sprintf(nbuf, "fme%d.Marrow", Nfmep->id);
	Nfmep->Marrowcount = stats_new_counter(nbuf,
	    "arrows marked by mark_arrows()", 1);
	(void) sprintf(nbuf, "fme%d.diags", Nfmep->id);
	Nfmep->diags = stats_new_counter(nbuf, "suspect lists diagnosed", 0);

	Nfmep->peek = 1;
	lut_walk(Nfmep->eventtree, (lut_cb)unset_needed_arrows, (void *)Nfmep);
	lut_free(Usednames, NULL, NULL);
	Usednames = NULL;
	lut_walk(Nfmep->eventtree, (lut_cb)clear_arrows, (void *)Nfmep);
	(void) hypothesise(Nfmep, Nfmep->e0, Nfmep->ull, &my_delay);
	itree_prune(Nfmep->eventtree);
	lut_walk(Nfmep->eventtree, (lut_cb)set_needed_arrows, (void *)Nfmep);

	stats_delete(Nfmep->Rcount);
	stats_delete(Nfmep->Hcallcount);
	stats_delete(Nfmep->Rcallcount);
	stats_delete(Nfmep->Ccallcount);
	stats_delete(Nfmep->Ecallcount);
	stats_delete(Nfmep->Tcallcount);
	stats_delete(Nfmep->Marrowcount);
	stats_delete(Nfmep->diags);
	itree_free(Nfmep->eventtree);
	lut_free(Nfmep->globals, globals_destructor, NULL);
	FREE(Nfmep);
}

static struct fme *
newfme(const char *e0class, const struct ipath *e0ipp, fmd_hdl_t *hdl,
	fmd_case_t *fmcase)
{
	struct cfgdata *cfgdata;
	int init_size;
	extern int alloc_total();

	init_size = alloc_total();
	out(O_ALTFP|O_STAMP, "start config_snapshot using %d bytes", init_size);
	if ((cfgdata = config_snapshot()) == NULL) {
		out(O_ALTFP, "newfme: NULL configuration");
		Undiag_reason = UD_NOCONF;
		return (NULL);
	}
	platform_save_config(hdl, fmcase);
	out(O_ALTFP|O_STAMP, "config_snapshot added %d bytes",
	    alloc_total() - init_size);

	Nfmep = alloc_fme();

	Nfmep->id = Nextid++;
	Nfmep->config = cfgdata->cooked;
	config_free(cfgdata);
	Nfmep->posted_suspects = 0;
	Nfmep->uniqobs = 0;
	Nfmep->state = FME_NOTHING;
	Nfmep->pull = 0ULL;
	Nfmep->overflow = 0;

	Nfmep->fmcase = fmcase;
	Nfmep->hdl = hdl;

	if ((Nfmep->eventtree = itree_create(Nfmep->config)) == NULL) {
		out(O_ALTFP, "newfme: NULL instance tree");
		Undiag_reason = UD_INSTFAIL;
		structconfig_free(Nfmep->config);
		destroy_fme_bufs(Nfmep);
		FREE(Nfmep);
		Nfmep = NULL;
		return (NULL);
	}

	itree_ptree(O_ALTFP|O_VERB2, Nfmep->eventtree);

	if ((Nfmep->e0 =
	    itree_lookup(Nfmep->eventtree, e0class, e0ipp)) == NULL) {
		out(O_ALTFP, "newfme: e0 not in instance tree");
		Undiag_reason = UD_BADEVENTI;
		itree_free(Nfmep->eventtree);
		structconfig_free(Nfmep->config);
		destroy_fme_bufs(Nfmep);
		FREE(Nfmep);
		Nfmep = NULL;
		return (NULL);
	}

	return (fme_ready(Nfmep));
}

void
fme_fini(void)
{
	struct fme *sfp, *fp;
	struct case_list *ucasep, *nextcasep;

	ucasep = Undiagablecaselist;
	while (ucasep != NULL) {
		nextcasep = ucasep->next;
		FREE(ucasep);
		ucasep = nextcasep;
	}
	Undiagablecaselist = NULL;

	/* clean up closed fmes */
	fp = ClosedFMEs;
	while (fp != NULL) {
		sfp = fp->next;
		destroy_fme(fp);
		fp = sfp;
	}
	ClosedFMEs = NULL;

	fp = FMElist;
	while (fp != NULL) {
		sfp = fp->next;
		destroy_fme(fp);
		fp = sfp;
	}
	FMElist = EFMElist = NULL;

	/* if we were in the middle of creating an fme, free it now */
	if (Nfmep) {
		destroy_fme(Nfmep);
		Nfmep = NULL;
	}
}

/*
 * Allocated space for a buffer name.  20 bytes allows for
 * a ridiculous 9,999,999 unique observations.
 */
#define	OBBUFNMSZ 20

/*
 *  serialize_observation
 *
 *  Create a recoverable version of the current observation
 *  (f->ecurrent).  We keep a serialized version of each unique
 *  observation in order that we may resume correctly the fme in the
 *  correct state if eft or fmd crashes and we're restarted.
 */
static void
serialize_observation(struct fme *fp, const char *cls, const struct ipath *ipp)
{
	size_t pkdlen;
	char tmpbuf[OBBUFNMSZ];
	char *pkd = NULL;
	char *estr;

	(void) snprintf(tmpbuf, OBBUFNMSZ, "observed%d", fp->uniqobs);
	estr = ipath2str(cls, ipp);
	fmd_buf_create(fp->hdl, fp->fmcase, tmpbuf, strlen(estr) + 1);
	fmd_buf_write(fp->hdl, fp->fmcase, tmpbuf, (void *)estr,
	    strlen(estr) + 1);
	FREE(estr);

	if (fp->ecurrent != NULL && fp->ecurrent->nvp != NULL) {
		(void) snprintf(tmpbuf,
		    OBBUFNMSZ, "observed%d.nvp", fp->uniqobs);
		if (nvlist_xpack(fp->ecurrent->nvp,
		    &pkd, &pkdlen, NV_ENCODE_XDR, &Eft_nv_hdl) != 0)
			out(O_DIE|O_SYS, "pack of observed nvl failed");
		fmd_buf_create(fp->hdl, fp->fmcase, tmpbuf, pkdlen);
		fmd_buf_write(fp->hdl, fp->fmcase, tmpbuf, (void *)pkd, pkdlen);
		FREE(pkd);
	}

	fp->uniqobs++;
	fmd_buf_write(fp->hdl, fp->fmcase, WOBUF_NOBS, (void *)&fp->uniqobs,
	    sizeof (fp->uniqobs));
}

/*
 *  init_fme_bufs -- We keep several bits of state about an fme for
 *	use if eft or fmd crashes and we're restarted.
 */
static void
init_fme_bufs(struct fme *fp)
{
	fmd_buf_create(fp->hdl, fp->fmcase, WOBUF_PULL, sizeof (fp->pull));
	fmd_buf_write(fp->hdl, fp->fmcase, WOBUF_PULL, (void *)&fp->pull,
	    sizeof (fp->pull));

	fmd_buf_create(fp->hdl, fp->fmcase, WOBUF_ID, sizeof (fp->id));
	fmd_buf_write(fp->hdl, fp->fmcase, WOBUF_ID, (void *)&fp->id,
	    sizeof (fp->id));

	fmd_buf_create(fp->hdl, fp->fmcase, WOBUF_NOBS, sizeof (fp->uniqobs));
	fmd_buf_write(fp->hdl, fp->fmcase, WOBUF_NOBS, (void *)&fp->uniqobs,
	    sizeof (fp->uniqobs));

	fmd_buf_create(fp->hdl, fp->fmcase, WOBUF_POSTD,
	    sizeof (fp->posted_suspects));
	fmd_buf_write(fp->hdl, fp->fmcase, WOBUF_POSTD,
	    (void *)&fp->posted_suspects, sizeof (fp->posted_suspects));
}

static void
destroy_fme_bufs(struct fme *fp)
{
	char tmpbuf[OBBUFNMSZ];
	int o;

	platform_restore_config(fp->hdl, fp->fmcase);
	fmd_buf_destroy(fp->hdl, fp->fmcase, WOBUF_CFGLEN);
	fmd_buf_destroy(fp->hdl, fp->fmcase, WOBUF_CFG);
	fmd_buf_destroy(fp->hdl, fp->fmcase, WOBUF_PULL);
	fmd_buf_destroy(fp->hdl, fp->fmcase, WOBUF_ID);
	fmd_buf_destroy(fp->hdl, fp->fmcase, WOBUF_POSTD);
	fmd_buf_destroy(fp->hdl, fp->fmcase, WOBUF_NOBS);

	for (o = 0; o < fp->uniqobs; o++) {
		(void) snprintf(tmpbuf, OBBUFNMSZ, "observed%d", o);
		fmd_buf_destroy(fp->hdl, fp->fmcase, tmpbuf);
		(void) snprintf(tmpbuf, OBBUFNMSZ, "observed%d.nvp", o);
		fmd_buf_destroy(fp->hdl, fp->fmcase, tmpbuf);
	}
}

/*
 * reconstitute_observations -- convert a case's serialized observations
 *	back into struct events.  Returns zero if all observations are
 *	successfully reconstituted.
 */
static int
reconstitute_observations(struct fme *fmep)
{
	struct event *ep;
	struct node *epnamenp = NULL;
	size_t pkdlen;
	char *pkd = NULL;
	char *tmpbuf = alloca(OBBUFNMSZ);
	char *sepptr;
	char *estr;
	int ocnt;
	int elen;

	for (ocnt = 0; ocnt < fmep->uniqobs; ocnt++) {
		(void) snprintf(tmpbuf, OBBUFNMSZ, "observed%d", ocnt);
		elen = fmd_buf_size(fmep->hdl, fmep->fmcase, tmpbuf);
		if (elen == 0) {
			out(O_ALTFP,
			    "reconstitute_observation: no %s buffer found.",
			    tmpbuf);
			Undiag_reason = UD_MISSINGOBS;
			break;
		}

		estr = MALLOC(elen);
		fmd_buf_read(fmep->hdl, fmep->fmcase, tmpbuf, estr, elen);
		sepptr = strchr(estr, '@');
		if (sepptr == NULL) {
			out(O_ALTFP,
			    "reconstitute_observation: %s: "
			    "missing @ separator in %s.",
			    tmpbuf, estr);
			Undiag_reason = UD_MISSINGPATH;
			FREE(estr);
			break;
		}

		*sepptr = '\0';
		if ((epnamenp = pathstring2epnamenp(sepptr + 1)) == NULL) {
			out(O_ALTFP,
			    "reconstitute_observation: %s: "
			    "trouble converting path string \"%s\" "
			    "to internal representation.",
			    tmpbuf, sepptr + 1);
			Undiag_reason = UD_MISSINGPATH;
			FREE(estr);
			break;
		}

		/* construct the event */
		ep = itree_lookup(fmep->eventtree,
		    stable(estr), ipath(epnamenp));
		if (ep == NULL) {
			out(O_ALTFP,
			    "reconstitute_observation: %s: "
			    "lookup of  \"%s\" in itree failed.",
			    tmpbuf, ipath2str(estr, ipath(epnamenp)));
			Undiag_reason = UD_BADOBS;
			tree_free(epnamenp);
			FREE(estr);
			break;
		}
		tree_free(epnamenp);

		/*
		 * We may or may not have a saved nvlist for the observation
		 */
		(void) snprintf(tmpbuf, OBBUFNMSZ, "observed%d.nvp", ocnt);
		pkdlen = fmd_buf_size(fmep->hdl, fmep->fmcase, tmpbuf);
		if (pkdlen != 0) {
			pkd = MALLOC(pkdlen);
			fmd_buf_read(fmep->hdl,
			    fmep->fmcase, tmpbuf, pkd, pkdlen);
			ASSERT(ep->nvp == NULL);
			if (nvlist_xunpack(pkd,
			    pkdlen, &ep->nvp, &Eft_nv_hdl) != 0)
				out(O_DIE|O_SYS, "pack of observed nvl failed");
			FREE(pkd);
		}

		if (ocnt == 0)
			fmep->e0 = ep;

		FREE(estr);
		fmep->ecurrent = ep;
		ep->count++;

		/* link it into list of observations seen */
		ep->observations = fmep->observations;
		fmep->observations = ep;
	}

	if (ocnt == fmep->uniqobs) {
		(void) fme_ready(fmep);
		return (0);
	}

	return (1);
}

/*
 * restart_fme -- called during eft initialization.  Reconstitutes
 *	an in-progress fme.
 */
void
fme_restart(fmd_hdl_t *hdl, fmd_case_t *inprogress)
{
	nvlist_t *defect;
	struct case_list *bad;
	struct fme *fmep;
	struct cfgdata *cfgdata;
	size_t rawsz;
	struct event *ep;
	char *tmpbuf = alloca(OBBUFNMSZ);
	char *sepptr;
	char *estr;
	int elen;
	struct node *epnamenp = NULL;
	int init_size;
	extern int alloc_total();

	/*
	 * ignore solved or closed cases
	 */
	if (fmd_case_solved(hdl, inprogress) ||
	    fmd_case_closed(hdl, inprogress))
		return;

	fmep = alloc_fme();
	fmep->fmcase = inprogress;
	fmep->hdl = hdl;

	if (fmd_buf_size(hdl, inprogress, WOBUF_POSTD) == 0) {
		out(O_ALTFP, "restart_fme: no saved posted status");
		Undiag_reason = UD_MISSINGINFO;
		goto badcase;
	} else {
		fmd_buf_read(hdl, inprogress, WOBUF_POSTD,
		    (void *)&fmep->posted_suspects,
		    sizeof (fmep->posted_suspects));
	}

	if (fmd_buf_size(hdl, inprogress, WOBUF_ID) == 0) {
		out(O_ALTFP, "restart_fme: no saved id");
		Undiag_reason = UD_MISSINGINFO;
		goto badcase;
	} else {
		fmd_buf_read(hdl, inprogress, WOBUF_ID, (void *)&fmep->id,
		    sizeof (fmep->id));
	}
	if (Nextid <= fmep->id)
		Nextid = fmep->id + 1;

	out(O_ALTFP, "Replay FME %d", fmep->id);

	if (fmd_buf_size(hdl, inprogress, WOBUF_CFGLEN) != sizeof (size_t)) {
		out(O_ALTFP, "restart_fme: No config data");
		Undiag_reason = UD_MISSINGINFO;
		goto badcase;
	}
	fmd_buf_read(hdl, inprogress, WOBUF_CFGLEN, (void *)&rawsz,
	    sizeof (size_t));

	if ((fmep->e0r = fmd_case_getprincipal(hdl, inprogress)) == NULL) {
		out(O_ALTFP, "restart_fme: No event zero");
		Undiag_reason = UD_MISSINGZERO;
		goto badcase;
	}

	if (fmd_buf_size(hdl, inprogress, WOBUF_PULL) == 0) {
		out(O_ALTFP, "restart_fme: no saved wait time");
		Undiag_reason = UD_MISSINGINFO;
		goto badcase;
	} else {
		fmd_buf_read(hdl, inprogress, WOBUF_PULL, (void *)&fmep->pull,
		    sizeof (fmep->pull));
	}

	if (fmd_buf_size(hdl, inprogress, WOBUF_NOBS) == 0) {
		out(O_ALTFP, "restart_fme: no count of observations");
		Undiag_reason = UD_MISSINGINFO;
		goto badcase;
	} else {
		fmd_buf_read(hdl, inprogress, WOBUF_NOBS,
		    (void *)&fmep->uniqobs, sizeof (fmep->uniqobs));
	}

	(void) snprintf(tmpbuf, OBBUFNMSZ, "observed0");
	elen = fmd_buf_size(fmep->hdl, fmep->fmcase, tmpbuf);
	if (elen == 0) {
		out(O_ALTFP, "reconstitute_observation: no %s buffer found.",
		    tmpbuf);
		Undiag_reason = UD_MISSINGOBS;
		goto badcase;
	}
	estr = MALLOC(elen);
	fmd_buf_read(fmep->hdl, fmep->fmcase, tmpbuf, estr, elen);
	sepptr = strchr(estr, '@');
	if (sepptr == NULL) {
		out(O_ALTFP, "reconstitute_observation: %s: "
		    "missing @ separator in %s.",
		    tmpbuf, estr);
		Undiag_reason = UD_MISSINGPATH;
		FREE(estr);
		goto badcase;
	}
	*sepptr = '\0';
	if ((epnamenp = pathstring2epnamenp(sepptr + 1)) == NULL) {
		out(O_ALTFP, "reconstitute_observation: %s: "
		    "trouble converting path string \"%s\" "
		    "to internal representation.", tmpbuf, sepptr + 1);
		Undiag_reason = UD_MISSINGPATH;
		FREE(estr);
		goto badcase;
	}
	prune_propagations(stable(estr), ipath(epnamenp));
	tree_free(epnamenp);
	FREE(estr);

	init_size = alloc_total();
	out(O_ALTFP|O_STAMP, "start config_restore using %d bytes", init_size);
	cfgdata = MALLOC(sizeof (struct cfgdata));
	cfgdata->cooked = NULL;
	cfgdata->devcache = NULL;
	cfgdata->cpucache = NULL;
	cfgdata->raw_refcnt = 1;

	if (rawsz > 0) {
		if (fmd_buf_size(hdl, inprogress, WOBUF_CFG) != rawsz) {
			out(O_ALTFP, "restart_fme: Config data size mismatch");
			Undiag_reason = UD_CFGMISMATCH;
			goto badcase;
		}
		cfgdata->begin = MALLOC(rawsz);
		cfgdata->end = cfgdata->nextfree = cfgdata->begin + rawsz;
		fmd_buf_read(hdl,
		    inprogress, WOBUF_CFG, cfgdata->begin, rawsz);
	} else {
		cfgdata->begin = cfgdata->end = cfgdata->nextfree = NULL;
	}

	config_cook(cfgdata);
	fmep->config = cfgdata->cooked;
	config_free(cfgdata);
	out(O_ALTFP|O_STAMP, "config_restore added %d bytes",
	    alloc_total() - init_size);

	if ((fmep->eventtree = itree_create(fmep->config)) == NULL) {
		/* case not properly saved or irretrievable */
		out(O_ALTFP, "restart_fme: NULL instance tree");
		Undiag_reason = UD_INSTFAIL;
		goto badcase;
	}

	itree_ptree(O_ALTFP|O_VERB2, fmep->eventtree);

	if (reconstitute_observations(fmep) != 0)
		goto badcase;

	out(O_ALTFP|O_NONL, "FME %d replay observations: ", fmep->id);
	for (ep = fmep->observations; ep; ep = ep->observations) {
		out(O_ALTFP|O_NONL, " ");
		itree_pevent_brief(O_ALTFP|O_NONL, ep);
	}
	out(O_ALTFP, NULL);

	Open_fme_count++;

	/* give the diagnosis algorithm a shot at the new FME state */
	fme_eval(fmep, fmep->e0r);
	return;

badcase:
	if (fmep->eventtree != NULL)
		itree_free(fmep->eventtree);
	if (fmep->config)
		structconfig_free(fmep->config);
	destroy_fme_bufs(fmep);
	FREE(fmep);

	/*
	 * Since we're unable to restart the case, add it to the undiagable
	 * list and solve and close it as appropriate.
	 */
	bad = MALLOC(sizeof (struct case_list));
	bad->next = NULL;

	if (Undiagablecaselist != NULL)
		bad->next = Undiagablecaselist;
	Undiagablecaselist = bad;
	bad->fmcase = inprogress;

	out(O_ALTFP|O_NONL, "[case %s (unable to restart), ",
	    fmd_case_uuid(hdl, bad->fmcase));

	if (fmd_case_solved(hdl, bad->fmcase)) {
		out(O_ALTFP|O_NONL, "already solved, ");
	} else {
		out(O_ALTFP|O_NONL, "solving, ");
		defect = fmd_nvl_create_fault(hdl, UNDIAGNOSABLE_DEFECT, 100,
		    NULL, NULL, NULL);
		if (Undiag_reason != NULL)
			(void) nvlist_add_string(defect,
			    UNDIAG_REASON, Undiag_reason);
		fmd_case_add_suspect(hdl, bad->fmcase, defect);
		fmd_case_solve(hdl, bad->fmcase);
	}

	if (fmd_case_closed(hdl, bad->fmcase)) {
		out(O_ALTFP, "already closed ]");
	} else {
		out(O_ALTFP, "closing ]");
		fmd_case_close(hdl, bad->fmcase);
	}
}

/*ARGSUSED*/
static void
globals_destructor(void *left, void *right, void *arg)
{
	struct evalue *evp = (struct evalue *)right;
	if (evp->t == NODEPTR)
		tree_free((struct node *)(uintptr_t)evp->v);
	evp->v = NULL;
	FREE(evp);
}

void
destroy_fme(struct fme *f)
{
	stats_delete(f->Rcount);
	stats_delete(f->Hcallcount);
	stats_delete(f->Rcallcount);
	stats_delete(f->Ccallcount);
	stats_delete(f->Ecallcount);
	stats_delete(f->Tcallcount);
	stats_delete(f->Marrowcount);
	stats_delete(f->diags);

	if (f->eventtree != NULL)
		itree_free(f->eventtree);
	if (f->config)
		structconfig_free(f->config);
	lut_free(f->globals, globals_destructor, NULL);
	FREE(f);
}

static const char *
fme_state2str(enum fme_state s)
{
	switch (s) {
	case FME_NOTHING:	return ("NOTHING");
	case FME_WAIT:		return ("WAIT");
	case FME_CREDIBLE:	return ("CREDIBLE");
	case FME_DISPROVED:	return ("DISPROVED");
	case FME_DEFERRED:	return ("DEFERRED");
	default:		return ("UNKNOWN");
	}
}

static int
is_problem(enum nametype t)
{
	return (t == N_FAULT || t == N_DEFECT || t == N_UPSET);
}

static int
is_fault(enum nametype t)
{
	return (t == N_FAULT);
}

static int
is_defect(enum nametype t)
{
	return (t == N_DEFECT);
}

static int
is_upset(enum nametype t)
{
	return (t == N_UPSET);
}

static void
fme_print(int flags, struct fme *fmep)
{
	struct event *ep;

	out(flags, "Fault Management Exercise %d", fmep->id);
	out(flags, "\t       State: %s", fme_state2str(fmep->state));
	out(flags|O_NONL, "\t  Start time: ");
	ptree_timeval(flags|O_NONL, &fmep->ull);
	out(flags, NULL);
	if (fmep->wull) {
		out(flags|O_NONL, "\t   Wait time: ");
		ptree_timeval(flags|O_NONL, &fmep->wull);
		out(flags, NULL);
	}
	out(flags|O_NONL, "\t          E0: ");
	if (fmep->e0)
		itree_pevent_brief(flags|O_NONL, fmep->e0);
	else
		out(flags|O_NONL, "NULL");
	out(flags, NULL);
	out(flags|O_NONL, "\tObservations:");
	for (ep = fmep->observations; ep; ep = ep->observations) {
		out(flags|O_NONL, " ");
		itree_pevent_brief(flags|O_NONL, ep);
	}
	out(flags, NULL);
	out(flags|O_NONL, "\tSuspect list:");
	for (ep = fmep->suspects; ep; ep = ep->suspects) {
		out(flags|O_NONL, " ");
		itree_pevent_brief(flags|O_NONL, ep);
	}
	out(flags, NULL);
	if (fmep->eventtree != NULL) {
		out(flags|O_VERB2, "\t        Tree:");
		itree_ptree(flags|O_VERB2, fmep->eventtree);
	}
}

static struct node *
pathstring2epnamenp(char *path)
{
	char *sep = "/";
	struct node *ret;
	char *ptr;

	if ((ptr = strtok(path, sep)) == NULL)
		out(O_DIE, "pathstring2epnamenp: invalid empty class");

	ret = tree_iname(stable(ptr), NULL, 0);

	while ((ptr = strtok(NULL, sep)) != NULL)
		ret = tree_name_append(ret,
		    tree_iname(stable(ptr), NULL, 0));

	return (ret);
}

/*
 * for a given upset sp, increment the corresponding SERD engine.  if the
 * SERD engine trips, return the ename and ipp of the resulting ereport.
 * returns true if engine tripped and *enamep and *ippp were filled in.
 */
static int
serd_eval(struct fme *fmep, fmd_hdl_t *hdl, fmd_event_t *ffep,
    fmd_case_t *fmcase, struct event *sp, const char **enamep,
    const struct ipath **ippp)
{
	struct node *serdinst;
	char *serdname;
	struct node *nid;
	struct serd_entry *newentp;

	ASSERT(sp->t == N_UPSET);
	ASSERT(ffep != NULL);

	/*
	 * obtain instanced SERD engine from the upset sp.  from this
	 * derive serdname, the string used to identify the SERD engine.
	 */
	serdinst = eventprop_lookup(sp, L_engine);

	if (serdinst == NULL)
		return (NULL);

	serdname = ipath2str(serdinst->u.stmt.np->u.event.ename->u.name.s,
	    ipath(serdinst->u.stmt.np->u.event.epname));

	/* handle serd engine "id" property, if there is one */
	if ((nid =
	    lut_lookup(serdinst->u.stmt.lutp, (void *)L_id, NULL)) != NULL) {
		struct evalue *gval;
		char suffixbuf[200];
		char *suffix;
		char *nserdname;
		size_t nname;

		out(O_ALTFP|O_NONL, "serd \"%s\" id: ", serdname);
		ptree_name_iter(O_ALTFP|O_NONL, nid);

		ASSERTinfo(nid->t == T_GLOBID, ptree_nodetype2str(nid->t));

		if ((gval = lut_lookup(fmep->globals,
		    (void *)nid->u.globid.s, NULL)) == NULL) {
			out(O_ALTFP, " undefined");
		} else if (gval->t == UINT64) {
			out(O_ALTFP, " %llu", gval->v);
			(void) sprintf(suffixbuf, "%llu", gval->v);
			suffix = suffixbuf;
		} else {
			out(O_ALTFP, " \"%s\"", (char *)(uintptr_t)gval->v);
			suffix = (char *)(uintptr_t)gval->v;
		}

		nname = strlen(serdname) + strlen(suffix) + 2;
		nserdname = MALLOC(nname);
		(void) snprintf(nserdname, nname, "%s:%s", serdname, suffix);
		FREE(serdname);
		serdname = nserdname;
	}

	if (!fmd_serd_exists(hdl, serdname)) {
		struct node *nN, *nT;
		const char *s;
		struct node *nodep;
		struct config *cp;
		char *path;
		uint_t nval;
		hrtime_t tval;
		const char *name;
		char *serd_name;
		int i;
		char *ptr;
		int got_n_override = 0, got_t_override = 0;

		/* no SERD engine yet, so create it */
		nodep = serdinst->u.stmt.np->u.event.epname;
		name = serdinst->u.stmt.np->u.event.ename->u.name.s;
		path = ipath2str(NULL, ipath(nodep));
		cp = config_lookup(fmep->config, path, 0);
		FREE((void *)path);

		/*
		 * We allow serd paramaters to be overridden, either from
		 * eft.conf file values (if Serd_Override is set) or from
		 * driver properties (for "serd.io.device" engines).
		 */
		if (Serd_Override != NULL) {
			char *save_ptr, *ptr1, *ptr2, *ptr3;
			ptr3 = save_ptr = STRDUP(Serd_Override);
			while (*ptr3 != '\0') {
				ptr1 = strchr(ptr3, ',');
				*ptr1 = '\0';
				if (strcmp(ptr3, name) == 0) {
					ptr2 =  strchr(ptr1 + 1, ',');
					*ptr2 = '\0';
					nval = atoi(ptr1 + 1);
					out(O_ALTFP, "serd override %s_n %d",
					    name, nval);
					ptr3 =  strchr(ptr2 + 1, ' ');
					if (ptr3)
						*ptr3 = '\0';
					ptr = STRDUP(ptr2 + 1);
					out(O_ALTFP, "serd override %s_t %s",
					    name, ptr);
					got_n_override = 1;
					got_t_override = 1;
					break;
				} else {
					ptr2 =  strchr(ptr1 + 1, ',');
					ptr3 =  strchr(ptr2 + 1, ' ');
					if (ptr3 == NULL)
						break;
				}
				ptr3++;
			}
			FREE(save_ptr);
		}

		if (cp && got_n_override == 0) {
			/*
			 * convert serd engine name into property name
			 */
			serd_name = MALLOC(strlen(name) + 3);
			for (i = 0; i < strlen(name); i++) {
				if (name[i] == '.')
					serd_name[i] = '_';
				else
					serd_name[i] = name[i];
			}
			serd_name[i++] = '_';
			serd_name[i++] = 'n';
			serd_name[i] = '\0';
			if (s = config_getprop(cp, serd_name)) {
				nval = atoi(s);
				out(O_ALTFP, "serd override %s_n %s", name, s);
				got_n_override = 1;
			}
			serd_name[i - 1] = 't';
			if (s = config_getprop(cp, serd_name)) {
				ptr = STRDUP(s);
				out(O_ALTFP, "serd override %s_t %s", name, s);
				got_t_override = 1;
			}
			FREE(serd_name);
		}

		if (!got_n_override) {
			nN = lut_lookup(serdinst->u.stmt.lutp, (void *)L_N,
			    NULL);
			ASSERT(nN->t == T_NUM);
			nval = (uint_t)nN->u.ull;
		}
		if (!got_t_override) {
			nT = lut_lookup(serdinst->u.stmt.lutp, (void *)L_T,
			    NULL);
			ASSERT(nT->t == T_TIMEVAL);
			tval = (hrtime_t)nT->u.ull;
		} else {
			const unsigned long long *ullp;
			const char *suffix;
			int len;

			len = strspn(ptr, "0123456789");
			suffix = stable(&ptr[len]);
			ullp = (unsigned long long *)lut_lookup(Timesuffixlut,
			    (void *)suffix, NULL);
			ptr[len] = '\0';
			tval = (unsigned long long)strtoul(ptr, NULL, 0) *
			    (ullp ? *ullp : 1ll);
			FREE(ptr);
		}
		fmd_serd_create(hdl, serdname, nval, tval);
	}

	newentp = MALLOC(sizeof (*newentp));
	newentp->ename = stable(serdinst->u.stmt.np->u.event.ename->u.name.s);
	newentp->ipath = ipath(serdinst->u.stmt.np->u.event.epname);
	newentp->hdl = hdl;
	if (lut_lookup(SerdEngines, newentp, (lut_cmp)serd_cmp) == NULL) {
		SerdEngines = lut_add(SerdEngines, (void *)newentp,
		    (void *)newentp, (lut_cmp)serd_cmp);
		Serd_need_save = 1;
		serd_save();
	} else {
		FREE(newentp);
	}


	/*
	 * increment SERD engine.  if engine fires, reset serd
	 * engine and return trip_strcode
	 */
	if (fmd_serd_record(hdl, serdname, ffep)) {
		struct node *tripinst = lut_lookup(serdinst->u.stmt.lutp,
		    (void *)L_trip, NULL);

		ASSERT(tripinst != NULL);

		*enamep = tripinst->u.event.ename->u.name.s;
		*ippp = ipath(tripinst->u.event.epname);

		fmd_case_add_serd(hdl, fmcase, serdname);
		fmd_serd_reset(hdl, serdname);
		out(O_ALTFP|O_NONL, "[engine fired: %s, sending: ", serdname);
		ipath_print(O_ALTFP|O_NONL, *enamep, *ippp);
		out(O_ALTFP, "]");

		FREE(serdname);
		return (1);
	}

	FREE(serdname);
	return (0);
}

/*
 * search a suspect list for upsets.  feed each upset to serd_eval() and
 * build up tripped[], an array of ereports produced by the firing of
 * any SERD engines.  then feed each ereport back into
 * fme_receive_report().
 *
 * returns ntrip, the number of these ereports produced.
 */
static int
upsets_eval(struct fme *fmep, fmd_event_t *ffep)
{
	/* we build an array of tripped ereports that we send ourselves */
	struct {
		const char *ename;
		const struct ipath *ipp;
	} *tripped;
	struct event *sp;
	int ntrip, nupset, i;

	/*
	 * count the number of upsets to determine the upper limit on
	 * expected trip ereport strings.  remember that one upset can
	 * lead to at most one ereport.
	 */
	nupset = 0;
	for (sp = fmep->suspects; sp; sp = sp->suspects) {
		if (sp->t == N_UPSET)
			nupset++;
	}

	if (nupset == 0)
		return (0);

	/*
	 * get to this point if we have upsets and expect some trip
	 * ereports
	 */
	tripped = alloca(sizeof (*tripped) * nupset);
	bzero((void *)tripped, sizeof (*tripped) * nupset);

	ntrip = 0;
	for (sp = fmep->suspects; sp; sp = sp->suspects)
		if (sp->t == N_UPSET &&
		    serd_eval(fmep, fmep->hdl, ffep, fmep->fmcase, sp,
		    &tripped[ntrip].ename, &tripped[ntrip].ipp))
			ntrip++;

	for (i = 0; i < ntrip; i++) {
		struct event *ep, *nep;
		struct fme *nfmep;
		fmd_case_t *fmcase;
		const struct ipath *ipp;
		const char *eventstring;
		int prev_verbose;
		unsigned long long my_delay = TIMEVAL_EVENTUALLY;
		enum fme_state state;

		/*
		 * First try and evaluate a case with the trip ereport plus
		 * all the other ereports that cause the trip. If that fails
		 * to evaluate then try again with just this ereport on its own.
		 */
		out(O_ALTFP|O_NONL, "fme_receive_report_serd: ");
		ipath_print(O_ALTFP|O_NONL, tripped[i].ename, tripped[i].ipp);
		out(O_ALTFP|O_STAMP, NULL);
		ep = fmep->e0;
		eventstring = ep->enode->u.event.ename->u.name.s;
		ipp = ep->ipp;
		prune_propagations(eventstring, ipp);

		/*
		 * create a duplicate fme and case
		 */
		fmcase = fmd_case_open(fmep->hdl, NULL);
		out(O_ALTFP|O_NONL, "duplicate fme for event [");
		ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
		out(O_ALTFP, " ]");
		if ((nfmep = newfme(eventstring, ipp, fmep->hdl,
		    fmcase)) == NULL) {
			out(O_ALTFP|O_NONL, "[");
			ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
			out(O_ALTFP, " CANNOT DIAGNOSE]");
			publish_undiagnosable(fmep->hdl, ffep, fmcase);
			continue;
		}
		Open_fme_count++;
		nfmep->pull = fmep->pull;
		init_fme_bufs(nfmep);
		out(O_ALTFP|O_NONL, "[");
		ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
		out(O_ALTFP, " created FME%d, case %s]", nfmep->id,
		    fmd_case_uuid(nfmep->hdl, nfmep->fmcase));
		if (ffep) {
			fmd_case_setprincipal(nfmep->hdl, nfmep->fmcase, ffep);
			nfmep->e0r = ffep;
		}

		/*
		 * add the original ereports
		 */
		for (ep = fmep->observations; ep; ep = ep->observations) {
			eventstring = ep->enode->u.event.ename->u.name.s;
			ipp = ep->ipp;
			out(O_ALTFP|O_NONL, "adding event [");
			ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
			out(O_ALTFP, " ]");
			nep = itree_lookup(nfmep->eventtree, eventstring, ipp);
			if (nep->count++ == 0) {
				nep->observations = nfmep->observations;
				nfmep->observations = nep;
				serialize_observation(nfmep, eventstring, ipp);
				nep->nvp = evnv_dupnvl(ep->nvp);
			}
			if (ffep)
				fmd_case_add_ereport(nfmep->hdl, nfmep->fmcase,
				    ffep);
			stats_counter_bump(nfmep->Rcount);
		}

		/*
		 * add the serd trigger ereport
		 */
		if ((ep = itree_lookup(nfmep->eventtree, tripped[i].ename,
		    tripped[i].ipp)) == NULL) {
			/*
			 * The trigger ereport is not in the instance tree. It
			 * was presumably removed by prune_propagations() as
			 * this combination of events is not present in the
			 * rules.
			 */
			out(O_ALTFP, "upsets_eval: e0 not in instance tree");
			Undiag_reason = UD_BADEVENTI;
			goto retry_lone_ereport;
		}
		out(O_ALTFP|O_NONL, "adding event [");
		ipath_print(O_ALTFP|O_NONL, tripped[i].ename, tripped[i].ipp);
		out(O_ALTFP, " ]");
		nfmep->ecurrent = ep;
		ep->nvp = NULL;
		ep->count = 1;
		ep->observations = nfmep->observations;
		nfmep->observations = ep;

		/*
		 * just peek first.
		 */
		nfmep->peek = 1;
		prev_verbose = Verbose;
		if (Debug == 0)
			Verbose = 0;
		lut_walk(nfmep->eventtree, (lut_cb)clear_arrows, (void *)nfmep);
		state = hypothesise(nfmep, nfmep->e0, nfmep->ull, &my_delay);
		nfmep->peek = 0;
		Verbose = prev_verbose;
		if (state == FME_DISPROVED) {
			out(O_ALTFP, "upsets_eval: hypothesis disproved");
			Undiag_reason = UD_UNSOLVD;
retry_lone_ereport:
			/*
			 * However the trigger ereport on its own might be
			 * diagnosable, so check for that. Undo the new fme
			 * and case we just created and call fme_receive_report.
			 */
			out(O_ALTFP|O_NONL, "[");
			ipath_print(O_ALTFP|O_NONL, tripped[i].ename,
			    tripped[i].ipp);
			out(O_ALTFP, " retrying with just trigger ereport]");
			itree_free(nfmep->eventtree);
			nfmep->eventtree = NULL;
			structconfig_free(nfmep->config);
			nfmep->config = NULL;
			destroy_fme_bufs(nfmep);
			fmd_case_close(nfmep->hdl, nfmep->fmcase);
			fme_receive_report(fmep->hdl, ffep,
			    tripped[i].ename, tripped[i].ipp, NULL);
			continue;
		}

		/*
		 * and evaluate
		 */
		serialize_observation(nfmep, tripped[i].ename, tripped[i].ipp);
		if (ffep)
			fmd_case_add_ereport(nfmep->hdl, nfmep->fmcase, ffep);
		stats_counter_bump(nfmep->Rcount);
		fme_eval(nfmep, ffep);
	}

	return (ntrip);
}

/*
 * fme_receive_external_report -- call when an external ereport comes in
 *
 * this routine just converts the relevant information from the ereport
 * into a format used internally and passes it on to fme_receive_report().
 */
void
fme_receive_external_report(fmd_hdl_t *hdl, fmd_event_t *ffep, nvlist_t *nvl,
    const char *eventstring)
{
	struct node *epnamenp = platform_getpath(nvl);
	const struct ipath *ipp;

	/*
	 * XFILE: If we ended up without a path, it's an X-file.
	 * For now, use our undiagnosable interface.
	 */
	if (epnamenp == NULL) {
		fmd_case_t *fmcase;

		out(O_ALTFP, "XFILE: Unable to get path from ereport");
		Undiag_reason = UD_NOPATH;
		fmcase = fmd_case_open(hdl, NULL);
		publish_undiagnosable(hdl, ffep, fmcase);
		return;
	}

	ipp = ipath(epnamenp);
	tree_free(epnamenp);
	fme_receive_report(hdl, ffep, stable(eventstring), ipp, nvl);
}

/*ARGSUSED*/
void
fme_receive_repair_list(fmd_hdl_t *hdl, fmd_event_t *ffep, nvlist_t *nvl,
    const char *eventstring)
{
	char *uuid;
	nvlist_t **nva;
	uint_t nvc;
	const struct ipath *ipp;

	if (nvlist_lookup_string(nvl, FM_SUSPECT_UUID, &uuid) != 0 ||
	    nvlist_lookup_nvlist_array(nvl, FM_SUSPECT_FAULT_LIST,
	    &nva, &nvc) != 0) {
		out(O_ALTFP, "No uuid or fault list for list.repaired event");
		return;
	}

	out(O_ALTFP, "Processing list.repaired from case %s", uuid);

	while (nvc-- != 0) {
		/*
		 * Reset any istat or serd engine associated with this path.
		 */
		char *path;

		if ((ipp = platform_fault2ipath(*nva++)) == NULL)
			continue;

		path = ipath2str(NULL, ipp);
		out(O_ALTFP, "fme_receive_repair_list: resetting state for %s",
		    path);
		FREE(path);

		lut_walk(Istats, (lut_cb)istat_counter_reset_cb, (void *)ipp);
		istat_save();

		lut_walk(SerdEngines, (lut_cb)serd_reset_cb, (void *)ipp);
		serd_save();
	}
}

/*ARGSUSED*/
void
fme_receive_topology_change(void)
{
	lut_walk(Istats, (lut_cb)istat_counter_topo_chg_cb, NULL);
	istat_save();

	lut_walk(SerdEngines, (lut_cb)serd_topo_chg_cb, NULL);
	serd_save();
}

static int mark_arrows(struct fme *fmep, struct event *ep, int mark,
    unsigned long long at_latest_by, unsigned long long *pdelay, int keep);

/* ARGSUSED */
static void
clear_arrows(struct event *ep, struct event *ep2, struct fme *fmep)
{
	struct bubble *bp;
	struct arrowlist *ap;

	ep->cached_state = 0;
	ep->keep_in_tree = 0;
	for (bp = itree_next_bubble(ep, NULL); bp;
	    bp = itree_next_bubble(ep, bp)) {
		if (bp->t != B_FROM)
			continue;
		bp->mark = 0;
		for (ap = itree_next_arrow(bp, NULL); ap;
		    ap = itree_next_arrow(bp, ap))
			ap->arrowp->mark = 0;
	}
}

static void
fme_receive_report(fmd_hdl_t *hdl, fmd_event_t *ffep,
    const char *eventstring, const struct ipath *ipp, nvlist_t *nvl)
{
	struct event *ep;
	struct fme *fmep = NULL;
	struct fme *ofmep = NULL;
	struct fme *cfmep, *svfmep;
	int matched = 0;
	nvlist_t *defect;
	fmd_case_t *fmcase;

	out(O_ALTFP|O_NONL, "fme_receive_report: ");
	ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
	out(O_ALTFP|O_STAMP, NULL);

	/* decide which FME it goes to */
	for (fmep = FMElist; fmep; fmep = fmep->next) {
		int prev_verbose;
		unsigned long long my_delay = TIMEVAL_EVENTUALLY;
		enum fme_state state;
		nvlist_t *pre_peek_nvp = NULL;

		if (fmep->overflow) {
			if (!(fmd_case_closed(fmep->hdl, fmep->fmcase)))
				ofmep = fmep;

			continue;
		}

		/*
		 * ignore solved or closed cases
		 */
		if (fmep->posted_suspects ||
		    fmd_case_solved(fmep->hdl, fmep->fmcase) ||
		    fmd_case_closed(fmep->hdl, fmep->fmcase))
			continue;

		/* look up event in event tree for this FME */
		if ((ep = itree_lookup(fmep->eventtree,
		    eventstring, ipp)) == NULL)
			continue;

		/* note observation */
		fmep->ecurrent = ep;
		if (ep->count++ == 0) {
			/* link it into list of observations seen */
			ep->observations = fmep->observations;
			fmep->observations = ep;
			ep->nvp = evnv_dupnvl(nvl);
		} else {
			/* use new payload values for peek */
			pre_peek_nvp = ep->nvp;
			ep->nvp = evnv_dupnvl(nvl);
		}

		/* tell hypothesise() not to mess with suspect list */
		fmep->peek = 1;

		/* don't want this to be verbose (unless Debug is set) */
		prev_verbose = Verbose;
		if (Debug == 0)
			Verbose = 0;

		lut_walk(fmep->eventtree, (lut_cb)clear_arrows, (void *)fmep);
		state = hypothesise(fmep, fmep->e0, fmep->ull, &my_delay);

		fmep->peek = 0;

		/* put verbose flag back */
		Verbose = prev_verbose;

		if (state != FME_DISPROVED) {
			/* found an FME that explains the ereport */
			matched++;
			out(O_ALTFP|O_NONL, "[");
			ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
			out(O_ALTFP, " explained by FME%d]", fmep->id);

			if (pre_peek_nvp)
				nvlist_free(pre_peek_nvp);

			if (ep->count == 1)
				serialize_observation(fmep, eventstring, ipp);

			if (ffep)
				fmd_case_add_ereport(hdl, fmep->fmcase, ffep);

			stats_counter_bump(fmep->Rcount);

			/* re-eval FME */
			fme_eval(fmep, ffep);
		} else {

			/* not a match, undo noting of observation */
			fmep->ecurrent = NULL;
			if (--ep->count == 0) {
				/* unlink it from observations */
				fmep->observations = ep->observations;
				ep->observations = NULL;
				nvlist_free(ep->nvp);
				ep->nvp = NULL;
			} else {
				nvlist_free(ep->nvp);
				ep->nvp = pre_peek_nvp;
			}
		}
	}

	if (matched)
		return;	/* explained by at least one existing FME */

	/* clean up closed fmes */
	cfmep = ClosedFMEs;
	while (cfmep != NULL) {
		svfmep = cfmep->next;
		destroy_fme(cfmep);
		cfmep = svfmep;
	}
	ClosedFMEs = NULL;
	prune_propagations(eventstring, ipp);

	if (ofmep) {
		out(O_ALTFP|O_NONL, "[");
		ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
		out(O_ALTFP, " ADDING TO OVERFLOW FME]");
		if (ffep)
			fmd_case_add_ereport(hdl, ofmep->fmcase, ffep);

		return;

	} else if (Max_fme && (Open_fme_count >= Max_fme)) {
		out(O_ALTFP|O_NONL, "[");
		ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
		out(O_ALTFP, " MAX OPEN FME REACHED]");

		fmcase = fmd_case_open(hdl, NULL);

		/* Create overflow fme */
		if ((fmep = newfme(eventstring, ipp, hdl, fmcase)) == NULL) {
			out(O_ALTFP|O_NONL, "[");
			ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
			out(O_ALTFP, " CANNOT OPEN OVERFLOW FME]");
			publish_undiagnosable(hdl, ffep, fmcase);
			return;
		}

		Open_fme_count++;

		init_fme_bufs(fmep);
		fmep->overflow = B_TRUE;

		if (ffep)
			fmd_case_add_ereport(hdl, fmep->fmcase, ffep);

		defect = fmd_nvl_create_fault(hdl, UNDIAGNOSABLE_DEFECT, 100,
		    NULL, NULL, NULL);
		(void) nvlist_add_string(defect, UNDIAG_REASON, UD_MAXFME);
		fmd_case_add_suspect(hdl, fmep->fmcase, defect);
		fmd_case_solve(hdl, fmep->fmcase);
		return;
	}

	/* open a case */
	fmcase = fmd_case_open(hdl, NULL);

	/* start a new FME */
	if ((fmep = newfme(eventstring, ipp, hdl, fmcase)) == NULL) {
		out(O_ALTFP|O_NONL, "[");
		ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
		out(O_ALTFP, " CANNOT DIAGNOSE]");
		publish_undiagnosable(hdl, ffep, fmcase);
		return;
	}

	Open_fme_count++;

	init_fme_bufs(fmep);

	out(O_ALTFP|O_NONL, "[");
	ipath_print(O_ALTFP|O_NONL, eventstring, ipp);
	out(O_ALTFP, " created FME%d, case %s]", fmep->id,
	    fmd_case_uuid(hdl, fmep->fmcase));

	ep = fmep->e0;
	ASSERT(ep != NULL);

	/* note observation */
	fmep->ecurrent = ep;
	if (ep->count++ == 0) {
		/* link it into list of observations seen */
		ep->observations = fmep->observations;
		fmep->observations = ep;
		ep->nvp = evnv_dupnvl(nvl);
		serialize_observation(fmep, eventstring, ipp);
	} else {
		/* new payload overrides any previous */
		nvlist_free(ep->nvp);
		ep->nvp = evnv_dupnvl(nvl);
	}

	stats_counter_bump(fmep->Rcount);

	if (ffep) {
		fmd_case_add_ereport(hdl, fmep->fmcase, ffep);
		fmd_case_setprincipal(hdl, fmep->fmcase, ffep);
		fmep->e0r = ffep;
	}

	/* give the diagnosis algorithm a shot at the new FME state */
	fme_eval(fmep, ffep);
}

void
fme_status(int flags)
{
	struct fme *fmep;

	if (FMElist == NULL) {
		out(flags, "No fault management exercises underway.");
		return;
	}

	for (fmep = FMElist; fmep; fmep = fmep->next)
		fme_print(flags, fmep);
}

/*
 * "indent" routines used mostly for nicely formatted debug output, but also
 * for sanity checking for infinite recursion bugs.
 */

#define	MAX_INDENT 1024
static const char *indent_s[MAX_INDENT];
static int current_indent;

static void
indent_push(const char *s)
{
	if (current_indent < MAX_INDENT)
		indent_s[current_indent++] = s;
	else
		out(O_DIE, "unexpected recursion depth (%d)", current_indent);
}

static void
indent_set(const char *s)
{
	current_indent = 0;
	indent_push(s);
}

static void
indent_pop(void)
{
	if (current_indent > 0)
		current_indent--;
	else
		out(O_DIE, "recursion underflow");
}

static void
indent(void)
{
	int i;
	if (!Verbose)
		return;
	for (i = 0; i < current_indent; i++)
		out(O_ALTFP|O_VERB|O_NONL, indent_s[i]);
}

#define	SLNEW		1
#define	SLCHANGED	2
#define	SLWAIT		3
#define	SLDISPROVED	4

static void
print_suspects(int circumstance, struct fme *fmep)
{
	struct event *ep;

	out(O_ALTFP|O_NONL, "[");
	if (circumstance == SLCHANGED) {
		out(O_ALTFP|O_NONL, "FME%d diagnosis changed. state: %s, "
		    "suspect list:", fmep->id, fme_state2str(fmep->state));
	} else if (circumstance == SLWAIT) {
		out(O_ALTFP|O_NONL, "FME%d set wait timer %ld ", fmep->id,
		    fmep->timer);
		ptree_timeval(O_ALTFP|O_NONL, &fmep->wull);
	} else if (circumstance == SLDISPROVED) {
		out(O_ALTFP|O_NONL, "FME%d DIAGNOSIS UNKNOWN", fmep->id);
	} else {
		out(O_ALTFP|O_NONL, "FME%d DIAGNOSIS PRODUCED:", fmep->id);
	}

	if (circumstance == SLWAIT || circumstance == SLDISPROVED) {
		out(O_ALTFP, "]");
		return;
	}

	for (ep = fmep->suspects; ep; ep = ep->suspects) {
		out(O_ALTFP|O_NONL, " ");
		itree_pevent_brief(O_ALTFP|O_NONL, ep);
	}
	out(O_ALTFP, "]");
}

static struct node *
eventprop_lookup(struct event *ep, const char *propname)
{
	return (lut_lookup(ep->props, (void *)propname, NULL));
}

#define	MAXDIGITIDX	23
static char numbuf[MAXDIGITIDX + 1];

static int
node2uint(struct node *n, uint_t *valp)
{
	struct evalue value;
	struct lut *globals = NULL;

	if (n == NULL)
		return (1);

	/*
	 * check value.v since we are being asked to convert an unsigned
	 * long long int to an unsigned int
	 */
	if (! eval_expr(n, NULL, NULL, &globals, NULL, NULL, 0, &value) ||
	    value.t != UINT64 || value.v > (1ULL << 32))
		return (1);

	*valp = (uint_t)value.v;

	return (0);
}

static nvlist_t *
node2fmri(struct node *n)
{
	nvlist_t **pa, *f, *p;
	struct node *nc;
	uint_t depth = 0;
	char *numstr, *nullbyte;
	char *failure;
	int err, i;

	/* XXX do we need to be able to handle a non-T_NAME node? */
	if (n == NULL || n->t != T_NAME)
		return (NULL);

	for (nc = n; nc != NULL; nc = nc->u.name.next) {
		if (nc->u.name.child == NULL || nc->u.name.child->t != T_NUM)
			break;
		depth++;
	}

	if (nc != NULL) {
		/* We bailed early, something went wrong */
		return (NULL);
	}

	if ((err = nvlist_xalloc(&f, NV_UNIQUE_NAME, &Eft_nv_hdl)) != 0)
		out(O_DIE|O_SYS, "alloc of fmri nvl failed");
	pa = alloca(depth * sizeof (nvlist_t *));
	for (i = 0; i < depth; i++)
		pa[i] = NULL;

	err = nvlist_add_string(f, FM_FMRI_SCHEME, FM_FMRI_SCHEME_HC);
	err |= nvlist_add_uint8(f, FM_VERSION, FM_HC_SCHEME_VERSION);
	err |= nvlist_add_string(f, FM_FMRI_HC_ROOT, "");
	err |= nvlist_add_uint32(f, FM_FMRI_HC_LIST_SZ, depth);
	if (err != 0) {
		failure = "basic construction of FMRI failed";
		goto boom;
	}

	numbuf[MAXDIGITIDX] = '\0';
	nullbyte = &numbuf[MAXDIGITIDX];
	i = 0;

	for (nc = n; nc != NULL; nc = nc->u.name.next) {
		err = nvlist_xalloc(&p, NV_UNIQUE_NAME, &Eft_nv_hdl);
		if (err != 0) {
			failure = "alloc of an hc-pair failed";
			goto boom;
		}
		err = nvlist_add_string(p, FM_FMRI_HC_NAME, nc->u.name.s);
		numstr = ulltostr(nc->u.name.child->u.ull, nullbyte);
		err |= nvlist_add_string(p, FM_FMRI_HC_ID, numstr);
		if (err != 0) {
			failure = "construction of an hc-pair failed";
			goto boom;
		}
		pa[i++] = p;
	}

	err = nvlist_add_nvlist_array(f, FM_FMRI_HC_LIST, pa, depth);
	if (err == 0) {
		for (i = 0; i < depth; i++)
			if (pa[i] != NULL)
				nvlist_free(pa[i]);
		return (f);
	}
	failure = "addition of hc-pair array to FMRI failed";

boom:
	for (i = 0; i < depth; i++)
		if (pa[i] != NULL)
			nvlist_free(pa[i]);
	nvlist_free(f);
	out(O_DIE, "%s", failure);
	/*NOTREACHED*/
	return (NULL);
}

/* an ipath cache entry is an array of these, with s==NULL at the end */
struct ipath {
	const char *s;	/* component name (in stable) */
	int i;		/* instance number */
};

static nvlist_t *
ipath2fmri(struct ipath *ipath)
{
	nvlist_t **pa, *f, *p;
	uint_t depth = 0;
	char *numstr, *nullbyte;
	char *failure;
	int err, i;
	struct ipath *ipp;

	for (ipp = ipath; ipp->s != NULL; ipp++)
		depth++;

	if ((err = nvlist_xalloc(&f, NV_UNIQUE_NAME, &Eft_nv_hdl)) != 0)
		out(O_DIE|O_SYS, "alloc of fmri nvl failed");
	pa = alloca(depth * sizeof (nvlist_t *));
	for (i = 0; i < depth; i++)
		pa[i] = NULL;

	err = nvlist_add_string(f, FM_FMRI_SCHEME, FM_FMRI_SCHEME_HC);
	err |= nvlist_add_uint8(f, FM_VERSION, FM_HC_SCHEME_VERSION);
	err |= nvlist_add_string(f, FM_FMRI_HC_ROOT, "");
	err |= nvlist_add_uint32(f, FM_FMRI_HC_LIST_SZ, depth);
	if (err != 0) {
		failure = "basic construction of FMRI failed";
		goto boom;
	}

	numbuf[MAXDIGITIDX] = '\0';
	nullbyte = &numbuf[MAXDIGITIDX];
	i = 0;

	for (ipp = ipath; ipp->s != NULL; ipp++) {
		err = nvlist_xalloc(&p, NV_UNIQUE_NAME, &Eft_nv_hdl);
		if (err != 0) {
			failure = "alloc of an hc-pair failed";
			goto boom;
		}
		err = nvlist_add_string(p, FM_FMRI_HC_NAME, ipp->s);
		numstr = ulltostr(ipp->i, nullbyte);
		err |= nvlist_add_string(p, FM_FMRI_HC_ID, numstr);
		if (err != 0) {
			failure = "construction of an hc-pair failed";
			goto boom;
		}
		pa[i++] = p;
	}

	err = nvlist_add_nvlist_array(f, FM_FMRI_HC_LIST, pa, depth);
	if (err == 0) {
		for (i = 0; i < depth; i++)
			if (pa[i] != NULL)
				nvlist_free(pa[i]);
		return (f);
	}
	failure = "addition of hc-pair array to FMRI failed";

boom:
	for (i = 0; i < depth; i++)
		if (pa[i] != NULL)
			nvlist_free(pa[i]);
	nvlist_free(f);
	out(O_DIE, "%s", failure);
	/*NOTREACHED*/
	return (NULL);
}

static uint_t
avg(uint_t sum, uint_t cnt)
{
	unsigned long long s = sum * 10;

	return ((s / cnt / 10) + (((s / cnt % 10) >= 5) ? 1 : 0));
}

static uint8_t
percentof(uint_t part, uint_t whole)
{
	unsigned long long p = part * 1000;

	return ((p / whole / 10) + (((p / whole % 10) >= 5) ? 1 : 0));
}

struct rsl {
	struct event *suspect;
	nvlist_t *asru;
	nvlist_t *fru;
	nvlist_t *rsrc;
};

/*
 *  rslfree -- free internal members of struct rsl not expected to be
 *	freed elsewhere.
 */
static void
rslfree(struct rsl *freeme)
{
	if (freeme->asru != NULL)
		nvlist_free(freeme->asru);
	if (freeme->fru != NULL)
		nvlist_free(freeme->fru);
	if (freeme->rsrc != NULL && freeme->rsrc != freeme->asru)
		nvlist_free(freeme->rsrc);
}

/*
 *  rslcmp -- compare two rsl structures.  Use the following
 *	comparisons to establish cardinality:
 *
 *	1. Name of the suspect's class. (simple strcmp)
 *	2. Name of the suspect's ASRU. (trickier, since nvlist)
 *
 */
static int
rslcmp(const void *a, const void *b)
{
	struct rsl *r1 = (struct rsl *)a;
	struct rsl *r2 = (struct rsl *)b;
	int rv;

	rv = strcmp(r1->suspect->enode->u.event.ename->u.name.s,
	    r2->suspect->enode->u.event.ename->u.name.s);
	if (rv != 0)
		return (rv);

	if (r1->asru == NULL && r2->asru == NULL)
		return (0);
	if (r1->asru == NULL)
		return (-1);
	if (r2->asru == NULL)
		return (1);
	return (evnv_cmpnvl(r1->asru, r2->asru, 0));
}

/*
 *  rsluniq -- given an array of rsl structures, seek out and "remove"
 *	any duplicates.  Dups are "remove"d by NULLing the suspect pointer
 *	of the array element.  Removal also means updating the number of
 *	problems and the number of problems which are not faults.  User
 *	provides the first and last element pointers.
 */
static void
rsluniq(struct rsl *first, struct rsl *last, int *nprobs, int *nnonf)
{
	struct rsl *cr;

	if (*nprobs == 1)
		return;

	/*
	 *  At this point, we only expect duplicate defects.
	 *  Eversholt's diagnosis algorithm prevents duplicate
	 *  suspects, but we rewrite defects in the platform code after
	 *  the diagnosis is made, and that can introduce new
	 *  duplicates.
	 */
	while (first <= last) {
		if (first->suspect == NULL || !is_defect(first->suspect->t)) {
			first++;
			continue;
		}
		cr = first + 1;
		while (cr <= last) {
			if (is_defect(first->suspect->t)) {
				if (rslcmp(first, cr) == 0) {
					cr->suspect = NULL;
					rslfree(cr);
					(*nprobs)--;
					(*nnonf)--;
				}
			}
			/*
			 * assume all defects are in order after our
			 * sort and short circuit here with "else break" ?
			 */
			cr++;
		}
		first++;
	}
}

/*
 * get_resources -- for a given suspect, determine what ASRU, FRU and
 *     RSRC nvlists should be advertised in the final suspect list.
 */
void
get_resources(struct event *sp, struct rsl *rsrcs, struct config *croot)
{
	struct node *asrudef, *frudef;
	nvlist_t *asru, *fru;
	nvlist_t *rsrc = NULL;
	char *pathstr;

	/*
	 * First find any ASRU and/or FRU defined in the
	 * initial fault tree.
	 */
	asrudef = eventprop_lookup(sp, L_ASRU);
	frudef = eventprop_lookup(sp, L_FRU);

	/*
	 * Create FMRIs based on those definitions
	 */
	asru = node2fmri(asrudef);
	fru = node2fmri(frudef);
	pathstr = ipath2str(NULL, sp->ipp);

	/*
	 * Allow for platform translations of the FMRIs
	 */
	platform_units_translate(is_defect(sp->t), croot, &asru, &fru, &rsrc,
	    pathstr);

	FREE(pathstr);
	rsrcs->suspect = sp;
	rsrcs->asru = asru;
	rsrcs->fru = fru;
	rsrcs->rsrc = rsrc;
}

/*
 * trim_suspects -- prior to publishing, we may need to remove some
 *    suspects from the list.  If we're auto-closing upsets, we don't
 *    want any of those in the published list.  If the ASRUs for multiple
 *    defects resolve to the same ASRU (driver) we only want to publish
 *    that as a single suspect.
 */
static void
trim_suspects(struct fme *fmep, boolean_t no_upsets, struct rsl **begin,
    struct rsl **end)
{
	struct event *ep;
	struct rsl *rp;
	int rpcnt;

	/*
	 * First save the suspects in the psuspects, then copy back
	 * only the ones we wish to retain.  This resets nsuspects to
	 * zero.
	 */
	rpcnt = fmep->nsuspects;
	save_suspects(fmep);

	/*
	 * allocate an array of resource pointers for the suspects.
	 * We may end up using less than the full allocation, but this
	 * is a very short-lived array.  publish_suspects() will free
	 * this array when it's done using it.
	 */
	rp = *begin = MALLOC(rpcnt * sizeof (struct rsl));
	bzero(rp, rpcnt * sizeof (struct rsl));

	/* first pass, remove any unwanted upsets and populate our array */
	for (ep = fmep->psuspects; ep; ep = ep->psuspects) {
		if (no_upsets && is_upset(ep->t))
			continue;
		get_resources(ep, rp, fmep->config);
		rp++;
		fmep->nsuspects++;
		if (!is_fault(ep->t))
			fmep->nonfault++;
	}

	/* if all we had was unwanted upsets, we're done */
	if (fmep->nsuspects == 0)
		return;

	*end = rp - 1;

	/* sort the array */
	qsort(*begin, fmep->nsuspects, sizeof (struct rsl), rslcmp);
	rsluniq(*begin, *end, &fmep->nsuspects, &fmep->nonfault);
}

/*
 * addpayloadprop -- add a payload prop to a problem
 */
static void
addpayloadprop(const char *lhs, struct evalue *rhs, nvlist_t *fault)
{
	ASSERT(fault != NULL);
	ASSERT(lhs != NULL);
	ASSERT(rhs != NULL);

	if (rhs->t == UINT64) {
		out(O_ALTFP|O_VERB2, "addpayloadprop: %s=%llu", lhs, rhs->v);

		if (nvlist_add_uint64(fault, lhs, rhs->v) != 0)
			out(O_DIE,
			    "cannot add payloadprop \"%s\" to fault", lhs);
	} else {
		out(O_ALTFP|O_VERB2, "addpayloadprop: %s=\"%s\"",
		    lhs, (char *)(uintptr_t)rhs->v);

		if (nvlist_add_string(fault, lhs, (char *)(uintptr_t)rhs->v) !=
		    0)
			out(O_DIE,
			    "cannot add payloadprop \"%s\" to fault", lhs);
	}
}

static char *Istatbuf;
static char *Istatbufptr;
static int Istatsz;

/*
 * istataddsize -- calculate size of istat and add it to Istatsz
 */
/*ARGSUSED2*/
static void
istataddsize(const struct istat_entry *lhs, struct stats *rhs, void *arg)
{
	int val;

	ASSERT(lhs != NULL);
	ASSERT(rhs != NULL);

	if ((val = stats_counter_value(rhs)) == 0)
		return;	/* skip zero-valued stats */

	/* count up the size of the stat name */
	Istatsz += ipath2strlen(lhs->ename, lhs->ipath);
	Istatsz++;	/* for the trailing NULL byte */

	/* count up the size of the stat value */
	Istatsz += snprintf(NULL, 0, "%d", val);
	Istatsz++;	/* for the trailing NULL byte */
}

/*
 * istat2str -- serialize an istat, writing result to *Istatbufptr
 */
/*ARGSUSED2*/
static void
istat2str(const struct istat_entry *lhs, struct stats *rhs, void *arg)
{
	char *str;
	int len;
	int val;

	ASSERT(lhs != NULL);
	ASSERT(rhs != NULL);

	if ((val = stats_counter_value(rhs)) == 0)
		return;	/* skip zero-valued stats */

	/* serialize the stat name */
	str = ipath2str(lhs->ename, lhs->ipath);
	len = strlen(str);

	ASSERT(Istatbufptr + len + 1 < &Istatbuf[Istatsz]);
	(void) strlcpy(Istatbufptr, str, &Istatbuf[Istatsz] - Istatbufptr);
	Istatbufptr += len;
	FREE(str);
	*Istatbufptr++ = '\0';

	/* serialize the stat value */
	Istatbufptr += snprintf(Istatbufptr, &Istatbuf[Istatsz] - Istatbufptr,
	    "%d", val);
	*Istatbufptr++ = '\0';

	ASSERT(Istatbufptr <= &Istatbuf[Istatsz]);
}

void
istat_save()
{
	if (Istat_need_save == 0)
		return;

	/* figure out how big the serialzed info is */
	Istatsz = 0;
	lut_walk(Istats, (lut_cb)istataddsize, NULL);

	if (Istatsz == 0) {
		/* no stats to save */
		fmd_buf_destroy(Hdl, NULL, WOBUF_ISTATS);
		return;
	}

	/* create the serialized buffer */
	Istatbufptr = Istatbuf = MALLOC(Istatsz);
	lut_walk(Istats, (lut_cb)istat2str, NULL);

	/* clear out current saved stats */
	fmd_buf_destroy(Hdl, NULL, WOBUF_ISTATS);

	/* write out the new version */
	fmd_buf_write(Hdl, NULL, WOBUF_ISTATS, Istatbuf, Istatsz);
	FREE(Istatbuf);

	Istat_need_save = 0;
}

int
istat_cmp(struct istat_entry *ent1, struct istat_entry *ent2)
{
	if (ent1->ename != ent2->ename)
		return (ent2->ename - ent1->ename);
	if (ent1->ipath != ent2->ipath)
		return ((char *)ent2->ipath - (char *)ent1->ipath);

	return (0);
}

/*
 * istat-verify -- verify the component associated with a stat still exists
 *
 * if the component no longer exists, this routine resets the stat and
 * returns 0.  if the component still exists, it returns 1.
 */
static int
istat_verify(struct node *snp, struct istat_entry *entp)
{
	struct stats *statp;
	nvlist_t *fmri;

	fmri = node2fmri(snp->u.event.epname);
	if (platform_path_exists(fmri)) {
		nvlist_free(fmri);
		return (1);
	}
	nvlist_free(fmri);

	/* component no longer in system.  zero out the associated stats */
	if ((statp = (struct stats *)
	    lut_lookup(Istats, entp, (lut_cmp)istat_cmp)) == NULL ||
	    stats_counter_value(statp) == 0)
		return (0);	/* stat is already reset */

	Istat_need_save = 1;
	stats_counter_reset(statp);
	return (0);
}

static void
istat_bump(struct node *snp, int n)
{
	struct stats *statp;
	struct istat_entry ent;

	ASSERT(snp != NULL);
	ASSERTinfo(snp->t == T_EVENT, ptree_nodetype2str(snp->t));
	ASSERT(snp->u.event.epname != NULL);

	/* class name should be hoisted into a single stable entry */
	ASSERT(snp->u.event.ename->u.name.next == NULL);
	ent.ename = snp->u.event.ename->u.name.s;
	ent.ipath = ipath(snp->u.event.epname);

	if (!istat_verify(snp, &ent)) {
		/* component no longer exists in system, nothing to do */
		return;
	}

	if ((statp = (struct stats *)
	    lut_lookup(Istats, &ent, (lut_cmp)istat_cmp)) == NULL) {
		/* need to create the counter */
		int cnt = 0;
		struct node *np;
		char *sname;
		char *snamep;
		struct istat_entry *newentp;

		/* count up the size of the stat name */
		np = snp->u.event.ename;
		while (np != NULL) {
			cnt += strlen(np->u.name.s);
			cnt++;	/* for the '.' or '@' */
			np = np->u.name.next;
		}
		np = snp->u.event.epname;
		while (np != NULL) {
			cnt += snprintf(NULL, 0, "%s%llu",
			    np->u.name.s, np->u.name.child->u.ull);
			cnt++;	/* for the '/' or trailing NULL byte */
			np = np->u.name.next;
		}

		/* build the stat name */
		snamep = sname = alloca(cnt);
		np = snp->u.event.ename;
		while (np != NULL) {
			snamep += snprintf(snamep, &sname[cnt] - snamep,
			    "%s", np->u.name.s);
			np = np->u.name.next;
			if (np)
				*snamep++ = '.';
		}
		*snamep++ = '@';
		np = snp->u.event.epname;
		while (np != NULL) {
			snamep += snprintf(snamep, &sname[cnt] - snamep,
			    "%s%llu", np->u.name.s, np->u.name.child->u.ull);
			np = np->u.name.next;
			if (np)
				*snamep++ = '/';
		}
		*snamep++ = '\0';

		/* create the new stat & add it to our list */
		newentp = MALLOC(sizeof (*newentp));
		*newentp = ent;
		statp = stats_new_counter(NULL, sname, 0);
		Istats = lut_add(Istats, (void *)newentp, (void *)statp,
		    (lut_cmp)istat_cmp);
	}

	/* if n is non-zero, set that value instead of bumping */
	if (n) {
		stats_counter_reset(statp);
		stats_counter_add(statp, n);
	} else
		stats_counter_bump(statp);
	Istat_need_save = 1;

	ipath_print(O_ALTFP|O_VERB2, ent.ename, ent.ipath);
	out(O_ALTFP|O_VERB2, " %s to value %d", n ? "set" : "incremented",
	    stats_counter_value(statp));
}

/*ARGSUSED*/
static void
istat_destructor(void *left, void *right, void *arg)
{
	struct istat_entry *entp = (struct istat_entry *)left;
	struct stats *statp = (struct stats *)right;
	FREE(entp);
	stats_delete(statp);
}

/*
 * Callback used in a walk of the Istats to reset matching stat counters.
 */
static void
istat_counter_reset_cb(struct istat_entry *entp, struct stats *statp,
    const struct ipath *ipp)
{
	char *path;

	if (entp->ipath == ipp) {
		path = ipath2str(entp->ename, ipp);
		out(O_ALTFP, "istat_counter_reset_cb: resetting %s", path);
		FREE(path);
		stats_counter_reset(statp);
		Istat_need_save = 1;
	}
}

/*ARGSUSED*/
static void
istat_counter_topo_chg_cb(struct istat_entry *entp, struct stats *statp,
    void *unused)
{
	char *path;
	nvlist_t *fmri;

	fmri = ipath2fmri((struct ipath *)(entp->ipath));
	if (!platform_path_exists(fmri)) {
		path = ipath2str(entp->ename, entp->ipath);
		out(O_ALTFP, "istat_counter_topo_chg_cb: not present %s", path);
		FREE(path);
		stats_counter_reset(statp);
		Istat_need_save = 1;
	}
	nvlist_free(fmri);
}

void
istat_fini(void)
{
	lut_free(Istats, istat_destructor, NULL);
}

static char *Serdbuf;
static char *Serdbufptr;
static int Serdsz;

/*
 * serdaddsize -- calculate size of serd and add it to Serdsz
 */
/*ARGSUSED*/
static void
serdaddsize(const struct serd_entry *lhs, struct stats *rhs, void *arg)
{
	ASSERT(lhs != NULL);

	/* count up the size of the stat name */
	Serdsz += ipath2strlen(lhs->ename, lhs->ipath);
	Serdsz++;	/* for the trailing NULL byte */
}

/*
 * serd2str -- serialize a serd engine, writing result to *Serdbufptr
 */
/*ARGSUSED*/
static void
serd2str(const struct serd_entry *lhs, struct stats *rhs, void *arg)
{
	char *str;
	int len;

	ASSERT(lhs != NULL);

	/* serialize the serd engine name */
	str = ipath2str(lhs->ename, lhs->ipath);
	len = strlen(str);

	ASSERT(Serdbufptr + len + 1 <= &Serdbuf[Serdsz]);
	(void) strlcpy(Serdbufptr, str, &Serdbuf[Serdsz] - Serdbufptr);
	Serdbufptr += len;
	FREE(str);
	*Serdbufptr++ = '\0';
	ASSERT(Serdbufptr <= &Serdbuf[Serdsz]);
}

void
serd_save()
{
	if (Serd_need_save == 0)
		return;

	/* figure out how big the serialzed info is */
	Serdsz = 0;
	lut_walk(SerdEngines, (lut_cb)serdaddsize, NULL);

	if (Serdsz == 0) {
		/* no serd engines to save */
		fmd_buf_destroy(Hdl, NULL, WOBUF_SERDS);
		return;
	}

	/* create the serialized buffer */
	Serdbufptr = Serdbuf = MALLOC(Serdsz);
	lut_walk(SerdEngines, (lut_cb)serd2str, NULL);

	/* clear out current saved stats */
	fmd_buf_destroy(Hdl, NULL, WOBUF_SERDS);

	/* write out the new version */
	fmd_buf_write(Hdl, NULL, WOBUF_SERDS, Serdbuf, Serdsz);
	FREE(Serdbuf);
	Serd_need_save = 0;
}

int
serd_cmp(struct serd_entry *ent1, struct serd_entry *ent2)
{
	if (ent1->ename != ent2->ename)
		return (ent2->ename - ent1->ename);
	if (ent1->ipath != ent2->ipath)
		return ((char *)ent2->ipath - (char *)ent1->ipath);

	return (0);
}

void
fme_serd_load(fmd_hdl_t *hdl)
{
	int sz;
	char *sbuf;
	char *sepptr;
	char *ptr;
	struct serd_entry *newentp;
	struct node *epname;
	nvlist_t *fmri;
	char *namestring;

	if ((sz = fmd_buf_size(hdl, NULL, WOBUF_SERDS)) == 0)
		return;
	sbuf = alloca(sz);
	fmd_buf_read(hdl, NULL, WOBUF_SERDS, sbuf, sz);
	ptr = sbuf;
	while (ptr < &sbuf[sz]) {
		sepptr = strchr(ptr, '@');
		*sepptr = '\0';
		namestring = ptr;
		sepptr++;
		ptr = sepptr;
		ptr += strlen(ptr);
		ptr++;	/* move past the '\0' separating paths */
		epname = pathstring2epnamenp(sepptr);
		fmri = node2fmri(epname);
		if (platform_path_exists(fmri)) {
			newentp = MALLOC(sizeof (*newentp));
			newentp->hdl = hdl;
			newentp->ipath = ipath(epname);
			newentp->ename = stable(namestring);
			SerdEngines = lut_add(SerdEngines, (void *)newentp,
			    (void *)newentp, (lut_cmp)serd_cmp);
		} else
			Serd_need_save = 1;
		tree_free(epname);
		nvlist_free(fmri);
	}
	/* save it back again in case some of the paths no longer exist */
	serd_save();
}

/*ARGSUSED*/
static void
serd_destructor(void *left, void *right, void *arg)
{
	struct serd_entry *entp = (struct serd_entry *)left;
	FREE(entp);
}

/*
 * Callback used in a walk of the SerdEngines to reset matching serd engines.
 */
/*ARGSUSED*/
static void
serd_reset_cb(struct serd_entry *entp, void *unused, const struct ipath *ipp)
{
	char *path;

	if (entp->ipath == ipp) {
		path = ipath2str(entp->ename, ipp);
		out(O_ALTFP, "serd_reset_cb: resetting %s", path);
		fmd_serd_reset(entp->hdl, path);
		FREE(path);
		Serd_need_save = 1;
	}
}

/*ARGSUSED*/
static void
serd_topo_chg_cb(struct serd_entry *entp, void *unused, void *unused2)
{
	char *path;
	nvlist_t *fmri;

	fmri = ipath2fmri((struct ipath *)(entp->ipath));
	if (!platform_path_exists(fmri)) {
		path = ipath2str(entp->ename, entp->ipath);
		out(O_ALTFP, "serd_topo_chg_cb: not present %s", path);
		fmd_serd_reset(entp->hdl, path);
		FREE(path);
		Serd_need_save = 1;
	}
	nvlist_free(fmri);
}

void
serd_fini(void)
{
	lut_free(SerdEngines, serd_destructor, NULL);
}

static void
publish_suspects(struct fme *fmep)
{
	struct rsl *srl = NULL;
	struct rsl *erl;
	struct rsl *rp;
	nvlist_t *fault;
	uint8_t cert;
	uint_t *frs;
	uint_t fravg, frsum, fr;
	uint_t messval;
	struct node *snp;
	int frcnt, fridx;
	boolean_t no_upsets = B_FALSE;
	boolean_t allfaulty = B_TRUE;

	stats_counter_bump(fmep->diags);

	/*
	 * If we're auto-closing upsets, we don't want to include them
	 * in any produced suspect lists or certainty accounting.
	 */
	if (Autoclose != NULL)
		if (strcmp(Autoclose, "true") == 0 ||
		    strcmp(Autoclose, "all") == 0 ||
		    strcmp(Autoclose, "upsets") == 0)
			no_upsets = B_TRUE;

	trim_suspects(fmep, no_upsets, &srl, &erl);

	/*
	 * If the resulting suspect list has no members, we're
	 * done.  Returning here will simply close the case.
	 */
	if (fmep->nsuspects == 0) {
		out(O_ALTFP,
		    "[FME%d, case %s (all suspects are upsets)]",
		    fmep->id, fmd_case_uuid(fmep->hdl, fmep->fmcase));
		FREE(srl);
		restore_suspects(fmep);
		return;
	}

	/*
	 * If the suspect list is all faults, then for a given fault,
	 * say X of N, X's certainty is computed via:
	 *
	 * fitrate(X) / (fitrate(1) + ... + fitrate(N)) * 100
	 *
	 * If none of the suspects are faults, and there are N suspects,
	 * the certainty of a given suspect is 100/N.
	 *
	 * If there are are a mixture of faults and other problems in
	 * the suspect list, we take an average of the faults'
	 * FITrates and treat this average as the FITrate for any
	 * non-faults.  The fitrate of any given suspect is then
	 * computed per the first formula above.
	 */
	if (fmep->nonfault == fmep->nsuspects) {
		/* NO faults in the suspect list */
		cert = percentof(1, fmep->nsuspects);
	} else {
		/* sum the fitrates */
		frs = alloca(fmep->nsuspects * sizeof (uint_t));
		fridx = frcnt = frsum = 0;

		for (rp = srl; rp <= erl; rp++) {
			struct node *n;

			if (rp->suspect == NULL)
				continue;
			if (!is_fault(rp->suspect->t)) {
				frs[fridx++] = 0;
				continue;
			}
			n = eventprop_lookup(rp->suspect, L_FITrate);
			if (node2uint(n, &fr) != 0) {
				out(O_DEBUG|O_NONL, "event ");
				ipath_print(O_DEBUG|O_NONL,
				    rp->suspect->enode->u.event.ename->u.name.s,
				    rp->suspect->ipp);
				out(O_DEBUG, " has no FITrate (using 1)");
				fr = 1;
			} else if (fr == 0) {
				out(O_DEBUG|O_NONL, "event ");
				ipath_print(O_DEBUG|O_NONL,
				    rp->suspect->enode->u.event.ename->u.name.s,
				    rp->suspect->ipp);
				out(O_DEBUG, " has zero FITrate (using 1)");
				fr = 1;
			}

			frs[fridx++] = fr;
			frsum += fr;
			frcnt++;
		}
		fravg = avg(frsum, frcnt);
		for (fridx = 0; fridx < fmep->nsuspects; fridx++)
			if (frs[fridx] == 0) {
				frs[fridx] = fravg;
				frsum += fravg;
			}
	}

	/* Add them in reverse order of our sort, as fmd reverses order */
	for (rp = erl; rp >= srl; rp--) {
		if (rp->suspect == NULL)
			continue;
		if (!is_fault(rp->suspect->t))
			allfaulty = B_FALSE;
		if (fmep->nonfault != fmep->nsuspects)
			cert = percentof(frs[--fridx], frsum);
		fault = fmd_nvl_create_fault(fmep->hdl,
		    rp->suspect->enode->u.event.ename->u.name.s,
		    cert,
		    rp->asru,
		    rp->fru,
		    rp->rsrc);
		if (fault == NULL)
			out(O_DIE, "fault creation failed");
		/* if "message" property exists, add it to the fault */
		if (node2uint(eventprop_lookup(rp->suspect, L_message),
		    &messval) == 0) {

			out(O_ALTFP,
			    "[FME%d, %s adds message=%d to suspect list]",
			    fmep->id,
			    rp->suspect->enode->u.event.ename->u.name.s,
			    messval);
			if (nvlist_add_boolean_value(fault,
			    FM_SUSPECT_MESSAGE,
			    (messval) ? B_TRUE : B_FALSE) != 0) {
				out(O_DIE, "cannot add no-message to fault");
			}
		}
		/* add any payload properties */
		lut_walk(rp->suspect->payloadprops,
		    (lut_cb)addpayloadprop, (void *)fault);
		fmd_case_add_suspect(fmep->hdl, fmep->fmcase, fault);
		rslfree(rp);

		/*
		 * If "action" property exists, evaluate it;  this must be done
		 * before the dupclose check below since some actions may
		 * modify the asru to be used in fmd_nvl_fmri_faulty.  This
		 * needs to be restructured if any new actions are introduced
		 * that have effects that we do not want to be visible if
		 * we decide not to publish in the dupclose check below.
		 */
		if ((snp = eventprop_lookup(rp->suspect, L_action)) != NULL) {
			struct evalue evalue;

			out(O_ALTFP|O_NONL,
			    "[FME%d, %s action ", fmep->id,
			    rp->suspect->enode->u.event.ename->u.name.s);
			ptree_name_iter(O_ALTFP|O_NONL, snp);
			out(O_ALTFP, "]");
			Action_nvl = fault;
			(void) eval_expr(snp, NULL, NULL, NULL, NULL,
			    NULL, 0, &evalue);
		}

		/*
		 * check if the asru is already marked as "faulty".
		 */
		if (allfaulty) {
			nvlist_t *asru;

			out(O_ALTFP|O_VERB, "FMD%d dup check ", fmep->id);
			itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, rp->suspect);
			out(O_ALTFP|O_VERB|O_NONL, " ");
			if (nvlist_lookup_nvlist(fault,
			    FM_FAULT_ASRU, &asru) != 0) {
				out(O_ALTFP|O_VERB, "NULL asru");
				allfaulty = B_FALSE;
			} else if (fmd_nvl_fmri_faulty(fmep->hdl, asru)) {
				out(O_ALTFP|O_VERB, "faulty");
			} else {
				out(O_ALTFP|O_VERB, "not faulty");
				allfaulty = B_FALSE;
			}
		}

	}

	/*
	 * We are going to publish so take any pre-publication actions.
	 */
	if (!allfaulty) {
		/*
		 * don't update the count stat if all asrus are already
		 * present and unrepaired in the asru cache
		 */
		for (rp = erl; rp >= srl; rp--) {
			struct event *suspect = rp->suspect;

			if (suspect == NULL)
				continue;

			/* if "count" exists, increment the appropriate stat */
			if ((snp = eventprop_lookup(suspect,
			    L_count)) != NULL) {
				out(O_ALTFP|O_NONL,
				    "[FME%d, %s count ", fmep->id,
				    suspect->enode->u.event.ename->u.name.s);
				ptree_name_iter(O_ALTFP|O_NONL, snp);
				out(O_ALTFP, "]");
				istat_bump(snp, 0);

			}
		}
		istat_save();	/* write out any istat changes */
	}

	out(O_ALTFP, "[solving FME%d, case %s]", fmep->id,
	    fmd_case_uuid(fmep->hdl, fmep->fmcase));
	fmd_case_solve(fmep->hdl, fmep->fmcase);

	/*
	 * revert to the original suspect list
	 */
	FREE(srl);
	restore_suspects(fmep);
}

static void
publish_undiagnosable(fmd_hdl_t *hdl, fmd_event_t *ffep, fmd_case_t *fmcase)
{
	struct case_list *newcase;
	nvlist_t *defect;

	out(O_ALTFP,
	    "[undiagnosable ereport received, "
	    "creating and closing a new case (%s)]",
	    Undiag_reason ? Undiag_reason : "reason not provided");

	newcase = MALLOC(sizeof (struct case_list));
	newcase->next = NULL;
	newcase->fmcase = fmcase;
	if (Undiagablecaselist != NULL)
		newcase->next = Undiagablecaselist;
	Undiagablecaselist = newcase;

	if (ffep != NULL)
		fmd_case_add_ereport(hdl, newcase->fmcase, ffep);

	defect = fmd_nvl_create_fault(hdl, UNDIAGNOSABLE_DEFECT, 100,
	    NULL, NULL, NULL);
	if (Undiag_reason != NULL)
		(void) nvlist_add_string(defect, UNDIAG_REASON, Undiag_reason);
	fmd_case_add_suspect(hdl, newcase->fmcase, defect);

	fmd_case_solve(hdl, newcase->fmcase);
	fmd_case_close(hdl, newcase->fmcase);
}

static void
fme_undiagnosable(struct fme *f)
{
	nvlist_t *defect;

	out(O_ALTFP, "[solving/closing FME%d, case %s (%s)]",
	    f->id, fmd_case_uuid(f->hdl, f->fmcase),
	    Undiag_reason ? Undiag_reason : "undiagnosable");

	defect = fmd_nvl_create_fault(f->hdl, UNDIAGNOSABLE_DEFECT, 100,
	    NULL, NULL, NULL);
	if (Undiag_reason != NULL)
		(void) nvlist_add_string(defect, UNDIAG_REASON, Undiag_reason);
	fmd_case_add_suspect(f->hdl, f->fmcase, defect);
	fmd_case_solve(f->hdl, f->fmcase);
	fmd_case_close(f->hdl, f->fmcase);
}

/*
 * fme_close_case
 *
 *	Find the requested case amongst our fmes and close it.  Free up
 *	the related fme.
 */
void
fme_close_case(fmd_hdl_t *hdl, fmd_case_t *fmcase)
{
	struct case_list *ucasep, *prevcasep = NULL;
	struct fme *prev = NULL;
	struct fme *fmep;

	for (ucasep = Undiagablecaselist; ucasep; ucasep = ucasep->next) {
		if (fmcase != ucasep->fmcase) {
			prevcasep = ucasep;
			continue;
		}

		if (prevcasep == NULL)
			Undiagablecaselist = Undiagablecaselist->next;
		else
			prevcasep->next = ucasep->next;

		FREE(ucasep);
		return;
	}

	for (fmep = FMElist; fmep; fmep = fmep->next) {
		if (fmep->hdl == hdl && fmep->fmcase == fmcase)
			break;
		prev = fmep;
	}

	if (fmep == NULL) {
		out(O_WARN, "Eft asked to close unrecognized case [%s].",
		    fmd_case_uuid(hdl, fmcase));
		return;
	}

	if (EFMElist == fmep)
		EFMElist = prev;

	if (prev == NULL)
		FMElist = FMElist->next;
	else
		prev->next = fmep->next;

	fmep->next = NULL;

	/* Get rid of any timer this fme has set */
	if (fmep->wull != 0)
		fmd_timer_remove(fmep->hdl, fmep->timer);

	if (ClosedFMEs == NULL) {
		ClosedFMEs = fmep;
	} else {
		fmep->next = ClosedFMEs;
		ClosedFMEs = fmep;
	}

	Open_fme_count--;

	/* See if we can close the overflow FME */
	if (Open_fme_count <= Max_fme) {
		for (fmep = FMElist; fmep; fmep = fmep->next) {
			if (fmep->overflow && !(fmd_case_closed(fmep->hdl,
			    fmep->fmcase)))
				break;
		}

		if (fmep != NULL)
			fmd_case_close(fmep->hdl, fmep->fmcase);
	}
}

/*
 * fme_set_timer()
 *	If the time we need to wait for the given FME is less than the
 *	current timer, kick that old timer out and establish a new one.
 */
static int
fme_set_timer(struct fme *fmep, unsigned long long wull)
{
	out(O_ALTFP|O_VERB|O_NONL, " fme_set_timer: request to wait ");
	ptree_timeval(O_ALTFP|O_VERB, &wull);

	if (wull <= fmep->pull) {
		out(O_ALTFP|O_VERB|O_NONL, "already have waited at least ");
		ptree_timeval(O_ALTFP|O_VERB, &fmep->pull);
		out(O_ALTFP|O_VERB, NULL);
		/* we've waited at least wull already, don't need timer */
		return (0);
	}

	out(O_ALTFP|O_VERB|O_NONL, " currently ");
	if (fmep->wull != 0) {
		out(O_ALTFP|O_VERB|O_NONL, "waiting ");
		ptree_timeval(O_ALTFP|O_VERB, &fmep->wull);
		out(O_ALTFP|O_VERB, NULL);
	} else {
		out(O_ALTFP|O_VERB|O_NONL, "not waiting");
		out(O_ALTFP|O_VERB, NULL);
	}

	if (fmep->wull != 0)
		if (wull >= fmep->wull)
			/* New timer would fire later than established timer */
			return (0);

	if (fmep->wull != 0) {
		fmd_timer_remove(fmep->hdl, fmep->timer);
	}

	fmep->timer = fmd_timer_install(fmep->hdl, (void *)fmep,
	    fmep->e0r, wull);
	out(O_ALTFP|O_VERB, "timer set, id is %ld", fmep->timer);
	fmep->wull = wull;
	return (1);
}

void
fme_timer_fired(struct fme *fmep, id_t tid)
{
	struct fme *ffmep = NULL;

	for (ffmep = FMElist; ffmep; ffmep = ffmep->next)
		if (ffmep == fmep)
			break;

	if (ffmep == NULL) {
		out(O_WARN, "Timer fired for an FME (%p) not in FMEs list.",
		    (void *)fmep);
		return;
	}

	out(O_ALTFP|O_VERB, "Timer fired %lx", tid);
	fmep->pull = fmep->wull;
	fmep->wull = 0;
	fmd_buf_write(fmep->hdl, fmep->fmcase,
	    WOBUF_PULL, (void *)&fmep->pull, sizeof (fmep->pull));

	fme_eval(fmep, fmep->e0r);
}

/*
 * Preserve the fme's suspect list in its psuspects list, NULLing the
 * suspects list in the meantime.
 */
static void
save_suspects(struct fme *fmep)
{
	struct event *ep;
	struct event *nextep;

	/* zero out the previous suspect list */
	for (ep = fmep->psuspects; ep; ep = nextep) {
		nextep = ep->psuspects;
		ep->psuspects = NULL;
	}
	fmep->psuspects = NULL;

	/* zero out the suspect list, copying it to previous suspect list */
	fmep->psuspects = fmep->suspects;
	for (ep = fmep->suspects; ep; ep = nextep) {
		nextep = ep->suspects;
		ep->psuspects = ep->suspects;
		ep->suspects = NULL;
		ep->is_suspect = 0;
	}
	fmep->suspects = NULL;
	fmep->nsuspects = 0;
	fmep->nonfault = 0;
}

/*
 * Retrieve the fme's suspect list from its psuspects list.
 */
static void
restore_suspects(struct fme *fmep)
{
	struct event *ep;
	struct event *nextep;

	fmep->nsuspects = fmep->nonfault = 0;
	fmep->suspects = fmep->psuspects;
	for (ep = fmep->psuspects; ep; ep = nextep) {
		fmep->nsuspects++;
		if (!is_fault(ep->t))
			fmep->nonfault++;
		nextep = ep->psuspects;
		ep->suspects = ep->psuspects;
	}
}

/*
 * this is what we use to call the Emrys prototype code instead of main()
 */
static void
fme_eval(struct fme *fmep, fmd_event_t *ffep)
{
	struct event *ep;
	unsigned long long my_delay = TIMEVAL_EVENTUALLY;

	save_suspects(fmep);

	out(O_ALTFP, "Evaluate FME %d", fmep->id);
	indent_set("  ");

	lut_walk(fmep->eventtree, (lut_cb)clear_arrows, (void *)fmep);
	fmep->state = hypothesise(fmep, fmep->e0, fmep->ull, &my_delay);

	out(O_ALTFP|O_NONL, "FME%d state: %s, suspect list:", fmep->id,
	    fme_state2str(fmep->state));
	for (ep = fmep->suspects; ep; ep = ep->suspects) {
		out(O_ALTFP|O_NONL, " ");
		itree_pevent_brief(O_ALTFP|O_NONL, ep);
	}
	out(O_ALTFP, NULL);

	switch (fmep->state) {
	case FME_CREDIBLE:
		print_suspects(SLNEW, fmep);
		(void) upsets_eval(fmep, ffep);

		/*
		 * we may have already posted suspects in upsets_eval() which
		 * can recurse into fme_eval() again. If so then just return.
		 */
		if (fmep->posted_suspects)
			return;

		publish_suspects(fmep);
		fmep->posted_suspects = 1;
		fmd_buf_write(fmep->hdl, fmep->fmcase,
		    WOBUF_POSTD,
		    (void *)&fmep->posted_suspects,
		    sizeof (fmep->posted_suspects));

		/*
		 * Now the suspects have been posted, we can clear up
		 * the instance tree as we won't be looking at it again.
		 * Also cancel the timer as the case is now solved.
		 */
		if (fmep->wull != 0) {
			fmd_timer_remove(fmep->hdl, fmep->timer);
			fmep->wull = 0;
		}
		break;

	case FME_WAIT:
		ASSERT(my_delay > fmep->ull);
		(void) fme_set_timer(fmep, my_delay);
		print_suspects(SLWAIT, fmep);
		itree_prune(fmep->eventtree);
		return;

	case FME_DISPROVED:
		print_suspects(SLDISPROVED, fmep);
		Undiag_reason = UD_UNSOLVD;
		fme_undiagnosable(fmep);
		break;
	}

	if (fmep->posted_suspects == 1 && Autoclose != NULL) {
		int doclose = 0;

		if (strcmp(Autoclose, "true") == 0 ||
		    strcmp(Autoclose, "all") == 0)
			doclose = 1;

		if (strcmp(Autoclose, "upsets") == 0) {
			doclose = 1;
			for (ep = fmep->suspects; ep; ep = ep->suspects) {
				if (ep->t != N_UPSET) {
					doclose = 0;
					break;
				}
			}
		}

		if (doclose) {
			out(O_ALTFP, "[closing FME%d, case %s (autoclose)]",
			    fmep->id, fmd_case_uuid(fmep->hdl, fmep->fmcase));
			fmd_case_close(fmep->hdl, fmep->fmcase);
		}
	}
	itree_free(fmep->eventtree);
	fmep->eventtree = NULL;
	structconfig_free(fmep->config);
	fmep->config = NULL;
	destroy_fme_bufs(fmep);
}

static void indent(void);
static int triggered(struct fme *fmep, struct event *ep, int mark);
static enum fme_state effects_test(struct fme *fmep,
    struct event *fault_event, unsigned long long at_latest_by,
    unsigned long long *pdelay);
static enum fme_state requirements_test(struct fme *fmep, struct event *ep,
    unsigned long long at_latest_by, unsigned long long *pdelay);
static enum fme_state causes_test(struct fme *fmep, struct event *ep,
    unsigned long long at_latest_by, unsigned long long *pdelay);

static int
checkconstraints(struct fme *fmep, struct arrow *arrowp)
{
	struct constraintlist *ctp;
	struct evalue value;
	char *sep = "";

	if (arrowp->forever_false) {
		indent();
		out(O_ALTFP|O_VERB|O_NONL, "  Forever false constraint: ");
		for (ctp = arrowp->constraints; ctp != NULL; ctp = ctp->next) {
			out(O_ALTFP|O_VERB|O_NONL, sep);
			ptree(O_ALTFP|O_VERB|O_NONL, ctp->cnode, 1, 0);
			sep = ", ";
		}
		out(O_ALTFP|O_VERB, NULL);
		return (0);
	}
	if (arrowp->forever_true) {
		indent();
		out(O_ALTFP|O_VERB|O_NONL, "  Forever true constraint: ");
		for (ctp = arrowp->constraints; ctp != NULL; ctp = ctp->next) {
			out(O_ALTFP|O_VERB|O_NONL, sep);
			ptree(O_ALTFP|O_VERB|O_NONL, ctp->cnode, 1, 0);
			sep = ", ";
		}
		out(O_ALTFP|O_VERB, NULL);
		return (1);
	}

	for (ctp = arrowp->constraints; ctp != NULL; ctp = ctp->next) {
		if (eval_expr(ctp->cnode, NULL, NULL,
		    &fmep->globals, fmep->config,
		    arrowp, 0, &value)) {
			/* evaluation successful */
			if (value.t == UNDEFINED || value.v == 0) {
				/* known false */
				arrowp->forever_false = 1;
				indent();
				out(O_ALTFP|O_VERB|O_NONL,
				    "  False constraint: ");
				ptree(O_ALTFP|O_VERB|O_NONL, ctp->cnode, 1, 0);
				out(O_ALTFP|O_VERB, NULL);
				return (0);
			}
		} else {
			/* evaluation unsuccessful -- unknown value */
			indent();
			out(O_ALTFP|O_VERB|O_NONL,
			    "  Deferred constraint: ");
			ptree(O_ALTFP|O_VERB|O_NONL, ctp->cnode, 1, 0);
			out(O_ALTFP|O_VERB, NULL);
			return (1);
		}
	}
	/* known true */
	arrowp->forever_true = 1;
	indent();
	out(O_ALTFP|O_VERB|O_NONL, "  True constraint: ");
	for (ctp = arrowp->constraints; ctp != NULL; ctp = ctp->next) {
		out(O_ALTFP|O_VERB|O_NONL, sep);
		ptree(O_ALTFP|O_VERB|O_NONL, ctp->cnode, 1, 0);
		sep = ", ";
	}
	out(O_ALTFP|O_VERB, NULL);
	return (1);
}

static int
triggered(struct fme *fmep, struct event *ep, int mark)
{
	struct bubble *bp;
	struct arrowlist *ap;
	int count = 0;

	stats_counter_bump(fmep->Tcallcount);
	for (bp = itree_next_bubble(ep, NULL); bp;
	    bp = itree_next_bubble(ep, bp)) {
		if (bp->t != B_TO)
			continue;
		for (ap = itree_next_arrow(bp, NULL); ap;
		    ap = itree_next_arrow(bp, ap)) {
			/* check count of marks against K in the bubble */
			if ((ap->arrowp->mark & mark) &&
			    ++count >= bp->nork)
				return (1);
		}
	}
	return (0);
}

static int
mark_arrows(struct fme *fmep, struct event *ep, int mark,
    unsigned long long at_latest_by, unsigned long long *pdelay, int keep)
{
	struct bubble *bp;
	struct arrowlist *ap;
	unsigned long long overall_delay = TIMEVAL_EVENTUALLY;
	unsigned long long my_delay;
	enum fme_state result;
	int retval = 0;

	for (bp = itree_next_bubble(ep, NULL); bp;
	    bp = itree_next_bubble(ep, bp)) {
		if (bp->t != B_FROM)
			continue;
		stats_counter_bump(fmep->Marrowcount);
		for (ap = itree_next_arrow(bp, NULL); ap;
		    ap = itree_next_arrow(bp, ap)) {
			struct event *ep2 = ap->arrowp->head->myevent;
			/*
			 * if we're clearing marks, we can avoid doing
			 * all that work evaluating constraints.
			 */
			if (mark == 0) {
				if (ap->arrowp->arrow_marked == 0)
					continue;
				ap->arrowp->arrow_marked = 0;
				ap->arrowp->mark &= ~EFFECTS_COUNTER;
				if (keep && (ep2->cached_state &
				    (WAIT_EFFECT|CREDIBLE_EFFECT|PARENT_WAIT)))
					ep2->keep_in_tree = 1;
				ep2->cached_state &=
				    ~(WAIT_EFFECT|CREDIBLE_EFFECT|PARENT_WAIT);
				(void) mark_arrows(fmep, ep2, mark, 0, NULL,
				    keep);
				continue;
			}
			ap->arrowp->arrow_marked = 1;
			if (ep2->cached_state & REQMNTS_DISPROVED) {
				indent();
				out(O_ALTFP|O_VERB|O_NONL,
				    "  ALREADY DISPROVED ");
				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
				out(O_ALTFP|O_VERB, NULL);
				continue;
			}
			if (ep2->cached_state & WAIT_EFFECT) {
				indent();
				out(O_ALTFP|O_VERB|O_NONL,
				    "  ALREADY EFFECTS WAIT ");
				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
				out(O_ALTFP|O_VERB, NULL);
				continue;
			}
			if (ep2->cached_state & CREDIBLE_EFFECT) {
				indent();
				out(O_ALTFP|O_VERB|O_NONL,
				    "  ALREADY EFFECTS CREDIBLE ");
				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
				out(O_ALTFP|O_VERB, NULL);
				continue;
			}
			if ((ep2->cached_state & PARENT_WAIT) &&
			    (mark & PARENT_WAIT)) {
				indent();
				out(O_ALTFP|O_VERB|O_NONL,
				    "  ALREADY PARENT EFFECTS WAIT ");
				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
				out(O_ALTFP|O_VERB, NULL);
				continue;
			}
			platform_set_payloadnvp(ep2->nvp);
			if (checkconstraints(fmep, ap->arrowp) == 0) {
				platform_set_payloadnvp(NULL);
				indent();
				out(O_ALTFP|O_VERB|O_NONL,
				    "  CONSTRAINTS FAIL ");
				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
				out(O_ALTFP|O_VERB, NULL);
				continue;
			}
			platform_set_payloadnvp(NULL);
			ap->arrowp->mark |= EFFECTS_COUNTER;
			if (!triggered(fmep, ep2, EFFECTS_COUNTER)) {
				indent();
				out(O_ALTFP|O_VERB|O_NONL,
				    "  K-COUNT NOT YET MET ");
				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
				out(O_ALTFP|O_VERB, NULL);
				continue;
			}
			ep2->cached_state &= ~PARENT_WAIT;
			/*
			 * if we've reached an ereport and no propagation time
			 * is specified, use the Hesitate value
			 */
			if (ep2->t == N_EREPORT && at_latest_by == 0ULL &&
			    ap->arrowp->maxdelay == 0ULL) {
				out(O_ALTFP|O_VERB|O_NONL, "  default wait ");
				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
				out(O_ALTFP|O_VERB, NULL);
				result = requirements_test(fmep, ep2, Hesitate,
				    &my_delay);
			} else {
				result = requirements_test(fmep, ep2,
				    at_latest_by + ap->arrowp->maxdelay,
				    &my_delay);
			}
			if (result == FME_WAIT) {
				retval = WAIT_EFFECT;
				if (overall_delay > my_delay)
					overall_delay = my_delay;
				ep2->cached_state |= WAIT_EFFECT;
				indent();
				out(O_ALTFP|O_VERB|O_NONL, "  EFFECTS WAIT ");
				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
				out(O_ALTFP|O_VERB, NULL);
				indent_push("  E");
				if (mark_arrows(fmep, ep2, PARENT_WAIT,
				    at_latest_by, &my_delay, 0) ==
				    WAIT_EFFECT) {
					retval = WAIT_EFFECT;
					if (overall_delay > my_delay)
						overall_delay = my_delay;
				}
				indent_pop();
			} else if (result == FME_DISPROVED) {
				indent();
				out(O_ALTFP|O_VERB|O_NONL,
				    "  EFFECTS DISPROVED ");
				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
				out(O_ALTFP|O_VERB, NULL);
			} else {
				ep2->cached_state |= mark;
				indent();
				if (mark == CREDIBLE_EFFECT)
					out(O_ALTFP|O_VERB|O_NONL,
					    "  EFFECTS CREDIBLE ");
				else
					out(O_ALTFP|O_VERB|O_NONL,
					    "  PARENT EFFECTS WAIT ");
				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep2);
				out(O_ALTFP|O_VERB, NULL);
				indent_push("  E");
				if (mark_arrows(fmep, ep2, mark, at_latest_by,
				    &my_delay, 0) == WAIT_EFFECT) {
					retval = WAIT_EFFECT;
					if (overall_delay > my_delay)
						overall_delay = my_delay;
				}
				indent_pop();
			}
		}
	}
	if (retval == WAIT_EFFECT)
		*pdelay = overall_delay;
	return (retval);
}

static enum fme_state
effects_test(struct fme *fmep, struct event *fault_event,
    unsigned long long at_latest_by, unsigned long long *pdelay)
{
	struct event *error_event;
	enum fme_state return_value = FME_CREDIBLE;
	unsigned long long overall_delay = TIMEVAL_EVENTUALLY;
	unsigned long long my_delay;

	stats_counter_bump(fmep->Ecallcount);
	indent_push("  E");
	indent();
	out(O_ALTFP|O_VERB|O_NONL, "->");
	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, fault_event);
	out(O_ALTFP|O_VERB, NULL);

	if (mark_arrows(fmep, fault_event, CREDIBLE_EFFECT, at_latest_by,
	    &my_delay, 0) == WAIT_EFFECT) {
		return_value = FME_WAIT;
		if (overall_delay > my_delay)
			overall_delay = my_delay;
	}
	for (error_event = fmep->observations;
	    error_event; error_event = error_event->observations) {
		indent();
		out(O_ALTFP|O_VERB|O_NONL, " ");
		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, error_event);
		if (!(error_event->cached_state & CREDIBLE_EFFECT)) {
			if (error_event->cached_state &
			    (PARENT_WAIT|WAIT_EFFECT)) {
				out(O_ALTFP|O_VERB, " NOT YET triggered");
				continue;
			}
			return_value = FME_DISPROVED;
			out(O_ALTFP|O_VERB, " NOT triggered");
			break;
		} else {
			out(O_ALTFP|O_VERB, " triggered");
		}
	}
	if (return_value == FME_DISPROVED) {
		(void) mark_arrows(fmep, fault_event, 0, 0, NULL, 0);
	} else {
		fault_event->keep_in_tree = 1;
		(void) mark_arrows(fmep, fault_event, 0, 0, NULL, 1);
	}

	indent();
	out(O_ALTFP|O_VERB|O_NONL, "<-EFFECTS %s ",
	    fme_state2str(return_value));
	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, fault_event);
	out(O_ALTFP|O_VERB, NULL);
	indent_pop();
	if (return_value == FME_WAIT)
		*pdelay = overall_delay;
	return (return_value);
}

static enum fme_state
requirements_test(struct fme *fmep, struct event *ep,
    unsigned long long at_latest_by, unsigned long long *pdelay)
{
	int waiting_events;
	int credible_events;
	int deferred_events;
	enum fme_state return_value = FME_CREDIBLE;
	unsigned long long overall_delay = TIMEVAL_EVENTUALLY;
	unsigned long long arrow_delay;
	unsigned long long my_delay;
	struct event *ep2;
	struct bubble *bp;
	struct arrowlist *ap;

	if (ep->cached_state & REQMNTS_CREDIBLE) {
		indent();
		out(O_ALTFP|O_VERB|O_NONL, "  REQMNTS ALREADY CREDIBLE ");
		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
		out(O_ALTFP|O_VERB, NULL);
		return (FME_CREDIBLE);
	}
	if (ep->cached_state & REQMNTS_DISPROVED) {
		indent();
		out(O_ALTFP|O_VERB|O_NONL, "  REQMNTS ALREADY DISPROVED ");
		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
		out(O_ALTFP|O_VERB, NULL);
		return (FME_DISPROVED);
	}
	if (ep->cached_state & REQMNTS_WAIT) {
		indent();
		*pdelay = ep->cached_delay;
		out(O_ALTFP|O_VERB|O_NONL, "  REQMNTS ALREADY WAIT ");
		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
		out(O_ALTFP|O_VERB|O_NONL, ", wait for: ");
		ptree_timeval(O_ALTFP|O_VERB|O_NONL, &at_latest_by);
		out(O_ALTFP|O_VERB, NULL);
		return (FME_WAIT);
	}
	stats_counter_bump(fmep->Rcallcount);
	indent_push("  R");
	indent();
	out(O_ALTFP|O_VERB|O_NONL, "->");
	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
	out(O_ALTFP|O_VERB|O_NONL, ", at latest by: ");
	ptree_timeval(O_ALTFP|O_VERB|O_NONL, &at_latest_by);
	out(O_ALTFP|O_VERB, NULL);

	if (ep->t == N_EREPORT) {
		if (ep->count == 0) {
			if (fmep->pull >= at_latest_by) {
				return_value = FME_DISPROVED;
			} else {
				ep->cached_delay = *pdelay = at_latest_by;
				return_value = FME_WAIT;
			}
		}

		indent();
		switch (return_value) {
		case FME_CREDIBLE:
			ep->cached_state |= REQMNTS_CREDIBLE;
			out(O_ALTFP|O_VERB|O_NONL, "<-REQMNTS CREDIBLE ");
			itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
			break;
		case FME_DISPROVED:
			ep->cached_state |= REQMNTS_DISPROVED;
			out(O_ALTFP|O_VERB|O_NONL, "<-REQMNTS DISPROVED ");
			itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
			break;
		case FME_WAIT:
			ep->cached_state |= REQMNTS_WAIT;
			out(O_ALTFP|O_VERB|O_NONL, "<-REQMNTS WAIT ");
			itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
			out(O_ALTFP|O_VERB|O_NONL, " to ");
			ptree_timeval(O_ALTFP|O_VERB|O_NONL, &at_latest_by);
			break;
		default:
			out(O_DIE, "requirements_test: unexpected fme_state");
			break;
		}
		out(O_ALTFP|O_VERB, NULL);
		indent_pop();

		return (return_value);
	}

	/* this event is not a report, descend the tree */
	for (bp = itree_next_bubble(ep, NULL); bp;
	    bp = itree_next_bubble(ep, bp)) {
		int n;

		if (bp->t != B_FROM)
			continue;

		n = bp->nork;

		credible_events = 0;
		waiting_events = 0;
		deferred_events = 0;
		arrow_delay = TIMEVAL_EVENTUALLY;
		/*
		 * n is -1 for 'A' so adjust it.
		 * XXX just count up the arrows for now.
		 */
		if (n < 0) {
			n = 0;
			for (ap = itree_next_arrow(bp, NULL); ap;
			    ap = itree_next_arrow(bp, ap))
				n++;
			indent();
			out(O_ALTFP|O_VERB, " Bubble Counted N=%d", n);
		} else {
			indent();
			out(O_ALTFP|O_VERB, " Bubble N=%d", n);
		}

		if (n == 0)
			continue;
		if (!(bp->mark & (BUBBLE_ELIDED|BUBBLE_OK))) {
			for (ap = itree_next_arrow(bp, NULL); ap;
			    ap = itree_next_arrow(bp, ap)) {
				ep2 = ap->arrowp->head->myevent;
				platform_set_payloadnvp(ep2->nvp);
				if (checkconstraints(fmep, ap->arrowp) == 0) {
					/*
					 * if any arrow is invalidated by the
					 * constraints, then we should elide the
					 * whole bubble to be consistant with
					 * the tree creation time behaviour
					 */
					bp->mark |= BUBBLE_ELIDED;
					platform_set_payloadnvp(NULL);
					break;
				}
				platform_set_payloadnvp(NULL);
			}
		}
		if (bp->mark & BUBBLE_ELIDED)
			continue;
		bp->mark |= BUBBLE_OK;
		for (ap = itree_next_arrow(bp, NULL); ap;
		    ap = itree_next_arrow(bp, ap)) {
			ep2 = ap->arrowp->head->myevent;
			if (n <= credible_events)
				break;

			ap->arrowp->mark |= REQMNTS_COUNTER;
			if (triggered(fmep, ep2, REQMNTS_COUNTER))
				/* XXX adding max timevals! */
				switch (requirements_test(fmep, ep2,
				    at_latest_by + ap->arrowp->maxdelay,
				    &my_delay)) {
				case FME_DEFERRED:
					deferred_events++;
					break;
				case FME_CREDIBLE:
					credible_events++;
					break;
				case FME_DISPROVED:
					break;
				case FME_WAIT:
					if (my_delay < arrow_delay)
						arrow_delay = my_delay;
					waiting_events++;
					break;
				default:
					out(O_DIE,
					"Bug in requirements_test.");
				}
			else
				deferred_events++;
		}
		indent();
		out(O_ALTFP|O_VERB, " Credible: %d Waiting %d",
		    credible_events + deferred_events, waiting_events);
		if (credible_events + deferred_events + waiting_events < n) {
			/* Can never meet requirements */
			ep->cached_state |= REQMNTS_DISPROVED;
			indent();
			out(O_ALTFP|O_VERB|O_NONL, "<-REQMNTS DISPROVED ");
			itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
			out(O_ALTFP|O_VERB, NULL);
			indent_pop();
			return (FME_DISPROVED);
		}
		if (credible_events + deferred_events < n) {
			/* will have to wait */
			/* wait time is shortest known */
			if (arrow_delay < overall_delay)
				overall_delay = arrow_delay;
			return_value = FME_WAIT;
		} else if (credible_events < n) {
			if (return_value != FME_WAIT)
				return_value = FME_DEFERRED;
		}
	}

	/*
	 * don't mark as FME_DEFERRED. If this event isn't reached by another
	 * path, then this will be considered FME_CREDIBLE. But if it is
	 * reached by a different path so the K-count is met, then might
	 * get overridden by FME_WAIT or FME_DISPROVED.
	 */
	if (return_value == FME_WAIT) {
		ep->cached_state |= REQMNTS_WAIT;
		ep->cached_delay = *pdelay = overall_delay;
	} else if (return_value == FME_CREDIBLE) {
		ep->cached_state |= REQMNTS_CREDIBLE;
	}
	indent();
	out(O_ALTFP|O_VERB|O_NONL, "<-REQMNTS %s ",
	    fme_state2str(return_value));
	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
	out(O_ALTFP|O_VERB, NULL);
	indent_pop();
	return (return_value);
}

static enum fme_state
causes_test(struct fme *fmep, struct event *ep,
    unsigned long long at_latest_by, unsigned long long *pdelay)
{
	unsigned long long overall_delay = TIMEVAL_EVENTUALLY;
	unsigned long long my_delay;
	int credible_results = 0;
	int waiting_results = 0;
	enum fme_state fstate;
	struct event *tail_event;
	struct bubble *bp;
	struct arrowlist *ap;
	int k = 1;

	stats_counter_bump(fmep->Ccallcount);
	indent_push("  C");
	indent();
	out(O_ALTFP|O_VERB|O_NONL, "->");
	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
	out(O_ALTFP|O_VERB, NULL);

	for (bp = itree_next_bubble(ep, NULL); bp;
	    bp = itree_next_bubble(ep, bp)) {
		if (bp->t != B_TO)
			continue;
		k = bp->nork;	/* remember the K value */
		for (ap = itree_next_arrow(bp, NULL); ap;
		    ap = itree_next_arrow(bp, ap)) {
			int do_not_follow = 0;

			/*
			 * if we get to the same event multiple times
			 * only worry about the first one.
			 */
			if (ap->arrowp->tail->myevent->cached_state &
			    CAUSES_TESTED) {
				indent();
				out(O_ALTFP|O_VERB|O_NONL,
				    "  causes test already run for ");
				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL,
				    ap->arrowp->tail->myevent);
				out(O_ALTFP|O_VERB, NULL);
				continue;
			}

			/*
			 * see if false constraint prevents us
			 * from traversing this arrow
			 */
			platform_set_payloadnvp(ep->nvp);
			if (checkconstraints(fmep, ap->arrowp) == 0)
				do_not_follow = 1;
			platform_set_payloadnvp(NULL);
			if (do_not_follow) {
				indent();
				out(O_ALTFP|O_VERB|O_NONL,
				    "  False arrow from ");
				itree_pevent_brief(O_ALTFP|O_VERB|O_NONL,
				    ap->arrowp->tail->myevent);
				out(O_ALTFP|O_VERB, NULL);
				continue;
			}

			ap->arrowp->tail->myevent->cached_state |=
			    CAUSES_TESTED;
			tail_event = ap->arrowp->tail->myevent;
			fstate = hypothesise(fmep, tail_event, at_latest_by,
			    &my_delay);

			switch (fstate) {
			case FME_WAIT:
				if (my_delay < overall_delay)
					overall_delay = my_delay;
				waiting_results++;
				break;
			case FME_CREDIBLE:
				credible_results++;
				break;
			case FME_DISPROVED:
				break;
			default:
				out(O_DIE, "Bug in causes_test");
			}
		}
	}
	/* compare against K */
	if (credible_results + waiting_results < k) {
		indent();
		out(O_ALTFP|O_VERB|O_NONL, "<-CAUSES DISPROVED ");
		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
		out(O_ALTFP|O_VERB, NULL);
		indent_pop();
		return (FME_DISPROVED);
	}
	if (waiting_results != 0) {
		*pdelay = overall_delay;
		indent();
		out(O_ALTFP|O_VERB|O_NONL, "<-CAUSES WAIT ");
		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
		out(O_ALTFP|O_VERB|O_NONL, " to ");
		ptree_timeval(O_ALTFP|O_VERB|O_NONL, &at_latest_by);
		out(O_ALTFP|O_VERB, NULL);
		indent_pop();
		return (FME_WAIT);
	}
	indent();
	out(O_ALTFP|O_VERB|O_NONL, "<-CAUSES CREDIBLE ");
	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
	out(O_ALTFP|O_VERB, NULL);
	indent_pop();
	return (FME_CREDIBLE);
}

static enum fme_state
hypothesise(struct fme *fmep, struct event *ep,
	unsigned long long at_latest_by, unsigned long long *pdelay)
{
	enum fme_state rtr, otr;
	unsigned long long my_delay;
	unsigned long long overall_delay = TIMEVAL_EVENTUALLY;

	stats_counter_bump(fmep->Hcallcount);
	indent_push("  H");
	indent();
	out(O_ALTFP|O_VERB|O_NONL, "->");
	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
	out(O_ALTFP|O_VERB|O_NONL, ", at latest by: ");
	ptree_timeval(O_ALTFP|O_VERB|O_NONL, &at_latest_by);
	out(O_ALTFP|O_VERB, NULL);

	rtr = requirements_test(fmep, ep, at_latest_by, &my_delay);
	if ((rtr == FME_WAIT) && (my_delay < overall_delay))
		overall_delay = my_delay;
	if (rtr != FME_DISPROVED) {
		if (is_problem(ep->t)) {
			otr = effects_test(fmep, ep, at_latest_by, &my_delay);
			if (otr != FME_DISPROVED) {
				if (fmep->peek == 0 && ep->is_suspect == 0) {
					ep->suspects = fmep->suspects;
					ep->is_suspect = 1;
					fmep->suspects = ep;
					fmep->nsuspects++;
					if (!is_fault(ep->t))
						fmep->nonfault++;
				}
			}
		} else
			otr = causes_test(fmep, ep, at_latest_by, &my_delay);
		if ((otr == FME_WAIT) && (my_delay < overall_delay))
			overall_delay = my_delay;
		if ((otr != FME_DISPROVED) &&
		    ((rtr == FME_WAIT) || (otr == FME_WAIT)))
			*pdelay = overall_delay;
	}
	if (rtr == FME_DISPROVED) {
		indent();
		out(O_ALTFP|O_VERB|O_NONL, "<-DISPROVED ");
		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
		out(O_ALTFP|O_VERB, " (doesn't meet requirements)");
		indent_pop();
		return (FME_DISPROVED);
	}
	if ((otr == FME_DISPROVED) && is_problem(ep->t)) {
		indent();
		out(O_ALTFP|O_VERB|O_NONL, "<-DISPROVED ");
		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
		out(O_ALTFP|O_VERB, " (doesn't explain all reports)");
		indent_pop();
		return (FME_DISPROVED);
	}
	if (otr == FME_DISPROVED) {
		indent();
		out(O_ALTFP|O_VERB|O_NONL, "<-DISPROVED ");
		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
		out(O_ALTFP|O_VERB, " (causes are not credible)");
		indent_pop();
		return (FME_DISPROVED);
	}
	if ((rtr == FME_WAIT) || (otr == FME_WAIT)) {
		indent();
		out(O_ALTFP|O_VERB|O_NONL, "<-WAIT ");
		itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
		out(O_ALTFP|O_VERB|O_NONL, " to ");
		ptree_timeval(O_ALTFP|O_VERB|O_NONL, &overall_delay);
		out(O_ALTFP|O_VERB, NULL);
		indent_pop();
		return (FME_WAIT);
	}
	indent();
	out(O_ALTFP|O_VERB|O_NONL, "<-CREDIBLE ");
	itree_pevent_brief(O_ALTFP|O_VERB|O_NONL, ep);
	out(O_ALTFP|O_VERB, NULL);
	indent_pop();
	return (FME_CREDIBLE);
}

/*
 * fme_istat_load -- reconstitute any persistent istats
 */
void
fme_istat_load(fmd_hdl_t *hdl)
{
	int sz;
	char *sbuf;
	char *ptr;

	if ((sz = fmd_buf_size(hdl, NULL, WOBUF_ISTATS)) == 0) {
		out(O_ALTFP, "fme_istat_load: No stats");
		return;
	}

	sbuf = alloca(sz);

	fmd_buf_read(hdl, NULL, WOBUF_ISTATS, sbuf, sz);

	/*
	 * pick apart the serialized stats
	 *
	 * format is:
	 *	<class-name>, '@', <path>, '\0', <value>, '\0'
	 * for example:
	 *	"stat.first@stat0/path0\02\0stat.second@stat0/path1\023\0"
	 *
	 * since this is parsing our own serialized data, any parsing issues
	 * are fatal, so we check for them all with ASSERT() below.
	 */
	ptr = sbuf;
	while (ptr < &sbuf[sz]) {
		char *sepptr;
		struct node *np;
		int val;

		sepptr = strchr(ptr, '@');
		ASSERT(sepptr != NULL);
		*sepptr = '\0';

		/* construct the event */
		np = newnode(T_EVENT, NULL, 0);
		np->u.event.ename = newnode(T_NAME, NULL, 0);
		np->u.event.ename->u.name.t = N_STAT;
		np->u.event.ename->u.name.s = stable(ptr);
		np->u.event.ename->u.name.it = IT_ENAME;
		np->u.event.ename->u.name.last = np->u.event.ename;

		ptr = sepptr + 1;
		ASSERT(ptr < &sbuf[sz]);
		ptr += strlen(ptr);
		ptr++;	/* move past the '\0' separating path from value */
		ASSERT(ptr < &sbuf[sz]);
		ASSERT(isdigit(*ptr));
		val = atoi(ptr);
		ASSERT(val > 0);
		ptr += strlen(ptr);
		ptr++;	/* move past the final '\0' for this entry */

		np->u.event.epname = pathstring2epnamenp(sepptr + 1);
		ASSERT(np->u.event.epname != NULL);

		istat_bump(np, val);
		tree_free(np);
	}

	istat_save();
}