]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/init.c
BDB_ALIASES and BDB_SUBENTRIES cleanup
[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 "back-bdb.h"
16 #include "external.h"
17
18 static struct bdbi_database {
19         char *file;
20         char *name;
21         int type;
22         int flags;
23 } bdbi_databases[] = {
24         { "id2entry" BDB_SUFFIX, "id2entry", DB_BTREE, 0 },
25 #ifdef BDB_HIER
26         { "id2parent" BDB_SUFFIX, "id2parent", DB_BTREE, 0 },
27 #else
28         { "dn2id" BDB_SUFFIX, "dn2id", DB_BTREE, 0 },
29 #endif
30         { NULL, NULL, 0, 0 }
31 };
32
33 #if 0
34 static int
35 bdb_destroy( BackendInfo *bi )
36 {
37         return 0;
38 }
39
40 static int
41 bdb_open( BackendInfo *bi )
42 {
43         /* initialize the underlying database system */
44         Debug( LDAP_DEBUG_TRACE, "bdb_open: initialize BDB backend\n",
45                 0, 0, 0 );
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         Debug( LDAP_DEBUG_ANY,
64                 "bdb_db_init: Initializing BDB database\n",
65                 0, 0, 0 );
66
67         /* indicate system schema supported */
68         be->be_flags |=
69 #ifdef BDB_SUBENTRIES
70                 SLAP_BFLAG_SUBENTRIES |
71 #endif
72 #ifdef BDB_ALIASES
73                 SLAP_BFLAG_ALIASES |
74 #endif
75                 SLAP_BFLAG_REFERRALS;
76
77         /* allocate backend-database-specific stuff */
78         bdb = (struct bdb_info *) ch_calloc( 1, sizeof(struct bdb_info) );
79
80         /* DBEnv parameters */
81         bdb->bi_dbenv_home = ch_strdup( BDB_DBENV_HOME );
82         bdb->bi_dbenv_xflags = 0;
83         bdb->bi_dbenv_mode = DEFAULT_MODE;
84
85         bdb->bi_cache.c_maxsize = DEFAULT_CACHE_SIZE;
86
87 #ifndef NO_THREADS
88 #if 0
89         bdb->bi_lock_detect = DB_LOCK_NORUN;
90 #else
91         bdb->bi_lock_detect = DB_LOCK_DEFAULT;
92 #endif
93 #endif
94
95         ldap_pvt_thread_mutex_init( &bdb->bi_database_mutex );
96         ldap_pvt_thread_mutex_init( &bdb->bi_lastid_mutex );
97         ldap_pvt_thread_mutex_init( &bdb->bi_cache.c_mutex );
98 #ifdef BDB_HIER
99         ldap_pvt_thread_rdwr_init( &bdb->bi_tree_rdwr );
100 #endif
101
102         be->be_private = bdb;
103         return 0;
104 }
105
106 #if 0 /* ifndef NO_THREADS */
107 static void *lock_detect_task( void *arg )
108 {
109         struct bdb_info *bdb = (struct bdb_info *) arg;
110
111         while( bdb->bi_dbenv != NULL ) {
112                 int rc;
113                 int aborted;
114                 sleep( bdb->bi_lock_detect_seconds );
115
116                 rc = LOCK_DETECT( bdb->bi_dbenv, 0,
117                         bdb->bi_lock_detect, &aborted );
118
119                 if( rc != 0 ) {
120                         break;
121                 }
122
123                 Debug( LDAP_DEBUG_ANY,
124                         "bdb_lock_detect: aborted %d locks\n",
125                         aborted, 0, 0 );
126         }
127
128         return NULL;
129 }
130 #endif
131
132 int
133 bdb_bt_compare(
134         DB *db, 
135         const DBT *usrkey,
136         const DBT *curkey
137 )
138 {
139         unsigned char *u, *c;
140         int i;
141
142         u = usrkey->data;
143         c = curkey->data;
144
145 #ifdef WORDS_BIGENDIAN
146         for( i = 0; i < sizeof(ID); i++)
147 #else
148         for( i = sizeof(ID)-1; i >= 0; i--)
149 #endif
150         {
151                 if( u[i] - c[i] )
152                         return u[i] - c[i];
153         }
154         return 0;
155 }
156
157 static int
158 bdb_db_open( BackendDB *be )
159 {
160         int rc, i;
161         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
162         u_int32_t flags;
163
164         Debug( LDAP_DEBUG_ARGS,
165                 "bdb_db_open: %s\n",
166                 be->be_suffix[0]->bv_val, 0, 0 );
167
168         /* we should check existance of dbenv_home and db_directory */
169
170         rc = db_env_create( &bdb->bi_dbenv, 0 );
171         if( rc != 0 ) {
172                 Debug( LDAP_DEBUG_ANY,
173                         "bdb_db_open: db_env_create failed: %s (%d)\n",
174                         db_strerror(rc), rc, 0 );
175                 return rc;
176         }
177
178         flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE
179                 | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN | DB_RECOVER;
180
181         bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0]->bv_val );
182         bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
183 #ifndef NO_THREADS
184         bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
185 #endif
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                         Debug( LDAP_DEBUG_ANY,
198                                 "bdb_db_open: set_tmp_dir(%s) failed: %s (%d)\n",
199                                 dir, db_strerror(rc), rc );
200                         return rc;
201                 }
202
203                 strcat( &dir[len], BDB_LG_SUBDIR );
204
205                 rc = bdb->bi_dbenv->set_lg_dir( bdb->bi_dbenv, dir );
206                 if( rc != 0 ) {
207                         Debug( LDAP_DEBUG_ANY,
208                                 "bdb_db_open: set_lg_dir(%s) failed: %s (%d)\n",
209                                 dir, db_strerror(rc), rc );
210                         return rc;
211                 }
212
213                 strcat( &dir[len], BDB_DATA_SUBDIR );
214
215                 rc = bdb->bi_dbenv->set_data_dir( bdb->bi_dbenv, dir );
216                 if( rc != 0 ) {
217                         Debug( LDAP_DEBUG_ANY,
218                                 "bdb_db_open: set_data_dir(%s) failed: %s (%d)\n",
219                                 dir, db_strerror(rc), rc );
220                         return rc;
221                 }
222         }
223 #endif
224
225         Debug( LDAP_DEBUG_TRACE,
226                 "bdb_db_open: dbenv_open(%s)\n",
227                 bdb->bi_dbenv_home, 0, 0);
228
229         rc = bdb->bi_dbenv->open( bdb->bi_dbenv,
230                 bdb->bi_dbenv_home,
231                 flags,
232                 bdb->bi_dbenv_mode );
233         if( rc != 0 ) {
234                 Debug( LDAP_DEBUG_ANY,
235                         "bdb_db_open: dbenv_open failed: %s (%d)\n",
236                         db_strerror(rc), rc, 0 );
237                 return rc;
238         }
239
240         if( bdb->bi_dbenv_xflags != 0 ) {
241                 rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
242                         bdb->bi_dbenv_xflags, 1);
243                 if( rc != 0 ) {
244                         Debug( LDAP_DEBUG_ANY,
245                                 "bdb_db_open: dbenv_set_flags failed: %s (%d)\n",
246                                 db_strerror(rc), rc, 0 );
247                         return rc;
248                 }
249         }
250
251         flags = DB_THREAD | DB_CREATE | bdb->bi_db_opflags;
252
253         bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
254                 BDB_INDICES * sizeof(struct bdb_db_info *) );
255
256         /* open (and create) main database */
257         for( i = 0; bdbi_databases[i].name; i++ ) {
258                 struct bdb_db_info *db;
259
260                 db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));
261
262                 rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
263                 if( rc != 0 ) {
264                         Debug( LDAP_DEBUG_ANY,
265                                 "bdb_db_open: db_create(%s) failed: %s (%d)\n",
266                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
267                         return rc;
268                 }
269
270                 if( i == BDB_ID2ENTRY ) {
271                         rc = db->bdi_db->set_bt_compare( db->bdi_db,
272                                 bdb_bt_compare );
273                         rc = db->bdi_db->set_pagesize( db->bdi_db,
274                                 BDB_ID2ENTRY_PAGESIZE );
275                 } else {
276 #ifdef BDB_HIER
277                         rc = db->bdi_db->set_bt_compare( db->bdi_db,
278                                 bdb_bt_compare );
279 #elif defined(BDB_IDL_MULTI)
280                         rc = db->bdi_db->set_flags( db->bdi_db, 
281                                 DB_DUP | DB_DUPSORT );
282                         rc = db->bdi_db->set_dup_compare( db->bdi_db,
283                                 bdb_bt_compare );
284 #endif
285                         rc = db->bdi_db->set_pagesize( db->bdi_db,
286                                 BDB_PAGESIZE );
287                 }
288
289                 rc = db->bdi_db->open( db->bdi_db,
290                         bdbi_databases[i].file,
291                 /*      bdbi_databases[i].name, */ NULL,
292                         bdbi_databases[i].type,
293                         bdbi_databases[i].flags | flags,
294                         bdb->bi_dbenv_mode );
295
296                 if( rc != 0 ) {
297                         Debug( LDAP_DEBUG_ANY,
298                                 "bdb_db_open: db_open(%s) failed: %s (%d)\n",
299                                 bdb->bi_dbenv_home, db_strerror(rc), rc );
300                         return rc;
301                 }
302
303                 db->bdi_name = bdbi_databases[i].name;
304                 bdb->bi_databases[i] = db;
305         }
306
307         bdb->bi_databases[i] = NULL;
308         bdb->bi_ndatabases = i;
309
310         /* get nextid */
311         rc = bdb_last_id( be, NULL );
312         if( rc != 0 ) {
313                 Debug( LDAP_DEBUG_ANY,
314                         "bdb_db_open: last_id(%s) failed: %s (%d)\n",
315                         bdb->bi_dbenv_home, db_strerror(rc), rc );
316                 return rc;
317         }
318
319         /* <insert> open (and create) index databases */
320 #ifdef BDB_HIER
321         rc = bdb_build_tree( be );
322 #endif
323
324 #if 0 /* ifndef NO_THREADS */
325         if( bdb->bi_lock_detect != DB_LOCK_NORUN ) {
326                 /* listener as a separate THREAD */
327                 rc = ldap_pvt_thread_create( &bdb->bi_lock_detect_tid,
328                         1, lock_detect_task, bdb );
329         }
330 #endif
331         return 0;
332 }
333
334 static int
335 bdb_db_close( BackendDB *be )
336 {
337         int rc;
338         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
339         struct bdb_db_info *db;
340
341         while( bdb->bi_ndatabases-- ) {
342                 db = bdb->bi_databases[bdb->bi_ndatabases];
343                 rc = db->bdi_db->close( db->bdi_db, 0 );
344                 /* Lower numbered names are not strdup'd */
345                 if( bdb->bi_ndatabases >= BDB_NDB )
346                         free( db->bdi_name );
347                 free( db );
348         }
349         free( bdb->bi_databases );
350         bdb_attr_index_destroy( bdb->bi_attrs );
351
352         bdb_cache_release_all (&bdb->bi_cache);
353
354         return 0;
355 }
356
357 static int
358 bdb_db_destroy( BackendDB *be )
359 {
360         int rc;
361         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
362
363         /* close db environment */
364         if( bdb->bi_dbenv ) {
365                 /* force a checkpoint */
366                 rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
367                 if( rc != 0 ) {
368                         Debug( LDAP_DEBUG_ANY,
369                                 "bdb_db_destroy: txn_checkpoint failed: %s (%d)\n",
370                                 db_strerror(rc), rc, 0 );
371                 }
372
373                 bdb_cache_release_all (&bdb->bi_cache);
374
375                 rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
376                 bdb->bi_dbenv = NULL;
377                 if( rc != 0 ) {
378                         Debug( LDAP_DEBUG_ANY,
379                                 "bdb_db_destroy: close failed: %s (%d)\n",
380                                 db_strerror(rc), rc, 0 );
381                         return rc;
382                 }
383         }
384
385         return 0;
386 }
387
388 #ifdef SLAPD_BDB_DYNAMIC
389 int back_bdb_LTX_init_module( int argc, char *argv[] ) {
390         BackendInfo bi;
391
392         memset( &bi, '\0', sizeof(bi) );
393         bi.bi_type = "bdb";
394         bi.bi_init = bdb_initialize;
395
396         backend_add( &bi );
397         return 0;
398 }
399 #endif /* SLAPD_BDB_DYNAMIC */
400
401 int
402 bdb_initialize(
403         BackendInfo     *bi
404 )
405 {
406         static char *controls[] = {
407                 LDAP_CONTROL_MANAGEDSAIT,
408                 LDAP_CONTROL_SUBENTRIES,
409                 LDAP_CONTROL_NOOP,
410                 NULL
411         };
412
413         {       /* version check */
414                 int major, minor, patch;
415                 char *version = db_version( &major, &minor, &patch );
416
417                 if( major != DB_VERSION_MAJOR ||
418                         minor != DB_VERSION_MINOR ||
419                         patch < DB_VERSION_PATCH )
420                 {
421                         Debug( LDAP_DEBUG_ANY,
422                                 "bi_back_initialize: version mismatch\n"
423                                 "\texpected: " DB_VERSION_STRING "\n"
424                                 "\tgot: %s \n", version, 0, 0 );
425                 }
426
427                 Debug( LDAP_DEBUG_ANY, "bdb_initialize: %s\n",
428                         version, 0, 0 );
429         }
430
431 #if 0
432         db_env_set_func_malloc( ch_malloc );
433         db_env_set_func_realloc( ch_realloc );
434         db_env_set_func_free( ch_free );
435 #endif
436
437         db_env_set_func_yield( ldap_pvt_thread_yield );
438
439         bi->bi_controls = controls;
440
441         bi->bi_open = 0;
442         bi->bi_close = 0;
443         bi->bi_config = 0;
444         bi->bi_destroy = 0;
445
446         bi->bi_db_init = bdb_db_init;
447         bi->bi_db_config = bdb_db_config;
448         bi->bi_db_open = bdb_db_open;
449         bi->bi_db_close = bdb_db_close;
450         bi->bi_db_destroy = bdb_db_destroy;
451
452         bi->bi_op_add = bdb_add;
453         bi->bi_op_bind = bdb_bind;
454         bi->bi_op_compare = bdb_compare;
455         bi->bi_op_delete = bdb_delete;
456         bi->bi_op_modify = bdb_modify;
457         bi->bi_op_modrdn = bdb_modrdn;
458         bi->bi_op_search = bdb_search;
459
460         bi->bi_op_unbind = 0;
461         bi->bi_op_abandon = 0;
462
463         bi->bi_extended = bdb_extended;
464
465 #if 0
466         /*
467          * these routines (and their callers) are not yet designed
468          * to work with transaction.  Using them may cause deadlock.
469          */
470         bi->bi_acl_group = bdb_group;
471         bi->bi_acl_attribute = bdb_attribute;
472 #else
473         bi->bi_acl_group = 0;
474         bi->bi_acl_attribute = 0;
475 #endif
476
477         bi->bi_chk_referrals = bdb_referrals;
478         bi->bi_entry_release_rw = bdb_entry_release;
479
480         /*
481          * hooks for slap tools
482          */
483         bi->bi_tool_entry_open = bdb_tool_entry_open;
484         bi->bi_tool_entry_close = bdb_tool_entry_close;
485         bi->bi_tool_entry_first = bdb_tool_entry_next;
486         bi->bi_tool_entry_next = bdb_tool_entry_next;
487         bi->bi_tool_entry_get = bdb_tool_entry_get;
488         bi->bi_tool_entry_put = bdb_tool_entry_put;
489         bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
490         bi->bi_tool_sync = 0;
491
492         bi->bi_connection_init = 0;
493         bi->bi_connection_destroy = 0;
494
495         return 0;
496 }