cache.c 36.3 KB
Newer Older
1
2
/* cache.c - routines to maintain an in-core cache of entries */
/* $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-2008 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>.
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 */

#include "portable.h"

#include <stdio.h>

#include <ac/errno.h>
#include <ac/string.h>
#include <ac/socket.h>

#include "slap.h"

#include "back-bdb.h"

29
30
#include "ldap_rq.h"

Howard Chu's avatar
Howard Chu committed
31
#ifdef BDB_HIER
32
#define bdb_cache_lru_purge	hdb_cache_lru_purge
Howard Chu's avatar
Howard Chu committed
33
#endif
34
static void bdb_cache_lru_purge( struct bdb_info *bdb );
Howard Chu's avatar
Howard Chu committed
35
36

static int	bdb_cache_delete_internal(Cache *cache, EntryInfo *e, int decr);
37
#ifdef LDAP_DEBUG
38
#define SLAPD_UNUSED
Pierangelo Masarati's avatar
Pierangelo Masarati committed
39
#ifdef SLAPD_UNUSED
40
static void	bdb_lru_print(Cache *cache);
41
#endif
Pierangelo Masarati's avatar
Pierangelo Masarati committed
42
#endif
43

44
45
46
47
48
49
50
51
52
53
54
55
/* For concurrency experiments only! */
#if 0
#define	ldap_pvt_thread_rdwr_wlock(a)	0
#define	ldap_pvt_thread_rdwr_wunlock(a)	0
#define	ldap_pvt_thread_rdwr_rlock(a)	0
#define	ldap_pvt_thread_rdwr_runlock(a)	0
#endif

#if 0
#define ldap_pvt_thread_mutex_trylock(a) 0
#endif

Howard Chu's avatar
Howard Chu committed
56
static EntryInfo *
Howard Chu's avatar
Howard Chu committed
57
bdb_cache_entryinfo_new( Cache *cache )
58
{
Howard Chu's avatar
Howard Chu committed
59
	EntryInfo *ei = NULL;
60

Howard Chu's avatar
Howard Chu committed
61
	if ( cache->c_eifree ) {
62
		ldap_pvt_thread_mutex_lock( &cache->c_eifree_mutex );
Howard Chu's avatar
Howard Chu committed
63
64
65
		if ( cache->c_eifree ) {
			ei = cache->c_eifree;
			cache->c_eifree = ei->bei_lrunext;
66
			ei->bei_finders = 0;
Howard Chu's avatar
Howard Chu committed
67
		}
68
		ldap_pvt_thread_mutex_unlock( &cache->c_eifree_mutex );
Howard Chu's avatar
Howard Chu committed
69
	}
70
71
	if ( !ei ) {
		ei = ch_calloc(1, sizeof(EntryInfo));
Howard Chu's avatar
Howard Chu committed
72
73
		ldap_pvt_thread_mutex_init( &ei->bei_kids_mutex );
	}
74

75
76
	ei->bei_state = CACHE_ENTRY_REFERENCED;

Howard Chu's avatar
Howard Chu committed
77
	return ei;
78
79
}

80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
static void
bdb_cache_entryinfo_free( Cache *cache, EntryInfo *ei )
{
	free( ei->bei_nrdn.bv_val );
	ei->bei_nrdn.bv_val = NULL;
#ifdef BDB_HIER
	free( ei->bei_rdn.bv_val );
	ei->bei_rdn.bv_val = NULL;
	ei->bei_modrdns = 0;
	ei->bei_ckids = 0;
	ei->bei_dkids = 0;
#endif
	ei->bei_parent = NULL;
	ei->bei_kids = NULL;
	ei->bei_lruprev = NULL;

	ldap_pvt_thread_mutex_lock( &cache->c_eifree_mutex );
	ei->bei_lrunext = cache->c_eifree;
	cache->c_eifree = ei;
	ldap_pvt_thread_mutex_unlock( &cache->c_eifree_mutex );
}

102
103
104
105
106
107
108
109
#define LRU_DEL( c, e ) do { \
	if ( e == (c)->c_lruhead ) (c)->c_lruhead = e->bei_lruprev; \
	if ( e == (c)->c_lrutail ) (c)->c_lrutail = e->bei_lruprev; \
	e->bei_lrunext->bei_lruprev = e->bei_lruprev; \
	e->bei_lruprev->bei_lrunext = e->bei_lrunext; \
	e->bei_lruprev = NULL; \
} while ( 0 )

110
111
112
113
114
115
116
117
118
/* Note - we now use a Second-Chance / Clock algorithm instead of
 * Least-Recently-Used. This tremendously improves concurrency
 * because we no longer need to manipulate the lists every time an
 * entry is touched. We only need to lock the lists when adding
 * or deleting an entry. It's now a circular doubly-linked list.
 * We always append to the tail, but the head traverses the circle
 * during a purge operation.
 */
static void
Howard Chu's avatar
Howard Chu committed
119
bdb_cache_lru_link( struct bdb_info *bdb, EntryInfo *ei )
120
{
121

122
123
124
125
	/* Already linked, ignore */
	if ( ei->bei_lruprev )
		return;

126
	/* Insert into circular LRU list */
Howard Chu's avatar
Howard Chu committed
127
	ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_lru_mutex );
128

Howard Chu's avatar
Howard Chu committed
129
130
131
132
	ei->bei_lruprev = bdb->bi_cache.c_lrutail;
	if ( bdb->bi_cache.c_lrutail ) {
		ei->bei_lrunext = bdb->bi_cache.c_lrutail->bei_lrunext;
		bdb->bi_cache.c_lrutail->bei_lrunext = ei;
133
134
135
136
		if ( ei->bei_lrunext )
			ei->bei_lrunext->bei_lruprev = ei;
	} else {
		ei->bei_lrunext = ei->bei_lruprev = ei;
Howard Chu's avatar
Howard Chu committed
137
		bdb->bi_cache.c_lruhead = ei;
138
	}
