]> git.sur5r.net Git - openldap/blob - servers/slapd/back-mdb/init.c
More porting
[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 0
251         if( mdb->mi_dbenv ) {
252                 mdb_reader_flush( mdb->mi_dbenv );
253         }
254 #endif
255
256         if ( mdb->mi_dbenv ) {
257                 if ( mdb->mi_dbis[0] ) {
258                         int i;
259                         rc = mdb_txn_begin( mdb->mi_dbenv, 1, &txn );
260
261                         mdb_attr_dbs_close( mdb, txn );
262                         for ( i=0; i<MDB_NDB; i++ )
263                                 mdb_close( txn, mdb->mi_dbis[i] );
264
265                         mdb_txn_abort( txn );
266
267                         /* force a sync */
268                         rc = mdb_env_sync( mdb->mi_dbenv, 1 );
269                         if( rc != 0 ) {
270                                 Debug( LDAP_DEBUG_ANY,
271                                         "mdb_db_close: database \"%s\": "
272                                         "mdb_env_sync failed: %s (%d).\n",
273                                         be->be_suffix[0].bv_val, mdb_strerror(rc), rc );
274                         }
275                 }
276
277                 mdb_env_close( mdb->mi_dbenv );
278                 mdb->mi_dbenv = NULL;
279         }
280
281         return 0;
282 }
283
284 static int
285 mdb_db_destroy( BackendDB *be, ConfigReply *cr )
286 {
287         struct mdb_info *mdb = (struct mdb_info *) be->be_private;
288
289         /* stop and remove checkpoint task */
290         if ( mdb->mi_txn_cp_task ) {
291                 struct re_s *re = mdb->mi_txn_cp_task;
292                 mdb->mi_txn_cp_task = NULL;
293                 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
294                 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ) )
295                         ldap_pvt_runqueue_stoptask( &slapd_rq, re );
296                 ldap_pvt_runqueue_remove( &slapd_rq, re );
297                 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
298         }
299
300         /* monitor handling */
301         (void)mdb_monitor_db_destroy( be );
302
303         if( mdb->mi_dbenv_home ) ch_free( mdb->mi_dbenv_home );
304
305         mdb_attr_index_destroy( mdb );
306
307         ch_free( mdb );
308         be->be_private = NULL;
309
310         return 0;
311 }
312
313 int
314 mdb_back_initialize(
315         BackendInfo     *bi )
316 {
317         int rc;
318
319         static char *controls[] = {
320                 LDAP_CONTROL_ASSERT,
321                 LDAP_CONTROL_MANAGEDSAIT,
322                 LDAP_CONTROL_NOOP,
323                 LDAP_CONTROL_PAGEDRESULTS,
324                 LDAP_CONTROL_PRE_READ,
325                 LDAP_CONTROL_POST_READ,
326                 LDAP_CONTROL_SUBENTRIES,
327                 LDAP_CONTROL_X_PERMISSIVE_MODIFY,
328 #ifdef LDAP_X_TXN
329                 LDAP_CONTROL_X_TXN_SPEC,
330 #endif
331                 NULL
332         };
333
334         /* initialize the underlying database system */
335         Debug( LDAP_DEBUG_TRACE,
336                 LDAP_XSTRING(mdb_back_initialize) ": initialize " 
337                 MDB_UCTYPE " backend\n", 0, 0, 0 );
338
339         bi->bi_flags |=
340                 SLAP_BFLAG_INCREMENT |
341                 SLAP_BFLAG_SUBENTRIES |
342                 SLAP_BFLAG_ALIASES |
343                 SLAP_BFLAG_REFERRALS;
344
345         bi->bi_controls = controls;
346
347         {       /* version check */
348                 int major, minor, patch, ver;
349                 char *version = mdb_version( &major, &minor, &patch );
350 #ifdef HAVE_EBCDIC
351                 char v2[1024];
352
353                 /* All our stdio does an ASCII to EBCDIC conversion on
354                  * the output. Strings from the MDB library are already
355                  * in EBCDIC; we have to go back and forth...
356                  */
357                 strcpy( v2, version );
358                 __etoa( v2 );
359                 version = v2;
360 #endif
361                 ver = (major << 24) | (minor << 16) | patch;
362                 if( ver != MDB_VERSION_FULL ) {
363                         /* fail if a versions don't match */
364                         Debug( LDAP_DEBUG_ANY,
365                                 LDAP_XSTRING(mdb_back_initialize) ": "
366                                 "MDB library version mismatch:"
367                                 " expected " MDB_VERSION_STRING ","
368                                 " got %s\n", version, 0, 0 );
369                         return -1;
370                 }
371
372                 Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(mdb_back_initialize)
373                         ": %s\n", version, 0, 0 );
374         }
375
376         bi->bi_open = 0;
377         bi->bi_close = 0;
378         bi->bi_config = 0;
379         bi->bi_destroy = 0;
380
381         bi->bi_db_init = mdb_db_init;
382         bi->bi_db_config = config_generic_wrapper;
383         bi->bi_db_open = mdb_db_open;
384         bi->bi_db_close = mdb_db_close;
385         bi->bi_db_destroy = mdb_db_destroy;
386
387         bi->bi_op_add = mdb_add;
388         bi->bi_op_bind = mdb_bind;
389         bi->bi_op_compare = mdb_compare;
390         bi->bi_op_delete = mdb_delete;
391         bi->bi_op_modify = mdb_modify;
392         bi->bi_op_modrdn = mdb_modrdn;
393         bi->bi_op_search = mdb_search;
394
395         bi->bi_op_unbind = 0;
396
397         bi->bi_extended = mdb_extended;
398
399         bi->bi_chk_referrals = mdb_referrals;
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