1 /* init.c - initialize bdb backend */
4 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/string.h>
12 #include <ac/unistd.h>
13 #include <ac/stdlib.h>
19 static struct bdbi_database {
24 } bdbi_databases[] = {
25 { "id2entry" BDB_SUFFIX, "id2entry", DB_BTREE, 0 },
26 { "dn2id" BDB_SUFFIX, "dn2id", DB_BTREE, 0 },
30 struct berval bdb_uuid = { 0, NULL };
32 typedef void * db_malloc(size_t);
33 typedef void * db_realloc(void *, size_t);
37 bdb_open( BackendInfo *bi )
43 bdb_destroy( BackendInfo *bi )
49 bdb_close( BackendInfo *bi )
51 /* terminate the underlying database system */
57 bdb_db_init( BackendDB *be )
62 LDAP_LOG( BACK_BDB, ENTRY, "bdb_db_init", 0, 0, 0 );
64 Debug( LDAP_DEBUG_ANY,
65 "bdb_db_init: Initializing BDB database\n",
69 /* indicate system schema supported */
72 SLAP_BFLAG_SUBENTRIES |
79 /* allocate backend-database-specific stuff */
80 bdb = (struct bdb_info *) ch_calloc( 1, sizeof(struct bdb_info) );
82 /* DBEnv parameters */
83 bdb->bi_dbenv_home = ch_strdup( SLAPD_DEFAULT_DB_DIR );
84 bdb->bi_dbenv_xflags = 0;
85 bdb->bi_dbenv_mode = SLAPD_DEFAULT_DB_MODE;
87 bdb->bi_cache.c_maxsize = DEFAULT_CACHE_SIZE;
89 bdb->bi_lock_detect = DB_LOCK_DEFAULT;
90 bdb->bi_search_stack_depth = DEFAULT_SEARCH_STACK_DEPTH;
91 bdb->bi_search_stack = NULL;
93 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
94 LDAP_LIST_INIT (&bdb->bi_psearch_list);
97 ldap_pvt_thread_mutex_init( &bdb->bi_lastid_mutex );
98 ldap_pvt_thread_mutex_init( &bdb->bi_cache.lru_mutex );
99 ldap_pvt_thread_rdwr_init ( &bdb->bi_cache.c_rwlock );
101 be->be_private = bdb;
113 unsigned char *u, *c;
119 #ifdef WORDS_BIGENDIAN
120 for( i = 0; i < (int)sizeof(ID); i++)
122 for( i = sizeof(ID)-1; i >= 0; i--)
133 bdb_db_open( BackendDB *be )
136 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
139 char path[MAXPATHLEN];
143 LDAP_LOG( BACK_BDB, ARGS,
144 "bdb_db_open: %s\n", be->be_suffix[0].bv_val, 0, 0 );
146 Debug( LDAP_DEBUG_ARGS,
148 be->be_suffix[0].bv_val, 0, 0 );
151 #ifndef BDB_MULTIPLE_SUFFIXES
152 if ( be->be_suffix[1].bv_val ) {
154 LDAP_LOG( BACK_BDB, ERR,
155 "bdb_db_open: only one suffix allowed\n", 0, 0, 0 );
157 Debug( LDAP_DEBUG_ANY,
158 "bdb_db_open: only one suffix allowed\n", 0, 0, 0 );
163 /* we should check existance of dbenv_home and db_directory */
165 rc = db_env_create( &bdb->bi_dbenv, 0 );
168 LDAP_LOG( BACK_BDB, ERR,
169 "bdb_db_open: db_env_create failed: %s (%d)\n",
170 db_strerror(rc), rc, 0 );
172 Debug( LDAP_DEBUG_ANY,
173 "bdb_db_open: db_env_create failed: %s (%d)\n",
174 db_strerror(rc), rc, 0 );
179 flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE
180 | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN;
183 /* Never do automatic recovery, must perform it manually.
184 * Otherwise restarting with gentlehup will corrupt the
187 if( !(slapMode & SLAP_TOOL_MODE) ) flags |= DB_RECOVER;
190 /* If a key was set, use shared memory for the BDB environment */
191 if ( bdb->bi_shm_key ) {
192 bdb->bi_dbenv->set_shm_key( bdb->bi_dbenv, bdb->bi_shm_key );
193 flags |= DB_SYSTEM_MEM;
196 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
197 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
198 bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
200 #ifdef SLAP_IDL_CACHE
201 if ( bdb->bi_idl_cache_max_size ) {
202 ldap_pvt_thread_rdwr_init( &bdb->bi_idl_tree_rwlock );
203 ldap_pvt_thread_mutex_init( &bdb->bi_idl_tree_lrulock );
204 bdb->bi_idl_cache_size = 0;
210 char dir[MAXPATHLEN], *ptr;
212 if (bdb->bi_dbenv_home[0] == '.') {
213 /* If home is a relative path, relative subdirs
214 * are just concat'd by BDB. We don't want the
215 * path to be concat'd twice, e.g.
216 * ./test-db/./test-db/tmp
220 ptr = lutil_strcopy( dir, bdb->bi_dbenv_home );
221 *ptr++ = LDAP_DIRSEP[0];
227 strcpy( ptr, BDB_TMP_SUBDIR );
231 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
234 LDAP_LOG( BACK_BDB, ERR,
235 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
236 dir, db_strerror(rc), rc );
238 Debug( LDAP_DEBUG_ANY,
239 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
240 dir, db_strerror(rc), rc );
245 strcpy( ptr, BDB_LG_SUBDIR );
249 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
252 LDAP_LOG( BACK_BDB, ERR,
253 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
254 dir, db_strerror(rc), rc );
256 Debug( LDAP_DEBUG_ANY,
257 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
258 dir, db_strerror(rc), rc );
263 strcpy( ptr, BDB_DATA_SUBDIR );
267 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
270 LDAP_LOG( BACK_BDB, ERR,
271 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
272 dir, db_strerror(rc), rc );
274 Debug( LDAP_DEBUG_ANY,
275 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
276 dir, db_strerror(rc), rc );
284 LDAP_LOG( BACK_BDB, DETAIL1,
285 "bdb_db_open: dbenv_open %s\n", bdb->bi_dbenv_home, 0, 0 );
287 Debug( LDAP_DEBUG_TRACE,
288 "bdb_db_open: dbenv_open(%s)\n",
289 bdb->bi_dbenv_home, 0, 0);
293 strcpy( path, bdb->bi_dbenv_home );
295 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
298 bdb->bi_dbenv_mode );
300 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
303 bdb->bi_dbenv_mode );
307 LDAP_LOG( BACK_BDB, ERR,
308 "bdb_db_open: dbenv_open failed: %s (%d)\n",
309 db_strerror(rc), rc, 0 );
311 Debug( LDAP_DEBUG_ANY,
312 "bdb_db_open: dbenv_open failed: %s (%d)\n",
313 db_strerror(rc), rc, 0 );
318 if( bdb->bi_dbenv_xflags != 0 ) {
319 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
320 bdb->bi_dbenv_xflags, 1);
323 LDAP_LOG( BACK_BDB, ERR,
324 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
325 db_strerror(rc), rc, 0 );
327 Debug( LDAP_DEBUG_ANY,
328 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
329 db_strerror(rc), rc, 0 );
335 flags = DB_THREAD | DB_CREATE | bdb->bi_db_opflags;
337 bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
338 BDB_INDICES * sizeof(struct bdb_db_info *) );
340 /* open (and create) main database */
341 for( i = 0; bdbi_databases[i].name; i++ ) {
342 struct bdb_db_info *db;
344 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
346 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
349 LDAP_LOG( BACK_BDB, ERR,
350 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
351 bdb->bi_dbenv_home, db_strerror(rc), rc );
353 Debug( LDAP_DEBUG_ANY,
354 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
355 bdb->bi_dbenv_home, db_strerror(rc), rc );
360 if( i == BDB_ID2ENTRY ) {
361 rc = db->bdi_db->set_bt_compare( db->bdi_db,
363 rc = db->bdi_db->set_pagesize( db->bdi_db,
364 BDB_ID2ENTRY_PAGESIZE );
367 rc = db->bdi_db->set_bt_compare( db->bdi_db,
370 rc = db->bdi_db->set_flags( db->bdi_db,
371 DB_DUP | DB_DUPSORT );
372 rc = db->bdi_db->set_dup_compare( db->bdi_db,
375 rc = db->bdi_db->set_pagesize( db->bdi_db,
380 strcpy( path, bdbi_databases[i].file );
382 rc = DB_OPEN( db->bdi_db, NULL,
384 /* bdbi_databases[i].name, */ NULL,
385 bdbi_databases[i].type,
386 bdbi_databases[i].flags | flags | DB_AUTO_COMMIT,
387 bdb->bi_dbenv_mode );
389 rc = DB_OPEN( db->bdi_db, NULL,
390 bdbi_databases[i].file,
391 /* bdbi_databases[i].name, */ NULL,
392 bdbi_databases[i].type,
393 bdbi_databases[i].flags | flags | DB_AUTO_COMMIT,
394 bdb->bi_dbenv_mode );
399 LDAP_LOG( BACK_BDB, ERR,
400 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
401 bdb->bi_dbenv_home, db_strerror(rc), rc );
403 Debug( LDAP_DEBUG_ANY,
404 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
405 bdb->bi_dbenv_home, db_strerror(rc), rc );
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 );
421 LDAP_LOG( BACK_BDB, ERR,
422 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
423 bdb->bi_dbenv_home, db_strerror(rc), rc );
425 Debug( LDAP_DEBUG_ANY,
426 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
427 bdb->bi_dbenv_home, db_strerror(rc), rc );
432 /* <insert> open (and create) index databases */
437 bdb_db_close( BackendDB *be )
440 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
441 struct bdb_db_info *db;
442 #ifdef SLAP_IDL_CACHE
443 bdb_idl_cache_entry_t *entry, *next_entry;
446 while( bdb->bi_ndatabases-- ) {
447 db = bdb->bi_databases[bdb->bi_ndatabases];
448 rc = db->bdi_db->close( db->bdi_db, 0 );
449 /* Lower numbered names are not strdup'd */
450 if( bdb->bi_ndatabases >= BDB_NDB )
451 free( db->bdi_name );
454 free( bdb->bi_databases );
455 bdb_attr_index_destroy( bdb->bi_attrs );
457 bdb_cache_release_all (&bdb->bi_cache);
459 #ifdef SLAP_IDL_CACHE
460 if ( bdb->bi_idl_cache_max_size ) {
461 ldap_pvt_thread_rdwr_wlock ( &bdb->bi_idl_tree_rwlock );
462 entry = bdb->bi_idl_lru_head;
463 while ( entry != NULL ) {
464 next_entry = entry->idl_lru_next;
465 avl_delete( &bdb->bi_idl_tree, (caddr_t) entry,
469 free( entry->kstr.bv_val );
473 ldap_pvt_thread_rdwr_wunlock ( &bdb->bi_idl_tree_rwlock );
481 bdb_db_destroy( BackendDB *be )
484 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
486 /* close db environment */
487 if( bdb->bi_dbenv ) {
488 /* force a checkpoint */
489 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
492 LDAP_LOG( BACK_BDB, ERR,
493 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
494 db_strerror(rc), rc, 0 );
496 Debug( LDAP_DEBUG_ANY,
497 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
498 db_strerror(rc), rc, 0 );
502 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
503 bdb->bi_dbenv = NULL;
506 LDAP_LOG( BACK_BDB, ERR,
507 "bdb_db_destroy: close failed: %s (%d)\n",
508 db_strerror(rc), rc, 0 );
510 Debug( LDAP_DEBUG_ANY,
511 "bdb_db_destroy: close failed: %s (%d)\n",
512 db_strerror(rc), rc, 0 );
518 if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
520 ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
521 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_mutex );
522 ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
523 #ifdef SLAP_IDL_CACHE
524 if ( bdb->bi_idl_cache_max_size ) {
525 ldap_pvt_thread_rdwr_destroy( &bdb->bi_idl_tree_rwlock );
526 ldap_pvt_thread_mutex_destroy( &bdb->bi_idl_tree_lrulock );
531 be->be_private = NULL;
536 #ifdef SLAPD_BDB_DYNAMIC
537 int back_bdb_LTX_init_module( int argc, char *argv[] ) {
540 memset( &bi, '\0', sizeof(bi) );
542 bi.bi_init = bdb_initialize;
547 #endif /* SLAPD_BDB_DYNAMIC */
554 static char *controls[] = {
555 LDAP_CONTROL_MANAGEDSAIT,
557 #ifdef LDAP_CONTROL_PAGEDRESULTS
558 LDAP_CONTROL_PAGEDRESULTS,
560 LDAP_CONTROL_VALUESRETURNFILTER,
561 #ifdef LDAP_CONTROL_SUBENTRIES
562 LDAP_CONTROL_SUBENTRIES,
564 #ifdef LDAP_CLIENT_UPDATE
565 LDAP_CONTROL_CLIENT_UPDATE,
570 bi->bi_controls = controls;
572 /* initialize the underlying database system */
574 LDAP_LOG( BACK_BDB, ENTRY, "bdb_db_initialize\n", 0, 0, 0 );
576 Debug( LDAP_DEBUG_TRACE, "bdb_initialize: initialize BDB backend\n",
580 { /* version check */
581 int major, minor, patch;
582 char *version = db_version( &major, &minor, &patch );
586 /* All our stdio does an ASCII to EBCDIC conversion on
587 * the output. Strings from the BDB library are already
588 * in EBCDIC; we have to go back and forth...
590 strcpy( v2, version );
595 if( major != DB_VERSION_MAJOR ||
596 minor != DB_VERSION_MINOR ||
597 patch < DB_VERSION_PATCH )
600 LDAP_LOG( BACK_BDB, ERR,
601 "bdb_db_initialize: version mismatch: "
602 "\texpected: %s \tgot: %s\n", DB_VERSION_STRING, version, 0 );
604 Debug( LDAP_DEBUG_ANY,
605 "bdb_initialize: version mismatch\n"
606 "\texpected: " DB_VERSION_STRING "\n"
607 "\tgot: %s \n", version, 0, 0 );
612 LDAP_LOG( BACK_BDB, DETAIL1,
613 "bdb_db_initialize: %s\n", version, 0, 0 );
615 Debug( LDAP_DEBUG_ANY, "bdb_initialize: %s\n",
620 db_env_set_func_free( ber_memfree );
621 db_env_set_func_malloc( (db_malloc *)ber_memalloc );
622 db_env_set_func_realloc( (db_realloc *)ber_memrealloc );
624 /* This is a no-op on a NO_THREAD build. Leave the default
625 * alone so that BDB will sleep on interprocess conflicts.
627 db_env_set_func_yield( ldap_pvt_thread_yield );
631 static char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
633 bdb_uuid.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ));
634 bdb_uuid.bv_val = uuidbuf;
642 bi->bi_db_init = bdb_db_init;
643 bi->bi_db_config = bdb_db_config;
644 bi->bi_db_open = bdb_db_open;
645 bi->bi_db_close = bdb_db_close;
646 bi->bi_db_destroy = bdb_db_destroy;
648 bi->bi_op_add = bdb_add;
649 bi->bi_op_bind = bdb_bind;
650 bi->bi_op_compare = bdb_compare;
651 bi->bi_op_delete = bdb_delete;
652 bi->bi_op_modify = bdb_modify;
653 bi->bi_op_modrdn = bdb_modrdn;
654 bi->bi_op_search = bdb_search;
656 bi->bi_op_unbind = 0;
658 #ifdef LDAP_CLIENT_UPDATE
659 bi->bi_op_abandon = bdb_abandon;
660 bi->bi_op_cancel = bdb_cancel;
662 bi->bi_op_abandon = 0;
663 bi->bi_op_cancel = 0;
666 bi->bi_extended = bdb_extended;
668 bi->bi_chk_referrals = bdb_referrals;
669 bi->bi_operational = bdb_operational;
670 bi->bi_has_subordinates = bdb_hasSubordinates;
671 bi->bi_entry_release_rw = bdb_entry_release;
672 bi->bi_entry_get_rw = bdb_entry_get;
675 * hooks for slap tools
677 bi->bi_tool_entry_open = bdb_tool_entry_open;
678 bi->bi_tool_entry_close = bdb_tool_entry_close;
679 bi->bi_tool_entry_first = bdb_tool_entry_next;
680 bi->bi_tool_entry_next = bdb_tool_entry_next;
681 bi->bi_tool_entry_get = bdb_tool_entry_get;
682 bi->bi_tool_entry_put = bdb_tool_entry_put;
683 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
684 bi->bi_tool_sync = 0;
686 bi->bi_connection_init = 0;
687 bi->bi_connection_destroy = 0;