init.c 19.3 KB
Newer Older
Kurt Zeilenga's avatar
Kurt Zeilenga committed
1
2
/* init.c - initialize bdb backend */
/* $OpenLDAP$ */
Kurt Zeilenga's avatar
Notices    
Kurt Zeilenga committed
3
4
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
Kurt Zeilenga's avatar
Kurt Zeilenga committed
5
 * Copyright 2000-2007 The OpenLDAP Foundation.
Kurt Zeilenga's avatar
Notices    
Kurt Zeilenga committed
6
7
8
9
10
11
12
13
14
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
 * Public License.
 *
 * A copy of this license is available in the file LICENSE in the
 * top-level directory of the distribution or, alternatively, at
 * <http://www.OpenLDAP.org/license.html>.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
15
16
17
18
19
20
 */

#include "portable.h"

#include <stdio.h>
#include <ac/string.h>
Howard Chu's avatar
Howard Chu committed
21
22
#include <ac/unistd.h>
#include <ac/stdlib.h>
Howard Chu's avatar
Howard Chu committed
23
24
#include <ac/errno.h>
#include <sys/stat.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
25
#include "back-bdb.h"
26
#include <lutil.h>
27
#include <ldap_rq.h>
28
#include "alock.h"
29
#include "config.h"
Kurt Zeilenga's avatar
Kurt Zeilenga committed
30

31
static const struct bdbi_database {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
32
33
34
35
	char *file;
	char *name;
	int type;
	int flags;
36
} bdbi_databases[] = {
37
	{ "id2entry" BDB_SUFFIX, "id2entry", DB_BTREE, 0 },
38
	{ "dn2id" BDB_SUFFIX, "dn2id", DB_BTREE, 0 },
39
	{ NULL, NULL, 0, 0 }
Kurt Zeilenga's avatar
Kurt Zeilenga committed
40
41
};

42
43
44
typedef void * db_malloc(size_t);
typedef void * db_realloc(void *, size_t);

45
46
47
48
#define bdb_db_init	BDB_SYMBOL(db_init)
#define bdb_db_open BDB_SYMBOL(db_open)
#define bdb_db_close BDB_SYMBOL(db_close)

