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 const struct bdbi_database {
24 } bdbi_databases[] = {
25 { "id2entry" BDB_SUFFIX, "id2entry", DB_BTREE, 0 },
26 { "dn2id" BDB_SUFFIX, "dn2id", DB_BTREE, 0 },
30 struct berval bdb_uuid = { 0, NULL };
32 typedef void * db_malloc(size_t);
33 typedef void * db_realloc(void *, size_t);
37 bdb_open( BackendInfo *bi )
43 bdb_destroy( BackendInfo *bi )
49 bdb_close( BackendInfo *bi )
51 /* terminate the underlying database system */
57 bdb_db_init( BackendDB *be )
62 LDAP_LOG( BACK_BDB, ENTRY, "bdb_db_init", 0, 0, 0 );
64 Debug( LDAP_DEBUG_ANY,
65 "bdb_db_init: Initializing BDB database\n",
69 /* indicate system schema supported */
71 SLAP_BFLAG_INCREMENT |
73 SLAP_BFLAG_SUBENTRIES |
80 /* allocate backend-database-specific stuff */
81 bdb = (struct bdb_info *) ch_calloc( 1, sizeof(struct bdb_info) );
83 /* DBEnv parameters */
84 bdb->bi_dbenv_home = ch_strdup( SLAPD_DEFAULT_DB_DIR );
85 bdb->bi_dbenv_xflags = 0;
86 bdb->bi_dbenv_mode = SLAPD_DEFAULT_DB_MODE;
88 bdb->bi_cache.c_maxsize = DEFAULT_CACHE_SIZE;
90 bdb->bi_lock_detect = DB_LOCK_DEFAULT;
91 bdb->bi_search_stack_depth = DEFAULT_SEARCH_STACK_DEPTH;
92 bdb->bi_search_stack = NULL;
94 LDAP_LIST_INIT (&bdb->bi_psearch_list);
96 ldap_pvt_thread_mutex_init( &bdb->bi_database_mutex );
97 ldap_pvt_thread_mutex_init( &bdb->bi_lastid_mutex );
98 ldap_pvt_thread_mutex_init( &bdb->bi_cache.lru_mutex );
99 ldap_pvt_thread_mutex_init( &bdb->bi_cache.c_dntree.bei_kids_mutex );
100 ldap_pvt_thread_rdwr_init ( &bdb->bi_cache.c_rwlock );
102 be->be_private = bdb;
114 unsigned char *u, *c;
120 #ifdef WORDS_BIGENDIAN
121 for( i = 0; i < (int)sizeof(ID); i++)
123 for( i = sizeof(ID)-1; i >= 0; i--)
134 bdb_db_open( BackendDB *be )
137 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
140 char path[MAXPATHLEN];
144 LDAP_LOG( BACK_BDB, ARGS,
145 "bdb_db_open: %s\n", be->be_suffix[0].bv_val, 0, 0 );
147 Debug( LDAP_DEBUG_ARGS,
149 be->be_suffix[0].bv_val, 0, 0 );
152 #ifndef BDB_MULTIPLE_SUFFIXES
153 if ( be->be_suffix[1].bv_val ) {
155 LDAP_LOG( BACK_BDB, ERR,
156 "bdb_db_open: only one suffix allowed\n", 0, 0, 0 );
158 Debug( LDAP_DEBUG_ANY,
159 "bdb_db_open: only one suffix allowed\n", 0, 0, 0 );
164 /* we should check existance of dbenv_home and db_directory */
166 rc = db_env_create( &bdb->bi_dbenv, 0 );
169 LDAP_LOG( BACK_BDB, ERR,
170 "bdb_db_open: db_env_create failed: %s (%d)\n",
171 db_strerror(rc), rc, 0 );
173 Debug( LDAP_DEBUG_ANY,
174 "bdb_db_open: db_env_create failed: %s (%d)\n",
175 db_strerror(rc), rc, 0 );
180 flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE
181 | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN;
184 /* Never do automatic recovery, must perform it manually.
185 * Otherwise restarting with gentlehup will corrupt the
188 if( !(slapMode & SLAP_TOOL_MODE) ) flags |= DB_RECOVER;
191 /* If a key was set, use shared memory for the BDB environment */
192 if ( bdb->bi_shm_key ) {
193 bdb->bi_dbenv->set_shm_key( bdb->bi_dbenv, bdb->bi_shm_key );
194 flags |= DB_SYSTEM_MEM;
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 bdb->bi_idl_tree = NULL;
204 ldap_pvt_thread_rdwr_init( &bdb->bi_idl_tree_rwlock );
205 ldap_pvt_thread_mutex_init( &bdb->bi_idl_tree_lrulock );
206 bdb->bi_idl_cache_size = 0;
212 char dir[MAXPATHLEN], *ptr;
214 if (bdb->bi_dbenv_home[0] == '.') {
215 /* If home is a relative path, relative subdirs
216 * are just concat'd by BDB. We don't want the
217 * path to be concat'd twice, e.g.
218 * ./test-db/./test-db/tmp
222 ptr = lutil_strcopy( dir, bdb->bi_dbenv_home );
223 *ptr++ = LDAP_DIRSEP[0];
229 strcpy( ptr, BDB_TMP_SUBDIR );
233 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
236 LDAP_LOG( BACK_BDB, ERR,
237 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
238 dir, db_strerror(rc), rc );
240 Debug( LDAP_DEBUG_ANY,
241 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
242 dir, db_strerror(rc), rc );
247 strcpy( ptr, BDB_LG_SUBDIR );
251 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
254 LDAP_LOG( BACK_BDB, ERR,
255 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
256 dir, db_strerror(rc), rc );
258 Debug( LDAP_DEBUG_ANY,
259 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
260 dir, db_strerror(rc), rc );
265 strcpy( ptr, BDB_DATA_SUBDIR );
269 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
272 LDAP_LOG( BACK_BDB, ERR,
273 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
274 dir, db_strerror(rc), rc );
276 Debug( LDAP_DEBUG_ANY,
277 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
278 dir, db_strerror(rc), rc );
286 LDAP_LOG( BACK_BDB, DETAIL1,
287 "bdb_db_open: dbenv_open %s\n", bdb->bi_dbenv_home, 0, 0 );
289 Debug( LDAP_DEBUG_TRACE,
290 "bdb_db_open: dbenv_open(%s)\n",
291 bdb->bi_dbenv_home, 0, 0);
295 strcpy( path, bdb->bi_dbenv_home );
297 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
300 bdb->bi_dbenv_mode );
302 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
305 bdb->bi_dbenv_mode );
309 LDAP_LOG( BACK_BDB, ERR,
310 "bdb_db_open: dbenv_open failed: %s (%d)\n",
311 db_strerror(rc), rc, 0 );
313 Debug( LDAP_DEBUG_ANY,
314 "bdb_db_open: dbenv_open failed: %s (%d)\n",
315 db_strerror(rc), rc, 0 );
320 if( bdb->bi_dbenv_xflags != 0 ) {
321 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
322 bdb->bi_dbenv_xflags, 1);
325 LDAP_LOG( BACK_BDB, ERR,
326 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
327 db_strerror(rc), rc, 0 );
329 Debug( LDAP_DEBUG_ANY,
330 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
331 db_strerror(rc), rc, 0 );
337 flags = DB_THREAD | bdb->bi_db_opflags;
339 bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
340 BDB_INDICES * sizeof(struct bdb_db_info *) );
342 /* open (and create) main database */
343 for( i = 0; bdbi_databases[i].name; i++ ) {
344 struct bdb_db_info *db;
346 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
348 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
351 LDAP_LOG( BACK_BDB, ERR,
352 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
353 bdb->bi_dbenv_home, db_strerror(rc), rc );
355 Debug( LDAP_DEBUG_ANY,
356 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
357 bdb->bi_dbenv_home, db_strerror(rc), rc );
362 if( i == BDB_ID2ENTRY ) {
363 rc = db->bdi_db->set_bt_compare( db->bdi_db,
365 rc = db->bdi_db->set_pagesize( db->bdi_db,
366 BDB_ID2ENTRY_PAGESIZE );
367 if ( slapMode & SLAP_TOOL_READMAIN ) {
373 rc = db->bdi_db->set_flags( db->bdi_db,
374 DB_DUP | DB_DUPSORT );
376 rc = db->bdi_db->set_dup_compare( db->bdi_db,
378 if ( slapMode & SLAP_TOOL_READONLY ) {
384 rc = db->bdi_db->set_dup_compare( db->bdi_db,
386 rc = db->bdi_db->set_bt_compare( db->bdi_db,
388 if ( slapMode & (SLAP_TOOL_READONLY|SLAP_TOOL_READMAIN) ) {
394 rc = db->bdi_db->set_pagesize( db->bdi_db,
399 strcpy( path, bdbi_databases[i].file );
401 rc = DB_OPEN( db->bdi_db,
403 /* bdbi_databases[i].name, */ NULL,
404 bdbi_databases[i].type,
405 bdbi_databases[i].flags | flags | DB_AUTO_COMMIT,
406 bdb->bi_dbenv_mode );
408 rc = DB_OPEN( db->bdi_db,
409 bdbi_databases[i].file,
410 /* bdbi_databases[i].name, */ NULL,
411 bdbi_databases[i].type,
412 bdbi_databases[i].flags | flags | DB_AUTO_COMMIT,
413 bdb->bi_dbenv_mode );
418 LDAP_LOG( BACK_BDB, ERR,
419 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
420 bdb->bi_dbenv_home, db_strerror(rc), rc );
422 Debug( LDAP_DEBUG_ANY,
423 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
424 bdb->bi_dbenv_home, db_strerror(rc), rc );
429 flags &= ~(DB_CREATE | DB_RDONLY);
430 db->bdi_name = bdbi_databases[i].name;
431 bdb->bi_databases[i] = db;
434 bdb->bi_databases[i] = NULL;
435 bdb->bi_ndatabases = i;
438 rc = bdb_last_id( be, NULL );
441 LDAP_LOG( BACK_BDB, ERR,
442 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
443 bdb->bi_dbenv_home, db_strerror(rc), rc );
445 Debug( LDAP_DEBUG_ANY,
446 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
447 bdb->bi_dbenv_home, db_strerror(rc), rc );
452 /* <insert> open (and create) index databases */
457 bdb_db_close( BackendDB *be )
460 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
461 struct bdb_db_info *db;
462 #ifdef SLAP_IDL_CACHE
463 bdb_idl_cache_entry_t *entry, *next_entry;
466 while( bdb->bi_ndatabases-- ) {
467 db = bdb->bi_databases[bdb->bi_ndatabases];
468 rc = db->bdi_db->close( db->bdi_db, 0 );
469 /* Lower numbered names are not strdup'd */
470 if( bdb->bi_ndatabases >= BDB_NDB )
471 free( db->bdi_name );
474 free( bdb->bi_databases );
475 bdb_attr_index_destroy( bdb->bi_attrs );
477 bdb_cache_release_all (&bdb->bi_cache);
479 #ifdef SLAP_IDL_CACHE
480 if ( bdb->bi_idl_cache_max_size ) {
481 ldap_pvt_thread_rdwr_wlock ( &bdb->bi_idl_tree_rwlock );
482 avl_free( bdb->bi_idl_tree, NULL );
483 entry = bdb->bi_idl_lru_head;
484 while ( entry != NULL ) {
485 next_entry = entry->idl_lru_next;
488 free( entry->kstr.bv_val );
492 ldap_pvt_thread_rdwr_wunlock ( &bdb->bi_idl_tree_rwlock );
500 bdb_db_destroy( BackendDB *be )
503 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
505 /* close db environment */
506 if( bdb->bi_dbenv ) {
507 /* force a checkpoint */
508 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
511 LDAP_LOG( BACK_BDB, ERR,
512 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
513 db_strerror(rc), rc, 0 );
515 Debug( LDAP_DEBUG_ANY,
516 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
517 db_strerror(rc), rc, 0 );
521 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
522 bdb->bi_dbenv = NULL;
525 LDAP_LOG( BACK_BDB, ERR,
526 "bdb_db_destroy: close failed: %s (%d)\n",
527 db_strerror(rc), rc, 0 );
529 Debug( LDAP_DEBUG_ANY,
530 "bdb_db_destroy: close failed: %s (%d)\n",
531 db_strerror(rc), rc, 0 );
537 if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
539 ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
540 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_mutex );
541 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_dntree.bei_kids_mutex );
542 ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
543 ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
544 #ifdef SLAP_IDL_CACHE
545 if ( bdb->bi_idl_cache_max_size ) {
546 ldap_pvt_thread_rdwr_destroy( &bdb->bi_idl_tree_rwlock );
547 ldap_pvt_thread_mutex_destroy( &bdb->bi_idl_tree_lrulock );
552 be->be_private = NULL;
557 #ifdef SLAPD_BDB_DYNAMIC
558 int init_module( int argc, char *argv[] ) {
561 memset( &bi, '\0', sizeof(bi) );
563 bi.bi_init = bdb_initialize;
568 #endif /* SLAPD_BDB_DYNAMIC */
575 static char *controls[] = {
577 LDAP_CONTROL_MANAGEDSAIT,
579 #ifdef LDAP_CONTROL_PAGEDRESULTS
580 LDAP_CONTROL_PAGEDRESULTS,
582 #ifdef LDAP_CONTROL_SUBENTRIES
583 LDAP_CONTROL_SUBENTRIES,
585 LDAP_CONTROL_VALUESRETURNFILTER,
589 bi->bi_controls = controls;
591 /* initialize the underlying database system */
593 LDAP_LOG( BACK_BDB, ENTRY, "bdb_db_initialize\n", 0, 0, 0 );
595 Debug( LDAP_DEBUG_TRACE, "bdb_initialize: initialize BDB backend\n",
599 { /* version check */
600 int major, minor, patch;
601 char *version = db_version( &major, &minor, &patch );
605 /* All our stdio does an ASCII to EBCDIC conversion on
606 * the output. Strings from the BDB library are already
607 * in EBCDIC; we have to go back and forth...
609 strcpy( v2, version );
614 if( major != DB_VERSION_MAJOR ||
615 minor != DB_VERSION_MINOR ||
616 patch < DB_VERSION_PATCH )
619 LDAP_LOG( BACK_BDB, ERR,
620 "bdb_initialize: BDB library version mismatch:"
621 " expected " DB_VERSION_STRING ","
622 " got %s\n", version, 0, 0 );
624 Debug( LDAP_DEBUG_ANY,
625 "bdb_initialize: BDB library version mismatch:"
626 " expected " DB_VERSION_STRING ","
627 " got %s\n", version, 0, 0 );
632 LDAP_LOG( BACK_BDB, DETAIL1,
633 "bdb_db_initialize: %s\n", version, 0, 0 );
635 Debug( LDAP_DEBUG_ANY, "bdb_initialize: %s\n",
640 db_env_set_func_free( ber_memfree );
641 db_env_set_func_malloc( (db_malloc *)ber_memalloc );
642 db_env_set_func_realloc( (db_realloc *)ber_memrealloc );
644 /* This is a no-op on a NO_THREAD build. Leave the default
645 * alone so that BDB will sleep on interprocess conflicts.
647 db_env_set_func_yield( ldap_pvt_thread_yield );
651 static char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
653 bdb_uuid.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ));
654 bdb_uuid.bv_val = uuidbuf;
662 bi->bi_db_init = bdb_db_init;
663 bi->bi_db_config = bdb_db_config;
664 bi->bi_db_open = bdb_db_open;
665 bi->bi_db_close = bdb_db_close;
666 bi->bi_db_destroy = bdb_db_destroy;
668 bi->bi_op_add = bdb_add;
669 bi->bi_op_bind = bdb_bind;
670 bi->bi_op_compare = bdb_compare;
671 bi->bi_op_delete = bdb_delete;
672 bi->bi_op_modify = bdb_modify;
673 bi->bi_op_modrdn = bdb_modrdn;
674 bi->bi_op_search = bdb_search;
676 bi->bi_op_unbind = 0;
678 bi->bi_op_abandon = bdb_abandon;
679 bi->bi_op_cancel = bdb_cancel;
681 bi->bi_extended = bdb_extended;
683 bi->bi_chk_referrals = bdb_referrals;
684 bi->bi_operational = bdb_operational;
685 bi->bi_has_subordinates = bdb_hasSubordinates;
686 bi->bi_entry_release_rw = bdb_entry_release;
687 bi->bi_entry_get_rw = bdb_entry_get;
690 * hooks for slap tools
692 bi->bi_tool_entry_open = bdb_tool_entry_open;
693 bi->bi_tool_entry_close = bdb_tool_entry_close;
694 bi->bi_tool_entry_first = bdb_tool_entry_next;
695 bi->bi_tool_entry_next = bdb_tool_entry_next;
696 bi->bi_tool_entry_get = bdb_tool_entry_get;
697 bi->bi_tool_entry_put = bdb_tool_entry_put;
698 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
699 bi->bi_tool_sync = 0;
700 bi->bi_tool_dn2id_get = bdb_tool_dn2id_get;
701 bi->bi_tool_id2entry_get = bdb_tool_id2entry_get;
702 bi->bi_tool_entry_modify = bdb_tool_entry_modify;
704 bi->bi_connection_init = 0;
705 bi->bi_connection_destroy = 0;