]> git.sur5r.net Git - openldap/blob - servers/slapd/back-mdb/init.c
f2e69a6ff6ee237f47e89c57c39ceaf0bc2dca8f
[openldap] / servers / slapd / back-mdb / init.c
1 /* init.c - initialize mdb backend */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2000-2011 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20 #include <ac/string.h>
21 #include <ac/unistd.h>
22 #include <ac/stdlib.h>
23 #include <ac/errno.h>
24 #include <sys/stat.h>
25 #include "back-mdb.h"
26 #include <lutil.h>
27 #include <ldap_rq.h>
28 #include "config.h"
29
30 static const struct berval mdmi_databases[] = {
31         BER_BVC("ad2i"),
32         BER_BVC("dn2i"),
33         BER_BVC("id2e"),
34         BER_BVNULL
35 };
36
37 static int
38 mdb_db_init( BackendDB *be, ConfigReply *cr )
39 {
40         struct mdb_info *mdb;
41         int rc;
42
43         Debug( LDAP_DEBUG_TRACE,
44                 LDAP_XSTRING(mdb_db_init) ": Initializing mdb database\n",
45                 0, 0, 0 );
46
47         /* allocate backend-database-specific stuff */
48         mdb = (struct mdb_info *) ch_calloc( 1, sizeof(struct mdb_info) );
49
50         /* DBEnv parameters */
51         mdb->mi_dbenv_home = ch_strdup( SLAPD_DEFAULT_DB_DIR );
52         mdb->mi_dbenv_flags = 0;
53         mdb->mi_dbenv_mode = SLAPD_DEFAULT_DB_MODE;
54
55         mdb->mi_search_stack_depth = DEFAULT_SEARCH_STACK_DEPTH;
56         mdb->mi_search_stack = NULL;
57
58         mdb->mi_mapsize = DEFAULT_MAPSIZE;
59
60         be->be_private = mdb;
61         be->be_cf_ocs = be->bd_info->bi_cf_ocs;
62
63 #ifndef MDB_MULTIPLE_SUFFIXES
64         SLAP_DBFLAGS( be ) |= SLAP_DBFLAG_ONE_SUFFIX;
65 #endif
66
67         rc = mdb_monitor_db_init( be );
68
69         return rc;
70 }
71
72 static int
73 mdb_db_close( BackendDB *be, ConfigReply *cr );
74
75 static int
76 mdb_db_open( BackendDB *be, ConfigReply *cr )
77 {
78         int rc, i;
79         struct mdb_info *mdb = (struct mdb_info *) be->be_private;
80         struct stat stat1;
81         u_int32_t flags;
82         char *dbhome;
83         MDB_txn *txn;
84
85         if ( be->be_suffix == NULL ) {
86                 Debug( LDAP_DEBUG_ANY,
87                         LDAP_XSTRING(mdb_db_open) ": need suffix.\n",
88                         1, 0, 0 );
89                 return -1;
90         }
91
92         Debug( LDAP_DEBUG_ARGS,
93                 LDAP_XSTRING(mdb_db_open) ": \"%s\"\n",
94                 be->be_suffix[0].bv_val, 0, 0 );
95
96         /* Check existence of dbenv_home. Any error means trouble */
97         rc = stat( mdb->mi_dbenv_home, &stat1 );
98         if( rc != 0 ) {
99                 Debug( LDAP_DEBUG_ANY,
100                         LDAP_XSTRING(mdb_db_open) ": database \"%s\": "
101                         "cannot access database directory \"%s\" (%d).\n",
102                         be->be_suffix[0].bv_val, mdb->mi_dbenv_home, errno );
103                 return -1;
104         }
105
106         /* mdb is always clean */
107         be->be_flags |= SLAP_DBFLAG_CLEAN;
108
109         rc = mdb_env_create( &mdb->mi_dbenv );
110         if( rc != 0 ) {
111                 Debug( LDAP_DEBUG_ANY,
112                         LDAP_XSTRING(mdb_db_open) ": database \"%s\": "
113                         "mdb_env_create failed: %s (%d).\n",
114                         be->be_suffix[0].bv_val, mdb_strerror(rc), rc );
115                 goto fail;
116         }
117
118         rc = mdb_env_set_mapsize( mdb->mi_dbenv, mdb->mi_mapsize );
119         if( rc != 0 ) {
120                 Debug( LDAP_DEBUG_ANY,
121                         LDAP_XSTRING(mdb_db_open) ": database \"%s\": "
122                         "mdb_env_set_mapsize failed: %s (%d).\n",
123                         be->be_suffix[0].bv_val, mdb_strerror(rc), rc );
124                 goto fail;
125         }
126
127         rc = mdb_env_set_maxdbs( mdb->mi_dbenv, MDB_INDICES );
128         if( rc != 0 ) {
129                 Debug( LDAP_DEBUG_ANY,
130                         LDAP_XSTRING(mdb_db_open) ": database \"%s\": "
131                         "mdb_env_set_maxdbs failed: %s (%d).\n",
132                         be->be_suffix[0].bv_val, mdb_strerror(rc), rc );
133                 goto fail;
134         }
135
136 #ifdef HAVE_EBCDIC
137         strcpy( path, mdb->mi_dbenv_home );
138         __atoe( path );
139         dbhome = path;
140 #else
141         dbhome = mdb->mi_dbenv_home;
142 #endif
143
144         Debug( LDAP_DEBUG_TRACE,
145                 LDAP_XSTRING(mdb_db_open) ": database \"%s\": "
146                 "dbenv_open(%s).\n",
147                 be->be_suffix[0].bv_val, mdb->mi_dbenv_home, 0);
148
149         flags = mdb->mi_dbenv_flags;
150
151         if ( slapMode & SLAP_TOOL_QUICK )
152                 flags |= MDB_NOSYNC;
153
154         if ( slapMode & SLAP_TOOL_READONLY)
155                 flags |= MDB_RDONLY;
156
157         rc = mdb_env_open( mdb->mi_dbenv, dbhome,
158                         flags, mdb->mi_dbenv_mode );
159
160         if ( rc ) {
161                 Debug( LDAP_DEBUG_ANY,
162                         LDAP_XSTRING(mdb_db_open) ": database \"%s\" cannot be opened, err %d. "
163                         "Restore from backup!\n",
164                         be->be_suffix[0].bv_val, rc, 0 );
165                 goto fail;
166         }
167
168         rc = mdb_txn_begin( mdb->mi_dbenv, 0, &txn );
169         if ( rc ) {
170                 Debug( LDAP_DEBUG_ANY,
171                         LDAP_XSTRING(mdb_db_open) ": database \"%s\" cannot be opened, err %d. "
172                         "Restore from backup!\n",
173                         be->be_suffix[0].bv_val, rc, 0 );
174                 goto fail;
175         }
176
177         /* open (and create) main databases */
178         for( i = 0; mdmi_databases[i].bv_val; i++ ) {
179                 flags = MDB_INTEGERKEY;
180                 if( i == MDB_ID2ENTRY ) {
181                         if ( !(slapMode & (SLAP_TOOL_READMAIN|SLAP_TOOL_READONLY) ))
182                                 flags |= MDB_CREATE;
183                 } else {
184                         if ( i == MDB_DN2ID )
185                                 flags |= MDB_DUPSORT;
186                         if ( !(slapMode & SLAP_TOOL_READONLY) )
187                                 flags |= MDB_CREATE;
188                 }
189
190                 rc = mdb_open( txn,
191                         mdmi_databases[i].bv_val,
192                         flags,
193                         &mdb->mi_dbis[i] );
194
195                 if ( rc != 0 ) {
196                         snprintf( cr->msg, sizeof(cr->msg), "database \"%s\": "
197                                 "mdb_open(%s/%s) failed: %s (%d).", 
198                                 be->be_suffix[0].bv_val, 
199                                 mdb->mi_dbenv_home, mdmi_databases[i].bv_val,
200                                 mdb_strerror(rc), rc );
201                         Debug( LDAP_DEBUG_ANY,
202                                 LDAP_XSTRING(mdb_db_open) ": %s\n",
203                                 cr->msg, 0, 0 );
204                         goto fail;
205                 }
206
207                 if ( i == MDB_DN2ID )
208                         mdb_set_dupsort( txn, mdb->mi_dbis[i], mdb_dup_compare );
209
210         }
211
212         rc = mdb_attr_dbs_open( be, txn, cr );
213         if ( rc ) {
214                 mdb_txn_abort( txn );
215                 goto fail;
216         }
217
218         rc = mdb_txn_commit(txn);
219         if ( rc != 0 ) {
220                 goto fail;
221         }
222
223         /* monitor setup */
224         rc = mdb_monitor_db_open( be );
225         if ( rc != 0 ) {
226                 goto fail;
227         }
228
229         mdb->mi_flags |= MDB_IS_OPEN;
230
231         return 0;
232
233 fail:
234         mdb_db_close( be, NULL );
235         return rc;
236 }
237
238 static int
239 mdb_db_close( BackendDB *be, ConfigReply *cr )
240 {
241         int rc;
242         struct mdb_info *mdb = (struct mdb_info *) be->be_private;
243         MDB_txn *txn;
244
245         /* monitor handling */
246         (void)mdb_monitor_db_close( be );
247
248         mdb->mi_flags &= ~MDB_IS_OPEN;
249
250         if( mdb->mi_dbenv ) {
251                 mdb_reader_flush( mdb->mi_dbenv );
252         }
253
254         if ( mdb->mi_dbenv ) {
255                 if ( mdb->mi_dbis[0] ) {
256                         int i;
257                         rc = mdb_txn_begin( mdb->mi_dbenv, 1, &txn );
258
259                         mdb_attr_dbs_close( mdb, txn );
260                         for ( i=0; i<MDB_NDB; i++ )
261                                 mdb_close( txn, mdb->mi_dbis[i] );
262
263                         mdb_txn_abort( txn );
264
265                         /* force a sync */
266                         rc = mdb_env_sync( mdb->mi_dbenv, 1 );
267                         if( rc != 0 ) {
268                                 Debug( LDAP_DEBUG_ANY,
269                                         "mdb_db_close: database \"%s\": "
270                                         "mdb_env_sync failed: %s (%d).\n",
271                                         be->be_suffix[0].bv_val, mdb_strerror(rc), rc );
272                         }
273                 }
274
275                 mdb_env_close( mdb->mi_dbenv );
276                 mdb->mi_dbenv = NULL;
277         }
278
279         return 0;
280 }
281
282 static int
283 mdb_db_destroy( BackendDB *be, ConfigReply *cr )
284 {
285         struct mdb_info *mdb = (struct mdb_info *) be->be_private;
286
287         /* stop and remove checkpoint task */
288         if ( mdb->mi_txn_cp_task ) {
289                 struct re_s *re = mdb->mi_txn_cp_task;
290                 mdb->mi_txn_cp_task = NULL;
291                 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
292                 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ) )
293                         ldap_pvt_runqueue_stoptask( &slapd_rq, re );
294                 ldap_pvt_runqueue_remove( &slapd_rq, re );
295                 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
296         }
297
298         /* monitor handling */
299         (void)mdb_monitor_db_destroy( be );
300
301         if( mdb->mi_dbenv_home ) ch_free( mdb->mi_dbenv_home );
302
303         mdb_attr_index_destroy( mdb );
304
305         ch_free( mdb );
306         be->be_private = NULL;
307
308         return 0;
309 }
310
311 int
312 mdb_back_initialize(
313         BackendInfo     *bi )
314 {
315         int rc;
316
317         static char *controls[] = {
318                 LDAP_CONTROL_ASSERT,
319                 LDAP_CONTROL_MANAGEDSAIT,
320                 LDAP_CONTROL_NOOP,
321                 LDAP_CONTROL_PAGEDRESULTS,
322                 LDAP_CONTROL_PRE_READ,
323                 LDAP_CONTROL_POST_READ,
324                 LDAP_CONTROL_SUBENTRIES,
325                 LDAP_CONTROL_X_PERMISSIVE_MODIFY,
326 #ifdef LDAP_X_TXN
327                 LDAP_CONTROL_X_TXN_SPEC,
328 #endif
329                 NULL
330         };
331
332         /* initialize the underlying database system */
333         Debug( LDAP_DEBUG_TRACE,
334                 LDAP_XSTRING(mdb_back_initialize) ": initialize " 
335                 MDB_UCTYPE " backend\n", 0, 0, 0 );
336
337         bi->bi_flags |=
338                 SLAP_BFLAG_INCREMENT |
339                 SLAP_BFLAG_SUBENTRIES |
340                 SLAP_BFLAG_ALIASES |
341                 SLAP_BFLAG_REFERRALS;
342
343         bi->bi_controls = controls;
344
345         {       /* version check */
346                 int major, minor, patch, ver;
347                 char *version = mdb_version( &major, &minor, &patch );
348 #ifdef HAVE_EBCDIC
349                 char v2[1024];
350
351                 /* All our stdio does an ASCII to EBCDIC conversion on
352                  * the output. Strings from the MDB library are already
353                  * in EBCDIC; we have to go back and forth...
354                  */
355                 strcpy( v2, version );
356                 __etoa( v2 );
357                 version = v2;
358 #endif
359                 ver = (major << 24) | (minor << 16) | patch;
360                 if( ver != MDB_VERSION_FULL ) {
361                         /* fail if a versions don't match */
362                         Debug( LDAP_DEBUG_ANY,
363                                 LDAP_XSTRING(mdb_back_initialize) ": "
364                                 "MDB library version mismatch:"
365                                 " expected " MDB_VERSION_STRING ","
366                                 " got %s\n", version, 0, 0 );
367                         return -1;
368                 }
369
370                 Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(mdb_back_initialize)
371                         ": %s\n", version, 0, 0 );
372         }
373
374         bi->bi_open = 0;
375         bi->bi_close = 0;
376         bi->bi_config = 0;
377         bi->bi_destroy = 0;
378
379         bi->bi_db_init = mdb_db_init;
380         bi->bi_db_config = config_generic_wrapper;
381         bi->bi_db_open = mdb_db_open;
382         bi->bi_db_close = mdb_db_close;
383         bi->bi_db_destroy = mdb_db_destroy;
384
385         bi->bi_op_add = mdb_add;
386         bi->bi_op_bind = mdb_bind;
387         bi->bi_op_compare = mdb_compare;
388         bi->bi_op_delete = mdb_delete;
389         bi->bi_op_modify = mdb_modify;
390         bi->bi_op_modrdn = mdb_modrdn;
391         bi->bi_op_search = mdb_search;
392
393         bi->bi_op_unbind = 0;
394
395         bi->bi_extended = mdb_extended;
396
397 #if 0   /* FIXME: Redundant, why does this exist? */
398         bi->bi_chk_referrals = mdb_referrals;
399 #endif
400         bi->bi_operational = mdb_operational;
401
402         bi->bi_has_subordinates = mdb_hasSubordinates;
403         bi->bi_entry_release_rw = mdb_entry_release;
404         bi->bi_entry_get_rw = mdb_entry_get;
405
406         /*
407          * hooks for slap tools
408          */
409         bi->bi_tool_entry_open = mdb_tool_entry_open;
410         bi->bi_tool_entry_close = mdb_tool_entry_close;
411         bi->bi_tool_entry_first = backend_tool_entry_first;
412         bi->bi_tool_entry_first_x = mdb_tool_entry_first_x;
413         bi->bi_tool_entry_next = mdb_tool_entry_next;
414         bi->bi_tool_entry_get = mdb_tool_entry_get;
415         bi->bi_tool_entry_put = mdb_tool_entry_put;
416         bi->bi_tool_entry_reindex = mdb_tool_entry_reindex;
417         bi->bi_tool_sync = 0;
418         bi->bi_tool_dn2id_get = mdb_tool_dn2id_get;
419         bi->bi_tool_entry_modify = mdb_tool_entry_modify;
420
421         bi->bi_connection_init = 0;
422         bi->bi_connection_destroy = 0;
423
424         rc = mdb_back_init_cf( bi );
425
426         return rc;
427 }
428
429 #if     (SLAPD_MDB == SLAPD_MOD_DYNAMIC)
430
431 SLAP_BACKEND_INIT_MODULE( mdb )
432
433 #endif /* SLAPD_MDB == SLAPD_MOD_DYNAMIC */
434