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