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.c_mutex );
103 ldap_pvt_thread_rdwr_init( &bdb->bi_tree_rdwr );
106 be->be_private = bdb;
110 #if 0 /* ifndef NO_THREADS */
111 static void *lock_detect_task( void *arg )
113 struct bdb_info *bdb = (struct bdb_info *) arg;
115 while( bdb->bi_dbenv != NULL ) {
118 sleep( bdb->bi_lock_detect_seconds );
120 rc = LOCK_DETECT( bdb->bi_dbenv, 0,
121 bdb->bi_lock_detect, &aborted );
128 LDAP_LOG(( "init", LDAP_LEVEL_ERR, "bdb_db_init: aborted %d locks\n", aborted ));
130 Debug( LDAP_DEBUG_ANY,
131 "bdb_lock_detect: aborted %d locks\n",
147 unsigned char *u, *c;
153 #ifdef WORDS_BIGENDIAN
154 for( i = 0; i < sizeof(ID); i++)
156 for( i = sizeof(ID)-1; i >= 0; i--)
166 bdb_db_open( BackendDB *be )
169 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
173 LDAP_LOG(( "init", LDAP_LEVEL_ARGS, "bdb_db_open: %s\n", be->be_suffix[0]->bv_val ));
175 Debug( LDAP_DEBUG_ARGS,
177 be->be_suffix[0]->bv_val, 0, 0 );
180 /* we should check existance of dbenv_home and db_directory */
182 rc = db_env_create( &bdb->bi_dbenv, 0 );
185 LDAP_LOG(( "init", LDAP_LEVEL_ERR, "bdb_db_open: db_env_create failed: %s (%d)\n", db_strerror(rc), rc ));
187 Debug( LDAP_DEBUG_ANY,
188 "bdb_db_open: db_env_create failed: %s (%d)\n",
189 db_strerror(rc), rc, 0 );
194 flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE
195 | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN | DB_RECOVER;
197 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0]->bv_val );
198 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
200 bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
205 char dir[MAXPATHLEN];
206 size_t len = strlen( bdb->bi_dbenv_home );
208 strcpy( dir, bdb->bi_dbenv_home );
209 strcat( &dir[len], BDB_TMP_SUBDIR );
211 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
214 LDAP_LOG(( "init", LDAP_LEVEL_ERR, "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n", dir, db_strerror(rc), rc ));
216 Debug( LDAP_DEBUG_ANY,
217 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
218 dir, db_strerror(rc), rc );
223 strcat( &dir[len], BDB_LG_SUBDIR );
225 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
228 LDAP_LOG(( "init", LDAP_LEVEL_ERR, "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n", dir, db_strerror(rc), rc ));
230 Debug( LDAP_DEBUG_ANY,
231 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
232 dir, db_strerror(rc), rc );
237 strcat( &dir[len], BDB_DATA_SUBDIR );
239 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
242 LDAP_LOG(( "init", LDAP_LEVEL_ERR, "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n", dir, db_strerror(rc), rc ));
244 Debug( LDAP_DEBUG_ANY,
245 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
246 dir, db_strerror(rc), rc );
254 LDAP_LOG(( "init", LDAP_LEVEL_DETAIL1, "bdb_db_open: dbenv_open %s\n", bdb->bi_dbenv_home ));
256 Debug( LDAP_DEBUG_TRACE,
257 "bdb_db_open: dbenv_open(%s)\n",
258 bdb->bi_dbenv_home, 0, 0);
261 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
264 bdb->bi_dbenv_mode );
267 LDAP_LOG(( "init", LDAP_LEVEL_ERR, "bdb_db_open: dbenv_open failed: %s (%d)\n", db_strerror(rc), rc ));
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(( "init", LDAP_LEVEL_ERR, "bdb_db_open: dbenv_set_flags failed: %s (%d)\n", db_strerror(rc), rc ));
283 Debug( LDAP_DEBUG_ANY,
284 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
285 db_strerror(rc), rc, 0 );
291 flags = DB_THREAD | DB_CREATE | bdb->bi_db_opflags;
293 bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
294 BDB_INDICES * sizeof(struct bdb_db_info *) );
296 /* open (and create) main database */
297 for( i = 0; bdbi_databases[i].name; i++ ) {
298 struct bdb_db_info *db;
300 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
302 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
305 LDAP_LOG(( "init", LDAP_LEVEL_ERR, "bdb_db_open: db_create(%s) failed: %s (%d)\n", bdb->bi_dbenv_home, db_strerror(rc), rc ));
307 Debug( LDAP_DEBUG_ANY,
308 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
309 bdb->bi_dbenv_home, db_strerror(rc), rc );
314 if( i == BDB_ID2ENTRY ) {
315 rc = db->bdi_db->set_bt_compare( db->bdi_db,
317 rc = db->bdi_db->set_pagesize( db->bdi_db,
318 BDB_ID2ENTRY_PAGESIZE );
321 rc = db->bdi_db->set_bt_compare( db->bdi_db,
323 #elif defined(BDB_IDL_MULTI)
324 rc = db->bdi_db->set_flags( db->bdi_db,
325 DB_DUP | DB_DUPSORT );
326 rc = db->bdi_db->set_dup_compare( db->bdi_db,
329 rc = db->bdi_db->set_pagesize( db->bdi_db,
333 rc = db->bdi_db->open( db->bdi_db,
334 bdbi_databases[i].file,
335 /* bdbi_databases[i].name, */ NULL,
336 bdbi_databases[i].type,
337 bdbi_databases[i].flags | flags,
338 bdb->bi_dbenv_mode );
342 LDAP_LOG(( "init", LDAP_LEVEL_ERR, "bdb_db_open: db_create(%s) failed: %s (%d)\n", bdb->bi_dbenv_home, db_strerror(rc), rc ));
344 Debug( LDAP_DEBUG_ANY,
345 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
346 bdb->bi_dbenv_home, db_strerror(rc), rc );
351 db->bdi_name = bdbi_databases[i].name;
352 bdb->bi_databases[i] = db;
355 bdb->bi_databases[i] = NULL;
356 bdb->bi_ndatabases = i;
359 rc = bdb_last_id( be, NULL );
362 LDAP_LOG(( "init", LDAP_LEVEL_ERR, "bdb_db_open: last_id(%s) failed: %s (%d)\n", bdb->bi_dbenv_home, db_strerror(rc), rc ));
364 Debug( LDAP_DEBUG_ANY,
365 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
366 bdb->bi_dbenv_home, db_strerror(rc), rc );
371 /* <insert> open (and create) index databases */
373 rc = bdb_build_tree( be );
376 #if 0 /* ifndef NO_THREADS */
377 if( bdb->bi_lock_detect != DB_LOCK_NORUN ) {
378 /* listener as a separate THREAD */
379 rc = ldap_pvt_thread_create( &bdb->bi_lock_detect_tid,
380 1, lock_detect_task, bdb );
387 bdb_db_close( BackendDB *be )
390 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
391 struct bdb_db_info *db;
393 while( bdb->bi_ndatabases-- ) {
394 db = bdb->bi_databases[bdb->bi_ndatabases];
395 rc = db->bdi_db->close( db->bdi_db, 0 );
396 /* Lower numbered names are not strdup'd */
397 if( bdb->bi_ndatabases >= BDB_NDB )
398 free( db->bdi_name );
401 free( bdb->bi_databases );
402 bdb_attr_index_destroy( bdb->bi_attrs );
404 bdb_cache_release_all (&bdb->bi_cache);
410 bdb_db_destroy( BackendDB *be )
413 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
415 /* close db environment */
416 if( bdb->bi_dbenv ) {
417 /* force a checkpoint */
418 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
421 LDAP_LOG(( "init", LDAP_LEVEL_ERR, "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n", db_strerror(rc), rc ));
423 Debug( LDAP_DEBUG_ANY,
424 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
425 db_strerror(rc), rc, 0 );
429 bdb_cache_release_all (&bdb->bi_cache);
431 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
432 bdb->bi_dbenv = NULL;
435 LDAP_LOG(( "init", LDAP_LEVEL_ERR, "bdb_db_destroy: close failed: %s (%d)\n", db_strerror(rc), rc ));
437 Debug( LDAP_DEBUG_ANY,
438 "bdb_db_destroy: close failed: %s (%d)\n",
439 db_strerror(rc), rc, 0 );
448 #ifdef SLAPD_BDB_DYNAMIC
449 int back_bdb_LTX_init_module( int argc, char *argv[] ) {
452 memset( &bi, '\0', sizeof(bi) );
454 bi.bi_init = bdb_initialize;
459 #endif /* SLAPD_BDB_DYNAMIC */
466 static char *controls[] = {
467 LDAP_CONTROL_MANAGEDSAIT,
468 #ifdef LDAP_CONTROL_SUBENTRIES
469 LDAP_CONTROL_SUBENTRIES,
471 #ifdef LDAP_CONTROL_NOOP
477 bi->bi_controls = controls;
479 /* initialize the underlying database system */
481 LDAP_LOG(( "init", LDAP_LEVEL_ENTRY, "bdb_db_initialize\n" ));
483 Debug( LDAP_DEBUG_TRACE, "bdb_open: initialize BDB backend\n",
487 { /* version check */
488 int major, minor, patch;
489 char *version = db_version( &major, &minor, &patch );
491 if( major != DB_VERSION_MAJOR ||
492 minor != DB_VERSION_MINOR ||
493 patch < DB_VERSION_PATCH )
496 LDAP_LOG(( "init", LDAP_LEVEL_ERR, "bdb_db_initialize: version mismatch: \texpected: %s \tgot: %s\n", DB_VERSION_STRING, version ));
498 Debug( LDAP_DEBUG_ANY,
499 "bdb_open: version mismatch\n"
500 "\texpected: " DB_VERSION_STRING "\n"
501 "\tgot: %s \n", version, 0, 0 );
506 LDAP_LOG(( "init", LDAP_LEVEL_DETAIL1, "bdb_db_initialize: bdb_open: %s\n", version ));
508 Debug( LDAP_DEBUG_ANY, "bdb_open: %s\n",
514 db_env_set_func_malloc( ch_malloc );
515 db_env_set_func_realloc( ch_realloc );
516 db_env_set_func_free( ch_free );
519 db_env_set_func_yield( ldap_pvt_thread_yield );
522 static char uuidbuf[40];
524 bdb_uuid.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ));
525 bdb_uuid.bv_val = uuidbuf;
533 bi->bi_db_init = bdb_db_init;
534 bi->bi_db_config = bdb_db_config;
535 bi->bi_db_open = bdb_db_open;
536 bi->bi_db_close = bdb_db_close;
537 bi->bi_db_destroy = bdb_db_destroy;
539 bi->bi_op_add = bdb_add;
540 bi->bi_op_bind = bdb_bind;
541 bi->bi_op_compare = bdb_compare;
542 bi->bi_op_delete = bdb_delete;
543 bi->bi_op_modify = bdb_modify;
544 bi->bi_op_modrdn = bdb_modrdn;
545 bi->bi_op_search = bdb_search;
547 bi->bi_op_unbind = 0;
548 bi->bi_op_abandon = 0;
550 bi->bi_extended = bdb_extended;
554 * these routines (and their callers) are not yet designed
555 * to work with transaction. Using them may cause deadlock.
557 bi->bi_acl_group = bdb_group;
558 bi->bi_acl_attribute = bdb_attribute;
560 bi->bi_acl_group = 0;
561 bi->bi_acl_attribute = 0;
564 bi->bi_chk_referrals = bdb_referrals;
565 bi->bi_operational = bdb_operational;
566 bi->bi_entry_release_rw = bdb_entry_release;
569 * hooks for slap tools
571 bi->bi_tool_entry_open = bdb_tool_entry_open;
572 bi->bi_tool_entry_close = bdb_tool_entry_close;
573 bi->bi_tool_entry_first = bdb_tool_entry_next;
574 bi->bi_tool_entry_next = bdb_tool_entry_next;
575 bi->bi_tool_entry_get = bdb_tool_entry_get;
576 bi->bi_tool_entry_put = bdb_tool_entry_put;
577 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
578 bi->bi_tool_sync = 0;
580 bi->bi_connection_init = 0;
581 bi->bi_connection_destroy = 0;