summaryrefslogtreecommitdiffstats
path: root/ldap/servers/slapd/back-ldbm/dblayer.c
blob: 9a9f6af0998f0f4ffd86f6ae0bf264c8a57b43f0 (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
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
/** BEGIN COPYRIGHT BLOCK
 * This Program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; version 2 of the License.
 * 
 * This Program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA.
 * 
 * In addition, as a special exception, Red Hat, Inc. gives You the additional
 * right to link the code of this Program with code not covered under the GNU
 * General Public License ("Non-GPL Code") and to distribute linked combinations
 * including the two, subject to the limitations in this paragraph. Non-GPL Code
 * permitted under this exception must only link to the code of this Program
 * through those well defined interfaces identified in the file named EXCEPTION
 * found in the source code files (the "Approved Interfaces"). The files of
 * Non-GPL Code may instantiate templates or use macros or inline functions from
 * the Approved Interfaces without causing the resulting work to be covered by
 * the GNU General Public License. Only Red Hat, Inc. may make changes or
 * additions to the list of Approved Interfaces. You must obey the GNU General
 * Public License in all respects for all of the Program code and other code used
 * in conjunction with the Program except the Non-GPL Code covered by this
 * exception. If you modify this file, you may extend this exception to your
 * version of the file, but you are not obligated to do so. If you do not wish to
 * provide this exception without modification, you must delete this exception
 * statement from your version and license this file solely under the GPL without
 * exception. 
 * 
 * 
 * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
 * Copyright (C) 2005 Red Hat, Inc.
 * All rights reserved.
 * END COPYRIGHT BLOCK **/

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif


/*
    Abstraction layer which sits between db2.0 and 
    higher layers in the directory server---typically
    the back-end.
    This module's purposes are 1) to hide messy stuff which
    db2.0 needs, and with which we don't want to pollute the back-end
    code. 2) Provide some degree of portability to other databases
    if that becomes a requirement. Note that it is NOT POSSIBLE
    to revert to db1.85 because the backend is now using features
    from db2.0 which db1.85 does not have.
    Also provides an emulation of the ldbm_ functions, for anyone
    who is still calling those. The use of these functions is
    deprecated. Only for backwards-compatibility.
    Blame: dboreham
*/

/* Return code conventions: 
    Unless otherwise advertised, all the functions in this module
    return an int which is zero if the operation was successful
    and non-zero if it wasn't. If the return'ed value was > 0,
    it can be interpreted as a system errno value. If it was < 0,
    its meaning is defined in dblayer.h
*/

/*
    Some information about how this stuff is to be used:
    
    Call dblayer_init() near the beginning of the application's life.
    This allocates some resources and allows the config line processing
    stuff to work.
    Call dblayer_start() when you're sure all config stuff has been seen.
    This needs to be called before you can do anything else.
    Call dblayer_close() when you're finished using the db and want to exit.
    This closes and flushes all files opened by your application since calling
    dblayer_start. If you do NOT call dblayer_close(), we assume that the
    application crashed, and initiate recover next time you call dblayer_start().
    Call dblayer_terminate() after close. This releases resources.

    DB* handles are retrieved from dblayer via these functions:

    dblayer_get_id2entry()
    dblayer_get_index_file()

    the caller must honour the protocol that these handles are released back
    to dblayer when you're done using them, use thse functions to do this:

    dblayer_release_id2entry()
    dblayer_release_index_file()


*/

#include "back-ldbm.h"
#include "dblayer.h"
#include <prrwlock.h>

#if 1000*DB_VERSION_MAJOR + 100*DB_VERSION_MINOR >= 4100
#define DB_OPEN(oflags, db, txnid, file, database, type, flags, mode, rval)    \
{                                                                              \
    if (((oflags) & DB_INIT_TXN) && ((oflags) & DB_INIT_LOG))                  \
    {                                                                          \
        (rval) = ((db)->open)((db), (txnid), (file), (database), (type), (flags)|DB_AUTO_COMMIT, (mode)); \
    }                                                                          \
    else                                                                       \
    {                                                                          \
        (rval) = ((db)->open)((db), (txnid), (file), (database), (type), (flags), (mode)); \
    }                                                                          \
}
/* 608145: db4.1 and newer does not require exclusive lock for checkpointing 
 * and transactions */
#define DB_CHECKPOINT_LOCK(use_lock, lock) ;
#define DB_CHECKPOINT_UNLOCK(use_lock, lock) ;
#else /* older then db 41 */
#define DB_OPEN(oflags, db, txnid, file, database, type, flags, mode, rval)    \
    (rval) = (db)->open((db), (file), (database), (type), (flags), (mode))
#define DB_CHECKPOINT_LOCK(use_lock, lock) if(use_lock) PR_RWLock_Wlock(lock);
#define DB_CHECKPOINT_UNLOCK(use_lock, lock) if(use_lock) PR_RWLock_Unlock(lock);
#endif

#if 1000*DB_VERSION_MAJOR + 100*DB_VERSION_MINOR >= 4000
#define DB_ENV_SET_REGION_INIT(env) (env)->set_flags((env), DB_REGION_INIT, 1)
#define TXN_BEGIN(env, parent_txn, tid, flags) \
    (env)->txn_begin((env), (parent_txn), (tid), (flags))
#define TXN_COMMIT(txn, flags) (txn)->commit((txn), (flags))
#define TXN_ABORT(txn) (txn)->abort(txn)
#define TXN_CHECKPOINT(env, kbyte, min, flags) \
    (env)->txn_checkpoint((env), (kbyte), (min), (flags))
#define MEMP_STAT(env, gsp, fsp, flags, malloc) \
    (env)->memp_stat((env), (gsp), (fsp), (flags))
#define MEMP_TRICKLE(env, pct, nwrotep) \
    (env)->memp_trickle((env), (pct), (nwrotep))
#define LOG_ARCHIVE(env, listp, flags, malloc) \
    (env)->log_archive((env), (listp), (flags))
#define LOG_FLUSH(env, lsn) (env)->log_flush((env), (lsn))
#define LOCK_DETECT(env, flags, atype, aborted) \
    (env)->lock_detect((env), (flags), (atype), (aborted))
#if DB_VERSION_MINOR >= 4 /* i.e. 4.4 or later */
#define DB_ENV_SET_TAS_SPINS(env, tas_spins) \
    (env)->mutex_set_tas_spins((env), (tas_spins))
#else /* < 4.4 */
#define DB_ENV_SET_TAS_SPINS(env, tas_spins) \
    (env)->set_tas_spins((env), (tas_spins))
#endif /* 4.4 or later */
#else    /* older than db 4.0 */
#define DB_ENV_SET_REGION_INIT(env) db_env_set_region_init(1)
#define DB_ENV_SET_TAS_SPINS(env, tas_spins) \
    db_env_set_tas_spins((tas_spins))
#define TXN_BEGIN(env, parent_txn, tid, flags) \
    txn_begin((env), (parent_txn), (tid), (flags))
#define TXN_COMMIT(txn, flags) txn_commit((txn), (flags))
#define TXN_ABORT(txn) txn_abort((txn))
#define TXN_CHECKPOINT(env, kbyte, min, flags) \
    txn_checkpoint((env), (kbyte), (min), (flags))
#define MEMP_TRICKLE(env, pct, nwrotep) memp_trickle((env), (pct), (nwrotep))
#define LOG_FLUSH(env, lsn) log_flush((env), (lsn))
#define LOCK_DETECT(env, flags, atype, aborted) \
    lock_detect((env), (flags), (atype), (aborted))

#if 1000*DB_VERSION_MAJOR + 100*DB_VERSION_MINOR >= 3300
#define MEMP_STAT(env, gsp, fsp, flags, malloc) memp_stat((env), (gsp), (fsp))
#define LOG_ARCHIVE(env, listp, flags, malloc) \
    log_archive((env), (listp), (flags))

#else    /* older than db 3.3 */
#define MEMP_STAT(env, gsp, fsp, flags, malloc) \
    memp_stat((env), (gsp), (fsp), (malloc))
#define LOG_ARCHIVE(env, listp, flags, malloc) \
    log_archive((env), (listp), (flags), (malloc))
#endif
#endif

/* Use these macros to incr/decrement the thread count for the
   database housekeeping threads.  This ensures that the
   value is changed in a thread safe manner, and safely notifies
   the main thread during cleanup. INCR_THREAD_COUNT should be
   the first real statement in the thread function, before any
   actual work is done, other than perhaps variable assignments.
   DECR_THREAD_COUNT should be called as the next to last thing
   in the thread function, just before the trace log message and
   return.
*/
#define INCR_THREAD_COUNT(priv) \
    PR_Lock(priv->thread_count_lock); \
    ++priv->dblayer_thread_count; \
    PR_Unlock(priv->thread_count_lock)

#define DECR_THREAD_COUNT(priv) \
    PR_Lock(priv->thread_count_lock); \
    if (--priv->dblayer_thread_count == 0) { \
        PR_NotifyCondVar(priv->thread_count_cv); \
    } \
    PR_Unlock(priv->thread_count_lock)

#define NEWDIR_MODE 0755


static int perf_threadmain(void *param);
static int checkpoint_threadmain(void *param);
static int trickle_threadmain(void *param);
static int deadlock_threadmain(void *param);
static int commit_good_database(dblayer_private *priv);
static int read_metadata(struct ldbminfo *li);
static int count_dbfiles_in_dir(char *directory, int *count, int recurse);
static int dblayer_override_libdb_functions(DB_ENV *pEnv, dblayer_private *priv);
static int dblayer_force_checkpoint(struct ldbminfo *li);
static int log_flush_threadmain(void *param);
static int dblayer_delete_transaction_logs(const char * log_dir);
static int dblayer_is_logfilename(const char* path);

static int dblayer_start_log_flush_thread(dblayer_private *priv);
static int dblayer_start_deadlock_thread(struct ldbminfo *li);
static int dblayer_start_checkpoint_thread(struct ldbminfo *li);
static int dblayer_start_trickle_thread(struct ldbminfo *li);
static int dblayer_start_perf_thread(struct ldbminfo *li);
static int trans_batch_count=1;
static int trans_batch_limit=0;
static PRBool log_flush_thread=PR_FALSE;
static int dblayer_db_remove_ex(dblayer_private_env *env, char const path[], char const dbName[], PRBool use_lock);

#define MEGABYTE (1024 * 1024)
#define GIGABYTE (1024 * MEGABYTE)


/* This function compares two index keys.  It is assumed
   that the values are already normalized, since they should have
   been when the index was created (by int_values2keys).

   richm - actually, the current syntax compare functions
   always normalize both arguments.  We need to add an additional
   syntax compare function that does not normalize or takes
   an argument like value_cmp to specify to normalize or not.

   More fun - this function is used to compare both raw database
   keys (e.g. with the prefix '=' or '+' or '*' etc.) and without
   (in the case of two equality keys, we want to strip off the
   leading '=' to compare the actual values).  We only use the
   value_compare function if both keys are equality keys with
   some data after the equality prefix.  In every other case,
   we will just use a standard berval cmp function.

   see also DBTcmp
*/
int
dblayer_bt_compare(DB *db, const DBT *dbt1, const DBT *dbt2)
{
    struct berval bv1, bv2;
    value_compare_fn_type syntax_cmp_fn = (value_compare_fn_type)db->app_private;

    if ((dbt1->data && (dbt1->size>1) && (*((char*)dbt1->data) == EQ_PREFIX)) &&
        (dbt2->data && (dbt2->size>1) && (*((char*)dbt2->data) == EQ_PREFIX))) {
        bv1.bv_val = (char *)dbt1->data+1; /* remove leading '=' */
        bv1.bv_len = (ber_len_t)dbt1->size-1;

        bv2.bv_val = (char *)dbt2->data+1; /* remove leading '=' */
        bv2.bv_len = (ber_len_t)dbt2->size-1;

        return syntax_cmp_fn(&bv1, &bv2);
    }

    /* else compare two "raw" index keys */
    bv1.bv_val = (char *)dbt1->data;
    bv1.bv_len = (ber_len_t)dbt1->size;

    bv2.bv_val = (char *)dbt2->data;
    bv2.bv_len = (ber_len_t)dbt2->size;

    return slapi_berval_cmp(&bv1, &bv2);
}

static int db_uses_feature(DB_ENV *db_env, u_int32_t flags)
{
    u_int32_t openflags = 0;
    PR_ASSERT(db_env);
    (*db_env->get_open_flags)(db_env, &openflags);

    return (flags & openflags);
}

int dblayer_db_uses_locking(DB_ENV *db_env) {
    return db_uses_feature(db_env, DB_INIT_LOCK);
}

int dblayer_db_uses_transactions(DB_ENV *db_env) {
    return db_uses_feature(db_env, DB_INIT_TXN);
}

int dblayer_db_uses_mpool(DB_ENV *db_env) {
    return db_uses_feature(db_env, DB_INIT_MPOOL);
}

int dblayer_db_uses_logging(DB_ENV *db_env) {
    return db_uses_feature(db_env, DB_INIT_LOG);
}

/* this flag use if user remotely turned batching off */

#define FLUSH_REMOTEOFF -1 
/* routine that allows batch value to be changed remotely:

    1. value = 0 turns batching off
    2. value = 1 makes behavior be like 5.0 but leaves batching on
    3. value > 1 changes batch value

    2 and 3 assume that nsslapd-db-transaction-batch-val is greater 0 at startup
*/

int
dblayer_set_batch_transactions(void *arg, void *value, char *errorbuf, int phase, int apply) {
    int val = (int)((uintptr_t)value);
    int retval = LDAP_SUCCESS;

    if (apply) {
        if(phase == CONFIG_PHASE_STARTUP) {
            trans_batch_limit=val;
        } else if(trans_batch_limit != FLUSH_REMOTEOFF ) { 
            if((val == 0) && (log_flush_thread)) { 
                log_flush_thread=PR_FALSE;
                trans_batch_limit = FLUSH_REMOTEOFF;
            } else if(val > 0) { 
                trans_batch_limit=val;
            }
        }
    }
    return retval;
}

void *
dblayer_get_batch_transactions(void *arg) {
    return (void *)((uintptr_t)trans_batch_limit);
}


/*
    Threading: dblayer isolates upper layers from threading considerations
    Everything in dblayer is free-threaded. That is, you can have multiple
    threads performing operations on a database and not worry about things.
    Obviously, if you do something stupid, like move a cursor forward in 
    one thread, and backwards in another at the same time, you get what you
    deserve. However, such a calling pattern will not crash your application ! 
*/

static int
dblayer_txn_checkpoint(struct ldbminfo *li, struct dblayer_private_env *env,
                       PRBool use_lock, PRBool busy_skip)
{
    int ret = 0;
    if (busy_skip && is_anyinstance_busy(li))
    {
        return ret;
    }
    DB_CHECKPOINT_LOCK(use_lock, env->dblayer_env_lock);
    ret = TXN_CHECKPOINT(env->dblayer_DB_ENV, 0, 0, DB_FORCE);
    DB_CHECKPOINT_UNLOCK(use_lock, env->dblayer_env_lock);
    return ret;
}

static int _dblayer_check_version(dblayer_private *priv)
{
    int major, minor = 0;
    char *string = 0;
    int ret = 0; 

    string = db_version(&major,&minor,NULL);
    if (major < DB_VERSION_MAJOR)
    {
        ret = -1;
    } 
    else
    {
        ret = 0;
    }
    /* DB3X: always POST 24 :) */
    priv->dblayer_lib_version = DBLAYER_LIB_VERSION_POST_24;
    LDAPDebug(LDAP_DEBUG_TRACE,"version check: %s (%d.%d)\n", string, major, minor);
    return ret;
}


/*
 * return nsslapd-db-home-directory (dblayer_dbhome_directory), if exists.
 * Otherwise, return nsslapd-directory (dblayer_home_directory).
 *
 * if dblayer_dbhome_directory exists, set 1 to dbhome.
 */
char *
dblayer_get_home_dir(struct ldbminfo *li, int *dbhome)
{
    dblayer_private *priv = (dblayer_private*)li->li_dblayer_private;
    char *home_dir = priv->dblayer_home_directory;
    if (dbhome)
        *dbhome = 0;

    if (priv->dblayer_dbhome_directory && *(priv->dblayer_dbhome_directory))
    {
        if (dbhome)
            *dbhome = 1;
        home_dir = priv->dblayer_dbhome_directory;
    }
    if (NULL == home_dir)
    {
        LDAPDebug(LDAP_DEBUG_ANY,"Db home directory is not set. "
            "Possibly %s (optinally %s) is missing in the config file.\n",
            CONFIG_DIRECTORY, CONFIG_DB_HOME_DIRECTORY, 0);
    }
    return home_dir;
}

/* Helper function which deletes the persistent state of the database library
 * IMHO this should be in inside libdb, but keith won't have it. 
 * Stop press---libdb now does delete these files on recovery, so we don't call this any more.
 */
static void dblayer_reset_env(struct ldbminfo *li)
{
    /* Remove the memory regions */
    dblayer_private *priv = (dblayer_private*)li->li_dblayer_private;
    DB_ENV *pEnv = priv->dblayer_env->dblayer_DB_ENV;
    char *home_dir = dblayer_get_home_dir(li, NULL);
    if (home_dir && *home_dir)
        pEnv->remove(pEnv, home_dir, DB_FORCE);
}

/* Callback function for libdb to spit error info into our log */
#if 1000*DB_VERSION_MAJOR + 100*DB_VERSION_MINOR >= 4300
void dblayer_log_print(const DB_ENV *dbenv, const char* prefix,
                       const char *buffer)
#else
void dblayer_log_print(const char* prefix, char *buffer)
#endif
{
    /* We ignore the prefix since we know who we are anyway */
    LDAPDebug(LDAP_DEBUG_ANY,"libdb: %s\n", buffer, 0, 0);    
}

void dblayer_remember_disk_filled(struct ldbminfo *li)
{
    dblayer_private *priv = NULL;

    PR_ASSERT(NULL != li);
    priv = li->li_dblayer_private;
    PR_ASSERT(NULL != priv);

    priv->dblayer_bad_stuff_happened = 1;
    
}

/* Function which calls libdb to override some system calls which
 * the library makes. We call this before calling any other function
 * in libdb.
 * Several OS use this, either partially or completely.
 * This will eventually change---we will simply pass to libdb
 * the addresses of a bunch of NSPR functions, and everything
 * will magically work on all platforms (Ha!)
 */

#ifdef DB_USE_64LFS
/* What is going on here ?
 * Well, some platforms now support an extended API for dealing with
 * files larger than 2G.  (This apparently comes from the LFS -- "Large
 * File Summit"... Summit, indeed.)  Anyway, we try to detect at runtime
 * whether this machine has the extended API, and use it if it's present.
 *
 */


/* helper function for open64 */
static int dblayer_open_large(const char *path, int oflag, mode_t mode)
{
    int err;

    err = open64(path, oflag, mode);
    /* weird but necessary: */
    if (err >= 0) errno = 0;
    return err;
}

/* this is REALLY dumb.  but nspr 19980529(x) doesn't support 64-bit files
 * because of some weirdness we're doing at initialization (?), so we need
 * to export some function that can open huge files, so that exporting
 * can work right.  when we fix the nspr problem (or get a more recent
 * version of nspr that might magically work?), this should be blown away.
 * (call mode_t an int because NT can't handle that in prototypes.)
 * -robey, 28oct98
 */
int dblayer_open_huge_file(const char *path, int oflag, int mode)
{
    return dblayer_open_large(path, oflag, (mode_t)mode);
}


#if 1000*DB_VERSION_MAJOR + 100*DB_VERSION_MINOR >= 4300
/* Helper function for large seeks, db4.3 */
static int dblayer_seek43_large(int fd, off64_t offset, int whence)
{
    int ret = 0;

    ret = lseek64(fd, offset, whence);

    return (ret < 0) ? errno : 0;
}
#else
/* Helper function for large seeks, db2.4 */
static int dblayer_seek24_large(int fd, size_t pgsize, db_pgno_t pageno,
                u_long relative, int isrewind, int whence)
{
    off64_t offset = 0, ret;

    offset = (off64_t)pgsize * pageno + relative;
    if (isrewind) offset = -offset;
    ret = lseek64(fd, offset, whence);

    return (ret < 0) ? errno : 0;
}
#endif

/* helper function for large fstat -- this depends on 'struct stat64' having
 * the following members:
 *    off64_t        st_size;
 *      long        st_blksize;
 */
static int dblayer_ioinfo_large(const char *path, int fd, u_int32_t *mbytesp,
                u_int32_t *bytesp, u_int32_t *iosizep)
{
    struct stat64 sb;

    if (fstat64(fd, &sb) < 0)
    return (errno);

    /* Return the size of the file. */
    if (mbytesp)
    *mbytesp = (u_int32_t) (sb.st_size / (off64_t) MEGABYTE);
    if (bytesp)
    *bytesp = (u_int32_t) (sb.st_size % (off64_t) MEGABYTE);

    if (iosizep)
    *iosizep = (u_int32_t)(sb.st_blksize);
    return 0;
}
/* Helper function to tell if a file exists */
/* On Solaris, if you use stat() on a file >4Gbytes, it fails with EOVERFLOW, 
   causing us to think that the file does not exist when it in fact does */
static int dblayer_exists_large(char *path, int *isdirp)
{
    struct stat64 sb;

    if (stat64(path, &sb) != 0)
    return (errno);

    if (isdirp != NULL)
        *isdirp = S_ISDIR(sb.st_mode);

    return (0);
}

#else   /* DB_USE_64LFS */

int dblayer_open_huge_file(const char *path, int oflag, int mode)
{
    return open(path, oflag, mode);
}

#endif  /* DB_USE_64LFS */


static int dblayer_override_libdb_functions(DB_ENV *pEnv, dblayer_private *priv)
{
#ifdef DB_USE_64LFS
    int major = 0;
    int minor = 0;

    /* Find out whether we are talking to a 2.3 or 2.4+ libdb */
    db_version(&major, &minor, NULL);

#ifndef irix
    /* irix doesn't have open64() */
    db_env_set_func_open((int (*)(const char *, int, ...))dblayer_open_large);
#endif  /* !irix */
    db_env_set_func_ioinfo(dblayer_ioinfo_large);
    db_env_set_func_exists((int (*)())dblayer_exists_large);
#if 1000*DB_VERSION_MAJOR + 100*DB_VERSION_MINOR >= 4300
    db_env_set_func_seek((int (*)())dblayer_seek43_large);
#else
    db_env_set_func_seek((int (*)())dblayer_seek24_large);
#endif

    LDAPDebug(LDAP_DEBUG_TRACE, "Enabled 64-bit files\n", 0, 0, 0);
#endif   /* DB_USE_64LFS */
    return 0;
}



/* This function is called in the initialization code, before the 
 * config file is read in, so we can't do much here
 */
int dblayer_init(struct ldbminfo *li)
{
    /* Allocate memory we need, create mutexes etc. */
    dblayer_private *priv = NULL;
    int ret = 0;

    PR_ASSERT(NULL != li);
    if (NULL != li->li_dblayer_private)
    {
        return -1;
    }
    
    priv = (dblayer_private*) slapi_ch_calloc(1,sizeof(dblayer_private));
    if (NULL == priv)
    {
        /* Memory allocation failed */
        return -1;
    }
    priv->thread_count_lock = PR_NewLock();
    priv->thread_count_cv = PR_NewCondVar(priv->thread_count_lock);
    li->li_dblayer_private = priv;

    /* For now, we call this to get debug printout */
    _dblayer_check_version(priv);

    /* moved db_env_create to dblayer_start */
    return ret;
}

int dblayer_terminate(struct ldbminfo *li)
{
    /* We assume that dblayer_close has been called already */
    dblayer_private *priv = (dblayer_private*)li->li_dblayer_private;
    Object *inst_obj;
    ldbm_instance *inst;
    int rval = 0;

    if (NULL == priv)    /* already terminated.  nothing to do */
        return rval;

    /* clean up mutexes */
    for (inst_obj = objset_first_obj(li->li_instance_set); inst_obj;
         inst_obj = objset_next_obj(li->li_instance_set, inst_obj)) {
        inst = (ldbm_instance *)object_get_data(inst_obj);
        if (NULL != inst->inst_db_mutex) {
            PR_DestroyLock(inst->inst_db_mutex);
            inst->inst_db_mutex = NULL;
        }
        if (NULL != inst->inst_handle_list_mutex) {
            PR_DestroyLock(inst->inst_handle_list_mutex);
            inst->inst_handle_list_mutex = NULL;
        }
    }
    
    slapi_ch_free_string(&priv->dblayer_log_directory);
    PR_DestroyCondVar(priv->thread_count_cv);
    priv->thread_count_cv = NULL;
    PR_DestroyLock(priv->thread_count_lock);
    priv->thread_count_lock = NULL;
    slapi_ch_free((void**)&priv);
    li->li_dblayer_private = NULL;

    if (config_get_entryusn_global()) {
        slapi_counter_destroy(&li->li_global_usn_counter);
    }

    return 0;
}

static void dblayer_select_ncache(size_t cachesize, int *ncachep)
{
	/* First thing, if the user asked to use a particular ncache,
	 * we let them, and don't override it here.
	 */
	if (*ncachep) {
		return;
	}
	/* If the user asked for a cache that's larger than 4G, 
	 * we _must_ select an ncache >0 , such that each 
	 * chunk is <4G. This is because DB won't accept a
	 * larger chunk.
	 */
#if defined(__LP64__) || defined (_LP64)
	if ( (sizeof(cachesize) > 4) && (cachesize > (4L * GIGABYTE))) {
		*ncachep = (cachesize / (4L * GIGABYTE)) + 1;
		LDAPDebug(LDAP_DEBUG_ANY,"Setting ncache to: %d to keep each chunk below 4Gbytes\n",
			*ncachep, 0, 0);
	}
#endif
	/* On Windows, we know that it's hard to allocate more than some 
	 * maximum chunk. In that case
	 * we set ncache to a sensible value.
	 */
#if defined(_WIN32)
	{
		size_t max_windows_chunk = (300 * MEGABYTE); /* This number was determined empirically on Win2k */
		if (cachesize > max_windows_chunk) {
			*ncachep = (cachesize / max_windows_chunk) + 1;
			LDAPDebug(LDAP_DEBUG_ANY,"Setting ncache to: %d for Windows memory address space fragmentation\n",
                *ncachep, 0, 0);
		}
	}
#endif
}

/* This function is no longer called : 
   It attempts to find the maximum allocatable chunk size
   by performing the actual allocations. However as a result
   it allocates pretty much _all_ the available memory, 
   causing the actual cache allocation to fail later.
   If there turns out to be a good safe way to determine
   the maximum chunk size, then we should use that instead.
   For now we just guess in dblayer_pick_ncache().
 */
#if 0
static void dblayer_get_ncache(size_t cachesize, int *ncachep)
{
    int myncache;
    int mymaxncache;
    int found = 0;
    char **head;

    if (*ncachep <= 0)    /* negative ncache is not allowed */
        myncache = 1;
    else
        myncache = *ncachep;

    mymaxncache = myncache + 20;    /* should be reasonable */

    head = (char **)slapi_ch_malloc(mymaxncache * sizeof(char *));
    do {
        int i;
        int end;
        size_t sz;
        size_t firstsz;
        size_t rest;

        rest = cachesize % myncache;
        sz = cachesize / myncache;
        firstsz = sz + rest;
        end = myncache;
        for (i = 0; i < myncache; i++) {
            if (i == 0)
                head[i] = (char *)malloc(firstsz);
            else
                head[i] = (char *)malloc(sz);
            if (NULL == head[i]) {
                end = i;
                myncache++;
                goto cleanup;
            }
        }
        found = 1;
cleanup:
        for (i = 0; i < end; i++) {
            slapi_ch_free((void **)&head[i]);
        }
        if (myncache == mymaxncache) {
            LDAPDebug(LDAP_DEBUG_ANY,
                "WARNING: dbcachesize %lu too big\n", cachesize, 0, 0);
            myncache = 0;
            found = -1;
        }
    } while (0 == found);
    *ncachep = myncache;
    slapi_ch_free((void **)&head);
    return;
}
#endif

void dblayer_free(void *ptr)
{
    slapi_ch_free(&ptr);
}

static void dblayer_init_dbenv(DB_ENV *pEnv, dblayer_private *priv)
{
    size_t  mysize;
    int     myncache = 1;

    mysize = priv->dblayer_cachesize;
    myncache = priv->dblayer_ncache;
    dblayer_select_ncache(mysize, &myncache); 
    priv->dblayer_ncache = myncache;

    dblayer_set_env_debugging(pEnv,priv);

    pEnv->set_lg_max(pEnv, priv->dblayer_logfile_size);
    pEnv->set_cachesize(pEnv, mysize / GIGABYTE, mysize % GIGABYTE, myncache);
    pEnv->set_lk_max_locks(pEnv, priv->dblayer_lock_config);
    pEnv->set_lk_max_objects(pEnv, priv->dblayer_lock_config);
    pEnv->set_lk_max_lockers(pEnv, priv->dblayer_lock_config); 

    /* shm_key required for named_regions (DB_SYSTEM_MEM) */
    pEnv->set_shm_key(pEnv, priv->dblayer_shm_key);

    /* increase max number of active transactions */
    pEnv->set_tx_max(pEnv, priv->dblayer_tx_max);

#if 1000*DB_VERSION_MAJOR + 100*DB_VERSION_MINOR >= 3300
    pEnv->set_alloc(pEnv, (void *)slapi_ch_malloc, (void *)slapi_ch_realloc, dblayer_free);

    /* 
     * The log region is used to store filenames and so needs to be
     * increased in size from the default for a large number of files. 
     */
    pEnv->set_lg_regionmax(pEnv, 1 * 1048576); /* 1 MB */
#endif
}

/* returns system pagesize (in bytes) and the number of pages of physical
 * RAM this machine has.
 * as a bonus, if 'procpages' is non-NULL, it will be filled in with the
 * approximate number of pages this process is using!
 * on platforms that we haven't figured out how to do this yet, both fields
 * are filled with zero and you're on your own.
 *
 * platforms supported so far:
 * Solaris, Linux, Windows
 */
#ifdef OS_solaris
#include <sys/procfs.h>
#include <sys/resource.h>
#endif
#ifdef LINUX
#include <linux/kernel.h>
#include <sys/sysinfo.h>    /* undocumented (?) */
#include <sys/resource.h>
#endif
#if defined ( hpux )
#include <sys/pstat.h>
#include <sys/resource.h>
#endif

#if !defined(_WIN32)
static size_t dblayer_getvirtualmemsize()
{
    struct rlimit rl;

    /* the maximum size of a process's total available memory, in bytes */
    getrlimit(RLIMIT_AS, &rl);
    return rl.rlim_cur;
}
#endif

/* pages = number of pages of physical ram on the machine (corrected for 32-bit build on 64-bit machine).
 * procpages = pages currently used by this process (or working set size, sometimes)
 * availpages = some notion of the number of pages 'free'. Typically this number is not useful.
 */
void dblayer_sys_pages(size_t *pagesize, size_t *pages, size_t *procpages, size_t *availpages)
{
    *pagesize = *pages = *availpages = 0;
    if (procpages)
        *procpages = 0;

#ifdef _WIN32
    {
        SYSTEM_INFO si;
        MEMORYSTATUS ms;

        GetSystemInfo(&si);
        ms.dwLength = sizeof(ms);
        GlobalMemoryStatus(&ms);
        *pagesize = si.dwPageSize;
        *pages = ms.dwTotalPhys  / si.dwPageSize;
        *availpages = ms.dwAvailVirtual / *pagesize;
        if (procpages) {
            DWORD minwss = 0, maxwss = 0;

            GetProcessWorkingSetSize(GetCurrentProcess(), &minwss, &maxwss);
            *procpages = (int)(maxwss / si.dwPageSize);
        }
    }
#endif

#ifdef OS_solaris
    *pagesize = (int)sysconf(_SC_PAGESIZE);
    *pages = (int)sysconf(_SC_PHYS_PAGES);
    *availpages = dblayer_getvirtualmemsize() / *pagesize;
    /* solaris has THE most annoying way to get this info */
    if (procpages) {
        struct prpsinfo psi;
        char fn[40];
        int fd;

        sprintf(fn, "/proc/%d", getpid());
        fd = open(fn, O_RDONLY);
        if (fd >= 0) {
                memset(&psi, 0, sizeof(psi));
            if (ioctl(fd, PIOCPSINFO, (void *)&psi) == 0)
                *procpages = psi.pr_size;
            close(fd);
        }
    }
#endif

#ifdef LINUX
    {
        struct sysinfo si;
        size_t pages_per_mem_unit = 0;
        size_t mem_units_per_page = 0; /* We don't know if these units are really pages */

        sysinfo(&si);
        *pagesize = getpagesize();
        if (si.mem_unit > *pagesize) {
            pages_per_mem_unit = si.mem_unit / *pagesize;
            *pages = si.totalram * pages_per_mem_unit;
        } else {
            mem_units_per_page = *pagesize / si.mem_unit;
            *pages = si.totalram / mem_units_per_page;
        }
        *availpages = dblayer_getvirtualmemsize() / *pagesize;
        /* okay i take that back, linux's method is more retarded here.
         * hopefully linux doesn't have the FILE* problem that solaris does
         * (where you can't use FILE if you have more than 256 fd's open)
         */
        if (procpages) {
            FILE *f;
            char fn[40], s[80];

            sprintf(fn, "/proc/%d/status", getpid());
            f = fopen(fn, "r");
            if (!f)    /* fopen failed */
                return;
            while (! feof(f)) {
                fgets(s, 79, f);
                if (feof(f))
                    break;
                if (strncmp(s, "VmSize:", 7) == 0) {
                    sscanf(s+7, "%lu", procpages);
                    break;
                }
            }
            fclose(f);
            /* procpages is now in 1k chunks, not pages... */
            *procpages /= (*pagesize / 1024);
        }
    }
#endif

#if defined ( hpux )
    {
        struct pst_static pst;
        int rval = pstat_getstatic(&pst, sizeof(pst), (size_t)1, 0);
        if (rval < 0)    /* pstat_getstatic failed */
            return;
        *pagesize = pst.page_size;
        *pages = pst.physical_memory;
        *availpages = dblayer_getvirtualmemsize() / *pagesize;
        if (procpages)
        {
#define BURST (size_t)32        /* get BURST proc info at one time... */
            struct pst_status psts[BURST];
            int i, count;
            int idx = 0; /* index within the context */
            int mypid = getpid();

            *procpages = 0;
            /* loop until count == 0, will occur all have been returned */
            while ((count = pstat_getproc(psts, sizeof(psts[0]), BURST, idx)) > 0) {
                /* got count (max of BURST) this time.  process them */
                for (i = 0; i < count; i++) {
                    if (psts[i].pst_pid == mypid)
                    {
                        *procpages = (size_t)(psts[i].pst_dsize + psts[i].pst_tsize + psts[i].pst_ssize);
                        break;
                    }
                }
                if (i < count)
                    break;

                /*
                 * now go back and do it again, using the next index after
                 * the current 'burst'
                 */
                idx = psts[count-1].pst_idx + 1;
            }
        }
    }
#endif
    /* If this is a 32-bit build, it might be running on a 64-bit machine,
     * in which case, if the box has tons of ram, we can end up telling 
     * the auto cache code to use more memory than the process can address.
     * so we cap the number returned here.
     */
#if defined(__LP64__) || defined (_LP64)
#else
    {    
        size_t one_gig_pages = GIGABYTE / *pagesize;
        if (*pages > (2 * one_gig_pages) ) {
            LDAPDebug(LDAP_DEBUG_TRACE,"More than 2Gbytes physical memory detected. Since this is a 32-bit process, truncating memory size used for auto cache calculations to 2Gbytes\n",
                0, 0, 0);
            *pages = (2 * one_gig_pages);
        }
    }
#endif
}


int dblayer_is_cachesize_sane(size_t *cachesize)
{
    size_t pages = 0, pagesize = 0, procpages = 0, availpages = 0;
    int issane = 1;

    dblayer_sys_pages(&pagesize, &pages, &procpages, &availpages);
    if (!pagesize || !pages)
        return 1;    /* do nothing when we can't get the avail mem */
    /* If the requested cache size is larger than the remaining pysical memory
     * after the current working set size for this process has been subtracted,
     * then we say that's insane and try to correct.
     */
    issane = (int)(*cachesize / pagesize) <= (pages - procpages);
    if (!issane) {
        *cachesize = (size_t)((pages - procpages) * pagesize);
    }
    /* We now compensate for DB's own compensation for metadata size 
     * They increase the actual cache size by 25%, but only for sizes
     * less than 500Meg.
     */
    if (*cachesize < 500*MEGABYTE) {
        *cachesize = (size_t)((double)*cachesize * (double)0.8);
    }
    
    return issane;
}


static void dblayer_dump_config_tracing(dblayer_private *priv)
{
    if (priv->dblayer_home_directory) {
        LDAPDebug(LDAP_DEBUG_TRACE,"home_directory=%s\n",priv->dblayer_home_directory,0,0);
    }
    if (priv->dblayer_log_directory) {
        LDAPDebug(LDAP_DEBUG_TRACE,"log_directory=%s\n",priv->dblayer_log_directory,0,0);
    }
    if (priv->dblayer_dbhome_directory) {
        LDAPDebug(LDAP_DEBUG_TRACE,"dbhome_directory=%s\n",priv->dblayer_dbhome_directory,0,0);
    }
    LDAPDebug(LDAP_DEBUG_TRACE,"trickle_percentage=%d\n",priv->dblayer_trickle_percentage,0,0);
    LDAPDebug(LDAP_DEBUG_TRACE,"page_size=%lu\n",priv->dblayer_page_size,0,0);
    LDAPDebug(LDAP_DEBUG_TRACE,"index_page_size=%lu\n",priv->dblayer_index_page_size,0,0);
    LDAPDebug(LDAP_DEBUG_TRACE,"cachesize=%lu\n",priv->dblayer_cachesize,0,0);
    LDAPDebug(LDAP_DEBUG_TRACE,"previous_cachesize=%lu\n",priv->dblayer_previous_cachesize,0,0);
    LDAPDebug(LDAP_DEBUG_TRACE,"ncache=%d\n",priv->dblayer_ncache,0,0);
    LDAPDebug(LDAP_DEBUG_TRACE,"previous_ncache=%d\n",priv->dblayer_previous_ncache,0,0);
    LDAPDebug(LDAP_DEBUG_TRACE,"recovery_required=%d\n",priv->dblayer_recovery_required,0,0);
    LDAPDebug(LDAP_DEBUG_TRACE,"durable_transactions=%d\n",priv->dblayer_durable_transactions,0,0);
    LDAPDebug(LDAP_DEBUG_TRACE,"checkpoint_interval=%d\n",priv->dblayer_checkpoint_interval,0,0);
    LDAPDebug(LDAP_DEBUG_TRACE,"transaction_batch_val=%d\n",trans_batch_limit,0,0);
    LDAPDebug(LDAP_DEBUG_TRACE,"circular_logging=%d\n",priv->dblayer_circular_logging,0,0);
    LDAPDebug(LDAP_DEBUG_TRACE,"idl_divisor=%d\n",priv->dblayer_idl_divisor,0,0);
    LDAPDebug(LDAP_DEBUG_TRACE,"logfile_size=%lu\n",priv->dblayer_logfile_size,0,0);
    LDAPDebug(LDAP_DEBUG_TRACE,"logbuf_size=%lu\n",priv->dblayer_logbuf_size,0,0);
    LDAPDebug(LDAP_DEBUG_TRACE,"file_mode=%d\n",priv->dblayer_file_mode,0,0);
    LDAPDebug(LDAP_DEBUG_TRACE,"cache_config=%d\n",priv->dblayer_cache_config,0,0);
    LDAPDebug(LDAP_DEBUG_TRACE,"lib_version=%d\n",priv->dblayer_lib_version,0,0);
    LDAPDebug(LDAP_DEBUG_TRACE,"spin_count=%d\n",priv->dblayer_spin_count,0,0);
    LDAPDebug(LDAP_DEBUG_TRACE,"named_regions=%d\n",priv->dblayer_named_regions,0,0);
    LDAPDebug(LDAP_DEBUG_TRACE,"private mem=%d\n",priv->dblayer_private_mem,0,0);
    LDAPDebug(LDAP_DEBUG_TRACE,"private import mem=%d\n",priv->dblayer_private_import_mem,0,0);
    LDAPDebug(LDAP_DEBUG_TRACE,"shm_key=%ld\n",priv->dblayer_shm_key,0,0);
    LDAPDebug(LDAP_DEBUG_TRACE,"lockdown=%d\n",priv->dblayer_lockdown,0,0);
    LDAPDebug(LDAP_DEBUG_TRACE,"tx_max=%d\n",priv->dblayer_tx_max,0,0);
}

/* Check a given filesystem directory for access we need */
#define DBLAYER_DIRECTORY_READ_ACCESS 1
#define DBLAYER_DIRECTORY_WRITE_ACCESS 2
#define DBLAYER_DIRECTORY_READWRITE_ACCESS 3
static int dblayer_grok_directory(char *directory, int flags)
{
    /* First try to open the directory using NSPR */
    /* If that fails, we can tell whether it's because it cannot be created or
     * we don't have any permission to access it */
    /* If that works, proceed to try to access files in the directory */
    char       filename[MAXPATHLEN];
    PRDir      *dirhandle = NULL;
    PRDirEntry *direntry = NULL;
    PRFileInfo info;

    dirhandle = PR_OpenDir(directory);
    if (NULL == dirhandle)
    {
        /* it does not exist or wrong file is there */
        /* try delete and mkdir */
        PR_Delete(directory);
        return mkdir_p(directory, 0700);
    }

    while (NULL !=
           (direntry = PR_ReadDir(dirhandle, PR_SKIP_DOT | PR_SKIP_DOT_DOT))) {
        if (NULL == direntry->name)
        {
            break;
        }
        PR_snprintf(filename, MAXPATHLEN, "%s/%s",directory,direntry->name);
        
        /* Right now this is set up to only look at files here. 
         * With multiple instances of the backend the are now other directories
         * in the db home directory.  This function wasn't ment to deal with
         * other directories, so we skip them. */
        if (PR_GetFileInfo(filename, &info) == PR_SUCCESS &&
            info.type == PR_FILE_DIRECTORY) {
            /* go into it (instance dir) */
            int retval = dblayer_grok_directory(filename, flags);
            PR_CloseDir(dirhandle);
            return retval;
        }

        /* If we are here, it means that the directory exists, that we can read
         * from it, and that there is at least one file there */
        /* We will try to open that file now if we were asked for read access */
        if (flags) {
            PRFileDesc *prfd;
            PRIntn open_flags = 0;
            char *access_string = NULL;
            
            if (DBLAYER_DIRECTORY_READ_ACCESS & flags) {
                open_flags = PR_RDONLY;
            }
            if (DBLAYER_DIRECTORY_WRITE_ACCESS & flags) {
                open_flags = PR_RDWR;
            }
            /* Let's hope that on Solaris we get to open large files OK */
            prfd = PR_Open(filename,open_flags,0);
            if (NULL == prfd) {
                if (DBLAYER_DIRECTORY_READ_ACCESS == flags) {
                    access_string = "read";
                } else {
                    if (DBLAYER_DIRECTORY_READ_ACCESS & flags) {
                        access_string = "write";
                    } else {
                        access_string = "****";
                    }
                }
                /* If we're here, it means that we did not have the requested
                 * permission on this file */
                LDAPDebug(LDAP_DEBUG_ANY,
                    "WARNING---no %s permission to file %s\n",
                    access_string,filename,0);
            } else {
                PR_Close(prfd);    /* okay */
            }
        }
    }
    PR_CloseDir(dirhandle);
    return 0;
}

static void
dblayer_set_data_dir(dblayer_private *priv, struct dblayer_private_env *pEnv,
                     char **data_directories)
{
    char **dirp;

    if (!(pEnv->dblayer_priv_flags & DBLAYER_PRIV_SET_DATA_DIR))
    {
        for (dirp = data_directories; dirp && *dirp; dirp++)
        {
            pEnv->dblayer_DB_ENV->set_data_dir(pEnv->dblayer_DB_ENV, *dirp);
        }
        pEnv->dblayer_priv_flags |= DBLAYER_PRIV_SET_DATA_DIR;
    }
}

static int
dblayer_inst_exists(ldbm_instance *inst, char *dbname)
{
    PRStatus prst;
    char id2entry_file[MAXPATHLEN];
    char *parent_dir = inst->inst_parent_dir_name;
    char sep = get_sep(parent_dir);
    char *dbnamep;
    if (dbname)
        dbnamep = dbname;
    else
        dbnamep = ID2ENTRY LDBM_FILENAME_SUFFIX;
    PR_snprintf(id2entry_file, sizeof(id2entry_file), "%s%c%s%c%s", parent_dir, sep, inst->inst_dir_name,
                                         sep, dbnamep);
    prst = PR_Access(id2entry_file, PR_ACCESS_EXISTS);
    if (PR_SUCCESS == prst)
        return 1;
    return 0;
}

/*
 * create a new DB_ENV and fill it with the goodies from dblayer_private
 */
static int 
dblayer_make_env(struct dblayer_private_env **env, struct ldbminfo *li)
{
    dblayer_private *priv = (dblayer_private*)li->li_dblayer_private;
    struct dblayer_private_env *pEnv;
    char *home_dir = NULL;
    int ret;
    Object *inst_obj;
    ldbm_instance *inst = NULL;

    pEnv = (struct dblayer_private_env *)slapi_ch_calloc(1,
                                                   sizeof(dblayer_private_env));

    if ((ret = db_env_create(&pEnv->dblayer_DB_ENV, 0)) != 0) {
        LDAPDebug(LDAP_DEBUG_ANY,
                  "ERROR -- Failed to create DB_ENV (returned: %d).\n",
                  ret, 0, 0);
    }

    DB_ENV_SET_REGION_INIT(pEnv->dblayer_DB_ENV);

    /* Here we overide various system functions called by libdb */
    ret = dblayer_override_libdb_functions(pEnv->dblayer_DB_ENV, priv);
    if (ret != 0)
        return ret;

    if (priv->dblayer_spin_count != 0) {
        DB_ENV_SET_TAS_SPINS(pEnv->dblayer_DB_ENV, priv->dblayer_spin_count);
    }

    dblayer_dump_config_tracing(priv);

    /* set data dir to avoid having absolute paths in the transaction log */
    for (inst_obj = objset_first_obj(li->li_instance_set);
         inst_obj;
         inst_obj = objset_next_obj(li->li_instance_set, inst_obj))
    {
        inst = (ldbm_instance *)object_get_data(inst_obj);
        if (inst->inst_parent_dir_name)
        {
            if (!charray_utf8_inlist(priv->dblayer_data_directories,
                                     inst->inst_parent_dir_name))
            {
                charray_add(&(priv->dblayer_data_directories),
                            slapi_ch_strdup(inst->inst_parent_dir_name));
            }
        }
    }
    home_dir = dblayer_get_home_dir(li, NULL);
    /* user specified db home */
    if (home_dir && *home_dir &&
        !charray_utf8_inlist(priv->dblayer_data_directories, home_dir))
    {
        charray_add(&(priv->dblayer_data_directories), home_dir);
    }

    /* user specified log dir */
    if (priv->dblayer_log_directory && *(priv->dblayer_log_directory)) {
        pEnv->dblayer_DB_ENV->set_lg_dir(pEnv->dblayer_DB_ENV,
                                         priv->dblayer_log_directory);
    }

    /* set up cache sizes */
    dblayer_init_dbenv(pEnv->dblayer_DB_ENV, priv);

    pEnv->dblayer_env_lock = PR_NewRWLock(PR_RWLOCK_RANK_NONE, "checkpointer");

    if (pEnv->dblayer_env_lock) {
        *env = pEnv;
    } else {
        LDAPDebug(LDAP_DEBUG_ANY,
            "ERROR -- Failed to create RWLock (returned: %d).\n", 
            ret, 0, 0);
    }

    return ret;
}

static void
dblayer_free_env(struct dblayer_private_env **env)
{
    if (NULL == env || NULL == *env) {
        return;
    }
    if ((*env)->dblayer_env_lock) {
        PR_DestroyRWLock((*env)->dblayer_env_lock);
        (*env)->dblayer_env_lock = NULL;
    }
    slapi_ch_free((void **)env);
    return;
}

/* generate an absolute path if the given instance dir is not.  */
char *
dblayer_get_full_inst_dir(struct ldbminfo *li, ldbm_instance *inst,
                          char *buf, int buflen)
{
    char *parent_dir = NULL;
    int mylen = 0;

    if (!inst)
        return NULL;

    if (inst->inst_parent_dir_name) /* e.g., /var/lib/dirsrv/slapd-ID/db */
    {
        parent_dir = inst->inst_parent_dir_name;
        mylen = strlen(parent_dir) + 1;
    }
    else
    {
        parent_dir = dblayer_get_home_dir(li, NULL);
        if (!parent_dir || !*parent_dir) {
            buf = NULL;
            return buf;
        }
        mylen = strlen(parent_dir);
        inst->inst_parent_dir_name = slapi_ch_strdup(parent_dir);
    }


    if (inst->inst_dir_name) /* e.g., userRoot */
    {
        mylen += strlen(inst->inst_dir_name) + 2;
        if (!buf || mylen > buflen)
            buf = slapi_ch_malloc(mylen);
        sprintf(buf, "%s%c%s",
                parent_dir, get_sep(parent_dir), inst->inst_dir_name);
    }
    else if (inst->inst_name)
    {
        inst->inst_dir_name = slapi_ch_strdup(inst->inst_name);
        mylen += strlen(inst->inst_dir_name) + 2;
        if (!buf || mylen > buflen)
            buf = slapi_ch_malloc(mylen);
        sprintf(buf, "%s%c%s",
                parent_dir, get_sep(parent_dir), inst->inst_dir_name);
    }
    else
    {
        mylen += 1;
        if (!buf || mylen > buflen)
            buf = slapi_ch_malloc(mylen);
        sprintf(buf, "%s", parent_dir);
    }
    return buf;
}

#if defined( OS_solaris )
#include <sys/types.h>
#include <sys/statvfs.h>
#endif

#if defined( hpux )
#undef f_type
#include <sys/types.h>
#include <sys/statvfs.h>
#define f_type        f_un.f_un_type
#endif

#if defined( linux )
#undef f_type
#include <sys/vfs.h>
#define f_type        f_un.f_un_type
#endif

static int
no_diskspace(struct ldbminfo *li)
{
    int rval = 0;
#if defined( OS_solaris ) || defined( hpux )
    struct statvfs fsbuf;
    if (statvfs(li->li_directory, &fsbuf) < 0)
    {
        LDAPDebug(LDAP_DEBUG_ANY,
            "Cannot get file system info; file system corrupted?\n", 0, 0, 0);
        rval = 1;
    }
    else
    {
        double fsiz = ((double)fsbuf.f_bavail) * fsbuf.f_frsize;
        double expected_siz = ((double)li->li_dbcachesize) * 1.5; /* dbcache +
                                                                region files */
        if (fsiz < expected_siz)
        {
            LDAPDebug(LDAP_DEBUG_ANY,
                "No enough space left on device (%lu bytes); "
                "at least %lu bytes space is needed for db region files\n",
                fsiz, expected_siz, 0);
            rval = 1;
        }
    }
#endif
#if defined( linux )
    struct statfs fsbuf;
    if (statfs(li->li_directory, &fsbuf) < 0)
    {
        LDAPDebug(LDAP_DEBUG_ANY,
            "Cannot get file system info; file system corrupted?\n", 0, 0, 0);
        rval = 1;
    }
    else
    {
        double fsiz = ((double)fsbuf.f_bavail) * fsbuf.f_bsize;
        double expected_siz = ((double)li->li_dbcachesize) * 1.5; /* dbcache +
                                                                region files */
        if (fsiz < expected_siz)
        {
            LDAPDebug(LDAP_DEBUG_ANY,
                "No enough space left on device (%lu bytes); "
                "at least %lu bytes space is needed for db region files\n",
                fsiz, expected_siz, 0);
            rval = 1;
        }
    }
#endif
    return rval;
}

/*
 * This function is called after all the config options have been read in,
 * so we can do real initialization work here.
 */
#define DBCONFLEN    3
#define CATASTROPHIC (struct dblayer_private_env *)-1

int dblayer_start(struct ldbminfo *li, int dbmode)
{
    /* 
     * So, here we open our DB_ENV session. We store it away for future use.
     * We also check to see if we exited cleanly last time. If we didn't,
     * we try to recover. If recovery fails, we're hosed.
     * We also create the thread which handles checkpointing and logfile
     * truncation here.
     */
    int return_value = -1;
    dblayer_private *priv = NULL;
    struct dblayer_private_env *pEnv = NULL;
    char *region_dir = NULL; /* directory to place region files */
    char *log_dir = NULL;    /* directory to place txn log files */
    int open_flags = 0;

    PR_ASSERT(NULL != li);

    priv = (dblayer_private*)li->li_dblayer_private;

    if (NULL == priv) {
        /* you didn't call init successfully */
        return -1;
    }

    if (NULL != priv->dblayer_env) {
        if (CATASTROPHIC == priv->dblayer_env) {
            LDAPDebug(LDAP_DEBUG_ANY,
                "Error: DB previously failed to start.\n", 0, 0, 0);
            return -1;
        } else {
            LDAPDebug(LDAP_DEBUG_ANY,
                "Warning: DB already started.\n", 0, 0, 0);
            return 0;
        }
    }

    /* DBDB we should pick these up in our config routine, and do away with
     *  the li_ one */
    if (NULL == li->li_directory || '\0' == *li->li_directory) {
        LDAPDebug(LDAP_DEBUG_ANY,
            "Error: DB directory is not specified.\n", 0, 0, 0);
        return -1;
    }
    PR_Lock(li->li_config_mutex);
    /* li->li_directory comes from nsslapd-directory */
    /* dblayer_home_directory is freed in dblayer_post_close.
     * li_directory needs to live beyond dblayer. */
    priv->dblayer_home_directory = slapi_ch_strdup(li->li_directory); 
    priv->dblayer_cachesize = li->li_dbcachesize;
    priv->dblayer_file_mode = li->li_mode;
    priv->dblayer_ncache = li->li_dbncache;
    PR_Unlock(li->li_config_mutex);

    /* use nsslapd-db-home-directory (dblayer_dbhome_directory), if set */
    /* Otherwise, nsslapd-directory (dblayer_home_directory). */
    region_dir = dblayer_get_home_dir(li, NULL);
    if (!region_dir || !(*region_dir)) {
        return -1;
    }

    /* Check here that the database directory both exists, and that we have
     * the appropriate access to it */
    return_value = dblayer_grok_directory(region_dir,
                                          DBLAYER_DIRECTORY_READWRITE_ACCESS);
    if (0 != return_value) {
        LDAPDebug(LDAP_DEBUG_ANY,"Can't start because the database "
                  "directory \"%s\" either doesn't exist, or is not "
                  "accessible\n", region_dir, 0, 0);
        return return_value;
    }

    log_dir = priv->dblayer_log_directory; /* nsslapd-db-logdirectory */
    if (log_dir && *log_dir) {
        /* checking the user defined log dir's accessability */
        return_value = dblayer_grok_directory(log_dir,
                           DBLAYER_DIRECTORY_READWRITE_ACCESS);
        if (0 != return_value) {
            LDAPDebug(LDAP_DEBUG_ANY,"Can't start because the log "
                      "directory \"%s\" either doesn't exist, or is not "
                      "accessible\n", log_dir, 0, 0);
            return return_value;
        }
    }

    /* Sanity check on cache size on platforms which allow us to figure out
     * the available phys mem */
    if (!dblayer_is_cachesize_sane(&(priv->dblayer_cachesize))) {
        /* Oops---looks like the admin misconfigured, let's warn them */
        LDAPDebug(LDAP_DEBUG_ANY,"WARNING---Likely CONFIGURATION ERROR---"
                  "dbcachesize is configured to use more than the available "
                  "physical memory, decreased to the largest available size (%lu bytes).\n",
                  priv->dblayer_cachesize, 0, 0);
        li->li_dbcachesize = priv->dblayer_cachesize;
    }

    /* fill in DB_ENV stuff from the common configuration */
    return_value = dblayer_make_env(&pEnv, li);
    if (return_value != 0)
        return return_value;

    if ((DBLAYER_NORMAL_MODE|DBLAYER_CLEAN_RECOVER_MODE) & dbmode)
    {
        /* Now, we read our metadata */
        return_value = read_metadata(li);
        if (0 != return_value) {
            /* The error message was output by read_metadata() */
            return -1;
        }
    }

    dblayer_free_env(&priv->dblayer_env);
    priv->dblayer_env = pEnv;

    open_flags = DB_CREATE | DB_INIT_MPOOL | DB_THREAD;

    if (priv->dblayer_enable_transactions) {
        open_flags |= (DB_INIT_TXN | DB_INIT_LOG | DB_INIT_LOCK);
        if (priv->dblayer_recovery_required) {
            open_flags |= DB_RECOVER;
            if (DBLAYER_RESTORE_MODE & dbmode) {
                LDAPDebug(LDAP_DEBUG_ANY, "Recovering database after restore "
                          "from archive.\n", 0, 0, 0);
            } else if (DBLAYER_CLEAN_RECOVER_MODE & dbmode) {
                LDAPDebug(LDAP_DEBUG_ANY, "Clean up db environment and start "
                          "from archive.\n", 0, 0, 0);
            } else {
                LDAPDebug(LDAP_DEBUG_ANY, "Detected Disorderly Shutdown last "
                          "time Directory Server was running, recovering "
                          "database.\n", 0, 0, 0);
            }
        }
        switch  (dbmode&DBLAYER_RESTORE_MASK) {
        case DBLAYER_RESTORE_MODE:
            open_flags |= DB_RECOVER_FATAL;
            open_flags &= ~DB_RECOVER;    /* shouldn't set both */
            if (!(dbmode & DBLAYER_NO_DBTHREADS_MODE))
                dbmode = DBLAYER_NORMAL_MODE; /* to restart helper threads */
            break;
        case DBLAYER_RESTORE_NO_RECOVERY_MODE:
            open_flags &= ~(DB_RECOVER | DB_RECOVER_FATAL);
            if (!(dbmode & DBLAYER_NO_DBTHREADS_MODE))
                dbmode = DBLAYER_NORMAL_MODE; /* to restart helper threads */
        }
    }

    if (priv->dblayer_private_mem) {
        LDAPDebug(LDAP_DEBUG_ANY,
                  "WARNING: Server is running with nsslapd-db-private-mem on; "
                  "No other process is allowed to access the database\n",
                  0, 0, 0);
        open_flags |= DB_PRIVATE;
    }

    if (priv->dblayer_named_regions) {
        open_flags |= DB_SYSTEM_MEM;
    }

    if (priv->dblayer_lockdown) {
        open_flags |= DB_LOCKDOWN;
    }
    

    /* Is the cache being re-sized ? (If we're just doing an archive or export,
     * we don't care if the cache is being re-sized) */
    if ( (priv->dblayer_previous_cachesize || priv->dblayer_previous_ncache) &&
         ((priv->dblayer_cachesize != priv->dblayer_previous_cachesize) ||
          (priv->dblayer_ncache != priv->dblayer_previous_ncache)) &&
         !(dbmode & (DBLAYER_ARCHIVE_MODE|DBLAYER_EXPORT_MODE)) ) {
         LDAPDebug(LDAP_DEBUG_ANY,
                      "I'm resizing my cache now...cache was %lu and is now %lu\n",
                      priv->dblayer_previous_cachesize, priv->dblayer_cachesize, 0);
        dblayer_reset_env(li);
        /*
         * Once pEnv->remove (via dblayer_reset_env) has been called,
         * the DB_ENV (pEnv) needs to be created again.
         */
        if ((return_value = dblayer_make_env(&pEnv, li)) != 0) {
            LDAPDebug(LDAP_DEBUG_ANY,
                      "ERROR -- Failed to create DBENV (returned: %d).\n",
                      return_value, 0, 0);
        }
        dblayer_free_env(&priv->dblayer_env);
        priv->dblayer_env = pEnv;
    }

    /* transactions enabled and logbuf size greater than sleepycat's default */
    if(priv->dblayer_enable_transactions && (priv->dblayer_logbuf_size > 0)) {
        if(priv->dblayer_logbuf_size >= 32768) {
            pEnv->dblayer_DB_ENV->set_lg_bsize(pEnv->dblayer_DB_ENV,priv->dblayer_logbuf_size);
        } else {
            LDAPDebug(LDAP_DEBUG_ANY, "using default value for log bufsize because configured value (%lu) is too small.\n", 
            priv->dblayer_logbuf_size, 0, 0);
        }
    }

    /* check if there's enough disk space to start */
    if (no_diskspace(li))
    {
        return ENOSPC;
    }

    dblayer_set_data_dir(priv, pEnv, priv->dblayer_data_directories);
    /* If we're doing recovery, we MUST open the env single-threaded ! */
    if ( (open_flags & DB_RECOVER) || (open_flags & DB_RECOVER_FATAL) ) {
        /* Recover, then close, then open again */
        int recover_flags = open_flags & ~DB_THREAD;

        if (DBLAYER_CLEAN_RECOVER_MODE & dbmode) /* upgrade case */
        {
            DB_ENV *thisenv = pEnv->dblayer_DB_ENV;
            return_value = thisenv->remove(thisenv, region_dir, DB_FORCE);
            if (0 != return_value)
            {
                LDAPDebug(LDAP_DEBUG_ANY,
                          "dblayer_start: failed to remove old db env "
                          "in %s: %s\n", region_dir, 
                          dblayer_strerror(return_value), 0);
                return return_value;
            }
            dbmode = DBLAYER_NORMAL_MODE;

            if ((return_value = dblayer_make_env(&pEnv, li)) != 0)
            {
                LDAPDebug(LDAP_DEBUG_ANY,
                          "ERROR -- Failed to create DBENV (returned: %d).\n",
                          return_value, 0, 0);
                return return_value;
            }
        }

        return_value = (pEnv->dblayer_DB_ENV->open)(
                        pEnv->dblayer_DB_ENV,
                        region_dir,
                        recover_flags,
                        priv->dblayer_file_mode
                        );
        if (0 != return_value) {
            if (return_value == ENOMEM) {
                /* 
                 * https://blackflag.mcom.com/show_bug.cgi?id=557319
                 * Crash ns-slapd while running scalab01 after restart slapd
                 */
                LDAPDebug(LDAP_DEBUG_ANY,
                          "mmap in opening database environment (recovery mode) "
                          "failed trying to allocate %lu bytes. (OS err %d - %s)\n",
                          li->li_dbcachesize, return_value, dblayer_strerror(return_value));
                dblayer_free_env(&priv->dblayer_env);
                priv->dblayer_env = CATASTROPHIC;
            } else {
                LDAPDebug(LDAP_DEBUG_ANY, "Database Recovery Process FAILED. "
                          "The database is not recoverable. err=%d: %s\n",
                          return_value, dblayer_strerror(return_value), 0);
                LDAPDebug(LDAP_DEBUG_ANY, 
                          "Please make sure there is enough disk space for "
                          "dbcache (%lu bytes) and db region files\n",
                          li->li_dbcachesize, 0, 0);
            }
            return return_value;
        } else {
            open_flags &= ~(DB_RECOVER | DB_RECOVER_FATAL);
            pEnv->dblayer_DB_ENV->close(pEnv->dblayer_DB_ENV, 0);
            if ((return_value = dblayer_make_env(&pEnv, li)) != 0) {
                LDAPDebug(LDAP_DEBUG_ANY,
                          "ERROR -- Failed to create DBENV (returned: %d).\n",
                          return_value, 0, 0);
                return return_value;
            }
            dblayer_free_env(&priv->dblayer_env);
            priv->dblayer_env = pEnv;
            dblayer_set_data_dir(priv, pEnv, priv->dblayer_data_directories);
        }
    }

    if ((!priv->dblayer_durable_transactions) || 
        ((priv->dblayer_enable_transactions) && (trans_batch_limit > 0))){
#if 1000*DB_VERSION_MAJOR + 100*DB_VERSION_MINOR >= 3200
#if 1000*DB_VERSION_MAJOR + 100*DB_VERSION_MINOR >= 4100 /* db4.1 and newer */
      pEnv->dblayer_DB_ENV->set_flags(pEnv->dblayer_DB_ENV, DB_TXN_WRITE_NOSYNC, 1);
#else /* db3.3 */
      pEnv->dblayer_DB_ENV->set_flags(pEnv->dblayer_DB_ENV, DB_TXN_NOSYNC, 1);
#endif
#else /* older */
      open_flags |= DB_TXN_NOSYNC;
#endif
    }
    if (!((DBLAYER_IMPORT_MODE|DBLAYER_INDEX_MODE) & dbmode))
    {
        pEnv->dblayer_openflags = open_flags;
        return_value = (pEnv->dblayer_DB_ENV->open)(
            pEnv->dblayer_DB_ENV,
            region_dir,
            open_flags,
            priv->dblayer_file_mode
            );


        /* Now attempt to start up the checkpoint and deadlock threads */
        /* note: need to be '==', not '&' to omit DBLAYER_NO_DBTHREADS_MODE */
        if ( (DBLAYER_NORMAL_MODE == dbmode ) && 
             (0 == return_value)) {
            /* update the dbversion file */
            dbversion_write(li, region_dir, NULL, DBVERSION_ALL);

            /* if dblayer_close then dblayer_start is called,
               this flag is set */
            priv->dblayer_stop_threads = 0; 
            if (0 != (return_value = dblayer_start_deadlock_thread(li))) {
                return return_value;
            }

            if (0 != (return_value = dblayer_start_checkpoint_thread(li))) {
                return return_value;
            }
            
            if (0 != (return_value = dblayer_start_log_flush_thread(priv))) {
                return return_value;
            }

            if (0 != (return_value = dblayer_start_trickle_thread(li))) {
                return return_value;
            }

            if (0 != (return_value = dblayer_start_perf_thread(li))) {
                return return_value;
            }

            /* Now open the performance counters stuff */
            perfctrs_init(li,&(priv->perf_private));
        }
        if (return_value != 0) {
            if (return_value == ENOMEM) {
                /* 
                 * https://blackflag.mcom.com/show_bug.cgi?id=557319
                 * Crash ns-slapd while running scalab01 after restart slapd
                 */
                LDAPDebug(LDAP_DEBUG_ANY,
                      "mmap in opening database environment "
                      "failed trying to allocate %d bytes. (OS err %lu - %s)\n",
                      li->li_dbcachesize, return_value, dblayer_strerror(return_value));
                dblayer_free_env(&priv->dblayer_env);
                priv->dblayer_env = CATASTROPHIC;
            } else {
                LDAPDebug(LDAP_DEBUG_ANY,
                      "Opening database environment (%s) failed. err=%d: %s\n",
                      region_dir, return_value, dblayer_strerror(return_value));
            }
        }
        return return_value;
    }
    return 0;
}

/*
 * If import cache autosize is enabled:
 *    nsslapd-import-cache-autosize: -1 or 1 ~ 99
 * calculate the import cache size.
 * If import cache is disabled:
 *    nsslapd-import-cache-autosize: 0
 * get the nsslapd-import-cachesize.
 * Calculate the memory size left after allocating the import cache size.
 * If the size is less than the hard limit, it issues an error and quit.
 * If the size is greater than the hard limit and less than the soft limit,
 * it issues a warning, but continues the import task.
 *
 * Note: this function is called only if the import is executed as a stand
 * alone command line (ldif2db).
 */
int
check_and_set_import_cache(struct ldbminfo *li)
{
    size_t import_pages = 0;
    size_t pagesize, pages, procpages, availpages;
    size_t soft_limit = 0;
    size_t hard_limit = 0;
    size_t page_delta = 0;
    char s[64];   /* big enough to hold %ld */

    dblayer_sys_pages(&pagesize, &pages, &procpages, &availpages);
    if (0 == pagesize || 0 == pages) {
        LDAPDebug2Args(LDAP_DEBUG_ANY, "check_and_set_import_cache: "
                       "Failed to get pagesize: %ld or pages: %ld\n",
                       pagesize, pages);
        return ENOENT;
    }
    LDAPDebug(LDAP_DEBUG_ANY, "check_and_set_import_cache: "
                  "pagesize: %ld, pages: %ld, procpages: %ld\n",
                  pagesize, pages, procpages);

    /* Soft limit: pages equivalent to 1GB (defined in dblayer.h) */
    soft_limit = (DBLAYER_IMPORTCACHESIZE_SL*1024) / (pagesize/1024);
    /* Hard limit: pages equivalent to 100MB (defined in dblayer.h) */
    hard_limit = (DBLAYER_IMPORTCACHESIZE_HL*1024) / (pagesize/1024);
    /*
     * default behavior for ldif2db import cache,
     * nsslapd-import-cache-autosize==-1,
     * autosize 50% mem to import cache
     */
    if (li->li_import_cache_autosize < 0) {
        li->li_import_cache_autosize = 50;
    }

    /* sanity check */
    if (li->li_import_cache_autosize >= 100) {
        LDAPDebug0Args(LDAP_DEBUG_ANY,
            "check_and_set_import_cache: "
            "import cache autosizing value "
            "(nsslapd-import-cache-autosize) should not be "
            "greater than or equal to 100(%). Reset to 50(%).\n");
        li->li_import_cache_autosize = 50;
    }

    if (li->li_import_cache_autosize == 0) {
        /* user specified importCache */
        import_pages = li->li_import_cachesize / pagesize;

    } else {
        /* autosizing importCache */
        /* ./125 instead of ./100 is for adjusting the BDB overhead. */
        import_pages = (li->li_import_cache_autosize * pages) / 125;
    }

    page_delta = pages - import_pages;
    if (page_delta < hard_limit) {
        LDAPDebug(LDAP_DEBUG_ANY, 
            "After allocating import cache %ldKB, "
            "the available memory is %ldKB, "
            "which is less than the hard limit %ldKB. "
            "Please decrease the import cache size and rerun import.\n",
            import_pages*(pagesize/1024), page_delta*(pagesize/1024),
            hard_limit*(pagesize/1024));
        return ENOMEM;
    }
    if (page_delta < soft_limit) {
        LDAPDebug(LDAP_DEBUG_ANY, 
            "WARNING: After allocating import cache %ldKB, "
            "the available memory is %ldKB, "
            "which is less than the soft limit %ldKB. "
            "You may want to decrease the import cache size and "
            "rerun import.\n",
            import_pages*(pagesize/1024), page_delta*(pagesize/1024),
            soft_limit*(pagesize/1024));
    }

    LDAPDebug1Arg(LDAP_DEBUG_ANY, "Import allocates %ldKB import cache.\n", 
                  import_pages*(pagesize/1024));
    if (li->li_import_cache_autosize > 0) { /* import cache autosizing */
        /* set the calculated import cache size to the config */
        sprintf(s, "%lu", (unsigned long)(import_pages * pagesize));
        ldbm_config_internal_set(li, CONFIG_IMPORT_CACHESIZE, s);
    }
    return 0;
}

/* mode is one of
 * DBLAYER_NORMAL_MODE,
 * DBLAYER_INDEX_MODE,
 * DBLAYER_IMPORT_MODE,
 * DBLAYER_EXPORT_MODE
 */
int dblayer_instance_start(backend *be, int mode)
{
    struct ldbminfo *li = (struct ldbminfo *) be->be_database->plg_private;
    ldbm_instance *inst = (ldbm_instance *) be->be_instance_info;
    dblayer_private *priv;
    struct dblayer_private_env *pEnv;
    char inst_dir[MAXPATHLEN];
    char *inst_dirp = NULL;
    int return_value = -1;

    priv = (dblayer_private*)li->li_dblayer_private;
    pEnv = priv->dblayer_env;
    if (CATASTROPHIC == pEnv || NULL == pEnv) {
        LDAPDebug(LDAP_DEBUG_ANY,
                  "instance %s: dbenv is not available (0x%x).\n", 
                  inst?inst->inst_name:"unknown", pEnv, 0);
        return return_value;
    }

    if (NULL != inst->inst_id2entry) {
        LDAPDebug(LDAP_DEBUG_ANY,
                  "Warning: DB instance \"%s\" already started.\n",
                  inst->inst_name, 0, 0);
        return 0;
    }

    if (attrcrypt_init(inst)) {
        LDAPDebug(LDAP_DEBUG_ANY,
                  "Error: unable to initialize attrcrypt system for %s\n",
                  inst->inst_name, 0, 0);
        return return_value;
    }

    /* Get the name of the directory that holds index files
     * for this instance. */
    if (dblayer_get_instance_data_dir(be) != 0) {
        /* Problem getting the name of the directory that holds the
         * index files for this instance. */
        return return_value;
    }

    inst_dirp = dblayer_get_full_inst_dir(li, inst, inst_dir, MAXPATHLEN);
    if (inst_dirp && *inst_dirp) {
        return_value = dblayer_grok_directory(inst_dirp,
                                          DBLAYER_DIRECTORY_READWRITE_ACCESS);
    } else {
        LDAPDebug(LDAP_DEBUG_ANY,"Can't start because the database instance "
                      "directory is NULL\n", 0, 0, 0);
        goto errout;
    }
    if (0 != return_value) {
        LDAPDebug(LDAP_DEBUG_ANY,"Can't start because the database instance "
                      "directory \"%s\" either doesn't exist, "
                      "or the db files are not accessible\n",
                      inst_dirp, 0, 0);
        goto errout;
    }

    if (mode & DBLAYER_NORMAL_MODE) {
        /* In normal mode (not db2ldif, ldif2db, etc.) we need to deal with 
         * the dbversion file here. */

        /* Read the dbversion file if there is one, and create it
         * if it doesn't exist. */
        if (dbversion_exists(li, inst_dirp)) {
            char *ldbmversion = NULL;
            char *dataversion = NULL;

            if (dbversion_read(li, inst_dirp, &ldbmversion, &dataversion) != 0) {
                LDAPDebug(LDAP_DEBUG_ANY, "Warning: Unable to read dbversion "
                          "file in %s\n", inst->inst_dir_name, 0, 0);
            } else {
                int rval = 0;
                /* check the DBVERSION and reset idl-switch if needed (DS6.2) */
                /* from the next major rel, we won't do this and just upgrade */
                if (!(li->li_flags & LI_FORCE_MOD_CONFIG)) {
                    adjust_idl_switch(ldbmversion, li);
                }
                slapi_ch_free_string(&ldbmversion);

                /* check to make sure these instance was made with the correct
                 * version. */
                rval = check_db_inst_version(inst);
                if (rval & DBVERSION_NOT_SUPPORTED)
                {
                    LDAPDebug(LDAP_DEBUG_ANY, "Instance %s does not have the "
                              "expected version\n", inst->inst_name, 0, 0);
                    PR_ASSERT(0);
                    return_value = -1;
                    goto errout;
                }
                else if (rval & DBVERSION_NEED_DN2RDN)
                {
                    LDAPDebug2Args(LDAP_DEBUG_ANY, 
                        "%s is on, while the instance %s is in the DN format. "
                        "Please run dn2rdn to convert the database format.\n",
                        CONFIG_ENTRYRDN_SWITCH, inst->inst_name);
                    return_value = -1;
                    goto errout;
                }
                else if (rval & DBVERSION_NEED_RDN2DN)
                {
                    LDAPDebug2Args(LDAP_DEBUG_ANY, 
                        "%s is off, while the instance %s is in the RDN "
                        "format. Please change the value to on in dse.ldif.\n",
                        CONFIG_ENTRYRDN_SWITCH, inst->inst_name);
                    return_value = -1;
                    goto errout;
                }

                /* record the dataversion */
                if (dataversion != NULL && *dataversion != '\0') {
                    inst->inst_dataversion = dataversion;
                } else {
                    slapi_ch_free_string(&dataversion);
                }

                rval = ldbm_upgrade(inst, rval);
                if (0 != rval)
                {
                    LDAPDebug(LDAP_DEBUG_ANY, "Upgrading instance %s failed\n",
                              inst->inst_name, 0, 0);
                    PR_ASSERT(0);
                    return_value = -1;
                    goto errout;
                }
                /**
                if (rval & DBVERSION_NEED_IDL_OLD2NEW)
                {
                    LDAPDebug(LDAP_DEBUG_ANY, 
                        "Instance %s: idl-switch is new while db idl format is "
                        "old, modify nsslapd-idl-switch in dse.ldif to old\n",
                        inst->inst_name, 0, 0);
                    return_value = -1;
                    goto errout;
                }
                else if (rval & DBVERSION_NEED_IDL_NEW2OLD)
                {
                    LDAPDebug(LDAP_DEBUG_ANY, 
                        "Instance %s: idl-switch is old while db idl format is "
                        "new, modify nsslapd-idl-switch in dse.ldif to new\n",
                        inst->inst_name, 0, 0);
                    return_value = -1;
                    goto errout;
                }
                **/
            }
        } else {
            /* The dbversion file didn't exist, so we'll create one. */
            dbversion_write(li, inst_dirp, NULL, DBVERSION_ALL);
        }
    } /* on import we don't mess with the dbversion file except to write it
       * when done with the import. */

    /* Now attempt to open id2entry */
    {
        char *id2entry_file;
        int open_flags = 0;
        DB *dbp;
        char *subname;
        struct dblayer_private_env *mypEnv;

        id2entry_file = slapi_ch_smprintf("%s/%s", inst->inst_dir_name, 
                ID2ENTRY LDBM_FILENAME_SUFFIX);

        open_flags = DB_CREATE | DB_THREAD;
        
        /* The subname argument allows applications to have
         * subdatabases, i.e., multiple databases inside of a single
         * physical file. This is useful when the logical databases
         * are both numerous and reasonably small, in order to
         * avoid creating a large number of underlying files.
         */
        subname = NULL;
        mypEnv = NULL;
        if (mode & (DBLAYER_IMPORT_MODE|DBLAYER_INDEX_MODE)) {
            size_t cachesize;
            char *data_directories[2] = {0, 0};
            /* [605974] delete DB_PRIVATE: 
             * to make import visible to the other process */
            int oflags = DB_CREATE | DB_INIT_MPOOL | DB_THREAD;
            /*
             * but nsslapd-db-private-import-mem should work with import,
             * as well */
            if (priv->dblayer_private_import_mem) {
                LDAPDebug(LDAP_DEBUG_ANY,
                  "WARNING: Import is running with "
                  "nsslapd-db-private-import-mem on; "
                  "No other process is allowed to access the database\n",
                  0, 0, 0);
                oflags |= DB_PRIVATE;
            }
            PR_Lock(li->li_config_mutex);
            /* import cache checking and autosizing is available only
             * for the command line */
            if (li->li_flags & SLAPI_TASK_RUNNING_FROM_COMMANDLINE) {
                return_value = check_and_set_import_cache(li);
                if (return_value) {
                    goto out;
                }
            }
            cachesize = li->li_import_cachesize;
            PR_Unlock(li->li_config_mutex);

            if (cachesize < 1048576) {
                /* make it at least 1M */
                cachesize = 1048576;
            }
            priv->dblayer_cachesize = cachesize;
            /* We always auto-calculate ncache for the import region */
            priv->dblayer_ncache = 0;

            /* use our own env */
            return_value = dblayer_make_env(&mypEnv, li);
            if (return_value != 0) {
                LDAPDebug(LDAP_DEBUG_ANY,
                          "Unable to create new DB_ENV for import/export! %d\n",
                          return_value, 0, 0);
                goto out;
            }
            /* do not assume import cache size is under 1G */
            mypEnv->dblayer_DB_ENV->set_cachesize(mypEnv->dblayer_DB_ENV,
                                                  cachesize/GIGABYTE,
                                                  cachesize%GIGABYTE,
                                                  priv->dblayer_ncache);
            /* probably want to change this -- but for now, create the 
             * mpool files in the instance directory.
             */
            mypEnv->dblayer_openflags = oflags;
            data_directories[0] = inst->inst_parent_dir_name;
            dblayer_set_data_dir(priv, mypEnv, data_directories);
            return_value = (mypEnv->dblayer_DB_ENV->open)(mypEnv->dblayer_DB_ENV,
                                                       inst_dirp,
                                                       oflags,
                                                       priv->dblayer_file_mode);
            if (return_value != 0) {
                LDAPDebug(LDAP_DEBUG_ANY,
                          "Unable to open new DB_ENV for import/export! %d\n",
                          return_value, 0, 0);
                goto out;
            }
            inst->import_env = mypEnv;
        } else {
            mypEnv = pEnv;
        }

        inst->inst_id2entry = NULL;
        return_value = db_create(&inst->inst_id2entry, mypEnv->dblayer_DB_ENV, 0);
        if (0 != return_value) {
            LDAPDebug(LDAP_DEBUG_ANY,
                      "Unable to create id2entry db file! %d\n",
                      return_value, 0, 0);
            goto out;
        }
        dbp = inst->inst_id2entry;

        return_value = dbp->set_pagesize(dbp,
            (priv->dblayer_page_size == 0) ? DBLAYER_PAGESIZE :
            priv->dblayer_page_size);
        if (0 != return_value) {
            LDAPDebug(LDAP_DEBUG_ANY,
                      "dbp->set_pagesize(%lu or %lu) failed %d\n",
                      priv->dblayer_page_size, DBLAYER_PAGESIZE,
                      return_value);
            goto out;
        }

#if 1000*DB_VERSION_MAJOR + 100*DB_VERSION_MINOR < 3300
        return_value = dbp->set_malloc(dbp, (void *)slapi_ch_malloc);
        if (0 != return_value) {
            LDAPDebug(LDAP_DEBUG_ANY,
                      "dbp->set_malloc failed %d\n",
                      return_value, 0, 0);

            goto out;
        }
#endif
        if ((charray_get_index(priv->dblayer_data_directories,
                               inst->inst_parent_dir_name) != 0) &&
            !dblayer_inst_exists(inst, NULL))
        {
            char *abs_id2entry_file = NULL;
            /* create a file with abs path, then try again */

            abs_id2entry_file = slapi_ch_smprintf( "%s%c%s", inst_dirp, 
                    get_sep(inst_dirp), ID2ENTRY LDBM_FILENAME_SUFFIX);
            DB_OPEN(mypEnv->dblayer_openflags,
                dbp, NULL/* txnid */, abs_id2entry_file, subname, DB_BTREE,
                open_flags, priv->dblayer_file_mode, return_value);
            dbp->close(dbp, 0);
            return_value = db_create(&inst->inst_id2entry,
                                     mypEnv->dblayer_DB_ENV, 0);
            if (0 != return_value)
                goto out;
            dbp = inst->inst_id2entry;
            slapi_ch_free_string(&abs_id2entry_file);
        }
        DB_OPEN(mypEnv->dblayer_openflags,
                dbp, NULL/* txnid */, id2entry_file, subname, DB_BTREE,
                open_flags, priv->dblayer_file_mode, return_value);
        if (0 != return_value) {
            LDAPDebug(LDAP_DEBUG_ANY,
                      "dbp->open(\"%s\") failed: %s (%d)\n",
                      id2entry_file, dblayer_strerror(return_value),
                      return_value);
            /* if it's a newly created backend instance,
             * need to check the inst_parent_dir already exists and
             * set as a data dir */
            if (strstr(dblayer_strerror(return_value),
                       "No such file or directory"))
            {
                LDAPDebug(LDAP_DEBUG_ANY,
                      "Instance %s is not registered as a db data directory. "
                      "Please restart the server to create it.\n",
                      inst?inst->inst_name:"unknown", pEnv, 0);
            }
            else if (strstr(dblayer_strerror(return_value),
                            "Permission denied"))
            {
                LDAPDebug(LDAP_DEBUG_ANY,
                          "Instance directory %s may not be writable\n",
                          inst_dirp, 0, 0);
            }

            goto out;
        }
#if 1000*DB_VERSION_MAJOR + 100*DB_VERSION_MINOR == 4100
        /* lower the buffer cache priority to avoid sleep in memp_alloc */
        /* W/ DB_PRIORITY_LOW, the db buffer page priority is calculated as:
         *     priority = lru_count + pages / (-1)
         * (by default, priority = lru_count)
         * When upgraded to db4.2, this setting may not needed, hopefully.
         * ask sleepycat [#8301]; blackflag #619964
         */
        dbp->set_cache_priority(dbp, DB_PRIORITY_LOW);
#endif
out:
        slapi_ch_free((void**)&id2entry_file);
    }

    if (0 == return_value) {
        /* get nextid from disk now */
        get_ids_from_disk(be);
    }

    if (mode & DBLAYER_NORMAL_MODE) {
        dbversion_write(li, inst_dirp, NULL, DBVERSION_ALL);
        /* richm - not sure if need to acquire the be lock first? */
        /* need to set state back to started - set to stopped in
           dblayer_instance_close */
        be->be_state = BE_STATE_STARTED;
    }

    /*
     * check if nextid is valid: it only matters if the database is either
     * being imported or is in normal mode
     */
    if (inst->inst_nextid > MAXID && !(mode & DBLAYER_EXPORT_MODE)) {
        LDAPDebug(LDAP_DEBUG_ANY, "dblayer_instance_start fail: backend '%s' "
            "has no IDs left. DATABASE MUST BE REBUILT.\n",
            be->be_name, 0, 0);
        return 1;
    }

    if (return_value != 0) {
        LDAPDebug(LDAP_DEBUG_ANY, "dblayer_instance_start fail: %s (%d)\n",
            dblayer_strerror(return_value), return_value, 0);   
    }
errout:
    if (inst_dirp != inst_dir)
        slapi_ch_free_string(&inst_dirp);
    return return_value;
}


/* This returns a DB* for the primary index.
 * If the database library is non-reentrant, we lock it.
 * the caller MUST call to unlock the db library once they're
 * finished with the handle. Luckily, the back-end already has
 * these semantics for the older dbcache stuff.
 */
/* Things have changed since the above comment was
 * written.  The database library is reentrant. */
int dblayer_get_id2entry(backend *be, DB **ppDB)
{
    ldbm_instance *inst;

    PR_ASSERT(NULL != be);
    
    inst = (ldbm_instance *) be->be_instance_info;

    *ppDB = inst->inst_id2entry;
    return 0;
}

int dblayer_release_id2entry(backend *be, DB *pDB)
{
    return 0;
}

/*
 * dblayer_get_aux_id2entry:
 * - create a dedicated db env and db handler for id2entry.
 * - introduced for upgradedb not to share the env and db handler with
 *   other index files to support multiple passes and merge.
 */
int dblayer_get_aux_id2entry(backend *be, DB **ppDB, DB_ENV **ppEnv)
{
    ldbm_instance *inst;
    struct dblayer_private_env *mypEnv = NULL;
    DB *dbp = NULL;
    int rval = 1;
    struct ldbminfo *li = NULL;
    dblayer_private *opriv = NULL;
    dblayer_private *priv = NULL;
    char *subname = NULL;
    int envflags;
    size_t cachesize;
    PRFileInfo prfinfo;
    PRStatus prst;
    char *id2entry_file = NULL;
    char inst_dir[MAXPATHLEN];
    char *inst_dirp = NULL;
    char *data_directories[2] = {0, 0};

    PR_ASSERT(NULL != be);
    
    *ppDB = NULL;
    *ppEnv = NULL;
    inst = (ldbm_instance *) be->be_instance_info;
    if (NULL == inst)
    {
        LDAPDebug(LDAP_DEBUG_ANY,
            "No instance/env: persistent id2entry is not available\n", 0, 0, 0);
        goto done;
    }

    li = inst->inst_li;
    if (NULL == li)
    {
        LDAPDebug(LDAP_DEBUG_ANY,
            "No ldbm info: persistent id2entry is not available\n", 0, 0, 0);
        goto done;
    }

    opriv = li->li_dblayer_private;
    if (NULL == opriv)
    {
        LDAPDebug(LDAP_DEBUG_ANY,
            "No dblayer info: persistent id2entry is not available\n", 0, 0, 0);
        goto done;
    }
    priv = (dblayer_private *)slapi_ch_malloc(sizeof(dblayer_private));
    memcpy(priv, opriv, sizeof(dblayer_private));
    priv->dblayer_spin_count = 0;

    inst_dirp = dblayer_get_full_inst_dir(li, inst, inst_dir, MAXPATHLEN);
    if (inst_dirp && *inst_dirp)
    {
        priv->dblayer_home_directory = slapi_ch_smprintf("%s/dbenv", inst_dirp);
    }
    else
    {
        LDAPDebug(LDAP_DEBUG_ANY,
          "Instance dir is NULL: persistent id2entry is not available\n",
          0, 0, 0);
        goto done;
    }
    priv->dblayer_log_directory = slapi_ch_strdup(priv->dblayer_home_directory);

    prst = PR_GetFileInfo(inst_dirp, &prfinfo);
    if (PR_FAILURE == prst || PR_FILE_DIRECTORY != prfinfo.type)
    {
        LDAPDebug(LDAP_DEBUG_ANY,
            "No inst dir: persistent id2entry is not available\n", 0, 0, 0);
        goto done;
    }

    prst = PR_GetFileInfo(priv->dblayer_home_directory, &prfinfo);
    if (PR_SUCCESS == prst)
    {
        ldbm_delete_dirs(priv->dblayer_home_directory);
    }
    rval = mkdir_p(priv->dblayer_home_directory, 0700);
    if (0 != rval)
    {
        LDAPDebug(LDAP_DEBUG_ANY,
            "can't create env dir: persistent id2entry is not available\n", 0, 0, 0);
        goto done;
    }

    /* use our own env */
    rval = dblayer_make_env(&mypEnv, li);
    if (rval != 0) {
        LDAPDebug(LDAP_DEBUG_ANY,
            "Unable to create new DB_ENV for import/export! %d\n", rval, 0, 0);
        goto err;
    }

    envflags = DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE;
    cachesize = 1048576; /* 1M */

    mypEnv->dblayer_DB_ENV->set_cachesize(mypEnv->dblayer_DB_ENV,
                0, cachesize, priv->dblayer_ncache);

    /* probably want to change this -- but for now, create the 
     * mpool files in the instance directory.
     */
    mypEnv->dblayer_openflags = envflags;
    data_directories[0] = inst->inst_parent_dir_name;
    dblayer_set_data_dir(priv, mypEnv, data_directories);
    rval = (mypEnv->dblayer_DB_ENV->open)(mypEnv->dblayer_DB_ENV,
            priv->dblayer_home_directory, envflags, priv->dblayer_file_mode);
    if (rval != 0)
    {
        LDAPDebug(LDAP_DEBUG_ANY,
            "Unable to open new DB_ENV for upgradedb/reindex %d\n", rval, 0, 0);
        goto err;
    }
    *ppEnv = mypEnv->dblayer_DB_ENV;

    rval = db_create(&dbp, mypEnv->dblayer_DB_ENV, 0);
    if (0 != rval) {
        LDAPDebug(LDAP_DEBUG_ANY,
                  "Unable to create id2entry db handler! %d\n", rval, 0, 0);
        goto err;
    }

    rval = dbp->set_pagesize(dbp, (priv->dblayer_page_size == 0) ?
                        DBLAYER_PAGESIZE : priv->dblayer_page_size);
    if (0 != rval)
    {
        LDAPDebug(LDAP_DEBUG_ANY,
            "dbp->set_pagesize(%lu or %lu) failed %d\n",
            priv->dblayer_page_size, DBLAYER_PAGESIZE, rval);
        goto err;
    }

    id2entry_file = slapi_ch_smprintf("%s/%s",
        inst->inst_dir_name, ID2ENTRY LDBM_FILENAME_SUFFIX);

    PR_ASSERT(dblayer_inst_exists(inst, NULL));
    DB_OPEN(mypEnv->dblayer_openflags,
            dbp, NULL/* txnid */, id2entry_file, subname, DB_BTREE,
            0, priv->dblayer_file_mode, rval);
    if (0 != rval)
    {
        LDAPDebug(LDAP_DEBUG_ANY,
                  "dbp->open(\"%s\") failed: %s (%d)\n",
                  id2entry_file, dblayer_strerror(rval), rval);
        if (strstr(dblayer_strerror(rval), "Permission denied"))
        {
            LDAPDebug(LDAP_DEBUG_ANY,
                "Instance directory %s may not be writable\n", inst_dirp, 0, 0);
        }
        goto err;
    }
    *ppDB = dbp;
    goto done;
err:
    if (*ppEnv)
       (*ppEnv)->close(*ppEnv, 0);
    if (priv->dblayer_home_directory)
        ldbm_delete_dirs(priv->dblayer_home_directory);
done:
    slapi_ch_free_string(&id2entry_file);
    if (priv) {
        slapi_ch_free_string(&priv->dblayer_home_directory);
        slapi_ch_free_string(&priv->dblayer_log_directory);
    }
    /* Don't free priv->dblayer_data_directories since priv doesn't own the memory */
    slapi_ch_free((void **)&priv);
    slapi_ch_free((void **)&mypEnv);
    if (inst_dirp != inst_dir)
        slapi_ch_free_string(&inst_dirp);
    return rval;
}

int dblayer_release_aux_id2entry(backend *be, DB *pDB, DB_ENV *pEnv)
{
    ldbm_instance *inst;
    char *envdir = NULL;
    char inst_dir[MAXPATHLEN];
    char *inst_dirp = NULL;

    inst = (ldbm_instance *) be->be_instance_info;
    if (NULL == inst)
    {
        LDAPDebug(LDAP_DEBUG_ANY,
            "No instance/env: persistent id2entry is not available\n", 0, 0, 0);
        goto done;
    }

    inst_dirp = dblayer_get_full_inst_dir(inst->inst_li, inst,
                                          inst_dir, MAXPATHLEN);
    if (inst_dirp && *inst_dirp)
    {
        envdir = slapi_ch_smprintf("%s/dbenv", inst_dirp);
    }

done:
    if (pDB)
       pDB->close(pDB, 0);
    if (pEnv)
       pEnv->close(pEnv, 0);
    if (envdir) {
        ldbm_delete_dirs(envdir);
        slapi_ch_free_string(&envdir);
    }
    if (inst_dirp != inst_dir)
        slapi_ch_free_string(&inst_dirp);
    return 0;
}

int dblayer_close_indexes(backend *be)
{
    ldbm_instance *inst;
    DB *pDB = NULL;
    dblayer_handle *handle = NULL;
    dblayer_handle *next = NULL;
    int return_value = 0;

    PR_ASSERT(NULL != be);
    inst = (ldbm_instance *) be->be_instance_info;
    PR_ASSERT(NULL != inst);

    for (handle = inst->inst_handle_head; handle != NULL; handle = next) {
        /* Close it, and remove from the list */
        pDB = handle->dblayer_dbp;
        return_value |= pDB->close(pDB,0);
        next = handle->dblayer_handle_next;
        *(handle->dblayer_handle_ai_backpointer) = NULL;
        slapi_ch_free((void**)&handle);
    }

    /* reset the list to make sure we don't use it again */
    inst->inst_handle_tail = NULL;
    inst->inst_handle_head = NULL;

    return return_value;
}

int dblayer_instance_close(backend *be)
{
    DB *pDB = NULL;
    int return_value = 0;
    DB_ENV * env = 0;
    ldbm_instance *inst = (ldbm_instance *)be->be_instance_info;

    if (NULL == inst)
        return -1;

#if defined(_USE_VALGRIND)
    /* When running a memory leak checking tool (e.g., valgrind),
       it reduces the noise by enabling this code. */
    LDAPDebug1Arg(LDAP_DEBUG_ANY, "%s: Cleaning up entry cache\n",
                                  inst->inst_name);
    cache_clear(&inst->inst_cache, CACHE_TYPE_ENTRY);
    LDAPDebug1Arg(LDAP_DEBUG_ANY, "%s: Cleaning up dn cache\n",
                                  inst->inst_name);
    cache_clear(&inst->inst_dncache, CACHE_TYPE_DN);
#endif

    if (attrcrypt_cleanup_private(inst)) {
        LDAPDebug(LDAP_DEBUG_ANY,
                  "Error: failed to clean up attrcrypt system for %s\n",
                  inst->inst_name, 0, 0);
    }

    return_value = dblayer_close_indexes(be);

    /* Now close id2entry if it's open */
    pDB = inst->inst_id2entry;
    if (NULL != pDB) {
        return_value |= pDB->close(pDB,0);
    }
    inst->inst_id2entry = NULL;

    if (inst->import_env) {
       /* ignore the value of env, close, because at this point, 
        * work is done with import env by calling env.close, 
        * env and all the associated db handles will be closed, ignore,
        * if sleepycat complains, that db handles are open at env close time */
       return_value |= inst->import_env->dblayer_DB_ENV->close(inst->import_env->dblayer_DB_ENV, 0);
       return_value = db_env_create(&env, 0);
       if (return_value == 0) {
         char inst_dir[MAXPATHLEN];
         char *inst_dirp = dblayer_get_full_inst_dir(inst->inst_li, inst,
                                                     inst_dir, MAXPATHLEN);
         if (inst_dirp && *inst_dir) {
           return_value = env->remove(env, inst_dirp, 0);
         }
         else
         {
           return_value = -1;
         }
         if (return_value == EBUSY) {
           return_value = 0; /* something else is using the env so ignore */
         }
         if (inst_dirp != inst_dir)
           slapi_ch_free_string(&inst_dirp);
       }
       PR_DestroyRWLock(inst->import_env->dblayer_env_lock);
       slapi_ch_free((void **)&inst->import_env);
    } else {
       be->be_state = BE_STATE_STOPPED;
    }

    return return_value;
}

void dblayer_pre_close(struct ldbminfo *li)
{
    dblayer_private *priv = 0;
    PRInt32 threadcount = 0;

    PR_ASSERT(NULL != li);
    priv = (dblayer_private*)li->li_dblayer_private;

    if (priv->dblayer_stop_threads)    /* already stopped.  do nothing... */
        return;

    /* first, see if there are any housekeeping threads running */
    PR_Lock(priv->thread_count_lock);
    threadcount = priv->dblayer_thread_count;
    PR_Unlock(priv->thread_count_lock);

    if (threadcount) {
        PRIntervalTime cvwaittime = PR_MillisecondsToInterval(DBLAYER_SLEEP_INTERVAL * 100);
        int timedout = 0;
        /* Print handy-dandy log message */
        LDAPDebug(LDAP_DEBUG_ANY,"Waiting for %d database threads to stop\n",
                  threadcount, 0,0);
        PR_Lock(priv->thread_count_lock);
        /* Tell them to stop - we wait until the last possible moment to invoke
           this.  If we do this much sooner than this, we could find ourselves
           in a situation where the threads see the stop_threads and exit before
           we can issue the WaitCondVar below, which means the last thread to
           exit will do a NotifyCondVar that has nothing waiting.  If we do this
           inside the lock, we will ensure that the threads will block until we
           issue the WaitCondVar below */
        priv->dblayer_stop_threads = 1;
        /* Wait for them to exit */
        while (priv->dblayer_thread_count > 0) {
            PRIntervalTime before = PR_IntervalNow();
            /* There are 3 ways to wake up from this WaitCondVar:
               1) The last database thread exits and calls NotifyCondVar - thread_count
               should be 0 in this case
               2) Timeout - in this case, thread_count will be > 0 - bad
               3) A bad error occurs - bad - will be reported as a timeout
            */
            PR_WaitCondVar(priv->thread_count_cv, cvwaittime);
            if (priv->dblayer_thread_count > 0) {
                /* still at least 1 thread running - see if this is a timeout */
                if ((PR_IntervalNow() - before) >= cvwaittime) {
                    threadcount = priv->dblayer_thread_count;
                    timedout = 1;
                    break;
                }
                /* else just a spurious interrupt */
            }
        }
        PR_Unlock(priv->thread_count_lock); 
        if (timedout) {
            LDAPDebug(LDAP_DEBUG_ANY,
                      "Timeout after [%d] milliseconds; leave %d database thread(s)...\n",
                      (DBLAYER_SLEEP_INTERVAL * 100), threadcount,0);
            priv->dblayer_bad_stuff_happened = 1;
            goto timeout_escape;
        }
    }
    LDAPDebug(LDAP_DEBUG_ANY,"All database threads now stopped\n",0,0,0);
timeout_escape:
    return;
}

int dblayer_post_close(struct ldbminfo *li, int dbmode)
{
    dblayer_private *priv = 0;
    int return_value = 0;
    dblayer_private_env *pEnv;

    PR_ASSERT(NULL != li);
    priv = (dblayer_private*)li->li_dblayer_private;

    /* We close all the files we ever opened, and call pEnv->close. */
    if (NULL == priv->dblayer_env) /* db env is already closed. do nothing. */
        return return_value;
    /* Shutdown the performance counter stuff */
    if (DBLAYER_NORMAL_MODE & dbmode) {
        if (priv->perf_private) {
            perfctrs_terminate(&priv->perf_private, priv->dblayer_env->dblayer_DB_ENV);
        }
    }

    /* Now release the db environment */
    pEnv = priv->dblayer_env;
    return_value = pEnv->dblayer_DB_ENV->close(pEnv->dblayer_DB_ENV, 0);
    dblayer_free_env(&priv->dblayer_env); /* pEnv is now garbage */

    if (0 == return_value
        && !((DBLAYER_ARCHIVE_MODE|DBLAYER_EXPORT_MODE) & dbmode)
        && !priv->dblayer_bad_stuff_happened) {
        commit_good_database(priv);
    }
    if (priv->dblayer_data_directories) {
        /* dblayer_data_directories are set in dblayer_make_env via
         * dblayer_start, which is paired with dblayer_close. */
        /* no need to release dblayer_home_directory,
         * which is one of dblayer_data_directories */
        charray_free(priv->dblayer_data_directories);
        priv->dblayer_data_directories = NULL;
    }
    return return_value;
}

/* 
 * This function is called when the server is shutting down.
 * This is not safe to call while other threads are calling into the open
 * databases !!!   So: DON'T !
 */
int dblayer_close(struct ldbminfo *li, int dbmode)
{
    backend *be = NULL;
    ldbm_instance *inst;
    Object *inst_obj;
    int return_value = 0;

    dblayer_pre_close(li);

    /* 
     * dblayer_close_indexes and pDB->close used to be located above loop:
     *   while(priv->dblayer_thread_count > 0) in pre_close.
     * This order fixes a bug: shutdown under the stress makes txn_checkpoint
     * (checkpoint_thread) fail b/c the mpool might have been already closed.
     */
    for (inst_obj = objset_first_obj(li->li_instance_set); inst_obj;
         inst_obj = objset_next_obj(li->li_instance_set, inst_obj)) {
        inst = (ldbm_instance *)object_get_data(inst_obj);
        be = inst->inst_be;
        if (NULL != be->be_instance_info) {
            return_value |= dblayer_instance_close(be);
        }
    }

    if (return_value != 0) {
        /* force recovery next startup if any close failed */
        dblayer_private *priv;
        PR_ASSERT(NULL != li);
        priv = (dblayer_private*)li->li_dblayer_private;
        PR_ASSERT(NULL != priv);
        priv->dblayer_bad_stuff_happened = 1;
    }

    return_value |= dblayer_post_close(li, dbmode);

    return return_value;
}

/* 
 * Called to tell us to flush any data not on disk to the disk
 * for the transacted database, we interpret this as an instruction
 * to write a checkpoint.
 */
int
dblayer_flush(struct ldbminfo *li)
{
    return 0;
}

/* API to remove the environment */
int
dblayer_remove_env(struct ldbminfo *li)
{
    DB_ENV *env = NULL;
    dblayer_private *priv = NULL;
    char *home_dir = NULL;
    int rc = db_env_create(&env, 0);
    if (rc) {
        LDAPDebug1Arg(LDAP_DEBUG_ANY,
                      "ERROR -- Failed to create DB_ENV (returned: %d)\n", rc);
        return rc;
    }
    if (NULL == li) {
        LDAPDebug0Args(LDAP_DEBUG_ANY, "ERROR -- No ldbm info is given\n");
        return -1;
    }
    priv = (dblayer_private *)li->li_dblayer_private;

    home_dir = dblayer_get_home_dir(li, NULL);
    if (home_dir) {
        rc = env->remove(env, home_dir, 0);
        if (rc) {
            LDAPDebug1Arg(LDAP_DEBUG_ANY,
                          "ERROR -- Failed to remove DB environment files. "
                          "Please remove %s/__db.00# (# is 1 through 6)\n",
                          home_dir);
        }
    }
    return rc;
}

#if !defined(DB_DUPSORT)
#define DB_DUPSORT 0
#endif

static int
_dblayer_set_db_callbacks(dblayer_private *priv, DB *dbp, struct attrinfo *ai)
{
    int rc = 0;

    /* With the new idl design, the large 8Kbyte pages we use are not
       optimal. The page pool churns very quickly as we add new IDs under a
       sustained add load. Smaller pages stop this happening so much and
       consequently make us spend less time flushing dirty pages on checkpoints.
       But 8K is still a good page size for id2entry. So we now allow different
       page sizes for the primary and secondary indices. 
       Filed as bug: 604654
     */
    if (idl_get_idl_new()) {
        rc = dbp->set_pagesize(
                        dbp,
                        (priv->dblayer_index_page_size == 0) ?
                        DBLAYER_INDEX_PAGESIZE : priv->dblayer_index_page_size);
    } else {
        rc = dbp->set_pagesize(
                        dbp,
                        (priv->dblayer_page_size == 0) ?
                        DBLAYER_PAGESIZE : priv->dblayer_page_size);
    }
    if (rc)
        return rc;

#if 1000*DB_VERSION_MAJOR + 100*DB_VERSION_MINOR < 3300
    rc = dbp->set_malloc(dbp, (void *)slapi_ch_malloc);
    if (rc)
        return rc;
#endif

    if (idl_get_idl_new() && !(ai->ai_indexmask & INDEX_VLV)) {
        rc = dbp->set_flags(dbp, DB_DUP | DB_DUPSORT);
        if (rc)
            return rc;

        if (ai->ai_dup_cmp_fn) {
            /* If set, use the special dup compare callback */
            rc = dbp->set_dup_compare(dbp, ai->ai_dup_cmp_fn);
        } else {
            rc = dbp->set_dup_compare(dbp, idl_new_compare_dups);
        }
        if (rc)
            return rc;
    }

    if (ai->ai_indexmask & INDEX_VLV) {
        /*
         * Need index with record numbers for
         * Virtual List View index
         */
        rc = dbp->set_flags(dbp, DB_RECNUM);
        if (rc)
            return rc;
    } else if (ai->ai_key_cmp_fn) { /* set in attr_index_config() */
        /*
          This is so that we can have ordered keys in the index, so that
          greater than/less than searches work on indexed attrs.  We had
          to introduce this when we changed the integer key format from
          a 32/64 bit value to a normalized string value.  The default
          bdb key cmp is based on length and lexicographic order, which
          does not work with integer strings.

          NOTE: If we ever need to use app_private for something else, we
          will have to create some sort of data structure with different
          fields for different uses.  We will also need to have a new()
          function that creates and allocates that structure, and a
          destroy() function that destroys the structure, and make sure
          to call it when the DB* is closed and/or freed.
        */
        dbp->app_private = (void *)ai->ai_key_cmp_fn;
        dbp->set_bt_compare(dbp, dblayer_bt_compare);
    }
    return rc;
}

/* Routines for opening and closing random files in the DB_ENV.
   Used by ldif2db merging code currently.

   Return value: 
       Success: 0
    Failure: -1
 */
int
dblayer_open_file(backend *be, char* indexname, int open_flag, 
                  struct attrinfo *ai, DB **ppDB)
{
    struct ldbminfo *li = (struct ldbminfo *) be->be_database->plg_private;
    ldbm_instance *inst = (ldbm_instance *) be->be_instance_info;
    int open_flags = 0;
    char *file_name = NULL;
    char *rel_path = NULL;
    dblayer_private_env *pENV = 0;
    dblayer_private *priv = NULL;
    int return_value = 0;
    DB *dbp = NULL;
    char *subname = NULL;

    PR_ASSERT(NULL != li);
    priv = (dblayer_private*)li->li_dblayer_private;
    PR_ASSERT(NULL != priv);

    if (NULL == inst->inst_dir_name)
    {
        if (dblayer_get_instance_data_dir(be) != 0)
            return -1;
    }

    if (NULL != inst->inst_parent_dir_name)
    {
        if (!charray_utf8_inlist(priv->dblayer_data_directories,
                                 inst->inst_parent_dir_name) &&
            !is_fullpath(inst->inst_dir_name))

        {
            LDAPDebug(LDAP_DEBUG_ANY, 
                "The instance path %s is not registered for db_data_dir, "
                "although %s is a relative path.\n",
                inst->inst_parent_dir_name, inst->inst_dir_name, 0);
            return -1;
        }
    }

    pENV = priv->dblayer_env;
    if (inst->import_env)
        pENV = inst->import_env;

    PR_ASSERT(NULL != pENV);
    file_name = slapi_ch_smprintf("%s%s", indexname, LDBM_FILENAME_SUFFIX);
    rel_path = slapi_ch_smprintf("%s/%s", inst->inst_dir_name, file_name);

    open_flags = DB_THREAD;
    if (open_flag & DBOPEN_CREATE)
        open_flags |= DB_CREATE;
    if (open_flag & DBOPEN_TRUNCATE)
        open_flags |= DB_TRUNCATE;

    if (!ppDB)
        goto out;
    return_value = db_create(ppDB, pENV->dblayer_DB_ENV, 0);
    if (0 != return_value)
        goto out;

    dbp = *ppDB;
    return_value = _dblayer_set_db_callbacks(priv, dbp, ai);
    if (return_value)
        goto out;

    /* The subname argument allows applications to have
     * subdatabases, i.e., multiple databases inside of a single
     * physical file. This is useful when the logical databases
     * are both numerous and reasonably small, in order to
     * avoid creating a large number of underlying files.
     */
    /* If inst_parent_dir_name is not the primary DB dir &&
     * the index file does not exist */
    if ((charray_get_index(priv->dblayer_data_directories,
                           inst->inst_parent_dir_name) > 0) &&
        !dblayer_inst_exists(inst, file_name))
    {
        char inst_dir[MAXPATHLEN];
        char *inst_dirp = NULL;
        char *abs_file_name = NULL;
        /* create a file with abs path, then try again */

        inst_dirp = dblayer_get_full_inst_dir(li, inst, inst_dir, MAXPATHLEN);
        if (!inst_dirp || !*inst_dirp)
        {
            return_value = -1;
            goto out;
        }
        abs_file_name = slapi_ch_smprintf("%s%c%s",
                inst_dirp, get_sep(inst_dirp), file_name);
        DB_OPEN(pENV->dblayer_openflags,
                dbp, NULL/* txnid */, abs_file_name, subname, DB_BTREE,
                open_flags, priv->dblayer_file_mode, return_value);
        dbp->close(dbp, 0);
        return_value = db_create(ppDB, pENV->dblayer_DB_ENV, 0);
        if (0 != return_value)
        {
            goto out;
        }
        dbp = *ppDB;
        return_value = _dblayer_set_db_callbacks(priv, dbp, ai);
        if (return_value)
            goto out;

        slapi_ch_free_string(&abs_file_name);
        if (inst_dirp != inst_dir)
            slapi_ch_free_string(&inst_dirp);
    }
    DB_OPEN(pENV->dblayer_openflags,
            dbp, NULL, /* txnid */ rel_path, subname, DB_BTREE,
            open_flags, priv->dblayer_file_mode, return_value);
#if 1000*DB_VERSION_MAJOR + 100*DB_VERSION_MINOR == 4100
    /* lower the buffer cache priority to avoid sleep in memp_alloc */
    /* W/ DB_PRIORITY_LOW, the db buffer page priority is calculated as:
     *     priority = lru_count + pages / (-1)
     * (by default, priority = lru_count)
     * When upgraded to db4.2, this setting may not needed, hopefully.
     * ask sleepycat [#8301]; blackflag #619964
     */
    dbp->set_cache_priority(dbp, DB_PRIORITY_LOW);
#endif
out:
    slapi_ch_free((void**)&file_name);
    slapi_ch_free((void**)&rel_path);
    /* close the database handle to avoid handle leak */
    if (dbp && (return_value != 0)) {
        dblayer_close_file(dbp);
    }
    return return_value;
}

int dblayer_close_file(DB *db)
{
    return db->close(db,0);
}

/*
 * OK, this is a tricky one. We store open DB* handles within an AVL
 * structure used in other parts of the back-end. This is nasty, because
 * that code has no idea we're doing this, and we don't have much control
 * over what it does. But, the reason is that we want to get fast lookup
 * of the index file pertaining to each particular attribute. Putting the
 * DB* handles in the attribute info structures is an easy way to achieve this
 * because we already lookup this structure as part of an index lookup.
 */

/* 
 * This function takes an attrinfo structure and returns a valid
 * DB* handle for the index file corresponding to this attribute.
 */

/*
 * If the db library is non-reentrant, we lock the mutex here,
 * see comments above for id2entry for the details on this.
 */

/*
 * The create flag determines if the index file should be created if it 
 * does not already exist.
 */

int dblayer_get_index_file(backend *be, struct attrinfo *a, DB** ppDB, int open_flags)
{
  /* 
   * We either already have a DB* handle in the attrinfo structure.
   * in which case we simply return it to the caller, OR:
   * we need to make one. We do this as follows: 
   * 1a) acquire the mutex that protects the handle list.
   * 1b) check that the DB* is still null.
   * 2) get the filename, and call libdb to open it
   * 3) if successful, store the result in the attrinfo stucture
   * 4) store the DB* in our own list so we can close it later.
   * 5) release the mutex.
   */
  ldbm_instance *inst = (ldbm_instance *) be->be_instance_info;
  int return_value = -1;
  DB *pDB = NULL;
  char *attribute_name = a->ai_type;
  
  *ppDB = NULL;
  
  /* it's like a semaphore -- when count > 0, any file handle that's in
   * the attrinfo will remain valid from here on.
   */
  PR_AtomicIncrement(&a->ai_dblayer_count);
  
  if (NULL != a->ai_dblayer) {
    /* This means that the pointer is valid, so we should return it. */
    *ppDB = ((dblayer_handle*)(a->ai_dblayer))->dblayer_dbp;
    return 0;
  }
  
  /* attrinfo handle is NULL, at least for now -- grab the mutex and try
   * again.
   */
  PR_Lock(inst->inst_handle_list_mutex);
  if (NULL != a->ai_dblayer) {
    /* another thread set the handle while we were waiting on the lock */
    *ppDB = ((dblayer_handle*)(a->ai_dblayer))->dblayer_dbp;
    PR_Unlock(inst->inst_handle_list_mutex);
    return 0;
  }
  
  /* attrinfo handle is still blank, and we have the mutex: open the
   * index file and stuff it in the attrinfo.
   */
  return_value = dblayer_open_file(be, attribute_name, open_flags,
                                   a, &pDB);
  if (0 == return_value) {
      /* Opened it OK */
      dblayer_handle *handle = (dblayer_handle *) 
        slapi_ch_calloc(1, sizeof(dblayer_handle));
      
      if (NULL == handle) {
        /* Memory allocation failed */
        return_value = -1;
      } else {
        dblayer_handle *prev_handle = inst->inst_handle_tail;
        
        PR_ASSERT(NULL != pDB);
        /* Store the returned DB* in our own private list of
         * open files */
        if (NULL == prev_handle) {
          /* List was empty */
          inst->inst_handle_tail = handle;
          inst->inst_handle_head = handle;
        } else {
          /* Chain the handle onto the last structure in the
           * list */
          inst->inst_handle_tail = handle;
          prev_handle->dblayer_handle_next = handle;
        }
        /* Stash a pointer to our wrapper structure in the
         * attrinfo structure */
        handle->dblayer_dbp = pDB;
        /* And, most importantly, return something to the caller!*/
        *ppDB = pDB;
        /* and save the hande in the attrinfo structure for
         * next time */
        a->ai_dblayer = handle;
        /* don't need to update count -- we incr'd it already */
        handle->dblayer_handle_ai_backpointer = &(a->ai_dblayer);
      }
  } else {
    /* Did not open it OK ! */
    /* Do nothing, because return value and fact that we didn't
     * store a DB* in the attrinfo is enough 
     */
  }
  PR_Unlock(inst->inst_handle_list_mutex);
  
  if (return_value != 0) {
    /* some sort of error -- we didn't open a handle at all.
     * decrement the refcount back to where it was.
     */
    PR_AtomicDecrement(&a->ai_dblayer_count);
  }
  
  return return_value;
}

/*
 * Unlock the db lib mutex here if we need to.
 */
int dblayer_release_index_file(backend *be,struct attrinfo *a, DB* pDB)
{
    PR_AtomicDecrement(&a->ai_dblayer_count);
    return 0;
}

/*
  dblayer_db_remove assumptions:
  
  No environment has the given database open.
  
*/

static int
dblayer_db_remove_ex(dblayer_private_env *env, char const path[], char const dbName[], PRBool use_lock) {
  DB_ENV * db_env = 0;
  int rc;
  DB *db;
  
  if (env) {
    if(use_lock) PR_RWLock_Wlock(env->dblayer_env_lock); /* We will be causing logging activity */
    db_env = env->dblayer_DB_ENV;
  }
  
  rc = db_create(&db, db_env, 0); /* must use new handle to database */
  if (0 != rc) {
    LDAPDebug(LDAP_DEBUG_ANY, "db_remove: Failed to create db (%d) %s\n",
                  rc, dblayer_strerror(rc), 0);
    goto done;
  }
  rc = db->remove(db, path, dbName, 0); /* kiss the db goodbye! */

done:
  if (env) {
    if(use_lock) PR_RWLock_Unlock(env->dblayer_env_lock);
  }
  
  return rc;
}


int
dblayer_db_remove(dblayer_private_env * env, char const path[], char const dbName[]) {
    return(dblayer_db_remove_ex(env,path,dbName,PR_TRUE));
}

#define DBLAYER_CACHE_DELAY     PR_MillisecondsToInterval(250)
int dblayer_erase_index_file_ex(backend *be, struct attrinfo *a,
                                PRBool use_lock, int no_force_checkpoint)
{
  struct ldbminfo *li = (struct ldbminfo *) be->be_database->plg_private;
  dblayer_private *priv = (dblayer_private*) li->li_dblayer_private;
  struct dblayer_private_env *pEnv = priv->dblayer_env;
  ldbm_instance *inst = (ldbm_instance *) be->be_instance_info;
  dblayer_handle *handle = NULL;
  char dbName[MAXPATHLEN];
  char *dbNamep;
  char *p;
  int dbbasenamelen, dbnamelen;
  int rc = 0;
  DB *db = 0;

  if (NULL == pEnv)        /* db does not exist */
    return rc;
 
  /* Added for bug 600401. Somehow the checkpoint thread deadlocked on
     index file with this function, index file couldn't be removed on win2k.
     Force a checkpoint here to break deadlock.
  */
  if (0 == no_force_checkpoint) {
    dblayer_force_checkpoint(li);
  }

  if (0 == dblayer_get_index_file(be, a, &db, 0 /* Don't create an index file
                                                   if it does not exist. */)) {
    /* first, remove the file handle for this index, if we have it open */
    PR_Lock(inst->inst_handle_list_mutex);
    if (a->ai_dblayer) {
      /* there is a handle */
      handle = (dblayer_handle *)a->ai_dblayer;
      
      /* when we successfully called dblayer_get_index_file we bumped up
         the reference count of how many threads are using the index. So we
         must manually back off the count by one here.... rwagner */
      
      dblayer_release_index_file(be, a, db);
      
      while (a->ai_dblayer_count > 0) {
        /* someone is using this index file */
        /* ASSUMPTION: you have already set the INDEX_OFFLINE flag, because
         * you intend to mess with this index.  therefore no new requests
         * for this indexfile should happen, so the dblayer_count should
         * NEVER increase.
         */
        PR_ASSERT(a->ai_indexmask & INDEX_OFFLINE);
        PR_Unlock(inst->inst_handle_list_mutex);
        DS_Sleep(DBLAYER_CACHE_DELAY);
        PR_Lock(inst->inst_handle_list_mutex);
      }
      dblayer_close_file(handle->dblayer_dbp);
      
      /* remove handle from handle-list */
      if (inst->inst_handle_head == handle) {
        inst->inst_handle_head = handle->dblayer_handle_next;
        if (inst->inst_handle_tail == handle) {
          inst->inst_handle_tail = NULL;
        }
      } else {
        dblayer_handle *hp;
        
        for (hp = inst->inst_handle_head; hp; hp = hp->dblayer_handle_next) {
          if (hp->dblayer_handle_next == handle) {
            hp->dblayer_handle_next = handle->dblayer_handle_next;
            if (inst->inst_handle_tail == handle) {
              inst->inst_handle_tail = hp;
            }
            break;
          }
        }
      }
      dbNamep = dblayer_get_full_inst_dir(li, inst, dbName, MAXPATHLEN);
      if (dbNamep && *dbNamep) {
        dbbasenamelen = strlen(dbNamep);
        dbnamelen = dbbasenamelen + strlen(a->ai_type) + 6;
        if (dbnamelen > MAXPATHLEN)
        {
          dbNamep = (char *)slapi_ch_realloc(dbNamep, dbnamelen);
        }
        p = dbNamep + dbbasenamelen;
        sprintf(p, "%c%s%s",
                   get_sep(dbNamep), a->ai_type, LDBM_FILENAME_SUFFIX);
        rc = dblayer_db_remove_ex(pEnv, dbNamep, 0, use_lock);
        a->ai_dblayer = NULL;
        if (dbNamep != dbName)
          slapi_ch_free_string(&dbNamep);
      }
      else
      {
        rc = -1;
      }
      slapi_ch_free((void **)&handle);
    } else {
      /* no handle to close */
    }
    PR_Unlock(inst->inst_handle_list_mutex);

  }
  
  return rc;
}

int dblayer_erase_index_file_nolock(backend *be, struct attrinfo *a, int no_force_chkpt) {
    return dblayer_erase_index_file_ex(be,a,PR_FALSE,no_force_chkpt);
}

int dblayer_erase_index_file(backend *be, struct attrinfo *a, int no_force_chkpt) {
    return dblayer_erase_index_file_ex(be,a,PR_TRUE,no_force_chkpt);
}


/*
 * Transaction stuff. The idea is that the caller doesn't need to 
 * know the transaction mechanism underneath (because the caller is
 * typically a few calls up the stack from any DB stuff).
 * Sadly, in slapd there was no handy structure associated with
 * an LDAP operation, and passed around evberywhere, so we had
 * to invent the back_txn structure.
 * The lower levels of the back-end look into this structure, and
 * take out the DB_TXN they need.
 */
int dblayer_txn_init(struct ldbminfo *li, back_txn *txn)
{
    PR_ASSERT(NULL != txn);

    txn->back_txn_txn = NULL;
    return 0;
}


int dblayer_txn_begin_ext(struct ldbminfo *li, back_txnid parent_txn, back_txn *txn, PRBool use_lock)
{
    int return_value = -1;
    dblayer_private *priv = NULL;
    PR_ASSERT(NULL != txn);
    PR_ASSERT(NULL != li);
    /*
     * When server is shutting down, some components need to
     * flush some data (e.g. replication to write ruv).
     * So don't check shutdown signal unless we can't write.
     */
    if ( g_get_shutdown() == SLAPI_SHUTDOWN_DISKFULL ) {
        return return_value;
    }

    priv = (dblayer_private*)li->li_dblayer_private;
    PR_ASSERT(NULL != priv);

    if (priv->dblayer_enable_transactions)
    {
        dblayer_private_env *pEnv = priv->dblayer_env;
        if(use_lock) PR_RWLock_Rlock(pEnv->dblayer_env_lock);
        return_value = TXN_BEGIN(pEnv->dblayer_DB_ENV,
                                 (DB_TXN*)parent_txn,
                                 &txn->back_txn_txn,
                                 0);
        if (0 != return_value) 
        {
            if(use_lock) PR_RWLock_Unlock(priv->dblayer_env->dblayer_env_lock);
            txn->back_txn_txn = NULL;
        }
    } else
    {
        return_value = 0;
    }
    if (0 != return_value) 
    {
        LDAPDebug(LDAP_DEBUG_ANY,
                  "Serious Error---Failed in dblayer_txn_begin, err=%d (%s)\n",
                  return_value, dblayer_strerror(return_value), 0);
    }
    return return_value;
}

int dblayer_read_txn_begin(struct ldbminfo *li, back_txnid parent_txn, back_txn *txn) {
  return (dblayer_txn_begin_ext(li,parent_txn,txn,PR_FALSE));
}

int dblayer_txn_begin(struct ldbminfo *li, back_txnid parent_txn, back_txn *txn) {
  return (dblayer_txn_begin_ext(li,parent_txn,txn,PR_TRUE));
}


int dblayer_txn_commit_ext(struct ldbminfo *li, back_txn *txn, PRBool use_lock)
{
    int return_value = -1;
    dblayer_private *priv = NULL;
    DB_TXN *db_txn;

    PR_ASSERT(NULL != txn);
    PR_ASSERT(NULL != li);

    priv = (dblayer_private*)li->li_dblayer_private;
    PR_ASSERT(NULL != priv);

    db_txn = txn->back_txn_txn;
    if (NULL != db_txn &&
        1 != priv->dblayer_stop_threads &&
        priv->dblayer_env &&
        priv->dblayer_enable_transactions)
    {
        return_value = TXN_COMMIT(db_txn, 0);
        if ((priv->dblayer_durable_transactions) && use_lock ) {
            if(trans_batch_limit > 0) {        
                if(trans_batch_count % trans_batch_limit) {
                    trans_batch_count++;    
                } else {
                    LOG_FLUSH(priv->dblayer_env->dblayer_DB_ENV,0);
                    trans_batch_count=1;
                }        
            } else if(trans_batch_limit == FLUSH_REMOTEOFF) { /* user remotely turned batching off */
                LOG_FLUSH(priv->dblayer_env->dblayer_DB_ENV,0);
            }
        }
        if(use_lock) PR_RWLock_Unlock(priv->dblayer_env->dblayer_env_lock);
    } else
    {
        return_value = 0;
    }

    if (0 != return_value) 
    {
        LDAPDebug(LDAP_DEBUG_ANY,
                  "Serious Error---Failed in dblayer_txn_commit, err=%d (%s)\n",
                  return_value, dblayer_strerror(return_value), 0);
        if (LDBM_OS_ERR_IS_DISKFULL(return_value)) {
            operation_out_of_disk_space();
        }
    }
    return return_value;
}

int dblayer_read_txn_commit(struct ldbminfo *li, back_txn *txn) {
  return(dblayer_txn_commit_ext(li,txn,PR_FALSE));
}

int dblayer_txn_commit(struct ldbminfo *li, back_txn *txn) {
  return(dblayer_txn_commit_ext(li,txn,PR_TRUE));
}

int dblayer_txn_abort_ext(struct ldbminfo *li, back_txn *txn, PRBool use_lock)
{
    int return_value = -1;
    dblayer_private *priv = NULL;
    DB_TXN *db_txn;

    PR_ASSERT(NULL != txn);
    PR_ASSERT(NULL != li);

    priv = (dblayer_private*)li->li_dblayer_private;
    PR_ASSERT(NULL != priv);

    db_txn = txn->back_txn_txn;
    if (NULL != db_txn &&
        priv->dblayer_env &&
        priv->dblayer_enable_transactions)
    {
        return_value = TXN_ABORT(db_txn);
        if(use_lock) PR_RWLock_Unlock(priv->dblayer_env->dblayer_env_lock);
    } else
    {
        return_value = 0;
    }    

    if (0 != return_value) 
    {
        LDAPDebug(LDAP_DEBUG_ANY,
                  "Serious Error---Failed in dblayer_txn_abort, err=%d (%s)\n",
                  return_value, dblayer_strerror(return_value), 0);
        if (LDBM_OS_ERR_IS_DISKFULL(return_value)) {
            operation_out_of_disk_space();
        }
    }
    return return_value;                                         
}

int dblayer_read_txn_abort(struct ldbminfo *li, back_txn *txn){
  return(dblayer_txn_abort_ext(li,txn,PR_FALSE));
}

int dblayer_txn_abort(struct ldbminfo *li, back_txn *txn){
  return(dblayer_txn_abort_ext(li,txn,PR_TRUE));
}


size_t dblayer_get_optimal_block_size(struct ldbminfo *li)
{
    dblayer_private *priv = NULL;
    size_t page_size = 0;

    PR_ASSERT(NULL != li);
    
    priv = (dblayer_private*)li->li_dblayer_private;
    PR_ASSERT(NULL != priv);

    page_size = (priv->dblayer_page_size == 0) ? DBLAYER_PAGESIZE : priv->dblayer_page_size; 
    if (priv->dblayer_idl_divisor == 0) 
    {
        return page_size - DB_EXTN_PAGE_HEADER_SIZE;
    } else 
    {
        return page_size / priv->dblayer_idl_divisor;
    }
}


/* 
 * The dblock serializes writes to the database,
 * which reduces deadlocking in the db code,
 * which means that we run faster.
 */
void dblayer_lock_backend(backend *be)
{
    ldbm_instance *inst;

    PR_ASSERT(NULL != be);
    inst = (ldbm_instance *) be->be_instance_info;
    PR_ASSERT(NULL != inst);
    
    if (NULL != inst->inst_db_mutex) {
        PR_Lock(inst->inst_db_mutex);
    }
}

void dblayer_unlock_backend(backend *be)
{
    ldbm_instance *inst;

    PR_ASSERT(NULL != be);
    inst = (ldbm_instance *) be->be_instance_info;
    PR_ASSERT(NULL != inst);
    
    if (NULL != inst->inst_db_mutex) {
        PR_Unlock(inst->inst_db_mutex);
    }
}


/* code which implements checkpointing and log file truncation */

/*
 * create a thread for perf_threadmain
 */
static int
dblayer_start_perf_thread(struct ldbminfo *li)
{
    int return_value = 0;
    if (NULL == PR_CreateThread (PR_USER_THREAD,
                                 (VFP) (void *) perf_threadmain, li,
                                 PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
                                 PR_UNJOINABLE_THREAD, 
                                 SLAPD_DEFAULT_THREAD_STACKSIZE) )
    {
        PRErrorCode prerr = PR_GetError();
        LDAPDebug(LDAP_DEBUG_ANY, "failed to create database perf thread, "
                  SLAPI_COMPONENT_NAME_NSPR " error %d (%s)\n",
                  prerr, slapd_pr_strerror(prerr), 0);
        return_value = -1;
    }
    return return_value;
}

/* Performance thread */
static int perf_threadmain(void *param)
{
    dblayer_private *priv = NULL;
    struct ldbminfo *li = NULL;

    PR_ASSERT(NULL != param);
    li = (struct ldbminfo*)param;

    priv = (dblayer_private*)li->li_dblayer_private;
    PR_ASSERT(NULL != priv);

    INCR_THREAD_COUNT(priv);

    while (!priv->dblayer_stop_threads) {
        /* sleep for a while, updating perf counters if we need to */
        perfctrs_wait(1000,priv->perf_private,priv->dblayer_env->dblayer_DB_ENV);
    }

    DECR_THREAD_COUNT(priv);
    LDAPDebug(LDAP_DEBUG_TRACE, "Leaving perf_threadmain\n", 0, 0, 0);
    return 0;
}

/*
 * create a thread for deadlock_threadmain
 */
static int
dblayer_start_deadlock_thread(struct ldbminfo *li)
{
    int return_value = 0;
    if (NULL == PR_CreateThread (PR_USER_THREAD,
                                 (VFP) (void *) deadlock_threadmain, li,
                                 PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
                                 PR_UNJOINABLE_THREAD,
                                 SLAPD_DEFAULT_THREAD_STACKSIZE) )
    {
        PRErrorCode prerr = PR_GetError();
        LDAPDebug(LDAP_DEBUG_ANY, "failed to create database deadlock thread, "
                  SLAPI_COMPONENT_NAME_NSPR " error %d (%s)\n",
                  prerr, slapd_pr_strerror(prerr), 0);
        return_value = -1;
    }
    return return_value;
}

/* deadlock thread main function */

static int deadlock_threadmain(void *param)
{
    int rval = -1;
    dblayer_private *priv = NULL;
    struct ldbminfo *li = NULL;
    PRIntervalTime    interval;   /*NSPR timeout stuffy*/

    PR_ASSERT(NULL != param);
    li = (struct ldbminfo*)param;

    priv = (dblayer_private*)li->li_dblayer_private;
    PR_ASSERT(NULL != priv);

    INCR_THREAD_COUNT(priv);

    interval = PR_MillisecondsToInterval(100);
    while (!priv->dblayer_stop_threads)
    {
        if (priv->dblayer_enable_transactions) 
        {
            if (dblayer_db_uses_locking(priv->dblayer_env->dblayer_DB_ENV)) {
                int aborted;
                if ((rval = LOCK_DETECT(priv->dblayer_env->dblayer_DB_ENV,
                            0,
                            DB_LOCK_YOUNGEST,
                            &aborted))
                    != 0) {
                    LDAPDebug(LDAP_DEBUG_ANY,
                      "Serious Error---Failed in deadlock detect (aborted at 0x%x), err=%d (%s)\n",
                      aborted, rval, dblayer_strerror(rval));
                }
            }
        }
        DS_Sleep(interval);
    }

    DECR_THREAD_COUNT(priv);
    LDAPDebug(LDAP_DEBUG_TRACE, "Leaving deadlock_threadmain\n", 0, 0, 0);
    return 0;
}

#define checkpoint_debug_message(debug, fmt, a1, a2, a3) \
    if (debug) { LDAPDebug(LDAP_DEBUG_ANY,fmt,a1,a2,a3); }

/* this thread tries to do two things: 
    1. catch a group of transactions that are pending allowing a worker thread 
       to work
    2. flush any left over transactions ( a single transaction for example)
*/

static int
dblayer_start_log_flush_thread(dblayer_private *priv)
{
    int return_value = 0;

    if ((priv->dblayer_durable_transactions) && 
        (priv->dblayer_enable_transactions) && (trans_batch_limit > 0)) {
        log_flush_thread=PR_TRUE;
        if (NULL == PR_CreateThread (PR_USER_THREAD,
                                     (VFP) (void *) log_flush_threadmain, priv,
                                     PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
                                     PR_UNJOINABLE_THREAD, 
                                     SLAPD_DEFAULT_THREAD_STACKSIZE) ) {
            PRErrorCode prerr = PR_GetError();
            LDAPDebug(LDAP_DEBUG_ANY,
                "failed to create database log flush thread, "
                SLAPI_COMPONENT_NAME_NSPR " error %d (%s)\n",
                prerr, slapd_pr_strerror(prerr), 0);
            return_value = -1;
        }
    }
    return return_value;
}

/* this thread tries to do two things: 
    1. catch a group of transactions that are pending allowing a worker thread 
       to work
    2. flush any left over transactions ( a single transaction for example)
*/

static int log_flush_threadmain(void *param)
{
    dblayer_private *priv = NULL;
    PRIntervalTime    interval;

    PR_ASSERT(NULL != param);
    priv = (dblayer_private *) param;

    INCR_THREAD_COUNT(priv);

    interval = PR_MillisecondsToInterval(300);
    while ((!priv->dblayer_stop_threads) && (log_flush_thread))
    {      
        if (priv->dblayer_enable_transactions) 
        {
          DB_CHECKPOINT_LOCK(1, priv->dblayer_env->dblayer_env_lock);
          if(trans_batch_limit > 0) {
            if(trans_batch_count > 1) {
              LOG_FLUSH(priv->dblayer_env->dblayer_DB_ENV,0);
              trans_batch_count=1;
            }
          }
          DB_CHECKPOINT_UNLOCK(1, priv->dblayer_env->dblayer_env_lock);
        }
        DS_Sleep(interval);
    }

    DECR_THREAD_COUNT(priv);
    LDAPDebug(LDAP_DEBUG_TRACE, "Leaving log_flush_threadmain\n", 0, 0, 0);
    return 0;
}

/*
 * create a thread for checkpoint_threadmain
 */
static int
dblayer_start_checkpoint_thread(struct ldbminfo *li)
{
    int return_value = 0;
    if (NULL == PR_CreateThread (PR_USER_THREAD,
                                 (VFP) (void *) checkpoint_threadmain, li,
                                 PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
                                 PR_UNJOINABLE_THREAD, 
                                 SLAPD_DEFAULT_THREAD_STACKSIZE) )
    {
        PRErrorCode prerr = PR_GetError();
        LDAPDebug(LDAP_DEBUG_ANY,
                  "failed to create database checkpoint thread, "
                  SLAPI_COMPONENT_NAME_NSPR " error %d (%s)\n",
                  prerr, slapd_pr_strerror(prerr), 0);
        return_value = -1;
    }
    return return_value;
}

static int checkpoint_threadmain(void *param)
{
    time_t time_of_last_checkpoint_completion = 0;    /* seconds since epoch */
    PRIntervalTime    interval;
    int rval = -1;
    dblayer_private *priv = NULL;
    struct ldbminfo *li = NULL;
    int debug_checkpointing = 0;
    int checkpoint_interval;
    char *home_dir = NULL;
    char **list = NULL;
    char **listp = NULL;
    struct dblayer_private_env *penv = NULL;

    PR_ASSERT(NULL != param);
    li = (struct ldbminfo*)param;

    priv = (dblayer_private*)li->li_dblayer_private;
    PR_ASSERT(NULL != priv);

    INCR_THREAD_COUNT(priv);

    interval = PR_MillisecondsToInterval(DBLAYER_SLEEP_INTERVAL);
    home_dir = dblayer_get_home_dir(li, NULL);
    if (NULL == home_dir || '\0' == *home_dir)
    {
        LDAPDebug(LDAP_DEBUG_ANY, 
            "Checkpoint thread failed due to missing db home directory info\n",
            0, 0, 0);
        goto error_return;
    }

    /* work around a problem with newly created environments */
    dblayer_force_checkpoint(li);

    penv = priv->dblayer_env;
    debug_checkpointing = priv->db_debug_checkpointing;
    /* assumes dblayer_force_checkpoint worked */
    time_of_last_checkpoint_completion = current_time();
    while (!priv->dblayer_stop_threads)
    {
        /* sleep for a while */
        /* why aren't we sleeping exactly the right amount of time ? */
        /* answer---because the interval might be changed after the server 
         * starts up */
        DS_Sleep(interval);

        if (0 == priv->dblayer_enable_transactions) 
            continue;

        PR_Lock(li->li_config_mutex);
        checkpoint_interval = priv->dblayer_checkpoint_interval;
        PR_Unlock(li->li_config_mutex);

        /* Check to see if the checkpoint interval has elapsed */
        if (current_time() - time_of_last_checkpoint_completion <
                                               checkpoint_interval) 
            continue;

        if (!dblayer_db_uses_transactions(priv->dblayer_env->dblayer_DB_ENV))
            continue;

        /* now checkpoint */
        checkpoint_debug_message(debug_checkpointing,
                                 "Starting checkpoint\n", 0, 0, 0);
        rval = dblayer_txn_checkpoint(li, priv->dblayer_env, PR_TRUE, PR_TRUE);
#if 1000*DB_VERSION_MAJOR + 100*DB_VERSION_MINOR < 4100
        if (DB_INCOMPLETE == rval) 
        {
            checkpoint_debug_message(debug_checkpointing,
                                     "Retrying checkpoint\n", 0, 0, 0);
        } else
#endif
        {
            checkpoint_debug_message(debug_checkpointing,
                                     "Checkpoint Done\n", 0, 0, 0);
            if (rval != 0) {
                /* bad error */
                LDAPDebug(LDAP_DEBUG_ANY,
                    "Serious Error---Failed to checkpoint database, "
                    "err=%d (%s)\n", rval, dblayer_strerror(rval), 0);
                if (LDBM_OS_ERR_IS_DISKFULL(rval)) {
                    operation_out_of_disk_space();
                    goto error_return;
                }
            } else {
                time_of_last_checkpoint_completion = current_time();
            }
        }

        checkpoint_debug_message(debug_checkpointing,
                                 "Starting checkpoint\n", 0, 0, 0);
        rval = dblayer_txn_checkpoint(li, priv->dblayer_env, PR_TRUE, PR_TRUE);
#if 1000*DB_VERSION_MAJOR + 100*DB_VERSION_MINOR < 4100
        if (DB_INCOMPLETE == rval) 
        {
            checkpoint_debug_message(debug_checkpointing,
                                     "Retrying checkpoint\n", 0, 0, 0);
        } else
#endif
        {
            checkpoint_debug_message(debug_checkpointing,
                                     "Checkpoint Done\n", 0, 0, 0);
            if (rval != 0) {
                /* bad error */
                LDAPDebug(LDAP_DEBUG_ANY,
                    "Serious Error---Failed to checkpoint database, "
                    "err=%d (%s)\n", rval, dblayer_strerror(rval), 0);
                if (LDBM_OS_ERR_IS_DISKFULL(rval)) {
                    operation_out_of_disk_space();
                    goto error_return;
                }
            } else {
                time_of_last_checkpoint_completion = current_time();
            }
        }
        /* find out which log files don't contain active txns */
        DB_CHECKPOINT_LOCK(PR_TRUE, penv->dblayer_env_lock);
        rval = LOG_ARCHIVE(penv->dblayer_DB_ENV, &list,
                           DB_ARCH_ABS, (void *)slapi_ch_malloc);
        DB_CHECKPOINT_UNLOCK(PR_TRUE, penv->dblayer_env_lock);
        if (rval) {
            LDAPDebug2Args(LDAP_DEBUG_ANY, "checkpoint_threadmain: "
                           "log archive failed - %s (%d)\n", 
                           dblayer_strerror(rval), rval);
        } else {
            for (listp = list; listp && *listp != NULL; ++listp) {
                if (priv->dblayer_circular_logging) {
                    checkpoint_debug_message(debug_checkpointing,
                                             "Deleting %s\n", *listp, 0, 0);
                    unlink(*listp);
                } else {
                    char new_filename[MAXPATHLEN];
                    PR_snprintf(new_filename, sizeof(new_filename),
                                "%s.old", *listp);
                    checkpoint_debug_message(debug_checkpointing,
                                "Renaming %s -> %s\n",*listp, new_filename, 0);
                    rename(*listp, new_filename);    
                }
            }
            slapi_ch_free((void**)&list);
            /* Note: references inside the returned memory need not be 
             * individually freed. */
        }
    }
    LDAPDebug0Args(LDAP_DEBUG_TRACE, "Check point before leaving\n");
    rval = dblayer_force_checkpoint(li);
error_return:

    DECR_THREAD_COUNT(priv);
    LDAPDebug0Args(LDAP_DEBUG_TRACE, "Leaving checkpoint_threadmain\n");
    return rval;
}

/*
 * create a thread for trickle_threadmain
 */
static int
dblayer_start_trickle_thread(struct ldbminfo *li)
{
    int return_value = 0;
    if (NULL == PR_CreateThread (PR_USER_THREAD,
                                 (VFP) (void *) trickle_threadmain, li,
                                 PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
                                 PR_UNJOINABLE_THREAD, 
                                 SLAPD_DEFAULT_THREAD_STACKSIZE) )
    {
        PRErrorCode prerr = PR_GetError();
        LDAPDebug(LDAP_DEBUG_ANY, "failed to create database trickle thread, "
                  SLAPI_COMPONENT_NAME_NSPR " error %d (%s)\n",
                  prerr, slapd_pr_strerror(prerr), 0);
        return_value = -1;
    }
    return return_value;
}

static int trickle_threadmain(void *param)
{
    PRIntervalTime    interval;   /*NSPR timeout stuffy*/
    int rval = -1;
    dblayer_private *priv = NULL;
    struct ldbminfo *li = NULL;
    int debug_checkpointing = 0;

    PR_ASSERT(NULL != param);
    li = (struct ldbminfo*)param;

    priv = (dblayer_private*)li->li_dblayer_private;
    PR_ASSERT(NULL != priv);

    INCR_THREAD_COUNT(priv);

    interval = PR_MillisecondsToInterval(DBLAYER_SLEEP_INTERVAL);
    debug_checkpointing = priv->db_debug_checkpointing;
    while (!priv->dblayer_stop_threads)
    {
        DS_Sleep(interval);   /* 622855: wait for other threads fully started */
        if (priv->dblayer_enable_transactions) 
        {
            if ( dblayer_db_uses_mpool(priv->dblayer_env->dblayer_DB_ENV) &&
                 (0 != priv->dblayer_trickle_percentage) )
            {
                int pages_written = 0;
                if ((rval = MEMP_TRICKLE(
                                      priv->dblayer_env->dblayer_DB_ENV,
                                      priv->dblayer_trickle_percentage,
                                      &pages_written)) != 0)
                {
                    LDAPDebug(LDAP_DEBUG_ANY,"Serious Error---Failed to trickle, err=%d (%s)\n",rval,dblayer_strerror(rval), 0);
                }
                if (pages_written > 0) 
                {
                    checkpoint_debug_message(debug_checkpointing,"Trickle thread wrote %d pages\n",pages_written,0, 0);
                }
            }
        }
    }

    DECR_THREAD_COUNT(priv);
    LDAPDebug(LDAP_DEBUG_TRACE, "Leaving trickle_threadmain priv\n", 0, 0, 0);
    return 0;
}


/* better atol -- it understands a trailing multiplier k/m/g
 * for example, "32k" will be returned as 32768
 * richm: added better error checking and support for 64 bit values.
 * The err parameter is used by the caller to tell if there was an error
 * during the a to i conversion - if 0, the value was successfully
 * converted - if non-zero, there was some error (e.g. not a number)
 */
PRInt64 db_atol(char *str, int *err)
{
    PRInt64 mres1 = LL_INIT(0, 1);
    PRInt64 mres2 = LL_INIT(0, 1);
    PRInt64 mres3 = LL_INIT(0, 1);
    PRInt64 onek = LL_INIT(0, 1024);
    PRInt64 multiplier = LL_INIT(0, 1);
    PRInt64 val = LL_INIT(0, 0);
    PRInt64 result = LL_INIT(0, 0);
    char x = 0;
    int num = PR_sscanf(str, "%lld%c", &val, &x);
    if (num < 1) { /* e.g. not a number */
        if (err)
            *err = 1;
        return result; /* return 0 */
    }

    switch (x) {
    case 'g':
    case 'G':
    LL_MUL(mres1, onek, multiplier);
/*    multiplier *= 1024;*/
    case 'm':
    case 'M':
    LL_MUL(mres2, onek, mres1);
/*    multiplier *= 1024;*/
    case 'k':
    case 'K':
    LL_MUL(mres3, onek, mres2);
/*    multiplier *= 1024;*/
    }
    LL_MUL(result, val, mres3);
/*    result = val * multiplier;*/
    if (err)
        *err = 0;
    return result;
}

PRInt64 db_atoi(char *str, int *err)
{
    return db_atol(str, err);
}

unsigned long db_strtoul(const char *str, int *err)
{
    unsigned long val = 0, result, multiplier = 1;
    char *p;
    errno = 0;

    /*
     * manpage of strtoul: Negative  values  are considered valid input and
     * are silently converted to the equivalent unsigned long int value.
     */
    /* We don't want to make it happen. */
    for (p = (char *)str; p && *p && (*p == ' ' || *p == '\t'); p++) ;
    if ('-' == *p) {
        if (err) *err = ERANGE;
        return val;
    }
    val = strtoul(str, &p, 10);
    if (errno != 0) {
        if (err) *err = errno;
        return val;
    }

    switch (*p) {
    case 'g':
    case 'G':
        multiplier *= 1024;
    case 'm':
    case 'M':
        multiplier *= 1024;
    case 'k':
    case 'K':
        multiplier *= 1024;
        p++;
        if (*p == 'b' || *p == 'B') p++;
        if (err) {
            /* extra chars? */
            *err = (*p != '\0') ? EINVAL : 0;
        }
        break;
    case '\0':
        if (err) *err = 0;
        break;
    default:
        if (err) *err = EINVAL;
        return val;
    }

    result = val * multiplier;

    return result;
}

/* functions called directly by the plugin interface from the front-end */

/* Begin transaction */
int dblayer_plugin_begin(Slapi_PBlock *pb)
{
    int return_value = -1;
    struct ldbminfo *li = NULL;
    back_txnid    parent;
    back_txn    current;

    slapi_pblock_get( pb, SLAPI_PLUGIN_PRIVATE, &li );
    slapi_pblock_get( pb, SLAPI_PARENT_TXN, (void**)&parent );

    /* call begin, and put the result in the txnid parameter */
    return_value = dblayer_txn_begin(li,parent,&current);

    if (0 == return_value) 
    {
        slapi_pblock_set( pb, SLAPI_TXN, (void*)current.back_txn_txn );
    }

    return return_value;
}

/* Commit transaction */
int dblayer_plugin_commit(Slapi_PBlock *pb)
{
    /* get the txnid and call commit */
    int return_value = -1;
    struct ldbminfo *li = NULL;
    back_txn    current;

    slapi_pblock_get( pb, SLAPI_PLUGIN_PRIVATE, &li );
    slapi_pblock_get( pb, SLAPI_TXN, (void**)&(current.back_txn_txn) );

    /* call begin, and put the result in the txnid parameter */
    return_value = dblayer_txn_commit(li,&current);

    return return_value;
}

/* Abort Transaction */
int dblayer_plugin_abort(Slapi_PBlock *pb)
{
    /* get the txnid and call abort */
    return 0;
}


/* Helper function for monitor stuff */
int dblayer_memp_stat(struct ldbminfo *li, DB_MPOOL_STAT **gsp,
                      DB_MPOOL_FSTAT ***fsp)
{
    dblayer_private *priv = NULL;
    DB_ENV *env = NULL;

    PR_ASSERT(NULL != li);
    
    priv = (dblayer_private*)li->li_dblayer_private;
    PR_ASSERT(NULL != priv);

    env = priv->dblayer_env->dblayer_DB_ENV;
    PR_ASSERT(NULL != env);
    
    return MEMP_STAT(env, gsp, fsp, 0, (void *)slapi_ch_malloc);
}

/* import wants this one */
int dblayer_memp_stat_instance(ldbm_instance *inst, DB_MPOOL_STAT **gsp,
                               DB_MPOOL_FSTAT ***fsp)
{
    DB_ENV *env = NULL;
    dblayer_private *priv = NULL;

    PR_ASSERT(NULL != inst);

    if (inst->import_env->dblayer_DB_ENV) {
        env = inst->import_env->dblayer_DB_ENV;
    } else {
        priv = (dblayer_private *)inst->inst_li->li_dblayer_private;
        PR_ASSERT(NULL != priv);
        env = priv->dblayer_env->dblayer_DB_ENV;
    }
    PR_ASSERT(NULL != env);

    return MEMP_STAT(env, gsp, fsp, 0, (void *)slapi_ch_malloc);
}

/* Helper functions for recovery */

#define DB_LINE_LENGTH 80

static int commit_good_database(dblayer_private *priv)
{
    /* Write out the guard file */
    char filename[MAXPATHLEN];
    char line[DB_LINE_LENGTH * 2];
    PRFileDesc *prfd;
    int return_value = 0;
    int num_bytes;

    PR_snprintf(filename,sizeof(filename), "%s/guardian", priv->dblayer_home_directory);

    prfd = PR_Open(filename, PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE,
        priv->dblayer_file_mode );
    if (NULL == prfd)
    {
        LDAPDebug( LDAP_DEBUG_ANY,"Fatal Error---Failed to write guardian file %s, database corruption possible" SLAPI_COMPONENT_NAME_NSPR " %d (%s)\n",
            filename, PR_GetError(), slapd_pr_strerror(PR_GetError()) );
        return -1;
    } 
    PR_snprintf(line,sizeof(line),"cachesize:%lu\nncache:%d\nversion:%d\n",
            priv->dblayer_cachesize, priv->dblayer_ncache, DB_VERSION_MAJOR);
    num_bytes = strlen(line);
    return_value = slapi_write_buffer(prfd, line, num_bytes);
    if (return_value != num_bytes)
    {
        goto error;
    }
    return_value = PR_Close(prfd);
    if (PR_SUCCESS == return_value)
    {
        return 0;
    } else
    {
        LDAPDebug( LDAP_DEBUG_ANY,"Fatal Error---Failed to write guardian file, database corruption possible\n", 0,0, 0 );
        (void)PR_Delete(filename);
        return -1;
    }
error:
    (void)PR_Close(prfd);
    (void)PR_Delete(filename);
    return -1;
}

/* read the guardian file from db/ and possibly recover the database */
static int read_metadata(struct ldbminfo *li)
{
    char filename[MAXPATHLEN];
    char *buf;
    char *thisline;
    char *nextline;
    char **dirp;
    PRFileDesc *prfd;
    PRFileInfo prfinfo;
    int return_value = 0;
    PRInt32 byte_count = 0;
    char attribute[512];
    char value[128], delimiter;
    int number = 0;
    dblayer_private *priv = (dblayer_private *)li->li_dblayer_private;

    /* dblayer_recovery_required is initialized in dblayer_init;
     * and might be set 1 in check_db_version;
     * we don't want to override it
     * priv->dblayer_recovery_required = 0; */
    priv->dblayer_previous_cachesize = 0;
    priv->dblayer_previous_ncache = 0;
    /* Open the guard file and read stuff, then delete it */
    PR_snprintf(filename,sizeof(filename),"%s/guardian",priv->dblayer_home_directory);

    memset(&prfinfo, '\0', sizeof(PRFileInfo));
    (void)PR_GetFileInfo(filename, &prfinfo);

    prfd = PR_Open(filename,PR_RDONLY,priv->dblayer_file_mode);
    if (NULL == prfd || 0 == prfinfo.size) {
        /* file empty or not present--means the database needs recovered */
        int count = 0;
        for (dirp = priv->dblayer_data_directories; dirp && *dirp; dirp++)
        {
            count_dbfiles_in_dir(*dirp, &count, 1 /* recurse */);
            if (count > 0) {
#if 0
                char *home_dir;
                /* This code used to check for a broken import by looking 
                 * for a dbversion file.  If it wasn't there, then an import
                 * failed.  Now each instance has its own dbversion file.
                 * If this check is done at all, it ought to be done when
                 * bringing up individual backend instances.
                 */
                /* While we're here, let's check for a broken import.
                   This would be indicated by the following conditions:
                   1. db files in the directory.
                   2. No guardian file.
                   3. No DBVERSION file.
                   If we're here we have confitions 1 and 2,
                   so we should check for condition 3.
                   */
                if (!dbversion_exists(li, home_dir)) {
                    LDAPDebug( LDAP_DEBUG_ANY,"Fatal Error---database is corrupt. Server can't start. Most likely cause is a previously aborted import. Either re-import or delete the database and re-start the server.\n", 0,0, 0 );
                    return -1;
                } else {
                    priv->dblayer_recovery_required = 1;
                }
#endif
                priv->dblayer_recovery_required = 1;
                return 0;
            }
        }
        return 0; /* no files found; no need to run recover start */
    }
    /* So, we opened the file, now let's read the cache size and version stuff
     */
    buf = slapi_ch_calloc(1, prfinfo.size + 1);
    byte_count = slapi_read_buffer(prfd, buf, prfinfo.size);
    if (byte_count < 0) {
        /* something bad happened while reading */
        priv->dblayer_recovery_required = 1;
    } else {
        buf[ byte_count ] = '\0';
        thisline = buf;
        while (1) {
            /* Find the end of the line */
            nextline = strchr( thisline, '\n' );
            if (NULL != nextline) {
                *nextline++ = '\0';
                while ('\n' == *nextline) {
                    nextline++;
                }
            }
            sscanf(thisline,"%[a-z]%c%s",attribute,&delimiter,value);
            if (0 == strcmp("cachesize",attribute)) {
                priv->dblayer_previous_cachesize = strtoul(value, NULL, 10);
            } else if (0 == strcmp("ncache",attribute)) {
                number = atoi(value);
                priv->dblayer_previous_ncache = number;
            } else if (0 == strcmp("version",attribute)) {
            }
            if (NULL == nextline || '\0' == *nextline) {
                /* Nothing more to read */
                break;
            }
            thisline = nextline;
        }
    }
    slapi_ch_free((void **)&buf);
    (void)PR_Close(prfd);
    return_value = PR_Delete(filename); /* very important that this happen ! */
    if (PR_SUCCESS != return_value) {
        LDAPDebug(LDAP_DEBUG_ANY,
            "Fatal Error---Failed to delete guardian file, "
            "database corruption possible\n", 0, 0, 0 );
    }
    return return_value;
}

/* handy routine for checkpointing the db */
static int dblayer_force_checkpoint(struct ldbminfo *li)
{
  int ret = 0, i;
  dblayer_private *priv = (dblayer_private *)li->li_dblayer_private;
  struct dblayer_private_env *pEnv;
  
  if (NULL == priv || NULL == priv->dblayer_env){
    /* already terminated.  nothing to do */  
    return -1;
  }
   
  pEnv= priv->dblayer_env;
  
  if (priv->dblayer_enable_transactions) {
    
    LDAPDebug(LDAP_DEBUG_TRACE, "Checkpointing database ...\n", 0, 0, 0);
    
    /* 
     * DB workaround. Newly created environments do not know what the
     * previous checkpoint LSN is. The default LSN of [0][0] would
     * cause us to read all log files from very beginning during a
     * later recovery. Taking two checkpoints solves the problem.
     */
    
    for (i = 0; i < 2; i++) {
      ret = dblayer_txn_checkpoint(li, pEnv, PR_TRUE, PR_FALSE);
      if (ret == 0) continue;
#if 1000*DB_VERSION_MAJOR + 100*DB_VERSION_MINOR < 4100
      if (ret != DB_INCOMPLETE)
#endif
      {
        LDAPDebug(LDAP_DEBUG_ANY, "Checkpoint FAILED, error %s (%d)\n",
                  dblayer_strerror(ret), ret, 0);
        break;
      }
#if 1000*DB_VERSION_MAJOR + 100*DB_VERSION_MINOR < 4100
      
      LDAPDebug(LDAP_DEBUG_ANY, "Busy: retrying checkpoint\n", 0, 0, 0);
      
      /* teletubbies: "again! again!" */
      ret = dblayer_txn_checkpoint(li, pEnv, PR_TRUE, PR_FALSE);
      if (ret == DB_INCOMPLETE) {
        LDAPDebug(LDAP_DEBUG_ANY, "Busy: giving up on checkpoint\n", 0, 0, 0);
        break;
      } else if (ret != 0) {
        LDAPDebug(LDAP_DEBUG_ANY, "Checkpoint FAILED, error %s (%d)\n",
                  dblayer_strerror(ret), ret, 0);
        break;
      }
#endif
    }
  }

  return ret;
}

static int
_dblayer_delete_aux_dir(struct ldbminfo *li, char *path)
{
    PRDir *dirhandle = NULL;
    PRDirEntry *direntry = NULL;
    char filename[MAXPATHLEN];
    dblayer_private *priv = NULL;
    struct dblayer_private_env *pEnv = NULL;
    int rc = -1;

    if (NULL == li || NULL == path) {
        LDAPDebug2Args(LDAP_DEBUG_ANY,
                       "_dblayer_delete_aux_dir: Invalid LDBM info (0x%x) "
                       "or path (0x%x)\n", li, path);
        return rc;
    }
    priv = (dblayer_private*)li->li_dblayer_private;
    if (priv) {
        pEnv = priv->dblayer_env;
    }
    dirhandle = PR_OpenDir(path);
    if (!dirhandle) {
        return 0; /* The dir does not exist. */
    }
    while (NULL != (direntry = PR_ReadDir(dirhandle,
                                          PR_SKIP_DOT | PR_SKIP_DOT_DOT))) {
        if (! direntry->name)
            break;
        PR_snprintf(filename, sizeof(filename), "%s/%s", path, direntry->name);
        if (pEnv &&
            strcmp(LDBM_FILENAME_SUFFIX , last_four_chars(direntry->name)) == 0)
        {
            rc = dblayer_db_remove_ex(pEnv, filename, 0, PR_TRUE);
        }
        else
        {
            rc = ldbm_delete_dirs(filename);
        }
    }
    PR_CloseDir(dirhandle);
    PR_RmDir(path);
    return rc;
}

/* TEL:  Added startdb flag.  If set (1), the DB environment will be started so
 * that dblayer_db_remove_ex will be used to remove the database files instead
 * of simply deleting them.  That is important when doing a selective restoration
 * of a single backend (FRI).  If not set (0), the traditional remove is used.
 */
static int _dblayer_delete_instance_dir(ldbm_instance *inst, int startdb)
{
    PRDir *dirhandle = NULL;
    PRDirEntry *direntry = NULL;
    char filename[MAXPATHLEN];
    struct ldbminfo *li = inst->inst_li;
    dblayer_private *priv = NULL;
    struct dblayer_private_env *pEnv = NULL;
    char inst_dir[MAXPATHLEN];
    char *inst_dirp = NULL;
    int rval = 0;

    if (NULL == li)
    {
        LDAPDebug0Args(LDAP_DEBUG_ANY,
                       "_dblayer_delete_instance_dir: NULL LDBM info\n");
        rval = -1;
        goto done;
    }

    if (startdb)
    {
        /* close immediately; no need to run db threads */
        rval = dblayer_start(li, DBLAYER_NORMAL_MODE|DBLAYER_NO_DBTHREADS_MODE);
        if (rval)
        {
            LDAPDebug(LDAP_DEBUG_ANY, "_dblayer_delete_instance_dir: dblayer_start failed! %s (%d)\n",
                dblayer_strerror(rval), rval, 0);
            goto done;
        }
    }

    priv = (dblayer_private*)li->li_dblayer_private;
    if (NULL != priv)
    {
        pEnv = priv->dblayer_env;
    }

    if (inst->inst_dir_name == NULL)
        dblayer_get_instance_data_dir(inst->inst_be);

    inst_dirp = dblayer_get_full_inst_dir(li, inst, inst_dir, MAXPATHLEN);
    if (inst_dirp && *inst_dirp) {
        dirhandle = PR_OpenDir(inst_dirp);
    }
    if (! dirhandle) {
        if ( PR_GetError() == PR_FILE_NOT_FOUND_ERROR ) {
             /* the directory does not exist... that's not an error */
             rval = 0;
             goto done;
        }
        if (inst_dirp && *inst_dirp) {
            LDAPDebug(LDAP_DEBUG_ANY,
              "_dblayer_delete_instance_dir: inst_dir is NULL\n", 0, 0, 0);
        } else {
            LDAPDebug(LDAP_DEBUG_ANY,
              "_dblayer_delete_instance_dir: PR_OpenDir(%s) failed (%d): %s\n", 
              inst_dirp, PR_GetError(),slapd_pr_strerror(PR_GetError()));
        }
        rval = -1;
        goto done;
    }

    /*
        Note the use of PR_Delete here as opposed to using
        sleepycat to "remove" the file. Reason: One should
        not expect logging to be able to recover the wholesale
        removal of a complete directory... a directory that includes
        files outside the scope of sleepycat's logging. rwagner

        ADDITIONAL COMMENT:
        libdb41 is more strict on the transaction log control.
        Even if checkpoint is forced before this delete function,
        no log regarding the file deleted found in the log file,
        following checkpoint repeatedly complains with these error messages:
        libdb: <path>/mail.db4: cannot sync: No such file or directory
        libdb: txn_checkpoint: failed to flush the buffer cache 
                               No such file or directory
    */

    while (NULL != (direntry = PR_ReadDir(dirhandle, PR_SKIP_DOT |
                                          PR_SKIP_DOT_DOT))) {
        if (! direntry->name)
            break;
        PR_snprintf(filename, MAXPATHLEN, "%s/%s", inst_dirp, direntry->name);
        if (pEnv &&
            strcmp(LDBM_FILENAME_SUFFIX , last_four_chars(direntry->name)) == 0)
        {
            rval = dblayer_db_remove_ex(pEnv, filename, 0, PR_TRUE);
        }
        else
        {
            rval = ldbm_delete_dirs(filename);
        }
    }
    PR_CloseDir(dirhandle);
    if (pEnv && startdb)
    {
        rval = dblayer_close(li, DBLAYER_NORMAL_MODE);
        if (rval)
        {
            LDAPDebug(LDAP_DEBUG_ANY, "_dblayer_delete_instance_dir: dblayer_close failed! %s (%d)\n",
                dblayer_strerror(rval), rval, 0);
        }
    }
done:
    /* remove the directory itself too */
    if (0 == rval)
        PR_RmDir(inst_dirp);
    if (inst_dirp != inst_dir)
        slapi_ch_free_string(&inst_dirp);
    return rval;
}

/* delete the db3 files in a specific backend instance --
 * this is probably only used for import.
 * assumption: dblayer is open, but the instance has been closed.
 */
int dblayer_delete_instance_dir(backend *be)
{
  struct ldbminfo *li = (struct ldbminfo *)be->be_database->plg_private;
  int ret = dblayer_force_checkpoint(li);
  
  if (ret != 0) {
    return ret;
  } else {
    ldbm_instance *inst = (ldbm_instance *)be->be_instance_info;
    return _dblayer_delete_instance_dir(inst, 0);
  }
}


/* delete an entire db/ directory, including either all instances, or
 * just a single instance (leaving the others intact), if the instance param is non-NULL !
 * this is used mostly for restores.
 * dblayer is assumed to be closed.
 */
static int
dblayer_delete_database_ex(struct ldbminfo *li, char *instance, char *cldir)
{
    dblayer_private *priv = NULL;
    Object *inst_obj;
    PRDir *dirhandle = NULL;
    PRDirEntry *direntry = NULL;
	PRFileInfo fileinfo;
    char filename[MAXPATHLEN];
    char *log_dir;
    int ret;

    PR_ASSERT(NULL != li);
    priv = (dblayer_private *)li->li_dblayer_private;
    PR_ASSERT(NULL != priv);

    /* delete each instance */
    for (inst_obj = objset_first_obj(li->li_instance_set); inst_obj;
        inst_obj = objset_next_obj(li->li_instance_set, inst_obj)) {
        ldbm_instance *inst = (ldbm_instance *)object_get_data(inst_obj);

        if (inst->inst_be->be_instance_info != NULL) {
			if ((NULL != instance) && (strcmp(inst->inst_name,instance) != 0)) 
			{
				LDAPDebug(LDAP_DEBUG_ANY,
					"dblayer_delete_database: skipping instance %s\n",inst->inst_name , 0, 0);	
			} else 
			{
				if (NULL == instance)
				{
					ret = _dblayer_delete_instance_dir(inst, 0 /* Do not start DB environment: traditional */);
				} else {
					ret = _dblayer_delete_instance_dir(inst, 1 /* Start DB environment: for FRI */);
				}
				if (ret != 0)
				{
					LDAPDebug(LDAP_DEBUG_ANY,
					"dblayer_delete_database: WARNING _dblayer_delete_instance_dir failed (%d)\n", ret, 0, 0);
					return ret;
				}	
			}
        }
    }

    /* changelog path is given; delete it, too. */
    if (cldir) {
        ret = _dblayer_delete_aux_dir(li, cldir);
        if (ret) {
            LDAPDebug1Arg(LDAP_DEBUG_ANY,
                          "dblayer_delete_database: failed to deelete \"%s\"\n",
                          chdir);
            return ret;
        }
    }

    /* now smash everything else in the db/ dir */
    dirhandle = PR_OpenDir(priv->dblayer_home_directory);
    if (! dirhandle)
    {
        LDAPDebug(LDAP_DEBUG_ANY, "PR_OpenDir (%s) failed (%d): %s\n", 
        priv->dblayer_home_directory,
        PR_GetError(),slapd_pr_strerror(PR_GetError()));
        return -1;
    }
    while (NULL != (direntry = PR_ReadDir(dirhandle, PR_SKIP_DOT |
                                          PR_SKIP_DOT_DOT))) {
		int rval_tmp = 0;
        if (! direntry->name)
            break;

		PR_snprintf(filename, MAXPATHLEN, "%s/%s", priv->dblayer_home_directory, 
			direntry->name);

		/* Do not call PR_Delete on the instance directories if they exist.
		 * It would not work, but we still should not do it. */
        rval_tmp = PR_GetFileInfo(filename, &fileinfo);
        if (rval_tmp == PR_SUCCESS && fileinfo.type != PR_FILE_DIRECTORY)
		{
			/* Skip deleting log files; that should be handled below.
			 * (Note, we don't want to use "filename," because that is qualified and would
			 * not be compatibile with what dblayer_is_logfilename expects.) */
			if (!dblayer_is_logfilename(direntry->name))
			{       
				PR_Delete(filename);
			}
		}
    }

    PR_CloseDir(dirhandle);
    /* remove transaction logs */
    if ((NULL != priv->dblayer_log_directory) &&
        (0 != strlen(priv->dblayer_log_directory) )) 
    {
        log_dir = priv->dblayer_log_directory;
    }
    else
    {
        log_dir = dblayer_get_home_dir(li, NULL);
    }
	if (instance == NULL && log_dir && *log_dir)
	{
		ret = dblayer_delete_transaction_logs(log_dir);
		if(ret) {
		  LDAPDebug(LDAP_DEBUG_ANY,
		  "dblayer_delete_database: dblayer_delete_transaction_logs failed (%d)\n",
		  ret, 0, 0);
		  return -1;
		}
	}
    return 0;
}

/* delete an entire db/ directory, including all instances under it!
 * this is used mostly for restores.
 * dblayer is assumed to be closed.
 */
int dblayer_delete_database(struct ldbminfo *li)
{
	return dblayer_delete_database_ex(li, NULL, NULL);
}


/* 
 * Return the size of the database (in kilobytes).  XXXggood returning
 * the size in units of kb is really a hack, and is done because we
 * don't have NSPR support for 64-bit file offsets.
 * Caveats:
 * - We can still return incorrect results if an individual file is
 *   larger than fit in a PRUint32.
 * - PR_GetFileInfo doesn't do any special processing for symlinks,
 *   nor does it inform us if the file is a symlink.  Nice.  So if
 *   a file in the db directory is a symlink, the size we return
 *   will probably be way too small.
 */
int dblayer_database_size(struct ldbminfo *li, unsigned int *size)
{
     dblayer_private *priv = NULL;
    int return_value = 0;
    char filename[MAXPATHLEN];
    PRDir    *dirhandle = NULL;
    /*
     * XXXggood - NSPR will only give us an unsigned 32-bit quantity for
     * file sizes.  This is bad.  Files can be bigger than that these days.
     */
    unsigned int cumulative_size = 0;
    unsigned int remainder = 0;
    PRFileInfo info;

    PR_ASSERT(NULL != li);
    priv = (dblayer_private*)li->li_dblayer_private;
    PR_ASSERT(NULL != priv);

    dirhandle = PR_OpenDir(priv->dblayer_home_directory);
    if (NULL != dirhandle)
    {
        PRDirEntry *direntry = NULL;
        while (NULL != (direntry = PR_ReadDir(dirhandle, PR_SKIP_DOT | PR_SKIP_DOT_DOT))) {
            if (NULL == direntry->name)
            {
                break;
            }
            PR_snprintf(filename,MAXPATHLEN, "%s/%s",priv->dblayer_home_directory,direntry->name);
            return_value = PR_GetFileInfo(filename, &info);
            if (PR_SUCCESS == return_value)
            {
                cumulative_size += (info.size / 1024);
                remainder += (info.size % 1024);
            } else
            {
                cumulative_size = (PRUint32) 0;
                return_value = -1;
                break;
            }
        }
        PR_CloseDir(dirhandle);
    } else
    {
        return_value = -1;
    }

    *size = cumulative_size + (remainder / 1024);
    return return_value;
}


static int count_dbfiles_in_dir(char *directory, int *count, int recurse)
{
    /* The new recurse argument was added to help with multiple backend
     * instances.  When recurse is true, this function will also look through
     * the directories in the given directory for .db3 files. */
    int return_value  = 0;
    PRDir *dirhandle = NULL;

    if (!recurse) {
        /* It is really the callers responsibility to set count to 0 before
         * calling.  However, if recurse isn't true, we can make sure it is
         * set to 0. */
        *count = 0;
    }
    dirhandle = PR_OpenDir(directory);
    if (NULL != dirhandle) {
        PRDirEntry *direntry = NULL;
        char *direntry_name;
        PRFileInfo info;

        while (NULL != (direntry = PR_ReadDir(dirhandle, PR_SKIP_DOT | PR_SKIP_DOT_DOT))) {
            if (NULL == direntry->name) {
                break;
            }
            direntry_name = PR_smprintf("%s/%s", directory, direntry->name);
            if ((PR_GetFileInfo(direntry_name, &info) == PR_SUCCESS) &&
                (PR_FILE_DIRECTORY == info.type) && recurse) {
                /* Recurse into this directory but not any further.  This is
                 * because each instance gets its own directory, but in those
                 * directories there should be only .db3 files.  There should
                 * not be any more directories in an instance directory. */
                count_dbfiles_in_dir(direntry_name, count, 0 /* don't recurse */);
            }
			if (direntry_name) {
				PR_smprintf_free(direntry_name);
			}
            if (strcmp( LDBM_FILENAME_SUFFIX , last_four_chars(direntry->name)) == 0) {
                (*count)++;
            }
        }
        PR_CloseDir(dirhandle);
    } else {
        return_value = -1;
    }

    return return_value;
}

/* And finally... Tubular Bells. 
 * Well, no, actually backup and restore...
 */

/* Backup works like this:
 * the slapd executable is run like for ldif2ldbm and so on.
 * this means that the front-end gets the back-end loaded, and then calls
 * into the back-end backup entry point. This then gets us down to here.
 * 
 * So, we need to copy the data files to the backup point.
 * While we are doing that, we need to make sure that the logfile
 * truncator in slapd doesn't delete our files. To do this we need
 * some way to signal to it that it should cease its work, or we need
 * to do something like start a long-lived transaction so that the
 * log files look like they're needed.
 *
 * When we've copied the data files, we can then copy the log files
 * too.
 * 
 * Finally, we tell the log file truncator to go back about its business in peace
 *
 */

int
dblayer_copyfile(char *source, char *destination, int overwrite, int mode) 
{
#if defined _WIN32
    return (0 == CopyFile(source,destination,overwrite ? FALSE : TRUE));
#else
#ifdef DB_USE_64LFS
#define OPEN_FUNCTION dblayer_open_large
#else
#define OPEN_FUNCTION open    
#endif
    int source_fd = -1;
    int dest_fd = -1;
    char *buffer = NULL;
    int return_value = -1;
    int bytes_to_write = 0;

    /* malloc the buffer */
    buffer = slapi_ch_malloc(64*1024);
    if (NULL == buffer)
    {
        goto error;
    }
    /* Open source file */
    source_fd = OPEN_FUNCTION(source,O_RDONLY,0);
    if (-1 == source_fd)
    {
        LDAPDebug1Arg(LDAP_DEBUG_ANY,
                      "dblayer_copyfile: failed to open source file: %s\n",
                      source);
        goto error;
    }
    /* Open destination file */
    dest_fd = OPEN_FUNCTION(destination,O_CREAT | O_WRONLY, mode);
    if (-1 == dest_fd)
    {
        LDAPDebug1Arg(LDAP_DEBUG_ANY,
                      "dblayer_copyfile: failed to open dest file: %s\n",
                      destination);
        goto error;
    }
    LDAPDebug2Args(LDAP_DEBUG_BACKLDBM,
                   "Copying %s to %s\n", source, destination);
    /* Loop round reading data and writing it */
    while (1)
    {
        return_value = read(source_fd,buffer,64*1024);
        if (return_value <= 0)
        {
            /* means error or EOF */
            if (return_value < 0)
            {
                LDAPDebug1Arg(LDAP_DEBUG_ANY,
                              "dblayer_copyfile: failed to read: %d\n", errno);
            }
            break;
        }
        bytes_to_write = return_value;
        return_value = write(dest_fd,buffer,bytes_to_write);
        if (return_value != bytes_to_write)
        {
            /* means error */
            LDAPDebug1Arg(LDAP_DEBUG_ANY,
                          "dblayer_copyfile: failed to write: %d\n", errno);
            return_value = -1;
            break;
        }
    }
error:
    if (source_fd != -1)
    {
        close(source_fd);
    }
    if (dest_fd != -1)
    {
        close(dest_fd);
    }
    slapi_ch_free((void**)&buffer);
    return return_value;
#endif
}

/*
 * Copies all the .db# files in instance_dir to a directory with the same name
 * in destination_dir.  Both instance_dir and destination_dir are absolute
 * paths.  
 * (#604921: added indexonly flag for the use in convindices 
 *           -- backup/restore indices)
 *
 * If the argument restore is true,
 *        logging messages will be about "Restoring" files.
 * If the argument restore is false,
 *        logging messages will be about "Backing up" files.
 * The argument cnt is used to count the number of files that were copied.
 *
 * This function is used during db2bak and bak2db.
 * DBDB added resetlsns arg which is used in partial restore (because the LSNs need to be reset to avoid
 * confusing transaction logging code).
 */
int
dblayer_copy_directory(struct ldbminfo *li,
                       Slapi_Task *task,
                       char *src_dir, 
                       char *dest_dir,
                       int restore,
                       int *cnt,
                       int instance_dir_flag,
                       int indexonly,
                       int resetlsns)
{
    dblayer_private *priv = NULL;
    char            *new_src_dir = NULL;
    char            *new_dest_dir = NULL;
    PRDir           *dirhandle = NULL;
    PRDirEntry      *direntry = NULL;
    size_t          filename_length = 0;
    size_t          offset = 0;
    char            *compare_piece = NULL;
    char            *filename1;
    char            *filename2;
    int             return_value = -1;
    char            *relative_instance_name = NULL;
    char            *inst_dirp = NULL;
    char            inst_dir[MAXPATHLEN];
    char            sep;

    if (!src_dir || '\0' == *src_dir)
    {
        LDAPDebug0Args(LDAP_DEBUG_ANY, 
                       "dblayer_copy_directory: src_dir is empty\n");
        return return_value;
    }
    if (!dest_dir || '\0' == *dest_dir)
    {
        LDAPDebug0Args(LDAP_DEBUG_ANY, 
                       "dblayer_copy_directory: dest_dir is empty\n");
        return return_value;
    }

    priv = (dblayer_private *) li->li_dblayer_private;

    /* get the backend instance name */
    sep = get_sep(src_dir);
    if ((relative_instance_name = strrchr(src_dir, sep)) == NULL)
        relative_instance_name = src_dir;
    else
        relative_instance_name++;

    if (is_fullpath(src_dir))
    {
        new_src_dir = src_dir;
    }
    else
    {
        int len;
        ldbm_instance *inst =
                       ldbm_instance_find_by_name(li, relative_instance_name);
        if (NULL == inst)
        {
            LDAPDebug(LDAP_DEBUG_ANY, "Backend instance \"%s\" does not exist; "
                  "Instance path %s could be invalid.\n",
                  relative_instance_name, src_dir, 0);
            return return_value;
        }

        inst_dirp = dblayer_get_full_inst_dir(inst->inst_li, inst,
                                              inst_dir, MAXPATHLEN);
        if (!inst_dirp || !*inst_dirp)
        {
            LDAPDebug(LDAP_DEBUG_ANY, "Instance dir is NULL.\n", 0, 0, 0);
            return return_value;
        }
        len = strlen(inst_dirp);
        sep = get_sep(inst_dirp);
        if (*(inst_dirp+len-1) == sep)
            sep = '\0';
        new_src_dir = inst_dirp;
    }

    dirhandle = PR_OpenDir(new_src_dir);
    if (NULL == dirhandle)
    {
        LDAPDebug1Arg(LDAP_DEBUG_ANY, 
                      "dblayer_copy_directory: failed to open dir %s\n",
                      new_src_dir);

        return return_value;
    }

    while (NULL != (direntry =
                    PR_ReadDir(dirhandle, PR_SKIP_DOT | PR_SKIP_DOT_DOT)))
    {
        if (NULL == direntry->name) {
            /* NSPR doesn't behave like the docs say it should */
            break;
        }
        if (indexonly &&
            0 == strcmp(direntry->name, ID2ENTRY LDBM_FILENAME_SUFFIX))
        {
            continue;
        }

        /* Look at the last three characters in the filename */
        filename_length = strlen(direntry->name);
        if (filename_length > 4) {
            offset = filename_length - 4;
        } else {
            offset = 0;
        }
        compare_piece = (char *)direntry->name + offset;

        if (0 == strcmp(compare_piece, LDBM_FILENAME_SUFFIX) || /* .db4 */
            0 == strcmp(compare_piece, LDBM_SUFFIX_OLD) || /* support .db3 */
            0 == strcmp(direntry->name, DBVERSION_FILENAME)) {
            /* Found a database file.  Copy it. */

            if (NULL == new_dest_dir) {
                /* Need to create the new directory where the files will be
                 * copied to. */
                PRFileInfo info;
                char *prefix = "";
                char mysep = 0;

                if (!is_fullpath(dest_dir))
                {
                    prefix = dblayer_get_home_dir(li, NULL);
                    if (!prefix || !*prefix)
                    {
                        continue;
                    }
                    mysep = get_sep(prefix);
                }

                if (mysep)
                    new_dest_dir = slapi_ch_smprintf("%s%c%s%c%s",
                        prefix, mysep, dest_dir, mysep, relative_instance_name);
                else
                    new_dest_dir = slapi_ch_smprintf("%s/%s",
                        dest_dir, relative_instance_name);
                /* } */
                if (PR_SUCCESS == PR_GetFileInfo(new_dest_dir, &info))
                {
                    ldbm_delete_dirs(new_dest_dir);
                }
                if (mkdir_p(new_dest_dir, 0700) != PR_SUCCESS)
                {
                    LDAPDebug(LDAP_DEBUG_ANY, "Can't create new directory %s, "
                        SLAPI_COMPONENT_NAME_NSPR " error %d (%s)\n",
                        new_dest_dir, PR_GetError(),
                        slapd_pr_strerror(PR_GetError()));
                    goto out;
                }
            }

            filename1 = slapi_ch_smprintf("%s/%s", new_src_dir, direntry->name);
            filename2 = slapi_ch_smprintf("%s/%s", new_dest_dir, direntry->name);

            if (restore) {
                LDAPDebug(LDAP_DEBUG_ANY, "Restoring file %d (%s)\n",
                          *cnt, filename2, 0);
                if (task) {
                    slapi_task_log_notice(task,
                        "Restoring file %d (%s)", *cnt, filename2);
                    slapi_task_log_status(task,
                        "Restoring file %d (%s)", *cnt, filename2);
                }
            } else {
                LDAPDebug(LDAP_DEBUG_ANY, "Backing up file %d (%s)\n",
                          *cnt, filename2, 0);
                if (task) {
                    slapi_task_log_notice(task,
                        "Backing up file %d (%s)", *cnt, filename2);
                    slapi_task_log_status(task,
                        "Backing up file %d (%s)", *cnt, filename2);
                }
            }

            /* copy filename1 to filename2 */
            /* If the file is a database file, and resetlsns is set, then we need to do a key by key copy */
            if (strcmp(LDBM_FILENAME_SUFFIX, last_four_chars(filename1)) == 0 && resetlsns) {
                return_value = dblayer_copy_file_resetlsns(src_dir, filename1, filename2,
                                            0, priv);
            } else {
                return_value = dblayer_copyfile(filename1, filename2,
                                            0, priv->dblayer_file_mode);
            }
            slapi_ch_free((void**)&filename1);
            slapi_ch_free((void**)&filename2);
            if (0 > return_value)
                break;

            (*cnt)++;
        }
    }
out:
    PR_CloseDir(dirhandle);
    slapi_ch_free_string(&new_dest_dir);
    if ((new_src_dir != src_dir) && (new_src_dir != inst_dir))
    {
        slapi_ch_free_string(&new_src_dir);
    }
    return return_value;
}

/*
 * Get changelogdir from cn=changelog5,cn=config
 * The value does not have trailing spaces nor slashes.
 */
static int
_dblayer_get_changelogdir(struct ldbminfo *li, char **changelogdir)
{
    Slapi_PBlock *pb = NULL;
    Slapi_Entry **entries = NULL;
    Slapi_Attr *attr = NULL;
    Slapi_Value *v = NULL;
    const char *s = NULL;
    char *attrs[2];
    int rc = -1;

    if (NULL == li || NULL == changelogdir) {
        LDAPDebug2Args(LDAP_DEBUG_ANY, 
                       "ERROR: _dblayer_get_changelogdir: Invalid arg: "
                       "li: 0x%x, changelogdir: 0x%x\n", li, changelogdir);
        return rc;
    }
    *changelogdir = NULL;

    pb = slapi_pblock_new();
    attrs[0] = CHANGELOGDIRATTR;
    attrs[1] = NULL;
    slapi_search_internal_set_pb(pb, CHANGELOGENTRY,
                                 LDAP_SCOPE_BASE, "cn=*", attrs, 0, NULL, NULL,
                                 li->li_identity, 0);
    slapi_search_internal_pb(pb);
    slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
    
    if (LDAP_NO_SUCH_OBJECT == rc) {
        /* No changelog; Most likely standalone or not a master. */
        rc = LDAP_SUCCESS;
        goto bail;
    }
    if (LDAP_SUCCESS != rc) {
        LDAPDebug1Arg(LDAP_DEBUG_ANY, 
                      "ERROR: Failed to search \"%s\"\n", CHANGELOGENTRY);
        goto bail;
    }
    /* rc == LDAP_SUCCESS */
    slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
    if (NULL == entries) {
        /* No changelog */
        goto bail;
    }
    /* There should be only one entry. */
    rc = slapi_entry_attr_find(entries[0], CHANGELOGDIRATTR, &attr);
    if (rc || NULL == attr) {
        /* No changelog dir */
        rc = LDAP_SUCCESS;
        goto bail;
    }
    rc = slapi_attr_first_value(attr, &v);
    if (rc || NULL == v) {
        /* No changelog dir */
        rc = LDAP_SUCCESS;
        goto bail;
    }
    rc = LDAP_SUCCESS;
    s = slapi_value_get_string(v);
    if (NULL == s) {
        /* No changelog dir */
        goto bail;
    }
    *changelogdir = slapi_ch_strdup(s);
    /* Remove trailing spaces and '/' if any */
    normalize_dir(*changelogdir);
bail:
    slapi_free_search_results_internal(pb);
    slapi_pblock_destroy(pb);
    return rc;
}

/* Destination Directory is an absolute pathname */
int
dblayer_backup(struct ldbminfo *li, char *dest_dir, Slapi_Task *task)
{
    dblayer_private *priv = NULL;
    char **listA = NULL, **listB = NULL, **listi, **listj, *prefix;
    char *home_dir = NULL;
    int return_value = -1;
    char *pathname1;
    char *pathname2;
    back_txn txn;
    int cnt = 1, ok = 0;
    Object *inst_obj;
    char inst_dir[MAXPATHLEN];
    char *inst_dirp = NULL;
    char *changelogdir = NULL;

    PR_ASSERT(NULL != li);
    priv = (dblayer_private*)li->li_dblayer_private;
    PR_ASSERT(NULL != priv);
    home_dir = dblayer_get_home_dir(li, NULL);
    if (NULL == home_dir || '\0' == *home_dir)
    {
        LDAPDebug0Args(LDAP_DEBUG_ANY, 
                       "Backup: missing db home directory info\n");
        return return_value;
    }

    /*
     * What are we doing here ?
     * We want to copy into the backup directory:
     * All the backend instance dir / database files;
     * All the logfiles
     * The version file
     */

    /* changed in may 1999 for political correctness.
     * 1. take checkpoint
     * 2. open transaction
     * 3. get list of logfiles (A)
     * 4. copy the db# files
     * 5. get list of logfiles (B)
     * 6. if !(A in B), goto 3
     *    (logfiles were flushed during our backup)
     * 7. copy logfiles from list B
     * 8. abort transaction
     * 9. backup index config info
     */

    /* Order of checkpointing and txn creation reversed to work
     * around DB problem. If we don't do it this way around DB
     * thinks all old transaction logs are required for recovery
     * when the DB environment has been newly created (such as
     * after an import).
     */

    /* do a quick checkpoint */
    dblayer_force_checkpoint(li);
    dblayer_txn_init(li,&txn);
    return_value=dblayer_txn_begin(li,NULL,&txn);  
    if (return_value) {
        LDAPDebug0Args(LDAP_DEBUG_ANY, 
                       "Backup: transaction error\n");
        return return_value;
    }

    if ( g_get_shutdown() || c_get_shutdown() ) {
        LDAPDebug0Args(LDAP_DEBUG_ANY, "Backup aborted\n");
        return_value = -1;
        goto bail;
    }

    /* repeat this until the logfile sets match... */
    do {
        /* get the list of logfiles currently existing */
        if (priv->dblayer_enable_transactions) {
            return_value = LOG_ARCHIVE(priv->dblayer_env->dblayer_DB_ENV,
                &listA, DB_ARCH_LOG, (void *)slapi_ch_malloc);
            if (return_value || (listA == NULL)) {
                LDAPDebug0Args(LDAP_DEBUG_ANY,
                               "Backup: log archive error\n");
                if (task) {
                    slapi_task_log_notice(task, "Backup: log archive error\n");
                }
                return_value = -1;
                goto bail;
            }
        } else {
            ok=1;
        }
        if ( g_get_shutdown() || c_get_shutdown() ) {
            LDAPDebug0Args(LDAP_DEBUG_ANY, "Backup aborted\n");
            return_value = -1;
            goto bail;
        }

        for (inst_obj = objset_first_obj(li->li_instance_set); inst_obj;
             inst_obj = objset_next_obj(li->li_instance_set, inst_obj))
        {
            ldbm_instance *inst = (ldbm_instance *)object_get_data(inst_obj);
            inst_dirp = dblayer_get_full_inst_dir(inst->inst_li, inst,
                                                  inst_dir, MAXPATHLEN);
            if ((NULL == inst_dirp) || ('\0' == *inst_dirp)) {
                LDAPDebug0Args(LDAP_DEBUG_ANY, 
                               "Backup: Instance dir is empty\n");
                if (task) {
                    slapi_task_log_notice(task,
                                          "Backup: Instance dir is empty\n");
                }
                return_value = -1;
                goto bail;
            }
            return_value = dblayer_copy_directory(li, task, inst_dirp,
                                                  dest_dir, 0 /* backup */,
                                                  &cnt, 0, 0, 0);
            if (return_value) {
                LDAPDebug(LDAP_DEBUG_ANY,
                          "Backup: error in copying directory "
                          "(%s -> %s): err=%d\n",
                          inst_dirp, dest_dir, return_value);
                if (task) {
                    slapi_task_log_notice(task,
                          "Backup: error in copying directory "
                          "(%s -> %s): err=%d\n",
                          inst_dirp, dest_dir, return_value);
                }
                if (inst_dirp != inst_dir)
                    slapi_ch_free_string(&inst_dirp);
                goto bail;
            }
            if (inst_dirp != inst_dir)
                slapi_ch_free_string(&inst_dirp);
        }
        /* Get changelogdir, if any */
        _dblayer_get_changelogdir(li, &changelogdir);
        if (changelogdir) {
            /* dest dir for changelog: dest_dir/repl_changelog_backup  */
            char *changelog_destdir = slapi_ch_smprintf("%s/%s",
                                                 dest_dir, CHANGELOG_BACKUPDIR);
            return_value = dblayer_copy_directory(li, task, changelogdir,
                                                  changelog_destdir,
                                                  0 /* backup */,
                                                  &cnt, 0, 0, 0);
            if (return_value) {
                LDAPDebug(LDAP_DEBUG_ANY,
                          "Backup: error in copying directory "
                          "(%s -> %s): err=%d\n",
                          changelogdir, changelog_destdir, return_value);
                if (task) {
                    slapi_task_log_notice(task,
                          "Backup: error in copying directory "
                          "(%s -> %s): err=%d\n",
                          changelogdir, changelog_destdir, return_value);
                }
                slapi_ch_free_string(&changelog_destdir);
                goto bail;
            }
            /* Copy DBVERSION */
            pathname1 = slapi_ch_smprintf("%s/%s",
                                         changelogdir, DBVERSION_FILENAME);
            pathname2 = slapi_ch_smprintf("%s/%s",
                                         changelog_destdir, DBVERSION_FILENAME);
            return_value = dblayer_copyfile(pathname1, pathname2,
                                            0, priv->dblayer_file_mode);
            slapi_ch_free_string(&pathname1);
            slapi_ch_free_string(&pathname1);
            slapi_ch_free_string(&changelog_destdir);
        }
        if (priv->dblayer_enable_transactions) {
            /* now, get the list of logfiles that still exist */
            return_value = LOG_ARCHIVE(priv->dblayer_env->dblayer_DB_ENV,
                &listB, DB_ARCH_LOG, (void *)slapi_ch_malloc);
            if (return_value || (listB == NULL)) {
                LDAPDebug0Args(LDAP_DEBUG_ANY,
                               "Backup: can't get list of logs\n");
                goto bail;
            }
            
            /* compare: make sure everything in list A is still in list B */
            ok = 1;
            for (listi = listA; listi && *listi && ok; listi++) {
                int found = 0;
                for (listj = listB; listj && *listj && !found; listj++) {
                    if (strcmp(*listi, *listj) == 0) {
                        found = 1;
                        break;
                    }
                }
                if (! found) {
                    ok = 0;     /* missing log: start over */
                    LDAPDebug1Arg(LDAP_DEBUG_ANY,
                                  "WARNING: Log %s has been swiped "
                                  "out from under me! (retrying)\n", *listi);
                    if (task) {
                        slapi_task_log_notice(task,
                          "WARNING: Log %s has been swiped out from under me! "
                          "(retrying)", *listi);
                    }
                }
            }
            
            if ( g_get_shutdown() || c_get_shutdown() ) {
                LDAPDebug0Args(LDAP_DEBUG_ANY, "Backup aborted\n");
                return_value = -1;
                goto bail;
            }

            if (ok) {
                size_t p1len, p2len;
                char **listptr;
                
                prefix = NULL;
                if ((NULL != priv->dblayer_log_directory) && 
                    (0 != strlen(priv->dblayer_log_directory))) {
                    prefix = priv->dblayer_log_directory;
                } else {
                    prefix = home_dir;
                }
                /* log files have the same filename len(100 is a safety net:) */
                p1len = strlen(prefix) + strlen(*listB) + 100;
                pathname1 = (char *)slapi_ch_malloc(p1len);
                p2len = strlen(dest_dir) + strlen(*listB) + 100;
                pathname2 = (char *)slapi_ch_malloc(p2len);
                /* We copy those over */
                for (listptr = listB; listptr && *listptr && ok; ++listptr) {
                    PR_snprintf(pathname1, p1len, "%s/%s", prefix, *listptr);
                    PR_snprintf(pathname2, p2len, "%s/%s", dest_dir, *listptr);
                    LDAPDebug2Args(LDAP_DEBUG_ANY, "Backing up file %d (%s)\n",
                        cnt, pathname2);
                    if (task)
                    {
                        slapi_task_log_notice(task,
                                     "Backing up file %d (%s)", cnt, pathname2);
                        slapi_task_log_status(task,
                                     "Backing up file %d (%s)", cnt, pathname2);
                    }
                    return_value = dblayer_copyfile(pathname1, pathname2,
                        0, priv->dblayer_file_mode);
                    if (0 > return_value) {
                        LDAPDebug2Args(LDAP_DEBUG_ANY, "Backup: error in "
                            "copying file '%s' (err=%d) -- Starting over...\n",
                            pathname1, return_value);
                        if (task) {
                            slapi_task_log_notice(task,
                                "Error copying file '%s' (err=%d) -- Starting "
                                "over...", pathname1, return_value);
                        }
                        ok = 0;
                    }
                    if ( g_get_shutdown() || c_get_shutdown() ) {
                        LDAPDebug0Args(LDAP_DEBUG_ANY, "Backup aborted\n");
                        return_value = -1;
                        slapi_ch_free((void **)&pathname1);
                        slapi_ch_free((void **)&pathname2);
                        goto bail;
                    }
                    cnt++;
                }
                slapi_ch_free((void **)&pathname1);
                slapi_ch_free((void **)&pathname2);
            }
            
            slapi_ch_free((void **)&listA);
            slapi_ch_free((void **)&listB);
        }
    } while (!ok);

    /* now copy the version file */
    pathname1 = slapi_ch_smprintf("%s/%s", home_dir, DBVERSION_FILENAME);
    pathname2 = slapi_ch_smprintf("%s/%s", dest_dir, DBVERSION_FILENAME);
    LDAPDebug2Args(LDAP_DEBUG_ANY, "Backing up file %d (%s)\n", cnt, pathname2);
    if (task) {
        slapi_task_log_notice(task, "Backing up file %d (%s)", cnt, pathname2);
        slapi_task_log_status(task, "Backing up file %d (%s)", cnt, pathname2);
    }
    return_value =
             dblayer_copyfile(pathname1, pathname2, 0, priv->dblayer_file_mode);
    if (return_value) {
        LDAPDebug(LDAP_DEBUG_ANY,
                  "Backup: error in copying version file "
                  "(%s -> %s): err=%d\n",
                  pathname1, pathname2, return_value);
        if (task) {
            slapi_task_log_notice(task,
                  "Backup: error in copying version file "
                  "(%s -> %s): err=%d\n",
                  pathname1, pathname2, return_value);
        }
    }
    slapi_ch_free((void **)&pathname1);
    slapi_ch_free((void **)&pathname2);

    /* Lastly we tell log file truncation to start again */

    if (0 == return_value)  /* if everything went well, backup the index conf */
        return_value = dse_conf_backup(li, dest_dir);
bail:
    slapi_ch_free((void **)&listA);
    slapi_ch_free((void **)&listB);
    dblayer_txn_abort(li,&txn);
    slapi_ch_free_string(&changelogdir);
    return return_value;
}


/* 
 * Restore is pretty easy. 
 * We delete the current database.
 * We then copy all the files over from the backup point.
 * We then leave them there for the slapd process to pick up and do the recovery
 * (which it will do as it sees no guard file).
 */

/* Helper function first */

static int dblayer_is_logfilename(const char* path)
{
    int ret = 0;
    /* Is the filename at least 4 characters long ? */
    if (strlen(path) < 4) 
    {
        return 0; /* Not a log file then */
    }
    /* Are the first 4 characters "log." ? */
    ret = strncmp(path,"log.",4);
    if (0 == ret) 
    {
        /* Now, are the last 4 characters _not_ .db# ? */
        const char *piece = path + (strlen(path) - 4);
        ret = strcmp(piece,LDBM_FILENAME_SUFFIX);
        if (0 != ret) 
        {
            /* Is */
            return 1;
        }
    }
    return 0; /* Is not */
}

/* remove log.xxx from log directory*/
static
int dblayer_delete_transaction_logs(const char * log_dir)
{
    int rc=0;
    char filename1[MAXPATHLEN];
    PRDir *dirhandle = NULL;
    dirhandle = PR_OpenDir(log_dir);
    if (NULL != dirhandle) {
        PRDirEntry *direntry = NULL;
        int is_a_logfile = 0;
        int pre=0; 
        PRFileInfo info ;
            
        while (NULL != (direntry =
                        PR_ReadDir(dirhandle, PR_SKIP_DOT | PR_SKIP_DOT_DOT)))
        {
            if (NULL == direntry->name) {
                /* NSPR doesn't behave like the docs say it should */
                LDAPDebug(LDAP_DEBUG_ANY, "PR_ReadDir failed (%d): %s\n", 
                PR_GetError(),slapd_pr_strerror(PR_GetError()), 0);
                break;
            }
            PR_snprintf(filename1, MAXPATHLEN, "%s/%s", log_dir, direntry->name);
            pre = PR_GetFileInfo(filename1, &info);
            if (pre == PR_SUCCESS && PR_FILE_DIRECTORY == info.type) {
                continue;
            }
            is_a_logfile = dblayer_is_logfilename(direntry->name);
            if (is_a_logfile && (NULL != log_dir) && (0 != strlen(log_dir)) )
            {
                LDAPDebug(LDAP_DEBUG_ANY, "Deleting log file: (%s)\n",
                          filename1, 0, 0);
                unlink(filename1);
            }
        }
        PR_CloseDir(dirhandle);
    }
    else if (PR_FILE_NOT_FOUND_ERROR != PR_GetError())
    {
        LDAPDebug(LDAP_DEBUG_ANY,
            "dblayer_delete_transaction_logs: PR_OpenDir(%s) failed (%d): %s\n",
             log_dir, PR_GetError(),slapd_pr_strerror(PR_GetError()));
        rc=1;
    }
    return rc;
}

const char *skip_list[] =
{
    ".ldif",
    NULL
};

static int doskip(const char *filename)
{
    const char **p;
    int len = strlen(filename);

    for (p = skip_list; p && *p; p++)
    {
        int n = strlen(*p);
        if (0 == strncmp(filename + len - n, *p, n))
            return 1;
    }
    return 0;
}

static int dblayer_copy_dirand_contents(char* src_dir, char* dst_dir, int mode, Slapi_Task *task)
{
  int return_value  = 0;
  int tmp_rval;
  char filename1[MAXPATHLEN];
  char filename2[MAXPATHLEN];
  PRDir *dirhandle = NULL;
  PRDirEntry *direntry = NULL;
  PRFileInfo info;

  dirhandle = PR_OpenDir(src_dir);
  if (NULL != dirhandle) 
  {

	while (NULL != (direntry = PR_ReadDir(dirhandle, PR_SKIP_DOT | PR_SKIP_DOT_DOT))) 
	{
      if (NULL == direntry->name) {
         /* NSPR doesn't behave like the docs say it should */
        break;
      }


       PR_snprintf(filename1, MAXPATHLEN, "%s/%s", src_dir, direntry->name);
       PR_snprintf(filename2, MAXPATHLEN, "%s/%s", dst_dir, direntry->name);
       LDAPDebug(LDAP_DEBUG_ANY, "Moving file %s\n",
                          filename2, 0, 0);
      /* Is this entry a directory? */
      tmp_rval = PR_GetFileInfo(filename1, &info);
      if (tmp_rval == PR_SUCCESS && PR_FILE_DIRECTORY == info.type) 
	  {
		 PR_MkDir(filename2,NEWDIR_MODE);
		   return_value = dblayer_copy_dirand_contents(filename1, filename2,
                                               mode,task);
			if (return_value) 
			{
				if (task) 
				{
					slapi_task_log_notice(task,
					"Failed to copy directory %s", filename1);
				}
				break;
			}
	   } else {
			if (task) 
			{
				slapi_task_log_notice(task, "Moving file %s",
                                          filename2);
				slapi_task_log_status(task, "Moving file %s",
                                          filename2);
			}
			return_value = dblayer_copyfile(filename1, filename2, 0,
                                               mode);
	   }
       if (0 > return_value)
         break;
                
    }
  }
  return return_value;
}

static int dblayer_fri_trim(char *fri_dir_path, char* bename)
{
	int retval = 0;
	int tmp_rval;
	char filename[MAXPATHLEN];
	PRDir *dirhandle = NULL;
	PRDirEntry *direntry = NULL;
	PRFileInfo info;

	dirhandle = PR_OpenDir(fri_dir_path);
	if (NULL != dirhandle) 
	{

		while (NULL != (direntry = PR_ReadDir(dirhandle, PR_SKIP_DOT | PR_SKIP_DOT_DOT))) 
		{
			if (NULL == direntry->name) {
			/* NSPR doesn't behave like the docs say it should */
			break;
			}

			PR_snprintf(filename, MAXPATHLEN, "%s/%s", fri_dir_path, direntry->name);

			/* Is this entry a directory? */
			tmp_rval = PR_GetFileInfo(filename, &info);
			if (tmp_rval == PR_SUCCESS && PR_FILE_DIRECTORY == info.type)
			{
				if(strcmp(direntry->name,bename)!=0)
				{
					LDAPDebug(LDAP_DEBUG_ANY, "Removing file %s from staging area\n",
                         filename, 0, 0);
					ldbm_delete_dirs(filename);
				}
				continue;
			}

			if ((strcmp(direntry->name,"DBVERSION") == 0)||
				    (strncmp(direntry->name,"__",2) == 0)||
					(strncmp(direntry->name,"log",3) == 0)){
				LDAPDebug(LDAP_DEBUG_ANY, "Removing file %s from staging area\n",
                         filename, 0, 0);
				PR_Delete(filename);
			}

		}
	}
	PR_CloseDir(dirhandle);
	return retval;
}

/* Recover a stand-alone environment , used in filesystem replica intialization restore */
static int dblayer_recover_environment_path(char *dbhome_dir, dblayer_private *priv)
{
	int retval = 0;
	DB_ENV *env = NULL;
	/* Make an environment for recovery */
	retval = dblayer_make_private_recovery_env(dbhome_dir, priv, &env);
	if (retval) {
		goto error;
	}
	if (env) {
		retval = env->close(env,0);
		if (retval) {
		}
	}
error:
	return retval;
}


static int dblayer_fri_restore(char *home_dir, char *src_dir, dblayer_private *priv, Slapi_Task *task, char** new_src_dir, char* bename)
{
		int retval = 0;
		char *fribak_dir_path = NULL;
		char *fribak_dir_name = "fribak";
		int mode = priv->dblayer_file_mode;

		*new_src_dir = NULL;


		/* First create the recovery directory */
		fribak_dir_path = slapi_ch_smprintf("%s/../%s",home_dir,fribak_dir_name);
		if((-1 == PR_MkDir(fribak_dir_path,NEWDIR_MODE)))
		{
		  LDAPDebug(LDAP_DEBUG_ANY, "dblayer_fri_restore: %s exists\n",fribak_dir_path, 0, 0);
		  LDAPDebug(LDAP_DEBUG_ANY, "dblayer_fri_restore: Removing %s.\n",fribak_dir_path, 0, 0);
		  retval = ldbm_delete_dirs(fribak_dir_path);
		  if (retval)
		  {
			LDAPDebug(LDAP_DEBUG_ANY, "dblayer_fri_restore: Removal of %s failed!\n", fribak_dir_path, 0, 0);
			goto error;
		  }
		  PR_MkDir(fribak_dir_path,NEWDIR_MODE);
		  if (retval != PR_SUCCESS)
		  {
			LDAPDebug(LDAP_DEBUG_ANY, "dblayer_fri_restore: Creation of %s failed!\n", fribak_dir_path, 0, 0);
			goto error;
		  }
		}
		/* Next copy over the entire backup file set to the recovery directory */
		/* We do this because we want to run recovery there, and we need all the files for that */
		retval = dblayer_copy_dirand_contents(src_dir, fribak_dir_path, mode, task);
		if (retval) 
		{
			LDAPDebug(LDAP_DEBUG_ANY, "dblayer_fri_restore: Copy contents to %s failed!\n", fribak_dir_path, 0, 0);
			goto error;
		}
		/* Next, run recovery on the files */
		retval = dblayer_recover_environment_path(fribak_dir_path, priv);
		if (retval) 
		{
			LDAPDebug(LDAP_DEBUG_ANY, "dblayer_fri_restore: Recovery failed!\n", 0, 0, 0);
			goto error;
		}
		/* Files nicely recovered, next we stip out what we don't need from the backup set */
		retval = dblayer_fri_trim(fribak_dir_path,bename);
		if (retval) 
		{
			LDAPDebug(LDAP_DEBUG_ANY, "dblayer_fri_restore: Trim failed!\n", 0, 0, 0);
			goto error;
		}
		*new_src_dir = fribak_dir_path;
	error:
		return retval;
}

/* Destination Directory is an absolute pathname */

int dblayer_restore(struct ldbminfo *li, char *src_dir, Slapi_Task *task, char *bename)
{
    dblayer_private *priv = NULL;
    int return_value = 0;
    int tmp_rval;
    char filename1[MAXPATHLEN];
    char filename2[MAXPATHLEN];
    PRDir *dirhandle = NULL;
    PRDirEntry *direntry = NULL;
    PRFileInfo info;
    ldbm_instance *inst = NULL;
    int seen_logfiles = 0;      /* Tells us if we restored any logfiles */
    int is_a_logfile = 0;
    int dbmode;
    int action = 0;
    char *home_dir = NULL;
    char *real_src_dir = NULL;
    int frirestore = 0; /* Is a an FRI/single instance restore. 0 for no, 1 for yes */
    struct stat sbuf;
    char *changelogdir = NULL;
    char *restore_dir = NULL;
    char *prefix = NULL;
    int cnt = 1;

    PR_ASSERT(NULL != li);
    priv = (dblayer_private*)li->li_dblayer_private;
    PR_ASSERT(NULL != priv);

    /* DBDB this is a hack, take out later */
    PR_Lock(li->li_config_mutex);
    /* dblayer_home_directory is freed in dblayer_post_close.
     * li_directory needs to live beyond dblayer. */
    priv->dblayer_home_directory = slapi_ch_strdup(li->li_directory);
    priv->dblayer_cachesize = li->li_dbcachesize;
    priv->dblayer_ncache = li->li_dbncache;
    priv->dblayer_file_mode = li->li_mode;
    PR_Unlock(li->li_config_mutex);

    home_dir = dblayer_get_home_dir(li, NULL);
    
    if (NULL == home_dir || '\0' == *home_dir)
    {
        LDAPDebug0Args(LDAP_DEBUG_ANY, 
                       "Restore: missing db home directory info\n");
        return -1;
    }

    /* We find out if slapd is running */
    /* If it is, we fail */
    /* We check on the source staging area, no point in going further if it
     * isn't there */
    if (stat(src_dir, &sbuf) < 0) {
        LDAPDebug1Arg(LDAP_DEBUG_ANY, "Restore: backup directory %s does not "
                      "exist.\n", src_dir);
        if (task) {
            slapi_task_log_notice(task, "Restore: backup directory %s does not "
                      "exist.\n", src_dir);
        }
        return LDAP_UNWILLING_TO_PERFORM;
    } else if (!S_ISDIR(sbuf.st_mode)) {
        LDAPDebug1Arg(LDAP_DEBUG_ANY, "Restore: backup directory %s is not "
                      "a directory.\n", src_dir);
        if (task) {
            slapi_task_log_notice(task, "Restore: backup directory %s is not "
                      "a directory.\n", src_dir);
        }
        return LDAP_UNWILLING_TO_PERFORM;
    }
    if (!dbversion_exists(li, src_dir)) {
        LDAPDebug1Arg(LDAP_DEBUG_ANY, "Restore: backup directory %s does not "
                      "contain a complete backup\n", src_dir);
        if (task) {
            slapi_task_log_notice(task, "Restore: backup directory %s does not "
                                  "contain a complete backup", src_dir );
        }
        return LDAP_UNWILLING_TO_PERFORM;
    }

    /* If this is a FRI restore, the bename will be non-NULL */
    if (bename != NULL)
        frirestore = 1;

    /*
     * Check if the target is a superset of the backup.
     * If not don't restore any db at all, otherwise
     * the target will be crippled.
     */
    dirhandle = PR_OpenDir(src_dir);
    if (NULL != dirhandle)
    {
        while ((direntry = PR_ReadDir(dirhandle, PR_SKIP_DOT | PR_SKIP_DOT_DOT))
            && direntry->name)
        {
            PR_snprintf(filename1, sizeof(filename1), "%s/%s",
                                                      src_dir, direntry->name);
            if(!frirestore || strcmp(direntry->name,bename)==0)
            {
                tmp_rval = PR_GetFileInfo(filename1, &info);
                if (tmp_rval == PR_SUCCESS && PR_FILE_DIRECTORY == info.type) {
                    /* Is it CHANGELOG_BACKUPDIR? */
                    if (0 == strcmp(CHANGELOG_BACKUPDIR, direntry->name)) {
                        /* Yes, this is a changelog backup. */
                        /* Get the changelog path */
                        _dblayer_get_changelogdir(li, &changelogdir);
                        continue;
                    }
                    inst = ldbm_instance_find_by_name(li, (char *)direntry->name);
                    if ( inst == NULL)
                    {
                        LDAPDebug1Arg(LDAP_DEBUG_ANY,
                                "Restore: target server has no %s configured\n",
                                direntry->name);
                        if (task) {
                            slapi_task_log_notice(task,
                                "Restore: target server has no %s configured\n",
                                direntry->name);
                        }
                        PR_CloseDir(dirhandle);
                        return_value = LDAP_UNWILLING_TO_PERFORM;
                        goto error_out;
                    }

                    if (slapd_comp_path(src_dir, inst->inst_parent_dir_name)
                        == 0) {
                        LDAPDebug2Args(LDAP_DEBUG_ANY,
                                "Restore: backup dir %s and target dir %s "
                                "are identical\n",
                                src_dir, inst->inst_parent_dir_name);
                        if (task) {
                            slapi_task_log_notice(task,
                                "Restore: backup dir %s and target dir %s "
                                "are identical\n",
                                src_dir, inst->inst_parent_dir_name);
                        }
                        PR_CloseDir(dirhandle);
                        return_value = LDAP_UNWILLING_TO_PERFORM;
                        goto error_out;
                    }
                }
            }
        }
        PR_CloseDir(dirhandle);
    }

    /* We delete the existing database */
    /* changelogdir is taken care only when it's not NULL. */
    return_value = dblayer_delete_database_ex(li, bename, changelogdir);
    if (return_value) {
        goto error_out;
    }

    if (frirestore) /*if we are restoring a single backend*/
    {
        char *new_src_dir = NULL;
        return_value = dblayer_fri_restore(home_dir,src_dir,priv,task,&new_src_dir,bename);
        if (return_value) {
            goto error_out;
        }
        /* Now modify the src_dir to point to our recovery area and carry on as if nothing had happened... */
        real_src_dir = new_src_dir;
    } else 
    {
        /* Otherwise use the src_dir from the caller */
        real_src_dir = src_dir;
    }

    /* We copy the files over from the staging area */
    /* We want to treat the logfiles specially: if there's
     * a log file directory configured, copy the logfiles there
     * rather than to the db dirctory */
    dirhandle = PR_OpenDir(real_src_dir);
    if (NULL == dirhandle) {
        LDAPDebug1Arg(LDAP_DEBUG_ANY,
            "Restore: failed to open the directory \"%s\"\n", real_src_dir);
        if (task) {
            slapi_task_log_notice(task,
                "Restore: failed to open the directory \"%s\"\n", real_src_dir);
        }
        return_value = -1;
        goto error_out;
    }
    
    while (NULL !=
           (direntry = PR_ReadDir(dirhandle, PR_SKIP_DOT | PR_SKIP_DOT_DOT))) {
        if (NULL == direntry->name) {
            /* NSPR doesn't behave like the docs say it should */
            break;
        }

        /* Is this entry a directory? */
        PR_snprintf(filename1, sizeof(filename1), "%s/%s",
                                                  real_src_dir, direntry->name);
        tmp_rval = PR_GetFileInfo(filename1, &info);
        if (tmp_rval == PR_SUCCESS && PR_FILE_DIRECTORY == info.type) {
            /* This is an instance directory. It contains the *.db#
             * files for the backend instance.
             * restore directory is supposed to be where the backend
             * directory is located.
             */
            if (0 == strcmp(CHANGELOG_BACKUPDIR, direntry->name)) {
                if (changelogdir) {
                    char *cldirname = PL_strrchr(changelogdir, '/');
                    char *p = filename1 + strlen(filename1);
                    if (NULL == cldirname) {
                        LDAPDebug1Arg(LDAP_DEBUG_ANY,
                              "Restore: broken changelog dir path %s\n",
                              changelogdir);
                        if (task) {
                            slapi_task_log_notice(task,
                                 "Restore: broken changelog dir path %s\n",
                                 changelogdir);
                        }
                        goto error_out;
                    }
                    PR_snprintf(p, sizeof(filename1) - (p - filename1),
                                    "/%s", cldirname+1);
                    /* Get the parent dir of changelogdir */
                    *cldirname = '\0';
                    return_value = dblayer_copy_directory(li, task, filename1,
                                  changelogdir, 1 /* restore */, &cnt, 0, 0, 0);
                    *cldirname = '/';
                    if (return_value) {
                        LDAPDebug1Arg(LDAP_DEBUG_ANY,
                              "Restore: failed to copy directory %s\n",
                              filename1);
                        if (task) {
                            slapi_task_log_notice(task,
                                "Restore: failed to copy directory %s", 
                                filename1);
                        }
                        goto error_out;
                    }
                    /* Copy DBVERSION */
                    p = filename1 + strlen(filename1);
                    PR_snprintf(p, sizeof(filename1) - (p - filename1),
                                              "/%s", DBVERSION_FILENAME);
                    PR_snprintf(filename2, sizeof(filename2), "%s/%s",
                                              changelogdir, DBVERSION_FILENAME);
                    return_value = dblayer_copyfile(filename1, filename2,
                                                    0, priv->dblayer_file_mode);
                }
                continue;
            }

            inst = ldbm_instance_find_by_name(li, (char *)direntry->name);
            if (inst == NULL)
                continue;

            restore_dir = inst->inst_parent_dir_name;
            /* If we're doing a partial restore, we need to reset the LSNs on the data files */
            if (dblayer_copy_directory(li, task, filename1,
                restore_dir, 1 /* restore */, &cnt, 0, 0, (bename) ? 1 : 0) == 0)
                continue;
            else
            {
                LDAPDebug1Arg(LDAP_DEBUG_ANY,
                              "Restore: failed to copy directory %s\n",
                              filename1);
                if (task) {
                    slapi_task_log_notice(task,
                        "Restore: failed to copy directory %s", filename1);
                }
                goto error_out;
            }
        }

        if (doskip(direntry->name))
            continue;

        /* Is this a log file ? */
        /* Log files have names of the form "log.xxxxx" */
        /* We detect these by looking for the prefix "log." and
         * the lack of the ".db#" suffix */
        is_a_logfile = dblayer_is_logfilename(direntry->name);
        if (is_a_logfile) {
            seen_logfiles = 1;
        }
        if (is_a_logfile && (NULL != priv->dblayer_log_directory) &&
            (0 != strlen(priv->dblayer_log_directory)) ) {
            prefix = priv->dblayer_log_directory;
        } else {
            prefix = home_dir;
        }
        mkdir_p(prefix, 0700);
        PR_snprintf(filename1, sizeof(filename1), "%s/%s",
                                                  real_src_dir, direntry->name);
        PR_snprintf(filename2, sizeof(filename2), "%s/%s",
                                                  prefix, direntry->name);
        LDAPDebug2Args(LDAP_DEBUG_ANY, "Restoring file %d (%s)\n",
                       cnt, filename2);
        if (task) {
            slapi_task_log_notice(task, "Restoring file %d (%s)",
                                  cnt, filename2);
            slapi_task_log_status(task, "Restoring file %d (%s)",
                                  cnt, filename2);
        }
        return_value = dblayer_copyfile(filename1, filename2, 0,
                                        priv->dblayer_file_mode);
        if (0 > return_value) {
            goto error_out;
        }
        cnt++;
    }
    PR_CloseDir(dirhandle);

    /* We're done ! */

    /* [605024] check the DBVERSION and reset idl-switch if needed */
    if (dbversion_exists(li, home_dir))
    {
        char *ldbmversion = NULL;
        char *dataversion = NULL;

        if (dbversion_read(li, home_dir, &ldbmversion, &dataversion) != 0)
        {
            LDAPDebug1Arg(LDAP_DEBUG_ANY, "Warning: Unable to read dbversion "
                          "file in %s\n", home_dir);
        }
        else
        {
            adjust_idl_switch(ldbmversion, li);
            slapi_ch_free_string(&ldbmversion);
            slapi_ch_free_string(&ldbmversion);
        }
    }

    return_value = check_db_version(li, &action);
    if (action & (DBVERSION_UPGRADE_3_4|DBVERSION_UPGRADE_4_4))
    {
        dbmode = DBLAYER_CLEAN_RECOVER_MODE;/* upgrade: remove logs & recover */
    }
    else if (seen_logfiles)
    {
        dbmode = DBLAYER_RESTORE_MODE;
    }
    else if (action & DBVERSION_NEED_DN2RDN)
    {
        LDAPDebug2Args(LDAP_DEBUG_ANY, 
            "%s is on, while the instance %s is in the DN format. "
            "Please run dn2rdn to convert the database format.\n",
            CONFIG_ENTRYRDN_SWITCH, inst->inst_name);
        return_value = -1;
        goto error_out;
    }
    else if (action & DBVERSION_NEED_RDN2DN)
    {
        LDAPDebug2Args(LDAP_DEBUG_ANY, 
            "%s is off, while the instance %s is in the RDN format. "
            "Please change the value to on in dse.ldif.\n",
            CONFIG_ENTRYRDN_SWITCH, inst->inst_name);
        return_value = -1;
        goto error_out;
    }
    else 
    {
        dbmode = DBLAYER_RESTORE_NO_RECOVERY_MODE;
    }

    /* now start the database code up, to prevent recovery next time the 
     * server starts;
     * dse_conf_verify may need to have db started, as well. */
    /* If no logfiles were stored, then fatal recovery isn't required */

    if (li->li_flags & SLAPI_TASK_RUNNING_FROM_COMMANDLINE)
    {
        /* command line mode; no need to run db threads */
        dbmode |= DBLAYER_NO_DBTHREADS_MODE;
    }
    else /* on-line mode */
    {
        allinstance_set_not_busy(li);
    }

    tmp_rval = dblayer_start(li, dbmode);
    if (0 != tmp_rval) {
        LDAPDebug0Args(LDAP_DEBUG_ANY,
                       "Restore: failed to init database\n");
        if (task) {
            slapi_task_log_notice(task, "Restore: failed to init database");
        }
        return_value = tmp_rval;
        goto error_out;
    }

    if (0 == return_value) { /* only when the copyfile succeeded */
        /* check the DSE_* files, if any */
        tmp_rval = dse_conf_verify(li, real_src_dir, bename);
        if (0 != tmp_rval)
            LDAPDebug0Args(LDAP_DEBUG_ANY,
                        "Warning: Unable to verify the index configuration\n");
    }

    if (li->li_flags & SLAPI_TASK_RUNNING_FROM_COMMANDLINE) {
        /* command line: close the database down again */
        tmp_rval = dblayer_close(li, dbmode);
        if (0 != tmp_rval) {
            LDAPDebug0Args(LDAP_DEBUG_ANY,
                           "Restore: Failed to close database\n");
        }
    } else {
        allinstance_set_busy(li); /* on-line mode */
    }

    return_value = tmp_rval?tmp_rval:return_value;

error_out:
    /* Free the restore src dir, but only if we allocated it above */
    if (real_src_dir && (real_src_dir != src_dir)) {
        /* If this was an FRI restore and the staging area exists, go ahead and remove it */
        if (frirestore && PR_Access(real_src_dir, PR_ACCESS_EXISTS) == PR_SUCCESS)
        {
            int ret1 = 0;
            LDAPDebug1Arg(LDAP_DEBUG_ANY,
                          "Restore: Removing staging area %s.\n", real_src_dir);
            ret1 = ldbm_delete_dirs(real_src_dir);
            if (ret1)
            {
                LDAPDebug1Arg(LDAP_DEBUG_ANY,
                              "Restore: Removal of staging area %s failed!\n",
                              real_src_dir);
            }
        }
        slapi_ch_free_string(&real_src_dir);
    }
    slapi_ch_free_string(&changelogdir);
    return return_value;
}

/*
 * inst_dir_name is a relative path  (from 6.21)
 *     ==> txn log stores relative paths and becomes relocatable
 * if full path is given, parent dir is inst_parent_dir_name;
 * otherwise, inst_dir in home_dir
 *
 * Set an appropriate path to inst_dir_name, if not yet.
 * Create the specified directory, if not exists.
 */
int dblayer_get_instance_data_dir(backend *be)
{
    ldbm_instance *inst = (ldbm_instance *)be->be_instance_info;
    char *full_namep = NULL;
    char full_name[MAXPATHLEN];
    PRDir *db_dir = NULL;
    int ret = -1;

    /* if a specific directory name was specified for this particular 
     * instance use it othewise use the ldbm-wide one 
     */
    full_namep = dblayer_get_full_inst_dir(inst->inst_li, inst,
                                           full_name, MAXPATHLEN);
    if (!full_namep || !*full_namep) {
        return ret;
    }
    /* Does this directory already exist? */
    if ((db_dir = PR_OpenDir(full_namep)) != NULL) {
        /* yep. */
        PR_CloseDir(db_dir);
        ret = 0;
    } else {
        /* nope -- create it. */
       ret = mkdir_p(full_namep, 0700);
    }

    if (full_name != full_namep)
        slapi_ch_free_string(&full_namep);
        
    return ret;
}

char *
dblayer_strerror(int error)
{
    return db_strerror(error);
}

/* [605974] check a db region file's existence to know whether import is executed by other process or not */
#define DB_REGION_PREFIX "__db."

int
dblayer_in_import(ldbm_instance *inst)
{
    PRDir *dirhandle = NULL;
    PRDirEntry *direntry = NULL;
    char inst_dir[MAXPATHLEN];
    char *inst_dirp = NULL;
    int rval = 0;

    inst_dirp = dblayer_get_full_inst_dir(inst->inst_li, inst,
                                          inst_dir, MAXPATHLEN);
    if (!inst_dirp || !*inst_dirp)
    {
        rval = -1;
        goto done;
    }
    dirhandle = PR_OpenDir(inst_dirp);

    if (NULL == dirhandle)
        goto done;

    while (NULL != (direntry = PR_ReadDir(dirhandle, PR_SKIP_DOT | PR_SKIP_DOT_DOT))) 
    {
        if (NULL == direntry->name)
        {
            break;
        }
        if (0 ==strncmp(direntry->name, DB_REGION_PREFIX, 5))
        {
            rval = 1;
            break;
        }
    }
    PR_CloseDir(dirhandle);
done:
    if (inst_dirp != inst_dir)
        slapi_ch_free_string(&inst_dirp);
    return rval;
}

/*
 * to change the db extention (e.g., .db3 -> .db4)
 */
int dblayer_update_db_ext(ldbm_instance *inst, char *oldext, char *newext)
{
    struct attrinfo *a = NULL;
    struct ldbminfo *li = NULL;
    dblayer_private *priv = NULL;
    DB *thisdb = NULL;
    int rval = 0;
    char *ofile = NULL;
    char *nfile = NULL;
    char inst_dir[MAXPATHLEN];
    char *inst_dirp;

    if (NULL == inst)
    {
        LDAPDebug(LDAP_DEBUG_ANY,
            "update_db_ext: Null instance is passed\n", 0, 0, 0);
        return -1;    /* non zero */
    }
    li = inst->inst_li;
    priv = (dblayer_private*)li->li_dblayer_private;
    inst_dirp = dblayer_get_full_inst_dir(li, inst, inst_dir, MAXPATHLEN);
    if (NULL == inst_dirp || '\0' == *inst_dirp) {
        LDAPDebug(LDAP_DEBUG_ANY,
            "update_db_ext: instance dir is NULL\n", 0, 0, 0);
        return -1;    /* non zero */
    }
    for (a = (struct attrinfo *)avl_getfirst(inst->inst_attrs);
         NULL != a;
         a = (struct attrinfo *)avl_getnext())
    {
        PRFileInfo info;
        ofile = slapi_ch_smprintf("%s/%s%s", inst_dirp, a->ai_type, oldext);

        if (PR_GetFileInfo(ofile, &info) != PR_SUCCESS)
        {
            slapi_ch_free_string(&ofile);
            continue;
        }

        /* db->rename disable DB in it; we need to create for each */
        rval = db_create(&thisdb, priv->dblayer_env->dblayer_DB_ENV, 0);
        if (0 != rval)
        {
            LDAPDebug(LDAP_DEBUG_ANY, "db_create returned %d (%s)\n",
                    rval, dblayer_strerror(rval), 0);
            goto done;
        }
        nfile = slapi_ch_smprintf("%s/%s%s", inst_dirp, a->ai_type, newext);
        LDAPDebug(LDAP_DEBUG_TRACE, "update_db_ext: rename %s -> %s\n",
            ofile, nfile, 0);

        rval = thisdb->rename(thisdb, (const char *)ofile, NULL /* subdb */,
                                  (const char *)nfile, 0);
        if (0 != rval)
        {
            LDAPDebug(LDAP_DEBUG_ANY, "rename returned %d (%s)\n",
                rval, dblayer_strerror(rval), 0);
            LDAPDebug(LDAP_DEBUG_ANY,
                "update_db_ext: index (%s) Failed to update index %s -> %s\n",
                inst->inst_name, ofile, nfile);
            goto done;
        }
        slapi_ch_free_string(&ofile);
        slapi_ch_free_string(&nfile);
    }

    rval = db_create(&thisdb, priv->dblayer_env->dblayer_DB_ENV, 0);
    if (0 != rval)
    {
        LDAPDebug(LDAP_DEBUG_ANY, "db_create returned %d (%s)\n",
                    rval, dblayer_strerror(rval), 0);
        goto done;
    }
    ofile = slapi_ch_smprintf("%s/%s%s", inst_dirp, ID2ENTRY, oldext);
    nfile = slapi_ch_smprintf("%s/%s%s", inst_dirp, ID2ENTRY, newext);
    LDAPDebug(LDAP_DEBUG_TRACE, "update_db_ext: rename %s -> %s\n",
            ofile, nfile, 0);
    rval = thisdb->rename(thisdb, (const char *)ofile, NULL /* subdb */,
                                  (const char *)nfile, 0);
    if (0 != rval)
    {
        LDAPDebug(LDAP_DEBUG_ANY, "rename returned %d (%s)\n",
                rval, dblayer_strerror(rval), 0);
        LDAPDebug(LDAP_DEBUG_ANY,
                "update_db_ext: index (%s) Failed to update index %s -> %s\n",
                inst->inst_name, ofile, nfile);
    }
done:
    slapi_ch_free_string(&ofile);
    slapi_ch_free_string(&nfile);
    if (inst_dirp != inst_dir)
        slapi_ch_free_string(&inst_dirp);

    return rval;
}

/*
 * delete the index files belonging to the instance
 */
int dblayer_delete_indices(ldbm_instance *inst)
{
    int rval = -1;
    struct attrinfo *a = NULL;
    int i;

    if (NULL == inst)
    {
        LDAPDebug(LDAP_DEBUG_ANY,
            "update_index_ext: Null instance is passed\n", 0, 0, 0);
        return rval;
    }
    rval = 0;
    for (a = (struct attrinfo *)avl_getfirst(inst->inst_attrs), i = 0;
         NULL != a;
         a = (struct attrinfo *)avl_getnext(), i++)
    {
        rval += dblayer_erase_index_file(inst->inst_be, a, i/* chkpt; 1st time only */);
    }
    return rval;
}

void dblayer_set_recovery_required(struct ldbminfo *li)
{
    if (NULL == li || NULL == li->li_dblayer_private)
    {
        LDAPDebug(LDAP_DEBUG_ANY,"set_recovery_required: no dblayer info\n",
                  0, 0, 0);
        return;
    }
    li->li_dblayer_private->dblayer_recovery_required = 1;
}

int
ldbm_back_get_info(Slapi_Backend *be, int cmd, void **info)
{
    int rc = -1;
    if (!be || !info) {
        return rc;
    }

    switch (cmd) {
    case BACK_INFO_DBENV:
    {
        struct ldbminfo *li = (struct ldbminfo *)be->be_database->plg_private;
        if (li) {
            dblayer_private *prv = (dblayer_private*)li->li_dblayer_private;
            if (prv && prv->dblayer_env && prv->dblayer_env->dblayer_DB_ENV) {
                *(DB_ENV **)info = prv->dblayer_env->dblayer_DB_ENV;
                rc = 0;
            }
        }
        break;
    }
    case BACK_INFO_DBENV_OPENFLAGS:
    {
        struct ldbminfo *li = (struct ldbminfo *)be->be_database->plg_private;
        if (li) {
            dblayer_private *prv = (dblayer_private*)li->li_dblayer_private;
            if (prv && prv->dblayer_env) {
                *(int *)info = prv->dblayer_env->dblayer_openflags;
                rc = 0;
            }
        }
        break;
    }
    case BACK_INFO_INDEXPAGESIZE:
    {
        struct ldbminfo *li = (struct ldbminfo *)be->be_database->plg_private;
        if (li) {
            dblayer_private *prv = (dblayer_private*)li->li_dblayer_private;
            if (prv && prv->dblayer_index_page_size) {
                *(size_t *)info = prv->dblayer_index_page_size;
            } else {
                *(size_t *)info = DBLAYER_INDEX_PAGESIZE;
            }
            rc = 0;
        }
        break;
    }
    default:
        break;
    }

    return rc;
}

int
ldbm_back_set_info(Slapi_Backend *be, int cmd, void *info)
{
    int rc = -1;
    if (!be || !info) {
        return rc;
    }

    switch (cmd) {
    default:
        break;
    }

    return rc;
}