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);
42 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;
94 bdb->bi_lock_detect = DB_LOCK_DEFAULT;
95 bdb->bi_search_stack_depth = DEFAULT_SEARCH_STACK_DEPTH;
96 bdb->bi_search_stack = NULL;
98 #ifdef LDAP_CLIENT_UPDATE
99 LDAP_LIST_INIT (&bdb->psearch_list);
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;
122 unsigned char *u, *c;
128 #ifdef WORDS_BIGENDIAN
129 for( i = 0; i < sizeof(ID); i++)
131 for( i = sizeof(ID)-1; i >= 0; i--)
141 bdb_db_open( BackendDB *be )
144 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
147 char path[MAXPATHLEN];
151 LDAP_LOG( BACK_BDB, ARGS,
152 "bdb_db_open: %s\n", be->be_suffix[0].bv_val, 0, 0 );
154 Debug( LDAP_DEBUG_ARGS,
156 be->be_suffix[0].bv_val, 0, 0 );
159 /* we should check existance of dbenv_home and db_directory */
161 rc = db_env_create( &bdb->bi_dbenv, 0 );
164 LDAP_LOG( BACK_BDB, ERR,
165 "bdb_db_open: db_env_create failed: %s (%d)\n",
166 db_strerror(rc), rc, 0 );
168 Debug( LDAP_DEBUG_ANY,
169 "bdb_db_open: db_env_create failed: %s (%d)\n",
170 db_strerror(rc), rc, 0 );
175 flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE
176 | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN;
179 /* Never do automatic recovery, must perform it manually.
180 * Otherwise restarting with gentlehup will corrupt the
183 if( !(slapMode & SLAP_TOOL_MODE) ) flags |= DB_RECOVER;
186 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
187 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
188 bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
190 #ifdef SLAP_IDL_CACHE
191 if ( bdb->bi_idl_cache_max_size ) {
192 ldap_pvt_thread_mutex_init( &bdb->bi_idl_tree_mutex );
193 bdb->bi_idl_cache_size = 0;
199 char dir[MAXPATHLEN];
200 size_t len = strlen( bdb->bi_dbenv_home );
202 strcpy( dir, bdb->bi_dbenv_home );
203 strcat( &dir[len], BDB_TMP_SUBDIR );
205 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
208 LDAP_LOG( BACK_BDB, ERR,
209 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
210 dir, db_strerror(rc), rc );
212 Debug( LDAP_DEBUG_ANY,
213 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
214 dir, db_strerror(rc), rc );
219 strcat( &dir[len], BDB_LG_SUBDIR );
221 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
224 LDAP_LOG( BACK_BDB, ERR,
225 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
226 dir, db_strerror(rc), rc );
228 Debug( LDAP_DEBUG_ANY,
229 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
230 dir, db_strerror(rc), rc );
235 strcat( &dir[len], BDB_DATA_SUBDIR );
237 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
240 LDAP_LOG( BACK_BDB, ERR,
241 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
242 dir, db_strerror(rc), rc );
244 Debug( LDAP_DEBUG_ANY,
245 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
246 dir, db_strerror(rc), rc );
254 LDAP_LOG( BACK_BDB, DETAIL1,
255 "bdb_db_open: dbenv_open %s\n", bdb->bi_dbenv_home, 0, 0 );
257 Debug( LDAP_DEBUG_TRACE,
258 "bdb_db_open: dbenv_open(%s)\n",
259 bdb->bi_dbenv_home, 0, 0);
263 strcpy( path, bdb->bi_dbenv_home );
265 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
268 bdb->bi_dbenv_mode );
270 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
273 bdb->bi_dbenv_mode );
277 LDAP_LOG( BACK_BDB, ERR,
278 "bdb_db_open: dbenv_open failed: %s (%d)\n",
279 db_strerror(rc), rc, 0 );
281 Debug( LDAP_DEBUG_ANY,
282 "bdb_db_open: dbenv_open failed: %s (%d)\n",
283 db_strerror(rc), rc, 0 );
288 if( bdb->bi_dbenv_xflags != 0 ) {
289 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
290 bdb->bi_dbenv_xflags, 1);
293 LDAP_LOG( BACK_BDB, ERR,
294 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
295 db_strerror(rc), rc, 0 );
297 Debug( LDAP_DEBUG_ANY,
298 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
299 db_strerror(rc), rc, 0 );
305 flags = DB_THREAD | DB_CREATE | bdb->bi_db_opflags;
307 bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
308 BDB_INDICES * sizeof(struct bdb_db_info *) );
310 /* open (and create) main database */
311 for( i = 0; bdbi_databases[i].name; i++ ) {
312 struct bdb_db_info *db;
314 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
316 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
319 LDAP_LOG( BACK_BDB, ERR,
320 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
321 bdb->bi_dbenv_home, db_strerror(rc), rc );
323 Debug( LDAP_DEBUG_ANY,
324 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
325 bdb->bi_dbenv_home, db_strerror(rc), rc );
330 if( i == BDB_ID2ENTRY ) {
331 rc = db->bdi_db->set_bt_compare( db->bdi_db,
333 rc = db->bdi_db->set_pagesize( db->bdi_db,
334 BDB_ID2ENTRY_PAGESIZE );
337 rc = db->bdi_db->set_bt_compare( db->bdi_db,
340 rc = db->bdi_db->set_flags( db->bdi_db,
341 DB_DUP | DB_DUPSORT );
342 rc = db->bdi_db->set_dup_compare( db->bdi_db,
345 rc = db->bdi_db->set_pagesize( db->bdi_db,
350 strcpy( path, bdbi_databases[i].file );
352 rc = DB_OPEN( db->bdi_db,
354 /* bdbi_databases[i].name, */ NULL,
355 bdbi_databases[i].type,
356 bdbi_databases[i].flags | flags,
357 bdb->bi_dbenv_mode );
359 rc = DB_OPEN( db->bdi_db,
360 bdbi_databases[i].file,
361 /* bdbi_databases[i].name, */ NULL,
362 bdbi_databases[i].type,
363 bdbi_databases[i].flags | flags,
364 bdb->bi_dbenv_mode );
369 LDAP_LOG( BACK_BDB, ERR,
370 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
371 bdb->bi_dbenv_home, db_strerror(rc), rc );
373 Debug( LDAP_DEBUG_ANY,
374 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
375 bdb->bi_dbenv_home, db_strerror(rc), rc );
380 db->bdi_name = bdbi_databases[i].name;
381 bdb->bi_databases[i] = db;
384 bdb->bi_databases[i] = NULL;
385 bdb->bi_ndatabases = i;
388 rc = bdb_last_id( be, NULL );
391 LDAP_LOG( BACK_BDB, ERR,
392 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
393 bdb->bi_dbenv_home, db_strerror(rc), rc );
395 Debug( LDAP_DEBUG_ANY,
396 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
397 bdb->bi_dbenv_home, db_strerror(rc), rc );
402 /* <insert> open (and create) index databases */
404 rc = bdb_build_tree( be );
410 bdb_db_close( BackendDB *be )
413 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
414 struct bdb_db_info *db;
415 #ifdef SLAP_IDL_CACHE
416 bdb_idl_cache_entry_t *entry, *next_entry;
419 while( bdb->bi_ndatabases-- ) {
420 db = bdb->bi_databases[bdb->bi_ndatabases];
421 rc = db->bdi_db->close( db->bdi_db, 0 );
422 /* Lower numbered names are not strdup'd */
423 if( bdb->bi_ndatabases >= BDB_NDB )
424 free( db->bdi_name );
427 free( bdb->bi_databases );
428 bdb_attr_index_destroy( bdb->bi_attrs );
430 bdb_cache_release_all (&bdb->bi_cache);
432 #ifdef SLAP_IDL_CACHE
433 ldap_pvt_thread_mutex_lock ( &bdb->bi_idl_tree_mutex );
434 entry = bdb->bi_idl_lru_head;
435 while ( entry != NULL ) {
436 next_entry = entry->idl_lru_next;
438 free( entry->kstr.bv_val );
442 ldap_pvt_thread_mutex_unlock ( &bdb->bi_idl_tree_mutex );
449 bdb_db_destroy( BackendDB *be )
452 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
454 /* close db environment */
455 if( bdb->bi_dbenv ) {
456 /* force a checkpoint */
457 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
460 LDAP_LOG( BACK_BDB, ERR,
461 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
462 db_strerror(rc), rc, 0 );
464 Debug( LDAP_DEBUG_ANY,
465 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
466 db_strerror(rc), rc, 0 );
470 bdb_cache_release_all (&bdb->bi_cache);
472 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
473 bdb->bi_dbenv = NULL;
476 LDAP_LOG( BACK_BDB, ERR,
477 "bdb_db_destroy: close failed: %s (%d)\n",
478 db_strerror(rc), rc, 0 );
480 Debug( LDAP_DEBUG_ANY,
481 "bdb_db_destroy: close failed: %s (%d)\n",
482 db_strerror(rc), rc, 0 );
488 if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
491 ldap_pvt_thread_rdwr_destroy( &bdb->bi_tree_rdwr );
493 ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
494 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_mutex );
495 ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
496 ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
499 be->be_private = NULL;
504 #ifdef SLAPD_BDB_DYNAMIC
505 int back_bdb_LTX_init_module( int argc, char *argv[] ) {
508 memset( &bi, '\0', sizeof(bi) );
510 bi.bi_init = bdb_initialize;
515 #endif /* SLAPD_BDB_DYNAMIC */
522 static char *controls[] = {
523 LDAP_CONTROL_MANAGEDSAIT,
525 LDAP_CONTROL_PAGEDRESULTS,
526 LDAP_CONTROL_VALUESRETURNFILTER,
527 #ifdef LDAP_CONTROL_SUBENTRIES
528 LDAP_CONTROL_SUBENTRIES,
530 #ifdef LDAP_CLIENT_UPDATE
531 LDAP_CONTROL_CLIENT_UPDATE,
536 bi->bi_controls = controls;
538 /* initialize the underlying database system */
540 LDAP_LOG( BACK_BDB, ENTRY, "bdb_db_initialize\n", 0, 0, 0 );
542 Debug( LDAP_DEBUG_TRACE, "bdb_initialize: initialize BDB backend\n",
546 { /* version check */
547 int major, minor, patch;
548 char *version = db_version( &major, &minor, &patch );
552 /* All our stdio does an ASCII to EBCDIC conversion on
553 * the output. Strings from the BDB library are already
554 * in EBCDIC; we have to go back and forth...
556 strcpy( v2, version );
561 if( major != DB_VERSION_MAJOR ||
562 minor != DB_VERSION_MINOR ||
563 patch < DB_VERSION_PATCH )
566 LDAP_LOG( BACK_BDB, ERR,
567 "bdb_db_initialize: version mismatch: "
568 "\texpected: %s \tgot: %s\n", DB_VERSION_STRING, version, 0 );
570 Debug( LDAP_DEBUG_ANY,
571 "bdb_initialize: version mismatch\n"
572 "\texpected: " DB_VERSION_STRING "\n"
573 "\tgot: %s \n", version, 0, 0 );
578 LDAP_LOG( BACK_BDB, DETAIL1,
579 "bdb_db_initialize: %s\n", version, 0, 0 );
581 Debug( LDAP_DEBUG_ANY, "bdb_initialize: %s\n",
586 db_env_set_func_free( ber_memfree );
587 db_env_set_func_malloc( (db_malloc *)ber_memalloc );
588 db_env_set_func_realloc( (db_realloc *)ber_memrealloc );
590 /* This is a no-op on a NO_THREAD build. Leave the default
591 * alone so that BDB will sleep on interprocess conflicts.
593 db_env_set_func_yield( ldap_pvt_thread_yield );
597 static char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
599 bdb_uuid.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ));
600 bdb_uuid.bv_val = uuidbuf;
608 bi->bi_db_init = bdb_db_init;
609 bi->bi_db_config = bdb_db_config;
610 bi->bi_db_open = bdb_db_open;
611 bi->bi_db_close = bdb_db_close;
612 bi->bi_db_destroy = bdb_db_destroy;
614 bi->bi_op_add = bdb_add;
615 bi->bi_op_bind = bdb_bind;
616 bi->bi_op_compare = bdb_compare;
617 bi->bi_op_delete = bdb_delete;
618 bi->bi_op_modify = bdb_modify;
619 bi->bi_op_modrdn = bdb_modrdn;
620 bi->bi_op_search = bdb_search;
622 bi->bi_op_unbind = 0;
623 bi->bi_op_abandon = 0;
625 bi->bi_extended = bdb_extended;
629 * these routines (and their callers) are not yet designed
630 * to work with transaction. Using them may cause deadlock.
632 bi->bi_acl_group = bdb_group;
633 bi->bi_acl_attribute = bdb_attribute;
635 bi->bi_acl_group = 0;
636 bi->bi_acl_attribute = 0;
639 bi->bi_chk_referrals = bdb_referrals;
640 bi->bi_operational = bdb_operational;
641 bi->bi_has_subordinates = bdb_hasSubordinates;
642 bi->bi_entry_release_rw = bdb_entry_release;
645 * hooks for slap tools
647 bi->bi_tool_entry_open = bdb_tool_entry_open;
648 bi->bi_tool_entry_close = bdb_tool_entry_close;
649 bi->bi_tool_entry_first = bdb_tool_entry_next;
650 bi->bi_tool_entry_next = bdb_tool_entry_next;
651 bi->bi_tool_entry_get = bdb_tool_entry_get;
652 bi->bi_tool_entry_put = bdb_tool_entry_put;
653 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
654 bi->bi_tool_sync = 0;
656 bi->bi_connection_init = 0;
657 bi->bi_connection_destroy = 0;