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 },
33 struct berval bdb_uuid = { 0, NULL };
36 bdb_open( BackendInfo *bi )
38 static char *controls[] = {
39 LDAP_CONTROL_MANAGEDSAIT,
40 LDAP_CONTROL_SUBENTRIES,
45 bi->bi_controls = controls;
47 /* initialize the underlying database system */
48 Debug( LDAP_DEBUG_TRACE, "bdb_open: initialize BDB backend\n",
52 int major, minor, patch;
53 char *version = db_version( &major, &minor, &patch );
55 if( major != DB_VERSION_MAJOR ||
56 minor != DB_VERSION_MINOR ||
57 patch < DB_VERSION_PATCH )
59 Debug( LDAP_DEBUG_ANY,
60 "bdb_open: version mismatch\n"
61 "\texpected: " DB_VERSION_STRING "\n"
62 "\tgot: %s \n", version, 0, 0 );
65 Debug( LDAP_DEBUG_ANY, "bdb_open: %s\n",
70 db_env_set_func_malloc( ch_malloc );
71 db_env_set_func_realloc( ch_realloc );
72 db_env_set_func_free( ch_free );
75 db_env_set_func_yield( ldap_pvt_thread_yield );
78 static char uuidbuf[40];
80 bdb_uuid.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ));
81 bdb_uuid.bv_val = uuidbuf;
89 bdb_destroy( BackendInfo *bi )
95 bdb_close( BackendInfo *bi )
97 /* terminate the underlying database system */
103 bdb_db_init( BackendDB *be )
105 struct bdb_info *bdb;
107 Debug( LDAP_DEBUG_ANY,
108 "bdb_db_init: Initializing BDB database\n",
111 /* indicate system schema supported */
113 #ifdef BDB_SUBENTRIES
114 SLAP_BFLAG_SUBENTRIES |
119 SLAP_BFLAG_REFERRALS;
121 /* allocate backend-database-specific stuff */
122 bdb = (struct bdb_info *) ch_calloc( 1, sizeof(struct bdb_info) );
124 /* DBEnv parameters */
125 bdb->bi_dbenv_home = ch_strdup( SLAPD_DEFAULT_DB_DIR );
126 bdb->bi_dbenv_xflags = 0;
127 bdb->bi_dbenv_mode = SLAPD_DEFAULT_DB_MODE;
129 bdb->bi_cache.c_maxsize = DEFAULT_CACHE_SIZE;
133 bdb->bi_lock_detect = DB_LOCK_NORUN;
135 bdb->bi_lock_detect = DB_LOCK_DEFAULT;
139 ldap_pvt_thread_mutex_init( &bdb->bi_database_mutex );
140 ldap_pvt_thread_mutex_init( &bdb->bi_lastid_mutex );
141 ldap_pvt_thread_mutex_init( &bdb->bi_cache.c_mutex );
143 ldap_pvt_thread_rdwr_init( &bdb->bi_tree_rdwr );
146 be->be_private = bdb;
150 #if 0 /* ifndef NO_THREADS */
151 static void *lock_detect_task( void *arg )
153 struct bdb_info *bdb = (struct bdb_info *) arg;
155 while( bdb->bi_dbenv != NULL ) {
158 sleep( bdb->bi_lock_detect_seconds );
160 rc = LOCK_DETECT( bdb->bi_dbenv, 0,
161 bdb->bi_lock_detect, &aborted );
167 Debug( LDAP_DEBUG_ANY,
168 "bdb_lock_detect: aborted %d locks\n",
183 unsigned char *u, *c;
189 #ifdef WORDS_BIGENDIAN
190 for( i = 0; i < sizeof(ID); i++)
192 for( i = sizeof(ID)-1; i >= 0; i--)
202 bdb_db_open( BackendDB *be )
205 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
208 Debug( LDAP_DEBUG_ARGS,
210 be->be_suffix[0]->bv_val, 0, 0 );
212 /* we should check existance of dbenv_home and db_directory */
214 rc = db_env_create( &bdb->bi_dbenv, 0 );
216 Debug( LDAP_DEBUG_ANY,
217 "bdb_db_open: db_env_create failed: %s (%d)\n",
218 db_strerror(rc), rc, 0 );
222 flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE
223 | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN | DB_RECOVER;
225 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0]->bv_val );
226 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
228 bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
233 char dir[MAXPATHLEN];
234 size_t len = strlen( bdb->bi_dbenv_home );
236 strcpy( dir, bdb->bi_dbenv_home );
237 strcat( &dir[len], BDB_TMP_SUBDIR );
239 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
241 Debug( LDAP_DEBUG_ANY,
242 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
243 dir, db_strerror(rc), rc );
247 strcat( &dir[len], BDB_LG_SUBDIR );
249 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
251 Debug( LDAP_DEBUG_ANY,
252 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
253 dir, db_strerror(rc), rc );
257 strcat( &dir[len], BDB_DATA_SUBDIR );
259 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
261 Debug( LDAP_DEBUG_ANY,
262 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
263 dir, db_strerror(rc), rc );
269 Debug( LDAP_DEBUG_TRACE,
270 "bdb_db_open: dbenv_open(%s)\n",
271 bdb->bi_dbenv_home, 0, 0);
273 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
276 bdb->bi_dbenv_mode );
278 Debug( LDAP_DEBUG_ANY,
279 "bdb_db_open: dbenv_open failed: %s (%d)\n",
280 db_strerror(rc), rc, 0 );
284 if( bdb->bi_dbenv_xflags != 0 ) {
285 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
286 bdb->bi_dbenv_xflags, 1);
288 Debug( LDAP_DEBUG_ANY,
289 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
290 db_strerror(rc), rc, 0 );
295 flags = DB_THREAD | DB_CREATE | bdb->bi_db_opflags;
297 bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
298 BDB_INDICES * sizeof(struct bdb_db_info *) );
300 /* open (and create) main database */
301 for( i = 0; bdbi_databases[i].name; i++ ) {
302 struct bdb_db_info *db;
304 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
306 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
308 Debug( LDAP_DEBUG_ANY,
309 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
310 bdb->bi_dbenv_home, db_strerror(rc), rc );
314 if( i == BDB_ID2ENTRY ) {
315 rc = db->bdi_db->set_bt_compare( db->bdi_db,
317 rc = db->bdi_db->set_pagesize( db->bdi_db,
318 BDB_ID2ENTRY_PAGESIZE );
321 rc = db->bdi_db->set_bt_compare( db->bdi_db,
323 #elif defined(BDB_IDL_MULTI)
324 rc = db->bdi_db->set_flags( db->bdi_db,
325 DB_DUP | DB_DUPSORT );
326 rc = db->bdi_db->set_dup_compare( db->bdi_db,
329 rc = db->bdi_db->set_pagesize( db->bdi_db,
333 rc = db->bdi_db->open( db->bdi_db,
334 bdbi_databases[i].file,
335 /* bdbi_databases[i].name, */ NULL,
336 bdbi_databases[i].type,
337 bdbi_databases[i].flags | flags,
338 bdb->bi_dbenv_mode );
341 Debug( LDAP_DEBUG_ANY,
342 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
343 bdb->bi_dbenv_home, db_strerror(rc), rc );
347 db->bdi_name = bdbi_databases[i].name;
348 bdb->bi_databases[i] = db;
351 bdb->bi_databases[i] = NULL;
352 bdb->bi_ndatabases = i;
355 rc = bdb_last_id( be, NULL );
357 Debug( LDAP_DEBUG_ANY,
358 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
359 bdb->bi_dbenv_home, db_strerror(rc), rc );
363 /* <insert> open (and create) index databases */
365 rc = bdb_build_tree( be );
368 #if 0 /* ifndef NO_THREADS */
369 if( bdb->bi_lock_detect != DB_LOCK_NORUN ) {
370 /* listener as a separate THREAD */
371 rc = ldap_pvt_thread_create( &bdb->bi_lock_detect_tid,
372 1, lock_detect_task, bdb );
379 bdb_db_close( BackendDB *be )
382 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
383 struct bdb_db_info *db;
385 while( bdb->bi_ndatabases-- ) {
386 db = bdb->bi_databases[bdb->bi_ndatabases];
387 rc = db->bdi_db->close( db->bdi_db, 0 );
388 /* Lower numbered names are not strdup'd */
389 if( bdb->bi_ndatabases >= BDB_NDB )
390 free( db->bdi_name );
393 free( bdb->bi_databases );
394 bdb_attr_index_destroy( bdb->bi_attrs );
396 bdb_cache_release_all (&bdb->bi_cache);
402 bdb_db_destroy( BackendDB *be )
405 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
407 /* close db environment */
408 if( bdb->bi_dbenv ) {
409 /* force a checkpoint */
410 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
412 Debug( LDAP_DEBUG_ANY,
413 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
414 db_strerror(rc), rc, 0 );
417 bdb_cache_release_all (&bdb->bi_cache);
419 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
420 bdb->bi_dbenv = NULL;
422 Debug( LDAP_DEBUG_ANY,
423 "bdb_db_destroy: close failed: %s (%d)\n",
424 db_strerror(rc), rc, 0 );
432 #ifdef SLAPD_BDB_DYNAMIC
433 int back_bdb_LTX_init_module( int argc, char *argv[] ) {
436 memset( &bi, '\0', sizeof(bi) );
438 bi.bi_init = bdb_initialize;
443 #endif /* SLAPD_BDB_DYNAMIC */
450 bi->bi_open = bdb_open;
455 bi->bi_db_init = bdb_db_init;
456 bi->bi_db_config = bdb_db_config;
457 bi->bi_db_open = bdb_db_open;
458 bi->bi_db_close = bdb_db_close;
459 bi->bi_db_destroy = bdb_db_destroy;
461 bi->bi_op_add = bdb_add;
462 bi->bi_op_bind = bdb_bind;
463 bi->bi_op_compare = bdb_compare;
464 bi->bi_op_delete = bdb_delete;
465 bi->bi_op_modify = bdb_modify;
466 bi->bi_op_modrdn = bdb_modrdn;
467 bi->bi_op_search = bdb_search;
469 bi->bi_op_unbind = 0;
470 bi->bi_op_abandon = 0;
472 bi->bi_extended = bdb_extended;
476 * these routines (and their callers) are not yet designed
477 * to work with transaction. Using them may cause deadlock.
479 bi->bi_acl_group = bdb_group;
480 bi->bi_acl_attribute = bdb_attribute;
482 bi->bi_acl_group = 0;
483 bi->bi_acl_attribute = 0;
486 bi->bi_chk_referrals = bdb_referrals;
487 bi->bi_entry_release_rw = bdb_entry_release;
490 * hooks for slap tools
492 bi->bi_tool_entry_open = bdb_tool_entry_open;
493 bi->bi_tool_entry_close = bdb_tool_entry_close;
494 bi->bi_tool_entry_first = bdb_tool_entry_next;
495 bi->bi_tool_entry_next = bdb_tool_entry_next;
496 bi->bi_tool_entry_get = bdb_tool_entry_get;
497 bi->bi_tool_entry_put = bdb_tool_entry_put;
498 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
499 bi->bi_tool_sync = 0;
501 bi->bi_connection_init = 0;
502 bi->bi_connection_destroy = 0;