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.c_lru_mutex );
75 ldap_pvt_thread_mutex_init( &bdb->bi_cache.c_count_mutex );
76 ldap_pvt_thread_mutex_init( &bdb->bi_cache.c_eifree_mutex );
77 ldap_pvt_thread_mutex_init( &bdb->bi_cache.c_dntree.bei_kids_mutex );
78 ldap_pvt_thread_rdwr_init ( &bdb->bi_cache.c_rwlock );
79 ldap_pvt_thread_rdwr_init( &bdb->bi_idl_tree_rwlock );
80 ldap_pvt_thread_mutex_init( &bdb->bi_idl_tree_lrulock );
83 Entry *e = entry_alloc();
85 e->e_private = &bdb->bi_cache.c_dntree;
86 BER_BVSTR( &e->e_name, "" );
87 BER_BVSTR( &e->e_nname, "" );
88 bdb->bi_cache.c_dntree.bei_e = e;
92 be->be_cf_ocs = be->bd_info->bi_cf_ocs;
94 rc = bdb_monitor_db_init( be );
100 bdb_db_close( BackendDB *be );
103 bdb_db_open( BackendDB *be )
106 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
107 struct stat stat1, stat2;
109 char path[MAXPATHLEN];
111 int do_recover = 0, do_alock_recover = 0;
112 int alockt, quick = 0;
114 if ( be->be_suffix == NULL ) {
115 Debug( LDAP_DEBUG_ANY,
116 "bdb_db_open: need suffix\n",
121 Debug( LDAP_DEBUG_ARGS,
123 be->be_suffix[0].bv_val, 0, 0 );
125 #ifndef BDB_MULTIPLE_SUFFIXES
126 if ( be->be_suffix[1].bv_val ) {
127 Debug( LDAP_DEBUG_ANY,
128 "bdb_db_open: only one suffix allowed\n", 0, 0, 0 );
133 /* Check existence of dbenv_home. Any error means trouble */
134 rc = stat( bdb->bi_dbenv_home, &stat1 );
136 Debug( LDAP_DEBUG_ANY,
137 "bdb_db_open: Cannot access database directory %s (%d)\n",
138 bdb->bi_dbenv_home, errno, 0 );
142 /* Perform database use arbitration/recovery logic */
143 alockt = (slapMode & SLAP_TOOL_READONLY) ? ALOCK_LOCKED : ALOCK_UNIQUE;
144 if ( slapMode & SLAP_TOOL_QUICK ) {
145 alockt |= ALOCK_NOSAVE;
149 rc = alock_open( &bdb->bi_alock_info,
151 bdb->bi_dbenv_home, alockt );
153 /* alockt is TRUE if the existing environment was created in Quick mode */
154 alockt = (rc & ALOCK_NOSAVE) ? 1 : 0;
157 if( rc == ALOCK_RECOVER ) {
158 Debug( LDAP_DEBUG_ANY,
159 "bdb_db_open: unclean shutdown detected;"
160 " attempting recovery.\n",
162 do_alock_recover = 1;
163 do_recover = DB_RECOVER;
164 } else if( rc == ALOCK_BUSY ) {
165 Debug( LDAP_DEBUG_ANY,
166 "bdb_db_open: database already in use\n",
169 } else if( rc != ALOCK_CLEAN ) {
170 Debug( LDAP_DEBUG_ANY,
171 "bdb_db_open: alock package is unstable\n",
177 * The DB_CONFIG file may have changed. If so, recover the
178 * database so that new settings are put into effect. Also
179 * note the possible absence of DB_CONFIG in the log.
181 if( stat( bdb->bi_db_config_path, &stat1 ) == 0 ) {
183 char *ptr = lutil_strcopy(path, bdb->bi_dbenv_home);
184 *ptr++ = LDAP_DIRSEP[0];
185 strcpy( ptr, "__db.001" );
186 if( stat( path, &stat2 ) == 0 ) {
187 if( stat2.st_mtime < stat1.st_mtime ) {
188 Debug( LDAP_DEBUG_ANY,
189 "bdb_db_open: DB_CONFIG for suffix %s has changed.\n"
190 "Performing database recovery to activate new settings.\n",
191 be->be_suffix[0].bv_val, 0, 0 );
192 do_recover = DB_RECOVER;
198 Debug( LDAP_DEBUG_ANY,
199 "bdb_db_open: Warning - No DB_CONFIG file found "
200 "in directory %s: (%d)\n"
201 "Expect poor performance for suffix %s.\n",
202 bdb->bi_dbenv_home, errno, be->be_suffix[0].bv_val );
205 /* Always let slapcat run, regardless of environment state.
206 * This can be used to cause a cache flush after an unclean
209 if ( do_recover && ( slapMode & SLAP_TOOL_READONLY )) {
210 Debug( LDAP_DEBUG_ANY,
211 "bdb_db_open: Recovery skipped in read-only mode. "
212 "Run manual recovery if errors are encountered.\n",
218 /* An existing environment in Quick mode has nothing to recover. */
219 if ( alockt && do_recover ) {
220 Debug( LDAP_DEBUG_ANY,
221 "bdb_db_open: cannot recover, database must be reinitialized.\n",
227 rc = db_env_create( &bdb->bi_dbenv, 0 );
229 Debug( LDAP_DEBUG_ANY,
230 "bdb_db_open: db_env_create failed: %s (%d)\n",
231 db_strerror(rc), rc, 0 );
236 strcpy( path, bdb->bi_dbenv_home );
240 dbhome = bdb->bi_dbenv_home;
243 /* If existing environment is clean but doesn't support
244 * currently requested modes, remove it.
246 if ( !do_recover && ( alockt ^ quick )) {
247 rc = bdb->bi_dbenv->remove( bdb->bi_dbenv, dbhome, DB_FORCE );
249 Debug( LDAP_DEBUG_ANY,
250 "bdb_db_open: dbenv remove failed: %s (%d)\n",
251 db_strerror(rc), rc, 0 );
252 bdb->bi_dbenv = NULL;
255 rc = db_env_create( &bdb->bi_dbenv, 0 );
257 Debug( LDAP_DEBUG_ANY,
258 "bdb_db_open: db_env_create failed: %s (%d)\n",
259 db_strerror(rc), rc, 0 );
264 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
265 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
267 bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
269 /* One long-lived TXN per thread, two TXNs per write op */
270 bdb->bi_dbenv->set_tx_max( bdb->bi_dbenv, connection_pool_max * 3 );
272 if( bdb->bi_dbenv_xflags != 0 ) {
273 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
274 bdb->bi_dbenv_xflags, 1);
276 Debug( LDAP_DEBUG_ANY,
277 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
278 db_strerror(rc), rc, 0 );
283 #define BDB_TXN_FLAGS (DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN)
285 Debug( LDAP_DEBUG_TRACE,
286 "bdb_db_open: dbenv_open(%s)\n",
287 bdb->bi_dbenv_home, 0, 0);
289 flags = DB_INIT_MPOOL | DB_CREATE | DB_THREAD;
292 flags |= BDB_TXN_FLAGS;
294 /* If a key was set, use shared memory for the BDB environment */
295 if ( bdb->bi_shm_key ) {
296 bdb->bi_dbenv->set_shm_key( bdb->bi_dbenv, bdb->bi_shm_key );
297 flags |= DB_SYSTEM_MEM;
299 rc = bdb->bi_dbenv->open( bdb->bi_dbenv, dbhome,
300 flags | do_recover, bdb->bi_dbenv_mode );
303 Debug( LDAP_DEBUG_ANY,
304 "bdb_db_open: Database cannot be %s, err %d. "
305 "Restore from backup!\n",
306 do_recover ? "recovered" : "opened", rc, 0);
310 if ( do_alock_recover && alock_recover (&bdb->bi_alock_info) != 0 ) {
311 Debug( LDAP_DEBUG_ANY,
312 "bdb_db_open: alock_recover failed\n",
318 #ifdef SLAP_ZONE_ALLOC
319 if ( bdb->bi_cache.c_maxsize ) {
320 bdb->bi_cache.c_zctx = slap_zn_mem_create(
321 SLAP_ZONE_INITSIZE, SLAP_ZONE_MAXSIZE,
322 SLAP_ZONE_DELTA, SLAP_ZONE_SIZE);
326 if ( bdb->bi_idl_cache_max_size ) {
327 bdb->bi_idl_tree = NULL;
328 bdb->bi_idl_cache_size = 0;
331 flags = DB_THREAD | bdb->bi_db_opflags;
333 #ifdef DB_AUTO_COMMIT
335 flags |= DB_AUTO_COMMIT;
338 bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
339 BDB_INDICES * sizeof(struct bdb_db_info *) );
341 /* open (and create) main database */
342 for( i = 0; bdbi_databases[i].name; i++ ) {
343 struct bdb_db_info *db;
345 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
347 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
349 Debug( LDAP_DEBUG_ANY,
350 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
351 bdb->bi_dbenv_home, db_strerror(rc), rc );
355 if( i == BDB_ID2ENTRY ) {
356 if ( slapMode & SLAP_TOOL_MODE )
357 db->bdi_db->mpf->set_priority( db->bdi_db->mpf,
358 DB_PRIORITY_VERY_LOW );
360 rc = db->bdi_db->set_pagesize( db->bdi_db,
361 BDB_ID2ENTRY_PAGESIZE );
362 if ( slapMode & SLAP_TOOL_READMAIN ) {
368 rc = db->bdi_db->set_flags( db->bdi_db,
369 DB_DUP | DB_DUPSORT );
371 if ( slapMode & SLAP_TOOL_READONLY ) {
377 if ( slapMode & (SLAP_TOOL_READONLY|SLAP_TOOL_READMAIN) ) {
383 rc = db->bdi_db->set_pagesize( db->bdi_db,
388 strcpy( path, bdbi_databases[i].file );
390 rc = DB_OPEN( db->bdi_db,
392 /* bdbi_databases[i].name, */ NULL,
393 bdbi_databases[i].type,
394 bdbi_databases[i].flags | flags,
395 bdb->bi_dbenv_mode );
397 rc = DB_OPEN( db->bdi_db,
398 bdbi_databases[i].file,
399 /* bdbi_databases[i].name, */ NULL,
400 bdbi_databases[i].type,
401 bdbi_databases[i].flags | flags,
402 bdb->bi_dbenv_mode );
406 char buf[SLAP_TEXT_BUFLEN];
408 snprintf( buf, sizeof(buf), "%s/%s",
409 bdb->bi_dbenv_home, bdbi_databases[i].file );
410 Debug( LDAP_DEBUG_ANY,
411 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
412 buf, db_strerror(rc), rc );
413 db->bdi_db->close( db->bdi_db, 0 );
417 flags &= ~(DB_CREATE | DB_RDONLY);
418 db->bdi_name = bdbi_databases[i].name;
419 bdb->bi_databases[i] = db;
422 bdb->bi_databases[i] = NULL;
423 bdb->bi_ndatabases = i;
426 rc = bdb_last_id( be, NULL );
428 Debug( LDAP_DEBUG_ANY,
429 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
430 bdb->bi_dbenv_home, db_strerror(rc), rc );
435 XLOCK_ID(bdb->bi_dbenv, &bdb->bi_cache.c_locker);
439 rc = bdb_monitor_db_open( be );
444 bdb->bi_flags |= BDB_IS_OPEN;
446 entry_prealloc( bdb->bi_cache.c_maxsize );
447 attr_prealloc( bdb->bi_cache.c_maxsize * 20 );
456 bdb_db_close( BackendDB *be )
459 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
460 struct bdb_db_info *db;
461 bdb_idl_cache_entry_t *entry, *next_entry;
463 /* monitor handling */
464 (void)bdb_monitor_db_close( be );
466 bdb->bi_flags &= ~BDB_IS_OPEN;
468 ber_bvarray_free( bdb->bi_db_config );
469 bdb->bi_db_config = NULL;
471 while( bdb->bi_databases && bdb->bi_ndatabases-- ) {
472 db = bdb->bi_databases[bdb->bi_ndatabases];
473 rc = db->bdi_db->close( db->bdi_db, 0 );
474 /* Lower numbered names are not strdup'd */
475 if( bdb->bi_ndatabases >= BDB_NDB )
476 free( db->bdi_name );
479 free( bdb->bi_databases );
480 bdb->bi_databases = NULL;
482 bdb_cache_release_all (&bdb->bi_cache);
484 if ( bdb->bi_idl_cache_size ) {
485 avl_free( bdb->bi_idl_tree, NULL );
486 bdb->bi_idl_tree = NULL;
487 entry = bdb->bi_idl_lru_head;
489 next_entry = entry->idl_lru_next;
492 free( entry->kstr.bv_val );
495 } while ( entry != bdb->bi_idl_lru_head );
496 bdb->bi_idl_lru_head = bdb->bi_idl_lru_tail = NULL;
499 /* close db environment */
500 if( bdb->bi_dbenv ) {
501 /* Free cache locker if we enabled locking */
502 if ( !( slapMode & SLAP_TOOL_QUICK )) {
503 XLOCK_ID_FREE(bdb->bi_dbenv, bdb->bi_cache.c_locker);
504 bdb->bi_cache.c_locker = 0;
506 #ifdef BDB_REUSE_LOCKERS
507 bdb_locker_flush( bdb->bi_dbenv );
509 /* force a checkpoint, but not if we were ReadOnly,
510 * and not in Quick mode since there are no transactions there.
512 if ( !( slapMode & ( SLAP_TOOL_QUICK|SLAP_TOOL_READONLY ))) {
513 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
515 Debug( LDAP_DEBUG_ANY,
516 "bdb_db_close: txn_checkpoint failed: %s (%d)\n",
517 db_strerror(rc), rc, 0 );
521 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
522 bdb->bi_dbenv = NULL;
524 Debug( LDAP_DEBUG_ANY,
525 "bdb_db_close: close failed: %s (%d)\n",
526 db_strerror(rc), rc, 0 );
531 rc = alock_close( &bdb->bi_alock_info );
533 Debug( LDAP_DEBUG_ANY,
534 "bdb_db_close: alock_close failed\n", 0, 0, 0 );
542 bdb_db_destroy( BackendDB *be )
544 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
546 /* monitor handling */
547 (void)bdb_monitor_db_destroy( be );
549 if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
550 if( bdb->bi_db_config_path ) ch_free( bdb->bi_db_config_path );
552 bdb_attr_index_destroy( bdb );
554 ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
555 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_lru_mutex );
556 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_count_mutex );
557 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_eifree_mutex );
558 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_dntree.bei_kids_mutex );
560 ldap_pvt_thread_mutex_destroy( &bdb->bi_modrdns_mutex );
562 ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
563 ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
564 ldap_pvt_thread_rdwr_destroy( &bdb->bi_idl_tree_rwlock );
565 ldap_pvt_thread_mutex_destroy( &bdb->bi_idl_tree_lrulock );
569 e = bdb->bi_cache.c_dntree.bei_e;
570 bdb->bi_cache.c_dntree.bei_e = NULL;
572 BER_BVZERO( &e->e_name );
573 BER_BVZERO( &e->e_nname );
578 be->be_private = NULL;
589 static char *controls[] = {
591 LDAP_CONTROL_MANAGEDSAIT,
593 LDAP_CONTROL_PAGEDRESULTS,
594 LDAP_CONTROL_PRE_READ,
595 LDAP_CONTROL_POST_READ,
596 LDAP_CONTROL_SUBENTRIES,
597 LDAP_CONTROL_X_PERMISSIVE_MODIFY,
599 LDAP_CONTROL_X_TXN_SPEC,
604 /* initialize the underlying database system */
605 Debug( LDAP_DEBUG_TRACE,
606 LDAP_XSTRING(bdb_back_initialize) ": initialize "
607 BDB_UCTYPE " backend\n", 0, 0, 0 );
610 SLAP_BFLAG_INCREMENT |
611 SLAP_BFLAG_SUBENTRIES |
613 SLAP_BFLAG_REFERRALS;
615 bi->bi_controls = controls;
617 { /* version check */
618 int major, minor, patch, ver;
619 char *version = db_version( &major, &minor, &patch );
623 /* All our stdio does an ASCII to EBCDIC conversion on
624 * the output. Strings from the BDB library are already
625 * in EBCDIC; we have to go back and forth...
627 strcpy( v2, version );
632 ver = (major << 24) | (minor << 16) | patch;
633 if( ver != DB_VERSION_FULL ) {
634 /* fail if a versions don't match */
635 Debug( LDAP_DEBUG_ANY,
636 LDAP_XSTRING(bdb_back_initialize) ": "
637 "BDB library version mismatch:"
638 " expected " DB_VERSION_STRING ","
639 " got %s\n", version, 0, 0 );
643 Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(bdb_back_initialize)
644 ": %s\n", version, 0, 0 );
647 db_env_set_func_free( ber_memfree );
648 db_env_set_func_malloc( (db_malloc *)ber_memalloc );
649 db_env_set_func_realloc( (db_realloc *)ber_memrealloc );
651 /* This is a no-op on a NO_THREAD build. Leave the default
652 * alone so that BDB will sleep on interprocess conflicts.
654 db_env_set_func_yield( ldap_pvt_thread_yield );
662 bi->bi_db_init = bdb_db_init;
663 bi->bi_db_config = config_generic_wrapper;
664 bi->bi_db_open = bdb_db_open;
665 bi->bi_db_close = bdb_db_close;
666 bi->bi_db_destroy = bdb_db_destroy;
668 bi->bi_op_add = bdb_add;
669 bi->bi_op_bind = bdb_bind;
670 bi->bi_op_compare = bdb_compare;
671 bi->bi_op_delete = bdb_delete;
672 bi->bi_op_modify = bdb_modify;
673 bi->bi_op_modrdn = bdb_modrdn;
674 bi->bi_op_search = bdb_search;
676 bi->bi_op_unbind = 0;
678 bi->bi_extended = bdb_extended;
680 bi->bi_chk_referrals = bdb_referrals;
681 bi->bi_operational = bdb_operational;
682 bi->bi_has_subordinates = bdb_hasSubordinates;
683 bi->bi_entry_release_rw = bdb_entry_release;
684 bi->bi_entry_get_rw = bdb_entry_get;
687 * hooks for slap tools
689 bi->bi_tool_entry_open = bdb_tool_entry_open;
690 bi->bi_tool_entry_close = bdb_tool_entry_close;
691 bi->bi_tool_entry_first = bdb_tool_entry_next;
692 bi->bi_tool_entry_next = bdb_tool_entry_next;
693 bi->bi_tool_entry_get = bdb_tool_entry_get;
694 bi->bi_tool_entry_put = bdb_tool_entry_put;
695 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
696 bi->bi_tool_sync = 0;
697 bi->bi_tool_dn2id_get = bdb_tool_dn2id_get;
698 bi->bi_tool_id2entry_get = bdb_tool_id2entry_get;
699 bi->bi_tool_entry_modify = bdb_tool_entry_modify;
701 bi->bi_connection_init = 0;
702 bi->bi_connection_destroy = 0;
704 rc = bdb_back_init_cf( bi );
709 #if (SLAPD_BDB == SLAPD_MOD_DYNAMIC && !defined(BDB_HIER)) || \
710 (SLAPD_HDB == SLAPD_MOD_DYNAMIC && defined(BDB_HIER))
712 /* conditionally define the init_module() function */
714 SLAP_BACKEND_INIT_MODULE( hdb )
715 #else /* !BDB_HIER */
716 SLAP_BACKEND_INIT_MODULE( bdb )
717 #endif /* !BDB_HIER */
719 #endif /* SLAPD_[BH]DB == SLAPD_MOD_DYNAMIC */