]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/init.c
ITS#3824 remove env after slapadd/slapindex -q
[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         bdb->bi_flags &= ~BDB_IS_OPEN;
547
548         ber_bvarray_free( bdb->bi_db_config );
549         bdb->bi_db_config = NULL;
550
551         while( bdb->bi_databases && bdb->bi_ndatabases-- ) {
552                 db = bdb->bi_databases[bdb->bi_ndatabases];
553                 rc = db->bdi_db->close( db->bdi_db, 0 );
554                 /* Lower numbered names are not strdup'd */
555                 if( bdb->bi_ndatabases >= BDB_NDB )
556                         free( db->bdi_name );
557                 free( db );
558         }
559         free( bdb->bi_databases );
560         bdb->bi_databases = NULL;
561
562         bdb_cache_release_all (&bdb->bi_cache);
563
564         if ( bdb->bi_idl_cache_max_size ) {
565                 ldap_pvt_thread_rdwr_wlock ( &bdb->bi_idl_tree_rwlock );
566                 avl_free( bdb->bi_idl_tree, NULL );
567                 bdb->bi_idl_tree = NULL;
568                 entry = bdb->bi_idl_lru_head;
569                 while ( entry != NULL ) {
570                         next_entry = entry->idl_lru_next;
571                         if ( entry->idl )
572                                 free( entry->idl );
573                         free( entry->kstr.bv_val );
574                         free( entry );
575                         entry = next_entry;
576                 }
577                 bdb->bi_idl_lru_head = bdb->bi_idl_lru_tail = NULL;
578                 ldap_pvt_thread_rdwr_wunlock ( &bdb->bi_idl_tree_rwlock );
579         }
580
581         if ( !( slapMode & SLAP_TOOL_QUICK ) && bdb->bi_dbenv ) {
582                 XLOCK_ID_FREE(bdb->bi_dbenv, bdb->bi_cache.c_locker);
583                 bdb->bi_cache.c_locker = 0;
584         }
585
586         /* close db environment */
587         if( bdb->bi_dbenv ) {
588                 /* force a checkpoint, but not if we were ReadOnly,
589                  * and not in Quick mode since there are no transactions there.
590                  */
591                 if ( !( slapMode & ( SLAP_TOOL_QUICK|SLAP_TOOL_READONLY ))) {
592                         rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
593                         if( rc != 0 ) {
594                                 Debug( LDAP_DEBUG_ANY,
595                                         "bdb_db_close: txn_checkpoint failed: %s (%d)\n",
596                                         db_strerror(rc), rc, 0 );
597                         }
598                 }
599
600                 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
601                 bdb->bi_dbenv = NULL;
602                 if( rc != 0 ) {
603                         Debug( LDAP_DEBUG_ANY,
604                                 "bdb_db_close: close failed: %s (%d)\n",
605                                 db_strerror(rc), rc, 0 );
606                         return rc;
607                 }
608
609 #if DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR == 2
610                 /* Delete the environment if we were in quick mode. This
611                  * works around a bug in bdb4.2 that interferes with the
612                  * operation of db_stat and other tools after a slapadd -q
613                  * or slapindex -q has taken place.
614                  */
615                 if( slapMode & SLAP_TOOL_QUICK ) {
616                         rc = db_env_create( &bdb->bi_dbenv, 0 );
617                         if( rc != 0 ) {
618                                 Debug( LDAP_DEBUG_ANY,
619                                         "bdb_db_close: db_env_create failed: %s (%d)\n",
620                                         db_strerror(rc), rc, 0 );
621                                 return rc;
622                         }
623                         rc = bdb->bi_dbenv->remove(bdb->bi_dbenv, bdb->bi_dbenv_home,
624                                         DB_FORCE);
625                         bdb->bi_dbenv = NULL;
626                         if( rc != 0 ) {
627                                 Debug( LDAP_DEBUG_ANY,
628                                         "bdb_db_close: dbenv_remove failed: %s (%d)\n",
629                                         db_strerror(rc), rc, 0 );
630                                 return rc;
631                         }
632                 }
633 #endif
634         }
635
636         rc = alock_close( &bdb->bi_alock_info );
637         if( rc != 0 ) {
638                 Debug( LDAP_DEBUG_ANY,
639                         "bdb_db_close: alock_close failed\n", 0, 0, 0 );
640                 return -1;
641         }
642
643         return 0;
644 }
645
646 static int
647 bdb_db_destroy( BackendDB *be )
648 {
649         int rc;
650         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
651
652         if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
653         if( bdb->bi_db_config_path ) ch_free( bdb->bi_db_config_path );
654
655         bdb_attr_index_destroy( bdb->bi_attrs );
656
657         ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
658         ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_mutex );
659         ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_dntree.bei_kids_mutex );
660         ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
661         ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
662         ldap_pvt_thread_rdwr_destroy( &bdb->bi_idl_tree_rwlock );
663         ldap_pvt_thread_mutex_destroy( &bdb->bi_idl_tree_lrulock );
664
665         ch_free( bdb );
666         be->be_private = NULL;
667
668         return 0;
669 }
670
671 int
672 bdb_back_initialize(
673         BackendInfo     *bi )
674 {
675         int rc;
676
677         static char *controls[] = {
678                 LDAP_CONTROL_ASSERT,
679                 LDAP_CONTROL_MANAGEDSAIT,
680                 LDAP_CONTROL_NOOP,
681                 LDAP_CONTROL_PAGEDRESULTS,
682 #ifdef LDAP_CONTROL_SUBENTRIES
683                 LDAP_CONTROL_SUBENTRIES,
684 #endif
685 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
686                 LDAP_CONTROL_X_PERMISSIVE_MODIFY,
687 #endif
688                 NULL
689         };
690
691         /* initialize the underlying database system */
692         Debug( LDAP_DEBUG_TRACE,
693                 LDAP_XSTRING(bdb_back_initialize) ": initialize " 
694                 BDB_UCTYPE " backend\n", 0, 0, 0 );
695
696         bi->bi_flags |=
697                 SLAP_BFLAG_INCREMENT |
698 #ifdef BDB_SUBENTRIES
699                 SLAP_BFLAG_SUBENTRIES |
700 #endif
701                 SLAP_BFLAG_ALIASES |
702                 SLAP_BFLAG_REFERRALS;
703
704         bi->bi_controls = controls;
705
706         {       /* version check */
707                 int major, minor, patch, ver;
708                 char *version = db_version( &major, &minor, &patch );
709 #ifdef HAVE_EBCDIC
710                 char v2[1024];
711
712                 /* All our stdio does an ASCII to EBCDIC conversion on
713                  * the output. Strings from the BDB library are already
714                  * in EBCDIC; we have to go back and forth...
715                  */
716                 strcpy( v2, version );
717                 __etoa( v2 );
718                 version = v2;
719 #endif
720
721                 ver = (major << 24) | (minor << 16) | patch;
722                 if( ver != DB_VERSION_FULL ) {
723                         /* fail if a versions don't match */
724                         Debug( LDAP_DEBUG_ANY,
725                                 LDAP_XSTRING(bdb_back_initialize) ": "
726                                 "BDB library version mismatch:"
727                                 " expected " DB_VERSION_STRING ","
728                                 " got %s\n", version, 0, 0 );
729                         return -1;
730                 }
731
732                 Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(bdb_back_initialize)
733                         ": %s\n", version, 0, 0 );
734         }
735
736         db_env_set_func_free( ber_memfree );
737         db_env_set_func_malloc( (db_malloc *)ber_memalloc );
738         db_env_set_func_realloc( (db_realloc *)ber_memrealloc );
739 #ifndef NO_THREAD
740         /* This is a no-op on a NO_THREAD build. Leave the default
741          * alone so that BDB will sleep on interprocess conflicts.
742          */
743         db_env_set_func_yield( ldap_pvt_thread_yield );
744 #endif
745
746         bi->bi_open = 0;
747         bi->bi_close = 0;
748         bi->bi_config = 0;
749         bi->bi_destroy = 0;
750
751         bi->bi_db_init = bdb_db_init;
752         bi->bi_db_config = config_generic_wrapper;
753         bi->bi_db_open = bdb_db_open;
754         bi->bi_db_close = bdb_db_close;
755         bi->bi_db_destroy = bdb_db_destroy;
756
757         bi->bi_op_add = bdb_add;
758         bi->bi_op_bind = bdb_bind;
759         bi->bi_op_compare = bdb_compare;
760         bi->bi_op_delete = bdb_delete;
761         bi->bi_op_modify = bdb_modify;
762         bi->bi_op_modrdn = bdb_modrdn;
763         bi->bi_op_search = bdb_search;
764
765         bi->bi_op_unbind = 0;
766
767         bi->bi_extended = bdb_extended;
768
769         bi->bi_chk_referrals = bdb_referrals;
770         bi->bi_operational = bdb_operational;
771         bi->bi_has_subordinates = bdb_hasSubordinates;
772         bi->bi_entry_release_rw = bdb_entry_release;
773         bi->bi_entry_get_rw = bdb_entry_get;
774
775         /*
776          * hooks for slap tools
777          */
778         bi->bi_tool_entry_open = bdb_tool_entry_open;
779         bi->bi_tool_entry_close = bdb_tool_entry_close;
780         bi->bi_tool_entry_first = bdb_tool_entry_next;
781         bi->bi_tool_entry_next = bdb_tool_entry_next;
782         bi->bi_tool_entry_get = bdb_tool_entry_get;
783         bi->bi_tool_entry_put = bdb_tool_entry_put;
784         bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
785         bi->bi_tool_sync = 0;
786         bi->bi_tool_dn2id_get = bdb_tool_dn2id_get;
787         bi->bi_tool_id2entry_get = bdb_tool_id2entry_get;
788         bi->bi_tool_entry_modify = bdb_tool_entry_modify;
789
790         bi->bi_connection_init = 0;
791         bi->bi_connection_destroy = 0;
792
793         rc = bdb_back_init_cf( bi );
794
795         return rc;
796 }
797
798 #if     (SLAPD_BDB == SLAPD_MOD_DYNAMIC && !defined(BDB_HIER)) || \
799         (SLAPD_HDB == SLAPD_MOD_DYNAMIC && defined(BDB_HIER))
800
801 /* conditionally define the init_module() function */
802 #ifdef BDB_HIER
803 SLAP_BACKEND_INIT_MODULE( hdb )
804 #else /* !BDB_HIER */
805 SLAP_BACKEND_INIT_MODULE( bdb )
806 #endif /* !BDB_HIER */
807
808 #endif /* SLAPD_[BH]DB == SLAPD_MOD_DYNAMIC */
809