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;
208 /* Only slapd will do recovery, slap-tools won't. Otherwise
209 * running slapcat while slapd is active corrupts the db.
211 if( !(slapMode & SLAP_TOOL_MODE) ) flags |= DB_RECOVER;
213 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
214 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
216 bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
221 char dir[MAXPATHLEN];
222 size_t len = strlen( bdb->bi_dbenv_home );
224 strcpy( dir, bdb->bi_dbenv_home );
225 strcat( &dir[len], BDB_TMP_SUBDIR );
227 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
230 LDAP_LOG( BACK_BDB, ERR,
231 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
232 dir, db_strerror(rc), rc );
234 Debug( LDAP_DEBUG_ANY,
235 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
236 dir, db_strerror(rc), rc );
241 strcat( &dir[len], BDB_LG_SUBDIR );
243 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
246 LDAP_LOG( BACK_BDB, ERR,
247 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
248 dir, db_strerror(rc), rc );
250 Debug( LDAP_DEBUG_ANY,
251 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
252 dir, db_strerror(rc), rc );
257 strcat( &dir[len], BDB_DATA_SUBDIR );
259 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
262 LDAP_LOG( BACK_BDB, ERR,
263 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
264 dir, db_strerror(rc), rc );
266 Debug( LDAP_DEBUG_ANY,
267 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
268 dir, db_strerror(rc), rc );
276 LDAP_LOG( BACK_BDB, DETAIL1,
277 "bdb_db_open: dbenv_open %s\n", bdb->bi_dbenv_home, 0, 0 );
279 Debug( LDAP_DEBUG_TRACE,
280 "bdb_db_open: dbenv_open(%s)\n",
281 bdb->bi_dbenv_home, 0, 0);
284 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
287 bdb->bi_dbenv_mode );
290 LDAP_LOG( BACK_BDB, ERR,
291 "bdb_db_open: dbenv_open failed: %s (%d)\n",
292 db_strerror(rc), rc, 0 );
294 Debug( LDAP_DEBUG_ANY,
295 "bdb_db_open: dbenv_open failed: %s (%d)\n",
296 db_strerror(rc), rc, 0 );
301 if( bdb->bi_dbenv_xflags != 0 ) {
302 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
303 bdb->bi_dbenv_xflags, 1);
306 LDAP_LOG( BACK_BDB, ERR,
307 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
308 db_strerror(rc), rc, 0 );
310 Debug( LDAP_DEBUG_ANY,
311 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
312 db_strerror(rc), rc, 0 );
318 flags = DB_THREAD | DB_CREATE | bdb->bi_db_opflags;
320 bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
321 BDB_INDICES * sizeof(struct bdb_db_info *) );
323 /* open (and create) main database */
324 for( i = 0; bdbi_databases[i].name; i++ ) {
325 struct bdb_db_info *db;
327 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
329 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
332 LDAP_LOG( BACK_BDB, ERR,
333 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
334 bdb->bi_dbenv_home, db_strerror(rc), rc );
336 Debug( LDAP_DEBUG_ANY,
337 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
338 bdb->bi_dbenv_home, db_strerror(rc), rc );
343 if( i == BDB_ID2ENTRY ) {
344 rc = db->bdi_db->set_bt_compare( db->bdi_db,
346 rc = db->bdi_db->set_pagesize( db->bdi_db,
347 BDB_ID2ENTRY_PAGESIZE );
350 rc = db->bdi_db->set_bt_compare( db->bdi_db,
352 #elif defined(BDB_IDL_MULTI)
353 rc = db->bdi_db->set_flags( db->bdi_db,
354 DB_DUP | DB_DUPSORT );
355 rc = db->bdi_db->set_dup_compare( db->bdi_db,
358 rc = db->bdi_db->set_pagesize( db->bdi_db,
362 rc = db->bdi_db->open( db->bdi_db,
363 bdbi_databases[i].file,
364 /* bdbi_databases[i].name, */ NULL,
365 bdbi_databases[i].type,
366 bdbi_databases[i].flags | flags,
367 bdb->bi_dbenv_mode );
371 LDAP_LOG( BACK_BDB, ERR,
372 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
373 bdb->bi_dbenv_home, db_strerror(rc), rc );
375 Debug( LDAP_DEBUG_ANY,
376 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
377 bdb->bi_dbenv_home, db_strerror(rc), rc );
382 db->bdi_name = bdbi_databases[i].name;
383 bdb->bi_databases[i] = db;
386 bdb->bi_databases[i] = NULL;
387 bdb->bi_ndatabases = i;
390 rc = bdb_last_id( be, NULL );
393 LDAP_LOG( BACK_BDB, ERR,
394 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
395 bdb->bi_dbenv_home, db_strerror(rc), rc );
397 Debug( LDAP_DEBUG_ANY,
398 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
399 bdb->bi_dbenv_home, db_strerror(rc), rc );
404 /* <insert> open (and create) index databases */
406 rc = bdb_build_tree( be );
409 #if 0 /* ifndef NO_THREADS */
410 if( bdb->bi_lock_detect != DB_LOCK_NORUN ) {
411 /* listener as a separate THREAD */
412 rc = ldap_pvt_thread_create( &bdb->bi_lock_detect_tid,
413 1, lock_detect_task, bdb );
420 bdb_db_close( BackendDB *be )
423 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
424 struct bdb_db_info *db;
426 while( bdb->bi_ndatabases-- ) {
427 db = bdb->bi_databases[bdb->bi_ndatabases];
428 rc = db->bdi_db->close( db->bdi_db, 0 );
429 /* Lower numbered names are not strdup'd */
430 if( bdb->bi_ndatabases >= BDB_NDB )
431 free( db->bdi_name );
434 free( bdb->bi_databases );
435 bdb_attr_index_destroy( bdb->bi_attrs );
437 bdb_cache_release_all (&bdb->bi_cache);
443 bdb_db_destroy( BackendDB *be )
446 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
448 /* close db environment */
449 if( bdb->bi_dbenv ) {
450 /* force a checkpoint */
451 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
454 LDAP_LOG( BACK_BDB, ERR,
455 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
456 db_strerror(rc), rc, 0 );
458 Debug( LDAP_DEBUG_ANY,
459 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
460 db_strerror(rc), rc, 0 );
464 bdb_cache_release_all (&bdb->bi_cache);
466 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
467 bdb->bi_dbenv = NULL;
470 LDAP_LOG( BACK_BDB, ERR,
471 "bdb_db_destroy: close failed: %s (%d)\n",
472 db_strerror(rc), rc, 0 );
474 Debug( LDAP_DEBUG_ANY,
475 "bdb_db_destroy: close failed: %s (%d)\n",
476 db_strerror(rc), rc, 0 );
483 ldap_pvt_thread_rdwr_destroy( &bdb->bi_tree_rdwr );
485 ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
486 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_mutex );
487 ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
488 ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
493 #ifdef SLAPD_BDB_DYNAMIC
494 int back_bdb_LTX_init_module( int argc, char *argv[] ) {
497 memset( &bi, '\0', sizeof(bi) );
499 bi.bi_init = bdb_initialize;
504 #endif /* SLAPD_BDB_DYNAMIC */
511 static char *controls[] = {
512 LDAP_CONTROL_MANAGEDSAIT,
513 #ifdef LDAP_CONTROL_SUBENTRIES
514 LDAP_CONTROL_SUBENTRIES,
516 #ifdef LDAP_CONTROL_NOOP
519 #ifdef LDAP_CONTROL_VALUESRETURNFILTER
520 LDAP_CONTROL_VALUESRETURNFILTER,
525 bi->bi_controls = controls;
527 /* initialize the underlying database system */
529 LDAP_LOG( BACK_BDB, ENTRY, "bdb_db_initialize\n", 0, 0, 0 );
531 Debug( LDAP_DEBUG_TRACE, "bdb_open: initialize BDB backend\n",
535 { /* version check */
536 int major, minor, patch;
537 char *version = db_version( &major, &minor, &patch );
539 if( major != DB_VERSION_MAJOR ||
540 minor != DB_VERSION_MINOR ||
541 patch < DB_VERSION_PATCH )
544 LDAP_LOG( BACK_BDB, ERR,
545 "bdb_db_initialize: version mismatch: "
546 "\texpected: %s \tgot: %s\n", DB_VERSION_STRING, version, 0 );
548 Debug( LDAP_DEBUG_ANY,
549 "bdb_open: version mismatch\n"
550 "\texpected: " DB_VERSION_STRING "\n"
551 "\tgot: %s \n", version, 0, 0 );
556 LDAP_LOG( BACK_BDB, DETAIL1,
557 "bdb_db_initialize: bdb_open: %s\n", version, 0, 0 );
559 Debug( LDAP_DEBUG_ANY, "bdb_open: %s\n",
565 db_env_set_func_malloc( ch_malloc );
566 db_env_set_func_realloc( ch_realloc );
567 db_env_set_func_free( ch_free );
570 db_env_set_func_yield( ldap_pvt_thread_yield );
573 static char uuidbuf[40];
575 bdb_uuid.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ));
576 bdb_uuid.bv_val = uuidbuf;
584 bi->bi_db_init = bdb_db_init;
585 bi->bi_db_config = bdb_db_config;
586 bi->bi_db_open = bdb_db_open;
587 bi->bi_db_close = bdb_db_close;
588 bi->bi_db_destroy = bdb_db_destroy;
590 bi->bi_op_add = bdb_add;
591 bi->bi_op_bind = bdb_bind;
592 bi->bi_op_compare = bdb_compare;
593 bi->bi_op_delete = bdb_delete;
594 bi->bi_op_modify = bdb_modify;
595 bi->bi_op_modrdn = bdb_modrdn;
596 bi->bi_op_search = bdb_search;
598 bi->bi_op_unbind = 0;
599 bi->bi_op_abandon = 0;
601 bi->bi_extended = bdb_extended;
605 * these routines (and their callers) are not yet designed
606 * to work with transaction. Using them may cause deadlock.
608 bi->bi_acl_group = bdb_group;
609 bi->bi_acl_attribute = bdb_attribute;
611 bi->bi_acl_group = 0;
612 bi->bi_acl_attribute = 0;
615 bi->bi_chk_referrals = bdb_referrals;
616 bi->bi_operational = bdb_operational;
617 bi->bi_entry_release_rw = bdb_entry_release;
620 * hooks for slap tools
622 bi->bi_tool_entry_open = bdb_tool_entry_open;
623 bi->bi_tool_entry_close = bdb_tool_entry_close;
624 bi->bi_tool_entry_first = bdb_tool_entry_next;
625 bi->bi_tool_entry_next = bdb_tool_entry_next;
626 bi->bi_tool_entry_get = bdb_tool_entry_get;
627 bi->bi_tool_entry_put = bdb_tool_entry_put;
628 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
629 bi->bi_tool_sync = 0;
631 bi->bi_connection_init = 0;
632 bi->bi_connection_destroy = 0;