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_rdwr_init( &bdb->bi_idl_tree_rwlock );
204 ldap_pvt_thread_mutex_init( &bdb->bi_idl_tree_lrulock );
205 bdb->bi_idl_cache_size = 0;
211 char dir[MAXPATHLEN], *ptr;
213 if (bdb->bi_dbenv_home[0] == '.') {
214 /* If home is a relative path, relative subdirs
215 * are just concat'd by BDB. We don't want the
216 * path to be concat'd twice, e.g.
217 * ./test-db/./test-db/tmp
221 ptr = lutil_strcopy( dir, bdb->bi_dbenv_home );
222 *ptr++ = LDAP_DIRSEP[0];
228 strcpy( ptr, BDB_TMP_SUBDIR );
232 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
235 LDAP_LOG( BACK_BDB, ERR,
236 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
237 dir, db_strerror(rc), rc );
239 Debug( LDAP_DEBUG_ANY,
240 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
241 dir, db_strerror(rc), rc );
246 strcpy( ptr, BDB_LG_SUBDIR );
250 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
253 LDAP_LOG( BACK_BDB, ERR,
254 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
255 dir, db_strerror(rc), rc );
257 Debug( LDAP_DEBUG_ANY,
258 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
259 dir, db_strerror(rc), rc );
264 strcpy( ptr, BDB_DATA_SUBDIR );
268 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
271 LDAP_LOG( BACK_BDB, ERR,
272 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
273 dir, db_strerror(rc), rc );
275 Debug( LDAP_DEBUG_ANY,
276 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
277 dir, db_strerror(rc), rc );
285 LDAP_LOG( BACK_BDB, DETAIL1,
286 "bdb_db_open: dbenv_open %s\n", bdb->bi_dbenv_home, 0, 0 );
288 Debug( LDAP_DEBUG_TRACE,
289 "bdb_db_open: dbenv_open(%s)\n",
290 bdb->bi_dbenv_home, 0, 0);
294 strcpy( path, bdb->bi_dbenv_home );
296 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
299 bdb->bi_dbenv_mode );
301 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
304 bdb->bi_dbenv_mode );
308 LDAP_LOG( BACK_BDB, ERR,
309 "bdb_db_open: dbenv_open failed: %s (%d)\n",
310 db_strerror(rc), rc, 0 );
312 Debug( LDAP_DEBUG_ANY,
313 "bdb_db_open: dbenv_open failed: %s (%d)\n",
314 db_strerror(rc), rc, 0 );
319 if( bdb->bi_dbenv_xflags != 0 ) {
320 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
321 bdb->bi_dbenv_xflags, 1);
324 LDAP_LOG( BACK_BDB, ERR,
325 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
326 db_strerror(rc), rc, 0 );
328 Debug( LDAP_DEBUG_ANY,
329 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
330 db_strerror(rc), rc, 0 );
336 flags = DB_THREAD | DB_CREATE | bdb->bi_db_opflags;
338 bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
339 BDB_INDICES * sizeof(struct bdb_db_info *) );
341 /* open (and create) main database */
342 for( i = 0; bdbi_databases[i].name; i++ ) {
343 struct bdb_db_info *db;
345 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
347 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
350 LDAP_LOG( BACK_BDB, ERR,
351 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
352 bdb->bi_dbenv_home, db_strerror(rc), rc );
354 Debug( LDAP_DEBUG_ANY,
355 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
356 bdb->bi_dbenv_home, db_strerror(rc), rc );
361 if( i == BDB_ID2ENTRY ) {
362 rc = db->bdi_db->set_bt_compare( db->bdi_db,
364 rc = db->bdi_db->set_pagesize( db->bdi_db,
365 BDB_ID2ENTRY_PAGESIZE );
368 rc = db->bdi_db->set_bt_compare( db->bdi_db,
371 rc = db->bdi_db->set_flags( db->bdi_db,
372 DB_DUP | DB_DUPSORT );
373 rc = db->bdi_db->set_dup_compare( db->bdi_db,
376 rc = db->bdi_db->set_pagesize( db->bdi_db,
381 strcpy( path, bdbi_databases[i].file );
383 rc = DB_OPEN( db->bdi_db, NULL,
385 /* bdbi_databases[i].name, */ NULL,
386 bdbi_databases[i].type,
387 bdbi_databases[i].flags | flags | DB_AUTO_COMMIT,
388 bdb->bi_dbenv_mode );
390 rc = DB_OPEN( db->bdi_db, NULL,
391 bdbi_databases[i].file,
392 /* bdbi_databases[i].name, */ NULL,
393 bdbi_databases[i].type,
394 bdbi_databases[i].flags | flags | DB_AUTO_COMMIT,
395 bdb->bi_dbenv_mode );
400 LDAP_LOG( BACK_BDB, ERR,
401 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
402 bdb->bi_dbenv_home, db_strerror(rc), rc );
404 Debug( LDAP_DEBUG_ANY,
405 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
406 bdb->bi_dbenv_home, db_strerror(rc), rc );
411 db->bdi_name = bdbi_databases[i].name;
412 bdb->bi_databases[i] = db;
415 bdb->bi_databases[i] = NULL;
416 bdb->bi_ndatabases = i;
419 rc = bdb_last_id( be, NULL );
422 LDAP_LOG( BACK_BDB, ERR,
423 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
424 bdb->bi_dbenv_home, db_strerror(rc), rc );
426 Debug( LDAP_DEBUG_ANY,
427 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
428 bdb->bi_dbenv_home, db_strerror(rc), rc );
433 /* <insert> open (and create) index databases */
435 rc = bdb_build_tree( be );
441 bdb_db_close( BackendDB *be )
444 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
445 struct bdb_db_info *db;
446 #ifdef SLAP_IDL_CACHE
447 bdb_idl_cache_entry_t *entry, *next_entry;
450 while( bdb->bi_ndatabases-- ) {
451 db = bdb->bi_databases[bdb->bi_ndatabases];
452 rc = db->bdi_db->close( db->bdi_db, 0 );
453 /* Lower numbered names are not strdup'd */
454 if( bdb->bi_ndatabases >= BDB_NDB )
455 free( db->bdi_name );
458 free( bdb->bi_databases );
459 bdb_attr_index_destroy( bdb->bi_attrs );
461 bdb_cache_release_all (&bdb->bi_cache);
463 #ifdef SLAP_IDL_CACHE
464 if ( bdb->bi_idl_cache_max_size ) {
465 ldap_pvt_thread_rdwr_wlock ( &bdb->bi_idl_tree_rwlock );
466 entry = bdb->bi_idl_lru_head;
467 while ( entry != NULL ) {
468 next_entry = entry->idl_lru_next;
469 avl_delete( &bdb->bi_idl_tree, (caddr_t) entry,
473 free( entry->kstr.bv_val );
477 ldap_pvt_thread_rdwr_wunlock ( &bdb->bi_idl_tree_rwlock );
485 bdb_db_destroy( BackendDB *be )
488 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
490 /* close db environment */
491 if( bdb->bi_dbenv ) {
492 /* force a checkpoint */
493 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
496 LDAP_LOG( BACK_BDB, ERR,
497 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
498 db_strerror(rc), rc, 0 );
500 Debug( LDAP_DEBUG_ANY,
501 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
502 db_strerror(rc), rc, 0 );
506 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
507 bdb->bi_dbenv = NULL;
510 LDAP_LOG( BACK_BDB, ERR,
511 "bdb_db_destroy: close failed: %s (%d)\n",
512 db_strerror(rc), rc, 0 );
514 Debug( LDAP_DEBUG_ANY,
515 "bdb_db_destroy: close failed: %s (%d)\n",
516 db_strerror(rc), rc, 0 );
522 if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
525 ldap_pvt_thread_rdwr_destroy( &bdb->bi_tree_rdwr );
527 ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
528 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_mutex );
529 ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
530 #ifdef SLAP_IDL_CACHE
531 if ( bdb->bi_idl_cache_max_size ) {
532 ldap_pvt_thread_rdwr_destroy( &bdb->bi_idl_tree_rwlock );
533 ldap_pvt_thread_mutex_destroy( &bdb->bi_idl_tree_lrulock );
538 be->be_private = NULL;
543 #ifdef SLAPD_BDB_DYNAMIC
544 int back_bdb_LTX_init_module( int argc, char *argv[] ) {
547 memset( &bi, '\0', sizeof(bi) );
549 bi.bi_init = bdb_initialize;
554 #endif /* SLAPD_BDB_DYNAMIC */
561 static char *controls[] = {
562 LDAP_CONTROL_MANAGEDSAIT,
564 #ifdef LDAP_CONTROL_PAGEDRESULTS
565 LDAP_CONTROL_PAGEDRESULTS,
567 LDAP_CONTROL_VALUESRETURNFILTER,
568 #ifdef LDAP_CONTROL_SUBENTRIES
569 LDAP_CONTROL_SUBENTRIES,
571 #ifdef LDAP_CLIENT_UPDATE
572 LDAP_CONTROL_CLIENT_UPDATE,
577 bi->bi_controls = controls;
579 /* initialize the underlying database system */
581 LDAP_LOG( BACK_BDB, ENTRY, "bdb_db_initialize\n", 0, 0, 0 );
583 Debug( LDAP_DEBUG_TRACE, "bdb_initialize: initialize BDB backend\n",
587 { /* version check */
588 int major, minor, patch;
589 char *version = db_version( &major, &minor, &patch );
593 /* All our stdio does an ASCII to EBCDIC conversion on
594 * the output. Strings from the BDB library are already
595 * in EBCDIC; we have to go back and forth...
597 strcpy( v2, version );
602 if( major != DB_VERSION_MAJOR ||
603 minor != DB_VERSION_MINOR ||
604 patch < DB_VERSION_PATCH )
607 LDAP_LOG( BACK_BDB, ERR,
608 "bdb_db_initialize: version mismatch: "
609 "\texpected: %s \tgot: %s\n", DB_VERSION_STRING, version, 0 );
611 Debug( LDAP_DEBUG_ANY,
612 "bdb_initialize: version mismatch\n"
613 "\texpected: " DB_VERSION_STRING "\n"
614 "\tgot: %s \n", version, 0, 0 );
619 LDAP_LOG( BACK_BDB, DETAIL1,
620 "bdb_db_initialize: %s\n", version, 0, 0 );
622 Debug( LDAP_DEBUG_ANY, "bdb_initialize: %s\n",
627 db_env_set_func_free( ber_memfree );
628 db_env_set_func_malloc( (db_malloc *)ber_memalloc );
629 db_env_set_func_realloc( (db_realloc *)ber_memrealloc );
631 /* This is a no-op on a NO_THREAD build. Leave the default
632 * alone so that BDB will sleep on interprocess conflicts.
634 db_env_set_func_yield( ldap_pvt_thread_yield );
638 static char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
640 bdb_uuid.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ));
641 bdb_uuid.bv_val = uuidbuf;
649 bi->bi_db_init = bdb_db_init;
650 bi->bi_db_config = bdb_db_config;
651 bi->bi_db_open = bdb_db_open;
652 bi->bi_db_close = bdb_db_close;
653 bi->bi_db_destroy = bdb_db_destroy;
655 bi->bi_op_add = bdb_add;
656 bi->bi_op_bind = bdb_bind;
657 bi->bi_op_compare = bdb_compare;
658 bi->bi_op_delete = bdb_delete;
659 bi->bi_op_modify = bdb_modify;
660 bi->bi_op_modrdn = bdb_modrdn;
661 bi->bi_op_search = bdb_search;
663 bi->bi_op_unbind = 0;
665 #ifdef LDAP_CLIENT_UPDATE
666 bi->bi_op_abandon = bdb_abandon;
667 bi->bi_op_cancel = bdb_cancel;
669 bi->bi_op_abandon = 0;
670 bi->bi_op_cancel = 0;
673 bi->bi_extended = bdb_extended;
675 bi->bi_chk_referrals = bdb_referrals;
676 bi->bi_operational = bdb_operational;
677 bi->bi_has_subordinates = bdb_hasSubordinates;
678 bi->bi_entry_release_rw = bdb_entry_release;
679 bi->bi_entry_get_rw = bdb_entry_get;
682 * hooks for slap tools
684 bi->bi_tool_entry_open = bdb_tool_entry_open;
685 bi->bi_tool_entry_close = bdb_tool_entry_close;
686 bi->bi_tool_entry_first = bdb_tool_entry_next;
687 bi->bi_tool_entry_next = bdb_tool_entry_next;
688 bi->bi_tool_entry_get = bdb_tool_entry_get;
689 bi->bi_tool_entry_put = bdb_tool_entry_put;
690 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
691 bi->bi_tool_sync = 0;
693 bi->bi_connection_init = 0;
694 bi->bi_connection_destroy = 0;