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 },
27 { "id2parent" BDB_SUFFIX, "id2parent", DB_BTREE, 0 },
29 { "dn2id" BDB_SUFFIX, "dn2id", DB_BTREE, 0 },
34 struct berval bdb_uuid = { 0, NULL };
36 typedef void * db_malloc(size_t);
37 typedef void * db_realloc(void *, size_t);
41 bdb_open( BackendInfo *bi )
47 bdb_destroy( BackendInfo *bi )
53 bdb_close( BackendInfo *bi )
55 /* terminate the underlying database system */
61 bdb_db_init( BackendDB *be )
66 LDAP_LOG( BACK_BDB, ENTRY, "bdb_db_init", 0, 0, 0 );
68 Debug( LDAP_DEBUG_ANY,
69 "bdb_db_init: Initializing BDB database\n",
73 /* indicate system schema supported */
76 SLAP_BFLAG_SUBENTRIES |
83 /* allocate backend-database-specific stuff */
84 bdb = (struct bdb_info *) ch_calloc( 1, sizeof(struct bdb_info) );
86 /* DBEnv parameters */
87 bdb->bi_dbenv_home = ch_strdup( SLAPD_DEFAULT_DB_DIR );
88 bdb->bi_dbenv_xflags = 0;
89 bdb->bi_dbenv_mode = SLAPD_DEFAULT_DB_MODE;
91 bdb->bi_cache.c_maxsize = DEFAULT_CACHE_SIZE;
93 bdb->bi_lock_detect = DB_LOCK_DEFAULT;
94 bdb->bi_search_stack_depth = DEFAULT_SEARCH_STACK_DEPTH;
95 bdb->bi_search_stack = NULL;
97 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
98 LDAP_LIST_INIT (&bdb->psearch_list);
101 ldap_pvt_thread_mutex_init( &bdb->bi_database_mutex );
102 ldap_pvt_thread_mutex_init( &bdb->bi_lastid_mutex );
103 ldap_pvt_thread_mutex_init( &bdb->bi_cache.lru_mutex );
104 ldap_pvt_thread_rdwr_init ( &bdb->bi_cache.c_rwlock );
106 ldap_pvt_thread_rdwr_init( &bdb->bi_tree_rdwr );
109 be->be_private = bdb;
121 unsigned char *u, *c;
127 #ifdef WORDS_BIGENDIAN
128 for( i = 0; i < (int)sizeof(ID); i++)
130 for( i = sizeof(ID)-1; i >= 0; i--)
141 bdb_db_open( BackendDB *be )
144 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
147 char path[MAXPATHLEN];
151 LDAP_LOG( BACK_BDB, ARGS,
152 "bdb_db_open: %s\n", be->be_suffix[0].bv_val, 0, 0 );
154 Debug( LDAP_DEBUG_ARGS,
156 be->be_suffix[0].bv_val, 0, 0 );
159 #ifndef BDB_MULTIPLE_SUFFIXES
160 if ( be->be_suffix[1].bv_val ) {
162 LDAP_LOG( BACK_BDB, ERR,
163 "bdb_db_open: only one suffix allowed\n", 0, 0, 0 );
165 Debug( LDAP_DEBUG_ANY,
166 "bdb_db_open: only one suffix allowed\n", 0, 0, 0 );
171 /* we should check existance of dbenv_home and db_directory */
173 rc = db_env_create( &bdb->bi_dbenv, 0 );
176 LDAP_LOG( BACK_BDB, ERR,
177 "bdb_db_open: db_env_create failed: %s (%d)\n",
178 db_strerror(rc), rc, 0 );
180 Debug( LDAP_DEBUG_ANY,
181 "bdb_db_open: db_env_create failed: %s (%d)\n",
182 db_strerror(rc), rc, 0 );
187 flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE
188 | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN;
191 /* Never do automatic recovery, must perform it manually.
192 * Otherwise restarting with gentlehup will corrupt the
195 if( !(slapMode & SLAP_TOOL_MODE) ) flags |= DB_RECOVER;
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 ldap_pvt_thread_mutex_init( &bdb->bi_idl_tree_mutex );
205 bdb->bi_idl_cache_size = 0;
211 char dir[MAXPATHLEN];
212 size_t len = strlen( bdb->bi_dbenv_home );
214 strcpy( dir, bdb->bi_dbenv_home );
215 strcat( &dir[len], BDB_TMP_SUBDIR );
217 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
220 LDAP_LOG( BACK_BDB, ERR,
221 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
222 dir, db_strerror(rc), rc );
224 Debug( LDAP_DEBUG_ANY,
225 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
226 dir, db_strerror(rc), rc );
231 strcat( &dir[len], BDB_LG_SUBDIR );
233 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
236 LDAP_LOG( BACK_BDB, ERR,
237 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
238 dir, db_strerror(rc), rc );
240 Debug( LDAP_DEBUG_ANY,
241 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
242 dir, db_strerror(rc), rc );
247 strcat( &dir[len], BDB_DATA_SUBDIR );
249 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
252 LDAP_LOG( BACK_BDB, ERR,
253 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
254 dir, db_strerror(rc), rc );
256 Debug( LDAP_DEBUG_ANY,
257 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
258 dir, db_strerror(rc), rc );
266 LDAP_LOG( BACK_BDB, DETAIL1,
267 "bdb_db_open: dbenv_open %s\n", bdb->bi_dbenv_home, 0, 0 );
269 Debug( LDAP_DEBUG_TRACE,
270 "bdb_db_open: dbenv_open(%s)\n",
271 bdb->bi_dbenv_home, 0, 0);
275 strcpy( path, bdb->bi_dbenv_home );
277 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
280 bdb->bi_dbenv_mode );
282 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
285 bdb->bi_dbenv_mode );
289 LDAP_LOG( BACK_BDB, ERR,
290 "bdb_db_open: dbenv_open failed: %s (%d)\n",
291 db_strerror(rc), rc, 0 );
293 Debug( LDAP_DEBUG_ANY,
294 "bdb_db_open: dbenv_open failed: %s (%d)\n",
295 db_strerror(rc), rc, 0 );
300 if( bdb->bi_dbenv_xflags != 0 ) {
301 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
302 bdb->bi_dbenv_xflags, 1);
305 LDAP_LOG( BACK_BDB, ERR,
306 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
307 db_strerror(rc), rc, 0 );
309 Debug( LDAP_DEBUG_ANY,
310 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
311 db_strerror(rc), rc, 0 );
317 flags = DB_THREAD | DB_CREATE | bdb->bi_db_opflags;
319 bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
320 BDB_INDICES * sizeof(struct bdb_db_info *) );
322 /* open (and create) main database */
323 for( i = 0; bdbi_databases[i].name; i++ ) {
324 struct bdb_db_info *db;
326 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
328 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
331 LDAP_LOG( BACK_BDB, ERR,
332 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
333 bdb->bi_dbenv_home, db_strerror(rc), rc );
335 Debug( LDAP_DEBUG_ANY,
336 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
337 bdb->bi_dbenv_home, db_strerror(rc), rc );
342 if( i == BDB_ID2ENTRY ) {
343 rc = db->bdi_db->set_bt_compare( db->bdi_db,
345 rc = db->bdi_db->set_pagesize( db->bdi_db,
346 BDB_ID2ENTRY_PAGESIZE );
349 rc = db->bdi_db->set_bt_compare( db->bdi_db,
352 rc = db->bdi_db->set_flags( db->bdi_db,
353 DB_DUP | DB_DUPSORT );
354 rc = db->bdi_db->set_dup_compare( db->bdi_db,
357 rc = db->bdi_db->set_pagesize( db->bdi_db,
362 strcpy( path, bdbi_databases[i].file );
364 rc = DB_OPEN( db->bdi_db,
366 /* bdbi_databases[i].name, */ NULL,
367 bdbi_databases[i].type,
368 bdbi_databases[i].flags | flags,
369 bdb->bi_dbenv_mode );
371 rc = DB_OPEN( db->bdi_db,
372 bdbi_databases[i].file,
373 /* bdbi_databases[i].name, */ NULL,
374 bdbi_databases[i].type,
375 bdbi_databases[i].flags | flags,
376 bdb->bi_dbenv_mode );
381 LDAP_LOG( BACK_BDB, ERR,
382 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
383 bdb->bi_dbenv_home, db_strerror(rc), rc );
385 Debug( LDAP_DEBUG_ANY,
386 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
387 bdb->bi_dbenv_home, db_strerror(rc), rc );
392 db->bdi_name = bdbi_databases[i].name;
393 bdb->bi_databases[i] = db;
396 bdb->bi_databases[i] = NULL;
397 bdb->bi_ndatabases = i;
400 rc = bdb_last_id( be, NULL );
403 LDAP_LOG( BACK_BDB, ERR,
404 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
405 bdb->bi_dbenv_home, db_strerror(rc), rc );
407 Debug( LDAP_DEBUG_ANY,
408 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
409 bdb->bi_dbenv_home, db_strerror(rc), rc );
414 /* <insert> open (and create) index databases */
416 rc = bdb_build_tree( be );
422 bdb_db_close( BackendDB *be )
425 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
426 struct bdb_db_info *db;
427 #ifdef SLAP_IDL_CACHE
428 bdb_idl_cache_entry_t *entry, *next_entry;
431 while( bdb->bi_ndatabases-- ) {
432 db = bdb->bi_databases[bdb->bi_ndatabases];
433 rc = db->bdi_db->close( db->bdi_db, 0 );
434 /* Lower numbered names are not strdup'd */
435 if( bdb->bi_ndatabases >= BDB_NDB )
436 free( db->bdi_name );
439 free( bdb->bi_databases );
440 bdb_attr_index_destroy( bdb->bi_attrs );
442 bdb_cache_release_all (&bdb->bi_cache);
444 #ifdef SLAP_IDL_CACHE
445 ldap_pvt_thread_mutex_lock ( &bdb->bi_idl_tree_mutex );
446 entry = bdb->bi_idl_lru_head;
447 while ( entry != NULL ) {
448 next_entry = entry->idl_lru_next;
449 avl_delete( &bdb->bi_idl_tree, (caddr_t) entry, bdb_idl_entry_cmp );
451 free( entry->kstr.bv_val );
455 ldap_pvt_thread_mutex_unlock ( &bdb->bi_idl_tree_mutex );
462 bdb_db_destroy( BackendDB *be )
465 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
467 /* close db environment */
468 if( bdb->bi_dbenv ) {
469 /* force a checkpoint */
470 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
473 LDAP_LOG( BACK_BDB, ERR,
474 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
475 db_strerror(rc), rc, 0 );
477 Debug( LDAP_DEBUG_ANY,
478 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
479 db_strerror(rc), rc, 0 );
483 bdb_cache_release_all (&bdb->bi_cache);
485 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
486 bdb->bi_dbenv = NULL;
489 LDAP_LOG( BACK_BDB, ERR,
490 "bdb_db_destroy: close failed: %s (%d)\n",
491 db_strerror(rc), rc, 0 );
493 Debug( LDAP_DEBUG_ANY,
494 "bdb_db_destroy: close failed: %s (%d)\n",
495 db_strerror(rc), rc, 0 );
501 if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
504 ldap_pvt_thread_rdwr_destroy( &bdb->bi_tree_rdwr );
506 ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
507 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_mutex );
508 ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
509 ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
512 be->be_private = NULL;
517 #ifdef SLAPD_BDB_DYNAMIC
518 int back_bdb_LTX_init_module( int argc, char *argv[] ) {
521 memset( &bi, '\0', sizeof(bi) );
523 bi.bi_init = bdb_initialize;
528 #endif /* SLAPD_BDB_DYNAMIC */
535 static char *controls[] = {
536 LDAP_CONTROL_MANAGEDSAIT,
538 #ifdef LDAP_CONTROL_PAGEDRESULTS
539 LDAP_CONTROL_PAGEDRESULTS,
541 LDAP_CONTROL_VALUESRETURNFILTER,
542 #ifdef LDAP_CONTROL_SUBENTRIES
543 LDAP_CONTROL_SUBENTRIES,
545 #ifdef LDAP_CLIENT_UPDATE
546 LDAP_CONTROL_CLIENT_UPDATE,
551 bi->bi_controls = controls;
553 /* initialize the underlying database system */
555 LDAP_LOG( BACK_BDB, ENTRY, "bdb_db_initialize\n", 0, 0, 0 );
557 Debug( LDAP_DEBUG_TRACE, "bdb_initialize: initialize BDB backend\n",
561 { /* version check */
562 int major, minor, patch;
563 char *version = db_version( &major, &minor, &patch );
567 /* All our stdio does an ASCII to EBCDIC conversion on
568 * the output. Strings from the BDB library are already
569 * in EBCDIC; we have to go back and forth...
571 strcpy( v2, version );
576 if( major != DB_VERSION_MAJOR ||
577 minor != DB_VERSION_MINOR ||
578 patch < DB_VERSION_PATCH )
581 LDAP_LOG( BACK_BDB, ERR,
582 "bdb_db_initialize: version mismatch: "
583 "\texpected: %s \tgot: %s\n", DB_VERSION_STRING, version, 0 );
585 Debug( LDAP_DEBUG_ANY,
586 "bdb_initialize: version mismatch\n"
587 "\texpected: " DB_VERSION_STRING "\n"
588 "\tgot: %s \n", version, 0, 0 );
593 LDAP_LOG( BACK_BDB, DETAIL1,
594 "bdb_db_initialize: %s\n", version, 0, 0 );
596 Debug( LDAP_DEBUG_ANY, "bdb_initialize: %s\n",
601 db_env_set_func_free( ber_memfree );
602 db_env_set_func_malloc( (db_malloc *)ber_memalloc );
603 db_env_set_func_realloc( (db_realloc *)ber_memrealloc );
605 /* This is a no-op on a NO_THREAD build. Leave the default
606 * alone so that BDB will sleep on interprocess conflicts.
608 db_env_set_func_yield( ldap_pvt_thread_yield );
612 static char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
614 bdb_uuid.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ));
615 bdb_uuid.bv_val = uuidbuf;
623 bi->bi_db_init = bdb_db_init;
624 bi->bi_db_config = bdb_db_config;
625 bi->bi_db_open = bdb_db_open;
626 bi->bi_db_close = bdb_db_close;
627 bi->bi_db_destroy = bdb_db_destroy;
629 bi->bi_op_add = bdb_add;
630 bi->bi_op_bind = bdb_bind;
631 bi->bi_op_compare = bdb_compare;
632 bi->bi_op_delete = bdb_delete;
633 bi->bi_op_modify = bdb_modify;
634 bi->bi_op_modrdn = bdb_modrdn;
635 bi->bi_op_search = bdb_search;
637 bi->bi_op_unbind = 0;
639 #ifdef LDAP_CLIENT_UPDATE
640 bi->bi_op_abandon = bdb_abandon;
641 bi->bi_op_cancel = bdb_cancel;
643 bi->bi_op_abandon = 0;
644 bi->bi_op_cancel = 0;
647 bi->bi_extended = bdb_extended;
651 * these routines (and their callers) are not yet designed
652 * to work with transaction. Using them may cause deadlock.
654 bi->bi_acl_group = bdb_group;
655 bi->bi_acl_attribute = bdb_attribute;
657 bi->bi_acl_group = 0;
658 bi->bi_acl_attribute = 0;
661 bi->bi_chk_referrals = bdb_referrals;
662 bi->bi_operational = bdb_operational;
663 bi->bi_has_subordinates = bdb_hasSubordinates;
664 bi->bi_entry_release_rw = bdb_entry_release;
667 * hooks for slap tools
669 bi->bi_tool_entry_open = bdb_tool_entry_open;
670 bi->bi_tool_entry_close = bdb_tool_entry_close;
671 bi->bi_tool_entry_first = bdb_tool_entry_next;
672 bi->bi_tool_entry_next = bdb_tool_entry_next;
673 bi->bi_tool_entry_get = bdb_tool_entry_get;
674 bi->bi_tool_entry_put = bdb_tool_entry_put;
675 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
676 bi->bi_tool_sync = 0;
678 bi->bi_connection_init = 0;
679 bi->bi_connection_destroy = 0;