1 /* init.c - initialize bdb backend */
4 * Copyright 1998-2002 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>
20 static struct bdbi_database {
25 } bdbi_databases[] = {
26 { "id2entry" BDB_SUFFIX, "id2entry", DB_BTREE, 0 },
28 { "id2parent" BDB_SUFFIX, "id2parent", DB_BTREE, 0 },
30 { "dn2id" BDB_SUFFIX, "dn2id", DB_BTREE, 0 },
35 struct berval bdb_uuid = { 0, NULL };
37 typedef void * db_malloc(size_t);
38 typedef void * db_realloc(void *, size_t);
41 bdb_open( BackendInfo *bi )
48 bdb_destroy( BackendInfo *bi )
54 bdb_close( BackendInfo *bi )
56 /* terminate the underlying database system */
62 bdb_db_init( BackendDB *be )
67 LDAP_LOG( BACK_BDB, ENTRY, "bdb_db_init", 0, 0, 0 );
69 Debug( LDAP_DEBUG_ANY,
70 "bdb_db_init: Initializing BDB database\n",
74 /* indicate system schema supported */
77 SLAP_BFLAG_SUBENTRIES |
84 /* allocate backend-database-specific stuff */
85 bdb = (struct bdb_info *) ch_calloc( 1, sizeof(struct bdb_info) );
87 /* DBEnv parameters */
88 bdb->bi_dbenv_home = ch_strdup( SLAPD_DEFAULT_DB_DIR );
89 bdb->bi_dbenv_xflags = 0;
90 bdb->bi_dbenv_mode = SLAPD_DEFAULT_DB_MODE;
92 bdb->bi_cache.c_maxsize = DEFAULT_CACHE_SIZE;
96 bdb->bi_lock_detect = DB_LOCK_NORUN;
98 bdb->bi_lock_detect = DB_LOCK_DEFAULT;
102 ldap_pvt_thread_mutex_init( &bdb->bi_database_mutex );
103 ldap_pvt_thread_mutex_init( &bdb->bi_lastid_mutex );
104 ldap_pvt_thread_mutex_init( &bdb->bi_cache.lru_mutex );
105 ldap_pvt_thread_rdwr_init ( &bdb->bi_cache.c_rwlock );
107 ldap_pvt_thread_rdwr_init( &bdb->bi_tree_rdwr );
110 be->be_private = bdb;
114 #if 0 /* ifndef NO_THREADS */
115 static void *lock_detect_task( void *arg )
117 struct bdb_info *bdb = (struct bdb_info *) arg;
119 while( bdb->bi_dbenv != NULL ) {
122 sleep( bdb->bi_lock_detect_seconds );
124 rc = LOCK_DETECT( bdb->bi_dbenv, 0,
125 bdb->bi_lock_detect, &aborted );
132 LDAP_LOG( BACK_BDB, ERR, "bdb_db_init: aborted %d locks\n",
135 Debug( LDAP_DEBUG_ANY,
136 "bdb_lock_detect: aborted %d locks\n",
152 unsigned char *u, *c;
158 #ifdef WORDS_BIGENDIAN
159 for( i = 0; i < sizeof(ID); i++)
161 for( i = sizeof(ID)-1; i >= 0; i--)
171 bdb_db_open( BackendDB *be )
174 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
177 char path[MAXPATHLEN];
181 LDAP_LOG( BACK_BDB, ARGS,
182 "bdb_db_open: %s\n", be->be_suffix[0].bv_val, 0, 0 );
184 Debug( LDAP_DEBUG_ARGS,
186 be->be_suffix[0].bv_val, 0, 0 );
189 db_env_set_func_free( ber_memfree );
190 db_env_set_func_malloc( ber_memalloc );
191 db_env_set_func_realloc( ber_memrealloc );
193 /* we should check existance of dbenv_home and db_directory */
195 rc = db_env_create( &bdb->bi_dbenv, 0 );
198 LDAP_LOG( BACK_BDB, ERR,
199 "bdb_db_open: db_env_create failed: %s (%d)\n",
200 db_strerror(rc), rc, 0 );
202 Debug( LDAP_DEBUG_ANY,
203 "bdb_db_open: db_env_create failed: %s (%d)\n",
204 db_strerror(rc), rc, 0 );
209 flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE
210 | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN;
213 /* Never do automatic recovery, must perform it manually.
214 * Otherwise restarting with gentlehup will corrupt the
217 if( !(slapMode & SLAP_TOOL_MODE) ) flags |= DB_RECOVER;
220 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
221 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
223 bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
228 char dir[MAXPATHLEN];
229 size_t len = strlen( bdb->bi_dbenv_home );
231 strcpy( dir, bdb->bi_dbenv_home );
232 strcat( &dir[len], BDB_TMP_SUBDIR );
234 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
237 LDAP_LOG( BACK_BDB, ERR,
238 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
239 dir, db_strerror(rc), rc );
241 Debug( LDAP_DEBUG_ANY,
242 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
243 dir, db_strerror(rc), rc );
248 strcat( &dir[len], 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 strcat( &dir[len], BDB_DATA_SUBDIR );
266 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
269 LDAP_LOG( BACK_BDB, ERR,
270 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
271 dir, db_strerror(rc), rc );
273 Debug( LDAP_DEBUG_ANY,
274 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
275 dir, db_strerror(rc), rc );
283 LDAP_LOG( BACK_BDB, DETAIL1,
284 "bdb_db_open: dbenv_open %s\n", bdb->bi_dbenv_home, 0, 0 );
286 Debug( LDAP_DEBUG_TRACE,
287 "bdb_db_open: dbenv_open(%s)\n",
288 bdb->bi_dbenv_home, 0, 0);
292 strcpy( path, bdb->bi_dbenv_home );
294 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
297 bdb->bi_dbenv_mode );
299 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
302 bdb->bi_dbenv_mode );
306 LDAP_LOG( BACK_BDB, ERR,
307 "bdb_db_open: dbenv_open failed: %s (%d)\n",
308 db_strerror(rc), rc, 0 );
310 Debug( LDAP_DEBUG_ANY,
311 "bdb_db_open: dbenv_open failed: %s (%d)\n",
312 db_strerror(rc), rc, 0 );
317 if( bdb->bi_dbenv_xflags != 0 ) {
318 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
319 bdb->bi_dbenv_xflags, 1);
322 LDAP_LOG( BACK_BDB, ERR,
323 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
324 db_strerror(rc), rc, 0 );
326 Debug( LDAP_DEBUG_ANY,
327 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
328 db_strerror(rc), rc, 0 );
334 flags = DB_THREAD | DB_CREATE | bdb->bi_db_opflags;
336 bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
337 BDB_INDICES * sizeof(struct bdb_db_info *) );
339 /* open (and create) main database */
340 for( i = 0; bdbi_databases[i].name; i++ ) {
341 struct bdb_db_info *db;
343 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
345 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
348 LDAP_LOG( BACK_BDB, ERR,
349 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
350 bdb->bi_dbenv_home, db_strerror(rc), rc );
352 Debug( LDAP_DEBUG_ANY,
353 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
354 bdb->bi_dbenv_home, db_strerror(rc), rc );
359 if( i == BDB_ID2ENTRY ) {
360 rc = db->bdi_db->set_bt_compare( db->bdi_db,
362 rc = db->bdi_db->set_pagesize( db->bdi_db,
363 BDB_ID2ENTRY_PAGESIZE );
366 rc = db->bdi_db->set_bt_compare( db->bdi_db,
369 rc = db->bdi_db->set_flags( db->bdi_db,
370 DB_DUP | DB_DUPSORT );
371 rc = db->bdi_db->set_dup_compare( db->bdi_db,
374 rc = db->bdi_db->set_pagesize( db->bdi_db,
379 strcpy( path, bdbi_databases[i].file );
381 rc = DB_OPEN( db->bdi_db,
383 /* bdbi_databases[i].name, */ NULL,
384 bdbi_databases[i].type,
385 bdbi_databases[i].flags | flags,
386 bdb->bi_dbenv_mode );
388 rc = DB_OPEN( db->bdi_db,
389 bdbi_databases[i].file,
390 /* bdbi_databases[i].name, */ NULL,
391 bdbi_databases[i].type,
392 bdbi_databases[i].flags | flags,
393 bdb->bi_dbenv_mode );
398 LDAP_LOG( BACK_BDB, ERR,
399 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
400 bdb->bi_dbenv_home, db_strerror(rc), rc );
402 Debug( LDAP_DEBUG_ANY,
403 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
404 bdb->bi_dbenv_home, db_strerror(rc), rc );
409 db->bdi_name = bdbi_databases[i].name;
410 bdb->bi_databases[i] = db;
413 bdb->bi_databases[i] = NULL;
414 bdb->bi_ndatabases = i;
417 rc = bdb_last_id( be, NULL );
420 LDAP_LOG( BACK_BDB, ERR,
421 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
422 bdb->bi_dbenv_home, db_strerror(rc), rc );
424 Debug( LDAP_DEBUG_ANY,
425 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
426 bdb->bi_dbenv_home, db_strerror(rc), rc );
431 /* <insert> open (and create) index databases */
433 rc = bdb_build_tree( be );
436 #if 0 /* ifndef NO_THREADS */
437 if( bdb->bi_lock_detect != DB_LOCK_NORUN ) {
438 /* listener as a separate THREAD */
439 rc = ldap_pvt_thread_create( &bdb->bi_lock_detect_tid,
440 1, lock_detect_task, bdb );
447 bdb_db_close( BackendDB *be )
450 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
451 struct bdb_db_info *db;
453 while( bdb->bi_ndatabases-- ) {
454 db = bdb->bi_databases[bdb->bi_ndatabases];
455 rc = db->bdi_db->close( db->bdi_db, 0 );
456 /* Lower numbered names are not strdup'd */
457 if( bdb->bi_ndatabases >= BDB_NDB )
458 free( db->bdi_name );
461 free( bdb->bi_databases );
462 bdb_attr_index_destroy( bdb->bi_attrs );
464 bdb_cache_release_all (&bdb->bi_cache);
470 bdb_db_destroy( BackendDB *be )
473 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
475 /* close db environment */
476 if( bdb->bi_dbenv ) {
477 /* force a checkpoint */
478 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
481 LDAP_LOG( BACK_BDB, ERR,
482 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
483 db_strerror(rc), rc, 0 );
485 Debug( LDAP_DEBUG_ANY,
486 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
487 db_strerror(rc), rc, 0 );
491 bdb_cache_release_all (&bdb->bi_cache);
493 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
494 bdb->bi_dbenv = NULL;
497 LDAP_LOG( BACK_BDB, ERR,
498 "bdb_db_destroy: close failed: %s (%d)\n",
499 db_strerror(rc), rc, 0 );
501 Debug( LDAP_DEBUG_ANY,
502 "bdb_db_destroy: close failed: %s (%d)\n",
503 db_strerror(rc), rc, 0 );
509 if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
512 ldap_pvt_thread_rdwr_destroy( &bdb->bi_tree_rdwr );
514 ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
515 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_mutex );
516 ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
517 ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
520 be->be_private = NULL;
525 #ifdef SLAPD_BDB_DYNAMIC
526 int back_bdb_LTX_init_module( int argc, char *argv[] ) {
529 memset( &bi, '\0', sizeof(bi) );
531 bi.bi_init = bdb_initialize;
536 #endif /* SLAPD_BDB_DYNAMIC */
543 static char *controls[] = {
544 LDAP_CONTROL_MANAGEDSAIT,
545 #ifdef LDAP_CONTROL_SUBENTRIES
546 LDAP_CONTROL_SUBENTRIES,
548 #ifdef LDAP_CONTROL_NOOP
551 #ifdef LDAP_CONTROL_VALUESRETURNFILTER
552 LDAP_CONTROL_VALUESRETURNFILTER,
554 #ifdef LDAP_CLIENT_UPDATE
555 LDAP_CONTROL_CLIENT_UPDATE,
560 bi->bi_controls = controls;
562 /* initialize the underlying database system */
564 LDAP_LOG( BACK_BDB, ENTRY, "bdb_db_initialize\n", 0, 0, 0 );
566 Debug( LDAP_DEBUG_TRACE, "bdb_open: initialize BDB backend\n",
570 { /* version check */
571 int major, minor, patch;
572 char *version = db_version( &major, &minor, &patch );
576 /* All our stdio does an ASCII to EBCDIC conversion on
577 * the output. Strings from the BDB library are already
578 * in EBCDIC; we have to go back and forth...
580 strcpy( v2, version );
585 if( major != DB_VERSION_MAJOR ||
586 minor != DB_VERSION_MINOR ||
587 patch < DB_VERSION_PATCH )
590 LDAP_LOG( BACK_BDB, ERR,
591 "bdb_db_initialize: version mismatch: "
592 "\texpected: %s \tgot: %s\n", DB_VERSION_STRING, version, 0 );
594 Debug( LDAP_DEBUG_ANY,
595 "bdb_open: version mismatch\n"
596 "\texpected: " DB_VERSION_STRING "\n"
597 "\tgot: %s \n", version, 0, 0 );
602 LDAP_LOG( BACK_BDB, DETAIL1,
603 "bdb_db_initialize: bdb_open: %s\n", version, 0, 0 );
605 Debug( LDAP_DEBUG_ANY, "bdb_open: %s\n",
611 db_env_set_func_malloc( ch_malloc );
612 db_env_set_func_realloc( ch_realloc );
613 db_env_set_func_free( ch_free );
616 db_env_set_func_yield( ldap_pvt_thread_yield );
619 static char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
621 bdb_uuid.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ));
622 bdb_uuid.bv_val = uuidbuf;
630 bi->bi_db_init = bdb_db_init;
631 bi->bi_db_config = bdb_db_config;
632 bi->bi_db_open = bdb_db_open;
633 bi->bi_db_close = bdb_db_close;
634 bi->bi_db_destroy = bdb_db_destroy;
636 bi->bi_op_add = bdb_add;
637 bi->bi_op_bind = bdb_bind;
638 bi->bi_op_compare = bdb_compare;
639 bi->bi_op_delete = bdb_delete;
640 bi->bi_op_modify = bdb_modify;
641 bi->bi_op_modrdn = bdb_modrdn;
642 bi->bi_op_search = bdb_search;
644 bi->bi_op_unbind = 0;
645 bi->bi_op_abandon = 0;
647 bi->bi_extended = bdb_extended;
651 * these routines (and their callers) are not yet designed
652 * to work with transaction. Using them may cause deadlock.
654 bi->bi_acl_group = bdb_group;
655 bi->bi_acl_attribute = bdb_attribute;
657 bi->bi_acl_group = 0;
658 bi->bi_acl_attribute = 0;
661 bi->bi_chk_referrals = bdb_referrals;
662 bi->bi_operational = bdb_operational;
663 bi->bi_entry_release_rw = bdb_entry_release;
666 * hooks for slap tools
668 bi->bi_tool_entry_open = bdb_tool_entry_open;
669 bi->bi_tool_entry_close = bdb_tool_entry_close;
670 bi->bi_tool_entry_first = bdb_tool_entry_next;
671 bi->bi_tool_entry_next = bdb_tool_entry_next;
672 bi->bi_tool_entry_get = bdb_tool_entry_get;
673 bi->bi_tool_entry_put = bdb_tool_entry_put;
674 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
675 bi->bi_tool_sync = 0;
677 bi->bi_connection_init = 0;
678 bi->bi_connection_destroy = 0;