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_oc BDB_SYMBOL(cf_oc)
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 ObjectClass *bdb_oc;
41 static ConfigDriver bdb_cf_oc, bdb_cf_gen;
54 static ConfigTable bdbcfg[] = {
55 { "", "", 0, 0, 0, ARG_MAGIC,
56 bdb_cf_oc, NULL, NULL, NULL },
57 { "directory", "dir", 2, 2, 0, ARG_STRING|ARG_MAGIC|BDB_DIRECTORY,
58 bdb_cf_gen, "( OLcfgDbAt:0.1 NAME 'olcDbDirectory' "
59 "DESC 'Directory for database content' "
60 "EQUALITY caseIgnoreMatch "
61 "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
62 { "cachesize", "size", 2, 2, 0, ARG_INT|ARG_OFFSET,
63 (void *)offsetof(struct bdb_info, bi_cache.c_maxsize),
64 "( OLcfgDbAt:1.1 NAME 'olcDbCacheSize' "
65 "DESC 'Entry cache size in entries' "
66 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
67 { "checkpoint", "kbyte> <min", 3, 3, 0, ARG_MAGIC|BDB_CHKPT,
68 bdb_cf_gen, "( OLcfgDbAt:1.2 NAME 'olcDbCheckpoint' "
69 "DESC 'Database checkpoint interval in kbytes and minutes' "
70 "SYNTAX OMsDirectoryString SINGLE-VALUE )",NULL, NULL },
71 { "dbconfig", "DB_CONFIG setting", 3, 0, 0, ARG_MAGIC|BDB_CONFIG,
72 bdb_cf_gen, "( OLcfgDbAt:1.3 NAME 'olcDbConfig' "
73 "DESC 'BerkeleyDB DB_CONFIG configuration directives' "
74 "SYNTAX OMsDirectoryString )",NULL, NULL },
75 { "dbnosync", NULL, 1, 2, 0, ARG_ON_OFF|ARG_MAGIC|BDB_NOSYNC,
76 bdb_cf_gen, "( OLcfgDbAt:1.4 NAME 'olcDbNoSync' "
77 "DESC 'Disable synchronous database writes' "
78 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
79 { "dirtyread", NULL, 1, 2, 0,
80 #ifdef SLAP_BDB_ALLOW_DIRTY_READ
81 ARG_ON_OFF|ARG_MAGIC|BDB_DIRTYR, bdb_cf_gen,
85 "( OLcfgDbAt:1.5 NAME 'olcDbDirtyRead' "
86 "DESC 'Allow reads of uncommitted data' "
87 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
88 { "idlcachesize", "size", 2, 2, 0, ARG_INT|ARG_OFFSET,
89 (void *)offsetof(struct bdb_info,bi_idl_cache_max_size),
90 "( OLcfgDbAt:1.6 NAME 'olcDbIDLcacheSize' "
91 "DESC 'IDL cache size in IDLs' "
92 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
93 { "index", "attr> <[pres,eq,approx,sub]", 2, 3, 0, ARG_MAGIC|BDB_INDEX,
94 bdb_cf_gen, "( OLcfgDbAt:0.2 NAME 'olcDbIndex' "
95 "DESC 'Attribute index parameters' "
96 "EQUALITY caseIgnoreMatch "
97 "SYNTAX OMsDirectoryString )", NULL, NULL },
98 { "linearindex", NULL, 1, 2, 0, ARG_ON_OFF|ARG_OFFSET,
99 (void *)offsetof(struct bdb_info, bi_linear_index),
100 "( OLcfgDbAt:1.7 NAME 'olcDbLinearIndex' "
101 "DESC 'Index attributes one at a time' "
102 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
103 { "lockdetect", "policy", 2, 2, 0, ARG_MAGIC|BDB_LOCKD,
104 bdb_cf_gen, "( OLcfgDbAt:1.8 NAME 'olcDbLockDetect' "
105 "DESC 'Deadlock detection algorithm' "
106 "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
107 { "mode", "mode", 2, 2, 0, ARG_INT|ARG_OFFSET,
108 (void *)offsetof(struct bdb_info, bi_dbenv_mode),
109 "( OLcfgDbAt:0.3 NAME 'olcDbMode' "
110 "DESC 'Unix permissions of database files' "
111 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
112 { "searchstack", "depth", 2, 2, 0, ARG_INT|ARG_MAGIC|BDB_SSTACK,
113 bdb_cf_gen, "( OLcfgDbAt:1.9 NAME 'olcDbSearchStack' "
114 "DESC 'Depth of search stack in IDLs' "
115 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
116 { "shm_key", "key", 2, 2, 0, ARG_INT|ARG_OFFSET,
117 (void *)offsetof(struct bdb_info, bi_shm_key),
118 "( OLcfgDbAt:1.10 NAME 'olcDbShmKey' "
119 "DESC 'Key for shared memory region' "
120 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
121 { NULL, NULL, 0, 0, 0, ARG_IGNORED,
122 NULL, NULL, NULL, NULL }
125 static ConfigOCs bdbocs[] = {
127 "NAME 'olcBdbConfig' "
128 "DESC 'BDB backend configuration' "
129 "SUP olcDatabaseConfig "
130 "MUST olcDbDirectory "
131 "MAY ( olcDbCacheSize $ olcDbCheckpoint $ olcDbConfig $ "
132 "olcDbNoSync $ olcDbDirtyRead $ olcDbIDLcacheSize $ "
133 "olcDbIndex $ olcDbLinearIndex $ olcDbLockDetect $ "
134 "olcDbMode $ olcDbSearchStack $ olcDbShmKey ) )",
135 Cft_Database, &bdb_oc },
140 bdb_cf_oc(ConfigArgs *c)
142 if ( c->op == SLAP_CONFIG_EMIT ) {
143 value_add_one( &c->rvalue_vals, &bdb_oc->soc_cname );
149 static slap_verbmasks bdb_lockd[] = {
150 { BER_BVC("default"), DB_LOCK_DEFAULT },
151 { BER_BVC("oldest"), DB_LOCK_OLDEST },
152 { BER_BVC("random"), DB_LOCK_RANDOM },
153 { BER_BVC("youngest"), DB_LOCK_YOUNGEST },
154 { BER_BVC("fewest"), DB_LOCK_MINLOCKS },
158 /* perform periodic checkpoints */
160 bdb_checkpoint( void *ctx, void *arg )
162 struct re_s *rtask = arg;
163 struct bdb_info *bdb = rtask->arg;
165 TXN_CHECKPOINT( bdb->bi_dbenv, bdb->bi_txn_cp_kbyte,
166 bdb->bi_txn_cp_min, 0 );
167 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
168 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
169 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
173 /* reindex entries on the fly */
175 bdb_online_index( void *ctx, void *arg )
177 struct re_s *rtask = arg;
178 BackendDB *be = rtask->arg;
179 struct bdb_info *bdb = be->be_private;
181 Connection conn = {0};
182 char opbuf[OPERATION_BUFFER_SIZE];
183 Operation *op = (Operation *)opbuf;
194 connection_fake_init( &conn, op, ctx );
203 key.size = key.ulen = sizeof(ID);
204 key.flags = DB_DBT_USERMEM;
206 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
207 data.dlen = data.ulen = 0;
210 if ( slapd_shutdown )
213 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &txn, bdb->bi_db_opflags );
216 locker = TXN_ID( txn );
219 BDB_ID2DISK( id, &nid );
220 rc = bdb->bi_id2entry->bdi_db->cursor(
221 bdb->bi_id2entry->bdi_db, txn, &curs, bdb->bi_db_opflags );
226 rc = curs->c_get( curs, &key, &data, DB_SET_RANGE );
227 curs->c_close( curs );
230 if ( rc == DB_NOTFOUND )
232 if ( rc == DB_LOCK_DEADLOCK ) {
233 ldap_pvt_thread_yield();
238 BDB_DISK2ID( &nid, &id );
242 rc = bdb_cache_find_id( op, txn, id, &ei, 0, locker, &lock );
245 if ( rc == DB_LOCK_DEADLOCK ) {
246 ldap_pvt_thread_yield();
249 if ( rc == DB_NOTFOUND ) {
257 rc = bdb_index_entry( op, txn, BDB_INDEX_UPDATE_OP, ei->bei_e );
258 if ( rc == DB_LOCK_DEADLOCK ) {
260 ldap_pvt_thread_yield();
264 rc = TXN_COMMIT( txn, 0 );
274 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
275 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
276 bdb->bi_index_task = NULL;
277 ldap_pvt_runqueue_remove( &slapd_rq, rtask );
278 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
283 /* Cleanup loose ends after Modify completes */
285 bdb_cf_cleanup( ConfigArgs *c )
287 struct bdb_info *bdb = c->be->be_private;
290 if ( bdb->bi_flags & BDB_UPD_CONFIG ) {
291 if ( bdb->bi_db_config ) {
293 FILE *f = fopen( bdb->bi_db_config_path, "w" );
295 for (i=0; bdb->bi_db_config[i].bv_val; i++)
296 fprintf( f, "%s\n", bdb->bi_db_config[i].bv_val );
300 unlink( bdb->bi_db_config_path );
302 bdb->bi_flags ^= BDB_UPD_CONFIG;
305 if ( bdb->bi_flags & BDB_DEL_INDEX ) {
306 bdb_attr_flush( bdb );
307 bdb->bi_flags ^= BDB_DEL_INDEX;
310 if ( bdb->bi_flags & BDB_RE_OPEN ) {
311 bdb->bi_flags ^= BDB_RE_OPEN;
312 rc = c->be->bd_info->bi_db_close( c->be );
314 rc = c->be->bd_info->bi_db_open( c->be );
315 /* If this fails, we need to restart */
318 Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(bdb_cf_cleanup)
319 ": failed to reopen database, rc=%d", rc, 0, 0 );
326 bdb_cf_gen(ConfigArgs *c)
328 struct bdb_info *bdb = c->be->be_private;
331 if ( c->op == SLAP_CONFIG_EMIT ) {
335 if (bdb->bi_txn_cp ) {
338 bv.bv_len = sprintf( buf, "%d %d", bdb->bi_txn_cp_kbyte,
339 bdb->bi_txn_cp_min );
341 value_add_one( &c->rvalue_vals, &bv );
348 if ( bdb->bi_dbenv_home ) {
349 c->value_string = ch_strdup( bdb->bi_dbenv_home );
356 if (( slapMode&SLAP_SERVER_MODE ) && !( bdb->bi_flags&BDB_IS_OPEN )
357 && !bdb->bi_db_config ) {
358 char buf[SLAP_TEXT_BUFLEN];
359 FILE *f = fopen( bdb->bi_db_config_path, "r" );
363 bdb->bi_flags |= BDB_HAS_CONFIG;
364 while ( fgets( buf, sizeof(buf), f )) {
365 ber_str2bv( buf, 0, 1, &bv );
366 if ( bv.bv_val[bv.bv_len-1] == '\n' ) {
368 bv.bv_val[bv.bv_len] = '\0';
370 /* shouldn't need this, but ... */
371 if ( bv.bv_val[bv.bv_len-1] == '\r' ) {
373 bv.bv_val[bv.bv_len] = '\0';
375 ber_bvarray_add( &bdb->bi_db_config, &bv );
380 if ( bdb->bi_db_config ) {
385 for (i=0; !BER_BVISNULL(&bdb->bi_db_config[i]); i++) {
386 bv.bv_len = sprintf( bv.bv_val, "{%d}%s", i,
387 bdb->bi_db_config[i].bv_val );
388 value_add_one( &c->rvalue_vals, &bv );
391 if ( !c->rvalue_vals ) rc = 1;
395 if ( bdb->bi_dbenv_xflags & DB_TXN_NOSYNC )
400 bdb_attr_index_unparse( bdb, &c->rvalue_vals );
401 if ( !c->rvalue_vals ) rc = 1;
406 if ( bdb->bi_lock_detect != DB_LOCK_DEFAULT ) {
408 for (i=0; !BER_BVISNULL(&bdb_lockd[i].word); i++) {
409 if ( bdb->bi_lock_detect == bdb_lockd[i].mask ) {
410 value_add_one( &c->rvalue_vals, &bdb_lockd[i].word );
419 c->value_int = bdb->bi_search_stack_depth;
423 } else if ( c->op == LDAP_MOD_DELETE ) {
426 /* single-valued no-ops */
432 if ( bdb->bi_txn_cp_task ) {
433 struct re_s *re = bdb->bi_txn_cp_task;
434 bdb->bi_txn_cp_task = NULL;
435 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ))
436 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
437 ldap_pvt_runqueue_remove( &slapd_rq, re );
443 ber_bvarray_free( bdb->bi_db_config );
444 bdb->bi_db_config = NULL;
447 ch_free( bdb->bi_db_config[i].bv_val );
448 for (; bdb->bi_db_config[i].bv_val; i++)
449 bdb->bi_db_config[i] = bdb->bi_db_config[i+1];
451 bdb->bi_flags |= BDB_UPD_CONFIG;
452 c->cleanup = bdb_cf_cleanup;
455 bdb->bi_flags |= BDB_RE_OPEN;
456 bdb->bi_flags ^= BDB_HAS_CONFIG;
457 ch_free( bdb->bi_dbenv_home );
458 bdb->bi_dbenv_home = NULL;
459 ch_free( bdb->bi_db_config_path );
460 bdb->bi_db_config_path = NULL;
461 c->cleanup = bdb_cf_cleanup;
462 ldap_pvt_thread_pool_purgekey( bdb->bi_dbenv );
463 ldap_pvt_thread_pool_purgekey( ((char *)bdb->bi_dbenv) + 1 );
466 bdb->bi_dbenv->set_flags( bdb->bi_dbenv, DB_TXN_NOSYNC, 0 );
469 AttributeDescription *ad = NULL;
470 struct berval bv, def = BER_BVC("default");
473 for (ptr = c->line; !isspace( *ptr ); ptr++);
475 bv.bv_len = ptr - bv.bv_val;
476 if ( bvmatch( &bv, &def )) {
477 bdb->bi_defaultmask = 0;
479 slap_bv2ad( &bv, &ad, &text );
481 AttrInfo *ai = bdb_attr_mask( bdb, ad );
482 ai->ai_indexmask |= BDB_INDEX_DELETING;
483 bdb->bi_flags |= BDB_DEL_INDEX;
484 c->cleanup = bdb_cf_cleanup;
496 bdb->bi_txn_cp_kbyte = strtol( c->argv[1], NULL, 0 );
497 bdb->bi_txn_cp_min = strtol( c->argv[2], NULL, 0 );
498 /* If we're in server mode and time-based checkpointing is enabled,
499 * submit a task to perform periodic checkpoints.
501 if ((slapMode & SLAP_SERVER_MODE) && bdb->bi_txn_cp_min ) {
502 struct re_s *re = bdb->bi_txn_cp_task;
504 re->interval.tv_sec = bdb->bi_txn_cp_min * 60;
506 bdb->bi_txn_cp_task = ldap_pvt_runqueue_insert( &slapd_rq,
507 bdb->bi_txn_cp_min * 60, bdb_checkpoint, bdb,
508 LDAP_XSTRING(bdb_checkpoint), c->be->be_suffix[0].bv_val );
513 char *ptr = c->line + STRLENOF("dbconfig");
515 while (!isspace(*ptr)) ptr++;
516 while (isspace(*ptr)) ptr++;
518 if ( bdb->bi_flags & BDB_IS_OPEN ) {
519 bdb->bi_flags |= BDB_UPD_CONFIG;
520 c->cleanup = bdb_cf_cleanup;
522 /* If we're just starting up...
525 /* If a DB_CONFIG file exists, or we don't know the path
526 * to the DB_CONFIG file, ignore these directives
528 if (( bdb->bi_flags & BDB_HAS_CONFIG ) || !bdb->bi_db_config_path )
530 f = fopen( bdb->bi_db_config_path, "a" );
532 /* FIXME: EBCDIC probably needs special handling */
533 fprintf( f, "%s\n", ptr );
537 ber_str2bv( ptr, 0, 1, &bv );
538 ber_bvarray_add( &bdb->bi_db_config, &bv );
542 case BDB_DIRECTORY: {
546 if ( bdb->bi_dbenv_home )
547 ch_free( bdb->bi_dbenv_home );
548 bdb->bi_dbenv_home = c->value_string;
550 /* See if a DB_CONFIG file already exists here */
551 if ( bdb->bi_db_config_path )
552 ch_free( bdb->bi_db_config_path );
553 bdb->bi_db_config_path = ch_malloc( strlen( bdb->bi_dbenv_home ) +
554 STRLENOF(LDAP_DIRSEP) + STRLENOF("DB_CONFIG") + 1 );
555 ptr = lutil_strcopy( bdb->bi_db_config_path, bdb->bi_dbenv_home );
556 *ptr++ = LDAP_DIRSEP[0];
557 strcpy( ptr, "DB_CONFIG" );
559 f = fopen( bdb->bi_db_config_path, "r" );
561 bdb->bi_flags |= BDB_HAS_CONFIG;
569 bdb->bi_dbenv_xflags |= DB_TXN_NOSYNC;
571 bdb->bi_dbenv_xflags &= ~DB_TXN_NOSYNC;
572 if ( bdb->bi_flags & BDB_IS_OPEN ) {
573 bdb->bi_dbenv->set_flags( bdb->bi_dbenv, DB_TXN_NOSYNC,
579 rc = bdb_attr_index_config( bdb, c->fname, c->lineno,
580 c->argc - 1, &c->argv[1] );
582 if( rc != LDAP_SUCCESS ) return 1;
583 if (( bdb->bi_flags & BDB_IS_OPEN ) && !bdb->bi_index_task ) {
584 /* Start the task as soon as we finish here. Set a long
585 * interval (10 hours) so that it only gets scheduled once.
587 bdb->bi_index_task = ldap_pvt_runqueue_insert( &slapd_rq, 36000,
588 bdb_online_index, c->be,
589 LDAP_XSTRING(bdb_online_index), c->be->be_suffix[0].bv_val );
594 rc = verb_to_mask( c->argv[1], bdb_lockd );
595 if ( BER_BVISNULL(&bdb_lockd[rc].word) ) {
596 fprintf( stderr, "%s: "
597 "bad policy (%s) in \"lockDetect <policy>\" line\n",
598 c->log, c->argv[1] );
601 bdb->bi_lock_detect = rc;
605 if ( c->value_int < MINIMUM_SEARCH_STACK_DEPTH ) {
607 "%s: depth %d too small, using %d\n",
608 c->log, c->value_int, MINIMUM_SEARCH_STACK_DEPTH );
609 c->value_int = MINIMUM_SEARCH_STACK_DEPTH;
611 bdb->bi_search_stack_depth = c->value_int;
617 int bdb_back_init_cf( BackendInfo *bi )
620 bi->bi_cf_table = bdbcfg;
622 rc = config_register_schema( bdbcfg, bdbocs );
624 bdbcfg[0].ad = slap_schema.si_ad_objectClass;