Howard Chu's avatar
Howard Chu committed
139
140
	bdb->bi_cache.c_lrutail = ei;
	ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_lru_mutex );
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
}

#ifdef NO_THREADS
#define NO_DB_LOCK
#endif

/* #define NO_DB_LOCK 1 */
/* Note: The BerkeleyDB locks are much slower than regular
 * mutexes or rdwr locks. But the BDB implementation has the
 * advantage of using a fixed size lock table, instead of
 * allocating a lock object per entry in the DB. That's a
 * key benefit for scaling. It also frees us from worrying
 * about undetectable deadlocks between BDB activity and our
 * own cache activity. It's still worth exploring faster
 * alternatives though.
 */

Howard Chu's avatar
Howard Chu committed
158
/* Atomically release and reacquire a lock */
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
159
int
Howard Chu's avatar
Howard Chu committed
160
bdb_cache_entry_db_relock(
Howard Chu's avatar
Howard Chu committed
161
	struct bdb_info *bdb,
162
	BDB_LOCKER locker,
Howard Chu's avatar
Howard Chu committed
163
164
165
166
	EntryInfo *ei,
	int rw,
	int tryOnly,
	DB_LOCK *lock )
167
{
168
#ifdef NO_DB_LOCK
Howard Chu's avatar
Howard Chu committed
169
	return 0;
170
#else
Howard Chu's avatar
Howard Chu committed
171
172
173
	int	rc;
	DBT	lockobj;
	DB_LOCKREQ list[2];
174

Howard Chu's avatar
Howard Chu committed
175
176
	if ( !lock ) return 0;

177
	lockobj.data = &ei->bei_id;
178
	lockobj.size = sizeof(ei->bei_id) + 1;
179

Howard Chu's avatar
Howard Chu committed
180
181
182
183
184
185
	list[0].op = DB_LOCK_PUT;
	list[0].lock = *lock;
	list[1].op = DB_LOCK_GET;
	list[1].lock = *lock;
	list[1].mode = rw ? DB_LOCK_WRITE : DB_LOCK_READ;
	list[1].obj = &lockobj;
186
	rc = bdb->bi_dbenv->lock_vec(bdb->bi_dbenv, BDB_LOCKID(locker), tryOnly ? DB_LOCK_NOWAIT : 0,
Howard Chu's avatar
Howard Chu committed
187
		list, 2, NULL );
188

189
	if (rc && !tryOnly) {
Howard Chu's avatar
Howard Chu committed
190
		Debug( LDAP_DEBUG_TRACE,
191
			"bdb_cache_entry_db_relock: entry %ld, rw %d, rc %d\n",
Howard Chu's avatar
Howard Chu committed
192
193
194
			ei->bei_id, rw, rc );
	} else {
		*lock = list[1].lock;
195
	}
Howard Chu's avatar
Howard Chu committed
196
	return rc;
Howard Chu's avatar
Howard Chu committed
197
#endif
198
}
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
199

200
static int
201
bdb_cache_entry_db_lock( struct bdb_info *bdb, BDB_LOCKER locker, EntryInfo *ei,
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
202
	int rw, int tryOnly, DB_LOCK *lock )
203
{
204
#ifdef NO_DB_LOCK
Howard Chu's avatar
Howard Chu committed
205
206
	return 0;
#else
207
208
209
210
	int       rc;
	DBT       lockobj;
	int       db_rw;

Howard Chu's avatar
Howard Chu committed
211
212
	if ( !lock ) return 0;

213
214
215
216
217
	if (rw)
		db_rw = DB_LOCK_WRITE;
	else
		db_rw = DB_LOCK_READ;

218
	lockobj.data = &ei->bei_id;
219
	lockobj.size = sizeof(ei->bei_id) + 1;
Howard Chu's avatar
Howard Chu committed
220

221
	rc = LOCK_GET(bdb->bi_dbenv, BDB_LOCKID(locker), tryOnly ? DB_LOCK_NOWAIT : 0,
222
					&lockobj, db_rw, lock);
223
	if (rc && !tryOnly) {
224
		Debug( LDAP_DEBUG_TRACE,
225
			"bdb_cache_entry_db_lock: entry %ld, rw %d, rc %d\n",
Howard Chu's avatar
Howard Chu committed
226
			ei->bei_id, rw, rc );
227
	}
228
	return rc;
229
#endif /* NO_DB_LOCK */
230
231
232
}

int
Howard Chu's avatar
Howard Chu committed
233
bdb_cache_entry_db_unlock ( struct bdb_info *bdb, DB_LOCK *lock )
234
{
235
#ifdef NO_DB_LOCK
Howard Chu's avatar
Howard Chu committed
236
237
	return 0;
#else
238
239
	int rc;

240
	if ( !lock || lock->mode == DB_LOCK_NG ) return 0;
241

Howard Chu's avatar
Howard Chu committed
242
	rc = LOCK_PUT ( bdb->bi_dbenv, lock );
243
	return rc;
Howard Chu's avatar
Howard Chu committed
244
#endif
245
246
}

247
248
249
250
251
252
253
254
void
bdb_cache_return_entry_rw( struct bdb_info *bdb, Entry *e,
	int rw, DB_LOCK *lock )
{
	EntryInfo *ei;
	int free = 0;

	ei = e->e_private;
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
255
256
257
258
259
260
261
262
263
	bdb_cache_entry_db_unlock( bdb, lock );
	if ( ei ) {
		bdb_cache_entryinfo_lock( ei );
		if ( ei->bei_state & CACHE_ENTRY_NOT_CACHED ) {
			ei->bei_e = NULL;
			ei->bei_state ^= CACHE_ENTRY_NOT_CACHED;
			free = 1;
		}
		bdb_cache_entryinfo_unlock( ei );
264
265
266
267
268
269
270
	}
	if ( free ) {
		e->e_private = NULL;
		bdb_entry_return( e );
	}
}

