1 /* init.c - initialize bdb backend */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2007 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 rc = bdb_monitor_db_open( be );
432 bdb->bi_flags |= BDB_IS_OPEN;
434 entry_prealloc( bdb->bi_cache.c_maxsize );
435 attr_prealloc( bdb->bi_cache.c_maxsize * 20 );
444 bdb_db_close( BackendDB *be )
447 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
448 struct bdb_db_info *db;
449 bdb_idl_cache_entry_t *entry, *next_entry;
451 /* monitor handling */
452 (void)bdb_monitor_db_close( be );
454 bdb->bi_flags &= ~BDB_IS_OPEN;
456 ber_bvarray_free( bdb->bi_db_config );
457 bdb->bi_db_config = NULL;
459 while( bdb->bi_databases && bdb->bi_ndatabases-- ) {
460 db = bdb->bi_databases[bdb->bi_ndatabases];
461 rc = db->bdi_db->close( db->bdi_db, 0 );
462 /* Lower numbered names are not strdup'd */
463 if( bdb->bi_ndatabases >= BDB_NDB )
464 free( db->bdi_name );
467 free( bdb->bi_databases );
468 bdb->bi_databases = NULL;
470 bdb_cache_release_all (&bdb->bi_cache);
472 if ( bdb->bi_idl_cache_size ) {
473 avl_free( bdb->bi_idl_tree, NULL );
474 bdb->bi_idl_tree = NULL;
475 entry = bdb->bi_idl_lru_head;
477 next_entry = entry->idl_lru_next;
480 free( entry->kstr.bv_val );
483 } while ( entry != bdb->bi_idl_lru_head );
484 bdb->bi_idl_lru_head = bdb->bi_idl_lru_tail = NULL;
487 /* close db environment */
488 if( bdb->bi_dbenv ) {
489 /* force a checkpoint, but not if we were ReadOnly,
490 * and not in Quick mode since there are no transactions there.
492 if ( !( slapMode & ( SLAP_TOOL_QUICK|SLAP_TOOL_READONLY ))) {
493 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
495 Debug( LDAP_DEBUG_ANY,
496 "bdb_db_close: txn_checkpoint failed: %s (%d)\n",
497 db_strerror(rc), rc, 0 );
501 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
502 bdb->bi_dbenv = NULL;
504 Debug( LDAP_DEBUG_ANY,
505 "bdb_db_close: close failed: %s (%d)\n",
506 db_strerror(rc), rc, 0 );
511 rc = alock_close( &bdb->bi_alock_info );
513 Debug( LDAP_DEBUG_ANY,
514 "bdb_db_close: alock_close failed\n", 0, 0, 0 );
522 bdb_db_destroy( BackendDB *be )
524 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
526 /* monitor handling */
527 (void)bdb_monitor_db_destroy( be );
529 if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
530 if( bdb->bi_db_config_path ) ch_free( bdb->bi_db_config_path );
532 bdb_attr_index_destroy( bdb );
534 ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
535 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_head_mutex );
536 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_tail_mutex );
537 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_count_mutex );
538 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_eifree_mutex );
539 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_dntree.bei_kids_mutex );
541 ldap_pvt_thread_mutex_destroy( &bdb->bi_modrdns_mutex );
543 ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
544 ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
545 ldap_pvt_thread_rdwr_destroy( &bdb->bi_idl_tree_rwlock );
546 ldap_pvt_thread_mutex_destroy( &bdb->bi_idl_tree_lrulock );
549 be->be_private = NULL;
560 static char *controls[] = {
562 LDAP_CONTROL_MANAGEDSAIT,
564 LDAP_CONTROL_PAGEDRESULTS,
565 LDAP_CONTROL_PRE_READ,
566 LDAP_CONTROL_POST_READ,
567 LDAP_CONTROL_SUBENTRIES,
568 LDAP_CONTROL_X_PERMISSIVE_MODIFY,
570 LDAP_CONTROL_X_TXN_SPEC,
575 /* initialize the underlying database system */
576 Debug( LDAP_DEBUG_TRACE,
577 LDAP_XSTRING(bdb_back_initialize) ": initialize "
578 BDB_UCTYPE " backend\n", 0, 0, 0 );
581 SLAP_BFLAG_INCREMENT |
582 SLAP_BFLAG_SUBENTRIES |
584 SLAP_BFLAG_REFERRALS;
586 bi->bi_controls = controls;
588 { /* version check */
589 int major, minor, patch, ver;
590 char *version = db_version( &major, &minor, &patch );
594 /* All our stdio does an ASCII to EBCDIC conversion on
595 * the output. Strings from the BDB library are already
596 * in EBCDIC; we have to go back and forth...
598 strcpy( v2, version );
603 ver = (major << 24) | (minor << 16) | patch;
604 if( ver != DB_VERSION_FULL ) {
605 /* fail if a versions don't match */
606 Debug( LDAP_DEBUG_ANY,
607 LDAP_XSTRING(bdb_back_initialize) ": "
608 "BDB library version mismatch:"
609 " expected " DB_VERSION_STRING ","
610 " got %s\n", version, 0, 0 );
614 Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(bdb_back_initialize)
615 ": %s\n", version, 0, 0 );
618 db_env_set_func_free( ber_memfree );
619 db_env_set_func_malloc( (db_malloc *)ber_memalloc );
620 db_env_set_func_realloc( (db_realloc *)ber_memrealloc );
622 /* This is a no-op on a NO_THREAD build. Leave the default
623 * alone so that BDB will sleep on interprocess conflicts.
625 db_env_set_func_yield( ldap_pvt_thread_yield );
633 bi->bi_db_init = bdb_db_init;
634 bi->bi_db_config = config_generic_wrapper;
635 bi->bi_db_open = bdb_db_open;
636 bi->bi_db_close = bdb_db_close;
637 bi->bi_db_destroy = bdb_db_destroy;
639 bi->bi_op_add = bdb_add;
640 bi->bi_op_bind = bdb_bind;
641 bi->bi_op_compare = bdb_compare;
642 bi->bi_op_delete = bdb_delete;
643 bi->bi_op_modify = bdb_modify;
644 bi->bi_op_modrdn = bdb_modrdn;
645 bi->bi_op_search = bdb_search;
647 bi->bi_op_unbind = 0;
649 bi->bi_extended = bdb_extended;
651 bi->bi_chk_referrals = bdb_referrals;
652 bi->bi_operational = bdb_operational;
653 bi->bi_has_subordinates = bdb_hasSubordinates;
654 bi->bi_entry_release_rw = bdb_entry_release;
655 bi->bi_entry_get_rw = bdb_entry_get;
658 * hooks for slap tools
660 bi->bi_tool_entry_open = bdb_tool_entry_open;
661 bi->bi_tool_entry_close = bdb_tool_entry_close;
662 bi->bi_tool_entry_first = bdb_tool_entry_next;
663 bi->bi_tool_entry_next = bdb_tool_entry_next;
664 bi->bi_tool_entry_get = bdb_tool_entry_get;
665 bi->bi_tool_entry_put = bdb_tool_entry_put;
666 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
667 bi->bi_tool_sync = 0;
668 bi->bi_tool_dn2id_get = bdb_tool_dn2id_get;
669 bi->bi_tool_id2entry_get = bdb_tool_id2entry_get;
670 bi->bi_tool_entry_modify = bdb_tool_entry_modify;
672 bi->bi_connection_init = 0;
673 bi->bi_connection_destroy = 0;
675 rc = bdb_back_init_cf( bi );
680 #if (SLAPD_BDB == SLAPD_MOD_DYNAMIC && !defined(BDB_HIER)) || \
681 (SLAPD_HDB == SLAPD_MOD_DYNAMIC && defined(BDB_HIER))
683 /* conditionally define the init_module() function */
685 SLAP_BACKEND_INIT_MODULE( hdb )
686 #else /* !BDB_HIER */
687 SLAP_BACKEND_INIT_MODULE( bdb )
688 #endif /* !BDB_HIER */
690 #endif /* SLAPD_[BH]DB == SLAPD_MOD_DYNAMIC */