Kurt Zeilenga's avatar
Kurt Zeilenga committed
49
static int
50
bdb_db_init( BackendDB *be, ConfigReply *cr )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
51
{
52
	struct bdb_info	*bdb;
53
	int rc;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
54

Kurt Zeilenga's avatar
Kurt Zeilenga committed
55
56
57
	Debug( LDAP_DEBUG_TRACE,
		LDAP_XSTRING(bdb_db_init) ": Initializing " BDB_UCTYPE " database\n",
		0, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
58

Kurt Zeilenga's avatar
Kurt Zeilenga committed
59
	/* allocate backend-database-specific stuff */
60
	bdb = (struct bdb_info *) ch_calloc( 1, sizeof(struct bdb_info) );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
61
62

	/* DBEnv parameters */
63
	bdb->bi_dbenv_home = ch_strdup( SLAPD_DEFAULT_DB_DIR );
64
	bdb->bi_dbenv_xflags = 0;
65
	bdb->bi_dbenv_mode = SLAPD_DEFAULT_DB_MODE;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
66

67
	bdb->bi_cache.c_maxsize = DEFAULT_CACHE_SIZE;
Howard Chu's avatar
Howard Chu committed
68
	bdb->bi_cache.c_minfree = 1;
69

70
	bdb->bi_lock_detect = DB_LOCK_DEFAULT;
71
72
	bdb->bi_search_stack_depth = DEFAULT_SEARCH_STACK_DEPTH;
	bdb->bi_search_stack = NULL;
73

Howard Chu's avatar
Howard Chu committed
74
	ldap_pvt_thread_mutex_init( &bdb->bi_database_mutex );
75
	ldap_pvt_thread_mutex_init( &bdb->bi_lastid_mutex );
76
77
78
#ifdef BDB_HIER
	ldap_pvt_thread_mutex_init( &bdb->bi_modrdns_mutex );
#endif
79
	ldap_pvt_thread_mutex_init( &bdb->bi_cache.c_lru_mutex );
80
81
	ldap_pvt_thread_mutex_init( &bdb->bi_cache.c_count_mutex );
	ldap_pvt_thread_mutex_init( &bdb->bi_cache.c_eifree_mutex );
Howard Chu's avatar
Howard Chu committed
82
	ldap_pvt_thread_mutex_init( &bdb->bi_cache.c_dntree.bei_kids_mutex );
83
	ldap_pvt_thread_rdwr_init ( &bdb->bi_cache.c_rwlock );
84
85
	ldap_pvt_thread_rdwr_init( &bdb->bi_idl_tree_rwlock );
	ldap_pvt_thread_mutex_init( &bdb->bi_idl_tree_lrulock );
86

87

88
	be->be_private = bdb;
89
	be->be_cf_ocs = be->bd_info->bi_cf_ocs;
90

Pierangelo Masarati's avatar
Pierangelo Masarati committed
91
	rc = bdb_monitor_db_init( be );
92
93

	return rc;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
94
95
}

96
static int
97
bdb_db_close( BackendDB *be, ConfigReply *cr );
98

Kurt Zeilenga's avatar
Kurt Zeilenga committed
99
static int
100
bdb_db_open( BackendDB *be, ConfigReply *cr )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
101
{
Kurt Zeilenga's avatar
Kurt Zeilenga committed
102
	int rc, i;
103
	struct bdb_info *bdb = (struct bdb_info *) be->be_private;
Howard Chu's avatar
Howard Chu committed
104
	struct stat stat1, stat2;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
105
	u_int32_t flags;
Howard Chu's avatar
Howard Chu committed
106
	char path[MAXPATHLEN];
Howard Chu's avatar
Howard Chu committed
107
	char *dbhome;
108
	Entry *e = NULL;
109
	int do_recover = 0, do_alock_recover = 0;
110
	int alockt, quick = 0;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
111

112
113
	if ( be->be_suffix == NULL ) {
		Debug( LDAP_DEBUG_ANY,
114
			LDAP_XSTRING(bdb_db_open) ": need suffix\n",
115
			1, 0, 0 );
116
117
118
		return -1;
	}

119
	Debug( LDAP_DEBUG_ARGS,
120
		LDAP_XSTRING(bdb_db_open) ": %s\n",
121
		be->be_suffix[0].bv_val, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
122

123
124
#ifndef BDB_MULTIPLE_SUFFIXES
	if ( be->be_suffix[1].bv_val ) {
125
126
127
128
129
		if (cr) {
			snprintf(cr->msg, sizeof(cr->msg), "only one suffix allowed");
			Debug( LDAP_DEBUG_ANY,
				LDAP_XSTRING(bdb_db_open) ": %s\n", cr->msg, 0, 0 );
		}
130
131
132
		return -1;
	}
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
133

Howard Chu's avatar
Howard Chu committed
134
135
136
	/* Check existence of dbenv_home. Any error means trouble */
	rc = stat( bdb->bi_dbenv_home, &stat1 );
	if( rc !=0 ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
137
		Debug( LDAP_DEBUG_ANY,
138
			LDAP_XSTRING(bdb_db_open) ": Cannot access database directory %s (%d)\n",
Howard Chu's avatar
Howard Chu committed
139
			bdb->bi_dbenv_home, errno, 0 );
140
		return -1;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
141
	}
Howard Chu's avatar
Howard Chu committed
142

Howard Chu's avatar
Howard Chu committed
143
	/* Perform database use arbitration/recovery logic */
144
145
146
147
148
149
	alockt = (slapMode & SLAP_TOOL_READONLY) ? ALOCK_LOCKED : ALOCK_UNIQUE;
	if ( slapMode & SLAP_TOOL_QUICK ) {
		alockt |= ALOCK_NOSAVE;
		quick = 1;
	}

Howard Chu's avatar
Howard Chu committed
150
151
	rc = alock_open( &bdb->bi_alock_info, 
				"slapd", 
152
153
154
155
156
				bdb->bi_dbenv_home, alockt );

	/* alockt is TRUE if the existing environment was created in Quick mode */
	alockt = (rc & ALOCK_NOSAVE) ? 1 : 0;
	rc &= ~ALOCK_NOSAVE;
Howard Chu's avatar
Howard Chu committed
157

158
159
	if( rc == ALOCK_RECOVER ) {
		Debug( LDAP_DEBUG_ANY,
160
			LDAP_XSTRING(bdb_db_open) ": unclean shutdown detected;"
Howard Chu's avatar
Howard Chu committed
161
162
			" attempting recovery.\n", 
			0, 0, 0 );
Howard Chu's avatar
Howard Chu committed
163
		do_alock_recover = 1;
164
		do_recover = DB_RECOVER;
165
166
	} else if( rc == ALOCK_BUSY ) {
		Debug( LDAP_DEBUG_ANY,
167
			LDAP_XSTRING(bdb_db_open) ": database already in use\n", 
Howard Chu's avatar
Howard Chu committed
168
			0, 0, 0 );
169
170
171
		return -1;
	} else if( rc != ALOCK_CLEAN ) {
		Debug( LDAP_DEBUG_ANY,
172
			LDAP_XSTRING(bdb_db_open) ": alock package is unstable\n", 
Howard Chu's avatar
Howard Chu committed
173
			0, 0, 0 );
174
175
		return -1;
	}
Howard Chu's avatar
Howard Chu committed
176

Howard Chu's avatar
Howard Chu committed
177
178
179
180
181
182
	/*
	 * The DB_CONFIG file may have changed. If so, recover the
	 * database so that new settings are put into effect. Also
	 * note the possible absence of DB_CONFIG in the log.
	 */
	if( stat( bdb->bi_db_config_path, &stat1 ) == 0 ) {
Howard Chu's avatar
Howard Chu committed
183
184
185
186
187
		if ( !do_recover ) {
			char *ptr = lutil_strcopy(path, bdb->bi_dbenv_home);
			*ptr++ = LDAP_DIRSEP[0];
			strcpy( ptr, "__db.001" );
			if( stat( path, &stat2 ) == 0 ) {
188
				if( stat2.st_mtime < stat1.st_mtime ) {
Howard Chu's avatar
Howard Chu committed
189
					Debug( LDAP_DEBUG_ANY,
190
						LDAP_XSTRING(bdb_db_open) ": DB_CONFIG for suffix %s has changed.\n"
Howard Chu's avatar
Howard Chu committed
191
192
						"Performing database recovery to activate new settings.\n",
						be->be_suffix[0].bv_val, 0, 0 );
193
					do_recover = DB_RECOVER;
Howard Chu's avatar
Howard Chu committed
194
195
196
197
198
199
				}
			}
		}
	}
	else {
		Debug( LDAP_DEBUG_ANY,
200
			LDAP_XSTRING(bdb_db_open) ": Warning - No DB_CONFIG file found "
Howard Chu's avatar
Howard Chu committed
201
202
203
204
205
			"in directory %s: (%d)\n"
			"Expect poor performance for suffix %s.\n",
			bdb->bi_dbenv_home, errno, be->be_suffix[0].bv_val );
	}

206
207
208
209
210
211
	/* Always let slapcat run, regardless of environment state.
	 * This can be used to cause a cache flush after an unclean
	 * shutdown.
	 */
	if ( do_recover && ( slapMode & SLAP_TOOL_READONLY )) {
		Debug( LDAP_DEBUG_ANY,
212
			LDAP_XSTRING(bdb_db_open) ": Recovery skipped in read-only mode. "
213
214
215
216
217
218
219
220
221
			"Run manual recovery if errors are encountered.\n",
			0, 0, 0 );
		do_recover = 0;
		quick = alockt;
	}

	/* An existing environment in Quick mode has nothing to recover. */
	if ( alockt && do_recover ) {
		Debug( LDAP_DEBUG_ANY,
222
			LDAP_XSTRING(bdb_db_open) ": cannot recover, database must be reinitialized.\n", 
223
224
225
226
227
			0, 0, 0 );
		rc = -1;
		goto fail;
	}

Howard Chu's avatar
Howard Chu committed
228
229
230
	rc = db_env_create( &bdb->bi_dbenv, 0 );
	if( rc != 0 ) {
		Debug( LDAP_DEBUG_ANY,
231
			LDAP_XSTRING(bdb_db_open) ": db_env_create failed: %s (%d)\n",
Howard Chu's avatar
Howard Chu committed
232
			db_strerror(rc), rc, 0 );
233
		goto fail;
Howard Chu's avatar
Howard Chu committed
234
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
235

Howard Chu's avatar
Howard Chu committed
236
237
238
239
240
241
242
243
#ifdef HAVE_EBCDIC
	strcpy( path, bdb->bi_dbenv_home );
	__atoe( path );
	dbhome = path;
#else
	dbhome = bdb->bi_dbenv_home;
#endif

244
245
246
247
	/* If existing environment is clean but doesn't support
	 * currently requested modes, remove it.
	 */
	if ( !do_recover && ( alockt ^ quick )) {
248
shm_retry:
249
250
251
		rc = bdb->bi_dbenv->remove( bdb->bi_dbenv, dbhome, DB_FORCE );
		if ( rc ) {
			Debug( LDAP_DEBUG_ANY,
252
				LDAP_XSTRING(bdb_db_open) ": dbenv remove failed: %s (%d)\n",
253
254
255
256
257
258
259
				db_strerror(rc), rc, 0 );
			bdb->bi_dbenv = NULL;
			goto fail;
		}
		rc = db_env_create( &bdb->bi_dbenv, 0 );
		if( rc != 0 ) {
			Debug( LDAP_DEBUG_ANY,
260
				LDAP_XSTRING(bdb_db_open) ": db_env_create failed: %s (%d)\n",
261
262
263
264
265
				db_strerror(rc), rc, 0 );
			goto fail;
		}
	}

266
	bdb->bi_dbenv->set_errpfx( bdb->bi_dbenv, be->be_suffix[0].bv_val );
267
	bdb->bi_dbenv->set_errcall( bdb->bi_dbenv, bdb_errcall );
Howard Chu's avatar
Howard Chu committed
268

269
	bdb->bi_dbenv->set_lk_detect( bdb->bi_dbenv, bdb->bi_lock_detect );
270

271
	/* One long-lived TXN per thread, two TXNs per write op */
272
	bdb->bi_dbenv->set_tx_max( bdb->bi_dbenv, connection_pool_max * 3 );
273

274
275
276
277
278
	if( bdb->bi_dbenv_xflags != 0 ) {
		rc = bdb->bi_dbenv->set_flags( bdb->bi_dbenv,
			bdb->bi_dbenv_xflags, 1);
		if( rc != 0 ) {
			Debug( LDAP_DEBUG_ANY,
279
				LDAP_XSTRING(bdb_db_open) ": dbenv_set_flags failed: %s (%d)\n",
280
				db_strerror(rc), rc, 0 );
281
			goto fail;
282
283
284
		}
	}

Howard Chu's avatar
Howard Chu committed
285
#define	BDB_TXN_FLAGS	(DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN)
Kurt Zeilenga's avatar
Kurt Zeilenga committed
286

Howard Chu's avatar
Howard Chu committed
287
	Debug( LDAP_DEBUG_TRACE,
288
		LDAP_XSTRING(bdb_db_open) ": dbenv_open(%s)\n",
Howard Chu's avatar
Howard Chu committed
289
290
		bdb->bi_dbenv_home, 0, 0);

291
	flags = DB_INIT_MPOOL | DB_CREATE | DB_THREAD;
Howard Chu's avatar
Howard Chu committed
292

293
294
	if ( !quick )
		flags |= BDB_TXN_FLAGS;
Howard Chu's avatar
Howard Chu committed
295

296
297
298
299
	/* If a key was set, use shared memory for the BDB environment */
	if ( bdb->bi_shm_key ) {
		bdb->bi_dbenv->set_shm_key( bdb->bi_dbenv, bdb->bi_shm_key );
		flags |= DB_SYSTEM_MEM;
Howard Chu's avatar
Howard Chu committed
300
	}
301
	rc = (bdb->bi_dbenv->open)( bdb->bi_dbenv, dbhome,
302
			flags | do_recover, bdb->bi_dbenv_mode );
Howard Chu's avatar
Howard Chu committed
303

304
	if ( rc ) {
305
306
307
308
309
310
311
312
313
314
315
316
317
		/* Regular open faied, probably a missing shm environment.
		 * Start over, do a recovery.
		 */
		if ( !do_recover && bdb->bi_shm_key ) {
			bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
			rc = db_env_create( &bdb->bi_dbenv, 0 );
			if( rc == 0 ) {
				Debug( LDAP_DEBUG_ANY, LDAP_XSTRING(bdb_db_open)
					": Shared memory env open failed, assuming stale env\n",
					0, 0, 0 );
				goto shm_retry;
			}
		}
318
		Debug( LDAP_DEBUG_ANY,
319
			LDAP_XSTRING(bdb_db_open) ": Database cannot be %s, err %d. "
320
321
322
			"Restore from backup!\n",
				do_recover ? "recovered" : "opened", rc, 0);
		goto fail;
Howard Chu's avatar
Howard Chu committed
323
324
325
	}

	if ( do_alock_recover && alock_recover (&bdb->bi_alock_info) != 0 ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
326
		Debug( LDAP_DEBUG_ANY,
327
			LDAP_XSTRING(bdb_db_open) ": alock_recover failed\n",
Howard Chu's avatar
Howard Chu committed
328
			0, 0, 0 );
329
330
		rc = -1;
		goto fail;
Howard Chu's avatar
Howard Chu committed
331
332
333
334
335
	}

#ifdef SLAP_ZONE_ALLOC
	if ( bdb->bi_cache.c_maxsize ) {
		bdb->bi_cache.c_zctx = slap_zn_mem_create(
Kurt Zeilenga's avatar
Kurt Zeilenga committed
336
337
			SLAP_ZONE_INITSIZE, SLAP_ZONE_MAXSIZE,
			SLAP_ZONE_DELTA, SLAP_ZONE_SIZE);
Howard Chu's avatar
Howard Chu committed
338
339
340
341
342
343
	}
#endif

	if ( bdb->bi_idl_cache_max_size ) {
		bdb->bi_idl_tree = NULL;
		bdb->bi_idl_cache_size = 0;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
344
345
	}

346
	flags = DB_THREAD | bdb->bi_db_opflags;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
347

348
#ifdef DB_AUTO_COMMIT
349
	if ( !quick )
350
351
352
		flags |= DB_AUTO_COMMIT;
#endif

353
354
355
	bdb->bi_databases = (struct bdb_db_info **) ch_malloc(
		BDB_INDICES * sizeof(struct bdb_db_info *) );

Kurt Zeilenga's avatar
Kurt Zeilenga committed
356
	/* open (and create) main database */
357
	for( i = 0; bdbi_databases[i].name; i++ ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
358
359
360
361
362
363
364
		struct bdb_db_info *db;

		db = (struct bdb_db_info *) ch_calloc(1, sizeof(struct bdb_db_info));

		rc = db_create( &db->bdi_db, bdb->bi_dbenv, 0 );
		if( rc != 0 ) {
			Debug( LDAP_DEBUG_ANY,
365
				LDAP_XSTRING(bdb_db_open) ": db_create(%s) failed: %s (%d)\n",
Kurt Zeilenga's avatar
Kurt Zeilenga committed
366
				bdb->bi_dbenv_home, db_strerror(rc), rc );
367
			goto fail;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
368
369
		}

370
		if( i == BDB_ID2ENTRY ) {
371
372
373
374
			if ( slapMode & SLAP_TOOL_MODE )
				db->bdi_db->mpf->set_priority( db->bdi_db->mpf,
					DB_PRIORITY_VERY_LOW );

375
376
			rc = db->bdi_db->set_pagesize( db->bdi_db,
				BDB_ID2ENTRY_PAGESIZE );
377
378
379
380
381
			if ( slapMode & SLAP_TOOL_READMAIN ) {
				flags |= DB_RDONLY;
			} else {
				flags |= DB_CREATE;
			}
382
		} else {
383
			rc = db->bdi_db->set_flags( db->bdi_db, 
384
				DB_DUP | DB_DUPSORT );
Howard Chu's avatar
Howard Chu committed
385
#ifndef BDB_HIER
386
387
388
389
390
			if ( slapMode & SLAP_TOOL_READONLY ) {
				flags |= DB_RDONLY;
			} else {
				flags |= DB_CREATE;
			}
Howard Chu's avatar
Howard Chu committed
391
#else
392
393
394
395
396
			if ( slapMode & (SLAP_TOOL_READONLY|SLAP_TOOL_READMAIN) ) {
				flags |= DB_RDONLY;
			} else {
				flags |= DB_CREATE;
			}
Howard Chu's avatar
Howard Chu committed
397
#endif
398
399
400
401
			rc = db->bdi_db->set_pagesize( db->bdi_db,
				BDB_PAGESIZE );
		}

Howard Chu's avatar
Howard Chu committed
402
403
404
#ifdef HAVE_EBCDIC
		strcpy( path, bdbi_databases[i].file );
		__atoe( path );
Howard Chu's avatar
Howard Chu committed
405
		rc = DB_OPEN( db->bdi_db,
Howard Chu's avatar
Howard Chu committed
406
407
408
			path,
		/*	bdbi_databases[i].name, */ NULL,
			bdbi_databases[i].type,
Howard Chu's avatar
Howard Chu committed
409
			bdbi_databases[i].flags | flags,
Howard Chu's avatar
Howard Chu committed
410
411
			bdb->bi_dbenv_mode );
#else
Howard Chu's avatar
Howard Chu committed
412
		rc = DB_OPEN( db->bdi_db,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
413
			bdbi_databases[i].file,
414
		/*	bdbi_databases[i].name, */ NULL,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
415
			bdbi_databases[i].type,
Howard Chu's avatar
Howard Chu committed
416
			bdbi_databases[i].flags | flags,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
417
			bdb->bi_dbenv_mode );
Howard Chu's avatar
Howard Chu committed
418
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
419

420
421
422
423
424
		if ( rc != 0 ) {
			char	buf[SLAP_TEXT_BUFLEN];

			snprintf( buf, sizeof(buf), "%s/%s", 
				bdb->bi_dbenv_home, bdbi_databases[i].file );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
425
			Debug( LDAP_DEBUG_ANY,
426
				LDAP_XSTRING(bdb_db_open) ": db_open(%s) failed: %s (%d)\n",
427
				buf, db_strerror(rc), rc );
428
429
			db->bdi_db->close( db->bdi_db, 0 );
			goto fail;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
430
		}
431

432
		flags &= ~(DB_CREATE | DB_RDONLY);
433
		db->bdi_name = bdbi_databases[i].name;
434
		bdb->bi_databases[i] = db;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
435
436
	}

437
438
439
	bdb->bi_databases[i] = NULL;
	bdb->bi_ndatabases = i;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
440
441
442
443
	/* get nextid */
	rc = bdb_last_id( be, NULL );
	if( rc != 0 ) {
		Debug( LDAP_DEBUG_ANY,
444
			LDAP_XSTRING(bdb_db_open) ": last_id(%s) failed: %s (%d)\n",
Kurt Zeilenga's avatar
Kurt Zeilenga committed
445
			bdb->bi_dbenv_home, db_strerror(rc), rc );
446
		goto fail;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
447
448
	}

449
450
451
452
	if ( !quick ) {
		XLOCK_ID(bdb->bi_dbenv, &bdb->bi_cache.c_locker);
	}

453
454
455
456
457
458
459
460
461
462
	entry_prealloc( bdb->bi_cache.c_maxsize );
	attr_prealloc( bdb->bi_cache.c_maxsize * 20 );

	/* setup for empty-DN contexts */
	if ( BER_BVISEMPTY( &be->be_nsuffix[0] )) {
		rc = bdb_id2entry( be, NULL, 0, 0, &e );
	}
	if ( !e ) {
		e = entry_alloc();
		e->e_id = 0;
Howard Chu's avatar
Howard Chu committed
463
464
		ber_dupbv( &e->e_name, (struct berval *)&slap_empty_bv );
		ber_dupbv( &e->e_nname, (struct berval *)&slap_empty_bv );
465
	}
466
	e->e_ocflags = SLAP_OC_GLUE|SLAP_OC__END;
467
468
469
	e->e_private = &bdb->bi_cache.c_dntree;
	bdb->bi_cache.c_dntree.bei_e = e;

470
	/* monitor setup */
Pierangelo Masarati's avatar
Pierangelo Masarati committed
471
	rc = bdb_monitor_db_open( be );
472
473
474
475
	if ( rc != 0 ) {
		goto fail;
	}

Howard Chu's avatar
Howard Chu committed
476
	bdb->bi_flags |= BDB_IS_OPEN;
477

Kurt Zeilenga's avatar
Kurt Zeilenga committed
478
	return 0;
479
480

fail:
481
	bdb_db_close( be, NULL );
482
	return rc;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
483
484
}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
485
static int
486
bdb_db_close( BackendDB *be, ConfigReply *cr )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
487
488
{
	int rc;
489
	struct bdb_info *bdb = (struct bdb_info *) be->be_private;
Howard Chu's avatar
Howard Chu committed
490
	struct bdb_db_info *db;
491
	bdb_idl_cache_entry_t *entry, *next_entry;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
492

493
	/* monitor handling */
Pierangelo Masarati's avatar
Pierangelo Masarati committed
494
	(void)bdb_monitor_db_close( be );
495

496
497
498
499
500
501
502
503
504
	{
		Entry *e = bdb->bi_cache.c_dntree.bei_e;
		if ( e ) {
			bdb->bi_cache.c_dntree.bei_e = NULL;
			e->e_private = NULL;
			bdb_entry_return( e );
		}
	}

Howard Chu's avatar
Howard Chu committed
505
	bdb->bi_flags &= ~BDB_IS_OPEN;
506
507

	ber_bvarray_free( bdb->bi_db_config );
508
	bdb->bi_db_config = NULL;
509

Howard Chu's avatar
Howard Chu committed
510
	while( bdb->bi_databases && bdb->bi_ndatabases-- ) {
Howard Chu's avatar
Howard Chu committed
511
512
		db = bdb->bi_databases[bdb->bi_ndatabases];
		rc = db->bdi_db->close( db->bdi_db, 0 );
513
514
		/* Lower numbered names are not strdup'd */
		if( bdb->bi_ndatabases >= BDB_NDB )
Howard Chu's avatar
Howard Chu committed
515
516
			free( db->bdi_name );
		free( db );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
517
	}
Howard Chu's avatar
Howard Chu committed
518
	free( bdb->bi_databases );
519
520
	bdb->bi_databases = NULL;

521
522
	bdb_cache_release_all (&bdb->bi_cache);

Howard Chu's avatar
Howard Chu committed
523
	if ( bdb->bi_idl_cache_size ) {
524
		avl_free( bdb->bi_idl_tree, NULL );
525
		bdb->bi_idl_tree = NULL;
Howard Chu's avatar
Howard Chu committed
526
527
528
529
530
531
532
533
534
535
		entry = bdb->bi_idl_lru_head;
		do {
			next_entry = entry->idl_lru_next;
			if ( entry->idl )
				free( entry->idl );
			free( entry->kstr.bv_val );
			free( entry );
			entry = next_entry;
		} while ( entry != bdb->bi_idl_lru_head );
		bdb->bi_idl_lru_head = bdb->bi_idl_lru_tail = NULL;
536
537
	}

538
	/* close db environment */
539
	if( bdb->bi_dbenv ) {
540
541
542
543
544
		/* Free cache locker if we enabled locking */
		if ( !( slapMode & SLAP_TOOL_QUICK )) {
			XLOCK_ID_FREE(bdb->bi_dbenv, bdb->bi_cache.c_locker);
			bdb->bi_cache.c_locker = 0;
		}
545
546
547
#ifdef BDB_REUSE_LOCKERS
		bdb_locker_flush( bdb->bi_dbenv );
#endif
548
549
550
551
		/* force a checkpoint, but not if we were ReadOnly,
		 * and not in Quick mode since there are no transactions there.
		 */
		if ( !( slapMode & ( SLAP_TOOL_QUICK|SLAP_TOOL_READONLY ))) {
552
553
554
			rc = TXN_CHECKPOINT( bdb->bi_dbenv, 0, 0, DB_FORCE );
			if( rc != 0 ) {
				Debug( LDAP_DEBUG_ANY,
555
					"bdb_db_close: txn_checkpoint failed: %s (%d)\n",
556
557
					db_strerror(rc), rc, 0 );
			}
558
559
		}

560
561
562
563
		rc = bdb->bi_dbenv->close( bdb->bi_dbenv, 0 );
		bdb->bi_dbenv = NULL;
		if( rc != 0 ) {
			Debug( LDAP_DEBUG_ANY,
564
				"bdb_db_close: close failed: %s (%d)\n",
565
566
567
				db_strerror(rc), rc, 0 );
			return rc;
		}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
568
569
	}

570
571
572
573
574
	rc = alock_close( &bdb->bi_alock_info );
	if( rc != 0 ) {
		Debug( LDAP_DEBUG_ANY,
			"bdb_db_close: alock_close failed\n", 0, 0, 0 );
		return -1;
575
576
	}

577
578
579
580
	return 0;
}

static int
581
bdb_db_destroy( BackendDB *be, ConfigReply *cr )
582
583
584
{
	struct bdb_info *bdb = (struct bdb_info *) be->be_private;

Pierangelo Masarati's avatar
Pierangelo Masarati committed
585
	/* monitor handling */
586
	(void)bdb_monitor_db_destroy( be );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
587

Howard Chu's avatar
Howard Chu committed
588
	if( bdb->bi_dbenv_home ) ch_free( bdb->bi_dbenv_home );
589
	if( bdb->bi_db_config_path ) ch_free( bdb->bi_db_config_path );
Howard Chu's avatar
Howard Chu committed
590

591
	bdb_attr_index_destroy( bdb );
Howard Chu's avatar
Howard Chu committed
592

593
	ldap_pvt_thread_rdwr_destroy ( &bdb->bi_cache.c_rwlock );
594
	ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_lru_mutex );
595
596
	ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_count_mutex );
	ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_eifree_mutex );
Howard Chu's avatar
Howard Chu committed
597
	ldap_pvt_thread_mutex_destroy( &bdb->bi_cache.c_dntree.bei_kids_mutex );
598
599
600
#ifdef BDB_HIER
	ldap_pvt_thread_mutex_destroy( &bdb->bi_modrdns_mutex );
#endif
601
	ldap_pvt_thread_mutex_destroy( &bdb->bi_lastid_mutex );
Howard Chu's avatar
Howard Chu committed
602
	ldap_pvt_thread_mutex_destroy( &bdb->bi_database_mutex );
603
604
	ldap_pvt_thread_rdwr_destroy( &bdb->bi_idl_tree_rwlock );
	ldap_pvt_thread_mutex_destroy( &bdb->bi_idl_tree_lrulock );
605

Howard Chu's avatar
Howard Chu committed
606
607
608
	ch_free( bdb );
	be->be_private = NULL;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
609
610
	return 0;
}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
611
612