271
static int
Howard Chu's avatar
Howard Chu committed
272
bdb_cache_entryinfo_destroy( EntryInfo *e )
273
{
Howard Chu's avatar
Howard Chu committed
274
275
	ldap_pvt_thread_mutex_destroy( &e->bei_kids_mutex );
	free( e->bei_nrdn.bv_val );
Howard Chu's avatar
Howard Chu committed
276
277
278
#ifdef BDB_HIER
	free( e->bei_rdn.bv_val );
#endif
Howard Chu's avatar
Howard Chu committed
279
	free( e );
280
281
282
	return 0;
}

283
/* Do a length-ordered sort on normalized RDNs */
Howard Chu's avatar
Howard Chu committed
284
285
static int
bdb_rdn_cmp( const void *v_e1, const void *v_e2 )
286
{
Howard Chu's avatar
Howard Chu committed
287
	const EntryInfo *e1 = v_e1, *e2 = v_e2;
288
	int rc = e1->bei_nrdn.bv_len - e2->bei_nrdn.bv_len;
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
289
290
291
292
	if (rc == 0) {
		rc = strncmp( e1->bei_nrdn.bv_val, e2->bei_nrdn.bv_val,
			e1->bei_nrdn.bv_len );
	}
Howard Chu's avatar
Howard Chu committed
293
294
	return rc;
}
295

Howard Chu's avatar
Howard Chu committed
296
297
298
299
300
static int
bdb_id_cmp( const void *v_e1, const void *v_e2 )
{
	const EntryInfo *e1 = v_e1, *e2 = v_e2;
	return e1->bei_id - e2->bei_id;
301
302
}

303
304
305
306
static int
bdb_id_dup_err( void *v1, void *v2 )
{
	EntryInfo *e2 = v2;
Howard Chu's avatar
Howard Chu committed
307
	e2->bei_lrunext = v1;
308
309
310
	return -1;
}

Howard Chu's avatar
Howard Chu committed
311
/* Create an entryinfo in the cache. Caller must release the locks later.
312
 */
313
static int
Howard Chu's avatar
Howard Chu committed
314
315
bdb_entryinfo_add_internal(
	struct bdb_info *bdb,
Howard Chu's avatar
Howard Chu committed
316
	EntryInfo *ei,
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
317
	EntryInfo **res )
318
{
Howard Chu's avatar
Howard Chu committed
319
	EntryInfo *ei2 = NULL;
320

Howard Chu's avatar
Howard Chu committed
321
	*res = NULL;
322

Howard Chu's avatar
Howard Chu committed
323
	ei2 = bdb_cache_entryinfo_new( &bdb->bi_cache );
324

Howard Chu's avatar
Howard Chu committed
325
	ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
Howard Chu's avatar
Howard Chu committed
326
	bdb_cache_entryinfo_lock( ei->bei_parent );
327

Howard Chu's avatar
Howard Chu committed
328
329
	ei2->bei_id = ei->bei_id;
	ei2->bei_parent = ei->bei_parent;
Howard Chu's avatar
Howard Chu committed
330
#ifdef BDB_HIER
Howard Chu's avatar
Howard Chu committed
331
	ei2->bei_rdn = ei->bei_rdn;
Howard Chu's avatar
Howard Chu committed
332
#endif
333
334
335
#ifdef SLAP_ZONE_ALLOC
	ei2->bei_bdb = bdb;
#endif
Howard Chu's avatar
Howard Chu committed
336
337

	/* Add to cache ID tree */
338
339
	if (avl_insert( &bdb->bi_cache.c_idtree, ei2, bdb_id_cmp,
		bdb_id_dup_err )) {
Howard Chu's avatar
Howard Chu committed
340
		EntryInfo *eix = ei2->bei_lrunext;
341
		bdb_cache_entryinfo_free( &bdb->bi_cache, ei2 );
Howard Chu's avatar
Howard Chu committed
342
		ei2 = eix;
Howard Chu's avatar
Howard Chu committed
343
#ifdef BDB_HIER
Howard Chu's avatar
Howard Chu committed
344
345
346
347
		/* It got freed above because its value was
		 * assigned to ei2.
		 */
		ei->bei_rdn.bv_val = NULL;
Howard Chu's avatar
Howard Chu committed
348
#endif
Howard Chu's avatar
Howard Chu committed
349
	} else {
Howard Chu's avatar
Howard Chu committed
350
351
		int rc;

Howard Chu's avatar
Howard Chu committed
352
		bdb->bi_cache.c_eiused++;
Howard Chu's avatar
Howard Chu committed
353
		ber_dupbv( &ei2->bei_nrdn, &ei->bei_nrdn );
354
355
356
357
358
359
360

		/* This is a new leaf node. But if parent had no kids, then it was
		 * a leaf and we would be decrementing that. So, only increment if
		 * the parent already has kids.
		 */
		if ( ei->bei_parent->bei_kids || !ei->bei_parent->bei_id )
			bdb->bi_cache.c_leaves++;
Howard Chu's avatar
Howard Chu committed
361
		rc = avl_insert( &ei->bei_parent->bei_kids, ei2, bdb_rdn_cmp,
Howard Chu's avatar
Howard Chu committed
362
			avl_dup_error );
Howard Chu's avatar
Howard Chu committed
363
364
365
366
367
		if ( rc ) {
			/* This should never happen; entry cache is corrupt */
			bdb->bi_dbenv->log_flush( bdb->bi_dbenv, NULL );
			assert( !rc );
		}
368
369
370
#ifdef BDB_HIER
		ei->bei_parent->bei_ckids++;
#endif
371
372
	}

Howard Chu's avatar
Howard Chu committed
373
374
	*res = ei2;
	return 0;
375
376
}

