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 static ObjectClass *bdb_oc;
35 static ConfigDriver bdb_cf_oc, bdb_cf_gen;
48 static ConfigTable bdbcfg[] = {
49 { "", "", 0, 0, 0, ARG_MAGIC,
50 bdb_cf_oc, NULL, NULL, NULL },
51 { "directory", "dir", 2, 2, 0, ARG_STRING|ARG_MAGIC|BDB_DIRECTORY,
52 bdb_cf_gen, "( OLcfgDbAt:0.1 NAME 'olcDbDirectory' "
53 "DESC 'Directory for database content' "
54 "EQUALITY caseIgnoreMatch "
55 "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
56 { "cachesize", "size", 2, 2, 0, ARG_INT|ARG_OFFSET,
57 (void *)offsetof(struct bdb_info, bi_cache.c_maxsize),
58 "( OLcfgDbAt:1.1 NAME 'olcDbCacheSize' "
59 "DESC 'Entry cache size in entries' "
60 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
61 { "checkpoint", "kbyte> <min", 3, 3, 0, ARG_MAGIC|BDB_CHKPT,
62 bdb_cf_gen, "( OLcfgDbAt:1.2 NAME 'olcDbCheckpoint' "
63 "DESC 'Database checkpoint interval in kbytes and minutes' "
64 "SYNTAX OMsDirectoryString SINGLE-VALUE )",NULL, NULL },
65 { "dbconfig", "DB_CONFIG setting", 3, 0, 0, ARG_MAGIC|BDB_CONFIG,
66 bdb_cf_gen, "( OLcfgDbAt:1.3 NAME 'olcDbConfig' "
67 "DESC 'BerkeleyDB DB_CONFIG configuration directives' "
68 "SYNTAX OMsDirectoryString )",NULL, NULL },
69 { "dbnosync", NULL, 1, 2, 0, ARG_ON_OFF|ARG_MAGIC|BDB_NOSYNC,
70 bdb_cf_gen, "( OLcfgDbAt:1.4 NAME 'olcDbNoSync' "
71 "DESC 'Disable synchronous database writes' "
72 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
73 { "dirtyread", NULL, 1, 2, 0,
74 #ifdef SLAP_BDB_ALLOW_DIRTY_READ
75 ARG_ON_OFF|ARG_MAGIC|BDB_DIRTYR, bdb_cf_gen,
79 "( OLcfgDbAt:1.5 NAME 'olcDbDirtyRead' "
80 "DESC 'Allow reads of uncommitted data' "
81 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
82 { "idlcachesize", "size", 2, 2, 0, ARG_INT|ARG_OFFSET,
83 (void *)offsetof(struct bdb_info,bi_idl_cache_max_size),
84 "( OLcfgDbAt:1.6 NAME 'olcDbIDLcacheSize' "
85 "DESC 'IDL cache size in IDLs' "
86 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
87 { "index", "attr> <[pres,eq,approx,sub]", 2, 3, 0, ARG_MAGIC|BDB_INDEX,
88 bdb_cf_gen, "( OLcfgDbAt:0.2 NAME 'olcDbIndex' "
89 "DESC 'Attribute index parameters' "
90 "EQUALITY caseIgnoreMatch "
91 "SYNTAX OMsDirectoryString )", NULL, NULL },
92 { "linearindex", NULL, 1, 2, 0, ARG_ON_OFF|ARG_OFFSET,
93 (void *)offsetof(struct bdb_info, bi_linear_index),
94 "( OLcfgDbAt:1.7 NAME 'olcDbLinearIndex' "
95 "DESC 'Index attributes one at a time' "
96 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
97 { "lockdetect", "policy", 2, 2, 0, ARG_MAGIC|BDB_LOCKD,
98 bdb_cf_gen, "( OLcfgDbAt:1.8 NAME 'olcDbLockDetect' "
99 "DESC 'Deadlock detection algorithm' "
100 "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
101 { "mode", "mode", 2, 2, 0, ARG_INT|ARG_OFFSET,
102 (void *)offsetof(struct bdb_info, bi_dbenv_mode),
103 "( OLcfgDbAt:0.3 NAME 'olcDbMode' "
104 "DESC 'Unix permissions of database files' "
105 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
106 { "searchstack", "depth", 2, 2, 0, ARG_INT|ARG_MAGIC|BDB_SSTACK,
107 bdb_cf_gen, "( OLcfgDbAt:1.9 NAME 'olcDbSearchStack' "
108 "DESC 'Depth of search stack in IDLs' "
109 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
110 { "shm_key", "key", 2, 2, 0, ARG_INT|ARG_OFFSET,
111 (void *)offsetof(struct bdb_info, bi_shm_key),
112 "( OLcfgDbAt:1.10 NAME 'olcDbShmKey' "
113 "DESC 'Key for shared memory region' "
114 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
115 { NULL, NULL, 0, 0, 0, ARG_IGNORED,
116 NULL, NULL, NULL, NULL }
119 static ConfigOCs bdbocs[] = {
121 "NAME 'olcBdbConfig' "
122 "DESC 'BDB backend configuration' "
123 "SUP olcDatabaseConfig "
124 "MUST olcDbDirectory "
125 "MAY ( olcDbCacheSize $ olcDbCheckpoint $ olcDbConfig $ "
126 "olcDbNoSync $ olcDbDirtyRead $ olcDbIDLcacheSize $ "
127 "olcDbIndex $ olcDbLinearIndex $ olcDbLockDetect $ "
128 "olcDbMode $ olcDbSearchStack $ olcDbShmKey ) )",
129 Cft_Database, &bdb_oc },
134 bdb_cf_oc(ConfigArgs *c)
136 if ( c->op == SLAP_CONFIG_EMIT ) {
137 value_add_one( &c->rvalue_vals, &bdb_oc->soc_cname );
143 static slap_verbmasks bdb_lockd[] = {
144 { BER_BVC("default"), DB_LOCK_DEFAULT },
145 { BER_BVC("oldest"), DB_LOCK_OLDEST },
146 { BER_BVC("random"), DB_LOCK_RANDOM },
147 { BER_BVC("youngest"), DB_LOCK_YOUNGEST },
148 { BER_BVC("fewest"), DB_LOCK_MINLOCKS },
152 /* perform periodic checkpoints */
154 bdb_checkpoint( void *ctx, void *arg )
156 struct re_s *rtask = arg;
157 struct bdb_info *bdb = rtask->arg;
159 TXN_CHECKPOINT( bdb->bi_dbenv, bdb->bi_txn_cp_kbyte,
160 bdb->bi_txn_cp_min, 0 );
161 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
162 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
163 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
167 /* reindex entries on the fly */
169 bdb_online_index( void *ctx, void *arg )
171 struct re_s *rtask = arg;
172 BackendDB *be = rtask->arg;
173 struct bdb_info *bdb = be->be_private;
175 Connection conn = {0};
176 char opbuf[OPERATION_BUFFER_SIZE];
177 Operation *op = (Operation *)opbuf;
188 connection_fake_init( &conn, op, ctx );
197 key.size = key.ulen = sizeof(ID);
198 key.flags = DB_DBT_USERMEM;
200 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
201 data.dlen = data.ulen = 0;
204 if ( slapd_shutdown )
207 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &txn, bdb->bi_db_opflags );
210 locker = TXN_ID( txn );
213 BDB_ID2DISK( id, &nid );
214 rc = bdb->bi_id2entry->bdi_db->cursor(
215 bdb->bi_id2entry->bdi_db, txn, &curs, bdb->bi_db_opflags );
220 rc = curs->c_get( curs, &key, &data, DB_SET_RANGE );
221 curs->c_close( curs );
224 if ( rc == DB_NOTFOUND )
226 if ( rc == DB_LOCK_DEADLOCK ) {
227 ldap_pvt_thread_yield();
232 BDB_DISK2ID( &nid, &id );
236 rc = bdb_cache_find_id( op, txn, id, &ei, 0, locker, &lock );
239 if ( rc == DB_LOCK_DEADLOCK ) {
240 ldap_pvt_thread_yield();
243 if ( rc == DB_NOTFOUND ) {
251 rc = bdb_index_entry( op, txn, BDB_INDEX_UPDATE_OP, ei->bei_e );
252 if ( rc == DB_LOCK_DEADLOCK ) {
254 ldap_pvt_thread_yield();
258 rc = TXN_COMMIT( txn, 0 );
268 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
269 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
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 */
316 bdb_cf_gen(ConfigArgs *c)
318 struct bdb_info *bdb = c->be->be_private;
321 if ( c->op == SLAP_CONFIG_EMIT ) {
325 if (bdb->bi_txn_cp ) {
328 bv.bv_len = sprintf( buf, "%d %d", bdb->bi_txn_cp_kbyte,
329 bdb->bi_txn_cp_min );
331 value_add_one( &c->rvalue_vals, &bv );
338 if ( bdb->bi_dbenv_home ) {
339 c->value_string = ch_strdup( bdb->bi_dbenv_home );
346 if ( bdb->bi_db_config ) {
351 for (i=0; !BER_BVISNULL(&bdb->bi_db_config[i]); i++) {
352 bv.bv_len = sprintf( bv.bv_val, "{%d}%s", i,
353 bdb->bi_db_config[i].bv_val );
354 value_add_one( &c->rvalue_vals, &bv );
357 if ( !c->rvalue_vals ) rc = 1;
361 if ( bdb->bi_dbenv_xflags & DB_TXN_NOSYNC )
366 bdb_attr_index_unparse( bdb, &c->rvalue_vals );
367 if ( !c->rvalue_vals ) rc = 1;
372 if ( bdb->bi_lock_detect != DB_LOCK_DEFAULT ) {
374 for (i=0; !BER_BVISNULL(&bdb_lockd[i].word); i++) {
375 if ( bdb->bi_lock_detect == bdb_lockd[i].mask ) {
376 value_add_one( &c->rvalue_vals, &bdb_lockd[i].word );
385 c->value_int = bdb->bi_search_stack_depth;
389 } else if ( c->op == LDAP_MOD_DELETE ) {
392 /* single-valued no-ops */
398 if ( bdb->bi_txn_cp_task ) {
399 struct re_s *re = bdb->bi_txn_cp_task;
400 bdb->bi_txn_cp_task = NULL;
401 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ))
402 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
403 ldap_pvt_runqueue_remove( &slapd_rq, re );
409 ber_bvarray_free( bdb->bi_db_config );
410 bdb->bi_db_config = NULL;
413 ch_free( bdb->bi_db_config[i].bv_val );
414 for (; bdb->bi_db_config[i].bv_val; i++)
415 bdb->bi_db_config[i] = bdb->bi_db_config[i+1];
417 bdb->bi_flags |= BDB_UPD_CONFIG;
418 c->cleanup = bdb_cf_cleanup;
421 bdb->bi_flags |= BDB_RE_OPEN;
422 bdb->bi_flags ^= BDB_HAS_CONFIG;
423 ch_free( bdb->bi_dbenv_home );
424 bdb->bi_dbenv_home = NULL;
425 ch_free( bdb->bi_db_config_path );
426 bdb->bi_db_config_path = NULL;
427 c->cleanup = bdb_cf_cleanup;
428 ldap_pvt_thread_pool_purgekey( bdb->bi_dbenv );
429 ldap_pvt_thread_pool_purgekey( ((char *)bdb->bi_dbenv) + 1 );
432 bdb->bi_dbenv->set_flags( bdb->bi_dbenv, DB_TXN_NOSYNC, 0 );
435 AttributeDescription *ad = NULL;
436 struct berval bv, def = BER_BVC("default");
439 for (ptr = c->line; !isspace( *ptr ); ptr++);
441 bv.bv_len = ptr - bv.bv_val;
442 if ( bvmatch( &bv, &def )) {
443 bdb->bi_defaultmask = 0;
445 slap_bv2ad( &bv, &ad, &text );
447 AttrInfo *ai = bdb_attr_mask( bdb, ad );
448 ai->ai_indexmask |= BDB_INDEX_DELETING;
449 bdb->bi_flags |= BDB_DEL_INDEX;
450 c->cleanup = bdb_cf_cleanup;
462 bdb->bi_txn_cp_kbyte = strtol( c->argv[1], NULL, 0 );
463 bdb->bi_txn_cp_min = strtol( c->argv[2], NULL, 0 );
464 /* If we're in server mode and time-based checkpointing is enabled,
465 * submit a task to perform periodic checkpoints.
467 if ((slapMode & SLAP_SERVER_MODE) && bdb->bi_txn_cp_min ) {
468 struct re_s *re = bdb->bi_txn_cp_task;
470 re->interval.tv_sec = bdb->bi_txn_cp_min * 60;
472 bdb->bi_txn_cp_task = ldap_pvt_runqueue_insert( &slapd_rq,
473 bdb->bi_txn_cp_min * 60, bdb_checkpoint, bdb );
478 char *ptr = c->line + STRLENOF("dbconfig");
480 while (!isspace(*ptr)) ptr++;
481 while (isspace(*ptr)) ptr++;
483 if ( bdb->bi_flags & BDB_IS_OPEN ) {
484 bdb->bi_flags |= BDB_UPD_CONFIG;
485 c->cleanup = bdb_cf_cleanup;
487 /* If we're just starting up...
490 /* If a DB_CONFIG file exists, or we don't know the path
491 * to the DB_CONFIG file, ignore these directives
493 if (( bdb->bi_flags & BDB_HAS_CONFIG ) || !bdb->bi_db_config_path )
495 f = fopen( bdb->bi_db_config_path, "a" );
497 /* FIXME: EBCDIC probably needs special handling */
498 fprintf( f, "%s\n", ptr );
502 ber_str2bv( ptr, 0, 1, &bv );
503 ber_bvarray_add( &bdb->bi_db_config, &bv );
507 case BDB_DIRECTORY: {
511 if ( bdb->bi_dbenv_home )
512 ch_free( bdb->bi_dbenv_home );
513 bdb->bi_dbenv_home = c->value_string;
515 /* See if a DB_CONFIG file already exists here */
516 if ( bdb->bi_db_config_path )
517 ch_free( bdb->bi_db_config_path );
518 bdb->bi_db_config_path = ch_malloc( strlen( bdb->bi_dbenv_home ) +
519 STRLENOF(LDAP_DIRSEP) + STRLENOF("DB_CONFIG") + 1 );
520 ptr = lutil_strcopy( bdb->bi_db_config_path, bdb->bi_dbenv_home );
521 *ptr++ = LDAP_DIRSEP[0];
522 strcpy( ptr, "DB_CONFIG" );
524 f = fopen( bdb->bi_db_config_path, "r" );
526 bdb->bi_flags |= BDB_HAS_CONFIG;
534 bdb->bi_dbenv_xflags |= DB_TXN_NOSYNC;
536 bdb->bi_dbenv_xflags &= ~DB_TXN_NOSYNC;
537 if ( bdb->bi_flags & BDB_IS_OPEN ) {
538 bdb->bi_dbenv->set_flags( bdb->bi_dbenv, DB_TXN_NOSYNC,
544 rc = bdb_attr_index_config( bdb, c->fname, c->lineno,
545 c->argc - 1, &c->argv[1] );
547 if( rc != LDAP_SUCCESS ) return 1;
548 if ( bdb->bi_flags & BDB_IS_OPEN ) {
549 /* Start the task as soon as we finish here */
550 ldap_pvt_runqueue_insert( &slapd_rq, 60,
551 bdb_online_index, c->be );
556 rc = verb_to_mask( c->argv[1], bdb_lockd );
557 if ( BER_BVISNULL(&bdb_lockd[rc].word) ) {
558 fprintf( stderr, "%s: "
559 "bad policy (%s) in \"lockDetect <policy>\" line\n",
560 c->log, c->argv[1] );
563 bdb->bi_lock_detect = rc;
567 if ( c->value_int < MINIMUM_SEARCH_STACK_DEPTH ) {
569 "%s: depth %d too small, using %d\n",
570 c->log, c->value_int, MINIMUM_SEARCH_STACK_DEPTH );
571 c->value_int = MINIMUM_SEARCH_STACK_DEPTH;
573 bdb->bi_search_stack_depth = c->value_int;
579 int bdb_back_init_cf( BackendInfo *bi )
582 bi->bi_cf_table = bdbcfg;
584 rc = config_register_schema( bdbcfg, bdbocs );
586 bdbcfg[0].ad = slap_schema.si_ad_objectClass;