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>.
21 #include <ac/string.h>
31 # define SLAP_BDB_ALLOW_DIRTY_READ
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 ConfigDriver bdb_cf_gen;
52 static ConfigTable bdbcfg[] = {
53 { "directory", "dir", 2, 2, 0, ARG_STRING|ARG_MAGIC|BDB_DIRECTORY,
54 bdb_cf_gen, "( OLcfgDbAt:0.1 NAME 'olcDbDirectory' "
55 "DESC 'Directory for database content' "
56 "EQUALITY caseIgnoreMatch "
57 "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
58 { "cachefree", "size", 2, 2, 0, ARG_INT|ARG_OFFSET,
59 (void *)offsetof(struct bdb_info, bi_cache.c_minfree),
60 "( OLcfgDbAt:1.11 NAME 'olcDbCacheFree' "
61 "DESC 'Number of extra entries to free when max is reached' "
62 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
63 { "cachesize", "size", 2, 2, 0, ARG_INT|ARG_OFFSET,
64 (void *)offsetof(struct bdb_info, bi_cache.c_maxsize),
65 "( OLcfgDbAt:1.1 NAME 'olcDbCacheSize' "
66 "DESC 'Entry cache size in entries' "
67 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
68 { "checkpoint", "kbyte> <min", 3, 3, 0, ARG_MAGIC|BDB_CHKPT,
69 bdb_cf_gen, "( OLcfgDbAt:1.2 NAME 'olcDbCheckpoint' "
70 "DESC 'Database checkpoint interval in kbytes and minutes' "
71 "SYNTAX OMsDirectoryString SINGLE-VALUE )",NULL, NULL },
72 { "dbconfig", "DB_CONFIG setting", 1, 0, 0, ARG_MAGIC|BDB_CONFIG,
73 bdb_cf_gen, "( OLcfgDbAt:1.3 NAME 'olcDbConfig' "
74 "DESC 'BerkeleyDB DB_CONFIG configuration directives' "
75 "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
76 { "dbnosync", NULL, 1, 2, 0, ARG_ON_OFF|ARG_MAGIC|BDB_NOSYNC,
77 bdb_cf_gen, "( OLcfgDbAt:1.4 NAME 'olcDbNoSync' "
78 "DESC 'Disable synchronous database writes' "
79 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
80 { "dirtyread", NULL, 1, 2, 0,
81 #ifdef SLAP_BDB_ALLOW_DIRTY_READ
82 ARG_ON_OFF|ARG_MAGIC|BDB_DIRTYR, bdb_cf_gen,
86 "( OLcfgDbAt:1.5 NAME 'olcDbDirtyRead' "
87 "DESC 'Allow reads of uncommitted data' "
88 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
89 { "idlcachesize", "size", 2, 2, 0, ARG_INT|ARG_OFFSET,
90 (void *)offsetof(struct bdb_info,bi_idl_cache_max_size),
91 "( OLcfgDbAt:1.6 NAME 'olcDbIDLcacheSize' "
92 "DESC 'IDL cache size in IDLs' "
93 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
94 { "index", "attr> <[pres,eq,approx,sub]", 2, 3, 0, ARG_MAGIC|BDB_INDEX,
95 bdb_cf_gen, "( OLcfgDbAt:0.2 NAME 'olcDbIndex' "
96 "DESC 'Attribute index parameters' "
97 "EQUALITY caseIgnoreMatch "
98 "SYNTAX OMsDirectoryString )", NULL, NULL },
99 { "linearindex", NULL, 1, 2, 0, ARG_ON_OFF|ARG_OFFSET,
100 (void *)offsetof(struct bdb_info, bi_linear_index),
101 "( OLcfgDbAt:1.7 NAME 'olcDbLinearIndex' "
102 "DESC 'Index attributes one at a time' "
103 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
104 { "lockdetect", "policy", 2, 2, 0, ARG_MAGIC|BDB_LOCKD,
105 bdb_cf_gen, "( OLcfgDbAt:1.8 NAME 'olcDbLockDetect' "
106 "DESC 'Deadlock detection algorithm' "
107 "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
108 { "mode", "mode", 2, 2, 0, ARG_INT|ARG_OFFSET,
109 (void *)offsetof(struct bdb_info, bi_dbenv_mode),
110 "( OLcfgDbAt:0.3 NAME 'olcDbMode' "
111 "DESC 'Unix permissions of database files' "
112 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
113 { "searchstack", "depth", 2, 2, 0, ARG_INT|ARG_MAGIC|BDB_SSTACK,
114 bdb_cf_gen, "( OLcfgDbAt:1.9 NAME 'olcDbSearchStack' "
115 "DESC 'Depth of search stack in IDLs' "
116 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
117 { "shm_key", "key", 2, 2, 0, ARG_INT|ARG_OFFSET,
118 (void *)offsetof(struct bdb_info, bi_shm_key),
119 "( OLcfgDbAt:1.10 NAME 'olcDbShmKey' "
120 "DESC 'Key for shared memory region' "
121 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
122 { NULL, NULL, 0, 0, 0, ARG_IGNORED,
123 NULL, NULL, NULL, NULL }
126 static ConfigOCs bdbocs[] = {
130 "NAME 'olcHdbConfig' "
131 "DESC 'HDB backend configuration' "
134 "NAME 'olcBdbConfig' "
135 "DESC 'BDB backend configuration' "
137 "SUP olcDatabaseConfig "
138 "MUST olcDbDirectory "
139 "MAY ( olcDbCacheSize $ olcDbCheckpoint $ olcDbConfig $ "
140 "olcDbNoSync $ olcDbDirtyRead $ olcDbIDLcacheSize $ "
141 "olcDbIndex $ olcDbLinearIndex $ olcDbLockDetect $ "
142 "olcDbMode $ olcDbSearchStack $ olcDbShmKey $ "
143 " olcDbCacheFree ) )",
144 Cft_Database, bdbcfg },
148 static slap_verbmasks bdb_lockd[] = {
149 { BER_BVC("default"), DB_LOCK_DEFAULT },
150 { BER_BVC("oldest"), DB_LOCK_OLDEST },
151 { BER_BVC("random"), DB_LOCK_RANDOM },
152 { BER_BVC("youngest"), DB_LOCK_YOUNGEST },
153 { BER_BVC("fewest"), DB_LOCK_MINLOCKS },
157 /* perform periodic checkpoints */
159 bdb_checkpoint( void *ctx, void *arg )
161 struct re_s *rtask = arg;
162 struct bdb_info *bdb = rtask->arg;
164 TXN_CHECKPOINT( bdb->bi_dbenv, bdb->bi_txn_cp_kbyte,
165 bdb->bi_txn_cp_min, 0 );
166 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
167 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
168 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
172 /* reindex entries on the fly */
174 bdb_online_index( void *ctx, void *arg )
176 struct re_s *rtask = arg;
177 BackendDB *be = rtask->arg;
178 struct bdb_info *bdb = be->be_private;
180 Connection conn = {0};
181 OperationBuffer opbuf;
182 Operation *op = (Operation *) &opbuf;
193 connection_fake_init( &conn, op, ctx );
202 key.size = key.ulen = sizeof(ID);
203 key.flags = DB_DBT_USERMEM;
205 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
206 data.dlen = data.ulen = 0;
209 if ( slapd_shutdown )
212 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &txn, bdb->bi_db_opflags );
215 locker = TXN_ID( txn );
218 BDB_ID2DISK( id, &nid );
219 rc = bdb->bi_id2entry->bdi_db->cursor(
220 bdb->bi_id2entry->bdi_db, txn, &curs, bdb->bi_db_opflags );
225 rc = curs->c_get( curs, &key, &data, DB_SET_RANGE );
226 curs->c_close( curs );
229 if ( rc == DB_NOTFOUND )
231 if ( rc == DB_LOCK_DEADLOCK ) {
232 ldap_pvt_thread_yield();
237 BDB_DISK2ID( &nid, &id );
241 rc = bdb_cache_find_id( op, txn, id, &ei, 0, locker, &lock );
244 if ( rc == DB_LOCK_DEADLOCK ) {
245 ldap_pvt_thread_yield();
248 if ( rc == DB_NOTFOUND ) {
256 rc = bdb_index_entry( op, txn, BDB_INDEX_UPDATE_OP, ei->bei_e );
257 if ( rc == DB_LOCK_DEADLOCK ) {
259 ldap_pvt_thread_yield();
263 rc = TXN_COMMIT( txn, 0 );
273 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
274 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
275 bdb->bi_index_task = NULL;
276 ldap_pvt_runqueue_remove( &slapd_rq, rtask );
277 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
282 /* Cleanup loose ends after Modify completes */
284 bdb_cf_cleanup( ConfigArgs *c )
286 struct bdb_info *bdb = c->be->be_private;
289 if ( bdb->bi_flags & BDB_UPD_CONFIG ) {
290 if ( bdb->bi_db_config ) {
292 FILE *f = fopen( bdb->bi_db_config_path, "w" );
294 for (i=0; bdb->bi_db_config[i].bv_val; i++)
295 fprintf( f, "%s\n", bdb->bi_db_config[i].bv_val );
299 unlink( bdb->bi_db_config_path );
301 bdb->bi_flags ^= BDB_UPD_CONFIG;
304 if ( bdb->bi_flags & BDB_DEL_INDEX ) {
305 bdb_attr_flush( bdb );
306 bdb->bi_flags ^= BDB_DEL_INDEX;
309 if ( bdb->bi_flags & BDB_RE_OPEN ) {
310 bdb->bi_flags ^= BDB_RE_OPEN;
311 rc = c->be->bd_info->bi_db_close( c->be );
313 rc = c->be->bd_info->bi_db_open( c->be );
314 /* If this fails, we need to restart */
317 Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(bdb_cf_cleanup)
318 ": failed to reopen database, rc=%d", rc, 0, 0 );
325 bdb_cf_gen(ConfigArgs *c)
327 struct bdb_info *bdb = c->be->be_private;
330 if ( c->op == SLAP_CONFIG_EMIT ) {
334 if (bdb->bi_txn_cp ) {
337 bv.bv_len = sprintf( buf, "%d %d", bdb->bi_txn_cp_kbyte,
338 bdb->bi_txn_cp_min );
340 value_add_one( &c->rvalue_vals, &bv );
347 if ( bdb->bi_dbenv_home ) {
348 c->value_string = ch_strdup( bdb->bi_dbenv_home );
355 if (( slapMode&SLAP_SERVER_MODE ) && !( bdb->bi_flags&BDB_IS_OPEN )
356 && !bdb->bi_db_config ) {
357 char buf[SLAP_TEXT_BUFLEN];
358 FILE *f = fopen( bdb->bi_db_config_path, "r" );
362 bdb->bi_flags |= BDB_HAS_CONFIG;
363 while ( fgets( buf, sizeof(buf), f )) {
364 ber_str2bv( buf, 0, 1, &bv );
365 if ( bv.bv_len > 0 && bv.bv_val[bv.bv_len-1] == '\n' ) {
367 bv.bv_val[bv.bv_len] = '\0';
369 /* shouldn't need this, but ... */
370 if ( bv.bv_len > 0 && bv.bv_val[bv.bv_len-1] == '\r' ) {
372 bv.bv_val[bv.bv_len] = '\0';
374 ber_bvarray_add( &bdb->bi_db_config, &bv );
379 if ( bdb->bi_db_config ) {
384 for (i=0; !BER_BVISNULL(&bdb->bi_db_config[i]); i++) {
385 bv.bv_len = sprintf( bv.bv_val, "{%d}%s", i,
386 bdb->bi_db_config[i].bv_val );
387 value_add_one( &c->rvalue_vals, &bv );
390 if ( !c->rvalue_vals ) rc = 1;
394 if ( bdb->bi_dbenv_xflags & DB_TXN_NOSYNC )
399 bdb_attr_index_unparse( bdb, &c->rvalue_vals );
400 if ( !c->rvalue_vals ) rc = 1;
405 if ( bdb->bi_lock_detect != DB_LOCK_DEFAULT ) {
407 for (i=0; !BER_BVISNULL(&bdb_lockd[i].word); i++) {
408 if ( bdb->bi_lock_detect == bdb_lockd[i].mask ) {
409 value_add_one( &c->rvalue_vals, &bdb_lockd[i].word );
418 c->value_int = bdb->bi_search_stack_depth;
422 } else if ( c->op == LDAP_MOD_DELETE ) {
425 /* single-valued no-ops */
431 if ( bdb->bi_txn_cp_task ) {
432 struct re_s *re = bdb->bi_txn_cp_task;
433 bdb->bi_txn_cp_task = NULL;
434 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ))
435 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
436 ldap_pvt_runqueue_remove( &slapd_rq, re );
442 ber_bvarray_free( bdb->bi_db_config );
443 bdb->bi_db_config = NULL;
446 ch_free( bdb->bi_db_config[i].bv_val );
447 for (; bdb->bi_db_config[i].bv_val; i++)
448 bdb->bi_db_config[i] = bdb->bi_db_config[i+1];
450 bdb->bi_flags |= BDB_UPD_CONFIG;
451 c->cleanup = bdb_cf_cleanup;
454 bdb->bi_flags |= BDB_RE_OPEN;
455 bdb->bi_flags ^= BDB_HAS_CONFIG;
456 ch_free( bdb->bi_dbenv_home );
457 bdb->bi_dbenv_home = NULL;
458 ch_free( bdb->bi_db_config_path );
459 bdb->bi_db_config_path = NULL;
460 c->cleanup = bdb_cf_cleanup;
461 ldap_pvt_thread_pool_purgekey( bdb->bi_dbenv );
462 ldap_pvt_thread_pool_purgekey( ((char *)bdb->bi_dbenv) + 1 );
465 bdb->bi_dbenv->set_flags( bdb->bi_dbenv, DB_TXN_NOSYNC, 0 );
468 if ( c->valx == -1 ) {
469 /* delete all (FIXME) */
472 struct berval bv, def = BER_BVC("default");
475 for (ptr = c->line; !isspace( *ptr ); ptr++);
478 bv.bv_len = ptr - bv.bv_val;
479 if ( bvmatch( &bv, &def )) {
480 bdb->bi_defaultmask = 0;
487 sep = bv.bv_val[ bv.bv_len ];
488 bv.bv_val[ bv.bv_len ] = '\0';
489 attrs = ldap_str2charray( bv.bv_val, "," );
491 for ( i = 0; attrs[ i ]; i++ ) {
492 AttributeDescription *ad = NULL;
496 slap_str2ad( attrs[ i ], &ad, &text );
497 /* if we got here... */
498 assert( ad != NULL );
500 ai = bdb_attr_mask( bdb, ad );
501 /* if we got here... */
502 assert( ai != NULL );
504 ai->ai_indexmask |= BDB_INDEX_DELETING;
505 bdb->bi_flags |= BDB_DEL_INDEX;
506 c->cleanup = bdb_cf_cleanup;
509 bv.bv_val[ bv.bv_len ] = sep;
510 ldap_charray_free( attrs );
522 if ( lutil_atolx( &l, c->argv[1], 0 ) != 0 ) {
523 fprintf( stderr, "%s: "
524 "invalid kbyte \"%s\" in \"checkpoint\".\n",
525 c->log, c->argv[1] );
528 bdb->bi_txn_cp_kbyte = l;
529 if ( lutil_atolx( &l, c->argv[2], 0 ) != 0 ) {
530 fprintf( stderr, "%s: "
531 "invalid minutes \"%s\" in \"checkpoint\".\n",
532 c->log, c->argv[2] );
535 bdb->bi_txn_cp_min = l;
536 /* If we're in server mode and time-based checkpointing is enabled,
537 * submit a task to perform periodic checkpoints.
539 if ((slapMode & SLAP_SERVER_MODE) && bdb->bi_txn_cp_min ) {
540 struct re_s *re = bdb->bi_txn_cp_task;
542 re->interval.tv_sec = bdb->bi_txn_cp_min * 60;
544 if ( c->be->be_suffix == NULL || BER_BVISNULL( &c->be->be_suffix[0] ) ) {
545 fprintf( stderr, "%s: "
546 "\"checkpoint\" must occur after \"suffix\".\n",
550 bdb->bi_txn_cp_task = ldap_pvt_runqueue_insert( &slapd_rq,
551 bdb->bi_txn_cp_min * 60, bdb_checkpoint, bdb,
552 LDAP_XSTRING(bdb_checkpoint), c->be->be_suffix[0].bv_val );
561 if ( c->op == SLAP_CONFIG_ADD ) {
562 ptr += STRLENOF("dbconfig");
563 while (!isspace(*ptr)) ptr++;
564 while (isspace(*ptr)) ptr++;
567 if ( bdb->bi_flags & BDB_IS_OPEN ) {
568 bdb->bi_flags |= BDB_UPD_CONFIG;
569 c->cleanup = bdb_cf_cleanup;
571 /* If we're just starting up...
574 /* If a DB_CONFIG file exists, or we don't know the path
575 * to the DB_CONFIG file, ignore these directives
577 if (( bdb->bi_flags & BDB_HAS_CONFIG ) || !bdb->bi_db_config_path )
579 f = fopen( bdb->bi_db_config_path, "a" );
581 /* FIXME: EBCDIC probably needs special handling */
582 fprintf( f, "%s\n", ptr );
586 ber_str2bv( ptr, 0, 1, &bv );
587 ber_bvarray_add( &bdb->bi_db_config, &bv );
591 case BDB_DIRECTORY: {
595 if ( bdb->bi_dbenv_home )
596 ch_free( bdb->bi_dbenv_home );
597 bdb->bi_dbenv_home = c->value_string;
599 /* See if a DB_CONFIG file already exists here */
600 if ( bdb->bi_db_config_path )
601 ch_free( bdb->bi_db_config_path );
602 bdb->bi_db_config_path = ch_malloc( strlen( bdb->bi_dbenv_home ) +
603 STRLENOF(LDAP_DIRSEP) + STRLENOF("DB_CONFIG") + 1 );
604 ptr = lutil_strcopy( bdb->bi_db_config_path, bdb->bi_dbenv_home );
605 *ptr++ = LDAP_DIRSEP[0];
606 strcpy( ptr, "DB_CONFIG" );
608 f = fopen( bdb->bi_db_config_path, "r" );
610 bdb->bi_flags |= BDB_HAS_CONFIG;
618 bdb->bi_dbenv_xflags |= DB_TXN_NOSYNC;
620 bdb->bi_dbenv_xflags &= ~DB_TXN_NOSYNC;
621 if ( bdb->bi_flags & BDB_IS_OPEN ) {
622 bdb->bi_dbenv->set_flags( bdb->bi_dbenv, DB_TXN_NOSYNC,
628 rc = bdb_attr_index_config( bdb, c->fname, c->lineno,
629 c->argc - 1, &c->argv[1] );
631 if( rc != LDAP_SUCCESS ) return 1;
632 if (( bdb->bi_flags & BDB_IS_OPEN ) && !bdb->bi_index_task ) {
633 /* Start the task as soon as we finish here. Set a long
634 * interval (10 hours) so that it only gets scheduled once.
636 if ( c->be->be_suffix == NULL || BER_BVISNULL( &c->be->be_suffix[0] ) ) {
637 fprintf( stderr, "%s: "
638 "\"index\" must occur after \"suffix\".\n",
642 bdb->bi_index_task = ldap_pvt_runqueue_insert( &slapd_rq, 36000,
643 bdb_online_index, c->be,
644 LDAP_XSTRING(bdb_online_index), c->be->be_suffix[0].bv_val );
649 rc = verb_to_mask( c->argv[1], bdb_lockd );
650 if ( BER_BVISNULL(&bdb_lockd[rc].word) ) {
651 fprintf( stderr, "%s: "
652 "bad policy (%s) in \"lockDetect <policy>\" line\n",
653 c->log, c->argv[1] );
656 bdb->bi_lock_detect = rc;
660 if ( c->value_int < MINIMUM_SEARCH_STACK_DEPTH ) {
662 "%s: depth %d too small, using %d\n",
663 c->log, c->value_int, MINIMUM_SEARCH_STACK_DEPTH );
664 c->value_int = MINIMUM_SEARCH_STACK_DEPTH;
666 bdb->bi_search_stack_depth = c->value_int;
672 int bdb_back_init_cf( BackendInfo *bi )
675 bi->bi_cf_ocs = bdbocs;
677 rc = config_register_schema( bdbcfg, bdbocs );