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