Howard Chu's avatar
Howard Chu committed
377
378
379
380
381
382
383
/* Find the EntryInfo for the requested DN. If the DN cannot be found, return
 * the info for its closest ancestor. *res should be NULL to process a
 * complete DN starting from the tree root. Otherwise *res must be the
 * immediate parent of the requested DN, and only the RDN will be searched.
 * The EntryInfo is locked upon return and must be unlocked by the caller.
 */
int
384
bdb_cache_find_ndn(
385
	Operation	*op,
Howard Chu's avatar
Howard Chu committed
386
	BDB_LOCKER		locker,
Howard Chu's avatar
Howard Chu committed
387
	struct berval	*ndn,
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
388
	EntryInfo	**res )
389
{
390
	struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
Howard Chu's avatar
Howard Chu committed
391
392
393
	EntryInfo	ei, *eip, *ei2;
	int rc = 0;
	char *ptr;
394
395

	/* this function is always called with normalized DN */
Howard Chu's avatar
Howard Chu committed
396
397
398
	if ( *res ) {
		/* we're doing a onelevel search for an RDN */
		ei.bei_nrdn.bv_val = ndn->bv_val;
399
		ei.bei_nrdn.bv_len = dn_rdnlen( op->o_bd, ndn );
Howard Chu's avatar
Howard Chu committed
400
401
402
		eip = *res;
	} else {
		/* we're searching a full DN from the root */
403
		ptr = ndn->bv_val + ndn->bv_len - op->o_bd->be_nsuffix[0].bv_len;
Howard Chu's avatar
Howard Chu committed
404
		ei.bei_nrdn.bv_val = ptr;
405
		ei.bei_nrdn.bv_len = op->o_bd->be_nsuffix[0].bv_len;
Howard Chu's avatar
Howard Chu committed
406
407
408
409
410
411
412
413
414
415
		/* Skip to next rdn if suffix is empty */
		if ( ei.bei_nrdn.bv_len == 0 ) {
			for (ptr = ei.bei_nrdn.bv_val - 2; ptr > ndn->bv_val
				&& !DN_SEPARATOR(*ptr); ptr--) /* empty */;
			if ( ptr >= ndn->bv_val ) {
				if (DN_SEPARATOR(*ptr)) ptr++;
				ei.bei_nrdn.bv_len = ei.bei_nrdn.bv_val - ptr;
				ei.bei_nrdn.bv_val = ptr;
			}
		}
Howard Chu's avatar
Howard Chu committed
416
417
418
419
		eip = &bdb->bi_cache.c_dntree;
	}
	
	for ( bdb_cache_entryinfo_lock( eip ); eip; ) {
420
		eip->bei_state |= CACHE_ENTRY_REFERENCED;
Howard Chu's avatar
Howard Chu committed
421
		ei.bei_parent = eip;
Howard Chu's avatar
Howard Chu committed
422
423
		ei2 = (EntryInfo *)avl_find( eip->bei_kids, &ei, bdb_rdn_cmp );
		if ( !ei2 ) {
Howard Chu's avatar
Howard Chu committed
424
			DB_LOCK lock;
Howard Chu's avatar
Howard Chu committed
425
426
			int len = ei.bei_nrdn.bv_len;
				
427
428
429
430
431
			if ( BER_BVISEMPTY( ndn )) {
				*res = eip;
				return LDAP_SUCCESS;
			}

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
432
433
			ei.bei_nrdn.bv_len = ndn->bv_len -
				(ei.bei_nrdn.bv_val - ndn->bv_val);
Howard Chu's avatar
Howard Chu committed
434
435
			bdb_cache_entryinfo_unlock( eip );

Howard Chu's avatar
Howard Chu committed
436
437
438
439
440
			BDB_LOG_PRINTF( bdb->bi_dbenv, NULL, "slapd Reading %s",
				ei.bei_nrdn.bv_val );

			lock.mode = DB_LOCK_NG;
			rc = bdb_dn2id( op, &ei.bei_nrdn, &ei, locker, &lock );
Howard Chu's avatar
Howard Chu committed
441
442
			if (rc) {
				bdb_cache_entryinfo_lock( eip );
Howard Chu's avatar
Howard Chu committed
443
				bdb_cache_entry_db_unlock( bdb, &lock );
Howard Chu's avatar
Howard Chu committed
444
445
446
				*res = eip;
				return rc;
			}
447

Howard Chu's avatar
Howard Chu committed
448
449
450
			BDB_LOG_PRINTF( bdb->bi_dbenv, NULL, "slapd Read got %s(%d)",
				ei.bei_nrdn.bv_val, ei.bei_id );

Howard Chu's avatar
Howard Chu committed
451
452
			/* DN exists but needs to be added to cache */
			ei.bei_nrdn.bv_len = len;
453
			rc = bdb_entryinfo_add_internal( bdb, &ei, &ei2 );
Howard Chu's avatar
Howard Chu committed
454
455
			/* add_internal left eip and c_rwlock locked */
			ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
Howard Chu's avatar
Howard Chu committed
456
			bdb_cache_entry_db_unlock( bdb, &lock );
Howard Chu's avatar
Howard Chu committed
457
458
459
460
			if ( rc ) {
				*res = eip;
				return rc;
			}
461
		} else if ( ei2->bei_state & CACHE_ENTRY_DELETED ) {
Howard Chu's avatar
Howard Chu committed
462
463
464
465
			/* In the midst of deleting? Give it a chance to
			 * complete.
			 */
			bdb_cache_entryinfo_unlock( eip );
466
			ldap_pvt_thread_yield();
Howard Chu's avatar
Howard Chu committed
467
468
469
470
471
472
473
474
475
476
477
			bdb_cache_entryinfo_lock( eip );
			*res = eip;
			return DB_NOTFOUND;
		}
		bdb_cache_entryinfo_unlock( eip );
		bdb_cache_entryinfo_lock( ei2 );

		eip = ei2;

		/* Advance to next lower RDN */
		for (ptr = ei.bei_nrdn.bv_val - 2; ptr > ndn->bv_val
478
			&& !DN_SEPARATOR(*ptr); ptr--) /* empty */;
Howard Chu's avatar
Howard Chu committed
479
		if ( ptr >= ndn->bv_val ) {
480
			if (DN_SEPARATOR(*ptr)) ptr++;
Howard Chu's avatar
Howard Chu committed
481
482
483
484
485
486
			ei.bei_nrdn.bv_len = ei.bei_nrdn.bv_val - ptr - 1;
			ei.bei_nrdn.bv_val = ptr;
		}
		if ( ptr < ndn->bv_val ) {
			*res = eip;
			break;
487
488
489
		}
	}

Howard Chu's avatar
Howard Chu committed
490
	return rc;
491
492
}

