Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / openldap / dist / servers / slapd / back-bdb / init.c
blob16ab550547c939d80fe7e15320cb8eb9c39de459
1 /* init.c - initialize bdb backend */
2 /* $OpenLDAP: pkg/ldap/servers/slapd/back-bdb/init.c,v 1.247.2.11 2008/02/11 23:26:45 kurt Exp $ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2008 The OpenLDAP Foundation.
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
10 * Public License.
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>.
17 #include "portable.h"
19 #include <stdio.h>
20 #include <ac/string.h>
21 #include <ac/unistd.h>
22 #include <ac/stdlib.h>
23 #include <ac/errno.h>
24 #include <sys/stat.h>
25 #include "back-bdb.h"
26 #include <lutil.h>
27 #include <ldap_rq.h>
28 #include "alock.h"
29 #include "config.h"
31 static const struct bdbi_database {
32 char *file;
33 struct berval name;
34 int type;
35 int flags;
36 } bdbi_databases[] = {
37 { "id2entry" BDB_SUFFIX, BER_BVC("id2entry"), DB_BTREE, 0 },
38 { "dn2id" BDB_SUFFIX, BER_BVC("dn2id"), DB_BTREE, 0 },
39 { NULL, BER_BVNULL, 0, 0 }
42 typedef void * db_malloc(size_t);
43 typedef void * db_realloc(void *, size_t);
45 #define bdb_db_init BDB_SYMBOL(db_init)
46 #define bdb_db_open BDB_SYMBOL(db_open)
47 #define bdb_db_close BDB_SYMBOL(db_close)
49 static int
50 bdb_db_init( BackendDB *be, ConfigReply *cr )
52 struct bdb_info *bdb;
53 int rc;
55 Debug( LDAP_DEBUG_TRACE,
56 LDAP_XSTRING(bdb_db_init) ": Initializing " BDB_UCTYPE " database\n",
57 0, 0, 0 );
59 /* allocate backend-database-specific stuff */
60 bdb = (struct bdb_info *) ch_calloc( 1, sizeof(struct bdb_info) );
62 /* DBEnv parameters */
63 bdb->bi_dbenv_home = ch_strdup( SLAPD_DEFAULT_DB_DIR );
64 bdb->bi_dbenv_xflags = 0;
65 bdb->bi_dbenv_mode = SLAPD_DEFAULT_DB_MODE;
67 bdb->bi_cache.c_maxsize = DEFAULT_CACHE_SIZE;
68 bdb->bi_cache.c_minfree = 1;
70 bdb->bi_lock_detect = DB_LOCK_DEFAULT;
71 bdb->bi_search_stack_depth = DEFAULT_SEARCH_STACK_DEPTH;
72 bdb->bi_search_stack = NULL;
74 ldap_pvt_thread_mutex_init( &bdb->bi_database_mutex );
75 ldap_pvt_thread_mutex_init( &bdb->bi_lastid_mutex );
76 #ifdef BDB_HIER
77 ldap_pvt_thread_mutex_init( &bdb->bi_modrdns_mutex );
78 #endif
79 ldap_pvt_thread_mutex_init( &bdb->bi_cache.c_lru_mutex );
80 ldap_pvt_thread_mutex_init( &bdb->bi_cache.c_count_mutex );
81 ldap_pvt_thread_mutex_init( &bdb->bi_cache.c_eifree_mutex );
82 ldap_pvt_thread_mutex_init( &bdb->bi_cache.c_dntree.bei_kids_mutex );
83 ldap_pvt_thread_rdwr_init ( &bdb->bi_cache.c_rwlock );
84 ldap_pvt_thread_rdwr_init( &bdb->bi_idl_tree_rwlock );
85 ldap_pvt_thread_mutex_init( &bdb->bi_idl_tree_lrulock );
87 be->be_private = bdb;
88 be->be_cf_ocs = be->bd_info->bi_cf_ocs;
90 #ifndef BDB_MULTIPLE_SUFFIXES
91 SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_ONE_SUFFIX;
92 #endif
94 rc = bdb_monitor_db_init( be );
96 return rc;
99 static int
100 bdb_db_close( BackendDB *be, ConfigReply *cr );
102 static int
103 bdb_db_open( BackendDB *be, ConfigReply *cr )
105 int rc, i;
106 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
107 struct stat stat1, stat2;
108 u_int32_t flags;
109 char path[MAXPATHLEN];
110 char *dbhome;
111 Entry *e = NULL;
112 int do_recover = 0, do_alock_recover = 0;
113 int alockt, quick = 0;
115 if ( be->be_suffix == NULL ) {
116 Debug( LDAP_DEBUG_ANY,
117 LDAP_XSTRING(bdb_db_open) ": need suffix.\n",
118 1, 0, 0 );
119 return -1;
122 Debug( LDAP_DEBUG_ARGS,
123 LDAP_XSTRING(bdb_db_open) ": \"%s\"\n",
124 be->be_suffix[0].bv_val, 0, 0 );
126 /* Check existence of dbenv_home. Any error means trouble */
127 rc = stat( bdb->bi_dbenv_home, &stat1 );
128 if( rc != 0 ) {
129 Debug( LDAP_DEBUG_ANY,
130 LDAP_XSTRING(bdb_db_open) ": database \"%s\": "
131 "cannot access database directory \"%s\" (%d).\n",
132 be->be_suffix[0].bv_val, bdb->bi_dbenv_home, errno );
133 return -1;
136 /* Perform database use arbitration/recovery logic */
137 alockt = (slapMode & SLAP_TOOL_READONLY) ? ALOCK_LOCKED : ALOCK_UNIQUE;
138 if ( slapMode & SLAP_TOOL_QUICK ) {
139 alockt |= ALOCK_NOSAVE;
140 quick = 1;
143 rc = alock_open( &bdb->bi_alock_info,
144 "slapd",
145 bdb->bi_dbenv_home, alockt );
147 /* alockt is TRUE if the existing environment was created in Quick mode */
148 alockt = (rc & ALOCK_NOSAVE) ? 1 : 0;
149 rc &= ~ALOCK_NOSAVE;
151 if( rc == ALOCK_RECOVER ) {
152 Debug( LDAP_DEBUG_ANY,
153 LDAP_XSTRING(bdb_db_open) ": database \"%s\": "
154 "unclean shutdown detected; attempting recovery.\n",
155 be->be_suffix[0].bv_val, 0, 0 );
156 do_alock_recover = 1;
157 do_recover = DB_RECOVER;
158 } else if( rc == ALOCK_BUSY ) {
159 Debug( LDAP_DEBUG_ANY,
160 LDAP_XSTRING(bdb_db_open) ": database \"%s\": "
161 "database already in use.\n",
162 be->be_suffix[0].bv_val, 0, 0 );
163 return -1;
164 } else if( rc != ALOCK_CLEAN ) {
165 Debug( LDAP_DEBUG_ANY,
166 LDAP_XSTRING(bdb_db_open) ": database \"%s\": "
167 "alock package is unstable.\n",
168 be->be_suffix[0].bv_val, 0, 0 );
169 return -1;
173 * The DB_CONFIG file may have changed. If so, recover the
174 * database so that new settings are put into effect. Also
175 * note the possible absence of DB_CONFIG in the log.
177 if( stat( bdb->bi_db_config_path, &stat1 ) == 0 ) {
178 if ( !do_recover ) {
179 char *ptr = lutil_strcopy(path, bdb->bi_dbenv_home);
180 *ptr++ = LDAP_DIRSEP[0];
181 strcpy( ptr, "__db.001" );
182 if( stat( path, &stat2 ) == 0 ) {
183 if( stat2.st_mtime < stat1.st_mtime ) {
184 Debug( LDAP_DEBUG_ANY,
185 LDAP_XSTRING(bdb_db_open) ": DB_CONFIG for suffix \"%s\" has changed.\n",
186 be->be_suffix[0].bv_val, 0, 0 );
187 if ( quick ) {
188 Debug( LDAP_DEBUG_ANY,
189 "Cannot use Quick mode; perform manual recovery first.\n",
190 0, 0, 0 );
191 slapMode ^= SLAP_TOOL_QUICK;
192 rc = -1;
193 goto fail;
194 } else {
195 Debug( LDAP_DEBUG_ANY,
196 "Performing database recovery to activate new settings.\n",
197 0, 0, 0 );
199 do_recover = DB_RECOVER;
204 else {
205 Debug( LDAP_DEBUG_ANY,
206 LDAP_XSTRING(bdb_db_open) ": warning - no DB_CONFIG file found "
207 "in directory %s: (%d).\n"
208 "Expect poor performance for suffix \"%s\".\n",
209 bdb->bi_dbenv_home, errno, be->be_suffix[0].bv_val );
212 /* Always let slapcat run, regardless of environment state.
213 * This can be used to cause a cache flush after an unclean
214 * shutdown.
216 if ( do_recover && ( slapMode & SLAP_TOOL_READONLY )) {
217 Debug( LDAP_DEBUG_ANY,
218 LDAP_XSTRING(bdb_db_open) ": database \"%s\": "
219 "recovery skipped in read-only mode. "
220 "Run manual recovery if errors are encountered.\n",
221 be->be_suffix[0].bv_val, 0, 0 );
222 do_recover = 0;
223 quick = alockt;
226 /* An existing environment in Quick mode has nothing to recover. */
227 if ( alockt && do_recover ) {
228 Debug( LDAP_DEBUG_ANY,
229 LDAP_XSTRING(bdb_db_open) ": database \"%s\": "
230 "cannot recover, database must be reinitialized.\n",
231 be->be_suffix[0].bv_val, 0, 0 );
232 rc = -1;
233 goto fail;
236 rc = db_env_create( &bdb->bi_dbenv, 0 );
237 if( rc != 0 ) {
238 Debug( LDAP_DEBUG_ANY,
239 LDAP_XSTRING(bdb_db_open) ": database \"%s\": "
240 "db_env_create failed: %s (%d).\n",
241 be->be_suffix[0].bv_val, db_strerror(rc), rc );
242 goto fail;
245 #ifdef HAVE_EBCDIC
246 strcpy( path, bdb->bi_dbenv_home );
247 __atoe( path );
248 dbhome = path;
249 #else
250 dbhome = bdb->bi_dbenv_home;
251 #endif
253 /* If existing environment is clean but doesn't support
254 * currently requested modes, remove it.
256 if ( !do_recover && ( alockt ^ quick )) {
257 shm_retry:
258 rc = bdb->bi_dbenv->remove( bdb->bi_dbenv, dbhome, DB_FORCE );
259 if ( rc ) {
260 Debug( LDAP_DEBUG_ANY,
261 LDAP_XSTRING(bdb_db_open) ": database \"%s\": "
262 "dbenv remove failed: %s (%d).\n",
263 be->be_suffix[0].bv_val, db_strerror(rc), rc );
264 bdb->bi_dbenv = NULL;
265 goto fail;
267 rc = db_env_create( &bdb->bi_dbenv, 0 );
268 if( rc != 0 ) {
269 Debug( LDAP_DEBUG_ANY,
270 LDAP_XSTRING(bdb_db_open) ": database \"%s\": "
271 "db_env_create failed: %s (%d).\n",
272 be->be_suffix[0].bv_val, db_strerror(rc), rc );
273 goto fail;
277 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
278 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
280 bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
282 if ( !BER_BVISNULL( &bdb->bi_db_crypt_key )) {
283 rc = bdb->bi_dbenv->set_encrypt( bdb->bi_dbenv, bdb->bi_db_crypt_key.bv_val,
284 DB_ENCRYPT_AES );
285 if ( rc ) {
286 Debug( LDAP_DEBUG_ANY,
287 LDAP_XSTRING(bdb_db_open) ": database \"%s\": "
288 "dbenv set_encrypt failed: %s (%d).\n",
289 be->be_suffix[0].bv_val, db_strerror(rc), rc );
290 goto fail;
294 /* One long-lived TXN per thread, two TXNs per write op */
295 bdb->bi_dbenv->set_tx_max( bdb->bi_dbenv, connection_pool_max * 3 );
297 if( bdb->bi_dbenv_xflags != 0 ) {
298 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
299 bdb->bi_dbenv_xflags, 1);
300 if( rc != 0 ) {
301 Debug( LDAP_DEBUG_ANY,
302 LDAP_XSTRING(bdb_db_open) ": database \"%s\": "
303 "dbenv_set_flags failed: %s (%d).\n",
304 be->be_suffix[0].bv_val, db_strerror(rc), rc );
305 goto fail;
309 #define BDB_TXN_FLAGS (DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN)
311 Debug( LDAP_DEBUG_TRACE,
312 LDAP_XSTRING(bdb_db_open) ": database \"%s\": "
313 "dbenv_open(%s).\n",
314 be->be_suffix[0].bv_val, bdb->bi_dbenv_home, 0);
316 flags = DB_INIT_MPOOL | DB_CREATE | DB_THREAD;
318 if ( !quick )
319 flags |= BDB_TXN_FLAGS;
321 /* If a key was set, use shared memory for the BDB environment */
322 if ( bdb->bi_shm_key ) {
323 bdb->bi_dbenv->set_shm_key( bdb->bi_dbenv, bdb->bi_shm_key );
324 flags |= DB_SYSTEM_MEM;
326 rc = (bdb->bi_dbenv->open)( bdb->bi_dbenv, dbhome,
327 flags | do_recover, bdb->bi_dbenv_mode );
329 if ( rc ) {
330 /* Regular open failed, probably a missing shm environment.
331 * Start over, do a recovery.
333 if ( !do_recover && bdb->bi_shm_key ) {
334 bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
335 rc = db_env_create( &bdb->bi_dbenv, 0 );
336 if( rc == 0 ) {
337 Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(bdb_db_open)
338 ": database \"%s\": "
339 "shared memory env open failed, assuming stale env.\n",
340 be->be_suffix[0].bv_val, 0, 0 );
341 goto shm_retry;
344 Debug( LDAP_DEBUG_ANY,
345 LDAP_XSTRING(bdb_db_open) ": database \"%s\" cannot be %s, err %d. "
346 "Restore from backup!\n",
347 be->be_suffix[0].bv_val, do_recover ? "recovered" : "opened", rc );
348 goto fail;
351 if ( do_alock_recover && alock_recover (&bdb->bi_alock_info) != 0 ) {
352 Debug( LDAP_DEBUG_ANY,
353 LDAP_XSTRING(bdb_db_open) ": database \"%s\": alock_recover failed\n",
354 be->be_suffix[0].bv_val, 0, 0 );
355 rc = -1;
356 goto fail;
359 #ifdef SLAP_ZONE_ALLOC
360 if ( bdb->bi_cache.c_maxsize ) {
361 bdb->bi_cache.c_zctx = slap_zn_mem_create(
362 SLAP_ZONE_INITSIZE, SLAP_ZONE_MAXSIZE,
363 SLAP_ZONE_DELTA, SLAP_ZONE_SIZE);
365 #endif
367 /* Default dncache to 2x entrycache */
368 if ( bdb->bi_cache.c_maxsize && !bdb->bi_cache.c_eimax ) {
369 bdb->bi_cache.c_eimax = bdb->bi_cache.c_maxsize * 2;
372 if ( bdb->bi_idl_cache_max_size ) {
373 bdb->bi_idl_tree = NULL;
374 bdb->bi_idl_cache_size = 0;
377 flags = DB_THREAD | bdb->bi_db_opflags;
379 #ifdef DB_AUTO_COMMIT
380 if ( !quick )
381 flags |= DB_AUTO_COMMIT;
382 #endif
384 bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
385 BDB_INDICES * sizeof(struct bdb_db_info *) );
387 /* open (and create) main database */
388 for( i = 0; bdbi_databases[i].name.bv_val; i++ ) {
389 struct bdb_db_info *db;
391 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
393 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
394 if( rc != 0 ) {
395 snprintf(cr->msg, sizeof(cr->msg),
396 "database \"%s\": db_create(%s) failed: %s (%d).",
397 be->be_suffix[0].bv_val,
398 bdb->bi_dbenv_home, db_strerror(rc), rc );
399 Debug( LDAP_DEBUG_ANY,
400 LDAP_XSTRING(bdb_db_open) ": %s\n",
401 cr->msg, 0, 0 );
402 goto fail;
405 if( !BER_BVISNULL( &bdb->bi_db_crypt_key )) {
406 rc = db->bdi_db->set_flags( db->bdi_db, DB_ENCRYPT );
407 if ( rc ) {
408 snprintf(cr->msg, sizeof(cr->msg),
409 "database \"%s\": db set_flags(DB_ENCRYPT)(%s) failed: %s (%d).",
410 be->be_suffix[0].bv_val,
411 bdb->bi_dbenv_home, db_strerror(rc), rc );
412 Debug( LDAP_DEBUG_ANY,
413 LDAP_XSTRING(bdb_db_open) ": %s\n",
414 cr->msg, 0, 0 );
415 goto fail;
419 if( i == BDB_ID2ENTRY ) {
420 if ( slapMode & SLAP_TOOL_MODE )
421 db->bdi_db->mpf->set_priority( db->bdi_db->mpf,
422 DB_PRIORITY_VERY_LOW );
424 rc = db->bdi_db->set_pagesize( db->bdi_db,
425 BDB_ID2ENTRY_PAGESIZE );
426 if ( slapMode & SLAP_TOOL_READMAIN ) {
427 flags |= DB_RDONLY;
428 } else {
429 flags |= DB_CREATE;
431 } else {
432 rc = db->bdi_db->set_flags( db->bdi_db,
433 DB_DUP | DB_DUPSORT );
434 #ifndef BDB_HIER
435 if ( slapMode & SLAP_TOOL_READONLY ) {
436 flags |= DB_RDONLY;
437 } else {
438 flags |= DB_CREATE;
440 #else
441 rc = db->bdi_db->set_dup_compare( db->bdi_db,
442 bdb_dup_compare );
443 if ( slapMode & (SLAP_TOOL_READONLY|SLAP_TOOL_READMAIN) ) {
444 flags |= DB_RDONLY;
445 } else {
446 flags |= DB_CREATE;
448 #endif
449 rc = db->bdi_db->set_pagesize( db->bdi_db,
450 BDB_PAGESIZE );
453 #ifdef HAVE_EBCDIC
454 strcpy( path, bdbi_databases[i].file );
455 __atoe( path );
456 rc = DB_OPEN( db->bdi_db,
457 path,
458 /* bdbi_databases[i].name, */ NULL,
459 bdbi_databases[i].type,
460 bdbi_databases[i].flags | flags,
461 bdb->bi_dbenv_mode );
462 #else
463 rc = DB_OPEN( db->bdi_db,
464 bdbi_databases[i].file,
465 /* bdbi_databases[i].name, */ NULL,
466 bdbi_databases[i].type,
467 bdbi_databases[i].flags | flags,
468 bdb->bi_dbenv_mode );
469 #endif
471 if ( rc != 0 ) {
472 snprintf( cr->msg, sizeof(cr->msg), "database \"%s\": "
473 "db_open(%s/%s) failed: %s (%d).",
474 be->be_suffix[0].bv_val,
475 bdb->bi_dbenv_home, bdbi_databases[i].file,
476 db_strerror(rc), rc );
477 Debug( LDAP_DEBUG_ANY,
478 LDAP_XSTRING(bdb_db_open) ": %s\n",
479 cr->msg, 0, 0 );
480 db->bdi_db->close( db->bdi_db, 0 );
481 goto fail;
484 flags &= ~(DB_CREATE | DB_RDONLY);
485 db->bdi_name = bdbi_databases[i].name;
486 bdb->bi_databases[i] = db;
489 bdb->bi_databases[i] = NULL;
490 bdb->bi_ndatabases = i;
492 /* get nextid */
493 rc = bdb_last_id( be, NULL );
494 if( rc != 0 ) {
495 snprintf( cr->msg, sizeof(cr->msg), "database \"%s\": "
496 "last_id(%s) failed: %s (%d).",
497 be->be_suffix[0].bv_val, bdb->bi_dbenv_home,
498 db_strerror(rc), rc );
499 Debug( LDAP_DEBUG_ANY,
500 LDAP_XSTRING(bdb_db_open) ": %s\n",
501 cr->msg, 0, 0 );
502 goto fail;
505 if ( !quick ) {
506 #if DB_VERSION_FULL >= 0x04060012
507 u_int32_t lid;
508 XLOCK_ID(bdb->bi_dbenv, &lid);
509 __lock_getlocker(bdb->bi_dbenv->lk_handle, lid, 0, &bdb->bi_cache.c_locker);
510 #else
511 XLOCK_ID(bdb->bi_dbenv, &bdb->bi_cache.c_locker);
512 #endif
515 entry_prealloc( bdb->bi_cache.c_maxsize );
516 attr_prealloc( bdb->bi_cache.c_maxsize * 20 );
518 /* setup for empty-DN contexts */
519 if ( BER_BVISEMPTY( &be->be_nsuffix[0] )) {
520 rc = bdb_id2entry( be, NULL, 0, 0, &e );
522 if ( !e ) {
523 e = entry_alloc();
524 e->e_id = 0;
525 ber_dupbv( &e->e_name, (struct berval *)&slap_empty_bv );
526 ber_dupbv( &e->e_nname, (struct berval *)&slap_empty_bv );
528 e->e_ocflags = SLAP_OC_GLUE|SLAP_OC__END;
529 e->e_private = &bdb->bi_cache.c_dntree;
530 bdb->bi_cache.c_dntree.bei_e = e;
532 /* monitor setup */
533 rc = bdb_monitor_db_open( be );
534 if ( rc != 0 ) {
535 goto fail;
538 bdb->bi_flags |= BDB_IS_OPEN;
540 return 0;
542 fail:
543 bdb_db_close( be, NULL );
544 return rc;
547 static int
548 bdb_db_close( BackendDB *be, ConfigReply *cr )
550 int rc;
551 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
552 struct bdb_db_info *db;
553 bdb_idl_cache_entry_t *entry, *next_entry;
555 /* monitor handling */
556 (void)bdb_monitor_db_close( be );
559 Entry *e = bdb->bi_cache.c_dntree.bei_e;
560 if ( e ) {
561 bdb->bi_cache.c_dntree.bei_e = NULL;
562 e->e_private = NULL;
563 bdb_entry_return( e );
567 bdb->bi_flags &= ~BDB_IS_OPEN;
569 ber_bvarray_free( bdb->bi_db_config );
570 bdb->bi_db_config = NULL;
572 while( bdb->bi_databases && bdb->bi_ndatabases-- ) {
573 db = bdb->bi_databases[bdb->bi_ndatabases];
574 rc = db->bdi_db->close( db->bdi_db, 0 );
575 /* Lower numbered names are not strdup'd */
576 if( bdb->bi_ndatabases >= BDB_NDB )
577 free( db->bdi_name.bv_val );
578 free( db );
580 free( bdb->bi_databases );
581 bdb->bi_databases = NULL;
583 bdb_cache_release_all (&bdb->bi_cache);
585 if ( bdb->bi_idl_cache_size ) {
586 avl_free( bdb->bi_idl_tree, NULL );
587 bdb->bi_idl_tree = NULL;
588 entry = bdb->bi_idl_lru_head;
589 do {
590 next_entry = entry->idl_lru_next;
591 if ( entry->idl )
592 free( entry->idl );
593 free( entry->kstr.bv_val );
594 free( entry );
595 entry = next_entry;
596 } while ( entry != bdb->bi_idl_lru_head );
597 bdb->bi_idl_lru_head = bdb->bi_idl_lru_tail = NULL;
600 /* close db environment */
601 if( bdb->bi_dbenv ) {
602 /* Free cache locker if we enabled locking */
603 if ( !( slapMode & SLAP_TOOL_QUICK ) && bdb->bi_cache.c_locker ) {
604 #if DB_VERSION_FULL >= 0x04060012
605 XLOCK_ID_FREE(bdb->bi_dbenv, bdb->bi_cache.c_locker->id);
606 #else
607 XLOCK_ID_FREE(bdb->bi_dbenv, bdb->bi_cache.c_locker);
608 #endif
609 bdb->bi_cache.c_locker = 0;
611 #ifdef BDB_REUSE_LOCKERS
612 bdb_locker_flush( bdb->bi_dbenv );
613 #endif
614 /* force a checkpoint, but not if we were ReadOnly,
615 * and not in Quick mode since there are no transactions there.
617 if ( !( slapMode & ( SLAP_TOOL_QUICK|SLAP_TOOL_READONLY ))) {
618 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
619 if( rc != 0 ) {
620 Debug( LDAP_DEBUG_ANY,
621 "bdb_db_close: database \"%s\": "
622 "txn_checkpoint failed: %s (%d).\n",
623 be->be_suffix[0].bv_val, db_strerror(rc), rc );
627 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
628 bdb->bi_dbenv = NULL;
629 if( rc != 0 ) {
630 Debug( LDAP_DEBUG_ANY,
631 "bdb_db_close: database \"%s\": "
632 "close failed: %s (%d)\n",
633 be->be_suffix[0].bv_val, db_strerror(rc), rc );
634 return rc;
638 rc = alock_close( &bdb->bi_alock_info, slapMode & SLAP_TOOL_QUICK );
639 if( rc != 0 ) {
640 Debug( LDAP_DEBUG_ANY,
641 "bdb_db_close: database \"%s\": alock_close failed\n",
642 be->be_suffix[0].bv_val, 0, 0 );
643 return -1;
646 return 0;
649 static int
650 bdb_db_destroy( BackendDB *be, ConfigReply *cr )
652 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
654 /* monitor handling */
655 (void)bdb_monitor_db_destroy( be );
657 if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
658 if( bdb->bi_db_config_path ) ch_free( bdb->bi_db_config_path );
660 bdb_attr_index_destroy( bdb );
662 ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
663 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_lru_mutex );
664 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_count_mutex );
665 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_eifree_mutex );
666 ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_dntree.bei_kids_mutex );
667 #ifdef BDB_HIER
668 ldap_pvt_thread_mutex_destroy( &bdb->bi_modrdns_mutex );
669 #endif
670 ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
671 ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
672 ldap_pvt_thread_rdwr_destroy( &bdb->bi_idl_tree_rwlock );
673 ldap_pvt_thread_mutex_destroy( &bdb->bi_idl_tree_lrulock );
675 ch_free( bdb );
676 be->be_private = NULL;
678 return 0;
682 bdb_back_initialize(
683 BackendInfo *bi )
685 int rc;
687 static char *controls[] = {
688 LDAP_CONTROL_ASSERT,
689 LDAP_CONTROL_MANAGEDSAIT,
690 LDAP_CONTROL_NOOP,
691 LDAP_CONTROL_PAGEDRESULTS,
692 LDAP_CONTROL_PRE_READ,
693 LDAP_CONTROL_POST_READ,
694 LDAP_CONTROL_SUBENTRIES,
695 LDAP_CONTROL_X_PERMISSIVE_MODIFY,
696 #ifdef LDAP_X_TXN
697 LDAP_CONTROL_X_TXN_SPEC,
698 #endif
699 NULL
702 /* initialize the underlying database system */
703 Debug( LDAP_DEBUG_TRACE,
704 LDAP_XSTRING(bdb_back_initialize) ": initialize "
705 BDB_UCTYPE " backend\n", 0, 0, 0 );
707 bi->bi_flags |=
708 SLAP_BFLAG_INCREMENT |
709 SLAP_BFLAG_SUBENTRIES |
710 SLAP_BFLAG_ALIASES |
711 SLAP_BFLAG_REFERRALS;
713 bi->bi_controls = controls;
715 { /* version check */
716 int major, minor, patch, ver;
717 char *version = db_version( &major, &minor, &patch );
718 #ifdef HAVE_EBCDIC
719 char v2[1024];
721 /* All our stdio does an ASCII to EBCDIC conversion on
722 * the output. Strings from the BDB library are already
723 * in EBCDIC; we have to go back and forth...
725 strcpy( v2, version );
726 __etoa( v2 );
727 version = v2;
728 #endif
730 ver = (major << 24) | (minor << 16) | patch;
731 if( ver != DB_VERSION_FULL ) {
732 /* fail if a versions don't match */
733 Debug( LDAP_DEBUG_ANY,
734 LDAP_XSTRING(bdb_back_initialize) ": "
735 "BDB library version mismatch:"
736 " expected " DB_VERSION_STRING ","
737 " got %s\n", version, 0, 0 );
738 return -1;
741 Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(bdb_back_initialize)
742 ": %s\n", version, 0, 0 );
745 db_env_set_func_free( ber_memfree );
746 db_env_set_func_malloc( (db_malloc *)ber_memalloc );
747 db_env_set_func_realloc( (db_realloc *)ber_memrealloc );
748 #ifndef NO_THREAD
749 /* This is a no-op on a NO_THREAD build. Leave the default
750 * alone so that BDB will sleep on interprocess conflicts.
752 db_env_set_func_yield( ldap_pvt_thread_yield );
753 #endif
755 bi->bi_open = 0;
756 bi->bi_close = 0;
757 bi->bi_config = 0;
758 bi->bi_destroy = 0;
760 bi->bi_db_init = bdb_db_init;
761 bi->bi_db_config = config_generic_wrapper;
762 bi->bi_db_open = bdb_db_open;
763 bi->bi_db_close = bdb_db_close;
764 bi->bi_db_destroy = bdb_db_destroy;
766 bi->bi_op_add = bdb_add;
767 bi->bi_op_bind = bdb_bind;
768 bi->bi_op_compare = bdb_compare;
769 bi->bi_op_delete = bdb_delete;
770 bi->bi_op_modify = bdb_modify;
771 bi->bi_op_modrdn = bdb_modrdn;
772 bi->bi_op_search = bdb_search;
774 bi->bi_op_unbind = 0;
776 bi->bi_extended = bdb_extended;
778 bi->bi_chk_referrals = bdb_referrals;
779 bi->bi_operational = bdb_operational;
780 bi->bi_has_subordinates = bdb_hasSubordinates;
781 bi->bi_entry_release_rw = bdb_entry_release;
782 bi->bi_entry_get_rw = bdb_entry_get;
785 * hooks for slap tools
787 bi->bi_tool_entry_open = bdb_tool_entry_open;
788 bi->bi_tool_entry_close = bdb_tool_entry_close;
789 bi->bi_tool_entry_first = bdb_tool_entry_next;
790 bi->bi_tool_entry_next = bdb_tool_entry_next;
791 bi->bi_tool_entry_get = bdb_tool_entry_get;
792 bi->bi_tool_entry_put = bdb_tool_entry_put;
793 bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
794 bi->bi_tool_sync = 0;
795 bi->bi_tool_dn2id_get = bdb_tool_dn2id_get;
796 bi->bi_tool_entry_modify = bdb_tool_entry_modify;
798 bi->bi_connection_init = 0;
799 bi->bi_connection_destroy = 0;
801 rc = bdb_back_init_cf( bi );
803 return rc;
806 #if (SLAPD_BDB == SLAPD_MOD_DYNAMIC && !defined(BDB_HIER)) || \
807 (SLAPD_HDB == SLAPD_MOD_DYNAMIC && defined(BDB_HIER))
809 /* conditionally define the init_module() function */
810 #ifdef BDB_HIER
811 SLAP_BACKEND_INIT_MODULE( hdb )
812 #else /* !BDB_HIER */
813 SLAP_BACKEND_INIT_MODULE( bdb )
814 #endif /* !BDB_HIER */
816 #endif /* SLAPD_[BH]DB == SLAPD_MOD_DYNAMIC */