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