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->bi_psearch_list);
101 ldap_pvt_thread_mutex_init( &bdb->bi_lastid_mutex );
102 ldap_pvt_thread_mutex_init( &bdb->bi_cache.lru_mutex );
103 ldap_pvt_thread_rdwr_init ( &bdb->bi_cache.c_rwlock );
105 ldap_pvt_thread_rdwr_init( &bdb->bi_tree_rdwr );
108 be->be_private = bdb;
120 unsigned char *u, *c;
126 #ifdef WORDS_BIGENDIAN
127 for( i = 0; i < (int)sizeof(ID); i++)
129 for( i = sizeof(ID)-1; i >= 0; i--)
140 bdb_db_open( BackendDB *be )
143 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
146 char path[MAXPATHLEN];
150 LDAP_LOG( BACK_BDB, ARGS,
151 "bdb_db_open: %s\n", be->be_suffix[0].bv_val, 0, 0 );
153 Debug( LDAP_DEBUG_ARGS,
155 be->be_suffix[0].bv_val, 0, 0 );
158 #ifndef BDB_MULTIPLE_SUFFIXES
159 if ( be->be_suffix[1].bv_val ) {
161 LDAP_LOG( BACK_BDB, ERR,
162 "bdb_db_open: only one suffix allowed\n", 0, 0, 0 );
164 Debug( LDAP_DEBUG_ANY,
165 "bdb_db_open: only one suffix allowed\n", 0, 0, 0 );
170 /* we should check existance of dbenv_home and db_directory */
172 rc = db_env_create( &bdb->bi_dbenv, 0 );
175 LDAP_LOG( BACK_BDB, ERR,
176 "bdb_db_open: db_env_create failed: %s (%d)\n",
177 db_strerror(rc), rc, 0 );
179 Debug( LDAP_DEBUG_ANY,
180 "bdb_db_open: db_env_create failed: %s (%d)\n",
181 db_strerror(rc), rc, 0 );
186 flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE
187 | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN;
190 /* Never do automatic recovery, must perform it manually.
191 * Otherwise restarting with gentlehup will corrupt the
194 if( !(slapMode & SLAP_TOOL_MODE) ) flags |= DB_RECOVER;
197 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
198 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
199 bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
201 #ifdef SLAP_IDL_CACHE
202 if ( bdb->bi_idl_cache_max_size ) {
203 ldap_pvt_thread_mutex_init( &bdb->bi_idl_tree_mutex );
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 */
434 rc = bdb_build_tree( be );
440 bdb_db_close( BackendDB *be )
443 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
444 struct bdb_db_info *db;
445 #ifdef SLAP_IDL_CACHE
446 bdb_idl_cache_entry_t *entry, *next_entry;
449 while( bdb->bi_ndatabases-- ) {
450 db = bdb->bi_databases[bdb->bi_ndatabases];
451 rc = db->bdi_db->close( db->bdi_db, 0 );
452 /* Lower numbered names are not strdup'd */
453 if( bdb->bi_ndatabases >= BDB_NDB )
454 free( db->bdi_name );
457 free( bdb->bi_databases );
458 bdb_attr_index_destroy( bdb->bi_attrs );
460 bdb_cache_release_all (&bdb->bi_cache);
462 #ifdef SLAP_IDL_CACHE
463 ldap_pvt_thread_mutex_lock ( &bdb->bi_idl_tree_mutex );
464 entry = bdb->bi_idl_lru_head;
465 while ( entry != NULL ) {
466 next_entry = entry->idl_lru_next;
467 avl_delete( &bdb->bi_idl_tree, (caddr_t) entry, bdb_idl_entry_cmp );
469 free( entry->kstr.bv_val );
473 ldap_pvt_thread_mutex_unlock ( &bdb->bi_idl_tree_mutex );
480 bdb_db_destroy( BackendDB *be )
483 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
485 /* close db environment */
486 if( bdb->bi_dbenv ) {
487 /* force a checkpoint */
488 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
491 LDAP_LOG( BACK_BDB, ERR,
492 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
493 db_strerror(rc), rc, 0 );
495 Debug( LDAP_DEBUG_ANY,
496 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
497 db_strerror(rc), rc, 0 );
501 bdb_cache_release_all (&bdb->bi_cache);
503 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
504 bdb->bi_dbenv = NULL;
507 LDAP_LOG( BACK_BDB, ERR,
508 "bdb_db_destroy: close failed: %s (%d)\n",
509 db_strerror(rc), rc, 0 );
511 Debug( LDAP_DEBUG_ANY,
512 "bdb_db_destroy: close failed: %s (%d)\n",
513 db_strerror(rc), rc, 0 );
519 if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
522 ldap_pvt_thread_rdwr_destroy( &bdb->bi_tree_rdwr );
524 ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
525 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_mutex );
526 ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
529 be->be_private = NULL;
534 #ifdef SLAPD_BDB_DYNAMIC
535 int back_bdb_LTX_init_module( int argc, char *argv[] ) {
538 memset( &bi, '\0', sizeof(bi) );
540 bi.bi_init = bdb_initialize;
545 #endif /* SLAPD_BDB_DYNAMIC */
552 static char *controls[] = {
553 LDAP_CONTROL_MANAGEDSAIT,
555 #ifdef LDAP_CONTROL_PAGEDRESULTS
556 LDAP_CONTROL_PAGEDRESULTS,
558 LDAP_CONTROL_VALUESRETURNFILTER,
559 #ifdef LDAP_CONTROL_SUBENTRIES
560 LDAP_CONTROL_SUBENTRIES,
562 #ifdef LDAP_CLIENT_UPDATE
563 LDAP_CONTROL_CLIENT_UPDATE,
568 bi->bi_controls = controls;
570 /* initialize the underlying database system */
572 LDAP_LOG( BACK_BDB, ENTRY, "bdb_db_initialize\n", 0, 0, 0 );
574 Debug( LDAP_DEBUG_TRACE, "bdb_initialize: initialize BDB backend\n",
578 { /* version check */
579 int major, minor, patch;
580 char *version = db_version( &major, &minor, &patch );
584 /* All our stdio does an ASCII to EBCDIC conversion on
585 * the output. Strings from the BDB library are already
586 * in EBCDIC; we have to go back and forth...
588 strcpy( v2, version );
593 if( major != DB_VERSION_MAJOR ||
594 minor != DB_VERSION_MINOR ||
595 patch < DB_VERSION_PATCH )
598 LDAP_LOG( BACK_BDB, ERR,
599 "bdb_db_initialize: version mismatch: "
600 "\texpected: %s \tgot: %s\n", DB_VERSION_STRING, version, 0 );
602 Debug( LDAP_DEBUG_ANY,
603 "bdb_initialize: version mismatch\n"
604 "\texpected: " DB_VERSION_STRING "\n"
605 "\tgot: %s \n", version, 0, 0 );
610 LDAP_LOG( BACK_BDB, DETAIL1,
611 "bdb_db_initialize: %s\n", version, 0, 0 );
613 Debug( LDAP_DEBUG_ANY, "bdb_initialize: %s\n",
618 db_env_set_func_free( ber_memfree );
619 db_env_set_func_malloc( (db_malloc *)ber_memalloc );
620 db_env_set_func_realloc( (db_realloc *)ber_memrealloc );
622 /* This is a no-op on a NO_THREAD build. Leave the default
623 * alone so that BDB will sleep on interprocess conflicts.
625 db_env_set_func_yield( ldap_pvt_thread_yield );
629 static char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
631 bdb_uuid.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ));
632 bdb_uuid.bv_val = uuidbuf;
640 bi->bi_db_init = bdb_db_init;
641 bi->bi_db_config = bdb_db_config;
642 bi->bi_db_open = bdb_db_open;
643 bi->bi_db_close = bdb_db_close;
644 bi->bi_db_destroy = bdb_db_destroy;
646 bi->bi_op_add = bdb_add;
647 bi->bi_op_bind = bdb_bind;
648 bi->bi_op_compare = bdb_compare;
649 bi->bi_op_delete = bdb_delete;
650 bi->bi_op_modify = bdb_modify;
651 bi->bi_op_modrdn = bdb_modrdn;
652 bi->bi_op_search = bdb_search;
654 bi->bi_op_unbind = 0;
656 #ifdef LDAP_CLIENT_UPDATE
657 bi->bi_op_abandon = bdb_abandon;
658 bi->bi_op_cancel = bdb_cancel;
660 bi->bi_op_abandon = 0;
661 bi->bi_op_cancel = 0;
664 bi->bi_extended = bdb_extended;
666 bi->bi_chk_referrals = bdb_referrals;
667 bi->bi_operational = bdb_operational;
668 bi->bi_has_subordinates = bdb_hasSubordinates;
669 bi->bi_entry_release_rw = bdb_entry_release;
670 bi->bi_entry_get_rw = bdb_entry_get;
673 * hooks for slap tools
675 bi->bi_tool_entry_open = bdb_tool_entry_open;
676 bi->bi_tool_entry_close = bdb_tool_entry_close;
677 bi->bi_tool_entry_first = bdb_tool_entry_next;
678 bi->bi_tool_entry_next = bdb_tool_entry_next;
679 bi->bi_tool_entry_get = bdb_tool_entry_get;
680 bi->bi_tool_entry_put = bdb_tool_entry_put;
681 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
682 bi->bi_tool_sync = 0;
684 bi->bi_connection_init = 0;
685 bi->bi_connection_destroy = 0;