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;
178 LDAP_LOG( BACK_BDB, ARGS,
179 "bdb_db_open: %s\n", be->be_suffix[0].bv_val, 0, 0 );
181 Debug( LDAP_DEBUG_ARGS,
183 be->be_suffix[0].bv_val, 0, 0 );
186 /* we should check existance of dbenv_home and db_directory */
188 rc = db_env_create( &bdb->bi_dbenv, 0 );
191 LDAP_LOG( BACK_BDB, ERR,
192 "bdb_db_open: db_env_create failed: %s (%d)\n",
193 db_strerror(rc), rc, 0 );
195 Debug( LDAP_DEBUG_ANY,
196 "bdb_db_open: db_env_create failed: %s (%d)\n",
197 db_strerror(rc), rc, 0 );
202 bdb->bi_dbenv->set_alloc( bdb->bi_dbenv, (db_malloc *)ber_memalloc,
203 (db_realloc *)ber_memrealloc, ber_memfree );
205 flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE
206 | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN;
209 /* Never do automatic recovery, must perform it manually.
210 * Otherwise restarting with gentlehup will corrupt the
213 if( !(slapMode & SLAP_TOOL_MODE) ) flags |= DB_RECOVER;
216 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
217 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
219 bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
224 char dir[MAXPATHLEN];
225 size_t len = strlen( bdb->bi_dbenv_home );
227 strcpy( dir, bdb->bi_dbenv_home );
228 strcat( &dir[len], BDB_TMP_SUBDIR );
230 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
233 LDAP_LOG( BACK_BDB, ERR,
234 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
235 dir, db_strerror(rc), rc );
237 Debug( LDAP_DEBUG_ANY,
238 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
239 dir, db_strerror(rc), rc );
244 strcat( &dir[len], BDB_LG_SUBDIR );
246 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
249 LDAP_LOG( BACK_BDB, ERR,
250 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
251 dir, db_strerror(rc), rc );
253 Debug( LDAP_DEBUG_ANY,
254 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
255 dir, db_strerror(rc), rc );
260 strcat( &dir[len], BDB_DATA_SUBDIR );
262 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
265 LDAP_LOG( BACK_BDB, ERR,
266 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
267 dir, db_strerror(rc), rc );
269 Debug( LDAP_DEBUG_ANY,
270 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
271 dir, db_strerror(rc), rc );
279 LDAP_LOG( BACK_BDB, DETAIL1,
280 "bdb_db_open: dbenv_open %s\n", bdb->bi_dbenv_home, 0, 0 );
282 Debug( LDAP_DEBUG_TRACE,
283 "bdb_db_open: dbenv_open(%s)\n",
284 bdb->bi_dbenv_home, 0, 0);
287 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
290 bdb->bi_dbenv_mode );
293 LDAP_LOG( BACK_BDB, ERR,
294 "bdb_db_open: dbenv_open failed: %s (%d)\n",
295 db_strerror(rc), rc, 0 );
297 Debug( LDAP_DEBUG_ANY,
298 "bdb_db_open: dbenv_open failed: %s (%d)\n",
299 db_strerror(rc), rc, 0 );
304 if( bdb->bi_dbenv_xflags != 0 ) {
305 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
306 bdb->bi_dbenv_xflags, 1);
309 LDAP_LOG( BACK_BDB, ERR,
310 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
311 db_strerror(rc), rc, 0 );
313 Debug( LDAP_DEBUG_ANY,
314 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
315 db_strerror(rc), rc, 0 );
321 flags = DB_THREAD | DB_CREATE | bdb->bi_db_opflags;
323 bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
324 BDB_INDICES * sizeof(struct bdb_db_info *) );
326 /* open (and create) main database */
327 for( i = 0; bdbi_databases[i].name; i++ ) {
328 struct bdb_db_info *db;
330 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
332 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
335 LDAP_LOG( BACK_BDB, ERR,
336 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
337 bdb->bi_dbenv_home, db_strerror(rc), rc );
339 Debug( LDAP_DEBUG_ANY,
340 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
341 bdb->bi_dbenv_home, db_strerror(rc), rc );
346 if( i == BDB_ID2ENTRY ) {
347 rc = db->bdi_db->set_bt_compare( db->bdi_db,
349 rc = db->bdi_db->set_pagesize( db->bdi_db,
350 BDB_ID2ENTRY_PAGESIZE );
353 rc = db->bdi_db->set_bt_compare( db->bdi_db,
355 #elif defined(BDB_IDL_MULTI)
356 rc = db->bdi_db->set_flags( db->bdi_db,
357 DB_DUP | DB_DUPSORT );
358 rc = db->bdi_db->set_dup_compare( db->bdi_db,
361 rc = db->bdi_db->set_pagesize( db->bdi_db,
365 rc = db->bdi_db->open( db->bdi_db,
366 bdbi_databases[i].file,
367 /* bdbi_databases[i].name, */ NULL,
368 bdbi_databases[i].type,
369 bdbi_databases[i].flags | flags,
370 bdb->bi_dbenv_mode );
374 LDAP_LOG( BACK_BDB, ERR,
375 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
376 bdb->bi_dbenv_home, db_strerror(rc), rc );
378 Debug( LDAP_DEBUG_ANY,
379 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
380 bdb->bi_dbenv_home, db_strerror(rc), rc );
385 db->bdi_name = bdbi_databases[i].name;
386 bdb->bi_databases[i] = db;
389 bdb->bi_databases[i] = NULL;
390 bdb->bi_ndatabases = i;
393 rc = bdb_last_id( be, NULL );
396 LDAP_LOG( BACK_BDB, ERR,
397 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
398 bdb->bi_dbenv_home, db_strerror(rc), rc );
400 Debug( LDAP_DEBUG_ANY,
401 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
402 bdb->bi_dbenv_home, db_strerror(rc), rc );
407 /* <insert> open (and create) index databases */
409 rc = bdb_build_tree( be );
412 #if 0 /* ifndef NO_THREADS */
413 if( bdb->bi_lock_detect != DB_LOCK_NORUN ) {
414 /* listener as a separate THREAD */
415 rc = ldap_pvt_thread_create( &bdb->bi_lock_detect_tid,
416 1, lock_detect_task, bdb );
423 bdb_db_close( BackendDB *be )
426 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
427 struct bdb_db_info *db;
429 while( bdb->bi_ndatabases-- ) {
430 db = bdb->bi_databases[bdb->bi_ndatabases];
431 rc = db->bdi_db->close( db->bdi_db, 0 );
432 /* Lower numbered names are not strdup'd */
433 if( bdb->bi_ndatabases >= BDB_NDB )
434 free( db->bdi_name );
437 free( bdb->bi_databases );
438 bdb_attr_index_destroy( bdb->bi_attrs );
440 bdb_cache_release_all (&bdb->bi_cache);
446 bdb_db_destroy( BackendDB *be )
449 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
451 /* close db environment */
452 if( bdb->bi_dbenv ) {
453 /* force a checkpoint */
454 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
457 LDAP_LOG( BACK_BDB, ERR,
458 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
459 db_strerror(rc), rc, 0 );
461 Debug( LDAP_DEBUG_ANY,
462 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
463 db_strerror(rc), rc, 0 );
467 bdb_cache_release_all (&bdb->bi_cache);
469 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
470 bdb->bi_dbenv = NULL;
473 LDAP_LOG( BACK_BDB, ERR,
474 "bdb_db_destroy: close failed: %s (%d)\n",
475 db_strerror(rc), rc, 0 );
477 Debug( LDAP_DEBUG_ANY,
478 "bdb_db_destroy: close failed: %s (%d)\n",
479 db_strerror(rc), rc, 0 );
485 if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
488 ldap_pvt_thread_rdwr_destroy( &bdb->bi_tree_rdwr );
490 ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
491 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_mutex );
492 ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
493 ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
496 be->be_private = NULL;
501 #ifdef SLAPD_BDB_DYNAMIC
502 int back_bdb_LTX_init_module( int argc, char *argv[] ) {
505 memset( &bi, '\0', sizeof(bi) );
507 bi.bi_init = bdb_initialize;
512 #endif /* SLAPD_BDB_DYNAMIC */
519 static char *controls[] = {
520 LDAP_CONTROL_MANAGEDSAIT,
521 #ifdef LDAP_CONTROL_SUBENTRIES
522 LDAP_CONTROL_SUBENTRIES,
524 #ifdef LDAP_CONTROL_NOOP
527 #ifdef LDAP_CONTROL_VALUESRETURNFILTER
528 LDAP_CONTROL_VALUESRETURNFILTER,
533 bi->bi_controls = controls;
535 /* initialize the underlying database system */
537 LDAP_LOG( BACK_BDB, ENTRY, "bdb_db_initialize\n", 0, 0, 0 );
539 Debug( LDAP_DEBUG_TRACE, "bdb_open: initialize BDB backend\n",
543 { /* version check */
544 int major, minor, patch;
545 char *version = db_version( &major, &minor, &patch );
547 if( major != DB_VERSION_MAJOR ||
548 minor != DB_VERSION_MINOR ||
549 patch < DB_VERSION_PATCH )
552 LDAP_LOG( BACK_BDB, ERR,
553 "bdb_db_initialize: version mismatch: "
554 "\texpected: %s \tgot: %s\n", DB_VERSION_STRING, version, 0 );
556 Debug( LDAP_DEBUG_ANY,
557 "bdb_open: version mismatch\n"
558 "\texpected: " DB_VERSION_STRING "\n"
559 "\tgot: %s \n", version, 0, 0 );
564 LDAP_LOG( BACK_BDB, DETAIL1,
565 "bdb_db_initialize: bdb_open: %s\n", version, 0, 0 );
567 Debug( LDAP_DEBUG_ANY, "bdb_open: %s\n",
573 db_env_set_func_malloc( ch_malloc );
574 db_env_set_func_realloc( ch_realloc );
575 db_env_set_func_free( ch_free );
578 db_env_set_func_yield( ldap_pvt_thread_yield );
581 static char uuidbuf[40];
583 bdb_uuid.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ));
584 bdb_uuid.bv_val = uuidbuf;
592 bi->bi_db_init = bdb_db_init;
593 bi->bi_db_config = bdb_db_config;
594 bi->bi_db_open = bdb_db_open;
595 bi->bi_db_close = bdb_db_close;
596 bi->bi_db_destroy = bdb_db_destroy;
598 bi->bi_op_add = bdb_add;
599 bi->bi_op_bind = bdb_bind;
600 bi->bi_op_compare = bdb_compare;
601 bi->bi_op_delete = bdb_delete;
602 bi->bi_op_modify = bdb_modify;
603 bi->bi_op_modrdn = bdb_modrdn;
604 bi->bi_op_search = bdb_search;
606 bi->bi_op_unbind = 0;
607 bi->bi_op_abandon = 0;
609 bi->bi_extended = bdb_extended;
613 * these routines (and their callers) are not yet designed
614 * to work with transaction. Using them may cause deadlock.
616 bi->bi_acl_group = bdb_group;
617 bi->bi_acl_attribute = bdb_attribute;
619 bi->bi_acl_group = 0;
620 bi->bi_acl_attribute = 0;
623 bi->bi_chk_referrals = bdb_referrals;
624 bi->bi_operational = bdb_operational;
625 bi->bi_entry_release_rw = bdb_entry_release;
628 * hooks for slap tools
630 bi->bi_tool_entry_open = bdb_tool_entry_open;
631 bi->bi_tool_entry_close = bdb_tool_entry_close;
632 bi->bi_tool_entry_first = bdb_tool_entry_next;
633 bi->bi_tool_entry_next = bdb_tool_entry_next;
634 bi->bi_tool_entry_get = bdb_tool_entry_get;
635 bi->bi_tool_entry_put = bdb_tool_entry_put;
636 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
637 bi->bi_tool_sync = 0;
639 bi->bi_connection_init = 0;
640 bi->bi_connection_destroy = 0;