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 = { 0, NULL };
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 BDB database\n",
78 /* indicate system schema supported */
80 SLAP_BFLAG_INCREMENT |
82 SLAP_BFLAG_SUBENTRIES |
89 /* allocate backend-database-specific stuff */
90 bdb = (struct bdb_info *) ch_calloc( 1, sizeof(struct bdb_info) );
92 /* DBEnv parameters */
93 bdb->bi_dbenv_home = ch_strdup( SLAPD_DEFAULT_DB_DIR );
94 bdb->bi_dbenv_xflags = 0;
95 bdb->bi_dbenv_mode = SLAPD_DEFAULT_DB_MODE;
97 bdb->bi_cache.c_maxsize = DEFAULT_CACHE_SIZE;
99 bdb->bi_lock_detect = DB_LOCK_DEFAULT;
100 bdb->bi_search_stack_depth = DEFAULT_SEARCH_STACK_DEPTH;
101 bdb->bi_search_stack = NULL;
103 LDAP_LIST_INIT (&bdb->bi_psearch_list);
105 ldap_pvt_thread_mutex_init( &bdb->bi_database_mutex );
106 ldap_pvt_thread_mutex_init( &bdb->bi_lastid_mutex );
107 ldap_pvt_thread_rdwr_init ( &bdb->bi_pslist_rwlock );
108 ldap_pvt_thread_mutex_init( &bdb->bi_cache.lru_mutex );
109 ldap_pvt_thread_mutex_init( &bdb->bi_cache.c_dntree.bei_kids_mutex );
110 ldap_pvt_thread_rdwr_init ( &bdb->bi_cache.c_rwlock );
112 be->be_private = bdb;
118 bdb_db_open( BackendDB *be )
121 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
124 char path[MAXPATHLEN];
128 LDAP_LOG( BACK_BDB, ARGS,
129 "bdb_db_open: %s\n", be->be_suffix[0].bv_val, 0, 0 );
131 Debug( LDAP_DEBUG_ARGS,
133 be->be_suffix[0].bv_val, 0, 0 );
136 #ifndef BDB_MULTIPLE_SUFFIXES
137 if ( be->be_suffix[1].bv_val ) {
139 LDAP_LOG( BACK_BDB, ERR,
140 "bdb_db_open: only one suffix allowed\n", 0, 0, 0 );
142 Debug( LDAP_DEBUG_ANY,
143 "bdb_db_open: only one suffix allowed\n", 0, 0, 0 );
148 /* we should check existance of dbenv_home and db_directory */
150 rc = db_env_create( &bdb->bi_dbenv, 0 );
153 LDAP_LOG( BACK_BDB, ERR,
154 "bdb_db_open: db_env_create failed: %s (%d)\n",
155 db_strerror(rc), rc, 0 );
157 Debug( LDAP_DEBUG_ANY,
158 "bdb_db_open: db_env_create failed: %s (%d)\n",
159 db_strerror(rc), rc, 0 );
164 flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE
165 | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN;
168 /* Never do automatic recovery, must perform it manually.
169 * Otherwise restarting with gentlehup will corrupt the
172 if( !(slapMode & SLAP_TOOL_MODE) ) flags |= DB_RECOVER;
175 /* If a key was set, use shared memory for the BDB environment */
176 if ( bdb->bi_shm_key ) {
177 bdb->bi_dbenv->set_shm_key( bdb->bi_dbenv, bdb->bi_shm_key );
178 flags |= DB_SYSTEM_MEM;
181 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
182 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
183 bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
185 /* default IDL cache to 10x entry cache */
186 if ( bdb->bi_idl_cache_max_size == 0 )
187 bdb->bi_idl_cache_max_size = 10 * bdb->bi_cache.c_maxsize;
189 if ( bdb->bi_idl_cache_max_size ) {
190 bdb->bi_idl_tree = NULL;
191 ldap_pvt_thread_rdwr_init( &bdb->bi_idl_tree_rwlock );
192 ldap_pvt_thread_mutex_init( &bdb->bi_idl_tree_lrulock );
193 bdb->bi_idl_cache_size = 0;
198 char dir[MAXPATHLEN], *ptr;
200 if (bdb->bi_dbenv_home[0] == '.') {
201 /* If home is a relative path, relative subdirs
202 * are just concat'd by BDB. We don't want the
203 * path to be concat'd twice, e.g.
204 * ./test-db/./test-db/tmp
208 ptr = lutil_strcopy( dir, bdb->bi_dbenv_home );
209 *ptr++ = LDAP_DIRSEP[0];
215 strcpy( ptr, BDB_TMP_SUBDIR );
219 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
222 LDAP_LOG( BACK_BDB, ERR,
223 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
224 dir, db_strerror(rc), rc );
226 Debug( LDAP_DEBUG_ANY,
227 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
228 dir, db_strerror(rc), rc );
233 strcpy( ptr, BDB_LG_SUBDIR );
237 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
240 LDAP_LOG( BACK_BDB, ERR,
241 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
242 dir, db_strerror(rc), rc );
244 Debug( LDAP_DEBUG_ANY,
245 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
246 dir, db_strerror(rc), rc );
251 strcpy( ptr, BDB_DATA_SUBDIR );
255 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
258 LDAP_LOG( BACK_BDB, ERR,
259 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
260 dir, db_strerror(rc), rc );
262 Debug( LDAP_DEBUG_ANY,
263 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
264 dir, db_strerror(rc), rc );
272 LDAP_LOG( BACK_BDB, DETAIL1,
273 "bdb_db_open: dbenv_open %s\n", bdb->bi_dbenv_home, 0, 0 );
275 Debug( LDAP_DEBUG_TRACE,
276 "bdb_db_open: dbenv_open(%s)\n",
277 bdb->bi_dbenv_home, 0, 0);
281 strcpy( path, bdb->bi_dbenv_home );
283 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
286 bdb->bi_dbenv_mode );
288 rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
291 bdb->bi_dbenv_mode );
295 LDAP_LOG( BACK_BDB, ERR,
296 "bdb_db_open: dbenv_open failed: %s (%d)\n",
297 db_strerror(rc), rc, 0 );
299 Debug( LDAP_DEBUG_ANY,
300 "bdb_db_open: dbenv_open failed: %s (%d)\n",
301 db_strerror(rc), rc, 0 );
306 if( bdb->bi_dbenv_xflags != 0 ) {
307 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
308 bdb->bi_dbenv_xflags, 1);
311 LDAP_LOG( BACK_BDB, ERR,
312 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
313 db_strerror(rc), rc, 0 );
315 Debug( LDAP_DEBUG_ANY,
316 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
317 db_strerror(rc), rc, 0 );
323 flags = DB_THREAD | bdb->bi_db_opflags;
325 bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
326 BDB_INDICES * sizeof(struct bdb_db_info *) );
328 /* open (and create) main database */
329 for( i = 0; bdbi_databases[i].name; i++ ) {
330 struct bdb_db_info *db;
332 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
334 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
337 LDAP_LOG( BACK_BDB, ERR,
338 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
339 bdb->bi_dbenv_home, db_strerror(rc), rc );
341 Debug( LDAP_DEBUG_ANY,
342 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
343 bdb->bi_dbenv_home, db_strerror(rc), rc );
348 if( i == BDB_ID2ENTRY ) {
349 rc = db->bdi_db->set_pagesize( db->bdi_db,
350 BDB_ID2ENTRY_PAGESIZE );
351 if ( slapMode & SLAP_TOOL_READMAIN ) {
357 rc = db->bdi_db->set_flags( db->bdi_db,
358 DB_DUP | DB_DUPSORT );
360 if ( slapMode & SLAP_TOOL_READONLY ) {
366 rc = db->bdi_db->set_dup_compare( db->bdi_db,
368 if ( slapMode & (SLAP_TOOL_READONLY|SLAP_TOOL_READMAIN) ) {
374 rc = db->bdi_db->set_pagesize( db->bdi_db,
379 strcpy( path, bdbi_databases[i].file );
381 rc = DB_OPEN( db->bdi_db,
383 /* bdbi_databases[i].name, */ NULL,
384 bdbi_databases[i].type,
385 bdbi_databases[i].flags | flags,
386 bdb->bi_dbenv_mode );
388 rc = DB_OPEN( db->bdi_db,
389 bdbi_databases[i].file,
390 /* bdbi_databases[i].name, */ NULL,
391 bdbi_databases[i].type,
392 bdbi_databases[i].flags | flags,
393 bdb->bi_dbenv_mode );
398 LDAP_LOG( BACK_BDB, ERR,
399 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
400 bdb->bi_dbenv_home, db_strerror(rc), rc );
402 Debug( LDAP_DEBUG_ANY,
403 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
404 bdb->bi_dbenv_home, db_strerror(rc), rc );
409 flags &= ~(DB_CREATE | DB_RDONLY);
410 db->bdi_name = bdbi_databases[i].name;
411 bdb->bi_databases[i] = db;
414 bdb->bi_databases[i] = NULL;
415 bdb->bi_ndatabases = i;
418 rc = bdb_last_id( be, NULL );
421 LDAP_LOG( BACK_BDB, ERR,
422 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
423 bdb->bi_dbenv_home, db_strerror(rc), rc );
425 Debug( LDAP_DEBUG_ANY,
426 "bdb_db_open: last_id(%s) failed: %s (%d)\n",
427 bdb->bi_dbenv_home, db_strerror(rc), rc );
432 /* <insert> open (and create) index databases */
437 bdb_db_close( BackendDB *be )
440 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
441 struct bdb_db_info *db;
442 bdb_idl_cache_entry_t *entry, *next_entry;
444 while( bdb->bi_ndatabases-- ) {
445 db = bdb->bi_databases[bdb->bi_ndatabases];
446 rc = db->bdi_db->close( db->bdi_db, 0 );
447 /* Lower numbered names are not strdup'd */
448 if( bdb->bi_ndatabases >= BDB_NDB )
449 free( db->bdi_name );
452 free( bdb->bi_databases );
453 bdb_attr_index_destroy( bdb->bi_attrs );
455 bdb_cache_release_all (&bdb->bi_cache);
457 if ( bdb->bi_idl_cache_max_size ) {
458 ldap_pvt_thread_rdwr_wlock ( &bdb->bi_idl_tree_rwlock );
459 avl_free( bdb->bi_idl_tree, NULL );
460 entry = bdb->bi_idl_lru_head;
461 while ( entry != NULL ) {
462 next_entry = entry->idl_lru_next;
465 free( entry->kstr.bv_val );
469 ldap_pvt_thread_rdwr_wunlock ( &bdb->bi_idl_tree_rwlock );
476 bdb_db_destroy( BackendDB *be )
479 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
481 /* close db environment */
482 if( bdb->bi_dbenv ) {
483 /* force a checkpoint */
484 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
487 LDAP_LOG( BACK_BDB, ERR,
488 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
489 db_strerror(rc), rc, 0 );
491 Debug( LDAP_DEBUG_ANY,
492 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
493 db_strerror(rc), rc, 0 );
497 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
498 bdb->bi_dbenv = NULL;
501 LDAP_LOG( BACK_BDB, ERR,
502 "bdb_db_destroy: close failed: %s (%d)\n",
503 db_strerror(rc), rc, 0 );
505 Debug( LDAP_DEBUG_ANY,
506 "bdb_db_destroy: close failed: %s (%d)\n",
507 db_strerror(rc), rc, 0 );
513 if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
515 ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
516 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_mutex );
517 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_dntree.bei_kids_mutex );
518 ldap_pvt_thread_rdwr_destroy ( &bdb->bi_pslist_rwlock );
519 ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
520 ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
522 if ( bdb->bi_idl_cache_max_size ) {
523 ldap_pvt_thread_rdwr_destroy( &bdb->bi_idl_tree_rwlock );
524 ldap_pvt_thread_mutex_destroy( &bdb->bi_idl_tree_lrulock );
528 be->be_private = NULL;
533 #if (defined(SLAPD_BDB_DYNAMIC) && !defined(BDB_HIER)) || \
534 (defined(SLAPD_HDB_DYNAMIC) && defined(BDB_HIER))
535 int init_module( int argc, char *argv[] ) {
538 memset( &bi, '\0', sizeof(bi) );
544 bi.bi_init = bdb_initialize;
549 #endif /* SLAPD_BDB_DYNAMIC */
556 static char *controls[] = {
558 LDAP_CONTROL_MANAGEDSAIT,
560 LDAP_CONTROL_PAGEDRESULTS,
561 #ifdef LDAP_CONTROL_SUBENTRIES
562 LDAP_CONTROL_SUBENTRIES,
564 LDAP_CONTROL_VALUESRETURNFILTER,
568 bi->bi_controls = controls;
570 /* initialize the underlying database system */
572 LDAP_LOG( BACK_BDB, ENTRY, "bdb_db_initialize\n", 0, 0, 0 );
574 Debug( LDAP_DEBUG_TRACE, "bdb_initialize: initialize BDB backend\n",
578 { /* version check */
579 int major, minor, patch;
580 char *version = db_version( &major, &minor, &patch );
584 /* All our stdio does an ASCII to EBCDIC conversion on
585 * the output. Strings from the BDB library are already
586 * in EBCDIC; we have to go back and forth...
588 strcpy( v2, version );
593 if( major != DB_VERSION_MAJOR ||
594 minor != DB_VERSION_MINOR ||
595 patch < DB_VERSION_PATCH )
598 LDAP_LOG( BACK_BDB, ERR,
599 "bdb_initialize: BDB library version mismatch:"
600 " expected " DB_VERSION_STRING ","
601 " got %s\n", version, 0, 0 );
603 Debug( LDAP_DEBUG_ANY,
604 "bdb_initialize: BDB library version mismatch:"
605 " expected " DB_VERSION_STRING ","
606 " got %s\n", version, 0, 0 );
611 LDAP_LOG( BACK_BDB, DETAIL1,
612 "bdb_db_initialize: %s\n", version, 0, 0 );
614 Debug( LDAP_DEBUG_ANY, "bdb_initialize: %s\n",
619 db_env_set_func_free( ber_memfree );
620 db_env_set_func_malloc( (db_malloc *)ber_memalloc );
621 db_env_set_func_realloc( (db_realloc *)ber_memrealloc );
623 /* This is a no-op on a NO_THREAD build. Leave the default
624 * alone so that BDB will sleep on interprocess conflicts.
626 db_env_set_func_yield( ldap_pvt_thread_yield );
630 static char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
632 bdb_uuid.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ));
633 bdb_uuid.bv_val = uuidbuf;
641 bi->bi_db_init = bdb_db_init;
642 bi->bi_db_config = bdb_db_config;
643 bi->bi_db_open = bdb_db_open;
644 bi->bi_db_close = bdb_db_close;
645 bi->bi_db_destroy = bdb_db_destroy;
647 bi->bi_op_add = bdb_add;
648 bi->bi_op_bind = bdb_bind;
649 bi->bi_op_compare = bdb_compare;
650 bi->bi_op_delete = bdb_delete;
651 bi->bi_op_modify = bdb_modify;
652 bi->bi_op_modrdn = bdb_modrdn;
653 bi->bi_op_search = bdb_search;
655 bi->bi_op_unbind = 0;
657 bi->bi_op_abandon = bdb_abandon;
658 bi->bi_op_cancel = bdb_cancel;
660 bi->bi_extended = bdb_extended;
662 bi->bi_chk_referrals = bdb_referrals;
663 bi->bi_operational = bdb_operational;
664 bi->bi_has_subordinates = bdb_hasSubordinates;
665 bi->bi_entry_release_rw = bdb_entry_release;
666 bi->bi_entry_get_rw = bdb_entry_get;
669 * hooks for slap tools
671 bi->bi_tool_entry_open = bdb_tool_entry_open;
672 bi->bi_tool_entry_close = bdb_tool_entry_close;
673 bi->bi_tool_entry_first = bdb_tool_entry_next;
674 bi->bi_tool_entry_next = bdb_tool_entry_next;
675 bi->bi_tool_entry_get = bdb_tool_entry_get;
676 bi->bi_tool_entry_put = bdb_tool_entry_put;
677 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
678 bi->bi_tool_sync = 0;
679 bi->bi_tool_dn2id_get = bdb_tool_dn2id_get;
680 bi->bi_tool_id2entry_get = bdb_tool_id2entry_get;
681 bi->bi_tool_entry_modify = bdb_tool_entry_modify;
683 bi->bi_connection_init = 0;
684 bi->bi_connection_destroy = 0;