int
613
bdb_back_initialize(
614
	BackendInfo	*bi )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
615
{
616
617
	int rc;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
618
	static char *controls[] = {
619
		LDAP_CONTROL_ASSERT,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
620
621
		LDAP_CONTROL_MANAGEDSAIT,
		LDAP_CONTROL_NOOP,
622
		LDAP_CONTROL_PAGEDRESULTS,
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
623
624
		LDAP_CONTROL_PRE_READ,
		LDAP_CONTROL_POST_READ,
625
		LDAP_CONTROL_SUBENTRIES,
626
		LDAP_CONTROL_X_PERMISSIVE_MODIFY,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
627
628
629
#ifdef LDAP_X_TXN
		LDAP_CONTROL_X_TXN_SPEC,
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
630
631
632
633
		NULL
	};

	/* initialize the underlying database system */
634
635
636
	Debug( LDAP_DEBUG_TRACE,
		LDAP_XSTRING(bdb_back_initialize) ": initialize " 
		BDB_UCTYPE " backend\n", 0, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
637

638
639
640
641
642
643
644
645
	bi->bi_flags |=
		SLAP_BFLAG_INCREMENT |
		SLAP_BFLAG_SUBENTRIES |
		SLAP_BFLAG_ALIASES |
		SLAP_BFLAG_REFERRALS;

	bi->bi_controls = controls;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
646
	{	/* version check */
647
		int major, minor, patch, ver;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
648
		char *version = db_version( &major, &minor, &patch );
Howard Chu's avatar
Howard Chu committed
649
650
651
652
653
654
655
656
657
658
659
#ifdef HAVE_EBCDIC
		char v2[1024];

		/* All our stdio does an ASCII to EBCDIC conversion on
		 * the output. Strings from the BDB library are already
		 * in EBCDIC; we have to go back and forth...
		 */
		strcpy( v2, version );
		__etoa( v2 );
		version = v2;
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
660

661
		ver = (major << 24) | (minor << 16) | patch;
662
663
664
		if( ver != DB_VERSION_FULL ) {
			/* fail if a versions don't match */
			Debug( LDAP_DEBUG_ANY,
665
				LDAP_XSTRING(bdb_back_initialize) ": "
Pierangelo Masarati's avatar
Pierangelo Masarati committed
666
				"BDB library version mismatch:"
667
668
				" expected " DB_VERSION_STRING ","
				" got %s\n", version, 0, 0 );
669
			return -1;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
670
671
		}

672
		Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(bdb_back_initialize)
673
			": %s\n", version, 0, 0 );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
674
675
	}

Howard Chu's avatar
Howard Chu committed
676
677
678
	db_env_set_func_free( ber_memfree );
	db_env_set_func_malloc( (db_malloc *)ber_memalloc );
	db_env_set_func_realloc( (db_realloc *)ber_memrealloc );
679
680
681
682
#ifndef NO_THREAD
	/* This is a no-op on a NO_THREAD build. Leave the default
	 * alone so that BDB will sleep on interprocess conflicts.
	 */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
683
	db_env_set_func_yield( ldap_pvt_thread_yield );
684
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
685
686

	bi->bi_open = 0;
687
	bi->bi_close = 0;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
688
	bi->bi_config = 0;
689
	bi->bi_destroy = 0;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
690

Kurt Zeilenga's avatar
Kurt Zeilenga committed
691
	bi->bi_db_init = bdb_db_init;
692
	bi->bi_db_config = config_generic_wrapper;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
693
694
695
	bi->bi_db_open = bdb_db_open;
	bi->bi_db_close = bdb_db_close;
	bi->bi_db_destroy = bdb_db_destroy;
696

Kurt Zeilenga's avatar
Kurt Zeilenga committed
697
	bi->bi_op_add = bdb_add;
698
	bi->bi_op_bind = bdb_bind;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
699
700
	bi->bi_op_compare = bdb_compare;
	bi->bi_op_delete = bdb_delete;
701
702
	bi->bi_op_modify = bdb_modify;
	bi->bi_op_modrdn = bdb_modrdn;
703
704
	bi->bi_op_search = bdb_search;

Howard Chu's avatar
Howard Chu committed
705
	bi->bi_op_unbind = 0;
706

707
	bi->bi_extended = bdb_extended;
Howard Chu's avatar
Howard Chu committed
708

709
	bi->bi_chk_referrals = bdb_referrals;
710
	bi->bi_operational = bdb_operational;
711
	bi->bi_has_subordinates = bdb_hasSubordinates;
712
	bi->bi_entry_release_rw = bdb_entry_release;
713
	bi->bi_entry_get_rw = bdb_entry_get;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
714
715
716
717

	/*
	 * hooks for slap tools
	 */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
718
719
720
721
722
723
	bi->bi_tool_entry_open = bdb_tool_entry_open;
	bi->bi_tool_entry_close = bdb_tool_entry_close;
	bi->bi_tool_entry_first = bdb_tool_entry_next;
	bi->bi_tool_entry_next = bdb_tool_entry_next;
	bi->bi_tool_entry_get = bdb_tool_entry_get;
	bi->bi_tool_entry_put = bdb_tool_entry_put;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
724
	bi->bi_tool_entry_reindex = bdb_tool_entry_reindex;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
725
	bi->bi_tool_sync = 0;
726
727
728
	bi->bi_tool_dn2id_get = bdb_tool_dn2id_get;
	bi->bi_tool_id2entry_get = bdb_tool_id2entry_get;
	bi->bi_tool_entry_modify = bdb_tool_entry_modify;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
729
730
731
732

	bi->bi_connection_init = 0;
	bi->bi_connection_destroy = 0;

733
	rc = bdb_back_init_cf( bi );
734
735

	return rc;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
736
}
737
738
739
740

#if	(SLAPD_BDB == SLAPD_MOD_DYNAMIC && !defined(BDB_HIER)) || \
	(SLAPD_HDB == SLAPD_MOD_DYNAMIC && defined(BDB_HIER))

Pierangelo Masarati's avatar
Pierangelo Masarati committed
741
/* conditionally define the init_module() function */
742
#ifdef BDB_HIER
Pierangelo Masarati's avatar
Pierangelo Masarati committed
743
744
745
746
SLAP_BACKEND_INIT_MODULE( hdb )
#else /* !BDB_HIER */
SLAP_BACKEND_INIT_MODULE( bdb )
#endif /* !BDB_HIER */
747

Pierangelo Masarati's avatar
Pierangelo Masarati committed
748
#endif /* SLAPD_[BH]DB == SLAPD_MOD_DYNAMIC */
749