]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/init.c
df3221e9fce12c3888188a366dd63239a058612f
[openldap] / servers / slapd / back-bdb / init.c
1 /* init.c - initialize bdb backend */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2000-2005 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
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.
11  *
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>.
15  */
16
17 #include "portable.h"
18
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 #ifdef HAVE_UTIME_H
26 #  ifdef HAVE_SYS_TYPES_H
27 #    include <sys/types.h>
28 #  endif
29 #  include <utime.h>
30 #endif /* HAVE_UTIME_H */
31 #include "back-bdb.h"
32 #include <lutil.h>
33 #include <ldap_rq.h>
34 #include "alock.h"
35
36 static const struct bdbi_database {
37         char *file;
38         char *name;
39         int type;
40         int flags;
41 } bdbi_databases[] = {
42         { "id2entry" BDB_SUFFIX, "id2entry", DB_BTREE, 0 },
43         { "dn2id" BDB_SUFFIX, "dn2id", DB_BTREE, 0 },
44         { NULL, NULL, 0, 0 }
45 };
46
47 typedef void * db_malloc(size_t);
48 typedef void * db_realloc(void *, size_t);
49
50 static int
51 bdb_db_init( BackendDB *be )
52 {
53         struct bdb_info *bdb;
54
55         Debug( LDAP_DEBUG_TRACE,
56                 LDAP_XSTRING(bdb_db_init) ": Initializing " BDB_UCTYPE " database\n",
57                 0, 0, 0 );
58
59         /* allocate backend-database-specific stuff */
60         bdb = (struct bdb_info *) ch_calloc( 1, sizeof(struct bdb_info) );
61
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;
66
67         bdb->bi_cache.c_maxsize = DEFAULT_CACHE_SIZE;
68
69         bdb->bi_lock_detect = DB_LOCK_DEFAULT;
70         bdb->bi_search_stack_depth = DEFAULT_SEARCH_STACK_DEPTH;
71         bdb->bi_search_stack = NULL;
72
73         ldap_pvt_thread_mutex_init( &bdb->bi_database_mutex );
74         ldap_pvt_thread_mutex_init( &bdb->bi_lastid_mutex );
75 #ifdef BDB_HIER
76         ldap_pvt_thread_mutex_init( &bdb->bi_modrdns_mutex );
77 #endif
78         ldap_pvt_thread_mutex_init( &bdb->bi_cache.lru_mutex );
79         ldap_pvt_thread_mutex_init( &bdb->bi_cache.c_dntree.bei_kids_mutex );
80         ldap_pvt_thread_rdwr_init ( &bdb->bi_cache.c_rwlock );
81         ldap_pvt_thread_rdwr_init( &bdb->bi_idl_tree_rwlock );
82         ldap_pvt_thread_mutex_init( &bdb->bi_idl_tree_lrulock );
83
84         be->be_private = bdb;
85         be->be_cf_ocs = be->bd_info->bi_cf_ocs;
86
87         return 0;
88 }
89
90 static int
91 bdb_db_open( BackendDB *be )
92 {
93         int rc, i;
94         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
95         struct stat stat1, stat2;
96         u_int32_t flags;
97         char path[MAXPATHLEN];
98         char *dbhome;
99         int do_recover = 0, do_alock_recover = 0, open_env = 1, got_env = 0;
100
101         if ( be->be_suffix == NULL ) {
102                 Debug( LDAP_DEBUG_ANY,
103                         "bdb_db_open: need suffix\n",
104                         0, 0, 0 );
105                 return -1;
106         }
107
108         Debug( LDAP_DEBUG_ARGS,
109                 "bdb_db_open: %s\n",
110                 be->be_suffix[0].bv_val, 0, 0 );
111
112 #ifndef BDB_MULTIPLE_SUFFIXES
113         if ( be->be_suffix[1].bv_val ) {
114         Debug( LDAP_DEBUG_ANY,
115                 "bdb_db_open: only one suffix allowed\n", 0, 0, 0 );
116                 return -1;
117         }
118 #endif
119
120         /* Check existence of dbenv_home. Any error means trouble */
121         rc = stat( bdb->bi_dbenv_home, &stat1 );
122         if( rc !=0 ) {
123                 Debug( LDAP_DEBUG_ANY,
124                         "bdb_db_open: Cannot access database directory %s (%d)\n",
125                         bdb->bi_dbenv_home, errno, 0 );
126                         return -1;
127         }
128
129         /* Perform database use arbitration/recovery logic */
130         rc = alock_open( &bdb->bi_alock_info, 
131                                 "slapd", 
132                                 bdb->bi_dbenv_home,
133                                 slapMode & SLAP_TOOL_READONLY ?
134                                 ALOCK_LOCKED : ALOCK_UNIQUE );
135
136         if( rc == ALOCK_RECOVER ) {
137                 Debug( LDAP_DEBUG_ANY,
138                         "bdb_db_open: unclean shutdown detected;"
139                         " attempting recovery.\n", 
140                         0, 0, 0 );
141                 do_alock_recover = 1;
142                 do_recover = 1;
143         } else if( rc == ALOCK_BUSY ) {
144                 Debug( LDAP_DEBUG_ANY,
145                         "bdb_db_open: database already in use\n", 
146                         0, 0, 0 );
147                 return -1;
148         } else if( rc != ALOCK_CLEAN ) {
149                 Debug( LDAP_DEBUG_ANY,
150                         "bdb_db_open: alock package is unstable\n", 
151                         0, 0, 0 );
152                 return -1;
153         }
154
155         /*
156          * The DB_CONFIG file may have changed. If so, recover the
157          * database so that new settings are put into effect. Also
158          * note the possible absence of DB_CONFIG in the log.
159          */
160         if( stat( bdb->bi_db_config_path, &stat1 ) == 0 ) {
161                 if ( !do_recover ) {
162                         char *ptr = lutil_strcopy(path, bdb->bi_dbenv_home);
163                         *ptr++ = LDAP_DIRSEP[0];
164                         strcpy( ptr, "__db.001" );
165                         if( stat( path, &stat2 ) == 0 ) {
166                                 if( stat2.st_mtime <= stat1.st_mtime ) {
167                                         Debug( LDAP_DEBUG_ANY,
168                                                 "bdb_db_open: DB_CONFIG for suffix %s has changed.\n"
169                                                 "Performing database recovery to activate new settings.\n",
170                                                 be->be_suffix[0].bv_val, 0, 0 );
171                                         do_recover = 1;
172                                 }
173                         }
174                 }
175         }
176         else {
177                 Debug( LDAP_DEBUG_ANY,
178                         "bdb_db_open: Warning - No DB_CONFIG file found "
179                         "in directory %s: (%d)\n"
180                         "Expect poor performance for suffix %s.\n",
181                         bdb->bi_dbenv_home, errno, be->be_suffix[0].bv_val );
182         }
183
184         rc = db_env_create( &bdb->bi_dbenv, 0 );
185         if( rc != 0 ) {
186                 Debug( LDAP_DEBUG_ANY,
187                         "bdb_db_open: db_env_create failed: %s (%d)\n",
188                         db_strerror(rc), rc, 0 );
189                 return rc;
190         }
191
192         bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
193         bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
194
195         bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
196
197         /* One long-lived TXN per thread, two TXNs per write op */
198         bdb->bi_dbenv->set_tx_max( bdb->bi_dbenv, connection_pool_max * 3 );
199
200         if( bdb->bi_dbenv_xflags != 0 ) {
201                 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
202                         bdb->bi_dbenv_xflags, 1);
203                 if( rc != 0 ) {
204                         Debug( LDAP_DEBUG_ANY,
205                                 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
206                                 db_strerror(rc), rc, 0 );
207                         return rc;
208                 }
209         }
210
211 #define BDB_TXN_FLAGS   (DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN)
212
213 #ifdef HAVE_EBCDIC
214         strcpy( path, bdb->bi_dbenv_home );
215         __atoe( path );
216         dbhome = path;
217 #else
218         dbhome = bdb->bi_dbenv_home;
219 #endif
220
221         Debug( LDAP_DEBUG_TRACE,
222                 "bdb_db_open: dbenv_open(%s)\n",
223                 bdb->bi_dbenv_home, 0, 0);
224
225         /* Check if there is a usable existing environment */
226         flags = DB_JOINENV | DB_THREAD;
227
228         rc = bdb->bi_dbenv->open( bdb->bi_dbenv, dbhome,
229                 flags, bdb->bi_dbenv_mode );
230         if( rc == 0 ) {
231                 int flags_ok = 0;
232
233                 got_env = 1;
234
235                 rc = bdb->bi_dbenv->get_open_flags( bdb->bi_dbenv, &flags );
236                 if ( rc == 0 ) {
237                         int flag2 = flags & BDB_TXN_FLAGS;
238
239                         /* In quick mode, none of these flags are allowed */
240                         if ( slapMode & SLAP_TOOL_QUICK ) {
241                                 if ( !flag2 )
242                                         flags_ok = 1;
243                         } else {
244                         /* In normal mode, all of these flags are required */
245                                 if ( flag2 == BDB_TXN_FLAGS )
246                                         flags_ok = 1;
247                         }
248                 }
249
250                 /* In Quick mode, we cannot Recover... */
251                 if ( slapMode & SLAP_TOOL_QUICK ) {
252                         /* If we need to recover but we had no TXNs, just fail */
253                         if ( do_recover && flags_ok ) {
254                                 Debug( LDAP_DEBUG_ANY,
255                                         "bdb_db_open: Database cannot be recovered. "
256                                         "Restore from backup!\n", 0, 0, 0);
257                                 return -1;
258                         }
259                         /* We need to recover, and we had TXN support before:
260                          * Close this env, open a new one with recovery flags.
261                          */
262                         if ( do_recover ) {
263                                 bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
264                                 bdb->bi_dbenv = NULL;
265                                 rc = db_env_create( &bdb->bi_dbenv, 0 );
266                                 if( rc != 0 ) {
267                                         Debug( LDAP_DEBUG_ANY,
268                                                 "bdb_db_open: db_env_create failed: %s (%d)\n",
269                                                 db_strerror(rc), rc, 0 );
270                                         return rc;
271                                 }
272                                 bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv,
273                                         be->be_suffix[0].bv_val );
274                                 bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
275                                 rc = bdb->bi_dbenv->open( bdb->bi_dbenv, dbhome,
276                                         flags | DB_RECOVER, bdb->bi_dbenv_mode );
277                                 if( rc != 0 ) {
278                                         Debug( LDAP_DEBUG_ANY,
279                                                 "bdb_db_open: recovery failed: %s (%d)\n",
280                                                 db_strerror(rc), rc, 0 );
281                                         return rc;
282                                 }
283                                 do_recover = 0;
284                         }
285                         /* Prev environment had TXN support, get rid of it */
286                         if ( !flags_ok ) {
287                                 bdb->bi_dbenv->remove( bdb->bi_dbenv, dbhome, 0 );
288                                 bdb->bi_dbenv = NULL;
289                         }
290                 /* Normal TXN mode */
291                 } else {
292                         /* If we need to recover but we had no TXNs, just fail */
293                         if ( do_recover && !flags_ok ) {
294                                 Debug( LDAP_DEBUG_ANY,
295                                         "bdb_db_open: Database cannot be recovered. "
296                                         "Restore from backup!\n", 0, 0, 0);
297                                 return -1;
298                         }
299                         /* Prev environment had no TXN support, close it */
300                         if ( !flags_ok ) {
301                                 bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
302                                 bdb->bi_dbenv = NULL;
303                                 do_recover = 1;
304                         }
305                 }
306
307                 if ( flags_ok && !do_recover ) {
308                         /* This environment is fine, don't reopen it */
309                         open_env = 0;
310                 } else {
311                         /* Create a new env that can take the desired settings */
312                         rc = db_env_create( &bdb->bi_dbenv, 0 );
313                         if( rc != 0 ) {
314                                 Debug( LDAP_DEBUG_ANY,
315                                         "bdb_db_open: db_env_create failed: %s (%d)\n",
316                                         db_strerror(rc), rc, 0 );
317                                 return rc;
318                         }
319
320                         bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
321                         bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
322                         bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
323
324                         /* One long-lived TXN per thread, two TXNs per write op */
325                         bdb->bi_dbenv->set_tx_max( bdb->bi_dbenv, connection_pool_max * 3 );
326
327                         if( bdb->bi_dbenv_xflags != 0 ) {
328                                 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
329                                         bdb->bi_dbenv_xflags, 1);
330                                 if( rc != 0 ) {
331                                         Debug( LDAP_DEBUG_ANY,
332                                                 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
333                                                 db_strerror(rc), rc, 0 );
334                                         return rc;
335                                 }
336                         }
337                 }
338         }
339
340         /* If we need to recover but there was no existing environment,
341          * then we assume that someone has already manually recovered using
342          * db_recover. Just ignore it.
343          */
344         if ( do_recover && !got_env ) {
345                 do_recover = 0;
346                 Debug( LDAP_DEBUG_TRACE,
347                         "bdb_db_open: Recovery needed but environment is missing - "
348                         "assuming recovery was done manually...\n", 0, 0, 0 );
349         }
350
351         if ( open_env ) {
352                 flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE;
353                 if ( !( slapMode & SLAP_TOOL_QUICK ))
354                         flags |= BDB_TXN_FLAGS;
355
356                 if ( do_recover )
357                         flags |= DB_RECOVER;
358
359                 /* If a key was set, use shared memory for the BDB environment */
360                 if ( bdb->bi_shm_key ) {
361                         bdb->bi_dbenv->set_shm_key( bdb->bi_dbenv, bdb->bi_shm_key );
362                         flags |= DB_SYSTEM_MEM;
363                 }
364
365                 rc = bdb->bi_dbenv->open( bdb->bi_dbenv, dbhome,
366                         flags, bdb->bi_dbenv_mode );
367                 if( rc != 0 ) {
368                         Debug( LDAP_DEBUG_ANY,
369                                 "bdb_db_open: dbenv_open failed: %s (%d)\n",
370                                 db_strerror(rc), rc, 0 );
371                         return rc;
372                 }
373         }
374
375         if ( do_alock_recover && alock_recover (&bdb->bi_alock_info) != 0 ) {
376                 Debug( LDAP_DEBUG_ANY,
377                         "bdb_db_open: alock_recover failed\n",
378                         0, 0, 0 );
379                 return -1;
380         }
381
382 #ifdef SLAP_ZONE_ALLOC
383         if ( bdb->bi_cache.c_maxsize ) {
384                 bdb->bi_cache.c_zctx = slap_zn_mem_create(
385                                                                 SLAP_ZONE_INITSIZE,
386                                                                 SLAP_ZONE_MAXSIZE,
387                                                                 SLAP_ZONE_DELTA,
388                                                                 SLAP_ZONE_SIZE);
389         }
390 #endif
391
392         if ( bdb->bi_idl_cache_max_size ) {
393                 bdb->bi_idl_tree = NULL;
394                 bdb->bi_idl_cache_size = 0;
395         }
396
397         flags = DB_THREAD | bdb->bi_db_opflags;
398
399 #ifdef DB_AUTO_COMMIT
400         if ( !( slapMode & SLAP_TOOL_QUICK ))
401                 flags |= DB_AUTO_COMMIT;
402 #endif
403
404         bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
405                 BDB_INDICES * sizeof(struct bdb_db_info *) );
406
407         /* open (and create) main database */
408         for( i = 0; bdbi_databases[i].name; i++ ) {
409                 struct bdb_db_info *db;
410
411                 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
412
413                 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
414                 if( rc != 0 ) {
415                         Debug( LDAP_DEBUG_ANY,
416                                 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
417                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
418                         return rc;
419                 }
420
421                 if( i == BDB_ID2ENTRY ) {
422                         rc = db->bdi_db->set_pagesize( db->bdi_db,
423                                 BDB_ID2ENTRY_PAGESIZE );
424                         if ( slapMode & SLAP_TOOL_READMAIN ) {
425                                 flags |= DB_RDONLY;
426                         } else {
427                                 flags |= DB_CREATE;
428                         }
429                 } else {
430                         rc = db->bdi_db->set_flags( db->bdi_db, 
431                                 DB_DUP | DB_DUPSORT );
432 #ifndef BDB_HIER
433                         if ( slapMode & SLAP_TOOL_READONLY ) {
434                                 flags |= DB_RDONLY;
435                         } else {
436                                 flags |= DB_CREATE;
437                         }
438 #else
439                         if ( slapMode & (SLAP_TOOL_READONLY|SLAP_TOOL_READMAIN) ) {
440                                 flags |= DB_RDONLY;
441                         } else {
442                                 flags |= DB_CREATE;
443                         }
444 #endif
445                         rc = db->bdi_db->set_pagesize( db->bdi_db,
446                                 BDB_PAGESIZE );
447                 }
448
449 #ifdef HAVE_EBCDIC
450                 strcpy( path, bdbi_databases[i].file );
451                 __atoe( path );
452                 rc = DB_OPEN( db->bdi_db,
453                         path,
454                 /*      bdbi_databases[i].name, */ NULL,
455                         bdbi_databases[i].type,
456                         bdbi_databases[i].flags | flags,
457                         bdb->bi_dbenv_mode );
458 #else
459                 rc = DB_OPEN( db->bdi_db,
460                         bdbi_databases[i].file,
461                 /*      bdbi_databases[i].name, */ NULL,
462                         bdbi_databases[i].type,
463                         bdbi_databases[i].flags | flags,
464                         bdb->bi_dbenv_mode );
465 #endif
466
467                 if ( rc != 0 ) {
468                         char    buf[SLAP_TEXT_BUFLEN];
469
470                         snprintf( buf, sizeof(buf), "%s/%s", 
471                                 bdb->bi_dbenv_home, bdbi_databases[i].file );
472                         Debug( LDAP_DEBUG_ANY,
473                                 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
474                                 buf, db_strerror(rc), rc );
475                         return rc;
476                 }
477
478 #if 0
479                 if( i == BDB_ID2ENTRY && ( slapMode & SLAP_TOOL_MODE )) {
480                         db->bdi_db->mpf->set_priority( db->bdi_db->mpf,
481                                 DB_PRIORITY_VERY_LOW );
482                 }
483 #endif
484
485                 flags &= ~(DB_CREATE | DB_RDONLY);
486                 db->bdi_name = bdbi_databases[i].name;
487                 bdb->bi_databases[i] = db;
488         }
489
490         bdb->bi_databases[i] = NULL;
491         bdb->bi_ndatabases = i;
492
493         /* get nextid */
494         rc = bdb_last_id( be, NULL );
495         if( rc != 0 ) {
496                 Debug( LDAP_DEBUG_ANY,
497                         "bdb_db_open: last_id(%s) failed: %s (%d)\n",
498                         bdb->bi_dbenv_home, db_strerror(rc), rc );
499                 return rc;
500         }
501
502         if ( !( slapMode & SLAP_TOOL_QUICK )) {
503                 XLOCK_ID(bdb->bi_dbenv, &bdb->bi_cache.c_locker);
504         }
505
506         bdb->bi_flags |= BDB_IS_OPEN;
507
508         return 0;
509 }
510
511 static int
512 bdb_db_close( BackendDB *be )
513 {
514         int rc;
515         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
516         struct bdb_db_info *db;
517         bdb_idl_cache_entry_t *entry, *next_entry;
518
519         /* backend_shutdown closes everything, even if not all were opened */
520         if ( !( bdb->bi_flags & BDB_IS_OPEN ))
521                 return 0;
522
523         bdb->bi_flags &= ~BDB_IS_OPEN;
524
525         ber_bvarray_free( bdb->bi_db_config );
526         bdb->bi_db_config = NULL;
527
528         while( bdb->bi_databases && bdb->bi_ndatabases-- ) {
529                 db = bdb->bi_databases[bdb->bi_ndatabases];
530                 rc = db->bdi_db->close( db->bdi_db, 0 );
531                 /* Lower numbered names are not strdup'd */
532                 if( bdb->bi_ndatabases >= BDB_NDB )
533                         free( db->bdi_name );
534                 free( db );
535         }
536         free( bdb->bi_databases );
537         bdb->bi_databases = NULL;
538
539         bdb_cache_release_all (&bdb->bi_cache);
540
541         if ( bdb->bi_idl_cache_max_size ) {
542                 ldap_pvt_thread_rdwr_wlock ( &bdb->bi_idl_tree_rwlock );
543                 avl_free( bdb->bi_idl_tree, NULL );
544                 bdb->bi_idl_tree = NULL;
545                 entry = bdb->bi_idl_lru_head;
546                 while ( entry != NULL ) {
547                         next_entry = entry->idl_lru_next;
548                         if ( entry->idl )
549                                 free( entry->idl );
550                         free( entry->kstr.bv_val );
551                         free( entry );
552                         entry = next_entry;
553                 }
554                 bdb->bi_idl_lru_head = bdb->bi_idl_lru_tail = NULL;
555                 ldap_pvt_thread_rdwr_wunlock ( &bdb->bi_idl_tree_rwlock );
556         }
557
558         if ( !( slapMode & SLAP_TOOL_QUICK ) && bdb->bi_dbenv ) {
559                 XLOCK_ID_FREE(bdb->bi_dbenv, bdb->bi_cache.c_locker);
560                 bdb->bi_cache.c_locker = 0;
561         }
562
563         /* close db environment */
564         if( bdb->bi_dbenv ) {
565                 /* force a checkpoint, but not if we were ReadOnly,
566                  * and not in Quick mode since there are no transactions there.
567                  */
568                 if ( !( slapMode & ( SLAP_TOOL_QUICK|SLAP_TOOL_READONLY ))) {
569                         rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
570                         if( rc != 0 ) {
571                                 Debug( LDAP_DEBUG_ANY,
572                                         "bdb_db_close: txn_checkpoint failed: %s (%d)\n",
573                                         db_strerror(rc), rc, 0 );
574                         }
575                 }
576
577                 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
578                 bdb->bi_dbenv = NULL;
579                 if( rc != 0 ) {
580                         Debug( LDAP_DEBUG_ANY,
581                                 "bdb_db_close: close failed: %s (%d)\n",
582                                 db_strerror(rc), rc, 0 );
583                         return rc;
584                 }
585         }
586
587         rc = alock_close( &bdb->bi_alock_info );
588         if( rc != 0 ) {
589                 Debug( LDAP_DEBUG_ANY,
590                         "bdb_db_close: alock_close failed\n", 0, 0, 0 );
591                 return -1;
592         }
593
594         return 0;
595 }
596
597 static int
598 bdb_db_destroy( BackendDB *be )
599 {
600         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
601
602         if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
603         if( bdb->bi_db_config_path ) ch_free( bdb->bi_db_config_path );
604
605         bdb_attr_index_destroy( bdb->bi_attrs );
606
607         ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
608         ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_mutex );
609         ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_dntree.bei_kids_mutex );
610 #ifdef BDB_HIER
611         ldap_pvt_thread_mutex_destroy( &bdb->bi_modrdns_mutex );
612 #endif
613         ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
614         ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
615         ldap_pvt_thread_rdwr_destroy( &bdb->bi_idl_tree_rwlock );
616         ldap_pvt_thread_mutex_destroy( &bdb->bi_idl_tree_lrulock );
617
618         ch_free( bdb );
619         be->be_private = NULL;
620
621         return 0;
622 }
623
624 int
625 bdb_back_initialize(
626         BackendInfo     *bi )
627 {
628         int rc;
629
630         static char *controls[] = {
631                 LDAP_CONTROL_ASSERT,
632                 LDAP_CONTROL_MANAGEDSAIT,
633                 LDAP_CONTROL_NOOP,
634                 LDAP_CONTROL_PAGEDRESULTS,
635 #ifdef LDAP_CONTROL_SUBENTRIES
636                 LDAP_CONTROL_SUBENTRIES,
637 #endif
638 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
639                 LDAP_CONTROL_X_PERMISSIVE_MODIFY,
640 #endif
641                 NULL
642         };
643
644         /* initialize the underlying database system */
645         Debug( LDAP_DEBUG_TRACE,
646                 LDAP_XSTRING(bdb_back_initialize) ": initialize " 
647                 BDB_UCTYPE " backend\n", 0, 0, 0 );
648
649         bi->bi_flags |=
650                 SLAP_BFLAG_INCREMENT |
651 #ifdef BDB_SUBENTRIES
652                 SLAP_BFLAG_SUBENTRIES |
653 #endif
654                 SLAP_BFLAG_ALIASES |
655                 SLAP_BFLAG_REFERRALS;
656
657         bi->bi_controls = controls;
658
659         {       /* version check */
660                 int major, minor, patch, ver;
661                 char *version = db_version( &major, &minor, &patch );
662 #ifdef HAVE_EBCDIC
663                 char v2[1024];
664
665                 /* All our stdio does an ASCII to EBCDIC conversion on
666                  * the output. Strings from the BDB library are already
667                  * in EBCDIC; we have to go back and forth...
668                  */
669                 strcpy( v2, version );
670                 __etoa( v2 );
671                 version = v2;
672 #endif
673
674                 ver = (major << 24) | (minor << 16) | patch;
675                 if( ver != DB_VERSION_FULL ) {
676                         /* fail if a versions don't match */
677                         Debug( LDAP_DEBUG_ANY,
678                                 LDAP_XSTRING(bdb_back_initialize) ": "
679                                 "BDB library version mismatch:"
680                                 " expected " DB_VERSION_STRING ","
681                                 " got %s\n", version, 0, 0 );
682                         return -1;
683                 }
684
685                 Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(bdb_back_initialize)
686                         ": %s\n", version, 0, 0 );
687         }
688
689         db_env_set_func_free( ber_memfree );
690         db_env_set_func_malloc( (db_malloc *)ber_memalloc );
691         db_env_set_func_realloc( (db_realloc *)ber_memrealloc );
692 #ifndef NO_THREAD
693         /* This is a no-op on a NO_THREAD build. Leave the default
694          * alone so that BDB will sleep on interprocess conflicts.
695          */
696         db_env_set_func_yield( ldap_pvt_thread_yield );
697 #endif
698
699         bi->bi_open = 0;
700         bi->bi_close = 0;
701         bi->bi_config = 0;
702         bi->bi_destroy = 0;
703
704         bi->bi_db_init = bdb_db_init;
705         bi->bi_db_config = config_generic_wrapper;
706         bi->bi_db_open = bdb_db_open;
707         bi->bi_db_close = bdb_db_close;
708         bi->bi_db_destroy = bdb_db_destroy;
709
710         bi->bi_op_add = bdb_add;
711         bi->bi_op_bind = bdb_bind;
712         bi->bi_op_compare = bdb_compare;
713         bi->bi_op_delete = bdb_delete;
714         bi->bi_op_modify = bdb_modify;
715         bi->bi_op_modrdn = bdb_modrdn;
716         bi->bi_op_search = bdb_search;
717
718         bi->bi_op_unbind = 0;
719
720         bi->bi_extended = bdb_extended;
721
722         bi->bi_chk_referrals = bdb_referrals;
723         bi->bi_operational = bdb_operational;
724         bi->bi_has_subordinates = bdb_hasSubordinates;
725         bi->bi_entry_release_rw = bdb_entry_release;
726         bi->bi_entry_get_rw = bdb_entry_get;
727
728         /*
729          * hooks for slap tools
730          */
731         bi->bi_tool_entry_open = bdb_tool_entry_open;
732         bi->bi_tool_entry_close = bdb_tool_entry_close;
733         bi->bi_tool_entry_first = bdb_tool_entry_next;
734         bi->bi_tool_entry_next = bdb_tool_entry_next;
735         bi->bi_tool_entry_get = bdb_tool_entry_get;
736         bi->bi_tool_entry_put = bdb_tool_entry_put;
737         bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
738         bi->bi_tool_sync = 0;
739         bi->bi_tool_dn2id_get = bdb_tool_dn2id_get;
740         bi->bi_tool_id2entry_get = bdb_tool_id2entry_get;
741         bi->bi_tool_entry_modify = bdb_tool_entry_modify;
742
743         bi->bi_connection_init = 0;
744         bi->bi_connection_destroy = 0;
745
746         rc = bdb_back_init_cf( bi );
747
748         return rc;
749 }
750
751 #if     (SLAPD_BDB == SLAPD_MOD_DYNAMIC && !defined(BDB_HIER)) || \
752         (SLAPD_HDB == SLAPD_MOD_DYNAMIC && defined(BDB_HIER))
753
754 /* conditionally define the init_module() function */
755 #ifdef BDB_HIER
756 SLAP_BACKEND_INIT_MODULE( hdb )
757 #else /* !BDB_HIER */
758 SLAP_BACKEND_INIT_MODULE( bdb )
759 #endif /* !BDB_HIER */
760
761 #endif /* SLAPD_[BH]DB == SLAPD_MOD_DYNAMIC */
762