1 /* init.c - initialize bdb backend */
4 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/string.h>
12 #include <ac/unistd.h>
13 #include <ac/stdlib.h>
20 static struct bdbi_database {
25 } bdbi_databases[] = {
26 { "id2entry" BDB_SUFFIX, "id2entry", DB_BTREE, 0 },
28 { "id2parent" BDB_SUFFIX, "id2parent", DB_BTREE, 0 },
30 { "dn2id" BDB_SUFFIX, "dn2id", DB_BTREE, 0 },
35 struct berval bdb_uuid = { 0, NULL };
37 typedef void * db_malloc(size_t);
38 typedef void * db_realloc(void *, size_t);
41 bdb_open( BackendInfo *bi )
48 bdb_destroy( BackendInfo *bi )
54 bdb_close( BackendInfo *bi )
56 /* terminate the underlying database system */
62 bdb_db_init( BackendDB *be )
67 LDAP_LOG( BACK_BDB, ENTRY, "bdb_db_init", 0, 0, 0 );
69 Debug( LDAP_DEBUG_ANY,
70 "bdb_db_init: Initializing BDB database\n",
74 /* indicate system schema supported */
77 SLAP_BFLAG_SUBENTRIES |
84 /* allocate backend-database-specific stuff */
85 bdb = (struct bdb_info *) ch_calloc( 1, sizeof(struct bdb_info) );
87 /* DBEnv parameters */
88 bdb->bi_dbenv_home = ch_strdup( SLAPD_DEFAULT_DB_DIR );
89 bdb->bi_dbenv_xflags = 0;
90 bdb->bi_dbenv_mode = SLAPD_DEFAULT_DB_MODE;
92 bdb->bi_cache.c_maxsize = DEFAULT_CACHE_SIZE;
94 bdb->bi_lock_detect = DB_LOCK_DEFAULT;
96 ldap_pvt_thread_mutex_init( &bdb->bi_database_mutex );
97 ldap_pvt_thread_mutex_init( &bdb->bi_lastid_mutex );
98 ldap_pvt_thread_mutex_init( &bdb->bi_cache.lru_mutex );
99 ldap_pvt_thread_rdwr_init ( &bdb->bi_cache.c_rwlock );
101 ldap_pvt_thread_rdwr_init( &bdb->bi_tree_rdwr );
104 be->be_private = bdb;
115 unsigned char *u, *c;
121 #ifdef WORDS_BIGENDIAN
122 for( i = 0; i < sizeof(ID); i++)
124 for( i = sizeof(ID)-1; i >= 0; i--)
134 bdb_db_open( BackendDB *be )
137 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
140 char path[MAXPATHLEN];
144 LDAP_LOG( BACK_BDB, ARGS,
145 "bdb_db_open: %s\n", be->be_suffix[0].bv_val, 0, 0 );
147 Debug( LDAP_DEBUG_ARGS,
149 be->be_suffix[0].bv_val, 0, 0 );
152 /* we should check existance of dbenv_home and db_directory */
154 rc = db_env_create( &bdb->bi_dbenv, 0 );
157 LDAP_LOG( BACK_BDB, ERR,
158 "bdb_db_open: db_env_create failed: %s (%d)\n",
159 db_strerror(rc), rc, 0 );
161 Debug( LDAP_DEBUG_ANY,
162 "bdb_db_open: db_env_create failed: %s (%d)\n",
163 db_strerror(rc), rc, 0 );
168 flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE
169 | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN;
172 /* Never do automatic recovery, must perform it manually.
173 * Otherwise restarting with gentlehup will corrupt the
176 if( !(slapMode & SLAP_TOOL_MODE) ) flags |= DB_RECOVER;
179 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
180 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
182 bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
187 char dir[MAXPATHLEN];
188 size_t len = strlen( bdb->bi_dbenv_home );
190 strcpy( dir, bdb->bi_dbenv_home );
191 strcat( &dir[len], BDB_TMP_SUBDIR );
193 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
196 LDAP_LOG( BACK_BDB, ERR,
197 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
198 dir, db_strerror(rc), rc );
200 Debug( LDAP_DEBUG_ANY,
201 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
202 dir, db_strerror(rc), rc );
207 strcat( &dir[len], BDB_LG_SUBDIR );
209 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
212 LDAP_LOG( BACK_BDB, ERR,
213 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
214 dir, db_strerror(rc), rc );
216 Debug( LDAP_DEBUG_ANY,
217 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
218 dir, db_strerror(rc), rc );
223 strcat( &dir[len], BDB_DATA_SUBDIR );
225 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
228 LDAP_LOG( BACK_BDB, ERR,
229 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
230 dir, db_strerror(rc), rc );
232 Debug( LDAP_DEBUG_ANY,
233 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
234 dir, db_strerror(rc), rc );
242 LDAP_LOG( BACK_BDB, DETAIL1,
243 "bdb_db_open: dbenv_open %s\n", bdb->bi_dbenv_home, 0, 0 );
245 Debug( LDAP_DEBUG_TRACE,
246 "bdb_db_open: dbenv_open(%s)\n",
247 bdb->bi_dbenv_home, 0, 0);
251 strcpy( path, bdb->bi_dbenv_home );
253 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
256 bdb->bi_dbenv_mode );
258 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
261 bdb->bi_dbenv_mode );
265 LDAP_LOG( BACK_BDB, ERR,
266 "bdb_db_open: dbenv_open failed: %s (%d)\n",
267 db_strerror(rc), rc, 0 );
269 Debug( LDAP_DEBUG_ANY,
270 "bdb_db_open: dbenv_open failed: %s (%d)\n",
271 db_strerror(rc), rc, 0 );
276 if( bdb->bi_dbenv_xflags != 0 ) {
277 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
278 bdb->bi_dbenv_xflags, 1);
281 LDAP_LOG( BACK_BDB, ERR,
282 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
283 db_strerror(rc), rc, 0 );
285 Debug( LDAP_DEBUG_ANY,
286 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
287 db_strerror(rc), rc, 0 );
293 flags = DB_THREAD | DB_CREATE | bdb->bi_db_opflags;
295 bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
296 BDB_INDICES * sizeof(struct bdb_db_info *) );
298 /* open (and create) main database */
299 for( i = 0; bdbi_databases[i].name; i++ ) {
300 struct bdb_db_info *db;
302 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
304 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
307 LDAP_LOG( BACK_BDB, ERR,
308 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
309 bdb->bi_dbenv_home, db_strerror(rc), rc );
311 Debug( LDAP_DEBUG_ANY,
312 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
313 bdb->bi_dbenv_home, db_strerror(rc), rc );
318 if( i == BDB_ID2ENTRY ) {
319 rc = db->bdi_db->set_bt_compare( db->bdi_db,
321 rc = db->bdi_db->set_pagesize( db->bdi_db,
322 BDB_ID2ENTRY_PAGESIZE );
325 rc = db->bdi_db->set_bt_compare( db->bdi_db,
328 rc = db->bdi_db->set_flags( db->bdi_db,
329 DB_DUP | DB_DUPSORT );
330 rc = db->bdi_db->set_dup_compare( db->bdi_db,
333 rc = db->bdi_db->set_pagesize( db->bdi_db,
338 strcpy( path, bdbi_databases[i].file );
340 rc = DB_OPEN( db->bdi_db,
342 /* bdbi_databases[i].name, */ NULL,
343 bdbi_databases[i].type,
344 bdbi_databases[i].flags | flags,
345 bdb->bi_dbenv_mode );
347 rc = DB_OPEN( db->bdi_db,
348 bdbi_databases[i].file,
349 /* bdbi_databases[i].name, */ NULL,
350 bdbi_databases[i].type,
351 bdbi_databases[i].flags | flags,
352 bdb->bi_dbenv_mode );
357 LDAP_LOG( BACK_BDB, ERR,
358 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
359 bdb->bi_dbenv_home, db_strerror(rc), rc );
361 Debug( LDAP_DEBUG_ANY,
362 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
363 bdb->bi_dbenv_home, db_strerror(rc), rc );
368 db->bdi_name = bdbi_databases[i].name;
369 bdb->bi_databases[i] = db;
372 bdb->bi_databases[i] = NULL;
373 bdb->bi_ndatabases = i;
376 rc = bdb_last_id( be, NULL );
379 LDAP_LOG( BACK_BDB, ERR,
380 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
381 bdb->bi_dbenv_home, db_strerror(rc), rc );
383 Debug( LDAP_DEBUG_ANY,
384 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
385 bdb->bi_dbenv_home, db_strerror(rc), rc );
390 /* <insert> open (and create) index databases */
392 rc = bdb_build_tree( be );
398 bdb_db_close( BackendDB *be )
401 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
402 struct bdb_db_info *db;
404 while( bdb->bi_ndatabases-- ) {
405 db = bdb->bi_databases[bdb->bi_ndatabases];
406 rc = db->bdi_db->close( db->bdi_db, 0 );
407 /* Lower numbered names are not strdup'd */
408 if( bdb->bi_ndatabases >= BDB_NDB )
409 free( db->bdi_name );
412 free( bdb->bi_databases );
413 bdb_attr_index_destroy( bdb->bi_attrs );
415 bdb_cache_release_all (&bdb->bi_cache);
417 #if defined(NO_THREADS) && defined(BDB_REUSE_LOCKERS)
418 if ( bdb->bi_locker_id ) {
419 bdb_locker_id_free( bdb->bi_dbenv, bdb->bi_locker_id );
426 bdb_db_destroy( BackendDB *be )
429 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
431 /* close db environment */
432 if( bdb->bi_dbenv ) {
433 /* force a checkpoint */
434 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
437 LDAP_LOG( BACK_BDB, ERR,
438 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
439 db_strerror(rc), rc, 0 );
441 Debug( LDAP_DEBUG_ANY,
442 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
443 db_strerror(rc), rc, 0 );
447 bdb_cache_release_all (&bdb->bi_cache);
449 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
450 bdb->bi_dbenv = NULL;
453 LDAP_LOG( BACK_BDB, ERR,
454 "bdb_db_destroy: close failed: %s (%d)\n",
455 db_strerror(rc), rc, 0 );
457 Debug( LDAP_DEBUG_ANY,
458 "bdb_db_destroy: close failed: %s (%d)\n",
459 db_strerror(rc), rc, 0 );
465 if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
468 ldap_pvt_thread_rdwr_destroy( &bdb->bi_tree_rdwr );
470 ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
471 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_mutex );
472 ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
473 ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
476 be->be_private = NULL;
481 #ifdef SLAPD_BDB_DYNAMIC
482 int back_bdb_LTX_init_module( int argc, char *argv[] ) {
485 memset( &bi, '\0', sizeof(bi) );
487 bi.bi_init = bdb_initialize;
492 #endif /* SLAPD_BDB_DYNAMIC */
499 static char *controls[] = {
500 LDAP_CONTROL_MANAGEDSAIT,
501 #ifdef LDAP_CONTROL_SUBENTRIES
502 LDAP_CONTROL_SUBENTRIES,
504 #ifdef LDAP_CONTROL_NOOP
507 #ifdef LDAP_CONTROL_VALUESRETURNFILTER
508 LDAP_CONTROL_VALUESRETURNFILTER,
510 #ifdef LDAP_CLIENT_UPDATE
511 LDAP_CONTROL_CLIENT_UPDATE,
516 bi->bi_controls = controls;
518 /* initialize the underlying database system */
520 LDAP_LOG( BACK_BDB, ENTRY, "bdb_db_initialize\n", 0, 0, 0 );
522 Debug( LDAP_DEBUG_TRACE, "bdb_open: initialize BDB backend\n",
526 { /* version check */
527 int major, minor, patch;
528 char *version = db_version( &major, &minor, &patch );
532 /* All our stdio does an ASCII to EBCDIC conversion on
533 * the output. Strings from the BDB library are already
534 * in EBCDIC; we have to go back and forth...
536 strcpy( v2, version );
541 if( major != DB_VERSION_MAJOR ||
542 minor != DB_VERSION_MINOR ||
543 patch < DB_VERSION_PATCH )
546 LDAP_LOG( BACK_BDB, ERR,
547 "bdb_db_initialize: version mismatch: "
548 "\texpected: %s \tgot: %s\n", DB_VERSION_STRING, version, 0 );
550 Debug( LDAP_DEBUG_ANY,
551 "bdb_open: version mismatch\n"
552 "\texpected: " DB_VERSION_STRING "\n"
553 "\tgot: %s \n", version, 0, 0 );
558 LDAP_LOG( BACK_BDB, DETAIL1,
559 "bdb_db_initialize: bdb_open: %s\n", version, 0, 0 );
561 Debug( LDAP_DEBUG_ANY, "bdb_open: %s\n",
566 db_env_set_func_free( ber_memfree );
567 db_env_set_func_malloc( (db_malloc *)ber_memalloc );
568 db_env_set_func_realloc( (db_realloc *)ber_memrealloc );
570 /* This is a no-op on a NO_THREAD build. Leave the default
571 * alone so that BDB will sleep on interprocess conflicts.
573 db_env_set_func_yield( ldap_pvt_thread_yield );
577 static char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
579 bdb_uuid.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ));
580 bdb_uuid.bv_val = uuidbuf;
588 bi->bi_db_init = bdb_db_init;
589 bi->bi_db_config = bdb_db_config;
590 bi->bi_db_open = bdb_db_open;
591 bi->bi_db_close = bdb_db_close;
592 bi->bi_db_destroy = bdb_db_destroy;
594 bi->bi_op_add = bdb_add;
595 bi->bi_op_bind = bdb_bind;
596 bi->bi_op_compare = bdb_compare;
597 bi->bi_op_delete = bdb_delete;
598 bi->bi_op_modify = bdb_modify;
599 bi->bi_op_modrdn = bdb_modrdn;
600 bi->bi_op_search = bdb_search;
602 bi->bi_op_unbind = 0;
603 bi->bi_op_abandon = 0;
605 bi->bi_extended = bdb_extended;
609 * these routines (and their callers) are not yet designed
610 * to work with transaction. Using them may cause deadlock.
612 bi->bi_acl_group = bdb_group;
613 bi->bi_acl_attribute = bdb_attribute;
615 bi->bi_acl_group = 0;
616 bi->bi_acl_attribute = 0;
619 bi->bi_chk_referrals = bdb_referrals;
620 bi->bi_operational = bdb_operational;
621 bi->bi_entry_release_rw = bdb_entry_release;
624 * hooks for slap tools
626 bi->bi_tool_entry_open = bdb_tool_entry_open;
627 bi->bi_tool_entry_close = bdb_tool_entry_close;
628 bi->bi_tool_entry_first = bdb_tool_entry_next;
629 bi->bi_tool_entry_next = bdb_tool_entry_next;
630 bi->bi_tool_entry_get = bdb_tool_entry_get;
631 bi->bi_tool_entry_put = bdb_tool_entry_put;
632 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
633 bi->bi_tool_sync = 0;
635 bi->bi_connection_init = 0;
636 bi->bi_connection_destroy = 0;