summaryrefslogtreecommitdiffstats
path: root/debian/htdig/htdig-3.2.0b6/db/db_vrfy.c
blob: 7356fd70a19e3a4280e332daa7481daad3eae0ed (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
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 2000
 *	Sleepycat Software.  All rights reserved.
 *
 * $Id: db_vrfy.c,v 1.2 2002/02/02 18:18:05 ghutchis Exp $
 */

#include "htconfig.h"

#ifndef lint
static const char revid[] = "$Id: db_vrfy.c,v 1.2 2002/02/02 18:18:05 ghutchis Exp $";
#endif /* not lint */

#ifndef NO_SYSTEM_INCLUDES
#include <sys/types.h>

#include <errno.h>
#include <string.h>
#endif

#include "db_int.h"
#include "db_page.h"
#include "db_swap.h"
#include "db_verify.h"
#include "db_ext.h"
#include "btree.h"
#include "hash.h"
#include "qam.h"

static int  __db_guesspgsize __P((DB_ENV *, DB_FH *));
static int  __db_is_valid_magicno __P((u_int32_t, DBTYPE *));
static int  __db_is_valid_pagetype __P((u_int32_t));
static int  __db_meta2pgset
		__P((DB *, VRFY_DBINFO *, db_pgno_t, u_int32_t, DB *));
static int  __db_salvage_subdbs
		__P((DB *, VRFY_DBINFO *, void *,
		int(*)(void *, const void *), u_int32_t, int *));
static int  __db_salvage_unknowns
		__P((DB *, VRFY_DBINFO *, void *,
		int (*)(void *, const void *), u_int32_t));
static int  __db_vrfy_common
		__P((DB *, VRFY_DBINFO *, PAGE *, db_pgno_t, u_int32_t));
static int  __db_vrfy_freelist __P((DB *, VRFY_DBINFO *, db_pgno_t, u_int32_t));
static int  __db_vrfy_invalid
		__P((DB *, VRFY_DBINFO *, PAGE *, db_pgno_t, u_int32_t));
static int  __db_vrfy_orderchkonly __P((DB *,
		VRFY_DBINFO *, const char *, const char *, u_int32_t));
static int  __db_vrfy_pagezero __P((DB *, VRFY_DBINFO *, DB_FH *, u_int32_t));
static int  __db_vrfy_subdbs __P((DB *, VRFY_DBINFO *, char *, u_int32_t));
static int  __db_vrfy_structure
		__P((DB *, VRFY_DBINFO *, char *, db_pgno_t, u_int32_t));
static int  __db_vrfy_walkpages
		__P((DB *, VRFY_DBINFO *, void *, int (*)(void *, const void *),
		u_int32_t));

/*
 * This is the code for DB->verify, the DB database consistency checker.
 * For now, it checks all subdatabases in a database, and verifies
 * everything it knows how to (i.e. it's all-or-nothing, and one can't
 * check only for a subset of possible problems).
 */

/*
 * CDB___db_verify --
 *	Walk the entire file page-by-page, either verifying with or without
 *	dumping in db_dump -d format, or DB_SALVAGE-ing whatever key/data
 *	pairs can be found and dumping them in standard (db_load-ready)
 *	dump format.
 *
 *	(Salvaging isn't really a verification operation, but we put it
 *	here anyway because it requires essentially identical top-level
 *	code.)
 *
 *	flags may be 0, DB_NOORDERCHK, DB_ORDERCHKONLY, or DB_SALVAGE
 *	(and optionally DB_AGGRESSIVE).
 *
 *	CDB___db_verify itself is simply a wrapper to CDB___db_verify_internal,
 *	which lets us pass appropriate equivalents to FILE * in from the
 *	non-C APIs.
 *
 * PUBLIC: int CDB___db_verify
 * PUBLIC:     __P((DB *, const char *, const char *, FILE *, u_int32_t));
 */
int
CDB___db_verify(dbp, file, database, outfile, flags)
	DB *dbp;
	const char *file, *database;
	FILE *outfile;
	u_int32_t flags;
{

	return (CDB___db_verify_internal(dbp,
	    file, database, outfile, CDB___db_verify_callback, flags));
}

/*
 * CDB___db_verify_callback --
 *	Callback function for using pr_* functions from C.
 *
 * PUBLIC: int  CDB___db_verify_callback __P((void *, const void *));
 */
int
CDB___db_verify_callback(handle, str_arg)
	void *handle;
	const void *str_arg;
{
	char *str;
	FILE *f;

	str = (char *)str_arg;
	f = (FILE *)handle;

	if (fprintf(f, str) != (int)strlen(str))
		return (EIO);

	return (0);
}

/*
 * CDB___db_verify_internal --
 *	Inner meat of CDB___db_verify.
 *
 * PUBLIC: int CDB___db_verify_internal __P((DB *, const char *,
 * PUBLIC:     const char *, void *, int (*)(void *, const void *), u_int32_t));
 */
int
CDB___db_verify_internal(dbp_orig, name, subdb, handle, callback, flags)
	DB *dbp_orig;
	const char *name, *subdb;
	void *handle;
	int (*callback) __P((void *, const void *));
	u_int32_t flags;
{
	DB *dbp;
	DB_ENV *dbenv;
	DB_FH fh, *fhp;
	PAGE *h;
	VRFY_DBINFO *vdp;
	db_pgno_t last;
	int has, ret, isbad;
	char *real_name;

	dbenv = dbp_orig->dbenv;
	vdp = NULL;
	real_name = NULL;
	ret = isbad = 0;

	memset(&fh, 0, sizeof(fh));
	fhp = &fh;

	PANIC_CHECK(dbenv);
	DB_ILLEGAL_AFTER_OPEN(dbp_orig, "verify");

#define	OKFLAGS (DB_AGGRESSIVE | DB_NOORDERCHK | DB_ORDERCHKONLY | DB_SALVAGE)
	if ((ret = CDB___db_fchk(dbenv, "DB->verify", flags, OKFLAGS)) != 0)
		return (ret);

	/*
	 * DB_SALVAGE is mutually exclusive with the other flags except
	 * DB_AGGRESSIVE.
	 */
	if (LF_ISSET(DB_SALVAGE) &&
	    (flags & ~DB_AGGRESSIVE) != DB_SALVAGE)
		return (CDB___db_ferr(dbenv, "CDB___db_verify", 1));

	if (LF_ISSET(DB_ORDERCHKONLY) && flags != DB_ORDERCHKONLY)
		return (CDB___db_ferr(dbenv, "CDB___db_verify", 1));

	if (LF_ISSET(DB_ORDERCHKONLY) && subdb == NULL) {
		CDB___db_err(dbenv, "DB_ORDERCHKONLY requires a database name");
		return (EINVAL);
	}

	/*
	 * Forbid working in an environment that uses transactions or
	 * locking;  we're going to be looking at the file freely,
	 * and while we're not going to modify it, we aren't obeying
	 * locking conventions either.
	 */
	if (TXN_ON(dbenv) || LOCKING_ON(dbenv) || LOGGING_ON(dbenv)) {
		dbp_orig->errx(dbp_orig,
	    "verify may not be used with transactions, logging, or locking");
		return (EINVAL);
		/* NOTREACHED */
	}

	/* Create a dbp to use internally, which we can close at our leisure. */
	if ((ret = CDB_db_create(&dbp, dbenv, 0)) != 0)
		goto err;

	/* Copy the supplied pagesize, which we use if the file one is bogus. */
	if (dbp_orig->pgsize >= DB_MIN_PGSIZE &&
	    dbp_orig->pgsize <= DB_MAX_PGSIZE)
		dbp->set_pagesize(dbp, dbp_orig->pgsize);

	/*
	 * We don't know how large the cache is, and if the database
	 * in question uses a small page size--which we don't know
	 * yet!--it may be uncomfortably small for the default page
	 * size [#2143].  However, the things we need temporary
	 * databases for in dbinfo are largely tiny, so using a
	 * 1024-byte pagesize is probably not going to be a big hit,
	 * and will make us fit better into small spaces.
	 */
	if ((ret = CDB___db_vrfy_dbinfo_create(dbenv, 1024, &vdp)) != 0)
		goto err;

	/* Find the real name of the file. */
	if ((ret = CDB___db_appname(dbenv,
	    DB_APP_DATA, NULL, name, 0, NULL, &real_name)) != 0)
		goto err;

	/*
	 * Our first order of business is to verify page 0, which is
	 * the metadata page for the master database of subdatabases
	 * or of the only database in the file.  We want to do this by hand
	 * rather than just calling CDB___db_open in case it's corrupt--various
	 * things in CDB___db_open might act funny.
	 *
	 * Once we know the metadata page is healthy, I believe that it's
	 * safe to open the database normally and then use the page swapping
	 * code, which makes life easier.
	 */
	if ((ret = CDB___os_open(dbenv, real_name, DB_OSO_RDONLY, 0444, fhp)) != 0)
		goto err;

	/* Verify the metadata page 0; set pagesize and type. */
	if ((ret = __db_vrfy_pagezero(dbp, vdp, fhp, flags)) != 0) {
		if (ret == DB_VERIFY_BAD)
			isbad = 1;
		else
			goto err;
	}

	/*
	 * We can assume at this point that dbp->pagesize and dbp->type are
	 * set correctly, or at least as well as they can be, and that
	 * locking, logging, and txns are not in use.  Thus we can trust
	 * the memp code not to look at the page, and thus to be safe
	 * enough to use.
	 *
	 * The dbp is not open, but the file is open in the fhp, and we
	 * cannot assume that CDB___db_open is safe.  Call CDB___db_dbenv_setup,
	 * the [safe] part of CDB___db_open that initializes the environment--
	 * and the mpool--manually.
	 */
	if ((ret = CDB___db_dbenv_setup(dbp,
	    name, DB_ODDFILESIZE | DB_RDONLY)) != 0)
		return (ret);

	/*
	 * Find out the page number of the last page in the database.
	 *
	 * XXX: This currently fails if the last page is of bad type,
	 * because it calls CDB___db_pgin and that pukes.  This is bad.
	 */
	if ((ret = CDB_memp_fget(dbp->mpf, &last, DB_MPOOL_LAST, &h)) != 0)
		goto err;
	if ((ret = CDB_memp_fput(dbp->mpf, h, 0)) != 0)
		goto err;

	vdp->last_pgno = last;

	/*
	 * DB_ORDERCHKONLY is a special case;  our file consists of
	 * several subdatabases, which use different hash, bt_compare,
	 * and/or dup_compare functions.  Consequently, we couldn't verify
	 * sorting and hashing simply by calling DB->verify() on the file.
	 * DB_ORDERCHKONLY allows us to come back and check those things;  it
	 * requires a subdatabase, and assumes that everything but that
	 * database's sorting/hashing is correct.
	 */
	if (LF_ISSET(DB_ORDERCHKONLY)) {
		ret = __db_vrfy_orderchkonly(dbp, vdp, name, subdb, flags);
		goto done;
	}

	/*
	 * When salvaging, we use a db to keep track of whether we've seen a
	 * given overflow or dup page in the course of traversing normal data.
	 * If in the end we have not, we assume its key got lost and print it
	 * with key "UNKNOWN".
	 */
	if (LF_ISSET(DB_SALVAGE)) {
		if ((ret = CDB___db_salvage_init(vdp)) != 0)
			return (ret);

		/*
		 * If we're not being aggressive, attempt to crack subdbs.
		 * "has" will indicate whether the attempt has succeeded
		 * (even in part), meaning that we have some semblance of
		 * subdbs;  on the walkpages pass, we print out
		 * whichever data pages we have not seen.
		 */
		has = 0;
		if (!LF_ISSET(DB_AGGRESSIVE) && (__db_salvage_subdbs(dbp,
		    vdp, handle, callback, flags, &has)) != 0)
			isbad = 1;

		/*
		 * If we have subdatabases, we need to signal that if
		 * any keys are found that don't belong to a subdatabase,
		 * they'll need to have an "__OTHER__" subdatabase header
		 * printed first.  Flag this.  Else, print a header for
		 * the normal, non-subdb database.
		 */
		if (has == 1)
			F_SET(vdp, SALVAGE_PRINTHEADER);
		else if ((ret = CDB___db_prheader(dbp,
		    NULL, 0, 0, handle, callback, vdp, PGNO_BASE_MD)) != 0)
			goto err;
	}

	if ((ret =
	    __db_vrfy_walkpages(dbp, vdp, handle, callback, flags)) != 0) {
		if (ret == DB_VERIFY_BAD)
			isbad = 1;
		else if (ret != 0)
			goto err;
	}

	/* If we're verifying, verify inter-page structure. */
	if (!LF_ISSET(DB_SALVAGE) && isbad == 0)
		if ((ret =
		    __db_vrfy_structure(dbp, vdp, real_name, 0, flags)) != 0) {
			if (ret == DB_VERIFY_BAD)
				isbad = 1;
			else if (ret != 0)
				goto err;
		}

	/*
	 * If we're salvaging, output with key UNKNOWN any overflow or dup pages
	 * we haven't been able to put in context.  Then destroy the salvager's
	 * state-saving database.
	 */
	if (LF_ISSET(DB_SALVAGE)) {
		if ((ret = __db_salvage_unknowns(dbp,
		    vdp, handle, callback, flags)) != 0)
			isbad = 1;
		/* No return value, since there's little we can do. */
		CDB___db_salvage_destroy(vdp);
	}

	if (0) {
err:		(void)CDB___db_err(dbenv, "%s: %s", name, CDB_db_strerror(ret));
	}

	if (LF_ISSET(DB_SALVAGE) &&
	    (has == 0 || F_ISSET(vdp, SALVAGE_PRINTFOOTER)))
		(void)CDB___db_prfooter(handle, callback);

done:	if (F_ISSET(fhp, DB_FH_VALID))
		(void)CDB___os_closehandle(fhp);
	if (dbp)
		(void)dbp->close(dbp, 0);
	if (vdp)
		(void)CDB___db_vrfy_dbinfo_destroy(vdp);
	if (real_name)
		CDB___os_freestr(real_name);

	if ((ret == 0 && isbad == 1) || ret == DB_VERIFY_FATAL)
		ret = DB_VERIFY_BAD;

	return (ret);
}

/*
 * __db_vrfy_pagezero --
 *	Verify the master metadata page.  Use seek, read, and a local buffer
 *	rather than the DB paging code, for safety.
 *
 *	Must correctly (or best-guess) set dbp->type and dbp->pagesize.
 */
static int
__db_vrfy_pagezero(dbp, vdp, fhp, flags)
	DB *dbp;
	VRFY_DBINFO *vdp;
	DB_FH *fhp;
	u_int32_t flags;
{
	DBMETA *meta;
	DB_ENV *dbenv;
	VRFY_PAGEINFO *pip;
	db_pgno_t freelist;
	int t_ret, ret, nr, swapped;
	u_int8_t mbuf[DBMETASIZE];

	swapped = ret = t_ret = 0;
	freelist = 0;
	dbenv = dbp->dbenv;
	meta = (DBMETA *)mbuf;
	dbp->type = DB_UNKNOWN;

	/*
	 * Seek to the metadata page.
	 * Note that if we're just starting a verification, dbp->pgsize
	 * may be zero;  this is okay, as we want page zero anyway and
	 * 0*0 == 0.
	 */
	if ((ret = CDB___os_seek(dbenv, fhp, 0, 0, 0, 0, DB_OS_SEEK_SET)) != 0)
		goto err;

	if ((ret = CDB___os_read(dbenv, fhp, mbuf, DBMETASIZE, (size_t *)&nr)) != 0)
		goto err;

	if (nr != DBMETASIZE) {
		EPRINT((dbp->dbenv,
		    "Incomplete metadata page %lu", PGNO_BASE_MD));
		t_ret = DB_VERIFY_FATAL;
		goto err;
	}

	/*
	 * Check all of the fields that we can.
	 */

	/* 08-11: Current page number.  Must == pgno. */
	/* Note that endianness doesn't matter--it's zero. */
	if (meta->pgno != PGNO_BASE_MD) {
		EPRINT((dbp->dbenv, "Bad pgno: was %lu, should be %lu",
		    meta->pgno, PGNO_BASE_MD));
		ret = DB_VERIFY_BAD;
	}

	/* 12-15: Magic number.  Must be one of valid set. */
	if (__db_is_valid_magicno(meta->magic, &dbp->type))
		swapped = 0;
	else {
		M_32_SWAP(meta->magic);
		if (__db_is_valid_magicno(meta->magic,
		    &dbp->type))
			swapped = 1;
		else {
			EPRINT((dbp->dbenv, "Bad magic no.: %lu", meta->magic));
			ret = DB_VERIFY_BAD;
		}
	}

	/*
	 * 16-19: Version.  Must be current;  for now, we
	 * don't support verification of old versions.
	 */
	if (swapped)
		M_32_SWAP(meta->version);
	if ((dbp->type == DB_BTREE && meta->version != DB_BTREEVERSION) ||
	    (dbp->type == DB_HASH && meta->version != DB_HASHVERSION) ||
	    (dbp->type == DB_QUEUE && meta->version != DB_QAMVERSION)) {
		ret = DB_VERIFY_BAD;
		EPRINT((dbp->dbenv, "%s%s", "Old or incorrect DB ",
		    "version; extraneous errors may result"));
	}

	/*
	 * 20-23: Pagesize.  Must be power of two,
	 * greater than 512, and less than 64K.
	 */
	if (swapped)
		M_32_SWAP(meta->pagesize);
	if (IS_VALID_PAGESIZE(meta->pagesize))
		dbp->pgsize = meta->pagesize;
	else {
		EPRINT((dbp->dbenv, "Bad page size: %lu",
		    meta->pagesize));
		ret = DB_VERIFY_BAD;

		/*
		 * Now try to settle on a pagesize to use.
		 * If the user-supplied one is reasonable,
		 * use it;  else, guess.
		 */
		if (!IS_VALID_PAGESIZE(dbp->pgsize))
			dbp->pgsize = __db_guesspgsize(dbenv, fhp);
	}

	/*
	 * 25: Page type.  Must be correct for dbp->type,
	 * which is by now set as well as it can be.
	 */
	/* Needs no swapping--only one byte! */
	if ((dbp->type == DB_BTREE && meta->type != P_BTREEMETA) ||
	    (dbp->type == DB_HASH && meta->type != P_HASHMETA) ||
	    (dbp->type == DB_QUEUE && meta->type != P_QAMMETA)) {
		ret = DB_VERIFY_BAD;
		EPRINT((dbp->dbenv, "Bad page type: %lu", meta->type));
	}

	/*
	 * 28-31: Free list page number.
	 * We'll verify its sensibility when we do inter-page
	 * verification later;  for now, just store it.
	 */
	if (swapped)
	    M_32_SWAP(meta->free);
	freelist = meta->free;

	/*
	 * Initialize vdp->pages to fit a single pageinfo structure for
	 * this one page.  We'll realloc later when we know how many
	 * pages there are.
	 */
	if ((ret = CDB___db_vrfy_getpageinfo(vdp, PGNO_BASE_MD, &pip)) != 0)
		return (ret);
	pip->pgno = PGNO_BASE_MD;
	pip->type = meta->type;

	/*
	 * Signal that we still have to check the info specific to
	 * a given type of meta page.
	 */
	F_SET(pip, VRFY_INCOMPLETE);

	pip->free = freelist;

	if ((ret = CDB___db_vrfy_putpageinfo(vdp, pip)) != 0)
		return (ret);

	if (0) {
err:		CDB___db_err(dbenv, "%s", CDB_db_strerror(ret));
	}

	if (swapped == 1)
		F_SET(dbp, DB_AM_SWAP);
	if (t_ret != 0)
		ret = t_ret;
	return (ret);
}

/*
 * __db_vrfy_walkpages --
 *	Main loop of the verifier/salvager.  Walks through,
 *	page by page, and verifies all pages and/or prints all data pages.
 */
static int
__db_vrfy_walkpages(dbp, vdp, handle, callback, flags)
	DB *dbp;
	VRFY_DBINFO *vdp;
	void *handle;
	int (*callback) __P((void *, const void *));
	u_int32_t flags;
{
	DB_ENV *dbenv;
	PAGE *h;
	db_pgno_t i;
	int ret, t_ret, isbad;

	ret = isbad = t_ret = 0;
	dbenv = dbp->dbenv;

	if ((ret = CDB___db_fchk(dbenv,
	    "__db_vrfy_walkpages", flags, OKFLAGS)) != 0)
		return (ret);

	for (i = 0; i <= vdp->last_pgno; i++) {
		/*
		 * If DB_SALVAGE is set, we inspect our database of
		 * completed pages, and skip any we've already printed in
		 * the subdb pass.
		 */
		if (LF_ISSET(DB_SALVAGE) && (CDB___db_salvage_isdone(vdp, i) != 0))
			continue;

		/* If an individual page get fails, keep going. */
		if ((t_ret = CDB_memp_fget(dbp->mpf, &i, 0, &h)) != 0) {
			if (ret == 0)
				ret = t_ret;
			continue;
		}

		if (LF_ISSET(DB_SALVAGE)) {
			/*
			 * We pretty much don't want to quit unless a
			 * bomb hits.  May as well return that something
			 * was screwy, however.
			 */
			if ((t_ret = CDB___db_salvage(dbp,
			    vdp, i, h, handle, callback, flags)) != 0) {
				if (ret == 0)
					ret = t_ret;
				isbad = 1;
			}
		} else {
			/*
			 * Verify info common to all page
			 * types.
			 */
			if (i != PGNO_BASE_MD)
				if ((t_ret = __db_vrfy_common(dbp,
				    vdp, h, i, flags)) == DB_VERIFY_BAD)
					isbad = 1;

			switch (TYPE(h)) {
			case P_INVALID:
				t_ret = __db_vrfy_invalid(dbp,
				    vdp, h, i, flags);
				break;
			case __P_DUPLICATE:
				isbad = 1;
				EPRINT((dbp->dbenv,
				    "Old-style dup page %lu", i));
				break;
			case P_HASH:
				t_ret = CDB___ham_vrfy(dbp,
				    vdp, h, i, flags);
				break;
			case P_IBTREE:
			case P_IRECNO:
			case P_LBTREE:
			case P_LDUP:
				t_ret = CDB___bam_vrfy(dbp,
				    vdp, h, i, flags);
				break;
			case P_LRECNO:
				t_ret = CDB___ram_vrfy_leaf(dbp,
				    vdp, h, i, flags);
				break;
			case P_OVERFLOW:
				t_ret = CDB___db_vrfy_overflow(dbp,
				    vdp, h, i, flags);
				break;
			case P_HASHMETA:
				t_ret = CDB___ham_vrfy_meta(dbp,
				    vdp, (HMETA *)h, i, flags);
				break;
			case P_BTREEMETA:
				t_ret = CDB___bam_vrfy_meta(dbp,
				    vdp, (BTMETA *)h, i, flags);
				break;
			case P_QAMMETA:
				t_ret = CDB___qam_vrfy_meta(dbp,
				    vdp, (QMETA *)h, i, flags);
				break;
			case P_QAMDATA:
				t_ret = CDB___qam_vrfy_data(dbp,
				    vdp, (QPAGE *)h, i, flags);
				break;
			default:
				EPRINT((dbp->dbenv,
				    "Unknown page type: %lu", TYPE(h)));
				isbad = 1;
				break;
			}

			/*
			 * Set up error return.
			 */
			if (t_ret == DB_VERIFY_BAD)
				isbad = 1;
			else if (t_ret == DB_VERIFY_FATAL)
				goto err;
			else
				ret = t_ret;
		}

		if ((t_ret = CDB_memp_fput(dbp->mpf, h, 0)) != 0 && ret == 0)
			ret = t_ret;
	}

	if (0) {
err:		if ((t_ret = CDB_memp_fput(dbp->mpf, h, 0)) != 0)
			return (ret == 0 ? t_ret : ret);
		return (DB_VERIFY_BAD);
	}

	return ((isbad == 1 && ret == 0) ? DB_VERIFY_BAD : ret);
}

/*
 * __db_vrfy_structure--
 *	After a beginning-to-end walk through the database has been
 *	completed, put together the information that has been collected
 *	to verify the overall database structure.
 *
 *	Should only be called if we want to do a database verification,
 *	i.e. if DB_SALVAGE is not set.
 */
static int
__db_vrfy_structure(dbp, vdp, dbname, meta_pgno, flags)
	DB *dbp;
	VRFY_DBINFO *vdp;
	char *dbname;
	db_pgno_t meta_pgno;
	u_int32_t flags;
{
	DB *pgset;
	DB_ENV *dbenv;
	VRFY_PAGEINFO *pip;
	db_pgno_t i;
	int ret, isbad, hassubs, p;

	isbad = 0;
	pip = NULL;
	dbenv = dbp->dbenv;
	pgset = vdp->pgset;

	if ((ret = CDB___db_fchk(dbenv, "DB->verify", flags, OKFLAGS)) != 0)
		return (ret);
	if (LF_ISSET(DB_SALVAGE)) {
		CDB___db_err(dbenv, "__db_vrfy_structure called with DB_SALVAGE");
		return (EINVAL);
	}

	/*
	 * Call the appropriate function to downwards-traverse the db type.
	 */
	switch(dbp->type) {
	case DB_BTREE:
	case DB_RECNO:
		if ((ret = CDB___bam_vrfy_structure(dbp, vdp, 0, flags)) != 0) {
			if (ret == DB_VERIFY_BAD)
				isbad = 1;
			else
				goto err;
		}

		/*
		 * If we have subdatabases and we know that the database is,
		 * thus far, sound, it's safe to walk the tree of subdatabases.
		 * Do so, and verify the structure of the databases within.
		 */
		if ((ret = CDB___db_vrfy_getpageinfo(vdp, 0, &pip)) != 0)
			goto err;
		hassubs = F_ISSET(pip, VRFY_HAS_SUBDBS);
		if ((ret = CDB___db_vrfy_putpageinfo(vdp, pip)) != 0)
			goto err;

		if (isbad == 0 && hassubs)
			if ((ret =
			    __db_vrfy_subdbs(dbp, vdp, dbname, flags)) != 0) {
				if (ret == DB_VERIFY_BAD)
					isbad = 1;
				else
					goto err;
			}
		break;
	case DB_HASH:
		if ((ret = CDB___ham_vrfy_structure(dbp, vdp, 0, flags)) != 0) {
			if (ret == DB_VERIFY_BAD)
				isbad = 1;
			else
				goto err;
		}
		break;
	case DB_QUEUE:
		if ((ret = CDB___qam_vrfy_structure(dbp, vdp, flags)) != 0) {
			if (ret == DB_VERIFY_BAD)
				isbad = 1;
		}

		/*
		 * Queue pages may be unreferenced and totally zeroed, if
		 * they're empty;  queue doesn't have much structure, so
		 * this is unlikely to be wrong in any troublesome sense.
		 * Skip to "err".
		 */
		goto err;
		/* NOTREACHED */
	default:
		/* This should only happen if the verifier is somehow broken. */
		DB_ASSERT(0);
		ret = EINVAL;
		goto err;
		/* NOTREACHED */
	}

	/* Walk free list. */
	if ((ret =
	    __db_vrfy_freelist(dbp, vdp, meta_pgno, flags)) == DB_VERIFY_BAD)
		isbad = 1;

	/*
	 * If structure checks up until now have failed, it's likely that
	 * checking what pages have been missed will result in oodles of
	 * extraneous error messages being EPRINTed.  Skip to the end
	 * if this is the case;  we're going to be printing at least one
	 * error anyway, and probably all the more salient ones.
	 */
	if (ret != 0 || isbad == 1)
		goto err;

	/*
	 * Make sure no page has been missed and that no page is still marked
	 * "all zeroes" (only certain hash pages can be, and they're unmarked
	 * in CDB___ham_vrfy_structure).
	 */
	for (i = 0; i < vdp->last_pgno + 1; i++) {
		if ((ret = CDB___db_vrfy_getpageinfo(vdp, i, &pip)) != 0)
			goto err;
		if ((ret = CDB___db_vrfy_pgset_get(pgset, i, &p)) != 0)
			goto err;
		if (p == 0) {
			EPRINT((dbp->dbenv, "Unreferenced page %lu", i));
			/* isbad = 1;  */
			/* XXX: this is a db bug */
			if (pip->type != P_LRECNO && pip->type != P_LDUP)
				isbad = 1;
		}

		if (F_ISSET(pip, VRFY_IS_ALLZEROES)) {
			EPRINT((dbp->dbenv, "Totally zeroed page %lu", i));
			isbad = 1;
		}
		if ((ret = CDB___db_vrfy_putpageinfo(vdp, pip)) != 0)
			goto err;
		pip = NULL;
	}

err:	if (pip != NULL)
		(void)CDB___db_vrfy_putpageinfo(vdp, pip);

	return ((isbad == 1 && ret == 0) ? DB_VERIFY_BAD : ret);
}

/*
 * __db_is_valid_pagetype
 */
static int
__db_is_valid_pagetype(type)
	u_int32_t type;
{
	switch (type) {
	case P_INVALID:			/* Order matches ordinal value. */
	case P_HASH:
	case P_IBTREE:
	case P_IRECNO:
	case P_LBTREE:
	case P_LRECNO:
	case P_OVERFLOW:
	case P_HASHMETA:
	case P_BTREEMETA:
	case P_QAMMETA:
	case P_QAMDATA:
	case P_LDUP:
		return (1);
	}
	return (0);
}

/*
 * __db_is_valid_magicno
 */
static int
__db_is_valid_magicno(magic, typep)
	u_int32_t magic;
	DBTYPE *typep;
{
	switch (magic) {
	case DB_BTREEMAGIC:
		*typep = DB_BTREE;
		return (1);
	case DB_HASHMAGIC:
		*typep = DB_HASH;
		return (1);
	case DB_QAMMAGIC:
		*typep = DB_QUEUE;
		return (1);
	}
	*typep = DB_UNKNOWN;
	return (0);
}

/*
 * __db_vrfy_common --
 *	Verify info common to all page types.
 */
static int
__db_vrfy_common(dbp, vdp, h, pgno, flags)
	DB *dbp;
	VRFY_DBINFO *vdp;
	PAGE *h;
	db_pgno_t pgno;
	u_int32_t flags;
{
	VRFY_PAGEINFO *pip;
	int ret, t_ret;
	u_int8_t *p;

	if ((ret = CDB___db_vrfy_getpageinfo(vdp, pgno, &pip)) != 0)
		return (ret);

	pip->pgno = pgno;
	F_CLR(pip, VRFY_IS_ALLZEROES);

	/*
	 * Hash expands the table by leaving some pages between the
	 * old last and the new last totally zeroed.  Its pgin function
	 * should fix things, but we might not be using that (e.g. if
	 * we're a subdatabase).
	 */
	if (pgno != 0 && PGNO(h) == 0) {
		for (p = (u_int8_t *)h; p < (u_int8_t *)h + dbp->pgsize; p++)
			if (*p != 0) {
				EPRINT((dbp->dbenv,
				    "Hash page %lu should be zeroed and is not",
				    pgno));
				ret = DB_VERIFY_BAD;
				goto err;
			}
		/*
		 * It's totally zeroed;  mark it as a hash, and we'll
		 * check that that makes sense structurally later.
		 */
		pip->type = P_HASH;
		F_SET(pip, VRFY_IS_ALLZEROES);
		ret = 0;
		goto err;	/* well, not really an err. */
	}

	if (PGNO(h) != pgno) {
		EPRINT((dbp->dbenv,
		    "Bad page number: %lu should be %lu", h->pgno, pgno));
		ret = DB_VERIFY_BAD;
	}

	if (!__db_is_valid_pagetype(h->type)) {
		EPRINT((dbp->dbenv, "Bad page type: %lu", h->type));
		ret = DB_VERIFY_BAD;
	}
	pip->type = h->type;

err:	if ((t_ret = CDB___db_vrfy_putpageinfo(vdp, pip)) != 0 && ret == 0)
		ret = t_ret;

	return (ret);
}

/*
 * __db_vrfy_invalid --
 *	Verify P_INVALID page.
 *	(Yes, there's not much to do here.)
 */
static int
__db_vrfy_invalid(dbp, vdp, h, pgno, flags)
	DB *dbp;
	VRFY_DBINFO *vdp;
	PAGE *h;
	db_pgno_t pgno;
	u_int32_t flags;
{
	VRFY_PAGEINFO *pip;
	int ret, t_ret;

	if ((ret = CDB___db_vrfy_getpageinfo(vdp, pgno, &pip)) != 0)
		return (ret);
	pip->next_pgno = pip->prev_pgno = 0;

	if (!IS_VALID_PGNO(NEXT_PGNO(h))) {
		EPRINT((dbp->dbenv,
		    "Invalid next_pgno %lu on page %lu", NEXT_PGNO(h), pgno));
		ret = DB_VERIFY_BAD;
	} else
		pip->next_pgno = NEXT_PGNO(h);

	if ((t_ret = CDB___db_vrfy_putpageinfo(vdp, pip)) != 0 && ret == 0)
		ret = t_ret;
	return (ret);
}

/*
 * CDB___db_vrfy_datapage --
 *	Verify elements common to data pages (P_HASH, P_LBTREE,
 *	P_IBTREE, P_IRECNO, P_LRECNO, P_OVERFLOW, P_DUPLICATE)--i.e.,
 *	those defined in the PAGE structure.
 *
 *	Called from each of the per-page routines, after the
 *	all-page-type-common elements of pip have been verified and filled
 *	in.
 *
 * PUBLIC: int CDB___db_vrfy_datapage
 * PUBLIC:     __P((DB *, VRFY_DBINFO *, PAGE *, db_pgno_t, u_int32_t));
 */
int
CDB___db_vrfy_datapage(dbp, vdp, h, pgno, flags)
	DB *dbp;
	VRFY_DBINFO *vdp;
	PAGE *h;
	db_pgno_t pgno;
	u_int32_t flags;
{
	VRFY_PAGEINFO *pip;
	int isbad, ret, t_ret;

	if ((ret = CDB___db_vrfy_getpageinfo(vdp, pgno, &pip)) != 0)
		return (ret);
	isbad = 0;

	/*
	 * prev_pgno and next_pgno:  store for inter-page checks,
	 * verify that they point to actual pages and not to self.
	 *
	 * !!!
	 * Internal btree pages do not maintain these fields (indeed,
	 * they overload them).  Skip.
	 */
	if (TYPE(h) != P_IBTREE && TYPE(h) != P_IRECNO) {
		if (!IS_VALID_PGNO(PREV_PGNO(h)) || PREV_PGNO(h) == pip->pgno) {
			isbad = 1;
			EPRINT((dbp->dbenv, "Page %lu: Invalid prev_pgno %lu",
			    pip->pgno, PREV_PGNO(h)));
		}
		if (!IS_VALID_PGNO(NEXT_PGNO(h)) || NEXT_PGNO(h) == pip->pgno) {
			isbad = 1;
			EPRINT((dbp->dbenv, "Page %lu: Invalid next_pgno %lu",
			    pip->pgno, NEXT_PGNO(h)));
		}
		pip->prev_pgno = PREV_PGNO(h);
		pip->next_pgno = NEXT_PGNO(h);
	}

	/*
	 * Verify the number of entries on the page.
	 * There is no good way to determine if this is accurate;  the
	 * best we can do is verify that it's not more than can, in theory,
	 * fit on the page.  Then, we make sure there are at least
	 * this many valid elements in inp[], and hope that this catches
	 * most cases.
	 */
	if (TYPE(h) != P_OVERFLOW) {
		if (BKEYDATA_PSIZE(0) * NUM_ENT(h) > dbp->pgsize) {
			isbad = 1;
			EPRINT((dbp->dbenv,
			    "Page %lu: Too many entries: %lu",
			    pgno, NUM_ENT(h)));
		}
		pip->entries = NUM_ENT(h);
	}

	/*
	 * btree level.  Should be zero unless we're a btree;
	 * if we are a btree, should be between LEAFLEVEL and MAXBTREELEVEL,
	 * and we need to save it off.
	 */
	switch (TYPE(h)) {
	case P_IBTREE:
	case P_IRECNO:
		if (LEVEL(h) < LEAFLEVEL + 1 || LEVEL(h) > MAXBTREELEVEL) {
			isbad = 1;
			EPRINT((dbp->dbenv, "Bad btree level %lu on page %lu",
			    LEVEL(h), pgno));
		}
		pip->bt_level = LEVEL(h);
		break;
	case P_LBTREE:
	case P_LDUP:
	case P_LRECNO:
		if (LEVEL(h) != LEAFLEVEL) {
			isbad = 1;
			EPRINT((dbp->dbenv,
			    "Btree leaf page %lu has incorrect level %lu",
			    pgno, LEVEL(h)));
		}
		break;
	default:
		if (LEVEL(h) != 0) {
			isbad = 1;
			EPRINT((dbp->dbenv,
			    "Nonzero level %lu in non-btree database page %lu",
			    LEVEL(h), pgno));
		}
		break;
	}

	/*
	 * Even though inp[] occurs in all PAGEs, we look at it in the
	 * access-method-specific code, since btree and hash treat
	 * item lengths very differently, and one of the most important
	 * things we want to verify is that the data--as specified
	 * by offset and length--cover the right part of the page
	 * without overlaps, gaps, or violations of the page boundary.
	 */
	if ((t_ret = CDB___db_vrfy_putpageinfo(vdp, pip)) != 0 && ret == 0)
		ret = t_ret;

	return ((ret == 0 && isbad == 1) ? DB_VERIFY_BAD : ret);
}

/*
 * CDB___db_vrfy_meta--
 *	Verify the access-method common parts of a meta page, using
 *	normal mpool routines.
 *
 * PUBLIC: int CDB___db_vrfy_meta
 * PUBLIC:     __P((DB *, VRFY_DBINFO *, DBMETA *, db_pgno_t, u_int32_t));
 */
int
CDB___db_vrfy_meta(dbp, vdp, meta, pgno, flags)
	DB *dbp;
	VRFY_DBINFO *vdp;
	DBMETA *meta;
	db_pgno_t pgno;
	u_int32_t flags;
{
	DBTYPE dbtype, magtype;
	VRFY_PAGEINFO *pip;
	int isbad, ret, t_ret;

	isbad = 0;
	if ((ret = CDB___db_vrfy_getpageinfo(vdp, pgno, &pip)) != 0)
		return (ret);

	/* type plausible for a meta page */
	switch (meta->type) {
	case P_BTREEMETA:
		dbtype = DB_BTREE;
		break;
	case P_HASHMETA:
		dbtype = DB_HASH;
		break;
	case P_QAMMETA:
		dbtype = DB_QUEUE;
		break;
	default:
		/* The verifier should never let us get here. */
		DB_ASSERT(0);
		ret = EINVAL;
		goto err;
	}

	/* magic number valid */
	if (!__db_is_valid_magicno(meta->magic, &magtype)) {
		isbad = 1;
		EPRINT((dbp->dbenv, "Magic number invalid on page %lu", pgno));
	}
	if (magtype != dbtype) {
		isbad = 1;
		EPRINT((dbp->dbenv,
		    "Magic number does not match type of page %lu", pgno));
	}

	/* version */
	if ((dbtype == DB_BTREE && meta->version != DB_BTREEVERSION) ||
	    (dbtype == DB_HASH && meta->version != DB_HASHVERSION) ||
	    (dbtype == DB_QUEUE && meta->version != DB_QAMVERSION)) {
		isbad = 1;
		EPRINT((dbp->dbenv, "%s%s", "Old of incorrect DB ",
		    "version; extraneous errors may result"));
	}

	/* pagesize */
	if (meta->pagesize != dbp->pgsize) {
		isbad = 1;
		EPRINT((dbp->dbenv,
		    "Invalid pagesize %lu on page %lu", meta->pagesize, pgno));
	}

	/* free list */
	/* Can correctly be PGNO_INVALID--that's just the end of the list. */
	if (meta->free != PGNO_INVALID && IS_VALID_PGNO(meta->free))
		pip->free = meta->free;
	else if (!IS_VALID_PGNO(meta->free)) {
		isbad = 1;
		EPRINT((dbp->dbenv,
		    "Nonsensical free list pgno %lu on page %lu",
		    meta->free, pgno));
	}

err:	if ((t_ret = CDB___db_vrfy_putpageinfo(vdp, pip)) != 0 && ret == 0)
		ret = t_ret;

	return ((ret == 0 && isbad == 1) ? DB_VERIFY_BAD : ret);
}

/*
 * __db_vrfy_freelist --
 *	Walk free list, checking off pages and verifying absence of
 *	loops.
 */
static int
__db_vrfy_freelist(dbp, vdp, meta, flags)
	DB *dbp;
	VRFY_DBINFO *vdp;
	db_pgno_t meta;
	u_int32_t flags;
{
	DB *pgset;
	VRFY_PAGEINFO *pip;
	db_pgno_t pgno;
	int p, ret, t_ret;

	pgset = vdp->pgset;
	DB_ASSERT(pgset != NULL);

	if ((ret = CDB___db_vrfy_getpageinfo(vdp, meta, &pip)) != 0)
		return (ret);
	for (pgno = pip->free; pgno != PGNO_INVALID; pgno = pip->next_pgno) {
		if ((ret = CDB___db_vrfy_putpageinfo(vdp, pip)) != 0)
			return (ret);

		/* This shouldn't happen, but just in case. */
		if (!IS_VALID_PGNO(pgno)) {
			EPRINT((dbp->dbenv,
			    "Invalid next_pgno on free list page %lu", pgno));
			return (DB_VERIFY_BAD);
		}

		/* Detect cycles. */
		if ((ret = CDB___db_vrfy_pgset_get(pgset, pgno, &p)) != 0)
			return (ret);
		if (p != 0) {
			EPRINT((dbp->dbenv,
			    "Page %lu encountered a second time on free list",
			    pgno));
			return (DB_VERIFY_BAD);
		}
		if ((ret = CDB___db_vrfy_pgset_inc(pgset, pgno)) != 0)
			return (ret);

		if ((ret = CDB___db_vrfy_getpageinfo(vdp, pgno, &pip)) != 0)
			return (ret);

		if (pip->type != P_INVALID) {
			EPRINT((dbp->dbenv, "Non-invalid page %lu on free list",
			    pgno, pip->type));
			ret = DB_VERIFY_BAD;	  /* unsafe to continue */
			break;
		}
	}

	if ((t_ret = CDB___db_vrfy_putpageinfo(vdp, pip)) != 0)
		ret = t_ret;
	return (ret);
}

/*
 * __db_vrfy_subdbs --
 *	Walk the known-safe master database of subdbs with a cursor,
 *	verifying the structure of each subdatabase we encounter.
 */
static int
__db_vrfy_subdbs(dbp, vdp, dbname, flags)
	DB *dbp;
	VRFY_DBINFO *vdp;
	char *dbname;
	u_int32_t flags;
{
	DB *mdbp;
	DBC *dbc;
	DBT key, data;
	VRFY_PAGEINFO *pip;
	db_pgno_t meta_pgno;
	int ret, t_ret, isbad;
	u_int8_t type;

	isbad = 0;
	dbc = NULL;

	if ((ret = CDB___db_master_open(dbp, dbname, DB_RDONLY, 0, &mdbp)) != 0)
		return (ret);

	if ((ret =
	    CDB___db_icursor(mdbp, NULL, DB_BTREE, PGNO_INVALID, 0, &dbc)) != 0)
		goto err;

	memset(&key, 0, sizeof(key));
	memset(&data, 0, sizeof(data));
	while ((ret = dbc->c_get(dbc, &key, &data, DB_NEXT)) == 0) {
		if (data.size != sizeof(db_pgno_t)) {
			EPRINT((dbp->dbenv, "Database entry of invalid size"));
			isbad = 1;
			goto err;
		}
		memcpy(&meta_pgno, data.data, data.size);
		/*
		 * Subdatabase meta pgnos are stored in network byte
		 * order for cross-endian compatibility.  Swap if appropriate.
		 */
		DB_NTOHL(&meta_pgno);
		if (meta_pgno == PGNO_INVALID || meta_pgno > vdp->last_pgno) {
			EPRINT((dbp->dbenv,
			    "Database entry references invalid page %lu",
			    meta_pgno));
			isbad = 1;
			goto err;
		}
		if ((ret = CDB___db_vrfy_getpageinfo(vdp, meta_pgno, &pip)) != 0)
			goto err;
		type = pip->type;
		if ((ret = CDB___db_vrfy_putpageinfo(vdp, pip)) != 0)
			goto err;
		switch (type) {
		case P_BTREEMETA:
			if ((ret = CDB___bam_vrfy_structure(
			    dbp, vdp, meta_pgno, flags)) != 0) {
				if (ret == DB_VERIFY_BAD)
					isbad = 1;
				else
					goto err;
			}
			break;
		case P_HASHMETA:
			if ((ret = CDB___ham_vrfy_structure(
			    dbp, vdp, meta_pgno, flags)) != 0) {
				if (ret == DB_VERIFY_BAD)
					isbad = 1;
				else
					goto err;
			}
			break;
		case P_QAMMETA:
		default:
			EPRINT((dbp->dbenv,
	    "Database entry references page %lu of invalid type %lu",
			    meta_pgno, type));
			ret = DB_VERIFY_BAD;
			goto err;
			/* NOTREACHED */
		}
	}

	if (ret == DB_NOTFOUND)
		ret = 0;

err:	if (dbc != NULL && (t_ret = CDB___db_c_close(dbc)) != 0 && ret == 0)
		ret = t_ret;

	if ((t_ret = mdbp->close(mdbp, 0)) != 0 && ret == 0)
		ret = t_ret;

	return ((ret == 0 && isbad == 1) ? DB_VERIFY_BAD : ret);
}

/*
 * __db_vrfy_orderchkonly --
 *	Do an sort-order/hashing check on a known-otherwise-good subdb.
 */
static int
__db_vrfy_orderchkonly(dbp, vdp, name, subdb, flags)
	DB *dbp;
	VRFY_DBINFO *vdp;
	const char *name, *subdb;
	u_int32_t flags;
{
	BTMETA *btmeta;
	DB *mdbp, *pgset;
	DBC *pgsc;
	DBT key, data;
	HASH *h_internal;
	HMETA *hmeta;
	PAGE *h, *currpg;
	db_pgno_t meta_pgno, p, pgno;
	u_int32_t bucket;
	int t_ret, ret;

	currpg = h = NULL;
	pgsc = NULL;
	pgset = NULL;

	LF_CLR(DB_NOORDERCHK);

	/* Open the master database and get the meta_pgno for the subdb. */
	if ((ret = CDB_db_create(&mdbp, NULL, 0)) != 0)
		return (ret);
	if ((ret = CDB___db_master_open(dbp, name, DB_RDONLY, 0, &mdbp)) != 0)
		goto err;

	memset(&key, 0, sizeof(key));
	key.data = (void *)subdb;
	memset(&data, 0, sizeof(data));
	if ((ret = dbp->get(dbp, NULL, &key, &data, 0)) != 0)
		goto err;

	if (data.size != sizeof(db_pgno_t)) {
		EPRINT((dbp->dbenv, "Database entry of invalid size"));
		ret = DB_VERIFY_BAD;
		goto err;
	}

	memcpy(&meta_pgno, data.data, data.size);

	if ((ret = CDB_memp_fget(dbp->mpf, &meta_pgno, 0, &h)) != 0)
		goto err;

	if ((ret = CDB___db_vrfy_pgset(dbp->dbenv, dbp->pgsize, &pgset)) != 0)
		goto err;

	switch (TYPE(h)) {
	case P_BTREEMETA:
		btmeta = (BTMETA *)h;
		if (F_ISSET(&btmeta->dbmeta, BTM_RECNO)) {
			/* Recnos have no order to check. */
			ret = 0;
			goto err;
		}
		if ((ret =
		    __db_meta2pgset(dbp, vdp, meta_pgno, flags, pgset)) != 0)
			goto err;
		if ((ret = pgset->cursor(pgset, NULL, &pgsc, 0)) != 0)
			goto err;
		while ((ret = CDB___db_vrfy_pgset_next(pgsc, &p)) == 0) {
			if ((ret = CDB_memp_fget(dbp->mpf, &p, 0, &currpg)) != 0)
				goto err;
			if ((ret = CDB___bam_vrfy_itemorder(dbp,
			    NULL, currpg, p, NUM_ENT(currpg), 1,
			    F_ISSET(&btmeta->dbmeta, BTM_DUP), flags)) != 0)
				goto err;
			if ((ret = CDB_memp_fput(dbp->mpf, currpg, 0)) != 0)
				goto err;
			currpg = NULL;
		}
		if ((ret = pgsc->c_close(pgsc)) != 0)
			goto err;
		break;
	case P_HASHMETA:
		hmeta = (HMETA *)h;
		h_internal = (HASH *)dbp->h_internal;
		/*
		 * Make sure h_charkey is right.
		 */
		if (h_internal == NULL || h_internal->h_hash == NULL) {
			EPRINT((dbp->dbenv,
		    "DB_ORDERCHKONLY requires that a hash function be set"));
			ret = DB_VERIFY_BAD;
			goto err;
		}
		if (hmeta->h_charkey !=
		    h_internal->h_hash(CHARKEY, sizeof(CHARKEY))) {
			EPRINT((dbp->dbenv,
			    "Incorrect hash function for database"));
			ret = DB_VERIFY_BAD;
			goto err;
		}

		/*
		 * Foreach bucket, verify hashing on each page in the
		 * corresponding chain of pages.
		 */
		for (bucket = 0; bucket <= hmeta->max_bucket; bucket++) {
			pgno = hmeta->spares[CDB___db_log2(bucket + 1)];
			while (pgno != PGNO_INVALID) {
				if ((ret = CDB_memp_fget(dbp->mpf,
				    &pgno, 0, &currpg)) != 0)
					goto err;
				if ((ret = CDB___ham_vrfy_hashing(dbp,
				    NUM_ENT(currpg),hmeta, bucket, pgno,
				    flags, h_internal->h_hash)) != 0)
					goto err;
				pgno = NEXT_PGNO(currpg);
				if ((ret = CDB_memp_fput(dbp->mpf, currpg, 0)) != 0)
					goto err;
				currpg = NULL;
			}
		}
		break;
	default:
		EPRINT((dbp->dbenv, "Database meta page %lu of bad type %lu",
		    meta_pgno, TYPE(h)));
		ret = DB_VERIFY_BAD;
		break;
	}

err:	if (pgsc != NULL)
		(void)pgsc->c_close(pgsc);
	if (pgset != NULL)
		(void)pgset->close(pgset, 0);
	if (h != NULL && (t_ret = CDB_memp_fput(dbp->mpf, h, 0)) != 0)
		ret = t_ret;
	if (currpg != NULL && (t_ret = CDB_memp_fput(dbp->mpf, currpg, 0)) != 0)
		ret = t_ret;
	if ((t_ret = mdbp->close(mdbp, 0)) != 0)
		ret = t_ret;
	return (ret);
}

/*
 * CDB___db_salvage --
 *	Walk through a page, salvaging all likely or plausible (w/
 *	DB_AGGRESSIVE) key/data pairs.
 *
 * PUBLIC: int CDB___db_salvage __P((DB *, VRFY_DBINFO *, db_pgno_t, PAGE *,
 * PUBLIC:     void *, int (*)(void *, const void *), u_int32_t));
 */
int
CDB___db_salvage(dbp, vdp, pgno, h, handle, callback, flags)
	DB *dbp;
	VRFY_DBINFO *vdp;
	db_pgno_t pgno;
	PAGE *h;
	void *handle;
	int (*callback) __P((void *, const void *));
	u_int32_t flags;
{
	DB_ASSERT(LF_ISSET(DB_SALVAGE));

	/* If we got this page in the subdb pass, we can safely skip it. */
	if (CDB___db_salvage_isdone(vdp, pgno))
		return (0);

	switch (TYPE(h)) {
	case P_HASH:
		return (CDB___ham_salvage(dbp,
		    vdp, pgno, h, handle, callback, flags));
		/* NOTREACHED */
	case P_LBTREE:
		return (CDB___bam_salvage(dbp,
		    vdp, pgno, P_LBTREE, h, handle, callback, NULL, flags));
		/* NOTREACHED */
	case P_LDUP:
		return (CDB___db_salvage_markneeded(vdp, pgno, SALVAGE_LDUP));
		/* NOTREACHED */
	case P_OVERFLOW:
		return (CDB___db_salvage_markneeded(vdp, pgno, SALVAGE_OVERFLOW));
		/* NOTREACHED */
	case P_LRECNO:
		/*
		 * Recnos are tricky -- they may represent dup pages, or
		 * they may be subdatabase/regular database pages in their
		 * own right.  If the former, they need to be printed with a
		 * key, preferably when we hit the corresponding datum in
		 * a btree/hash page.  If the latter, there is no key.
		 *
		 * If a database is sufficiently frotzed, we're not going
		 * to be able to get this right, so we best-guess:  just
		 * mark it needed now, and if we're really a normal recno
		 * database page, the "unknowns" pass will pick us up.
		 */
		return (CDB___db_salvage_markneeded(vdp, pgno, SALVAGE_LRECNO));
		/* NOTREACHED */
	case P_IBTREE:
	case P_INVALID:
	case P_IRECNO:
	case __P_DUPLICATE:
	default:
		/* XXX: Should we be more aggressive here? */
		break;
	}
	return (0);
}

/*
 * __db_salvage_unknowns --
 *	Walk through the salvager database, printing with key "UNKNOWN"
 *	any pages we haven't dealt with.
 */
static int
__db_salvage_unknowns(dbp, vdp, handle, callback, flags)
	DB *dbp;
	VRFY_DBINFO *vdp;
	void *handle;
	int (*callback) __P((void *, const void *));
	u_int32_t flags;
{
	DBT unkdbt, key, *dbt;
	PAGE *h;
	db_pgno_t pgno;
	u_int32_t pgtype;
	int ret, err_ret;
	void *ovflbuf;

	memset(&unkdbt, 0, sizeof(DBT));
	unkdbt.size = strlen("UNKNOWN") + 1;
	unkdbt.data = "UNKNOWN";

	if ((ret = CDB___os_malloc(dbp->dbenv, dbp->pgsize, 0, &ovflbuf)) != 0)
		return (ret);

	err_ret = 0;
	while ((ret = CDB___db_salvage_getnext(vdp, &pgno, &pgtype)) == 0) {
		dbt = NULL;

		if ((ret = CDB_memp_fget(dbp->mpf, &pgno, 0, &h)) != 0) {
			err_ret = ret;
			continue;
		}

		switch (pgtype) {
		case SALVAGE_LDUP:
		case SALVAGE_LRECNODUP:
			dbt = &unkdbt;
			/* FALLTHROUGH */
		case SALVAGE_LBTREE:
		case SALVAGE_LRECNO:
			if ((ret = CDB___bam_salvage(dbp, vdp, pgno, pgtype,
			    h, handle, callback, dbt, flags)) != 0)
				err_ret = ret;
			break;
		case SALVAGE_OVERFLOW:
			/*
			 * XXX:
			 * This may generate multiple "UNKNOWN" keys in
			 * a database with no dups.  What to do?
			 */
			if ((ret = CDB___db_safe_goff(dbp,
			    vdp, pgno, &key, &ovflbuf, flags)) != 0) {
				err_ret = ret;
				continue;
			}
			if ((ret = CDB___db_prdbt(&key,
			    0, " ", handle, callback, 0, NULL)) != 0) {
				err_ret = ret;
				continue;
			}
			if ((ret = CDB___db_prdbt(&unkdbt,
				0, " ", handle, callback, 0, NULL)) != 0)
				err_ret = ret;
			break;
		case SALVAGE_HASH:
			if ((ret = CDB___ham_salvage(
			    dbp, vdp, pgno, h, handle, callback, flags)) != 0)
				err_ret = ret;
			break;
		case SALVAGE_INVALID:
		case SALVAGE_IGNORE:
		default:
			/*
			 * Shouldn't happen, but if it does, just do what the
			 * nice man says.
			 */
			DB_ASSERT(0);
			break;
		}
		if ((ret = CDB_memp_fput(dbp->mpf, h, 0)) != 0)
			err_ret = ret;
	}

	CDB___os_free(ovflbuf, 0);

	if (err_ret != 0 && ret == 0)
		ret = err_ret;

	return (ret == DB_NOTFOUND ? 0 : ret);
}

/*
 * Offset of the ith inp array entry, which we can compare to the offset
 * the entry stores.
 */
#define	INP_OFFSET(h, i)	\
    ((db_indx_t)((u_int8_t *)(h)->inp + (i) - (u_int8_t *)(h)))

/*
 * CDB___db_vrfy_inpitem --
 *	Verify that a single entry in the inp array is sane, and update
 *	the high water mark and current item offset.  (The former of these is
 *	used for state information between calls, and is required;  it must
 *	be initialized to the pagesize before the first call.)
 *
 *	Returns DB_VERIFY_FATAL if inp has collided with the data,
 *	since verification can't continue from there;  returns DB_VERIFY_BAD
 *	if anything else is wrong.
 *
 * PUBLIC: int CDB___db_vrfy_inpitem __P((DB *, PAGE *,
 * PUBLIC:     db_pgno_t, u_int32_t, int, u_int32_t, u_int32_t *, u_int32_t *));
 */
int
CDB___db_vrfy_inpitem(dbp, h, pgno, i, is_btree, flags, himarkp, offsetp)
	DB *dbp;
	PAGE *h;
	db_pgno_t pgno;
	u_int32_t i;
	int is_btree;
	u_int32_t flags, *himarkp, *offsetp;
{
	BKEYDATA *bk;
	db_indx_t offset, len;

	DB_ASSERT(himarkp != NULL);

	/*
	 * Check that the inp array, which grows from the beginning of the
	 * page forward, has not collided with the data, which grow from the
	 * end of the page backward.
	 */
	if ((u_int8_t *)h->inp + i >= (u_int8_t *)h + *himarkp) {
		/* We've collided with the data.  We need to bail. */
		EPRINT((dbp->dbenv,
		    "Page %lu entries listing %lu overlaps data", pgno, i));
		return (DB_VERIFY_FATAL);
	}

	offset = h->inp[i];

	/*
	 * Check that the item offset is reasonable:  it points somewhere
	 * after the inp array and before the end of the page.
	 */
	if (offset <= INP_OFFSET(h, i) || offset > dbp->pgsize) {
		EPRINT((dbp->dbenv,
		    "Bad offset %lu at page %lu index %lu", offset, pgno, i));
		return (DB_VERIFY_BAD);
	}

	/* Update the high-water mark (what HOFFSET should be) */
	if (offset < *himarkp)
		*himarkp = offset;

	if (is_btree) {
		/*
		 * Check that the item length remains on-page.
		 */
		bk = GET_BKEYDATA(h, i);
		len = B_TYPE(bk->type) == B_KEYDATA ? bk->len : BOVERFLOW_SIZE;
		if ((size_t)(offset + len) > dbp->pgsize) {
			EPRINT((dbp->dbenv,
			    "Item %lu on page %lu extends past page boundary",
			    i, pgno));
			return (DB_VERIFY_BAD);
		}
	}

	if (offsetp != NULL)
		*offsetp = offset;
	return (0);
}

/*
 * CDB___db_vrfy_duptype--
 *	Given a page number and a set of flags to CDB___bam_vrfy_subtree,
 *	verify that the dup tree type is correct--i.e., it's a recno
 *	if DUPSORT is not set and a btree if it is.
 *
 * PUBLIC: int CDB___db_vrfy_duptype
 * PUBLIC:     __P((DB *, VRFY_DBINFO *, db_pgno_t, u_int32_t));
 */
int
CDB___db_vrfy_duptype(dbp, vdp, pgno, flags)
	DB *dbp;
	VRFY_DBINFO *vdp;
	db_pgno_t pgno;
	u_int32_t flags;
{
	VRFY_PAGEINFO *pip;
	int ret, isbad;

	isbad = 0;

	if ((ret = CDB___db_vrfy_getpageinfo(vdp, pgno, &pip)) != 0)
		return (ret);

	switch (pip->type) {
	case P_IBTREE:
	case P_LDUP:
		if (!LF_ISSET(ST_DUPSORT)) {
			EPRINT((dbp->dbenv,
	    "Sorted duplicate set at page %lu in unsorted-dup database",
			    pgno));
			isbad = 1;
		}
		break;
	case P_IRECNO:
	case P_LRECNO:
		if (LF_ISSET(ST_DUPSORT)) {
			EPRINT((dbp->dbenv,
	    "Unsorted duplicate set at page %lu in sorted-dup database",
			    pgno));
			isbad = 1;
		}
		break;
	default:
		EPRINT((dbp->dbenv, "Duplicate page %lu of inappropriate type %lu",
		    pgno, pip->type));
		isbad = 1;
		break;
	}

	if ((ret = CDB___db_vrfy_putpageinfo(vdp, pip)) != 0)
		return (ret);
	return (isbad == 1 ? DB_VERIFY_BAD : 0);
}

/*
 * CDB___db_salvage_duptree --
 *	Attempt to salvage a given duplicate tree, given its alleged root.
 *
 *	The key that corresponds to this dup set has been passed to us
 *	in DBT *key.  Because data items follow keys, though, it has been
 *	printed once already.
 *
 *	The basic idea here is that pgno ought to be a P_LDUP, a P_LRECNO, a
 *	P_IBTREE, or a P_IRECNO.  If it's an internal page, use the verifier
 *	functions to make sure it's safe;  if it's not, we simply bail and the
 *	data will have to be printed with no key later on.  if it is safe,
 *	recurse on each of its children.
 *
 *	Whether or not it's safe, if it's a leaf page, CDB___bam_salvage it.
 *
 *	At all times, use the DB hanging off vdp to mark and check what we've
 *	done, so each page gets printed exactly once and we don't get caught
 *	in any cycles.
 *
 * PUBLIC: int CDB___db_salvage_duptree __P((DB *, VRFY_DBINFO *, db_pgno_t,
 * PUBLIC:     DBT *, void *, int (*)(void *, const void *), u_int32_t));
 */
int
CDB___db_salvage_duptree(dbp, vdp, pgno, key, handle, callback, flags)
	DB *dbp;
	VRFY_DBINFO *vdp;
	db_pgno_t pgno;
	DBT *key;
	void *handle;
	int (*callback) __P((void *, const void *));
	u_int32_t flags;
{
	PAGE *h;
	int ret, t_ret;

	if (pgno == PGNO_INVALID || !IS_VALID_PGNO(pgno))
		return (DB_VERIFY_BAD);

	/* We have a plausible page.  Try it. */
	if ((ret = CDB_memp_fget(dbp->mpf, &pgno, 0, &h)) != 0)
		return (ret);

	switch (TYPE(h)) {
	case P_IBTREE:
	case P_IRECNO:
		if ((ret = __db_vrfy_common(dbp, vdp, h, pgno, flags)) != 0)
			goto err;
		if ((ret = CDB___bam_vrfy(dbp,
		    vdp, h, pgno, flags | DB_NOORDERCHK)) != 0 ||
		    (ret = CDB___db_salvage_markdone(vdp, pgno)) != 0)
			goto err;
		/*
		 * We have a known-healthy internal page.  Walk it.
		 */
		if ((ret = CDB___bam_salvage_walkdupint(dbp, vdp, h, key,
		    handle, callback, flags)) != 0)
			goto err;
		break;
	case P_LRECNO:
	case P_LDUP:
		if ((ret = CDB___bam_salvage(dbp,
		    vdp, pgno, TYPE(h), h, handle, callback, key, flags)) != 0)
			goto err;
		break;
	default:
		ret = DB_VERIFY_BAD;
		goto err;
		/* NOTREACHED */
	}

err:	if ((t_ret = CDB_memp_fput(dbp->mpf, h, 0)) != 0 && ret == 0)
		ret = t_ret;
	return (ret);
}

/*
 * __db_salvage_subdbs --
 *	Check and see if this database has subdbs;  if so, try to salvage
 *	them independently.
 */
static int
__db_salvage_subdbs(dbp, vdp, handle, callback, flags, hassubsp)
	DB *dbp;
	VRFY_DBINFO *vdp;
	void *handle;
	int (*callback) __P((void *, const void *));
	u_int32_t flags;
	int *hassubsp;
{
	BTMETA *btmeta;
	DB *pgset;
	DBC *pgsc;
	PAGE *h;
	db_pgno_t p, meta_pgno;
	int ret, err_ret;

	err_ret = 0;
	pgsc = NULL;
	pgset = NULL;

	meta_pgno = PGNO_BASE_MD;
	if ((ret = CDB_memp_fget(dbp->mpf, &meta_pgno, 0, &h)) != 0)
		return (ret);

	if (TYPE(h) == P_BTREEMETA)
		btmeta = (BTMETA *)h;
	else {
		/* Not a btree metadata, ergo no subdbs, so just return. */
		ret = 0;
		goto err;
	}

	/* If it's not a safe page, bail on the attempt. */
	if ((ret = __db_vrfy_common(dbp, vdp, h, PGNO_BASE_MD, flags)) != 0 ||
	   (ret = CDB___bam_vrfy_meta(dbp, vdp, btmeta, PGNO_BASE_MD, flags)) != 0)
		goto err;

	if (!F_ISSET(&btmeta->dbmeta, BTM_SUBDB)) {
		/* No subdbs, just return. */
		ret = 0;
		goto err;
	}

	/* We think we've got subdbs.  Mark it so. */
	*hassubsp = 1;

	if ((ret = CDB_memp_fput(dbp->mpf, h, 0)) != 0)
		return (ret);

	/*
	 * We have subdbs.  Try to crack them.
	 *
	 * To do so, get a set of leaf pages in the master
	 * database, and then walk each of the valid ones, salvaging
	 * subdbs as we go.  If any prove invalid, just drop them;  we'll
	 * pick them up on a later pass.
	 */
	if ((ret = CDB___db_vrfy_pgset(dbp->dbenv, dbp->pgsize, &pgset)) != 0)
		return (ret);
	if ((ret =
	    __db_meta2pgset(dbp, vdp, PGNO_BASE_MD, flags, pgset)) != 0)
		goto err;

	if ((ret = pgset->cursor(pgset, NULL, &pgsc, 0)) != 0)
		goto err;
	while ((ret = CDB___db_vrfy_pgset_next(pgsc, &p)) == 0) {
		if ((ret = CDB_memp_fget(dbp->mpf, &p, 0, &h)) != 0) {
			err_ret = ret;
			continue;
		}
		if ((ret = __db_vrfy_common(dbp, vdp, h, p, flags)) != 0 ||
		    (ret = CDB___bam_vrfy(dbp,
		    vdp, h, p, flags | DB_NOORDERCHK)) != 0)
			goto nextpg;
		if (TYPE(h) != P_LBTREE)
			goto nextpg;
		else if ((ret = CDB___db_salvage_subdbpg(
		    dbp, vdp, h, handle, callback, flags)) != 0)
			err_ret = ret;
nextpg:		if ((ret = CDB_memp_fput(dbp->mpf, h, 0)) != 0)
			err_ret = ret;
	}

	if (ret != DB_NOTFOUND)
		goto err;
	if ((ret = pgsc->c_close(pgsc)) != 0)
		goto err;

	ret = pgset->close(pgset, 0);
	return ((ret == 0 && err_ret != 0) ? err_ret : ret);

	/* NOTREACHED */

err:	if (pgsc != NULL)
		(void)pgsc->c_close(pgsc);
	if (pgset != NULL)
		(void)pgset->close(pgset, 0);
	(void)CDB_memp_fput(dbp->mpf, h, 0);
	return (ret);
}

/*
 * CDB___db_salvage_subdbpg --
 *	Given a known-good leaf page in the master database, salvage all
 *	leaf pages corresponding to each subdb.
 *
 * PUBLIC: int CDB___db_salvage_subdbpg
 * PUBLIC:     __P((DB *, VRFY_DBINFO *, PAGE *, void *,
 * PUBLIC:     int (*)(void *, const void *), u_int32_t));
 */
int
CDB___db_salvage_subdbpg(dbp, vdp, master, handle, callback, flags)
	DB *dbp;
	VRFY_DBINFO *vdp;
	PAGE *master;
	void *handle;
	int (*callback) __P((void *, const void *));
	u_int32_t flags;
{
	BKEYDATA *bkkey, *bkdata;
	BOVERFLOW *bo;
	DB *pgset;
	DBC *pgsc;
	DBT key;
	PAGE *subpg;
	db_indx_t i;
	db_pgno_t meta_pgno, p;
	int ret, err_ret, t_ret;
	char *subdbname;

	ret = err_ret = 0;
	subdbname = NULL;

	if ((ret = CDB___db_vrfy_pgset(dbp->dbenv, dbp->pgsize, &pgset)) != 0)
		return (ret);

	/*
	 * For each entry, get and salvage the set of pages
	 * corresponding to that entry.
	 */
	for (i = 0; i < NUM_ENT(master); i += P_INDX) {
		bkkey = GET_BKEYDATA(master, i);
		bkdata = GET_BKEYDATA(master, i + O_INDX);

		/* Get the subdatabase name. */
		if (B_TYPE(bkkey->type) == B_OVERFLOW) {
			/*
			 * We can, in principle anyway, have a subdb
			 * name so long it overflows.  Ick.
			 */
			bo = (BOVERFLOW *)bkkey;
			if ((ret = CDB___db_safe_goff(dbp, vdp, bo->pgno, &key,
			    (void **)&subdbname, flags)) != 0) {
				err_ret = DB_VERIFY_BAD;
				continue;
			}

			/* Nul-terminate it. */
			if ((ret = CDB___os_realloc(dbp->dbenv,
			    key.size + 1, NULL, &subdbname)) != 0)
				goto err;
			subdbname[key.size] = '\0';
		} else if (B_TYPE(bkkey->type == B_KEYDATA)) {
			if ((ret = CDB___os_realloc(dbp->dbenv,
			    bkkey->len + 1, NULL, &subdbname)) != 0)
				goto err;
			memcpy(subdbname, bkkey->data, bkkey->len);
			subdbname[bkkey->len] = '\0';
		}

		/* Get the corresponding pgno. */
		if (bkdata->len != sizeof(db_pgno_t)) {
			err_ret = DB_VERIFY_BAD;
			continue;
		}
		memcpy(&meta_pgno, bkdata->data, sizeof(db_pgno_t));

		/* If we can't get the subdb meta page, just skip the subdb. */
		if (!IS_VALID_PGNO(meta_pgno) ||
		    (ret = CDB_memp_fget(dbp->mpf, &meta_pgno, 0, &subpg)) != 0) {
			err_ret = ret;
			continue;
		}

		/*
		 * Verify the subdatabase meta page.  This has two functions.
		 * First, if it's bad, we have no choice but to skip the subdb
		 * and let the pages just get printed on a later pass.  Second,
		 * the access-method-specific meta verification routines record
		 * the various state info (such as the presence of dups)
		 * that we need for CDB___db_prheader().
		 */
		if ((ret =
		    __db_vrfy_common(dbp, vdp, subpg, meta_pgno, flags)) != 0) {
			err_ret = ret;
			(void)CDB_memp_fput(dbp->mpf, subpg, 0);
			continue;
		}
		switch (TYPE(subpg)) {
		case P_BTREEMETA:
			if ((ret = CDB___bam_vrfy_meta(dbp,
			    vdp, (BTMETA *)subpg, meta_pgno, flags)) != 0) {
				err_ret = ret;
				(void)CDB_memp_fput(dbp->mpf, subpg, 0);
				continue;
			}
			break;
		case P_HASHMETA:
			if ((ret = CDB___ham_vrfy_meta(dbp,
			    vdp, (HMETA *)subpg, meta_pgno, flags)) != 0) {
				err_ret = ret;
				(void)CDB_memp_fput(dbp->mpf, subpg, 0);
				continue;
			}
			break;
		default:
			/* This isn't an appropriate page;  skip this subdb. */
			err_ret = DB_VERIFY_BAD;
			continue;
			/* NOTREACHED */
		}

		if ((ret = CDB_memp_fput(dbp->mpf, subpg, 0)) != 0) {
			err_ret = ret;
			continue;
		}

		/* Print a subdatabase header. */
		if ((ret = CDB___db_prheader(dbp,
		    subdbname, 0, 0, handle, callback, vdp, meta_pgno)) != 0)
			goto err;

		if ((ret = __db_meta2pgset(dbp, vdp, meta_pgno,
		    flags, pgset)) != 0) {
			err_ret = ret;
			continue;
		}

		if ((ret = pgset->cursor(pgset, NULL, &pgsc, 0)) != 0)
			goto err;
		while ((ret = CDB___db_vrfy_pgset_next(pgsc, &p)) == 0) {
			if ((ret = CDB_memp_fget(dbp->mpf, &p, 0, &subpg)) != 0) {
				err_ret = ret;
				continue;
			}
			if ((ret = CDB___db_salvage(dbp, vdp, p, subpg,
			    handle, callback, flags)) != 0)
				err_ret = ret;
			if ((ret = CDB_memp_fput(dbp->mpf, subpg, 0)) != 0)
				err_ret = ret;
		}

		if (ret != DB_NOTFOUND)
			goto err;

		if ((ret = pgsc->c_close(pgsc)) != 0)
			goto err;
		if ((ret = CDB___db_prfooter(handle, callback)) != 0)
			goto err;
	}
err:	if (subdbname)
		CDB___os_free(subdbname, 0);

	if ((t_ret = pgset->close(pgset, 0)) != 0)
		ret = t_ret;

	if ((t_ret = CDB___db_salvage_markdone(vdp, PGNO(master))) != 0)
		return (t_ret);

	return ((err_ret != 0) ? err_ret : ret);
}

/*
 * __db_meta2pgset --
 *	Given a known-safe meta page number, return the set of pages
 *	corresponding to the database it represents.  Return DB_VERIFY_BAD if
 *	it's not a suitable meta page or is invalid.
 */
static int
__db_meta2pgset(dbp, vdp, pgno, flags, pgset)
	DB *dbp;
	VRFY_DBINFO *vdp;
	db_pgno_t pgno;
	u_int32_t flags;
	DB *pgset;
{
	PAGE *h;
	int ret, t_ret;

	if ((ret = CDB_memp_fget(dbp->mpf, &pgno, 0, &h)) != 0)
		return (ret);

	switch (TYPE(h)) {
	case P_BTREEMETA:
		ret = CDB___bam_meta2pgset(dbp, vdp, (BTMETA *)h, flags, pgset);
		break;
	case P_HASHMETA:
		ret = CDB___ham_meta2pgset(dbp, vdp, (HMETA *)h, flags, pgset);
		break;
	default:
		ret = DB_VERIFY_BAD;
		break;
	}

	if ((t_ret = CDB_memp_fput(dbp->mpf, h, 0)) != 0)
		return (t_ret);
	return (ret);
}

/*
 * __db_guesspgsize --
 *	Try to guess what the pagesize is if the one on the meta page
 *	and the one in the db are invalid.
 */
static int
__db_guesspgsize(dbenv, fhp)
	DB_ENV *dbenv;
	DB_FH *fhp;
{
	db_pgno_t i;
	size_t nr;
	u_int32_t guess;
	u_int8_t type;
	int ret;

	for (guess = DB_MAX_PGSIZE; guess >= DB_MIN_PGSIZE; guess >>= 1) {
		/*
		 * We try to read three pages ahead after the first one
		 * and make sure we have plausible types for all of them.
		 * If the seeks fail, continue with a smaller size;
		 * we're probably just looking past the end of the database.
		 * If they succeed but the types are wrong, also continue
		 * with a size smaller;  we may be looking at pages N,
		 * 2N, and 3N for some N > 1.
		 *
		 * As soon as we hit an invalid type, we stop and return
		 * our best guess; the last one was probably the page size.
		 */
		for (i = 1; i <= 3; i++) {
			if ((ret = CDB___os_seek(dbenv, fhp, guess,
			    i, SSZ(DBMETA, type), 0, DB_OS_SEEK_SET)) != 0)
				break;
			if ((ret = CDB___os_read(dbenv,
			    fhp, &type, 1, &nr)) != 0 || nr == 0)
				break;
			if (type == P_INVALID || type >= P_PAGETYPE_MAX)
				break;
		}
	}

	return (guess);
}