1 /* config.c - bdb backend configuration file routine */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2007 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>
32 # define SLAP_BDB_ALLOW_DIRTY_READ
35 #define bdb_cf_gen BDB_SYMBOL(cf_gen)
36 #define bdb_cf_cleanup BDB_SYMBOL(cf_cleanup)
37 #define bdb_checkpoint BDB_SYMBOL(checkpoint)
38 #define bdb_online_index BDB_SYMBOL(online_index)
40 static ConfigDriver bdb_cf_gen;
53 static ConfigTable bdbcfg[] = {
54 { "directory", "dir", 2, 2, 0, ARG_STRING|ARG_MAGIC|BDB_DIRECTORY,
55 bdb_cf_gen, "( OLcfgDbAt:0.1 NAME 'olcDbDirectory' "
56 "DESC 'Directory for database content' "
57 "EQUALITY caseIgnoreMatch "
58 "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
59 { "cachefree", "size", 2, 2, 0, ARG_UINT|ARG_OFFSET,
60 (void *)offsetof(struct bdb_info, bi_cache.c_minfree),
61 "( OLcfgDbAt:1.11 NAME 'olcDbCacheFree' "
62 "DESC 'Number of extra entries to free when max is reached' "
63 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
64 { "cachesize", "size", 2, 2, 0, ARG_UINT|ARG_OFFSET,
65 (void *)offsetof(struct bdb_info, bi_cache.c_maxsize),
66 "( OLcfgDbAt:1.1 NAME 'olcDbCacheSize' "
67 "DESC 'Entry cache size in entries' "
68 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
69 { "checkpoint", "kbyte> <min", 3, 3, 0, ARG_MAGIC|BDB_CHKPT,
70 bdb_cf_gen, "( OLcfgDbAt:1.2 NAME 'olcDbCheckpoint' "
71 "DESC 'Database checkpoint interval in kbytes and minutes' "
72 "SYNTAX OMsDirectoryString SINGLE-VALUE )",NULL, NULL },
73 { "dbconfig", "DB_CONFIG setting", 1, 0, 0, ARG_MAGIC|BDB_CONFIG,
74 bdb_cf_gen, "( OLcfgDbAt:1.3 NAME 'olcDbConfig' "
75 "DESC 'BerkeleyDB DB_CONFIG configuration directives' "
76 "SYNTAX OMsIA5String X-ORDERED 'VALUES' )", NULL, NULL },
77 { "dbnosync", NULL, 1, 2, 0, ARG_ON_OFF|ARG_MAGIC|BDB_NOSYNC,
78 bdb_cf_gen, "( OLcfgDbAt:1.4 NAME 'olcDbNoSync' "
79 "DESC 'Disable synchronous database writes' "
80 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
81 { "dirtyread", NULL, 1, 2, 0,
82 #ifdef SLAP_BDB_ALLOW_DIRTY_READ
83 ARG_ON_OFF|ARG_MAGIC|BDB_DIRTYR, bdb_cf_gen,
87 "( OLcfgDbAt:1.5 NAME 'olcDbDirtyRead' "
88 "DESC 'Allow reads of uncommitted data' "
89 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
90 { "dncachesize", "size", 2, 2, 0, ARG_UINT|ARG_OFFSET,
91 (void *)offsetof(struct bdb_info, bi_cache.c_eimax),
92 "( OLcfgDbAt:1.12 NAME 'olcDbDNcacheSize' "
93 "DESC 'DN cache size' "
94 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
95 { "idlcachesize", "size", 2, 2, 0, ARG_UINT|ARG_OFFSET,
96 (void *)offsetof(struct bdb_info, bi_idl_cache_max_size),
97 "( OLcfgDbAt:1.6 NAME 'olcDbIDLcacheSize' "
98 "DESC 'IDL cache size in IDLs' "
99 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
100 { "index", "attr> <[pres,eq,approx,sub]", 2, 3, 0, ARG_MAGIC|BDB_INDEX,
101 bdb_cf_gen, "( OLcfgDbAt:0.2 NAME 'olcDbIndex' "
102 "DESC 'Attribute index parameters' "
103 "EQUALITY caseIgnoreMatch "
104 "SYNTAX OMsDirectoryString )", NULL, NULL },
105 { "linearindex", NULL, 1, 2, 0, ARG_ON_OFF|ARG_OFFSET,
106 (void *)offsetof(struct bdb_info, bi_linear_index),
107 "( OLcfgDbAt:1.7 NAME 'olcDbLinearIndex' "
108 "DESC 'Index attributes one at a time' "
109 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
110 { "lockdetect", "policy", 2, 2, 0, ARG_MAGIC|BDB_LOCKD,
111 bdb_cf_gen, "( OLcfgDbAt:1.8 NAME 'olcDbLockDetect' "
112 "DESC 'Deadlock detection algorithm' "
113 "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
114 { "mode", "mode", 2, 2, 0, ARG_INT|ARG_OFFSET,
115 (void *)offsetof(struct bdb_info, bi_dbenv_mode),
116 "( OLcfgDbAt:0.3 NAME 'olcDbMode' "
117 "DESC 'Unix permissions of database files' "
118 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
119 { "searchstack", "depth", 2, 2, 0, ARG_INT|ARG_MAGIC|BDB_SSTACK,
120 bdb_cf_gen, "( OLcfgDbAt:1.9 NAME 'olcDbSearchStack' "
121 "DESC 'Depth of search stack in IDLs' "
122 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
123 { "shm_key", "key", 2, 2, 0, ARG_LONG|ARG_OFFSET,
124 (void *)offsetof(struct bdb_info, bi_shm_key),
125 "( OLcfgDbAt:1.10 NAME 'olcDbShmKey' "
126 "DESC 'Key for shared memory region' "
127 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
128 { NULL, NULL, 0, 0, 0, ARG_IGNORED,
129 NULL, NULL, NULL, NULL }
132 static ConfigOCs bdbocs[] = {
136 "NAME 'olcHdbConfig' "
137 "DESC 'HDB backend configuration' "
140 "NAME 'olcBdbConfig' "
141 "DESC 'BDB backend configuration' "
143 "SUP olcDatabaseConfig "
144 "MUST olcDbDirectory "
145 "MAY ( olcDbCacheSize $ olcDbCheckpoint $ olcDbConfig $ "
146 "olcDbNoSync $ olcDbDirtyRead $ olcDbIDLcacheSize $ "
147 "olcDbIndex $ olcDbLinearIndex $ olcDbLockDetect $ "
148 "olcDbMode $ olcDbSearchStack $ olcDbShmKey $ "
149 "olcDbCacheFree $ olcDbDNcacheSize ) )",
150 Cft_Database, bdbcfg },
154 static slap_verbmasks bdb_lockd[] = {
155 { BER_BVC("default"), DB_LOCK_DEFAULT },
156 { BER_BVC("oldest"), DB_LOCK_OLDEST },
157 { BER_BVC("random"), DB_LOCK_RANDOM },
158 { BER_BVC("youngest"), DB_LOCK_YOUNGEST },
159 { BER_BVC("fewest"), DB_LOCK_MINLOCKS },
163 /* perform periodic checkpoints */
165 bdb_checkpoint( void *ctx, void *arg )
167 struct re_s *rtask = arg;
168 struct bdb_info *bdb = rtask->arg;
170 TXN_CHECKPOINT( bdb->bi_dbenv, bdb->bi_txn_cp_kbyte,
171 bdb->bi_txn_cp_min, 0 );
172 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
173 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
174 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
178 /* reindex entries on the fly */
180 bdb_online_index( void *ctx, void *arg )
182 struct re_s *rtask = arg;
183 BackendDB *be = rtask->arg;
184 struct bdb_info *bdb = be->be_private;
186 Connection conn = {0};
187 OperationBuffer opbuf;
200 connection_fake_init( &conn, &opbuf, ctx );
210 key.size = key.ulen = sizeof(ID);
211 key.flags = DB_DBT_USERMEM;
213 data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
214 data.dlen = data.ulen = 0;
217 if ( slapd_shutdown )
220 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &txn, bdb->bi_db_opflags );
223 locker = TXN_ID( txn );
226 BDB_ID2DISK( id, &nid );
227 rc = bdb->bi_id2entry->bdi_db->cursor(
228 bdb->bi_id2entry->bdi_db, txn, &curs, bdb->bi_db_opflags );
233 rc = curs->c_get( curs, &key, &data, DB_SET_RANGE );
234 curs->c_close( curs );
237 if ( rc == DB_NOTFOUND )
239 if ( rc == DB_LOCK_DEADLOCK ) {
240 ldap_pvt_thread_yield();
245 BDB_DISK2ID( &nid, &id );
249 rc = bdb_cache_find_id( op, txn, id, &ei, 0, locker, &lock );
252 if ( rc == DB_LOCK_DEADLOCK ) {
253 ldap_pvt_thread_yield();
256 if ( rc == DB_NOTFOUND ) {
264 rc = bdb_index_entry( op, txn, BDB_INDEX_UPDATE_OP, ei->bei_e );
265 if ( rc == DB_LOCK_DEADLOCK ) {
267 ldap_pvt_thread_yield();
271 rc = TXN_COMMIT( txn, 0 );
281 for ( i = 0; i < bdb->bi_nattrs; i++ ) {
282 if ( bdb->bi_attrs[ i ]->ai_indexmask & BDB_INDEX_DELETING
283 || bdb->bi_attrs[ i ]->ai_newmask == 0 )
287 bdb->bi_attrs[ i ]->ai_indexmask = bdb->bi_attrs[ i ]->ai_newmask;
288 bdb->bi_attrs[ i ]->ai_newmask = 0;
291 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
292 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
293 bdb->bi_index_task = NULL;
294 ldap_pvt_runqueue_remove( &slapd_rq, rtask );
295 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
300 /* Cleanup loose ends after Modify completes */
302 bdb_cf_cleanup( ConfigArgs *c )
304 struct bdb_info *bdb = c->be->be_private;
307 if ( bdb->bi_flags & BDB_UPD_CONFIG ) {
308 if ( bdb->bi_db_config ) {
310 FILE *f = fopen( bdb->bi_db_config_path, "w" );
312 for (i=0; bdb->bi_db_config[i].bv_val; i++)
313 fprintf( f, "%s\n", bdb->bi_db_config[i].bv_val );
317 unlink( bdb->bi_db_config_path );
319 bdb->bi_flags ^= BDB_UPD_CONFIG;
322 if ( bdb->bi_flags & BDB_DEL_INDEX ) {
323 bdb_attr_flush( bdb );
324 bdb->bi_flags ^= BDB_DEL_INDEX;
327 if ( bdb->bi_flags & BDB_RE_OPEN ) {
328 bdb->bi_flags ^= BDB_RE_OPEN;
329 rc = c->be->bd_info->bi_db_close( c->be, NULL );
331 rc = c->be->bd_info->bi_db_open( c->be, NULL );
332 /* If this fails, we need to restart */
335 snprintf( c->cr_msg, sizeof( c->cr_msg ),
336 "failed to reopen database, rc=%d", rc );
337 Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(bdb_cf_cleanup)
338 ": %s\n", c->cr_msg, 0, 0 );
346 bdb_cf_gen( ConfigArgs *c )
348 struct bdb_info *bdb = c->be->be_private;
351 if ( c->op == SLAP_CONFIG_EMIT ) {
355 if ( bdb->bi_txn_cp ) {
358 bv.bv_len = sprintf( buf, "%d %d", bdb->bi_txn_cp_kbyte,
359 bdb->bi_txn_cp_min );
361 value_add_one( &c->rvalue_vals, &bv );
368 if ( bdb->bi_dbenv_home ) {
369 c->value_string = ch_strdup( bdb->bi_dbenv_home );
376 if ( !( bdb->bi_flags & BDB_IS_OPEN )
377 && !bdb->bi_db_config )
379 char buf[SLAP_TEXT_BUFLEN];
380 FILE *f = fopen( bdb->bi_db_config_path, "r" );
384 bdb->bi_flags |= BDB_HAS_CONFIG;
385 while ( fgets( buf, sizeof(buf), f )) {
386 ber_str2bv( buf, 0, 1, &bv );
387 if ( bv.bv_len > 0 && bv.bv_val[bv.bv_len-1] == '\n' ) {
389 bv.bv_val[bv.bv_len] = '\0';
391 /* shouldn't need this, but ... */
392 if ( bv.bv_len > 0 && bv.bv_val[bv.bv_len-1] == '\r' ) {
394 bv.bv_val[bv.bv_len] = '\0';
396 ber_bvarray_add( &bdb->bi_db_config, &bv );
401 if ( bdb->bi_db_config ) {
406 for (i=0; !BER_BVISNULL(&bdb->bi_db_config[i]); i++) {
407 bv.bv_len = sprintf( bv.bv_val, "{%d}%s", i,
408 bdb->bi_db_config[i].bv_val );
409 value_add_one( &c->rvalue_vals, &bv );
412 if ( !c->rvalue_vals ) rc = 1;
416 if ( bdb->bi_dbenv_xflags & DB_TXN_NOSYNC )
421 bdb_attr_index_unparse( bdb, &c->rvalue_vals );
422 if ( !c->rvalue_vals ) rc = 1;
427 if ( bdb->bi_lock_detect != DB_LOCK_DEFAULT ) {
429 for (i=0; !BER_BVISNULL(&bdb_lockd[i].word); i++) {
430 if ( bdb->bi_lock_detect == bdb_lockd[i].mask ) {
431 value_add_one( &c->rvalue_vals, &bdb_lockd[i].word );
440 c->value_int = bdb->bi_search_stack_depth;
444 } else if ( c->op == LDAP_MOD_DELETE ) {
447 /* single-valued no-ops */
453 if ( bdb->bi_txn_cp_task ) {
454 struct re_s *re = bdb->bi_txn_cp_task;
455 bdb->bi_txn_cp_task = NULL;
456 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ) )
457 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
458 ldap_pvt_runqueue_remove( &slapd_rq, re );
464 ber_bvarray_free( bdb->bi_db_config );
465 bdb->bi_db_config = NULL;
468 ch_free( bdb->bi_db_config[i].bv_val );
469 for (; bdb->bi_db_config[i].bv_val; i++)
470 bdb->bi_db_config[i] = bdb->bi_db_config[i+1];
472 bdb->bi_flags |= BDB_UPD_CONFIG;
473 c->cleanup = bdb_cf_cleanup;
476 bdb->bi_flags |= BDB_RE_OPEN;
477 bdb->bi_flags ^= BDB_HAS_CONFIG;
478 ch_free( bdb->bi_dbenv_home );
479 bdb->bi_dbenv_home = NULL;
480 ch_free( bdb->bi_db_config_path );
481 bdb->bi_db_config_path = NULL;
482 c->cleanup = bdb_cf_cleanup;
483 ldap_pvt_thread_pool_purgekey( bdb->bi_dbenv );
486 bdb->bi_dbenv->set_flags( bdb->bi_dbenv, DB_TXN_NOSYNC, 0 );
489 if ( c->valx == -1 ) {
492 /* delete all (FIXME) */
493 for ( i = 0; i < bdb->bi_nattrs; i++ ) {
494 bdb->bi_attrs[i]->ai_indexmask |= BDB_INDEX_DELETING;
496 bdb->bi_flags |= BDB_DEL_INDEX;
497 c->cleanup = bdb_cf_cleanup;
500 struct berval bv, def = BER_BVC("default");
503 for (ptr = c->line; !isspace( (unsigned char) *ptr ); ptr++);
506 bv.bv_len = ptr - bv.bv_val;
507 if ( bvmatch( &bv, &def )) {
508 bdb->bi_defaultmask = 0;
515 sep = bv.bv_val[ bv.bv_len ];
516 bv.bv_val[ bv.bv_len ] = '\0';
517 attrs = ldap_str2charray( bv.bv_val, "," );
519 for ( i = 0; attrs[ i ]; i++ ) {
520 AttributeDescription *ad = NULL;
524 slap_str2ad( attrs[ i ], &ad, &text );
525 /* if we got here... */
526 assert( ad != NULL );
528 ai = bdb_attr_mask( bdb, ad );
529 /* if we got here... */
530 assert( ai != NULL );
532 ai->ai_indexmask |= BDB_INDEX_DELETING;
533 bdb->bi_flags |= BDB_DEL_INDEX;
534 c->cleanup = bdb_cf_cleanup;
537 bv.bv_val[ bv.bv_len ] = sep;
538 ldap_charray_free( attrs );
550 if ( lutil_atolx( &l, c->argv[1], 0 ) != 0 ) {
551 fprintf( stderr, "%s: "
552 "invalid kbyte \"%s\" in \"checkpoint\".\n",
553 c->log, c->argv[1] );
556 bdb->bi_txn_cp_kbyte = l;
557 if ( lutil_atolx( &l, c->argv[2], 0 ) != 0 ) {
558 fprintf( stderr, "%s: "
559 "invalid minutes \"%s\" in \"checkpoint\".\n",
560 c->log, c->argv[2] );
563 bdb->bi_txn_cp_min = l;
564 /* If we're in server mode and time-based checkpointing is enabled,
565 * submit a task to perform periodic checkpoints.
567 if ((slapMode & SLAP_SERVER_MODE) && bdb->bi_txn_cp_min ) {
568 struct re_s *re = bdb->bi_txn_cp_task;
570 re->interval.tv_sec = bdb->bi_txn_cp_min * 60;
572 if ( c->be->be_suffix == NULL || BER_BVISNULL( &c->be->be_suffix[0] ) ) {
573 fprintf( stderr, "%s: "
574 "\"checkpoint\" must occur after \"suffix\".\n",
578 bdb->bi_txn_cp_task = ldap_pvt_runqueue_insert( &slapd_rq,
579 bdb->bi_txn_cp_min * 60, bdb_checkpoint, bdb,
580 LDAP_XSTRING(bdb_checkpoint), c->be->be_suffix[0].bv_val );
589 if ( c->op == SLAP_CONFIG_ADD ) {
590 ptr += STRLENOF("dbconfig");
591 while (!isspace((unsigned char)*ptr)) ptr++;
592 while (isspace((unsigned char)*ptr)) ptr++;
595 if ( bdb->bi_flags & BDB_IS_OPEN ) {
596 bdb->bi_flags |= BDB_UPD_CONFIG;
597 c->cleanup = bdb_cf_cleanup;
599 /* If we're just starting up...
602 /* If a DB_CONFIG file exists, or we don't know the path
603 * to the DB_CONFIG file, ignore these directives
605 if (( bdb->bi_flags & BDB_HAS_CONFIG ) || !bdb->bi_db_config_path )
607 f = fopen( bdb->bi_db_config_path, "a" );
609 /* FIXME: EBCDIC probably needs special handling */
610 fprintf( f, "%s\n", ptr );
614 ber_str2bv( ptr, 0, 1, &bv );
615 ber_bvarray_add( &bdb->bi_db_config, &bv );
619 case BDB_DIRECTORY: {
621 char *ptr, *testpath;
624 len = strlen( c->value_string );
625 testpath = ch_malloc( len + STRLENOF(LDAP_DIRSEP) + STRLENOF("DUMMY") + 1 );
626 ptr = lutil_strcopy( testpath, c->value_string );
627 *ptr++ = LDAP_DIRSEP[0];
628 strcpy( ptr, "DUMMY" );
629 f = fopen( testpath, "w" );
636 snprintf( c->cr_msg, sizeof( c->cr_msg ), "%s: invalid path: %s",
637 c->log, strerror( errno ));
638 Debug( LDAP_DEBUG_ANY, "%s\n", c->cr_msg, 0, 0 );
642 if ( bdb->bi_dbenv_home )
643 ch_free( bdb->bi_dbenv_home );
644 bdb->bi_dbenv_home = c->value_string;
646 /* See if a DB_CONFIG file already exists here */
647 if ( bdb->bi_db_config_path )
648 ch_free( bdb->bi_db_config_path );
649 bdb->bi_db_config_path = ch_malloc( len +
650 STRLENOF(LDAP_DIRSEP) + STRLENOF("DB_CONFIG") + 1 );
651 ptr = lutil_strcopy( bdb->bi_db_config_path, bdb->bi_dbenv_home );
652 *ptr++ = LDAP_DIRSEP[0];
653 strcpy( ptr, "DB_CONFIG" );
655 f = fopen( bdb->bi_db_config_path, "r" );
657 bdb->bi_flags |= BDB_HAS_CONFIG;
665 bdb->bi_dbenv_xflags |= DB_TXN_NOSYNC;
667 bdb->bi_dbenv_xflags &= ~DB_TXN_NOSYNC;
668 if ( bdb->bi_flags & BDB_IS_OPEN ) {
669 bdb->bi_dbenv->set_flags( bdb->bi_dbenv, DB_TXN_NOSYNC,
675 rc = bdb_attr_index_config( bdb, c->fname, c->lineno,
676 c->argc - 1, &c->argv[1] );
678 if( rc != LDAP_SUCCESS ) return 1;
679 if (( bdb->bi_flags & BDB_IS_OPEN ) && !bdb->bi_index_task ) {
680 /* Start the task as soon as we finish here. Set a long
681 * interval (10 hours) so that it only gets scheduled once.
683 if ( c->be->be_suffix == NULL || BER_BVISNULL( &c->be->be_suffix[0] ) ) {
684 fprintf( stderr, "%s: "
685 "\"index\" must occur after \"suffix\".\n",
689 bdb->bi_index_task = ldap_pvt_runqueue_insert( &slapd_rq, 36000,
690 bdb_online_index, c->be,
691 LDAP_XSTRING(bdb_online_index), c->be->be_suffix[0].bv_val );
696 rc = verb_to_mask( c->argv[1], bdb_lockd );
697 if ( BER_BVISNULL(&bdb_lockd[rc].word) ) {
698 fprintf( stderr, "%s: "
699 "bad policy (%s) in \"lockDetect <policy>\" line\n",
700 c->log, c->argv[1] );
703 bdb->bi_lock_detect = rc;
707 if ( c->value_int < MINIMUM_SEARCH_STACK_DEPTH ) {
709 "%s: depth %d too small, using %d\n",
710 c->log, c->value_int, MINIMUM_SEARCH_STACK_DEPTH );
711 c->value_int = MINIMUM_SEARCH_STACK_DEPTH;
713 bdb->bi_search_stack_depth = c->value_int;
719 int bdb_back_init_cf( BackendInfo *bi )
722 bi->bi_cf_ocs = bdbocs;
724 rc = config_register_schema( bdbcfg, bdbocs );