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>.
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 { "cachesize", "size", 2, 2, 0, ARG_INT|ARG_OFFSET,
59 (void *)offsetof(struct bdb_info, bi_cache.c_maxsize),
60 "( OLcfgDbAt:1.1 NAME 'olcDbCacheSize' "
61 "DESC 'Entry cache size in entries' "
62 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
63 { "checkpoint", "kbyte> <min", 3, 3, 0, ARG_MAGIC|BDB_CHKPT,
64 bdb_cf_gen, "( OLcfgDbAt:1.2 NAME 'olcDbCheckpoint' "
65 "DESC 'Database checkpoint interval in kbytes and minutes' "
66 "SYNTAX OMsDirectoryString SINGLE-VALUE )",NULL, NULL },
67 { "dbconfig", "DB_CONFIG setting", 1, 0, 0, ARG_MAGIC|BDB_CONFIG,
68 bdb_cf_gen, "( OLcfgDbAt:1.3 NAME 'olcDbConfig' "
69 "DESC 'BerkeleyDB DB_CONFIG configuration directives' "
70 "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
71 { "dbnosync", NULL, 1, 2, 0, ARG_ON_OFF|ARG_MAGIC|BDB_NOSYNC,
72 bdb_cf_gen, "( OLcfgDbAt:1.4 NAME 'olcDbNoSync' "
73 "DESC 'Disable synchronous database writes' "
74 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
75 { "dirtyread", NULL, 1, 2, 0,
76 #ifdef SLAP_BDB_ALLOW_DIRTY_READ
77 ARG_ON_OFF|ARG_MAGIC|BDB_DIRTYR, bdb_cf_gen,
81 "( OLcfgDbAt:1.5 NAME 'olcDbDirtyRead' "
82 "DESC 'Allow reads of uncommitted data' "
83 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
84 { "idlcachesize", "size", 2, 2, 0, ARG_INT|ARG_OFFSET,
85 (void *)offsetof(struct bdb_info,bi_idl_cache_max_size),
86 "( OLcfgDbAt:1.6 NAME 'olcDbIDLcacheSize' "
87 "DESC 'IDL cache size in IDLs' "
88 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
89 { "index", "attr> <[pres,eq,approx,sub]", 2, 3, 0, ARG_MAGIC|BDB_INDEX,
90 bdb_cf_gen, "( OLcfgDbAt:0.2 NAME 'olcDbIndex' "
91 "DESC 'Attribute index parameters' "
92 "EQUALITY caseIgnoreMatch "
93 "SYNTAX OMsDirectoryString )", NULL, NULL },
94 { "linearindex", NULL, 1, 2, 0, ARG_ON_OFF|ARG_OFFSET,
95 (void *)offsetof(struct bdb_info, bi_linear_index),
96 "( OLcfgDbAt:1.7 NAME 'olcDbLinearIndex' "
97 "DESC 'Index attributes one at a time' "
98 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
99 { "lockdetect", "policy", 2, 2, 0, ARG_MAGIC|BDB_LOCKD,
100 bdb_cf_gen, "( OLcfgDbAt:1.8 NAME 'olcDbLockDetect' "
101 "DESC 'Deadlock detection algorithm' "
102 "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
103 { "mode", "mode", 2, 2, 0, ARG_INT|ARG_OFFSET,
104 (void *)offsetof(struct bdb_info, bi_dbenv_mode),
105 "( OLcfgDbAt:0.3 NAME 'olcDbMode' "
106 "DESC 'Unix permissions of database files' "
107 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
108 { "searchstack", "depth", 2, 2, 0, ARG_INT|ARG_MAGIC|BDB_SSTACK,
109 bdb_cf_gen, "( OLcfgDbAt:1.9 NAME 'olcDbSearchStack' "
110 "DESC 'Depth of search stack in IDLs' "
111 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
112 { "shm_key", "key", 2, 2, 0, ARG_INT|ARG_OFFSET,
113 (void *)offsetof(struct bdb_info, bi_shm_key),
114 "( OLcfgDbAt:1.10 NAME 'olcDbShmKey' "
115 "DESC 'Key for shared memory region' "
116 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
117 { NULL, NULL, 0, 0, 0, ARG_IGNORED,
118 NULL, NULL, NULL, NULL }
121 static ConfigOCs bdbocs[] = {
125 "NAME 'olcHdbConfig' "
126 "DESC 'HDB backend configuration' "
129 "NAME 'olcBdbConfig' "
130 "DESC 'BDB backend configuration' "
132 "SUP olcDatabaseConfig "
133 "MUST olcDbDirectory "
134 "MAY ( olcDbCacheSize $ olcDbCheckpoint $ olcDbConfig $ "
135 "olcDbNoSync $ olcDbDirtyRead $ olcDbIDLcacheSize $ "
136 "olcDbIndex $ olcDbLinearIndex $ olcDbLockDetect $ "
137 "olcDbMode $ olcDbSearchStack $ olcDbShmKey ) )",
138 Cft_Database, bdbcfg },
142 static slap_verbmasks bdb_lockd[] = {
143 { BER_BVC("default"), DB_LOCK_DEFAULT },
144 { BER_BVC("oldest"), DB_LOCK_OLDEST },
145 { BER_BVC("random"), DB_LOCK_RANDOM },
146 { BER_BVC("youngest"), DB_LOCK_YOUNGEST },
147 { BER_BVC("fewest"), DB_LOCK_MINLOCKS },
151 /* perform periodic checkpoints */
153 bdb_checkpoint( void *ctx, void *arg )
155 struct re_s *rtask = arg;
156 struct bdb_info *bdb = rtask->arg;
158 TXN_CHECKPOINT( bdb->bi_dbenv, bdb->bi_txn_cp_kbyte,
159 bdb->bi_txn_cp_min, 0 );
160 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
161 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
162 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
166 /* reindex entries on the fly */
168 bdb_online_index( void *ctx, void *arg )
170 struct re_s *rtask = arg;
171 BackendDB *be = rtask->arg;
172 struct bdb_info *bdb = be->be_private;
174 Connection conn = {0};
175 OperationBuffer opbuf;
176 Operation *op = (Operation *) &opbuf;
187 connection_fake_init( &conn, op, ctx );
196 key.size = key.ulen = sizeof(ID);
197 key.flags = DB_DBT_USERMEM;
199 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
200 data.dlen = data.ulen = 0;
203 if ( slapd_shutdown )
206 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &txn, bdb->bi_db_opflags );
209 locker = TXN_ID( txn );
212 BDB_ID2DISK( id, &nid );
213 rc = bdb->bi_id2entry->bdi_db->cursor(
214 bdb->bi_id2entry->bdi_db, txn, &curs, bdb->bi_db_opflags );
219 rc = curs->c_get( curs, &key, &data, DB_SET_RANGE );
220 curs->c_close( curs );
223 if ( rc == DB_NOTFOUND )
225 if ( rc == DB_LOCK_DEADLOCK ) {
226 ldap_pvt_thread_yield();
231 BDB_DISK2ID( &nid, &id );
235 rc = bdb_cache_find_id( op, txn, id, &ei, 0, locker, &lock );
238 if ( rc == DB_LOCK_DEADLOCK ) {
239 ldap_pvt_thread_yield();
242 if ( rc == DB_NOTFOUND ) {
250 rc = bdb_index_entry( op, txn, BDB_INDEX_UPDATE_OP, ei->bei_e );
251 if ( rc == DB_LOCK_DEADLOCK ) {
253 ldap_pvt_thread_yield();
257 rc = TXN_COMMIT( txn, 0 );
267 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
268 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
269 bdb->bi_index_task = NULL;
270 ldap_pvt_runqueue_remove( &slapd_rq, rtask );
271 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
276 /* Cleanup loose ends after Modify completes */
278 bdb_cf_cleanup( ConfigArgs *c )
280 struct bdb_info *bdb = c->be->be_private;
283 if ( bdb->bi_flags & BDB_UPD_CONFIG ) {
284 if ( bdb->bi_db_config ) {
286 FILE *f = fopen( bdb->bi_db_config_path, "w" );
288 for (i=0; bdb->bi_db_config[i].bv_val; i++)
289 fprintf( f, "%s\n", bdb->bi_db_config[i].bv_val );
293 unlink( bdb->bi_db_config_path );
295 bdb->bi_flags ^= BDB_UPD_CONFIG;
298 if ( bdb->bi_flags & BDB_DEL_INDEX ) {
299 bdb_attr_flush( bdb );
300 bdb->bi_flags ^= BDB_DEL_INDEX;
303 if ( bdb->bi_flags & BDB_RE_OPEN ) {
304 bdb->bi_flags ^= BDB_RE_OPEN;
305 rc = c->be->bd_info->bi_db_close( c->be );
307 rc = c->be->bd_info->bi_db_open( c->be );
308 /* If this fails, we need to restart */
311 Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(bdb_cf_cleanup)
312 ": failed to reopen database, rc=%d", rc, 0, 0 );
319 bdb_cf_gen(ConfigArgs *c)
321 struct bdb_info *bdb = c->be->be_private;
324 if ( c->op == SLAP_CONFIG_EMIT ) {
328 if (bdb->bi_txn_cp ) {
331 bv.bv_len = sprintf( buf, "%d %d", bdb->bi_txn_cp_kbyte,
332 bdb->bi_txn_cp_min );
334 value_add_one( &c->rvalue_vals, &bv );
341 if ( bdb->bi_dbenv_home ) {
342 c->value_string = ch_strdup( bdb->bi_dbenv_home );
349 if (( slapMode&SLAP_SERVER_MODE ) && !( bdb->bi_flags&BDB_IS_OPEN )
350 && !bdb->bi_db_config ) {
351 char buf[SLAP_TEXT_BUFLEN];
352 FILE *f = fopen( bdb->bi_db_config_path, "r" );
356 bdb->bi_flags |= BDB_HAS_CONFIG;
357 while ( fgets( buf, sizeof(buf), f )) {
358 ber_str2bv( buf, 0, 1, &bv );
359 if ( bv.bv_len > 0 && bv.bv_val[bv.bv_len-1] == '\n' ) {
361 bv.bv_val[bv.bv_len] = '\0';
363 /* shouldn't need this, but ... */
364 if ( bv.bv_len > 0 && bv.bv_val[bv.bv_len-1] == '\r' ) {
366 bv.bv_val[bv.bv_len] = '\0';
368 ber_bvarray_add( &bdb->bi_db_config, &bv );
373 if ( bdb->bi_db_config ) {
378 for (i=0; !BER_BVISNULL(&bdb->bi_db_config[i]); i++) {
379 bv.bv_len = sprintf( bv.bv_val, "{%d}%s", i,
380 bdb->bi_db_config[i].bv_val );
381 value_add_one( &c->rvalue_vals, &bv );
384 if ( !c->rvalue_vals ) rc = 1;
388 if ( bdb->bi_dbenv_xflags & DB_TXN_NOSYNC )
393 bdb_attr_index_unparse( bdb, &c->rvalue_vals );
394 if ( !c->rvalue_vals ) rc = 1;
399 if ( bdb->bi_lock_detect != DB_LOCK_DEFAULT ) {
401 for (i=0; !BER_BVISNULL(&bdb_lockd[i].word); i++) {
402 if ( bdb->bi_lock_detect == bdb_lockd[i].mask ) {
403 value_add_one( &c->rvalue_vals, &bdb_lockd[i].word );
412 c->value_int = bdb->bi_search_stack_depth;
416 } else if ( c->op == LDAP_MOD_DELETE ) {
419 /* single-valued no-ops */
425 if ( bdb->bi_txn_cp_task ) {
426 struct re_s *re = bdb->bi_txn_cp_task;
427 bdb->bi_txn_cp_task = NULL;
428 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ))
429 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
430 ldap_pvt_runqueue_remove( &slapd_rq, re );
436 ber_bvarray_free( bdb->bi_db_config );
437 bdb->bi_db_config = NULL;
440 ch_free( bdb->bi_db_config[i].bv_val );
441 for (; bdb->bi_db_config[i].bv_val; i++)
442 bdb->bi_db_config[i] = bdb->bi_db_config[i+1];
444 bdb->bi_flags |= BDB_UPD_CONFIG;
445 c->cleanup = bdb_cf_cleanup;
448 bdb->bi_flags |= BDB_RE_OPEN;
449 bdb->bi_flags ^= BDB_HAS_CONFIG;
450 ch_free( bdb->bi_dbenv_home );
451 bdb->bi_dbenv_home = NULL;
452 ch_free( bdb->bi_db_config_path );
453 bdb->bi_db_config_path = NULL;
454 c->cleanup = bdb_cf_cleanup;
455 ldap_pvt_thread_pool_purgekey( bdb->bi_dbenv );
456 ldap_pvt_thread_pool_purgekey( ((char *)bdb->bi_dbenv) + 1 );
459 bdb->bi_dbenv->set_flags( bdb->bi_dbenv, DB_TXN_NOSYNC, 0 );
462 AttributeDescription *ad = NULL;
463 struct berval bv, def = BER_BVC("default");
466 for (ptr = c->line; !isspace( *ptr ); ptr++);
468 bv.bv_len = ptr - bv.bv_val;
469 if ( bvmatch( &bv, &def )) {
470 bdb->bi_defaultmask = 0;
472 slap_bv2ad( &bv, &ad, &text );
474 AttrInfo *ai = bdb_attr_mask( bdb, ad );
475 ai->ai_indexmask |= BDB_INDEX_DELETING;
476 bdb->bi_flags |= BDB_DEL_INDEX;
477 c->cleanup = bdb_cf_cleanup;
489 bdb->bi_txn_cp_kbyte = strtol( c->argv[1], NULL, 0 );
490 bdb->bi_txn_cp_min = strtol( c->argv[2], NULL, 0 );
491 /* If we're in server mode and time-based checkpointing is enabled,
492 * submit a task to perform periodic checkpoints.
494 if ((slapMode & SLAP_SERVER_MODE) && bdb->bi_txn_cp_min ) {
495 struct re_s *re = bdb->bi_txn_cp_task;
497 re->interval.tv_sec = bdb->bi_txn_cp_min * 60;
499 if ( c->be->be_suffix == NULL || BER_BVISNULL( &c->be->be_suffix[0] ) ) {
500 fprintf( stderr, "%s: "
501 "\"checkpoint\" must occur after \"suffix\".\n",
505 bdb->bi_txn_cp_task = ldap_pvt_runqueue_insert( &slapd_rq,
506 bdb->bi_txn_cp_min * 60, bdb_checkpoint, bdb,
507 LDAP_XSTRING(bdb_checkpoint), c->be->be_suffix[0].bv_val );
516 if ( c->op == SLAP_CONFIG_ADD ) {
517 ptr += STRLENOF("dbconfig");
518 while (!isspace(*ptr)) ptr++;
519 while (isspace(*ptr)) ptr++;
522 if ( bdb->bi_flags & BDB_IS_OPEN ) {
523 bdb->bi_flags |= BDB_UPD_CONFIG;
524 c->cleanup = bdb_cf_cleanup;
526 /* If we're just starting up...
529 /* If a DB_CONFIG file exists, or we don't know the path
530 * to the DB_CONFIG file, ignore these directives
532 if (( bdb->bi_flags & BDB_HAS_CONFIG ) || !bdb->bi_db_config_path )
534 f = fopen( bdb->bi_db_config_path, "a" );
536 /* FIXME: EBCDIC probably needs special handling */
537 fprintf( f, "%s\n", ptr );
541 ber_str2bv( ptr, 0, 1, &bv );
542 ber_bvarray_add( &bdb->bi_db_config, &bv );
546 case BDB_DIRECTORY: {
550 if ( bdb->bi_dbenv_home )
551 ch_free( bdb->bi_dbenv_home );
552 bdb->bi_dbenv_home = c->value_string;
554 /* See if a DB_CONFIG file already exists here */
555 if ( bdb->bi_db_config_path )
556 ch_free( bdb->bi_db_config_path );
557 bdb->bi_db_config_path = ch_malloc( strlen( bdb->bi_dbenv_home ) +
558 STRLENOF(LDAP_DIRSEP) + STRLENOF("DB_CONFIG") + 1 );
559 ptr = lutil_strcopy( bdb->bi_db_config_path, bdb->bi_dbenv_home );
560 *ptr++ = LDAP_DIRSEP[0];
561 strcpy( ptr, "DB_CONFIG" );
563 f = fopen( bdb->bi_db_config_path, "r" );
565 bdb->bi_flags |= BDB_HAS_CONFIG;
573 bdb->bi_dbenv_xflags |= DB_TXN_NOSYNC;
575 bdb->bi_dbenv_xflags &= ~DB_TXN_NOSYNC;
576 if ( bdb->bi_flags & BDB_IS_OPEN ) {
577 bdb->bi_dbenv->set_flags( bdb->bi_dbenv, DB_TXN_NOSYNC,
583 rc = bdb_attr_index_config( bdb, c->fname, c->lineno,
584 c->argc - 1, &c->argv[1] );
586 if( rc != LDAP_SUCCESS ) return 1;
587 if (( bdb->bi_flags & BDB_IS_OPEN ) && !bdb->bi_index_task ) {
588 /* Start the task as soon as we finish here. Set a long
589 * interval (10 hours) so that it only gets scheduled once.
591 if ( c->be->be_suffix == NULL || BER_BVISNULL( &c->be->be_suffix[0] ) ) {
592 fprintf( stderr, "%s: "
593 "\"index\" must occur after \"suffix\".\n",
597 bdb->bi_index_task = ldap_pvt_runqueue_insert( &slapd_rq, 36000,
598 bdb_online_index, c->be,
599 LDAP_XSTRING(bdb_online_index), c->be->be_suffix[0].bv_val );
604 rc = verb_to_mask( c->argv[1], bdb_lockd );
605 if ( BER_BVISNULL(&bdb_lockd[rc].word) ) {
606 fprintf( stderr, "%s: "
607 "bad policy (%s) in \"lockDetect <policy>\" line\n",
608 c->log, c->argv[1] );
611 bdb->bi_lock_detect = rc;
615 if ( c->value_int < MINIMUM_SEARCH_STACK_DEPTH ) {
617 "%s: depth %d too small, using %d\n",
618 c->log, c->value_int, MINIMUM_SEARCH_STACK_DEPTH );
619 c->value_int = MINIMUM_SEARCH_STACK_DEPTH;
621 bdb->bi_search_stack_depth = c->value_int;
627 int bdb_back_init_cf( BackendInfo *bi )
630 bi->bi_cf_ocs = bdbocs;
632 rc = config_register_schema( bdbcfg, bdbocs );