]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/init.c
8563b3ee83a88388177fda06395349a6d4c5835e
[openldap] / servers / slapd / back-bdb / init.c
1 /* init.c - initialize bdb backend */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11 #include <ac/string.h>
12 #include <ac/unistd.h>
13 #include <ac/stdlib.h>
14
15 #include <lutil.h>
16
17 #include "back-bdb.h"
18 #include "external.h"
19
20 static struct bdbi_database {
21         char *file;
22         char *name;
23         int type;
24         int flags;
25 } bdbi_databases[] = {
26         { "id2entry" BDB_SUFFIX, "id2entry", DB_BTREE, 0 },
27 #ifdef BDB_HIER
28         { "id2parent" BDB_SUFFIX, "id2parent", DB_BTREE, 0 },
29 #else
30         { "dn2id" BDB_SUFFIX, "dn2id", DB_BTREE, 0 },
31 #endif
32         { NULL, NULL, 0, 0 }
33 };
34
35 struct berval bdb_uuid = { 0, NULL };
36
37 static int
38 bdb_open( BackendInfo *bi )
39 {
40         return 0;
41 }
42
43 #if 0
44 static int
45 bdb_destroy( BackendInfo *bi )
46 {
47         return 0;
48 }
49
50 static int
51 bdb_close( BackendInfo *bi )
52 {
53         /* terminate the underlying database system */
54         return 0;
55 }
56 #endif
57
58 static int
59 bdb_db_init( BackendDB *be )
60 {
61         struct bdb_info *bdb;
62
63 #ifdef NEW_LOGGING
64         LDAP_LOG(( "init", LDAP_LEVEL_ENTRY, "bdb_db_init" ));
65 #else
66         Debug( LDAP_DEBUG_ANY,
67                 "bdb_db_init: Initializing BDB database\n",
68                 0, 0, 0 );
69 #endif
70
71         /* indicate system schema supported */
72         be->be_flags |=
73 #ifdef BDB_SUBENTRIES
74                 SLAP_BFLAG_SUBENTRIES |
75 #endif
76 #ifdef BDB_ALIASES
77                 SLAP_BFLAG_ALIASES |
78 #endif
79                 SLAP_BFLAG_REFERRALS;
80
81         /* allocate backend-database-specific stuff */
82         bdb = (struct bdb_info *) ch_calloc( 1, sizeof(struct bdb_info) );
83
84         /* DBEnv parameters */
85         bdb->bi_dbenv_home = ch_strdup( SLAPD_DEFAULT_DB_DIR );
86         bdb->bi_dbenv_xflags = 0;
87         bdb->bi_dbenv_mode = SLAPD_DEFAULT_DB_MODE;
88
89         bdb->bi_cache.c_maxsize = DEFAULT_CACHE_SIZE;
90
91 #ifndef NO_THREADS
92 #if 0
93         bdb->bi_lock_detect = DB_LOCK_NORUN;
94 #else
95         bdb->bi_lock_detect = DB_LOCK_DEFAULT;
96 #endif
97 #endif
98
99         ldap_pvt_thread_mutex_init( &bdb->bi_database_mutex );
100         ldap_pvt_thread_mutex_init( &bdb->bi_lastid_mutex );
101         ldap_pvt_thread_mutex_init( &bdb->bi_cache.lru_mutex );
102         ldap_pvt_thread_rdwr_init ( &bdb->bi_cache.c_rwlock );
103 #ifdef BDB_HIER
104         ldap_pvt_thread_rdwr_init( &bdb->bi_tree_rdwr );
105 #endif
106
107         be->be_private = bdb;
108         return 0;
109 }
110
111 #if 0 /* ifndef NO_THREADS */
112 static void *lock_detect_task( void *arg )
113 {
114         struct bdb_info *bdb = (struct bdb_info *) arg;
115
116         while( bdb->bi_dbenv != NULL ) {
117                 int rc;
118                 int aborted;
119                 sleep( bdb->bi_lock_detect_seconds );
120
121                 rc = LOCK_DETECT( bdb->bi_dbenv, 0,
122                         bdb->bi_lock_detect, &aborted );
123
124                 if( rc != 0 ) {
125                         break;
126                 }
127
128 #ifdef NEW_LOGGING
129                 LDAP_LOG(( "init", LDAP_LEVEL_ERR, "bdb_db_init: aborted %d locks\n", aborted ));
130 #else
131                 Debug( LDAP_DEBUG_ANY,
132                         "bdb_lock_detect: aborted %d locks\n",
133                         aborted, 0, 0 );
134 #endif
135         }
136
137         return NULL;
138 }
139 #endif
140
141 int
142 bdb_bt_compare(
143         DB *db, 
144         const DBT *usrkey,
145         const DBT *curkey
146 )
147 {
148         unsigned char *u, *c;
149         int i;
150
151         u = usrkey->data;
152         c = curkey->data;
153
154 #ifdef WORDS_BIGENDIAN
155         for( i = 0; i < sizeof(ID); i++)
156 #else
157         for( i = sizeof(ID)-1; i >= 0; i--)
158 #endif
159         {
160                 if( u[i] - c[i] )
161                         return u[i] - c[i];
162         }
163         return 0;
164 }
165
166 static int
167 bdb_db_open( BackendDB *be )
168 {
169         int rc, i;
170         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
171         u_int32_t flags;
172
173 #ifdef NEW_LOGGING
174         LDAP_LOG(( "init", LDAP_LEVEL_ARGS, "bdb_db_open: %s\n", be->be_suffix[0].bv_val ));
175 #else
176         Debug( LDAP_DEBUG_ARGS,
177                 "bdb_db_open: %s\n",
178                 be->be_suffix[0].bv_val, 0, 0 );
179 #endif
180
181         /* we should check existance of dbenv_home and db_directory */
182
183         rc = db_env_create( &bdb->bi_dbenv, 0 );
184         if( rc != 0 ) {
185 #ifdef NEW_LOGGING
186                 LDAP_LOG(( "init", LDAP_LEVEL_ERR, "bdb_db_open: db_env_create failed: %s (%d)\n", db_strerror(rc), rc ));
187 #else
188                 Debug( LDAP_DEBUG_ANY,
189                         "bdb_db_open: db_env_create failed: %s (%d)\n",
190                         db_strerror(rc), rc, 0 );
191 #endif
192                 return rc;
193         }
194
195         flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE
196                 | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN | DB_RECOVER;
197
198         bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
199         bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
200 #ifndef NO_THREADS
201         bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
202 #endif
203
204 #ifdef BDB_SUBDIRS
205         {
206                 char dir[MAXPATHLEN];
207                 size_t len = strlen( bdb->bi_dbenv_home );
208
209                 strcpy( dir, bdb->bi_dbenv_home );
210                 strcat( &dir[len], BDB_TMP_SUBDIR );
211                 
212                 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
213                 if( rc != 0 ) {
214 #ifdef NEW_LOGGING
215                         LDAP_LOG(( "init", LDAP_LEVEL_ERR, "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n", dir, db_strerror(rc), rc ));
216 #else
217                         Debug( LDAP_DEBUG_ANY,
218                                 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
219                                 dir, db_strerror(rc), rc );
220 #endif
221                         return rc;
222                 }
223
224                 strcat( &dir[len], BDB_LG_SUBDIR );
225
226                 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
227                 if( rc != 0 ) {
228 #ifdef NEW_LOGGING
229                         LDAP_LOG(( "init", LDAP_LEVEL_ERR, "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n", dir, db_strerror(rc), rc ));
230 #else
231                         Debug( LDAP_DEBUG_ANY,
232                                 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
233                                 dir, db_strerror(rc), rc );
234 #endif
235                         return rc;
236                 }
237
238                 strcat( &dir[len], BDB_DATA_SUBDIR );
239
240                 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
241                 if( rc != 0 ) {
242 #ifdef NEW_LOGGING
243                         LDAP_LOG(( "init", LDAP_LEVEL_ERR, "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n", dir, db_strerror(rc), rc ));
244 #else
245                         Debug( LDAP_DEBUG_ANY,
246                                 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
247                                 dir, db_strerror(rc), rc );
248 #endif
249                         return rc;
250                 }
251         }
252 #endif
253
254 #ifdef NEW_LOGGING
255         LDAP_LOG(( "init", LDAP_LEVEL_DETAIL1, "bdb_db_open: dbenv_open %s\n", bdb->bi_dbenv_home ));
256 #else
257         Debug( LDAP_DEBUG_TRACE,
258                 "bdb_db_open: dbenv_open(%s)\n",
259                 bdb->bi_dbenv_home, 0, 0);
260 #endif
261
262         rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
263                 bdb->bi_dbenv_home,
264                 flags,
265                 bdb->bi_dbenv_mode );
266         if( rc != 0 ) {
267 #ifdef NEW_LOGGING
268                 LDAP_LOG(( "init", LDAP_LEVEL_ERR, "bdb_db_open: dbenv_open failed: %s (%d)\n", db_strerror(rc), rc ));
269 #else
270                 Debug( LDAP_DEBUG_ANY,
271                         "bdb_db_open: dbenv_open failed: %s (%d)\n",
272                         db_strerror(rc), rc, 0 );
273 #endif
274                 return rc;
275         }
276
277         if( bdb->bi_dbenv_xflags != 0 ) {
278                 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
279                         bdb->bi_dbenv_xflags, 1);
280                 if( rc != 0 ) {
281 #ifdef NEW_LOGGING
282                         LDAP_LOG(( "init", LDAP_LEVEL_ERR, "bdb_db_open: dbenv_set_flags failed: %s (%d)\n", db_strerror(rc), rc ));
283 #else
284                         Debug( LDAP_DEBUG_ANY,
285                                 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
286                                 db_strerror(rc), rc, 0 );
287 #endif
288                         return rc;
289                 }
290         }
291
292         flags = DB_THREAD | DB_CREATE | bdb->bi_db_opflags;
293
294         bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
295                 BDB_INDICES * sizeof(struct bdb_db_info *) );
296
297         /* open (and create) main database */
298         for( i = 0; bdbi_databases[i].name; i++ ) {
299                 struct bdb_db_info *db;
300
301                 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
302
303                 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
304                 if( rc != 0 ) {
305 #ifdef NEW_LOGGING
306                         LDAP_LOG(( "init", LDAP_LEVEL_ERR, "bdb_db_open: db_create(%s) failed: %s (%d)\n", bdb->bi_dbenv_home, db_strerror(rc), rc ));
307 #else
308                         Debug( LDAP_DEBUG_ANY,
309                                 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
310                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
311 #endif
312                         return rc;
313                 }
314
315                 if( i == BDB_ID2ENTRY ) {
316                         rc = db->bdi_db->set_bt_compare( db->bdi_db,
317                                 bdb_bt_compare );
318                         rc = db->bdi_db->set_pagesize( db->bdi_db,
319                                 BDB_ID2ENTRY_PAGESIZE );
320                 } else {
321 #ifdef BDB_HIER
322                         rc = db->bdi_db->set_bt_compare( db->bdi_db,
323                                 bdb_bt_compare );
324 #elif defined(BDB_IDL_MULTI)
325                         rc = db->bdi_db->set_flags( db->bdi_db, 
326                                 DB_DUP | DB_DUPSORT );
327                         rc = db->bdi_db->set_dup_compare( db->bdi_db,
328                                 bdb_bt_compare );
329 #endif
330                         rc = db->bdi_db->set_pagesize( db->bdi_db,
331                                 BDB_PAGESIZE );
332                 }
333
334                 rc = db->bdi_db->open( db->bdi_db,
335                         bdbi_databases[i].file,
336                 /*      bdbi_databases[i].name, */ NULL,
337                         bdbi_databases[i].type,
338                         bdbi_databases[i].flags | flags,
339                         bdb->bi_dbenv_mode );
340
341                 if( rc != 0 ) {
342 #ifdef NEW_LOGGING
343                         LDAP_LOG(( "init", LDAP_LEVEL_ERR, "bdb_db_open: db_create(%s) failed: %s (%d)\n", bdb->bi_dbenv_home, db_strerror(rc), rc ));
344 #else
345                         Debug( LDAP_DEBUG_ANY,
346                                 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
347                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
348 #endif
349                         return rc;
350                 }
351
352                 db->bdi_name = bdbi_databases[i].name;
353                 bdb->bi_databases[i] = db;
354         }
355
356         bdb->bi_databases[i] = NULL;
357         bdb->bi_ndatabases = i;
358
359         /* get nextid */
360         rc = bdb_last_id( be, NULL );
361         if( rc != 0 ) {
362 #ifdef NEW_LOGGING
363                         LDAP_LOG(( "init", LDAP_LEVEL_ERR, "bdb_db_open: last_id(%s) failed: %s (%d)\n", bdb->bi_dbenv_home, db_strerror(rc), rc ));
364 #else
365                 Debug( LDAP_DEBUG_ANY,
366                         "bdb_db_open: last_id(%s) failed: %s (%d)\n",
367                         bdb->bi_dbenv_home, db_strerror(rc), rc );
368 #endif
369                 return rc;
370         }
371
372         /* <insert> open (and create) index databases */
373 #ifdef BDB_HIER
374         rc = bdb_build_tree( be );
375 #endif
376
377 #if 0 /* ifndef NO_THREADS */
378         if( bdb->bi_lock_detect != DB_LOCK_NORUN ) {
379                 /* listener as a separate THREAD */
380                 rc = ldap_pvt_thread_create( &bdb->bi_lock_detect_tid,
381                         1, lock_detect_task, bdb );
382         }
383 #endif
384         return 0;
385 }
386
387 static int
388 bdb_db_close( BackendDB *be )
389 {
390         int rc;
391         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
392         struct bdb_db_info *db;
393
394         while( bdb->bi_ndatabases-- ) {
395                 db = bdb->bi_databases[bdb->bi_ndatabases];
396                 rc = db->bdi_db->close( db->bdi_db, 0 );
397                 /* Lower numbered names are not strdup'd */
398                 if( bdb->bi_ndatabases >= BDB_NDB )
399                         free( db->bdi_name );
400                 free( db );
401         }
402         free( bdb->bi_databases );
403         bdb_attr_index_destroy( bdb->bi_attrs );
404
405         bdb_cache_release_all (&bdb->bi_cache);
406
407         return 0;
408 }
409
410 static int
411 bdb_db_destroy( BackendDB *be )
412 {
413         int rc;
414         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
415
416         /* close db environment */
417         if( bdb->bi_dbenv ) {
418                 /* force a checkpoint */
419                 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
420                 if( rc != 0 ) {
421 #ifdef NEW_LOGGING
422                         LDAP_LOG(( "init", LDAP_LEVEL_ERR, "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n", db_strerror(rc), rc ));
423 #else
424                         Debug( LDAP_DEBUG_ANY,
425                                 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
426                                 db_strerror(rc), rc, 0 );
427 #endif
428                 }
429
430                 bdb_cache_release_all (&bdb->bi_cache);
431
432                 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
433                 bdb->bi_dbenv = NULL;
434                 if( rc != 0 ) {
435 #ifdef NEW_LOGGING
436                         LDAP_LOG(( "init", LDAP_LEVEL_ERR, "bdb_db_destroy: close failed: %s (%d)\n", db_strerror(rc), rc ));
437 #else
438                         Debug( LDAP_DEBUG_ANY,
439                                 "bdb_db_destroy: close failed: %s (%d)\n",
440                                 db_strerror(rc), rc, 0 );
441 #endif
442                         return rc;
443                 }
444         }
445
446 #ifdef BDB_HIER
447         ldap_pvt_thread_rdwr_destroy( &bdb->bi_tree_rdwr );
448 #endif
449         ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
450         ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_mutex );
451         ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
452         ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
453
454         return 0;
455 }
456
457 #ifdef SLAPD_BDB_DYNAMIC
458 int back_bdb_LTX_init_module( int argc, char *argv[] ) {
459         BackendInfo bi;
460
461         memset( &bi, '\0', sizeof(bi) );
462         bi.bi_type = "bdb";
463         bi.bi_init = bdb_initialize;
464
465         backend_add( &bi );
466         return 0;
467 }
468 #endif /* SLAPD_BDB_DYNAMIC */
469
470 int
471 bdb_initialize(
472         BackendInfo     *bi
473 )
474 {
475         static char *controls[] = {
476                 LDAP_CONTROL_MANAGEDSAIT,
477 #ifdef LDAP_CONTROL_SUBENTRIES
478                 LDAP_CONTROL_SUBENTRIES,
479 #endif
480 #ifdef LDAP_CONTROL_NOOP
481                 LDAP_CONTROL_NOOP,
482 #endif
483 #ifdef LDAP_CONTROL_VALUESRETURNFILTER
484                 LDAP_CONTROL_VALUESRETURNFILTER,
485 #endif
486                 NULL
487         };
488
489         bi->bi_controls = controls;
490
491         /* initialize the underlying database system */
492 #ifdef NEW_LOGGING
493         LDAP_LOG(( "init", LDAP_LEVEL_ENTRY, "bdb_db_initialize\n" ));
494 #else
495         Debug( LDAP_DEBUG_TRACE, "bdb_open: initialize BDB backend\n",
496                 0, 0, 0 );
497 #endif
498
499         {       /* version check */
500                 int major, minor, patch;
501                 char *version = db_version( &major, &minor, &patch );
502
503                 if( major != DB_VERSION_MAJOR ||
504                         minor != DB_VERSION_MINOR ||
505                         patch < DB_VERSION_PATCH )
506                 {
507 #ifdef NEW_LOGGING
508                         LDAP_LOG(( "init", LDAP_LEVEL_ERR, "bdb_db_initialize: version mismatch: \texpected: %s \tgot: %s\n", DB_VERSION_STRING, version ));
509 #else
510                         Debug( LDAP_DEBUG_ANY,
511                                 "bdb_open: version mismatch\n"
512                                 "\texpected: " DB_VERSION_STRING "\n"
513                                 "\tgot: %s \n", version, 0, 0 );
514 #endif
515                 }
516
517 #ifdef NEW_LOGGING
518                 LDAP_LOG(( "init", LDAP_LEVEL_DETAIL1, "bdb_db_initialize: bdb_open: %s\n", version ));
519 #else
520                 Debug( LDAP_DEBUG_ANY, "bdb_open: %s\n",
521                         version, 0, 0 );
522 #endif
523         }
524
525 #if 0
526         db_env_set_func_malloc( ch_malloc );
527         db_env_set_func_realloc( ch_realloc );
528         db_env_set_func_free( ch_free );
529 #endif
530
531         db_env_set_func_yield( ldap_pvt_thread_yield );
532
533         {
534                 static char uuidbuf[40];
535
536                 bdb_uuid.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ));
537                 bdb_uuid.bv_val = uuidbuf;
538         }
539
540         bi->bi_open = 0;
541         bi->bi_close = 0;
542         bi->bi_config = 0;
543         bi->bi_destroy = 0;
544
545         bi->bi_db_init = bdb_db_init;
546         bi->bi_db_config = bdb_db_config;
547         bi->bi_db_open = bdb_db_open;
548         bi->bi_db_close = bdb_db_close;
549         bi->bi_db_destroy = bdb_db_destroy;
550
551         bi->bi_op_add = bdb_add;
552         bi->bi_op_bind = bdb_bind;
553         bi->bi_op_compare = bdb_compare;
554         bi->bi_op_delete = bdb_delete;
555         bi->bi_op_modify = bdb_modify;
556         bi->bi_op_modrdn = bdb_modrdn;
557         bi->bi_op_search = bdb_search;
558
559         bi->bi_op_unbind = 0;
560         bi->bi_op_abandon = 0;
561
562         bi->bi_extended = bdb_extended;
563
564 #if 1
565         /*
566          * these routines (and their callers) are not yet designed
567          * to work with transaction.  Using them may cause deadlock.
568          */
569         bi->bi_acl_group = bdb_group;
570         bi->bi_acl_attribute = bdb_attribute;
571 #else
572         bi->bi_acl_group = 0;
573         bi->bi_acl_attribute = 0;
574 #endif
575
576         bi->bi_chk_referrals = bdb_referrals;
577         bi->bi_operational = bdb_operational;
578         bi->bi_entry_release_rw = bdb_entry_release;
579
580         /*
581          * hooks for slap tools
582          */
583         bi->bi_tool_entry_open = bdb_tool_entry_open;
584         bi->bi_tool_entry_close = bdb_tool_entry_close;
585         bi->bi_tool_entry_first = bdb_tool_entry_next;
586         bi->bi_tool_entry_next = bdb_tool_entry_next;
587         bi->bi_tool_entry_get = bdb_tool_entry_get;
588         bi->bi_tool_entry_put = bdb_tool_entry_put;
589         bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
590         bi->bi_tool_sync = 0;
591
592         bi->bi_connection_init = 0;
593         bi->bi_connection_destroy = 0;
594
595         return 0;
596 }