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 { "cachesize", "size", 2, 2, 0, ARG_INT|ARG_OFFSET,
59 (void *)offsetof(struct bdb_info, bi_cache.c_maxsize),
60 "( OLcfgDbAt:1.1 NAME 'olcDbCacheSize' "
61 "DESC 'Entry cache size in entries' "
62 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
63 { "checkpoint", "kbyte> <min", 3, 3, 0, ARG_MAGIC|BDB_CHKPT,
64 bdb_cf_gen, "( OLcfgDbAt:1.2 NAME 'olcDbCheckpoint' "
65 "DESC 'Database checkpoint interval in kbytes and minutes' "
66 "SYNTAX OMsDirectoryString SINGLE-VALUE )",NULL, NULL },
67 { "dbconfig", "DB_CONFIG setting", 1, 0, 0, ARG_MAGIC|BDB_CONFIG,
68 bdb_cf_gen, "( OLcfgDbAt:1.3 NAME 'olcDbConfig' "
69 "DESC 'BerkeleyDB DB_CONFIG configuration directives' "
70 "SYNTAX OMsDirectoryString X-ORDERED 'VALUES' )", NULL, NULL },
71 { "dbnosync", NULL, 1, 2, 0, ARG_ON_OFF|ARG_MAGIC|BDB_NOSYNC,
72 bdb_cf_gen, "( OLcfgDbAt:1.4 NAME 'olcDbNoSync' "
73 "DESC 'Disable synchronous database writes' "
74 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
75 { "dirtyread", NULL, 1, 2, 0,
76 #ifdef SLAP_BDB_ALLOW_DIRTY_READ
77 ARG_ON_OFF|ARG_MAGIC|BDB_DIRTYR, bdb_cf_gen,
81 "( OLcfgDbAt:1.5 NAME 'olcDbDirtyRead' "
82 "DESC 'Allow reads of uncommitted data' "
83 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
84 { "idlcachesize", "size", 2, 2, 0, ARG_INT|ARG_OFFSET,
85 (void *)offsetof(struct bdb_info,bi_idl_cache_max_size),
86 "( OLcfgDbAt:1.6 NAME 'olcDbIDLcacheSize' "
87 "DESC 'IDL cache size in IDLs' "
88 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
89 { "index", "attr> <[pres,eq,approx,sub]", 2, 3, 0, ARG_MAGIC|BDB_INDEX,
90 bdb_cf_gen, "( OLcfgDbAt:0.2 NAME 'olcDbIndex' "
91 "DESC 'Attribute index parameters' "
92 "EQUALITY caseIgnoreMatch "
93 "SYNTAX OMsDirectoryString )", NULL, NULL },
94 { "linearindex", NULL, 1, 2, 0, ARG_ON_OFF|ARG_OFFSET,
95 (void *)offsetof(struct bdb_info, bi_linear_index),
96 "( OLcfgDbAt:1.7 NAME 'olcDbLinearIndex' "
97 "DESC 'Index attributes one at a time' "
98 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
99 { "lockdetect", "policy", 2, 2, 0, ARG_MAGIC|BDB_LOCKD,
100 bdb_cf_gen, "( OLcfgDbAt:1.8 NAME 'olcDbLockDetect' "
101 "DESC 'Deadlock detection algorithm' "
102 "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
103 { "mode", "mode", 2, 2, 0, ARG_INT|ARG_OFFSET,
104 (void *)offsetof(struct bdb_info, bi_dbenv_mode),
105 "( OLcfgDbAt:0.3 NAME 'olcDbMode' "
106 "DESC 'Unix permissions of database files' "
107 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
108 { "searchstack", "depth", 2, 2, 0, ARG_INT|ARG_MAGIC|BDB_SSTACK,
109 bdb_cf_gen, "( OLcfgDbAt:1.9 NAME 'olcDbSearchStack' "
110 "DESC 'Depth of search stack in IDLs' "
111 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
112 { "shm_key", "key", 2, 2, 0, ARG_INT|ARG_OFFSET,
113 (void *)offsetof(struct bdb_info, bi_shm_key),
114 "( OLcfgDbAt:1.10 NAME 'olcDbShmKey' "
115 "DESC 'Key for shared memory region' "
116 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
117 { NULL, NULL, 0, 0, 0, ARG_IGNORED,
118 NULL, NULL, NULL, NULL }
121 static ConfigOCs bdbocs[] = {
125 "NAME 'olcHdbConfig' "
126 "DESC 'HDB backend configuration' "
129 "NAME 'olcBdbConfig' "
130 "DESC 'BDB backend configuration' "
132 "SUP olcDatabaseConfig "
133 "MUST olcDbDirectory "
134 "MAY ( olcDbCacheSize $ olcDbCheckpoint $ olcDbConfig $ "
135 "olcDbNoSync $ olcDbDirtyRead $ olcDbIDLcacheSize $ "
136 "olcDbIndex $ olcDbLinearIndex $ olcDbLockDetect $ "
137 "olcDbMode $ olcDbSearchStack $ olcDbShmKey ) )",
138 Cft_Database, bdbcfg },
142 static slap_verbmasks bdb_lockd[] = {
143 { BER_BVC("default"), DB_LOCK_DEFAULT },
144 { BER_BVC("oldest"), DB_LOCK_OLDEST },
145 { BER_BVC("random"), DB_LOCK_RANDOM },
146 { BER_BVC("youngest"), DB_LOCK_YOUNGEST },
147 { BER_BVC("fewest"), DB_LOCK_MINLOCKS },
151 /* perform periodic checkpoints */
153 bdb_checkpoint( void *ctx, void *arg )
155 struct re_s *rtask = arg;
156 struct bdb_info *bdb = rtask->arg;
158 TXN_CHECKPOINT( bdb->bi_dbenv, bdb->bi_txn_cp_kbyte,
159 bdb->bi_txn_cp_min, 0 );
160 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
161 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
162 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
166 /* reindex entries on the fly */
168 bdb_online_index( void *ctx, void *arg )
170 struct re_s *rtask = arg;
171 BackendDB *be = rtask->arg;
172 struct bdb_info *bdb = be->be_private;
174 Connection conn = {0};
175 OperationBuffer opbuf;
176 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 for ( i = 0; i < bdb->bi_nattrs; i++ ) {
269 if ( bdb->bi_attrs[ i ]->ai_indexmask & BDB_INDEX_DELETING ) {
272 bdb->bi_attrs[ i ]->ai_indexmask = bdb->bi_attrs[ i ]->ai_newmask;
273 bdb->bi_attrs[ i ]->ai_newmask = 0;
276 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
277 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
278 bdb->bi_index_task = NULL;
279 ldap_pvt_runqueue_remove( &slapd_rq, rtask );
280 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
285 /* Cleanup loose ends after Modify completes */
287 bdb_cf_cleanup( ConfigArgs *c )
289 struct bdb_info *bdb = c->be->be_private;
292 if ( bdb->bi_flags & BDB_UPD_CONFIG ) {
293 if ( bdb->bi_db_config ) {
295 FILE *f = fopen( bdb->bi_db_config_path, "w" );
297 for (i=0; bdb->bi_db_config[i].bv_val; i++)
298 fprintf( f, "%s\n", bdb->bi_db_config[i].bv_val );
302 unlink( bdb->bi_db_config_path );
304 bdb->bi_flags ^= BDB_UPD_CONFIG;
307 if ( bdb->bi_flags & BDB_DEL_INDEX ) {
308 bdb_attr_flush( bdb );
309 bdb->bi_flags ^= BDB_DEL_INDEX;
312 if ( bdb->bi_flags & BDB_RE_OPEN ) {
313 bdb->bi_flags ^= BDB_RE_OPEN;
314 rc = c->be->bd_info->bi_db_close( c->be );
316 rc = c->be->bd_info->bi_db_open( c->be );
317 /* If this fails, we need to restart */
320 Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(bdb_cf_cleanup)
321 ": failed to reopen database, rc=%d", rc, 0, 0 );
328 bdb_cf_gen(ConfigArgs *c)
330 struct bdb_info *bdb = c->be->be_private;
333 if ( c->op == SLAP_CONFIG_EMIT ) {
337 if (bdb->bi_txn_cp ) {
340 bv.bv_len = sprintf( buf, "%d %d", bdb->bi_txn_cp_kbyte,
341 bdb->bi_txn_cp_min );
343 value_add_one( &c->rvalue_vals, &bv );
350 if ( bdb->bi_dbenv_home ) {
351 c->value_string = ch_strdup( bdb->bi_dbenv_home );
358 if (( slapMode&SLAP_SERVER_MODE ) && !( bdb->bi_flags&BDB_IS_OPEN )
359 && !bdb->bi_db_config ) {
360 char buf[SLAP_TEXT_BUFLEN];
361 FILE *f = fopen( bdb->bi_db_config_path, "r" );
365 bdb->bi_flags |= BDB_HAS_CONFIG;
366 while ( fgets( buf, sizeof(buf), f )) {
367 ber_str2bv( buf, 0, 1, &bv );
368 if ( bv.bv_len > 0 && bv.bv_val[bv.bv_len-1] == '\n' ) {
370 bv.bv_val[bv.bv_len] = '\0';
372 /* shouldn't need this, but ... */
373 if ( bv.bv_len > 0 && bv.bv_val[bv.bv_len-1] == '\r' ) {
375 bv.bv_val[bv.bv_len] = '\0';
377 ber_bvarray_add( &bdb->bi_db_config, &bv );
382 if ( bdb->bi_db_config ) {
387 for (i=0; !BER_BVISNULL(&bdb->bi_db_config[i]); i++) {
388 bv.bv_len = sprintf( bv.bv_val, "{%d}%s", i,
389 bdb->bi_db_config[i].bv_val );
390 value_add_one( &c->rvalue_vals, &bv );
393 if ( !c->rvalue_vals ) rc = 1;
397 if ( bdb->bi_dbenv_xflags & DB_TXN_NOSYNC )
402 bdb_attr_index_unparse( bdb, &c->rvalue_vals );
403 if ( !c->rvalue_vals ) rc = 1;
408 if ( bdb->bi_lock_detect != DB_LOCK_DEFAULT ) {
410 for (i=0; !BER_BVISNULL(&bdb_lockd[i].word); i++) {
411 if ( bdb->bi_lock_detect == bdb_lockd[i].mask ) {
412 value_add_one( &c->rvalue_vals, &bdb_lockd[i].word );
421 c->value_int = bdb->bi_search_stack_depth;
425 } else if ( c->op == LDAP_MOD_DELETE ) {
428 /* single-valued no-ops */
434 if ( bdb->bi_txn_cp_task ) {
435 struct re_s *re = bdb->bi_txn_cp_task;
436 bdb->bi_txn_cp_task = NULL;
437 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ))
438 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
439 ldap_pvt_runqueue_remove( &slapd_rq, re );
445 ber_bvarray_free( bdb->bi_db_config );
446 bdb->bi_db_config = NULL;
449 ch_free( bdb->bi_db_config[i].bv_val );
450 for (; bdb->bi_db_config[i].bv_val; i++)
451 bdb->bi_db_config[i] = bdb->bi_db_config[i+1];
453 bdb->bi_flags |= BDB_UPD_CONFIG;
454 c->cleanup = bdb_cf_cleanup;
457 bdb->bi_flags |= BDB_RE_OPEN;
458 bdb->bi_flags ^= BDB_HAS_CONFIG;
459 ch_free( bdb->bi_dbenv_home );
460 bdb->bi_dbenv_home = NULL;
461 ch_free( bdb->bi_db_config_path );
462 bdb->bi_db_config_path = NULL;
463 c->cleanup = bdb_cf_cleanup;
464 ldap_pvt_thread_pool_purgekey( bdb->bi_dbenv );
465 ldap_pvt_thread_pool_purgekey( ((char *)bdb->bi_dbenv) + 1 );
468 bdb->bi_dbenv->set_flags( bdb->bi_dbenv, DB_TXN_NOSYNC, 0 );
471 if ( c->valx == -1 ) {
474 /* delete all (FIXME) */
475 for ( i = 0; i < bdb->bi_nattrs; i++ ) {
476 bdb->bi_attrs[i]->ai_indexmask |= BDB_INDEX_DELETING;
478 bdb->bi_flags |= BDB_DEL_INDEX;
479 c->cleanup = bdb_cf_cleanup;
482 struct berval bv, def = BER_BVC("default");
485 for (ptr = c->line; !isspace( *ptr ); ptr++);
488 bv.bv_len = ptr - bv.bv_val;
489 if ( bvmatch( &bv, &def )) {
490 bdb->bi_defaultmask = 0;
497 sep = bv.bv_val[ bv.bv_len ];
498 bv.bv_val[ bv.bv_len ] = '\0';
499 attrs = ldap_str2charray( bv.bv_val, "," );
501 for ( i = 0; attrs[ i ]; i++ ) {
502 AttributeDescription *ad = NULL;
506 slap_str2ad( attrs[ i ], &ad, &text );
507 /* if we got here... */
508 assert( ad != NULL );
510 ai = bdb_attr_mask( bdb, ad );
511 /* if we got here... */
512 assert( ai != NULL );
514 ai->ai_indexmask |= BDB_INDEX_DELETING;
515 bdb->bi_flags |= BDB_DEL_INDEX;
516 c->cleanup = bdb_cf_cleanup;
519 bv.bv_val[ bv.bv_len ] = sep;
520 ldap_charray_free( attrs );
532 if ( lutil_atolx( &l, c->argv[1], 0 ) != 0 ) {
533 fprintf( stderr, "%s: "
534 "invalid kbyte \"%s\" in \"checkpoint\".\n",
535 c->log, c->argv[1] );
538 bdb->bi_txn_cp_kbyte = l;
539 if ( lutil_atolx( &l, c->argv[2], 0 ) != 0 ) {
540 fprintf( stderr, "%s: "
541 "invalid minutes \"%s\" in \"checkpoint\".\n",
542 c->log, c->argv[2] );
545 bdb->bi_txn_cp_min = l;
546 /* If we're in server mode and time-based checkpointing is enabled,
547 * submit a task to perform periodic checkpoints.
549 if ((slapMode & SLAP_SERVER_MODE) && bdb->bi_txn_cp_min ) {
550 struct re_s *re = bdb->bi_txn_cp_task;
552 re->interval.tv_sec = bdb->bi_txn_cp_min * 60;
554 if ( c->be->be_suffix == NULL || BER_BVISNULL( &c->be->be_suffix[0] ) ) {
555 fprintf( stderr, "%s: "
556 "\"checkpoint\" must occur after \"suffix\".\n",
560 bdb->bi_txn_cp_task = ldap_pvt_runqueue_insert( &slapd_rq,
561 bdb->bi_txn_cp_min * 60, bdb_checkpoint, bdb,
562 LDAP_XSTRING(bdb_checkpoint), c->be->be_suffix[0].bv_val );
571 if ( c->op == SLAP_CONFIG_ADD ) {
572 ptr += STRLENOF("dbconfig");
573 while (!isspace(*ptr)) ptr++;
574 while (isspace(*ptr)) ptr++;
577 if ( bdb->bi_flags & BDB_IS_OPEN ) {
578 bdb->bi_flags |= BDB_UPD_CONFIG;
579 c->cleanup = bdb_cf_cleanup;
581 /* If we're just starting up...
584 /* If a DB_CONFIG file exists, or we don't know the path
585 * to the DB_CONFIG file, ignore these directives
587 if (( bdb->bi_flags & BDB_HAS_CONFIG ) || !bdb->bi_db_config_path )
589 f = fopen( bdb->bi_db_config_path, "a" );
591 /* FIXME: EBCDIC probably needs special handling */
592 fprintf( f, "%s\n", ptr );
596 ber_str2bv( ptr, 0, 1, &bv );
597 ber_bvarray_add( &bdb->bi_db_config, &bv );
601 case BDB_DIRECTORY: {
605 if ( bdb->bi_dbenv_home )
606 ch_free( bdb->bi_dbenv_home );
607 bdb->bi_dbenv_home = c->value_string;
609 /* See if a DB_CONFIG file already exists here */
610 if ( bdb->bi_db_config_path )
611 ch_free( bdb->bi_db_config_path );
612 bdb->bi_db_config_path = ch_malloc( strlen( bdb->bi_dbenv_home ) +
613 STRLENOF(LDAP_DIRSEP) + STRLENOF("DB_CONFIG") + 1 );
614 ptr = lutil_strcopy( bdb->bi_db_config_path, bdb->bi_dbenv_home );
615 *ptr++ = LDAP_DIRSEP[0];
616 strcpy( ptr, "DB_CONFIG" );
618 f = fopen( bdb->bi_db_config_path, "r" );
620 bdb->bi_flags |= BDB_HAS_CONFIG;
628 bdb->bi_dbenv_xflags |= DB_TXN_NOSYNC;
630 bdb->bi_dbenv_xflags &= ~DB_TXN_NOSYNC;
631 if ( bdb->bi_flags & BDB_IS_OPEN ) {
632 bdb->bi_dbenv->set_flags( bdb->bi_dbenv, DB_TXN_NOSYNC,
638 rc = bdb_attr_index_config( bdb, c->fname, c->lineno,
639 c->argc - 1, &c->argv[1] );
641 if( rc != LDAP_SUCCESS ) return 1;
642 if (( bdb->bi_flags & BDB_IS_OPEN ) && !bdb->bi_index_task ) {
643 /* Start the task as soon as we finish here. Set a long
644 * interval (10 hours) so that it only gets scheduled once.
646 if ( c->be->be_suffix == NULL || BER_BVISNULL( &c->be->be_suffix[0] ) ) {
647 fprintf( stderr, "%s: "
648 "\"index\" must occur after \"suffix\".\n",
652 bdb->bi_index_task = ldap_pvt_runqueue_insert( &slapd_rq, 36000,
653 bdb_online_index, c->be,
654 LDAP_XSTRING(bdb_online_index), c->be->be_suffix[0].bv_val );
659 rc = verb_to_mask( c->argv[1], bdb_lockd );
660 if ( BER_BVISNULL(&bdb_lockd[rc].word) ) {
661 fprintf( stderr, "%s: "
662 "bad policy (%s) in \"lockDetect <policy>\" line\n",
663 c->log, c->argv[1] );
666 bdb->bi_lock_detect = rc;
670 if ( c->value_int < MINIMUM_SEARCH_STACK_DEPTH ) {
672 "%s: depth %d too small, using %d\n",
673 c->log, c->value_int, MINIMUM_SEARCH_STACK_DEPTH );
674 c->value_int = MINIMUM_SEARCH_STACK_DEPTH;
676 bdb->bi_search_stack_depth = c->value_int;
682 int bdb_back_init_cf( BackendInfo *bi )
685 bi->bi_cf_ocs = bdbocs;
687 rc = config_register_schema( bdbcfg, bdbocs );