1 /* init.c - initialize bdb backend */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2006 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
20 #include <ac/string.h>
21 #include <ac/unistd.h>
22 #include <ac/stdlib.h>
30 static const struct bdbi_database {
35 } bdbi_databases[] = {
36 { "id2entry" BDB_SUFFIX, "id2entry", DB_BTREE, 0 },
37 { "dn2id" BDB_SUFFIX, "dn2id", DB_BTREE, 0 },
41 typedef void * db_malloc(size_t);
42 typedef void * db_realloc(void *, size_t);
45 bdb_db_init( BackendDB *be )
50 Debug( LDAP_DEBUG_TRACE,
51 LDAP_XSTRING(bdb_db_init) ": Initializing " BDB_UCTYPE " database\n",
54 /* allocate backend-database-specific stuff */
55 bdb = (struct bdb_info *) ch_calloc( 1, sizeof(struct bdb_info) );
57 /* DBEnv parameters */
58 bdb->bi_dbenv_home = ch_strdup( SLAPD_DEFAULT_DB_DIR );
59 bdb->bi_dbenv_xflags = 0;
60 bdb->bi_dbenv_mode = SLAPD_DEFAULT_DB_MODE;
62 bdb->bi_cache.c_maxsize = DEFAULT_CACHE_SIZE;
63 bdb->bi_cache.c_minfree = 1;
65 bdb->bi_lock_detect = DB_LOCK_DEFAULT;
66 bdb->bi_search_stack_depth = DEFAULT_SEARCH_STACK_DEPTH;
67 bdb->bi_search_stack = NULL;
69 ldap_pvt_thread_mutex_init( &bdb->bi_database_mutex );
70 ldap_pvt_thread_mutex_init( &bdb->bi_lastid_mutex );
72 ldap_pvt_thread_mutex_init( &bdb->bi_modrdns_mutex );
74 ldap_pvt_thread_mutex_init( &bdb->bi_cache.lru_head_mutex );
75 ldap_pvt_thread_mutex_init( &bdb->bi_cache.lru_tail_mutex );
76 ldap_pvt_thread_mutex_init( &bdb->bi_cache.c_count_mutex );
77 ldap_pvt_thread_mutex_init( &bdb->bi_cache.c_eifree_mutex );
78 ldap_pvt_thread_mutex_init( &bdb->bi_cache.c_dntree.bei_kids_mutex );
79 ldap_pvt_thread_rdwr_init ( &bdb->bi_cache.c_rwlock );
80 ldap_pvt_thread_rdwr_init( &bdb->bi_idl_tree_rwlock );
81 ldap_pvt_thread_mutex_init( &bdb->bi_idl_tree_lrulock );
84 be->be_cf_ocs = be->bd_info->bi_cf_ocs;
86 rc = bdb_monitor_db_init( be );
92 bdb_db_close( BackendDB *be );
95 bdb_db_open( BackendDB *be )
98 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
99 struct stat stat1, stat2;
101 char path[MAXPATHLEN];
103 int do_recover = 0, do_alock_recover = 0;
104 int alockt, quick = 0;
106 if ( be->be_suffix == NULL ) {
107 Debug( LDAP_DEBUG_ANY,
108 "bdb_db_open: need suffix\n",
113 Debug( LDAP_DEBUG_ARGS,
115 be->be_suffix[0].bv_val, 0, 0 );
117 #ifndef BDB_MULTIPLE_SUFFIXES
118 if ( be->be_suffix[1].bv_val ) {
119 Debug( LDAP_DEBUG_ANY,
120 "bdb_db_open: only one suffix allowed\n", 0, 0, 0 );
125 /* Check existence of dbenv_home. Any error means trouble */
126 rc = stat( bdb->bi_dbenv_home, &stat1 );
128 Debug( LDAP_DEBUG_ANY,
129 "bdb_db_open: Cannot access database directory %s (%d)\n",
130 bdb->bi_dbenv_home, errno, 0 );
134 /* Perform database use arbitration/recovery logic */
135 alockt = (slapMode & SLAP_TOOL_READONLY) ? ALOCK_LOCKED : ALOCK_UNIQUE;
136 if ( slapMode & SLAP_TOOL_QUICK ) {
137 alockt |= ALOCK_NOSAVE;
141 rc = alock_open( &bdb->bi_alock_info,
143 bdb->bi_dbenv_home, alockt );
145 /* alockt is TRUE if the existing environment was created in Quick mode */
146 alockt = (rc & ALOCK_NOSAVE) ? 1 : 0;
149 if( rc == ALOCK_RECOVER ) {
150 Debug( LDAP_DEBUG_ANY,
151 "bdb_db_open: unclean shutdown detected;"
152 " attempting recovery.\n",
154 do_alock_recover = 1;
155 do_recover = DB_RECOVER;
156 } else if( rc == ALOCK_BUSY ) {
157 Debug( LDAP_DEBUG_ANY,
158 "bdb_db_open: database already in use\n",
161 } else if( rc != ALOCK_CLEAN ) {
162 Debug( LDAP_DEBUG_ANY,
163 "bdb_db_open: alock package is unstable\n",
169 * The DB_CONFIG file may have changed. If so, recover the
170 * database so that new settings are put into effect. Also
171 * note the possible absence of DB_CONFIG in the log.
173 if( stat( bdb->bi_db_config_path, &stat1 ) == 0 ) {
175 char *ptr = lutil_strcopy(path, bdb->bi_dbenv_home);
176 *ptr++ = LDAP_DIRSEP[0];
177 strcpy( ptr, "__db.001" );
178 if( stat( path, &stat2 ) == 0 ) {
179 if( stat2.st_mtime < stat1.st_mtime ) {
180 Debug( LDAP_DEBUG_ANY,
181 "bdb_db_open: DB_CONFIG for suffix %s has changed.\n"
182 "Performing database recovery to activate new settings.\n",
183 be->be_suffix[0].bv_val, 0, 0 );
184 do_recover = DB_RECOVER;
190 Debug( LDAP_DEBUG_ANY,
191 "bdb_db_open: Warning - No DB_CONFIG file found "
192 "in directory %s: (%d)\n"
193 "Expect poor performance for suffix %s.\n",
194 bdb->bi_dbenv_home, errno, be->be_suffix[0].bv_val );
197 /* Always let slapcat run, regardless of environment state.
198 * This can be used to cause a cache flush after an unclean
201 if ( do_recover && ( slapMode & SLAP_TOOL_READONLY )) {
202 Debug( LDAP_DEBUG_ANY,
203 "bdb_db_open: Recovery skipped in read-only mode. "
204 "Run manual recovery if errors are encountered.\n",
210 /* An existing environment in Quick mode has nothing to recover. */
211 if ( alockt && do_recover ) {
212 Debug( LDAP_DEBUG_ANY,
213 "bdb_db_open: cannot recover, database must be reinitialized.\n",
219 rc = db_env_create( &bdb->bi_dbenv, 0 );
221 Debug( LDAP_DEBUG_ANY,
222 "bdb_db_open: db_env_create failed: %s (%d)\n",
223 db_strerror(rc), rc, 0 );
228 strcpy( path, bdb->bi_dbenv_home );
232 dbhome = bdb->bi_dbenv_home;
235 /* If existing environment is clean but doesn't support
236 * currently requested modes, remove it.
238 if ( !do_recover && ( alockt ^ quick )) {
239 rc = bdb->bi_dbenv->remove( bdb->bi_dbenv, dbhome, DB_FORCE );
241 Debug( LDAP_DEBUG_ANY,
242 "bdb_db_open: dbenv remove failed: %s (%d)\n",
243 db_strerror(rc), rc, 0 );
244 bdb->bi_dbenv = NULL;
247 rc = db_env_create( &bdb->bi_dbenv, 0 );
249 Debug( LDAP_DEBUG_ANY,
250 "bdb_db_open: db_env_create failed: %s (%d)\n",
251 db_strerror(rc), rc, 0 );
256 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
257 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
259 bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
261 /* One long-lived TXN per thread, two TXNs per write op */
262 bdb->bi_dbenv->set_tx_max( bdb->bi_dbenv, connection_pool_max * 3 );
264 if( bdb->bi_dbenv_xflags != 0 ) {
265 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
266 bdb->bi_dbenv_xflags, 1);
268 Debug( LDAP_DEBUG_ANY,
269 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
270 db_strerror(rc), rc, 0 );
275 #define BDB_TXN_FLAGS (DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN)
277 Debug( LDAP_DEBUG_TRACE,
278 "bdb_db_open: dbenv_open(%s)\n",
279 bdb->bi_dbenv_home, 0, 0);
281 flags = DB_INIT_MPOOL | DB_CREATE | DB_THREAD;
284 flags |= BDB_TXN_FLAGS;
286 /* If a key was set, use shared memory for the BDB environment */
287 if ( bdb->bi_shm_key ) {
288 bdb->bi_dbenv->set_shm_key( bdb->bi_dbenv, bdb->bi_shm_key );
289 flags |= DB_SYSTEM_MEM;
291 rc = bdb->bi_dbenv->open( bdb->bi_dbenv, dbhome,
292 flags | do_recover, bdb->bi_dbenv_mode );
295 Debug( LDAP_DEBUG_ANY,
296 "bdb_db_open: Database cannot be %s, err %d. "
297 "Restore from backup!\n",
298 do_recover ? "recovered" : "opened", rc, 0);
302 if ( do_alock_recover && alock_recover (&bdb->bi_alock_info) != 0 ) {
303 Debug( LDAP_DEBUG_ANY,
304 "bdb_db_open: alock_recover failed\n",
310 #ifdef SLAP_ZONE_ALLOC
311 if ( bdb->bi_cache.c_maxsize ) {
312 bdb->bi_cache.c_zctx = slap_zn_mem_create(
313 SLAP_ZONE_INITSIZE, SLAP_ZONE_MAXSIZE,
314 SLAP_ZONE_DELTA, SLAP_ZONE_SIZE);
318 if ( bdb->bi_idl_cache_max_size ) {
319 bdb->bi_idl_tree = NULL;
320 bdb->bi_idl_cache_size = 0;
323 flags = DB_THREAD | bdb->bi_db_opflags;
325 #ifdef DB_AUTO_COMMIT
327 flags |= DB_AUTO_COMMIT;
330 bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
331 BDB_INDICES * sizeof(struct bdb_db_info *) );
333 /* open (and create) main database */
334 for( i = 0; bdbi_databases[i].name; i++ ) {
335 struct bdb_db_info *db;
337 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
339 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
341 Debug( LDAP_DEBUG_ANY,
342 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
343 bdb->bi_dbenv_home, db_strerror(rc), rc );
347 if( i == BDB_ID2ENTRY ) {
348 if ( slapMode & SLAP_TOOL_MODE )
349 db->bdi_db->mpf->set_priority( db->bdi_db->mpf,
350 DB_PRIORITY_VERY_LOW );
352 rc = db->bdi_db->set_pagesize( db->bdi_db,
353 BDB_ID2ENTRY_PAGESIZE );
354 if ( slapMode & SLAP_TOOL_READMAIN ) {
360 rc = db->bdi_db->set_flags( db->bdi_db,
361 DB_DUP | DB_DUPSORT );
363 if ( slapMode & SLAP_TOOL_READONLY ) {
369 if ( slapMode & (SLAP_TOOL_READONLY|SLAP_TOOL_READMAIN) ) {
375 rc = db->bdi_db->set_pagesize( db->bdi_db,
380 strcpy( path, bdbi_databases[i].file );
382 rc = DB_OPEN( db->bdi_db,
384 /* bdbi_databases[i].name, */ NULL,
385 bdbi_databases[i].type,
386 bdbi_databases[i].flags | flags,
387 bdb->bi_dbenv_mode );
389 rc = DB_OPEN( db->bdi_db,
390 bdbi_databases[i].file,
391 /* bdbi_databases[i].name, */ NULL,
392 bdbi_databases[i].type,
393 bdbi_databases[i].flags | flags,
394 bdb->bi_dbenv_mode );
398 char buf[SLAP_TEXT_BUFLEN];
400 snprintf( buf, sizeof(buf), "%s/%s",
401 bdb->bi_dbenv_home, bdbi_databases[i].file );
402 Debug( LDAP_DEBUG_ANY,
403 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
404 buf, db_strerror(rc), rc );
405 db->bdi_db->close( db->bdi_db, 0 );
409 flags &= ~(DB_CREATE | DB_RDONLY);
410 db->bdi_name = bdbi_databases[i].name;
411 bdb->bi_databases[i] = db;
414 bdb->bi_databases[i] = NULL;
415 bdb->bi_ndatabases = i;
418 rc = bdb_last_id( be, NULL );
420 Debug( LDAP_DEBUG_ANY,
421 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
422 bdb->bi_dbenv_home, db_strerror(rc), rc );
427 XLOCK_ID(bdb->bi_dbenv, &bdb->bi_cache.c_locker);
431 rc = bdb_monitor_db_open( be );
436 bdb->bi_flags |= BDB_IS_OPEN;
438 entry_prealloc( bdb->bi_cache.c_maxsize );
439 attr_prealloc( bdb->bi_cache.c_maxsize * 20 );
448 bdb_db_close( BackendDB *be )
451 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
452 struct bdb_db_info *db;
453 bdb_idl_cache_entry_t *entry, *next_entry;
455 /* monitor handling */
456 (void)bdb_monitor_db_close( be );
458 bdb->bi_flags &= ~BDB_IS_OPEN;
460 ber_bvarray_free( bdb->bi_db_config );
461 bdb->bi_db_config = NULL;
463 while( bdb->bi_databases && bdb->bi_ndatabases-- ) {
464 db = bdb->bi_databases[bdb->bi_ndatabases];
465 rc = db->bdi_db->close( db->bdi_db, 0 );
466 /* Lower numbered names are not strdup'd */
467 if( bdb->bi_ndatabases >= BDB_NDB )
468 free( db->bdi_name );
471 free( bdb->bi_databases );
472 bdb->bi_databases = NULL;
474 bdb_cache_release_all (&bdb->bi_cache);
476 if ( bdb->bi_idl_cache_max_size ) {
477 avl_free( bdb->bi_idl_tree, NULL );
478 bdb->bi_idl_tree = NULL;
479 entry = bdb->bi_idl_lru_head;
480 while ( entry != NULL ) {
481 next_entry = entry->idl_lru_next;
484 free( entry->kstr.bv_val );
488 bdb->bi_idl_lru_head = bdb->bi_idl_lru_tail = NULL;
491 /* close db environment */
492 if( bdb->bi_dbenv ) {
493 /* Free cache locker if we enabled locking */
494 if ( !( slapMode & SLAP_TOOL_QUICK )) {
495 XLOCK_ID_FREE(bdb->bi_dbenv, bdb->bi_cache.c_locker);
496 bdb->bi_cache.c_locker = 0;
499 /* force a checkpoint, but not if we were ReadOnly,
500 * and not in Quick mode since there are no transactions there.
502 if ( !( slapMode & ( SLAP_TOOL_QUICK|SLAP_TOOL_READONLY ))) {
503 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
505 Debug( LDAP_DEBUG_ANY,
506 "bdb_db_close: txn_checkpoint failed: %s (%d)\n",
507 db_strerror(rc), rc, 0 );
511 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
512 bdb->bi_dbenv = NULL;
514 Debug( LDAP_DEBUG_ANY,
515 "bdb_db_close: close failed: %s (%d)\n",
516 db_strerror(rc), rc, 0 );
521 rc = alock_close( &bdb->bi_alock_info );
523 Debug( LDAP_DEBUG_ANY,
524 "bdb_db_close: alock_close failed\n", 0, 0, 0 );
532 bdb_db_destroy( BackendDB *be )
534 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
536 /* monitor handling */
537 (void)bdb_monitor_db_destroy( be );
539 if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
540 if( bdb->bi_db_config_path ) ch_free( bdb->bi_db_config_path );
542 bdb_attr_index_destroy( bdb );
544 ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
545 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_head_mutex );
546 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_tail_mutex );
547 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_count_mutex );
548 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_eifree_mutex );
549 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_dntree.bei_kids_mutex );
551 ldap_pvt_thread_mutex_destroy( &bdb->bi_modrdns_mutex );
553 ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
554 ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
555 ldap_pvt_thread_rdwr_destroy( &bdb->bi_idl_tree_rwlock );
556 ldap_pvt_thread_mutex_destroy( &bdb->bi_idl_tree_lrulock );
559 be->be_private = NULL;
570 static char *controls[] = {
572 LDAP_CONTROL_MANAGEDSAIT,
574 LDAP_CONTROL_PAGEDRESULTS,
575 LDAP_CONTROL_PRE_READ,
576 LDAP_CONTROL_POST_READ,
577 LDAP_CONTROL_SUBENTRIES,
578 LDAP_CONTROL_X_PERMISSIVE_MODIFY,
580 LDAP_CONTROL_X_TXN_SPEC,
585 /* initialize the underlying database system */
586 Debug( LDAP_DEBUG_TRACE,
587 LDAP_XSTRING(bdb_back_initialize) ": initialize "
588 BDB_UCTYPE " backend\n", 0, 0, 0 );
591 SLAP_BFLAG_INCREMENT |
592 SLAP_BFLAG_SUBENTRIES |
594 SLAP_BFLAG_REFERRALS;
596 bi->bi_controls = controls;
598 { /* version check */
599 int major, minor, patch, ver;
600 char *version = db_version( &major, &minor, &patch );
604 /* All our stdio does an ASCII to EBCDIC conversion on
605 * the output. Strings from the BDB library are already
606 * in EBCDIC; we have to go back and forth...
608 strcpy( v2, version );
613 ver = (major << 24) | (minor << 16) | patch;
614 if( ver != DB_VERSION_FULL ) {
615 /* fail if a versions don't match */
616 Debug( LDAP_DEBUG_ANY,
617 LDAP_XSTRING(bdb_back_initialize) ": "
618 "BDB library version mismatch:"
619 " expected " DB_VERSION_STRING ","
620 " got %s\n", version, 0, 0 );
624 Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(bdb_back_initialize)
625 ": %s\n", version, 0, 0 );
628 db_env_set_func_free( ber_memfree );
629 db_env_set_func_malloc( (db_malloc *)ber_memalloc );
630 db_env_set_func_realloc( (db_realloc *)ber_memrealloc );
632 /* This is a no-op on a NO_THREAD build. Leave the default
633 * alone so that BDB will sleep on interprocess conflicts.
635 db_env_set_func_yield( ldap_pvt_thread_yield );
643 bi->bi_db_init = bdb_db_init;
644 bi->bi_db_config = config_generic_wrapper;
645 bi->bi_db_open = bdb_db_open;
646 bi->bi_db_close = bdb_db_close;
647 bi->bi_db_destroy = bdb_db_destroy;
649 bi->bi_op_add = bdb_add;
650 bi->bi_op_bind = bdb_bind;
651 bi->bi_op_compare = bdb_compare;
652 bi->bi_op_delete = bdb_delete;
653 bi->bi_op_modify = bdb_modify;
654 bi->bi_op_modrdn = bdb_modrdn;
655 bi->bi_op_search = bdb_search;
657 bi->bi_op_unbind = 0;
659 bi->bi_extended = bdb_extended;
661 bi->bi_chk_referrals = bdb_referrals;
662 bi->bi_operational = bdb_operational;
663 bi->bi_has_subordinates = bdb_hasSubordinates;
664 bi->bi_entry_release_rw = bdb_entry_release;
665 bi->bi_entry_get_rw = bdb_entry_get;
668 * hooks for slap tools
670 bi->bi_tool_entry_open = bdb_tool_entry_open;
671 bi->bi_tool_entry_close = bdb_tool_entry_close;
672 bi->bi_tool_entry_first = bdb_tool_entry_next;
673 bi->bi_tool_entry_next = bdb_tool_entry_next;
674 bi->bi_tool_entry_get = bdb_tool_entry_get;
675 bi->bi_tool_entry_put = bdb_tool_entry_put;
676 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
677 bi->bi_tool_sync = 0;
678 bi->bi_tool_dn2id_get = bdb_tool_dn2id_get;
679 bi->bi_tool_id2entry_get = bdb_tool_id2entry_get;
680 bi->bi_tool_entry_modify = bdb_tool_entry_modify;
682 bi->bi_connection_init = 0;
683 bi->bi_connection_destroy = 0;
685 rc = bdb_back_init_cf( bi );
690 #if (SLAPD_BDB == SLAPD_MOD_DYNAMIC && !defined(BDB_HIER)) || \
691 (SLAPD_HDB == SLAPD_MOD_DYNAMIC && defined(BDB_HIER))
693 /* conditionally define the init_module() function */
695 SLAP_BACKEND_INIT_MODULE( hdb )
696 #else /* !BDB_HIER */
697 SLAP_BACKEND_INIT_MODULE( bdb )
698 #endif /* !BDB_HIER */
700 #endif /* SLAPD_[BH]DB == SLAPD_MOD_DYNAMIC */