]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/config.c
cache LRU concurrency improvements, reorder DB accesses for consistency
[openldap] / servers / slapd / back-bdb / config.c
1 /* config.c - bdb backend configuration file routine */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2000-2005 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/ctype.h>
21 #include <ac/string.h>
22
23 #include "back-bdb.h"
24
25 #include "config.h"
26
27 #include "lutil.h"
28 #include "ldap_rq.h"
29
30 #ifdef DB_DIRTY_READ
31 #       define  SLAP_BDB_ALLOW_DIRTY_READ
32 #endif
33
34 #define bdb_cf_gen                      BDB_SYMBOL(cf_gen)
35 #define bdb_cf_cleanup          BDB_SYMBOL(cf_cleanup)
36 #define bdb_checkpoint          BDB_SYMBOL(checkpoint)
37 #define bdb_online_index        BDB_SYMBOL(online_index)
38
39 static ConfigDriver bdb_cf_gen;
40
41 enum {
42         BDB_CHKPT = 1,
43         BDB_CONFIG,
44         BDB_DIRECTORY,
45         BDB_NOSYNC,
46         BDB_DIRTYR,
47         BDB_INDEX,
48         BDB_LOCKD,
49         BDB_SSTACK
50 };
51
52 static ConfigTable bdbcfg[] = {
53         { "directory", "dir", 2, 2, 0, ARG_STRING|ARG_MAGIC|BDB_DIRECTORY,
54                 bdb_cf_gen, "( OLcfgDbAt:0.1 NAME 'olcDbDirectory' "
55                         "DESC 'Directory for database content' "
56                         "EQUALITY caseIgnoreMatch "
57                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
58         { "cachefree", "size", 2, 2, 0, ARG_INT|ARG_OFFSET,
59                 (void *)offsetof(struct bdb_info, bi_cache.c_minfree),
60                 "( OLcfgDbAt:1.11 NAME 'olcDbCacheFree' "
61                         "DESC 'Number of extra entries to free when max is reached' "
62                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
63         { "cachesize", "size", 2, 2, 0, ARG_INT|ARG_OFFSET,
64                 (void *)offsetof(struct bdb_info, bi_cache.c_maxsize),
65                 "( OLcfgDbAt:1.1 NAME 'olcDbCacheSize' "
66                         "DESC 'Entry cache size in entries' "
67                         "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
68         { "checkpoint", "kbyte> <min", 3, 3, 0, ARG_MAGIC|BDB_CHKPT,
69                 bdb_cf_gen, "( OLcfgDbAt:1.2 NAME 'olcDbCheckpoint' "
70                         "DESC 'Database checkpoint interval in kbytes and minutes' "
71                         "SYNTAX OMsDirectoryString SINGLE-VALUE )",NULL, NULL },
72         { "dbconfig", "DB_CONFIG setting", 1, 0, 0, ARG_MAGIC|BDB_CONFIG,
73                 bdb_cf_gen, "( OLcfgDbAt:1.3 NAME 'olcDbConfig' "
74                         "DESC 'BerkeleyDB DB_CONFIG configuration directives' "
75                         "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
76         { "dbnosync", NULL, 1, 2, 0, ARG_ON_OFF|ARG_MAGIC|BDB_NOSYNC,
77                 bdb_cf_gen, "( OLcfgDbAt:1.4 NAME 'olcDbNoSync' "
78                         "DESC 'Disable synchronous database writes' "
79                         "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
80         { "dirtyread", NULL, 1, 2, 0,
81 #ifdef SLAP_BDB_ALLOW_DIRTY_READ
82                 ARG_ON_OFF|ARG_MAGIC|BDB_DIRTYR, bdb_cf_gen,
83 #else
84                 ARG_IGNORED, NULL,
85 #endif
86                 "( OLcfgDbAt:1.5 NAME 'olcDbDirtyRead' "
87                 "DESC 'Allow reads of uncommitted data' "
88                 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
89         { "idlcachesize", "size", 2, 2, 0, ARG_INT|ARG_OFFSET,
90                 (void *)offsetof(struct bdb_info,bi_idl_cache_max_size),
91                 "( OLcfgDbAt:1.6 NAME 'olcDbIDLcacheSize' "
92                 "DESC 'IDL cache size in IDLs' "
93                 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
94         { "index", "attr> <[pres,eq,approx,sub]", 2, 3, 0, ARG_MAGIC|BDB_INDEX,
95                 bdb_cf_gen, "( OLcfgDbAt:0.2 NAME 'olcDbIndex' "
96                 "DESC 'Attribute index parameters' "
97                 "EQUALITY caseIgnoreMatch "
98                 "SYNTAX OMsDirectoryString )", NULL, NULL },
99         { "linearindex", NULL, 1, 2, 0, ARG_ON_OFF|ARG_OFFSET,
100                 (void *)offsetof(struct bdb_info, bi_linear_index), 
101                 "( OLcfgDbAt:1.7 NAME 'olcDbLinearIndex' "
102                 "DESC 'Index attributes one at a time' "
103                 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
104         { "lockdetect", "policy", 2, 2, 0, ARG_MAGIC|BDB_LOCKD,
105                 bdb_cf_gen, "( OLcfgDbAt:1.8 NAME 'olcDbLockDetect' "
106                 "DESC 'Deadlock detection algorithm' "
107                 "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
108         { "mode", "mode", 2, 2, 0, ARG_INT|ARG_OFFSET,
109                 (void *)offsetof(struct bdb_info, bi_dbenv_mode),
110                 "( OLcfgDbAt:0.3 NAME 'olcDbMode' "
111                 "DESC 'Unix permissions of database files' "
112                 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
113         { "searchstack", "depth", 2, 2, 0, ARG_INT|ARG_MAGIC|BDB_SSTACK,
114                 bdb_cf_gen, "( OLcfgDbAt:1.9 NAME 'olcDbSearchStack' "
115                 "DESC 'Depth of search stack in IDLs' "
116                 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
117         { "shm_key", "key", 2, 2, 0, ARG_INT|ARG_OFFSET,
118                 (void *)offsetof(struct bdb_info, bi_shm_key), 
119                 "( OLcfgDbAt:1.10 NAME 'olcDbShmKey' "
120                 "DESC 'Key for shared memory region' "
121                 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
122         { NULL, NULL, 0, 0, 0, ARG_IGNORED,
123                 NULL, NULL, NULL, NULL }
124 };
125
126 static ConfigOCs bdbocs[] = {
127         {
128 #ifdef BDB_HIER
129                 "( OLcfgDbOc:1.2 "
130                 "NAME 'olcHdbConfig' "
131                 "DESC 'HDB backend configuration' "
132 #else
133                 "( OLcfgDbOc:1.1 "
134                 "NAME 'olcBdbConfig' "
135                 "DESC 'BDB backend configuration' "
136 #endif
137                 "SUP olcDatabaseConfig "
138                 "MUST olcDbDirectory "
139                 "MAY ( olcDbCacheSize $ olcDbCheckpoint $ olcDbConfig $ "
140                 "olcDbNoSync $ olcDbDirtyRead $ olcDbIDLcacheSize $ "
141                 "olcDbIndex $ olcDbLinearIndex $ olcDbLockDetect $ "
142                 "olcDbMode $ olcDbSearchStack $ olcDbShmKey $ "
143                 " olcDbCacheFree ) )",
144                         Cft_Database, bdbcfg },
145         { NULL, 0, NULL }
146 };
147
148 static slap_verbmasks bdb_lockd[] = {
149         { BER_BVC("default"), DB_LOCK_DEFAULT },
150         { BER_BVC("oldest"), DB_LOCK_OLDEST },
151         { BER_BVC("random"), DB_LOCK_RANDOM },
152         { BER_BVC("youngest"), DB_LOCK_YOUNGEST },
153         { BER_BVC("fewest"), DB_LOCK_MINLOCKS },
154         { BER_BVNULL, 0 }
155 };
156
157 /* perform periodic checkpoints */
158 static void *
159 bdb_checkpoint( void *ctx, void *arg )
160 {
161         struct re_s *rtask = arg;
162         struct bdb_info *bdb = rtask->arg;
163         
164         TXN_CHECKPOINT( bdb->bi_dbenv, bdb->bi_txn_cp_kbyte,
165                 bdb->bi_txn_cp_min, 0 );
166         ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
167         ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
168         ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
169         return NULL;
170 }
171
172 /* reindex entries on the fly */
173 static void *
174 bdb_online_index( void *ctx, void *arg )
175 {
176         struct re_s *rtask = arg;
177         BackendDB *be = rtask->arg;
178         struct bdb_info *bdb = be->be_private;
179
180         Connection conn = {0};
181         OperationBuffer opbuf;
182         Operation *op = (Operation *) &opbuf;
183
184         DBC *curs;
185         DBT key, data;
186         DB_TXN *txn;
187         DB_LOCK lock;
188         u_int32_t locker;
189         ID id, nid;
190         EntryInfo *ei;
191         int rc, getnext = 1;
192
193         connection_fake_init( &conn, op, ctx );
194
195         op->o_bd = be;
196
197         DBTzero( &key );
198         DBTzero( &data );
199         
200         id = 1;
201         key.data = &nid;
202         key.size = key.ulen = sizeof(ID);
203         key.flags = DB_DBT_USERMEM;
204
205         data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
206         data.dlen = data.ulen = 0;
207
208         while ( 1 ) {
209                 if ( slapd_shutdown )
210                         break;
211
212                 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &txn, bdb->bi_db_opflags );
213                 if ( rc ) 
214                         break;
215                 locker = TXN_ID( txn );
216                 if ( getnext ) {
217                         getnext = 0;
218                         BDB_ID2DISK( id, &nid );
219                         rc = bdb->bi_id2entry->bdi_db->cursor(
220                                 bdb->bi_id2entry->bdi_db, txn, &curs, bdb->bi_db_opflags );
221                         if ( rc ) {
222                                 TXN_ABORT( txn );
223                                 break;
224                         }
225                         rc = curs->c_get( curs, &key, &data, DB_SET_RANGE );
226                         curs->c_close( curs );
227                         if ( rc ) {
228                                 TXN_ABORT( txn );
229                                 if ( rc == DB_NOTFOUND )
230                                         rc = 0;
231                                 if ( rc == DB_LOCK_DEADLOCK ) {
232                                         ldap_pvt_thread_yield();
233                                         continue;
234                                 }
235                                 break;
236                         }
237                         BDB_DISK2ID( &nid, &id );
238                 }
239
240                 ei = NULL;
241                 rc = bdb_cache_find_id( op, txn, id, &ei, 0, locker, &lock );
242                 if ( rc ) {
243                         TXN_ABORT( txn );
244                         if ( rc == DB_LOCK_DEADLOCK ) {
245                                 ldap_pvt_thread_yield();
246                                 continue;
247                         }
248                         if ( rc == DB_NOTFOUND ) {
249                                 id++;
250                                 getnext = 1;
251                                 continue;
252                         }
253                         break;
254                 }
255                 if ( ei->bei_e ) {
256                         rc = bdb_index_entry( op, txn, BDB_INDEX_UPDATE_OP, ei->bei_e );
257                         if ( rc == DB_LOCK_DEADLOCK ) {
258                                 TXN_ABORT( txn );
259                                 ldap_pvt_thread_yield();
260                                 continue;
261                         }
262                         if ( rc == 0 ) {
263                                 rc = TXN_COMMIT( txn, 0 );
264                                 txn = NULL;
265                         }
266                         if ( rc )
267                                 break;
268                 }
269                 id++;
270                 getnext = 1;
271         }
272
273         ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
274         ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
275         bdb->bi_index_task = NULL;
276         ldap_pvt_runqueue_remove( &slapd_rq, rtask );
277         ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
278
279         return NULL;
280 }
281
282 /* Cleanup loose ends after Modify completes */
283 static int
284 bdb_cf_cleanup( ConfigArgs *c )
285 {
286         struct bdb_info *bdb = c->be->be_private;
287         int rc = 0;
288
289         if ( bdb->bi_flags & BDB_UPD_CONFIG ) {
290                 if ( bdb->bi_db_config ) {
291                         int i;
292                         FILE *f = fopen( bdb->bi_db_config_path, "w" );
293                         if ( f ) {
294                                 for (i=0; bdb->bi_db_config[i].bv_val; i++)
295                                         fprintf( f, "%s\n", bdb->bi_db_config[i].bv_val );
296                                 fclose( f );
297                         }
298                 } else {
299                         unlink( bdb->bi_db_config_path );
300                 }
301                 bdb->bi_flags ^= BDB_UPD_CONFIG;
302         }
303
304         if ( bdb->bi_flags & BDB_DEL_INDEX ) {
305                 bdb_attr_flush( bdb );
306                 bdb->bi_flags ^= BDB_DEL_INDEX;
307         }
308         
309         if ( bdb->bi_flags & BDB_RE_OPEN ) {
310                 bdb->bi_flags ^= BDB_RE_OPEN;
311                 rc = c->be->bd_info->bi_db_close( c->be );
312                 if ( rc == 0 )
313                         rc = c->be->bd_info->bi_db_open( c->be );
314                 /* If this fails, we need to restart */
315                 if ( rc ) {
316                         slapd_shutdown = 2;
317                         Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(bdb_cf_cleanup)
318                                 ": failed to reopen database, rc=%d", rc, 0, 0 );
319                 }
320         }
321         return rc;
322 }
323
324 static int
325 bdb_cf_gen(ConfigArgs *c)
326 {
327         struct bdb_info *bdb = c->be->be_private;
328         int rc;
329
330         if ( c->op == SLAP_CONFIG_EMIT ) {
331                 rc = 0;
332                 switch( c->type ) {
333                 case BDB_CHKPT:
334                         if (bdb->bi_txn_cp ) {
335                                 char buf[64];
336                                 struct berval bv;
337                                 bv.bv_len = sprintf( buf, "%d %d", bdb->bi_txn_cp_kbyte,
338                                         bdb->bi_txn_cp_min );
339                                 bv.bv_val = buf;
340                                 value_add_one( &c->rvalue_vals, &bv );
341                         } else{
342                                 rc = 1;
343                         }
344                         break;
345
346                 case BDB_DIRECTORY:
347                         if ( bdb->bi_dbenv_home ) {
348                                 c->value_string = ch_strdup( bdb->bi_dbenv_home );
349                         } else {
350                                 rc = 1;
351                         }
352                         break;
353
354                 case BDB_CONFIG:
355                         if (( slapMode&SLAP_SERVER_MODE ) && !( bdb->bi_flags&BDB_IS_OPEN )
356                                 && !bdb->bi_db_config ) {
357                                 char    buf[SLAP_TEXT_BUFLEN];
358                                 FILE *f = fopen( bdb->bi_db_config_path, "r" );
359                                 struct berval bv;
360
361                                 if ( f ) {
362                                         bdb->bi_flags |= BDB_HAS_CONFIG;
363                                         while ( fgets( buf, sizeof(buf), f )) {
364                                                 ber_str2bv( buf, 0, 1, &bv );
365                                                 if ( bv.bv_len > 0 && bv.bv_val[bv.bv_len-1] == '\n' ) {
366                                                         bv.bv_len--;
367                                                         bv.bv_val[bv.bv_len] = '\0';
368                                                 }
369                                                 /* shouldn't need this, but ... */
370                                                 if ( bv.bv_len > 0 && bv.bv_val[bv.bv_len-1] == '\r' ) {
371                                                         bv.bv_len--;
372                                                         bv.bv_val[bv.bv_len] = '\0';
373                                                 }
374                                                 ber_bvarray_add( &bdb->bi_db_config, &bv );
375                                         }
376                                         fclose( f );
377                                 }
378                         }
379                         if ( bdb->bi_db_config ) {
380                                 int i;
381                                 struct berval bv;
382
383                                 bv.bv_val = c->log;
384                                 for (i=0; !BER_BVISNULL(&bdb->bi_db_config[i]); i++) {
385                                         bv.bv_len = sprintf( bv.bv_val, "{%d}%s", i,
386                                                 bdb->bi_db_config[i].bv_val );
387                                         value_add_one( &c->rvalue_vals, &bv );
388                                 }
389                         }
390                         if ( !c->rvalue_vals ) rc = 1;
391                         break;
392
393                 case BDB_NOSYNC:
394                         if ( bdb->bi_dbenv_xflags & DB_TXN_NOSYNC )
395                                 c->value_int = 1;
396                         break;
397                         
398                 case BDB_INDEX:
399                         bdb_attr_index_unparse( bdb, &c->rvalue_vals );
400                         if ( !c->rvalue_vals ) rc = 1;
401                         break;
402
403                 case BDB_LOCKD:
404                         rc = 1;
405                         if ( bdb->bi_lock_detect != DB_LOCK_DEFAULT ) {
406                                 int i;
407                                 for (i=0; !BER_BVISNULL(&bdb_lockd[i].word); i++) {
408                                         if ( bdb->bi_lock_detect == bdb_lockd[i].mask ) {
409                                                 value_add_one( &c->rvalue_vals, &bdb_lockd[i].word );
410                                                 rc = 0;
411                                                 break;
412                                         }
413                                 }
414                         }
415                         break;
416
417                 case BDB_SSTACK:
418                         c->value_int = bdb->bi_search_stack_depth;
419                         break;
420                 }
421                 return rc;
422         } else if ( c->op == LDAP_MOD_DELETE ) {
423                 rc = 0;
424                 switch( c->type ) {
425                 /* single-valued no-ops */
426                 case BDB_LOCKD:
427                 case BDB_SSTACK:
428                         break;
429
430                 case BDB_CHKPT:
431                         if ( bdb->bi_txn_cp_task ) {
432                                 struct re_s *re = bdb->bi_txn_cp_task;
433                                 bdb->bi_txn_cp_task = NULL;
434                                 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ))
435                                         ldap_pvt_runqueue_stoptask( &slapd_rq, re );
436                                 ldap_pvt_runqueue_remove( &slapd_rq, re );
437                         }
438                         bdb->bi_txn_cp = 0;
439                         break;
440                 case BDB_CONFIG:
441                         if ( c->valx < 0 ) {
442                                 ber_bvarray_free( bdb->bi_db_config );
443                                 bdb->bi_db_config = NULL;
444                         } else {
445                                 int i = c->valx;
446                                 ch_free( bdb->bi_db_config[i].bv_val );
447                                 for (; bdb->bi_db_config[i].bv_val; i++)
448                                         bdb->bi_db_config[i] = bdb->bi_db_config[i+1];
449                         }
450                         bdb->bi_flags |= BDB_UPD_CONFIG;
451                         c->cleanup = bdb_cf_cleanup;
452                         break;
453                 case BDB_DIRECTORY:
454                         bdb->bi_flags |= BDB_RE_OPEN;
455                         bdb->bi_flags ^= BDB_HAS_CONFIG;
456                         ch_free( bdb->bi_dbenv_home );
457                         bdb->bi_dbenv_home = NULL;
458                         ch_free( bdb->bi_db_config_path );
459                         bdb->bi_db_config_path = NULL;
460                         c->cleanup = bdb_cf_cleanup;
461                         ldap_pvt_thread_pool_purgekey( bdb->bi_dbenv );
462                         ldap_pvt_thread_pool_purgekey( ((char *)bdb->bi_dbenv) + 1 );
463                         break;
464                 case BDB_NOSYNC:
465                         bdb->bi_dbenv->set_flags( bdb->bi_dbenv, DB_TXN_NOSYNC, 0 );
466                         break;
467                 case BDB_INDEX: {
468                         AttributeDescription *ad = NULL;
469                         struct berval bv, def = BER_BVC("default");
470                         char *ptr;
471                         const char *text;
472                         for (ptr = c->line; !isspace( *ptr ); ptr++);
473                         bv.bv_val = c->line;
474                         bv.bv_len = ptr - bv.bv_val;
475                         if ( bvmatch( &bv, &def )) {
476                                 bdb->bi_defaultmask = 0;
477                         } else {
478                                 slap_bv2ad( &bv, &ad, &text );
479                                 if ( ad ) {
480                                         AttrInfo *ai = bdb_attr_mask( bdb, ad );
481                                         ai->ai_indexmask |= BDB_INDEX_DELETING;
482                                         bdb->bi_flags |= BDB_DEL_INDEX;
483                                         c->cleanup = bdb_cf_cleanup;
484                                 }
485                         }
486                         }
487                         break;
488                 }
489                 return rc;
490         }
491
492         switch( c->type ) {
493         case BDB_CHKPT: {
494                 long    l;
495                 bdb->bi_txn_cp = 1;
496                 if ( lutil_atolx( &l, c->argv[1], 0 ) != 0 ) {
497                         fprintf( stderr, "%s: "
498                                 "invalid kbyte \"%s\" in \"checkpoint\".\n",
499                                 c->log, c->argv[1] );
500                         return 1;
501                 }
502                 bdb->bi_txn_cp_kbyte = l;
503                 if ( lutil_atolx( &l, c->argv[2], 0 ) != 0 ) {
504                         fprintf( stderr, "%s: "
505                                 "invalid minutes \"%s\" in \"checkpoint\".\n",
506                                 c->log, c->argv[2] );
507                         return 1;
508                 }
509                 bdb->bi_txn_cp_min = l;
510                 /* If we're in server mode and time-based checkpointing is enabled,
511                  * submit a task to perform periodic checkpoints.
512                  */
513                 if ((slapMode & SLAP_SERVER_MODE) && bdb->bi_txn_cp_min ) {
514                         struct re_s *re = bdb->bi_txn_cp_task;
515                         if ( re ) {
516                                 re->interval.tv_sec = bdb->bi_txn_cp_min * 60;
517                         } else {
518                                 if ( c->be->be_suffix == NULL || BER_BVISNULL( &c->be->be_suffix[0] ) ) {
519                                         fprintf( stderr, "%s: "
520                                                 "\"checkpoint\" must occur after \"suffix\".\n",
521                                                 c->log );
522                                         return 1;
523                                 }
524                                 bdb->bi_txn_cp_task = ldap_pvt_runqueue_insert( &slapd_rq,
525                                         bdb->bi_txn_cp_min * 60, bdb_checkpoint, bdb,
526                                         LDAP_XSTRING(bdb_checkpoint), c->be->be_suffix[0].bv_val );
527                         }
528                 }
529                 } break;
530
531         case BDB_CONFIG: {
532                 char *ptr = c->line;
533                 struct berval bv;
534
535                 if ( c->op == SLAP_CONFIG_ADD ) {
536                         ptr += STRLENOF("dbconfig");
537                         while (!isspace(*ptr)) ptr++;
538                         while (isspace(*ptr)) ptr++;
539                 }
540
541                 if ( bdb->bi_flags & BDB_IS_OPEN ) {
542                         bdb->bi_flags |= BDB_UPD_CONFIG;
543                         c->cleanup = bdb_cf_cleanup;
544                 } else {
545                 /* If we're just starting up...
546                  */
547                         FILE *f;
548                         /* If a DB_CONFIG file exists, or we don't know the path
549                          * to the DB_CONFIG file, ignore these directives
550                          */
551                         if (( bdb->bi_flags & BDB_HAS_CONFIG ) || !bdb->bi_db_config_path )
552                                 break;
553                         f = fopen( bdb->bi_db_config_path, "a" );
554                         if ( f ) {
555                                 /* FIXME: EBCDIC probably needs special handling */
556                                 fprintf( f, "%s\n", ptr );
557                                 fclose( f );
558                         }
559                 }
560                 ber_str2bv( ptr, 0, 1, &bv );
561                 ber_bvarray_add( &bdb->bi_db_config, &bv );
562                 }
563                 break;
564
565         case BDB_DIRECTORY: {
566                 FILE *f;
567                 char *ptr;
568
569                 if ( bdb->bi_dbenv_home )
570                         ch_free( bdb->bi_dbenv_home );
571                 bdb->bi_dbenv_home = c->value_string;
572
573                 /* See if a DB_CONFIG file already exists here */
574                 if ( bdb->bi_db_config_path )
575                         ch_free( bdb->bi_db_config_path );
576                 bdb->bi_db_config_path = ch_malloc( strlen( bdb->bi_dbenv_home ) +
577                         STRLENOF(LDAP_DIRSEP) + STRLENOF("DB_CONFIG") + 1 );
578                 ptr = lutil_strcopy( bdb->bi_db_config_path, bdb->bi_dbenv_home );
579                 *ptr++ = LDAP_DIRSEP[0];
580                 strcpy( ptr, "DB_CONFIG" );
581
582                 f = fopen( bdb->bi_db_config_path, "r" );
583                 if ( f ) {
584                         bdb->bi_flags |= BDB_HAS_CONFIG;
585                         fclose(f);
586                 }
587                 }
588                 break;
589
590         case BDB_NOSYNC:
591                 if ( c->value_int )
592                         bdb->bi_dbenv_xflags |= DB_TXN_NOSYNC;
593                 else
594                         bdb->bi_dbenv_xflags &= ~DB_TXN_NOSYNC;
595                 if ( bdb->bi_flags & BDB_IS_OPEN ) {
596                         bdb->bi_dbenv->set_flags( bdb->bi_dbenv, DB_TXN_NOSYNC,
597                                 c->value_int );
598                 }
599                 break;
600
601         case BDB_INDEX:
602                 rc = bdb_attr_index_config( bdb, c->fname, c->lineno,
603                         c->argc - 1, &c->argv[1] );
604
605                 if( rc != LDAP_SUCCESS ) return 1;
606                 if (( bdb->bi_flags & BDB_IS_OPEN ) && !bdb->bi_index_task ) {
607                         /* Start the task as soon as we finish here. Set a long
608                          * interval (10 hours) so that it only gets scheduled once.
609                          */
610                         if ( c->be->be_suffix == NULL || BER_BVISNULL( &c->be->be_suffix[0] ) ) {
611                                 fprintf( stderr, "%s: "
612                                         "\"index\" must occur after \"suffix\".\n",
613                                         c->log );
614                                 return 1;
615                         }
616                         bdb->bi_index_task = ldap_pvt_runqueue_insert( &slapd_rq, 36000,
617                                 bdb_online_index, c->be,
618                                 LDAP_XSTRING(bdb_online_index), c->be->be_suffix[0].bv_val );
619                 }
620                 break;
621
622         case BDB_LOCKD:
623                 rc = verb_to_mask( c->argv[1], bdb_lockd );
624                 if ( BER_BVISNULL(&bdb_lockd[rc].word) ) {
625                         fprintf( stderr, "%s: "
626                                 "bad policy (%s) in \"lockDetect <policy>\" line\n",
627                                 c->log, c->argv[1] );
628                         return 1;
629                 }
630                 bdb->bi_lock_detect = rc;
631                 break;
632
633         case BDB_SSTACK:
634                 if ( c->value_int < MINIMUM_SEARCH_STACK_DEPTH ) {
635                         fprintf( stderr,
636                 "%s: depth %d too small, using %d\n",
637                         c->log, c->value_int, MINIMUM_SEARCH_STACK_DEPTH );
638                         c->value_int = MINIMUM_SEARCH_STACK_DEPTH;
639                 }
640                 bdb->bi_search_stack_depth = c->value_int;
641                 break;
642         }
643         return 0;
644 }
645
646 int bdb_back_init_cf( BackendInfo *bi )
647 {
648         int rc;
649         bi->bi_cf_ocs = bdbocs;
650
651         rc = config_register_schema( bdbcfg, bdbocs );
652         if ( rc ) return rc;
653         return 0;
654 }