1 /* init.c - initialize bdb backend */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2003 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
20 #include <ac/string.h>
21 #include <ac/unistd.h>
22 #include <ac/stdlib.h>
28 static const struct bdbi_database {
33 } bdbi_databases[] = {
34 { "id2entry" BDB_SUFFIX, "id2entry", DB_BTREE, 0 },
35 { "dn2id" BDB_SUFFIX, "dn2id", DB_BTREE, 0 },
39 struct berval bdb_uuid = { 0, NULL };
41 typedef void * db_malloc(size_t);
42 typedef void * db_realloc(void *, size_t);
46 bdb_open( BackendInfo *bi )
52 bdb_destroy( BackendInfo *bi )
58 bdb_close( BackendInfo *bi )
60 /* terminate the underlying database system */
66 bdb_db_init( BackendDB *be )
71 LDAP_LOG( BACK_BDB, ENTRY, "bdb_db_init", 0, 0, 0 );
73 Debug( LDAP_DEBUG_ANY,
74 "bdb_db_init: Initializing BDB database\n",
78 /* indicate system schema supported */
80 SLAP_BFLAG_INCREMENT |
82 SLAP_BFLAG_SUBENTRIES |
89 /* allocate backend-database-specific stuff */
90 bdb = (struct bdb_info *) ch_calloc( 1, sizeof(struct bdb_info) );
92 /* DBEnv parameters */
93 bdb->bi_dbenv_home = ch_strdup( SLAPD_DEFAULT_DB_DIR );
94 bdb->bi_dbenv_xflags = 0;
95 bdb->bi_dbenv_mode = SLAPD_DEFAULT_DB_MODE;
97 bdb->bi_cache.c_maxsize = DEFAULT_CACHE_SIZE;
99 bdb->bi_lock_detect = DB_LOCK_DEFAULT;
100 bdb->bi_search_stack_depth = DEFAULT_SEARCH_STACK_DEPTH;
101 bdb->bi_search_stack = NULL;
103 LDAP_LIST_INIT (&bdb->bi_psearch_list);
105 ldap_pvt_thread_mutex_init( &bdb->bi_database_mutex );
106 ldap_pvt_thread_mutex_init( &bdb->bi_lastid_mutex );
107 ldap_pvt_thread_mutex_init( &bdb->bi_cache.lru_mutex );
108 ldap_pvt_thread_mutex_init( &bdb->bi_cache.c_dntree.bei_kids_mutex );
109 ldap_pvt_thread_rdwr_init ( &bdb->bi_cache.c_rwlock );
111 be->be_private = bdb;
123 unsigned char *u, *c;
129 #ifdef WORDS_BIGENDIAN
130 for( i = 0; i < (int)sizeof(ID); i++)
132 for( i = sizeof(ID)-1; i >= 0; i--)
143 bdb_db_open( BackendDB *be )
146 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
149 char path[MAXPATHLEN];
153 LDAP_LOG( BACK_BDB, ARGS,
154 "bdb_db_open: %s\n", be->be_suffix[0].bv_val, 0, 0 );
156 Debug( LDAP_DEBUG_ARGS,
158 be->be_suffix[0].bv_val, 0, 0 );
161 #ifndef BDB_MULTIPLE_SUFFIXES
162 if ( be->be_suffix[1].bv_val ) {
164 LDAP_LOG( BACK_BDB, ERR,
165 "bdb_db_open: only one suffix allowed\n", 0, 0, 0 );
167 Debug( LDAP_DEBUG_ANY,
168 "bdb_db_open: only one suffix allowed\n", 0, 0, 0 );
173 /* we should check existance of dbenv_home and db_directory */
175 rc = db_env_create( &bdb->bi_dbenv, 0 );
178 LDAP_LOG( BACK_BDB, ERR,
179 "bdb_db_open: db_env_create failed: %s (%d)\n",
180 db_strerror(rc), rc, 0 );
182 Debug( LDAP_DEBUG_ANY,
183 "bdb_db_open: db_env_create failed: %s (%d)\n",
184 db_strerror(rc), rc, 0 );
189 flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE
190 | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN;
193 /* Never do automatic recovery, must perform it manually.
194 * Otherwise restarting with gentlehup will corrupt the
197 if( !(slapMode & SLAP_TOOL_MODE) ) flags |= DB_RECOVER;
200 /* If a key was set, use shared memory for the BDB environment */
201 if ( bdb->bi_shm_key ) {
202 bdb->bi_dbenv->set_shm_key( bdb->bi_dbenv, bdb->bi_shm_key );
203 flags |= DB_SYSTEM_MEM;
206 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
207 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
208 bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
210 #ifdef SLAP_IDL_CACHE
211 if ( bdb->bi_idl_cache_max_size ) {
212 bdb->bi_idl_tree = NULL;
213 ldap_pvt_thread_rdwr_init( &bdb->bi_idl_tree_rwlock );
214 ldap_pvt_thread_mutex_init( &bdb->bi_idl_tree_lrulock );
215 bdb->bi_idl_cache_size = 0;
221 char dir[MAXPATHLEN], *ptr;
223 if (bdb->bi_dbenv_home[0] == '.') {
224 /* If home is a relative path, relative subdirs
225 * are just concat'd by BDB. We don't want the
226 * path to be concat'd twice, e.g.
227 * ./test-db/./test-db/tmp
231 ptr = lutil_strcopy( dir, bdb->bi_dbenv_home );
232 *ptr++ = LDAP_DIRSEP[0];
238 strcpy( ptr, BDB_TMP_SUBDIR );
242 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
245 LDAP_LOG( BACK_BDB, ERR,
246 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
247 dir, db_strerror(rc), rc );
249 Debug( LDAP_DEBUG_ANY,
250 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
251 dir, db_strerror(rc), rc );
256 strcpy( ptr, BDB_LG_SUBDIR );
260 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
263 LDAP_LOG( BACK_BDB, ERR,
264 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
265 dir, db_strerror(rc), rc );
267 Debug( LDAP_DEBUG_ANY,
268 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
269 dir, db_strerror(rc), rc );
274 strcpy( ptr, BDB_DATA_SUBDIR );
278 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
281 LDAP_LOG( BACK_BDB, ERR,
282 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
283 dir, db_strerror(rc), rc );
285 Debug( LDAP_DEBUG_ANY,
286 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
287 dir, db_strerror(rc), rc );
295 LDAP_LOG( BACK_BDB, DETAIL1,
296 "bdb_db_open: dbenv_open %s\n", bdb->bi_dbenv_home, 0, 0 );
298 Debug( LDAP_DEBUG_TRACE,
299 "bdb_db_open: dbenv_open(%s)\n",
300 bdb->bi_dbenv_home, 0, 0);
304 strcpy( path, bdb->bi_dbenv_home );
306 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
309 bdb->bi_dbenv_mode );
311 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
314 bdb->bi_dbenv_mode );
318 LDAP_LOG( BACK_BDB, ERR,
319 "bdb_db_open: dbenv_open failed: %s (%d)\n",
320 db_strerror(rc), rc, 0 );
322 Debug( LDAP_DEBUG_ANY,
323 "bdb_db_open: dbenv_open failed: %s (%d)\n",
324 db_strerror(rc), rc, 0 );
329 if( bdb->bi_dbenv_xflags != 0 ) {
330 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
331 bdb->bi_dbenv_xflags, 1);
334 LDAP_LOG( BACK_BDB, ERR,
335 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
336 db_strerror(rc), rc, 0 );
338 Debug( LDAP_DEBUG_ANY,
339 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
340 db_strerror(rc), rc, 0 );
346 flags = DB_THREAD | bdb->bi_db_opflags;
348 bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
349 BDB_INDICES * sizeof(struct bdb_db_info *) );
351 /* open (and create) main database */
352 for( i = 0; bdbi_databases[i].name; i++ ) {
353 struct bdb_db_info *db;
355 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
357 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
360 LDAP_LOG( BACK_BDB, ERR,
361 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
362 bdb->bi_dbenv_home, db_strerror(rc), rc );
364 Debug( LDAP_DEBUG_ANY,
365 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
366 bdb->bi_dbenv_home, db_strerror(rc), rc );
371 if( i == BDB_ID2ENTRY ) {
372 rc = db->bdi_db->set_bt_compare( db->bdi_db,
374 rc = db->bdi_db->set_pagesize( db->bdi_db,
375 BDB_ID2ENTRY_PAGESIZE );
376 if ( slapMode & SLAP_TOOL_READMAIN ) {
382 rc = db->bdi_db->set_flags( db->bdi_db,
383 DB_DUP | DB_DUPSORT );
385 rc = db->bdi_db->set_dup_compare( db->bdi_db,
387 if ( slapMode & SLAP_TOOL_READONLY ) {
393 rc = db->bdi_db->set_dup_compare( db->bdi_db,
395 rc = db->bdi_db->set_bt_compare( db->bdi_db,
397 if ( slapMode & (SLAP_TOOL_READONLY|SLAP_TOOL_READMAIN) ) {
403 rc = db->bdi_db->set_pagesize( db->bdi_db,
408 strcpy( path, bdbi_databases[i].file );
410 rc = DB_OPEN( db->bdi_db,
412 /* bdbi_databases[i].name, */ NULL,
413 bdbi_databases[i].type,
414 bdbi_databases[i].flags | flags,
415 bdb->bi_dbenv_mode );
417 rc = DB_OPEN( db->bdi_db,
418 bdbi_databases[i].file,
419 /* bdbi_databases[i].name, */ NULL,
420 bdbi_databases[i].type,
421 bdbi_databases[i].flags | flags,
422 bdb->bi_dbenv_mode );
427 LDAP_LOG( BACK_BDB, ERR,
428 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
429 bdb->bi_dbenv_home, db_strerror(rc), rc );
431 Debug( LDAP_DEBUG_ANY,
432 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
433 bdb->bi_dbenv_home, db_strerror(rc), rc );
438 flags &= ~(DB_CREATE | DB_RDONLY);
439 db->bdi_name = bdbi_databases[i].name;
440 bdb->bi_databases[i] = db;
443 bdb->bi_databases[i] = NULL;
444 bdb->bi_ndatabases = i;
447 rc = bdb_last_id( be, NULL );
450 LDAP_LOG( BACK_BDB, ERR,
451 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
452 bdb->bi_dbenv_home, db_strerror(rc), rc );
454 Debug( LDAP_DEBUG_ANY,
455 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
456 bdb->bi_dbenv_home, db_strerror(rc), rc );
461 /* <insert> open (and create) index databases */
466 bdb_db_close( BackendDB *be )
469 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
470 struct bdb_db_info *db;
471 #ifdef SLAP_IDL_CACHE
472 bdb_idl_cache_entry_t *entry, *next_entry;
475 while( bdb->bi_ndatabases-- ) {
476 db = bdb->bi_databases[bdb->bi_ndatabases];
477 rc = db->bdi_db->close( db->bdi_db, 0 );
478 /* Lower numbered names are not strdup'd */
479 if( bdb->bi_ndatabases >= BDB_NDB )
480 free( db->bdi_name );
483 free( bdb->bi_databases );
484 bdb_attr_index_destroy( bdb->bi_attrs );
486 bdb_cache_release_all (&bdb->bi_cache);
488 #ifdef SLAP_IDL_CACHE
489 if ( bdb->bi_idl_cache_max_size ) {
490 ldap_pvt_thread_rdwr_wlock ( &bdb->bi_idl_tree_rwlock );
491 avl_free( bdb->bi_idl_tree, NULL );
492 entry = bdb->bi_idl_lru_head;
493 while ( entry != NULL ) {
494 next_entry = entry->idl_lru_next;
497 free( entry->kstr.bv_val );
501 ldap_pvt_thread_rdwr_wunlock ( &bdb->bi_idl_tree_rwlock );
509 bdb_db_destroy( BackendDB *be )
512 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
514 /* close db environment */
515 if( bdb->bi_dbenv ) {
516 /* force a checkpoint */
517 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
520 LDAP_LOG( BACK_BDB, ERR,
521 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
522 db_strerror(rc), rc, 0 );
524 Debug( LDAP_DEBUG_ANY,
525 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
526 db_strerror(rc), rc, 0 );
530 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
531 bdb->bi_dbenv = NULL;
534 LDAP_LOG( BACK_BDB, ERR,
535 "bdb_db_destroy: close failed: %s (%d)\n",
536 db_strerror(rc), rc, 0 );
538 Debug( LDAP_DEBUG_ANY,
539 "bdb_db_destroy: close failed: %s (%d)\n",
540 db_strerror(rc), rc, 0 );
546 if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
548 ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
549 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_mutex );
550 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_dntree.bei_kids_mutex );
551 ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
552 ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
553 #ifdef SLAP_IDL_CACHE
554 if ( bdb->bi_idl_cache_max_size ) {
555 ldap_pvt_thread_rdwr_destroy( &bdb->bi_idl_tree_rwlock );
556 ldap_pvt_thread_mutex_destroy( &bdb->bi_idl_tree_lrulock );
561 be->be_private = NULL;
566 #if (defined(SLAPD_BDB_DYNAMIC) && !defined(BDB_HIER)) || \
567 (defined(SLAPD_HDB_DYNAMIC) && defined(BDB_HIER))
568 int init_module( int argc, char *argv[] ) {
571 memset( &bi, '\0', sizeof(bi) );
577 bi.bi_init = bdb_initialize;
582 #endif /* SLAPD_BDB_DYNAMIC */
589 static char *controls[] = {
591 LDAP_CONTROL_MANAGEDSAIT,
593 LDAP_CONTROL_PAGEDRESULTS,
594 #ifdef LDAP_CONTROL_SUBENTRIES
595 LDAP_CONTROL_SUBENTRIES,
597 LDAP_CONTROL_VALUESRETURNFILTER,
601 bi->bi_controls = controls;
603 /* initialize the underlying database system */
605 LDAP_LOG( BACK_BDB, ENTRY, "bdb_db_initialize\n", 0, 0, 0 );
607 Debug( LDAP_DEBUG_TRACE, "bdb_initialize: initialize BDB backend\n",
611 { /* version check */
612 int major, minor, patch;
613 char *version = db_version( &major, &minor, &patch );
617 /* All our stdio does an ASCII to EBCDIC conversion on
618 * the output. Strings from the BDB library are already
619 * in EBCDIC; we have to go back and forth...
621 strcpy( v2, version );
626 if( major != DB_VERSION_MAJOR ||
627 minor != DB_VERSION_MINOR ||
628 patch < DB_VERSION_PATCH )
631 LDAP_LOG( BACK_BDB, ERR,
632 "bdb_initialize: BDB library version mismatch:"
633 " expected " DB_VERSION_STRING ","
634 " got %s\n", version, 0, 0 );
636 Debug( LDAP_DEBUG_ANY,
637 "bdb_initialize: BDB library version mismatch:"
638 " expected " DB_VERSION_STRING ","
639 " got %s\n", version, 0, 0 );
644 LDAP_LOG( BACK_BDB, DETAIL1,
645 "bdb_db_initialize: %s\n", version, 0, 0 );
647 Debug( LDAP_DEBUG_ANY, "bdb_initialize: %s\n",
652 db_env_set_func_free( ber_memfree );
653 db_env_set_func_malloc( (db_malloc *)ber_memalloc );
654 db_env_set_func_realloc( (db_realloc *)ber_memrealloc );
656 /* This is a no-op on a NO_THREAD build. Leave the default
657 * alone so that BDB will sleep on interprocess conflicts.
659 db_env_set_func_yield( ldap_pvt_thread_yield );
663 static char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
665 bdb_uuid.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ));
666 bdb_uuid.bv_val = uuidbuf;
674 bi->bi_db_init = bdb_db_init;
675 bi->bi_db_config = bdb_db_config;
676 bi->bi_db_open = bdb_db_open;
677 bi->bi_db_close = bdb_db_close;
678 bi->bi_db_destroy = bdb_db_destroy;
680 bi->bi_op_add = bdb_add;
681 bi->bi_op_bind = bdb_bind;
682 bi->bi_op_compare = bdb_compare;
683 bi->bi_op_delete = bdb_delete;
684 bi->bi_op_modify = bdb_modify;
685 bi->bi_op_modrdn = bdb_modrdn;
686 bi->bi_op_search = bdb_search;
688 bi->bi_op_unbind = 0;
690 bi->bi_op_abandon = bdb_abandon;
691 bi->bi_op_cancel = bdb_cancel;
693 bi->bi_extended = bdb_extended;
695 bi->bi_chk_referrals = bdb_referrals;
696 bi->bi_operational = bdb_operational;
697 bi->bi_has_subordinates = bdb_hasSubordinates;
698 bi->bi_entry_release_rw = bdb_entry_release;
699 bi->bi_entry_get_rw = bdb_entry_get;
702 * hooks for slap tools
704 bi->bi_tool_entry_open = bdb_tool_entry_open;
705 bi->bi_tool_entry_close = bdb_tool_entry_close;
706 bi->bi_tool_entry_first = bdb_tool_entry_next;
707 bi->bi_tool_entry_next = bdb_tool_entry_next;
708 bi->bi_tool_entry_get = bdb_tool_entry_get;
709 bi->bi_tool_entry_put = bdb_tool_entry_put;
710 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
711 bi->bi_tool_sync = 0;
712 bi->bi_tool_dn2id_get = bdb_tool_dn2id_get;
713 bi->bi_tool_id2entry_get = bdb_tool_id2entry_get;
714 bi->bi_tool_entry_modify = bdb_tool_entry_modify;
716 bi->bi_connection_init = 0;
717 bi->bi_connection_destroy = 0;