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 /* reindex entries on the fly */
154 bdb_online_index( void *ctx, void *arg )
156 struct re_s *rtask = arg;
157 BackendDB *be = rtask->arg;
158 struct bdb_info *bdb = be->be_private;
160 Connection conn = {0};
161 char opbuf[OPERATION_BUFFER_SIZE];
162 Operation *op = (Operation *)opbuf;
173 connection_fake_init( &conn, op, ctx );
182 key.size = key.ulen = sizeof(ID);
183 key.flags = DB_DBT_USERMEM;
185 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
186 data.dlen = data.ulen = 0;
189 if ( slapd_shutdown )
192 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &txn, bdb->bi_db_opflags );
195 locker = TXN_ID( txn );
198 BDB_ID2DISK( id, &nid );
199 rc = bdb->bi_id2entry->bdi_db->cursor(
200 bdb->bi_id2entry->bdi_db, txn, &curs, bdb->bi_db_opflags );
205 rc = curs->c_get( curs, &key, &data, DB_SET_RANGE );
206 curs->c_close( curs );
209 if ( rc == DB_NOTFOUND )
211 if ( rc == DB_LOCK_DEADLOCK ) {
212 ldap_pvt_thread_yield();
217 BDB_DISK2ID( &nid, &id );
221 rc = bdb_cache_find_id( op, txn, id, &ei, 0, locker, &lock );
224 if ( rc == DB_LOCK_DEADLOCK ) {
225 ldap_pvt_thread_yield();
228 if ( rc == DB_NOTFOUND ) {
236 rc = bdb_index_entry( op, txn, BDB_INDEX_UPDATE_OP, ei->bei_e );
237 if ( rc == DB_LOCK_DEADLOCK ) {
239 ldap_pvt_thread_yield();
243 rc = TXN_COMMIT( txn, 0 );
253 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
254 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
255 ldap_pvt_runqueue_remove( &slapd_rq, rtask );
256 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
261 /* Cleanup loose ends after Modify completes */
263 bdb_cf_cleanup( ConfigArgs *c )
265 struct bdb_info *bdb = c->be->be_private;
267 if ( bdb->bi_flags & BDB_UPD_CONFIG ) {
268 if ( bdb->bi_db_config ) {
270 FILE *f = fopen( bdb->bi_db_config_path, "w" );
272 for (i=0; bdb->bi_db_config[i].bv_val; i++)
273 fprintf( f, "%s\n", bdb->bi_db_config[i].bv_val );
277 unlink( bdb->bi_db_config_path );
279 bdb->bi_flags ^= BDB_UPD_CONFIG;
282 if ( bdb->bi_flags & BDB_DEL_INDEX ) {
283 bdb_attr_flush( bdb );
284 bdb->bi_flags ^= BDB_DEL_INDEX;
291 bdb_cf_gen(ConfigArgs *c)
293 struct bdb_info *bdb = c->be->be_private;
296 if ( c->op == SLAP_CONFIG_EMIT ) {
300 if (bdb->bi_txn_cp ) {
303 bv.bv_len = sprintf( buf, "%d %d", bdb->bi_txn_cp_kbyte,
304 bdb->bi_txn_cp_min );
306 value_add_one( &c->rvalue_vals, &bv );
313 if ( bdb->bi_dbenv_home ) {
314 c->value_string = ch_strdup( bdb->bi_dbenv_home );
321 if ( bdb->bi_db_config ) {
326 for (i=0; !BER_BVISNULL(&bdb->bi_db_config[i]); i++) {
327 bv.bv_len = sprintf( bv.bv_val, "{%d}%s", i,
328 bdb->bi_db_config[i].bv_val );
329 value_add_one( &c->rvalue_vals, &bv );
332 if ( !c->rvalue_vals ) rc = 1;
336 if ( bdb->bi_dbenv_xflags & DB_TXN_NOSYNC )
341 bdb_attr_index_unparse( bdb, &c->rvalue_vals );
342 if ( !c->rvalue_vals ) rc = 1;
347 if ( bdb->bi_lock_detect != DB_LOCK_DEFAULT ) {
349 for (i=0; !BER_BVISNULL(&bdb_lockd[i].word); i++) {
350 if ( bdb->bi_lock_detect == bdb_lockd[i].mask ) {
351 value_add_one( &c->rvalue_vals, &bdb_lockd[i].word );
360 c->value_int = bdb->bi_search_stack_depth;
364 } else if ( c->op == LDAP_MOD_DELETE ) {
367 /* single-valued no-ops */
373 /* FIXME: should stop the checkpoint task too */
378 ber_bvarray_free( bdb->bi_db_config );
379 bdb->bi_db_config = NULL;
382 ch_free( bdb->bi_db_config[i].bv_val );
383 for (; bdb->bi_db_config[i].bv_val; i++)
384 bdb->bi_db_config[i] = bdb->bi_db_config[i+1];
386 bdb->bi_flags |= BDB_UPD_CONFIG;
387 c->cleanup = bdb_cf_cleanup;
391 /* FIXME: what does this mean? */
394 bdb->bi_dbenv->set_flags( bdb->bi_dbenv, DB_TXN_NOSYNC, 0 );
397 AttributeDescription *ad = NULL;
398 struct berval bv, def = BER_BVC("default");
401 for (ptr = c->line; !isspace( *ptr ); ptr++);
403 bv.bv_len = ptr - bv.bv_val;
404 if ( bvmatch( &bv, &def )) {
405 bdb->bi_defaultmask = 0;
407 slap_bv2ad( &bv, &ad, &text );
409 AttrInfo *ai = bdb_attr_mask( bdb, ad );
410 ai->ai_indexmask |= BDB_INDEX_DELETING;
411 bdb->bi_flags |= BDB_DEL_INDEX;
412 c->cleanup = bdb_cf_cleanup;
424 bdb->bi_txn_cp_kbyte = strtol( c->argv[1], NULL, 0 );
425 bdb->bi_txn_cp_min = strtol( c->argv[2], NULL, 0 );
429 char *ptr = c->line + STRLENOF("dbconfig");
431 while (!isspace(*ptr)) ptr++;
432 while (isspace(*ptr)) ptr++;
434 if ( bdb->bi_flags & BDB_IS_OPEN ) {
435 bdb->bi_flags |= BDB_UPD_CONFIG;
436 c->cleanup = bdb_cf_cleanup;
438 /* If we're just starting up...
441 /* If a DB_CONFIG file exists, or we don't know the path
442 * to the DB_CONFIG file, ignore these directives
444 if (( bdb->bi_flags & BDB_HAS_CONFIG ) || !bdb->bi_db_config_path )
446 f = fopen( bdb->bi_db_config_path, "a" );
448 /* FIXME: EBCDIC probably needs special handling */
449 fprintf( f, "%s\n", ptr );
453 ber_str2bv( ptr, 0, 1, &bv );
454 ber_bvarray_add( &bdb->bi_db_config, &bv );
458 case BDB_DIRECTORY: {
462 bdb->bi_dbenv_home = c->value_string;
464 /* See if a DB_CONFIG file already exists here */
465 bdb->bi_db_config_path = ch_malloc( strlen( bdb->bi_dbenv_home ) +
466 STRLENOF(LDAP_DIRSEP) + STRLENOF("DB_CONFIG") + 1 );
467 ptr = lutil_strcopy( bdb->bi_db_config_path, bdb->bi_dbenv_home );
468 *ptr++ = LDAP_DIRSEP[0];
469 strcpy( ptr, "DB_CONFIG" );
471 f = fopen( bdb->bi_db_config_path, "r" );
473 bdb->bi_flags |= BDB_HAS_CONFIG;
481 bdb->bi_dbenv_xflags |= DB_TXN_NOSYNC;
483 bdb->bi_dbenv_xflags &= ~DB_TXN_NOSYNC;
484 if ( bdb->bi_flags & BDB_IS_OPEN ) {
485 bdb->bi_dbenv->set_flags( bdb->bi_dbenv, DB_TXN_NOSYNC,
491 rc = bdb_attr_index_config( bdb, c->fname, c->lineno,
492 c->argc - 1, &c->argv[1] );
494 if( rc != LDAP_SUCCESS ) return 1;
495 if ( bdb->bi_flags & BDB_IS_OPEN ) {
496 /* Start the task as soon as we finish here */
497 ldap_pvt_runqueue_insert( &slapd_rq, 60,
498 bdb_online_index, c->be );
503 rc = verb_to_mask( c->argv[1], bdb_lockd );
504 if ( BER_BVISNULL(&bdb_lockd[rc].word) ) {
505 fprintf( stderr, "%s: "
506 "bad policy (%s) in \"lockDetect <policy>\" line\n",
507 c->log, c->argv[1] );
510 bdb->bi_lock_detect = rc;
514 if ( c->value_int < MINIMUM_SEARCH_STACK_DEPTH ) {
516 "%s: depth %d too small, using %d\n",
517 c->log, c->value_int, MINIMUM_SEARCH_STACK_DEPTH );
518 c->value_int = MINIMUM_SEARCH_STACK_DEPTH;
520 bdb->bi_search_stack_depth = c->value_int;
526 int bdb_back_init_cf( BackendInfo *bi )
529 bi->bi_cf_table = bdbcfg;
531 rc = config_register_schema( bdbcfg, bdbocs );
533 bdbcfg[0].ad = slap_schema.si_ad_objectClass;