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