]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/cache.c
Revert prev commit
[openldap] / servers / slapd / back-bdb / cache.c
1 /* cache.c - routines to maintain an in-core cache of entries */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2000-2007 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
21 #include <ac/errno.h>
22 #include <ac/string.h>
23 #include <ac/socket.h>
24
25 #include "slap.h"
26
27 #include "back-bdb.h"
28
29 #include "ldap_rq.h"
30
31 #ifdef BDB_HIER
32 #define bdb_cache_lru_purge     hdb_cache_lru_purge
33 #endif
34 static void bdb_cache_lru_purge( struct bdb_info *bdb );
35
36 static int      bdb_cache_delete_internal(Cache *cache, EntryInfo *e, int decr);
37 #ifdef LDAP_DEBUG
38 #define SLAPD_UNUSED
39 #ifdef SLAPD_UNUSED
40 static void     bdb_lru_print(Cache *cache);
41 #endif
42 #endif
43
44 /* For concurrency experiments only! */
45 #if 0
46 #define ldap_pvt_thread_rdwr_wlock(a)   0
47 #define ldap_pvt_thread_rdwr_wunlock(a) 0
48 #define ldap_pvt_thread_rdwr_rlock(a)   0
49 #define ldap_pvt_thread_rdwr_runlock(a) 0
50 #endif
51
52 #if 0
53 #define ldap_pvt_thread_mutex_trylock(a) 0
54 #endif
55
56 static EntryInfo *
57 bdb_cache_entryinfo_new( Cache *cache )
58 {
59         EntryInfo *ei = NULL;
60
61         if ( cache->c_eifree ) {
62                 ldap_pvt_thread_mutex_lock( &cache->c_eifree_mutex );
63                 if ( cache->c_eifree ) {
64                         ei = cache->c_eifree;
65                         cache->c_eifree = ei->bei_lrunext;
66                 }
67                 ldap_pvt_thread_mutex_unlock( &cache->c_eifree_mutex );
68                 ei->bei_finders = 0;
69         }
70         if ( !ei ) {
71                 ei = ch_calloc(1, sizeof(EntryInfo));
72                 ldap_pvt_thread_mutex_init( &ei->bei_kids_mutex );
73         }
74
75         ei->bei_state = CACHE_ENTRY_REFERENCED;
76
77         return ei;
78 }
79
80 static void
81 bdb_cache_entryinfo_free( Cache *cache, EntryInfo *ei )
82 {
83         free( ei->bei_nrdn.bv_val );
84         ei->bei_nrdn.bv_val = NULL;
85 #ifdef BDB_HIER
86         free( ei->bei_rdn.bv_val );
87         ei->bei_rdn.bv_val = NULL;
88         ei->bei_modrdns = 0;
89         ei->bei_ckids = 0;
90         ei->bei_dkids = 0;
91 #endif
92         ei->bei_parent = NULL;
93         ei->bei_kids = NULL;
94         ei->bei_lruprev = NULL;
95
96         ldap_pvt_thread_mutex_lock( &cache->c_eifree_mutex );
97         ei->bei_lrunext = cache->c_eifree;
98         cache->c_eifree = ei;
99         ldap_pvt_thread_mutex_unlock( &cache->c_eifree_mutex );
100 }
101
102 #define LRU_DEL( c, e ) do { \
103         if ( e == (c)->c_lruhead ) (c)->c_lruhead = e->bei_lruprev; \
104         if ( e == (c)->c_lrutail ) (c)->c_lrutail = e->bei_lruprev; \
105         e->bei_lrunext->bei_lruprev = e->bei_lruprev; \
106         e->bei_lruprev->bei_lrunext = e->bei_lrunext; \
107         e->bei_lruprev = NULL; \
108 } while ( 0 )
109
110 /* Note - we now use a Second-Chance / Clock algorithm instead of
111  * Least-Recently-Used. This tremendously improves concurrency
112  * because we no longer need to manipulate the lists every time an
113  * entry is touched. We only need to lock the lists when adding
114  * or deleting an entry. It's now a circular doubly-linked list.
115  * We always append to the tail, but the head traverses the circle
116  * during a purge operation.
117  */
118 static void
119 bdb_cache_lru_link( struct bdb_info *bdb, EntryInfo *ei )
120 {
121
122         /* Insert into circular LRU list */
123         ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_lru_mutex );
124
125         /* Still linked, remove */
126         if ( ei->bei_lruprev ) {
127                 LRU_DEL( &bdb->bi_cache, ei );
128         }
129         ei->bei_lruprev = bdb->bi_cache.c_lrutail;
130         if ( bdb->bi_cache.c_lrutail ) {
131                 ei->bei_lrunext = bdb->bi_cache.c_lrutail->bei_lrunext;
132                 bdb->bi_cache.c_lrutail->bei_lrunext = ei;
133                 if ( ei->bei_lrunext )
134                         ei->bei_lrunext->bei_lruprev = ei;
135         } else {
136                 ei->bei_lrunext = ei->bei_lruprev = ei;
137                 bdb->bi_cache.c_lruhead = ei;
138         }
139         bdb->bi_cache.c_lrutail = ei;
140         ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_lru_mutex );
141 }
142
143 #ifdef NO_THREADS
144 #define NO_DB_LOCK
145 #endif
146
147 /* #define NO_DB_LOCK 1 */
148 /* Note: The BerkeleyDB locks are much slower than regular
149  * mutexes or rdwr locks. But the BDB implementation has the
150  * advantage of using a fixed size lock table, instead of
151  * allocating a lock object per entry in the DB. That's a
152  * key benefit for scaling. It also frees us from worrying
153  * about undetectable deadlocks between BDB activity and our
154  * own cache activity. It's still worth exploring faster
155  * alternatives though.
156  */
157
158 /* Atomically release and reacquire a lock */
159 int
160 bdb_cache_entry_db_relock(
161         struct bdb_info *bdb,
162         u_int32_t locker,
163         EntryInfo *ei,
164         int rw,
165         int tryOnly,
166         DB_LOCK *lock )
167 {
168 #ifdef NO_DB_LOCK
169         return 0;
170 #else
171         int     rc;
172         DBT     lockobj;
173         DB_LOCKREQ list[2];
174
175         if ( !lock ) return 0;
176
177         lockobj.data = &ei->bei_id;
178         lockobj.size = sizeof(ei->bei_id) + 1;
179
180         list[0].op = DB_LOCK_PUT;
181         list[0].lock = *lock;
182         list[1].op = DB_LOCK_GET;
183         list[1].lock = *lock;
184         list[1].mode = rw ? DB_LOCK_WRITE : DB_LOCK_READ;
185         list[1].obj = &lockobj;
186         rc = bdb->bi_dbenv->lock_vec(bdb->bi_dbenv, locker, tryOnly ? DB_LOCK_NOWAIT : 0,
187                 list, 2, NULL );
188
189         if (rc && !tryOnly) {
190                 Debug( LDAP_DEBUG_TRACE,
191                         "bdb_cache_entry_db_relock: entry %ld, rw %d, rc %d\n",
192                         ei->bei_id, rw, rc );
193         } else {
194                 *lock = list[1].lock;
195         }
196         return rc;
197 #endif
198 }
199
200 static int
201 bdb_cache_entry_db_lock( struct bdb_info *bdb, u_int32_t locker, EntryInfo *ei,
202         int rw, int tryOnly, DB_LOCK *lock )
203 {
204 #ifdef NO_DB_LOCK
205         return 0;
206 #else
207         int       rc;
208         DBT       lockobj;
209         int       db_rw;
210
211         if ( !lock ) return 0;
212
213         if (rw)
214                 db_rw = DB_LOCK_WRITE;
215         else
216                 db_rw = DB_LOCK_READ;
217
218         lockobj.data = &ei->bei_id;
219         lockobj.size = sizeof(ei->bei_id) + 1;
220
221         rc = LOCK_GET(bdb->bi_dbenv, 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",
226                         ei->bei_id, rw, rc );
227         }
228         return rc;
229 #endif /* NO_DB_LOCK */
230 }
231
232 int
233 bdb_cache_entry_db_unlock ( struct bdb_info *bdb, DB_LOCK *lock )
234 {
235 #ifdef NO_DB_LOCK
236         return 0;
237 #else
238         int rc;
239
240         if ( !lock || lock->mode == DB_LOCK_NG ) return 0;
241
242         rc = LOCK_PUT ( bdb->bi_dbenv, lock );
243         return rc;
244 #endif
245 }
246
247 static int
248 bdb_cache_entryinfo_destroy( EntryInfo *e )
249 {
250         ldap_pvt_thread_mutex_destroy( &e->bei_kids_mutex );
251         free( e->bei_nrdn.bv_val );
252 #ifdef BDB_HIER
253         free( e->bei_rdn.bv_val );
254 #endif
255         free( e );
256         return 0;
257 }
258
259 /* Do a length-ordered sort on normalized RDNs */
260 static int
261 bdb_rdn_cmp( const void *v_e1, const void *v_e2 )
262 {
263         const EntryInfo *e1 = v_e1, *e2 = v_e2;
264         int rc = e1->bei_nrdn.bv_len - e2->bei_nrdn.bv_len;
265         if (rc == 0) {
266                 rc = strncmp( e1->bei_nrdn.bv_val, e2->bei_nrdn.bv_val,
267                         e1->bei_nrdn.bv_len );
268         }
269         return rc;
270 }
271
272 static int
273 bdb_id_cmp( const void *v_e1, const void *v_e2 )
274 {
275         const EntryInfo *e1 = v_e1, *e2 = v_e2;
276         return e1->bei_id - e2->bei_id;
277 }
278
279 static int
280 bdb_id_dup_err( void *v1, void *v2 )
281 {
282         EntryInfo *e2 = v2;
283         e2->bei_lrunext = v1;
284         return -1;
285 }
286
287 /* Create an entryinfo in the cache. Caller must release the locks later.
288  */
289 static int
290 bdb_entryinfo_add_internal(
291         struct bdb_info *bdb,
292         EntryInfo *ei,
293         EntryInfo **res )
294 {
295         EntryInfo *ei2 = NULL;
296
297         *res = NULL;
298
299         ei2 = bdb_cache_entryinfo_new( &bdb->bi_cache );
300
301         ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
302         bdb_cache_entryinfo_lock( ei->bei_parent );
303
304         ei2->bei_id = ei->bei_id;
305         ei2->bei_parent = ei->bei_parent;
306 #ifdef BDB_HIER
307         ei2->bei_rdn = ei->bei_rdn;
308 #endif
309 #ifdef SLAP_ZONE_ALLOC
310         ei2->bei_bdb = bdb;
311 #endif
312
313         /* Add to cache ID tree */
314         if (avl_insert( &bdb->bi_cache.c_idtree, ei2, bdb_id_cmp,
315                 bdb_id_dup_err )) {
316                 EntryInfo *eix = ei2->bei_lrunext;
317                 bdb_cache_entryinfo_free( &bdb->bi_cache, ei2 );
318                 ei2 = eix;
319 #ifdef BDB_HIER
320                 /* It got freed above because its value was
321                  * assigned to ei2.
322                  */
323                 ei->bei_rdn.bv_val = NULL;
324 #endif
325         } else {
326                 bdb->bi_cache.c_eiused++;
327                 ber_dupbv( &ei2->bei_nrdn, &ei->bei_nrdn );
328
329                 /* This is a new leaf node. But if parent had no kids, then it was
330                  * a leaf and we would be decrementing that. So, only increment if
331                  * the parent already has kids.
332                  */
333                 if ( ei->bei_parent->bei_kids || !ei->bei_parent->bei_id )
334                         bdb->bi_cache.c_leaves++;
335                 avl_insert( &ei->bei_parent->bei_kids, ei2, bdb_rdn_cmp,
336                         avl_dup_error );
337 #ifdef BDB_HIER
338                 ei->bei_parent->bei_ckids++;
339 #endif
340         }
341
342         *res = ei2;
343         return 0;
344 }
345
346 /* Find the EntryInfo for the requested DN. If the DN cannot be found, return
347  * the info for its closest ancestor. *res should be NULL to process a
348  * complete DN starting from the tree root. Otherwise *res must be the
349  * immediate parent of the requested DN, and only the RDN will be searched.
350  * The EntryInfo is locked upon return and must be unlocked by the caller.
351  */
352 int
353 bdb_cache_find_ndn(
354         Operation       *op,
355         DB_TXN          *txn,
356         struct berval   *ndn,
357         EntryInfo       **res )
358 {
359         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
360         EntryInfo       ei, *eip, *ei2;
361         int rc = 0;
362         char *ptr;
363
364         /* this function is always called with normalized DN */
365         if ( *res ) {
366                 /* we're doing a onelevel search for an RDN */
367                 ei.bei_nrdn.bv_val = ndn->bv_val;
368                 ei.bei_nrdn.bv_len = dn_rdnlen( op->o_bd, ndn );
369                 eip = *res;
370         } else {
371                 /* we're searching a full DN from the root */
372                 ptr = ndn->bv_val + ndn->bv_len - op->o_bd->be_nsuffix[0].bv_len;
373                 ei.bei_nrdn.bv_val = ptr;
374                 ei.bei_nrdn.bv_len = op->o_bd->be_nsuffix[0].bv_len;
375                 /* Skip to next rdn if suffix is empty */
376                 if ( ei.bei_nrdn.bv_len == 0 ) {
377                         for (ptr = ei.bei_nrdn.bv_val - 2; ptr > ndn->bv_val
378                                 && !DN_SEPARATOR(*ptr); ptr--) /* empty */;
379                         if ( ptr >= ndn->bv_val ) {
380                                 if (DN_SEPARATOR(*ptr)) ptr++;
381                                 ei.bei_nrdn.bv_len = ei.bei_nrdn.bv_val - ptr;
382                                 ei.bei_nrdn.bv_val = ptr;
383                         }
384                 }
385                 eip = &bdb->bi_cache.c_dntree;
386         }
387         
388         for ( bdb_cache_entryinfo_lock( eip ); eip; ) {
389                 eip->bei_state |= CACHE_ENTRY_REFERENCED;
390                 ei.bei_parent = eip;
391                 ei2 = (EntryInfo *)avl_find( eip->bei_kids, &ei, bdb_rdn_cmp );
392                 if ( !ei2 ) {
393                         int len = ei.bei_nrdn.bv_len;
394                                 
395                         if ( BER_BVISEMPTY( ndn )) {
396                                 *res = eip;
397                                 return LDAP_SUCCESS;
398                         }
399
400                         ei.bei_nrdn.bv_len = ndn->bv_len -
401                                 (ei.bei_nrdn.bv_val - ndn->bv_val);
402                         bdb_cache_entryinfo_unlock( eip );
403
404                         rc = bdb_dn2id( op, txn, &ei.bei_nrdn, &ei );
405                         if (rc) {
406                                 bdb_cache_entryinfo_lock( eip );
407                                 *res = eip;
408                                 return rc;
409                         }
410
411                         /* DN exists but needs to be added to cache */
412                         ei.bei_nrdn.bv_len = len;
413                         rc = bdb_entryinfo_add_internal( bdb, &ei, &ei2 );
414                         /* add_internal left eip and c_rwlock locked */
415                         ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
416                         if ( rc ) {
417                                 *res = eip;
418                                 return rc;
419                         }
420                 } else if ( ei2->bei_state & CACHE_ENTRY_DELETED ) {
421                         /* In the midst of deleting? Give it a chance to
422                          * complete.
423                          */
424                         bdb_cache_entryinfo_unlock( eip );
425                         ldap_pvt_thread_yield();
426                         bdb_cache_entryinfo_lock( eip );
427                         *res = eip;
428                         return DB_NOTFOUND;
429                 }
430                 bdb_cache_entryinfo_unlock( eip );
431                 bdb_cache_entryinfo_lock( ei2 );
432
433                 eip = ei2;
434
435                 /* Advance to next lower RDN */
436                 for (ptr = ei.bei_nrdn.bv_val - 2; ptr > ndn->bv_val
437                         && !DN_SEPARATOR(*ptr); ptr--) /* empty */;
438                 if ( ptr >= ndn->bv_val ) {
439                         if (DN_SEPARATOR(*ptr)) ptr++;
440                         ei.bei_nrdn.bv_len = ei.bei_nrdn.bv_val - ptr - 1;
441                         ei.bei_nrdn.bv_val = ptr;
442                 }
443                 if ( ptr < ndn->bv_val ) {
444                         *res = eip;
445                         break;
446                 }
447         }
448
449         return rc;
450 }
451
452 #ifdef BDB_HIER
453 /* Walk up the tree from a child node, looking for an ID that's already
454  * been linked into the cache.
455  */
456 int
457 hdb_cache_find_parent(
458         Operation *op,
459         DB_TXN *txn,
460         u_int32_t       locker,
461         ID id,
462         EntryInfo **res )
463 {
464         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
465         EntryInfo ei, eip, *ei2 = NULL, *ein = NULL, *eir = NULL;
466         int rc;
467
468         ei.bei_id = id;
469         ei.bei_kids = NULL;
470         ei.bei_ckids = 0;
471
472         for (;;) {
473                 rc = hdb_dn2id_parent( op, txn, locker, &ei, &eip.bei_id );
474                 if ( rc ) break;
475
476                 /* Save the previous node, if any */
477                 ei2 = ein;
478
479                 /* Create a new node for the current ID */
480                 ein = bdb_cache_entryinfo_new( &bdb->bi_cache );
481                 ein->bei_id = ei.bei_id;
482                 ein->bei_kids = ei.bei_kids;
483                 ein->bei_nrdn = ei.bei_nrdn;
484                 ein->bei_rdn = ei.bei_rdn;
485                 ein->bei_ckids = ei.bei_ckids;
486 #ifdef SLAP_ZONE_ALLOC
487                 ein->bei_bdb = bdb;
488 #endif
489                 ei.bei_ckids = 0;
490                 
491                 /* This node is not fully connected yet */
492                 ein->bei_state |= CACHE_ENTRY_NOT_LINKED;
493
494                 /* Insert this node into the ID tree */
495                 ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
496                 if ( avl_insert( &bdb->bi_cache.c_idtree, (caddr_t)ein,
497                         bdb_id_cmp, bdb_id_dup_err ) ) {
498                         EntryInfo *eix = ein->bei_lrunext;
499
500                         /* Someone else created this node just before us.
501                          * Free our new copy and use the existing one.
502                          */
503                         bdb_cache_entryinfo_free( &bdb->bi_cache, ein );
504                         ein = eix;
505                         
506                         /* Link in any kids we've already processed */
507                         if ( ei2 ) {
508                                 bdb_cache_entryinfo_lock( ein );
509                                 avl_insert( &ein->bei_kids, (caddr_t)ei2,
510                                         bdb_rdn_cmp, avl_dup_error );
511                                 ein->bei_ckids++;
512                                 bdb_cache_entryinfo_unlock( ein );
513                         }
514                 }
515
516                 /* If this is the first time, save this node
517                  * to be returned later.
518                  */
519                 if ( eir == NULL ) eir = ein;
520
521                 /* If there was a previous node, link it to this one */
522                 if ( ei2 ) ei2->bei_parent = ein;
523
524                 /* Look for this node's parent */
525                 if ( eip.bei_id ) {
526                         ei2 = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
527                                         (caddr_t) &eip, bdb_id_cmp );
528                 } else {
529                         ei2 = &bdb->bi_cache.c_dntree;
530                 }
531                 bdb->bi_cache.c_eiused++;
532                 if ( ei2 && ( ei2->bei_kids || !ei2->bei_id ))
533                                 bdb->bi_cache.c_leaves++;
534                 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
535
536                 /* Got the parent, link in and we're done. */
537                 if ( ei2 ) {
538                         bdb_cache_entryinfo_lock( ei2 );
539                         ein->bei_parent = ei2;
540
541                         avl_insert( &ei2->bei_kids, (caddr_t)ein, bdb_rdn_cmp,
542                                 avl_dup_error);
543                         ei2->bei_ckids++;
544
545                         /* Reset all the state info */
546                         for (ein = eir; ein != ei2; ein=ein->bei_parent)
547                                 ein->bei_state &= ~CACHE_ENTRY_NOT_LINKED;
548
549                         bdb_cache_entryinfo_unlock( ei2 );
550                         bdb_cache_entryinfo_lock( eir );
551
552                         *res = eir;
553                         break;
554                 }
555                 ei.bei_kids = NULL;
556                 ei.bei_id = eip.bei_id;
557                 ei.bei_ckids = 1;
558                 avl_insert( &ei.bei_kids, (caddr_t)ein, bdb_rdn_cmp,
559                         avl_dup_error );
560         }
561         return rc;
562 }
563
564 /* Used by hdb_dn2idl when loading the EntryInfo for all the children
565  * of a given node
566  */
567 int hdb_cache_load(
568         struct bdb_info *bdb,
569         EntryInfo *ei,
570         EntryInfo **res )
571 {
572         EntryInfo *ei2;
573         int rc;
574
575         /* See if we already have this one */
576         bdb_cache_entryinfo_lock( ei->bei_parent );
577         ei2 = (EntryInfo *)avl_find( ei->bei_parent->bei_kids, ei, bdb_rdn_cmp );
578         bdb_cache_entryinfo_unlock( ei->bei_parent );
579
580         if ( !ei2 ) {
581                 /* Not found, add it */
582                 struct berval bv;
583
584                 /* bei_rdn was not malloc'd before, do it now */
585                 ber_dupbv( &bv, &ei->bei_rdn );
586                 ei->bei_rdn = bv;
587
588                 rc = bdb_entryinfo_add_internal( bdb, ei, res );
589                 bdb_cache_entryinfo_unlock( ei->bei_parent );
590                 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
591         } else {
592                 /* Found, return it */
593                 *res = ei2;
594                 return 0;
595         }
596         return rc;
597 }
598 #endif
599
600 /* This is best-effort only. If all entries in the cache are
601  * busy, they will all be kept. This is unlikely to happen
602  * unless the cache is very much smaller than the working set.
603  */
604 static void
605 bdb_cache_lru_purge( struct bdb_info *bdb )
606 {
607         DB_LOCK         lock, *lockp;
608         EntryInfo *elru, *elnext = NULL;
609         int count, islocked, eimax;
610
611         /* Wait for the mutex; we're the only one trying to purge. */
612         ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_lru_mutex );
613
614         if ( bdb->bi_cache.c_cursize <= bdb->bi_cache.c_maxsize ) {
615                 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_lru_mutex );
616                 bdb->bi_cache.c_purging = 0;
617                 return;
618         }
619
620         if ( bdb->bi_cache.c_locker ) {
621                 lockp = &lock;
622         } else {
623                 lockp = NULL;
624         }
625
626         count = 0;
627
628         /* maximum number of EntryInfo leaves to cache. In slapcat
629          * we always free all leaf nodes.
630          */
631         if ( slapMode & SLAP_TOOL_READONLY )
632                 eimax = 0;
633         else
634                 eimax = bdb->bi_cache.c_maxsize * 4;
635
636         /* Look for an unused entry to remove */
637         for ( elru = bdb->bi_cache.c_lruhead; elru; elru = elnext ) {
638                 elnext = elru->bei_lrunext;
639
640                 if ( bdb_cache_entryinfo_trylock( elru ))
641                         goto bottom;
642
643                 /* This flag implements the clock replacement behavior */
644                 if ( elru->bei_state & ( CACHE_ENTRY_REFERENCED )) {
645                         elru->bei_state &= ~CACHE_ENTRY_REFERENCED;
646                         bdb_cache_entryinfo_unlock( elru );
647                         goto bottom;
648                 }
649
650                 /* If this node is in the process of linking into the cache,
651                  * or this node is being deleted, skip it.
652                  */
653                 if (( elru->bei_state & ( CACHE_ENTRY_NOT_LINKED |
654                         CACHE_ENTRY_DELETED | CACHE_ENTRY_LOADING )) ||
655                         elru->bei_finders > 0 ) {
656                         bdb_cache_entryinfo_unlock( elru );
657                         goto bottom;
658                 }
659
660                 /* entryinfo is locked */
661                 islocked = 1;
662
663                 /* If we can successfully writelock it, then
664                  * the object is idle.
665                  */
666                 if ( bdb_cache_entry_db_lock( bdb,
667                         bdb->bi_cache.c_locker, elru, 1, 1, lockp ) == 0 ) {
668
669                         /* Free entry for this node if it's present */
670                         if ( elru->bei_e ) {
671                                 elru->bei_e->e_private = NULL;
672 #ifdef SLAP_ZONE_ALLOC
673                                 bdb_entry_return( bdb, elru->bei_e, elru->bei_zseq );
674 #else
675                                 bdb_entry_return( elru->bei_e );
676 #endif
677                                 elru->bei_e = NULL;
678                                 count++;
679                         }
680                         bdb_cache_entry_db_unlock( bdb, lockp );
681
682                         /* 
683                          * If it is a leaf node, and we're over the limit, free it.
684                          */
685                         if ( elru->bei_kids ) {
686                                 /* Drop from list, we ignore it... */
687                                 LRU_DEL( &bdb->bi_cache, elru );
688                         } else if ( bdb->bi_cache.c_leaves > eimax ) {
689                                 /* Too many leaf nodes, free this one */
690                                 bdb_cache_delete_internal( &bdb->bi_cache, elru, 0 );
691                                 bdb_cache_delete_cleanup( &bdb->bi_cache, elru );
692                                 islocked = 0;
693                         }       /* Leave on list until we need to free it */
694                 }
695
696                 if ( islocked )
697                         bdb_cache_entryinfo_unlock( elru );
698
699                 if ( count >= bdb->bi_cache.c_minfree ) {
700                         ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_count_mutex );
701                         bdb->bi_cache.c_cursize -= count;
702                         ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_count_mutex );
703                         break;
704                 }
705 bottom:
706                 if ( elnext == bdb->bi_cache.c_lruhead )
707                         break;
708         }
709
710         bdb->bi_cache.c_lruhead = elnext;
711         ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_lru_mutex );
712         bdb->bi_cache.c_purging = 0;
713 }
714
715 EntryInfo *
716 bdb_cache_find_info(
717         struct bdb_info *bdb,
718         ID id )
719 {
720         EntryInfo       ei = { 0 },
721                         *ei2;
722
723         ei.bei_id = id;
724
725         ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
726         ei2 = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
727                                         (caddr_t) &ei, bdb_id_cmp );
728         ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
729         return ei2;
730 }
731
732 /*
733  * cache_find_id - find an entry in the cache, given id.
734  * The entry is locked for Read upon return. Call with islocked TRUE if
735  * the supplied *eip was already locked.
736  */
737
738 int
739 bdb_cache_find_id(
740         Operation *op,
741         DB_TXN  *tid,
742         ID                              id,
743         EntryInfo       **eip,
744         int             islocked,
745         u_int32_t       locker,
746         DB_LOCK         *lock )
747 {
748         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
749         Entry   *ep = NULL;
750         int     rc = 0, load = 0;
751         EntryInfo ei = { 0 };
752
753         ei.bei_id = id;
754
755 #ifdef SLAP_ZONE_ALLOC
756         slap_zh_rlock(bdb->bi_cache.c_zctx);
757 #endif
758         /* If we weren't given any info, see if we have it already cached */
759         if ( !*eip ) {
760 again:  ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
761                 *eip = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
762                         (caddr_t) &ei, bdb_id_cmp );
763                 if ( *eip ) {
764                         /* If the lock attempt fails, the info is in use */
765                         if ( bdb_cache_entryinfo_trylock( *eip )) {
766                                 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
767                                 /* If this node is being deleted, treat
768                                  * as if the delete has already finished
769                                  */
770                                 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
771                                         return DB_NOTFOUND;
772                                 }
773                                 /* otherwise, wait for the info to free up */
774                                 ldap_pvt_thread_yield();
775                                 goto again;
776                         }
777                         /* If this info isn't hooked up to its parent yet,
778                          * unlock and wait for it to be fully initialized
779                          */
780                         if ( (*eip)->bei_state & CACHE_ENTRY_NOT_LINKED ) {
781                                 bdb_cache_entryinfo_unlock( *eip );
782                                 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
783                                 ldap_pvt_thread_yield();
784                                 goto again;
785                         }
786                         islocked = 1;
787                 }
788                 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
789         }
790
791         /* See if the ID exists in the database; add it to the cache if so */
792         if ( !*eip ) {
793 #ifndef BDB_HIER
794                 rc = bdb_id2entry( op->o_bd, tid, locker, id, &ep );
795                 if ( rc == 0 ) {
796                         rc = bdb_cache_find_ndn( op, tid,
797                                 &ep->e_nname, eip );
798                         if ( *eip ) islocked = 1;
799                         if ( rc ) {
800                                 ep->e_private = NULL;
801 #ifdef SLAP_ZONE_ALLOC
802                                 bdb_entry_return( bdb, ep, (*eip)->bei_zseq );
803 #else
804                                 bdb_entry_return( ep );
805 #endif
806                                 ep = NULL;
807                         }
808                 }
809 #else
810                 rc = hdb_cache_find_parent(op, tid, locker, id, eip );
811                 if ( rc == 0 ) islocked = 1;
812 #endif
813         }
814
815         /* Ok, we found the info, do we have the entry? */
816         if ( rc == 0 ) {
817                 if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
818                         rc = DB_NOTFOUND;
819                 } else {
820                         (*eip)->bei_finders++;
821                         (*eip)->bei_state |= CACHE_ENTRY_REFERENCED;
822                         /* Make sure only one thread tries to load the entry */
823 load1:
824 #ifdef SLAP_ZONE_ALLOC
825                         if ((*eip)->bei_e && !slap_zn_validate(
826                                         bdb->bi_cache.c_zctx, (*eip)->bei_e, (*eip)->bei_zseq)) {
827                                 (*eip)->bei_e = NULL;
828                                 (*eip)->bei_zseq = 0;
829                         }
830 #endif
831                         if ( !(*eip)->bei_e && !((*eip)->bei_state & CACHE_ENTRY_LOADING)) {
832                                 load = 1;
833                                 (*eip)->bei_state |= CACHE_ENTRY_LOADING;
834                         }
835
836                         if ( islocked ) {
837                                 bdb_cache_entryinfo_unlock( *eip );
838                                 islocked = 0;
839                         }
840                         rc = bdb_cache_entry_db_lock( bdb, locker, *eip, load, 0, lock );
841                         if ( (*eip)->bei_state & CACHE_ENTRY_DELETED ) {
842                                 rc = DB_NOTFOUND;
843                                 bdb_cache_entry_db_unlock( bdb, lock );
844                         } else if ( rc == 0 ) {
845                                 if ( load ) {
846                                         if ( !ep) {
847                                                 rc = bdb_id2entry( op->o_bd, tid, locker, id, &ep );
848                                         }
849                                         if ( rc == 0 ) {
850                                                 ep->e_private = *eip;
851 #ifdef BDB_HIER
852                                                 bdb_fix_dn( ep, 0 );
853 #endif
854                                                 (*eip)->bei_e = ep;
855 #ifdef SLAP_ZONE_ALLOC
856                                                 (*eip)->bei_zseq = *((ber_len_t *)ep - 2);
857 #endif
858                                                 ep = NULL;
859                                                 bdb_cache_lru_link( bdb, *eip );
860                                         }
861                                         if ( rc == 0 ) {
862                                                 /* If we succeeded, downgrade back to a readlock. */
863                                                 rc = bdb_cache_entry_db_relock( bdb, locker,
864                                                         *eip, 0, 0, lock );
865                                         } else {
866                                                 /* Otherwise, release the lock. */
867                                                 bdb_cache_entry_db_unlock( bdb, lock );
868                                         }
869                                 } else if ( !(*eip)->bei_e ) {
870                                         /* Some other thread is trying to load the entry,
871                                          * wait for it to finish.
872                                          */
873                                         bdb_cache_entry_db_unlock( bdb, lock );
874                                         bdb_cache_entryinfo_lock( *eip );
875                                         islocked = 1;
876                                         goto load1;
877 #ifdef BDB_HIER
878                                 } else {
879                                         /* Check for subtree renames
880                                          */
881                                         rc = bdb_fix_dn( (*eip)->bei_e, 1 );
882                                         if ( rc ) {
883                                                 bdb_cache_entry_db_relock( bdb,
884                                                         locker, *eip, 1, 0, lock );
885                                                 /* check again in case other modifier did it already */
886                                                 if ( bdb_fix_dn( (*eip)->bei_e, 1 ) )
887                                                         rc = bdb_fix_dn( (*eip)->bei_e, 2 );
888                                                 bdb_cache_entry_db_relock( bdb,
889                                                         locker, *eip, 0, 0, lock );
890                                         }
891 #endif
892                                 }
893                                 bdb_cache_entryinfo_lock( *eip );
894                                 (*eip)->bei_finders--;
895                                 if ( load )
896                                         (*eip)->bei_state ^= CACHE_ENTRY_LOADING;
897                                 bdb_cache_entryinfo_unlock( *eip );
898                         }
899                 }
900         }
901         if ( islocked ) {
902                 bdb_cache_entryinfo_unlock( *eip );
903         }
904         if ( ep ) {
905                 ep->e_private = NULL;
906 #ifdef SLAP_ZONE_ALLOC
907                 bdb_entry_return( bdb, ep, (*eip)->bei_zseq );
908 #else
909                 bdb_entry_return( ep );
910 #endif
911         }
912         if ( rc == 0 ) {
913                 int purge = 0;
914
915                 if ( load ) {
916                         ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_count_mutex );
917                         bdb->bi_cache.c_cursize++;
918                         if ( bdb->bi_cache.c_cursize > bdb->bi_cache.c_maxsize &&
919                                 !bdb->bi_cache.c_purging ) {
920                                 purge = 1;
921                                 bdb->bi_cache.c_purging = 1;
922                         }
923                         ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_count_mutex );
924                 }
925                 if ( purge )
926                         bdb_cache_lru_purge( bdb );
927         }
928
929 #ifdef SLAP_ZONE_ALLOC
930         if (rc == 0 && (*eip)->bei_e) {
931                 slap_zn_rlock(bdb->bi_cache.c_zctx, (*eip)->bei_e);
932         }
933         slap_zh_runlock(bdb->bi_cache.c_zctx);
934 #endif
935         return rc;
936 }
937
938 int
939 bdb_cache_children(
940         Operation *op,
941         DB_TXN *txn,
942         Entry *e )
943 {
944         int rc;
945
946         if ( BEI(e)->bei_kids ) {
947                 return 0;
948         }
949         if ( BEI(e)->bei_state & CACHE_ENTRY_NO_KIDS ) {
950                 return DB_NOTFOUND;
951         }
952         rc = bdb_dn2id_children( op, txn, e );
953         if ( rc == DB_NOTFOUND ) {
954                 BEI(e)->bei_state |= CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
955         }
956         return rc;
957 }
958
959 /* Update the cache after a successful database Add. */
960 int
961 bdb_cache_add(
962         struct bdb_info *bdb,
963         EntryInfo *eip,
964         Entry *e,
965         struct berval *nrdn,
966         u_int32_t locker,
967         DB_LOCK *lock )
968 {
969         EntryInfo *new, ei;
970         int rc, purge = 0;
971 #ifdef BDB_HIER
972         struct berval rdn = e->e_name;
973 #endif
974
975         ei.bei_id = e->e_id;
976         ei.bei_parent = eip;
977         ei.bei_nrdn = *nrdn;
978         ei.bei_lockpad = 0;
979
980         /* Lock this entry so that bdb_add can run to completion.
981          * It can only fail if BDB has run out of lock resources.
982          */
983         rc = bdb_cache_entry_db_lock( bdb, locker, &ei, 0, 0, lock );
984         if ( rc ) {
985                 bdb_cache_entryinfo_unlock( eip );
986                 return rc;
987         }
988
989 #ifdef BDB_HIER
990         if ( nrdn->bv_len != e->e_nname.bv_len ) {
991                 char *ptr = ber_bvchr( &rdn, ',' );
992                 assert( ptr != NULL );
993                 rdn.bv_len = ptr - rdn.bv_val;
994         }
995         ber_dupbv( &ei.bei_rdn, &rdn );
996         if ( eip->bei_dkids ) eip->bei_dkids++;
997 #endif
998
999         rc = bdb_entryinfo_add_internal( bdb, &ei, &new );
1000         /* bdb_csn_commit can cause this when adding the database root entry */
1001         if ( new->bei_e ) {
1002                 new->bei_e->e_private = NULL;
1003 #ifdef SLAP_ZONE_ALLOC
1004                 bdb_entry_return( bdb, new->bei_e, new->bei_zseq );
1005 #else
1006                 bdb_entry_return( new->bei_e );
1007 #endif
1008         }
1009         new->bei_e = e;
1010         e->e_private = new;
1011         new->bei_state |= CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
1012         eip->bei_state &= ~CACHE_ENTRY_NO_KIDS;
1013         if (eip->bei_parent) {
1014                 eip->bei_parent->bei_state &= ~CACHE_ENTRY_NO_GRANDKIDS;
1015         }
1016         bdb_cache_entryinfo_unlock( eip );
1017
1018         ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
1019         ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_count_mutex );
1020         ++bdb->bi_cache.c_cursize;
1021         if ( bdb->bi_cache.c_cursize > bdb->bi_cache.c_maxsize &&
1022                 !bdb->bi_cache.c_purging ) {
1023                 purge = 1;
1024                 bdb->bi_cache.c_purging = 1;
1025         }
1026         ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_count_mutex );
1027
1028         bdb_cache_lru_link( bdb, new );
1029
1030         if ( purge )
1031                 bdb_cache_lru_purge( bdb );
1032
1033         return rc;
1034 }
1035
1036 int
1037 bdb_cache_modify(
1038         struct bdb_info *bdb,
1039         Entry *e,
1040         Attribute *newAttrs,
1041         u_int32_t locker,
1042         DB_LOCK *lock )
1043 {
1044         EntryInfo *ei = BEI(e);
1045         int rc;
1046         /* Get write lock on data */
1047         rc = bdb_cache_entry_db_relock( bdb, locker, ei, 1, 0, lock );
1048
1049         /* If we've done repeated mods on a cached entry, then e_attrs
1050          * is no longer contiguous with the entry, and must be freed.
1051          */
1052         if ( ! rc ) {
1053                 if ( (void *)e->e_attrs != (void *)(e+1) ) {
1054                         attrs_free( e->e_attrs ); 
1055                 }
1056                 e->e_attrs = newAttrs;
1057         }
1058         return rc;
1059 }
1060
1061 /*
1062  * Change the rdn in the entryinfo. Also move to a new parent if needed.
1063  */
1064 int
1065 bdb_cache_modrdn(
1066         struct bdb_info *bdb,
1067         Entry *e,
1068         struct berval *nrdn,
1069         Entry *new,
1070         EntryInfo *ein,
1071         u_int32_t locker,
1072         DB_LOCK *lock )
1073 {
1074         EntryInfo *ei = BEI(e), *pei;
1075         int rc;
1076 #ifdef BDB_HIER
1077         struct berval rdn;
1078 #endif
1079
1080         /* Get write lock on data */
1081         rc =  bdb_cache_entry_db_relock( bdb, locker, ei, 1, 0, lock );
1082         if ( rc ) return rc;
1083
1084         /* If we've done repeated mods on a cached entry, then e_attrs
1085          * is no longer contiguous with the entry, and must be freed.
1086          */
1087         if ( (void *)e->e_attrs != (void *)(e+1) ) {
1088                 attrs_free( e->e_attrs );
1089         }
1090         e->e_attrs = new->e_attrs;
1091         if( e->e_nname.bv_val < e->e_bv.bv_val ||
1092                 e->e_nname.bv_val > e->e_bv.bv_val + e->e_bv.bv_len )
1093         {
1094                 ch_free(e->e_name.bv_val);
1095                 ch_free(e->e_nname.bv_val);
1096         }
1097         e->e_name = new->e_name;
1098         e->e_nname = new->e_nname;
1099
1100         /* Lock the parent's kids AVL tree */
1101         pei = ei->bei_parent;
1102         bdb_cache_entryinfo_lock( pei );
1103         avl_delete( &pei->bei_kids, (caddr_t) ei, bdb_rdn_cmp );
1104         free( ei->bei_nrdn.bv_val );
1105         ber_dupbv( &ei->bei_nrdn, nrdn );
1106
1107         if ( !pei->bei_kids )
1108                 pei->bei_state |= CACHE_ENTRY_NO_KIDS | CACHE_ENTRY_NO_GRANDKIDS;
1109
1110 #ifdef BDB_HIER
1111         free( ei->bei_rdn.bv_val );
1112
1113         rdn = e->e_name;
1114         if ( nrdn->bv_len != e->e_nname.bv_len ) {
1115                 char *ptr = ber_bvchr(&rdn, ',');
1116                 assert( ptr != NULL );
1117                 rdn.bv_len = ptr - rdn.bv_val;
1118         }
1119         ber_dupbv( &ei->bei_rdn, &rdn );
1120         pei->bei_ckids--;
1121         if ( pei->bei_dkids ) pei->bei_dkids--;
1122 #endif
1123
1124         if (!ein) {
1125                 ein = ei->bei_parent;
1126         } else {
1127                 ei->bei_parent = ein;
1128                 bdb_cache_entryinfo_unlock( pei );
1129                 bdb_cache_entryinfo_lock( ein );
1130         }
1131         /* parent now has kids */
1132         if ( ein->bei_state & CACHE_ENTRY_NO_KIDS )
1133                 ein->bei_state ^= CACHE_ENTRY_NO_KIDS;
1134
1135 #ifdef BDB_HIER
1136         /* parent might now have grandkids */
1137         if ( ein->bei_state & CACHE_ENTRY_NO_GRANDKIDS &&
1138                 !(ei->bei_state & (CACHE_ENTRY_NO_KIDS)))
1139                 ein->bei_state ^= CACHE_ENTRY_NO_GRANDKIDS;
1140
1141         {
1142                 /* Record the generation number of this change */
1143                 ldap_pvt_thread_mutex_lock( &bdb->bi_modrdns_mutex );
1144                 bdb->bi_modrdns++;
1145                 ei->bei_modrdns = bdb->bi_modrdns;
1146                 ldap_pvt_thread_mutex_unlock( &bdb->bi_modrdns_mutex );
1147         }
1148         ein->bei_ckids++;
1149         if ( ein->bei_dkids ) ein->bei_dkids++;
1150 #endif
1151         avl_insert( &ein->bei_kids, ei, bdb_rdn_cmp, avl_dup_error );
1152         bdb_cache_entryinfo_unlock( ein );
1153         return rc;
1154 }
1155 /*
1156  * cache_delete - delete the entry e from the cache. 
1157  *
1158  * returns:     0       e was deleted ok
1159  *              1       e was not in the cache
1160  *              -1      something bad happened
1161  */
1162 int
1163 bdb_cache_delete(
1164         struct bdb_info *bdb,
1165     Entry               *e,
1166     u_int32_t   locker,
1167     DB_LOCK     *lock )
1168 {
1169         EntryInfo *ei = BEI(e);
1170         int     rc;
1171
1172         assert( e->e_private != NULL );
1173
1174         /* Set this early, warn off any queriers */
1175         ei->bei_state |= CACHE_ENTRY_DELETED;
1176
1177         /* Lock the entry's info */
1178         bdb_cache_entryinfo_lock( ei );
1179
1180         /* Get write lock on the data */
1181         rc = bdb_cache_entry_db_relock( bdb, locker, ei, 1, 0, lock );
1182         if ( rc ) {
1183                 /* couldn't lock, undo and give up */
1184                 ei->bei_state ^= CACHE_ENTRY_DELETED;
1185                 bdb_cache_entryinfo_unlock( ei );
1186                 return rc;
1187         }
1188
1189         Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_delete( %ld )\n",
1190                 e->e_id, 0, 0 );
1191
1192         /* set lru mutex */
1193         ldap_pvt_thread_mutex_lock( &bdb->bi_cache.c_lru_mutex );
1194
1195         rc = bdb_cache_delete_internal( &bdb->bi_cache, e->e_private, 1 );
1196
1197         /* free lru mutex */
1198         ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.c_lru_mutex );
1199
1200         /* Leave entry info locked */
1201
1202         return( rc );
1203 }
1204
1205 void
1206 bdb_cache_delete_cleanup(
1207         Cache *cache,
1208         EntryInfo *ei )
1209 {
1210         if ( ei->bei_e ) {
1211                 ei->bei_e->e_private = NULL;
1212 #ifdef SLAP_ZONE_ALLOC
1213                 bdb_entry_return( ei->bei_bdb, ei->bei_e, ei->bei_zseq );
1214 #else
1215                 bdb_entry_return( ei->bei_e );
1216 #endif
1217                 ei->bei_e = NULL;
1218         }
1219
1220         bdb_cache_entryinfo_free( cache, ei );
1221         bdb_cache_entryinfo_unlock( ei );
1222 }
1223
1224 static int
1225 bdb_cache_delete_internal(
1226     Cache       *cache,
1227     EntryInfo           *e,
1228     int         decr )
1229 {
1230         int rc = 0;     /* return code */
1231         int decr_leaf = 0;
1232
1233         /* Lock the parent's kids tree */
1234         bdb_cache_entryinfo_lock( e->bei_parent );
1235
1236 #ifdef BDB_HIER
1237         e->bei_parent->bei_ckids--;
1238         if ( decr && e->bei_parent->bei_dkids ) e->bei_parent->bei_dkids--;
1239 #endif
1240         /* dn tree */
1241         if ( avl_delete( &e->bei_parent->bei_kids, (caddr_t) e, bdb_rdn_cmp )
1242                 == NULL )
1243         {
1244                 rc = -1;
1245         }
1246         if ( e->bei_parent->bei_kids )
1247                 decr_leaf = 1;
1248
1249         bdb_cache_entryinfo_unlock( e->bei_parent );
1250
1251         ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
1252         /* id tree */
1253         if ( avl_delete( &cache->c_idtree, (caddr_t) e, bdb_id_cmp )) {
1254                 cache->c_eiused--;
1255                 if ( decr_leaf )
1256                         cache->c_leaves--;
1257         } else {
1258                 rc = -1;
1259         }
1260         ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1261
1262         if ( rc == 0 ){
1263                 /* lru */
1264                 LRU_DEL( cache, e );
1265
1266                 if ( e->bei_e ) {
1267                         ldap_pvt_thread_mutex_lock( &cache->c_count_mutex );
1268                         cache->c_cursize--;
1269                         ldap_pvt_thread_mutex_unlock( &cache->c_count_mutex );
1270                 }
1271         }
1272
1273         return( rc );
1274 }
1275
1276 static void
1277 bdb_entryinfo_release( void *data )
1278 {
1279         EntryInfo *ei = (EntryInfo *)data;
1280         if ( ei->bei_kids ) {
1281                 avl_free( ei->bei_kids, NULL );
1282         }
1283         if ( ei->bei_e ) {
1284                 ei->bei_e->e_private = NULL;
1285 #ifdef SLAP_ZONE_ALLOC
1286                 bdb_entry_return( ei->bei_bdb, ei->bei_e, ei->bei_zseq );
1287 #else
1288                 bdb_entry_return( ei->bei_e );
1289 #endif
1290         }
1291         bdb_cache_entryinfo_destroy( ei );
1292 }
1293
1294 void
1295 bdb_cache_release_all( Cache *cache )
1296 {
1297         /* set cache write lock */
1298         ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
1299         /* set lru mutex */
1300         ldap_pvt_thread_mutex_lock( &cache->c_lru_mutex );
1301
1302         Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_release_all\n", 0, 0, 0 );
1303
1304         avl_free( cache->c_dntree.bei_kids, NULL );
1305         avl_free( cache->c_idtree, bdb_entryinfo_release );
1306         for (;cache->c_eifree;cache->c_eifree = cache->c_lruhead) {
1307                 cache->c_lruhead = cache->c_eifree->bei_lrunext;
1308                 bdb_cache_entryinfo_destroy(cache->c_eifree);
1309         }
1310         cache->c_cursize = 0;
1311         cache->c_eiused = 0;
1312         cache->c_leaves = 0;
1313         cache->c_idtree = NULL;
1314         cache->c_lruhead = NULL;
1315         cache->c_lrutail = NULL;
1316         cache->c_dntree.bei_kids = NULL;
1317
1318         /* free lru mutex */
1319         ldap_pvt_thread_mutex_unlock( &cache->c_lru_mutex );
1320         /* free cache write lock */
1321         ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
1322 }
1323
1324 #ifdef LDAP_DEBUG
1325 #ifdef SLAPD_UNUSED
1326 static void
1327 bdb_lru_print( Cache *cache )
1328 {
1329         EntryInfo       *e;
1330
1331         fprintf( stderr, "LRU circle head: %p\n", cache->c_lruhead );
1332         fprintf( stderr, "LRU circle (tail forward):\n" );
1333         for ( e = cache->c_lrutail; ; ) {
1334                 fprintf( stderr, "\t%p, %p id %ld rdn \"%s\"\n",
1335                         e, e->bei_e, e->bei_id, e->bei_nrdn.bv_val );
1336                 e = e->bei_lrunext;
1337                 if ( e == cache->c_lrutail )
1338                         break;
1339         }
1340         fprintf( stderr, "LRU circle (tail backward):\n" );
1341         for ( e = cache->c_lrutail; ; ) {
1342                 fprintf( stderr, "\t%p, %p id %ld rdn \"%s\"\n",
1343                         e, e->bei_e, e->bei_id, e->bei_nrdn.bv_val );
1344                 e = e->bei_lruprev;
1345                 if ( e == cache->c_lrutail )
1346                         break;
1347         }
1348 }
1349 #endif
1350 #endif
1351
1352 #ifdef BDB_REUSE_LOCKERS
1353 static void
1354 bdb_locker_id_free( void *key, void *data )
1355 {
1356         DB_ENV *env = key;
1357         u_int32_t lockid = (long)data;
1358         int rc;
1359
1360         rc = XLOCK_ID_FREE( env, lockid );
1361         if ( rc == EINVAL ) {
1362                 DB_LOCKREQ lr;
1363                 Debug( LDAP_DEBUG_ANY,
1364                         "bdb_locker_id_free: %lu err %s(%d)\n",
1365                         (unsigned long) lockid, db_strerror(rc), rc );
1366                 /* release all locks held by this locker. */
1367                 lr.op = DB_LOCK_PUT_ALL;
1368                 lr.obj = NULL;
1369                 env->lock_vec( env, lockid, 0, &lr, 1, NULL );
1370                 XLOCK_ID_FREE( env, lockid );
1371         }
1372 }
1373
1374 /* free up any keys used by the main thread */
1375 void
1376 bdb_locker_flush( DB_ENV *env )
1377 {
1378         void *data;
1379         void *ctx = ldap_pvt_thread_pool_context();
1380
1381         if ( !ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
1382                 ldap_pvt_thread_pool_setkey( ctx, env, NULL, NULL );
1383                 bdb_locker_id_free( env, data );
1384         }
1385 }
1386
1387 int
1388 bdb_locker_id( Operation *op, DB_ENV *env, u_int32_t *locker )
1389 {
1390         int i, rc;
1391         u_int32_t lockid;
1392         void *data;
1393         void *ctx;
1394
1395         if ( !env || !locker ) return -1;
1396
1397         /* If no op was provided, try to find the ctx anyway... */
1398         if ( op ) {
1399                 ctx = op->o_threadctx;
1400         } else {
1401                 ctx = ldap_pvt_thread_pool_context();
1402         }
1403
1404         /* Shouldn't happen unless we're single-threaded */
1405         if ( !ctx ) {
1406                 *locker = 0;
1407                 return 0;
1408         }
1409
1410         if ( ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
1411                 for ( i=0, rc=1; rc != 0 && i<4; i++ ) {
1412                         rc = XLOCK_ID( env, &lockid );
1413                         if (rc) ldap_pvt_thread_yield();
1414                 }
1415                 if ( rc != 0) {
1416                         return rc;
1417                 }
1418                 data = (void *)((long)lockid);
1419                 if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, env,
1420                         data, bdb_locker_id_free ) ) ) {
1421                         XLOCK_ID_FREE( env, lockid );
1422                         Debug( LDAP_DEBUG_ANY, "bdb_locker_id: err %s(%d)\n",
1423                                 db_strerror(rc), rc, 0 );
1424
1425                         return rc;
1426                 }
1427         } else {
1428                 lockid = (long)data;
1429         }
1430         *locker = lockid;
1431         return 0;
1432 }
1433 #endif /* BDB_REUSE_LOCKERS */