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