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