2
* See the file LICENSE for redistribution information.
4
* Copyright (c) 1999-2002
5
* Sleepycat Software. All rights reserved.
11
static const char revid[] = "$Id$";
14
#ifndef NO_SYSTEM_INCLUDES
15
#include <sys/types.h>
25
#include "dbinc/crypto.h"
26
#include "dbinc/db_page.h"
27
#include "dbinc/db_shash.h"
28
#include "dbinc/btree.h"
29
#include "dbinc/hash.h"
30
#include "dbinc/qam.h"
32
#include "dbinc_auto/xa_ext.h"
33
#include "dbinc/db_shash.h"
34
#include "dbinc/lock.h"
37
#include "dbinc_auto/db_server.h"
38
#include "dbinc_auto/rpc_client_ext.h"
41
static int __db_get_byteswapped __P((DB *, int *));
42
static int __db_get_type __P((DB *, DBTYPE *dbtype));
43
static int __db_init __P((DB *, u_int32_t));
44
static int __db_key_range
45
__P((DB *, DB_TXN *, DBT *, DB_KEY_RANGE *, u_int32_t));
46
static int __db_set_alloc __P((DB *, void *(*)(size_t),
47
void *(*)(void *, size_t), void (*)(void *)));
48
static int __db_set_append_recno __P((DB *, int (*)(DB *, DBT *, db_recno_t)));
49
static int __db_set_cachesize __P((DB *, u_int32_t, u_int32_t, int));
50
static int __db_set_cache_priority __P((DB *, DB_CACHE_PRIORITY));
51
static int __db_set_dup_compare
52
__P((DB *, int (*)(DB *, const DBT *, const DBT *)));
53
static int __db_set_encrypt __P((DB *, const char *, u_int32_t));
54
static int __db_set_feedback __P((DB *, void (*)(DB *, int, int)));
55
static int __db_set_flags __P((DB *, u_int32_t));
56
static int __db_set_pagesize __P((DB *, u_int32_t));
57
static int __db_set_paniccall __P((DB *, void (*)(DB_ENV *, int)));
58
static void __db_set_errcall __P((DB *, void (*)(const char *, char *)));
59
static void __db_set_errfile __P((DB *, FILE *));
60
static void __db_set_errpfx __P((DB *, const char *));
61
static int __db_stat_fail __P((DB *, void *, u_int32_t));
62
static void __dbh_err __P((DB *, int, const char *, ...));
63
static void __dbh_errx __P((DB *, const char *, ...));
66
static int __dbcl_init __P((DB *, DB_ENV *, u_int32_t));
73
* EXTERN: int db_create __P((DB **, DB_ENV *, u_int32_t));
76
db_create(dbpp, dbenv, flags)
84
/* Check for invalid function flags. */
91
"XA applications may not specify an environment to db_create");
96
* If it's an XA database, open it within the XA environment,
97
* taken from the global list of environments. (When the XA
98
* transaction manager called our xa_start() routine the
99
* "current" environment was moved to the start of the list.
101
dbenv = TAILQ_FIRST(&DB_GLOBAL(db_envq));
104
return (__db_ferr(dbenv, "db_create", 0));
107
/* Allocate the DB. */
108
if ((ret = __os_calloc(dbenv, 1, sizeof(*dbp), &dbp)) != 0)
111
if (dbenv != NULL && RPC_ON(dbenv))
112
ret = __dbcl_init(dbp, dbenv, flags);
115
ret = __db_init(dbp, flags);
117
__os_free(dbenv, dbp);
121
/* If we don't have an environment yet, allocate a local one. */
123
if ((ret = db_env_create(&dbenv, 0)) != 0) {
124
__os_free(dbenv, dbp);
127
F_SET(dbenv, DB_ENV_DBLOCAL);
139
* Initialize a DB structure.
142
__db_init(dbp, flags)
148
dbp->lid = DB_LOCK_INVALIDID;
149
LOCK_INIT(dbp->handle_lock);
151
TAILQ_INIT(&dbp->free_queue);
152
TAILQ_INIT(&dbp->active_queue);
153
TAILQ_INIT(&dbp->join_queue);
154
LIST_INIT(&dbp->s_secondaries);
157
DB_OK_BTREE | DB_OK_HASH | DB_OK_QUEUE | DB_OK_RECNO);
159
dbp->associate = __db_associate;
160
dbp->close = __db_close;
161
dbp->cursor = __db_cursor;
162
dbp->del = __db_delete;
163
dbp->err = __dbh_err;
164
dbp->errx = __dbh_errx;
167
dbp->get_byteswapped = __db_get_byteswapped;
168
dbp->get_type = __db_get_type;
169
dbp->join = __db_join;
170
dbp->key_range = __db_key_range;
171
dbp->open = __db_open;
172
dbp->pget = __db_pget;
174
dbp->remove = __db_remove;
175
dbp->rename = __db_rename;
176
dbp->truncate = __db_truncate;
177
dbp->set_alloc = __db_set_alloc;
178
dbp->set_append_recno = __db_set_append_recno;
179
dbp->set_cachesize = __db_set_cachesize;
180
dbp->set_cache_priority = __db_set_cache_priority;
181
dbp->set_dup_compare = __db_set_dup_compare;
182
dbp->set_encrypt = __db_set_encrypt;
183
dbp->set_errcall = __db_set_errcall;
184
dbp->set_errfile = __db_set_errfile;
185
dbp->set_errpfx = __db_set_errpfx;
186
dbp->set_feedback = __db_set_feedback;
187
dbp->set_flags = __db_set_flags;
188
dbp->set_lorder = __db_set_lorder;
189
dbp->set_pagesize = __db_set_pagesize;
190
dbp->set_paniccall = __db_set_paniccall;
191
dbp->stat = __db_stat_fail;
192
dbp->sync = __db_sync;
193
dbp->upgrade = __db_upgrade;
194
dbp->verify = __db_verify;
196
/* Access method specific. */
197
if ((ret = __bam_db_create(dbp)) != 0)
199
if ((ret = __ham_db_create(dbp)) != 0)
201
if ((ret = __qam_db_create(dbp)) != 0)
205
* XA specific: must be last, as we replace methods set by the
208
if (LF_ISSET(DB_XA_CREATE) && (ret = __db_xa_create(dbp)) != 0)
216
* Error if an unreasonable method is called.
218
* PUBLIC: int __dbh_am_chk __P((DB *, u_int32_t));
221
__dbh_am_chk(dbp, flags)
226
* We start out allowing any access methods to be called, and as the
227
* application calls the methods the options become restricted. The
228
* idea is to quit as soon as an illegal method combination is called.
230
if ((LF_ISSET(DB_OK_BTREE) && FLD_ISSET(dbp->am_ok, DB_OK_BTREE)) ||
231
(LF_ISSET(DB_OK_HASH) && FLD_ISSET(dbp->am_ok, DB_OK_HASH)) ||
232
(LF_ISSET(DB_OK_QUEUE) && FLD_ISSET(dbp->am_ok, DB_OK_QUEUE)) ||
233
(LF_ISSET(DB_OK_RECNO) && FLD_ISSET(dbp->am_ok, DB_OK_RECNO))) {
234
FLD_CLR(dbp->am_ok, ~flags);
239
"call implies an access method which is inconsistent with previous calls");
245
* Error message, including the standard error string.
249
__dbh_err(DB *dbp, int error, const char *fmt, ...)
251
__dbh_err(dbp, error, fmt, va_alist)
258
DB_REAL_ERR(dbp->dbenv, error, 1, 1, fmt);
267
__dbh_errx(DB *dbp, const char *fmt, ...)
269
__dbh_errx(dbp, fmt, va_alist)
275
DB_REAL_ERR(dbp->dbenv, 0, 0, 1, fmt);
279
* __db_get_byteswapped --
280
* Return if database requires byte swapping.
283
__db_get_byteswapped(dbp, isswapped)
287
DB_ILLEGAL_BEFORE_OPEN(dbp, "get_byteswapped");
289
*isswapped = F_ISSET(dbp, DB_AM_SWAP) ? 1 : 0;
295
* Return type of underlying database.
298
__db_get_type(dbp, dbtype)
302
DB_ILLEGAL_BEFORE_OPEN(dbp, "get_type");
310
* Return proportion of keys above and below given key.
313
__db_key_range(dbp, txn, key, kr, flags)
320
COMPQUIET(txn, NULL);
321
COMPQUIET(key, NULL);
325
DB_ILLEGAL_BEFORE_OPEN(dbp, "key_range");
326
DB_ILLEGAL_METHOD(dbp, DB_OK_BTREE);
332
* __db_set_append_recno --
333
* Set record number append routine.
336
__db_set_append_recno(dbp, func)
338
int (*func) __P((DB *, DBT *, db_recno_t));
340
DB_ILLEGAL_AFTER_OPEN(dbp, "set_append_recno");
341
DB_ILLEGAL_METHOD(dbp, DB_OK_QUEUE | DB_OK_RECNO);
343
dbp->db_append_recno = func;
349
* __db_set_cachesize --
350
* Set underlying cache size.
353
__db_set_cachesize(dbp, cache_gbytes, cache_bytes, ncache)
355
u_int32_t cache_gbytes, cache_bytes;
358
DB_ILLEGAL_IN_ENV(dbp, "set_cachesize");
359
DB_ILLEGAL_AFTER_OPEN(dbp, "set_cachesize");
361
return (dbp->dbenv->set_cachesize(
362
dbp->dbenv, cache_gbytes, cache_bytes, ncache));
366
* __db_set_cache_priority --
367
* Set cache priority for pages from this file.
370
__db_set_cache_priority(dbp, priority)
372
DB_CACHE_PRIORITY priority;
375
* If an underlying DB_MPOOLFILE exists, call it. Otherwise, save
376
* the information away until DB->open is called.
378
if (dbp->mpf == NULL) {
379
dbp->priority = priority;
382
return (dbp->mpf->set_priority(dbp->mpf, priority));
386
* __db_set_dup_compare --
387
* Set duplicate comparison routine.
390
__db_set_dup_compare(dbp, func)
392
int (*func) __P((DB *, const DBT *, const DBT *));
396
DB_ILLEGAL_AFTER_OPEN(dbp, "dup_compare");
397
DB_ILLEGAL_METHOD(dbp, DB_OK_BTREE | DB_OK_HASH);
399
if ((ret = dbp->set_flags(dbp, DB_DUPSORT)) != 0)
402
dbp->dup_compare = func;
408
* __db_set_encrypt --
409
* Set database passwd.
412
__db_set_encrypt(dbp, passwd, flags)
417
DB_CIPHER *db_cipher;
420
DB_ILLEGAL_IN_ENV(dbp, "set_encrypt");
421
DB_ILLEGAL_AFTER_OPEN(dbp, "set_encrypt");
423
if ((ret = dbp->dbenv->set_encrypt(dbp->dbenv, passwd, flags)) != 0)
427
* In a real env, this gets initialized with the region. In a local
428
* env, we must do it here.
430
db_cipher = (DB_CIPHER *)dbp->dbenv->crypto_handle;
431
if (!F_ISSET(db_cipher, CIPHER_ANY) &&
432
(ret = db_cipher->init(dbp->dbenv, db_cipher)) != 0)
435
return (dbp->set_flags(dbp, DB_ENCRYPT));
439
__db_set_errcall(dbp, errcall)
441
void (*errcall) __P((const char *, char *));
443
dbp->dbenv->set_errcall(dbp->dbenv, errcall);
447
__db_set_errfile(dbp, errfile)
451
dbp->dbenv->set_errfile(dbp->dbenv, errfile);
455
__db_set_errpfx(dbp, errpfx)
459
dbp->dbenv->set_errpfx(dbp->dbenv, errpfx);
463
__db_set_feedback(dbp, feedback)
465
void (*feedback) __P((DB *, int, int));
467
dbp->db_feedback = feedback;
472
__db_set_flags(dbp, flags)
480
* The hash access method only takes two flags: DB_DUP and DB_DUPSORT.
481
* The Btree access method uses them for the same purposes, and so we
482
* resolve them there.
484
* The queue access method takes no flags.
486
if (LF_ISSET(DB_ENCRYPT)) {
487
if (!CRYPTO_ON(dbp->dbenv)) {
489
"Database environment not configured for encryption");
492
F_SET(dbp, DB_AM_ENCRYPT);
493
F_SET(dbp, DB_AM_CHKSUM);
496
if (LF_ISSET(DB_CHKSUM_SHA1)) {
497
F_SET(dbp, DB_AM_CHKSUM);
498
LF_CLR(DB_CHKSUM_SHA1);
501
if ((ret = __bam_set_flags(dbp, &flags)) != 0)
503
if ((ret = __ram_set_flags(dbp, &flags)) != 0)
506
return (flags == 0 ? 0 : __db_ferr(dbp->dbenv, "DB->set_flags", 0));
511
* Set whether lorder is swapped or not.
513
* PUBLIC: int __db_set_lorder __P((DB *, int));
516
__db_set_lorder(dbp, db_lorder)
522
DB_ILLEGAL_AFTER_OPEN(dbp, "set_lorder");
524
/* Flag if the specified byte order requires swapping. */
525
switch (ret = __db_byteorder(dbp->dbenv, db_lorder)) {
527
F_CLR(dbp, DB_AM_SWAP);
530
F_SET(dbp, DB_AM_SWAP);
540
__db_set_alloc(dbp, mal_func, real_func, free_func)
542
void *(*mal_func) __P((size_t));
543
void *(*real_func) __P((void *, size_t));
544
void (*free_func) __P((void *));
546
DB_ILLEGAL_IN_ENV(dbp, "set_alloc");
547
DB_ILLEGAL_AFTER_OPEN(dbp, "set_alloc");
549
return (dbp->dbenv->set_alloc(dbp->dbenv,
550
mal_func, real_func, free_func));
554
__db_set_pagesize(dbp, db_pagesize)
556
u_int32_t db_pagesize;
558
DB_ILLEGAL_AFTER_OPEN(dbp, "set_pagesize");
560
if (db_pagesize < DB_MIN_PGSIZE) {
561
__db_err(dbp->dbenv, "page sizes may not be smaller than %lu",
562
(u_long)DB_MIN_PGSIZE);
565
if (db_pagesize > DB_MAX_PGSIZE) {
566
__db_err(dbp->dbenv, "page sizes may not be larger than %lu",
567
(u_long)DB_MAX_PGSIZE);
572
* We don't want anything that's not a power-of-2, as we rely on that
573
* for alignment of various types on the pages.
575
if (!POWER_OF_TWO(db_pagesize)) {
576
__db_err(dbp->dbenv, "page sizes must be a power-of-2");
582
* Should we be checking for a page size that's not a multiple of 512,
583
* so that we never try and write less than a disk sector?
585
dbp->pgsize = db_pagesize;
591
__db_set_paniccall(dbp, paniccall)
593
void (*paniccall) __P((DB_ENV *, int));
595
return (dbp->dbenv->set_paniccall(dbp->dbenv, paniccall));
599
__db_stat_fail(dbp, sp, flags)
608
* DB->stat isn't initialized until the actual DB->open call,
609
* but we don't want to core dump.
611
PANIC_CHECK(dbp->dbenv);
612
DB_ILLEGAL_BEFORE_OPEN(dbp, "DB->stat");
621
* Initialize a DB structure on the server.
624
__dbcl_init(dbp, dbenv, flags)
629
TAILQ_INIT(&dbp->free_queue);
630
TAILQ_INIT(&dbp->active_queue);
632
* Note that we don't need to initialize the join_queue; it's
633
* not used in RPC clients. See the comment in __dbcl_db_join_ret().
636
dbp->associate = __dbcl_db_associate;
637
dbp->close = __dbcl_db_close;
638
dbp->cursor = __dbcl_db_cursor;
639
dbp->del = __dbcl_db_del;
640
dbp->err = __dbh_err;
641
dbp->errx = __dbh_errx;
642
dbp->fd = __dbcl_db_fd;
643
dbp->get = __dbcl_db_get;
644
dbp->get_byteswapped = __db_get_byteswapped;
645
dbp->get_type = __db_get_type;
646
dbp->join = __dbcl_db_join;
647
dbp->key_range = __dbcl_db_key_range;
648
dbp->open = __dbcl_db_open_wrap;
649
dbp->pget = __dbcl_db_pget;
650
dbp->put = __dbcl_db_put;
651
dbp->remove = __dbcl_db_remove;
652
dbp->rename = __dbcl_db_rename;
653
dbp->set_alloc = __dbcl_db_alloc;
654
dbp->set_append_recno = __dbcl_db_set_append_recno;
655
dbp->set_cachesize = __dbcl_db_cachesize;
656
dbp->set_cache_priority = __dbcl_db_cache_priority;
657
dbp->set_dup_compare = __dbcl_db_dup_compare;
658
dbp->set_encrypt = __dbcl_db_encrypt;
659
dbp->set_errcall = __db_set_errcall;
660
dbp->set_errfile = __db_set_errfile;
661
dbp->set_errpfx = __db_set_errpfx;
662
dbp->set_feedback = __dbcl_db_feedback;
663
dbp->set_flags = __dbcl_db_flags;
664
dbp->set_lorder = __dbcl_db_lorder;
665
dbp->set_pagesize = __dbcl_db_pagesize;
666
dbp->set_paniccall = __dbcl_db_panic;
667
dbp->stat = __dbcl_db_stat;
668
dbp->sync = __dbcl_db_sync;
669
dbp->truncate = __dbcl_db_truncate;
670
dbp->upgrade = __dbcl_db_upgrade;
671
dbp->verify = __dbcl_db_verify;
674
* Set all the method specific functions to client funcs as well.
676
dbp->set_bt_compare = __dbcl_db_bt_compare;
677
dbp->set_bt_maxkey = __dbcl_db_bt_maxkey;
678
dbp->set_bt_minkey = __dbcl_db_bt_minkey;
679
dbp->set_bt_prefix = __dbcl_db_bt_prefix;
680
dbp->set_h_ffactor = __dbcl_db_h_ffactor;
681
dbp->set_h_hash = __dbcl_db_h_hash;
682
dbp->set_h_nelem = __dbcl_db_h_nelem;
683
dbp->set_q_extentsize = __dbcl_db_extentsize;
684
dbp->set_re_delim = __dbcl_db_re_delim;
685
dbp->set_re_len = __dbcl_db_re_len;
686
dbp->set_re_pad = __dbcl_db_re_pad;
687
dbp->set_re_source = __dbcl_db_re_source;
689
return (__dbcl_db_create(dbp, dbenv, flags));