1 /* init.c - initialize bdb backend */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2005 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 #ifdef SLAP_ZONE_ALLOC
196 if ( bdb->bi_cache.c_maxsize ) {
197 bdb->bi_cache.c_zctx = slap_zn_mem_create(
205 if ( bdb->bi_idl_cache_max_size ) {
206 bdb->bi_idl_tree = NULL;
207 ldap_pvt_thread_rdwr_init( &bdb->bi_idl_tree_rwlock );
208 ldap_pvt_thread_mutex_init( &bdb->bi_idl_tree_lrulock );
209 bdb->bi_idl_cache_size = 0;
214 char dir[MAXPATHLEN], *ptr;
216 if (bdb->bi_dbenv_home[0] == '.') {
217 /* If home is a relative path, relative subdirs
218 * are just concat'd by BDB. We don't want the
219 * path to be concat'd twice, e.g.
220 * ./test-db/./test-db/tmp
224 ptr = lutil_strcopy( dir, bdb->bi_dbenv_home );
225 *ptr++ = LDAP_DIRSEP[0];
231 strcpy( ptr, BDB_TMP_SUBDIR );
235 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
237 Debug( LDAP_DEBUG_ANY,
238 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
239 dir, db_strerror(rc), rc );
243 strcpy( ptr, BDB_LG_SUBDIR );
247 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
249 Debug( LDAP_DEBUG_ANY,
250 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
251 dir, db_strerror(rc), rc );
255 strcpy( ptr, 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 if( bdb->bi_dbenv_xflags != 0 ) {
270 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
271 bdb->bi_dbenv_xflags, 1);
273 Debug( LDAP_DEBUG_ANY,
274 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
275 db_strerror(rc), rc, 0 );
280 Debug( LDAP_DEBUG_TRACE,
281 "bdb_db_open: dbenv_open(%s)\n",
282 bdb->bi_dbenv_home, 0, 0);
285 strcpy( path, bdb->bi_dbenv_home );
287 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
290 bdb->bi_dbenv_mode );
292 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
295 bdb->bi_dbenv_mode );
298 Debug( LDAP_DEBUG_ANY,
299 "bdb_db_open: dbenv_open failed: %s (%d)\n",
300 db_strerror(rc), rc, 0 );
304 flags = DB_THREAD | bdb->bi_db_opflags;
306 bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
307 BDB_INDICES * sizeof(struct bdb_db_info *) );
309 /* open (and create) main database */
310 for( i = 0; bdbi_databases[i].name; i++ ) {
311 struct bdb_db_info *db;
313 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
315 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
317 Debug( LDAP_DEBUG_ANY,
318 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
319 bdb->bi_dbenv_home, db_strerror(rc), rc );
323 if( i == BDB_ID2ENTRY ) {
325 rc = db->bdi_db->set_bt_compare( db->bdi_db,
328 rc = db->bdi_db->set_pagesize( db->bdi_db,
329 BDB_ID2ENTRY_PAGESIZE );
330 if ( slapMode & SLAP_TOOL_READMAIN ) {
336 rc = db->bdi_db->set_flags( db->bdi_db,
337 DB_DUP | DB_DUPSORT );
340 rc = db->bdi_db->set_dup_compare( db->bdi_db,
343 if ( slapMode & SLAP_TOOL_READONLY ) {
349 rc = db->bdi_db->set_dup_compare( db->bdi_db,
351 rc = db->bdi_db->set_bt_compare( db->bdi_db,
353 if ( slapMode & (SLAP_TOOL_READONLY|SLAP_TOOL_READMAIN) ) {
359 rc = db->bdi_db->set_pagesize( db->bdi_db,
364 strcpy( path, bdbi_databases[i].file );
366 rc = DB_OPEN( db->bdi_db,
368 /* bdbi_databases[i].name, */ NULL,
369 bdbi_databases[i].type,
370 bdbi_databases[i].flags | flags,
371 bdb->bi_dbenv_mode );
373 rc = DB_OPEN( db->bdi_db,
374 bdbi_databases[i].file,
375 /* bdbi_databases[i].name, */ NULL,
376 bdbi_databases[i].type,
377 bdbi_databases[i].flags | flags,
378 bdb->bi_dbenv_mode );
382 Debug( LDAP_DEBUG_ANY,
383 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
384 bdb->bi_dbenv_home, db_strerror(rc), rc );
388 flags &= ~(DB_CREATE | DB_RDONLY);
389 db->bdi_name = bdbi_databases[i].name;
390 bdb->bi_databases[i] = db;
393 bdb->bi_databases[i] = NULL;
394 bdb->bi_ndatabases = i;
397 rc = bdb_last_id( be, NULL );
399 Debug( LDAP_DEBUG_ANY,
400 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
401 bdb->bi_dbenv_home, db_strerror(rc), rc );
405 XLOCK_ID(bdb->bi_dbenv, &bdb->bi_cache.c_locker);
407 /* If we're in server mode and time-based checkpointing is enabled,
408 * submit a task to perform periodic checkpoints.
410 if ( slapMode & SLAP_SERVER_MODE && bdb->bi_txn_cp &&
411 bdb->bi_txn_cp_min ) {
412 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
413 ldap_pvt_runqueue_insert( &slapd_rq, bdb->bi_txn_cp_min*60,
414 bdb_checkpoint, bdb );
415 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
418 /* <insert> open (and create) index databases */
423 bdb_db_close( BackendDB *be )
426 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
427 struct bdb_db_info *db;
428 bdb_idl_cache_entry_t *entry, *next_entry;
430 while( bdb->bi_ndatabases-- ) {
431 db = bdb->bi_databases[bdb->bi_ndatabases];
432 rc = db->bdi_db->close( db->bdi_db, 0 );
433 /* Lower numbered names are not strdup'd */
434 if( bdb->bi_ndatabases >= BDB_NDB )
435 free( db->bdi_name );
438 free( bdb->bi_databases );
439 bdb_attr_index_destroy( bdb->bi_attrs );
441 bdb_cache_release_all (&bdb->bi_cache);
443 if ( bdb->bi_idl_cache_max_size ) {
444 ldap_pvt_thread_rdwr_wlock ( &bdb->bi_idl_tree_rwlock );
445 avl_free( bdb->bi_idl_tree, NULL );
446 entry = bdb->bi_idl_lru_head;
447 while ( entry != NULL ) {
448 next_entry = entry->idl_lru_next;
451 free( entry->kstr.bv_val );
455 ldap_pvt_thread_rdwr_wunlock ( &bdb->bi_idl_tree_rwlock );
458 XLOCK_ID_FREE(bdb->bi_dbenv, bdb->bi_cache.c_locker);
464 bdb_db_destroy( BackendDB *be )
467 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
468 Operation *ps = NULL;
469 Operation *psn = NULL;
470 void *saved_tmpmemctx = NULL;
472 /* close db environment */
473 if( bdb->bi_dbenv ) {
474 /* force a checkpoint */
475 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
477 Debug( LDAP_DEBUG_ANY,
478 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
479 db_strerror(rc), rc, 0 );
482 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
483 bdb->bi_dbenv = NULL;
485 Debug( LDAP_DEBUG_ANY,
486 "bdb_db_destroy: close failed: %s (%d)\n",
487 db_strerror(rc), rc, 0 );
492 if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
494 ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
495 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_mutex );
496 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_dntree.bei_kids_mutex );
497 ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
498 ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
499 if ( bdb->bi_idl_cache_max_size ) {
500 ldap_pvt_thread_rdwr_destroy( &bdb->bi_idl_tree_rwlock );
501 ldap_pvt_thread_mutex_destroy( &bdb->bi_idl_tree_lrulock );
505 ldap_pvt_thread_rdwr_destroy ( &bdb->bi_pslist_rwlock );
506 ps = LDAP_LIST_FIRST( &bdb->bi_psearch_list );
509 psn = LDAP_LIST_NEXT( ps, o_ps_link );
511 saved_tmpmemctx = ps->o_tmpmemctx;
513 if (!BER_BVISNULL(&ps->o_req_dn)) {
514 slap_sl_free( ps->o_req_dn.bv_val, ps->o_tmpmemctx );
516 if (!BER_BVISNULL(&ps->o_req_ndn)) {
517 slap_sl_free( ps->o_req_ndn.bv_val, ps->o_tmpmemctx );
519 if (!BER_BVISNULL(&ps->ors_filterstr)) {
520 slap_sl_free(ps->ors_filterstr.bv_val, ps->o_tmpmemctx);
522 if (ps->ors_filter != NULL) {
523 filter_free_x(ps, ps->ors_filter);
525 if ( ps->ors_attrs != NULL) {
526 ps->o_tmpfree(ps->ors_attrs, ps->o_tmpmemctx);
531 if ( saved_tmpmemctx ) {
532 slap_sl_mem_destroy( NULL, saved_tmpmemctx );
538 psn = LDAP_LIST_NEXT( ps, o_ps_link );
540 saved_tmpmemctx = ps->o_tmpmemctx;
542 if (!BER_BVISNULL(&ps->o_req_dn)) {
543 slap_sl_free( ps->o_req_dn.bv_val, ps->o_tmpmemctx );
545 if (!BER_BVISNULL(&ps->o_req_ndn)) {
546 slap_sl_free( ps->o_req_ndn.bv_val, ps->o_tmpmemctx );
548 if (!BER_BVISNULL(&ps->ors_filterstr)) {
549 slap_sl_free(ps->ors_filterstr.bv_val, ps->o_tmpmemctx);
551 if (ps->ors_filter != NULL) {
552 filter_free_x(ps, ps->ors_filter);
554 if ( ps->ors_attrs != NULL) {
555 ps->o_tmpfree(ps->ors_attrs, ps->o_tmpmemctx);
560 if ( saved_tmpmemctx ) {
561 slap_sl_mem_destroy( NULL, saved_tmpmemctx );
567 be->be_private = NULL;
576 static char *controls[] = {
578 LDAP_CONTROL_MANAGEDSAIT,
580 LDAP_CONTROL_PAGEDRESULTS,
581 #ifdef LDAP_CONTROL_SUBENTRIES
582 LDAP_CONTROL_SUBENTRIES,
584 LDAP_CONTROL_VALUESRETURNFILTER,
585 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
586 LDAP_CONTROL_X_PERMISSIVE_MODIFY,
591 /* initialize the underlying database system */
592 Debug( LDAP_DEBUG_TRACE,
593 LDAP_XSTRING(bdb_back_initialize) ": initialize "
594 BDB_UCTYPE " backend\n", 0, 0, 0 );
597 SLAP_BFLAG_INCREMENT |
598 #ifdef BDB_SUBENTRIES
599 SLAP_BFLAG_SUBENTRIES |
602 SLAP_BFLAG_REFERRALS;
604 bi->bi_controls = controls;
606 { /* version check */
607 int major, minor, patch, ver;
608 char *version = db_version( &major, &minor, &patch );
612 /* All our stdio does an ASCII to EBCDIC conversion on
613 * the output. Strings from the BDB library are already
614 * in EBCDIC; we have to go back and forth...
616 strcpy( v2, version );
621 ver = (major << 24) | (minor << 16) | patch;
622 if( ver < DB_VERSION_FULL )
624 Debug( LDAP_DEBUG_ANY,
625 LDAP_XSTRING(bdb_back_initialize) ": "
626 "BDB library version mismatch:"
627 " expected " DB_VERSION_STRING ","
628 " got %s\n", version, 0, 0 );
631 Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(bdb_back_initialize)
632 ": %s\n", version, 0, 0 );
635 db_env_set_func_free( ber_memfree );
636 db_env_set_func_malloc( (db_malloc *)ber_memalloc );
637 db_env_set_func_realloc( (db_realloc *)ber_memrealloc );
639 /* This is a no-op on a NO_THREAD build. Leave the default
640 * alone so that BDB will sleep on interprocess conflicts.
642 db_env_set_func_yield( ldap_pvt_thread_yield );
646 static char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
648 bdb_uuid.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ));
649 bdb_uuid.bv_val = uuidbuf;
657 bi->bi_db_init = bdb_db_init;
658 bi->bi_db_config = bdb_db_config;
659 bi->bi_db_open = bdb_db_open;
660 bi->bi_db_close = bdb_db_close;
661 bi->bi_db_destroy = bdb_db_destroy;
663 bi->bi_op_add = bdb_add;
664 bi->bi_op_bind = bdb_bind;
665 bi->bi_op_compare = bdb_compare;
666 bi->bi_op_delete = bdb_delete;
667 bi->bi_op_modify = bdb_modify;
668 bi->bi_op_modrdn = bdb_modrdn;
669 bi->bi_op_search = bdb_search;
671 bi->bi_op_unbind = 0;
673 #if 0 /* DELETE ME */
674 bi->bi_op_abandon = bdb_abandon;
675 bi->bi_op_cancel = bdb_cancel;
678 bi->bi_extended = bdb_extended;
680 bi->bi_chk_referrals = bdb_referrals;
681 bi->bi_operational = bdb_operational;
682 bi->bi_has_subordinates = bdb_hasSubordinates;
683 bi->bi_entry_release_rw = bdb_entry_release;
684 bi->bi_entry_get_rw = bdb_entry_get;
687 * hooks for slap tools
689 bi->bi_tool_entry_open = bdb_tool_entry_open;
690 bi->bi_tool_entry_close = bdb_tool_entry_close;
691 bi->bi_tool_entry_first = bdb_tool_entry_next;
692 bi->bi_tool_entry_next = bdb_tool_entry_next;
693 bi->bi_tool_entry_get = bdb_tool_entry_get;
694 bi->bi_tool_entry_put = bdb_tool_entry_put;
695 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
696 bi->bi_tool_sync = 0;
697 bi->bi_tool_dn2id_get = bdb_tool_dn2id_get;
698 bi->bi_tool_id2entry_get = bdb_tool_id2entry_get;
699 bi->bi_tool_entry_modify = bdb_tool_entry_modify;
701 bi->bi_connection_init = 0;
702 bi->bi_connection_destroy = 0;
707 #if (SLAPD_BDB == SLAPD_MOD_DYNAMIC && !defined(BDB_HIER)) || \
708 (SLAPD_HDB == SLAPD_MOD_DYNAMIC && defined(BDB_HIER))
710 /* conditionally define the init_module() function */
712 SLAP_BACKEND_INIT_MODULE( hdb )
713 #else /* !BDB_HIER */
714 SLAP_BACKEND_INIT_MODULE( bdb )
715 #endif /* !BDB_HIER */
717 #endif /* SLAPD_[BH]DB == SLAPD_MOD_DYNAMIC */