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 LDAP_XSTRING(bdb_db_init) ": Initializing "
72 BDB_UCTYPE " database\n", 0, 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;
89 LDAP_LIST_INIT (&bdb->bi_psearch_list);
90 ldap_pvt_thread_rdwr_init ( &bdb->bi_pslist_rwlock );
93 ldap_pvt_thread_mutex_init( &bdb->bi_database_mutex );
94 ldap_pvt_thread_mutex_init( &bdb->bi_lastid_mutex );
95 ldap_pvt_thread_mutex_init( &bdb->bi_cache.lru_mutex );
96 ldap_pvt_thread_mutex_init( &bdb->bi_cache.c_dntree.bei_kids_mutex );
97 ldap_pvt_thread_rdwr_init ( &bdb->bi_cache.c_rwlock );
110 unsigned char *u, *c;
116 #ifdef WORDS_BIGENDIAN
117 for( i = 0; i < (int)sizeof(ID); i++)
119 for( i = sizeof(ID)-1; i >= 0; i--)
130 bdb_checkpoint( void *ctx, void *arg )
132 struct re_s *rtask = arg;
133 struct bdb_info *bdb = rtask->arg;
135 TXN_CHECKPOINT( bdb->bi_dbenv, bdb->bi_txn_cp_kbyte,
136 bdb->bi_txn_cp_min, 0 );
141 bdb_db_open( BackendDB *be )
144 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
147 char path[MAXPATHLEN];
150 Debug( LDAP_DEBUG_ARGS,
152 be->be_suffix[0].bv_val, 0, 0 );
154 #ifndef BDB_MULTIPLE_SUFFIXES
155 if ( be->be_suffix[1].bv_val ) {
156 Debug( LDAP_DEBUG_ANY,
157 "bdb_db_open: only one suffix allowed\n", 0, 0, 0 );
161 /* we should check existance of dbenv_home and db_directory */
163 rc = db_env_create( &bdb->bi_dbenv, 0 );
165 Debug( LDAP_DEBUG_ANY,
166 "bdb_db_open: db_env_create failed: %s (%d)\n",
167 db_strerror(rc), rc, 0 );
171 flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE
172 | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN;
175 /* Never do automatic recovery, must perform it manually.
176 * Otherwise restarting with gentlehup will corrupt the
179 if( !(slapMode & SLAP_TOOL_MODE) ) flags |= DB_RECOVER;
182 /* If a key was set, use shared memory for the BDB environment */
183 if ( bdb->bi_shm_key ) {
184 bdb->bi_dbenv->set_shm_key( bdb->bi_dbenv, bdb->bi_shm_key );
185 flags |= DB_SYSTEM_MEM;
188 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
189 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
190 bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
192 /* One long-lived TXN per thread, two TXNs per write op */
193 bdb->bi_dbenv->set_tx_max( bdb->bi_dbenv, connection_pool_max * 3 );
195 if ( bdb->bi_idl_cache_max_size ) {
196 bdb->bi_idl_tree = NULL;
197 ldap_pvt_thread_rdwr_init( &bdb->bi_idl_tree_rwlock );
198 ldap_pvt_thread_mutex_init( &bdb->bi_idl_tree_lrulock );
199 bdb->bi_idl_cache_size = 0;
204 char dir[MAXPATHLEN], *ptr;
206 if (bdb->bi_dbenv_home[0] == '.') {
207 /* If home is a relative path, relative subdirs
208 * are just concat'd by BDB. We don't want the
209 * path to be concat'd twice, e.g.
210 * ./test-db/./test-db/tmp
214 ptr = lutil_strcopy( dir, bdb->bi_dbenv_home );
215 *ptr++ = LDAP_DIRSEP[0];
221 strcpy( ptr, BDB_TMP_SUBDIR );
225 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
227 Debug( LDAP_DEBUG_ANY,
228 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
229 dir, db_strerror(rc), rc );
233 strcpy( ptr, BDB_LG_SUBDIR );
237 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
239 Debug( LDAP_DEBUG_ANY,
240 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
241 dir, db_strerror(rc), rc );
245 strcpy( ptr, 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 if( bdb->bi_dbenv_xflags != 0 ) {
260 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
261 bdb->bi_dbenv_xflags, 1);
263 Debug( LDAP_DEBUG_ANY,
264 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
265 db_strerror(rc), rc, 0 );
270 Debug( LDAP_DEBUG_TRACE,
271 "bdb_db_open: dbenv_open(%s)\n",
272 bdb->bi_dbenv_home, 0, 0);
275 strcpy( path, bdb->bi_dbenv_home );
277 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
280 bdb->bi_dbenv_mode );
282 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
285 bdb->bi_dbenv_mode );
288 Debug( LDAP_DEBUG_ANY,
289 "bdb_db_open: dbenv_open failed: %s (%d)\n",
290 db_strerror(rc), rc, 0 );
294 flags = DB_THREAD | bdb->bi_db_opflags;
296 bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
297 BDB_INDICES * sizeof(struct bdb_db_info *) );
299 /* open (and create) main database */
300 for( i = 0; bdbi_databases[i].name; i++ ) {
301 struct bdb_db_info *db;
303 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
305 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
307 Debug( LDAP_DEBUG_ANY,
308 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
309 bdb->bi_dbenv_home, db_strerror(rc), rc );
313 if( i == BDB_ID2ENTRY ) {
315 rc = db->bdi_db->set_bt_compare( db->bdi_db,
318 rc = db->bdi_db->set_pagesize( db->bdi_db,
319 BDB_ID2ENTRY_PAGESIZE );
320 if ( slapMode & SLAP_TOOL_READMAIN ) {
326 rc = db->bdi_db->set_flags( db->bdi_db,
327 DB_DUP | DB_DUPSORT );
330 rc = db->bdi_db->set_dup_compare( db->bdi_db,
333 if ( slapMode & SLAP_TOOL_READONLY ) {
339 rc = db->bdi_db->set_dup_compare( db->bdi_db,
341 rc = db->bdi_db->set_bt_compare( db->bdi_db,
343 if ( slapMode & (SLAP_TOOL_READONLY|SLAP_TOOL_READMAIN) ) {
349 rc = db->bdi_db->set_pagesize( db->bdi_db,
354 strcpy( path, bdbi_databases[i].file );
356 rc = DB_OPEN( db->bdi_db,
358 /* bdbi_databases[i].name, */ NULL,
359 bdbi_databases[i].type,
360 bdbi_databases[i].flags | flags,
361 bdb->bi_dbenv_mode );
363 rc = DB_OPEN( db->bdi_db,
364 bdbi_databases[i].file,
365 /* bdbi_databases[i].name, */ NULL,
366 bdbi_databases[i].type,
367 bdbi_databases[i].flags | flags,
368 bdb->bi_dbenv_mode );
372 Debug( LDAP_DEBUG_ANY,
373 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
374 bdb->bi_dbenv_home, db_strerror(rc), rc );
378 flags &= ~(DB_CREATE | DB_RDONLY);
379 db->bdi_name = bdbi_databases[i].name;
380 bdb->bi_databases[i] = db;
383 bdb->bi_databases[i] = NULL;
384 bdb->bi_ndatabases = i;
387 rc = bdb_last_id( be, NULL );
389 Debug( LDAP_DEBUG_ANY,
390 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
391 bdb->bi_dbenv_home, db_strerror(rc), rc );
395 XLOCK_ID(bdb->bi_dbenv, &bdb->bi_cache.c_locker);
397 /* If we're in server mode and time-based checkpointing is enabled,
398 * submit a task to perform periodic checkpoints.
400 if ( slapMode & SLAP_SERVER_MODE && bdb->bi_txn_cp &&
401 bdb->bi_txn_cp_min ) {
402 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
403 ldap_pvt_runqueue_insert( &slapd_rq, bdb->bi_txn_cp_min*60,
404 bdb_checkpoint, bdb );
405 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
408 /* <insert> open (and create) index databases */
413 bdb_db_close( BackendDB *be )
416 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
417 struct bdb_db_info *db;
418 bdb_idl_cache_entry_t *entry, *next_entry;
420 while( bdb->bi_ndatabases-- ) {
421 db = bdb->bi_databases[bdb->bi_ndatabases];
422 rc = db->bdi_db->close( db->bdi_db, 0 );
423 /* Lower numbered names are not strdup'd */
424 if( bdb->bi_ndatabases >= BDB_NDB )
425 free( db->bdi_name );
428 free( bdb->bi_databases );
429 bdb_attr_index_destroy( bdb->bi_attrs );
431 bdb_cache_release_all (&bdb->bi_cache);
433 if ( bdb->bi_idl_cache_max_size ) {
434 ldap_pvt_thread_rdwr_wlock ( &bdb->bi_idl_tree_rwlock );
435 avl_free( bdb->bi_idl_tree, NULL );
436 entry = bdb->bi_idl_lru_head;
437 while ( entry != NULL ) {
438 next_entry = entry->idl_lru_next;
441 free( entry->kstr.bv_val );
445 ldap_pvt_thread_rdwr_wunlock ( &bdb->bi_idl_tree_rwlock );
448 XLOCK_ID_FREE(bdb->bi_dbenv, bdb->bi_cache.c_locker);
454 bdb_db_destroy( BackendDB *be )
457 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
458 Operation *ps = NULL;
459 Operation *psn = NULL;
460 void *saved_tmpmemctx = NULL;
462 /* close db environment */
463 if( bdb->bi_dbenv ) {
464 /* force a checkpoint */
465 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
467 Debug( LDAP_DEBUG_ANY,
468 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
469 db_strerror(rc), rc, 0 );
472 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
473 bdb->bi_dbenv = NULL;
475 Debug( LDAP_DEBUG_ANY,
476 "bdb_db_destroy: close failed: %s (%d)\n",
477 db_strerror(rc), rc, 0 );
482 if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
484 ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
485 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_mutex );
486 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_dntree.bei_kids_mutex );
487 ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
488 ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
489 if ( bdb->bi_idl_cache_max_size ) {
490 ldap_pvt_thread_rdwr_destroy( &bdb->bi_idl_tree_rwlock );
491 ldap_pvt_thread_mutex_destroy( &bdb->bi_idl_tree_lrulock );
495 ldap_pvt_thread_rdwr_destroy ( &bdb->bi_pslist_rwlock );
496 ps = LDAP_LIST_FIRST( &bdb->bi_psearch_list );
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 );
528 psn = LDAP_LIST_NEXT( ps, o_ps_link );
530 saved_tmpmemctx = ps->o_tmpmemctx;
532 if (!BER_BVISNULL(&ps->o_req_dn)) {
533 slap_sl_free( ps->o_req_dn.bv_val, ps->o_tmpmemctx );
535 if (!BER_BVISNULL(&ps->o_req_ndn)) {
536 slap_sl_free( ps->o_req_ndn.bv_val, ps->o_tmpmemctx );
538 if (!BER_BVISNULL(&ps->ors_filterstr)) {
539 slap_sl_free(ps->ors_filterstr.bv_val, ps->o_tmpmemctx);
541 if (ps->ors_filter != NULL) {
542 filter_free_x(ps, ps->ors_filter);
544 if ( ps->ors_attrs != NULL) {
545 ps->o_tmpfree(ps->ors_attrs, ps->o_tmpmemctx);
550 if ( saved_tmpmemctx ) {
551 slap_sl_mem_destroy( NULL, saved_tmpmemctx );
557 be->be_private = NULL;
566 static char *controls[] = {
568 LDAP_CONTROL_MANAGEDSAIT,
570 LDAP_CONTROL_PAGEDRESULTS,
571 #ifdef LDAP_CONTROL_SUBENTRIES
572 LDAP_CONTROL_SUBENTRIES,
574 LDAP_CONTROL_VALUESRETURNFILTER,
578 /* initialize the underlying database system */
579 Debug( LDAP_DEBUG_TRACE,
580 LDAP_XSTRING(bdb_back_initialize) ": initialize "
581 BDB_UCTYPE " backend\n", 0, 0, 0 );
584 SLAP_BFLAG_INCREMENT |
585 #ifdef BDB_SUBENTRIES
586 SLAP_BFLAG_SUBENTRIES |
589 SLAP_BFLAG_REFERRALS;
591 bi->bi_controls = controls;
593 { /* version check */
594 int major, minor, patch, ver;
595 char *version = db_version( &major, &minor, &patch );
599 /* All our stdio does an ASCII to EBCDIC conversion on
600 * the output. Strings from the BDB library are already
601 * in EBCDIC; we have to go back and forth...
603 strcpy( v2, version );
608 ver = (major << 24) | (minor << 16) | patch;
609 if( ver < DB_VERSION_FULL )
611 Debug( LDAP_DEBUG_ANY,
612 LDAP_XSTRING(bdb_back_initialize) ": "
613 "BDB library version mismatch:"
614 " expected " DB_VERSION_STRING ","
615 " got %s\n", version, 0, 0 );
618 Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(bdb_back_initialize)
619 ": %s\n", version, 0, 0 );
622 db_env_set_func_free( ber_memfree );
623 db_env_set_func_malloc( (db_malloc *)ber_memalloc );
624 db_env_set_func_realloc( (db_realloc *)ber_memrealloc );
626 /* This is a no-op on a NO_THREAD build. Leave the default
627 * alone so that BDB will sleep on interprocess conflicts.
629 db_env_set_func_yield( ldap_pvt_thread_yield );
633 static char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
635 bdb_uuid.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ));
636 bdb_uuid.bv_val = uuidbuf;
644 bi->bi_db_init = bdb_db_init;
645 bi->bi_db_config = bdb_db_config;
646 bi->bi_db_open = bdb_db_open;
647 bi->bi_db_close = bdb_db_close;
648 bi->bi_db_destroy = bdb_db_destroy;
650 bi->bi_op_add = bdb_add;
651 bi->bi_op_bind = bdb_bind;
652 bi->bi_op_compare = bdb_compare;
653 bi->bi_op_delete = bdb_delete;
654 bi->bi_op_modify = bdb_modify;
655 bi->bi_op_modrdn = bdb_modrdn;
656 bi->bi_op_search = bdb_search;
658 bi->bi_op_unbind = 0;
660 #if 0 /* DELETE ME */
661 bi->bi_op_abandon = bdb_abandon;
662 bi->bi_op_cancel = bdb_cancel;
665 bi->bi_extended = bdb_extended;
667 bi->bi_chk_referrals = bdb_referrals;
668 bi->bi_operational = bdb_operational;
669 bi->bi_has_subordinates = bdb_hasSubordinates;
670 bi->bi_entry_release_rw = bdb_entry_release;
671 bi->bi_entry_get_rw = bdb_entry_get;
674 * hooks for slap tools
676 bi->bi_tool_entry_open = bdb_tool_entry_open;
677 bi->bi_tool_entry_close = bdb_tool_entry_close;
678 bi->bi_tool_entry_first = bdb_tool_entry_next;
679 bi->bi_tool_entry_next = bdb_tool_entry_next;
680 bi->bi_tool_entry_get = bdb_tool_entry_get;
681 bi->bi_tool_entry_put = bdb_tool_entry_put;
682 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
683 bi->bi_tool_sync = 0;
684 bi->bi_tool_dn2id_get = bdb_tool_dn2id_get;
685 bi->bi_tool_id2entry_get = bdb_tool_id2entry_get;
686 bi->bi_tool_entry_modify = bdb_tool_entry_modify;
688 bi->bi_connection_init = 0;
689 bi->bi_connection_destroy = 0;
694 #if (SLAPD_BDB == SLAPD_MOD_DYNAMIC && !defined(BDB_HIER)) || \
695 (SLAPD_HDB == SLAPD_MOD_DYNAMIC && defined(BDB_HIER))
697 /* conditionally define the init_module() function */
699 SLAP_BACKEND_INIT_MODULE( hdb )
700 #else /* !BDB_HIER */
701 SLAP_BACKEND_INIT_MODULE( bdb )
702 #endif /* !BDB_HIER */
704 #endif /* SLAPD_[BH]DB == SLAPD_MOD_DYNAMIC */