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 );
388 bdb_db_close( BackendDB *be )
391 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
392 struct bdb_db_info *db;
393 bdb_idl_cache_entry_t *entry, *next_entry;
395 while( bdb->bi_ndatabases-- ) {
396 db = bdb->bi_databases[bdb->bi_ndatabases];
397 rc = db->bdi_db->close( db->bdi_db, 0 );
398 /* Lower numbered names are not strdup'd */
399 if( bdb->bi_ndatabases >= BDB_NDB )
400 free( db->bdi_name );
403 free( bdb->bi_databases );
404 bdb_attr_index_destroy( bdb->bi_attrs );
406 bdb_cache_release_all (&bdb->bi_cache);
408 if ( bdb->bi_idl_cache_max_size ) {
409 ldap_pvt_thread_rdwr_wlock ( &bdb->bi_idl_tree_rwlock );
410 avl_free( bdb->bi_idl_tree, NULL );
411 entry = bdb->bi_idl_lru_head;
412 while ( entry != NULL ) {
413 next_entry = entry->idl_lru_next;
416 free( entry->kstr.bv_val );
420 ldap_pvt_thread_rdwr_wunlock ( &bdb->bi_idl_tree_rwlock );
423 if ( !( slapMode & SLAP_TOOL_QUICK )) {
424 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;
436 /* close db environment */
437 if( bdb->bi_dbenv ) {
438 /* force a checkpoint */
439 if ( !( slapMode & SLAP_TOOL_QUICK )) {
440 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
442 Debug( LDAP_DEBUG_ANY,
443 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
444 db_strerror(rc), rc, 0 );
448 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
449 bdb->bi_dbenv = NULL;
451 Debug( LDAP_DEBUG_ANY,
452 "bdb_db_destroy: close failed: %s (%d)\n",
453 db_strerror(rc), rc, 0 );
458 rc = alock_close( &bdb->bi_alock_info );
460 Debug( LDAP_DEBUG_ANY,
461 "bdb_db_destroy: alock_close failed\n", 0, 0, 0 );
465 if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
467 ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
468 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_mutex );
469 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_dntree.bei_kids_mutex );
470 ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
471 ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
472 if ( bdb->bi_idl_cache_max_size ) {
473 ldap_pvt_thread_rdwr_destroy( &bdb->bi_idl_tree_rwlock );
474 ldap_pvt_thread_mutex_destroy( &bdb->bi_idl_tree_lrulock );
478 be->be_private = NULL;
489 static char *controls[] = {
491 LDAP_CONTROL_MANAGEDSAIT,
493 LDAP_CONTROL_PAGEDRESULTS,
494 #ifdef LDAP_CONTROL_SUBENTRIES
495 LDAP_CONTROL_SUBENTRIES,
497 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
498 LDAP_CONTROL_X_PERMISSIVE_MODIFY,
503 /* initialize the underlying database system */
504 Debug( LDAP_DEBUG_TRACE,
505 LDAP_XSTRING(bdb_back_initialize) ": initialize "
506 BDB_UCTYPE " backend\n", 0, 0, 0 );
509 SLAP_BFLAG_INCREMENT |
510 #ifdef BDB_SUBENTRIES
511 SLAP_BFLAG_SUBENTRIES |
514 SLAP_BFLAG_REFERRALS;
516 bi->bi_controls = controls;
518 { /* version check */
519 int major, minor, patch, ver;
520 char *version = db_version( &major, &minor, &patch );
524 /* All our stdio does an ASCII to EBCDIC conversion on
525 * the output. Strings from the BDB library are already
526 * in EBCDIC; we have to go back and forth...
528 strcpy( v2, version );
533 ver = (major << 24) | (minor << 16) | patch;
534 if( ver != DB_VERSION_FULL ) {
535 /* fail if a versions don't match */
536 Debug( LDAP_DEBUG_ANY,
537 LDAP_XSTRING(bdb_back_initialize) ": "
538 "BDB library version mismatch:"
539 " expected " DB_VERSION_STRING ","
540 " got %s\n", version, 0, 0 );
544 Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(bdb_back_initialize)
545 ": %s\n", version, 0, 0 );
548 db_env_set_func_free( ber_memfree );
549 db_env_set_func_malloc( (db_malloc *)ber_memalloc );
550 db_env_set_func_realloc( (db_realloc *)ber_memrealloc );
552 /* This is a no-op on a NO_THREAD build. Leave the default
553 * alone so that BDB will sleep on interprocess conflicts.
555 db_env_set_func_yield( ldap_pvt_thread_yield );
563 bi->bi_db_init = bdb_db_init;
564 bi->bi_db_config = config_generic_wrapper;
565 bi->bi_db_open = bdb_db_open;
566 bi->bi_db_close = bdb_db_close;
567 bi->bi_db_destroy = bdb_db_destroy;
569 bi->bi_op_add = bdb_add;
570 bi->bi_op_bind = bdb_bind;
571 bi->bi_op_compare = bdb_compare;
572 bi->bi_op_delete = bdb_delete;
573 bi->bi_op_modify = bdb_modify;
574 bi->bi_op_modrdn = bdb_modrdn;
575 bi->bi_op_search = bdb_search;
577 bi->bi_op_unbind = 0;
579 bi->bi_extended = bdb_extended;
581 bi->bi_chk_referrals = bdb_referrals;
582 bi->bi_operational = bdb_operational;
583 bi->bi_has_subordinates = bdb_hasSubordinates;
584 bi->bi_entry_release_rw = bdb_entry_release;
585 bi->bi_entry_get_rw = bdb_entry_get;
588 * hooks for slap tools
590 bi->bi_tool_entry_open = bdb_tool_entry_open;
591 bi->bi_tool_entry_close = bdb_tool_entry_close;
592 bi->bi_tool_entry_first = bdb_tool_entry_next;
593 bi->bi_tool_entry_next = bdb_tool_entry_next;
594 bi->bi_tool_entry_get = bdb_tool_entry_get;
595 bi->bi_tool_entry_put = bdb_tool_entry_put;
596 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
597 bi->bi_tool_sync = 0;
598 bi->bi_tool_dn2id_get = bdb_tool_dn2id_get;
599 bi->bi_tool_id2entry_get = bdb_tool_id2entry_get;
600 bi->bi_tool_entry_modify = bdb_tool_entry_modify;
602 bi->bi_connection_init = 0;
603 bi->bi_connection_destroy = 0;
605 rc = bdb_back_init_cf(bi);
610 #if (SLAPD_BDB == SLAPD_MOD_DYNAMIC && !defined(BDB_HIER)) || \
611 (SLAPD_HDB == SLAPD_MOD_DYNAMIC && defined(BDB_HIER))
613 /* conditionally define the init_module() function */
615 SLAP_BACKEND_INIT_MODULE( hdb )
616 #else /* !BDB_HIER */
617 SLAP_BACKEND_INIT_MODULE( bdb )
618 #endif /* !BDB_HIER */
620 #endif /* SLAPD_[BH]DB == SLAPD_MOD_DYNAMIC */