]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/cache.c
Hierarchical cache management.
[openldap] / servers / slapd / back-bdb / cache.c
1 /* cache.c - routines to maintain an in-core cache of entries */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/errno.h>
13 #include <ac/string.h>
14 #include <ac/socket.h>
15
16 #include "slap.h"
17
18 #include "back-bdb.h"
19
20 static int      bdb_cache_delete_entry_internal(Cache *cache, EntryInfo *e);
21 #ifdef LDAP_DEBUG
22 static void     bdb_lru_print(Cache *cache);
23 #endif
24
25 static EntryInfo *
26 bdb_cache_entryinfo_new( )
27 {
28         EntryInfo *ei;
29
30         ei = ch_calloc(1, sizeof(struct bdb_entry_info));
31         ldap_pvt_thread_mutex_init( &ei->bei_kids_mutex );
32
33         return ei;
34 }
35
36 /* Atomically release and reacquire a lock */
37 int
38 bdb_cache_entry_db_relock(
39         DB_ENV *env,
40         u_int32_t locker,
41         EntryInfo *ei,
42         int rw,
43         int tryOnly,
44         DB_LOCK *lock )
45 {
46 #ifdef NO_THREADS
47         return 0;
48 #else
49         int     rc;
50         DBT     lockobj;
51         DB_LOCKREQ list[2];
52
53         lockobj.data = ei;
54         lockobj.size = sizeof(ei->bei_parent) + sizeof(ei->bei_id);
55
56         list[0].op = DB_LOCK_PUT;
57         list[0].lock = *lock;
58         list[1].op = DB_LOCK_GET;
59         list[1].lock = *lock;
60         list[1].mode = rw ? DB_LOCK_WRITE : DB_LOCK_READ;
61         list[1].obj = &lockobj;
62         rc = env->lock_vec(env, locker, tryOnly ? DB_LOCK_NOWAIT : 0,
63                 list, 2, NULL );
64
65         if (rc) {
66 #ifdef NEW_LOGGING
67                 LDAP_LOG( CACHE, DETAIL1, 
68                         "bdb_cache_entry_db_relock: entry %d, rw %d, rc %d\n",
69                         ei->bei_id, rw, rc );
70 #else
71                 Debug( LDAP_DEBUG_TRACE,
72                         "bdb_cache_entry_db_relock: entry %d, rw %d, rc %d\n",
73                         ei->bei_id, rw, rc );
74 #endif
75         } else {
76                 *lock = list[1].lock;
77         }
78         return rc;
79 #endif
80 }
81 int
82 bdb_cache_entry_db_lock
83 ( DB_ENV *env, u_int32_t locker, EntryInfo *ei, int rw, int tryOnly, DB_LOCK *lock )
84 {
85 #ifdef NO_THREADS
86         return 0;
87 #else
88         int       rc;
89         DBT       lockobj;
90         int       db_rw;
91
92         if (rw)
93                 db_rw = DB_LOCK_WRITE;
94         else
95                 db_rw = DB_LOCK_READ;
96
97         lockobj.data = ei;
98         lockobj.size = sizeof(ei->bei_parent) + sizeof(ei->bei_id);
99
100         rc = LOCK_GET(env, locker, tryOnly ? DB_LOCK_NOWAIT : 0,
101                                         &lockobj, db_rw, lock);
102         if (rc) {
103 #ifdef NEW_LOGGING
104                 LDAP_LOG( CACHE, DETAIL1, 
105                         "bdb_cache_entry_db_lock: entry %d, rw %d, rc %d\n",
106                         ei->bei_id, rw, rc );
107 #else
108                 Debug( LDAP_DEBUG_TRACE,
109                         "bdb_cache_entry_db_lock: entry %d, rw %d, rc %d\n",
110                         ei->bei_id, rw, rc );
111 #endif
112         }
113         return rc;
114 #endif /* NO_THREADS */
115 }
116
117 int
118 bdb_cache_entry_db_unlock
119 ( DB_ENV *env, DB_LOCK *lock )
120 {
121 #ifdef NO_THREADS
122         return 0;
123 #else
124         int rc;
125
126         rc = LOCK_PUT ( env, lock );
127         return rc;
128 #endif
129 }
130
131 static int
132 bdb_cache_entryinfo_destroy( EntryInfo *e )
133 {
134         ldap_pvt_thread_mutex_destroy( &e->bei_kids_mutex );
135         free( e->bei_nrdn.bv_val );
136         free( e );
137         return 0;
138 }
139
140 #define LRU_DELETE( cache, ei ) do { \
141         if ( (ei)->bei_lruprev != NULL ) { \
142                 (ei)->bei_lruprev->bei_lrunext = (ei)->bei_lrunext; \
143         } else { \
144                 (cache)->c_lruhead = (ei)->bei_lrunext; \
145         } \
146         if ( (ei)->bei_lrunext != NULL ) { \
147                 (ei)->bei_lrunext->bei_lruprev = (ei)->bei_lruprev; \
148         } else { \
149                 (cache)->c_lrutail = (ei)->bei_lruprev; \
150         } \
151 } while(0)
152
153 #define LRU_ADD( cache, ei ) do { \
154         (ei)->bei_lrunext = (cache)->c_lruhead; \
155         if ( (ei)->bei_lrunext != NULL ) { \
156                 (ei)->bei_lrunext->bei_lruprev = (ei); \
157         } \
158         (cache)->c_lruhead = (ei); \
159         (ei)->bei_lruprev = NULL; \
160         if ( (cache)->c_lrutail == NULL ) { \
161                 (cache)->c_lrutail = (ei); \
162         } \
163 } while(0)
164
165 /* Do a lexical sort on normalized RDNs */
166 static int
167 bdb_rdn_cmp( const void *v_e1, const void *v_e2 )
168 {
169         const EntryInfo *e1 = v_e1, *e2 = v_e2;
170         int rc = strncmp( e1->bei_nrdn.bv_val, e2->bei_nrdn.bv_val, e1->bei_nrdn.bv_len );
171         if (rc == 0) rc = e1->bei_nrdn.bv_len - e2->bei_nrdn.bv_len;
172         return rc;
173 }
174
175 static int
176 bdb_id_cmp( const void *v_e1, const void *v_e2 )
177 {
178         const EntryInfo *e1 = v_e1, *e2 = v_e2;
179         return e1->bei_id - e2->bei_id;
180 }
181
182 /* Create an entryinfo in the cache. Caller must release the locks later.
183  */
184 int
185 bdb_entryinfo_add_internal(
186         struct bdb_info *bdb,
187         EntryInfo *eip,
188         ID id,
189         struct berval *nrdn,
190         EntryInfo **res,
191         u_int32_t locker
192 )
193 {
194         Cache *cache = &bdb->bi_cache;
195         DB_ENV *env = bdb->bi_dbenv;
196         EntryInfo *ei2 = NULL;
197         int incr = 1;
198         int addkid = 1;
199         int rc;
200         DB_LOCK lock;
201
202         *res = NULL;
203
204         ldap_pvt_thread_rdwr_wlock( &bdb->bi_cache.c_rwlock );
205         bdb_cache_entryinfo_lock( eip );
206
207         /* if parent was previously considered a leaf node,
208          * it was on the LRU list. Now it's going to have
209          * kids, take it off the LRU list.
210          */
211         ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
212         if ( eip->bei_id && !eip->bei_kids ) {
213                 LRU_DELETE( cache, eip );
214                 incr = 0;
215         }
216
217         cache->c_cursize += incr;
218
219         /* See if we're above the cache size limit */
220         if ( cache->c_cursize > cache->c_maxsize ) {
221                 EntryInfo *elru, *elprev;
222                 int i = 0;
223
224                 /* Look for an unused entry to remove */
225                 for (elru = cache->c_lrutail; elru; elru = elprev, i++ ) {
226                         elprev = elru->bei_lruprev;
227
228                         /* Too many probes, not enough idle, give up */
229                         if (i > 10) break;
230
231                         /* If we can successfully writelock it, then
232                          * the object is idle.
233                          */
234                         if ( bdb_cache_entry_db_lock( env, locker, elru, 1, 1,
235                                 &lock ) == 0 ) {
236                                 /* Need to lock parent to delete child */
237                                 if ( ldap_pvt_thread_mutex_trylock(
238                                         &elru->bei_parent->bei_kids_mutex )) {
239                                         bdb_cache_entry_db_unlock( env, &lock );
240                                         continue;
241                                 }
242                                 bdb_cache_delete_entry_internal( cache, elru );
243                                 bdb_cache_entryinfo_unlock( elru->bei_parent );
244                                 elru->bei_e->e_private = NULL;
245                                 bdb_entry_return( elru->bei_e );
246                                 bdb_cache_entry_db_unlock( env, &lock );
247                                 if (ei2) {
248                                         bdb_cache_entryinfo_destroy( elru );
249                                 } else {
250                                         /* re-use this one */
251                                         ch_free(elru->bei_nrdn.bv_val);
252                                         elru->bei_nrdn.bv_val = NULL;
253                                         elru->bei_e = NULL;
254                                         elru->bei_kids = NULL;
255                                         elru->bei_lrunext = NULL;
256                                         elru->bei_lruprev = NULL;
257                                         elru->bei_state = 0;
258                                         ei2 = elru;
259                                 }
260                                 if (cache->c_cursize < cache->c_maxsize)
261                                         break;
262                         }
263                 }
264         }
265         if (!ei2) {
266                 ei2 = bdb_cache_entryinfo_new();
267         }
268         ei2->bei_id = id;
269         ei2->bei_parent = eip;
270
271         /* Add to cache ID tree */
272         if (avl_insert( &cache->c_idtree, ei2, bdb_id_cmp, avl_dup_error )) {
273                 EntryInfo *ei;
274                 ei = avl_find( cache->c_idtree, ei2, bdb_id_cmp );
275                 bdb_cache_entryinfo_destroy( ei2 );
276                 ei2 = ei;
277                 addkid = 0;
278                 cache->c_cursize -= incr;
279         } else {
280                 LRU_ADD( cache, ei2 );
281                 ber_dupbv( &ei2->bei_nrdn, nrdn );
282         }
283
284         if ( addkid ) {
285                 avl_insert( &eip->bei_kids, ei2, bdb_rdn_cmp, avl_dup_error );
286         }
287
288         ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
289
290 #if 0 /* caller must do these frees */
291         ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
292         bdb_cache_entryinfo_unlock( eip );
293 #endif
294
295         *res = ei2;
296         return 0;
297 }
298
299 /* Find the EntryInfo for the requested DN. If the DN cannot be found, return
300  * the info for its closest ancestor. *res should be NULL to process a
301  * complete DN starting from the tree root. Otherwise *res must be the
302  * immediate parent of the requested DN, and only the RDN will be searched.
303  * The EntryInfo is locked upon return and must be unlocked by the caller.
304  */
305 int
306 bdb_cache_find_entry_ndn2id(
307         Backend         *be,
308         DB_TXN          *txn,
309         struct berval   *ndn,
310         EntryInfo       **res,
311         u_int32_t       locker,
312         void            *ctx
313 )
314 {
315         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
316         EntryInfo       ei, *eip, *ei2;
317         ID                      id;
318         int rc = 0;
319         char *ptr;
320
321         /* this function is always called with normalized DN */
322         if ( *res ) {
323                 /* we're doing a onelevel search for an RDN */
324                 ei.bei_nrdn.bv_val = ndn->bv_val;
325                 ei.bei_nrdn.bv_len = dn_rdnlen( be, ndn );
326                 eip = *res;
327         } else {
328                 /* we're searching a full DN from the root */
329                 ptr = ndn->bv_val + ndn->bv_len - be->be_nsuffix[0].bv_len;
330                 ei.bei_nrdn.bv_val = ptr;
331                 ei.bei_nrdn.bv_len = be->be_nsuffix[0].bv_len;
332                 eip = &bdb->bi_cache.c_dntree;
333         }
334         
335         for ( bdb_cache_entryinfo_lock( eip ); eip; ) {
336                 ei2 = (EntryInfo *)avl_find( eip->bei_kids, &ei, bdb_rdn_cmp );
337                 if ( !ei2 ) {
338                         int len = ei.bei_nrdn.bv_len;
339                                 
340                         ei.bei_nrdn.bv_len = ndn->bv_len - (ei.bei_nrdn.bv_val - ndn->bv_val);
341                         bdb_cache_entryinfo_unlock( eip );
342
343                         rc = bdb_dn2id( be, txn, &ei.bei_nrdn, &id, ctx );
344                         if (rc) {
345                                 bdb_cache_entryinfo_lock( eip );
346                                 *res = eip;
347                                 return rc;
348                         }
349
350                         /* DN exists but needs to be added to cache */
351                         ei.bei_nrdn.bv_len = len;
352                         rc = bdb_entryinfo_add_internal( bdb,
353                                 eip, id, &ei.bei_nrdn, &ei2, locker );
354                         /* add_internal left eip and c_rwlock locked */
355                         ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
356                         if ( rc ) {
357                                 *res = eip;
358                                 return rc;
359                         }
360                 } else if ( ei2->bei_state == CACHE_ENTRY_DELETED ) {
361                         /* In the midst of deleting? Give it a chance to
362                          * complete.
363                          */
364                         bdb_cache_entryinfo_unlock( eip );
365                         ldap_pvt_thread_yield();
366                         bdb_cache_entryinfo_lock( eip );
367                         *res = eip;
368                         return DB_NOTFOUND;
369                 }
370                 bdb_cache_entryinfo_unlock( eip );
371                 bdb_cache_entryinfo_lock( ei2 );
372
373                 eip = ei2;
374
375                 /* Advance to next lower RDN */
376                 for (ptr = ei.bei_nrdn.bv_val - 2; ptr > ndn->bv_val
377                         && !DN_SEPARATOR(*ptr); ptr--);
378                 if ( ptr >= ndn->bv_val ) {
379                         if (DN_SEPARATOR(*ptr)) ptr++;
380                         ei.bei_nrdn.bv_len = ei.bei_nrdn.bv_val - ptr - 1;
381                         ei.bei_nrdn.bv_val = ptr;
382                 }
383                 if ( ptr < ndn->bv_val ) {
384                         *res = eip;
385                         break;
386                 }
387         }
388
389         return rc;
390 }
391
392 /*
393  * cache_find_entry_id - find an entry in the cache, given id.
394  * The entry is locked for Read upon return. Call with islocked TRUE if
395  * the supplied *eip was already locked.
396  */
397
398 int
399 bdb_cache_find_entry_id(
400         Backend *be,
401         DB_TXN  *tid,
402         ID                              id,
403         EntryInfo       **eip,
404         int             islocked,
405         u_int32_t       locker,
406         DB_LOCK         *lock,
407         void            *ctx
408 )
409 {
410         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
411         Entry   *ep = NULL;
412         int     rc = 0;
413         EntryInfo ei;
414
415         ei.bei_id = id;
416
417         /* If we weren't given any info, see if we have it already cached */
418         if ( !*eip ) {
419                 ldap_pvt_thread_rdwr_rlock( &bdb->bi_cache.c_rwlock );
420                 *eip = (EntryInfo *) avl_find( bdb->bi_cache.c_idtree,
421                                         (caddr_t) &ei, bdb_id_cmp );
422                 if ( *eip ) {
423                         bdb_cache_entryinfo_lock( *eip );
424                         islocked = 1;
425                 }
426                 ldap_pvt_thread_rdwr_runlock( &bdb->bi_cache.c_rwlock );
427         }
428
429         /* See if the ID exists in the database; add it to the cache if so */
430         if ( !*eip ) {
431                 rc = bdb_id2entry( be, tid, id, &ep );
432                 if ( rc == 0 ) {
433                         rc = bdb_cache_find_entry_ndn2id( be, tid,
434                                 &ep->e_nname, eip, locker, ctx );
435                         if ( *eip )
436                                 islocked = 1;
437                         if ( rc ) {
438                                 bdb_entry_return( ep );
439                                 ep = NULL;
440                         }
441                 }
442         }
443
444         /* Ok, we found the info, do we have the entry? */
445         if ( *eip && rc == 0 ) {
446                 if ( (*eip)->bei_state == CACHE_ENTRY_DELETED ) {
447                         rc = DB_NOTFOUND;
448                 } else if (!(*eip)->bei_e ) {
449                         if (!ep) {
450                                 rc = bdb_id2entry( be, tid, id, &ep );
451                         }
452                         if ( rc == 0 ) {
453                                 bdb_cache_entry_db_lock( bdb->bi_dbenv, locker,
454                                         *eip, 1, 0, lock );
455                                 (*eip)->bei_e = ep;
456                                 ep->e_private = *eip;
457                                 bdb_cache_entry_db_relock( bdb->bi_dbenv, locker,
458                                         *eip, 0, 0, lock );
459                         }
460                 } else {
461                         bdb_cache_entry_db_lock( bdb->bi_dbenv, locker,
462                                         *eip, 0, 0, lock );
463                 }
464         }
465         if ( rc == 0 && (*eip)->bei_kids == NULL ) {
466                 /* set lru mutex */
467                 ldap_pvt_thread_mutex_lock( &bdb->bi_cache.lru_mutex );
468                 LRU_DELETE( &bdb->bi_cache, *eip );
469                 LRU_ADD( &bdb->bi_cache, *eip );
470                 ldap_pvt_thread_mutex_unlock( &bdb->bi_cache.lru_mutex );
471         }
472
473         if ( islocked ) {
474                 bdb_cache_entryinfo_unlock( *eip );
475         }
476         return rc;
477 }
478
479 /* Update the cache after a successful database Add. */
480 int
481 bdb_cache_add(
482         struct bdb_info *bdb,
483         EntryInfo *ei,
484         Entry *e,
485         struct berval *nrdn,
486         u_int32_t locker
487 )
488 {
489         EntryInfo *new;
490         int rc;
491
492         rc = bdb_entryinfo_add_internal( bdb, ei, e->e_id, nrdn, &new, locker );
493         new->bei_e = e;
494         e->e_private = new;
495         bdb_cache_entryinfo_unlock( ei );
496         ldap_pvt_thread_rdwr_wunlock( &bdb->bi_cache.c_rwlock );
497         return rc;
498 }
499
500 int
501 bdb_cache_modify(
502         Entry *e,
503         Attribute *newAttrs,
504         DB_ENV *env,
505         u_int32_t locker,
506         DB_LOCK *lock
507 )
508 {
509         EntryInfo *ei = BEI(e);
510         
511         /* Get write lock on data */
512         bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
513
514         /* If we've done repeated mods on a cached entry, then e_attrs
515          * is no longer contiguous with the entry, and must be freed.
516          */
517         if ( (void *)e->e_attrs != (void *)(e+1) ) {
518                 attrs_free( e->e_attrs );
519         }
520         e->e_attrs = newAttrs;
521
522         return 0;
523 }
524
525 /*
526  * Change the rdn in the entryinfo. Also move to a new parent if needed.
527  */
528 int
529 bdb_cache_modrdn(
530         Entry *e,
531         struct berval *nrdn,
532         Entry *new,
533         EntryInfo *ein,
534         DB_ENV *env,
535         u_int32_t locker,
536         DB_LOCK *lock
537 )
538 {
539         EntryInfo *ei = BEI(e), *pei;
540         int rc = 0;
541
542         /* Get write lock on data */
543         bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
544
545         /* If we've done repeated mods on a cached entry, then e_attrs
546          * is no longer contiguous with the entry, and must be freed.
547          */
548         if ( (void *)e->e_attrs != (void *)(e+1) ) {
549                 attrs_free( e->e_attrs );
550         }
551         e->e_attrs = new->e_attrs;
552 #ifdef BDB_HIER
553         ch_free(e->e_name.bv_val);
554 #else
555         if( e->e_nname.bv_val < e->e_bv.bv_val || e->e_nname.bv_val >
556                 e->e_bv.bv_val + e->e_bv.bv_len ) {
557                 ch_free(e->e_name.bv_val);
558                 ch_free(e->e_nname.bv_val);
559         }
560 #endif
561         e->e_name = new->e_name;
562         e->e_nname = new->e_nname;
563
564         /* Lock the parent's kids AVL tree */
565         pei = ei->bei_parent;
566         bdb_cache_entryinfo_lock( pei );
567         avl_delete( &pei->bei_kids, (caddr_t) ei, bdb_rdn_cmp );
568         free( ei->bei_nrdn.bv_val );
569         ber_dupbv( &ei->bei_nrdn, nrdn );
570         if (!ein) {
571                 ein = ei->bei_parent;
572         } else {
573                 ei->bei_parent = ein;
574                 bdb_cache_entryinfo_unlock( pei );
575                 bdb_cache_entryinfo_lock( ein );
576         }
577         avl_insert( &ein->bei_kids, ei, bdb_rdn_cmp, avl_dup_error );
578         bdb_cache_entryinfo_unlock( ein );
579         return rc;
580 }
581 /*
582  * cache_delete_entry - delete the entry e from the cache. 
583  *
584  * returns:     0       e was deleted ok
585  *              1       e was not in the cache
586  *              -1      something bad happened
587  */
588 int
589 bdb_cache_delete_entry(
590     Cache       *cache,
591     Entry               *e,
592     DB_ENV      *env,
593     u_int32_t   locker,
594     DB_LOCK     *lock
595 )
596 {
597         EntryInfo *ei = BEI(e);
598         int     rc;
599
600         assert( e->e_private );
601
602         /* Set this early, warn off any queriers */
603         ei->bei_state = CACHE_ENTRY_DELETED;
604
605         /* Get write lock on the data */
606         bdb_cache_entry_db_relock( env, locker, ei, 1, 0, lock );
607
608         /* set cache write lock */
609         ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
610
611         /* Lock the parent's kids tree */
612         bdb_cache_entryinfo_lock( ei->bei_parent );
613
614 #ifdef NEW_LOGGING
615         LDAP_LOG( CACHE, ENTRY, 
616                 "bdb_cache_delete_entry: delete %ld.\n", e->e_id, 0, 0 );
617 #else
618         Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_delete_entry( %ld )\n",
619                 e->e_id, 0, 0 );
620 #endif
621
622         /* set lru mutex */
623         ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
624         rc = bdb_cache_delete_entry_internal( cache, e->e_private );
625         /* free lru mutex */
626         ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
627
628         /* free cache write lock */
629         ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
630         bdb_cache_entryinfo_unlock( ei->bei_parent );
631         bdb_cache_entryinfo_destroy( ei );
632         e->e_private = NULL;
633         return( rc );
634 }
635
636 static int
637 bdb_cache_delete_entry_internal(
638     Cache       *cache,
639     EntryInfo           *e
640 )
641 {
642         int rc = 0;     /* return code */
643
644         /* dn tree */
645         if ( avl_delete( &e->bei_parent->bei_kids, (caddr_t) e, bdb_rdn_cmp ) == NULL )
646         {
647                 rc = -1;
648         }
649
650         /* If parent has no more kids, put in on LRU list */
651         if ( e->bei_parent->bei_kids == NULL ) {
652                 LRU_ADD( cache, e->bei_parent );
653                 cache->c_cursize++;
654         }
655
656         /* id tree */
657         if ( avl_delete( &cache->c_idtree, (caddr_t) e, bdb_id_cmp ) == NULL )
658         {
659                 rc = -1;
660         }
661
662         if (rc != 0) {
663                 return rc;
664         }
665
666         /* lru */
667         LRU_DELETE( cache, e );
668         cache->c_cursize--;
669
670         /*
671          * flag entry to be freed later by a call to cache_return_entry()
672          */
673         e->bei_state = CACHE_ENTRY_DELETED;
674
675         return( 0 );
676 }
677
678 static void
679 bdb_entryinfo_release( void *data )
680 {
681         EntryInfo *ei = (EntryInfo *)data;
682         avl_free( ei->bei_kids, NULL );
683         if ( ei->bei_e ) {
684                 ei->bei_e->e_private = NULL;
685                 bdb_entry_return( ei->bei_e );
686         }
687         bdb_cache_entryinfo_destroy( ei );
688 }
689
690 void
691 bdb_cache_release_all( Cache *cache )
692 {
693         /* set cache write lock */
694         ldap_pvt_thread_rdwr_wlock( &cache->c_rwlock );
695         /* set lru mutex */
696         ldap_pvt_thread_mutex_lock( &cache->lru_mutex );
697
698 #ifdef NEW_LOGGING
699         LDAP_LOG( CACHE, ENTRY, "bdb_cache_release_all: enter\n", 0, 0, 0 );
700 #else
701         Debug( LDAP_DEBUG_TRACE, "====> bdb_cache_release_all\n", 0, 0, 0 );
702 #endif
703
704         avl_free( cache->c_dntree.bei_kids, NULL );
705         avl_free( cache->c_idtree, bdb_entryinfo_release );
706         cache->c_lruhead = NULL;
707         cache->c_lrutail = NULL;
708
709         /* free lru mutex */
710         ldap_pvt_thread_mutex_unlock( &cache->lru_mutex );
711         /* free cache write lock */
712         ldap_pvt_thread_rdwr_wunlock( &cache->c_rwlock );
713 }
714
715 #ifdef LDAP_DEBUG
716 static void
717 bdb_lru_print( Cache *cache )
718 {
719         EntryInfo       *e;
720
721         fprintf( stderr, "LRU queue (head to tail):\n" );
722         for ( e = cache->c_lruhead; e != NULL; e = e->bei_lrunext ) {
723                 fprintf( stderr, "\trdn \"%20s\" id %ld\n",
724                         e->bei_nrdn.bv_val, e->bei_id );
725         }
726         fprintf( stderr, "LRU queue (tail to head):\n" );
727         for ( e = cache->c_lrutail; e != NULL; e = e->bei_lruprev ) {
728                 fprintf( stderr, "\trdn \"%20s\" id %ld\n",
729                         e->bei_nrdn.bv_val, e->bei_id );
730         }
731 }
732 #endif
733
734 #ifdef BDB_REUSE_LOCKERS
735 void
736 bdb_locker_id_free( void *key, void *data )
737 {
738         DB_ENV *env = key;
739         int lockid = (int) data;
740
741         XLOCK_ID_FREE( env, lockid );
742 }
743
744 int
745 bdb_locker_id( Operation *op, DB_ENV *env, int *locker )
746 {
747         int i, rc, lockid;
748         void *data;
749         void *ctx;
750
751         if ( !env || !locker ) return -1;
752
753         /* If no op was provided, try to find the ctx anyway... */
754         if ( op ) {
755                 ctx = op->o_threadctx;
756         } else {
757                 ctx = ldap_pvt_thread_pool_context();
758         }
759
760         /* Shouldn't happen unless we're single-threaded */
761         if ( !ctx ) {
762                 *locker = 0;
763                 return 0;
764         }
765
766         if ( ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
767                 for ( i=0, rc=1; rc != 0 && i<4; i++ ) {
768                         rc = XLOCK_ID( env, &lockid );
769                         if (rc) ldap_pvt_thread_yield();
770                 }
771                 if ( rc != 0) {
772                         return rc;
773                 }
774                 data = (void *)lockid;
775                 if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, env,
776                         data, bdb_locker_id_free ) ) ) {
777                         XLOCK_ID_FREE( env, lockid );
778 #ifdef NEW_LOGGING
779                         LDAP_LOG( BACK_BDB, ERR, "bdb_locker_id: err %s(%d)\n",
780                                 db_strerror(rc), rc, 0 );
781 #else
782                         Debug( LDAP_DEBUG_ANY, "bdb_locker_id: err %s(%d)\n",
783                                 db_strerror(rc), rc, 0 );
784 #endif
785
786                         return rc;
787                 }
788         } else {
789                 lockid = (int)data;
790         }
791         *locker = lockid;
792         return 0;
793 }
794 #endif