Howard Chu's avatar
Howard Chu committed
493
494
495
496
#ifdef BDB_HIER
/* Walk up the tree from a child node, looking for an ID that's already
 * been linked into the cache.
 */
Howard Chu's avatar
Howard Chu committed
497
int
498
499
hdb_cache_find_parent(
	Operation *op,
500
	BDB_LOCKER	locker,
Howard Chu's avatar
Howard Chu committed
501
	ID id,
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
502
	EntryInfo **res )
Howard Chu's avatar
Howard Chu committed
503
{
504
	struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
Howard Chu's avatar
Howard Chu committed
505
506
507
508
509
	EntryInfo ei, eip, *ei2 = NULL, *ein = NULL, *eir = NULL;
	int rc;

	ei.bei_id = id;
	ei.bei_kids = NULL;
Howard Chu's avatar
Howard Chu committed
510
	ei.bei_ckids = 0;
Howard Chu's avatar
Howard Chu committed
511
512

	for (;;) {
Howard Chu's avatar
Howard Chu committed
513
		rc = hdb_dn2id_parent( op, locker, &ei, &eip.bei_id );
Howard Chu's avatar
Howard Chu committed
514
515
516
517
518
519
		if ( rc ) break;

		/* Save the previous node, if any */
		ei2 = ein;

		/* Create a new node for the current ID */
Howard Chu's avatar
Howard Chu committed
520
		ein = bdb_cache_entryinfo_new( &bdb->bi_cache );
Howard Chu's avatar
Howard Chu committed
521
		ein->bei_id = ei.bei_id;
Howard Chu's avatar
Howard Chu committed
522
		ein->bei_kids = ei.bei_kids;
Howard Chu's avatar
Howard Chu committed
523
524
		ein->bei_nrdn = ei.bei_nrdn;
		ein->bei_rdn = ei.bei_rdn;
Howard Chu's avatar
Howard Chu committed
525
		ein->bei_ckids = ei.bei_ckids;
526
527
528
#ifdef SLAP_ZONE_ALLOC
		ein->bei_bdb = bdb;
#endif
Howard Chu's avatar
Howard Chu committed
529
		ei.bei_ckids = 0;
Howard Chu's avatar
Howard Chu committed
530
531
		
		/* This node is not fully connected yet */
532
		ein->bei_state |= CACHE_ENTRY_NOT_LINKED;
Howard Chu's avatar
Howard Chu committed
533
534

		/* Insert this node into the ID tree */
Howard Chu's avatar
Howard Chu committed
535
		ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
Howard Chu's avatar
Howard Chu committed
536
		if ( avl_insert( &bdb->bi_cache.c_idtree, (caddr_t)ein,
537
			bdb_id_cmp, bdb_id_dup_err ) ) {
Howard Chu's avatar
Howard Chu committed
538
			EntryInfo *eix = ein->bei_lrunext;
Howard Chu's avatar
Howard Chu committed
539

Howard Chu's avatar
Howard Chu committed
540
541
542
			/* Someone else created this node just before us.
			 * Free our new copy and use the existing one.
			 */
543
544
			bdb_cache_entryinfo_free( &bdb->bi_cache, ein );
			ein = eix;
Howard Chu's avatar
Howard Chu committed
545
546
			
			/* Link in any kids we've already processed */
Howard Chu's avatar
Howard Chu committed
547
548
549
550
			if ( ei2 ) {
				bdb_cache_entryinfo_lock( ein );
				avl_insert( &ein->bei_kids, (caddr_t)ei2,
					bdb_rdn_cmp, avl_dup_error );
Howard Chu's avatar
Howard Chu committed
551
				ein->bei_ckids++;
Howard Chu's avatar
Howard Chu committed
552
553
				bdb_cache_entryinfo_unlock( ein );
			}
Howard Chu's avatar
Howard Chu committed
554
555
		}

556
557
558
559
560
		/* If this is the first time, save this node
		 * to be returned later.
		 */
		if ( eir == NULL ) eir = ein;

Howard Chu's avatar
Howard Chu committed
561
562
563
		/* If there was a previous node, link it to this one */
		if ( ei2 ) ei2->bei_parent = ein;

Howard Chu's avatar
Howard Chu committed
564
		/* Look for this node's parent */
Howard Chu's avatar
Howard Chu committed
565
566
567
568
569
570
		if ( eip.bei_id ) {
			ei2 = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
					(caddr_t) &eip, bdb_id_cmp );
		} else {
			ei2 = &bdb->bi_cache.c_dntree;
		}
Howard Chu's avatar
Howard Chu committed
571
		bdb->bi_cache.c_eiused++;
572
573
		if ( ei2 && ( ei2->bei_kids || !ei2->bei_id ))
				bdb->bi_cache.c_leaves++;
Howard Chu's avatar
Howard Chu committed
574
		ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
Howard Chu's avatar
Howard Chu committed
575

Howard Chu's avatar
Howard Chu committed
576
		/* Got the parent, link in and we're done. */
Howard Chu's avatar
Howard Chu committed
577
		if ( ei2 ) {
578
			bdb_cache_entryinfo_lock( eir );
Howard Chu's avatar
Howard Chu committed
579
			bdb_cache_entryinfo_lock( ei2 );
Howard Chu's avatar
Howard Chu committed
580
			ein->bei_parent = ei2;
581

582
583
584
585
			avl_insert( &ei2->bei_kids, (caddr_t)ein, bdb_rdn_cmp,
				avl_dup_error);
			ei2->bei_ckids++;

586
587
588
589
			/* Reset all the state info */
			for (ein = eir; ein != ei2; ein=ein->bei_parent)
				ein->bei_state &= ~CACHE_ENTRY_NOT_LINKED;

Howard Chu's avatar
Howard Chu committed
590
			bdb_cache_entryinfo_unlock( ei2 );
Howard Chu's avatar
Howard Chu committed
591
592

			*res = eir;
Howard Chu's avatar
Howard Chu committed
593
594
595
596
			break;
		}
		ei.bei_kids = NULL;
		ei.bei_id = eip.bei_id;
Howard Chu's avatar
Howard Chu committed
597
		ei.bei_ckids = 1;
Howard Chu's avatar
Howard Chu committed
598
599
600
601
602
		avl_insert( &ei.bei_kids, (caddr_t)ein, bdb_rdn_cmp,
			avl_dup_error );
	}
	return rc;
}
603

