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>
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 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
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 < (int)sizeof(ID); i++)
131 for( i = sizeof(ID)-1; i >= 0; i--)
142 bdb_db_open( BackendDB *be )
145 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
148 char path[MAXPATHLEN];
152 LDAP_LOG( BACK_BDB, ARGS,
153 "bdb_db_open: %s\n", be->be_suffix[0].bv_val, 0, 0 );
155 Debug( LDAP_DEBUG_ARGS,
157 be->be_suffix[0].bv_val, 0, 0 );
160 #ifndef BDB_MULTIPLE_SUFFIXES
161 if ( be->be_suffix[1].bv_val ) {
163 LDAP_LOG( BACK_BDB, ARGS,
164 "bdb_db_open: only one suffix allowed\n", 0, 0, 0 );
166 Debug( LDAP_DEBUG_ARGS,
167 "bdb_db_open: only one suffix allowed\n", 0, 0, 0 );
172 /* we should check existance of dbenv_home and db_directory */
174 rc = db_env_create( &bdb->bi_dbenv, 0 );
177 LDAP_LOG( BACK_BDB, ERR,
178 "bdb_db_open: db_env_create failed: %s (%d)\n",
179 db_strerror(rc), rc, 0 );
181 Debug( LDAP_DEBUG_ANY,
182 "bdb_db_open: db_env_create failed: %s (%d)\n",
183 db_strerror(rc), rc, 0 );
188 flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE
189 | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN;
192 /* Never do automatic recovery, must perform it manually.
193 * Otherwise restarting with gentlehup will corrupt the
196 if( !(slapMode & SLAP_TOOL_MODE) ) flags |= DB_RECOVER;
199 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
200 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
201 bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
203 #ifdef SLAP_IDL_CACHE
204 if ( bdb->bi_idl_cache_max_size ) {
205 ldap_pvt_thread_mutex_init( &bdb->bi_idl_tree_mutex );
206 bdb->bi_idl_cache_size = 0;
212 char dir[MAXPATHLEN];
213 size_t len = strlen( bdb->bi_dbenv_home );
215 strcpy( dir, bdb->bi_dbenv_home );
216 strcat( &dir[len], BDB_TMP_SUBDIR );
218 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
221 LDAP_LOG( BACK_BDB, ERR,
222 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
223 dir, db_strerror(rc), rc );
225 Debug( LDAP_DEBUG_ANY,
226 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
227 dir, db_strerror(rc), rc );
232 strcat( &dir[len], BDB_LG_SUBDIR );
234 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
237 LDAP_LOG( BACK_BDB, ERR,
238 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
239 dir, db_strerror(rc), rc );
241 Debug( LDAP_DEBUG_ANY,
242 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
243 dir, db_strerror(rc), rc );
248 strcat( &dir[len], BDB_DATA_SUBDIR );
250 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
253 LDAP_LOG( BACK_BDB, ERR,
254 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
255 dir, db_strerror(rc), rc );
257 Debug( LDAP_DEBUG_ANY,
258 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
259 dir, db_strerror(rc), rc );
267 LDAP_LOG( BACK_BDB, DETAIL1,
268 "bdb_db_open: dbenv_open %s\n", bdb->bi_dbenv_home, 0, 0 );
270 Debug( LDAP_DEBUG_TRACE,
271 "bdb_db_open: dbenv_open(%s)\n",
272 bdb->bi_dbenv_home, 0, 0);
276 strcpy( path, bdb->bi_dbenv_home );
278 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
281 bdb->bi_dbenv_mode );
283 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
286 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,
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,
363 strcpy( path, bdbi_databases[i].file );
365 rc = DB_OPEN( db->bdi_db,
367 /* bdbi_databases[i].name, */ NULL,
368 bdbi_databases[i].type,
369 bdbi_databases[i].flags | flags,
370 bdb->bi_dbenv_mode );
372 rc = DB_OPEN( db->bdi_db,
373 bdbi_databases[i].file,
374 /* bdbi_databases[i].name, */ NULL,
375 bdbi_databases[i].type,
376 bdbi_databases[i].flags | flags,
377 bdb->bi_dbenv_mode );
382 LDAP_LOG( BACK_BDB, ERR,
383 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
384 bdb->bi_dbenv_home, db_strerror(rc), rc );
386 Debug( LDAP_DEBUG_ANY,
387 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
388 bdb->bi_dbenv_home, db_strerror(rc), rc );
393 db->bdi_name = bdbi_databases[i].name;
394 bdb->bi_databases[i] = db;
397 bdb->bi_databases[i] = NULL;
398 bdb->bi_ndatabases = i;
401 rc = bdb_last_id( be, NULL );
404 LDAP_LOG( BACK_BDB, ERR,
405 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
406 bdb->bi_dbenv_home, db_strerror(rc), rc );
408 Debug( LDAP_DEBUG_ANY,
409 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
410 bdb->bi_dbenv_home, db_strerror(rc), rc );
415 /* <insert> open (and create) index databases */
417 rc = bdb_build_tree( be );
423 bdb_db_close( BackendDB *be )
426 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
427 struct bdb_db_info *db;
428 #ifdef SLAP_IDL_CACHE
429 bdb_idl_cache_entry_t *entry, *next_entry;
432 while( bdb->bi_ndatabases-- ) {
433 db = bdb->bi_databases[bdb->bi_ndatabases];
434 rc = db->bdi_db->close( db->bdi_db, 0 );
435 /* Lower numbered names are not strdup'd */
436 if( bdb->bi_ndatabases >= BDB_NDB )
437 free( db->bdi_name );
440 free( bdb->bi_databases );
441 bdb_attr_index_destroy( bdb->bi_attrs );
443 bdb_cache_release_all (&bdb->bi_cache);
445 #ifdef SLAP_IDL_CACHE
446 ldap_pvt_thread_mutex_lock ( &bdb->bi_idl_tree_mutex );
447 entry = bdb->bi_idl_lru_head;
448 while ( entry != NULL ) {
449 next_entry = entry->idl_lru_next;
451 free( entry->kstr.bv_val );
455 ldap_pvt_thread_mutex_unlock ( &bdb->bi_idl_tree_mutex );
462 bdb_db_destroy( BackendDB *be )
465 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
467 /* close db environment */
468 if( bdb->bi_dbenv ) {
469 /* force a checkpoint */
470 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
473 LDAP_LOG( BACK_BDB, ERR,
474 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
475 db_strerror(rc), rc, 0 );
477 Debug( LDAP_DEBUG_ANY,
478 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
479 db_strerror(rc), rc, 0 );
483 bdb_cache_release_all (&bdb->bi_cache);
485 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
486 bdb->bi_dbenv = NULL;
489 LDAP_LOG( BACK_BDB, ERR,
490 "bdb_db_destroy: close failed: %s (%d)\n",
491 db_strerror(rc), rc, 0 );
493 Debug( LDAP_DEBUG_ANY,
494 "bdb_db_destroy: close failed: %s (%d)\n",
495 db_strerror(rc), rc, 0 );
501 if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
504 ldap_pvt_thread_rdwr_destroy( &bdb->bi_tree_rdwr );
506 ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
507 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_mutex );
508 ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
509 ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
512 be->be_private = NULL;
517 #ifdef SLAPD_BDB_DYNAMIC
518 int back_bdb_LTX_init_module( int argc, char *argv[] ) {
521 memset( &bi, '\0', sizeof(bi) );
523 bi.bi_init = bdb_initialize;
528 #endif /* SLAPD_BDB_DYNAMIC */
535 static char *controls[] = {
536 LDAP_CONTROL_MANAGEDSAIT,
538 #ifdef LDAP_CONTROL_PAGEDRESULTS
539 LDAP_CONTROL_PAGEDRESULTS,
541 LDAP_CONTROL_VALUESRETURNFILTER,
542 #ifdef LDAP_CONTROL_SUBENTRIES
543 LDAP_CONTROL_SUBENTRIES,
545 #ifdef LDAP_CLIENT_UPDATE
546 LDAP_CONTROL_CLIENT_UPDATE,
551 bi->bi_controls = controls;
553 /* initialize the underlying database system */
555 LDAP_LOG( BACK_BDB, ENTRY, "bdb_db_initialize\n", 0, 0, 0 );
557 Debug( LDAP_DEBUG_TRACE, "bdb_initialize: initialize BDB backend\n",
561 { /* version check */
562 int major, minor, patch;
563 char *version = db_version( &major, &minor, &patch );
567 /* All our stdio does an ASCII to EBCDIC conversion on
568 * the output. Strings from the BDB library are already
569 * in EBCDIC; we have to go back and forth...
571 strcpy( v2, version );
576 if( major != DB_VERSION_MAJOR ||
577 minor != DB_VERSION_MINOR ||
578 patch < DB_VERSION_PATCH )
581 LDAP_LOG( BACK_BDB, ERR,
582 "bdb_db_initialize: version mismatch: "
583 "\texpected: %s \tgot: %s\n", DB_VERSION_STRING, version, 0 );
585 Debug( LDAP_DEBUG_ANY,
586 "bdb_initialize: version mismatch\n"
587 "\texpected: " DB_VERSION_STRING "\n"
588 "\tgot: %s \n", version, 0, 0 );
593 LDAP_LOG( BACK_BDB, DETAIL1,
594 "bdb_db_initialize: %s\n", version, 0, 0 );
596 Debug( LDAP_DEBUG_ANY, "bdb_initialize: %s\n",
601 db_env_set_func_free( ber_memfree );
602 db_env_set_func_malloc( (db_malloc *)ber_memalloc );
603 db_env_set_func_realloc( (db_realloc *)ber_memrealloc );
605 /* This is a no-op on a NO_THREAD build. Leave the default
606 * alone so that BDB will sleep on interprocess conflicts.
608 db_env_set_func_yield( ldap_pvt_thread_yield );
612 static char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
614 bdb_uuid.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ));
615 bdb_uuid.bv_val = uuidbuf;
623 bi->bi_db_init = bdb_db_init;
624 bi->bi_db_config = bdb_db_config;
625 bi->bi_db_open = bdb_db_open;
626 bi->bi_db_close = bdb_db_close;
627 bi->bi_db_destroy = bdb_db_destroy;
629 bi->bi_op_add = bdb_add;
630 bi->bi_op_bind = bdb_bind;
631 bi->bi_op_compare = bdb_compare;
632 bi->bi_op_delete = bdb_delete;
633 bi->bi_op_modify = bdb_modify;
634 bi->bi_op_modrdn = bdb_modrdn;
635 bi->bi_op_search = bdb_search;
637 bi->bi_op_unbind = 0;
638 bi->bi_op_abandon = 0;
640 bi->bi_extended = bdb_extended;
644 * these routines (and their callers) are not yet designed
645 * to work with transaction. Using them may cause deadlock.
647 bi->bi_acl_group = bdb_group;
648 bi->bi_acl_attribute = bdb_attribute;
650 bi->bi_acl_group = 0;
651 bi->bi_acl_attribute = 0;
654 bi->bi_chk_referrals = bdb_referrals;
655 bi->bi_operational = bdb_operational;
656 bi->bi_has_subordinates = bdb_hasSubordinates;
657 bi->bi_entry_release_rw = bdb_entry_release;
660 * hooks for slap tools
662 bi->bi_tool_entry_open = bdb_tool_entry_open;
663 bi->bi_tool_entry_close = bdb_tool_entry_close;
664 bi->bi_tool_entry_first = bdb_tool_entry_next;
665 bi->bi_tool_entry_next = bdb_tool_entry_next;
666 bi->bi_tool_entry_get = bdb_tool_entry_get;
667 bi->bi_tool_entry_put = bdb_tool_entry_put;
668 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
669 bi->bi_tool_sync = 0;
671 bi->bi_connection_init = 0;
672 bi->bi_connection_destroy = 0;