1 /* config.c - bdb backend configuration file routine */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2007 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
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>.
21 #include <ac/string.h>
31 # define SLAP_BDB_ALLOW_DIRTY_READ
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)
39 static ConfigDriver bdb_cf_gen;
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_UINT|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_UINT|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 OMsIA5String 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,
86 "( OLcfgDbAt:1.5 NAME 'olcDbDirtyRead' "
87 "DESC 'Allow reads of uncommitted data' "
88 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
89 { "dncachesize", "size", 2, 2, 0, ARG_UINT|ARG_OFFSET,
90 (void *)offsetof(struct bdb_info, bi_cache.c_eimax),
91 "( OLcfgDbAt:1.12 NAME 'olcDbDNcacheSize' "
92 "DESC 'DN cache size' "
93 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
94 { "idlcachesize", "size", 2, 2, 0, ARG_UINT|ARG_OFFSET,
95 (void *)offsetof(struct bdb_info, bi_idl_cache_max_size),
96 "( OLcfgDbAt:1.6 NAME 'olcDbIDLcacheSize' "
97 "DESC 'IDL cache size in IDLs' "
98 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
99 { "index", "attr> <[pres,eq,approx,sub]", 2, 3, 0, ARG_MAGIC|BDB_INDEX,
100 bdb_cf_gen, "( OLcfgDbAt:0.2 NAME 'olcDbIndex' "
101 "DESC 'Attribute index parameters' "
102 "EQUALITY caseIgnoreMatch "
103 "SYNTAX OMsDirectoryString )", NULL, NULL },
104 { "linearindex", NULL, 1, 2, 0, ARG_ON_OFF|ARG_OFFSET,
105 (void *)offsetof(struct bdb_info, bi_linear_index),
106 "( OLcfgDbAt:1.7 NAME 'olcDbLinearIndex' "
107 "DESC 'Index attributes one at a time' "
108 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
109 { "lockdetect", "policy", 2, 2, 0, ARG_MAGIC|BDB_LOCKD,
110 bdb_cf_gen, "( OLcfgDbAt:1.8 NAME 'olcDbLockDetect' "
111 "DESC 'Deadlock detection algorithm' "
112 "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
113 { "mode", "mode", 2, 2, 0, ARG_INT|ARG_OFFSET,
114 (void *)offsetof(struct bdb_info, bi_dbenv_mode),
115 "( OLcfgDbAt:0.3 NAME 'olcDbMode' "
116 "DESC 'Unix permissions of database files' "
117 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
118 { "searchstack", "depth", 2, 2, 0, ARG_INT|ARG_MAGIC|BDB_SSTACK,
119 bdb_cf_gen, "( OLcfgDbAt:1.9 NAME 'olcDbSearchStack' "
120 "DESC 'Depth of search stack in IDLs' "
121 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
122 { "shm_key", "key", 2, 2, 0, ARG_LONG|ARG_OFFSET,
123 (void *)offsetof(struct bdb_info, bi_shm_key),
124 "( OLcfgDbAt:1.10 NAME 'olcDbShmKey' "
125 "DESC 'Key for shared memory region' "
126 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
127 { NULL, NULL, 0, 0, 0, ARG_IGNORED,
128 NULL, NULL, NULL, NULL }
131 static ConfigOCs bdbocs[] = {
135 "NAME 'olcHdbConfig' "
136 "DESC 'HDB backend configuration' "
139 "NAME 'olcBdbConfig' "
140 "DESC 'BDB backend configuration' "
142 "SUP olcDatabaseConfig "
143 "MUST olcDbDirectory "
144 "MAY ( olcDbCacheSize $ olcDbCheckpoint $ olcDbConfig $ "
145 "olcDbNoSync $ olcDbDirtyRead $ olcDbIDLcacheSize $ "
146 "olcDbIndex $ olcDbLinearIndex $ olcDbLockDetect $ "
147 "olcDbMode $ olcDbSearchStack $ olcDbShmKey $ "
148 "olcDbCacheFree $ olcDbDNcacheSize ) )",
149 Cft_Database, bdbcfg },
153 static slap_verbmasks bdb_lockd[] = {
154 { BER_BVC("default"), DB_LOCK_DEFAULT },
155 { BER_BVC("oldest"), DB_LOCK_OLDEST },
156 { BER_BVC("random"), DB_LOCK_RANDOM },
157 { BER_BVC("youngest"), DB_LOCK_YOUNGEST },
158 { BER_BVC("fewest"), DB_LOCK_MINLOCKS },
162 /* perform periodic checkpoints */
164 bdb_checkpoint( void *ctx, void *arg )
166 struct re_s *rtask = arg;
167 struct bdb_info *bdb = rtask->arg;
169 TXN_CHECKPOINT( bdb->bi_dbenv, bdb->bi_txn_cp_kbyte,
170 bdb->bi_txn_cp_min, 0 );
171 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
172 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
173 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
177 /* reindex entries on the fly */
179 bdb_online_index( void *ctx, void *arg )
181 struct re_s *rtask = arg;
182 BackendDB *be = rtask->arg;
183 struct bdb_info *bdb = be->be_private;
185 Connection conn = {0};
186 OperationBuffer opbuf;
199 connection_fake_init( &conn, &opbuf, ctx );
209 key.size = key.ulen = sizeof(ID);
210 key.flags = DB_DBT_USERMEM;
212 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
213 data.dlen = data.ulen = 0;
216 if ( slapd_shutdown )
219 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &txn, bdb->bi_db_opflags );
222 locker = TXN_ID( txn );
225 BDB_ID2DISK( id, &nid );
226 rc = bdb->bi_id2entry->bdi_db->cursor(
227 bdb->bi_id2entry->bdi_db, txn, &curs, bdb->bi_db_opflags );
232 rc = curs->c_get( curs, &key, &data, DB_SET_RANGE );
233 curs->c_close( curs );
236 if ( rc == DB_NOTFOUND )
238 if ( rc == DB_LOCK_DEADLOCK ) {
239 ldap_pvt_thread_yield();
244 BDB_DISK2ID( &nid, &id );
248 rc = bdb_cache_find_id( op, txn, id, &ei, 0, locker, &lock );
251 if ( rc == DB_LOCK_DEADLOCK ) {
252 ldap_pvt_thread_yield();
255 if ( rc == DB_NOTFOUND ) {
263 rc = bdb_index_entry( op, txn, BDB_INDEX_UPDATE_OP, ei->bei_e );
264 if ( rc == DB_LOCK_DEADLOCK ) {
266 ldap_pvt_thread_yield();
270 rc = TXN_COMMIT( txn, 0 );
280 for ( i = 0; i < bdb->bi_nattrs; i++ ) {
281 if ( bdb->bi_attrs[ i ]->ai_indexmask & BDB_INDEX_DELETING
282 || bdb->bi_attrs[ i ]->ai_newmask == 0 )
286 bdb->bi_attrs[ i ]->ai_indexmask = bdb->bi_attrs[ i ]->ai_newmask;
287 bdb->bi_attrs[ i ]->ai_newmask = 0;
290 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
291 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
292 bdb->bi_index_task = NULL;
293 ldap_pvt_runqueue_remove( &slapd_rq, rtask );
294 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
299 /* Cleanup loose ends after Modify completes */
301 bdb_cf_cleanup( ConfigArgs *c )
303 struct bdb_info *bdb = c->be->be_private;
306 if ( bdb->bi_flags & BDB_UPD_CONFIG ) {
307 if ( bdb->bi_db_config ) {
309 FILE *f = fopen( bdb->bi_db_config_path, "w" );
311 for (i=0; bdb->bi_db_config[i].bv_val; i++)
312 fprintf( f, "%s\n", bdb->bi_db_config[i].bv_val );
316 unlink( bdb->bi_db_config_path );
318 bdb->bi_flags ^= BDB_UPD_CONFIG;
321 if ( bdb->bi_flags & BDB_DEL_INDEX ) {
322 bdb_attr_flush( bdb );
323 bdb->bi_flags ^= BDB_DEL_INDEX;
326 if ( bdb->bi_flags & BDB_RE_OPEN ) {
327 bdb->bi_flags ^= BDB_RE_OPEN;
328 rc = c->be->bd_info->bi_db_close( c->be, NULL );
330 rc = c->be->bd_info->bi_db_open( c->be, NULL );
331 /* If this fails, we need to restart */
334 Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(bdb_cf_cleanup)
335 ": failed to reopen database, rc=%d", rc, 0, 0 );
342 bdb_cf_gen( ConfigArgs *c )
344 struct bdb_info *bdb = c->be->be_private;
347 if ( c->op == SLAP_CONFIG_EMIT ) {
351 if ( bdb->bi_txn_cp ) {
354 bv.bv_len = sprintf( buf, "%d %d", bdb->bi_txn_cp_kbyte,
355 bdb->bi_txn_cp_min );
357 value_add_one( &c->rvalue_vals, &bv );
364 if ( bdb->bi_dbenv_home ) {
365 c->value_string = ch_strdup( bdb->bi_dbenv_home );
372 if ( !( bdb->bi_flags & BDB_IS_OPEN )
373 && !bdb->bi_db_config )
375 char buf[SLAP_TEXT_BUFLEN];
376 FILE *f = fopen( bdb->bi_db_config_path, "r" );
380 bdb->bi_flags |= BDB_HAS_CONFIG;
381 while ( fgets( buf, sizeof(buf), f )) {
382 ber_str2bv( buf, 0, 1, &bv );
383 if ( bv.bv_len > 0 && bv.bv_val[bv.bv_len-1] == '\n' ) {
385 bv.bv_val[bv.bv_len] = '\0';
387 /* shouldn't need this, but ... */
388 if ( bv.bv_len > 0 && bv.bv_val[bv.bv_len-1] == '\r' ) {
390 bv.bv_val[bv.bv_len] = '\0';
392 ber_bvarray_add( &bdb->bi_db_config, &bv );
397 if ( bdb->bi_db_config ) {
402 for (i=0; !BER_BVISNULL(&bdb->bi_db_config[i]); i++) {
403 bv.bv_len = sprintf( bv.bv_val, "{%d}%s", i,
404 bdb->bi_db_config[i].bv_val );
405 value_add_one( &c->rvalue_vals, &bv );
408 if ( !c->rvalue_vals ) rc = 1;
412 if ( bdb->bi_dbenv_xflags & DB_TXN_NOSYNC )
417 bdb_attr_index_unparse( bdb, &c->rvalue_vals );
418 if ( !c->rvalue_vals ) rc = 1;
423 if ( bdb->bi_lock_detect != DB_LOCK_DEFAULT ) {
425 for (i=0; !BER_BVISNULL(&bdb_lockd[i].word); i++) {
426 if ( bdb->bi_lock_detect == bdb_lockd[i].mask ) {
427 value_add_one( &c->rvalue_vals, &bdb_lockd[i].word );
436 c->value_int = bdb->bi_search_stack_depth;
440 } else if ( c->op == LDAP_MOD_DELETE ) {
443 /* single-valued no-ops */
449 if ( bdb->bi_txn_cp_task ) {
450 struct re_s *re = bdb->bi_txn_cp_task;
451 bdb->bi_txn_cp_task = NULL;
452 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ) )
453 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
454 ldap_pvt_runqueue_remove( &slapd_rq, re );
460 ber_bvarray_free( bdb->bi_db_config );
461 bdb->bi_db_config = NULL;
464 ch_free( bdb->bi_db_config[i].bv_val );
465 for (; bdb->bi_db_config[i].bv_val; i++)
466 bdb->bi_db_config[i] = bdb->bi_db_config[i+1];
468 bdb->bi_flags |= BDB_UPD_CONFIG;
469 c->cleanup = bdb_cf_cleanup;
472 bdb->bi_flags |= BDB_RE_OPEN;
473 bdb->bi_flags ^= BDB_HAS_CONFIG;
474 ch_free( bdb->bi_dbenv_home );
475 bdb->bi_dbenv_home = NULL;
476 ch_free( bdb->bi_db_config_path );
477 bdb->bi_db_config_path = NULL;
478 c->cleanup = bdb_cf_cleanup;
479 ldap_pvt_thread_pool_purgekey( bdb->bi_dbenv );
482 bdb->bi_dbenv->set_flags( bdb->bi_dbenv, DB_TXN_NOSYNC, 0 );
485 if ( c->valx == -1 ) {
488 /* delete all (FIXME) */
489 for ( i = 0; i < bdb->bi_nattrs; i++ ) {
490 bdb->bi_attrs[i]->ai_indexmask |= BDB_INDEX_DELETING;
492 bdb->bi_flags |= BDB_DEL_INDEX;
493 c->cleanup = bdb_cf_cleanup;
496 struct berval bv, def = BER_BVC("default");
499 for (ptr = c->line; !isspace( (unsigned char) *ptr ); ptr++);
502 bv.bv_len = ptr - bv.bv_val;
503 if ( bvmatch( &bv, &def )) {
504 bdb->bi_defaultmask = 0;
511 sep = bv.bv_val[ bv.bv_len ];
512 bv.bv_val[ bv.bv_len ] = '\0';
513 attrs = ldap_str2charray( bv.bv_val, "," );
515 for ( i = 0; attrs[ i ]; i++ ) {
516 AttributeDescription *ad = NULL;
520 slap_str2ad( attrs[ i ], &ad, &text );
521 /* if we got here... */
522 assert( ad != NULL );
524 ai = bdb_attr_mask( bdb, ad );
525 /* if we got here... */
526 assert( ai != NULL );
528 ai->ai_indexmask |= BDB_INDEX_DELETING;
529 bdb->bi_flags |= BDB_DEL_INDEX;
530 c->cleanup = bdb_cf_cleanup;
533 bv.bv_val[ bv.bv_len ] = sep;
534 ldap_charray_free( attrs );
546 if ( lutil_atolx( &l, c->argv[1], 0 ) != 0 ) {
547 fprintf( stderr, "%s: "
548 "invalid kbyte \"%s\" in \"checkpoint\".\n",
549 c->log, c->argv[1] );
552 bdb->bi_txn_cp_kbyte = l;
553 if ( lutil_atolx( &l, c->argv[2], 0 ) != 0 ) {
554 fprintf( stderr, "%s: "
555 "invalid minutes \"%s\" in \"checkpoint\".\n",
556 c->log, c->argv[2] );
559 bdb->bi_txn_cp_min = l;
560 /* If we're in server mode and time-based checkpointing is enabled,
561 * submit a task to perform periodic checkpoints.
563 if ((slapMode & SLAP_SERVER_MODE) && bdb->bi_txn_cp_min ) {
564 struct re_s *re = bdb->bi_txn_cp_task;
566 re->interval.tv_sec = bdb->bi_txn_cp_min * 60;
568 if ( c->be->be_suffix == NULL || BER_BVISNULL( &c->be->be_suffix[0] ) ) {
569 fprintf( stderr, "%s: "
570 "\"checkpoint\" must occur after \"suffix\".\n",
574 bdb->bi_txn_cp_task = ldap_pvt_runqueue_insert( &slapd_rq,
575 bdb->bi_txn_cp_min * 60, bdb_checkpoint, bdb,
576 LDAP_XSTRING(bdb_checkpoint), c->be->be_suffix[0].bv_val );
585 if ( c->op == SLAP_CONFIG_ADD ) {
586 ptr += STRLENOF("dbconfig");
587 while (!isspace((unsigned char)*ptr)) ptr++;
588 while (isspace((unsigned char)*ptr)) ptr++;
591 if ( bdb->bi_flags & BDB_IS_OPEN ) {
592 bdb->bi_flags |= BDB_UPD_CONFIG;
593 c->cleanup = bdb_cf_cleanup;
595 /* If we're just starting up...
598 /* If a DB_CONFIG file exists, or we don't know the path
599 * to the DB_CONFIG file, ignore these directives
601 if (( bdb->bi_flags & BDB_HAS_CONFIG ) || !bdb->bi_db_config_path )
603 f = fopen( bdb->bi_db_config_path, "a" );
605 /* FIXME: EBCDIC probably needs special handling */
606 fprintf( f, "%s\n", ptr );
610 ber_str2bv( ptr, 0, 1, &bv );
611 ber_bvarray_add( &bdb->bi_db_config, &bv );
615 case BDB_DIRECTORY: {
619 if ( bdb->bi_dbenv_home )
620 ch_free( bdb->bi_dbenv_home );
621 bdb->bi_dbenv_home = c->value_string;
623 /* See if a DB_CONFIG file already exists here */
624 if ( bdb->bi_db_config_path )
625 ch_free( bdb->bi_db_config_path );
626 bdb->bi_db_config_path = ch_malloc( strlen( bdb->bi_dbenv_home ) +
627 STRLENOF(LDAP_DIRSEP) + STRLENOF("DB_CONFIG") + 1 );
628 ptr = lutil_strcopy( bdb->bi_db_config_path, bdb->bi_dbenv_home );
629 *ptr++ = LDAP_DIRSEP[0];
630 strcpy( ptr, "DB_CONFIG" );
632 f = fopen( bdb->bi_db_config_path, "r" );
634 bdb->bi_flags |= BDB_HAS_CONFIG;
642 bdb->bi_dbenv_xflags |= DB_TXN_NOSYNC;
644 bdb->bi_dbenv_xflags &= ~DB_TXN_NOSYNC;
645 if ( bdb->bi_flags & BDB_IS_OPEN ) {
646 bdb->bi_dbenv->set_flags( bdb->bi_dbenv, DB_TXN_NOSYNC,
652 rc = bdb_attr_index_config( bdb, c->fname, c->lineno,
653 c->argc - 1, &c->argv[1] );
655 if( rc != LDAP_SUCCESS ) return 1;
656 if (( bdb->bi_flags & BDB_IS_OPEN ) && !bdb->bi_index_task ) {
657 /* Start the task as soon as we finish here. Set a long
658 * interval (10 hours) so that it only gets scheduled once.
660 if ( c->be->be_suffix == NULL || BER_BVISNULL( &c->be->be_suffix[0] ) ) {
661 fprintf( stderr, "%s: "
662 "\"index\" must occur after \"suffix\".\n",
666 bdb->bi_index_task = ldap_pvt_runqueue_insert( &slapd_rq, 36000,
667 bdb_online_index, c->be,
668 LDAP_XSTRING(bdb_online_index), c->be->be_suffix[0].bv_val );
673 rc = verb_to_mask( c->argv[1], bdb_lockd );
674 if ( BER_BVISNULL(&bdb_lockd[rc].word) ) {
675 fprintf( stderr, "%s: "
676 "bad policy (%s) in \"lockDetect <policy>\" line\n",
677 c->log, c->argv[1] );
680 bdb->bi_lock_detect = rc;
684 if ( c->value_int < MINIMUM_SEARCH_STACK_DEPTH ) {
686 "%s: depth %d too small, using %d\n",
687 c->log, c->value_int, MINIMUM_SEARCH_STACK_DEPTH );
688 c->value_int = MINIMUM_SEARCH_STACK_DEPTH;
690 bdb->bi_search_stack_depth = c->value_int;
696 int bdb_back_init_cf( BackendInfo *bi )
699 bi->bi_cf_ocs = bdbocs;
701 rc = config_register_schema( bdbcfg, bdbocs );