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