]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/config.c
91dcad1074d0b5ed2f1069126db96e841d0d0151
[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                 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, &txn, bdb->bi_db_opflags );
190                 if ( rc ) 
191                         break;
192                 locker = TXN_ID( txn );
193                 if ( getnext ) {
194                         getnext = 0;
195                         BDB_ID2DISK( id, &nid );
196                         rc = bdb->bi_id2entry->bdi_db->cursor(
197                                 bdb->bi_id2entry->bdi_db, txn, &curs, bdb->bi_db_opflags );
198                         if ( rc ) {
199                                 TXN_ABORT( txn );
200                                 break;
201                         }
202                         rc = curs->c_get( curs, &key, &data, DB_SET_RANGE );
203                         curs->c_close( curs );
204                         if ( rc ) {
205                                 TXN_ABORT( txn );
206                                 if ( rc == DB_NOTFOUND )
207                                         rc = 0;
208                                 if ( rc == DB_LOCK_DEADLOCK ) {
209                                         ldap_pvt_thread_yield();
210                                         continue;
211                                 }
212                                 break;
213                         }
214                         BDB_DISK2ID( &nid, &id );
215                 }
216
217                 ei = NULL;
218                 rc = bdb_cache_find_id( op, txn, id, &ei, 0, locker, &lock );
219                 if ( rc ) {
220                         TXN_ABORT( txn );
221                         if ( rc == DB_LOCK_DEADLOCK ) {
222                                 ldap_pvt_thread_yield();
223                                 continue;
224                         }
225                         if ( rc == DB_NOTFOUND ) {
226                                 id++
227                                 getnext = 1;
228                                 continue;
229                         }
230                         break;
231                 }
232                 if ( ei->bei_e ) {
233                         rc = bdb_index_entry( op, txn, BDB_INDEX_UPDATE_OP, ei->bei_e );
234                         if ( rc == DB_LOCK_DEADLOCK ) {
235                                 TXN_ABORT( txn );
236                                 ldap_pvt_thread_yield();
237                                 continue;
238                         }
239                         if ( rc == 0 ) {
240                                 rc = TXN_COMMIT( txn, 0 );
241                                 txn = NULL;
242                         }
243                         if ( rc )
244                                 break;
245                 }
246                 id++;
247                 getnext = 1;
248         }
249 out:
250         ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
251         ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
252         ldap_pvt_runqueue_remove( &slapd_rq, rtask );
253         ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
254
255         return NULL;
256 }
257
258 /* Cleanup loose ends after Modify completes */
259 static int
260 bdb_cf_cleanup( ConfigArgs *c )
261 {
262         struct bdb_info *bdb = c->be->be_private;
263
264         if ( bdb->bi_flags & BDB_UPD_CONFIG ) {
265                 if ( bdb->bi_db_config ) {
266                         int i;
267                         FILE *f = fopen( bdb->bi_db_config_path, "w" );
268                         if ( f ) {
269                                 for (i=0; bdb->bi_db_config[i].bv_val; i++)
270                                         fprintf( f, "%s\n", bdb->bi_db_config[i].bv_val );
271                                 fclose( f );
272                         }
273                 } else {
274                         unlink( bdb->bi_db_config_path );
275                 }
276                 bdb->bi_flags ^= BDB_UPD_CONFIG;
277         }
278
279         if ( bdb->bi_flags & BDB_DEL_INDEX ) {
280                 bdb_attr_flush( bdb );
281                 bdb->bi_flags ^= BDB_DEL_INDEX;
282         }
283
284         return 0;
285 }
286
287 static int
288 bdb_cf_gen(ConfigArgs *c)
289 {
290         struct bdb_info *bdb = c->be->be_private;
291         int rc;
292
293         if ( c->op == SLAP_CONFIG_EMIT ) {
294                 rc = 0;
295                 switch( c->type ) {
296                 case BDB_CHKPT:
297                         if (bdb->bi_txn_cp ) {
298                                 char buf[64];
299                                 struct berval bv;
300                                 bv.bv_len = sprintf( buf, "%d %d", bdb->bi_txn_cp_kbyte,
301                                         bdb->bi_txn_cp_min );
302                                 bv.bv_val = buf;
303                                 value_add_one( &c->rvalue_vals, &bv );
304                         } else{
305                                 rc = 1;
306                         }
307                         break;
308
309                 case BDB_DIRECTORY:
310                         if ( bdb->bi_dbenv_home ) {
311                                 c->value_string = ch_strdup( bdb->bi_dbenv_home );
312                         } else {
313                                 rc = 1;
314                         }
315                         break;
316
317                 case BDB_CONFIG:
318                         if ( bdb->bi_db_config ) {
319                                 int i;
320                                 struct berval bv;
321
322                                 bv.bv_val = c->log;
323                                 for (i=0; !BER_BVISNULL(&bdb->bi_db_config[i]); i++) {
324                                         bv.bv_len = sprintf( bv.bv_val, "{%d}%s", i,
325                                                 bdb->bi_db_config[i].bv_val );
326                                         value_add_one( &c->rvalue_vals, &bv );
327                                 }
328                         }
329                         if ( !c->rvalue_vals ) rc = 1;
330                         break;
331
332                 case BDB_NOSYNC:
333                         if ( bdb->bi_dbenv_xflags & DB_TXN_NOSYNC )
334                                 c->value_int = 1;
335                         break;
336                         
337                 case BDB_INDEX:
338                         bdb_attr_index_unparse( bdb, &c->rvalue_vals );
339                         if ( !c->rvalue_vals ) rc = 1;
340                         break;
341
342                 case BDB_LOCKD:
343                         rc = 1;
344                         if ( bdb->bi_lock_detect != DB_LOCK_DEFAULT ) {
345                                 int i;
346                                 for (i=0; !BER_BVISNULL(&bdb_lockd[i].word); i++) {
347                                         if ( bdb->bi_lock_detect == bdb_lockd[i].mask ) {
348                                                 value_add_one( &c->rvalue_vals, &bdb_lockd[i].word );
349                                                 rc = 0;
350                                                 break;
351                                         }
352                                 }
353                         }
354                         break;
355
356                 case BDB_SSTACK:
357                         c->value_int = bdb->bi_search_stack_depth;
358                         break;
359                 }
360                 return rc;
361         } else if ( c->op == LDAP_MOD_DELETE ) {
362                 rc = 0;
363                 switch( c->type ) {
364                 /* single-valued no-ops */
365                 case BDB_LOCKD:
366                 case BDB_SSTACK:
367                         break;
368
369                 case BDB_CHKPT:
370                         /* FIXME: should stop the checkpoint task too */
371                         bdb->bi_txn_cp = 0;
372                         break;
373                 case BDB_CONFIG:
374                         if ( c->valx < 0 ) {
375                                 ber_bvarray_free( bdb->bi_db_config );
376                                 bdb->bi_db_config = NULL;
377                         } else {
378                                 int i = c->valx;
379                                 ch_free( bdb->bi_db_config[i].bv_val );
380                                 for (; bdb->bi_db_config[i].bv_val; i++)
381                                         bdb->bi_db_config[i] = bdb->bi_db_config[i+1];
382                         }
383                         bdb->bi_flags |= BDB_UPD_CONFIG;
384                         c->cleanup = bdb_cf_cleanup;
385                         break;
386                 case BDB_DIRECTORY:
387                         rc = 1;
388                         /* FIXME: what does this mean? */
389                         break;
390                 case BDB_NOSYNC:
391                         bdb->bi_dbenv->set_flags( bdb->bi_dbenv, DB_TXN_NOSYNC, 0 );
392                         break;
393                 case BDB_INDEX: {
394                         AttributeDescription *ad = NULL;
395                         struct berval bv, def = BER_BVC("default");
396                         char *ptr;
397                         const char *text;
398                         for (ptr = c->line; !isspace( *ptr ); ptr++);
399                         bv.bv_val = c->line;
400                         bv.bv_len = ptr - bv.bv_val;
401                         if ( bvmatch( &bv, &def )) {
402                                 bdb->bi_defaultmask = 0;
403                         } else {
404                                 slap_bv2ad( &bv, &ad, &text );
405                                 if ( ad ) {
406                                         AttrInfo *ai = bdb_attr_mask( bdb, ad );
407                                         ai->ai_indexmask |= BDB_INDEX_DELETING;
408                                         bdb->bi_flags |= BDB_DEL_INDEX;
409                                         c->cleanup = bdb_cf_cleanup;
410                                 }
411                         }
412                         }
413                         break;
414                 }
415                 return rc;
416         }
417
418         switch( c->type ) {
419         case BDB_CHKPT:
420                 bdb->bi_txn_cp = 1;
421                 bdb->bi_txn_cp_kbyte = strtol( c->argv[1], NULL, 0 );
422                 bdb->bi_txn_cp_min = strtol( c->argv[2], NULL, 0 );
423                 break;
424
425         case BDB_CONFIG: {
426                 char *ptr = c->line + STRLENOF("dbconfig");
427                 struct berval bv;
428                 while (!isspace(*ptr)) ptr++;
429                 while (isspace(*ptr)) ptr++;
430                 
431                 if ( bdb->bi_flags & BDB_IS_OPEN ) {
432                         bdb->bi_flags |= BDB_UPD_CONFIG;
433                         c->cleanup = bdb_cf_cleanup;
434                 } else {
435                 /* If we're just starting up...
436                  */
437                         FILE *f;
438                         /* If a DB_CONFIG file exists, or we don't know the path
439                          * to the DB_CONFIG file, ignore these directives
440                          */
441                         if (( bdb->bi_flags & BDB_HAS_CONFIG ) || !bdb->bi_db_config_path )
442                                 break;
443                         f = fopen( bdb->bi_db_config_path, "a" );
444                         if ( f ) {
445                                 /* FIXME: EBCDIC probably needs special handling */
446                                 fprintf( f, "%s\n", ptr );
447                                 fclose( f );
448                         }
449                 }
450                 ber_str2bv( ptr, 0, 1, &bv );
451                 ber_bvarray_add( &bdb->bi_db_config, &bv );
452                 }
453                 break;
454
455         case BDB_DIRECTORY: {
456                 FILE *f;
457                 char *ptr;
458
459                 bdb->bi_dbenv_home = c->value_string;
460
461                 /* See if a DB_CONFIG file already exists here */
462                 bdb->bi_db_config_path = ch_malloc( strlen( bdb->bi_dbenv_home ) +
463                         STRLENOF(LDAP_DIRSEP) + STRLENOF("DB_CONFIG") + 1 );
464                 ptr = lutil_strcopy( bdb->bi_db_config_path, bdb->bi_dbenv_home );
465                 *ptr++ = LDAP_DIRSEP[0];
466                 strcpy( ptr, "DB_CONFIG" );
467
468                 f = fopen( bdb->bi_db_config_path, "r" );
469                 if ( f ) {
470                         bdb->bi_flags |= BDB_HAS_CONFIG;
471                         fclose(f);
472                 }
473                 }
474                 break;
475
476         case BDB_NOSYNC:
477                 if ( c->value_int )
478                         bdb->bi_dbenv_xflags |= DB_TXN_NOSYNC;
479                 else
480                         bdb->bi_dbenv_xflags &= ~DB_TXN_NOSYNC;
481                 if ( bdb->bi_flags & BDB_IS_OPEN ) {
482                         bdb->bi_dbenv->set_flags( bdb->bi_dbenv, DB_TXN_NOSYNC,
483                                 c->value_int );
484                 }
485                 break;
486
487         case BDB_INDEX:
488                 rc = bdb_attr_index_config( bdb, c->fname, c->lineno,
489                         c->argc - 1, &c->argv[1] );
490
491                 if( rc != LDAP_SUCCESS ) return 1;
492                 if ( bdb->bi_flags & BDB_IS_OPEN ) {
493                         /* Start the task as soon as we finish here */
494                         ldap_pvt_runqueue_insert( &slapd_rq, 60,
495                                 bdb_online_index, c->be );
496                 }
497                 break;
498
499         case BDB_LOCKD:
500                 rc = verb_to_mask( c->argv[1], bdb_lockd );
501                 if ( BER_BVISNULL(&bdb_lockd[rc].word) ) {
502                         fprintf( stderr, "%s: "
503                                 "bad policy (%s) in \"lockDetect <policy>\" line\n",
504                                 c->log, c->argv[1] );
505                         return 1;
506                 }
507                 bdb->bi_lock_detect = rc;
508                 break;
509
510         case BDB_SSTACK:
511                 if ( c->value_int < MINIMUM_SEARCH_STACK_DEPTH ) {
512                         fprintf( stderr,
513                 "%s: depth %d too small, using %d\n",
514                         c->log, c->value_int, MINIMUM_SEARCH_STACK_DEPTH );
515                         c->value_int = MINIMUM_SEARCH_STACK_DEPTH;
516                 }
517                 bdb->bi_search_stack_depth = c->value_int;
518                 break;
519         }
520         return 0;
521 }
522
523 int bdb_back_init_cf( BackendInfo *bi )
524 {
525         int rc;
526         bi->bi_cf_table = bdbcfg;
527
528         rc = config_register_schema( bdbcfg, bdbocs );
529         if ( rc ) return rc;
530         bdbcfg[0].ad = slap_schema.si_ad_objectClass;
531         return 0;
532 }