Howard Chu's avatar
Howard Chu committed
604
605
606
607
/* Used by hdb_dn2idl when loading the EntryInfo for all the children
 * of a given node
 */
int hdb_cache_load(
608
609
610
611
612
613
614
	struct bdb_info *bdb,
	EntryInfo *ei,
	EntryInfo **res )
{
	EntryInfo *ei2;
	int rc;

Howard Chu's avatar
Howard Chu committed
615
	/* See if we already have this one */
616
617
618
	bdb_cache_entryinfo_lock( ei->bei_parent );
	ei2 = (EntryInfo *)avl_find( ei->bei_parent->bei_kids, ei, bdb_rdn_cmp );
	bdb_cache_entryinfo_unlock( ei->bei_parent );
Howard Chu's avatar
Howard Chu committed
619

620
	if ( !ei2 ) {
Howard Chu's avatar
Howard Chu committed
621
622
623
624
625
626
627
		/* Not found, add it */
		struct berval bv;

		/* bei_rdn was not malloc'd before, do it now */
		ber_dupbv( &bv, &ei->bei_rdn );
		ei->bei_rdn = bv;

628
629
630
631
		rc = bdb_entryinfo_add_internal( bdb, ei, res );
		bdb_cache_entryinfo_unlock( ei->bei_parent );
		ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
	} else {
Howard Chu's avatar
Howard Chu committed
632
		/* Found, return it */
633
634
635
636
637
		*res = ei2;
		return 0;
	}
	return rc;
}
Howard Chu's avatar
Howard Chu committed
638
639
#endif

640
641
642
643
/* This is best-effort only. If all entries in the cache are
 * busy, they will all be kept. This is unlikely to happen
 * unless the cache is very much smaller than the working set.
 */
644
static void
645
bdb_cache_lru_purge( struct bdb_info *bdb )
646
{
647
	DB_LOCK		lock, *lockp;
648
649
	EntryInfo *elru, *elnext = NULL;
	int count, islocked, eimax;
650

651
652
	/* Wait for the mutex; we're the only one trying to purge. */
	ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_lru_mutex );
653

654
	if ( bdb->bi_cache.c_cursize <= bdb->bi_cache.c_maxsize ) {
655
		ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_lru_mutex );
Howard Chu's avatar
Howard Chu committed
656
		bdb->bi_cache.c_purging = 0;
657
		return;
658
	}
659

660
661
662
663
664
665
	if ( bdb->bi_cache.c_locker ) {
		lockp = &lock;
	} else {
		lockp = NULL;
	}

666
	count = 0;
667

668
669
670
671
672
673
	/* maximum number of EntryInfo leaves to cache. In slapcat
	 * we always free all leaf nodes.
	 */
	if ( slapMode & SLAP_TOOL_READONLY )
		eimax = 0;
	else
674
		eimax = bdb->bi_cache.c_eimax;
675

676
	/* Look for an unused entry to remove */
