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 };
38 bdb_open( BackendInfo *bi )
45 bdb_destroy( BackendInfo *bi )
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 */
70 SLAP_BFLAG_SUBENTRIES |
77 /* allocate backend-database-specific stuff */
78 bdb = (struct bdb_info *) ch_calloc( 1, sizeof(struct bdb_info) );
80 /* DBEnv parameters */
81 bdb->bi_dbenv_home = ch_strdup( SLAPD_DEFAULT_DB_DIR );
82 bdb->bi_dbenv_xflags = 0;
83 bdb->bi_dbenv_mode = SLAPD_DEFAULT_DB_MODE;
85 bdb->bi_cache.c_maxsize = DEFAULT_CACHE_SIZE;
89 bdb->bi_lock_detect = DB_LOCK_NORUN;
91 bdb->bi_lock_detect = DB_LOCK_DEFAULT;
95 ldap_pvt_thread_mutex_init( &bdb->bi_database_mutex );
96 ldap_pvt_thread_mutex_init( &bdb->bi_lastid_mutex );
97 ldap_pvt_thread_mutex_init( &bdb->bi_cache.c_mutex );
99 ldap_pvt_thread_rdwr_init( &bdb->bi_tree_rdwr );
102 be->be_private = bdb;
106 #if 0 /* ifndef NO_THREADS */
107 static void *lock_detect_task( void *arg )
109 struct bdb_info *bdb = (struct bdb_info *) arg;
111 while( bdb->bi_dbenv != NULL ) {
114 sleep( bdb->bi_lock_detect_seconds );
116 rc = LOCK_DETECT( bdb->bi_dbenv, 0,
117 bdb->bi_lock_detect, &aborted );
123 Debug( LDAP_DEBUG_ANY,
124 "bdb_lock_detect: aborted %d locks\n",
139 unsigned char *u, *c;
145 #ifdef WORDS_BIGENDIAN
146 for( i = 0; i < sizeof(ID); i++)
148 for( i = sizeof(ID)-1; i >= 0; i--)
158 bdb_db_open( BackendDB *be )
161 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
164 Debug( LDAP_DEBUG_ARGS,
166 be->be_suffix[0]->bv_val, 0, 0 );
168 /* we should check existance of dbenv_home and db_directory */
170 rc = db_env_create( &bdb->bi_dbenv, 0 );
172 Debug( LDAP_DEBUG_ANY,
173 "bdb_db_open: db_env_create failed: %s (%d)\n",
174 db_strerror(rc), rc, 0 );
178 flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE
179 | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN | DB_RECOVER;
181 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0]->bv_val );
182 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
184 bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
189 char dir[MAXPATHLEN];
190 size_t len = strlen( bdb->bi_dbenv_home );
192 strcpy( dir, bdb->bi_dbenv_home );
193 strcat( &dir[len], BDB_TMP_SUBDIR );
195 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
197 Debug( LDAP_DEBUG_ANY,
198 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
199 dir, db_strerror(rc), rc );
203 strcat( &dir[len], BDB_LG_SUBDIR );
205 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
207 Debug( LDAP_DEBUG_ANY,
208 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
209 dir, db_strerror(rc), rc );
213 strcat( &dir[len], BDB_DATA_SUBDIR );
215 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
217 Debug( LDAP_DEBUG_ANY,
218 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
219 dir, db_strerror(rc), rc );
225 Debug( LDAP_DEBUG_TRACE,
226 "bdb_db_open: dbenv_open(%s)\n",
227 bdb->bi_dbenv_home, 0, 0);
229 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
232 bdb->bi_dbenv_mode );
234 Debug( LDAP_DEBUG_ANY,
235 "bdb_db_open: dbenv_open failed: %s (%d)\n",
236 db_strerror(rc), rc, 0 );
240 if( bdb->bi_dbenv_xflags != 0 ) {
241 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
242 bdb->bi_dbenv_xflags, 1);
244 Debug( LDAP_DEBUG_ANY,
245 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
246 db_strerror(rc), rc, 0 );
251 flags = DB_THREAD | DB_CREATE | bdb->bi_db_opflags;
253 bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
254 BDB_INDICES * sizeof(struct bdb_db_info *) );
256 /* open (and create) main database */
257 for( i = 0; bdbi_databases[i].name; i++ ) {
258 struct bdb_db_info *db;
260 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
262 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
264 Debug( LDAP_DEBUG_ANY,
265 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
266 bdb->bi_dbenv_home, db_strerror(rc), rc );
270 if( i == BDB_ID2ENTRY ) {
271 rc = db->bdi_db->set_bt_compare( db->bdi_db,
273 rc = db->bdi_db->set_pagesize( db->bdi_db,
274 BDB_ID2ENTRY_PAGESIZE );
277 rc = db->bdi_db->set_bt_compare( db->bdi_db,
279 #elif defined(BDB_IDL_MULTI)
280 rc = db->bdi_db->set_flags( db->bdi_db,
281 DB_DUP | DB_DUPSORT );
282 rc = db->bdi_db->set_dup_compare( db->bdi_db,
285 rc = db->bdi_db->set_pagesize( db->bdi_db,
289 rc = db->bdi_db->open( db->bdi_db,
290 bdbi_databases[i].file,
291 /* bdbi_databases[i].name, */ NULL,
292 bdbi_databases[i].type,
293 bdbi_databases[i].flags | flags,
294 bdb->bi_dbenv_mode );
297 Debug( LDAP_DEBUG_ANY,
298 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
299 bdb->bi_dbenv_home, db_strerror(rc), rc );
303 db->bdi_name = bdbi_databases[i].name;
304 bdb->bi_databases[i] = db;
307 bdb->bi_databases[i] = NULL;
308 bdb->bi_ndatabases = i;
311 rc = bdb_last_id( be, NULL );
313 Debug( LDAP_DEBUG_ANY,
314 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
315 bdb->bi_dbenv_home, db_strerror(rc), rc );
319 /* <insert> open (and create) index databases */
321 rc = bdb_build_tree( be );
324 #if 0 /* ifndef NO_THREADS */
325 if( bdb->bi_lock_detect != DB_LOCK_NORUN ) {
326 /* listener as a separate THREAD */
327 rc = ldap_pvt_thread_create( &bdb->bi_lock_detect_tid,
328 1, lock_detect_task, bdb );
335 bdb_db_close( BackendDB *be )
338 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
339 struct bdb_db_info *db;
341 while( bdb->bi_ndatabases-- ) {
342 db = bdb->bi_databases[bdb->bi_ndatabases];
343 rc = db->bdi_db->close( db->bdi_db, 0 );
344 /* Lower numbered names are not strdup'd */
345 if( bdb->bi_ndatabases >= BDB_NDB )
346 free( db->bdi_name );
349 free( bdb->bi_databases );
350 bdb_attr_index_destroy( bdb->bi_attrs );
352 bdb_cache_release_all (&bdb->bi_cache);
358 bdb_db_destroy( BackendDB *be )
361 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
363 /* close db environment */
364 if( bdb->bi_dbenv ) {
365 /* force a checkpoint */
366 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
368 Debug( LDAP_DEBUG_ANY,
369 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
370 db_strerror(rc), rc, 0 );
373 bdb_cache_release_all (&bdb->bi_cache);
375 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
376 bdb->bi_dbenv = NULL;
378 Debug( LDAP_DEBUG_ANY,
379 "bdb_db_destroy: close failed: %s (%d)\n",
380 db_strerror(rc), rc, 0 );
388 #ifdef SLAPD_BDB_DYNAMIC
389 int back_bdb_LTX_init_module( int argc, char *argv[] ) {
392 memset( &bi, '\0', sizeof(bi) );
394 bi.bi_init = bdb_initialize;
399 #endif /* SLAPD_BDB_DYNAMIC */
406 static char *controls[] = {
407 LDAP_CONTROL_MANAGEDSAIT,
408 #ifdef LDAP_CONTROL_SUBENTRIES
409 LDAP_CONTROL_SUBENTRIES,
411 #ifdef LDAP_CONTROL_NOOP
417 bi->bi_controls = controls;
419 /* initialize the underlying database system */
420 Debug( LDAP_DEBUG_TRACE, "bdb_open: initialize BDB backend\n",
423 { /* version check */
424 int major, minor, patch;
425 char *version = db_version( &major, &minor, &patch );
427 if( major != DB_VERSION_MAJOR ||
428 minor != DB_VERSION_MINOR ||
429 patch < DB_VERSION_PATCH )
431 Debug( LDAP_DEBUG_ANY,
432 "bdb_open: version mismatch\n"
433 "\texpected: " DB_VERSION_STRING "\n"
434 "\tgot: %s \n", version, 0, 0 );
437 Debug( LDAP_DEBUG_ANY, "bdb_open: %s\n",
442 db_env_set_func_malloc( ch_malloc );
443 db_env_set_func_realloc( ch_realloc );
444 db_env_set_func_free( ch_free );
447 db_env_set_func_yield( ldap_pvt_thread_yield );
450 static char uuidbuf[40];
452 bdb_uuid.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ));
453 bdb_uuid.bv_val = uuidbuf;
461 bi->bi_db_init = bdb_db_init;
462 bi->bi_db_config = bdb_db_config;
463 bi->bi_db_open = bdb_db_open;
464 bi->bi_db_close = bdb_db_close;
465 bi->bi_db_destroy = bdb_db_destroy;
467 bi->bi_op_add = bdb_add;
468 bi->bi_op_bind = bdb_bind;
469 bi->bi_op_compare = bdb_compare;
470 bi->bi_op_delete = bdb_delete;
471 bi->bi_op_modify = bdb_modify;
472 bi->bi_op_modrdn = bdb_modrdn;
473 bi->bi_op_search = bdb_search;
475 bi->bi_op_unbind = 0;
476 bi->bi_op_abandon = 0;
478 bi->bi_extended = bdb_extended;
482 * these routines (and their callers) are not yet designed
483 * to work with transaction. Using them may cause deadlock.
485 bi->bi_acl_group = bdb_group;
486 bi->bi_acl_attribute = bdb_attribute;
488 bi->bi_acl_group = 0;
489 bi->bi_acl_attribute = 0;
492 bi->bi_chk_referrals = bdb_referrals;
493 bi->bi_entry_release_rw = bdb_entry_release;
496 * hooks for slap tools
498 bi->bi_tool_entry_open = bdb_tool_entry_open;
499 bi->bi_tool_entry_close = bdb_tool_entry_close;
500 bi->bi_tool_entry_first = bdb_tool_entry_next;
501 bi->bi_tool_entry_next = bdb_tool_entry_next;
502 bi->bi_tool_entry_get = bdb_tool_entry_get;
503 bi->bi_tool_entry_put = bdb_tool_entry_put;
504 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
505 bi->bi_tool_sync = 0;
507 bi->bi_connection_init = 0;
508 bi->bi_connection_destroy = 0;