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_dntree.bei_kids_mutex );
77 ldap_pvt_thread_rdwr_init ( &bdb->bi_cache.c_rwlock );
78 ldap_pvt_thread_rdwr_init( &bdb->bi_idl_tree_rwlock );
79 ldap_pvt_thread_mutex_init( &bdb->bi_idl_tree_lrulock );
82 be->be_cf_ocs = be->bd_info->bi_cf_ocs;
84 rc = bdb_monitor_db_init( be );
90 bdb_db_close( BackendDB *be );
93 bdb_db_open( BackendDB *be )
96 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
97 struct stat stat1, stat2;
99 char path[MAXPATHLEN];
101 int do_recover = 0, do_alock_recover = 0;
102 int alockt, quick = 0;
104 if ( be->be_suffix == NULL ) {
105 Debug( LDAP_DEBUG_ANY,
106 "bdb_db_open: need suffix\n",
111 Debug( LDAP_DEBUG_ARGS,
113 be->be_suffix[0].bv_val, 0, 0 );
115 #ifndef BDB_MULTIPLE_SUFFIXES
116 if ( be->be_suffix[1].bv_val ) {
117 Debug( LDAP_DEBUG_ANY,
118 "bdb_db_open: only one suffix allowed\n", 0, 0, 0 );
123 /* Check existence of dbenv_home. Any error means trouble */
124 rc = stat( bdb->bi_dbenv_home, &stat1 );
126 Debug( LDAP_DEBUG_ANY,
127 "bdb_db_open: Cannot access database directory %s (%d)\n",
128 bdb->bi_dbenv_home, errno, 0 );
132 /* Perform database use arbitration/recovery logic */
133 alockt = (slapMode & SLAP_TOOL_READONLY) ? ALOCK_LOCKED : ALOCK_UNIQUE;
134 if ( slapMode & SLAP_TOOL_QUICK ) {
135 alockt |= ALOCK_NOSAVE;
139 rc = alock_open( &bdb->bi_alock_info,
141 bdb->bi_dbenv_home, alockt );
143 /* alockt is TRUE if the existing environment was created in Quick mode */
144 alockt = (rc & ALOCK_NOSAVE) ? 1 : 0;
147 if( rc == ALOCK_RECOVER ) {
148 Debug( LDAP_DEBUG_ANY,
149 "bdb_db_open: unclean shutdown detected;"
150 " attempting recovery.\n",
152 do_alock_recover = 1;
153 do_recover = DB_RECOVER;
154 } else if( rc == ALOCK_BUSY ) {
155 Debug( LDAP_DEBUG_ANY,
156 "bdb_db_open: database already in use\n",
159 } else if( rc != ALOCK_CLEAN ) {
160 Debug( LDAP_DEBUG_ANY,
161 "bdb_db_open: alock package is unstable\n",
167 * The DB_CONFIG file may have changed. If so, recover the
168 * database so that new settings are put into effect. Also
169 * note the possible absence of DB_CONFIG in the log.
171 if( stat( bdb->bi_db_config_path, &stat1 ) == 0 ) {
173 char *ptr = lutil_strcopy(path, bdb->bi_dbenv_home);
174 *ptr++ = LDAP_DIRSEP[0];
175 strcpy( ptr, "__db.001" );
176 if( stat( path, &stat2 ) == 0 ) {
177 if( stat2.st_mtime < stat1.st_mtime ) {
178 Debug( LDAP_DEBUG_ANY,
179 "bdb_db_open: DB_CONFIG for suffix %s has changed.\n"
180 "Performing database recovery to activate new settings.\n",
181 be->be_suffix[0].bv_val, 0, 0 );
182 do_recover = DB_RECOVER;
188 Debug( LDAP_DEBUG_ANY,
189 "bdb_db_open: Warning - No DB_CONFIG file found "
190 "in directory %s: (%d)\n"
191 "Expect poor performance for suffix %s.\n",
192 bdb->bi_dbenv_home, errno, be->be_suffix[0].bv_val );
195 /* Always let slapcat run, regardless of environment state.
196 * This can be used to cause a cache flush after an unclean
199 if ( do_recover && ( slapMode & SLAP_TOOL_READONLY )) {
200 Debug( LDAP_DEBUG_ANY,
201 "bdb_db_open: Recovery skipped in read-only mode. "
202 "Run manual recovery if errors are encountered.\n",
208 /* An existing environment in Quick mode has nothing to recover. */
209 if ( alockt && do_recover ) {
210 Debug( LDAP_DEBUG_ANY,
211 "bdb_db_open: cannot recover, database must be reinitialized.\n",
217 rc = db_env_create( &bdb->bi_dbenv, 0 );
219 Debug( LDAP_DEBUG_ANY,
220 "bdb_db_open: db_env_create failed: %s (%d)\n",
221 db_strerror(rc), rc, 0 );
226 strcpy( path, bdb->bi_dbenv_home );
230 dbhome = bdb->bi_dbenv_home;
233 /* If existing environment is clean but doesn't support
234 * currently requested modes, remove it.
236 if ( !do_recover && ( alockt ^ quick )) {
237 rc = bdb->bi_dbenv->remove( bdb->bi_dbenv, dbhome, DB_FORCE );
239 Debug( LDAP_DEBUG_ANY,
240 "bdb_db_open: dbenv remove failed: %s (%d)\n",
241 db_strerror(rc), rc, 0 );
242 bdb->bi_dbenv = NULL;
245 rc = db_env_create( &bdb->bi_dbenv, 0 );
247 Debug( LDAP_DEBUG_ANY,
248 "bdb_db_open: db_env_create failed: %s (%d)\n",
249 db_strerror(rc), rc, 0 );
254 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
255 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
257 bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
259 /* One long-lived TXN per thread, two TXNs per write op */
260 bdb->bi_dbenv->set_tx_max( bdb->bi_dbenv, connection_pool_max * 3 );
262 if( bdb->bi_dbenv_xflags != 0 ) {
263 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
264 bdb->bi_dbenv_xflags, 1);
266 Debug( LDAP_DEBUG_ANY,
267 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
268 db_strerror(rc), rc, 0 );
273 #define BDB_TXN_FLAGS (DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN)
275 Debug( LDAP_DEBUG_TRACE,
276 "bdb_db_open: dbenv_open(%s)\n",
277 bdb->bi_dbenv_home, 0, 0);
279 flags = DB_INIT_MPOOL | DB_CREATE | DB_THREAD;
282 flags |= BDB_TXN_FLAGS;
284 /* If a key was set, use shared memory for the BDB environment */
285 if ( bdb->bi_shm_key ) {
286 bdb->bi_dbenv->set_shm_key( bdb->bi_dbenv, bdb->bi_shm_key );
287 flags |= DB_SYSTEM_MEM;
289 rc = bdb->bi_dbenv->open( bdb->bi_dbenv, dbhome,
290 flags | do_recover, bdb->bi_dbenv_mode );
293 Debug( LDAP_DEBUG_ANY,
294 "bdb_db_open: Database cannot be %s, err %d. "
295 "Restore from backup!\n",
296 do_recover ? "recovered" : "opened", rc, 0);
300 if ( do_alock_recover && alock_recover (&bdb->bi_alock_info) != 0 ) {
301 Debug( LDAP_DEBUG_ANY,
302 "bdb_db_open: alock_recover failed\n",
308 #ifdef SLAP_ZONE_ALLOC
309 if ( bdb->bi_cache.c_maxsize ) {
310 bdb->bi_cache.c_zctx = slap_zn_mem_create(
311 SLAP_ZONE_INITSIZE, SLAP_ZONE_MAXSIZE,
312 SLAP_ZONE_DELTA, SLAP_ZONE_SIZE);
316 if ( bdb->bi_idl_cache_max_size ) {
317 bdb->bi_idl_tree = NULL;
318 bdb->bi_idl_cache_size = 0;
321 flags = DB_THREAD | bdb->bi_db_opflags;
323 #ifdef DB_AUTO_COMMIT
325 flags |= DB_AUTO_COMMIT;
328 bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
329 BDB_INDICES * sizeof(struct bdb_db_info *) );
331 /* open (and create) main database */
332 for( i = 0; bdbi_databases[i].name; i++ ) {
333 struct bdb_db_info *db;
335 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
337 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
339 Debug( LDAP_DEBUG_ANY,
340 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
341 bdb->bi_dbenv_home, db_strerror(rc), rc );
345 if( i == BDB_ID2ENTRY ) {
346 if ( slapMode & SLAP_TOOL_MODE )
347 db->bdi_db->mpf->set_priority( db->bdi_db->mpf,
348 DB_PRIORITY_VERY_LOW );
350 rc = db->bdi_db->set_pagesize( db->bdi_db,
351 BDB_ID2ENTRY_PAGESIZE );
352 if ( slapMode & SLAP_TOOL_READMAIN ) {
358 rc = db->bdi_db->set_flags( db->bdi_db,
359 DB_DUP | DB_DUPSORT );
361 if ( slapMode & SLAP_TOOL_READONLY ) {
367 if ( slapMode & (SLAP_TOOL_READONLY|SLAP_TOOL_READMAIN) ) {
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 );
396 char buf[SLAP_TEXT_BUFLEN];
398 snprintf( buf, sizeof(buf), "%s/%s",
399 bdb->bi_dbenv_home, bdbi_databases[i].file );
400 Debug( LDAP_DEBUG_ANY,
401 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
402 buf, db_strerror(rc), rc );
403 db->bdi_db->close( db->bdi_db, 0 );
407 flags &= ~(DB_CREATE | DB_RDONLY);
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 );
418 Debug( LDAP_DEBUG_ANY,
419 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
420 bdb->bi_dbenv_home, db_strerror(rc), rc );
425 XLOCK_ID(bdb->bi_dbenv, &bdb->bi_cache.c_locker);
429 rc = bdb_monitor_db_open( be );
434 bdb->bi_flags |= BDB_IS_OPEN;
436 entry_prealloc( bdb->bi_cache.c_maxsize );
437 attr_prealloc( bdb->bi_cache.c_maxsize * 20 );
446 bdb_db_close( BackendDB *be )
449 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
450 struct bdb_db_info *db;
451 bdb_idl_cache_entry_t *entry, *next_entry;
453 /* monitor handling */
454 (void)bdb_monitor_db_close( be );
456 bdb->bi_flags &= ~BDB_IS_OPEN;
458 ber_bvarray_free( bdb->bi_db_config );
459 bdb->bi_db_config = NULL;
461 while( bdb->bi_databases && bdb->bi_ndatabases-- ) {
462 db = bdb->bi_databases[bdb->bi_ndatabases];
463 rc = db->bdi_db->close( db->bdi_db, 0 );
464 /* Lower numbered names are not strdup'd */
465 if( bdb->bi_ndatabases >= BDB_NDB )
466 free( db->bdi_name );
469 free( bdb->bi_databases );
470 bdb->bi_databases = NULL;
472 bdb_cache_release_all (&bdb->bi_cache);
474 if ( bdb->bi_idl_cache_max_size ) {
475 avl_free( bdb->bi_idl_tree, NULL );
476 bdb->bi_idl_tree = NULL;
477 entry = bdb->bi_idl_lru_head;
478 while ( entry != NULL ) {
479 next_entry = entry->idl_lru_next;
482 free( entry->kstr.bv_val );
486 bdb->bi_idl_lru_head = bdb->bi_idl_lru_tail = NULL;
489 /* close db environment */
490 if( bdb->bi_dbenv ) {
491 /* Free cache locker if we enabled locking */
492 if ( !( slapMode & SLAP_TOOL_QUICK )) {
493 XLOCK_ID_FREE(bdb->bi_dbenv, bdb->bi_cache.c_locker);
494 bdb->bi_cache.c_locker = 0;
497 /* force a checkpoint, but not if we were ReadOnly,
498 * and not in Quick mode since there are no transactions there.
500 if ( !( slapMode & ( SLAP_TOOL_QUICK|SLAP_TOOL_READONLY ))) {
501 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
503 Debug( LDAP_DEBUG_ANY,
504 "bdb_db_close: txn_checkpoint failed: %s (%d)\n",
505 db_strerror(rc), rc, 0 );
509 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
510 bdb->bi_dbenv = NULL;
512 Debug( LDAP_DEBUG_ANY,
513 "bdb_db_close: close failed: %s (%d)\n",
514 db_strerror(rc), rc, 0 );
519 rc = alock_close( &bdb->bi_alock_info );
521 Debug( LDAP_DEBUG_ANY,
522 "bdb_db_close: alock_close failed\n", 0, 0, 0 );
530 bdb_db_destroy( BackendDB *be )
532 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
534 /* monitor handling */
535 (void)bdb_monitor_db_destroy( be );
537 if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
538 if( bdb->bi_db_config_path ) ch_free( bdb->bi_db_config_path );
540 bdb_attr_index_destroy( bdb );
542 ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
543 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_head_mutex );
544 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_tail_mutex );
545 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_dntree.bei_kids_mutex );
547 ldap_pvt_thread_mutex_destroy( &bdb->bi_modrdns_mutex );
549 ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
550 ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
551 ldap_pvt_thread_rdwr_destroy( &bdb->bi_idl_tree_rwlock );
552 ldap_pvt_thread_mutex_destroy( &bdb->bi_idl_tree_lrulock );
555 be->be_private = NULL;
566 static char *controls[] = {
568 LDAP_CONTROL_MANAGEDSAIT,
570 LDAP_CONTROL_PAGEDRESULTS,
571 LDAP_CONTROL_PRE_READ,
572 LDAP_CONTROL_POST_READ,
573 LDAP_CONTROL_SUBENTRIES,
574 LDAP_CONTROL_X_PERMISSIVE_MODIFY,
576 LDAP_CONTROL_X_TXN_SPEC,
581 /* initialize the underlying database system */
582 Debug( LDAP_DEBUG_TRACE,
583 LDAP_XSTRING(bdb_back_initialize) ": initialize "
584 BDB_UCTYPE " backend\n", 0, 0, 0 );
587 SLAP_BFLAG_INCREMENT |
588 SLAP_BFLAG_SUBENTRIES |
590 SLAP_BFLAG_REFERRALS;
592 bi->bi_controls = controls;
594 { /* version check */
595 int major, minor, patch, ver;
596 char *version = db_version( &major, &minor, &patch );
600 /* All our stdio does an ASCII to EBCDIC conversion on
601 * the output. Strings from the BDB library are already
602 * in EBCDIC; we have to go back and forth...
604 strcpy( v2, version );
609 ver = (major << 24) | (minor << 16) | patch;
610 if( ver != DB_VERSION_FULL ) {
611 /* fail if a versions don't match */
612 Debug( LDAP_DEBUG_ANY,
613 LDAP_XSTRING(bdb_back_initialize) ": "
614 "BDB library version mismatch:"
615 " expected " DB_VERSION_STRING ","
616 " got %s\n", version, 0, 0 );
620 Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(bdb_back_initialize)
621 ": %s\n", version, 0, 0 );
624 db_env_set_func_free( ber_memfree );
625 db_env_set_func_malloc( (db_malloc *)ber_memalloc );
626 db_env_set_func_realloc( (db_realloc *)ber_memrealloc );
628 /* This is a no-op on a NO_THREAD build. Leave the default
629 * alone so that BDB will sleep on interprocess conflicts.
631 db_env_set_func_yield( ldap_pvt_thread_yield );
639 bi->bi_db_init = bdb_db_init;
640 bi->bi_db_config = config_generic_wrapper;
641 bi->bi_db_open = bdb_db_open;
642 bi->bi_db_close = bdb_db_close;
643 bi->bi_db_destroy = bdb_db_destroy;
645 bi->bi_op_add = bdb_add;
646 bi->bi_op_bind = bdb_bind;
647 bi->bi_op_compare = bdb_compare;
648 bi->bi_op_delete = bdb_delete;
649 bi->bi_op_modify = bdb_modify;
650 bi->bi_op_modrdn = bdb_modrdn;
651 bi->bi_op_search = bdb_search;
653 bi->bi_op_unbind = 0;
655 bi->bi_extended = bdb_extended;
657 bi->bi_chk_referrals = bdb_referrals;
658 bi->bi_operational = bdb_operational;
659 bi->bi_has_subordinates = bdb_hasSubordinates;
660 bi->bi_entry_release_rw = bdb_entry_release;
661 bi->bi_entry_get_rw = bdb_entry_get;
664 * hooks for slap tools
666 bi->bi_tool_entry_open = bdb_tool_entry_open;
667 bi->bi_tool_entry_close = bdb_tool_entry_close;
668 bi->bi_tool_entry_first = bdb_tool_entry_next;
669 bi->bi_tool_entry_next = bdb_tool_entry_next;
670 bi->bi_tool_entry_get = bdb_tool_entry_get;
671 bi->bi_tool_entry_put = bdb_tool_entry_put;
672 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
673 bi->bi_tool_sync = 0;
674 bi->bi_tool_dn2id_get = bdb_tool_dn2id_get;
675 bi->bi_tool_id2entry_get = bdb_tool_id2entry_get;
676 bi->bi_tool_entry_modify = bdb_tool_entry_modify;
678 bi->bi_connection_init = 0;
679 bi->bi_connection_destroy = 0;
681 rc = bdb_back_init_cf( bi );
686 #if (SLAPD_BDB == SLAPD_MOD_DYNAMIC && !defined(BDB_HIER)) || \
687 (SLAPD_HDB == SLAPD_MOD_DYNAMIC && defined(BDB_HIER))
689 /* conditionally define the init_module() function */
691 SLAP_BACKEND_INIT_MODULE( hdb )
692 #else /* !BDB_HIER */
693 SLAP_BACKEND_INIT_MODULE( bdb )
694 #endif /* !BDB_HIER */
696 #endif /* SLAPD_[BH]DB == SLAPD_MOD_DYNAMIC */