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( BACK_BDB, ENTRY, "bdb_db_init", 0, 0, 0 );
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( BACK_BDB, ERR, "bdb_db_init: aborted %d locks\n",
132 Debug( LDAP_DEBUG_ANY,
133 "bdb_lock_detect: aborted %d locks\n",
149 unsigned char *u, *c;
155 #ifdef WORDS_BIGENDIAN
156 for( i = 0; i < sizeof(ID); i++)
158 for( i = sizeof(ID)-1; i >= 0; i--)
168 bdb_db_open( BackendDB *be )
171 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
175 LDAP_LOG( BACK_BDB, ARGS,
176 "bdb_db_open: %s\n", be->be_suffix[0].bv_val, 0, 0 );
178 Debug( LDAP_DEBUG_ARGS,
180 be->be_suffix[0].bv_val, 0, 0 );
183 /* we should check existance of dbenv_home and db_directory */
185 rc = db_env_create( &bdb->bi_dbenv, 0 );
188 LDAP_LOG( BACK_BDB, ERR,
189 "bdb_db_open: db_env_create failed: %s (%d)\n",
190 db_strerror(rc), rc, 0 );
192 Debug( LDAP_DEBUG_ANY,
193 "bdb_db_open: db_env_create failed: %s (%d)\n",
194 db_strerror(rc), rc, 0 );
199 flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE
200 | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN | DB_RECOVER;
202 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
203 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
205 bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
210 char dir[MAXPATHLEN];
211 size_t len = strlen( bdb->bi_dbenv_home );
213 strcpy( dir, bdb->bi_dbenv_home );
214 strcat( &dir[len], BDB_TMP_SUBDIR );
216 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
219 LDAP_LOG( BACK_BDB, ERR,
220 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
221 dir, db_strerror(rc), rc );
223 Debug( LDAP_DEBUG_ANY,
224 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
225 dir, db_strerror(rc), rc );
230 strcat( &dir[len], BDB_LG_SUBDIR );
232 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
235 LDAP_LOG( BACK_BDB, ERR,
236 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
237 dir, db_strerror(rc), rc );
239 Debug( LDAP_DEBUG_ANY,
240 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
241 dir, db_strerror(rc), rc );
246 strcat( &dir[len], BDB_DATA_SUBDIR );
248 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
251 LDAP_LOG( BACK_BDB, ERR,
252 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
253 dir, db_strerror(rc), rc );
255 Debug( LDAP_DEBUG_ANY,
256 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
257 dir, db_strerror(rc), rc );
265 LDAP_LOG( BACK_BDB, DETAIL1,
266 "bdb_db_open: dbenv_open %s\n", bdb->bi_dbenv_home, 0, 0 );
268 Debug( LDAP_DEBUG_TRACE,
269 "bdb_db_open: dbenv_open(%s)\n",
270 bdb->bi_dbenv_home, 0, 0);
273 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
276 bdb->bi_dbenv_mode );
279 LDAP_LOG( BACK_BDB, ERR,
280 "bdb_db_open: dbenv_open failed: %s (%d)\n",
281 db_strerror(rc), rc, 0 );
283 Debug( LDAP_DEBUG_ANY,
284 "bdb_db_open: dbenv_open failed: %s (%d)\n",
285 db_strerror(rc), rc, 0 );
290 if( bdb->bi_dbenv_xflags != 0 ) {
291 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
292 bdb->bi_dbenv_xflags, 1);
295 LDAP_LOG( BACK_BDB, ERR,
296 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
297 db_strerror(rc), rc, 0 );
299 Debug( LDAP_DEBUG_ANY,
300 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
301 db_strerror(rc), rc, 0 );
307 flags = DB_THREAD | DB_CREATE | bdb->bi_db_opflags;
309 bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
310 BDB_INDICES * sizeof(struct bdb_db_info *) );
312 /* open (and create) main database */
313 for( i = 0; bdbi_databases[i].name; i++ ) {
314 struct bdb_db_info *db;
316 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
318 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
321 LDAP_LOG( BACK_BDB, ERR,
322 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
323 bdb->bi_dbenv_home, db_strerror(rc), rc );
325 Debug( LDAP_DEBUG_ANY,
326 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
327 bdb->bi_dbenv_home, db_strerror(rc), rc );
332 if( i == BDB_ID2ENTRY ) {
333 rc = db->bdi_db->set_bt_compare( db->bdi_db,
335 rc = db->bdi_db->set_pagesize( db->bdi_db,
336 BDB_ID2ENTRY_PAGESIZE );
339 rc = db->bdi_db->set_bt_compare( db->bdi_db,
341 #elif defined(BDB_IDL_MULTI)
342 rc = db->bdi_db->set_flags( db->bdi_db,
343 DB_DUP | DB_DUPSORT );
344 rc = db->bdi_db->set_dup_compare( db->bdi_db,
347 rc = db->bdi_db->set_pagesize( db->bdi_db,
351 rc = db->bdi_db->open( db->bdi_db,
352 bdbi_databases[i].file,
353 /* bdbi_databases[i].name, */ NULL,
354 bdbi_databases[i].type,
355 bdbi_databases[i].flags | flags,
356 bdb->bi_dbenv_mode );
360 LDAP_LOG( BACK_BDB, ERR,
361 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
362 bdb->bi_dbenv_home, db_strerror(rc), rc );
364 Debug( LDAP_DEBUG_ANY,
365 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
366 bdb->bi_dbenv_home, db_strerror(rc), rc );
371 db->bdi_name = bdbi_databases[i].name;
372 bdb->bi_databases[i] = db;
375 bdb->bi_databases[i] = NULL;
376 bdb->bi_ndatabases = i;
379 rc = bdb_last_id( be, NULL );
382 LDAP_LOG( BACK_BDB, ERR,
383 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
384 bdb->bi_dbenv_home, db_strerror(rc), rc );
386 Debug( LDAP_DEBUG_ANY,
387 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
388 bdb->bi_dbenv_home, db_strerror(rc), rc );
393 /* <insert> open (and create) index databases */
395 rc = bdb_build_tree( be );
398 #if 0 /* ifndef NO_THREADS */
399 if( bdb->bi_lock_detect != DB_LOCK_NORUN ) {
400 /* listener as a separate THREAD */
401 rc = ldap_pvt_thread_create( &bdb->bi_lock_detect_tid,
402 1, lock_detect_task, bdb );
409 bdb_db_close( BackendDB *be )
412 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
413 struct bdb_db_info *db;
415 while( bdb->bi_ndatabases-- ) {
416 db = bdb->bi_databases[bdb->bi_ndatabases];
417 rc = db->bdi_db->close( db->bdi_db, 0 );
418 /* Lower numbered names are not strdup'd */
419 if( bdb->bi_ndatabases >= BDB_NDB )
420 free( db->bdi_name );
423 free( bdb->bi_databases );
424 bdb_attr_index_destroy( bdb->bi_attrs );
426 bdb_cache_release_all (&bdb->bi_cache);
432 bdb_db_destroy( BackendDB *be )
435 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
437 /* close db environment */
438 if( bdb->bi_dbenv ) {
439 /* force a checkpoint */
440 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
443 LDAP_LOG( BACK_BDB, ERR,
444 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
445 db_strerror(rc), rc, 0 );
447 Debug( LDAP_DEBUG_ANY,
448 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
449 db_strerror(rc), rc, 0 );
453 bdb_cache_release_all (&bdb->bi_cache);
455 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
456 bdb->bi_dbenv = NULL;
459 LDAP_LOG( BACK_BDB, ERR,
460 "bdb_db_destroy: close failed: %s (%d)\n",
461 db_strerror(rc), rc, 0 );
463 Debug( LDAP_DEBUG_ANY,
464 "bdb_db_destroy: close failed: %s (%d)\n",
465 db_strerror(rc), rc, 0 );
472 ldap_pvt_thread_rdwr_destroy( &bdb->bi_tree_rdwr );
474 ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
475 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_mutex );
476 ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
477 ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
482 #ifdef SLAPD_BDB_DYNAMIC
483 int back_bdb_LTX_init_module( int argc, char *argv[] ) {
486 memset( &bi, '\0', sizeof(bi) );
488 bi.bi_init = bdb_initialize;
493 #endif /* SLAPD_BDB_DYNAMIC */
500 static char *controls[] = {
501 LDAP_CONTROL_MANAGEDSAIT,
502 #ifdef LDAP_CONTROL_SUBENTRIES
503 LDAP_CONTROL_SUBENTRIES,
505 #ifdef LDAP_CONTROL_NOOP
508 #ifdef LDAP_CONTROL_VALUESRETURNFILTER
509 LDAP_CONTROL_VALUESRETURNFILTER,
514 bi->bi_controls = controls;
516 /* initialize the underlying database system */
518 LDAP_LOG( BACK_BDB, ENTRY, "bdb_db_initialize\n", 0, 0, 0 );
520 Debug( LDAP_DEBUG_TRACE, "bdb_open: initialize BDB backend\n",
524 { /* version check */
525 int major, minor, patch;
526 char *version = db_version( &major, &minor, &patch );
528 if( major != DB_VERSION_MAJOR ||
529 minor != DB_VERSION_MINOR ||
530 patch < DB_VERSION_PATCH )
533 LDAP_LOG( BACK_BDB, ERR,
534 "bdb_db_initialize: version mismatch: "
535 "\texpected: %s \tgot: %s\n", DB_VERSION_STRING, version, 0 );
537 Debug( LDAP_DEBUG_ANY,
538 "bdb_open: version mismatch\n"
539 "\texpected: " DB_VERSION_STRING "\n"
540 "\tgot: %s \n", version, 0, 0 );
545 LDAP_LOG( BACK_BDB, DETAIL1,
546 "bdb_db_initialize: bdb_open: %s\n", version, 0, 0 );
548 Debug( LDAP_DEBUG_ANY, "bdb_open: %s\n",
554 db_env_set_func_malloc( ch_malloc );
555 db_env_set_func_realloc( ch_realloc );
556 db_env_set_func_free( ch_free );
559 db_env_set_func_yield( ldap_pvt_thread_yield );
562 static char uuidbuf[40];
564 bdb_uuid.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ));
565 bdb_uuid.bv_val = uuidbuf;
573 bi->bi_db_init = bdb_db_init;
574 bi->bi_db_config = bdb_db_config;
575 bi->bi_db_open = bdb_db_open;
576 bi->bi_db_close = bdb_db_close;
577 bi->bi_db_destroy = bdb_db_destroy;
579 bi->bi_op_add = bdb_add;
580 bi->bi_op_bind = bdb_bind;
581 bi->bi_op_compare = bdb_compare;
582 bi->bi_op_delete = bdb_delete;
583 bi->bi_op_modify = bdb_modify;
584 bi->bi_op_modrdn = bdb_modrdn;
585 bi->bi_op_search = bdb_search;
587 bi->bi_op_unbind = 0;
588 bi->bi_op_abandon = 0;
590 bi->bi_extended = bdb_extended;
594 * these routines (and their callers) are not yet designed
595 * to work with transaction. Using them may cause deadlock.
597 bi->bi_acl_group = bdb_group;
598 bi->bi_acl_attribute = bdb_attribute;
600 bi->bi_acl_group = 0;
601 bi->bi_acl_attribute = 0;
604 bi->bi_chk_referrals = bdb_referrals;
605 bi->bi_operational = bdb_operational;
606 bi->bi_entry_release_rw = bdb_entry_release;
609 * hooks for slap tools
611 bi->bi_tool_entry_open = bdb_tool_entry_open;
612 bi->bi_tool_entry_close = bdb_tool_entry_close;
613 bi->bi_tool_entry_first = bdb_tool_entry_next;
614 bi->bi_tool_entry_next = bdb_tool_entry_next;
615 bi->bi_tool_entry_get = bdb_tool_entry_get;
616 bi->bi_tool_entry_put = bdb_tool_entry_put;
617 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
618 bi->bi_tool_sync = 0;
620 bi->bi_connection_init = 0;
621 bi->bi_connection_destroy = 0;