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