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_gen;
54 static ConfigTable bdbcfg[] = {
55 { "directory", "dir", 2, 2, 0, ARG_STRING|ARG_MAGIC|BDB_DIRECTORY,
56 bdb_cf_gen, "( OLcfgDbAt:0.1 NAME 'olcDbDirectory' "
57 "DESC 'Directory for database content' "
58 "EQUALITY caseIgnoreMatch "
59 "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
60 { "cachesize", "size", 2, 2, 0, ARG_INT|ARG_OFFSET,
61 (void *)offsetof(struct bdb_info, bi_cache.c_maxsize),
62 "( OLcfgDbAt:1.1 NAME 'olcDbCacheSize' "
63 "DESC 'Entry cache size in entries' "
64 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
65 { "checkpoint", "kbyte> <min", 3, 3, 0, ARG_MAGIC|BDB_CHKPT,
66 bdb_cf_gen, "( OLcfgDbAt:1.2 NAME 'olcDbCheckpoint' "
67 "DESC 'Database checkpoint interval in kbytes and minutes' "
68 "SYNTAX OMsDirectoryString SINGLE-VALUE )",NULL, NULL },
69 { "dbconfig", "DB_CONFIG setting", 1, 0, 0, ARG_MAGIC|BDB_CONFIG,
70 bdb_cf_gen, "( OLcfgDbAt:1.3 NAME 'olcDbConfig' "
71 "DESC 'BerkeleyDB DB_CONFIG configuration directives' "
72 "SYNTAX OMsDirectoryString )",NULL, NULL },
73 { "dbnosync", NULL, 1, 2, 0, ARG_ON_OFF|ARG_MAGIC|BDB_NOSYNC,
74 bdb_cf_gen, "( OLcfgDbAt:1.4 NAME 'olcDbNoSync' "
75 "DESC 'Disable synchronous database writes' "
76 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
77 { "dirtyread", NULL, 1, 2, 0,
78 #ifdef SLAP_BDB_ALLOW_DIRTY_READ
79 ARG_ON_OFF|ARG_MAGIC|BDB_DIRTYR, bdb_cf_gen,
83 "( OLcfgDbAt:1.5 NAME 'olcDbDirtyRead' "
84 "DESC 'Allow reads of uncommitted data' "
85 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
86 { "idlcachesize", "size", 2, 2, 0, ARG_INT|ARG_OFFSET,
87 (void *)offsetof(struct bdb_info,bi_idl_cache_max_size),
88 "( OLcfgDbAt:1.6 NAME 'olcDbIDLcacheSize' "
89 "DESC 'IDL cache size in IDLs' "
90 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
91 { "index", "attr> <[pres,eq,approx,sub]", 2, 3, 0, ARG_MAGIC|BDB_INDEX,
92 bdb_cf_gen, "( OLcfgDbAt:0.2 NAME 'olcDbIndex' "
93 "DESC 'Attribute index parameters' "
94 "EQUALITY caseIgnoreMatch "
95 "SYNTAX OMsDirectoryString )", NULL, NULL },
96 { "linearindex", NULL, 1, 2, 0, ARG_ON_OFF|ARG_OFFSET,
97 (void *)offsetof(struct bdb_info, bi_linear_index),
98 "( OLcfgDbAt:1.7 NAME 'olcDbLinearIndex' "
99 "DESC 'Index attributes one at a time' "
100 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
101 { "lockdetect", "policy", 2, 2, 0, ARG_MAGIC|BDB_LOCKD,
102 bdb_cf_gen, "( OLcfgDbAt:1.8 NAME 'olcDbLockDetect' "
103 "DESC 'Deadlock detection algorithm' "
104 "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
105 { "mode", "mode", 2, 2, 0, ARG_INT|ARG_OFFSET,
106 (void *)offsetof(struct bdb_info, bi_dbenv_mode),
107 "( OLcfgDbAt:0.3 NAME 'olcDbMode' "
108 "DESC 'Unix permissions of database files' "
109 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
110 { "searchstack", "depth", 2, 2, 0, ARG_INT|ARG_MAGIC|BDB_SSTACK,
111 bdb_cf_gen, "( OLcfgDbAt:1.9 NAME 'olcDbSearchStack' "
112 "DESC 'Depth of search stack in IDLs' "
113 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
114 { "shm_key", "key", 2, 2, 0, ARG_INT|ARG_OFFSET,
115 (void *)offsetof(struct bdb_info, bi_shm_key),
116 "( OLcfgDbAt:1.10 NAME 'olcDbShmKey' "
117 "DESC 'Key for shared memory region' "
118 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
119 { NULL, NULL, 0, 0, 0, ARG_IGNORED,
120 NULL, NULL, NULL, NULL }
123 static ConfigOCs bdbocs[] = {
125 "NAME 'olcBdbConfig' "
126 "DESC 'BDB backend configuration' "
127 "SUP olcDatabaseConfig "
128 "MUST olcDbDirectory "
129 "MAY ( olcDbCacheSize $ olcDbCheckpoint $ olcDbConfig $ "
130 "olcDbNoSync $ olcDbDirtyRead $ olcDbIDLcacheSize $ "
131 "olcDbIndex $ olcDbLinearIndex $ olcDbLockDetect $ "
132 "olcDbMode $ olcDbSearchStack $ olcDbShmKey ) )",
133 Cft_Database, &bdb_oc, bdbcfg },
138 bdb_cf_oc(ConfigArgs *c)
140 if ( c->op == SLAP_CONFIG_EMIT ) {
141 value_add_one( &c->rvalue_vals, &bdb_oc->soc_cname );
147 static slap_verbmasks bdb_lockd[] = {
148 { BER_BVC("default"), DB_LOCK_DEFAULT },
149 { BER_BVC("oldest"), DB_LOCK_OLDEST },
150 { BER_BVC("random"), DB_LOCK_RANDOM },
151 { BER_BVC("youngest"), DB_LOCK_YOUNGEST },
152 { BER_BVC("fewest"), DB_LOCK_MINLOCKS },
156 /* perform periodic checkpoints */
158 bdb_checkpoint( void *ctx, void *arg )
160 struct re_s *rtask = arg;
161 struct bdb_info *bdb = rtask->arg;
163 TXN_CHECKPOINT( bdb->bi_dbenv, bdb->bi_txn_cp_kbyte,
164 bdb->bi_txn_cp_min, 0 );
165 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
166 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
167 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
171 /* reindex entries on the fly */
173 bdb_online_index( void *ctx, void *arg )
175 struct re_s *rtask = arg;
176 BackendDB *be = rtask->arg;
177 struct bdb_info *bdb = be->be_private;
179 Connection conn = {0};
180 char opbuf[OPERATION_BUFFER_SIZE];
181 Operation *op = (Operation *)opbuf;
192 connection_fake_init( &conn, op, ctx );
201 key.size = key.ulen = sizeof(ID);
202 key.flags = DB_DBT_USERMEM;
204 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
205 data.dlen = data.ulen = 0;
208 if ( slapd_shutdown )
211 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &txn, bdb->bi_db_opflags );
214 locker = TXN_ID( txn );
217 BDB_ID2DISK( id, &nid );
218 rc = bdb->bi_id2entry->bdi_db->cursor(
219 bdb->bi_id2entry->bdi_db, txn, &curs, bdb->bi_db_opflags );
224 rc = curs->c_get( curs, &key, &data, DB_SET_RANGE );
225 curs->c_close( curs );
228 if ( rc == DB_NOTFOUND )
230 if ( rc == DB_LOCK_DEADLOCK ) {
231 ldap_pvt_thread_yield();
236 BDB_DISK2ID( &nid, &id );
240 rc = bdb_cache_find_id( op, txn, id, &ei, 0, locker, &lock );
243 if ( rc == DB_LOCK_DEADLOCK ) {
244 ldap_pvt_thread_yield();
247 if ( rc == DB_NOTFOUND ) {
255 rc = bdb_index_entry( op, txn, BDB_INDEX_UPDATE_OP, ei->bei_e );
256 if ( rc == DB_LOCK_DEADLOCK ) {
258 ldap_pvt_thread_yield();
262 rc = TXN_COMMIT( txn, 0 );
272 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
273 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
274 bdb->bi_index_task = NULL;
275 ldap_pvt_runqueue_remove( &slapd_rq, rtask );
276 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
281 /* Cleanup loose ends after Modify completes */
283 bdb_cf_cleanup( ConfigArgs *c )
285 struct bdb_info *bdb = c->be->be_private;
288 if ( bdb->bi_flags & BDB_UPD_CONFIG ) {
289 if ( bdb->bi_db_config ) {
291 FILE *f = fopen( bdb->bi_db_config_path, "w" );
293 for (i=0; bdb->bi_db_config[i].bv_val; i++)
294 fprintf( f, "%s\n", bdb->bi_db_config[i].bv_val );
298 unlink( bdb->bi_db_config_path );
300 bdb->bi_flags ^= BDB_UPD_CONFIG;
303 if ( bdb->bi_flags & BDB_DEL_INDEX ) {
304 bdb_attr_flush( bdb );
305 bdb->bi_flags ^= BDB_DEL_INDEX;
308 if ( bdb->bi_flags & BDB_RE_OPEN ) {
309 bdb->bi_flags ^= BDB_RE_OPEN;
310 rc = c->be->bd_info->bi_db_close( c->be );
312 rc = c->be->bd_info->bi_db_open( c->be );
313 /* If this fails, we need to restart */
316 Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(bdb_cf_cleanup)
317 ": failed to reopen database, rc=%d", rc, 0, 0 );
324 bdb_cf_gen(ConfigArgs *c)
326 struct bdb_info *bdb = c->be->be_private;
329 if ( c->op == SLAP_CONFIG_EMIT ) {
333 if (bdb->bi_txn_cp ) {
336 bv.bv_len = sprintf( buf, "%d %d", bdb->bi_txn_cp_kbyte,
337 bdb->bi_txn_cp_min );
339 value_add_one( &c->rvalue_vals, &bv );
346 if ( bdb->bi_dbenv_home ) {
347 c->value_string = ch_strdup( bdb->bi_dbenv_home );
354 if (( slapMode&SLAP_SERVER_MODE ) && !( bdb->bi_flags&BDB_IS_OPEN )
355 && !bdb->bi_db_config ) {
356 char buf[SLAP_TEXT_BUFLEN];
357 FILE *f = fopen( bdb->bi_db_config_path, "r" );
361 bdb->bi_flags |= BDB_HAS_CONFIG;
362 while ( fgets( buf, sizeof(buf), f )) {
363 ber_str2bv( buf, 0, 1, &bv );
364 if ( bv.bv_val[bv.bv_len-1] == '\n' ) {
366 bv.bv_val[bv.bv_len] = '\0';
368 /* shouldn't need this, but ... */
369 if ( bv.bv_val[bv.bv_len-1] == '\r' ) {
371 bv.bv_val[bv.bv_len] = '\0';
373 ber_bvarray_add( &bdb->bi_db_config, &bv );
378 if ( bdb->bi_db_config ) {
383 for (i=0; !BER_BVISNULL(&bdb->bi_db_config[i]); i++) {
384 bv.bv_len = sprintf( bv.bv_val, "{%d}%s", i,
385 bdb->bi_db_config[i].bv_val );
386 value_add_one( &c->rvalue_vals, &bv );
389 if ( !c->rvalue_vals ) rc = 1;
393 if ( bdb->bi_dbenv_xflags & DB_TXN_NOSYNC )
398 bdb_attr_index_unparse( bdb, &c->rvalue_vals );
399 if ( !c->rvalue_vals ) rc = 1;
404 if ( bdb->bi_lock_detect != DB_LOCK_DEFAULT ) {
406 for (i=0; !BER_BVISNULL(&bdb_lockd[i].word); i++) {
407 if ( bdb->bi_lock_detect == bdb_lockd[i].mask ) {
408 value_add_one( &c->rvalue_vals, &bdb_lockd[i].word );
417 c->value_int = bdb->bi_search_stack_depth;
421 } else if ( c->op == LDAP_MOD_DELETE ) {
424 /* single-valued no-ops */
430 if ( bdb->bi_txn_cp_task ) {
431 struct re_s *re = bdb->bi_txn_cp_task;
432 bdb->bi_txn_cp_task = NULL;
433 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ))
434 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
435 ldap_pvt_runqueue_remove( &slapd_rq, re );
441 ber_bvarray_free( bdb->bi_db_config );
442 bdb->bi_db_config = NULL;
445 ch_free( bdb->bi_db_config[i].bv_val );
446 for (; bdb->bi_db_config[i].bv_val; i++)
447 bdb->bi_db_config[i] = bdb->bi_db_config[i+1];
449 bdb->bi_flags |= BDB_UPD_CONFIG;
450 c->cleanup = bdb_cf_cleanup;
453 bdb->bi_flags |= BDB_RE_OPEN;
454 bdb->bi_flags ^= BDB_HAS_CONFIG;
455 ch_free( bdb->bi_dbenv_home );
456 bdb->bi_dbenv_home = NULL;
457 ch_free( bdb->bi_db_config_path );
458 bdb->bi_db_config_path = NULL;
459 c->cleanup = bdb_cf_cleanup;
460 ldap_pvt_thread_pool_purgekey( bdb->bi_dbenv );
461 ldap_pvt_thread_pool_purgekey( ((char *)bdb->bi_dbenv) + 1 );
464 bdb->bi_dbenv->set_flags( bdb->bi_dbenv, DB_TXN_NOSYNC, 0 );
467 AttributeDescription *ad = NULL;
468 struct berval bv, def = BER_BVC("default");
471 for (ptr = c->line; !isspace( *ptr ); ptr++);
473 bv.bv_len = ptr - bv.bv_val;
474 if ( bvmatch( &bv, &def )) {
475 bdb->bi_defaultmask = 0;
477 slap_bv2ad( &bv, &ad, &text );
479 AttrInfo *ai = bdb_attr_mask( bdb, ad );
480 ai->ai_indexmask |= BDB_INDEX_DELETING;
481 bdb->bi_flags |= BDB_DEL_INDEX;
482 c->cleanup = bdb_cf_cleanup;
494 bdb->bi_txn_cp_kbyte = strtol( c->argv[1], NULL, 0 );
495 bdb->bi_txn_cp_min = strtol( c->argv[2], NULL, 0 );
496 /* If we're in server mode and time-based checkpointing is enabled,
497 * submit a task to perform periodic checkpoints.
499 if ((slapMode & SLAP_SERVER_MODE) && bdb->bi_txn_cp_min ) {
500 struct re_s *re = bdb->bi_txn_cp_task;
502 re->interval.tv_sec = bdb->bi_txn_cp_min * 60;
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 );
511 char *ptr = c->line + STRLENOF("dbconfig");
513 while (!isspace(*ptr)) ptr++;
514 while (isspace(*ptr)) ptr++;
516 if ( bdb->bi_flags & BDB_IS_OPEN ) {
517 bdb->bi_flags |= BDB_UPD_CONFIG;
518 c->cleanup = bdb_cf_cleanup;
520 /* If we're just starting up...
523 /* If a DB_CONFIG file exists, or we don't know the path
524 * to the DB_CONFIG file, ignore these directives
526 if (( bdb->bi_flags & BDB_HAS_CONFIG ) || !bdb->bi_db_config_path )
528 f = fopen( bdb->bi_db_config_path, "a" );
530 /* FIXME: EBCDIC probably needs special handling */
531 fprintf( f, "%s\n", ptr );
535 ber_str2bv( ptr, 0, 1, &bv );
536 ber_bvarray_add( &bdb->bi_db_config, &bv );
540 case BDB_DIRECTORY: {
544 if ( bdb->bi_dbenv_home )
545 ch_free( bdb->bi_dbenv_home );
546 bdb->bi_dbenv_home = c->value_string;
548 /* See if a DB_CONFIG file already exists here */
549 if ( bdb->bi_db_config_path )
550 ch_free( bdb->bi_db_config_path );
551 bdb->bi_db_config_path = ch_malloc( strlen( bdb->bi_dbenv_home ) +
552 STRLENOF(LDAP_DIRSEP) + STRLENOF("DB_CONFIG") + 1 );
553 ptr = lutil_strcopy( bdb->bi_db_config_path, bdb->bi_dbenv_home );
554 *ptr++ = LDAP_DIRSEP[0];
555 strcpy( ptr, "DB_CONFIG" );
557 f = fopen( bdb->bi_db_config_path, "r" );
559 bdb->bi_flags |= BDB_HAS_CONFIG;
567 bdb->bi_dbenv_xflags |= DB_TXN_NOSYNC;
569 bdb->bi_dbenv_xflags &= ~DB_TXN_NOSYNC;
570 if ( bdb->bi_flags & BDB_IS_OPEN ) {
571 bdb->bi_dbenv->set_flags( bdb->bi_dbenv, DB_TXN_NOSYNC,
577 rc = bdb_attr_index_config( bdb, c->fname, c->lineno,
578 c->argc - 1, &c->argv[1] );
580 if( rc != LDAP_SUCCESS ) return 1;
581 if (( bdb->bi_flags & BDB_IS_OPEN ) && !bdb->bi_index_task ) {
582 /* Start the task as soon as we finish here. Set a long
583 * interval (10 hours) so that it only gets scheduled once.
585 bdb->bi_index_task = ldap_pvt_runqueue_insert( &slapd_rq, 36000,
586 bdb_online_index, c->be,
587 LDAP_XSTRING(bdb_online_index), c->be->be_suffix[0].bv_val );
592 rc = verb_to_mask( c->argv[1], bdb_lockd );
593 if ( BER_BVISNULL(&bdb_lockd[rc].word) ) {
594 fprintf( stderr, "%s: "
595 "bad policy (%s) in \"lockDetect <policy>\" line\n",
596 c->log, c->argv[1] );
599 bdb->bi_lock_detect = rc;
603 if ( c->value_int < MINIMUM_SEARCH_STACK_DEPTH ) {
605 "%s: depth %d too small, using %d\n",
606 c->log, c->value_int, MINIMUM_SEARCH_STACK_DEPTH );
607 c->value_int = MINIMUM_SEARCH_STACK_DEPTH;
609 bdb->bi_search_stack_depth = c->value_int;
615 int bdb_back_init_cf( BackendInfo *bi )
618 bi->bi_cf_ocs = bdbocs;
620 rc = config_register_schema( bdbcfg, bdbocs );