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 );
105 bdb_checkpoint( void *ctx, void *arg )
107 struct re_s *rtask = arg;
108 struct bdb_info *bdb = rtask->arg;
110 TXN_CHECKPOINT( bdb->bi_dbenv, bdb->bi_txn_cp_kbyte,
111 bdb->bi_txn_cp_min, 0 );
116 bdb_db_open( BackendDB *be )
119 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
122 char path[MAXPATHLEN];
125 Debug( LDAP_DEBUG_ARGS,
127 be->be_suffix[0].bv_val, 0, 0 );
129 #ifndef BDB_MULTIPLE_SUFFIXES
130 if ( be->be_suffix[1].bv_val ) {
131 Debug( LDAP_DEBUG_ANY,
132 "bdb_db_open: only one suffix allowed\n", 0, 0, 0 );
136 /* we should check existance of dbenv_home and db_directory */
138 rc = db_env_create( &bdb->bi_dbenv, 0 );
140 Debug( LDAP_DEBUG_ANY,
141 "bdb_db_open: db_env_create failed: %s (%d)\n",
142 db_strerror(rc), rc, 0 );
146 flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE
147 | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN;
150 /* Never do automatic recovery, must perform it manually.
151 * Otherwise restarting with gentlehup will corrupt the
154 if( !(slapMode & SLAP_TOOL_MODE) ) flags |= DB_RECOVER;
157 /* If a key was set, use shared memory for the BDB environment */
158 if ( bdb->bi_shm_key ) {
159 bdb->bi_dbenv->set_shm_key( bdb->bi_dbenv, bdb->bi_shm_key );
160 flags |= DB_SYSTEM_MEM;
163 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
164 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
165 bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
167 /* One long-lived TXN per thread, two TXNs per write op */
168 bdb->bi_dbenv->set_tx_max( bdb->bi_dbenv, connection_pool_max * 3 );
170 #ifdef SLAP_ZONE_ALLOC
171 if ( bdb->bi_cache.c_maxsize ) {
172 bdb->bi_cache.c_zctx = slap_zn_mem_create(
180 if ( bdb->bi_idl_cache_max_size ) {
181 bdb->bi_idl_tree = NULL;
182 ldap_pvt_thread_rdwr_init( &bdb->bi_idl_tree_rwlock );
183 ldap_pvt_thread_mutex_init( &bdb->bi_idl_tree_lrulock );
184 bdb->bi_idl_cache_size = 0;
189 char dir[MAXPATHLEN], *ptr;
191 if (bdb->bi_dbenv_home[0] == '.') {
192 /* If home is a relative path, relative subdirs
193 * are just concat'd by BDB. We don't want the
194 * path to be concat'd twice, e.g.
195 * ./test-db/./test-db/tmp
199 ptr = lutil_strcopy( dir, bdb->bi_dbenv_home );
200 *ptr++ = LDAP_DIRSEP[0];
206 strcpy( ptr, BDB_TMP_SUBDIR );
210 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
212 Debug( LDAP_DEBUG_ANY,
213 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
214 dir, db_strerror(rc), rc );
218 strcpy( ptr, BDB_LG_SUBDIR );
222 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
224 Debug( LDAP_DEBUG_ANY,
225 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
226 dir, db_strerror(rc), rc );
230 strcpy( ptr, BDB_DATA_SUBDIR );
234 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
236 Debug( LDAP_DEBUG_ANY,
237 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
238 dir, db_strerror(rc), rc );
244 if( bdb->bi_dbenv_xflags != 0 ) {
245 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
246 bdb->bi_dbenv_xflags, 1);
248 Debug( LDAP_DEBUG_ANY,
249 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
250 db_strerror(rc), rc, 0 );
255 Debug( LDAP_DEBUG_TRACE,
256 "bdb_db_open: dbenv_open(%s)\n",
257 bdb->bi_dbenv_home, 0, 0);
260 strcpy( path, bdb->bi_dbenv_home );
262 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
265 bdb->bi_dbenv_mode );
267 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
270 bdb->bi_dbenv_mode );
273 Debug( LDAP_DEBUG_ANY,
274 "bdb_db_open: dbenv_open failed: %s (%d)\n",
275 db_strerror(rc), rc, 0 );
279 flags = DB_THREAD | bdb->bi_db_opflags;
281 bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
282 BDB_INDICES * sizeof(struct bdb_db_info *) );
284 /* open (and create) main database */
285 for( i = 0; bdbi_databases[i].name; i++ ) {
286 struct bdb_db_info *db;
288 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
290 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
292 Debug( LDAP_DEBUG_ANY,
293 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
294 bdb->bi_dbenv_home, db_strerror(rc), rc );
298 if( i == BDB_ID2ENTRY ) {
299 rc = db->bdi_db->set_pagesize( db->bdi_db,
300 BDB_ID2ENTRY_PAGESIZE );
301 if ( slapMode & SLAP_TOOL_READMAIN ) {
307 rc = db->bdi_db->set_flags( db->bdi_db,
308 DB_DUP | DB_DUPSORT );
310 if ( slapMode & SLAP_TOOL_READONLY ) {
316 if ( slapMode & (SLAP_TOOL_READONLY|SLAP_TOOL_READMAIN) ) {
322 rc = db->bdi_db->set_pagesize( db->bdi_db,
327 strcpy( path, bdbi_databases[i].file );
329 rc = DB_OPEN( db->bdi_db,
331 /* bdbi_databases[i].name, */ NULL,
332 bdbi_databases[i].type,
333 bdbi_databases[i].flags | flags,
334 bdb->bi_dbenv_mode );
336 rc = DB_OPEN( db->bdi_db,
337 bdbi_databases[i].file,
338 /* bdbi_databases[i].name, */ NULL,
339 bdbi_databases[i].type,
340 bdbi_databases[i].flags | flags,
341 bdb->bi_dbenv_mode );
345 char buf[SLAP_TEXT_BUFLEN];
347 snprintf( buf, sizeof(buf), "%s/%s",
348 bdb->bi_dbenv_home, bdbi_databases[i].file );
349 Debug( LDAP_DEBUG_ANY,
350 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
351 buf, db_strerror(rc), rc );
355 flags &= ~(DB_CREATE | DB_RDONLY);
356 db->bdi_name = bdbi_databases[i].name;
357 bdb->bi_databases[i] = db;
360 bdb->bi_databases[i] = NULL;
361 bdb->bi_ndatabases = i;
364 rc = bdb_last_id( be, NULL );
366 Debug( LDAP_DEBUG_ANY,
367 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
368 bdb->bi_dbenv_home, db_strerror(rc), rc );
372 XLOCK_ID(bdb->bi_dbenv, &bdb->bi_cache.c_locker);
374 /* If we're in server mode and time-based checkpointing is enabled,
375 * submit a task to perform periodic checkpoints.
377 if ( slapMode & SLAP_SERVER_MODE && bdb->bi_txn_cp &&
378 bdb->bi_txn_cp_min ) {
379 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
380 ldap_pvt_runqueue_insert( &slapd_rq, bdb->bi_txn_cp_min*60,
381 bdb_checkpoint, bdb );
382 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
385 /* <insert> open (and create) index databases */
390 bdb_db_close( BackendDB *be )
393 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
394 struct bdb_db_info *db;
395 bdb_idl_cache_entry_t *entry, *next_entry;
397 while( bdb->bi_ndatabases-- ) {
398 db = bdb->bi_databases[bdb->bi_ndatabases];
399 rc = db->bdi_db->close( db->bdi_db, 0 );
400 /* Lower numbered names are not strdup'd */
401 if( bdb->bi_ndatabases >= BDB_NDB )
402 free( db->bdi_name );
405 free( bdb->bi_databases );
406 bdb_attr_index_destroy( bdb->bi_attrs );
408 bdb_cache_release_all (&bdb->bi_cache);
410 if ( bdb->bi_idl_cache_max_size ) {
411 ldap_pvt_thread_rdwr_wlock ( &bdb->bi_idl_tree_rwlock );
412 avl_free( bdb->bi_idl_tree, NULL );
413 entry = bdb->bi_idl_lru_head;
414 while ( entry != NULL ) {
415 next_entry = entry->idl_lru_next;
418 free( entry->kstr.bv_val );
422 ldap_pvt_thread_rdwr_wunlock ( &bdb->bi_idl_tree_rwlock );
425 XLOCK_ID_FREE(bdb->bi_dbenv, bdb->bi_cache.c_locker);
431 bdb_db_destroy( BackendDB *be )
434 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
435 Operation *ps = NULL;
436 Operation *psn = NULL;
437 void *saved_tmpmemctx = NULL;
439 /* close db environment */
440 if( bdb->bi_dbenv ) {
441 /* force a checkpoint */
442 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
444 Debug( LDAP_DEBUG_ANY,
445 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
446 db_strerror(rc), rc, 0 );
449 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
450 bdb->bi_dbenv = NULL;
452 Debug( LDAP_DEBUG_ANY,
453 "bdb_db_destroy: close failed: %s (%d)\n",
454 db_strerror(rc), rc, 0 );
459 if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
461 ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
462 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_mutex );
463 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_dntree.bei_kids_mutex );
464 ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
465 ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
466 if ( bdb->bi_idl_cache_max_size ) {
467 ldap_pvt_thread_rdwr_destroy( &bdb->bi_idl_tree_rwlock );
468 ldap_pvt_thread_mutex_destroy( &bdb->bi_idl_tree_lrulock );
472 ldap_pvt_thread_rdwr_destroy ( &bdb->bi_pslist_rwlock );
473 ps = LDAP_LIST_FIRST( &bdb->bi_psearch_list );
476 psn = LDAP_LIST_NEXT( ps, o_ps_link );
478 saved_tmpmemctx = ps->o_tmpmemctx;
480 if (!BER_BVISNULL(&ps->o_req_dn)) {
481 slap_sl_free( ps->o_req_dn.bv_val, ps->o_tmpmemctx );
483 if (!BER_BVISNULL(&ps->o_req_ndn)) {
484 slap_sl_free( ps->o_req_ndn.bv_val, ps->o_tmpmemctx );
486 if (!BER_BVISNULL(&ps->ors_filterstr)) {
487 slap_sl_free(ps->ors_filterstr.bv_val, ps->o_tmpmemctx);
489 if (ps->ors_filter != NULL) {
490 filter_free_x(ps, ps->ors_filter);
492 if ( ps->ors_attrs != NULL) {
493 ps->o_tmpfree(ps->ors_attrs, ps->o_tmpmemctx);
498 if ( saved_tmpmemctx ) {
499 slap_sl_mem_destroy( NULL, saved_tmpmemctx );
505 psn = LDAP_LIST_NEXT( ps, o_ps_link );
507 saved_tmpmemctx = ps->o_tmpmemctx;
509 if (!BER_BVISNULL(&ps->o_req_dn)) {
510 slap_sl_free( ps->o_req_dn.bv_val, ps->o_tmpmemctx );
512 if (!BER_BVISNULL(&ps->o_req_ndn)) {
513 slap_sl_free( ps->o_req_ndn.bv_val, ps->o_tmpmemctx );
515 if (!BER_BVISNULL(&ps->ors_filterstr)) {
516 slap_sl_free(ps->ors_filterstr.bv_val, ps->o_tmpmemctx);
518 if (ps->ors_filter != NULL) {
519 filter_free_x(ps, ps->ors_filter);
521 if ( ps->ors_attrs != NULL) {
522 ps->o_tmpfree(ps->ors_attrs, ps->o_tmpmemctx);
527 if ( saved_tmpmemctx ) {
528 slap_sl_mem_destroy( NULL, saved_tmpmemctx );
534 be->be_private = NULL;
543 static char *controls[] = {
545 LDAP_CONTROL_MANAGEDSAIT,
547 LDAP_CONTROL_PAGEDRESULTS,
548 #ifdef LDAP_CONTROL_SUBENTRIES
549 LDAP_CONTROL_SUBENTRIES,
551 LDAP_CONTROL_VALUESRETURNFILTER,
552 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
553 LDAP_CONTROL_X_PERMISSIVE_MODIFY,
558 /* initialize the underlying database system */
559 Debug( LDAP_DEBUG_TRACE,
560 LDAP_XSTRING(bdb_back_initialize) ": initialize "
561 BDB_UCTYPE " backend\n", 0, 0, 0 );
564 SLAP_BFLAG_INCREMENT |
565 #ifdef BDB_SUBENTRIES
566 SLAP_BFLAG_SUBENTRIES |
569 SLAP_BFLAG_REFERRALS;
571 bi->bi_controls = controls;
573 { /* version check */
574 int major, minor, patch, ver;
575 char *version = db_version( &major, &minor, &patch );
579 /* All our stdio does an ASCII to EBCDIC conversion on
580 * the output. Strings from the BDB library are already
581 * in EBCDIC; we have to go back and forth...
583 strcpy( v2, version );
588 ver = (major << 24) | (minor << 16) | patch;
589 if( ver < DB_VERSION_FULL )
591 Debug( LDAP_DEBUG_ANY,
592 LDAP_XSTRING(bdb_back_initialize) ": "
593 "BDB library version mismatch:"
594 " expected " DB_VERSION_STRING ","
595 " got %s\n", version, 0, 0 );
598 Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(bdb_back_initialize)
599 ": %s\n", version, 0, 0 );
602 db_env_set_func_free( ber_memfree );
603 db_env_set_func_malloc( (db_malloc *)ber_memalloc );
604 db_env_set_func_realloc( (db_realloc *)ber_memrealloc );
606 /* This is a no-op on a NO_THREAD build. Leave the default
607 * alone so that BDB will sleep on interprocess conflicts.
609 db_env_set_func_yield( ldap_pvt_thread_yield );
613 static char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
615 bdb_uuid.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ));
616 bdb_uuid.bv_val = uuidbuf;
624 bi->bi_db_init = bdb_db_init;
625 bi->bi_db_config = bdb_db_config;
626 bi->bi_db_open = bdb_db_open;
627 bi->bi_db_close = bdb_db_close;
628 bi->bi_db_destroy = bdb_db_destroy;
630 bi->bi_op_add = bdb_add;
631 bi->bi_op_bind = bdb_bind;
632 bi->bi_op_compare = bdb_compare;
633 bi->bi_op_delete = bdb_delete;
634 bi->bi_op_modify = bdb_modify;
635 bi->bi_op_modrdn = bdb_modrdn;
636 bi->bi_op_search = bdb_search;
638 bi->bi_op_unbind = 0;
640 #if 0 /* DELETE ME */
641 bi->bi_op_abandon = bdb_abandon;
642 bi->bi_op_cancel = bdb_cancel;
645 bi->bi_extended = bdb_extended;
647 bi->bi_chk_referrals = bdb_referrals;
648 bi->bi_operational = bdb_operational;
649 bi->bi_has_subordinates = bdb_hasSubordinates;
650 bi->bi_entry_release_rw = bdb_entry_release;
651 bi->bi_entry_get_rw = bdb_entry_get;
654 * hooks for slap tools
656 bi->bi_tool_entry_open = bdb_tool_entry_open;
657 bi->bi_tool_entry_close = bdb_tool_entry_close;
658 bi->bi_tool_entry_first = bdb_tool_entry_next;
659 bi->bi_tool_entry_next = bdb_tool_entry_next;
660 bi->bi_tool_entry_get = bdb_tool_entry_get;
661 bi->bi_tool_entry_put = bdb_tool_entry_put;
662 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
663 bi->bi_tool_sync = 0;
664 bi->bi_tool_dn2id_get = bdb_tool_dn2id_get;
665 bi->bi_tool_id2entry_get = bdb_tool_id2entry_get;
666 bi->bi_tool_entry_modify = bdb_tool_entry_modify;
668 bi->bi_connection_init = 0;
669 bi->bi_connection_destroy = 0;
674 #if (SLAPD_BDB == SLAPD_MOD_DYNAMIC && !defined(BDB_HIER)) || \
675 (SLAPD_HDB == SLAPD_MOD_DYNAMIC && defined(BDB_HIER))
677 /* conditionally define the init_module() function */
679 SLAP_BACKEND_INIT_MODULE( hdb )
680 #else /* !BDB_HIER */
681 SLAP_BACKEND_INIT_MODULE( bdb )
682 #endif /* !BDB_HIER */
684 #endif /* SLAPD_[BH]DB == SLAPD_MOD_DYNAMIC */