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);
45 bdb_db_init( BackendDB *be )
49 Debug( LDAP_DEBUG_ANY,
50 LDAP_XSTRING(bdb_db_init) ": Initializing "
51 BDB_UCTYPE " database\n", 0, 0, 0 );
53 /* allocate backend-database-specific stuff */
54 bdb = (struct bdb_info *) ch_calloc( 1, sizeof(struct bdb_info) );
56 /* DBEnv parameters */
57 bdb->bi_dbenv_home = ch_strdup( SLAPD_DEFAULT_DB_DIR );
58 bdb->bi_dbenv_xflags = 0;
59 bdb->bi_dbenv_mode = SLAPD_DEFAULT_DB_MODE;
61 bdb->bi_cache.c_maxsize = DEFAULT_CACHE_SIZE;
63 bdb->bi_lock_detect = DB_LOCK_DEFAULT;
64 bdb->bi_search_stack_depth = DEFAULT_SEARCH_STACK_DEPTH;
65 bdb->bi_search_stack = NULL;
67 ldap_pvt_thread_mutex_init( &bdb->bi_database_mutex );
68 ldap_pvt_thread_mutex_init( &bdb->bi_lastid_mutex );
69 ldap_pvt_thread_mutex_init( &bdb->bi_cache.lru_mutex );
70 ldap_pvt_thread_mutex_init( &bdb->bi_cache.c_dntree.bei_kids_mutex );
71 ldap_pvt_thread_rdwr_init ( &bdb->bi_cache.c_rwlock );
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;
126 /* Never do automatic recovery, must perform it manually.
127 * Otherwise restarting with gentlehup will corrupt the
130 if( !(slapMode & SLAP_TOOL_MODE) ) flags |= DB_RECOVER;
133 /* If a key was set, use shared memory for the BDB environment */
134 if ( bdb->bi_shm_key ) {
135 bdb->bi_dbenv->set_shm_key( bdb->bi_dbenv, bdb->bi_shm_key );
136 flags |= DB_SYSTEM_MEM;
139 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
140 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
141 bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
143 /* One long-lived TXN per thread, two TXNs per write op */
144 bdb->bi_dbenv->set_tx_max( bdb->bi_dbenv, connection_pool_max * 3 );
146 #ifdef SLAP_ZONE_ALLOC
147 if ( bdb->bi_cache.c_maxsize ) {
148 bdb->bi_cache.c_zctx = slap_zn_mem_create(
156 if ( bdb->bi_idl_cache_max_size ) {
157 bdb->bi_idl_tree = NULL;
158 ldap_pvt_thread_rdwr_init( &bdb->bi_idl_tree_rwlock );
159 ldap_pvt_thread_mutex_init( &bdb->bi_idl_tree_lrulock );
160 bdb->bi_idl_cache_size = 0;
165 char dir[MAXPATHLEN], *ptr;
167 if (bdb->bi_dbenv_home[0] == '.') {
168 /* If home is a relative path, relative subdirs
169 * are just concat'd by BDB. We don't want the
170 * path to be concat'd twice, e.g.
171 * ./test-db/./test-db/tmp
175 ptr = lutil_strcopy( dir, bdb->bi_dbenv_home );
176 *ptr++ = LDAP_DIRSEP[0];
182 strcpy( ptr, BDB_TMP_SUBDIR );
186 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
188 Debug( LDAP_DEBUG_ANY,
189 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
190 dir, db_strerror(rc), rc );
194 strcpy( ptr, BDB_LG_SUBDIR );
198 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
200 Debug( LDAP_DEBUG_ANY,
201 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
202 dir, db_strerror(rc), rc );
206 strcpy( ptr, BDB_DATA_SUBDIR );
210 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
212 Debug( LDAP_DEBUG_ANY,
213 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
214 dir, db_strerror(rc), rc );
220 if( bdb->bi_dbenv_xflags != 0 ) {
221 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
222 bdb->bi_dbenv_xflags, 1);
224 Debug( LDAP_DEBUG_ANY,
225 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
226 db_strerror(rc), rc, 0 );
231 Debug( LDAP_DEBUG_TRACE,
232 "bdb_db_open: dbenv_open(%s)\n",
233 bdb->bi_dbenv_home, 0, 0);
236 strcpy( path, bdb->bi_dbenv_home );
238 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
241 bdb->bi_dbenv_mode );
243 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
246 bdb->bi_dbenv_mode );
249 Debug( LDAP_DEBUG_ANY,
250 "bdb_db_open: dbenv_open failed: %s (%d)\n",
251 db_strerror(rc), rc, 0 );
255 flags = DB_THREAD | bdb->bi_db_opflags;
257 #ifdef DB_AUTO_COMMIT
258 if ( !( slapMode & SLAP_TOOL_QUICK ))
259 flags |= DB_AUTO_COMMIT;
262 bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
263 BDB_INDICES * sizeof(struct bdb_db_info *) );
265 /* open (and create) main database */
266 for( i = 0; bdbi_databases[i].name; i++ ) {
267 struct bdb_db_info *db;
269 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
271 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
273 Debug( LDAP_DEBUG_ANY,
274 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
275 bdb->bi_dbenv_home, db_strerror(rc), rc );
279 if( i == BDB_ID2ENTRY ) {
280 rc = db->bdi_db->set_pagesize( db->bdi_db,
281 BDB_ID2ENTRY_PAGESIZE );
282 if ( slapMode & SLAP_TOOL_READMAIN ) {
288 rc = db->bdi_db->set_flags( db->bdi_db,
289 DB_DUP | DB_DUPSORT );
291 if ( slapMode & SLAP_TOOL_READONLY ) {
297 if ( slapMode & (SLAP_TOOL_READONLY|SLAP_TOOL_READMAIN) ) {
303 rc = db->bdi_db->set_pagesize( db->bdi_db,
308 strcpy( path, bdbi_databases[i].file );
310 rc = DB_OPEN( db->bdi_db,
312 /* bdbi_databases[i].name, */ NULL,
313 bdbi_databases[i].type,
314 bdbi_databases[i].flags | flags,
315 bdb->bi_dbenv_mode );
317 rc = DB_OPEN( db->bdi_db,
318 bdbi_databases[i].file,
319 /* bdbi_databases[i].name, */ NULL,
320 bdbi_databases[i].type,
321 bdbi_databases[i].flags | flags,
322 bdb->bi_dbenv_mode );
326 char buf[SLAP_TEXT_BUFLEN];
328 snprintf( buf, sizeof(buf), "%s/%s",
329 bdb->bi_dbenv_home, bdbi_databases[i].file );
330 Debug( LDAP_DEBUG_ANY,
331 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
332 buf, db_strerror(rc), rc );
336 flags &= ~(DB_CREATE | DB_RDONLY);
337 db->bdi_name = bdbi_databases[i].name;
338 bdb->bi_databases[i] = db;
341 bdb->bi_databases[i] = NULL;
342 bdb->bi_ndatabases = i;
345 rc = bdb_last_id( be, NULL );
347 Debug( LDAP_DEBUG_ANY,
348 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
349 bdb->bi_dbenv_home, db_strerror(rc), rc );
353 if ( !( slapMode & SLAP_TOOL_QUICK )) {
354 XLOCK_ID(bdb->bi_dbenv, &bdb->bi_cache.c_locker);
357 /* If we're in server mode and time-based checkpointing is enabled,
358 * submit a task to perform periodic checkpoints.
360 if ( slapMode & SLAP_SERVER_MODE && bdb->bi_txn_cp &&
361 bdb->bi_txn_cp_min ) {
362 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
363 ldap_pvt_runqueue_insert( &slapd_rq, bdb->bi_txn_cp_min*60,
364 bdb_checkpoint, bdb );
365 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
372 bdb_db_close( BackendDB *be )
375 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
376 struct bdb_db_info *db;
377 bdb_idl_cache_entry_t *entry, *next_entry;
379 while( bdb->bi_ndatabases-- ) {
380 db = bdb->bi_databases[bdb->bi_ndatabases];
381 rc = db->bdi_db->close( db->bdi_db, 0 );
382 /* Lower numbered names are not strdup'd */
383 if( bdb->bi_ndatabases >= BDB_NDB )
384 free( db->bdi_name );
387 free( bdb->bi_databases );
388 bdb_attr_index_destroy( bdb->bi_attrs );
390 bdb_cache_release_all (&bdb->bi_cache);
392 if ( bdb->bi_idl_cache_max_size ) {
393 ldap_pvt_thread_rdwr_wlock ( &bdb->bi_idl_tree_rwlock );
394 avl_free( bdb->bi_idl_tree, NULL );
395 entry = bdb->bi_idl_lru_head;
396 while ( entry != NULL ) {
397 next_entry = entry->idl_lru_next;
400 free( entry->kstr.bv_val );
404 ldap_pvt_thread_rdwr_wunlock ( &bdb->bi_idl_tree_rwlock );
407 if ( !( slapMode & SLAP_TOOL_QUICK )) {
408 XLOCK_ID_FREE(bdb->bi_dbenv, bdb->bi_cache.c_locker);
415 bdb_db_destroy( BackendDB *be )
418 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
419 Operation *ps = NULL;
420 Operation *psn = NULL;
421 void *saved_tmpmemctx = NULL;
423 /* close db environment */
424 if( bdb->bi_dbenv ) {
425 /* force a checkpoint */
426 if ( !( slapMode & SLAP_TOOL_QUICK )) {
427 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
429 Debug( LDAP_DEBUG_ANY,
430 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
431 db_strerror(rc), rc, 0 );
435 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
436 bdb->bi_dbenv = NULL;
438 Debug( LDAP_DEBUG_ANY,
439 "bdb_db_destroy: close failed: %s (%d)\n",
440 db_strerror(rc), rc, 0 );
445 if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
447 ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
448 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_mutex );
449 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_dntree.bei_kids_mutex );
450 ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
451 ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
452 if ( bdb->bi_idl_cache_max_size ) {
453 ldap_pvt_thread_rdwr_destroy( &bdb->bi_idl_tree_rwlock );
454 ldap_pvt_thread_mutex_destroy( &bdb->bi_idl_tree_lrulock );
458 be->be_private = NULL;
467 static char *controls[] = {
469 LDAP_CONTROL_MANAGEDSAIT,
471 LDAP_CONTROL_PAGEDRESULTS,
472 #ifdef LDAP_CONTROL_SUBENTRIES
473 LDAP_CONTROL_SUBENTRIES,
475 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
476 LDAP_CONTROL_X_PERMISSIVE_MODIFY,
481 /* initialize the underlying database system */
482 Debug( LDAP_DEBUG_TRACE,
483 LDAP_XSTRING(bdb_back_initialize) ": initialize "
484 BDB_UCTYPE " backend\n", 0, 0, 0 );
487 SLAP_BFLAG_INCREMENT |
488 #ifdef BDB_SUBENTRIES
489 SLAP_BFLAG_SUBENTRIES |
492 SLAP_BFLAG_REFERRALS;
494 bi->bi_controls = controls;
496 { /* version check */
497 int major, minor, patch, ver;
498 char *version = db_version( &major, &minor, &patch );
502 /* All our stdio does an ASCII to EBCDIC conversion on
503 * the output. Strings from the BDB library are already
504 * in EBCDIC; we have to go back and forth...
506 strcpy( v2, version );
511 ver = (major << 24) | (minor << 16) | patch;
512 if( ver < DB_VERSION_FULL )
514 Debug( LDAP_DEBUG_ANY,
515 LDAP_XSTRING(bdb_back_initialize) ": "
516 "BDB library version mismatch:"
517 " expected " DB_VERSION_STRING ","
518 " got %s\n", version, 0, 0 );
521 Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(bdb_back_initialize)
522 ": %s\n", version, 0, 0 );
525 db_env_set_func_free( ber_memfree );
526 db_env_set_func_malloc( (db_malloc *)ber_memalloc );
527 db_env_set_func_realloc( (db_realloc *)ber_memrealloc );
529 /* This is a no-op on a NO_THREAD build. Leave the default
530 * alone so that BDB will sleep on interprocess conflicts.
532 db_env_set_func_yield( ldap_pvt_thread_yield );
536 static char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
538 bdb_uuid.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ));
539 bdb_uuid.bv_val = uuidbuf;
547 bi->bi_db_init = bdb_db_init;
548 bi->bi_db_config = bdb_db_config;
549 bi->bi_db_open = bdb_db_open;
550 bi->bi_db_close = bdb_db_close;
551 bi->bi_db_destroy = bdb_db_destroy;
553 bi->bi_op_add = bdb_add;
554 bi->bi_op_bind = bdb_bind;
555 bi->bi_op_compare = bdb_compare;
556 bi->bi_op_delete = bdb_delete;
557 bi->bi_op_modify = bdb_modify;
558 bi->bi_op_modrdn = bdb_modrdn;
559 bi->bi_op_search = bdb_search;
561 bi->bi_op_unbind = 0;
563 bi->bi_extended = bdb_extended;
565 bi->bi_chk_referrals = bdb_referrals;
566 bi->bi_operational = bdb_operational;
567 bi->bi_has_subordinates = bdb_hasSubordinates;
568 bi->bi_entry_release_rw = bdb_entry_release;
569 bi->bi_entry_get_rw = bdb_entry_get;
572 * hooks for slap tools
574 bi->bi_tool_entry_open = bdb_tool_entry_open;
575 bi->bi_tool_entry_close = bdb_tool_entry_close;
576 bi->bi_tool_entry_first = bdb_tool_entry_next;
577 bi->bi_tool_entry_next = bdb_tool_entry_next;
578 bi->bi_tool_entry_get = bdb_tool_entry_get;
579 bi->bi_tool_entry_put = bdb_tool_entry_put;
580 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
581 bi->bi_tool_sync = 0;
582 bi->bi_tool_dn2id_get = bdb_tool_dn2id_get;
583 bi->bi_tool_id2entry_get = bdb_tool_id2entry_get;
584 bi->bi_tool_entry_modify = bdb_tool_entry_modify;
586 bi->bi_connection_init = 0;
587 bi->bi_connection_destroy = 0;
592 #if (SLAPD_BDB == SLAPD_MOD_DYNAMIC && !defined(BDB_HIER)) || \
593 (SLAPD_HDB == SLAPD_MOD_DYNAMIC && defined(BDB_HIER))
595 /* conditionally define the init_module() function */
597 SLAP_BACKEND_INIT_MODULE( hdb )
598 #else /* !BDB_HIER */
599 SLAP_BACKEND_INIT_MODULE( bdb )
600 #endif /* !BDB_HIER */
602 #endif /* SLAPD_[BH]DB == SLAPD_MOD_DYNAMIC */