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 ) {
314 rc = db->bdi_db->set_bt_compare( db->bdi_db,
316 rc = db->bdi_db->set_pagesize( db->bdi_db,
317 BDB_ID2ENTRY_PAGESIZE );
318 if ( slapMode & SLAP_TOOL_READMAIN ) {
324 rc = db->bdi_db->set_flags( db->bdi_db,
325 DB_DUP | DB_DUPSORT );
327 rc = db->bdi_db->set_dup_compare( db->bdi_db,
329 if ( slapMode & SLAP_TOOL_READONLY ) {
335 rc = db->bdi_db->set_dup_compare( db->bdi_db,
337 rc = db->bdi_db->set_bt_compare( db->bdi_db,
339 if ( slapMode & (SLAP_TOOL_READONLY|SLAP_TOOL_READMAIN) ) {
345 rc = db->bdi_db->set_pagesize( db->bdi_db,
350 strcpy( path, bdbi_databases[i].file );
352 rc = DB_OPEN( db->bdi_db,
354 /* bdbi_databases[i].name, */ NULL,
355 bdbi_databases[i].type,
356 bdbi_databases[i].flags | flags,
357 bdb->bi_dbenv_mode );
359 rc = DB_OPEN( db->bdi_db,
360 bdbi_databases[i].file,
361 /* bdbi_databases[i].name, */ NULL,
362 bdbi_databases[i].type,
363 bdbi_databases[i].flags | flags,
364 bdb->bi_dbenv_mode );
368 Debug( LDAP_DEBUG_ANY,
369 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
370 bdb->bi_dbenv_home, db_strerror(rc), rc );
374 flags &= ~(DB_CREATE | DB_RDONLY);
375 db->bdi_name = bdbi_databases[i].name;
376 bdb->bi_databases[i] = db;
379 bdb->bi_databases[i] = NULL;
380 bdb->bi_ndatabases = i;
383 rc = bdb_last_id( be, NULL );
385 Debug( LDAP_DEBUG_ANY,
386 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
387 bdb->bi_dbenv_home, db_strerror(rc), rc );
391 XLOCK_ID(bdb->bi_dbenv, &bdb->bi_cache.c_locker);
393 /* If we're in server mode and time-based checkpointing is enabled,
394 * submit a task to perform periodic checkpoints.
396 if ( slapMode & SLAP_SERVER_MODE && bdb->bi_txn_cp &&
397 bdb->bi_txn_cp_min ) {
398 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
399 ldap_pvt_runqueue_insert( &slapd_rq, bdb->bi_txn_cp_min*60,
400 bdb_checkpoint, bdb );
401 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
404 /* <insert> open (and create) index databases */
409 bdb_db_close( BackendDB *be )
412 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
413 struct bdb_db_info *db;
414 bdb_idl_cache_entry_t *entry, *next_entry;
416 while( bdb->bi_ndatabases-- ) {
417 db = bdb->bi_databases[bdb->bi_ndatabases];
418 rc = db->bdi_db->close( db->bdi_db, 0 );
419 /* Lower numbered names are not strdup'd */
420 if( bdb->bi_ndatabases >= BDB_NDB )
421 free( db->bdi_name );
424 free( bdb->bi_databases );
425 bdb_attr_index_destroy( bdb->bi_attrs );
427 bdb_cache_release_all (&bdb->bi_cache);
429 if ( bdb->bi_idl_cache_max_size ) {
430 ldap_pvt_thread_rdwr_wlock ( &bdb->bi_idl_tree_rwlock );
431 avl_free( bdb->bi_idl_tree, NULL );
432 entry = bdb->bi_idl_lru_head;
433 while ( entry != NULL ) {
434 next_entry = entry->idl_lru_next;
437 free( entry->kstr.bv_val );
441 ldap_pvt_thread_rdwr_wunlock ( &bdb->bi_idl_tree_rwlock );
444 XLOCK_ID_FREE(bdb->bi_dbenv, bdb->bi_cache.c_locker);
450 bdb_db_destroy( BackendDB *be )
453 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
454 Operation *ps = NULL;
455 Operation *psn = NULL;
456 void *saved_tmpmemctx = NULL;
458 /* close db environment */
459 if( bdb->bi_dbenv ) {
460 /* force a checkpoint */
461 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
463 Debug( LDAP_DEBUG_ANY,
464 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
465 db_strerror(rc), rc, 0 );
468 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
469 bdb->bi_dbenv = NULL;
471 Debug( LDAP_DEBUG_ANY,
472 "bdb_db_destroy: close failed: %s (%d)\n",
473 db_strerror(rc), rc, 0 );
478 if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
480 ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
481 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_mutex );
482 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_dntree.bei_kids_mutex );
483 ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
484 ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
485 if ( bdb->bi_idl_cache_max_size ) {
486 ldap_pvt_thread_rdwr_destroy( &bdb->bi_idl_tree_rwlock );
487 ldap_pvt_thread_mutex_destroy( &bdb->bi_idl_tree_lrulock );
491 ldap_pvt_thread_rdwr_destroy ( &bdb->bi_pslist_rwlock );
492 ps = LDAP_LIST_FIRST( &bdb->bi_psearch_list );
495 psn = LDAP_LIST_NEXT( ps, o_ps_link );
497 saved_tmpmemctx = ps->o_tmpmemctx;
499 if (!BER_BVISNULL(&ps->o_req_dn)) {
500 slap_sl_free( ps->o_req_dn.bv_val, ps->o_tmpmemctx );
502 if (!BER_BVISNULL(&ps->o_req_ndn)) {
503 slap_sl_free( ps->o_req_ndn.bv_val, ps->o_tmpmemctx );
505 if (!BER_BVISNULL(&ps->ors_filterstr)) {
506 slap_sl_free(ps->ors_filterstr.bv_val, ps->o_tmpmemctx);
508 if (ps->ors_filter != NULL) {
509 filter_free_x(ps, ps->ors_filter);
511 if ( ps->ors_attrs != NULL) {
512 ps->o_tmpfree(ps->ors_attrs, ps->o_tmpmemctx);
517 if ( saved_tmpmemctx ) {
518 slap_sl_mem_destroy( NULL, saved_tmpmemctx );
524 psn = LDAP_LIST_NEXT( ps, o_ps_link );
526 saved_tmpmemctx = ps->o_tmpmemctx;
528 if (!BER_BVISNULL(&ps->o_req_dn)) {
529 slap_sl_free( ps->o_req_dn.bv_val, ps->o_tmpmemctx );
531 if (!BER_BVISNULL(&ps->o_req_ndn)) {
532 slap_sl_free( ps->o_req_ndn.bv_val, ps->o_tmpmemctx );
534 if (!BER_BVISNULL(&ps->ors_filterstr)) {
535 slap_sl_free(ps->ors_filterstr.bv_val, ps->o_tmpmemctx);
537 if (ps->ors_filter != NULL) {
538 filter_free_x(ps, ps->ors_filter);
540 if ( ps->ors_attrs != NULL) {
541 ps->o_tmpfree(ps->ors_attrs, ps->o_tmpmemctx);
546 if ( saved_tmpmemctx ) {
547 slap_sl_mem_destroy( NULL, saved_tmpmemctx );
553 be->be_private = NULL;
562 static char *controls[] = {
564 LDAP_CONTROL_MANAGEDSAIT,
566 LDAP_CONTROL_PAGEDRESULTS,
567 #ifdef LDAP_CONTROL_SUBENTRIES
568 LDAP_CONTROL_SUBENTRIES,
570 LDAP_CONTROL_VALUESRETURNFILTER,
574 /* initialize the underlying database system */
575 Debug( LDAP_DEBUG_TRACE,
576 LDAP_XSTRING(bdb_back_initialize) ": initialize "
577 BDB_UCTYPE " backend\n", 0, 0, 0 );
580 SLAP_BFLAG_INCREMENT |
581 #ifdef BDB_SUBENTRIES
582 SLAP_BFLAG_SUBENTRIES |
585 SLAP_BFLAG_REFERRALS;
587 bi->bi_controls = controls;
589 { /* version check */
590 int major, minor, patch, ver;
591 char *version = db_version( &major, &minor, &patch );
595 /* All our stdio does an ASCII to EBCDIC conversion on
596 * the output. Strings from the BDB library are already
597 * in EBCDIC; we have to go back and forth...
599 strcpy( v2, version );
604 ver = (major << 24) | (minor << 16) | patch;
605 if( ver < DB_VERSION_FULL )
607 Debug( LDAP_DEBUG_ANY,
608 LDAP_XSTRING(bdb_back_initialize) ": "
609 "BDB library version mismatch:"
610 " expected " DB_VERSION_STRING ","
611 " got %s\n", version, 0, 0 );
614 Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(bdb_back_initialize)
615 ": %s\n", version, 0, 0 );
618 db_env_set_func_free( ber_memfree );
619 db_env_set_func_malloc( (db_malloc *)ber_memalloc );
620 db_env_set_func_realloc( (db_realloc *)ber_memrealloc );
622 /* This is a no-op on a NO_THREAD build. Leave the default
623 * alone so that BDB will sleep on interprocess conflicts.
625 db_env_set_func_yield( ldap_pvt_thread_yield );
629 static char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
631 bdb_uuid.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ));
632 bdb_uuid.bv_val = uuidbuf;
640 bi->bi_db_init = bdb_db_init;
641 bi->bi_db_config = bdb_db_config;
642 bi->bi_db_open = bdb_db_open;
643 bi->bi_db_close = bdb_db_close;
644 bi->bi_db_destroy = bdb_db_destroy;
646 bi->bi_op_add = bdb_add;
647 bi->bi_op_bind = bdb_bind;
648 bi->bi_op_compare = bdb_compare;
649 bi->bi_op_delete = bdb_delete;
650 bi->bi_op_modify = bdb_modify;
651 bi->bi_op_modrdn = bdb_modrdn;
652 bi->bi_op_search = bdb_search;
654 bi->bi_op_unbind = 0;
656 #if 0 /* DELETE ME */
657 bi->bi_op_abandon = bdb_abandon;
658 bi->bi_op_cancel = bdb_cancel;
661 bi->bi_extended = bdb_extended;
663 bi->bi_chk_referrals = bdb_referrals;
664 bi->bi_operational = bdb_operational;
665 bi->bi_has_subordinates = bdb_hasSubordinates;
666 bi->bi_entry_release_rw = bdb_entry_release;
667 bi->bi_entry_get_rw = bdb_entry_get;
670 * hooks for slap tools
672 bi->bi_tool_entry_open = bdb_tool_entry_open;
673 bi->bi_tool_entry_close = bdb_tool_entry_close;
674 bi->bi_tool_entry_first = bdb_tool_entry_next;
675 bi->bi_tool_entry_next = bdb_tool_entry_next;
676 bi->bi_tool_entry_get = bdb_tool_entry_get;
677 bi->bi_tool_entry_put = bdb_tool_entry_put;
678 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
679 bi->bi_tool_sync = 0;
680 bi->bi_tool_dn2id_get = bdb_tool_dn2id_get;
681 bi->bi_tool_id2entry_get = bdb_tool_id2entry_get;
682 bi->bi_tool_entry_modify = bdb_tool_entry_modify;
684 bi->bi_connection_init = 0;
685 bi->bi_connection_destroy = 0;
690 #if (SLAPD_BDB == SLAPD_MOD_DYNAMIC && !defined(BDB_HIER)) || \
691 (SLAPD_HDB == SLAPD_MOD_DYNAMIC && defined(BDB_HIER))
693 /* conditionally define the init_module() function */
695 SLAP_BACKEND_INIT_MODULE( hdb )
696 #else /* !BDB_HIER */
697 SLAP_BACKEND_INIT_MODULE( bdb )
698 #endif /* !BDB_HIER */
700 #endif /* SLAPD_[BH]DB == SLAPD_MOD_DYNAMIC */