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