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