677
	for ( elru = bdb->bi_cache.c_lruhead; elru; elru = elnext ) {
678
679
		elnext = elru->bei_lrunext;

680
		if ( bdb_cache_entryinfo_trylock( elru ))
681
			goto bottom;
682

683
684
685
686
		/* This flag implements the clock replacement behavior */
		if ( elru->bei_state & ( CACHE_ENTRY_REFERENCED )) {
			elru->bei_state &= ~CACHE_ENTRY_REFERENCED;
			bdb_cache_entryinfo_unlock( elru );
687
			goto bottom;
688
689
690
691
		}

		/* If this node is in the process of linking into the cache,
		 * or this node is being deleted, skip it.
692
		 */
Howard Chu's avatar
Howard Chu committed
693
694
		if (( elru->bei_state & ( CACHE_ENTRY_NOT_LINKED |
			CACHE_ENTRY_DELETED | CACHE_ENTRY_LOADING )) ||
695
			elru->bei_finders > 0 ) {
696
			bdb_cache_entryinfo_unlock( elru );
697
			goto bottom;
698
		}
699

700
		/* entryinfo is locked */
701
		islocked = 1;
702

703
704
705
		/* If we can successfully writelock it, then
		 * the object is idle.
		 */
706
707
		if ( bdb_cache_entry_db_lock( bdb,
			bdb->bi_cache.c_locker, elru, 1, 1, lockp ) == 0 ) {
708
709
710
711

			/* Free entry for this node if it's present */
			if ( elru->bei_e ) {
				elru->bei_e->e_private = NULL;
712
#ifdef SLAP_ZONE_ALLOC
713
				bdb_entry_return( bdb, elru->bei_e, elru->bei_zseq );
714
#else
715
				bdb_entry_return( elru->bei_e );
716
#endif
717
718
719
				elru->bei_e = NULL;
				count++;
			}
720
			bdb_cache_entry_db_unlock( bdb, lockp );
721

722
723
			/* 
			 * If it is a leaf node, and we're over the limit, free it.
724
			 */
725
726
			if ( elru->bei_kids ) {
				/* Drop from list, we ignore it... */
Howard Chu's avatar
Howard Chu committed
727
				LRU_DEL( &bdb->bi_cache, elru );
728
729
730
731
732
733
			} else if ( bdb->bi_cache.c_leaves > eimax ) {
				/* Too many leaf nodes, free this one */
				bdb_cache_delete_internal( &bdb->bi_cache, elru, 0 );
				bdb_cache_delete_cleanup( &bdb->bi_cache, elru );
				islocked = 0;
			}	/* Leave on list until we need to free it */
734
		}
735

736
737
738
739
740
741
742
743
		if ( islocked )
			bdb_cache_entryinfo_unlock( elru );

		if ( count >= bdb->bi_cache.c_minfree ) {
			ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_count_mutex );
			bdb->bi_cache.c_cursize -= count;
			ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_count_mutex );
			break;
744
		}
745
746
747
bottom:
		if ( elnext == bdb->bi_cache.c_lruhead )
			break;
748
749
	}

Howard Chu's avatar
Howard Chu committed
750
	bdb->bi_cache.c_lruhead = elnext;
751
	ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_lru_mutex );
Howard Chu's avatar
Howard Chu committed
752
	bdb->bi_cache.c_purging = 0;
753
754
}

Howard Chu's avatar
Howard Chu committed
755
756
757
EntryInfo *
bdb_cache_find_info(
	struct bdb_info *bdb,
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
758
	ID id )
Howard Chu's avatar
Howard Chu committed
759
{
Pierangelo Masarati's avatar
Pierangelo Masarati committed
760
761
	EntryInfo	ei = { 0 },
			*ei2;
Howard Chu's avatar
Howard Chu committed
762
763
764
765
766
767
768
769
770
771

	ei.bei_id = id;

	ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
	ei2 = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
					(caddr_t) &ei, bdb_id_cmp );
	ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
	return ei2;
}

772
/*
773
 * cache_find_id - find an entry in the cache, given id.
774
 * The entry is locked for Read upon return. Call with flag ID_LOCKED if
Howard Chu's avatar
Howard Chu committed
775
 * the supplied *eip was already locked.
776
777
 */

Howard Chu's avatar
Howard Chu committed
778
int
779
bdb_cache_find_id(
780
	Operation *op,
Howard Chu's avatar
Howard Chu committed
781
	DB_TXN	*tid,
782
	ID				id,
Howard Chu's avatar
Howard Chu committed
783
	EntryInfo	**eip,
784
	int		flag,
785
	BDB_LOCKER	locker,
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
786
	DB_LOCK		*lock )
787
{
788
	struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
Howard Chu's avatar
Howard Chu committed
789
	Entry	*ep = NULL;
790
	int	rc = 0, load = 0;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
791
	EntryInfo ei = { 0 };
Howard Chu's avatar
Howard Chu committed
792
793
794

	ei.bei_id = id;

795
796
797
#ifdef SLAP_ZONE_ALLOC
	slap_zh_rlock(bdb->bi_cache.c_zctx);
#endif
Howard Chu's avatar
Howard Chu committed
798
799
	/* If we weren't given any info, see if we have it already cached */
	if ( !*eip ) {
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
800
again:	ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
Howard Chu's avatar
Howard Chu committed
801
		*eip = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
802
			(caddr_t) &ei, bdb_id_cmp );
Howard Chu's avatar
Howard Chu committed
803
		if ( *eip ) {
Howard Chu's avatar
Howard Chu committed
804
			/* If the lock attempt fails, the info is in use */
805
			if ( bdb_cache_entryinfo_trylock( *eip )) {
Howard Chu's avatar
Howard Chu committed
806
				ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
Howard Chu's avatar
Howard Chu committed
807
808
809
				/* If this node is being deleted, treat
				 * as if the delete has already finished
				 */
810
811
812
				if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
					return DB_NOTFOUND;
				}
Howard Chu's avatar
Howard Chu committed
813
814
815
816
817
818
819
820
821
822
				/* otherwise, wait for the info to free up */
				ldap_pvt_thread_yield();
				goto again;
			}
			/* If this info isn't hooked up to its parent yet,
			 * unlock and wait for it to be fully initialized
			 */
			if ( (*eip)->bei_state & CACHE_ENTRY_NOT_LINKED ) {
				bdb_cache_entryinfo_unlock( *eip );
				ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
Howard Chu's avatar
Howard Chu committed
823
824
825
				ldap_pvt_thread_yield();
				goto again;
			}
826
			flag |= ID_LOCKED;
Howard Chu's avatar
Howard Chu committed
827
828
829
		}
		ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
	}
