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