1 /* init.c - initialize bdb backend */
4 * Copyright 1998-2002 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>
20 static struct bdbi_database {
25 } bdbi_databases[] = {
26 { "id2entry" BDB_SUFFIX, "id2entry", DB_BTREE, 0 },
28 { "id2parent" BDB_SUFFIX, "id2parent", DB_BTREE, 0 },
30 { "dn2id" BDB_SUFFIX, "dn2id", DB_BTREE, 0 },
35 struct berval bdb_uuid = { 0, NULL };
37 typedef void * db_malloc(size_t);
38 typedef void * db_realloc(void *, size_t);
42 bdb_open( BackendInfo *bi )
48 bdb_destroy( BackendInfo *bi )
54 bdb_close( BackendInfo *bi )
56 /* terminate the underlying database system */
62 bdb_db_init( BackendDB *be )
67 LDAP_LOG( BACK_BDB, ENTRY, "bdb_db_init", 0, 0, 0 );
69 Debug( LDAP_DEBUG_ANY,
70 "bdb_db_init: Initializing BDB database\n",
74 /* indicate system schema supported */
77 SLAP_BFLAG_SUBENTRIES |
84 /* allocate backend-database-specific stuff */
85 bdb = (struct bdb_info *) ch_calloc( 1, sizeof(struct bdb_info) );
87 /* DBEnv parameters */
88 bdb->bi_dbenv_home = ch_strdup( SLAPD_DEFAULT_DB_DIR );
89 bdb->bi_dbenv_xflags = 0;
90 bdb->bi_dbenv_mode = SLAPD_DEFAULT_DB_MODE;
92 bdb->bi_cache.c_maxsize = DEFAULT_CACHE_SIZE;
94 bdb->bi_lock_detect = DB_LOCK_DEFAULT;
95 bdb->bi_search_stack_depth = DEFAULT_SEARCH_STACK_DEPTH;
96 bdb->bi_search_stack = NULL;
98 #ifdef LDAP_CLIENT_UPDATE
99 LDAP_LIST_INIT (&bdb->psearch_list);
102 ldap_pvt_thread_mutex_init( &bdb->bi_database_mutex );
103 ldap_pvt_thread_mutex_init( &bdb->bi_lastid_mutex );
104 ldap_pvt_thread_mutex_init( &bdb->bi_cache.lru_mutex );
105 ldap_pvt_thread_rdwr_init ( &bdb->bi_cache.c_rwlock );
107 ldap_pvt_thread_rdwr_init( &bdb->bi_tree_rdwr );
110 be->be_private = bdb;
122 unsigned char x, *u, *c;
128 #ifdef WORDS_BIGENDIAN
129 for( i = 0; i < sizeof(ID); i++)
131 for( i = sizeof(ID)-1; i >= 0; i--)
142 bdb_db_open( BackendDB *be )
145 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
148 char path[MAXPATHLEN];
152 LDAP_LOG( BACK_BDB, ARGS,
153 "bdb_db_open: %s\n", be->be_suffix[0].bv_val, 0, 0 );
155 Debug( LDAP_DEBUG_ARGS,
157 be->be_suffix[0].bv_val, 0, 0 );
160 /* we should check existance of dbenv_home and db_directory */
162 rc = db_env_create( &bdb->bi_dbenv, 0 );
165 LDAP_LOG( BACK_BDB, ERR,
166 "bdb_db_open: db_env_create failed: %s (%d)\n",
167 db_strerror(rc), rc, 0 );
169 Debug( LDAP_DEBUG_ANY,
170 "bdb_db_open: db_env_create failed: %s (%d)\n",
171 db_strerror(rc), rc, 0 );
176 flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE
177 | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN;
180 /* Never do automatic recovery, must perform it manually.
181 * Otherwise restarting with gentlehup will corrupt the
184 if( !(slapMode & SLAP_TOOL_MODE) ) flags |= DB_RECOVER;
187 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
188 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
189 bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
191 #ifdef SLAP_IDL_CACHE
192 if ( bdb->bi_idl_cache_max_size ) {
193 ldap_pvt_thread_mutex_init( &bdb->bi_idl_tree_mutex );
194 bdb->bi_idl_cache_size = 0;
200 char dir[MAXPATHLEN];
201 size_t len = strlen( bdb->bi_dbenv_home );
203 strcpy( dir, bdb->bi_dbenv_home );
204 strcat( &dir[len], BDB_TMP_SUBDIR );
206 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
209 LDAP_LOG( BACK_BDB, ERR,
210 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
211 dir, db_strerror(rc), rc );
213 Debug( LDAP_DEBUG_ANY,
214 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
215 dir, db_strerror(rc), rc );
220 strcat( &dir[len], BDB_LG_SUBDIR );
222 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
225 LDAP_LOG( BACK_BDB, ERR,
226 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
227 dir, db_strerror(rc), rc );
229 Debug( LDAP_DEBUG_ANY,
230 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
231 dir, db_strerror(rc), rc );
236 strcat( &dir[len], BDB_DATA_SUBDIR );
238 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
241 LDAP_LOG( BACK_BDB, ERR,
242 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
243 dir, db_strerror(rc), rc );
245 Debug( LDAP_DEBUG_ANY,
246 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
247 dir, db_strerror(rc), rc );
255 LDAP_LOG( BACK_BDB, DETAIL1,
256 "bdb_db_open: dbenv_open %s\n", bdb->bi_dbenv_home, 0, 0 );
258 Debug( LDAP_DEBUG_TRACE,
259 "bdb_db_open: dbenv_open(%s)\n",
260 bdb->bi_dbenv_home, 0, 0);
264 strcpy( path, bdb->bi_dbenv_home );
266 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
269 bdb->bi_dbenv_mode );
271 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
274 bdb->bi_dbenv_mode );
278 LDAP_LOG( BACK_BDB, ERR,
279 "bdb_db_open: dbenv_open failed: %s (%d)\n",
280 db_strerror(rc), rc, 0 );
282 Debug( LDAP_DEBUG_ANY,
283 "bdb_db_open: dbenv_open failed: %s (%d)\n",
284 db_strerror(rc), rc, 0 );
289 if( bdb->bi_dbenv_xflags != 0 ) {
290 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
291 bdb->bi_dbenv_xflags, 1);
294 LDAP_LOG( BACK_BDB, ERR,
295 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
296 db_strerror(rc), rc, 0 );
298 Debug( LDAP_DEBUG_ANY,
299 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
300 db_strerror(rc), rc, 0 );
306 flags = DB_THREAD | DB_CREATE | bdb->bi_db_opflags;
308 bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
309 BDB_INDICES * sizeof(struct bdb_db_info *) );
311 /* open (and create) main database */
312 for( i = 0; bdbi_databases[i].name; i++ ) {
313 struct bdb_db_info *db;
315 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
317 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
320 LDAP_LOG( BACK_BDB, ERR,
321 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
322 bdb->bi_dbenv_home, db_strerror(rc), rc );
324 Debug( LDAP_DEBUG_ANY,
325 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
326 bdb->bi_dbenv_home, db_strerror(rc), rc );
331 if( i == BDB_ID2ENTRY ) {
332 rc = db->bdi_db->set_bt_compare( db->bdi_db,
334 rc = db->bdi_db->set_pagesize( db->bdi_db,
335 BDB_ID2ENTRY_PAGESIZE );
338 rc = db->bdi_db->set_bt_compare( db->bdi_db,
341 rc = db->bdi_db->set_flags( db->bdi_db,
342 DB_DUP | DB_DUPSORT );
343 rc = db->bdi_db->set_dup_compare( db->bdi_db,
346 rc = db->bdi_db->set_pagesize( db->bdi_db,
351 strcpy( path, bdbi_databases[i].file );
353 rc = DB_OPEN( db->bdi_db,
355 /* bdbi_databases[i].name, */ NULL,
356 bdbi_databases[i].type,
357 bdbi_databases[i].flags | flags,
358 bdb->bi_dbenv_mode );
360 rc = DB_OPEN( db->bdi_db,
361 bdbi_databases[i].file,
362 /* bdbi_databases[i].name, */ NULL,
363 bdbi_databases[i].type,
364 bdbi_databases[i].flags | flags,
365 bdb->bi_dbenv_mode );
370 LDAP_LOG( BACK_BDB, ERR,
371 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
372 bdb->bi_dbenv_home, db_strerror(rc), rc );
374 Debug( LDAP_DEBUG_ANY,
375 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
376 bdb->bi_dbenv_home, db_strerror(rc), rc );
381 db->bdi_name = bdbi_databases[i].name;
382 bdb->bi_databases[i] = db;
385 bdb->bi_databases[i] = NULL;
386 bdb->bi_ndatabases = i;
389 rc = bdb_last_id( be, NULL );
392 LDAP_LOG( BACK_BDB, ERR,
393 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
394 bdb->bi_dbenv_home, db_strerror(rc), rc );
396 Debug( LDAP_DEBUG_ANY,
397 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
398 bdb->bi_dbenv_home, db_strerror(rc), rc );
403 /* <insert> open (and create) index databases */
405 rc = bdb_build_tree( be );
411 bdb_db_close( BackendDB *be )
414 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
415 struct bdb_db_info *db;
416 #ifdef SLAP_IDL_CACHE
417 bdb_idl_cache_entry_t *entry, *next_entry;
420 while( bdb->bi_ndatabases-- ) {
421 db = bdb->bi_databases[bdb->bi_ndatabases];
422 rc = db->bdi_db->close( db->bdi_db, 0 );
423 /* Lower numbered names are not strdup'd */
424 if( bdb->bi_ndatabases >= BDB_NDB )
425 free( db->bdi_name );
428 free( bdb->bi_databases );
429 bdb_attr_index_destroy( bdb->bi_attrs );
431 bdb_cache_release_all (&bdb->bi_cache);
433 #ifdef SLAP_IDL_CACHE
434 ldap_pvt_thread_mutex_lock ( &bdb->bi_idl_tree_mutex );
435 entry = bdb->bi_idl_lru_head;
436 while ( entry != NULL ) {
437 next_entry = entry->idl_lru_next;
439 free( entry->kstr.bv_val );
443 ldap_pvt_thread_mutex_unlock ( &bdb->bi_idl_tree_mutex );
450 bdb_db_destroy( BackendDB *be )
453 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
455 /* close db environment */
456 if( bdb->bi_dbenv ) {
457 /* force a checkpoint */
458 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
461 LDAP_LOG( BACK_BDB, ERR,
462 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
463 db_strerror(rc), rc, 0 );
465 Debug( LDAP_DEBUG_ANY,
466 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
467 db_strerror(rc), rc, 0 );
471 bdb_cache_release_all (&bdb->bi_cache);
473 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
474 bdb->bi_dbenv = NULL;
477 LDAP_LOG( BACK_BDB, ERR,
478 "bdb_db_destroy: close failed: %s (%d)\n",
479 db_strerror(rc), rc, 0 );
481 Debug( LDAP_DEBUG_ANY,
482 "bdb_db_destroy: close failed: %s (%d)\n",
483 db_strerror(rc), rc, 0 );
489 if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
492 ldap_pvt_thread_rdwr_destroy( &bdb->bi_tree_rdwr );
494 ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
495 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_mutex );
496 ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
497 ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
500 be->be_private = NULL;
505 #ifdef SLAPD_BDB_DYNAMIC
506 int back_bdb_LTX_init_module( int argc, char *argv[] ) {
509 memset( &bi, '\0', sizeof(bi) );
511 bi.bi_init = bdb_initialize;
516 #endif /* SLAPD_BDB_DYNAMIC */
523 static char *controls[] = {
524 LDAP_CONTROL_MANAGEDSAIT,
526 LDAP_CONTROL_PAGEDRESULTS,
527 LDAP_CONTROL_VALUESRETURNFILTER,
528 #ifdef LDAP_CONTROL_SUBENTRIES
529 LDAP_CONTROL_SUBENTRIES,
531 #ifdef LDAP_CLIENT_UPDATE
532 LDAP_CONTROL_CLIENT_UPDATE,
537 bi->bi_controls = controls;
539 /* initialize the underlying database system */
541 LDAP_LOG( BACK_BDB, ENTRY, "bdb_db_initialize\n", 0, 0, 0 );
543 Debug( LDAP_DEBUG_TRACE, "bdb_initialize: initialize BDB backend\n",
547 { /* version check */
548 int major, minor, patch;
549 char *version = db_version( &major, &minor, &patch );
553 /* All our stdio does an ASCII to EBCDIC conversion on
554 * the output. Strings from the BDB library are already
555 * in EBCDIC; we have to go back and forth...
557 strcpy( v2, version );
562 if( major != DB_VERSION_MAJOR ||
563 minor != DB_VERSION_MINOR ||
564 patch < DB_VERSION_PATCH )
567 LDAP_LOG( BACK_BDB, ERR,
568 "bdb_db_initialize: version mismatch: "
569 "\texpected: %s \tgot: %s\n", DB_VERSION_STRING, version, 0 );
571 Debug( LDAP_DEBUG_ANY,
572 "bdb_initialize: version mismatch\n"
573 "\texpected: " DB_VERSION_STRING "\n"
574 "\tgot: %s \n", version, 0, 0 );
579 LDAP_LOG( BACK_BDB, DETAIL1,
580 "bdb_db_initialize: %s\n", version, 0, 0 );
582 Debug( LDAP_DEBUG_ANY, "bdb_initialize: %s\n",
587 db_env_set_func_free( ber_memfree );
588 db_env_set_func_malloc( (db_malloc *)ber_memalloc );
589 db_env_set_func_realloc( (db_realloc *)ber_memrealloc );
591 /* This is a no-op on a NO_THREAD build. Leave the default
592 * alone so that BDB will sleep on interprocess conflicts.
594 db_env_set_func_yield( ldap_pvt_thread_yield );
598 static char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
600 bdb_uuid.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ));
601 bdb_uuid.bv_val = uuidbuf;
609 bi->bi_db_init = bdb_db_init;
610 bi->bi_db_config = bdb_db_config;
611 bi->bi_db_open = bdb_db_open;
612 bi->bi_db_close = bdb_db_close;
613 bi->bi_db_destroy = bdb_db_destroy;
615 bi->bi_op_add = bdb_add;
616 bi->bi_op_bind = bdb_bind;
617 bi->bi_op_compare = bdb_compare;
618 bi->bi_op_delete = bdb_delete;
619 bi->bi_op_modify = bdb_modify;
620 bi->bi_op_modrdn = bdb_modrdn;
621 bi->bi_op_search = bdb_search;
623 bi->bi_op_unbind = 0;
624 bi->bi_op_abandon = 0;
626 bi->bi_extended = bdb_extended;
630 * these routines (and their callers) are not yet designed
631 * to work with transaction. Using them may cause deadlock.
633 bi->bi_acl_group = bdb_group;
634 bi->bi_acl_attribute = bdb_attribute;
636 bi->bi_acl_group = 0;
637 bi->bi_acl_attribute = 0;
640 bi->bi_chk_referrals = bdb_referrals;
641 bi->bi_operational = bdb_operational;
642 bi->bi_has_subordinates = bdb_hasSubordinates;
643 bi->bi_entry_release_rw = bdb_entry_release;
646 * hooks for slap tools
648 bi->bi_tool_entry_open = bdb_tool_entry_open;
649 bi->bi_tool_entry_close = bdb_tool_entry_close;
650 bi->bi_tool_entry_first = bdb_tool_entry_next;
651 bi->bi_tool_entry_next = bdb_tool_entry_next;
652 bi->bi_tool_entry_get = bdb_tool_entry_get;
653 bi->bi_tool_entry_put = bdb_tool_entry_put;
654 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
655 bi->bi_tool_sync = 0;
657 bi->bi_connection_init = 0;
658 bi->bi_connection_destroy = 0;