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