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