summaryrefslogtreecommitdiff
path: root/src/db/db_method.c
blob: d807bab6a16a7913b8376971d289fd40706731ef (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
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 1999, 2015 Oracle and/or its affiliates.  All rights reserved.
 *
 * $Id$
 */

#include "db_config.h"

#include "db_int.h"
#include "dbinc/blob.h"
#include "dbinc/crypto.h"
#include "dbinc/db_page.h"
#include "dbinc/btree.h"
#include "dbinc/hash.h"
#include "dbinc/heap.h"
#include "dbinc/lock.h"
#include "dbinc/mp.h"
#include "dbinc/qam.h"
#include "dbinc/txn.h"

static int  __db_get_byteswapped __P((DB *, int *));
static int  __db_get_dbname __P((DB *, const char **, const char **));
static DB_ENV *__db_get_env __P((DB *));
static void __db_get_msgcall
	      __P((DB *, void (**)(const DB_ENV *, const char *)));
static DB_MPOOLFILE *__db_get_mpf __P((DB *));
static int  __db_get_multiple __P((DB *));
static int  __db_get_transactional __P((DB *));
static int  __db_get_type __P((DB *, DBTYPE *dbtype));
static int  __db_init __P((DB *, u_int32_t));
static int  __db_get_alloc __P((DB *, void *(**)(size_t),
		void *(**)(void *, size_t), void (**)(void *)));
static int  __db_set_alloc __P((DB *, void *(*)(size_t),
		void *(*)(void *, size_t), void (*)(void *)));
static int  __db_get_append_recno __P((DB *,
		int (**)(DB *, DBT *, db_recno_t)));
static int  __db_set_append_recno __P((DB *, int (*)(DB *, DBT *, db_recno_t)));
static int  __db_get_blob_dir __P((DB *, const char **));
static int  __db_set_blob_dir __P((DB *, const char *));
static int  __db_get_blob_sub_dir __P((DB *, const char **));
static int  __db_get_cachesize __P((DB *, u_int32_t *, u_int32_t *, int *));
static int  __db_set_cachesize __P((DB *, u_int32_t, u_int32_t, int));
static int  __db_get_create_dir __P((DB *, const char **));
static int  __db_set_create_dir __P((DB *, const char *));
static int  __db_get_dup_compare
		__P((DB *, int (**)(DB *, const DBT *, const DBT *, size_t *)));
static int  __db_get_encrypt_flags __P((DB *, u_int32_t *));
static int  __db_set_encrypt __P((DB *, const char *, u_int32_t));
static int  __db_get_feedback __P((DB *, void (**)(DB *, int, int)));
static int  __db_set_feedback __P((DB *, void (*)(DB *, int, int)));
static int  __db_get_lk_exclusive __P((DB *, int *, int *));
static int  __db_set_lk_exclusive __P((DB *, int));
static void __db_map_flags __P((DB *, u_int32_t *, u_int32_t *));
static int  __db_get_pagesize __P((DB *, u_int32_t *));
static int  __db_set_paniccall __P((DB *, void (*)(DB_ENV *, int)));
static int  __db_set_priority __P((DB *, DB_CACHE_PRIORITY));
static int  __db_get_priority __P((DB *, DB_CACHE_PRIORITY *));
static void __db_get_errcall __P((DB *,
	      void (**)(const DB_ENV *, const char *, const char *)));
static void __db_set_errcall
	      __P((DB *, void (*)(const DB_ENV *, const char *, const char *)));
static void __db_get_errfile __P((DB *, FILE **));
static void __db_set_errfile __P((DB *, FILE *));
static void __db_get_errpfx __P((DB *, const char **));
static void __db_set_errpfx __P((DB *, const char *));
static void __db_set_msgcall
	      __P((DB *, void (*)(const DB_ENV *, const char *)));
static void __db_get_msgfile __P((DB *, FILE **));
static void __db_set_msgfile __P((DB *, FILE *));
static int  __db_get_assoc_flags __P((DB *, u_int32_t *));
static void __dbh_err __P((DB *, int, const char *, ...));
static void __dbh_errx __P((DB *, const char *, ...));

/*
 * db_create --
 *	DB constructor.
 *
 * EXTERN: int db_create __P((DB **, DB_ENV *, u_int32_t));
 */
int
db_create(dbpp, dbenv, flags)
	DB **dbpp;
	DB_ENV *dbenv;
	u_int32_t flags;
{
	DB_THREAD_INFO *ip;
	ENV *env;
	int ret;

	ip = NULL;
	env = dbenv == NULL ? NULL : dbenv->env;

#ifdef HAVE_ERROR_HISTORY
	/* Call thread local storage initializer at least once per process. */
	if (env == NULL)
		__db_thread_init();
#endif

	/* Check for invalid function flags. */
	switch (flags) {
	case 0:
		break;
	case DB_XA_CREATE:
		if (dbenv != NULL) {
			__db_errx(env, DB_STR("0504",
	    "XA applications may not specify an environment to db_create"));
			return (EINVAL);
		}

		/*
		 * If it's an XA database, open it within the XA environment,
		 * taken from the global list of environments.  (When the XA
		 * transaction manager called our xa_start() routine the
		 * "current" environment was moved to the start of the list.
		 */
		env = TAILQ_FIRST(&DB_GLOBAL(envq));
		if (env == NULL) {
			__db_errx(env, DB_STR("0505",
			    "Cannot open XA database before XA is enabled"));
			return (EINVAL);
		}
		break;
	default:
		return (__db_ferr(env, "db_create", 0));
	}

	if (env != NULL)
		ENV_ENTER(env, ip);

	/*
	 * If we are opening an XA database, make sure we don't have a global XA
	 * transaction running.
	 */
	if (LF_ISSET(DB_XA_CREATE)) {
		XA_NO_TXN(ip, ret);
		if (ret != 0)
			goto err;
	}

	ret = __db_create_internal(dbpp, env, flags);
err:	if (env != NULL)
		ENV_LEAVE(env, ip);

	return (ret);
}

/*
 * __db_create_internal --
 *	DB constructor internal routine.
 *
 * PUBLIC: int __db_create_internal  __P((DB **, ENV *, u_int32_t));
 */
int
__db_create_internal(dbpp, env, flags)
	DB **dbpp;
	ENV *env;
	u_int32_t flags;
{
	DB *dbp;
	DB_ENV *dbenv;
	DB_REP *db_rep;
	int ret;

	*dbpp = NULL;

	/* If we don't have an environment yet, allocate a local one. */
	if (env == NULL) {
		if ((ret = db_env_create(&dbenv, 0)) != 0)
			return (ret);
		env = dbenv->env;
		F_SET(env, ENV_DBLOCAL);
	} else
		dbenv = env->dbenv;

	/* Allocate and initialize the DB handle. */
	if ((ret = __os_calloc(env, 1, sizeof(*dbp), &dbp)) != 0)
		goto err;

	dbp->dbenv = env->dbenv;
	dbp->env = env;
	if ((ret = __db_init(dbp, flags)) != 0)
		goto err;

	MUTEX_LOCK(env, env->mtx_dblist);
	++env->db_ref;
	MUTEX_UNLOCK(env, env->mtx_dblist);

	/*
	 * Set the replication timestamp; it's 0 if we're not in a replicated
	 * environment.  Don't acquire a lock to read the value, even though
	 * it's opaque: all we check later is value equality, nothing else.
	 */
	dbp->timestamp = REP_ON(env) ?
	    ((REGENV *)env->reginfo->primary)->rep_timestamp : 0;
	/*
	 * Set the replication generation number for fid management; valid
	 * replication generations start at 1.  Don't acquire a lock to
	 * read the value.  All we check later is value equality.
	 */
	db_rep = env->rep_handle;
	dbp->fid_gen = REP_ON(env) ? ((REP *)db_rep->region)->gen : 0;

	/* Open a backing DB_MPOOLFILE handle in the memory pool. */
	if ((ret = __memp_fcreate(env, &dbp->mpf)) != 0)
		goto err;

	dbp->type = DB_UNKNOWN;

	*dbpp = dbp;
	return (0);

err:	if (dbp != NULL) {
		if (dbp->mpf != NULL)
			(void)__memp_fclose(dbp->mpf, 0);
		if (F_ISSET(env, ENV_DBLOCAL))
			(void)__env_close(dbp->dbenv, 0);
		__os_free(env, dbp);
	}

	return (ret);
}

/*
 * __db_init --
 *	Initialize a DB structure.
 */
static int
__db_init(dbp, flags)
	DB *dbp;
	u_int32_t flags;
{
	int ret;
	u_int32_t bytes;

	dbp->locker = NULL;
	dbp->alt_close = NULL;
	LOCK_INIT(dbp->handle_lock);

	TAILQ_INIT(&dbp->free_queue);
	TAILQ_INIT(&dbp->active_queue);
	TAILQ_INIT(&dbp->join_queue);
	LIST_INIT(&dbp->s_secondaries);

	FLD_SET(dbp->am_ok,
	    DB_OK_BTREE | DB_OK_HASH | DB_OK_HEAP | DB_OK_QUEUE | DB_OK_RECNO);

	/* DB PUBLIC HANDLE LIST BEGIN */
	dbp->associate = __db_associate_pp;
	dbp->associate_foreign = __db_associate_foreign_pp;
	dbp->close = __db_close_pp;
	dbp->compact = __db_compact_pp;
	dbp->cursor = __db_cursor_pp;
	dbp->del = __db_del_pp;
	dbp->dump = __db_dump_pp;
	dbp->err = __dbh_err;
	dbp->errx = __dbh_errx;
	dbp->exists = __db_exists;
	dbp->fd = __db_fd_pp;
	dbp->get = __db_get_pp;
	dbp->get_alloc = __db_get_alloc;
	dbp->get_append_recno = __db_get_append_recno;
	dbp->get_assoc_flags = __db_get_assoc_flags;
	dbp->get_blob_dir = __db_get_blob_dir;
	dbp->get_blob_sub_dir = __db_get_blob_sub_dir;
	dbp->get_blob_threshold = __db_get_blob_threshold;
	dbp->get_byteswapped = __db_get_byteswapped;
	dbp->get_cachesize = __db_get_cachesize;
	dbp->get_create_dir = __db_get_create_dir;
	dbp->get_dbname = __db_get_dbname;
	dbp->get_dup_compare = __db_get_dup_compare;
	dbp->get_encrypt_flags = __db_get_encrypt_flags;
	dbp->get_env = __db_get_env;
	dbp->get_errcall = __db_get_errcall;
	dbp->get_errfile = __db_get_errfile;
	dbp->get_errpfx = __db_get_errpfx;
	dbp->get_feedback = __db_get_feedback;
	dbp->get_flags = __db_get_flags;
	dbp->get_lorder = __db_get_lorder;
	dbp->get_mpf = __db_get_mpf;
	dbp->get_msgcall = __db_get_msgcall;
	dbp->get_msgfile = __db_get_msgfile;
	dbp->get_multiple = __db_get_multiple;
	dbp->get_open_flags = __db_get_open_flags;
	dbp->get_partition_dirs = __partition_get_dirs;
	dbp->get_partition_callback = __partition_get_callback;
	dbp->get_partition_keys = __partition_get_keys;
	dbp->get_pagesize = __db_get_pagesize;
	dbp->get_priority = __db_get_priority;
	dbp->get_transactional = __db_get_transactional;
	dbp->get_type = __db_get_type;
	dbp->join = __db_join_pp;
	dbp->key_range = __db_key_range_pp;
	dbp->get_lk_exclusive = __db_get_lk_exclusive;
	dbp->set_lk_exclusive = __db_set_lk_exclusive;
	dbp->open = __db_open_pp;
	dbp->pget = __db_pget_pp;
	dbp->put = __db_put_pp;
	dbp->remove = __db_remove_pp;
	dbp->rename = __db_rename_pp;
	dbp->set_alloc = __db_set_alloc;
	dbp->set_append_recno = __db_set_append_recno;
	dbp->set_blob_dir = __db_set_blob_dir;
	dbp->set_blob_threshold = __db_set_blob_threshold;
	dbp->set_cachesize = __db_set_cachesize;
	dbp->set_create_dir = __db_set_create_dir;
	dbp->set_dup_compare = __db_set_dup_compare;
	dbp->set_encrypt = __db_set_encrypt;
	dbp->set_errcall = __db_set_errcall;
	dbp->set_errfile = __db_set_errfile;
	dbp->set_errpfx = __db_set_errpfx;
	dbp->set_feedback = __db_set_feedback;
	dbp->set_flags = __db_set_flags;
	dbp->set_lorder = __db_set_lorder;
	dbp->set_msgcall = __db_set_msgcall;
	dbp->set_msgfile = __db_set_msgfile;
	dbp->set_pagesize = __db_set_pagesize;
	dbp->set_paniccall = __db_set_paniccall;
	dbp->set_partition = __partition_set;
	dbp->set_partition_dirs = __partition_set_dirs;
	dbp->set_priority = __db_set_priority;
	dbp->sort_multiple = __db_sort_multiple;
	dbp->stat = __db_stat_pp;
	dbp->stat_print = __db_stat_print_pp;
	dbp->sync = __db_sync_pp;
	dbp->truncate = __db_truncate_pp;
	dbp->upgrade = __db_upgrade_pp;
	dbp->verify = __db_verify_pp;
	/* DB PUBLIC HANDLE LIST END */

	if ((ret = __env_get_blob_threshold_int(dbp->env, &bytes)) != 0)
		return (ret);
	dbp->blob_threshold = bytes;

	/* Access method specific. */
	if ((ret = __bam_db_create(dbp)) != 0)
		return (ret);
	if ((ret = __ham_db_create(dbp)) != 0)
		return (ret);
	if ((ret = __heap_db_create(dbp)) != 0)
		return (ret);
	if ((ret = __qam_db_create(dbp)) != 0)
		return (ret);

	COMPQUIET(flags, 0);

	return (0);
}

/*
 * __dbh_am_chk --
 *	Error if an unreasonable method is called.
 *
 * PUBLIC: int __dbh_am_chk __P((DB *, u_int32_t));
 */
int
__dbh_am_chk(dbp, flags)
	DB *dbp;
	u_int32_t flags;
{
	/*
	 * We start out allowing any access methods to be called, and as the
	 * application calls the methods the options become restricted.  The
	 * idea is to quit as soon as an illegal method combination is called.
	 */
	if ((LF_ISSET(DB_OK_BTREE) && FLD_ISSET(dbp->am_ok, DB_OK_BTREE)) ||
	    (LF_ISSET(DB_OK_HASH) && FLD_ISSET(dbp->am_ok, DB_OK_HASH)) ||
	    (LF_ISSET(DB_OK_HEAP) && FLD_ISSET(dbp->am_ok, DB_OK_HEAP)) ||
	    (LF_ISSET(DB_OK_QUEUE) && FLD_ISSET(dbp->am_ok, DB_OK_QUEUE)) ||
	    (LF_ISSET(DB_OK_RECNO) && FLD_ISSET(dbp->am_ok, DB_OK_RECNO))) {
		FLD_CLR(dbp->am_ok, ~flags);
		return (0);
	}

	__db_errx(dbp->env, DB_STR("0506",
"call implies an access method which is inconsistent with previous calls"));
	return (EINVAL);
}

/*
 * __dbh_err --
 *	Db.err method.
 */
static void
#ifdef STDC_HEADERS
__dbh_err(DB *dbp, int error, const char *fmt, ...)
#else
__dbh_err(dbp, error, fmt, va_alist)
	DB *dbp;
	int error;
	const char *fmt;
	va_dcl
#endif
{
	/* Message with error string, to stderr by default. */
	DB_REAL_ERR(dbp->dbenv, error, DB_ERROR_SET, 1, fmt);
}

/*
 * __dbh_errx --
 *	Db.errx method.
 */
static void
#ifdef STDC_HEADERS
__dbh_errx(DB *dbp, const char *fmt, ...)
#else
__dbh_errx(dbp, fmt, va_alist)
	DB *dbp;
	const char *fmt;
	va_dcl
#endif
{
	/* Message without error string, to stderr by default. */
	DB_REAL_ERR(dbp->dbenv, 0, DB_ERROR_NOT_SET, 1, fmt);
}

/*
 * __db_get_byteswapped --
 *	Return if database requires byte swapping.
 */
static int
__db_get_byteswapped(dbp, isswapped)
	DB *dbp;
	int *isswapped;
{
	DB_ILLEGAL_BEFORE_OPEN(dbp, "DB->get_byteswapped");

	*isswapped = F_ISSET(dbp, DB_AM_SWAP) ? 1 : 0;
	return (0);
}

/*
 * __db_get_dbname --
 *	Get the name of the database as passed to DB->open.
 */
static int
__db_get_dbname(dbp, fnamep, dnamep)
	DB *dbp;
	const char **fnamep, **dnamep;
{
	DB_ILLEGAL_BEFORE_OPEN(dbp, "DB->get_dbname");

	if (fnamep != NULL)
		*fnamep = dbp->fname;
	if (dnamep != NULL)
		*dnamep = dbp->dname;
	return (0);
}

/*
 * __db_get_env --
 *	Get the DB_ENV handle that was passed to db_create.
 */
static DB_ENV *
__db_get_env(dbp)
	DB *dbp;
{
	return (dbp->dbenv);
}

/*
 * __db_get_mpf --
 *	Get the underlying DB_MPOOLFILE handle.
 */
static DB_MPOOLFILE *
__db_get_mpf(dbp)
	DB *dbp;
{
	return (dbp->mpf);
}

/*
 * get_multiple --
 *	Return whether this DB handle references a physical file with multiple
 *	databases.
 */
static int
__db_get_multiple(dbp)
	DB *dbp;
{
	DB_ILLEGAL_BEFORE_OPEN(dbp, "DB->get_multiple");

	/*
	 * Only return TRUE if the handle is for the master database, not for
	 * any subdatabase in the physical file.  If it's a Btree, with the
	 * subdatabases flag set, and the meta-data page has the right value,
	 * return TRUE.  (We don't need to check it's a Btree, I suppose, but
	 * it doesn't hurt.)
	 */
	return (dbp->type == DB_BTREE &&
	    F_ISSET(dbp, DB_AM_SUBDB) &&
	    dbp->meta_pgno == PGNO_BASE_MD ? 1 : 0);
}

/*
 * get_transactional --
 *	Return whether this database was created in a transaction.
 */
static int
__db_get_transactional(dbp)
	DB *dbp;
{
	return (F_ISSET(dbp, DB_AM_TXN) ? 1 : 0);
}

/*
 * __db_get_type --
 *	Return type of underlying database.
 */
static int
__db_get_type(dbp, dbtype)
	DB *dbp;
	DBTYPE *dbtype;
{
	DB_ILLEGAL_BEFORE_OPEN(dbp, "DB->get_type");

	*dbtype = dbp->type;
	return (0);
}

/*
 * __db_get_append_recno --
 *	Get record number append routine.
 */
static int
__db_get_append_recno(dbp, funcp)
	DB *dbp;
	int (**funcp) __P((DB *, DBT *, db_recno_t));
{
	DB_ILLEGAL_METHOD(dbp, DB_OK_QUEUE | DB_OK_RECNO);
	if (funcp)
		*funcp = dbp->db_append_recno;

	return (0);
}
/*
 * __db_set_append_recno --
 *	Set record number append routine.
 */
static int
__db_set_append_recno(dbp, func)
	DB *dbp;
	int (*func) __P((DB *, DBT *, db_recno_t));
{
	DB_ILLEGAL_AFTER_OPEN(dbp, "DB->set_append_recno");
	DB_ILLEGAL_METHOD(dbp, DB_OK_QUEUE | DB_OK_RECNO);

	dbp->db_append_recno = func;

	return (0);
}

/*
 * __db_get_blob_threshold --
 *	Get the current threshold size at which records are stored as blobs.
 *
 *  PUBLIC: int __db_get_blob_threshold __P((DB *, u_int32_t *));
 */
int
__db_get_blob_threshold(dbp, bytes)
	DB *dbp;
	u_int32_t *bytes;
{
	/*
	 * While shared, this value never changes after open, so it is safe
	 * to access it without mutex protection.
	 */
	*bytes = dbp->blob_threshold;

	return (0);
}

/*
 * __db_set_blob_threshold --
 *	API to allow setting the threshold size at which records are stored
 *	as blobs rather than in database items. No flags currently supported.
 * PUBLIC: int __db_set_blob_threshold __P((DB *, u_int32_t, u_int32_t));
 */
int
__db_set_blob_threshold(dbp, bytes, flags)
	DB *dbp;
	u_int32_t bytes;
	u_int32_t flags;
{
	if (__db_fchk(dbp->env, "DB->set_blob_threshold", flags, 0) != 0)
		return (EINVAL);

	DB_ILLEGAL_AFTER_OPEN(dbp, "DB->set_blob_threshold");

	if (bytes != 0 && F_ISSET(dbp,
	    (DB_AM_CHKSUM | DB_AM_ENCRYPT | DB_AM_DUP | DB_AM_DUPSORT))) {
		__db_errx(dbp->env, DB_STR("0760",
"Cannot enable blobs in databases with checksum, encryption, or duplicates."));
		return (EINVAL);
	}
#ifdef HAVE_COMPRESSION
	if (DB_IS_COMPRESSED(dbp) && bytes != 0) {
		__db_errx(dbp->env, DB_STR("0761",
		    "Cannot enable blobs in databases with compression."));
		return (EINVAL);
	}
#endif

	dbp->blob_threshold = bytes;

	return (0);
}

/*
 * __db_blobs_enabled --
 *
 * Used to tell if the database is configured to support blobs.
 * PUBLIC: int __db_blobs_enabled __P((DB *));
 */
int
__db_blobs_enabled(dbp)
	DB *dbp;
{
	/* Blob threshold must be non-0. */
	if (!dbp->blob_threshold)
		return (0);
	/* Blobs cannot support encryption or checksum, but that may change. */
	if (F_ISSET(dbp, (DB_AM_CHKSUM | DB_AM_ENCRYPT)))
		return (0);
	/* Blobs do not support compression, but that may change. */
#ifdef HAVE_COMPRESSION
	if (DB_IS_COMPRESSED(dbp))
		return (0);
#endif
	if (dbp->env->dbenv != NULL &&
	    F_ISSET(dbp->env->dbenv, DB_ENV_TXN_SNAPSHOT))
		return (0);
	/* Cannot support blobs in recno or queue. */
	if (dbp->type == DB_RECNO || dbp->type == DB_QUEUE)
		return (0);
	/*
	 * Cannot support dups because that would require comparing
	 * blob data items.
	 */
	if (F_ISSET(dbp, (DB_AM_DUP | DB_AM_DUPSORT)))
		return (0);
	/* No place to put blob files when using an in-memory db. */
	if (F_ISSET(dbp, (DB_AM_INMEM)))
		return (0);

	/* BDB managed databases should not support blobs. */
	if ((dbp->fname != NULL && IS_DB_FILE(dbp->fname)) ||
	    (dbp->dname != NULL && IS_DB_FILE(dbp->dname)))
		return (0);

	return (1);
}

/*
 * __db_get_blob_sub_dir --
 *
 * Returns the subdirectory of the blob directory in which the blob files
 * for the given db are stored, or NULL if there is none.
 *
 */
static int
__db_get_blob_sub_dir(dbp, dir)
	DB *dbp;
	const char **dir;
{
	DB_ILLEGAL_BEFORE_OPEN(dbp, "DB->get_blob_sub_dir");

	*dir = dbp->blob_sub_dir;

	return (0);
}

/*
 * __db_get_blob_dir --
 *
 * Get the blob directory for this database.
 */
static int
__db_get_blob_dir(dbp, dir)
	DB *dbp;
	const char **dir;
{
	DB_ENV *dbenv;
	ENV *env;

	env = dbp->env;
	dbenv = dbp->env->dbenv;
	*dir = NULL;

	if (dbenv == NULL)
		return (0);

	if (dbenv->db_blob_dir != NULL)
		*dir = dbenv->db_blob_dir;
	else if (env->db_home != NULL)
		*dir = BLOB_DEFAULT_DIR;

	return (0);
}

/*
 * __db_set_blob_dir --
 *
 * Set the blob directory in a local environment.
 */
static int
__db_set_blob_dir(dbp, dir)
	DB *dbp;
	const char *dir;
{
	DB_ENV *dbenv;
	ENV *env;

	DB_ILLEGAL_IN_ENV(dbp, "DB->set_blob_dir");
	DB_ILLEGAL_AFTER_OPEN(dbp, "DB->set_blob_dir");
	env = dbp->env;
	dbenv = dbp->env->dbenv;

	if (dbenv == NULL)
		return (0);

	if (dbenv->db_blob_dir != NULL)
		__os_free(env, dbenv->db_blob_dir);
	dbenv->db_blob_dir = NULL;

	return (__os_strdup(env, dir, &dbenv->db_blob_dir));
}

/*
 * __db_get_cachesize --
 *	Get underlying cache size.
 */
static int
__db_get_cachesize(dbp, cache_gbytesp, cache_bytesp, ncachep)
	DB *dbp;
	u_int32_t *cache_gbytesp, *cache_bytesp;
	int *ncachep;
{
	DB_ILLEGAL_IN_ENV(dbp, "DB->get_cachesize");

	return (__memp_get_cachesize(dbp->dbenv,
	    cache_gbytesp, cache_bytesp, ncachep));
}

/*
 * __db_set_cachesize --
 *	Set underlying cache size.
 */
static int
__db_set_cachesize(dbp, cache_gbytes, cache_bytes, ncache)
	DB *dbp;
	u_int32_t cache_gbytes, cache_bytes;
	int ncache;
{
	DB_ILLEGAL_IN_ENV(dbp, "DB->set_cachesize");
	DB_ILLEGAL_AFTER_OPEN(dbp, "DB->set_cachesize");

	return (__memp_set_cachesize(
	    dbp->dbenv, cache_gbytes, cache_bytes, ncache));
}

static int
__db_set_create_dir(dbp, dir)
	DB *dbp;
	const char *dir;
{
	DB_ENV *dbenv;
	int i;

	dbenv = dbp->dbenv;

	for (i = 0; i < dbenv->data_next; i++)
		if (strcmp(dir, dbenv->db_data_dir[i]) == 0)
			break;

	if (i == dbenv->data_next) {
		__db_errx(dbp->env, DB_STR_A("0507",
		    "Directory %s not in environment list.", "%s"), dir);
		return (EINVAL);
	}

	dbp->dirname = dbenv->db_data_dir[i];
	return (0);
}

static int
__db_get_create_dir(dbp, dirp)
	DB *dbp;
	const char **dirp;
{
	*dirp = dbp->dirname;
	return (0);
}

/*
 * __db_get_dup_compare --
 *	Get duplicate comparison routine.
 */
static int
__db_get_dup_compare(dbp, funcp)
	DB *dbp;
	int (**funcp) __P((DB *, const DBT *, const DBT *, size_t *));
{

	DB_ILLEGAL_METHOD(dbp, DB_OK_BTREE | DB_OK_HASH);

	if (funcp != NULL) {
#ifdef HAVE_COMPRESSION
		if (DB_IS_COMPRESSED(dbp)) {
			*funcp =
			     ((BTREE *)dbp->bt_internal)->compress_dup_compare;
		} else
#endif
			*funcp = dbp->dup_compare;
	}

	return (0);
}

/*
 * __db_set_dup_compare --
 *	Set duplicate comparison routine.
 *
 * PUBLIC: int __db_set_dup_compare __P((DB *,
 * PUBLIC:     int (*)(DB *, const DBT *, const DBT *, size_t *)));
 */
int
__db_set_dup_compare(dbp, func)
	DB *dbp;
	int (*func) __P((DB *, const DBT *, const DBT *, size_t *));
{
	int ret;

	DB_ILLEGAL_AFTER_OPEN(dbp, "DB->set_dup_compare");
	DB_ILLEGAL_METHOD(dbp, DB_OK_BTREE | DB_OK_HASH);

	if ((ret = __db_set_flags(dbp, DB_DUPSORT)) != 0)
		return (ret);

#ifdef HAVE_COMPRESSION
	if (DB_IS_COMPRESSED(dbp)) {
		dbp->dup_compare = __bam_compress_dupcmp;
		((BTREE *)dbp->bt_internal)->compress_dup_compare = func;
	} else
#endif
		dbp->dup_compare = func;

	return (0);
}

/*
 * __db_get_encrypt_flags --
 */
static int
__db_get_encrypt_flags(dbp, flagsp)
	DB *dbp;
	u_int32_t *flagsp;
{
	DB_ILLEGAL_IN_ENV(dbp, "DB->get_encrypt_flags");

	return (__env_get_encrypt_flags(dbp->dbenv, flagsp));
}

/*
 * __db_set_encrypt --
 *	Set database passwd.
 */
static int
__db_set_encrypt(dbp, passwd, flags)
	DB *dbp;
	const char *passwd;
	u_int32_t flags;
{
	DB_CIPHER *db_cipher;
	int ret;

	DB_ILLEGAL_IN_ENV(dbp, "DB->set_encrypt");
	DB_ILLEGAL_AFTER_OPEN(dbp, "DB->set_encrypt");

	if ((ret = __env_set_encrypt(dbp->dbenv, passwd, flags)) != 0)
		return (ret);

	/*
	 * In a real env, this gets initialized with the region.  In a local
	 * env, we must do it here.
	 */
	db_cipher = dbp->env->crypto_handle;
	if (!F_ISSET(db_cipher, CIPHER_ANY) &&
	    (ret = db_cipher->init(dbp->env, db_cipher)) != 0)
		return (ret);

	return (__db_set_flags(dbp, DB_ENCRYPT));
}

static void
__db_get_errcall(dbp, errcallp)
	DB *dbp;
	void (**errcallp) __P((const DB_ENV *, const char *, const char *));
{
	__env_get_errcall(dbp->dbenv, errcallp);
}

static void
__db_set_errcall(dbp, errcall)
	DB *dbp;
	void (*errcall) __P((const DB_ENV *, const char *, const char *));
{
	__env_set_errcall(dbp->dbenv, errcall);
}

static void
__db_get_errfile(dbp, errfilep)
	DB *dbp;
	FILE **errfilep;
{
	__env_get_errfile(dbp->dbenv, errfilep);
}

static void
__db_set_errfile(dbp, errfile)
	DB *dbp;
	FILE *errfile;
{
	__env_set_errfile(dbp->dbenv, errfile);
}

static void
__db_get_errpfx(dbp, errpfxp)
	DB *dbp;
	const char **errpfxp;
{
	__env_get_errpfx(dbp->dbenv, errpfxp);
}

static void
__db_set_errpfx(dbp, errpfx)
	DB *dbp;
	const char *errpfx;
{
	__env_set_errpfx(dbp->dbenv, errpfx);
}

static int
__db_get_feedback(dbp, feedbackp)
	DB *dbp;
	void (**feedbackp) __P((DB *, int, int));
{
	if (feedbackp != NULL)
		*feedbackp = dbp->db_feedback;
	return (0);
}

static int
__db_set_feedback(dbp, feedback)
	DB *dbp;
	void (*feedback) __P((DB *, int, int));
{
	dbp->db_feedback = feedback;
	return (0);
}

static int
__db_get_lk_exclusive(dbp, onoff, nowait)
	DB *dbp;
	int *onoff;
	int *nowait;
{
	*onoff = (F2_ISSET(dbp, DB2_AM_EXCL) ? 1 : 0);
	*nowait = (F2_ISSET(dbp, DB2_AM_NOWAIT) ? 1 : 0);
	return (0);
}

static int
__db_set_lk_exclusive(dbp, nowait)
	DB *dbp;
	int nowait;
{
	DB_ILLEGAL_AFTER_OPEN(dbp, "DB->set_lk_exclusive");

	F2_CLR(dbp, DB2_AM_NOWAIT);
	F2_SET(dbp, (nowait ? DB2_AM_NOWAIT|DB2_AM_EXCL :
	    DB2_AM_EXCL));
	return (0);
}

/*
 * __db_map_flags --
 *	Maps between public and internal flag values.
 *      This function doesn't check for validity, so it can't fail.
 */
static void
__db_map_flags(dbp, inflagsp, outflagsp)
	DB *dbp;
	u_int32_t *inflagsp, *outflagsp;
{
	COMPQUIET(dbp, NULL);

	if (FLD_ISSET(*inflagsp, DB_CHKSUM)) {
		FLD_SET(*outflagsp, DB_AM_CHKSUM);
		FLD_CLR(*inflagsp, DB_CHKSUM);
	}
	if (FLD_ISSET(*inflagsp, DB_ENCRYPT)) {
		FLD_SET(*outflagsp, DB_AM_ENCRYPT | DB_AM_CHKSUM);
		FLD_CLR(*inflagsp, DB_ENCRYPT);
	}
	if (FLD_ISSET(*inflagsp, DB_TXN_NOT_DURABLE)) {
		FLD_SET(*outflagsp, DB_AM_NOT_DURABLE);
		FLD_CLR(*inflagsp, DB_TXN_NOT_DURABLE);
	}
}

/*
 * __db_get_assoc_flags --
 */
static int
__db_get_assoc_flags(dbp, flagsp)
	DB *dbp;
	u_int32_t *flagsp;
{
	DB_ILLEGAL_BEFORE_OPEN(dbp, "DB->get_assoc_flags");

	*flagsp = dbp->s_assoc_flags;
	return (0);
}

/*
 * __db_get_flags --
 *	The DB->get_flags method.
 *
 * PUBLIC: int __db_get_flags __P((DB *, u_int32_t *));
 */
int
__db_get_flags(dbp, flagsp)
	DB *dbp;
	u_int32_t *flagsp;
{
	static const u_int32_t db_flags[] = {
		DB_CHKSUM,
		DB_DUP,
		DB_DUPSORT,
		DB_ENCRYPT,
#ifdef HAVE_QUEUE
		DB_INORDER,
#endif
		DB_RECNUM,
		DB_RENUMBER,
		DB_REVSPLITOFF,
		DB_SNAPSHOT,
		DB_TXN_NOT_DURABLE,
		0
	};
	u_int32_t f, flags, mapped_flag;
	int i;

	flags = 0;
	for (i = 0; (f = db_flags[i]) != 0; i++) {
		mapped_flag = 0;
		__db_map_flags(dbp, &f, &mapped_flag);
		__bam_map_flags(dbp, &f, &mapped_flag);
		__ram_map_flags(dbp, &f, &mapped_flag);
#ifdef HAVE_QUEUE
		__qam_map_flags(dbp, &f, &mapped_flag);
#endif
		DB_ASSERT(dbp->env, f == 0);
		if (F_ISSET(dbp, mapped_flag) == mapped_flag)
			LF_SET(db_flags[i]);
	}

	*flagsp = flags;
	return (0);
}

/*
 * __db_set_flags --
 *	DB->set_flags.
 *
 * PUBLIC: int  __db_set_flags __P((DB *, u_int32_t));
 */
int
__db_set_flags(dbp, flags)
	DB *dbp;
	u_int32_t flags;
{
	ENV *env;
	int ret;

	env = dbp->env;

	if (LF_ISSET(DB_ENCRYPT) && !CRYPTO_ON(env)) {
		__db_errx(env, DB_STR("0508",
		    "Database environment not configured for encryption"));
		return (EINVAL);
	}
	if (LF_ISSET(DB_TXN_NOT_DURABLE))
		ENV_REQUIRES_CONFIG(env,
		    env->tx_handle, "DB_NOT_DURABLE", DB_INIT_TXN);

	if (dbp->blob_threshold &&
	    LF_ISSET(DB_CHKSUM | DB_ENCRYPT | DB_DUP | DB_DUPSORT)) {
		__db_errx(dbp->env, DB_STR("0763",
"Cannot enable checksum, encryption, or duplicates with blob support."));
		return (EINVAL);
	}

	__db_map_flags(dbp, &flags, &dbp->flags);

	if ((ret = __bam_set_flags(dbp, &flags)) != 0)
		return (ret);
	if ((ret = __ram_set_flags(dbp, &flags)) != 0)
		return (ret);
#ifdef HAVE_QUEUE
	if ((ret = __qam_set_flags(dbp, &flags)) != 0)
		return (ret);
#endif

	return (flags == 0 ? 0 : __db_ferr(env, "DB->set_flags", 0));
}

/*
 * __db_get_lorder --
 *	Get whether lorder is swapped or not.
 *
 * PUBLIC: int  __db_get_lorder __P((DB *, int *));
 */
int
__db_get_lorder(dbp, db_lorderp)
	DB *dbp;
	int *db_lorderp;
{
	int ret;

	/* Flag if the specified byte order requires swapping. */
	switch (ret = __db_byteorder(dbp->env, 1234)) {
	case 0:
		*db_lorderp = F_ISSET(dbp, DB_AM_SWAP) ? 4321 : 1234;
		break;
	case DB_SWAPBYTES:
		*db_lorderp = F_ISSET(dbp, DB_AM_SWAP) ? 1234 : 4321;
		break;
	default:
		return (ret);
		/* NOTREACHED */
	}

	return (0);
}

/*
 * __db_set_lorder --
 *	Set whether lorder is swapped or not.
 *
 * PUBLIC: int  __db_set_lorder __P((DB *, int));
 */
int
__db_set_lorder(dbp, db_lorder)
	DB *dbp;
	int db_lorder;
{
	int ret;

	DB_ILLEGAL_AFTER_OPEN(dbp, "DB->set_lorder");

	/* Flag if the specified byte order requires swapping. */
	switch (ret = __db_byteorder(dbp->env, db_lorder)) {
	case 0:
		F_CLR(dbp, DB_AM_SWAP);
		break;
	case DB_SWAPBYTES:
		F_SET(dbp, DB_AM_SWAP);
		break;
	default:
		return (ret);
		/* NOTREACHED */
	}
	return (0);
}

static int
__db_get_alloc(dbp, mal_funcp, real_funcp, free_funcp)
	DB *dbp;
	void *(**mal_funcp) __P((size_t));
	void *(**real_funcp) __P((void *, size_t));
	void (**free_funcp) __P((void *));
{
	DB_ILLEGAL_IN_ENV(dbp, "DB->get_alloc");

	return (__env_get_alloc(dbp->dbenv, mal_funcp,
	    real_funcp, free_funcp));
}

static int
__db_set_alloc(dbp, mal_func, real_func, free_func)
	DB *dbp;
	void *(*mal_func) __P((size_t));
	void *(*real_func) __P((void *, size_t));
	void (*free_func) __P((void *));
{
	DB_ILLEGAL_IN_ENV(dbp, "DB->set_alloc");
	DB_ILLEGAL_AFTER_OPEN(dbp, "DB->set_alloc");

	return (__env_set_alloc(dbp->dbenv, mal_func, real_func, free_func));
}

static void
__db_get_msgcall(dbp, msgcallp)
	DB *dbp;
	void (**msgcallp) __P((const DB_ENV *, const char *));
{
	__env_get_msgcall(dbp->dbenv, msgcallp);
}

static void
__db_set_msgcall(dbp, msgcall)
	DB *dbp;
	void (*msgcall) __P((const DB_ENV *, const char *));
{
	__env_set_msgcall(dbp->dbenv, msgcall);
}

static void
__db_get_msgfile(dbp, msgfilep)
	DB *dbp;
	FILE **msgfilep;
{
	__env_get_msgfile(dbp->dbenv, msgfilep);
}

static void
__db_set_msgfile(dbp, msgfile)
	DB *dbp;
	FILE *msgfile;
{
	__env_set_msgfile(dbp->dbenv, msgfile);
}

static int
__db_get_pagesize(dbp, db_pagesizep)
	DB *dbp;
	u_int32_t *db_pagesizep;
{
	*db_pagesizep = dbp->pgsize;
	return (0);
}

/*
 * __db_set_pagesize --
 *	DB->set_pagesize
 *
 * PUBLIC: int  __db_set_pagesize __P((DB *, u_int32_t));
 */
int
__db_set_pagesize(dbp, db_pagesize)
	DB *dbp;
	u_int32_t db_pagesize;
{
	DB_ILLEGAL_AFTER_OPEN(dbp, "DB->set_pagesize");

	if (db_pagesize < DB_MIN_PGSIZE) {
		__db_errx(dbp->env, DB_STR_A("0509",
		    "page sizes may not be smaller than %lu", "%lu"),
		    (u_long)DB_MIN_PGSIZE);
		return (EINVAL);
	}
	if (db_pagesize > DB_MAX_PGSIZE) {
		__db_errx(dbp->env, DB_STR_A("0510",
		    "page sizes may not be larger than %lu", "%lu"),
		    (u_long)DB_MAX_PGSIZE);
		return (EINVAL);
	}

	/*
	 * We don't want anything that's not a power-of-2, as we rely on that
	 * for alignment of various types on the pages.
	 */
	if (!POWER_OF_TWO(db_pagesize)) {
		__db_errx(dbp->env, DB_STR("0511",
		    "page sizes must be a power-of-2"));
		return (EINVAL);
	}

	/*
	 * XXX
	 * Should we be checking for a page size that's not a multiple of 512,
	 * so that we never try and write less than a disk sector?
	 */
	dbp->pgsize = db_pagesize;

	return (0);
}

static int
__db_set_paniccall(dbp, paniccall)
	DB *dbp;
	void (*paniccall) __P((DB_ENV *, int));
{
	return (__env_set_paniccall(dbp->dbenv, paniccall));
}

static int
__db_set_priority(dbp, priority)
	DB *dbp;
	DB_CACHE_PRIORITY priority;
{
	dbp->priority = priority;
	return (0);
}

static int
__db_get_priority(dbp, priority)
	DB *dbp;
	DB_CACHE_PRIORITY *priority;
{
	if (dbp->priority == DB_PRIORITY_UNCHANGED)
		return (__memp_get_priority(dbp->mpf, priority));
	else
		*priority = dbp->priority;

	return (0);
}