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);
46 bdb_open( BackendInfo *bi )
52 bdb_destroy( BackendInfo *bi )
58 bdb_close( BackendInfo *bi )
60 /* terminate the underlying database system */
66 bdb_db_init( BackendDB *be )
70 Debug( LDAP_DEBUG_ANY,
71 LDAP_XSTRING(bdb_db_init) ": Initializing "
72 BDB_UCTYPE " database\n", 0, 0, 0 );
74 /* allocate backend-database-specific stuff */
75 bdb = (struct bdb_info *) ch_calloc( 1, sizeof(struct bdb_info) );
77 /* DBEnv parameters */
78 bdb->bi_dbenv_home = ch_strdup( SLAPD_DEFAULT_DB_DIR );
79 bdb->bi_dbenv_xflags = 0;
80 bdb->bi_dbenv_mode = SLAPD_DEFAULT_DB_MODE;
82 bdb->bi_cache.c_maxsize = DEFAULT_CACHE_SIZE;
84 bdb->bi_lock_detect = DB_LOCK_DEFAULT;
85 bdb->bi_search_stack_depth = DEFAULT_SEARCH_STACK_DEPTH;
86 bdb->bi_search_stack = NULL;
88 ldap_pvt_thread_mutex_init( &bdb->bi_database_mutex );
89 ldap_pvt_thread_mutex_init( &bdb->bi_lastid_mutex );
90 ldap_pvt_thread_mutex_init( &bdb->bi_cache.lru_mutex );
91 ldap_pvt_thread_mutex_init( &bdb->bi_cache.c_dntree.bei_kids_mutex );
92 ldap_pvt_thread_rdwr_init ( &bdb->bi_cache.c_rwlock );
100 bdb_checkpoint( void *ctx, void *arg )
102 struct re_s *rtask = arg;
103 struct bdb_info *bdb = rtask->arg;
105 TXN_CHECKPOINT( bdb->bi_dbenv, bdb->bi_txn_cp_kbyte,
106 bdb->bi_txn_cp_min, 0 );
111 bdb_db_open( BackendDB *be )
114 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
117 char path[MAXPATHLEN];
120 Debug( LDAP_DEBUG_ARGS,
122 be->be_suffix[0].bv_val, 0, 0 );
124 #ifndef BDB_MULTIPLE_SUFFIXES
125 if ( be->be_suffix[1].bv_val ) {
126 Debug( LDAP_DEBUG_ANY,
127 "bdb_db_open: only one suffix allowed\n", 0, 0, 0 );
131 /* we should check existance of dbenv_home and db_directory */
133 rc = db_env_create( &bdb->bi_dbenv, 0 );
135 Debug( LDAP_DEBUG_ANY,
136 "bdb_db_open: db_env_create failed: %s (%d)\n",
137 db_strerror(rc), rc, 0 );
141 flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE
142 | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN;
145 /* Never do automatic recovery, must perform it manually.
146 * Otherwise restarting with gentlehup will corrupt the
149 if( !(slapMode & SLAP_TOOL_MODE) ) flags |= DB_RECOVER;
152 /* If a key was set, use shared memory for the BDB environment */
153 if ( bdb->bi_shm_key ) {
154 bdb->bi_dbenv->set_shm_key( bdb->bi_dbenv, bdb->bi_shm_key );
155 flags |= DB_SYSTEM_MEM;
158 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
159 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
160 bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
162 /* One long-lived TXN per thread, two TXNs per write op */
163 bdb->bi_dbenv->set_tx_max( bdb->bi_dbenv, connection_pool_max * 3 );
165 #ifdef SLAP_ZONE_ALLOC
166 if ( bdb->bi_cache.c_maxsize ) {
167 bdb->bi_cache.c_zctx = slap_zn_mem_create(
175 if ( bdb->bi_idl_cache_max_size ) {
176 bdb->bi_idl_tree = NULL;
177 ldap_pvt_thread_rdwr_init( &bdb->bi_idl_tree_rwlock );
178 ldap_pvt_thread_mutex_init( &bdb->bi_idl_tree_lrulock );
179 bdb->bi_idl_cache_size = 0;
184 char dir[MAXPATHLEN], *ptr;
186 if (bdb->bi_dbenv_home[0] == '.') {
187 /* If home is a relative path, relative subdirs
188 * are just concat'd by BDB. We don't want the
189 * path to be concat'd twice, e.g.
190 * ./test-db/./test-db/tmp
194 ptr = lutil_strcopy( dir, bdb->bi_dbenv_home );
195 *ptr++ = LDAP_DIRSEP[0];
201 strcpy( ptr, BDB_TMP_SUBDIR );
205 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
207 Debug( LDAP_DEBUG_ANY,
208 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
209 dir, db_strerror(rc), rc );
213 strcpy( ptr, BDB_LG_SUBDIR );
217 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
219 Debug( LDAP_DEBUG_ANY,
220 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
221 dir, db_strerror(rc), rc );
225 strcpy( ptr, BDB_DATA_SUBDIR );
229 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
231 Debug( LDAP_DEBUG_ANY,
232 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
233 dir, db_strerror(rc), rc );
239 if( bdb->bi_dbenv_xflags != 0 ) {
240 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
241 bdb->bi_dbenv_xflags, 1);
243 Debug( LDAP_DEBUG_ANY,
244 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
245 db_strerror(rc), rc, 0 );
250 Debug( LDAP_DEBUG_TRACE,
251 "bdb_db_open: dbenv_open(%s)\n",
252 bdb->bi_dbenv_home, 0, 0);
255 strcpy( path, bdb->bi_dbenv_home );
257 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
260 bdb->bi_dbenv_mode );
262 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
265 bdb->bi_dbenv_mode );
268 Debug( LDAP_DEBUG_ANY,
269 "bdb_db_open: dbenv_open failed: %s (%d)\n",
270 db_strerror(rc), rc, 0 );
274 flags = DB_THREAD | bdb->bi_db_opflags;
276 bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
277 BDB_INDICES * sizeof(struct bdb_db_info *) );
279 /* open (and create) main database */
280 for( i = 0; bdbi_databases[i].name; i++ ) {
281 struct bdb_db_info *db;
283 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
285 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
287 Debug( LDAP_DEBUG_ANY,
288 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
289 bdb->bi_dbenv_home, db_strerror(rc), rc );
293 if( i == BDB_ID2ENTRY ) {
294 rc = db->bdi_db->set_pagesize( db->bdi_db,
295 BDB_ID2ENTRY_PAGESIZE );
296 if ( slapMode & SLAP_TOOL_READMAIN ) {
302 rc = db->bdi_db->set_flags( db->bdi_db,
303 DB_DUP | DB_DUPSORT );
305 if ( slapMode & SLAP_TOOL_READONLY ) {
311 if ( slapMode & (SLAP_TOOL_READONLY|SLAP_TOOL_READMAIN) ) {
317 rc = db->bdi_db->set_pagesize( db->bdi_db,
322 strcpy( path, bdbi_databases[i].file );
324 rc = DB_OPEN( db->bdi_db,
326 /* bdbi_databases[i].name, */ NULL,
327 bdbi_databases[i].type,
328 bdbi_databases[i].flags | flags,
329 bdb->bi_dbenv_mode );
331 rc = DB_OPEN( db->bdi_db,
332 bdbi_databases[i].file,
333 /* bdbi_databases[i].name, */ NULL,
334 bdbi_databases[i].type,
335 bdbi_databases[i].flags | flags,
336 bdb->bi_dbenv_mode );
340 char buf[SLAP_TEXT_BUFLEN];
342 snprintf( buf, sizeof(buf), "%s/%s",
343 bdb->bi_dbenv_home, bdbi_databases[i].file );
344 Debug( LDAP_DEBUG_ANY,
345 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
346 buf, db_strerror(rc), rc );
350 flags &= ~(DB_CREATE | DB_RDONLY);
351 db->bdi_name = bdbi_databases[i].name;
352 bdb->bi_databases[i] = db;
355 bdb->bi_databases[i] = NULL;
356 bdb->bi_ndatabases = i;
359 rc = bdb_last_id( be, NULL );
361 Debug( LDAP_DEBUG_ANY,
362 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
363 bdb->bi_dbenv_home, db_strerror(rc), rc );
367 XLOCK_ID(bdb->bi_dbenv, &bdb->bi_cache.c_locker);
369 /* If we're in server mode and time-based checkpointing is enabled,
370 * submit a task to perform periodic checkpoints.
372 if ( slapMode & SLAP_SERVER_MODE && bdb->bi_txn_cp &&
373 bdb->bi_txn_cp_min ) {
374 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
375 ldap_pvt_runqueue_insert( &slapd_rq, bdb->bi_txn_cp_min*60,
376 bdb_checkpoint, bdb );
377 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
380 /* <insert> open (and create) index databases */
385 bdb_db_close( BackendDB *be )
388 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
389 struct bdb_db_info *db;
390 bdb_idl_cache_entry_t *entry, *next_entry;
392 while( bdb->bi_ndatabases-- ) {
393 db = bdb->bi_databases[bdb->bi_ndatabases];
394 rc = db->bdi_db->close( db->bdi_db, 0 );
395 /* Lower numbered names are not strdup'd */
396 if( bdb->bi_ndatabases >= BDB_NDB )
397 free( db->bdi_name );
400 free( bdb->bi_databases );
401 bdb_attr_index_destroy( bdb->bi_attrs );
403 bdb_cache_release_all (&bdb->bi_cache);
405 if ( bdb->bi_idl_cache_max_size ) {
406 ldap_pvt_thread_rdwr_wlock ( &bdb->bi_idl_tree_rwlock );
407 avl_free( bdb->bi_idl_tree, NULL );
408 entry = bdb->bi_idl_lru_head;
409 while ( entry != NULL ) {
410 next_entry = entry->idl_lru_next;
413 free( entry->kstr.bv_val );
417 ldap_pvt_thread_rdwr_wunlock ( &bdb->bi_idl_tree_rwlock );
420 XLOCK_ID_FREE(bdb->bi_dbenv, bdb->bi_cache.c_locker);
426 bdb_db_destroy( BackendDB *be )
429 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
430 Operation *ps = NULL;
431 Operation *psn = NULL;
432 void *saved_tmpmemctx = NULL;
434 /* close db environment */
435 if( bdb->bi_dbenv ) {
436 /* force a checkpoint */
437 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
439 Debug( LDAP_DEBUG_ANY,
440 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
441 db_strerror(rc), rc, 0 );
444 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
445 bdb->bi_dbenv = NULL;
447 Debug( LDAP_DEBUG_ANY,
448 "bdb_db_destroy: close failed: %s (%d)\n",
449 db_strerror(rc), rc, 0 );
454 if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
456 ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
457 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_mutex );
458 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_dntree.bei_kids_mutex );
459 ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
460 ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
461 if ( bdb->bi_idl_cache_max_size ) {
462 ldap_pvt_thread_rdwr_destroy( &bdb->bi_idl_tree_rwlock );
463 ldap_pvt_thread_mutex_destroy( &bdb->bi_idl_tree_lrulock );
467 be->be_private = NULL;
476 static char *controls[] = {
478 LDAP_CONTROL_MANAGEDSAIT,
480 LDAP_CONTROL_PAGEDRESULTS,
481 #ifdef LDAP_CONTROL_SUBENTRIES
482 LDAP_CONTROL_SUBENTRIES,
484 LDAP_CONTROL_VALUESRETURNFILTER,
485 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
486 LDAP_CONTROL_X_PERMISSIVE_MODIFY,
491 /* initialize the underlying database system */
492 Debug( LDAP_DEBUG_TRACE,
493 LDAP_XSTRING(bdb_back_initialize) ": initialize "
494 BDB_UCTYPE " backend\n", 0, 0, 0 );
497 SLAP_BFLAG_INCREMENT |
498 #ifdef BDB_SUBENTRIES
499 SLAP_BFLAG_SUBENTRIES |
502 SLAP_BFLAG_REFERRALS;
504 bi->bi_controls = controls;
506 { /* version check */
507 int major, minor, patch, ver;
508 char *version = db_version( &major, &minor, &patch );
512 /* All our stdio does an ASCII to EBCDIC conversion on
513 * the output. Strings from the BDB library are already
514 * in EBCDIC; we have to go back and forth...
516 strcpy( v2, version );
521 ver = (major << 24) | (minor << 16) | patch;
522 if( ver < DB_VERSION_FULL )
524 Debug( LDAP_DEBUG_ANY,
525 LDAP_XSTRING(bdb_back_initialize) ": "
526 "BDB library version mismatch:"
527 " expected " DB_VERSION_STRING ","
528 " got %s\n", version, 0, 0 );
531 Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(bdb_back_initialize)
532 ": %s\n", version, 0, 0 );
535 db_env_set_func_free( ber_memfree );
536 db_env_set_func_malloc( (db_malloc *)ber_memalloc );
537 db_env_set_func_realloc( (db_realloc *)ber_memrealloc );
539 /* This is a no-op on a NO_THREAD build. Leave the default
540 * alone so that BDB will sleep on interprocess conflicts.
542 db_env_set_func_yield( ldap_pvt_thread_yield );
546 static char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
548 bdb_uuid.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ));
549 bdb_uuid.bv_val = uuidbuf;
557 bi->bi_db_init = bdb_db_init;
558 bi->bi_db_config = bdb_db_config;
559 bi->bi_db_open = bdb_db_open;
560 bi->bi_db_close = bdb_db_close;
561 bi->bi_db_destroy = bdb_db_destroy;
563 bi->bi_op_add = bdb_add;
564 bi->bi_op_bind = bdb_bind;
565 bi->bi_op_compare = bdb_compare;
566 bi->bi_op_delete = bdb_delete;
567 bi->bi_op_modify = bdb_modify;
568 bi->bi_op_modrdn = bdb_modrdn;
569 bi->bi_op_search = bdb_search;
571 bi->bi_op_unbind = 0;
573 #if 0 /* DELETE ME */
574 bi->bi_op_abandon = bdb_abandon;
575 bi->bi_op_cancel = bdb_cancel;
578 bi->bi_extended = bdb_extended;
580 bi->bi_chk_referrals = bdb_referrals;
581 bi->bi_operational = bdb_operational;
582 bi->bi_has_subordinates = bdb_hasSubordinates;
583 bi->bi_entry_release_rw = bdb_entry_release;
584 bi->bi_entry_get_rw = bdb_entry_get;
587 * hooks for slap tools
589 bi->bi_tool_entry_open = bdb_tool_entry_open;
590 bi->bi_tool_entry_close = bdb_tool_entry_close;
591 bi->bi_tool_entry_first = bdb_tool_entry_next;
592 bi->bi_tool_entry_next = bdb_tool_entry_next;
593 bi->bi_tool_entry_get = bdb_tool_entry_get;
594 bi->bi_tool_entry_put = bdb_tool_entry_put;
595 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
596 bi->bi_tool_sync = 0;
597 bi->bi_tool_dn2id_get = bdb_tool_dn2id_get;
598 bi->bi_tool_id2entry_get = bdb_tool_id2entry_get;
599 bi->bi_tool_entry_modify = bdb_tool_entry_modify;
601 bi->bi_connection_init = 0;
602 bi->bi_connection_destroy = 0;
607 #if (SLAPD_BDB == SLAPD_MOD_DYNAMIC && !defined(BDB_HIER)) || \
608 (SLAPD_HDB == SLAPD_MOD_DYNAMIC && defined(BDB_HIER))
610 /* conditionally define the init_module() function */
612 SLAP_BACKEND_INIT_MODULE( hdb )
613 #else /* !BDB_HIER */
614 SLAP_BACKEND_INIT_MODULE( bdb )
615 #endif /* !BDB_HIER */
617 #endif /* SLAPD_[BH]DB == SLAPD_MOD_DYNAMIC */