]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/init.c
5fe8baa358ada6836e0cf5022f7d1333bf126754
[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( BACK_BDB, ENTRY, "bdb_db_init", 0, 0, 0 );
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( BACK_BDB, ERR, "bdb_db_init: aborted %d locks\n", 
130                         aborted, 0, 0 );
131 #else
132                 Debug( LDAP_DEBUG_ANY,
133                         "bdb_lock_detect: aborted %d locks\n",
134                         aborted, 0, 0 );
135 #endif
136         }
137
138         return NULL;
139 }
140 #endif
141
142 int
143 bdb_bt_compare(
144         DB *db, 
145         const DBT *usrkey,
146         const DBT *curkey
147 )
148 {
149         unsigned char *u, *c;
150         int i;
151
152         u = usrkey->data;
153         c = curkey->data;
154
155 #ifdef WORDS_BIGENDIAN
156         for( i = 0; i < sizeof(ID); i++)
157 #else
158         for( i = sizeof(ID)-1; i >= 0; i--)
159 #endif
160         {
161                 if( u[i] - c[i] )
162                         return u[i] - c[i];
163         }
164         return 0;
165 }
166
167 static int
168 bdb_db_open( BackendDB *be )
169 {
170         int rc, i;
171         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
172         u_int32_t flags;
173
174 #ifdef NEW_LOGGING
175         LDAP_LOG( BACK_BDB, ARGS, 
176                 "bdb_db_open: %s\n", be->be_suffix[0].bv_val, 0, 0 );
177 #else
178         Debug( LDAP_DEBUG_ARGS,
179                 "bdb_db_open: %s\n",
180                 be->be_suffix[0].bv_val, 0, 0 );
181 #endif
182
183         /* we should check existance of dbenv_home and db_directory */
184
185         rc = db_env_create( &bdb->bi_dbenv, 0 );
186         if( rc != 0 ) {
187 #ifdef NEW_LOGGING
188                 LDAP_LOG( BACK_BDB, ERR, 
189                         "bdb_db_open: db_env_create failed: %s (%d)\n", 
190                         db_strerror(rc), rc, 0 );
191 #else
192                 Debug( LDAP_DEBUG_ANY,
193                         "bdb_db_open: db_env_create failed: %s (%d)\n",
194                         db_strerror(rc), rc, 0 );
195 #endif
196                 return rc;
197         }
198
199         flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE
200                 | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN | DB_RECOVER;
201
202         bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
203         bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
204 #ifndef NO_THREADS
205         bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
206 #endif
207
208 #ifdef BDB_SUBDIRS
209         {
210                 char dir[MAXPATHLEN];
211                 size_t len = strlen( bdb->bi_dbenv_home );
212
213                 strcpy( dir, bdb->bi_dbenv_home );
214                 strcat( &dir[len], BDB_TMP_SUBDIR );
215                 
216                 rc = bdb->bi_dbenv->set_tmp_dir( bdb->bi_dbenv, dir );
217                 if( rc != 0 ) {
218 #ifdef NEW_LOGGING
219                         LDAP_LOG( BACK_BDB, ERR, 
220                                 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n", 
221                                 dir, db_strerror(rc), rc );
222 #else
223                         Debug( LDAP_DEBUG_ANY,
224                                 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
225                                 dir, db_strerror(rc), rc );
226 #endif
227                         return rc;
228                 }
229
230                 strcat( &dir[len], BDB_LG_SUBDIR );
231
232                 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
233                 if( rc != 0 ) {
234 #ifdef NEW_LOGGING
235                         LDAP_LOG( BACK_BDB, ERR, 
236                                 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n", 
237                                 dir, db_strerror(rc), rc );
238 #else
239                         Debug( LDAP_DEBUG_ANY,
240                                 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
241                                 dir, db_strerror(rc), rc );
242 #endif
243                         return rc;
244                 }
245
246                 strcat( &dir[len], BDB_DATA_SUBDIR );
247
248                 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
249                 if( rc != 0 ) {
250 #ifdef NEW_LOGGING
251                         LDAP_LOG( BACK_BDB, ERR, 
252                                 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
253                                 dir, db_strerror(rc), rc );
254 #else
255                         Debug( LDAP_DEBUG_ANY,
256                                 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
257                                 dir, db_strerror(rc), rc );
258 #endif
259                         return rc;
260                 }
261         }
262 #endif
263
264 #ifdef NEW_LOGGING
265         LDAP_LOG( BACK_BDB, DETAIL1, 
266                 "bdb_db_open: dbenv_open %s\n", bdb->bi_dbenv_home, 0, 0 );
267 #else
268         Debug( LDAP_DEBUG_TRACE,
269                 "bdb_db_open: dbenv_open(%s)\n",
270                 bdb->bi_dbenv_home, 0, 0);
271 #endif
272
273         rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
274                 bdb->bi_dbenv_home,
275                 flags,
276                 bdb->bi_dbenv_mode );
277         if( rc != 0 ) {
278 #ifdef NEW_LOGGING
279                 LDAP_LOG( BACK_BDB, ERR, 
280                         "bdb_db_open: dbenv_open failed: %s (%d)\n", 
281                         db_strerror(rc), rc, 0 );
282 #else
283                 Debug( LDAP_DEBUG_ANY,
284                         "bdb_db_open: dbenv_open failed: %s (%d)\n",
285                         db_strerror(rc), rc, 0 );
286 #endif
287                 return rc;
288         }
289
290         if( bdb->bi_dbenv_xflags != 0 ) {
291                 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
292                         bdb->bi_dbenv_xflags, 1);
293                 if( rc != 0 ) {
294 #ifdef NEW_LOGGING
295                         LDAP_LOG( BACK_BDB, ERR, 
296                                 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n", 
297                                 db_strerror(rc), rc, 0 );
298 #else
299                         Debug( LDAP_DEBUG_ANY,
300                                 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
301                                 db_strerror(rc), rc, 0 );
302 #endif
303                         return rc;
304                 }
305         }
306
307         flags = DB_THREAD | DB_CREATE | bdb->bi_db_opflags;
308
309         bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
310                 BDB_INDICES * sizeof(struct bdb_db_info *) );
311
312         /* open (and create) main database */
313         for( i = 0; bdbi_databases[i].name; i++ ) {
314                 struct bdb_db_info *db;
315
316                 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
317
318                 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
319                 if( rc != 0 ) {
320 #ifdef NEW_LOGGING
321                         LDAP_LOG( BACK_BDB, ERR, 
322                                 "bdb_db_open: db_create(%s) failed: %s (%d)\n", 
323                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
324 #else
325                         Debug( LDAP_DEBUG_ANY,
326                                 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
327                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
328 #endif
329                         return rc;
330                 }
331
332                 if( i == BDB_ID2ENTRY ) {
333                         rc = db->bdi_db->set_bt_compare( db->bdi_db,
334                                 bdb_bt_compare );
335                         rc = db->bdi_db->set_pagesize( db->bdi_db,
336                                 BDB_ID2ENTRY_PAGESIZE );
337                 } else {
338 #ifdef BDB_HIER
339                         rc = db->bdi_db->set_bt_compare( db->bdi_db,
340                                 bdb_bt_compare );
341 #elif defined(BDB_IDL_MULTI)
342                         rc = db->bdi_db->set_flags( db->bdi_db, 
343                                 DB_DUP | DB_DUPSORT );
344                         rc = db->bdi_db->set_dup_compare( db->bdi_db,
345                                 bdb_bt_compare );
346 #endif
347                         rc = db->bdi_db->set_pagesize( db->bdi_db,
348                                 BDB_PAGESIZE );
349                 }
350
351                 rc = db->bdi_db->open( db->bdi_db,
352                         bdbi_databases[i].file,
353                 /*      bdbi_databases[i].name, */ NULL,
354                         bdbi_databases[i].type,
355                         bdbi_databases[i].flags | flags,
356                         bdb->bi_dbenv_mode );
357
358                 if( rc != 0 ) {
359 #ifdef NEW_LOGGING
360                         LDAP_LOG( BACK_BDB, ERR, 
361                                 "bdb_db_open: db_create(%s) failed: %s (%d)\n", 
362                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
363 #else
364                         Debug( LDAP_DEBUG_ANY,
365                                 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
366                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
367 #endif
368                         return rc;
369                 }
370
371                 db->bdi_name = bdbi_databases[i].name;
372                 bdb->bi_databases[i] = db;
373         }
374
375         bdb->bi_databases[i] = NULL;
376         bdb->bi_ndatabases = i;
377
378         /* get nextid */
379         rc = bdb_last_id( be, NULL );
380         if( rc != 0 ) {
381 #ifdef NEW_LOGGING
382                         LDAP_LOG( BACK_BDB, ERR, 
383                                 "bdb_db_open: last_id(%s) failed: %s (%d)\n", 
384                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
385 #else
386                 Debug( LDAP_DEBUG_ANY,
387                         "bdb_db_open: last_id(%s) failed: %s (%d)\n",
388                         bdb->bi_dbenv_home, db_strerror(rc), rc );
389 #endif
390                 return rc;
391         }
392
393         /* <insert> open (and create) index databases */
394 #ifdef BDB_HIER
395         rc = bdb_build_tree( be );
396 #endif
397
398 #if 0 /* ifndef NO_THREADS */
399         if( bdb->bi_lock_detect != DB_LOCK_NORUN ) {
400                 /* listener as a separate THREAD */
401                 rc = ldap_pvt_thread_create( &bdb->bi_lock_detect_tid,
402                         1, lock_detect_task, bdb );
403         }
404 #endif
405         return 0;
406 }
407
408 static int
409 bdb_db_close( BackendDB *be )
410 {
411         int rc;
412         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
413         struct bdb_db_info *db;
414
415         while( bdb->bi_ndatabases-- ) {
416                 db = bdb->bi_databases[bdb->bi_ndatabases];
417                 rc = db->bdi_db->close( db->bdi_db, 0 );
418                 /* Lower numbered names are not strdup'd */
419                 if( bdb->bi_ndatabases >= BDB_NDB )
420                         free( db->bdi_name );
421                 free( db );
422         }
423         free( bdb->bi_databases );
424         bdb_attr_index_destroy( bdb->bi_attrs );
425
426         bdb_cache_release_all (&bdb->bi_cache);
427
428         return 0;
429 }
430
431 static int
432 bdb_db_destroy( BackendDB *be )
433 {
434         int rc;
435         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
436
437         /* close db environment */
438         if( bdb->bi_dbenv ) {
439                 /* force a checkpoint */
440                 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
441                 if( rc != 0 ) {
442 #ifdef NEW_LOGGING
443                         LDAP_LOG( BACK_BDB, ERR, 
444                                 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
445                                 db_strerror(rc), rc, 0 );
446 #else
447                         Debug( LDAP_DEBUG_ANY,
448                                 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
449                                 db_strerror(rc), rc, 0 );
450 #endif
451                 }
452
453                 bdb_cache_release_all (&bdb->bi_cache);
454
455                 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
456                 bdb->bi_dbenv = NULL;
457                 if( rc != 0 ) {
458 #ifdef NEW_LOGGING
459                         LDAP_LOG( BACK_BDB, ERR, 
460                                 "bdb_db_destroy: close failed: %s (%d)\n", 
461                                 db_strerror(rc), rc, 0 );
462 #else
463                         Debug( LDAP_DEBUG_ANY,
464                                 "bdb_db_destroy: close failed: %s (%d)\n",
465                                 db_strerror(rc), rc, 0 );
466 #endif
467                         return rc;
468                 }
469         }
470
471 #ifdef BDB_HIER
472         ldap_pvt_thread_rdwr_destroy( &bdb->bi_tree_rdwr );
473 #endif
474         ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
475         ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.lru_mutex );
476         ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
477         ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
478
479         return 0;
480 }
481
482 #ifdef SLAPD_BDB_DYNAMIC
483 int back_bdb_LTX_init_module( int argc, char *argv[] ) {
484         BackendInfo bi;
485
486         memset( &bi, '\0', sizeof(bi) );
487         bi.bi_type = "bdb";
488         bi.bi_init = bdb_initialize;
489
490         backend_add( &bi );
491         return 0;
492 }
493 #endif /* SLAPD_BDB_DYNAMIC */
494
495 int
496 bdb_initialize(
497         BackendInfo     *bi
498 )
499 {
500         static char *controls[] = {
501                 LDAP_CONTROL_MANAGEDSAIT,
502 #ifdef LDAP_CONTROL_SUBENTRIES
503                 LDAP_CONTROL_SUBENTRIES,
504 #endif
505 #ifdef LDAP_CONTROL_NOOP
506                 LDAP_CONTROL_NOOP,
507 #endif
508 #ifdef LDAP_CONTROL_VALUESRETURNFILTER
509                 LDAP_CONTROL_VALUESRETURNFILTER,
510 #endif
511                 NULL
512         };
513
514         bi->bi_controls = controls;
515
516         /* initialize the underlying database system */
517 #ifdef NEW_LOGGING
518         LDAP_LOG( BACK_BDB, ENTRY, "bdb_db_initialize\n", 0, 0, 0 );
519 #else
520         Debug( LDAP_DEBUG_TRACE, "bdb_open: initialize BDB backend\n",
521                 0, 0, 0 );
522 #endif
523
524         {       /* version check */
525                 int major, minor, patch;
526                 char *version = db_version( &major, &minor, &patch );
527
528                 if( major != DB_VERSION_MAJOR ||
529                         minor != DB_VERSION_MINOR ||
530                         patch < DB_VERSION_PATCH )
531                 {
532 #ifdef NEW_LOGGING
533                         LDAP_LOG( BACK_BDB, ERR, 
534                                 "bdb_db_initialize: version mismatch: "
535                                 "\texpected: %s \tgot: %s\n", DB_VERSION_STRING, version, 0 );
536 #else
537                         Debug( LDAP_DEBUG_ANY,
538                                 "bdb_open: version mismatch\n"
539                                 "\texpected: " DB_VERSION_STRING "\n"
540                                 "\tgot: %s \n", version, 0, 0 );
541 #endif
542                 }
543
544 #ifdef NEW_LOGGING
545                 LDAP_LOG( BACK_BDB, DETAIL1, 
546                         "bdb_db_initialize: bdb_open: %s\n", version, 0, 0 );
547 #else
548                 Debug( LDAP_DEBUG_ANY, "bdb_open: %s\n",
549                         version, 0, 0 );
550 #endif
551         }
552
553 #if 0
554         db_env_set_func_malloc( ch_malloc );
555         db_env_set_func_realloc( ch_realloc );
556         db_env_set_func_free( ch_free );
557 #endif
558
559         db_env_set_func_yield( ldap_pvt_thread_yield );
560
561         {
562                 static char uuidbuf[40];
563
564                 bdb_uuid.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ));
565                 bdb_uuid.bv_val = uuidbuf;
566         }
567
568         bi->bi_open = 0;
569         bi->bi_close = 0;
570         bi->bi_config = 0;
571         bi->bi_destroy = 0;
572
573         bi->bi_db_init = bdb_db_init;
574         bi->bi_db_config = bdb_db_config;
575         bi->bi_db_open = bdb_db_open;
576         bi->bi_db_close = bdb_db_close;
577         bi->bi_db_destroy = bdb_db_destroy;
578
579         bi->bi_op_add = bdb_add;
580         bi->bi_op_bind = bdb_bind;
581         bi->bi_op_compare = bdb_compare;
582         bi->bi_op_delete = bdb_delete;
583         bi->bi_op_modify = bdb_modify;
584         bi->bi_op_modrdn = bdb_modrdn;
585         bi->bi_op_search = bdb_search;
586
587         bi->bi_op_unbind = 0;
588         bi->bi_op_abandon = 0;
589
590         bi->bi_extended = bdb_extended;
591
592 #if 1
593         /*
594          * these routines (and their callers) are not yet designed
595          * to work with transaction.  Using them may cause deadlock.
596          */
597         bi->bi_acl_group = bdb_group;
598         bi->bi_acl_attribute = bdb_attribute;
599 #else
600         bi->bi_acl_group = 0;
601         bi->bi_acl_attribute = 0;
602 #endif
603
604         bi->bi_chk_referrals = bdb_referrals;
605         bi->bi_operational = bdb_operational;
606         bi->bi_entry_release_rw = bdb_entry_release;
607
608         /*
609          * hooks for slap tools
610          */
611         bi->bi_tool_entry_open = bdb_tool_entry_open;
612         bi->bi_tool_entry_close = bdb_tool_entry_close;
613         bi->bi_tool_entry_first = bdb_tool_entry_next;
614         bi->bi_tool_entry_next = bdb_tool_entry_next;
615         bi->bi_tool_entry_get = bdb_tool_entry_get;
616         bi->bi_tool_entry_put = bdb_tool_entry_put;
617         bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
618         bi->bi_tool_sync = 0;
619
620         bi->bi_connection_init = 0;
621         bi->bi_connection_destroy = 0;
622
623         return 0;
624 }