1 /* init.c - initialize bdb backend */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2008 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>
31 static const struct bdbi_database {
36 } bdbi_databases[] = {
37 { "id2entry" BDB_SUFFIX, BER_BVC("id2entry"), DB_BTREE, 0 },
38 { "dn2id" BDB_SUFFIX, BER_BVC("dn2id"), DB_BTREE, 0 },
39 { NULL, BER_BVNULL, 0, 0 }
42 typedef void * db_malloc(size_t);
43 typedef void * db_realloc(void *, size_t);
45 #define bdb_db_init BDB_SYMBOL(db_init)
46 #define bdb_db_open BDB_SYMBOL(db_open)
47 #define bdb_db_close BDB_SYMBOL(db_close)
50 bdb_db_init( BackendDB *be, ConfigReply *cr )
55 Debug( LDAP_DEBUG_TRACE,
56 LDAP_XSTRING(bdb_db_init) ": Initializing " BDB_UCTYPE " database\n",
59 /* allocate backend-database-specific stuff */
60 bdb = (struct bdb_info *) ch_calloc( 1, sizeof(struct bdb_info) );
62 /* DBEnv parameters */
63 bdb->bi_dbenv_home = ch_strdup( SLAPD_DEFAULT_DB_DIR );
64 bdb->bi_dbenv_xflags = DB_TIME_NOTGRANTED;
65 bdb->bi_dbenv_mode = SLAPD_DEFAULT_DB_MODE;
67 bdb->bi_cache.c_maxsize = DEFAULT_CACHE_SIZE;
68 bdb->bi_cache.c_minfree = 1;
70 bdb->bi_lock_detect = DB_LOCK_DEFAULT;
71 bdb->bi_search_stack_depth = DEFAULT_SEARCH_STACK_DEPTH;
72 bdb->bi_search_stack = NULL;
74 ldap_pvt_thread_mutex_init( &bdb->bi_database_mutex );
75 ldap_pvt_thread_mutex_init( &bdb->bi_lastid_mutex );
77 ldap_pvt_thread_mutex_init( &bdb->bi_modrdns_mutex );
79 ldap_pvt_thread_mutex_init( &bdb->bi_cache.c_lru_mutex );
80 ldap_pvt_thread_mutex_init( &bdb->bi_cache.c_count_mutex );
81 ldap_pvt_thread_mutex_init( &bdb->bi_cache.c_eifree_mutex );
82 ldap_pvt_thread_mutex_init( &bdb->bi_cache.c_dntree.bei_kids_mutex );
83 ldap_pvt_thread_rdwr_init ( &bdb->bi_cache.c_rwlock );
84 ldap_pvt_thread_rdwr_init( &bdb->bi_idl_tree_rwlock );
85 ldap_pvt_thread_mutex_init( &bdb->bi_idl_tree_lrulock );
88 be->be_cf_ocs = be->bd_info->bi_cf_ocs;
90 #ifndef BDB_MULTIPLE_SUFFIXES
91 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_ONE_SUFFIX;
94 rc = bdb_monitor_db_init( be );
100 bdb_db_close( BackendDB *be, ConfigReply *cr );
103 bdb_db_open( BackendDB *be, ConfigReply *cr )
106 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
107 struct stat stat1, stat2;
109 char path[MAXPATHLEN];
112 int do_recover = 0, do_alock_recover = 0;
113 int alockt, quick = 0;
115 if ( be->be_suffix == NULL ) {
116 Debug( LDAP_DEBUG_ANY,
117 LDAP_XSTRING(bdb_db_open) ": need suffix.\n",
122 Debug( LDAP_DEBUG_ARGS,
123 LDAP_XSTRING(bdb_db_open) ": \"%s\"\n",
124 be->be_suffix[0].bv_val, 0, 0 );
126 /* Check existence of dbenv_home. Any error means trouble */
127 rc = stat( bdb->bi_dbenv_home, &stat1 );
129 Debug( LDAP_DEBUG_ANY,
130 LDAP_XSTRING(bdb_db_open) ": database \"%s\": "
131 "cannot access database directory \"%s\" (%d).\n",
132 be->be_suffix[0].bv_val, bdb->bi_dbenv_home, errno );
136 /* Perform database use arbitration/recovery logic */
137 alockt = (slapMode & SLAP_TOOL_READONLY) ? ALOCK_LOCKED : ALOCK_UNIQUE;
138 if ( slapMode & SLAP_TOOL_QUICK ) {
139 alockt |= ALOCK_NOSAVE;
143 rc = alock_open( &bdb->bi_alock_info,
145 bdb->bi_dbenv_home, alockt );
147 /* alockt is TRUE if the existing environment was created in Quick mode */
148 alockt = (rc & ALOCK_NOSAVE) ? 1 : 0;
151 if( rc == ALOCK_RECOVER ) {
152 Debug( LDAP_DEBUG_ANY,
153 LDAP_XSTRING(bdb_db_open) ": database \"%s\": "
154 "unclean shutdown detected; attempting recovery.\n",
155 be->be_suffix[0].bv_val, 0, 0 );
156 do_alock_recover = 1;
157 do_recover = DB_RECOVER;
158 } else if( rc == ALOCK_BUSY ) {
159 Debug( LDAP_DEBUG_ANY,
160 LDAP_XSTRING(bdb_db_open) ": database \"%s\": "
161 "database already in use.\n",
162 be->be_suffix[0].bv_val, 0, 0 );
164 } else if( rc != ALOCK_CLEAN ) {
165 Debug( LDAP_DEBUG_ANY,
166 LDAP_XSTRING(bdb_db_open) ": database \"%s\": "
167 "alock package is unstable.\n",
168 be->be_suffix[0].bv_val, 0, 0 );
171 if ( rc == ALOCK_CLEAN )
172 be->be_flags |= SLAP_DBFLAG_CLEAN;
175 * The DB_CONFIG file may have changed. If so, recover the
176 * database so that new settings are put into effect. Also
177 * note the possible absence of DB_CONFIG in the log.
179 if( stat( bdb->bi_db_config_path, &stat1 ) == 0 ) {
181 char *ptr = lutil_strcopy(path, bdb->bi_dbenv_home);
182 *ptr++ = LDAP_DIRSEP[0];
183 strcpy( ptr, "__db.001" );
184 if( stat( path, &stat2 ) == 0 ) {
185 if( stat2.st_mtime < stat1.st_mtime ) {
186 Debug( LDAP_DEBUG_ANY,
187 LDAP_XSTRING(bdb_db_open) ": DB_CONFIG for suffix \"%s\" has changed.\n",
188 be->be_suffix[0].bv_val, 0, 0 );
190 Debug( LDAP_DEBUG_ANY,
191 "Cannot use Quick mode; perform manual recovery first.\n",
193 slapMode ^= SLAP_TOOL_QUICK;
197 Debug( LDAP_DEBUG_ANY,
198 "Performing database recovery to activate new settings.\n",
201 do_recover = DB_RECOVER;
207 Debug( LDAP_DEBUG_ANY,
208 LDAP_XSTRING(bdb_db_open) ": warning - no DB_CONFIG file found "
209 "in directory %s: (%d).\n"
210 "Expect poor performance for suffix \"%s\".\n",
211 bdb->bi_dbenv_home, errno, be->be_suffix[0].bv_val );
214 /* Always let slapcat run, regardless of environment state.
215 * This can be used to cause a cache flush after an unclean
218 if ( do_recover && ( slapMode & SLAP_TOOL_READONLY )) {
219 Debug( LDAP_DEBUG_ANY,
220 LDAP_XSTRING(bdb_db_open) ": database \"%s\": "
221 "recovery skipped in read-only mode. "
222 "Run manual recovery if errors are encountered.\n",
223 be->be_suffix[0].bv_val, 0, 0 );
228 /* An existing environment in Quick mode has nothing to recover. */
229 if ( alockt && do_recover ) {
230 Debug( LDAP_DEBUG_ANY,
231 LDAP_XSTRING(bdb_db_open) ": database \"%s\": "
232 "cannot recover, database must be reinitialized.\n",
233 be->be_suffix[0].bv_val, 0, 0 );
238 rc = db_env_create( &bdb->bi_dbenv, 0 );
240 Debug( LDAP_DEBUG_ANY,
241 LDAP_XSTRING(bdb_db_open) ": database \"%s\": "
242 "db_env_create failed: %s (%d).\n",
243 be->be_suffix[0].bv_val, db_strerror(rc), rc );
248 strcpy( path, bdb->bi_dbenv_home );
252 dbhome = bdb->bi_dbenv_home;
255 /* If existing environment is clean but doesn't support
256 * currently requested modes, remove it.
258 if ( !do_recover && ( alockt ^ quick )) {
260 rc = bdb->bi_dbenv->remove( bdb->bi_dbenv, dbhome, DB_FORCE );
262 Debug( LDAP_DEBUG_ANY,
263 LDAP_XSTRING(bdb_db_open) ": database \"%s\": "
264 "dbenv remove failed: %s (%d).\n",
265 be->be_suffix[0].bv_val, db_strerror(rc), rc );
266 bdb->bi_dbenv = NULL;
269 rc = db_env_create( &bdb->bi_dbenv, 0 );
271 Debug( LDAP_DEBUG_ANY,
272 LDAP_XSTRING(bdb_db_open) ": database \"%s\": "
273 "db_env_create failed: %s (%d).\n",
274 be->be_suffix[0].bv_val, db_strerror(rc), rc );
279 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
280 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
282 bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
284 if ( !BER_BVISNULL( &bdb->bi_db_crypt_key )) {
285 rc = bdb->bi_dbenv->set_encrypt( bdb->bi_dbenv, bdb->bi_db_crypt_key.bv_val,
288 Debug( LDAP_DEBUG_ANY,
289 LDAP_XSTRING(bdb_db_open) ": database \"%s\": "
290 "dbenv set_encrypt failed: %s (%d).\n",
291 be->be_suffix[0].bv_val, db_strerror(rc), rc );
296 /* One long-lived TXN per thread, two TXNs per write op */
297 bdb->bi_dbenv->set_tx_max( bdb->bi_dbenv, connection_pool_max * 3 );
299 if( bdb->bi_dbenv_xflags != 0 ) {
300 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
301 bdb->bi_dbenv_xflags, 1);
303 Debug( LDAP_DEBUG_ANY,
304 LDAP_XSTRING(bdb_db_open) ": database \"%s\": "
305 "dbenv_set_flags failed: %s (%d).\n",
306 be->be_suffix[0].bv_val, db_strerror(rc), rc );
311 #define BDB_TXN_FLAGS (DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN)
313 Debug( LDAP_DEBUG_TRACE,
314 LDAP_XSTRING(bdb_db_open) ": database \"%s\": "
316 be->be_suffix[0].bv_val, bdb->bi_dbenv_home, 0);
318 flags = DB_INIT_MPOOL | DB_CREATE | DB_THREAD;
321 flags |= BDB_TXN_FLAGS;
323 /* If a key was set, use shared memory for the BDB environment */
324 if ( bdb->bi_shm_key ) {
325 bdb->bi_dbenv->set_shm_key( bdb->bi_dbenv, bdb->bi_shm_key );
326 flags |= DB_SYSTEM_MEM;
328 rc = (bdb->bi_dbenv->open)( bdb->bi_dbenv, dbhome,
329 flags | do_recover, bdb->bi_dbenv_mode );
332 /* Regular open failed, probably a missing shm environment.
333 * Start over, do a recovery.
335 if ( !do_recover && bdb->bi_shm_key ) {
336 bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
337 rc = db_env_create( &bdb->bi_dbenv, 0 );
339 Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(bdb_db_open)
340 ": database \"%s\": "
341 "shared memory env open failed, assuming stale env.\n",
342 be->be_suffix[0].bv_val, 0, 0 );
346 Debug( LDAP_DEBUG_ANY,
347 LDAP_XSTRING(bdb_db_open) ": database \"%s\" cannot be %s, err %d. "
348 "Restore from backup!\n",
349 be->be_suffix[0].bv_val, do_recover ? "recovered" : "opened", rc );
353 if ( do_alock_recover && alock_recover (&bdb->bi_alock_info) != 0 ) {
354 Debug( LDAP_DEBUG_ANY,
355 LDAP_XSTRING(bdb_db_open) ": database \"%s\": alock_recover failed\n",
356 be->be_suffix[0].bv_val, 0, 0 );
361 #ifdef SLAP_ZONE_ALLOC
362 if ( bdb->bi_cache.c_maxsize ) {
363 bdb->bi_cache.c_zctx = slap_zn_mem_create(
364 SLAP_ZONE_INITSIZE, SLAP_ZONE_MAXSIZE,
365 SLAP_ZONE_DELTA, SLAP_ZONE_SIZE);
369 /* Default dncache to 2x entrycache */
370 if ( bdb->bi_cache.c_maxsize && !bdb->bi_cache.c_eimax ) {
371 bdb->bi_cache.c_eimax = bdb->bi_cache.c_maxsize * 2;
374 if ( bdb->bi_idl_cache_max_size ) {
375 bdb->bi_idl_tree = NULL;
376 bdb->bi_idl_cache_size = 0;
379 flags = DB_THREAD | bdb->bi_db_opflags;
381 #ifdef DB_AUTO_COMMIT
383 flags |= DB_AUTO_COMMIT;
386 bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
387 BDB_INDICES * sizeof(struct bdb_db_info *) );
389 /* open (and create) main database */
390 for( i = 0; bdbi_databases[i].name.bv_val; i++ ) {
391 struct bdb_db_info *db;
393 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
395 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
397 snprintf(cr->msg, sizeof(cr->msg),
398 "database \"%s\": db_create(%s) failed: %s (%d).",
399 be->be_suffix[0].bv_val,
400 bdb->bi_dbenv_home, db_strerror(rc), rc );
401 Debug( LDAP_DEBUG_ANY,
402 LDAP_XSTRING(bdb_db_open) ": %s\n",
407 if( !BER_BVISNULL( &bdb->bi_db_crypt_key )) {
408 rc = db->bdi_db->set_flags( db->bdi_db, DB_ENCRYPT );
410 snprintf(cr->msg, sizeof(cr->msg),
411 "database \"%s\": db set_flags(DB_ENCRYPT)(%s) failed: %s (%d).",
412 be->be_suffix[0].bv_val,
413 bdb->bi_dbenv_home, db_strerror(rc), rc );
414 Debug( LDAP_DEBUG_ANY,
415 LDAP_XSTRING(bdb_db_open) ": %s\n",
421 if( bdb->bi_flags & BDB_CHKSUM ) {
422 rc = db->bdi_db->set_flags( db->bdi_db, DB_CHKSUM );
424 snprintf(cr->msg, sizeof(cr->msg),
425 "database \"%s\": db set_flags(DB_CHKSUM)(%s) failed: %s (%d).",
426 be->be_suffix[0].bv_val,
427 bdb->bi_dbenv_home, db_strerror(rc), rc );
428 Debug( LDAP_DEBUG_ANY,
429 LDAP_XSTRING(bdb_db_open) ": %s\n",
435 rc = bdb_db_findsize( bdb, (struct berval *)&bdbi_databases[i].name );
437 if( i == BDB_ID2ENTRY ) {
438 if ( !rc ) rc = BDB_ID2ENTRY_PAGESIZE;
439 rc = db->bdi_db->set_pagesize( db->bdi_db, rc );
441 if ( slapMode & SLAP_TOOL_MODE )
442 db->bdi_db->mpf->set_priority( db->bdi_db->mpf,
443 DB_PRIORITY_VERY_LOW );
445 if ( slapMode & SLAP_TOOL_READMAIN ) {
451 /* Use FS default size if not configured */
453 rc = db->bdi_db->set_pagesize( db->bdi_db, rc );
455 rc = db->bdi_db->set_flags( db->bdi_db,
456 DB_DUP | DB_DUPSORT );
458 if ( slapMode & SLAP_TOOL_READONLY ) {
464 rc = db->bdi_db->set_dup_compare( db->bdi_db,
466 if ( slapMode & (SLAP_TOOL_READONLY|SLAP_TOOL_READMAIN) ) {
475 strcpy( path, bdbi_databases[i].file );
477 rc = DB_OPEN( db->bdi_db,
479 /* bdbi_databases[i].name, */ NULL,
480 bdbi_databases[i].type,
481 bdbi_databases[i].flags | flags,
482 bdb->bi_dbenv_mode );
484 rc = DB_OPEN( db->bdi_db,
485 bdbi_databases[i].file,
486 /* bdbi_databases[i].name, */ NULL,
487 bdbi_databases[i].type,
488 bdbi_databases[i].flags | flags,
489 bdb->bi_dbenv_mode );
493 snprintf( cr->msg, sizeof(cr->msg), "database \"%s\": "
494 "db_open(%s/%s) failed: %s (%d).",
495 be->be_suffix[0].bv_val,
496 bdb->bi_dbenv_home, bdbi_databases[i].file,
497 db_strerror(rc), rc );
498 Debug( LDAP_DEBUG_ANY,
499 LDAP_XSTRING(bdb_db_open) ": %s\n",
501 db->bdi_db->close( db->bdi_db, 0 );
505 flags &= ~(DB_CREATE | DB_RDONLY);
506 db->bdi_name = bdbi_databases[i].name;
507 bdb->bi_databases[i] = db;
510 bdb->bi_databases[i] = NULL;
511 bdb->bi_ndatabases = i;
514 rc = bdb_last_id( be, NULL );
516 snprintf( cr->msg, sizeof(cr->msg), "database \"%s\": "
517 "last_id(%s) failed: %s (%d).",
518 be->be_suffix[0].bv_val, bdb->bi_dbenv_home,
519 db_strerror(rc), rc );
520 Debug( LDAP_DEBUG_ANY,
521 LDAP_XSTRING(bdb_db_open) ": %s\n",
527 TXN_BEGIN(bdb->bi_dbenv, NULL, &bdb->bi_cache.c_txn, DB_READ_COMMITTED | DB_TXN_NOWAIT);
530 entry_prealloc( bdb->bi_cache.c_maxsize );
531 attr_prealloc( bdb->bi_cache.c_maxsize * 20 );
533 /* setup for empty-DN contexts */
534 if ( BER_BVISEMPTY( &be->be_nsuffix[0] )) {
535 rc = bdb_id2entry( be, NULL, 0, &e );
540 ber_dupbv( &e->e_name, (struct berval *)&slap_empty_bv );
541 ber_dupbv( &e->e_nname, (struct berval *)&slap_empty_bv );
543 e->e_ocflags = SLAP_OC_GLUE|SLAP_OC__END;
544 e->e_private = &bdb->bi_cache.c_dntree;
545 bdb->bi_cache.c_dntree.bei_e = e;
548 rc = bdb_monitor_db_open( be );
553 bdb->bi_flags |= BDB_IS_OPEN;
558 bdb_db_close( be, NULL );
563 bdb_db_close( BackendDB *be, ConfigReply *cr )
566 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
567 struct bdb_db_info *db;
568 bdb_idl_cache_entry_t *entry, *next_entry;
570 /* monitor handling */
571 (void)bdb_monitor_db_close( be );
574 Entry *e = bdb->bi_cache.c_dntree.bei_e;
576 bdb->bi_cache.c_dntree.bei_e = NULL;
578 bdb_entry_return( e );
582 bdb->bi_flags &= ~BDB_IS_OPEN;
584 ber_bvarray_free( bdb->bi_db_config );
585 bdb->bi_db_config = NULL;
587 if( bdb->bi_dbenv ) {
588 /* Free cache locker if we enabled locking.
589 * TXNs must all be closed before DBs...
591 if ( !( slapMode & SLAP_TOOL_QUICK ) && bdb->bi_cache.c_txn ) {
592 TXN_ABORT( bdb->bi_cache.c_txn );
593 bdb->bi_cache.c_txn = NULL;
595 bdb_reader_flush( bdb->bi_dbenv );
598 while( bdb->bi_databases && bdb->bi_ndatabases-- ) {
599 db = bdb->bi_databases[bdb->bi_ndatabases];
600 rc = db->bdi_db->close( db->bdi_db, 0 );
601 /* Lower numbered names are not strdup'd */
602 if( bdb->bi_ndatabases >= BDB_NDB )
603 free( db->bdi_name.bv_val );
606 free( bdb->bi_databases );
607 bdb->bi_databases = NULL;
609 bdb_cache_release_all (&bdb->bi_cache);
611 if ( bdb->bi_idl_cache_size ) {
612 avl_free( bdb->bi_idl_tree, NULL );
613 bdb->bi_idl_tree = NULL;
614 entry = bdb->bi_idl_lru_head;
616 next_entry = entry->idl_lru_next;
619 free( entry->kstr.bv_val );
622 } while ( entry != bdb->bi_idl_lru_head );
623 bdb->bi_idl_lru_head = bdb->bi_idl_lru_tail = NULL;
626 /* close db environment */
627 if( bdb->bi_dbenv ) {
628 /* force a checkpoint, but not if we were ReadOnly,
629 * and not in Quick mode since there are no transactions there.
631 if ( !( slapMode & ( SLAP_TOOL_QUICK|SLAP_TOOL_READONLY ))) {
632 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
634 Debug( LDAP_DEBUG_ANY,
635 "bdb_db_close: database \"%s\": "
636 "txn_checkpoint failed: %s (%d).\n",
637 be->be_suffix[0].bv_val, db_strerror(rc), rc );
641 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
642 bdb->bi_dbenv = NULL;
644 Debug( LDAP_DEBUG_ANY,
645 "bdb_db_close: database \"%s\": "
646 "close failed: %s (%d)\n",
647 be->be_suffix[0].bv_val, db_strerror(rc), rc );
652 rc = alock_close( &bdb->bi_alock_info, slapMode & SLAP_TOOL_QUICK );
654 Debug( LDAP_DEBUG_ANY,
655 "bdb_db_close: database \"%s\": alock_close failed\n",
656 be->be_suffix[0].bv_val, 0, 0 );
664 bdb_db_destroy( BackendDB *be, ConfigReply *cr )
666 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
668 /* stop and remove checkpoint task */
669 if ( bdb->bi_txn_cp_task ) {
670 struct re_s *re = bdb->bi_txn_cp_task;
671 bdb->bi_txn_cp_task = NULL;
672 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
673 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ) )
674 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
675 ldap_pvt_runqueue_remove( &slapd_rq, re );
676 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
679 /* monitor handling */
680 (void)bdb_monitor_db_destroy( be );
682 if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
683 if( bdb->bi_db_config_path ) ch_free( bdb->bi_db_config_path );
685 bdb_attr_index_destroy( bdb );
687 ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
688 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_lru_mutex );
689 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_count_mutex );
690 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_eifree_mutex );
691 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_dntree.bei_kids_mutex );
693 ldap_pvt_thread_mutex_destroy( &bdb->bi_modrdns_mutex );
695 ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
696 ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
697 ldap_pvt_thread_rdwr_destroy( &bdb->bi_idl_tree_rwlock );
698 ldap_pvt_thread_mutex_destroy( &bdb->bi_idl_tree_lrulock );
701 be->be_private = NULL;
712 static char *controls[] = {
714 LDAP_CONTROL_MANAGEDSAIT,
716 LDAP_CONTROL_PAGEDRESULTS,
717 LDAP_CONTROL_PRE_READ,
718 LDAP_CONTROL_POST_READ,
719 LDAP_CONTROL_SUBENTRIES,
720 LDAP_CONTROL_X_PERMISSIVE_MODIFY,
722 LDAP_CONTROL_X_TXN_SPEC,
727 /* initialize the underlying database system */
728 Debug( LDAP_DEBUG_TRACE,
729 LDAP_XSTRING(bdb_back_initialize) ": initialize "
730 BDB_UCTYPE " backend\n", 0, 0, 0 );
733 SLAP_BFLAG_INCREMENT |
734 SLAP_BFLAG_SUBENTRIES |
736 SLAP_BFLAG_REFERRALS;
738 bi->bi_controls = controls;
740 { /* version check */
741 int major, minor, patch, ver;
742 char *version = db_version( &major, &minor, &patch );
746 /* All our stdio does an ASCII to EBCDIC conversion on
747 * the output. Strings from the BDB library are already
748 * in EBCDIC; we have to go back and forth...
750 strcpy( v2, version );
755 ver = (major << 24) | (minor << 16) | patch;
756 if( ver != DB_VERSION_FULL ) {
757 /* fail if a versions don't match */
758 Debug( LDAP_DEBUG_ANY,
759 LDAP_XSTRING(bdb_back_initialize) ": "
760 "BDB library version mismatch:"
761 " expected " DB_VERSION_STRING ","
762 " got %s\n", version, 0, 0 );
766 Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(bdb_back_initialize)
767 ": %s\n", version, 0, 0 );
770 db_env_set_func_free( ber_memfree );
771 db_env_set_func_malloc( (db_malloc *)ber_memalloc );
772 db_env_set_func_realloc( (db_realloc *)ber_memrealloc );
773 #if !defined(NO_THREAD) && DB_VERSION_FULL <= 0x04070000
774 /* This is a no-op on a NO_THREAD build. Leave the default
775 * alone so that BDB will sleep on interprocess conflicts.
776 * Don't bother on BDB 4.7...
778 db_env_set_func_yield( ldap_pvt_thread_yield );
786 bi->bi_db_init = bdb_db_init;
787 bi->bi_db_config = config_generic_wrapper;
788 bi->bi_db_open = bdb_db_open;
789 bi->bi_db_close = bdb_db_close;
790 bi->bi_db_destroy = bdb_db_destroy;
792 bi->bi_op_add = bdb_add;
793 bi->bi_op_bind = bdb_bind;
794 bi->bi_op_compare = bdb_compare;
795 bi->bi_op_delete = bdb_delete;
796 bi->bi_op_modify = bdb_modify;
797 bi->bi_op_modrdn = bdb_modrdn;
798 bi->bi_op_search = bdb_search;
800 bi->bi_op_unbind = 0;
802 bi->bi_extended = bdb_extended;
804 bi->bi_chk_referrals = bdb_referrals;
805 bi->bi_operational = bdb_operational;
806 bi->bi_has_subordinates = bdb_hasSubordinates;
807 bi->bi_entry_release_rw = bdb_entry_release;
808 bi->bi_entry_get_rw = bdb_entry_get;
811 * hooks for slap tools
813 bi->bi_tool_entry_open = bdb_tool_entry_open;
814 bi->bi_tool_entry_close = bdb_tool_entry_close;
815 bi->bi_tool_entry_first = bdb_tool_entry_next;
816 bi->bi_tool_entry_next = bdb_tool_entry_next;
817 bi->bi_tool_entry_get = bdb_tool_entry_get;
818 bi->bi_tool_entry_put = bdb_tool_entry_put;
819 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
820 bi->bi_tool_sync = 0;
821 bi->bi_tool_dn2id_get = bdb_tool_dn2id_get;
822 bi->bi_tool_entry_modify = bdb_tool_entry_modify;
824 bi->bi_connection_init = 0;
825 bi->bi_connection_destroy = 0;
827 rc = bdb_back_init_cf( bi );
832 #if (SLAPD_BDB == SLAPD_MOD_DYNAMIC && !defined(BDB_HIER)) || \
833 (SLAPD_HDB == SLAPD_MOD_DYNAMIC && defined(BDB_HIER))
835 /* conditionally define the init_module() function */
837 SLAP_BACKEND_INIT_MODULE( hdb )
838 #else /* !BDB_HIER */
839 SLAP_BACKEND_INIT_MODULE( bdb )
840 #endif /* !BDB_HIER */
842 #endif /* SLAPD_[BH]DB == SLAPD_MOD_DYNAMIC */