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 ldap_pvt_thread_mutex_init( &bdb->bi_database_mutex );
97 ldap_pvt_thread_mutex_init( &bdb->bi_lastid_mutex );
98 ldap_pvt_thread_mutex_init( &bdb->bi_cache.lru_mutex );
99 ldap_pvt_thread_rdwr_init ( &bdb->bi_cache.c_rwlock );
101 ldap_pvt_thread_rdwr_init( &bdb->bi_tree_rdwr );
104 be->be_private = bdb;
115 unsigned char *u, *c;
121 #ifdef WORDS_BIGENDIAN
122 for( i = 0; i < sizeof(ID); i++)
124 for( i = sizeof(ID)-1; i >= 0; i--)
134 bdb_db_open( BackendDB *be )
137 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
140 char path[MAXPATHLEN];
144 LDAP_LOG( BACK_BDB, ARGS,
145 "bdb_db_open: %s\n", be->be_suffix[0].bv_val, 0, 0 );
147 Debug( LDAP_DEBUG_ARGS,
149 be->be_suffix[0].bv_val, 0, 0 );
152 /* we should check existance of dbenv_home and db_directory */
154 rc = db_env_create( &bdb->bi_dbenv, 0 );
157 LDAP_LOG( BACK_BDB, ERR,
158 "bdb_db_open: db_env_create failed: %s (%d)\n",
159 db_strerror(rc), rc, 0 );
161 Debug( LDAP_DEBUG_ANY,
162 "bdb_db_open: db_env_create failed: %s (%d)\n",
163 db_strerror(rc), rc, 0 );
168 flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE
169 | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN;
172 /* Never do automatic recovery, must perform it manually.
173 * Otherwise restarting with gentlehup will corrupt the
176 if( !(slapMode & SLAP_TOOL_MODE) ) flags |= DB_RECOVER;
179 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
180 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
181 bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
185 char dir[MAXPATHLEN];
186 size_t len = strlen( bdb->bi_dbenv_home );
188 strcpy( dir, bdb->bi_dbenv_home );
189 strcat( &dir[len], BDB_TMP_SUBDIR );
191 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
194 LDAP_LOG( BACK_BDB, ERR,
195 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
196 dir, db_strerror(rc), rc );
198 Debug( LDAP_DEBUG_ANY,
199 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
200 dir, db_strerror(rc), rc );
205 strcat( &dir[len], BDB_LG_SUBDIR );
207 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
210 LDAP_LOG( BACK_BDB, ERR,
211 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
212 dir, db_strerror(rc), rc );
214 Debug( LDAP_DEBUG_ANY,
215 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
216 dir, db_strerror(rc), rc );
221 strcat( &dir[len], BDB_DATA_SUBDIR );
223 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
226 LDAP_LOG( BACK_BDB, ERR,
227 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
228 dir, db_strerror(rc), rc );
230 Debug( LDAP_DEBUG_ANY,
231 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
232 dir, db_strerror(rc), rc );
240 LDAP_LOG( BACK_BDB, DETAIL1,
241 "bdb_db_open: dbenv_open %s\n", bdb->bi_dbenv_home, 0, 0 );
243 Debug( LDAP_DEBUG_TRACE,
244 "bdb_db_open: dbenv_open(%s)\n",
245 bdb->bi_dbenv_home, 0, 0);
249 strcpy( path, bdb->bi_dbenv_home );
251 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
254 bdb->bi_dbenv_mode );
256 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
259 bdb->bi_dbenv_mode );
263 LDAP_LOG( BACK_BDB, ERR,
264 "bdb_db_open: dbenv_open failed: %s (%d)\n",
265 db_strerror(rc), rc, 0 );
267 Debug( LDAP_DEBUG_ANY,
268 "bdb_db_open: dbenv_open failed: %s (%d)\n",
269 db_strerror(rc), rc, 0 );
274 if( bdb->bi_dbenv_xflags != 0 ) {
275 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
276 bdb->bi_dbenv_xflags, 1);
279 LDAP_LOG( BACK_BDB, ERR,
280 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
281 db_strerror(rc), rc, 0 );
283 Debug( LDAP_DEBUG_ANY,
284 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
285 db_strerror(rc), rc, 0 );
291 flags = DB_THREAD | DB_CREATE | bdb->bi_db_opflags;
293 bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
294 BDB_INDICES * sizeof(struct bdb_db_info *) );
296 /* open (and create) main database */
297 for( i = 0; bdbi_databases[i].name; i++ ) {
298 struct bdb_db_info *db;
300 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
302 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
305 LDAP_LOG( BACK_BDB, ERR,
306 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
307 bdb->bi_dbenv_home, db_strerror(rc), rc );
309 Debug( LDAP_DEBUG_ANY,
310 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
311 bdb->bi_dbenv_home, db_strerror(rc), rc );
316 if( i == BDB_ID2ENTRY ) {
317 rc = db->bdi_db->set_bt_compare( db->bdi_db,
319 rc = db->bdi_db->set_pagesize( db->bdi_db,
320 BDB_ID2ENTRY_PAGESIZE );
323 rc = db->bdi_db->set_bt_compare( db->bdi_db,
326 rc = db->bdi_db->set_flags( db->bdi_db,
327 DB_DUP | DB_DUPSORT );
328 rc = db->bdi_db->set_dup_compare( db->bdi_db,
331 rc = db->bdi_db->set_pagesize( db->bdi_db,
336 strcpy( path, bdbi_databases[i].file );
338 rc = DB_OPEN( db->bdi_db,
340 /* bdbi_databases[i].name, */ NULL,
341 bdbi_databases[i].type,
342 bdbi_databases[i].flags | flags,
343 bdb->bi_dbenv_mode );
345 rc = DB_OPEN( db->bdi_db,
346 bdbi_databases[i].file,
347 /* bdbi_databases[i].name, */ NULL,
348 bdbi_databases[i].type,
349 bdbi_databases[i].flags | flags,
350 bdb->bi_dbenv_mode );
355 LDAP_LOG( BACK_BDB, ERR,
356 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
357 bdb->bi_dbenv_home, db_strerror(rc), rc );
359 Debug( LDAP_DEBUG_ANY,
360 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
361 bdb->bi_dbenv_home, db_strerror(rc), rc );
366 db->bdi_name = bdbi_databases[i].name;
367 bdb->bi_databases[i] = db;
370 bdb->bi_databases[i] = NULL;
371 bdb->bi_ndatabases = i;
374 rc = bdb_last_id( be, NULL );
377 LDAP_LOG( BACK_BDB, ERR,
378 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
379 bdb->bi_dbenv_home, db_strerror(rc), rc );
381 Debug( LDAP_DEBUG_ANY,
382 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
383 bdb->bi_dbenv_home, db_strerror(rc), rc );
388 /* <insert> open (and create) index databases */
390 rc = bdb_build_tree( be );
396 bdb_db_close( BackendDB *be )
399 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
400 struct bdb_db_info *db;
402 while( bdb->bi_ndatabases-- ) {
403 db = bdb->bi_databases[bdb->bi_ndatabases];
404 rc = db->bdi_db->close( db->bdi_db, 0 );
405 /* Lower numbered names are not strdup'd */
406 if( bdb->bi_ndatabases >= BDB_NDB )
407 free( db->bdi_name );
410 free( bdb->bi_databases );
411 bdb_attr_index_destroy( bdb->bi_attrs );
413 bdb_cache_release_all (&bdb->bi_cache);
419 bdb_db_destroy( BackendDB *be )
422 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
424 /* close db environment */
425 if( bdb->bi_dbenv ) {
426 /* force a checkpoint */
427 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
430 LDAP_LOG( BACK_BDB, ERR,
431 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
432 db_strerror(rc), rc, 0 );
434 Debug( LDAP_DEBUG_ANY,
435 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
436 db_strerror(rc), rc, 0 );
440 bdb_cache_release_all (&bdb->bi_cache);
442 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
443 bdb->bi_dbenv = NULL;
446 LDAP_LOG( BACK_BDB, ERR,
447 "bdb_db_destroy: close failed: %s (%d)\n",
448 db_strerror(rc), rc, 0 );
450 Debug( LDAP_DEBUG_ANY,
451 "bdb_db_destroy: close failed: %s (%d)\n",
452 db_strerror(rc), rc, 0 );
458 if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
461 ldap_pvt_thread_rdwr_destroy( &bdb->bi_tree_rdwr );
463 ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
464 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_mutex );
465 ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
466 ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
469 be->be_private = NULL;
474 #ifdef SLAPD_BDB_DYNAMIC
475 int back_bdb_LTX_init_module( int argc, char *argv[] ) {
478 memset( &bi, '\0', sizeof(bi) );
480 bi.bi_init = bdb_initialize;
485 #endif /* SLAPD_BDB_DYNAMIC */
492 static char *controls[] = {
493 LDAP_CONTROL_MANAGEDSAIT,
494 #ifdef LDAP_CONTROL_SUBENTRIES
495 LDAP_CONTROL_SUBENTRIES,
497 #ifdef LDAP_CONTROL_NOOP
500 #ifdef LDAP_CONTROL_VALUESRETURNFILTER
501 LDAP_CONTROL_VALUESRETURNFILTER,
503 #ifdef LDAP_CLIENT_UPDATE
504 LDAP_CONTROL_CLIENT_UPDATE,
509 bi->bi_controls = controls;
511 /* initialize the underlying database system */
513 LDAP_LOG( BACK_BDB, ENTRY, "bdb_db_initialize\n", 0, 0, 0 );
515 Debug( LDAP_DEBUG_TRACE, "bdb_open: initialize BDB backend\n",
519 { /* version check */
520 int major, minor, patch;
521 char *version = db_version( &major, &minor, &patch );
525 /* All our stdio does an ASCII to EBCDIC conversion on
526 * the output. Strings from the BDB library are already
527 * in EBCDIC; we have to go back and forth...
529 strcpy( v2, version );
534 if( major != DB_VERSION_MAJOR ||
535 minor != DB_VERSION_MINOR ||
536 patch < DB_VERSION_PATCH )
539 LDAP_LOG( BACK_BDB, ERR,
540 "bdb_db_initialize: version mismatch: "
541 "\texpected: %s \tgot: %s\n", DB_VERSION_STRING, version, 0 );
543 Debug( LDAP_DEBUG_ANY,
544 "bdb_open: version mismatch\n"
545 "\texpected: " DB_VERSION_STRING "\n"
546 "\tgot: %s \n", version, 0, 0 );
551 LDAP_LOG( BACK_BDB, DETAIL1,
552 "bdb_db_initialize: bdb_open: %s\n", version, 0, 0 );
554 Debug( LDAP_DEBUG_ANY, "bdb_open: %s\n",
559 db_env_set_func_free( ber_memfree );
560 db_env_set_func_malloc( (db_malloc *)ber_memalloc );
561 db_env_set_func_realloc( (db_realloc *)ber_memrealloc );
563 /* This is a no-op on a NO_THREAD build. Leave the default
564 * alone so that BDB will sleep on interprocess conflicts.
566 db_env_set_func_yield( ldap_pvt_thread_yield );
570 static char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
572 bdb_uuid.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ));
573 bdb_uuid.bv_val = uuidbuf;
581 bi->bi_db_init = bdb_db_init;
582 bi->bi_db_config = bdb_db_config;
583 bi->bi_db_open = bdb_db_open;
584 bi->bi_db_close = bdb_db_close;
585 bi->bi_db_destroy = bdb_db_destroy;
587 bi->bi_op_add = bdb_add;
588 bi->bi_op_bind = bdb_bind;
589 bi->bi_op_compare = bdb_compare;
590 bi->bi_op_delete = bdb_delete;
591 bi->bi_op_modify = bdb_modify;
592 bi->bi_op_modrdn = bdb_modrdn;
593 bi->bi_op_search = bdb_search;
595 bi->bi_op_unbind = 0;
596 bi->bi_op_abandon = 0;
598 bi->bi_extended = bdb_extended;
602 * these routines (and their callers) are not yet designed
603 * to work with transaction. Using them may cause deadlock.
605 bi->bi_acl_group = bdb_group;
606 bi->bi_acl_attribute = bdb_attribute;
608 bi->bi_acl_group = 0;
609 bi->bi_acl_attribute = 0;
612 bi->bi_chk_referrals = bdb_referrals;
613 bi->bi_operational = bdb_operational;
614 bi->bi_entry_release_rw = bdb_entry_release;
617 * hooks for slap tools
619 bi->bi_tool_entry_open = bdb_tool_entry_open;
620 bi->bi_tool_entry_close = bdb_tool_entry_close;
621 bi->bi_tool_entry_first = bdb_tool_entry_next;
622 bi->bi_tool_entry_next = bdb_tool_entry_next;
623 bi->bi_tool_entry_get = bdb_tool_entry_get;
624 bi->bi_tool_entry_put = bdb_tool_entry_put;
625 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
626 bi->bi_tool_sync = 0;
628 bi->bi_connection_init = 0;
629 bi->bi_connection_destroy = 0;