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