]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/init.c
ITS#3841 fix test in rev 1.220
[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         Debug( LDAP_DEBUG_ANY,
250                 "bdb_db_recover: Database cannot be recovered. "\
251                 "Restore from backup!\n", 0, 0, 0);
252         return -1;
253
254 }
255
256
257 static int
258 bdb_db_open( BackendDB *be )
259 {
260         int rc, i;
261         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
262         struct stat stat1, stat2;
263         u_int32_t flags;
264         char path[MAXPATHLEN];
265         char *ptr;
266
267         Debug( LDAP_DEBUG_ARGS,
268                 "bdb_db_open: %s\n",
269                 be->be_suffix[0].bv_val, 0, 0 );
270
271 #ifndef BDB_MULTIPLE_SUFFIXES
272         if ( be->be_suffix[1].bv_val ) {
273         Debug( LDAP_DEBUG_ANY,
274                 "bdb_db_open: only one suffix allowed\n", 0, 0, 0 );
275                 return -1;
276         }
277 #endif
278
279         /* Check existence of dbenv_home. Any error means trouble */
280         rc = stat( bdb->bi_dbenv_home, &stat1 );
281         if( rc !=0 ) {
282                 Debug( LDAP_DEBUG_ANY,
283                         "bdb_db_open: Cannot access database directory %s (%d)\n",
284                         bdb->bi_dbenv_home, errno, 0 );
285                         return -1;
286         }
287         
288         /* Perform database use arbitration/recovery logic */
289         rc = alock_open( &bdb->bi_alock_info, 
290                                 "slapd", 
291                                 bdb->bi_dbenv_home,
292                                 slapMode & SLAP_TOOL_READONLY ?
293                                 ALOCK_LOCKED : ALOCK_UNIQUE );
294
295         if( rc == ALOCK_RECOVER ) {
296                 Debug( LDAP_DEBUG_ANY,
297                         "bdb_db_open: unclean shutdown detected;"
298                         " attempting recovery.\n", 
299                         0, 0, 0 );
300                 if( bdb_db_recover( be ) != 0 ) {
301                         Debug( LDAP_DEBUG_ANY,
302                                 "bdb_db_open: DB recovery failed.\n",
303                                 0, 0, 0 );
304                         return -1;
305                 }
306                 if( alock_recover (&bdb->bi_alock_info) != 0 ) {
307                         Debug( LDAP_DEBUG_ANY,
308                                 "bdb_db_open: alock_recover failed\n",
309                                 0, 0, 0 );
310                         return -1;
311                 }
312
313         } else if( rc == ALOCK_BUSY ) {
314                 Debug( LDAP_DEBUG_ANY,
315                         "bdb_db_open: database already in use\n", 
316                         0, 0, 0 );
317                 return -1;
318         } else if( rc != ALOCK_CLEAN ) {
319                 Debug( LDAP_DEBUG_ANY,
320                         "bdb_db_open: alock package is unstable\n", 
321                         0, 0, 0 );
322                 return -1;
323         }
324         
325         /*
326          * The DB_CONFIG file may have changed. If so, recover the
327          * database so that new settings are put into effect. Also
328          * note the possible absence of DB_CONFIG in the log.
329          */
330         if( stat( bdb->bi_db_config_path, &stat1 ) == 0 ) {
331                 ptr = lutil_strcopy(path, bdb->bi_dbenv_home);
332                 *ptr++ = LDAP_DIRSEP[0];
333                 strcpy( ptr, bdbi_databases[0].file);
334                 if( stat( path, &stat2 ) == 0 ) {
335                         if( stat2.st_mtime <= stat1.st_mtime ) {
336                                 Debug( LDAP_DEBUG_ANY,
337                                         "bdb_db_open: DB_CONFIG for suffix %s has changed.\n"
338                                         "Performing database recovery to activate new settings.\n",
339                                         be->be_suffix[0].bv_val, 0, 0 );
340                                 if( bdb_do_recovery( be ) != 0) {
341                                         Debug( LDAP_DEBUG_ANY,
342                                                 "bdb_db_open: db recovery failed.\n",
343                                                 0, 0, 0 );
344                                         return -1;
345                                 }
346                         }
347                                         
348                 }
349         }
350         else {
351                 Debug( LDAP_DEBUG_ANY,
352                         "bdb_db_open: Warning - No DB_CONFIG file found "
353                         "in directory %s: (%d)\n"
354                         "Expect poor performance for suffix %s.\n",
355                         bdb->bi_dbenv_home, errno, be->be_suffix[0].bv_val );
356         }
357                 
358         flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE;
359         if ( !( slapMode & SLAP_TOOL_QUICK ))
360                 flags |= DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN;
361
362         rc = db_env_create( &bdb->bi_dbenv, 0 );
363         if( rc != 0 ) {
364                 Debug( LDAP_DEBUG_ANY,
365                         "bdb_db_open: db_env_create failed: %s (%d)\n",
366                         db_strerror(rc), rc, 0 );
367                 return rc;
368         }
369
370         /* If a key was set, use shared memory for the BDB environment */
371         if ( bdb->bi_shm_key ) {
372                 bdb->bi_dbenv->set_shm_key( bdb->bi_dbenv, bdb->bi_shm_key );
373                 flags |= DB_SYSTEM_MEM;
374         }
375
376         bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
377         bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
378         bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
379
380         /* One long-lived TXN per thread, two TXNs per write op */
381         bdb->bi_dbenv->set_tx_max( bdb->bi_dbenv, connection_pool_max * 3 );
382
383 #ifdef SLAP_ZONE_ALLOC
384         if ( bdb->bi_cache.c_maxsize ) {
385                 bdb->bi_cache.c_zctx = slap_zn_mem_create(
386                                                                 SLAP_ZONE_INITSIZE,
387                                                                 SLAP_ZONE_MAXSIZE,
388                                                                 SLAP_ZONE_DELTA,
389                                                                 SLAP_ZONE_SIZE);
390         }
391 #endif
392
393         if ( bdb->bi_idl_cache_max_size ) {
394                 bdb->bi_idl_tree = NULL;
395                 bdb->bi_idl_cache_size = 0;
396         }
397
398         if( bdb->bi_dbenv_xflags != 0 ) {
399                 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
400                         bdb->bi_dbenv_xflags, 1);
401                 if( rc != 0 ) {
402                         Debug( LDAP_DEBUG_ANY,
403                                 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
404                                 db_strerror(rc), rc, 0 );
405                         return rc;
406                 }
407         }
408
409         Debug( LDAP_DEBUG_TRACE,
410                 "bdb_db_open: dbenv_open(%s)\n",
411                 bdb->bi_dbenv_home, 0, 0);
412
413 #ifdef HAVE_EBCDIC
414         strcpy( path, bdb->bi_dbenv_home );
415         __atoe( path );
416         rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
417                 path,
418                 flags,
419                 bdb->bi_dbenv_mode );
420 #else
421         rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
422                 bdb->bi_dbenv_home,
423                 flags,
424                 bdb->bi_dbenv_mode );
425 #endif
426         if( rc != 0 ) {
427                 Debug( LDAP_DEBUG_ANY,
428                         "bdb_db_open: dbenv_open failed: %s (%d)\n",
429                         db_strerror(rc), rc, 0 );
430                 return rc;
431         }
432
433         flags = DB_THREAD | bdb->bi_db_opflags;
434
435 #ifdef DB_AUTO_COMMIT
436         if ( !( slapMode & SLAP_TOOL_QUICK ))
437                 flags |= DB_AUTO_COMMIT;
438 #endif
439
440         bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
441                 BDB_INDICES * sizeof(struct bdb_db_info *) );
442
443         /* open (and create) main database */
444         for( i = 0; bdbi_databases[i].name; i++ ) {
445                 struct bdb_db_info *db;
446
447                 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
448
449                 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
450                 if( rc != 0 ) {
451                         Debug( LDAP_DEBUG_ANY,
452                                 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
453                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
454                         return rc;
455                 }
456
457                 if( i == BDB_ID2ENTRY ) {
458                         rc = db->bdi_db->set_pagesize( db->bdi_db,
459                                 BDB_ID2ENTRY_PAGESIZE );
460                         if ( slapMode & SLAP_TOOL_READMAIN ) {
461                                 flags |= DB_RDONLY;
462                         } else {
463                                 flags |= DB_CREATE;
464                         }
465                 } else {
466                         rc = db->bdi_db->set_flags( db->bdi_db, 
467                                 DB_DUP | DB_DUPSORT );
468 #ifndef BDB_HIER
469                         if ( slapMode & SLAP_TOOL_READONLY ) {
470                                 flags |= DB_RDONLY;
471                         } else {
472                                 flags |= DB_CREATE;
473                         }
474 #else
475                         if ( slapMode & (SLAP_TOOL_READONLY|SLAP_TOOL_READMAIN) ) {
476                                 flags |= DB_RDONLY;
477                         } else {
478                                 flags |= DB_CREATE;
479                         }
480 #endif
481                         rc = db->bdi_db->set_pagesize( db->bdi_db,
482                                 BDB_PAGESIZE );
483                 }
484
485 #ifdef HAVE_EBCDIC
486                 strcpy( path, bdbi_databases[i].file );
487                 __atoe( path );
488                 rc = DB_OPEN( db->bdi_db,
489                         path,
490                 /*      bdbi_databases[i].name, */ NULL,
491                         bdbi_databases[i].type,
492                         bdbi_databases[i].flags | flags,
493                         bdb->bi_dbenv_mode );
494 #else
495                 rc = DB_OPEN( db->bdi_db,
496                         bdbi_databases[i].file,
497                 /*      bdbi_databases[i].name, */ NULL,
498                         bdbi_databases[i].type,
499                         bdbi_databases[i].flags | flags,
500                         bdb->bi_dbenv_mode );
501 #endif
502
503                 if ( rc != 0 ) {
504                         char    buf[SLAP_TEXT_BUFLEN];
505
506                         snprintf( buf, sizeof(buf), "%s/%s", 
507                                 bdb->bi_dbenv_home, bdbi_databases[i].file );
508                         Debug( LDAP_DEBUG_ANY,
509                                 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
510                                 buf, db_strerror(rc), rc );
511                         return rc;
512                 }
513
514                 flags &= ~(DB_CREATE | DB_RDONLY);
515                 db->bdi_name = bdbi_databases[i].name;
516                 bdb->bi_databases[i] = db;
517         }
518
519         bdb->bi_databases[i] = NULL;
520         bdb->bi_ndatabases = i;
521
522         /* get nextid */
523         rc = bdb_last_id( be, NULL );
524         if( rc != 0 ) {
525                 Debug( LDAP_DEBUG_ANY,
526                         "bdb_db_open: last_id(%s) failed: %s (%d)\n",
527                         bdb->bi_dbenv_home, db_strerror(rc), rc );
528                 return rc;
529         }
530
531         if ( !( slapMode & SLAP_TOOL_QUICK )) {
532                 XLOCK_ID(bdb->bi_dbenv, &bdb->bi_cache.c_locker);
533         }
534
535         bdb->bi_flags |= BDB_IS_OPEN;
536
537         return 0;
538 }
539
540 static int
541 bdb_db_close( BackendDB *be )
542 {
543         int rc;
544         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
545         struct bdb_db_info *db;
546         bdb_idl_cache_entry_t *entry, *next_entry;
547
548         /* backend_shutdown closes everything, even if not all were opened */
549         if ( !( bdb->bi_flags & BDB_IS_OPEN ))
550                 return 0;
551
552         bdb->bi_flags &= ~BDB_IS_OPEN;
553
554         ber_bvarray_free( bdb->bi_db_config );
555         bdb->bi_db_config = NULL;
556
557         while( bdb->bi_databases && bdb->bi_ndatabases-- ) {
558                 db = bdb->bi_databases[bdb->bi_ndatabases];
559                 rc = db->bdi_db->close( db->bdi_db, 0 );
560                 /* Lower numbered names are not strdup'd */
561                 if( bdb->bi_ndatabases >= BDB_NDB )
562                         free( db->bdi_name );
563                 free( db );
564         }
565         free( bdb->bi_databases );
566         bdb->bi_databases = NULL;
567
568         bdb_cache_release_all (&bdb->bi_cache);
569
570         if ( bdb->bi_idl_cache_max_size ) {
571                 ldap_pvt_thread_rdwr_wlock ( &bdb->bi_idl_tree_rwlock );
572                 avl_free( bdb->bi_idl_tree, NULL );
573                 bdb->bi_idl_tree = NULL;
574                 entry = bdb->bi_idl_lru_head;
575                 while ( entry != NULL ) {
576                         next_entry = entry->idl_lru_next;
577                         if ( entry->idl )
578                                 free( entry->idl );
579                         free( entry->kstr.bv_val );
580                         free( entry );
581                         entry = next_entry;
582                 }
583                 bdb->bi_idl_lru_head = bdb->bi_idl_lru_tail = NULL;
584                 ldap_pvt_thread_rdwr_wunlock ( &bdb->bi_idl_tree_rwlock );
585         }
586
587         if ( !( slapMode & SLAP_TOOL_QUICK ) && bdb->bi_dbenv ) {
588                 XLOCK_ID_FREE(bdb->bi_dbenv, bdb->bi_cache.c_locker);
589                 bdb->bi_cache.c_locker = 0;
590         }
591
592         /* close db environment */
593         if( bdb->bi_dbenv ) {
594                 /* force a checkpoint, but not if we were ReadOnly,
595                  * and not in Quick mode since there are no transactions there.
596                  */
597                 if ( !( slapMode & ( SLAP_TOOL_QUICK|SLAP_TOOL_READONLY ))) {
598                         rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
599                         if( rc != 0 ) {
600                                 Debug( LDAP_DEBUG_ANY,
601                                         "bdb_db_close: txn_checkpoint failed: %s (%d)\n",
602                                         db_strerror(rc), rc, 0 );
603                         }
604                 }
605
606                 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
607                 bdb->bi_dbenv = NULL;
608                 if( rc != 0 ) {
609                         Debug( LDAP_DEBUG_ANY,
610                                 "bdb_db_close: close failed: %s (%d)\n",
611                                 db_strerror(rc), rc, 0 );
612                         return rc;
613                 }
614
615 #if DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR == 2
616                 /* Delete the environment if we were in quick mode. This
617                  * works around a bug in bdb4.2 that interferes with the
618                  * operation of db_stat and other tools after a slapadd -q
619                  * or slapindex -q has taken place.
620                  */
621                 if( slapMode & SLAP_TOOL_QUICK ) {
622                         rc = db_env_create( &bdb->bi_dbenv, 0 );
623                         if( rc != 0 ) {
624                                 Debug( LDAP_DEBUG_ANY,
625                                         "bdb_db_close: db_env_create failed: %s (%d)\n",
626                                         db_strerror(rc), rc, 0 );
627                                 return rc;
628                         }
629                         rc = bdb->bi_dbenv->remove(bdb->bi_dbenv, bdb->bi_dbenv_home,
630                                         DB_FORCE);
631                         bdb->bi_dbenv = NULL;
632                         if( rc != 0 ) {
633                                 Debug( LDAP_DEBUG_ANY,
634                                         "bdb_db_close: dbenv_remove failed: %s (%d)\n",
635                                         db_strerror(rc), rc, 0 );
636                                 return rc;
637                         }
638                 }
639 #endif
640         }
641
642         rc = alock_close( &bdb->bi_alock_info );
643         if( rc != 0 ) {
644                 Debug( LDAP_DEBUG_ANY,
645                         "bdb_db_close: alock_close failed\n", 0, 0, 0 );
646                 return -1;
647         }
648
649         return 0;
650 }
651
652 static int
653 bdb_db_destroy( BackendDB *be )
654 {
655         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
656
657         if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
658         if( bdb->bi_db_config_path ) ch_free( bdb->bi_db_config_path );
659
660         bdb_attr_index_destroy( bdb->bi_attrs );
661
662         ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
663         ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_mutex );
664         ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_dntree.bei_kids_mutex );
665 #ifdef BDB_HIER
666         ldap_pvt_thread_mutex_destroy( &bdb->bi_modrdns_mutex );
667 #endif
668         ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
669         ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
670         ldap_pvt_thread_rdwr_destroy( &bdb->bi_idl_tree_rwlock );
671         ldap_pvt_thread_mutex_destroy( &bdb->bi_idl_tree_lrulock );
672
673         ch_free( bdb );
674         be->be_private = NULL;
675
676         return 0;
677 }
678
679 int
680 bdb_back_initialize(
681         BackendInfo     *bi )
682 {
683         int rc;
684
685         static char *controls[] = {
686                 LDAP_CONTROL_ASSERT,
687                 LDAP_CONTROL_MANAGEDSAIT,
688                 LDAP_CONTROL_NOOP,
689                 LDAP_CONTROL_PAGEDRESULTS,
690 #ifdef LDAP_CONTROL_SUBENTRIES
691                 LDAP_CONTROL_SUBENTRIES,
692 #endif
693 #ifdef LDAP_CONTROL_X_PERMISSIVE_MODIFY
694                 LDAP_CONTROL_X_PERMISSIVE_MODIFY,
695 #endif
696                 NULL
697         };
698
699         /* initialize the underlying database system */
700         Debug( LDAP_DEBUG_TRACE,
701                 LDAP_XSTRING(bdb_back_initialize) ": initialize " 
702                 BDB_UCTYPE " backend\n", 0, 0, 0 );
703
704         bi->bi_flags |=
705                 SLAP_BFLAG_INCREMENT |
706 #ifdef BDB_SUBENTRIES
707                 SLAP_BFLAG_SUBENTRIES |
708 #endif
709                 SLAP_BFLAG_ALIASES |
710                 SLAP_BFLAG_REFERRALS;
711
712         bi->bi_controls = controls;
713
714         {       /* version check */
715                 int major, minor, patch, ver;
716                 char *version = db_version( &major, &minor, &patch );
717 #ifdef HAVE_EBCDIC
718                 char v2[1024];
719
720                 /* All our stdio does an ASCII to EBCDIC conversion on
721                  * the output. Strings from the BDB library are already
722                  * in EBCDIC; we have to go back and forth...
723                  */
724                 strcpy( v2, version );
725                 __etoa( v2 );
726                 version = v2;
727 #endif
728
729                 ver = (major << 24) | (minor << 16) | patch;
730                 if( ver != DB_VERSION_FULL ) {
731                         /* fail if a versions don't match */
732                         Debug( LDAP_DEBUG_ANY,
733                                 LDAP_XSTRING(bdb_back_initialize) ": "
734                                 "BDB library version mismatch:"
735                                 " expected " DB_VERSION_STRING ","
736                                 " got %s\n", version, 0, 0 );
737                         return -1;
738                 }
739
740                 Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(bdb_back_initialize)
741                         ": %s\n", version, 0, 0 );
742         }
743
744         db_env_set_func_free( ber_memfree );
745         db_env_set_func_malloc( (db_malloc *)ber_memalloc );
746         db_env_set_func_realloc( (db_realloc *)ber_memrealloc );
747 #ifndef NO_THREAD
748         /* This is a no-op on a NO_THREAD build. Leave the default
749          * alone so that BDB will sleep on interprocess conflicts.
750          */
751         db_env_set_func_yield( ldap_pvt_thread_yield );
752 #endif
753
754         bi->bi_open = 0;
755         bi->bi_close = 0;
756         bi->bi_config = 0;
757         bi->bi_destroy = 0;
758
759         bi->bi_db_init = bdb_db_init;
760         bi->bi_db_config = config_generic_wrapper;
761         bi->bi_db_open = bdb_db_open;
762         bi->bi_db_close = bdb_db_close;
763         bi->bi_db_destroy = bdb_db_destroy;
764
765         bi->bi_op_add = bdb_add;
766         bi->bi_op_bind = bdb_bind;
767         bi->bi_op_compare = bdb_compare;
768         bi->bi_op_delete = bdb_delete;
769         bi->bi_op_modify = bdb_modify;
770         bi->bi_op_modrdn = bdb_modrdn;
771         bi->bi_op_search = bdb_search;
772
773         bi->bi_op_unbind = 0;
774
775         bi->bi_extended = bdb_extended;
776
777         bi->bi_chk_referrals = bdb_referrals;
778         bi->bi_operational = bdb_operational;
779         bi->bi_has_subordinates = bdb_hasSubordinates;
780         bi->bi_entry_release_rw = bdb_entry_release;
781         bi->bi_entry_get_rw = bdb_entry_get;
782
783         /*
784          * hooks for slap tools
785          */
786         bi->bi_tool_entry_open = bdb_tool_entry_open;
787         bi->bi_tool_entry_close = bdb_tool_entry_close;
788         bi->bi_tool_entry_first = bdb_tool_entry_next;
789         bi->bi_tool_entry_next = bdb_tool_entry_next;
790         bi->bi_tool_entry_get = bdb_tool_entry_get;
791         bi->bi_tool_entry_put = bdb_tool_entry_put;
792         bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
793         bi->bi_tool_sync = 0;
794         bi->bi_tool_dn2id_get = bdb_tool_dn2id_get;
795         bi->bi_tool_id2entry_get = bdb_tool_id2entry_get;
796         bi->bi_tool_entry_modify = bdb_tool_entry_modify;
797
798         bi->bi_connection_init = 0;
799         bi->bi_connection_destroy = 0;
800
801         rc = bdb_back_init_cf( bi );
802
803         return rc;
804 }
805
806 #if     (SLAPD_BDB == SLAPD_MOD_DYNAMIC && !defined(BDB_HIER)) || \
807         (SLAPD_HDB == SLAPD_MOD_DYNAMIC && defined(BDB_HIER))
808
809 /* conditionally define the init_module() function */
810 #ifdef BDB_HIER
811 SLAP_BACKEND_INIT_MODULE( hdb )
812 #else /* !BDB_HIER */
813 SLAP_BACKEND_INIT_MODULE( bdb )
814 #endif /* !BDB_HIER */
815
816 #endif /* SLAPD_[BH]DB == SLAPD_MOD_DYNAMIC */
817