1 /* init.c - initialize bdb backend */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2004 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_rdwr_init ( &bdb->bi_pslist_rwlock );
108 ldap_pvt_thread_mutex_init( &bdb->bi_cache.lru_mutex );
109 ldap_pvt_thread_mutex_init( &bdb->bi_cache.c_dntree.bei_kids_mutex );
110 ldap_pvt_thread_rdwr_init ( &bdb->bi_cache.c_rwlock );
112 be->be_private = bdb;
124 unsigned char *u, *c;
130 #ifdef WORDS_BIGENDIAN
131 for( i = 0; i < (int)sizeof(ID); i++)
133 for( i = sizeof(ID)-1; i >= 0; i--)
144 bdb_db_open( BackendDB *be )
147 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
150 char path[MAXPATHLEN];
154 LDAP_LOG( BACK_BDB, ARGS,
155 "bdb_db_open: %s\n", be->be_suffix[0].bv_val, 0, 0 );
157 Debug( LDAP_DEBUG_ARGS,
159 be->be_suffix[0].bv_val, 0, 0 );
162 #ifndef BDB_MULTIPLE_SUFFIXES
163 if ( be->be_suffix[1].bv_val ) {
165 LDAP_LOG( BACK_BDB, ERR,
166 "bdb_db_open: only one suffix allowed\n", 0, 0, 0 );
168 Debug( LDAP_DEBUG_ANY,
169 "bdb_db_open: only one suffix allowed\n", 0, 0, 0 );
174 /* we should check existance of dbenv_home and db_directory */
176 rc = db_env_create( &bdb->bi_dbenv, 0 );
179 LDAP_LOG( BACK_BDB, ERR,
180 "bdb_db_open: db_env_create failed: %s (%d)\n",
181 db_strerror(rc), rc, 0 );
183 Debug( LDAP_DEBUG_ANY,
184 "bdb_db_open: db_env_create failed: %s (%d)\n",
185 db_strerror(rc), rc, 0 );
190 flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE
191 | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN;
194 /* Never do automatic recovery, must perform it manually.
195 * Otherwise restarting with gentlehup will corrupt the
198 if( !(slapMode & SLAP_TOOL_MODE) ) flags |= DB_RECOVER;
201 /* If a key was set, use shared memory for the BDB environment */
202 if ( bdb->bi_shm_key ) {
203 bdb->bi_dbenv->set_shm_key( bdb->bi_dbenv, bdb->bi_shm_key );
204 flags |= DB_SYSTEM_MEM;
207 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
208 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
209 bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
211 #ifdef SLAP_IDL_CACHE
212 if ( bdb->bi_idl_cache_max_size ) {
213 bdb->bi_idl_tree = NULL;
214 ldap_pvt_thread_rdwr_init( &bdb->bi_idl_tree_rwlock );
215 ldap_pvt_thread_mutex_init( &bdb->bi_idl_tree_lrulock );
216 bdb->bi_idl_cache_size = 0;
222 char dir[MAXPATHLEN], *ptr;
224 if (bdb->bi_dbenv_home[0] == '.') {
225 /* If home is a relative path, relative subdirs
226 * are just concat'd by BDB. We don't want the
227 * path to be concat'd twice, e.g.
228 * ./test-db/./test-db/tmp
232 ptr = lutil_strcopy( dir, bdb->bi_dbenv_home );
233 *ptr++ = LDAP_DIRSEP[0];
239 strcpy( ptr, BDB_TMP_SUBDIR );
243 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
246 LDAP_LOG( BACK_BDB, ERR,
247 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
248 dir, db_strerror(rc), rc );
250 Debug( LDAP_DEBUG_ANY,
251 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
252 dir, db_strerror(rc), rc );
257 strcpy( ptr, BDB_LG_SUBDIR );
261 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
264 LDAP_LOG( BACK_BDB, ERR,
265 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
266 dir, db_strerror(rc), rc );
268 Debug( LDAP_DEBUG_ANY,
269 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
270 dir, db_strerror(rc), rc );
275 strcpy( ptr, BDB_DATA_SUBDIR );
279 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
282 LDAP_LOG( BACK_BDB, ERR,
283 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
284 dir, db_strerror(rc), rc );
286 Debug( LDAP_DEBUG_ANY,
287 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
288 dir, db_strerror(rc), rc );
296 LDAP_LOG( BACK_BDB, DETAIL1,
297 "bdb_db_open: dbenv_open %s\n", bdb->bi_dbenv_home, 0, 0 );
299 Debug( LDAP_DEBUG_TRACE,
300 "bdb_db_open: dbenv_open(%s)\n",
301 bdb->bi_dbenv_home, 0, 0);
305 strcpy( path, bdb->bi_dbenv_home );
307 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
310 bdb->bi_dbenv_mode );
312 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
315 bdb->bi_dbenv_mode );
319 LDAP_LOG( BACK_BDB, ERR,
320 "bdb_db_open: dbenv_open failed: %s (%d)\n",
321 db_strerror(rc), rc, 0 );
323 Debug( LDAP_DEBUG_ANY,
324 "bdb_db_open: dbenv_open failed: %s (%d)\n",
325 db_strerror(rc), rc, 0 );
330 if( bdb->bi_dbenv_xflags != 0 ) {
331 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
332 bdb->bi_dbenv_xflags, 1);
335 LDAP_LOG( BACK_BDB, ERR,
336 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
337 db_strerror(rc), rc, 0 );
339 Debug( LDAP_DEBUG_ANY,
340 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
341 db_strerror(rc), rc, 0 );
347 flags = DB_THREAD | bdb->bi_db_opflags;
349 bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
350 BDB_INDICES * sizeof(struct bdb_db_info *) );
352 /* open (and create) main database */
353 for( i = 0; bdbi_databases[i].name; i++ ) {
354 struct bdb_db_info *db;
356 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
358 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
361 LDAP_LOG( BACK_BDB, ERR,
362 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
363 bdb->bi_dbenv_home, db_strerror(rc), rc );
365 Debug( LDAP_DEBUG_ANY,
366 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
367 bdb->bi_dbenv_home, db_strerror(rc), rc );
372 if( i == BDB_ID2ENTRY ) {
373 rc = db->bdi_db->set_bt_compare( db->bdi_db,
375 rc = db->bdi_db->set_pagesize( db->bdi_db,
376 BDB_ID2ENTRY_PAGESIZE );
377 if ( slapMode & SLAP_TOOL_READMAIN ) {
383 rc = db->bdi_db->set_flags( db->bdi_db,
384 DB_DUP | DB_DUPSORT );
386 rc = db->bdi_db->set_dup_compare( db->bdi_db,
388 if ( slapMode & SLAP_TOOL_READONLY ) {
394 rc = db->bdi_db->set_dup_compare( db->bdi_db,
396 rc = db->bdi_db->set_bt_compare( db->bdi_db,
398 if ( slapMode & (SLAP_TOOL_READONLY|SLAP_TOOL_READMAIN) ) {
404 rc = db->bdi_db->set_pagesize( db->bdi_db,
409 strcpy( path, bdbi_databases[i].file );
411 rc = DB_OPEN( db->bdi_db,
413 /* bdbi_databases[i].name, */ NULL,
414 bdbi_databases[i].type,
415 bdbi_databases[i].flags | flags,
416 bdb->bi_dbenv_mode );
418 rc = DB_OPEN( db->bdi_db,
419 bdbi_databases[i].file,
420 /* bdbi_databases[i].name, */ NULL,
421 bdbi_databases[i].type,
422 bdbi_databases[i].flags | flags,
423 bdb->bi_dbenv_mode );
428 LDAP_LOG( BACK_BDB, ERR,
429 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
430 bdb->bi_dbenv_home, db_strerror(rc), rc );
432 Debug( LDAP_DEBUG_ANY,
433 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
434 bdb->bi_dbenv_home, db_strerror(rc), rc );
439 flags &= ~(DB_CREATE | DB_RDONLY);
440 db->bdi_name = bdbi_databases[i].name;
441 bdb->bi_databases[i] = db;
444 bdb->bi_databases[i] = NULL;
445 bdb->bi_ndatabases = i;
448 rc = bdb_last_id( be, NULL );
451 LDAP_LOG( BACK_BDB, ERR,
452 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
453 bdb->bi_dbenv_home, db_strerror(rc), rc );
455 Debug( LDAP_DEBUG_ANY,
456 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
457 bdb->bi_dbenv_home, db_strerror(rc), rc );
462 /* <insert> open (and create) index databases */
467 bdb_db_close( BackendDB *be )
470 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
471 struct bdb_db_info *db;
472 #ifdef SLAP_IDL_CACHE
473 bdb_idl_cache_entry_t *entry, *next_entry;
476 while( bdb->bi_ndatabases-- ) {
477 db = bdb->bi_databases[bdb->bi_ndatabases];
478 rc = db->bdi_db->close( db->bdi_db, 0 );
479 /* Lower numbered names are not strdup'd */
480 if( bdb->bi_ndatabases >= BDB_NDB )
481 free( db->bdi_name );
484 free( bdb->bi_databases );
485 bdb_attr_index_destroy( bdb->bi_attrs );
487 bdb_cache_release_all (&bdb->bi_cache);
489 #ifdef SLAP_IDL_CACHE
490 if ( bdb->bi_idl_cache_max_size ) {
491 ldap_pvt_thread_rdwr_wlock ( &bdb->bi_idl_tree_rwlock );
492 avl_free( bdb->bi_idl_tree, NULL );
493 entry = bdb->bi_idl_lru_head;
494 while ( entry != NULL ) {
495 next_entry = entry->idl_lru_next;
498 free( entry->kstr.bv_val );
502 ldap_pvt_thread_rdwr_wunlock ( &bdb->bi_idl_tree_rwlock );
510 bdb_db_destroy( BackendDB *be )
513 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
515 /* close db environment */
516 if( bdb->bi_dbenv ) {
517 /* force a checkpoint */
518 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
521 LDAP_LOG( BACK_BDB, ERR,
522 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
523 db_strerror(rc), rc, 0 );
525 Debug( LDAP_DEBUG_ANY,
526 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
527 db_strerror(rc), rc, 0 );
531 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
532 bdb->bi_dbenv = NULL;
535 LDAP_LOG( BACK_BDB, ERR,
536 "bdb_db_destroy: close failed: %s (%d)\n",
537 db_strerror(rc), rc, 0 );
539 Debug( LDAP_DEBUG_ANY,
540 "bdb_db_destroy: close failed: %s (%d)\n",
541 db_strerror(rc), rc, 0 );
547 if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
549 ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
550 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_mutex );
551 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_dntree.bei_kids_mutex );
552 ldap_pvt_thread_rdwr_destroy ( &bdb->bi_pslist_rwlock );
553 ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
554 ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
555 #ifdef SLAP_IDL_CACHE
556 if ( bdb->bi_idl_cache_max_size ) {
557 ldap_pvt_thread_rdwr_destroy( &bdb->bi_idl_tree_rwlock );
558 ldap_pvt_thread_mutex_destroy( &bdb->bi_idl_tree_lrulock );
563 be->be_private = NULL;
568 #if (defined(SLAPD_BDB_DYNAMIC) && !defined(BDB_HIER)) || \
569 (defined(SLAPD_HDB_DYNAMIC) && defined(BDB_HIER))
570 int init_module( int argc, char *argv[] ) {
573 memset( &bi, '\0', sizeof(bi) );
579 bi.bi_init = bdb_initialize;
584 #endif /* SLAPD_BDB_DYNAMIC */
591 static char *controls[] = {
593 LDAP_CONTROL_MANAGEDSAIT,
595 LDAP_CONTROL_PAGEDRESULTS,
596 #ifdef LDAP_CONTROL_SUBENTRIES
597 LDAP_CONTROL_SUBENTRIES,
599 LDAP_CONTROL_VALUESRETURNFILTER,
603 bi->bi_controls = controls;
605 /* initialize the underlying database system */
607 LDAP_LOG( BACK_BDB, ENTRY, "bdb_db_initialize\n", 0, 0, 0 );
609 Debug( LDAP_DEBUG_TRACE, "bdb_initialize: initialize BDB backend\n",
613 { /* version check */
614 int major, minor, patch;
615 char *version = db_version( &major, &minor, &patch );
619 /* All our stdio does an ASCII to EBCDIC conversion on
620 * the output. Strings from the BDB library are already
621 * in EBCDIC; we have to go back and forth...
623 strcpy( v2, version );
628 if( major != DB_VERSION_MAJOR ||
629 minor != DB_VERSION_MINOR ||
630 patch < DB_VERSION_PATCH )
633 LDAP_LOG( BACK_BDB, ERR,
634 "bdb_initialize: BDB library version mismatch:"
635 " expected " DB_VERSION_STRING ","
636 " got %s\n", version, 0, 0 );
638 Debug( LDAP_DEBUG_ANY,
639 "bdb_initialize: BDB library version mismatch:"
640 " expected " DB_VERSION_STRING ","
641 " got %s\n", version, 0, 0 );
646 LDAP_LOG( BACK_BDB, DETAIL1,
647 "bdb_db_initialize: %s\n", version, 0, 0 );
649 Debug( LDAP_DEBUG_ANY, "bdb_initialize: %s\n",
654 db_env_set_func_free( ber_memfree );
655 db_env_set_func_malloc( (db_malloc *)ber_memalloc );
656 db_env_set_func_realloc( (db_realloc *)ber_memrealloc );
658 /* This is a no-op on a NO_THREAD build. Leave the default
659 * alone so that BDB will sleep on interprocess conflicts.
661 db_env_set_func_yield( ldap_pvt_thread_yield );
665 static char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
667 bdb_uuid.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ));
668 bdb_uuid.bv_val = uuidbuf;
676 bi->bi_db_init = bdb_db_init;
677 bi->bi_db_config = bdb_db_config;
678 bi->bi_db_open = bdb_db_open;
679 bi->bi_db_close = bdb_db_close;
680 bi->bi_db_destroy = bdb_db_destroy;
682 bi->bi_op_add = bdb_add;
683 bi->bi_op_bind = bdb_bind;
684 bi->bi_op_compare = bdb_compare;
685 bi->bi_op_delete = bdb_delete;
686 bi->bi_op_modify = bdb_modify;
687 bi->bi_op_modrdn = bdb_modrdn;
688 bi->bi_op_search = bdb_search;
690 bi->bi_op_unbind = 0;
692 bi->bi_op_abandon = bdb_abandon;
693 bi->bi_op_cancel = bdb_cancel;
695 bi->bi_extended = bdb_extended;
697 bi->bi_chk_referrals = bdb_referrals;
698 bi->bi_operational = bdb_operational;
699 bi->bi_has_subordinates = bdb_hasSubordinates;
700 bi->bi_entry_release_rw = bdb_entry_release;
701 bi->bi_entry_get_rw = bdb_entry_get;
704 * hooks for slap tools
706 bi->bi_tool_entry_open = bdb_tool_entry_open;
707 bi->bi_tool_entry_close = bdb_tool_entry_close;
708 bi->bi_tool_entry_first = bdb_tool_entry_next;
709 bi->bi_tool_entry_next = bdb_tool_entry_next;
710 bi->bi_tool_entry_get = bdb_tool_entry_get;
711 bi->bi_tool_entry_put = bdb_tool_entry_put;
712 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
713 bi->bi_tool_sync = 0;
714 bi->bi_tool_dn2id_get = bdb_tool_dn2id_get;
715 bi->bi_tool_id2entry_get = bdb_tool_id2entry_get;
716 bi->bi_tool_entry_modify = bdb_tool_entry_modify;
718 bi->bi_connection_init = 0;
719 bi->bi_connection_destroy = 0;