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