]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/init.c
ITS#6095 experimental fix: avoid purging other cases of in-use EntryInfos,
[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                 do_alock_recover = 0;
227                 quick = alockt;
228         }
229
230         /* An existing environment in Quick mode has nothing to recover. */
231         if ( alockt && do_recover ) {
232                 Debug( LDAP_DEBUG_ANY,
233                         LDAP_XSTRING(bdb_db_open) ": database \"%s\": "
234                         "cannot recover, database must be reinitialized.\n", 
235                         be->be_suffix[0].bv_val, 0, 0 );
236                 rc = -1;
237                 goto fail;
238         }
239
240         rc = db_env_create( &bdb->bi_dbenv, 0 );
241         if( rc != 0 ) {
242                 Debug( LDAP_DEBUG_ANY,
243                         LDAP_XSTRING(bdb_db_open) ": database \"%s\": "
244                         "db_env_create failed: %s (%d).\n",
245                         be->be_suffix[0].bv_val, db_strerror(rc), rc );
246                 goto fail;
247         }
248
249 #ifdef HAVE_EBCDIC
250         strcpy( path, bdb->bi_dbenv_home );
251         __atoe( path );
252         dbhome = path;
253 #else
254         dbhome = bdb->bi_dbenv_home;
255 #endif
256
257         /* If existing environment is clean but doesn't support
258          * currently requested modes, remove it.
259          */
260         if ( !do_recover && ( alockt ^ quick )) {
261 shm_retry:
262                 rc = bdb->bi_dbenv->remove( bdb->bi_dbenv, dbhome, DB_FORCE );
263                 if ( rc ) {
264                         Debug( LDAP_DEBUG_ANY,
265                                 LDAP_XSTRING(bdb_db_open) ": database \"%s\": "
266                                 "dbenv remove failed: %s (%d).\n",
267                                 be->be_suffix[0].bv_val, db_strerror(rc), rc );
268                         bdb->bi_dbenv = NULL;
269                         goto fail;
270                 }
271                 rc = db_env_create( &bdb->bi_dbenv, 0 );
272                 if( rc != 0 ) {
273                         Debug( LDAP_DEBUG_ANY,
274                                 LDAP_XSTRING(bdb_db_open) ": database \"%s\": "
275                                 "db_env_create failed: %s (%d).\n",
276                                 be->be_suffix[0].bv_val, db_strerror(rc), rc );
277                         goto fail;
278                 }
279         }
280
281         bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
282         bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
283
284         bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
285
286         if ( !BER_BVISNULL( &bdb->bi_db_crypt_key )) {
287                 rc = bdb->bi_dbenv->set_encrypt( bdb->bi_dbenv, bdb->bi_db_crypt_key.bv_val,
288                         DB_ENCRYPT_AES );
289                 if ( rc ) {
290                         Debug( LDAP_DEBUG_ANY,
291                                 LDAP_XSTRING(bdb_db_open) ": database \"%s\": "
292                                 "dbenv set_encrypt failed: %s (%d).\n",
293                                 be->be_suffix[0].bv_val, db_strerror(rc), rc );
294                         goto fail;
295                 }
296         }
297
298         /* One long-lived TXN per thread, two TXNs per write op */
299         bdb->bi_dbenv->set_tx_max( bdb->bi_dbenv, connection_pool_max * 3 );
300
301         if( bdb->bi_dbenv_xflags != 0 ) {
302                 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
303                         bdb->bi_dbenv_xflags, 1);
304                 if( rc != 0 ) {
305                         Debug( LDAP_DEBUG_ANY,
306                                 LDAP_XSTRING(bdb_db_open) ": database \"%s\": "
307                                 "dbenv_set_flags failed: %s (%d).\n",
308                                 be->be_suffix[0].bv_val, db_strerror(rc), rc );
309                         goto fail;
310                 }
311         }
312
313 #define BDB_TXN_FLAGS   (DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN)
314
315         Debug( LDAP_DEBUG_TRACE,
316                 LDAP_XSTRING(bdb_db_open) ": database \"%s\": "
317                 "dbenv_open(%s).\n",
318                 be->be_suffix[0].bv_val, bdb->bi_dbenv_home, 0);
319
320         flags = DB_INIT_MPOOL | DB_CREATE | DB_THREAD;
321
322         if ( !quick )
323                 flags |= BDB_TXN_FLAGS;
324
325         /* If a key was set, use shared memory for the BDB environment */
326         if ( bdb->bi_shm_key ) {
327                 bdb->bi_dbenv->set_shm_key( bdb->bi_dbenv, bdb->bi_shm_key );
328                 flags |= DB_SYSTEM_MEM;
329         }
330         rc = (bdb->bi_dbenv->open)( bdb->bi_dbenv, dbhome,
331                         flags | do_recover, bdb->bi_dbenv_mode );
332
333         if ( rc ) {
334                 /* Regular open failed, probably a missing shm environment.
335                  * Start over, do a recovery.
336                  */
337                 if ( !do_recover && bdb->bi_shm_key && do_retry ) {
338                         bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
339                         rc = db_env_create( &bdb->bi_dbenv, 0 );
340                         if( rc == 0 ) {
341                                 Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(bdb_db_open)
342                                         ": database \"%s\": "
343                                         "shared memory env open failed, assuming stale env.\n",
344                                         be->be_suffix[0].bv_val, 0, 0 );
345                                 do_retry = 0;
346                                 goto shm_retry;
347                         }
348                 }
349                 Debug( LDAP_DEBUG_ANY,
350                         LDAP_XSTRING(bdb_db_open) ": database \"%s\" cannot be %s, err %d. "
351                         "Restore from backup!\n",
352                         be->be_suffix[0].bv_val, do_recover ? "recovered" : "opened", rc );
353                 goto fail;
354         }
355
356         if ( do_alock_recover && alock_recover (&bdb->bi_alock_info) != 0 ) {
357                 Debug( LDAP_DEBUG_ANY,
358                         LDAP_XSTRING(bdb_db_open) ": database \"%s\": alock_recover failed\n",
359                         be->be_suffix[0].bv_val, 0, 0 );
360                 rc = -1;
361                 goto fail;
362         }
363
364 #ifdef SLAP_ZONE_ALLOC
365         if ( bdb->bi_cache.c_maxsize ) {
366                 bdb->bi_cache.c_zctx = slap_zn_mem_create(
367                         SLAP_ZONE_INITSIZE, SLAP_ZONE_MAXSIZE,
368                         SLAP_ZONE_DELTA, SLAP_ZONE_SIZE);
369         }
370 #endif
371
372         /* Default dncache to 2x entrycache */
373         if ( bdb->bi_cache.c_maxsize && !bdb->bi_cache.c_eimax ) {
374                 bdb->bi_cache.c_eimax = bdb->bi_cache.c_maxsize * 2;
375         }
376
377         if ( bdb->bi_idl_cache_max_size ) {
378                 bdb->bi_idl_tree = NULL;
379                 bdb->bi_idl_cache_size = 0;
380         }
381
382         flags = DB_THREAD | bdb->bi_db_opflags;
383
384 #ifdef DB_AUTO_COMMIT
385         if ( !quick )
386                 flags |= DB_AUTO_COMMIT;
387 #endif
388
389         bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
390                 BDB_INDICES * sizeof(struct bdb_db_info *) );
391
392         /* open (and create) main database */
393         for( i = 0; bdbi_databases[i].name.bv_val; i++ ) {
394                 struct bdb_db_info *db;
395
396                 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
397
398                 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
399                 if( rc != 0 ) {
400                         snprintf(cr->msg, sizeof(cr->msg),
401                                 "database \"%s\": db_create(%s) failed: %s (%d).",
402                                 be->be_suffix[0].bv_val, 
403                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
404                         Debug( LDAP_DEBUG_ANY,
405                                 LDAP_XSTRING(bdb_db_open) ": %s\n",
406                                 cr->msg, 0, 0 );
407                         goto fail;
408                 }
409
410                 if( !BER_BVISNULL( &bdb->bi_db_crypt_key )) {
411                         rc = db->bdi_db->set_flags( db->bdi_db, DB_ENCRYPT );
412                         if ( rc ) {
413                                 snprintf(cr->msg, sizeof(cr->msg),
414                                         "database \"%s\": db set_flags(DB_ENCRYPT)(%s) failed: %s (%d).",
415                                         be->be_suffix[0].bv_val, 
416                                         bdb->bi_dbenv_home, db_strerror(rc), rc );
417                                 Debug( LDAP_DEBUG_ANY,
418                                         LDAP_XSTRING(bdb_db_open) ": %s\n",
419                                         cr->msg, 0, 0 );
420                                 goto fail;
421                         }
422                 }
423
424                 if( bdb->bi_flags & BDB_CHKSUM ) {
425                         rc = db->bdi_db->set_flags( db->bdi_db, DB_CHKSUM );
426                         if ( rc ) {
427                                 snprintf(cr->msg, sizeof(cr->msg),
428                                         "database \"%s\": db set_flags(DB_CHKSUM)(%s) failed: %s (%d).",
429                                         be->be_suffix[0].bv_val, 
430                                         bdb->bi_dbenv_home, db_strerror(rc), rc );
431                                 Debug( LDAP_DEBUG_ANY,
432                                         LDAP_XSTRING(bdb_db_open) ": %s\n",
433                                         cr->msg, 0, 0 );
434                                 goto fail;
435                         }
436                 }
437
438                 rc = bdb_db_findsize( bdb, (struct berval *)&bdbi_databases[i].name );
439
440                 if( i == BDB_ID2ENTRY ) {
441                         if ( !rc ) rc = BDB_ID2ENTRY_PAGESIZE;
442                         rc = db->bdi_db->set_pagesize( db->bdi_db, rc );
443
444                         if ( slapMode & SLAP_TOOL_MODE )
445                                 db->bdi_db->mpf->set_priority( db->bdi_db->mpf,
446                                         DB_PRIORITY_VERY_LOW );
447
448                         if ( slapMode & SLAP_TOOL_READMAIN ) {
449                                 flags |= DB_RDONLY;
450                         } else {
451                                 flags |= DB_CREATE;
452                         }
453                 } else {
454                         /* Use FS default size if not configured */
455                         if ( rc )
456                                 rc = db->bdi_db->set_pagesize( db->bdi_db, rc );
457
458                         rc = db->bdi_db->set_flags( db->bdi_db, 
459                                 DB_DUP | DB_DUPSORT );
460 #ifndef BDB_HIER
461                         if ( slapMode & SLAP_TOOL_READONLY ) {
462                                 flags |= DB_RDONLY;
463                         } else {
464                                 flags |= DB_CREATE;
465                         }
466 #else
467                         rc = db->bdi_db->set_dup_compare( db->bdi_db,
468                                 bdb_dup_compare );
469                         if ( slapMode & (SLAP_TOOL_READONLY|SLAP_TOOL_READMAIN) ) {
470                                 flags |= DB_RDONLY;
471                         } else {
472                                 flags |= DB_CREATE;
473                         }
474 #endif
475                 }
476
477 #ifdef HAVE_EBCDIC
478                 strcpy( path, bdbi_databases[i].file );
479                 __atoe( path );
480                 rc = DB_OPEN( db->bdi_db,
481                         path,
482                 /*      bdbi_databases[i].name, */ NULL,
483                         bdbi_databases[i].type,
484                         bdbi_databases[i].flags | flags,
485                         bdb->bi_dbenv_mode );
486 #else
487                 rc = DB_OPEN( db->bdi_db,
488                         bdbi_databases[i].file,
489                 /*      bdbi_databases[i].name, */ NULL,
490                         bdbi_databases[i].type,
491                         bdbi_databases[i].flags | flags,
492                         bdb->bi_dbenv_mode );
493 #endif
494
495                 if ( rc != 0 ) {
496                         snprintf( cr->msg, sizeof(cr->msg), "database \"%s\": "
497                                 "db_open(%s/%s) failed: %s (%d).", 
498                                 be->be_suffix[0].bv_val, 
499                                 bdb->bi_dbenv_home, bdbi_databases[i].file,
500                                 db_strerror(rc), rc );
501                         Debug( LDAP_DEBUG_ANY,
502                                 LDAP_XSTRING(bdb_db_open) ": %s\n",
503                                 cr->msg, 0, 0 );
504                         db->bdi_db->close( db->bdi_db, 0 );
505                         goto fail;
506                 }
507
508                 flags &= ~(DB_CREATE | DB_RDONLY);
509                 db->bdi_name = bdbi_databases[i].name;
510                 bdb->bi_databases[i] = db;
511         }
512
513         bdb->bi_databases[i] = NULL;
514         bdb->bi_ndatabases = i;
515
516         /* get nextid */
517         rc = bdb_last_id( be, NULL );
518         if( rc != 0 ) {
519                 snprintf( cr->msg, sizeof(cr->msg), "database \"%s\": "
520                         "last_id(%s) failed: %s (%d).",
521                         be->be_suffix[0].bv_val, bdb->bi_dbenv_home,
522                         db_strerror(rc), rc );
523                 Debug( LDAP_DEBUG_ANY,
524                         LDAP_XSTRING(bdb_db_open) ": %s\n",
525                         cr->msg, 0, 0 );
526                 goto fail;
527         }
528
529         if ( !quick ) {
530                 TXN_BEGIN(bdb->bi_dbenv, NULL, &bdb->bi_cache.c_txn, DB_READ_COMMITTED | DB_TXN_NOWAIT);
531         }
532
533         entry_prealloc( bdb->bi_cache.c_maxsize );
534         attr_prealloc( bdb->bi_cache.c_maxsize * 20 );
535
536         /* setup for empty-DN contexts */
537         if ( BER_BVISEMPTY( &be->be_nsuffix[0] )) {
538                 rc = bdb_id2entry( be, NULL, 0, &e );
539         }
540         if ( !e ) {
541                 struct berval gluebv = BER_BVC("glue");
542                 Operation op = {0};
543                 Opheader ohdr = {0};
544                 e = entry_alloc();
545                 e->e_id = 0;
546                 ber_dupbv( &e->e_name, (struct berval *)&slap_empty_bv );
547                 ber_dupbv( &e->e_nname, (struct berval *)&slap_empty_bv );
548                 attr_merge_one( e, slap_schema.si_ad_objectClass,
549                         &gluebv, NULL );
550                 attr_merge_one( e, slap_schema.si_ad_structuralObjectClass,
551                         &gluebv, NULL );
552                 op.o_hdr = &ohdr;
553                 op.o_bd = be;
554                 op.ora_e = e;
555                 op.o_dn = be->be_rootdn;
556                 op.o_ndn = be->be_rootndn;
557                 slap_add_opattrs( &op, NULL, NULL, 0, 0 );
558         }
559         e->e_ocflags = SLAP_OC_GLUE|SLAP_OC__END;
560         e->e_private = &bdb->bi_cache.c_dntree;
561         bdb->bi_cache.c_dntree.bei_e = e;
562
563         /* monitor setup */
564         rc = bdb_monitor_db_open( be );
565         if ( rc != 0 ) {
566                 goto fail;
567         }
568
569         bdb->bi_flags |= BDB_IS_OPEN;
570
571         return 0;
572
573 fail:
574         bdb_db_close( be, NULL );
575         return rc;
576 }
577
578 static int
579 bdb_db_close( BackendDB *be, ConfigReply *cr )
580 {
581         int rc;
582         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
583         struct bdb_db_info *db;
584         bdb_idl_cache_entry_t *entry, *next_entry;
585
586         /* monitor handling */
587         (void)bdb_monitor_db_close( be );
588
589         {
590                 Entry *e = bdb->bi_cache.c_dntree.bei_e;
591                 if ( e ) {
592                         bdb->bi_cache.c_dntree.bei_e = NULL;
593                         e->e_private = NULL;
594                         bdb_entry_return( e );
595                 }
596         }
597
598         bdb->bi_flags &= ~BDB_IS_OPEN;
599
600         ber_bvarray_free( bdb->bi_db_config );
601         bdb->bi_db_config = NULL;
602
603         if( bdb->bi_dbenv ) {
604                 /* Free cache locker if we enabled locking.
605                  * TXNs must all be closed before DBs...
606                  */
607                 if ( !( slapMode & SLAP_TOOL_QUICK ) && bdb->bi_cache.c_txn ) {
608                         TXN_ABORT( bdb->bi_cache.c_txn );
609                         bdb->bi_cache.c_txn = NULL;
610                 }
611                 bdb_reader_flush( bdb->bi_dbenv );
612         }
613
614         while( bdb->bi_databases && bdb->bi_ndatabases-- ) {
615                 db = bdb->bi_databases[bdb->bi_ndatabases];
616                 rc = db->bdi_db->close( db->bdi_db, 0 );
617                 /* Lower numbered names are not strdup'd */
618                 if( bdb->bi_ndatabases >= BDB_NDB )
619                         free( db->bdi_name.bv_val );
620                 free( db );
621         }
622         free( bdb->bi_databases );
623         bdb->bi_databases = NULL;
624
625         bdb_cache_release_all (&bdb->bi_cache);
626
627         if ( bdb->bi_idl_cache_size ) {
628                 avl_free( bdb->bi_idl_tree, NULL );
629                 bdb->bi_idl_tree = NULL;
630                 entry = bdb->bi_idl_lru_head;
631                 do {
632                         next_entry = entry->idl_lru_next;
633                         if ( entry->idl )
634                                 free( entry->idl );
635                         free( entry->kstr.bv_val );
636                         free( entry );
637                         entry = next_entry;
638                 } while ( entry != bdb->bi_idl_lru_head );
639                 bdb->bi_idl_lru_head = bdb->bi_idl_lru_tail = NULL;
640         }
641
642         /* close db environment */
643         if( bdb->bi_dbenv ) {
644                 /* force a checkpoint, but not if we were ReadOnly,
645                  * and not in Quick mode since there are no transactions there.
646                  */
647                 if ( !( slapMode & ( SLAP_TOOL_QUICK|SLAP_TOOL_READONLY ))) {
648                         rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
649                         if( rc != 0 ) {
650                                 Debug( LDAP_DEBUG_ANY,
651                                         "bdb_db_close: database \"%s\": "
652                                         "txn_checkpoint failed: %s (%d).\n",
653                                         be->be_suffix[0].bv_val, db_strerror(rc), rc );
654                         }
655                 }
656
657                 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
658                 bdb->bi_dbenv = NULL;
659                 if( rc != 0 ) {
660                         Debug( LDAP_DEBUG_ANY,
661                                 "bdb_db_close: database \"%s\": "
662                                 "close failed: %s (%d)\n",
663                                 be->be_suffix[0].bv_val, db_strerror(rc), rc );
664                         return rc;
665                 }
666         }
667
668         rc = alock_close( &bdb->bi_alock_info, slapMode & SLAP_TOOL_QUICK );
669         if( rc != 0 ) {
670                 Debug( LDAP_DEBUG_ANY,
671                         "bdb_db_close: database \"%s\": alock_close failed\n",
672                         be->be_suffix[0].bv_val, 0, 0 );
673                 return -1;
674         }
675
676         return 0;
677 }
678
679 static int
680 bdb_db_destroy( BackendDB *be, ConfigReply *cr )
681 {
682         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
683
684         /* stop and remove checkpoint task */
685         if ( bdb->bi_txn_cp_task ) {
686                 struct re_s *re = bdb->bi_txn_cp_task;
687                 bdb->bi_txn_cp_task = NULL;
688                 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
689                 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ) )
690                         ldap_pvt_runqueue_stoptask( &slapd_rq, re );
691                 ldap_pvt_runqueue_remove( &slapd_rq, re );
692                 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
693         }
694
695         /* monitor handling */
696         (void)bdb_monitor_db_destroy( be );
697
698         if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
699         if( bdb->bi_db_config_path ) ch_free( bdb->bi_db_config_path );
700
701         bdb_attr_index_destroy( bdb );
702
703         ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
704         ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_lru_mutex );
705         ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_count_mutex );
706         ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_eifree_mutex );
707         ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_dntree.bei_kids_mutex );
708 #ifdef BDB_HIER
709         ldap_pvt_thread_mutex_destroy( &bdb->bi_modrdns_mutex );
710 #endif
711         ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
712         ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
713         ldap_pvt_thread_rdwr_destroy( &bdb->bi_idl_tree_rwlock );
714         ldap_pvt_thread_mutex_destroy( &bdb->bi_idl_tree_lrulock );
715
716         ch_free( bdb );
717         be->be_private = NULL;
718
719         return 0;
720 }
721
722 int
723 bdb_back_initialize(
724         BackendInfo     *bi )
725 {
726         int rc;
727
728         static char *controls[] = {
729                 LDAP_CONTROL_ASSERT,
730                 LDAP_CONTROL_MANAGEDSAIT,
731                 LDAP_CONTROL_NOOP,
732                 LDAP_CONTROL_PAGEDRESULTS,
733                 LDAP_CONTROL_PRE_READ,
734                 LDAP_CONTROL_POST_READ,
735                 LDAP_CONTROL_SUBENTRIES,
736                 LDAP_CONTROL_X_PERMISSIVE_MODIFY,
737 #ifdef LDAP_X_TXN
738                 LDAP_CONTROL_X_TXN_SPEC,
739 #endif
740                 NULL
741         };
742
743         /* initialize the underlying database system */
744         Debug( LDAP_DEBUG_TRACE,
745                 LDAP_XSTRING(bdb_back_initialize) ": initialize " 
746                 BDB_UCTYPE " backend\n", 0, 0, 0 );
747
748         bi->bi_flags |=
749                 SLAP_BFLAG_INCREMENT |
750                 SLAP_BFLAG_SUBENTRIES |
751                 SLAP_BFLAG_ALIASES |
752                 SLAP_BFLAG_REFERRALS;
753
754         bi->bi_controls = controls;
755
756         {       /* version check */
757                 int major, minor, patch, ver;
758                 char *version = db_version( &major, &minor, &patch );
759 #ifdef HAVE_EBCDIC
760                 char v2[1024];
761
762                 /* All our stdio does an ASCII to EBCDIC conversion on
763                  * the output. Strings from the BDB library are already
764                  * in EBCDIC; we have to go back and forth...
765                  */
766                 strcpy( v2, version );
767                 __etoa( v2 );
768                 version = v2;
769 #endif
770
771                 ver = (major << 24) | (minor << 16) | patch;
772                 if( ver != DB_VERSION_FULL ) {
773                         /* fail if a versions don't match */
774                         Debug( LDAP_DEBUG_ANY,
775                                 LDAP_XSTRING(bdb_back_initialize) ": "
776                                 "BDB library version mismatch:"
777                                 " expected " DB_VERSION_STRING ","
778                                 " got %s\n", version, 0, 0 );
779                         return -1;
780                 }
781
782                 Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(bdb_back_initialize)
783                         ": %s\n", version, 0, 0 );
784         }
785
786         db_env_set_func_free( ber_memfree );
787         db_env_set_func_malloc( (db_malloc *)ber_memalloc );
788         db_env_set_func_realloc( (db_realloc *)ber_memrealloc );
789 #if !defined(NO_THREAD) && DB_VERSION_FULL <= 0x04070000
790         /* This is a no-op on a NO_THREAD build. Leave the default
791          * alone so that BDB will sleep on interprocess conflicts.
792          * Don't bother on BDB 4.7...
793          */
794         db_env_set_func_yield( ldap_pvt_thread_yield );
795 #endif
796
797         bi->bi_open = 0;
798         bi->bi_close = 0;
799         bi->bi_config = 0;
800         bi->bi_destroy = 0;
801
802         bi->bi_db_init = bdb_db_init;
803         bi->bi_db_config = config_generic_wrapper;
804         bi->bi_db_open = bdb_db_open;
805         bi->bi_db_close = bdb_db_close;
806         bi->bi_db_destroy = bdb_db_destroy;
807
808         bi->bi_op_add = bdb_add;
809         bi->bi_op_bind = bdb_bind;
810         bi->bi_op_compare = bdb_compare;
811         bi->bi_op_delete = bdb_delete;
812         bi->bi_op_modify = bdb_modify;
813         bi->bi_op_modrdn = bdb_modrdn;
814         bi->bi_op_search = bdb_search;
815
816         bi->bi_op_unbind = 0;
817
818         bi->bi_extended = bdb_extended;
819
820         bi->bi_chk_referrals = bdb_referrals;
821         bi->bi_operational = bdb_operational;
822         bi->bi_has_subordinates = bdb_hasSubordinates;
823         bi->bi_entry_release_rw = bdb_entry_release;
824         bi->bi_entry_get_rw = bdb_entry_get;
825
826         /*
827          * hooks for slap tools
828          */
829         bi->bi_tool_entry_open = bdb_tool_entry_open;
830         bi->bi_tool_entry_close = bdb_tool_entry_close;
831         bi->bi_tool_entry_first = bdb_tool_entry_next;
832         bi->bi_tool_entry_next = bdb_tool_entry_next;
833         bi->bi_tool_entry_get = bdb_tool_entry_get;
834         bi->bi_tool_entry_put = bdb_tool_entry_put;
835         bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
836         bi->bi_tool_sync = 0;
837         bi->bi_tool_dn2id_get = bdb_tool_dn2id_get;
838         bi->bi_tool_entry_modify = bdb_tool_entry_modify;
839
840         bi->bi_connection_init = 0;
841         bi->bi_connection_destroy = 0;
842
843         rc = bdb_back_init_cf( bi );
844
845         return rc;
846 }
847
848 #if     (SLAPD_BDB == SLAPD_MOD_DYNAMIC && !defined(BDB_HIER)) || \
849         (SLAPD_HDB == SLAPD_MOD_DYNAMIC && defined(BDB_HIER))
850
851 /* conditionally define the init_module() function */
852 #ifdef BDB_HIER
853 SLAP_BACKEND_INIT_MODULE( hdb )
854 #else /* !BDB_HIER */
855 SLAP_BACKEND_INIT_MODULE( bdb )
856 #endif /* !BDB_HIER */
857
858 #endif /* SLAPD_[BH]DB == SLAPD_MOD_DYNAMIC */
859