1 /* init.c - initialize bdb backend */
4 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/string.h>
16 static struct bdbi_database {
21 } bdbi_databases[] = {
22 { "id2entry" BDB_SUFFIX, "id2entry", DB_BTREE, 0 },
24 { "id2parent" BDB_SUFFIX, "id2parent", DB_BTREE, 0 },
26 { "dn2id" BDB_SUFFIX, "dn2id", DB_BTREE, 0 },
33 bdb_destroy( BackendInfo *bi )
39 bdb_open( BackendInfo *bi )
41 /* initialize the underlying database system */
42 Debug( LDAP_DEBUG_TRACE, "bdb_open: initialize BDB backend\n",
49 bdb_close( BackendInfo *bi )
51 /* terminate the underlying database system */
57 bdb_db_init( BackendDB *be )
61 Debug( LDAP_DEBUG_ANY,
62 "bdb_db_init: Initializing BDB database\n",
65 /* allocate backend-database-specific stuff */
66 bdb = (struct bdb_info *) ch_calloc( 1, sizeof(struct bdb_info) );
68 /* DBEnv parameters */
69 bdb->bi_dbenv_home = ch_strdup( BDB_DBENV_HOME );
70 bdb->bi_dbenv_xflags = 0;
71 bdb->bi_dbenv_mode = DEFAULT_MODE;
72 bdb->bi_txn = 1; /* default to using transactions */
75 bdb->bi_lock_detect = DB_LOCK_NORUN;
78 ldap_pvt_thread_mutex_init( &bdb->bi_database_mutex );
79 ldap_pvt_thread_mutex_init( &bdb->bi_lastid_mutex );
81 ldap_pvt_thread_rdwr_init( &bdb->bi_tree_rdwr );
89 static void *lock_detect_task( void *arg )
91 struct bdb_info *bdb = (struct bdb_info *) arg;
93 while( bdb->bi_dbenv != NULL ) {
95 sleep( bdb->bi_lock_detect_seconds );
97 rc = LOCK_DETECT( bdb->bi_dbenv, 0,
98 bdb->bi_lock_detect, NULL );
116 unsigned char *u, *c;
122 #ifdef WORDS_BIGENDIAN
123 for( i = 0; i < sizeof(ID); i++)
125 for( i = sizeof(ID)-1; i >= 0; i--)
135 bdb_db_open( BackendDB *be )
138 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
141 Debug( LDAP_DEBUG_ARGS,
143 be->be_suffix[0], 0, 0 );
145 /* we should check existance of dbenv_home and db_directory */
147 rc = db_env_create( &bdb->bi_dbenv, 0 );
149 Debug( LDAP_DEBUG_ANY,
150 "bdb_db_open: db_env_create failed: %s (%d)\n",
151 db_strerror(rc), rc, 0 );
155 flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE;
158 flags |= DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN | DB_RECOVER;
161 flags |= DB_INIT_CDB;
165 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0] );
166 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
170 char dir[MAXPATHLEN];
171 size_t len = strlen( bdb->bi_dbenv_home );
173 strcpy( dir, bdb->bi_dbenv_home );
174 strcat( &dir[len], BDB_TMP_SUBDIR );
176 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
178 Debug( LDAP_DEBUG_ANY,
179 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
180 dir, db_strerror(rc), rc );
184 strcat( &dir[len], BDB_LG_SUBDIR );
186 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
188 Debug( LDAP_DEBUG_ANY,
189 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
190 dir, db_strerror(rc), rc );
194 strcat( &dir[len], BDB_DATA_SUBDIR );
196 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
198 Debug( LDAP_DEBUG_ANY,
199 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
200 dir, db_strerror(rc), rc );
206 Debug( LDAP_DEBUG_TRACE,
207 "bdb_db_open: dbenv_open(%s)\n",
208 bdb->bi_dbenv_home, 0, 0);
210 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
213 bdb->bi_dbenv_mode );
215 Debug( LDAP_DEBUG_ANY,
216 "bdb_db_open: dbenv_open failed: %s (%d)\n",
217 db_strerror(rc), rc, 0 );
221 if( bdb->bi_dbenv_xflags != 0 ) {
222 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
223 bdb->bi_dbenv_xflags, 1);
225 Debug( LDAP_DEBUG_ANY,
226 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
227 db_strerror(rc), rc, 0 );
232 flags = DB_THREAD | DB_CREATE | bdb->bi_db_opflags;
234 bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
235 BDB_INDICES * sizeof(struct bdb_db_info *) );
237 /* open (and create) main database */
238 for( i = 0; bdbi_databases[i].name; i++ ) {
239 struct bdb_db_info *db;
241 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
243 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
245 Debug( LDAP_DEBUG_ANY,
246 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
247 bdb->bi_dbenv_home, db_strerror(rc), rc );
251 if( i == BDB_ID2ENTRY ) {
252 rc = db->bdi_db->set_bt_compare( db->bdi_db,
254 rc = db->bdi_db->set_pagesize( db->bdi_db,
255 BDB_ID2ENTRY_PAGESIZE );
258 rc = db->bdi_db->set_bt_compare( db->bdi_db,
260 #elif defined(BDB_IDL_MULTI)
261 rc = db->bdi_db->set_flags( db->bdi_db,
262 DB_DUP | DB_DUPSORT | DB_NODUPDATA );
263 rc = db->bdi_db->set_dup_compare( db->bdi_db,
266 rc = db->bdi_db->set_pagesize( db->bdi_db,
270 rc = db->bdi_db->open( db->bdi_db,
271 bdbi_databases[i].file,
272 /* bdbi_databases[i].name, */ NULL,
273 bdbi_databases[i].type,
274 bdbi_databases[i].flags | flags,
275 bdb->bi_dbenv_mode );
278 Debug( LDAP_DEBUG_ANY,
279 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
280 bdb->bi_dbenv_home, db_strerror(rc), rc );
284 db->bdi_name = bdbi_databases[i].name;
285 bdb->bi_databases[i] = db;
288 bdb->bi_databases[i] = NULL;
289 bdb->bi_ndatabases = i;
292 rc = bdb_last_id( be, NULL );
294 Debug( LDAP_DEBUG_ANY,
295 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
296 bdb->bi_dbenv_home, db_strerror(rc), rc );
300 /* <insert> open (and create) index databases */
302 rc = bdb_build_tree( be );
306 if( bdb->bi_lock_detect != DB_LOCK_NORUN ) {
307 /* listener as a separate THREAD */
308 rc = ldap_pvt_thread_create( &bdb->bi_lock_detect_tid,
309 1, lock_detect_task, bdb );
316 bdb_db_close( BackendDB *be )
319 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
321 while( bdb->bi_ndatabases-- ) {
322 rc = bdb->bi_databases[bdb->bi_ndatabases]->bdi_db->close(
323 bdb->bi_databases[bdb->bi_ndatabases]->bdi_db, 0 );
330 bdb_db_destroy( BackendDB *be )
333 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
335 /* force a checkpoint */
337 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
339 Debug( LDAP_DEBUG_ANY,
340 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
341 db_strerror(rc), rc, 0 );
345 /* close db environment */
346 if( bdb->bi_dbenv ) {
347 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
348 bdb->bi_dbenv = NULL;
350 Debug( LDAP_DEBUG_ANY,
351 "bdb_db_destroy: close failed: %s (%d)\n",
352 db_strerror(rc), rc, 0 );
360 #ifdef SLAPD_BDB_DYNAMIC
361 int back_bdb_LTX_init_module( int argc, char *argv[] ) {
364 memset( &bi, '\0', sizeof(bi) );
366 bi.bi_init = bdb_initialize;
371 #endif /* SLAPD_BDB_DYNAMIC */
378 static char *controls[] = {
379 LDAP_CONTROL_MANAGEDSAIT,
383 { /* version check */
384 int major, minor, patch;
385 char *version = db_version( &major, &minor, &patch );
387 if( major != DB_VERSION_MAJOR ||
388 minor != DB_VERSION_MINOR ||
389 patch < DB_VERSION_PATCH )
391 Debug( LDAP_DEBUG_ANY,
392 "bi_back_initialize: version mismatch\n"
393 "\texpected: " DB_VERSION_STRING "\n"
394 "\tgot: %s \n", version, 0, 0 );
397 Debug( LDAP_DEBUG_ANY, "bdb_initialize: %s\n",
402 db_env_set_func_malloc( ch_malloc );
403 db_env_set_func_realloc( ch_realloc );
404 db_env_set_func_free( ch_free );
407 db_env_set_func_yield( ldap_pvt_thread_yield );
409 bi->bi_controls = controls;
416 bi->bi_db_init = bdb_db_init;
417 bi->bi_db_config = bdb_db_config;
418 bi->bi_db_open = bdb_db_open;
419 bi->bi_db_close = bdb_db_close;
420 bi->bi_db_destroy = bdb_db_destroy;
422 bi->bi_op_add = bdb_add;
423 bi->bi_op_bind = bdb_bind;
424 bi->bi_op_compare = bdb_compare;
425 bi->bi_op_delete = bdb_delete;
426 bi->bi_op_modify = bdb_modify;
427 bi->bi_op_modrdn = bdb_modrdn;
428 bi->bi_op_search = bdb_search;
431 bi->bi_op_unbind = bdb_unbind;
432 bi->bi_op_abandon = bdb_abandon;
434 bi->bi_extended = bdb_extended;
435 bi->bi_acl_group = bdb_group;
437 bi->bi_acl_attribute = bdb_attribute;
438 bi->bi_chk_referrals = bdb_referrals;
440 bi->bi_entry_release_rw = bdb_entry_release;
443 * hooks for slap tools
445 bi->bi_tool_entry_open = bdb_tool_entry_open;
446 bi->bi_tool_entry_close = bdb_tool_entry_close;
447 bi->bi_tool_entry_first = bdb_tool_entry_next;
448 bi->bi_tool_entry_next = bdb_tool_entry_next;
449 bi->bi_tool_entry_get = bdb_tool_entry_get;
450 bi->bi_tool_entry_put = bdb_tool_entry_put;
451 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
452 bi->bi_tool_sync = 0;
454 bi->bi_connection_init = 0;
455 bi->bi_connection_destroy = 0;