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