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