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;
174 if( bdb->bi_dbenv_xflags != 0 ) {
175 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
176 bdb->bi_dbenv_xflags, 1);
178 Debug( LDAP_DEBUG_ANY,
179 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
180 db_strerror(rc), rc, 0 );
185 Debug( LDAP_DEBUG_TRACE,
186 "bdb_db_open: dbenv_open(%s)\n",
187 bdb->bi_dbenv_home, 0, 0);
190 strcpy( path, bdb->bi_dbenv_home );
192 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
195 bdb->bi_dbenv_mode );
197 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
200 bdb->bi_dbenv_mode );
203 Debug( LDAP_DEBUG_ANY,
204 "bdb_db_open: dbenv_open failed: %s (%d)\n",
205 db_strerror(rc), rc, 0 );
208 if( flags & DB_RECOVER ) {
209 rc = alock_recover (&bdb->bi_alock_info);
211 Debug( LDAP_DEBUG_ANY,
212 "bdb_db_open: unable to alock_recover\n", 0, 0, 0 );
217 flags = DB_THREAD | bdb->bi_db_opflags;
219 #ifdef DB_AUTO_COMMIT
220 if ( !( slapMode & SLAP_TOOL_QUICK ))
221 flags |= DB_AUTO_COMMIT;
224 bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
225 BDB_INDICES * sizeof(struct bdb_db_info *) );
227 /* open (and create) main database */
228 for( i = 0; bdbi_databases[i].name; i++ ) {
229 struct bdb_db_info *db;
231 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
233 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
235 Debug( LDAP_DEBUG_ANY,
236 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
237 bdb->bi_dbenv_home, db_strerror(rc), rc );
241 if( i == BDB_ID2ENTRY ) {
242 rc = db->bdi_db->set_pagesize( db->bdi_db,
243 BDB_ID2ENTRY_PAGESIZE );
244 if ( slapMode & SLAP_TOOL_READMAIN ) {
250 rc = db->bdi_db->set_flags( db->bdi_db,
251 DB_DUP | DB_DUPSORT );
253 if ( slapMode & SLAP_TOOL_READONLY ) {
259 if ( slapMode & (SLAP_TOOL_READONLY|SLAP_TOOL_READMAIN) ) {
265 rc = db->bdi_db->set_pagesize( db->bdi_db,
270 strcpy( path, bdbi_databases[i].file );
272 rc = DB_OPEN( db->bdi_db,
274 /* bdbi_databases[i].name, */ NULL,
275 bdbi_databases[i].type,
276 bdbi_databases[i].flags | flags,
277 bdb->bi_dbenv_mode );
279 rc = DB_OPEN( db->bdi_db,
280 bdbi_databases[i].file,
281 /* bdbi_databases[i].name, */ NULL,
282 bdbi_databases[i].type,
283 bdbi_databases[i].flags | flags,
284 bdb->bi_dbenv_mode );
288 char buf[SLAP_TEXT_BUFLEN];
290 snprintf( buf, sizeof(buf), "%s/%s",
291 bdb->bi_dbenv_home, bdbi_databases[i].file );
292 Debug( LDAP_DEBUG_ANY,
293 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
294 buf, db_strerror(rc), rc );
298 flags &= ~(DB_CREATE | DB_RDONLY);
299 db->bdi_name = bdbi_databases[i].name;
300 bdb->bi_databases[i] = db;
303 bdb->bi_databases[i] = NULL;
304 bdb->bi_ndatabases = i;
307 rc = bdb_last_id( be, NULL );
309 Debug( LDAP_DEBUG_ANY,
310 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
311 bdb->bi_dbenv_home, db_strerror(rc), rc );
315 if ( !( slapMode & SLAP_TOOL_QUICK )) {
316 XLOCK_ID(bdb->bi_dbenv, &bdb->bi_cache.c_locker);
319 /* If we're in server mode and time-based checkpointing is enabled,
320 * submit a task to perform periodic checkpoints.
322 if (( slapMode & SLAP_SERVER_MODE ) && bdb->bi_txn_cp &&
323 bdb->bi_txn_cp_min ) {
324 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
325 ldap_pvt_runqueue_insert( &slapd_rq, bdb->bi_txn_cp_min*60,
326 bdb_checkpoint, bdb );
327 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
330 if ( slapMode & SLAP_SERVER_MODE && bdb->bi_db_has_config ) {
331 char buf[SLAP_TEXT_BUFLEN];
332 FILE *f = fopen( bdb->bi_db_config_path, "r" );
336 while ( fgets( buf, sizeof(buf), f )) {
337 ber_str2bv( buf, 0, 1, &bv );
338 if ( bv.bv_val[bv.bv_len-1] == '\n' ) {
340 bv.bv_val[bv.bv_len] = '\0';
342 /* shouldn't need this, but ... */
343 if ( bv.bv_val[bv.bv_len-1] == '\r' ) {
345 bv.bv_val[bv.bv_len] = '\0';
347 ber_bvarray_add( &bdb->bi_db_config, &bv );
351 /* Eh? It disappeared between config and open?? */
352 bdb->bi_db_has_config = 0;
356 bdb->bi_db_is_open = 1;
362 bdb_db_close( BackendDB *be )
365 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
366 struct bdb_db_info *db;
367 bdb_idl_cache_entry_t *entry, *next_entry;
369 bdb->bi_db_is_open = 0;
371 ber_bvarray_free( bdb->bi_db_config );
373 while( bdb->bi_ndatabases-- ) {
374 db = bdb->bi_databases[bdb->bi_ndatabases];
375 rc = db->bdi_db->close( db->bdi_db, 0 );
376 /* Lower numbered names are not strdup'd */
377 if( bdb->bi_ndatabases >= BDB_NDB )
378 free( db->bdi_name );
381 free( bdb->bi_databases );
382 bdb_attr_index_destroy( bdb->bi_attrs );
384 bdb_cache_release_all (&bdb->bi_cache);
386 if ( bdb->bi_idl_cache_max_size ) {
387 ldap_pvt_thread_rdwr_wlock ( &bdb->bi_idl_tree_rwlock );
388 avl_free( bdb->bi_idl_tree, NULL );
389 entry = bdb->bi_idl_lru_head;
390 while ( entry != NULL ) {
391 next_entry = entry->idl_lru_next;
394 free( entry->kstr.bv_val );
398 ldap_pvt_thread_rdwr_wunlock ( &bdb->bi_idl_tree_rwlock );
401 if ( !( slapMode & SLAP_TOOL_QUICK )) {
402 XLOCK_ID_FREE(bdb->bi_dbenv, bdb->bi_cache.c_locker);
409 bdb_db_destroy( BackendDB *be )
412 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
414 /* close db environment */
415 if( bdb->bi_dbenv ) {
416 /* force a checkpoint */
417 if ( !( slapMode & SLAP_TOOL_QUICK )) {
418 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
420 Debug( LDAP_DEBUG_ANY,
421 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
422 db_strerror(rc), rc, 0 );
426 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
427 bdb->bi_dbenv = NULL;
429 Debug( LDAP_DEBUG_ANY,
430 "bdb_db_destroy: close failed: %s (%d)\n",
431 db_strerror(rc), rc, 0 );
436 rc = alock_close( &bdb->bi_alock_info );
438 Debug( LDAP_DEBUG_ANY,
439 "bdb_db_destroy: alock_close failed\n", 0, 0, 0 );
443 if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
444 if( bdb->bi_db_config_path ) ch_free( bdb->bi_db_config_path );
446 ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
447 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_mutex );
448 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_dntree.bei_kids_mutex );
449 ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
450 ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
451 if ( bdb->bi_idl_cache_max_size ) {
452 ldap_pvt_thread_rdwr_destroy( &bdb->bi_idl_tree_rwlock );
453 ldap_pvt_thread_mutex_destroy( &bdb->bi_idl_tree_lrulock );
457 be->be_private = NULL;
468 static char *controls[] = {
470 LDAP_CONTROL_MANAGEDSAIT,
472 LDAP_CONTROL_PAGEDRESULTS,
473 #ifdef LDAP_CONTROL_SUBENTRIES
474 LDAP_CONTROL_SUBENTRIES,
476 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
477 LDAP_CONTROL_X_PERMISSIVE_MODIFY,
482 /* initialize the underlying database system */
483 Debug( LDAP_DEBUG_TRACE,
484 LDAP_XSTRING(bdb_back_initialize) ": initialize "
485 BDB_UCTYPE " backend\n", 0, 0, 0 );
488 SLAP_BFLAG_INCREMENT |
489 #ifdef BDB_SUBENTRIES
490 SLAP_BFLAG_SUBENTRIES |
493 SLAP_BFLAG_REFERRALS;
495 bi->bi_controls = controls;
497 { /* version check */
498 int major, minor, patch, ver;
499 char *version = db_version( &major, &minor, &patch );
503 /* All our stdio does an ASCII to EBCDIC conversion on
504 * the output. Strings from the BDB library are already
505 * in EBCDIC; we have to go back and forth...
507 strcpy( v2, version );
512 ver = (major << 24) | (minor << 16) | patch;
513 if( ver != DB_VERSION_FULL ) {
514 /* fail if a versions don't match */
515 Debug( LDAP_DEBUG_ANY,
516 LDAP_XSTRING(bdb_back_initialize) ": "
517 "BDB library version mismatch:"
518 " expected " DB_VERSION_STRING ","
519 " got %s\n", version, 0, 0 );
523 Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(bdb_back_initialize)
524 ": %s\n", version, 0, 0 );
527 db_env_set_func_free( ber_memfree );
528 db_env_set_func_malloc( (db_malloc *)ber_memalloc );
529 db_env_set_func_realloc( (db_realloc *)ber_memrealloc );
531 /* This is a no-op on a NO_THREAD build. Leave the default
532 * alone so that BDB will sleep on interprocess conflicts.
534 db_env_set_func_yield( ldap_pvt_thread_yield );
542 bi->bi_db_init = bdb_db_init;
543 bi->bi_db_config = config_generic_wrapper;
544 bi->bi_db_open = bdb_db_open;
545 bi->bi_db_close = bdb_db_close;
546 bi->bi_db_destroy = bdb_db_destroy;
548 bi->bi_op_add = bdb_add;
549 bi->bi_op_bind = bdb_bind;
550 bi->bi_op_compare = bdb_compare;
551 bi->bi_op_delete = bdb_delete;
552 bi->bi_op_modify = bdb_modify;
553 bi->bi_op_modrdn = bdb_modrdn;
554 bi->bi_op_search = bdb_search;
556 bi->bi_op_unbind = 0;
558 bi->bi_extended = bdb_extended;
560 bi->bi_chk_referrals = bdb_referrals;
561 bi->bi_operational = bdb_operational;
562 bi->bi_has_subordinates = bdb_hasSubordinates;
563 bi->bi_entry_release_rw = bdb_entry_release;
564 bi->bi_entry_get_rw = bdb_entry_get;
567 * hooks for slap tools
569 bi->bi_tool_entry_open = bdb_tool_entry_open;
570 bi->bi_tool_entry_close = bdb_tool_entry_close;
571 bi->bi_tool_entry_first = bdb_tool_entry_next;
572 bi->bi_tool_entry_next = bdb_tool_entry_next;
573 bi->bi_tool_entry_get = bdb_tool_entry_get;
574 bi->bi_tool_entry_put = bdb_tool_entry_put;
575 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
576 bi->bi_tool_sync = 0;
577 bi->bi_tool_dn2id_get = bdb_tool_dn2id_get;
578 bi->bi_tool_id2entry_get = bdb_tool_id2entry_get;
579 bi->bi_tool_entry_modify = bdb_tool_entry_modify;
581 bi->bi_connection_init = 0;
582 bi->bi_connection_destroy = 0;
584 rc = bdb_back_init_cf( bi );
589 #if (SLAPD_BDB == SLAPD_MOD_DYNAMIC && !defined(BDB_HIER)) || \
590 (SLAPD_HDB == SLAPD_MOD_DYNAMIC && defined(BDB_HIER))
592 /* conditionally define the init_module() function */
594 SLAP_BACKEND_INIT_MODULE( hdb )
595 #else /* !BDB_HIER */
596 SLAP_BACKEND_INIT_MODULE( bdb )
597 #endif /* !BDB_HIER */
599 #endif /* SLAPD_[BH]DB == SLAPD_MOD_DYNAMIC */