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