]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/config.c
776545de1b11f415232a08599bd060597e8f78ce
[openldap] / servers / slapd / back-bdb / config.c
1 /* config.c - bdb backend configuration file routine */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2000-2005 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
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>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20 #include <ac/string.h>
21
22 #include "back-bdb.h"
23
24 #include "config.h"
25
26 #include "lutil.h"
27 #include "ldap_rq.h"
28
29 #ifdef DB_DIRTY_READ
30 #       define  SLAP_BDB_ALLOW_DIRTY_READ
31 #endif
32
33 static ObjectClass *bdb_oc;
34
35 static ConfigDriver bdb_cf_oc, bdb_cf_gen;
36
37 enum {
38         BDB_CHKPT = 1,
39         BDB_CONFIG,
40         BDB_DIRECTORY,
41         BDB_NOSYNC,
42         BDB_DIRTYR,
43         BDB_INDEX,
44         BDB_LOCKD,
45         BDB_SSTACK
46 };
47
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,
76 #else
77                 ARG_IGNORED, NULL,
78 #endif
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 }
117 };
118
119 static ConfigOCs bdbocs[] = {
120         { "( OLcfgDbOc:1.1 "
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 },
130         { NULL, 0, NULL }
131 };
132
133 static int
134 bdb_cf_oc(ConfigArgs *c)
135 {
136         if ( c->op == SLAP_CONFIG_EMIT ) {
137                 value_add_one( &c->rvalue_vals, &bdb_oc->soc_cname );
138                 return 0;
139         }
140         return 1;
141 }
142
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 },
149         { BER_BVNULL, 0 }
150 };
151
152 /* reindex entries on the fly */
153 static void *
154 bdb_online_index( void *ctx, void *arg )
155 {
156         struct re_s *rtask = arg;
157         BackendDB *be = rtask->arg;
158         struct bdb_info *bdb = be->be_private;
159
160         Connection conn = {0};
161         char opbuf[OPERATION_BUFFER_SIZE];
162         Operation *op = (Operation *)opbuf;
163
164         DBC *curs;
165         DBT key, data;
166         DB_TXN *txn;
167         DB_LOCK lock;
168         u_int32_t locker;
169         ID id, nid;
170         EntryInfo *ei;
171         int rc, getnext = 1;
172
173         connection_fake_init( &conn, op, ctx );
174
175         op->o_bd = be;
176
177         DBTzero( &key );
178         DBTzero( &data );
179         
180         id = 1;
181         key.data = &nid;
182         key.size = key.ulen = sizeof(ID);
183         key.flags = DB_DBT_USERMEM;
184
185         data.flags = DB_DBT_USERMEM | DB_DBT_PARTIAL;
186         data.dlen = data.ulen = 0;
187
188         while ( 1 ) {
189                 if ( slapd_shutdown )
190                         break;
191
192                 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &txn, bdb->bi_db_opflags );
193                 if ( rc ) 
194                         break;
195                 locker = TXN_ID( txn );
196                 if ( getnext ) {
197                         getnext = 0;
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 );
201                         if ( rc ) {
202                                 TXN_ABORT( txn );
203                                 break;
204                         }
205                         rc = curs->c_get( curs, &key, &data, DB_SET_RANGE );
206                         curs->c_close( curs );
207                         if ( rc ) {
208                                 TXN_ABORT( txn );
209                                 if ( rc == DB_NOTFOUND )
210                                         rc = 0;
211                                 if ( rc == DB_LOCK_DEADLOCK ) {
212                                         ldap_pvt_thread_yield();
213                                         continue;
214                                 }
215                                 break;
216                         }
217                         BDB_DISK2ID( &nid, &id );
218                 }
219
220                 ei = NULL;
221                 rc = bdb_cache_find_id( op, txn, id, &ei, 0, locker, &lock );
222                 if ( rc ) {
223                         TXN_ABORT( txn );
224                         if ( rc == DB_LOCK_DEADLOCK ) {
225                                 ldap_pvt_thread_yield();
226                                 continue;
227                         }
228                         if ( rc == DB_NOTFOUND ) {
229                                 id++;
230                                 getnext = 1;
231                                 continue;
232                         }
233                         break;
234                 }
235                 if ( ei->bei_e ) {
236                         rc = bdb_index_entry( op, txn, BDB_INDEX_UPDATE_OP, ei->bei_e );
237                         if ( rc == DB_LOCK_DEADLOCK ) {
238                                 TXN_ABORT( txn );
239                                 ldap_pvt_thread_yield();
240                                 continue;
241                         }
242                         if ( rc == 0 ) {
243                                 rc = TXN_COMMIT( txn, 0 );
244                                 txn = NULL;
245                         }
246                         if ( rc )
247                                 break;
248                 }
249                 id++;
250                 getnext = 1;
251         }
252 out:
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 );
257
258         return NULL;
259 }
260
261 /* Cleanup loose ends after Modify completes */
262 static int
263 bdb_cf_cleanup( ConfigArgs *c )
264 {
265         struct bdb_info *bdb = c->be->be_private;
266
267         if ( bdb->bi_flags & BDB_UPD_CONFIG ) {
268                 if ( bdb->bi_db_config ) {
269                         int i;
270                         FILE *f = fopen( bdb->bi_db_config_path, "w" );
271                         if ( f ) {
272                                 for (i=0; bdb->bi_db_config[i].bv_val; i++)
273                                         fprintf( f, "%s\n", bdb->bi_db_config[i].bv_val );
274                                 fclose( f );
275                         }
276                 } else {
277                         unlink( bdb->bi_db_config_path );
278                 }
279                 bdb->bi_flags ^= BDB_UPD_CONFIG;
280         }
281
282         if ( bdb->bi_flags & BDB_DEL_INDEX ) {
283                 bdb_attr_flush( bdb );
284                 bdb->bi_flags ^= BDB_DEL_INDEX;
285         }
286
287         return 0;
288 }
289
290 static int
291 bdb_cf_gen(ConfigArgs *c)
292 {
293         struct bdb_info *bdb = c->be->be_private;
294         int rc;
295
296         if ( c->op == SLAP_CONFIG_EMIT ) {
297                 rc = 0;
298                 switch( c->type ) {
299                 case BDB_CHKPT:
300                         if (bdb->bi_txn_cp ) {
301                                 char buf[64];
302                                 struct berval bv;
303                                 bv.bv_len = sprintf( buf, "%d %d", bdb->bi_txn_cp_kbyte,
304                                         bdb->bi_txn_cp_min );
305                                 bv.bv_val = buf;
306                                 value_add_one( &c->rvalue_vals, &bv );
307                         } else{
308                                 rc = 1;
309                         }
310                         break;
311
312                 case BDB_DIRECTORY:
313                         if ( bdb->bi_dbenv_home ) {
314                                 c->value_string = ch_strdup( bdb->bi_dbenv_home );
315                         } else {
316                                 rc = 1;
317                         }
318                         break;
319
320                 case BDB_CONFIG:
321                         if ( bdb->bi_db_config ) {
322                                 int i;
323                                 struct berval bv;
324
325                                 bv.bv_val = c->log;
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 );
330                                 }
331                         }
332                         if ( !c->rvalue_vals ) rc = 1;
333                         break;
334
335                 case BDB_NOSYNC:
336                         if ( bdb->bi_dbenv_xflags & DB_TXN_NOSYNC )
337                                 c->value_int = 1;
338                         break;
339                         
340                 case BDB_INDEX:
341                         bdb_attr_index_unparse( bdb, &c->rvalue_vals );
342                         if ( !c->rvalue_vals ) rc = 1;
343                         break;
344
345                 case BDB_LOCKD:
346                         rc = 1;
347                         if ( bdb->bi_lock_detect != DB_LOCK_DEFAULT ) {
348                                 int i;
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 );
352                                                 rc = 0;
353                                                 break;
354                                         }
355                                 }
356                         }
357                         break;
358
359                 case BDB_SSTACK:
360                         c->value_int = bdb->bi_search_stack_depth;
361                         break;
362                 }
363                 return rc;
364         } else if ( c->op == LDAP_MOD_DELETE ) {
365                 rc = 0;
366                 switch( c->type ) {
367                 /* single-valued no-ops */
368                 case BDB_LOCKD:
369                 case BDB_SSTACK:
370                         break;
371
372                 case BDB_CHKPT:
373                         /* FIXME: should stop the checkpoint task too */
374                         bdb->bi_txn_cp = 0;
375                         break;
376                 case BDB_CONFIG:
377                         if ( c->valx < 0 ) {
378                                 ber_bvarray_free( bdb->bi_db_config );
379                                 bdb->bi_db_config = NULL;
380                         } else {
381                                 int i = c->valx;
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];
385                         }
386                         bdb->bi_flags |= BDB_UPD_CONFIG;
387                         c->cleanup = bdb_cf_cleanup;
388                         break;
389                 case BDB_DIRECTORY:
390                         rc = 1;
391                         /* FIXME: what does this mean? */
392                         break;
393                 case BDB_NOSYNC:
394                         bdb->bi_dbenv->set_flags( bdb->bi_dbenv, DB_TXN_NOSYNC, 0 );
395                         break;
396                 case BDB_INDEX: {
397                         AttributeDescription *ad = NULL;
398                         struct berval bv, def = BER_BVC("default");
399                         char *ptr;
400                         const char *text;
401                         for (ptr = c->line; !isspace( *ptr ); ptr++);
402                         bv.bv_val = c->line;
403                         bv.bv_len = ptr - bv.bv_val;
404                         if ( bvmatch( &bv, &def )) {
405                                 bdb->bi_defaultmask = 0;
406                         } else {
407                                 slap_bv2ad( &bv, &ad, &text );
408                                 if ( ad ) {
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;
413                                 }
414                         }
415                         }
416                         break;
417                 }
418                 return rc;
419         }
420
421         switch( c->type ) {
422         case BDB_CHKPT:
423                 bdb->bi_txn_cp = 1;
424                 bdb->bi_txn_cp_kbyte = strtol( c->argv[1], NULL, 0 );
425                 bdb->bi_txn_cp_min = strtol( c->argv[2], NULL, 0 );
426                 break;
427
428         case BDB_CONFIG: {
429                 char *ptr = c->line + STRLENOF("dbconfig");
430                 struct berval bv;
431                 while (!isspace(*ptr)) ptr++;
432                 while (isspace(*ptr)) ptr++;
433                 
434                 if ( bdb->bi_flags & BDB_IS_OPEN ) {
435                         bdb->bi_flags |= BDB_UPD_CONFIG;
436                         c->cleanup = bdb_cf_cleanup;
437                 } else {
438                 /* If we're just starting up...
439                  */
440                         FILE *f;
441                         /* If a DB_CONFIG file exists, or we don't know the path
442                          * to the DB_CONFIG file, ignore these directives
443                          */
444                         if (( bdb->bi_flags & BDB_HAS_CONFIG ) || !bdb->bi_db_config_path )
445                                 break;
446                         f = fopen( bdb->bi_db_config_path, "a" );
447                         if ( f ) {
448                                 /* FIXME: EBCDIC probably needs special handling */
449                                 fprintf( f, "%s\n", ptr );
450                                 fclose( f );
451                         }
452                 }
453                 ber_str2bv( ptr, 0, 1, &bv );
454                 ber_bvarray_add( &bdb->bi_db_config, &bv );
455                 }
456                 break;
457
458         case BDB_DIRECTORY: {
459                 FILE *f;
460                 char *ptr;
461
462                 bdb->bi_dbenv_home = c->value_string;
463
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" );
470
471                 f = fopen( bdb->bi_db_config_path, "r" );
472                 if ( f ) {
473                         bdb->bi_flags |= BDB_HAS_CONFIG;
474                         fclose(f);
475                 }
476                 }
477                 break;
478
479         case BDB_NOSYNC:
480                 if ( c->value_int )
481                         bdb->bi_dbenv_xflags |= DB_TXN_NOSYNC;
482                 else
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,
486                                 c->value_int );
487                 }
488                 break;
489
490         case BDB_INDEX:
491                 rc = bdb_attr_index_config( bdb, c->fname, c->lineno,
492                         c->argc - 1, &c->argv[1] );
493
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 );
499                 }
500                 break;
501
502         case BDB_LOCKD:
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] );
508                         return 1;
509                 }
510                 bdb->bi_lock_detect = rc;
511                 break;
512
513         case BDB_SSTACK:
514                 if ( c->value_int < MINIMUM_SEARCH_STACK_DEPTH ) {
515                         fprintf( stderr,
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;
519                 }
520                 bdb->bi_search_stack_depth = c->value_int;
521                 break;
522         }
523         return 0;
524 }
525
526 int bdb_back_init_cf( BackendInfo *bi )
527 {
528         int rc;
529         bi->bi_cf_table = bdbcfg;
530
531         rc = config_register_schema( bdbcfg, bdbocs );
532         if ( rc ) return rc;
533         bdbcfg[0].ad = slap_schema.si_ad_objectClass;
534         return 0;
535 }