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;
96 bdb->bi_lock_detect = DB_LOCK_NORUN;
98 bdb->bi_lock_detect = DB_LOCK_DEFAULT;
102 ldap_pvt_thread_mutex_init( &bdb->bi_database_mutex );
103 ldap_pvt_thread_mutex_init( &bdb->bi_lastid_mutex );
104 ldap_pvt_thread_mutex_init( &bdb->bi_cache.lru_mutex );
105 ldap_pvt_thread_rdwr_init ( &bdb->bi_cache.c_rwlock );
107 ldap_pvt_thread_rdwr_init( &bdb->bi_tree_rdwr );
110 be->be_private = bdb;
114 #if 0 /* ifndef NO_THREADS */
115 static void *lock_detect_task( void *arg )
117 struct bdb_info *bdb = (struct bdb_info *) arg;
119 while( bdb->bi_dbenv != NULL ) {
122 sleep( bdb->bi_lock_detect_seconds );
124 rc = LOCK_DETECT( bdb->bi_dbenv, 0,
125 bdb->bi_lock_detect, &aborted );
132 LDAP_LOG( BACK_BDB, ERR, "bdb_db_init: aborted %d locks\n",
135 Debug( LDAP_DEBUG_ANY,
136 "bdb_lock_detect: aborted %d locks\n",
152 unsigned char *u, *c;
158 #ifdef WORDS_BIGENDIAN
159 for( i = 0; i < sizeof(ID); i++)
161 for( i = sizeof(ID)-1; i >= 0; i--)
171 bdb_db_open( BackendDB *be )
174 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
177 char path[MAXPATHLEN];
181 LDAP_LOG( BACK_BDB, ARGS,
182 "bdb_db_open: %s\n", be->be_suffix[0].bv_val, 0, 0 );
184 Debug( LDAP_DEBUG_ARGS,
186 be->be_suffix[0].bv_val, 0, 0 );
189 /* we should check existance of dbenv_home and db_directory */
191 rc = db_env_create( &bdb->bi_dbenv, 0 );
194 LDAP_LOG( BACK_BDB, ERR,
195 "bdb_db_open: db_env_create failed: %s (%d)\n",
196 db_strerror(rc), rc, 0 );
198 Debug( LDAP_DEBUG_ANY,
199 "bdb_db_open: db_env_create failed: %s (%d)\n",
200 db_strerror(rc), rc, 0 );
205 bdb->bi_dbenv->set_alloc( bdb->bi_dbenv, (db_malloc *)ber_memalloc,
206 (db_realloc *)ber_memrealloc, ber_memfree );
208 flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE
209 | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN;
212 /* Never do automatic recovery, must perform it manually.
213 * Otherwise restarting with gentlehup will corrupt the
216 if( !(slapMode & SLAP_TOOL_MODE) ) flags |= DB_RECOVER;
219 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
220 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
222 bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
227 char dir[MAXPATHLEN];
228 size_t len = strlen( bdb->bi_dbenv_home );
230 strcpy( dir, bdb->bi_dbenv_home );
231 strcat( &dir[len], BDB_TMP_SUBDIR );
233 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
236 LDAP_LOG( BACK_BDB, ERR,
237 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
238 dir, db_strerror(rc), rc );
240 Debug( LDAP_DEBUG_ANY,
241 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
242 dir, db_strerror(rc), rc );
247 strcat( &dir[len], BDB_LG_SUBDIR );
249 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
252 LDAP_LOG( BACK_BDB, ERR,
253 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
254 dir, db_strerror(rc), rc );
256 Debug( LDAP_DEBUG_ANY,
257 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
258 dir, db_strerror(rc), rc );
263 strcat( &dir[len], BDB_DATA_SUBDIR );
265 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
268 LDAP_LOG( BACK_BDB, ERR,
269 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
270 dir, db_strerror(rc), rc );
272 Debug( LDAP_DEBUG_ANY,
273 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
274 dir, db_strerror(rc), rc );
282 LDAP_LOG( BACK_BDB, DETAIL1,
283 "bdb_db_open: dbenv_open %s\n", bdb->bi_dbenv_home, 0, 0 );
285 Debug( LDAP_DEBUG_TRACE,
286 "bdb_db_open: dbenv_open(%s)\n",
287 bdb->bi_dbenv_home, 0, 0);
291 strcpy( path, bdb->bi_dbenv_home );
293 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
296 bdb->bi_dbenv_mode );
298 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
301 bdb->bi_dbenv_mode );
305 LDAP_LOG( BACK_BDB, ERR,
306 "bdb_db_open: dbenv_open failed: %s (%d)\n",
307 db_strerror(rc), rc, 0 );
309 Debug( LDAP_DEBUG_ANY,
310 "bdb_db_open: dbenv_open failed: %s (%d)\n",
311 db_strerror(rc), rc, 0 );
316 if( bdb->bi_dbenv_xflags != 0 ) {
317 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
318 bdb->bi_dbenv_xflags, 1);
321 LDAP_LOG( BACK_BDB, ERR,
322 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
323 db_strerror(rc), rc, 0 );
325 Debug( LDAP_DEBUG_ANY,
326 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
327 db_strerror(rc), rc, 0 );
333 flags = DB_THREAD | DB_CREATE | bdb->bi_db_opflags;
335 bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
336 BDB_INDICES * sizeof(struct bdb_db_info *) );
338 /* open (and create) main database */
339 for( i = 0; bdbi_databases[i].name; i++ ) {
340 struct bdb_db_info *db;
342 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
344 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
347 LDAP_LOG( BACK_BDB, ERR,
348 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
349 bdb->bi_dbenv_home, db_strerror(rc), rc );
351 Debug( LDAP_DEBUG_ANY,
352 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
353 bdb->bi_dbenv_home, db_strerror(rc), rc );
358 if( i == BDB_ID2ENTRY ) {
359 rc = db->bdi_db->set_bt_compare( db->bdi_db,
361 rc = db->bdi_db->set_pagesize( db->bdi_db,
362 BDB_ID2ENTRY_PAGESIZE );
365 rc = db->bdi_db->set_bt_compare( db->bdi_db,
367 #elif defined(BDB_IDL_MULTI)
368 rc = db->bdi_db->set_flags( db->bdi_db,
369 DB_DUP | DB_DUPSORT );
370 rc = db->bdi_db->set_dup_compare( db->bdi_db,
373 rc = db->bdi_db->set_pagesize( db->bdi_db,
378 strcpy( path, bdbi_databases[i].file );
380 rc = DB_OPEN( db->bdi_db,
382 /* bdbi_databases[i].name, */ NULL,
383 bdbi_databases[i].type,
384 bdbi_databases[i].flags | flags,
385 bdb->bi_dbenv_mode );
387 rc = DB_OPEN( db->bdi_db,
388 bdbi_databases[i].file,
389 /* bdbi_databases[i].name, */ NULL,
390 bdbi_databases[i].type,
391 bdbi_databases[i].flags | flags,
392 bdb->bi_dbenv_mode );
397 LDAP_LOG( BACK_BDB, ERR,
398 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
399 bdb->bi_dbenv_home, db_strerror(rc), rc );
401 Debug( LDAP_DEBUG_ANY,
402 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
403 bdb->bi_dbenv_home, db_strerror(rc), rc );
408 db->bdi_name = bdbi_databases[i].name;
409 bdb->bi_databases[i] = db;
412 bdb->bi_databases[i] = NULL;
413 bdb->bi_ndatabases = i;
416 rc = bdb_last_id( be, NULL );
419 LDAP_LOG( BACK_BDB, ERR,
420 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
421 bdb->bi_dbenv_home, db_strerror(rc), rc );
423 Debug( LDAP_DEBUG_ANY,
424 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
425 bdb->bi_dbenv_home, db_strerror(rc), rc );
430 /* <insert> open (and create) index databases */
432 rc = bdb_build_tree( be );
435 #if 0 /* ifndef NO_THREADS */
436 if( bdb->bi_lock_detect != DB_LOCK_NORUN ) {
437 /* listener as a separate THREAD */
438 rc = ldap_pvt_thread_create( &bdb->bi_lock_detect_tid,
439 1, lock_detect_task, bdb );
446 bdb_db_close( BackendDB *be )
449 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
450 struct bdb_db_info *db;
452 while( bdb->bi_ndatabases-- ) {
453 db = bdb->bi_databases[bdb->bi_ndatabases];
454 rc = db->bdi_db->close( db->bdi_db, 0 );
455 /* Lower numbered names are not strdup'd */
456 if( bdb->bi_ndatabases >= BDB_NDB )
457 free( db->bdi_name );
460 free( bdb->bi_databases );
461 bdb_attr_index_destroy( bdb->bi_attrs );
463 bdb_cache_release_all (&bdb->bi_cache);
469 bdb_db_destroy( BackendDB *be )
472 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
474 /* close db environment */
475 if( bdb->bi_dbenv ) {
476 /* force a checkpoint */
477 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
480 LDAP_LOG( BACK_BDB, ERR,
481 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
482 db_strerror(rc), rc, 0 );
484 Debug( LDAP_DEBUG_ANY,
485 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
486 db_strerror(rc), rc, 0 );
490 bdb_cache_release_all (&bdb->bi_cache);
492 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
493 bdb->bi_dbenv = NULL;
496 LDAP_LOG( BACK_BDB, ERR,
497 "bdb_db_destroy: close failed: %s (%d)\n",
498 db_strerror(rc), rc, 0 );
500 Debug( LDAP_DEBUG_ANY,
501 "bdb_db_destroy: close failed: %s (%d)\n",
502 db_strerror(rc), rc, 0 );
508 if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
511 ldap_pvt_thread_rdwr_destroy( &bdb->bi_tree_rdwr );
513 ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
514 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_mutex );
515 ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
516 ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
519 be->be_private = NULL;
524 #ifdef SLAPD_BDB_DYNAMIC
525 int back_bdb_LTX_init_module( int argc, char *argv[] ) {
528 memset( &bi, '\0', sizeof(bi) );
530 bi.bi_init = bdb_initialize;
535 #endif /* SLAPD_BDB_DYNAMIC */
542 static char *controls[] = {
543 LDAP_CONTROL_MANAGEDSAIT,
544 #ifdef LDAP_CONTROL_SUBENTRIES
545 LDAP_CONTROL_SUBENTRIES,
547 #ifdef LDAP_CONTROL_NOOP
550 #ifdef LDAP_CONTROL_VALUESRETURNFILTER
551 LDAP_CONTROL_VALUESRETURNFILTER,
556 bi->bi_controls = controls;
558 /* initialize the underlying database system */
560 LDAP_LOG( BACK_BDB, ENTRY, "bdb_db_initialize\n", 0, 0, 0 );
562 Debug( LDAP_DEBUG_TRACE, "bdb_open: initialize BDB backend\n",
566 { /* version check */
567 int major, minor, patch;
568 char *version = db_version( &major, &minor, &patch );
572 /* All our stdio does an ASCII to EBCDIC conversion on
573 * the output. Strings from the BDB library are already
574 * in EBCDIC; we have to go back and forth...
576 strcpy( v2, version );
581 if( major != DB_VERSION_MAJOR ||
582 minor != DB_VERSION_MINOR ||
583 patch < DB_VERSION_PATCH )
586 LDAP_LOG( BACK_BDB, ERR,
587 "bdb_db_initialize: version mismatch: "
588 "\texpected: %s \tgot: %s\n", DB_VERSION_STRING, version, 0 );
590 Debug( LDAP_DEBUG_ANY,
591 "bdb_open: version mismatch\n"
592 "\texpected: " DB_VERSION_STRING "\n"
593 "\tgot: %s \n", version, 0, 0 );
598 LDAP_LOG( BACK_BDB, DETAIL1,
599 "bdb_db_initialize: bdb_open: %s\n", version, 0, 0 );
601 Debug( LDAP_DEBUG_ANY, "bdb_open: %s\n",
607 db_env_set_func_malloc( ch_malloc );
608 db_env_set_func_realloc( ch_realloc );
609 db_env_set_func_free( ch_free );
612 db_env_set_func_yield( ldap_pvt_thread_yield );
615 static char uuidbuf[40];
617 bdb_uuid.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ));
618 bdb_uuid.bv_val = uuidbuf;
626 bi->bi_db_init = bdb_db_init;
627 bi->bi_db_config = bdb_db_config;
628 bi->bi_db_open = bdb_db_open;
629 bi->bi_db_close = bdb_db_close;
630 bi->bi_db_destroy = bdb_db_destroy;
632 bi->bi_op_add = bdb_add;
633 bi->bi_op_bind = bdb_bind;
634 bi->bi_op_compare = bdb_compare;
635 bi->bi_op_delete = bdb_delete;
636 bi->bi_op_modify = bdb_modify;
637 bi->bi_op_modrdn = bdb_modrdn;
638 bi->bi_op_search = bdb_search;
640 bi->bi_op_unbind = 0;
641 bi->bi_op_abandon = 0;
643 bi->bi_extended = bdb_extended;
647 * these routines (and their callers) are not yet designed
648 * to work with transaction. Using them may cause deadlock.
650 bi->bi_acl_group = bdb_group;
651 bi->bi_acl_attribute = bdb_attribute;
653 bi->bi_acl_group = 0;
654 bi->bi_acl_attribute = 0;
657 bi->bi_chk_referrals = bdb_referrals;
658 bi->bi_operational = bdb_operational;
659 bi->bi_entry_release_rw = bdb_entry_release;
662 * hooks for slap tools
664 bi->bi_tool_entry_open = bdb_tool_entry_open;
665 bi->bi_tool_entry_close = bdb_tool_entry_close;
666 bi->bi_tool_entry_first = bdb_tool_entry_next;
667 bi->bi_tool_entry_next = bdb_tool_entry_next;
668 bi->bi_tool_entry_get = bdb_tool_entry_get;
669 bi->bi_tool_entry_put = bdb_tool_entry_put;
670 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
671 bi->bi_tool_sync = 0;
673 bi->bi_connection_init = 0;
674 bi->bi_connection_destroy = 0;