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 /* shouldn't need this, but ... */
397 if ( bv.bv_val[bv.bv_len-1] == '\r' ) {
399 bv.bv_val[bv.bv_len] = '\0';
401 ber_bvarray_add( &bdb->bi_db_config, &bv );
405 /* Eh? It disappeared between config and open?? */
406 bdb->bi_db_has_config = 0;
410 bdb->bi_db_is_open = 1;
416 bdb_db_close( BackendDB *be )
419 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
420 struct bdb_db_info *db;
421 bdb_idl_cache_entry_t *entry, *next_entry;
423 bdb->bi_db_is_open = 0;
425 ber_bvarray_free( bdb->bi_db_config );
427 while( bdb->bi_ndatabases-- ) {
428 db = bdb->bi_databases[bdb->bi_ndatabases];
429 rc = db->bdi_db->close( db->bdi_db, 0 );
430 /* Lower numbered names are not strdup'd */
431 if( bdb->bi_ndatabases >= BDB_NDB )
432 free( db->bdi_name );
435 free( bdb->bi_databases );
436 bdb_attr_index_destroy( bdb->bi_attrs );
438 bdb_cache_release_all (&bdb->bi_cache);
440 if ( bdb->bi_idl_cache_max_size ) {
441 ldap_pvt_thread_rdwr_wlock ( &bdb->bi_idl_tree_rwlock );
442 avl_free( bdb->bi_idl_tree, NULL );
443 entry = bdb->bi_idl_lru_head;
444 while ( entry != NULL ) {
445 next_entry = entry->idl_lru_next;
448 free( entry->kstr.bv_val );
452 ldap_pvt_thread_rdwr_wunlock ( &bdb->bi_idl_tree_rwlock );
455 if ( !( slapMode & SLAP_TOOL_QUICK )) {
456 XLOCK_ID_FREE(bdb->bi_dbenv, bdb->bi_cache.c_locker);
463 bdb_db_destroy( BackendDB *be )
466 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
468 /* close db environment */
469 if( bdb->bi_dbenv ) {
470 /* force a checkpoint */
471 if ( !( slapMode & SLAP_TOOL_QUICK )) {
472 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
474 Debug( LDAP_DEBUG_ANY,
475 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
476 db_strerror(rc), rc, 0 );
480 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
481 bdb->bi_dbenv = NULL;
483 Debug( LDAP_DEBUG_ANY,
484 "bdb_db_destroy: close failed: %s (%d)\n",
485 db_strerror(rc), rc, 0 );
490 rc = alock_close( &bdb->bi_alock_info );
492 Debug( LDAP_DEBUG_ANY,
493 "bdb_db_destroy: alock_close failed\n", 0, 0, 0 );
497 if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
498 if( bdb->bi_db_config_path ) ch_free( bdb->bi_db_config_path );
500 ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
501 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_mutex );
502 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_dntree.bei_kids_mutex );
503 ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
504 ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
505 if ( bdb->bi_idl_cache_max_size ) {
506 ldap_pvt_thread_rdwr_destroy( &bdb->bi_idl_tree_rwlock );
507 ldap_pvt_thread_mutex_destroy( &bdb->bi_idl_tree_lrulock );
511 be->be_private = NULL;
522 static char *controls[] = {
524 LDAP_CONTROL_MANAGEDSAIT,
526 LDAP_CONTROL_PAGEDRESULTS,
527 #ifdef LDAP_CONTROL_SUBENTRIES
528 LDAP_CONTROL_SUBENTRIES,
530 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
531 LDAP_CONTROL_X_PERMISSIVE_MODIFY,
536 /* initialize the underlying database system */
537 Debug( LDAP_DEBUG_TRACE,
538 LDAP_XSTRING(bdb_back_initialize) ": initialize "
539 BDB_UCTYPE " backend\n", 0, 0, 0 );
542 SLAP_BFLAG_INCREMENT |
543 #ifdef BDB_SUBENTRIES
544 SLAP_BFLAG_SUBENTRIES |
547 SLAP_BFLAG_REFERRALS;
549 bi->bi_controls = controls;
551 { /* version check */
552 int major, minor, patch, ver;
553 char *version = db_version( &major, &minor, &patch );
557 /* All our stdio does an ASCII to EBCDIC conversion on
558 * the output. Strings from the BDB library are already
559 * in EBCDIC; we have to go back and forth...
561 strcpy( v2, version );
566 ver = (major << 24) | (minor << 16) | patch;
567 if( ver != DB_VERSION_FULL ) {
568 /* fail if a versions don't match */
569 Debug( LDAP_DEBUG_ANY,
570 LDAP_XSTRING(bdb_back_initialize) ": "
571 "BDB library version mismatch:"
572 " expected " DB_VERSION_STRING ","
573 " got %s\n", version, 0, 0 );
577 Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(bdb_back_initialize)
578 ": %s\n", version, 0, 0 );
581 db_env_set_func_free( ber_memfree );
582 db_env_set_func_malloc( (db_malloc *)ber_memalloc );
583 db_env_set_func_realloc( (db_realloc *)ber_memrealloc );
585 /* This is a no-op on a NO_THREAD build. Leave the default
586 * alone so that BDB will sleep on interprocess conflicts.
588 db_env_set_func_yield( ldap_pvt_thread_yield );
596 bi->bi_db_init = bdb_db_init;
597 bi->bi_db_config = config_generic_wrapper;
598 bi->bi_db_open = bdb_db_open;
599 bi->bi_db_close = bdb_db_close;
600 bi->bi_db_destroy = bdb_db_destroy;
602 bi->bi_op_add = bdb_add;
603 bi->bi_op_bind = bdb_bind;
604 bi->bi_op_compare = bdb_compare;
605 bi->bi_op_delete = bdb_delete;
606 bi->bi_op_modify = bdb_modify;
607 bi->bi_op_modrdn = bdb_modrdn;
608 bi->bi_op_search = bdb_search;
610 bi->bi_op_unbind = 0;
612 bi->bi_extended = bdb_extended;
614 bi->bi_chk_referrals = bdb_referrals;
615 bi->bi_operational = bdb_operational;
616 bi->bi_has_subordinates = bdb_hasSubordinates;
617 bi->bi_entry_release_rw = bdb_entry_release;
618 bi->bi_entry_get_rw = bdb_entry_get;
621 * hooks for slap tools
623 bi->bi_tool_entry_open = bdb_tool_entry_open;
624 bi->bi_tool_entry_close = bdb_tool_entry_close;
625 bi->bi_tool_entry_first = bdb_tool_entry_next;
626 bi->bi_tool_entry_next = bdb_tool_entry_next;
627 bi->bi_tool_entry_get = bdb_tool_entry_get;
628 bi->bi_tool_entry_put = bdb_tool_entry_put;
629 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
630 bi->bi_tool_sync = 0;
631 bi->bi_tool_dn2id_get = bdb_tool_dn2id_get;
632 bi->bi_tool_id2entry_get = bdb_tool_id2entry_get;
633 bi->bi_tool_entry_modify = bdb_tool_entry_modify;
635 bi->bi_connection_init = 0;
636 bi->bi_connection_destroy = 0;
638 rc = bdb_back_init_cf(bi);
643 #if (SLAPD_BDB == SLAPD_MOD_DYNAMIC && !defined(BDB_HIER)) || \
644 (SLAPD_HDB == SLAPD_MOD_DYNAMIC && defined(BDB_HIER))
646 /* conditionally define the init_module() function */
648 SLAP_BACKEND_INIT_MODULE( hdb )
649 #else /* !BDB_HIER */
650 SLAP_BACKEND_INIT_MODULE( bdb )
651 #endif /* !BDB_HIER */
653 #endif /* SLAPD_[BH]DB == SLAPD_MOD_DYNAMIC */