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