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