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