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 const 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 */
71 SLAP_BFLAG_INCREMENT |
73 SLAP_BFLAG_SUBENTRIES |
80 /* allocate backend-database-specific stuff */
81 bdb = (struct bdb_info *) ch_calloc( 1, sizeof(struct bdb_info) );
83 /* DBEnv parameters */
84 bdb->bi_dbenv_home = ch_strdup( SLAPD_DEFAULT_DB_DIR );
85 bdb->bi_dbenv_xflags = 0;
86 bdb->bi_dbenv_mode = SLAPD_DEFAULT_DB_MODE;
88 bdb->bi_cache.c_maxsize = DEFAULT_CACHE_SIZE;
90 bdb->bi_lock_detect = DB_LOCK_DEFAULT;
91 bdb->bi_search_stack_depth = DEFAULT_SEARCH_STACK_DEPTH;
92 bdb->bi_search_stack = NULL;
95 LDAP_LIST_INIT (&bdb->bi_psearch_list);
98 ldap_pvt_thread_mutex_init( &bdb->bi_lastid_mutex );
99 ldap_pvt_thread_mutex_init( &bdb->bi_cache.lru_mutex );
100 ldap_pvt_thread_mutex_init( &bdb->bi_cache.c_dntree.bei_kids_mutex );
101 ldap_pvt_thread_rdwr_init ( &bdb->bi_cache.c_rwlock );
103 be->be_private = bdb;
115 unsigned char *u, *c;
121 #ifdef WORDS_BIGENDIAN
122 for( i = 0; i < (int)sizeof(ID); i++)
124 for( i = sizeof(ID)-1; i >= 0; i--)
135 bdb_db_open( BackendDB *be )
138 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
141 char path[MAXPATHLEN];
145 LDAP_LOG( BACK_BDB, ARGS,
146 "bdb_db_open: %s\n", be->be_suffix[0].bv_val, 0, 0 );
148 Debug( LDAP_DEBUG_ARGS,
150 be->be_suffix[0].bv_val, 0, 0 );
153 #ifndef BDB_MULTIPLE_SUFFIXES
154 if ( be->be_suffix[1].bv_val ) {
156 LDAP_LOG( BACK_BDB, ERR,
157 "bdb_db_open: only one suffix allowed\n", 0, 0, 0 );
159 Debug( LDAP_DEBUG_ANY,
160 "bdb_db_open: only one suffix allowed\n", 0, 0, 0 );
165 /* we should check existance of dbenv_home and db_directory */
167 rc = db_env_create( &bdb->bi_dbenv, 0 );
170 LDAP_LOG( BACK_BDB, ERR,
171 "bdb_db_open: db_env_create failed: %s (%d)\n",
172 db_strerror(rc), rc, 0 );
174 Debug( LDAP_DEBUG_ANY,
175 "bdb_db_open: db_env_create failed: %s (%d)\n",
176 db_strerror(rc), rc, 0 );
181 flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE
182 | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN;
185 /* Never do automatic recovery, must perform it manually.
186 * Otherwise restarting with gentlehup will corrupt the
189 if( !(slapMode & SLAP_TOOL_MODE) ) flags |= DB_RECOVER;
192 /* If a key was set, use shared memory for the BDB environment */
193 if ( bdb->bi_shm_key ) {
194 bdb->bi_dbenv->set_shm_key( bdb->bi_dbenv, bdb->bi_shm_key );
195 flags |= DB_SYSTEM_MEM;
198 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
199 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
200 bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
202 #ifdef SLAP_IDL_CACHE
203 if ( bdb->bi_idl_cache_max_size ) {
204 bdb->bi_idl_tree = NULL;
205 ldap_pvt_thread_rdwr_init( &bdb->bi_idl_tree_rwlock );
206 ldap_pvt_thread_mutex_init( &bdb->bi_idl_tree_lrulock );
207 bdb->bi_idl_cache_size = 0;
213 char dir[MAXPATHLEN], *ptr;
215 if (bdb->bi_dbenv_home[0] == '.') {
216 /* If home is a relative path, relative subdirs
217 * are just concat'd by BDB. We don't want the
218 * path to be concat'd twice, e.g.
219 * ./test-db/./test-db/tmp
223 ptr = lutil_strcopy( dir, bdb->bi_dbenv_home );
224 *ptr++ = LDAP_DIRSEP[0];
230 strcpy( ptr, BDB_TMP_SUBDIR );
234 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
237 LDAP_LOG( BACK_BDB, ERR,
238 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
239 dir, db_strerror(rc), rc );
241 Debug( LDAP_DEBUG_ANY,
242 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
243 dir, db_strerror(rc), rc );
248 strcpy( ptr, BDB_LG_SUBDIR );
252 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
255 LDAP_LOG( BACK_BDB, ERR,
256 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
257 dir, db_strerror(rc), rc );
259 Debug( LDAP_DEBUG_ANY,
260 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
261 dir, db_strerror(rc), rc );
266 strcpy( ptr, BDB_DATA_SUBDIR );
270 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
273 LDAP_LOG( BACK_BDB, ERR,
274 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
275 dir, db_strerror(rc), rc );
277 Debug( LDAP_DEBUG_ANY,
278 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
279 dir, db_strerror(rc), rc );
287 LDAP_LOG( BACK_BDB, DETAIL1,
288 "bdb_db_open: dbenv_open %s\n", bdb->bi_dbenv_home, 0, 0 );
290 Debug( LDAP_DEBUG_TRACE,
291 "bdb_db_open: dbenv_open(%s)\n",
292 bdb->bi_dbenv_home, 0, 0);
296 strcpy( path, bdb->bi_dbenv_home );
298 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
301 bdb->bi_dbenv_mode );
303 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
306 bdb->bi_dbenv_mode );
310 LDAP_LOG( BACK_BDB, ERR,
311 "bdb_db_open: dbenv_open failed: %s (%d)\n",
312 db_strerror(rc), rc, 0 );
314 Debug( LDAP_DEBUG_ANY,
315 "bdb_db_open: dbenv_open failed: %s (%d)\n",
316 db_strerror(rc), rc, 0 );
321 if( bdb->bi_dbenv_xflags != 0 ) {
322 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
323 bdb->bi_dbenv_xflags, 1);
326 LDAP_LOG( BACK_BDB, ERR,
327 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
328 db_strerror(rc), rc, 0 );
330 Debug( LDAP_DEBUG_ANY,
331 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
332 db_strerror(rc), rc, 0 );
338 flags = DB_THREAD | bdb->bi_db_opflags;
340 bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
341 BDB_INDICES * sizeof(struct bdb_db_info *) );
343 /* open (and create) main database */
344 for( i = 0; bdbi_databases[i].name; i++ ) {
345 struct bdb_db_info *db;
347 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
349 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
352 LDAP_LOG( BACK_BDB, ERR,
353 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
354 bdb->bi_dbenv_home, db_strerror(rc), rc );
356 Debug( LDAP_DEBUG_ANY,
357 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
358 bdb->bi_dbenv_home, db_strerror(rc), rc );
363 if( i == BDB_ID2ENTRY ) {
364 rc = db->bdi_db->set_bt_compare( db->bdi_db,
366 rc = db->bdi_db->set_pagesize( db->bdi_db,
367 BDB_ID2ENTRY_PAGESIZE );
368 if ( slapMode & SLAP_TOOL_READMAIN ) {
374 rc = db->bdi_db->set_flags( db->bdi_db,
375 DB_DUP | DB_DUPSORT );
377 rc = db->bdi_db->set_dup_compare( db->bdi_db,
379 if ( slapMode & SLAP_TOOL_READONLY ) {
385 rc = db->bdi_db->set_dup_compare( db->bdi_db,
387 rc = db->bdi_db->set_bt_compare( db->bdi_db,
389 if ( slapMode & (SLAP_TOOL_READONLY|SLAP_TOOL_READMAIN) ) {
395 rc = db->bdi_db->set_pagesize( db->bdi_db,
400 strcpy( path, bdbi_databases[i].file );
402 rc = DB_OPEN( db->bdi_db, NULL,
404 /* bdbi_databases[i].name, */ NULL,
405 bdbi_databases[i].type,
406 bdbi_databases[i].flags | flags | DB_AUTO_COMMIT,
407 bdb->bi_dbenv_mode );
409 rc = DB_OPEN( db->bdi_db, NULL,
410 bdbi_databases[i].file,
411 /* bdbi_databases[i].name, */ NULL,
412 bdbi_databases[i].type,
413 bdbi_databases[i].flags | flags | DB_AUTO_COMMIT,
414 bdb->bi_dbenv_mode );
419 LDAP_LOG( BACK_BDB, ERR,
420 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
421 bdb->bi_dbenv_home, db_strerror(rc), rc );
423 Debug( LDAP_DEBUG_ANY,
424 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
425 bdb->bi_dbenv_home, db_strerror(rc), rc );
430 flags &= ~(DB_CREATE | DB_RDONLY);
431 db->bdi_name = bdbi_databases[i].name;
432 bdb->bi_databases[i] = db;
435 bdb->bi_databases[i] = NULL;
436 bdb->bi_ndatabases = i;
439 rc = bdb_last_id( be, NULL );
442 LDAP_LOG( BACK_BDB, ERR,
443 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
444 bdb->bi_dbenv_home, db_strerror(rc), rc );
446 Debug( LDAP_DEBUG_ANY,
447 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
448 bdb->bi_dbenv_home, db_strerror(rc), rc );
453 /* <insert> open (and create) index databases */
458 bdb_db_close( BackendDB *be )
461 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
462 struct bdb_db_info *db;
463 #ifdef SLAP_IDL_CACHE
464 bdb_idl_cache_entry_t *entry, *next_entry;
467 while( bdb->bi_ndatabases-- ) {
468 db = bdb->bi_databases[bdb->bi_ndatabases];
469 rc = db->bdi_db->close( db->bdi_db, 0 );
470 /* Lower numbered names are not strdup'd */
471 if( bdb->bi_ndatabases >= BDB_NDB )
472 free( db->bdi_name );
475 free( bdb->bi_databases );
476 bdb_attr_index_destroy( bdb->bi_attrs );
478 bdb_cache_release_all (&bdb->bi_cache);
480 #ifdef SLAP_IDL_CACHE
481 if ( bdb->bi_idl_cache_max_size ) {
482 ldap_pvt_thread_rdwr_wlock ( &bdb->bi_idl_tree_rwlock );
483 avl_free( bdb->bi_idl_tree, NULL );
484 entry = bdb->bi_idl_lru_head;
485 while ( entry != NULL ) {
486 next_entry = entry->idl_lru_next;
489 free( entry->kstr.bv_val );
493 ldap_pvt_thread_rdwr_wunlock ( &bdb->bi_idl_tree_rwlock );
501 bdb_db_destroy( BackendDB *be )
504 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
506 /* close db environment */
507 if( bdb->bi_dbenv ) {
508 /* force a checkpoint */
509 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
512 LDAP_LOG( BACK_BDB, ERR,
513 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
514 db_strerror(rc), rc, 0 );
516 Debug( LDAP_DEBUG_ANY,
517 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
518 db_strerror(rc), rc, 0 );
522 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
523 bdb->bi_dbenv = NULL;
526 LDAP_LOG( BACK_BDB, ERR,
527 "bdb_db_destroy: close failed: %s (%d)\n",
528 db_strerror(rc), rc, 0 );
530 Debug( LDAP_DEBUG_ANY,
531 "bdb_db_destroy: close failed: %s (%d)\n",
532 db_strerror(rc), rc, 0 );
538 if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
540 ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
541 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_mutex );
542 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_dntree.bei_kids_mutex );
543 ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
544 #ifdef SLAP_IDL_CACHE
545 if ( bdb->bi_idl_cache_max_size ) {
546 ldap_pvt_thread_rdwr_destroy( &bdb->bi_idl_tree_rwlock );
547 ldap_pvt_thread_mutex_destroy( &bdb->bi_idl_tree_lrulock );
552 be->be_private = NULL;
557 #ifdef SLAPD_BDB_DYNAMIC
558 int init_module( int argc, char *argv[] ) {
561 memset( &bi, '\0', sizeof(bi) );
563 bi.bi_init = bdb_initialize;
568 #endif /* SLAPD_BDB_DYNAMIC */
575 static char *controls[] = {
577 LDAP_CONTROL_MANAGEDSAIT,
579 #ifdef LDAP_CONTROL_PAGEDRESULTS
580 LDAP_CONTROL_PAGEDRESULTS,
582 #ifdef LDAP_CONTROL_SUBENTRIES
583 LDAP_CONTROL_SUBENTRIES,
585 LDAP_CONTROL_VALUESRETURNFILTER,
589 bi->bi_controls = controls;
591 /* initialize the underlying database system */
593 LDAP_LOG( BACK_BDB, ENTRY, "bdb_db_initialize\n", 0, 0, 0 );
595 Debug( LDAP_DEBUG_TRACE, "bdb_initialize: initialize BDB backend\n",
599 { /* version check */
600 int major, minor, patch;
601 char *version = db_version( &major, &minor, &patch );
605 /* All our stdio does an ASCII to EBCDIC conversion on
606 * the output. Strings from the BDB library are already
607 * in EBCDIC; we have to go back and forth...
609 strcpy( v2, version );
614 if( major != DB_VERSION_MAJOR ||
615 minor != DB_VERSION_MINOR ||
616 patch < DB_VERSION_PATCH )
619 LDAP_LOG( BACK_BDB, ERR,
620 "bdb_db_initialize: version mismatch: "
621 "\texpected: %s \tgot: %s\n", DB_VERSION_STRING, version, 0 );
623 Debug( LDAP_DEBUG_ANY,
624 "bdb_initialize: version mismatch\n"
625 "\texpected: " DB_VERSION_STRING "\n"
626 "\tgot: %s \n", version, 0, 0 );
631 LDAP_LOG( BACK_BDB, DETAIL1,
632 "bdb_db_initialize: %s\n", version, 0, 0 );
634 Debug( LDAP_DEBUG_ANY, "bdb_initialize: %s\n",
639 db_env_set_func_free( ber_memfree );
640 db_env_set_func_malloc( (db_malloc *)ber_memalloc );
641 db_env_set_func_realloc( (db_realloc *)ber_memrealloc );
643 /* This is a no-op on a NO_THREAD build. Leave the default
644 * alone so that BDB will sleep on interprocess conflicts.
646 db_env_set_func_yield( ldap_pvt_thread_yield );
650 static char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
652 bdb_uuid.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ));
653 bdb_uuid.bv_val = uuidbuf;
661 bi->bi_db_init = bdb_db_init;
662 bi->bi_db_config = bdb_db_config;
663 bi->bi_db_open = bdb_db_open;
664 bi->bi_db_close = bdb_db_close;
665 bi->bi_db_destroy = bdb_db_destroy;
667 bi->bi_op_add = bdb_add;
668 bi->bi_op_bind = bdb_bind;
669 bi->bi_op_compare = bdb_compare;
670 bi->bi_op_delete = bdb_delete;
671 bi->bi_op_modify = bdb_modify;
672 bi->bi_op_modrdn = bdb_modrdn;
673 bi->bi_op_search = bdb_search;
675 bi->bi_op_unbind = 0;
678 bi->bi_op_abandon = bdb_abandon;
679 bi->bi_op_cancel = bdb_cancel;
681 bi->bi_op_abandon = 0;
682 bi->bi_op_cancel = 0;
685 bi->bi_extended = bdb_extended;
687 bi->bi_chk_referrals = bdb_referrals;
688 bi->bi_operational = bdb_operational;
689 bi->bi_has_subordinates = bdb_hasSubordinates;
690 bi->bi_entry_release_rw = bdb_entry_release;
691 bi->bi_entry_get_rw = bdb_entry_get;
694 * hooks for slap tools
696 bi->bi_tool_entry_open = bdb_tool_entry_open;
697 bi->bi_tool_entry_close = bdb_tool_entry_close;
698 bi->bi_tool_entry_first = bdb_tool_entry_next;
699 bi->bi_tool_entry_next = bdb_tool_entry_next;
700 bi->bi_tool_entry_get = bdb_tool_entry_get;
701 bi->bi_tool_entry_put = bdb_tool_entry_put;
702 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
703 bi->bi_tool_sync = 0;
705 bi->bi_connection_init = 0;
706 bi->bi_connection_destroy = 0;