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 };
38 bdb_open( BackendInfo *bi )
45 bdb_destroy( BackendInfo *bi )
51 bdb_close( BackendInfo *bi )
53 /* terminate the underlying database system */
59 bdb_db_init( BackendDB *be )
64 LDAP_LOG(( "init", LDAP_LEVEL_ENTRY, "bdb_db_init" ));
66 Debug( LDAP_DEBUG_ANY,
67 "bdb_db_init: Initializing BDB database\n",
71 /* indicate system schema supported */
74 SLAP_BFLAG_SUBENTRIES |
81 /* allocate backend-database-specific stuff */
82 bdb = (struct bdb_info *) ch_calloc( 1, sizeof(struct bdb_info) );
84 /* DBEnv parameters */
85 bdb->bi_dbenv_home = ch_strdup( SLAPD_DEFAULT_DB_DIR );
86 bdb->bi_dbenv_xflags = 0;
87 bdb->bi_dbenv_mode = SLAPD_DEFAULT_DB_MODE;
89 bdb->bi_cache.c_maxsize = DEFAULT_CACHE_SIZE;
93 bdb->bi_lock_detect = DB_LOCK_NORUN;
95 bdb->bi_lock_detect = DB_LOCK_DEFAULT;
99 ldap_pvt_thread_mutex_init( &bdb->bi_database_mutex );
100 ldap_pvt_thread_mutex_init( &bdb->bi_lastid_mutex );
101 ldap_pvt_thread_mutex_init( &bdb->bi_cache.lru_mutex );
102 ldap_pvt_thread_rdwr_init ( &bdb->bi_cache.c_rwlock );
104 ldap_pvt_thread_rdwr_init( &bdb->bi_tree_rdwr );
107 be->be_private = bdb;
111 #if 0 /* ifndef NO_THREADS */
112 static void *lock_detect_task( void *arg )
114 struct bdb_info *bdb = (struct bdb_info *) arg;
116 while( bdb->bi_dbenv != NULL ) {
119 sleep( bdb->bi_lock_detect_seconds );
121 rc = LOCK_DETECT( bdb->bi_dbenv, 0,
122 bdb->bi_lock_detect, &aborted );
129 LDAP_LOG(( "init", LDAP_LEVEL_ERR, "bdb_db_init: aborted %d locks\n", aborted ));
131 Debug( LDAP_DEBUG_ANY,
132 "bdb_lock_detect: aborted %d locks\n",
148 unsigned char *u, *c;
154 #ifdef WORDS_BIGENDIAN
155 for( i = 0; i < sizeof(ID); i++)
157 for( i = sizeof(ID)-1; i >= 0; i--)
167 bdb_db_open( BackendDB *be )
170 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
174 LDAP_LOG(( "init", LDAP_LEVEL_ARGS, "bdb_db_open: %s\n", be->be_suffix[0].bv_val ));
176 Debug( LDAP_DEBUG_ARGS,
178 be->be_suffix[0].bv_val, 0, 0 );
181 /* we should check existance of dbenv_home and db_directory */
183 rc = db_env_create( &bdb->bi_dbenv, 0 );
186 LDAP_LOG(( "init", LDAP_LEVEL_ERR, "bdb_db_open: db_env_create failed: %s (%d)\n", db_strerror(rc), rc ));
188 Debug( LDAP_DEBUG_ANY,
189 "bdb_db_open: db_env_create failed: %s (%d)\n",
190 db_strerror(rc), rc, 0 );
195 flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE
196 | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN | DB_RECOVER;
198 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
199 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
201 bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
206 char dir[MAXPATHLEN];
207 size_t len = strlen( bdb->bi_dbenv_home );
209 strcpy( dir, bdb->bi_dbenv_home );
210 strcat( &dir[len], BDB_TMP_SUBDIR );
212 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
215 LDAP_LOG(( "init", LDAP_LEVEL_ERR, "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n", dir, db_strerror(rc), rc ));
217 Debug( LDAP_DEBUG_ANY,
218 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
219 dir, db_strerror(rc), rc );
224 strcat( &dir[len], BDB_LG_SUBDIR );
226 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
229 LDAP_LOG(( "init", LDAP_LEVEL_ERR, "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n", dir, db_strerror(rc), rc ));
231 Debug( LDAP_DEBUG_ANY,
232 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
233 dir, db_strerror(rc), rc );
238 strcat( &dir[len], BDB_DATA_SUBDIR );
240 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
243 LDAP_LOG(( "init", LDAP_LEVEL_ERR, "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n", dir, db_strerror(rc), rc ));
245 Debug( LDAP_DEBUG_ANY,
246 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
247 dir, db_strerror(rc), rc );
255 LDAP_LOG(( "init", LDAP_LEVEL_DETAIL1, "bdb_db_open: dbenv_open %s\n", bdb->bi_dbenv_home ));
257 Debug( LDAP_DEBUG_TRACE,
258 "bdb_db_open: dbenv_open(%s)\n",
259 bdb->bi_dbenv_home, 0, 0);
262 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
265 bdb->bi_dbenv_mode );
268 LDAP_LOG(( "init", LDAP_LEVEL_ERR, "bdb_db_open: dbenv_open failed: %s (%d)\n", db_strerror(rc), rc ));
270 Debug( LDAP_DEBUG_ANY,
271 "bdb_db_open: dbenv_open failed: %s (%d)\n",
272 db_strerror(rc), rc, 0 );
277 if( bdb->bi_dbenv_xflags != 0 ) {
278 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
279 bdb->bi_dbenv_xflags, 1);
282 LDAP_LOG(( "init", LDAP_LEVEL_ERR, "bdb_db_open: dbenv_set_flags failed: %s (%d)\n", db_strerror(rc), rc ));
284 Debug( LDAP_DEBUG_ANY,
285 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
286 db_strerror(rc), rc, 0 );
292 flags = DB_THREAD | DB_CREATE | bdb->bi_db_opflags;
294 bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
295 BDB_INDICES * sizeof(struct bdb_db_info *) );
297 /* open (and create) main database */
298 for( i = 0; bdbi_databases[i].name; i++ ) {
299 struct bdb_db_info *db;
301 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
303 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
306 LDAP_LOG(( "init", LDAP_LEVEL_ERR, "bdb_db_open: db_create(%s) failed: %s (%d)\n", bdb->bi_dbenv_home, db_strerror(rc), rc ));
308 Debug( LDAP_DEBUG_ANY,
309 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
310 bdb->bi_dbenv_home, db_strerror(rc), rc );
315 if( i == BDB_ID2ENTRY ) {
316 rc = db->bdi_db->set_bt_compare( db->bdi_db,
318 rc = db->bdi_db->set_pagesize( db->bdi_db,
319 BDB_ID2ENTRY_PAGESIZE );
322 rc = db->bdi_db->set_bt_compare( db->bdi_db,
324 #elif defined(BDB_IDL_MULTI)
325 rc = db->bdi_db->set_flags( db->bdi_db,
326 DB_DUP | DB_DUPSORT );
327 rc = db->bdi_db->set_dup_compare( db->bdi_db,
330 rc = db->bdi_db->set_pagesize( db->bdi_db,
334 rc = db->bdi_db->open( db->bdi_db,
335 bdbi_databases[i].file,
336 /* bdbi_databases[i].name, */ NULL,
337 bdbi_databases[i].type,
338 bdbi_databases[i].flags | flags,
339 bdb->bi_dbenv_mode );
343 LDAP_LOG(( "init", LDAP_LEVEL_ERR, "bdb_db_open: db_create(%s) failed: %s (%d)\n", bdb->bi_dbenv_home, db_strerror(rc), rc ));
345 Debug( LDAP_DEBUG_ANY,
346 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
347 bdb->bi_dbenv_home, db_strerror(rc), rc );
352 db->bdi_name = bdbi_databases[i].name;
353 bdb->bi_databases[i] = db;
356 bdb->bi_databases[i] = NULL;
357 bdb->bi_ndatabases = i;
360 rc = bdb_last_id( be, NULL );
363 LDAP_LOG(( "init", LDAP_LEVEL_ERR, "bdb_db_open: last_id(%s) failed: %s (%d)\n", bdb->bi_dbenv_home, db_strerror(rc), rc ));
365 Debug( LDAP_DEBUG_ANY,
366 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
367 bdb->bi_dbenv_home, db_strerror(rc), rc );
372 /* <insert> open (and create) index databases */
374 rc = bdb_build_tree( be );
377 #if 0 /* ifndef NO_THREADS */
378 if( bdb->bi_lock_detect != DB_LOCK_NORUN ) {
379 /* listener as a separate THREAD */
380 rc = ldap_pvt_thread_create( &bdb->bi_lock_detect_tid,
381 1, lock_detect_task, bdb );
388 bdb_db_close( BackendDB *be )
391 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
392 struct bdb_db_info *db;
394 while( bdb->bi_ndatabases-- ) {
395 db = bdb->bi_databases[bdb->bi_ndatabases];
396 rc = db->bdi_db->close( db->bdi_db, 0 );
397 /* Lower numbered names are not strdup'd */
398 if( bdb->bi_ndatabases >= BDB_NDB )
399 free( db->bdi_name );
402 free( bdb->bi_databases );
403 bdb_attr_index_destroy( bdb->bi_attrs );
405 bdb_cache_release_all (&bdb->bi_cache);
411 bdb_db_destroy( BackendDB *be )
414 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
416 /* close db environment */
417 if( bdb->bi_dbenv ) {
418 /* force a checkpoint */
419 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
422 LDAP_LOG(( "init", LDAP_LEVEL_ERR, "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n", db_strerror(rc), rc ));
424 Debug( LDAP_DEBUG_ANY,
425 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
426 db_strerror(rc), rc, 0 );
430 bdb_cache_release_all (&bdb->bi_cache);
432 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
433 bdb->bi_dbenv = NULL;
436 LDAP_LOG(( "init", LDAP_LEVEL_ERR, "bdb_db_destroy: close failed: %s (%d)\n", db_strerror(rc), rc ));
438 Debug( LDAP_DEBUG_ANY,
439 "bdb_db_destroy: close failed: %s (%d)\n",
440 db_strerror(rc), rc, 0 );
447 ldap_pvt_thread_rdwr_destroy( &bdb->bi_tree_rdwr );
449 ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
450 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_mutex );
451 ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
452 ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
457 #ifdef SLAPD_BDB_DYNAMIC
458 int back_bdb_LTX_init_module( int argc, char *argv[] ) {
461 memset( &bi, '\0', sizeof(bi) );
463 bi.bi_init = bdb_initialize;
468 #endif /* SLAPD_BDB_DYNAMIC */
475 static char *controls[] = {
476 LDAP_CONTROL_MANAGEDSAIT,
477 #ifdef LDAP_CONTROL_SUBENTRIES
478 LDAP_CONTROL_SUBENTRIES,
480 #ifdef LDAP_CONTROL_NOOP
483 #ifdef LDAP_CONTROL_VALUESRETURNFILTER
484 LDAP_CONTROL_VALUESRETURNFILTER,
489 bi->bi_controls = controls;
491 /* initialize the underlying database system */
493 LDAP_LOG(( "init", LDAP_LEVEL_ENTRY, "bdb_db_initialize\n" ));
495 Debug( LDAP_DEBUG_TRACE, "bdb_open: initialize BDB backend\n",
499 { /* version check */
500 int major, minor, patch;
501 char *version = db_version( &major, &minor, &patch );
503 if( major != DB_VERSION_MAJOR ||
504 minor != DB_VERSION_MINOR ||
505 patch < DB_VERSION_PATCH )
508 LDAP_LOG(( "init", LDAP_LEVEL_ERR, "bdb_db_initialize: version mismatch: \texpected: %s \tgot: %s\n", DB_VERSION_STRING, version ));
510 Debug( LDAP_DEBUG_ANY,
511 "bdb_open: version mismatch\n"
512 "\texpected: " DB_VERSION_STRING "\n"
513 "\tgot: %s \n", version, 0, 0 );
518 LDAP_LOG(( "init", LDAP_LEVEL_DETAIL1, "bdb_db_initialize: bdb_open: %s\n", version ));
520 Debug( LDAP_DEBUG_ANY, "bdb_open: %s\n",
526 db_env_set_func_malloc( ch_malloc );
527 db_env_set_func_realloc( ch_realloc );
528 db_env_set_func_free( ch_free );
531 db_env_set_func_yield( ldap_pvt_thread_yield );
534 static char uuidbuf[40];
536 bdb_uuid.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ));
537 bdb_uuid.bv_val = uuidbuf;
545 bi->bi_db_init = bdb_db_init;
546 bi->bi_db_config = bdb_db_config;
547 bi->bi_db_open = bdb_db_open;
548 bi->bi_db_close = bdb_db_close;
549 bi->bi_db_destroy = bdb_db_destroy;
551 bi->bi_op_add = bdb_add;
552 bi->bi_op_bind = bdb_bind;
553 bi->bi_op_compare = bdb_compare;
554 bi->bi_op_delete = bdb_delete;
555 bi->bi_op_modify = bdb_modify;
556 bi->bi_op_modrdn = bdb_modrdn;
557 bi->bi_op_search = bdb_search;
559 bi->bi_op_unbind = 0;
560 bi->bi_op_abandon = 0;
562 bi->bi_extended = bdb_extended;
566 * these routines (and their callers) are not yet designed
567 * to work with transaction. Using them may cause deadlock.
569 bi->bi_acl_group = bdb_group;
570 bi->bi_acl_attribute = bdb_attribute;
572 bi->bi_acl_group = 0;
573 bi->bi_acl_attribute = 0;
576 bi->bi_chk_referrals = bdb_referrals;
577 bi->bi_operational = bdb_operational;
578 bi->bi_entry_release_rw = bdb_entry_release;
581 * hooks for slap tools
583 bi->bi_tool_entry_open = bdb_tool_entry_open;
584 bi->bi_tool_entry_close = bdb_tool_entry_close;
585 bi->bi_tool_entry_first = bdb_tool_entry_next;
586 bi->bi_tool_entry_next = bdb_tool_entry_next;
587 bi->bi_tool_entry_get = bdb_tool_entry_get;
588 bi->bi_tool_entry_put = bdb_tool_entry_put;
589 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
590 bi->bi_tool_sync = 0;
592 bi->bi_connection_init = 0;
593 bi->bi_connection_destroy = 0;