830

Howard Chu's avatar
Howard Chu committed
831
832
	/* See if the ID exists in the database; add it to the cache if so */
	if ( !*eip ) {
Howard Chu's avatar
Howard Chu committed
833
#ifndef BDB_HIER
834
		rc = bdb_id2entry( op->o_bd, tid, locker, id, &ep );
Howard Chu's avatar
Howard Chu committed
835
		if ( rc == 0 ) {
Howard Chu's avatar
Howard Chu committed
836
			rc = bdb_cache_find_ndn( op, locker,
837
				&ep->e_nname, eip );
838
			if ( *eip ) flag |= ID_LOCKED;
Howard Chu's avatar
Howard Chu committed
839
			if ( rc ) {
840
				ep->e_private = NULL;
841
842
843
#ifdef SLAP_ZONE_ALLOC
				bdb_entry_return( bdb, ep, (*eip)->bei_zseq );
#else
Howard Chu's avatar
Howard Chu committed
844
				bdb_entry_return( ep );
845
#endif
Howard Chu's avatar
Howard Chu committed
846
847
848
				ep = NULL;
			}
		}
Howard Chu's avatar
Howard Chu committed
849
#else
Howard Chu's avatar
Howard Chu committed
850
		rc = hdb_cache_find_parent(op, locker, id, eip );
851
		if ( rc == 0 ) flag |= ID_LOCKED;
Howard Chu's avatar
Howard Chu committed
852
#endif
Howard Chu's avatar
Howard Chu committed
853
	}
854

Howard Chu's avatar
Howard Chu committed
855
	/* Ok, we found the info, do we have the entry? */
Howard Chu's avatar
cleanup    
Howard Chu committed
856
	if ( rc == 0 ) {
857
		if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
Howard Chu's avatar
Howard Chu committed
858
			rc = DB_NOTFOUND;
859
		} else {
860
			(*eip)->bei_finders++;
Howard Chu's avatar
Howard Chu committed
861
			(*eip)->bei_state |= CACHE_ENTRY_REFERENCED;
862
			/* Make sure only one thread tries to load the entry */
863
864
865
866
867
868
869
870
871
load1:
#ifdef SLAP_ZONE_ALLOC
			if ((*eip)->bei_e && !slap_zn_validate(
					bdb->bi_cache.c_zctx, (*eip)->bei_e, (*eip)->bei_zseq)) {
				(*eip)->bei_e = NULL;
				(*eip)->bei_zseq = 0;
			}
#endif
			if ( !(*eip)->bei_e && !((*eip)->bei_state & CACHE_ENTRY_LOADING)) {
872
873
874
				load = 1;
				(*eip)->bei_state |= CACHE_ENTRY_LOADING;
			}
875

876
877
878
879
880
881
882
883
884
885
			/* If the entry was loaded before but uncached, and we need
			 * it again, clear the uncached state
			 */
			if ( (*eip)->bei_state & CACHE_ENTRY_NOT_CACHED ) {
				(*eip)->bei_state ^= CACHE_ENTRY_NOT_CACHED;
				if ( flag & ID_NOCACHE )
					flag ^= ID_NOCACHE;
			}

			if ( flag & ID_LOCKED ) {
886
				bdb_cache_entryinfo_unlock( *eip );
887
				flag ^= ID_LOCKED;
888
			}
Howard Chu's avatar
Howard Chu committed
889
			rc = bdb_cache_entry_db_lock( bdb, locker, *eip, load, 0, lock );
890
891
			if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
				rc = DB_NOTFOUND;
Howard Chu's avatar
Howard Chu committed
892
				bdb_cache_entry_db_unlock( bdb, lock );
893
			} else if ( rc == 0 ) {
894
				if ( load ) {
Howard Chu's avatar
Howard Chu committed
895
					if ( !ep) {
896
						rc = bdb_id2entry( op->o_bd, tid, locker, id, &ep );
897
898
					}
					if ( rc == 0 ) {
899
						ep->e_private = *eip;
Howard Chu's avatar
Howard Chu committed
900
#ifdef BDB_HIER
901
						bdb_fix_dn( ep, 0 );
Howard Chu's avatar
Howard Chu committed
902
#endif
903
						(*eip)->bei_e = ep;
904
905
906
#ifdef SLAP_ZONE_ALLOC
						(*eip)->bei_zseq = *((ber_len_t *)ep - 2);
#endif
907
						ep = NULL;
Howard Chu's avatar
Howard Chu committed
908
						bdb_cache_lru_link( bdb, *eip );
909
910
911
912
913
						if ( flag & ID_NOCACHE ) {
							bdb_cache_entryinfo_lock( *eip );
							(*eip)->bei_state |= CACHE_ENTRY_NOT_CACHED;
							bdb_cache_entryinfo_unlock( *eip );
						}
914
					}
Howard Chu's avatar
Howard Chu committed
915
916
917
918
919
920
					if ( rc == 0 ) {
						/* If we succeeded, downgrade back to a readlock. */
						rc = bdb_cache_entry_db_relock( bdb, locker,
							*eip, 0, 0, lock );
					} else {
						/* Otherwise, release the lock. */
Howard Chu's avatar
Howard Chu committed
921
						bdb_cache_entry_db_unlock( bdb, lock );
922
					}
923
924
				} else if ( !(*eip)->bei_e ) {
					/* Some other thread is trying to load the entry,