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 typedef void * db_malloc(size_t);
40 typedef void * db_realloc(void *, size_t);
43 bdb_db_init( BackendDB *be )
47 Debug( LDAP_DEBUG_TRACE,
48 LDAP_XSTRING(bdb_db_init) ": Initializing " BDB_UCTYPE " database\n",
51 /* allocate backend-database-specific stuff */
52 bdb = (struct bdb_info *) ch_calloc( 1, sizeof(struct bdb_info) );
54 /* DBEnv parameters */
55 bdb->bi_dbenv_home = ch_strdup( SLAPD_DEFAULT_DB_DIR );
56 bdb->bi_dbenv_xflags = 0;
57 bdb->bi_dbenv_mode = SLAPD_DEFAULT_DB_MODE;
59 bdb->bi_cache.c_maxsize = DEFAULT_CACHE_SIZE;
61 bdb->bi_lock_detect = DB_LOCK_DEFAULT;
62 bdb->bi_search_stack_depth = DEFAULT_SEARCH_STACK_DEPTH;
63 bdb->bi_search_stack = NULL;
65 ldap_pvt_thread_mutex_init( &bdb->bi_database_mutex );
66 ldap_pvt_thread_mutex_init( &bdb->bi_lastid_mutex );
67 ldap_pvt_thread_mutex_init( &bdb->bi_cache.lru_mutex );
68 ldap_pvt_thread_mutex_init( &bdb->bi_cache.c_dntree.bei_kids_mutex );
69 ldap_pvt_thread_rdwr_init ( &bdb->bi_cache.c_rwlock );
72 be->be_cf_table = be->bd_info->bi_cf_table;
78 bdb_checkpoint( void *ctx, void *arg )
80 struct re_s *rtask = arg;
81 struct bdb_info *bdb = rtask->arg;
83 TXN_CHECKPOINT( bdb->bi_dbenv, bdb->bi_txn_cp_kbyte,
84 bdb->bi_txn_cp_min, 0 );
89 bdb_db_open( BackendDB *be )
92 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
95 char path[MAXPATHLEN];
98 Debug( LDAP_DEBUG_ARGS,
100 be->be_suffix[0].bv_val, 0, 0 );
102 #ifndef BDB_MULTIPLE_SUFFIXES
103 if ( be->be_suffix[1].bv_val ) {
104 Debug( LDAP_DEBUG_ANY,
105 "bdb_db_open: only one suffix allowed\n", 0, 0, 0 );
109 /* we should check existance of dbenv_home and db_directory */
111 rc = db_env_create( &bdb->bi_dbenv, 0 );
113 Debug( LDAP_DEBUG_ANY,
114 "bdb_db_open: db_env_create failed: %s (%d)\n",
115 db_strerror(rc), rc, 0 );
119 flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE;
121 if ( !( slapMode & SLAP_TOOL_QUICK ))
122 flags |= DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN;
125 /* Never do automatic recovery, must perform it manually.
126 * Otherwise restarting with gentlehup will corrupt the
129 if( !(slapMode & SLAP_TOOL_MODE) ) flags |= DB_RECOVER;
132 /* If a key was set, use shared memory for the BDB environment */
133 if ( bdb->bi_shm_key ) {
134 bdb->bi_dbenv->set_shm_key( bdb->bi_dbenv, bdb->bi_shm_key );
135 flags |= DB_SYSTEM_MEM;
138 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
139 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
140 bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
142 /* One long-lived TXN per thread, two TXNs per write op */
143 bdb->bi_dbenv->set_tx_max( bdb->bi_dbenv, connection_pool_max * 3 );
145 #ifdef SLAP_ZONE_ALLOC
146 if ( bdb->bi_cache.c_maxsize ) {
147 bdb->bi_cache.c_zctx = slap_zn_mem_create(
155 if ( bdb->bi_idl_cache_max_size ) {
156 bdb->bi_idl_tree = NULL;
157 ldap_pvt_thread_rdwr_init( &bdb->bi_idl_tree_rwlock );
158 ldap_pvt_thread_mutex_init( &bdb->bi_idl_tree_lrulock );
159 bdb->bi_idl_cache_size = 0;
164 char dir[MAXPATHLEN], *ptr;
166 if (bdb->bi_dbenv_home[0] == '.') {
167 /* If home is a relative path, relative subdirs
168 * are just concat'd by BDB. We don't want the
169 * path to be concat'd twice, e.g.
170 * ./test-db/./test-db/tmp
174 ptr = lutil_strcopy( dir, bdb->bi_dbenv_home );
175 *ptr++ = LDAP_DIRSEP[0];
181 strcpy( ptr, BDB_TMP_SUBDIR );
185 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
187 Debug( LDAP_DEBUG_ANY,
188 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
189 dir, db_strerror(rc), rc );
193 strcpy( ptr, BDB_LG_SUBDIR );
197 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
199 Debug( LDAP_DEBUG_ANY,
200 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
201 dir, db_strerror(rc), rc );
205 strcpy( ptr, BDB_DATA_SUBDIR );
209 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
211 Debug( LDAP_DEBUG_ANY,
212 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
213 dir, db_strerror(rc), rc );
219 if( bdb->bi_dbenv_xflags != 0 ) {
220 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
221 bdb->bi_dbenv_xflags, 1);
223 Debug( LDAP_DEBUG_ANY,
224 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
225 db_strerror(rc), rc, 0 );
230 Debug( LDAP_DEBUG_TRACE,
231 "bdb_db_open: dbenv_open(%s)\n",
232 bdb->bi_dbenv_home, 0, 0);
235 strcpy( path, bdb->bi_dbenv_home );
237 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
240 bdb->bi_dbenv_mode );
242 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
245 bdb->bi_dbenv_mode );
248 Debug( LDAP_DEBUG_ANY,
249 "bdb_db_open: dbenv_open failed: %s (%d)\n",
250 db_strerror(rc), rc, 0 );
254 flags = DB_THREAD | bdb->bi_db_opflags;
256 #ifdef DB_AUTO_COMMIT
257 if ( !( slapMode & SLAP_TOOL_QUICK ))
258 flags |= DB_AUTO_COMMIT;
261 bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
262 BDB_INDICES * sizeof(struct bdb_db_info *) );
264 /* open (and create) main database */
265 for( i = 0; bdbi_databases[i].name; i++ ) {
266 struct bdb_db_info *db;
268 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
270 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
272 Debug( LDAP_DEBUG_ANY,
273 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
274 bdb->bi_dbenv_home, db_strerror(rc), rc );
278 if( i == BDB_ID2ENTRY ) {
279 rc = db->bdi_db->set_pagesize( db->bdi_db,
280 BDB_ID2ENTRY_PAGESIZE );
281 if ( slapMode & SLAP_TOOL_READMAIN ) {
287 rc = db->bdi_db->set_flags( db->bdi_db,
288 DB_DUP | DB_DUPSORT );
290 if ( slapMode & SLAP_TOOL_READONLY ) {
296 if ( slapMode & (SLAP_TOOL_READONLY|SLAP_TOOL_READMAIN) ) {
302 rc = db->bdi_db->set_pagesize( db->bdi_db,
307 strcpy( path, bdbi_databases[i].file );
309 rc = DB_OPEN( db->bdi_db,
311 /* bdbi_databases[i].name, */ NULL,
312 bdbi_databases[i].type,
313 bdbi_databases[i].flags | flags,
314 bdb->bi_dbenv_mode );
316 rc = DB_OPEN( db->bdi_db,
317 bdbi_databases[i].file,
318 /* bdbi_databases[i].name, */ NULL,
319 bdbi_databases[i].type,
320 bdbi_databases[i].flags | flags,
321 bdb->bi_dbenv_mode );
325 char buf[SLAP_TEXT_BUFLEN];
327 snprintf( buf, sizeof(buf), "%s/%s",
328 bdb->bi_dbenv_home, bdbi_databases[i].file );
329 Debug( LDAP_DEBUG_ANY,
330 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
331 buf, db_strerror(rc), rc );
335 flags &= ~(DB_CREATE | DB_RDONLY);
336 db->bdi_name = bdbi_databases[i].name;
337 bdb->bi_databases[i] = db;
340 bdb->bi_databases[i] = NULL;
341 bdb->bi_ndatabases = i;
344 rc = bdb_last_id( be, NULL );
346 Debug( LDAP_DEBUG_ANY,
347 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
348 bdb->bi_dbenv_home, db_strerror(rc), rc );
352 if ( !( slapMode & SLAP_TOOL_QUICK )) {
353 XLOCK_ID(bdb->bi_dbenv, &bdb->bi_cache.c_locker);
356 /* If we're in server mode and time-based checkpointing is enabled,
357 * submit a task to perform periodic checkpoints.
359 if ( slapMode & SLAP_SERVER_MODE && bdb->bi_txn_cp &&
360 bdb->bi_txn_cp_min ) {
361 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
362 ldap_pvt_runqueue_insert( &slapd_rq, bdb->bi_txn_cp_min*60,
363 bdb_checkpoint, bdb );
364 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
371 bdb_db_close( BackendDB *be )
374 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
375 struct bdb_db_info *db;
376 bdb_idl_cache_entry_t *entry, *next_entry;
378 while( bdb->bi_ndatabases-- ) {
379 db = bdb->bi_databases[bdb->bi_ndatabases];
380 rc = db->bdi_db->close( db->bdi_db, 0 );
381 /* Lower numbered names are not strdup'd */
382 if( bdb->bi_ndatabases >= BDB_NDB )
383 free( db->bdi_name );
386 free( bdb->bi_databases );
387 bdb_attr_index_destroy( bdb->bi_attrs );
389 bdb_cache_release_all (&bdb->bi_cache);
391 if ( bdb->bi_idl_cache_max_size ) {
392 ldap_pvt_thread_rdwr_wlock ( &bdb->bi_idl_tree_rwlock );
393 avl_free( bdb->bi_idl_tree, NULL );
394 entry = bdb->bi_idl_lru_head;
395 while ( entry != NULL ) {
396 next_entry = entry->idl_lru_next;
399 free( entry->kstr.bv_val );
403 ldap_pvt_thread_rdwr_wunlock ( &bdb->bi_idl_tree_rwlock );
406 if ( !( slapMode & SLAP_TOOL_QUICK )) {
407 XLOCK_ID_FREE(bdb->bi_dbenv, bdb->bi_cache.c_locker);
414 bdb_db_destroy( BackendDB *be )
417 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
419 /* close db environment */
420 if( bdb->bi_dbenv ) {
421 /* force a checkpoint */
422 if ( !( slapMode & SLAP_TOOL_QUICK )) {
423 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
425 Debug( LDAP_DEBUG_ANY,
426 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
427 db_strerror(rc), rc, 0 );
431 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
432 bdb->bi_dbenv = NULL;
434 Debug( LDAP_DEBUG_ANY,
435 "bdb_db_destroy: close failed: %s (%d)\n",
436 db_strerror(rc), rc, 0 );
441 if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
443 ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
444 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_mutex );
445 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_dntree.bei_kids_mutex );
446 ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
447 ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
448 if ( bdb->bi_idl_cache_max_size ) {
449 ldap_pvt_thread_rdwr_destroy( &bdb->bi_idl_tree_rwlock );
450 ldap_pvt_thread_mutex_destroy( &bdb->bi_idl_tree_lrulock );
454 be->be_private = NULL;
465 static char *controls[] = {
467 LDAP_CONTROL_MANAGEDSAIT,
469 LDAP_CONTROL_PAGEDRESULTS,
470 #ifdef LDAP_CONTROL_SUBENTRIES
471 LDAP_CONTROL_SUBENTRIES,
473 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
474 LDAP_CONTROL_X_PERMISSIVE_MODIFY,
479 /* initialize the underlying database system */
480 Debug( LDAP_DEBUG_TRACE,
481 LDAP_XSTRING(bdb_back_initialize) ": initialize "
482 BDB_UCTYPE " backend\n", 0, 0, 0 );
485 SLAP_BFLAG_INCREMENT |
486 #ifdef BDB_SUBENTRIES
487 SLAP_BFLAG_SUBENTRIES |
490 SLAP_BFLAG_REFERRALS;
492 bi->bi_controls = controls;
494 { /* version check */
495 int major, minor, patch, ver;
496 char *version = db_version( &major, &minor, &patch );
500 /* All our stdio does an ASCII to EBCDIC conversion on
501 * the output. Strings from the BDB library are already
502 * in EBCDIC; we have to go back and forth...
504 strcpy( v2, version );
509 ver = (major << 24) | (minor << 16) | patch;
510 if( ver < DB_VERSION_FULL ) {
511 Debug( LDAP_DEBUG_TRACE,
512 LDAP_XSTRING(bdb_back_initialize) ": "
513 "BDB library version mismatch:"
514 " expected " DB_VERSION_STRING ","
515 " got %s\n", version, 0, 0 );
518 Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(bdb_back_initialize)
519 ": %s\n", version, 0, 0 );
522 db_env_set_func_free( ber_memfree );
523 db_env_set_func_malloc( (db_malloc *)ber_memalloc );
524 db_env_set_func_realloc( (db_realloc *)ber_memrealloc );
526 /* This is a no-op on a NO_THREAD build. Leave the default
527 * alone so that BDB will sleep on interprocess conflicts.
529 db_env_set_func_yield( ldap_pvt_thread_yield );
537 bi->bi_db_init = bdb_db_init;
538 bi->bi_db_config = config_generic_wrapper;
539 bi->bi_db_open = bdb_db_open;
540 bi->bi_db_close = bdb_db_close;
541 bi->bi_db_destroy = bdb_db_destroy;
543 bi->bi_op_add = bdb_add;
544 bi->bi_op_bind = bdb_bind;
545 bi->bi_op_compare = bdb_compare;
546 bi->bi_op_delete = bdb_delete;
547 bi->bi_op_modify = bdb_modify;
548 bi->bi_op_modrdn = bdb_modrdn;
549 bi->bi_op_search = bdb_search;
551 bi->bi_op_unbind = 0;
553 bi->bi_extended = bdb_extended;
555 bi->bi_chk_referrals = bdb_referrals;
556 bi->bi_operational = bdb_operational;
557 bi->bi_has_subordinates = bdb_hasSubordinates;
558 bi->bi_entry_release_rw = bdb_entry_release;
559 bi->bi_entry_get_rw = bdb_entry_get;
562 * hooks for slap tools
564 bi->bi_tool_entry_open = bdb_tool_entry_open;
565 bi->bi_tool_entry_close = bdb_tool_entry_close;
566 bi->bi_tool_entry_first = bdb_tool_entry_next;
567 bi->bi_tool_entry_next = bdb_tool_entry_next;
568 bi->bi_tool_entry_get = bdb_tool_entry_get;
569 bi->bi_tool_entry_put = bdb_tool_entry_put;
570 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
571 bi->bi_tool_sync = 0;
572 bi->bi_tool_dn2id_get = bdb_tool_dn2id_get;
573 bi->bi_tool_id2entry_get = bdb_tool_id2entry_get;
574 bi->bi_tool_entry_modify = bdb_tool_entry_modify;
576 bi->bi_connection_init = 0;
577 bi->bi_connection_destroy = 0;
579 rc = bdb_back_init_cf(bi);
584 #if (SLAPD_BDB == SLAPD_MOD_DYNAMIC && !defined(BDB_HIER)) || \
585 (SLAPD_HDB == SLAPD_MOD_DYNAMIC && defined(BDB_HIER))
587 /* conditionally define the init_module() function */
589 SLAP_BACKEND_INIT_MODULE( hdb )
590 #else /* !BDB_HIER */
591 SLAP_BACKEND_INIT_MODULE( bdb )
592 #endif /* !BDB_HIER */
594 #endif /* SLAPD_[BH]DB == SLAPD_MOD_DYNAMIC */