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