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