1 /* init.c - initialize bdb backend */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2015 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;
116 if ( be->be_suffix == NULL ) {
117 Debug( LDAP_DEBUG_ANY,
118 LDAP_XSTRING(bdb_db_open) ": need suffix.\n",
123 Debug( LDAP_DEBUG_ARGS,
124 LDAP_XSTRING(bdb_db_open) ": \"%s\"\n",
125 be->be_suffix[0].bv_val, 0, 0 );
127 /* Check existence of dbenv_home. Any error means trouble */
128 rc = stat( bdb->bi_dbenv_home, &stat1 );
130 Debug( LDAP_DEBUG_ANY,
131 LDAP_XSTRING(bdb_db_open) ": database \"%s\": "
132 "cannot access database directory \"%s\" (%d).\n",
133 be->be_suffix[0].bv_val, bdb->bi_dbenv_home, errno );
137 /* Perform database use arbitration/recovery logic */
138 alockt = (slapMode & SLAP_TOOL_READONLY) ? ALOCK_LOCKED : ALOCK_UNIQUE;
139 if ( slapMode & SLAP_TOOL_QUICK ) {
140 alockt |= ALOCK_NOSAVE;
144 rc = alock_open( &bdb->bi_alock_info,
146 bdb->bi_dbenv_home, alockt );
148 /* alockt is TRUE if the existing environment was created in Quick mode */
149 alockt = (rc & ALOCK_NOSAVE) ? 1 : 0;
152 if( rc == ALOCK_RECOVER ) {
153 Debug( LDAP_DEBUG_ANY,
154 LDAP_XSTRING(bdb_db_open) ": database \"%s\": "
155 "unclean shutdown detected; attempting recovery.\n",
156 be->be_suffix[0].bv_val, 0, 0 );
157 do_alock_recover = 1;
158 do_recover = DB_RECOVER;
159 } else if( rc == ALOCK_BUSY ) {
160 Debug( LDAP_DEBUG_ANY,
161 LDAP_XSTRING(bdb_db_open) ": database \"%s\": "
162 "database already in use.\n",
163 be->be_suffix[0].bv_val, 0, 0 );
165 } else if( rc != ALOCK_CLEAN ) {
166 Debug( LDAP_DEBUG_ANY,
167 LDAP_XSTRING(bdb_db_open) ": database \"%s\": "
168 "alock package is unstable.\n",
169 be->be_suffix[0].bv_val, 0, 0 );
172 if ( rc == ALOCK_CLEAN )
173 be->be_flags |= SLAP_DBFLAG_CLEAN;
176 * The DB_CONFIG file may have changed. If so, recover the
177 * database so that new settings are put into effect. Also
178 * note the possible absence of DB_CONFIG in the log.
180 if( stat( bdb->bi_db_config_path, &stat1 ) == 0 ) {
182 char *ptr = lutil_strcopy(path, bdb->bi_dbenv_home);
183 *ptr++ = LDAP_DIRSEP[0];
184 strcpy( ptr, "__db.001" );
185 if( stat( path, &stat2 ) == 0 ) {
186 if( stat2.st_mtime < stat1.st_mtime ) {
187 Debug( LDAP_DEBUG_ANY,
188 LDAP_XSTRING(bdb_db_open) ": DB_CONFIG for suffix \"%s\" has changed.\n",
189 be->be_suffix[0].bv_val, 0, 0 );
191 Debug( LDAP_DEBUG_ANY,
192 "Cannot use Quick mode; perform manual recovery first.\n",
194 slapMode ^= SLAP_TOOL_QUICK;
198 Debug( LDAP_DEBUG_ANY,
199 "Performing database recovery to activate new settings.\n",
202 do_recover = DB_RECOVER;
208 Debug( LDAP_DEBUG_ANY,
209 LDAP_XSTRING(bdb_db_open) ": warning - no DB_CONFIG file found "
210 "in directory %s: (%d).\n"
211 "Expect poor performance for suffix \"%s\".\n",
212 bdb->bi_dbenv_home, errno, be->be_suffix[0].bv_val );
215 /* Always let slapcat run, regardless of environment state.
216 * This can be used to cause a cache flush after an unclean
219 if ( do_recover && ( slapMode & SLAP_TOOL_READONLY )) {
220 Debug( LDAP_DEBUG_ANY,
221 LDAP_XSTRING(bdb_db_open) ": database \"%s\": "
222 "recovery skipped in read-only mode. "
223 "Run manual recovery if errors are encountered.\n",
224 be->be_suffix[0].bv_val, 0, 0 );
226 do_alock_recover = 0;
230 /* An existing environment in Quick mode has nothing to recover. */
231 if ( alockt && do_recover ) {
232 Debug( LDAP_DEBUG_ANY,
233 LDAP_XSTRING(bdb_db_open) ": database \"%s\": "
234 "cannot recover, database must be reinitialized.\n",
235 be->be_suffix[0].bv_val, 0, 0 );
240 rc = db_env_create( &bdb->bi_dbenv, 0 );
242 Debug( LDAP_DEBUG_ANY,
243 LDAP_XSTRING(bdb_db_open) ": database \"%s\": "
244 "db_env_create failed: %s (%d).\n",
245 be->be_suffix[0].bv_val, db_strerror(rc), rc );
250 strcpy( path, bdb->bi_dbenv_home );
254 dbhome = bdb->bi_dbenv_home;
257 /* If existing environment is clean but doesn't support
258 * currently requested modes, remove it.
260 if ( !do_recover && ( alockt ^ quick )) {
262 rc = bdb->bi_dbenv->remove( bdb->bi_dbenv, dbhome, DB_FORCE );
264 Debug( LDAP_DEBUG_ANY,
265 LDAP_XSTRING(bdb_db_open) ": database \"%s\": "
266 "dbenv remove failed: %s (%d).\n",
267 be->be_suffix[0].bv_val, db_strerror(rc), rc );
268 bdb->bi_dbenv = NULL;
271 rc = db_env_create( &bdb->bi_dbenv, 0 );
273 Debug( LDAP_DEBUG_ANY,
274 LDAP_XSTRING(bdb_db_open) ": database \"%s\": "
275 "db_env_create failed: %s (%d).\n",
276 be->be_suffix[0].bv_val, db_strerror(rc), rc );
281 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
282 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
284 bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
286 if ( !BER_BVISNULL( &bdb->bi_db_crypt_key )) {
287 rc = bdb->bi_dbenv->set_encrypt( bdb->bi_dbenv, bdb->bi_db_crypt_key.bv_val,
290 Debug( LDAP_DEBUG_ANY,
291 LDAP_XSTRING(bdb_db_open) ": database \"%s\": "
292 "dbenv set_encrypt failed: %s (%d).\n",
293 be->be_suffix[0].bv_val, db_strerror(rc), rc );
298 /* One long-lived TXN per thread, two TXNs per write op */
299 bdb->bi_dbenv->set_tx_max( bdb->bi_dbenv, connection_pool_max * 3 );
301 if( bdb->bi_dbenv_xflags != 0 ) {
302 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
303 bdb->bi_dbenv_xflags, 1);
305 Debug( LDAP_DEBUG_ANY,
306 LDAP_XSTRING(bdb_db_open) ": database \"%s\": "
307 "dbenv_set_flags failed: %s (%d).\n",
308 be->be_suffix[0].bv_val, db_strerror(rc), rc );
313 #define BDB_TXN_FLAGS (DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN)
315 Debug( LDAP_DEBUG_TRACE,
316 LDAP_XSTRING(bdb_db_open) ": database \"%s\": "
318 be->be_suffix[0].bv_val, bdb->bi_dbenv_home, 0);
320 flags = DB_INIT_MPOOL | DB_CREATE | DB_THREAD;
323 flags |= BDB_TXN_FLAGS;
325 /* If a key was set, use shared memory for the BDB environment */
326 if ( bdb->bi_shm_key ) {
327 bdb->bi_dbenv->set_shm_key( bdb->bi_dbenv, bdb->bi_shm_key );
328 flags |= DB_SYSTEM_MEM;
330 rc = (bdb->bi_dbenv->open)( bdb->bi_dbenv, dbhome,
331 flags | do_recover, bdb->bi_dbenv_mode );
334 /* Regular open failed, probably a missing shm environment.
335 * Start over, do a recovery.
337 if ( !do_recover && bdb->bi_shm_key && do_retry ) {
338 bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
339 rc = db_env_create( &bdb->bi_dbenv, 0 );
341 Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(bdb_db_open)
342 ": database \"%s\": "
343 "shared memory env open failed, assuming stale env.\n",
344 be->be_suffix[0].bv_val, 0, 0 );
349 Debug( LDAP_DEBUG_ANY,
350 LDAP_XSTRING(bdb_db_open) ": database \"%s\" cannot be %s, err %d. "
351 "Restore from backup!\n",
352 be->be_suffix[0].bv_val, do_recover ? "recovered" : "opened", rc );
356 if ( do_alock_recover && alock_recover (&bdb->bi_alock_info) != 0 ) {
357 Debug( LDAP_DEBUG_ANY,
358 LDAP_XSTRING(bdb_db_open) ": database \"%s\": alock_recover failed\n",
359 be->be_suffix[0].bv_val, 0, 0 );
364 #ifdef SLAP_ZONE_ALLOC
365 if ( bdb->bi_cache.c_maxsize ) {
366 bdb->bi_cache.c_zctx = slap_zn_mem_create(
367 SLAP_ZONE_INITSIZE, SLAP_ZONE_MAXSIZE,
368 SLAP_ZONE_DELTA, SLAP_ZONE_SIZE);
372 /* dncache defaults to 0 == unlimited
373 * must be >= entrycache
375 if ( bdb->bi_cache.c_eimax && bdb->bi_cache.c_eimax < bdb->bi_cache.c_maxsize ) {
376 bdb->bi_cache.c_eimax = bdb->bi_cache.c_maxsize;
379 if ( bdb->bi_idl_cache_max_size ) {
380 bdb->bi_idl_tree = NULL;
381 bdb->bi_idl_cache_size = 0;
384 flags = DB_THREAD | bdb->bi_db_opflags;
386 #ifdef DB_AUTO_COMMIT
388 flags |= DB_AUTO_COMMIT;
391 bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
392 BDB_INDICES * sizeof(struct bdb_db_info *) );
394 /* open (and create) main database */
395 for( i = 0; bdbi_databases[i].name.bv_val; i++ ) {
396 struct bdb_db_info *db;
398 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
400 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
402 snprintf(cr->msg, sizeof(cr->msg),
403 "database \"%s\": db_create(%s) failed: %s (%d).",
404 be->be_suffix[0].bv_val,
405 bdb->bi_dbenv_home, db_strerror(rc), rc );
406 Debug( LDAP_DEBUG_ANY,
407 LDAP_XSTRING(bdb_db_open) ": %s\n",
413 if( !BER_BVISNULL( &bdb->bi_db_crypt_key )) {
414 rc = db->bdi_db->set_flags( db->bdi_db, DB_ENCRYPT );
416 snprintf(cr->msg, sizeof(cr->msg),
417 "database \"%s\": db set_flags(DB_ENCRYPT)(%s) failed: %s (%d).",
418 be->be_suffix[0].bv_val,
419 bdb->bi_dbenv_home, db_strerror(rc), rc );
420 Debug( LDAP_DEBUG_ANY,
421 LDAP_XSTRING(bdb_db_open) ": %s\n",
423 db->bdi_db->close( db->bdi_db, 0 );
429 if( bdb->bi_flags & BDB_CHKSUM ) {
430 rc = db->bdi_db->set_flags( db->bdi_db, DB_CHKSUM );
432 snprintf(cr->msg, sizeof(cr->msg),
433 "database \"%s\": db set_flags(DB_CHKSUM)(%s) failed: %s (%d).",
434 be->be_suffix[0].bv_val,
435 bdb->bi_dbenv_home, db_strerror(rc), rc );
436 Debug( LDAP_DEBUG_ANY,
437 LDAP_XSTRING(bdb_db_open) ": %s\n",
439 db->bdi_db->close( db->bdi_db, 0 );
445 rc = bdb_db_findsize( bdb, (struct berval *)&bdbi_databases[i].name );
447 if( i == BDB_ID2ENTRY ) {
448 if ( !rc ) rc = BDB_ID2ENTRY_PAGESIZE;
449 rc = db->bdi_db->set_pagesize( db->bdi_db, rc );
451 if ( slapMode & SLAP_TOOL_MODE )
452 db->bdi_db->mpf->set_priority( db->bdi_db->mpf,
453 DB_PRIORITY_VERY_LOW );
455 if ( slapMode & SLAP_TOOL_READMAIN ) {
461 /* Use FS default size if not configured */
463 rc = db->bdi_db->set_pagesize( db->bdi_db, rc );
465 rc = db->bdi_db->set_flags( db->bdi_db,
466 DB_DUP | DB_DUPSORT );
468 if ( slapMode & SLAP_TOOL_READONLY ) {
474 rc = db->bdi_db->set_dup_compare( db->bdi_db,
476 if ( slapMode & (SLAP_TOOL_READONLY|SLAP_TOOL_READMAIN) ) {
485 strcpy( path, bdbi_databases[i].file );
487 rc = DB_OPEN( db->bdi_db,
489 /* bdbi_databases[i].name, */ NULL,
490 bdbi_databases[i].type,
491 bdbi_databases[i].flags | flags,
492 bdb->bi_dbenv_mode );
494 rc = DB_OPEN( db->bdi_db,
495 bdbi_databases[i].file,
496 /* bdbi_databases[i].name, */ NULL,
497 bdbi_databases[i].type,
498 bdbi_databases[i].flags | flags,
499 bdb->bi_dbenv_mode );
503 snprintf( cr->msg, sizeof(cr->msg), "database \"%s\": "
504 "db_open(%s/%s) failed: %s (%d).",
505 be->be_suffix[0].bv_val,
506 bdb->bi_dbenv_home, bdbi_databases[i].file,
507 db_strerror(rc), rc );
508 Debug( LDAP_DEBUG_ANY,
509 LDAP_XSTRING(bdb_db_open) ": %s\n",
511 db->bdi_db->close( db->bdi_db, 0 );
516 flags &= ~(DB_CREATE | DB_RDONLY);
517 db->bdi_name = bdbi_databases[i].name;
518 bdb->bi_databases[i] = db;
521 bdb->bi_databases[i] = NULL;
522 bdb->bi_ndatabases = i;
525 rc = bdb_last_id( be, NULL );
527 snprintf( cr->msg, sizeof(cr->msg), "database \"%s\": "
528 "last_id(%s) failed: %s (%d).",
529 be->be_suffix[0].bv_val, bdb->bi_dbenv_home,
530 db_strerror(rc), rc );
531 Debug( LDAP_DEBUG_ANY,
532 LDAP_XSTRING(bdb_db_open) ": %s\n",
538 int txflag = DB_READ_COMMITTED;
539 /* avoid deadlocks in server; tools should
540 * wait since they have no deadlock retry mechanism.
542 if ( slapMode & SLAP_SERVER_MODE )
543 txflag |= DB_TXN_NOWAIT;
544 TXN_BEGIN(bdb->bi_dbenv, NULL, &bdb->bi_cache.c_txn, txflag);
547 entry_prealloc( bdb->bi_cache.c_maxsize );
548 attr_prealloc( bdb->bi_cache.c_maxsize * 20 );
550 /* setup for empty-DN contexts */
551 if ( BER_BVISEMPTY( &be->be_nsuffix[0] )) {
552 rc = bdb_id2entry( be, NULL, 0, &e );
555 struct berval gluebv = BER_BVC("glue");
560 ber_dupbv( &e->e_name, (struct berval *)&slap_empty_bv );
561 ber_dupbv( &e->e_nname, (struct berval *)&slap_empty_bv );
562 attr_merge_one( e, slap_schema.si_ad_objectClass,
564 attr_merge_one( e, slap_schema.si_ad_structuralObjectClass,
569 op.o_dn = be->be_rootdn;
570 op.o_ndn = be->be_rootndn;
571 slap_add_opattrs( &op, NULL, NULL, 0, 0 );
573 e->e_ocflags = SLAP_OC_GLUE|SLAP_OC__END;
574 e->e_private = &bdb->bi_cache.c_dntree;
575 bdb->bi_cache.c_dntree.bei_e = e;
578 rc = bdb_monitor_db_open( be );
583 bdb->bi_flags |= BDB_IS_OPEN;
588 bdb_db_close( be, NULL );
593 bdb_db_close( BackendDB *be, ConfigReply *cr )
596 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
597 struct bdb_db_info *db;
598 bdb_idl_cache_entry_t *entry, *next_entry;
600 /* monitor handling */
601 (void)bdb_monitor_db_close( be );
604 Entry *e = bdb->bi_cache.c_dntree.bei_e;
606 bdb->bi_cache.c_dntree.bei_e = NULL;
608 bdb_entry_return( e );
612 bdb->bi_flags &= ~BDB_IS_OPEN;
614 ber_bvarray_free( bdb->bi_db_config );
615 bdb->bi_db_config = NULL;
617 if( bdb->bi_dbenv ) {
618 /* Free cache locker if we enabled locking.
619 * TXNs must all be closed before DBs...
621 if ( !( slapMode & SLAP_TOOL_QUICK ) && bdb->bi_cache.c_txn ) {
622 TXN_ABORT( bdb->bi_cache.c_txn );
623 bdb->bi_cache.c_txn = NULL;
625 bdb_reader_flush( bdb->bi_dbenv );
628 while( bdb->bi_databases && bdb->bi_ndatabases-- ) {
629 db = bdb->bi_databases[bdb->bi_ndatabases];
630 rc = db->bdi_db->close( db->bdi_db, 0 );
631 /* Lower numbered names are not strdup'd */
632 if( bdb->bi_ndatabases >= BDB_NDB )
633 free( db->bdi_name.bv_val );
636 free( bdb->bi_databases );
637 bdb->bi_databases = NULL;
639 bdb_cache_release_all (&bdb->bi_cache);
641 if ( bdb->bi_idl_cache_size ) {
642 avl_free( bdb->bi_idl_tree, NULL );
643 bdb->bi_idl_tree = NULL;
644 entry = bdb->bi_idl_lru_head;
646 next_entry = entry->idl_lru_next;
649 free( entry->kstr.bv_val );
652 } while ( entry != bdb->bi_idl_lru_head );
653 bdb->bi_idl_lru_head = bdb->bi_idl_lru_tail = NULL;
656 /* close db environment */
657 if( bdb->bi_dbenv ) {
658 /* force a checkpoint, but not if we were ReadOnly,
659 * and not in Quick mode since there are no transactions there.
661 if ( !( slapMode & ( SLAP_TOOL_QUICK|SLAP_TOOL_READONLY ))) {
662 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
664 Debug( LDAP_DEBUG_ANY,
665 "bdb_db_close: database \"%s\": "
666 "txn_checkpoint failed: %s (%d).\n",
667 be->be_suffix[0].bv_val, db_strerror(rc), rc );
671 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
672 bdb->bi_dbenv = NULL;
674 Debug( LDAP_DEBUG_ANY,
675 "bdb_db_close: database \"%s\": "
676 "close failed: %s (%d)\n",
677 be->be_suffix[0].bv_val, db_strerror(rc), rc );
682 rc = alock_close( &bdb->bi_alock_info, slapMode & SLAP_TOOL_QUICK );
684 Debug( LDAP_DEBUG_ANY,
685 "bdb_db_close: database \"%s\": alock_close failed\n",
686 be->be_suffix[0].bv_val, 0, 0 );
694 bdb_db_destroy( BackendDB *be, ConfigReply *cr )
696 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
698 /* stop and remove checkpoint task */
699 if ( bdb->bi_txn_cp_task ) {
700 struct re_s *re = bdb->bi_txn_cp_task;
701 bdb->bi_txn_cp_task = NULL;
702 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
703 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ) )
704 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
705 ldap_pvt_runqueue_remove( &slapd_rq, re );
706 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
709 /* monitor handling */
710 (void)bdb_monitor_db_destroy( be );
712 if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
713 if( bdb->bi_db_config_path ) ch_free( bdb->bi_db_config_path );
715 bdb_attr_index_destroy( bdb );
717 ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
718 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_lru_mutex );
719 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_count_mutex );
720 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_eifree_mutex );
721 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_dntree.bei_kids_mutex );
723 ldap_pvt_thread_mutex_destroy( &bdb->bi_modrdns_mutex );
725 ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
726 ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
727 ldap_pvt_thread_rdwr_destroy( &bdb->bi_idl_tree_rwlock );
728 ldap_pvt_thread_mutex_destroy( &bdb->bi_idl_tree_lrulock );
731 be->be_private = NULL;
742 static char *controls[] = {
744 LDAP_CONTROL_MANAGEDSAIT,
746 LDAP_CONTROL_PAGEDRESULTS,
747 LDAP_CONTROL_PRE_READ,
748 LDAP_CONTROL_POST_READ,
749 LDAP_CONTROL_SUBENTRIES,
750 LDAP_CONTROL_X_PERMISSIVE_MODIFY,
752 LDAP_CONTROL_X_TXN_SPEC,
757 /* initialize the underlying database system */
758 Debug( LDAP_DEBUG_TRACE,
759 LDAP_XSTRING(bdb_back_initialize) ": initialize "
760 BDB_UCTYPE " backend\n", 0, 0, 0 );
763 SLAP_BFLAG_INCREMENT |
764 SLAP_BFLAG_SUBENTRIES |
766 SLAP_BFLAG_REFERRALS;
768 bi->bi_controls = controls;
770 { /* version check */
771 int major, minor, patch, ver;
772 char *version = db_version( &major, &minor, &patch );
776 /* All our stdio does an ASCII to EBCDIC conversion on
777 * the output. Strings from the BDB library are already
778 * in EBCDIC; we have to go back and forth...
780 strcpy( v2, version );
785 ver = (major << 24) | (minor << 16) | patch;
786 if( ver != DB_VERSION_FULL ) {
787 /* fail if a versions don't match */
788 Debug( LDAP_DEBUG_ANY,
789 LDAP_XSTRING(bdb_back_initialize) ": "
790 "BDB library version mismatch:"
791 " expected " DB_VERSION_STRING ","
792 " got %s\n", version, 0, 0 );
796 Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(bdb_back_initialize)
797 ": %s\n", version, 0, 0 );
800 db_env_set_func_free( ber_memfree );
801 db_env_set_func_malloc( (db_malloc *)ber_memalloc );
802 db_env_set_func_realloc( (db_realloc *)ber_memrealloc );
803 #if !defined(NO_THREAD) && DB_VERSION_FULL <= 0x04070000
804 /* This is a no-op on a NO_THREAD build. Leave the default
805 * alone so that BDB will sleep on interprocess conflicts.
806 * Don't bother on BDB 4.7...
808 db_env_set_func_yield( ldap_pvt_thread_yield );
816 bi->bi_db_init = bdb_db_init;
817 bi->bi_db_config = config_generic_wrapper;
818 bi->bi_db_open = bdb_db_open;
819 bi->bi_db_close = bdb_db_close;
820 bi->bi_db_destroy = bdb_db_destroy;
822 bi->bi_op_add = bdb_add;
823 bi->bi_op_bind = bdb_bind;
824 bi->bi_op_compare = bdb_compare;
825 bi->bi_op_delete = bdb_delete;
826 bi->bi_op_modify = bdb_modify;
827 bi->bi_op_modrdn = bdb_modrdn;
828 bi->bi_op_search = bdb_search;
830 bi->bi_op_unbind = 0;
832 bi->bi_extended = bdb_extended;
834 bi->bi_chk_referrals = bdb_referrals;
835 bi->bi_operational = bdb_operational;
836 bi->bi_has_subordinates = bdb_hasSubordinates;
837 bi->bi_entry_release_rw = bdb_entry_release;
838 bi->bi_entry_get_rw = bdb_entry_get;
841 * hooks for slap tools
843 bi->bi_tool_entry_open = bdb_tool_entry_open;
844 bi->bi_tool_entry_close = bdb_tool_entry_close;
845 bi->bi_tool_entry_first = backend_tool_entry_first;
846 bi->bi_tool_entry_first_x = bdb_tool_entry_first_x;
847 bi->bi_tool_entry_next = bdb_tool_entry_next;
848 bi->bi_tool_entry_get = bdb_tool_entry_get;
849 bi->bi_tool_entry_put = bdb_tool_entry_put;
850 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
851 bi->bi_tool_sync = 0;
852 bi->bi_tool_dn2id_get = bdb_tool_dn2id_get;
853 bi->bi_tool_entry_modify = bdb_tool_entry_modify;
855 bi->bi_connection_init = 0;
856 bi->bi_connection_destroy = 0;
858 rc = bdb_back_init_cf( bi );
863 #if (SLAPD_BDB == SLAPD_MOD_DYNAMIC && !defined(BDB_HIER)) || \
864 (SLAPD_HDB == SLAPD_MOD_DYNAMIC && defined(BDB_HIER))
866 /* conditionally define the init_module() function */
868 SLAP_BACKEND_INIT_MODULE( hdb )
869 #else /* !BDB_HIER */
870 SLAP_BACKEND_INIT_MODULE( bdb )
871 #endif /* !BDB_HIER */
873 #endif /* SLAPD_[BH]DB == SLAPD_MOD_DYNAMIC */