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 );
86 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
87 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
88 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
93 bdb_db_open( BackendDB *be )
96 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
99 char path[MAXPATHLEN];
102 Debug( LDAP_DEBUG_ARGS,
104 be->be_suffix[0].bv_val, 0, 0 );
106 #ifndef BDB_MULTIPLE_SUFFIXES
107 if ( be->be_suffix[1].bv_val ) {
108 Debug( LDAP_DEBUG_ANY,
109 "bdb_db_open: only one suffix allowed\n", 0, 0, 0 );
113 /* we should check existance of dbenv_home and db_directory */
115 rc = db_env_create( &bdb->bi_dbenv, 0 );
117 Debug( LDAP_DEBUG_ANY,
118 "bdb_db_open: db_env_create failed: %s (%d)\n",
119 db_strerror(rc), rc, 0 );
123 flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE;
125 if ( !( slapMode & SLAP_TOOL_QUICK ))
126 flags |= DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN;
128 rc = alock_open( &bdb->bi_alock_info, "slapd", bdb->bi_dbenv_home,
129 slapMode & SLAP_TOOL_READONLY ? ALOCK_LOCKED : ALOCK_UNIQUE );
130 if( rc == ALOCK_RECOVER ) {
131 Debug( LDAP_DEBUG_ANY,
132 "bdb_db_open: alock_open: recovery required\n", 0, 0, 0 );
134 } else if( rc == ALOCK_BUSY ) {
135 Debug( LDAP_DEBUG_ANY,
136 "bdb_db_open: alock_open: database in use\n", 0, 0, 0 );
138 } else if( rc != ALOCK_CLEAN ) {
139 Debug( LDAP_DEBUG_ANY,
140 "bdb_db_open: alock_open: database unstable\n", 0, 0, 0 );
144 /* If a key was set, use shared memory for the BDB environment */
145 if ( bdb->bi_shm_key ) {
146 bdb->bi_dbenv->set_shm_key( bdb->bi_dbenv, bdb->bi_shm_key );
147 flags |= DB_SYSTEM_MEM;
150 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
151 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
152 bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
154 /* One long-lived TXN per thread, two TXNs per write op */
155 bdb->bi_dbenv->set_tx_max( bdb->bi_dbenv, connection_pool_max * 3 );
157 #ifdef SLAP_ZONE_ALLOC
158 if ( bdb->bi_cache.c_maxsize ) {
159 bdb->bi_cache.c_zctx = slap_zn_mem_create(
167 if ( bdb->bi_idl_cache_max_size ) {
168 bdb->bi_idl_tree = NULL;
169 ldap_pvt_thread_rdwr_init( &bdb->bi_idl_tree_rwlock );
170 ldap_pvt_thread_mutex_init( &bdb->bi_idl_tree_lrulock );
171 bdb->bi_idl_cache_size = 0;
176 char dir[MAXPATHLEN], *ptr;
178 if (bdb->bi_dbenv_home[0] == '.') {
179 /* If home is a relative path, relative subdirs
180 * are just concat'd by BDB. We don't want the
181 * path to be concat'd twice, e.g.
182 * ./test-db/./test-db/tmp
186 ptr = lutil_strcopy( dir, bdb->bi_dbenv_home );
187 *ptr++ = LDAP_DIRSEP[0];
193 strcpy( ptr, BDB_TMP_SUBDIR );
197 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
199 Debug( LDAP_DEBUG_ANY,
200 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
201 dir, db_strerror(rc), rc );
205 strcpy( ptr, BDB_LG_SUBDIR );
209 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
211 Debug( LDAP_DEBUG_ANY,
212 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
213 dir, db_strerror(rc), rc );
217 strcpy( ptr, BDB_DATA_SUBDIR );
221 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
223 Debug( LDAP_DEBUG_ANY,
224 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
225 dir, db_strerror(rc), rc );
231 if( bdb->bi_dbenv_xflags != 0 ) {
232 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
233 bdb->bi_dbenv_xflags, 1);
235 Debug( LDAP_DEBUG_ANY,
236 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
237 db_strerror(rc), rc, 0 );
242 Debug( LDAP_DEBUG_TRACE,
243 "bdb_db_open: dbenv_open(%s)\n",
244 bdb->bi_dbenv_home, 0, 0);
247 strcpy( path, bdb->bi_dbenv_home );
249 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
252 bdb->bi_dbenv_mode );
254 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
257 bdb->bi_dbenv_mode );
260 Debug( LDAP_DEBUG_ANY,
261 "bdb_db_open: dbenv_open failed: %s (%d)\n",
262 db_strerror(rc), rc, 0 );
265 if( flags & DB_RECOVER ) {
266 rc = alock_recover (&bdb->bi_alock_info);
268 Debug( LDAP_DEBUG_ANY,
269 "bdb_db_open: unable to alock_recover\n", 0, 0, 0 );
274 flags = DB_THREAD | bdb->bi_db_opflags;
276 #ifdef DB_AUTO_COMMIT
277 if ( !( slapMode & SLAP_TOOL_QUICK ))
278 flags |= DB_AUTO_COMMIT;
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 if ( !( slapMode & SLAP_TOOL_QUICK )) {
373 XLOCK_ID(bdb->bi_dbenv, &bdb->bi_cache.c_locker);
376 /* If we're in server mode and time-based checkpointing is enabled,
377 * submit a task to perform periodic checkpoints.
379 if (( slapMode & SLAP_SERVER_MODE ) && bdb->bi_txn_cp &&
380 bdb->bi_txn_cp_min ) {
381 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
382 ldap_pvt_runqueue_insert( &slapd_rq, bdb->bi_txn_cp_min*60,
383 bdb_checkpoint, bdb );
384 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
387 if ( slapMode & SLAP_SERVER_MODE && bdb->bi_db_has_config ) {
388 char buf[SLAP_TEXT_BUFLEN];
389 FILE *f = fopen( bdb->bi_db_config_path, "r" );
393 while ( fgets( buf, sizeof(buf), f )) {
394 ber_str2bv( buf, 0, 1, &bv );
395 if ( bv.bv_val[bv.bv_len-1] == '\n' ) {
397 bv.bv_val[bv.bv_len] = '\0';
399 /* shouldn't need this, but ... */
400 if ( bv.bv_val[bv.bv_len-1] == '\r' ) {
402 bv.bv_val[bv.bv_len] = '\0';
404 ber_bvarray_add( &bdb->bi_db_config, &bv );
408 /* Eh? It disappeared between config and open?? */
409 bdb->bi_db_has_config = 0;
413 bdb->bi_db_is_open = 1;
419 bdb_db_close( BackendDB *be )
422 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
423 struct bdb_db_info *db;
424 bdb_idl_cache_entry_t *entry, *next_entry;
426 bdb->bi_db_is_open = 0;
428 ber_bvarray_free( bdb->bi_db_config );
430 while( bdb->bi_ndatabases-- ) {
431 db = bdb->bi_databases[bdb->bi_ndatabases];
432 rc = db->bdi_db->close( db->bdi_db, 0 );
433 /* Lower numbered names are not strdup'd */
434 if( bdb->bi_ndatabases >= BDB_NDB )
435 free( db->bdi_name );
438 free( bdb->bi_databases );
439 bdb_attr_index_destroy( bdb->bi_attrs );
441 bdb_cache_release_all (&bdb->bi_cache);
443 if ( bdb->bi_idl_cache_max_size ) {
444 ldap_pvt_thread_rdwr_wlock ( &bdb->bi_idl_tree_rwlock );
445 avl_free( bdb->bi_idl_tree, NULL );
446 entry = bdb->bi_idl_lru_head;
447 while ( entry != NULL ) {
448 next_entry = entry->idl_lru_next;
451 free( entry->kstr.bv_val );
455 ldap_pvt_thread_rdwr_wunlock ( &bdb->bi_idl_tree_rwlock );
458 if ( !( slapMode & SLAP_TOOL_QUICK )) {
459 XLOCK_ID_FREE(bdb->bi_dbenv, bdb->bi_cache.c_locker);
466 bdb_db_destroy( BackendDB *be )
469 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
471 /* close db environment */
472 if( bdb->bi_dbenv ) {
473 /* force a checkpoint */
474 if ( !( slapMode & SLAP_TOOL_QUICK )) {
475 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
477 Debug( LDAP_DEBUG_ANY,
478 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
479 db_strerror(rc), rc, 0 );
483 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
484 bdb->bi_dbenv = NULL;
486 Debug( LDAP_DEBUG_ANY,
487 "bdb_db_destroy: close failed: %s (%d)\n",
488 db_strerror(rc), rc, 0 );
493 rc = alock_close( &bdb->bi_alock_info );
495 Debug( LDAP_DEBUG_ANY,
496 "bdb_db_destroy: alock_close failed\n", 0, 0, 0 );
500 if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
501 if( bdb->bi_db_config_path ) ch_free( bdb->bi_db_config_path );
503 ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
504 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_mutex );
505 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_dntree.bei_kids_mutex );
506 ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
507 ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
508 if ( bdb->bi_idl_cache_max_size ) {
509 ldap_pvt_thread_rdwr_destroy( &bdb->bi_idl_tree_rwlock );
510 ldap_pvt_thread_mutex_destroy( &bdb->bi_idl_tree_lrulock );
514 be->be_private = NULL;
525 static char *controls[] = {
527 LDAP_CONTROL_MANAGEDSAIT,
529 LDAP_CONTROL_PAGEDRESULTS,
530 #ifdef LDAP_CONTROL_SUBENTRIES
531 LDAP_CONTROL_SUBENTRIES,
533 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
534 LDAP_CONTROL_X_PERMISSIVE_MODIFY,
539 /* initialize the underlying database system */
540 Debug( LDAP_DEBUG_TRACE,
541 LDAP_XSTRING(bdb_back_initialize) ": initialize "
542 BDB_UCTYPE " backend\n", 0, 0, 0 );
545 SLAP_BFLAG_INCREMENT |
546 #ifdef BDB_SUBENTRIES
547 SLAP_BFLAG_SUBENTRIES |
550 SLAP_BFLAG_REFERRALS;
552 bi->bi_controls = controls;
554 { /* version check */
555 int major, minor, patch, ver;
556 char *version = db_version( &major, &minor, &patch );
560 /* All our stdio does an ASCII to EBCDIC conversion on
561 * the output. Strings from the BDB library are already
562 * in EBCDIC; we have to go back and forth...
564 strcpy( v2, version );
569 ver = (major << 24) | (minor << 16) | patch;
570 if( ver != DB_VERSION_FULL ) {
571 /* fail if a versions don't match */
572 Debug( LDAP_DEBUG_ANY,
573 LDAP_XSTRING(bdb_back_initialize) ": "
574 "BDB library version mismatch:"
575 " expected " DB_VERSION_STRING ","
576 " got %s\n", version, 0, 0 );
580 Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(bdb_back_initialize)
581 ": %s\n", version, 0, 0 );
584 db_env_set_func_free( ber_memfree );
585 db_env_set_func_malloc( (db_malloc *)ber_memalloc );
586 db_env_set_func_realloc( (db_realloc *)ber_memrealloc );
588 /* This is a no-op on a NO_THREAD build. Leave the default
589 * alone so that BDB will sleep on interprocess conflicts.
591 db_env_set_func_yield( ldap_pvt_thread_yield );
599 bi->bi_db_init = bdb_db_init;
600 bi->bi_db_config = config_generic_wrapper;
601 bi->bi_db_open = bdb_db_open;
602 bi->bi_db_close = bdb_db_close;
603 bi->bi_db_destroy = bdb_db_destroy;
605 bi->bi_op_add = bdb_add;
606 bi->bi_op_bind = bdb_bind;
607 bi->bi_op_compare = bdb_compare;
608 bi->bi_op_delete = bdb_delete;
609 bi->bi_op_modify = bdb_modify;
610 bi->bi_op_modrdn = bdb_modrdn;
611 bi->bi_op_search = bdb_search;
613 bi->bi_op_unbind = 0;
615 bi->bi_extended = bdb_extended;
617 bi->bi_chk_referrals = bdb_referrals;
618 bi->bi_operational = bdb_operational;
619 bi->bi_has_subordinates = bdb_hasSubordinates;
620 bi->bi_entry_release_rw = bdb_entry_release;
621 bi->bi_entry_get_rw = bdb_entry_get;
624 * hooks for slap tools
626 bi->bi_tool_entry_open = bdb_tool_entry_open;
627 bi->bi_tool_entry_close = bdb_tool_entry_close;
628 bi->bi_tool_entry_first = bdb_tool_entry_next;
629 bi->bi_tool_entry_next = bdb_tool_entry_next;
630 bi->bi_tool_entry_get = bdb_tool_entry_get;
631 bi->bi_tool_entry_put = bdb_tool_entry_put;
632 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
633 bi->bi_tool_sync = 0;
634 bi->bi_tool_dn2id_get = bdb_tool_dn2id_get;
635 bi->bi_tool_id2entry_get = bdb_tool_id2entry_get;
636 bi->bi_tool_entry_modify = bdb_tool_entry_modify;
638 bi->bi_connection_init = 0;
639 bi->bi_connection_destroy = 0;
641 rc = bdb_back_init_cf( bi );
646 #if (SLAPD_BDB == SLAPD_MOD_DYNAMIC && !defined(BDB_HIER)) || \
647 (SLAPD_HDB == SLAPD_MOD_DYNAMIC && defined(BDB_HIER))
649 /* conditionally define the init_module() function */
651 SLAP_BACKEND_INIT_MODULE( hdb )
652 #else /* !BDB_HIER */
653 SLAP_BACKEND_INIT_MODULE( bdb )
654 #endif /* !BDB_HIER */
656 #endif /* SLAPD_[BH]DB == SLAPD_MOD_DYNAMIC */