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;
94 bdb->bi_lock_detect = DB_LOCK_DEFAULT;
96 #ifdef LDAP_CLIENT_UPDATE
97 LDAP_LIST_INIT (&bdb->psearch_list);
100 ldap_pvt_thread_mutex_init( &bdb->bi_database_mutex );
101 ldap_pvt_thread_mutex_init( &bdb->bi_lastid_mutex );
102 ldap_pvt_thread_mutex_init( &bdb->bi_cache.lru_mutex );
103 ldap_pvt_thread_rdwr_init ( &bdb->bi_cache.c_rwlock );
105 ldap_pvt_thread_rdwr_init( &bdb->bi_tree_rdwr );
108 be->be_private = bdb;
119 unsigned char *u, *c;
125 #ifdef WORDS_BIGENDIAN
126 for( i = 0; i < sizeof(ID); i++)
128 for( i = sizeof(ID)-1; i >= 0; i--)
138 bdb_db_open( BackendDB *be )
141 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
144 char path[MAXPATHLEN];
148 LDAP_LOG( BACK_BDB, ARGS,
149 "bdb_db_open: %s\n", be->be_suffix[0].bv_val, 0, 0 );
151 Debug( LDAP_DEBUG_ARGS,
153 be->be_suffix[0].bv_val, 0, 0 );
156 /* we should check existance of dbenv_home and db_directory */
158 rc = db_env_create( &bdb->bi_dbenv, 0 );
161 LDAP_LOG( BACK_BDB, ERR,
162 "bdb_db_open: db_env_create failed: %s (%d)\n",
163 db_strerror(rc), rc, 0 );
165 Debug( LDAP_DEBUG_ANY,
166 "bdb_db_open: db_env_create failed: %s (%d)\n",
167 db_strerror(rc), rc, 0 );
172 flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE
173 | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN;
176 /* Never do automatic recovery, must perform it manually.
177 * Otherwise restarting with gentlehup will corrupt the
180 if( !(slapMode & SLAP_TOOL_MODE) ) flags |= DB_RECOVER;
183 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
184 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
185 bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
189 char dir[MAXPATHLEN];
190 size_t len = strlen( bdb->bi_dbenv_home );
192 strcpy( dir, bdb->bi_dbenv_home );
193 strcat( &dir[len], BDB_TMP_SUBDIR );
195 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
198 LDAP_LOG( BACK_BDB, ERR,
199 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
200 dir, db_strerror(rc), rc );
202 Debug( LDAP_DEBUG_ANY,
203 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
204 dir, db_strerror(rc), rc );
209 strcat( &dir[len], BDB_LG_SUBDIR );
211 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
214 LDAP_LOG( BACK_BDB, ERR,
215 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
216 dir, db_strerror(rc), rc );
218 Debug( LDAP_DEBUG_ANY,
219 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
220 dir, db_strerror(rc), rc );
225 strcat( &dir[len], BDB_DATA_SUBDIR );
227 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
230 LDAP_LOG( BACK_BDB, ERR,
231 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
232 dir, db_strerror(rc), rc );
234 Debug( LDAP_DEBUG_ANY,
235 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
236 dir, db_strerror(rc), rc );
244 LDAP_LOG( BACK_BDB, DETAIL1,
245 "bdb_db_open: dbenv_open %s\n", bdb->bi_dbenv_home, 0, 0 );
247 Debug( LDAP_DEBUG_TRACE,
248 "bdb_db_open: dbenv_open(%s)\n",
249 bdb->bi_dbenv_home, 0, 0);
253 strcpy( path, bdb->bi_dbenv_home );
255 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
258 bdb->bi_dbenv_mode );
260 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
263 bdb->bi_dbenv_mode );
267 LDAP_LOG( BACK_BDB, ERR,
268 "bdb_db_open: dbenv_open failed: %s (%d)\n",
269 db_strerror(rc), rc, 0 );
271 Debug( LDAP_DEBUG_ANY,
272 "bdb_db_open: dbenv_open failed: %s (%d)\n",
273 db_strerror(rc), rc, 0 );
278 if( bdb->bi_dbenv_xflags != 0 ) {
279 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
280 bdb->bi_dbenv_xflags, 1);
283 LDAP_LOG( BACK_BDB, ERR,
284 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
285 db_strerror(rc), rc, 0 );
287 Debug( LDAP_DEBUG_ANY,
288 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
289 db_strerror(rc), rc, 0 );
295 flags = DB_THREAD | DB_CREATE | bdb->bi_db_opflags;
297 bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
298 BDB_INDICES * sizeof(struct bdb_db_info *) );
300 /* open (and create) main database */
301 for( i = 0; bdbi_databases[i].name; i++ ) {
302 struct bdb_db_info *db;
304 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
306 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
309 LDAP_LOG( BACK_BDB, ERR,
310 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
311 bdb->bi_dbenv_home, db_strerror(rc), rc );
313 Debug( LDAP_DEBUG_ANY,
314 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
315 bdb->bi_dbenv_home, db_strerror(rc), rc );
320 if( i == BDB_ID2ENTRY ) {
321 rc = db->bdi_db->set_bt_compare( db->bdi_db,
323 rc = db->bdi_db->set_pagesize( db->bdi_db,
324 BDB_ID2ENTRY_PAGESIZE );
327 rc = db->bdi_db->set_bt_compare( db->bdi_db,
330 rc = db->bdi_db->set_flags( db->bdi_db,
331 DB_DUP | DB_DUPSORT );
332 rc = db->bdi_db->set_dup_compare( db->bdi_db,
335 rc = db->bdi_db->set_pagesize( db->bdi_db,
340 strcpy( path, bdbi_databases[i].file );
342 rc = DB_OPEN( db->bdi_db,
344 /* bdbi_databases[i].name, */ NULL,
345 bdbi_databases[i].type,
346 bdbi_databases[i].flags | flags,
347 bdb->bi_dbenv_mode );
349 rc = DB_OPEN( db->bdi_db,
350 bdbi_databases[i].file,
351 /* bdbi_databases[i].name, */ NULL,
352 bdbi_databases[i].type,
353 bdbi_databases[i].flags | flags,
354 bdb->bi_dbenv_mode );
359 LDAP_LOG( BACK_BDB, ERR,
360 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
361 bdb->bi_dbenv_home, db_strerror(rc), rc );
363 Debug( LDAP_DEBUG_ANY,
364 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
365 bdb->bi_dbenv_home, db_strerror(rc), rc );
370 db->bdi_name = bdbi_databases[i].name;
371 bdb->bi_databases[i] = db;
374 bdb->bi_databases[i] = NULL;
375 bdb->bi_ndatabases = i;
378 rc = bdb_last_id( be, NULL );
381 LDAP_LOG( BACK_BDB, ERR,
382 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
383 bdb->bi_dbenv_home, db_strerror(rc), rc );
385 Debug( LDAP_DEBUG_ANY,
386 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
387 bdb->bi_dbenv_home, db_strerror(rc), rc );
392 /* <insert> open (and create) index databases */
394 rc = bdb_build_tree( be );
400 bdb_db_close( BackendDB *be )
403 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
404 struct bdb_db_info *db;
406 while( bdb->bi_ndatabases-- ) {
407 db = bdb->bi_databases[bdb->bi_ndatabases];
408 rc = db->bdi_db->close( db->bdi_db, 0 );
409 /* Lower numbered names are not strdup'd */
410 if( bdb->bi_ndatabases >= BDB_NDB )
411 free( db->bdi_name );
414 free( bdb->bi_databases );
415 bdb_attr_index_destroy( bdb->bi_attrs );
417 bdb_cache_release_all (&bdb->bi_cache);
423 bdb_db_destroy( BackendDB *be )
426 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
428 /* close db environment */
429 if( bdb->bi_dbenv ) {
430 /* force a checkpoint */
431 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
434 LDAP_LOG( BACK_BDB, ERR,
435 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
436 db_strerror(rc), rc, 0 );
438 Debug( LDAP_DEBUG_ANY,
439 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
440 db_strerror(rc), rc, 0 );
444 bdb_cache_release_all (&bdb->bi_cache);
446 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
447 bdb->bi_dbenv = NULL;
450 LDAP_LOG( BACK_BDB, ERR,
451 "bdb_db_destroy: close failed: %s (%d)\n",
452 db_strerror(rc), rc, 0 );
454 Debug( LDAP_DEBUG_ANY,
455 "bdb_db_destroy: close failed: %s (%d)\n",
456 db_strerror(rc), rc, 0 );
462 if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
465 ldap_pvt_thread_rdwr_destroy( &bdb->bi_tree_rdwr );
467 ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
468 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_mutex );
469 ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
470 ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
473 be->be_private = NULL;
478 #ifdef SLAPD_BDB_DYNAMIC
479 int back_bdb_LTX_init_module( int argc, char *argv[] ) {
482 memset( &bi, '\0', sizeof(bi) );
484 bi.bi_init = bdb_initialize;
489 #endif /* SLAPD_BDB_DYNAMIC */
496 static char *controls[] = {
497 LDAP_CONTROL_MANAGEDSAIT,
498 #ifdef LDAP_CONTROL_SUBENTRIES
499 LDAP_CONTROL_SUBENTRIES,
501 #ifdef LDAP_CONTROL_NOOP
504 #ifdef LDAP_CONTROL_VALUESRETURNFILTER
505 LDAP_CONTROL_VALUESRETURNFILTER,
507 #ifdef LDAP_CLIENT_UPDATE
508 LDAP_CONTROL_CLIENT_UPDATE,
513 bi->bi_controls = controls;
515 /* initialize the underlying database system */
517 LDAP_LOG( BACK_BDB, ENTRY, "bdb_db_initialize\n", 0, 0, 0 );
519 Debug( LDAP_DEBUG_TRACE, "bdb_open: initialize BDB backend\n",
523 { /* version check */
524 int major, minor, patch;
525 char *version = db_version( &major, &minor, &patch );
529 /* All our stdio does an ASCII to EBCDIC conversion on
530 * the output. Strings from the BDB library are already
531 * in EBCDIC; we have to go back and forth...
533 strcpy( v2, version );
538 if( major != DB_VERSION_MAJOR ||
539 minor != DB_VERSION_MINOR ||
540 patch < DB_VERSION_PATCH )
543 LDAP_LOG( BACK_BDB, ERR,
544 "bdb_db_initialize: version mismatch: "
545 "\texpected: %s \tgot: %s\n", DB_VERSION_STRING, version, 0 );
547 Debug( LDAP_DEBUG_ANY,
548 "bdb_open: version mismatch\n"
549 "\texpected: " DB_VERSION_STRING "\n"
550 "\tgot: %s \n", version, 0, 0 );
555 LDAP_LOG( BACK_BDB, DETAIL1,
556 "bdb_db_initialize: bdb_open: %s\n", version, 0, 0 );
558 Debug( LDAP_DEBUG_ANY, "bdb_open: %s\n",
563 db_env_set_func_free( ber_memfree );
564 db_env_set_func_malloc( (db_malloc *)ber_memalloc );
565 db_env_set_func_realloc( (db_realloc *)ber_memrealloc );
567 /* This is a no-op on a NO_THREAD build. Leave the default
568 * alone so that BDB will sleep on interprocess conflicts.
570 db_env_set_func_yield( ldap_pvt_thread_yield );
574 static char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
576 bdb_uuid.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ));
577 bdb_uuid.bv_val = uuidbuf;
585 bi->bi_db_init = bdb_db_init;
586 bi->bi_db_config = bdb_db_config;
587 bi->bi_db_open = bdb_db_open;
588 bi->bi_db_close = bdb_db_close;
589 bi->bi_db_destroy = bdb_db_destroy;
591 bi->bi_op_add = bdb_add;
592 bi->bi_op_bind = bdb_bind;
593 bi->bi_op_compare = bdb_compare;
594 bi->bi_op_delete = bdb_delete;
595 bi->bi_op_modify = bdb_modify;
596 bi->bi_op_modrdn = bdb_modrdn;
597 bi->bi_op_search = bdb_search;
599 bi->bi_op_unbind = 0;
600 bi->bi_op_abandon = 0;
602 bi->bi_extended = bdb_extended;
606 * these routines (and their callers) are not yet designed
607 * to work with transaction. Using them may cause deadlock.
609 bi->bi_acl_group = bdb_group;
610 bi->bi_acl_attribute = bdb_attribute;
612 bi->bi_acl_group = 0;
613 bi->bi_acl_attribute = 0;
616 bi->bi_chk_referrals = bdb_referrals;
617 bi->bi_operational = bdb_operational;
618 #ifdef SLAP_X_FILTER_HASSUBORDINATES
619 bi->bi_has_subordinates = bdb_hasSubordinates;
620 #endif /* SLAP_X_FILTER_HASSUBORDINATES */
621 bi->bi_entry_release_rw = bdb_entry_release;
624 * hooks for slap tools
626 bi->bi_tool_entry_open = bdb_tool_entry_open;
627 bi->bi_tool_entry_close = bdb_tool_entry_close;
628 bi->bi_tool_entry_first = bdb_tool_entry_next;
629 bi->bi_tool_entry_next = bdb_tool_entry_next;
630 bi->bi_tool_entry_get = bdb_tool_entry_get;
631 bi->bi_tool_entry_put = bdb_tool_entry_put;
632 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
633 bi->bi_tool_sync = 0;
635 bi->bi_connection_init = 0;
636 bi->bi_connection_destroy = 0;