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[] = {
122 "NAME 'olcBdbConfig' "
123 "DESC 'BDB backend configuration' "
124 "SUP olcDatabaseConfig "
125 "MUST olcDbDirectory "
126 "MAY ( olcDbCacheSize $ olcDbCheckpoint $ olcDbConfig $ "
127 "olcDbNoSync $ olcDbDirtyRead $ olcDbIDLcacheSize $ "
128 "olcDbIndex $ olcDbLinearIndex $ olcDbLockDetect $ "
129 "olcDbMode $ olcDbSearchStack $ olcDbShmKey ) )",
130 Cft_Database, bdbcfg },
134 static slap_verbmasks bdb_lockd[] = {
135 { BER_BVC("default"), DB_LOCK_DEFAULT },
136 { BER_BVC("oldest"), DB_LOCK_OLDEST },
137 { BER_BVC("random"), DB_LOCK_RANDOM },
138 { BER_BVC("youngest"), DB_LOCK_YOUNGEST },
139 { BER_BVC("fewest"), DB_LOCK_MINLOCKS },
143 /* perform periodic checkpoints */
145 bdb_checkpoint( void *ctx, void *arg )
147 struct re_s *rtask = arg;
148 struct bdb_info *bdb = rtask->arg;
150 TXN_CHECKPOINT( bdb->bi_dbenv, bdb->bi_txn_cp_kbyte,
151 bdb->bi_txn_cp_min, 0 );
152 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
153 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
154 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
158 /* reindex entries on the fly */
160 bdb_online_index( void *ctx, void *arg )
162 struct re_s *rtask = arg;
163 BackendDB *be = rtask->arg;
164 struct bdb_info *bdb = be->be_private;
166 Connection conn = {0};
167 char opbuf[OPERATION_BUFFER_SIZE];
168 Operation *op = (Operation *)opbuf;
179 connection_fake_init( &conn, op, ctx );
188 key.size = key.ulen = sizeof(ID);
189 key.flags = DB_DBT_USERMEM;
191 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
192 data.dlen = data.ulen = 0;
195 if ( slapd_shutdown )
198 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &txn, bdb->bi_db_opflags );
201 locker = TXN_ID( txn );
204 BDB_ID2DISK( id, &nid );
205 rc = bdb->bi_id2entry->bdi_db->cursor(
206 bdb->bi_id2entry->bdi_db, txn, &curs, bdb->bi_db_opflags );
211 rc = curs->c_get( curs, &key, &data, DB_SET_RANGE );
212 curs->c_close( curs );
215 if ( rc == DB_NOTFOUND )
217 if ( rc == DB_LOCK_DEADLOCK ) {
218 ldap_pvt_thread_yield();
223 BDB_DISK2ID( &nid, &id );
227 rc = bdb_cache_find_id( op, txn, id, &ei, 0, locker, &lock );
230 if ( rc == DB_LOCK_DEADLOCK ) {
231 ldap_pvt_thread_yield();
234 if ( rc == DB_NOTFOUND ) {
242 rc = bdb_index_entry( op, txn, BDB_INDEX_UPDATE_OP, ei->bei_e );
243 if ( rc == DB_LOCK_DEADLOCK ) {
245 ldap_pvt_thread_yield();
249 rc = TXN_COMMIT( txn, 0 );
259 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
260 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
261 bdb->bi_index_task = NULL;
262 ldap_pvt_runqueue_remove( &slapd_rq, rtask );
263 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
268 /* Cleanup loose ends after Modify completes */
270 bdb_cf_cleanup( ConfigArgs *c )
272 struct bdb_info *bdb = c->be->be_private;
275 if ( bdb->bi_flags & BDB_UPD_CONFIG ) {
276 if ( bdb->bi_db_config ) {
278 FILE *f = fopen( bdb->bi_db_config_path, "w" );
280 for (i=0; bdb->bi_db_config[i].bv_val; i++)
281 fprintf( f, "%s\n", bdb->bi_db_config[i].bv_val );
285 unlink( bdb->bi_db_config_path );
287 bdb->bi_flags ^= BDB_UPD_CONFIG;
290 if ( bdb->bi_flags & BDB_DEL_INDEX ) {
291 bdb_attr_flush( bdb );
292 bdb->bi_flags ^= BDB_DEL_INDEX;
295 if ( bdb->bi_flags & BDB_RE_OPEN ) {
296 bdb->bi_flags ^= BDB_RE_OPEN;
297 rc = c->be->bd_info->bi_db_close( c->be );
299 rc = c->be->bd_info->bi_db_open( c->be );
300 /* If this fails, we need to restart */
303 Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(bdb_cf_cleanup)
304 ": failed to reopen database, rc=%d", rc, 0, 0 );
311 bdb_cf_gen(ConfigArgs *c)
313 struct bdb_info *bdb = c->be->be_private;
316 if ( c->op == SLAP_CONFIG_EMIT ) {
320 if (bdb->bi_txn_cp ) {
323 bv.bv_len = sprintf( buf, "%d %d", bdb->bi_txn_cp_kbyte,
324 bdb->bi_txn_cp_min );
326 value_add_one( &c->rvalue_vals, &bv );
333 if ( bdb->bi_dbenv_home ) {
334 c->value_string = ch_strdup( bdb->bi_dbenv_home );
341 if (( slapMode&SLAP_SERVER_MODE ) && !( bdb->bi_flags&BDB_IS_OPEN )
342 && !bdb->bi_db_config ) {
343 char buf[SLAP_TEXT_BUFLEN];
344 FILE *f = fopen( bdb->bi_db_config_path, "r" );
348 bdb->bi_flags |= BDB_HAS_CONFIG;
349 while ( fgets( buf, sizeof(buf), f )) {
350 ber_str2bv( buf, 0, 1, &bv );
351 if ( bv.bv_val[bv.bv_len-1] == '\n' ) {
353 bv.bv_val[bv.bv_len] = '\0';
355 /* shouldn't need this, but ... */
356 if ( bv.bv_val[bv.bv_len-1] == '\r' ) {
358 bv.bv_val[bv.bv_len] = '\0';
360 ber_bvarray_add( &bdb->bi_db_config, &bv );
365 if ( bdb->bi_db_config ) {
370 for (i=0; !BER_BVISNULL(&bdb->bi_db_config[i]); i++) {
371 bv.bv_len = sprintf( bv.bv_val, "{%d}%s", i,
372 bdb->bi_db_config[i].bv_val );
373 value_add_one( &c->rvalue_vals, &bv );
376 if ( !c->rvalue_vals ) rc = 1;
380 if ( bdb->bi_dbenv_xflags & DB_TXN_NOSYNC )
385 bdb_attr_index_unparse( bdb, &c->rvalue_vals );
386 if ( !c->rvalue_vals ) rc = 1;
391 if ( bdb->bi_lock_detect != DB_LOCK_DEFAULT ) {
393 for (i=0; !BER_BVISNULL(&bdb_lockd[i].word); i++) {
394 if ( bdb->bi_lock_detect == bdb_lockd[i].mask ) {
395 value_add_one( &c->rvalue_vals, &bdb_lockd[i].word );
404 c->value_int = bdb->bi_search_stack_depth;
408 } else if ( c->op == LDAP_MOD_DELETE ) {
411 /* single-valued no-ops */
417 if ( bdb->bi_txn_cp_task ) {
418 struct re_s *re = bdb->bi_txn_cp_task;
419 bdb->bi_txn_cp_task = NULL;
420 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ))
421 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
422 ldap_pvt_runqueue_remove( &slapd_rq, re );
428 ber_bvarray_free( bdb->bi_db_config );
429 bdb->bi_db_config = NULL;
432 ch_free( bdb->bi_db_config[i].bv_val );
433 for (; bdb->bi_db_config[i].bv_val; i++)
434 bdb->bi_db_config[i] = bdb->bi_db_config[i+1];
436 bdb->bi_flags |= BDB_UPD_CONFIG;
437 c->cleanup = bdb_cf_cleanup;
440 bdb->bi_flags |= BDB_RE_OPEN;
441 bdb->bi_flags ^= BDB_HAS_CONFIG;
442 ch_free( bdb->bi_dbenv_home );
443 bdb->bi_dbenv_home = NULL;
444 ch_free( bdb->bi_db_config_path );
445 bdb->bi_db_config_path = NULL;
446 c->cleanup = bdb_cf_cleanup;
447 ldap_pvt_thread_pool_purgekey( bdb->bi_dbenv );
448 ldap_pvt_thread_pool_purgekey( ((char *)bdb->bi_dbenv) + 1 );
451 bdb->bi_dbenv->set_flags( bdb->bi_dbenv, DB_TXN_NOSYNC, 0 );
454 AttributeDescription *ad = NULL;
455 struct berval bv, def = BER_BVC("default");
458 for (ptr = c->line; !isspace( *ptr ); ptr++);
460 bv.bv_len = ptr - bv.bv_val;
461 if ( bvmatch( &bv, &def )) {
462 bdb->bi_defaultmask = 0;
464 slap_bv2ad( &bv, &ad, &text );
466 AttrInfo *ai = bdb_attr_mask( bdb, ad );
467 ai->ai_indexmask |= BDB_INDEX_DELETING;
468 bdb->bi_flags |= BDB_DEL_INDEX;
469 c->cleanup = bdb_cf_cleanup;
481 bdb->bi_txn_cp_kbyte = strtol( c->argv[1], NULL, 0 );
482 bdb->bi_txn_cp_min = strtol( c->argv[2], NULL, 0 );
483 /* If we're in server mode and time-based checkpointing is enabled,
484 * submit a task to perform periodic checkpoints.
486 if ((slapMode & SLAP_SERVER_MODE) && bdb->bi_txn_cp_min ) {
487 struct re_s *re = bdb->bi_txn_cp_task;
489 re->interval.tv_sec = bdb->bi_txn_cp_min * 60;
491 bdb->bi_txn_cp_task = ldap_pvt_runqueue_insert( &slapd_rq,
492 bdb->bi_txn_cp_min * 60, bdb_checkpoint, bdb,
493 LDAP_XSTRING(bdb_checkpoint), c->be->be_suffix[0].bv_val );
498 char *ptr = c->line + STRLENOF("dbconfig");
500 while (!isspace(*ptr)) ptr++;
501 while (isspace(*ptr)) ptr++;
503 if ( bdb->bi_flags & BDB_IS_OPEN ) {
504 bdb->bi_flags |= BDB_UPD_CONFIG;
505 c->cleanup = bdb_cf_cleanup;
507 /* If we're just starting up...
510 /* If a DB_CONFIG file exists, or we don't know the path
511 * to the DB_CONFIG file, ignore these directives
513 if (( bdb->bi_flags & BDB_HAS_CONFIG ) || !bdb->bi_db_config_path )
515 f = fopen( bdb->bi_db_config_path, "a" );
517 /* FIXME: EBCDIC probably needs special handling */
518 fprintf( f, "%s\n", ptr );
522 ber_str2bv( ptr, 0, 1, &bv );
523 ber_bvarray_add( &bdb->bi_db_config, &bv );
527 case BDB_DIRECTORY: {
531 if ( bdb->bi_dbenv_home )
532 ch_free( bdb->bi_dbenv_home );
533 bdb->bi_dbenv_home = c->value_string;
535 /* See if a DB_CONFIG file already exists here */
536 if ( bdb->bi_db_config_path )
537 ch_free( bdb->bi_db_config_path );
538 bdb->bi_db_config_path = ch_malloc( strlen( bdb->bi_dbenv_home ) +
539 STRLENOF(LDAP_DIRSEP) + STRLENOF("DB_CONFIG") + 1 );
540 ptr = lutil_strcopy( bdb->bi_db_config_path, bdb->bi_dbenv_home );
541 *ptr++ = LDAP_DIRSEP[0];
542 strcpy( ptr, "DB_CONFIG" );
544 f = fopen( bdb->bi_db_config_path, "r" );
546 bdb->bi_flags |= BDB_HAS_CONFIG;
554 bdb->bi_dbenv_xflags |= DB_TXN_NOSYNC;
556 bdb->bi_dbenv_xflags &= ~DB_TXN_NOSYNC;
557 if ( bdb->bi_flags & BDB_IS_OPEN ) {
558 bdb->bi_dbenv->set_flags( bdb->bi_dbenv, DB_TXN_NOSYNC,
564 rc = bdb_attr_index_config( bdb, c->fname, c->lineno,
565 c->argc - 1, &c->argv[1] );
567 if( rc != LDAP_SUCCESS ) return 1;
568 if (( bdb->bi_flags & BDB_IS_OPEN ) && !bdb->bi_index_task ) {
569 /* Start the task as soon as we finish here. Set a long
570 * interval (10 hours) so that it only gets scheduled once.
572 bdb->bi_index_task = ldap_pvt_runqueue_insert( &slapd_rq, 36000,
573 bdb_online_index, c->be,
574 LDAP_XSTRING(bdb_online_index), c->be->be_suffix[0].bv_val );
579 rc = verb_to_mask( c->argv[1], bdb_lockd );
580 if ( BER_BVISNULL(&bdb_lockd[rc].word) ) {
581 fprintf( stderr, "%s: "
582 "bad policy (%s) in \"lockDetect <policy>\" line\n",
583 c->log, c->argv[1] );
586 bdb->bi_lock_detect = rc;
590 if ( c->value_int < MINIMUM_SEARCH_STACK_DEPTH ) {
592 "%s: depth %d too small, using %d\n",
593 c->log, c->value_int, MINIMUM_SEARCH_STACK_DEPTH );
594 c->value_int = MINIMUM_SEARCH_STACK_DEPTH;
596 bdb->bi_search_stack_depth = c->value_int;
602 int bdb_back_init_cf( BackendInfo *bi )
605 bi->bi_cf_ocs = bdbocs;
607 rc = config_register_schema( bdbcfg, bdbocs );