]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/init.c
Silence "empty statement" warnings
[openldap] / servers / slapd / back-bdb / init.c
1 /* init.c - initialize bdb backend */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2000-2005 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20 #include <ac/string.h>
21 #include <ac/unistd.h>
22 #include <ac/stdlib.h>
23 #include <ac/errno.h>
24 #include <sys/stat.h>
25 #include "back-bdb.h"
26 #include <lutil.h>
27 #include <ldap_rq.h>
28 #include "alock.h"
29
30 static const struct bdbi_database {
31         char *file;
32         char *name;
33         int type;
34         int flags;
35 } bdbi_databases[] = {
36         { "id2entry" BDB_SUFFIX, "id2entry", DB_BTREE, 0 },
37         { "dn2id" BDB_SUFFIX, "dn2id", DB_BTREE, 0 },
38         { NULL, NULL, 0, 0 }
39 };
40
41 typedef void * db_malloc(size_t);
42 typedef void * db_realloc(void *, size_t);
43
44 static int
45 bdb_db_init( BackendDB *be )
46 {
47         struct bdb_info *bdb;
48
49         Debug( LDAP_DEBUG_TRACE,
50                 LDAP_XSTRING(bdb_db_init) ": Initializing " BDB_UCTYPE " database\n",
51                 0, 0, 0 );
52
53         /* allocate backend-database-specific stuff */
54         bdb = (struct bdb_info *) ch_calloc( 1, sizeof(struct bdb_info) );
55
56         /* DBEnv parameters */
57         bdb->bi_dbenv_home = ch_strdup( SLAPD_DEFAULT_DB_DIR );
58         bdb->bi_dbenv_xflags = 0;
59         bdb->bi_dbenv_mode = SLAPD_DEFAULT_DB_MODE;
60
61         bdb->bi_cache.c_maxsize = DEFAULT_CACHE_SIZE;
62
63         bdb->bi_lock_detect = DB_LOCK_DEFAULT;
64         bdb->bi_search_stack_depth = DEFAULT_SEARCH_STACK_DEPTH;
65         bdb->bi_search_stack = NULL;
66
67         ldap_pvt_thread_mutex_init( &bdb->bi_database_mutex );
68         ldap_pvt_thread_mutex_init( &bdb->bi_lastid_mutex );
69 #ifdef BDB_HIER
70         ldap_pvt_thread_mutex_init( &bdb->bi_modrdns_mutex );
71 #endif
72         ldap_pvt_thread_mutex_init( &bdb->bi_cache.lru_mutex );
73         ldap_pvt_thread_mutex_init( &bdb->bi_cache.c_dntree.bei_kids_mutex );
74         ldap_pvt_thread_rdwr_init ( &bdb->bi_cache.c_rwlock );
75         ldap_pvt_thread_rdwr_init( &bdb->bi_idl_tree_rwlock );
76         ldap_pvt_thread_mutex_init( &bdb->bi_idl_tree_lrulock );
77
78         be->be_private = bdb;
79         be->be_cf_ocs = be->bd_info->bi_cf_ocs;
80
81         return 0;
82 }
83
84 /*
85  * Unconditionally perform a database recovery. Only works on
86  * databases that were previously opened with transactions and
87  * logs enabled.
88  */
89 static int
90 bdb_do_recovery( BackendDB *be )
91 {
92         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
93         DB_ENV  *re_dbenv;
94         u_int32_t flags;
95         int             rc;
96         char    path[MAXPATHLEN], *ptr;
97
98         /* Create and init the recovery environment */
99         rc = db_env_create( &re_dbenv, 0 );
100         if( rc != 0 ) {
101                 Debug( LDAP_DEBUG_ANY,
102                         "bdb_do_recovery: db_env_create failed: %s (%d)\n",
103                         db_strerror(rc), rc, 0 );
104                 return rc;
105         }
106         re_dbenv->set_errpfx( re_dbenv, be->be_suffix[0].bv_val );
107         re_dbenv->set_errcall( re_dbenv, bdb_errcall );
108         (void)re_dbenv->set_verbose(re_dbenv, DB_VERB_RECOVERY, 1);
109 #if DB_VERSION_FULL < 0x04030000
110         (void)re_dbenv->set_verbose(re_dbenv, DB_VERB_CHKPOINT, 1);
111 #else
112         re_dbenv->set_msgcall( re_dbenv, bdb_msgcall );
113 #endif
114
115         flags = DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL |
116                 DB_INIT_TXN | DB_USE_ENVIRON | DB_RECOVER;
117
118         /* If a key was set, use shared memory for the BDB environment */
119         if ( bdb->bi_shm_key ) {
120                 re_dbenv->set_shm_key( re_dbenv, bdb->bi_shm_key );
121                 flags |= DB_SYSTEM_MEM;
122         }
123
124         /* Open the environment, which will also perform the recovery */
125 #ifdef HAVE_EBCDIC
126         strcpy( path, bdb->bi_dbenv_home );
127         __atoe( path );
128         rc = re_dbenv->open( re_dbenv,
129                 path,
130                 flags,
131                 bdb->bi_dbenv_mode );
132 #else
133         rc = re_dbenv->open( re_dbenv,
134                 bdb->bi_dbenv_home,
135                 flags,
136                 bdb->bi_dbenv_mode );
137 #endif
138         if( rc != 0 ) {
139                 Debug( LDAP_DEBUG_ANY,
140                         "bdb_do_recovery: dbenv_open failed: %s (%d)\n",
141                         db_strerror(rc), rc, 0 );
142                 return rc;
143         }
144         (void) re_dbenv->close( re_dbenv, 0 );
145
146         /* By convention we reset the mtime for id2entry.bdb to the current time */
147         ptr = lutil_strcopy( path, bdb->bi_dbenv_home);
148         *ptr++ = LDAP_DIRSEP[0];
149         strcpy( ptr, bdbi_databases[0].file);
150         (void) utime( path, NULL);
151
152         return 0;
153 }
154
155 /*
156  * Database recovery logic:
157  * This function is called whenever the database appears to have been
158  * shut down uncleanly, as determined by the alock functions. 
159  * Because of the -q function in slapadd, there is also the possibility
160  * that the shutdown happened when transactions weren't being used and
161  * the database is likely to be corrupt. The function checks for this
162  * condition by examining the environment to make sure it had previously
163  * been opened with transactions enabled. If this is the case, the
164  * database is recovered as usual. If transactions were not enabled,
165  * then this function will return a fail.
166  */
167 static int
168 bdb_db_recover( BackendDB *be )
169 {
170         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
171         DB_ENV  *re_dbenv;
172         u_int32_t flags;
173         int             rc;
174 #ifdef HAVE_EBCDIC
175         char    path[MAXPATHLEN];
176 #endif
177
178         /* Create the recovery environment, then open it.
179          * We use the DB_JOIN in combination with a flags value of
180          * zero so we join an existing environment and can read the
181          * value of the flags that were used the last time the 
182          * environment was opened. DB_CREATE is added because the
183          * open would fail if the only thing that had been done
184          * was an open with transactions and logs disabled.
185          */
186         rc = db_env_create( &re_dbenv, 0 );
187         if( rc != 0 ) {
188                 Debug( LDAP_DEBUG_ANY,
189                         "bdb_db_recover: db_env_create failed: %s (%d)\n",
190                         db_strerror(rc), rc, 0 );
191                 return rc;
192         }
193         re_dbenv->set_errpfx( re_dbenv, be->be_suffix[0].bv_val );
194         re_dbenv->set_errcall( re_dbenv, bdb_errcall );
195
196         Debug( LDAP_DEBUG_TRACE,
197                 "bdb_db_recover: dbenv_open(%s)\n",
198                 bdb->bi_dbenv_home, 0, 0);
199
200 #ifdef HAVE_EBCDIC
201         strcpy( path, bdb->bi_dbenv_home );
202         __atoe( path );
203         rc = re_dbenv->open( re_dbenv,
204                 path,
205                 DB_JOINENV,
206                 bdb->bi_dbenv_mode );
207 #else
208         rc = re_dbenv->open( re_dbenv,
209                 bdb->bi_dbenv_home,
210                 DB_JOINENV,
211                 bdb->bi_dbenv_mode );
212 #endif
213
214         if( rc == ENOENT ) {
215                 Debug( LDAP_DEBUG_TRACE,
216                         "bdb_db_recover: DB environment files are missing, assuming it was "
217                         "manually recovered\n", 0, 0, 0 );
218                 return 0;
219         }
220         else if( rc != 0 ) {
221                 Debug( LDAP_DEBUG_ANY,
222                         "bdb_db_recover: dbenv_open failed: %s (%d)\n",
223                         db_strerror(rc), rc, 0 );
224                 return rc;
225         }
226
227         /*
228          * Check the flags that had been used in the previous open.
229          * The environment needed to have had both
230          * DB_INIT_LOG and DB_INIT_TXN set for us to be willing to
231          * recover the database. Otherwise the an app failed while running
232          * without transactions and logs enabled and the dn2id and id2entry
233          * mapping is likely to be corrupt.
234          */
235         rc = re_dbenv->get_open_flags( re_dbenv, &flags );
236         if( rc != 0 ) {
237                 Debug( LDAP_DEBUG_ANY,
238                         "bdb_db_recover: get_open_flags failed: %s (%d)\n",
239                         db_strerror(rc), rc, 0 );
240                 return rc;
241         }
242
243         (void) re_dbenv->close( re_dbenv, 0 );
244
245         if( (flags & DB_INIT_LOG) && (flags & DB_INIT_TXN) ) {
246                 return bdb_do_recovery( be );
247         }
248
249 re_exit:
250         Debug( LDAP_DEBUG_ANY,
251                 "bdb_db_recover: Database cannot be recovered. "\
252                 "Restore from backup!\n", 0, 0, 0);
253         return -1;
254
255 }
256
257
258 static int
259 bdb_db_open( BackendDB *be )
260 {
261         int rc, i;
262         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
263         struct stat stat1, stat2;
264         u_int32_t flags;
265         char path[MAXPATHLEN];
266         char *ptr;
267
268         Debug( LDAP_DEBUG_ARGS,
269                 "bdb_db_open: %s\n",
270                 be->be_suffix[0].bv_val, 0, 0 );
271
272 #ifndef BDB_MULTIPLE_SUFFIXES
273         if ( be->be_suffix[1].bv_val ) {
274         Debug( LDAP_DEBUG_ANY,
275                 "bdb_db_open: only one suffix allowed\n", 0, 0, 0 );
276                 return -1;
277         }
278 #endif
279
280         /* Check existence of dbenv_home. Any error means trouble */
281         rc = stat( bdb->bi_dbenv_home, &stat1 );
282         if( rc !=0 ) {
283                 Debug( LDAP_DEBUG_ANY,
284                         "bdb_db_open: Cannot access database directory %s (%d)\n",
285                         bdb->bi_dbenv_home, errno, 0 );
286                         return -1;
287         }
288         
289         /* Perform database use arbitration/recovery logic */
290         rc = alock_open( &bdb->bi_alock_info, 
291                                 "slapd", 
292                                 bdb->bi_dbenv_home,
293                                 slapMode & SLAP_TOOL_READONLY ?
294                                 ALOCK_LOCKED : ALOCK_UNIQUE );
295
296         if( rc == ALOCK_RECOVER ) {
297                 Debug( LDAP_DEBUG_ANY,
298                         "bdb_db_open: unclean shutdown detected;"
299                         " attempting recovery.\n", 
300                         0, 0, 0 );
301                 if( bdb_db_recover( be ) != 0 ) {
302                         Debug( LDAP_DEBUG_ANY,
303                                 "bdb_db_open: DB recovery failed.\n",
304                                 0, 0, 0 );
305                         return -1;
306                 }
307                 if( alock_recover (&bdb->bi_alock_info) != 0 ) {
308                         Debug( LDAP_DEBUG_ANY,
309                                 "bdb_db_open: alock_recover failed\n",
310                                 0, 0, 0 );
311                         return -1;
312                 }
313
314         } else if( rc == ALOCK_BUSY ) {
315                 Debug( LDAP_DEBUG_ANY,
316                         "bdb_db_open: database already in use\n", 
317                         0, 0, 0 );
318                 return -1;
319         } else if( rc != ALOCK_CLEAN ) {
320                 Debug( LDAP_DEBUG_ANY,
321                         "bdb_db_open: alock package is unstable\n", 
322                         0, 0, 0 );
323                 return -1;
324         }
325         
326         /*
327          * The DB_CONFIG file may have changed. If so, recover the
328          * database so that new settings are put into effect. Also
329          * note the possible absence of DB_CONFIG in the log.
330          */
331         if( stat( bdb->bi_db_config_path, &stat1 ) == 0 ) {
332                 ptr = lutil_strcopy(path, bdb->bi_dbenv_home);
333                 *ptr++ = LDAP_DIRSEP[0];
334                 strcpy( ptr, bdbi_databases[0].file);
335                 if( stat( path, &stat2 ) == 0 ) {
336                         if( stat2.st_mtime <= stat1.st_mtime ) {
337                                 Debug( LDAP_DEBUG_ANY,
338                                         "bdb_db_open: DB_CONFIG for suffix %s has changed.\n"
339                                         "Performing database recovery to activate new settings.\n",
340                                         be->be_suffix[0].bv_val, 0, 0 );
341                                 if( bdb_do_recovery( be ) != 0) {
342                                         Debug( LDAP_DEBUG_ANY,
343                                                 "bdb_db_open: db recovery failed.\n",
344                                                 0, 0, 0 );
345                                         return -1;
346                                 }
347                         }
348                                         
349                 }
350         }
351         else {
352                 Debug( LDAP_DEBUG_ANY,
353                         "bdb_db_open: Warning - No DB_CONFIG file found "
354                         "in directory %s: (%d)\n"
355                         "Expect poor performance for suffix %s.\n",
356                         bdb->bi_dbenv_home, errno, be->be_suffix[0].bv_val );
357         }
358                 
359         flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE;
360         if ( !( slapMode & SLAP_TOOL_QUICK ))
361                 flags |= DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN;
362
363         rc = db_env_create( &bdb->bi_dbenv, 0 );
364         if( rc != 0 ) {
365                 Debug( LDAP_DEBUG_ANY,
366                         "bdb_db_open: db_env_create failed: %s (%d)\n",
367                         db_strerror(rc), rc, 0 );
368                 return rc;
369         }
370
371         /* If a key was set, use shared memory for the BDB environment */
372         if ( bdb->bi_shm_key ) {
373                 bdb->bi_dbenv->set_shm_key( bdb->bi_dbenv, bdb->bi_shm_key );
374                 flags |= DB_SYSTEM_MEM;
375         }
376
377         bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
378         bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
379         bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
380
381         /* One long-lived TXN per thread, two TXNs per write op */
382         bdb->bi_dbenv->set_tx_max( bdb->bi_dbenv, connection_pool_max * 3 );
383
384 #ifdef SLAP_ZONE_ALLOC
385         if ( bdb->bi_cache.c_maxsize ) {
386                 bdb->bi_cache.c_zctx = slap_zn_mem_create(
387                                                                 SLAP_ZONE_INITSIZE,
388                                                                 SLAP_ZONE_MAXSIZE,
389                                                                 SLAP_ZONE_DELTA,
390                                                                 SLAP_ZONE_SIZE);
391         }
392 #endif
393
394         if ( bdb->bi_idl_cache_max_size ) {
395                 bdb->bi_idl_tree = NULL;
396                 bdb->bi_idl_cache_size = 0;
397         }
398
399         if( bdb->bi_dbenv_xflags != 0 ) {
400                 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
401                         bdb->bi_dbenv_xflags, 1);
402                 if( rc != 0 ) {
403                         Debug( LDAP_DEBUG_ANY,
404                                 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
405                                 db_strerror(rc), rc, 0 );
406                         return rc;
407                 }
408         }
409
410         Debug( LDAP_DEBUG_TRACE,
411                 "bdb_db_open: dbenv_open(%s)\n",
412                 bdb->bi_dbenv_home, 0, 0);
413
414 #ifdef HAVE_EBCDIC
415         strcpy( path, bdb->bi_dbenv_home );
416         __atoe( path );
417         rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
418                 path,
419                 flags,
420                 bdb->bi_dbenv_mode );
421 #else
422         rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
423                 bdb->bi_dbenv_home,
424                 flags,
425                 bdb->bi_dbenv_mode );
426 #endif
427         if( rc != 0 ) {
428                 Debug( LDAP_DEBUG_ANY,
429                         "bdb_db_open: dbenv_open failed: %s (%d)\n",
430                         db_strerror(rc), rc, 0 );
431                 return rc;
432         }
433
434         flags = DB_THREAD | bdb->bi_db_opflags;
435
436 #ifdef DB_AUTO_COMMIT
437         if ( !( slapMode & SLAP_TOOL_QUICK ))
438                 flags |= DB_AUTO_COMMIT;
439 #endif
440
441         bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
442                 BDB_INDICES * sizeof(struct bdb_db_info *) );
443
444         /* open (and create) main database */
445         for( i = 0; bdbi_databases[i].name; i++ ) {
446                 struct bdb_db_info *db;
447
448                 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
449
450                 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
451                 if( rc != 0 ) {
452                         Debug( LDAP_DEBUG_ANY,
453                                 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
454                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
455                         return rc;
456                 }
457
458                 if( i == BDB_ID2ENTRY ) {
459                         rc = db->bdi_db->set_pagesize( db->bdi_db,
460                                 BDB_ID2ENTRY_PAGESIZE );
461                         if ( slapMode & SLAP_TOOL_READMAIN ) {
462                                 flags |= DB_RDONLY;
463                         } else {
464                                 flags |= DB_CREATE;
465                         }
466                 } else {
467                         rc = db->bdi_db->set_flags( db->bdi_db, 
468                                 DB_DUP | DB_DUPSORT );
469 #ifndef BDB_HIER
470                         if ( slapMode & SLAP_TOOL_READONLY ) {
471                                 flags |= DB_RDONLY;
472                         } else {
473                                 flags |= DB_CREATE;
474                         }
475 #else
476                         if ( slapMode & (SLAP_TOOL_READONLY|SLAP_TOOL_READMAIN) ) {
477                                 flags |= DB_RDONLY;
478                         } else {
479                                 flags |= DB_CREATE;
480                         }
481 #endif
482                         rc = db->bdi_db->set_pagesize( db->bdi_db,
483                                 BDB_PAGESIZE );
484                 }
485
486 #ifdef HAVE_EBCDIC
487                 strcpy( path, bdbi_databases[i].file );
488                 __atoe( path );
489                 rc = DB_OPEN( db->bdi_db,
490                         path,
491                 /*      bdbi_databases[i].name, */ NULL,
492                         bdbi_databases[i].type,
493                         bdbi_databases[i].flags | flags,
494                         bdb->bi_dbenv_mode );
495 #else
496                 rc = DB_OPEN( db->bdi_db,
497                         bdbi_databases[i].file,
498                 /*      bdbi_databases[i].name, */ NULL,
499                         bdbi_databases[i].type,
500                         bdbi_databases[i].flags | flags,
501                         bdb->bi_dbenv_mode );
502 #endif
503
504                 if ( rc != 0 ) {
505                         char    buf[SLAP_TEXT_BUFLEN];
506
507                         snprintf( buf, sizeof(buf), "%s/%s", 
508                                 bdb->bi_dbenv_home, bdbi_databases[i].file );
509                         Debug( LDAP_DEBUG_ANY,
510                                 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
511                                 buf, db_strerror(rc), rc );
512                         return rc;
513                 }
514
515                 flags &= ~(DB_CREATE | DB_RDONLY);
516                 db->bdi_name = bdbi_databases[i].name;
517                 bdb->bi_databases[i] = db;
518         }
519
520         bdb->bi_databases[i] = NULL;
521         bdb->bi_ndatabases = i;
522
523         /* get nextid */
524         rc = bdb_last_id( be, NULL );
525         if( rc != 0 ) {
526                 Debug( LDAP_DEBUG_ANY,
527                         "bdb_db_open: last_id(%s) failed: %s (%d)\n",
528                         bdb->bi_dbenv_home, db_strerror(rc), rc );
529                 return rc;
530         }
531
532         if ( !( slapMode & SLAP_TOOL_QUICK )) {
533                 XLOCK_ID(bdb->bi_dbenv, &bdb->bi_cache.c_locker);
534         }
535
536         bdb->bi_flags |= BDB_IS_OPEN;
537
538         return 0;
539 }
540
541 static int
542 bdb_db_close( BackendDB *be )
543 {
544         int rc;
545         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
546         struct bdb_db_info *db;
547         bdb_idl_cache_entry_t *entry, *next_entry;
548
549         /* backend_shutdown closes everything, even if not all were opened */
550         if ( !bdb->bi_flags & BDB_IS_OPEN )
551                 return 0;
552
553         bdb->bi_flags &= ~BDB_IS_OPEN;
554
555         ber_bvarray_free( bdb->bi_db_config );
556         bdb->bi_db_config = NULL;
557
558         while( bdb->bi_databases && bdb->bi_ndatabases-- ) {
559                 db = bdb->bi_databases[bdb->bi_ndatabases];
560                 rc = db->bdi_db->close( db->bdi_db, 0 );
561                 /* Lower numbered names are not strdup'd */
562                 if( bdb->bi_ndatabases >= BDB_NDB )
563                         free( db->bdi_name );
564                 free( db );
565         }
566         free( bdb->bi_databases );
567         bdb->bi_databases = NULL;
568
569         bdb_cache_release_all (&bdb->bi_cache);
570
571         if ( bdb->bi_idl_cache_max_size ) {
572                 ldap_pvt_thread_rdwr_wlock ( &bdb->bi_idl_tree_rwlock );
573                 avl_free( bdb->bi_idl_tree, NULL );
574                 bdb->bi_idl_tree = NULL;
575                 entry = bdb->bi_idl_lru_head;
576                 while ( entry != NULL ) {
577                         next_entry = entry->idl_lru_next;
578                         if ( entry->idl )
579                                 free( entry->idl );
580                         free( entry->kstr.bv_val );
581                         free( entry );
582                         entry = next_entry;
583                 }
584                 bdb->bi_idl_lru_head = bdb->bi_idl_lru_tail = NULL;
585                 ldap_pvt_thread_rdwr_wunlock ( &bdb->bi_idl_tree_rwlock );
586         }
587
588         if ( !( slapMode & SLAP_TOOL_QUICK ) && bdb->bi_dbenv ) {
589                 XLOCK_ID_FREE(bdb->bi_dbenv, bdb->bi_cache.c_locker);
590                 bdb->bi_cache.c_locker = 0;
591         }
592
593         /* close db environment */
594         if( bdb->bi_dbenv ) {
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: txn_checkpoint failed: %s (%d)\n",
603                                         db_strerror(rc), rc, 0 );
604                         }
605                 }
606
607                 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
608                 bdb->bi_dbenv = NULL;
609                 if( rc != 0 ) {
610                         Debug( LDAP_DEBUG_ANY,
611                                 "bdb_db_close: close failed: %s (%d)\n",
612                                 db_strerror(rc), rc, 0 );
613                         return rc;
614                 }
615
616 #if DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR == 2
617                 /* Delete the environment if we were in quick mode. This
618                  * works around a bug in bdb4.2 that interferes with the
619                  * operation of db_stat and other tools after a slapadd -q
620                  * or slapindex -q has taken place.
621                  */
622                 if( slapMode & SLAP_TOOL_QUICK ) {
623                         rc = db_env_create( &bdb->bi_dbenv, 0 );
624                         if( rc != 0 ) {
625                                 Debug( LDAP_DEBUG_ANY,
626                                         "bdb_db_close: db_env_create failed: %s (%d)\n",
627                                         db_strerror(rc), rc, 0 );
628                                 return rc;
629                         }
630                         rc = bdb->bi_dbenv->remove(bdb->bi_dbenv, bdb->bi_dbenv_home,
631                                         DB_FORCE);
632                         bdb->bi_dbenv = NULL;
633                         if( rc != 0 ) {
634                                 Debug( LDAP_DEBUG_ANY,
635                                         "bdb_db_close: dbenv_remove failed: %s (%d)\n",
636                                         db_strerror(rc), rc, 0 );
637                                 return rc;
638                         }
639                 }
640 #endif
641         }
642
643         rc = alock_close( &bdb->bi_alock_info );
644         if( rc != 0 ) {
645                 Debug( LDAP_DEBUG_ANY,
646                         "bdb_db_close: alock_close failed\n", 0, 0, 0 );
647                 return -1;
648         }
649
650         return 0;
651 }
652
653 static int
654 bdb_db_destroy( BackendDB *be )
655 {
656         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
657
658         if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
659         if( bdb->bi_db_config_path ) ch_free( bdb->bi_db_config_path );
660
661         bdb_attr_index_destroy( bdb->bi_attrs );
662
663         ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
664         ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_mutex );
665         ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_dntree.bei_kids_mutex );
666 #ifdef BDB_HIER
667         ldap_pvt_thread_mutex_destroy( &bdb->bi_modrdns_mutex );
668 #endif
669         ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
670         ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
671         ldap_pvt_thread_rdwr_destroy( &bdb->bi_idl_tree_rwlock );
672         ldap_pvt_thread_mutex_destroy( &bdb->bi_idl_tree_lrulock );
673
674         ch_free( bdb );
675         be->be_private = NULL;
676
677         return 0;
678 }
679
680 int
681 bdb_back_initialize(
682         BackendInfo     *bi )
683 {
684         int rc;
685
686         static char *controls[] = {
687                 LDAP_CONTROL_ASSERT,
688                 LDAP_CONTROL_MANAGEDSAIT,
689                 LDAP_CONTROL_NOOP,
690                 LDAP_CONTROL_PAGEDRESULTS,
691 #ifdef LDAP_CONTROL_SUBENTRIES
692                 LDAP_CONTROL_SUBENTRIES,
693 #endif
694 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
695                 LDAP_CONTROL_X_PERMISSIVE_MODIFY,
696 #endif
697                 NULL
698         };
699
700         /* initialize the underlying database system */
701         Debug( LDAP_DEBUG_TRACE,
702                 LDAP_XSTRING(bdb_back_initialize) ": initialize " 
703                 BDB_UCTYPE " backend\n", 0, 0, 0 );
704
705         bi->bi_flags |=
706                 SLAP_BFLAG_INCREMENT |
707 #ifdef BDB_SUBENTRIES
708                 SLAP_BFLAG_SUBENTRIES |
709 #endif
710                 SLAP_BFLAG_ALIASES |
711                 SLAP_BFLAG_REFERRALS;
712
713         bi->bi_controls = controls;
714
715         {       /* version check */
716                 int major, minor, patch, ver;
717                 char *version = db_version( &major, &minor, &patch );
718 #ifdef HAVE_EBCDIC
719                 char v2[1024];
720
721                 /* All our stdio does an ASCII to EBCDIC conversion on
722                  * the output. Strings from the BDB library are already
723                  * in EBCDIC; we have to go back and forth...
724                  */
725                 strcpy( v2, version );
726                 __etoa( v2 );
727                 version = v2;
728 #endif
729
730                 ver = (major << 24) | (minor << 16) | patch;
731                 if( ver != DB_VERSION_FULL ) {
732                         /* fail if a versions don't match */
733                         Debug( LDAP_DEBUG_ANY,
734                                 LDAP_XSTRING(bdb_back_initialize) ": "
735                                 "BDB library version mismatch:"
736                                 " expected " DB_VERSION_STRING ","
737                                 " got %s\n", version, 0, 0 );
738                         return -1;
739                 }
740
741                 Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(bdb_back_initialize)
742                         ": %s\n", version, 0, 0 );
743         }
744
745         db_env_set_func_free( ber_memfree );
746         db_env_set_func_malloc( (db_malloc *)ber_memalloc );
747         db_env_set_func_realloc( (db_realloc *)ber_memrealloc );
748 #ifndef NO_THREAD
749         /* This is a no-op on a NO_THREAD build. Leave the default
750          * alone so that BDB will sleep on interprocess conflicts.
751          */
752         db_env_set_func_yield( ldap_pvt_thread_yield );
753 #endif
754
755         bi->bi_open = 0;
756         bi->bi_close = 0;
757         bi->bi_config = 0;
758         bi->bi_destroy = 0;
759
760         bi->bi_db_init = bdb_db_init;
761         bi->bi_db_config = config_generic_wrapper;
762         bi->bi_db_open = bdb_db_open;
763         bi->bi_db_close = bdb_db_close;
764         bi->bi_db_destroy = bdb_db_destroy;
765
766         bi->bi_op_add = bdb_add;
767         bi->bi_op_bind = bdb_bind;
768         bi->bi_op_compare = bdb_compare;
769         bi->bi_op_delete = bdb_delete;
770         bi->bi_op_modify = bdb_modify;
771         bi->bi_op_modrdn = bdb_modrdn;
772         bi->bi_op_search = bdb_search;
773
774         bi->bi_op_unbind = 0;
775
776         bi->bi_extended = bdb_extended;
777
778         bi->bi_chk_referrals = bdb_referrals;
779         bi->bi_operational = bdb_operational;
780         bi->bi_has_subordinates = bdb_hasSubordinates;
781         bi->bi_entry_release_rw = bdb_entry_release;
782         bi->bi_entry_get_rw = bdb_entry_get;
783
784         /*
785          * hooks for slap tools
786          */
787         bi->bi_tool_entry_open = bdb_tool_entry_open;
788         bi->bi_tool_entry_close = bdb_tool_entry_close;
789         bi->bi_tool_entry_first = bdb_tool_entry_next;
790         bi->bi_tool_entry_next = bdb_tool_entry_next;
791         bi->bi_tool_entry_get = bdb_tool_entry_get;
792         bi->bi_tool_entry_put = bdb_tool_entry_put;
793         bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
794         bi->bi_tool_sync = 0;
795         bi->bi_tool_dn2id_get = bdb_tool_dn2id_get;
796         bi->bi_tool_id2entry_get = bdb_tool_id2entry_get;
797         bi->bi_tool_entry_modify = bdb_tool_entry_modify;
798
799         bi->bi_connection_init = 0;
800         bi->bi_connection_destroy = 0;
801
802         rc = bdb_back_init_cf( bi );
803
804         return rc;
805 }
806
807 #if     (SLAPD_BDB == SLAPD_MOD_DYNAMIC && !defined(BDB_HIER)) || \
808         (SLAPD_HDB == SLAPD_MOD_DYNAMIC && defined(BDB_HIER))
809
810 /* conditionally define the init_module() function */
811 #ifdef BDB_HIER
812 SLAP_BACKEND_INIT_MODULE( hdb )
813 #else /* !BDB_HIER */
814 SLAP_BACKEND_INIT_MODULE( bdb )
815 #endif /* !BDB_HIER */
816
817 #endif /* SLAPD_[BH]DB == SLAPD_MOD_DYNAMIC */
818