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;
282 if ( bdb->bi_flags & BDB_UPD_CONFIG ) {
283 if ( bdb->bi_db_config ) {
285 FILE *f = fopen( bdb->bi_db_config_path, "w" );
287 for (i=0; bdb->bi_db_config[i].bv_val; i++)
288 fprintf( f, "%s\n", bdb->bi_db_config[i].bv_val );
292 unlink( bdb->bi_db_config_path );
294 bdb->bi_flags ^= BDB_UPD_CONFIG;
297 if ( bdb->bi_flags & BDB_DEL_INDEX ) {
298 bdb_attr_flush( bdb );
299 bdb->bi_flags ^= BDB_DEL_INDEX;
306 bdb_cf_gen(ConfigArgs *c)
308 struct bdb_info *bdb = c->be->be_private;
311 if ( c->op == SLAP_CONFIG_EMIT ) {
315 if (bdb->bi_txn_cp ) {
318 bv.bv_len = sprintf( buf, "%d %d", bdb->bi_txn_cp_kbyte,
319 bdb->bi_txn_cp_min );
321 value_add_one( &c->rvalue_vals, &bv );
328 if ( bdb->bi_dbenv_home ) {
329 c->value_string = ch_strdup( bdb->bi_dbenv_home );
336 if ( bdb->bi_db_config ) {
341 for (i=0; !BER_BVISNULL(&bdb->bi_db_config[i]); i++) {
342 bv.bv_len = sprintf( bv.bv_val, "{%d}%s", i,
343 bdb->bi_db_config[i].bv_val );
344 value_add_one( &c->rvalue_vals, &bv );
347 if ( !c->rvalue_vals ) rc = 1;
351 if ( bdb->bi_dbenv_xflags & DB_TXN_NOSYNC )
356 bdb_attr_index_unparse( bdb, &c->rvalue_vals );
357 if ( !c->rvalue_vals ) rc = 1;
362 if ( bdb->bi_lock_detect != DB_LOCK_DEFAULT ) {
364 for (i=0; !BER_BVISNULL(&bdb_lockd[i].word); i++) {
365 if ( bdb->bi_lock_detect == bdb_lockd[i].mask ) {
366 value_add_one( &c->rvalue_vals, &bdb_lockd[i].word );
375 c->value_int = bdb->bi_search_stack_depth;
379 } else if ( c->op == LDAP_MOD_DELETE ) {
382 /* single-valued no-ops */
388 if ( bdb->bi_txn_cp_task ) {
389 struct re_s *re = bdb->bi_txn_cp_task;
390 bdb->bi_txn_cp_task = NULL;
391 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ))
392 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
393 ldap_pvt_runqueue_remove( &slapd_rq, re );
399 ber_bvarray_free( bdb->bi_db_config );
400 bdb->bi_db_config = NULL;
403 ch_free( bdb->bi_db_config[i].bv_val );
404 for (; bdb->bi_db_config[i].bv_val; i++)
405 bdb->bi_db_config[i] = bdb->bi_db_config[i+1];
407 bdb->bi_flags |= BDB_UPD_CONFIG;
408 c->cleanup = bdb_cf_cleanup;
412 /* FIXME: what does this mean? */
415 bdb->bi_dbenv->set_flags( bdb->bi_dbenv, DB_TXN_NOSYNC, 0 );
418 AttributeDescription *ad = NULL;
419 struct berval bv, def = BER_BVC("default");
422 for (ptr = c->line; !isspace( *ptr ); ptr++);
424 bv.bv_len = ptr - bv.bv_val;
425 if ( bvmatch( &bv, &def )) {
426 bdb->bi_defaultmask = 0;
428 slap_bv2ad( &bv, &ad, &text );
430 AttrInfo *ai = bdb_attr_mask( bdb, ad );
431 ai->ai_indexmask |= BDB_INDEX_DELETING;
432 bdb->bi_flags |= BDB_DEL_INDEX;
433 c->cleanup = bdb_cf_cleanup;
445 bdb->bi_txn_cp_kbyte = strtol( c->argv[1], NULL, 0 );
446 bdb->bi_txn_cp_min = strtol( c->argv[2], NULL, 0 );
447 /* If we're in server mode and time-based checkpointing is enabled,
448 * submit a task to perform periodic checkpoints.
450 if ((slapMode & SLAP_SERVER_MODE) && bdb->bi_txn_cp_min ) {
451 struct re_s *re = bdb->bi_txn_cp_task;
453 re->interval.tv_sec = bdb->bi_txn_cp_min * 60;
455 bdb->bi_txn_cp_task = ldap_pvt_runqueue_insert( &slapd_rq,
456 bdb->bi_txn_cp_min * 60, bdb_checkpoint, bdb );
461 char *ptr = c->line + STRLENOF("dbconfig");
463 while (!isspace(*ptr)) ptr++;
464 while (isspace(*ptr)) ptr++;
466 if ( bdb->bi_flags & BDB_IS_OPEN ) {
467 bdb->bi_flags |= BDB_UPD_CONFIG;
468 c->cleanup = bdb_cf_cleanup;
470 /* If we're just starting up...
473 /* If a DB_CONFIG file exists, or we don't know the path
474 * to the DB_CONFIG file, ignore these directives
476 if (( bdb->bi_flags & BDB_HAS_CONFIG ) || !bdb->bi_db_config_path )
478 f = fopen( bdb->bi_db_config_path, "a" );
480 /* FIXME: EBCDIC probably needs special handling */
481 fprintf( f, "%s\n", ptr );
485 ber_str2bv( ptr, 0, 1, &bv );
486 ber_bvarray_add( &bdb->bi_db_config, &bv );
490 case BDB_DIRECTORY: {
494 bdb->bi_dbenv_home = c->value_string;
496 /* See if a DB_CONFIG file already exists here */
497 bdb->bi_db_config_path = ch_malloc( strlen( bdb->bi_dbenv_home ) +
498 STRLENOF(LDAP_DIRSEP) + STRLENOF("DB_CONFIG") + 1 );
499 ptr = lutil_strcopy( bdb->bi_db_config_path, bdb->bi_dbenv_home );
500 *ptr++ = LDAP_DIRSEP[0];
501 strcpy( ptr, "DB_CONFIG" );
503 f = fopen( bdb->bi_db_config_path, "r" );
505 bdb->bi_flags |= BDB_HAS_CONFIG;
513 bdb->bi_dbenv_xflags |= DB_TXN_NOSYNC;
515 bdb->bi_dbenv_xflags &= ~DB_TXN_NOSYNC;
516 if ( bdb->bi_flags & BDB_IS_OPEN ) {
517 bdb->bi_dbenv->set_flags( bdb->bi_dbenv, DB_TXN_NOSYNC,
523 rc = bdb_attr_index_config( bdb, c->fname, c->lineno,
524 c->argc - 1, &c->argv[1] );
526 if( rc != LDAP_SUCCESS ) return 1;
527 if ( bdb->bi_flags & BDB_IS_OPEN ) {
528 /* Start the task as soon as we finish here */
529 ldap_pvt_runqueue_insert( &slapd_rq, 60,
530 bdb_online_index, c->be );
535 rc = verb_to_mask( c->argv[1], bdb_lockd );
536 if ( BER_BVISNULL(&bdb_lockd[rc].word) ) {
537 fprintf( stderr, "%s: "
538 "bad policy (%s) in \"lockDetect <policy>\" line\n",
539 c->log, c->argv[1] );
542 bdb->bi_lock_detect = rc;
546 if ( c->value_int < MINIMUM_SEARCH_STACK_DEPTH ) {
548 "%s: depth %d too small, using %d\n",
549 c->log, c->value_int, MINIMUM_SEARCH_STACK_DEPTH );
550 c->value_int = MINIMUM_SEARCH_STACK_DEPTH;
552 bdb->bi_search_stack_depth = c->value_int;
558 int bdb_back_init_cf( BackendInfo *bi )
561 bi->bi_cf_table = bdbcfg;
563 rc = config_register_schema( bdbcfg, bdbocs );
565 bdbcfg[0].ad = slap_schema.si_ad_objectClass;