1 /* init.c - initialize bdb backend */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2004 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 )
71 LDAP_LOG( BACK_BDB, ENTRY, "bdb_db_init", 0, 0, 0 );
73 Debug( LDAP_DEBUG_ANY,
74 "bdb_db_init: Initializing %s database\n",
75 be->bd_info->bi_type, 0, 0 );
78 /* allocate backend-database-specific stuff */
79 bdb = (struct bdb_info *) ch_calloc( 1, sizeof(struct bdb_info) );
81 /* DBEnv parameters */
82 bdb->bi_dbenv_home = ch_strdup( SLAPD_DEFAULT_DB_DIR );
83 bdb->bi_dbenv_xflags = 0;
84 bdb->bi_dbenv_mode = SLAPD_DEFAULT_DB_MODE;
86 bdb->bi_cache.c_maxsize = DEFAULT_CACHE_SIZE;
88 bdb->bi_lock_detect = DB_LOCK_DEFAULT;
89 bdb->bi_search_stack_depth = DEFAULT_SEARCH_STACK_DEPTH;
90 bdb->bi_search_stack = NULL;
92 LDAP_LIST_INIT (&bdb->bi_psearch_list);
94 ldap_pvt_thread_mutex_init( &bdb->bi_database_mutex );
95 ldap_pvt_thread_mutex_init( &bdb->bi_lastid_mutex );
96 ldap_pvt_thread_rdwr_init ( &bdb->bi_pslist_rwlock );
97 ldap_pvt_thread_mutex_init( &bdb->bi_cache.lru_mutex );
98 ldap_pvt_thread_mutex_init( &bdb->bi_cache.c_dntree.bei_kids_mutex );
99 ldap_pvt_thread_rdwr_init ( &bdb->bi_cache.c_rwlock );
101 be->be_private = bdb;
112 unsigned char *u, *c;
118 #ifdef WORDS_BIGENDIAN
119 for( i = 0; i < (int)sizeof(ID); i++)
121 for( i = sizeof(ID)-1; i >= 0; i--)
132 bdb_db_open( BackendDB *be )
135 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
138 char path[MAXPATHLEN];
142 LDAP_LOG( BACK_BDB, ARGS,
143 "bdb_db_open: %s\n", be->be_suffix[0].bv_val, 0, 0 );
145 Debug( LDAP_DEBUG_ARGS,
147 be->be_suffix[0].bv_val, 0, 0 );
150 #ifndef BDB_MULTIPLE_SUFFIXES
151 if ( be->be_suffix[1].bv_val ) {
153 LDAP_LOG( BACK_BDB, ERR,
154 "bdb_db_open: only one suffix allowed\n", 0, 0, 0 );
156 Debug( LDAP_DEBUG_ANY,
157 "bdb_db_open: only one suffix allowed\n", 0, 0, 0 );
162 /* we should check existance of dbenv_home and db_directory */
164 rc = db_env_create( &bdb->bi_dbenv, 0 );
167 LDAP_LOG( BACK_BDB, ERR,
168 "bdb_db_open: db_env_create failed: %s (%d)\n",
169 db_strerror(rc), rc, 0 );
171 Debug( LDAP_DEBUG_ANY,
172 "bdb_db_open: db_env_create failed: %s (%d)\n",
173 db_strerror(rc), rc, 0 );
178 flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE
179 | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN;
182 /* Never do automatic recovery, must perform it manually.
183 * Otherwise restarting with gentlehup will corrupt the
186 if( !(slapMode & SLAP_TOOL_MODE) ) flags |= DB_RECOVER;
189 /* If a key was set, use shared memory for the BDB environment */
190 if ( bdb->bi_shm_key ) {
191 bdb->bi_dbenv->set_shm_key( bdb->bi_dbenv, bdb->bi_shm_key );
192 flags |= DB_SYSTEM_MEM;
195 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
196 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
197 bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
199 /* One long-lived TXN per thread, two TXNs per write op */
200 bdb->bi_dbenv->set_tx_max( bdb->bi_dbenv, connection_pool_max * 3 );
202 if ( bdb->bi_idl_cache_max_size ) {
203 bdb->bi_idl_tree = NULL;
204 ldap_pvt_thread_rdwr_init( &bdb->bi_idl_tree_rwlock );
205 ldap_pvt_thread_mutex_init( &bdb->bi_idl_tree_lrulock );
206 bdb->bi_idl_cache_size = 0;
211 char dir[MAXPATHLEN], *ptr;
213 if (bdb->bi_dbenv_home[0] == '.') {
214 /* If home is a relative path, relative subdirs
215 * are just concat'd by BDB. We don't want the
216 * path to be concat'd twice, e.g.
217 * ./test-db/./test-db/tmp
221 ptr = lutil_strcopy( dir, bdb->bi_dbenv_home );
222 *ptr++ = LDAP_DIRSEP[0];
228 strcpy( ptr, BDB_TMP_SUBDIR );
232 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
235 LDAP_LOG( BACK_BDB, ERR,
236 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
237 dir, db_strerror(rc), rc );
239 Debug( LDAP_DEBUG_ANY,
240 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
241 dir, db_strerror(rc), rc );
246 strcpy( ptr, BDB_LG_SUBDIR );
250 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
253 LDAP_LOG( BACK_BDB, ERR,
254 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
255 dir, db_strerror(rc), rc );
257 Debug( LDAP_DEBUG_ANY,
258 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
259 dir, db_strerror(rc), rc );
264 strcpy( ptr, BDB_DATA_SUBDIR );
268 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
271 LDAP_LOG( BACK_BDB, ERR,
272 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
273 dir, db_strerror(rc), rc );
275 Debug( LDAP_DEBUG_ANY,
276 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
277 dir, db_strerror(rc), rc );
285 LDAP_LOG( BACK_BDB, DETAIL1,
286 "bdb_db_open: dbenv_open %s\n", bdb->bi_dbenv_home, 0, 0 );
288 Debug( LDAP_DEBUG_TRACE,
289 "bdb_db_open: dbenv_open(%s)\n",
290 bdb->bi_dbenv_home, 0, 0);
294 strcpy( path, bdb->bi_dbenv_home );
296 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
299 bdb->bi_dbenv_mode );
301 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
304 bdb->bi_dbenv_mode );
308 LDAP_LOG( BACK_BDB, ERR,
309 "bdb_db_open: dbenv_open failed: %s (%d)\n",
310 db_strerror(rc), rc, 0 );
312 Debug( LDAP_DEBUG_ANY,
313 "bdb_db_open: dbenv_open failed: %s (%d)\n",
314 db_strerror(rc), rc, 0 );
319 if( bdb->bi_dbenv_xflags != 0 ) {
320 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
321 bdb->bi_dbenv_xflags, 1);
324 LDAP_LOG( BACK_BDB, ERR,
325 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
326 db_strerror(rc), rc, 0 );
328 Debug( LDAP_DEBUG_ANY,
329 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
330 db_strerror(rc), rc, 0 );
336 flags = DB_THREAD | bdb->bi_db_opflags;
338 bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
339 BDB_INDICES * sizeof(struct bdb_db_info *) );
341 /* open (and create) main database */
342 for( i = 0; bdbi_databases[i].name; i++ ) {
343 struct bdb_db_info *db;
345 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
347 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
350 LDAP_LOG( BACK_BDB, ERR,
351 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
352 bdb->bi_dbenv_home, db_strerror(rc), rc );
354 Debug( LDAP_DEBUG_ANY,
355 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
356 bdb->bi_dbenv_home, db_strerror(rc), rc );
361 if( i == BDB_ID2ENTRY ) {
362 rc = db->bdi_db->set_bt_compare( db->bdi_db,
364 rc = db->bdi_db->set_pagesize( db->bdi_db,
365 BDB_ID2ENTRY_PAGESIZE );
366 if ( slapMode & SLAP_TOOL_READMAIN ) {
372 rc = db->bdi_db->set_flags( db->bdi_db,
373 DB_DUP | DB_DUPSORT );
375 rc = db->bdi_db->set_dup_compare( db->bdi_db,
377 if ( slapMode & SLAP_TOOL_READONLY ) {
383 rc = db->bdi_db->set_dup_compare( db->bdi_db,
385 rc = db->bdi_db->set_bt_compare( db->bdi_db,
387 if ( slapMode & (SLAP_TOOL_READONLY|SLAP_TOOL_READMAIN) ) {
393 rc = db->bdi_db->set_pagesize( db->bdi_db,
398 strcpy( path, bdbi_databases[i].file );
400 rc = DB_OPEN( db->bdi_db,
402 /* bdbi_databases[i].name, */ NULL,
403 bdbi_databases[i].type,
404 bdbi_databases[i].flags | flags,
405 bdb->bi_dbenv_mode );
407 rc = DB_OPEN( db->bdi_db,
408 bdbi_databases[i].file,
409 /* bdbi_databases[i].name, */ NULL,
410 bdbi_databases[i].type,
411 bdbi_databases[i].flags | flags,
412 bdb->bi_dbenv_mode );
417 LDAP_LOG( BACK_BDB, ERR,
418 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
419 bdb->bi_dbenv_home, db_strerror(rc), rc );
421 Debug( LDAP_DEBUG_ANY,
422 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
423 bdb->bi_dbenv_home, db_strerror(rc), rc );
428 flags &= ~(DB_CREATE | DB_RDONLY);
429 db->bdi_name = bdbi_databases[i].name;
430 bdb->bi_databases[i] = db;
433 bdb->bi_databases[i] = NULL;
434 bdb->bi_ndatabases = i;
437 rc = bdb_last_id( be, NULL );
440 LDAP_LOG( BACK_BDB, ERR,
441 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
442 bdb->bi_dbenv_home, db_strerror(rc), rc );
444 Debug( LDAP_DEBUG_ANY,
445 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
446 bdb->bi_dbenv_home, db_strerror(rc), rc );
451 XLOCK_ID(bdb->bi_dbenv, &bdb->bi_cache.c_locker);
453 /* <insert> open (and create) index databases */
458 bdb_db_close( BackendDB *be )
461 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
462 struct bdb_db_info *db;
463 bdb_idl_cache_entry_t *entry, *next_entry;
465 while( bdb->bi_ndatabases-- ) {
466 db = bdb->bi_databases[bdb->bi_ndatabases];
467 rc = db->bdi_db->close( db->bdi_db, 0 );
468 /* Lower numbered names are not strdup'd */
469 if( bdb->bi_ndatabases >= BDB_NDB )
470 free( db->bdi_name );
473 free( bdb->bi_databases );
474 bdb_attr_index_destroy( bdb->bi_attrs );
476 bdb_cache_release_all (&bdb->bi_cache);
478 if ( bdb->bi_idl_cache_max_size ) {
479 ldap_pvt_thread_rdwr_wlock ( &bdb->bi_idl_tree_rwlock );
480 avl_free( bdb->bi_idl_tree, NULL );
481 entry = bdb->bi_idl_lru_head;
482 while ( entry != NULL ) {
483 next_entry = entry->idl_lru_next;
486 free( entry->kstr.bv_val );
490 ldap_pvt_thread_rdwr_wunlock ( &bdb->bi_idl_tree_rwlock );
493 XLOCK_ID_FREE(bdb->bi_dbenv, bdb->bi_cache.c_locker);
499 bdb_db_destroy( BackendDB *be )
502 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
504 /* close db environment */
505 if( bdb->bi_dbenv ) {
506 /* force a checkpoint */
507 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
510 LDAP_LOG( BACK_BDB, ERR,
511 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
512 db_strerror(rc), rc, 0 );
514 Debug( LDAP_DEBUG_ANY,
515 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
516 db_strerror(rc), rc, 0 );
520 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
521 bdb->bi_dbenv = NULL;
524 LDAP_LOG( BACK_BDB, ERR,
525 "bdb_db_destroy: close failed: %s (%d)\n",
526 db_strerror(rc), rc, 0 );
528 Debug( LDAP_DEBUG_ANY,
529 "bdb_db_destroy: close failed: %s (%d)\n",
530 db_strerror(rc), rc, 0 );
536 if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
538 ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
539 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_mutex );
540 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_dntree.bei_kids_mutex );
541 ldap_pvt_thread_rdwr_destroy ( &bdb->bi_pslist_rwlock );
542 ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
543 ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
544 if ( bdb->bi_idl_cache_max_size ) {
545 ldap_pvt_thread_rdwr_destroy( &bdb->bi_idl_tree_rwlock );
546 ldap_pvt_thread_mutex_destroy( &bdb->bi_idl_tree_lrulock );
550 be->be_private = NULL;
555 #if (SLAPD_BDB == SLAPD_MOD_DYNAMIC && !defined(BDB_HIER)) || \
556 (SLAPD_HDB == SLAPD_MOD_DYNAMIC && defined(BDB_HIER))
557 int init_module( int argc, char *argv[] ) {
560 memset( &bi, '\0', sizeof(bi) );
566 bi.bi_init = bdb_initialize;
571 #endif /* SLAPD_BDB */
577 static char *controls[] = {
579 LDAP_CONTROL_MANAGEDSAIT,
581 LDAP_CONTROL_PAGEDRESULTS,
582 #ifdef LDAP_CONTROL_SUBENTRIES
583 LDAP_CONTROL_SUBENTRIES,
585 LDAP_CONTROL_VALUESRETURNFILTER,
589 /* initialize the underlying database system */
591 LDAP_LOG( BACK_BDB, ENTRY, "bdb_db_initialize\n", 0, 0, 0 );
593 Debug( LDAP_DEBUG_TRACE, "bdb_initialize: initialize BDB backend\n",
598 SLAP_BFLAG_INCREMENT |
599 #ifdef BDB_SUBENTRIES
600 SLAP_BFLAG_SUBENTRIES |
605 SLAP_BFLAG_REFERRALS;
607 bi->bi_controls = controls;
609 { /* version check */
610 int major, minor, patch;
611 char *version = db_version( &major, &minor, &patch );
615 /* All our stdio does an ASCII to EBCDIC conversion on
616 * the output. Strings from the BDB library are already
617 * in EBCDIC; we have to go back and forth...
619 strcpy( v2, version );
624 if( major != DB_VERSION_MAJOR ||
625 minor != DB_VERSION_MINOR ||
626 patch < DB_VERSION_PATCH )
629 LDAP_LOG( BACK_BDB, ERR,
630 "bdb_initialize: BDB library version mismatch:"
631 " expected " DB_VERSION_STRING ","
632 " got %s\n", version, 0, 0 );
634 Debug( LDAP_DEBUG_ANY,
635 "bdb_initialize: BDB library version mismatch:"
636 " expected " DB_VERSION_STRING ","
637 " got %s\n", version, 0, 0 );
642 LDAP_LOG( BACK_BDB, DETAIL1,
643 "bdb_db_initialize: %s\n", version, 0, 0 );
645 Debug( LDAP_DEBUG_ANY, "bdb_initialize: %s\n",
650 db_env_set_func_free( ber_memfree );
651 db_env_set_func_malloc( (db_malloc *)ber_memalloc );
652 db_env_set_func_realloc( (db_realloc *)ber_memrealloc );
654 /* This is a no-op on a NO_THREAD build. Leave the default
655 * alone so that BDB will sleep on interprocess conflicts.
657 db_env_set_func_yield( ldap_pvt_thread_yield );
661 static char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
663 bdb_uuid.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ));
664 bdb_uuid.bv_val = uuidbuf;
672 bi->bi_db_init = bdb_db_init;
673 bi->bi_db_config = bdb_db_config;
674 bi->bi_db_open = bdb_db_open;
675 bi->bi_db_close = bdb_db_close;
676 bi->bi_db_destroy = bdb_db_destroy;
678 bi->bi_op_add = bdb_add;
679 bi->bi_op_bind = bdb_bind;
680 bi->bi_op_compare = bdb_compare;
681 bi->bi_op_delete = bdb_delete;
682 bi->bi_op_modify = bdb_modify;
683 bi->bi_op_modrdn = bdb_modrdn;
684 bi->bi_op_search = bdb_search;
686 bi->bi_op_unbind = 0;
688 bi->bi_op_abandon = bdb_abandon;
689 bi->bi_op_cancel = bdb_cancel;
691 bi->bi_extended = bdb_extended;
693 bi->bi_chk_referrals = bdb_referrals;
694 bi->bi_operational = bdb_operational;
695 bi->bi_has_subordinates = bdb_hasSubordinates;
696 bi->bi_entry_release_rw = bdb_entry_release;
697 bi->bi_entry_get_rw = bdb_entry_get;
700 * hooks for slap tools
702 bi->bi_tool_entry_open = bdb_tool_entry_open;
703 bi->bi_tool_entry_close = bdb_tool_entry_close;
704 bi->bi_tool_entry_first = bdb_tool_entry_next;
705 bi->bi_tool_entry_next = bdb_tool_entry_next;
706 bi->bi_tool_entry_get = bdb_tool_entry_get;
707 bi->bi_tool_entry_put = bdb_tool_entry_put;
708 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
709 bi->bi_tool_sync = 0;
710 bi->bi_tool_dn2id_get = bdb_tool_dn2id_get;
711 bi->bi_tool_id2entry_get = bdb_tool_id2entry_get;
712 bi->bi_tool_entry_modify = bdb_tool_entry_modify;
714 bi->bi_connection_init = 0;
715 bi->bi_connection_destroy = 0;