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 );
111 unsigned char *u, *c;
117 #ifdef WORDS_BIGENDIAN
118 for( i = 0; i < (int)sizeof(ID); i++)
120 for( i = sizeof(ID)-1; i >= 0; i--)
132 bdb_checkpoint( void *ctx, void *arg )
134 struct re_s *rtask = arg;
135 struct bdb_info *bdb = rtask->arg;
137 TXN_CHECKPOINT( bdb->bi_dbenv, bdb->bi_txn_cp_kbyte,
138 bdb->bi_txn_cp_min, 0 );
143 bdb_db_open( BackendDB *be )
146 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
149 char path[MAXPATHLEN];
152 Debug( LDAP_DEBUG_ARGS,
154 be->be_suffix[0].bv_val, 0, 0 );
156 #ifndef BDB_MULTIPLE_SUFFIXES
157 if ( be->be_suffix[1].bv_val ) {
158 Debug( LDAP_DEBUG_ANY,
159 "bdb_db_open: only one suffix allowed\n", 0, 0, 0 );
163 /* we should check existance of dbenv_home and db_directory */
165 rc = db_env_create( &bdb->bi_dbenv, 0 );
167 Debug( LDAP_DEBUG_ANY,
168 "bdb_db_open: db_env_create failed: %s (%d)\n",
169 db_strerror(rc), rc, 0 );
173 flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE
174 | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN;
177 /* Never do automatic recovery, must perform it manually.
178 * Otherwise restarting with gentlehup will corrupt the
181 if( !(slapMode & SLAP_TOOL_MODE) ) flags |= DB_RECOVER;
184 /* If a key was set, use shared memory for the BDB environment */
185 if ( bdb->bi_shm_key ) {
186 bdb->bi_dbenv->set_shm_key( bdb->bi_dbenv, bdb->bi_shm_key );
187 flags |= DB_SYSTEM_MEM;
190 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
191 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
192 bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
194 /* One long-lived TXN per thread, two TXNs per write op */
195 bdb->bi_dbenv->set_tx_max( bdb->bi_dbenv, connection_pool_max * 3 );
197 #ifdef SLAP_ZONE_ALLOC
198 if ( bdb->bi_cache.c_maxsize ) {
199 bdb->bi_cache.c_zctx = slap_zn_mem_create(
207 if ( bdb->bi_idl_cache_max_size ) {
208 bdb->bi_idl_tree = NULL;
209 ldap_pvt_thread_rdwr_init( &bdb->bi_idl_tree_rwlock );
210 ldap_pvt_thread_mutex_init( &bdb->bi_idl_tree_lrulock );
211 bdb->bi_idl_cache_size = 0;
216 char dir[MAXPATHLEN], *ptr;
218 if (bdb->bi_dbenv_home[0] == '.') {
219 /* If home is a relative path, relative subdirs
220 * are just concat'd by BDB. We don't want the
221 * path to be concat'd twice, e.g.
222 * ./test-db/./test-db/tmp
226 ptr = lutil_strcopy( dir, bdb->bi_dbenv_home );
227 *ptr++ = LDAP_DIRSEP[0];
233 strcpy( ptr, BDB_TMP_SUBDIR );
237 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
239 Debug( LDAP_DEBUG_ANY,
240 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
241 dir, db_strerror(rc), rc );
245 strcpy( ptr, 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 strcpy( ptr, BDB_DATA_SUBDIR );
261 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
263 Debug( LDAP_DEBUG_ANY,
264 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
265 dir, db_strerror(rc), rc );
271 if( bdb->bi_dbenv_xflags != 0 ) {
272 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
273 bdb->bi_dbenv_xflags, 1);
275 Debug( LDAP_DEBUG_ANY,
276 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
277 db_strerror(rc), rc, 0 );
282 Debug( LDAP_DEBUG_TRACE,
283 "bdb_db_open: dbenv_open(%s)\n",
284 bdb->bi_dbenv_home, 0, 0);
287 strcpy( path, bdb->bi_dbenv_home );
289 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
292 bdb->bi_dbenv_mode );
294 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
297 bdb->bi_dbenv_mode );
300 Debug( LDAP_DEBUG_ANY,
301 "bdb_db_open: dbenv_open failed: %s (%d)\n",
302 db_strerror(rc), rc, 0 );
306 flags = DB_THREAD | bdb->bi_db_opflags;
308 bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
309 BDB_INDICES * sizeof(struct bdb_db_info *) );
311 /* open (and create) main database */
312 for( i = 0; bdbi_databases[i].name; i++ ) {
313 struct bdb_db_info *db;
315 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
317 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
319 Debug( LDAP_DEBUG_ANY,
320 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
321 bdb->bi_dbenv_home, db_strerror(rc), rc );
325 if( i == BDB_ID2ENTRY ) {
327 rc = db->bdi_db->set_bt_compare( db->bdi_db,
330 rc = db->bdi_db->set_pagesize( db->bdi_db,
331 BDB_ID2ENTRY_PAGESIZE );
332 if ( slapMode & SLAP_TOOL_READMAIN ) {
338 rc = db->bdi_db->set_flags( db->bdi_db,
339 DB_DUP | DB_DUPSORT );
342 rc = db->bdi_db->set_dup_compare( db->bdi_db,
345 if ( slapMode & SLAP_TOOL_READONLY ) {
351 rc = db->bdi_db->set_dup_compare( db->bdi_db,
354 rc = db->bdi_db->set_bt_compare( db->bdi_db,
357 if ( slapMode & (SLAP_TOOL_READONLY|SLAP_TOOL_READMAIN) ) {
363 rc = db->bdi_db->set_pagesize( db->bdi_db,
368 strcpy( path, bdbi_databases[i].file );
370 rc = DB_OPEN( db->bdi_db,
372 /* bdbi_databases[i].name, */ NULL,
373 bdbi_databases[i].type,
374 bdbi_databases[i].flags | flags,
375 bdb->bi_dbenv_mode );
377 rc = DB_OPEN( db->bdi_db,
378 bdbi_databases[i].file,
379 /* bdbi_databases[i].name, */ NULL,
380 bdbi_databases[i].type,
381 bdbi_databases[i].flags | flags,
382 bdb->bi_dbenv_mode );
386 char buf[SLAP_TEXT_BUFLEN];
388 snprintf( buf, sizeof(buf), "%s/%s",
389 bdb->bi_dbenv_home, bdbi_databases[i].file );
390 Debug( LDAP_DEBUG_ANY,
391 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
392 buf, db_strerror(rc), rc );
396 flags &= ~(DB_CREATE | DB_RDONLY);
397 db->bdi_name = bdbi_databases[i].name;
398 bdb->bi_databases[i] = db;
401 bdb->bi_databases[i] = NULL;
402 bdb->bi_ndatabases = i;
405 rc = bdb_last_id( be, NULL );
407 Debug( LDAP_DEBUG_ANY,
408 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
409 bdb->bi_dbenv_home, db_strerror(rc), rc );
413 XLOCK_ID(bdb->bi_dbenv, &bdb->bi_cache.c_locker);
415 /* If we're in server mode and time-based checkpointing is enabled,
416 * submit a task to perform periodic checkpoints.
418 if ( slapMode & SLAP_SERVER_MODE && bdb->bi_txn_cp &&
419 bdb->bi_txn_cp_min ) {
420 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
421 ldap_pvt_runqueue_insert( &slapd_rq, bdb->bi_txn_cp_min*60,
422 bdb_checkpoint, bdb );
423 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
426 /* <insert> open (and create) index databases */
431 bdb_db_close( BackendDB *be )
434 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
435 struct bdb_db_info *db;
436 bdb_idl_cache_entry_t *entry, *next_entry;
438 while( bdb->bi_ndatabases-- ) {
439 db = bdb->bi_databases[bdb->bi_ndatabases];
440 rc = db->bdi_db->close( db->bdi_db, 0 );
441 /* Lower numbered names are not strdup'd */
442 if( bdb->bi_ndatabases >= BDB_NDB )
443 free( db->bdi_name );
446 free( bdb->bi_databases );
447 bdb_attr_index_destroy( bdb->bi_attrs );
449 bdb_cache_release_all (&bdb->bi_cache);
451 if ( bdb->bi_idl_cache_max_size ) {
452 ldap_pvt_thread_rdwr_wlock ( &bdb->bi_idl_tree_rwlock );
453 avl_free( bdb->bi_idl_tree, NULL );
454 entry = bdb->bi_idl_lru_head;
455 while ( entry != NULL ) {
456 next_entry = entry->idl_lru_next;
459 free( entry->kstr.bv_val );
463 ldap_pvt_thread_rdwr_wunlock ( &bdb->bi_idl_tree_rwlock );
466 XLOCK_ID_FREE(bdb->bi_dbenv, bdb->bi_cache.c_locker);
472 bdb_db_destroy( BackendDB *be )
475 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
476 Operation *ps = NULL;
477 Operation *psn = NULL;
478 void *saved_tmpmemctx = NULL;
480 /* close db environment */
481 if( bdb->bi_dbenv ) {
482 /* force a checkpoint */
483 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
485 Debug( LDAP_DEBUG_ANY,
486 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
487 db_strerror(rc), rc, 0 );
490 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
491 bdb->bi_dbenv = NULL;
493 Debug( LDAP_DEBUG_ANY,
494 "bdb_db_destroy: close failed: %s (%d)\n",
495 db_strerror(rc), rc, 0 );
500 if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
502 ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
503 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_mutex );
504 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_dntree.bei_kids_mutex );
505 ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
506 ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
507 if ( bdb->bi_idl_cache_max_size ) {
508 ldap_pvt_thread_rdwr_destroy( &bdb->bi_idl_tree_rwlock );
509 ldap_pvt_thread_mutex_destroy( &bdb->bi_idl_tree_lrulock );
513 ldap_pvt_thread_rdwr_destroy ( &bdb->bi_pslist_rwlock );
514 ps = LDAP_LIST_FIRST( &bdb->bi_psearch_list );
517 psn = LDAP_LIST_NEXT( ps, o_ps_link );
519 saved_tmpmemctx = ps->o_tmpmemctx;
521 if (!BER_BVISNULL(&ps->o_req_dn)) {
522 slap_sl_free( ps->o_req_dn.bv_val, ps->o_tmpmemctx );
524 if (!BER_BVISNULL(&ps->o_req_ndn)) {
525 slap_sl_free( ps->o_req_ndn.bv_val, ps->o_tmpmemctx );
527 if (!BER_BVISNULL(&ps->ors_filterstr)) {
528 slap_sl_free(ps->ors_filterstr.bv_val, ps->o_tmpmemctx);
530 if (ps->ors_filter != NULL) {
531 filter_free_x(ps, ps->ors_filter);
533 if ( ps->ors_attrs != NULL) {
534 ps->o_tmpfree(ps->ors_attrs, ps->o_tmpmemctx);
539 if ( saved_tmpmemctx ) {
540 slap_sl_mem_destroy( NULL, saved_tmpmemctx );
546 psn = LDAP_LIST_NEXT( ps, o_ps_link );
548 saved_tmpmemctx = ps->o_tmpmemctx;
550 if (!BER_BVISNULL(&ps->o_req_dn)) {
551 slap_sl_free( ps->o_req_dn.bv_val, ps->o_tmpmemctx );
553 if (!BER_BVISNULL(&ps->o_req_ndn)) {
554 slap_sl_free( ps->o_req_ndn.bv_val, ps->o_tmpmemctx );
556 if (!BER_BVISNULL(&ps->ors_filterstr)) {
557 slap_sl_free(ps->ors_filterstr.bv_val, ps->o_tmpmemctx);
559 if (ps->ors_filter != NULL) {
560 filter_free_x(ps, ps->ors_filter);
562 if ( ps->ors_attrs != NULL) {
563 ps->o_tmpfree(ps->ors_attrs, ps->o_tmpmemctx);
568 if ( saved_tmpmemctx ) {
569 slap_sl_mem_destroy( NULL, saved_tmpmemctx );
575 be->be_private = NULL;
584 static char *controls[] = {
586 LDAP_CONTROL_MANAGEDSAIT,
588 LDAP_CONTROL_PAGEDRESULTS,
589 #ifdef LDAP_CONTROL_SUBENTRIES
590 LDAP_CONTROL_SUBENTRIES,
592 LDAP_CONTROL_VALUESRETURNFILTER,
593 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
594 LDAP_CONTROL_X_PERMISSIVE_MODIFY,
599 /* initialize the underlying database system */
600 Debug( LDAP_DEBUG_TRACE,
601 LDAP_XSTRING(bdb_back_initialize) ": initialize "
602 BDB_UCTYPE " backend\n", 0, 0, 0 );
605 SLAP_BFLAG_INCREMENT |
606 #ifdef BDB_SUBENTRIES
607 SLAP_BFLAG_SUBENTRIES |
610 SLAP_BFLAG_REFERRALS;
612 bi->bi_controls = controls;
614 { /* version check */
615 int major, minor, patch, ver;
616 char *version = db_version( &major, &minor, &patch );
620 /* All our stdio does an ASCII to EBCDIC conversion on
621 * the output. Strings from the BDB library are already
622 * in EBCDIC; we have to go back and forth...
624 strcpy( v2, version );
629 ver = (major << 24) | (minor << 16) | patch;
630 if( ver < DB_VERSION_FULL )
632 Debug( LDAP_DEBUG_ANY,
633 LDAP_XSTRING(bdb_back_initialize) ": "
634 "BDB library version mismatch:"
635 " expected " DB_VERSION_STRING ","
636 " got %s\n", version, 0, 0 );
639 Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(bdb_back_initialize)
640 ": %s\n", version, 0, 0 );
643 db_env_set_func_free( ber_memfree );
644 db_env_set_func_malloc( (db_malloc *)ber_memalloc );
645 db_env_set_func_realloc( (db_realloc *)ber_memrealloc );
647 /* This is a no-op on a NO_THREAD build. Leave the default
648 * alone so that BDB will sleep on interprocess conflicts.
650 db_env_set_func_yield( ldap_pvt_thread_yield );
654 static char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
656 bdb_uuid.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ));
657 bdb_uuid.bv_val = uuidbuf;
665 bi->bi_db_init = bdb_db_init;
666 bi->bi_db_config = bdb_db_config;
667 bi->bi_db_open = bdb_db_open;
668 bi->bi_db_close = bdb_db_close;
669 bi->bi_db_destroy = bdb_db_destroy;
671 bi->bi_op_add = bdb_add;
672 bi->bi_op_bind = bdb_bind;
673 bi->bi_op_compare = bdb_compare;
674 bi->bi_op_delete = bdb_delete;
675 bi->bi_op_modify = bdb_modify;
676 bi->bi_op_modrdn = bdb_modrdn;
677 bi->bi_op_search = bdb_search;
679 bi->bi_op_unbind = 0;
681 #if 0 /* DELETE ME */
682 bi->bi_op_abandon = bdb_abandon;
683 bi->bi_op_cancel = bdb_cancel;
686 bi->bi_extended = bdb_extended;
688 bi->bi_chk_referrals = bdb_referrals;
689 bi->bi_operational = bdb_operational;
690 bi->bi_has_subordinates = bdb_hasSubordinates;
691 bi->bi_entry_release_rw = bdb_entry_release;
692 bi->bi_entry_get_rw = bdb_entry_get;
695 * hooks for slap tools
697 bi->bi_tool_entry_open = bdb_tool_entry_open;
698 bi->bi_tool_entry_close = bdb_tool_entry_close;
699 bi->bi_tool_entry_first = bdb_tool_entry_next;
700 bi->bi_tool_entry_next = bdb_tool_entry_next;
701 bi->bi_tool_entry_get = bdb_tool_entry_get;
702 bi->bi_tool_entry_put = bdb_tool_entry_put;
703 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
704 bi->bi_tool_sync = 0;
705 bi->bi_tool_dn2id_get = bdb_tool_dn2id_get;
706 bi->bi_tool_id2entry_get = bdb_tool_id2entry_get;
707 bi->bi_tool_entry_modify = bdb_tool_entry_modify;
709 bi->bi_connection_init = 0;
710 bi->bi_connection_destroy = 0;
715 #if (SLAPD_BDB == SLAPD_MOD_DYNAMIC && !defined(BDB_HIER)) || \
716 (SLAPD_HDB == SLAPD_MOD_DYNAMIC && defined(BDB_HIER))
718 /* conditionally define the init_module() function */
720 SLAP_BACKEND_INIT_MODULE( hdb )
721 #else /* !BDB_HIER */
722 SLAP_BACKEND_INIT_MODULE( bdb )
723 #endif /* !BDB_HIER */
725 #endif /* SLAPD_[BH]DB == SLAPD_MOD_DYNAMIC */