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