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