]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/init.c
a7260399ad7379adc175bdc35aa74b3bfc7a305e
[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
73         be->be_private = bdb;
74         be->be_cf_table = be->bd_info->bi_cf_table;
75
76         return 0;
77 }
78
79 static void *
80 bdb_checkpoint( void *ctx, void *arg )
81 {
82         struct re_s *rtask = arg;
83         struct bdb_info *bdb = rtask->arg;
84         
85         TXN_CHECKPOINT( bdb->bi_dbenv, bdb->bi_txn_cp_kbyte,
86                 bdb->bi_txn_cp_min, 0 );
87         ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
88         ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
89         ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
90         return NULL;
91 }
92
93 /*
94  * Unconditionally perform a database recovery. Only works on
95  * databases that were previously opened with transactions and
96  * logs enabled.
97  */
98 static int
99 bdb_do_recovery( BackendDB *be )
100 {
101         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
102         DB_ENV  *re_dbenv;
103         u_int32_t flags;
104         int             rc;
105         char    path[MAXPATHLEN], *ptr;
106
107         /* Create and init the recovery environment */
108         rc = db_env_create( &re_dbenv, 0 );
109         if( rc != 0 ) {
110                 Debug( LDAP_DEBUG_ANY,
111                         "bdb_do_recovery: db_env_create failed: %s (%d)\n",
112                         db_strerror(rc), rc, 0 );
113                 return rc;
114         }
115         re_dbenv->set_errpfx( re_dbenv, be->be_suffix[0].bv_val );
116         re_dbenv->set_errcall( re_dbenv, bdb_errcall );
117         (void)re_dbenv->set_verbose(re_dbenv, DB_VERB_RECOVERY, 1);
118 #if DB_VERSION_FULL < 0x04030000
119         (void)re_dbenv->set_verbose(re_dbenv, DB_VERB_CHKPOINT, 1);
120 #else
121         re_dbenv->set_msgcall( re_dbenv, bdb_msgcall );
122 #endif
123
124         flags = DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL |
125                 DB_INIT_TXN | DB_USE_ENVIRON | DB_RECOVER;
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                 goto re_exit;
219         }
220         else if( rc != 0 ) {
221                 Debug( LDAP_DEBUG_ANY,
222                         "bdb_db_recover: dbenv_open failed: %s (%d)\n",
223                         db_strerror(rc), rc, 0 );
224                 return rc;
225         }
226
227         /*
228          * Check the flags that had been used in the previous open.
229          * The environment needed to have had both
230          * DB_INIT_LOG and DB_INIT_TXN set for us to be willing to
231          * recover the database. Otherwise the an app failed while running
232          * without transactions and logs enabled and the dn2id and id2entry
233          * mapping is likely to be corrupt.
234          */
235         rc = re_dbenv->get_open_flags( re_dbenv, &flags );
236         if( rc != 0 ) {
237                 Debug( LDAP_DEBUG_ANY,
238                         "bdb_db_recover: get_open_flags failed: %s (%d)\n",
239                         db_strerror(rc), rc, 0 );
240                 return rc;
241         }
242
243         (void) re_dbenv->close( re_dbenv, 0 );
244
245         if( (flags & DB_INIT_LOG) && (flags & DB_INIT_TXN) ) {
246                 return bdb_do_recovery( be );
247         }
248
249 re_exit:
250         Debug( LDAP_DEBUG_ANY,
251                 "bdb_db_recover: Database cannot be recovered. "\
252                 "Restore from backup!\n", 0, 0, 0);
253         return -1;
254
255 }
256
257
258 static int
259 bdb_db_open( BackendDB *be )
260 {
261         int rc, i;
262         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
263         struct stat stat1, stat2;
264         u_int32_t flags;
265         char path[MAXPATHLEN];
266         char *ptr;
267
268         Debug( LDAP_DEBUG_ARGS,
269                 "bdb_db_open: %s\n",
270                 be->be_suffix[0].bv_val, 0, 0 );
271
272 #ifndef BDB_MULTIPLE_SUFFIXES
273         if ( be->be_suffix[1].bv_val ) {
274         Debug( LDAP_DEBUG_ANY,
275                 "bdb_db_open: only one suffix allowed\n", 0, 0, 0 );
276                 return -1;
277         }
278 #endif
279
280         /* Check existence of dbenv_home. Any error means trouble */
281         rc = stat( bdb->bi_dbenv_home, &stat1 );
282         if( rc !=0 ) {
283                 Debug( LDAP_DEBUG_ANY,
284                         "bdb_db_open: Cannot access database directory %s (%d)\n",
285                         bdb->bi_dbenv_home, errno, 0 );
286                         return -1;
287         }
288         
289         /* Perform database use arbitration/recovery logic */
290         rc = alock_open( &bdb->bi_alock_info, 
291                                 "slapd", 
292                                 bdb->bi_dbenv_home,
293                                 slapMode & SLAP_TOOL_READONLY ?
294                                 ALOCK_LOCKED : ALOCK_UNIQUE );
295
296         if( rc == ALOCK_RECOVER ) {
297                 Debug( LDAP_DEBUG_ANY,
298                         "bdb_db_open: unclean shutdown detected;"
299                         " attempting recovery.\n", 
300                         0, 0, 0 );
301                 if( bdb_db_recover( be ) != 0 ) {
302                         Debug( LDAP_DEBUG_ANY,
303                                 "bdb_db_open: DB recovery failed.\n",
304                                 0, 0, 0 );
305                         return -1;
306                 }
307                 if( alock_recover (&bdb->bi_alock_info) != 0 ) {
308                         Debug( LDAP_DEBUG_ANY,
309                                 "bdb_db_open: alock_recover failed\n",
310                                 0, 0, 0 );
311                         return -1;
312                 }
313
314         } else if( rc == ALOCK_BUSY ) {
315                 Debug( LDAP_DEBUG_ANY,
316                         "bdb_db_open: database already in use\n", 
317                         0, 0, 0 );
318                 return -1;
319         } else if( rc != ALOCK_CLEAN ) {
320                 Debug( LDAP_DEBUG_ANY,
321                         "bdb_db_open: alock package is unstable\n", 
322                         0, 0, 0 );
323                 return -1;
324         }
325         
326         /*
327          * The DB_CONFIG file may have changed. If so, recover the
328          * database so that new settings are put into effect. Also
329          * note the possible absence of DB_CONFIG in the log.
330          */
331         if( stat( bdb->bi_db_config_path, &stat1 ) == 0 ) {
332                 ptr = lutil_strcopy(path, bdb->bi_dbenv_home);
333                 *ptr++ = LDAP_DIRSEP[0];
334                 strcpy( ptr, bdbi_databases[0].file);
335                 if( stat( path, &stat2 ) == 0 ) {
336                         if( stat2.st_mtime <= stat1.st_mtime ) {
337                                 Debug( LDAP_DEBUG_ANY,
338                                         "bdb_db_open: DB_CONFIG for suffix %s has changed.\n"
339                                         "Performing database recovery to activate new settings.\n",
340                                         be->be_suffix[0].bv_val, 0, 0 );
341                                 if( bdb_do_recovery( be ) != 0) {
342                                         Debug( LDAP_DEBUG_ANY,
343                                                 "bdb_db_open: db recovery failed.\n",
344                                                 0, 0, 0 );
345                                         return -1;
346                                 }
347                         }
348                                         
349                 }
350         }
351         else {
352                 Debug( LDAP_DEBUG_ANY,
353                         "bdb_db_open: Warning - No DB_CONFIG file found "
354                         "in directory %s: (%d)\n"
355                         "Expect poor performance for suffix %s.\n",
356                         bdb->bi_dbenv_home, errno, be->be_suffix[0].bv_val );
357         }
358                 
359         flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE;
360         if ( !( slapMode & SLAP_TOOL_QUICK ))
361                 flags |= DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN;
362
363         rc = db_env_create( &bdb->bi_dbenv, 0 );
364         if( rc != 0 ) {
365                 Debug( LDAP_DEBUG_ANY,
366                         "bdb_db_open: db_env_create failed: %s (%d)\n",
367                         db_strerror(rc), rc, 0 );
368                 return rc;
369         }
370
371         /* If a key was set, use shared memory for the BDB environment */
372         if ( bdb->bi_shm_key ) {
373                 bdb->bi_dbenv->set_shm_key( bdb->bi_dbenv, bdb->bi_shm_key );
374                 flags |= DB_SYSTEM_MEM;
375         }
376
377         bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
378         bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
379         bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
380
381         /* One long-lived TXN per thread, two TXNs per write op */
382         bdb->bi_dbenv->set_tx_max( bdb->bi_dbenv, connection_pool_max * 3 );
383
384 #ifdef SLAP_ZONE_ALLOC
385         if ( bdb->bi_cache.c_maxsize ) {
386                 bdb->bi_cache.c_zctx = slap_zn_mem_create(
387                                                                 SLAP_ZONE_INITSIZE,
388                                                                 SLAP_ZONE_MAXSIZE,
389                                                                 SLAP_ZONE_DELTA,
390                                                                 SLAP_ZONE_SIZE);
391         }
392 #endif
393
394         if ( bdb->bi_idl_cache_max_size ) {
395                 bdb->bi_idl_tree = NULL;
396                 ldap_pvt_thread_rdwr_init( &bdb->bi_idl_tree_rwlock );
397                 ldap_pvt_thread_mutex_init( &bdb->bi_idl_tree_lrulock );
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         /* If we're in server mode and time-based checkpointing is enabled,
539          * submit a task to perform periodic checkpoints.
540          */
541         if (( slapMode & SLAP_SERVER_MODE ) && bdb->bi_txn_cp &&
542                 bdb->bi_txn_cp_min )  {
543                 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
544                 ldap_pvt_runqueue_insert( &slapd_rq, bdb->bi_txn_cp_min*60,
545                         bdb_checkpoint, bdb );
546                 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
547         }
548
549         if (( slapMode&SLAP_SERVER_MODE ) && ( bdb->bi_flags&BDB_HAS_CONFIG )) {
550                 char    buf[SLAP_TEXT_BUFLEN];
551                 FILE *f = fopen( bdb->bi_db_config_path, "r" );
552                 struct berval bv;
553
554                 if ( f ) {
555                         while ( fgets( buf, sizeof(buf), f )) {
556                                 ber_str2bv( buf, 0, 1, &bv );
557                                 if ( bv.bv_val[bv.bv_len-1] == '\n' ) {
558                                         bv.bv_len--;
559                                         bv.bv_val[bv.bv_len] = '\0';
560                                 }
561                                 /* shouldn't need this, but ... */
562                                 if ( bv.bv_val[bv.bv_len-1] == '\r' ) {
563                                         bv.bv_len--;
564                                         bv.bv_val[bv.bv_len] = '\0';
565                                 }
566                                 ber_bvarray_add( &bdb->bi_db_config, &bv );
567                         }
568                         fclose( f );
569                 } else {
570                         /* Eh? It disappeared between config and open?? */
571                         bdb->bi_flags &= ~BDB_HAS_CONFIG;
572                 }
573
574         }
575         bdb->bi_flags |= BDB_IS_OPEN;
576
577         return 0;
578 }
579
580 static int
581 bdb_db_close( BackendDB *be )
582 {
583         int rc;
584         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
585         struct bdb_db_info *db;
586         bdb_idl_cache_entry_t *entry, *next_entry;
587
588         bdb->bi_flags &= ~BDB_IS_OPEN;
589
590         ber_bvarray_free( bdb->bi_db_config );
591
592         while( bdb->bi_ndatabases-- ) {
593                 db = bdb->bi_databases[bdb->bi_ndatabases];
594                 rc = db->bdi_db->close( db->bdi_db, 0 );
595                 /* Lower numbered names are not strdup'd */
596                 if( bdb->bi_ndatabases >= BDB_NDB )
597                         free( db->bdi_name );
598                 free( db );
599         }
600         free( bdb->bi_databases );
601         bdb_attr_index_destroy( bdb->bi_attrs );
602
603         bdb_cache_release_all (&bdb->bi_cache);
604
605         if ( bdb->bi_idl_cache_max_size ) {
606                 ldap_pvt_thread_rdwr_wlock ( &bdb->bi_idl_tree_rwlock );
607                 avl_free( bdb->bi_idl_tree, NULL );
608                 entry = bdb->bi_idl_lru_head;
609                 while ( entry != NULL ) {
610                         next_entry = entry->idl_lru_next;
611                         if ( entry->idl )
612                                 free( entry->idl );
613                         free( entry->kstr.bv_val );
614                         free( entry );
615                         entry = next_entry;
616                 }
617                 ldap_pvt_thread_rdwr_wunlock ( &bdb->bi_idl_tree_rwlock );
618         }
619
620         if ( !( slapMode & SLAP_TOOL_QUICK ) && bdb->bi_dbenv ) {
621                 XLOCK_ID_FREE(bdb->bi_dbenv, bdb->bi_cache.c_locker);
622         }
623
624         return 0;
625 }
626
627 static int
628 bdb_db_destroy( BackendDB *be )
629 {
630         int rc;
631         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
632
633         /* close db environment */
634         if( bdb->bi_dbenv ) {
635                 /* force a checkpoint */
636                 if ( !( slapMode & SLAP_TOOL_QUICK )) {
637                         rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
638                         if( rc != 0 ) {
639                                 Debug( LDAP_DEBUG_ANY,
640                                         "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
641                                         db_strerror(rc), rc, 0 );
642                         }
643                 }
644
645                 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
646                 bdb->bi_dbenv = NULL;
647                 if( rc != 0 ) {
648                         Debug( LDAP_DEBUG_ANY,
649                                 "bdb_db_destroy: close failed: %s (%d)\n",
650                                 db_strerror(rc), rc, 0 );
651                         return rc;
652                 }
653         }
654
655         rc = alock_close( &bdb->bi_alock_info );
656         if( rc != 0 ) {
657                 Debug( LDAP_DEBUG_ANY,
658                         "bdb_db_destroy: alock_close failed\n", 0, 0, 0 );
659                 return -1;
660         }
661
662         if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
663         if( bdb->bi_db_config_path ) ch_free( bdb->bi_db_config_path );
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         ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
669         ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
670         if ( bdb->bi_idl_cache_max_size ) {
671                 ldap_pvt_thread_rdwr_destroy( &bdb->bi_idl_tree_rwlock );
672                 ldap_pvt_thread_mutex_destroy( &bdb->bi_idl_tree_lrulock );
673         }
674
675         ch_free( bdb );
676         be->be_private = NULL;
677
678         return 0;
679 }
680
681 int
682 bdb_back_initialize(
683         BackendInfo     *bi )
684 {
685         int rc;
686
687         static char *controls[] = {
688                 LDAP_CONTROL_ASSERT,
689                 LDAP_CONTROL_MANAGEDSAIT,
690                 LDAP_CONTROL_NOOP,
691                 LDAP_CONTROL_PAGEDRESULTS,
692 #ifdef LDAP_CONTROL_SUBENTRIES
693                 LDAP_CONTROL_SUBENTRIES,
694 #endif
695 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
696                 LDAP_CONTROL_X_PERMISSIVE_MODIFY,
697 #endif
698                 NULL
699         };
700
701         /* initialize the underlying database system */
702         Debug( LDAP_DEBUG_TRACE,
703                 LDAP_XSTRING(bdb_back_initialize) ": initialize " 
704                 BDB_UCTYPE " backend\n", 0, 0, 0 );
705
706         bi->bi_flags |=
707                 SLAP_BFLAG_INCREMENT |
708 #ifdef BDB_SUBENTRIES
709                 SLAP_BFLAG_SUBENTRIES |
710 #endif
711                 SLAP_BFLAG_ALIASES |
712                 SLAP_BFLAG_REFERRALS;
713
714         bi->bi_controls = controls;
715
716         {       /* version check */
717                 int major, minor, patch, ver;
718                 char *version = db_version( &major, &minor, &patch );
719 #ifdef HAVE_EBCDIC
720                 char v2[1024];
721
722                 /* All our stdio does an ASCII to EBCDIC conversion on
723                  * the output. Strings from the BDB library are already
724                  * in EBCDIC; we have to go back and forth...
725                  */
726                 strcpy( v2, version );
727                 __etoa( v2 );
728                 version = v2;
729 #endif
730
731                 ver = (major << 24) | (minor << 16) | patch;
732                 if( ver != DB_VERSION_FULL ) {
733                         /* fail if a versions don't match */
734                         Debug( LDAP_DEBUG_ANY,
735                                 LDAP_XSTRING(bdb_back_initialize) ": "
736                                 "BDB library version mismatch:"
737                                 " expected " DB_VERSION_STRING ","
738                                 " got %s\n", version, 0, 0 );
739                         return -1;
740                 }
741
742                 Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(bdb_back_initialize)
743                         ": %s\n", version, 0, 0 );
744         }
745
746         db_env_set_func_free( ber_memfree );
747         db_env_set_func_malloc( (db_malloc *)ber_memalloc );
748         db_env_set_func_realloc( (db_realloc *)ber_memrealloc );
749 #ifndef NO_THREAD
750         /* This is a no-op on a NO_THREAD build. Leave the default
751          * alone so that BDB will sleep on interprocess conflicts.
752          */
753         db_env_set_func_yield( ldap_pvt_thread_yield );
754 #endif
755
756         bi->bi_open = 0;
757         bi->bi_close = 0;
758         bi->bi_config = 0;
759         bi->bi_destroy = 0;
760
761         bi->bi_db_init = bdb_db_init;
762         bi->bi_db_config = config_generic_wrapper;
763         bi->bi_db_open = bdb_db_open;
764         bi->bi_db_close = bdb_db_close;
765         bi->bi_db_destroy = bdb_db_destroy;
766
767         bi->bi_op_add = bdb_add;
768         bi->bi_op_bind = bdb_bind;
769         bi->bi_op_compare = bdb_compare;
770         bi->bi_op_delete = bdb_delete;
771         bi->bi_op_modify = bdb_modify;
772         bi->bi_op_modrdn = bdb_modrdn;
773         bi->bi_op_search = bdb_search;
774
775         bi->bi_op_unbind = 0;
776
777         bi->bi_extended = bdb_extended;
778
779         bi->bi_chk_referrals = bdb_referrals;
780         bi->bi_operational = bdb_operational;
781         bi->bi_has_subordinates = bdb_hasSubordinates;
782         bi->bi_entry_release_rw = bdb_entry_release;
783         bi->bi_entry_get_rw = bdb_entry_get;
784
785         /*
786          * hooks for slap tools
787          */
788         bi->bi_tool_entry_open = bdb_tool_entry_open;
789         bi->bi_tool_entry_close = bdb_tool_entry_close;
790         bi->bi_tool_entry_first = bdb_tool_entry_next;
791         bi->bi_tool_entry_next = bdb_tool_entry_next;
792         bi->bi_tool_entry_get = bdb_tool_entry_get;
793         bi->bi_tool_entry_put = bdb_tool_entry_put;
794         bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
795         bi->bi_tool_sync = 0;
796         bi->bi_tool_dn2id_get = bdb_tool_dn2id_get;
797         bi->bi_tool_id2entry_get = bdb_tool_id2entry_get;
798         bi->bi_tool_entry_modify = bdb_tool_entry_modify;
799
800         bi->bi_connection_init = 0;
801         bi->bi_connection_destroy = 0;
802
803         rc = bdb_back_init_cf( bi );
804
805         return rc;
806 }
807
808 #if     (SLAPD_BDB == SLAPD_MOD_DYNAMIC && !defined(BDB_HIER)) || \
809         (SLAPD_HDB == SLAPD_MOD_DYNAMIC && defined(BDB_HIER))
810
811 /* conditionally define the init_module() function */
812 #ifdef BDB_HIER
813 SLAP_BACKEND_INIT_MODULE( hdb )
814 #else /* !BDB_HIER */
815 SLAP_BACKEND_INIT_MODULE( bdb )
816 #endif /* !BDB_HIER */
817
818 #endif /* SLAPD_[BH]DB == SLAPD_MOD_DYNAMIC */
819