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