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 },
34 bdb_open( BackendInfo *bi )
36 static char *controls[] = {
37 LDAP_CONTROL_MANAGEDSAIT,
38 LDAP_CONTROL_SUBENTRIES,
43 /* initialize the underlying database system */
44 Debug( LDAP_DEBUG_TRACE, "bdb_open: initialize BDB backend\n",
48 int major, minor, patch;
49 char *version = db_version( &major, &minor, &patch );
51 if( major != DB_VERSION_MAJOR ||
52 minor != DB_VERSION_MINOR ||
53 patch < DB_VERSION_PATCH )
55 Debug( LDAP_DEBUG_ANY,
56 "bdb_open: version mismatch\n"
57 "\texpected: " DB_VERSION_STRING "\n"
58 "\tgot: %s \n", version, 0, 0 );
61 Debug( LDAP_DEBUG_ANY, "bdb_open: %s\n",
66 db_env_set_func_malloc( ch_malloc );
67 db_env_set_func_realloc( ch_realloc );
68 db_env_set_func_free( ch_free );
71 db_env_set_func_yield( ldap_pvt_thread_yield );
73 bi->bi_controls = controls;
79 bdb_destroy( BackendInfo *bi )
85 bdb_close( BackendInfo *bi )
87 /* terminate the underlying database system */
93 bdb_db_init( BackendDB *be )
97 Debug( LDAP_DEBUG_ANY,
98 "bdb_db_init: Initializing BDB database\n",
101 /* indicate system schema supported */
103 #ifdef BDB_SUBENTRIES
104 SLAP_BFLAG_SUBENTRIES |
109 SLAP_BFLAG_REFERRALS;
111 /* allocate backend-database-specific stuff */
112 bdb = (struct bdb_info *) ch_calloc( 1, sizeof(struct bdb_info) );
114 /* DBEnv parameters */
115 bdb->bi_dbenv_home = ch_strdup( SLAPD_DEFAULT_DB_DIR );
116 bdb->bi_dbenv_xflags = 0;
117 bdb->bi_dbenv_mode = SLAPD_DEFAULT_DB_MODE;
119 bdb->bi_cache.c_maxsize = DEFAULT_CACHE_SIZE;
123 bdb->bi_lock_detect = DB_LOCK_NORUN;
125 bdb->bi_lock_detect = DB_LOCK_DEFAULT;
129 ldap_pvt_thread_mutex_init( &bdb->bi_database_mutex );
130 ldap_pvt_thread_mutex_init( &bdb->bi_lastid_mutex );
131 ldap_pvt_thread_mutex_init( &bdb->bi_cache.c_mutex );
133 ldap_pvt_thread_rdwr_init( &bdb->bi_tree_rdwr );
136 be->be_private = bdb;
140 #if 0 /* ifndef NO_THREADS */
141 static void *lock_detect_task( void *arg )
143 struct bdb_info *bdb = (struct bdb_info *) arg;
145 while( bdb->bi_dbenv != NULL ) {
148 sleep( bdb->bi_lock_detect_seconds );
150 rc = LOCK_DETECT( bdb->bi_dbenv, 0,
151 bdb->bi_lock_detect, &aborted );
157 Debug( LDAP_DEBUG_ANY,
158 "bdb_lock_detect: aborted %d locks\n",
173 unsigned char *u, *c;
179 #ifdef WORDS_BIGENDIAN
180 for( i = 0; i < sizeof(ID); i++)
182 for( i = sizeof(ID)-1; i >= 0; i--)
192 bdb_db_open( BackendDB *be )
195 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
198 Debug( LDAP_DEBUG_ARGS,
200 be->be_suffix[0]->bv_val, 0, 0 );
202 /* we should check existance of dbenv_home and db_directory */
204 rc = db_env_create( &bdb->bi_dbenv, 0 );
206 Debug( LDAP_DEBUG_ANY,
207 "bdb_db_open: db_env_create failed: %s (%d)\n",
208 db_strerror(rc), rc, 0 );
212 flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE
213 | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN | DB_RECOVER;
215 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0]->bv_val );
216 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
218 bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
223 char dir[MAXPATHLEN];
224 size_t len = strlen( bdb->bi_dbenv_home );
226 strcpy( dir, bdb->bi_dbenv_home );
227 strcat( &dir[len], BDB_TMP_SUBDIR );
229 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
231 Debug( LDAP_DEBUG_ANY,
232 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
233 dir, db_strerror(rc), rc );
237 strcat( &dir[len], BDB_LG_SUBDIR );
239 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
241 Debug( LDAP_DEBUG_ANY,
242 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
243 dir, db_strerror(rc), rc );
247 strcat( &dir[len], BDB_DATA_SUBDIR );
249 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
251 Debug( LDAP_DEBUG_ANY,
252 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
253 dir, db_strerror(rc), rc );
259 Debug( LDAP_DEBUG_TRACE,
260 "bdb_db_open: dbenv_open(%s)\n",
261 bdb->bi_dbenv_home, 0, 0);
263 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
266 bdb->bi_dbenv_mode );
268 Debug( LDAP_DEBUG_ANY,
269 "bdb_db_open: dbenv_open failed: %s (%d)\n",
270 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);
278 Debug( LDAP_DEBUG_ANY,
279 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
280 db_strerror(rc), rc, 0 );
285 flags = DB_THREAD | DB_CREATE | bdb->bi_db_opflags;
287 bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
288 BDB_INDICES * sizeof(struct bdb_db_info *) );
290 /* open (and create) main database */
291 for( i = 0; bdbi_databases[i].name; i++ ) {
292 struct bdb_db_info *db;
294 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
296 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
298 Debug( LDAP_DEBUG_ANY,
299 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
300 bdb->bi_dbenv_home, db_strerror(rc), rc );
304 if( i == BDB_ID2ENTRY ) {
305 rc = db->bdi_db->set_bt_compare( db->bdi_db,
307 rc = db->bdi_db->set_pagesize( db->bdi_db,
308 BDB_ID2ENTRY_PAGESIZE );
311 rc = db->bdi_db->set_bt_compare( db->bdi_db,
313 #elif defined(BDB_IDL_MULTI)
314 rc = db->bdi_db->set_flags( db->bdi_db,
315 DB_DUP | DB_DUPSORT );
316 rc = db->bdi_db->set_dup_compare( db->bdi_db,
319 rc = db->bdi_db->set_pagesize( db->bdi_db,
323 rc = db->bdi_db->open( db->bdi_db,
324 bdbi_databases[i].file,
325 /* bdbi_databases[i].name, */ NULL,
326 bdbi_databases[i].type,
327 bdbi_databases[i].flags | flags,
328 bdb->bi_dbenv_mode );
331 Debug( LDAP_DEBUG_ANY,
332 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
333 bdb->bi_dbenv_home, db_strerror(rc), rc );
337 db->bdi_name = bdbi_databases[i].name;
338 bdb->bi_databases[i] = db;
341 bdb->bi_databases[i] = NULL;
342 bdb->bi_ndatabases = i;
345 rc = bdb_last_id( be, NULL );
347 Debug( LDAP_DEBUG_ANY,
348 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
349 bdb->bi_dbenv_home, db_strerror(rc), rc );
353 /* <insert> open (and create) index databases */
355 rc = bdb_build_tree( be );
358 #if 0 /* ifndef NO_THREADS */
359 if( bdb->bi_lock_detect != DB_LOCK_NORUN ) {
360 /* listener as a separate THREAD */
361 rc = ldap_pvt_thread_create( &bdb->bi_lock_detect_tid,
362 1, lock_detect_task, bdb );
369 bdb_db_close( BackendDB *be )
372 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
373 struct bdb_db_info *db;
375 while( bdb->bi_ndatabases-- ) {
376 db = bdb->bi_databases[bdb->bi_ndatabases];
377 rc = db->bdi_db->close( db->bdi_db, 0 );
378 /* Lower numbered names are not strdup'd */
379 if( bdb->bi_ndatabases >= BDB_NDB )
380 free( db->bdi_name );
383 free( bdb->bi_databases );
384 bdb_attr_index_destroy( bdb->bi_attrs );
386 bdb_cache_release_all (&bdb->bi_cache);
392 bdb_db_destroy( BackendDB *be )
395 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
397 /* close db environment */
398 if( bdb->bi_dbenv ) {
399 /* force a checkpoint */
400 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
402 Debug( LDAP_DEBUG_ANY,
403 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
404 db_strerror(rc), rc, 0 );
407 bdb_cache_release_all (&bdb->bi_cache);
409 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
410 bdb->bi_dbenv = NULL;
412 Debug( LDAP_DEBUG_ANY,
413 "bdb_db_destroy: close failed: %s (%d)\n",
414 db_strerror(rc), rc, 0 );
422 #ifdef SLAPD_BDB_DYNAMIC
423 int back_bdb_LTX_init_module( int argc, char *argv[] ) {
426 memset( &bi, '\0', sizeof(bi) );
428 bi.bi_init = bdb_initialize;
433 #endif /* SLAPD_BDB_DYNAMIC */
440 bi->bi_open = bdb_open;
445 bi->bi_db_init = bdb_db_init;
446 bi->bi_db_config = bdb_db_config;
447 bi->bi_db_open = bdb_db_open;
448 bi->bi_db_close = bdb_db_close;
449 bi->bi_db_destroy = bdb_db_destroy;
451 bi->bi_op_add = bdb_add;
452 bi->bi_op_bind = bdb_bind;
453 bi->bi_op_compare = bdb_compare;
454 bi->bi_op_delete = bdb_delete;
455 bi->bi_op_modify = bdb_modify;
456 bi->bi_op_modrdn = bdb_modrdn;
457 bi->bi_op_search = bdb_search;
459 bi->bi_op_unbind = 0;
460 bi->bi_op_abandon = 0;
462 bi->bi_extended = bdb_extended;
466 * these routines (and their callers) are not yet designed
467 * to work with transaction. Using them may cause deadlock.
469 bi->bi_acl_group = bdb_group;
470 bi->bi_acl_attribute = bdb_attribute;
472 bi->bi_acl_group = 0;
473 bi->bi_acl_attribute = 0;
476 bi->bi_chk_referrals = bdb_referrals;
477 bi->bi_entry_release_rw = bdb_entry_release;
480 * hooks for slap tools
482 bi->bi_tool_entry_open = bdb_tool_entry_open;
483 bi->bi_tool_entry_close = bdb_tool_entry_close;
484 bi->bi_tool_entry_first = bdb_tool_entry_next;
485 bi->bi_tool_entry_next = bdb_tool_entry_next;
486 bi->bi_tool_entry_get = bdb_tool_entry_get;
487 bi->bi_tool_entry_put = bdb_tool_entry_put;
488 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
489 bi->bi_tool_sync = 0;
491 bi->bi_connection_init = 0;
492 bi->bi_connection_destroy = 0;