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>
18 static struct bdbi_database {
23 } bdbi_databases[] = {
24 { "id2entry" BDB_SUFFIX, "id2entry", DB_BTREE, 0 },
26 { "id2parent" BDB_SUFFIX, "id2parent", DB_BTREE, 0 },
28 { "dn2id" BDB_SUFFIX, "dn2id", DB_BTREE, 0 },
35 bdb_destroy( BackendInfo *bi )
41 bdb_open( BackendInfo *bi )
43 /* initialize the underlying database system */
44 Debug( LDAP_DEBUG_TRACE, "bdb_open: initialize BDB backend\n",
51 bdb_close( BackendInfo *bi )
53 /* terminate the underlying database system */
59 bdb_db_init( BackendDB *be )
63 Debug( LDAP_DEBUG_ANY,
64 "bdb_db_init: Initializing BDB database\n",
67 /* indicate system schema supported */
68 be->be_flags |= SLAP_BFLAG_ALIASES
69 | SLAP_BFLAG_REFERRALS
70 | SLAP_BFLAG_SUBENTRIES;
72 /* allocate backend-database-specific stuff */
73 bdb = (struct bdb_info *) ch_calloc( 1, sizeof(struct bdb_info) );
75 /* DBEnv parameters */
76 bdb->bi_dbenv_home = ch_strdup( BDB_DBENV_HOME );
77 bdb->bi_dbenv_xflags = 0;
78 bdb->bi_dbenv_mode = DEFAULT_MODE;
79 bdb->bi_txn = 1; /* default to using transactions */
81 bdb->bi_cache.c_maxsize = DEFAULT_CACHE_SIZE;
85 bdb->bi_lock_detect = DB_LOCK_NORUN;
87 bdb->bi_lock_detect = DB_LOCK_DEFAULT;
91 ldap_pvt_thread_mutex_init( &bdb->bi_database_mutex );
92 ldap_pvt_thread_mutex_init( &bdb->bi_lastid_mutex );
93 ldap_pvt_thread_mutex_init( &bdb->bi_cache.c_mutex );
95 ldap_pvt_thread_rdwr_init( &bdb->bi_tree_rdwr );
102 #if 0 /* ifndef NO_THREADS */
103 static void *lock_detect_task( void *arg )
105 struct bdb_info *bdb = (struct bdb_info *) arg;
107 while( bdb->bi_dbenv != NULL ) {
110 sleep( bdb->bi_lock_detect_seconds );
112 rc = LOCK_DETECT( bdb->bi_dbenv, 0,
113 bdb->bi_lock_detect, &aborted );
119 Debug( LDAP_DEBUG_ANY,
120 "bdb_lock_detect: aborted %d locks\n",
135 unsigned char *u, *c;
141 #ifdef WORDS_BIGENDIAN
142 for( i = 0; i < sizeof(ID); i++)
144 for( i = sizeof(ID)-1; i >= 0; i--)
154 bdb_db_open( BackendDB *be )
157 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
160 Debug( LDAP_DEBUG_ARGS,
162 be->be_suffix[0]->bv_val, 0, 0 );
164 /* we should check existance of dbenv_home and db_directory */
166 rc = db_env_create( &bdb->bi_dbenv, 0 );
168 Debug( LDAP_DEBUG_ANY,
169 "bdb_db_open: db_env_create failed: %s (%d)\n",
170 db_strerror(rc), rc, 0 );
174 flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE;
177 flags |= DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN | DB_RECOVER;
180 flags |= DB_INIT_CDB;
184 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0]->bv_val );
185 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
187 bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
192 char dir[MAXPATHLEN];
193 size_t len = strlen( bdb->bi_dbenv_home );
195 strcpy( dir, bdb->bi_dbenv_home );
196 strcat( &dir[len], BDB_TMP_SUBDIR );
198 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
200 Debug( LDAP_DEBUG_ANY,
201 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
202 dir, db_strerror(rc), rc );
206 strcat( &dir[len], BDB_LG_SUBDIR );
208 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
210 Debug( LDAP_DEBUG_ANY,
211 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
212 dir, db_strerror(rc), rc );
216 strcat( &dir[len], BDB_DATA_SUBDIR );
218 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
220 Debug( LDAP_DEBUG_ANY,
221 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
222 dir, db_strerror(rc), rc );
228 Debug( LDAP_DEBUG_TRACE,
229 "bdb_db_open: dbenv_open(%s)\n",
230 bdb->bi_dbenv_home, 0, 0);
232 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
235 bdb->bi_dbenv_mode );
237 Debug( LDAP_DEBUG_ANY,
238 "bdb_db_open: dbenv_open failed: %s (%d)\n",
239 db_strerror(rc), rc, 0 );
243 if( bdb->bi_dbenv_xflags != 0 ) {
244 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
245 bdb->bi_dbenv_xflags, 1);
247 Debug( LDAP_DEBUG_ANY,
248 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
249 db_strerror(rc), rc, 0 );
254 flags = DB_THREAD | DB_CREATE | bdb->bi_db_opflags;
256 bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
257 BDB_INDICES * sizeof(struct bdb_db_info *) );
259 /* open (and create) main database */
260 for( i = 0; bdbi_databases[i].name; i++ ) {
261 struct bdb_db_info *db;
263 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
265 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
267 Debug( LDAP_DEBUG_ANY,
268 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
269 bdb->bi_dbenv_home, db_strerror(rc), rc );
273 if( i == BDB_ID2ENTRY ) {
274 rc = db->bdi_db->set_bt_compare( db->bdi_db,
276 rc = db->bdi_db->set_pagesize( db->bdi_db,
277 BDB_ID2ENTRY_PAGESIZE );
280 rc = db->bdi_db->set_bt_compare( db->bdi_db,
282 #elif defined(BDB_IDL_MULTI)
283 rc = db->bdi_db->set_flags( db->bdi_db,
284 DB_DUP | DB_DUPSORT );
285 rc = db->bdi_db->set_dup_compare( db->bdi_db,
288 rc = db->bdi_db->set_pagesize( db->bdi_db,
292 rc = db->bdi_db->open( db->bdi_db,
293 bdbi_databases[i].file,
294 /* bdbi_databases[i].name, */ NULL,
295 bdbi_databases[i].type,
296 bdbi_databases[i].flags | flags,
297 bdb->bi_dbenv_mode );
300 Debug( LDAP_DEBUG_ANY,
301 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
302 bdb->bi_dbenv_home, db_strerror(rc), rc );
306 db->bdi_name = bdbi_databases[i].name;
307 bdb->bi_databases[i] = db;
310 bdb->bi_databases[i] = NULL;
311 bdb->bi_ndatabases = i;
314 rc = bdb_last_id( be, NULL );
316 Debug( LDAP_DEBUG_ANY,
317 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
318 bdb->bi_dbenv_home, db_strerror(rc), rc );
322 /* <insert> open (and create) index databases */
324 rc = bdb_build_tree( be );
327 #if 0 /* ifndef NO_THREADS */
328 if( bdb->bi_lock_detect != DB_LOCK_NORUN ) {
329 /* listener as a separate THREAD */
330 rc = ldap_pvt_thread_create( &bdb->bi_lock_detect_tid,
331 1, lock_detect_task, bdb );
338 bdb_db_close( BackendDB *be )
341 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
342 struct bdb_db_info *db;
344 while( bdb->bi_ndatabases-- ) {
345 db = bdb->bi_databases[bdb->bi_ndatabases];
346 rc = db->bdi_db->close( db->bdi_db, 0 );
347 /* Lower numbered names are not strdup'd */
348 if( bdb->bi_ndatabases >= BDB_NDB )
349 free( db->bdi_name );
352 free( bdb->bi_databases );
353 bdb_attr_index_destroy( bdb->bi_attrs );
355 bdb_cache_release_all (&bdb->bi_cache);
361 bdb_db_destroy( BackendDB *be )
364 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
366 /* close db environment */
367 if( bdb->bi_dbenv ) {
368 /* force a checkpoint */
370 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
372 Debug( LDAP_DEBUG_ANY,
373 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
374 db_strerror(rc), rc, 0 );
378 bdb_cache_release_all (&bdb->bi_cache);
380 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
381 bdb->bi_dbenv = NULL;
383 Debug( LDAP_DEBUG_ANY,
384 "bdb_db_destroy: close failed: %s (%d)\n",
385 db_strerror(rc), rc, 0 );
393 #ifdef SLAPD_BDB_DYNAMIC
394 int back_bdb_LTX_init_module( int argc, char *argv[] ) {
397 memset( &bi, '\0', sizeof(bi) );
399 bi.bi_init = bdb_initialize;
404 #endif /* SLAPD_BDB_DYNAMIC */
411 static char *controls[] = {
412 LDAP_CONTROL_MANAGEDSAIT,
413 LDAP_CONTROL_SUBENTRIES,
417 { /* version check */
418 int major, minor, patch;
419 char *version = db_version( &major, &minor, &patch );
421 if( major != DB_VERSION_MAJOR ||
422 minor != DB_VERSION_MINOR ||
423 patch < DB_VERSION_PATCH )
425 Debug( LDAP_DEBUG_ANY,
426 "bi_back_initialize: version mismatch\n"
427 "\texpected: " DB_VERSION_STRING "\n"
428 "\tgot: %s \n", version, 0, 0 );
431 Debug( LDAP_DEBUG_ANY, "bdb_initialize: %s\n",
436 db_env_set_func_malloc( ch_malloc );
437 db_env_set_func_realloc( ch_realloc );
438 db_env_set_func_free( ch_free );
441 db_env_set_func_yield( ldap_pvt_thread_yield );
443 bi->bi_controls = controls;
450 bi->bi_db_init = bdb_db_init;
451 bi->bi_db_config = bdb_db_config;
452 bi->bi_db_open = bdb_db_open;
453 bi->bi_db_close = bdb_db_close;
454 bi->bi_db_destroy = bdb_db_destroy;
456 bi->bi_op_add = bdb_add;
457 bi->bi_op_bind = bdb_bind;
458 bi->bi_op_compare = bdb_compare;
459 bi->bi_op_delete = bdb_delete;
460 bi->bi_op_modify = bdb_modify;
461 bi->bi_op_modrdn = bdb_modrdn;
462 bi->bi_op_search = bdb_search;
464 bi->bi_op_unbind = 0;
465 bi->bi_op_abandon = 0;
467 bi->bi_extended = bdb_extended;
471 * these routines (and their callers) are not yet designed
472 * to work with transaction. Using them may cause deadlock.
474 bi->bi_acl_group = bdb_group;
475 bi->bi_acl_attribute = bdb_attribute;
477 bi->bi_acl_group = 0;
478 bi->bi_acl_attribute = 0;
481 bi->bi_chk_referrals = bdb_referrals;
482 bi->bi_entry_release_rw = bdb_entry_release;
485 * hooks for slap tools
487 bi->bi_tool_entry_open = bdb_tool_entry_open;
488 bi->bi_tool_entry_close = bdb_tool_entry_close;
489 bi->bi_tool_entry_first = bdb_tool_entry_next;
490 bi->bi_tool_entry_next = bdb_tool_entry_next;
491 bi->bi_tool_entry_get = bdb_tool_entry_get;
492 bi->bi_tool_entry_put = bdb_tool_entry_put;
493 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
494 bi->bi_tool_sync = 0;
496 bi->bi_connection_init = 0;
497 bi->bi_connection_destroy = 0;