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;
80 bdb->bi_cache.c_maxsize = DEFAULT_CACHE_SIZE;
84 bdb->bi_lock_detect = DB_LOCK_NORUN;
86 bdb->bi_lock_detect = DB_LOCK_DEFAULT;
90 ldap_pvt_thread_mutex_init( &bdb->bi_database_mutex );
91 ldap_pvt_thread_mutex_init( &bdb->bi_lastid_mutex );
92 ldap_pvt_thread_mutex_init( &bdb->bi_cache.c_mutex );
94 ldap_pvt_thread_rdwr_init( &bdb->bi_tree_rdwr );
101 #if 0 /* ifndef NO_THREADS */
102 static void *lock_detect_task( void *arg )
104 struct bdb_info *bdb = (struct bdb_info *) arg;
106 while( bdb->bi_dbenv != NULL ) {
109 sleep( bdb->bi_lock_detect_seconds );
111 rc = LOCK_DETECT( bdb->bi_dbenv, 0,
112 bdb->bi_lock_detect, &aborted );
118 Debug( LDAP_DEBUG_ANY,
119 "bdb_lock_detect: aborted %d locks\n",
134 unsigned char *u, *c;
140 #ifdef WORDS_BIGENDIAN
141 for( i = 0; i < sizeof(ID); i++)
143 for( i = sizeof(ID)-1; i >= 0; i--)
153 bdb_db_open( BackendDB *be )
156 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
159 Debug( LDAP_DEBUG_ARGS,
161 be->be_suffix[0]->bv_val, 0, 0 );
163 /* we should check existance of dbenv_home and db_directory */
165 rc = db_env_create( &bdb->bi_dbenv, 0 );
167 Debug( LDAP_DEBUG_ANY,
168 "bdb_db_open: db_env_create failed: %s (%d)\n",
169 db_strerror(rc), rc, 0 );
173 flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE
174 | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN | DB_RECOVER;
176 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0]->bv_val );
177 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
179 bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
184 char dir[MAXPATHLEN];
185 size_t len = strlen( bdb->bi_dbenv_home );
187 strcpy( dir, bdb->bi_dbenv_home );
188 strcat( &dir[len], BDB_TMP_SUBDIR );
190 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
192 Debug( LDAP_DEBUG_ANY,
193 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
194 dir, db_strerror(rc), rc );
198 strcat( &dir[len], BDB_LG_SUBDIR );
200 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
202 Debug( LDAP_DEBUG_ANY,
203 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
204 dir, db_strerror(rc), rc );
208 strcat( &dir[len], BDB_DATA_SUBDIR );
210 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
212 Debug( LDAP_DEBUG_ANY,
213 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
214 dir, db_strerror(rc), rc );
220 Debug( LDAP_DEBUG_TRACE,
221 "bdb_db_open: dbenv_open(%s)\n",
222 bdb->bi_dbenv_home, 0, 0);
224 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
227 bdb->bi_dbenv_mode );
229 Debug( LDAP_DEBUG_ANY,
230 "bdb_db_open: dbenv_open failed: %s (%d)\n",
231 db_strerror(rc), rc, 0 );
235 if( bdb->bi_dbenv_xflags != 0 ) {
236 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
237 bdb->bi_dbenv_xflags, 1);
239 Debug( LDAP_DEBUG_ANY,
240 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
241 db_strerror(rc), rc, 0 );
246 flags = DB_THREAD | DB_CREATE | bdb->bi_db_opflags;
248 bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
249 BDB_INDICES * sizeof(struct bdb_db_info *) );
251 /* open (and create) main database */
252 for( i = 0; bdbi_databases[i].name; i++ ) {
253 struct bdb_db_info *db;
255 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
257 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
259 Debug( LDAP_DEBUG_ANY,
260 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
261 bdb->bi_dbenv_home, db_strerror(rc), rc );
265 if( i == BDB_ID2ENTRY ) {
266 rc = db->bdi_db->set_bt_compare( db->bdi_db,
268 rc = db->bdi_db->set_pagesize( db->bdi_db,
269 BDB_ID2ENTRY_PAGESIZE );
272 rc = db->bdi_db->set_bt_compare( db->bdi_db,
274 #elif defined(BDB_IDL_MULTI)
275 rc = db->bdi_db->set_flags( db->bdi_db,
276 DB_DUP | DB_DUPSORT );
277 rc = db->bdi_db->set_dup_compare( db->bdi_db,
280 rc = db->bdi_db->set_pagesize( db->bdi_db,
284 rc = db->bdi_db->open( db->bdi_db,
285 bdbi_databases[i].file,
286 /* bdbi_databases[i].name, */ NULL,
287 bdbi_databases[i].type,
288 bdbi_databases[i].flags | flags,
289 bdb->bi_dbenv_mode );
292 Debug( LDAP_DEBUG_ANY,
293 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
294 bdb->bi_dbenv_home, db_strerror(rc), rc );
298 db->bdi_name = bdbi_databases[i].name;
299 bdb->bi_databases[i] = db;
302 bdb->bi_databases[i] = NULL;
303 bdb->bi_ndatabases = i;
306 rc = bdb_last_id( be, NULL );
308 Debug( LDAP_DEBUG_ANY,
309 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
310 bdb->bi_dbenv_home, db_strerror(rc), rc );
314 /* <insert> open (and create) index databases */
316 rc = bdb_build_tree( be );
319 #if 0 /* ifndef NO_THREADS */
320 if( bdb->bi_lock_detect != DB_LOCK_NORUN ) {
321 /* listener as a separate THREAD */
322 rc = ldap_pvt_thread_create( &bdb->bi_lock_detect_tid,
323 1, lock_detect_task, bdb );
330 bdb_db_close( BackendDB *be )
333 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
334 struct bdb_db_info *db;
336 while( bdb->bi_ndatabases-- ) {
337 db = bdb->bi_databases[bdb->bi_ndatabases];
338 rc = db->bdi_db->close( db->bdi_db, 0 );
339 /* Lower numbered names are not strdup'd */
340 if( bdb->bi_ndatabases >= BDB_NDB )
341 free( db->bdi_name );
344 free( bdb->bi_databases );
345 bdb_attr_index_destroy( bdb->bi_attrs );
347 bdb_cache_release_all (&bdb->bi_cache);
353 bdb_db_destroy( BackendDB *be )
356 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
358 /* close db environment */
359 if( bdb->bi_dbenv ) {
360 /* force a checkpoint */
361 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
363 Debug( LDAP_DEBUG_ANY,
364 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
365 db_strerror(rc), rc, 0 );
368 bdb_cache_release_all (&bdb->bi_cache);
370 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
371 bdb->bi_dbenv = NULL;
373 Debug( LDAP_DEBUG_ANY,
374 "bdb_db_destroy: close failed: %s (%d)\n",
375 db_strerror(rc), rc, 0 );
383 #ifdef SLAPD_BDB_DYNAMIC
384 int back_bdb_LTX_init_module( int argc, char *argv[] ) {
387 memset( &bi, '\0', sizeof(bi) );
389 bi.bi_init = bdb_initialize;
394 #endif /* SLAPD_BDB_DYNAMIC */
401 static char *controls[] = {
402 LDAP_CONTROL_MANAGEDSAIT,
403 LDAP_CONTROL_SUBENTRIES,
410 { /* version check */
411 int major, minor, patch;
412 char *version = db_version( &major, &minor, &patch );
414 if( major != DB_VERSION_MAJOR ||
415 minor != DB_VERSION_MINOR ||
416 patch < DB_VERSION_PATCH )
418 Debug( LDAP_DEBUG_ANY,
419 "bi_back_initialize: version mismatch\n"
420 "\texpected: " DB_VERSION_STRING "\n"
421 "\tgot: %s \n", version, 0, 0 );
424 Debug( LDAP_DEBUG_ANY, "bdb_initialize: %s\n",
429 db_env_set_func_malloc( ch_malloc );
430 db_env_set_func_realloc( ch_realloc );
431 db_env_set_func_free( ch_free );
434 db_env_set_func_yield( ldap_pvt_thread_yield );
436 bi->bi_controls = controls;
443 bi->bi_db_init = bdb_db_init;
444 bi->bi_db_config = bdb_db_config;
445 bi->bi_db_open = bdb_db_open;
446 bi->bi_db_close = bdb_db_close;
447 bi->bi_db_destroy = bdb_db_destroy;
449 bi->bi_op_add = bdb_add;
450 bi->bi_op_bind = bdb_bind;
451 bi->bi_op_compare = bdb_compare;
452 bi->bi_op_delete = bdb_delete;
453 bi->bi_op_modify = bdb_modify;
454 bi->bi_op_modrdn = bdb_modrdn;
455 bi->bi_op_search = bdb_search;
457 bi->bi_op_unbind = 0;
458 bi->bi_op_abandon = 0;
460 bi->bi_extended = bdb_extended;
464 * these routines (and their callers) are not yet designed
465 * to work with transaction. Using them may cause deadlock.
467 bi->bi_acl_group = bdb_group;
468 bi->bi_acl_attribute = bdb_attribute;
470 bi->bi_acl_group = 0;
471 bi->bi_acl_attribute = 0;
474 bi->bi_chk_referrals = bdb_referrals;
475 bi->bi_entry_release_rw = bdb_entry_release;
478 * hooks for slap tools
480 bi->bi_tool_entry_open = bdb_tool_entry_open;
481 bi->bi_tool_entry_close = bdb_tool_entry_close;
482 bi->bi_tool_entry_first = bdb_tool_entry_next;
483 bi->bi_tool_entry_next = bdb_tool_entry_next;
484 bi->bi_tool_entry_get = bdb_tool_entry_get;
485 bi->bi_tool_entry_put = bdb_tool_entry_put;
486 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
487 bi->bi_tool_sync = 0;
489 bi->bi_connection_init = 0;
490 bi->bi_connection_destroy = 0;