1 /* config.c - bdb backend configuration file routine */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2005 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>.
20 #include <ac/string.h>
30 # define SLAP_BDB_ALLOW_DIRTY_READ
33 #define bdb_cf_gen BDB_SYMBOL(cf_gen)
34 #define bdb_cf_cleanup BDB_SYMBOL(cf_cleanup)
35 #define bdb_checkpoint BDB_SYMBOL(checkpoint)
36 #define bdb_online_index BDB_SYMBOL(online_index)
38 static ConfigDriver bdb_cf_gen;
51 static ConfigTable bdbcfg[] = {
52 { "directory", "dir", 2, 2, 0, ARG_STRING|ARG_MAGIC|BDB_DIRECTORY,
53 bdb_cf_gen, "( OLcfgDbAt:0.1 NAME 'olcDbDirectory' "
54 "DESC 'Directory for database content' "
55 "EQUALITY caseIgnoreMatch "
56 "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
57 { "cachesize", "size", 2, 2, 0, ARG_INT|ARG_OFFSET,
58 (void *)offsetof(struct bdb_info, bi_cache.c_maxsize),
59 "( OLcfgDbAt:1.1 NAME 'olcDbCacheSize' "
60 "DESC 'Entry cache size in entries' "
61 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
62 { "checkpoint", "kbyte> <min", 3, 3, 0, ARG_MAGIC|BDB_CHKPT,
63 bdb_cf_gen, "( OLcfgDbAt:1.2 NAME 'olcDbCheckpoint' "
64 "DESC 'Database checkpoint interval in kbytes and minutes' "
65 "SYNTAX OMsDirectoryString SINGLE-VALUE )",NULL, NULL },
66 { "dbconfig", "DB_CONFIG setting", 1, 0, 0, ARG_MAGIC|BDB_CONFIG,
67 bdb_cf_gen, "( OLcfgDbAt:1.3 NAME 'olcDbConfig' "
68 "DESC 'BerkeleyDB DB_CONFIG configuration directives' "
69 "SYNTAX OMsDirectoryString )",NULL, NULL },
70 { "dbnosync", NULL, 1, 2, 0, ARG_ON_OFF|ARG_MAGIC|BDB_NOSYNC,
71 bdb_cf_gen, "( OLcfgDbAt:1.4 NAME 'olcDbNoSync' "
72 "DESC 'Disable synchronous database writes' "
73 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
74 { "dirtyread", NULL, 1, 2, 0,
75 #ifdef SLAP_BDB_ALLOW_DIRTY_READ
76 ARG_ON_OFF|ARG_MAGIC|BDB_DIRTYR, bdb_cf_gen,
80 "( OLcfgDbAt:1.5 NAME 'olcDbDirtyRead' "
81 "DESC 'Allow reads of uncommitted data' "
82 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
83 { "idlcachesize", "size", 2, 2, 0, ARG_INT|ARG_OFFSET,
84 (void *)offsetof(struct bdb_info,bi_idl_cache_max_size),
85 "( OLcfgDbAt:1.6 NAME 'olcDbIDLcacheSize' "
86 "DESC 'IDL cache size in IDLs' "
87 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
88 { "index", "attr> <[pres,eq,approx,sub]", 2, 3, 0, ARG_MAGIC|BDB_INDEX,
89 bdb_cf_gen, "( OLcfgDbAt:0.2 NAME 'olcDbIndex' "
90 "DESC 'Attribute index parameters' "
91 "EQUALITY caseIgnoreMatch "
92 "SYNTAX OMsDirectoryString )", NULL, NULL },
93 { "linearindex", NULL, 1, 2, 0, ARG_ON_OFF|ARG_OFFSET,
94 (void *)offsetof(struct bdb_info, bi_linear_index),
95 "( OLcfgDbAt:1.7 NAME 'olcDbLinearIndex' "
96 "DESC 'Index attributes one at a time' "
97 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
98 { "lockdetect", "policy", 2, 2, 0, ARG_MAGIC|BDB_LOCKD,
99 bdb_cf_gen, "( OLcfgDbAt:1.8 NAME 'olcDbLockDetect' "
100 "DESC 'Deadlock detection algorithm' "
101 "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
102 { "mode", "mode", 2, 2, 0, ARG_INT|ARG_OFFSET,
103 (void *)offsetof(struct bdb_info, bi_dbenv_mode),
104 "( OLcfgDbAt:0.3 NAME 'olcDbMode' "
105 "DESC 'Unix permissions of database files' "
106 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
107 { "searchstack", "depth", 2, 2, 0, ARG_INT|ARG_MAGIC|BDB_SSTACK,
108 bdb_cf_gen, "( OLcfgDbAt:1.9 NAME 'olcDbSearchStack' "
109 "DESC 'Depth of search stack in IDLs' "
110 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
111 { "shm_key", "key", 2, 2, 0, ARG_INT|ARG_OFFSET,
112 (void *)offsetof(struct bdb_info, bi_shm_key),
113 "( OLcfgDbAt:1.10 NAME 'olcDbShmKey' "
114 "DESC 'Key for shared memory region' "
115 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
116 { NULL, NULL, 0, 0, 0, ARG_IGNORED,
117 NULL, NULL, NULL, NULL }
120 static ConfigOCs bdbocs[] = {
124 "NAME 'olcHdbConfig' "
125 "DESC 'HDB backend configuration' "
128 "NAME 'olcBdbConfig' "
129 "DESC 'BDB backend configuration' "
131 "SUP olcDatabaseConfig "
132 "MUST olcDbDirectory "
133 "MAY ( olcDbCacheSize $ olcDbCheckpoint $ olcDbConfig $ "
134 "olcDbNoSync $ olcDbDirtyRead $ olcDbIDLcacheSize $ "
135 "olcDbIndex $ olcDbLinearIndex $ olcDbLockDetect $ "
136 "olcDbMode $ olcDbSearchStack $ olcDbShmKey ) )",
137 Cft_Database, bdbcfg },
141 static slap_verbmasks bdb_lockd[] = {
142 { BER_BVC("default"), DB_LOCK_DEFAULT },
143 { BER_BVC("oldest"), DB_LOCK_OLDEST },
144 { BER_BVC("random"), DB_LOCK_RANDOM },
145 { BER_BVC("youngest"), DB_LOCK_YOUNGEST },
146 { BER_BVC("fewest"), DB_LOCK_MINLOCKS },
150 /* perform periodic checkpoints */
152 bdb_checkpoint( void *ctx, void *arg )
154 struct re_s *rtask = arg;
155 struct bdb_info *bdb = rtask->arg;
157 TXN_CHECKPOINT( bdb->bi_dbenv, bdb->bi_txn_cp_kbyte,
158 bdb->bi_txn_cp_min, 0 );
159 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
160 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
161 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
165 /* reindex entries on the fly */
167 bdb_online_index( void *ctx, void *arg )
169 struct re_s *rtask = arg;
170 BackendDB *be = rtask->arg;
171 struct bdb_info *bdb = be->be_private;
173 Connection conn = {0};
174 char opbuf[OPERATION_BUFFER_SIZE];
175 Operation *op = (Operation *)opbuf;
186 connection_fake_init( &conn, op, ctx );
195 key.size = key.ulen = sizeof(ID);
196 key.flags = DB_DBT_USERMEM;
198 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
199 data.dlen = data.ulen = 0;
202 if ( slapd_shutdown )
205 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &txn, bdb->bi_db_opflags );
208 locker = TXN_ID( txn );
211 BDB_ID2DISK( id, &nid );
212 rc = bdb->bi_id2entry->bdi_db->cursor(
213 bdb->bi_id2entry->bdi_db, txn, &curs, bdb->bi_db_opflags );
218 rc = curs->c_get( curs, &key, &data, DB_SET_RANGE );
219 curs->c_close( curs );
222 if ( rc == DB_NOTFOUND )
224 if ( rc == DB_LOCK_DEADLOCK ) {
225 ldap_pvt_thread_yield();
230 BDB_DISK2ID( &nid, &id );
234 rc = bdb_cache_find_id( op, txn, id, &ei, 0, locker, &lock );
237 if ( rc == DB_LOCK_DEADLOCK ) {
238 ldap_pvt_thread_yield();
241 if ( rc == DB_NOTFOUND ) {
249 rc = bdb_index_entry( op, txn, BDB_INDEX_UPDATE_OP, ei->bei_e );
250 if ( rc == DB_LOCK_DEADLOCK ) {
252 ldap_pvt_thread_yield();
256 rc = TXN_COMMIT( txn, 0 );
266 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
267 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
268 bdb->bi_index_task = NULL;
269 ldap_pvt_runqueue_remove( &slapd_rq, rtask );
270 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
275 /* Cleanup loose ends after Modify completes */
277 bdb_cf_cleanup( ConfigArgs *c )
279 struct bdb_info *bdb = c->be->be_private;
282 if ( bdb->bi_flags & BDB_UPD_CONFIG ) {
283 if ( bdb->bi_db_config ) {
285 FILE *f = fopen( bdb->bi_db_config_path, "w" );
287 for (i=0; bdb->bi_db_config[i].bv_val; i++)
288 fprintf( f, "%s\n", bdb->bi_db_config[i].bv_val );
292 unlink( bdb->bi_db_config_path );
294 bdb->bi_flags ^= BDB_UPD_CONFIG;
297 if ( bdb->bi_flags & BDB_DEL_INDEX ) {
298 bdb_attr_flush( bdb );
299 bdb->bi_flags ^= BDB_DEL_INDEX;
302 if ( bdb->bi_flags & BDB_RE_OPEN ) {
303 bdb->bi_flags ^= BDB_RE_OPEN;
304 rc = c->be->bd_info->bi_db_close( c->be );
306 rc = c->be->bd_info->bi_db_open( c->be );
307 /* If this fails, we need to restart */
310 Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(bdb_cf_cleanup)
311 ": failed to reopen database, rc=%d", rc, 0, 0 );
318 bdb_cf_gen(ConfigArgs *c)
320 struct bdb_info *bdb = c->be->be_private;
323 if ( c->op == SLAP_CONFIG_EMIT ) {
327 if (bdb->bi_txn_cp ) {
330 bv.bv_len = sprintf( buf, "%d %d", bdb->bi_txn_cp_kbyte,
331 bdb->bi_txn_cp_min );
333 value_add_one( &c->rvalue_vals, &bv );
340 if ( bdb->bi_dbenv_home ) {
341 c->value_string = ch_strdup( bdb->bi_dbenv_home );
348 if (( slapMode&SLAP_SERVER_MODE ) && !( bdb->bi_flags&BDB_IS_OPEN )
349 && !bdb->bi_db_config ) {
350 char buf[SLAP_TEXT_BUFLEN];
351 FILE *f = fopen( bdb->bi_db_config_path, "r" );
355 bdb->bi_flags |= BDB_HAS_CONFIG;
356 while ( fgets( buf, sizeof(buf), f )) {
357 ber_str2bv( buf, 0, 1, &bv );
358 if ( bv.bv_val[bv.bv_len-1] == '\n' ) {
360 bv.bv_val[bv.bv_len] = '\0';
362 /* shouldn't need this, but ... */
363 if ( bv.bv_val[bv.bv_len-1] == '\r' ) {
365 bv.bv_val[bv.bv_len] = '\0';
367 ber_bvarray_add( &bdb->bi_db_config, &bv );
372 if ( bdb->bi_db_config ) {
377 for (i=0; !BER_BVISNULL(&bdb->bi_db_config[i]); i++) {
378 bv.bv_len = sprintf( bv.bv_val, "{%d}%s", i,
379 bdb->bi_db_config[i].bv_val );
380 value_add_one( &c->rvalue_vals, &bv );
383 if ( !c->rvalue_vals ) rc = 1;
387 if ( bdb->bi_dbenv_xflags & DB_TXN_NOSYNC )
392 bdb_attr_index_unparse( bdb, &c->rvalue_vals );
393 if ( !c->rvalue_vals ) rc = 1;
398 if ( bdb->bi_lock_detect != DB_LOCK_DEFAULT ) {
400 for (i=0; !BER_BVISNULL(&bdb_lockd[i].word); i++) {
401 if ( bdb->bi_lock_detect == bdb_lockd[i].mask ) {
402 value_add_one( &c->rvalue_vals, &bdb_lockd[i].word );
411 c->value_int = bdb->bi_search_stack_depth;
415 } else if ( c->op == LDAP_MOD_DELETE ) {
418 /* single-valued no-ops */
424 if ( bdb->bi_txn_cp_task ) {
425 struct re_s *re = bdb->bi_txn_cp_task;
426 bdb->bi_txn_cp_task = NULL;
427 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ))
428 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
429 ldap_pvt_runqueue_remove( &slapd_rq, re );
435 ber_bvarray_free( bdb->bi_db_config );
436 bdb->bi_db_config = NULL;
439 ch_free( bdb->bi_db_config[i].bv_val );
440 for (; bdb->bi_db_config[i].bv_val; i++)
441 bdb->bi_db_config[i] = bdb->bi_db_config[i+1];
443 bdb->bi_flags |= BDB_UPD_CONFIG;
444 c->cleanup = bdb_cf_cleanup;
447 bdb->bi_flags |= BDB_RE_OPEN;
448 bdb->bi_flags ^= BDB_HAS_CONFIG;
449 ch_free( bdb->bi_dbenv_home );
450 bdb->bi_dbenv_home = NULL;
451 ch_free( bdb->bi_db_config_path );
452 bdb->bi_db_config_path = NULL;
453 c->cleanup = bdb_cf_cleanup;
454 ldap_pvt_thread_pool_purgekey( bdb->bi_dbenv );
455 ldap_pvt_thread_pool_purgekey( ((char *)bdb->bi_dbenv) + 1 );
458 bdb->bi_dbenv->set_flags( bdb->bi_dbenv, DB_TXN_NOSYNC, 0 );
461 AttributeDescription *ad = NULL;
462 struct berval bv, def = BER_BVC("default");
465 for (ptr = c->line; !isspace( *ptr ); ptr++);
467 bv.bv_len = ptr - bv.bv_val;
468 if ( bvmatch( &bv, &def )) {
469 bdb->bi_defaultmask = 0;
471 slap_bv2ad( &bv, &ad, &text );
473 AttrInfo *ai = bdb_attr_mask( bdb, ad );
474 ai->ai_indexmask |= BDB_INDEX_DELETING;
475 bdb->bi_flags |= BDB_DEL_INDEX;
476 c->cleanup = bdb_cf_cleanup;
488 bdb->bi_txn_cp_kbyte = strtol( c->argv[1], NULL, 0 );
489 bdb->bi_txn_cp_min = strtol( c->argv[2], NULL, 0 );
490 /* If we're in server mode and time-based checkpointing is enabled,
491 * submit a task to perform periodic checkpoints.
493 if ((slapMode & SLAP_SERVER_MODE) && bdb->bi_txn_cp_min ) {
494 struct re_s *re = bdb->bi_txn_cp_task;
496 re->interval.tv_sec = bdb->bi_txn_cp_min * 60;
498 if ( c->be->be_suffix == NULL || BER_BVISNULL( &c->be->be_suffix[0] ) ) {
499 fprintf( stderr, "%s: "
500 "\"checkpoint\" must occur after \"suffix\".\n",
504 bdb->bi_txn_cp_task = ldap_pvt_runqueue_insert( &slapd_rq,
505 bdb->bi_txn_cp_min * 60, bdb_checkpoint, bdb,
506 LDAP_XSTRING(bdb_checkpoint), c->be->be_suffix[0].bv_val );
515 if ( c->op == SLAP_CONFIG_ADD ) {
516 ptr += STRLENOF("dbconfig");
517 while (!isspace(*ptr)) ptr++;
518 while (isspace(*ptr)) ptr++;
521 ptr = strchr( ptr+1, '}');
528 if ( bdb->bi_flags & BDB_IS_OPEN ) {
529 bdb->bi_flags |= BDB_UPD_CONFIG;
530 c->cleanup = bdb_cf_cleanup;
532 /* If we're just starting up...
535 /* If a DB_CONFIG file exists, or we don't know the path
536 * to the DB_CONFIG file, ignore these directives
538 if (( bdb->bi_flags & BDB_HAS_CONFIG ) || !bdb->bi_db_config_path )
540 f = fopen( bdb->bi_db_config_path, "a" );
542 /* FIXME: EBCDIC probably needs special handling */
543 fprintf( f, "%s\n", ptr );
547 ber_str2bv( ptr, 0, 1, &bv );
548 ber_bvarray_add( &bdb->bi_db_config, &bv );
552 case BDB_DIRECTORY: {
556 if ( bdb->bi_dbenv_home )
557 ch_free( bdb->bi_dbenv_home );
558 bdb->bi_dbenv_home = c->value_string;
560 /* See if a DB_CONFIG file already exists here */
561 if ( bdb->bi_db_config_path )
562 ch_free( bdb->bi_db_config_path );
563 bdb->bi_db_config_path = ch_malloc( strlen( bdb->bi_dbenv_home ) +
564 STRLENOF(LDAP_DIRSEP) + STRLENOF("DB_CONFIG") + 1 );
565 ptr = lutil_strcopy( bdb->bi_db_config_path, bdb->bi_dbenv_home );
566 *ptr++ = LDAP_DIRSEP[0];
567 strcpy( ptr, "DB_CONFIG" );
569 f = fopen( bdb->bi_db_config_path, "r" );
571 bdb->bi_flags |= BDB_HAS_CONFIG;
579 bdb->bi_dbenv_xflags |= DB_TXN_NOSYNC;
581 bdb->bi_dbenv_xflags &= ~DB_TXN_NOSYNC;
582 if ( bdb->bi_flags & BDB_IS_OPEN ) {
583 bdb->bi_dbenv->set_flags( bdb->bi_dbenv, DB_TXN_NOSYNC,
589 rc = bdb_attr_index_config( bdb, c->fname, c->lineno,
590 c->argc - 1, &c->argv[1] );
592 if( rc != LDAP_SUCCESS ) return 1;
593 if (( bdb->bi_flags & BDB_IS_OPEN ) && !bdb->bi_index_task ) {
594 /* Start the task as soon as we finish here. Set a long
595 * interval (10 hours) so that it only gets scheduled once.
597 if ( c->be->be_suffix == NULL || BER_BVISNULL( &c->be->be_suffix[0] ) ) {
598 fprintf( stderr, "%s: "
599 "\"index\" must occur after \"suffix\".\n",
603 bdb->bi_index_task = ldap_pvt_runqueue_insert( &slapd_rq, 36000,
604 bdb_online_index, c->be,
605 LDAP_XSTRING(bdb_online_index), c->be->be_suffix[0].bv_val );
610 rc = verb_to_mask( c->argv[1], bdb_lockd );
611 if ( BER_BVISNULL(&bdb_lockd[rc].word) ) {
612 fprintf( stderr, "%s: "
613 "bad policy (%s) in \"lockDetect <policy>\" line\n",
614 c->log, c->argv[1] );
617 bdb->bi_lock_detect = rc;
621 if ( c->value_int < MINIMUM_SEARCH_STACK_DEPTH ) {
623 "%s: depth %d too small, using %d\n",
624 c->log, c->value_int, MINIMUM_SEARCH_STACK_DEPTH );
625 c->value_int = MINIMUM_SEARCH_STACK_DEPTH;
627 bdb->bi_search_stack_depth = c->value_int;
633 int bdb_back_init_cf( BackendInfo *bi )
636 bi->bi_cf_ocs = bdbocs;
638 rc = config_register_schema( bdbcfg, bdbocs );