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>
29 static const struct bdbi_database {
34 } bdbi_databases[] = {
35 { "id2entry" BDB_SUFFIX, "id2entry", DB_BTREE, 0 },
36 { "dn2id" BDB_SUFFIX, "dn2id", DB_BTREE, 0 },
40 typedef void * db_malloc(size_t);
41 typedef void * db_realloc(void *, size_t);
44 bdb_db_init( BackendDB *be )
48 Debug( LDAP_DEBUG_TRACE,
49 LDAP_XSTRING(bdb_db_init) ": Initializing " BDB_UCTYPE " database\n",
52 /* allocate backend-database-specific stuff */
53 bdb = (struct bdb_info *) ch_calloc( 1, sizeof(struct bdb_info) );
55 /* DBEnv parameters */
56 bdb->bi_dbenv_home = ch_strdup( SLAPD_DEFAULT_DB_DIR );
57 bdb->bi_dbenv_xflags = 0;
58 bdb->bi_dbenv_mode = SLAPD_DEFAULT_DB_MODE;
60 bdb->bi_cache.c_maxsize = DEFAULT_CACHE_SIZE;
62 bdb->bi_lock_detect = DB_LOCK_DEFAULT;
63 bdb->bi_search_stack_depth = DEFAULT_SEARCH_STACK_DEPTH;
64 bdb->bi_search_stack = NULL;
66 ldap_pvt_thread_mutex_init( &bdb->bi_database_mutex );
67 ldap_pvt_thread_mutex_init( &bdb->bi_lastid_mutex );
68 ldap_pvt_thread_mutex_init( &bdb->bi_cache.lru_mutex );
69 ldap_pvt_thread_mutex_init( &bdb->bi_cache.c_dntree.bei_kids_mutex );
70 ldap_pvt_thread_rdwr_init ( &bdb->bi_cache.c_rwlock );
73 be->be_cf_table = be->bd_info->bi_cf_table;
79 bdb_checkpoint( void *ctx, void *arg )
81 struct re_s *rtask = arg;
82 struct bdb_info *bdb = rtask->arg;
84 TXN_CHECKPOINT( bdb->bi_dbenv, bdb->bi_txn_cp_kbyte,
85 bdb->bi_txn_cp_min, 0 );
90 bdb_db_open( BackendDB *be )
93 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
96 char path[MAXPATHLEN];
99 Debug( LDAP_DEBUG_ARGS,
101 be->be_suffix[0].bv_val, 0, 0 );
103 #ifndef BDB_MULTIPLE_SUFFIXES
104 if ( be->be_suffix[1].bv_val ) {
105 Debug( LDAP_DEBUG_ANY,
106 "bdb_db_open: only one suffix allowed\n", 0, 0, 0 );
110 /* we should check existance of dbenv_home and db_directory */
112 rc = db_env_create( &bdb->bi_dbenv, 0 );
114 Debug( LDAP_DEBUG_ANY,
115 "bdb_db_open: db_env_create failed: %s (%d)\n",
116 db_strerror(rc), rc, 0 );
120 flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE;
122 if ( !( slapMode & SLAP_TOOL_QUICK ))
123 flags |= DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN;
125 rc = alock_open( &bdb->bi_alock_info, "slapd", bdb->bi_dbenv_home,
126 slapMode & SLAP_TOOL_READONLY ? ALOCK_LOCKED : ALOCK_UNIQUE );
127 if( rc == ALOCK_RECOVER ) {
128 Debug( LDAP_DEBUG_ANY,
129 "bdb_db_open: alock_open: recovery required\n", 0, 0, 0 );
131 } else if( rc == ALOCK_BUSY ) {
132 Debug( LDAP_DEBUG_ANY,
133 "bdb_db_open: alock_open: database in use\n", 0, 0, 0 );
135 } else if( rc != ALOCK_CLEAN ) {
136 Debug( LDAP_DEBUG_ANY,
137 "bdb_db_open: alock_open: database unstable\n", 0, 0, 0 );
141 /* If a key was set, use shared memory for the BDB environment */
142 if ( bdb->bi_shm_key ) {
143 bdb->bi_dbenv->set_shm_key( bdb->bi_dbenv, bdb->bi_shm_key );
144 flags |= DB_SYSTEM_MEM;
147 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
148 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
149 bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
151 /* One long-lived TXN per thread, two TXNs per write op */
152 bdb->bi_dbenv->set_tx_max( bdb->bi_dbenv, connection_pool_max * 3 );
154 #ifdef SLAP_ZONE_ALLOC
155 if ( bdb->bi_cache.c_maxsize ) {
156 bdb->bi_cache.c_zctx = slap_zn_mem_create(
164 if ( bdb->bi_idl_cache_max_size ) {
165 bdb->bi_idl_tree = NULL;
166 ldap_pvt_thread_rdwr_init( &bdb->bi_idl_tree_rwlock );
167 ldap_pvt_thread_mutex_init( &bdb->bi_idl_tree_lrulock );
168 bdb->bi_idl_cache_size = 0;
173 char dir[MAXPATHLEN], *ptr;
175 if (bdb->bi_dbenv_home[0] == '.') {
176 /* If home is a relative path, relative subdirs
177 * are just concat'd by BDB. We don't want the
178 * path to be concat'd twice, e.g.
179 * ./test-db/./test-db/tmp
183 ptr = lutil_strcopy( dir, bdb->bi_dbenv_home );
184 *ptr++ = LDAP_DIRSEP[0];
190 strcpy( ptr, BDB_TMP_SUBDIR );
194 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
196 Debug( LDAP_DEBUG_ANY,
197 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
198 dir, db_strerror(rc), rc );
202 strcpy( ptr, BDB_LG_SUBDIR );
206 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
208 Debug( LDAP_DEBUG_ANY,
209 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
210 dir, db_strerror(rc), rc );
214 strcpy( ptr, BDB_DATA_SUBDIR );
218 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
220 Debug( LDAP_DEBUG_ANY,
221 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
222 dir, db_strerror(rc), rc );
228 if( bdb->bi_dbenv_xflags != 0 ) {
229 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
230 bdb->bi_dbenv_xflags, 1);
232 Debug( LDAP_DEBUG_ANY,
233 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
234 db_strerror(rc), rc, 0 );
239 Debug( LDAP_DEBUG_TRACE,
240 "bdb_db_open: dbenv_open(%s)\n",
241 bdb->bi_dbenv_home, 0, 0);
244 strcpy( path, bdb->bi_dbenv_home );
246 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
249 bdb->bi_dbenv_mode );
251 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
254 bdb->bi_dbenv_mode );
257 Debug( LDAP_DEBUG_ANY,
258 "bdb_db_open: dbenv_open failed: %s (%d)\n",
259 db_strerror(rc), rc, 0 );
262 if( flags & DB_RECOVER ) {
263 rc = alock_recover (&bdb->bi_alock_info);
265 Debug( LDAP_DEBUG_ANY,
266 "bdb_db_open: unable to alock_recover\n", 0, 0, 0 );
271 flags = DB_THREAD | bdb->bi_db_opflags;
273 #ifdef DB_AUTO_COMMIT
274 if ( !( slapMode & SLAP_TOOL_QUICK ))
275 flags |= DB_AUTO_COMMIT;
278 bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
279 BDB_INDICES * sizeof(struct bdb_db_info *) );
281 /* open (and create) main database */
282 for( i = 0; bdbi_databases[i].name; i++ ) {
283 struct bdb_db_info *db;
285 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
287 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
289 Debug( LDAP_DEBUG_ANY,
290 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
291 bdb->bi_dbenv_home, db_strerror(rc), rc );
295 if( i == BDB_ID2ENTRY ) {
296 rc = db->bdi_db->set_pagesize( db->bdi_db,
297 BDB_ID2ENTRY_PAGESIZE );
298 if ( slapMode & SLAP_TOOL_READMAIN ) {
304 rc = db->bdi_db->set_flags( db->bdi_db,
305 DB_DUP | DB_DUPSORT );
307 if ( slapMode & SLAP_TOOL_READONLY ) {
313 if ( slapMode & (SLAP_TOOL_READONLY|SLAP_TOOL_READMAIN) ) {
319 rc = db->bdi_db->set_pagesize( db->bdi_db,
324 strcpy( path, bdbi_databases[i].file );
326 rc = DB_OPEN( db->bdi_db,
328 /* bdbi_databases[i].name, */ NULL,
329 bdbi_databases[i].type,
330 bdbi_databases[i].flags | flags,
331 bdb->bi_dbenv_mode );
333 rc = DB_OPEN( db->bdi_db,
334 bdbi_databases[i].file,
335 /* bdbi_databases[i].name, */ NULL,
336 bdbi_databases[i].type,
337 bdbi_databases[i].flags | flags,
338 bdb->bi_dbenv_mode );
342 char buf[SLAP_TEXT_BUFLEN];
344 snprintf( buf, sizeof(buf), "%s/%s",
345 bdb->bi_dbenv_home, bdbi_databases[i].file );
346 Debug( LDAP_DEBUG_ANY,
347 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
348 buf, db_strerror(rc), rc );
352 flags &= ~(DB_CREATE | DB_RDONLY);
353 db->bdi_name = bdbi_databases[i].name;
354 bdb->bi_databases[i] = db;
357 bdb->bi_databases[i] = NULL;
358 bdb->bi_ndatabases = i;
361 rc = bdb_last_id( be, NULL );
363 Debug( LDAP_DEBUG_ANY,
364 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
365 bdb->bi_dbenv_home, db_strerror(rc), rc );
369 if ( !( slapMode & SLAP_TOOL_QUICK )) {
370 XLOCK_ID(bdb->bi_dbenv, &bdb->bi_cache.c_locker);
373 /* If we're in server mode and time-based checkpointing is enabled,
374 * submit a task to perform periodic checkpoints.
376 if ( slapMode & SLAP_SERVER_MODE && bdb->bi_txn_cp &&
377 bdb->bi_txn_cp_min ) {
378 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
379 ldap_pvt_runqueue_insert( &slapd_rq, bdb->bi_txn_cp_min*60,
380 bdb_checkpoint, bdb );
381 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
384 if ( slapMode & SLAP_SERVER_MODE && bdb->bi_db_has_config ) {
385 char buf[SLAP_TEXT_BUFLEN];
386 FILE *f = fopen( bdb->bi_db_config_path, "r" );
390 while ( fgets( buf, sizeof(buf), f )) {
391 ber_str2bv( buf, 0, 1, &bv );
392 if ( bv.bv_val[bv.bv_len-1] == '\n' ) {
394 bv.bv_val[bv.bv_len] = '\0';
396 ber_bvarray_add( &bdb->bi_db_config, &bv );
400 /* Eh? It disappeared between config and open?? */
401 bdb->bi_db_has_config = 0;
405 bdb->bi_db_is_open = 1;
411 bdb_db_close( BackendDB *be )
414 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
415 struct bdb_db_info *db;
416 bdb_idl_cache_entry_t *entry, *next_entry;
418 bdb->bi_db_is_open = 0;
420 ber_bvarray_free( bdb->bi_db_config );
422 while( bdb->bi_ndatabases-- ) {
423 db = bdb->bi_databases[bdb->bi_ndatabases];
424 rc = db->bdi_db->close( db->bdi_db, 0 );
425 /* Lower numbered names are not strdup'd */
426 if( bdb->bi_ndatabases >= BDB_NDB )
427 free( db->bdi_name );
430 free( bdb->bi_databases );
431 bdb_attr_index_destroy( bdb->bi_attrs );
433 bdb_cache_release_all (&bdb->bi_cache);
435 if ( bdb->bi_idl_cache_max_size ) {
436 ldap_pvt_thread_rdwr_wlock ( &bdb->bi_idl_tree_rwlock );
437 avl_free( bdb->bi_idl_tree, NULL );
438 entry = bdb->bi_idl_lru_head;
439 while ( entry != NULL ) {
440 next_entry = entry->idl_lru_next;
443 free( entry->kstr.bv_val );
447 ldap_pvt_thread_rdwr_wunlock ( &bdb->bi_idl_tree_rwlock );
450 if ( !( slapMode & SLAP_TOOL_QUICK )) {
451 XLOCK_ID_FREE(bdb->bi_dbenv, bdb->bi_cache.c_locker);
458 bdb_db_destroy( BackendDB *be )
461 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
463 /* close db environment */
464 if( bdb->bi_dbenv ) {
465 /* force a checkpoint */
466 if ( !( slapMode & SLAP_TOOL_QUICK )) {
467 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
469 Debug( LDAP_DEBUG_ANY,
470 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
471 db_strerror(rc), rc, 0 );
475 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
476 bdb->bi_dbenv = NULL;
478 Debug( LDAP_DEBUG_ANY,
479 "bdb_db_destroy: close failed: %s (%d)\n",
480 db_strerror(rc), rc, 0 );
485 rc = alock_close( &bdb->bi_alock_info );
487 Debug( LDAP_DEBUG_ANY,
488 "bdb_db_destroy: alock_close failed\n", 0, 0, 0 );
492 if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
493 if( bdb->bi_db_config_path ) ch_free( bdb->bi_db_config_path );
495 ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
496 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_mutex );
497 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_dntree.bei_kids_mutex );
498 ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
499 ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
500 if ( bdb->bi_idl_cache_max_size ) {
501 ldap_pvt_thread_rdwr_destroy( &bdb->bi_idl_tree_rwlock );
502 ldap_pvt_thread_mutex_destroy( &bdb->bi_idl_tree_lrulock );
506 be->be_private = NULL;
517 static char *controls[] = {
519 LDAP_CONTROL_MANAGEDSAIT,
521 LDAP_CONTROL_PAGEDRESULTS,
522 #ifdef LDAP_CONTROL_SUBENTRIES
523 LDAP_CONTROL_SUBENTRIES,
525 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
526 LDAP_CONTROL_X_PERMISSIVE_MODIFY,
531 /* initialize the underlying database system */
532 Debug( LDAP_DEBUG_TRACE,
533 LDAP_XSTRING(bdb_back_initialize) ": initialize "
534 BDB_UCTYPE " backend\n", 0, 0, 0 );
537 SLAP_BFLAG_INCREMENT |
538 #ifdef BDB_SUBENTRIES
539 SLAP_BFLAG_SUBENTRIES |
542 SLAP_BFLAG_REFERRALS;
544 bi->bi_controls = controls;
546 { /* version check */
547 int major, minor, patch, ver;
548 char *version = db_version( &major, &minor, &patch );
552 /* All our stdio does an ASCII to EBCDIC conversion on
553 * the output. Strings from the BDB library are already
554 * in EBCDIC; we have to go back and forth...
556 strcpy( v2, version );
561 ver = (major << 24) | (minor << 16) | patch;
562 if( ver != DB_VERSION_FULL ) {
563 /* fail if a versions don't match */
564 Debug( LDAP_DEBUG_ANY,
565 LDAP_XSTRING(bdb_back_initialize) ": "
566 "BDB library version mismatch:"
567 " expected " DB_VERSION_STRING ","
568 " got %s\n", version, 0, 0 );
572 Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(bdb_back_initialize)
573 ": %s\n", version, 0, 0 );
576 db_env_set_func_free( ber_memfree );
577 db_env_set_func_malloc( (db_malloc *)ber_memalloc );
578 db_env_set_func_realloc( (db_realloc *)ber_memrealloc );
580 /* This is a no-op on a NO_THREAD build. Leave the default
581 * alone so that BDB will sleep on interprocess conflicts.
583 db_env_set_func_yield( ldap_pvt_thread_yield );
591 bi->bi_db_init = bdb_db_init;
592 bi->bi_db_config = config_generic_wrapper;
593 bi->bi_db_open = bdb_db_open;
594 bi->bi_db_close = bdb_db_close;
595 bi->bi_db_destroy = bdb_db_destroy;
597 bi->bi_op_add = bdb_add;
598 bi->bi_op_bind = bdb_bind;
599 bi->bi_op_compare = bdb_compare;
600 bi->bi_op_delete = bdb_delete;
601 bi->bi_op_modify = bdb_modify;
602 bi->bi_op_modrdn = bdb_modrdn;
603 bi->bi_op_search = bdb_search;
605 bi->bi_op_unbind = 0;
607 bi->bi_extended = bdb_extended;
609 bi->bi_chk_referrals = bdb_referrals;
610 bi->bi_operational = bdb_operational;
611 bi->bi_has_subordinates = bdb_hasSubordinates;
612 bi->bi_entry_release_rw = bdb_entry_release;
613 bi->bi_entry_get_rw = bdb_entry_get;
616 * hooks for slap tools
618 bi->bi_tool_entry_open = bdb_tool_entry_open;
619 bi->bi_tool_entry_close = bdb_tool_entry_close;
620 bi->bi_tool_entry_first = bdb_tool_entry_next;
621 bi->bi_tool_entry_next = bdb_tool_entry_next;
622 bi->bi_tool_entry_get = bdb_tool_entry_get;
623 bi->bi_tool_entry_put = bdb_tool_entry_put;
624 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
625 bi->bi_tool_sync = 0;
626 bi->bi_tool_dn2id_get = bdb_tool_dn2id_get;
627 bi->bi_tool_id2entry_get = bdb_tool_id2entry_get;
628 bi->bi_tool_entry_modify = bdb_tool_entry_modify;
630 bi->bi_connection_init = 0;
631 bi->bi_connection_destroy = 0;
633 rc = bdb_back_init_cf(bi);
638 #if (SLAPD_BDB == SLAPD_MOD_DYNAMIC && !defined(BDB_HIER)) || \
639 (SLAPD_HDB == SLAPD_MOD_DYNAMIC && defined(BDB_HIER))
641 /* conditionally define the init_module() function */
643 SLAP_BACKEND_INIT_MODULE( hdb )
644 #else /* !BDB_HIER */
645 SLAP_BACKEND_INIT_MODULE( bdb )
646 #endif /* !BDB_HIER */
648 #endif /* SLAPD_[BH]DB == SLAPD_MOD_DYNAMIC */