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