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