1 /* init.c - initialize bdb backend */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2004 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
20 #include <ac/string.h>
21 #include <ac/unistd.h>
22 #include <ac/stdlib.h>
28 static const struct bdbi_database {
33 } bdbi_databases[] = {
34 { "id2entry" BDB_SUFFIX, "id2entry", DB_BTREE, 0 },
35 { "dn2id" BDB_SUFFIX, "dn2id", DB_BTREE, 0 },
39 struct berval bdb_uuid = BER_BVNULL;
41 typedef void * db_malloc(size_t);
42 typedef void * db_realloc(void *, size_t);
46 bdb_open( BackendInfo *bi )
52 bdb_destroy( BackendInfo *bi )
58 bdb_close( BackendInfo *bi )
60 /* terminate the underlying database system */
66 bdb_db_init( BackendDB *be )
70 Debug( LDAP_DEBUG_ANY,
71 "bdb_db_init: Initializing %s database\n",
72 be->bd_info->bi_type, 0, 0 );
74 /* allocate backend-database-specific stuff */
75 bdb = (struct bdb_info *) ch_calloc( 1, sizeof(struct bdb_info) );
77 /* DBEnv parameters */
78 bdb->bi_dbenv_home = ch_strdup( SLAPD_DEFAULT_DB_DIR );
79 bdb->bi_dbenv_xflags = 0;
80 bdb->bi_dbenv_mode = SLAPD_DEFAULT_DB_MODE;
82 bdb->bi_cache.c_maxsize = DEFAULT_CACHE_SIZE;
84 bdb->bi_lock_detect = DB_LOCK_DEFAULT;
85 bdb->bi_search_stack_depth = DEFAULT_SEARCH_STACK_DEPTH;
86 bdb->bi_search_stack = NULL;
88 LDAP_LIST_INIT (&bdb->bi_psearch_list);
90 ldap_pvt_thread_mutex_init( &bdb->bi_database_mutex );
91 ldap_pvt_thread_mutex_init( &bdb->bi_lastid_mutex );
92 ldap_pvt_thread_rdwr_init ( &bdb->bi_pslist_rwlock );
93 ldap_pvt_thread_mutex_init( &bdb->bi_cache.lru_mutex );
94 ldap_pvt_thread_mutex_init( &bdb->bi_cache.c_dntree.bei_kids_mutex );
95 ldap_pvt_thread_rdwr_init ( &bdb->bi_cache.c_rwlock );
108 unsigned char *u, *c;
114 #ifdef WORDS_BIGENDIAN
115 for( i = 0; i < (int)sizeof(ID); i++)
117 for( i = sizeof(ID)-1; i >= 0; i--)
128 bdb_db_open( BackendDB *be )
131 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
134 char path[MAXPATHLEN];
137 Debug( LDAP_DEBUG_ARGS,
139 be->be_suffix[0].bv_val, 0, 0 );
141 #ifndef BDB_MULTIPLE_SUFFIXES
142 if ( be->be_suffix[1].bv_val ) {
143 Debug( LDAP_DEBUG_ANY,
144 "bdb_db_open: only one suffix allowed\n", 0, 0, 0 );
148 /* we should check existance of dbenv_home and db_directory */
150 rc = db_env_create( &bdb->bi_dbenv, 0 );
152 Debug( LDAP_DEBUG_ANY,
153 "bdb_db_open: db_env_create failed: %s (%d)\n",
154 db_strerror(rc), rc, 0 );
158 flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE
159 | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN;
162 /* Never do automatic recovery, must perform it manually.
163 * Otherwise restarting with gentlehup will corrupt the
166 if( !(slapMode & SLAP_TOOL_MODE) ) flags |= DB_RECOVER;
169 /* If a key was set, use shared memory for the BDB environment */
170 if ( bdb->bi_shm_key ) {
171 bdb->bi_dbenv->set_shm_key( bdb->bi_dbenv, bdb->bi_shm_key );
172 flags |= DB_SYSTEM_MEM;
175 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
176 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
177 bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
179 /* One long-lived TXN per thread, two TXNs per write op */
180 bdb->bi_dbenv->set_tx_max( bdb->bi_dbenv, connection_pool_max * 3 );
182 if ( bdb->bi_idl_cache_max_size ) {
183 bdb->bi_idl_tree = NULL;
184 ldap_pvt_thread_rdwr_init( &bdb->bi_idl_tree_rwlock );
185 ldap_pvt_thread_mutex_init( &bdb->bi_idl_tree_lrulock );
186 bdb->bi_idl_cache_size = 0;
191 char dir[MAXPATHLEN], *ptr;
193 if (bdb->bi_dbenv_home[0] == '.') {
194 /* If home is a relative path, relative subdirs
195 * are just concat'd by BDB. We don't want the
196 * path to be concat'd twice, e.g.
197 * ./test-db/./test-db/tmp
201 ptr = lutil_strcopy( dir, bdb->bi_dbenv_home );
202 *ptr++ = LDAP_DIRSEP[0];
208 strcpy( ptr, BDB_TMP_SUBDIR );
212 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
214 Debug( LDAP_DEBUG_ANY,
215 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
216 dir, db_strerror(rc), rc );
220 strcpy( ptr, BDB_LG_SUBDIR );
224 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
226 Debug( LDAP_DEBUG_ANY,
227 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
228 dir, db_strerror(rc), rc );
232 strcpy( ptr, BDB_DATA_SUBDIR );
236 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
238 Debug( LDAP_DEBUG_ANY,
239 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
240 dir, db_strerror(rc), rc );
246 Debug( LDAP_DEBUG_TRACE,
247 "bdb_db_open: dbenv_open(%s)\n",
248 bdb->bi_dbenv_home, 0, 0);
251 strcpy( path, bdb->bi_dbenv_home );
253 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
256 bdb->bi_dbenv_mode );
258 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
261 bdb->bi_dbenv_mode );
264 Debug( LDAP_DEBUG_ANY,
265 "bdb_db_open: dbenv_open failed: %s (%d)\n",
266 db_strerror(rc), rc, 0 );
270 if( bdb->bi_dbenv_xflags != 0 ) {
271 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
272 bdb->bi_dbenv_xflags, 1);
274 Debug( LDAP_DEBUG_ANY,
275 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
276 db_strerror(rc), rc, 0 );
281 flags = DB_THREAD | bdb->bi_db_opflags;
283 bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
284 BDB_INDICES * sizeof(struct bdb_db_info *) );
286 /* open (and create) main database */
287 for( i = 0; bdbi_databases[i].name; i++ ) {
288 struct bdb_db_info *db;
290 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
292 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
294 Debug( LDAP_DEBUG_ANY,
295 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
296 bdb->bi_dbenv_home, db_strerror(rc), rc );
300 if( i == BDB_ID2ENTRY ) {
301 rc = db->bdi_db->set_bt_compare( db->bdi_db,
303 rc = db->bdi_db->set_pagesize( db->bdi_db,
304 BDB_ID2ENTRY_PAGESIZE );
305 if ( slapMode & SLAP_TOOL_READMAIN ) {
311 rc = db->bdi_db->set_flags( db->bdi_db,
312 DB_DUP | DB_DUPSORT );
314 rc = db->bdi_db->set_dup_compare( db->bdi_db,
316 if ( slapMode & SLAP_TOOL_READONLY ) {
322 rc = db->bdi_db->set_dup_compare( db->bdi_db,
324 rc = db->bdi_db->set_bt_compare( db->bdi_db,
326 if ( slapMode & (SLAP_TOOL_READONLY|SLAP_TOOL_READMAIN) ) {
332 rc = db->bdi_db->set_pagesize( db->bdi_db,
337 strcpy( path, bdbi_databases[i].file );
339 rc = DB_OPEN( db->bdi_db,
341 /* bdbi_databases[i].name, */ NULL,
342 bdbi_databases[i].type,
343 bdbi_databases[i].flags | flags,
344 bdb->bi_dbenv_mode );
346 rc = DB_OPEN( db->bdi_db,
347 bdbi_databases[i].file,
348 /* bdbi_databases[i].name, */ NULL,
349 bdbi_databases[i].type,
350 bdbi_databases[i].flags | flags,
351 bdb->bi_dbenv_mode );
355 Debug( LDAP_DEBUG_ANY,
356 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
357 bdb->bi_dbenv_home, db_strerror(rc), rc );
361 flags &= ~(DB_CREATE | DB_RDONLY);
362 db->bdi_name = bdbi_databases[i].name;
363 bdb->bi_databases[i] = db;
366 bdb->bi_databases[i] = NULL;
367 bdb->bi_ndatabases = i;
370 rc = bdb_last_id( be, NULL );
372 Debug( LDAP_DEBUG_ANY,
373 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
374 bdb->bi_dbenv_home, db_strerror(rc), rc );
378 XLOCK_ID(bdb->bi_dbenv, &bdb->bi_cache.c_locker);
380 /* <insert> open (and create) index databases */
385 bdb_db_close( BackendDB *be )
388 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
389 struct bdb_db_info *db;
390 bdb_idl_cache_entry_t *entry, *next_entry;
392 while( bdb->bi_ndatabases-- ) {
393 db = bdb->bi_databases[bdb->bi_ndatabases];
394 rc = db->bdi_db->close( db->bdi_db, 0 );
395 /* Lower numbered names are not strdup'd */
396 if( bdb->bi_ndatabases >= BDB_NDB )
397 free( db->bdi_name );
400 free( bdb->bi_databases );
401 bdb_attr_index_destroy( bdb->bi_attrs );
403 bdb_cache_release_all (&bdb->bi_cache);
405 if ( bdb->bi_idl_cache_max_size ) {
406 ldap_pvt_thread_rdwr_wlock ( &bdb->bi_idl_tree_rwlock );
407 avl_free( bdb->bi_idl_tree, NULL );
408 entry = bdb->bi_idl_lru_head;
409 while ( entry != NULL ) {
410 next_entry = entry->idl_lru_next;
413 free( entry->kstr.bv_val );
417 ldap_pvt_thread_rdwr_wunlock ( &bdb->bi_idl_tree_rwlock );
420 XLOCK_ID_FREE(bdb->bi_dbenv, bdb->bi_cache.c_locker);
426 bdb_db_destroy( BackendDB *be )
429 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
430 Operation *ps = NULL;
431 Operation *psn = NULL;
432 void *saved_tmpmemctx = NULL;
434 /* close db environment */
435 if( bdb->bi_dbenv ) {
436 /* force a checkpoint */
437 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
439 Debug( LDAP_DEBUG_ANY,
440 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
441 db_strerror(rc), rc, 0 );
444 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
445 bdb->bi_dbenv = NULL;
447 Debug( LDAP_DEBUG_ANY,
448 "bdb_db_destroy: close failed: %s (%d)\n",
449 db_strerror(rc), rc, 0 );
454 if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
456 ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
457 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_mutex );
458 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_dntree.bei_kids_mutex );
459 ldap_pvt_thread_rdwr_destroy ( &bdb->bi_pslist_rwlock );
460 ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
461 ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
462 if ( bdb->bi_idl_cache_max_size ) {
463 ldap_pvt_thread_rdwr_destroy( &bdb->bi_idl_tree_rwlock );
464 ldap_pvt_thread_mutex_destroy( &bdb->bi_idl_tree_lrulock );
467 ps = LDAP_LIST_FIRST( &bdb->bi_psearch_list );
470 psn = LDAP_LIST_NEXT( ps, o_ps_link );
472 saved_tmpmemctx = ps->o_tmpmemctx;
474 if (!BER_BVISNULL(&ps->o_req_dn)) {
475 slap_sl_free( ps->o_req_dn.bv_val, ps->o_tmpmemctx );
477 if (!BER_BVISNULL(&ps->o_req_ndn)) {
478 slap_sl_free( ps->o_req_ndn.bv_val, ps->o_tmpmemctx );
480 if (!BER_BVISNULL(&ps->ors_filterstr)) {
481 slap_sl_free(ps->ors_filterstr.bv_val, ps->o_tmpmemctx);
483 if (ps->ors_filter != NULL) {
484 filter_free_x(ps, ps->ors_filter);
486 if ( ps->ors_attrs != NULL) {
487 ps->o_tmpfree(ps->ors_attrs, ps->o_tmpmemctx);
492 if ( saved_tmpmemctx ) {
493 slap_sl_mem_destroy( NULL, saved_tmpmemctx );
499 psn = LDAP_LIST_NEXT( ps, o_ps_link );
501 saved_tmpmemctx = ps->o_tmpmemctx;
503 if (!BER_BVISNULL(&ps->o_req_dn)) {
504 slap_sl_free( ps->o_req_dn.bv_val, ps->o_tmpmemctx );
506 if (!BER_BVISNULL(&ps->o_req_ndn)) {
507 slap_sl_free( ps->o_req_ndn.bv_val, ps->o_tmpmemctx );
509 if (!BER_BVISNULL(&ps->ors_filterstr)) {
510 slap_sl_free(ps->ors_filterstr.bv_val, ps->o_tmpmemctx);
512 if (ps->ors_filter != NULL) {
513 filter_free_x(ps, ps->ors_filter);
515 if ( ps->ors_attrs != NULL) {
516 ps->o_tmpfree(ps->ors_attrs, ps->o_tmpmemctx);
521 if ( saved_tmpmemctx ) {
522 slap_sl_mem_destroy( NULL, saved_tmpmemctx );
527 be->be_private = NULL;
532 #if (SLAPD_BDB == SLAPD_MOD_DYNAMIC && !defined(BDB_HIER)) || \
533 (SLAPD_HDB == SLAPD_MOD_DYNAMIC && defined(BDB_HIER))
534 int init_module( int argc, char *argv[] ) {
537 memset( &bi, '\0', sizeof(bi) );
543 bi.bi_init = bdb_initialize;
548 #endif /* SLAPD_BDB */
554 static char *controls[] = {
556 LDAP_CONTROL_MANAGEDSAIT,
558 LDAP_CONTROL_PAGEDRESULTS,
559 #ifdef LDAP_CONTROL_SUBENTRIES
560 LDAP_CONTROL_SUBENTRIES,
562 LDAP_CONTROL_VALUESRETURNFILTER,
566 /* initialize the underlying database system */
567 Debug( LDAP_DEBUG_TRACE, "bdb_initialize: initialize BDB backend\n",
571 SLAP_BFLAG_INCREMENT |
572 #ifdef BDB_SUBENTRIES
573 SLAP_BFLAG_SUBENTRIES |
578 SLAP_BFLAG_REFERRALS;
580 bi->bi_controls = controls;
582 { /* version check */
583 int major, minor, patch, ver;
584 char *version = db_version( &major, &minor, &patch );
588 /* All our stdio does an ASCII to EBCDIC conversion on
589 * the output. Strings from the BDB library are already
590 * in EBCDIC; we have to go back and forth...
592 strcpy( v2, version );
597 ver = (major << 24) | (minor << 16) | patch;
598 if( ver < DB_VERSION_FULL )
600 Debug( LDAP_DEBUG_ANY,
601 "bdb_initialize: BDB library version mismatch:"
602 " expected " DB_VERSION_STRING ","
603 " got %s\n", version, 0, 0 );
606 Debug( LDAP_DEBUG_ANY, "bdb_initialize: %s\n",
610 db_env_set_func_free( ber_memfree );
611 db_env_set_func_malloc( (db_malloc *)ber_memalloc );
612 db_env_set_func_realloc( (db_realloc *)ber_memrealloc );
614 /* This is a no-op on a NO_THREAD build. Leave the default
615 * alone so that BDB will sleep on interprocess conflicts.
617 db_env_set_func_yield( ldap_pvt_thread_yield );
621 static char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
623 bdb_uuid.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ));
624 bdb_uuid.bv_val = uuidbuf;
632 bi->bi_db_init = bdb_db_init;
633 bi->bi_db_config = bdb_db_config;
634 bi->bi_db_open = bdb_db_open;
635 bi->bi_db_close = bdb_db_close;
636 bi->bi_db_destroy = bdb_db_destroy;
638 bi->bi_op_add = bdb_add;
639 bi->bi_op_bind = bdb_bind;
640 bi->bi_op_compare = bdb_compare;
641 bi->bi_op_delete = bdb_delete;
642 bi->bi_op_modify = bdb_modify;
643 bi->bi_op_modrdn = bdb_modrdn;
644 bi->bi_op_search = bdb_search;
646 bi->bi_op_unbind = 0;
648 bi->bi_op_abandon = bdb_abandon;
649 bi->bi_op_cancel = bdb_cancel;
651 bi->bi_extended = bdb_extended;
653 bi->bi_chk_referrals = bdb_referrals;
654 bi->bi_operational = bdb_operational;
655 bi->bi_has_subordinates = bdb_hasSubordinates;
656 bi->bi_entry_release_rw = bdb_entry_release;
657 bi->bi_entry_get_rw = bdb_entry_get;
660 * hooks for slap tools
662 bi->bi_tool_entry_open = bdb_tool_entry_open;
663 bi->bi_tool_entry_close = bdb_tool_entry_close;
664 bi->bi_tool_entry_first = bdb_tool_entry_next;
665 bi->bi_tool_entry_next = bdb_tool_entry_next;
666 bi->bi_tool_entry_get = bdb_tool_entry_get;
667 bi->bi_tool_entry_put = bdb_tool_entry_put;
668 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
669 bi->bi_tool_sync = 0;
670 bi->bi_tool_dn2id_get = bdb_tool_dn2id_get;
671 bi->bi_tool_id2entry_get = bdb_tool_id2entry_get;
672 bi->bi_tool_entry_modify = bdb_tool_entry_modify;
674 bi->bi_connection_init = 0;
675 bi->bi_connection_destroy = 0;